2 * $Id: usbmouse.c,v 1.5 2000/05/29 09:01:52 vojtech Exp $
4 * Copyright (c) 1999-2000 Vojtech Pavlik
6 * USB HIDBP Mouse support
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.
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.
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
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
31 #include <linux/kernel.h>
32 #include <linux/malloc.h>
33 #include <linux/input.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/usb.h>
38 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
48 static void usb_mouse_irq(struct urb
*urb
)
50 struct usb_mouse
*mouse
= urb
->context
;
51 signed char *data
= mouse
->data
;
52 struct input_dev
*dev
= &mouse
->dev
;
54 if (urb
->status
) return;
56 input_report_key(dev
, BTN_LEFT
, data
[0] & 0x01);
57 input_report_key(dev
, BTN_RIGHT
, data
[0] & 0x02);
58 input_report_key(dev
, BTN_MIDDLE
, data
[0] & 0x04);
59 input_report_key(dev
, BTN_SIDE
, data
[0] & 0x08);
60 input_report_key(dev
, BTN_EXTRA
, data
[0] & 0x10);
62 input_report_rel(dev
, REL_X
, data
[1]);
63 input_report_rel(dev
, REL_Y
, data
[2]);
64 input_report_rel(dev
, REL_WHEEL
, data
[3]);
67 static int usb_mouse_open(struct input_dev
*dev
)
69 struct usb_mouse
*mouse
= dev
->private;
74 if (usb_submit_urb(&mouse
->irq
))
80 static void usb_mouse_close(struct input_dev
*dev
)
82 struct usb_mouse
*mouse
= dev
->private;
85 usb_unlink_urb(&mouse
->irq
);
88 static void *usb_mouse_probe(struct usb_device
*dev
, unsigned int ifnum
)
90 struct usb_interface_descriptor
*interface
;
91 struct usb_endpoint_descriptor
*endpoint
;
92 struct usb_mouse
*mouse
;
96 if (dev
->descriptor
.bNumConfigurations
!= 1) return NULL
;
97 interface
= dev
->config
[0].interface
[ifnum
].altsetting
+ 0;
99 if (interface
->bInterfaceClass
!= 3) return NULL
;
100 if (interface
->bInterfaceSubClass
!= 1) return NULL
;
101 if (interface
->bInterfaceProtocol
!= 2) return NULL
;
102 if (interface
->bNumEndpoints
!= 1) return NULL
;
104 endpoint
= interface
->endpoint
+ 0;
105 if (!(endpoint
->bEndpointAddress
& 0x80)) return NULL
;
106 if ((endpoint
->bmAttributes
& 3) != 3) return NULL
;
108 pipe
= usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
);
109 maxp
= usb_maxpacket(dev
, pipe
, usb_pipeout(pipe
));
111 usb_set_idle(dev
, interface
->bInterfaceNumber
, 0, 0);
113 if (!(mouse
= kmalloc(sizeof(struct usb_mouse
), GFP_KERNEL
))) return NULL
;
114 memset(mouse
, 0, sizeof(struct usb_mouse
));
116 mouse
->dev
.evbit
[0] = BIT(EV_KEY
) | BIT(EV_REL
);
117 mouse
->dev
.keybit
[LONG(BTN_MOUSE
)] = BIT(BTN_LEFT
) | BIT(BTN_RIGHT
) | BIT(BTN_MIDDLE
);
118 mouse
->dev
.relbit
[0] = BIT(REL_X
) | BIT(REL_Y
);
119 mouse
->dev
.keybit
[LONG(BTN_MOUSE
)] |= BIT(BTN_SIDE
) | BIT(BTN_EXTRA
);
120 mouse
->dev
.relbit
[0] |= BIT(REL_WHEEL
);
122 mouse
->dev
.private = mouse
;
123 mouse
->dev
.open
= usb_mouse_open
;
124 mouse
->dev
.close
= usb_mouse_close
;
126 mouse
->dev
.name
= mouse
->name
;
127 mouse
->dev
.idbus
= BUS_USB
;
128 mouse
->dev
.idvendor
= dev
->descriptor
.idVendor
;
129 mouse
->dev
.idproduct
= dev
->descriptor
.idProduct
;
130 mouse
->dev
.idversion
= dev
->descriptor
.bcdDevice
;
132 if (!(buf
= kmalloc(63, GFP_KERNEL
))) {
137 if (dev
->descriptor
.iManufacturer
&&
138 usb_string(dev
, dev
->descriptor
.iManufacturer
, buf
, 63) > 0)
139 strcat(mouse
->name
, buf
);
140 if (dev
->descriptor
.iProduct
&&
141 usb_string(dev
, dev
->descriptor
.iProduct
, buf
, 63) > 0)
142 sprintf(mouse
->name
, "%s %s", mouse
->name
, buf
);
144 if (!strlen(mouse
->name
))
145 sprintf(mouse
->name
, "USB HIDBP Mouse %04x:%04x",
146 mouse
->dev
.idvendor
, mouse
->dev
.idproduct
);
150 FILL_INT_URB(&mouse
->irq
, dev
, pipe
, mouse
->data
, maxp
> 8 ? 8 : maxp
,
151 usb_mouse_irq
, mouse
, endpoint
->bInterval
);
153 input_register_device(&mouse
->dev
);
155 printk(KERN_INFO
"input%d: %s on usb%d:%d.%d\n",
156 mouse
->dev
.number
, mouse
->name
, dev
->bus
->busnum
, dev
->devnum
, ifnum
);
161 static void usb_mouse_disconnect(struct usb_device
*dev
, void *ptr
)
163 struct usb_mouse
*mouse
= ptr
;
164 usb_unlink_urb(&mouse
->irq
);
165 input_unregister_device(&mouse
->dev
);
169 static struct usb_driver usb_mouse_driver
= {
171 probe
: usb_mouse_probe
,
172 disconnect
: usb_mouse_disconnect
,
175 static int __init
usb_mouse_init(void)
177 usb_register(&usb_mouse_driver
);
181 static void __exit
usb_mouse_exit(void)
183 usb_deregister(&usb_mouse_driver
);
186 module_init(usb_mouse_init
);
187 module_exit(usb_mouse_exit
);