xref: /openbmc/linux/drivers/hid/usbhid/usbmouse.c (revision 643d1f7f)
1 /*
2  * $Id: usbmouse.c,v 1.15 2001/12/27 10:37:41 vojtech Exp $
3  *
4  *  Copyright (c) 1999-2001 Vojtech Pavlik
5  *
6  *  USB HIDBP Mouse support
7  */
8 
9 /*
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  * Should you need to contact me, the author, you can do so either by
25  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
27  */
28 
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/usb/input.h>
34 #include <linux/hid.h>
35 
36 /*
37  * Version Information
38  */
39 #define DRIVER_VERSION "v1.6"
40 #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@ucw.cz>"
41 #define DRIVER_DESC "USB HID Boot Protocol mouse driver"
42 #define DRIVER_LICENSE "GPL"
43 
44 MODULE_AUTHOR(DRIVER_AUTHOR);
45 MODULE_DESCRIPTION(DRIVER_DESC);
46 MODULE_LICENSE(DRIVER_LICENSE);
47 
48 struct usb_mouse {
49 	char name[128];
50 	char phys[64];
51 	struct usb_device *usbdev;
52 	struct input_dev *dev;
53 	struct urb *irq;
54 
55 	signed char *data;
56 	dma_addr_t data_dma;
57 };
58 
59 static void usb_mouse_irq(struct urb *urb)
60 {
61 	struct usb_mouse *mouse = urb->context;
62 	signed char *data = mouse->data;
63 	struct input_dev *dev = mouse->dev;
64 	int status;
65 
66 	switch (urb->status) {
67 	case 0:			/* success */
68 		break;
69 	case -ECONNRESET:	/* unlink */
70 	case -ENOENT:
71 	case -ESHUTDOWN:
72 		return;
73 	/* -EPIPE:  should clear the halt */
74 	default:		/* error */
75 		goto resubmit;
76 	}
77 
78 	input_report_key(dev, BTN_LEFT,   data[0] & 0x01);
79 	input_report_key(dev, BTN_RIGHT,  data[0] & 0x02);
80 	input_report_key(dev, BTN_MIDDLE, data[0] & 0x04);
81 	input_report_key(dev, BTN_SIDE,   data[0] & 0x08);
82 	input_report_key(dev, BTN_EXTRA,  data[0] & 0x10);
83 
84 	input_report_rel(dev, REL_X,     data[1]);
85 	input_report_rel(dev, REL_Y,     data[2]);
86 	input_report_rel(dev, REL_WHEEL, data[3]);
87 
88 	input_sync(dev);
89 resubmit:
90 	status = usb_submit_urb (urb, GFP_ATOMIC);
91 	if (status)
92 		err ("can't resubmit intr, %s-%s/input0, status %d",
93 				mouse->usbdev->bus->bus_name,
94 				mouse->usbdev->devpath, status);
95 }
96 
97 static int usb_mouse_open(struct input_dev *dev)
98 {
99 	struct usb_mouse *mouse = input_get_drvdata(dev);
100 
101 	mouse->irq->dev = mouse->usbdev;
102 	if (usb_submit_urb(mouse->irq, GFP_KERNEL))
103 		return -EIO;
104 
105 	return 0;
106 }
107 
108 static void usb_mouse_close(struct input_dev *dev)
109 {
110 	struct usb_mouse *mouse = input_get_drvdata(dev);
111 
112 	usb_kill_urb(mouse->irq);
113 }
114 
115 static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id)
116 {
117 	struct usb_device *dev = interface_to_usbdev(intf);
118 	struct usb_host_interface *interface;
119 	struct usb_endpoint_descriptor *endpoint;
120 	struct usb_mouse *mouse;
121 	struct input_dev *input_dev;
122 	int pipe, maxp;
123 	int error = -ENOMEM;
124 
125 	interface = intf->cur_altsetting;
126 
127 	if (interface->desc.bNumEndpoints != 1)
128 		return -ENODEV;
129 
130 	endpoint = &interface->endpoint[0].desc;
131 	if (!usb_endpoint_is_int_in(endpoint))
132 		return -ENODEV;
133 
134 #ifdef CONFIG_USB_HID
135 	if (usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
136 				le16_to_cpu(dev->descriptor.idProduct))
137 			& (HID_QUIRK_IGNORE|HID_QUIRK_IGNORE_MOUSE)) {
138 		return -ENODEV;
139 	}
140 #endif
141 
142 	pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
143 	maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
144 
145 	mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL);
146 	input_dev = input_allocate_device();
147 	if (!mouse || !input_dev)
148 		goto fail1;
149 
150 	mouse->data = usb_buffer_alloc(dev, 8, GFP_ATOMIC, &mouse->data_dma);
151 	if (!mouse->data)
152 		goto fail1;
153 
154 	mouse->irq = usb_alloc_urb(0, GFP_KERNEL);
155 	if (!mouse->irq)
156 		goto fail2;
157 
158 	mouse->usbdev = dev;
159 	mouse->dev = input_dev;
160 
161 	if (dev->manufacturer)
162 		strlcpy(mouse->name, dev->manufacturer, sizeof(mouse->name));
163 
164 	if (dev->product) {
165 		if (dev->manufacturer)
166 			strlcat(mouse->name, " ", sizeof(mouse->name));
167 		strlcat(mouse->name, dev->product, sizeof(mouse->name));
168 	}
169 
170 	if (!strlen(mouse->name))
171 		snprintf(mouse->name, sizeof(mouse->name),
172 			 "USB HIDBP Mouse %04x:%04x",
173 			 le16_to_cpu(dev->descriptor.idVendor),
174 			 le16_to_cpu(dev->descriptor.idProduct));
175 
176 	usb_make_path(dev, mouse->phys, sizeof(mouse->phys));
177 	strlcat(mouse->phys, "/input0", sizeof(mouse->phys));
178 
179 	input_dev->name = mouse->name;
180 	input_dev->phys = mouse->phys;
181 	usb_to_input_id(dev, &input_dev->id);
182 	input_dev->dev.parent = &intf->dev;
183 
184 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
185 	input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
186 		BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
187 	input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
188 	input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) |
189 		BIT_MASK(BTN_EXTRA);
190 	input_dev->relbit[0] |= BIT_MASK(REL_WHEEL);
191 
192 	input_set_drvdata(input_dev, mouse);
193 
194 	input_dev->open = usb_mouse_open;
195 	input_dev->close = usb_mouse_close;
196 
197 	usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,
198 			 (maxp > 8 ? 8 : maxp),
199 			 usb_mouse_irq, mouse, endpoint->bInterval);
200 	mouse->irq->transfer_dma = mouse->data_dma;
201 	mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
202 
203 	error = input_register_device(mouse->dev);
204 	if (error)
205 		goto fail3;
206 
207 	usb_set_intfdata(intf, mouse);
208 	return 0;
209 
210 fail3:
211 	usb_free_urb(mouse->irq);
212 fail2:
213 	usb_buffer_free(dev, 8, mouse->data, mouse->data_dma);
214 fail1:
215 	input_free_device(input_dev);
216 	kfree(mouse);
217 	return error;
218 }
219 
220 static void usb_mouse_disconnect(struct usb_interface *intf)
221 {
222 	struct usb_mouse *mouse = usb_get_intfdata (intf);
223 
224 	usb_set_intfdata(intf, NULL);
225 	if (mouse) {
226 		usb_kill_urb(mouse->irq);
227 		input_unregister_device(mouse->dev);
228 		usb_free_urb(mouse->irq);
229 		usb_buffer_free(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma);
230 		kfree(mouse);
231 	}
232 }
233 
234 static struct usb_device_id usb_mouse_id_table [] = {
235 	{ USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,
236 		USB_INTERFACE_PROTOCOL_MOUSE) },
237 	{ }	/* Terminating entry */
238 };
239 
240 MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
241 
242 static struct usb_driver usb_mouse_driver = {
243 	.name		= "usbmouse",
244 	.probe		= usb_mouse_probe,
245 	.disconnect	= usb_mouse_disconnect,
246 	.id_table	= usb_mouse_id_table,
247 };
248 
249 static int __init usb_mouse_init(void)
250 {
251 	int retval = usb_register(&usb_mouse_driver);
252 	if (retval == 0)
253 		info(DRIVER_VERSION ":" DRIVER_DESC);
254 	return retval;
255 }
256 
257 static void __exit usb_mouse_exit(void)
258 {
259 	usb_deregister(&usb_mouse_driver);
260 }
261 
262 module_init(usb_mouse_init);
263 module_exit(usb_mouse_exit);
264