blob: 8713cf48c8429e1dc3916d4ff83ac7e99815edf1 [file] [log] [blame]
Patrick Georgid21f68b2008-09-02 16:06:22 +00001/*
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
Jordan Crouse29061a52008-09-11 17:29:00 +000030#include <usb/usb.h>
Patrick Georgi4727c072008-10-16 19:20:51 +000031#include <curses.h>
Patrick Georgid21f68b2008-09-02 16:06:22 +000032
33enum { hid_subclass_none = 0, hid_subclass_boot = 1 };
Patrick Georgi4727c072008-10-16 19:20:51 +000034typedef enum { hid_proto_boot = 0, hid_proto_report = 1 } hid_proto;
Patrick Georgid21f68b2008-09-02 16:06:22 +000035enum { hid_boot_proto_none = 0, hid_boot_proto_keyboard =
36 1, hid_boot_proto_mouse = 2
37};
38static const char *boot_protos[3] = { "(none)", "keyboard", "mouse" };
39enum { GET_REPORT = 0x1, GET_IDLE = 0x2, GET_PROTOCOL = 0x3, SET_REPORT =
40 0x9, SET_IDLE = 0xa, SET_PROTOCOL = 0xb
41};
42
43static void
44usb_hid_destroy (usbdev_t *dev)
45{
Patrick Georgi4727c072008-10-16 19:20:51 +000046 free (dev->data);
Patrick Georgid21f68b2008-09-02 16:06:22 +000047}
48
Patrick Georgi4727c072008-10-16 19:20:51 +000049typedef 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 */
56int count;
57short keybuffer[16];
58
Patrick Georgid21f68b2008-09-02 16:06:22 +000059int keypress;
Patrick Georgi4727c072008-10-16 19:20:51 +000060short 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
Patrick Georgid21f68b2008-09-02 16:06:22 +000082 -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
94static void
95usb_hid_poll (usbdev_t *dev)
96{
Patrick Georgi4727c072008-10-16 19:20:51 +000097 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 }
Patrick Georgid21f68b2008-09-02 16:06:22 +0000117 }
118}
119
Patrick Georgi4727c072008-10-16 19:20:51 +0000120static void
121usb_hid_set_idle (usbdev_t *dev, interface_descriptor_t *interface, u16 duration)
Patrick Georgid21f68b2008-09-02 16:06:22 +0000122{
Patrick Georgi4727c072008-10-16 19:20:51 +0000123 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
134static void
135usb_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);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000146}
147
148void
149usb_hid_init (usbdev_t *dev)
150{
151
Patrick Georgi4727c072008-10-16 19:20:51 +0000152 configuration_descriptor_t *cd = (configuration_descriptor_t*)dev->configuration;
153 interface_descriptor_t *interface = (interface_descriptor_t*)(((char *) cd) + cd->bLength);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000154
155 if (interface->bInterfaceSubClass == hid_subclass_boot) {
156 printf (" supports boot interface..\n");
157 printf (" it's a %s\n",
158 boot_protos[interface->bInterfaceProtocol]);
159 if (interface->bInterfaceProtocol == hid_boot_proto_keyboard) {
Patrick Georgi4727c072008-10-16 19:20:51 +0000160 dev->data = malloc (sizeof (usbhid_inst_t));
161 printf (" configuring...\n");
162 usb_hid_set_protocol(dev, interface, hid_proto_boot);
163 usb_hid_set_idle(dev, interface, 0);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000164 printf (" activating...\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000165
166 // only add here, because we only support boot-keyboard HID devices
Patrick Georgid21f68b2008-09-02 16:06:22 +0000167 dev->destroy = usb_hid_destroy;
168 dev->poll = usb_hid_poll;
Patrick Georgi4727c072008-10-16 19:20:51 +0000169 int i;
170 for (i = 1; i <= dev->num_endp; i++) {
171 if (dev->endpoints[i].endpoint == 0)
172 continue;
173 if (dev->endpoints[i].type != INTERRUPT)
174 continue;
175 if (dev->endpoints[i].direction != IN)
176 continue;
177 break;
178 }
179 /* 20 buffers of 8 bytes, for every 10 msecs */
180 HID_INST(dev)->queue = dev->controller->create_intr_queue (&dev->endpoints[i], 8, 20, 10);
181 count = 0;
182 printf (" configuration done.\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000183 }
184 }
185}
Patrick Georgi4727c072008-10-16 19:20:51 +0000186
187int usbhid_havechar (void)
188{
189 return (count != 0);
190}
191
192int usbhid_getchar (void)
193{
194 if (count == 0) return 0;
195 short ret = keybuffer[0];
196 memmove (keybuffer, keybuffer+1, --count);
197 return ret;
198}