Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2001 |
| 4 | * Denis Peter, MPL AG Switzerland |
| 5 | * |
| 6 | * Part of this source has been derived from the Linux USB |
| 7 | * project. |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 8 | */ |
| 9 | #include <common.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 10 | #include <console.h> |
Simon Glass | 5f1296b | 2015-03-25 12:22:34 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 12 | #include <env.h> |
Hans de Goede | f565623 | 2014-09-20 16:54:34 +0200 | [diff] [blame] | 13 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 15 | #include <malloc.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 16 | #include <memalign.h> |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 17 | #include <stdio_dev.h> |
Michal Simek | 07b3cc9 | 2018-06-27 14:16:54 +0200 | [diff] [blame] | 18 | #include <watchdog.h> |
Christian Eggers | 4e0e8d0 | 2008-05-21 22:12:00 +0200 | [diff] [blame] | 19 | #include <asm/byteorder.h> |
Thomas Watson | d36c85a | 2022-02-10 15:13:14 -0600 | [diff] [blame] | 20 | #ifdef CONFIG_SANDBOX |
| 21 | #include <asm/state.h> |
| 22 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 23 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 24 | #include <usb.h> |
| 25 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 26 | /* |
Janne Grunau | 4a7fda9 | 2024-04-04 08:25:53 +0200 | [diff] [blame] | 27 | * USB vendor and product IDs used for quirks. |
| 28 | */ |
| 29 | #define USB_VENDOR_ID_APPLE 0x05ac |
| 30 | #define USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021 0x029c |
| 31 | #define USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021 0x029a |
| 32 | #define USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021 0x029f |
| 33 | |
Janne Grunau | c838172 | 2024-04-04 08:25:54 +0200 | [diff] [blame] | 34 | #define USB_VENDOR_ID_KEYCHRON 0x3434 |
| 35 | |
| 36 | #define USB_HID_QUIRK_POLL_NO_REPORT_IDLE BIT(0) |
| 37 | |
Janne Grunau | 4a7fda9 | 2024-04-04 08:25:53 +0200 | [diff] [blame] | 38 | /* |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 39 | * If overwrite_console returns 1, the stdin, stderr and stdout |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 40 | * are switched to the serial port, else the settings in the |
| 41 | * environment are used |
| 42 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 43 | #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 44 | extern int overwrite_console(void); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 45 | #else |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 46 | int overwrite_console(void) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 47 | { |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 48 | return 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 49 | } |
| 50 | #endif |
| 51 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 52 | /* Keyboard sampling rate */ |
Hans de Goede | 650d720 | 2015-05-19 22:51:03 +0200 | [diff] [blame] | 53 | #define REPEAT_RATE 40 /* 40msec -> 25cps */ |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 54 | #define REPEAT_DELAY 10 /* 10 x REPEAT_RATE = 400msec */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 55 | |
| 56 | #define NUM_LOCK 0x53 |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 57 | #define CAPS_LOCK 0x39 |
| 58 | #define SCROLL_LOCK 0x47 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 59 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 60 | /* Modifier bits */ |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 61 | #define LEFT_CNTR (1 << 0) |
| 62 | #define LEFT_SHIFT (1 << 1) |
| 63 | #define LEFT_ALT (1 << 2) |
| 64 | #define LEFT_GUI (1 << 3) |
| 65 | #define RIGHT_CNTR (1 << 4) |
| 66 | #define RIGHT_SHIFT (1 << 5) |
| 67 | #define RIGHT_ALT (1 << 6) |
| 68 | #define RIGHT_GUI (1 << 7) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 69 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 70 | /* Size of the keyboard buffer */ |
| 71 | #define USB_KBD_BUFFER_LEN 0x20 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 72 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 73 | /* Device name */ |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 74 | #define DEVNAME "usbkbd" |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 75 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 76 | /* Keyboard maps */ |
| 77 | static const unsigned char usb_kbd_numkey[] = { |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 78 | '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', |
| 79 | '\r', 0x1b, '\b', '\t', ' ', '-', '=', '[', ']', |
| 80 | '\\', '#', ';', '\'', '`', ',', '.', '/' |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 81 | }; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 82 | static const unsigned char usb_kbd_numkey_shifted[] = { |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 83 | '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', |
| 84 | '\r', 0x1b, '\b', '\t', ' ', '_', '+', '{', '}', |
| 85 | '|', '~', ':', '"', '~', '<', '>', '?' |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Vincent Palatin | 92f6a62 | 2012-01-09 12:59:36 +0000 | [diff] [blame] | 88 | static const unsigned char usb_kbd_num_keypad[] = { |
| 89 | '/', '*', '-', '+', '\r', |
| 90 | '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', |
| 91 | '.', 0, 0, 0, '=' |
| 92 | }; |
| 93 | |
Heinrich Schuchardt | d05aaef | 2019-11-23 18:15:20 +0100 | [diff] [blame] | 94 | static const u8 usb_special_keys[] = { |
Heinrich Schuchardt | 8e505ee | 2019-11-23 18:15:21 +0100 | [diff] [blame] | 95 | #ifdef CONFIG_USB_KEYBOARD_FN_KEYS |
| 96 | '2', 'H', '5', '3', 'F', '6', 'C', 'D', 'B', 'A' |
| 97 | #else |
Heinrich Schuchardt | d05aaef | 2019-11-23 18:15:20 +0100 | [diff] [blame] | 98 | 'C', 'D', 'B', 'A' |
Heinrich Schuchardt | 8e505ee | 2019-11-23 18:15:21 +0100 | [diff] [blame] | 99 | #endif |
Allen Martin | 1bfb4cd | 2012-11-06 13:26:03 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | /* |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 103 | * NOTE: It's important for the NUM, CAPS, SCROLL-lock bits to be in this |
| 104 | * order. See usb_kbd_setled() function! |
| 105 | */ |
| 106 | #define USB_KBD_NUMLOCK (1 << 0) |
| 107 | #define USB_KBD_CAPSLOCK (1 << 1) |
| 108 | #define USB_KBD_SCROLLLOCK (1 << 2) |
| 109 | #define USB_KBD_CTRL (1 << 3) |
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 | #define USB_KBD_LEDMASK \ |
| 112 | (USB_KBD_NUMLOCK | USB_KBD_CAPSLOCK | USB_KBD_SCROLLLOCK) |
Marek Vasut | 82cf9a4 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 113 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 114 | struct usb_kbd_pdata { |
Hans de Goede | 3c79470 | 2014-09-24 14:06:10 +0200 | [diff] [blame] | 115 | unsigned long intpipe; |
| 116 | int intpktsize; |
| 117 | int intinterval; |
Hans de Goede | 650d720 | 2015-05-19 22:51:03 +0200 | [diff] [blame] | 118 | unsigned long last_report; |
Hans de Goede | 2d02ae7 | 2014-09-24 14:06:11 +0200 | [diff] [blame] | 119 | struct int_queue *intq; |
Hans de Goede | 3c79470 | 2014-09-24 14:06:10 +0200 | [diff] [blame] | 120 | |
Janne Grunau | 4a7fda9 | 2024-04-04 08:25:53 +0200 | [diff] [blame] | 121 | uint32_t ifnum; |
| 122 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 123 | uint32_t repeat_delay; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 124 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 125 | uint32_t usb_in_pointer; |
| 126 | uint32_t usb_out_pointer; |
| 127 | uint8_t usb_kbd_buffer[USB_KBD_BUFFER_LEN]; |
Marek Vasut | 82cf9a4 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 128 | |
Allen Martin | ef769ca | 2012-10-24 08:32:04 +0000 | [diff] [blame] | 129 | uint8_t *new; |
Adrian Cox | 17472dc | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 130 | uint8_t old[USB_KBD_BOOT_REPORT_SIZE]; |
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 | uint8_t flags; |
| 133 | }; |
Marek Vasut | 82cf9a4 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 134 | |
Jim Lin | c562f97 | 2013-08-13 19:04:22 +0800 | [diff] [blame] | 135 | extern int __maybe_unused net_busy_flag; |
| 136 | |
| 137 | /* The period of time between two calls of usb_kbd_testc(). */ |
Thomas Watson | d36c85a | 2022-02-10 15:13:14 -0600 | [diff] [blame] | 138 | static unsigned long kbd_testc_tms; |
Jim Lin | c562f97 | 2013-08-13 19:04:22 +0800 | [diff] [blame] | 139 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 140 | /* Puts character in the queue and sets up the in and out pointer. */ |
Heinrich Schuchardt | 90d746f | 2019-11-23 18:15:19 +0100 | [diff] [blame] | 141 | static void usb_kbd_put_queue(struct usb_kbd_pdata *data, u8 c) |
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 | if (data->usb_in_pointer == USB_KBD_BUFFER_LEN - 1) { |
| 144 | /* Check for buffer full. */ |
| 145 | if (data->usb_out_pointer == 0) |
| 146 | return; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 147 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 148 | data->usb_in_pointer = 0; |
| 149 | } else { |
| 150 | /* Check for buffer full. */ |
| 151 | if (data->usb_in_pointer == data->usb_out_pointer - 1) |
| 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++; |
| 155 | } |
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_kbd_buffer[data->usb_in_pointer] = c; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 160 | /* |
| 161 | * Set the LEDs. Since this is used in the irq routine, the control job is |
| 162 | * issued with a timeout of 0. This means, that the job is queued without |
| 163 | * waiting for job completion. |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 164 | */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 165 | static void usb_kbd_setled(struct usb_device *dev) |
| 166 | { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 167 | struct usb_kbd_pdata *data = dev->privptr; |
Janne Grunau | 4a7fda9 | 2024-04-04 08:25:53 +0200 | [diff] [blame] | 168 | struct usb_interface *iface = &dev->config.if_desc[data->ifnum]; |
Hans de Goede | e402b6c | 2014-09-20 16:51:26 +0200 | [diff] [blame] | 169 | ALLOC_ALIGN_BUFFER(uint32_t, leds, 1, USB_DMA_MINALIGN); |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 170 | |
Hans de Goede | e402b6c | 2014-09-20 16:51:26 +0200 | [diff] [blame] | 171 | *leds = data->flags & USB_KBD_LEDMASK; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 172 | usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 173 | USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
Hans de Goede | e402b6c | 2014-09-20 16:51:26 +0200 | [diff] [blame] | 174 | 0x200, iface->desc.bInterfaceNumber, leds, 1, 0); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 177 | #define CAPITAL_MASK 0x20 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 178 | /* Translate the scancode in ASCII */ |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 179 | static int usb_kbd_translate(struct usb_kbd_pdata *data, unsigned char scancode, |
| 180 | unsigned char modifier, int pressed) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 181 | { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 182 | uint8_t keycode = 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 183 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 184 | /* Key released */ |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 185 | if (pressed == 0) { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 186 | data->repeat_delay = 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 187 | return 0; |
| 188 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 189 | |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 190 | if (pressed == 2) { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 191 | data->repeat_delay++; |
| 192 | if (data->repeat_delay < REPEAT_DELAY) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 193 | return 0; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 194 | |
| 195 | data->repeat_delay = REPEAT_DELAY; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 196 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 197 | |
| 198 | /* Alphanumeric values */ |
| 199 | if ((scancode > 3) && (scancode <= 0x1d)) { |
| 200 | keycode = scancode - 4 + 'a'; |
| 201 | |
| 202 | if (data->flags & USB_KBD_CAPSLOCK) |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 203 | keycode &= ~CAPITAL_MASK; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 204 | |
| 205 | if (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) { |
| 206 | /* Handle CAPSLock + Shift pressed simultaneously */ |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 207 | if (keycode & CAPITAL_MASK) |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 208 | keycode &= ~CAPITAL_MASK; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 209 | else |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 210 | keycode |= CAPITAL_MASK; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 213 | |
Marek Vasut | 0082cd1 | 2016-01-25 22:00:44 +0100 | [diff] [blame] | 214 | if ((scancode > 0x1d) && (scancode < 0x39)) { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 215 | /* Shift pressed */ |
| 216 | if (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) |
| 217 | keycode = usb_kbd_numkey_shifted[scancode - 0x1e]; |
| 218 | else |
| 219 | keycode = usb_kbd_numkey[scancode - 0x1e]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 220 | } |
Zhang Wei | 37cc4f0 | 2008-01-03 10:51:15 +0800 | [diff] [blame] | 221 | |
Vincent Palatin | 92f6a62 | 2012-01-09 12:59:36 +0000 | [diff] [blame] | 222 | /* Numeric keypad */ |
| 223 | if ((scancode >= 0x54) && (scancode <= 0x67)) |
| 224 | keycode = usb_kbd_num_keypad[scancode - 0x54]; |
| 225 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 226 | if (data->flags & USB_KBD_CTRL) |
Zhang Wei | 37cc4f0 | 2008-01-03 10:51:15 +0800 | [diff] [blame] | 227 | keycode = scancode - 0x3; |
| 228 | |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 229 | if (pressed == 1) { |
| 230 | if (scancode == NUM_LOCK) { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 231 | data->flags ^= USB_KBD_NUMLOCK; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 232 | return 1; |
| 233 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 234 | |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 235 | if (scancode == CAPS_LOCK) { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 236 | data->flags ^= USB_KBD_CAPSLOCK; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 237 | return 1; |
| 238 | } |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 239 | if (scancode == SCROLL_LOCK) { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 240 | data->flags ^= USB_KBD_SCROLLLOCK; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 241 | return 1; |
| 242 | } |
| 243 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 244 | |
| 245 | /* Report keycode if any */ |
Heinrich Schuchardt | d05aaef | 2019-11-23 18:15:20 +0100 | [diff] [blame] | 246 | if (keycode) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 247 | debug("%c", keycode); |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 248 | usb_kbd_put_queue(data, keycode); |
Heinrich Schuchardt | d05aaef | 2019-11-23 18:15:20 +0100 | [diff] [blame] | 249 | return 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 250 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 251 | |
Heinrich Schuchardt | 8e505ee | 2019-11-23 18:15:21 +0100 | [diff] [blame] | 252 | #ifdef CONFIG_USB_KEYBOARD_FN_KEYS |
| 253 | if (scancode < 0x3a || scancode > 0x52 || |
| 254 | scancode == 0x46 || scancode == 0x47) |
| 255 | return 1; |
| 256 | |
| 257 | usb_kbd_put_queue(data, 0x1b); |
| 258 | if (scancode < 0x3e) { |
| 259 | /* F1 - F4 */ |
| 260 | usb_kbd_put_queue(data, 0x4f); |
| 261 | usb_kbd_put_queue(data, scancode - 0x3a + 'P'); |
| 262 | return 0; |
| 263 | } |
| 264 | usb_kbd_put_queue(data, '['); |
| 265 | if (scancode < 0x42) { |
| 266 | /* F5 - F8 */ |
| 267 | usb_kbd_put_queue(data, '1'); |
| 268 | if (scancode == 0x3e) |
| 269 | --scancode; |
| 270 | keycode = scancode - 0x3f + '7'; |
| 271 | } else if (scancode < 0x49) { |
| 272 | /* F9 - F12 */ |
| 273 | usb_kbd_put_queue(data, '2'); |
| 274 | if (scancode > 0x43) |
| 275 | ++scancode; |
| 276 | keycode = scancode - 0x42 + '0'; |
| 277 | } else { |
| 278 | /* |
| 279 | * INSERT, HOME, PAGE UP, DELETE, END, PAGE DOWN, |
| 280 | * RIGHT, LEFT, DOWN, UP |
| 281 | */ |
| 282 | keycode = usb_special_keys[scancode - 0x49]; |
| 283 | } |
| 284 | usb_kbd_put_queue(data, keycode); |
| 285 | if (scancode < 0x4f && scancode != 0x4a && scancode != 0x4d) |
| 286 | usb_kbd_put_queue(data, '~'); |
| 287 | return 0; |
| 288 | #else |
Heinrich Schuchardt | d05aaef | 2019-11-23 18:15:20 +0100 | [diff] [blame] | 289 | /* Left, Right, Up, Down */ |
| 290 | if (scancode > 0x4e && scancode < 0x53) { |
| 291 | usb_kbd_put_queue(data, 0x1b); |
| 292 | usb_kbd_put_queue(data, '['); |
| 293 | usb_kbd_put_queue(data, usb_special_keys[scancode - 0x4f]); |
| 294 | return 0; |
| 295 | } |
| 296 | return 1; |
Heinrich Schuchardt | 8e505ee | 2019-11-23 18:15:21 +0100 | [diff] [blame] | 297 | #endif /* CONFIG_USB_KEYBOARD_FN_KEYS */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 300 | static uint32_t usb_kbd_service_key(struct usb_device *dev, int i, int up) |
| 301 | { |
| 302 | uint32_t res = 0; |
| 303 | struct usb_kbd_pdata *data = dev->privptr; |
| 304 | uint8_t *new; |
| 305 | uint8_t *old; |
| 306 | |
| 307 | if (up) { |
| 308 | new = data->old; |
| 309 | old = data->new; |
| 310 | } else { |
| 311 | new = data->new; |
| 312 | old = data->old; |
| 313 | } |
| 314 | |
Adrian Cox | 17472dc | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 315 | if ((old[i] > 3) && |
| 316 | (memscan(new + 2, old[i], USB_KBD_BOOT_REPORT_SIZE - 2) == |
| 317 | new + USB_KBD_BOOT_REPORT_SIZE)) { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 318 | res |= usb_kbd_translate(data, old[i], data->new[0], up); |
Adrian Cox | 17472dc | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 319 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 320 | |
| 321 | return res; |
| 322 | } |
| 323 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 324 | /* Interrupt service routine */ |
Marek Vasut | 82cf9a4 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 325 | static int usb_kbd_irq_worker(struct usb_device *dev) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 326 | { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 327 | struct usb_kbd_pdata *data = dev->privptr; |
| 328 | int i, res = 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 329 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 330 | /* No combo key pressed */ |
| 331 | if (data->new[0] == 0x00) |
| 332 | data->flags &= ~USB_KBD_CTRL; |
| 333 | /* Left or Right Ctrl pressed */ |
| 334 | else if ((data->new[0] == LEFT_CNTR) || (data->new[0] == RIGHT_CNTR)) |
| 335 | data->flags |= USB_KBD_CTRL; |
Zhang Wei | 37cc4f0 | 2008-01-03 10:51:15 +0800 | [diff] [blame] | 336 | |
Adrian Cox | 17472dc | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 337 | for (i = 2; i < USB_KBD_BOOT_REPORT_SIZE; i++) { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 338 | res |= usb_kbd_service_key(dev, i, 0); |
| 339 | res |= usb_kbd_service_key(dev, i, 1); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 340 | } |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 341 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 342 | /* Key is still pressed */ |
| 343 | if ((data->new[2] > 3) && (data->old[2] == data->new[2])) |
| 344 | res |= usb_kbd_translate(data, data->new[2], data->new[0], 2); |
| 345 | |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 346 | if (res == 1) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 347 | usb_kbd_setled(dev); |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 348 | |
Adrian Cox | 17472dc | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 349 | memcpy(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE); |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 350 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 351 | return 1; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 354 | /* Keyboard interrupt handler */ |
Marek Vasut | 82cf9a4 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 355 | static int usb_kbd_irq(struct usb_device *dev) |
| 356 | { |
Adrian Cox | 17472dc | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 357 | if ((dev->irq_status != 0) || |
| 358 | (dev->irq_act_len != USB_KBD_BOOT_REPORT_SIZE)) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 359 | debug("USB KBD: Error %lX, len %d\n", |
| 360 | dev->irq_status, dev->irq_act_len); |
Marek Vasut | 82cf9a4 | 2011-09-25 20:00:37 +0100 | [diff] [blame] | 361 | return 1; |
| 362 | } |
| 363 | |
| 364 | return usb_kbd_irq_worker(dev); |
| 365 | } |
| 366 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 367 | /* Interrupt polling */ |
| 368 | static inline void usb_kbd_poll_for_event(struct usb_device *dev) |
| 369 | { |
Hans de Goede | 650d720 | 2015-05-19 22:51:03 +0200 | [diff] [blame] | 370 | #if defined(CONFIG_SYS_USB_EVENT_POLL) |
Hans de Goede | 3c79470 | 2014-09-24 14:06:10 +0200 | [diff] [blame] | 371 | struct usb_kbd_pdata *data = dev->privptr; |
amartin@nvidia.com | 185da95 | 2011-12-20 14:56:16 +0000 | [diff] [blame] | 372 | |
Heinrich Schuchardt | 38ed7fe | 2019-11-23 18:15:18 +0100 | [diff] [blame] | 373 | /* Submit an interrupt transfer request */ |
Michal Suchanek | f5d5af4 | 2019-08-18 10:55:25 +0200 | [diff] [blame] | 374 | if (usb_int_msg(dev, data->intpipe, &data->new[0], |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 375 | data->intpktsize, data->intinterval, true) >= 0) |
Michal Suchanek | 46c83f3 | 2019-08-18 10:55:24 +0200 | [diff] [blame] | 376 | usb_kbd_irq_worker(dev); |
Hans de Goede | 650d720 | 2015-05-19 22:51:03 +0200 | [diff] [blame] | 377 | #elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP) || \ |
| 378 | defined(CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE) |
| 379 | #if defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP) |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 380 | struct usb_interface *iface; |
| 381 | struct usb_kbd_pdata *data = dev->privptr; |
Janne Grunau | 4a7fda9 | 2024-04-04 08:25:53 +0200 | [diff] [blame] | 382 | iface = &dev->config.if_desc[data->ifnum]; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 383 | usb_get_report(dev, iface->desc.bInterfaceNumber, |
Adrian Cox | 17472dc | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 384 | 1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE); |
Hans de Goede | 650d720 | 2015-05-19 22:51:03 +0200 | [diff] [blame] | 385 | if (memcmp(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE)) { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 386 | usb_kbd_irq_worker(dev); |
Hans de Goede | 650d720 | 2015-05-19 22:51:03 +0200 | [diff] [blame] | 387 | #else |
Hans de Goede | 2d02ae7 | 2014-09-24 14:06:11 +0200 | [diff] [blame] | 388 | struct usb_kbd_pdata *data = dev->privptr; |
| 389 | if (poll_int_queue(dev, data->intq)) { |
| 390 | usb_kbd_irq_worker(dev); |
| 391 | /* We've consumed all queued int packets, create new */ |
| 392 | destroy_int_queue(dev, data->intq); |
| 393 | data->intq = create_int_queue(dev, data->intpipe, 1, |
Hans de Goede | bbd2d2a | 2015-01-11 20:38:28 +0100 | [diff] [blame] | 394 | USB_KBD_BOOT_REPORT_SIZE, data->new, |
| 395 | data->intinterval); |
Hans de Goede | 650d720 | 2015-05-19 22:51:03 +0200 | [diff] [blame] | 396 | #endif |
| 397 | data->last_report = get_timer(0); |
| 398 | /* Repeat last usb hid report every REPEAT_RATE ms for keyrepeat */ |
| 399 | } else if (data->last_report != -1 && |
| 400 | get_timer(data->last_report) > REPEAT_RATE) { |
| 401 | usb_kbd_irq_worker(dev); |
| 402 | data->last_report = get_timer(0); |
Hans de Goede | 2d02ae7 | 2014-09-24 14:06:11 +0200 | [diff] [blame] | 403 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 404 | #endif |
| 405 | } |
| 406 | |
| 407 | /* test if a character is in the queue */ |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 408 | static int usb_kbd_testc(struct stdio_dev *sdev) |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 409 | { |
| 410 | struct stdio_dev *dev; |
| 411 | struct usb_device *usb_kbd_dev; |
| 412 | struct usb_kbd_pdata *data; |
| 413 | |
Thomas Watson | d36c85a | 2022-02-10 15:13:14 -0600 | [diff] [blame] | 414 | /* |
| 415 | * Polling the keyboard for an event can take dozens of milliseconds. |
| 416 | * Add a delay between polls to avoid blocking activity which polls |
| 417 | * rapidly, like the UEFI console timer. |
| 418 | */ |
| 419 | unsigned long poll_delay = CONFIG_SYS_HZ / 50; |
| 420 | |
Jim Lin | e2ebec8 | 2013-08-26 20:21:09 +0800 | [diff] [blame] | 421 | #ifdef CONFIG_CMD_NET |
| 422 | /* |
| 423 | * If net_busy_flag is 1, NET transfer is running, |
| 424 | * then we check key-pressed every second (first check may be |
| 425 | * less than 1 second) to improve TFTP booting performance. |
| 426 | */ |
Thomas Watson | d36c85a | 2022-02-10 15:13:14 -0600 | [diff] [blame] | 427 | if (net_busy_flag) |
| 428 | poll_delay = CONFIG_SYS_HZ; |
Jim Lin | e2ebec8 | 2013-08-26 20:21:09 +0800 | [diff] [blame] | 429 | #endif |
Thomas Watson | d36c85a | 2022-02-10 15:13:14 -0600 | [diff] [blame] | 430 | |
| 431 | #ifdef CONFIG_SANDBOX |
| 432 | /* |
| 433 | * Skip delaying polls if a test requests it. |
| 434 | */ |
| 435 | if (state_get_skip_delays()) |
| 436 | poll_delay = 0; |
| 437 | #endif |
| 438 | |
Michal Simek | 2b0634d | 2018-06-27 14:35:07 +0200 | [diff] [blame] | 439 | dev = stdio_get_by_name(sdev->name); |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 440 | usb_kbd_dev = (struct usb_device *)dev->priv; |
| 441 | data = usb_kbd_dev->privptr; |
| 442 | |
Thomas Watson | d36c85a | 2022-02-10 15:13:14 -0600 | [diff] [blame] | 443 | if (get_timer(kbd_testc_tms) >= poll_delay) { |
| 444 | usb_kbd_poll_for_event(usb_kbd_dev); |
| 445 | kbd_testc_tms = get_timer(0); |
| 446 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 447 | |
| 448 | return !(data->usb_in_pointer == data->usb_out_pointer); |
| 449 | } |
| 450 | |
| 451 | /* gets the character from the queue */ |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 452 | static int usb_kbd_getc(struct stdio_dev *sdev) |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 453 | { |
| 454 | struct stdio_dev *dev; |
| 455 | struct usb_device *usb_kbd_dev; |
| 456 | struct usb_kbd_pdata *data; |
| 457 | |
Michal Simek | 2b0634d | 2018-06-27 14:35:07 +0200 | [diff] [blame] | 458 | dev = stdio_get_by_name(sdev->name); |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 459 | usb_kbd_dev = (struct usb_device *)dev->priv; |
| 460 | data = usb_kbd_dev->privptr; |
| 461 | |
Michal Simek | 07b3cc9 | 2018-06-27 14:16:54 +0200 | [diff] [blame] | 462 | while (data->usb_in_pointer == data->usb_out_pointer) { |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 463 | schedule(); |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 464 | usb_kbd_poll_for_event(usb_kbd_dev); |
Michal Simek | 07b3cc9 | 2018-06-27 14:16:54 +0200 | [diff] [blame] | 465 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 466 | |
| 467 | if (data->usb_out_pointer == USB_KBD_BUFFER_LEN - 1) |
| 468 | data->usb_out_pointer = 0; |
| 469 | else |
| 470 | data->usb_out_pointer++; |
| 471 | |
| 472 | return data->usb_kbd_buffer[data->usb_out_pointer]; |
| 473 | } |
| 474 | |
| 475 | /* probes the USB device dev for keyboard type. */ |
Simon Glass | 02f0c28 | 2015-09-08 11:15:11 -0600 | [diff] [blame] | 476 | static int usb_kbd_probe_dev(struct usb_device *dev, unsigned int ifnum) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 477 | { |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 478 | struct usb_interface *iface; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 479 | struct usb_endpoint_descriptor *ep; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 480 | struct usb_kbd_pdata *data; |
Janne Grunau | c838172 | 2024-04-04 08:25:54 +0200 | [diff] [blame] | 481 | unsigned int quirks = 0; |
Stefan Brüns | 6de61c8 | 2021-02-20 17:26:06 +0100 | [diff] [blame] | 482 | int epNum; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 483 | |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 484 | if (dev->descriptor.bNumConfigurations != 1) |
| 485 | return 0; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 486 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 487 | iface = &dev->config.if_desc[ifnum]; |
| 488 | |
Simon Glass | ab88fdb | 2015-11-08 23:48:04 -0700 | [diff] [blame] | 489 | if (iface->desc.bInterfaceClass != USB_CLASS_HID) |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 490 | return 0; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 491 | |
Simon Glass | ab88fdb | 2015-11-08 23:48:04 -0700 | [diff] [blame] | 492 | if (iface->desc.bInterfaceSubClass != USB_SUB_HID_BOOT) |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 493 | return 0; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 494 | |
Simon Glass | ab88fdb | 2015-11-08 23:48:04 -0700 | [diff] [blame] | 495 | if (iface->desc.bInterfaceProtocol != USB_PROT_HID_KEYBOARD) |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 496 | return 0; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 497 | |
Stefan Brüns | 6de61c8 | 2021-02-20 17:26:06 +0100 | [diff] [blame] | 498 | for (epNum = 0; epNum < iface->desc.bNumEndpoints; epNum++) { |
| 499 | ep = &iface->ep_desc[epNum]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 500 | |
Stefan Brüns | 6de61c8 | 2021-02-20 17:26:06 +0100 | [diff] [blame] | 501 | /* Check if endpoint is interrupt IN endpoint */ |
| 502 | if ((ep->bmAttributes & 3) != 3) |
| 503 | continue; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 504 | |
Stefan Brüns | 6de61c8 | 2021-02-20 17:26:06 +0100 | [diff] [blame] | 505 | if (ep->bEndpointAddress & 0x80) |
| 506 | break; |
| 507 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 508 | |
Stefan Brüns | 6de61c8 | 2021-02-20 17:26:06 +0100 | [diff] [blame] | 509 | if (epNum == iface->desc.bNumEndpoints) |
Marek Vasut | b930098 | 2011-10-10 05:34:25 +0000 | [diff] [blame] | 510 | return 0; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 511 | |
Stefan Brüns | 6de61c8 | 2021-02-20 17:26:06 +0100 | [diff] [blame] | 512 | debug("USB KBD: found interrupt EP: 0x%x\n", ep->bEndpointAddress); |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 513 | |
Janne Grunau | c838172 | 2024-04-04 08:25:54 +0200 | [diff] [blame] | 514 | switch (dev->descriptor.idVendor) { |
| 515 | case USB_VENDOR_ID_APPLE: |
| 516 | case USB_VENDOR_ID_KEYCHRON: |
| 517 | quirks |= USB_HID_QUIRK_POLL_NO_REPORT_IDLE; |
| 518 | break; |
| 519 | default: |
| 520 | break; |
| 521 | } |
| 522 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 523 | data = malloc(sizeof(struct usb_kbd_pdata)); |
| 524 | if (!data) { |
| 525 | printf("USB KBD: Error allocating private data\n"); |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | /* Clear private data */ |
| 530 | memset(data, 0, sizeof(struct usb_kbd_pdata)); |
| 531 | |
Allen Martin | ef769ca | 2012-10-24 08:32:04 +0000 | [diff] [blame] | 532 | /* allocate input buffer aligned and sized to USB DMA alignment */ |
Adrian Cox | 17472dc | 2014-04-10 14:02:44 +0100 | [diff] [blame] | 533 | data->new = memalign(USB_DMA_MINALIGN, |
| 534 | roundup(USB_KBD_BOOT_REPORT_SIZE, USB_DMA_MINALIGN)); |
Allen Martin | ef769ca | 2012-10-24 08:32:04 +0000 | [diff] [blame] | 535 | |
Janne Grunau | 4a7fda9 | 2024-04-04 08:25:53 +0200 | [diff] [blame] | 536 | data->ifnum = ifnum; |
| 537 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 538 | /* Insert private data into USB device structure */ |
| 539 | dev->privptr = data; |
| 540 | |
| 541 | /* Set IRQ handler */ |
| 542 | dev->irq_handle = usb_kbd_irq; |
| 543 | |
Hans de Goede | 3c79470 | 2014-09-24 14:06:10 +0200 | [diff] [blame] | 544 | data->intpipe = usb_rcvintpipe(dev, ep->bEndpointAddress); |
| 545 | data->intpktsize = min(usb_maxpacket(dev, data->intpipe), |
| 546 | USB_KBD_BOOT_REPORT_SIZE); |
| 547 | data->intinterval = ep->bInterval; |
Hans de Goede | 650d720 | 2015-05-19 22:51:03 +0200 | [diff] [blame] | 548 | data->last_report = -1; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 549 | |
| 550 | /* We found a USB Keyboard, install it. */ |
Stefan Brüns | 6de61c8 | 2021-02-20 17:26:06 +0100 | [diff] [blame] | 551 | debug("USB KBD: set boot protocol\n"); |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 552 | usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0); |
| 553 | |
Hans de Goede | 650d720 | 2015-05-19 22:51:03 +0200 | [diff] [blame] | 554 | #if !defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP) && \ |
| 555 | !defined(CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE) |
Stefan Brüns | 6de61c8 | 2021-02-20 17:26:06 +0100 | [diff] [blame] | 556 | debug("USB KBD: set idle interval...\n"); |
Hans de Goede | 650d720 | 2015-05-19 22:51:03 +0200 | [diff] [blame] | 557 | usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE / 4, 0); |
Hans de Goede | d4e99a6 | 2015-06-18 22:34:34 +0200 | [diff] [blame] | 558 | #else |
Stefan Brüns | 6de61c8 | 2021-02-20 17:26:06 +0100 | [diff] [blame] | 559 | debug("USB KBD: set idle interval=0...\n"); |
Hans de Goede | d4e99a6 | 2015-06-18 22:34:34 +0200 | [diff] [blame] | 560 | usb_set_idle(dev, iface->desc.bInterfaceNumber, 0, 0); |
Hans de Goede | 650d720 | 2015-05-19 22:51:03 +0200 | [diff] [blame] | 561 | #endif |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 562 | |
Janne Grunau | c838172 | 2024-04-04 08:25:54 +0200 | [diff] [blame] | 563 | /* |
| 564 | * Apple and Keychron keyboards do not report the device state. Reports |
| 565 | * are only returned during key presses. |
| 566 | */ |
| 567 | if (quirks & USB_HID_QUIRK_POLL_NO_REPORT_IDLE) { |
| 568 | debug("USB KBD: quirk: skip testing device state\n"); |
| 569 | return 1; |
| 570 | } |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 571 | debug("USB KBD: enable interrupt pipe...\n"); |
Hans de Goede | 2d02ae7 | 2014-09-24 14:06:11 +0200 | [diff] [blame] | 572 | #ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE |
| 573 | data->intq = create_int_queue(dev, data->intpipe, 1, |
Hans de Goede | bbd2d2a | 2015-01-11 20:38:28 +0100 | [diff] [blame] | 574 | USB_KBD_BOOT_REPORT_SIZE, data->new, |
| 575 | data->intinterval); |
Hans de Goede | 2d02ae7 | 2014-09-24 14:06:11 +0200 | [diff] [blame] | 576 | if (!data->intq) { |
Stephen Warren | 1fd879e | 2015-11-13 13:34:09 -0700 | [diff] [blame] | 577 | #elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP) |
| 578 | if (usb_get_report(dev, iface->desc.bInterfaceNumber, |
| 579 | 1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE) < 0) { |
Hans de Goede | 2d02ae7 | 2014-09-24 14:06:11 +0200 | [diff] [blame] | 580 | #else |
Michal Suchanek | f5d5af4 | 2019-08-18 10:55:25 +0200 | [diff] [blame] | 581 | if (usb_int_msg(dev, data->intpipe, data->new, data->intpktsize, |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 582 | data->intinterval, false) < 0) { |
Hans de Goede | 2d02ae7 | 2014-09-24 14:06:11 +0200 | [diff] [blame] | 583 | #endif |
Vincent Palatin | 0eab998 | 2013-05-10 19:48:59 -0700 | [diff] [blame] | 584 | printf("Failed to get keyboard state from device %04x:%04x\n", |
| 585 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
| 586 | /* Abort, we don't want to use that non-functional keyboard. */ |
| 587 | return 0; |
| 588 | } |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 589 | |
| 590 | /* Success. */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 591 | return 1; |
| 592 | } |
| 593 | |
Simon Glass | fcdc0c3 | 2015-03-25 12:22:33 -0600 | [diff] [blame] | 594 | static int probe_usb_keyboard(struct usb_device *dev) |
| 595 | { |
| 596 | char *stdinname; |
| 597 | struct stdio_dev usb_kbd_dev; |
Janne Grunau | 4a7fda9 | 2024-04-04 08:25:53 +0200 | [diff] [blame] | 598 | unsigned int ifnum; |
| 599 | unsigned int max_ifnum = min((unsigned int)USB_MAX_ACTIVE_INTERFACES, |
| 600 | (unsigned int)dev->config.no_of_if); |
Simon Glass | fcdc0c3 | 2015-03-25 12:22:33 -0600 | [diff] [blame] | 601 | int error; |
| 602 | |
| 603 | /* Try probing the keyboard */ |
Janne Grunau | 4a7fda9 | 2024-04-04 08:25:53 +0200 | [diff] [blame] | 604 | for (ifnum = 0; ifnum < max_ifnum; ifnum++) { |
| 605 | if (usb_kbd_probe_dev(dev, ifnum) == 1) |
| 606 | break; |
| 607 | } |
| 608 | if (ifnum >= max_ifnum) |
Simon Glass | fcdc0c3 | 2015-03-25 12:22:33 -0600 | [diff] [blame] | 609 | return -ENOENT; |
| 610 | |
| 611 | /* Register the keyboard */ |
| 612 | debug("USB KBD: register.\n"); |
| 613 | memset(&usb_kbd_dev, 0, sizeof(struct stdio_dev)); |
| 614 | strcpy(usb_kbd_dev.name, DEVNAME); |
Bin Meng | 6abe4b6 | 2015-11-03 23:23:37 -0800 | [diff] [blame] | 615 | usb_kbd_dev.flags = DEV_FLAGS_INPUT; |
Simon Glass | fcdc0c3 | 2015-03-25 12:22:33 -0600 | [diff] [blame] | 616 | usb_kbd_dev.getc = usb_kbd_getc; |
| 617 | usb_kbd_dev.tstc = usb_kbd_testc; |
| 618 | usb_kbd_dev.priv = (void *)dev; |
| 619 | error = stdio_register(&usb_kbd_dev); |
| 620 | if (error) |
| 621 | return error; |
| 622 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 623 | stdinname = env_get("stdin"); |
Simon Glass | dfc2535 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 624 | #if CONFIG_IS_ENABLED(CONSOLE_MUX) |
Kory Maincent | 70ffe4e | 2022-06-22 10:59:31 +0200 | [diff] [blame] | 625 | if (strstr(stdinname, DEVNAME) != NULL) { |
| 626 | error = iomux_doenv(stdin, stdinname); |
| 627 | if (error) |
| 628 | return error; |
| 629 | } |
Simon Glass | fcdc0c3 | 2015-03-25 12:22:33 -0600 | [diff] [blame] | 630 | #else |
| 631 | /* Check if this is the standard input device. */ |
Kory Maincent | 70ffe4e | 2022-06-22 10:59:31 +0200 | [diff] [blame] | 632 | if (!strcmp(stdinname, DEVNAME)) { |
| 633 | /* Reassign the console */ |
| 634 | if (overwrite_console()) |
| 635 | return 1; |
Simon Glass | fcdc0c3 | 2015-03-25 12:22:33 -0600 | [diff] [blame] | 636 | |
Kory Maincent | 70ffe4e | 2022-06-22 10:59:31 +0200 | [diff] [blame] | 637 | error = console_assign(stdin, DEVNAME); |
| 638 | if (error) |
| 639 | return error; |
| 640 | } |
Simon Glass | fcdc0c3 | 2015-03-25 12:22:33 -0600 | [diff] [blame] | 641 | #endif |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 646 | #if !CONFIG_IS_ENABLED(DM_USB) |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 647 | /* Search for keyboard and register it if found. */ |
| 648 | int drv_usb_kbd_init(void) |
| 649 | { |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 650 | int error, i; |
| 651 | |
Simon Glass | 5f1296b | 2015-03-25 12:22:34 -0600 | [diff] [blame] | 652 | debug("%s: Probing for keyboard\n", __func__); |
Simon Glass | 5f1296b | 2015-03-25 12:22:34 -0600 | [diff] [blame] | 653 | /* Scan all USB Devices */ |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 654 | for (i = 0; i < USB_MAX_DEVICE; i++) { |
Simon Glass | fcdc0c3 | 2015-03-25 12:22:33 -0600 | [diff] [blame] | 655 | struct usb_device *dev; |
| 656 | |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 657 | /* Get USB device. */ |
| 658 | dev = usb_get_dev_index(i); |
| 659 | if (!dev) |
Simon Glass | fcdc0c3 | 2015-03-25 12:22:33 -0600 | [diff] [blame] | 660 | break; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 661 | |
| 662 | if (dev->devnum == -1) |
| 663 | continue; |
| 664 | |
Simon Glass | fcdc0c3 | 2015-03-25 12:22:33 -0600 | [diff] [blame] | 665 | error = probe_usb_keyboard(dev); |
| 666 | if (!error) |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 667 | return 1; |
Simon Glass | fcdc0c3 | 2015-03-25 12:22:33 -0600 | [diff] [blame] | 668 | if (error && error != -ENOENT) |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 669 | return error; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* No USB Keyboard found */ |
| 673 | return -1; |
| 674 | } |
| 675 | |
| 676 | /* Deregister the keyboard. */ |
Hans de Goede | 3e51df0 | 2014-09-20 16:54:38 +0200 | [diff] [blame] | 677 | int usb_kbd_deregister(int force) |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 678 | { |
Simon Glass | 3110ece | 2016-10-17 20:13:02 -0600 | [diff] [blame] | 679 | #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) |
Hans de Goede | 55a5451 | 2014-09-24 14:06:08 +0200 | [diff] [blame] | 680 | struct stdio_dev *dev; |
| 681 | struct usb_device *usb_kbd_dev; |
| 682 | struct usb_kbd_pdata *data; |
| 683 | |
| 684 | dev = stdio_get_by_name(DEVNAME); |
| 685 | if (dev) { |
| 686 | usb_kbd_dev = (struct usb_device *)dev->priv; |
| 687 | data = usb_kbd_dev->privptr; |
Simon Glass | dfc2535 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 688 | #if CONFIG_IS_ENABLED(CONSOLE_MUX) |
Andy Shevchenko | 05a17e8 | 2021-02-11 17:09:44 +0200 | [diff] [blame] | 689 | if (iomux_replace_device(stdin, DEVNAME, force ? "nulldev" : "")) |
Hans de Goede | e8790a2 | 2015-01-11 20:34:44 +0100 | [diff] [blame] | 690 | return 1; |
| 691 | #endif |
Andy Shevchenko | 05a17e8 | 2021-02-11 17:09:44 +0200 | [diff] [blame] | 692 | if (stdio_deregister_dev(dev, force) != 0) |
| 693 | return 1; |
Hans de Goede | 2d02ae7 | 2014-09-24 14:06:11 +0200 | [diff] [blame] | 694 | #ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE |
| 695 | destroy_int_queue(usb_kbd_dev, data->intq); |
| 696 | #endif |
Hans de Goede | 55a5451 | 2014-09-24 14:06:08 +0200 | [diff] [blame] | 697 | free(data->new); |
| 698 | free(data); |
| 699 | } |
Hans de Goede | f565623 | 2014-09-20 16:54:34 +0200 | [diff] [blame] | 700 | |
| 701 | return 0; |
Marek Vasut | 7534a3e | 2011-10-10 16:34:26 +0100 | [diff] [blame] | 702 | #else |
| 703 | return 1; |
| 704 | #endif |
| 705 | } |
Simon Glass | 02f0c28 | 2015-09-08 11:15:11 -0600 | [diff] [blame] | 706 | |
Hans de Goede | a88d437 | 2016-04-03 09:18:53 +0200 | [diff] [blame] | 707 | #endif |
| 708 | |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 709 | #if CONFIG_IS_ENABLED(DM_USB) |
Simon Glass | 02f0c28 | 2015-09-08 11:15:11 -0600 | [diff] [blame] | 710 | |
| 711 | static int usb_kbd_probe(struct udevice *dev) |
| 712 | { |
| 713 | struct usb_device *udev = dev_get_parent_priv(dev); |
Simon Glass | 02f0c28 | 2015-09-08 11:15:11 -0600 | [diff] [blame] | 714 | |
Masahiro Yamada | 9b70df5 | 2016-09-06 22:17:35 +0900 | [diff] [blame] | 715 | return probe_usb_keyboard(udev); |
Simon Glass | 02f0c28 | 2015-09-08 11:15:11 -0600 | [diff] [blame] | 716 | } |
| 717 | |
Simon Glass | 3c3ba90 | 2016-01-03 13:50:31 -0700 | [diff] [blame] | 718 | static int usb_kbd_remove(struct udevice *dev) |
| 719 | { |
| 720 | struct usb_device *udev = dev_get_parent_priv(dev); |
| 721 | struct usb_kbd_pdata *data; |
| 722 | struct stdio_dev *sdev; |
| 723 | int ret; |
| 724 | |
| 725 | sdev = stdio_get_by_name(DEVNAME); |
| 726 | if (!sdev) { |
| 727 | ret = -ENXIO; |
| 728 | goto err; |
| 729 | } |
| 730 | data = udev->privptr; |
Simon Glass | dfc2535 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 731 | #if CONFIG_IS_ENABLED(CONSOLE_MUX) |
Andy Shevchenko | 05a17e8 | 2021-02-11 17:09:44 +0200 | [diff] [blame] | 732 | if (iomux_replace_device(stdin, DEVNAME, "nulldev")) { |
Simon Glass | 3c3ba90 | 2016-01-03 13:50:31 -0700 | [diff] [blame] | 733 | ret = -ENOLINK; |
| 734 | goto err; |
| 735 | } |
| 736 | #endif |
Andy Shevchenko | 05a17e8 | 2021-02-11 17:09:44 +0200 | [diff] [blame] | 737 | if (stdio_deregister_dev(sdev, true)) { |
| 738 | ret = -EPERM; |
| 739 | goto err; |
| 740 | } |
Simon Glass | 3c3ba90 | 2016-01-03 13:50:31 -0700 | [diff] [blame] | 741 | #ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE |
| 742 | destroy_int_queue(udev, data->intq); |
| 743 | #endif |
| 744 | free(data->new); |
| 745 | free(data); |
| 746 | |
| 747 | return 0; |
| 748 | err: |
| 749 | printf("%s: warning, ret=%d", __func__, ret); |
| 750 | return ret; |
| 751 | } |
| 752 | |
Simon Glass | 02f0c28 | 2015-09-08 11:15:11 -0600 | [diff] [blame] | 753 | static const struct udevice_id usb_kbd_ids[] = { |
| 754 | { .compatible = "usb-keyboard" }, |
| 755 | { } |
| 756 | }; |
| 757 | |
| 758 | U_BOOT_DRIVER(usb_kbd) = { |
| 759 | .name = "usb_kbd", |
| 760 | .id = UCLASS_KEYBOARD, |
| 761 | .of_match = usb_kbd_ids, |
| 762 | .probe = usb_kbd_probe, |
Simon Glass | 3c3ba90 | 2016-01-03 13:50:31 -0700 | [diff] [blame] | 763 | .remove = usb_kbd_remove, |
Simon Glass | 02f0c28 | 2015-09-08 11:15:11 -0600 | [diff] [blame] | 764 | }; |
| 765 | |
Simon Glass | 02f0c28 | 2015-09-08 11:15:11 -0600 | [diff] [blame] | 766 | static const struct usb_device_id kbd_id_table[] = { |
| 767 | { |
| 768 | .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS | |
| 769 | USB_DEVICE_ID_MATCH_INT_SUBCLASS | |
| 770 | USB_DEVICE_ID_MATCH_INT_PROTOCOL, |
| 771 | .bInterfaceClass = USB_CLASS_HID, |
Simon Glass | ab88fdb | 2015-11-08 23:48:04 -0700 | [diff] [blame] | 772 | .bInterfaceSubClass = USB_SUB_HID_BOOT, |
| 773 | .bInterfaceProtocol = USB_PROT_HID_KEYBOARD, |
Simon Glass | 02f0c28 | 2015-09-08 11:15:11 -0600 | [diff] [blame] | 774 | }, |
Janne Grunau | 4a7fda9 | 2024-04-04 08:25:53 +0200 | [diff] [blame] | 775 | { |
| 776 | USB_DEVICE(USB_VENDOR_ID_APPLE, |
| 777 | USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021), |
| 778 | }, |
| 779 | { |
| 780 | USB_DEVICE(USB_VENDOR_ID_APPLE, |
| 781 | USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021), |
| 782 | }, |
| 783 | { |
| 784 | USB_DEVICE(USB_VENDOR_ID_APPLE, |
| 785 | USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021), |
| 786 | }, |
Simon Glass | 02f0c28 | 2015-09-08 11:15:11 -0600 | [diff] [blame] | 787 | { } /* Terminating entry */ |
| 788 | }; |
| 789 | |
| 790 | U_BOOT_USB_DEVICE(usb_kbd, kbd_id_table); |
| 791 | |
| 792 | #endif |