自动创建设备文件
mdev:根据系统信息自动创建设备节点
1.定义两个类
static struct class *firstdrv_class;
static struct class_device *firstdrv_class_dev;
2.在 xxx_init 函数
int major;
static int first_drv_init(void)
{
major = register_chrdev(0, "first_drv", &first_drv_fops); // 注册, 告诉内核
firstdrv_class = class_create(THIS_MODULE, "firstdrv");
firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */**
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
return 0;
}
3.在 xxx_exit 函数中
tatic void first_drv_exit(void)
{
unregister_chrdev(major, "first_drv"); // 卸载
class_device_unregister(firstdrv_class_dev);
class_destroy(firstdrv_class);
iounmap(gpfcon);
}
出错的话添加 MODULE_LICENSE(“GPL”); 到文件尾。
错误:error: implicit declaration of function ‘class_device_create’
/home/allen/mydriver/hello_led/hello_led.c:39: warning: assignment makes pointer from integer without a cast
/home/allen/mydriver/hello_led/hello_led.c: In function ‘led_exit’:
/home/allen/mydriver/hello_led/hello_led.c:48: error: implicit declaration of fu…
解决方法:
2.6.29以后(我用的是2.6.32的),使用的函数则变成了 class_create()和device_create(),并且要在声明中加入#include