1 /*
2  * $Id: usbkbd.c,v 1.20 2001/04/26 08:34:49 vojtech Exp $
3  *
4  *  Copyright (c) 1999-2001 Vojtech Pavlik
5  *
6  *  USB HIDBP Keyboard 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/module.h>
34 #include <linux/input.h>
35 #include <linux/init.h>
36 #include <linux/usb.h>
37 
38 /*
39  * Version Information
40  */
41 #define DRIVER_VERSION ""
42 #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@suse.cz>"
43 #define DRIVER_DESC "USB HID Boot Protocol keyboard driver"
44 
45 MODULE_AUTHOR( DRIVER_AUTHOR );
46 MODULE_DESCRIPTION( DRIVER_DESC );
47 MODULE_LICENSE("GPL");
48 
49 static unsigned char usb_kbd_keycode[256] = {
50 	  0,  0,  0,  0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
51 	 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44,  2,  3,
52 	  4,  5,  6,  7,  8,  9, 10, 11, 28,  1, 14, 15, 57, 12, 13, 26,
53 	 27, 43, 84, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
54 	 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
55 	105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
56 	 72, 73, 82, 83, 86,127,116,117, 85, 89, 90, 91, 92, 93, 94, 95,
57 	120,121,122,123,134,138,130,132,128,129,131,137,133,135,136,113,
58 	115,114,  0,  0,  0,124,  0,181,182,183,184,185,186,187,188,189,
59 	190,191,192,193,194,195,196,197,198,  0,  0,  0,  0,  0,  0,  0,
60 	  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
61 	  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
62 	  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
63 	  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
64 	 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
65 	150,158,159,128,136,177,178,176,142,152,173,140
66 };
67 
68 struct usb_kbd {
69 	struct input_dev dev;
70 	struct usb_device *usbdev;
71 	unsigned char new[8];
72 	unsigned char old[8];
73 	struct urb irq, led;
74 	struct usb_ctrlrequest dr;
75 	unsigned char leds, newleds;
76 	char name[128];
77 	int open;
78 };
79 
usb_kbd_irq(struct urb * urb)80 static void usb_kbd_irq(struct urb *urb)
81 {
82 	struct usb_kbd *kbd = urb->context;
83 	int i;
84 
85 	if (urb->status) return;
86 
87 	for (i = 0; i < 8; i++)
88 		input_report_key(&kbd->dev, usb_kbd_keycode[i + 224], (kbd->new[0] >> i) & 1);
89 
90 	for (i = 2; i < 8; i++) {
91 
92 		if (kbd->old[i] > 3 && memscan(kbd->new + 2, kbd->old[i], 6) == kbd->new + 8) {
93 			if (usb_kbd_keycode[kbd->old[i]])
94 				input_report_key(&kbd->dev, usb_kbd_keycode[kbd->old[i]], 0);
95 			else
96 				info("Unknown key (scancode %#x) released.", kbd->old[i]);
97 		}
98 
99 		if (kbd->new[i] > 3 && memscan(kbd->old + 2, kbd->new[i], 6) == kbd->old + 8) {
100 			if (usb_kbd_keycode[kbd->new[i]])
101 				input_report_key(&kbd->dev, usb_kbd_keycode[kbd->new[i]], 1);
102 			else
103 				info("Unknown key (scancode %#x) pressed.", kbd->new[i]);
104 		}
105 	}
106 
107 	memcpy(kbd->old, kbd->new, 8);
108 }
109 
usb_kbd_event(struct input_dev * dev,unsigned int type,unsigned int code,int value)110 int usb_kbd_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
111 {
112 	struct usb_kbd *kbd = dev->private;
113 
114 	if (type != EV_LED) return -1;
115 
116 
117 	kbd->newleds = (!!test_bit(LED_KANA,    dev->led) << 3) | (!!test_bit(LED_COMPOSE, dev->led) << 3) |
118 		       (!!test_bit(LED_SCROLLL, dev->led) << 2) | (!!test_bit(LED_CAPSL,   dev->led) << 1) |
119 		       (!!test_bit(LED_NUML,    dev->led));
120 
121 	if (kbd->led.status == -EINPROGRESS)
122 		return 0;
123 
124 	if (kbd->leds == kbd->newleds)
125 		return 0;
126 
127 	kbd->leds = kbd->newleds;
128 	kbd->led.dev = kbd->usbdev;
129 	if (usb_submit_urb(&kbd->led))
130 		err("usb_submit_urb(leds) failed");
131 
132 	return 0;
133 }
134 
usb_kbd_led(struct urb * urb)135 static void usb_kbd_led(struct urb *urb)
136 {
137 	struct usb_kbd *kbd = urb->context;
138 
139 	if (urb->status)
140 		warn("led urb status %d received", urb->status);
141 
142 	if (kbd->leds == kbd->newleds)
143 		return;
144 
145 	kbd->leds = kbd->newleds;
146 	kbd->led.dev = kbd->usbdev;
147 	if (usb_submit_urb(&kbd->led))
148 		err("usb_submit_urb(leds) failed");
149 }
150 
usb_kbd_open(struct input_dev * dev)151 static int usb_kbd_open(struct input_dev *dev)
152 {
153 	struct usb_kbd *kbd = dev->private;
154 
155 	if (kbd->open++)
156 		return 0;
157 
158 	kbd->irq.dev = kbd->usbdev;
159 	if (usb_submit_urb(&kbd->irq))
160 		return -EIO;
161 
162 	return 0;
163 }
164 
usb_kbd_close(struct input_dev * dev)165 static void usb_kbd_close(struct input_dev *dev)
166 {
167 	struct usb_kbd *kbd = dev->private;
168 
169 	if (!--kbd->open)
170 		usb_unlink_urb(&kbd->irq);
171 }
172 
usb_kbd_probe(struct usb_device * dev,unsigned int ifnum,const struct usb_device_id * id)173 static void *usb_kbd_probe(struct usb_device *dev, unsigned int ifnum,
174 			   const struct usb_device_id *id)
175 {
176 	struct usb_interface *iface;
177 	struct usb_interface_descriptor *interface;
178 	struct usb_endpoint_descriptor *endpoint;
179 	struct usb_kbd *kbd;
180 	int i, pipe, maxp;
181 	char *buf;
182 
183 	iface = &dev->actconfig->interface[ifnum];
184 	interface = &iface->altsetting[iface->act_altsetting];
185 
186 	if (interface->bNumEndpoints != 1) return NULL;
187 
188 	endpoint = interface->endpoint + 0;
189 	if (!(endpoint->bEndpointAddress & 0x80)) return NULL;
190 	if ((endpoint->bmAttributes & 3) != 3) return NULL;
191 
192 	pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
193 	maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
194 
195 	usb_set_protocol(dev, interface->bInterfaceNumber, 0);
196 	usb_set_idle(dev, interface->bInterfaceNumber, 0, 0);
197 
198 	if (!(kbd = kmalloc(sizeof(struct usb_kbd), GFP_KERNEL))) return NULL;
199 	memset(kbd, 0, sizeof(struct usb_kbd));
200 
201 	kbd->usbdev = dev;
202 
203 	kbd->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_REP);
204 	kbd->dev.ledbit[0] = BIT(LED_NUML) | BIT(LED_CAPSL) | BIT(LED_SCROLLL) | BIT(LED_COMPOSE) | BIT(LED_KANA);
205 
206 	for (i = 0; i < 255; i++)
207 		set_bit(usb_kbd_keycode[i], kbd->dev.keybit);
208 	clear_bit(0, kbd->dev.keybit);
209 
210 	kbd->dev.private = kbd;
211 	kbd->dev.event = usb_kbd_event;
212 	kbd->dev.open = usb_kbd_open;
213 	kbd->dev.close = usb_kbd_close;
214 
215 	FILL_INT_URB(&kbd->irq, dev, pipe, kbd->new, maxp > 8 ? 8 : maxp,
216 		usb_kbd_irq, kbd, endpoint->bInterval);
217 
218 	kbd->dr.bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
219 	kbd->dr.bRequest = USB_REQ_SET_REPORT;
220 	kbd->dr.wValue = 0x200;
221 	kbd->dr.wIndex = interface->bInterfaceNumber;
222 	kbd->dr.wLength = 1;
223 
224 	kbd->dev.name = kbd->name;
225 	kbd->dev.idbus = BUS_USB;
226 	kbd->dev.idvendor = dev->descriptor.idVendor;
227 	kbd->dev.idproduct = dev->descriptor.idProduct;
228 	kbd->dev.idversion = dev->descriptor.bcdDevice;
229 
230 	if (!(buf = kmalloc(63, GFP_KERNEL))) {
231 		kfree(kbd);
232 		return NULL;
233 	}
234 
235 	if (dev->descriptor.iManufacturer &&
236 		usb_string(dev, dev->descriptor.iManufacturer, buf, 63) > 0)
237 			strcat(kbd->name, buf);
238 	if (dev->descriptor.iProduct &&
239 		usb_string(dev, dev->descriptor.iProduct, buf, 63) > 0)
240 			sprintf(kbd->name, "%s %s", kbd->name, buf);
241 
242 	if (!strlen(kbd->name))
243 		sprintf(kbd->name, "USB HIDBP Keyboard %04x:%04x",
244 			kbd->dev.idvendor, kbd->dev.idproduct);
245 
246 	kfree(buf);
247 
248 	FILL_CONTROL_URB(&kbd->led, dev, usb_sndctrlpipe(dev, 0),
249 		(void*) &kbd->dr, &kbd->leds, 1, usb_kbd_led, kbd);
250 
251 	input_register_device(&kbd->dev);
252 
253 	printk(KERN_INFO "input%d: %s on usb%d:%d.%d\n",
254 		 kbd->dev.number, kbd->name, dev->bus->busnum, dev->devnum, ifnum);
255 
256 	return kbd;
257 }
258 
usb_kbd_disconnect(struct usb_device * dev,void * ptr)259 static void usb_kbd_disconnect(struct usb_device *dev, void *ptr)
260 {
261 	struct usb_kbd *kbd = ptr;
262 	usb_unlink_urb(&kbd->irq);
263 	input_unregister_device(&kbd->dev);
264 	kfree(kbd);
265 }
266 
267 static struct usb_device_id usb_kbd_id_table [] = {
268 	{ USB_INTERFACE_INFO(3, 1, 1) },
269 	{ }						/* Terminating entry */
270 };
271 
272 MODULE_DEVICE_TABLE (usb, usb_kbd_id_table);
273 
274 static struct usb_driver usb_kbd_driver = {
275 	name:		"usbkbd",
276 	probe:		usb_kbd_probe,
277 	disconnect:	usb_kbd_disconnect,
278 	id_table:	usb_kbd_id_table,
279 };
280 
usb_kbd_init(void)281 static int __init usb_kbd_init(void)
282 {
283 	usb_register(&usb_kbd_driver);
284 	info(DRIVER_VERSION ":" DRIVER_DESC);
285 	return 0;
286 }
287 
usb_kbd_exit(void)288 static void __exit usb_kbd_exit(void)
289 {
290 	usb_deregister(&usb_kbd_driver);
291 }
292 
293 module_init(usb_kbd_init);
294 module_exit(usb_kbd_exit);
295