注册字符驱动的一种老方法:

注册一个字符设备的经典方法是使用:
int register_chrdev(unsigned int major, const char *name, structfile_operations *fops);
这里, major 的主编号, name 是驱动的名子(出现在 /proc/devices),
fops   file_operations 结构. 一个对 register_chrdev 的调用为给定的主编号注册 0 - 255 的次编号, 并且为每一个建立一个缺省的 cdev 结构。使用这个接口的驱动必须准备好处理对所有 256 个次编号的 open 调用( 不管它们是否对应真实设备 ), 它们不能使用大于 255 的主或次编号。
如果你使用 register_chrdev, 从系统中去除你的设备的正确的函数是:

int unregister_chrdev(unsigned int major, const char *name);
major 和 name 必须和传递给 register_chrdev 的相同, 否则调用会失败.

现在常用的方法,cdev

包含 <linux/cdev.h>, 这个结构和它的关联帮助函数定义在这里.

/**
* 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)
{
memset(cdev, , sizeof *cdev);
INIT_LIST_HEAD(&cdev->list);
kobject_init(&cdev->kobj, &ktype_cdev_default);
cdev->ops = fops;
}

struct cdev 有一个拥有者成员, 应当设置为THIS_MODULE.

一旦 cdev 结构建立, 最后的步骤是把它告诉内核, 调用:
int cdev_add(struct cdev *dev, dev_t num, unsigned int count);

/**
* 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->dev = dev;
p->count = count;
return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
}

这里, dev 是 cdev 结构, dev 是这个设备响应的第一个设备号, count 是应当关联到设备的设备号的数目. 常常 count 是 1

在使用 cdev_add 是有几个重要事情要记住. 除非你的驱动完全准备好处理设备上的操作, 否则你不应当调用 cdev_add。

第一个是这个调用可能失败. 如果它返回一个负的错误码, 你的设备没有增加到系统中.

它几乎会一直成功,但是, 并且带起了其他的点: cdev_add 返回, 你的设备就是"活的",并且内核可以调用它的操作.

从系统去除一个字符设备, 调用:
void cdev_del(struct cdev *dev);

/**
* 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)
{
cdev_unmap(p->dev, p->count);
kobject_put(&p->kobj);
}

显然, 你不应当在传递给 cdev_del 后存取 cdev 结构。

引入 2 个核心函数来管理 Linux 内核中的内存. 这些函数, 定义
在 <linux/slab.h>, 是:
void *kmalloc(size_t size, int flags);

void kfree(void *ptr);

 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

 /**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *))->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
container_of的功能,通过结构体成员的地址,获得结构体的首地址。

以下是简单用法:

Linux字符设备学习,总结的更多相关文章

  1. linux字符设备学习笔记【原创】

    1.申请设备号 int register_chrdev_region(dev_t from, unsigned count, const char *name) 指定从设备号from开始,申请coun ...

  2. Smart210学习记录----beep linux字符设备驱动

    今天搞定了beep linux字符设备驱动,心里还是很开心的,哈哈...但在完成的过程中却遇到了一个非常棘手的问题,花费了我大量的时间,,,, 还是把问题描述一下吧,好像这个问题很普遍的,网上许多解决 ...

  3. Linux字符设备驱动实现

    Linux字符设备驱动实现 要求 编写一个字符设备驱动,并利用对字符设备的同步操作,设计实现一个聊天程序.可以有一个读,一个写进程共享该字符设备,进行聊天:也可以由多个读和多个写进程共享该字符设备,进 ...

  4. (57)Linux驱动开发之三Linux字符设备驱动

    1.一般情况下,对每一种设备驱动都会定义一个软件模块,这个工程模块包含.h和.c文件,前者定义该设备驱动的数据结构并声明外部函数,后者进行设备驱动的具体实现. 2.典型的无操作系统下的逻辑开发程序是: ...

  5. 深入理解Linux字符设备驱动

    文章从上层应用访问字符设备驱动开始,一步步地深入分析Linux字符设备的软件层次.组成框架和交互.如何编写驱动.设备文件的创建和mdev原理,对Linux字符设备驱动有全面的讲解.本文整合之前发表的& ...

  6. Linux字符设备驱动结构(一)--cdev结构体、设备号相关知识机械【转】

    本文转载自:http://blog.csdn.net/zqixiao_09/article/details/50839042 一.字符设备基础知识 1.设备驱动分类 linux系统将设备分为3类:字符 ...

  7. Linux字符设备中的两个重要结构体(file、inode)

    对于Linux系统中,一般字符设备和驱动之间的函数调用关系如下图所示 上图描述了用户空间应用程序通过系统调用来调用程序的过程.一般而言在驱动程序的设计中,会关系 struct file 和 struc ...

  8. Linux字符设备简单示例

    1. Linux字符设备是一种按字节来访问的设备,字符驱动则负责驱动字符设备,这样的驱动通常实现open.close.read和write系统调用.例如:串口.Led.按键等. 2. 通过字符设备文件 ...

  9. Linux字符设备驱动基本结构

    1.Linux字符设备驱动的基本结构 Linux系统下具有三种设备,分别是字符设备.块设备和网络设备,Linux下的字符设备是指只能一个字节一个字节读写的设备,不能随机读取设备内存中某一数据,读取数据 ...

随机推荐

  1. input输入框数字验证

    正则限制input只能输入大于零的数字:onkeyup="this.value=this.value.replace(/\D|^0/g,'')" onafterpaste=&quo ...

  2. 架设Git服务器

    架设Git服务器项目 Git Candy:http://github.com/Aimeast/GitCandy/ AE大神的作品, Git Candy© 是一个基于ASP.NET MVC的Git分布式 ...

  3. 如何使用Adobe Reader复制PDF文档上的文字

    PDF文档大家常用,但是有没有简单的方法能够提取PDF文档上的文字,然后使用呢?除了将PDF转换成Word,这里介绍一种更为简单实用的方法复制PDF文本文字,Adobe Reader是大家都常用的PD ...

  4. Spring-aop实现切面的四种方式 (2)

    AOP实现方式一 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...

  5. June 29th 2017 Week 26th Thursday

    Hope for the best, but prepare for the worst. 做最好的期望,做最坏的打算. Always remember that quotes about being ...

  6. JS解析json数据(如何将json字符串转化为数组)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...

  7. Kali-linux识别活跃的主机

    尝试渗透测试之前,必须先识别在这个目标网络内活跃的主机.在一个目标网络内,最简单的方法将是执行ping命令.当然,它可能被一个主机拒绝,也可能被接收.本节将介绍使用Nmap工具识别活跃的主机. 网络映 ...

  8. mongodb安装(比较全一点)

    Linux下MongoDB安装和配置详解  转:https://www.cnblogs.com/pfnie/articles/6759105.html 一.创建MongoDB的安装路径 在/usr/l ...

  9. mysql使用Navicat 导出和导入数据库

    系统环境: Win7 x64软件准备:Navicat Premium_11.2.7简体中文版下载网址:http://www.cr173.com/soft/419023.html 现在我就向大家介绍 m ...

  10. java 编写小工具 尝试 学习(一)

    1.单片机 调试经常 需要  用 串口 工具 发送 一些 特定的  协议或者 命令,每次要 翻译  写成 2进制 很麻烦 ,因此 打算自己用 java 写一个 工具 方便自己  调试,2017年3月2 ...