blob: bbfee23bc2698d989e3d49efcc0f452f502e6a07 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00002/*
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.
wdenkaffae2b2002-08-17 09:36:01 +00008 */
Simon Glassa73bda42015-11-08 23:47:45 -07009#include <console.h>
Simon Glass5f1296b2015-03-25 12:22:34 -060010#include <dm.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060011#include <env.h>
Hans de Goedef5656232014-09-20 16:54:34 +020012#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Marek Vasut7534a3e2011-10-10 16:34:26 +010014#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060015#include <memalign.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020016#include <stdio_dev.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060017#include <time.h>
Michal Simek07b3cc92018-06-27 14:16:54 +020018#include <watchdog.h>
Christian Eggers4e0e8d02008-05-21 22:12:00 +020019#include <asm/byteorder.h>
Thomas Watsond36c85a2022-02-10 15:13:14 -060020#ifdef CONFIG_SANDBOX
21#include <asm/state.h>
22#endif
wdenkaffae2b2002-08-17 09:36:01 +000023
wdenkaffae2b2002-08-17 09:36:01 +000024#include <usb.h>
25
wdenkaffae2b2002-08-17 09:36:01 +000026/*
Janne Grunau4a7fda92024-04-04 08:25:53 +020027 * 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 Grunauc8381722024-04-04 08:25:54 +020034#define USB_VENDOR_ID_KEYCHRON 0x3434
35
36#define USB_HID_QUIRK_POLL_NO_REPORT_IDLE BIT(0)
37
Janne Grunau4a7fda92024-04-04 08:25:53 +020038/*
Marek Vasutb9300982011-10-10 05:34:25 +000039 * If overwrite_console returns 1, the stdin, stderr and stdout
wdenkaffae2b2002-08-17 09:36:01 +000040 * are switched to the serial port, else the settings in the
41 * environment are used
42 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020043#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
Marek Vasutb9300982011-10-10 05:34:25 +000044extern int overwrite_console(void);
wdenkaffae2b2002-08-17 09:36:01 +000045#else
Marek Vasutb9300982011-10-10 05:34:25 +000046int overwrite_console(void)
wdenkaffae2b2002-08-17 09:36:01 +000047{
Marek Vasutb9300982011-10-10 05:34:25 +000048 return 0;
wdenkaffae2b2002-08-17 09:36:01 +000049}
50#endif
51
Marek Vasut7534a3e2011-10-10 16:34:26 +010052/* Keyboard sampling rate */
Hans de Goede650d7202015-05-19 22:51:03 +020053#define REPEAT_RATE 40 /* 40msec -> 25cps */
Marek Vasut7534a3e2011-10-10 16:34:26 +010054#define REPEAT_DELAY 10 /* 10 x REPEAT_RATE = 400msec */
wdenkaffae2b2002-08-17 09:36:01 +000055
56#define NUM_LOCK 0x53
Marek Vasutb9300982011-10-10 05:34:25 +000057#define CAPS_LOCK 0x39
58#define SCROLL_LOCK 0x47
wdenkaffae2b2002-08-17 09:36:01 +000059
wdenkaffae2b2002-08-17 09:36:01 +000060/* Modifier bits */
Marek Vasut7534a3e2011-10-10 16:34:26 +010061#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)
wdenkaffae2b2002-08-17 09:36:01 +000069
Marek Vasut7534a3e2011-10-10 16:34:26 +010070/* Size of the keyboard buffer */
71#define USB_KBD_BUFFER_LEN 0x20
wdenkaffae2b2002-08-17 09:36:01 +000072
Marek Vasut7534a3e2011-10-10 16:34:26 +010073/* Device name */
Marek Vasutb9300982011-10-10 05:34:25 +000074#define DEVNAME "usbkbd"
wdenkaffae2b2002-08-17 09:36:01 +000075
Marek Vasut7534a3e2011-10-10 16:34:26 +010076/* Keyboard maps */
77static const unsigned char usb_kbd_numkey[] = {
Marek Vasutb9300982011-10-10 05:34:25 +000078 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
79 '\r', 0x1b, '\b', '\t', ' ', '-', '=', '[', ']',
80 '\\', '#', ';', '\'', '`', ',', '.', '/'
wdenkaffae2b2002-08-17 09:36:01 +000081};
Marek Vasut7534a3e2011-10-10 16:34:26 +010082static const unsigned char usb_kbd_numkey_shifted[] = {
Marek Vasutb9300982011-10-10 05:34:25 +000083 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
84 '\r', 0x1b, '\b', '\t', ' ', '_', '+', '{', '}',
85 '|', '~', ':', '"', '~', '<', '>', '?'
wdenkaffae2b2002-08-17 09:36:01 +000086};
87
Vincent Palatin92f6a622012-01-09 12:59:36 +000088static 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 Schuchardtd05aaef2019-11-23 18:15:20 +010094static const u8 usb_special_keys[] = {
Heinrich Schuchardt8e505ee2019-11-23 18:15:21 +010095#ifdef CONFIG_USB_KEYBOARD_FN_KEYS
96 '2', 'H', '5', '3', 'F', '6', 'C', 'D', 'B', 'A'
97#else
Heinrich Schuchardtd05aaef2019-11-23 18:15:20 +010098 'C', 'D', 'B', 'A'
Heinrich Schuchardt8e505ee2019-11-23 18:15:21 +010099#endif
Allen Martin1bfb4cd2012-11-06 13:26:03 -0800100};
101
102/*
Marek Vasut7534a3e2011-10-10 16:34:26 +0100103 * 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 Vasut82cf9a42011-09-25 20:00:37 +0100110
Marek Vasut7534a3e2011-10-10 16:34:26 +0100111#define USB_KBD_LEDMASK \
112 (USB_KBD_NUMLOCK | USB_KBD_CAPSLOCK | USB_KBD_SCROLLLOCK)
Marek Vasut82cf9a42011-09-25 20:00:37 +0100113
Marek Vasut7534a3e2011-10-10 16:34:26 +0100114struct usb_kbd_pdata {
Hans de Goede3c794702014-09-24 14:06:10 +0200115 unsigned long intpipe;
116 int intpktsize;
117 int intinterval;
Hans de Goede650d7202015-05-19 22:51:03 +0200118 unsigned long last_report;
Hans de Goede2d02ae72014-09-24 14:06:11 +0200119 struct int_queue *intq;
Hans de Goede3c794702014-09-24 14:06:10 +0200120
Janne Grunau4a7fda92024-04-04 08:25:53 +0200121 uint32_t ifnum;
122
Marek Vasut7534a3e2011-10-10 16:34:26 +0100123 uint32_t repeat_delay;
wdenkaffae2b2002-08-17 09:36:01 +0000124
Marek Vasut7534a3e2011-10-10 16:34:26 +0100125 uint32_t usb_in_pointer;
126 uint32_t usb_out_pointer;
127 uint8_t usb_kbd_buffer[USB_KBD_BUFFER_LEN];
Marek Vasut82cf9a42011-09-25 20:00:37 +0100128
Allen Martinef769ca2012-10-24 08:32:04 +0000129 uint8_t *new;
Adrian Cox17472dc2014-04-10 14:02:44 +0100130 uint8_t old[USB_KBD_BOOT_REPORT_SIZE];
Marek Vasut82cf9a42011-09-25 20:00:37 +0100131
Marek Vasut7534a3e2011-10-10 16:34:26 +0100132 uint8_t flags;
133};
Marek Vasut82cf9a42011-09-25 20:00:37 +0100134
Jim Linc562f972013-08-13 19:04:22 +0800135extern int __maybe_unused net_busy_flag;
136
137/* The period of time between two calls of usb_kbd_testc(). */
Thomas Watsond36c85a2022-02-10 15:13:14 -0600138static unsigned long kbd_testc_tms;
Jim Linc562f972013-08-13 19:04:22 +0800139
Simon Glass1880b8a2024-09-01 16:26:20 -0600140int usb_kbd_remove_for_test(void)
141{
142 return console_remove_by_name(DEVNAME);
143}
144
Marek Vasut7534a3e2011-10-10 16:34:26 +0100145/* Puts character in the queue and sets up the in and out pointer. */
Heinrich Schuchardt90d746f2019-11-23 18:15:19 +0100146static void usb_kbd_put_queue(struct usb_kbd_pdata *data, u8 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_kbd_pdata *data = dev->privptr;
Janne Grunau4a7fda92024-04-04 08:25:53 +0200173 struct usb_interface *iface = &dev->config.if_desc[data->ifnum];
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
Marek Vasut0082cd12016-01-25 22:00:44 +0100219 if ((scancode > 0x1d) && (scancode < 0x39)) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100220 /* 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
Vincent Palatin92f6a622012-01-09 12:59:36 +0000227 /* Numeric keypad */
228 if ((scancode >= 0x54) && (scancode <= 0x67))
229 keycode = usb_kbd_num_keypad[scancode - 0x54];
230
Marek Vasut7534a3e2011-10-10 16:34:26 +0100231 if (data->flags & USB_KBD_CTRL)
Zhang Wei37cc4f02008-01-03 10:51:15 +0800232 keycode = scancode - 0x3;
233
Marek Vasutb9300982011-10-10 05:34:25 +0000234 if (pressed == 1) {
235 if (scancode == NUM_LOCK) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100236 data->flags ^= USB_KBD_NUMLOCK;
wdenkaffae2b2002-08-17 09:36:01 +0000237 return 1;
238 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100239
Marek Vasutb9300982011-10-10 05:34:25 +0000240 if (scancode == CAPS_LOCK) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100241 data->flags ^= USB_KBD_CAPSLOCK;
wdenkaffae2b2002-08-17 09:36:01 +0000242 return 1;
243 }
Marek Vasutb9300982011-10-10 05:34:25 +0000244 if (scancode == SCROLL_LOCK) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100245 data->flags ^= USB_KBD_SCROLLLOCK;
wdenkaffae2b2002-08-17 09:36:01 +0000246 return 1;
247 }
248 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100249
250 /* Report keycode if any */
Heinrich Schuchardtd05aaef2019-11-23 18:15:20 +0100251 if (keycode) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530252 debug("%c", keycode);
Marek Vasut7534a3e2011-10-10 16:34:26 +0100253 usb_kbd_put_queue(data, keycode);
Heinrich Schuchardtd05aaef2019-11-23 18:15:20 +0100254 return 0;
wdenkaffae2b2002-08-17 09:36:01 +0000255 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100256
Heinrich Schuchardt8e505ee2019-11-23 18:15:21 +0100257#ifdef CONFIG_USB_KEYBOARD_FN_KEYS
258 if (scancode < 0x3a || scancode > 0x52 ||
259 scancode == 0x46 || scancode == 0x47)
260 return 1;
261
262 usb_kbd_put_queue(data, 0x1b);
263 if (scancode < 0x3e) {
264 /* F1 - F4 */
265 usb_kbd_put_queue(data, 0x4f);
266 usb_kbd_put_queue(data, scancode - 0x3a + 'P');
267 return 0;
268 }
269 usb_kbd_put_queue(data, '[');
270 if (scancode < 0x42) {
271 /* F5 - F8 */
272 usb_kbd_put_queue(data, '1');
273 if (scancode == 0x3e)
274 --scancode;
275 keycode = scancode - 0x3f + '7';
276 } else if (scancode < 0x49) {
277 /* F9 - F12 */
278 usb_kbd_put_queue(data, '2');
279 if (scancode > 0x43)
280 ++scancode;
281 keycode = scancode - 0x42 + '0';
282 } else {
283 /*
284 * INSERT, HOME, PAGE UP, DELETE, END, PAGE DOWN,
285 * RIGHT, LEFT, DOWN, UP
286 */
287 keycode = usb_special_keys[scancode - 0x49];
288 }
289 usb_kbd_put_queue(data, keycode);
290 if (scancode < 0x4f && scancode != 0x4a && scancode != 0x4d)
291 usb_kbd_put_queue(data, '~');
292 return 0;
293#else
Heinrich Schuchardtd05aaef2019-11-23 18:15:20 +0100294 /* Left, Right, Up, Down */
295 if (scancode > 0x4e && scancode < 0x53) {
296 usb_kbd_put_queue(data, 0x1b);
297 usb_kbd_put_queue(data, '[');
298 usb_kbd_put_queue(data, usb_special_keys[scancode - 0x4f]);
299 return 0;
300 }
301 return 1;
Heinrich Schuchardt8e505ee2019-11-23 18:15:21 +0100302#endif /* CONFIG_USB_KEYBOARD_FN_KEYS */
wdenkaffae2b2002-08-17 09:36:01 +0000303}
304
Marek Vasut7534a3e2011-10-10 16:34:26 +0100305static uint32_t usb_kbd_service_key(struct usb_device *dev, int i, int up)
306{
307 uint32_t res = 0;
308 struct usb_kbd_pdata *data = dev->privptr;
309 uint8_t *new;
310 uint8_t *old;
311
312 if (up) {
313 new = data->old;
314 old = data->new;
315 } else {
316 new = data->new;
317 old = data->old;
318 }
319
Adrian Cox17472dc2014-04-10 14:02:44 +0100320 if ((old[i] > 3) &&
321 (memscan(new + 2, old[i], USB_KBD_BOOT_REPORT_SIZE - 2) ==
322 new + USB_KBD_BOOT_REPORT_SIZE)) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100323 res |= usb_kbd_translate(data, old[i], data->new[0], up);
Adrian Cox17472dc2014-04-10 14:02:44 +0100324 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100325
326 return res;
327}
328
wdenkaffae2b2002-08-17 09:36:01 +0000329/* Interrupt service routine */
Marek Vasut82cf9a42011-09-25 20:00:37 +0100330static int usb_kbd_irq_worker(struct usb_device *dev)
wdenkaffae2b2002-08-17 09:36:01 +0000331{
Marek Vasut7534a3e2011-10-10 16:34:26 +0100332 struct usb_kbd_pdata *data = dev->privptr;
333 int i, res = 0;
wdenkaffae2b2002-08-17 09:36:01 +0000334
Marek Vasut7534a3e2011-10-10 16:34:26 +0100335 /* No combo key pressed */
336 if (data->new[0] == 0x00)
337 data->flags &= ~USB_KBD_CTRL;
338 /* Left or Right Ctrl pressed */
339 else if ((data->new[0] == LEFT_CNTR) || (data->new[0] == RIGHT_CNTR))
340 data->flags |= USB_KBD_CTRL;
Zhang Wei37cc4f02008-01-03 10:51:15 +0800341
Adrian Cox17472dc2014-04-10 14:02:44 +0100342 for (i = 2; i < USB_KBD_BOOT_REPORT_SIZE; i++) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100343 res |= usb_kbd_service_key(dev, i, 0);
344 res |= usb_kbd_service_key(dev, i, 1);
wdenkaffae2b2002-08-17 09:36:01 +0000345 }
Marek Vasutb9300982011-10-10 05:34:25 +0000346
Marek Vasut7534a3e2011-10-10 16:34:26 +0100347 /* Key is still pressed */
348 if ((data->new[2] > 3) && (data->old[2] == data->new[2]))
349 res |= usb_kbd_translate(data, data->new[2], data->new[0], 2);
350
Marek Vasutb9300982011-10-10 05:34:25 +0000351 if (res == 1)
wdenkaffae2b2002-08-17 09:36:01 +0000352 usb_kbd_setled(dev);
Marek Vasutb9300982011-10-10 05:34:25 +0000353
Adrian Cox17472dc2014-04-10 14:02:44 +0100354 memcpy(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE);
Marek Vasutb9300982011-10-10 05:34:25 +0000355
Marek Vasut7534a3e2011-10-10 16:34:26 +0100356 return 1;
wdenkaffae2b2002-08-17 09:36:01 +0000357}
358
Marek Vasut7534a3e2011-10-10 16:34:26 +0100359/* Keyboard interrupt handler */
Marek Vasut82cf9a42011-09-25 20:00:37 +0100360static int usb_kbd_irq(struct usb_device *dev)
361{
Adrian Cox17472dc2014-04-10 14:02:44 +0100362 if ((dev->irq_status != 0) ||
363 (dev->irq_act_len != USB_KBD_BOOT_REPORT_SIZE)) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530364 debug("USB KBD: Error %lX, len %d\n",
365 dev->irq_status, dev->irq_act_len);
Marek Vasut82cf9a42011-09-25 20:00:37 +0100366 return 1;
367 }
368
369 return usb_kbd_irq_worker(dev);
370}
371
Marek Vasut7534a3e2011-10-10 16:34:26 +0100372/* Interrupt polling */
373static inline void usb_kbd_poll_for_event(struct usb_device *dev)
374{
Hans de Goede650d7202015-05-19 22:51:03 +0200375#if defined(CONFIG_SYS_USB_EVENT_POLL)
Hans de Goede3c794702014-09-24 14:06:10 +0200376 struct usb_kbd_pdata *data = dev->privptr;
amartin@nvidia.com185da952011-12-20 14:56:16 +0000377
Heinrich Schuchardt38ed7fe2019-11-23 18:15:18 +0100378 /* Submit an interrupt transfer request */
Michal Suchanekf5d5af42019-08-18 10:55:25 +0200379 if (usb_int_msg(dev, data->intpipe, &data->new[0],
Michal Suchanek1c95b9f2019-08-18 10:55:27 +0200380 data->intpktsize, data->intinterval, true) >= 0)
Michal Suchanek46c83f32019-08-18 10:55:24 +0200381 usb_kbd_irq_worker(dev);
Hans de Goede650d7202015-05-19 22:51:03 +0200382#elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP) || \
383 defined(CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE)
384#if defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
Marek Vasut7534a3e2011-10-10 16:34:26 +0100385 struct usb_interface *iface;
386 struct usb_kbd_pdata *data = dev->privptr;
Janne Grunau4a7fda92024-04-04 08:25:53 +0200387 iface = &dev->config.if_desc[data->ifnum];
Marek Vasut7534a3e2011-10-10 16:34:26 +0100388 usb_get_report(dev, iface->desc.bInterfaceNumber,
Adrian Cox17472dc2014-04-10 14:02:44 +0100389 1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE);
Hans de Goede650d7202015-05-19 22:51:03 +0200390 if (memcmp(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE)) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100391 usb_kbd_irq_worker(dev);
Hans de Goede650d7202015-05-19 22:51:03 +0200392#else
Hans de Goede2d02ae72014-09-24 14:06:11 +0200393 struct usb_kbd_pdata *data = dev->privptr;
394 if (poll_int_queue(dev, data->intq)) {
395 usb_kbd_irq_worker(dev);
396 /* We've consumed all queued int packets, create new */
397 destroy_int_queue(dev, data->intq);
398 data->intq = create_int_queue(dev, data->intpipe, 1,
Hans de Goedebbd2d2a2015-01-11 20:38:28 +0100399 USB_KBD_BOOT_REPORT_SIZE, data->new,
400 data->intinterval);
Hans de Goede650d7202015-05-19 22:51:03 +0200401#endif
402 data->last_report = get_timer(0);
403 /* Repeat last usb hid report every REPEAT_RATE ms for keyrepeat */
404 } else if (data->last_report != -1 &&
405 get_timer(data->last_report) > REPEAT_RATE) {
406 usb_kbd_irq_worker(dev);
407 data->last_report = get_timer(0);
Hans de Goede2d02ae72014-09-24 14:06:11 +0200408 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100409#endif
410}
411
412/* test if a character is in the queue */
Simon Glass0d1e1f72014-07-23 06:54:59 -0600413static int usb_kbd_testc(struct stdio_dev *sdev)
Marek Vasut7534a3e2011-10-10 16:34:26 +0100414{
415 struct stdio_dev *dev;
416 struct usb_device *usb_kbd_dev;
417 struct usb_kbd_pdata *data;
418
Thomas Watsond36c85a2022-02-10 15:13:14 -0600419 /*
420 * Polling the keyboard for an event can take dozens of milliseconds.
421 * Add a delay between polls to avoid blocking activity which polls
422 * rapidly, like the UEFI console timer.
423 */
424 unsigned long poll_delay = CONFIG_SYS_HZ / 50;
425
Jim Line2ebec82013-08-26 20:21:09 +0800426#ifdef CONFIG_CMD_NET
427 /*
428 * If net_busy_flag is 1, NET transfer is running,
429 * then we check key-pressed every second (first check may be
430 * less than 1 second) to improve TFTP booting performance.
431 */
Thomas Watsond36c85a2022-02-10 15:13:14 -0600432 if (net_busy_flag)
433 poll_delay = CONFIG_SYS_HZ;
Jim Line2ebec82013-08-26 20:21:09 +0800434#endif
Thomas Watsond36c85a2022-02-10 15:13:14 -0600435
436#ifdef CONFIG_SANDBOX
437 /*
438 * Skip delaying polls if a test requests it.
439 */
440 if (state_get_skip_delays())
441 poll_delay = 0;
442#endif
443
Michal Simek2b0634d2018-06-27 14:35:07 +0200444 dev = stdio_get_by_name(sdev->name);
Marek Vasut7534a3e2011-10-10 16:34:26 +0100445 usb_kbd_dev = (struct usb_device *)dev->priv;
446 data = usb_kbd_dev->privptr;
447
Thomas Watsond36c85a2022-02-10 15:13:14 -0600448 if (get_timer(kbd_testc_tms) >= poll_delay) {
449 usb_kbd_poll_for_event(usb_kbd_dev);
450 kbd_testc_tms = get_timer(0);
451 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100452
453 return !(data->usb_in_pointer == data->usb_out_pointer);
454}
455
456/* gets the character from the queue */
Simon Glass0d1e1f72014-07-23 06:54:59 -0600457static int usb_kbd_getc(struct stdio_dev *sdev)
Marek Vasut7534a3e2011-10-10 16:34:26 +0100458{
459 struct stdio_dev *dev;
460 struct usb_device *usb_kbd_dev;
461 struct usb_kbd_pdata *data;
462
Michal Simek2b0634d2018-06-27 14:35:07 +0200463 dev = stdio_get_by_name(sdev->name);
Marek Vasut7534a3e2011-10-10 16:34:26 +0100464 usb_kbd_dev = (struct usb_device *)dev->priv;
465 data = usb_kbd_dev->privptr;
466
Michal Simek07b3cc92018-06-27 14:16:54 +0200467 while (data->usb_in_pointer == data->usb_out_pointer) {
Stefan Roese80877fa2022-09-02 14:10:46 +0200468 schedule();
Marek Vasut7534a3e2011-10-10 16:34:26 +0100469 usb_kbd_poll_for_event(usb_kbd_dev);
Michal Simek07b3cc92018-06-27 14:16:54 +0200470 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100471
472 if (data->usb_out_pointer == USB_KBD_BUFFER_LEN - 1)
473 data->usb_out_pointer = 0;
474 else
475 data->usb_out_pointer++;
476
477 return data->usb_kbd_buffer[data->usb_out_pointer];
478}
479
480/* probes the USB device dev for keyboard type. */
Simon Glass02f0c282015-09-08 11:15:11 -0600481static int usb_kbd_probe_dev(struct usb_device *dev, unsigned int ifnum)
wdenkaffae2b2002-08-17 09:36:01 +0000482{
Tom Rix83b9e1d2009-10-31 12:37:38 -0500483 struct usb_interface *iface;
wdenkaffae2b2002-08-17 09:36:01 +0000484 struct usb_endpoint_descriptor *ep;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100485 struct usb_kbd_pdata *data;
Janne Grunauc8381722024-04-04 08:25:54 +0200486 unsigned int quirks = 0;
Stefan Brüns6de61c82021-02-20 17:26:06 +0100487 int epNum;
wdenkaffae2b2002-08-17 09:36:01 +0000488
Marek Vasutb9300982011-10-10 05:34:25 +0000489 if (dev->descriptor.bNumConfigurations != 1)
490 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100491
wdenkaffae2b2002-08-17 09:36:01 +0000492 iface = &dev->config.if_desc[ifnum];
493
Simon Glassab88fdb2015-11-08 23:48:04 -0700494 if (iface->desc.bInterfaceClass != USB_CLASS_HID)
Tom Rix83b9e1d2009-10-31 12:37:38 -0500495 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100496
Simon Glassab88fdb2015-11-08 23:48:04 -0700497 if (iface->desc.bInterfaceSubClass != USB_SUB_HID_BOOT)
Tom Rix83b9e1d2009-10-31 12:37:38 -0500498 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100499
Simon Glassab88fdb2015-11-08 23:48:04 -0700500 if (iface->desc.bInterfaceProtocol != USB_PROT_HID_KEYBOARD)
Tom Rix83b9e1d2009-10-31 12:37:38 -0500501 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100502
Stefan Brüns6de61c82021-02-20 17:26:06 +0100503 for (epNum = 0; epNum < iface->desc.bNumEndpoints; epNum++) {
504 ep = &iface->ep_desc[epNum];
wdenkaffae2b2002-08-17 09:36:01 +0000505
Stefan Brüns6de61c82021-02-20 17:26:06 +0100506 /* Check if endpoint is interrupt IN endpoint */
507 if ((ep->bmAttributes & 3) != 3)
508 continue;
wdenkaffae2b2002-08-17 09:36:01 +0000509
Stefan Brüns6de61c82021-02-20 17:26:06 +0100510 if (ep->bEndpointAddress & 0x80)
511 break;
512 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100513
Stefan Brüns6de61c82021-02-20 17:26:06 +0100514 if (epNum == iface->desc.bNumEndpoints)
Marek Vasutb9300982011-10-10 05:34:25 +0000515 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100516
Stefan Brüns6de61c82021-02-20 17:26:06 +0100517 debug("USB KBD: found interrupt EP: 0x%x\n", ep->bEndpointAddress);
Marek Vasut7534a3e2011-10-10 16:34:26 +0100518
Janne Grunauc8381722024-04-04 08:25:54 +0200519 switch (dev->descriptor.idVendor) {
520 case USB_VENDOR_ID_APPLE:
521 case USB_VENDOR_ID_KEYCHRON:
522 quirks |= USB_HID_QUIRK_POLL_NO_REPORT_IDLE;
523 break;
524 default:
525 break;
526 }
527
Marek Vasut7534a3e2011-10-10 16:34:26 +0100528 data = malloc(sizeof(struct usb_kbd_pdata));
529 if (!data) {
530 printf("USB KBD: Error allocating private data\n");
531 return 0;
532 }
533
534 /* Clear private data */
535 memset(data, 0, sizeof(struct usb_kbd_pdata));
536
Allen Martinef769ca2012-10-24 08:32:04 +0000537 /* allocate input buffer aligned and sized to USB DMA alignment */
Adrian Cox17472dc2014-04-10 14:02:44 +0100538 data->new = memalign(USB_DMA_MINALIGN,
539 roundup(USB_KBD_BOOT_REPORT_SIZE, USB_DMA_MINALIGN));
Allen Martinef769ca2012-10-24 08:32:04 +0000540
Janne Grunau4a7fda92024-04-04 08:25:53 +0200541 data->ifnum = ifnum;
542
Marek Vasut7534a3e2011-10-10 16:34:26 +0100543 /* Insert private data into USB device structure */
544 dev->privptr = data;
545
546 /* Set IRQ handler */
547 dev->irq_handle = usb_kbd_irq;
548
Hans de Goede3c794702014-09-24 14:06:10 +0200549 data->intpipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
550 data->intpktsize = min(usb_maxpacket(dev, data->intpipe),
551 USB_KBD_BOOT_REPORT_SIZE);
552 data->intinterval = ep->bInterval;
Hans de Goede650d7202015-05-19 22:51:03 +0200553 data->last_report = -1;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100554
555 /* We found a USB Keyboard, install it. */
Stefan Brüns6de61c82021-02-20 17:26:06 +0100556 debug("USB KBD: set boot protocol\n");
Marek Vasut7534a3e2011-10-10 16:34:26 +0100557 usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0);
558
Hans de Goede650d7202015-05-19 22:51:03 +0200559#if !defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP) && \
560 !defined(CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE)
Stefan Brüns6de61c82021-02-20 17:26:06 +0100561 debug("USB KBD: set idle interval...\n");
Hans de Goede650d7202015-05-19 22:51:03 +0200562 usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE / 4, 0);
Hans de Goeded4e99a62015-06-18 22:34:34 +0200563#else
Stefan Brüns6de61c82021-02-20 17:26:06 +0100564 debug("USB KBD: set idle interval=0...\n");
Hans de Goeded4e99a62015-06-18 22:34:34 +0200565 usb_set_idle(dev, iface->desc.bInterfaceNumber, 0, 0);
Hans de Goede650d7202015-05-19 22:51:03 +0200566#endif
Marek Vasut7534a3e2011-10-10 16:34:26 +0100567
Janne Grunauc8381722024-04-04 08:25:54 +0200568 /*
569 * Apple and Keychron keyboards do not report the device state. Reports
570 * are only returned during key presses.
571 */
572 if (quirks & USB_HID_QUIRK_POLL_NO_REPORT_IDLE) {
573 debug("USB KBD: quirk: skip testing device state\n");
574 return 1;
575 }
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530576 debug("USB KBD: enable interrupt pipe...\n");
Hans de Goede2d02ae72014-09-24 14:06:11 +0200577#ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE
578 data->intq = create_int_queue(dev, data->intpipe, 1,
Hans de Goedebbd2d2a2015-01-11 20:38:28 +0100579 USB_KBD_BOOT_REPORT_SIZE, data->new,
580 data->intinterval);
Hans de Goede2d02ae72014-09-24 14:06:11 +0200581 if (!data->intq) {
Stephen Warren1fd879e2015-11-13 13:34:09 -0700582#elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
583 if (usb_get_report(dev, iface->desc.bInterfaceNumber,
584 1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE) < 0) {
Hans de Goede2d02ae72014-09-24 14:06:11 +0200585#else
Michal Suchanekf5d5af42019-08-18 10:55:25 +0200586 if (usb_int_msg(dev, data->intpipe, data->new, data->intpktsize,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +0200587 data->intinterval, false) < 0) {
Hans de Goede2d02ae72014-09-24 14:06:11 +0200588#endif
Vincent Palatin0eab9982013-05-10 19:48:59 -0700589 printf("Failed to get keyboard state from device %04x:%04x\n",
590 dev->descriptor.idVendor, dev->descriptor.idProduct);
591 /* Abort, we don't want to use that non-functional keyboard. */
592 return 0;
593 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100594
595 /* Success. */
wdenkaffae2b2002-08-17 09:36:01 +0000596 return 1;
597}
598
Simon Glassfcdc0c32015-03-25 12:22:33 -0600599static int probe_usb_keyboard(struct usb_device *dev)
600{
601 char *stdinname;
602 struct stdio_dev usb_kbd_dev;
Janne Grunau4a7fda92024-04-04 08:25:53 +0200603 unsigned int ifnum;
604 unsigned int max_ifnum = min((unsigned int)USB_MAX_ACTIVE_INTERFACES,
605 (unsigned int)dev->config.no_of_if);
Simon Glassfcdc0c32015-03-25 12:22:33 -0600606 int error;
607
608 /* Try probing the keyboard */
Janne Grunau4a7fda92024-04-04 08:25:53 +0200609 for (ifnum = 0; ifnum < max_ifnum; ifnum++) {
610 if (usb_kbd_probe_dev(dev, ifnum) == 1)
611 break;
612 }
613 if (ifnum >= max_ifnum)
Simon Glassfcdc0c32015-03-25 12:22:33 -0600614 return -ENOENT;
615
616 /* Register the keyboard */
617 debug("USB KBD: register.\n");
618 memset(&usb_kbd_dev, 0, sizeof(struct stdio_dev));
619 strcpy(usb_kbd_dev.name, DEVNAME);
Simon Glasse21ad262024-09-01 16:26:19 -0600620 usb_kbd_dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_DM;
Simon Glassfcdc0c32015-03-25 12:22:33 -0600621 usb_kbd_dev.getc = usb_kbd_getc;
622 usb_kbd_dev.tstc = usb_kbd_testc;
623 usb_kbd_dev.priv = (void *)dev;
624 error = stdio_register(&usb_kbd_dev);
625 if (error)
626 return error;
627
Simon Glass64b723f2017-08-03 12:22:12 -0600628 stdinname = env_get("stdin");
Simon Glassdfc25352017-01-16 07:03:26 -0700629#if CONFIG_IS_ENABLED(CONSOLE_MUX)
Kory Maincent70ffe4e2022-06-22 10:59:31 +0200630 if (strstr(stdinname, DEVNAME) != NULL) {
631 error = iomux_doenv(stdin, stdinname);
632 if (error)
633 return error;
634 }
Simon Glassfcdc0c32015-03-25 12:22:33 -0600635#else
636 /* Check if this is the standard input device. */
Kory Maincent70ffe4e2022-06-22 10:59:31 +0200637 if (!strcmp(stdinname, DEVNAME)) {
638 /* Reassign the console */
639 if (overwrite_console())
640 return 1;
Simon Glassfcdc0c32015-03-25 12:22:33 -0600641
Kory Maincent70ffe4e2022-06-22 10:59:31 +0200642 error = console_assign(stdin, DEVNAME);
643 if (error)
644 return error;
645 }
Simon Glassfcdc0c32015-03-25 12:22:33 -0600646#endif
647
648 return 0;
649}
650
Simon Glass02f0c282015-09-08 11:15:11 -0600651static int usb_kbd_probe(struct udevice *dev)
652{
653 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glass02f0c282015-09-08 11:15:11 -0600654
Masahiro Yamada9b70df52016-09-06 22:17:35 +0900655 return probe_usb_keyboard(udev);
Simon Glass02f0c282015-09-08 11:15:11 -0600656}
657
Simon Glass3c3ba902016-01-03 13:50:31 -0700658static int usb_kbd_remove(struct udevice *dev)
659{
660 struct usb_device *udev = dev_get_parent_priv(dev);
661 struct usb_kbd_pdata *data;
662 struct stdio_dev *sdev;
663 int ret;
664
665 sdev = stdio_get_by_name(DEVNAME);
666 if (!sdev) {
667 ret = -ENXIO;
668 goto err;
669 }
670 data = udev->privptr;
Simon Glassdfc25352017-01-16 07:03:26 -0700671#if CONFIG_IS_ENABLED(CONSOLE_MUX)
Andy Shevchenko05a17e82021-02-11 17:09:44 +0200672 if (iomux_replace_device(stdin, DEVNAME, "nulldev")) {
Simon Glass3c3ba902016-01-03 13:50:31 -0700673 ret = -ENOLINK;
674 goto err;
675 }
676#endif
Andy Shevchenko05a17e82021-02-11 17:09:44 +0200677 if (stdio_deregister_dev(sdev, true)) {
678 ret = -EPERM;
679 goto err;
680 }
Simon Glass3c3ba902016-01-03 13:50:31 -0700681#ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE
682 destroy_int_queue(udev, data->intq);
683#endif
684 free(data->new);
685 free(data);
686
687 return 0;
688err:
689 printf("%s: warning, ret=%d", __func__, ret);
690 return ret;
691}
692
Simon Glass02f0c282015-09-08 11:15:11 -0600693static const struct udevice_id usb_kbd_ids[] = {
694 { .compatible = "usb-keyboard" },
695 { }
696};
697
698U_BOOT_DRIVER(usb_kbd) = {
699 .name = "usb_kbd",
700 .id = UCLASS_KEYBOARD,
701 .of_match = usb_kbd_ids,
702 .probe = usb_kbd_probe,
Simon Glass3c3ba902016-01-03 13:50:31 -0700703 .remove = usb_kbd_remove,
Simon Glass02f0c282015-09-08 11:15:11 -0600704};
705
Simon Glass02f0c282015-09-08 11:15:11 -0600706static const struct usb_device_id kbd_id_table[] = {
707 {
708 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
709 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
710 USB_DEVICE_ID_MATCH_INT_PROTOCOL,
711 .bInterfaceClass = USB_CLASS_HID,
Simon Glassab88fdb2015-11-08 23:48:04 -0700712 .bInterfaceSubClass = USB_SUB_HID_BOOT,
713 .bInterfaceProtocol = USB_PROT_HID_KEYBOARD,
Simon Glass02f0c282015-09-08 11:15:11 -0600714 },
Janne Grunau4a7fda92024-04-04 08:25:53 +0200715 {
716 USB_DEVICE(USB_VENDOR_ID_APPLE,
717 USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021),
718 },
719 {
720 USB_DEVICE(USB_VENDOR_ID_APPLE,
721 USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021),
722 },
723 {
724 USB_DEVICE(USB_VENDOR_ID_APPLE,
725 USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021),
726 },
Simon Glass02f0c282015-09-08 11:15:11 -0600727 { } /* Terminating entry */
728};
729
730U_BOOT_USB_DEVICE(usb_kbd, kbd_id_table);