2 * Copyright (c) 1999-2001 Vojtech Pavlik
4 * USB HIDBP Mouse support
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
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
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.
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
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
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>
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"
42 MODULE_AUTHOR(DRIVER_AUTHOR
);
43 MODULE_DESCRIPTION(DRIVER_DESC
);
44 MODULE_LICENSE(DRIVER_LICENSE
);
49 struct usb_device
*usbdev
;
50 struct input_dev
*dev
;
57 static void usb_mouse_irq(struct urb
*urb
)
59 struct usb_mouse
*mouse
= urb
->context
;
60 signed char *data
= mouse
->data
;
61 struct input_dev
*dev
= mouse
->dev
;
64 switch (urb
->status
) {
67 case -ECONNRESET
: /* unlink */
71 /* -EPIPE: should clear the halt */
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);
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]);
88 status
= usb_submit_urb (urb
, GFP_ATOMIC
);
90 err ("can't resubmit intr, %s-%s/input0, status %d",
91 mouse
->usbdev
->bus
->bus_name
,
92 mouse
->usbdev
->devpath
, status
);
95 static int usb_mouse_open(struct input_dev
*dev
)
97 struct usb_mouse
*mouse
= input_get_drvdata(dev
);
99 mouse
->irq
->dev
= mouse
->usbdev
;
100 if (usb_submit_urb(mouse
->irq
, GFP_KERNEL
))
106 static void usb_mouse_close(struct input_dev
*dev
)
108 struct usb_mouse
*mouse
= input_get_drvdata(dev
);
110 usb_kill_urb(mouse
->irq
);
113 static int usb_mouse_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
115 struct usb_device
*dev
= interface_to_usbdev(intf
);
116 struct usb_host_interface
*interface
;
117 struct usb_endpoint_descriptor
*endpoint
;
118 struct usb_mouse
*mouse
;
119 struct input_dev
*input_dev
;
123 interface
= intf
->cur_altsetting
;
125 if (interface
->desc
.bNumEndpoints
!= 1)
128 endpoint
= &interface
->endpoint
[0].desc
;
129 if (!usb_endpoint_is_int_in(endpoint
))
132 pipe
= usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
);
133 maxp
= usb_maxpacket(dev
, pipe
, usb_pipeout(pipe
));
135 mouse
= kzalloc(sizeof(struct usb_mouse
), GFP_KERNEL
);
136 input_dev
= input_allocate_device();
137 if (!mouse
|| !input_dev
)
140 mouse
->data
= usb_buffer_alloc(dev
, 8, GFP_ATOMIC
, &mouse
->data_dma
);
144 mouse
->irq
= usb_alloc_urb(0, GFP_KERNEL
);
149 mouse
->dev
= input_dev
;
151 if (dev
->manufacturer
)
152 strlcpy(mouse
->name
, dev
->manufacturer
, sizeof(mouse
->name
));
155 if (dev
->manufacturer
)
156 strlcat(mouse
->name
, " ", sizeof(mouse
->name
));
157 strlcat(mouse
->name
, dev
->product
, sizeof(mouse
->name
));
160 if (!strlen(mouse
->name
))
161 snprintf(mouse
->name
, sizeof(mouse
->name
),
162 "USB HIDBP Mouse %04x:%04x",
163 le16_to_cpu(dev
->descriptor
.idVendor
),
164 le16_to_cpu(dev
->descriptor
.idProduct
));
166 usb_make_path(dev
, mouse
->phys
, sizeof(mouse
->phys
));
167 strlcat(mouse
->phys
, "/input0", sizeof(mouse
->phys
));
169 input_dev
->name
= mouse
->name
;
170 input_dev
->phys
= mouse
->phys
;
171 usb_to_input_id(dev
, &input_dev
->id
);
172 input_dev
->dev
.parent
= &intf
->dev
;
174 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REL
);
175 input_dev
->keybit
[BIT_WORD(BTN_MOUSE
)] = BIT_MASK(BTN_LEFT
) |
176 BIT_MASK(BTN_RIGHT
) | BIT_MASK(BTN_MIDDLE
);
177 input_dev
->relbit
[0] = BIT_MASK(REL_X
) | BIT_MASK(REL_Y
);
178 input_dev
->keybit
[BIT_WORD(BTN_MOUSE
)] |= BIT_MASK(BTN_SIDE
) |
180 input_dev
->relbit
[0] |= BIT_MASK(REL_WHEEL
);
182 input_set_drvdata(input_dev
, mouse
);
184 input_dev
->open
= usb_mouse_open
;
185 input_dev
->close
= usb_mouse_close
;
187 usb_fill_int_urb(mouse
->irq
, dev
, pipe
, mouse
->data
,
188 (maxp
> 8 ? 8 : maxp
),
189 usb_mouse_irq
, mouse
, endpoint
->bInterval
);
190 mouse
->irq
->transfer_dma
= mouse
->data_dma
;
191 mouse
->irq
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
193 error
= input_register_device(mouse
->dev
);
197 usb_set_intfdata(intf
, mouse
);
201 usb_free_urb(mouse
->irq
);
203 usb_buffer_free(dev
, 8, mouse
->data
, mouse
->data_dma
);
205 input_free_device(input_dev
);
210 static void usb_mouse_disconnect(struct usb_interface
*intf
)
212 struct usb_mouse
*mouse
= usb_get_intfdata (intf
);
214 usb_set_intfdata(intf
, NULL
);
216 usb_kill_urb(mouse
->irq
);
217 input_unregister_device(mouse
->dev
);
218 usb_free_urb(mouse
->irq
);
219 usb_buffer_free(interface_to_usbdev(intf
), 8, mouse
->data
, mouse
->data_dma
);
224 static struct usb_device_id usb_mouse_id_table
[] = {
225 { USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID
, USB_INTERFACE_SUBCLASS_BOOT
,
226 USB_INTERFACE_PROTOCOL_MOUSE
) },
227 { } /* Terminating entry */
230 MODULE_DEVICE_TABLE (usb
, usb_mouse_id_table
);
232 static struct usb_driver usb_mouse_driver
= {
234 .probe
= usb_mouse_probe
,
235 .disconnect
= usb_mouse_disconnect
,
236 .id_table
= usb_mouse_id_table
,
239 static int __init
usb_mouse_init(void)
241 int retval
= usb_register(&usb_mouse_driver
);
243 info(DRIVER_VERSION
":" DRIVER_DESC
);
247 static void __exit
usb_mouse_exit(void)
249 usb_deregister(&usb_mouse_driver
);
252 module_init(usb_mouse_init
);
253 module_exit(usb_mouse_exit
);