diff options
Diffstat (limited to 'drivers/hid/usbhid/usbkbd.c')
-rw-r--r-- | drivers/hid/usbhid/usbkbd.c | 362 |
1 files changed, 362 insertions, 0 deletions
diff --git a/drivers/hid/usbhid/usbkbd.c b/drivers/hid/usbhid/usbkbd.c new file mode 100644 index 000000000000..3749f4a235f9 --- /dev/null +++ b/drivers/hid/usbhid/usbkbd.c | |||
@@ -0,0 +1,362 @@ | |||
1 | /* | ||
2 | * $Id: usbkbd.c,v 1.27 2001/12/27 10:37:41 vojtech Exp $ | ||
3 | * | ||
4 | * Copyright (c) 1999-2001 Vojtech Pavlik | ||
5 | * | ||
6 | * USB HIDBP Keyboard support | ||
7 | */ | ||
8 | |||
9 | /* | ||
10 | * This program is free software; you can redistribute it and/or modify | ||
11 | * it under the terms of the GNU General Public License as published by | ||
12 | * the Free Software Foundation; either version 2 of the License, or | ||
13 | * (at your option) any later version. | ||
14 | * | ||
15 | * This program is distributed in the hope that it will be useful, | ||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
18 | * GNU General Public License for more details. | ||
19 | * | ||
20 | * You should have received a copy of the GNU General Public License | ||
21 | * along with this program; if not, write to the Free Software | ||
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
23 | * | ||
24 | * Should you need to contact me, the author, you can do so either by | ||
25 | * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: | ||
26 | * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic | ||
27 | */ | ||
28 | |||
29 | #include <linux/kernel.h> | ||
30 | #include <linux/slab.h> | ||
31 | #include <linux/module.h> | ||
32 | #include <linux/init.h> | ||
33 | #include <linux/usb/input.h> | ||
34 | #include <linux/hid.h> | ||
35 | |||
36 | /* | ||
37 | * Version Information | ||
38 | */ | ||
39 | #define DRIVER_VERSION "" | ||
40 | #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@ucw.cz>" | ||
41 | #define DRIVER_DESC "USB HID Boot Protocol keyboard driver" | ||
42 | #define DRIVER_LICENSE "GPL" | ||
43 | |||
44 | MODULE_AUTHOR(DRIVER_AUTHOR); | ||
45 | MODULE_DESCRIPTION(DRIVER_DESC); | ||
46 | MODULE_LICENSE(DRIVER_LICENSE); | ||
47 | |||
48 | static unsigned char usb_kbd_keycode[256] = { | ||
49 | 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, | ||
50 | 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3, | ||
51 | 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26, | ||
52 | 27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64, | ||
53 | 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106, | ||
54 | 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71, | ||
55 | 72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190, | ||
56 | 191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113, | ||
57 | 115,114, 0, 0, 0,121, 0, 89, 93,124, 92, 94, 95, 0, 0, 0, | ||
58 | 122,123, 90, 91, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
59 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
60 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
61 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
62 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
63 | 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113, | ||
64 | 150,158,159,128,136,177,178,176,142,152,173,140 | ||
65 | }; | ||
66 | |||
67 | struct usb_kbd { | ||
68 | struct input_dev *dev; | ||
69 | struct usb_device *usbdev; | ||
70 | unsigned char old[8]; | ||
71 | struct urb *irq, *led; | ||
72 | unsigned char newleds; | ||
73 | char name[128]; | ||
74 | char phys[64]; | ||
75 | |||
76 | unsigned char *new; | ||
77 | struct usb_ctrlrequest *cr; | ||
78 | unsigned char *leds; | ||
79 | dma_addr_t cr_dma; | ||
80 | dma_addr_t new_dma; | ||
81 | dma_addr_t leds_dma; | ||
82 | }; | ||
83 | |||
84 | static void usb_kbd_irq(struct urb *urb) | ||
85 | { | ||
86 | struct usb_kbd *kbd = urb->context; | ||
87 | int i; | ||
88 | |||
89 | switch (urb->status) { | ||
90 | case 0: /* success */ | ||
91 | break; | ||
92 | case -ECONNRESET: /* unlink */ | ||
93 | case -ENOENT: | ||
94 | case -ESHUTDOWN: | ||
95 | return; | ||
96 | /* -EPIPE: should clear the halt */ | ||
97 | default: /* error */ | ||
98 | goto resubmit; | ||
99 | } | ||
100 | |||
101 | for (i = 0; i < 8; i++) | ||
102 | input_report_key(kbd->dev, usb_kbd_keycode[i + 224], (kbd->new[0] >> i) & 1); | ||
103 | |||
104 | for (i = 2; i < 8; i++) { | ||
105 | |||
106 | if (kbd->old[i] > 3 && memscan(kbd->new + 2, kbd->old[i], 6) == kbd->new + 8) { | ||
107 | if (usb_kbd_keycode[kbd->old[i]]) | ||
108 | input_report_key(kbd->dev, usb_kbd_keycode[kbd->old[i]], 0); | ||
109 | else | ||
110 | info("Unknown key (scancode %#x) released.", kbd->old[i]); | ||
111 | } | ||
112 | |||
113 | if (kbd->new[i] > 3 && memscan(kbd->old + 2, kbd->new[i], 6) == kbd->old + 8) { | ||
114 | if (usb_kbd_keycode[kbd->new[i]]) | ||
115 | input_report_key(kbd->dev, usb_kbd_keycode[kbd->new[i]], 1); | ||
116 | else | ||
117 | info("Unknown key (scancode %#x) pressed.", kbd->new[i]); | ||
118 | } | ||
119 | } | ||
120 | |||
121 | input_sync(kbd->dev); | ||
122 | |||
123 | memcpy(kbd->old, kbd->new, 8); | ||
124 | |||
125 | resubmit: | ||
126 | i = usb_submit_urb (urb, GFP_ATOMIC); | ||
127 | if (i) | ||
128 | err ("can't resubmit intr, %s-%s/input0, status %d", | ||
129 | kbd->usbdev->bus->bus_name, | ||
130 | kbd->usbdev->devpath, i); | ||
131 | } | ||
132 | |||
133 | static int usb_kbd_event(struct input_dev *dev, unsigned int type, | ||
134 | unsigned int code, int value) | ||
135 | { | ||
136 | struct usb_kbd *kbd = dev->private; | ||
137 | |||
138 | if (type != EV_LED) | ||
139 | return -1; | ||
140 | |||
141 | |||
142 | kbd->newleds = (!!test_bit(LED_KANA, dev->led) << 3) | (!!test_bit(LED_COMPOSE, dev->led) << 3) | | ||
143 | (!!test_bit(LED_SCROLLL, dev->led) << 2) | (!!test_bit(LED_CAPSL, dev->led) << 1) | | ||
144 | (!!test_bit(LED_NUML, dev->led)); | ||
145 | |||
146 | if (kbd->led->status == -EINPROGRESS) | ||
147 | return 0; | ||
148 | |||
149 | if (*(kbd->leds) == kbd->newleds) | ||
150 | return 0; | ||
151 | |||
152 | *(kbd->leds) = kbd->newleds; | ||
153 | kbd->led->dev = kbd->usbdev; | ||
154 | if (usb_submit_urb(kbd->led, GFP_ATOMIC)) | ||
155 | err("usb_submit_urb(leds) failed"); | ||
156 | |||
157 | return 0; | ||
158 | } | ||
159 | |||
160 | static void usb_kbd_led(struct urb *urb) | ||
161 | { | ||
162 | struct usb_kbd *kbd = urb->context; | ||
163 | |||
164 | if (urb->status) | ||
165 | warn("led urb status %d received", urb->status); | ||
166 | |||
167 | if (*(kbd->leds) == kbd->newleds) | ||
168 | return; | ||
169 | |||
170 | *(kbd->leds) = kbd->newleds; | ||
171 | kbd->led->dev = kbd->usbdev; | ||
172 | if (usb_submit_urb(kbd->led, GFP_ATOMIC)) | ||
173 | err("usb_submit_urb(leds) failed"); | ||
174 | } | ||
175 | |||
176 | static int usb_kbd_open(struct input_dev *dev) | ||
177 | { | ||
178 | struct usb_kbd *kbd = dev->private; | ||
179 | |||
180 | kbd->irq->dev = kbd->usbdev; | ||
181 | if (usb_submit_urb(kbd->irq, GFP_KERNEL)) | ||
182 | return -EIO; | ||
183 | |||
184 | return 0; | ||
185 | } | ||
186 | |||
187 | static void usb_kbd_close(struct input_dev *dev) | ||
188 | { | ||
189 | struct usb_kbd *kbd = dev->private; | ||
190 | |||
191 | usb_kill_urb(kbd->irq); | ||
192 | } | ||
193 | |||
194 | static int usb_kbd_alloc_mem(struct usb_device *dev, struct usb_kbd *kbd) | ||
195 | { | ||
196 | if (!(kbd->irq = usb_alloc_urb(0, GFP_KERNEL))) | ||
197 | return -1; | ||
198 | if (!(kbd->led = usb_alloc_urb(0, GFP_KERNEL))) | ||
199 | return -1; | ||
200 | if (!(kbd->new = usb_buffer_alloc(dev, 8, GFP_ATOMIC, &kbd->new_dma))) | ||
201 | return -1; | ||
202 | if (!(kbd->cr = usb_buffer_alloc(dev, sizeof(struct usb_ctrlrequest), GFP_ATOMIC, &kbd->cr_dma))) | ||
203 | return -1; | ||
204 | if (!(kbd->leds = usb_buffer_alloc(dev, 1, GFP_ATOMIC, &kbd->leds_dma))) | ||
205 | return -1; | ||
206 | |||
207 | return 0; | ||
208 | } | ||
209 | |||
210 | static void usb_kbd_free_mem(struct usb_device *dev, struct usb_kbd *kbd) | ||
211 | { | ||
212 | usb_free_urb(kbd->irq); | ||
213 | usb_free_urb(kbd->led); | ||
214 | if (kbd->new) | ||
215 | usb_buffer_free(dev, 8, kbd->new, kbd->new_dma); | ||
216 | if (kbd->cr) | ||
217 | usb_buffer_free(dev, sizeof(struct usb_ctrlrequest), kbd->cr, kbd->cr_dma); | ||
218 | if (kbd->leds) | ||
219 | usb_buffer_free(dev, 1, kbd->leds, kbd->leds_dma); | ||
220 | } | ||
221 | |||
222 | static int usb_kbd_probe(struct usb_interface *iface, | ||
223 | const struct usb_device_id *id) | ||
224 | { | ||
225 | struct usb_device *dev = interface_to_usbdev(iface); | ||
226 | struct usb_host_interface *interface; | ||
227 | struct usb_endpoint_descriptor *endpoint; | ||
228 | struct usb_kbd *kbd; | ||
229 | struct input_dev *input_dev; | ||
230 | int i, pipe, maxp; | ||
231 | |||
232 | interface = iface->cur_altsetting; | ||
233 | |||
234 | if (interface->desc.bNumEndpoints != 1) | ||
235 | return -ENODEV; | ||
236 | |||
237 | endpoint = &interface->endpoint[0].desc; | ||
238 | if (!usb_endpoint_is_int_in(endpoint)) | ||
239 | return -ENODEV; | ||
240 | |||
241 | pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress); | ||
242 | maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe)); | ||
243 | |||
244 | kbd = kzalloc(sizeof(struct usb_kbd), GFP_KERNEL); | ||
245 | input_dev = input_allocate_device(); | ||
246 | if (!kbd || !input_dev) | ||
247 | goto fail1; | ||
248 | |||
249 | if (usb_kbd_alloc_mem(dev, kbd)) | ||
250 | goto fail2; | ||
251 | |||
252 | kbd->usbdev = dev; | ||
253 | kbd->dev = input_dev; | ||
254 | |||
255 | if (dev->manufacturer) | ||
256 | strlcpy(kbd->name, dev->manufacturer, sizeof(kbd->name)); | ||
257 | |||
258 | if (dev->product) { | ||
259 | if (dev->manufacturer) | ||
260 | strlcat(kbd->name, " ", sizeof(kbd->name)); | ||
261 | strlcat(kbd->name, dev->product, sizeof(kbd->name)); | ||
262 | } | ||
263 | |||
264 | if (!strlen(kbd->name)) | ||
265 | snprintf(kbd->name, sizeof(kbd->name), | ||
266 | "USB HIDBP Keyboard %04x:%04x", | ||
267 | le16_to_cpu(dev->descriptor.idVendor), | ||
268 | le16_to_cpu(dev->descriptor.idProduct)); | ||
269 | |||
270 | usb_make_path(dev, kbd->phys, sizeof(kbd->phys)); | ||
271 | strlcpy(kbd->phys, "/input0", sizeof(kbd->phys)); | ||
272 | |||
273 | input_dev->name = kbd->name; | ||
274 | input_dev->phys = kbd->phys; | ||
275 | usb_to_input_id(dev, &input_dev->id); | ||
276 | input_dev->cdev.dev = &iface->dev; | ||
277 | input_dev->private = kbd; | ||
278 | |||
279 | input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_REP); | ||
280 | input_dev->ledbit[0] = BIT(LED_NUML) | BIT(LED_CAPSL) | BIT(LED_SCROLLL) | BIT(LED_COMPOSE) | BIT(LED_KANA); | ||
281 | |||
282 | for (i = 0; i < 255; i++) | ||
283 | set_bit(usb_kbd_keycode[i], input_dev->keybit); | ||
284 | clear_bit(0, input_dev->keybit); | ||
285 | |||
286 | input_dev->event = usb_kbd_event; | ||
287 | input_dev->open = usb_kbd_open; | ||
288 | input_dev->close = usb_kbd_close; | ||
289 | |||
290 | usb_fill_int_urb(kbd->irq, dev, pipe, | ||
291 | kbd->new, (maxp > 8 ? 8 : maxp), | ||
292 | usb_kbd_irq, kbd, endpoint->bInterval); | ||
293 | kbd->irq->transfer_dma = kbd->new_dma; | ||
294 | kbd->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | ||
295 | |||
296 | kbd->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE; | ||
297 | kbd->cr->bRequest = 0x09; | ||
298 | kbd->cr->wValue = cpu_to_le16(0x200); | ||
299 | kbd->cr->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber); | ||
300 | kbd->cr->wLength = cpu_to_le16(1); | ||
301 | |||
302 | usb_fill_control_urb(kbd->led, dev, usb_sndctrlpipe(dev, 0), | ||
303 | (void *) kbd->cr, kbd->leds, 1, | ||
304 | usb_kbd_led, kbd); | ||
305 | kbd->led->setup_dma = kbd->cr_dma; | ||
306 | kbd->led->transfer_dma = kbd->leds_dma; | ||
307 | kbd->led->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP); | ||
308 | |||
309 | input_register_device(kbd->dev); | ||
310 | |||
311 | usb_set_intfdata(iface, kbd); | ||
312 | return 0; | ||
313 | |||
314 | fail2: usb_kbd_free_mem(dev, kbd); | ||
315 | fail1: input_free_device(input_dev); | ||
316 | kfree(kbd); | ||
317 | return -ENOMEM; | ||
318 | } | ||
319 | |||
320 | static void usb_kbd_disconnect(struct usb_interface *intf) | ||
321 | { | ||
322 | struct usb_kbd *kbd = usb_get_intfdata (intf); | ||
323 | |||
324 | usb_set_intfdata(intf, NULL); | ||
325 | if (kbd) { | ||
326 | usb_kill_urb(kbd->irq); | ||
327 | input_unregister_device(kbd->dev); | ||
328 | usb_kbd_free_mem(interface_to_usbdev(intf), kbd); | ||
329 | kfree(kbd); | ||
330 | } | ||
331 | } | ||
332 | |||
333 | static struct usb_device_id usb_kbd_id_table [] = { | ||
334 | { USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT, | ||
335 | USB_INTERFACE_PROTOCOL_KEYBOARD) }, | ||
336 | { } /* Terminating entry */ | ||
337 | }; | ||
338 | |||
339 | MODULE_DEVICE_TABLE (usb, usb_kbd_id_table); | ||
340 | |||
341 | static struct usb_driver usb_kbd_driver = { | ||
342 | .name = "usbkbd", | ||
343 | .probe = usb_kbd_probe, | ||
344 | .disconnect = usb_kbd_disconnect, | ||
345 | .id_table = usb_kbd_id_table, | ||
346 | }; | ||
347 | |||
348 | static int __init usb_kbd_init(void) | ||
349 | { | ||
350 | int result = usb_register(&usb_kbd_driver); | ||
351 | if (result == 0) | ||
352 | info(DRIVER_VERSION ":" DRIVER_DESC); | ||
353 | return result; | ||
354 | } | ||
355 | |||
356 | static void __exit usb_kbd_exit(void) | ||
357 | { | ||
358 | usb_deregister(&usb_kbd_driver); | ||
359 | } | ||
360 | |||
361 | module_init(usb_kbd_init); | ||
362 | module_exit(usb_kbd_exit); | ||