Chinaunix首页 | 论坛 | 博客
  • 博客访问: 392797
  • 博文数量: 69
  • 博客积分: 1992
  • 博客等级: 上尉
  • 技术积分: 1162
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-03 19:50
文章分类
文章存档

2015年(1)

2011年(55)

2010年(13)

分类: LINUX

2010-07-14 20:58:38

此版本主要参照友善之臂和一篇博客的。不解释,自己理解。
驱动程序:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/irq.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
//#include

#include <linux/cdev.h>
//#include

#include <linux/sched.h>
#include <linux/gpio.h>
//#include

#include <linux/device.h>

#define DEVICE_NAME "buttons"
#define DEVICE_MAJOR        234

struct class *button_class;

struct button_irq_desc {
    int irq;
    int pin;
    int pin_setting;
    int number;
    char *name;    
};

static struct button_irq_desc button_irqs [] = {
    {IRQ_EINT8 , S3C2410_GPG(0) , S3C2410_GPG0_EINT8 , 0, "KEY0"},
    {IRQ_EINT11, S3C2410_GPG(3) , S3C2410_GPG3_EINT11 , 1, "KEY1"},
    {IRQ_EINT13, S3C2410_GPG(5) , S3C2410_GPG5_EINT13 , 2, "KEY2"},
    {IRQ_EINT14, S3C2410_GPG(6) , S3C2410_GPG6_EINT14 , 3, "KEY3"},
    {IRQ_EINT15, S3C2410_GPG(7) , S3C2410_GPG7_EINT15 , 4, "KEY4"},
    {IRQ_EINT19, S3C2410_GPG(11), S3C2410_GPG11_EINT19, 5, "KEY5"},
};
static volatile char key_values [] = {'0', '0', '0', '0', '0', '0'};

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

static volatile int ev_press = 0;


static irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
    struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id;
    int down;

    down = !s3c2410_gpio_getpin(button_irqs->pin);

    if (down != (key_values[button_irqs->number] & 1))
    {

        key_values[button_irqs->number] = '0' + down;
    
        ev_press = 1;
        wake_up_interruptible(&button_waitq);
        }
    
    return IRQ_RETVAL(IRQ_HANDLED);
}


static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
{
    int i;
    int err = 0;
    
    for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++)
        {
            s3c2410_gpio_cfgpin(button_irqs[i].pin,button_irqs[i].pin_setting);
            err = request_irq(button_irqs[i].irq, buttons_interrupt, IRQ_TYPE_EDGE_BOTH,
                           button_irqs[i].name, (void *)&button_irqs[i]);
            //set_irq_type(button_irqs[i].irq,IRQ_TYPE_EDGE_BOTH);

            if (err)
            break;
            }

    if (err) {
        i--;
        for (; i >= 0; i--)
        {
         disable_irq(button_irqs[i].irq);
         free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);
                }
        return -EBUSY;
    }
    return 0;
}


static int s3c24xx_buttons_close(struct inode *inode, struct file *file)
{
    int i;
    
    for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++)
    {

    free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);
       }

    return 0;
}


static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
    unsigned long err;

    if (!ev_press) {
    if (filp->f_flags & O_NONBLOCK)
     return -EAGAIN;
    else
     wait_event_interruptible(button_waitq, ev_press);
    }
    
    ev_press = 0;

    err = copy_to_user(buff, (const void *)key_values, min(sizeof(key_values), count));

    return err ? -EFAULT : min(sizeof(key_values), count);
}

static unsigned int s3c24xx_buttons_poll( struct file *file, struct poll_table_struct *wait)
{
    unsigned int mask = 0;
    poll_wait(file, &button_waitq, wait);
    if (ev_press)
        mask |= POLLIN | POLLRDNORM;
    return mask;
}


static struct file_operations button_fops = {
    .owner = THIS_MODULE,
    .open = s3c24xx_buttons_open,
    .release = s3c24xx_buttons_close,
    .read = s3c24xx_buttons_read,
    .poll = s3c24xx_buttons_poll,
};

static int __init button_init(void)
{
    int ret;

    ret = register_chrdev(DEVICE_MAJOR,DEVICE_NAME,&button_fops);
    
    if(ret < 0)
        {
        printk(DEVICE_NAME"can't register this device\n");
        return ret;
        }
//////////////////////////////////////////////////////////////////////////////////////////////////

    //devfs_mk_cdev(MKDEV(DEVICE_MAJOR,0),S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP,DEVICE_NAME);

    //printk(DEVICE_NAME"is setup completed\n");

    button_class = class_create(THIS_MODULE,"button_driver");
    device_create(button_class,NULL,MKDEV(DEVICE_MAJOR,0),NULL,"buttons");
//////////////////////////////////////////////////////////////////////////////////////////////////

    return 0;
}

static void __exit button_exit(void)
{    
///////////////////////////////////////////////////////////////////////////////////////////////////

    //devfs_remove(DEVICE_NAME);

    device_destroy(button_class,MKDEV(DEVICE_MAJOR,0));
    class_destroy(button_class);
///////////////////////////////////////////////////////////////////////////////////////////////////

    unregister_chrdev(DEVICE_MAJOR,DEVICE_NAME);
}

module_init(button_init);
module_exit(button_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("pursuitxh_lwm");

驱动程序Makefile:
KERNELDIR = /home/lwm/linux-2.6.32.2
PWD := $(shell pwd)
CROSS_COMPILE = /usr/local/arm/4.3.2/bin/arm-linux-
CC = $(CROSS_COMPILE)gcc
obj-m := button.o
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
    rm -rf *.ko *.o *.mod.o *.mod.c *.symvers *.order *~
.PHONY: modules modules_install clean

测试程序:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>

int main(void)
{
    int buttons_fd;
    char buttons[6] = {'0', '0', '0', '0', '0', '0'};

    buttons_fd = open("/dev/buttons", 0);
    if (buttons_fd < 0) {
        perror("open device buttons");
        exit(1);
    }

    for (;;) {
        char current_buttons[6];
        int count_of_changed_key;
        int i;
        if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) {
            perror("read buttons:");
            exit(1);
        }

        for (i = 0, count_of_changed_key = 0; i < sizeof buttons / sizeof buttons[0]; i++) {
            if (buttons[i] != current_buttons[i]) {
                buttons[i] = current_buttons[i];
                printf("%skey %d is %s", count_of_changed_key? ", ": "", i+1, buttons[i] == '0' ? "up" : "down");
                count_of_changed_key++;
            }
        }
        if (count_of_changed_key) {
            printf("\n");
        }
        
        
    }

    close(buttons_fd);
    return 0;
}

测试程序Makefile:
CROSS=arm-linux-

all: buttons

buttons: buttons_test.c
    $(CROSS)gcc -o buttons buttons_test.c

clean:
    @rm -vf buttons *.o *~


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