一直以来对Linux系统了解不深,最多就在自个儿的Mac上了解一下类Unix的shell命令.别说深入到内核里了.不过学习这事儿其实挺简单,花时间下工夫,迟早能成大神的,对吧?
字符设备学习,跟着书来(<Linux设备驱动程序>与<Linux设备驱动开发详解>).作为入门,先编写一个操作内存的字符设备,其提供的功能就是对内存的读写清空等操作.代码很简单,但有一个驱动的基本架构了.
#include <linux/module.h> /** * 动态加载模块 * 定义 LICESENCE(), AUTHOR等等. * kobject kset 定义 */ #include <linux/init.h> /** * 模块初始化的相关定义 */ #include <linux/types.h> #include <linux/fs.h> #include <linux/errno.h> #include <linux/mm.h> #include <linux/sched.h> #include <linux/cdev.h> #include <linux/slab.h>//kfree, kmalloc #define MEM_SIZE 0x1000 //定义操作4KB内存 #define MEM_CLEAR 0x1 //定义fileoperations操作 #define MEM_MAJOR 250 //预设主设备号 #define EDITION 1 //调试版本 static int mem_major = MEM_MAJOR;//储存第一次分配到的主设备号,防止重复申请. /** * 驱动操作的设备结构体 */ struct mem_dev { int offset; struct cdev cdev; unsigned char mem[MEM_SIZE]; }*mem_devp; int mem_open(struct inode *inode, struct file *filp) { // if (down_interruptible(&mem_devp->sem)) // return - ERESTARTSYS; filp->private_data = mem_devp;//将设备结构体指针交给file,适合单设备驱动 //TODO:多设备 return 0; } int mem_release(struct inode *inode, struct file *filp) { filp->private_data = NULL; return 0; } static int mem_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { struct mem_dev *dev = filp->private_data;//推荐用这样的方法,而不是直接操作mem_devp,因filp可能会有很多设备 switch (cmd) { case MEM_CLEAR: memset(dev->mem, 0, MEM_SIZE); printk(KERN_INFO "mem cleared\n"); break; default : return - EINVAL; } return 0; } static ssize_t mem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) { unsigned long p = *ppos; printk("read_ppos:%d\n", p); printk("read_filp->f_pos:%d\n", filp->f_pos); unsigned int count = size; int ret = 0; struct mem_dev *dev = filp->private_data; if (p >= MEM_SIZE) { printk(KERN_INFO "ppos >= mem size\n"); return 0; }//越界 if (count > MEM_SIZE - p) { count = MEM_SIZE - p; }//读取过多 if (copy_to_user(buf, (void *)(dev->mem + p), count)) { printk(KERN_INFO "%s failed\n", __FUNCTION__); ret = - EFAULT; } else { *ppos += count; ret = count; printk(KERN_INFO "read %u bytes from %lu\n", count, p); } return ret; } static ssize_t mem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos) { unsigned long p = *ppos; printk("write_ppos:%d\n", p); printk("write_filp->f_pos:%d\n", filp->f_pos); printk("filp->flag:0x%x\n", filp->f_flags); unsigned int count = size; int ret = 0; struct mem_dev *dev = filp->private_data; if (p >= MEM_SIZE) { printk(KERN_INFO "ppos >= mem size\n"); return 0; }//越界 if (count > MEM_SIZE - p) { count = MEM_SIZE - p; }//写过多 if (copy_from_user(dev->mem + p, buf, count)) { printk(KERN_INFO "%s failed\n", __FUNCTION__); ret = - EFAULT; } else { *ppos += count; ret = count; printk(KERN_INFO "written %u bytes, now %lu to:%lu\n", count, p, *ppos); } return ret; } static loff_t mem_lseek(struct file *filp, loff_t offset, int orig) { loff_t ret = 0; switch (orig) { case 0: if (offset < 0) { ret = - EINVAL; break; } if ((unsigned long)offset > MEM_SIZE) { ret = - EINVAL; break; } filp->f_pos = offset; ret = filp->f_pos; break; case 1: if ((filp->f_pos + offset) > MEM_SIZE) { ret = - EINVAL; break; } if ((filp->f_pos + offset) < 0) { ret = - EINVAL; break; } filp->f_pos += offset; ret = filp->f_pos; break; default: ret = - EINVAL; break; } return ret; } static const struct file_operations mem_fops = { .owner = THIS_MODULE, .llseek = mem_lseek, .read = mem_read, .write = mem_write, .release = mem_release, .open = mem_open, .compat_ioctl = mem_ioctl, }; static void mem_setup_cdev(struct mem_dev *dev, int index) { int err, devno = MKDEV(mem_major, index);//主,次设备号 cdev_init(&dev->cdev, &mem_fops);//cdev的指针,fileoperations的指针 dev->cdev.owner = THIS_MODULE;//TODO:THIS_MODULE的实现与作用 err = cdev_add(&dev->cdev, devno, 1); if (err) printk(KERN_NOTICE "Error %d adding globalmem cdev:%d", err, index); } static int globalmem_init(void) { int result; dev_t devno = MKDEV(mem_major, 0); if (mem_major) result = register_chrdev_region(devno, 1, "globalmem"); else { result = alloc_chrdev_region(&devno, 0 ,1, "globalmem"); mem_major = MAJOR(devno); } if (result<0) return result; mem_devp = kmalloc(sizeof(struct mem_dev), GFP_KERNEL);//get free pages if (!mem_devp) { result = - ENOMEM; goto fail_malloc; } memset(mem_devp, 0, sizeof(struct mem_dev)); mem_setup_cdev(mem_devp, 0); mem_devp->offset = 0; printk(KERN_INFO "edition:%d init success_________________________________________________________\n", EDITION); return 0;//返回0或负值,如果不加return会返回17.弹出警告 fail_malloc: unregister_chrdev_region(devno, 1); return result; } static int globalmem_exit(void) { cdev_del(&mem_devp->cdev); kfree(mem_devp); unregister_chrdev_region(MKDEV(mem_major, 0), 1); printk(KERN_INFO "exit success___________________________________________________________________\n"); return 0; } MODULE_AUTHOR("huxy11"); module_param(mem_major, int, S_IRUGO); module_init(globalmem_init); module_exit(globalmem_exit)
mem_dev结构体,在驱动中就代表一个操作一段内存的设备,其中有struct cdev结构体,因其中包含了字符设备都具备的属性,每个自定结构体都需要包含.这么样一说,不就是"继承"这一思想么..再仔细看看cdev
struct cdev { struct kobject kobj;//KOBJECT系统 struct module *owner;//THIS_MODULE const struct file_operations *ops;//file操作 struct list_head list;//等待队列 dev_t dev;//设备号 unsigned int count;//一个计数器,是否和多设备有关 };
比较让人崩溃的是,除了设备号,其余的都不了解.在实际工程里知道怎么填充能用就勉强够用了,而在学习上还是完全弄懂比较好.
今天提出问题,明天解决:
1.KOBJECT是什么?
2.THIS_MODULE是什么宏?
3.file操作是什么?
4.等待队列是什么?
5.count是什么?
时间: 2024-10-13 12:04:44