linux内核头文件 cdev.h 解析

遇到一个内核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 解析

时间: 2024-08-03 06:20:19

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

linux内核头文件kdev_t.h 宏定义解析

kdev_t.h 宏定义解析 这个header file反正不多,直接原因是--遇到了,就搞定它! dev_t 类型的变量定义在linux/types.h 用来保存设备编号--包括主设备号和次设备号.dev_t 是一个32位的数,其中12位用来表示设备号,其余20位用来表示次设备号. 始终不要对这32位是高12位是主设备号还是低2位是主设备号做出假定,不利于代码的可移植性,始终记得使用宏定义来处理dev_t ! 都在这里了: #define MINORBITS 20 //次设备号的占位数目 #d

linux 内核头文件及内核库文件

与用户空间应用不同的是,内核不能调用标准C库或其它任何不属于内核的库.主要原因是,速度及大小的限制. 标准C库对内核而言实在是太大了. 许多常用的的 libc 函数已经在内核中实现了.内核中的库文件是在内核的根目录下 lib 目录下存放. 如常用的字符串操作函数是在 lib/string.c 中定义的,调用时主需要将头文件 <linux/string.h>包含进即可. 关于内核的头文件,内核是不能包含内核之外的头文件的,如同内核不能调用内核之外的库一样. 1)基本的头文件放在内核的 inclu

Linux下 头文件&lt;getopt.h&gt; 及其函数的应用

本文内容摘抄自:https://www.cnblogs.com/qingergege/p/5914218.html 函数一: int getopt(int argc,    char* argv[],   const char* optstring); 参数1.2为 main函数的输入参数,参数3 为选项字符串,函数返回值为选项字符. <getopt.h 文件内部的参数: 1. extern char* optarg ;   用于保存选项. 2. extern int   optind,  用来

linux自学心得之--安装内核头文件

对于linux初学者来说有很多的问题需要去探索,不能完全按照书本上的章节顺序学习,有些知识和问题就像是安装软件一样有依赖关系.刚安装linux系统需要安装驱动,安装驱动就要提前安装内核头文件,不然会报各种错误,比如找不到目录,xxx不存在.安装内核头文件的时候可能又会遇到"未发现软件包......"     废话少说,下面就讲一下如何安装内核头文件,本文以kali linux为例,kali linux是基于Debian的Linux发行版,本文章适用于其他基于Debian的linux. 

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

头文件 dirent.h 定义了文件类型: enum{    DT_UNKNOWN = 0,         //未知类型    DT_FIFO = 1,            //first in, first out 类似于管道, 有名管道    DT_CHR = 2,             //字符设备文件    DT_DIR = 4,             //目录    DT_BLK = 6,             //块设备文件    DT_REG = 8,          

linux驱动程序头文件

1.编写任何驱动程序都必须带的头文件 #include <linux/module.h>:在编写任何模块都需要包含此头文件.该头文件自动包含了 <linux/version.h>头文件,该头文件包含了宏                                                                                                  MODULE_LICENSE("GPL")的定义. #include

linux常用头文件及说明

linux常用头文件及说明 1. Linux中一些头文件的作用: <assert.h>:ANSI C.提供断言,assert(表达式)<glib.h>:GCC.GTK,GNOME的基础库,提供很多有用的函数,如有数据结构操作函数.使用glib只需要包含<glib.h><dirent.h>:GCC.文件夹操作函数.struct dirent,struct DIR,opendir(),closedir(),readdir(),readdir64()等 <c

VMVare9.0, Ubuntu12.04安装VMWare Tools找不到内核头文件解决方案

VMVare9.2, Ubuntu14.04安装VMWare Tools 遇到一点问题,提示找不到内核头文件,于是baidu之,作如下总结. 首先执行: sudo apt-get install build-essential linux-headers-$(uname -r) sudo cp /lib/modules/3.13.0-24-generic/build/include/generated/uapi/linux/version.h /lib/modules/3.13.0-24-gen

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

一.问题引入 1.头文件与库 当我们在PC主机linux环境下(如ubuntu),编写linux应用程序,然后利用gcc来编译.在源代码的开始位置会写入头文件,那是因为我们使用了系统提供的库函数,例如printf.open.read.write等等.我们会写入类似的内容: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> ...... 我们的应用程序代码