2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
30 __FBSDID("$FreeBSD$");
39 #include <dev/usb/usb.h>
40 #include <dev/usb/usbdi.h>
46 static int umouse_debug = 0;
47 #define DPRINTF(params) if (umouse_debug) printf params
48 #define WPRINTF(params) printf params
50 /* USB endpoint context (1-15) for reporting mouse data events*/
51 #define UMOUSE_INTR_ENDPT 1
53 #define UMOUSE_REPORT_DESC_TYPE 0x22
55 #define UMOUSE_GET_REPORT 0x01
56 #define UMOUSE_GET_IDLE 0x02
57 #define UMOUSE_GET_PROTOCOL 0x03
58 #define UMOUSE_SET_REPORT 0x09
59 #define UMOUSE_SET_IDLE 0x0A
60 #define UMOUSE_SET_PROTOCOL 0x0B
62 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
73 static const char *umouse_desc_strings[] = {
81 struct umouse_hid_descriptor {
83 uint8_t bDescriptorType;
86 uint8_t bNumDescriptors;
87 uint8_t bReportDescriptorType;
88 uint8_t wItemLength[2];
91 struct umouse_config_desc {
92 struct usb_config_descriptor confd;
93 struct usb_interface_descriptor ifcd;
94 struct umouse_hid_descriptor hidd;
95 struct usb_endpoint_descriptor endpd;
96 struct usb_endpoint_ss_comp_descriptor sscompd;
99 #define MOUSE_MAX_X 0x8000
100 #define MOUSE_MAX_Y 0x8000
102 static const uint8_t umouse_report_desc[] = {
103 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
104 0x09, 0x02, /* USAGE (Mouse) */
105 0xa1, 0x01, /* COLLECTION (Application) */
106 0x09, 0x01, /* USAGE (Pointer) */
107 0xa1, 0x00, /* COLLECTION (Physical) */
108 0x05, 0x09, /* USAGE_PAGE (Button) */
109 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
110 0x29, 0x03, /* USAGE_MAXIMUM (Button 3) */
111 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
112 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
113 0x75, 0x01, /* REPORT_SIZE (1) */
114 0x95, 0x03, /* REPORT_COUNT (3) */
115 0x81, 0x02, /* INPUT (Data,Var,Abs); 3 buttons */
116 0x75, 0x05, /* REPORT_SIZE (5) */
117 0x95, 0x01, /* REPORT_COUNT (1) */
118 0x81, 0x03, /* INPUT (Cnst,Var,Abs); padding */
119 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
120 0x09, 0x30, /* USAGE (X) */
121 0x09, 0x31, /* USAGE (Y) */
122 0x35, 0x00, /* PHYSICAL_MINIMUM (0) */
123 0x46, 0xff, 0x7f, /* PHYSICAL_MAXIMUM (0x7fff) */
124 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
125 0x26, 0xff, 0x7f, /* LOGICAL_MAXIMUM (0x7fff) */
126 0x75, 0x10, /* REPORT_SIZE (16) */
127 0x95, 0x02, /* REPORT_COUNT (2) */
128 0x81, 0x02, /* INPUT (Data,Var,Abs) */
129 0x05, 0x01, /* USAGE Page (Generic Desktop) */
130 0x09, 0x38, /* USAGE (Wheel) */
131 0x35, 0x00, /* PHYSICAL_MINIMUM (0) */
132 0x45, 0x00, /* PHYSICAL_MAXIMUM (0) */
133 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
134 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
135 0x75, 0x08, /* REPORT_SIZE (8) */
136 0x95, 0x01, /* REPORT_COUNT (1) */
137 0x81, 0x06, /* INPUT (Data,Var,Rel) */
138 0xc0, /* END_COLLECTION */
139 0xc0 /* END_COLLECTION */
142 struct umouse_report {
143 uint8_t buttons; /* bits: 0 left, 1 right, 2 middle */
144 int16_t x; /* x position */
145 int16_t y; /* y position */
146 int8_t z; /* z wheel position */
150 #define MSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
152 static struct usb_device_descriptor umouse_dev_desc = {
153 .bLength = sizeof(umouse_dev_desc),
154 .bDescriptorType = UDESC_DEVICE,
155 MSETW(.bcdUSB, UD_USB_3_0),
156 .bMaxPacketSize = 8, /* max packet size */
157 MSETW(.idVendor, 0xFB5D), /* vendor */
158 MSETW(.idProduct, 0x0001), /* product */
159 MSETW(.bcdDevice, 0), /* device version */
160 .iManufacturer = UMSTR_MANUFACTURER,
161 .iProduct = UMSTR_PRODUCT,
162 .iSerialNumber = UMSTR_SERIAL,
163 .bNumConfigurations = 1,
166 static struct umouse_config_desc umouse_confd = {
168 .bLength = sizeof(umouse_confd.confd),
169 .bDescriptorType = UDESC_CONFIG,
170 .wTotalLength[0] = sizeof(umouse_confd),
172 .bConfigurationValue = 1,
173 .iConfiguration = UMSTR_CONFIG,
174 .bmAttributes = UC_BUS_POWERED | UC_REMOTE_WAKEUP,
178 .bLength = sizeof(umouse_confd.ifcd),
179 .bDescriptorType = UDESC_INTERFACE,
181 .bInterfaceClass = UICLASS_HID,
182 .bInterfaceSubClass = UISUBCLASS_BOOT,
183 .bInterfaceProtocol = UIPROTO_MOUSE,
186 .bLength = sizeof(umouse_confd.hidd),
187 .bDescriptorType = 0x21,
188 .bcdHID = { 0x01, 0x10 },
190 .bNumDescriptors = 1,
191 .bReportDescriptorType = UMOUSE_REPORT_DESC_TYPE,
192 .wItemLength = { sizeof(umouse_report_desc), 0 },
195 .bLength = sizeof(umouse_confd.endpd),
196 .bDescriptorType = UDESC_ENDPOINT,
197 .bEndpointAddress = UE_DIR_IN | UMOUSE_INTR_ENDPT,
198 .bmAttributes = UE_INTERRUPT,
199 .wMaxPacketSize[0] = 8,
203 .bLength = sizeof(umouse_confd.sscompd),
204 .bDescriptorType = UDESC_ENDPOINT_SS_COMP,
207 MSETW(.wBytesPerInterval, 0),
212 struct umouse_bos_desc {
213 struct usb_bos_descriptor bosd;
214 struct usb_devcap_ss_descriptor usbssd;
218 struct umouse_bos_desc umouse_bosd = {
220 .bLength = sizeof(umouse_bosd.bosd),
221 .bDescriptorType = UDESC_BOS,
222 HSETW(.wTotalLength, sizeof(umouse_bosd)),
226 .bLength = sizeof(umouse_bosd.usbssd),
227 .bDescriptorType = UDESC_DEVICE_CAPABILITY,
228 .bDevCapabilityType = 3,
230 HSETW(.wSpeedsSupported, 0x08),
231 .bFunctionalitySupport = 3,
232 .bU1DevExitLat = 0xa, /* dummy - not used */
233 .wU2DevExitLat = { 0x20, 0x00 },
238 struct umouse_softc {
243 struct umouse_report um_report;
252 pthread_mutex_t ev_mtx;
254 struct timeval prev_evt;
258 umouse_event(uint8_t button, int x, int y, void *arg)
260 struct umouse_softc *sc;
261 struct bhyvegc_image *gc;
263 gc = console_get_image();
271 pthread_mutex_lock(&sc->mtx);
273 sc->um_report.buttons = 0;
277 sc->um_report.buttons |= 0x01; /* left */
279 sc->um_report.buttons |= 0x04; /* middle */
281 sc->um_report.buttons |= 0x02; /* right */
285 sc->um_report.z = -1;
287 /* scale coords to mouse resolution */
288 sc->um_report.x = MOUSE_MAX_X * x / gc->width;
289 sc->um_report.y = MOUSE_MAX_Y * y / gc->height;
291 pthread_mutex_unlock(&sc->mtx);
293 pthread_mutex_lock(&sc->ev_mtx);
294 sc->hci->hci_intr(sc->hci, UE_DIR_IN | UMOUSE_INTR_ENDPT);
295 pthread_mutex_unlock(&sc->ev_mtx);
299 umouse_init(struct usb_hci *hci, char *opt)
301 struct umouse_softc *sc;
303 sc = calloc(1, sizeof(struct umouse_softc));
306 sc->hid.protocol = 1; /* REPORT protocol */
307 sc->opt = strdup(opt);
308 pthread_mutex_init(&sc->mtx, NULL);
309 pthread_mutex_init(&sc->ev_mtx, NULL);
311 console_ptr_register(umouse_event, sc, 10);
316 #define UREQ(x,y) ((x) | ((y) << 8))
319 umouse_request(void *scarg, struct usb_data_xfer *xfer)
321 struct umouse_softc *sc;
322 struct usb_data_xfer_block *data;
338 for (i = 0; i < xfer->ndata; i++) {
339 xfer->data[idx].bdone = 0;
340 if (data == NULL && USB_DATA_OK(xfer,i)) {
341 data = &xfer->data[idx];
345 xfer->data[idx].processed = 1;
346 idx = (idx + 1) % USB_MAX_XFER_BLOCKS;
349 err = USB_ERR_NORMAL_COMPLETION;
353 DPRINTF(("umouse_request: port %d\r\n", sc->hci->hci_port));
357 value = UGETW(xfer->ureq->wValue);
358 index = UGETW(xfer->ureq->wIndex);
359 len = UGETW(xfer->ureq->wLength);
361 DPRINTF(("umouse_request: port %d, type 0x%x, req 0x%x, val 0x%x, "
362 "idx 0x%x, len %u\r\n",
363 sc->hci->hci_port, xfer->ureq->bmRequestType,
364 xfer->ureq->bRequest, value, index, len));
366 switch (UREQ(xfer->ureq->bRequest, xfer->ureq->bmRequestType)) {
367 case UREQ(UR_GET_CONFIG, UT_READ_DEVICE):
368 DPRINTF(("umouse: (UR_GET_CONFIG, UT_READ_DEVICE)\r\n"));
372 *udata = umouse_confd.confd.bConfigurationValue;
373 data->blen = len > 0 ? len - 1 : 0;
374 eshort = data->blen > 0;
378 case UREQ(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
379 DPRINTF(("umouse: (UR_GET_DESCRIPTOR, UT_READ_DEVICE) val %x\r\n",
384 switch (value >> 8) {
386 DPRINTF(("umouse: (->UDESC_DEVICE) len %u ?= "
387 "sizeof(umouse_dev_desc) %lu\r\n",
388 len, sizeof(umouse_dev_desc)));
389 if ((value & 0xFF) != 0) {
390 err = USB_ERR_IOERROR;
393 if (len > sizeof(umouse_dev_desc)) {
394 data->blen = len - sizeof(umouse_dev_desc);
395 len = sizeof(umouse_dev_desc);
398 memcpy(data->buf, &umouse_dev_desc, len);
403 DPRINTF(("umouse: (->UDESC_CONFIG)\r\n"));
404 if ((value & 0xFF) != 0) {
405 err = USB_ERR_IOERROR;
408 if (len > sizeof(umouse_confd)) {
409 data->blen = len - sizeof(umouse_confd);
410 len = sizeof(umouse_confd);
414 memcpy(data->buf, &umouse_confd, len);
419 DPRINTF(("umouse: (->UDESC_STRING)\r\n"));
421 if ((value & 0xFF) < UMSTR_MAX)
422 str = umouse_desc_strings[value & 0xFF];
426 if ((value & 0xFF) == UMSTR_LANG) {
428 udata[1] = UDESC_STRING;
429 data->blen = len - 2;
444 slen = 2 + strlen(str) * 2;
446 udata[1] = UDESC_STRING;
449 data->blen = len - slen;
453 for (i = 2; i < len; i += 2) {
462 DPRINTF(("umouse: USB3 BOS\r\n"));
463 if (len > sizeof(umouse_bosd)) {
464 data->blen = len - sizeof(umouse_bosd);
465 len = sizeof(umouse_bosd);
468 memcpy(udata, &umouse_bosd, len);
473 DPRINTF(("umouse: unknown(%d)->ERROR\r\n", value >> 8));
474 err = USB_ERR_IOERROR;
477 eshort = data->blen > 0;
480 case UREQ(UR_GET_DESCRIPTOR, UT_READ_INTERFACE):
481 DPRINTF(("umouse: (UR_GET_DESCRIPTOR, UT_READ_INTERFACE) "
482 "0x%x\r\n", (value >> 8)));
486 switch (value >> 8) {
487 case UMOUSE_REPORT_DESC_TYPE:
488 if (len > sizeof(umouse_report_desc)) {
489 data->blen = len - sizeof(umouse_report_desc);
490 len = sizeof(umouse_report_desc);
493 memcpy(data->buf, umouse_report_desc, len);
497 DPRINTF(("umouse: IO ERROR\r\n"));
498 err = USB_ERR_IOERROR;
501 eshort = data->blen > 0;
504 case UREQ(UR_GET_INTERFACE, UT_READ_INTERFACE):
505 DPRINTF(("umouse: (UR_GET_INTERFACE, UT_READ_INTERFACE)\r\n"));
507 DPRINTF(("umouse get_interface, invalid index %d\r\n",
509 err = USB_ERR_IOERROR;
518 data->blen = len - 1;
520 eshort = data->blen > 0;
524 case UREQ(UR_GET_STATUS, UT_READ_DEVICE):
525 DPRINTF(("umouse: (UR_GET_STATUS, UT_READ_DEVICE)\r\n"));
526 if (data != NULL && len > 1) {
527 if (sc->hid.feature == UF_DEVICE_REMOTE_WAKEUP)
528 USETW(udata, UDS_REMOTE_WAKEUP);
531 data->blen = len - 2;
535 eshort = data->blen > 0;
538 case UREQ(UR_GET_STATUS, UT_READ_INTERFACE):
539 case UREQ(UR_GET_STATUS, UT_READ_ENDPOINT):
540 DPRINTF(("umouse: (UR_GET_STATUS, UT_READ_INTERFACE)\r\n"));
541 if (data != NULL && len > 1) {
543 data->blen = len - 2;
546 eshort = data->blen > 0;
549 case UREQ(UR_SET_ADDRESS, UT_WRITE_DEVICE):
550 /* XXX Controller should've handled this */
551 DPRINTF(("umouse set address %u\r\n", value));
554 case UREQ(UR_SET_CONFIG, UT_WRITE_DEVICE):
555 DPRINTF(("umouse set config %u\r\n", value));
558 case UREQ(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
559 DPRINTF(("umouse set descriptor %u\r\n", value));
563 case UREQ(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
564 DPRINTF(("umouse: (UR_SET_FEATURE, UT_WRITE_DEVICE) %x\r\n", value));
565 if (value == UF_DEVICE_REMOTE_WAKEUP)
569 case UREQ(UR_SET_FEATURE, UT_WRITE_DEVICE):
570 DPRINTF(("umouse: (UR_SET_FEATURE, UT_WRITE_DEVICE) %x\r\n", value));
571 if (value == UF_DEVICE_REMOTE_WAKEUP)
572 sc->hid.feature = UF_DEVICE_REMOTE_WAKEUP;
575 case UREQ(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
576 case UREQ(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
577 case UREQ(UR_SET_FEATURE, UT_WRITE_INTERFACE):
578 case UREQ(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
579 DPRINTF(("umouse: (UR_CLEAR_FEATURE, UT_WRITE_INTERFACE)\r\n"));
580 err = USB_ERR_IOERROR;
583 case UREQ(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
584 DPRINTF(("umouse set interface %u\r\n", value));
587 case UREQ(UR_ISOCH_DELAY, UT_WRITE_DEVICE):
588 DPRINTF(("umouse set isoch delay %u\r\n", value));
591 case UREQ(UR_SET_SEL, 0):
592 DPRINTF(("umouse set sel\r\n"));
595 case UREQ(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
596 DPRINTF(("umouse synch frame\r\n"));
599 /* HID device requests */
601 case UREQ(UMOUSE_GET_REPORT, UT_READ_CLASS_INTERFACE):
602 DPRINTF(("umouse: (UMOUSE_GET_REPORT, UT_READ_CLASS_INTERFACE) "
603 "0x%x\r\n", (value >> 8)));
607 if ((value >> 8) == 0x01 && len >= sizeof(sc->um_report)) {
608 /* TODO read from backend */
610 if (len > sizeof(sc->um_report)) {
611 data->blen = len - sizeof(sc->um_report);
612 len = sizeof(sc->um_report);
616 memcpy(data->buf, &sc->um_report, len);
619 err = USB_ERR_IOERROR;
622 eshort = data->blen > 0;
625 case UREQ(UMOUSE_GET_IDLE, UT_READ_CLASS_INTERFACE):
626 if (data != NULL && len > 0) {
627 *udata = sc->hid.idle;
628 data->blen = len - 1;
631 eshort = data->blen > 0;
634 case UREQ(UMOUSE_GET_PROTOCOL, UT_READ_CLASS_INTERFACE):
635 if (data != NULL && len > 0) {
636 *udata = sc->hid.protocol;
637 data->blen = len - 1;
640 eshort = data->blen > 0;
643 case UREQ(UMOUSE_SET_REPORT, UT_WRITE_CLASS_INTERFACE):
644 DPRINTF(("umouse: (UMOUSE_SET_REPORT, UT_WRITE_CLASS_INTERFACE) ignored\r\n"));
647 case UREQ(UMOUSE_SET_IDLE, UT_WRITE_CLASS_INTERFACE):
648 sc->hid.idle = UGETW(xfer->ureq->wValue) >> 8;
649 DPRINTF(("umouse: (UMOUSE_SET_IDLE, UT_WRITE_CLASS_INTERFACE) %x\r\n",
653 case UREQ(UMOUSE_SET_PROTOCOL, UT_WRITE_CLASS_INTERFACE):
654 sc->hid.protocol = UGETW(xfer->ureq->wValue) >> 8;
655 DPRINTF(("umouse: (UR_CLEAR_FEATURE, UT_WRITE_CLASS_INTERFACE) %x\r\n",
660 DPRINTF(("**** umouse request unhandled\r\n"));
661 err = USB_ERR_IOERROR;
666 if (xfer->ureq && (xfer->ureq->bmRequestType & UT_WRITE) &&
667 (err == USB_ERR_NORMAL_COMPLETION) && (data != NULL))
670 err = USB_ERR_SHORT_XFER;
672 DPRINTF(("umouse request error code %d (0=ok), blen %u txlen %u\r\n",
673 err, (data ? data->blen : 0), (data ? data->bdone : 0)));
679 umouse_data_handler(void *scarg, struct usb_data_xfer *xfer, int dir,
682 struct umouse_softc *sc;
683 struct usb_data_xfer_block *data;
688 DPRINTF(("umouse handle data - DIR=%s|EP=%d, blen %d\r\n",
689 dir ? "IN" : "OUT", epctx, xfer->data[0].blen));
692 /* find buffer to add data */
694 err = USB_ERR_NORMAL_COMPLETION;
696 /* handle xfer at first unprocessed item with buffer */
699 for (i = 0; i < xfer->ndata; i++) {
700 data = &xfer->data[idx];
701 if (data->buf != NULL && data->blen != 0) {
707 idx = (idx + 1) % USB_MAX_XFER_BLOCKS;
716 DPRINTF(("umouse no buffer provided for input\r\n"));
725 pthread_mutex_lock(&sc->mtx);
728 err = USB_ERR_CANCELLED;
729 USB_DATA_SET_ERRCODE(&xfer->data[xfer->head], USB_NAK);
730 pthread_mutex_unlock(&sc->mtx);
735 err = USB_ERR_STALLED;
736 USB_DATA_SET_ERRCODE(data, USB_STALL);
737 pthread_mutex_unlock(&sc->mtx);
747 memcpy(udata, &sc->um_report, 6);
748 data->blen = len - 6;
750 err = USB_ERR_SHORT_XFER;
754 pthread_mutex_unlock(&sc->mtx);
756 USB_DATA_SET_ERRCODE(data, USB_STALL);
757 err = USB_ERR_STALLED;
765 umouse_reset(void *scarg)
767 struct umouse_softc *sc;
777 umouse_remove(void *scarg)
784 umouse_stop(void *scarg)
791 struct usb_devemu ue_mouse = {
794 .ue_usbspeed = USB_SPEED_HIGH,
795 .ue_init = umouse_init,
796 .ue_request = umouse_request,
797 .ue_data = umouse_data_handler,
798 .ue_reset = umouse_reset,
799 .ue_remove = umouse_remove,
800 .ue_stop = umouse_stop
802 USB_EMUL_SET(ue_mouse);