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 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 94 | struct usb_kbd_pdata { |
| 95 | uint32_t repeat_delay; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 96 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 97 | uint32_t usb_in_pointer; |
| 98 | uint32_t usb_out_pointer; |
| 99 | uint8_t usb_kbd_buffer[USB_KBD_BUFFER_LEN]; |
Marek Vasut | 82cf9a4 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 100 | |
Allen Martin | ef769ca | 2012-10-24 08:32:04 +0000 | [diff] [blame] | 101 | uint8_t *new; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 102 | uint8_t old[8]; |
Marek Vasut | 82cf9a4 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 103 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 104 | uint8_t flags; |
| 105 | }; |
Marek Vasut | 82cf9a4 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 106 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 107 | /* Generic keyboard event polling. */ |
| 108 | void usb_kbd_generic_poll(void) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 109 | { |
Marek Vasut | 82cf9a4 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 110 | struct stdio_dev *dev; |
| 111 | struct usb_device *usb_kbd_dev; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 112 | struct usb_kbd_pdata *data; |
| 113 | struct usb_interface *iface; |
| 114 | struct usb_endpoint_descriptor *ep; |
| 115 | int pipe; |
| 116 | int maxp; |
Marek Vasut | 82cf9a4 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 117 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 118 | /* Get the pointer to USB Keyboard device pointer */ |
| 119 | dev = stdio_get_by_name(DEVNAME); |
Marek Vasut | 82cf9a4 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 120 | usb_kbd_dev = (struct usb_device *)dev->priv; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 121 | data = usb_kbd_dev->privptr; |
| 122 | iface = &usb_kbd_dev->config.if_desc[0]; |
| 123 | ep = &iface->ep_desc[0]; |
| 124 | pipe = usb_rcvintpipe(usb_kbd_dev, ep->bEndpointAddress); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 125 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 126 | /* Submit a interrupt transfer request */ |
| 127 | maxp = usb_maxpacket(usb_kbd_dev, pipe); |
| 128 | usb_submit_int_msg(usb_kbd_dev, pipe, data->new, |
| 129 | maxp > 8 ? 8 : maxp, ep->bInterval); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 132 | /* Puts character in the queue and sets up the in and out pointer. */ |
| 133 | static void usb_kbd_put_queue(struct usb_kbd_pdata *data, char c) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 134 | { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 135 | if (data->usb_in_pointer == USB_KBD_BUFFER_LEN - 1) { |
| 136 | /* Check for buffer full. */ |
| 137 | if (data->usb_out_pointer == 0) |
| 138 | return; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 139 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 140 | data->usb_in_pointer = 0; |
| 141 | } else { |
| 142 | /* Check for buffer full. */ |
| 143 | if (data->usb_in_pointer == data->usb_out_pointer - 1) |
| 144 | return; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 145 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 146 | data->usb_in_pointer++; |
| 147 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 148 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 149 | data->usb_kbd_buffer[data->usb_in_pointer] = c; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 152 | /* |
| 153 | * Set the LEDs. Since this is used in the irq routine, the control job is |
| 154 | * issued with a timeout of 0. This means, that the job is queued without |
| 155 | * waiting for job completion. |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 156 | */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 157 | static void usb_kbd_setled(struct usb_device *dev) |
| 158 | { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 159 | struct usb_interface *iface = &dev->config.if_desc[0]; |
| 160 | struct usb_kbd_pdata *data = dev->privptr; |
| 161 | uint32_t leds = data->flags & USB_KBD_LEDMASK; |
| 162 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 163 | usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 164 | USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 165 | 0x200, iface->desc.bInterfaceNumber, (void *)&leds, 1, 0); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 168 | #define CAPITAL_MASK 0x20 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 169 | /* Translate the scancode in ASCII */ |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 170 | static int usb_kbd_translate(struct usb_kbd_pdata *data, unsigned char scancode, |
| 171 | unsigned char modifier, int pressed) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 172 | { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 173 | uint8_t keycode = 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 174 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 175 | /* Key released */ |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 176 | if (pressed == 0) { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 177 | data->repeat_delay = 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 178 | return 0; |
| 179 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 180 | |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 181 | if (pressed == 2) { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 182 | data->repeat_delay++; |
| 183 | if (data->repeat_delay < REPEAT_DELAY) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 184 | return 0; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 185 | |
| 186 | data->repeat_delay = REPEAT_DELAY; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 187 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 188 | |
| 189 | /* Alphanumeric values */ |
| 190 | if ((scancode > 3) && (scancode <= 0x1d)) { |
| 191 | keycode = scancode - 4 + 'a'; |
| 192 | |
| 193 | if (data->flags & USB_KBD_CAPSLOCK) |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 194 | keycode &= ~CAPITAL_MASK; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 195 | |
| 196 | if (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) { |
| 197 | /* Handle CAPSLock + Shift pressed simultaneously */ |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 198 | if (keycode & CAPITAL_MASK) |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 199 | keycode &= ~CAPITAL_MASK; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 200 | else |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 201 | keycode |= CAPITAL_MASK; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 204 | |
| 205 | if ((scancode > 0x1d) && (scancode < 0x3a)) { |
| 206 | /* Shift pressed */ |
| 207 | if (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) |
| 208 | keycode = usb_kbd_numkey_shifted[scancode - 0x1e]; |
| 209 | else |
| 210 | keycode = usb_kbd_numkey[scancode - 0x1e]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 211 | } |
Zhang Wei | 37cc4f0 | 2008-01-03 10:51:15 +0800 | [diff] [blame] | 212 | |
Allen Martin | 1bfb4cd | 2012-11-06 13:26:03 -0800 | [diff] [blame] | 213 | /* Arrow keys */ |
| 214 | if ((scancode >= 0x4f) && (scancode <= 0x52)) |
| 215 | keycode = usb_kbd_arrow[scancode - 0x4f]; |
| 216 | |
Vincent Palatin | 92f6a62 | 2012-01-09 12:59:36 +0000 | [diff] [blame] | 217 | /* Numeric keypad */ |
| 218 | if ((scancode >= 0x54) && (scancode <= 0x67)) |
| 219 | keycode = usb_kbd_num_keypad[scancode - 0x54]; |
| 220 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 221 | if (data->flags & USB_KBD_CTRL) |
Zhang Wei | 37cc4f0 | 2008-01-03 10:51:15 +0800 | [diff] [blame] | 222 | keycode = scancode - 0x3; |
| 223 | |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 224 | if (pressed == 1) { |
| 225 | if (scancode == NUM_LOCK) { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 226 | data->flags ^= USB_KBD_NUMLOCK; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 227 | return 1; |
| 228 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 229 | |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 230 | if (scancode == CAPS_LOCK) { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 231 | data->flags ^= USB_KBD_CAPSLOCK; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 232 | return 1; |
| 233 | } |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 234 | if (scancode == SCROLL_LOCK) { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 235 | data->flags ^= USB_KBD_SCROLLLOCK; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 236 | return 1; |
| 237 | } |
| 238 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 239 | |
| 240 | /* Report keycode if any */ |
| 241 | if (keycode) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 242 | debug("%c", keycode); |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 243 | usb_kbd_put_queue(data, keycode); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 244 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 245 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 249 | static uint32_t usb_kbd_service_key(struct usb_device *dev, int i, int up) |
| 250 | { |
| 251 | uint32_t res = 0; |
| 252 | struct usb_kbd_pdata *data = dev->privptr; |
| 253 | uint8_t *new; |
| 254 | uint8_t *old; |
| 255 | |
| 256 | if (up) { |
| 257 | new = data->old; |
| 258 | old = data->new; |
| 259 | } else { |
| 260 | new = data->new; |
| 261 | old = data->old; |
| 262 | } |
| 263 | |
| 264 | if ((old[i] > 3) && (memscan(new + 2, old[i], 6) == new + 8)) |
| 265 | res |= usb_kbd_translate(data, old[i], data->new[0], up); |
| 266 | |
| 267 | return res; |
| 268 | } |
| 269 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 270 | /* Interrupt service routine */ |
Marek Vasut | 82cf9a4 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 271 | static int usb_kbd_irq_worker(struct usb_device *dev) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 272 | { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 273 | struct usb_kbd_pdata *data = dev->privptr; |
| 274 | int i, res = 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 275 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 276 | /* No combo key pressed */ |
| 277 | if (data->new[0] == 0x00) |
| 278 | data->flags &= ~USB_KBD_CTRL; |
| 279 | /* Left or Right Ctrl pressed */ |
| 280 | else if ((data->new[0] == LEFT_CNTR) || (data->new[0] == RIGHT_CNTR)) |
| 281 | data->flags |= USB_KBD_CTRL; |
Zhang Wei | 37cc4f0 | 2008-01-03 10:51:15 +0800 | [diff] [blame] | 282 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 283 | for (i = 2; i < 8; i++) { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 284 | res |= usb_kbd_service_key(dev, i, 0); |
| 285 | res |= usb_kbd_service_key(dev, i, 1); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 286 | } |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 287 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 288 | /* Key is still pressed */ |
| 289 | if ((data->new[2] > 3) && (data->old[2] == data->new[2])) |
| 290 | res |= usb_kbd_translate(data, data->new[2], data->new[0], 2); |
| 291 | |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 292 | if (res == 1) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 293 | usb_kbd_setled(dev); |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 294 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 295 | memcpy(data->old, data->new, 8); |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 296 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 297 | return 1; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 300 | /* Keyboard interrupt handler */ |
Marek Vasut | 82cf9a4 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 301 | static int usb_kbd_irq(struct usb_device *dev) |
| 302 | { |
| 303 | if ((dev->irq_status != 0) || (dev->irq_act_len != 8)) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 304 | debug("USB KBD: Error %lX, len %d\n", |
| 305 | dev->irq_status, dev->irq_act_len); |
Marek Vasut | 82cf9a4 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 306 | return 1; |
| 307 | } |
| 308 | |
| 309 | return usb_kbd_irq_worker(dev); |
| 310 | } |
| 311 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 312 | /* Interrupt polling */ |
| 313 | static inline void usb_kbd_poll_for_event(struct usb_device *dev) |
| 314 | { |
| 315 | #if defined(CONFIG_SYS_USB_EVENT_POLL) |
amartin@nvidia.com | 185da95 | 2011-12-20 14:56:16 +0000 | [diff] [blame] | 316 | struct usb_interface *iface; |
| 317 | struct usb_endpoint_descriptor *ep; |
| 318 | struct usb_kbd_pdata *data; |
| 319 | int pipe; |
| 320 | int maxp; |
| 321 | |
| 322 | /* Get the pointer to USB Keyboard device pointer */ |
| 323 | data = dev->privptr; |
| 324 | iface = &dev->config.if_desc[0]; |
| 325 | ep = &iface->ep_desc[0]; |
| 326 | pipe = usb_rcvintpipe(dev, ep->bEndpointAddress); |
| 327 | |
| 328 | /* Submit a interrupt transfer request */ |
| 329 | maxp = usb_maxpacket(dev, pipe); |
| 330 | usb_submit_int_msg(dev, pipe, &data->new[0], |
| 331 | maxp > 8 ? 8 : maxp, ep->bInterval); |
| 332 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 333 | usb_kbd_irq_worker(dev); |
| 334 | #elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP) |
| 335 | struct usb_interface *iface; |
| 336 | struct usb_kbd_pdata *data = dev->privptr; |
| 337 | iface = &dev->config.if_desc[0]; |
| 338 | usb_get_report(dev, iface->desc.bInterfaceNumber, |
Vincent Palatin | f3f3a5d | 2012-01-09 08:35:09 +0000 | [diff] [blame] | 339 | 1, 0, data->new, sizeof(data->new)); |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 340 | if (memcmp(data->old, data->new, sizeof(data->new))) |
| 341 | usb_kbd_irq_worker(dev); |
| 342 | #endif |
| 343 | } |
| 344 | |
| 345 | /* test if a character is in the queue */ |
| 346 | static int usb_kbd_testc(void) |
| 347 | { |
| 348 | struct stdio_dev *dev; |
| 349 | struct usb_device *usb_kbd_dev; |
| 350 | struct usb_kbd_pdata *data; |
| 351 | |
| 352 | dev = stdio_get_by_name(DEVNAME); |
| 353 | usb_kbd_dev = (struct usb_device *)dev->priv; |
| 354 | data = usb_kbd_dev->privptr; |
| 355 | |
| 356 | usb_kbd_poll_for_event(usb_kbd_dev); |
| 357 | |
| 358 | return !(data->usb_in_pointer == data->usb_out_pointer); |
| 359 | } |
| 360 | |
| 361 | /* gets the character from the queue */ |
| 362 | static int usb_kbd_getc(void) |
| 363 | { |
| 364 | struct stdio_dev *dev; |
| 365 | struct usb_device *usb_kbd_dev; |
| 366 | struct usb_kbd_pdata *data; |
| 367 | |
| 368 | dev = stdio_get_by_name(DEVNAME); |
| 369 | usb_kbd_dev = (struct usb_device *)dev->priv; |
| 370 | data = usb_kbd_dev->privptr; |
| 371 | |
| 372 | while (data->usb_in_pointer == data->usb_out_pointer) |
| 373 | usb_kbd_poll_for_event(usb_kbd_dev); |
| 374 | |
| 375 | if (data->usb_out_pointer == USB_KBD_BUFFER_LEN - 1) |
| 376 | data->usb_out_pointer = 0; |
| 377 | else |
| 378 | data->usb_out_pointer++; |
| 379 | |
| 380 | return data->usb_kbd_buffer[data->usb_out_pointer]; |
| 381 | } |
| 382 | |
| 383 | /* probes the USB device dev for keyboard type. */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 384 | static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum) |
| 385 | { |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 386 | struct usb_interface *iface; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 387 | struct usb_endpoint_descriptor *ep; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 388 | struct usb_kbd_pdata *data; |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 389 | int pipe, maxp; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 390 | |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 391 | if (dev->descriptor.bNumConfigurations != 1) |
| 392 | return 0; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 393 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 394 | iface = &dev->config.if_desc[ifnum]; |
| 395 | |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 396 | if (iface->desc.bInterfaceClass != 3) |
| 397 | return 0; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 398 | |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 399 | if (iface->desc.bInterfaceSubClass != 1) |
| 400 | return 0; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 401 | |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 402 | if (iface->desc.bInterfaceProtocol != 1) |
| 403 | return 0; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 404 | |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 405 | if (iface->desc.bNumEndpoints != 1) |
| 406 | return 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 407 | |
| 408 | ep = &iface->ep_desc[0]; |
| 409 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 410 | /* Check if endpoint 1 is interrupt endpoint */ |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 411 | if (!(ep->bEndpointAddress & 0x80)) |
| 412 | return 0; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 413 | |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 414 | if ((ep->bmAttributes & 3) != 3) |
| 415 | return 0; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 416 | |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 417 | debug("USB KBD: found set protocol...\n"); |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 418 | |
| 419 | data = malloc(sizeof(struct usb_kbd_pdata)); |
| 420 | if (!data) { |
| 421 | printf("USB KBD: Error allocating private data\n"); |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | /* Clear private data */ |
| 426 | memset(data, 0, sizeof(struct usb_kbd_pdata)); |
| 427 | |
Allen Martin | ef769ca | 2012-10-24 08:32:04 +0000 | [diff] [blame] | 428 | /* allocate input buffer aligned and sized to USB DMA alignment */ |
| 429 | data->new = memalign(USB_DMA_MINALIGN, roundup(8, USB_DMA_MINALIGN)); |
| 430 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 431 | /* Insert private data into USB device structure */ |
| 432 | dev->privptr = data; |
| 433 | |
| 434 | /* Set IRQ handler */ |
| 435 | dev->irq_handle = usb_kbd_irq; |
| 436 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 437 | pipe = usb_rcvintpipe(dev, ep->bEndpointAddress); |
| 438 | maxp = usb_maxpacket(dev, pipe); |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 439 | |
| 440 | /* We found a USB Keyboard, install it. */ |
| 441 | usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0); |
| 442 | |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 443 | debug("USB KBD: found set idle...\n"); |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 444 | usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE, 0); |
| 445 | |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 446 | debug("USB KBD: enable interrupt pipe...\n"); |
Vincent Palatin | 0eab998 | 2013-05-10 19:48:59 -0700 | [diff] [blame] | 447 | if (usb_submit_int_msg(dev, pipe, data->new, maxp > 8 ? 8 : maxp, |
| 448 | ep->bInterval) < 0) { |
| 449 | printf("Failed to get keyboard state from device %04x:%04x\n", |
| 450 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
| 451 | /* Abort, we don't want to use that non-functional keyboard. */ |
| 452 | return 0; |
| 453 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 454 | |
| 455 | /* Success. */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 456 | return 1; |
| 457 | } |
| 458 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 459 | /* Search for keyboard and register it if found. */ |
| 460 | int drv_usb_kbd_init(void) |
| 461 | { |
| 462 | struct stdio_dev usb_kbd_dev, *old_dev; |
| 463 | struct usb_device *dev; |
| 464 | char *stdinname = getenv("stdin"); |
| 465 | int error, i; |
| 466 | |
| 467 | /* Scan all USB Devices */ |
| 468 | for (i = 0; i < USB_MAX_DEVICE; i++) { |
| 469 | /* Get USB device. */ |
| 470 | dev = usb_get_dev_index(i); |
| 471 | if (!dev) |
| 472 | return -1; |
| 473 | |
| 474 | if (dev->devnum == -1) |
| 475 | continue; |
| 476 | |
| 477 | /* Try probing the keyboard */ |
| 478 | if (usb_kbd_probe(dev, 0) != 1) |
| 479 | continue; |
| 480 | |
| 481 | /* We found a keyboard, check if it is already registered. */ |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 482 | debug("USB KBD: found set up device.\n"); |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 483 | old_dev = stdio_get_by_name(DEVNAME); |
| 484 | if (old_dev) { |
| 485 | /* Already registered, just return ok. */ |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 486 | debug("USB KBD: is already registered.\n"); |
Vincent Palatin | 4d6839f | 2013-05-10 19:48:58 -0700 | [diff] [blame] | 487 | usb_kbd_deregister(); |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 488 | return 1; |
| 489 | } |
| 490 | |
| 491 | /* Register the keyboard */ |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 492 | debug("USB KBD: register.\n"); |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 493 | memset(&usb_kbd_dev, 0, sizeof(struct stdio_dev)); |
| 494 | strcpy(usb_kbd_dev.name, DEVNAME); |
| 495 | usb_kbd_dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; |
| 496 | usb_kbd_dev.putc = NULL; |
| 497 | usb_kbd_dev.puts = NULL; |
| 498 | usb_kbd_dev.getc = usb_kbd_getc; |
| 499 | usb_kbd_dev.tstc = usb_kbd_testc; |
| 500 | usb_kbd_dev.priv = (void *)dev; |
| 501 | error = stdio_register(&usb_kbd_dev); |
| 502 | if (error) |
| 503 | return error; |
| 504 | |
amartin@nvidia.com | c9d6202 | 2011-12-23 10:29:48 +0000 | [diff] [blame] | 505 | #ifdef CONFIG_CONSOLE_MUX |
| 506 | error = iomux_doenv(stdin, stdinname); |
| 507 | if (error) |
| 508 | return error; |
| 509 | #else |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 510 | /* Check if this is the standard input device. */ |
| 511 | if (strcmp(stdinname, DEVNAME)) |
| 512 | return 1; |
| 513 | |
| 514 | /* Reassign the console */ |
| 515 | if (overwrite_console()) |
| 516 | return 1; |
| 517 | |
| 518 | error = console_assign(stdin, DEVNAME); |
| 519 | if (error) |
| 520 | return error; |
amartin@nvidia.com | c9d6202 | 2011-12-23 10:29:48 +0000 | [diff] [blame] | 521 | #endif |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 522 | |
| 523 | return 1; |
| 524 | } |
| 525 | |
| 526 | /* No USB Keyboard found */ |
| 527 | return -1; |
| 528 | } |
| 529 | |
| 530 | /* Deregister the keyboard. */ |
| 531 | int usb_kbd_deregister(void) |
| 532 | { |
| 533 | #ifdef CONFIG_SYS_STDIO_DEREGISTER |
| 534 | return stdio_deregister(DEVNAME); |
| 535 | #else |
| 536 | return 1; |
| 537 | #endif |
| 538 | } |