usbkbd.c 11 KB

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