wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Denis Peter, MPL AG Switzerland |
| 4 | * |
| 5 | * Part of this source has been derived from the Linux USB |
| 6 | * project. |
| 7 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 9 | */ |
| 10 | #include <common.h> |
Hans de Goede | 0ea09df | 2014-09-20 16:54:34 +0200 | [diff] [blame] | 11 | #include <errno.h> |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 12 | #include <malloc.h> |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 13 | #include <stdio_dev.h> |
Christian Eggers | c918261 | 2008-05-21 22:12:00 +0200 | [diff] [blame] | 14 | #include <asm/byteorder.h> |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 15 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 16 | #include <usb.h> |
| 17 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 18 | /* |
Marek Vasut | 00b7d6e | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 19 | * If overwrite_console returns 1, the stdin, stderr and stdout |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 20 | * are switched to the serial port, else the settings in the |
| 21 | * environment are used |
| 22 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 23 | #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE |
Marek Vasut | 00b7d6e | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 24 | extern int overwrite_console(void); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 25 | #else |
Marek Vasut | 00b7d6e | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 26 | int overwrite_console(void) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 27 | { |
Marek Vasut | 00b7d6e | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 28 | return 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 29 | } |
| 30 | #endif |
| 31 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 32 | /* Keyboard sampling rate */ |
| 33 | #define REPEAT_RATE (40 / 4) /* 40msec -> 25cps */ |
| 34 | #define REPEAT_DELAY 10 /* 10 x REPEAT_RATE = 400msec */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 35 | |
| 36 | #define NUM_LOCK 0x53 |
Marek Vasut | 00b7d6e | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 37 | #define CAPS_LOCK 0x39 |
| 38 | #define SCROLL_LOCK 0x47 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 39 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 40 | /* Modifier bits */ |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 41 | #define LEFT_CNTR (1 << 0) |
| 42 | #define LEFT_SHIFT (1 << 1) |
| 43 | #define LEFT_ALT (1 << 2) |
| 44 | #define LEFT_GUI (1 << 3) |
| 45 | #define RIGHT_CNTR (1 << 4) |
| 46 | #define RIGHT_SHIFT (1 << 5) |
| 47 | #define RIGHT_ALT (1 << 6) |
| 48 | #define RIGHT_GUI (1 << 7) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 49 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 50 | /* Size of the keyboard buffer */ |
| 51 | #define USB_KBD_BUFFER_LEN 0x20 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 52 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 53 | /* Device name */ |
Marek Vasut | 00b7d6e | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 54 | #define DEVNAME "usbkbd" |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 55 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 56 | /* Keyboard maps */ |
| 57 | static const unsigned char usb_kbd_numkey[] = { |
Marek Vasut | 00b7d6e | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 58 | '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', |
| 59 | '\r', 0x1b, '\b', '\t', ' ', '-', '=', '[', ']', |
| 60 | '\\', '#', ';', '\'', '`', ',', '.', '/' |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 61 | }; |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 62 | static const unsigned char usb_kbd_numkey_shifted[] = { |
Marek Vasut | 00b7d6e | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 63 | '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', |
| 64 | '\r', 0x1b, '\b', '\t', ' ', '_', '+', '{', '}', |
| 65 | '|', '~', ':', '"', '~', '<', '>', '?' |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
Vincent Palatin | d53da84 | 2012-01-09 12:59:36 +0000 | [diff] [blame] | 68 | static const unsigned char usb_kbd_num_keypad[] = { |
| 69 | '/', '*', '-', '+', '\r', |
| 70 | '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', |
| 71 | '.', 0, 0, 0, '=' |
| 72 | }; |
| 73 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 74 | /* |
Allen Martin | 4151a40 | 2012-11-06 13:26:03 -0800 | [diff] [blame] | 75 | * map arrow keys to ^F/^B ^N/^P, can't really use the proper |
| 76 | * ANSI sequence for arrow keys because the queuing code breaks |
| 77 | * when a single keypress expands to 3 queue elements |
| 78 | */ |
| 79 | static const unsigned char usb_kbd_arrow[] = { |
| 80 | 0x6, 0x2, 0xe, 0x10 |
| 81 | }; |
| 82 | |
| 83 | /* |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 84 | * NOTE: It's important for the NUM, CAPS, SCROLL-lock bits to be in this |
| 85 | * order. See usb_kbd_setled() function! |
| 86 | */ |
| 87 | #define USB_KBD_NUMLOCK (1 << 0) |
| 88 | #define USB_KBD_CAPSLOCK (1 << 1) |
| 89 | #define USB_KBD_SCROLLLOCK (1 << 2) |
| 90 | #define USB_KBD_CTRL (1 << 3) |
Marek Vasut | 48c8073 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 91 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 92 | #define USB_KBD_LEDMASK \ |
| 93 | (USB_KBD_NUMLOCK | USB_KBD_CAPSLOCK | USB_KBD_SCROLLLOCK) |
| 94 | |
Adrian Cox | 08a98b8 | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 95 | /* |
| 96 | * USB Keyboard reports are 8 bytes in boot protocol. |
| 97 | * Appendix B of HID Device Class Definition 1.11 |
| 98 | */ |
| 99 | #define USB_KBD_BOOT_REPORT_SIZE 8 |
| 100 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 101 | struct usb_kbd_pdata { |
| 102 | uint32_t repeat_delay; |
| 103 | |
| 104 | uint32_t usb_in_pointer; |
| 105 | uint32_t usb_out_pointer; |
| 106 | uint8_t usb_kbd_buffer[USB_KBD_BUFFER_LEN]; |
| 107 | |
Allen Martin | d747538 | 2012-10-24 08:32:04 +0000 | [diff] [blame] | 108 | uint8_t *new; |
Adrian Cox | 08a98b8 | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 109 | uint8_t old[USB_KBD_BOOT_REPORT_SIZE]; |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 110 | |
| 111 | uint8_t flags; |
| 112 | }; |
| 113 | |
Jim Lin | 07551f2 | 2013-08-13 19:04:22 +0800 | [diff] [blame] | 114 | extern int __maybe_unused net_busy_flag; |
| 115 | |
| 116 | /* The period of time between two calls of usb_kbd_testc(). */ |
| 117 | static unsigned long __maybe_unused kbd_testc_tms; |
| 118 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 119 | /* Generic keyboard event polling. */ |
| 120 | void usb_kbd_generic_poll(void) |
| 121 | { |
| 122 | struct stdio_dev *dev; |
| 123 | struct usb_device *usb_kbd_dev; |
| 124 | struct usb_kbd_pdata *data; |
| 125 | struct usb_interface *iface; |
| 126 | struct usb_endpoint_descriptor *ep; |
| 127 | int pipe; |
| 128 | int maxp; |
| 129 | |
| 130 | /* Get the pointer to USB Keyboard device pointer */ |
| 131 | dev = stdio_get_by_name(DEVNAME); |
| 132 | usb_kbd_dev = (struct usb_device *)dev->priv; |
| 133 | data = usb_kbd_dev->privptr; |
| 134 | iface = &usb_kbd_dev->config.if_desc[0]; |
| 135 | ep = &iface->ep_desc[0]; |
| 136 | pipe = usb_rcvintpipe(usb_kbd_dev, ep->bEndpointAddress); |
| 137 | |
| 138 | /* Submit a interrupt transfer request */ |
| 139 | maxp = usb_maxpacket(usb_kbd_dev, pipe); |
| 140 | usb_submit_int_msg(usb_kbd_dev, pipe, data->new, |
Adrian Cox | 08a98b8 | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 141 | min(maxp, USB_KBD_BOOT_REPORT_SIZE), |
| 142 | ep->bInterval); |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* Puts character in the queue and sets up the in and out pointer. */ |
| 146 | static void usb_kbd_put_queue(struct usb_kbd_pdata *data, char c) |
| 147 | { |
| 148 | if (data->usb_in_pointer == USB_KBD_BUFFER_LEN - 1) { |
| 149 | /* Check for buffer full. */ |
| 150 | if (data->usb_out_pointer == 0) |
| 151 | return; |
| 152 | |
| 153 | data->usb_in_pointer = 0; |
| 154 | } else { |
| 155 | /* Check for buffer full. */ |
| 156 | if (data->usb_in_pointer == data->usb_out_pointer - 1) |
| 157 | return; |
| 158 | |
| 159 | data->usb_in_pointer++; |
| 160 | } |
| 161 | |
| 162 | data->usb_kbd_buffer[data->usb_in_pointer] = c; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Set the LEDs. Since this is used in the irq routine, the control job is |
| 167 | * issued with a timeout of 0. This means, that the job is queued without |
| 168 | * waiting for job completion. |
| 169 | */ |
| 170 | static void usb_kbd_setled(struct usb_device *dev) |
| 171 | { |
| 172 | struct usb_interface *iface = &dev->config.if_desc[0]; |
| 173 | struct usb_kbd_pdata *data = dev->privptr; |
Hans de Goede | 9b23938 | 2014-09-20 16:51:26 +0200 | [diff] [blame] | 174 | ALLOC_ALIGN_BUFFER(uint32_t, leds, 1, USB_DMA_MINALIGN); |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 175 | |
Hans de Goede | 9b23938 | 2014-09-20 16:51:26 +0200 | [diff] [blame] | 176 | *leds = data->flags & USB_KBD_LEDMASK; |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 177 | usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 178 | USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
Hans de Goede | 9b23938 | 2014-09-20 16:51:26 +0200 | [diff] [blame] | 179 | 0x200, iface->desc.bInterfaceNumber, leds, 1, 0); |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | #define CAPITAL_MASK 0x20 |
| 183 | /* Translate the scancode in ASCII */ |
| 184 | static int usb_kbd_translate(struct usb_kbd_pdata *data, unsigned char scancode, |
| 185 | unsigned char modifier, int pressed) |
| 186 | { |
| 187 | uint8_t keycode = 0; |
| 188 | |
| 189 | /* Key released */ |
| 190 | if (pressed == 0) { |
| 191 | data->repeat_delay = 0; |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | if (pressed == 2) { |
| 196 | data->repeat_delay++; |
| 197 | if (data->repeat_delay < REPEAT_DELAY) |
| 198 | return 0; |
| 199 | |
| 200 | data->repeat_delay = REPEAT_DELAY; |
| 201 | } |
| 202 | |
| 203 | /* Alphanumeric values */ |
| 204 | if ((scancode > 3) && (scancode <= 0x1d)) { |
| 205 | keycode = scancode - 4 + 'a'; |
| 206 | |
| 207 | if (data->flags & USB_KBD_CAPSLOCK) |
| 208 | keycode &= ~CAPITAL_MASK; |
| 209 | |
| 210 | if (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) { |
| 211 | /* Handle CAPSLock + Shift pressed simultaneously */ |
| 212 | if (keycode & CAPITAL_MASK) |
| 213 | keycode &= ~CAPITAL_MASK; |
| 214 | else |
| 215 | keycode |= CAPITAL_MASK; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if ((scancode > 0x1d) && (scancode < 0x3a)) { |
| 220 | /* Shift pressed */ |
| 221 | if (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) |
| 222 | keycode = usb_kbd_numkey_shifted[scancode - 0x1e]; |
| 223 | else |
| 224 | keycode = usb_kbd_numkey[scancode - 0x1e]; |
| 225 | } |
| 226 | |
Allen Martin | 4151a40 | 2012-11-06 13:26:03 -0800 | [diff] [blame] | 227 | /* Arrow keys */ |
| 228 | if ((scancode >= 0x4f) && (scancode <= 0x52)) |
| 229 | keycode = usb_kbd_arrow[scancode - 0x4f]; |
| 230 | |
Vincent Palatin | d53da84 | 2012-01-09 12:59:36 +0000 | [diff] [blame] | 231 | /* Numeric keypad */ |
| 232 | if ((scancode >= 0x54) && (scancode <= 0x67)) |
| 233 | keycode = usb_kbd_num_keypad[scancode - 0x54]; |
| 234 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 235 | if (data->flags & USB_KBD_CTRL) |
| 236 | keycode = scancode - 0x3; |
| 237 | |
| 238 | if (pressed == 1) { |
| 239 | if (scancode == NUM_LOCK) { |
| 240 | data->flags ^= USB_KBD_NUMLOCK; |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | if (scancode == CAPS_LOCK) { |
| 245 | data->flags ^= USB_KBD_CAPSLOCK; |
| 246 | return 1; |
| 247 | } |
| 248 | if (scancode == SCROLL_LOCK) { |
| 249 | data->flags ^= USB_KBD_SCROLLLOCK; |
| 250 | return 1; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* Report keycode if any */ |
| 255 | if (keycode) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 256 | debug("%c", keycode); |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 257 | usb_kbd_put_queue(data, keycode); |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static uint32_t usb_kbd_service_key(struct usb_device *dev, int i, int up) |
| 264 | { |
| 265 | uint32_t res = 0; |
| 266 | struct usb_kbd_pdata *data = dev->privptr; |
| 267 | uint8_t *new; |
| 268 | uint8_t *old; |
| 269 | |
| 270 | if (up) { |
| 271 | new = data->old; |
| 272 | old = data->new; |
| 273 | } else { |
| 274 | new = data->new; |
| 275 | old = data->old; |
| 276 | } |
| 277 | |
Adrian Cox | 08a98b8 | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 278 | if ((old[i] > 3) && |
| 279 | (memscan(new + 2, old[i], USB_KBD_BOOT_REPORT_SIZE - 2) == |
| 280 | new + USB_KBD_BOOT_REPORT_SIZE)) { |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 281 | res |= usb_kbd_translate(data, old[i], data->new[0], up); |
Adrian Cox | 08a98b8 | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 282 | } |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 283 | |
| 284 | return res; |
| 285 | } |
| 286 | |
| 287 | /* Interrupt service routine */ |
| 288 | static int usb_kbd_irq_worker(struct usb_device *dev) |
| 289 | { |
| 290 | struct usb_kbd_pdata *data = dev->privptr; |
| 291 | int i, res = 0; |
| 292 | |
| 293 | /* No combo key pressed */ |
| 294 | if (data->new[0] == 0x00) |
| 295 | data->flags &= ~USB_KBD_CTRL; |
| 296 | /* Left or Right Ctrl pressed */ |
| 297 | else if ((data->new[0] == LEFT_CNTR) || (data->new[0] == RIGHT_CNTR)) |
| 298 | data->flags |= USB_KBD_CTRL; |
| 299 | |
Adrian Cox | 08a98b8 | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 300 | for (i = 2; i < USB_KBD_BOOT_REPORT_SIZE; i++) { |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 301 | res |= usb_kbd_service_key(dev, i, 0); |
| 302 | res |= usb_kbd_service_key(dev, i, 1); |
| 303 | } |
| 304 | |
| 305 | /* Key is still pressed */ |
| 306 | if ((data->new[2] > 3) && (data->old[2] == data->new[2])) |
| 307 | res |= usb_kbd_translate(data, data->new[2], data->new[0], 2); |
| 308 | |
| 309 | if (res == 1) |
| 310 | usb_kbd_setled(dev); |
| 311 | |
Adrian Cox | 08a98b8 | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 312 | memcpy(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE); |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 313 | |
| 314 | return 1; |
| 315 | } |
| 316 | |
| 317 | /* Keyboard interrupt handler */ |
| 318 | static int usb_kbd_irq(struct usb_device *dev) |
| 319 | { |
Adrian Cox | 08a98b8 | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 320 | if ((dev->irq_status != 0) || |
| 321 | (dev->irq_act_len != USB_KBD_BOOT_REPORT_SIZE)) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 322 | debug("USB KBD: Error %lX, len %d\n", |
| 323 | dev->irq_status, dev->irq_act_len); |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 324 | return 1; |
| 325 | } |
| 326 | |
| 327 | return usb_kbd_irq_worker(dev); |
| 328 | } |
| 329 | |
| 330 | /* Interrupt polling */ |
Marek Vasut | 48c8073 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 331 | static inline void usb_kbd_poll_for_event(struct usb_device *dev) |
| 332 | { |
| 333 | #if defined(CONFIG_SYS_USB_EVENT_POLL) |
amartin@nvidia.com | f9636e8 | 2011-12-20 14:56:16 +0000 | [diff] [blame] | 334 | struct usb_interface *iface; |
| 335 | struct usb_endpoint_descriptor *ep; |
| 336 | struct usb_kbd_pdata *data; |
| 337 | int pipe; |
| 338 | int maxp; |
| 339 | |
| 340 | /* Get the pointer to USB Keyboard device pointer */ |
| 341 | data = dev->privptr; |
| 342 | iface = &dev->config.if_desc[0]; |
| 343 | ep = &iface->ep_desc[0]; |
| 344 | pipe = usb_rcvintpipe(dev, ep->bEndpointAddress); |
| 345 | |
| 346 | /* Submit a interrupt transfer request */ |
| 347 | maxp = usb_maxpacket(dev, pipe); |
| 348 | usb_submit_int_msg(dev, pipe, &data->new[0], |
Adrian Cox | 08a98b8 | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 349 | min(maxp, USB_KBD_BOOT_REPORT_SIZE), |
| 350 | ep->bInterval); |
amartin@nvidia.com | f9636e8 | 2011-12-20 14:56:16 +0000 | [diff] [blame] | 351 | |
Marek Vasut | 48c8073 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 352 | usb_kbd_irq_worker(dev); |
| 353 | #elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP) |
| 354 | struct usb_interface *iface; |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 355 | struct usb_kbd_pdata *data = dev->privptr; |
Marek Vasut | 48c8073 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 356 | iface = &dev->config.if_desc[0]; |
| 357 | usb_get_report(dev, iface->desc.bInterfaceNumber, |
Adrian Cox | 08a98b8 | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 358 | 1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE); |
| 359 | if (memcmp(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE)) |
Marek Vasut | 48c8073 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 360 | usb_kbd_irq_worker(dev); |
| 361 | #endif |
| 362 | } |
| 363 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 364 | /* test if a character is in the queue */ |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 365 | static int usb_kbd_testc(struct stdio_dev *sdev) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 366 | { |
Marek Vasut | 48c8073 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 367 | struct stdio_dev *dev; |
| 368 | struct usb_device *usb_kbd_dev; |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 369 | struct usb_kbd_pdata *data; |
Marek Vasut | 48c8073 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 370 | |
Jim Lin | c95e2b9 | 2013-08-26 20:21:09 +0800 | [diff] [blame] | 371 | #ifdef CONFIG_CMD_NET |
| 372 | /* |
| 373 | * If net_busy_flag is 1, NET transfer is running, |
| 374 | * then we check key-pressed every second (first check may be |
| 375 | * less than 1 second) to improve TFTP booting performance. |
| 376 | */ |
| 377 | if (net_busy_flag && (get_timer(kbd_testc_tms) < CONFIG_SYS_HZ)) |
| 378 | return 0; |
| 379 | kbd_testc_tms = get_timer(0); |
| 380 | #endif |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 381 | dev = stdio_get_by_name(DEVNAME); |
Marek Vasut | 48c8073 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 382 | usb_kbd_dev = (struct usb_device *)dev->priv; |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 383 | data = usb_kbd_dev->privptr; |
Marek Vasut | 48c8073 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 384 | |
| 385 | usb_kbd_poll_for_event(usb_kbd_dev); |
| 386 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 387 | return !(data->usb_in_pointer == data->usb_out_pointer); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 388 | } |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 389 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 390 | /* gets the character from the queue */ |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 391 | static int usb_kbd_getc(struct stdio_dev *sdev) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 392 | { |
Marek Vasut | 48c8073 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 393 | struct stdio_dev *dev; |
| 394 | struct usb_device *usb_kbd_dev; |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 395 | struct usb_kbd_pdata *data; |
Marek Vasut | 48c8073 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 396 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 397 | dev = stdio_get_by_name(DEVNAME); |
Marek Vasut | 48c8073 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 398 | usb_kbd_dev = (struct usb_device *)dev->priv; |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 399 | data = usb_kbd_dev->privptr; |
Marek Vasut | 48c8073 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 400 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 401 | while (data->usb_in_pointer == data->usb_out_pointer) |
Marek Vasut | 48c8073 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 402 | usb_kbd_poll_for_event(usb_kbd_dev); |
| 403 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 404 | if (data->usb_out_pointer == USB_KBD_BUFFER_LEN - 1) |
| 405 | data->usb_out_pointer = 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 406 | else |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 407 | data->usb_out_pointer++; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 408 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 409 | return data->usb_kbd_buffer[data->usb_out_pointer]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 412 | /* probes the USB device dev for keyboard type. */ |
| 413 | static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum) |
| 414 | { |
| 415 | struct usb_interface *iface; |
| 416 | struct usb_endpoint_descriptor *ep; |
| 417 | struct usb_kbd_pdata *data; |
| 418 | int pipe, maxp; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 419 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 420 | if (dev->descriptor.bNumConfigurations != 1) |
| 421 | return 0; |
| 422 | |
| 423 | iface = &dev->config.if_desc[ifnum]; |
| 424 | |
| 425 | if (iface->desc.bInterfaceClass != 3) |
| 426 | return 0; |
| 427 | |
| 428 | if (iface->desc.bInterfaceSubClass != 1) |
| 429 | return 0; |
| 430 | |
| 431 | if (iface->desc.bInterfaceProtocol != 1) |
| 432 | return 0; |
| 433 | |
| 434 | if (iface->desc.bNumEndpoints != 1) |
| 435 | return 0; |
| 436 | |
| 437 | ep = &iface->ep_desc[0]; |
| 438 | |
| 439 | /* Check if endpoint 1 is interrupt endpoint */ |
| 440 | if (!(ep->bEndpointAddress & 0x80)) |
| 441 | return 0; |
| 442 | |
| 443 | if ((ep->bmAttributes & 3) != 3) |
| 444 | return 0; |
| 445 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 446 | debug("USB KBD: found set protocol...\n"); |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 447 | |
| 448 | data = malloc(sizeof(struct usb_kbd_pdata)); |
| 449 | if (!data) { |
| 450 | printf("USB KBD: Error allocating private data\n"); |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | /* Clear private data */ |
| 455 | memset(data, 0, sizeof(struct usb_kbd_pdata)); |
| 456 | |
Allen Martin | d747538 | 2012-10-24 08:32:04 +0000 | [diff] [blame] | 457 | /* allocate input buffer aligned and sized to USB DMA alignment */ |
Adrian Cox | 08a98b8 | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 458 | data->new = memalign(USB_DMA_MINALIGN, |
| 459 | roundup(USB_KBD_BOOT_REPORT_SIZE, USB_DMA_MINALIGN)); |
Allen Martin | d747538 | 2012-10-24 08:32:04 +0000 | [diff] [blame] | 460 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 461 | /* Insert private data into USB device structure */ |
| 462 | dev->privptr = data; |
| 463 | |
| 464 | /* Set IRQ handler */ |
| 465 | dev->irq_handle = usb_kbd_irq; |
| 466 | |
| 467 | pipe = usb_rcvintpipe(dev, ep->bEndpointAddress); |
| 468 | maxp = usb_maxpacket(dev, pipe); |
| 469 | |
| 470 | /* We found a USB Keyboard, install it. */ |
| 471 | usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0); |
| 472 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 473 | debug("USB KBD: found set idle...\n"); |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 474 | usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE, 0); |
| 475 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 476 | debug("USB KBD: enable interrupt pipe...\n"); |
Adrian Cox | 08a98b8 | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 477 | if (usb_submit_int_msg(dev, pipe, data->new, |
| 478 | min(maxp, USB_KBD_BOOT_REPORT_SIZE), |
Vincent Palatin | 5da2dc9 | 2013-05-10 19:48:59 -0700 | [diff] [blame] | 479 | ep->bInterval) < 0) { |
| 480 | printf("Failed to get keyboard state from device %04x:%04x\n", |
| 481 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
| 482 | /* Abort, we don't want to use that non-functional keyboard. */ |
| 483 | return 0; |
| 484 | } |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 485 | |
| 486 | /* Success. */ |
| 487 | return 1; |
| 488 | } |
| 489 | |
| 490 | /* Search for keyboard and register it if found. */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 491 | int drv_usb_kbd_init(void) |
| 492 | { |
Hans de Goede | 3f78a28 | 2014-09-20 16:54:36 +0200 | [diff] [blame] | 493 | struct stdio_dev usb_kbd_dev; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 494 | struct usb_device *dev; |
Marek Vasut | 00b7d6e | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 495 | char *stdinname = getenv("stdin"); |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 496 | int error, i; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 497 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 498 | /* Scan all USB Devices */ |
| 499 | for (i = 0; i < USB_MAX_DEVICE; i++) { |
| 500 | /* Get USB device. */ |
| 501 | dev = usb_get_dev_index(i); |
| 502 | if (!dev) |
Ryan CHEN | 3b20fd8 | 2008-08-20 13:00:17 -0400 | [diff] [blame] | 503 | return -1; |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 504 | |
| 505 | if (dev->devnum == -1) |
| 506 | continue; |
| 507 | |
| 508 | /* Try probing the keyboard */ |
| 509 | if (usb_kbd_probe(dev, 0) != 1) |
| 510 | continue; |
| 511 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 512 | /* Register the keyboard */ |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 513 | debug("USB KBD: register.\n"); |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 514 | memset(&usb_kbd_dev, 0, sizeof(struct stdio_dev)); |
| 515 | strcpy(usb_kbd_dev.name, DEVNAME); |
| 516 | usb_kbd_dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 517 | usb_kbd_dev.getc = usb_kbd_getc; |
| 518 | usb_kbd_dev.tstc = usb_kbd_testc; |
| 519 | usb_kbd_dev.priv = (void *)dev; |
| 520 | error = stdio_register(&usb_kbd_dev); |
| 521 | if (error) |
| 522 | return error; |
| 523 | |
amartin@nvidia.com | fb3ef64 | 2011-12-23 10:29:48 +0000 | [diff] [blame] | 524 | #ifdef CONFIG_CONSOLE_MUX |
| 525 | error = iomux_doenv(stdin, stdinname); |
| 526 | if (error) |
| 527 | return error; |
| 528 | #else |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 529 | /* Check if this is the standard input device. */ |
| 530 | if (strcmp(stdinname, DEVNAME)) |
| 531 | return 1; |
| 532 | |
| 533 | /* Reassign the console */ |
| 534 | if (overwrite_console()) |
| 535 | return 1; |
| 536 | |
| 537 | error = console_assign(stdin, DEVNAME); |
| 538 | if (error) |
| 539 | return error; |
amartin@nvidia.com | fb3ef64 | 2011-12-23 10:29:48 +0000 | [diff] [blame] | 540 | #endif |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 541 | |
| 542 | return 1; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 543 | } |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 544 | |
| 545 | /* No USB Keyboard found */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 546 | return -1; |
| 547 | } |
| 548 | |
Marek Vasut | 9a8c72a | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 549 | /* Deregister the keyboard. */ |
Hans de Goede | 8a8a225 | 2014-09-20 16:54:38 +0200 | [diff] [blame] | 550 | int usb_kbd_deregister(int force) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 551 | { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 552 | #ifdef CONFIG_SYS_STDIO_DEREGISTER |
Hans de Goede | 8a8a225 | 2014-09-20 16:54:38 +0200 | [diff] [blame] | 553 | int ret = stdio_deregister(DEVNAME, force); |
Hans de Goede | 0ea09df | 2014-09-20 16:54:34 +0200 | [diff] [blame] | 554 | if (ret && ret != -ENODEV) |
| 555 | return ret; |
| 556 | |
| 557 | return 0; |
Jean-Christophe PLAGNIOL-VILLARD | fea91ed | 2008-12-02 21:58:04 +0100 | [diff] [blame] | 558 | #else |
| 559 | return 1; |
| 560 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 561 | } |