Merge git://git.kernel.org/pub/scm/linux/kernel/git/wim/linux-2.6-watchdog
[powerpc.git] / drivers / usb / input / usbmouse.c
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 /*
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  * Should you need to contact me, the author, you can do so either by
25  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
27  */
28
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/usb/input.h>
34
35 /*
36  * Version Information
37  */
38 #define DRIVER_VERSION "v1.6"
39 #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@ucw.cz>"
40 #define DRIVER_DESC "USB HID Boot Protocol mouse driver"
41 #define DRIVER_LICENSE "GPL"
42
43 MODULE_AUTHOR(DRIVER_AUTHOR);
44 MODULE_DESCRIPTION(DRIVER_DESC);
45 MODULE_LICENSE(DRIVER_LICENSE);
46
47 struct usb_mouse {
48         char name[128];
49         char phys[64];
50         struct usb_device *usbdev;
51         struct input_dev *dev;
52         struct urb *irq;
53
54         signed char *data;
55         dma_addr_t data_dma;
56 };
57
58 static void usb_mouse_irq(struct urb *urb)
59 {
60         struct usb_mouse *mouse = urb->context;
61         signed char *data = mouse->data;
62         struct input_dev *dev = mouse->dev;
63         int status;
64
65         switch (urb->status) {
66         case 0:                 /* success */
67                 break;
68         case -ECONNRESET:       /* unlink */
69         case -ENOENT:
70         case -ESHUTDOWN:
71                 return;
72         /* -EPIPE:  should clear the halt */
73         default:                /* error */
74                 goto resubmit;
75         }
76
77         input_report_key(dev, BTN_LEFT,   data[0] & 0x01);
78         input_report_key(dev, BTN_RIGHT,  data[0] & 0x02);
79         input_report_key(dev, BTN_MIDDLE, data[0] & 0x04);
80         input_report_key(dev, BTN_SIDE,   data[0] & 0x08);
81         input_report_key(dev, BTN_EXTRA,  data[0] & 0x10);
82
83         input_report_rel(dev, REL_X,     data[1]);
84         input_report_rel(dev, REL_Y,     data[2]);
85         input_report_rel(dev, REL_WHEEL, data[3]);
86
87         input_sync(dev);
88 resubmit:
89         status = usb_submit_urb (urb, SLAB_ATOMIC);
90         if (status)
91                 err ("can't resubmit intr, %s-%s/input0, status %d",
92                                 mouse->usbdev->bus->bus_name,
93                                 mouse->usbdev->devpath, status);
94 }
95
96 static int usb_mouse_open(struct input_dev *dev)
97 {
98         struct usb_mouse *mouse = dev->private;
99
100         mouse->irq->dev = mouse->usbdev;
101         if (usb_submit_urb(mouse->irq, GFP_KERNEL))
102                 return -EIO;
103
104         return 0;
105 }
106
107 static void usb_mouse_close(struct input_dev *dev)
108 {
109         struct usb_mouse *mouse = dev->private;
110
111         usb_kill_urb(mouse->irq);
112 }
113
114 static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id)
115 {
116         struct usb_device *dev = interface_to_usbdev(intf);
117         struct usb_host_interface *interface;
118         struct usb_endpoint_descriptor *endpoint;
119         struct usb_mouse *mouse;
120         struct input_dev *input_dev;
121         int pipe, maxp;
122
123         interface = intf->cur_altsetting;
124
125         if (interface->desc.bNumEndpoints != 1)
126                 return -ENODEV;
127
128         endpoint = &interface->endpoint[0].desc;
129         if (!(endpoint->bEndpointAddress & USB_DIR_IN))
130                 return -ENODEV;
131         if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
132                 return -ENODEV;
133
134         pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
135         maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
136
137         mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL);
138         input_dev = input_allocate_device();
139         if (!mouse || !input_dev)
140                 goto fail1;
141
142         mouse->data = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &mouse->data_dma);
143         if (!mouse->data)
144                 goto fail1;
145
146         mouse->irq = usb_alloc_urb(0, GFP_KERNEL);
147         if (!mouse->irq)
148                 goto fail2;
149
150         mouse->usbdev = dev;
151         mouse->dev = input_dev;
152
153         if (dev->manufacturer)
154                 strlcpy(mouse->name, dev->manufacturer, sizeof(mouse->name));
155
156         if (dev->product) {
157                 if (dev->manufacturer)
158                         strlcat(mouse->name, " ", sizeof(mouse->name));
159                 strlcat(mouse->name, dev->product, sizeof(mouse->name));
160         }
161
162         if (!strlen(mouse->name))
163                 snprintf(mouse->name, sizeof(mouse->name),
164                          "USB HIDBP Mouse %04x:%04x",
165                          le16_to_cpu(dev->descriptor.idVendor),
166                          le16_to_cpu(dev->descriptor.idProduct));
167
168         usb_make_path(dev, mouse->phys, sizeof(mouse->phys));
169         strlcat(mouse->phys, "/input0", sizeof(mouse->phys));
170
171         input_dev->name = mouse->name;
172         input_dev->phys = mouse->phys;
173         usb_to_input_id(dev, &input_dev->id);
174         input_dev->cdev.dev = &intf->dev;
175
176         input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
177         input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
178         input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
179         input_dev->keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);
180         input_dev->relbit[0] |= BIT(REL_WHEEL);
181
182         input_dev->private = mouse;
183         input_dev->open = usb_mouse_open;
184         input_dev->close = usb_mouse_close;
185
186         usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,
187                          (maxp > 8 ? 8 : maxp),
188                          usb_mouse_irq, mouse, endpoint->bInterval);
189         mouse->irq->transfer_dma = mouse->data_dma;
190         mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
191
192         input_register_device(mouse->dev);
193
194         usb_set_intfdata(intf, mouse);
195         return 0;
196
197 fail2:  usb_buffer_free(dev, 8, mouse->data, mouse->data_dma);
198 fail1:  input_free_device(input_dev);
199         kfree(mouse);
200         return -ENOMEM;
201 }
202
203 static void usb_mouse_disconnect(struct usb_interface *intf)
204 {
205         struct usb_mouse *mouse = usb_get_intfdata (intf);
206
207         usb_set_intfdata(intf, NULL);
208         if (mouse) {
209                 usb_kill_urb(mouse->irq);
210                 input_unregister_device(mouse->dev);
211                 usb_free_urb(mouse->irq);
212                 usb_buffer_free(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma);
213                 kfree(mouse);
214         }
215 }
216
217 static struct usb_device_id usb_mouse_id_table [] = {
218         { USB_INTERFACE_INFO(3, 1, 2) },
219         { }     /* Terminating entry */
220 };
221
222 MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
223
224 static struct usb_driver usb_mouse_driver = {
225         .name           = "usbmouse",
226         .probe          = usb_mouse_probe,
227         .disconnect     = usb_mouse_disconnect,
228         .id_table       = usb_mouse_id_table,
229 };
230
231 static int __init usb_mouse_init(void)
232 {
233         int retval = usb_register(&usb_mouse_driver);
234         if (retval == 0)
235                 info(DRIVER_VERSION ":" DRIVER_DESC);
236         return retval;
237 }
238
239 static void __exit usb_mouse_exit(void)
240 {
241         usb_deregister(&usb_mouse_driver);
242 }
243
244 module_init(usb_mouse_init);
245 module_exit(usb_mouse_exit);