usbmouse.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:6k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * $Id: usbmouse.c,v 1.6 2000/08/14 21:05:26 vojtech Exp $
  3.  *
  4.  *  Copyright (c) 1999-2000 Vojtech Pavlik
  5.  *
  6.  *  USB HIDBP Mouse support
  7.  *
  8.  *  Sponsored by SuSE
  9.  */
  10. /*
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or 
  14.  * (at your option) any later version.
  15.  * 
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  * 
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24.  * 
  25.  * Should you need to contact me, the author, you can do so either by
  26.  * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
  27.  * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
  28.  */
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/input.h>
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/usb.h>
  35. /*
  36.  * Version Information
  37.  */
  38. #define DRIVER_VERSION "v1.6"
  39. #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@suse.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. signed char data[8];
  46. char name[128];
  47. struct usb_device *usbdev;
  48. struct input_dev dev;
  49. struct urb irq;
  50. int open;
  51. };
  52. static void usb_mouse_irq(struct urb *urb)
  53. {
  54. struct usb_mouse *mouse = urb->context;
  55. signed char *data = mouse->data;
  56. struct input_dev *dev = &mouse->dev;
  57. if (urb->status) return;
  58. input_report_key(dev, BTN_LEFT,   data[0] & 0x01);
  59. input_report_key(dev, BTN_RIGHT,  data[0] & 0x02);
  60. input_report_key(dev, BTN_MIDDLE, data[0] & 0x04);
  61. input_report_key(dev, BTN_SIDE,   data[0] & 0x08);
  62. input_report_key(dev, BTN_EXTRA,  data[0] & 0x10);
  63. input_report_rel(dev, REL_X,     data[1]);
  64. input_report_rel(dev, REL_Y,     data[2]);
  65. input_report_rel(dev, REL_WHEEL, data[3]);
  66. }
  67. static int usb_mouse_open(struct input_dev *dev)
  68. {
  69. struct usb_mouse *mouse = dev->private;
  70. if (mouse->open++)
  71. return 0;
  72. mouse->irq.dev = mouse->usbdev;
  73. if (usb_submit_urb(&mouse->irq))
  74. return -EIO;
  75. return 0;
  76. }
  77. static void usb_mouse_close(struct input_dev *dev)
  78. {
  79. struct usb_mouse *mouse = dev->private;
  80. if (!--mouse->open)
  81. usb_unlink_urb(&mouse->irq);
  82. }
  83. static void *usb_mouse_probe(struct usb_device *dev, unsigned int ifnum,
  84.      const struct usb_device_id *id)
  85. {
  86. struct usb_interface *iface;
  87. struct usb_interface_descriptor *interface;
  88. struct usb_endpoint_descriptor *endpoint;
  89. struct usb_mouse *mouse;
  90. int pipe, maxp;
  91. char *buf;
  92. iface = &dev->actconfig->interface[ifnum];
  93. interface = &iface->altsetting[iface->act_altsetting];
  94. if (interface->bNumEndpoints != 1) return NULL;
  95. endpoint = interface->endpoint + 0;
  96. if (!(endpoint->bEndpointAddress & 0x80)) return NULL;
  97. if ((endpoint->bmAttributes & 3) != 3) return NULL;
  98. /* wacom tablets match... */
  99.   if (dev->descriptor.idVendor == 0x056a) return NULL;
  100. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  101. maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  102. usb_set_idle(dev, interface->bInterfaceNumber, 0, 0);
  103. if (!(mouse = kmalloc(sizeof(struct usb_mouse), GFP_KERNEL))) return NULL;
  104. memset(mouse, 0, sizeof(struct usb_mouse));
  105. mouse->usbdev = dev;
  106. mouse->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
  107. mouse->dev.keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
  108. mouse->dev.relbit[0] = BIT(REL_X) | BIT(REL_Y);
  109. mouse->dev.keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);
  110. mouse->dev.relbit[0] |= BIT(REL_WHEEL);
  111. mouse->dev.private = mouse;
  112. mouse->dev.open = usb_mouse_open;
  113. mouse->dev.close = usb_mouse_close;
  114. mouse->dev.name = mouse->name;
  115. mouse->dev.idbus = BUS_USB;
  116. mouse->dev.idvendor = dev->descriptor.idVendor;
  117. mouse->dev.idproduct = dev->descriptor.idProduct;
  118. mouse->dev.idversion = dev->descriptor.bcdDevice;
  119. if (!(buf = kmalloc(63, GFP_KERNEL))) {
  120. kfree(mouse);
  121. return NULL;
  122. }
  123. if (dev->descriptor.iManufacturer &&
  124. usb_string(dev, dev->descriptor.iManufacturer, buf, 63) > 0)
  125. strcat(mouse->name, buf);
  126. if (dev->descriptor.iProduct &&
  127. usb_string(dev, dev->descriptor.iProduct, buf, 63) > 0)
  128. sprintf(mouse->name, "%s %s", mouse->name, buf);
  129. if (!strlen(mouse->name))
  130. sprintf(mouse->name, "USB HIDBP Mouse %04x:%04x",
  131. mouse->dev.idvendor, mouse->dev.idproduct);
  132. kfree(buf);
  133. FILL_INT_URB(&mouse->irq, dev, pipe, mouse->data, maxp > 8 ? 8 : maxp,
  134. usb_mouse_irq, mouse, endpoint->bInterval);
  135. input_register_device(&mouse->dev);
  136. printk(KERN_INFO "input%d: %s on usb%d:%d.%dn",
  137.  mouse->dev.number, mouse->name, dev->bus->busnum, dev->devnum, ifnum);
  138. return mouse;
  139. }
  140. static void usb_mouse_disconnect(struct usb_device *dev, void *ptr)
  141. {
  142. struct usb_mouse *mouse = ptr;
  143. usb_unlink_urb(&mouse->irq);
  144. input_unregister_device(&mouse->dev);
  145. kfree(mouse);
  146. }
  147. static struct usb_device_id usb_mouse_id_table [] = {
  148. { USB_INTERFACE_INFO(3, 1, 2) },
  149.     { } /* Terminating entry */
  150. };
  151. MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
  152. static struct usb_driver usb_mouse_driver = {
  153. name: "usb_mouse",
  154. probe: usb_mouse_probe,
  155. disconnect: usb_mouse_disconnect,
  156. id_table: usb_mouse_id_table,
  157. };
  158. static int __init usb_mouse_init(void)
  159. {
  160. usb_register(&usb_mouse_driver);
  161. info(DRIVER_VERSION ":" DRIVER_DESC);
  162. return 0;
  163. }
  164. static void __exit usb_mouse_exit(void)
  165. {
  166. usb_deregister(&usb_mouse_driver);
  167. }
  168. module_init(usb_mouse_init);
  169. module_exit(usb_mouse_exit);