Some driver fixes for libpayload:
[coreboot.git] / payloads / libpayload / drivers / usb / usbhid.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2008 coresystems GmbH
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <usb/usb.h>
31 #include <curses.h>
32
33 enum { hid_subclass_none = 0, hid_subclass_boot = 1 };
34 typedef enum { hid_proto_boot = 0, hid_proto_report = 1 } hid_proto;
35 enum { hid_boot_proto_none = 0, hid_boot_proto_keyboard =
36                 1, hid_boot_proto_mouse = 2
37 };
38 static const char *boot_protos[3] = { "(none)", "keyboard", "mouse" };
39 enum { GET_REPORT = 0x1, GET_IDLE = 0x2, GET_PROTOCOL = 0x3, SET_REPORT =
40                 0x9, SET_IDLE = 0xa, SET_PROTOCOL = 0xb
41 };
42
43 static void
44 usb_hid_destroy (usbdev_t *dev)
45 {
46         free (dev->data);
47 }
48
49 typedef struct {
50         void* queue;
51 } usbhid_inst_t;
52
53 #define HID_INST(dev) ((usbhid_inst_t*)(dev)->data)
54
55 /* buffer is global to all keyboard drivers */
56 int count;
57 short keybuffer[16];
58
59 int keypress;
60 short keymap[256] = {
61         -1, -1, -1, -1, 'a', 'b', 'c', 'd',
62         'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
63
64         'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
65         'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
66
67         '3', '4', '5', '6', '7', '8', '9', '0',
68         '\n', '\e', '\b', '\t', ' ', '-', '=', '[',
69
70         ']', '\\', -1, ';', '\'', '`', ',', '.',
71         '/', -1, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5), KEY_F(6),
72
73         KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), KEY_F(11), KEY_F(12), -1, -1,
74         -1, -1, -1, -1, -1, -1, -1, -1,
75 /* 50 */
76         -1, -1, -1, -1, -1, '*', '-', '+',
77         -1, KEY_END, KEY_DOWN, KEY_NPAGE, KEY_LEFT, -1, KEY_RIGHT, KEY_HOME,
78
79         KEY_UP, KEY_PPAGE, -1, -1, -1, -1, -1, -1,
80         -1, -1, -1, -1, -1, -1, -1, -1,
81
82         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
83         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
84         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
85         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
86         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
87         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
88         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
89         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
91 };
92
93
94 static void
95 usb_hid_poll (usbdev_t *dev)
96 {
97         u8* buf;
98         while ((buf=dev->controller->poll_intr_queue (HID_INST(dev)->queue))) {
99                 // FIXME: manage buf[0]=special keys, too
100                 int i;
101                 keypress = 0;
102                 for (i=2; i<9; i++) {
103                         if (buf[i] != 0)
104                                 keypress = keymap[buf[i]];
105                         else
106                                 break;
107                 }
108                 if ((keypress == -1) && (buf[2] != 0)) {
109                         printf ("%x %x %x %x %x %x %x %x\n", buf[0], buf[1], buf[2],
110                                 buf[3], buf[4], buf[5], buf[6], buf[7]);
111                 }
112                 if (keypress != -1) {
113                         /* ignore key presses if buffer full */
114                         if (count < 16)
115                                 keybuffer[count++] = keypress;
116                 }
117         }
118 }
119
120 static void
121 usb_hid_set_idle (usbdev_t *dev, interface_descriptor_t *interface, u16 duration)
122 {
123         dev_req_t dr;
124         dr.data_dir = host_to_device;
125         dr.req_type = class_type;
126         dr.req_recp = iface_recp;
127         dr.bRequest = SET_IDLE;
128         dr.wValue = (duration >> 2) << 8;
129         dr.wIndex = interface->bInterfaceNumber;
130         dr.wLength = 0;
131         dev->controller->control (dev, OUT, sizeof (dev_req_t), &dr, 0, 0);
132 }
133
134 static void
135 usb_hid_set_protocol (usbdev_t *dev, interface_descriptor_t *interface, hid_proto proto)
136 {
137         dev_req_t dr;
138         dr.data_dir = host_to_device;
139         dr.req_type = class_type;
140         dr.req_recp = iface_recp;
141         dr.bRequest = SET_PROTOCOL;
142         dr.wValue = proto;
143         dr.wIndex = interface->bInterfaceNumber;
144         dr.wLength = 0;
145         dev->controller->control (dev, OUT, sizeof (dev_req_t), &dr, 0, 0);
146 }
147
148 static struct console_input_driver cons = {
149         .havekey = usbhid_havechar,
150         .getchar = usbhid_getchar
151 };
152
153 void
154 usb_hid_init (usbdev_t *dev)
155 {
156
157         static int installed = 0;
158         if (!installed) {
159                 installed = 1;
160                 console_add_input_driver (&cons);
161         }
162
163         configuration_descriptor_t *cd = (configuration_descriptor_t*)dev->configuration;
164         interface_descriptor_t *interface = (interface_descriptor_t*)(((char *) cd) + cd->bLength);
165
166         if (interface->bInterfaceSubClass == hid_subclass_boot) {
167                 printf ("  supports boot interface..\n");
168                 printf ("  it's a %s\n",
169                         boot_protos[interface->bInterfaceProtocol]);
170                 if (interface->bInterfaceProtocol == hid_boot_proto_keyboard) {
171                         dev->data = malloc (sizeof (usbhid_inst_t));
172                         printf ("  configuring...\n");
173                         usb_hid_set_protocol(dev, interface, hid_proto_boot);
174                         usb_hid_set_idle(dev, interface, 0);
175                         printf ("  activating...\n");
176
177                         // only add here, because we only support boot-keyboard HID devices
178                         dev->destroy = usb_hid_destroy;
179                         dev->poll = usb_hid_poll;
180                         int i;
181                         for (i = 0; i <= dev->num_endp; i++) {
182                                 if (dev->endpoints[i].endpoint == 0)
183                                         continue;
184                                 if (dev->endpoints[i].type != INTERRUPT)
185                                         continue;
186                                 if (dev->endpoints[i].direction != IN)
187                                         continue;
188                                 break;
189                         }
190                         printf ("  found endpoint %x for interrupt-in\n", i);
191                         /* 20 buffers of 8 bytes, for every 10 msecs */
192                         HID_INST(dev)->queue = dev->controller->create_intr_queue (&dev->endpoints[i], 8, 20, 10);
193                         count = 0;
194                         printf ("  configuration done.\n");
195                 }
196         }
197 }
198
199 int usbhid_havechar (void)
200 {
201         return (count != 0);
202 }
203
204 int usbhid_getchar (void)
205 {
206         if (count == 0) return 0;
207         short ret = keybuffer[0];
208         memmove (keybuffer, keybuffer+1, --count);
209         return ret;
210 }