字符设备相关函数

来源:互联网 发布:linux查看tcp连接状态 编辑:程序博客网 时间:2024/04/29 11:35

字符设备相关函数

1.alloc_chrdev_region()

功能: 自动分配一个主设备号及基于此主设备号的若干个连续的指定数量的次设备号。

函数原型如下:

[cpp] view plaincopy
  1. /** 
  2.  * alloc_chrdev_region() - register a range of char device numbers 
  3.  * @dev: output parameter for first assigned number 
  4.  * @baseminor: first of the requested range of minor numbers 
  5.  * @count: the number of minor numbers required 
  6.  * @name: the name of the associated device or driver 
  7.  * 
  8.  * Allocates a range of char device numbers.  The major number will be 
  9.  * chosen dynamically, and returned (along with the first minor number) 
  10.  * in @dev.  Returns zero or a negative error code. 
  11.  */  
  12. int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,const char *name)  

2.register_chrdev_region()

功能:注册连续的若干个设备号,用这个函数要自己指定主设备号。

函数原型:

[cpp] view plaincopy
  1. /** 
  2.  * register_chrdev_region() - register a range of device numbers 
  3.  * @from: the first in the desired range of device numbers; must include 
  4.  *        the major number. 
  5.  * @count: the number of consecutive device numbers required 
  6.  * @name: the name of the device or driver. 
  7.  * 
  8.  * Return value is zero on success, a negative error code on failure. 
  9.  */  
  10. int register_chrdev_region(dev_t from, unsigned count, const char *name)  

注: register_chrdev() 函数也用于分配设备号,但是用于2.6版本之前的内核中,在之后的版本中不再使用。

3.unregister_chrdev_region()

功能:注销由alloc_chrdev_region()或者register_chrdev_region()注册的设备号,一般在驱动模块的退出函数中要执行此操作,将注册的设备号还给系统。

函数原型:

[cpp] view plaincopy
  1. /** 
  2.  * unregister_chrdev_region() - return a range of device numbers 
  3.  * @from: the first in the range of numbers to unregister 
  4.  * @count: the number of device numbers to unregister 
  5.  * 
  6.  * This function will unregister a range of @count device numbers, 
  7.  * starting with @from.  The caller should normally be the one who 
  8.  * allocated those numbers in the first place... 
  9.  */  
  10. void unregister_chrdev_region(dev_t from, unsigned count)  

4.cdev_alloc()

功能:分配一个表示字符设备的cdev结构体。
函数原型:
[cpp] view plaincopy
  1. /** 
  2.  * cdev_alloc() - allocate a cdev structure 
  3.  * 
  4.  * Allocates and returns a cdev structure, or NULL on failure. 
  5.  */  
  6. struct cdev *cdev_alloc(void)  

5.cdev_init()

功能:初始化由cdev_alloc()分配的表示字符设备的cdev结构体,并通过向该函数传入的struct file_operations *fops参数来指定该字符设备的文件操作函数。
函数原型:
[cpp] view plaincopy
  1. /** 
  2.  * cdev_init() - initialize a cdev structure 
  3.  * @cdev: the structure to initialize 
  4.  * @fops: the file_operations for this device 
  5.  * 
  6.  * Initializes @cdev, remembering @fops, making it ready to add to the 
  7.  * system with cdev_add(). 
  8.  */  
  9. void cdev_init(struct cdev *cdev, const struct file_operations *fops)  

6.cdev_add()

功能:添加一个(由cdev_alloc()分配并通过cdev_init()初始化的)字符设备到系统中。
函数原型:
[cpp] view plaincopy
  1. /** 
  2.  * cdev_add() - add a char device to the system 
  3.  * @p: the cdev structure for the device 
  4.  * @dev: the first device number for which this device is responsible 
  5.  * @count: the number of consecutive minor numbers corresponding to this 
  6.  *         device 
  7.  * 
  8.  * cdev_add() adds the device represented by @p to the system, making it 
  9.  * live immediately.  A negative error code is returned on failure. 
  10.  */  
  11. int cdev_add(struct cdev *p, dev_t dev, unsigned count)  

7.cdev_del()

功能:执行和cdev_add()相反的操作,将一个指定的字符设备从系统中删除。
函数原型:
[cpp] view plaincopy
  1. /** 
  2.  * cdev_del() - remove a cdev from the system 
  3.  * @p: the cdev structure to be removed 
  4.  * 
  5.  * cdev_del() removes @p from the system, possibly freeing the structure 
  6.  * itself. 
  7.  */  
  8. void cdev_del(struct cdev *p)  
以下为一个简单的字符设备初始化函数,函数中列出了向系统中添加一个字符设备的简单步骤:
[cpp] view plaincopy
  1. struct cdev *myDev_dev;  
  2. /*主设备和从设备号变量*/  
  3. static int myDev_major = 0;  
  4. static int myDev_minor = 0;  
  5.   
  6. static int __init myDev_init(void)  
  7. {   
  8.     int err = -1;  
  9.     dev_t dev = 0;  
  10.   
  11.     /*1.动态分配主设备号和从设备号,第三个参数表示分配的此设备号的数量*/  
  12.     err = alloc_chrdev_region(&dev, 0, TOTAL_DEVICE_COUNT, "myDev");  
  13.     if(err < 0) {  
  14.         printk(KERN_ALERT"Failed to alloc char dev region.\n");  
  15.         return err;  
  16.     }  
  17.   
  18.     myDev_major = MAJOR(dev);  
  19.     myDev_minor = MINOR(dev);          
  20.   
  21.     /*2.分配cdev字符设备结构体变量*/  
  22.     myDev_dev = cdev_alloc();  
  23.     if(!myDev_dev) {  
  24.         err = -ENOMEM;  
  25.         printk(KERN_ALERT"Failed to alloc myDev_dev.\n");  
  26.         return err;  
  27.     }          
  28.     /*3.初始化cdev字符设备结构体*/  
  29.     cdev_init(myDev_dev, &myDev_fops);   
  30.   
  31.     /*4.添加字符设备到系统中*/  
  32.     err = cdev_add(myDev_dev,dev, TOTAL_DEVICE_COUNT);  
  33.     if(err) {  
  34.         return err;  
  35.     }                
  36.     return err;  
  37. }  

自动创建设备节点相关函数

1.class_create()

功能:创建一个struct class结构体,作为device_create()函数的参数。
函数原型:
[cpp] view plaincopy
  1. #define class_create(owner, name)       \  
  2. ({                      \  
  3.     static struct lock_class_key __key; \  
  4.     __class_create(owner, name, &__key);    \  
  5. })  

2.device_create()

功能:该函数创建一个设备并将其注册到sysfs中,同时在系统的sys/class和sys/device目录下会生成相应的类和设备入口。并且,该函数还会出发用户空间udev的动作,udev会根据sysfs下的class在/dev目录下创建设备节点,这也为自动创建设备节点提供了一种途径。通过device_create()函数,我们就可以不用通过mknod命令手动的创建设备节点了。
函数原型:
[cpp] view plaincopy
  1. /** 
  2.  * device_create - creates a device and registers it with sysfs 
  3.  * @class: pointer to the struct class that this device should be registered to 
  4.  * @parent: pointer to the parent struct device of this new device, if any 
  5.  * @devt: the dev_t for the char device to be added 
  6.  * @drvdata: the data to be added to the device for callbacks 
  7.  * @fmt: string for the device's name 
  8.  * 
  9.  * This function can be used by char device classes.  A struct device 
  10.  * will be created in sysfs, registered to the specified class. 
  11.  * 
  12.  * A "dev" file will be created, showing the dev_t for the device, if 
  13.  * the dev_t is not 0,0. 
  14.  * If a pointer to a parent struct device is passed in, the newly created 
  15.  * struct device will be a child of that device in sysfs. 
  16.  * The pointer to the struct device will be returned from the call. 
  17.  * Any further sysfs files that might be required can be created using this 
  18.  * pointer. 
  19.  * 
  20.  * Returns &struct device pointer on success, or ERR_PTR() on error. 
  21.  * 
  22.  * Note: the struct class passed to this function must have previously 
  23.  * been created with a call to class_create(). 
  24.  */  
  25. struct device *device_create(struct class *classstruct device *parent,  
  26.                  dev_t devt, void *drvdata, const char *fmt, ...)  

3.device_create_file()

功能:该函数用于为指定设备在sysfs下建立属性文件,如:
device_create_file(dev, &dev_attr_val);
其中的dev_attr_val可以通过DEVICE_ATTR宏进行定义,也可以直接通过struct device_attribute xxx=__ATTR(_name,_mode,_show,_store)进行定义,区别就是用DEVICE_ATTR宏进行定义时得到的变量名都是以dev_attr_为前缀的,而通过struct device_attribute xxx=__ATTR(_name,_mode,_show,_store)进行定义时,变量名可以任意指定。
函数原型:
[cpp] view plaincopy
  1. /** 
  2.  * device_create_file - create sysfs attribute file for device. 
  3.  * @dev: device. 
  4.  * @attr: device attribute descriptor. 
  5.  */  
  6.    
  7. int device_create_file(struct device *dev,const struct device_attribute *attr)  
  8.   
  9.   
  10. #define DEVICE_ATTR(_name, _mode, _show, _store) \  
  11. struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)  
如下代码可以实现自动创建设备节点:

[cpp] view plaincopy
  1. static struct class* myDev_class = NULL;  
  2. struct device* devc= NULL;        
  3.   
  4. /*5.创建一个struct class结构体*/  
  5. myDev_class = class_create(THIS_MODULE,"myDevClass");  
  6. if(IS_ERR(myDev_class)) {  
  7.     err = PTR_ERR(myDev_class);  
  8.     printk(KERN_ALERT"Failed to create  class.\n");  
  9.     return err;  
  10. }          
  11. /*6.在sysf中生成设备文件并在/dev下自动生成设备节点*/  
  12. devc = device_create(myDev_class,NULL,dev,NULL,"myDev");  
  13. if(IS_ERR(devc)) {  
  14.     err = PTR_ERR(devc);  
  15.     printk(KERN_ALERT"Failed to create device.");  
  16.     return err;  
  17. }     

以下贴出以上分两部分介绍的字符设备的初始化代码:
[cpp] view plaincopy
  1. struct cdev *myDev_dev;  
  2. /*主设备号和从设备号变量*/  
  3. static int myDev_major = 0;  
  4. static int myDev_minor = 0;  
  5.   
  6.   
  7. static int __init myDev_init(void)  
  8. {   
  9.     int err = -1;  
  10.     dev_t dev = 0;  
  11.     struct device *devc = NULL;  
  12.   
  13.     /*1.动态分配主设备号和从设备号,第三个参数表示分配的此设备号的数量*/  
  14.     err = alloc_chrdev_region(&dev, 0, TOTAL_DEVICE_COUNT, "myDev");  
  15.     if(err < 0) {  
  16.         printk(KERN_ALERT"Failed to alloc char dev region.\n");  
  17.         return err;  
  18.     }  
  19.   
  20.     myDev_major = MAJOR(dev);  
  21.     myDev_minor = MINOR(dev);          
  22.   
  23.     /*2.分配cdev字符设备结构体变量*/  
  24.     myDev_dev = cdev_alloc();  
  25.     if(!myDev_dev) {  
  26.         err = -ENOMEM;  
  27.         printk(KERN_ALERT"Failed to alloc myDev_dev.\n");  
  28.         return err;  
  29.     }          
  30.     /*3.初始化cdev字符设备结构体*/  
  31.     cdev_init(myDev_dev, &myDev_fops);   
  32.   
  33.     /*4.添加字符设备到系统中*/  
  34.     err = cdev_add(myDev_dev,dev, TOTAL_DEVICE_COUNT);  
  35.     if(err) {  
  36.         return err;  
  37.     }                
  38.   
  39.     /*5.创建一个struct class结构体*/  
  40.     myDev_class = class_create(THIS_MODULE,"myDevClass");  
  41.     if(IS_ERR(myDev_class)) {  
  42.         err = PTR_ERR(myDev_class);  
  43.         printk(KERN_ALERT"Failed to create  class.\n");  
  44.         return err;  
  45.     }          
  46.     /*6.在sysf中生成设备文件并在/dev下自动生成设备节点*/  
  47.     devc = device_create(myDev_class,NULL,dev,NULL,"myDev");  
  48.     if(IS_ERR(devc)) {  
  49.         err = PTR_ERR(devc);  
  50.         printk(KERN_ALERT"Failed to create device.");  
  51.         return err;  
  52.     }     
  53.     return err;  
  54. }  

通用函数

1.kmalloc()和kzalloc()的异同

在内核include/linux/slab.h文件中有如下定义:

[cpp] view plaincopy
  1. /** 
  2.  * kzalloc - allocate memory. The memory is set to zero. 
  3.  * @size: how many bytes of memory are required. 
  4.  * @flags: the type of memory to allocate (see kmalloc). 
  5.  */  
  6. static inline void *kzalloc(size_t size, gfp_t flags)  
  7. {  
  8.     return kmalloc(size, flags | __GFP_ZERO);  
  9. }  

从注释以及函数原型均能看出kzalloc()函数的作用,即申请一块内存并且该内存会在函数调用过程中被清零。

2.module_platform_driver()

该函数实际是一个宏,它在include/linux/platform_device.h中定义如下:

[cpp] view plaincopy
  1. /* module_platform_driver() - Helper macro for drivers that don't do 
  2.  * anything special in module init/exit.  This eliminates a lot of 
  3.  * boilerplate.  Each module may only use this macro once, and 
  4.  * calling it replaces module_init() and module_exit() 
  5.  */  
  6. #define module_platform_driver(__platform_driver) \  
  7.     module_driver(__platform_driver, platform_driver_register, \  
  8.             platform_driver_unregister)  

其中的module_driver在/include/linux/device.h中定义,如下:
[cpp] view plaincopy
  1. /** 
  2.  * module_driver() - Helper macro for drivers that don't do anything 
  3.  * special in module init/exit. This eliminates a lot of boilerplate. 
  4.  * Each module may only use this macro once, and calling it replaces 
  5.  * module_init() and module_exit(). 
  6.  * 
  7.  * @__driver: driver name 
  8.  * @__register: register function for this driver type 
  9.  * @__unregister: unregister function for this driver type 
  10.  * @...: Additional arguments to be passed to __register and __unregister. 
  11.  * 
  12.  * Use this macro to construct bus specific macros for registering 
  13.  * drivers, and do not use it on its own. 
  14.  */  
  15. #define module_driver(__driver, __register, __unregister, ...) \  
  16. static int __init __driver##_init(void) \  
  17. { \  
  18.     return __register(&(__driver) , ##__VA_ARGS__); \  
  19. } \  
  20. module_init(__driver##_init); \  
  21. static void __exit __driver##_exit(void) \  
  22. { \  
  23.     __unregister(&(__driver) , ##__VA_ARGS__); \  
  24. } \  
  25. module_exit(__driver##_exit);  

由上述定义可知,module_platform_driver()宏的作用就是定义指定名称的平台设备驱动注册函数和平台设备驱动注销函数,并且在函数体内分别通过platform_driver_register()函数和platform_driver_unregister()函数注册和注销该平台设备驱动。

3.read_cpuid_id()

读取CPU ID,定义于arch/arm/include/asm/cputype.h文件中。

4.

0 0
原创粉丝点击