1. scull太麻烦了,还要自己手动创建设备结点。有没有自动创建的方法呢?class_create
- #include
- struct class * p_scull_class;
- static int __init scull_init(void)
- {
- int ret;
- printk(KERN_ALERT "in scull driver\n");
- //动态分配一个设备号,从0开始分配一个从设备号
- ret = alloc_chrdev_region(&scull_dev_num, 0, 1, "scull");
- if(ret < 0)
- {
- printk(KERN_ALERT "alloc_chrdev_region error!\n");
- }
-
- //字符设备注册
- p_scull_dev = cdev_alloc();
- p_scull_dev->owner = THIS_MODULE;
- p_scull_dev->ops = &scull_fops;
- cdev_add(p_scull_dev, scull_dev_num, 1);
-
- p_scull_class = class_create(THIS_MODULE, "scull_class");
- if(IS_ERR(p_scull_class))
- {
- printk(KERN_ALERT "class create error!\n");
- return -1;
- }
- device_create(p_scull_class, NULL, scull_dev_num, NULL, "scull");
- printk(KERN_ALERT "create device node!\n");
- return 0;
- }
-
- static void __exit scull_exit(void)
- {
- printk(KERN_ALERT "goodbye scull dirver\n");
- unregister_chrdev_region(scull_dev_num, 1);
- cdev_del(p_scull_dev);
-
- //删除设备
- device_destroy(p_scull_class, scull_dev_num);
- class_destroy(p_scull_class);
- return ;
- }
2. class_create、device_create、class_destroy、device_destroy 这四个函数都定义在linux/device.h中
root@ubuntu:~/driver/3scull# vi /lib/modules/`uname -r`/build/include/linux/device.h
3.class_create、class_destroy、device_create、device_destroy
3.1 实现在 driver/base/core.c中
/**
* device_create - creates a device and registers it with sysfs
* @class: pointer to the struct class that this device should be registered to
* @parent: pointer to the parent struct device of this new device, if any
* @devt: the dev_t for the char device to be added
* @drvdata: the data to be added to the device for callbacks
* @fmt: string for the device's name
*
* This function can be used by char device classes. A struct device
* will be created in sysfs, registered to the specified class.
*
* A "dev" file will be created, showing the dev_t for the device, if
* the dev_t is not 0,0.
* If a pointer to a parent struct device is passed in, the newly created
* struct device will be a child of that device in sysfs.
* The pointer to the struct device will be returned from the call.
* Any further sysfs files that might be required can be created using this
* pointer.
*
* Returns &struct device pointer on success, or ERR_PTR() on error.
*
* Note: the struct class passed to this function must have previously
* been created with a call to class_create().
*/
struct device *device_create(struct class *class, struct device *parent, dev_t devt, void *drvdata, const char *fmt, ...)
3.2 实现在 driver/base/core.c中
/**
* device_destroy - removes a device that was created with device_create()
* @class: pointer to the struct class that this device was registered with
* @devt: the dev_t of the device that was previously registered
*
* This call unregisters and cleans up a device that was created with a
* call to device_create().
*/
void device_destroy(struct class *class, dev_t devt)
3.3 实现在 driver/base/class.c中
- device_create
- --> device_create_vargs
- --> device_add(struct device *dev)
- --> kobject_add(&dev->kobj, dev->kobj.parent, NULL);
- --> kobject_add_varg
- --> kobject_add_internal lib/kobject.c L178
- --> kobject_uevent(&dev->kobj, KOBJ_ADD);
- --> kobject_uevent_env(kobj, action, NULL);
-
-
- lib/kobject.c:kobject_add_internal[185]: kobject: 'fb0' (cf0ba908): kobject_add_internal: parent: 'graphics', set: 'devices'
-
- lib/kobject_uevent.c:kobject_uevent_env[203]: action_string=add, devpath=/devices/virtual/graphics/fb0, subsystem=graphics
/**
* class_create - create a struct class structure
* @owner: pointer to the module that is to "own" this struct class
* @name: pointer to a string for the name of this class.
* @key: the lock_class_key for this class; used by mutex lock debugging
*
* This is used to create a struct class pointer that can then be used
* in calls to device_create().
*
* Returns &struct class pointer on success, or ERR_PTR() on error.
*
* Note, the pointer created here is to be destroyed when finished by
* making a call to class_destroy().
*/
struct class *__class_create(struct module *owner, const char *name, struct lock_class_key *key)
3.4 实现在 driver/base/class.c中
/**
* class_destroy - destroys a struct class structure
* @cls: pointer to the struct class that is to be destroyed
*
* Note, the pointer to be destroyed must have been created with a call
* to class_create().
*/
void class_destroy(struct class *cls)
3scull.zip (改名3scull.tar.gz)