2 * Copyright (c) 2014 Leon Dang <ldang@nahannisys.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
37 #include <dev/usb/usb.h>
38 #include <dev/usb/usbdi.h>
44 static int umouse_debug = 0;
45 #define DPRINTF(params) if (umouse_debug) printf params
46 #define WPRINTF(params) printf params
48 /* USB endpoint context (1-15) for reporting mouse data events*/
49 #define UMOUSE_INTR_ENDPT 1
51 #define UMOUSE_REPORT_DESC_TYPE 0x22
53 #define UMOUSE_GET_REPORT 0x01
54 #define UMOUSE_GET_IDLE 0x02
55 #define UMOUSE_GET_PROTOCOL 0x03
56 #define UMOUSE_SET_REPORT 0x09
57 #define UMOUSE_SET_IDLE 0x0A
58 #define UMOUSE_SET_PROTOCOL 0x0B
60 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
71 static const char *umouse_desc_strings[] = {
79 struct umouse_hid_descriptor {
81 uint8_t bDescriptorType;
84 uint8_t bNumDescriptors;
85 uint8_t bReportDescriptorType;
86 uint8_t wItemLength[2];
89 struct umouse_config_desc {
90 struct usb_config_descriptor confd;
91 struct usb_interface_descriptor ifcd;
92 struct umouse_hid_descriptor hidd;
93 struct usb_endpoint_descriptor endpd;
94 struct usb_endpoint_ss_comp_descriptor sscompd;
97 #define MOUSE_MAX_X 0x8000
98 #define MOUSE_MAX_Y 0x8000
100 static const uint8_t umouse_report_desc[] = {
101 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
102 0x09, 0x02, /* USAGE (Mouse) */
103 0xa1, 0x01, /* COLLECTION (Application) */
104 0x09, 0x01, /* USAGE (Pointer) */
105 0xa1, 0x00, /* COLLECTION (Physical) */
106 0x05, 0x09, /* USAGE_PAGE (Button) */
107 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
108 0x29, 0x03, /* USAGE_MAXIMUM (Button 3) */
109 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
110 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
111 0x75, 0x01, /* REPORT_SIZE (1) */
112 0x95, 0x03, /* REPORT_COUNT (3) */
113 0x81, 0x02, /* INPUT (Data,Var,Abs); 3 buttons */
114 0x75, 0x05, /* REPORT_SIZE (5) */
115 0x95, 0x01, /* REPORT_COUNT (1) */
116 0x81, 0x03, /* INPUT (Cnst,Var,Abs); padding */
117 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
118 0x09, 0x30, /* USAGE (X) */
119 0x09, 0x31, /* USAGE (Y) */
120 0x35, 0x00, /* PHYSICAL_MINIMUM (0) */
121 0x46, 0xff, 0x7f, /* PHYSICAL_MAXIMUM (0x7fff) */
122 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
123 0x26, 0xff, 0x7f, /* LOGICAL_MAXIMUM (0x7fff) */
124 0x75, 0x10, /* REPORT_SIZE (16) */
125 0x95, 0x02, /* REPORT_COUNT (2) */
126 0x81, 0x02, /* INPUT (Data,Var,Abs) */
127 0x05, 0x01, /* USAGE Page (Generic Desktop) */
128 0x09, 0x38, /* USAGE (Wheel) */
129 0x35, 0x00, /* PHYSICAL_MINIMUM (0) */
130 0x45, 0x00, /* PHYSICAL_MAXIMUM (0) */
131 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
132 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
133 0x75, 0x08, /* REPORT_SIZE (8) */
134 0x95, 0x01, /* REPORT_COUNT (1) */
135 0x81, 0x06, /* INPUT (Data,Var,Rel) */
136 0xc0, /* END_COLLECTION */
137 0xc0 /* END_COLLECTION */
140 struct umouse_report {
141 uint8_t buttons; /* bits: 0 left, 1 right, 2 middle */
142 int16_t x; /* x position */
143 int16_t y; /* y position */
144 int8_t z; /* z wheel position */
148 #define MSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
150 static struct usb_device_descriptor umouse_dev_desc = {
151 .bLength = sizeof(umouse_dev_desc),
152 .bDescriptorType = UDESC_DEVICE,
153 MSETW(.bcdUSB, UD_USB_3_0),
154 .bMaxPacketSize = 8, /* max packet size */
155 MSETW(.idVendor, 0xFB5D), /* vendor */
156 MSETW(.idProduct, 0x0001), /* product */
157 MSETW(.bcdDevice, 0), /* device version */
158 .iManufacturer = UMSTR_MANUFACTURER,
159 .iProduct = UMSTR_PRODUCT,
160 .iSerialNumber = UMSTR_SERIAL,
161 .bNumConfigurations = 1,
164 static struct umouse_config_desc umouse_confd = {
166 .bLength = sizeof(umouse_confd.confd),
167 .bDescriptorType = UDESC_CONFIG,
168 .wTotalLength[0] = sizeof(umouse_confd),
170 .bConfigurationValue = 1,
171 .iConfiguration = UMSTR_CONFIG,
172 .bmAttributes = UC_BUS_POWERED | UC_REMOTE_WAKEUP,
176 .bLength = sizeof(umouse_confd.ifcd),
177 .bDescriptorType = UDESC_INTERFACE,
179 .bInterfaceClass = UICLASS_HID,
180 .bInterfaceSubClass = UISUBCLASS_BOOT,
181 .bInterfaceProtocol = UIPROTO_MOUSE,
184 .bLength = sizeof(umouse_confd.hidd),
185 .bDescriptorType = 0x21,
186 .bcdHID = { 0x01, 0x10 },
188 .bNumDescriptors = 1,
189 .bReportDescriptorType = UMOUSE_REPORT_DESC_TYPE,
190 .wItemLength = { sizeof(umouse_report_desc), 0 },
193 .bLength = sizeof(umouse_confd.endpd),
194 .bDescriptorType = UDESC_ENDPOINT,
195 .bEndpointAddress = UE_DIR_IN | UMOUSE_INTR_ENDPT,
196 .bmAttributes = UE_INTERRUPT,
197 .wMaxPacketSize[0] = 8,
201 .bLength = sizeof(umouse_confd.sscompd),
202 .bDescriptorType = UDESC_ENDPOINT_SS_COMP,
205 MSETW(.wBytesPerInterval, 0),
210 struct umouse_bos_desc {
211 struct usb_bos_descriptor bosd;
212 struct usb_devcap_ss_descriptor usbssd;
216 struct umouse_bos_desc umouse_bosd = {
218 .bLength = sizeof(umouse_bosd.bosd),
219 .bDescriptorType = UDESC_BOS,
220 HSETW(.wTotalLength, sizeof(umouse_bosd)),
224 .bLength = sizeof(umouse_bosd.usbssd),
225 .bDescriptorType = UDESC_DEVICE_CAPABILITY,
226 .bDevCapabilityType = 3,
228 HSETW(.wSpeedsSupported, 0x08),
229 .bFunctionalitySupport = 3,
230 .bU1DevExitLat = 0xa, /* dummy - not used */
231 .wU2DevExitLat = { 0x20, 0x00 },
236 struct umouse_softc {
241 struct umouse_report um_report;
250 pthread_mutex_t ev_mtx;
252 struct timeval prev_evt;
256 umouse_event(uint8_t button, int x, int y, void *arg)
258 struct umouse_softc *sc;
259 struct bhyvegc_image *gc;
261 gc = console_get_image();
269 pthread_mutex_lock(&sc->mtx);
271 sc->um_report.buttons = 0;
275 sc->um_report.buttons |= 0x01; /* left */
277 sc->um_report.buttons |= 0x04; /* middle */
279 sc->um_report.buttons |= 0x02; /* right */
283 sc->um_report.z = -1;
285 /* scale coords to mouse resolution */
286 sc->um_report.x = MOUSE_MAX_X * x / gc->width;
287 sc->um_report.y = MOUSE_MAX_X * y / gc->height;
289 pthread_mutex_unlock(&sc->mtx);
291 pthread_mutex_lock(&sc->ev_mtx);
292 sc->hci->hci_intr(sc->hci, UE_DIR_IN | UMOUSE_INTR_ENDPT);
293 pthread_mutex_unlock(&sc->ev_mtx);
297 umouse_init(struct usb_hci *hci, char *opt)
299 struct umouse_softc *sc;
304 sc = calloc(1, sizeof(struct umouse_softc));
307 sc->hid.protocol = 1; /* REPORT protocol */
308 sc->opt = strdup(opt);
309 pthread_mutex_init(&sc->mtx, NULL);
310 pthread_mutex_init(&sc->ev_mtx, NULL);
312 console_ptr_register(umouse_event, sc, 10);
317 #define UREQ(x,y) ((x) | ((y) << 8))
320 umouse_request(void *scarg, struct usb_data_xfer *xfer)
322 struct umouse_softc *sc;
323 struct usb_data_xfer_block *data;
339 for (i = 0; i < xfer->ndata; i++) {
340 xfer->data[idx].bdone = 0;
341 if (data == NULL && USB_DATA_OK(xfer,i)) {
342 data = &xfer->data[idx];
346 xfer->data[idx].processed = 1;
347 idx = (idx + 1) % USB_MAX_XFER_BLOCKS;
350 err = USB_ERR_NORMAL_COMPLETION;
354 DPRINTF(("umouse_request: port %d\r\n", sc->hci->hci_port));
358 value = UGETW(xfer->ureq->wValue);
359 index = UGETW(xfer->ureq->wIndex);
360 len = UGETW(xfer->ureq->wLength);
362 DPRINTF(("umouse_request: port %d, type 0x%x, req 0x%x, val 0x%x, "
363 "idx 0x%x, len %u\r\n",
364 sc->hci->hci_port, xfer->ureq->bmRequestType,
365 xfer->ureq->bRequest, value, index, len));
367 switch (UREQ(xfer->ureq->bRequest, xfer->ureq->bmRequestType)) {
368 case UREQ(UR_GET_CONFIG, UT_READ_DEVICE):
369 DPRINTF(("umouse: (UR_GET_CONFIG, UT_READ_DEVICE)\r\n"));
373 *udata = umouse_confd.confd.bConfigurationValue;
374 data->blen = len > 0 ? len - 1 : 0;
375 eshort = data->blen > 0;
379 case UREQ(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
380 DPRINTF(("umouse: (UR_GET_DESCRIPTOR, UT_READ_DEVICE) val %x\r\n",
385 switch (value >> 8) {
387 DPRINTF(("umouse: (->UDESC_DEVICE) len %u ?= "
388 "sizeof(umouse_dev_desc) %lu\r\n",
389 len, sizeof(umouse_dev_desc)));
390 if ((value & 0xFF) != 0) {
391 err = USB_ERR_IOERROR;
394 if (len > sizeof(umouse_dev_desc)) {
395 data->blen = len - sizeof(umouse_dev_desc);
396 len = sizeof(umouse_dev_desc);
399 memcpy(data->buf, &umouse_dev_desc, len);
404 DPRINTF(("umouse: (->UDESC_CONFIG)\r\n"));
405 if ((value & 0xFF) != 0) {
406 err = USB_ERR_IOERROR;
409 if (len > sizeof(umouse_confd)) {
410 data->blen = len - sizeof(umouse_confd);
411 len = sizeof(umouse_confd);
415 memcpy(data->buf, &umouse_confd, len);
420 DPRINTF(("umouse: (->UDESC_STRING)\r\n"));
422 if ((value & 0xFF) < UMSTR_MAX)
423 str = umouse_desc_strings[value & 0xFF];
427 if ((value & 0xFF) == UMSTR_LANG) {
429 udata[1] = UDESC_STRING;
430 data->blen = len - 2;
445 slen = 2 + strlen(str) * 2;
447 udata[1] = UDESC_STRING;
450 data->blen = len - slen;
454 for (i = 2; i < len; i += 2) {
463 DPRINTF(("umouse: USB3 BOS\r\n"));
464 if (len > sizeof(umouse_bosd)) {
465 data->blen = len - sizeof(umouse_bosd);
466 len = sizeof(umouse_bosd);
469 memcpy(udata, &umouse_bosd, len);
474 DPRINTF(("umouse: unknown(%d)->ERROR\r\n", value >> 8));
475 err = USB_ERR_IOERROR;
478 eshort = data->blen > 0;
481 case UREQ(UR_GET_DESCRIPTOR, UT_READ_INTERFACE):
482 DPRINTF(("umouse: (UR_GET_DESCRIPTOR, UT_READ_INTERFACE) "
483 "0x%x\r\n", (value >> 8)));
487 switch (value >> 8) {
488 case UMOUSE_REPORT_DESC_TYPE:
489 if (len > sizeof(umouse_report_desc)) {
490 data->blen = len - sizeof(umouse_report_desc);
491 len = sizeof(umouse_report_desc);
494 memcpy(data->buf, umouse_report_desc, len);
498 DPRINTF(("umouse: IO ERROR\r\n"));
499 err = USB_ERR_IOERROR;
502 eshort = data->blen > 0;
505 case UREQ(UR_GET_INTERFACE, UT_READ_INTERFACE):
506 DPRINTF(("umouse: (UR_GET_INTERFACE, UT_READ_INTERFACE)\r\n"));
508 DPRINTF(("umouse get_interface, invalid index %d\r\n",
510 err = USB_ERR_IOERROR;
519 data->blen = len - 1;
521 eshort = data->blen > 0;
525 case UREQ(UR_GET_STATUS, UT_READ_DEVICE):
526 DPRINTF(("umouse: (UR_GET_STATUS, UT_READ_DEVICE)\r\n"));
527 if (data != NULL && len > 1) {
528 if (sc->hid.feature == UF_DEVICE_REMOTE_WAKEUP)
529 USETW(udata, UDS_REMOTE_WAKEUP);
532 data->blen = len - 2;
536 eshort = data->blen > 0;
539 case UREQ(UR_GET_STATUS, UT_READ_INTERFACE):
540 case UREQ(UR_GET_STATUS, UT_READ_ENDPOINT):
541 DPRINTF(("umouse: (UR_GET_STATUS, UT_READ_INTERFACE)\r\n"));
542 if (data != NULL && len > 1) {
544 data->blen = len - 2;
547 eshort = data->blen > 0;
550 case UREQ(UR_SET_ADDRESS, UT_WRITE_DEVICE):
551 /* XXX Controller should've handled this */
552 DPRINTF(("umouse set address %u\r\n", value));
555 case UREQ(UR_SET_CONFIG, UT_WRITE_DEVICE):
556 DPRINTF(("umouse set config %u\r\n", value));
559 case UREQ(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
560 DPRINTF(("umouse set descriptor %u\r\n", value));
564 case UREQ(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
565 DPRINTF(("umouse: (UR_SET_FEATURE, UT_WRITE_DEVICE) %x\r\n", value));
566 if (value == UF_DEVICE_REMOTE_WAKEUP)
570 case UREQ(UR_SET_FEATURE, UT_WRITE_DEVICE):
571 DPRINTF(("umouse: (UR_SET_FEATURE, UT_WRITE_DEVICE) %x\r\n", value));
572 if (value == UF_DEVICE_REMOTE_WAKEUP)
573 sc->hid.feature = UF_DEVICE_REMOTE_WAKEUP;
576 case UREQ(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
577 case UREQ(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
578 case UREQ(UR_SET_FEATURE, UT_WRITE_INTERFACE):
579 case UREQ(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
580 DPRINTF(("umouse: (UR_CLEAR_FEATURE, UT_WRITE_INTERFACE)\r\n"));
581 err = USB_ERR_IOERROR;
584 case UREQ(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
585 DPRINTF(("umouse set interface %u\r\n", value));
588 case UREQ(UR_ISOCH_DELAY, UT_WRITE_DEVICE):
589 DPRINTF(("umouse set isoch delay %u\r\n", value));
592 case UREQ(UR_SET_SEL, 0):
593 DPRINTF(("umouse set sel\r\n"));
596 case UREQ(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
597 DPRINTF(("umouse synch frame\r\n"));
600 /* HID device requests */
602 case UREQ(UMOUSE_GET_REPORT, UT_READ_CLASS_INTERFACE):
603 DPRINTF(("umouse: (UMOUSE_GET_REPORT, UT_READ_CLASS_INTERFACE) "
604 "0x%x\r\n", (value >> 8)));
608 if ((value >> 8) == 0x01 && len >= sizeof(sc->um_report)) {
609 /* TODO read from backend */
611 if (len > sizeof(sc->um_report)) {
612 data->blen = len - sizeof(sc->um_report);
613 len = sizeof(sc->um_report);
617 memcpy(data->buf, &sc->um_report, len);
620 err = USB_ERR_IOERROR;
623 eshort = data->blen > 0;
626 case UREQ(UMOUSE_GET_IDLE, UT_READ_CLASS_INTERFACE):
627 if (data != NULL && len > 0) {
628 *udata = sc->hid.idle;
629 data->blen = len - 1;
632 eshort = data->blen > 0;
635 case UREQ(UMOUSE_GET_PROTOCOL, UT_READ_CLASS_INTERFACE):
636 if (data != NULL && len > 0) {
637 *udata = sc->hid.protocol;
638 data->blen = len - 1;
641 eshort = data->blen > 0;
644 case UREQ(UMOUSE_SET_REPORT, UT_WRITE_CLASS_INTERFACE):
645 DPRINTF(("umouse: (UMOUSE_SET_REPORT, UT_WRITE_CLASS_INTERFACE) ignored\r\n"));
648 case UREQ(UMOUSE_SET_IDLE, UT_WRITE_CLASS_INTERFACE):
649 sc->hid.idle = UGETW(xfer->ureq->wValue) >> 8;
650 DPRINTF(("umouse: (UMOUSE_SET_IDLE, UT_WRITE_CLASS_INTERFACE) %x\r\n",
654 case UREQ(UMOUSE_SET_PROTOCOL, UT_WRITE_CLASS_INTERFACE):
655 sc->hid.protocol = UGETW(xfer->ureq->wValue) >> 8;
656 DPRINTF(("umouse: (UR_CLEAR_FEATURE, UT_WRITE_CLASS_INTERFACE) %x\r\n",
661 DPRINTF(("**** umouse request unhandled\r\n"));
662 err = USB_ERR_IOERROR;
667 if (xfer->ureq && (xfer->ureq->bmRequestType & UT_WRITE) &&
668 (err == USB_ERR_NORMAL_COMPLETION) && (data != NULL))
671 err = USB_ERR_SHORT_XFER;
673 DPRINTF(("umouse request error code %d (0=ok), blen %u txlen %u\r\n",
674 err, (data ? data->blen : 0), (data ? data->bdone : 0)));
680 umouse_data_handler(void *scarg, struct usb_data_xfer *xfer, int dir,
683 struct umouse_softc *sc;
684 struct usb_data_xfer_block *data;
689 DPRINTF(("umouse handle data - DIR=%s|EP=%d, blen %d\r\n",
690 dir ? "IN" : "OUT", epctx, xfer->data[0].blen));
693 /* find buffer to add data */
695 err = USB_ERR_NORMAL_COMPLETION;
697 /* handle xfer at first unprocessed item with buffer */
700 for (i = 0; i < xfer->ndata; i++) {
701 data = &xfer->data[idx];
702 if (data->buf != NULL && data->blen != 0) {
708 idx = (idx + 1) % USB_MAX_XFER_BLOCKS;
717 DPRINTF(("umouse no buffer provided for input\r\n"));
726 pthread_mutex_lock(&sc->mtx);
729 err = USB_ERR_CANCELLED;
730 USB_DATA_SET_ERRCODE(&xfer->data[xfer->head], USB_NAK);
731 pthread_mutex_unlock(&sc->mtx);
736 err = USB_ERR_STALLED;
737 USB_DATA_SET_ERRCODE(data, USB_STALL);
738 pthread_mutex_unlock(&sc->mtx);
748 memcpy(udata, &sc->um_report, 6);
749 data->blen = len - 6;
751 err = USB_ERR_SHORT_XFER;
755 pthread_mutex_unlock(&sc->mtx);
757 USB_DATA_SET_ERRCODE(data, USB_STALL);
758 err = USB_ERR_STALLED;
766 umouse_reset(void *scarg)
768 struct umouse_softc *sc;
778 umouse_remove(void *scarg)
785 umouse_stop(void *scarg)
792 struct usb_devemu ue_mouse = {
795 .ue_usbspeed = USB_SPEED_HIGH,
796 .ue_init = umouse_init,
797 .ue_request = umouse_request,
798 .ue_data = umouse_data_handler,
799 .ue_reset = umouse_reset,
800 .ue_remove = umouse_remove,
801 .ue_stop = umouse_stop
803 USB_EMUL_SET(ue_mouse);