问: 为何要基于input子系统??
答: 因为linux下所有GUI都可以接收input子系统的消息。写基于GUI的程序时可以用GUI实现的按键机制,而不是单片机的思维,一个死循环来处理用户输入。
不扯淡了,一切尽在源码中!
硬件平台: s3c2440
软件平台: linux-3.1.6 + qt-4.7.4
此驱动源码以GPL协议开放.- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/interrupt.h>
- #include <linux/platform_device.h>
- #include <linux/input.h>
- #include <linux/slab.h>
- #include <mach/regs-gpio.h>
- #include <linux/gpio.h>
- #include <linux/irq.h>
- #define DHOLE2440_KBD "dhole2440kbd"
- #define DHOLE2440_KBD_IRQ_NUM (6)
- #define KBD_NONE (0xff)
- #define KBD_UP (0)
- #define KBD_DOWN (1)
- typedef struct _dhole2440_key{
- unsigned int gpio;/*对应gpio口*/
- unsigned int irq;/*对应中断*/
- int n_key;/*键值*/
- }dhole2440_key;
- struct dhole2440_kbd{
- dhole2440_key keys[DHOLE2440_KBD_IRQ_NUM];
- struct timer_list key_timer; /*按键去抖定时器*/
- unsigned int key_status; /*按键状态*/
- struct input_dev *input;
- };
- struct dhole2440_kbd *p_dhole2440_kbd;
- struct dhole2440_kbd *get_kbd(void)
- {
- printk("get_kbd p_dhole2440_kbd=%x\n", (unsigned int)p_dhole2440_kbd);
- return p_dhole2440_kbd;
- }
- void set_kbd(struct dhole2440_kbd *p_kbd)
- {
- p_dhole2440_kbd = p_kbd;
- printk("set_kbd p_kbd=%x, p_dhole2440_kbd=%x\n",
- (unsigned int)p_kbd, (unsigned int)p_dhole2440_kbd);
- }
- static irqreturn_t dhole2440_kbd_handler(int irq, void *p_date)
- {
- unsigned int n_key = 0;
- struct dhole2440_kbd *p_kbd = p_date;
- unsigned int key_state = 0;
- int i;
- for(i = 0; i < DHOLE2440_KBD_IRQ_NUM; i++)
- {
- if( irq == p_kbd->keys[i].irq )
- {
- key_state = s3c2410_gpio_getpin(p_kbd->keys[i].gpio);
- n_key = p_kbd->keys[i].n_key;
- break;
- }
- }
- printk("dhole2440_kbd_handler n_key=%d, key_state=%d\n", n_key, key_state);
- input_report_key(p_kbd->input, n_key, !key_state);/*1表示按下*/
- input_sync(p_kbd->input);
- return IRQ_HANDLED;
- }
- static void kbd_free_irqs(void)
- {
- struct dhole2440_kbd *p_kbd = get_kbd();
- int i;
- printk("kbd_free_irqs p_kbd=%x\n", (unsigned int)p_kbd);
-
- for(i = 0; i < DHOLE2440_KBD_IRQ_NUM; i++)
- free_irq(p_kbd->keys[i].irq, p_kbd);
- }
- static int kbd_req_irqs(void)
- {
- int n_ret;
- int i;
- struct dhole2440_kbd *p_kbd = get_kbd();
- printk("kbd_req_irqs p_kbd=%x\n", (unsigned int)p_kbd);
- for(i = 0; i < DHOLE2440_KBD_IRQ_NUM; i++)
- {
- n_ret = request_irq(p_kbd->keys[i].irq, dhole2440_kbd_handler, IRQ_TYPE_EDGE_BOTH, DHOLE2440_KBD, p_kbd);
- if(n_ret)
- {
- printk("%d: could not register interrupt\n", p_kbd->keys[i].irq);
- goto fail;
- }
- }
- return n_ret;
- fail:
- /*因为上面申请失败的那个没有成功,所以也不要释放*/
- for(i--; i >= 0; i--)
- {
- disable_irq(p_kbd->keys[i].irq);
- free_irq(p_kbd->keys[i].irq, p_kbd);
- }
- return n_ret;
- }
- static void dhole2440_init_kbd_data(struct dhole2440_kbd *p_kbd)
- {
- printk("dhole2440_init_kbd_data p_kbd=%x\n", (unsigned int)p_kbd);
- p_kbd->keys[0].gpio = S3C2410_GPG(11);
- p_kbd->keys[1].gpio = S3C2410_GPG(7);
- p_kbd->keys[2].gpio = S3C2410_GPG(6);
- p_kbd->keys[3].gpio = S3C2410_GPG(5);
- p_kbd->keys[4].gpio = S3C2410_GPG(3);
- p_kbd->keys[5].gpio = S3C2410_GPG(0);
- p_kbd->keys[0].irq = IRQ_EINT19;
- p_kbd->keys[1].irq = IRQ_EINT15;
- p_kbd->keys[2].irq = IRQ_EINT14;
- p_kbd->keys[3].irq = IRQ_EINT13;
- p_kbd->keys[4].irq = IRQ_EINT11;
- p_kbd->keys[5].irq = IRQ_EINT8;
- p_kbd->keys[0].n_key = KEY_0;
- p_kbd->keys[1].n_key = KEY_1;
- p_kbd->keys[2].n_key = KEY_2;
- p_kbd->keys[3].n_key = KEY_3;
- p_kbd->keys[4].n_key = KEY_ESC;
- p_kbd->keys[5].n_key = KEY_ENTER;
- }
- static int __devinit dhole2440_keys_probe(struct platform_device *pdev)
- {
- int err = -ENOMEM;
- struct dhole2440_kbd *p_dhole2440_keys = NULL;
- struct input_dev *input_dev = NULL;
- printk("dhole2440_keys_probe entry!\n");
- p_dhole2440_keys = kmalloc(sizeof(struct dhole2440_kbd), GFP_KERNEL);
- if( !p_dhole2440_keys )
- {
- printk("dhole2440_keys_probe kmalloc error!\n");
- return err;
- }
- printk("dhole2440_keys_probe p_dhole2440_keys=%x\n", (unsigned int)p_dhole2440_keys);
- dhole2440_init_kbd_data(p_dhole2440_keys);
- input_dev = input_allocate_device();
- if (!input_dev)
- {
- printk("dhole2440_keys_probe input_allocate_device error!\n");
- goto fail;
- }
- p_dhole2440_keys->input = input_dev;
- platform_set_drvdata(pdev, p_dhole2440_keys);
- input_dev->name = pdev->name;
- input_dev->phys = DHOLE2440_KBD"/input0";
- input_dev->id.bustype = BUS_HOST;
- input_dev->dev.parent = &pdev->dev;
- input_dev->id.vendor = 0x0001;
- input_dev->id.product = 0x0001;
- input_dev->id.version = 0x0100;
- __set_bit(EV_KEY, input_dev->evbit);
- __set_bit(KEY_0, input_dev->keybit);
- __set_bit(KEY_1, input_dev->keybit);
- __set_bit(KEY_2, input_dev->keybit);
- __set_bit(KEY_3, input_dev->keybit);
- __set_bit(KEY_ESC, input_dev->keybit);
- __set_bit(KEY_ENTER, input_dev->keybit);
- input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
- err = input_register_device(input_dev);
- if( err )
- {
- printk("dhole2440_keys_probe input_register_device error!\n");
- goto fail_allocate;
- }
- set_kbd(p_dhole2440_keys);
- err = kbd_req_irqs();
- if( err )
- {
- printk("dhole2440_keys_probe kbd_req_irqs error!\n");
- goto fail_register;
- }
- printk("dhole2440_keys_probe sucess!\n");
- return 0;
- fail_register:
- input_unregister_device(input_dev);
- goto fail;
- fail_allocate:
- input_free_device(input_dev);
- fail:
- kfree(p_dhole2440_keys);
- return err;
- }
- static int __devexit dhole2440_keys_remove(struct platform_device *pdev)
- {
- struct dhole2440_kbd *p_dhole2440_keys = platform_get_drvdata(pdev);
- printk("dhole2440_keys_remove entry!\n");
- kbd_free_irqs();
- input_unregister_device(p_dhole2440_keys->input);
- kfree(p_dhole2440_keys);
- printk("dhole2440_keys_remove sucess!\n");
- return 0;
- }
- static void dhole2440_keys_release(struct device *dev)
- {
- dev = dev;
- }
- static struct platform_driver dhole2440_keys_device_driver = {
- .probe = dhole2440_keys_probe,
- .remove = __devexit_p(dhole2440_keys_remove),
- .driver = {
- .name = DHOLE2440_KBD,
- .owner = THIS_MODULE,
- }
- };
- static struct platform_device dhole2440_device_kbd = {
- .name = DHOLE2440_KBD,
- .id = -1,
- .dev = {
- .release = dhole2440_keys_release,
- }
- };
- static int __init dhole2440_keys_init(void)
- {
- int n_ret;
- n_ret = platform_driver_register(&dhole2440_keys_device_driver);
- printk("dhole2440_keys_init 1 n_ret=%d jiffies=%lu,HZ=%d\n", n_ret, jiffies, HZ);
- if( n_ret )
- return n_ret;
- n_ret = platform_device_register(&dhole2440_device_kbd);
- printk("dhole2440_keys_init 2 n_ret=%d\n", n_ret);
- if( n_ret )
- goto fail;
- return n_ret;
- fail:
- platform_driver_unregister(&dhole2440_keys_device_driver);
- return n_ret;
- }
- static void __exit dhole2440_keys_exit(void)
- {
- printk("dhole2440_keys_exit\n");
- platform_device_unregister(&dhole2440_device_kbd);
- platform_driver_unregister(&dhole2440_keys_device_driver);
- }
- module_init(dhole2440_keys_init);
- module_exit(dhole2440_keys_exit);
- MODULE_DESCRIPTION("dhole2440 keyboard input events driver");
- MODULE_AUTHOR("liuzhiping ");
- MODULE_LICENSE("GPL");
- MODULE_ALIAS("platform:dhole2440 keyboard driver");
qt环境变量配置
export set QWS_KEYBOARD="LinuxInput:/dev/input/
event1"
"
event1"可能是event
0,因驱动加载的先后顺序而不同。
以上驱动在给出的软硬件平台达到预期效果->"把键盘融入整个linux生态系统".
代码没整理,而且没实现去抖动。各位就将就这看。
期间参考了很多网上大牛的博客,在此一并感谢!!
又一个小土丘被我征服了,很有成就感,该执行下一步计划了!
但是还得先睡觉。
阅读(8188) | 评论(0) | 转发(1) |