4 * Copyright (c) 1999 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>
38 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
40 static unsigned char usb_kbd_keycode
[256] = {
41 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
42 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
43 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
44 27, 43, 84, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
45 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
46 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
47 72, 73, 82, 83, 86,127,116,117, 85, 89, 90, 91, 92, 93, 94, 95,
48 120,121,122,123,134,138,130,132,128,129,131,137,133,135,136,113,
49 115,114, 0, 0, 0, 0, 0,124, 0, 0, 0, 0, 0, 0, 0, 0,
50 0, 0, 0, 0, 0, 0, 0, 0, 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 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
56 150,158,159,128,136,177,178,176,142,152,173,140
68 static void usb_kbd_irq(struct urb
*urb
)
70 struct usb_kbd
*kbd
= urb
->context
;
73 if (urb
->status
) return;
75 for (i
= 0; i
< 8; i
++)
76 input_report_key(&kbd
->dev
, usb_kbd_keycode
[i
+ 224], (kbd
->new[0] >> i
) & 1);
78 for (i
= 2; i
< 8; i
++) {
80 if (kbd
->old
[i
] > 3 && memscan(kbd
->new + 2, kbd
->old
[i
], 6) == kbd
->new + 8) {
81 if (usb_kbd_keycode
[kbd
->old
[i
]])
82 input_report_key(&kbd
->dev
, usb_kbd_keycode
[kbd
->old
[i
]], 0);
84 info("Unknown key (scancode %#x) released.", kbd
->old
[i
]);
87 if (kbd
->new[i
] > 3 && memscan(kbd
->old
+ 2, kbd
->new[i
], 6) == kbd
->old
+ 8) {
88 if (usb_kbd_keycode
[kbd
->new[i
]])
89 input_report_key(&kbd
->dev
, usb_kbd_keycode
[kbd
->new[i
]], 1);
91 info("Unknown key (scancode %#x) pressed.", kbd
->new[i
]);
95 memcpy(kbd
->old
, kbd
->new, 8);
98 int usb_kbd_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
100 struct usb_kbd
*kbd
= dev
->private;
102 if (type
!= EV_LED
) return -1;
104 if (kbd
->led
.status
== -EINPROGRESS
) {
105 warn("had to kill led urb");
106 usb_unlink_urb(&kbd
->led
);
109 kbd
->leds
= (!!test_bit(LED_KANA
, dev
->led
) << 3) | (!!test_bit(LED_COMPOSE
, dev
->led
) << 3) |
110 (!!test_bit(LED_SCROLLL
, dev
->led
) << 2) | (!!test_bit(LED_CAPSL
, dev
->led
) << 1) |
111 (!!test_bit(LED_NUML
, dev
->led
));
114 if (usb_submit_urb(&kbd
->led
)) {
115 err("usb_submit_urb(leds) failed");
122 static void usb_kbd_led(struct urb
*urb
)
125 warn("led urb status %d received", urb
->status
);
128 static void *usb_kbd_probe(struct usb_device
*dev
, unsigned int ifnum
)
130 struct usb_interface_descriptor
*interface
;
131 struct usb_endpoint_descriptor
*endpoint
;
135 if (dev
->descriptor
.bNumConfigurations
!= 1) return NULL
;
136 interface
= dev
->config
[0].interface
[ifnum
].altsetting
+ 0;
138 if (interface
->bInterfaceClass
!= 3) return NULL
;
139 if (interface
->bInterfaceSubClass
!= 1) return NULL
;
140 if (interface
->bInterfaceProtocol
!= 1) return NULL
;
141 if (interface
->bNumEndpoints
!= 1) return NULL
;
143 endpoint
= interface
->endpoint
+ 0;
144 if (!(endpoint
->bEndpointAddress
& 0x80)) return NULL
;
145 if ((endpoint
->bmAttributes
& 3) != 3) return NULL
;
147 usb_set_protocol(dev
, interface
->bInterfaceNumber
, 0);
148 usb_set_idle(dev
, interface
->bInterfaceNumber
, 0, 0);
150 if (!(kbd
= kmalloc(sizeof(struct usb_kbd
), GFP_KERNEL
))) return NULL
;
151 memset(kbd
, 0, sizeof(struct usb_kbd
));
153 kbd
->dev
.evbit
[0] = BIT(EV_KEY
) | BIT(EV_LED
) | BIT(EV_REP
);
154 kbd
->dev
.ledbit
[0] = BIT(LED_NUML
) | BIT(LED_CAPSL
) | BIT(LED_SCROLLL
) | BIT(LED_COMPOSE
) | BIT(LED_KANA
);
156 for (i
= 0; i
< 255; i
++)
157 set_bit(usb_kbd_keycode
[i
], kbd
->dev
.keybit
);
158 clear_bit(0, kbd
->dev
.keybit
);
160 kbd
->dev
.private = kbd
;
161 kbd
->dev
.event
= usb_kbd_event
;
164 int pipe
= usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
);
165 int maxp
= usb_maxpacket(dev
, pipe
, usb_pipeout(pipe
));
167 FILL_INT_URB(&kbd
->irq
, dev
, pipe
, kbd
->new, maxp
> 8 ? 8 : maxp
,
168 usb_kbd_irq
, kbd
, endpoint
->bInterval
);
171 kbd
->dr
.requesttype
= USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
172 kbd
->dr
.request
= USB_REQ_SET_REPORT
;
173 kbd
->dr
.value
= 0x200;
174 kbd
->dr
.index
= interface
->bInterfaceNumber
;
177 FILL_CONTROL_URB(&kbd
->led
, dev
, usb_sndctrlpipe(dev
, 0),
178 (void*) &kbd
->dr
, &kbd
->leds
, 1, usb_kbd_led
, kbd
);
180 if (usb_submit_urb(&kbd
->irq
)) {
185 input_register_device(&kbd
->dev
);
187 printk(KERN_INFO
"input%d: USB HIDBP keyboard\n", kbd
->dev
.number
);
193 static void usb_kbd_disconnect(struct usb_device
*dev
, void *ptr
)
195 struct usb_kbd
*kbd
= ptr
;
196 usb_unlink_urb(&kbd
->irq
);
197 input_unregister_device(&kbd
->dev
);
201 static struct usb_driver usb_kbd_driver
= {
203 probe
: usb_kbd_probe
,
204 disconnect
: usb_kbd_disconnect
207 static int __init
usb_kbd_init(void)
209 usb_register(&usb_kbd_driver
);
213 static void __exit
usb_kbd_exit(void)
215 usb_deregister(&usb_kbd_driver
);
218 module_init(usb_kbd_init
);
219 module_exit(usb_kbd_exit
);