Rev 846 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
846 | giacomo | 1 | /* |
2 | * $Id: usbkbd.c,v 1.1 2004-09-13 15:04:47 giacomo 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/input.h> |
||
33 | #include <linux/init.h> |
||
34 | #include <linux/usb.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, 84, 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, 85, 89, 90, 91, 92, 93, 94, 95, |
||
56 | 120,121,122,123,134,138,130,132,128,129,131,137,133,135,136,113, |
||
57 | 115,114, 0, 0, 0,124, 0,181,182,183,184,185,186,187,188,189, |
||
58 | 190,191,192,193,194,195,196,197,198, 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 | int open; |
||
76 | |||
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 | |||
85 | static 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 | |||
102 | input_regs(&kbd->dev, regs); |
||
103 | |||
104 | for (i = 0; i < 8; i++) |
||
105 | input_report_key(&kbd->dev, usb_kbd_keycode[i + 224], (kbd->new[0] >> i) & 1); |
||
106 | |||
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]]) |
||
111 | input_report_key(&kbd->dev, usb_kbd_keycode[kbd->old[i]], 0); |
||
112 | 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]]) |
||
118 | input_report_key(&kbd->dev, usb_kbd_keycode[kbd->new[i]], 1); |
||
119 | else |
||
120 | info("Unknown key (scancode %#x) pressed.", kbd->new[i]); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | input_sync(&kbd->dev); |
||
125 | |||
126 | memcpy(kbd->old, kbd->new, 8); |
||
127 | |||
128 | resubmit: |
||
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 | |||
136 | int usb_kbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
||
137 | { |
||
138 | struct usb_kbd *kbd = dev->private; |
||
139 | |||
140 | if (type != EV_LED) |
||
141 | return -1; |
||
142 | |||
143 | |||
144 | kbd->newleds = (!!test_bit(LED_KANA, dev->led) << 3) | (!!test_bit(LED_COMPOSE, dev->led) << 3) | |
||
145 | (!!test_bit(LED_SCROLLL, dev->led) << 2) | (!!test_bit(LED_CAPSL, dev->led) << 1) | |
||
146 | (!!test_bit(LED_NUML, dev->led)); |
||
147 | |||
148 | if (kbd->led->status == -EINPROGRESS) |
||
149 | return 0; |
||
150 | |||
151 | if (*(kbd->leds) == kbd->newleds) |
||
152 | return 0; |
||
153 | |||
154 | *(kbd->leds) = kbd->newleds; |
||
155 | kbd->led->dev = kbd->usbdev; |
||
156 | if (usb_submit_urb(kbd->led, GFP_ATOMIC)) |
||
157 | err("usb_submit_urb(leds) failed"); |
||
158 | |||
159 | return 0; |
||
160 | } |
||
161 | |||
162 | static void usb_kbd_led(struct urb *urb, struct pt_regs *regs) |
||
163 | { |
||
164 | struct usb_kbd *kbd = urb->context; |
||
165 | |||
166 | if (urb->status) |
||
167 | warn("led urb status %d received", urb->status); |
||
168 | |||
169 | if (*(kbd->leds) == kbd->newleds) |
||
170 | return; |
||
171 | |||
172 | *(kbd->leds) = kbd->newleds; |
||
173 | kbd->led->dev = kbd->usbdev; |
||
174 | if (usb_submit_urb(kbd->led, GFP_ATOMIC)) |
||
175 | err("usb_submit_urb(leds) failed"); |
||
176 | } |
||
177 | |||
178 | static int usb_kbd_open(struct input_dev *dev) |
||
179 | { |
||
180 | struct usb_kbd *kbd = dev->private; |
||
181 | |||
182 | if (kbd->open++) |
||
183 | return 0; |
||
184 | |||
185 | kbd->irq->dev = kbd->usbdev; |
||
186 | if (usb_submit_urb(kbd->irq, GFP_KERNEL)) { |
||
187 | kbd->open--; |
||
188 | return -EIO; |
||
189 | } |
||
190 | |||
191 | return 0; |
||
192 | } |
||
193 | |||
194 | static void usb_kbd_close(struct input_dev *dev) |
||
195 | { |
||
196 | struct usb_kbd *kbd = dev->private; |
||
197 | |||
198 | if (!--kbd->open) |
||
199 | usb_unlink_urb(kbd->irq); |
||
200 | } |
||
201 | |||
202 | static int usb_kbd_alloc_mem(struct usb_device *dev, struct usb_kbd *kbd) |
||
203 | { |
||
204 | if (!(kbd->irq = usb_alloc_urb(0, GFP_KERNEL))) |
||
205 | return -1; |
||
206 | if (!(kbd->led = usb_alloc_urb(0, GFP_KERNEL))) |
||
207 | return -1; |
||
208 | if (!(kbd->new = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &kbd->new_dma))) |
||
209 | return -1; |
||
210 | if (!(kbd->cr = usb_buffer_alloc(dev, sizeof(struct usb_ctrlrequest), SLAB_ATOMIC, &kbd->cr_dma))) |
||
211 | return -1; |
||
212 | if (!(kbd->leds = usb_buffer_alloc(dev, 1, SLAB_ATOMIC, &kbd->leds_dma))) |
||
213 | return -1; |
||
214 | |||
215 | return 0; |
||
216 | } |
||
217 | |||
218 | static void usb_kbd_free_mem(struct usb_device *dev, struct usb_kbd *kbd) |
||
219 | { |
||
220 | if (kbd->irq) |
||
221 | usb_free_urb(kbd->irq); |
||
222 | if (kbd->led) |
||
223 | usb_free_urb(kbd->led); |
||
224 | if (kbd->new) |
||
225 | usb_buffer_free(dev, 8, kbd->new, kbd->new_dma); |
||
226 | if (kbd->cr) |
||
227 | usb_buffer_free(dev, sizeof(struct usb_ctrlrequest), kbd->cr, kbd->cr_dma); |
||
228 | if (kbd->leds) |
||
229 | usb_buffer_free(dev, 1, kbd->leds, kbd->leds_dma); |
||
230 | } |
||
231 | |||
232 | static int usb_kbd_probe(struct usb_interface *iface, |
||
233 | const struct usb_device_id *id) |
||
234 | { |
||
235 | struct usb_device * dev = interface_to_usbdev(iface); |
||
236 | struct usb_host_interface *interface; |
||
237 | struct usb_endpoint_descriptor *endpoint; |
||
238 | struct usb_kbd *kbd; |
||
239 | int i, pipe, maxp; |
||
240 | char path[64]; |
||
241 | char *buf; |
||
242 | |||
243 | interface = &iface->altsetting[iface->act_altsetting]; |
||
244 | |||
245 | if (interface->desc.bNumEndpoints != 1) |
||
246 | return -ENODEV; |
||
247 | |||
248 | endpoint = &interface->endpoint[0].desc; |
||
249 | if (!(endpoint->bEndpointAddress & 0x80)) |
||
250 | return -ENODEV; |
||
251 | if ((endpoint->bmAttributes & 3) != 3) |
||
252 | return -ENODEV; |
||
253 | |||
254 | pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress); |
||
255 | maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe)); |
||
256 | |||
257 | if (!(kbd = kmalloc(sizeof(struct usb_kbd), GFP_KERNEL))) |
||
258 | return -ENOMEM; |
||
259 | memset(kbd, 0, sizeof(struct usb_kbd)); |
||
260 | |||
261 | if (usb_kbd_alloc_mem(dev, kbd)) { |
||
262 | usb_kbd_free_mem(dev, kbd); |
||
263 | kfree(kbd); |
||
264 | return -ENOMEM; |
||
265 | } |
||
266 | |||
267 | kbd->usbdev = dev; |
||
268 | |||
269 | kbd->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_REP); |
||
270 | kbd->dev.ledbit[0] = BIT(LED_NUML) | BIT(LED_CAPSL) | BIT(LED_SCROLLL) | BIT(LED_COMPOSE) | BIT(LED_KANA); |
||
271 | |||
272 | for (i = 0; i < 255; i++) |
||
273 | set_bit(usb_kbd_keycode[i], kbd->dev.keybit); |
||
274 | clear_bit(0, kbd->dev.keybit); |
||
275 | |||
276 | kbd->dev.private = kbd; |
||
277 | kbd->dev.event = usb_kbd_event; |
||
278 | kbd->dev.open = usb_kbd_open; |
||
279 | kbd->dev.close = usb_kbd_close; |
||
280 | |||
281 | usb_fill_int_urb(kbd->irq, dev, pipe, |
||
282 | kbd->new, (maxp > 8 ? 8 : maxp), |
||
283 | usb_kbd_irq, kbd, endpoint->bInterval); |
||
284 | kbd->irq->transfer_dma = kbd->new_dma; |
||
285 | kbd->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
||
286 | |||
287 | kbd->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE; |
||
288 | kbd->cr->bRequest = 0x09; |
||
289 | kbd->cr->wValue = cpu_to_le16(0x200); |
||
290 | kbd->cr->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber); |
||
291 | kbd->cr->wLength = cpu_to_le16(1); |
||
292 | |||
293 | usb_make_path(dev, path, 64); |
||
294 | sprintf26(kbd->phys, "%s/input0", path); |
||
295 | |||
296 | kbd->dev.name = kbd->name; |
||
297 | kbd->dev.phys = kbd->phys; |
||
298 | kbd->dev.id.bustype = BUS_USB; |
||
299 | kbd->dev.id.vendor = dev->descriptor.idVendor; |
||
300 | kbd->dev.id.product = dev->descriptor.idProduct; |
||
301 | kbd->dev.id.version = dev->descriptor.bcdDevice; |
||
302 | |||
303 | if (!(buf = kmalloc(63, GFP_KERNEL))) { |
||
304 | usb_free_urb(kbd->irq); |
||
305 | usb_kbd_free_mem(dev, kbd); |
||
306 | kfree(kbd); |
||
307 | return -ENOMEM; |
||
308 | } |
||
309 | |||
310 | if (dev->descriptor.iManufacturer && |
||
311 | usb_string(dev, dev->descriptor.iManufacturer, buf, 63) > 0) |
||
312 | strcat(kbd->name, buf); |
||
313 | if (dev->descriptor.iProduct && |
||
314 | usb_string(dev, dev->descriptor.iProduct, buf, 63) > 0) |
||
315 | sprintf26(kbd->name, "%s %s", kbd->name, buf); |
||
316 | |||
317 | if (!strlen(kbd->name)) |
||
318 | sprintf26(kbd->name, "USB HIDBP Keyboard %04x:%04x", |
||
319 | kbd->dev.id.vendor, kbd->dev.id.product); |
||
320 | |||
321 | kfree(buf); |
||
322 | |||
323 | usb_fill_control_urb(kbd->led, dev, usb_sndctrlpipe(dev, 0), |
||
324 | (void *) kbd->cr, kbd->leds, 1, |
||
325 | usb_kbd_led, kbd); |
||
326 | kbd->led->setup_dma = kbd->cr_dma; |
||
327 | kbd->led->transfer_dma = kbd->leds_dma; |
||
328 | kbd->led->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP |
||
329 | | URB_NO_SETUP_DMA_MAP); |
||
330 | |||
331 | input_register_device(&kbd->dev); |
||
332 | |||
333 | printk(KERN_INFO "input: %s on %s\n", kbd->name, path); |
||
334 | |||
335 | usb_set_intfdata(iface, kbd); |
||
336 | return 0; |
||
337 | } |
||
338 | |||
339 | static void usb_kbd_disconnect(struct usb_interface *intf) |
||
340 | { |
||
341 | struct usb_kbd *kbd = usb_get_intfdata (intf); |
||
342 | |||
343 | usb_set_intfdata(intf, NULL); |
||
344 | if (kbd) { |
||
345 | usb_unlink_urb(kbd->irq); |
||
346 | input_unregister_device(&kbd->dev); |
||
347 | usb_kbd_free_mem(interface_to_usbdev(intf), kbd); |
||
348 | kfree(kbd); |
||
349 | } |
||
350 | } |
||
351 | |||
352 | static struct usb_device_id usb_kbd_id_table [] = { |
||
353 | { USB_INTERFACE_INFO(3, 1, 1) }, |
||
354 | { } /* Terminating entry */ |
||
355 | }; |
||
356 | |||
357 | MODULE_DEVICE_TABLE (usb, usb_kbd_id_table); |
||
358 | |||
359 | static struct usb_driver usb_kbd_driver = { |
||
360 | .owner = THIS_MODULE, |
||
361 | .name = "usbkbd", |
||
362 | .probe = usb_kbd_probe, |
||
363 | .disconnect = usb_kbd_disconnect, |
||
364 | .id_table = usb_kbd_id_table, |
||
365 | }; |
||
366 | |||
367 | /*static*/ int /*__init*/ usb_kbd_init(void) |
||
368 | { |
||
369 | int result = usb_register(&usb_kbd_driver); |
||
370 | if (result == 0) |
||
371 | info(DRIVER_VERSION ":" DRIVER_DESC); |
||
372 | return result; |
||
373 | } |
||
374 | |||
375 | /*static*/ void /*__exit*/ usb_kbd_exit(void) |
||
376 | { |
||
377 | usb_deregister(&usb_kbd_driver); |
||
378 | } |
||
379 | |||
380 | module_init(usb_kbd_init); |
||
381 | module_exit(usb_kbd_exit); |