2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2014 Leon Dang <ldang@nahannisys.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
32 #include <machine/vmm_snapshot.h>
39 #include <dev/usb/usb.h>
40 #include <dev/usb/usbdi.h>
47 static int umouse_debug = 0;
48 #define DPRINTF(params) if (umouse_debug) PRINTLN params
49 #define WPRINTF(params) PRINTLN params
51 /* USB endpoint context (1-15) for reporting mouse data events*/
52 #define UMOUSE_INTR_ENDPT 1
54 #define UMOUSE_REPORT_DESC_TYPE 0x22
56 #define UMOUSE_GET_REPORT 0x01
57 #define UMOUSE_GET_IDLE 0x02
58 #define UMOUSE_GET_PROTOCOL 0x03
59 #define UMOUSE_SET_REPORT 0x09
60 #define UMOUSE_SET_IDLE 0x0A
61 #define UMOUSE_SET_PROTOCOL 0x0B
63 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
74 static const char *umouse_desc_strings[] = {
82 struct umouse_hid_descriptor {
84 uint8_t bDescriptorType;
87 uint8_t bNumDescriptors;
88 uint8_t bReportDescriptorType;
89 uint8_t wItemLength[2];
92 struct umouse_config_desc {
93 struct usb_config_descriptor confd;
94 struct usb_interface_descriptor ifcd;
95 struct umouse_hid_descriptor hidd;
96 struct usb_endpoint_descriptor endpd;
97 struct usb_endpoint_ss_comp_descriptor sscompd;
100 #define MOUSE_MAX_X 0x8000
101 #define MOUSE_MAX_Y 0x8000
103 static const uint8_t umouse_report_desc[] = {
104 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
105 0x09, 0x02, /* USAGE (Mouse) */
106 0xa1, 0x01, /* COLLECTION (Application) */
107 0x09, 0x01, /* USAGE (Pointer) */
108 0xa1, 0x00, /* COLLECTION (Physical) */
109 0x05, 0x09, /* USAGE_PAGE (Button) */
110 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
111 0x29, 0x03, /* USAGE_MAXIMUM (Button 3) */
112 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
113 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
114 0x75, 0x01, /* REPORT_SIZE (1) */
115 0x95, 0x03, /* REPORT_COUNT (3) */
116 0x81, 0x02, /* INPUT (Data,Var,Abs); 3 buttons */
117 0x75, 0x05, /* REPORT_SIZE (5) */
118 0x95, 0x01, /* REPORT_COUNT (1) */
119 0x81, 0x03, /* INPUT (Cnst,Var,Abs); padding */
120 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
121 0x09, 0x30, /* USAGE (X) */
122 0x09, 0x31, /* USAGE (Y) */
123 0x35, 0x00, /* PHYSICAL_MINIMUM (0) */
124 0x46, 0xff, 0x7f, /* PHYSICAL_MAXIMUM (0x7fff) */
125 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
126 0x26, 0xff, 0x7f, /* LOGICAL_MAXIMUM (0x7fff) */
127 0x75, 0x10, /* REPORT_SIZE (16) */
128 0x95, 0x02, /* REPORT_COUNT (2) */
129 0x81, 0x02, /* INPUT (Data,Var,Abs) */
130 0x05, 0x01, /* USAGE Page (Generic Desktop) */
131 0x09, 0x38, /* USAGE (Wheel) */
132 0x35, 0x00, /* PHYSICAL_MINIMUM (0) */
133 0x45, 0x00, /* PHYSICAL_MAXIMUM (0) */
134 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
135 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
136 0x75, 0x08, /* REPORT_SIZE (8) */
137 0x95, 0x01, /* REPORT_COUNT (1) */
138 0x81, 0x06, /* INPUT (Data,Var,Rel) */
139 0xc0, /* END_COLLECTION */
140 0xc0 /* END_COLLECTION */
143 struct umouse_report {
144 uint8_t buttons; /* bits: 0 left, 1 right, 2 middle */
145 int16_t x; /* x position */
146 int16_t y; /* y position */
147 int8_t z; /* z wheel position */
151 #define MSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
153 static struct usb_device_descriptor umouse_dev_desc = {
154 .bLength = sizeof(umouse_dev_desc),
155 .bDescriptorType = UDESC_DEVICE,
156 MSETW(.bcdUSB, UD_USB_3_0),
157 .bMaxPacketSize = 8, /* max packet size */
158 MSETW(.idVendor, 0xFB5D), /* vendor */
159 MSETW(.idProduct, 0x0001), /* product */
160 MSETW(.bcdDevice, 0), /* device version */
161 .iManufacturer = UMSTR_MANUFACTURER,
162 .iProduct = UMSTR_PRODUCT,
163 .iSerialNumber = UMSTR_SERIAL,
164 .bNumConfigurations = 1,
167 static struct umouse_config_desc umouse_confd = {
169 .bLength = sizeof(umouse_confd.confd),
170 .bDescriptorType = UDESC_CONFIG,
171 .wTotalLength[0] = sizeof(umouse_confd),
173 .bConfigurationValue = 1,
174 .iConfiguration = UMSTR_CONFIG,
175 .bmAttributes = UC_BUS_POWERED | UC_REMOTE_WAKEUP,
179 .bLength = sizeof(umouse_confd.ifcd),
180 .bDescriptorType = UDESC_INTERFACE,
182 .bInterfaceClass = UICLASS_HID,
183 .bInterfaceSubClass = UISUBCLASS_BOOT,
184 .bInterfaceProtocol = UIPROTO_MOUSE,
187 .bLength = sizeof(umouse_confd.hidd),
188 .bDescriptorType = 0x21,
189 .bcdHID = { 0x01, 0x10 },
191 .bNumDescriptors = 1,
192 .bReportDescriptorType = UMOUSE_REPORT_DESC_TYPE,
193 .wItemLength = { sizeof(umouse_report_desc), 0 },
196 .bLength = sizeof(umouse_confd.endpd),
197 .bDescriptorType = UDESC_ENDPOINT,
198 .bEndpointAddress = UE_DIR_IN | UMOUSE_INTR_ENDPT,
199 .bmAttributes = UE_INTERRUPT,
200 .wMaxPacketSize[0] = 8,
204 .bLength = sizeof(umouse_confd.sscompd),
205 .bDescriptorType = UDESC_ENDPOINT_SS_COMP,
208 MSETW(.wBytesPerInterval, 0),
213 struct umouse_bos_desc {
214 struct usb_bos_descriptor bosd;
215 struct usb_devcap_ss_descriptor usbssd;
219 static struct umouse_bos_desc umouse_bosd = {
221 .bLength = sizeof(umouse_bosd.bosd),
222 .bDescriptorType = UDESC_BOS,
223 HSETW(.wTotalLength, sizeof(umouse_bosd)),
227 .bLength = sizeof(umouse_bosd.usbssd),
228 .bDescriptorType = UDESC_DEVICE_CAPABILITY,
229 .bDevCapabilityType = 3,
231 HSETW(.wSpeedsSupported, 0x08),
232 .bFunctionalitySupport = 3,
233 .bU1DevExitLat = 0xa, /* dummy - not used */
234 .wU2DevExitLat = { 0x20, 0x00 },
239 struct umouse_softc {
242 struct umouse_report um_report;
251 pthread_mutex_t ev_mtx;
253 struct timeval prev_evt;
257 umouse_event(uint8_t button, int x, int y, void *arg)
259 struct umouse_softc *sc;
260 struct bhyvegc_image *gc;
262 gc = console_get_image();
270 pthread_mutex_lock(&sc->mtx);
272 sc->um_report.buttons = 0;
276 sc->um_report.buttons |= 0x01; /* left */
278 sc->um_report.buttons |= 0x04; /* middle */
280 sc->um_report.buttons |= 0x02; /* right */
284 sc->um_report.z = -1;
286 /* scale coords to mouse resolution */
287 sc->um_report.x = MOUSE_MAX_X * x / gc->width;
288 sc->um_report.y = MOUSE_MAX_Y * y / gc->height;
290 pthread_mutex_unlock(&sc->mtx);
292 pthread_mutex_lock(&sc->ev_mtx);
293 sc->hci->hci_intr(sc->hci, UE_DIR_IN | UMOUSE_INTR_ENDPT);
294 pthread_mutex_unlock(&sc->ev_mtx);
298 umouse_init(struct usb_hci *hci, nvlist_t *nvl __unused)
300 struct umouse_softc *sc;
302 sc = calloc(1, sizeof(struct umouse_softc));
305 sc->hid.protocol = 1; /* REPORT protocol */
306 pthread_mutex_init(&sc->mtx, NULL);
307 pthread_mutex_init(&sc->ev_mtx, NULL);
309 console_ptr_register(umouse_event, sc, 10);
314 #define UREQ(x,y) ((x) | ((y) << 8))
317 umouse_request(void *scarg, struct usb_data_xfer *xfer)
319 struct umouse_softc *sc;
320 struct usb_data_xfer_block *data;
336 for (i = 0; i < xfer->ndata; i++) {
337 xfer->data[idx].bdone = 0;
338 if (data == NULL && USB_DATA_OK(xfer,i)) {
339 data = &xfer->data[idx];
343 xfer->data[idx].processed = 1;
344 idx = (idx + 1) % USB_MAX_XFER_BLOCKS;
347 err = USB_ERR_NORMAL_COMPLETION;
351 DPRINTF(("umouse_request: port %d", sc->hci->hci_port));
355 value = UGETW(xfer->ureq->wValue);
356 index = UGETW(xfer->ureq->wIndex);
357 len = UGETW(xfer->ureq->wLength);
359 DPRINTF(("umouse_request: port %d, type 0x%x, req 0x%x, val 0x%x, "
361 sc->hci->hci_port, xfer->ureq->bmRequestType,
362 xfer->ureq->bRequest, value, index, len));
364 switch (UREQ(xfer->ureq->bRequest, xfer->ureq->bmRequestType)) {
365 case UREQ(UR_GET_CONFIG, UT_READ_DEVICE):
366 DPRINTF(("umouse: (UR_GET_CONFIG, UT_READ_DEVICE)"));
370 *udata = umouse_confd.confd.bConfigurationValue;
371 data->blen = len > 0 ? len - 1 : 0;
372 eshort = data->blen > 0;
376 case UREQ(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
377 DPRINTF(("umouse: (UR_GET_DESCRIPTOR, UT_READ_DEVICE) val %x",
382 switch (value >> 8) {
384 DPRINTF(("umouse: (->UDESC_DEVICE) len %u ?= "
385 "sizeof(umouse_dev_desc) %lu",
386 len, sizeof(umouse_dev_desc)));
387 if ((value & 0xFF) != 0) {
388 err = USB_ERR_STALLED;
391 if (len > sizeof(umouse_dev_desc)) {
392 data->blen = len - sizeof(umouse_dev_desc);
393 len = sizeof(umouse_dev_desc);
396 memcpy(data->buf, &umouse_dev_desc, len);
401 DPRINTF(("umouse: (->UDESC_CONFIG)"));
402 if ((value & 0xFF) != 0) {
403 err = USB_ERR_STALLED;
406 if (len > sizeof(umouse_confd)) {
407 data->blen = len - sizeof(umouse_confd);
408 len = sizeof(umouse_confd);
412 memcpy(data->buf, &umouse_confd, len);
417 DPRINTF(("umouse: (->UDESC_STRING)"));
419 if ((value & 0xFF) < UMSTR_MAX)
420 str = umouse_desc_strings[value & 0xFF];
424 if ((value & 0xFF) == UMSTR_LANG) {
426 udata[1] = UDESC_STRING;
427 data->blen = len - 2;
442 slen = 2 + strlen(str) * 2;
444 udata[1] = UDESC_STRING;
447 data->blen = len - slen;
451 for (i = 2; i < len; i += 2) {
460 DPRINTF(("umouse: USB3 BOS"));
461 if (len > sizeof(umouse_bosd)) {
462 data->blen = len - sizeof(umouse_bosd);
463 len = sizeof(umouse_bosd);
466 memcpy(udata, &umouse_bosd, len);
471 DPRINTF(("umouse: unknown(%d)->ERROR", value >> 8));
472 err = USB_ERR_STALLED;
475 eshort = data->blen > 0;
478 case UREQ(UR_GET_DESCRIPTOR, UT_READ_INTERFACE):
479 DPRINTF(("umouse: (UR_GET_DESCRIPTOR, UT_READ_INTERFACE) "
480 "0x%x", (value >> 8)));
484 switch (value >> 8) {
485 case UMOUSE_REPORT_DESC_TYPE:
486 if (len > sizeof(umouse_report_desc)) {
487 data->blen = len - sizeof(umouse_report_desc);
488 len = sizeof(umouse_report_desc);
491 memcpy(data->buf, umouse_report_desc, len);
495 DPRINTF(("umouse: IO ERROR"));
496 err = USB_ERR_STALLED;
499 eshort = data->blen > 0;
502 case UREQ(UR_GET_INTERFACE, UT_READ_INTERFACE):
503 DPRINTF(("umouse: (UR_GET_INTERFACE, UT_READ_INTERFACE)"));
505 DPRINTF(("umouse get_interface, invalid index %d",
507 err = USB_ERR_STALLED;
516 data->blen = len - 1;
518 eshort = data->blen > 0;
522 case UREQ(UR_GET_STATUS, UT_READ_DEVICE):
523 DPRINTF(("umouse: (UR_GET_STATUS, UT_READ_DEVICE)"));
524 if (data != NULL && len > 1) {
525 if (sc->hid.feature == UF_DEVICE_REMOTE_WAKEUP)
526 USETW(udata, UDS_REMOTE_WAKEUP);
529 data->blen = len - 2;
533 eshort = data->blen > 0;
536 case UREQ(UR_GET_STATUS, UT_READ_INTERFACE):
537 case UREQ(UR_GET_STATUS, UT_READ_ENDPOINT):
538 DPRINTF(("umouse: (UR_GET_STATUS, UT_READ_INTERFACE)"));
539 if (data != NULL && len > 1) {
541 data->blen = len - 2;
544 eshort = data->blen > 0;
547 case UREQ(UR_SET_ADDRESS, UT_WRITE_DEVICE):
548 /* XXX Controller should've handled this */
549 DPRINTF(("umouse set address %u", value));
552 case UREQ(UR_SET_CONFIG, UT_WRITE_DEVICE):
553 DPRINTF(("umouse set config %u", value));
556 case UREQ(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
557 DPRINTF(("umouse set descriptor %u", value));
561 case UREQ(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
562 DPRINTF(("umouse: (UR_SET_FEATURE, UT_WRITE_DEVICE) %x", value));
563 if (value == UF_DEVICE_REMOTE_WAKEUP)
567 case UREQ(UR_SET_FEATURE, UT_WRITE_DEVICE):
568 DPRINTF(("umouse: (UR_SET_FEATURE, UT_WRITE_DEVICE) %x", value));
569 if (value == UF_DEVICE_REMOTE_WAKEUP)
570 sc->hid.feature = UF_DEVICE_REMOTE_WAKEUP;
573 case UREQ(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
574 case UREQ(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
575 case UREQ(UR_SET_FEATURE, UT_WRITE_INTERFACE):
576 case UREQ(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
577 DPRINTF(("umouse: (UR_CLEAR_FEATURE, UT_WRITE_INTERFACE)"));
578 err = USB_ERR_STALLED;
581 case UREQ(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
582 DPRINTF(("umouse set interface %u", value));
585 case UREQ(UR_ISOCH_DELAY, UT_WRITE_DEVICE):
586 DPRINTF(("umouse set isoch delay %u", value));
589 case UREQ(UR_SET_SEL, 0):
590 DPRINTF(("umouse set sel"));
593 case UREQ(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
594 DPRINTF(("umouse synch frame"));
597 /* HID device requests */
599 case UREQ(UMOUSE_GET_REPORT, UT_READ_CLASS_INTERFACE):
600 DPRINTF(("umouse: (UMOUSE_GET_REPORT, UT_READ_CLASS_INTERFACE) "
601 "0x%x", (value >> 8)));
605 if ((value >> 8) == 0x01 && len >= sizeof(sc->um_report)) {
606 /* TODO read from backend */
608 if (len > sizeof(sc->um_report)) {
609 data->blen = len - sizeof(sc->um_report);
610 len = sizeof(sc->um_report);
614 memcpy(data->buf, &sc->um_report, len);
617 err = USB_ERR_STALLED;
620 eshort = data->blen > 0;
623 case UREQ(UMOUSE_GET_IDLE, UT_READ_CLASS_INTERFACE):
624 if (data != NULL && len > 0) {
625 *udata = sc->hid.idle;
626 data->blen = len - 1;
629 eshort = data->blen > 0;
632 case UREQ(UMOUSE_GET_PROTOCOL, UT_READ_CLASS_INTERFACE):
633 if (data != NULL && len > 0) {
634 *udata = sc->hid.protocol;
635 data->blen = len - 1;
638 eshort = data->blen > 0;
641 case UREQ(UMOUSE_SET_REPORT, UT_WRITE_CLASS_INTERFACE):
642 DPRINTF(("umouse: (UMOUSE_SET_REPORT, UT_WRITE_CLASS_INTERFACE) ignored"));
645 case UREQ(UMOUSE_SET_IDLE, UT_WRITE_CLASS_INTERFACE):
646 sc->hid.idle = UGETW(xfer->ureq->wValue) >> 8;
647 DPRINTF(("umouse: (UMOUSE_SET_IDLE, UT_WRITE_CLASS_INTERFACE) %x",
651 case UREQ(UMOUSE_SET_PROTOCOL, UT_WRITE_CLASS_INTERFACE):
652 sc->hid.protocol = UGETW(xfer->ureq->wValue) >> 8;
653 DPRINTF(("umouse: (UR_CLEAR_FEATURE, UT_WRITE_CLASS_INTERFACE) %x",
658 DPRINTF(("**** umouse request unhandled"));
659 err = USB_ERR_STALLED;
664 if (xfer->ureq && (xfer->ureq->bmRequestType & UT_WRITE) &&
665 (err == USB_ERR_NORMAL_COMPLETION) && (data != NULL))
668 err = USB_ERR_SHORT_XFER;
670 DPRINTF(("umouse request error code %d (0=ok), blen %u txlen %u",
671 err, (data ? data->blen : 0), (data ? data->bdone : 0)));
677 umouse_data_handler(void *scarg, struct usb_data_xfer *xfer, int dir,
680 struct umouse_softc *sc;
681 struct usb_data_xfer_block *data;
686 DPRINTF(("umouse handle data - DIR=%s|EP=%d, blen %d",
687 dir ? "IN" : "OUT", epctx, xfer->data[0].blen));
690 /* find buffer to add data */
692 err = USB_ERR_NORMAL_COMPLETION;
694 /* handle xfer at first unprocessed item with buffer */
697 for (i = 0; i < xfer->ndata; i++) {
698 data = &xfer->data[idx];
699 if (data->buf != NULL && data->blen != 0) {
705 idx = (idx + 1) % USB_MAX_XFER_BLOCKS;
714 DPRINTF(("umouse no buffer provided for input"));
723 pthread_mutex_lock(&sc->mtx);
726 err = USB_ERR_CANCELLED;
727 USB_DATA_SET_ERRCODE(&xfer->data[xfer->head], USB_NAK);
728 pthread_mutex_unlock(&sc->mtx);
733 err = USB_ERR_STALLED;
734 USB_DATA_SET_ERRCODE(data, USB_STALL);
735 pthread_mutex_unlock(&sc->mtx);
745 memcpy(udata, &sc->um_report, 6);
746 data->blen = len - 6;
748 err = USB_ERR_SHORT_XFER;
752 pthread_mutex_unlock(&sc->mtx);
754 USB_DATA_SET_ERRCODE(data, USB_STALL);
755 err = USB_ERR_STALLED;
763 umouse_reset(void *scarg)
765 struct umouse_softc *sc;
775 umouse_remove(void *scarg __unused)
781 umouse_stop(void *scarg __unused)
786 #ifdef BHYVE_SNAPSHOT
788 umouse_snapshot(void *scarg, struct vm_snapshot_meta *meta)
791 struct umouse_softc *sc;
795 SNAPSHOT_VAR_OR_LEAVE(sc->um_report, meta, ret, done);
796 SNAPSHOT_VAR_OR_LEAVE(sc->newdata, meta, ret, done);
797 SNAPSHOT_VAR_OR_LEAVE(sc->hid.idle, meta, ret, done);
798 SNAPSHOT_VAR_OR_LEAVE(sc->hid.protocol, meta, ret, done);
799 SNAPSHOT_VAR_OR_LEAVE(sc->hid.feature, meta, ret, done);
801 SNAPSHOT_VAR_OR_LEAVE(sc->polling, meta, ret, done);
802 SNAPSHOT_VAR_OR_LEAVE(sc->prev_evt.tv_sec, meta, ret, done);
803 SNAPSHOT_VAR_OR_LEAVE(sc->prev_evt.tv_usec, meta, ret, done);
810 static struct usb_devemu ue_mouse = {
813 .ue_usbspeed = USB_SPEED_HIGH,
814 .ue_init = umouse_init,
815 .ue_request = umouse_request,
816 .ue_data = umouse_data_handler,
817 .ue_reset = umouse_reset,
818 .ue_remove = umouse_remove,
819 .ue_stop = umouse_stop,
820 #ifdef BHYVE_SNAPSHOT
821 .ue_snapshot = umouse_snapshot,
824 USB_EMUL_SET(ue_mouse);