]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/usb_kbd.c
stdio: Add force parameter to stdio_deregister
[people/ms/u-boot.git] / common / usb_kbd.c
CommitLineData
affae2bf
WD
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 *
1a459660 8 * SPDX-License-Identifier: GPL-2.0+
affae2bf
WD
9 */
10#include <common.h>
0ea09dfe 11#include <errno.h>
9a8c72a6 12#include <malloc.h>
52cb4d4f 13#include <stdio_dev.h>
c918261c 14#include <asm/byteorder.h>
affae2bf 15
affae2bf
WD
16#include <usb.h>
17
affae2bf 18/*
00b7d6ec 19 * If overwrite_console returns 1, the stdin, stderr and stdout
affae2bf
WD
20 * are switched to the serial port, else the settings in the
21 * environment are used
22 */
6d0f6bcf 23#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
00b7d6ec 24extern int overwrite_console(void);
affae2bf 25#else
00b7d6ec 26int overwrite_console(void)
affae2bf 27{
00b7d6ec 28 return 0;
affae2bf
WD
29}
30#endif
31
9a8c72a6
MV
32/* Keyboard sampling rate */
33#define REPEAT_RATE (40 / 4) /* 40msec -> 25cps */
34#define REPEAT_DELAY 10 /* 10 x REPEAT_RATE = 400msec */
affae2bf
WD
35
36#define NUM_LOCK 0x53
00b7d6ec
MV
37#define CAPS_LOCK 0x39
38#define SCROLL_LOCK 0x47
affae2bf 39
affae2bf 40/* Modifier bits */
9a8c72a6
MV
41#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)
49
50/* Size of the keyboard buffer */
51#define USB_KBD_BUFFER_LEN 0x20
52
53/* Device name */
00b7d6ec 54#define DEVNAME "usbkbd"
affae2bf 55
9a8c72a6
MV
56/* Keyboard maps */
57static const unsigned char usb_kbd_numkey[] = {
00b7d6ec
MV
58 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
59 '\r', 0x1b, '\b', '\t', ' ', '-', '=', '[', ']',
60 '\\', '#', ';', '\'', '`', ',', '.', '/'
affae2bf 61};
9a8c72a6 62static const unsigned char usb_kbd_numkey_shifted[] = {
00b7d6ec
MV
63 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
64 '\r', 0x1b, '\b', '\t', ' ', '_', '+', '{', '}',
65 '|', '~', ':', '"', '~', '<', '>', '?'
affae2bf
WD
66};
67
d53da847
VP
68static 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
4151a400
AM
74/*
75 * 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
9a8c72a6
MV
83/*
84 * 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)
48c8073e 91
9a8c72a6
MV
92#define USB_KBD_LEDMASK \
93 (USB_KBD_NUMLOCK | USB_KBD_CAPSLOCK | USB_KBD_SCROLLLOCK)
48c8073e 94
08a98b89
AC
95/*
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
9a8c72a6
MV
101struct usb_kbd_pdata {
102 uint32_t repeat_delay;
affae2bf 103
9a8c72a6
MV
104 uint32_t usb_in_pointer;
105 uint32_t usb_out_pointer;
106 uint8_t usb_kbd_buffer[USB_KBD_BUFFER_LEN];
48c8073e 107
d7475386 108 uint8_t *new;
08a98b89 109 uint8_t old[USB_KBD_BOOT_REPORT_SIZE];
48c8073e 110
9a8c72a6
MV
111 uint8_t flags;
112};
48c8073e 113
07551f23
JL
114extern 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
9a8c72a6
MV
119/* Generic keyboard event polling. */
120void usb_kbd_generic_poll(void)
affae2bf 121{
48c8073e
MV
122 struct stdio_dev *dev;
123 struct usb_device *usb_kbd_dev;
9a8c72a6
MV
124 struct usb_kbd_pdata *data;
125 struct usb_interface *iface;
126 struct usb_endpoint_descriptor *ep;
127 int pipe;
128 int maxp;
48c8073e 129
9a8c72a6
MV
130 /* Get the pointer to USB Keyboard device pointer */
131 dev = stdio_get_by_name(DEVNAME);
48c8073e 132 usb_kbd_dev = (struct usb_device *)dev->priv;
9a8c72a6
MV
133 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);
48c8073e 137
9a8c72a6
MV
138 /* Submit a interrupt transfer request */
139 maxp = usb_maxpacket(usb_kbd_dev, pipe);
140 usb_submit_int_msg(usb_kbd_dev, pipe, data->new,
08a98b89
AC
141 min(maxp, USB_KBD_BOOT_REPORT_SIZE),
142 ep->bInterval);
affae2bf
WD
143}
144
9a8c72a6
MV
145/* 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)
affae2bf 147{
9a8c72a6
MV
148 if (data->usb_in_pointer == USB_KBD_BUFFER_LEN - 1) {
149 /* Check for buffer full. */
150 if (data->usb_out_pointer == 0)
151 return;
affae2bf 152
9a8c72a6
MV
153 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;
affae2bf 158
9a8c72a6
MV
159 data->usb_in_pointer++;
160 }
affae2bf 161
9a8c72a6 162 data->usb_kbd_buffer[data->usb_in_pointer] = c;
affae2bf
WD
163}
164
9a8c72a6
MV
165/*
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.
affae2bf 169 */
affae2bf
WD
170static void usb_kbd_setled(struct usb_device *dev)
171{
9a8c72a6
MV
172 struct usb_interface *iface = &dev->config.if_desc[0];
173 struct usb_kbd_pdata *data = dev->privptr;
9b239381 174 ALLOC_ALIGN_BUFFER(uint32_t, leds, 1, USB_DMA_MINALIGN);
9a8c72a6 175
9b239381 176 *leds = data->flags & USB_KBD_LEDMASK;
affae2bf
WD
177 usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
178 USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
9b239381 179 0x200, iface->desc.bInterfaceNumber, leds, 1, 0);
affae2bf
WD
180}
181
9a8c72a6 182#define CAPITAL_MASK 0x20
affae2bf 183/* Translate the scancode in ASCII */
9a8c72a6
MV
184static int usb_kbd_translate(struct usb_kbd_pdata *data, unsigned char scancode,
185 unsigned char modifier, int pressed)
affae2bf 186{
9a8c72a6 187 uint8_t keycode = 0;
affae2bf 188
9a8c72a6 189 /* Key released */
00b7d6ec 190 if (pressed == 0) {
9a8c72a6 191 data->repeat_delay = 0;
affae2bf
WD
192 return 0;
193 }
9a8c72a6 194
00b7d6ec 195 if (pressed == 2) {
9a8c72a6
MV
196 data->repeat_delay++;
197 if (data->repeat_delay < REPEAT_DELAY)
affae2bf 198 return 0;
9a8c72a6
MV
199
200 data->repeat_delay = REPEAT_DELAY;
affae2bf 201 }
9a8c72a6
MV
202
203 /* Alphanumeric values */
204 if ((scancode > 3) && (scancode <= 0x1d)) {
205 keycode = scancode - 4 + 'a';
206
207 if (data->flags & USB_KBD_CAPSLOCK)
00b7d6ec 208 keycode &= ~CAPITAL_MASK;
9a8c72a6
MV
209
210 if (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) {
211 /* Handle CAPSLock + Shift pressed simultaneously */
00b7d6ec 212 if (keycode & CAPITAL_MASK)
00b7d6ec 213 keycode &= ~CAPITAL_MASK;
affae2bf 214 else
00b7d6ec 215 keycode |= CAPITAL_MASK;
affae2bf
WD
216 }
217 }
9a8c72a6
MV
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];
affae2bf 225 }
4785a694 226
4151a400
AM
227 /* Arrow keys */
228 if ((scancode >= 0x4f) && (scancode <= 0x52))
229 keycode = usb_kbd_arrow[scancode - 0x4f];
230
d53da847
VP
231 /* Numeric keypad */
232 if ((scancode >= 0x54) && (scancode <= 0x67))
233 keycode = usb_kbd_num_keypad[scancode - 0x54];
234
9a8c72a6 235 if (data->flags & USB_KBD_CTRL)
4785a694
ZW
236 keycode = scancode - 0x3;
237
00b7d6ec
MV
238 if (pressed == 1) {
239 if (scancode == NUM_LOCK) {
9a8c72a6 240 data->flags ^= USB_KBD_NUMLOCK;
affae2bf
WD
241 return 1;
242 }
9a8c72a6 243
00b7d6ec 244 if (scancode == CAPS_LOCK) {
9a8c72a6 245 data->flags ^= USB_KBD_CAPSLOCK;
affae2bf
WD
246 return 1;
247 }
00b7d6ec 248 if (scancode == SCROLL_LOCK) {
9a8c72a6 249 data->flags ^= USB_KBD_SCROLLLOCK;
affae2bf
WD
250 return 1;
251 }
252 }
9a8c72a6
MV
253
254 /* Report keycode if any */
255 if (keycode) {
ceb4972a 256 debug("%c", keycode);
9a8c72a6 257 usb_kbd_put_queue(data, keycode);
affae2bf 258 }
9a8c72a6 259
affae2bf
WD
260 return 0;
261}
262
9a8c72a6
MV
263static 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
08a98b89
AC
278 if ((old[i] > 3) &&
279 (memscan(new + 2, old[i], USB_KBD_BOOT_REPORT_SIZE - 2) ==
280 new + USB_KBD_BOOT_REPORT_SIZE)) {
9a8c72a6 281 res |= usb_kbd_translate(data, old[i], data->new[0], up);
08a98b89 282 }
9a8c72a6
MV
283
284 return res;
285}
286
affae2bf 287/* Interrupt service routine */
48c8073e 288static int usb_kbd_irq_worker(struct usb_device *dev)
affae2bf 289{
9a8c72a6
MV
290 struct usb_kbd_pdata *data = dev->privptr;
291 int i, res = 0;
4785a694 292
9a8c72a6
MV
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;
00b7d6ec 299
08a98b89 300 for (i = 2; i < USB_KBD_BOOT_REPORT_SIZE; i++) {
9a8c72a6
MV
301 res |= usb_kbd_service_key(dev, i, 0);
302 res |= usb_kbd_service_key(dev, i, 1);
affae2bf 303 }
00b7d6ec 304
9a8c72a6
MV
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
00b7d6ec 309 if (res == 1)
affae2bf 310 usb_kbd_setled(dev);
00b7d6ec 311
08a98b89 312 memcpy(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE);
00b7d6ec 313
9a8c72a6 314 return 1;
affae2bf
WD
315}
316
9a8c72a6 317/* Keyboard interrupt handler */
48c8073e
MV
318static int usb_kbd_irq(struct usb_device *dev)
319{
08a98b89
AC
320 if ((dev->irq_status != 0) ||
321 (dev->irq_act_len != USB_KBD_BOOT_REPORT_SIZE)) {
ceb4972a
VG
322 debug("USB KBD: Error %lX, len %d\n",
323 dev->irq_status, dev->irq_act_len);
48c8073e
MV
324 return 1;
325 }
326
327 return usb_kbd_irq_worker(dev);
328}
329
9a8c72a6
MV
330/* Interrupt polling */
331static inline void usb_kbd_poll_for_event(struct usb_device *dev)
332{
333#if defined(CONFIG_SYS_USB_EVENT_POLL)
f9636e8d
AM
334 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],
08a98b89
AC
349 min(maxp, USB_KBD_BOOT_REPORT_SIZE),
350 ep->bInterval);
f9636e8d 351
9a8c72a6
MV
352 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,
08a98b89
AC
358 1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE);
359 if (memcmp(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE))
9a8c72a6
MV
360 usb_kbd_irq_worker(dev);
361#endif
362}
363
364/* test if a character is in the queue */
709ea543 365static int usb_kbd_testc(struct stdio_dev *sdev)
9a8c72a6
MV
366{
367 struct stdio_dev *dev;
368 struct usb_device *usb_kbd_dev;
369 struct usb_kbd_pdata *data;
370
c95e2b9e
JL
371#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
9a8c72a6
MV
381 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 */
709ea543 391static int usb_kbd_getc(struct stdio_dev *sdev)
9a8c72a6
MV
392{
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. */
affae2bf
WD
413static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum)
414{
8f8bd565 415 struct usb_interface *iface;
affae2bf 416 struct usb_endpoint_descriptor *ep;
9a8c72a6 417 struct usb_kbd_pdata *data;
00b7d6ec 418 int pipe, maxp;
affae2bf 419
00b7d6ec
MV
420 if (dev->descriptor.bNumConfigurations != 1)
421 return 0;
9a8c72a6 422
affae2bf
WD
423 iface = &dev->config.if_desc[ifnum];
424
8f8bd565
TR
425 if (iface->desc.bInterfaceClass != 3)
426 return 0;
9a8c72a6 427
8f8bd565
TR
428 if (iface->desc.bInterfaceSubClass != 1)
429 return 0;
9a8c72a6 430
8f8bd565
TR
431 if (iface->desc.bInterfaceProtocol != 1)
432 return 0;
9a8c72a6 433
8f8bd565
TR
434 if (iface->desc.bNumEndpoints != 1)
435 return 0;
affae2bf
WD
436
437 ep = &iface->ep_desc[0];
438
9a8c72a6 439 /* Check if endpoint 1 is interrupt endpoint */
00b7d6ec
MV
440 if (!(ep->bEndpointAddress & 0x80))
441 return 0;
9a8c72a6 442
00b7d6ec
MV
443 if ((ep->bmAttributes & 3) != 3)
444 return 0;
9a8c72a6 445
ceb4972a 446 debug("USB KBD: found set protocol...\n");
9a8c72a6
MV
447
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
d7475386 457 /* allocate input buffer aligned and sized to USB DMA alignment */
08a98b89
AC
458 data->new = memalign(USB_DMA_MINALIGN,
459 roundup(USB_KBD_BOOT_REPORT_SIZE, USB_DMA_MINALIGN));
d7475386 460
9a8c72a6
MV
461 /* Insert private data into USB device structure */
462 dev->privptr = data;
463
464 /* Set IRQ handler */
465 dev->irq_handle = usb_kbd_irq;
466
affae2bf
WD
467 pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
468 maxp = usb_maxpacket(dev, pipe);
9a8c72a6
MV
469
470 /* We found a USB Keyboard, install it. */
471 usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0);
472
ceb4972a 473 debug("USB KBD: found set idle...\n");
9a8c72a6
MV
474 usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE, 0);
475
ceb4972a 476 debug("USB KBD: enable interrupt pipe...\n");
08a98b89
AC
477 if (usb_submit_int_msg(dev, pipe, data->new,
478 min(maxp, USB_KBD_BOOT_REPORT_SIZE),
5da2dc97
VP
479 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 }
9a8c72a6
MV
485
486 /* Success. */
affae2bf
WD
487 return 1;
488}
489
9a8c72a6
MV
490/* Search for keyboard and register it if found. */
491int drv_usb_kbd_init(void)
492{
3f78a280 493 struct stdio_dev usb_kbd_dev;
9a8c72a6
MV
494 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
9a8c72a6 512 /* Register the keyboard */
ceb4972a 513 debug("USB KBD: register.\n");
9a8c72a6
MV
514 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;
9a8c72a6
MV
517 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
fb3ef649
AM
524#ifdef CONFIG_CONSOLE_MUX
525 error = iomux_doenv(stdin, stdinname);
526 if (error)
527 return error;
528#else
9a8c72a6
MV
529 /* 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;
fb3ef649 540#endif
9a8c72a6
MV
541
542 return 1;
543 }
544
545 /* No USB Keyboard found */
546 return -1;
547}
548
549/* Deregister the keyboard. */
550int usb_kbd_deregister(void)
551{
552#ifdef CONFIG_SYS_STDIO_DEREGISTER
32d01926 553 int ret = stdio_deregister(DEVNAME, 0);
0ea09dfe
HG
554 if (ret && ret != -ENODEV)
555 return ret;
556
557 return 0;
9a8c72a6
MV
558#else
559 return 1;
560#endif
561}