dwc2.c revision 1.8 1 /* $NetBSD: dwc2.c,v 1.8 2013/10/02 22:45:10 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nick Hudson
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: dwc2.c,v 1.8 2013/10/02 22:45:10 skrll Exp $");
34
35 #include "opt_usb.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kmem.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/select.h>
43 #include <sys/proc.h>
44 #include <sys/queue.h>
45 #include <sys/cpu.h>
46
47 #include <machine/endian.h>
48
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdivar.h>
52 #include <dev/usb/usb_mem.h>
53
54 #include <dev/usb/usbroothub_subr.h>
55
56 #include <dwc2/dwc2.h>
57 #include <dwc2/dwc2var.h>
58
59 #include "dwc2_core.h"
60 #include "dwc2_hcd.h"
61
62 #ifdef DWC2_COUNTERS
63 #define DWC2_EVCNT_ADD(a,b) ((void)((a).ev_count += (b)))
64 #else
65 #define DWC2_EVCNT_ADD(a,b) do { } while (/*CONSTCOND*/0)
66 #endif
67 #define DWC2_EVCNT_INCR(a) DWC2_EVCNT_ADD((a), 1)
68
69 #define DWC2_DEBUG
70 #ifdef DWC2_DEBUG
71 #define DPRINTFN(n,fmt,...) do { \
72 if (dwc2debug >= (n)) { \
73 printf("%s: " fmt, \
74 __FUNCTION__,## __VA_ARGS__); \
75 } \
76 } while (0)
77 #define DPRINTF(...) DPRINTFN(1, __VA_ARGS__)
78 int dwc2debug = 0;
79 #else
80 #define DPRINTF(...) do { } while (0)
81 #define DPRINTFN(...) do { } while (0)
82 #endif
83
84 Static usbd_status dwc2_open(usbd_pipe_handle);
85 Static void dwc2_poll(struct usbd_bus *);
86 Static void dwc2_softintr(void *);
87 Static void dwc2_waitintr(struct dwc2_softc *, usbd_xfer_handle);
88
89 Static usbd_status dwc2_allocm(struct usbd_bus *, usb_dma_t *, uint32_t);
90 Static void dwc2_freem(struct usbd_bus *, usb_dma_t *);
91
92 Static usbd_xfer_handle dwc2_allocx(struct usbd_bus *);
93 Static void dwc2_freex(struct usbd_bus *, usbd_xfer_handle);
94 Static void dwc2_get_lock(struct usbd_bus *, kmutex_t **);
95
96 Static usbd_status dwc2_root_ctrl_transfer(usbd_xfer_handle);
97 Static usbd_status dwc2_root_ctrl_start(usbd_xfer_handle);
98 Static void dwc2_root_ctrl_abort(usbd_xfer_handle);
99 Static void dwc2_root_ctrl_close(usbd_pipe_handle);
100 Static void dwc2_root_ctrl_done(usbd_xfer_handle);
101
102 Static usbd_status dwc2_root_intr_transfer(usbd_xfer_handle);
103 Static usbd_status dwc2_root_intr_start(usbd_xfer_handle);
104 Static void dwc2_root_intr_abort(usbd_xfer_handle);
105 Static void dwc2_root_intr_close(usbd_pipe_handle);
106 Static void dwc2_root_intr_done(usbd_xfer_handle);
107
108 Static usbd_status dwc2_device_ctrl_transfer(usbd_xfer_handle);
109 Static usbd_status dwc2_device_ctrl_start(usbd_xfer_handle);
110 Static void dwc2_device_ctrl_abort(usbd_xfer_handle);
111 Static void dwc2_device_ctrl_close(usbd_pipe_handle);
112 Static void dwc2_device_ctrl_done(usbd_xfer_handle);
113
114 Static usbd_status dwc2_device_bulk_transfer(usbd_xfer_handle);
115 Static usbd_status dwc2_device_bulk_start(usbd_xfer_handle);
116 Static void dwc2_device_bulk_abort(usbd_xfer_handle);
117 Static void dwc2_device_bulk_close(usbd_pipe_handle);
118 Static void dwc2_device_bulk_done(usbd_xfer_handle);
119
120 Static usbd_status dwc2_device_intr_transfer(usbd_xfer_handle);
121 Static usbd_status dwc2_device_intr_start(usbd_xfer_handle);
122 Static void dwc2_device_intr_abort(usbd_xfer_handle);
123 Static void dwc2_device_intr_close(usbd_pipe_handle);
124 Static void dwc2_device_intr_done(usbd_xfer_handle);
125
126 Static usbd_status dwc2_device_isoc_transfer(usbd_xfer_handle);
127 Static usbd_status dwc2_device_isoc_start(usbd_xfer_handle);
128 Static void dwc2_device_isoc_abort(usbd_xfer_handle);
129 Static void dwc2_device_isoc_close(usbd_pipe_handle);
130 Static void dwc2_device_isoc_done(usbd_xfer_handle);
131
132 Static usbd_status dwc2_device_start(usbd_xfer_handle);
133 Static void dwc2_device_close(usbd_pipe_handle);
134
135 Static void dwc2_close_pipe(usbd_pipe_handle);
136 Static void dwc2_abort_xfer(usbd_xfer_handle, usbd_status);
137
138 Static void dwc2_device_clear_toggle(usbd_pipe_handle);
139 Static void dwc2_noop(usbd_pipe_handle pipe);
140
141 Static int dwc2_interrupt(struct dwc2_softc *);
142 Static void dwc2_rhc(void *);
143
144 Static void dwc2_timeout(void *);
145 Static void dwc2_timeout_task(void *);
146
147
148
149 static inline void
150 dwc2_allocate_bus_bandwidth(struct dwc2_hsotg *hsotg, u16 bw,
151 usbd_xfer_handle xfer)
152 {
153 }
154
155 static inline void
156 dwc2_free_bus_bandwidth(struct dwc2_hsotg *hsotg, u16 bw,
157 usbd_xfer_handle xfer)
158 {
159 }
160
161 #define DWC2_INTR_ENDPT 1
162
163 Static const struct usbd_bus_methods dwc2_bus_methods = {
164 .open_pipe = dwc2_open,
165 .soft_intr = dwc2_softintr,
166 .do_poll = dwc2_poll,
167 .allocm = dwc2_allocm,
168 .freem = dwc2_freem,
169 .allocx = dwc2_allocx,
170 .freex = dwc2_freex,
171 .get_lock = dwc2_get_lock,
172 };
173
174 Static const struct usbd_pipe_methods dwc2_root_ctrl_methods = {
175 .transfer = dwc2_root_ctrl_transfer,
176 .start = dwc2_root_ctrl_start,
177 .abort = dwc2_root_ctrl_abort,
178 .close = dwc2_root_ctrl_close,
179 .cleartoggle = dwc2_noop,
180 .done = dwc2_root_ctrl_done,
181 };
182
183 Static const struct usbd_pipe_methods dwc2_root_intr_methods = {
184 .transfer = dwc2_root_intr_transfer,
185 .start = dwc2_root_intr_start,
186 .abort = dwc2_root_intr_abort,
187 .close = dwc2_root_intr_close,
188 .cleartoggle = dwc2_noop,
189 .done = dwc2_root_intr_done,
190 };
191
192 Static const struct usbd_pipe_methods dwc2_device_ctrl_methods = {
193 .transfer = dwc2_device_ctrl_transfer,
194 .start = dwc2_device_ctrl_start,
195 .abort = dwc2_device_ctrl_abort,
196 .close = dwc2_device_ctrl_close,
197 .cleartoggle = dwc2_noop,
198 .done = dwc2_device_ctrl_done,
199 };
200
201 Static const struct usbd_pipe_methods dwc2_device_intr_methods = {
202 .transfer = dwc2_device_intr_transfer,
203 .start = dwc2_device_intr_start,
204 .abort = dwc2_device_intr_abort,
205 .close = dwc2_device_intr_close,
206 .cleartoggle = dwc2_device_clear_toggle,
207 .done = dwc2_device_intr_done,
208 };
209
210 Static const struct usbd_pipe_methods dwc2_device_bulk_methods = {
211 .transfer = dwc2_device_bulk_transfer,
212 .start = dwc2_device_bulk_start,
213 .abort = dwc2_device_bulk_abort,
214 .close = dwc2_device_bulk_close,
215 .cleartoggle = dwc2_device_clear_toggle,
216 .done = dwc2_device_bulk_done,
217 };
218
219 Static const struct usbd_pipe_methods dwc2_device_isoc_methods = {
220 .transfer = dwc2_device_isoc_transfer,
221 .start = dwc2_device_isoc_start,
222 .abort = dwc2_device_isoc_abort,
223 .close = dwc2_device_isoc_close,
224 .cleartoggle = dwc2_noop,
225 .done = dwc2_device_isoc_done,
226 };
227
228 Static usbd_status
229 dwc2_allocm(struct usbd_bus *bus, usb_dma_t *dma, uint32_t size)
230 {
231 struct dwc2_softc *sc = DWC2_BUS2SC(bus);
232 usbd_status status;
233
234 DPRINTFN(10, "\n");
235
236 status = usb_allocmem(&sc->sc_bus, size, 0, dma);
237 if (status == USBD_NOMEM)
238 status = usb_reserve_allocm(&sc->sc_dma_reserve, dma, size);
239 return status;
240 }
241
242 Static void
243 dwc2_freem(struct usbd_bus *bus, usb_dma_t *dma)
244 {
245 struct dwc2_softc *sc = DWC2_BUS2SC(bus);
246
247 DPRINTFN(10, "\n");
248
249 if (dma->block->flags & USB_DMA_RESERVE) {
250 usb_reserve_freem(&sc->sc_dma_reserve, dma);
251 return;
252 }
253 usb_freemem(&sc->sc_bus, dma);
254 }
255
256 usbd_xfer_handle
257 dwc2_allocx(struct usbd_bus *bus)
258 {
259 struct dwc2_softc *sc = DWC2_BUS2SC(bus);
260 struct dwc2_xfer *dxfer;
261
262 DPRINTFN(10, "\n");
263
264 DWC2_EVCNT_INCR(sc->sc_ev_xferpoolget);
265 dxfer = pool_cache_get(sc->sc_xferpool, PR_NOWAIT);
266 if (dxfer != NULL) {
267 memset(dxfer, 0, sizeof(*dxfer));
268
269 dxfer->urb = dwc2_hcd_urb_alloc(sc->sc_hsotg,
270 DWC2_MAXISOCPACKETS, GFP_KERNEL);
271
272 #ifdef DIAGNOSTIC
273 dxfer->xfer.busy_free = XFER_BUSY;
274 #endif
275 }
276 return (usbd_xfer_handle)dxfer;
277 }
278
279 void
280 dwc2_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
281 {
282 struct dwc2_xfer *dxfer = DWC2_XFER2DXFER(xfer);
283 struct dwc2_softc *sc = DWC2_BUS2SC(bus);
284
285 DPRINTFN(10, "\n");
286
287 #ifdef DIAGNOSTIC
288 if (xfer->busy_free != XFER_BUSY) {
289 DPRINTF("xfer=%p not busy, 0x%08x\n", xfer, xfer->busy_free);
290 }
291 xfer->busy_free = XFER_FREE;
292 #endif
293 DWC2_EVCNT_INCR(sc->sc_ev_xferpoolput);
294 dwc2_hcd_urb_free(sc->sc_hsotg, dxfer->urb, DWC2_MAXISOCPACKETS);
295 pool_cache_put(sc->sc_xferpool, xfer);
296 }
297
298 Static void
299 dwc2_get_lock(struct usbd_bus *bus, kmutex_t **lock)
300 {
301 struct dwc2_softc *sc = DWC2_BUS2SC(bus);
302
303 *lock = &sc->sc_lock;
304 }
305
306 Static void
307 dwc2_rhc(void *addr)
308 {
309 struct dwc2_softc *sc = addr;
310 usbd_xfer_handle xfer;
311 usbd_pipe_handle pipe;
312 u_char *p;
313
314 DPRINTF("\n");
315 mutex_enter(&sc->sc_lock);
316 xfer = sc->sc_intrxfer;
317
318 if (xfer == NULL) {
319 /* Just ignore the change. */
320 mutex_exit(&sc->sc_lock);
321 return;
322
323 }
324 /* set port bit */
325 pipe = xfer->pipe;
326
327 p = KERNADDR(&xfer->dmabuf, 0);
328
329 p[0] = 0x02; /* we only have one port (1 << 1) */
330
331 xfer->actlen = xfer->length;
332 xfer->status = USBD_NORMAL_COMPLETION;
333
334 usb_transfer_complete(xfer);
335 mutex_exit(&sc->sc_lock);
336 }
337
338 Static void
339 dwc2_softintr(void *v)
340 {
341 struct usbd_bus *bus = v;
342 struct dwc2_softc *sc = DWC2_BUS2SC(bus);
343 struct dwc2_hsotg *hsotg = sc->sc_hsotg;
344 struct dwc2_xfer *dxfer;
345
346 KASSERT(sc->sc_bus.use_polling || mutex_owned(&sc->sc_lock));
347
348 mutex_spin_enter(&hsotg->lock);
349 while ((dxfer = TAILQ_FIRST(&sc->sc_complete)) != NULL) {
350 /*
351 * dwc2_abort_xfer will remove this transfer from the
352 * sc_complete queue
353 */
354 /*XXXNH not tested */
355 if (dxfer->xfer.hcflags & UXFER_ABORTING) {
356 cv_broadcast(&dxfer->xfer.hccv);
357 continue;
358 }
359
360 TAILQ_REMOVE(&sc->sc_complete, dxfer, xnext);
361 /* XXXNH Already done - can I assert this? */
362 callout_stop(&dxfer->xfer.timeout_handle);
363
364 mutex_spin_exit(&hsotg->lock);
365 usb_transfer_complete(&dxfer->xfer);
366 mutex_spin_enter(&hsotg->lock);
367 }
368 mutex_spin_exit(&hsotg->lock);
369 }
370
371 Static void
372 dwc2_waitintr(struct dwc2_softc *sc, usbd_xfer_handle xfer)
373 {
374 struct dwc2_hsotg *hsotg = sc->sc_hsotg;
375 uint32_t intrs;
376 int timo;
377
378 xfer->status = USBD_IN_PROGRESS;
379 for (timo = xfer->timeout; timo >= 0; timo--) {
380 usb_delay_ms(&sc->sc_bus, 1);
381 if (sc->sc_dying)
382 break;
383 intrs = dwc2_read_core_intr(hsotg);
384
385 DPRINTFN(15, "0x%08x\n", intrs);
386
387 if (intrs) {
388 mutex_spin_enter(&hsotg->lock);
389 dwc2_interrupt(sc);
390 mutex_spin_exit(&hsotg->lock);
391 if (xfer->status != USBD_IN_PROGRESS)
392 return;
393 }
394 }
395
396 /* Timeout */
397 DPRINTF("timeout\n");
398
399 mutex_enter(&sc->sc_lock);
400 xfer->status = USBD_TIMEOUT;
401 usb_transfer_complete(xfer);
402 mutex_exit(&sc->sc_lock);
403 }
404
405 Static void
406 dwc2_timeout(void *addr)
407 {
408 usbd_xfer_handle xfer = addr;
409 struct dwc2_xfer *dxfer = DWC2_XFER2DXFER(xfer);
410 // struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
411 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
412
413 DPRINTF("dxfer=%p\n", dxfer);
414
415 if (sc->sc_dying) {
416 mutex_enter(&sc->sc_lock);
417 dwc2_abort_xfer(&dxfer->xfer, USBD_TIMEOUT);
418 mutex_exit(&sc->sc_lock);
419 return;
420 }
421
422 /* Execute the abort in a process context. */
423 usb_init_task(&dxfer->abort_task, dwc2_timeout_task, addr,
424 USB_TASKQ_MPSAFE);
425 usb_add_task(dxfer->xfer.pipe->device, &dxfer->abort_task,
426 USB_TASKQ_HC);
427 }
428
429 Static void
430 dwc2_timeout_task(void *addr)
431 {
432 usbd_xfer_handle xfer = addr;
433 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
434
435 DPRINTF("xfer=%p\n", xfer);
436
437 mutex_enter(&sc->sc_lock);
438 dwc2_abort_xfer(xfer, USBD_TIMEOUT);
439 mutex_exit(&sc->sc_lock);
440 }
441
442 usbd_status
443 dwc2_open(usbd_pipe_handle pipe)
444 {
445 usbd_device_handle dev = pipe->device;
446 struct dwc2_softc *sc = DWC2_PIPE2SC(pipe);
447 struct dwc2_pipe *dpipe = DWC2_PIPE2DPIPE(pipe);
448 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
449 uint8_t addr = dev->address;
450 uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
451 usbd_status err;
452
453 DPRINTF("pipe %p addr %d xfertype %d dir %s\n", pipe, addr, xfertype,
454 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? "in" : "out");
455
456 if (sc->sc_dying) {
457 return USBD_IOERROR;
458 }
459
460 if (addr == sc->sc_addr) {
461 switch (ed->bEndpointAddress) {
462 case USB_CONTROL_ENDPOINT:
463 pipe->methods = &dwc2_root_ctrl_methods;
464 break;
465 case UE_DIR_IN | DWC2_INTR_ENDPT:
466 pipe->methods = &dwc2_root_intr_methods;
467 break;
468 default:
469 DPRINTF("bad bEndpointAddress 0x%02x\n",
470 ed->bEndpointAddress);
471 return USBD_INVAL;
472 }
473 DPRINTF("root hub pipe open\n");
474 return USBD_NORMAL_COMPLETION;
475 }
476
477 switch (xfertype) {
478 case UE_CONTROL:
479 pipe->methods = &dwc2_device_ctrl_methods;
480 err = usb_allocmem(&sc->sc_bus, sizeof(usb_device_request_t),
481 0, &dpipe->req_dma);
482 if (err)
483 return err;
484 break;
485 case UE_INTERRUPT:
486 pipe->methods = &dwc2_device_intr_methods;
487 break;
488 case UE_ISOCHRONOUS:
489 pipe->methods = &dwc2_device_isoc_methods;
490 break;
491 case UE_BULK:
492 pipe->methods = &dwc2_device_bulk_methods;
493 break;
494 default:
495 DPRINTF("bad xfer type %d\n", xfertype);
496 return USBD_INVAL;
497 }
498
499 dpipe->priv = NULL; /* QH */
500 dpipe->xfer = NULL; /* dwc2_urb */
501
502 return USBD_NORMAL_COMPLETION;
503 }
504
505 Static void
506 dwc2_poll(struct usbd_bus *bus)
507 {
508 struct dwc2_softc *sc = DWC2_BUS2SC(bus);
509 struct dwc2_hsotg *hsotg = sc->sc_hsotg;
510
511 mutex_spin_enter(&hsotg->lock);
512 dwc2_interrupt(sc);
513 mutex_spin_exit(&hsotg->lock);
514 }
515
516 /*
517 * Close a reqular pipe.
518 * Assumes that there are no pending transactions.
519 */
520 Static void
521 dwc2_close_pipe(usbd_pipe_handle pipe)
522 {
523 struct dwc2_pipe *dpipe = (struct dwc2_pipe *)pipe;
524 struct dwc2_softc *sc = pipe->device->bus->hci_private;
525
526 dpipe = dpipe;
527
528 KASSERT(mutex_owned(&sc->sc_lock));
529 }
530
531 /*
532 * Abort a device request.
533 */
534 Static void
535 dwc2_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
536 {
537 struct dwc2_xfer *dxfer = DWC2_XFER2DXFER(xfer);
538 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
539 struct dwc2_hsotg *hsotg = sc->sc_hsotg;
540 struct dwc2_xfer *d, *tmp;
541 bool wake;
542 int err;
543
544 DPRINTF("xfer=%p\n", xfer);
545
546 KASSERT(mutex_owned(&sc->sc_lock));
547 KASSERT(!cpu_intr_p() && !cpu_softintr_p());
548
549 if (sc->sc_dying) {
550 xfer->status = status;
551 callout_stop(&xfer->timeout_handle);
552 usb_transfer_complete(xfer);
553 return;
554 }
555
556 /*
557 * If an abort is already in progress then just wait for it to
558 * complete and return.
559 */
560 if (xfer->hcflags & UXFER_ABORTING) {
561 xfer->status = status;
562 xfer->hcflags |= UXFER_ABORTWAIT;
563 while (xfer->hcflags & UXFER_ABORTING)
564 cv_wait(&xfer->hccv, &sc->sc_lock);
565 return;
566 }
567
568 /*
569 * Step 1: Make the stack ignore it and stop the callout.
570 */
571 mutex_spin_enter(&hsotg->lock);
572 xfer->hcflags |= UXFER_ABORTING;
573
574 xfer->status = status; /* make software ignore it */
575 callout_stop(&xfer->timeout_handle);
576
577 /* XXXNH suboptimal */
578 TAILQ_FOREACH_SAFE(d, &sc->sc_complete, xnext, tmp) {
579 if (d == dxfer) {
580 TAILQ_REMOVE(&sc->sc_complete, dxfer, xnext);
581 }
582 }
583
584 err = dwc2_hcd_urb_dequeue(hsotg, dxfer->urb);
585 if (err) {
586 DPRINTF("dwc2_hcd_urb_dequeue failed\n");
587 }
588
589 mutex_spin_exit(&hsotg->lock);
590
591 /*
592 * Step 2: Execute callback.
593 */
594 wake = xfer->hcflags & UXFER_ABORTWAIT;
595 xfer->hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT);
596
597 usb_transfer_complete(xfer);
598 if (wake) {
599 cv_broadcast(&xfer->hccv);
600 }
601 }
602
603 Static void
604 dwc2_noop(usbd_pipe_handle pipe)
605 {
606
607 }
608
609 Static void
610 dwc2_device_clear_toggle(usbd_pipe_handle pipe)
611 {
612
613 DPRINTF("toggle %d -> 0", pipe->endpoint->datatoggle);
614 }
615
616 /***********************************************************************/
617
618 /*
619 * Data structures and routines to emulate the root hub.
620 */
621
622 Static const usb_device_descriptor_t dwc2_devd = {
623 .bLength = sizeof(usb_device_descriptor_t),
624 .bDescriptorType = UDESC_DEVICE,
625 .bcdUSB = {0x00, 0x02},
626 .bDeviceClass = UDCLASS_HUB,
627 .bDeviceSubClass = UDSUBCLASS_HUB,
628 .bDeviceProtocol = UDPROTO_HSHUBSTT,
629 .bMaxPacketSize = 64,
630 .bcdDevice = {0x00, 0x01},
631 .iManufacturer = 1,
632 .iProduct = 2,
633 .bNumConfigurations = 1,
634 };
635
636 struct dwc2_config_desc {
637 usb_config_descriptor_t confd;
638 usb_interface_descriptor_t ifcd;
639 usb_endpoint_descriptor_t endpd;
640 } __packed;
641
642 Static const struct dwc2_config_desc dwc2_confd = {
643 .confd = {
644 .bLength = USB_CONFIG_DESCRIPTOR_SIZE,
645 .bDescriptorType = UDESC_CONFIG,
646 .wTotalLength[0] = sizeof(dwc2_confd),
647 .bNumInterface = 1,
648 .bConfigurationValue = 1,
649 .iConfiguration = 0,
650 .bmAttributes = UC_SELF_POWERED,
651 .bMaxPower = 0,
652 },
653 .ifcd = {
654 .bLength = USB_INTERFACE_DESCRIPTOR_SIZE,
655 .bDescriptorType = UDESC_INTERFACE,
656 .bInterfaceNumber = 0,
657 .bAlternateSetting = 0,
658 .bNumEndpoints = 1,
659 .bInterfaceClass = UICLASS_HUB,
660 .bInterfaceSubClass = UISUBCLASS_HUB,
661 .bInterfaceProtocol = UIPROTO_HSHUBSTT,
662 .iInterface = 0
663 },
664 .endpd = {
665 .bLength = USB_ENDPOINT_DESCRIPTOR_SIZE,
666 .bDescriptorType = UDESC_ENDPOINT,
667 .bEndpointAddress = UE_DIR_IN | DWC2_INTR_ENDPT,
668 .bmAttributes = UE_INTERRUPT,
669 .wMaxPacketSize = {8, 0}, /* max packet */
670 .bInterval = 255,
671 },
672 };
673
674 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
675 Static const usb_hub_descriptor_t dwc2_hubd = {
676 .bDescLength = USB_HUB_DESCRIPTOR_SIZE,
677 .bDescriptorType = UDESC_HUB,
678 .bNbrPorts = 1,
679 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
680 .bPwrOn2PwrGood = 50,
681 .bHubContrCurrent = 0,
682 .DeviceRemovable = {0}, /* port is removable */
683 };
684
685 Static usbd_status
686 dwc2_root_ctrl_transfer(usbd_xfer_handle xfer)
687 {
688 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
689 usbd_status err;
690
691 mutex_enter(&sc->sc_lock);
692 err = usb_insert_transfer(xfer);
693 mutex_exit(&sc->sc_lock);
694 if (err)
695 return err;
696
697 return dwc2_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
698 }
699
700 Static usbd_status
701 dwc2_root_ctrl_start(usbd_xfer_handle xfer)
702 {
703 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
704 usb_device_request_t *req;
705 uint8_t *buf;
706 uint16_t len;
707 int value, index, l, totlen;
708 usbd_status err = USBD_IOERROR;
709
710 if (sc->sc_dying)
711 return USBD_IOERROR;
712
713 req = &xfer->request;
714
715 DPRINTFN(4, "type=0x%02x request=%02x\n",
716 req->bmRequestType, req->bRequest);
717
718 len = UGETW(req->wLength);
719 value = UGETW(req->wValue);
720 index = UGETW(req->wIndex);
721
722 buf = len ? KERNADDR(&xfer->dmabuf, 0) : NULL;
723
724 totlen = 0;
725
726 #define C(x,y) ((x) | ((y) << 8))
727 switch (C(req->bRequest, req->bmRequestType)) {
728 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
729 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
730 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
731 /*
732 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
733 * for the integrated root hub.
734 */
735 break;
736 case C(UR_GET_CONFIG, UT_READ_DEVICE):
737 if (len > 0) {
738 *buf = sc->sc_conf;
739 totlen = 1;
740 }
741 break;
742 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
743 DPRINTFN(8, "wValue=0x%04x\n", value);
744
745 if (len == 0)
746 break;
747 switch (value) {
748 case C(0, UDESC_DEVICE):
749 l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
750 // USETW(dwc2_devd.idVendor, sc->sc_id_vendor);
751 memcpy(buf, &dwc2_devd, l);
752 buf += l;
753 len -= l;
754 totlen += l;
755
756 break;
757 case C(0, UDESC_CONFIG):
758 l = min(len, sizeof(dwc2_confd));
759 memcpy(buf, &dwc2_confd, l);
760 buf += l;
761 len -= l;
762 totlen += l;
763
764 break;
765 #define sd ((usb_string_descriptor_t *)buf)
766 case C(0, UDESC_STRING):
767 totlen = usb_makelangtbl(sd, len);
768 break;
769 case C(1, UDESC_STRING):
770 totlen = usb_makestrdesc(sd, len, sc->sc_vendor);
771 break;
772 case C(2, UDESC_STRING):
773 totlen = usb_makestrdesc(sd, len, "DWC2 root hub");
774 break;
775 #undef sd
776 default:
777 goto fail;
778 }
779 break;
780 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
781 if (len > 0) {
782 *buf = 0;
783 totlen = 1;
784 }
785 break;
786 case C(UR_GET_STATUS, UT_READ_DEVICE):
787 if (len > 1) {
788 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
789 totlen = 2;
790 }
791 break;
792 case C(UR_GET_STATUS, UT_READ_INTERFACE):
793 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
794 if (len > 1) {
795 USETW(((usb_status_t *)buf)->wStatus, 0);
796 totlen = 2;
797 }
798 break;
799 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
800 DPRINTF("UR_SET_ADDRESS, UT_WRITE_DEVICE: addr %d\n",
801 value);
802 if (value >= USB_MAX_DEVICES)
803 goto fail;
804
805 sc->sc_addr = value;
806 break;
807 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
808 if (value != 0 && value != 1)
809 goto fail;
810
811 sc->sc_conf = value;
812 break;
813 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
814 break;
815 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
816 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
817 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
818 err = USBD_IOERROR;
819 goto fail;
820 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
821 break;
822 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
823 break;
824 default:
825 /* Hub requests - XXXNH len check? */
826 err = dwc2_hcd_hub_control(sc->sc_hsotg,
827 C(req->bRequest, req->bmRequestType), value, index,
828 buf, len);
829 if (err) {
830 err = USBD_IOERROR;
831 goto fail;
832 }
833 totlen = len;
834 }
835 xfer->actlen = totlen;
836 err = USBD_NORMAL_COMPLETION;
837
838 fail:
839 mutex_enter(&sc->sc_lock);
840 xfer->status = err;
841 usb_transfer_complete(xfer);
842 mutex_exit(&sc->sc_lock);
843
844 return USBD_IN_PROGRESS;
845 }
846
847 Static void
848 dwc2_root_ctrl_abort(usbd_xfer_handle xfer)
849 {
850 DPRINTFN(10, "\n");
851
852 /* Nothing to do, all transfers are synchronous. */
853 }
854
855 Static void
856 dwc2_root_ctrl_close(usbd_pipe_handle pipe)
857 {
858 DPRINTFN(10, "\n");
859
860 /* Nothing to do. */
861 }
862
863 Static void
864 dwc2_root_ctrl_done(usbd_xfer_handle xfer)
865 {
866 DPRINTFN(10, "\n");
867
868 /* Nothing to do. */
869 }
870
871 Static usbd_status
872 dwc2_root_intr_transfer(usbd_xfer_handle xfer)
873 {
874 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
875 usbd_status err;
876
877 DPRINTF("\n");
878
879 /* Insert last in queue. */
880 mutex_enter(&sc->sc_lock);
881 err = usb_insert_transfer(xfer);
882 mutex_exit(&sc->sc_lock);
883 if (err)
884 return err;
885
886 /* Pipe isn't running, start first */
887 return dwc2_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
888 }
889
890 Static usbd_status
891 dwc2_root_intr_start(usbd_xfer_handle xfer)
892 {
893 struct dwc2_softc *sc = DWC2_PIPE2SC(xfer);
894
895 DPRINTF("\n");
896
897 if (sc->sc_dying)
898 return USBD_IOERROR;
899
900 mutex_enter(&sc->sc_lock);
901 KASSERT(sc->sc_intrxfer == NULL);
902 sc->sc_intrxfer = xfer;
903 mutex_exit(&sc->sc_lock);
904
905 return USBD_IN_PROGRESS;
906 }
907
908 /* Abort a root interrupt request. */
909 Static void
910 dwc2_root_intr_abort(usbd_xfer_handle xfer)
911 {
912 #ifdef DIAGNOSTIC
913 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
914 #endif
915 DPRINTF("xfer=%p\n", xfer);
916
917 KASSERT(mutex_owned(&sc->sc_lock));
918
919 if (xfer->pipe->intrxfer == xfer) {
920 DPRINTF("remove\n");
921 xfer->pipe->intrxfer = NULL;
922 }
923 xfer->status = USBD_CANCELLED;
924 usb_transfer_complete(xfer);
925 }
926
927 Static void
928 dwc2_root_intr_close(usbd_pipe_handle pipe)
929 {
930 struct dwc2_softc *sc = DWC2_PIPE2SC(pipe);
931
932 DPRINTF("\n");
933
934 KASSERT(mutex_owned(&sc->sc_lock));
935
936 sc->sc_intrxfer = NULL;
937 }
938
939 Static void
940 dwc2_root_intr_done(usbd_xfer_handle xfer)
941 {
942
943 DPRINTF("\n");
944 }
945
946 /***********************************************************************/
947
948 Static usbd_status
949 dwc2_device_ctrl_transfer(usbd_xfer_handle xfer)
950 {
951 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
952 usbd_status err;
953
954 DPRINTF("\n");
955
956 /* Insert last in queue. */
957 mutex_enter(&sc->sc_lock);
958 err = usb_insert_transfer(xfer);
959 mutex_exit(&sc->sc_lock);
960 if (err)
961 return err;
962
963 /* Pipe isn't running, start first */
964 return dwc2_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
965 }
966
967 Static usbd_status
968 dwc2_device_ctrl_start(usbd_xfer_handle xfer)
969 {
970 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
971 usbd_status err;
972
973 DPRINTF("\n");
974
975 mutex_enter(&sc->sc_lock);
976 xfer->status = USBD_IN_PROGRESS;
977 err = dwc2_device_start(xfer);
978 mutex_exit(&sc->sc_lock);
979
980 if (err)
981 return err;
982
983 if (sc->sc_bus.use_polling)
984 dwc2_waitintr(sc, xfer);
985
986 return USBD_IN_PROGRESS;
987 }
988
989 Static void
990 dwc2_device_ctrl_abort(usbd_xfer_handle xfer)
991 {
992 #ifdef DIAGNOSTIC
993 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
994 #endif
995 KASSERT(mutex_owned(&sc->sc_lock));
996
997 DPRINTF("xfer=%p\n", xfer);
998 dwc2_abort_xfer(xfer, USBD_CANCELLED);
999 }
1000
1001 Static void
1002 dwc2_device_ctrl_close(usbd_pipe_handle pipe)
1003 {
1004
1005 DPRINTF("pipe=%p\n", pipe);
1006 dwc2_close_pipe(pipe);
1007 }
1008
1009 Static void
1010 dwc2_device_ctrl_done(usbd_xfer_handle xfer)
1011 {
1012
1013 DPRINTF("xfer=%p\n", xfer);
1014 }
1015
1016 /***********************************************************************/
1017
1018 Static usbd_status
1019 dwc2_device_bulk_transfer(usbd_xfer_handle xfer)
1020 {
1021 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1022 usbd_status err;
1023
1024 DPRINTF("xfer=%p\n", xfer);
1025
1026 /* Insert last in queue. */
1027 mutex_enter(&sc->sc_lock);
1028 err = usb_insert_transfer(xfer);
1029 mutex_exit(&sc->sc_lock);
1030 if (err)
1031 return err;
1032
1033 /* Pipe isn't running, start first */
1034 return dwc2_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
1035 }
1036
1037 Static usbd_status
1038 dwc2_device_bulk_start(usbd_xfer_handle xfer)
1039 {
1040 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1041 usbd_status err;
1042
1043 DPRINTF("xfer=%p\n", xfer);
1044 mutex_enter(&sc->sc_lock);
1045 xfer->status = USBD_IN_PROGRESS;
1046 err = dwc2_device_start(xfer);
1047 mutex_exit(&sc->sc_lock);
1048
1049 return err;
1050 }
1051
1052 Static void
1053 dwc2_device_bulk_abort(usbd_xfer_handle xfer)
1054 {
1055 #ifdef DIAGNOSTIC
1056 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1057 #endif
1058 KASSERT(mutex_owned(&sc->sc_lock));
1059
1060 DPRINTF("xfer=%p\n", xfer);
1061 dwc2_abort_xfer(xfer, USBD_CANCELLED);
1062 }
1063
1064 Static void
1065 dwc2_device_bulk_close(usbd_pipe_handle pipe)
1066 {
1067
1068 DPRINTF("pipe=%p\n", pipe);
1069
1070 dwc2_close_pipe(pipe);
1071 }
1072
1073 Static void
1074 dwc2_device_bulk_done(usbd_xfer_handle xfer)
1075 {
1076
1077 DPRINTF("xfer=%p\n", xfer);
1078 }
1079
1080 /***********************************************************************/
1081
1082 Static usbd_status
1083 dwc2_device_intr_transfer(usbd_xfer_handle xfer)
1084 {
1085 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1086 usbd_status err;
1087
1088 DPRINTF("xfer=%p\n", xfer);
1089
1090 /* Insert last in queue. */
1091 mutex_enter(&sc->sc_lock);
1092 err = usb_insert_transfer(xfer);
1093 mutex_exit(&sc->sc_lock);
1094 if (err)
1095 return err;
1096
1097 /* Pipe isn't running, start first */
1098 return dwc2_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
1099 }
1100
1101 Static usbd_status
1102 dwc2_device_intr_start(usbd_xfer_handle xfer)
1103 {
1104 struct dwc2_pipe *dpipe = (struct dwc2_pipe *)xfer->pipe;
1105 usbd_device_handle dev = dpipe->pipe.device;
1106 struct dwc2_softc *sc = dev->bus->hci_private;
1107 usbd_status err;
1108
1109 mutex_enter(&sc->sc_lock);
1110 xfer->status = USBD_IN_PROGRESS;
1111 err = dwc2_device_start(xfer);
1112 mutex_exit(&sc->sc_lock);
1113
1114 if (err)
1115 return err;
1116
1117 if (sc->sc_bus.use_polling)
1118 dwc2_waitintr(sc, xfer);
1119
1120 return USBD_IN_PROGRESS;
1121 }
1122
1123 /* Abort a device interrupt request. */
1124 Static void
1125 dwc2_device_intr_abort(usbd_xfer_handle xfer)
1126 {
1127 #ifdef DIAGNOSTIC
1128 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1129 #endif
1130
1131 KASSERT(mutex_owned(&sc->sc_lock));
1132
1133 if (xfer->pipe->intrxfer == xfer) {
1134 DPRINTF("remove\n");
1135 xfer->pipe->intrxfer = NULL;
1136 }
1137 DPRINTF("xfer=%p\n", xfer);
1138 dwc2_abort_xfer(xfer, USBD_CANCELLED);
1139 }
1140
1141 Static void
1142 dwc2_device_intr_close(usbd_pipe_handle pipe)
1143 {
1144
1145 DPRINTF("pipe=%p\n", pipe);
1146
1147 dwc2_close_pipe(pipe);
1148 }
1149
1150 Static void
1151 dwc2_device_intr_done(usbd_xfer_handle xfer)
1152 {
1153
1154 DPRINTF("\n");
1155
1156 if (xfer->pipe->repeat) {
1157 xfer->status = USBD_IN_PROGRESS;
1158 dwc2_device_start(xfer);
1159 }
1160 }
1161
1162 /***********************************************************************/
1163
1164 usbd_status
1165 dwc2_device_isoc_transfer(usbd_xfer_handle xfer)
1166 {
1167 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1168 usbd_status err;
1169
1170 DPRINTF("xfer=%p\n", xfer);
1171
1172 /* Insert last in queue. */
1173 mutex_enter(&sc->sc_lock);
1174 err = usb_insert_transfer(xfer);
1175 mutex_exit(&sc->sc_lock);
1176 if (err)
1177 return err;
1178
1179 /* Pipe isn't running, start first */
1180 return dwc2_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
1181 }
1182
1183 usbd_status
1184 dwc2_device_isoc_start(usbd_xfer_handle xfer)
1185 {
1186 struct dwc2_pipe *dpipe = (struct dwc2_pipe *)xfer->pipe;
1187 usbd_device_handle dev = dpipe->pipe.device;
1188 struct dwc2_softc *sc = dev->bus->hci_private;
1189 usbd_status err;
1190
1191 mutex_enter(&sc->sc_lock);
1192 xfer->status = USBD_IN_PROGRESS;
1193 err = dwc2_device_start(xfer);
1194 mutex_exit(&sc->sc_lock);
1195
1196 if (sc->sc_bus.use_polling)
1197 dwc2_waitintr(sc, xfer);
1198
1199 return err;
1200 }
1201
1202 void
1203 dwc2_device_isoc_abort(usbd_xfer_handle xfer)
1204 {
1205 #ifdef DIAGNOSTIC
1206 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1207 #endif
1208 KASSERT(mutex_owned(&sc->sc_lock));
1209
1210 DPRINTF("xfer=%p\n", xfer);
1211 dwc2_abort_xfer(xfer, USBD_CANCELLED);
1212 }
1213
1214 void
1215 dwc2_device_isoc_close(usbd_pipe_handle pipe)
1216 {
1217 DPRINTF("\n");
1218
1219 dwc2_close_pipe(pipe);
1220 }
1221
1222 void
1223 dwc2_device_isoc_done(usbd_xfer_handle xfer)
1224 {
1225
1226 DPRINTF("\n");
1227 }
1228
1229
1230 usbd_status
1231 dwc2_device_start(usbd_xfer_handle xfer)
1232 {
1233 struct dwc2_xfer *dxfer = DWC2_XFER2DXFER(xfer);
1234 struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
1235 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1236 struct dwc2_hsotg *hsotg = sc->sc_hsotg;
1237 struct dwc2_hcd_urb *dwc2_urb;
1238
1239 usbd_device_handle dev = xfer->pipe->device;
1240 usb_endpoint_descriptor_t *ed = xfer->pipe->endpoint->edesc;
1241 uint8_t addr = dev->address;
1242 uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
1243 uint8_t epnum = UE_GET_ADDR(ed->bEndpointAddress);
1244 uint8_t dir = UE_GET_DIR(ed->bEndpointAddress);
1245 uint16_t mps = UE_GET_SIZE(UGETW(ed->wMaxPacketSize));
1246 uint32_t len;
1247
1248 uint32_t flags = 0;
1249 uint32_t off = 0;
1250 int retval, err = USBD_IN_PROGRESS;
1251 int alloc_bandwidth = 0;
1252 int i;
1253
1254 DPRINTFN(1, "xfer=%p pipe=%p\n", xfer, xfer->pipe);
1255
1256 if (xfertype == UE_ISOCHRONOUS ||
1257 xfertype == UE_INTERRUPT) {
1258 mutex_spin_enter(&hsotg->lock);
1259 if (!dwc2_hcd_is_bandwidth_allocated(hsotg, xfer))
1260 alloc_bandwidth = 1;
1261 mutex_spin_exit(&hsotg->lock);
1262 }
1263
1264 /*
1265 * For Control pipe the direction is from the request, all other
1266 * transfers have been set correctly at pipe open time.
1267 */
1268 if (xfertype == UE_CONTROL) {
1269 usb_device_request_t *req = &xfer->request;
1270
1271 DPRINTFN(3, "xfer=%p type=0x%02x request=0x%02x wValue=0x%04x "
1272 "wIndex=0x%04x len=%d addr=%d endpt=%d dir=%s speed=%d "
1273 "mps=%d\n",
1274 xfer, req->bmRequestType, req->bRequest, UGETW(req->wValue),
1275 UGETW(req->wIndex), UGETW(req->wLength), dev->address,
1276 epnum, dir == UT_READ ? "in" :"out", dev->speed, mps);
1277
1278 /* Copy request packet to our DMA buffer */
1279 memcpy(KERNADDR(&dpipe->req_dma, 0), req, sizeof(*req));
1280 usb_syncmem(&dpipe->req_dma, 0, sizeof(*req),
1281 BUS_DMASYNC_PREWRITE);
1282 len = UGETW(req->wLength);
1283 if ((req->bmRequestType & UT_READ) == UT_READ) {
1284 dir = UE_DIR_IN;
1285 } else {
1286 dir = UE_DIR_OUT;
1287 }
1288
1289 DPRINTFN(3, "req = %p dma = %lx len %d dir %s\n",
1290 KERNADDR(&dpipe->req_dma, 0), DMAADDR(&dpipe->req_dma, 0),
1291 len, dir == UE_DIR_IN ? "in" : "out");
1292 } else {
1293 DPRINTFN(3, "xfer=%p len=%d flags=%d addr=%d endpt=%d,"
1294 " mps=%d dir %s\n", xfer, xfer->length, xfer->flags, addr,
1295 epnum, mps, dir == UT_READ ? "in" :"out");
1296
1297 len = xfer->length;
1298 }
1299
1300 dwc2_urb = dxfer->urb;
1301 if (!dwc2_urb)
1302 return USBD_NOMEM;
1303
1304 memset(dwc2_urb, 0, sizeof(*dwc2_urb));
1305
1306 dwc2_urb->priv = xfer;
1307 dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, addr, epnum, xfertype, dir,
1308 mps);
1309
1310 if (xfertype == UE_CONTROL) {
1311 dwc2_urb->setup_usbdma = &dpipe->req_dma;
1312 dwc2_urb->setup_packet = KERNADDR(&dpipe->req_dma, 0);
1313 dwc2_urb->setup_dma = DMAADDR(&dpipe->req_dma, 0);
1314 } else {
1315 /* XXXNH - % mps required? */
1316 if ((xfer->flags & USBD_FORCE_SHORT_XFER) && (len % mps) == 0)
1317 flags |= URB_SEND_ZERO_PACKET;
1318 }
1319 flags |= URB_GIVEBACK_ASAP;
1320
1321 /* XXXNH this shouldn't be required */
1322 if (xfertype == UE_CONTROL && len == 0) {
1323 KASSERT(xfertype == UE_CONTROL);
1324 dwc2_urb->usbdma = &dpipe->req_dma;
1325 } else {
1326 KASSERT(xfertype != UE_CONTROL || len != 0);
1327 dwc2_urb->usbdma = &xfer->dmabuf;
1328 }
1329 dwc2_urb->buf = KERNADDR(dwc2_urb->usbdma, 0);
1330 dwc2_urb->dma = DMAADDR(dwc2_urb->usbdma, 0);
1331 dwc2_urb->length = len;
1332 dwc2_urb->flags = flags;
1333 dwc2_urb->status = -EINPROGRESS;
1334 dwc2_urb->packet_count = xfer->nframes;
1335
1336 if (xfertype == UE_INTERRUPT)
1337 dwc2_urb->interval = dpipe->pipe.interval;
1338 else if (xfertype == UE_ISOCHRONOUS)
1339 dwc2_urb->interval = dpipe->pipe.endpoint->edesc->bInterval;
1340
1341 /* XXXNH bring down from callers?? */
1342 // mutex_enter(&sc->sc_lock);
1343
1344 xfer->actlen = 0;
1345
1346 KASSERT(xfertype != UE_ISOCHRONOUS ||
1347 xfer->nframes < DWC2_MAXISOCPACKETS);
1348 KASSERTMSG(xfer->nframes == 0 || xfertype == UE_ISOCHRONOUS,
1349 "nframes %d xfertype %d\n", xfer->nframes, xfertype);
1350
1351 for (off = i = 0; i < xfer->nframes; ++i) {
1352 DPRINTFN(3, "xfer=%p frame=%d offset=%d length=%d\n", xfer, i,
1353 off, xfer->frlengths[i]);
1354
1355 dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i, off,
1356 xfer->frlengths[i]);
1357 off += xfer->frlengths[i];
1358 }
1359
1360 /* might need to check cpu_intr_p */
1361 retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, &dpipe->priv, 0);
1362 if (retval)
1363 goto fail2;
1364
1365 if (xfer->timeout && !sc->sc_bus.use_polling) {
1366 callout_reset(&xfer->timeout_handle, mstohz(xfer->timeout),
1367 dwc2_timeout, xfer);
1368 }
1369
1370 if (alloc_bandwidth) {
1371 mutex_spin_enter(&hsotg->lock);
1372 dwc2_allocate_bus_bandwidth(hsotg,
1373 dwc2_hcd_get_ep_bandwidth(hsotg, dpipe),
1374 xfer);
1375 mutex_spin_exit(&hsotg->lock);
1376 }
1377
1378 fail2:
1379 // mutex_exit(&sc->sc_lock);
1380
1381 switch (retval) {
1382 case 0:
1383 break;
1384 case -ENODEV:
1385 err = USBD_NOMEM;
1386 break;
1387 case -ENOMEM:
1388 err = USBD_NOMEM;
1389 break;
1390 default:
1391 err = USBD_IOERROR;
1392 }
1393
1394 // fail1:
1395 if (err) {
1396 dpipe->xfer = NULL;
1397 }
1398
1399 return err;
1400
1401 }
1402
1403 void
1404 dwc2_worker(struct work *wk, void *priv)
1405 {
1406 struct dwc2_softc *sc = priv;
1407 struct dwc2_hsotg *hsotg = sc->sc_hsotg;
1408
1409 Debugger();
1410 #if 0
1411 usbd_xfer_handle xfer = dwork->xfer;
1412 struct dwc2_xfer *dxfer = DWC2_XFER2DXFER(xfer);
1413
1414 dwc2_hcd_endpoint_disable(sc->dwc_dev.hcd, dpipe->priv, 250);
1415 dwc_free(NULL, dpipe->urb);
1416 #endif
1417
1418 mutex_enter(&sc->sc_lock);
1419 if (wk == &hsotg->wf_otg) {
1420 dwc2_conn_id_status_change(wk);
1421 } else if (wk == &hsotg->start_work.work) {
1422 dwc2_hcd_start_func(wk);
1423 } else if (wk == &hsotg->reset_work.work) {
1424 dwc2_hcd_reset_func(wk);
1425 } else {
1426 #if 0
1427 KASSERT(dwork->xfer != NULL);
1428 KASSERT(dxfer->queued == true);
1429
1430 if (!(xfer->hcflags & UXFER_ABORTING)) {
1431 dwc2_start_standard_chain(xfer);
1432 }
1433 dxfer->queued = false;
1434 cv_broadcast(&xfer->hccv);
1435 #endif
1436 }
1437 mutex_exit(&sc->sc_lock);
1438 }
1439
1440 int dwc2_intr(void *p)
1441 {
1442 struct dwc2_softc *sc = p;
1443 struct dwc2_hsotg *hsotg;
1444 int ret = 0;
1445
1446 if (sc == NULL)
1447 return 0;
1448
1449 hsotg = sc->sc_hsotg;
1450 mutex_spin_enter(&hsotg->lock);
1451
1452 if (sc->sc_dying || !device_has_power(sc->sc_dev))
1453 goto done;
1454
1455 if (sc->sc_bus.use_polling) {
1456 uint32_t intrs;
1457
1458 intrs = dwc2_read_core_intr(hsotg);
1459 DWC2_WRITE_4(hsotg, GINTSTS, intrs);
1460 } else {
1461 ret = dwc2_interrupt(sc);
1462 }
1463
1464 done:
1465 mutex_spin_exit(&hsotg->lock);
1466
1467 return ret;
1468 }
1469
1470 int
1471 dwc2_interrupt(struct dwc2_softc *sc)
1472 {
1473 int ret = 0;
1474
1475 if (sc->sc_hcdenabled) {
1476 ret |= dwc2_handle_hcd_intr(sc->sc_hsotg);
1477 }
1478
1479 ret |= dwc2_handle_common_intr(sc->sc_hsotg);
1480
1481 return ret;
1482 }
1483
1484 /***********************************************************************/
1485
1486 int
1487 dwc2_detach(struct dwc2_softc *sc, int flags)
1488 {
1489 int rv = 0;
1490
1491 if (sc->sc_child != NULL)
1492 rv = config_detach(sc->sc_child, flags);
1493
1494 return rv;
1495 }
1496
1497 bool
1498 dwc2_shutdown(device_t self, int flags)
1499 {
1500 struct dwc2_softc *sc = device_private(self);
1501
1502 sc = sc;
1503
1504 return true;
1505 }
1506
1507 void
1508 dwc2_childdet(device_t self, device_t child)
1509 {
1510 struct dwc2_softc *sc = device_private(self);
1511
1512 sc = sc;
1513 }
1514
1515 int
1516 dwc2_activate(device_t self, enum devact act)
1517 {
1518 struct dwc2_softc *sc = device_private(self);
1519
1520 sc = sc;
1521
1522 return 0;
1523 }
1524
1525 bool
1526 dwc2_resume(device_t dv, const pmf_qual_t *qual)
1527 {
1528 struct dwc2_softc *sc = device_private(dv);
1529
1530 sc = sc;
1531
1532 return true;
1533 }
1534
1535 bool
1536 dwc2_suspend(device_t dv, const pmf_qual_t *qual)
1537 {
1538 struct dwc2_softc *sc = device_private(dv);
1539
1540 sc = sc;
1541
1542 return true;
1543 }
1544
1545 /***********************************************************************/
1546 Static int
1547 dwc2_init(struct dwc2_softc *sc)
1548 {
1549 int err = 0;
1550
1551 sc->sc_bus.hci_private = sc;
1552 sc->sc_bus.usbrev = USBREV_2_0;
1553 sc->sc_bus.methods = &dwc2_bus_methods;
1554 sc->sc_bus.pipe_size = sizeof(struct dwc2_pipe);
1555 sc->sc_hcdenabled = false;
1556
1557 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
1558
1559 TAILQ_INIT(&sc->sc_complete);
1560
1561 sc->sc_rhc_si = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
1562 dwc2_rhc, sc);
1563
1564 usb_setup_reserve(sc->sc_dev, &sc->sc_dma_reserve, sc->sc_bus.dmatag,
1565 USB_MEM_RESERVE);
1566
1567 sc->sc_xferpool = pool_cache_init(sizeof(struct dwc2_xfer), 0, 0, 0,
1568 "dwc2xfer", NULL, IPL_USB, NULL, NULL, NULL);
1569 sc->sc_qhpool = pool_cache_init(sizeof(struct dwc2_qh), 0, 0, 0,
1570 "dwc2qh", NULL, IPL_USB, NULL, NULL, NULL);
1571 sc->sc_qtdpool = pool_cache_init(sizeof(struct dwc2_qtd), 0, 0, 0,
1572 "dwc2qtd", NULL, IPL_USB, NULL, NULL, NULL);
1573
1574 sc->sc_hsotg = kmem_zalloc(sizeof(struct dwc2_hsotg), KM_SLEEP);
1575 if (sc->sc_hsotg == NULL) {
1576 err = ENOMEM;
1577 goto fail1;
1578 }
1579
1580 sc->sc_hsotg->hsotg_sc = sc;
1581 sc->sc_hsotg->dev = sc->sc_dev;
1582 sc->sc_hcdenabled = true;
1583
1584 err = dwc2_hcd_init(sc->sc_hsotg, sc->sc_params);
1585 if (err) {
1586 err = -err;
1587 goto fail2;
1588 }
1589
1590 return 0;
1591
1592 fail2:
1593 kmem_free(sc->sc_hsotg, sizeof(struct dwc2_hsotg));
1594 fail1:
1595 softint_disestablish(sc->sc_rhc_si);
1596
1597 return err;
1598 }
1599
1600 #if 0
1601 /*
1602 * curmode is a mode indication bit 0 = device, 1 = host
1603 */
1604 static const char * const intnames[32] = {
1605 "curmode", "modemis", "otgint", "sof",
1606 "rxflvl", "nptxfemp", "ginnakeff", "goutnakeff",
1607 "ulpickint", "i2cint", "erlysusp", "usbsusp",
1608 "usbrst", "enumdone", "isooutdrop", "eopf",
1609 "restore_done", "epmis", "iepint", "oepint",
1610 "incompisoin", "incomplp", "fetsusp", "resetdet",
1611 "prtint", "hchint", "ptxfemp", "lpm",
1612 "conidstschng", "disconnint", "sessreqint", "wkupint"
1613 };
1614
1615
1616 /***********************************************************************/
1617
1618 #endif
1619
1620
1621 void
1622 dw_callout(void *arg)
1623 {
1624 struct delayed_work *dw = arg;
1625
1626 workqueue_enqueue(dw->dw_wq, &dw->work, NULL);
1627 }
1628
1629 void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context, int *hub_addr,
1630 int *hub_port)
1631 {
1632 usbd_xfer_handle xfer = context;
1633 struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
1634 usbd_device_handle dev = dpipe->pipe.device;
1635
1636 *hub_addr = dev->myhsport->parent->address;
1637 *hub_port = dev->myhsport->portno;
1638 }
1639
1640 int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
1641 {
1642 usbd_xfer_handle xfer = context;
1643 struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
1644 usbd_device_handle dev = dpipe->pipe.device;
1645
1646 return dev->speed;
1647 }
1648
1649 /*
1650 * Sets the final status of an URB and returns it to the upper layer. Any
1651 * required cleanup of the URB is performed.
1652 *
1653 * Must be called with interrupt disabled and spinlock held
1654 */
1655 void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
1656 int status)
1657 {
1658 usbd_xfer_handle xfer;
1659 struct dwc2_xfer *dxfer;
1660 struct dwc2_softc *sc;
1661 usb_endpoint_descriptor_t *ed;
1662 uint8_t xfertype;
1663
1664 if (!qtd) {
1665 dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
1666 return;
1667 }
1668
1669 if (!qtd->urb) {
1670 dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
1671 return;
1672 }
1673
1674 xfer = qtd->urb->priv;
1675 if (!xfer) {
1676 dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
1677 return;
1678 }
1679
1680 dxfer = DWC2_XFER2DXFER(xfer);
1681 sc = DWC2_XFER2SC(xfer);
1682 ed = xfer->pipe->endpoint->edesc;
1683 xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
1684
1685 xfer->actlen = dwc2_hcd_urb_get_actual_length(qtd->urb);
1686
1687 if (xfertype == UE_ISOCHRONOUS && dbg_perio()) {
1688 int i;
1689
1690 for (i = 0; i < xfer->nframes; i++)
1691 dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
1692 i, qtd->urb->iso_descs[i].status);
1693 }
1694
1695 if (xfertype == UE_ISOCHRONOUS) {
1696 int i;
1697
1698 xfer->actlen = 0;
1699 for (i = 0; i < xfer->nframes; ++i) {
1700 xfer->frlengths[i] =
1701 dwc2_hcd_urb_get_iso_desc_actual_length(
1702 qtd->urb, i);
1703 xfer->actlen += xfer->frlengths[i];
1704 }
1705 }
1706
1707 if (!status) {
1708 if (!(xfer->flags & USBD_SHORT_XFER_OK) &&
1709 xfer->actlen < xfer->length)
1710 status = -EIO;
1711 }
1712
1713 switch (status) {
1714 case 0:
1715 xfer->status = USBD_NORMAL_COMPLETION;
1716 break;
1717 case -EPIPE:
1718 xfer->status = USBD_STALLED;
1719 break;
1720 case -ETIMEDOUT:
1721 xfer->status = USBD_TIMEOUT;
1722 break;
1723 case -EPROTO:
1724 xfer->status = USBD_INVAL;
1725 break;
1726 case -EIO:
1727 xfer->status = USBD_IOERROR;
1728 break;
1729 case -EOVERFLOW:
1730 xfer->status = USBD_IOERROR;
1731 break;
1732 default:
1733 printf("%s: unknown error status %d\n", __func__, status);
1734 }
1735
1736 if (xfertype == UE_ISOCHRONOUS ||
1737 xfertype == UE_INTERRUPT) {
1738 struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
1739
1740 dwc2_free_bus_bandwidth(hsotg,
1741 dwc2_hcd_get_ep_bandwidth(hsotg, dpipe),
1742 xfer);
1743 }
1744
1745 qtd->urb = NULL;
1746 callout_stop(&xfer->timeout_handle);
1747
1748 KASSERT(mutex_owned(&hsotg->lock));
1749
1750 TAILQ_INSERT_TAIL(&sc->sc_complete, dxfer, xnext);
1751
1752 usb_schedsoftintr(&sc->sc_bus);
1753 }
1754
1755
1756 int
1757 _dwc2_hcd_start(struct dwc2_hsotg *hsotg)
1758 {
1759 dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
1760
1761 mutex_spin_enter(&hsotg->lock);
1762
1763 hsotg->op_state = OTG_STATE_A_HOST;
1764
1765 dwc2_hcd_reinit(hsotg);
1766
1767 /*XXXNH*/
1768 delay(50);
1769
1770 mutex_spin_exit(&hsotg->lock);
1771 return 0;
1772 }
1773
1774 int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
1775 {
1776
1777 return false;
1778 }
1779