libpayload: Reduce verbosity in USB stack
[coreboot.git] / payloads / libpayload / drivers / usb / usbhid.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2008-2010 coresystems GmbH
5  *
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
30 // #define USB_DEBUG
31
32 #include <usb/usb.h>
33 #include <curses.h>
34
35 enum { hid_subclass_none = 0, hid_subclass_boot = 1 };
36 typedef enum { hid_proto_boot = 0, hid_proto_report = 1 } hid_proto;
37 enum { hid_boot_proto_none = 0, hid_boot_proto_keyboard =
38                 1, hid_boot_proto_mouse = 2
39 };
40 static const char *boot_protos[3] = { "(none)", "keyboard", "mouse" };
41 enum { GET_REPORT = 0x1, GET_IDLE = 0x2, GET_PROTOCOL = 0x3, SET_REPORT =
42                 0x9, SET_IDLE = 0xa, SET_PROTOCOL = 0xb
43 };
44
45 static void
46 usb_hid_destroy (usbdev_t *dev)
47 {
48         free (dev->data);
49 }
50
51 typedef struct {
52         void* queue;
53         hid_descriptor_t *descriptor;
54 } usbhid_inst_t;
55
56 #define HID_INST(dev) ((usbhid_inst_t*)(dev)->data)
57
58 /* keybuffer is global to all USB keyboards */
59 static int keycount;
60 #define KEYBOARD_BUFFER_SIZE 16
61 static short keybuffer[KEYBOARD_BUFFER_SIZE];
62
63 const char *countries[36][2] = {
64         { "not supported", "us" },
65         { "Arabic", "ae" },
66         { "Belgian", "be" },
67         { "Canadian-Bilingual", "ca" },
68         { "Canadian-French", "ca" },
69         { "Czech Republic", "cz" },
70         { "Danish", "dk" },
71         { "Finnish", "fi" },
72         { "French", "fr" },
73         { "German", "de" },
74         { "Greek", "gr" },
75         { "Hebrew", "il" },
76         { "Hungary", "hu" },
77         { "International (ISO)", "iso" },
78         { "Italian", "it" },
79         { "Japan (Katakana)", "jp" },
80         { "Korean", "us" },
81         { "Latin American", "us" },
82         { "Netherlands/Dutch", "nl" },
83         { "Norwegian", "no" },
84         { "Persian (Farsi)", "ir" },
85         { "Poland", "pl" },
86         { "Portuguese", "pt" },
87         { "Russia", "ru" },
88         { "Slovakia", "sl" },
89         { "Spanish", "es" },
90         { "Swedish", "se" },
91         { "Swiss/French", "ch" },
92         { "Swiss/German", "ch" },
93         { "Switzerland", "ch" },
94         { "Taiwan", "tw" },
95         { "Turkish-Q", "tr" },
96         { "UK", "uk" },
97         { "US", "us" },
98         { "Yugoslavia", "yu" },
99         { "Turkish-F", "tr" },
100         /* 36 - 255: Reserved */
101 };
102
103
104
105 struct layout_maps {
106         const char *country;
107         const short map[4][0x80];
108 };
109
110 static const struct layout_maps *map;
111
112 static const struct layout_maps keyboard_layouts[] = {
113 // #ifdef CONFIG_PC_KEYBOARD_LAYOUT_US
114 { .country = "us", .map = {
115         { /* No modifier */
116         -1, -1, -1, -1, 'a', 'b', 'c', 'd',
117         'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
118         /* 0x10 */
119         'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
120         'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
121         /* 0x20 */
122         '3', '4', '5', '6', '7', '8', '9', '0',
123         '\n', '\e', '\b', '\t', ' ', '-', '=', '[',
124         /* 0x30 */
125         ']', '\\', -1, ';', '\'', '`', ',', '.',
126         '/', -1 /* CapsLk */, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5), KEY_F(6),
127         /* 0x40 */
128         KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), KEY_F(11), KEY_F(12), KEY_PRINT, -1 /* ScrLk */,
129         KEY_BREAK, KEY_IC, KEY_HOME, KEY_PPAGE, KEY_DC, KEY_END, KEY_NPAGE, KEY_RIGHT,
130         /* 50 */
131         KEY_LEFT, KEY_DOWN, KEY_UP, -1 /*NumLck*/, '/', '*', '-' /* = ? */, '+',
132         KEY_ENTER, KEY_END, KEY_DOWN, KEY_NPAGE, KEY_LEFT, -1, KEY_RIGHT, KEY_HOME,
133         /* 60 */
134         KEY_UP, KEY_PPAGE, -1, KEY_DC, -1 /* < > | */, -1 /* Win Key Right */, -1, -1,
135         -1, -1, -1, -1, -1, -1, -1, -1,
136         /* 70 */
137         -1, -1, -1, -1, -1, -1, -1, -1,
138         -1, -1, -1, -1, -1, -1, -1, -1,
139          },
140         { /* Shift modifier */
141         -1, -1, -1, -1, 'A', 'B', 'C', 'D',
142         'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
143         /* 0x10 */
144         'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
145         'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@',
146         /* 0x20 */
147         '#', '$', '%', '^', '&', '*', '(', ')',
148         '\n', '\e', '\b', '\t', ' ', '-', '=', '[',
149         /* 0x30 */
150         ']', '\\', -1, ':', '\'', '`', ',', '.',
151         '/', -1 /* CapsLk */, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5), KEY_F(6),
152         /* 0x40 */
153         KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), KEY_F(11), KEY_F(12), KEY_PRINT, -1 /* ScrLk */,
154         KEY_BREAK, KEY_IC, KEY_HOME, KEY_PPAGE, KEY_DC, KEY_END, KEY_NPAGE, KEY_RIGHT,
155         /* 50 */
156         KEY_LEFT, KEY_DOWN, KEY_UP, -1 /*NumLck*/, '/', '*', '-' /* = ? */, '+',
157         KEY_ENTER, KEY_END, KEY_DOWN, KEY_NPAGE, KEY_LEFT, -1, KEY_RIGHT, KEY_HOME,
158         /* 60 */
159         KEY_UP, KEY_PPAGE, -1, KEY_DC, -1 /* < > | */, -1 /* Win Key Right */, -1, -1,
160         -1, -1, -1, -1, -1, -1, -1, -1,
161         /* 70 */
162         -1, -1, -1, -1, -1, -1, -1, -1,
163         -1, -1, -1, -1, -1, -1, -1, -1,
164          },
165         { /* Alt */
166         -1, -1, -1, -1, 'a', 'b', 'c', 'd',
167         'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
168         /* 0x10 */
169         'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
170         'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
171         /* 0x20 */
172         '3', '4', '5', '6', '7', '8', '9', '0',
173         '\n', '\e', '\b', '\t', ' ', '-', '=', '[',
174         /* 0x30 */
175         ']', '\\', -1, ';', '\'', '`', ',', '.',
176         '/', -1 /* CapsLk */, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5), KEY_F(6),
177         /* 0x40 */
178         KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), KEY_F(11), KEY_F(12), KEY_PRINT, -1 /* ScrLk */,
179         KEY_BREAK, KEY_IC, KEY_HOME, KEY_PPAGE, KEY_DC, KEY_END, KEY_NPAGE, KEY_RIGHT,
180         /* 50 */
181         KEY_LEFT, KEY_DOWN, KEY_UP, -1 /*NumLck*/, '/', '*', '-' /* = ? */, '+',
182         KEY_ENTER, KEY_END, KEY_DOWN, KEY_NPAGE, KEY_LEFT, -1, KEY_RIGHT, KEY_HOME,
183         /* 60 */
184         KEY_UP, KEY_PPAGE, -1, KEY_DC, -1 /* < > | */, -1 /* Win Key Right */, -1, -1,
185         -1, -1, -1, -1, -1, -1, -1, -1,
186         /* 70 */
187         -1, -1, -1, -1, -1, -1, -1, -1,
188         -1, -1, -1, -1, -1, -1, -1, -1,
189          },
190         { /* Shift+Alt modifier */
191         -1, -1, -1, -1, 'A', 'B', 'C', 'D',
192         'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
193         /* 0x10 */
194         'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
195         'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@',
196         /* 0x20 */
197         '#', '$', '%', '^', '&', '*', '(', ')',
198         '\n', '\e', '\b', '\t', ' ', '-', '=', '[',
199         /* 0x30 */
200         ']', '\\', -1, ':', '\'', '`', ',', '.',
201         '/', -1 /* CapsLk */, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5), KEY_F(6),
202         /* 0x40 */
203         KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), KEY_F(11), KEY_F(12), KEY_PRINT, -1 /* ScrLk */,
204         KEY_BREAK, KEY_IC, KEY_HOME, KEY_PPAGE, KEY_DC, KEY_END, KEY_NPAGE, KEY_RIGHT,
205         /* 50 */
206         KEY_LEFT, KEY_DOWN, KEY_UP, -1 /*NumLck*/, '/', '*', '-' /* = ? */, '+',
207         KEY_ENTER, KEY_END, KEY_DOWN, KEY_NPAGE, KEY_LEFT, -1, KEY_RIGHT, KEY_HOME,
208         /* 60 */
209         KEY_UP, KEY_PPAGE, -1, KEY_DC, -1 /* < > | */, -1 /* Win Key Right */, -1, -1,
210         -1, -1, -1, -1, -1, -1, -1, -1,
211         /* 70 */
212         -1, -1, -1, -1, -1, -1, -1, -1,
213         -1, -1, -1, -1, -1, -1, -1, -1,
214          }
215 }},
216 //#endif
217 };
218
219 #define MOD_SHIFT    (1 << 0)
220 #define MOD_ALT      (1 << 1)
221 #define MOD_CTRL     (1 << 2)
222
223 static void usb_hid_keyboard_queue(int ch) {
224         /* ignore key presses if buffer full */
225         if (keycount < KEYBOARD_BUFFER_SIZE)
226                 keybuffer[keycount++] = ch;
227 }
228
229 typedef union {
230         struct {
231                 u8 modifiers;
232                 u8 repeats;
233                 u8 keys[6];
234         };
235         u8 buffer[8];
236 } usb_hid_keyboard_event_t;
237
238 #define KEYBOARD_REPEAT_MS      30
239 #define INITIAL_REPEAT_DELAY    10
240 #define REPEAT_DELAY             2
241
242 static void
243 usb_hid_process_keyboard_event(usb_hid_keyboard_event_t *current,
244                 usb_hid_keyboard_event_t *previous)
245 {
246         int i, keypress = 0, modifiers = 0;
247         static int lastkeypress = 0, repeat_delay = INITIAL_REPEAT_DELAY;
248
249         if (current->modifiers & 0x01) /* Left-Ctrl */   modifiers |= MOD_CTRL;
250         if (current->modifiers & 0x02) /* Left-Shift */  modifiers |= MOD_SHIFT;
251         if (current->modifiers & 0x04) /* Left-Alt */    modifiers |= MOD_ALT;
252         if (current->modifiers & 0x08) /* Left-GUI */    ;
253         if (current->modifiers & 0x10) /* Right-Ctrl */  modifiers |= MOD_CTRL;
254         if (current->modifiers & 0x20) /* Right-Shift */ modifiers |= MOD_SHIFT;
255         if (current->modifiers & 0x40) /* Right-AltGr */ modifiers |= MOD_ALT;
256         if (current->modifiers & 0x80) /* Right-GUI */   ;
257
258         if ((current->modifiers & 0x05) && ((current->keys[0] == 0x4c) ||
259                                 (current->keys[0]==0x63))) {
260                 /* vulcan nerve pinch */
261                 if (reset_handler)
262                         reset_handler();
263         }
264
265         /* Did the event change at all? */
266         if (lastkeypress && !memcmp(current, previous, sizeof(usb_hid_keyboard_event_t))) {
267                 /* No. Then it's a key repeat event. */
268                 if (repeat_delay) {
269                         repeat_delay--;
270                 } else {
271                         usb_hid_keyboard_queue(lastkeypress);
272                         repeat_delay = REPEAT_DELAY;
273                 }
274
275                 return;
276         }
277
278         lastkeypress = 0;
279
280         for (i=0; i<6; i++) {
281                 int j;
282                 int skip = 0;
283                 // No more keys? skip
284                 if (current->keys[i] == 0)
285                         return;
286
287                 for (j=0; j<6; j++) {
288                         if (current->keys[i] == previous->keys[j]) {
289                                 skip = 1;
290                                 break;
291                         }
292                 }
293                 if (skip)
294                         continue;
295
296
297                 /* Mask off MOD_CTRL */
298                 keypress = map->map[modifiers & 0x03][current->keys[i]];
299
300                 if (modifiers & MOD_CTRL) {
301                         switch (keypress) {
302                         case 'a' ... 'z':
303                                 keypress &= 0x1f;
304                                 break;
305                         default:
306                                 continue;
307                         }
308                 }
309
310                 if (keypress == -1) {
311                         /* Debug: Print unknown keys */
312                         debug ("usbhid: <%x> %x [ %x %x %x %x %x %x ] %d\n",
313                                 current->modifiers, current->repeats,
314                         current->keys[0], current->keys[1],
315                         current->keys[2], current->keys[3],
316                         current->keys[4], current->keys[5], i);
317
318                         /* Unknown key? Try next one in the queue */
319                         continue;
320                 }
321
322                 usb_hid_keyboard_queue(keypress);
323
324                 /* Remember for authentic key repeat */
325                 lastkeypress = keypress;
326                 repeat_delay = INITIAL_REPEAT_DELAY;
327         }
328 }
329
330 static void
331 usb_hid_poll (usbdev_t *dev)
332 {
333         usb_hid_keyboard_event_t current;
334         static usb_hid_keyboard_event_t previous = {
335                 .buffer = { 0, 0, 0, 0, 0, 0, 0, 0}
336         };
337         u8* buf;
338         while ((buf=dev->controller->poll_intr_queue (HID_INST(dev)->queue))) {
339                 memcpy(&current.buffer, buf, 8);
340                 usb_hid_process_keyboard_event(&current, &previous);
341                 previous = current;
342         }
343 }
344
345 static void
346 usb_hid_set_idle (usbdev_t *dev, interface_descriptor_t *interface, u16 duration)
347 {
348         dev_req_t dr;
349         dr.data_dir = host_to_device;
350         dr.req_type = class_type;
351         dr.req_recp = iface_recp;
352         dr.bRequest = SET_IDLE;
353         dr.wValue = (duration >> 2) << 8;
354         dr.wIndex = interface->bInterfaceNumber;
355         dr.wLength = 0;
356         dev->controller->control (dev, OUT, sizeof (dev_req_t), &dr, 0, 0);
357 }
358
359 static void
360 usb_hid_set_protocol (usbdev_t *dev, interface_descriptor_t *interface, hid_proto proto)
361 {
362         dev_req_t dr;
363         dr.data_dir = host_to_device;
364         dr.req_type = class_type;
365         dr.req_recp = iface_recp;
366         dr.bRequest = SET_PROTOCOL;
367         dr.wValue = proto;
368         dr.wIndex = interface->bInterfaceNumber;
369         dr.wLength = 0;
370         dev->controller->control (dev, OUT, sizeof (dev_req_t), &dr, 0, 0);
371 }
372
373 static struct console_input_driver cons = {
374         .havekey = usbhid_havechar,
375         .getchar = usbhid_getchar
376 };
377
378
379 static int usb_hid_set_layout (const char *country)
380 {
381         /* FIXME should be per keyboard */
382         int i;
383
384         for (i=0; i<ARRAY_SIZE(keyboard_layouts); i++) {
385                 if (strncmp(keyboard_layouts[i].country, country,
386                                         strlen(keyboard_layouts[i].country)))
387                         continue;
388
389                 /* Found, changing keyboard layout */
390                 map = &keyboard_layouts[i];
391                 printf("  Keyboard layout '%s'\n", map->country);
392                 return 0;
393         }
394
395         printf("  Keyboard layout '%s' not found, using '%s'\n",
396                         country, map->country);
397
398         /* Nothing found, not changed */
399         return -1;
400 }
401
402 void
403 usb_hid_init (usbdev_t *dev)
404 {
405
406         static int installed = 0;
407         if (!installed) {
408                 installed = 1;
409                 console_add_input_driver (&cons);
410         }
411
412         configuration_descriptor_t *cd = (configuration_descriptor_t*)dev->configuration;
413         interface_descriptor_t *interface = (interface_descriptor_t*)(((char *) cd) + cd->bLength);
414
415         if (interface->bInterfaceSubClass == hid_subclass_boot) {
416                 u8 countrycode;
417                 debug ("  supports boot interface..\n");
418                 debug ("  it's a %s\n",
419                         boot_protos[interface->bInterfaceProtocol]);
420                 switch (interface->bInterfaceProtocol) {
421                 case hid_boot_proto_keyboard:
422                         dev->data = malloc (sizeof (usbhid_inst_t));
423                         if (!dev->data)
424                                 usb_fatal("Not enough memory for USB HID device.\n");
425                         debug ("  configuring...\n");
426                         usb_hid_set_protocol(dev, interface, hid_proto_boot);
427                         usb_hid_set_idle(dev, interface, KEYBOARD_REPEAT_MS);
428                         debug ("  activating...\n");
429
430                         HID_INST (dev)->descriptor =
431                                 (hid_descriptor_t *)
432                                         get_descriptor(dev, gen_bmRequestType
433                                         (device_to_host, standard_type, iface_recp),
434                                         0x21, 0, 0);
435                         countrycode = HID_INST(dev)->descriptor->bCountryCode;
436                         /* 35 countries defined: */
437                         if (countrycode > 35)
438                                 countrycode = 0;
439                         debug ("  Keyboard has %s layout (country code %02x)\n",
440                                         countries[countrycode][0], countrycode);
441
442                         /* Set keyboard layout accordingly */
443                         usb_hid_set_layout(countries[countrycode][1]);
444
445                         // only add here, because we only support boot-keyboard HID devices
446                         dev->destroy = usb_hid_destroy;
447                         dev->poll = usb_hid_poll;
448                         int i;
449                         for (i = 0; i <= dev->num_endp; i++) {
450                                 if (dev->endpoints[i].endpoint == 0)
451                                         continue;
452                                 if (dev->endpoints[i].type != INTERRUPT)
453                                         continue;
454                                 if (dev->endpoints[i].direction != IN)
455                                         continue;
456                                 break;
457                         }
458                         debug ("  found endpoint %x for interrupt-in\n", i);
459                         /* 20 buffers of 8 bytes, for every 10 msecs */
460                         HID_INST(dev)->queue = dev->controller->create_intr_queue (&dev->endpoints[i], 8, 20, 10);
461                         keycount = 0;
462                         debug ("  configuration done.\n");
463                         break;
464                 case hid_boot_proto_mouse:
465                         debug("NOTICE: USB mice are not supported.\n");
466                         break;
467                 }
468         }
469 }
470
471 int usbhid_havechar (void)
472 {
473         return (keycount != 0);
474 }
475
476 int usbhid_getchar (void)
477 {
478         short ret;
479
480         if (keycount == 0)
481                 return 0;
482         ret = keybuffer[0];
483         memmove(keybuffer, keybuffer + 1, --keycount);
484
485         return (int)ret;
486 }
487