Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / nvec / nvec_kbd.c
blob9a9850725b5b06c127d3d698c6d1f56e8d9bd12b
1 #include <linux/slab.h>
2 #include <linux/input.h>
3 #include <linux/delay.h>
4 #include "nvec-keytable.h"
5 #include "nvec.h"
7 #define ACK_KBD_EVENT {'\x05','\xed','\x01'}
9 static unsigned char keycodes[ARRAY_SIZE(code_tab_102us)
10 + ARRAY_SIZE(extcode_tab_us102)];
12 struct nvec_keys {
13 struct input_dev *input;
14 struct notifier_block notifier;
15 struct nvec_chip *nvec;
18 static struct nvec_keys keys_dev;
20 static int nvec_keys_notifier(struct notifier_block *nb,
21 unsigned long event_type, void *data)
23 int code, state;
24 unsigned char *msg = (unsigned char *)data;
26 if (event_type == NVEC_KB_EVT) {
27 nvec_size _size = (msg[0] & (3 << 5)) >> 5;
29 /* power on/off button */
30 if(_size == NVEC_VAR_SIZE)
31 return NOTIFY_STOP;
33 if(_size == NVEC_3BYTES)
34 msg++;
36 code = msg[1] & 0x7f;
37 state = msg[1] & 0x80;
39 input_report_key(keys_dev.input, code_tabs[_size][code], !state);
40 input_sync(keys_dev.input);
42 return NOTIFY_STOP;
45 return NOTIFY_DONE;
48 static int nvec_kbd_event(struct input_dev *dev, unsigned int type,
49 unsigned int code, int value)
51 unsigned char buf[] = ACK_KBD_EVENT;
52 struct nvec_chip *nvec = keys_dev.nvec;
54 if(type==EV_REP)
55 return 0;
57 if(type!=EV_LED)
58 return -1;
60 if(code!=LED_CAPSL)
61 return -1;
63 buf[2] = !!value;
64 nvec_write_async(nvec, buf, sizeof(buf));
66 return 0;
69 int __init nvec_kbd_init(struct nvec_chip *nvec)
71 int i, j, err;
72 struct input_dev *idev;
74 j = 0;
76 for(i = 0; i < ARRAY_SIZE(code_tab_102us); ++i)
77 keycodes[j++] = code_tab_102us[i];
79 for(i = 0; i < ARRAY_SIZE(extcode_tab_us102); ++i)
80 keycodes[j++]=extcode_tab_us102[i];
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);
92 for( i = 0; i < ARRAY_SIZE(keycodes); ++i)
93 set_bit(keycodes[i], idev->keybit);
95 clear_bit(0, idev->keybit);
96 err = input_register_device(idev);
97 if(err)
98 goto fail;
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);
105 /* Enable keyboard */
106 nvec_write_async(nvec, "\x05\xf4", 2);
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);
117 return 0;
119 fail:
120 input_free_device(idev);
121 return err;