遇到一个内核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);

Clearly, you should not access the cdev structure after passing it to cdev_del .

还有几个API没用到,有缘遇到再说吧。。。哈哈哈

linux内核头文件 cdev.h 解析的更多相关文章

  1. Linux内核头文件与内核与库的关系

    看上一篇文章中对buildroot的介绍,里面的文档第 3.1.1.1 Internal toolchain backend 节内容 C库会去访问Linux kernel headers(*.h)文件 ...

  2. linux 内核头文件 linux kernel header

    概述:在进行有关系统软件的安装的时候(编译一个新的驱动,或者安装一个系统级别的测试工具,例如systemtap),经常需要重新编译内核,相应的问题往往与内核头文件有关.那么,什么是内核头文件,为什么需 ...

  3. 单片机中用c编程时头文件reg51.h及reg52.h解析

    单片机中用c编程时头文件reg51.h及reg52.h解析 我们在用c语言编程是往往第一行就是reg51.h或者其他的自定义头文件,我们怎么样来理解呢? 1)“文件包含”处理. 程序的第一行是一个“文 ...

  4. 内存管理pbuf.h头文件源码解析——LwIP学习

    声明:个人所写所有博客均为自己在学习中的记录与感想,或为在学习中总结他人学习成果,但因本人才疏学浅,如果大家在阅读过程中发现错误,欢迎大家指正. LwIP的内核(core文件夹)文件中pbuf.c是包 ...

  5. 文件类型分类:头文件dirent.h中定义的文件类型与linux内文件符号对应关系

    头文件 dirent.h 定义了文件类型: enum{    DT_UNKNOWN = 0,         //未知类型    DT_FIFO = 1,            //first in, ...

  6. linux常用头文件及说明

    linux常用头文件及说明 1. Linux中一些头文件的作用: <assert.h>:ANSI C.提供断言,assert(表达式)<glib.h>:GCC.GTK,GNOM ...

  7. Linux内核很吊之 module_init解析 (下)【转】

    转自:https://blog.csdn.net/richard_liujh/article/details/46758073 版权声明:本文为博主原创文章,未经博主允许不得转载. https://b ...

  8. linux编程头文件所在路径的问题

    一.问题引入 1.头文件与库 当我们在PC主机linux环境下(如ubuntu),编写linux应用程序,然后利用gcc来编译.在源代码的开始位置会写入头文件,那是因为我们使用了系统提供的库函数,例如 ...

  9. Linux内核DTB文件启动的几种方式

      版权:  凌云物网智科实验室< www.iot-yun.com > 声明:  本文档由凌云物网智科实验室郭工编著! 作者:  郭文学< QQ: 281143292  guowen ...

随机推荐

  1. vim 技巧之用宏命令批量处理文件

    今天遇到了一种情况,就是我需要同时修改34个文件中的某些字符串的内容,如果一个个打开需改的话,那也太麻烦了.后来就想着能不能通过vim的宏命令来修改呢?现在就总结下关于宏在文件列表中的应用1.首先,我 ...

  2. ubuntu-删除内核

    今天进入公司第一天,公司需要给电脑安装ubuntu,这个是由it部门帮忙安装的.但是,我不小心升级了内核版本,接下来就悲剧了,因为内核版本升级以后,直接导致了环境错误,很多公司内部使用的工具都不能用了 ...

  3. css选择器和优先级总结

    本文转自http://www.cnblogs.com/zxjwlh/p/6213239.html CSS三大特性—— 继承. 优先级和层叠. 继承:即子类元素继承父类的样式; 优先级:是指不同类别样式 ...

  4. POJ 3037 SPFA

    题意: 思路: 我们可以发现 到每个点的速度是一样的 那这就成水题了-. 裸的SPFA跑一哈 搞定 //By SiriusRen #include <cmath> #include < ...

  5. 为root账户更名

    为root账户更名 处于安全考虑许多管理员想把root更名,具体方法如下: 1.先以root登陆系统 2.用vi 编辑/etc/passwd文件,将第一行的第一个root修改为你想要的账户名,然后保存 ...

  6. Android ijkplayer在windows下编译并导入Android Studio

     我是看着里面的步骤来做的,由于我自己对Linux环境和命令不熟悉,导致我对Cygwin的知识为零,在编译ijkplayer的时候走了一点弯路,需要的同学先去看一下上面的这篇文章,我这边是对上面文章做 ...

  7. vuejs模板中使用html代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 洛谷 P1850 换教室

    P1850 换教室 题目描述 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程. 在可以选择的课程中,有 2n2n 节课程安排在 nn 个时间段上.在第 ii(1 \leq ...

  9. linux设置tab键的宽度为4

    先cd 到~目录 ~$ cd ~$ vi .vimrc set nu

  10. Day3晚笔记

    DEV C++扩展栈空间 -Wl,--stack=64000000000 带权二分图匹配 建一个超级源点S,超级汇点T 把左边的点的点权作为权值,连一条S到左边的点的边 把右边的点的点权作为权值,连一 ...