码迷,mamicode.com
首页 > Windows程序 > 详细

05 字符设备的API

时间:2016-12-04 19:35:08      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:自动   with   hit   res   文件系统   出错   move   line   number   

一、字符设备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 的指针变量

返回值:

 

/**
* 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, ...)

头文件:#include <linux/device.h>

功能:创建一个设备,并注册到sys文件系统中

参数:

@class

@parent  NULL

@devt   设备号

@drvdata  NULL

@fmt    设备号的名字

返回值:

返回一个struct device的指针变量,通过IS_ERR()判断出错,通过PTR_ERR()获取负的错误码。 

 

/**
* 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)

头文件:#include <linux/device.h>

功能:销毁设备

参数:

@class  

@dev_t  设备号

返回值:

 三、内核中错误码

头文件:#include <linux/err.h>

/*
* Kernel pointers have redundant information, so we can use a
* scheme where we can return either an error code or a normal
* pointer with the same return value.
*
* This should be a per-architecture thing, to allow different
* error and pointer decisions.
*/
#define MAX_ERRNO 4095

#ifndef __ASSEMBLY__

#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)

static inline void * __must_check ERR_PTR(long error)
{
return (void *) error;
}

static inline long __must_check PTR_ERR(__force const void *ptr)
{
return (long) ptr;
}

static inline bool __must_check IS_ERR(__force const void *ptr)
{
return IS_ERR_VALUE((unsigned long)ptr);
}

static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
{
return !ptr || IS_ERR_VALUE((unsigned long)ptr);
}

05 字符设备的API

标签:自动   with   hit   res   文件系统   出错   move   line   number   

原文地址:http://www.cnblogs.com/gaoningbo/p/6131061.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!