1/*
2 * $Id: usbmouse.c,v 1.1.1.1 2008/10/15 03:27:02 james26_jang Exp $
3 *
4 *  Copyright (c) 1999-2000 Vojtech Pavlik
5 *
6 *  USB HIDBP Mouse support
7 *
8 *  Sponsored by SuSE
9 */
10
11/*
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Should you need to contact me, the author, you can do so either by
27 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
28 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
29 */
30
31#include <linux/kernel.h>
32#include <linux/slab.h>
33#include <linux/input.h>
34#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/usb.h>
37
38/*
39 * Version Information
40 */
41#define DRIVER_VERSION "v1.6"
42#define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@suse.cz>"
43#define DRIVER_DESC "USB HID Boot Protocol mouse driver"
44
45MODULE_AUTHOR( DRIVER_AUTHOR );
46MODULE_DESCRIPTION( DRIVER_DESC );
47MODULE_LICENSE("GPL");
48
49struct usb_mouse {
50	signed char data[8];
51	char name[128];
52	struct usb_device *usbdev;
53	struct input_dev dev;
54	struct urb irq;
55	int open;
56};
57
58static void usb_mouse_irq(struct urb *urb)
59{
60	struct usb_mouse *mouse = urb->context;
61	signed char *data = mouse->data;
62	struct input_dev *dev = &mouse->dev;
63
64	if (urb->status) return;
65
66	input_report_key(dev, BTN_LEFT,   data[0] & 0x01);
67	input_report_key(dev, BTN_RIGHT,  data[0] & 0x02);
68	input_report_key(dev, BTN_MIDDLE, data[0] & 0x04);
69	input_report_key(dev, BTN_SIDE,   data[0] & 0x08);
70	input_report_key(dev, BTN_EXTRA,  data[0] & 0x10);
71
72	input_report_rel(dev, REL_X,     data[1]);
73	input_report_rel(dev, REL_Y,     data[2]);
74	input_report_rel(dev, REL_WHEEL, data[3]);
75}
76
77static int usb_mouse_open(struct input_dev *dev)
78{
79	struct usb_mouse *mouse = dev->private;
80
81	if (mouse->open++)
82		return 0;
83
84	mouse->irq.dev = mouse->usbdev;
85	if (usb_submit_urb(&mouse->irq))
86		return -EIO;
87
88	return 0;
89}
90
91static void usb_mouse_close(struct input_dev *dev)
92{
93	struct usb_mouse *mouse = dev->private;
94
95	if (!--mouse->open)
96		usb_unlink_urb(&mouse->irq);
97}
98
99static void *usb_mouse_probe(struct usb_device *dev, unsigned int ifnum,
100			     const struct usb_device_id *id)
101{
102	struct usb_interface *iface;
103	struct usb_interface_descriptor *interface;
104	struct usb_endpoint_descriptor *endpoint;
105	struct usb_mouse *mouse;
106	int pipe, maxp;
107	char *buf;
108
109	iface = &dev->actconfig->interface[ifnum];
110	interface = &iface->altsetting[iface->act_altsetting];
111
112	if (interface->bNumEndpoints != 1) return NULL;
113
114	endpoint = interface->endpoint + 0;
115	if (!(endpoint->bEndpointAddress & 0x80)) return NULL;
116	if ((endpoint->bmAttributes & 3) != 3) return NULL;
117	/* wacom tablets match... */
118 	if (dev->descriptor.idVendor == 0x056a) return NULL;
119
120	pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
121	maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
122
123	usb_set_idle(dev, interface->bInterfaceNumber, 0, 0);
124
125	if (!(mouse = kmalloc(sizeof(struct usb_mouse), GFP_KERNEL))) return NULL;
126	memset(mouse, 0, sizeof(struct usb_mouse));
127
128	mouse->usbdev = dev;
129
130	mouse->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
131	mouse->dev.keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
132	mouse->dev.relbit[0] = BIT(REL_X) | BIT(REL_Y);
133	mouse->dev.keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);
134	mouse->dev.relbit[0] |= BIT(REL_WHEEL);
135
136	mouse->dev.private = mouse;
137	mouse->dev.open = usb_mouse_open;
138	mouse->dev.close = usb_mouse_close;
139
140	mouse->dev.name = mouse->name;
141	mouse->dev.idbus = BUS_USB;
142	mouse->dev.idvendor = dev->descriptor.idVendor;
143	mouse->dev.idproduct = dev->descriptor.idProduct;
144	mouse->dev.idversion = dev->descriptor.bcdDevice;
145
146	if (!(buf = kmalloc(63, GFP_KERNEL))) {
147		kfree(mouse);
148		return NULL;
149	}
150
151	if (dev->descriptor.iManufacturer &&
152		usb_string(dev, dev->descriptor.iManufacturer, buf, 63) > 0)
153			strcat(mouse->name, buf);
154	if (dev->descriptor.iProduct &&
155		usb_string(dev, dev->descriptor.iProduct, buf, 63) > 0)
156			sprintf(mouse->name, "%s %s", mouse->name, buf);
157
158	if (!strlen(mouse->name))
159		sprintf(mouse->name, "USB HIDBP Mouse %04x:%04x",
160			mouse->dev.idvendor, mouse->dev.idproduct);
161
162	kfree(buf);
163
164	FILL_INT_URB(&mouse->irq, dev, pipe, mouse->data, maxp > 8 ? 8 : maxp,
165		usb_mouse_irq, mouse, endpoint->bInterval);
166
167	input_register_device(&mouse->dev);
168
169	printk(KERN_INFO "input%d: %s on usb%d:%d.%d\n",
170		 mouse->dev.number, mouse->name, dev->bus->busnum, dev->devnum, ifnum);
171
172	return mouse;
173}
174
175static void usb_mouse_disconnect(struct usb_device *dev, void *ptr)
176{
177	struct usb_mouse *mouse = ptr;
178	usb_unlink_urb(&mouse->irq);
179	input_unregister_device(&mouse->dev);
180	kfree(mouse);
181}
182
183static struct usb_device_id usb_mouse_id_table [] = {
184	{ USB_INTERFACE_INFO(3, 1, 2) },
185    { }						/* Terminating entry */
186};
187
188MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
189
190static struct usb_driver usb_mouse_driver = {
191	name:		"usbmouse",
192	probe:		usb_mouse_probe,
193	disconnect:	usb_mouse_disconnect,
194	id_table:	usb_mouse_id_table,
195};
196
197static int __init usb_mouse_init(void)
198{
199	usb_register(&usb_mouse_driver);
200	info(DRIVER_VERSION ":" DRIVER_DESC);
201	return 0;
202}
203
204static void __exit usb_mouse_exit(void)
205{
206	usb_deregister(&usb_mouse_driver);
207}
208
209module_init(usb_mouse_init);
210module_exit(usb_mouse_exit);
211