xref: /rk3399_rockchip-uboot/common/usb_kbd.c (revision c39c9a8375169d07fdcac148f8dfdc6865a1a5fe)
1 /*
2  * (C) Copyright 2001
3  * Denis Peter, MPL AG Switzerland
4  *
5  * Part of this source has been derived from the Linux USB
6  * project.
7  *
8  * SPDX-License-Identifier:	GPL-2.0+
9  */
10 #include <common.h>
11 #include <console.h>
12 #include <dm.h>
13 #include <errno.h>
14 #include <malloc.h>
15 #include <memalign.h>
16 #include <stdio_dev.h>
17 #include <asm/byteorder.h>
18 
19 #include <usb.h>
20 
21 /*
22  * If overwrite_console returns 1, the stdin, stderr and stdout
23  * are switched to the serial port, else the settings in the
24  * environment are used
25  */
26 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
27 extern int overwrite_console(void);
28 #else
29 int overwrite_console(void)
30 {
31 	return 0;
32 }
33 #endif
34 
35 /* Keyboard sampling rate */
36 #define REPEAT_RATE	40		/* 40msec -> 25cps */
37 #define REPEAT_DELAY	10		/* 10 x REPEAT_RATE = 400msec */
38 
39 #define NUM_LOCK	0x53
40 #define CAPS_LOCK	0x39
41 #define SCROLL_LOCK	0x47
42 
43 /* Modifier bits */
44 #define LEFT_CNTR	(1 << 0)
45 #define LEFT_SHIFT	(1 << 1)
46 #define LEFT_ALT	(1 << 2)
47 #define LEFT_GUI	(1 << 3)
48 #define RIGHT_CNTR	(1 << 4)
49 #define RIGHT_SHIFT	(1 << 5)
50 #define RIGHT_ALT	(1 << 6)
51 #define RIGHT_GUI	(1 << 7)
52 
53 /* Size of the keyboard buffer */
54 #define USB_KBD_BUFFER_LEN	0x20
55 
56 /* Device name */
57 #define DEVNAME			"usbkbd"
58 
59 /* Keyboard maps */
60 static const unsigned char usb_kbd_numkey[] = {
61 	'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
62 	'\r', 0x1b, '\b', '\t', ' ', '-', '=', '[', ']',
63 	'\\', '#', ';', '\'', '`', ',', '.', '/'
64 };
65 static const unsigned char usb_kbd_numkey_shifted[] = {
66 	'!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
67 	'\r', 0x1b, '\b', '\t', ' ', '_', '+', '{', '}',
68 	'|', '~', ':', '"', '~', '<', '>', '?'
69 };
70 
71 static const unsigned char usb_kbd_num_keypad[] = {
72 	'/', '*', '-', '+', '\r',
73 	'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
74 	'.', 0, 0, 0, '='
75 };
76 
77 static const u8 usb_special_keys[] = {
78 #ifdef CONFIG_USB_KEYBOARD_FN_KEYS
79 	'2', 'H', '5', '3', 'F', '6', 'C', 'D', 'B', 'A'
80 #else
81 	'C', 'D', 'B', 'A'
82 #endif
83 };
84 
85 /*
86  * NOTE: It's important for the NUM, CAPS, SCROLL-lock bits to be in this
87  *       order. See usb_kbd_setled() function!
88  */
89 #define USB_KBD_NUMLOCK		(1 << 0)
90 #define USB_KBD_CAPSLOCK	(1 << 1)
91 #define USB_KBD_SCROLLLOCK	(1 << 2)
92 #define USB_KBD_CTRL		(1 << 3)
93 
94 #define USB_KBD_LEDMASK		\
95 	(USB_KBD_NUMLOCK | USB_KBD_CAPSLOCK | USB_KBD_SCROLLLOCK)
96 
97 /*
98  * USB Keyboard reports are 8 bytes in boot protocol.
99  * Appendix B of HID Device Class Definition 1.11
100  */
101 #define USB_KBD_BOOT_REPORT_SIZE 8
102 
103 struct usb_kbd_pdata {
104 	unsigned long	intpipe;
105 	int		intpktsize;
106 	int		intinterval;
107 	unsigned long	last_report;
108 	struct int_queue *intq;
109 
110 	uint32_t	repeat_delay;
111 
112 	uint32_t	usb_in_pointer;
113 	uint32_t	usb_out_pointer;
114 	uint8_t		usb_kbd_buffer[USB_KBD_BUFFER_LEN];
115 
116 	uint8_t		*new;
117 	uint8_t		old[USB_KBD_BOOT_REPORT_SIZE];
118 
119 	uint8_t		flags;
120 };
121 
122 extern int __maybe_unused net_busy_flag;
123 
124 /* The period of time between two calls of usb_kbd_testc(). */
125 static unsigned long __maybe_unused kbd_testc_tms;
126 
127 /* Puts character in the queue and sets up the in and out pointer. */
128 static void usb_kbd_put_queue(struct usb_kbd_pdata *data, u8 c)
129 {
130 	if (data->usb_in_pointer == USB_KBD_BUFFER_LEN - 1) {
131 		/* Check for buffer full. */
132 		if (data->usb_out_pointer == 0)
133 			return;
134 
135 		data->usb_in_pointer = 0;
136 	} else {
137 		/* Check for buffer full. */
138 		if (data->usb_in_pointer == data->usb_out_pointer - 1)
139 			return;
140 
141 		data->usb_in_pointer++;
142 	}
143 
144 	data->usb_kbd_buffer[data->usb_in_pointer] = c;
145 }
146 
147 /*
148  * Set the LEDs. Since this is used in the irq routine, the control job is
149  * issued with a timeout of 0. This means, that the job is queued without
150  * waiting for job completion.
151  */
152 static void usb_kbd_setled(struct usb_device *dev)
153 {
154 	struct usb_interface *iface = &dev->config.if_desc[0];
155 	struct usb_kbd_pdata *data = dev->privptr;
156 	ALLOC_ALIGN_BUFFER(uint32_t, leds, 1, USB_DMA_MINALIGN);
157 
158 	*leds = data->flags & USB_KBD_LEDMASK;
159 	usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
160 		USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
161 		0x200, iface->desc.bInterfaceNumber, leds, 1, 0);
162 }
163 
164 #define CAPITAL_MASK	0x20
165 /* Translate the scancode in ASCII */
166 static int usb_kbd_translate(struct usb_kbd_pdata *data, unsigned char scancode,
167 				unsigned char modifier, int pressed)
168 {
169 	uint8_t keycode = 0;
170 
171 	/* Key released */
172 	if (pressed == 0) {
173 		data->repeat_delay = 0;
174 		return 0;
175 	}
176 
177 	if (pressed == 2) {
178 		data->repeat_delay++;
179 		if (data->repeat_delay < REPEAT_DELAY)
180 			return 0;
181 
182 		data->repeat_delay = REPEAT_DELAY;
183 	}
184 
185 	/* Alphanumeric values */
186 	if ((scancode > 3) && (scancode <= 0x1d)) {
187 		keycode = scancode - 4 + 'a';
188 
189 		if (data->flags & USB_KBD_CAPSLOCK)
190 			keycode &= ~CAPITAL_MASK;
191 
192 		if (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) {
193 			/* Handle CAPSLock + Shift pressed simultaneously */
194 			if (keycode & CAPITAL_MASK)
195 				keycode &= ~CAPITAL_MASK;
196 			else
197 				keycode |= CAPITAL_MASK;
198 		}
199 	}
200 
201 	if ((scancode > 0x1d) && (scancode < 0x39)) {
202 		/* Shift pressed */
203 		if (modifier & (LEFT_SHIFT | RIGHT_SHIFT))
204 			keycode = usb_kbd_numkey_shifted[scancode - 0x1e];
205 		else
206 			keycode = usb_kbd_numkey[scancode - 0x1e];
207 	}
208 
209 	/* Numeric keypad */
210 	if ((scancode >= 0x54) && (scancode <= 0x67))
211 		keycode = usb_kbd_num_keypad[scancode - 0x54];
212 
213 	if (data->flags & USB_KBD_CTRL)
214 		keycode = scancode - 0x3;
215 
216 	if (pressed == 1) {
217 		if (scancode == NUM_LOCK) {
218 			data->flags ^= USB_KBD_NUMLOCK;
219 			return 1;
220 		}
221 
222 		if (scancode == CAPS_LOCK) {
223 			data->flags ^= USB_KBD_CAPSLOCK;
224 			return 1;
225 		}
226 		if (scancode == SCROLL_LOCK) {
227 			data->flags ^= USB_KBD_SCROLLLOCK;
228 			return 1;
229 		}
230 	}
231 
232 	/* Report keycode if any */
233 	if (keycode) {
234 		debug("%c", keycode);
235 		usb_kbd_put_queue(data, keycode);
236 		return 0;
237 	}
238 
239 #ifdef CONFIG_USB_KEYBOARD_FN_KEYS
240 	if (scancode < 0x3a || scancode > 0x52 ||
241 	    scancode == 0x46 || scancode == 0x47)
242 		return 1;
243 
244 	usb_kbd_put_queue(data, 0x1b);
245 	if (scancode < 0x3e) {
246 		/* F1 - F4 */
247 		usb_kbd_put_queue(data, 0x4f);
248 		usb_kbd_put_queue(data, scancode - 0x3a + 'P');
249 		return 0;
250 	}
251 	usb_kbd_put_queue(data, '[');
252 	if (scancode < 0x42) {
253 		/* F5 - F8 */
254 		usb_kbd_put_queue(data, '1');
255 		if (scancode == 0x3e)
256 			--scancode;
257 		keycode = scancode - 0x3f + '7';
258 	} else if (scancode < 0x49) {
259 		/* F9 - F12 */
260 		usb_kbd_put_queue(data, '2');
261 		if (scancode > 0x43)
262 			++scancode;
263 		keycode = scancode - 0x42 + '0';
264 	} else {
265 		/*
266 		 * INSERT, HOME, PAGE UP, DELETE, END, PAGE DOWN,
267 		 * RIGHT, LEFT, DOWN, UP
268 		 */
269 		keycode = usb_special_keys[scancode - 0x49];
270 	}
271 	usb_kbd_put_queue(data, keycode);
272 	if (scancode < 0x4f && scancode != 0x4a && scancode != 0x4d)
273 		usb_kbd_put_queue(data, '~');
274 	return 0;
275 #else
276 	/* Left, Right, Up, Down */
277 	if (scancode > 0x4e && scancode < 0x53) {
278 		usb_kbd_put_queue(data, 0x1b);
279 		usb_kbd_put_queue(data, '[');
280 		usb_kbd_put_queue(data, usb_special_keys[scancode - 0x4f]);
281 		return 0;
282 	}
283 	return 1;
284 #endif /* CONFIG_USB_KEYBOARD_FN_KEYS */
285 }
286 
287 static uint32_t usb_kbd_service_key(struct usb_device *dev, int i, int up)
288 {
289 	uint32_t res = 0;
290 	struct usb_kbd_pdata *data = dev->privptr;
291 	uint8_t *new;
292 	uint8_t *old;
293 
294 	if (up) {
295 		new = data->old;
296 		old = data->new;
297 	} else {
298 		new = data->new;
299 		old = data->old;
300 	}
301 
302 	if ((old[i] > 3) &&
303 	    (memscan(new + 2, old[i], USB_KBD_BOOT_REPORT_SIZE - 2) ==
304 			new + USB_KBD_BOOT_REPORT_SIZE)) {
305 		res |= usb_kbd_translate(data, old[i], data->new[0], up);
306 	}
307 
308 	return res;
309 }
310 
311 /* Interrupt service routine */
312 static int usb_kbd_irq_worker(struct usb_device *dev)
313 {
314 	struct usb_kbd_pdata *data = dev->privptr;
315 	int i, res = 0;
316 
317 	/* No combo key pressed */
318 	if (data->new[0] == 0x00)
319 		data->flags &= ~USB_KBD_CTRL;
320 	/* Left or Right Ctrl pressed */
321 	else if ((data->new[0] == LEFT_CNTR) || (data->new[0] == RIGHT_CNTR))
322 		data->flags |= USB_KBD_CTRL;
323 
324 	for (i = 2; i < USB_KBD_BOOT_REPORT_SIZE; i++) {
325 		res |= usb_kbd_service_key(dev, i, 0);
326 		res |= usb_kbd_service_key(dev, i, 1);
327 	}
328 
329 	/* Key is still pressed */
330 	if ((data->new[2] > 3) && (data->old[2] == data->new[2]))
331 		res |= usb_kbd_translate(data, data->new[2], data->new[0], 2);
332 
333 	if (res == 1)
334 		usb_kbd_setled(dev);
335 
336 	memcpy(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE);
337 
338 	return 1;
339 }
340 
341 /* Keyboard interrupt handler */
342 static int usb_kbd_irq(struct usb_device *dev)
343 {
344 	if ((dev->irq_status != 0) ||
345 	    (dev->irq_act_len != USB_KBD_BOOT_REPORT_SIZE)) {
346 		debug("USB KBD: Error %lX, len %d\n",
347 		      dev->irq_status, dev->irq_act_len);
348 		return 1;
349 	}
350 
351 	return usb_kbd_irq_worker(dev);
352 }
353 
354 /* Interrupt polling */
355 static inline void usb_kbd_poll_for_event(struct usb_device *dev)
356 {
357 #if defined(CONFIG_SYS_USB_EVENT_POLL)
358 	struct usb_kbd_pdata *data = dev->privptr;
359 
360 	/* Submit an interrupt transfer request */
361 	if (usb_int_msg(dev, data->intpipe, &data->new[0],
362 			data->intpktsize, data->intinterval, true) >= 0)
363 		usb_kbd_irq_worker(dev);
364 #elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP) || \
365       defined(CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE)
366 #if defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
367 	struct usb_interface *iface;
368 	struct usb_kbd_pdata *data = dev->privptr;
369 	iface = &dev->config.if_desc[0];
370 	usb_get_report(dev, iface->desc.bInterfaceNumber,
371 		       1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE);
372 	if (memcmp(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE)) {
373 		usb_kbd_irq_worker(dev);
374 #else
375 	struct usb_kbd_pdata *data = dev->privptr;
376 	if (poll_int_queue(dev, data->intq)) {
377 		usb_kbd_irq_worker(dev);
378 		/* We've consumed all queued int packets, create new */
379 		destroy_int_queue(dev, data->intq);
380 		data->intq = create_int_queue(dev, data->intpipe, 1,
381 				      USB_KBD_BOOT_REPORT_SIZE, data->new,
382 				      data->intinterval);
383 #endif
384 		data->last_report = get_timer(0);
385 	/* Repeat last usb hid report every REPEAT_RATE ms for keyrepeat */
386 	} else if (data->last_report != -1 &&
387 		   get_timer(data->last_report) > REPEAT_RATE) {
388 		usb_kbd_irq_worker(dev);
389 		data->last_report = get_timer(0);
390 	}
391 #endif
392 }
393 
394 /* test if a character is in the queue */
395 static int usb_kbd_testc(struct stdio_dev *sdev)
396 {
397 	struct stdio_dev *dev;
398 	struct usb_device *usb_kbd_dev;
399 	struct usb_kbd_pdata *data;
400 
401 #ifdef CONFIG_CMD_NET
402 	/*
403 	 * If net_busy_flag is 1, NET transfer is running,
404 	 * then we check key-pressed every second (first check may be
405 	 * less than 1 second) to improve TFTP booting performance.
406 	 */
407 	if (net_busy_flag && (get_timer(kbd_testc_tms) < CONFIG_SYS_HZ))
408 		return 0;
409 	kbd_testc_tms = get_timer(0);
410 #endif
411 	dev = stdio_get_by_name(DEVNAME);
412 	usb_kbd_dev = (struct usb_device *)dev->priv;
413 	data = usb_kbd_dev->privptr;
414 
415 	usb_kbd_poll_for_event(usb_kbd_dev);
416 
417 	return !(data->usb_in_pointer == data->usb_out_pointer);
418 }
419 
420 /* gets the character from the queue */
421 static int usb_kbd_getc(struct stdio_dev *sdev)
422 {
423 	struct stdio_dev *dev;
424 	struct usb_device *usb_kbd_dev;
425 	struct usb_kbd_pdata *data;
426 
427 	dev = stdio_get_by_name(DEVNAME);
428 	usb_kbd_dev = (struct usb_device *)dev->priv;
429 	data = usb_kbd_dev->privptr;
430 
431 	while (data->usb_in_pointer == data->usb_out_pointer)
432 		usb_kbd_poll_for_event(usb_kbd_dev);
433 
434 	if (data->usb_out_pointer == USB_KBD_BUFFER_LEN - 1)
435 		data->usb_out_pointer = 0;
436 	else
437 		data->usb_out_pointer++;
438 
439 	return data->usb_kbd_buffer[data->usb_out_pointer];
440 }
441 
442 /* probes the USB device dev for keyboard type. */
443 static int usb_kbd_probe_dev(struct usb_device *dev, unsigned int ifnum)
444 {
445 	struct usb_interface *iface;
446 	struct usb_endpoint_descriptor *ep;
447 	struct usb_kbd_pdata *data;
448 
449 	if (dev->descriptor.bNumConfigurations != 1)
450 		return 0;
451 
452 	iface = &dev->config.if_desc[ifnum];
453 
454 	if (iface->desc.bInterfaceClass != USB_CLASS_HID)
455 		return 0;
456 
457 	if (iface->desc.bInterfaceSubClass != USB_SUB_HID_BOOT)
458 		return 0;
459 
460 	if (iface->desc.bInterfaceProtocol != USB_PROT_HID_KEYBOARD)
461 		return 0;
462 
463 	if (iface->desc.bNumEndpoints != 1)
464 		return 0;
465 
466 	ep = &iface->ep_desc[0];
467 
468 	/* Check if endpoint 1 is interrupt endpoint */
469 	if (!(ep->bEndpointAddress & 0x80))
470 		return 0;
471 
472 	if ((ep->bmAttributes & 3) != 3)
473 		return 0;
474 
475 	debug("USB KBD: found set protocol...\n");
476 
477 	data = malloc(sizeof(struct usb_kbd_pdata));
478 	if (!data) {
479 		printf("USB KBD: Error allocating private data\n");
480 		return 0;
481 	}
482 
483 	/* Clear private data */
484 	memset(data, 0, sizeof(struct usb_kbd_pdata));
485 
486 	/* allocate input buffer aligned and sized to USB DMA alignment */
487 	data->new = memalign(USB_DMA_MINALIGN,
488 		roundup(USB_KBD_BOOT_REPORT_SIZE, USB_DMA_MINALIGN));
489 
490 	/* Insert private data into USB device structure */
491 	dev->privptr = data;
492 
493 	/* Set IRQ handler */
494 	dev->irq_handle = usb_kbd_irq;
495 
496 	data->intpipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
497 	data->intpktsize = min(usb_maxpacket(dev, data->intpipe),
498 			       USB_KBD_BOOT_REPORT_SIZE);
499 	data->intinterval = ep->bInterval;
500 	data->last_report = -1;
501 
502 	/* We found a USB Keyboard, install it. */
503 	usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0);
504 
505 	debug("USB KBD: found set idle...\n");
506 #if !defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP) && \
507     !defined(CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE)
508 	usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE / 4, 0);
509 #else
510 	usb_set_idle(dev, iface->desc.bInterfaceNumber, 0, 0);
511 #endif
512 
513 	debug("USB KBD: enable interrupt pipe...\n");
514 #ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE
515 	data->intq = create_int_queue(dev, data->intpipe, 1,
516 				      USB_KBD_BOOT_REPORT_SIZE, data->new,
517 				      data->intinterval);
518 	if (!data->intq) {
519 #elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
520 	if (usb_get_report(dev, iface->desc.bInterfaceNumber,
521 			   1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE) < 0) {
522 #else
523 	if (usb_int_msg(dev, data->intpipe, data->new, data->intpktsize,
524 			data->intinterval, false) < 0) {
525 #endif
526 		printf("Failed to get keyboard state from device %04x:%04x\n",
527 		       dev->descriptor.idVendor, dev->descriptor.idProduct);
528 		/* Abort, we don't want to use that non-functional keyboard. */
529 		return 0;
530 	}
531 
532 	/* Success. */
533 	return 1;
534 }
535 
536 static int probe_usb_keyboard(struct usb_device *dev)
537 {
538 	char *stdinname;
539 	struct stdio_dev usb_kbd_dev;
540 	int error;
541 
542 	/* Try probing the keyboard */
543 	if (usb_kbd_probe_dev(dev, 0) != 1)
544 		return -ENOENT;
545 
546 	/* Register the keyboard */
547 	debug("USB KBD: register.\n");
548 	memset(&usb_kbd_dev, 0, sizeof(struct stdio_dev));
549 	strcpy(usb_kbd_dev.name, DEVNAME);
550 	usb_kbd_dev.flags =  DEV_FLAGS_INPUT;
551 	usb_kbd_dev.getc = usb_kbd_getc;
552 	usb_kbd_dev.tstc = usb_kbd_testc;
553 	usb_kbd_dev.priv = (void *)dev;
554 	error = stdio_register(&usb_kbd_dev);
555 	if (error)
556 		return error;
557 
558 	stdinname = env_get("stdin");
559 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
560 	error = iomux_doenv(stdin, stdinname);
561 	if (error)
562 		return error;
563 #else
564 	/* Check if this is the standard input device. */
565 	if (strcmp(stdinname, DEVNAME))
566 		return 1;
567 
568 	/* Reassign the console */
569 	if (overwrite_console())
570 		return 1;
571 
572 	error = console_assign(stdin, DEVNAME);
573 	if (error)
574 		return error;
575 #endif
576 
577 	return 0;
578 }
579 
580 #if !CONFIG_IS_ENABLED(DM_USB)
581 /* Search for keyboard and register it if found. */
582 int drv_usb_kbd_init(void)
583 {
584 	int error, i;
585 
586 	debug("%s: Probing for keyboard\n", __func__);
587 	/* Scan all USB Devices */
588 	for (i = 0; i < USB_MAX_DEVICE; i++) {
589 		struct usb_device *dev;
590 
591 		/* Get USB device. */
592 		dev = usb_get_dev_index(i);
593 		if (!dev)
594 			break;
595 
596 		if (dev->devnum == -1)
597 			continue;
598 
599 		error = probe_usb_keyboard(dev);
600 		if (!error)
601 			return 1;
602 		if (error && error != -ENOENT)
603 			return error;
604 	}
605 
606 	/* No USB Keyboard found */
607 	return -1;
608 }
609 
610 /* Deregister the keyboard. */
611 int usb_kbd_deregister(int force)
612 {
613 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
614 	struct stdio_dev *dev;
615 	struct usb_device *usb_kbd_dev;
616 	struct usb_kbd_pdata *data;
617 
618 	dev = stdio_get_by_name(DEVNAME);
619 	if (dev) {
620 		usb_kbd_dev = (struct usb_device *)dev->priv;
621 		data = usb_kbd_dev->privptr;
622 		if (stdio_deregister_dev(dev, force) != 0)
623 			return 1;
624 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
625 		if (iomux_doenv(stdin, env_get("stdin")) != 0)
626 			return 1;
627 #endif
628 #ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE
629 		destroy_int_queue(usb_kbd_dev, data->intq);
630 #endif
631 		free(data->new);
632 		free(data);
633 	}
634 
635 	return 0;
636 #else
637 	return 1;
638 #endif
639 }
640 
641 #endif
642 
643 #if CONFIG_IS_ENABLED(DM_USB)
644 
645 static int usb_kbd_probe(struct udevice *dev)
646 {
647 	struct usb_device *udev = dev_get_parent_priv(dev);
648 
649 	return probe_usb_keyboard(udev);
650 }
651 
652 static int usb_kbd_remove(struct udevice *dev)
653 {
654 	struct usb_device *udev = dev_get_parent_priv(dev);
655 	struct usb_kbd_pdata *data;
656 	struct stdio_dev *sdev;
657 	int ret;
658 
659 	sdev = stdio_get_by_name(DEVNAME);
660 	if (!sdev) {
661 		ret = -ENXIO;
662 		goto err;
663 	}
664 	data = udev->privptr;
665 	if (stdio_deregister_dev(sdev, true)) {
666 		ret = -EPERM;
667 		goto err;
668 	}
669 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
670 	if (iomux_doenv(stdin, env_get("stdin"))) {
671 		ret = -ENOLINK;
672 		goto err;
673 	}
674 #endif
675 #ifdef CONFIG_SYS_USB_EVENT_POLL_VIA_INT_QUEUE
676 	destroy_int_queue(udev, data->intq);
677 #endif
678 	free(data->new);
679 	free(data);
680 
681 	return 0;
682 err:
683 	printf("%s: warning, ret=%d", __func__, ret);
684 	return ret;
685 }
686 
687 static const struct udevice_id usb_kbd_ids[] = {
688 	{ .compatible = "usb-keyboard" },
689 	{ }
690 };
691 
692 U_BOOT_DRIVER(usb_kbd) = {
693 	.name	= "usb_kbd",
694 	.id	= UCLASS_KEYBOARD,
695 	.of_match = usb_kbd_ids,
696 	.probe = usb_kbd_probe,
697 	.remove = usb_kbd_remove,
698 };
699 
700 static const struct usb_device_id kbd_id_table[] = {
701 	{
702 		.match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
703 			USB_DEVICE_ID_MATCH_INT_SUBCLASS |
704 			USB_DEVICE_ID_MATCH_INT_PROTOCOL,
705 		.bInterfaceClass = USB_CLASS_HID,
706 		.bInterfaceSubClass = USB_SUB_HID_BOOT,
707 		.bInterfaceProtocol = USB_PROT_HID_KEYBOARD,
708 	},
709 	{ }		/* Terminating entry */
710 };
711 
712 U_BOOT_USB_DEVICE(usb_kbd, kbd_id_table);
713 
714 #endif
715