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