blob: 1ad67caf13c554c6fd0eb34945f41b3a20f736bc [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 Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00009 */
10#include <common.h>
Marek Vasut7534a3e2011-10-10 16:34:26 +010011#include <malloc.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020012#include <stdio_dev.h>
Christian Eggers4e0e8d02008-05-21 22:12:00 +020013#include <asm/byteorder.h>
wdenkaffae2b2002-08-17 09:36:01 +000014
wdenkaffae2b2002-08-17 09:36:01 +000015#include <usb.h>
16
wdenkaffae2b2002-08-17 09:36:01 +000017/*
Marek Vasutb9300982011-10-10 05:34:25 +000018 * If overwrite_console returns 1, the stdin, stderr and stdout
wdenkaffae2b2002-08-17 09:36:01 +000019 * are switched to the serial port, else the settings in the
20 * environment are used
21 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020022#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
Marek Vasutb9300982011-10-10 05:34:25 +000023extern int overwrite_console(void);
wdenkaffae2b2002-08-17 09:36:01 +000024#else
Marek Vasutb9300982011-10-10 05:34:25 +000025int overwrite_console(void)
wdenkaffae2b2002-08-17 09:36:01 +000026{
Marek Vasutb9300982011-10-10 05:34:25 +000027 return 0;
wdenkaffae2b2002-08-17 09:36:01 +000028}
29#endif
30
Marek Vasut7534a3e2011-10-10 16:34:26 +010031/* Keyboard sampling rate */
32#define REPEAT_RATE (40 / 4) /* 40msec -> 25cps */
33#define REPEAT_DELAY 10 /* 10 x REPEAT_RATE = 400msec */
wdenkaffae2b2002-08-17 09:36:01 +000034
35#define NUM_LOCK 0x53
Marek Vasutb9300982011-10-10 05:34:25 +000036#define CAPS_LOCK 0x39
37#define SCROLL_LOCK 0x47
wdenkaffae2b2002-08-17 09:36:01 +000038
wdenkaffae2b2002-08-17 09:36:01 +000039/* Modifier bits */
Marek Vasut7534a3e2011-10-10 16:34:26 +010040#define LEFT_CNTR (1 << 0)
41#define LEFT_SHIFT (1 << 1)
42#define LEFT_ALT (1 << 2)
43#define LEFT_GUI (1 << 3)
44#define RIGHT_CNTR (1 << 4)
45#define RIGHT_SHIFT (1 << 5)
46#define RIGHT_ALT (1 << 6)
47#define RIGHT_GUI (1 << 7)
wdenkaffae2b2002-08-17 09:36:01 +000048
Marek Vasut7534a3e2011-10-10 16:34:26 +010049/* Size of the keyboard buffer */
50#define USB_KBD_BUFFER_LEN 0x20
wdenkaffae2b2002-08-17 09:36:01 +000051
Marek Vasut7534a3e2011-10-10 16:34:26 +010052/* Device name */
Marek Vasutb9300982011-10-10 05:34:25 +000053#define DEVNAME "usbkbd"
wdenkaffae2b2002-08-17 09:36:01 +000054
Marek Vasut7534a3e2011-10-10 16:34:26 +010055/* Keyboard maps */
56static const unsigned char usb_kbd_numkey[] = {
Marek Vasutb9300982011-10-10 05:34:25 +000057 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
58 '\r', 0x1b, '\b', '\t', ' ', '-', '=', '[', ']',
59 '\\', '#', ';', '\'', '`', ',', '.', '/'
wdenkaffae2b2002-08-17 09:36:01 +000060};
Marek Vasut7534a3e2011-10-10 16:34:26 +010061static const unsigned char usb_kbd_numkey_shifted[] = {
Marek Vasutb9300982011-10-10 05:34:25 +000062 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
63 '\r', 0x1b, '\b', '\t', ' ', '_', '+', '{', '}',
64 '|', '~', ':', '"', '~', '<', '>', '?'
wdenkaffae2b2002-08-17 09:36:01 +000065};
66
Vincent Palatin92f6a622012-01-09 12:59:36 +000067static const unsigned char usb_kbd_num_keypad[] = {
68 '/', '*', '-', '+', '\r',
69 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
70 '.', 0, 0, 0, '='
71};
72
Marek Vasut7534a3e2011-10-10 16:34:26 +010073/*
Allen Martin1bfb4cd2012-11-06 13:26:03 -080074 * map arrow keys to ^F/^B ^N/^P, can't really use the proper
75 * ANSI sequence for arrow keys because the queuing code breaks
76 * when a single keypress expands to 3 queue elements
77 */
78static const unsigned char usb_kbd_arrow[] = {
79 0x6, 0x2, 0xe, 0x10
80};
81
82/*
Marek Vasut7534a3e2011-10-10 16:34:26 +010083 * NOTE: It's important for the NUM, CAPS, SCROLL-lock bits to be in this
84 * order. See usb_kbd_setled() function!
85 */
86#define USB_KBD_NUMLOCK (1 << 0)
87#define USB_KBD_CAPSLOCK (1 << 1)
88#define USB_KBD_SCROLLLOCK (1 << 2)
89#define USB_KBD_CTRL (1 << 3)
Marek Vasut82cf9a42011-09-25 20:00:37 +010090
Marek Vasut7534a3e2011-10-10 16:34:26 +010091#define USB_KBD_LEDMASK \
92 (USB_KBD_NUMLOCK | USB_KBD_CAPSLOCK | USB_KBD_SCROLLLOCK)
Marek Vasut82cf9a42011-09-25 20:00:37 +010093
Marek Vasut7534a3e2011-10-10 16:34:26 +010094struct usb_kbd_pdata {
95 uint32_t repeat_delay;
wdenkaffae2b2002-08-17 09:36:01 +000096
Marek Vasut7534a3e2011-10-10 16:34:26 +010097 uint32_t usb_in_pointer;
98 uint32_t usb_out_pointer;
99 uint8_t usb_kbd_buffer[USB_KBD_BUFFER_LEN];
Marek Vasut82cf9a42011-09-25 20:00:37 +0100100
Allen Martinef769ca2012-10-24 08:32:04 +0000101 uint8_t *new;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100102 uint8_t old[8];
Marek Vasut82cf9a42011-09-25 20:00:37 +0100103
Marek Vasut7534a3e2011-10-10 16:34:26 +0100104 uint8_t flags;
105};
Marek Vasut82cf9a42011-09-25 20:00:37 +0100106
Jim Linc562f972013-08-13 19:04:22 +0800107extern int __maybe_unused net_busy_flag;
108
109/* The period of time between two calls of usb_kbd_testc(). */
110static unsigned long __maybe_unused kbd_testc_tms;
111
Marek Vasut7534a3e2011-10-10 16:34:26 +0100112/* Generic keyboard event polling. */
113void usb_kbd_generic_poll(void)
wdenkaffae2b2002-08-17 09:36:01 +0000114{
Marek Vasut82cf9a42011-09-25 20:00:37 +0100115 struct stdio_dev *dev;
116 struct usb_device *usb_kbd_dev;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100117 struct usb_kbd_pdata *data;
118 struct usb_interface *iface;
119 struct usb_endpoint_descriptor *ep;
120 int pipe;
121 int maxp;
Marek Vasut82cf9a42011-09-25 20:00:37 +0100122
Marek Vasut7534a3e2011-10-10 16:34:26 +0100123 /* Get the pointer to USB Keyboard device pointer */
124 dev = stdio_get_by_name(DEVNAME);
Marek Vasut82cf9a42011-09-25 20:00:37 +0100125 usb_kbd_dev = (struct usb_device *)dev->priv;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100126 data = usb_kbd_dev->privptr;
127 iface = &usb_kbd_dev->config.if_desc[0];
128 ep = &iface->ep_desc[0];
129 pipe = usb_rcvintpipe(usb_kbd_dev, ep->bEndpointAddress);
wdenkaffae2b2002-08-17 09:36:01 +0000130
Marek Vasut7534a3e2011-10-10 16:34:26 +0100131 /* Submit a interrupt transfer request */
132 maxp = usb_maxpacket(usb_kbd_dev, pipe);
133 usb_submit_int_msg(usb_kbd_dev, pipe, data->new,
134 maxp > 8 ? 8 : maxp, ep->bInterval);
wdenkaffae2b2002-08-17 09:36:01 +0000135}
136
Marek Vasut7534a3e2011-10-10 16:34:26 +0100137/* Puts character in the queue and sets up the in and out pointer. */
138static void usb_kbd_put_queue(struct usb_kbd_pdata *data, char c)
wdenkaffae2b2002-08-17 09:36:01 +0000139{
Marek Vasut7534a3e2011-10-10 16:34:26 +0100140 if (data->usb_in_pointer == USB_KBD_BUFFER_LEN - 1) {
141 /* Check for buffer full. */
142 if (data->usb_out_pointer == 0)
143 return;
wdenkaffae2b2002-08-17 09:36:01 +0000144
Marek Vasut7534a3e2011-10-10 16:34:26 +0100145 data->usb_in_pointer = 0;
146 } else {
147 /* Check for buffer full. */
148 if (data->usb_in_pointer == data->usb_out_pointer - 1)
149 return;
wdenkaffae2b2002-08-17 09:36:01 +0000150
Marek Vasut7534a3e2011-10-10 16:34:26 +0100151 data->usb_in_pointer++;
152 }
wdenkaffae2b2002-08-17 09:36:01 +0000153
Marek Vasut7534a3e2011-10-10 16:34:26 +0100154 data->usb_kbd_buffer[data->usb_in_pointer] = c;
wdenkaffae2b2002-08-17 09:36:01 +0000155}
156
Marek Vasut7534a3e2011-10-10 16:34:26 +0100157/*
158 * Set the LEDs. Since this is used in the irq routine, the control job is
159 * issued with a timeout of 0. This means, that the job is queued without
160 * waiting for job completion.
wdenkaffae2b2002-08-17 09:36:01 +0000161 */
wdenkaffae2b2002-08-17 09:36:01 +0000162static void usb_kbd_setled(struct usb_device *dev)
163{
Marek Vasut7534a3e2011-10-10 16:34:26 +0100164 struct usb_interface *iface = &dev->config.if_desc[0];
165 struct usb_kbd_pdata *data = dev->privptr;
166 uint32_t leds = data->flags & USB_KBD_LEDMASK;
167
wdenkaffae2b2002-08-17 09:36:01 +0000168 usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
169 USB_REQ_SET_REPORT, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Tom Rix83b9e1d2009-10-31 12:37:38 -0500170 0x200, iface->desc.bInterfaceNumber, (void *)&leds, 1, 0);
wdenkaffae2b2002-08-17 09:36:01 +0000171}
172
Marek Vasut7534a3e2011-10-10 16:34:26 +0100173#define CAPITAL_MASK 0x20
wdenkaffae2b2002-08-17 09:36:01 +0000174/* Translate the scancode in ASCII */
Marek Vasut7534a3e2011-10-10 16:34:26 +0100175static int usb_kbd_translate(struct usb_kbd_pdata *data, unsigned char scancode,
176 unsigned char modifier, int pressed)
wdenkaffae2b2002-08-17 09:36:01 +0000177{
Marek Vasut7534a3e2011-10-10 16:34:26 +0100178 uint8_t keycode = 0;
wdenkaffae2b2002-08-17 09:36:01 +0000179
Marek Vasut7534a3e2011-10-10 16:34:26 +0100180 /* Key released */
Marek Vasutb9300982011-10-10 05:34:25 +0000181 if (pressed == 0) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100182 data->repeat_delay = 0;
wdenkaffae2b2002-08-17 09:36:01 +0000183 return 0;
184 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100185
Marek Vasutb9300982011-10-10 05:34:25 +0000186 if (pressed == 2) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100187 data->repeat_delay++;
188 if (data->repeat_delay < REPEAT_DELAY)
wdenkaffae2b2002-08-17 09:36:01 +0000189 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100190
191 data->repeat_delay = REPEAT_DELAY;
wdenkaffae2b2002-08-17 09:36:01 +0000192 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100193
194 /* Alphanumeric values */
195 if ((scancode > 3) && (scancode <= 0x1d)) {
196 keycode = scancode - 4 + 'a';
197
198 if (data->flags & USB_KBD_CAPSLOCK)
Marek Vasutb9300982011-10-10 05:34:25 +0000199 keycode &= ~CAPITAL_MASK;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100200
201 if (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) {
202 /* Handle CAPSLock + Shift pressed simultaneously */
Marek Vasutb9300982011-10-10 05:34:25 +0000203 if (keycode & CAPITAL_MASK)
Marek Vasutb9300982011-10-10 05:34:25 +0000204 keycode &= ~CAPITAL_MASK;
wdenkaffae2b2002-08-17 09:36:01 +0000205 else
Marek Vasutb9300982011-10-10 05:34:25 +0000206 keycode |= CAPITAL_MASK;
wdenkaffae2b2002-08-17 09:36:01 +0000207 }
208 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100209
210 if ((scancode > 0x1d) && (scancode < 0x3a)) {
211 /* Shift pressed */
212 if (modifier & (LEFT_SHIFT | RIGHT_SHIFT))
213 keycode = usb_kbd_numkey_shifted[scancode - 0x1e];
214 else
215 keycode = usb_kbd_numkey[scancode - 0x1e];
wdenkaffae2b2002-08-17 09:36:01 +0000216 }
Zhang Wei37cc4f02008-01-03 10:51:15 +0800217
Allen Martin1bfb4cd2012-11-06 13:26:03 -0800218 /* Arrow keys */
219 if ((scancode >= 0x4f) && (scancode <= 0x52))
220 keycode = usb_kbd_arrow[scancode - 0x4f];
221
Vincent Palatin92f6a622012-01-09 12:59:36 +0000222 /* Numeric keypad */
223 if ((scancode >= 0x54) && (scancode <= 0x67))
224 keycode = usb_kbd_num_keypad[scancode - 0x54];
225
Marek Vasut7534a3e2011-10-10 16:34:26 +0100226 if (data->flags & USB_KBD_CTRL)
Zhang Wei37cc4f02008-01-03 10:51:15 +0800227 keycode = scancode - 0x3;
228
Marek Vasutb9300982011-10-10 05:34:25 +0000229 if (pressed == 1) {
230 if (scancode == NUM_LOCK) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100231 data->flags ^= USB_KBD_NUMLOCK;
wdenkaffae2b2002-08-17 09:36:01 +0000232 return 1;
233 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100234
Marek Vasutb9300982011-10-10 05:34:25 +0000235 if (scancode == CAPS_LOCK) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100236 data->flags ^= USB_KBD_CAPSLOCK;
wdenkaffae2b2002-08-17 09:36:01 +0000237 return 1;
238 }
Marek Vasutb9300982011-10-10 05:34:25 +0000239 if (scancode == SCROLL_LOCK) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100240 data->flags ^= USB_KBD_SCROLLLOCK;
wdenkaffae2b2002-08-17 09:36:01 +0000241 return 1;
242 }
243 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100244
245 /* Report keycode if any */
246 if (keycode) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530247 debug("%c", keycode);
Marek Vasut7534a3e2011-10-10 16:34:26 +0100248 usb_kbd_put_queue(data, keycode);
wdenkaffae2b2002-08-17 09:36:01 +0000249 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100250
wdenkaffae2b2002-08-17 09:36:01 +0000251 return 0;
252}
253
Marek Vasut7534a3e2011-10-10 16:34:26 +0100254static uint32_t usb_kbd_service_key(struct usb_device *dev, int i, int up)
255{
256 uint32_t res = 0;
257 struct usb_kbd_pdata *data = dev->privptr;
258 uint8_t *new;
259 uint8_t *old;
260
261 if (up) {
262 new = data->old;
263 old = data->new;
264 } else {
265 new = data->new;
266 old = data->old;
267 }
268
269 if ((old[i] > 3) && (memscan(new + 2, old[i], 6) == new + 8))
270 res |= usb_kbd_translate(data, old[i], data->new[0], up);
271
272 return res;
273}
274
wdenkaffae2b2002-08-17 09:36:01 +0000275/* Interrupt service routine */
Marek Vasut82cf9a42011-09-25 20:00:37 +0100276static int usb_kbd_irq_worker(struct usb_device *dev)
wdenkaffae2b2002-08-17 09:36:01 +0000277{
Marek Vasut7534a3e2011-10-10 16:34:26 +0100278 struct usb_kbd_pdata *data = dev->privptr;
279 int i, res = 0;
wdenkaffae2b2002-08-17 09:36:01 +0000280
Marek Vasut7534a3e2011-10-10 16:34:26 +0100281 /* No combo key pressed */
282 if (data->new[0] == 0x00)
283 data->flags &= ~USB_KBD_CTRL;
284 /* Left or Right Ctrl pressed */
285 else if ((data->new[0] == LEFT_CNTR) || (data->new[0] == RIGHT_CNTR))
286 data->flags |= USB_KBD_CTRL;
Zhang Wei37cc4f02008-01-03 10:51:15 +0800287
wdenkaffae2b2002-08-17 09:36:01 +0000288 for (i = 2; i < 8; i++) {
Marek Vasut7534a3e2011-10-10 16:34:26 +0100289 res |= usb_kbd_service_key(dev, i, 0);
290 res |= usb_kbd_service_key(dev, i, 1);
wdenkaffae2b2002-08-17 09:36:01 +0000291 }
Marek Vasutb9300982011-10-10 05:34:25 +0000292
Marek Vasut7534a3e2011-10-10 16:34:26 +0100293 /* Key is still pressed */
294 if ((data->new[2] > 3) && (data->old[2] == data->new[2]))
295 res |= usb_kbd_translate(data, data->new[2], data->new[0], 2);
296
Marek Vasutb9300982011-10-10 05:34:25 +0000297 if (res == 1)
wdenkaffae2b2002-08-17 09:36:01 +0000298 usb_kbd_setled(dev);
Marek Vasutb9300982011-10-10 05:34:25 +0000299
Marek Vasut7534a3e2011-10-10 16:34:26 +0100300 memcpy(data->old, data->new, 8);
Marek Vasutb9300982011-10-10 05:34:25 +0000301
Marek Vasut7534a3e2011-10-10 16:34:26 +0100302 return 1;
wdenkaffae2b2002-08-17 09:36:01 +0000303}
304
Marek Vasut7534a3e2011-10-10 16:34:26 +0100305/* Keyboard interrupt handler */
Marek Vasut82cf9a42011-09-25 20:00:37 +0100306static int usb_kbd_irq(struct usb_device *dev)
307{
308 if ((dev->irq_status != 0) || (dev->irq_act_len != 8)) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530309 debug("USB KBD: Error %lX, len %d\n",
310 dev->irq_status, dev->irq_act_len);
Marek Vasut82cf9a42011-09-25 20:00:37 +0100311 return 1;
312 }
313
314 return usb_kbd_irq_worker(dev);
315}
316
Marek Vasut7534a3e2011-10-10 16:34:26 +0100317/* Interrupt polling */
318static inline void usb_kbd_poll_for_event(struct usb_device *dev)
319{
320#if defined(CONFIG_SYS_USB_EVENT_POLL)
amartin@nvidia.com185da952011-12-20 14:56:16 +0000321 struct usb_interface *iface;
322 struct usb_endpoint_descriptor *ep;
323 struct usb_kbd_pdata *data;
324 int pipe;
325 int maxp;
326
327 /* Get the pointer to USB Keyboard device pointer */
328 data = dev->privptr;
329 iface = &dev->config.if_desc[0];
330 ep = &iface->ep_desc[0];
331 pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
332
333 /* Submit a interrupt transfer request */
334 maxp = usb_maxpacket(dev, pipe);
335 usb_submit_int_msg(dev, pipe, &data->new[0],
336 maxp > 8 ? 8 : maxp, ep->bInterval);
337
Marek Vasut7534a3e2011-10-10 16:34:26 +0100338 usb_kbd_irq_worker(dev);
339#elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
340 struct usb_interface *iface;
341 struct usb_kbd_pdata *data = dev->privptr;
342 iface = &dev->config.if_desc[0];
343 usb_get_report(dev, iface->desc.bInterfaceNumber,
Vincent Palatinf3f3a5d2012-01-09 08:35:09 +0000344 1, 0, data->new, sizeof(data->new));
Marek Vasut7534a3e2011-10-10 16:34:26 +0100345 if (memcmp(data->old, data->new, sizeof(data->new)))
346 usb_kbd_irq_worker(dev);
347#endif
348}
349
350/* test if a character is in the queue */
351static int usb_kbd_testc(void)
352{
353 struct stdio_dev *dev;
354 struct usb_device *usb_kbd_dev;
355 struct usb_kbd_pdata *data;
356
Jim Line2ebec82013-08-26 20:21:09 +0800357#ifdef CONFIG_CMD_NET
358 /*
359 * If net_busy_flag is 1, NET transfer is running,
360 * then we check key-pressed every second (first check may be
361 * less than 1 second) to improve TFTP booting performance.
362 */
363 if (net_busy_flag && (get_timer(kbd_testc_tms) < CONFIG_SYS_HZ))
364 return 0;
365 kbd_testc_tms = get_timer(0);
366#endif
Marek Vasut7534a3e2011-10-10 16:34:26 +0100367 dev = stdio_get_by_name(DEVNAME);
368 usb_kbd_dev = (struct usb_device *)dev->priv;
369 data = usb_kbd_dev->privptr;
370
371 usb_kbd_poll_for_event(usb_kbd_dev);
372
373 return !(data->usb_in_pointer == data->usb_out_pointer);
374}
375
376/* gets the character from the queue */
377static int usb_kbd_getc(void)
378{
379 struct stdio_dev *dev;
380 struct usb_device *usb_kbd_dev;
381 struct usb_kbd_pdata *data;
382
383 dev = stdio_get_by_name(DEVNAME);
384 usb_kbd_dev = (struct usb_device *)dev->priv;
385 data = usb_kbd_dev->privptr;
386
387 while (data->usb_in_pointer == data->usb_out_pointer)
388 usb_kbd_poll_for_event(usb_kbd_dev);
389
390 if (data->usb_out_pointer == USB_KBD_BUFFER_LEN - 1)
391 data->usb_out_pointer = 0;
392 else
393 data->usb_out_pointer++;
394
395 return data->usb_kbd_buffer[data->usb_out_pointer];
396}
397
398/* probes the USB device dev for keyboard type. */
wdenkaffae2b2002-08-17 09:36:01 +0000399static int usb_kbd_probe(struct usb_device *dev, unsigned int ifnum)
400{
Tom Rix83b9e1d2009-10-31 12:37:38 -0500401 struct usb_interface *iface;
wdenkaffae2b2002-08-17 09:36:01 +0000402 struct usb_endpoint_descriptor *ep;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100403 struct usb_kbd_pdata *data;
Marek Vasutb9300982011-10-10 05:34:25 +0000404 int pipe, maxp;
wdenkaffae2b2002-08-17 09:36:01 +0000405
Marek Vasutb9300982011-10-10 05:34:25 +0000406 if (dev->descriptor.bNumConfigurations != 1)
407 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100408
wdenkaffae2b2002-08-17 09:36:01 +0000409 iface = &dev->config.if_desc[ifnum];
410
Tom Rix83b9e1d2009-10-31 12:37:38 -0500411 if (iface->desc.bInterfaceClass != 3)
412 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100413
Tom Rix83b9e1d2009-10-31 12:37:38 -0500414 if (iface->desc.bInterfaceSubClass != 1)
415 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100416
Tom Rix83b9e1d2009-10-31 12:37:38 -0500417 if (iface->desc.bInterfaceProtocol != 1)
418 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100419
Tom Rix83b9e1d2009-10-31 12:37:38 -0500420 if (iface->desc.bNumEndpoints != 1)
421 return 0;
wdenkaffae2b2002-08-17 09:36:01 +0000422
423 ep = &iface->ep_desc[0];
424
Marek Vasut7534a3e2011-10-10 16:34:26 +0100425 /* Check if endpoint 1 is interrupt endpoint */
Marek Vasutb9300982011-10-10 05:34:25 +0000426 if (!(ep->bEndpointAddress & 0x80))
427 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100428
Marek Vasutb9300982011-10-10 05:34:25 +0000429 if ((ep->bmAttributes & 3) != 3)
430 return 0;
Marek Vasut7534a3e2011-10-10 16:34:26 +0100431
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530432 debug("USB KBD: found set protocol...\n");
Marek Vasut7534a3e2011-10-10 16:34:26 +0100433
434 data = malloc(sizeof(struct usb_kbd_pdata));
435 if (!data) {
436 printf("USB KBD: Error allocating private data\n");
437 return 0;
438 }
439
440 /* Clear private data */
441 memset(data, 0, sizeof(struct usb_kbd_pdata));
442
Allen Martinef769ca2012-10-24 08:32:04 +0000443 /* allocate input buffer aligned and sized to USB DMA alignment */
444 data->new = memalign(USB_DMA_MINALIGN, roundup(8, USB_DMA_MINALIGN));
445
Marek Vasut7534a3e2011-10-10 16:34:26 +0100446 /* Insert private data into USB device structure */
447 dev->privptr = data;
448
449 /* Set IRQ handler */
450 dev->irq_handle = usb_kbd_irq;
451
wdenkaffae2b2002-08-17 09:36:01 +0000452 pipe = usb_rcvintpipe(dev, ep->bEndpointAddress);
453 maxp = usb_maxpacket(dev, pipe);
Marek Vasut7534a3e2011-10-10 16:34:26 +0100454
455 /* We found a USB Keyboard, install it. */
456 usb_set_protocol(dev, iface->desc.bInterfaceNumber, 0);
457
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530458 debug("USB KBD: found set idle...\n");
Marek Vasut7534a3e2011-10-10 16:34:26 +0100459 usb_set_idle(dev, iface->desc.bInterfaceNumber, REPEAT_RATE, 0);
460
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530461 debug("USB KBD: enable interrupt pipe...\n");
Vincent Palatin0eab9982013-05-10 19:48:59 -0700462 if (usb_submit_int_msg(dev, pipe, data->new, maxp > 8 ? 8 : maxp,
463 ep->bInterval) < 0) {
464 printf("Failed to get keyboard state from device %04x:%04x\n",
465 dev->descriptor.idVendor, dev->descriptor.idProduct);
466 /* Abort, we don't want to use that non-functional keyboard. */
467 return 0;
468 }
Marek Vasut7534a3e2011-10-10 16:34:26 +0100469
470 /* Success. */
wdenkaffae2b2002-08-17 09:36:01 +0000471 return 1;
472}
473
Marek Vasut7534a3e2011-10-10 16:34:26 +0100474/* Search for keyboard and register it if found. */
475int drv_usb_kbd_init(void)
476{
477 struct stdio_dev usb_kbd_dev, *old_dev;
478 struct usb_device *dev;
479 char *stdinname = getenv("stdin");
480 int error, i;
481
482 /* Scan all USB Devices */
483 for (i = 0; i < USB_MAX_DEVICE; i++) {
484 /* Get USB device. */
485 dev = usb_get_dev_index(i);
486 if (!dev)
487 return -1;
488
489 if (dev->devnum == -1)
490 continue;
491
492 /* Try probing the keyboard */
493 if (usb_kbd_probe(dev, 0) != 1)
494 continue;
495
496 /* We found a keyboard, check if it is already registered. */
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530497 debug("USB KBD: found set up device.\n");
Marek Vasut7534a3e2011-10-10 16:34:26 +0100498 old_dev = stdio_get_by_name(DEVNAME);
499 if (old_dev) {
500 /* Already registered, just return ok. */
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530501 debug("USB KBD: is already registered.\n");
Vincent Palatin4d6839f2013-05-10 19:48:58 -0700502 usb_kbd_deregister();
Marek Vasut7534a3e2011-10-10 16:34:26 +0100503 return 1;
504 }
505
506 /* Register the keyboard */
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530507 debug("USB KBD: register.\n");
Marek Vasut7534a3e2011-10-10 16:34:26 +0100508 memset(&usb_kbd_dev, 0, sizeof(struct stdio_dev));
509 strcpy(usb_kbd_dev.name, DEVNAME);
510 usb_kbd_dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
511 usb_kbd_dev.putc = NULL;
512 usb_kbd_dev.puts = NULL;
513 usb_kbd_dev.getc = usb_kbd_getc;
514 usb_kbd_dev.tstc = usb_kbd_testc;
515 usb_kbd_dev.priv = (void *)dev;
516 error = stdio_register(&usb_kbd_dev);
517 if (error)
518 return error;
519
amartin@nvidia.comc9d62022011-12-23 10:29:48 +0000520#ifdef CONFIG_CONSOLE_MUX
521 error = iomux_doenv(stdin, stdinname);
522 if (error)
523 return error;
524#else
Marek Vasut7534a3e2011-10-10 16:34:26 +0100525 /* Check if this is the standard input device. */
526 if (strcmp(stdinname, DEVNAME))
527 return 1;
528
529 /* Reassign the console */
530 if (overwrite_console())
531 return 1;
532
533 error = console_assign(stdin, DEVNAME);
534 if (error)
535 return error;
amartin@nvidia.comc9d62022011-12-23 10:29:48 +0000536#endif
Marek Vasut7534a3e2011-10-10 16:34:26 +0100537
538 return 1;
539 }
540
541 /* No USB Keyboard found */
542 return -1;
543}
544
545/* Deregister the keyboard. */
546int usb_kbd_deregister(void)
547{
548#ifdef CONFIG_SYS_STDIO_DEREGISTER
549 return stdio_deregister(DEVNAME);
550#else
551 return 1;
552#endif
553}