if (xxx_major) { dev = MKDEV(xxx_major, xxx_minor); retsult = register_chrdev_region(dev, xxx_nr_devs, DEV_NAME); } else { retsult = alloc_chrdev_region(&dev, xxx_minor, xxx_nr_devs, DEV_NAME); xxx_major = MAJOR(dev); } if (retsult < 0) { printk(KERN_WARNING "DEV_NAME: can't get major %d\n", xxx_major); return retsult; }
二、以下将给出三个驱动程序操作涉及的内核数据结构,其中struct file_operations结构体保存了
字符驱动的方法,struct file 表示一个打开的文件, struct inode 表示一个磁盘文件。
1)、struct file_operations结构体:struct file_operations { struct module *owner; loff_t (*llseek) (struct file *filp, loff_t, int); ssize_t (*read) (struct file *filp, char __user *buf, size_t count, loff_t *f_ops); ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t); ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t); int (*readdir) (struct file *, void *, filldir_t); unsigned int (*poll) (struct file *, struct poll_table_struct *); int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); long (*compat_ioctl) (struct file *, unsigned int, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); int (*flush) (struct file *, fl_owner_t id); int (*release) (struct inode *, struct file *); int (*fsync) (struct file *, struct dentry *, int datasync); int (*aio_fsync) (struct kiocb *, int datasync); int (*fasync) (int, struct file *, int); int (*lock) (struct file *, int, struct file_lock *); ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *); ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int); unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); int (*check_flags)(int); int (*dir_notify)(struct file *filp, unsigned long arg); int (*flock) (struct file *, int, struct file_lock *); ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int); ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int); };其中方法poll、epoll、select这三个系统调用可用来查询某个或多个文件描述符上的读取或是写入是否会被阻塞,
struct file { /* * fu_list becomes invalid after file_free is called and queued via * fu_rcuhead for RCU freeing */ union { struct list_head fu_list; struct rcu_head fu_rcuhead; } f_u; struct path f_path; #define f_dentry f_path.dentry #define f_vfsmnt f_path.mnt const struct file_operations *f_op; atomic_t f_count; unsigned int f_flags; mode_t f_mode; loff_t f_pos; struct fown_struct f_owner; unsigned int f_uid, f_gid; struct file_ra_state f_ra; unsigned long f_version; #ifdef CONFIG_SECURITY void *f_security; #endif /* needed for tty driver, and maybe others */ void *private_data; #ifdef CONFIG_EPOLL /* Used by fs/eventpoll.c to link all the hooks to this file */ struct list_head f_ep_links; spinlock_t f_ep_lock; #endif /* #ifdef CONFIG_EPOLL */ struct address_space *f_mapping; };其中void *private_data; private_data用来保存跨系统调用时保存的状态信息的非常有用的资源,
struct cdev { struct kobject kobj; struct module *owner; const struct file_operations *ops; struct list_head list; dev_t dev; //表示设备号 unsigned int count; };完成cdev的初始化并添加到系统中的源码如下:
static void scull_setup_cdev(struct scull_dev *dev, int index) { int err, devno = MKDEV(scull_major, scull_minor + index); cdev_init(&dev->cdev, &scull_fops); dev->cdev.owner = THIS_MODULE; dev->cdev.ops = &scull_fops; err = cdev_add(&dev->cdev, devno, 1); if (err) printk(KERN_NOTICE "Error %d adding scull %d", err, index); }四、read和write实现:
1)、read实现 static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) { //*ppos是要读的位置相对于文件开头的偏移 unsigned long p = *ppos; unsigned int count = size; int ret = 0; struct globalmem_dev *dev = filp->private_data; if (p > GLOBALMEM_XIZE) return 0; if (count > GLOBALMEM_SIZE - p) count = GLOBALMEM_SIZE - p; if (copy_to_user(buf, dev->mem + p, count) { ret = -EFAULT; } else { *ppos += count; ret = count; printk(KERN_INFO "read %u byte(s) from %lu\n", count, p); } return ret; } 2)、write实现 static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) { //*ppos是要读的位置相对于文件开头的偏移 unsigned long p = *ppos; unsigned int count = size; int ret = 0; struct globalmem_dev *dev = filp->private_data; if (p > GLOBALMEM_XIZE) return 0; if (count > GLOBALMEM_SIZE - p) count = GLOBALMEM_SIZE - p; if (copy_from_user(dev->mem + p, buf, count) { ret = -EFAULT; } else { *ppos += count; ret = count; printk(KERN_INFO "written %u byte(s) from %lu\n", count, p); } return ret; }