usbkbd.c 12 KB

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