Linux设备驱动工程师之路——platform按键驱动 来自于衡阳师范学院08电2

 

一 、重要知识点:

1.platform设备模型

    从Linux 2.6起引入了一套新的驱动管理和注册机制,platform_device和platform_driver,Linux中大部分的设备驱动都可以使用这套机制。platform是一条虚拟的总线。设备用platform_device表示,驱动用platform_driver进行注册,Linux platform driver机制和传统的device driver机制(通过driver_register进行注册)相比,一个明显的优势在于platform机制将设备本身的资源注册进内核,由内核统一管理,在驱动中使用这些资源时通过platform device提供的标准结构进行申请并使用。这样提高了驱动和资源的独立性,并且具有较好的可移植性和安全性(这些标准接口是安全的)。

    pltform机制本身使用并不复杂,由两部分组成:platform_device和platform_driver。通过platform机制开发底层驱动的大致流程为:定义platform_deive->注册platform_device->定义platform_driver->注册platform_driver。

    首先要确认的就是设备的资源信息,例如设备的地址,中断号等。

1)platform_device

在 2.6 内核中 platform 设备用结构体 platform_device 来描述,该结构体定义在 kernel/include/linux/platform_device.h 中,

structplatform_device {

 const char * name;

 u32  id;

 struct device dev;

 u32  num_resources;

 struct resource * resource;

};

该结构一个重要的元素是resource ,该元素存入了最为重要的设备资源信息,定义在kernel/include/linux/ioport.h 中,

structresource {

 const char *name;//资源的名称

 unsigned long start, end;//资源起始的和结束的物理地址

 unsigned long flags;//资源的类型,比如MEM,IO,IRQ类型

 struct resource *parent, *sibling, *child;//资源链表的指针

};

structplatform_device的分配使用

structplatform_device *platform_device_alloc(const char *name, int id)

name是设备名,id,设备id,一般为-1,如果是-1,表示同样名字的设备只有一个

举个简单的例子,name/id是“serial/1”则它的bus_id就是serial.1  如果name/id是“serial/0”则它的bus_id就是serial.0 ,如果它的name/id是“serial/-1”则它的bus_id就是serial。

注册平台设备,使用函数

intplatform_device_add(struct platform_device *pdev)

注销使用

voidplatform_device_unregister(struct platform_device *pdev)

2)platform_driver

在平台设备驱动中获取平台设备资源使用

structresource *platform_get_resource(struct platform_device *dev, unsigned int type,unsigned int num)

该函数用于获取dev设备的第num个类型为type的资源,如果获取失败,则返回NULL。例如 platform_get_resource(pdev,IORESOURCE_IRQ, 0)。

平台驱动描述使用

structplatform_driver {

 int (*probe)(struct platform_device *);

 int (*remove)(struct platform_device *);

 void (*shutdown)(struct platform_device *);

 int (*suspend)(struct platform_device *, pm_message_t state);

 int (*suspend_late)(struct platform_device *, pm_message_t state);

 int (*resume_early)(struct platform_device *);

 int (*resume)(struct platform_device *);

 struct device_driver driver;

};

Probe()函数必须验证指定设备的硬件是否真的存在,probe()可以使用设备的资源,包括时钟,platform_data等,Platform driver可以通过下面的函数完成对驱动的注册:

int platform_driver_register(structplatform_driver *drv);一般来说设备是不能被热插拔的,所以可以将probe()函数放在init段里面来节省driver运行时候的内存开销:

int platform_driver_probe(struct platform_driver *drv, int (*probe)(structplatform_device *));

注销使用void platform_driver_unregister(struct platform_driver *drv)

2.中断处理

在Linux驱动程序中,为设备实现一个中断包含 两个步骤1.向内核注册(申请中断)中断 2.实现中断处理函数

request_irq用于实现中断的注册

intrequest_irq(unsigned in irq, void(*handler)(int, void *, struct pt_regs *), unsigned long flags, const char *devname, void*dev_id)

向内核申请中断号为irq,中断处理函数为handler指针指向的函数,中断标志为flag,设备名为devname的中断。成功返回0,或者返回一个错误码。

当request_irq不用于共享中断时,dev_id可以为NULL,或者指向驱动程序自己的私有数据。但用于共享中断时dev_id必须唯一。因为free_irq时也需要dev_id做参数,这样free_irq才知道要卸载共享中断上哪个中断服务处理函数。共享中断会在后面讲到。

在flag参数中,可以选以下参数

IRQF_DISABLED(SA_INTERRUPT)

如果设置该位,表示是一个“快速”中断处理程序,如果没有,那么就是一个“慢速”中断处理程序。

IRQF_SHARED(SA_SHITQ)

该位表示中断可以在设备间共享。

快速/慢速中断

这两种类型的中断处理程序的主要区别在于:快速中断保证中断处理的原子性(不被打断),而慢速中断则不保证。换句话说,也就是开启中断标志位在运行快速中断处理程序时

关闭的,因此在服务该中断时,不会被其他类型的中断打断;而调用慢速中断处理时,其他类型中断扔可以得到服务。

    共享中断

共享中断就是将不同的设备挂到同一个中断信号线上。linux对共享的支持主要是位PCI设备服务。

释放中断

voidfree_irq(unsigned int irq)

当设备不再需要使用中断时(通常是设备关闭和驱动卸载时),应该使用该函数把他们返回给内核使用。

禁用中断

voiddisable_irq(int irq)

当一些代码中不能使用中断时(如支持自旋锁的上下文中)使用该函数禁用中断。

启用中断

voidenable_irq(int irq)

当禁止后可以使用该函数重新启用。

 

二、驱动代码

  该驱动实现能够读取按键按下的键值,比如说如果是第一个键按下读取的键值就为1。

  platform平台设备

  1. #include <linux/device.h>  
  2. #include <linux/string.h>  
  3. #include <linux/platform_device.h>  
  4. #include <linux/module.h>  
  5. #include <linux/kernel.h>  
  6. #include <linux/fs.h>  
  7. #include <linux/init.h>  
  8. #include <linux/delay.h>  
  9. #include <linux/poll.h>  
  10. #include <linux/irq.h>  
  11. #include <asm/irq.h>  
  12. #include <linux/interrupt.h>  
  13. #include <asm/uaccess.h>  
  14. #include <mach/regs-gpio.h>  
  15. #include <mach/hardware.h>  
  16. #include <linux/cdev.h>  
  17. #include <linux/miscdevice.h>  
  18. #include <linux/sched.h>  
  19. #include <linux/gpio.h>  
  20.   
  21. static struct resource key_resource[]=  
  22. {     
  23.     [0] = {  
  24.         .start = IRQ_EINT8,  
  25.         .end = IRQ_EINT8,  
  26.         .flags = IORESOURCE_IRQ,  
  27.     },  
  28.     [1] = {  
  29.         .start = IRQ_EINT11,  
  30.         .end = IRQ_EINT11,  
  31.         .flags = IORESOURCE_IRQ,  
  32.     },  
  33.     [2]= {  
  34.         .start = IRQ_EINT13,  
  35.         .end = IRQ_EINT13,  
  36.         .flags = IORESOURCE_IRQ,  
  37.     },  
  38.     [3] = {  
  39.         .start = IRQ_EINT14,  
  40.         .end = IRQ_EINT14,  
  41.         .flags = IORESOURCE_IRQ,  
  42.     },  
  43.     [4] = {  
  44.         .start = IRQ_EINT15,  
  45.         .end = IRQ_EINT15,  
  46.         .flags = IORESOURCE_IRQ,  
  47.     },  
  48.     [5] = {  
  49.         .start = IRQ_EINT19,  
  50.         .end = IRQ_EINT19,  
  51.         .flags = IORESOURCE_IRQ,  
  52.     },  
  53. };  
  54.   
  55. struct platform_device *my_buttons_dev;  
  56.   
  57. static int __init platform_dev_init(void)  
  58. {  
  59.     int ret;  
  60.       
  61.     my_buttons_dev = platform_device_alloc("my_buttons", -1);  
  62.       
  63.     platform_device_add_resources(my_buttons_dev,key_resource,6);//添加资源一定要用该函数,不能使用对platform_device->resource幅值  
  64.                                                                 //否则会导致platform_device_unregister调用失败,内核异常。  
  65.       
  66.     ret = platform_device_add(my_buttons_dev);  
  67.       
  68.     if(ret)  
  69.         platform_device_put(my_buttons_dev);  
  70.       
  71.     return ret;  
  72. }  
  73.   
  74. static void __exit platform_dev_exit(void)  
  75. {  
  76.     platform_device_unregister(my_buttons_dev);  
  77. }  
  78.   
  79. module_init(platform_dev_init);  
  80. module_exit(platform_dev_exit);  
  81.   
  82. MODULE_AUTHOR("Y-Kee");  
  83. MODULE_LICENSE("GPL");  


 

platform平台驱动

  1. //platform driver  
  2. #include <linux/module.h>  
  3. #include <linux/types.h>  
  4. #include <linux/miscdevice.h>  
  5. #include <linux/fs.h>  
  6. #include <linux/init.h>  
  7. #include <linux/platform_device.h>  
  8. #include <linux/interrupt.h>  
  9. #include <linux/clk.h>  
  10. #include <linux/uaccess.h>  
  11. #include <linux/io.h>  
  12. #include <mach/map.h>  
  13. #include <mach/regs-gpio.h>  
  14. #include <linux/poll.h>  
  15. #include <linux/irq.h>  
  16. #include <asm/unistd.h>  
  17. #include <linux/device.h>  
  18.   
  19.   
  20. static int buttons_irq[6];  
  21.   
  22. struct irq_des  
  23. {  
  24.     int *buttons_irq;  
  25.     char *name[6];  
  26. };  
  27.   
  28.   
  29. struct irq_des button_irqs = {   
  30.     .buttons_irq = buttons_irq,  
  31.     .name = {"KEY0", "KEY1", "KEY2", "KEY3", "KEY4", "KEY5"},  
  32. };  
  33.   
  34. static volatile int key_values;  
  35.   
  36.   
  37. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);  
  38.   
  39.   
  40. static volatile int ev_press = 0;  
  41.   
  42.   
  43. static irqreturn_t buttons_interrupt(int irq, void *dev_id)  
  44. {  
  45.     int i;  
  46.     for(i=0; i<6; i++){  
  47.         if(irq == buttons_irq[i]){  
  48.             key_values = i;  
  49.             ev_press = 1;  
  50.             wake_up_interruptible(&button_waitq);     
  51.         }  
  52.     }  
  53.       
  54.     return IRQ_RETVAL(IRQ_HANDLED);  
  55. }  
  56.   
  57.   
  58. static int s3c24xx_buttons_open(struct inode *inode, struct file *file)  
  59. {  
  60.     int i;  
  61.     int err = 0;  
  62.       
  63.     for (i = 0; i < 6; i++) {  
  64.         err = request_irq(button_irqs.buttons_irq[i], buttons_interrupt, IRQ_TYPE_EDGE_BOTH,   
  65.                           button_irqs.name[i], (void *)&button_irqs.buttons_irq[i]);  
  66.         if (err)  
  67.             break;  
  68.     }  
  69.   
  70.     if (err) {  
  71.         i--;  
  72.         for (; i >= 0; i--) {  
  73.         if (button_irqs.buttons_irq[i] < 0) {  
  74.         continue;  
  75.         }  
  76.         disable_irq(button_irqs.buttons_irq[i]);  
  77.             free_irq(button_irqs.buttons_irq[i], (void *)&button_irqs.buttons_irq[i]);  
  78.         }  
  79.         return -EBUSY;  
  80.     }  
  81.       
  82.     return 0;  
  83. }  
  84.   
  85.   
  86. static int s3c24xx_buttons_close(struct inode *inode, struct file *file)  
  87. {  
  88.     int i;  
  89.       
  90.     for (i = 0; i < 6; i++) {  
  91.     free_irq(button_irqs.buttons_irq[i], (void *)&button_irqs.buttons_irq[i]);  
  92.     }  
  93.   
  94.     return 0;  
  95. }  
  96.   
  97.   
  98. static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)  
  99. {  
  100.     unsigned long err;  
  101.   
  102.     if (!ev_press) {  
  103.     if (filp->f_flags & O_NONBLOCK)  
  104.         return -EAGAIN;  
  105.     else  
  106.         wait_event_interruptible(button_waitq, ev_press);  
  107.     }  
  108.       
  109.     ev_press = 0;  
  110.   
  111.     err = copy_to_user(buff, (const void *)&key_values, min(sizeof(key_values), count));  
  112.   
  113.     return err ? -EFAULT : min(sizeof(key_values), count);  
  114. }  
  115.   
  116. static unsigned int s3c24xx_buttons_poll( struct file *file, struct poll_table_struct *wait)  
  117. {  
  118.     unsigned int mask = 0;  
  119.     poll_wait(file, &button_waitq, wait);  
  120.     if (ev_press)  
  121.         mask |= POLLIN | POLLRDNORM;  
  122.     return mask;  
  123. }  
  124.   
  125.   
  126. static struct file_operations dev_fops = {  
  127.     .owner   =   THIS_MODULE,  
  128.     .open    =   s3c24xx_buttons_open,  
  129.     .release =   s3c24xx_buttons_close,   
  130.     .read    =   s3c24xx_buttons_read,  
  131.     .poll    =   s3c24xx_buttons_poll,  
  132. };  
  133.   
  134. static struct miscdevice misc = {  
  135.     .minor = MISC_DYNAMIC_MINOR,  
  136.     .name = "my_buttons",  
  137.     .fops = &dev_fops,  
  138. };  
  139.   
  140.   
  141. static int my_plat_probe(struct platform_device *dev)  
  142. {  
  143.     int ret,i;  
  144.     struct resource *plat_resource;  
  145.     struct platform_device *pdev = dev;  
  146.       
  147.     printk("my platform dirver find my platfrom device.\n");  
  148.   
  149.     for(i=0; i<6; i++){  
  150.         plat_resource = platform_get_resource(pdev,IORESOURCE_IRQ,i);  
  151.         if(plat_resource == NULL)  
  152.             return -ENOENT;   
  153.         buttons_irq[i] = plat_resource->start;  
  154.     }  
  155.   
  156.     ret = misc_register(&misc);  
  157.     if(ret)  
  158.         return ret;  
  159.   
  160.       
  161.     return 0;  
  162. }  
  163.   
  164. static int my_plat_remove(struct platform_device *dev)  
  165. {  
  166.     printk("my platfrom device has removed.\n");  
  167.     misc_deregister(&misc);  
  168.     return 0;  
  169. }  
  170.   
  171. struct platform_driver my_buttons_drv = {   
  172.     .probe = my_plat_probe,  
  173.     .remove = my_plat_remove,  
  174.     .driver = {   
  175.         .owner = THIS_MODULE,  
  176.         .name = "my_buttons",  
  177.     },  
  178. };  
  179.   
  180. static int __init platform_drv_init(void)  
  181. {  
  182.     int ret;  
  183.   
  184.     ret = platform_driver_register(&my_buttons_drv);  
  185.       
  186.     return ret;  
  187. }  
  188.   
  189. static void __exit platform_drv_exit(void)  
  190. {  
  191.     platform_driver_unregister(&my_buttons_drv);  
  192. }  
  193.   
  194. module_init(platform_drv_init);  
  195. module_exit(platform_drv_exit);  
  196.   
  197. MODULE_AUTHOR("Y-Kee");  
  198. MODULE_LICENSE("GPL");  

测试代码
  1. /*  
  2.  *      Buttons Example for Matrix V  
  3.  *  
  4.  *      Copyright (C) 2004 capbily - friendly-arm  
  5.  *  capbily@hotmail.com  
  6.  */  
  7. #include <stdio.h>  
  8. #include <stdlib.h>  
  9. #include <unistd.h>  
  10. #include <sys/ioctl.h>  
  11. #include <sys/types.h>  
  12. #include <sys/stat.h>  
  13. #include <fcntl.h>  
  14. #include <sys/select.h>  
  15. #include <sys/time.h>  
  16. #include <errno.h>  
  17.   
  18. int main(void)  
  19. {  
  20.     int buttons_fd;  
  21.     int key_value;  
  22.   
  23.     buttons_fd = open("/dev/buttons", 0);  
  24.     if (buttons_fd < 0) {  
  25.         perror("open device buttons");  
  26.         exit(1);  
  27.     }  
  28.   
  29.     for (;;) {  
  30.         fd_set rds;  
  31.         int ret;  
  32.   
  33.         FD_ZERO(&rds);  
  34.         FD_SET(buttons_fd, &rds);  
  35.   
  36.         ret = select(buttons_fd + 1, &rds, NULL, NULL, NULL);  
  37.         if (ret < 0) {  
  38.             perror("select");  
  39.             exit(1);  
  40.         }  
  41.         if (ret == 0) {  
  42.             printf("Timeout.\n");  
  43.         } else if (FD_ISSET(buttons_fd, &rds)) {  
  44.             int ret = read(buttons_fd, &key_value, sizeof key_value);  
  45.             if (ret != sizeof key_value) {  
  46.                 if (errno != EAGAIN)  
  47.                     perror("read buttons\n");  
  48.                 continue;  
  49.             } else {  
  50.                 printf("buttons_value: %d\n", key_value+1);  
  51.             }  
  52.                   
  53.         }  
  54.     }  
  55.   
  56.     close(buttons_fd);  
  57.     return 0;  
  58. }  

测试结果:
运行测试程序后按下第二个键,中断上打印了多次按键的键值,产生原因是因为按键抖动。导致按一下按键,产生多次中断。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值