blob: 1ec41b5effe627fee269239b8350a1777fd5ae3f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050012 * the Free Software Foundation; either version 2 of the License, or
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * (at your option) any later version.
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * 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.
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * 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
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 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/input.h>
32#include <linux/module.h>
33#include <linux/init.h>
34#include <linux/usb.h>
35
36/*
37 * Version Information
38 */
39#define DRIVER_VERSION "v1.6"
40#define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@ucw.cz>"
41#define DRIVER_DESC "USB HID Boot Protocol mouse driver"
42#define DRIVER_LICENSE "GPL"
43
44MODULE_AUTHOR(DRIVER_AUTHOR);
45MODULE_DESCRIPTION(DRIVER_DESC);
46MODULE_LICENSE(DRIVER_LICENSE);
47
48struct usb_mouse {
49 char name[128];
50 char phys[64];
51 struct usb_device *usbdev;
52 struct input_dev dev;
53 struct urb *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 signed char *data;
56 dma_addr_t data_dma;
57};
58
59static void usb_mouse_irq(struct urb *urb, struct pt_regs *regs)
60{
61 struct usb_mouse *mouse = urb->context;
62 signed char *data = mouse->data;
63 struct input_dev *dev = &mouse->dev;
64 int status;
65
66 switch (urb->status) {
67 case 0: /* success */
68 break;
69 case -ECONNRESET: /* unlink */
70 case -ENOENT:
71 case -ESHUTDOWN:
72 return;
73 /* -EPIPE: should clear the halt */
74 default: /* error */
75 goto resubmit;
76 }
77
78 input_regs(dev, regs);
79
80 input_report_key(dev, BTN_LEFT, data[0] & 0x01);
81 input_report_key(dev, BTN_RIGHT, data[0] & 0x02);
82 input_report_key(dev, BTN_MIDDLE, data[0] & 0x04);
83 input_report_key(dev, BTN_SIDE, data[0] & 0x08);
84 input_report_key(dev, BTN_EXTRA, data[0] & 0x10);
85
86 input_report_rel(dev, REL_X, data[1]);
87 input_report_rel(dev, REL_Y, data[2]);
88 input_report_rel(dev, REL_WHEEL, data[3]);
89
90 input_sync(dev);
91resubmit:
92 status = usb_submit_urb (urb, SLAB_ATOMIC);
93 if (status)
94 err ("can't resubmit intr, %s-%s/input0, status %d",
95 mouse->usbdev->bus->bus_name,
96 mouse->usbdev->devpath, status);
97}
98
99static int usb_mouse_open(struct input_dev *dev)
100{
101 struct usb_mouse *mouse = dev->private;
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 mouse->irq->dev = mouse->usbdev;
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500104 if (usb_submit_urb(mouse->irq, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 return 0;
108}
109
110static void usb_mouse_close(struct input_dev *dev)
111{
112 struct usb_mouse *mouse = dev->private;
113
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500114 usb_kill_urb(mouse->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static int usb_mouse_probe(struct usb_interface * intf, const struct usb_device_id * id)
118{
119 struct usb_device * dev = interface_to_usbdev(intf);
120 struct usb_host_interface *interface;
121 struct usb_endpoint_descriptor *endpoint;
122 struct usb_mouse *mouse;
123 int pipe, maxp;
124 char path[64];
125
126 interface = intf->cur_altsetting;
127
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500128 if (interface->desc.bNumEndpoints != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return -ENODEV;
130
131 endpoint = &interface->endpoint[0].desc;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500132 if (!(endpoint->bEndpointAddress & 0x80))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return -ENODEV;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500134 if ((endpoint->bmAttributes & 3) != 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return -ENODEV;
136
137 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
138 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
139
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500140 if (!(mouse = kmalloc(sizeof(struct usb_mouse), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return -ENOMEM;
142 memset(mouse, 0, sizeof(struct usb_mouse));
143
144 mouse->data = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &mouse->data_dma);
145 if (!mouse->data) {
146 kfree(mouse);
147 return -ENOMEM;
148 }
149
150 mouse->irq = usb_alloc_urb(0, GFP_KERNEL);
151 if (!mouse->irq) {
152 usb_buffer_free(dev, 8, mouse->data, mouse->data_dma);
153 kfree(mouse);
154 return -ENODEV;
155 }
156
157 mouse->usbdev = dev;
158
159 mouse->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
160 mouse->dev.keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
161 mouse->dev.relbit[0] = BIT(REL_X) | BIT(REL_Y);
162 mouse->dev.keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);
163 mouse->dev.relbit[0] |= BIT(REL_WHEEL);
164
165 mouse->dev.private = mouse;
166 mouse->dev.open = usb_mouse_open;
167 mouse->dev.close = usb_mouse_close;
168
169 usb_make_path(dev, path, 64);
170 sprintf(mouse->phys, "%s/input0", path);
171
172 mouse->dev.name = mouse->name;
173 mouse->dev.phys = mouse->phys;
174 mouse->dev.id.bustype = BUS_USB;
175 mouse->dev.id.vendor = le16_to_cpu(dev->descriptor.idVendor);
176 mouse->dev.id.product = le16_to_cpu(dev->descriptor.idProduct);
177 mouse->dev.id.version = le16_to_cpu(dev->descriptor.bcdDevice);
178 mouse->dev.dev = &intf->dev;
179
180 if (dev->manufacturer)
181 strcat(mouse->name, dev->manufacturer);
182 if (dev->product)
183 sprintf(mouse->name, "%s %s", mouse->name, dev->product);
184
185 if (!strlen(mouse->name))
186 sprintf(mouse->name, "USB HIDBP Mouse %04x:%04x",
187 mouse->dev.id.vendor, mouse->dev.id.product);
188
189 usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,
190 (maxp > 8 ? 8 : maxp),
191 usb_mouse_irq, mouse, endpoint->bInterval);
192 mouse->irq->transfer_dma = mouse->data_dma;
193 mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
194
195 input_register_device(&mouse->dev);
196 printk(KERN_INFO "input: %s on %s\n", mouse->name, path);
197
198 usb_set_intfdata(intf, mouse);
199 return 0;
200}
201
202static void usb_mouse_disconnect(struct usb_interface *intf)
203{
204 struct usb_mouse *mouse = usb_get_intfdata (intf);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 usb_set_intfdata(intf, NULL);
207 if (mouse) {
208 usb_kill_urb(mouse->irq);
209 input_unregister_device(&mouse->dev);
210 usb_free_urb(mouse->irq);
211 usb_buffer_free(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma);
212 kfree(mouse);
213 }
214}
215
216static struct usb_device_id usb_mouse_id_table [] = {
217 { USB_INTERFACE_INFO(3, 1, 2) },
218 { } /* Terminating entry */
219};
220
221MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
222
223static struct usb_driver usb_mouse_driver = {
224 .owner = THIS_MODULE,
225 .name = "usbmouse",
226 .probe = usb_mouse_probe,
227 .disconnect = usb_mouse_disconnect,
228 .id_table = usb_mouse_id_table,
229};
230
231static int __init usb_mouse_init(void)
232{
233 int retval = usb_register(&usb_mouse_driver);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500234 if (retval == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 info(DRIVER_VERSION ":" DRIVER_DESC);
236 return retval;
237}
238
239static void __exit usb_mouse_exit(void)
240{
241 usb_deregister(&usb_mouse_driver);
242}
243
244module_init(usb_mouse_init);
245module_exit(usb_mouse_exit);