blob: 64a33e420cfbabb3b6758f25353c67b4b02ee55e [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 Torokhov05f091a2005-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 Torokhov05f091a2005-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 Torokhov05f091a2005-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 Torokhov05f091a2005-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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/init.h>
David Brownellae0dadc2006-06-13 10:04:34 -070033#include <linux/usb/input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
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
43MODULE_AUTHOR(DRIVER_AUTHOR);
44MODULE_DESCRIPTION(DRIVER_DESC);
45MODULE_LICENSE(DRIVER_LICENSE);
46
47struct usb_mouse {
48 char name[128];
49 char phys[64];
50 struct usb_device *usbdev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050051 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct urb *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 signed char *data;
55 dma_addr_t data_dma;
56};
57
David Howells7d12e782006-10-05 14:55:46 +010058static void usb_mouse_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 struct usb_mouse *mouse = urb->context;
61 signed char *data = mouse->data;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050062 struct input_dev *dev = mouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 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);
88resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -080089 status = usb_submit_urb (urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 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
96static int usb_mouse_open(struct input_dev *dev)
97{
98 struct usb_mouse *mouse = dev->private;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 mouse->irq->dev = mouse->usbdev;
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500101 if (usb_submit_urb(mouse->irq, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 return 0;
105}
106
107static void usb_mouse_close(struct input_dev *dev)
108{
109 struct usb_mouse *mouse = dev->private;
110
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500111 usb_kill_urb(mouse->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500114static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500116 struct usb_device *dev = interface_to_usbdev(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 struct usb_host_interface *interface;
118 struct usb_endpoint_descriptor *endpoint;
119 struct usb_mouse *mouse;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500120 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 int pipe, maxp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 interface = intf->cur_altsetting;
124
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500125 if (interface->desc.bNumEndpoints != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return -ENODEV;
127
128 endpoint = &interface->endpoint[0].desc;
Luiz Fernando N. Capitulino30f36ef2006-10-26 13:03:00 -0300129 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -ENODEV;
131
132 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
133 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
134
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500135 mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL);
136 input_dev = input_allocate_device();
137 if (!mouse || !input_dev)
138 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800140 mouse->data = usb_buffer_alloc(dev, 8, GFP_ATOMIC, &mouse->data_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500141 if (!mouse->data)
142 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 mouse->irq = usb_alloc_urb(0, GFP_KERNEL);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500145 if (!mouse->irq)
146 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 mouse->usbdev = dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500149 mouse->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 if (dev->manufacturer)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500152 strlcpy(mouse->name, dev->manufacturer, sizeof(mouse->name));
153
154 if (dev->product) {
155 if (dev->manufacturer)
156 strlcat(mouse->name, " ", sizeof(mouse->name));
157 strlcat(mouse->name, dev->product, sizeof(mouse->name));
158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 if (!strlen(mouse->name))
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500161 snprintf(mouse->name, sizeof(mouse->name),
162 "USB HIDBP Mouse %04x:%04x",
163 le16_to_cpu(dev->descriptor.idVendor),
164 le16_to_cpu(dev->descriptor.idProduct));
165
166 usb_make_path(dev, mouse->phys, sizeof(mouse->phys));
167 strlcat(mouse->phys, "/input0", sizeof(mouse->phys));
168
169 input_dev->name = mouse->name;
170 input_dev->phys = mouse->phys;
171 usb_to_input_id(dev, &input_dev->id);
172 input_dev->cdev.dev = &intf->dev;
173
174 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
175 input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
176 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
177 input_dev->keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);
178 input_dev->relbit[0] |= BIT(REL_WHEEL);
179
180 input_dev->private = mouse;
181 input_dev->open = usb_mouse_open;
182 input_dev->close = usb_mouse_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,
185 (maxp > 8 ? 8 : maxp),
186 usb_mouse_irq, mouse, endpoint->bInterval);
187 mouse->irq->transfer_dma = mouse->data_dma;
188 mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
189
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500190 input_register_device(mouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 usb_set_intfdata(intf, mouse);
193 return 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500194
195fail2: usb_buffer_free(dev, 8, mouse->data, mouse->data_dma);
196fail1: input_free_device(input_dev);
197 kfree(mouse);
198 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201static void usb_mouse_disconnect(struct usb_interface *intf)
202{
203 struct usb_mouse *mouse = usb_get_intfdata (intf);
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 usb_set_intfdata(intf, NULL);
206 if (mouse) {
207 usb_kill_urb(mouse->irq);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500208 input_unregister_device(mouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 usb_free_urb(mouse->irq);
210 usb_buffer_free(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma);
211 kfree(mouse);
212 }
213}
214
215static struct usb_device_id usb_mouse_id_table [] = {
216 { USB_INTERFACE_INFO(3, 1, 2) },
Reiner Herrmann14acdcd2006-09-06 02:37:21 +0200217 { } /* Terminating entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219
220MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
221
222static struct usb_driver usb_mouse_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 .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 Torokhov05f091a2005-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);