Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 4 | * Copyright (C) 2008-2010 coresystems GmbH |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 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 | |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 30 | // #define USB_DEBUG |
| 31 | |
Gabe Black | a2d786f | 2013-03-12 15:44:56 -0700 | [diff] [blame^] | 32 | #include <keycodes.h> |
Jordan Crouse | 29061a5 | 2008-09-11 17:29:00 +0000 | [diff] [blame] | 33 | #include <usb/usb.h> |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 34 | |
| 35 | enum { hid_subclass_none = 0, hid_subclass_boot = 1 }; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 36 | typedef enum { hid_proto_boot = 0, hid_proto_report = 1 } hid_proto; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 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 | |
Nico Huber | cd587f1 | 2012-11-23 13:18:13 +0100 | [diff] [blame] | 45 | typedef 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 Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 54 | typedef struct { |
| 55 | void* queue; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 56 | hid_descriptor_t *descriptor; |
Nico Huber | cd587f1 | 2012-11-23 13:18:13 +0100 | [diff] [blame] | 57 | |
| 58 | usb_hid_keyboard_event_t previous; |
| 59 | int lastkeypress; |
| 60 | int repeat_delay; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 61 | } usbhid_inst_t; |
| 62 | |
| 63 | #define HID_INST(dev) ((usbhid_inst_t*)(dev)->data) |
| 64 | |
Nico Huber | 4842dfe | 2012-05-25 09:59:19 +0200 | [diff] [blame] | 65 | static void |
| 66 | usb_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 Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 86 | /* keybuffer is global to all USB keyboards */ |
| 87 | static int keycount; |
| 88 | #define KEYBOARD_BUFFER_SIZE 16 |
| 89 | static short keybuffer[KEYBOARD_BUFFER_SIZE]; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 90 | |
Patrick Georgi | 7f96583 | 2011-04-21 18:57:16 +0200 | [diff] [blame] | 91 | const char *countries[36][2] = { |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 92 | { "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 Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 115 | { "Russia", "ru" }, |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 116 | { "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 Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 132 | |
| 133 | struct layout_maps { |
Patrick Georgi | 7f96583 | 2011-04-21 18:57:16 +0200 | [diff] [blame] | 134 | const char *country; |
| 135 | const short map[4][0x80]; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
Patrick Georgi | 7f96583 | 2011-04-21 18:57:16 +0200 | [diff] [blame] | 138 | static const struct layout_maps *map; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 139 | |
Patrick Georgi | 7f96583 | 2011-04-21 18:57:16 +0200 | [diff] [blame] | 140 | static const struct layout_maps keyboard_layouts[] = { |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 141 | // #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 Georgi | 28b900a | 2012-11-27 10:19:59 +0100 | [diff] [blame] | 176 | '\n', '\e', '\b', '\t', ' ', '_', '+', '[', |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 177 | /* 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 | |
| 251 | static 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 Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 257 | #define KEYBOARD_REPEAT_MS 30 |
| 258 | #define INITIAL_REPEAT_DELAY 10 |
| 259 | #define REPEAT_DELAY 2 |
| 260 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 261 | static void |
Nico Huber | cd587f1 | 2012-11-23 13:18:13 +0100 | [diff] [blame] | 262 | usb_hid_process_keyboard_event(usbhid_inst_t *const inst, |
| 263 | const usb_hid_keyboard_event_t *const current) |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 264 | { |
Nico Huber | cd587f1 | 2012-11-23 13:18:13 +0100 | [diff] [blame] | 265 | const usb_hid_keyboard_event_t *const previous = &inst->previous; |
| 266 | |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 267 | int i, keypress = 0, modifiers = 0; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 268 | |
| 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 Huber | cd587f1 | 2012-11-23 13:18:13 +0100 | [diff] [blame] | 286 | if (inst->lastkeypress && |
| 287 | !memcmp(current, previous, sizeof(*current))) { |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 288 | /* No. Then it's a key repeat event. */ |
Nico Huber | cd587f1 | 2012-11-23 13:18:13 +0100 | [diff] [blame] | 289 | if (inst->repeat_delay) { |
| 290 | inst->repeat_delay--; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 291 | } else { |
Nico Huber | cd587f1 | 2012-11-23 13:18:13 +0100 | [diff] [blame] | 292 | usb_hid_keyboard_queue(inst->lastkeypress); |
| 293 | inst->repeat_delay = REPEAT_DELAY; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | return; |
| 297 | } |
| 298 | |
Nico Huber | cd587f1 | 2012-11-23 13:18:13 +0100 | [diff] [blame] | 299 | inst->lastkeypress = 0; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 300 | |
| 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 Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 333 | usb_debug ("usbhid: <%x> %x [ %x %x %x %x %x %x ] %d\n", |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 334 | 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 Huber | cd587f1 | 2012-11-23 13:18:13 +0100 | [diff] [blame] | 346 | inst->lastkeypress = keypress; |
| 347 | inst->repeat_delay = INITIAL_REPEAT_DELAY; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 351 | static void |
| 352 | usb_hid_poll (usbdev_t *dev) |
| 353 | { |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 354 | usb_hid_keyboard_event_t current; |
Nico Huber | cd587f1 | 2012-11-23 13:18:13 +0100 | [diff] [blame] | 355 | const u8 *buf; |
| 356 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 357 | while ((buf=dev->controller->poll_intr_queue (HID_INST(dev)->queue))) { |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 358 | memcpy(¤t.buffer, buf, 8); |
Nico Huber | cd587f1 | 2012-11-23 13:18:13 +0100 | [diff] [blame] | 359 | usb_hid_process_keyboard_event(HID_INST(dev), ¤t); |
| 360 | HID_INST(dev)->previous = current; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 364 | static void |
| 365 | usb_hid_set_idle (usbdev_t *dev, interface_descriptor_t *interface, u16 duration) |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 366 | { |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 367 | 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 | |
| 378 | static void |
| 379 | usb_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 Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Patrick Georgi | 657a6dc | 2008-10-21 15:08:18 +0000 | [diff] [blame] | 392 | static struct console_input_driver cons = { |
| 393 | .havekey = usbhid_havechar, |
| 394 | .getchar = usbhid_getchar |
| 395 | }; |
| 396 | |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 397 | |
Patrick Georgi | 7f96583 | 2011-04-21 18:57:16 +0200 | [diff] [blame] | 398 | static int usb_hid_set_layout (const char *country) |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 399 | { |
| 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 Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 410 | usb_debug(" Keyboard layout '%s'\n", map->country); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 411 | return 0; |
| 412 | } |
| 413 | |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 414 | usb_debug(" Keyboard layout '%s' not found, using '%s'\n", |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 415 | country, map->country); |
| 416 | |
| 417 | /* Nothing found, not changed */ |
| 418 | return -1; |
| 419 | } |
| 420 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 421 | void |
| 422 | usb_hid_init (usbdev_t *dev) |
| 423 | { |
| 424 | |
Patrick Georgi | 657a6dc | 2008-10-21 15:08:18 +0000 | [diff] [blame] | 425 | static int installed = 0; |
| 426 | if (!installed) { |
| 427 | installed = 1; |
| 428 | console_add_input_driver (&cons); |
| 429 | } |
| 430 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 431 | configuration_descriptor_t *cd = (configuration_descriptor_t*)dev->configuration; |
| 432 | interface_descriptor_t *interface = (interface_descriptor_t*)(((char *) cd) + cd->bLength); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 433 | |
| 434 | if (interface->bInterfaceSubClass == hid_subclass_boot) { |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 435 | u8 countrycode; |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 436 | usb_debug (" supports boot interface..\n"); |
| 437 | usb_debug (" it's a %s\n", |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 438 | boot_protos[interface->bInterfaceProtocol]); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 439 | switch (interface->bInterfaceProtocol) { |
| 440 | case hid_boot_proto_keyboard: |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 441 | dev->data = malloc (sizeof (usbhid_inst_t)); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 442 | if (!dev->data) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 443 | fatal("Not enough memory for USB HID device.\n"); |
Nico Huber | cd587f1 | 2012-11-23 13:18:13 +0100 | [diff] [blame] | 444 | memset(&HID_INST(dev)->previous, 0x00, |
| 445 | sizeof(HID_INST(dev)->previous)); |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 446 | usb_debug (" configuring...\n"); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 447 | usb_hid_set_protocol(dev, interface, hid_proto_boot); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 448 | usb_hid_set_idle(dev, interface, KEYBOARD_REPEAT_MS); |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 449 | usb_debug (" activating...\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 450 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 451 | HID_INST (dev)->descriptor = |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 452 | (hid_descriptor_t *) |
| 453 | get_descriptor(dev, gen_bmRequestType |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 454 | (device_to_host, standard_type, iface_recp), |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 455 | 0x21, 0, 0); |
| 456 | countrycode = HID_INST(dev)->descriptor->bCountryCode; |
| 457 | /* 35 countries defined: */ |
| 458 | if (countrycode > 35) |
| 459 | countrycode = 0; |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 460 | usb_debug (" Keyboard has %s layout (country code %02x)\n", |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 461 | countries[countrycode][0], countrycode); |
| 462 | |
| 463 | /* Set keyboard layout accordingly */ |
| 464 | usb_hid_set_layout(countries[countrycode][1]); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 465 | |
| 466 | // only add here, because we only support boot-keyboard HID devices |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 467 | dev->destroy = usb_hid_destroy; |
| 468 | dev->poll = usb_hid_poll; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 469 | int i; |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 470 | for (i = 0; i <= dev->num_endp; i++) { |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 471 | 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 Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 479 | usb_debug (" found endpoint %x for interrupt-in\n", i); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 480 | /* 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 Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 482 | keycount = 0; |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 483 | usb_debug (" configuration done.\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 484 | break; |
| 485 | case hid_boot_proto_mouse: |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 486 | usb_debug("NOTICE: USB mice are not supported.\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 487 | break; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | } |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 491 | |
| 492 | int usbhid_havechar (void) |
| 493 | { |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 494 | return (keycount != 0); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | int usbhid_getchar (void) |
| 498 | { |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 499 | 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 Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 507 | } |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 508 | |