vhci.c revision 1.9 1 /* $NetBSD: vhci.c,v 1.9 2020/03/22 15:14:03 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.9 2020/03/22 15:14:03 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 /*
127 * There are three structures to understand: vxfers, packets, and ports.
128 *
129 * Each xfer from the point of view of the USB stack is a vxfer from the point
130 * of view of vHCI.
131 *
132 * A vxfer has a linked list containing a maximum of two packets: a request
133 * packet and possibly a data packet. Packets basically contain data exchanged
134 * between the Host and the virtual USB device. A packet is linked to both a
135 * vxfer and a port.
136 *
137 * A port is an abstraction of an actual USB port. Each virtual USB device gets
138 * connected to a port. A port has two lists:
139 * - The Usb-To-Host list, containing packets to be fetched from the USB
140 * device and provided to the host.
141 * - The Host-To-Usb list, containing packets to be sent from the Host to the
142 * USB device.
143 * Request packets are always in the H->U direction. Data packets however can
144 * be in both the H->U and U->H directions.
145 *
146 * With read() and write() operations on /dev/vhci, userland respectively
147 * "fetches" and "sends" packets from or to the virtual USB device, which
148 * respectively means reading/inserting packets in the H->U and U->H lists on
149 * the port where the virtual USB device is connected.
150 *
151 * +------------------------------------------------+
152 * | USB Stack |
153 * +---------------------^--------------------------+
154 * |
155 * +---------------------V--------------------------+
156 * | +----------------+ +-------------+ |
157 * | | Request Packet | | Data Packet | Xfer |
158 * | +-------|--------+ +----|---^----+ |
159 * +---------|------------------|---|---------------+
160 * | | |
161 * | +--------------+ |
162 * | | |
163 * +---------|---|------------------|---------------+
164 * | +---V---V---+ +---------|-+ |
165 * | | H->U List | | U->H List | vHCI Port |
166 * | +-----|-----+ +-----^-----+ |
167 * +-----------|----------------|-------------------+
168 * | |
169 * +-----------|----------------|-------------------+
170 * | +-----V-----+ +-----|-----+ |
171 * | | read() | | write() | vHCI FD |
172 * | +-----------+ +-----------+ |
173 * +------------------------------------------------+
174 */
175
176 struct vhci_xfer;
177
178 typedef struct vhci_packet {
179 TAILQ_ENTRY(vhci_packet) portlist;
180 TAILQ_ENTRY(vhci_packet) xferlist;
181 struct vhci_xfer *vxfer;
182 bool utoh;
183 uint8_t *buf;
184 size_t size;
185 size_t cursor;
186 } vhci_packet_t;
187
188 typedef TAILQ_HEAD(, vhci_packet) vhci_packet_list_t;
189
190 typedef struct {
191 kmutex_t lock;
192 int status;
193 int change;
194 struct {
195 vhci_packet_list_t usb_to_host;
196 vhci_packet_list_t host_to_usb;
197 } pkts_device_ctrl;
198 } vhci_port_t;
199
200 typedef struct {
201 struct usbd_pipe pipe;
202 } vhci_pipe_t;
203
204 typedef struct vhci_xfer {
205 /* General. */
206 struct usbd_xfer xfer;
207
208 /* Port where the xfer occurs. */
209 vhci_port_t *port;
210
211 /* Packets in the xfer. */
212 size_t npkts;
213 vhci_packet_list_t pkts;
214
215 /* Used for G/C. */
216 TAILQ_ENTRY(vhci_xfer) freelist;
217 } vhci_xfer_t;
218
219 typedef TAILQ_HEAD(, vhci_xfer) vhci_xfer_list_t;
220
221 #define VHCI_INDEX2PORT(idx) (idx)
222 #define VHCI_NPORTS 4
223
224 typedef struct {
225 device_t sc_dev;
226
227 struct usbd_bus sc_bus;
228 bool sc_dying;
229 kmutex_t sc_lock;
230
231 /*
232 * Intr Root. Used to attach the devices.
233 */
234 struct usbd_xfer *sc_intrxfer;
235
236 /*
237 * The ports. Zero is for the roothub, one and beyond for the USB
238 * devices.
239 */
240 size_t sc_nports;
241 vhci_port_t sc_port[VHCI_NPORTS];
242
243 device_t sc_child; /* /dev/usb# device */
244 } vhci_softc_t;
245
246 typedef struct {
247 u_int port;
248 vhci_softc_t *softc;
249 } vhci_fd_t;
250
251 extern struct cfdriver vhci_cd;
252
253 /* -------------------------------------------------------------------------- */
254
255 static void
256 vhci_pkt_create(vhci_port_t *port, struct usbd_xfer *xfer, bool usb_to_host)
257 {
258 vhci_xfer_t *vxfer = (vhci_xfer_t *)xfer;
259 vhci_packet_list_t *reqlist, *datlist;
260 vhci_packet_t *req, *dat = NULL;
261 size_t npkts = 0;
262
263 /* Request packet. */
264 reqlist = &port->pkts_device_ctrl.host_to_usb;
265 req = kmem_zalloc(sizeof(*req), KM_SLEEP);
266 req->vxfer = vxfer;
267 req->utoh = false;
268 req->buf = (uint8_t *)&xfer->ux_request;
269 req->size = sizeof(xfer->ux_request);
270 req->cursor = 0;
271 npkts++;
272
273 /* Data packet. */
274 if (xfer->ux_length > 0) {
275 if (usb_to_host) {
276 datlist = &port->pkts_device_ctrl.usb_to_host;
277 } else {
278 datlist = &port->pkts_device_ctrl.host_to_usb;
279 }
280 dat = kmem_zalloc(sizeof(*dat), KM_SLEEP);
281 dat->vxfer = vxfer;
282 dat->utoh = usb_to_host;
283 dat->buf = xfer->ux_buf;
284 dat->size = xfer->ux_length;
285 dat->cursor = 0;
286 npkts++;
287 }
288
289 /* Insert in the xfer. */
290 vxfer->port = port;
291 vxfer->npkts = npkts;
292 TAILQ_INIT(&vxfer->pkts);
293 TAILQ_INSERT_TAIL(&vxfer->pkts, req, xferlist);
294 if (dat != NULL)
295 TAILQ_INSERT_TAIL(&vxfer->pkts, dat, xferlist);
296
297 /* Insert in the port. */
298 KASSERT(mutex_owned(&port->lock));
299 TAILQ_INSERT_TAIL(reqlist, req, portlist);
300 if (dat != NULL)
301 TAILQ_INSERT_TAIL(datlist, dat, portlist);
302 }
303
304 static void
305 vhci_pkt_destroy(vhci_softc_t *sc, vhci_packet_t *pkt)
306 {
307 vhci_xfer_t *vxfer = pkt->vxfer;
308 vhci_port_t *port = vxfer->port;
309 vhci_packet_list_t *pktlist;
310
311 KASSERT(mutex_owned(&port->lock));
312
313 /* Remove from the port. */
314 if (pkt->utoh) {
315 pktlist = &port->pkts_device_ctrl.usb_to_host;
316 } else {
317 pktlist = &port->pkts_device_ctrl.host_to_usb;
318 }
319 TAILQ_REMOVE(pktlist, pkt, portlist);
320
321 /* Remove from the xfer. */
322 TAILQ_REMOVE(&vxfer->pkts, pkt, xferlist);
323 kmem_free(pkt, sizeof(*pkt));
324
325 /* Unref. */
326 KASSERT(vxfer->npkts > 0);
327 vxfer->npkts--;
328 if (vxfer->npkts > 0)
329 return;
330 KASSERT(TAILQ_FIRST(&vxfer->pkts) == NULL);
331 }
332
333 /* -------------------------------------------------------------------------- */
334
335 static usbd_status
336 vhci_open(struct usbd_pipe *pipe)
337 {
338 struct usbd_device *dev = pipe->up_dev;
339 struct usbd_bus *bus = dev->ud_bus;
340 usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
341 vhci_softc_t *sc = bus->ub_hcpriv;
342 uint8_t addr = dev->ud_addr;
343
344 if (sc->sc_dying)
345 return USBD_IOERROR;
346
347 DPRINTF("%s: called, type=%d\n", __func__,
348 UE_GET_XFERTYPE(ed->bmAttributes));
349
350 if (addr == bus->ub_rhaddr) {
351 switch (ed->bEndpointAddress) {
352 case USB_CONTROL_ENDPOINT:
353 DPRINTF("%s: roothub_ctrl\n", __func__);
354 pipe->up_methods = &roothub_ctrl_methods;
355 break;
356 case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
357 DPRINTF("%s: root_intr\n", __func__);
358 pipe->up_methods = &vhci_root_intr_methods;
359 break;
360 default:
361 DPRINTF("%s: inval\n", __func__);
362 return USBD_INVAL;
363 }
364 } else {
365 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
366 case UE_CONTROL:
367 pipe->up_methods = &vhci_device_ctrl_methods;
368 break;
369 case UE_BULK:
370 case UE_INTERRUPT:
371 default:
372 goto bad;
373 }
374 }
375
376 return USBD_NORMAL_COMPLETION;
377
378 bad:
379 return USBD_NOMEM;
380 }
381
382 static void
383 vhci_softintr(void *v)
384 {
385 DPRINTF("%s: called\n", __func__);
386 }
387
388 static struct usbd_xfer *
389 vhci_allocx(struct usbd_bus *bus, unsigned int nframes)
390 {
391 vhci_xfer_t *vxfer;
392
393 vxfer = kmem_zalloc(sizeof(*vxfer), KM_SLEEP);
394 #ifdef DIAGNOSTIC
395 vxfer->xfer.ux_state = XFER_BUSY;
396 #endif
397 return (struct usbd_xfer *)vxfer;
398 }
399
400 static void
401 vhci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
402 {
403 vhci_xfer_t *vxfer = (vhci_xfer_t *)xfer;
404
405 KASSERT(vxfer->npkts == 0);
406 KASSERT(TAILQ_FIRST(&vxfer->pkts) == NULL);
407
408 #ifdef DIAGNOSTIC
409 vxfer->xfer.ux_state = XFER_FREE;
410 #endif
411 kmem_free(vxfer, sizeof(*vxfer));
412 }
413
414 static void
415 vhci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
416 {
417 vhci_softc_t *sc = bus->ub_hcpriv;
418
419 *lock = &sc->sc_lock;
420 }
421
422 static int
423 vhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
424 void *buf, int buflen)
425 {
426 vhci_softc_t *sc = bus->ub_hcpriv;
427 vhci_port_t *port;
428 usb_hub_descriptor_t hubd;
429 uint16_t len, value, index;
430 int totlen = 0;
431
432 len = UGETW(req->wLength);
433 value = UGETW(req->wValue);
434 index = UGETW(req->wIndex);
435
436 #define C(x,y) ((x) | ((y) << 8))
437 switch (C(req->bRequest, req->bmRequestType)) {
438 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
439 switch (value) {
440 case C(0, UDESC_DEVICE): {
441 usb_device_descriptor_t devd;
442
443 totlen = uimin(buflen, sizeof(devd));
444 memcpy(&devd, buf, totlen);
445 USETW(devd.idVendor, 0);
446 USETW(devd.idProduct, 0);
447 memcpy(buf, &devd, totlen);
448 break;
449 }
450 #define sd ((usb_string_descriptor_t *)buf)
451 case C(1, UDESC_STRING):
452 /* Vendor */
453 totlen = usb_makestrdesc(sd, len, "NetBSD");
454 break;
455 case C(2, UDESC_STRING):
456 /* Product */
457 totlen = usb_makestrdesc(sd, len, "VHCI root hub");
458 break;
459 #undef sd
460 default:
461 /* default from usbroothub */
462 return buflen;
463 }
464 break;
465
466 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
467 switch (value) {
468 case UHF_PORT_RESET:
469 if (index < 1 || index >= sc->sc_nports) {
470 return -1;
471 }
472 port = &sc->sc_port[VHCI_INDEX2PORT(index)];
473 port->status |= UPS_C_PORT_RESET;
474 break;
475 case UHF_PORT_POWER:
476 break;
477 default:
478 return -1;
479 }
480 break;
481
482 /* Hub requests. */
483 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
484 break;
485 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
486 if (index < 1 || index >= sc->sc_nports) {
487 return -1;
488 }
489 port = &sc->sc_port[VHCI_INDEX2PORT(index)];
490 switch (value) {
491 case UHF_PORT_ENABLE:
492 port->status &= ~UPS_PORT_ENABLED;
493 break;
494 case UHF_C_PORT_ENABLE:
495 port->change |= UPS_C_PORT_ENABLED;
496 break;
497 default:
498 return -1;
499 }
500 break;
501
502 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
503 totlen = uimin(buflen, sizeof(hubd));
504 memcpy(&hubd, buf, totlen);
505 hubd.bNbrPorts = sc->sc_nports - 1;
506 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE;
507 totlen = uimin(totlen, hubd.bDescLength);
508 memcpy(buf, &hubd, totlen);
509 break;
510
511 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
512 /* XXX The other HCs do this */
513 memset(buf, 0, len);
514 totlen = len;
515 break;
516
517 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): {
518 usb_port_status_t ps;
519
520 if (index < 1 || index >= sc->sc_nports) {
521 return -1;
522 }
523 port = &sc->sc_port[VHCI_INDEX2PORT(index)];
524 USETW(ps.wPortStatus, port->status);
525 USETW(ps.wPortChange, port->change);
526 totlen = uimin(len, sizeof(ps));
527 memcpy(buf, &ps, totlen);
528 break;
529 }
530 default:
531 /* default from usbroothub */
532 return buflen;
533 }
534
535 return totlen;
536 }
537
538 /* -------------------------------------------------------------------------- */
539
540 static usbd_status
541 vhci_device_ctrl_transfer(struct usbd_xfer *xfer)
542 {
543 vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
544 usbd_status err;
545
546 DPRINTF("%s: called\n", __func__);
547
548 /* Insert last in queue. */
549 mutex_enter(&sc->sc_lock);
550 err = usb_insert_transfer(xfer);
551 mutex_exit(&sc->sc_lock);
552 if (err)
553 return err;
554
555 /* Pipe isn't running, start first */
556 return vhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
557 }
558
559 static usbd_status
560 vhci_device_ctrl_start(struct usbd_xfer *xfer)
561 {
562 usb_device_request_t *req = &xfer->ux_request;
563 struct usbd_device *dev = xfer->ux_pipe->up_dev;
564 vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
565 vhci_port_t *port;
566 bool polling = sc->sc_bus.ub_usepolling;
567 bool isread = (req->bmRequestType & UT_READ) != 0;
568 int portno, ret;
569
570 KASSERT(xfer->ux_rqflags & URQ_REQUEST);
571 KASSERT(dev->ud_myhsport != NULL);
572 portno = dev->ud_myhsport->up_portno;
573
574 DPRINTF("%s: type=0x%02x, len=%d, isread=%d, portno=%d\n",
575 __func__, req->bmRequestType, UGETW(req->wLength), isread, portno);
576
577 if (sc->sc_dying)
578 return USBD_IOERROR;
579
580 port = &sc->sc_port[portno];
581
582 if (!polling)
583 mutex_enter(&sc->sc_lock);
584
585 mutex_enter(&port->lock);
586 if (port->status & UPS_PORT_ENABLED) {
587 xfer->ux_status = USBD_IN_PROGRESS;
588 vhci_pkt_create(port, xfer, isread);
589 ret = USBD_IN_PROGRESS;
590 } else {
591 ret = USBD_IOERROR;
592 }
593 mutex_exit(&port->lock);
594
595 if (!polling)
596 mutex_exit(&sc->sc_lock);
597
598 return ret;
599 }
600
601 static void
602 vhci_device_ctrl_abort(struct usbd_xfer *xfer)
603 {
604 vhci_xfer_t *vxfer = (vhci_xfer_t *)xfer;
605 vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
606 vhci_port_t *port = vxfer->port;
607 vhci_packet_t *pkt;
608
609 DPRINTF("%s: called\n", __func__);
610
611 KASSERT(mutex_owned(&sc->sc_lock));
612
613 callout_halt(&xfer->ux_callout, &sc->sc_lock);
614
615 KASSERT(xfer->ux_status != USBD_CANCELLED);
616
617 /* If anyone else beat us, we're done. */
618 if (xfer->ux_status != USBD_IN_PROGRESS)
619 return;
620
621 mutex_enter(&port->lock);
622 while (vxfer->npkts > 0) {
623 pkt = TAILQ_FIRST(&vxfer->pkts);
624 KASSERT(pkt != NULL);
625 vhci_pkt_destroy(sc, pkt);
626 }
627 KASSERT(TAILQ_FIRST(&vxfer->pkts) == NULL);
628 mutex_exit(&port->lock);
629
630 xfer->ux_status = USBD_CANCELLED;
631 usb_transfer_complete(xfer);
632 KASSERT(mutex_owned(&sc->sc_lock));
633 }
634
635 static void
636 vhci_device_ctrl_close(struct usbd_pipe *pipe)
637 {
638 DPRINTF("%s: called\n", __func__);
639 }
640
641 static void
642 vhci_device_ctrl_cleartoggle(struct usbd_pipe *pipe)
643 {
644 DPRINTF("%s: called\n", __func__);
645 }
646
647 static void
648 vhci_device_ctrl_done(struct usbd_xfer *xfer)
649 {
650 DPRINTF("%s: called\n", __func__);
651 }
652
653 /* -------------------------------------------------------------------------- */
654
655 static usbd_status
656 vhci_root_intr_transfer(struct usbd_xfer *xfer)
657 {
658 vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
659 usbd_status err;
660
661 DPRINTF("%s: called\n", __func__);
662
663 /* Insert last in queue. */
664 mutex_enter(&sc->sc_lock);
665 err = usb_insert_transfer(xfer);
666 mutex_exit(&sc->sc_lock);
667 if (err)
668 return err;
669
670 /* Pipe isn't running, start first */
671 return vhci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
672 }
673
674 static usbd_status
675 vhci_root_intr_start(struct usbd_xfer *xfer)
676 {
677 vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
678 const bool polling = sc->sc_bus.ub_usepolling;
679
680 DPRINTF("%s: called, len=%zu\n", __func__, (size_t)xfer->ux_length);
681
682 if (sc->sc_dying)
683 return USBD_IOERROR;
684
685 if (!polling)
686 mutex_enter(&sc->sc_lock);
687 KASSERT(sc->sc_intrxfer == NULL);
688 sc->sc_intrxfer = xfer;
689 xfer->ux_status = USBD_IN_PROGRESS;
690 if (!polling)
691 mutex_exit(&sc->sc_lock);
692
693 return USBD_IN_PROGRESS;
694 }
695
696 static void
697 vhci_root_intr_abort(struct usbd_xfer *xfer)
698 {
699 vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
700
701 DPRINTF("%s: called\n", __func__);
702
703 KASSERT(mutex_owned(&sc->sc_lock));
704 KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
705
706 /* If xfer has already completed, nothing to do here. */
707 if (sc->sc_intrxfer == NULL)
708 return;
709
710 /*
711 * Otherwise, sc->sc_intrxfer had better be this transfer.
712 * Cancel it.
713 */
714 KASSERT(sc->sc_intrxfer == xfer);
715 KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
716 xfer->ux_status = USBD_CANCELLED;
717 usb_transfer_complete(xfer);
718 }
719
720 static void
721 vhci_root_intr_close(struct usbd_pipe *pipe)
722 {
723 vhci_softc_t *sc __diagused = pipe->up_dev->ud_bus->ub_hcpriv;
724
725 DPRINTF("%s: called\n", __func__);
726
727 KASSERT(mutex_owned(&sc->sc_lock));
728
729 /*
730 * Caller must guarantee the xfer has completed first, by
731 * closing the pipe only after normal completion or an abort.
732 */
733 KASSERT(sc->sc_intrxfer == NULL);
734 }
735
736 static void
737 vhci_root_intr_cleartoggle(struct usbd_pipe *pipe)
738 {
739 DPRINTF("%s: called\n", __func__);
740 }
741
742 static void
743 vhci_root_intr_done(struct usbd_xfer *xfer)
744 {
745 vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
746
747 KASSERT(mutex_owned(&sc->sc_lock));
748
749 /* Claim the xfer so it doesn't get completed again. */
750 KASSERT(sc->sc_intrxfer == xfer);
751 KASSERT(xfer->ux_status != USBD_IN_PROGRESS);
752 sc->sc_intrxfer = NULL;
753 }
754
755 /* -------------------------------------------------------------------------- */
756
757 struct vhci_ioc_get_info {
758 /* General. */
759 size_t nports;
760
761 /* Current port. */
762 u_int port;
763 int status;
764 };
765
766 struct vhci_ioc_set_port {
767 u_int port;
768 };
769
770 struct vhci_ioc_usb_attach {
771 u_int port;
772 };
773
774 struct vhci_ioc_usb_detach {
775 u_int port;
776 };
777
778 #define VHCI_IOC_GET_INFO _IOR('V', 0, struct vhci_ioc_get_info)
779 #define VHCI_IOC_SET_PORT _IOW('V', 1, struct vhci_ioc_set_port)
780 #define VHCI_IOC_USB_ATTACH _IOW('V', 10, struct vhci_ioc_usb_attach)
781 #define VHCI_IOC_USB_DETACH _IOW('V', 11, struct vhci_ioc_usb_detach)
782
783 static int
784 vhci_usb_attach(vhci_fd_t *vfd, struct vhci_ioc_usb_attach *args)
785 {
786 vhci_softc_t *sc = vfd->softc;
787 vhci_port_t *port;
788 struct usbd_xfer *xfer;
789 u_char *p;
790 int ret = 0;
791
792 if (args->port == 0 || args->port >= sc->sc_nports)
793 return EINVAL;
794 port = &sc->sc_port[args->port];
795
796 mutex_enter(&sc->sc_lock);
797
798 mutex_enter(&port->lock);
799 port->status = UPS_CURRENT_CONNECT_STATUS | UPS_PORT_ENABLED |
800 UPS_PORT_POWER;
801 port->change = UPS_C_CONNECT_STATUS | UPS_C_PORT_RESET;
802 mutex_exit(&port->lock);
803
804 xfer = sc->sc_intrxfer;
805
806 if (xfer == NULL) {
807 ret = ENOBUFS;
808 goto done;
809 }
810 KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
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
820 done:
821 mutex_exit(&sc->sc_lock);
822 return ret;
823 }
824
825 static void
826 vhci_port_flush(vhci_softc_t *sc, vhci_port_t *port)
827 {
828 vhci_packet_list_t *pktlist;
829 vhci_packet_t *pkt, *nxt;
830 vhci_xfer_list_t vxferlist;
831 vhci_xfer_t *vxfer;
832
833 KASSERT(mutex_owned(&sc->sc_lock));
834 KASSERT(mutex_owned(&port->lock));
835
836 TAILQ_INIT(&vxferlist);
837
838 /* Drop all the packets in the H->U direction. */
839 pktlist = &port->pkts_device_ctrl.host_to_usb;
840 TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
841 vxfer = pkt->vxfer;
842 KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
843 vhci_pkt_destroy(sc, pkt);
844 if (vxfer->npkts == 0)
845 TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
846 }
847 KASSERT(TAILQ_FIRST(pktlist) == NULL);
848
849 /* Drop all the packets in the U->H direction. */
850 pktlist = &port->pkts_device_ctrl.usb_to_host;
851 TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
852 vxfer = pkt->vxfer;
853 KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
854 vhci_pkt_destroy(sc, pkt);
855 if (vxfer->npkts == 0)
856 TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
857 }
858 KASSERT(TAILQ_FIRST(pktlist) == NULL);
859
860 /* Terminate all the xfers collected. */
861 while ((vxfer = TAILQ_FIRST(&vxferlist)) != NULL) {
862 struct usbd_xfer *xfer = &vxfer->xfer;
863 TAILQ_REMOVE(&vxferlist, vxfer, freelist);
864
865 xfer->ux_status = USBD_TIMEOUT;
866 usb_transfer_complete(xfer);
867 }
868 }
869
870 static int
871 vhci_usb_detach(vhci_fd_t *vfd, struct vhci_ioc_usb_detach *args)
872 {
873 vhci_softc_t *sc = vfd->softc;
874 vhci_port_t *port;
875 struct usbd_xfer *xfer;
876 u_char *p;
877
878 if (args->port == 0 || args->port >= sc->sc_nports)
879 return EINVAL;
880 port = &sc->sc_port[args->port];
881
882 mutex_enter(&sc->sc_lock);
883
884 xfer = sc->sc_intrxfer;
885 if (xfer == NULL) {
886 mutex_exit(&sc->sc_lock);
887 return ENOBUFS;
888 }
889 KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
890
891 mutex_enter(&port->lock);
892
893 port->status = 0;
894 port->change = UPS_C_CONNECT_STATUS | UPS_C_PORT_RESET;
895
896 p = xfer->ux_buf;
897 memset(p, 0, xfer->ux_length);
898 p[0] = __BIT(args->port);
899 xfer->ux_actlen = xfer->ux_length;
900 xfer->ux_status = USBD_NORMAL_COMPLETION;
901
902 usb_transfer_complete(xfer);
903 vhci_port_flush(sc, port);
904
905 mutex_exit(&port->lock);
906 mutex_exit(&sc->sc_lock);
907 return 0;
908 }
909
910 static int
911 vhci_get_info(vhci_fd_t *vfd, struct vhci_ioc_get_info *args)
912 {
913 vhci_softc_t *sc = vfd->softc;
914 vhci_port_t *port;
915
916 port = &sc->sc_port[vfd->port];
917
918 args->nports = VHCI_NPORTS;
919 args->port = vfd->port;
920 mutex_enter(&port->lock);
921 args->status = port->status;
922 mutex_exit(&port->lock);
923
924 return 0;
925 }
926
927 static int
928 vhci_set_port(vhci_fd_t *vfd, struct vhci_ioc_set_port *args)
929 {
930 vhci_softc_t *sc = vfd->softc;
931
932 if (args->port == 0 || args->port >= sc->sc_nports)
933 return EINVAL;
934
935 vfd->port = args->port;
936
937 return 0;
938 }
939
940 /* -------------------------------------------------------------------------- */
941
942 static dev_type_open(vhci_fd_open);
943
944 const struct cdevsw vhci_cdevsw = {
945 .d_open = vhci_fd_open,
946 .d_close = noclose,
947 .d_read = noread,
948 .d_write = nowrite,
949 .d_ioctl = noioctl,
950 .d_stop = nostop,
951 .d_tty = notty,
952 .d_poll = nopoll,
953 .d_mmap = nommap,
954 .d_kqfilter = nokqfilter,
955 .d_discard = nodiscard,
956 .d_flag = D_OTHER | D_MPSAFE
957 };
958
959 static int vhci_fd_ioctl(file_t *, u_long, void *);
960 static int vhci_fd_close(file_t *);
961 static int vhci_fd_read(struct file *, off_t *, struct uio *, kauth_cred_t, int);
962 static int vhci_fd_write(struct file *, off_t *, struct uio *, kauth_cred_t, int);
963
964 const struct fileops vhci_fileops = {
965 .fo_read = vhci_fd_read,
966 .fo_write = vhci_fd_write,
967 .fo_ioctl = vhci_fd_ioctl,
968 .fo_fcntl = fnullop_fcntl,
969 .fo_poll = fnullop_poll,
970 .fo_stat = fbadop_stat,
971 .fo_close = vhci_fd_close,
972 .fo_kqfilter = fnullop_kqfilter,
973 .fo_restart = fnullop_restart,
974 .fo_mmap = NULL,
975 };
976
977 static int
978 vhci_fd_open(dev_t dev, int flags, int type, struct lwp *l)
979 {
980 vhci_fd_t *vfd;
981 struct file *fp;
982 int error, fd;
983
984 if (minor(dev) != 0)
985 return EXDEV;
986 error = fd_allocfile(&fp, &fd);
987 if (error)
988 return error;
989
990 vfd = kmem_alloc(sizeof(*vfd), KM_SLEEP);
991 vfd->port = 1;
992 vfd->softc = device_lookup_private(&vhci_cd, minor(dev));
993
994 return fd_clone(fp, fd, flags, &vhci_fileops, vfd);
995 }
996
997 static int
998 vhci_fd_close(file_t *fp)
999 {
1000 struct vhci_ioc_usb_detach args;
1001 vhci_fd_t *vfd = fp->f_data;
1002 int ret __diagused;
1003
1004 KASSERT(vfd != NULL);
1005
1006 args.port = vfd->port;
1007 ret = vhci_usb_detach(vfd, &args);
1008 KASSERT(ret == 0);
1009
1010 kmem_free(vfd, sizeof(*vfd));
1011 fp->f_data = NULL;
1012
1013 return 0;
1014 }
1015
1016 static int
1017 vhci_fd_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
1018 int flags)
1019 {
1020 vhci_fd_t *vfd = fp->f_data;
1021 vhci_softc_t *sc = vfd->softc;
1022 vhci_packet_list_t *pktlist;
1023 vhci_packet_t *pkt, *nxt;
1024 vhci_xfer_list_t vxferlist;
1025 vhci_xfer_t *vxfer;
1026 vhci_port_t *port;
1027 int error = 0;
1028 uint8_t *buf;
1029 size_t size;
1030
1031 if (uio->uio_resid == 0)
1032 return 0;
1033 port = &sc->sc_port[vfd->port];
1034 pktlist = &port->pkts_device_ctrl.host_to_usb;
1035
1036 TAILQ_INIT(&vxferlist);
1037
1038 mutex_enter(&port->lock);
1039
1040 if (!(port->status & UPS_PORT_ENABLED)) {
1041 error = ENOBUFS;
1042 goto out;
1043 }
1044
1045 TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
1046 vxfer = pkt->vxfer;
1047 buf = pkt->buf + pkt->cursor;
1048 KASSERT(pkt->size >= pkt->cursor);
1049 size = uimin(uio->uio_resid, pkt->size - pkt->cursor);
1050
1051 KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
1052
1053 error = uiomove(buf, size, uio);
1054 if (error) {
1055 DPRINTF("%s: error = %d\n", __func__, error);
1056 goto out;
1057 }
1058
1059 pkt->cursor += size;
1060
1061 if (pkt->cursor == pkt->size) {
1062 vhci_pkt_destroy(sc, pkt);
1063 if (vxfer->npkts == 0) {
1064 TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
1065 }
1066 }
1067 if (uio->uio_resid == 0) {
1068 break;
1069 }
1070 }
1071
1072 out:
1073 mutex_exit(&port->lock);
1074
1075 while ((vxfer = TAILQ_FIRST(&vxferlist)) != NULL) {
1076 struct usbd_xfer *xfer = &vxfer->xfer;
1077 TAILQ_REMOVE(&vxferlist, vxfer, freelist);
1078
1079 mutex_enter(&sc->sc_lock);
1080 xfer->ux_actlen = xfer->ux_length;
1081 xfer->ux_status = USBD_NORMAL_COMPLETION;
1082 usb_transfer_complete(xfer);
1083 mutex_exit(&sc->sc_lock);
1084 }
1085
1086 return error;
1087 }
1088
1089 static int
1090 vhci_fd_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
1091 int flags)
1092 {
1093 vhci_fd_t *vfd = fp->f_data;
1094 vhci_softc_t *sc = vfd->softc;
1095 vhci_packet_list_t *pktlist;
1096 vhci_packet_t *pkt, *nxt;
1097 vhci_xfer_list_t vxferlist;
1098 vhci_xfer_t *vxfer;
1099 vhci_port_t *port;
1100 int error = 0;
1101 uint8_t *buf;
1102 size_t size;
1103
1104 if (uio->uio_resid == 0)
1105 return 0;
1106 port = &sc->sc_port[vfd->port];
1107 pktlist = &port->pkts_device_ctrl.usb_to_host;
1108
1109 TAILQ_INIT(&vxferlist);
1110
1111 mutex_enter(&port->lock);
1112
1113 if (!(port->status & UPS_PORT_ENABLED)) {
1114 error = ENOBUFS;
1115 goto out;
1116 }
1117
1118 TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
1119 vxfer = pkt->vxfer;
1120 buf = pkt->buf + pkt->cursor;
1121 KASSERT(pkt->size >= pkt->cursor);
1122 size = uimin(uio->uio_resid, pkt->size - pkt->cursor);
1123
1124 KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
1125
1126 error = uiomove(buf, size, uio);
1127 if (error) {
1128 DPRINTF("%s: error = %d\n", __func__, error);
1129 goto out;
1130 }
1131
1132 pkt->cursor += size;
1133
1134 if (pkt->cursor == pkt->size) {
1135 vhci_pkt_destroy(sc, pkt);
1136 if (vxfer->npkts == 0) {
1137 TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
1138 }
1139 }
1140 if (uio->uio_resid == 0) {
1141 break;
1142 }
1143 }
1144
1145 out:
1146 mutex_exit(&port->lock);
1147
1148 while ((vxfer = TAILQ_FIRST(&vxferlist)) != NULL) {
1149 struct usbd_xfer *xfer = &vxfer->xfer;
1150 TAILQ_REMOVE(&vxferlist, vxfer, freelist);
1151
1152 mutex_enter(&sc->sc_lock);
1153 xfer->ux_actlen = xfer->ux_length;
1154 xfer->ux_status = USBD_NORMAL_COMPLETION;
1155 usb_transfer_complete(xfer);
1156 mutex_exit(&sc->sc_lock);
1157 }
1158
1159 return error;
1160 }
1161
1162 static int
1163 vhci_fd_ioctl(file_t *fp, u_long cmd, void *data)
1164 {
1165 vhci_fd_t *vfd = fp->f_data;
1166
1167 KASSERT(vfd != NULL);
1168
1169 switch (cmd) {
1170 case VHCI_IOC_GET_INFO:
1171 return vhci_get_info(vfd, data);
1172 case VHCI_IOC_SET_PORT:
1173 return vhci_set_port(vfd, data);
1174 case VHCI_IOC_USB_ATTACH:
1175 return vhci_usb_attach(vfd, data);
1176 case VHCI_IOC_USB_DETACH:
1177 return vhci_usb_detach(vfd, data);
1178 default:
1179 return EINVAL;
1180 }
1181 }
1182
1183 /* -------------------------------------------------------------------------- */
1184
1185 static int vhci_match(device_t, cfdata_t, void *);
1186 static void vhci_attach(device_t, device_t, void *);
1187 static int vhci_activate(device_t, enum devact);
1188
1189 CFATTACH_DECL_NEW(vhci, sizeof(vhci_softc_t), vhci_match, vhci_attach,
1190 NULL, vhci_activate);
1191
1192 void
1193 vhciattach(int nunits)
1194 {
1195 static struct cfdata vhci_cfdata = {
1196 .cf_name = "vhci",
1197 .cf_atname = "vhci",
1198 .cf_unit = 0,
1199 .cf_fstate = FSTATE_STAR,
1200 };
1201 int error;
1202
1203 error = config_cfattach_attach(vhci_cd.cd_name, &vhci_ca);
1204 if (error) {
1205 aprint_error("%s: unable to register cfattach\n",
1206 vhci_cd.cd_name);
1207 (void)config_cfdriver_detach(&vhci_cd);
1208 return;
1209 }
1210
1211 config_attach_pseudo(&vhci_cfdata);
1212 }
1213
1214 static int
1215 vhci_activate(device_t self, enum devact act)
1216 {
1217 vhci_softc_t *sc = device_private(self);
1218
1219 switch (act) {
1220 case DVACT_DEACTIVATE:
1221 sc->sc_dying = 1;
1222 return 0;
1223 default:
1224 return EOPNOTSUPP;
1225 }
1226 }
1227
1228 static int
1229 vhci_match(device_t parent, cfdata_t match, void *aux)
1230 {
1231 return 1;
1232 }
1233
1234 static void
1235 vhci_attach(device_t parent, device_t self, void *aux)
1236 {
1237 vhci_softc_t *sc = device_private(self);
1238 vhci_port_t *port;
1239 size_t i;
1240
1241 sc->sc_dev = self;
1242 sc->sc_bus.ub_revision = USBREV_2_0;
1243 sc->sc_bus.ub_usedma = false;
1244 sc->sc_bus.ub_methods = &vhci_bus_methods;
1245 sc->sc_bus.ub_pipesize = sizeof(vhci_pipe_t);
1246 sc->sc_bus.ub_hcpriv = sc;
1247 sc->sc_dying = false;
1248 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
1249
1250 sc->sc_nports = VHCI_NPORTS;
1251 for (i = 0; i < sc->sc_nports; i++) {
1252 port = &sc->sc_port[i];
1253 mutex_init(&port->lock, MUTEX_DEFAULT, IPL_SOFTUSB);
1254 TAILQ_INIT(&port->pkts_device_ctrl.usb_to_host);
1255 TAILQ_INIT(&port->pkts_device_ctrl.host_to_usb);
1256 }
1257
1258 sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
1259 }
1260