blob: 2f3edc26cb5057f2da7eba68e06b1482eb246b30 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050012 * the Free Software Foundation; either version 2 of the License, or
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * (at your option) any later version.
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * 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.
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * 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
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 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/input.h>
33#include <linux/init.h>
34#include <linux/usb.h>
Dmitry Torokhov16a334c2005-06-30 00:49:08 -050035#include <linux/usb_input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
38 * Version Information
39 */
40#define DRIVER_VERSION ""
41#define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@ucw.cz>"
42#define DRIVER_DESC "USB HID Boot Protocol keyboard driver"
43#define DRIVER_LICENSE "GPL"
44
45MODULE_AUTHOR(DRIVER_AUTHOR);
46MODULE_DESCRIPTION(DRIVER_DESC);
47MODULE_LICENSE(DRIVER_LICENSE);
48
49static unsigned char usb_kbd_keycode[256] = {
50 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
51 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
52 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
53 27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
54 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
55 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
56 72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
57 191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
58 115,114, 0, 0, 0,121, 0, 89, 93,124, 92, 94, 95, 0, 0, 0,
59 122,123, 90, 91, 85, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
65 150,158,159,128,136,177,178,176,142,152,173,140
66};
67
68struct usb_kbd {
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050069 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 struct usb_device *usbdev;
71 unsigned char old[8];
72 struct urb *irq, *led;
73 unsigned char newleds;
74 char name[128];
75 char phys[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 unsigned char *new;
78 struct usb_ctrlrequest *cr;
79 unsigned char *leds;
80 dma_addr_t cr_dma;
81 dma_addr_t new_dma;
82 dma_addr_t leds_dma;
83};
84
85static void usb_kbd_irq(struct urb *urb, struct pt_regs *regs)
86{
87 struct usb_kbd *kbd = urb->context;
88 int i;
89
90 switch (urb->status) {
91 case 0: /* success */
92 break;
93 case -ECONNRESET: /* unlink */
94 case -ENOENT:
95 case -ESHUTDOWN:
96 return;
97 /* -EPIPE: should clear the halt */
98 default: /* error */
99 goto resubmit;
100 }
101
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500102 input_regs(kbd->dev, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 for (i = 0; i < 8; i++)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500105 input_report_key(kbd->dev, usb_kbd_keycode[i + 224], (kbd->new[0] >> i) & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 for (i = 2; i < 8; i++) {
108
109 if (kbd->old[i] > 3 && memscan(kbd->new + 2, kbd->old[i], 6) == kbd->new + 8) {
110 if (usb_kbd_keycode[kbd->old[i]])
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500111 input_report_key(kbd->dev, usb_kbd_keycode[kbd->old[i]], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 else
113 info("Unknown key (scancode %#x) released.", kbd->old[i]);
114 }
115
116 if (kbd->new[i] > 3 && memscan(kbd->old + 2, kbd->new[i], 6) == kbd->old + 8) {
117 if (usb_kbd_keycode[kbd->new[i]])
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500118 input_report_key(kbd->dev, usb_kbd_keycode[kbd->new[i]], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 else
120 info("Unknown key (scancode %#x) pressed.", kbd->new[i]);
121 }
122 }
123
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500124 input_sync(kbd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 memcpy(kbd->old, kbd->new, 8);
127
128resubmit:
129 i = usb_submit_urb (urb, SLAB_ATOMIC);
130 if (i)
131 err ("can't resubmit intr, %s-%s/input0, status %d",
132 kbd->usbdev->bus->bus_name,
133 kbd->usbdev->devpath, i);
134}
135
Adrian Bunkbe5e33832005-04-22 15:07:00 -0700136static int usb_kbd_event(struct input_dev *dev, unsigned int type,
137 unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 struct usb_kbd *kbd = dev->private;
140
141 if (type != EV_LED)
142 return -1;
143
144
145 kbd->newleds = (!!test_bit(LED_KANA, dev->led) << 3) | (!!test_bit(LED_COMPOSE, dev->led) << 3) |
146 (!!test_bit(LED_SCROLLL, dev->led) << 2) | (!!test_bit(LED_CAPSL, dev->led) << 1) |
147 (!!test_bit(LED_NUML, dev->led));
148
149 if (kbd->led->status == -EINPROGRESS)
150 return 0;
151
152 if (*(kbd->leds) == kbd->newleds)
153 return 0;
154
155 *(kbd->leds) = kbd->newleds;
156 kbd->led->dev = kbd->usbdev;
157 if (usb_submit_urb(kbd->led, GFP_ATOMIC))
158 err("usb_submit_urb(leds) failed");
159
160 return 0;
161}
162
163static void usb_kbd_led(struct urb *urb, struct pt_regs *regs)
164{
165 struct usb_kbd *kbd = urb->context;
166
167 if (urb->status)
168 warn("led urb status %d received", urb->status);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (*(kbd->leds) == kbd->newleds)
171 return;
172
173 *(kbd->leds) = kbd->newleds;
174 kbd->led->dev = kbd->usbdev;
175 if (usb_submit_urb(kbd->led, GFP_ATOMIC))
176 err("usb_submit_urb(leds) failed");
177}
178
179static int usb_kbd_open(struct input_dev *dev)
180{
181 struct usb_kbd *kbd = dev->private;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 kbd->irq->dev = kbd->usbdev;
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500184 if (usb_submit_urb(kbd->irq, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 return 0;
188}
189
190static void usb_kbd_close(struct input_dev *dev)
191{
192 struct usb_kbd *kbd = dev->private;
193
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500194 usb_kill_urb(kbd->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197static int usb_kbd_alloc_mem(struct usb_device *dev, struct usb_kbd *kbd)
198{
199 if (!(kbd->irq = usb_alloc_urb(0, GFP_KERNEL)))
200 return -1;
201 if (!(kbd->led = usb_alloc_urb(0, GFP_KERNEL)))
202 return -1;
203 if (!(kbd->new = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &kbd->new_dma)))
204 return -1;
205 if (!(kbd->cr = usb_buffer_alloc(dev, sizeof(struct usb_ctrlrequest), SLAB_ATOMIC, &kbd->cr_dma)))
206 return -1;
207 if (!(kbd->leds = usb_buffer_alloc(dev, 1, SLAB_ATOMIC, &kbd->leds_dma)))
208 return -1;
209
210 return 0;
211}
212
213static void usb_kbd_free_mem(struct usb_device *dev, struct usb_kbd *kbd)
214{
215 if (kbd->irq)
216 usb_free_urb(kbd->irq);
217 if (kbd->led)
218 usb_free_urb(kbd->led);
219 if (kbd->new)
220 usb_buffer_free(dev, 8, kbd->new, kbd->new_dma);
221 if (kbd->cr)
222 usb_buffer_free(dev, sizeof(struct usb_ctrlrequest), kbd->cr, kbd->cr_dma);
223 if (kbd->leds)
224 usb_buffer_free(dev, 1, kbd->leds, kbd->leds_dma);
225}
226
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500227static int usb_kbd_probe(struct usb_interface *iface,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 const struct usb_device_id *id)
229{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500230 struct usb_device *dev = interface_to_usbdev(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct usb_host_interface *interface;
232 struct usb_endpoint_descriptor *endpoint;
233 struct usb_kbd *kbd;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500234 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 int i, pipe, maxp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 interface = iface->cur_altsetting;
238
239 if (interface->desc.bNumEndpoints != 1)
240 return -ENODEV;
241
242 endpoint = &interface->endpoint[0].desc;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500243 if (!(endpoint->bEndpointAddress & USB_DIR_IN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return -ENODEV;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500245 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return -ENODEV;
247
248 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
249 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
250
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500251 kbd = kzalloc(sizeof(struct usb_kbd), GFP_KERNEL);
252 input_dev = input_allocate_device();
253 if (!kbd || !input_dev)
254 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500256 if (usb_kbd_alloc_mem(dev, kbd))
257 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 kbd->usbdev = dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500260 kbd->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500262 if (dev->manufacturer)
263 strlcpy(kbd->name, dev->manufacturer, sizeof(kbd->name));
264
265 if (dev->product) {
266 if (dev->manufacturer)
267 strlcat(kbd->name, " ", sizeof(kbd->name));
268 strlcat(kbd->name, dev->product, sizeof(kbd->name));
269 }
270
271 if (!strlen(kbd->name))
272 snprintf(kbd->name, sizeof(kbd->name),
273 "USB HIDBP Keyboard %04x:%04x",
274 le16_to_cpu(dev->descriptor.idVendor),
275 le16_to_cpu(dev->descriptor.idProduct));
276
277 usb_make_path(dev, kbd->phys, sizeof(kbd->phys));
278 strlcpy(kbd->phys, "/input0", sizeof(kbd->phys));
279
280 input_dev->name = kbd->name;
281 input_dev->phys = kbd->phys;
282 usb_to_input_id(dev, &input_dev->id);
283 input_dev->cdev.dev = &iface->dev;
284 input_dev->private = kbd;
285
286 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_REP);
287 input_dev->ledbit[0] = BIT(LED_NUML) | BIT(LED_CAPSL) | BIT(LED_SCROLLL) | BIT(LED_COMPOSE) | BIT(LED_KANA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 for (i = 0; i < 255; i++)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500290 set_bit(usb_kbd_keycode[i], input_dev->keybit);
291 clear_bit(0, input_dev->keybit);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500292
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500293 input_dev->event = usb_kbd_event;
294 input_dev->open = usb_kbd_open;
295 input_dev->close = usb_kbd_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 usb_fill_int_urb(kbd->irq, dev, pipe,
298 kbd->new, (maxp > 8 ? 8 : maxp),
299 usb_kbd_irq, kbd, endpoint->bInterval);
300 kbd->irq->transfer_dma = kbd->new_dma;
301 kbd->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
302
303 kbd->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
304 kbd->cr->bRequest = 0x09;
305 kbd->cr->wValue = cpu_to_le16(0x200);
306 kbd->cr->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber);
307 kbd->cr->wLength = cpu_to_le16(1);
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 usb_fill_control_urb(kbd->led, dev, usb_sndctrlpipe(dev, 0),
310 (void *) kbd->cr, kbd->leds, 1,
311 usb_kbd_led, kbd);
312 kbd->led->setup_dma = kbd->cr_dma;
313 kbd->led->transfer_dma = kbd->leds_dma;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500314 kbd->led->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500316 input_register_device(kbd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 usb_set_intfdata(iface, kbd);
319 return 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500320
321fail2: usb_kbd_free_mem(dev, kbd);
322fail1: input_free_device(input_dev);
323 kfree(kbd);
324 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327static void usb_kbd_disconnect(struct usb_interface *intf)
328{
329 struct usb_kbd *kbd = usb_get_intfdata (intf);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 usb_set_intfdata(intf, NULL);
332 if (kbd) {
333 usb_kill_urb(kbd->irq);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500334 input_unregister_device(kbd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 usb_kbd_free_mem(interface_to_usbdev(intf), kbd);
336 kfree(kbd);
337 }
338}
339
340static struct usb_device_id usb_kbd_id_table [] = {
341 { USB_INTERFACE_INFO(3, 1, 1) },
342 { } /* Terminating entry */
343};
344
345MODULE_DEVICE_TABLE (usb, usb_kbd_id_table);
346
347static struct usb_driver usb_kbd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 .name = "usbkbd",
349 .probe = usb_kbd_probe,
350 .disconnect = usb_kbd_disconnect,
351 .id_table = usb_kbd_id_table,
352};
353
354static int __init usb_kbd_init(void)
355{
356 int result = usb_register(&usb_kbd_driver);
357 if (result == 0)
358 info(DRIVER_VERSION ":" DRIVER_DESC);
359 return result;
360}
361
362static void __exit usb_kbd_exit(void)
363{
364 usb_deregister(&usb_kbd_driver);
365}
366
367module_init(usb_kbd_init);
368module_exit(usb_kbd_exit);