usbmouse.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * $Id: usbmouse.c,v 1.15 2001/12/27 10:37:41 vojtech Exp $
  3. *
  4. * Copyright (c) 1999-2001 Vojtech Pavlik
  5. *
  6. * USB HIDBP Mouse support
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * Should you need to contact me, the author, you can do so either by
  24. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  25. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/usb/input.h>
  32. #include <linux/hid.h>
  33. /*
  34. * Version Information
  35. */
  36. #define DRIVER_VERSION "v1.6"
  37. #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@ucw.cz>"
  38. #define DRIVER_DESC "USB HID Boot Protocol mouse driver"
  39. #define DRIVER_LICENSE "GPL"
  40. MODULE_AUTHOR(DRIVER_AUTHOR);
  41. MODULE_DESCRIPTION(DRIVER_DESC);
  42. MODULE_LICENSE(DRIVER_LICENSE);
  43. struct usb_mouse {
  44. char name[128];
  45. char phys[64];
  46. struct usb_device *usbdev;
  47. struct input_dev *dev;
  48. struct urb *irq;
  49. signed char *data;
  50. dma_addr_t data_dma;
  51. };
  52. static void usb_mouse_irq(struct urb *urb)
  53. {
  54. struct usb_mouse *mouse = urb->context;
  55. signed char *data = mouse->data;
  56. struct input_dev *dev = mouse->dev;
  57. int status;
  58. switch (urb->status) {
  59. case 0: /* success */
  60. break;
  61. case -ECONNRESET: /* unlink */
  62. case -ENOENT:
  63. case -ESHUTDOWN:
  64. return;
  65. /* -EPIPE: should clear the halt */
  66. default: /* error */
  67. goto resubmit;
  68. }
  69. input_report_key(dev, BTN_LEFT, data[0] & 0x01);
  70. input_report_key(dev, BTN_RIGHT, data[0] & 0x02);
  71. input_report_key(dev, BTN_MIDDLE, data[0] & 0x04);
  72. input_report_key(dev, BTN_SIDE, data[0] & 0x08);
  73. input_report_key(dev, BTN_EXTRA, data[0] & 0x10);
  74. input_report_rel(dev, REL_X, data[1]);
  75. input_report_rel(dev, REL_Y, data[2]);
  76. input_report_rel(dev, REL_WHEEL, data[3]);
  77. input_sync(dev);
  78. resubmit:
  79. status = usb_submit_urb (urb, GFP_ATOMIC);
  80. if (status)
  81. err ("can't resubmit intr, %s-%s/input0, status %d",
  82. mouse->usbdev->bus->bus_name,
  83. mouse->usbdev->devpath, status);
  84. }
  85. static int usb_mouse_open(struct input_dev *dev)
  86. {
  87. struct usb_mouse *mouse = input_get_drvdata(dev);
  88. mouse->irq->dev = mouse->usbdev;
  89. if (usb_submit_urb(mouse->irq, GFP_KERNEL))
  90. return -EIO;
  91. return 0;
  92. }
  93. static void usb_mouse_close(struct input_dev *dev)
  94. {
  95. struct usb_mouse *mouse = input_get_drvdata(dev);
  96. usb_kill_urb(mouse->irq);
  97. }
  98. static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id)
  99. {
  100. struct usb_device *dev = interface_to_usbdev(intf);
  101. struct usb_host_interface *interface;
  102. struct usb_endpoint_descriptor *endpoint;
  103. struct usb_mouse *mouse;
  104. struct input_dev *input_dev;
  105. int pipe, maxp;
  106. int error = -ENOMEM;
  107. interface = intf->cur_altsetting;
  108. if (interface->desc.bNumEndpoints != 1)
  109. return -ENODEV;
  110. endpoint = &interface->endpoint[0].desc;
  111. if (!usb_endpoint_is_int_in(endpoint))
  112. return -ENODEV;
  113. #ifdef CONFIG_USB_HID
  114. if (usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
  115. le16_to_cpu(dev->descriptor.idProduct))
  116. & (HID_QUIRK_IGNORE|HID_QUIRK_IGNORE_MOUSE)) {
  117. return -ENODEV;
  118. }
  119. #endif
  120. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  121. maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  122. mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL);
  123. input_dev = input_allocate_device();
  124. if (!mouse || !input_dev)
  125. goto fail1;
  126. mouse->data = usb_buffer_alloc(dev, 8, GFP_ATOMIC, &mouse->data_dma);
  127. if (!mouse->data)
  128. goto fail1;
  129. mouse->irq = usb_alloc_urb(0, GFP_KERNEL);
  130. if (!mouse->irq)
  131. goto fail2;
  132. mouse->usbdev = dev;
  133. mouse->dev = input_dev;
  134. if (dev->manufacturer)
  135. strlcpy(mouse->name, dev->manufacturer, sizeof(mouse->name));
  136. if (dev->product) {
  137. if (dev->manufacturer)
  138. strlcat(mouse->name, " ", sizeof(mouse->name));
  139. strlcat(mouse->name, dev->product, sizeof(mouse->name));
  140. }
  141. if (!strlen(mouse->name))
  142. snprintf(mouse->name, sizeof(mouse->name),
  143. "USB HIDBP Mouse %04x:%04x",
  144. le16_to_cpu(dev->descriptor.idVendor),
  145. le16_to_cpu(dev->descriptor.idProduct));
  146. usb_make_path(dev, mouse->phys, sizeof(mouse->phys));
  147. strlcat(mouse->phys, "/input0", sizeof(mouse->phys));
  148. input_dev->name = mouse->name;
  149. input_dev->phys = mouse->phys;
  150. usb_to_input_id(dev, &input_dev->id);
  151. input_dev->dev.parent = &intf->dev;
  152. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  153. input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
  154. BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
  155. input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  156. input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) |
  157. BIT_MASK(BTN_EXTRA);
  158. input_dev->relbit[0] |= BIT_MASK(REL_WHEEL);
  159. input_set_drvdata(input_dev, mouse);
  160. input_dev->open = usb_mouse_open;
  161. input_dev->close = usb_mouse_close;
  162. usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,
  163. (maxp > 8 ? 8 : maxp),
  164. usb_mouse_irq, mouse, endpoint->bInterval);
  165. mouse->irq->transfer_dma = mouse->data_dma;
  166. mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  167. error = input_register_device(mouse->dev);
  168. if (error)
  169. goto fail3;
  170. usb_set_intfdata(intf, mouse);
  171. return 0;
  172. fail3:
  173. usb_free_urb(mouse->irq);
  174. fail2:
  175. usb_buffer_free(dev, 8, mouse->data, mouse->data_dma);
  176. fail1:
  177. input_free_device(input_dev);
  178. kfree(mouse);
  179. return error;
  180. }
  181. static void usb_mouse_disconnect(struct usb_interface *intf)
  182. {
  183. struct usb_mouse *mouse = usb_get_intfdata (intf);
  184. usb_set_intfdata(intf, NULL);
  185. if (mouse) {
  186. usb_kill_urb(mouse->irq);
  187. input_unregister_device(mouse->dev);
  188. usb_free_urb(mouse->irq);
  189. usb_buffer_free(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma);
  190. kfree(mouse);
  191. }
  192. }
  193. static struct usb_device_id usb_mouse_id_table [] = {
  194. { USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,
  195. USB_INTERFACE_PROTOCOL_MOUSE) },
  196. { } /* Terminating entry */
  197. };
  198. MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
  199. static struct usb_driver usb_mouse_driver = {
  200. .name = "usbmouse",
  201. .probe = usb_mouse_probe,
  202. .disconnect = usb_mouse_disconnect,
  203. .id_table = usb_mouse_id_table,
  204. };
  205. static int __init usb_mouse_init(void)
  206. {
  207. int retval = usb_register(&usb_mouse_driver);
  208. if (retval == 0)
  209. info(DRIVER_VERSION ":" DRIVER_DESC);
  210. return retval;
  211. }
  212. static void __exit usb_mouse_exit(void)
  213. {
  214. usb_deregister(&usb_mouse_driver);
  215. }
  216. module_init(usb_mouse_init);
  217. module_exit(usb_mouse_exit);