标签:
一、字符设备API 1. cdev_init()/**
* cdev_init() - initialize a cdev structure
* @cdev: the structure to initialize
* @fops: the file_operations for this device
*
* Initializes @cdev, remembering @fops, making it ready to add to the
* system with cdev_add().
*/
void cdev_init(struct cdev *cdev, const struct file_operations *fops)
头文件:#include <linux/cdev.h>
功能:初始化cdev结构体
参数:
@cdev cdev结构体
@fops 操作函数的结构体
返回值:
无
2. register_chrdev_region()/**
* register_chrdev_region() - register a range of device numbers
* @from: the first in the desired range of device numbers; must include
* the major number.
* @count: the number of consecutive device numbers required
* @name: the name of the device or driver.
*
* Return value is zero on success, a negative error code on failure.
*/
int register_chrdev_region(dev_t from, unsigned count, const char *name)
头文件:#include <linux/cdev.h>
功能:静态注册设备号,必须包含主设备号
参数:
@from 设备号(主设备号(12bit)+次设备号(20bit)),由MKDEV(MAJOR, MINOR)的返回值得到
@count 设备号的个数
@name 设备的名字 (在cat /proc/devices看到它)
返回值:
成功返回0,失败返回负的错误码。
3. unregister_chrdev_region()/**
* unregister_chrdev_region() - return a range of device numbers
* @from: the first in the range of numbers to unregister
* @count: the number of device numbers to unregister
*
* This function will unregister a range of @count device numbers,
* starting with @from. The caller should normally be the one who
* allocated those numbers in the first place...
*/
void unregister_chrdev_region(dev_t from, unsigned count)
头文件:#include <linux/cdev.h>
功能:注销设备号
参数:
@from 设备号
@count 次设备的个数
返回值:
无
4. cdev_add()/**
* cdev_add() - add a char device to the system
* @p: the cdev structure for the device
* @dev: the first device number for which this device is responsible
* @count: the number of consecutive minor numbers corresponding to this
* device
*
* cdev_add() adds the device represented by @p to the system, making it
* live immediately. A negative error code is returned on failure.
*/
int cdev_add(struct cdev *p, dev_t dev, unsigned count)
功能:添加字符设备
参数:
@p cdev结构体
@dev 设备号(第一个设备号)
@count 次设备的个数
返回值:
成功返回0,失败返回负的错误码。
/**
* cdev_del() - remove a cdev from the system
* @p: the cdev structure to be removed
*
* cdev_del() removes @p from the system, possibly freeing the structure
* itself.
*/
void cdev_del(struct cdev *p)
功能:删除字符设备
参数:
@p cdev结构体
一、动态注册设备号/**
* alloc_chrdev_region() - register a range of char device numbers
* @dev: output parameter for first assigned number
* @baseminor: first of the requested range of minor numbers
* @count: the number of minor numbers required
* @name: the name of the associated device or driver
*
* Allocates a range of char device numbers. The major number will be
* chosen dynamically, and returned (along with the first minor number)
* in @dev. Returns zero or a negative error code.
*/
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
头文件:#include <linux/cdev.h>
功能:让系统自动分配一个设备号,并注册
参数:
@dev 用来获得系统分配的设备号
@baseminor 第一个次设备号
@count 次设备号个数
@name 设备的名字
返回值:
成功返回0,,失败返回负的错误码。
二、自动创建设备节点/* This is a #define to keep the compiler from merging different
* instances of the __key variable */
#define class_create(owner, name) \
({ \
static struct lock_class_key __key; \
__class_create(owner, name, &__key); \
})
头文件:#include <linux/device.h>
功能:创建设备类
参数:
@owner THIS_MODULE
@name 设备类的名字
返回值:
返回一个struct class结构的指针,然后通过IS_ERR()判断是否出错,通过PTR_ERR()获取负的返回值。
/**
* 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)
头文件:#include <linux/device.h>
功能:删除设备类
参数:
@cls struct class 的指针变量
返回值:
无