usbmouse.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. /*
  33. * Version Information
  34. */
  35. #define DRIVER_VERSION "v1.6"
  36. #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@ucw.cz>"
  37. #define DRIVER_DESC "USB HID Boot Protocol mouse driver"
  38. #define DRIVER_LICENSE "GPL"
  39. MODULE_AUTHOR(DRIVER_AUTHOR);
  40. MODULE_DESCRIPTION(DRIVER_DESC);
  41. MODULE_LICENSE(DRIVER_LICENSE);
  42. struct usb_mouse {
  43. char name[128];
  44. char phys[64];
  45. struct usb_device *usbdev;
  46. struct input_dev *dev;
  47. struct urb *irq;
  48. signed char *data;
  49. dma_addr_t data_dma;
  50. };
  51. static void usb_mouse_irq(struct urb *urb)
  52. {
  53. struct usb_mouse *mouse = urb->context;
  54. signed char *data = mouse->data;
  55. struct input_dev *dev = mouse->dev;
  56. int status;
  57. switch (urb->status) {
  58. case 0: /* success */
  59. break;
  60. case -ECONNRESET: /* unlink */
  61. case -ENOENT:
  62. case -ESHUTDOWN:
  63. return;
  64. /* -EPIPE: should clear the halt */
  65. default: /* error */
  66. goto resubmit;
  67. }
  68. input_report_key(dev, BTN_LEFT, data[0] & 0x01);
  69. input_report_key(dev, BTN_RIGHT, data[0] & 0x02);
  70. input_report_key(dev, BTN_MIDDLE, data[0] & 0x04);
  71. input_report_key(dev, BTN_SIDE, data[0] & 0x08);
  72. input_report_key(dev, BTN_EXTRA, data[0] & 0x10);
  73. input_report_rel(dev, REL_X, data[1]);
  74. input_report_rel(dev, REL_Y, data[2]);
  75. input_report_rel(dev, REL_WHEEL, data[3]);
  76. input_sync(dev);
  77. resubmit:
  78. status = usb_submit_urb (urb, SLAB_ATOMIC);
  79. if (status)
  80. err ("can't resubmit intr, %s-%s/input0, status %d",
  81. mouse->usbdev->bus->bus_name,
  82. mouse->usbdev->devpath, status);
  83. }
  84. static int usb_mouse_open(struct input_dev *dev)
  85. {
  86. struct usb_mouse *mouse = dev->private;
  87. mouse->irq->dev = mouse->usbdev;
  88. if (usb_submit_urb(mouse->irq, GFP_KERNEL))
  89. return -EIO;
  90. return 0;
  91. }
  92. static void usb_mouse_close(struct input_dev *dev)
  93. {
  94. struct usb_mouse *mouse = dev->private;
  95. usb_kill_urb(mouse->irq);
  96. }
  97. static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id)
  98. {
  99. struct usb_device *dev = interface_to_usbdev(intf);
  100. struct usb_host_interface *interface;
  101. struct usb_endpoint_descriptor *endpoint;
  102. struct usb_mouse *mouse;
  103. struct input_dev *input_dev;
  104. int pipe, maxp;
  105. interface = intf->cur_altsetting;
  106. if (interface->desc.bNumEndpoints != 1)
  107. return -ENODEV;
  108. endpoint = &interface->endpoint[0].desc;
  109. if (!(endpoint->bEndpointAddress & USB_DIR_IN))
  110. return -ENODEV;
  111. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
  112. return -ENODEV;
  113. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  114. maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  115. mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL);
  116. input_dev = input_allocate_device();
  117. if (!mouse || !input_dev)
  118. goto fail1;
  119. mouse->data = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &mouse->data_dma);
  120. if (!mouse->data)
  121. goto fail1;
  122. mouse->irq = usb_alloc_urb(0, GFP_KERNEL);
  123. if (!mouse->irq)
  124. goto fail2;
  125. mouse->usbdev = dev;
  126. mouse->dev = input_dev;
  127. if (dev->manufacturer)
  128. strlcpy(mouse->name, dev->manufacturer, sizeof(mouse->name));
  129. if (dev->product) {
  130. if (dev->manufacturer)
  131. strlcat(mouse->name, " ", sizeof(mouse->name));
  132. strlcat(mouse->name, dev->product, sizeof(mouse->name));
  133. }
  134. if (!strlen(mouse->name))
  135. snprintf(mouse->name, sizeof(mouse->name),
  136. "USB HIDBP Mouse %04x:%04x",
  137. le16_to_cpu(dev->descriptor.idVendor),
  138. le16_to_cpu(dev->descriptor.idProduct));
  139. usb_make_path(dev, mouse->phys, sizeof(mouse->phys));
  140. strlcat(mouse->phys, "/input0", sizeof(mouse->phys));
  141. input_dev->name = mouse->name;
  142. input_dev->phys = mouse->phys;
  143. usb_to_input_id(dev, &input_dev->id);
  144. input_dev->cdev.dev = &intf->dev;
  145. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
  146. input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
  147. input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
  148. input_dev->keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);
  149. input_dev->relbit[0] |= BIT(REL_WHEEL);
  150. input_dev->private = mouse;
  151. input_dev->open = usb_mouse_open;
  152. input_dev->close = usb_mouse_close;
  153. usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,
  154. (maxp > 8 ? 8 : maxp),
  155. usb_mouse_irq, mouse, endpoint->bInterval);
  156. mouse->irq->transfer_dma = mouse->data_dma;
  157. mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  158. input_register_device(mouse->dev);
  159. usb_set_intfdata(intf, mouse);
  160. return 0;
  161. fail2: usb_buffer_free(dev, 8, mouse->data, mouse->data_dma);
  162. fail1: input_free_device(input_dev);
  163. kfree(mouse);
  164. return -ENOMEM;
  165. }
  166. static void usb_mouse_disconnect(struct usb_interface *intf)
  167. {
  168. struct usb_mouse *mouse = usb_get_intfdata (intf);
  169. usb_set_intfdata(intf, NULL);
  170. if (mouse) {
  171. usb_kill_urb(mouse->irq);
  172. input_unregister_device(mouse->dev);
  173. usb_free_urb(mouse->irq);
  174. usb_buffer_free(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma);
  175. kfree(mouse);
  176. }
  177. }
  178. static struct usb_device_id usb_mouse_id_table [] = {
  179. { USB_INTERFACE_INFO(3, 1, 2) },
  180. { } /* Terminating entry */
  181. };
  182. MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
  183. static struct usb_driver usb_mouse_driver = {
  184. .name = "usbmouse",
  185. .probe = usb_mouse_probe,
  186. .disconnect = usb_mouse_disconnect,
  187. .id_table = usb_mouse_id_table,
  188. };
  189. static int __init usb_mouse_init(void)
  190. {
  191. int retval = usb_register(&usb_mouse_driver);
  192. if (retval == 0)
  193. info(DRIVER_VERSION ":" DRIVER_DESC);
  194. return retval;
  195. }
  196. static void __exit usb_mouse_exit(void)
  197. {
  198. usb_deregister(&usb_mouse_driver);
  199. }
  200. module_init(usb_mouse_init);
  201. module_exit(usb_mouse_exit);