字元裝置驅動之Buttons-等待隊列

來源:互聯網
上載者:User

buttons.c 

#include <linux/fs.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/ioport.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <asm/mach/irq.h>
#include <asm/arch/regs-gpio.h>

static int major = 0;
static struct class *cls;

/* gpecon 0x56000040 */
/* gpfcon 0x56000050 */
/* gpgcon 0x56000060 */
static volatile unsigned long *gpecon;
static volatile unsigned long *gpedat;

static volatile unsigned long *gpfcon;
static volatile unsigned long *gpfdat;

static volatile unsigned long *gpgcon;
static volatile unsigned long *gpgdat;

struct key_desc {
 int irq;
 int pin;
 char *name;
 char key_val;
};

struct key_desc key_desc[] = {
 {IRQ_EINT0,  S3C2410_GPF0,  "K10", 1}, /* 鬆開: 1, 按下: 0x81 */
 {IRQ_EINT2,  S3C2410_GPF2,  "K7",  2}, /* 鬆開: 2, 按下: 0x82 */
 {IRQ_EINT11, S3C2410_GPG3,  "K4",  3}, /* 鬆開: 3, 按下: 0x83 */
 {IRQ_EINT19, S3C2410_GPG11, "K1",  4}, /* 鬆開: 4, 按下: 0x84 */
};
 
volatile char key = 0;

static wait_queue_head_t
button_waitq;

static irqreturn_t buttons_irq(int irq, void *dev_id)
{
 struct key_desc *kd = (struct key_desc *)dev_id;
 int up;
 
 /* 確定按鍵: 哪個按鍵,按下還是鬆開 */
 up = s3c2410_gpio_getpin(kd->pin); // gpfdat, gpgdat
 if (up)
 {
  key = kd->key_val;
 }
 else
 {
  key = kd->key_val | 0x80;
 }

 // printk("key = 0x%x\n", key);

 /* 喚醒應用程式 */
 wake_up_interruptible(&button_waitq);

 printk("buttons_irq current %s , pid = %d \n", current->comm, current->pid);

 
 return IRQ_HANDLED; 
}

int buttons_open(struct inode *inode, struct file *file)
{
 int i;
 
 /* 註冊中斷 */ 
 for (i = 0; i < 4; i++)
 {
  request_irq(key_desc[i].irq, buttons_irq,
              IRQF_SHARED | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
              key_desc[i].name, &key_desc[i]);
 }

 /* 設定GPIO為中斷引腳
  * 設定觸發方式
  * 使能中斷
  */

 /* 設定KSCAN0(GPE11)為輸出引腳,輸出0 */
 *gpecon &= ~(0x3 << 22);
 *gpecon |= (1 << 22);
 *gpedat &= ~(1<<11);
 
 return 0;
}

ssize_t buttons_read(struct file *inode, char __user *buf, size_t size, loff_t *offset)
{
 /* 如果沒有按鍵發生, 休眠 */
 /* key 等於 0, 才會休眠
  * key 非0, 不會休眠
  */
 // command
 printk("current %s , pid = %d before sleep\n", current->comm, current->pid);
 
 wait_event_interruptible(button_waitq,
key
);/*key為等待事件,等待這個為真*/
 
 printk("current %s , pid = %d after sleep\n", current->comm, current->pid);

 /* 被喚醒後, 把按索引值返回給使用者程式 */
 copy_to_user(buf, &key, 1);
 key = 0;
 
 return 1;
}

int buttons_close(struct inode *inode, struct file *file)
{
 int i;
 for (i = 0; i < 4; i++)
 {
  free_irq(key_desc[i].irq,
&key_desc[i]);
 }
 return 0;
}

 

static const struct file_operations buttons_fops = {
 .owner  = THIS_MODULE,
 .read  = buttons_read,
 .open  = buttons_open,  /* 設定引腳,申請資源 */
 .release = buttons_close,
};

int buttons_init(void)
{
 int i;
 
 major = register_chrdev(0, "buttons", &buttons_fops);

 /* sysfs  ==> 掛接到/sys */
 cls = class_create(THIS_MODULE, "buttons_class");
 class_device_create(cls, NULL, MKDEV(major, 0), NULL, "buttons");

 // mdev會根據/sys下的這些內容建立/dev/buttons

 gpecon = ioremap(0x56000040, 4096);
 gpedat = gpecon + 1;

 gpfcon = gpecon + 4;
 gpfdat = gpfcon + 1;

 gpgcon = gpecon + 8;
 gpgdat = gpgcon + 1;

 init_waitqueue_head(&button_waitq);
 
 return 0;
}

void buttons_exit(void)

 unregister_chrdev(major, "buttons");

 class_device_destroy(cls, MKDEV(major, 0));
 class_destroy(cls);

 iounmap(gpecon); 
}

module_init(buttos_init);
module_exit(buttons_exit);

MODULE_LICENSE("GPL");

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.