fl2440——驱动学习-Platform

    xiaoxiao2021-03-25  132

    #include "s3c_driver.h" #define DRV_AUTHOR "Tang bin" #define DRV_DESC "S3C24XX button driver" /* Driver version*/ #define DRV_MAJOR_VER 1 #define DRV_MINOR_VER 0 #define DRV_REVER_VER 0 #define DEV_NAME DEV_BUTTON_NAME //#define DEV_MAJOR DEV_BUTTON_MAJOR #ifndef DEV_MAJOR #define DEV_MAJOR 0 /* dynamic major by default */ #endif #define BUTTON_UP 0 /* Button status is up */ #define BUTTON_DOWN 1 /* Button status is pushed down */ #define BUTTON_UNCERTAIN 2 /* Button status uncerntain */ #define TIMER_DELAY_DOWN (HZ/50) /*Remove button push down dithering timer delay 20ms */ #define TIMER_DELAY_UP (HZ/10) /*Remove button up dithering timer delay 100ms */ static int debug = DISABLE; static int dev_major = DEV_MAJOR; static int dev_minor = 0; /*============================ Platform Device part ===============================*/ /* Button hardware informtation structure*/ struct s3c_button_info //按键结构体信息 { unsigned char num; /*Button nubmer 第几个按键*/ char * name; /*Button nubmer 按键名*/ int nIRQ; /*Button IRQ number 按键中断号*/ unsigned int setting; /*Button IRQ Pin Setting 管脚设置中断*/ unsigned int gpio; /*Button GPIO port 按键对应的管脚*/ }; /* The button plaotform device private data structure */ struct s3c_button_platform_data //platform总线的按键结构体 { struct s3c_button_info *buttons; //用来访问按键硬件信息的指针 int nbuttons; //按键数量 }; /* Button hardware informtation data*/ static struct s3c_button_info s3c_buttons[] = { //保存每个按键的信息 [0] = { .num = 1, .name = "KEY1", .nIRQ = IRQ_EINT0, .gpio = S3C2410_GPF(0), .setting = S3C2410_GPF0_EINT0, }, [1] = { .num = 2, .name = "KEY2", .nIRQ = IRQ_EINT2, .gpio = S3C2410_GPF(2), .setting = S3C2410_GPF2_EINT2, }, [2] = { .num = 3, .name = "KEY3", .nIRQ = IRQ_EINT3, .gpio = S3C2410_GPF(3), .setting = S3C2410_GPF3_EINT3, }, [3] = { .num = 4, .name = "KEY4", .nIRQ = IRQ_EINT4, .gpio = S3C2410_GPF(4), .setting = S3C2410_GPF4_EINT4, }, }; /* The button platform device private data */ static struct s3c_button_platform_data s3c_button_data = { //定义button的结构体信息 .buttons = s3c_buttons, //每个button信息 .nbuttons = ARRAY_SIZE(s3c_buttons), //button数量 }; struct button_device //定义一个button_device的结构体 { unsigned char *status; /* The buttons Push down or up status */ struct s3c_button_platform_data *data; /* The buttons hardware information data */ struct timer_list *timers; /* The buttons remove dithering timers 去抖定时器*/ wait_queue_head_t waitq; /* Wait queue for poll() */ volatile int ev_press; /* Button pressed event 标识,判断在读取设备时是否有数据*/ struct cdev cdev; struct class *dev_class; } button_device; static void platform_button_release(struct device * dev) { return; } static struct platform_device s3c_button_device = { //设备节点结构体 .name = "s3c_button", .id = 1, .dev = { .platform_data = &s3c_button_data, .release = platform_button_release, }, }; static irqreturn_t s3c_button_intterupt(int irq,void *de_id) //中断处理程序 { int i; int found = 0; struct s3c_button_platform_data *pdata = button_device.data; for(i=0; i<pdata->nbuttons; i++) { if(irq == pdata->buttons[i].nIRQ) //按键有下降沿 { found = 1; break; } } if(!found) /* An ERROR interrupt */ return IRQ_NONE; /* Only when button is up then we will handle this event */ if(BUTTON_UP == button_device.status[i]) { button_device.status[i] = BUTTON_UNCERTAIN;//设置当前按键状态为不确定 mod_timer(&(button_device.timers[i]), jiffies+TIMER_DELAY_DOWN);//设置当前按键按下去抖定时器的延时并启动定时器 } return IRQ_HANDLED; } static void button_timer_handler(unsigned long data) { struct s3c_button_platform_data *pdata = button_device.data; int num =(int)data; int status = s3c2410_gpio_getpin( pdata->buttons[num].gpio );//获取当前按键引脚上的电平值来判断按键是按下还是抬起 if(LOWLEVEL == status) { if(BUTTON_UNCERTAIN == button_device.status[num]) /* Come from interrupt */ { //dbg_print("Key pressed!\n"); button_device.status[num] = BUTTON_DOWN;//当前按键按下 printk("%s pressed.\n", pdata->buttons[num].name); /* Wake up the wait queue for read()/poll() */ button_device.ev_press = 1; //设置ev_press为1 wake_up_interruptible(&(button_device.waitq));//如果ev_press为0,则进程进入休眠状态,为1则响应中断,唤醒等待队列让设备进行读取 } /* Cancel the dithering */ mod_timer(&(button_device.timers[num]), jiffies+TIMER_DELAY_UP);//去抖 } else { //dbg_print("Key Released!\n"); button_device.status[num] = BUTTON_UP; // enable_irq(pdata->buttons[num].nIRQ); } return ; } /*===================== Button device driver part ===========================*/ static int button_open(struct inode *inode, struct file *file)//打开按键 { struct button_device *pdev ; struct s3c_button_platform_data *pdata; int i, result; pdev = container_of(inode->i_cdev,struct button_device, cdev); //由i_cdev找到cdev结构体 pdata = pdev->data; file->private_data = pdev; /* Malloc for all the buttons remove dithering timer */ pdev->timers = (struct timer_list *) kmalloc(pdata->nbuttons*sizeof(struct timer_list), GFP_KERNEL);//在cdev结构中给timer分配内存 if(NULL == pdev->timers) { printk("Alloc %s driver for timers failure.\n", DEV_NAME); return -ENOMEM; } memset(pdev->timers, 0, pdata->nbuttons*sizeof(struct timer_list));//初始化timer /* Malloc for all the buttons status buffer */ pdev->status = (unsigned char *)kmalloc(pdata->nbuttons*sizeof(unsigned char), GFP_KERNEL);//为button状态分配内存 if(NULL == pdev->status) { printk("Alloc %s driver for status failure.\n", DEV_NAME); result = -ENOMEM; goto ERROR; } memset(pdev->status, 0, pdata->nbuttons*sizeof(unsigned char)); //初始化状态内存 init_waitqueue_head(&(pdev->waitq));//加入等待队列 for(i=0; i<pdata->nbuttons; i++) //初始化button信息 { /* Initialize all the buttons status to UP */ pdev->status[i] = BUTTON_UP; //未按下 /* Initialize all the buttons' remove dithering timer */ setup_timer(&(pdev->timers[i]), button_timer_handler, i);//设置定时器及调用函数 /* Set all the buttons GPIO to EDGE_FALLING interrupt mode */ s3c2410_gpio_cfgpin(pdata->buttons[i].gpio, pdata->buttons[i].setting);//配置成中断模式 irq_set_irq_type(pdata->buttons[i].nIRQ, IRQ_TYPE_EDGE_FALLING);//下降沿触发中断 /* Request for button GPIO pin interrupt */ result = request_irq(pdata->buttons[i].nIRQ, s3c_button_intterupt, IRQF_DISABLED, DEV_NAME, (void *)i);//注册给内核,一旦发生pdata->buttons[i].nIRQ中断号的中断之后,调用s3c_button_intterupt中断处理程序 if( result ) { result = -EBUSY; goto ERROR1; } } return 0; ERROR1: //出错 ,反向退出 kfree((unsigned char *)pdev->status); while(--i) { disable_irq(pdata->buttons[i].nIRQ); free_irq(pdata->buttons[i].nIRQ, (void *)i); } ERROR: kfree(pdev->timers); return result; } static int button_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) //读按键信息函数 { struct button_device *pdev = file->private_data; struct s3c_button_platform_data *pdata; int i, ret; unsigned int status = 0; pdata = pdev->data; dbg_print("ev_press: %d\n", pdev->ev_press); if(!pdev->ev_press) //按键没有按下 { if(file->f_flags & O_NONBLOCK) //如果是非阻塞模式则返回EAGAIN { dbg_print("read() without block mode.\n"); return -EAGAIN; } else//阻塞模式加入等待队列 { /* Read() will be blocked here */ dbg_print("read() blocked here now.\n"); wait_event_interruptible(pdev->waitq, pdev->ev_press); //加入等待队列 } } pdev->ev_press = 0; //将按键设置为未按下 for(i=0; i<pdata->nbuttons; i++)//读出按键状态并保存在status中 { dbg_print("button[%d] status=%d\n", i, pdev->status[i]); status |= (pdev->status[i]<<i); } ret = copy_to_user(buf, (void *)&status, min(sizeof(status), count));//按键信息发给用户空间 return ret ? -EFAULT : min(sizeof(status), count); } static unsigned int button_poll(struct file *file, poll_table * wait) { struct button_device *pdev = file->private_data; unsigned int mask = 0; poll_wait(file, &(pdev->waitq), wait); if(pdev->ev_press) { mask |= POLLIN | POLLRDNORM; /* The data aviable */ } return mask; } static int button_release(struct inode *inode, struct file *file) { int i; struct button_device *pdev = file->private_data; struct s3c_button_platform_data *pdata; pdata = pdev->data; for(i=0; i<pdata->nbuttons; i++) { disable_irq(pdata->buttons[i].nIRQ);//关闭中断 free_irq(pdata->buttons[i].nIRQ, (void *)i);//删除中断 del_timer(&(pdev->timers[i])); //关timer } kfree(pdev->timers);//释放timer内存 kfree((unsigned char *)pdev->status);//释放申请的status内存 return 0; } static struct file_operations button_fops = { //fops .owner = THIS_MODULE, .open = button_open, .read = button_read, .poll = button_poll, .release = button_release, }; static int s3c_button_probe(struct platform_device *dev) { int result = 0; dev_t devno; /* Alloc the device for driver */ if (0 != dev_major) { devno = MKDEV(dev_major, dev_minor); result = register_chrdev_region(devno, 1, DEV_NAME); } else { result = alloc_chrdev_region(&devno, dev_minor, 1, DEV_NAME); dev_major = MAJOR(devno); } /* Alloc for device major failure */ if (result < 0) { printk("%s driver can't get major %d\n", DEV_NAME, dev_major); return result; } /* Initialize button_device structure and register cdev*/ memset(&button_device, 0, sizeof(button_device)); button_device.data = dev->dev.platform_data; cdev_init (&(button_device.cdev), &button_fops); button_device.cdev.owner = THIS_MODULE; result = cdev_add (&(button_device.cdev), devno , 1); if (result) { printk (KERN_NOTICE "error %d add %s device", result, DEV_NAME); goto ERROR; } button_device.dev_class = class_create(THIS_MODULE, DEV_NAME); if(IS_ERR(button_device.dev_class)) { printk("%s driver create class failture\n",DEV_NAME); result = -ENOMEM; goto ERROR; } #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24) device_create(button_device.dev_class, NULL, devno, NULL, DEV_NAME); #else device_create (button_device.dev_class, NULL, devno, DEV_NAME); #endif printk("S3C %s driver version %d.%d.%d initiliazed.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER); return 0; ERROR: printk("S3C %s driver version %d.%d.%d install failure.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER); cdev_del(&(button_device.cdev)); unregister_chrdev_region(devno, 1); return result; } static int s3c_button_remove(struct platform_device *dev) { dev_t devno = MKDEV(dev_major, dev_minor); cdev_del(&(button_device.cdev)); device_destroy(button_device.dev_class, devno); class_destroy(button_device.dev_class); unregister_chrdev_region(devno, 1); printk("S3C %s driver removed\n", DEV_NAME); return 0; } /*===================== Platform Device and driver regist part ===========================*/ static struct platform_driver s3c_button_driver = { .probe = s3c_button_probe, .remove = s3c_button_remove, .driver = { .name = "s3c_button", .owner = THIS_MODULE, }, }; static int __init s3c_button_init(void) { int ret = 0; ret = platform_device_register(&s3c_button_device); if(ret) { printk(KERN_ERR "%s: Can't register platform device %d\n", __FUNCTION__, ret); goto fail_reg_plat_dev; } dbg_print("Regist S3C %s Device successfully.\n", DEV_NAME); ret = platform_driver_register(&s3c_button_driver); if(ret) { printk(KERN_ERR "%s: Can't register platform driver %d\n", __FUNCTION__, ret); goto fail_reg_plat_drv; } dbg_print("Regist S3C %s Driver successfully.\n", DEV_NAME); return 0; fail_reg_plat_drv: platform_driver_unregister(&s3c_button_driver); fail_reg_plat_dev: return ret; } static void s3c_button_exit(void) { platform_driver_unregister(&s3c_button_driver); dbg_print("S3C %s platform device removed.\n", DEV_NAME); platform_device_unregister(&s3c_button_device); dbg_print("S3C %s platform driver removed.\n", DEV_NAME); } module_init(s3c_button_init); module_exit(s3c_button_exit); module_param(debug, int, S_IRUGO); module_param(dev_major, int, S_IRUGO); module_param(dev_minor, int, S_IRUGO); MODULE_AUTHOR(DRV_AUTHOR); MODULE_DESCRIPTION(DRV_DESC); MODULE_LICENSE("GPL"); MODULE_ALIAS("platform:S3C24XX_button");

    小结:对比之前学习的LED驱动,按键驱动多了几个我比较难理解的地方。 1.中断,中断服务程序。2.定时器,去抖动。3.轮询(poll、select)。 这几个重点了解学习下,里面的一些细节还是要多多揣摩。

    转载请注明原文地址: https://ju.6miu.com/read-3976.html

    最新回复(0)