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