blob: fdc083c70cf9952ec000429355b605b2ec4ff43c [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
4 *
5 * Part of this source has been derived from the Linux USB
6 * project.
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00009 */
10#include <common.h>
Hans de Goede0ea09df2014-09-20 16:54:34 +020011#include <errno.h>
Marek Vasut9a8c72a2011-10-10 16:34:26 +010012#include <malloc.h>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020013#include <stdio_dev.h>
Christian Eggersc9182612008-05-21 22:12:00 +020014#include <asm/byteorder.h>
wdenkaffae2b2002-08-17 09:36:01 +000015
wdenkaffae2b2002-08-17 09:36:01 +000016#include <usb.h>
17
wdenkaffae2b2002-08-17 09:36:01 +000018/*
Marek Vasut00b7d6e2011-10-10 05:34:25 +000019 * If overwrite_console returns 1, the stdin, stderr and stdout
wdenkaffae2b2002-08-17 09:36:01 +000020 * are switched to the serial port, else the settings in the
21 * environment are used
22 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020023#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
Marek Vasut00b7d6e2011-10-10 05:34:25 +000024extern int overwrite_console(void);
wdenkaffae2b2002-08-17 09:36:01 +000025#else
Marek Vasut00b7d6e2011-10-10 05:34:25 +000026int overwrite_console(void)
wdenkaffae2b2002-08-17 09:36:01 +000027{
Marek Vasut00b7d6e2011-10-10 05:34:25 +000028 return 0;
wdenkaffae2b2002-08-17 09:36:01 +000029}
30#endif
31
Marek Vasut9a8c72a2011-10-10 16:34:26 +010032/* Keyboard sampling rate */
33#define REPEAT_RATE (40 / 4) /* 40msec -> 25cps */
34#define REPEAT_DELAY 10 /* 10 x REPEAT_RATE = 400msec */
wdenkaffae2b2002-08-17 09:36:01 +000035
36#define NUM_LOCK 0x53
Marek Vasut00b7d6e2011-10-10 05:34:25 +000037#define CAPS_LOCK 0x39
38#define SCROLL_LOCK 0x47
wdenkaffae2b2002-08-17 09:36:01 +000039
wdenkaffae2b2002-08-17 09:36:01 +000040/* Modifier bits */
Marek Vasut9a8c72a2011-10-10 16:34:26 +010041#define LEFT_CNTR (1 << 0)
42#define LEFT_SHIFT (1 << 1)
43#define LEFT_ALT (1 << 2)
44#define LEFT_GUI (1 << 3)
45#define RIGHT_CNTR (1 << 4)
46#define RIGHT_SHIFT (1 << 5)
47#define RIGHT_ALT (1 << 6)
48#define RIGHT_GUI (1 << 7)
wdenkaffae2b2002-08-17 09:36:01 +000049
Marek Vasut9a8c72a2011-10-10 16:34:26 +010050/* Size of the keyboard buffer */
51#define USB_KBD_BUFFER_LEN 0x20
wdenkaffae2b2002-08-17 09:36:01 +000052
Marek Vasut9a8c72a2011-10-10 16:34:26 +010053/* Device name */
Marek Vasut00b7d6e2011-10-10 05:34:25 +000054#define DEVNAME "usbkbd"
wdenkaffae2b2002-08-17 09:36:01 +000055
Marek Vasut9a8c72a2011-10-10 16:34:26 +010056/* Keyboard maps */
57static const unsigned char usb_kbd_numkey[] = {
Marek Vasut00b7d6e2011-10-10 05:34:25 +000058 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
59 '\r', 0x1b, '\b', '\t', ' ', '-', '=', '[', ']',
60 '\\', '#', ';', '\'', '`', ',', '.', '/'
wdenkaffae2b2002-08-17 09:36:01 +000061};
Marek Vasut9a8c72a2011-10-10 16:34:26 +010062static const unsigned char usb_kbd_numkey_shifted[] = {
Marek Vasut00b7d6e2011-10-10 05:34:25 +000063 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
64 '\r', 0x1b, '\b', '\t', ' ', '_', '+', '{', '}',
65 '|', '~', ':', '"', '~', '<', '>', '?'
wdenkaffae2b2002-08-17 09:36:01 +000066};
67
Vincent Palatind53da842012-01-09 12:59:36 +000068static const unsigned char usb_kbd_num_keypad[] = {
69 '/', '*', '-', '+', '\r',
70 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
71 '.', 0, 0, 0, '='
72};
73
Marek Vasut9a8c72a2011-10-10 16:34:26 +010074/*
Allen Martin4151a402012-11-06 13:26:03 -080075 * map arrow keys to ^F/^B ^N/^P, can't really use the proper
76 * ANSI sequence for arrow keys because the queuing code breaks
77 * when a single keypress expands to 3 queue elements
78 */
79static const unsigned char usb_kbd_arrow[] = {
80 0x6, 0x2, 0xe, 0x10
81};
82
83/*
Marek Vasut9a8c72a2011-10-10 16:34:26 +010084 * NOTE: It's important for the NUM, CAPS, SCROLL-lock bits to be in this
85 * order. See usb_kbd_setled() function!
86 */
87#define USB_KBD_NUMLOCK (1 << 0)
88#define USB_KBD_CAPSLOCK (1 << 1)
89#define USB_KBD_SCROLLLOCK (1 << 2)
90#define USB_KBD_CTRL (1 << 3)
Marek Vasut48c80732011-09-25 20:00:37 +010091
Marek Vasut9a8c72a2011-10-10 16:34:26 +010092#define USB_KBD_LEDMASK \
93 (USB_KBD_NUMLOCK | USB_KBD_CAPSLOCK | USB_KBD_SCROLLLOCK)
94
Adrian Cox08a98b82014-04-10 14:02:44 +010095/*
96 * USB Keyboard reports are 8 bytes in boot protocol.
97 * Appendix B of HID Device Class Definition 1.11
98 */
99#define USB_KBD_BOOT_REPORT_SIZE 8
100
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100101struct usb_kbd_pdata {
102 uint32_t repeat_delay;
103
104 uint32_t usb_in_pointer;
105 uint32_t usb_out_pointer;
106 uint8_t usb_kbd_buffer[USB_KBD_BUFFER_LEN];
107
Allen Martind7475382012-10-24 08:32:04 +0000108 uint8_t *new;
Adrian Cox08a98b82014-04-10 14:02:44 +0100109 uint8_t old[USB_KBD_BOOT_REPORT_SIZE];
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100110
111 uint8_t flags;
112};
113
Jim Lin07551f22013-08-13 19:04:22 +0800114extern int __maybe_unused net_busy_flag;
115
116/* The period of time between two calls of usb_kbd_testc(). */
117static unsigned long __maybe_unused kbd_testc_tms;
118
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100119/* Generic keyboard event polling. */
120void usb_kbd_generic_poll(void)
121{
122 struct stdio_dev *dev;
123 struct usb_device *usb_kbd_dev;
124 struct usb_kbd_pdata *data;
125 struct usb_interface *iface;
126 struct usb_endpoint_descriptor *ep;
127 int pipe;
128 int maxp;
129
130 /* Get the pointer to USB Keyboard device pointer */
131 dev = stdio_get_by_name(DEVNAME);
132 usb_kbd_dev = (struct usb_device *)dev->priv;
133 data = usb_kbd_dev->privptr;
134 iface = &usb_kbd_dev->config.if_desc[0];
135 ep = &iface->ep_desc[0];
136 pipe = usb_rcvintpipe(usb_kbd_dev, ep->bEndpointAddress);
137
138 /* Submit a interrupt transfer request */
139 maxp = usb_maxpacket(usb_kbd_dev, pipe);
140 usb_submit_int_msg(usb_kbd_dev, pipe, data->new,
Adrian Cox08a98b82014-04-10 14:02:44 +0100141 min(maxp, USB_KBD_BOOT_REPORT_SIZE),
142 ep->bInterval);
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100143}
144
145/* Puts character in the queue and sets up the in and out pointer. */
146static void usb_kbd_put_queue(struct usb_kbd_pdata *data, char c)
147{
148 if (data->usb_in_pointer == USB_KBD_BUFFER_LEN - 1) {
149 /* Check for buffer full. */
150 if (data->usb_out_pointer == 0)
151 return;
152
153 data->usb_in_pointer = 0;
154 } else {
155 /* Check for buffer full. */
156 if (data->usb_in_pointer == data->usb_out_pointer - 1)
157 return;
158
159 data->usb_in_pointer++;
160 }
161
162 data->usb_kbd_buffer[data->usb_in_pointer] = c;
163}
164
165/*
166 * Set the LEDs. Since this is used in the irq routine, the control job is
167 * issued with a timeout of 0. This means, that the job is queued without
168 * waiting for job completion.
169 */
170static void usb_kbd_setled(struct usb_device *dev)
171{
172 struct usb_interface *iface = &dev->config.if_desc[0];
173 struct usb_kbd_pdata *data = dev->privptr;
Hans de Goede9b239382014-09-20 16:51:26 +0200174 ALLOC_ALIGN_BUFFER(uint32_t, leds, 1, USB_DMA_MINALIGN);
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100175
Hans de Goede9b239382014-09-20 16:51:26 +0200176 *leds = data->flags & USB_KBD_LEDMASK;
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100177 usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
178 USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Hans de Goede9b239382014-09-20 16:51:26 +0200179 0x200, iface->desc.bInterfaceNumber, leds, 1, 0);
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100180}
181
182#define CAPITAL_MASK 0x20
183/* Translate the scancode in ASCII */
184static int usb_kbd_translate(struct usb_kbd_pdata *data, unsigned char scancode,
185 unsigned char modifier, int pressed)
186{
187 uint8_t keycode = 0;
188
189 /* Key released */
190 if (pressed == 0) {
191 data->repeat_delay = 0;
192 return 0;
193 }
194
195 if (pressed == 2) {
196 data->repeat_delay++;
197 if (data->repeat_delay < REPEAT_DELAY)
198 return 0;
199
200 data->repeat_delay = REPEAT_DELAY;
201 }
202
203 /* Alphanumeric values */
204 if ((scancode > 3) && (scancode <= 0x1d)) {
205 keycode = scancode - 4 + 'a';
206
207 if (data->flags & USB_KBD_CAPSLOCK)
208 keycode &= ~CAPITAL_MASK;
209
210 if (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) {
211 /* Handle CAPSLock + Shift pressed simultaneously */
212 if (keycode & CAPITAL_MASK)
213 keycode &= ~CAPITAL_MASK;
214 else
215 keycode |= CAPITAL_MASK;
216 }
217 }
218
219 if ((scancode > 0x1d) && (scancode < 0x3a)) {
220 /* Shift pressed */
221 if (modifier & (LEFT_SHIFT | RIGHT_SHIFT))
222 keycode = usb_kbd_numkey_shifted[scancode - 0x1e];
223 else
224 keycode = usb_kbd_numkey[scancode - 0x1e];
225 }
226
Allen Martin4151a402012-11-06 13:26:03 -0800227 /* Arrow keys */
228 if ((scancode >= 0x4f) && (scancode <= 0x52))
229 keycode = usb_kbd_arrow[scancode - 0x4f];
230
Vincent Palatind53da842012-01-09 12:59:36 +0000231 /* Numeric keypad */
232 if ((scancode >= 0x54) && (scancode <= 0x67))
233 keycode = usb_kbd_num_keypad[scancode - 0x54];
234
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100235 if (data->flags & USB_KBD_CTRL)
236 keycode = scancode - 0x3;
237
238 if (pressed == 1) {
239 if (scancode == NUM_LOCK) {
240 data->flags ^= USB_KBD_NUMLOCK;
241 return 1;
242 }
243
244 if (scancode == CAPS_LOCK) {
245 data->flags ^= USB_KBD_CAPSLOCK;
246 return 1;
247 }
248 if (scancode == SCROLL_LOCK) {
249 data->flags ^= USB_KBD_SCROLLLOCK;
250 return 1;
251 }
252 }
253
254 /* Report keycode if any */
255 if (keycode) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530256 debug("%c", keycode);
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100257 usb_kbd_put_queue(data, keycode);
258 }
259
260 return 0;
261}
262
263static uint32_t usb_kbd_service_key(struct usb_device *dev, int i, int up)
264{
265 uint32_t res = 0;
266 struct usb_kbd_pdata *data = dev->privptr;
267 uint8_t *new;
268 uint8_t *old;
269
270 if (up) {
271 new = data->old;
272 old = data->new;
273 } else {
274 new = data->new;
275 old = data->old;
276 }
277
Adrian Cox08a98b82014-04-10 14:02:44 +0100278 if ((old[i] > 3) &&
279 (memscan(new + 2, old[i], USB_KBD_BOOT_REPORT_SIZE - 2) ==
280 new + USB_KBD_BOOT_REPORT_SIZE)) {
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100281 res |= usb_kbd_translate(data, old[i], data->new[0], up);
Adrian Cox08a98b82014-04-10 14:02:44 +0100282 }
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100283
284 return res;
285}
286
287/* Interrupt service routine */
288static int usb_kbd_irq_worker(struct usb_device *dev)
289{
290 struct usb_kbd_pdata *data = dev->privptr;
291 int i, res = 0;
292
293 /* No combo key pressed */
294 if (data->new[0] == 0x00)
295 data->flags &= ~USB_KBD_CTRL;
296 /* Left or Right Ctrl pressed */
297 else if ((data->new[0] == LEFT_CNTR) || (data->new[0] == RIGHT_CNTR))
298 data->flags |= USB_KBD_CTRL;
299
Adrian Cox08a98b82014-04-10 14:02:44 +0100300 for (i = 2; i < USB_KBD_BOOT_REPORT_SIZE; i++) {
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100301 res |= usb_kbd_service_key(dev, i, 0);
302 res |= usb_kbd_service_key(dev, i, 1);
303 }
304
305 /* Key is still pressed */
306 if ((data->new[2] > 3) && (data->old[2] == data->new[2]))
307 res |= usb_kbd_translate(data, data->new[2], data->new[0], 2);
308
309 if (res == 1)
310 usb_kbd_setled(dev);
311
Adrian Cox08a98b82014-04-10 14:02:44 +0100312 memcpy(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE);
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100313
314 return 1;
315}
316
317/* Keyboard interrupt handler */
318static int usb_kbd_irq(struct usb_device *dev)
319{
Adrian Cox08a98b82014-04-10 14:02:44 +0100320 if ((dev->irq_status != 0) ||
321 (dev->irq_act_len != USB_KBD_BOOT_REPORT_SIZE)) {
Vivek Gautamceb49722013-04-12 16:34:33 +0530322 debug("USB KBD: Error %lX, len %d\n",
323 dev->irq_status, dev->irq_act_len);
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100324 return 1;
325 }
326
327 return usb_kbd_irq_worker(dev);
328}
329
330/* Interrupt polling */
Marek Vasut48c80732011-09-25 20:00:37 +0100331static inline void usb_kbd_poll_for_event(struct usb_device *dev)
332{
333#if defined(CONFIG_SYS_USB_EVENT_POLL)
amartin@nvidia.comf9636e82011-12-20 14:56:16 +0000334 struct usb_interface *iface;
335 struct usb_endpoint_descriptor *ep;
336 struct usb_kbd_pdata *data;
337 int pipe;
338 int maxp;
339
340 /* Get the pointer to USB Keyboard device pointer */
341 data = dev->privptr;
342 iface = &dev->config.if_desc[0];
343 ep = &iface->ep_desc[0];
344 pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
345
346 /* Submit a interrupt transfer request */
347 maxp = usb_maxpacket(dev, pipe);
348 usb_submit_int_msg(dev, pipe, &data->new[0],
Adrian Cox08a98b82014-04-10 14:02:44 +0100349 min(maxp, USB_KBD_BOOT_REPORT_SIZE),
350 ep->bInterval);
amartin@nvidia.comf9636e82011-12-20 14:56:16 +0000351
Marek Vasut48c80732011-09-25 20:00:37 +0100352 usb_kbd_irq_worker(dev);
353#elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
354 struct usb_interface *iface;
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100355 struct usb_kbd_pdata *data = dev->privptr;
Marek Vasut48c80732011-09-25 20:00:37 +0100356 iface = &dev->config.if_desc[0];
357 usb_get_report(dev, iface->desc.bInterfaceNumber,
Adrian Cox08a98b82014-04-10 14:02:44 +0100358 1, 0, data->new, USB_KBD_BOOT_REPORT_SIZE);
359 if (memcmp(data->old, data->new, USB_KBD_BOOT_REPORT_SIZE))
Marek Vasut48c80732011-09-25 20:00:37 +0100360 usb_kbd_irq_worker(dev);
361#endif
362}
363
wdenkaffae2b2002-08-17 09:36:01 +0000364/* test if a character is in the queue */
Simon Glass709ea542014-07-23 06:54:59 -0600365static int usb_kbd_testc(struct stdio_dev *sdev)
wdenkaffae2b2002-08-17 09:36:01 +0000366{
Marek Vasut48c80732011-09-25 20:00:37 +0100367 struct stdio_dev *dev;
368 struct usb_device *usb_kbd_dev;
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100369 struct usb_kbd_pdata *data;
Marek Vasut48c80732011-09-25 20:00:37 +0100370
Jim Linc95e2b92013-08-26 20:21:09 +0800371#ifdef CONFIG_CMD_NET
372 /*
373 * If net_busy_flag is 1, NET transfer is running,
374 * then we check key-pressed every second (first check may be
375 * less than 1 second) to improve TFTP booting performance.
376 */
377 if (net_busy_flag && (get_timer(kbd_testc_tms) < CONFIG_SYS_HZ))
378 return 0;
379 kbd_testc_tms = get_timer(0);
380#endif
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100381 dev = stdio_get_by_name(DEVNAME);
Marek Vasut48c80732011-09-25 20:00:37 +0100382 usb_kbd_dev = (struct usb_device *)dev->priv;
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100383 data = usb_kbd_dev->privptr;
Marek Vasut48c80732011-09-25 20:00:37 +0100384
385 usb_kbd_poll_for_event(usb_kbd_dev);
386
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100387 return !(data->usb_in_pointer == data->usb_out_pointer);
wdenkaffae2b2002-08-17 09:36:01 +0000388}
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100389
wdenkaffae2b2002-08-17 09:36:01 +0000390/* gets the character from the queue */
Simon Glass709ea542014-07-23 06:54:59 -0600391static int usb_kbd_getc(struct stdio_dev *sdev)
wdenkaffae2b2002-08-17 09:36:01 +0000392{
Marek Vasut48c80732011-09-25 20:00:37 +0100393 struct stdio_dev *dev;
394 struct usb_device *usb_kbd_dev;
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100395 struct usb_kbd_pdata *data;
Marek Vasut48c80732011-09-25 20:00:37 +0100396
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100397 dev = stdio_get_by_name(DEVNAME);
Marek Vasut48c80732011-09-25 20:00:37 +0100398 usb_kbd_dev = (struct usb_device *)dev->priv;
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100399 data = usb_kbd_dev->privptr;
Marek Vasut48c80732011-09-25 20:00:37 +0100400
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100401 while (data->usb_in_pointer == data->usb_out_pointer)
Marek Vasut48c80732011-09-25 20:00:37 +0100402 usb_kbd_poll_for_event(usb_kbd_dev);
403
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100404 if (data->usb_out_pointer == USB_KBD_BUFFER_LEN - 1)
405 data->usb_out_pointer = 0;
wdenkaffae2b2002-08-17 09:36:01 +0000406 else
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100407 data->usb_out_pointer++;
wdenkaffae2b2002-08-17 09:36:01 +0000408
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100409 return data->usb_kbd_buffer[data->usb_out_pointer];
wdenkaffae2b2002-08-17 09:36:01 +0000410}
411
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100412/* probes the USB device dev for keyboard type. */
413static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum)
414{
415 struct usb_interface *iface;
416 struct usb_endpoint_descriptor *ep;
417 struct usb_kbd_pdata *data;
418 int pipe, maxp;
wdenkaffae2b2002-08-17 09:36:01 +0000419
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100420 if (dev->descriptor.bNumConfigurations != 1)
421 return 0;
422
423 iface = &dev->config.if_desc[ifnum];
424
425 if (iface->desc.bInterfaceClass != 3)
426 return 0;
427
428 if (iface->desc.bInterfaceSubClass != 1)
429 return 0;
430
431 if (iface->desc.bInterfaceProtocol != 1)
432 return 0;
433
434 if (iface->desc.bNumEndpoints != 1)
435 return 0;
436
437 ep = &iface->ep_desc[0];
438
439 /* Check if endpoint 1 is interrupt endpoint */
440 if (!(ep->bEndpointAddress & 0x80))
441 return 0;
442
443 if ((ep->bmAttributes & 3) != 3)
444 return 0;
445
Vivek Gautamceb49722013-04-12 16:34:33 +0530446 debug("USB KBD: found set protocol...\n");
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100447
448 data = malloc(sizeof(struct usb_kbd_pdata));
449 if (!data) {
450 printf("USB KBD: Error allocating private data\n");
451 return 0;
452 }
453
454 /* Clear private data */
455 memset(data, 0, sizeof(struct usb_kbd_pdata));
456
Allen Martind7475382012-10-24 08:32:04 +0000457 /* allocate input buffer aligned and sized to USB DMA alignment */
Adrian Cox08a98b82014-04-10 14:02:44 +0100458 data->new = memalign(USB_DMA_MINALIGN,
459 roundup(USB_KBD_BOOT_REPORT_SIZE, USB_DMA_MINALIGN));
Allen Martind7475382012-10-24 08:32:04 +0000460
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100461 /* Insert private data into USB device structure */
462 dev->privptr = data;
463
464 /* Set IRQ handler */
465 dev->irq_handle = usb_kbd_irq;
466
467 pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
468 maxp = usb_maxpacket(dev, pipe);
469
470 /* We found a USB Keyboard, install it. */
471 usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0);
472
Vivek Gautamceb49722013-04-12 16:34:33 +0530473 debug("USB KBD: found set idle...\n");
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100474 usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE, 0);
475
Vivek Gautamceb49722013-04-12 16:34:33 +0530476 debug("USB KBD: enable interrupt pipe...\n");
Adrian Cox08a98b82014-04-10 14:02:44 +0100477 if (usb_submit_int_msg(dev, pipe, data->new,
478 min(maxp, USB_KBD_BOOT_REPORT_SIZE),
Vincent Palatin5da2dc92013-05-10 19:48:59 -0700479 ep->bInterval) < 0) {
480 printf("Failed to get keyboard state from device %04x:%04x\n",
481 dev->descriptor.idVendor, dev->descriptor.idProduct);
482 /* Abort, we don't want to use that non-functional keyboard. */
483 return 0;
484 }
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100485
486 /* Success. */
487 return 1;
488}
489
490/* Search for keyboard and register it if found. */
wdenkaffae2b2002-08-17 09:36:01 +0000491int drv_usb_kbd_init(void)
492{
Hans de Goede3f78a282014-09-20 16:54:36 +0200493 struct stdio_dev usb_kbd_dev;
wdenkaffae2b2002-08-17 09:36:01 +0000494 struct usb_device *dev;
Marek Vasut00b7d6e2011-10-10 05:34:25 +0000495 char *stdinname = getenv("stdin");
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100496 int error, i;
wdenkaffae2b2002-08-17 09:36:01 +0000497
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100498 /* Scan all USB Devices */
499 for (i = 0; i < USB_MAX_DEVICE; i++) {
500 /* Get USB device. */
501 dev = usb_get_dev_index(i);
502 if (!dev)
Ryan CHEN3b20fd82008-08-20 13:00:17 -0400503 return -1;
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100504
505 if (dev->devnum == -1)
506 continue;
507
508 /* Try probing the keyboard */
509 if (usb_kbd_probe(dev, 0) != 1)
510 continue;
511
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100512 /* Register the keyboard */
Vivek Gautamceb49722013-04-12 16:34:33 +0530513 debug("USB KBD: register.\n");
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100514 memset(&usb_kbd_dev, 0, sizeof(struct stdio_dev));
515 strcpy(usb_kbd_dev.name, DEVNAME);
516 usb_kbd_dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100517 usb_kbd_dev.getc = usb_kbd_getc;
518 usb_kbd_dev.tstc = usb_kbd_testc;
519 usb_kbd_dev.priv = (void *)dev;
520 error = stdio_register(&usb_kbd_dev);
521 if (error)
522 return error;
523
amartin@nvidia.comfb3ef642011-12-23 10:29:48 +0000524#ifdef CONFIG_CONSOLE_MUX
525 error = iomux_doenv(stdin, stdinname);
526 if (error)
527 return error;
528#else
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100529 /* Check if this is the standard input device. */
530 if (strcmp(stdinname, DEVNAME))
531 return 1;
532
533 /* Reassign the console */
534 if (overwrite_console())
535 return 1;
536
537 error = console_assign(stdin, DEVNAME);
538 if (error)
539 return error;
amartin@nvidia.comfb3ef642011-12-23 10:29:48 +0000540#endif
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100541
542 return 1;
wdenkaffae2b2002-08-17 09:36:01 +0000543 }
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100544
545 /* No USB Keyboard found */
wdenkaffae2b2002-08-17 09:36:01 +0000546 return -1;
547}
548
Marek Vasut9a8c72a2011-10-10 16:34:26 +0100549/* Deregister the keyboard. */
Hans de Goede8a8a2252014-09-20 16:54:38 +0200550int usb_kbd_deregister(int force)
wdenkaffae2b2002-08-17 09:36:01 +0000551{
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200552#ifdef CONFIG_SYS_STDIO_DEREGISTER
Hans de Goede8a8a2252014-09-20 16:54:38 +0200553 int ret = stdio_deregister(DEVNAME, force);
Hans de Goede0ea09df2014-09-20 16:54:34 +0200554 if (ret && ret != -ENODEV)
555 return ret;
556
557 return 0;
Jean-Christophe PLAGNIOL-VILLARDfea91ed2008-12-02 21:58:04 +0100558#else
559 return 1;
560#endif
wdenkaffae2b2002-08-17 09:36:01 +0000561}