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