1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/kernel.h>
28#include <linux/slab.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/usb/input.h>
32#include <linux/hid.h>
33
34
35#ifdef CONFIG_USB_HID_MODULE
36#include "../hid-ids.h"
37#endif
38
39
40
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
46MODULE_AUTHOR(DRIVER_AUTHOR);
47MODULE_DESCRIPTION(DRIVER_DESC);
48MODULE_LICENSE("GPL");
49
50struct usb_mouse {
51 char name[128];
52 char phys[64];
53 struct usb_device *usbdev;
54 struct input_dev *dev;
55 struct urb *irq;
56
57 signed char *data;
58 dma_addr_t data_dma;
59};
60
61static void usb_mouse_irq(struct urb *urb)
62{
63 struct usb_mouse *mouse = urb->context;
64 signed char *data = mouse->data;
65 struct input_dev *dev = mouse->dev;
66 int status;
67
68 switch (urb->status) {
69 case 0:
70 break;
71 case -ECONNRESET:
72 case -ENOENT:
73 case -ESHUTDOWN:
74 return;
75
76 default:
77 goto resubmit;
78 }
79
80 input_report_key(dev, BTN_LEFT, data[0] & 0x01);
81 input_report_key(dev, BTN_RIGHT, data[0] & 0x02);
82 input_report_key(dev, BTN_MIDDLE, data[0] & 0x04);
83 input_report_key(dev, BTN_SIDE, data[0] & 0x08);
84 input_report_key(dev, BTN_EXTRA, data[0] & 0x10);
85
86 input_report_rel(dev, REL_X, data[1]);
87 input_report_rel(dev, REL_Y, data[2]);
88 input_report_rel(dev, REL_WHEEL, data[3]);
89
90 input_sync(dev);
91resubmit:
92 status = usb_submit_urb (urb, GFP_ATOMIC);
93 if (status)
94 dev_err(&mouse->usbdev->dev,
95 "can't resubmit intr, %s-%s/input0, status %d\n",
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 = input_get_drvdata(dev);
103
104 mouse->irq->dev = mouse->usbdev;
105 if (usb_submit_urb(mouse->irq, GFP_KERNEL))
106 return -EIO;
107
108 return 0;
109}
110
111static void usb_mouse_close(struct input_dev *dev)
112{
113 struct usb_mouse *mouse = input_get_drvdata(dev);
114
115 usb_kill_urb(mouse->irq);
116}
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 struct input_dev *input_dev;
125 int pipe, maxp;
126 int error = -ENOMEM;
127
128 interface = intf->cur_altsetting;
129
130 if (interface->desc.bNumEndpoints != 1)
131 return -ENODEV;
132
133 endpoint = &interface->endpoint[0].desc;
134 if (!usb_endpoint_is_int_in(endpoint))
135 return -ENODEV;
136
137 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
138 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
139
140 mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL);
141 input_dev = input_allocate_device();
142 if (!mouse || !input_dev)
143 goto fail1;
144
145 mouse->data = usb_alloc_coherent(dev, 8, GFP_ATOMIC, &mouse->data_dma);
146 if (!mouse->data)
147 goto fail1;
148
149 mouse->irq = usb_alloc_urb(0, GFP_KERNEL);
150 if (!mouse->irq)
151 goto fail2;
152
153 mouse->usbdev = dev;
154 mouse->dev = input_dev;
155
156 if (dev->manufacturer)
157 strlcpy(mouse->name, dev->manufacturer, sizeof(mouse->name));
158
159 if (dev->product) {
160 if (dev->manufacturer)
161 strlcat(mouse->name, " ", sizeof(mouse->name));
162 strlcat(mouse->name, dev->product, sizeof(mouse->name));
163 }
164
165 if (!strlen(mouse->name))
166 snprintf(mouse->name, sizeof(mouse->name),
167 "USB HIDBP Mouse %04x:%04x",
168 le16_to_cpu(dev->descriptor.idVendor),
169 le16_to_cpu(dev->descriptor.idProduct));
170
171 usb_make_path(dev, mouse->phys, sizeof(mouse->phys));
172 strlcat(mouse->phys, "/input0", sizeof(mouse->phys));
173
174 input_dev->name = mouse->name;
175 input_dev->phys = mouse->phys;
176 usb_to_input_id(dev, &input_dev->id);
177 input_dev->dev.parent = &intf->dev;
178
179 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
180 input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
181 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
182 input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
183 input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) |
184 BIT_MASK(BTN_EXTRA);
185 input_dev->relbit[0] |= BIT_MASK(REL_WHEEL);
186
187 input_set_drvdata(input_dev, mouse);
188
189 input_dev->open = usb_mouse_open;
190 input_dev->close = usb_mouse_close;
191
192 usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,
193 (maxp > 8 ? 8 : maxp),
194 usb_mouse_irq, mouse, endpoint->bInterval);
195 mouse->irq->transfer_dma = mouse->data_dma;
196 mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
197
198 error = input_register_device(mouse->dev);
199 if (error)
200 goto fail3;
201
202 usb_set_intfdata(intf, mouse);
203 return 0;
204
205fail3:
206 usb_free_urb(mouse->irq);
207fail2:
208 usb_free_coherent(dev, 8, mouse->data, mouse->data_dma);
209fail1:
210 input_free_device(input_dev);
211 kfree(mouse);
212 return error;
213}
214
215static void usb_mouse_disconnect(struct usb_interface *intf)
216{
217 struct usb_mouse *mouse = usb_get_intfdata (intf);
218
219 usb_set_intfdata(intf, NULL);
220 if (mouse) {
221 usb_kill_urb(mouse->irq);
222 input_unregister_device(mouse->dev);
223 usb_free_urb(mouse->irq);
224 usb_free_coherent(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma);
225 kfree(mouse);
226 }
227}
228
229static const struct usb_device_id usb_mouse_id_table[] = {
230 { USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,
231 USB_INTERFACE_PROTOCOL_MOUSE) },
232 { }
233};
234
235MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
236
237static struct usb_driver usb_mouse_driver = {
238 .name = "usbmouse",
239 .probe = usb_mouse_probe,
240 .disconnect = usb_mouse_disconnect,
241 .id_table = usb_mouse_id_table,
242};
243
244module_usb_driver(usb_mouse_driver);
245