驱动基本知识
2017-10-18 13:44
316 查看
1. 用户空间与内核空间##
用户空间:应用程序运行的空间,有独立的地址映射,虚拟地址中大栈空间,发生段错误无害,内核空间:内核程序运行的空间,有独立的地址映射,虚拟地址中小栈空间,发生段错误可能到时系统无法正常运行,
用户空间和内核空间拥有独立的内存空间,用户空间数据与内核空间数据不能直接交互,不能直接相互引用,必须使用像copy_to_user 及 copy_from_user类似的函数,将数据从一个空间copy 到另一个空间。 对内核资源的直接操作可能导致系统崩溃。 内核与用户空间的操作级别不同,拥有的资源也不同。
2. 内核中并发
概念:同一时刻对同一资源的访问 原因:1、多进程并发; 2、SMP系统,多核同时调用同一个驱动;3、中断异步;4、定时异步 所以内核代码必须是可重入的(同时发生多次调用)
3、内核符号表
一个内核模块中的函数、变量可能被其他内核模块引用时,函数、变量需要使用模块导出宏来导出符号。 宏为 EXPORT_SYMBOL(name); EXPORT_SYMBOL_GPL(name); 只能被GPL许可证下的模块使用; 导出的符号会被内核包含在内核符号表中,此表包含了所有的全局内核项的地址,可被其他模块使用,进而实现模块层叠技术
4、在用户空间编写驱动程序
通过mmap 函数将linux 系统的物理地址映射到用户空间,用户可以直接操作寄存器。系统不能直接使用物理地址,需要通过转换。
fd = open("/dev/mem",O_RDWR); reg_addr = mmap(NULL,0x100,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0xE0080000);
可以直接使用 reg_addr地址。
5 hello 示例
/* * $Id: hellop.c,v 1.4 2004/09/26 07:02:43 gregkh Exp $ */ #include <linux/init.h> #include <linux/module.h> #include <linux/moduleparam.h> MODULE_LICENSE("Dual BSD/GPL"); /* * These lines, although not shown in the book, * are needed to make hello.c run properly even when * your kernel has version support enabled */ /* * A couple of parameters that can be passed in: how many times we say * hello, and to whom. */ static char *whom = "world"; static int howmany = 1; module_param(howmany, int, S_IRUGO); module_param(whom, charp, S_IRUGO); static int __init hello_init(void) { int i; for (i = 0; i < howmany; i++) printk(KERN_ALERT "(%d) Hello, %s\n", i, whom); return 0; } static void __exit hello_exit(void) { printk(KERN_ALERT "Goodbye, cruel world\n"); } module_init(hello_init); module_exit(hello_exit);
参数加载 sudo insmod hellop.ko howmany=2 whom=ldd
module_param 为模块参数加载, 只能一次改写一个
对多个参数一起加载 module_param_array
/* * Custom GPIO-based I2C driver * * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * --------------------------------------------------------------------------- * * The behaviour of this driver can be altered by setting some parameters * from the insmod command line. * * The following parameters are adjustable: * * bus0 These four arguments can be arrays of * bus1 1-8 unsigned integers as follows: * bus2 * bus3 <id>,<sda>,<scl>,<udelay>,<timeout>,<sda_od>,<scl_od>,<scl_oo> * * where: * * <id> ID to used as device_id for the corresponding bus (required) * <sda> GPIO pin ID to used for SDA (required) * <scl> GPIO pin ID to used for SCL (required) * <udelay> signal toggle delay. * <timeout> clock stretching timeout. * <sda_od> SDA is configured as open drain. * <scl_od> SCL is configured as open drain. * <scl_oo> SCL output drivers cannot be turned off. * * See include/i2c-gpio.h for more information about the parameters. * * If this driver is built into the kernel, you can use the following kernel * command line parameters, with the same values as the corresponding module * parameters listed above: * * i2c-gpio-custom.bus0 * i2c-gpio-custom.bus1 * i2c-gpio-custom.bus2 * i2c-gpio-custom.bus3 */ #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/platform_device.h> #include <linux/i2c-gpio.h> #define DRV_NAME "i2c-gpio-custom" #define DRV_DESC "Custom GPIO-based I2C driver" #define DRV_VERSION "0.1.1" #define PFX DRV_NAME ": " #define BUS_PARAM_ID 0 #define BUS_PARAM_SDA 1 #define BUS_PARAM_SCL 2 #define BUS_PARAM_UDELAY 3 #define BUS_PARAM_TIMEOUT 4 #define BUS_PARAM_SDA_OD 5 #define BUS_PARAM_SCL_OD 6 #define BUS_PARAM_SCL_OO 7 #define BUS_PARAM_REQUIRED 3 #define BUS_PARAM_COUNT 8 #define BUS_COUNT_MAX 4 static unsigned int bus0[BUS_PARAM_COUNT] __initdata; static unsigned int bus1[BUS_PARAM_COUNT] __initdata; static unsigned int bus2[BUS_PARAM_COUNT] __initdata; static unsigned int bus3[BUS_PARAM_COUNT] __initdata; static unsigned int bus_nump[BUS_COUNT_MAX] __initdata; #define BUS_PARM_DESC \ " config -> id,sda,scl[,udelay,timeout,sda_od,scl_od,scl_oo]" **module_param_array**(bus0, uint, &bus_nump[0], 0); MODULE_PARM_DESC(bus0, "bus0" BUS_PARM_DESC); module_param_array(bus1, uint, &bus_nump[1], 0); MODULE_PARM_DESC(bus1, "bus1" BUS_PARM_DESC); module_param_array(bus2, uint, &bus_nump[2], 0); MODULE_PARM_DESC(bus2, "bus2" BUS_PARM_DESC); module_param_array(bus3, uint, &bus_nump[3], 0); MODULE_PARM_DESC(bus3, "bus3" BUS_PARM_DESC); static struct platform_device *devices[BUS_COUNT_MAX]; static unsigned int nr_devices; static void i2c_gpio_custom_cleanup(void) { int i; for (i = 0; i < nr_devices; i++) if (devices[i]) platform_device_unregister(devices[i]); } static int __init i2c_gpio_custom_add_one(unsigned int id, unsigned int *params) { struct platform_device *pdev; struct i2c_gpio_platform_data pdata; int err; if (!bus_nump[id]) return 0; if (bus_nump[id] < BUS_PARAM_REQUIRED) { printk(KERN_ERR PFX "not enough parameters for bus%d\n", id); err = -EINVAL; goto err; } pdev = platform_device_alloc("i2c-gpio", params[BUS_PARAM_ID]); if (!pdev) { err = -ENOMEM; goto err; } pdata.sda_pin = params[BUS_PARAM_SDA]; pdata.scl_pin = params[BUS_PARAM_SCL]; pdata.udelay = params[BUS_PARAM_UDELAY]; pdata.timeout = params[BUS_PARAM_TIMEOUT]; pdata.sda_is_open_drain = params[BUS_PARAM_SDA_OD] != 0; pdata.scl_is_open_drain = params[BUS_PARAM_SCL_OD] != 0; pdata.scl_is_output_only = params[BUS_PARAM_SCL_OO] != 0; err = platform_device_add_data(pdev, &pdata, sizeof(pdata)); if (err) goto err_put; err = platform_device_add(pdev); if (err) goto err_put; devices[nr_devices++] = pdev; return 0; err_put: platform_device_put(pdev); err: return err; } static int __init i2c_gpio_custom_probe(void) { int err; printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n"); err = i2c_gpio_custom_add_one(0, bus0); if (err) goto err; err = i2c_gpio_custom_add_one(1, bus1); if (err) goto err; err = i2c_gpio_custom_add_one(2, bus2); if (err) goto err; err = i2c_gpio_custom_add_one(3, bus3); if (err) goto err; if (!nr_devices) { printk(KERN_ERR PFX "no bus parameter(s) specified\n"); err = -ENODEV; goto err; } return 0; err: i2c_gpio_custom_cleanup(); return err; } #ifdef MODULE static int __init i2c_gpio_custom_init(void) { return i2c_gpio_custom_probe(); } module_init(i2c_gpio_custom_init); static void __exit i2c_gpio_custom_exit(void) { i2c_gpio_custom_cleanup(); } module_exit(i2c_gpio_custom_exit); #else subsys_initcall(i2c_gpio_custom_probe); #endif /* MODULE*/ MODULE_LICENSE("GPL v2"); MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org >"); MODULE_DESCRIPTION(DRV_DESC); MODULE_VERSION(DRV_VERSION);
参数加载方式:sudo insmod i2c-gpio-custom.ko bus0=0,1,2
相关文章推荐
- 1、驱动的基本知识
- DDD领域驱动设计基本理论知识总结
- 嵌入式Linux驱动笔记-基本知识二
- DDD领域驱动设计基本理论知识总结
- DDD领域驱动设计基本理论知识总结(转)
- Linux驱动开发入门——基本知识简介
- DDD领域驱动设计基本理论知识总结
- 嵌入式Linux驱动笔记-基本知识一
- DDD领域驱动设计基本理论知识总结
- wince驱动学习之基本知识
- usb驱动的基本知识
- DDD领域驱动设计基本理论知识总结
- [转]领域驱动设计基本理论知识总结
- DDD领域驱动设计基本理论知识总结
- 领域驱动设计基本理论知识总结
- 领域驱动设计基本理论知识总结
- 领域驱动设计基本理论知识总结
- linux驱动之:模块基本知识
- DDD领域驱动设计基本理论知识总结
- [LCD,hdimi,tvout]显示驱动的基本知识,VSYNC,HSYNC,PCLK