added simple keycode->char lookup functionality
[ppcskel.git] / usb / drivers / class / hid.c
1 /*
2         ppcskel - a Free Software replacement for the Nintendo/BroadOn bootloader.
3         hid driver
4
5 Copyright (C) 2009     Bernhard Urban <lewurm@gmx.net>
6 Copyright (C) 2009     Sebastian Falbesoner <sebastian.falbesoner@gmail.com>
7
8 # This code is licensed to you under the terms of the GNU GPL, version 2;
9 # see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
10 */
11
12 #include "../../core/core.h"
13 #include "../../core/usb.h"
14 #include "../../usbspec/usb11spec.h"
15 #include "../../../malloc.h"
16 #include "../../../string.h"
17
18 #include "hid.h"
19
20 struct usb_driver hidkb = {
21         .name     = "hidkb",
22         .probe  = usb_hidkb_probe,
23         .check  = usb_hidkb_check,
24         .data     = NULL
25 };
26
27 /*
28  * just using two very simple US layout code translation tables that
29  * are sufficient for providing a getc() C standard library call;
30  * the only non-printable character here in this table is ESCAPE which
31  * has index 0x29 and is zero
32  */
33 unsigned char code_translation_table[2][57] = {
34         { /* unshifted */
35          0,   0,   0,   0,  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
36         'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
37         '3', '4', '5', '6', '7', '8', '9', '0', '\n', 0,  '\r','\t',' ', '-', '=', '[',
38         ']', '\\','\\',';', '\'','`', ',', '.', '/'
39         },
40         { /* shifted */
41          0,   0,   0,   0,  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
42         'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@',
43         '#', '$', '%', '^', '&', '*', '(', ')', '\n', 0,  '\r','\t',' ', '_', '+', '{',
44         '}', '|', '|', ':', '\"','~', '<', '>', '?'
45         }
46 };
47
48
49 void usb_hidkb_init()
50 {
51         usb_register_driver(&hidkb);
52 }
53
54 void usb_hidkb_probe()
55 {
56         struct usb_device *dev;
57         struct element *iterator = core.devices->head;
58         
59         while(iterator != NULL) {
60                 dev = (struct usb_device*)iterator->data;
61                 if(dev == NULL) {
62                         continue;
63                 }
64
65                 if(dev->conf->intf->bInterfaceClass == HID_CLASSCODE &&
66                                 dev->conf->intf->bInterfaceSubClass == 1 && /* keyboard support boot protocol? */
67                                 dev->conf->intf->bInterfaceProtocol == 1) { /* keyboard? */
68                         hidkb.data = (void*) dev;
69                 }
70
71                 iterator=iterator->next;
72         }
73 }
74
75 void usb_hidkb_check()
76 {
77 }
78
79 u8 usb_hidkb_inuse()
80 {
81         return hidkb.data ? 1 : 0;
82 }
83
84 struct kbrep *usb_hidkb_getChars() {
85         struct usb_device *dev = (struct usb_device*) hidkb.data;
86         struct kbrep *ret = (struct kbrep*) malloc(sizeof(struct kbrep));
87
88         memset(ret, 0, 8);
89         s8 epnum = dev->conf->intf->endp->bEndpointAddress & 0xf;
90         (void) usb_interrupt_read(dev, epnum, (u8*) ret, 8, 0);
91         printf("============\nusb_interrupt_read:\n");
92         hexdump((void*)ret, 8);
93
94         return ret;
95 }
96
97 unsigned char usb_hidkb_get_char_from_keycode(u8 keycode, int shifted)
98 {
99         unsigned char result = 0;
100         if (keycode < 57)
101                 result = code_translation_table[!!shifted][keycode];
102         return result;
103 }