static int __init ofcd_init(void) /* Constructor */
{
printk(KERN_INFO "Welcome!");
if (alloc_chrdev_region(&first, 0, 1, "char_dev") < 0) //$cat /proc/devices
{
return -1;
}
if ((cl = class_create(THIS_MODULE, "chardrv")) == NULL) //$ls /sys/class
{
unregister_chrdev_region(first, 1);
return -1;
}
cdev_init(&c_dev, &fops);
if (cdev_add(&c_dev, first, 1) == -1)
{
device_destroy(cl, first);
class_destroy(cl);
unregister_chrdev_region(first, 1);
return -1;
}
return 0;if (device_create(cl, NULL, first, NULL, "mynull") == NULL) //$ls /dev/ { class_destroy(cl); unregister_chrdev_region(first, 1); return -1; }
}
Code might be clear if you would use goto like kernel uses.
本文介绍了一个具体的Linux字符设备驱动初始化过程。该过程包括了设备号的注册、设备类的创建、设备文件操作结构的初始化及添加,以及最终的设备实例创建。通过这个例子可以了解Linux字符设备驱动的基本构造步骤。
3363

被折叠的 条评论
为什么被折叠?



