blob: fdc083c70cf9952ec000429355b605b2ec4ff43c [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
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 Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00009 */
10#include <common.h>
Hans de Goedef5656232014-09-20 16:54:34 +020011#include <errno.h>
Marek Vasut7534a3e2011-10-10 16:34:26 +010012#include <malloc.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020013#include <stdio_dev.h>
Christian Eggers4e0e8d02008-05-21 22:12:00 +020014#include <asm/byteorder.h>
wdenkaffae2b2002-08-17 09:36:01 +000015
wdenkaffae2b2002-08-17 09:36:01 +000016#include <usb.h>
17
wdenkaffae2b2002-08-17 09:36:01 +000018/*
Marek Vasutb9300982011-10-10 05:34:25 +000019 * If overwrite_console returns 1, the stdin, stderr and stdout
wdenkaffae2b2002-08-17 09:36:01 +000020 * are switched to the serial port, else the settings in the
21 * environment are used
22 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020023#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
Marek Vasutb9300982011-10-10 05:34:25 +000024extern int overwrite_console(void);
wdenkaffae2b2002-08-17 09:36:01 +000025#else
Marek Vasutb9300982011-10-10 05:34:25 +000026int overwrite_console(void)
wdenkaffae2b2002-08-17 09:36:01 +000027{
Marek Vasutb9300982011-10-10 05:34:25 +000028 return 0;
wdenkaffae2b2002-08-17 09:36:01 +000029}
30#endif
31
Marek Vasut7534a3e2011-10-10 16:34:26 +010032/* Keyboard sampling rate */
33#define REPEAT_RATE (40 / 4) /* 40msec -> 25cps */
34#define REPEAT_DELAY 10 /* 10 x REPEAT_RATE = 400msec */
wdenkaffae2b2002-08-17 09:36:01 +000035
36#define NUM_LOCK 0x53
Marek Vasutb9300982011-10-10 05:34:25 +000037#define CAPS_LOCK 0x39
38#define SCROLL_LOCK 0x47
wdenkaffae2b2002-08-17 09:36:01 +000039
wdenkaffae2b2002-08-17 09:36:01 +000040/* Modifier bits */
Marek Vasut7534a3e2011-10-10 16:34:26 +010041#define LEFT_CNTR (1 << 0)
42#define LEFT_SHIFT (1 << 1)
43#define LEFT_ALT (1 << 2)
44#define LEFT_GUI (1 << 3)
45#define RIGHT_CNTR (1 << 4)
46#define RIGHT_SHIFT (1 << 5)
47#define RIGHT_ALT (1 << 6)
48#define RIGHT_GUI (1 << 7)
wdenkaffae2b2002-08-17 09:36:01 +000049
Marek Vasut7534a3e2011-10-10 16:34:26 +010050/* Size of the keyboard buffer */
51#define USB_KBD_BUFFER_LEN 0x20
wdenkaffae2b2002-08-17 09:36:01 +000052
Marek Vasut7534a3e2011-10-10 16:34:26 +010053/* Device name */
Marek Vasutb9300982011-10-10 05:34:25 +000054#define DEVNAME "usbkbd"
wdenkaffae2b2002-08-17 09:36:01 +000055
Marek Vasut7534a3e2011-10-10 16:34:26 +010056/* Keyboard maps */
57static const unsigned char usb_kbd_numkey[] = {
Marek Vasutb9300982011-10-10 05:34:25 +000058 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
59 '\r', 0x1b, '\b', '\t', ' ', '-', '=', '[', ']',
60 '\\', '#', ';', '\'', '`', ',', '.', '/'
wdenkaffae2b2002-08-17 09:36:01 +000061};
Marek Vasut7534a3e2011-10-10 16:34:26 +010062static const unsigned char usb_kbd_numkey_shifted[] = {
Marek Vasutb9300982011-10-10 05:34:25 +000063 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
64 '\r', 0x1b, '\b', '\t', ' ', '_', '+', '{', '}',
65 '|', '~', ':', '"', '~', '<', '>', '?'
wdenkaffae2b2002-08-17 09:36:01 +000066};
67
Vincent Palatin92f6a622012-01-09 12:59:36 +000068static const unsigned char usb_kbd_num_keypad[] = {
69 '/', '*', '-', '+', '\r',
70 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
71 '.', 0, 0, 0, '='
72};
73
Marek Vasut7534a3e2011-10-10 16:34:26 +010074/*
Allen Martin1bfb4cd2012-11-06 13:26:03 -080075 * map arrow keys to ^F/^B ^N/^P, can't really use the proper
76 * ANSI sequence for arrow keys because the queuing code breaks
77 * when a single keypress expands to 3 queue elements
78 */
79static const unsigned char usb_kbd_arrow[] = {
80 0x6, 0x2, 0xe, 0x10
81};
82
83/*
Marek Vasut7534a3e2011-10-10 16:34:26 +010084 * NOTE: It's important for the NUM, CAPS, SCROLL-lock bits to be in this
85 * order. See usb_kbd_setled() function!
86 */
87#define USB_KBD_NUMLOCK (1 << 0)
88#define USB_KBD_CAPSLOCK (1 << 1)
89#define USB_KBD_SCROLLLOCK (1 << 2)
90#define USB_KBD_CTRL (1 << 3)
Marek Vasut82cf9a42011-09-25 20:00:37 +010091
Marek Vasut7534a3e2011-10-10 16:34:26 +010092#define USB_KBD_LEDMASK \
93 (USB_KBD_NUMLOCK | USB_KBD_CAPSLOCK | USB_KBD_SCROLLLOCK)
Marek Vasut82cf9a42011-09-25 20:00:37 +010094
Adrian Cox17472dc2014-04-10 14:02:44 +010095/*
96 * USB Keyboard reports are 8 bytes in boot protocol.
97 * Appendix B of HID Device Class Definition 1.11
98 */
99#define USB_KBD_BOOT_REPORT_SIZE 8
100
Marek Vasut7534a3e2011-10-10 16:34:26 +0100101struct usb_kbd_pdata {
102 uint32_t repeat_delay;
wdenkaffae2b2002-08-17 09:36:01 +0000103
Marek Vasut7534a3e2011-10-10 16:34:26 +0100104 uint32_t usb_in_pointer;
105 uint32_t usb_out_pointer;
106 uint8_t usb_kbd_buffer[USB_KBD_BUFFER_LEN];
Marek Vasut82cf9a42011-09-25 20:00:37 +0100107
Allen Martinef769ca2012-10-24 08:32:04 +0000108 uint8_t *new;
Adrian Cox17472dc2014-04-10 14:02:44 +0100109 uint8_t old[USB_KBD_BOOT_REPORT_SIZE];
Marek Vasut82cf9a42011-09-25 20:00:37 +0100110
Marek Vasut7534a3e2011-10-10 16:34:26 +0100111 uint8_t flags;
112};
Marek Vasut82cf9a42011-09-25 20:00:37 +0100113
Jim Linc562f972013-08-13 19:04:22 +0800114extern int __maybe_unused net_busy_flag;
115
116/* The period of time between two calls of usb_kbd_testc(). */
117static unsigned long __maybe_unused kbd_testc_tms;
118
Marek Vasut7534a3e2011-10-10 16:34:26 +0100119/* Generic keyboard event polling. */
120void usb_kbd_generic_poll(void)
wdenkaffae2b2002-08-17 09:36:01 +0000121{
Marek Vasut82cf9a42011-09-25 20:00:37 +0100122 struct stdio_dev *dev;
123 struct usb_device *usb_kbd_dev;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100124 struct usb_kbd_pdata *data;
125 struct usb_interface *iface;
126 struct usb_endpoint_descriptor *ep;
127 int pipe;
128 int maxp;
Marek Vasut82cf9a42011-09-25 20:00:37 +0100129
Marek Vasut7534a3e2011-10-10 16:34:26 +0100130 /* Get the pointer to USB Keyboard device pointer */
131 dev = stdio_get_by_name(DEVNAME);
Marek Vasut82cf9a42011-09-25 20:00:37 +0100132 usb_kbd_dev = (struct usb_device *)dev->priv;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100133 data = usb_kbd_dev->privptr;
134 iface = &usb_kbd_dev->config.if_desc[0];
135 ep = &iface->ep_desc[0];
136 pipe = usb_rcvintpipe(usb_kbd_dev, ep->bEndpointAddress);
wdenkaffae2b2002-08-17 09:36:01 +0000137
Marek Vasut7534a3e2011-10-10 16:34:26 +0100138 /* Submit a interrupt transfer request */
139 maxp = usb_maxpacket(usb_kbd_dev, pipe);
140 usb_submit_int_msg(usb_kbd_dev, pipe, data->new,
Adrian Cox17472dc2014-04-10 14:02:44 +0100141 min(maxp, USB_KBD_BOOT_REPORT_SIZE),
142 ep->bInterval);
wdenkaffae2b2002-08-17 09:36:01 +0000143}
144
Marek Vasut7534a3e2011-10-10 16:34:26 +0100145/* Puts character in the queue and sets up the in and out pointer. */
146static void usb_kbd_put_queue(struct usb_kbd_pdata *data, char c)
wdenkaffae2b2002-08-17 09:36:01 +0000147{
Marek Vasut7534a3e2011-10-10 16:34:26 +0100148 if (data->usb_in_pointer == USB_KBD_BUFFER_LEN - 1) {
149 /* Check for buffer full. */
150 if (data->usb_out_pointer == 0)
151 return;
wdenkaffae2b2002-08-17 09:36:01 +0000152
Marek Vasut7534a3e2011-10-10 16:34:26 +0100153 data->usb_in_pointer = 0;
154 } else {
155 /* Check for buffer full. */
156 if (data->usb_in_pointer == data->usb_out_pointer - 1)
157 return;
wdenkaffae2b2002-08-17 09:36:01 +0000158
Marek Vasut7534a3e2011-10-10 16:34:26 +0100159 data->usb_in_pointer++;
160 }
wdenkaffae2b2002-08-17 09:36:01 +0000161
Marek Vasut7534a3e2011-10-10 16:34:26 +0100162 data->usb_kbd_buffer[data->usb_in_pointer] = c;
wdenkaffae2b2002-08-17 09:36:01 +0000163}
164
Marek Vasut7534a3e2011-10-10 16:34:26 +0100165/*
166 * Set the LEDs. Since this is used in the irq routine, the control job is
167 * issued with a timeout of 0. This means, that the job is queued without
168 * waiting for job completion.
wdenkaffae2b2002-08-17 09:36:01 +0000169 */
wdenkaffae2b2002-08-17 09:36:01 +0000170static void usb_kbd_setled(struct usb_device *dev)
171{
Marek Vasut7534a3e2011-10-10 16:34:26 +0100172 struct usb_interface *iface = &dev->config.if_desc[0];
173 struct usb_kbd_pdata *data = dev->privptr;
Hans de Goedee402b6c2014-09-20 16:51:26 +0200174 ALLOC_ALIGN_BUFFER(uint32_t, leds, 1, USB_DMA_MINALIGN);
Marek Vasut7534a3e2011-10-10 16:34:26 +0100175
Hans de Goedee402b6c2014-09-20 16:51:26 +0200176 *leds = data->flags & USB_KBD_LEDMASK;
wdenkaffae2b2002-08-17 09:36:01 +0000177 usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
178 USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Hans de Goedee402b6c2014-09-20 16:51:26 +0200179 0x200, iface->desc.bInterfaceNumber, leds, 1, 0);
wdenkaffae2b2002-08-17 09:36:01 +0000180}
181
Marek Vasut7534a3e2011-10-10 16:34:26 +0100182#define CAPITAL_MASK 0x20
wdenkaffae2b2002-08-17 09:36:01 +0000183/* Translate the scancode in ASCII */
Marek Vasut7534a3e2011-10-10 16:34:26 +0100184static int usb_kbd_translate(struct usb_kbd_pdata *data, unsigned char scancode,
185 unsigned char modifier, int pressed)
wdenkaffae2b2002-08-17 09:36:01 +0000186{
Marek Vasut7534a3e2011-10-10 16:34:26 +0100187 uint8_t keycode = 0;
wdenkaffae2b2002-08-17 09:36:01 +0000188
Marek Vasut7534a3e2011-10-10 16:34:26 +0100189 /* Key released */
Marek Vasutb9300982011-10-10 05:34:25 +0000190 if (pressed == 0) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100191 data->repeat_delay = 0;
wdenkaffae2b2002-08-17 09:36:01 +0000192 return 0;
193 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100194
Marek Vasutb9300982011-10-10 05:34:25 +0000195 if (pressed == 2) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100196 data->repeat_delay++;
197 if (data->repeat_delay < REPEAT_DELAY)
wdenkaffae2b2002-08-17 09:36:01 +0000198 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100199
200 data->repeat_delay = REPEAT_DELAY;
wdenkaffae2b2002-08-17 09:36:01 +0000201 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100202
203 /* Alphanumeric values */
204 if ((scancode > 3) && (scancode <= 0x1d)) {
205 keycode = scancode - 4 + 'a';
206
207 if (data->flags & USB_KBD_CAPSLOCK)
Marek Vasutb9300982011-10-10 05:34:25 +0000208 keycode &= ~CAPITAL_MASK;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100209
210 if (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) {
211 /* Handle CAPSLock + Shift pressed simultaneously */
Marek Vasutb9300982011-10-10 05:34:25 +0000212 if (keycode & CAPITAL_MASK)
Marek Vasutb9300982011-10-10 05:34:25 +0000213 keycode &= ~CAPITAL_MASK;
wdenkaffae2b2002-08-17 09:36:01 +0000214 else
Marek Vasutb9300982011-10-10 05:34:25 +0000215 keycode |= CAPITAL_MASK;
wdenkaffae2b2002-08-17 09:36:01 +0000216 }
217 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100218
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];
wdenkaffae2b2002-08-17 09:36:01 +0000225 }
Zhang Wei37cc4f02008-01-03 10:51:15 +0800226
Allen Martin1bfb4cd2012-11-06 13:26:03 -0800227 /* Arrow keys */
228 if ((scancode >= 0x4f) && (scancode <= 0x52))
229 keycode = usb_kbd_arrow[scancode - 0x4f];
230
Vincent Palatin92f6a622012-01-09 12:59:36 +0000231 /* Numeric keypad */
232 if ((scancode >= 0x54) && (scancode <= 0x67))
233 keycode = usb_kbd_num_keypad[scancode - 0x54];
234
Marek Vasut7534a3e2011-10-10 16:34:26 +0100235 if (data->flags & USB_KBD_CTRL)
Zhang Wei37cc4f02008-01-03 10:51:15 +0800236 keycode = scancode - 0x3;
237
Marek Vasutb9300982011-10-10 05:34:25 +0000238 if (pressed == 1) {
239 if (scancode == NUM_LOCK) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100240 data->flags ^= USB_KBD_NUMLOCK;
wdenkaffae2b2002-08-17 09:36:01 +0000241 return 1;
242 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100243
Marek Vasutb9300982011-10-10 05:34:25 +0000244 if (scancode == CAPS_LOCK) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100245 data->flags ^= USB_KBD_CAPSLOCK;
wdenkaffae2b2002-08-17 09:36:01 +0000246 return 1;
247 }
Marek Vasutb9300982011-10-10 05:34:25 +0000248 if (scancode == SCROLL_LOCK) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100249 data->flags ^= USB_KBD_SCROLLLOCK;
wdenkaffae2b2002-08-17 09:36:01 +0000250 return 1;
251 }
252 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100253
254 /* Report keycode if any */
255 if (keycode) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530256 debug("%c", keycode);
Marek Vasut7534a3e2011-10-10 16:34:26 +0100257 usb_kbd_put_queue(data, keycode);
wdenkaffae2b2002-08-17 09:36:01 +0000258 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100259
wdenkaffae2b2002-08-17 09:36:01 +0000260 return 0;
261}
262
Marek Vasut7534a3e2011-10-10 16:34:26 +0100263static uint32_t usb_kbd_service_key(struct usb_device *dev, int i, int up)
264{
265 uint32_t res = 0;
266 struct usb_kbd_pdata *data = dev->privptr;
267 uint8_t *new;
268 uint8_t *old;
269
270 if (up) {
271 new = data->old;
272 old = data->new;
273 } else {
274 new = data->new;
275 old = data->old;
276 }
277
Adrian Cox17472dc2014-04-10 14:02:44 +0100278 if ((old[i] > 3) &&
279 (memscan(new + 2, old[i], USB_KBD_BOOT_REPORT_SIZE - 2) ==
280 new + USB_KBD_BOOT_REPORT_SIZE)) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100281 res |= usb_kbd_translate(data, old[i], data->new[0], up);
Adrian Cox17472dc2014-04-10 14:02:44 +0100282 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100283
284 return res;
285}
286
wdenkaffae2b2002-08-17 09:36:01 +0000287/* Interrupt service routine */
Marek Vasut82cf9a42011-09-25 20:00:37 +0100288static int usb_kbd_irq_worker(struct usb_device *dev)
wdenkaffae2b2002-08-17 09:36:01 +0000289{
Marek Vasut7534a3e2011-10-10 16:34:26 +0100290 struct usb_kbd_pdata *data = dev->privptr;
291 int i, res = 0;
wdenkaffae2b2002-08-17 09:36:01 +0000292
Marek Vasut7534a3e2011-10-10 16:34:26 +0100293 /* 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 Wei37cc4f02008-01-03 10:51:15 +0800299
Adrian Cox17472dc2014-04-10 14:02:44 +0100300 for (i = 2; i < USB_KBD_BOOT_REPORT_SIZE; i++) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100301 res |= usb_kbd_service_key(dev, i, 0);
302 res |= usb_kbd_service_key(dev, i, 1);
wdenkaffae2b2002-08-17 09:36:01 +0000303 }
Marek Vasutb9300982011-10-10 05:34:25 +0000304
Marek Vasut7534a3e2011-10-10 16:34:26 +0100305 /* 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 Vasutb9300982011-10-10 05:34:25 +0000309 if (res == 1)
wdenkaffae2b2002-08-17 09:36:01 +0000310 usb_kbd_setled(dev);
Marek Vasutb9300982011-10-10 05:34:25 +0000311
Adrian Cox17472dc2014-04-10 14:02:44 +0100312 memcpy(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE);
Marek Vasutb9300982011-10-10 05:34:25 +0000313
Marek Vasut7534a3e2011-10-10 16:34:26 +0100314 return 1;
wdenkaffae2b2002-08-17 09:36:01 +0000315}
316
Marek Vasut7534a3e2011-10-10 16:34:26 +0100317/* Keyboard interrupt handler */
Marek Vasut82cf9a42011-09-25 20:00:37 +0100318static int usb_kbd_irq(struct usb_device *dev)
319{
Adrian Cox17472dc2014-04-10 14:02:44 +0100320 if ((dev->irq_status != 0) ||
321 (dev->irq_act_len != USB_KBD_BOOT_REPORT_SIZE)) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530322 debug("USB KBD: Error %lX, len %d\n",
323 dev->irq_status, dev->irq_act_len);
Marek Vasut82cf9a42011-09-25 20:00:37 +0100324 return 1;
325 }
326
327 return usb_kbd_irq_worker(dev);
328}
329
Marek Vasut7534a3e2011-10-10 16:34:26 +0100330/* Interrupt polling */
331static inline void usb_kbd_poll_for_event(struct usb_device *dev)
332{
333#if defined(CONFIG_SYS_USB_EVENT_POLL)
amartin@nvidia.com185da952011-12-20 14:56:16 +0000334 struct usb_interface *iface;
335 struct usb_endpoint_descriptor *ep;
336 struct usb_kbd_pdata *data;
337 int pipe;
338 int maxp;
339
340 /* Get the pointer to USB Keyboard device pointer */
341 data = dev->privptr;
342 iface = &dev->config.if_desc[0];
343 ep = &iface->ep_desc[0];
344 pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
345
346 /* Submit a interrupt transfer request */
347 maxp = usb_maxpacket(dev, pipe);
348 usb_submit_int_msg(dev, pipe, &data->new[0],
Adrian Cox17472dc2014-04-10 14:02:44 +0100349 min(maxp, USB_KBD_BOOT_REPORT_SIZE),
350 ep->bInterval);
amartin@nvidia.com185da952011-12-20 14:56:16 +0000351
Marek Vasut7534a3e2011-10-10 16:34:26 +0100352 usb_kbd_irq_worker(dev);
353#elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
354 struct usb_interface *iface;
355 struct usb_kbd_pdata *data = dev->privptr;
356 iface = &dev->config.if_desc[0];
357 usb_get_report(dev, iface->desc.bInterfaceNumber,
Adrian Cox17472dc2014-04-10 14:02:44 +0100358 1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE);
359 if (memcmp(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE))
Marek Vasut7534a3e2011-10-10 16:34:26 +0100360 usb_kbd_irq_worker(dev);
361#endif
362}
363
364/* test if a character is in the queue */
Simon Glass0d1e1f72014-07-23 06:54:59 -0600365static int usb_kbd_testc(struct stdio_dev *sdev)
Marek Vasut7534a3e2011-10-10 16:34:26 +0100366{
367 struct stdio_dev *dev;
368 struct usb_device *usb_kbd_dev;
369 struct usb_kbd_pdata *data;
370
Jim Line2ebec82013-08-26 20:21:09 +0800371#ifdef CONFIG_CMD_NET
372 /*
373 * If net_busy_flag is 1, NET transfer is running,
374 * then we check key-pressed every second (first check may be
375 * less than 1 second) to improve TFTP booting performance.
376 */
377 if (net_busy_flag && (get_timer(kbd_testc_tms) < CONFIG_SYS_HZ))
378 return 0;
379 kbd_testc_tms = get_timer(0);
380#endif
Marek Vasut7534a3e2011-10-10 16:34:26 +0100381 dev = stdio_get_by_name(DEVNAME);
382 usb_kbd_dev = (struct usb_device *)dev->priv;
383 data = usb_kbd_dev->privptr;
384
385 usb_kbd_poll_for_event(usb_kbd_dev);
386
387 return !(data->usb_in_pointer == data->usb_out_pointer);
388}
389
390/* gets the character from the queue */
Simon Glass0d1e1f72014-07-23 06:54:59 -0600391static int usb_kbd_getc(struct stdio_dev *sdev)
Marek Vasut7534a3e2011-10-10 16:34:26 +0100392{
393 struct stdio_dev *dev;
394 struct usb_device *usb_kbd_dev;
395 struct usb_kbd_pdata *data;
396
397 dev = stdio_get_by_name(DEVNAME);
398 usb_kbd_dev = (struct usb_device *)dev->priv;
399 data = usb_kbd_dev->privptr;
400
401 while (data->usb_in_pointer == data->usb_out_pointer)
402 usb_kbd_poll_for_event(usb_kbd_dev);
403
404 if (data->usb_out_pointer == USB_KBD_BUFFER_LEN - 1)
405 data->usb_out_pointer = 0;
406 else
407 data->usb_out_pointer++;
408
409 return data->usb_kbd_buffer[data->usb_out_pointer];
410}
411
412/* probes the USB device dev for keyboard type. */
wdenkaffae2b2002-08-17 09:36:01 +0000413static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum)
414{
Tom Rix83b9e1d2009-10-31 12:37:38 -0500415 struct usb_interface *iface;
wdenkaffae2b2002-08-17 09:36:01 +0000416 struct usb_endpoint_descriptor *ep;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100417 struct usb_kbd_pdata *data;
Marek Vasutb9300982011-10-10 05:34:25 +0000418 int pipe, maxp;
wdenkaffae2b2002-08-17 09:36:01 +0000419
Marek Vasutb9300982011-10-10 05:34:25 +0000420 if (dev->descriptor.bNumConfigurations != 1)
421 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100422
wdenkaffae2b2002-08-17 09:36:01 +0000423 iface = &dev->config.if_desc[ifnum];
424
Tom Rix83b9e1d2009-10-31 12:37:38 -0500425 if (iface->desc.bInterfaceClass != 3)
426 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100427
Tom Rix83b9e1d2009-10-31 12:37:38 -0500428 if (iface->desc.bInterfaceSubClass != 1)
429 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100430
Tom Rix83b9e1d2009-10-31 12:37:38 -0500431 if (iface->desc.bInterfaceProtocol != 1)
432 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100433
Tom Rix83b9e1d2009-10-31 12:37:38 -0500434 if (iface->desc.bNumEndpoints != 1)
435 return 0;
wdenkaffae2b2002-08-17 09:36:01 +0000436
437 ep = &iface->ep_desc[0];
438
Marek Vasut7534a3e2011-10-10 16:34:26 +0100439 /* Check if endpoint 1 is interrupt endpoint */
Marek Vasutb9300982011-10-10 05:34:25 +0000440 if (!(ep->bEndpointAddress & 0x80))
441 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100442
Marek Vasutb9300982011-10-10 05:34:25 +0000443 if ((ep->bmAttributes & 3) != 3)
444 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100445
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530446 debug("USB KBD: found set protocol...\n");
Marek Vasut7534a3e2011-10-10 16:34:26 +0100447
448 data = malloc(sizeof(struct usb_kbd_pdata));
449 if (!data) {
450 printf("USB KBD: Error allocating private data\n");
451 return 0;
452 }
453
454 /* Clear private data */
455 memset(data, 0, sizeof(struct usb_kbd_pdata));
456
Allen Martinef769ca2012-10-24 08:32:04 +0000457 /* allocate input buffer aligned and sized to USB DMA alignment */
Adrian Cox17472dc2014-04-10 14:02:44 +0100458 data->new = memalign(USB_DMA_MINALIGN,
459 roundup(USB_KBD_BOOT_REPORT_SIZE, USB_DMA_MINALIGN));
Allen Martinef769ca2012-10-24 08:32:04 +0000460
Marek Vasut7534a3e2011-10-10 16:34:26 +0100461 /* Insert private data into USB device structure */
462 dev->privptr = data;
463
464 /* Set IRQ handler */
465 dev->irq_handle = usb_kbd_irq;
466
wdenkaffae2b2002-08-17 09:36:01 +0000467 pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
468 maxp = usb_maxpacket(dev, pipe);
Marek Vasut7534a3e2011-10-10 16:34:26 +0100469
470 /* We found a USB Keyboard, install it. */
471 usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0);
472
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530473 debug("USB KBD: found set idle...\n");
Marek Vasut7534a3e2011-10-10 16:34:26 +0100474 usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE, 0);
475
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530476 debug("USB KBD: enable interrupt pipe...\n");
Adrian Cox17472dc2014-04-10 14:02:44 +0100477 if (usb_submit_int_msg(dev, pipe, data->new,
478 min(maxp, USB_KBD_BOOT_REPORT_SIZE),
Vincent Palatin0eab9982013-05-10 19:48:59 -0700479 ep->bInterval) < 0) {
480 printf("Failed to get keyboard state from device %04x:%04x\n",
481 dev->descriptor.idVendor, dev->descriptor.idProduct);
482 /* Abort, we don't want to use that non-functional keyboard. */
483 return 0;
484 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100485
486 /* Success. */
wdenkaffae2b2002-08-17 09:36:01 +0000487 return 1;
488}
489
Marek Vasut7534a3e2011-10-10 16:34:26 +0100490/* Search for keyboard and register it if found. */
491int drv_usb_kbd_init(void)
492{
Hans de Goede7d21c8b2014-09-20 16:54:36 +0200493 struct stdio_dev usb_kbd_dev;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100494 struct usb_device *dev;
495 char *stdinname = getenv("stdin");
496 int error, i;
497
498 /* Scan all USB Devices */
499 for (i = 0; i < USB_MAX_DEVICE; i++) {
500 /* Get USB device. */
501 dev = usb_get_dev_index(i);
502 if (!dev)
503 return -1;
504
505 if (dev->devnum == -1)
506 continue;
507
508 /* Try probing the keyboard */
509 if (usb_kbd_probe(dev, 0) != 1)
510 continue;
511
Marek Vasut7534a3e2011-10-10 16:34:26 +0100512 /* Register the keyboard */
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530513 debug("USB KBD: register.\n");
Marek Vasut7534a3e2011-10-10 16:34:26 +0100514 memset(&usb_kbd_dev, 0, sizeof(struct stdio_dev));
515 strcpy(usb_kbd_dev.name, DEVNAME);
516 usb_kbd_dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100517 usb_kbd_dev.getc = usb_kbd_getc;
518 usb_kbd_dev.tstc = usb_kbd_testc;
519 usb_kbd_dev.priv = (void *)dev;
520 error = stdio_register(&usb_kbd_dev);
521 if (error)
522 return error;
523
amartin@nvidia.comc9d62022011-12-23 10:29:48 +0000524#ifdef CONFIG_CONSOLE_MUX
525 error = iomux_doenv(stdin, stdinname);
526 if (error)
527 return error;
528#else
Marek Vasut7534a3e2011-10-10 16:34:26 +0100529 /* Check if this is the standard input device. */
530 if (strcmp(stdinname, DEVNAME))
531 return 1;
532
533 /* Reassign the console */
534 if (overwrite_console())
535 return 1;
536
537 error = console_assign(stdin, DEVNAME);
538 if (error)
539 return error;
amartin@nvidia.comc9d62022011-12-23 10:29:48 +0000540#endif
Marek Vasut7534a3e2011-10-10 16:34:26 +0100541
542 return 1;
543 }
544
545 /* No USB Keyboard found */
546 return -1;
547}
548
549/* Deregister the keyboard. */
Hans de Goede3e51df02014-09-20 16:54:38 +0200550int usb_kbd_deregister(int force)
Marek Vasut7534a3e2011-10-10 16:34:26 +0100551{
552#ifdef CONFIG_SYS_STDIO_DEREGISTER
Hans de Goede3e51df02014-09-20 16:54:38 +0200553 int ret = stdio_deregister(DEVNAME, force);
Hans de Goedef5656232014-09-20 16:54:34 +0200554 if (ret && ret != -ENODEV)
555 return ret;
556
557 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100558#else
559 return 1;
560#endif
561}