usbmouse.c 6.4 KB

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