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

2015年(1)

2011年(55)

2010年(13)

分类: LINUX

2010-07-15 20:04:36

用了cdev结构体
最新的注册字符设备的函数

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>

#include <linux/delay.h>

#include <linux/irq.h>
#include <asm/irq.h>
#include <linux/interrupt.h>

#include <asm/uaccess.h>
//#include

#include <mach/regs-gpio.h>
//#include

#include <mach/hardware.h>

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

#include <linux/cdev.h>

#include <linux/device.h>//自动创建inode节点包含头文件


struct class *key_class;

#define DEVICE_NAME "mini2440_key"
#define KEY_MAJOR 200
static int key_major = KEY_MAJOR;
static DECLARE_WAIT_QUEUE_HEAD(key_waitq);
static volatile int ev_press = 0;
static volatile int press_cnt[] = { 0, 0, 0, 0, 0, 0};
struct key_irq_desc {
 unsigned int irq;
 unsigned long flags;
 const char *name;
};
static struct key_irq_desc key_irqs[] = {
 {IRQ_EINT8, IRQF_TRIGGER_FALLING, "key1"},
 {IRQ_EINT11, IRQF_TRIGGER_FALLING, "key2"},
 {IRQ_EINT13, IRQF_TRIGGER_FALLING, "key3"},
 {IRQ_EINT14, IRQF_TRIGGER_FALLING, "key4"},
 {IRQ_EINT15, IRQF_TRIGGER_FALLING, "key5"},
 {IRQ_EINT19, IRQF_TRIGGER_FALLING, "key6"}
};

struct key_dev {
    struct cdev cdev;
};

struct key_dev *key_devp;

static irqreturn_t key_interrupt(int irq, void *dev_id)
{
 volatile int *press_cnt = (volatile int *)dev_id;
 
 *press_cnt = *press_cnt + 1;
 ev_press = 1;
 wake_up_interruptible(&key_waitq);
 
 return IRQ_RETVAL(IRQ_HANDLED);
}
 

static int mini2440_key_open(struct inode *inode, struct file *file)
{
 int i;
 int err;
 
 for (i = 0; i < sizeof(key_irqs) / sizeof(key_irqs[0]); i++)
 {
  err = request_irq(key_irqs[i].irq, key_interrupt, key_irqs[i].flags,key_irqs[i].name, (void *)&press_cnt[i]);
  if (err)
  {
   break;
  }
 }
 if (err)
 {
  i--;
  for (; i >= 0; i--)
  {
   free_irq(key_irqs[i].irq, (void *)&press_cnt[i]);
  }
 }
 return 0;
}

static int mini2440_key_release(struct inode *inode, struct file *file)
{
 int i;
 
 for (i = 0; i < sizeof(key_irqs) / sizeof(key_irqs[0]); i++)
 {
  free_irq(key_irqs[i].irq, (void *)&press_cnt[i]);
 }
 return 0;
}
static int mini2440_key_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
 unsigned long err;
 
 wait_event_interruptible(key_waitq, ev_press);
 
 ev_press = 0;
 
 err = copy_to_user(buff, (const void *)press_cnt, min(sizeof(press_cnt), count));
 
 //memset((void *)press_cnt, 0, sizeof(press_cnt));

 
 return err ? -EFAULT : 0;
}
static struct file_operations mini2440_key_fops = {
 .owner = THIS_MODULE,
 .open = mini2440_key_open,
 .release = mini2440_key_release,
 .read = mini2440_key_read,
};
static int __init mini2440_key_init(void)
{
    int ret;
    dev_t devnum = MKDEV(KEY_MAJOR,0);
    if(key_major)
    {
        ret = register_chrdev_region(devnum,1,DEVICE_NAME);
        printk(KERN_INFO" static keymajor is %d\n",key_major);
    }
    else
    {
        ret = alloc_chrdev_region(&devnum,0,1,DEVICE_NAME);
        key_major = MAJOR(devnum);
        printk(KERN_INFO" auto keymajor is %d\n",key_major);
    }
// ret = register_chrdev(KEY_MAJOR, DEVICE_NAME, &mini2440_key_fops);

     if (ret < 0)
     {
        printk(KERN_NOTICE"can not register this major number!\n");
          return ret;
     }
    
    key_devp = kmalloc(sizeof(struct key_dev),GFP_KERNEL);
    if (!key_devp)
    {
        ret = -ENOMEM;
        goto fail_malloc;
    }
    
    /*下面两行自动创建inode节点*/
    key_class = class_create(THIS_MODULE,"key_driver");
    device_create(key_class,NULL,MKDEV(key_major,0),NULL,DEVICE_NAME);


    cdev_init(&key_devp->cdev,&mini2440_key_fops);
    key_devp->cdev.owner = THIS_MODULE;
    key_devp->cdev.ops = &mini2440_key_fops;
    ret = cdev_add(&key_devp->cdev,devnum,1);

    if(ret)
    {
        printk(KERN_NOTICE"regist char device error!\n");
    }    
    
     printk(KERN_INFO" button driver module has register sucessed!\n");

    fail_malloc:unregister_chrdev_region(devnum,1);
     return 0;
}
static void __exit mini2440_key_exit(void)
{
    
    //unregister_chrdev(KEY_MAJOR, DEVICE_NAME);

    
    cdev_del(&(key_devp->cdev));
    kfree(key_devp);
    key_devp = NULL;
    
    device_destroy(key_class,MKDEV(key_major,0));
    class_destroy(key_class);
    
    unregister_chrdev_region(MKDEV(key_major,0),1);    

    printk(KERN_INFO" button driver module has removed sucessed!\n");
 }
module_init(mini2440_key_init);
module_exit(mini2440_key_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lwm");


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