Home | History | Annotate | Line # | Download | only in usb
vhci.c revision 1.3
      1 /*	$NetBSD: vhci.c,v 1.3 2019/10/03 05:13:23 maxv Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Maxime Villard.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: vhci.c,v 1.3 2019/10/03 05:13:23 maxv Exp $");
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "opt_usb.h"
     37 #endif
     38 
     39 #include <sys/param.h>
     40 
     41 #include <sys/bus.h>
     42 #include <sys/cpu.h>
     43 #include <sys/conf.h>
     44 #include <sys/device.h>
     45 #include <sys/kernel.h>
     46 #include <sys/kmem.h>
     47 #include <sys/mutex.h>
     48 #include <sys/proc.h>
     49 #include <sys/queue.h>
     50 #include <sys/systm.h>
     51 #include <sys/mman.h>
     52 #include <sys/file.h>
     53 #include <sys/filedesc.h>
     54 
     55 #include <machine/endian.h>
     56 
     57 #include "ioconf.h"
     58 
     59 #include <dev/usb/usb.h>
     60 #include <dev/usb/usbdi.h>
     61 #include <dev/usb/usbdivar.h>
     62 
     63 #include <dev/usb/usbroothub.h>
     64 
     65 #ifdef VHCI_DEBUG
     66 #define DPRINTF(fmt, ...)	printf(fmt, __VA_ARGS__)
     67 #else
     68 #define DPRINTF(fmt, ...)	__nothing
     69 #endif
     70 
     71 static usbd_status vhci_open(struct usbd_pipe *);
     72 static void vhci_softintr(void *);
     73 
     74 static struct usbd_xfer *vhci_allocx(struct usbd_bus *, unsigned int);
     75 static void vhci_freex(struct usbd_bus *, struct usbd_xfer *);
     76 static void vhci_get_lock(struct usbd_bus *, kmutex_t **);
     77 static int vhci_roothub_ctrl(struct usbd_bus *, usb_device_request_t *,
     78     void *, int);
     79 
     80 static const struct usbd_bus_methods vhci_bus_methods = {
     81 	.ubm_open =	vhci_open,
     82 	.ubm_softint =	vhci_softintr,
     83 	.ubm_dopoll =	NULL,
     84 	.ubm_allocx =	vhci_allocx,
     85 	.ubm_freex =	vhci_freex,
     86 	.ubm_getlock =	vhci_get_lock,
     87 	.ubm_rhctrl =	vhci_roothub_ctrl,
     88 };
     89 
     90 static usbd_status vhci_device_ctrl_transfer(struct usbd_xfer *);
     91 static usbd_status vhci_device_ctrl_start(struct usbd_xfer *);
     92 static void vhci_device_ctrl_abort(struct usbd_xfer *);
     93 static void vhci_device_ctrl_close(struct usbd_pipe *);
     94 static void vhci_device_ctrl_cleartoggle(struct usbd_pipe *);
     95 static void vhci_device_ctrl_done(struct usbd_xfer *);
     96 
     97 static const struct usbd_pipe_methods vhci_device_ctrl_methods = {
     98 	.upm_init =		NULL,
     99 	.upm_fini =		NULL,
    100 	.upm_transfer =		vhci_device_ctrl_transfer,
    101 	.upm_start =		vhci_device_ctrl_start,
    102 	.upm_abort =		vhci_device_ctrl_abort,
    103 	.upm_close =		vhci_device_ctrl_close,
    104 	.upm_cleartoggle =	vhci_device_ctrl_cleartoggle,
    105 	.upm_done =		vhci_device_ctrl_done,
    106 };
    107 
    108 static usbd_status vhci_root_intr_transfer(struct usbd_xfer *);
    109 static usbd_status vhci_root_intr_start(struct usbd_xfer *);
    110 static void vhci_root_intr_abort(struct usbd_xfer *);
    111 static void vhci_root_intr_close(struct usbd_pipe *);
    112 static void vhci_root_intr_cleartoggle(struct usbd_pipe *);
    113 static void vhci_root_intr_done(struct usbd_xfer *);
    114 
    115 static const struct usbd_pipe_methods vhci_root_intr_methods = {
    116 	.upm_init =		NULL,
    117 	.upm_fini =		NULL,
    118 	.upm_transfer =		vhci_root_intr_transfer,
    119 	.upm_start =		vhci_root_intr_start,
    120 	.upm_abort =		vhci_root_intr_abort,
    121 	.upm_close =		vhci_root_intr_close,
    122 	.upm_cleartoggle =	vhci_root_intr_cleartoggle,
    123 	.upm_done =		vhci_root_intr_done,
    124 };
    125 
    126 typedef struct vhci_packet {
    127 	TAILQ_ENTRY(vhci_packet) portlist;
    128 	TAILQ_ENTRY(vhci_packet) xferlist;
    129 	struct usbd_xfer *xfer; /* also vxfer */
    130 	bool utoh;
    131 	uint8_t *buf;
    132 	size_t size;
    133 	size_t cursor;
    134 } vhci_packet_t;
    135 
    136 typedef TAILQ_HEAD(, vhci_packet) vhci_packet_list_t;
    137 
    138 typedef struct {
    139 	kmutex_t lock;
    140 	int status;
    141 	int change;
    142 	struct {
    143 		vhci_packet_list_t usb_to_host;
    144 		vhci_packet_list_t host_to_usb;
    145 	} pkts_device_ctrl;
    146 } vhci_port_t;
    147 
    148 typedef struct {
    149 	struct usbd_pipe pipe;
    150 } vhci_pipe_t;
    151 
    152 typedef struct vhci_xfer {
    153 	/* General. */
    154 	struct usbd_xfer xfer;
    155 
    156 	/* vHCI-specific. */
    157 	size_t refcnt;
    158 	vhci_port_t *port;
    159 	vhci_packet_list_t pkts;
    160 	TAILQ_ENTRY(vhci_xfer) freelist;
    161 } vhci_xfer_t;
    162 
    163 typedef TAILQ_HEAD(, vhci_xfer) vhci_xfer_list_t;
    164 
    165 #define VHCI_INDEX2PORT(idx)	(idx)
    166 #define VHCI_NPORTS		4
    167 
    168 typedef struct {
    169 	device_t sc_dev;
    170 
    171 	struct usbd_bus sc_bus;
    172 	bool sc_dying;
    173 	kmutex_t sc_lock;
    174 
    175 	/*
    176 	 * Intr Root. Used to attach the devices.
    177 	 */
    178 	struct usbd_xfer *sc_intrxfer;
    179 
    180 	/*
    181 	 * The ports. Zero is for the roothub, one and beyond for the USB
    182 	 * devices.
    183 	 */
    184 	size_t sc_nports;
    185 	vhci_port_t sc_port[VHCI_NPORTS];
    186 
    187 	device_t sc_child; /* /dev/usb# device */
    188 } vhci_softc_t;
    189 
    190 typedef struct {
    191 	u_int port;
    192 	vhci_softc_t *softc;
    193 } vhci_fd_t;
    194 
    195 extern struct cfdriver vhci_cd;
    196 
    197 /* -------------------------------------------------------------------------- */
    198 
    199 static void
    200 vhci_pkt_create(vhci_port_t *port, struct usbd_xfer *xfer, bool usb_to_host)
    201 {
    202 	vhci_xfer_t *vxfer = (vhci_xfer_t *)xfer;
    203 	vhci_packet_list_t *reqlist, *pktlist;
    204 	vhci_packet_t *req, *pkt = NULL;
    205 	size_t refcnt = 0;
    206 
    207 	/* Setup packet. */
    208 	reqlist = &port->pkts_device_ctrl.host_to_usb;
    209 	req = kmem_zalloc(sizeof(*req), KM_SLEEP);
    210 	req->xfer = xfer;
    211 	req->utoh = false;
    212 	req->buf = (uint8_t *)&xfer->ux_request;
    213 	req->size = sizeof(xfer->ux_request);
    214 	req->cursor = 0;
    215 	refcnt++;
    216 
    217 	/* Data packet. */
    218 	if (xfer->ux_length > 0) {
    219 		if (usb_to_host) {
    220 			pktlist = &port->pkts_device_ctrl.usb_to_host;
    221 		} else {
    222 			pktlist = &port->pkts_device_ctrl.host_to_usb;
    223 		}
    224 		pkt = kmem_zalloc(sizeof(*pkt), KM_SLEEP);
    225 		pkt->xfer = xfer;
    226 		pkt->utoh = usb_to_host;
    227 		pkt->buf = xfer->ux_buf;
    228 		pkt->size = xfer->ux_length;
    229 		pkt->cursor = 0;
    230 		refcnt++;
    231 	}
    232 
    233 	/* Insert in the xfer. */
    234 	vxfer->refcnt = refcnt;
    235 	vxfer->port = port;
    236 	TAILQ_INIT(&vxfer->pkts);
    237 	TAILQ_INSERT_TAIL(&vxfer->pkts, req, xferlist);
    238 	if (pkt != NULL)
    239 		TAILQ_INSERT_TAIL(&vxfer->pkts, pkt, xferlist);
    240 
    241 	/* Insert in the port. */
    242 	KASSERT(mutex_owned(&port->lock));
    243 	TAILQ_INSERT_TAIL(reqlist, req, portlist);
    244 	if (pkt != NULL)
    245 		TAILQ_INSERT_TAIL(pktlist, pkt, portlist);
    246 }
    247 
    248 static void
    249 vhci_pkt_destroy(vhci_softc_t *sc, vhci_packet_t *pkt)
    250 {
    251 	struct usbd_xfer *xfer = pkt->xfer;
    252 	vhci_xfer_t *vxfer = (vhci_xfer_t *)xfer;
    253 	vhci_port_t *port = vxfer->port;
    254 	vhci_packet_list_t *pktlist;
    255 
    256 	KASSERT(mutex_owned(&port->lock));
    257 
    258 	if (pkt->utoh) {
    259 		pktlist = &port->pkts_device_ctrl.usb_to_host;
    260 	} else {
    261 		pktlist = &port->pkts_device_ctrl.host_to_usb;
    262 	}
    263 	TAILQ_REMOVE(pktlist, pkt, portlist);
    264 
    265 	TAILQ_REMOVE(&vxfer->pkts, pkt, xferlist);
    266 	kmem_free(pkt, sizeof(*pkt));
    267 
    268 	KASSERT(vxfer->refcnt > 0);
    269 	vxfer->refcnt--;
    270 	if (vxfer->refcnt > 0)
    271 		return;
    272 
    273 	KASSERT(TAILQ_FIRST(&vxfer->pkts) == NULL);
    274 }
    275 
    276 /* -------------------------------------------------------------------------- */
    277 
    278 static usbd_status
    279 vhci_open(struct usbd_pipe *pipe)
    280 {
    281 	struct usbd_device *dev = pipe->up_dev;
    282 	struct usbd_bus *bus = dev->ud_bus;
    283 	usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
    284 	vhci_softc_t *sc = bus->ub_hcpriv;
    285 	uint8_t addr = dev->ud_addr;
    286 
    287 	if (sc->sc_dying)
    288 		return USBD_IOERROR;
    289 
    290 	DPRINTF("%s: called, type=%d\n", __func__,
    291 	    UE_GET_XFERTYPE(ed->bmAttributes));
    292 
    293 	if (addr == bus->ub_rhaddr) {
    294 		switch (ed->bEndpointAddress) {
    295 		case USB_CONTROL_ENDPOINT:
    296 			DPRINTF("%s: roothub_ctrl\n", __func__);
    297 			pipe->up_methods = &roothub_ctrl_methods;
    298 			break;
    299 		case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
    300 			DPRINTF("%s: root_intr\n", __func__);
    301 			pipe->up_methods = &vhci_root_intr_methods;
    302 			break;
    303 		default:
    304 			DPRINTF("%s: inval\n", __func__);
    305 			return USBD_INVAL;
    306 		}
    307 	} else {
    308 		switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
    309 		case UE_CONTROL:
    310 			pipe->up_methods = &vhci_device_ctrl_methods;
    311 			break;
    312 		case UE_BULK:
    313 		case UE_INTERRUPT:
    314 		default:
    315 			goto bad;
    316 		}
    317 	}
    318 
    319 	return USBD_NORMAL_COMPLETION;
    320 
    321 bad:
    322 	return USBD_NOMEM;
    323 }
    324 
    325 static void
    326 vhci_softintr(void *v)
    327 {
    328 	DPRINTF("%s: called\n", __func__);
    329 }
    330 
    331 static struct usbd_xfer *
    332 vhci_allocx(struct usbd_bus *bus, unsigned int nframes)
    333 {
    334 	vhci_xfer_t *vxfer;
    335 
    336 	vxfer = kmem_zalloc(sizeof(*vxfer), KM_SLEEP);
    337 #ifdef DIAGNOSTIC
    338 	vxfer->xfer.ux_state = XFER_BUSY;
    339 #endif
    340 	return (struct usbd_xfer *)vxfer;
    341 }
    342 
    343 static void
    344 vhci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
    345 {
    346 	vhci_xfer_t *vxfer = (vhci_xfer_t *)xfer;
    347 
    348 	KASSERT(vxfer->refcnt == 0);
    349 	KASSERT(TAILQ_FIRST(&vxfer->pkts) == NULL);
    350 
    351 #ifdef DIAGNOSTIC
    352 	vxfer->xfer.ux_state = XFER_FREE;
    353 #endif
    354 	kmem_free(vxfer, sizeof(*vxfer));
    355 }
    356 
    357 static void
    358 vhci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
    359 {
    360 	vhci_softc_t *sc = bus->ub_hcpriv;
    361 
    362 	*lock = &sc->sc_lock;
    363 }
    364 
    365 static int
    366 vhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
    367     void *buf, int buflen)
    368 {
    369 	vhci_softc_t *sc = bus->ub_hcpriv;
    370 	vhci_port_t *port;
    371 	usb_hub_descriptor_t hubd;
    372 	uint16_t len, value, index;
    373 	int totlen = 0;
    374 
    375 	len = UGETW(req->wLength);
    376 	value = UGETW(req->wValue);
    377 	index = UGETW(req->wIndex);
    378 
    379 	port = &sc->sc_port[VHCI_INDEX2PORT(index)];
    380 
    381 #define C(x,y) ((x) | ((y) << 8))
    382 	switch (C(req->bRequest, req->bmRequestType)) {
    383 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
    384 		switch (value) {
    385 		case C(0, UDESC_DEVICE): {
    386 			usb_device_descriptor_t devd;
    387 
    388 			totlen = uimin(buflen, sizeof(devd));
    389 			memcpy(&devd, buf, totlen);
    390 			USETW(devd.idVendor, 0);
    391 			USETW(devd.idProduct, 0);
    392 			memcpy(buf, &devd, totlen);
    393 			break;
    394 		}
    395 #define sd ((usb_string_descriptor_t *)buf)
    396 		case C(1, UDESC_STRING):
    397 			/* Vendor */
    398 			totlen = usb_makestrdesc(sd, len, "NetBSD");
    399 			break;
    400 		case C(2, UDESC_STRING):
    401 			/* Product */
    402 			totlen = usb_makestrdesc(sd, len, "VHCI root hub");
    403 			break;
    404 #undef sd
    405 		default:
    406 			/* default from usbroothub */
    407 			return buflen;
    408 		}
    409 		break;
    410 
    411 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
    412 		switch (value) {
    413 		case UHF_PORT_RESET:
    414 			if (index < 1 || index >= sc->sc_nports) {
    415 				return -1;
    416 			}
    417 			port->status |= UPS_C_PORT_RESET;
    418 			break;
    419 		case UHF_PORT_POWER:
    420 			break;
    421 		default:
    422 			return -1;
    423 		}
    424 		break;
    425 
    426 	/* Hub requests. */
    427 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
    428 		break;
    429 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
    430 		if (index < 1 || index >= sc->sc_nports) {
    431 			return -1;
    432 		}
    433 		switch (value) {
    434 		case UHF_PORT_ENABLE:
    435 			port->status &= ~UPS_PORT_ENABLED;
    436 			break;
    437 		case UHF_C_PORT_ENABLE:
    438 			port->change |= UPS_C_PORT_ENABLED;
    439 			break;
    440 		default:
    441 			return -1;
    442 		}
    443 		break;
    444 
    445 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
    446 		totlen = uimin(buflen, sizeof(hubd));
    447 		memcpy(&hubd, buf, totlen);
    448 		hubd.bNbrPorts = sc->sc_nports - 1;
    449 		hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE;
    450 		totlen = uimin(totlen, hubd.bDescLength);
    451 		memcpy(buf, &hubd, totlen);
    452 		break;
    453 
    454 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
    455 		/* XXX The other HCs do this */
    456 		memset(buf, 0, len);
    457 		totlen = len;
    458 		break;
    459 
    460 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): {
    461 		usb_port_status_t ps;
    462 
    463 		if (index < 1 || index >= sc->sc_nports) {
    464 			return -1;
    465 		}
    466 		USETW(ps.wPortStatus, port->status);
    467 		USETW(ps.wPortChange, port->change);
    468 		totlen = uimin(len, sizeof(ps));
    469 		memcpy(buf, &ps, totlen);
    470 		break;
    471 	}
    472 	default:
    473 		/* default from usbroothub */
    474 		return buflen;
    475 	}
    476 
    477 	return totlen;
    478 }
    479 
    480 /* -------------------------------------------------------------------------- */
    481 
    482 static usbd_status
    483 vhci_device_ctrl_transfer(struct usbd_xfer *xfer)
    484 {
    485 	vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
    486 	usbd_status err;
    487 
    488 	DPRINTF("%s: called\n", __func__);
    489 
    490 	/* Insert last in queue. */
    491 	mutex_enter(&sc->sc_lock);
    492 	err = usb_insert_transfer(xfer);
    493 	mutex_exit(&sc->sc_lock);
    494 	if (err)
    495 		return err;
    496 
    497 	/* Pipe isn't running, start first */
    498 	return vhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
    499 }
    500 
    501 static usbd_status
    502 vhci_device_ctrl_start(struct usbd_xfer *xfer)
    503 {
    504 	usb_device_request_t *req = &xfer->ux_request;
    505 	struct usbd_device *dev = xfer->ux_pipe->up_dev;
    506 	vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
    507 	vhci_port_t *port;
    508 	bool polling = sc->sc_bus.ub_usepolling;
    509 	bool isread = (req->bmRequestType & UT_READ) != 0;
    510 	int portno, ret;
    511 
    512 	KASSERT(xfer->ux_rqflags & URQ_REQUEST);
    513 	KASSERT(dev->ud_myhsport != NULL);
    514 	portno = dev->ud_myhsport->up_portno;
    515 
    516 	DPRINTF("%s: type=0x%02x, len=%d, isread=%d, portno=%d\n",
    517 	    __func__, req->bmRequestType, UGETW(req->wLength), isread, portno);
    518 
    519 	if (sc->sc_dying)
    520 		return USBD_IOERROR;
    521 
    522 	port = &sc->sc_port[portno];
    523 
    524 	if (!polling)
    525 		mutex_enter(&sc->sc_lock);
    526 
    527 	mutex_enter(&port->lock);
    528 	if (port->status & UPS_PORT_ENABLED) {
    529 		xfer->ux_status = USBD_IN_PROGRESS;
    530 		vhci_pkt_create(port, xfer, isread);
    531 		ret = USBD_IN_PROGRESS;
    532 	} else {
    533 		ret = USBD_IOERROR;
    534 	}
    535 	mutex_exit(&port->lock);
    536 
    537 	if (!polling)
    538 		mutex_exit(&sc->sc_lock);
    539 
    540 	return ret;
    541 }
    542 
    543 static void
    544 vhci_device_ctrl_abort(struct usbd_xfer *xfer)
    545 {
    546 	vhci_xfer_t *vxfer = (vhci_xfer_t *)xfer;
    547 	vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
    548 	vhci_port_t *port = vxfer->port;
    549 	vhci_packet_t *pkt;
    550 
    551 	DPRINTF("%s: called\n", __func__);
    552 
    553 	KASSERT(mutex_owned(&sc->sc_lock));
    554 
    555 	callout_halt(&xfer->ux_callout, &sc->sc_lock);
    556 
    557 	KASSERT(xfer->ux_status != USBD_CANCELLED);
    558 
    559 	/* If anyone else beat us, we're done.  */
    560 	if (xfer->ux_status != USBD_IN_PROGRESS)
    561 		return;
    562 
    563 	mutex_enter(&port->lock);
    564 	while (vxfer->refcnt > 0) {
    565 		pkt = TAILQ_FIRST(&vxfer->pkts);
    566 		KASSERT(pkt != NULL);
    567 		vhci_pkt_destroy(sc, pkt);
    568 	}
    569 	KASSERT(TAILQ_FIRST(&vxfer->pkts) == NULL);
    570 	mutex_exit(&port->lock);
    571 
    572 	xfer->ux_status = USBD_CANCELLED;
    573 	usb_transfer_complete(xfer);
    574 	KASSERT(mutex_owned(&sc->sc_lock));
    575 }
    576 
    577 static void
    578 vhci_device_ctrl_close(struct usbd_pipe *pipe)
    579 {
    580 	DPRINTF("%s: called\n", __func__);
    581 }
    582 
    583 static void
    584 vhci_device_ctrl_cleartoggle(struct usbd_pipe *pipe)
    585 {
    586 	DPRINTF("%s: called\n", __func__);
    587 }
    588 
    589 static void
    590 vhci_device_ctrl_done(struct usbd_xfer *xfer)
    591 {
    592 	DPRINTF("%s: called\n", __func__);
    593 }
    594 
    595 /* -------------------------------------------------------------------------- */
    596 
    597 static usbd_status
    598 vhci_root_intr_transfer(struct usbd_xfer *xfer)
    599 {
    600 	vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
    601 	usbd_status err;
    602 
    603 	DPRINTF("%s: called\n", __func__);
    604 
    605 	/* Insert last in queue. */
    606 	mutex_enter(&sc->sc_lock);
    607 	err = usb_insert_transfer(xfer);
    608 	mutex_exit(&sc->sc_lock);
    609 	if (err)
    610 		return err;
    611 
    612 	/* Pipe isn't running, start first */
    613 	return vhci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
    614 }
    615 
    616 static usbd_status
    617 vhci_root_intr_start(struct usbd_xfer *xfer)
    618 {
    619 	vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
    620 	const bool polling = sc->sc_bus.ub_usepolling;
    621 
    622 	DPRINTF("%s: called, len=%zu\n", __func__, (size_t)xfer->ux_length);
    623 
    624 	if (sc->sc_dying)
    625 		return USBD_IOERROR;
    626 
    627 	if (!polling)
    628 		mutex_enter(&sc->sc_lock);
    629 	sc->sc_intrxfer = xfer;
    630 	if (!polling)
    631 		mutex_exit(&sc->sc_lock);
    632 
    633 	return USBD_IN_PROGRESS;
    634 }
    635 
    636 static void
    637 vhci_root_intr_abort(struct usbd_xfer *xfer)
    638 {
    639 	vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
    640 
    641 	DPRINTF("%s: called\n", __func__);
    642 
    643 	KASSERT(mutex_owned(&sc->sc_lock));
    644 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
    645 
    646 	sc->sc_intrxfer = NULL;
    647 
    648 	xfer->ux_status = USBD_CANCELLED;
    649 	usb_transfer_complete(xfer);
    650 }
    651 
    652 static void
    653 vhci_root_intr_close(struct usbd_pipe *pipe)
    654 {
    655 	vhci_softc_t *sc = pipe->up_dev->ud_bus->ub_hcpriv;
    656 
    657 	DPRINTF("%s: called\n", __func__);
    658 
    659 	KASSERT(mutex_owned(&sc->sc_lock));
    660 
    661 	sc->sc_intrxfer = NULL;
    662 }
    663 
    664 static void
    665 vhci_root_intr_cleartoggle(struct usbd_pipe *pipe)
    666 {
    667 	DPRINTF("%s: called\n", __func__);
    668 }
    669 
    670 static void
    671 vhci_root_intr_done(struct usbd_xfer *xfer)
    672 {
    673 }
    674 
    675 /* -------------------------------------------------------------------------- */
    676 
    677 struct vhci_ioc_get_info {
    678 	/* General. */
    679 	size_t nports;
    680 
    681 	/* Current port. */
    682 	u_int port;
    683 	int status;
    684 };
    685 
    686 struct vhci_ioc_set_port {
    687 	u_int port;
    688 };
    689 
    690 struct vhci_ioc_usb_attach {
    691 	u_int port;
    692 };
    693 
    694 struct vhci_ioc_usb_detach {
    695 	u_int port;
    696 };
    697 
    698 #define VHCI_IOC_GET_INFO	_IOR('V', 0, struct vhci_ioc_get_info)
    699 #define VHCI_IOC_SET_PORT	_IOW('V', 1, struct vhci_ioc_set_port)
    700 #define VHCI_IOC_USB_ATTACH	_IOW('V', 10, struct vhci_ioc_usb_attach)
    701 #define VHCI_IOC_USB_DETACH	_IOW('V', 11, struct vhci_ioc_usb_detach)
    702 
    703 static int
    704 vhci_usb_attach(vhci_fd_t *vfd, struct vhci_ioc_usb_attach *args)
    705 {
    706 	vhci_softc_t *sc = vfd->softc;
    707 	vhci_port_t *port;
    708 	struct usbd_xfer *xfer;
    709 	u_char *p;
    710 	int ret = 0;
    711 
    712 	if (args->port == 0 || args->port >= sc->sc_nports)
    713 		return EINVAL;
    714 	port = &sc->sc_port[args->port];
    715 
    716 	mutex_enter(&sc->sc_lock);
    717 
    718 	mutex_enter(&port->lock);
    719 	port->status = UPS_CURRENT_CONNECT_STATUS | UPS_PORT_ENABLED |
    720 	    UPS_PORT_POWER;
    721 	port->change = UPS_C_CONNECT_STATUS | UPS_C_PORT_RESET;
    722 	mutex_exit(&port->lock);
    723 
    724 	xfer = sc->sc_intrxfer;
    725 
    726 	if (xfer == NULL) {
    727 		ret = ENOBUFS;
    728 		goto done;
    729 	}
    730 
    731 	p = xfer->ux_buf;
    732 	memset(p, 0, xfer->ux_length);
    733 	p[0] = __BIT(args->port);
    734 	xfer->ux_actlen = xfer->ux_length;
    735 	xfer->ux_status = USBD_NORMAL_COMPLETION;
    736 
    737 	usb_transfer_complete(xfer);
    738 
    739 done:
    740 	mutex_exit(&sc->sc_lock);
    741 	return ret;
    742 }
    743 
    744 static void
    745 vhci_port_flush(vhci_softc_t *sc, vhci_port_t *port)
    746 {
    747 	vhci_packet_list_t *pktlist;
    748 	vhci_packet_t *pkt, *nxt;
    749 	vhci_xfer_list_t vxferlist;
    750 	vhci_xfer_t *vxfer;
    751 
    752 	KASSERT(mutex_owned(&sc->sc_lock));
    753 	KASSERT(mutex_owned(&port->lock));
    754 
    755 	TAILQ_INIT(&vxferlist);
    756 
    757 	pktlist = &port->pkts_device_ctrl.host_to_usb;
    758 	TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
    759 		vxfer = (vhci_xfer_t *)pkt->xfer;
    760 		KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
    761 		vhci_pkt_destroy(sc, pkt);
    762 		if (vxfer->refcnt == 0)
    763 			TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
    764 	}
    765 	KASSERT(TAILQ_FIRST(pktlist) == NULL);
    766 
    767 	pktlist = &port->pkts_device_ctrl.usb_to_host;
    768 	TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
    769 		vxfer = (vhci_xfer_t *)pkt->xfer;
    770 		KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
    771 		vhci_pkt_destroy(sc, pkt);
    772 		if (vxfer->refcnt == 0)
    773 			TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
    774 	}
    775 	KASSERT(TAILQ_FIRST(pktlist) == NULL);
    776 
    777 	while ((vxfer = TAILQ_FIRST(&vxferlist)) != NULL) {
    778 		struct usbd_xfer *xfer = &vxfer->xfer;
    779 		TAILQ_REMOVE(&vxferlist, vxfer, freelist);
    780 
    781 		xfer->ux_status = USBD_TIMEOUT;
    782 		usb_transfer_complete(xfer);
    783 	}
    784 }
    785 
    786 static int
    787 vhci_usb_detach(vhci_fd_t *vfd, struct vhci_ioc_usb_detach *args)
    788 {
    789 	vhci_softc_t *sc = vfd->softc;
    790 	vhci_port_t *port;
    791 	struct usbd_xfer *xfer;
    792 	u_char *p;
    793 
    794 	if (args->port == 0 || args->port >= sc->sc_nports)
    795 		return EINVAL;
    796 	port = &sc->sc_port[args->port];
    797 
    798 	mutex_enter(&sc->sc_lock);
    799 
    800 	xfer = sc->sc_intrxfer;
    801 	if (xfer == NULL) {
    802 		mutex_exit(&sc->sc_lock);
    803 		return ENOBUFS;
    804 	}
    805 
    806 	mutex_enter(&port->lock);
    807 
    808 	port->status = 0;
    809 	port->change = UPS_C_CONNECT_STATUS | UPS_C_PORT_RESET;
    810 
    811 	p = xfer->ux_buf;
    812 	memset(p, 0, xfer->ux_length);
    813 	p[0] = __BIT(args->port);
    814 	xfer->ux_actlen = xfer->ux_length;
    815 	xfer->ux_status = USBD_NORMAL_COMPLETION;
    816 
    817 	usb_transfer_complete(xfer);
    818 	vhci_port_flush(sc, port);
    819 
    820 	mutex_exit(&port->lock);
    821 	mutex_exit(&sc->sc_lock);
    822 	return 0;
    823 }
    824 
    825 static int
    826 vhci_get_info(vhci_fd_t *vfd, struct vhci_ioc_get_info *args)
    827 {
    828 	vhci_softc_t *sc = vfd->softc;
    829 	vhci_port_t *port;
    830 
    831 	port = &sc->sc_port[vfd->port];
    832 
    833 	args->nports = VHCI_NPORTS;
    834 	args->port = vfd->port;
    835 	mutex_enter(&port->lock);
    836 	args->status = port->status;
    837 	mutex_exit(&port->lock);
    838 
    839 	return 0;
    840 }
    841 
    842 static int
    843 vhci_set_port(vhci_fd_t *vfd, struct vhci_ioc_set_port *args)
    844 {
    845 	vhci_softc_t *sc = vfd->softc;
    846 
    847 	if (args->port == 0 || args->port >= sc->sc_nports)
    848 		return EINVAL;
    849 
    850 	vfd->port = args->port;
    851 
    852 	return 0;
    853 }
    854 
    855 /* -------------------------------------------------------------------------- */
    856 
    857 static dev_type_open(vhci_fd_open);
    858 
    859 const struct cdevsw vhci_cdevsw = {
    860 	.d_open = vhci_fd_open,
    861 	.d_close = noclose,
    862 	.d_read = noread,
    863 	.d_write = nowrite,
    864 	.d_ioctl = noioctl,
    865 	.d_stop = nostop,
    866 	.d_tty = notty,
    867 	.d_poll = nopoll,
    868 	.d_mmap = nommap,
    869 	.d_kqfilter = nokqfilter,
    870 	.d_discard = nodiscard,
    871 	.d_flag = D_OTHER | D_MPSAFE
    872 };
    873 
    874 static int vhci_fd_ioctl(file_t *, u_long, void *);
    875 static int vhci_fd_close(file_t *);
    876 static int vhci_fd_read(struct file *, off_t *, struct uio *, kauth_cred_t, int);
    877 static int vhci_fd_write(struct file *, off_t *, struct uio *, kauth_cred_t, int);
    878 
    879 const struct fileops vhci_fileops = {
    880 	.fo_read = vhci_fd_read,
    881 	.fo_write = vhci_fd_write,
    882 	.fo_ioctl = vhci_fd_ioctl,
    883 	.fo_fcntl = fnullop_fcntl,
    884 	.fo_poll = fnullop_poll,
    885 	.fo_stat = fbadop_stat,
    886 	.fo_close = vhci_fd_close,
    887 	.fo_kqfilter = fnullop_kqfilter,
    888 	.fo_restart = fnullop_restart,
    889 	.fo_mmap = NULL,
    890 };
    891 
    892 static int
    893 vhci_fd_open(dev_t dev, int flags, int type, struct lwp *l)
    894 {
    895 	vhci_fd_t *vfd;
    896 	struct file *fp;
    897 	int error, fd;
    898 
    899 	if (minor(dev) != 0)
    900 		return EXDEV;
    901 	error = fd_allocfile(&fp, &fd);
    902 	if (error)
    903 		return error;
    904 
    905 	vfd = kmem_alloc(sizeof(*vfd), KM_SLEEP);
    906 	vfd->port = 1;
    907 	vfd->softc = device_lookup_private(&vhci_cd, minor(dev));
    908 
    909 	return fd_clone(fp, fd, flags, &vhci_fileops, vfd);
    910 }
    911 
    912 static int
    913 vhci_fd_close(file_t *fp)
    914 {
    915 	struct vhci_ioc_usb_detach args;
    916 	vhci_fd_t *vfd = fp->f_data;
    917 	int ret __diagused;
    918 
    919 	KASSERT(vfd != NULL);
    920 
    921 	args.port = vfd->port;
    922 	ret = vhci_usb_detach(vfd, &args);
    923 	KASSERT(ret == 0);
    924 
    925 	kmem_free(vfd, sizeof(*vfd));
    926 	fp->f_data = NULL;
    927 
    928 	return 0;
    929 }
    930 
    931 static int
    932 vhci_fd_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    933     int flags)
    934 {
    935 	vhci_fd_t *vfd = fp->f_data;
    936 	vhci_softc_t *sc = vfd->softc;
    937 	vhci_packet_list_t *pktlist;
    938 	vhci_packet_t *pkt, *nxt;
    939 	vhci_xfer_list_t vxferlist;
    940 	vhci_xfer_t *vxfer;
    941 	vhci_port_t *port;
    942 	int error = 0;
    943 	uint8_t *buf;
    944 	size_t size;
    945 
    946 	if (uio->uio_resid == 0)
    947 		return 0;
    948 	port = &sc->sc_port[vfd->port];
    949 	pktlist = &port->pkts_device_ctrl.host_to_usb;
    950 
    951 	TAILQ_INIT(&vxferlist);
    952 
    953 	mutex_enter(&port->lock);
    954 
    955 	if (!(port->status & UPS_PORT_ENABLED)) {
    956 		error = ENOBUFS;
    957 		goto out;
    958 	}
    959 
    960 	TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
    961 		vxfer = (vhci_xfer_t *)pkt->xfer;
    962 		buf = pkt->buf + pkt->cursor;
    963 		KASSERT(pkt->size >= pkt->cursor);
    964 		size = uimin(uio->uio_resid, pkt->size - pkt->cursor);
    965 
    966 		KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
    967 
    968 		error = uiomove(buf, size, uio);
    969 		if (error) {
    970 			DPRINTF("%s: error = %d\n", __func__, error);
    971 			goto out;
    972 		}
    973 
    974 		pkt->cursor += size;
    975 
    976 		if (pkt->cursor == pkt->size) {
    977 			vhci_pkt_destroy(sc, pkt);
    978 			if (vxfer->refcnt == 0) {
    979 				TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
    980 			}
    981 		}
    982 		if (uio->uio_resid == 0) {
    983 			break;
    984 		}
    985 	}
    986 
    987 out:
    988 	mutex_exit(&port->lock);
    989 
    990 	while ((vxfer = TAILQ_FIRST(&vxferlist)) != NULL) {
    991 		struct usbd_xfer *xfer = &vxfer->xfer;
    992 		TAILQ_REMOVE(&vxferlist, vxfer, freelist);
    993 
    994 		mutex_enter(&sc->sc_lock);
    995 		xfer->ux_actlen = xfer->ux_length;
    996 		xfer->ux_status = USBD_NORMAL_COMPLETION;
    997 		usb_transfer_complete(xfer);
    998 		mutex_exit(&sc->sc_lock);
    999 	}
   1000 
   1001 	return error;
   1002 }
   1003 
   1004 static int
   1005 vhci_fd_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
   1006     int flags)
   1007 {
   1008 	vhci_fd_t *vfd = fp->f_data;
   1009 	vhci_softc_t *sc = vfd->softc;
   1010 	vhci_packet_list_t *pktlist;
   1011 	vhci_packet_t *pkt, *nxt;
   1012 	vhci_xfer_list_t vxferlist;
   1013 	vhci_xfer_t *vxfer;
   1014 	vhci_port_t *port;
   1015 	int error = 0;
   1016 	uint8_t *buf;
   1017 	size_t size;
   1018 
   1019 	if (uio->uio_resid == 0)
   1020 		return 0;
   1021 	port = &sc->sc_port[vfd->port];
   1022 	pktlist = &port->pkts_device_ctrl.usb_to_host;
   1023 
   1024 	TAILQ_INIT(&vxferlist);
   1025 
   1026 	mutex_enter(&port->lock);
   1027 
   1028 	if (!(port->status & UPS_PORT_ENABLED)) {
   1029 		error = ENOBUFS;
   1030 		goto out;
   1031 	}
   1032 
   1033 	TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
   1034 		vxfer = (vhci_xfer_t *)pkt->xfer;
   1035 		buf = pkt->buf + pkt->cursor;
   1036 		KASSERT(pkt->size >= pkt->cursor);
   1037 		size = uimin(uio->uio_resid, pkt->size - pkt->cursor);
   1038 
   1039 		KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
   1040 
   1041 		error = uiomove(buf, size, uio);
   1042 		if (error) {
   1043 			DPRINTF("%s: error = %d\n", __func__, error);
   1044 			goto out;
   1045 		}
   1046 
   1047 		pkt->cursor += size;
   1048 
   1049 		if (pkt->cursor == pkt->size) {
   1050 			vhci_pkt_destroy(sc, pkt);
   1051 			if (vxfer->refcnt == 0) {
   1052 				TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
   1053 			}
   1054 		}
   1055 		if (uio->uio_resid == 0) {
   1056 			break;
   1057 		}
   1058 	}
   1059 
   1060 out:
   1061 	mutex_exit(&port->lock);
   1062 
   1063 	while ((vxfer = TAILQ_FIRST(&vxferlist)) != NULL) {
   1064 		struct usbd_xfer *xfer = &vxfer->xfer;
   1065 		TAILQ_REMOVE(&vxferlist, vxfer, freelist);
   1066 
   1067 		mutex_enter(&sc->sc_lock);
   1068 		xfer->ux_actlen = xfer->ux_length;
   1069 		xfer->ux_status = USBD_NORMAL_COMPLETION;
   1070 		usb_transfer_complete(xfer);
   1071 		mutex_exit(&sc->sc_lock);
   1072 	}
   1073 
   1074 	return error;
   1075 }
   1076 
   1077 static int
   1078 vhci_fd_ioctl(file_t *fp, u_long cmd, void *data)
   1079 {
   1080 	vhci_fd_t *vfd = fp->f_data;
   1081 
   1082 	KASSERT(vfd != NULL);
   1083 
   1084 	switch (cmd) {
   1085 	case VHCI_IOC_GET_INFO:
   1086 		return vhci_get_info(vfd, data);
   1087 	case VHCI_IOC_SET_PORT:
   1088 		return vhci_set_port(vfd, data);
   1089 	case VHCI_IOC_USB_ATTACH:
   1090 		return vhci_usb_attach(vfd, data);
   1091 	case VHCI_IOC_USB_DETACH:
   1092 		return vhci_usb_detach(vfd, data);
   1093 	default:
   1094 		return EINVAL;
   1095 	}
   1096 }
   1097 
   1098 /* -------------------------------------------------------------------------- */
   1099 
   1100 static int vhci_match(device_t, cfdata_t, void *);
   1101 static void vhci_attach(device_t, device_t, void *);
   1102 static int vhci_activate(device_t, enum devact);
   1103 
   1104 CFATTACH_DECL_NEW(vhci, sizeof(vhci_softc_t), vhci_match, vhci_attach,
   1105     NULL, vhci_activate);
   1106 
   1107 void
   1108 vhciattach(int nunits)
   1109 {
   1110 	static struct cfdata vhci_cfdata = {
   1111 		.cf_name = "vhci",
   1112 		.cf_atname = "vhci",
   1113 		.cf_unit = 0,
   1114 		.cf_fstate = FSTATE_STAR,
   1115 	};
   1116 	int error;
   1117 
   1118 	error = config_cfattach_attach(vhci_cd.cd_name, &vhci_ca);
   1119 	if (error) {
   1120 		aprint_error("%s: unable to register cfattach\n",
   1121 		    vhci_cd.cd_name);
   1122 		(void)config_cfdriver_detach(&vhci_cd);
   1123 		return;
   1124 	}
   1125 
   1126 	config_attach_pseudo(&vhci_cfdata);
   1127 }
   1128 
   1129 static int
   1130 vhci_activate(device_t self, enum devact act)
   1131 {
   1132 	vhci_softc_t *sc = device_private(self);
   1133 
   1134 	switch (act) {
   1135 	case DVACT_DEACTIVATE:
   1136 		sc->sc_dying = 1;
   1137 		return 0;
   1138 	default:
   1139 		return EOPNOTSUPP;
   1140 	}
   1141 }
   1142 
   1143 static int
   1144 vhci_match(device_t parent, cfdata_t match, void *aux)
   1145 {
   1146 	return 1;
   1147 }
   1148 
   1149 static void
   1150 vhci_attach(device_t parent, device_t self, void *aux)
   1151 {
   1152 	vhci_softc_t *sc = device_private(self);
   1153 	vhci_port_t *port;
   1154 	size_t i;
   1155 
   1156 	sc->sc_dev = self;
   1157 	sc->sc_bus.ub_revision = USBREV_2_0;
   1158 	sc->sc_bus.ub_usedma = false;
   1159 	sc->sc_bus.ub_methods = &vhci_bus_methods;
   1160 	sc->sc_bus.ub_pipesize = sizeof(vhci_pipe_t);
   1161 	sc->sc_bus.ub_hcpriv = sc;
   1162 	sc->sc_dying = false;
   1163 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1164 
   1165 	sc->sc_nports = VHCI_NPORTS;
   1166 	for (i = 0; i < sc->sc_nports; i++) {
   1167 		port = &sc->sc_port[i];
   1168 		mutex_init(&port->lock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1169 		TAILQ_INIT(&port->pkts_device_ctrl.usb_to_host);
   1170 		TAILQ_INIT(&port->pkts_device_ctrl.host_to_usb);
   1171 	}
   1172 
   1173 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
   1174 }
   1175