Home | History | Annotate | Line # | Download | only in usb
vhci.c revision 1.4
      1 /*	$NetBSD: vhci.c,v 1.4 2019/11/17 11:28:48 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.4 2019/11/17 11:28:48 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 #define C(x,y) ((x) | ((y) << 8))
    380 	switch (C(req->bRequest, req->bmRequestType)) {
    381 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
    382 		switch (value) {
    383 		case C(0, UDESC_DEVICE): {
    384 			usb_device_descriptor_t devd;
    385 
    386 			totlen = uimin(buflen, sizeof(devd));
    387 			memcpy(&devd, buf, totlen);
    388 			USETW(devd.idVendor, 0);
    389 			USETW(devd.idProduct, 0);
    390 			memcpy(buf, &devd, totlen);
    391 			break;
    392 		}
    393 #define sd ((usb_string_descriptor_t *)buf)
    394 		case C(1, UDESC_STRING):
    395 			/* Vendor */
    396 			totlen = usb_makestrdesc(sd, len, "NetBSD");
    397 			break;
    398 		case C(2, UDESC_STRING):
    399 			/* Product */
    400 			totlen = usb_makestrdesc(sd, len, "VHCI root hub");
    401 			break;
    402 #undef sd
    403 		default:
    404 			/* default from usbroothub */
    405 			return buflen;
    406 		}
    407 		break;
    408 
    409 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
    410 		switch (value) {
    411 		case UHF_PORT_RESET:
    412 			if (index < 1 || index >= sc->sc_nports) {
    413 				return -1;
    414 			}
    415 			port = &sc->sc_port[VHCI_INDEX2PORT(index)];
    416 			port->status |= UPS_C_PORT_RESET;
    417 			break;
    418 		case UHF_PORT_POWER:
    419 			break;
    420 		default:
    421 			return -1;
    422 		}
    423 		break;
    424 
    425 	/* Hub requests. */
    426 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
    427 		break;
    428 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
    429 		if (index < 1 || index >= sc->sc_nports) {
    430 			return -1;
    431 		}
    432 		port = &sc->sc_port[VHCI_INDEX2PORT(index)];
    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 		port = &sc->sc_port[VHCI_INDEX2PORT(index)];
    467 		USETW(ps.wPortStatus, port->status);
    468 		USETW(ps.wPortChange, port->change);
    469 		totlen = uimin(len, sizeof(ps));
    470 		memcpy(buf, &ps, totlen);
    471 		break;
    472 	}
    473 	default:
    474 		/* default from usbroothub */
    475 		return buflen;
    476 	}
    477 
    478 	return totlen;
    479 }
    480 
    481 /* -------------------------------------------------------------------------- */
    482 
    483 static usbd_status
    484 vhci_device_ctrl_transfer(struct usbd_xfer *xfer)
    485 {
    486 	vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
    487 	usbd_status err;
    488 
    489 	DPRINTF("%s: called\n", __func__);
    490 
    491 	/* Insert last in queue. */
    492 	mutex_enter(&sc->sc_lock);
    493 	err = usb_insert_transfer(xfer);
    494 	mutex_exit(&sc->sc_lock);
    495 	if (err)
    496 		return err;
    497 
    498 	/* Pipe isn't running, start first */
    499 	return vhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
    500 }
    501 
    502 static usbd_status
    503 vhci_device_ctrl_start(struct usbd_xfer *xfer)
    504 {
    505 	usb_device_request_t *req = &xfer->ux_request;
    506 	struct usbd_device *dev = xfer->ux_pipe->up_dev;
    507 	vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
    508 	vhci_port_t *port;
    509 	bool polling = sc->sc_bus.ub_usepolling;
    510 	bool isread = (req->bmRequestType & UT_READ) != 0;
    511 	int portno, ret;
    512 
    513 	KASSERT(xfer->ux_rqflags & URQ_REQUEST);
    514 	KASSERT(dev->ud_myhsport != NULL);
    515 	portno = dev->ud_myhsport->up_portno;
    516 
    517 	DPRINTF("%s: type=0x%02x, len=%d, isread=%d, portno=%d\n",
    518 	    __func__, req->bmRequestType, UGETW(req->wLength), isread, portno);
    519 
    520 	if (sc->sc_dying)
    521 		return USBD_IOERROR;
    522 
    523 	port = &sc->sc_port[portno];
    524 
    525 	if (!polling)
    526 		mutex_enter(&sc->sc_lock);
    527 
    528 	mutex_enter(&port->lock);
    529 	if (port->status & UPS_PORT_ENABLED) {
    530 		xfer->ux_status = USBD_IN_PROGRESS;
    531 		vhci_pkt_create(port, xfer, isread);
    532 		ret = USBD_IN_PROGRESS;
    533 	} else {
    534 		ret = USBD_IOERROR;
    535 	}
    536 	mutex_exit(&port->lock);
    537 
    538 	if (!polling)
    539 		mutex_exit(&sc->sc_lock);
    540 
    541 	return ret;
    542 }
    543 
    544 static void
    545 vhci_device_ctrl_abort(struct usbd_xfer *xfer)
    546 {
    547 	vhci_xfer_t *vxfer = (vhci_xfer_t *)xfer;
    548 	vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
    549 	vhci_port_t *port = vxfer->port;
    550 	vhci_packet_t *pkt;
    551 
    552 	DPRINTF("%s: called\n", __func__);
    553 
    554 	KASSERT(mutex_owned(&sc->sc_lock));
    555 
    556 	callout_halt(&xfer->ux_callout, &sc->sc_lock);
    557 
    558 	KASSERT(xfer->ux_status != USBD_CANCELLED);
    559 
    560 	/* If anyone else beat us, we're done.  */
    561 	if (xfer->ux_status != USBD_IN_PROGRESS)
    562 		return;
    563 
    564 	mutex_enter(&port->lock);
    565 	while (vxfer->refcnt > 0) {
    566 		pkt = TAILQ_FIRST(&vxfer->pkts);
    567 		KASSERT(pkt != NULL);
    568 		vhci_pkt_destroy(sc, pkt);
    569 	}
    570 	KASSERT(TAILQ_FIRST(&vxfer->pkts) == NULL);
    571 	mutex_exit(&port->lock);
    572 
    573 	xfer->ux_status = USBD_CANCELLED;
    574 	usb_transfer_complete(xfer);
    575 	KASSERT(mutex_owned(&sc->sc_lock));
    576 }
    577 
    578 static void
    579 vhci_device_ctrl_close(struct usbd_pipe *pipe)
    580 {
    581 	DPRINTF("%s: called\n", __func__);
    582 }
    583 
    584 static void
    585 vhci_device_ctrl_cleartoggle(struct usbd_pipe *pipe)
    586 {
    587 	DPRINTF("%s: called\n", __func__);
    588 }
    589 
    590 static void
    591 vhci_device_ctrl_done(struct usbd_xfer *xfer)
    592 {
    593 	DPRINTF("%s: called\n", __func__);
    594 }
    595 
    596 /* -------------------------------------------------------------------------- */
    597 
    598 static usbd_status
    599 vhci_root_intr_transfer(struct usbd_xfer *xfer)
    600 {
    601 	vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
    602 	usbd_status err;
    603 
    604 	DPRINTF("%s: called\n", __func__);
    605 
    606 	/* Insert last in queue. */
    607 	mutex_enter(&sc->sc_lock);
    608 	err = usb_insert_transfer(xfer);
    609 	mutex_exit(&sc->sc_lock);
    610 	if (err)
    611 		return err;
    612 
    613 	/* Pipe isn't running, start first */
    614 	return vhci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
    615 }
    616 
    617 static usbd_status
    618 vhci_root_intr_start(struct usbd_xfer *xfer)
    619 {
    620 	vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
    621 	const bool polling = sc->sc_bus.ub_usepolling;
    622 
    623 	DPRINTF("%s: called, len=%zu\n", __func__, (size_t)xfer->ux_length);
    624 
    625 	if (sc->sc_dying)
    626 		return USBD_IOERROR;
    627 
    628 	if (!polling)
    629 		mutex_enter(&sc->sc_lock);
    630 	sc->sc_intrxfer = xfer;
    631 	if (!polling)
    632 		mutex_exit(&sc->sc_lock);
    633 
    634 	return USBD_IN_PROGRESS;
    635 }
    636 
    637 static void
    638 vhci_root_intr_abort(struct usbd_xfer *xfer)
    639 {
    640 	vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
    641 
    642 	DPRINTF("%s: called\n", __func__);
    643 
    644 	KASSERT(mutex_owned(&sc->sc_lock));
    645 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
    646 
    647 	sc->sc_intrxfer = NULL;
    648 
    649 	xfer->ux_status = USBD_CANCELLED;
    650 	usb_transfer_complete(xfer);
    651 }
    652 
    653 static void
    654 vhci_root_intr_close(struct usbd_pipe *pipe)
    655 {
    656 	vhci_softc_t *sc = pipe->up_dev->ud_bus->ub_hcpriv;
    657 
    658 	DPRINTF("%s: called\n", __func__);
    659 
    660 	KASSERT(mutex_owned(&sc->sc_lock));
    661 
    662 	sc->sc_intrxfer = NULL;
    663 }
    664 
    665 static void
    666 vhci_root_intr_cleartoggle(struct usbd_pipe *pipe)
    667 {
    668 	DPRINTF("%s: called\n", __func__);
    669 }
    670 
    671 static void
    672 vhci_root_intr_done(struct usbd_xfer *xfer)
    673 {
    674 }
    675 
    676 /* -------------------------------------------------------------------------- */
    677 
    678 struct vhci_ioc_get_info {
    679 	/* General. */
    680 	size_t nports;
    681 
    682 	/* Current port. */
    683 	u_int port;
    684 	int status;
    685 };
    686 
    687 struct vhci_ioc_set_port {
    688 	u_int port;
    689 };
    690 
    691 struct vhci_ioc_usb_attach {
    692 	u_int port;
    693 };
    694 
    695 struct vhci_ioc_usb_detach {
    696 	u_int port;
    697 };
    698 
    699 #define VHCI_IOC_GET_INFO	_IOR('V', 0, struct vhci_ioc_get_info)
    700 #define VHCI_IOC_SET_PORT	_IOW('V', 1, struct vhci_ioc_set_port)
    701 #define VHCI_IOC_USB_ATTACH	_IOW('V', 10, struct vhci_ioc_usb_attach)
    702 #define VHCI_IOC_USB_DETACH	_IOW('V', 11, struct vhci_ioc_usb_detach)
    703 
    704 static int
    705 vhci_usb_attach(vhci_fd_t *vfd, struct vhci_ioc_usb_attach *args)
    706 {
    707 	vhci_softc_t *sc = vfd->softc;
    708 	vhci_port_t *port;
    709 	struct usbd_xfer *xfer;
    710 	u_char *p;
    711 	int ret = 0;
    712 
    713 	if (args->port == 0 || args->port >= sc->sc_nports)
    714 		return EINVAL;
    715 	port = &sc->sc_port[args->port];
    716 
    717 	mutex_enter(&sc->sc_lock);
    718 
    719 	mutex_enter(&port->lock);
    720 	port->status = UPS_CURRENT_CONNECT_STATUS | UPS_PORT_ENABLED |
    721 	    UPS_PORT_POWER;
    722 	port->change = UPS_C_CONNECT_STATUS | UPS_C_PORT_RESET;
    723 	mutex_exit(&port->lock);
    724 
    725 	xfer = sc->sc_intrxfer;
    726 
    727 	if (xfer == NULL) {
    728 		ret = ENOBUFS;
    729 		goto done;
    730 	}
    731 
    732 	p = xfer->ux_buf;
    733 	memset(p, 0, xfer->ux_length);
    734 	p[0] = __BIT(args->port);
    735 	xfer->ux_actlen = xfer->ux_length;
    736 	xfer->ux_status = USBD_NORMAL_COMPLETION;
    737 
    738 	usb_transfer_complete(xfer);
    739 
    740 done:
    741 	mutex_exit(&sc->sc_lock);
    742 	return ret;
    743 }
    744 
    745 static void
    746 vhci_port_flush(vhci_softc_t *sc, vhci_port_t *port)
    747 {
    748 	vhci_packet_list_t *pktlist;
    749 	vhci_packet_t *pkt, *nxt;
    750 	vhci_xfer_list_t vxferlist;
    751 	vhci_xfer_t *vxfer;
    752 
    753 	KASSERT(mutex_owned(&sc->sc_lock));
    754 	KASSERT(mutex_owned(&port->lock));
    755 
    756 	TAILQ_INIT(&vxferlist);
    757 
    758 	pktlist = &port->pkts_device_ctrl.host_to_usb;
    759 	TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
    760 		vxfer = (vhci_xfer_t *)pkt->xfer;
    761 		KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
    762 		vhci_pkt_destroy(sc, pkt);
    763 		if (vxfer->refcnt == 0)
    764 			TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
    765 	}
    766 	KASSERT(TAILQ_FIRST(pktlist) == NULL);
    767 
    768 	pktlist = &port->pkts_device_ctrl.usb_to_host;
    769 	TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
    770 		vxfer = (vhci_xfer_t *)pkt->xfer;
    771 		KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
    772 		vhci_pkt_destroy(sc, pkt);
    773 		if (vxfer->refcnt == 0)
    774 			TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
    775 	}
    776 	KASSERT(TAILQ_FIRST(pktlist) == NULL);
    777 
    778 	while ((vxfer = TAILQ_FIRST(&vxferlist)) != NULL) {
    779 		struct usbd_xfer *xfer = &vxfer->xfer;
    780 		TAILQ_REMOVE(&vxferlist, vxfer, freelist);
    781 
    782 		xfer->ux_status = USBD_TIMEOUT;
    783 		usb_transfer_complete(xfer);
    784 	}
    785 }
    786 
    787 static int
    788 vhci_usb_detach(vhci_fd_t *vfd, struct vhci_ioc_usb_detach *args)
    789 {
    790 	vhci_softc_t *sc = vfd->softc;
    791 	vhci_port_t *port;
    792 	struct usbd_xfer *xfer;
    793 	u_char *p;
    794 
    795 	if (args->port == 0 || args->port >= sc->sc_nports)
    796 		return EINVAL;
    797 	port = &sc->sc_port[args->port];
    798 
    799 	mutex_enter(&sc->sc_lock);
    800 
    801 	xfer = sc->sc_intrxfer;
    802 	if (xfer == NULL) {
    803 		mutex_exit(&sc->sc_lock);
    804 		return ENOBUFS;
    805 	}
    806 
    807 	mutex_enter(&port->lock);
    808 
    809 	port->status = 0;
    810 	port->change = UPS_C_CONNECT_STATUS | UPS_C_PORT_RESET;
    811 
    812 	p = xfer->ux_buf;
    813 	memset(p, 0, xfer->ux_length);
    814 	p[0] = __BIT(args->port);
    815 	xfer->ux_actlen = xfer->ux_length;
    816 	xfer->ux_status = USBD_NORMAL_COMPLETION;
    817 
    818 	usb_transfer_complete(xfer);
    819 	vhci_port_flush(sc, port);
    820 
    821 	mutex_exit(&port->lock);
    822 	mutex_exit(&sc->sc_lock);
    823 	return 0;
    824 }
    825 
    826 static int
    827 vhci_get_info(vhci_fd_t *vfd, struct vhci_ioc_get_info *args)
    828 {
    829 	vhci_softc_t *sc = vfd->softc;
    830 	vhci_port_t *port;
    831 
    832 	port = &sc->sc_port[vfd->port];
    833 
    834 	args->nports = VHCI_NPORTS;
    835 	args->port = vfd->port;
    836 	mutex_enter(&port->lock);
    837 	args->status = port->status;
    838 	mutex_exit(&port->lock);
    839 
    840 	return 0;
    841 }
    842 
    843 static int
    844 vhci_set_port(vhci_fd_t *vfd, struct vhci_ioc_set_port *args)
    845 {
    846 	vhci_softc_t *sc = vfd->softc;
    847 
    848 	if (args->port == 0 || args->port >= sc->sc_nports)
    849 		return EINVAL;
    850 
    851 	vfd->port = args->port;
    852 
    853 	return 0;
    854 }
    855 
    856 /* -------------------------------------------------------------------------- */
    857 
    858 static dev_type_open(vhci_fd_open);
    859 
    860 const struct cdevsw vhci_cdevsw = {
    861 	.d_open = vhci_fd_open,
    862 	.d_close = noclose,
    863 	.d_read = noread,
    864 	.d_write = nowrite,
    865 	.d_ioctl = noioctl,
    866 	.d_stop = nostop,
    867 	.d_tty = notty,
    868 	.d_poll = nopoll,
    869 	.d_mmap = nommap,
    870 	.d_kqfilter = nokqfilter,
    871 	.d_discard = nodiscard,
    872 	.d_flag = D_OTHER | D_MPSAFE
    873 };
    874 
    875 static int vhci_fd_ioctl(file_t *, u_long, void *);
    876 static int vhci_fd_close(file_t *);
    877 static int vhci_fd_read(struct file *, off_t *, struct uio *, kauth_cred_t, int);
    878 static int vhci_fd_write(struct file *, off_t *, struct uio *, kauth_cred_t, int);
    879 
    880 const struct fileops vhci_fileops = {
    881 	.fo_read = vhci_fd_read,
    882 	.fo_write = vhci_fd_write,
    883 	.fo_ioctl = vhci_fd_ioctl,
    884 	.fo_fcntl = fnullop_fcntl,
    885 	.fo_poll = fnullop_poll,
    886 	.fo_stat = fbadop_stat,
    887 	.fo_close = vhci_fd_close,
    888 	.fo_kqfilter = fnullop_kqfilter,
    889 	.fo_restart = fnullop_restart,
    890 	.fo_mmap = NULL,
    891 };
    892 
    893 static int
    894 vhci_fd_open(dev_t dev, int flags, int type, struct lwp *l)
    895 {
    896 	vhci_fd_t *vfd;
    897 	struct file *fp;
    898 	int error, fd;
    899 
    900 	if (minor(dev) != 0)
    901 		return EXDEV;
    902 	error = fd_allocfile(&fp, &fd);
    903 	if (error)
    904 		return error;
    905 
    906 	vfd = kmem_alloc(sizeof(*vfd), KM_SLEEP);
    907 	vfd->port = 1;
    908 	vfd->softc = device_lookup_private(&vhci_cd, minor(dev));
    909 
    910 	return fd_clone(fp, fd, flags, &vhci_fileops, vfd);
    911 }
    912 
    913 static int
    914 vhci_fd_close(file_t *fp)
    915 {
    916 	struct vhci_ioc_usb_detach args;
    917 	vhci_fd_t *vfd = fp->f_data;
    918 	int ret __diagused;
    919 
    920 	KASSERT(vfd != NULL);
    921 
    922 	args.port = vfd->port;
    923 	ret = vhci_usb_detach(vfd, &args);
    924 	KASSERT(ret == 0);
    925 
    926 	kmem_free(vfd, sizeof(*vfd));
    927 	fp->f_data = NULL;
    928 
    929 	return 0;
    930 }
    931 
    932 static int
    933 vhci_fd_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    934     int flags)
    935 {
    936 	vhci_fd_t *vfd = fp->f_data;
    937 	vhci_softc_t *sc = vfd->softc;
    938 	vhci_packet_list_t *pktlist;
    939 	vhci_packet_t *pkt, *nxt;
    940 	vhci_xfer_list_t vxferlist;
    941 	vhci_xfer_t *vxfer;
    942 	vhci_port_t *port;
    943 	int error = 0;
    944 	uint8_t *buf;
    945 	size_t size;
    946 
    947 	if (uio->uio_resid == 0)
    948 		return 0;
    949 	port = &sc->sc_port[vfd->port];
    950 	pktlist = &port->pkts_device_ctrl.host_to_usb;
    951 
    952 	TAILQ_INIT(&vxferlist);
    953 
    954 	mutex_enter(&port->lock);
    955 
    956 	if (!(port->status & UPS_PORT_ENABLED)) {
    957 		error = ENOBUFS;
    958 		goto out;
    959 	}
    960 
    961 	TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
    962 		vxfer = (vhci_xfer_t *)pkt->xfer;
    963 		buf = pkt->buf + pkt->cursor;
    964 		KASSERT(pkt->size >= pkt->cursor);
    965 		size = uimin(uio->uio_resid, pkt->size - pkt->cursor);
    966 
    967 		KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
    968 
    969 		error = uiomove(buf, size, uio);
    970 		if (error) {
    971 			DPRINTF("%s: error = %d\n", __func__, error);
    972 			goto out;
    973 		}
    974 
    975 		pkt->cursor += size;
    976 
    977 		if (pkt->cursor == pkt->size) {
    978 			vhci_pkt_destroy(sc, pkt);
    979 			if (vxfer->refcnt == 0) {
    980 				TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
    981 			}
    982 		}
    983 		if (uio->uio_resid == 0) {
    984 			break;
    985 		}
    986 	}
    987 
    988 out:
    989 	mutex_exit(&port->lock);
    990 
    991 	while ((vxfer = TAILQ_FIRST(&vxferlist)) != NULL) {
    992 		struct usbd_xfer *xfer = &vxfer->xfer;
    993 		TAILQ_REMOVE(&vxferlist, vxfer, freelist);
    994 
    995 		mutex_enter(&sc->sc_lock);
    996 		xfer->ux_actlen = xfer->ux_length;
    997 		xfer->ux_status = USBD_NORMAL_COMPLETION;
    998 		usb_transfer_complete(xfer);
    999 		mutex_exit(&sc->sc_lock);
   1000 	}
   1001 
   1002 	return error;
   1003 }
   1004 
   1005 static int
   1006 vhci_fd_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
   1007     int flags)
   1008 {
   1009 	vhci_fd_t *vfd = fp->f_data;
   1010 	vhci_softc_t *sc = vfd->softc;
   1011 	vhci_packet_list_t *pktlist;
   1012 	vhci_packet_t *pkt, *nxt;
   1013 	vhci_xfer_list_t vxferlist;
   1014 	vhci_xfer_t *vxfer;
   1015 	vhci_port_t *port;
   1016 	int error = 0;
   1017 	uint8_t *buf;
   1018 	size_t size;
   1019 
   1020 	if (uio->uio_resid == 0)
   1021 		return 0;
   1022 	port = &sc->sc_port[vfd->port];
   1023 	pktlist = &port->pkts_device_ctrl.usb_to_host;
   1024 
   1025 	TAILQ_INIT(&vxferlist);
   1026 
   1027 	mutex_enter(&port->lock);
   1028 
   1029 	if (!(port->status & UPS_PORT_ENABLED)) {
   1030 		error = ENOBUFS;
   1031 		goto out;
   1032 	}
   1033 
   1034 	TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
   1035 		vxfer = (vhci_xfer_t *)pkt->xfer;
   1036 		buf = pkt->buf + pkt->cursor;
   1037 		KASSERT(pkt->size >= pkt->cursor);
   1038 		size = uimin(uio->uio_resid, pkt->size - pkt->cursor);
   1039 
   1040 		KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
   1041 
   1042 		error = uiomove(buf, size, uio);
   1043 		if (error) {
   1044 			DPRINTF("%s: error = %d\n", __func__, error);
   1045 			goto out;
   1046 		}
   1047 
   1048 		pkt->cursor += size;
   1049 
   1050 		if (pkt->cursor == pkt->size) {
   1051 			vhci_pkt_destroy(sc, pkt);
   1052 			if (vxfer->refcnt == 0) {
   1053 				TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
   1054 			}
   1055 		}
   1056 		if (uio->uio_resid == 0) {
   1057 			break;
   1058 		}
   1059 	}
   1060 
   1061 out:
   1062 	mutex_exit(&port->lock);
   1063 
   1064 	while ((vxfer = TAILQ_FIRST(&vxferlist)) != NULL) {
   1065 		struct usbd_xfer *xfer = &vxfer->xfer;
   1066 		TAILQ_REMOVE(&vxferlist, vxfer, freelist);
   1067 
   1068 		mutex_enter(&sc->sc_lock);
   1069 		xfer->ux_actlen = xfer->ux_length;
   1070 		xfer->ux_status = USBD_NORMAL_COMPLETION;
   1071 		usb_transfer_complete(xfer);
   1072 		mutex_exit(&sc->sc_lock);
   1073 	}
   1074 
   1075 	return error;
   1076 }
   1077 
   1078 static int
   1079 vhci_fd_ioctl(file_t *fp, u_long cmd, void *data)
   1080 {
   1081 	vhci_fd_t *vfd = fp->f_data;
   1082 
   1083 	KASSERT(vfd != NULL);
   1084 
   1085 	switch (cmd) {
   1086 	case VHCI_IOC_GET_INFO:
   1087 		return vhci_get_info(vfd, data);
   1088 	case VHCI_IOC_SET_PORT:
   1089 		return vhci_set_port(vfd, data);
   1090 	case VHCI_IOC_USB_ATTACH:
   1091 		return vhci_usb_attach(vfd, data);
   1092 	case VHCI_IOC_USB_DETACH:
   1093 		return vhci_usb_detach(vfd, data);
   1094 	default:
   1095 		return EINVAL;
   1096 	}
   1097 }
   1098 
   1099 /* -------------------------------------------------------------------------- */
   1100 
   1101 static int vhci_match(device_t, cfdata_t, void *);
   1102 static void vhci_attach(device_t, device_t, void *);
   1103 static int vhci_activate(device_t, enum devact);
   1104 
   1105 CFATTACH_DECL_NEW(vhci, sizeof(vhci_softc_t), vhci_match, vhci_attach,
   1106     NULL, vhci_activate);
   1107 
   1108 void
   1109 vhciattach(int nunits)
   1110 {
   1111 	static struct cfdata vhci_cfdata = {
   1112 		.cf_name = "vhci",
   1113 		.cf_atname = "vhci",
   1114 		.cf_unit = 0,
   1115 		.cf_fstate = FSTATE_STAR,
   1116 	};
   1117 	int error;
   1118 
   1119 	error = config_cfattach_attach(vhci_cd.cd_name, &vhci_ca);
   1120 	if (error) {
   1121 		aprint_error("%s: unable to register cfattach\n",
   1122 		    vhci_cd.cd_name);
   1123 		(void)config_cfdriver_detach(&vhci_cd);
   1124 		return;
   1125 	}
   1126 
   1127 	config_attach_pseudo(&vhci_cfdata);
   1128 }
   1129 
   1130 static int
   1131 vhci_activate(device_t self, enum devact act)
   1132 {
   1133 	vhci_softc_t *sc = device_private(self);
   1134 
   1135 	switch (act) {
   1136 	case DVACT_DEACTIVATE:
   1137 		sc->sc_dying = 1;
   1138 		return 0;
   1139 	default:
   1140 		return EOPNOTSUPP;
   1141 	}
   1142 }
   1143 
   1144 static int
   1145 vhci_match(device_t parent, cfdata_t match, void *aux)
   1146 {
   1147 	return 1;
   1148 }
   1149 
   1150 static void
   1151 vhci_attach(device_t parent, device_t self, void *aux)
   1152 {
   1153 	vhci_softc_t *sc = device_private(self);
   1154 	vhci_port_t *port;
   1155 	size_t i;
   1156 
   1157 	sc->sc_dev = self;
   1158 	sc->sc_bus.ub_revision = USBREV_2_0;
   1159 	sc->sc_bus.ub_usedma = false;
   1160 	sc->sc_bus.ub_methods = &vhci_bus_methods;
   1161 	sc->sc_bus.ub_pipesize = sizeof(vhci_pipe_t);
   1162 	sc->sc_bus.ub_hcpriv = sc;
   1163 	sc->sc_dying = false;
   1164 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1165 
   1166 	sc->sc_nports = VHCI_NPORTS;
   1167 	for (i = 0; i < sc->sc_nports; i++) {
   1168 		port = &sc->sc_port[i];
   1169 		mutex_init(&port->lock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1170 		TAILQ_INIT(&port->pkts_device_ctrl.usb_to_host);
   1171 		TAILQ_INIT(&port->pkts_device_ctrl.host_to_usb);
   1172 	}
   1173 
   1174 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
   1175 }
   1176