2 * $Id: usbkbd.c,v 1.11 2000/05/29 09:01:52 vojtech Exp $
4 * Copyright (c) 1999-2000 Vojtech Pavlik
6 * USB HIDBP Keyboard 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/module.h>
34 #include <linux/input.h>
35 #include <linux/init.h>
36 #include <linux/usb.h>
38 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
39 MODULE_DESCRIPTION("USB HID Boot Protocol keyboard driver");
41 static unsigned char usb_kbd_keycode
[256] = {
42 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
43 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
44 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
45 27, 43, 84, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
46 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
47 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
48 72, 73, 82, 83, 86,127,116,117, 85, 89, 90, 91, 92, 93, 94, 95,
49 120,121,122,123,134,138,130,132,128,129,131,137,133,135,136,113,
50 115,114, 0, 0, 0, 0, 0,124, 0, 0, 0, 0, 0, 0, 0, 0,
51 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
57 150,158,159,128,136,177,178,176,142,152,173,140
71 static void usb_kbd_irq(struct urb
*urb
)
73 struct usb_kbd
*kbd
= urb
->context
;
76 if (urb
->status
) return;
78 for (i
= 0; i
< 8; i
++)
79 input_report_key(&kbd
->dev
, usb_kbd_keycode
[i
+ 224], (kbd
->new[0] >> i
) & 1);
81 for (i
= 2; i
< 8; i
++) {
83 if (kbd
->old
[i
] > 3 && memscan(kbd
->new + 2, kbd
->old
[i
], 6) == kbd
->new + 8) {
84 if (usb_kbd_keycode
[kbd
->old
[i
]])
85 input_report_key(&kbd
->dev
, usb_kbd_keycode
[kbd
->old
[i
]], 0);
87 info("Unknown key (scancode %#x) released.", kbd
->old
[i
]);
90 if (kbd
->new[i
] > 3 && memscan(kbd
->old
+ 2, kbd
->new[i
], 6) == kbd
->old
+ 8) {
91 if (usb_kbd_keycode
[kbd
->new[i
]])
92 input_report_key(&kbd
->dev
, usb_kbd_keycode
[kbd
->new[i
]], 1);
94 info("Unknown key (scancode %#x) pressed.", kbd
->new[i
]);
98 memcpy(kbd
->old
, kbd
->new, 8);
101 int usb_kbd_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
103 struct usb_kbd
*kbd
= dev
->private;
105 if (type
!= EV_LED
) return -1;
107 if (kbd
->led
.status
== -EINPROGRESS
) {
108 warn("had to kill led urb");
109 usb_unlink_urb(&kbd
->led
);
112 kbd
->leds
= (!!test_bit(LED_KANA
, dev
->led
) << 3) | (!!test_bit(LED_COMPOSE
, dev
->led
) << 3) |
113 (!!test_bit(LED_SCROLLL
, dev
->led
) << 2) | (!!test_bit(LED_CAPSL
, dev
->led
) << 1) |
114 (!!test_bit(LED_NUML
, dev
->led
));
117 if (usb_submit_urb(&kbd
->led
)) {
118 err("usb_submit_urb(leds) failed");
125 static void usb_kbd_led(struct urb
*urb
)
128 warn("led urb status %d received", urb
->status
);
131 static int usb_kbd_open(struct input_dev
*dev
)
133 struct usb_kbd
*kbd
= dev
->private;
138 if (usb_submit_urb(&kbd
->irq
))
144 static void usb_kbd_close(struct input_dev
*dev
)
146 struct usb_kbd
*kbd
= dev
->private;
149 usb_unlink_urb(&kbd
->irq
);
152 static void *usb_kbd_probe(struct usb_device
*dev
, unsigned int ifnum
)
154 struct usb_interface_descriptor
*interface
;
155 struct usb_endpoint_descriptor
*endpoint
;
160 if (dev
->descriptor
.bNumConfigurations
!= 1) return NULL
;
161 interface
= dev
->config
[0].interface
[ifnum
].altsetting
+ 0;
163 if (interface
->bInterfaceClass
!= 3) return NULL
;
164 if (interface
->bInterfaceSubClass
!= 1) return NULL
;
165 if (interface
->bInterfaceProtocol
!= 1) return NULL
;
166 if (interface
->bNumEndpoints
!= 1) return NULL
;
168 endpoint
= interface
->endpoint
+ 0;
169 if (!(endpoint
->bEndpointAddress
& 0x80)) return NULL
;
170 if ((endpoint
->bmAttributes
& 3) != 3) return NULL
;
172 pipe
= usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
);
173 maxp
= usb_maxpacket(dev
, pipe
, usb_pipeout(pipe
));
175 usb_set_protocol(dev
, interface
->bInterfaceNumber
, 0);
176 usb_set_idle(dev
, interface
->bInterfaceNumber
, 0, 0);
178 if (!(kbd
= kmalloc(sizeof(struct usb_kbd
), GFP_KERNEL
))) return NULL
;
179 memset(kbd
, 0, sizeof(struct usb_kbd
));
181 kbd
->dev
.evbit
[0] = BIT(EV_KEY
) | BIT(EV_LED
) | BIT(EV_REP
);
182 kbd
->dev
.ledbit
[0] = BIT(LED_NUML
) | BIT(LED_CAPSL
) | BIT(LED_SCROLLL
) | BIT(LED_COMPOSE
) | BIT(LED_KANA
);
184 for (i
= 0; i
< 255; i
++)
185 set_bit(usb_kbd_keycode
[i
], kbd
->dev
.keybit
);
186 clear_bit(0, kbd
->dev
.keybit
);
188 kbd
->dev
.private = kbd
;
189 kbd
->dev
.event
= usb_kbd_event
;
190 kbd
->dev
.open
= usb_kbd_open
;
191 kbd
->dev
.close
= usb_kbd_close
;
193 FILL_INT_URB(&kbd
->irq
, dev
, pipe
, kbd
->new, maxp
> 8 ? 8 : maxp
,
194 usb_kbd_irq
, kbd
, endpoint
->bInterval
);
196 kbd
->dr
.requesttype
= USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
197 kbd
->dr
.request
= USB_REQ_SET_REPORT
;
198 kbd
->dr
.value
= 0x200;
199 kbd
->dr
.index
= interface
->bInterfaceNumber
;
202 kbd
->dev
.name
= kbd
->name
;
203 kbd
->dev
.idbus
= BUS_USB
;
204 kbd
->dev
.idvendor
= dev
->descriptor
.idVendor
;
205 kbd
->dev
.idproduct
= dev
->descriptor
.idProduct
;
206 kbd
->dev
.idversion
= dev
->descriptor
.bcdDevice
;
208 if (!(buf
= kmalloc(63, GFP_KERNEL
))) {
213 if (dev
->descriptor
.iManufacturer
&&
214 usb_string(dev
, dev
->descriptor
.iManufacturer
, buf
, 63) > 0)
215 strcat(kbd
->name
, buf
);
216 if (dev
->descriptor
.iProduct
&&
217 usb_string(dev
, dev
->descriptor
.iProduct
, buf
, 63) > 0)
218 sprintf(kbd
->name
, "%s %s", kbd
->name
, buf
);
220 if (!strlen(kbd
->name
))
221 sprintf(kbd
->name
, "USB HIDBP Keyboard %04x:%04x",
222 kbd
->dev
.idvendor
, kbd
->dev
.idproduct
);
226 FILL_CONTROL_URB(&kbd
->led
, dev
, usb_sndctrlpipe(dev
, 0),
227 (void*) &kbd
->dr
, &kbd
->leds
, 1, usb_kbd_led
, kbd
);
229 input_register_device(&kbd
->dev
);
231 printk(KERN_INFO
"input%d: %s on on usb%d:%d.%d\n",
232 kbd
->dev
.number
, kbd
->name
, dev
->bus
->busnum
, dev
->devnum
, ifnum
);
237 static void usb_kbd_disconnect(struct usb_device
*dev
, void *ptr
)
239 struct usb_kbd
*kbd
= ptr
;
240 usb_unlink_urb(&kbd
->irq
);
241 input_unregister_device(&kbd
->dev
);
245 static struct usb_driver usb_kbd_driver
= {
247 probe
: usb_kbd_probe
,
248 disconnect
: usb_kbd_disconnect
251 static int __init
usb_kbd_init(void)
253 usb_register(&usb_kbd_driver
);
257 static void __exit
usb_kbd_exit(void)
259 usb_deregister(&usb_kbd_driver
);
262 module_init(usb_kbd_init
);
263 module_exit(usb_kbd_exit
);