]>
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 | 33 | |
8c19a515 JS |
34 | /* for apple IDs */ |
35 | #ifdef CONFIG_USB_HID_MODULE | |
36 | #include "../hid-ids.h" | |
37 | #endif | |
38 | ||
1da177e4 LT |
39 | /* |
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 | ||
47 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
48 | MODULE_DESCRIPTION(DRIVER_DESC); | |
49 | MODULE_LICENSE(DRIVER_LICENSE); | |
50 | ||
51 | struct usb_mouse { | |
52 | char name[128]; | |
53 | char phys[64]; | |
54 | struct usb_device *usbdev; | |
c5b7c7c3 | 55 | struct input_dev *dev; |
1da177e4 | 56 | struct urb *irq; |
1da177e4 LT |
57 | |
58 | signed char *data; | |
59 | dma_addr_t data_dma; | |
60 | }; | |
61 | ||
7d12e780 | 62 | static void usb_mouse_irq(struct urb *urb) |
1da177e4 LT |
63 | { |
64 | struct usb_mouse *mouse = urb->context; | |
65 | signed char *data = mouse->data; | |
c5b7c7c3 | 66 | struct input_dev *dev = mouse->dev; |
1da177e4 LT |
67 | 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 | ||
1da177e4 LT |
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); | |
92 | resubmit: | |
54e6ecb2 | 93 | status = usb_submit_urb (urb, GFP_ATOMIC); |
1da177e4 | 94 | if (status) |
185a8551 GKH |
95 | 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); | |
1da177e4 LT |
99 | } |
100 | ||
101 | static int usb_mouse_open(struct input_dev *dev) | |
102 | { | |
e0712985 | 103 | struct usb_mouse *mouse = input_get_drvdata(dev); |
1da177e4 | 104 | |
1da177e4 | 105 | mouse->irq->dev = mouse->usbdev; |
65cde54b | 106 | if (usb_submit_urb(mouse->irq, GFP_KERNEL)) |
1da177e4 | 107 | return -EIO; |
1da177e4 LT |
108 | |
109 | return 0; | |
110 | } | |
111 | ||
112 | static void usb_mouse_close(struct input_dev *dev) | |
113 | { | |
e0712985 | 114 | struct usb_mouse *mouse = input_get_drvdata(dev); |
1da177e4 | 115 | |
65cde54b | 116 | usb_kill_urb(mouse->irq); |
1da177e4 LT |
117 | } |
118 | ||
c5b7c7c3 | 119 | static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id) |
1da177e4 | 120 | { |
c5b7c7c3 | 121 | struct usb_device *dev = interface_to_usbdev(intf); |
1da177e4 LT |
122 | struct usb_host_interface *interface; |
123 | struct usb_endpoint_descriptor *endpoint; | |
124 | struct usb_mouse *mouse; | |
c5b7c7c3 | 125 | struct input_dev *input_dev; |
1da177e4 | 126 | int pipe, maxp; |
5d6341c6 | 127 | int error = -ENOMEM; |
1da177e4 LT |
128 | |
129 | interface = intf->cur_altsetting; | |
130 | ||
05f091ab | 131 | if (interface->desc.bNumEndpoints != 1) |
1da177e4 LT |
132 | return -ENODEV; |
133 | ||
134 | endpoint = &interface->endpoint[0].desc; | |
30f36ef9 | 135 | if (!usb_endpoint_is_int_in(endpoint)) |
1da177e4 LT |
136 | return -ENODEV; |
137 | ||
138 | pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress); | |
139 | maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe)); | |
140 | ||
c5b7c7c3 DT |
141 | mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL); |
142 | input_dev = input_allocate_device(); | |
143 | if (!mouse || !input_dev) | |
144 | goto fail1; | |
1da177e4 | 145 | |
997ea58e | 146 | mouse->data = usb_alloc_coherent(dev, 8, GFP_ATOMIC, &mouse->data_dma); |
c5b7c7c3 DT |
147 | if (!mouse->data) |
148 | goto fail1; | |
1da177e4 LT |
149 | |
150 | mouse->irq = usb_alloc_urb(0, GFP_KERNEL); | |
c5b7c7c3 DT |
151 | if (!mouse->irq) |
152 | goto fail2; | |
1da177e4 LT |
153 | |
154 | mouse->usbdev = dev; | |
c5b7c7c3 DT |
155 | mouse->dev = input_dev; |
156 | ||
157 | if (dev->manufacturer) | |
158 | strlcpy(mouse->name, dev->manufacturer, sizeof(mouse->name)); | |
1da177e4 | 159 | |
c5b7c7c3 DT |
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 | } | |
1da177e4 | 165 | |
c5b7c7c3 DT |
166 | if (!strlen(mouse->name)) |
167 | 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)); | |
1da177e4 | 171 | |
c5b7c7c3 DT |
172 | usb_make_path(dev, mouse->phys, sizeof(mouse->phys)); |
173 | strlcat(mouse->phys, "/input0", sizeof(mouse->phys)); | |
1da177e4 | 174 | |
c5b7c7c3 DT |
175 | input_dev->name = mouse->name; |
176 | input_dev->phys = mouse->phys; | |
177 | usb_to_input_id(dev, &input_dev->id); | |
e0712985 | 178 | input_dev->dev.parent = &intf->dev; |
1da177e4 | 179 | |
7b19ada2 JS |
180 | 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); | |
1da177e4 | 187 | |
e0712985 DT |
188 | input_set_drvdata(input_dev, mouse); |
189 | ||
c5b7c7c3 DT |
190 | input_dev->open = usb_mouse_open; |
191 | input_dev->close = usb_mouse_close; | |
1da177e4 LT |
192 | |
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 | ||
5d6341c6 DT |
199 | error = input_register_device(mouse->dev); |
200 | if (error) | |
201 | goto fail3; | |
1da177e4 LT |
202 | |
203 | usb_set_intfdata(intf, mouse); | |
204 | return 0; | |
c5b7c7c3 | 205 | |
5d6341c6 DT |
206 | fail3: |
207 | usb_free_urb(mouse->irq); | |
208 | fail2: | |
997ea58e | 209 | usb_free_coherent(dev, 8, mouse->data, mouse->data_dma); |
5d6341c6 DT |
210 | fail1: |
211 | input_free_device(input_dev); | |
c5b7c7c3 | 212 | kfree(mouse); |
5d6341c6 | 213 | return error; |
1da177e4 LT |
214 | } |
215 | ||
216 | static void usb_mouse_disconnect(struct usb_interface *intf) | |
217 | { | |
218 | struct usb_mouse *mouse = usb_get_intfdata (intf); | |
05f091ab | 219 | |
1da177e4 LT |
220 | usb_set_intfdata(intf, NULL); |
221 | if (mouse) { | |
222 | usb_kill_urb(mouse->irq); | |
c5b7c7c3 | 223 | input_unregister_device(mouse->dev); |
1da177e4 | 224 | usb_free_urb(mouse->irq); |
997ea58e | 225 | usb_free_coherent(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma); |
1da177e4 LT |
226 | kfree(mouse); |
227 | } | |
228 | } | |
229 | ||
230 | static struct usb_device_id usb_mouse_id_table [] = { | |
4ef2e23f MO |
231 | { USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT, |
232 | USB_INTERFACE_PROTOCOL_MOUSE) }, | |
14acdcd3 | 233 | { } /* Terminating entry */ |
1da177e4 LT |
234 | }; |
235 | ||
236 | MODULE_DEVICE_TABLE (usb, usb_mouse_id_table); | |
237 | ||
238 | static struct usb_driver usb_mouse_driver = { | |
1da177e4 LT |
239 | .name = "usbmouse", |
240 | .probe = usb_mouse_probe, | |
241 | .disconnect = usb_mouse_disconnect, | |
242 | .id_table = usb_mouse_id_table, | |
243 | }; | |
244 | ||
42f06a13 | 245 | module_usb_driver(usb_mouse_driver); |