usbmouse.c 6.4 KB

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