Bootstrap

四、 class_create(),device_create(),device_create_file()


开始写Linux设备驱动程序的时候,很多时候都是利用mknod命令手动创建设备节点(包括ldd3中不少例子也是这样),实际上现在Linux内核为我们提供了一组函数,可以用来在模块加载的时候自动在/dev目录下创建相应设备节点,并在卸载模块时删除该节点。

内核中定义了struct class结构体,顾名思义,一个struct class结构体类型变量对应一个类,内核同时提供了class_create(…)函数,可以用它来创建一个类,这个类存放于sysfs下面,一旦创建好了这个类,再调用device_create(…)函数来在/dev目录下创建相应的设备节点。这样,加载模块的时候,用户空间中的udev会自动响应device_create(…)函数,去/sysfs下寻找对应的类从而创建设备节点。

此外,利用device_create_file函数可以在/sys/class/下创建对应的属性文件,从而通过对该文件的读写实现特定的数据操作。

例子:

1、在驱动初始化的代码里调用class_create为该设备创建一个class,再为每个设备调用 class_device_create创建对应的设备大致用法如下:
struct class *myclass = class_create(THIS_MODULE, “my_device_driver”);
class_device_create(myclass, NULL, MKDEV(major_num, 0), NULL, “my_device”);
这样的module被加载时,udev daemon就会自动在/dev下创建my_device设备文件

2

例子1

  device_create( my_class, NULL, MKDEV(hello_major, 0), "hello" "%d", 0 );

如果成功,它将会在/dev目录下产生/dev/hello0设备。

 

例子2

led_cdev->dev = device_create_drvdata(leds_class, parent, 0, led_cdev,

                                         "%s", led_cdev->name);

这是前文platformgpio-leds里面的源码

由于dev_t0,所以它不会在/dev下产生设备文件。

led_cdev为传递给class的私有数据。

会把第6个参数的内容复制到第5个参数 “%s”,就像printf一样

3

device_create_file好像用来在 文件系统的设备子系统下 设置某个设备文件属性的。而这个设备属性里包括了 属性名称, 读写属性的方法等

调用device_create_file(dev, &dev_attr_debug);函数在/sys中导出信息,device_create_file()的第一个入口参数类型为struct device结构体。问题是如何把cdev转换到device,

结构层次:


kobject


/ \
cdev device
所以从子类cdev--->父类kobject--->子类device
container_of(kobj)-->list_entry(entry)->(struct device*)


一、class_create

官方说明:

[cpp]  view plain   copy
  在CODE上查看代码片 派生到我的代码片
  1. /* This is a #define to keep the compiler from merging different 
  2.  * instances of the __key variable */  
  3. #define class_create(owner, name)       \  
  4. ({                      \  
  5.     static struct lock_class_key __key; \  
  6.     __class_create(owner, name, &__key);    \  
  7. })  
  8.   
  9. /** 
  10.  * class_create - create a struct class structure 
  11.  * @owner: pointer to the module that is to "own" this struct class 
  12.  * @name: pointer to a string for the name of this class. 
  13.  * @key: the lock_class_key for this class; used by mutex lock debugging 
  14.  * 
  15.  * This is used to create a struct class pointer that can then be used 
  16.  * in calls to device_create(). 
  17.  * 
;