summaryrefslogtreecommitdiff
path: root/payloads/libpayload/drivers/usb/usbhid.c
diff options
context:
space:
mode:
Diffstat (limited to 'payloads/libpayload/drivers/usb/usbhid.c')
-rw-r--r--payloads/libpayload/drivers/usb/usbhid.c163
1 files changed, 112 insertions, 51 deletions
diff --git a/payloads/libpayload/drivers/usb/usbhid.c b/payloads/libpayload/drivers/usb/usbhid.c
index 34e7cdb635..8713cf48c8 100644
--- a/payloads/libpayload/drivers/usb/usbhid.c
+++ b/payloads/libpayload/drivers/usb/usbhid.c
@@ -28,9 +28,10 @@
*/
#include <usb/usb.h>
+#include <curses.h>
enum { hid_subclass_none = 0, hid_subclass_boot = 1 };
-enum { hid_proto_boot = 0, hid_proto_report = 1 };
+typedef enum { hid_proto_boot = 0, hid_proto_report = 1 } hid_proto;
enum { hid_boot_proto_none = 0, hid_boot_proto_keyboard =
1, hid_boot_proto_mouse = 2
};
@@ -42,23 +43,42 @@ enum { GET_REPORT = 0x1, GET_IDLE = 0x2, GET_PROTOCOL = 0x3, SET_REPORT =
static void
usb_hid_destroy (usbdev_t *dev)
{
+ free (dev->data);
}
+typedef struct {
+ void* queue;
+} usbhid_inst_t;
+
+#define HID_INST(dev) ((usbhid_inst_t*)(dev)->data)
+
+/* buffer is global to all keyboard drivers */
+int count;
+short keybuffer[16];
+
int keypress;
-char keymap[256] = {
- -1, -1, -1, -1, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
- 'l',
- 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
- '1', '2',
- '3', '4', '5', '6', '7', '8', '9', '0', '\n', TERM_ESC,
- TERM_BACKSPACE, TERM_TAB, ' ', '-', '=', '[',
- ']', '\\', -1, ';', '\'', '`', ',', '.', '/', -1, -1, -1, -1, -1, -1,
- -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, TERM_HOME, TERM_PPAGE, -1,
- TERM_END, TERM_NPAGE, TERM_RIGHT,
- TERM_LEFT, TERM_DOWN, TERM_UP, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+short keymap[256] = {
+ -1, -1, -1, -1, 'a', 'b', 'c', 'd',
+ 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
+
+ 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
+ 'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
+
+ '3', '4', '5', '6', '7', '8', '9', '0',
+ '\n', '\e', '\b', '\t', ' ', '-', '=', '[',
+
+ ']', '\\', -1, ';', '\'', '`', ',', '.',
+ '/', -1, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5), KEY_F(6),
+
+ KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), KEY_F(11), KEY_F(12), -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+/* 50 */
+ -1, -1, -1, -1, -1, '*', '-', '+',
+ -1, KEY_END, KEY_DOWN, KEY_NPAGE, KEY_LEFT, -1, KEY_RIGHT, KEY_HOME,
+
+ KEY_UP, KEY_PPAGE, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1,
+
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
@@ -74,64 +94,105 @@ char keymap[256] = {
static void
usb_hid_poll (usbdev_t *dev)
{
- char buf[8];
- static int toggle = 0;
- // hardcode to endpoint 1, 8 bytes
- dev->controller->packet (dev, 1, IN, toggle, 8, buf);
- toggle ^= 1;
- // FIXME: manage buf[0]=special keys, too
- keypress = keymap[buf[2]];
- if ((keypress == -1) && (buf[2] != 0)) {
- printf ("%x %x %x %x %x %x %x %x\n", buf[0], buf[1], buf[2],
- buf[3], buf[4], buf[5], buf[6], buf[7]);
+ u8* buf;
+ while ((buf=dev->controller->poll_intr_queue (HID_INST(dev)->queue))) {
+ // FIXME: manage buf[0]=special keys, too
+ int i;
+ keypress = 0;
+ for (i=2; i<9; i++) {
+ if (buf[i] != 0)
+ keypress = keymap[buf[i]];
+ else
+ break;
+ }
+ if ((keypress == -1) && (buf[2] != 0)) {
+ printf ("%x %x %x %x %x %x %x %x\n", buf[0], buf[1], buf[2],
+ buf[3], buf[4], buf[5], buf[6], buf[7]);
+ }
+ if (keypress != -1) {
+ /* ignore key presses if buffer full */
+ if (count < 16)
+ keybuffer[count++] = keypress;
+ }
}
}
-int (*oldhook) (void);
+static void
+usb_hid_set_idle (usbdev_t *dev, interface_descriptor_t *interface, u16 duration)
+{
+ dev_req_t dr;
+ dr.data_dir = host_to_device;
+ dr.req_type = class_type;
+ dr.req_recp = iface_recp;
+ dr.bRequest = SET_IDLE;
+ dr.wValue = (duration >> 2) << 8;
+ dr.wIndex = interface->bInterfaceNumber;
+ dr.wLength = 0;
+ dev->controller->control (dev, OUT, sizeof (dev_req_t), &dr, 0, 0);
+}
-int
-hookfunc (void)
+static void
+usb_hid_set_protocol (usbdev_t *dev, interface_descriptor_t *interface, hid_proto proto)
{
- int key;
- if (oldhook != 0)
- key = oldhook ();
- if (key == -1)
- key = keypress;
- return key;
+ dev_req_t dr;
+ dr.data_dir = host_to_device;
+ dr.req_type = class_type;
+ dr.req_recp = iface_recp;
+ dr.bRequest = SET_PROTOCOL;
+ dr.wValue = proto;
+ dr.wIndex = interface->bInterfaceNumber;
+ dr.wLength = 0;
+ dev->controller->control (dev, OUT, sizeof (dev_req_t), &dr, 0, 0);
}
void
usb_hid_init (usbdev_t *dev)
{
- configuration_descriptor_t *cd = dev->configuration;
- interface_descriptor_t *interface = ((char *) cd) + cd->bLength;
+ configuration_descriptor_t *cd = (configuration_descriptor_t*)dev->configuration;
+ interface_descriptor_t *interface = (interface_descriptor_t*)(((char *) cd) + cd->bLength);
if (interface->bInterfaceSubClass == hid_subclass_boot) {
printf (" supports boot interface..\n");
printf (" it's a %s\n",
boot_protos[interface->bInterfaceProtocol]);
if (interface->bInterfaceProtocol == hid_boot_proto_keyboard) {
+ dev->data = malloc (sizeof (usbhid_inst_t));
+ printf (" configuring...\n");
+ usb_hid_set_protocol(dev, interface, hid_proto_boot);
+ usb_hid_set_idle(dev, interface, 0);
printf (" activating...\n");
- dev_req_t dr;
- // set_protocol(hid_proto_boot)
- dr.data_dir = host_to_device;
- dr.req_type = class_type;
- dr.req_recp = iface_recp;
- dr.bRequest = SET_PROTOCOL;
- dr.wValue = hid_proto_boot;
- dr.wIndex = interface->bInterfaceNumber;
- dr.wLength = 0;
- dev->controller->control (dev, OUT,
- sizeof (dev_req_t), &dr, 0,
- 0);
// only add here, because we only support boot-keyboard HID devices
- // FIXME: make this a real console input driver instead, once the API is there
dev->destroy = usb_hid_destroy;
dev->poll = usb_hid_poll;
- oldhook = getkey_hook;
- getkey_hook = hookfunc;
+ int i;
+ for (i = 1; i <= dev->num_endp; i++) {
+ if (dev->endpoints[i].endpoint == 0)
+ continue;
+ if (dev->endpoints[i].type != INTERRUPT)
+ continue;
+ if (dev->endpoints[i].direction != IN)
+ continue;
+ break;
+ }
+ /* 20 buffers of 8 bytes, for every 10 msecs */
+ HID_INST(dev)->queue = dev->controller->create_intr_queue (&dev->endpoints[i], 8, 20, 10);
+ count = 0;
+ printf (" configuration done.\n");
}
}
}
+
+int usbhid_havechar (void)
+{
+ return (count != 0);
+}
+
+int usbhid_getchar (void)
+{
+ if (count == 0) return 0;
+ short ret = keybuffer[0];
+ memmove (keybuffer, keybuffer+1, --count);
+ return ret;
+}