Fix the USB code to find the headers after they were moved.
[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
32 enum { hid_subclass_none = 0, hid_subclass_boot = 1 };
33 enum { hid_proto_boot = 0, hid_proto_report = 1 };
34 enum { hid_boot_proto_none = 0, hid_boot_proto_keyboard =
35                 1, hid_boot_proto_mouse = 2
36 };
37 static const char *boot_protos[3] = { "(none)", "keyboard", "mouse" };
38 enum { GET_REPORT = 0x1, GET_IDLE = 0x2, GET_PROTOCOL = 0x3, SET_REPORT =
39                 0x9, SET_IDLE = 0xa, SET_PROTOCOL = 0xb
40 };
41
42 static void
43 usb_hid_destroy (usbdev_t *dev)
44 {
45 }
46
47 int keypress;
48 char keymap[256] = {
49         -1, -1, -1, -1, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
50         'l',
51         'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
52         '1', '2',
53         '3', '4', '5', '6', '7', '8', '9', '0', '\n', TERM_ESC,
54         TERM_BACKSPACE, TERM_TAB, ' ', '-', '=', '[',
55         ']', '\\', -1, ';', '\'', '`', ',', '.', '/', -1, -1, -1, -1, -1, -1,
56         -1,
57         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, TERM_HOME, TERM_PPAGE, -1,
58         TERM_END, TERM_NPAGE, TERM_RIGHT,
59         TERM_LEFT, TERM_DOWN, TERM_UP, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60         -1, -1, -1,
61         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
62         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
63         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
64         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
65         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
66         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
67         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
68         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
69         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
70         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
71 };
72
73
74 static void
75 usb_hid_poll (usbdev_t *dev)
76 {
77         char buf[8];
78         static int toggle = 0;
79         // hardcode to endpoint 1, 8 bytes
80         dev->controller->packet (dev, 1, IN, toggle, 8, buf);
81         toggle ^= 1;
82         // FIXME: manage buf[0]=special keys, too
83         keypress = keymap[buf[2]];
84         if ((keypress == -1) && (buf[2] != 0)) {
85                 printf ("%x %x %x %x %x %x %x %x\n", buf[0], buf[1], buf[2],
86                         buf[3], buf[4], buf[5], buf[6], buf[7]);
87         }
88 }
89
90 int (*oldhook) (void);
91
92 int
93 hookfunc (void)
94 {
95         int key;
96         if (oldhook != 0)
97                 key = oldhook ();
98         if (key == -1)
99                 key = keypress;
100         return key;
101 }
102
103 void
104 usb_hid_init (usbdev_t *dev)
105 {
106
107         configuration_descriptor_t *cd = dev->configuration;
108         interface_descriptor_t *interface = ((char *) cd) + cd->bLength;
109
110         if (interface->bInterfaceSubClass == hid_subclass_boot) {
111                 printf ("  supports boot interface..\n");
112                 printf ("  it's a %s\n",
113                         boot_protos[interface->bInterfaceProtocol]);
114                 if (interface->bInterfaceProtocol == hid_boot_proto_keyboard) {
115                         printf ("  activating...\n");
116                         dev_req_t dr;
117                         // set_protocol(hid_proto_boot)
118                         dr.data_dir = host_to_device;
119                         dr.req_type = class_type;
120                         dr.req_recp = iface_recp;
121                         dr.bRequest = SET_PROTOCOL;
122                         dr.wValue = hid_proto_boot;
123                         dr.wIndex = interface->bInterfaceNumber;
124                         dr.wLength = 0;
125                         dev->controller->control (dev, OUT,
126                                                   sizeof (dev_req_t), &dr, 0,
127                                                   0);
128
129                         // only add here, because we only support boot-keyboard HID devices
130                         // FIXME: make this a real console input driver instead, once the API is there
131                         dev->destroy = usb_hid_destroy;
132                         dev->poll = usb_hid_poll;
133                         oldhook = getkey_hook;
134                         getkey_hook = hookfunc;
135                 }
136         }
137 }