驱动注册设备号要用:
(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 );
释放设备号要用:
void unregister_chrdev_region( dev_t first, unsigned int count );
内核字符设备驱动注册要用:
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);
内核字符设备驱动移除要用:
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);
以上也可以用命令创建设备文件:
mknod /dev/设备文件名 字符设备(c是字符设备,b是块设备) 主设备号 次设备号
例如:mknod /dev/testChar c 100 0
删除设备入口:
rm /dev/testChar
驱动总体编写框架:
static int __init my_init(void)
{
//分配设备编号
if(主设备号)
{
sbc2440_leds_dev = MKDEV (LED_MAJOR, LED_MINOR);
result = register_chrdev_region (sbc2440_leds_dev, count, DEVICE_NAME);
}
else
{
result = alloc_chrdev_region (&sbc2440_leds_dev, LED_MINOR, count, DEVICE_NAME);
LED_MAJOR = MAJOR (sbc2440_leds_dev);
}
//注册字符设备驱动
sbc2440_leds_cdev = cdev_alloc();
if (sbc2440_leds_cdev != NULL)
{
cdev_init (sbc2440_leds_cdev, &sbc2440_leds_fops);
sbc2440_leds_cdev->ops = &sbc2440_leds_fops;
sbc2440_leds_cdev->owner = THIS_MODULE;
if (cdev_add (sbc2440_leds_cdev, num, count) )
printk (KERN_NOTICE "Someting wrong when adding sbc2440_leds_cdev!\n");
else
printk ("Success adding sbc2440_leds_cdev!\n");
}
//创建设备文件
devfs_handle = devfs_mk_cdev( MKDEV(LED_MAJOR, LED_MINOR), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);
}
static int __exit my_exit(void)
{
//移除设备文件
devfs_remove(DEVICE_NAME);
//注销字符设备
cdev_del (sbc2440_leds_cdev);
//释放设备编号:
unregister_chrdev_region (sbc2440_leds_dev, count);
}
module_init( my_init );
module_exit( my_exit );
驱动程序的编译:
驱动程序在编译之前,所使用的内核必须要经过编译,否则驱动程序不能编译。