usbmouse.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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, struct pt_regs *regs)
  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_regs(dev, regs);
  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, SLAB_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 = dev->private;
  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 = dev->private;
  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. interface = intf->cur_altsetting;
  107. if (interface->desc.bNumEndpoints != 1)
  108. return -ENODEV;
  109. endpoint = &interface->endpoint[0].desc;
  110. if (!(endpoint->bEndpointAddress & USB_DIR_IN))
  111. return -ENODEV;
  112. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
  113. return -ENODEV;
  114. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  115. maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  116. mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL);
  117. input_dev = input_allocate_device();
  118. if (!mouse || !input_dev)
  119. goto fail1;
  120. mouse->data = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &mouse->data_dma);
  121. if (!mouse->data)
  122. goto fail1;
  123. mouse->irq = usb_alloc_urb(0, GFP_KERNEL);
  124. if (!mouse->irq)
  125. goto fail2;
  126. mouse->usbdev = dev;
  127. mouse->dev = input_dev;
  128. if (dev->manufacturer)
  129. strlcpy(mouse->name, dev->manufacturer, sizeof(mouse->name));
  130. if (dev->product) {
  131. if (dev->manufacturer)
  132. strlcat(mouse->name, " ", sizeof(mouse->name));
  133. strlcat(mouse->name, dev->product, sizeof(mouse->name));
  134. }
  135. if (!strlen(mouse->name))
  136. snprintf(mouse->name, sizeof(mouse->name),
  137. "USB HIDBP Mouse %04x:%04x",
  138. le16_to_cpu(dev->descriptor.idVendor),
  139. le16_to_cpu(dev->descriptor.idProduct));
  140. usb_make_path(dev, mouse->phys, sizeof(mouse->phys));
  141. strlcat(mouse->phys, "/input0", sizeof(mouse->phys));
  142. input_dev->name = mouse->name;
  143. input_dev->phys = mouse->phys;
  144. usb_to_input_id(dev, &input_dev->id);
  145. input_dev->cdev.dev = &intf->dev;
  146. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
  147. input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
  148. input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
  149. input_dev->keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);
  150. input_dev->relbit[0] |= BIT(REL_WHEEL);
  151. input_dev->private = mouse;
  152. input_dev->open = usb_mouse_open;
  153. input_dev->close = usb_mouse_close;
  154. usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,
  155. (maxp > 8 ? 8 : maxp),
  156. usb_mouse_irq, mouse, endpoint->bInterval);
  157. mouse->irq->transfer_dma = mouse->data_dma;
  158. mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  159. input_register_device(mouse->dev);
  160. usb_set_intfdata(intf, mouse);
  161. return 0;
  162. fail2: usb_buffer_free(dev, 8, mouse->data, mouse->data_dma);
  163. fail1: input_free_device(input_dev);
  164. kfree(mouse);
  165. return -ENOMEM;
  166. }
  167. static void usb_mouse_disconnect(struct usb_interface *intf)
  168. {
  169. struct usb_mouse *mouse = usb_get_intfdata (intf);
  170. usb_set_intfdata(intf, NULL);
  171. if (mouse) {
  172. usb_kill_urb(mouse->irq);
  173. input_unregister_device(mouse->dev);
  174. usb_free_urb(mouse->irq);
  175. usb_buffer_free(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma);
  176. kfree(mouse);
  177. }
  178. }
  179. static struct usb_device_id usb_mouse_id_table [] = {
  180. { USB_INTERFACE_INFO(3, 1, 2) },
  181. { } /* Terminating entry */
  182. };
  183. MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
  184. static struct usb_driver usb_mouse_driver = {
  185. .name = "usbmouse",
  186. .probe = usb_mouse_probe,
  187. .disconnect = usb_mouse_disconnect,
  188. .id_table = usb_mouse_id_table,
  189. };
  190. static int __init usb_mouse_init(void)
  191. {
  192. int retval = usb_register(&usb_mouse_driver);
  193. if (retval == 0)
  194. info(DRIVER_VERSION ":" DRIVER_DESC);
  195. return retval;
  196. }
  197. static void __exit usb_mouse_exit(void)
  198. {
  199. usb_deregister(&usb_mouse_driver);
  200. }
  201. module_init(usb_mouse_init);
  202. module_exit(usb_mouse_exit);