查看: 1857|回复: 0

[基础] linux生成设备节点

[复制链接]
  • TA的每日心情
    开心
    2015-7-14 09:10
  • 签到天数: 9 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2017-8-24 16:47:23 | 显示全部楼层 |阅读模式
    分享到:
    一部分驱动要和上层通信,都需要生成设备节点,上层应用通过一套标准的接口函数调用设备节点就可以控制底层以及和底层通信。最简单易用的杂项设备节点如何生成。

    为什么引入杂项设备


    在exynos4412开发板,在超级终端中输入命令“cat /proc/misc”可以查看对应的杂项设备。主设备号只有256 个,设备又非常多,所以引入了子设备号。其中杂项设备的主设备号是10,在任何Linux 系统中它都是固定的。一般将linux驱动分为字符设备、块设备、网络设备,但是这个分类不能包含所有的设备,所以将无法归类的设备统称为杂项设备,杂项设备可能用到字符设备、快设备、网络设备中的一项或者多项设备。
    在linux虚拟机上在内核源码文件下,ls drivers/char可以看到misc.c和misc.o,可见它被编译进内核,使用vim drivers/char/Makefile打开杂项设备文件的编译文件。
    obj-y +=misc.o,该文件被强制编译进内核
    杂项设备设备部分完全制作好了,只需要添加子设备,非常方便。
    这样杂项设备的引入即解决了设备号数量少的问题,又降低了使用难度,还能防止碎片化,一举多得。

    杂项设备注册函数以及结构体

    杂项设备的头文件在“include/linux/miscdevice.h”
    1. struct miscdevice  {
    2.     int minor;//设备号,赋值为MISC_DYNAMIC_MINOR,这个宏定义可以查到为10
    3.     const char *name;//设备名称
    4.     const struct file_operations *fops;//file_operations 结构体
    5.     struct list_head list;
    6.     struct device *parent;
    7.     struct device *this_device;
    8.     const char *nodename;
    9.     mode_t mode;
    10. };

    11. extern int misc_register(struct miscdevice * misc);
    12. extern int misc_deregister(struct miscdevice *misc);
    复制代码
    Cextern int misc_register(struct miscdevice * misc);
    杂项设备注册函数;一般在probe 中调用,参数是miscdevice
    Cextern int misc_deregister(struct miscdevice *misc);
    杂项设备卸载函数;一般是在hello_remove 中用于卸载驱动。

    file_operations结构体

    file_operations 结构体的成员函数属于驱动开发的主体内容,里面的函数和Linux 系统给应用程序提供系统接口一一对应。
    file_operations结构体在内核源码“include/linux/fs.h”中,使用命令vim /include/linux/fs.h打开
    1. struct file_operations {
    2.     struct module *owner;
    3.     loff_t (*llseek) (struct file *, loff_t, int);
    4.     ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
    5.     ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
    6.     ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
    7.     ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
    8.     int (*readdir) (struct file *, void *, filldir_t);
    9.     unsigned int (*poll) (struct file *, struct poll_table_struct *);
    10. /* remove by cym 20130408 support for MT660.ko */
    11. #if 0
    12. //#ifdef CONFIG_SMM6260_MODEM
    13. #if 1// liang, Pixtree also need to use ioctl interface...
    14.     int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
    15. #endif
    16. #endif
    17. /* end remove */
    18.     long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
    19.     long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
    20.     int (*mmap) (struct file *, struct vm_area_struct *);
    21.     int (*open) (struct inode *, struct file *);
    22.     int (*flush) (struct file *, fl_owner_t id);
    23.     int (*release) (struct inode *, struct file *);
    24.     int (*fsync) (struct file *, int datasync);
    25.     int (*aio_fsync) (struct kiocb *, int datasync);
    26.     int (*fasync) (int, struct file *, int);
    27.     int (*lock) (struct file *, int, struct file_lock *);
    28.     ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
    29.     unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
    30.     int (*check_flags)(int);
    31.     int (*flock) (struct file *, int, struct file_lock *);
    32.     ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
    33.     ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
    34.     int (*setlease)(struct file *, long, struct file_lock **);
    35.     long (*fallocate)(struct file *file, int mode, loff_t offset,loff_t len);
    36. /* add by cym 20130408 support for MT6260 and Pixtree */
    37. #if defined(CONFIG_SMM6260_MODEM) || defined(CONFIG_USE_GPIO_AS_I2C)
    38.     int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
    39. #endif
    40. /* end add */
    41. };
    复制代码
    该结构体中参数非常之多,介绍常用的的参数
    1. struct module *owner;一般是THIS_MODULE。
    2. int (*open) (struct inode *, struct file *);//对应上层的open 函数,打开文件。
    3. int (*release) (struct inode *, struct file *);//对应上层的close 函数,打开文件操作之后一般需要关闭。
    4. ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);//读函数,上层应用从底层读取函数。
    5. ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);//写函数,上层应用向底层传输数据。
    6. long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);//这个函数功能和写函数稍微有点重合,但是这个函数占用的内存非常小,主要针对IO 口的控制。
    复制代码
    定义一个DEVICE_NAME,将其赋值为”hello_ctl123”,理解这个设备节点名称和前面介绍的注册设备名称是不同的。
    示例代码
    1. linux_misc.c
    2. #include <linux/init.h>
    3. #include <linux/module.h>

    4. /*驱动注册的头文件,包含驱动的结构体和注册和卸载的函数*/
    5. #include <linux/platform_device.h>
    6. /*注册杂项设备头文件*/
    7. #include <linux/miscdevice.h>
    8. /*注册设备节点的文件结构体*/
    9. #include <linux/fs.h>

    10. #define DRIVER_NAME "hello_ctl"
    11. #define DEVICE_NAME "hello_ctl123"
    12. MODULE_LICENSE("Dual BSD/GPL");
    13. MODULE_AUTHOR("chenmiaohong");

    14. static long hello_ioctl( struct file *files, unsigned int cmd, unsigned long arg){


    15.     printk("cmd is %d,arg is %d\n",cmd,arg);
    16.     return 0;
    17. }

    18. static int hello_release(struct inode *inode, struct file *file){
    19.     printk(KERN_EMERG "hello release\n");
    20.     return 0;
    21. }

    22. static int hello_open(struct inode *inode, struct file *file){
    23.     printk(KERN_EMERG "hello open\n");
    24.     return 0;
    25. }

    26. static struct file_operations hello_ops = {
    27.     .owner = THIS_MODULE,
    28.     .open = hello_open,
    29.     .release = hello_release,
    30.     .unlocked_ioctl = hello_ioctl,
    31. };

    32. static  struct miscdevice hello_dev = {
    33.     .minor = MISC_DYNAMIC_MINOR,
    34.     .name = DEVICE_NAME,
    35.     .fops = &hello_ops,
    36. };


    37. static int hello_probe(struct platform_device *pdv){

    38.     printk(KERN_EMERG "\tinitialized\n");
    39.     misc_register(&hello_dev);

    40.     return 0;
    41. }

    42. static int hello_remove(struct platform_device *pdv){

    43.     printk(KERN_EMERG "\tremove\n");
    44.     misc_deregister(&hello_dev);
    45.     return 0;
    46. }

    47. static void hello_shutdown(struct platform_device *pdv){

    48.     ;
    49. }

    50. static int hello_suspend(struct platform_device *pdv,pm_message_t pmt){

    51.     return 0;
    52. }

    53. static int hello_resume(struct platform_device *pdv){

    54.     return 0;
    55. }

    56. struct platform_driver hello_driver = {
    57.     .probe = hello_probe,
    58.     .remove = hello_remove,
    59.     .shutdown = hello_shutdown,
    60.     .suspend = hello_suspend,
    61.     .resume = hello_resume,
    62.     .driver = {
    63.         .name = DRIVER_NAME,
    64.         .owner = THIS_MODULE,
    65.     }
    66. };


    67. static int hello_init(void)
    68. {
    69.     int DriverState;

    70.     printk(KERN_EMERG "HELLO WORLD enter!\n");
    71.     DriverState = platform_driver_register(&hello_driver);

    72.     printk(KERN_EMERG "\tDriverState is %d\n",DriverState);
    73.     return 0;
    74. }


    75. static void hello_exit(void)
    76. {
    77.     printk(KERN_EMERG "HELLO WORLD exit!\n");

    78.     platform_driver_unregister(&hello_driver);  
    79. }

    80. module_init(hello_init);
    81. module_exit(hello_exit);
    复制代码
    编译完成后通过TFTP服务器下载到exynos4412开发板中,使用insmod加载linux_misc.ko文件,然后使用ls /dev查看出现了新的设备hello_ctl123。
    转自博客陈庙红
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-20 02:30 , Processed in 0.113095 second(s), 17 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.