Chinaunix首页 | 论坛 | 博客
  • 博客访问: 138861
  • 博文数量: 49
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 182
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-14 13:22
个人简介

Linux的热爱者

文章存档

2014年(7)

2013年(42)

我的朋友

分类: LINUX

2013-05-03 11:10:26

1. scull太麻烦了,还要自己手动创建设备结点。有没有自动创建的方法呢?class_create
  1. #include  
  2. struct class * p_scull_class;
  3. static int __init scull_init(void)
  4. {
  5.     int ret;
  6.     printk(KERN_ALERT "in scull driver\n");
  7.     //动态分配一个设备号,从0开始分配一个从设备号
  8.     ret = alloc_chrdev_region(&scull_dev_num, 0, 1, "scull");
  9.     if(ret < 0)
  10.     {
  11.         printk(KERN_ALERT "alloc_chrdev_region error!\n");
  12.     }
  13.     
  14.     //字符设备注册
  15.     p_scull_dev = cdev_alloc();
  16.     p_scull_dev->owner = THIS_MODULE;
  17.     p_scull_dev->ops = &scull_fops;
  18.     cdev_add(p_scull_dev, scull_dev_num, 1);
  19.     
  20.     p_scull_class = class_create(THIS_MODULE, "scull_class");
  21.     if(IS_ERR(p_scull_class))
  22.     {
  23.         printk(KERN_ALERT "class create error!\n");
  24.         return -1;
  25.     }
  26.     device_create(p_scull_class, NULL, scull_dev_num, NULL, "scull");
  27.     printk(KERN_ALERT "create device node!\n");
  28.     return 0;
  29. }

  30. static void __exit scull_exit(void)
  31. {
  32.     printk(KERN_ALERT "goodbye scull dirver\n");
  33.     unregister_chrdev_region(scull_dev_num, 1);
  34.     cdev_del(p_scull_dev);

  35.     //删除设备
  36.     device_destroy(p_scull_class, scull_dev_num);
  37.     class_destroy(p_scull_class);
  38.     return ;
  39. }

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)
  1. device_create
  2.     --> device_create_vargs
  3.     --> device_add(struct device *dev)
  4.         --> kobject_add(&dev->kobj, dev->kobj.parent, NULL);
  5.             --> kobject_add_varg
  6.                 --> kobject_add_internal lib/kobject.c L178
  7.         -->    kobject_uevent(&dev->kobj, KOBJ_ADD);
  8.             --> kobject_uevent_env(kobj, action, NULL);


  9. lib/kobject.c:kobject_add_internal[185]: kobject: 'fb0' (cf0ba908): kobject_add_internal: parent: 'graphics', set: 'devices'

  10. lib/kobject_uevent.c:kobject_uevent_env[203]: action_string=add, devpath=/devices/virtual/graphics/fb0, subsystem=graphics
3.3  实现在 driver/base/class.c中
/**
 * 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)

【参考文章】
阅读(767) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~