<<linux device driver,third edition>> Chapter 3:Char Drivers
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的更多相关文章
- <<linux device driver,third edition>> Chapter 4:Debugging Techniques
Debugging by Printing printk lets you classify messages accoring to their severity by associating di ...
- <<linux device driver,third edition>> Chapter 2: Building and Running Modules
Kernel Modules Versus Applications Kernel modules programming is similar to event driven programming ...
- linux device driver —— 环形缓冲区的实现
还是没有接触到怎么控制硬件,但是在书里看到了一个挺巧妙的环形缓冲区实现. 此环形缓冲区实际为一个大小为bufsize的一维数组,有一个rp的读指针,一个wp的写指针. 在数据满时写进程会等待读进程读取 ...
- Linux Device Driver 学习(1)
Linux Device Driver 学习(1) 一.搭建虚拟机开发环境 1.选择虚拟机VirtualBox,官网下载.deb包安装: VirtualBox Linux 5.1.6 下载fedora ...
- 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 ...
- Linux Device Driver && Device File
catalog . 设备驱动程序简介 . I/O体系结构 . 访问设备 . 与文件系统关联 . 字符设备操作 . 块设备操作 . 资源分配 . 总线系统 1. 设备驱动程序简介 设备驱动程序是内核的关 ...
- How to learn linux device driver
To learn device driver development, like any other new knowledge, the bestapproach for me is to lear ...
- Linux Device Driver 3th 中的一些坑
linux设备驱动第三版由于年代比较久远,有很多东西已过时.开一贴记录自己发现的一些问题. 4.3.1.4. seq_file接口 此节最后提到用 struct proc_dir_entry* cre ...
- linux device driver —— 字符设备
现在对linux设备驱动还没有什么认识,跟着书上敲了一个字符驱动,这里把代码贴一下. 测试环境是 Ubuntu 16.04 64bit 驱动程序: #include <linux/fs.h> ...
随机推荐
- Java并发编程:synchronized、Lock、ReentrantLock以及ReadWriteLock的那些事儿
目录 前言 synchronized用法 修饰方法 修饰实例方法 修饰静态方法 同步代码块 引出Lock Lock用法 子类:ReentrantLock 读写分离锁:ReadWriteLock Loc ...
- Java框架之Struts2(二)
一.Action 配置说明 //请求的直接转发 <package name="packageUser" namespace="" extends=&quo ...
- JSP内置对象——pageContext对象和config对象
它对应的常用方法有: 现在,我新建一个“pageContext.jsp”页面,可以获得“session_page1.jsp”这个页面中保存的用户名: pageContext.jap: session_ ...
- spring资源访问接口和资源加载接口
spring 资源访问接口 JDK提供的资源访问类,如java.net.URL.File等,不能很好地满足各种资源的访问需求,比如缺少从类路径或者Web容器的上下文中获取资源的操作类. 鉴于此,spr ...
- dmesg七种用法
dmesg 命令的使用范例 ‘dmesg’命令设备故障的诊断是非常重要的.在‘dmesg’命令的帮助下进行硬件的连接或断开连接操作时,我们可以看到硬件的检测或者断开连接的信息.‘dmesg’命令在多数 ...
- 自己写bitmap
备注:1.原创文章,转载请标明出处: 2.欢迎建议和意见 3.我的实现是C语言,为了保护公司隐私,下述数据类型被我改了.实际上int应改是无符号4个字节的类型,byte是有符号1个字节,才能保证移植性 ...
- [201804012]关于hugepages 3.txt
[201804012]关于hugepages 3.txt --//有一段时间我一直强调安装oracle一定要配置hugepage,因为现在的服务器内存越来越大,如果还使用4K的页面表,如果内存表占用内 ...
- 洗礼灵魂,修炼python(15)--列表进阶话题—>列表解析/列表生成器
是的,我是想到什么知识点就说什么,没有固定的主题,我的标题都是在写完博客再给的.本篇博文说说列表进阶话题.其实列表应该是比较熟悉的了,而毫不夸张的说,在实际的开发中,列表也是使用的最多的,以后你会体会 ...
- REPLACE函数的使用方法
Replace函数的含义~ 用新字符串替换旧字符串,而且替换的位置和数量都是指定的. replace函数的语法格式 =Replace(old_text,start_num,num_chars,new_ ...
- 位运算符&与、或|、异或^
&按照二进制位进行运算 如:运算规则:0&0=0: 0&1=0:1&0=0:1&1=1:即:两位同时为“1”,结果才为“1”,否则为0[有0则0] 3& ...