2013年9月14日

(转载)Linux中__init、__devinit等初始化宏

摘要: 内核使用了大量不同的宏来标记具有不同作用的函数和数据结构。如宏__init 、__devinit 等。这些宏在include/linux/init.h 头文件中定义。编译器通过这些宏可以把代码优化放到合适的内存位置,以减少内存占用和提高内核效率。下面是一些常用的宏:? ?__init ,标记内核启动时使用的初始化代码,内核启动完成后不再需要。以此标记的代码位于.init.text 内存区域。它的宏定义是这样的:?#define _ _init _ _attribute_ _ ((_ _section_ _ (".text.init"))) ? ?__exit ,标记退出代码 阅读全文

posted @ 2013-09-14 09:29 熊猫酒仙是也 阅读(286) 评论(0) 推荐(0) 编辑

2013年9月13日

(转载)驱动之路-platform按键驱动

摘要: 一 、重要知识点: 1.platform设备模型 从Linux 2.6起引入了一套新的驱动管理和注册机制,platform_device和platform_driver,Linux中大部分的设备驱动都可以使用这套机制。platform是一条虚拟的总线。设备用platform_device表示,驱动用platform_driver进行注册,Linux platform driver机制和传统的device driver机制(通过driver_register进行注册)相比,一个明显的优势在于platform机制将设备本身的资源注册进内核,由内核统一管理,在驱动中使用这些资源时通过platform 阅读全文

posted @ 2013-09-13 16:19 熊猫酒仙是也 阅读(245) 评论(0) 推荐(0) 编辑

platform_driver_register()--如何match之后调用probe

摘要: int platform_driver_register(struct platform_driver *drv){ drv->driver.bus = &platform_bus_type;/*关联总线*/ /*关联driver的设备方法*/ if (drv->probe) drv->driver.probe = platform_drv_probe; if (drv->remove) drv->driver.remove = platform_drv_remove; if (drv->shutdown) drv->dr... 阅读全文

posted @ 2013-09-13 14:24 熊猫酒仙是也 阅读(4339) 评论(0) 推荐(0) 编辑

2013年9月12日

《C和指针》读书笔记——第三章 数据

摘要: 1.typedef:为各种数据类型定义新名字 typedef char *ptr_to_char; ptr_to_char a;//声明a是一个指向字符的指针。2.链接属性:extern;static3.变量存储位置:普通内存、运行时的堆栈、硬件寄存器。 (1)在代码块内部声明的变量的缺省存储类型为自动(auto),存储于堆栈中,当程序执行到声明处才创建,离开代码块时自动销毁; 在代码块之外的都存储于静态内存中。 (2)加上static则变为静态变量,存储于静态内存中,程序执行期间始终存在。 (3)关键字register,声明为寄存器变量。4.static关键字 用于函数定义或者代码... 阅读全文

posted @ 2013-09-12 20:45 熊猫酒仙是也 阅读(207) 评论(0) 推荐(0) 编辑

2013年9月11日

《C和指针》读书笔记——第二章 基本概念

摘要: 1.编译过程: source code→Compiler→Object code→Linker←Lib ↓Exe2.经过初始化的静态变量(static)在程序执行前能获得他们的值。3.绝大多数环境使用堆栈来存储局部变量和其他数据。 阅读全文

posted @ 2013-09-11 22:21 熊猫酒仙是也 阅读(145) 评论(0) 推荐(0) 编辑

《C和指针》读书笔记——第一章 快速上手

摘要: 1.注释代码可以用:#if 0 statements#endif2.参数被声明为const,表明函数将不会修改函数调用者的所传递的这个参数。3.scanf("%d",&columns[num]);//返回存储于参数中的值的个数。4.函数名: strncpy功 能: 串拷贝用 法: char *strncpy(char *destin, char *source, int maxlen);程序例:#include #include int main(void){ char string[10]; char *str1 = "abcdefghi"; 阅读全文

posted @ 2013-09-11 17:33 熊猫酒仙是也 阅读(194) 评论(0) 推荐(0) 编辑

2013年9月10日

(转载)2.6.32关于bus_id的问题

摘要: 实验环境:linux2.6.32.2在做bus驱动实验的时候,出现了一个问题:提示bus_id找不到。于是到内核源代码找了一番,果然没有看见。直接到device结构体中看,找到的最像的也就constchar*init_name;/*initialnameofthedevice*/想到这个也可以作为标识,并且BUS_ID_SIZE也找不到,于是将strncpy(my_dev.init_name,"my_dev",BUS_ID_SIZE);注释掉,直接在mydev中添加.init_name="my_dev"。但是在注册驱动时又出现了段错误,根据console 阅读全文

posted @ 2013-09-10 12:50 熊猫酒仙是也 阅读(231) 评论(0) 推荐(0) 编辑

2013年9月5日

使用内核定时器的second字符设备驱动及测试代码

摘要: 驱动:#include #include #include #include #include #include #include //cdev#include //udev#include //schedule#include //copy_to_user#include //kmalloc()#define DEVICE_NAME "second_drv"static int second_major=0;/*主设备号*/struct class *second_class;/*second设备结构体*/static struct second_dev{ struct 阅读全文

posted @ 2013-09-05 13:43 熊猫酒仙是也 阅读(324) 评论(0) 推荐(0) 编辑

2013年9月4日

(转载)linux 驱动头文件说明

摘要: #include 是在linux-2.6.29/include/linux下面寻找源文件。#include 是在linux-2.6.29/arch/arm/include/asm下面寻找源文件。#include 是在linux-2.6.29/arch/arm/mach-s3c2410/include/mach下面寻找源文件。#include在linux-2.6.31_TX2440A20100510\linux-2.6.31_TX2440A\arch\arm\plat-s3c\include\plat#include//最基本的文件,支持动态添加和卸载模块。Hello World驱动要这一... 阅读全文

posted @ 2013-09-04 13:16 熊猫酒仙是也 阅读(130) 评论(0) 推荐(0) 编辑

2013年9月3日

用内核定时器来实现的按键驱动代码分析以及测试代码

摘要: 驱动代码:#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //udevstruct class *key_class;static int key_major = 0;struct key_desc_t { int p... 阅读全文

posted @ 2013-09-03 21:55 熊猫酒仙是也 阅读(394) 评论(0) 推荐(0) 编辑

2013年9月2日

基于等待队列及poll机制的按键驱动代码分析和测试代码

摘要: 按键驱动分析:#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include #include struct class *key_class;//udev机制自动创建文件结点#define USING_TASKLET//struct timespec start_time;//struct tim 阅读全文

posted @ 2013-09-02 17:03 熊猫酒仙是也 阅读(520) 评论(0) 推荐(0) 编辑

2013年9月1日

(转载)Linux poll机制分析

摘要: 最近看了一下Linux Poll 机制的实现,看了韦老师的分析文档,总结如下:int poll(struct pollfd *fds,nfds_t nfds, int timeout);总的来说,Poll机制会判断fds中的文件是否可读,如果可读则会立即返回,返回的值就是可读fd的数量,如果不可读,那么就进程就会休眠timeout这么长的时间,然后再来判断是否有文件可读,如果有,返回fd的数量,如果没有,则返回0.在内核中大致上实现过程:当应用程序调用poll函数的时候,会调用到系统调用sys_poll函数,该函数最终调用do_poll函数,do_poll函数中有一个死循 环,在里面又会利用d 阅读全文

posted @ 2013-09-01 22:55 熊猫酒仙是也 阅读(375) 评论(0) 推荐(0) 编辑

2013年8月29日

支持阻塞操作和轮询操作的globalfifo设备驱动代码分析以及测试代码

摘要: #include #include #include #include #include #include #include #include #include #include #include #include #include //-------------class_create,device_create------#include /*用udev机制自动添加设备节点*/struct class *globalfifo_class;#define GLOBALFIFO_SIZE 0x1000 /*全局内存最大4K字节*/#define MEM_CLEAR 0x1 /*清... 阅读全文

posted @ 2013-08-29 20:43 熊猫酒仙是也 阅读(563) 评论(0) 推荐(0) 编辑

2013年8月28日

(转载)关于schedule_timeout

摘要: schedule_timeout这个函数除了对当前进程调用schedule之外,还有一个功能,如同其名字中暗示的,在指定的时间到期后(timeout了)将进程唤醒。我们知道,进程一旦进入睡眠状态,就会从cpu的run queue中移走,直觉是系统将不会维护散落到系统各处(等待队列等)的这些睡眠进程的时间信息,那么如何在指定的时间到期时唤醒这些进程呢?Linux内核使用了timer机制来完成,timer不依赖于进程,依赖于处理器的中断,当然关于timer的内部实现的机制可以成为另一个帖子了,这里就不多说。看看schedule_timeout的源码:signed long __sched sche 阅读全文

posted @ 2013-08-28 23:25 熊猫酒仙是也 阅读(1228) 评论(0) 推荐(0) 编辑

(转载)signal_pending与返回-ERESTARTSYS

摘要: 经常我们在睡眠的代码中 会看到这样的例子:if (signal_pending(current)) { ret = -ERESTARTSYS;return ret;} 关于 -ERESTARTSYS 到底是什么意思? -ERESTARTSYS表示信号函数处理完毕后重新执行信号函数前的某个系统调用.也就是说,如果信号函数前有发生系统调用,在调度用户信号函数之前,内核会检查系统调用的返回值,看看是不是因为这个信号而中断了系统调用.如果返回值 -ERESTARTSYS,并且当前调度的信号具备-ERESTARTSYS属性,系统就会在用户信号函数返回之后再执行该系统调用 这个过程,不必深究,你就知道.. 阅读全文

posted @ 2013-08-28 23:03 熊猫酒仙是也 阅读(1287) 评论(0) 推荐(0) 编辑

2013年8月27日

虚拟内存设备驱动memdev及实例代码

摘要: 驱动:#include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef MEMDEV_MAJOR#define MEMDEV_MAJOR 0 /*预设的mem的主设备号*/#endif#ifndef MEMDEV_NR_DEVS#define MEMDEV_NR_DEVS 2 /*设备数*/#endif#ifndef MEMDEV_SIZE#define MEMDEV_SIZE 4096#endif#d... 阅读全文

posted @ 2013-08-27 09:42 熊猫酒仙是也 阅读(762) 评论(0) 推荐(0) 编辑

2013年8月26日

Linux下实现流水灯等功能的LED驱动代码及测试实例

摘要: 驱动代码:#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //-------------class_create,device_create------#include /*用udev机制自动添加设备节点*/struct class *led_class;static int led_major = 0; ... 阅读全文

posted @ 2013-08-26 15:25 熊猫酒仙是也 阅读(4517) 评论(0) 推荐(0) 编辑

(转载)ioctl与unlocked_ioctl区别

摘要: 今天调一个程序调了半天,发现应用程序的ioctl的cmd参数传送到驱动程序的ioctl发生改变。而根据《linux设备驱动》这个cmd应该是不变的。因为在kernel 2.6.36 中已经完全删除了struct file_operations 中的ioctl 函数指针,取而代之的是unlocked_ioctl ,所以我怀疑二者是不是兼容的。上网查了一些资料,很多文章只是泛泛谈了一下,说在应用程序中ioctl是兼容的,不必变化。而在驱动程序中这个指针函数变了之后最大的影响是参数中少了inode ,所以应用程序ioctl是兼容的,但驱动程序中我们的ioctl函数必须变化,否则就会发生cmd参数的变 阅读全文

posted @ 2013-08-26 14:10 熊猫酒仙是也 阅读(428) 评论(0) 推荐(0) 编辑

2013年8月25日

Linux下简易蜂鸣器驱动代码及测试实例

摘要: 驱动代码:#include #include #include #include #include #include #include #include #include #define DEVICE_NAME "beep3"#define BEEP_MAGIC 'k'#define BEEP_START_CMD _IO (BEEP_MAGIC, 1)#define BEEP_STOP_CMD _IO (BEEP_MAGIC, 2)static void beep3_stop(void) { printk("in the beep3_stop!!\ 阅读全文

posted @ 2013-08-25 20:43 熊猫酒仙是也 阅读(1598) 评论(0) 推荐(0) 编辑

Linux下18b20温度传感器驱动代码及测试实例

摘要: 驱动代码:#include #include #include #include #include #include #include #include #include #define DEVICE_NAME "TEM0"#define TEM_SHOW_CODE 0x01//static struct cdev cdev;struct class *tem_class;//static dev_t devno;//static int major = 243;//可以用int alloc_chrdev_region(dev_... 阅读全文

posted @ 2013-08-25 15:32 熊猫酒仙是也 阅读(1278) 评论(0) 推荐(0) 编辑

LED字符设备驱动实例及测试代码

摘要: 驱动代码如下:#include //内核头文件#include //__init等#include //模块加载的头文件#include //file_operations#include //错误状态常数#include //size_t,ssize_t等//--------------cdev----------------#include //-------------class_create,device_create------#include //--------------GPIO-----------------#include #include #include //---- 阅读全文

posted @ 2013-08-25 15:27 熊猫酒仙是也 阅读(938) 评论(0) 推荐(0) 编辑

2013年8月24日

Linux下GPIO驱动(五) ----misc_register();

摘要: //struct miscdevice { int minor; const char *name; const struct file_operations *fops; struct list_head list; struct device *parent; struct device *this_device; const char *nodename; mode_t mode;}; int misc_register(struct miscdevice * misc);//注册为杂项设备 int misc_deregister(str... 阅读全文

posted @ 2013-08-24 16:23 熊猫酒仙是也 阅读(2020) 评论(0) 推荐(0) 编辑

Linux下GPIO驱动(四) ----gpio_request();gpio_free();

摘要: //gpio_request申请gpio口int gpio_request(unsigned gpio, const char *label){ struct gpio_desc *desc; struct gpio_chip *chip; int status = -EINVAL; unsigned long flags; spin_lock_irqsave(&gpio_lock, flags); // gpio_lock是自旋锁,上锁,保存FLAG在flags变量 if (!gpio_is_valid(gp... 阅读全文

posted @ 2013-08-24 15:57 熊猫酒仙是也 阅读(16187) 评论(0) 推荐(0) 编辑

Linux下GPIO驱动(三) ----gpio_desc()的分析

摘要: 上篇最后提出的疑问是结构体gpio_chip中的成员函数set等是怎么实现的,在回答之前先介绍下gpio_desc这个结构体。 如上图所示,右上方部分为GPIO驱动对其它驱动提供的GPIO操作接口,其对应的右下方部分为GPIO硬件操作接口,也就是说对外提供的接口最终会一一对应的对硬件GPIO进行操作。再来看左边部分,左上方部分为一全局数组,记录各个GPIO的描述符,即对应左下方的gpio_desc结构体,其中gpio_chip指向硬件层的GPIO,flags为一标志位,用来指示当前GPIO是否已经占用,当用gpio_request申请GPIO资源时,flags位就会置位,当调用gpio_fre 阅读全文

posted @ 2013-08-24 15:46 熊猫酒仙是也 阅读(12335) 评论(0) 推荐(1) 编辑

Linux下GPIO驱动(二) ----s3c_gpio_cfgpin();gpio_set_value();

摘要: 首先来看s3c_gpio_cfgpin();int s3c_gpio_cfgpin(unsigned int pin, unsigned int config){ struct s3c_gpio_chip *chip = s3c_gpiolib_getchip(pin);//得到对应GPIO结构体首指针,里面包含了该GPIO的各种参数 unsigned long flags; int offset; int ret; if (!chip) return -EINVAL; offset = pin - chip->chip.base; s3... 阅读全文

posted @ 2013-08-24 14:55 熊猫酒仙是也 阅读(6859) 评论(0) 推荐(0) 编辑

Linux下GPIO驱动(一) ----一个简单的LED驱动

摘要: /******************************* * *杂项设备驱动:miscdevice *majior=10; * * *****************************/#include #include #include #include #include //#include //#include //kcalloc,kzalloc等内存分配函数//---------ioctl------------#include //---------misc_register----#include //----------cdev--------------#incl 阅读全文

posted @ 2013-08-24 13:52 熊猫酒仙是也 阅读(7431) 评论(0) 推荐(0) 编辑

导航