Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2707820
  • 博文数量: 505
  • 博客积分: 1552
  • 博客等级: 上尉
  • 技术积分: 2514
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-23 18:24
文章分类

全部博文(505)

文章存档

2019年(12)

2018年(15)

2017年(1)

2016年(17)

2015年(14)

2014年(93)

2013年(233)

2012年(108)

2011年(1)

2009年(11)

分类:

2012-08-29 23:08:57

原文地址:Linux驱动学习笔记(6) 作者:jiaweijing

同步、互斥、阻塞
目标:同一时刻只能有一个应用程序打开驱动程序。

点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/delay.h>
  6. #include <linux/irq.h>
  7. #include <asm/uaccess.h>
  8. #include <asm/irq.h>
  9. #include <asm/io.h>
  10. #include <asm/arch/regs-gpio.h>
  11. #include <asm/hardware.h>
  12. #include <linux/poll.h>


  13. static struct class *sixthdrv_class;
  14. static struct class_device    *sixthdrv_class_dev;

  15. volatile unsigned long *gpfcon;
  16. volatile unsigned long *gpfdat;

  17. volatile unsigned long *gpgcon;
  18. volatile unsigned long *gpgdat;


  19. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

  20. /* 中断事件标志, 中断服务程序将它置1,sixth_drv_read将它清0 */
  21. static volatile int ev_press = 0;

  22. static struct fasync_struct *button_async;


  23. struct pin_desc{
  24.     unsigned int pin;
  25.     unsigned int key_val;
  26. };


  27. /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
  28. /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
  29. static unsigned char key_val;

  30. struct pin_desc pins_desc[4] = {
  31.     {S3C2410_GPF0, 0x01},
  32.     {S3C2410_GPF2, 0x02},
  33.     {S3C2410_GPG3, 0x03},
  34.     {S3C2410_GPG11, 0x04},
  35. };

  36. //static atomic_t canopen = ATOMIC_INIT(1); //定义原子变量并初始化为1

  37. static DECLARE_MUTEX(button_lock); //定义互斥锁

  38. /*
  39.   * 确定按键值
  40.   */
  41. static irqreturn_t buttons_irq(int irq, void *dev_id)
  42. {
  43.     struct pin_desc * pindesc = (struct pin_desc *)dev_id;
  44.     unsigned int pinval;
  45.     
  46.     pinval = s3c2410_gpio_getpin(pindesc->pin);

  47.     if (pinval)
  48.     {
  49.         /* 松开 */
  50.         key_val = 0x80 | pindesc->key_val;
  51.     }
  52.     else
  53.     {
  54.         /* 按下 */
  55.         key_val = pindesc->key_val;
  56.     }

  57.     ev_press = 1; /* 表示中断发生了 */
  58.     wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
  59.     
  60.     kill_fasync (&button_async, SIGIO, POLL_IN);
  61.     
  62.     return IRQ_RETVAL(IRQ_HANDLED);
  63. }

  64. static int sixth_drv_open(struct inode *inode, struct file *file)
  65. {
  66. #if 0    
  67.     if (!atomic_dec_and_test(&canopen))
  68.     {
  69.         atomic_inc(&canopen);
  70.         return -EBUSY;
  71.     }
  72. #endif        

  73.     if (file->f_flags & O_NONBLOCK)    // 阻塞
  74.     {
  75.         if (down_trylock(&button_lock))  // 如果获取不到信号量,则立即返回。
  76.             return -EBUSY;
  77.     }                                 
  78.     else                               // 非阻塞
  79.     {
  80.         /* 获取信号量 */
  81.         down(&button_lock);    // 第二次获取信号量时会进入休眠,第一个应用程序释放后才会被唤醒。
  82.     }

  83.     /* 配置GPF0,2为输入引脚 */
  84.     /* 配置GPG3,11为输入引脚 */
  85.     request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
  86.     request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
  87.     request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
  88.     request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);    

  89.     return 0;
  90. }

  91. ssize_t sixth_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
  92. {
  93.     if (size != 1)
  94.         return -EINVAL;

  95.     if (file->f_flags & O_NONBLOCK)  // 如果是阻塞
  96.     {
  97.         if (!ev_press)               // 判断有无按键按下
  98.             return -EAGAIN;          // 没有按键按下再再次执行。
  99.     }
  100.     else                             // 阻塞
  101.     {
  102.         /* 如果没有按键动作, 休眠 */
  103.         wait_event_interruptible(button_waitq, ev_press);
  104.     }

  105.     /* 如果有按键动作, 返回键值 */
  106.     copy_to_user(buf, &key_val, 1);
  107.     ev_press = 0;
  108.     
  109.     return 1;
  110. }


  111. int sixth_drv_close(struct inode *inode, struct file *file)
  112. {
  113.     //atomic_inc(&canopen);
  114.     free_irq(IRQ_EINT0, &pins_desc[0]);
  115.     free_irq(IRQ_EINT2, &pins_desc[1]);
  116.     free_irq(IRQ_EINT11, &pins_desc[2]);
  117.     free_irq(IRQ_EINT19, &pins_desc[3]);
  118.     up(&button_lock);
  119.     return 0;
  120. }

  121. static unsigned sixth_drv_poll(struct file *file, poll_table *wait)
  122. {
  123.     unsigned int mask = 0;
  124.     poll_wait(file, &button_waitq, wait); // 不会立即休眠

  125.     if (ev_press)
  126.         mask |= POLLIN | POLLRDNORM;

  127.     return mask;
  128. }

  129. static int sixth_drv_fasync (int fd, struct file *filp, int on)
  130. {
  131.     printk("driver: sixth_drv_fasync\n");
  132.     return fasync_helper (fd, filp, on, &button_async);
  133. }


  134. static struct file_operations sencod_drv_fops = {
  135.     .owner = THIS_MODULE, /* 这是一个宏,指向编译模块时自动创建的__this_module变量 */
  136.     .open = sixth_drv_open,
  137.     .read     =    sixth_drv_read,    
  138.     .release = sixth_drv_close,
  139.     .poll = sixth_drv_poll,
  140.     .fasync     = sixth_drv_fasync,
  141. };


  142. int major;
  143. static int sixth_drv_init(void)
  144. {
  145.     major = register_chrdev(0, "sixth_drv", &sencod_drv_fops);

  146.     sixthdrv_class = class_create(THIS_MODULE, "sixth_drv");

  147.     sixthdrv_class_dev = class_device_create(sixthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */

  148.     gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
  149.     gpfdat = gpfcon + 1;

  150.     gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
  151.     gpgdat = gpgcon + 1;

  152.     return 0;
  153. }

  154. static void sixth_drv_exit(void)
  155. {
  156.     unregister_chrdev(major, "sixth_drv");
  157.     class_device_unregister(sixthdrv_class_dev);
  158.     class_destroy(sixthdrv_class);
  159.     iounmap(gpfcon);
  160.     iounmap(gpgcon);
  161.     return 0;
  162. }


  163. module_init(sixth_drv_init);

  164. module_exit(sixth_drv_exit);

  165. MODULE_LICENSE("GPL");
1. 原子操作
原子操作指的是在执行过程中不会被别的代码路径所中断的操作。
常用原子操作函数举例:
atomic_t v = ATOMIC_INIT(0);     //定义原子变量v并初始化为0
atomic_read(atomic_t *v);        //返回原子变量的值
void atomic_inc(atomic_t *v);    //原子变量增加1
void atomic_dec(atomic_t *v);    //原子变量减少1
int atomic_dec_and_test(atomic_t *v); //自减操作后测试其是否为0,为0则返回true,否则返回false。

2. 信号量
信号量(semaphore)是用于保护临界区的一种常用方法,只有得到信号量的进程才能执行临界区代码。
当获取不到信号量时,进程进入休眠等待状态。

定义信号量
struct semaphore sem;
初始化信号量
void sema_init (struct semaphore *sem, int val);
void init_MUTEX(struct semaphore *sem);//初始化为0

static DECLARE_MUTEX(button_lock);     //定义互斥锁

获得信号量
void down(struct semaphore * sem);
int down_interruptible(struct semaphore * sem);
int down_trylock(struct semaphore * sem);
释放信号量
void up(struct semaphore * sem);

3. 阻塞
阻塞操作   
是指在执行设备操作时若不能获得资源则挂起进程,直到满足可操作的条件后再进行操作。
被挂起的进程进入休眠状态,被从调度器的运行队列移走,直到等待的条件被满足。

非阻塞操作 
进程在不能进行设备操作时并不挂起,它或者放弃,或者不停地查询,直至可以进行操作为止。

fd = open("...", O_RDWR | O_NONBLOCK);


点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <poll.h>
  6. #include <signal.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>


  10. /* sixthdrvtest
  11.   */
  12. int fd;

  13. void my_signal_fun(int signum)
  14. {
  15.     unsigned char key_val;
  16.     read(fd, &key_val, 1);
  17.     printf("key_val: 0x%x\n", key_val);
  18. }

  19. int main(int argc, char **argv)
  20. {
  21.     unsigned char key_val;
  22.     int ret;
  23.     int Oflags;

  24.     //signal(SIGIO, my_signal_fun);
  25.     
  26.     fd = open("/dev/buttons", O_RDWR | O_NONBLOCK);   // 非阻塞的方式打开
  27.     if (fd < 0)
  28.     {
  29.         printf("can't open!\n");
  30.         return -1;
  31.     }

  32.     //fcntl(fd, F_SETOWN, getpid());
  33.     
  34.     //Oflags = fcntl(fd, F_GETFL);
  35.     
  36.     //fcntl(fd, F_SETFL, Oflags | FASYNC);


  37.     while (1)
  38.     {
  39.         ret = read(fd, &key_val, 1);
  40.         printf("key_val: 0x%x, ret = %d\n", key_val, ret);
  41.         sleep(5);
  42.     }
  43.     
  44.     return 0;
  45. }




阅读(1322) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~