1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
/*****************************************************************************
* Copyright (c) 2013 IBM Corporation
* All rights reserved.
* This program and the accompanying materials
* are made available under the terms of the BSD License
* which accompanies this distribution, and is available at
* http://www.opensource.org/licenses/bsd-license.php
*
* Contributors:
* IBM Corporation - initial implementation
*****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <termctrl.h>
#include "usb-core.h"
#include "usb-key.h"
/*
* HID Spec Version 1.11
*/
#define HID_REQ_GET_REPORT 0x01
#define HID_REQ_GET_IDLE 0x02
#define HID_REQ_GET_PROTOCOL 0x03
#define HID_REQ_SET_REPORT 0x09
#define HID_REQ_SET_IDLE 0x0A
#define HID_REQ_SET_PROTOCOL 0x0B
//#define KEY_DEBUG
/* HID SPEC - 7.2.6 Set_Protocol Request */
static int usb_hid_set_protocol(struct usb_dev *dev, uint16_t value)
{
struct usb_dev_req req;
if (!dev)
return false;
req.bmRequestType = REQT_TYPE_CLASS | REQT_REC_INTERFACE | REQT_DIR_OUT;
req.bRequest = HID_REQ_SET_PROTOCOL;
req.wValue = cpu_to_le16(value);
req.wIndex = cpu_to_le16(dev->intf_num);
req.wLength = 0;
return usb_send_ctrl(dev->control, &req, NULL);
}
/* HID SPEC - 7.2.4 Set_Idle Request */
static int usb_hid_set_idle(struct usb_dev *dev, uint16_t ms_delay)
{
struct usb_dev_req req;
if (!dev)
return false;
req.bmRequestType = REQT_TYPE_CLASS | REQT_REC_INTERFACE | REQT_DIR_OUT;
req.bRequest = HID_REQ_SET_IDLE;
req.wValue = cpu_to_le16((ms_delay/4) << 8);
req.wIndex = cpu_to_le16(dev->intf_num);
req.wLength = 0;
return usb_send_ctrl(dev->control, &req, NULL);
}
/* HID SPEC - 7.2.1 Get Report Request */
static int usb_hid_get_report(struct usb_dev *dev, void *data, size_t size)
{
struct usb_dev_req req;
if (!dev)
return false;
req.bmRequestType = REQT_TYPE_CLASS | REQT_REC_INTERFACE | REQT_DIR_IN;
req.bRequest = HID_REQ_GET_REPORT;
req.wIndex = cpu_to_le16(dev->intf_num);
req.wLength = cpu_to_le16((uint16_t)size);
req.wValue = cpu_to_le16(1 << 8);
return usb_send_ctrl(dev->control, &req, data);
}
/* ring buffer with RD/WR indices for key buffering */
static uint8_t keybuf[256]; /* size fixed to byte range ! */
uint8_t r_ptr = 0; /* RD-index for Keyboard-Buffer */
uint8_t w_ptr = 0; /* WR-index for Keyboard-Buffer */
/* variables for LED status */
uint8_t set_leds;
const uint8_t *key_std = NULL;
const uint8_t *key_std_shift = NULL;
/**
* read character from Keyboard-Buffer
*
* @param -
* @return > 0 Keycode
* = 0 if no key available
*/
static int read_key(void)
{
if (r_ptr != w_ptr)
return (int)keybuf[r_ptr++];
else
return false;
}
/**
* Store character into Keyboard-Buffer
*
* @param Key = detected ASCII-Key (> 0)
* @return -
*/
static void write_key(uint8_t key)
{
if ((w_ptr + 1) != r_ptr)
keybuf[w_ptr++] = key;
}
/**
* Convert keyboard usage-ID to ANSI-Code
*
* @param Ctrl=Modifier Byte
* Key =Usage ID from USB Keyboard
* @return -
*/
static void get_char(uint8_t ctrl, uint8_t keypos)
{
uint8_t ch;
#ifdef KEY_DEBUG
printf("pos %02X\n", keypos);
#endif
if (set_leds & LED_CAPS_LOCK) /* is CAPS Lock set ? */
ctrl |= MODIFIER_SHIFT; /* simulate shift */
if (ctrl == 0) {
ch = key_std[keypos];
if (ch != 0)
write_key(ch);
return;
}
if (ctrl & MODIFIER_SHIFT) {
ch = key_std_shift[keypos];
if (ch != 0)
write_key(ch);
return;
}
if (ctrl & MODIFIER_CTRL) {
ch = keycodes_ctrl[keypos];
if (ch != 0)
write_key(ch);
return;
}
if (ctrl == MODIFIER_ALT_GR) {
ch = keycodes_alt_GR[keypos];
if (ch != 0)
write_key(ch);
return;
}
}
static void check_key_code(uint8_t *buf)
{
static uint8_t key_last[6]; /* list of processed keys */
uint8_t i, j, key_pos;
/* set translation table to defaults */
if ((key_std == NULL) || (key_std_shift == NULL)) {
key_std = keycodes_std_US;
key_std_shift = keycodes_shift_US;
}
if (buf[0] & MODIFIER_SHIFT) /* any shift key pressed ? */
set_leds &= ~LED_CAPS_LOCK; /* CAPS-LOCK-LED always off */
i = 2; /* skip modifier byte and reserved byte */
while (i < 8) {
key_pos = buf[i];
if ((key_pos != 0) && (key_pos <= 100)) { /* support for 101 keys */
j = 0;
/* search if already processed */
while ((j < 6) && (key_pos != key_last[j]))
j++;
if (j >= 6) { /* not found (= not processed) */
switch (key_pos) {
case 0x39: /* caps-lock key ? */
case 0x32: /* caps-lock key ? */
set_leds ^= LED_CAPS_LOCK;
break;
case 0x3a: /* F1 */
write_key(0x1b);
write_key(0x5b);
write_key(0x31);
write_key(0x31);
write_key(0x7e);
break;
case 0x3b: /* F2 */
write_key(0x1b);
write_key(0x5b);
write_key(0x31);
write_key(0x32);
write_key(0x7e);
break;
case 0x3c:
write_key(0x1b); /* F3 */
write_key(0x5b);
write_key(0x31);
write_key(0x33);
write_key(0x7e);
break;
case 0x3d:
write_key(0x1b); /* F4 */
write_key(0x5b);
write_key(0x31);
write_key(0x34);
write_key(0x7e);
break;
case 0x3e:
write_key(0x1b); /* F5 */
write_key(0x5b);
write_key(0x31);
write_key(0x35);
write_key(0x7e);
break;
case 0x3f:
write_key(0x1b); /* F6 */
write_key(0x5b);
write_key(0x31);
write_key(0x37);
write_key(0x7e);
break;
case 0x40:
write_key(0x1b); /* F7 */
write_key(0x5b);
write_key(0x31);
write_key(0x38);
write_key(0x7e);
break;
case 0x41:
write_key(0x1b); /* F8 */
write_key(0x5b);
write_key(0x31);
write_key(0x39);
write_key(0x7e);
break;
case 0x42:
write_key(0x1b); /* F9 */
write_key(0x5b);
write_key(0x31);
write_key(0x30);
write_key(0x7e);
break;
case 0x43:
write_key(0x1b); /* F10 */
write_key(0x5b);
write_key(0x31);
write_key(0x31);
write_key(0x7e);
break;
case 0x44:
write_key(0x1b); /* F11 */
write_key(0x5b);
write_key(0x31);
write_key(0x33);
write_key(0x7e);
break;
case 0x45:
write_key(0x1b); /* F12 */
write_key(0x5b);
write_key(0x31);
write_key(0x34);
write_key(0x7e);
break;
case 0x47: /* scroll-lock key ? */
set_leds ^= LED_SCROLL_LOCK;
break;
case 0x49:
write_key(0x1b); /* INS */
write_key(0x5b);
write_key(0x31);
write_key(0x7e);
break;
case 0x4a:
write_key(0x1b); /* HOME */
write_key(0x5b);
write_key(0x32);
write_key(0x7e);
break;
case 0x4b:
write_key(0x1b); /* PgUp */
write_key(0x5b);
write_key(0x33);
write_key(0x7e);
break;
case 0x4c:
write_key(0x1b); /* DEL */
write_key(0x5b);
write_key(0x34);
write_key(0x7e);
break;
case 0x4d:
write_key(0x1b); /* END */
write_key(0x5b);
write_key(0x35);
write_key(0x7e);
break;
case 0x4e:
write_key(0x1b); /* PgDn */
write_key(0x5b);
write_key(0x36);
write_key(0x7e);
break;
case 0x4f:
write_key(0x1b); /* R-Arrow */
write_key(0x5b);
write_key(0x43);
break;
case 0x50:
write_key(0x1b); /* L-Arrow */
write_key(0x5b);
write_key(0x44);
break;
case 0x51:
write_key(0x1b); /* D-Arrow */
write_key(0x5b);
write_key(0x42);
break;
case 0x52:
write_key(0x1b); /* U-Arrow */
write_key(0x5b);
write_key(0x41);
break;
case 0x53: /* num-lock key ? */
set_leds ^= LED_NUM_LOCK;
break;
default:
/* convert key position to ASCII code */
get_char(buf[0], key_pos);
break;
}
}
}
i++;
}
/*****************************************/
/* all keys are processed, create a copy */
/* to flag them as processed */
/*****************************************/
for (i = 2, j = 0; j < 6; i++, j++)
key_last[j] = buf[i]; /* copy all actual keys to last */
}
#define USB_HID_SIZE 128
uint32_t *kbd_buffer;
int usb_hid_kbd_init(struct usb_dev *dev)
{
int i;
uint8_t key[8];
usb_hid_set_protocol(dev, 0);
usb_hid_set_idle(dev, 500);
memset(key, 0, 8);
if (usb_hid_get_report(dev, key, 8))
check_key_code(key);
kbd_buffer = SLOF_dma_alloc(USB_HID_SIZE);
if (!kbd_buffer) {
printf("%s: unable to allocate keyboard buffer\n", __func__);
return false;
}
#ifdef KEY_DEBUG
printf("HID kbd init %d\n", dev->ep_cnt);
#endif
for (i = 0; i < dev->ep_cnt; i++) {
if ((dev->ep[i].bmAttributes & USB_EP_TYPE_MASK)
== USB_EP_TYPE_INTR)
usb_dev_populate_pipe(dev, &dev->ep[i], kbd_buffer, USB_HID_SIZE);
}
return true;
}
int usb_hid_kbd_exit(struct usb_dev *dev)
{
if (dev->intr) {
usb_put_pipe(dev->intr);
dev->intr = NULL;
}
SLOF_dma_free(kbd_buffer, USB_HID_SIZE);
return true;
}
static int usb_poll_key(void *vdev)
{
struct usb_dev *dev = vdev;
uint8_t key[8];
int rc;
memset(key, 0, 8);
rc = usb_poll_intr(dev->intr, key);
if (rc)
check_key_code(key);
return rc;
}
unsigned char usb_key_available(void *dev)
{
if (!dev)
return false;
usb_poll_key(dev);
if (r_ptr != w_ptr)
return true;
else
return false;
}
unsigned char usb_read_keyb(void *vdev)
{
if (!vdev)
return false;
while (usb_poll_key(vdev)) {
/* loop for all pending keys */
}
return read_key();
}
|