1/*
2 * Copyright (c) 2007-2013 ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
8 */
9
10#include <stdlib.h>
11#include <stdio.h>
12#include <string.h>
13
14#include <barrelfish/barrelfish.h>
15#include <barrelfish/nameservice_client.h>
16
17#include <usb/usb.h>
18#include <usb/usb_device.h>
19#include <usb/usb_request.h>
20#include <usb/usb_transfer.h>
21#include <usb/class/usb_hid.h>
22
23#include "usb_keyboard_driver.h"
24#include "usb_keyboard_service.h"
25
26#include "usb_keyboard_keymap.h"
27
28static struct usb_keyboard keyboard;
29
30/*
31 * setup information for the two transfer types needed
32 */
33
34static usb_transfer_setup_t keyboard_tconf[USB_KEYBOARD_NUM_TRANSFERS] = {
35    [USB_KEYBOARD_DATA] = {
36        .type = USB_TYPE_INTR,
37        .interface = 0,
38        .endpoint = USB_ENDPOINT_ADDRESS_ANY, /* any address */
39        .direction = USB_ENDPOINT_DIRECTION_IN,
40        .max_bytes = 0, /* use wMaxPacketSize */
41        .flags = {
42            .short_xfer_ok = 1,
43            .pipe_on_falure = 1,
44            .auto_restart = 1,
45        },
46    },
47};
48
49/**
50 *
51 */
52static void usb_keyboard_put_key(uint32_t key)
53{
54    if (keyboard.input_size < USB_KEYBOARD_IN_BUFSIZE) {
55        keyboard.input[keyboard.input_tail] = key;
56        ++(keyboard.input_size);
57        ++(keyboard.input_tail);
58        if (keyboard.input_tail >= USB_KEYBOARD_IN_BUFSIZE) {
59            keyboard.input_tail = 0;
60        } USB_DEBUG_KBD("buffer: but key %x\n", key);
61    } else {
62        debug_printf("WARNING: input buffer is full\n");
63    }
64}
65
66/**
67 *
68 */
69static int32_t usb_keyboard_get_key(void)
70{
71    int32_t c;
72
73    if (keyboard.input_size == 0) {
74        /* start transfer, if not already started */
75        //usb_transfer_start(keyboard.xferids[USB_KEYBOARD_DATA]);
76    }
77
78    if (keyboard.input_size == 0) {
79        c = -1;
80        USB_DEBUG_KBD("buffer: return no key \n");
81    } else {
82        c = keyboard.input[keyboard.input_head];
83        --(keyboard.input_size);
84        ++(keyboard.input_head);
85        if (keyboard.input_head >= USB_KEYBOARD_IN_BUFSIZE) {
86            keyboard.input_head = 0;
87        } USB_DEBUG_KBD("buffer: return key %x\n", c);
88    }
89    return (c);
90}
91
92static uint32_t usb_keyboard_keyaction(uint32_t keycode, uint32_t up)
93{
94    union usb_keyboard_modifiers *mods = &keyboard.state;
95
96    uint32_t action;
97    struct usb_key_map *key = &usb_keyboard_key_map[keycode];
98
99    uint32_t state = ((mods->shift_l | mods->shift_r) ? 1 : 0)
100            | ((mods->ctrl_l | mods->ctrl_r) ? 2 : 0)
101            | ((mods->alt_l | mods->alt_r) ? 4 : 0);
102
103    if (((key->flgs & 0x1) && mods->clock)
104            || ((key->flgs & 0x02) && mods->nlock)) {
105        state ^= 1;
106    }
107
108    if (up) {
109        USB_DEBUG_KBD(
110                "keyaction release: key = %x, %c\n", key->map[0], key->map[0]);
111        action = keyboard.last_active[keycode];
112        keyboard.last_active[keycode] = NOP;
113
114        switch (action) {
115            case LSH:
116                mods->shift_l = 0;
117                break;
118            case RSH:
119                mods->shift_r = 0;
120                break;
121            case RCTR:
122                mods->ctrl_r = 0;
123                break;
124            case LCTR:
125                mods->ctrl_l = 0;
126                break;
127            case LALT:
128                mods->alt_l = 0;
129                break;
130            case RALT:
131                mods->alt_r = 0;
132                break;
133            case NLK:
134                //mods->nlock ^= 1;
135                break;
136            case SLK:
137                //mods->slock ^= 1;
138                break;
139            case CLK:
140                //mods->clock ^= 1;
141                break;
142            case NOP:
143                /* release of normal key */
144                return (USB_KEYBOARD_KEY_NOKEY);
145        }
146        return (SPCLKEY | RELKEY | action);
147    } else {
148        /* key press */
149        action = key->map[state];
150        USB_DEBUG_KBD(
151                "keyaction release: key = [%x, %x], [%c, %c]\n", key->map[0], key->map[state], key->map[0], key->map[state]);
152
153        if (key->spcl & (0x80 >> state)) {
154            if (keyboard.last_active[keycode] == NOP) {
155                keyboard.last_active[keycode] = action;
156            }
157            if (keyboard.last_active[keycode] != action) {
158                action = NOP;
159            }
160            switch (action) {
161                case NLK:
162                    mods->nlock ^= 1;
163                    break;
164                case SLK:
165                    mods->slock ^= 1;
166                    break;
167                case CLK:
168                    mods->clock ^= 1;
169                    break;
170                case BTAB:
171                    action |= 0x08000000; /* back tab */
172                    break;
173                case LSH:
174                    mods->shift_l = 1;
175                    break;
176                case RSH:
177                    mods->shift_r = 1;
178                    break;
179                case LCTR:
180                    mods->ctrl_l = 1;
181                    break;
182                case RCTR:
183                    mods->ctrl_r = 1;
184                    break;
185                case LALT:
186                    mods->alt_l = 1;
187                    break;
188                case RALT:
189                    mods->alt_r = 1;
190                    break;
191                case NOP:
192                    return (USB_KEYBOARD_KEY_NOKEY);
193                    break;
194                default:
195                    USB_DEBUG_KBD("accents...\n");
196                    break;
197
198            }
199            return (0x80000000 | action);
200        } else {
201            keyboard.last_active[keycode] = NOP;
202            return (action);
203        }
204
205    }
206
207}
208
209/**
210 * \brief
211 */
212static void usb_keyboard_set_leds(void)
213{
214
215    struct usb_device_request req;
216
217    req.bRequest = USB_HID_REQUEST_SET_REPORT;
218    req.bType.direction = USB_REQUEST_WRITE;
219    req.bType.recipient = USB_REQUEST_RECIPIENT_INTERFACE;
220    req.bType.type = USB_REQUEST_TYPE_CLASS;
221    req.wValue = USB_HID_REPORT_OUTPUT << 8;
222
223    req.wIndex = keyboard.usb_iface_number;
224
225    memset(keyboard.buffer, 0, USB_KEYBOARD_BUFSIZE);
226
227    uint8_t report_id = 0;
228
229    if (keyboard.numlock.valid) {
230        if (keyboard.state.nlock) {
231            keyboard.keyboard_led_state.numlock = 1;
232            usb_hid_put_data_unsigned(keyboard.buffer + 1,
233                    USB_KEYBOARD_BUFSIZE - 1, &keyboard.numlock.loc, 1);
234        } else {
235            keyboard.keyboard_led_state.numlock = 1;
236        }
237        report_id = keyboard.numlock.report_id;
238    }
239
240    if (keyboard.scrolllock.valid) {
241        if (keyboard.state.slock) {
242            keyboard.keyboard_led_state.scrolllock = 1;
243            usb_hid_put_data_unsigned(keyboard.buffer + 1,
244                    USB_KEYBOARD_BUFSIZE - 1, &keyboard.scrolllock.loc, 1);
245        } else {
246            keyboard.keyboard_led_state.scrolllock = 1;
247        }
248        report_id = keyboard.scrolllock.report_id;
249    }
250
251    if (keyboard.capslock.valid) {
252        if (keyboard.state.clock) {
253            keyboard.keyboard_led_state.capslock = 1;
254            usb_hid_put_data_unsigned(keyboard.buffer + 1,
255                    USB_KEYBOARD_BUFSIZE - 1, &keyboard.capslock.loc, 1);
256        } else {
257            keyboard.keyboard_led_state.capslock = 0;
258        }
259        report_id = keyboard.capslock.report_id;
260    }
261
262    uint16_t len = keyboard.keyboard_led_size;
263    /* check if we don't have too much LEDs set on */
264    if (len > (USB_KEYBOARD_BUFSIZE - 1)) {
265        len = (USB_KEYBOARD_BUFSIZE - 1);
266    }
267
268    keyboard.buffer[0] = report_id;
269    void *data;
270    if (report_id != 0) {
271        len++;
272        data = (void *) (keyboard.buffer);
273    } else {
274        data = (void *) (&keyboard.buffer[1]);
275    }
276
277    req.wLength = len;
278
279    usb_error_t err = usb_do_request_write(&req, len, data);
280
281    if (err != USB_ERR_OK) {
282        debug_printf("WARNING: set LED request not executed propperly: %s\n",
283                usb_get_error_string(err));
284    }
285
286}
287
288#if USB_KEYBOARD_MODE_ATCODE
289static int32_t usb_keyboard_atcode(uint32_t keycode, int32_t up)
290{
291    static const int scan[] = {
292        /* 89 */
293        0x11c, /* Enter */
294        /* 90-99 */
295        0x11d, /* Ctrl-R */
296        0x135, /* Divide */
297        0x137 | 0x800, /* PrintScreen */
298        0x138, /* Alt-R */
299        0x147, /* Home */
300        0x148, /* Up */
301        0x149, /* PageUp */
302        0x14b, /* Left */
303        0x14d, /* Right */
304        0x14f, /* End */
305        /* 100-109 */
306        0x150, /* Down */
307        0x151, /* PageDown */
308        0x152, /* Insert */
309        0x153, /* Delete */
310        0x146, /* XXX Pause/Break */
311        0x15b, /* Win_L(Super_L) */
312        0x15c, /* Win_R(Super_R) */
313        0x15d, /* Application(Menu) */
314
315        /* SUN TYPE 6 USB KEYBOARD */
316        0x168, /* Sun Type 6 Help */
317        0x15e, /* Sun Type 6 Stop */
318        /* 110 - 119 */
319        0x15f, /* Sun Type 6 Again */
320        0x160, /* Sun Type 6 Props */
321        0x161, /* Sun Type 6 Undo */
322        0x162, /* Sun Type 6 Front */
323        0x163, /* Sun Type 6 Copy */
324        0x164, /* Sun Type 6 Open */
325        0x165, /* Sun Type 6 Paste */
326        0x166, /* Sun Type 6 Find */
327        0x167, /* Sun Type 6 Cut */
328        0x125, /* Sun Type 6 Mute */
329        /* 120 - 128 */
330        0x11f, /* Sun Type 6 VolumeDown */
331        0x11e, /* Sun Type 6 VolumeUp */
332        0x120, /* Sun Type 6 PowerDown */
333
334        /* Japanese 106/109 keyboard */
335        0x73, /* Keyboard Intl' 1 (backslash / underscore) */
336        0x70, /* Keyboard Intl' 2 (Katakana / Hiragana) */
337        0x7d, /* Keyboard Intl' 3 (Yen sign) (Not using in jp106/109) */
338        0x79, /* Keyboard Intl' 4 (Henkan) */
339        0x7b, /* Keyboard Intl' 5 (Muhenkan) */
340        0x5c,
341    /* Keyboard Intl' 6 (Keypad ,) (For PC-9821 layout) */
342    };
343
344    if ((keycode >= 89)
345            && (keycode < (89 + (sizeof(scan) / sizeof(scan[0]))))) {
346        keycode = scan[keycode - 89];
347    }
348    /* Pause/Break */
349    if ((keycode == 104)
350            && (!(keyboard.new_data.modifiers.ctrl_l
351                    | keyboard.new_data.modifiers.ctrl_r))) {
352        keycode = (0x45 | 0x200 | 0x400);
353    }
354    if ((keyboard.new_data.modifiers.shift_l
355            | keyboard.new_data.modifiers.shift_r)) {
356        keycode &= ~0x800;
357    }
358    keycode |= (up ? 0x80 : 0x00);
359
360    if (keycode & 0xF00) {
361        if (keycode & 0x400) {
362            /* Ctrl */
363            keyboard.at_buffered_char[0] = (0x1d | (keycode & 0x80));
364            keyboard.at_buffered_char[1] = (keycode & ~0xF00);
365        } else if (keycode & 0x800) {
366            /* Shift */
367            keyboard.at_buffered_char[0] = (0x2a | (keycode & 0x80));
368            keyboard.at_buffered_char[1] = (keycode & ~0x800);
369        } else {
370            keyboard.at_buffered_char[0] = (keycode & ~0xF00);
371            keyboard.at_buffered_char[1] = 0;
372        }
373        return ((keycode & 0x100) ? 0xe0 : 0xe1);
374    }
375    return (keycode);
376}
377
378static int32_t usb_keyboard_read(void)
379{
380    uint32_t keycode;
381    uint32_t scancode;
382
383    if (keyboard.at_buffered_char[0]) {
384        scancode = keyboard.at_buffered_char[0];
385        if (scancode & 0xF00) {
386            keyboard.at_buffered_char[0] &= ~0xF00;
387            return ((scancode & 0x100) ? 0xE0 : 0xE1);
388        }
389        keyboard.at_buffered_char[0] = keyboard.at_buffered_char[1];
390        keyboard.at_buffered_char[1] = 0;
391        return (scancode);
392    }
393
394    int32_t usbcode = usb_keyboard_get_key();
395
396    if (usbcode == -1) {
397        return (-1);
398    }
399
400    keycode = usb_keyboard_keycodes[usbcode & 0xFF];
401    if (keycode == NN) {
402        return (-1);
403    }
404    return (usb_keyboard_atcode(keycode, usbcode & USB_KEYBOARD_KEY_RELEASE));
405}
406#else
407static int32_t usb_keyboard_read(void)
408{
409    return (-1);
410}
411#endif
412
413static uint32_t usb_keyboard_read_char(void)
414{
415    uint32_t keycode;
416    uint32_t action = USB_KEYBOARD_KEY_NOKEY;
417    int32_t usbcode;
418#if USB_KEYBOARD_MODE_ATCODE
419    uint32_t scancode;
420#endif
421
422    uint8_t error = 0;
423
424    while (1) {
425        /* return composed char */
426        if (keyboard.composed_char > 0 && keyboard.composed_done) {
427            action = keyboard.composed_char;
428            keyboard.composed_char = 0;
429            if (action > 0xFF) {
430                /* invalid char */
431                return (USB_KEYBOARD_KEY_ERROR);
432            }
433            return (action);
434        }
435
436#if USB_KEYBOARD_MODE_ATCODE
437        scancode = keyboard.at_buffered_char[0];
438        if (scancode) {
439            if (scancode & 0xF00) {
440                keyboard.at_buffered_char[0] = (scancode & ~0xF00);
441                return ((scancode & 0x100) ? 0xE0 : 0xE1);
442            }
443            keyboard.at_buffered_char[0] = keyboard.at_buffered_char[1];
444            keyboard.at_buffered_char[1] = 0;
445            return (scancode);
446        }
447#endif
448        usbcode = usb_keyboard_get_key();
449        if (usbcode == -1) {
450            return (USB_KEYBOARD_KEY_NOKEY);
451        }
452
453#if USB_KEYBOARD_MODE_ATCODE
454        keycode = usb_keyboard_keycodes[USB_KEYBOARD_KEY_INDEX(usbcode)];
455        if (keycode == NN) {
456            return (USB_KEYBOARD_KEY_NOKEY);
457        }
458        return (usb_keyboard_atcode(keycode,
459                (usbcode & USB_KEYBOARD_KEY_RELEASE)));
460#else
461        keycode = usb_keyboard_keycodes[USB_KEYBOARD_KEY_INDEX(usbcode)];
462
463        if (keycode == NN) {
464            USB_DEBUG_KBD("read char: no key (NN)\n");
465            return (USB_KEYBOARD_KEY_NOKEY);
466        }
467#endif
468
469        switch (keycode) {
470            case 0x38:
471                /* alt_l */
472                if (usbcode & USB_KEYBOARD_KEY_RELEASE) {
473                    if (!keyboard.composed_done) {
474                        keyboard.composed_done = 1;
475                    }
476                    if (keyboard.composed_char > 0xFF) {
477                        keyboard.composed_char = 0;
478                    }
479                } else {
480                    if (keyboard.composed_done) {
481                        keyboard.composed_done = 0;
482                        keyboard.composed_char = 0;
483                    }
484                }
485                break;
486
487            case 0x5C:
488                /* print screen */
489                keycode = 0x54;
490                break;
491
492            case 0x68:
493                /* pause / break */
494                keycode = 0x6c;
495                break;
496        }
497
498        USB_DEBUG_KBD(
499                "read char: ucode = %x, keycode = %x, released = %x\n", usbcode, keycode, usbcode & USB_KEYBOARD_KEY_RELEASE);
500
501        if (usbcode & USB_KEYBOARD_KEY_RELEASE) {
502            keycode |= USB_KEYBOARD_SCAN_RELEASE;
503        }
504
505        if (!keyboard.composed_done) {
506            switch (keycode) {
507                case 0x47:
508                case 0x48:
509                case 0x49:
510                    /* keypad 7,8,9 */
511                    keyboard.composed_char *= 10;
512                    keyboard.composed_char += keycode - 0x40;
513                    if (keyboard.composed_char > 0xFF) {
514                        error = 1;
515                    }
516                    break;
517                case 0x4B:
518                case 0x4C:
519                case 0x4D:
520                    /* keypad 4,5,6 */
521                    keyboard.composed_char *= 10;
522                    keyboard.composed_char += keycode - 0x47;
523                    if (keyboard.composed_char > 0xFF) {
524                        return (USB_KEYBOARD_KEY_ERROR);
525                    }
526                    break;
527                case 0x4F:
528                case 0x50:
529                case 0x51:
530                    /* keypad 1,2,3 */
531                    keyboard.composed_char *= 10;
532                    keyboard.composed_char += keycode - 0x4E;
533                    if (keyboard.composed_char > 0xFF) {
534                        return (USB_KEYBOARD_KEY_ERROR);
535                    }
536                    break;
537                case 0x52:
538                    keyboard.composed_char *= 10;
539                    if (keyboard.composed_char > 0xFF) {
540                        return (USB_KEYBOARD_KEY_ERROR);
541                    }
542                    break;
543                    /* key released, no interest here */
544                case USB_KEYBOARD_SCAN_RELEASE | 0x47:
545                case USB_KEYBOARD_SCAN_RELEASE | 0x48:
546                case USB_KEYBOARD_SCAN_RELEASE | 0x49: /* keypad 7,8,9 */
547                case USB_KEYBOARD_SCAN_RELEASE | 0x4B:
548                case USB_KEYBOARD_SCAN_RELEASE | 0x4C:
549                case USB_KEYBOARD_SCAN_RELEASE | 0x4D: /* keypad 4,5,6 */
550                case USB_KEYBOARD_SCAN_RELEASE | 0x4F:
551                case USB_KEYBOARD_SCAN_RELEASE | 0x50:
552                case USB_KEYBOARD_SCAN_RELEASE | 0x51: /* keypad 1,2,3 */
553                case USB_KEYBOARD_SCAN_RELEASE | 0x52: /* keypad 0 */
554                    continue;
555                    break;
556                case 0x38:
557                    /* alt_l */
558                    break;
559                default:
560                    if (keyboard.composed_char > 0) {
561                        keyboard.composed_done = 1;
562                        keyboard.composed_char = 0;
563                        return (USB_KEYBOARD_KEY_ERROR);
564                    }
565                    break;
566            }
567        }
568
569        action = usb_keyboard_keyaction(keycode & 0x7F, keycode & 0x80);
570        if (action != USB_KEYBOARD_KEY_NOKEY) {
571            USB_DEBUG_KBD("read char: return code %x\n", action);
572            return (action);
573        }
574    }
575
576    return (USB_KEYBOARD_KEY_NOKEY);
577}
578
579static void usb_keyboard_process_data(void)
580{
581    if (USB_KEYBOARD_MODE_ATCODE) {
582        int32_t sc;
583        while ((sc = usb_keyboard_read()) != -1) {
584            if (sc & 0xF00) {
585                key_event((uint8_t) (0xFF & sc), 1);
586            } else {
587                key_event((uint8_t) (0xFF & sc), 0);
588            }
589        }
590    } else {
591        uint32_t c;
592        char cp[2];
593        while ((c = usb_keyboard_read_char()) != USB_KEYBOARD_KEY_NOKEY) {
594            if (c < 0xFF) {
595                cp[0] = (char) (0xFF & c);
596                if (cp[0] == '\n') {
597                    cp[1] = cp[0];
598                    cp[0] = '\r';
599                    sys_print(cp, 2);
600                } else {
601                    sys_print(cp, 1);
602                }
603                //debug_printf("%c\n", 0xFF & c);
604                printf("f");
605            } else {
606                if ((c & SPCLKEY) && !(c & RELKEY)) {
607                    // debug_printf("special: %x\n", c);
608                    usb_keyboard_set_leds();
609                }
610            }
611        }
612    }
613}
614
615/**
616 * \brief   this function gets called if the data transfer is completed
617 *
618 * \param   err     outcome of the transfer
619 * \param   data    raw data buffer
620 * \param   length  number of bytes in the data buffer
621 *
622 */
623static void usb_keyboard_transfer_cb(usb_error_t err, const void *data_in,
624        uint32_t length)
625{
626    //USB_DEBUG("usb_keyboard_transfer_cb() %u\n", length);
627
628    uint8_t *data = (uint8_t*) data_in;
629
630    //debug_printf("[%x][%x][%x][%x][%x][%x][%x][%x]\n", data[0], data[1], data[2],
631    //        data[3],data[4], data[5], data[6], data[7]);
632
633    if (err != USB_ERR_OK) {
634        debug_printf("WARNING: transfer not completed propperly.\n");
635        return;
636    }
637
638    if (length == 0) {
639        /* no data, do nothing. the transfer is set to auto start... */
640        return;
641    }
642
643    uint8_t rid = 0;
644
645    /*
646     * if a keyboard ID is set, then the data contains a HID ID byte which we
647     * have to remove first
648     */
649    if (keyboard.keyboard_id) {
650        debug_printf("copy out id byte..\n");
651        rid = *((uint8_t *) data);
652        data++;
653        length--;
654        if (length == 0) {
655            /* just the HID ID byte.. no data, */
656            return;
657        }
658    }
659
660    memset(&keyboard.new_data, 0, sizeof(keyboard.old_data));
661    keyboard.modifiers.generic = 0x0000;
662
663    USB_KEYBOARD_MODIFIER_CHECK(ctrl_l);
664    USB_KEYBOARD_MODIFIER_CHECK(ctrl_r);
665    USB_KEYBOARD_MODIFIER_CHECK(shift_l);
666    USB_KEYBOARD_MODIFIER_CHECK(shift_r);
667    USB_KEYBOARD_MODIFIER_CHECK(alt_l);
668    USB_KEYBOARD_MODIFIER_CHECK(alt_r);
669    USB_KEYBOARD_MODIFIER_CHECK(win_r);
670    USB_KEYBOARD_MODIFIER_CHECK(win_l);
671
672    keyboard.new_data.modifiers = keyboard.modifiers;
673
674    if (keyboard.events.valid && (rid == keyboard.events.report_id)) {
675        uint32_t i = keyboard.events.loc.count;
676        if (i > USB_KEYBOARD_KEYCODES) {
677            i = USB_KEYBOARD_KEYCODES;
678        }
679        if (i > length) {
680            i = length;
681        }
682
683        while (i--) {
684            keyboard.new_data.keycode[i] = usb_hid_get_data(&data[i],
685                    length - i, &(keyboard.events.loc));
686        }
687    }
688
689    if (keyboard.new_data.keycode[0] == USB_KEYBOARD_KEY_ERROR) {
690        USB_DEBUG("keyboard key error...");
691        return;
692    }
693
694    union usb_keyboard_modifiers *old_mod = &keyboard.old_data.modifiers;
695    union usb_keyboard_modifiers *new_mod = &keyboard.new_data.modifiers;
696
697    /* check for released modifiers keys */
698    if (old_mod->generic != new_mod->generic) {
699        USB_KEYBOARD_KEY_RELEASE_CHECK(ctrl_l, 0xe0);
700        USB_KEYBOARD_KEY_RELEASE_CHECK(ctrl_r, 0xe4);
701        USB_KEYBOARD_KEY_RELEASE_CHECK(shift_r, 0xe5);
702        USB_KEYBOARD_KEY_RELEASE_CHECK(shift_l, 0xe1);
703        USB_KEYBOARD_KEY_RELEASE_CHECK(alt_l, 0xe2);
704        USB_KEYBOARD_KEY_RELEASE_CHECK(alt_r, 0xe6);
705        USB_KEYBOARD_KEY_RELEASE_CHECK(win_r, 0xe7);
706        USB_KEYBOARD_KEY_RELEASE_CHECK(win_l, 0xe3);
707    }
708
709    uint8_t key;
710    uint8_t found = 0;
711
712    /* check for released keys */
713    for (uint32_t i = 0; i < USB_KEYBOARD_KEYCODES; i++) {
714        key = keyboard.old_data.keycode[i];
715        found = 0;
716        if (key == 0) {
717            continue;
718        }
719        for (uint32_t j = 0; j < USB_KEYBOARD_KEYCODES; j++) {
720            if (keyboard.new_data.keycode[j] == 0) {
721                continue;
722            }
723            if (key == keyboard.new_data.keycode[j]) {
724                /* found the key again, so its not released...*/
725                found = 1;
726                break;
727            }
728        }
729        if (!found) {
730            /* key is released. put key release into buffer */
731            usb_keyboard_put_key(key | USB_KEYBOARD_KEY_RELEASE);
732        }
733    }
734
735    /* check for pressed keys */
736    for (uint32_t i = 0; i < USB_KEYBOARD_KEYCODES; i++) {
737        key = keyboard.new_data.keycode[i];
738        if (key == 0) {
739            continue;
740        }
741        found = 0;
742        for (uint32_t j = 0; j < USB_KEYBOARD_KEYCODES; j++) {
743            if (keyboard.old_data.keycode[j] == 0) {
744                continue;
745            }
746            if (key == keyboard.old_data.keycode[j]) {
747                /* key is still pressed... */
748                found = 1;
749                break;
750            }
751        }
752        if (!found) {
753            usb_keyboard_put_key(key | USB_KEYBOARD_KEY_PRESS);
754        }
755    }
756
757    memcpy(&keyboard.old_data, &keyboard.new_data, sizeof(keyboard.old_data));
758
759    usb_keyboard_process_data();
760}
761
762/*
763 * --------------------------------------------------------------------------
764 * USB Keyboard initialization and de-initialization functions
765 * --------------------------------------------------------------------------
766 */
767
768#define USB_KEYBOARD_PARSE_DEBUG(_str, _key) \
769    USB_DEBUG_HID("%s: pos = %u, count = %u, size = %u, report_id = %u\n",\
770            _str, keyboard._key.loc.position, keyboard._key.loc.count,\
771        keyboard._key.loc.size, keyboard._key.report_id );
772
773/**
774 * \brief   this function parses the HID descriptor and sets some special
775 *          key location values in the keyboard struct
776 *
777 * \param ptr pointer to the hid descriptor
778 * \param len the length of the HID descriptor
779 */
780static void usb_keyboard_parse_hid(const uint8_t *ptr, uint32_t len)
781{
782    uint32_t flags;
783
784    /* check if there is an ID byte */
785    keyboard.keyboard_size = usb_hid_report_size(ptr, len, USB_HID_KIND_INPUT,
786            &keyboard.keyboard_id);
787
788    /* figure out some keys */
789
790    /* key CTRL left */
791    if (usb_hid_locate(ptr, len,
792            USB_HID_USAGE_COMBINE(USB_HID_USAGE_KEYBOARD, 0xE0),
793            USB_HID_KIND_INPUT, 0, &keyboard.ctrl_l.loc, &flags,
794            &keyboard.ctrl_l.report_id)) {
795        if (flags & USB_HID_IO_VARIABLE)
796            keyboard.ctrl_l.valid = 1;
797
798        USB_KEYBOARD_PARSE_DEBUG("ctrl_l ", ctrl_l);
799    }
800
801    /* key CTRL right */
802    if (usb_hid_locate(ptr, len,
803            USB_HID_USAGE_COMBINE(USB_HID_USAGE_KEYBOARD, 0xE4),
804            USB_HID_KIND_INPUT, 0, &keyboard.ctrl_r.loc, &flags,
805            &keyboard.ctrl_r.report_id)) {
806        if (flags & USB_HID_IO_VARIABLE)
807            keyboard.ctrl_r.valid = 1;
808
809        USB_KEYBOARD_PARSE_DEBUG("ctrl_r ", ctrl_r);
810    }
811
812    /* key SHIFT left */
813    if (usb_hid_locate(ptr, len,
814            USB_HID_USAGE_COMBINE(USB_HID_USAGE_KEYBOARD, 0xE1),
815            USB_HID_KIND_INPUT, 0, &keyboard.shift_l.loc, &flags,
816            &keyboard.shift_l.report_id)) {
817        if (flags & USB_HID_IO_VARIABLE)
818            keyboard.shift_l.valid = 1;
819
820        USB_KEYBOARD_PARSE_DEBUG("shift_l", shift_l);
821    }
822
823    /* key SHIFT right */
824    if (usb_hid_locate(ptr, len,
825            USB_HID_USAGE_COMBINE(USB_HID_USAGE_KEYBOARD, 0xE5),
826            USB_HID_KIND_INPUT, 0, &keyboard.shift_r.loc, &flags,
827            &keyboard.shift_r.report_id)) {
828        if (flags & USB_HID_IO_VARIABLE)
829            keyboard.shift_r.valid = 1;
830
831        USB_KEYBOARD_PARSE_DEBUG("shift_r", shift_r);
832    }
833
834    /* key ALT left */
835    if (usb_hid_locate(ptr, len,
836            USB_HID_USAGE_COMBINE(USB_HID_USAGE_KEYBOARD, 0xE2),
837            USB_HID_KIND_INPUT, 0, &keyboard.alt_l.loc, &flags,
838            &keyboard.alt_l.report_id)) {
839        if (flags & USB_HID_IO_VARIABLE)
840            keyboard.alt_l.valid = 1;
841
842        USB_KEYBOARD_PARSE_DEBUG("alt_l  ", alt_l);
843    }
844
845    /* key ALT right */
846    if (usb_hid_locate(ptr, len,
847            USB_HID_USAGE_COMBINE(USB_HID_USAGE_KEYBOARD, 0xE6),
848            USB_HID_KIND_INPUT, 0, &keyboard.alt_r.loc, &flags,
849            &keyboard.alt_r.report_id)) {
850        if (flags & USB_HID_IO_VARIABLE)
851            keyboard.alt_r.valid = 1;
852
853        USB_KEYBOARD_PARSE_DEBUG("alt_r  ", alt_r);
854    }
855
856    /* key WIN left */
857    if (usb_hid_locate(ptr, len,
858            USB_HID_USAGE_COMBINE(USB_HID_USAGE_KEYBOARD, 0xE3),
859            USB_HID_KIND_INPUT, 0, &keyboard.win_l.loc, &flags,
860            &keyboard.win_l.report_id)) {
861        if (flags & USB_HID_IO_VARIABLE)
862            keyboard.win_l.valid = 1;
863
864        USB_KEYBOARD_PARSE_DEBUG("win_l  ", win_l);
865    }
866
867    /* key WIN right */
868    if (usb_hid_locate(ptr, len,
869            USB_HID_USAGE_COMBINE(USB_HID_USAGE_KEYBOARD, 0xE7),
870            USB_HID_KIND_INPUT, 0, &keyboard.win_r.loc, &flags,
871            &keyboard.win_r.report_id)) {
872        if (flags & USB_HID_IO_VARIABLE)
873            keyboard.win_r.valid = 1;
874
875        USB_KEYBOARD_PARSE_DEBUG("win_r  ", win_r);
876    }
877    /* figure out event buffer */
878    if (usb_hid_locate(ptr, len,
879            USB_HID_USAGE_COMBINE(USB_HID_USAGE_KEYBOARD, 0x00),
880            USB_HID_KIND_INPUT, 0, &keyboard.events.loc, &flags,
881            &keyboard.events.report_id)) {
882
883        keyboard.events.valid = 1;
884
885        USB_KEYBOARD_PARSE_DEBUG("events", events);
886
887    }
888
889    /* figure out leds on keyboard */
890    keyboard.keyboard_led_size = usb_hid_report_size(ptr, len,
891            USB_HID_KIND_OUTPUT, NULL);
892
893    if (usb_hid_locate(ptr, len,
894            USB_HID_USAGE_COMBINE(USB_HID_USAGE_LEDS, 0x01),
895            USB_HID_KIND_OUTPUT, 0, &keyboard.numlock.loc, &flags,
896            &keyboard.numlock.report_id)) {
897        if (flags & USB_HID_IO_VARIABLE)
898            keyboard.numlock.valid = 1;
899
900        USB_KEYBOARD_PARSE_DEBUG("lednl ", numlock);
901    }
902    if (usb_hid_locate(ptr, len,
903            USB_HID_USAGE_COMBINE(USB_HID_USAGE_LEDS, 0x02),
904            USB_HID_KIND_OUTPUT, 0, &keyboard.capslock.loc, &flags,
905            &keyboard.capslock.report_id)) {
906        if (flags & USB_HID_IO_VARIABLE)
907            keyboard.capslock.valid = 1;
908
909        USB_KEYBOARD_PARSE_DEBUG("ledcs ", capslock);
910    }
911    if (usb_hid_locate(ptr, len,
912            USB_HID_USAGE_COMBINE(USB_HID_USAGE_LEDS, 0x03),
913            USB_HID_KIND_OUTPUT, 0, &keyboard.scrolllock.loc, &flags,
914            &keyboard.scrolllock.report_id)) {
915        if (flags & USB_HID_IO_VARIABLE)
916            keyboard.scrolllock.valid = 1;
917
918        USB_KEYBOARD_PARSE_DEBUG("ledsl", scrolllock);
919    }
920}
921
922/**
923 * \brief   initializes the USB keyboard
924 */
925usb_error_t usb_keyboard_init(void)
926{
927    USB_DEBUG("usb_keyboard_init()...\n");
928
929    memset(&keyboard, 0, sizeof(struct usb_keyboard));
930
931    /*
932     * The HID class uses the standard request Get_Descriptor as described in
933     * the USB Specification. When a Get_Descriptor(Configuration) request is
934     * issued, it returns the Configuration descriptor, all Interface
935     * descriptors, all Endpoint descriptors, and the HID descriptor for each
936     * interface.
937     */
938
939    keyboard.num_config = usb_device_get_num_config();
940
941    struct usb_interface *iface = usb_device_get_iface(0);
942
943    if (iface != NULL && iface->iface_class != USB_HID_CLASS_CODE) {
944        debug_printf("ERROR: device is not HID class..\n");
945        return (USB_ERR_INVAL);
946    }
947
948    if (iface->iface_protocol != USB_HID_PROTOCOL_KEYBOARD) {
949        debug_printf("ERROR: device is not a keyboard");
950        return (USB_ERR_INVAL);
951    }
952
953    /*
954     * setting up the USB transfers
955     */
956    usb_error_t err = usb_transfer_setup_intr(
957            &keyboard_tconf[USB_KEYBOARD_DATA], usb_keyboard_transfer_cb,
958            &keyboard.xferids[USB_KEYBOARD_DATA]);
959
960    err = usb_transfer_setup_intr(&keyboard_tconf[USB_KEYBOARD_DATA],
961            usb_keyboard_transfer_cb, &keyboard.xferids[USB_KEYBOARD_DATA_2]);
962
963    if (err != USB_ERR_OK) {
964        debug_printf("Failed to setup USB transfer: %s\n",
965                usb_get_error_string(err));
966        return (err);
967    }
968
969    if (err != USB_ERR_OK) {
970        debug_printf("Failed to setup USB transfer: %s\n",
971                usb_get_error_string(err));
972        return (err);
973    }
974
975    struct usb_hid_descriptor *hid_ptr;
976    uint16_t hid_length;
977    err = usb_hid_get_hid_descriptor(&hid_ptr, &hid_length, 0);
978    if (err != USB_ERR_OK) {
979        debug_printf("could not get the HID descriptor: %s\n",
980                usb_get_error_string(err));
981    }
982
983    if (err == USB_ERR_OK) {
984        USB_DEBUG_KBD("Parsing HID descriptor of %d bytes\n", (int16_t)hid_length);
985        usb_keyboard_parse_hid((void *) hid_ptr, hid_length);
986        free(hid_ptr);
987    }
988
989    /* TODO: figure out why it takes so long with interrupts... */
990    err = usb_hid_set_idle(0, 16, 0);
991    if (err != USB_ERR_OK) {
992        USB_DEBUG("NOTICE: setting idle rate failed. (ignored)\n");
993    }
994
995    return (USB_ERR_OK);
996}
997
998usb_error_t usb_keyboard_start_transfers(void)
999{
1000    usb_error_t err;
1001    /* start the interrupt transfer */
1002    err = usb_transfer_start(keyboard.xferids[USB_KEYBOARD_DATA]);
1003    if (err != USB_ERR_OK) {
1004        return (err);
1005    }
1006    err = usb_transfer_start(keyboard.xferids[USB_KEYBOARD_DATA_2]);
1007
1008    return (err);
1009}
1010
1011/**
1012 * \brief deinitializes the transfers upon shutting down the keyboard
1013 *        driver
1014 */
1015void usb_keyboard_deinit(void)
1016{
1017    for (uint32_t i = 0; i < USB_KEYBOARD_NUM_TRANSFERS; i++) {
1018        usb_transfer_unsetup(keyboard.xferids[i]);
1019    }
1020
1021}
1022