1 /*****************************************************************************
2 * Copyright (c) 2013 IBM Corporation
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
21 * HID Spec Version 1.11
24 #define HID_REQ_GET_REPORT 0x01
25 #define HID_REQ_GET_IDLE 0x02
26 #define HID_REQ_GET_PROTOCOL 0x03
27 #define HID_REQ_SET_REPORT 0x09
28 #define HID_REQ_SET_IDLE 0x0A
29 #define HID_REQ_SET_PROTOCOL 0x0B
33 /* HID SPEC - 7.2.6 Set_Protocol Request */
34 static int usb_hid_set_protocol(struct usb_dev *dev, uint16_t value)
36 struct usb_dev_req req;
39 req.bmRequestType = REQT_TYPE_CLASS | REQT_REC_INTERFACE | REQT_DIR_OUT;
40 req.bRequest = HID_REQ_SET_PROTOCOL;
41 req.wValue = cpu_to_le16(value);
42 req.wIndex = cpu_to_le16(dev->intf_num);
44 return usb_send_ctrl(dev->control, &req, NULL);
47 /* HID SPEC - 7.2.4 Set_Idle Request */
48 static int usb_hid_set_idle(struct usb_dev *dev, uint16_t ms_delay)
50 struct usb_dev_req req;
53 req.bmRequestType = REQT_TYPE_CLASS | REQT_REC_INTERFACE | REQT_DIR_OUT;
54 req.bRequest = HID_REQ_SET_IDLE;
55 req.wValue = cpu_to_le16((ms_delay/4) << 8);
56 req.wIndex = cpu_to_le16(dev->intf_num);
58 return usb_send_ctrl(dev->control, &req, NULL);
61 /* HID SPEC - 7.2.1 Get Report Request */
62 static int usb_hid_get_report(struct usb_dev *dev, void *data, size_t size)
64 struct usb_dev_req req;
67 req.bmRequestType = REQT_TYPE_CLASS | REQT_REC_INTERFACE | REQT_DIR_IN;
68 req.bRequest = HID_REQ_GET_REPORT;
69 req.wIndex = cpu_to_le16(dev->intf_num);
71 req.wLength = cpu_to_le16((uint16_t)size);
72 req.wValue = cpu_to_le16(1 << 8);
73 return usb_send_ctrl(dev->control, &req, data);
76 /* ring buffer with RD/WR indices for key buffering */
77 static uint8_t keybuf[256]; /* size fixed to byte range ! */
78 uint8_t r_ptr = 0; /* RD-index for Keyboard-Buffer */
79 uint8_t w_ptr = 0; /* WR-index for Keyboard-Buffer */
81 /* variables for LED status */
83 const uint8_t *key_std = NULL;
84 const uint8_t *key_std_shift = NULL;
87 * read character from Keyboard-Buffer
91 * = 0 if no key available
93 static int read_key(void)
96 return (int)keybuf[r_ptr++];
102 * Store character into Keyboard-Buffer
104 * @param Key = detected ASCII-Key (> 0)
107 static void write_key(uint8_t key)
109 if ((w_ptr + 1) != r_ptr)
110 keybuf[w_ptr++] = key;
114 * Convert keyboard usage-ID to ANSI-Code
116 * @param Ctrl=Modifier Byte
117 * Key =Usage ID from USB Keyboard
120 static void get_char(uint8_t ctrl, uint8_t keypos)
125 printf("pos %02X\n", keypos);
128 if (set_leds & LED_CAPS_LOCK) /* is CAPS Lock set ? */
129 ctrl |= MODIFIER_SHIFT; /* simulate shift */
132 ch = key_std[keypos];
138 if (ctrl & MODIFIER_SHIFT) {
139 ch = key_std_shift[keypos];
145 if (ctrl & MODIFIER_CTRL) {
146 ch = keycodes_ctrl[keypos];
152 if (ctrl == MODIFIER_ALT_GR) {
153 ch = keycodes_alt_GR[keypos];
160 static void check_key_code(uint8_t *buf)
162 static uint8_t key_last[6]; /* list of processed keys */
163 uint8_t i, j, key_pos;
165 /* set translation table to defaults */
166 if ((key_std == NULL) || (key_std_shift == NULL)) {
167 key_std = keycodes_std_US;
168 key_std_shift = keycodes_shift_US;
171 if (buf[0] & MODIFIER_SHIFT) /* any shift key pressed ? */
172 set_leds &= ~LED_CAPS_LOCK; /* CAPS-LOCK-LED always off */
174 i = 2; /* skip modifier byte and reserved byte */
177 if ((key_pos != 0) && (key_pos <= 100)) { /* support for 101 keys */
179 /* search if already processed */
180 while ((j < 6) && (key_pos != key_last[j]))
183 if (j >= 6) { /* not found (= not processed) */
185 case 0x39: /* caps-lock key ? */
186 case 0x32: /* caps-lock key ? */
187 set_leds ^= LED_CAPS_LOCK;
207 write_key(0x1b); /* F3 */
215 write_key(0x1b); /* F4 */
223 write_key(0x1b); /* F5 */
231 write_key(0x1b); /* F6 */
239 write_key(0x1b); /* F7 */
247 write_key(0x1b); /* F8 */
255 write_key(0x1b); /* F9 */
263 write_key(0x1b); /* F10 */
271 write_key(0x1b); /* F11 */
279 write_key(0x1b); /* F12 */
286 case 0x47: /* scroll-lock key ? */
287 set_leds ^= LED_SCROLL_LOCK;
291 write_key(0x1b); /* INS */
298 write_key(0x1b); /* HOME */
305 write_key(0x1b); /* PgUp */
312 write_key(0x1b); /* DEL */
319 write_key(0x1b); /* END */
326 write_key(0x1b); /* PgDn */
333 write_key(0x1b); /* R-Arrow */
339 write_key(0x1b); /* L-Arrow */
345 write_key(0x1b); /* D-Arrow */
351 write_key(0x1b); /* U-Arrow */
356 case 0x53: /* num-lock key ? */
357 set_leds ^= LED_NUM_LOCK;
361 /* convert key position to ASCII code */
362 get_char(buf[0], key_pos);
369 /*****************************************/
370 /* all keys are processed, create a copy */
371 /* to flag them as processed */
372 /*****************************************/
373 for (i = 2, j = 0; j < 6; i++, j++)
374 key_last[j] = buf[i]; /* copy all actual keys to last */
377 #define USB_HID_SIZE 128
378 uint32_t *kbd_buffer;
380 int usb_hid_kbd_init(struct usb_dev *dev)
385 usb_hid_set_protocol(dev, 0);
386 usb_hid_set_idle(dev, 500);
389 if (usb_hid_get_report(dev, key, 8))
392 kbd_buffer = SLOF_dma_alloc(USB_HID_SIZE);
394 printf("%s: unable to allocate keyboard buffer\n", __func__);
399 printf("HID kbd init %d\n", dev->ep_cnt);
401 for (i = 0; i < dev->ep_cnt; i++) {
402 if ((dev->ep[i].bmAttributes & USB_EP_TYPE_MASK)
404 usb_dev_populate_pipe(dev, &dev->ep[i], kbd_buffer, USB_HID_SIZE);
409 int usb_hid_kbd_exit(struct usb_dev *dev)
412 usb_put_pipe(dev->intr);
415 SLOF_dma_free(kbd_buffer, USB_HID_SIZE);
419 static int usb_poll_key(void *vdev)
421 struct usb_dev *dev = vdev;
426 rc = usb_poll_intr(dev->intr, key);
432 unsigned char usb_key_available(void *dev)
444 unsigned char usb_read_keyb(void *vdev)
449 while (usb_poll_key(vdev)) {
450 /* loop for all pending keys */