- 设备号的申请
- 静态申请
- register_chrdev_region(dev_t from, unsigned count, const char *name)
- 动态申请
- alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
- <baseminor>起始的次设备号
- 注销设备号
- void unregister_chrdev_region(dev_t from, unsigned count)
- 手工创建设备文件
- mknod filename type major minor
- 例: mknod serial0 c 100 0
- LINUX字符设备2中数据结构
- Struct File代表打开的文件
- 重要成员
- loff_t f_pos/*文件读写位置*/
- Struct Inode物理上存在的文件
- 重要成员
- dev_t i_rdev/*设备号*/
- struct file_operations *f_op
- 例: struct file_operations men_fpos = {
- .owner = THIS_MODULE,
- .llseek = mem_seek,
- .read = mem_read,
- .write = mem_write,
- .ioctl = mem_ioctl,
- .open = mem_open,
- .release = mem_release,
- };
- 设备注册
- 使用struct cdev来描述
- 分配cdev
- struct cdev *cdev_alloc(void)
- 初始化cdev
- void cdev_init(struct cdev *cdev, const struct file_operation *fops)
- 设备的注册
- int cdev_add(struct cdev *p, dev_t dev, unsigned count)
- 设备操作
- int (*open)(struct inode *, struct file *)
- void (*release)(struct inode *, struct file *)
- ssize_t (*read)(struct file *, char __user *buff, size_t, loff_t *offp)
- ssize_t (*write)(struct file*, conct char __user *buff, size_t,loff_t *offp)
- unsigned int (*poll)(struct file *, struct poll_table_struct *)
- int (*ioctl)(struct iond *, struct file *, unsigned int, unsigned long)
- int (*mmap)(struct sile *, struct vm_area_struct *)
- off_t (*llseek)(struct file *, loof_t, int)
- 读和写(与用户空间的交互)
- int copy_from_user(void *to, const void __user *form, int n)
- int copy_to_user(void __user *to, const void *from, int n)
- 设备注销
- int cdev_del(struct cdev *p)