《Linux device driver》字符设备和scull
来源:互联网 发布:手机修改mac地址 编辑:程序博客网 时间:2024/05/19 04:08
《Linux device driver》-字符设备驱动程序 (并非全部书本知识)
主要通过介绍字符设备scull(Simple Character Utility for Loading Localities,区域装载的简单字符工具)的驱动程序编写,来学习Linux设备驱动的基本知识。
一、主设备号和次设备号
主设备号表示设备对应的驱动程序;次设备号由内核使用,用于正确确定设备文件所指的设备。
内核用dev_t类型(<linux/types.h>)来保存设备编号,dev_t是一个32位的数,12位表示主设备号,20为表示次设备号。
在实际使用中,是通过<linux/kdev_t.h>中定义的宏来转换格式。
建立一个字符设备之前,驱动程序首先要做的事情就是获得设备编号。其这主要函数在<linux/fs.h>中声明:
intregister_chrdev_region(dev_t first, unsigned int count, char *name); //指定设备编号
intalloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count,char *name); //动态生成设备编号
void unregister_chrdev_region(dev_tfirst, unsigned int count); //释放设备编号
分配之设备号的最佳方式是:默认采用动态分配,同时保留在加载甚至是编译时指定主设备号的余地。
以下是在scull.c中用来获取主设备好的代码:
if (scull_major) {
dev = MKDEV(scull_major, scull_minor);
result = register_chrdev_region(dev,scull_nr_devs, "scull")
;
} else {
result = alloc_chrdev_region(&dev,scull_minor, scull_nr_devs,"scull");
scull_major = MAJOR(dev); }
if (result < 0) {
printk(KERN_WARNING "scull: can't get major%d\n", scull_major);
return result; }
在这部分中,比较重要的是在用函数获取设备编号后,其中的参数name是和该编号范围关联的设备名称,它将出现在/proc/devices和sysfs中。
看到这里,就可以理解为什么mdev和udev可以动态、自动地生成当前系统需要的设备文件。udev就是通过读取sysfs下的信息来识别硬件设备的.
大部分基本的驱动程序操作涉及及到三个重要的内核数据结构,分别是file_operations、file和inode,它们的定义都在<linux/fs.h>。
二、字符设备的注册
内核内部使用struct cdev结构来表示字符设备。在内核调用设备的操作之前,必须分配并注册一个或多个struct cdev。代码应包含<linux/cdev.h>,它定义了struct cdev以及与其相关的一些辅助函数。
注册一个独立的cdev设备的基本过程如下:
1、为struct cdev分配空间(如果已经将struct cdev嵌入到自己的设备的特定结构体中,并分配了空间,这步略过!)
struct cdev *my_cdev = cdev_alloc();
2、初始化structcdev
void cdev_init(struct cdev *cdev, const struct file_operations*fops)
3、初始化cdev.owner
cdev.owner =THIS_MODULE;
4、cdev设置完成,通知内核structcdev的信息(在执行这步之前必须确定你对struct cdev的以上设置已经完成!)
int cdev_add(struct cdev *p, dev_t dev, unsigned count)
从系统中移除一个字符设备:voidcdev_del(struct cdev *p)
以下是scull中的初始化代码(之前已经为structscull_dev分配了空间):
/*
* Set up the char_devstructure for this device. */
static void scull_setup_cdev(struct scull_dev *dev, int index) {
int err, devno =MKDEV(scull_major, scull_minor + index);
cdev_init(&dev->cdev, &scull_fops); dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &scull_fops; //这句可以省略,在cdev_init中已经做过
err = cdev_add(&dev->cdev, devno, 1);
/* Fail gracefully ifneed be这步值得注意*/ if (err)
printk(KERN_NOTICE"Error %d adding scull%d", err, index); }
三、scull模型的内存使用示意图
- 《Linux device driver》字符设备和scull
- Linux Device Driver study: scull TIPS
- Linux Device Driver study: scull解析
- [linux device driver] Chapter 03:字符设备注册学习
- LINUX字符设备驱动程序实例(scull)
- LINUX字符设备驱动程序实例(scull)
- linux字符设备驱动程序scull实例
- LINUX字符设备驱动程序实例(chapter3 scull)
- LINUX字符设备驱动程序实例(scull)
- LINUX字符设备驱动程序实例(scull)
- LINUX字符设备驱动程序实例(scull)
- LINUX DEVICE DRIVER(2ND)第3章 字符设备驱动程序(2,主设备号和次设备号)
- scull字符设备
- Linux设备模型(5)_device和device driver
- Linux设备模型(5)_device和device driver
- Linux设备模型(5)_device和device driver
- Linux设备模型(5)_device和device driver
- Linux设备模型(5)_device和device driver
- MediaScanner与音乐信息扫描==
- 简单工厂模式
- python升级
- 【PHP入门篇】3.常量--慕课网【学习总结】
- cocos2d-x学习笔记【二】导演、场景、层。精灵介绍
- 《Linux device driver》字符设备和scull
- linux 汇编学习
- BufferedReader和BufferedWriter读写文件
- 12.使用命令对用户进行管理
- 【ITOO 2】.NET 动态建库建表:使用SQL字符串拼接方式
- 块元素、内联元素、inline-block的特点
- Android 网络通信框架Volley完全解析(一)
- VS中调试DLL工程的正确方法
- Android的目录结构说明