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