blob: f14bda0c04740e9cfd0cb2dc7d35f6dbeda17b9c [file] [log] [blame]
Patrick Georgid21f68b2008-09-02 16:06:22 +00001/*
2 * This file is part of the libpayload project.
3 *
Stefan Reinauerb56f2d02010-03-25 22:17:36 +00004 * Copyright (C) 2008-2010 coresystems GmbH
Patrick Georgid21f68b2008-09-02 16:06:22 +00005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
Stefan Reinauerb56f2d02010-03-25 22:17:36 +000030// #define USB_DEBUG
31
Gabe Blacka2d786f2013-03-12 15:44:56 -070032#include <keycodes.h>
Jordan Crouse29061a52008-09-11 17:29:00 +000033#include <usb/usb.h>
Patrick Georgid21f68b2008-09-02 16:06:22 +000034
35enum { hid_subclass_none = 0, hid_subclass_boot = 1 };
Patrick Georgi4727c072008-10-16 19:20:51 +000036typedef enum { hid_proto_boot = 0, hid_proto_report = 1 } hid_proto;
Patrick Georgid21f68b2008-09-02 16:06:22 +000037enum { hid_boot_proto_none = 0, hid_boot_proto_keyboard =
38 1, hid_boot_proto_mouse = 2
39};
40static const char *boot_protos[3] = { "(none)", "keyboard", "mouse" };
41enum { GET_REPORT = 0x1, GET_IDLE = 0x2, GET_PROTOCOL = 0x3, SET_REPORT =
42 0x9, SET_IDLE = 0xa, SET_PROTOCOL = 0xb
43};
44
Nico Hubercd587f12012-11-23 13:18:13 +010045typedef union {
46 struct {
47 u8 modifiers;
48 u8 repeats;
49 u8 keys[6];
50 };
51 u8 buffer[8];
52} usb_hid_keyboard_event_t;
53
Patrick Georgi4727c072008-10-16 19:20:51 +000054typedef struct {
55 void* queue;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +000056 hid_descriptor_t *descriptor;
Nico Hubercd587f12012-11-23 13:18:13 +010057
58 usb_hid_keyboard_event_t previous;
59 int lastkeypress;
60 int repeat_delay;
Patrick Georgi4727c072008-10-16 19:20:51 +000061} usbhid_inst_t;
62
63#define HID_INST(dev) ((usbhid_inst_t*)(dev)->data)
64
Nico Huber4842dfe2012-05-25 09:59:19 +020065static void
66usb_hid_destroy (usbdev_t *dev)
67{
68 if (HID_INST(dev)->queue) {
69 int i;
70 for (i = 0; i <= dev->num_endp; i++) {
71 if (dev->endpoints[i].endpoint == 0)
72 continue;
73 if (dev->endpoints[i].type != INTERRUPT)
74 continue;
75 if (dev->endpoints[i].direction != IN)
76 continue;
77 break;
78 }
79 dev->controller->destroy_intr_queue(
80 &dev->endpoints[i], HID_INST(dev)->queue);
81 HID_INST(dev)->queue = NULL;
82 }
83 free (dev->data);
84}
85
Stefan Reinauerb56f2d02010-03-25 22:17:36 +000086/* keybuffer is global to all USB keyboards */
87static int keycount;
88#define KEYBOARD_BUFFER_SIZE 16
89static short keybuffer[KEYBOARD_BUFFER_SIZE];
Patrick Georgi4727c072008-10-16 19:20:51 +000090
Patrick Georgi7f965832011-04-21 18:57:16 +020091const char *countries[36][2] = {
Stefan Reinauerb56f2d02010-03-25 22:17:36 +000092 { "not supported", "us" },
93 { "Arabic", "ae" },
94 { "Belgian", "be" },
95 { "Canadian-Bilingual", "ca" },
96 { "Canadian-French", "ca" },
97 { "Czech Republic", "cz" },
98 { "Danish", "dk" },
99 { "Finnish", "fi" },
100 { "French", "fr" },
101 { "German", "de" },
102 { "Greek", "gr" },
103 { "Hebrew", "il" },
104 { "Hungary", "hu" },
105 { "International (ISO)", "iso" },
106 { "Italian", "it" },
107 { "Japan (Katakana)", "jp" },
108 { "Korean", "us" },
109 { "Latin American", "us" },
110 { "Netherlands/Dutch", "nl" },
111 { "Norwegian", "no" },
112 { "Persian (Farsi)", "ir" },
113 { "Poland", "pl" },
114 { "Portuguese", "pt" },
Stefan Reinauer14e22772010-04-27 06:56:47 +0000115 { "Russia", "ru" },
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000116 { "Slovakia", "sl" },
117 { "Spanish", "es" },
118 { "Swedish", "se" },
119 { "Swiss/French", "ch" },
120 { "Swiss/German", "ch" },
121 { "Switzerland", "ch" },
122 { "Taiwan", "tw" },
123 { "Turkish-Q", "tr" },
124 { "UK", "uk" },
125 { "US", "us" },
126 { "Yugoslavia", "yu" },
127 { "Turkish-F", "tr" },
128 /* 36 - 255: Reserved */
Patrick Georgid21f68b2008-09-02 16:06:22 +0000129};
130
131
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000132
133struct layout_maps {
Patrick Georgi7f965832011-04-21 18:57:16 +0200134 const char *country;
135 const short map[4][0x80];
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000136};
137
Patrick Georgi7f965832011-04-21 18:57:16 +0200138static const struct layout_maps *map;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000139
Patrick Georgi7f965832011-04-21 18:57:16 +0200140static const struct layout_maps keyboard_layouts[] = {
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000141// #ifdef CONFIG_PC_KEYBOARD_LAYOUT_US
142{ .country = "us", .map = {
143 { /* No modifier */
144 -1, -1, -1, -1, 'a', 'b', 'c', 'd',
145 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
146 /* 0x10 */
147 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
148 'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
149 /* 0x20 */
150 '3', '4', '5', '6', '7', '8', '9', '0',
151 '\n', '\e', '\b', '\t', ' ', '-', '=', '[',
152 /* 0x30 */
153 ']', '\\', -1, ';', '\'', '`', ',', '.',
154 '/', -1 /* CapsLk */, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5), KEY_F(6),
155 /* 0x40 */
156 KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), KEY_F(11), KEY_F(12), KEY_PRINT, -1 /* ScrLk */,
157 KEY_BREAK, KEY_IC, KEY_HOME, KEY_PPAGE, KEY_DC, KEY_END, KEY_NPAGE, KEY_RIGHT,
158 /* 50 */
159 KEY_LEFT, KEY_DOWN, KEY_UP, -1 /*NumLck*/, '/', '*', '-' /* = ? */, '+',
160 KEY_ENTER, KEY_END, KEY_DOWN, KEY_NPAGE, KEY_LEFT, -1, KEY_RIGHT, KEY_HOME,
161 /* 60 */
162 KEY_UP, KEY_PPAGE, -1, KEY_DC, -1 /* < > | */, -1 /* Win Key Right */, -1, -1,
163 -1, -1, -1, -1, -1, -1, -1, -1,
164 /* 70 */
165 -1, -1, -1, -1, -1, -1, -1, -1,
166 -1, -1, -1, -1, -1, -1, -1, -1,
167 },
168 { /* Shift modifier */
169 -1, -1, -1, -1, 'A', 'B', 'C', 'D',
170 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
171 /* 0x10 */
172 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
173 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@',
174 /* 0x20 */
175 '#', '$', '%', '^', '&', '*', '(', ')',
Patrick Georgi28b900a2012-11-27 10:19:59 +0100176 '\n', '\e', '\b', '\t', ' ', '_', '+', '[',
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000177 /* 0x30 */
178 ']', '\\', -1, ':', '\'', '`', ',', '.',
179 '/', -1 /* CapsLk */, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5), KEY_F(6),
180 /* 0x40 */
181 KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), KEY_F(11), KEY_F(12), KEY_PRINT, -1 /* ScrLk */,
182 KEY_BREAK, KEY_IC, KEY_HOME, KEY_PPAGE, KEY_DC, KEY_END, KEY_NPAGE, KEY_RIGHT,
183 /* 50 */
184 KEY_LEFT, KEY_DOWN, KEY_UP, -1 /*NumLck*/, '/', '*', '-' /* = ? */, '+',
185 KEY_ENTER, KEY_END, KEY_DOWN, KEY_NPAGE, KEY_LEFT, -1, KEY_RIGHT, KEY_HOME,
186 /* 60 */
187 KEY_UP, KEY_PPAGE, -1, KEY_DC, -1 /* < > | */, -1 /* Win Key Right */, -1, -1,
188 -1, -1, -1, -1, -1, -1, -1, -1,
189 /* 70 */
190 -1, -1, -1, -1, -1, -1, -1, -1,
191 -1, -1, -1, -1, -1, -1, -1, -1,
192 },
193 { /* Alt */
194 -1, -1, -1, -1, 'a', 'b', 'c', 'd',
195 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
196 /* 0x10 */
197 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
198 'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
199 /* 0x20 */
200 '3', '4', '5', '6', '7', '8', '9', '0',
201 '\n', '\e', '\b', '\t', ' ', '-', '=', '[',
202 /* 0x30 */
203 ']', '\\', -1, ';', '\'', '`', ',', '.',
204 '/', -1 /* CapsLk */, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5), KEY_F(6),
205 /* 0x40 */
206 KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), KEY_F(11), KEY_F(12), KEY_PRINT, -1 /* ScrLk */,
207 KEY_BREAK, KEY_IC, KEY_HOME, KEY_PPAGE, KEY_DC, KEY_END, KEY_NPAGE, KEY_RIGHT,
208 /* 50 */
209 KEY_LEFT, KEY_DOWN, KEY_UP, -1 /*NumLck*/, '/', '*', '-' /* = ? */, '+',
210 KEY_ENTER, KEY_END, KEY_DOWN, KEY_NPAGE, KEY_LEFT, -1, KEY_RIGHT, KEY_HOME,
211 /* 60 */
212 KEY_UP, KEY_PPAGE, -1, KEY_DC, -1 /* < > | */, -1 /* Win Key Right */, -1, -1,
213 -1, -1, -1, -1, -1, -1, -1, -1,
214 /* 70 */
215 -1, -1, -1, -1, -1, -1, -1, -1,
216 -1, -1, -1, -1, -1, -1, -1, -1,
217 },
218 { /* Shift+Alt modifier */
219 -1, -1, -1, -1, 'A', 'B', 'C', 'D',
220 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
221 /* 0x10 */
222 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
223 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@',
224 /* 0x20 */
225 '#', '$', '%', '^', '&', '*', '(', ')',
226 '\n', '\e', '\b', '\t', ' ', '-', '=', '[',
227 /* 0x30 */
228 ']', '\\', -1, ':', '\'', '`', ',', '.',
229 '/', -1 /* CapsLk */, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5), KEY_F(6),
230 /* 0x40 */
231 KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), KEY_F(11), KEY_F(12), KEY_PRINT, -1 /* ScrLk */,
232 KEY_BREAK, KEY_IC, KEY_HOME, KEY_PPAGE, KEY_DC, KEY_END, KEY_NPAGE, KEY_RIGHT,
233 /* 50 */
234 KEY_LEFT, KEY_DOWN, KEY_UP, -1 /*NumLck*/, '/', '*', '-' /* = ? */, '+',
235 KEY_ENTER, KEY_END, KEY_DOWN, KEY_NPAGE, KEY_LEFT, -1, KEY_RIGHT, KEY_HOME,
236 /* 60 */
237 KEY_UP, KEY_PPAGE, -1, KEY_DC, -1 /* < > | */, -1 /* Win Key Right */, -1, -1,
238 -1, -1, -1, -1, -1, -1, -1, -1,
239 /* 70 */
240 -1, -1, -1, -1, -1, -1, -1, -1,
241 -1, -1, -1, -1, -1, -1, -1, -1,
242 }
243}},
244//#endif
245};
246
247#define MOD_SHIFT (1 << 0)
248#define MOD_ALT (1 << 1)
249#define MOD_CTRL (1 << 2)
250
251static void usb_hid_keyboard_queue(int ch) {
252 /* ignore key presses if buffer full */
253 if (keycount < KEYBOARD_BUFFER_SIZE)
254 keybuffer[keycount++] = ch;
255}
256
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000257#define KEYBOARD_REPEAT_MS 30
258#define INITIAL_REPEAT_DELAY 10
259#define REPEAT_DELAY 2
260
Stefan Reinauer14e22772010-04-27 06:56:47 +0000261static void
Nico Hubercd587f12012-11-23 13:18:13 +0100262usb_hid_process_keyboard_event(usbhid_inst_t *const inst,
263 const usb_hid_keyboard_event_t *const current)
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000264{
Nico Hubercd587f12012-11-23 13:18:13 +0100265 const usb_hid_keyboard_event_t *const previous = &inst->previous;
266
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000267 int i, keypress = 0, modifiers = 0;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000268
269 if (current->modifiers & 0x01) /* Left-Ctrl */ modifiers |= MOD_CTRL;
270 if (current->modifiers & 0x02) /* Left-Shift */ modifiers |= MOD_SHIFT;
271 if (current->modifiers & 0x04) /* Left-Alt */ modifiers |= MOD_ALT;
272 if (current->modifiers & 0x08) /* Left-GUI */ ;
273 if (current->modifiers & 0x10) /* Right-Ctrl */ modifiers |= MOD_CTRL;
274 if (current->modifiers & 0x20) /* Right-Shift */ modifiers |= MOD_SHIFT;
275 if (current->modifiers & 0x40) /* Right-AltGr */ modifiers |= MOD_ALT;
276 if (current->modifiers & 0x80) /* Right-GUI */ ;
277
278 if ((current->modifiers & 0x05) && ((current->keys[0] == 0x4c) ||
279 (current->keys[0]==0x63))) {
280 /* vulcan nerve pinch */
281 if (reset_handler)
282 reset_handler();
283 }
284
285 /* Did the event change at all? */
Nico Hubercd587f12012-11-23 13:18:13 +0100286 if (inst->lastkeypress &&
287 !memcmp(current, previous, sizeof(*current))) {
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000288 /* No. Then it's a key repeat event. */
Nico Hubercd587f12012-11-23 13:18:13 +0100289 if (inst->repeat_delay) {
290 inst->repeat_delay--;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000291 } else {
Nico Hubercd587f12012-11-23 13:18:13 +0100292 usb_hid_keyboard_queue(inst->lastkeypress);
293 inst->repeat_delay = REPEAT_DELAY;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000294 }
295
296 return;
297 }
298
Nico Hubercd587f12012-11-23 13:18:13 +0100299 inst->lastkeypress = 0;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000300
301 for (i=0; i<6; i++) {
302 int j;
303 int skip = 0;
304 // No more keys? skip
305 if (current->keys[i] == 0)
306 return;
307
308 for (j=0; j<6; j++) {
309 if (current->keys[i] == previous->keys[j]) {
310 skip = 1;
311 break;
312 }
313 }
314 if (skip)
315 continue;
316
317
318 /* Mask off MOD_CTRL */
319 keypress = map->map[modifiers & 0x03][current->keys[i]];
320
321 if (modifiers & MOD_CTRL) {
322 switch (keypress) {
323 case 'a' ... 'z':
324 keypress &= 0x1f;
325 break;
326 default:
327 continue;
328 }
329 }
330
331 if (keypress == -1) {
332 /* Debug: Print unknown keys */
Gabe Black93ded592012-11-01 15:44:10 -0700333 usb_debug ("usbhid: <%x> %x [ %x %x %x %x %x %x ] %d\n",
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000334 current->modifiers, current->repeats,
335 current->keys[0], current->keys[1],
336 current->keys[2], current->keys[3],
337 current->keys[4], current->keys[5], i);
338
339 /* Unknown key? Try next one in the queue */
340 continue;
341 }
342
343 usb_hid_keyboard_queue(keypress);
344
345 /* Remember for authentic key repeat */
Nico Hubercd587f12012-11-23 13:18:13 +0100346 inst->lastkeypress = keypress;
347 inst->repeat_delay = INITIAL_REPEAT_DELAY;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000348 }
349}
350
Patrick Georgid21f68b2008-09-02 16:06:22 +0000351static void
352usb_hid_poll (usbdev_t *dev)
353{
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000354 usb_hid_keyboard_event_t current;
Nico Hubercd587f12012-11-23 13:18:13 +0100355 const u8 *buf;
356
Patrick Georgi4727c072008-10-16 19:20:51 +0000357 while ((buf=dev->controller->poll_intr_queue (HID_INST(dev)->queue))) {
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000358 memcpy(&current.buffer, buf, 8);
Nico Hubercd587f12012-11-23 13:18:13 +0100359 usb_hid_process_keyboard_event(HID_INST(dev), &current);
360 HID_INST(dev)->previous = current;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000361 }
362}
363
Patrick Georgi4727c072008-10-16 19:20:51 +0000364static void
365usb_hid_set_idle (usbdev_t *dev, interface_descriptor_t *interface, u16 duration)
Patrick Georgid21f68b2008-09-02 16:06:22 +0000366{
Patrick Georgi4727c072008-10-16 19:20:51 +0000367 dev_req_t dr;
368 dr.data_dir = host_to_device;
369 dr.req_type = class_type;
370 dr.req_recp = iface_recp;
371 dr.bRequest = SET_IDLE;
372 dr.wValue = (duration >> 2) << 8;
373 dr.wIndex = interface->bInterfaceNumber;
374 dr.wLength = 0;
375 dev->controller->control (dev, OUT, sizeof (dev_req_t), &dr, 0, 0);
376}
377
378static void
379usb_hid_set_protocol (usbdev_t *dev, interface_descriptor_t *interface, hid_proto proto)
380{
381 dev_req_t dr;
382 dr.data_dir = host_to_device;
383 dr.req_type = class_type;
384 dr.req_recp = iface_recp;
385 dr.bRequest = SET_PROTOCOL;
386 dr.wValue = proto;
387 dr.wIndex = interface->bInterfaceNumber;
388 dr.wLength = 0;
389 dev->controller->control (dev, OUT, sizeof (dev_req_t), &dr, 0, 0);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000390}
391
Patrick Georgi657a6dc2008-10-21 15:08:18 +0000392static struct console_input_driver cons = {
393 .havekey = usbhid_havechar,
394 .getchar = usbhid_getchar
395};
396
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000397
Patrick Georgi7f965832011-04-21 18:57:16 +0200398static int usb_hid_set_layout (const char *country)
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000399{
400 /* FIXME should be per keyboard */
401 int i;
402
403 for (i=0; i<ARRAY_SIZE(keyboard_layouts); i++) {
404 if (strncmp(keyboard_layouts[i].country, country,
405 strlen(keyboard_layouts[i].country)))
406 continue;
407
408 /* Found, changing keyboard layout */
409 map = &keyboard_layouts[i];
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700410 usb_debug(" Keyboard layout '%s'\n", map->country);
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000411 return 0;
412 }
413
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700414 usb_debug(" Keyboard layout '%s' not found, using '%s'\n",
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000415 country, map->country);
416
417 /* Nothing found, not changed */
418 return -1;
419}
420
Patrick Georgid21f68b2008-09-02 16:06:22 +0000421void
422usb_hid_init (usbdev_t *dev)
423{
424
Patrick Georgi657a6dc2008-10-21 15:08:18 +0000425 static int installed = 0;
426 if (!installed) {
427 installed = 1;
428 console_add_input_driver (&cons);
429 }
430
Patrick Georgi4727c072008-10-16 19:20:51 +0000431 configuration_descriptor_t *cd = (configuration_descriptor_t*)dev->configuration;
432 interface_descriptor_t *interface = (interface_descriptor_t*)(((char *) cd) + cd->bLength);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000433
434 if (interface->bInterfaceSubClass == hid_subclass_boot) {
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000435 u8 countrycode;
Gabe Black93ded592012-11-01 15:44:10 -0700436 usb_debug (" supports boot interface..\n");
437 usb_debug (" it's a %s\n",
Patrick Georgid21f68b2008-09-02 16:06:22 +0000438 boot_protos[interface->bInterfaceProtocol]);
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000439 switch (interface->bInterfaceProtocol) {
440 case hid_boot_proto_keyboard:
Patrick Georgi4727c072008-10-16 19:20:51 +0000441 dev->data = malloc (sizeof (usbhid_inst_t));
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000442 if (!dev->data)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100443 fatal("Not enough memory for USB HID device.\n");
Nico Hubercd587f12012-11-23 13:18:13 +0100444 memset(&HID_INST(dev)->previous, 0x00,
445 sizeof(HID_INST(dev)->previous));
Gabe Black93ded592012-11-01 15:44:10 -0700446 usb_debug (" configuring...\n");
Patrick Georgi4727c072008-10-16 19:20:51 +0000447 usb_hid_set_protocol(dev, interface, hid_proto_boot);
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000448 usb_hid_set_idle(dev, interface, KEYBOARD_REPEAT_MS);
Gabe Black93ded592012-11-01 15:44:10 -0700449 usb_debug (" activating...\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000450
Stefan Reinauer14e22772010-04-27 06:56:47 +0000451 HID_INST (dev)->descriptor =
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000452 (hid_descriptor_t *)
453 get_descriptor(dev, gen_bmRequestType
Stefan Reinauer14e22772010-04-27 06:56:47 +0000454 (device_to_host, standard_type, iface_recp),
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000455 0x21, 0, 0);
456 countrycode = HID_INST(dev)->descriptor->bCountryCode;
457 /* 35 countries defined: */
458 if (countrycode > 35)
459 countrycode = 0;
Gabe Black93ded592012-11-01 15:44:10 -0700460 usb_debug (" Keyboard has %s layout (country code %02x)\n",
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000461 countries[countrycode][0], countrycode);
462
463 /* Set keyboard layout accordingly */
464 usb_hid_set_layout(countries[countrycode][1]);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000465
466 // only add here, because we only support boot-keyboard HID devices
Patrick Georgid21f68b2008-09-02 16:06:22 +0000467 dev->destroy = usb_hid_destroy;
468 dev->poll = usb_hid_poll;
Patrick Georgi4727c072008-10-16 19:20:51 +0000469 int i;
Stefan Reinauerd233f362009-04-30 16:46:12 +0000470 for (i = 0; i <= dev->num_endp; i++) {
Patrick Georgi4727c072008-10-16 19:20:51 +0000471 if (dev->endpoints[i].endpoint == 0)
472 continue;
473 if (dev->endpoints[i].type != INTERRUPT)
474 continue;
475 if (dev->endpoints[i].direction != IN)
476 continue;
477 break;
478 }
Gabe Black93ded592012-11-01 15:44:10 -0700479 usb_debug (" found endpoint %x for interrupt-in\n", i);
Patrick Georgi4727c072008-10-16 19:20:51 +0000480 /* 20 buffers of 8 bytes, for every 10 msecs */
481 HID_INST(dev)->queue = dev->controller->create_intr_queue (&dev->endpoints[i], 8, 20, 10);
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000482 keycount = 0;
Gabe Black93ded592012-11-01 15:44:10 -0700483 usb_debug (" configuration done.\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000484 break;
485 case hid_boot_proto_mouse:
Gabe Black93ded592012-11-01 15:44:10 -0700486 usb_debug("NOTICE: USB mice are not supported.\n");
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000487 break;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000488 }
489 }
490}
Patrick Georgi4727c072008-10-16 19:20:51 +0000491
492int usbhid_havechar (void)
493{
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000494 return (keycount != 0);
Patrick Georgi4727c072008-10-16 19:20:51 +0000495}
496
497int usbhid_getchar (void)
498{
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000499 short ret;
500
501 if (keycount == 0)
502 return 0;
503 ret = keybuffer[0];
504 memmove(keybuffer, keybuffer + 1, --keycount);
505
506 return (int)ret;
Patrick Georgi4727c072008-10-16 19:20:51 +0000507}
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000508