随笔分类 -  linux驱动

摘要:毛主席说过,没有调查,就没有发言权,所以一定要深入代码内部,探个究竟。1什么是并发并发是指多个进程”同时“对共享资源的访问,这样做势必会带来一些问题,我们称这种情况为竞态。2并发处理方法来看看Linux中两种常见的并发处理方法(1)自旋锁自旋锁定义include/linux/spinlock_types.h20 typedef struct {21 raw_spinlock_t raw_lock;22 #ifdef CONFIG_GENERIC_LOCKBREAK23 unsigned int break_lock;24 #endif25 #ifdef CONFIG_DEBUG_SPINLOC 阅读全文
posted @ 2012-01-31 15:09 移动应用开发 阅读(237) 评论(0) 推荐(0)
摘要:接下来以一个实际的例子来看I2C设备驱动,就以drivers/i2c/i2c-dev.c为例。先看它的初始化和注销函数static int __init i2c_dev_init(void) { int res; printk(KERN_INFO "i2c /dev entries driver\n"); res = register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops); if (res) goto out; i2c_dev_class = class_create(THIS_MODULE, " 阅读全文
posted @ 2012-01-19 16:03 移动应用开发 阅读(387) 评论(0) 推荐(0)
摘要:在drivers/i2c/busses下包含各种I2C总线驱动,如S3C2440的I2C总线驱动i2c-s3c2410.c,使用GPIO模拟I2C总线的驱动i2c-gpio.c,这里只分析i2c-gpio.c。i2c-gpio.c它是gpio模拟I2C总线的驱动,总线也是个设备,在这里将总线当作平台设备处理,那驱动当然是平台设备驱动,看它的驱动注册和注销函数。static int __init i2c_gpio_init(void) { int ret; ret = platform_driver_register(&i2c_gpio_driver); if (ret) print.. 阅读全文
posted @ 2012-01-19 15:56 移动应用开发 阅读(760) 评论(0) 推荐(0)
摘要:平台总线是内核实现的一条虚拟总线,Linux设备模型包含三个重要的元素,总线、设备和驱动,那看看平台总线又是怎样去实现的。首先看平台总线的定义: 946 struct bus_type platform_bus_type = { 947 .name = "platform", 948 .dev_attrs = platform_dev_attrs, 949 .match = platform_match, 950 .uevent = platform_... 阅读全文
posted @ 2012-01-19 15:23 移动应用开发 阅读(288) 评论(0) 推荐(0)
摘要:3.驱动122struct device_driver {123 const char *name;124 struct bus_type *bus;125126 struct module *owner;127 const char *mod_name; /* used for built-inmodules */128129 bool suppress_bind_attrs; /* disables bind/unbind viasysfs */130131 int (*probe) (struct device *dev);132 int (*remove) (struct device 阅读全文
posted @ 2011-12-22 16:36 移动应用开发 阅读(261) 评论(0) 推荐(0)
摘要:2.设备379struct device {380 struct device *parent;381382 struct device_private *p;383384 struct kobject kobj;385 const char *init_name; /* initial name of thedevice */386 struct device_type *type;387388 struct semaphore sem; /* semaphore to synchronizecalls to389 * its driver.390 */391392 struct bus_t 阅读全文
posted @ 2011-12-22 16:27 移动应用开发 阅读(551) 评论(0) 推荐(0)
摘要:Linux设备模型总线、设备和驱动1.总线总线使用结构structbus_type描述,这个结构类型定义在include/linux/device.h中51struct bus_type {52 const char *name;53 struct bus_attribute *bus_attrs;54 struct device_attribute *dev_attrs;55 struct driver_attribute *drv_attrs;5657 int (*match)(struct device *dev, struct device_driver *drv);58 int (* 阅读全文
posted @ 2011-12-22 15:57 移动应用开发 阅读(307) 评论(0) 推荐(0)
摘要:Linux内核链表定义在include/linux/list.h文件中链表结构定义struct list_head { struct list_head *next, *prev;};Linux内核链表它是一个双向循环链表,里面只包含两个struct list_head结构指针。1. 链表初始化链表初始化有两种方式:(1)使用LIST_HEAD宏定义个头节点并初始化#define LIST_HEAD(name) \ struct list_head name = LIST_HEAD_INIT(name)#define LIST_HEAD_INIT(name) { &(name), &a 阅读全文
posted @ 2011-12-14 23:10 移动应用开发 阅读(260) 评论(1) 推荐(0)
摘要:包含头文件#include<linux/miscdevice.h>建一个miscdevice结构体static struct miscdevice misc = { .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &dev_fops,};在驱动函数初始化中注册这个混杂设备misc_register(&misc);驱动卸载时,注销这个设备misc_deregister(&misc);混杂设备miscdevice说明miscdevice结构体struct miscdevice { int min 阅读全文
posted @ 2011-12-09 23:59 移动应用开发 阅读(279) 评论(0) 推荐(0)
摘要:以前rmmod不能卸载驱动,提示rmmod: chdir(/lib/modules): No such file or directory我在lib目录下新建目录modules,再次卸载提示rmmod: chdir(2.6.32.2): No such file or directory我在modules目录下再新建目录2.6.32.2如果配置General setup --->() Local version - append to kernel release这里什么也没有的话,那么新建目录就是Linux的版本号如果有字符的话,就是Linux版本号加上你输入的字符记得改变后要重新编译 阅读全文
posted @ 2011-12-09 23:54 移动应用开发 阅读(469) 评论(0) 推荐(0)
摘要:驱动程序编写好后,还需要创建设备节点,有两种方式,一是通过mknod命令去手动创建,例如:mknod /dev/hello c 250 0,/dev/hello为设备节点名字,c代表字符设备,250和0代表它的主次设备号。二是使用udev或mdev来实现自动创建设备节点。使用mknod手动创建设备节点不够灵活,如果是动态分配的设备号怎么办,难道每次加载驱动后去查看/proc/devices文件中查看它的主设备号,要是产品发布时怎么办,显然不太现实利用udev(mdev)来实现设备节点的自动创建,udev(mdev)存在于应用层。包含头文件#include<linux/device.h&g 阅读全文
posted @ 2011-12-09 23:52 移动应用开发 阅读(442) 评论(0) 推荐(0)
摘要:/*Linux字符设备驱动源代码scdd.c*/#include <linux/init.h> /*模块头文件*/#include <linux/module.h>#include <linux/types.h> /*dev_t头文件*/#include <linux/kdev_t.h> /*MAJOR和MINOR宏头文件*/#include <linux/fs.h> /*register_chrdev_region等函数 file_operations结构体*/#include <linux/cdev.h> /*stru 阅读全文
posted @ 2011-12-09 23:19 移动应用开发 阅读(179) 评论(0) 推荐(0)
摘要:/*Linux驱动源码hellop.c*/#include<linux/init.h>#include<linux/module.h>#include<linux/moduleparam.h>static char *whom="bufan";static int howmany=1;static int paramA[] = {1,2,3,4,5,6,7,8};static int paramA_no = 1;module_param(howmany,int,S_IRUGO);module_param(whom,charp,S_IRUG 阅读全文
posted @ 2011-12-09 23:00 移动应用开发 阅读(254) 评论(0) 推荐(0)
摘要:/*Linux驱动源代码hello.c*/#include<linux/init.h>#include<linux/module.h>static int __inithello_init(void){ /*模块初始化函数*/ printk(KERN_ALERT"Hello,world\n"); return 0;}static void __exithello_exit(void){ /*模块卸载函数*/ printk(KERN_ALERT "Goodbye,cruelworld\n");}module_init(hello_i 阅读全文
posted @ 2011-12-09 22:56 移动应用开发 阅读(210) 评论(0) 推荐(0)
摘要:linux/slab.h使用kmalloc分配一段连续的内存空间,同malloc相似函数原型void *kmalloc(size_t size,int flags)第一个参数size表示分配内存的大小,flags标志通常取GFP_KERNEL,注意分配的是虚拟地址空间使用kfree释放申请的内存空间函数原型void kfree(const void *) 阅读全文
posted @ 2011-12-08 22:27 移动应用开发 阅读(170) 评论(0) 推荐(0)