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