]> Pileus Git - ~andy/linux/blob - drivers/staging/nvec/nvec_kbd.c
Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[~andy/linux] / drivers / staging / nvec / nvec_kbd.c
1 #include <linux/slab.h>
2 #include <linux/input.h>
3 #include <linux/delay.h>
4 #include "nvec-keytable.h"
5 #include "nvec.h"
6
7 #define ACK_KBD_EVENT {'\x05','\xed','\x01'}
8
9 static unsigned char keycodes[ARRAY_SIZE(code_tab_102us)
10                         + ARRAY_SIZE(extcode_tab_us102)];
11
12 struct nvec_keys {
13         struct input_dev *input;
14         struct notifier_block notifier;
15         struct nvec_chip *nvec;
16 };
17
18 static struct nvec_keys keys_dev;
19
20 static int nvec_keys_notifier(struct notifier_block *nb,
21                                 unsigned long event_type, void *data)
22 {
23         int code, state;
24         unsigned char *msg = (unsigned char *)data;
25
26         if (event_type == NVEC_KB_EVT) {
27                 nvec_size _size = (msg[0] & (3 << 5)) >> 5;
28
29 /* power on/off button */
30                 if(_size == NVEC_VAR_SIZE)
31                         return NOTIFY_STOP;
32
33                 if(_size == NVEC_3BYTES)
34                         msg++;
35
36                 code = msg[1] & 0x7f;
37                 state = msg[1] & 0x80;
38
39                 input_report_key(keys_dev.input, code_tabs[_size][code], !state);
40                 input_sync(keys_dev.input);
41
42                 return NOTIFY_STOP;
43         }
44
45         return NOTIFY_DONE;
46 }
47
48 static int nvec_kbd_event(struct input_dev *dev, unsigned int type,
49                                 unsigned int code, int value)
50 {
51         unsigned char buf[] = ACK_KBD_EVENT;
52         struct nvec_chip *nvec = keys_dev.nvec;
53
54         if(type==EV_REP)
55                 return 0;
56
57         if(type!=EV_LED)
58                 return -1;
59
60         if(code!=LED_CAPSL)
61                 return -1;
62
63         buf[2] = !!value;
64         nvec_write_async(nvec, buf, sizeof(buf));
65
66         return 0;
67 }
68
69 int __init nvec_kbd_init(struct nvec_chip *nvec)
70 {
71         int i, j, err;
72         struct input_dev *idev;
73
74         j = 0;
75
76         for(i = 0; i < ARRAY_SIZE(code_tab_102us); ++i)
77                 keycodes[j++] = code_tab_102us[i];
78
79         for(i = 0; i < ARRAY_SIZE(extcode_tab_us102); ++i)
80                 keycodes[j++]=extcode_tab_us102[i];
81
82         idev = input_allocate_device();
83         idev->name = "Tegra nvec keyboard";
84         idev->phys = "i2c3_slave/nvec";
85         idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_LED);
86         idev->ledbit[0] = BIT_MASK(LED_CAPSL);
87         idev->event = nvec_kbd_event;
88         idev->keycode = keycodes;
89         idev->keycodesize = sizeof(unsigned char);
90         idev->keycodemax = ARRAY_SIZE(keycodes);
91
92         for( i = 0; i < ARRAY_SIZE(keycodes); ++i)
93                 set_bit(keycodes[i], idev->keybit);
94
95         clear_bit(0, idev->keybit);
96         err = input_register_device(idev);
97         if(err)
98                 goto fail;
99
100         keys_dev.input = idev;
101         keys_dev.notifier.notifier_call = nvec_keys_notifier;
102         keys_dev.nvec = nvec;
103         nvec_register_notifier(nvec, &keys_dev.notifier, 0);
104
105         /* Enable keyboard */
106         nvec_write_async(nvec, "\x05\xf4", 2);
107
108         /* keyboard reset? */
109         nvec_write_async(nvec, "\x05\x03\x01\x01", 4);
110         nvec_write_async(nvec, "\x05\x04\x01", 3);
111         nvec_write_async(nvec, "\x06\x01\xff\x03", 4);
112 /*      FIXME
113         wait until keyboard reset is finished
114         or until we have a sync write */
115         mdelay(1000);
116
117         return 0;
118
119 fail:
120         input_free_device(idev);
121         return err;
122 }