iTOP4412设备驱动学习三

本节是关于生成设备节点。

学习资料来源于迅为的视频学习教程的整理

1. 杂项设备,或者说是对一部分字符设备的封装或者一部分不好分类的。

    可以节省主设备号,驱动写起来相对简单(用封装好的杂项设备可以减少一步注册主设备号的过程)

2.源代码位置

    杂项设备初始化源代码:/drivers/char/misc.c,属于内核中强制编译的。

    杂项设备注册头文件:include/linux/miscdevice.h,主要的结构体为miscdevice

 1 struct miscdevice  {
 2         int minor;     //设备号,一般系统分配随机取值
 3         const char *name;     //生成设备节点的名称,无限制
 4         const struct file_operations *fops;     //指向一个设备节点文件,对文件的操作
 5         struct list_head list;
 6         struct device *parent;
 7         struct device *this_device;
 8         const char *nodename;
 9         mode_t mode;
10 };
11 
12 extern int misc_register(struct miscdevice * misc);    //生成设备节点的函数
13 extern int misc_deregister(struct miscdevice *misc);

     设备节点本质是文件一个特殊文件,包括文件名,打开关闭等,文件结构体的头文件为include/linux/fs.h,结构体为file_operations

 1 //相当重要的结构体
 2 /* These macros are for out of kernel modules to test that
 3  * the kernel supports the unlocked_ioctl and compat_ioctl
 4  * fields in struct file_operations. */
 5 #define HAVE_COMPAT_IOCTL 1
 6 #define HAVE_UNLOCKED_IOCTL 1
 7 
 8 /*
 9  * NOTE:
10  * all file operations except setlease can be called without
11  * the big kernel lock held in all filesystems.
12  */
13 struct file_operations {
14         struct module *owner;
15         loff_t (*llseek) (struct file *, loff_t, int);
16         ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
17         ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
18         ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
19         ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
20         int (*readdir) (struct file *, void *, filldir_t);
21         unsigned int (*poll) (struct file *, struct poll_table_struct *);
22 /* remove by cym 20130408 support for MT660.ko */
23 #if 0
24 //#ifdef CONFIG_SMM6260_MODEM
25 #if 1// liang, Pixtree also need to use ioctl interface...
26         int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
27 #endif
28 #endif
29 /* end remove */
30         long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
31         long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
32         int (*mmap) (struct file *, struct vm_area_struct *);
33         int (*open) (struct inode *, struct file *);
34         int (*flush) (struct file *, fl_owner_t id);
35         int (*release) (struct inode *, struct file *);
36         int (*fsync) (struct file *, int datasync);
37         int (*aio_fsync) (struct kiocb *, int datasync);
38         int (*fasync) (int, struct file *, int);
39         int (*lock) (struct file *, int, struct file_lock *);
40         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
41         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
42         int (*check_flags)(int);
43         int (*flock) (struct file *, int, struct file_lock *);
44         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
45         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
46         int (*setlease)(struct file *, long, struct file_lock **);
47         long (*fallocate)(struct file *file, int mode, loff_t offset,
48                           loff_t len);
49 /* add by cym 20130408 support for MT6260 and Pixtree */
50 #if defined(CONFIG_SMM6260_MODEM) || defined(CONFIG_USE_GPIO_AS_I2C)
51         int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
52 #endif
53 /* end add */
54 };

猜你喜欢

转载自www.cnblogs.com/nanzh/p/12444029.html