1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 1999-2001 Vojtech Pavlik
5 * USB HIDBP Mouse support
10 * Should you need to contact me, the author, you can do so either by
11 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
12 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/usb/input.h>
20 #include <linux/hid.h>
23 #ifdef CONFIG_USB_HID_MODULE
24 #include "../hid-ids.h"
30 #define DRIVER_VERSION "v1.6"
31 #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@ucw.cz>"
32 #define DRIVER_DESC "USB HID Boot Protocol mouse driver"
34 MODULE_AUTHOR(DRIVER_AUTHOR
);
35 MODULE_DESCRIPTION(DRIVER_DESC
);
36 MODULE_LICENSE("GPL");
41 struct usb_device
*usbdev
;
42 struct input_dev
*dev
;
49 static void usb_mouse_irq(struct urb
*urb
)
51 struct usb_mouse
*mouse
= urb
->context
;
52 signed char *data
= mouse
->data
;
53 struct input_dev
*dev
= mouse
->dev
;
56 switch (urb
->status
) {
59 case -ECONNRESET
: /* unlink */
63 /* -EPIPE: should clear the halt */
68 input_report_key(dev
, BTN_LEFT
, data
[0] & 0x01);
69 input_report_key(dev
, BTN_RIGHT
, data
[0] & 0x02);
70 input_report_key(dev
, BTN_MIDDLE
, data
[0] & 0x04);
71 input_report_key(dev
, BTN_SIDE
, data
[0] & 0x08);
72 input_report_key(dev
, BTN_EXTRA
, data
[0] & 0x10);
74 input_report_rel(dev
, REL_X
, data
[1]);
75 input_report_rel(dev
, REL_Y
, data
[2]);
76 input_report_rel(dev
, REL_WHEEL
, data
[3]);
80 status
= usb_submit_urb (urb
, GFP_ATOMIC
);
82 dev_err(&mouse
->usbdev
->dev
,
83 "can't resubmit intr, %s-%s/input0, status %d\n",
84 mouse
->usbdev
->bus
->bus_name
,
85 mouse
->usbdev
->devpath
, status
);
88 static int usb_mouse_open(struct input_dev
*dev
)
90 struct usb_mouse
*mouse
= input_get_drvdata(dev
);
92 mouse
->irq
->dev
= mouse
->usbdev
;
93 if (usb_submit_urb(mouse
->irq
, GFP_KERNEL
))
99 static void usb_mouse_close(struct input_dev
*dev
)
101 struct usb_mouse
*mouse
= input_get_drvdata(dev
);
103 usb_kill_urb(mouse
->irq
);
106 static int usb_mouse_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
108 struct usb_device
*dev
= interface_to_usbdev(intf
);
109 struct usb_host_interface
*interface
;
110 struct usb_endpoint_descriptor
*endpoint
;
111 struct usb_mouse
*mouse
;
112 struct input_dev
*input_dev
;
116 interface
= intf
->cur_altsetting
;
118 if (interface
->desc
.bNumEndpoints
!= 1)
121 endpoint
= &interface
->endpoint
[0].desc
;
122 if (!usb_endpoint_is_int_in(endpoint
))
125 pipe
= usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
);
126 maxp
= usb_maxpacket(dev
, pipe
, usb_pipeout(pipe
));
128 mouse
= kzalloc(sizeof(struct usb_mouse
), GFP_KERNEL
);
129 input_dev
= input_allocate_device();
130 if (!mouse
|| !input_dev
)
133 mouse
->data
= usb_alloc_coherent(dev
, 8, GFP_ATOMIC
, &mouse
->data_dma
);
137 mouse
->irq
= usb_alloc_urb(0, GFP_KERNEL
);
142 mouse
->dev
= input_dev
;
144 if (dev
->manufacturer
)
145 strlcpy(mouse
->name
, dev
->manufacturer
, sizeof(mouse
->name
));
148 if (dev
->manufacturer
)
149 strlcat(mouse
->name
, " ", sizeof(mouse
->name
));
150 strlcat(mouse
->name
, dev
->product
, sizeof(mouse
->name
));
153 if (!strlen(mouse
->name
))
154 snprintf(mouse
->name
, sizeof(mouse
->name
),
155 "USB HIDBP Mouse %04x:%04x",
156 le16_to_cpu(dev
->descriptor
.idVendor
),
157 le16_to_cpu(dev
->descriptor
.idProduct
));
159 usb_make_path(dev
, mouse
->phys
, sizeof(mouse
->phys
));
160 strlcat(mouse
->phys
, "/input0", sizeof(mouse
->phys
));
162 input_dev
->name
= mouse
->name
;
163 input_dev
->phys
= mouse
->phys
;
164 usb_to_input_id(dev
, &input_dev
->id
);
165 input_dev
->dev
.parent
= &intf
->dev
;
167 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REL
);
168 input_dev
->keybit
[BIT_WORD(BTN_MOUSE
)] = BIT_MASK(BTN_LEFT
) |
169 BIT_MASK(BTN_RIGHT
) | BIT_MASK(BTN_MIDDLE
);
170 input_dev
->relbit
[0] = BIT_MASK(REL_X
) | BIT_MASK(REL_Y
);
171 input_dev
->keybit
[BIT_WORD(BTN_MOUSE
)] |= BIT_MASK(BTN_SIDE
) |
173 input_dev
->relbit
[0] |= BIT_MASK(REL_WHEEL
);
175 input_set_drvdata(input_dev
, mouse
);
177 input_dev
->open
= usb_mouse_open
;
178 input_dev
->close
= usb_mouse_close
;
180 usb_fill_int_urb(mouse
->irq
, dev
, pipe
, mouse
->data
,
181 (maxp
> 8 ? 8 : maxp
),
182 usb_mouse_irq
, mouse
, endpoint
->bInterval
);
183 mouse
->irq
->transfer_dma
= mouse
->data_dma
;
184 mouse
->irq
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
186 error
= input_register_device(mouse
->dev
);
190 usb_set_intfdata(intf
, mouse
);
194 usb_free_urb(mouse
->irq
);
196 usb_free_coherent(dev
, 8, mouse
->data
, mouse
->data_dma
);
198 input_free_device(input_dev
);
203 static void usb_mouse_disconnect(struct usb_interface
*intf
)
205 struct usb_mouse
*mouse
= usb_get_intfdata (intf
);
207 usb_set_intfdata(intf
, NULL
);
209 usb_kill_urb(mouse
->irq
);
210 input_unregister_device(mouse
->dev
);
211 usb_free_urb(mouse
->irq
);
212 usb_free_coherent(interface_to_usbdev(intf
), 8, mouse
->data
, mouse
->data_dma
);
217 static const struct usb_device_id usb_mouse_id_table
[] = {
218 { USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID
, USB_INTERFACE_SUBCLASS_BOOT
,
219 USB_INTERFACE_PROTOCOL_MOUSE
) },
220 { } /* Terminating entry */
223 MODULE_DEVICE_TABLE (usb
, usb_mouse_id_table
);
225 static struct usb_driver usb_mouse_driver
= {
227 .probe
= usb_mouse_probe
,
228 .disconnect
= usb_mouse_disconnect
,
229 .id_table
= usb_mouse_id_table
,
232 module_usb_driver(usb_mouse_driver
);