usbmouse.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. int pipe, maxp;
  107. char path[64];
  108. interface = intf->cur_altsetting;
  109. if (interface->desc.bNumEndpoints != 1)
  110. return -ENODEV;
  111. endpoint = &interface->endpoint[0].desc;
  112. if (!(endpoint->bEndpointAddress & 0x80))
  113. return -ENODEV;
  114. if ((endpoint->bmAttributes & 3) != 3)
  115. return -ENODEV;
  116. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  117. maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  118. if (!(mouse = kmalloc(sizeof(struct usb_mouse), GFP_KERNEL)))
  119. return -ENOMEM;
  120. memset(mouse, 0, sizeof(struct usb_mouse));
  121. mouse->data = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &mouse->data_dma);
  122. if (!mouse->data) {
  123. kfree(mouse);
  124. return -ENOMEM;
  125. }
  126. mouse->irq = usb_alloc_urb(0, GFP_KERNEL);
  127. if (!mouse->irq) {
  128. usb_buffer_free(dev, 8, mouse->data, mouse->data_dma);
  129. kfree(mouse);
  130. return -ENODEV;
  131. }
  132. mouse->usbdev = dev;
  133. mouse->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
  134. mouse->dev.keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
  135. mouse->dev.relbit[0] = BIT(REL_X) | BIT(REL_Y);
  136. mouse->dev.keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);
  137. mouse->dev.relbit[0] |= BIT(REL_WHEEL);
  138. mouse->dev.private = mouse;
  139. mouse->dev.open = usb_mouse_open;
  140. mouse->dev.close = usb_mouse_close;
  141. usb_make_path(dev, path, 64);
  142. sprintf(mouse->phys, "%s/input0", path);
  143. mouse->dev.name = mouse->name;
  144. mouse->dev.phys = mouse->phys;
  145. usb_to_input_id(dev, &mouse->dev.id);
  146. mouse->dev.dev = &intf->dev;
  147. if (dev->manufacturer)
  148. strcat(mouse->name, dev->manufacturer);
  149. if (dev->product)
  150. sprintf(mouse->name, "%s %s", mouse->name, dev->product);
  151. if (!strlen(mouse->name))
  152. sprintf(mouse->name, "USB HIDBP Mouse %04x:%04x",
  153. mouse->dev.id.vendor, mouse->dev.id.product);
  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. printk(KERN_INFO "input: %s on %s\n", mouse->name, path);
  161. usb_set_intfdata(intf, mouse);
  162. return 0;
  163. }
  164. static void usb_mouse_disconnect(struct usb_interface *intf)
  165. {
  166. struct usb_mouse *mouse = usb_get_intfdata (intf);
  167. usb_set_intfdata(intf, NULL);
  168. if (mouse) {
  169. usb_kill_urb(mouse->irq);
  170. input_unregister_device(&mouse->dev);
  171. usb_free_urb(mouse->irq);
  172. usb_buffer_free(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma);
  173. kfree(mouse);
  174. }
  175. }
  176. static struct usb_device_id usb_mouse_id_table [] = {
  177. { USB_INTERFACE_INFO(3, 1, 2) },
  178. { } /* Terminating entry */
  179. };
  180. MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
  181. static struct usb_driver usb_mouse_driver = {
  182. .owner = THIS_MODULE,
  183. .name = "usbmouse",
  184. .probe = usb_mouse_probe,
  185. .disconnect = usb_mouse_disconnect,
  186. .id_table = usb_mouse_id_table,
  187. };
  188. static int __init usb_mouse_init(void)
  189. {
  190. int retval = usb_register(&usb_mouse_driver);
  191. if (retval == 0)
  192. info(DRIVER_VERSION ":" DRIVER_DESC);
  193. return retval;
  194. }
  195. static void __exit usb_mouse_exit(void)
  196. {
  197. usb_deregister(&usb_mouse_driver);
  198. }
  199. module_init(usb_mouse_init);
  200. module_exit(usb_mouse_exit);