vhci.c revision 1.8 1 /* $NetBSD: vhci.c,v 1.8 2020/03/14 02:35:34 christos 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.8 2020/03/14 02:35:34 christos 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 KASSERT(sc->sc_intrxfer == NULL);
631 sc->sc_intrxfer = xfer;
632 xfer->ux_status = USBD_IN_PROGRESS;
633 if (!polling)
634 mutex_exit(&sc->sc_lock);
635
636 return USBD_IN_PROGRESS;
637 }
638
639 static void
640 vhci_root_intr_abort(struct usbd_xfer *xfer)
641 {
642 vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
643
644 DPRINTF("%s: called\n", __func__);
645
646 KASSERT(mutex_owned(&sc->sc_lock));
647 KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
648
649 /* If xfer has already completed, nothing to do here. */
650 if (sc->sc_intrxfer == NULL)
651 return;
652
653 /*
654 * Otherwise, sc->sc_intrxfer had better be this transfer.
655 * Cancel it.
656 */
657 KASSERT(sc->sc_intrxfer == xfer);
658 KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
659 xfer->ux_status = USBD_CANCELLED;
660 usb_transfer_complete(xfer);
661 }
662
663 static void
664 vhci_root_intr_close(struct usbd_pipe *pipe)
665 {
666 vhci_softc_t *sc __diagused = pipe->up_dev->ud_bus->ub_hcpriv;
667
668 DPRINTF("%s: called\n", __func__);
669
670 KASSERT(mutex_owned(&sc->sc_lock));
671
672 /*
673 * Caller must guarantee the xfer has completed first, by
674 * closing the pipe only after normal completion or an abort.
675 */
676 KASSERT(sc->sc_intrxfer == NULL);
677 }
678
679 static void
680 vhci_root_intr_cleartoggle(struct usbd_pipe *pipe)
681 {
682 DPRINTF("%s: called\n", __func__);
683 }
684
685 static void
686 vhci_root_intr_done(struct usbd_xfer *xfer)
687 {
688 vhci_softc_t *sc = xfer->ux_bus->ub_hcpriv;
689
690 KASSERT(mutex_owned(&sc->sc_lock));
691
692 /* Claim the xfer so it doesn't get completed again. */
693 KASSERT(sc->sc_intrxfer == xfer);
694 KASSERT(xfer->ux_status != USBD_IN_PROGRESS);
695 sc->sc_intrxfer = NULL;
696 }
697
698 /* -------------------------------------------------------------------------- */
699
700 struct vhci_ioc_get_info {
701 /* General. */
702 size_t nports;
703
704 /* Current port. */
705 u_int port;
706 int status;
707 };
708
709 struct vhci_ioc_set_port {
710 u_int port;
711 };
712
713 struct vhci_ioc_usb_attach {
714 u_int port;
715 };
716
717 struct vhci_ioc_usb_detach {
718 u_int port;
719 };
720
721 #define VHCI_IOC_GET_INFO _IOR('V', 0, struct vhci_ioc_get_info)
722 #define VHCI_IOC_SET_PORT _IOW('V', 1, struct vhci_ioc_set_port)
723 #define VHCI_IOC_USB_ATTACH _IOW('V', 10, struct vhci_ioc_usb_attach)
724 #define VHCI_IOC_USB_DETACH _IOW('V', 11, struct vhci_ioc_usb_detach)
725
726 static int
727 vhci_usb_attach(vhci_fd_t *vfd, struct vhci_ioc_usb_attach *args)
728 {
729 vhci_softc_t *sc = vfd->softc;
730 vhci_port_t *port;
731 struct usbd_xfer *xfer;
732 u_char *p;
733 int ret = 0;
734
735 if (args->port == 0 || args->port >= sc->sc_nports)
736 return EINVAL;
737 port = &sc->sc_port[args->port];
738
739 mutex_enter(&sc->sc_lock);
740
741 mutex_enter(&port->lock);
742 port->status = UPS_CURRENT_CONNECT_STATUS | UPS_PORT_ENABLED |
743 UPS_PORT_POWER;
744 port->change = UPS_C_CONNECT_STATUS | UPS_C_PORT_RESET;
745 mutex_exit(&port->lock);
746
747 xfer = sc->sc_intrxfer;
748
749 if (xfer == NULL) {
750 ret = ENOBUFS;
751 goto done;
752 }
753 KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
754
755 p = xfer->ux_buf;
756 memset(p, 0, xfer->ux_length);
757 p[0] = __BIT(args->port);
758 xfer->ux_actlen = xfer->ux_length;
759 xfer->ux_status = USBD_NORMAL_COMPLETION;
760
761 usb_transfer_complete(xfer);
762
763 done:
764 mutex_exit(&sc->sc_lock);
765 return ret;
766 }
767
768 static void
769 vhci_port_flush(vhci_softc_t *sc, vhci_port_t *port)
770 {
771 vhci_packet_list_t *pktlist;
772 vhci_packet_t *pkt, *nxt;
773 vhci_xfer_list_t vxferlist;
774 vhci_xfer_t *vxfer;
775
776 KASSERT(mutex_owned(&sc->sc_lock));
777 KASSERT(mutex_owned(&port->lock));
778
779 TAILQ_INIT(&vxferlist);
780
781 pktlist = &port->pkts_device_ctrl.host_to_usb;
782 TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
783 vxfer = (vhci_xfer_t *)pkt->xfer;
784 KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
785 vhci_pkt_destroy(sc, pkt);
786 if (vxfer->refcnt == 0)
787 TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
788 }
789 KASSERT(TAILQ_FIRST(pktlist) == NULL);
790
791 pktlist = &port->pkts_device_ctrl.usb_to_host;
792 TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
793 vxfer = (vhci_xfer_t *)pkt->xfer;
794 KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
795 vhci_pkt_destroy(sc, pkt);
796 if (vxfer->refcnt == 0)
797 TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
798 }
799 KASSERT(TAILQ_FIRST(pktlist) == NULL);
800
801 while ((vxfer = TAILQ_FIRST(&vxferlist)) != NULL) {
802 struct usbd_xfer *xfer = &vxfer->xfer;
803 TAILQ_REMOVE(&vxferlist, vxfer, freelist);
804
805 xfer->ux_status = USBD_TIMEOUT;
806 usb_transfer_complete(xfer);
807 }
808 }
809
810 static int
811 vhci_usb_detach(vhci_fd_t *vfd, struct vhci_ioc_usb_detach *args)
812 {
813 vhci_softc_t *sc = vfd->softc;
814 vhci_port_t *port;
815 struct usbd_xfer *xfer;
816 u_char *p;
817
818 if (args->port == 0 || args->port >= sc->sc_nports)
819 return EINVAL;
820 port = &sc->sc_port[args->port];
821
822 mutex_enter(&sc->sc_lock);
823
824 xfer = sc->sc_intrxfer;
825 if (xfer == NULL) {
826 mutex_exit(&sc->sc_lock);
827 return ENOBUFS;
828 }
829 KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
830
831 mutex_enter(&port->lock);
832
833 port->status = 0;
834 port->change = UPS_C_CONNECT_STATUS | UPS_C_PORT_RESET;
835
836 p = xfer->ux_buf;
837 memset(p, 0, xfer->ux_length);
838 p[0] = __BIT(args->port);
839 xfer->ux_actlen = xfer->ux_length;
840 xfer->ux_status = USBD_NORMAL_COMPLETION;
841
842 usb_transfer_complete(xfer);
843 vhci_port_flush(sc, port);
844
845 mutex_exit(&port->lock);
846 mutex_exit(&sc->sc_lock);
847 return 0;
848 }
849
850 static int
851 vhci_get_info(vhci_fd_t *vfd, struct vhci_ioc_get_info *args)
852 {
853 vhci_softc_t *sc = vfd->softc;
854 vhci_port_t *port;
855
856 port = &sc->sc_port[vfd->port];
857
858 args->nports = VHCI_NPORTS;
859 args->port = vfd->port;
860 mutex_enter(&port->lock);
861 args->status = port->status;
862 mutex_exit(&port->lock);
863
864 return 0;
865 }
866
867 static int
868 vhci_set_port(vhci_fd_t *vfd, struct vhci_ioc_set_port *args)
869 {
870 vhci_softc_t *sc = vfd->softc;
871
872 if (args->port == 0 || args->port >= sc->sc_nports)
873 return EINVAL;
874
875 vfd->port = args->port;
876
877 return 0;
878 }
879
880 /* -------------------------------------------------------------------------- */
881
882 static dev_type_open(vhci_fd_open);
883
884 const struct cdevsw vhci_cdevsw = {
885 .d_open = vhci_fd_open,
886 .d_close = noclose,
887 .d_read = noread,
888 .d_write = nowrite,
889 .d_ioctl = noioctl,
890 .d_stop = nostop,
891 .d_tty = notty,
892 .d_poll = nopoll,
893 .d_mmap = nommap,
894 .d_kqfilter = nokqfilter,
895 .d_discard = nodiscard,
896 .d_flag = D_OTHER | D_MPSAFE
897 };
898
899 static int vhci_fd_ioctl(file_t *, u_long, void *);
900 static int vhci_fd_close(file_t *);
901 static int vhci_fd_read(struct file *, off_t *, struct uio *, kauth_cred_t, int);
902 static int vhci_fd_write(struct file *, off_t *, struct uio *, kauth_cred_t, int);
903
904 const struct fileops vhci_fileops = {
905 .fo_read = vhci_fd_read,
906 .fo_write = vhci_fd_write,
907 .fo_ioctl = vhci_fd_ioctl,
908 .fo_fcntl = fnullop_fcntl,
909 .fo_poll = fnullop_poll,
910 .fo_stat = fbadop_stat,
911 .fo_close = vhci_fd_close,
912 .fo_kqfilter = fnullop_kqfilter,
913 .fo_restart = fnullop_restart,
914 .fo_mmap = NULL,
915 };
916
917 static int
918 vhci_fd_open(dev_t dev, int flags, int type, struct lwp *l)
919 {
920 vhci_fd_t *vfd;
921 struct file *fp;
922 int error, fd;
923
924 if (minor(dev) != 0)
925 return EXDEV;
926 error = fd_allocfile(&fp, &fd);
927 if (error)
928 return error;
929
930 vfd = kmem_alloc(sizeof(*vfd), KM_SLEEP);
931 vfd->port = 1;
932 vfd->softc = device_lookup_private(&vhci_cd, minor(dev));
933
934 return fd_clone(fp, fd, flags, &vhci_fileops, vfd);
935 }
936
937 static int
938 vhci_fd_close(file_t *fp)
939 {
940 struct vhci_ioc_usb_detach args;
941 vhci_fd_t *vfd = fp->f_data;
942 int ret __diagused;
943
944 KASSERT(vfd != NULL);
945
946 args.port = vfd->port;
947 ret = vhci_usb_detach(vfd, &args);
948 KASSERT(ret == 0);
949
950 kmem_free(vfd, sizeof(*vfd));
951 fp->f_data = NULL;
952
953 return 0;
954 }
955
956 static int
957 vhci_fd_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
958 int flags)
959 {
960 vhci_fd_t *vfd = fp->f_data;
961 vhci_softc_t *sc = vfd->softc;
962 vhci_packet_list_t *pktlist;
963 vhci_packet_t *pkt, *nxt;
964 vhci_xfer_list_t vxferlist;
965 vhci_xfer_t *vxfer;
966 vhci_port_t *port;
967 int error = 0;
968 uint8_t *buf;
969 size_t size;
970
971 if (uio->uio_resid == 0)
972 return 0;
973 port = &sc->sc_port[vfd->port];
974 pktlist = &port->pkts_device_ctrl.host_to_usb;
975
976 TAILQ_INIT(&vxferlist);
977
978 mutex_enter(&port->lock);
979
980 if (!(port->status & UPS_PORT_ENABLED)) {
981 error = ENOBUFS;
982 goto out;
983 }
984
985 TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
986 vxfer = (vhci_xfer_t *)pkt->xfer;
987 buf = pkt->buf + pkt->cursor;
988 KASSERT(pkt->size >= pkt->cursor);
989 size = uimin(uio->uio_resid, pkt->size - pkt->cursor);
990
991 KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
992
993 error = uiomove(buf, size, uio);
994 if (error) {
995 DPRINTF("%s: error = %d\n", __func__, error);
996 goto out;
997 }
998
999 pkt->cursor += size;
1000
1001 if (pkt->cursor == pkt->size) {
1002 vhci_pkt_destroy(sc, pkt);
1003 if (vxfer->refcnt == 0) {
1004 TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
1005 }
1006 }
1007 if (uio->uio_resid == 0) {
1008 break;
1009 }
1010 }
1011
1012 out:
1013 mutex_exit(&port->lock);
1014
1015 while ((vxfer = TAILQ_FIRST(&vxferlist)) != NULL) {
1016 struct usbd_xfer *xfer = &vxfer->xfer;
1017 TAILQ_REMOVE(&vxferlist, vxfer, freelist);
1018
1019 mutex_enter(&sc->sc_lock);
1020 xfer->ux_actlen = xfer->ux_length;
1021 xfer->ux_status = USBD_NORMAL_COMPLETION;
1022 usb_transfer_complete(xfer);
1023 mutex_exit(&sc->sc_lock);
1024 }
1025
1026 return error;
1027 }
1028
1029 static int
1030 vhci_fd_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
1031 int flags)
1032 {
1033 vhci_fd_t *vfd = fp->f_data;
1034 vhci_softc_t *sc = vfd->softc;
1035 vhci_packet_list_t *pktlist;
1036 vhci_packet_t *pkt, *nxt;
1037 vhci_xfer_list_t vxferlist;
1038 vhci_xfer_t *vxfer;
1039 vhci_port_t *port;
1040 int error = 0;
1041 uint8_t *buf;
1042 size_t size;
1043
1044 if (uio->uio_resid == 0)
1045 return 0;
1046 port = &sc->sc_port[vfd->port];
1047 pktlist = &port->pkts_device_ctrl.usb_to_host;
1048
1049 TAILQ_INIT(&vxferlist);
1050
1051 mutex_enter(&port->lock);
1052
1053 if (!(port->status & UPS_PORT_ENABLED)) {
1054 error = ENOBUFS;
1055 goto out;
1056 }
1057
1058 TAILQ_FOREACH_SAFE(pkt, pktlist, portlist, nxt) {
1059 vxfer = (vhci_xfer_t *)pkt->xfer;
1060 buf = pkt->buf + pkt->cursor;
1061 KASSERT(pkt->size >= pkt->cursor);
1062 size = uimin(uio->uio_resid, pkt->size - pkt->cursor);
1063
1064 KASSERT(vxfer->xfer.ux_status == USBD_IN_PROGRESS);
1065
1066 error = uiomove(buf, size, uio);
1067 if (error) {
1068 DPRINTF("%s: error = %d\n", __func__, error);
1069 goto out;
1070 }
1071
1072 pkt->cursor += size;
1073
1074 if (pkt->cursor == pkt->size) {
1075 vhci_pkt_destroy(sc, pkt);
1076 if (vxfer->refcnt == 0) {
1077 TAILQ_INSERT_TAIL(&vxferlist, vxfer, freelist);
1078 }
1079 }
1080 if (uio->uio_resid == 0) {
1081 break;
1082 }
1083 }
1084
1085 out:
1086 mutex_exit(&port->lock);
1087
1088 while ((vxfer = TAILQ_FIRST(&vxferlist)) != NULL) {
1089 struct usbd_xfer *xfer = &vxfer->xfer;
1090 TAILQ_REMOVE(&vxferlist, vxfer, freelist);
1091
1092 mutex_enter(&sc->sc_lock);
1093 xfer->ux_actlen = xfer->ux_length;
1094 xfer->ux_status = USBD_NORMAL_COMPLETION;
1095 usb_transfer_complete(xfer);
1096 mutex_exit(&sc->sc_lock);
1097 }
1098
1099 return error;
1100 }
1101
1102 static int
1103 vhci_fd_ioctl(file_t *fp, u_long cmd, void *data)
1104 {
1105 vhci_fd_t *vfd = fp->f_data;
1106
1107 KASSERT(vfd != NULL);
1108
1109 switch (cmd) {
1110 case VHCI_IOC_GET_INFO:
1111 return vhci_get_info(vfd, data);
1112 case VHCI_IOC_SET_PORT:
1113 return vhci_set_port(vfd, data);
1114 case VHCI_IOC_USB_ATTACH:
1115 return vhci_usb_attach(vfd, data);
1116 case VHCI_IOC_USB_DETACH:
1117 return vhci_usb_detach(vfd, data);
1118 default:
1119 return EINVAL;
1120 }
1121 }
1122
1123 /* -------------------------------------------------------------------------- */
1124
1125 static int vhci_match(device_t, cfdata_t, void *);
1126 static void vhci_attach(device_t, device_t, void *);
1127 static int vhci_activate(device_t, enum devact);
1128
1129 CFATTACH_DECL_NEW(vhci, sizeof(vhci_softc_t), vhci_match, vhci_attach,
1130 NULL, vhci_activate);
1131
1132 void
1133 vhciattach(int nunits)
1134 {
1135 static struct cfdata vhci_cfdata = {
1136 .cf_name = "vhci",
1137 .cf_atname = "vhci",
1138 .cf_unit = 0,
1139 .cf_fstate = FSTATE_STAR,
1140 };
1141 int error;
1142
1143 error = config_cfattach_attach(vhci_cd.cd_name, &vhci_ca);
1144 if (error) {
1145 aprint_error("%s: unable to register cfattach\n",
1146 vhci_cd.cd_name);
1147 (void)config_cfdriver_detach(&vhci_cd);
1148 return;
1149 }
1150
1151 config_attach_pseudo(&vhci_cfdata);
1152 }
1153
1154 static int
1155 vhci_activate(device_t self, enum devact act)
1156 {
1157 vhci_softc_t *sc = device_private(self);
1158
1159 switch (act) {
1160 case DVACT_DEACTIVATE:
1161 sc->sc_dying = 1;
1162 return 0;
1163 default:
1164 return EOPNOTSUPP;
1165 }
1166 }
1167
1168 static int
1169 vhci_match(device_t parent, cfdata_t match, void *aux)
1170 {
1171 return 1;
1172 }
1173
1174 static void
1175 vhci_attach(device_t parent, device_t self, void *aux)
1176 {
1177 vhci_softc_t *sc = device_private(self);
1178 vhci_port_t *port;
1179 size_t i;
1180
1181 sc->sc_dev = self;
1182 sc->sc_bus.ub_revision = USBREV_2_0;
1183 sc->sc_bus.ub_usedma = false;
1184 sc->sc_bus.ub_methods = &vhci_bus_methods;
1185 sc->sc_bus.ub_pipesize = sizeof(vhci_pipe_t);
1186 sc->sc_bus.ub_hcpriv = sc;
1187 sc->sc_dying = false;
1188 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
1189
1190 sc->sc_nports = VHCI_NPORTS;
1191 for (i = 0; i < sc->sc_nports; i++) {
1192 port = &sc->sc_port[i];
1193 mutex_init(&port->lock, MUTEX_DEFAULT, IPL_SOFTUSB);
1194 TAILQ_INIT(&port->pkts_device_ctrl.usb_to_host);
1195 TAILQ_INIT(&port->pkts_device_ctrl.host_to_usb);
1196 }
1197
1198 sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
1199 }
1200