标签:des style blog http color os 文件 io
遇到一个内核API——cdev_init 就找到这里来了。
#ifndef _LINUX_CDEV_H #define _LINUX_CDEV_H #include <linux/kobject.h #include <linux/kdev_t.h> #include <linux/list.h> struct file_operations; struct inode; struct module; struct cdev { struct kobject kobj; struct module *owner; const struct file_operations *ops; struct list_head list; dev_t dev; unsigned int count; }; void cdev_init(struct cdev *, const struct file_operations *); //初始化字符设备可以进行的文件操作——file_operations 结构体记录了所有可以对cdev结构体描述的字符设备进行的操作 struct cdev *cdev_alloc(void); void cdev_put(struct cdev *p); int cdev_add(struct cdev *, dev_t, unsigned); void cdev_del(struct cdev *); void cd_forget(struct inode *); extern struct backing_dev_info directly_mappable_cdev_bdi; #endif
作者也不写个API的说明。。。以后用到其他的API再update。。。no zuo no die...
没想到一个小时之后就update 了哈。。。。。
update:2014年07月29日 凌晨
Char Device Registration
As we mentioned, the kernel uses structures of type
struct cdev to represent char devices internally. Before the kernel invokes your device’s operations, you must allocate and register one or more of these structures.
先申请cdev结构体
To do so, your code should include <linux/cdev.h>, where the structure and its associated helper functions are defined.
There are two ways of allocating and initializing one of these structures. If you wish to obtain a standal one cdev structure at runtime, you may do so with code such as:
struct cdev *my_cdev = cdev_alloc(); my_cdev->ops = &my_fops;初始化cdev结构体
Chances are, however, that you will want to embed the cdev structure within a device-specific structure of your own; that is what scull does. In that case, you should initialize the structure that you have already
allocated with:
void cdev_init(struct cdev *cdev, struct file_operations *fops);
Either way, there is one other struct cdev field that you need to initialize. Like the file_operations structure, struct cdev has an owner field that should
be set to THIS_MODULE .(这里是,例如,struct cdev* dev; dev->owener = THIS_MODULE.)
cdev结构体初始化完事之后,就需要把设备加入到内核中了,调用cdev_add
Once the cdev structure is set up, the final step is to tell the kernel about it with a call to: int
cdev_add(struct cdev *dev, dev_t num, unsigned int count);
Here,dev is the cdev structure,num is the first device number to which this device responds, and count is the number of device numbers that should be associated with the device. Often count is one, but there
are situations where it makes sense to have more than one device number correspond to a specific device. Consider, for example, the SCSI tape driver, which allows user space to select operating modes (such as density) by assigning multiple minor numbers to
each physical device.
ATTENTION!
There are a couple of important things to keep in mind when using cdev_add . The first is that this call can fail. If it returns a negative error code, your device has not been added to the system. It almost always
succeeds, however, and that brings up the other point: as soon as cdev_add returns, your device is “live” and its operations
can be called by the kernel. You should not call cdev_add until your driver is completely ready to handle operations on the device.
不用cdev设备的话就调用cdev_dev
To remove a char device from the system, call:
void cdev_del(struct cdev *dev);
还有几个API没用到,有缘遇到再说吧。。。哈哈哈
linux内核头文件 cdev.h 解析,布布扣,bubuko.com
标签:des style blog http color os 文件 io
原文地址:http://blog.csdn.net/cinmyheart/article/details/38238557