字符设备
注册设备号
每个设备都有设备号,主设备号用来区分驱动,从设备号用来区分具体设备;内核维护着一个以主设备号为key的全局哈希表,数据部分就是对应的驱动程序指针或同类驱动程序组成的数组的指针
动态:int alloc_chrdev_region(dev_t*dev, unsigned baseminor, unsigned count, const char*name)
静态:int register_chrdev_region(dev_t from, unsigned count, const char*name)
释放:void unregister_chrdev_region(dev_t from, unsigned count)
字符设备结构操作
初始化:void cdev_init(struct cdev *cdev, const struct file_operations *fops)
添加字符设备:int cdev_add(struct cdev *p, dev_t dev, unsigned count)
卸载:void cdev_del(struct cdev *p)
自动创建设备节点文件:
创建和删除类:
struct class *class_create (struct module *owner, const char *name) 会在sysfs在/sys/class/下生成类
void class_destroy(struct class *cls);
创建和删除设备:
struct device *device_create(struct class *class,struct device *parent,dev_t devt,void *drvdata,const char *fmt, ...)会通过udev在/dev/下生成设备节点
void device_destroy(struct class *class, dev_t devt)
insmod
sys_init_module系统调用通过mod = load_module(umod, len, uargs)加载模块进内核,然后通过do_one_initcall(mod->init)调用模块的init函数