The Internal Representation of Device Numbers

Within the kernel,the dev_t type(defined in linux/types.h ) is used to hold device numbers---both the major and minor parts.

it should,instead,make use of a set of macros found in linux/kdev_t.h.To obtain the major or minor parts of a dev_t,use:

MAJOR(dev_t dev);
MINOR(dev_t dev);

If,instead,you have the major and minor numbers and need to turn them into a dev_t,use:

MKDEV(int major,int minor);

Allocating and Freeing Device Numbers

one of the first things your driver will need to do when setting up a char device is to obtain one or more device numbers to work with.

declared in linux/fs.h:

//static alloc device number
int register_chrdev_region(dev_t first,unsigned int count,char *name);

//dynamic alloc
int alloc_chrdev_region(dev_t *dev,unsigned int firstminor,unsigned int count,char *name);

//free device number
void unregister_chrdev_region(dev_t first,unsigned int count);

Some Important Data Structures

File Operations

Each open file(represented internally by a file structrue)is associated with its own set of functions(by including a field called f_op that points to a file_operations structure)

We can consider the file to be an "object" and the functions operating on it to be its "method"(object-oriented programming)

The file Structure

The file structure represents an open file.

The inode Structure

The inode structure is used by the kernel internally to represent files.

There can be numerous file structures representing multiple open descriptors on a single file,but all point to a single inode structure.

Char Device Registration

Before the kernel invokes your device's operations,you must allocate and register one or more of type struct cdev(linux/cdev.h).

There are two ways of allocating and initializing one of these structures.

struct cdev *my_cdev = cdev_alloc();
my_cdev->ops = &my_fops;
//or
void cdev_init(struct cdev *cdev,struct file_operations *fops);
int cdev_add(struct cdev *dev,dev_t num,unsigned int count);
void cdev_del(struct cdev *dev);

The open Method

int (*open)(struct inode * inode,struct file *filp);

The release Method

int (*release)(struct inode * inode,struct file *filp);

read and write

prototype:

ssize_t read(struct file *filp,char __user *buf,size_t count,loff_t *offp);

ssize_t write(struct file *filp,const char __user *buf,size_t count,loff_t *offp);

copy a whole segment of data to or from the user address space.(linux/uaccess.h)

unsigned long copy_to_user(void __user *to,const void *from,unsigned long count);

unsigned long copy_from_user(void *to,const void __user * from,unsigend long count);

<<linux device driver,third edition>> Chapter 3:Char Drivers的更多相关文章

  1. <<linux device driver,third edition>> Chapter 4:Debugging Techniques

    Debugging by Printing printk lets you classify messages accoring to their severity by associating di ...

  2. <<linux device driver,third edition>> Chapter 2: Building and Running Modules

    Kernel Modules Versus Applications Kernel modules programming is similar to event driven programming ...

  3. linux device driver —— 环形缓冲区的实现

    还是没有接触到怎么控制硬件,但是在书里看到了一个挺巧妙的环形缓冲区实现. 此环形缓冲区实际为一个大小为bufsize的一维数组,有一个rp的读指针,一个wp的写指针. 在数据满时写进程会等待读进程读取 ...

  4. Linux Device Driver 学习(1)

    Linux Device Driver 学习(1) 一.搭建虚拟机开发环境 1.选择虚拟机VirtualBox,官网下载.deb包安装: VirtualBox Linux 5.1.6 下载fedora ...

  5. how to write your first linux device driver

    how to write your first linux device driver 0. environment-ubuntu 1804 64bit 1. apt-get install linu ...

  6. Linux Device Driver && Device File

    catalog . 设备驱动程序简介 . I/O体系结构 . 访问设备 . 与文件系统关联 . 字符设备操作 . 块设备操作 . 资源分配 . 总线系统 1. 设备驱动程序简介 设备驱动程序是内核的关 ...

  7. How to learn linux device driver

    To learn device driver development, like any other new knowledge, the bestapproach for me is to lear ...

  8. Linux Device Driver 3th 中的一些坑

    linux设备驱动第三版由于年代比较久远,有很多东西已过时.开一贴记录自己发现的一些问题. 4.3.1.4. seq_file接口 此节最后提到用 struct proc_dir_entry* cre ...

  9. linux device driver —— 字符设备

    现在对linux设备驱动还没有什么认识,跟着书上敲了一个字符驱动,这里把代码贴一下. 测试环境是 Ubuntu 16.04 64bit 驱动程序: #include <linux/fs.h> ...

随机推荐

  1. 【RabbitMQ】6、rabbitmq生产者的消息确认

    通过Publisher Confirms and Returns机制,生产者可以判断消息是否发送到了exchange及queue,而通过消费者确认机制,Rabbitmq可以决定是否重发消息给消费者,以 ...

  2. C++中析构函数的作用

    如果构造函数打开了一个文件,最后不需要使用时文件就要被关闭.析构函数允许类自动完成类似清理工作,不必调用其他成员函数. 析构函数也是特殊的类成员函数.简单来说,析构函数与构造函数的作用正好相反,它用来 ...

  3. python全局解释器锁(GIL)

    文章作者:卢钧轶(cenalulu) 本文原文地址:http://cenalulu.github.io/python/gil-in-python/ ,对文章做了适当的修改,加入了一些自己的理解. CP ...

  4. SQL Server 基本UPDATE和DELETE语句

    1.UPDATA 基本UPDATE语法:(可以修改多行的列) 2.DELETE

  5. jQuery动画切换引擎插件Velocity.js

    Velocity.js 官网 Velocity.js实现弹出式相框 慕课网 极棒的jquery动画切换引擎插件Velocity.js jQ库 (function($){ // 普通调用 /*$('#d ...

  6. canvas-star0.html

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

  7. [HTML/CSS]有一种节点叫做文本节点

    HTML可以看成是由节点(node)组成的树结构 我们一般都是在<p>节点里面写字符串. 在上图中,<p>节点和字符串之间有一个text, 这个text就是文本节点. 我们可以 ...

  8. 小程序和PHP学习笔记 ----- 不定期更新。

    学习tp5和小程序过程需要记住的重点记录 1,box-sizing: border-box; 规定两个并排的带边框的框 border-box 为元素设定的宽度和高度决定了元素的边框盒. 就是说,为元素 ...

  9. Django框架理解和使用常见问题

    1.什么是中间件? 中间件是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出. 中间件一般做认证或批量请求处理,django中的中间 ...

  10. HTML中令人惊喜的全局属性

    1.accesskey 属性 : 规定激活元素的快捷键. 浏览器支持:几乎所有浏览器均 accesskey 属性,除了 Opera. 定义和用法 accesskey 属性规定激活(使元素获得焦点)元素 ...