2 * This file is part of the libpayload project.
4 * Copyright (C) 2008 coresystems GmbH
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
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
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
44 usb_hid_destroy (usbdev_t *dev)
53 #define HID_INST(dev) ((usbhid_inst_t*)(dev)->data)
55 /* buffer is global to all keyboard drivers */
61 -1, -1, -1, -1, 'a', 'b', 'c', 'd',
62 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
64 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
65 'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
67 '3', '4', '5', '6', '7', '8', '9', '0',
68 '\n', '\e', '\b', '\t', ' ', '-', '=', '[',
70 ']', '\\', -1, ';', '\'', '`', ',', '.',
71 '/', -1, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5), KEY_F(6),
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,
76 -1, -1, -1, -1, -1, '*', '-', '+',
77 -1, KEY_END, KEY_DOWN, KEY_NPAGE, KEY_LEFT, -1, KEY_RIGHT, KEY_HOME,
79 KEY_UP, KEY_PPAGE, -1, -1, -1, -1, -1, -1,
80 -1, -1, -1, -1, -1, -1, -1, -1,
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,
95 usb_hid_poll (usbdev_t *dev)
98 while ((buf=dev->controller->poll_intr_queue (HID_INST(dev)->queue))) {
99 // FIXME: manage buf[0]=special keys, too
102 for (i=2; i<9; i++) {
104 keypress = keymap[buf[i]];
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]);
112 if (keypress != -1) {
113 /* ignore key presses if buffer full */
115 keybuffer[count++] = keypress;
121 usb_hid_set_idle (usbdev_t *dev, interface_descriptor_t *interface, u16 duration)
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;
131 dev->controller->control (dev, OUT, sizeof (dev_req_t), &dr, 0, 0);
135 usb_hid_set_protocol (usbdev_t *dev, interface_descriptor_t *interface, hid_proto proto)
138 dr.data_dir = host_to_device;
139 dr.req_type = class_type;
140 dr.req_recp = iface_recp;
141 dr.bRequest = SET_PROTOCOL;
143 dr.wIndex = interface->bInterfaceNumber;
145 dev->controller->control (dev, OUT, sizeof (dev_req_t), &dr, 0, 0);
149 usb_hid_init (usbdev_t *dev)
152 configuration_descriptor_t *cd = (configuration_descriptor_t*)dev->configuration;
153 interface_descriptor_t *interface = (interface_descriptor_t*)(((char *) cd) + cd->bLength);
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) {
160 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);
164 printf (" activating...\n");
166 // only add here, because we only support boot-keyboard HID devices
167 dev->destroy = usb_hid_destroy;
168 dev->poll = usb_hid_poll;
170 for (i = 1; i <= dev->num_endp; i++) {
171 if (dev->endpoints[i].endpoint == 0)
173 if (dev->endpoints[i].type != INTERRUPT)
175 if (dev->endpoints[i].direction != IN)
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);
182 printf (" configuration done.\n");
187 int usbhid_havechar (void)
192 int usbhid_getchar (void)
194 if (count == 0) return 0;
195 short ret = keybuffer[0];
196 memmove (keybuffer, keybuffer+1, --count);