linux2.6驱动注册、字符设备注册和它们的释放
linux2.6驱动注册设备号
(1)如果主设备号事先知道,要用:int register_chrdev_region( dev_t first, unsigned int count, char *name );
(2)如果主设备号为0,则要用动态分配:
int alloc_chrdev_region( dev_t *dev, unsigned int firstminor,
unsigned int count, char *name );
linux2.6释放设备号
void unregister_chrdev_region( dev_t first, unsigned int count );
linux2.6内核字符设备驱动注册
struct cdev *my_cdev = cdev_alloc();my_cdev->ops = &chr_fops;
void cdev_init( struct cdev *cdev, struct file_operations *fops);
int cdev_add( struct cdev *dev, dev_t num, unsigned int count);
2.6内核字符设备驱动移除
void cdev_del( struct cdev *dev );
2.6内核驱动注册完后,要用以下代码创建设备文件
devfs_mk_cdev( MKDEV(LED_MAJOR, LED_MINOR),S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);
linux2.6内核驱动要用以下代码移除设备文件
devfs_remove(DEVICE_NAME);可以用命令创建设备文件
mknod /dev/设备文件名 字符设备(c是字符设备,b是块设备) 主设备号 次设备号例如:mknod /dev/testChar c 100 0
删除设备入口:
rm /dev/testChar
最后更新:2017-04-03 20:19:50