blob: 4104dec847fbad4248137ee0a9ffebf7f530b20d [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>
Dmitry Torokhov16a334c2005-06-30 00:49:08 -050035#include <linux/usb_input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
38 * Version Information
39 */
40#define DRIVER_VERSION "v1.6"
41#define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@ucw.cz>"
42#define DRIVER_DESC "USB HID Boot Protocol mouse driver"
43#define DRIVER_LICENSE "GPL"
44
45MODULE_AUTHOR(DRIVER_AUTHOR);
46MODULE_DESCRIPTION(DRIVER_DESC);
47MODULE_LICENSE(DRIVER_LICENSE);
48
49struct usb_mouse {
50 char name[128];
51 char phys[64];
52 struct usb_device *usbdev;
53 struct input_dev dev;
54 struct urb *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 signed char *data;
57 dma_addr_t data_dma;
58};
59
60static void usb_mouse_irq(struct urb *urb, struct pt_regs *regs)
61{
62 struct usb_mouse *mouse = urb->context;
63 signed char *data = mouse->data;
64 struct input_dev *dev = &mouse->dev;
65 int status;
66
67 switch (urb->status) {
68 case 0: /* success */
69 break;
70 case -ECONNRESET: /* unlink */
71 case -ENOENT:
72 case -ESHUTDOWN:
73 return;
74 /* -EPIPE: should clear the halt */
75 default: /* error */
76 goto resubmit;
77 }
78
79 input_regs(dev, regs);
80
81 input_report_key(dev, BTN_LEFT, data[0] & 0x01);
82 input_report_key(dev, BTN_RIGHT, data[0] & 0x02);
83 input_report_key(dev, BTN_MIDDLE, data[0] & 0x04);
84 input_report_key(dev, BTN_SIDE, data[0] & 0x08);
85 input_report_key(dev, BTN_EXTRA, data[0] & 0x10);
86
87 input_report_rel(dev, REL_X, data[1]);
88 input_report_rel(dev, REL_Y, data[2]);
89 input_report_rel(dev, REL_WHEEL, data[3]);
90
91 input_sync(dev);
92resubmit:
93 status = usb_submit_urb (urb, SLAB_ATOMIC);
94 if (status)
95 err ("can't resubmit intr, %s-%s/input0, status %d",
96 mouse->usbdev->bus->bus_name,
97 mouse->usbdev->devpath, status);
98}
99
100static int usb_mouse_open(struct input_dev *dev)
101{
102 struct usb_mouse *mouse = dev->private;
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 mouse->irq->dev = mouse->usbdev;
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500105 if (usb_submit_urb(mouse->irq, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 return 0;
109}
110
111static void usb_mouse_close(struct input_dev *dev)
112{
113 struct usb_mouse *mouse = dev->private;
114
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500115 usb_kill_urb(mouse->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118static int usb_mouse_probe(struct usb_interface * intf, const struct usb_device_id * id)
119{
120 struct usb_device * dev = interface_to_usbdev(intf);
121 struct usb_host_interface *interface;
122 struct usb_endpoint_descriptor *endpoint;
123 struct usb_mouse *mouse;
124 int pipe, maxp;
125 char path[64];
126
127 interface = intf->cur_altsetting;
128
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500129 if (interface->desc.bNumEndpoints != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -ENODEV;
131
132 endpoint = &interface->endpoint[0].desc;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500133 if (!(endpoint->bEndpointAddress & 0x80))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -ENODEV;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500135 if ((endpoint->bmAttributes & 3) != 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return -ENODEV;
137
138 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
139 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
140
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500141 if (!(mouse = kmalloc(sizeof(struct usb_mouse), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return -ENOMEM;
143 memset(mouse, 0, sizeof(struct usb_mouse));
144
145 mouse->data = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &mouse->data_dma);
146 if (!mouse->data) {
147 kfree(mouse);
148 return -ENOMEM;
149 }
150
151 mouse->irq = usb_alloc_urb(0, GFP_KERNEL);
152 if (!mouse->irq) {
153 usb_buffer_free(dev, 8, mouse->data, mouse->data_dma);
154 kfree(mouse);
155 return -ENODEV;
156 }
157
158 mouse->usbdev = dev;
159
160 mouse->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
161 mouse->dev.keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
162 mouse->dev.relbit[0] = BIT(REL_X) | BIT(REL_Y);
163 mouse->dev.keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);
164 mouse->dev.relbit[0] |= BIT(REL_WHEEL);
165
166 mouse->dev.private = mouse;
167 mouse->dev.open = usb_mouse_open;
168 mouse->dev.close = usb_mouse_close;
169
170 usb_make_path(dev, path, 64);
171 sprintf(mouse->phys, "%s/input0", path);
172
173 mouse->dev.name = mouse->name;
174 mouse->dev.phys = mouse->phys;
Dmitry Torokhov16a334c2005-06-30 00:49:08 -0500175 usb_to_input_id(dev, &mouse->dev.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 mouse->dev.dev = &intf->dev;
177
178 if (dev->manufacturer)
179 strcat(mouse->name, dev->manufacturer);
180 if (dev->product)
181 sprintf(mouse->name, "%s %s", mouse->name, dev->product);
182
183 if (!strlen(mouse->name))
184 sprintf(mouse->name, "USB HIDBP Mouse %04x:%04x",
185 mouse->dev.id.vendor, mouse->dev.id.product);
186
187 usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,
188 (maxp > 8 ? 8 : maxp),
189 usb_mouse_irq, mouse, endpoint->bInterval);
190 mouse->irq->transfer_dma = mouse->data_dma;
191 mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
192
193 input_register_device(&mouse->dev);
194 printk(KERN_INFO "input: %s on %s\n", mouse->name, path);
195
196 usb_set_intfdata(intf, mouse);
197 return 0;
198}
199
200static void usb_mouse_disconnect(struct usb_interface *intf)
201{
202 struct usb_mouse *mouse = usb_get_intfdata (intf);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 usb_set_intfdata(intf, NULL);
205 if (mouse) {
206 usb_kill_urb(mouse->irq);
207 input_unregister_device(&mouse->dev);
208 usb_free_urb(mouse->irq);
209 usb_buffer_free(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma);
210 kfree(mouse);
211 }
212}
213
214static struct usb_device_id usb_mouse_id_table [] = {
215 { USB_INTERFACE_INFO(3, 1, 2) },
216 { } /* Terminating entry */
217};
218
219MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
220
221static struct usb_driver usb_mouse_driver = {
222 .owner = THIS_MODULE,
223 .name = "usbmouse",
224 .probe = usb_mouse_probe,
225 .disconnect = usb_mouse_disconnect,
226 .id_table = usb_mouse_id_table,
227};
228
229static int __init usb_mouse_init(void)
230{
231 int retval = usb_register(&usb_mouse_driver);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500232 if (retval == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 info(DRIVER_VERSION ":" DRIVER_DESC);
234 return retval;
235}
236
237static void __exit usb_mouse_exit(void)
238{
239 usb_deregister(&usb_mouse_driver);
240}
241
242module_init(usb_mouse_init);
243module_exit(usb_mouse_exit);