/* * Logitech Unifying recever emulation. * * Copyright (c) 2014 Peter Wu * * Based on: hw/usb/dev-wacom.c: * Copyright (c) 2006 Openedhand Ltd. * Author: Andrzej Zaborowski * * Based on hw/usb-hid.c: * Copyright (c) 2005 Fabrice Bellard * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include "hw/hw.h" #include "ui/console.h" #include "hw/usb.h" #include "hw/usb/desc.h" #include "dump.h" #if 0 /* Interface requests */ #define LTUNIFY_GET_REPORT 0x2101 #define LTUNIFY_SET_REPORT 0x2109 /* HID interface requests */ #define HID_GET_REPORT 0xa101 #define HID_GET_IDLE 0xa102 #define HID_GET_PROTOCOL 0xa103 #define HID_SET_IDLE 0x210a #define HID_SET_PROTOCOL 0x210b #endif typedef struct USBLtunifyState { USBDevice dev; USBEndpoint *intr; UsbDumpState *usb_dump_state; #if 0 QEMUPutMouseEntry *eh_entry; int dx, dy, dz, buttons_state; int x, y; int mouse_grabbed; enum { LTUNIFY_MODE_HID = 1, LTUNIFY_MODE_LTUNIFY = 2, } mode; uint8_t idle; int changed; #endif } USBLtunifyState; /* descriptors are retrieved with usbhid-dump (need to unbind interfaces from * usbhid) and prettified with hidrd 0.2 */ /* keyboard */ static const uint8_t iface0_hid_report_descriptor[] = { 0x05, 0x01, /* Usage Page (Desktop), */ 0x09, 0x06, /* Usage (Keyboard), */ 0xA1, 0x01, /* Collection (Application), */ 0x05, 0x07, /* Usage Page (Keyboard), */ 0x19, 0xE0, /* Usage Minimum (KB Leftcontrol), */ 0x29, 0xE7, /* Usage Maximum (KB Right GUI), */ 0x15, 0x00, /* Logical Minimum (0), */ 0x25, 0x01, /* Logical Maximum (1), */ 0x75, 0x01, /* Report Size (1), */ 0x95, 0x08, /* Report Count (8), */ 0x81, 0x02, /* Input (Variable), */ 0x81, 0x03, /* Input (Constant, Variable), */ 0x95, 0x05, /* Report Count (5), */ 0x05, 0x08, /* Usage Page (LED), */ 0x19, 0x01, /* Usage Minimum (01h), */ 0x29, 0x05, /* Usage Maximum (05h), */ 0x91, 0x02, /* Output (Variable), */ 0x95, 0x01, /* Report Count (1), */ 0x75, 0x03, /* Report Size (3), */ 0x91, 0x01, /* Output (Constant), */ 0x95, 0x06, /* Report Count (6), */ 0x75, 0x08, /* Report Size (8), */ 0x15, 0x00, /* Logical Minimum (0), */ 0x26, 0xA4, 0x00, /* Logical Maximum (164), */ 0x05, 0x07, /* Usage Page (Keyboard), */ 0x19, 0x00, /* Usage Minimum (None), */ 0x2A, 0xA4, 0x00, /* Usage Maximum (KB ExSel), */ 0x81, 0x00, /* Input, */ 0xC0 /* End Collection */ }; /* mouse, multimedia, power, media center */ static const uint8_t iface1_hid_report_descriptor[] = { 0x05, 0x01, /* Usage Page (Desktop), */ 0x09, 0x02, /* Usage (Mouse), */ 0xA1, 0x01, /* Collection (Application), */ 0x85, 0x02, /* Report ID (2), */ 0x09, 0x01, /* Usage (Pointer), */ 0xA1, 0x00, /* Collection (Physical), */ 0x05, 0x09, /* Usage Page (Button), */ 0x19, 0x01, /* Usage Minimum (01h), */ 0x29, 0x10, /* Usage Maximum (10h), */ 0x15, 0x00, /* Logical Minimum (0), */ 0x25, 0x01, /* Logical Maximum (1), */ 0x95, 0x10, /* Report Count (16), */ 0x75, 0x01, /* Report Size (1), */ 0x81, 0x02, /* Input (Variable), */ 0x05, 0x01, /* Usage Page (Desktop), */ 0x16, 0x01, 0xF8, /* Logical Minimum (-2047), */ 0x26, 0xFF, 0x07, /* Logical Maximum (2047), */ 0x75, 0x0C, /* Report Size (12), */ 0x95, 0x02, /* Report Count (2), */ 0x09, 0x30, /* Usage (X), */ 0x09, 0x31, /* Usage (Y), */ 0x81, 0x06, /* Input (Variable, Relative), */ 0x15, 0x81, /* Logical Minimum (-127), */ 0x25, 0x7F, /* Logical Maximum (127), */ 0x75, 0x08, /* Report Size (8), */ 0x95, 0x01, /* Report Count (1), */ 0x09, 0x38, /* Usage (Wheel), */ 0x81, 0x06, /* Input (Variable, Relative), */ 0x05, 0x0C, /* Usage Page (Consumer), */ 0x0A, 0x38, 0x02, /* Usage (AC Pan), */ 0x95, 0x01, /* Report Count (1), */ 0x81, 0x06, /* Input (Variable, Relative), */ 0xC0, /* End Collection, */ 0xC0, /* End Collection, */ 0x05, 0x0C, /* Usage Page (Consumer), */ 0x09, 0x01, /* Usage (Consumer Control), */ 0xA1, 0x01, /* Collection (Application), */ 0x85, 0x03, /* Report ID (3), */ 0x75, 0x10, /* Report Size (16), */ 0x95, 0x02, /* Report Count (2), */ 0x15, 0x01, /* Logical Minimum (1), */ 0x26, 0x8C, 0x02, /* Logical Maximum (652), */ 0x19, 0x01, /* Usage Minimum (Consumer Control), */ 0x2A, 0x8C, 0x02, /* Usage Maximum (AC Send), */ 0x81, 0x00, /* Input, */ 0xC0, /* End Collection, */ 0x05, 0x01, /* Usage Page (Desktop), */ 0x09, 0x80, /* Usage (Sys Control), */ 0xA1, 0x01, /* Collection (Application), */ 0x85, 0x04, /* Report ID (4), */ 0x75, 0x02, /* Report Size (2), */ 0x95, 0x01, /* Report Count (1), */ 0x15, 0x01, /* Logical Minimum (1), */ 0x25, 0x03, /* Logical Maximum (3), */ 0x09, 0x82, /* Usage (Sys Sleep), */ 0x09, 0x81, /* Usage (Sys Power Down), */ 0x09, 0x83, /* Usage (Sys Wake Up), */ 0x81, 0x60, /* Input (No Preferred, Null State), */ 0x75, 0x06, /* Report Size (6), */ 0x81, 0x03, /* Input (Constant, Variable), */ 0xC0, /* End Collection, */ 0x06, 0xBC, 0xFF, /* Usage Page (FFBCh), */ 0x09, 0x88, /* Usage (88h), */ 0xA1, 0x01, /* Collection (Application), */ 0x85, 0x08, /* Report ID (8), */ 0x19, 0x01, /* Usage Minimum (01h), */ 0x29, 0xFF, /* Usage Maximum (FFh), */ 0x15, 0x01, /* Logical Minimum (1), */ 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ 0x75, 0x08, /* Report Size (8), */ 0x95, 0x01, /* Report Count (1), */ 0x81, 0x00, /* Input, */ 0xC0 /* End Collection */ }; /* vendor specific: short HID++, long HID++, DJ */ static const uint8_t iface2_hid_report_descriptor[] = { 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ 0x09, 0x01, /* Usage (01h), */ 0xA1, 0x01, /* Collection (Application), */ 0x85, 0x10, /* Report ID (16), */ 0x75, 0x08, /* Report Size (8), */ 0x95, 0x06, /* Report Count (6), */ 0x15, 0x00, /* Logical Minimum (0), */ 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ 0x09, 0x01, /* Usage (01h), */ 0x81, 0x00, /* Input, */ 0x09, 0x01, /* Usage (01h), */ 0x91, 0x00, /* Output, */ 0xC0, /* End Collection, */ 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ 0x09, 0x02, /* Usage (02h), */ 0xA1, 0x01, /* Collection (Application), */ 0x85, 0x11, /* Report ID (17), */ 0x75, 0x08, /* Report Size (8), */ 0x95, 0x13, /* Report Count (19), */ 0x15, 0x00, /* Logical Minimum (0), */ 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ 0x09, 0x02, /* Usage (02h), */ 0x81, 0x00, /* Input, */ 0x09, 0x02, /* Usage (02h), */ 0x91, 0x00, /* Output, */ 0xC0, /* End Collection, */ 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ 0x09, 0x04, /* Usage (04h), */ 0xA1, 0x01, /* Collection (Application), */ 0x85, 0x20, /* Report ID (32), */ 0x75, 0x08, /* Report Size (8), */ 0x95, 0x0E, /* Report Count (14), */ 0x15, 0x00, /* Logical Minimum (0), */ 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ 0x09, 0x41, /* Usage (41h), */ 0x81, 0x00, /* Input, */ 0x09, 0x41, /* Usage (41h), */ 0x91, 0x00, /* Output, */ 0x85, 0x21, /* Report ID (33), */ 0x95, 0x1F, /* Report Count (31), */ 0x15, 0x00, /* Logical Minimum (0), */ 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ 0x09, 0x42, /* Usage (42h), */ 0x81, 0x00, /* Input, */ 0x09, 0x42, /* Usage (42h), */ 0x91, 0x00, /* Output, */ 0xC0 /* End Collection */ }; enum { STR_MANUFACTURER = 1, STR_PRODUCT = 2, /* string descriptor 3 is not defined */ STR_CONFIG_FULL = 4, }; static const USBDescStrings desc_strings = { [STR_MANUFACTURER] = "Logitech", [STR_PRODUCT] = "USB Receiver", // Other string: RQR24.00_B0018, bcdDevice = 24.00, bMaxPacketSize0 = 32 [STR_CONFIG_FULL] = "RQR12.01_B0019", }; static const USBDescIface desc_iface_ltunify[] = { { /* DJ disabled: keyboard, DJ enabled: unused */ .bInterfaceNumber = 0, .bNumEndpoints = 1, .bInterfaceClass = USB_CLASS_HID, .bInterfaceSubClass = 0x01, /* boot */ .bInterfaceProtocol = 0x01, /* Keyboard */ .ndesc = 1, .descs = (USBDescOther[]) { { /* HID descriptor */ .data = (uint8_t[]) { 0x09, /* u8 bLength */ 0x21, /* u8 bDescriptorType */ 0x11, 0x01, /* u16 HID_class */ 0x00, /* u8 country_code */ 0x01, /* u8 num_descriptors */ 0x22, /* u8 type: Report */ 59, 0, /* u16 len */ }, }, }, .eps = (USBDescEndpoint[]) { { .bEndpointAddress = USB_DIR_IN | 0x01, .bmAttributes = USB_ENDPOINT_XFER_INT, .wMaxPacketSize = 8, .bInterval = 8, }, }, },{ /* DJ disabled: mouse, multimedia, power, media center. DJ enabled: * unused */ .bInterfaceNumber = 1, .bNumEndpoints = 1, .bInterfaceClass = USB_CLASS_HID, .bInterfaceSubClass = 0x01, /* boot */ .bInterfaceProtocol = 0x02, /* Mouse */ .ndesc = 1, .descs = (USBDescOther[]) { { /* HID descriptor */ .data = (uint8_t[]) { 0x09, /* u8 bLength */ 0x21, /* u8 bDescriptorType */ 0x11, 0x01, /* u16 HID_class */ 0x00, /* u8 country_code */ 0x01, /* u8 num_descriptors */ 0x22, /* u8 type: Report */ 148, 0, /* u16 len */ }, }, }, .eps = (USBDescEndpoint[]) { { .bEndpointAddress = USB_DIR_IN | 0x02, .bmAttributes = USB_ENDPOINT_XFER_INT, .wMaxPacketSize = 8, .bInterval = 2, }, }, },{ /* Short HID++, Long HID++, DJ (if enabled) */ .bInterfaceNumber = 2, .bNumEndpoints = 1, .bInterfaceClass = USB_CLASS_HID, .bInterfaceSubClass = 0x00, /* No Subclass */ .bInterfaceProtocol = 0x00, /* None */ .ndesc = 1, .descs = (USBDescOther[]) { { /* HID descriptor */ .data = (uint8_t[]) { 0x09, /* u8 bLength */ 0x21, /* u8 bDescriptorType */ 0x11, 0x01, /* u16 HID_class */ 0x00, /* u8 country_code */ 0x01, /* u8 num_descriptors */ 0x22, /* u8 type: Report */ 98, 0, /* u16 len */ }, }, }, .eps = (USBDescEndpoint[]) { { .bEndpointAddress = USB_DIR_IN | 0x03, .bmAttributes = USB_ENDPOINT_XFER_INT, .wMaxPacketSize = 0x20, .bInterval = 2, }, }, }, }; static const USBDescDevice desc_device_ltunify = { .bcdUSB = 0x0200, .bMaxPacketSize0 = 8, .bNumConfigurations = 1, .confs = (USBDescConfig[]) { { .bNumInterfaces = 3, .bConfigurationValue = 1, .iConfiguration = STR_CONFIG_FULL, // TODO: USB_CFG_ATT_WAKEUP is reported, but how to handle it? .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP, .bMaxPower = 49, /* 98 mA */ .nif = ARRAY_SIZE(desc_iface_ltunify), .ifs = desc_iface_ltunify, }, }, }; static const USBDesc desc_ltunify = { .id = { .idVendor = 0x046d, .idProduct = 0xc52b, .bcdDevice = 0x1201, .iManufacturer = STR_MANUFACTURER, .iProduct = STR_PRODUCT, .iSerialNumber = 0, /* device does not have such a serial number */ }, .full = &desc_device_ltunify, .str = desc_strings, }; #if 0 static void usb_mouse_event(void *opaque, int dx1, int dy1, int dz1, int buttons_state) { USBLtunifyState *s = opaque; s->dx += dx1; s->dy += dy1; s->dz += dz1; s->buttons_state = buttons_state; s->changed = 1; usb_wakeup(s->intr, 0); } static void usb_ltunify_event(void *opaque, int x, int y, int dz, int buttons_state) { USBLtunifyState *s = opaque; /* scale to Penpartner resolution */ s->x = (x * 5040 / 0x7FFF); s->y = (y * 3780 / 0x7FFF); s->dz += dz; s->buttons_state = buttons_state; s->changed = 1; usb_wakeup(s->intr, 0); } static inline int int_clamp(int val, int vmin, int vmax) { if (val < vmin) return vmin; else if (val > vmax) return vmax; else return val; } static int usb_mouse_poll(USBLtunifyState *s, uint8_t *buf, int len) { int dx, dy, dz, b, l; if (!s->mouse_grabbed) { s->eh_entry = qemu_add_mouse_event_handler(usb_mouse_event, s, 0, "QEMU PenPartner tablet"); qemu_activate_mouse_event_handler(s->eh_entry); s->mouse_grabbed = 1; } dx = int_clamp(s->dx, -128, 127); dy = int_clamp(s->dy, -128, 127); dz = int_clamp(s->dz, -128, 127); s->dx -= dx; s->dy -= dy; s->dz -= dz; b = 0; if (s->buttons_state & MOUSE_EVENT_LBUTTON) b |= 0x01; if (s->buttons_state & MOUSE_EVENT_RBUTTON) b |= 0x02; if (s->buttons_state & MOUSE_EVENT_MBUTTON) b |= 0x04; buf[0] = b; buf[1] = dx; buf[2] = dy; l = 3; if (len >= 4) { buf[3] = dz; l = 4; } return l; } static int usb_ltunify_poll(USBLtunifyState *s, uint8_t *buf, int len) { int b; if (!s->mouse_grabbed) { s->eh_entry = qemu_add_mouse_event_handler(usb_ltunify_event, s, 1, "QEMU PenPartner tablet"); qemu_activate_mouse_event_handler(s->eh_entry); s->mouse_grabbed = 1; } b = 0; if (s->buttons_state & MOUSE_EVENT_LBUTTON) b |= 0x01; if (s->buttons_state & MOUSE_EVENT_RBUTTON) b |= 0x40; if (s->buttons_state & MOUSE_EVENT_MBUTTON) b |= 0x20; /* eraser */ if (len < 7) return 0; buf[0] = s->mode; buf[5] = 0x00 | (b & 0xf0); buf[1] = s->x & 0xff; buf[2] = s->x >> 8; buf[3] = s->y & 0xff; buf[4] = s->y >> 8; if (b & 0x3f) { buf[6] = 0; } else { buf[6] = (unsigned char) -127; } return 7; } static void usb_ltunify_handle_reset(USBDevice *dev) { USBLtunifyState *s = (USBLtunifyState *) dev; s->dx = 0; s->dy = 0; s->dz = 0; s->x = 0; s->y = 0; s->buttons_state = 0; s->mode = LTUNIFY_MODE_HID; } #endif static void usb_ltunify_handle_control(USBDevice *dev, USBPacket *p, int request, int value, int index, int length, uint8_t *data) { USBLtunifyState *s = (USBLtunifyState *) dev; int ret; int desc_len = 0; const uint8_t *desc = NULL; if (s->usb_dump_state) { usb_dump_submit(s->usb_dump_state, p); } /* handle basic requests such as DeviceRequest | USB_REQ_GET_DESCRIPTOR */ ret = usb_desc_handle_control(dev, p, request, value, index, length, data); if (ret >= 0) { goto data_ready; } switch (request) { case InterfaceRequest | USB_REQ_GET_DESCRIPTOR: // look for class descriptor type Report (0x22), descriptor index is // always 0 for class descriptor type other than Physical descriptor. if (value == 0x2200) { // index: interface number switch (index) { case 0: desc = iface0_hid_report_descriptor; desc_len = sizeof(iface0_hid_report_descriptor); break; case 1: desc = iface1_hid_report_descriptor; desc_len = sizeof(iface1_hid_report_descriptor); break; case 2: desc = iface2_hid_report_descriptor; desc_len = sizeof(iface2_hid_report_descriptor); break; } if (desc_len > 0) { if (desc_len > length) { desc_len = length; } memcpy(data, desc, desc_len); p->actual_length = desc_len; } } break; #if 0 case LTUNIFY_SET_REPORT: if (s->mouse_grabbed) { qemu_remove_mouse_event_handler(s->eh_entry); s->mouse_grabbed = 0; } s->mode = data[0]; break; case LTUNIFY_GET_REPORT: data[0] = 0; data[1] = s->mode; p->actual_length = 2; break; /* USB HID requests */ case HID_GET_REPORT: if (s->mode == LTUNIFY_MODE_HID) p->actual_length = usb_mouse_poll(s, data, length); else if (s->mode == LTUNIFY_MODE_LTUNIFY) p->actual_length = usb_ltunify_poll(s, data, length); break; case HID_GET_IDLE: data[0] = s->idle; p->actual_length = 1; break; case HID_SET_IDLE: s->idle = (uint8_t) (value >> 8); break; #endif default: p->status = USB_RET_STALL; break; } data_ready: if (s->usb_dump_state) { usb_dump_complete(s->usb_dump_state, p); } } #if 0 static void usb_ltunify_handle_data(USBDevice *dev, USBPacket *p) { USBLtunifyState *s = (USBLtunifyState *) dev; uint8_t buf[p->iov.size]; int len = 0; switch (p->pid) { case USB_TOKEN_IN: if (p->ep->nr == 1) { if (!(s->changed || s->idle)) { p->status = USB_RET_NAK; return; } s->changed = 0; if (s->mode == LTUNIFY_MODE_HID) len = usb_mouse_poll(s, buf, p->iov.size); else if (s->mode == LTUNIFY_MODE_LTUNIFY) len = usb_ltunify_poll(s, buf, p->iov.size); usb_packet_copy(p, buf, len); break; } /* Fall through. */ case USB_TOKEN_OUT: default: p->status = USB_RET_STALL; } } #endif static void usb_ltunify_handle_destroy(USBDevice *dev) { USBLtunifyState *s = (USBLtunifyState *) dev; #if 0 if (s->mouse_grabbed) { qemu_remove_mouse_event_handler(s->eh_entry); s->mouse_grabbed = 0; } #endif if (s->usb_dump_state) { usb_dump_cleanup(s->usb_dump_state); g_free(s->usb_dump_state); s->usb_dump_state = NULL; } } static int usb_ltunify_initfn(USBDevice *dev) { USBLtunifyState *s = DO_UPCAST(USBLtunifyState, dev, dev); // This makes QEMU create a iSerial (using the serial attribute if // available). Since we hard-code one, do not use it. //usb_desc_create_serial(dev); usb_desc_init(dev); s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1); // WTF is this for: //s->changed = 1; // TODO: API sucks... s->usb_dump_state = usb_dump_init_alloc("/tmp/usbdump.pcap"); return 0; } static const VMStateDescription vmstate_usb_ltunify = { .name = "usb-ltunify", .unmigratable = 1, }; static void usb_ltunify_class_init(ObjectClass *klass, void *data) { DeviceClass *dc = DEVICE_CLASS(klass); USBDeviceClass *uc = USB_DEVICE_CLASS(klass); uc->product_desc = "QEMU Logitech Unifying Receiver"; uc->usb_desc = &desc_ltunify; uc->init = usb_ltunify_initfn; #if 0 uc->handle_reset = usb_ltunify_handle_reset; #endif uc->handle_control = usb_ltunify_handle_control; #if 0 uc->handle_data = usb_ltunify_handle_data; #endif uc->handle_destroy = usb_ltunify_handle_destroy; // uc->handle_attach = usb_desc_attach; ?? found in dev-hid.c set_bit(DEVICE_CATEGORY_INPUT, dc->categories); // TODO: is this mecessary? dc->desc = "QEMU Logitech Unifying Receiver"; dc->vmsd = &vmstate_usb_ltunify; } static const TypeInfo ltunify_info = { .name = "usb-ltunify-receiver", .parent = TYPE_USB_DEVICE, .instance_size = sizeof(USBLtunifyState), .class_init = usb_ltunify_class_init, }; static void usb_ltunify_register_types(void) { type_register_static(<unify_info); usb_legacy_register("usb-ltunify-receiver", "ltunify-receiver", NULL); } type_init(usb_ltunify_register_types)