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