linux驱动摸索 --驱动框架初始化(结合韦东山视频教程)
一.驱动框架
初始化:insmod 加载
1.确定主设备号:
分为静态和动态分配,其中LED_GPIO_SIZE 表示支持的次设备号数目,一般默认为1. 相关实现代码如下:
- int result;
- dev_t dev;
- /*分配主设备号*/
- if (scull_major) /*静态分配一个主设备号*/
- {
- dev = MKDEV(scull_major,0);
- result = register_chrdev_region(dev,LED_GPIO_SIZE,DEVICE_NAME);
- }
- else /*动态分配一个主设备号*/
- {
- result = alloc_chrdev_region(&dev,0,LED_GPIO_SIZE,DEVICE_NAME);
- scull_major = MAJOR(dev);
- }
- if(result <0)
- {
- printk("LED:can not get major:%d\n",scull_major);
- return result;
- }
2.构造 file_operations 结构:结构成员对应相应的处理函数:
- static struct file_operations mini2440_leds_fops = {
- .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
- .open = mini2440_leds_open,
- .write = mini2440_leds_write,
- };
3.将相关操作告诉内核:
内核用cdev结构来表示字符设备,cev_init()将文件操作和cdev关联。cdev_add()将之前生成的主次设备号和cdev连接在一起,
- led_class = class_create(THIS_MODULE,DEVICE_NAME);
- cdev_init(&led_gpio_cdev, &mini2440_leds_fops);
- result = cdev_add(&led_gpio_cdev, dev, 1);
- if(result <0)
- {
- printk("LED:cdev_add error\n");
- return result;
- }
- device_create(led_class, NULL, MKDEV(scull_major, 0), NULL, "led0");
卸载驱动 rmmod 卸载 代码实现如下:
- dev_t dev_id = MKDEV(scull_major, 0);
- /*卸载主设备号*/
- unregister_chrdev_region(dev_id, LED_GPIO_SIZE);
- device_destroy(led_class,MKDEV(scull_major, 0));
- cdev_del(&led_gpio_cdev);
- class_destroy(led_class);
最后附上一个较为完整的驱动框架,其中创建了主设备号和次设备号,驱动代码如下:
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/init.h>
- #include <linux/delay.h>
- #include <asm/uaccess.h>
- #include <asm/irq.h>
- #include <mach/io.h>
- #include <mach/regs-gpio.h>
- #include <mach/hardware.h>
- #include <linux/device.h>
- #include <linux/cdev.h>
- #define DEVICE_NAME "led_1"
- #define LED_GPIO_SIZE 4
- static int scull_major = 0;
- static struct class *led_class;
- static struct cdev led_gpio_cdev[LED_GPIO_SIZE];
- static int mini2440_leds_open(struct inode *inode, struct file *file)
- {
- int minor = MINOR(inode->i_rdev); //MINOR(inode->i_cdev);
- printk("/dev/led%d has opened\n",minor);
- return 0;
- }
- static ssize_t mini2440_leds_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
- {
- char val;
- int minor = MINOR(filp->f_dentry->d_inode->i_rdev);
- copy_from_user(&val, buf, 1);
- printk("/dev/led%d write the val = %d\n",minor,val);
- return 0;
- }
- static struct file_operations mini2440_leds_fops = {
- .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
- .open = mini2440_leds_open,
- .write = mini2440_leds_write,
- };
- /*
- * 执行insmod命令时就会调用这个函数
- */
- static int mini2440_leds_init(void)
- {
- int result,i;
- dev_t dev;
- /*分配主设备号*/
- if (scull_major) /*静态分配一个主设备号*/
- {
- dev = MKDEV(scull_major,0);
- result = register_chrdev_region(dev,LED_GPIO_SIZE,DEVICE_NAME);
- }
- else /*动态分配一个主设备号*/
- {
- result = alloc_chrdev_region(&dev,0,LED_GPIO_SIZE,DEVICE_NAME);
- scull_major = MAJOR(dev);
- }
- if(result <0)
- {
- printk("LED:can not get major:%d\n",scull_major);
- return result;
- }
- led_class = class_create(THIS_MODULE,DEVICE_NAME);
- if (IS_ERR(led_class)) {
- return PTR_ERR(led_class);
- }
- for (i=0; i<LED_GPIO_SIZE;i++)
- {
- cdev_init(&led_gpio_cdev[i], &mini2440_leds_fops);
- result = cdev_add(&led_gpio_cdev[i], (dev+i), 1);
- if(result <0)
- {
- printk("LED:cdev_add error\n");
- return result;
- }
- device_create(led_class, NULL, MKDEV(scull_major, i), NULL, "led%d",i);
- }
- return 0;
- }
- /*
- * 执行rmmod命令时就会调用这个函数
- */
- static void mini2440_leds_exit(void)
- {
- int i;
- dev_t dev_id = MKDEV(scull_major, 0);
- /*卸载主设备号*/
- unregister_chrdev_region(dev_id, LED_GPIO_SIZE);
- for(i=0;i<LED_GPIO_SIZE;i++)
- {
- device_destroy(led_class,MKDEV(scull_major, i));
- cdev_del(&led_gpio_cdev[i]);
- }
- class_destroy(led_class);
- }
- /* 这两行指定驱动程序的初始化函数和卸载函数 */
- module_init(mini2440_leds_init);
- module_exit(mini2440_leds_exit);
- /* 描述驱动程序的一些信息,不是必须的 */
- MODULE_LICENSE("GPL");
linux 测试代码:
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdio.h>
- /*
- * ledtest <dev> <on|off>
- */
- void print_usage(char *file)
- {
- printf("Usage:\n");
- printf("%s <dev> <on|off>\n",file);
- printf("eg. \n");
- printf("%s /dev/led0 a\n", file);
- printf("%s /dev/led1 b\n", file);
- printf("%s /dev/led2 c\n", file);
- printf("%s /dev/led3 d\n", file);
- }
- int main(int argc, char **argv)
- {
- int fd;
- char* filename;
- char val;
- if (argc != 3)
- {
- print_usage(argv[0]);
- return 0;
- }
- filename = argv[1];
- fd = open(filename, O_RDWR);
- if (fd < 0)
- {
- printf("error, can't open %s\n", filename);
- return 0;
- }
- if (!strcmp("a", argv[2]))
- {
- val = 10;
- write(fd, &val, 1);
- }
- else if (!strcmp("b", argv[2]))
- {
- val = 11;
- write(fd, &val, 1);
- }
- else if (!strcmp("c", argv[2]))
- {
- val = 12;
- write(fd, &val, 1);
- }
- else if (!strcmp("d", argv[2]))
- {
- val = 13;
- write(fd, &val, 1);
- }
- return 0;
- }
linux驱动摸索 --驱动框架初始化(结合韦东山视频教程)的更多相关文章
- 【linux】驱动-5-驱动框架分层分离&实战
目录 前言 5. 分离分层 5.1 回顾-设备驱动实现 5.2 分离分层 5.3 设备 5.4 驱动 5.5 系统,模块 5.6 Makefile 参考: 前言 5. 分离分层 本章节记录实现LED驱 ...
- Linux下USB驱动框架分析【转】
转自:http://blog.csdn.net/brucexu1978/article/details/17583407 版权声明:本文为博主原创文章,未经博主允许不得转载. http://www.c ...
- linux块设备驱动---概念与框架(转)
基本概念 块设备(blockdevice) --- 是一种具有一定结构的随机存取设备,对这种设备的读写是按块进行的,他使用缓冲区来存放暂时的数据,待条件成熟后,从缓存一次性写入设备或者从设备一次性 ...
- Linux内核USB驱动【转】
本文转载自:http://www.360doc.com/content/12/0321/14/8363527_196286673.shtml 注意,该文件是2.4的内核的驱动源文件,并不保证在2.6内 ...
- 深入理解Linux字符设备驱动
文章从上层应用访问字符设备驱动开始,一步步地深入分析Linux字符设备的软件层次.组成框架和交互.如何编写驱动.设备文件的创建和mdev原理,对Linux字符设备驱动有全面的讲解.本文整合之前发表的& ...
- Linux块设备驱动(一) _驱动模型
块设备是Linux三大设备之一,其驱动模型主要针对磁盘,Flash等存储类设备,本文以3.14为蓝本,探讨内核中的块设备驱动模型 框架 下图是Linux中的块设备模型示意图,应用层程序有两种方式访问一 ...
- linux usb总线驱动(一)
目录 linux usb总线驱动框架 USB 介绍 传输类型 控制器接口 2440接口 基本流程 alloc_dev choose_address hub_port_init usb_get_devi ...
- Linux网络设备驱动 _驱动模型
Linux素来以其强大的网络功能著名,同时, 网络设备也作为三大设备之一, 成为Linux驱动学习中必不可少的设备类型, 此外, 由于历史原因, Linux并没有强制对网络设备贯彻其"一切皆 ...
- EDK II之USB总线驱动的实现框架
本文简单介绍一下UEFI中USB驱动的实现框架: 下图是USBD向上层驱动提供的接口: 1.从图中我们可以看出,USBDI的实现主要通过调用HCDI实现 和 访问USB_INTERFACE结构体(该结 ...
随机推荐
- LeetCode Regular Expression Matching 网上一个不错的实现(非递归)
'.' Matches any single character.'*' Matches zero or more of the preceding element. The matching sho ...
- 转:java读取配置文件的几种方法
转自: http://www.iteye.com/topic/56496 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置文件来完成,本文根据笔者工作中用到的读取配置文件的方法小小 ...
- mysql六:索引原理与慢查询优化
一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...
- Linux虚拟地址空间布局以及进程栈和线程栈总结【转】
转自:http://www.cnblogs.com/xzzzh/p/6596982.html 原文链接:http://blog.csdn.net/freeelinux/article/details/ ...
- [转载]基于Redis的Bloomfilter去重(附Python代码)
前言: “去重”是日常工作中会经常用到的一项技能,在爬虫领域更是常用,并且规模一般都比较大.去重需要考虑两个点:去重的数据量.去重速度.为了保持较快的去重速度,一般选择在内存中进行去重. 数据量不大时 ...
- Mac-装机
不过大家可别被「命令行」三个字吓到,其实你只需按步骤来,复制粘贴命令即可快速完成,事实上是很简单的. 一.准备工作: 准备一个 8GB 或以上容量的 U 盘,确保里面的数据已经妥善备份好(该过程会抹掉 ...
- SSM+Maven的JavaWeb项目中的异常的可能性
1.404 可能:1):被拦截了,即:springmvc中的controller可能不存在,可能没有被配置,可能配置出错 2):资源确实不存在 3):路径出错 2.500,程序异常,但是业务逻辑什么都 ...
- 【 Linux 】三大主流软件负载均衡器对比(LVS、Nginx、HAproxy)
三大主流软件负载均衡器对比(LVS.Nginx.HAproxy) (资料来自网络,做了部分的补充说明) LVS: 1. 抗负载能力强,性能高,能达到F5的60%,对内存和CPU资源消耗比较低 ...
- 【 Zabbix 】— 监控nginx
一.环境说明 OS:centos6.7 x64 nginx:nginx/1.9.9 ZABBIX:2.4.8 zabbix监控nginx是根据nginx的stub_status模块,抓取status模 ...
- Java常见知识点(二)
21.常量池专门用于管理在编译时被确定并被保存在已编译的.class文件中的一些数据.它包括了关于类.方法.接口中的常量,还包括字符串常量. 22.String已经重写了Object的equals ...