usbmouse.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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/input.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/usb.h>
  33. #include <linux/usb_input.h>
  34. /*
  35. * Version Information
  36. */
  37. #define DRIVER_VERSION "v1.6"
  38. #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@ucw.cz>"
  39. #define DRIVER_DESC "USB HID Boot Protocol mouse driver"
  40. #define DRIVER_LICENSE "GPL"
  41. MODULE_AUTHOR(DRIVER_AUTHOR);
  42. MODULE_DESCRIPTION(DRIVER_DESC);
  43. MODULE_LICENSE(DRIVER_LICENSE);
  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, struct pt_regs *regs)
  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_regs(dev, regs);
  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, SLAB_ATOMIC);
  82. if (status)
  83. err ("can't resubmit intr, %s-%s/input0, status %d",
  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 = dev->private;
  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 = dev->private;
  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. interface = intf->cur_altsetting;
  109. if (interface->desc.bNumEndpoints != 1)
  110. return -ENODEV;
  111. endpoint = &interface->endpoint[0].desc;
  112. if (!(endpoint->bEndpointAddress & USB_DIR_IN))
  113. return -ENODEV;
  114. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
  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_buffer_alloc(dev, 8, SLAB_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->cdev.dev = &intf->dev;
  148. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
  149. input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
  150. input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
  151. input_dev->keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);
  152. input_dev->relbit[0] |= BIT(REL_WHEEL);
  153. input_dev->private = mouse;
  154. input_dev->open = usb_mouse_open;
  155. input_dev->close = usb_mouse_close;
  156. usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,
  157. (maxp > 8 ? 8 : maxp),
  158. usb_mouse_irq, mouse, endpoint->bInterval);
  159. mouse->irq->transfer_dma = mouse->data_dma;
  160. mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  161. input_register_device(mouse->dev);
  162. usb_set_intfdata(intf, mouse);
  163. return 0;
  164. fail2: usb_buffer_free(dev, 8, mouse->data, mouse->data_dma);
  165. fail1: input_free_device(input_dev);
  166. kfree(mouse);
  167. return -ENOMEM;
  168. }
  169. static void usb_mouse_disconnect(struct usb_interface *intf)
  170. {
  171. struct usb_mouse *mouse = usb_get_intfdata (intf);
  172. usb_set_intfdata(intf, NULL);
  173. if (mouse) {
  174. usb_kill_urb(mouse->irq);
  175. input_unregister_device(mouse->dev);
  176. usb_free_urb(mouse->irq);
  177. usb_buffer_free(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma);
  178. kfree(mouse);
  179. }
  180. }
  181. static struct usb_device_id usb_mouse_id_table [] = {
  182. { USB_INTERFACE_INFO(3, 1, 2) },
  183. { } /* Terminating entry */
  184. };
  185. MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
  186. static struct usb_driver usb_mouse_driver = {
  187. .name = "usbmouse",
  188. .probe = usb_mouse_probe,
  189. .disconnect = usb_mouse_disconnect,
  190. .id_table = usb_mouse_id_table,
  191. };
  192. static int __init usb_mouse_init(void)
  193. {
  194. int retval = usb_register(&usb_mouse_driver);
  195. if (retval == 0)
  196. info(DRIVER_VERSION ":" DRIVER_DESC);
  197. return retval;
  198. }
  199. static void __exit usb_mouse_exit(void)
  200. {
  201. usb_deregister(&usb_mouse_driver);
  202. }
  203. module_init(usb_mouse_init);
  204. module_exit(usb_mouse_exit);