2.6内核前后--注册字符设备驱动区别

来源:互联网 发布:百度贴吧论坛 源码 编辑:程序博客网 时间:2024/05/16 12:17

1:2.6内核前

int register_chrdev (unsigned int major, const char *name, struct file_operations *fops);

int unregister_chrdev( unsigned int major, const char *name );

创建设备文件的方法:

static devfs_handle_t devfs_handle;
devfs_handle = devfs_register( NULL, DEVICE_NAME, DEVFS_FL_DEFAULT,
BUTTON_MAJOR,&sbc2410_buttons_fops, NULL);

devfs_unregister( devfs_handle);


2:2.6内核以及以后

int register_chrdev_region( dev_t first, unsigned int count, char *name );

int alloc_chrdev_region( dev_t *dev, unsigned int firstminor,unsigned int count, char *name );

void unregister_chrdev_region( dev_t first, unsigned int count );

void cdev_init( struct cdev *cdev, struct file_operations *fops);
int cdev_add( struct cdev *dev, dev_t num, unsigned int count);

void cdev_del( struct cdev *dev );

创建设备文件的方法:

devfs_mk_cdev( MKDEV(LED_MAJOR, LED_MINOR),
S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);

devfs_remove(DEVICE_NAME);


3:字符设备文件生成方法

1:通过命令创建

mknod /dev/设备文件名 字符设备(c是字符设备,b是块设备) 主设备号 次设备号
例如:mknod /dev/testChar c 100 0
删除设备入口:
rm /dev/testChar

2:使用devfs动态创建设备节点

#ifdef CONFIG_DEVFS_FS---内核配置项

3:用udev配合class动态生成设备文件
注册设备类
class_create,class_simple_create在/sys/class创建类目录
例如:
sys/class/cdd_class的文件夹
dev_class = class_create(THIS_MODULE, "cdd_class");
注册设备
device_create,class_device_create,class_simple_device_add
例如:
sys/class/cdd_class/cdd0 /dev/cdd0
dev_device = device_create(dev_class, NULL, dev, NULL, "cdd%d", cdd_minor);


总结:2.6内核以前的字符驱动注册机制不应该在新代码中使用了,因为会在将来的内核中消失。


0 0
原创粉丝点击