分类: LINUX
2012-08-12 18:44:44
make -C /home/zxl/soft/kernel/linux-2.6.22 M=`pwd` clean
字符设备的生成 -- 这个版本简洁
-------------------------------------------------------
static struct class *node_class;
static int __init init_mtdchar(void)
{
if (register_chrdev(CHAR_MAJOR, "dev_node", &node_fops))
{
printk(KERN_NOTICE "Can't allocate major number %d.\n", CHAR_MAJOR);
return -EAGAIN;
}
node_class = class_create(THIS_MODULE, "dev_node_class");
if (IS_ERR(node_class)) {
printk(KERN_ERR "Error creating node_class.\n");
unregister_chrdev(CHAR_MAJOR, "dev_node");
return PTR_ERR(node_class);
}
class_device_create(node_class, NULL, MKDEV(CHAR_MAJOR, 0), NULL,
"node_name" "%d", CHAR_MINOR);
return 0;
}
static void __exit cleanup_mtdchar(void)
{
class_device_destroy(node_class, MKDEV(CHAR_MAJOR, 0));
class_destroy(node_class);
unregister_chrdev(CHAR_MAJOR, "dev_node");
}
linux-2.6.21.7 内核中的使用实例
-------------------------------------------------------
static void adbdev_init(void)
{
if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
return;
}
adb_dev_class = class_create(THIS_MODULE, "adb");
if (IS_ERR(adb_dev_class))
return;
class_device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL, "adb");
}
linux-2.6.28 内核中的使用实例
-------------------------------------------------------
static void __init adbdev_init(void)
{
if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
return;
}
adb_dev_class = class_create(THIS_MODULE, "adb");
if (IS_ERR(adb_dev_class))
return;
device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL, "adb");
platform_device_register(&adb_pfdev);
platform_driver_probe(&adb_pfdrv, adb_dummy_probe);
}