]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/usb_kbd.c
3 * Denis Peter, MPL AG Switzerland
5 * Part of this source has been derived from the Linux USB
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <stdio_dev.h>
30 #include <asm/byteorder.h>
35 * If overwrite_console returns 1, the stdin, stderr and stdout
36 * are switched to the serial port, else the settings in the
37 * environment are used
39 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
40 extern int overwrite_console(void);
42 int overwrite_console(void)
48 /* Keyboard sampling rate */
49 #define REPEAT_RATE (40 / 4) /* 40msec -> 25cps */
50 #define REPEAT_DELAY 10 /* 10 x REPEAT_RATE = 400msec */
53 #define CAPS_LOCK 0x39
54 #define SCROLL_LOCK 0x47
57 #define LEFT_CNTR (1 << 0)
58 #define LEFT_SHIFT (1 << 1)
59 #define LEFT_ALT (1 << 2)
60 #define LEFT_GUI (1 << 3)
61 #define RIGHT_CNTR (1 << 4)
62 #define RIGHT_SHIFT (1 << 5)
63 #define RIGHT_ALT (1 << 6)
64 #define RIGHT_GUI (1 << 7)
66 /* Size of the keyboard buffer */
67 #define USB_KBD_BUFFER_LEN 0x20
70 #define DEVNAME "usbkbd"
73 static const unsigned char usb_kbd_numkey
[] = {
74 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
75 '\r', 0x1b, '\b', '\t', ' ', '-', '=', '[', ']',
76 '\\', '#', ';', '\'', '`', ',', '.', '/'
78 static const unsigned char usb_kbd_numkey_shifted
[] = {
79 '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
80 '\r', 0x1b, '\b', '\t', ' ', '_', '+', '{', '}',
81 '|', '~', ':', '"', '~', '<', '>', '?'
84 static const unsigned char usb_kbd_num_keypad
[] = {
85 '/', '*', '-', '+', '\r',
86 '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
91 * map arrow keys to ^F/^B ^N/^P, can't really use the proper
92 * ANSI sequence for arrow keys because the queuing code breaks
93 * when a single keypress expands to 3 queue elements
95 static const unsigned char usb_kbd_arrow
[] = {
100 * NOTE: It's important for the NUM, CAPS, SCROLL-lock bits to be in this
101 * order. See usb_kbd_setled() function!
103 #define USB_KBD_NUMLOCK (1 << 0)
104 #define USB_KBD_CAPSLOCK (1 << 1)
105 #define USB_KBD_SCROLLLOCK (1 << 2)
106 #define USB_KBD_CTRL (1 << 3)
108 #define USB_KBD_LEDMASK \
109 (USB_KBD_NUMLOCK | USB_KBD_CAPSLOCK | USB_KBD_SCROLLLOCK)
111 struct usb_kbd_pdata
{
112 uint32_t repeat_delay
;
114 uint32_t usb_in_pointer
;
115 uint32_t usb_out_pointer
;
116 uint8_t usb_kbd_buffer
[USB_KBD_BUFFER_LEN
];
124 /* Generic keyboard event polling. */
125 void usb_kbd_generic_poll(void)
127 struct stdio_dev
*dev
;
128 struct usb_device
*usb_kbd_dev
;
129 struct usb_kbd_pdata
*data
;
130 struct usb_interface
*iface
;
131 struct usb_endpoint_descriptor
*ep
;
135 /* Get the pointer to USB Keyboard device pointer */
136 dev
= stdio_get_by_name(DEVNAME
);
137 usb_kbd_dev
= (struct usb_device
*)dev
->priv
;
138 data
= usb_kbd_dev
->privptr
;
139 iface
= &usb_kbd_dev
->config
.if_desc
[0];
140 ep
= &iface
->ep_desc
[0];
141 pipe
= usb_rcvintpipe(usb_kbd_dev
, ep
->bEndpointAddress
);
143 /* Submit a interrupt transfer request */
144 maxp
= usb_maxpacket(usb_kbd_dev
, pipe
);
145 usb_submit_int_msg(usb_kbd_dev
, pipe
, data
->new,
146 maxp
> 8 ? 8 : maxp
, ep
->bInterval
);
149 /* Puts character in the queue and sets up the in and out pointer. */
150 static void usb_kbd_put_queue(struct usb_kbd_pdata
*data
, char c
)
152 if (data
->usb_in_pointer
== USB_KBD_BUFFER_LEN
- 1) {
153 /* Check for buffer full. */
154 if (data
->usb_out_pointer
== 0)
157 data
->usb_in_pointer
= 0;
159 /* Check for buffer full. */
160 if (data
->usb_in_pointer
== data
->usb_out_pointer
- 1)
163 data
->usb_in_pointer
++;
166 data
->usb_kbd_buffer
[data
->usb_in_pointer
] = c
;
170 * Set the LEDs. Since this is used in the irq routine, the control job is
171 * issued with a timeout of 0. This means, that the job is queued without
172 * waiting for job completion.
174 static void usb_kbd_setled(struct usb_device
*dev
)
176 struct usb_interface
*iface
= &dev
->config
.if_desc
[0];
177 struct usb_kbd_pdata
*data
= dev
->privptr
;
178 uint32_t leds
= data
->flags
& USB_KBD_LEDMASK
;
180 usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
181 USB_REQ_SET_REPORT
, USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
182 0x200, iface
->desc
.bInterfaceNumber
, (void *)&leds
, 1, 0);
185 #define CAPITAL_MASK 0x20
186 /* Translate the scancode in ASCII */
187 static int usb_kbd_translate(struct usb_kbd_pdata
*data
, unsigned char scancode
,
188 unsigned char modifier
, int pressed
)
194 data
->repeat_delay
= 0;
199 data
->repeat_delay
++;
200 if (data
->repeat_delay
< REPEAT_DELAY
)
203 data
->repeat_delay
= REPEAT_DELAY
;
206 /* Alphanumeric values */
207 if ((scancode
> 3) && (scancode
<= 0x1d)) {
208 keycode
= scancode
- 4 + 'a';
210 if (data
->flags
& USB_KBD_CAPSLOCK
)
211 keycode
&= ~CAPITAL_MASK
;
213 if (modifier
& (LEFT_SHIFT
| RIGHT_SHIFT
)) {
214 /* Handle CAPSLock + Shift pressed simultaneously */
215 if (keycode
& CAPITAL_MASK
)
216 keycode
&= ~CAPITAL_MASK
;
218 keycode
|= CAPITAL_MASK
;
222 if ((scancode
> 0x1d) && (scancode
< 0x3a)) {
224 if (modifier
& (LEFT_SHIFT
| RIGHT_SHIFT
))
225 keycode
= usb_kbd_numkey_shifted
[scancode
- 0x1e];
227 keycode
= usb_kbd_numkey
[scancode
- 0x1e];
231 if ((scancode
>= 0x4f) && (scancode
<= 0x52))
232 keycode
= usb_kbd_arrow
[scancode
- 0x4f];
235 if ((scancode
>= 0x54) && (scancode
<= 0x67))
236 keycode
= usb_kbd_num_keypad
[scancode
- 0x54];
238 if (data
->flags
& USB_KBD_CTRL
)
239 keycode
= scancode
- 0x3;
242 if (scancode
== NUM_LOCK
) {
243 data
->flags
^= USB_KBD_NUMLOCK
;
247 if (scancode
== CAPS_LOCK
) {
248 data
->flags
^= USB_KBD_CAPSLOCK
;
251 if (scancode
== SCROLL_LOCK
) {
252 data
->flags
^= USB_KBD_SCROLLLOCK
;
257 /* Report keycode if any */
259 debug("%c", keycode
);
260 usb_kbd_put_queue(data
, keycode
);
266 static uint32_t usb_kbd_service_key(struct usb_device
*dev
, int i
, int up
)
269 struct usb_kbd_pdata
*data
= dev
->privptr
;
281 if ((old
[i
] > 3) && (memscan(new + 2, old
[i
], 6) == new + 8))
282 res
|= usb_kbd_translate(data
, old
[i
], data
->new[0], up
);
287 /* Interrupt service routine */
288 static int usb_kbd_irq_worker(struct usb_device
*dev
)
290 struct usb_kbd_pdata
*data
= dev
->privptr
;
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
;
300 for (i
= 2; i
< 8; i
++) {
301 res
|= usb_kbd_service_key(dev
, i
, 0);
302 res
|= usb_kbd_service_key(dev
, i
, 1);
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);
312 memcpy(data
->old
, data
->new, 8);
317 /* Keyboard interrupt handler */
318 static int usb_kbd_irq(struct usb_device
*dev
)
320 if ((dev
->irq_status
!= 0) || (dev
->irq_act_len
!= 8)) {
321 debug("USB KBD: Error %lX, len %d\n",
322 dev
->irq_status
, dev
->irq_act_len
);
326 return usb_kbd_irq_worker(dev
);
329 /* Interrupt polling */
330 static inline void usb_kbd_poll_for_event(struct usb_device
*dev
)
332 #if defined(CONFIG_SYS_USB_EVENT_POLL)
333 struct usb_interface
*iface
;
334 struct usb_endpoint_descriptor
*ep
;
335 struct usb_kbd_pdata
*data
;
339 /* Get the pointer to USB Keyboard device pointer */
341 iface
= &dev
->config
.if_desc
[0];
342 ep
= &iface
->ep_desc
[0];
343 pipe
= usb_rcvintpipe(dev
, ep
->bEndpointAddress
);
345 /* Submit a interrupt transfer request */
346 maxp
= usb_maxpacket(dev
, pipe
);
347 usb_submit_int_msg(dev
, pipe
, &data
->new[0],
348 maxp
> 8 ? 8 : maxp
, ep
->bInterval
);
350 usb_kbd_irq_worker(dev
);
351 #elif defined(CONFIG_SYS_USB_EVENT_POLL_VIA_CONTROL_EP)
352 struct usb_interface
*iface
;
353 struct usb_kbd_pdata
*data
= dev
->privptr
;
354 iface
= &dev
->config
.if_desc
[0];
355 usb_get_report(dev
, iface
->desc
.bInterfaceNumber
,
356 1, 0, data
->new, sizeof(data
->new));
357 if (memcmp(data
->old
, data
->new, sizeof(data
->new)))
358 usb_kbd_irq_worker(dev
);
362 /* test if a character is in the queue */
363 static int usb_kbd_testc(void)
365 struct stdio_dev
*dev
;
366 struct usb_device
*usb_kbd_dev
;
367 struct usb_kbd_pdata
*data
;
369 dev
= stdio_get_by_name(DEVNAME
);
370 usb_kbd_dev
= (struct usb_device
*)dev
->priv
;
371 data
= usb_kbd_dev
->privptr
;
373 usb_kbd_poll_for_event(usb_kbd_dev
);
375 return !(data
->usb_in_pointer
== data
->usb_out_pointer
);
378 /* gets the character from the queue */
379 static int usb_kbd_getc(void)
381 struct stdio_dev
*dev
;
382 struct usb_device
*usb_kbd_dev
;
383 struct usb_kbd_pdata
*data
;
385 dev
= stdio_get_by_name(DEVNAME
);
386 usb_kbd_dev
= (struct usb_device
*)dev
->priv
;
387 data
= usb_kbd_dev
->privptr
;
389 while (data
->usb_in_pointer
== data
->usb_out_pointer
)
390 usb_kbd_poll_for_event(usb_kbd_dev
);
392 if (data
->usb_out_pointer
== USB_KBD_BUFFER_LEN
- 1)
393 data
->usb_out_pointer
= 0;
395 data
->usb_out_pointer
++;
397 return data
->usb_kbd_buffer
[data
->usb_out_pointer
];
400 /* probes the USB device dev for keyboard type. */
401 static int usb_kbd_probe(struct usb_device
*dev
, unsigned int ifnum
)
403 struct usb_interface
*iface
;
404 struct usb_endpoint_descriptor
*ep
;
405 struct usb_kbd_pdata
*data
;
408 if (dev
->descriptor
.bNumConfigurations
!= 1)
411 iface
= &dev
->config
.if_desc
[ifnum
];
413 if (iface
->desc
.bInterfaceClass
!= 3)
416 if (iface
->desc
.bInterfaceSubClass
!= 1)
419 if (iface
->desc
.bInterfaceProtocol
!= 1)
422 if (iface
->desc
.bNumEndpoints
!= 1)
425 ep
= &iface
->ep_desc
[0];
427 /* Check if endpoint 1 is interrupt endpoint */
428 if (!(ep
->bEndpointAddress
& 0x80))
431 if ((ep
->bmAttributes
& 3) != 3)
434 debug("USB KBD: found set protocol...\n");
436 data
= malloc(sizeof(struct usb_kbd_pdata
));
438 printf("USB KBD: Error allocating private data\n");
442 /* Clear private data */
443 memset(data
, 0, sizeof(struct usb_kbd_pdata
));
445 /* allocate input buffer aligned and sized to USB DMA alignment */
446 data
->new = memalign(USB_DMA_MINALIGN
, roundup(8, USB_DMA_MINALIGN
));
448 /* Insert private data into USB device structure */
451 /* Set IRQ handler */
452 dev
->irq_handle
= usb_kbd_irq
;
454 pipe
= usb_rcvintpipe(dev
, ep
->bEndpointAddress
);
455 maxp
= usb_maxpacket(dev
, pipe
);
457 /* We found a USB Keyboard, install it. */
458 usb_set_protocol(dev
, iface
->desc
.bInterfaceNumber
, 0);
460 debug("USB KBD: found set idle...\n");
461 usb_set_idle(dev
, iface
->desc
.bInterfaceNumber
, REPEAT_RATE
, 0);
463 debug("USB KBD: enable interrupt pipe...\n");
464 if (usb_submit_int_msg(dev
, pipe
, data
->new, maxp
> 8 ? 8 : maxp
,
465 ep
->bInterval
) < 0) {
466 printf("Failed to get keyboard state from device %04x:%04x\n",
467 dev
->descriptor
.idVendor
, dev
->descriptor
.idProduct
);
468 /* Abort, we don't want to use that non-functional keyboard. */
476 /* Search for keyboard and register it if found. */
477 int drv_usb_kbd_init(void)
479 struct stdio_dev usb_kbd_dev
, *old_dev
;
480 struct usb_device
*dev
;
481 char *stdinname
= getenv("stdin");
484 /* Scan all USB Devices */
485 for (i
= 0; i
< USB_MAX_DEVICE
; i
++) {
486 /* Get USB device. */
487 dev
= usb_get_dev_index(i
);
491 if (dev
->devnum
== -1)
494 /* Try probing the keyboard */
495 if (usb_kbd_probe(dev
, 0) != 1)
498 /* We found a keyboard, check if it is already registered. */
499 debug("USB KBD: found set up device.\n");
500 old_dev
= stdio_get_by_name(DEVNAME
);
502 /* Already registered, just return ok. */
503 debug("USB KBD: is already registered.\n");
504 usb_kbd_deregister();
508 /* Register the keyboard */
509 debug("USB KBD: register.\n");
510 memset(&usb_kbd_dev
, 0, sizeof(struct stdio_dev
));
511 strcpy(usb_kbd_dev
.name
, DEVNAME
);
512 usb_kbd_dev
.flags
= DEV_FLAGS_INPUT
| DEV_FLAGS_SYSTEM
;
513 usb_kbd_dev
.putc
= NULL
;
514 usb_kbd_dev
.puts
= NULL
;
515 usb_kbd_dev
.getc
= usb_kbd_getc
;
516 usb_kbd_dev
.tstc
= usb_kbd_testc
;
517 usb_kbd_dev
.priv
= (void *)dev
;
518 error
= stdio_register(&usb_kbd_dev
);
522 #ifdef CONFIG_CONSOLE_MUX
523 error
= iomux_doenv(stdin
, stdinname
);
527 /* Check if this is the standard input device. */
528 if (strcmp(stdinname
, DEVNAME
))
531 /* Reassign the console */
532 if (overwrite_console())
535 error
= console_assign(stdin
, DEVNAME
);
543 /* No USB Keyboard found */
547 /* Deregister the keyboard. */
548 int usb_kbd_deregister(void)
550 #ifdef CONFIG_SYS_STDIO_DEREGISTER
551 return stdio_deregister(DEVNAME
);