blob: bf16d72dc3700f0b5feec7d6c60bd6942b4759b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (c) 1999-2001 Vojtech Pavlik
3 *
4 * USB HIDBP Mouse support
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050010 * the Free Software Foundation; either version 2 of the License, or
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * (at your option) any later version.
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050017 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050021 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * Should you need to contact me, the author, you can do so either by
23 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
24 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
25 */
26
27#include <linux/kernel.h>
28#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/init.h>
David Brownellae0dadc2006-06-13 10:04:34 -070031#include <linux/usb/input.h>
Michael Opdenacker4ef2e232007-02-21 22:51:25 +010032#include <linux/hid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jiri Slaby8c19a512008-06-18 23:36:49 +020034/* for apple IDs */
35#ifdef CONFIG_USB_HID_MODULE
36#include "../hid-ids.h"
37#endif
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
40 * Version Information
41 */
42#define DRIVER_VERSION "v1.6"
43#define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@ucw.cz>"
44#define DRIVER_DESC "USB HID Boot Protocol mouse driver"
45#define DRIVER_LICENSE "GPL"
46
47MODULE_AUTHOR(DRIVER_AUTHOR);
48MODULE_DESCRIPTION(DRIVER_DESC);
49MODULE_LICENSE(DRIVER_LICENSE);
50
51struct usb_mouse {
52 char name[128];
53 char phys[64];
54 struct usb_device *usbdev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050055 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 struct urb *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 signed char *data;
59 dma_addr_t data_dma;
60};
61
David Howells7d12e782006-10-05 14:55:46 +010062static void usb_mouse_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 struct usb_mouse *mouse = urb->context;
65 signed char *data = mouse->data;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050066 struct input_dev *dev = mouse->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 int status;
68
69 switch (urb->status) {
70 case 0: /* success */
71 break;
72 case -ECONNRESET: /* unlink */
73 case -ENOENT:
74 case -ESHUTDOWN:
75 return;
76 /* -EPIPE: should clear the halt */
77 default: /* error */
78 goto resubmit;
79 }
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 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:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -080093 status = usb_submit_urb (urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (status)
Greg Kroah-Hartman185a8552012-04-25 14:48:20 -070095 dev_err(&mouse->usbdev->dev,
96 "can't resubmit intr, %s-%s/input0, status %d\n",
97 mouse->usbdev->bus->bus_name,
98 mouse->usbdev->devpath, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static int usb_mouse_open(struct input_dev *dev)
102{
Dmitry Torokhove0712982007-05-09 10:17:31 +0200103 struct usb_mouse *mouse = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 mouse->irq->dev = mouse->usbdev;
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500106 if (usb_submit_urb(mouse->irq, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 return 0;
110}
111
112static void usb_mouse_close(struct input_dev *dev)
113{
Dmitry Torokhove0712982007-05-09 10:17:31 +0200114 struct usb_mouse *mouse = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500116 usb_kill_urb(mouse->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500119static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500121 struct usb_device *dev = interface_to_usbdev(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 struct usb_host_interface *interface;
123 struct usb_endpoint_descriptor *endpoint;
124 struct usb_mouse *mouse;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500125 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int pipe, maxp;
Dmitry Torokhov5d6341c2007-04-04 10:40:57 +0200127 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 interface = intf->cur_altsetting;
130
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500131 if (interface->desc.bNumEndpoints != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return -ENODEV;
133
134 endpoint = &interface->endpoint[0].desc;
Luiz Fernando N. Capitulino30f36ef2006-10-26 13:03:00 -0300135 if (!usb_endpoint_is_int_in(endpoint))
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 Torokhovc5b7c7c2005-09-15 02:01:47 -0500141 mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL);
142 input_dev = input_allocate_device();
143 if (!mouse || !input_dev)
144 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Daniel Mack997ea582010-04-12 13:17:25 +0200146 mouse->data = usb_alloc_coherent(dev, 8, GFP_ATOMIC, &mouse->data_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500147 if (!mouse->data)
148 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 mouse->irq = usb_alloc_urb(0, GFP_KERNEL);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500151 if (!mouse->irq)
152 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 mouse->usbdev = dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500155 mouse->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 if (dev->manufacturer)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500158 strlcpy(mouse->name, dev->manufacturer, sizeof(mouse->name));
159
160 if (dev->product) {
161 if (dev->manufacturer)
162 strlcat(mouse->name, " ", sizeof(mouse->name));
163 strlcat(mouse->name, dev->product, sizeof(mouse->name));
164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 if (!strlen(mouse->name))
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500167 snprintf(mouse->name, sizeof(mouse->name),
168 "USB HIDBP Mouse %04x:%04x",
169 le16_to_cpu(dev->descriptor.idVendor),
170 le16_to_cpu(dev->descriptor.idProduct));
171
172 usb_make_path(dev, mouse->phys, sizeof(mouse->phys));
173 strlcat(mouse->phys, "/input0", sizeof(mouse->phys));
174
175 input_dev->name = mouse->name;
176 input_dev->phys = mouse->phys;
177 usb_to_input_id(dev, &input_dev->id);
Dmitry Torokhove0712982007-05-09 10:17:31 +0200178 input_dev->dev.parent = &intf->dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500179
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700180 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
181 input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
182 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
183 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
184 input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) |
185 BIT_MASK(BTN_EXTRA);
186 input_dev->relbit[0] |= BIT_MASK(REL_WHEEL);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500187
Dmitry Torokhove0712982007-05-09 10:17:31 +0200188 input_set_drvdata(input_dev, mouse);
189
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500190 input_dev->open = usb_mouse_open;
191 input_dev->close = usb_mouse_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,
194 (maxp > 8 ? 8 : maxp),
195 usb_mouse_irq, mouse, endpoint->bInterval);
196 mouse->irq->transfer_dma = mouse->data_dma;
197 mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
198
Dmitry Torokhov5d6341c2007-04-04 10:40:57 +0200199 error = input_register_device(mouse->dev);
200 if (error)
201 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 usb_set_intfdata(intf, mouse);
204 return 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500205
Dmitry Torokhov5d6341c2007-04-04 10:40:57 +0200206fail3:
207 usb_free_urb(mouse->irq);
208fail2:
Daniel Mack997ea582010-04-12 13:17:25 +0200209 usb_free_coherent(dev, 8, mouse->data, mouse->data_dma);
Dmitry Torokhov5d6341c2007-04-04 10:40:57 +0200210fail1:
211 input_free_device(input_dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500212 kfree(mouse);
Dmitry Torokhov5d6341c2007-04-04 10:40:57 +0200213 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216static void usb_mouse_disconnect(struct usb_interface *intf)
217{
218 struct usb_mouse *mouse = usb_get_intfdata (intf);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 usb_set_intfdata(intf, NULL);
221 if (mouse) {
222 usb_kill_urb(mouse->irq);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500223 input_unregister_device(mouse->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 usb_free_urb(mouse->irq);
Daniel Mack997ea582010-04-12 13:17:25 +0200225 usb_free_coherent(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 kfree(mouse);
227 }
228}
229
230static struct usb_device_id usb_mouse_id_table [] = {
Michael Opdenacker4ef2e232007-02-21 22:51:25 +0100231 { USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,
232 USB_INTERFACE_PROTOCOL_MOUSE) },
Reiner Herrmann14acdcd2006-09-06 02:37:21 +0200233 { } /* Terminating entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
236MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
237
238static struct usb_driver usb_mouse_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 .name = "usbmouse",
240 .probe = usb_mouse_probe,
241 .disconnect = usb_mouse_disconnect,
242 .id_table = usb_mouse_id_table,
243};
244
Greg Kroah-Hartman42f06a12011-11-18 09:49:34 -0800245module_usb_driver(usb_mouse_driver);