Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * 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 | |
05f091ab | 10 | * the Free Software Foundation; either version 2 of the License, or |
1da177e4 | 11 | * (at your option) any later version. |
05f091ab | 12 | * |
1da177e4 LT |
13 | * 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. | |
05f091ab | 17 | * |
1da177e4 LT |
18 | * 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 | |
05f091ab | 21 | * |
1da177e4 LT |
22 | * 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> | |
1da177e4 LT |
29 | #include <linux/module.h> |
30 | #include <linux/init.h> | |
ae0dadcf | 31 | #include <linux/usb/input.h> |
4ef2e23f | 32 | #include <linux/hid.h> |
1da177e4 LT |
33 | |
34 | /* | |
35 | * Version Information | |
36 | */ | |
37 | #define DRIVER_VERSION "v1.6" | |
38 | #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@ucw.cz>" | |
39 | #define DRIVER_DESC "USB HID Boot Protocol mouse driver" | |
40 | #define DRIVER_LICENSE "GPL" | |
41 | ||
42 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
43 | MODULE_DESCRIPTION(DRIVER_DESC); | |
44 | MODULE_LICENSE(DRIVER_LICENSE); | |
45 | ||
46 | struct usb_mouse { | |
47 | char name[128]; | |
48 | char phys[64]; | |
49 | struct usb_device *usbdev; | |
c5b7c7c3 | 50 | struct input_dev *dev; |
1da177e4 | 51 | struct urb *irq; |
1da177e4 LT |
52 | |
53 | signed char *data; | |
54 | dma_addr_t data_dma; | |
55 | }; | |
56 | ||
7d12e780 | 57 | static void usb_mouse_irq(struct urb *urb) |
1da177e4 LT |
58 | { |
59 | struct usb_mouse *mouse = urb->context; | |
60 | signed char *data = mouse->data; | |
c5b7c7c3 | 61 | struct input_dev *dev = mouse->dev; |
1da177e4 LT |
62 | int status; |
63 | ||
64 | switch (urb->status) { | |
65 | case 0: /* success */ | |
66 | break; | |
67 | case -ECONNRESET: /* unlink */ | |
68 | case -ENOENT: | |
69 | case -ESHUTDOWN: | |
70 | return; | |
71 | /* -EPIPE: should clear the halt */ | |
72 | default: /* error */ | |
73 | goto resubmit; | |
74 | } | |
75 | ||
1da177e4 LT |
76 | input_report_key(dev, BTN_LEFT, data[0] & 0x01); |
77 | input_report_key(dev, BTN_RIGHT, data[0] & 0x02); | |
78 | input_report_key(dev, BTN_MIDDLE, data[0] & 0x04); | |
79 | input_report_key(dev, BTN_SIDE, data[0] & 0x08); | |
80 | input_report_key(dev, BTN_EXTRA, data[0] & 0x10); | |
81 | ||
82 | input_report_rel(dev, REL_X, data[1]); | |
83 | input_report_rel(dev, REL_Y, data[2]); | |
84 | input_report_rel(dev, REL_WHEEL, data[3]); | |
85 | ||
86 | input_sync(dev); | |
87 | resubmit: | |
54e6ecb2 | 88 | status = usb_submit_urb (urb, GFP_ATOMIC); |
1da177e4 LT |
89 | if (status) |
90 | err ("can't resubmit intr, %s-%s/input0, status %d", | |
91 | mouse->usbdev->bus->bus_name, | |
92 | mouse->usbdev->devpath, status); | |
93 | } | |
94 | ||
95 | static int usb_mouse_open(struct input_dev *dev) | |
96 | { | |
e0712985 | 97 | struct usb_mouse *mouse = input_get_drvdata(dev); |
1da177e4 | 98 | |
1da177e4 | 99 | mouse->irq->dev = mouse->usbdev; |
65cde54b | 100 | if (usb_submit_urb(mouse->irq, GFP_KERNEL)) |
1da177e4 | 101 | return -EIO; |
1da177e4 LT |
102 | |
103 | return 0; | |
104 | } | |
105 | ||
106 | static void usb_mouse_close(struct input_dev *dev) | |
107 | { | |
e0712985 | 108 | struct usb_mouse *mouse = input_get_drvdata(dev); |
1da177e4 | 109 | |
65cde54b | 110 | usb_kill_urb(mouse->irq); |
1da177e4 LT |
111 | } |
112 | ||
c5b7c7c3 | 113 | static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id) |
1da177e4 | 114 | { |
c5b7c7c3 | 115 | struct usb_device *dev = interface_to_usbdev(intf); |
1da177e4 LT |
116 | struct usb_host_interface *interface; |
117 | struct usb_endpoint_descriptor *endpoint; | |
118 | struct usb_mouse *mouse; | |
c5b7c7c3 | 119 | struct input_dev *input_dev; |
1da177e4 | 120 | int pipe, maxp; |
5d6341c6 | 121 | int error = -ENOMEM; |
1da177e4 LT |
122 | |
123 | interface = intf->cur_altsetting; | |
124 | ||
05f091ab | 125 | if (interface->desc.bNumEndpoints != 1) |
1da177e4 LT |
126 | return -ENODEV; |
127 | ||
128 | endpoint = &interface->endpoint[0].desc; | |
30f36ef9 | 129 | if (!usb_endpoint_is_int_in(endpoint)) |
1da177e4 LT |
130 | return -ENODEV; |
131 | ||
dc25b78f | 132 | #ifdef CONFIG_USB_HID_MODULE |
9f6b3727 PT |
133 | if (usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor), |
134 | le16_to_cpu(dev->descriptor.idProduct)) | |
135 | & (HID_QUIRK_IGNORE|HID_QUIRK_IGNORE_MOUSE)) { | |
136 | return -ENODEV; | |
137 | } | |
b0e66824 | 138 | #endif |
9f6b3727 | 139 | |
1da177e4 LT |
140 | pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress); |
141 | maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe)); | |
142 | ||
c5b7c7c3 DT |
143 | mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL); |
144 | input_dev = input_allocate_device(); | |
145 | if (!mouse || !input_dev) | |
146 | goto fail1; | |
1da177e4 | 147 | |
54e6ecb2 | 148 | mouse->data = usb_buffer_alloc(dev, 8, GFP_ATOMIC, &mouse->data_dma); |
c5b7c7c3 DT |
149 | if (!mouse->data) |
150 | goto fail1; | |
1da177e4 LT |
151 | |
152 | mouse->irq = usb_alloc_urb(0, GFP_KERNEL); | |
c5b7c7c3 DT |
153 | if (!mouse->irq) |
154 | goto fail2; | |
1da177e4 LT |
155 | |
156 | mouse->usbdev = dev; | |
c5b7c7c3 DT |
157 | mouse->dev = input_dev; |
158 | ||
159 | if (dev->manufacturer) | |
160 | strlcpy(mouse->name, dev->manufacturer, sizeof(mouse->name)); | |
1da177e4 | 161 | |
c5b7c7c3 DT |
162 | if (dev->product) { |
163 | if (dev->manufacturer) | |
164 | strlcat(mouse->name, " ", sizeof(mouse->name)); | |
165 | strlcat(mouse->name, dev->product, sizeof(mouse->name)); | |
166 | } | |
1da177e4 | 167 | |
c5b7c7c3 DT |
168 | if (!strlen(mouse->name)) |
169 | snprintf(mouse->name, sizeof(mouse->name), | |
170 | "USB HIDBP Mouse %04x:%04x", | |
171 | le16_to_cpu(dev->descriptor.idVendor), | |
172 | le16_to_cpu(dev->descriptor.idProduct)); | |
1da177e4 | 173 | |
c5b7c7c3 DT |
174 | usb_make_path(dev, mouse->phys, sizeof(mouse->phys)); |
175 | strlcat(mouse->phys, "/input0", sizeof(mouse->phys)); | |
1da177e4 | 176 | |
c5b7c7c3 DT |
177 | input_dev->name = mouse->name; |
178 | input_dev->phys = mouse->phys; | |
179 | usb_to_input_id(dev, &input_dev->id); | |
e0712985 | 180 | input_dev->dev.parent = &intf->dev; |
1da177e4 | 181 | |
7b19ada2 JS |
182 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL); |
183 | input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) | | |
184 | BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE); | |
185 | input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y); | |
186 | input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) | | |
187 | BIT_MASK(BTN_EXTRA); | |
188 | input_dev->relbit[0] |= BIT_MASK(REL_WHEEL); | |
1da177e4 | 189 | |
e0712985 DT |
190 | input_set_drvdata(input_dev, mouse); |
191 | ||
c5b7c7c3 DT |
192 | input_dev->open = usb_mouse_open; |
193 | input_dev->close = usb_mouse_close; | |
1da177e4 LT |
194 | |
195 | usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data, | |
196 | (maxp > 8 ? 8 : maxp), | |
197 | usb_mouse_irq, mouse, endpoint->bInterval); | |
198 | mouse->irq->transfer_dma = mouse->data_dma; | |
199 | mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
200 | ||
5d6341c6 DT |
201 | error = input_register_device(mouse->dev); |
202 | if (error) | |
203 | goto fail3; | |
1da177e4 LT |
204 | |
205 | usb_set_intfdata(intf, mouse); | |
206 | return 0; | |
c5b7c7c3 | 207 | |
5d6341c6 DT |
208 | fail3: |
209 | usb_free_urb(mouse->irq); | |
210 | fail2: | |
211 | usb_buffer_free(dev, 8, mouse->data, mouse->data_dma); | |
212 | fail1: | |
213 | input_free_device(input_dev); | |
c5b7c7c3 | 214 | kfree(mouse); |
5d6341c6 | 215 | return error; |
1da177e4 LT |
216 | } |
217 | ||
218 | static void usb_mouse_disconnect(struct usb_interface *intf) | |
219 | { | |
220 | struct usb_mouse *mouse = usb_get_intfdata (intf); | |
05f091ab | 221 | |
1da177e4 LT |
222 | usb_set_intfdata(intf, NULL); |
223 | if (mouse) { | |
224 | usb_kill_urb(mouse->irq); | |
c5b7c7c3 | 225 | input_unregister_device(mouse->dev); |
1da177e4 LT |
226 | usb_free_urb(mouse->irq); |
227 | usb_buffer_free(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma); | |
228 | kfree(mouse); | |
229 | } | |
230 | } | |
231 | ||
232 | static struct usb_device_id usb_mouse_id_table [] = { | |
4ef2e23f MO |
233 | { USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT, |
234 | USB_INTERFACE_PROTOCOL_MOUSE) }, | |
14acdcd3 | 235 | { } /* Terminating entry */ |
1da177e4 LT |
236 | }; |
237 | ||
238 | MODULE_DEVICE_TABLE (usb, usb_mouse_id_table); | |
239 | ||
240 | static struct usb_driver usb_mouse_driver = { | |
1da177e4 LT |
241 | .name = "usbmouse", |
242 | .probe = usb_mouse_probe, | |
243 | .disconnect = usb_mouse_disconnect, | |
244 | .id_table = usb_mouse_id_table, | |
245 | }; | |
246 | ||
247 | static int __init usb_mouse_init(void) | |
248 | { | |
249 | int retval = usb_register(&usb_mouse_driver); | |
05f091ab | 250 | if (retval == 0) |
1da177e4 LT |
251 | info(DRIVER_VERSION ":" DRIVER_DESC); |
252 | return retval; | |
253 | } | |
254 | ||
255 | static void __exit usb_mouse_exit(void) | |
256 | { | |
257 | usb_deregister(&usb_mouse_driver); | |
258 | } | |
259 | ||
260 | module_init(usb_mouse_init); | |
261 | module_exit(usb_mouse_exit); |