~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/usb/usbmouse.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.