dwc2.c revision 1.7 1 /* $NetBSD: dwc2.c,v 1.7 2013/09/27 22:03:01 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.7 2013/09/27 22:03:01 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
972 DPRINTF("\n");
973
974 mutex_enter(&sc->sc_lock);
975 xfer->status = USBD_IN_PROGRESS;
976 dwc2_device_start(xfer);
977 mutex_exit(&sc->sc_lock);
978
979 if (sc->sc_bus.use_polling)
980 dwc2_waitintr(sc, xfer);
981
982 return USBD_IN_PROGRESS;
983 }
984
985 Static void
986 dwc2_device_ctrl_abort(usbd_xfer_handle xfer)
987 {
988 #ifdef DIAGNOSTIC
989 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
990 #endif
991 KASSERT(mutex_owned(&sc->sc_lock));
992
993 DPRINTF("xfer=%p\n", xfer);
994 dwc2_abort_xfer(xfer, USBD_CANCELLED);
995 }
996
997 Static void
998 dwc2_device_ctrl_close(usbd_pipe_handle pipe)
999 {
1000
1001 DPRINTF("pipe=%p\n", pipe);
1002 dwc2_close_pipe(pipe);
1003 }
1004
1005 Static void
1006 dwc2_device_ctrl_done(usbd_xfer_handle xfer)
1007 {
1008
1009 DPRINTF("xfer=%p\n", xfer);
1010 }
1011
1012 /***********************************************************************/
1013
1014 Static usbd_status
1015 dwc2_device_bulk_transfer(usbd_xfer_handle xfer)
1016 {
1017 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1018 usbd_status err;
1019
1020 DPRINTF("xfer=%p\n", xfer);
1021
1022 /* Insert last in queue. */
1023 mutex_enter(&sc->sc_lock);
1024 err = usb_insert_transfer(xfer);
1025 mutex_exit(&sc->sc_lock);
1026 if (err)
1027 return err;
1028
1029 /* Pipe isn't running, start first */
1030 return dwc2_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
1031 }
1032
1033 Static usbd_status
1034 dwc2_device_bulk_start(usbd_xfer_handle xfer)
1035 {
1036 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1037
1038 DPRINTF("xfer=%p\n", xfer);
1039 mutex_enter(&sc->sc_lock);
1040 xfer->status = USBD_IN_PROGRESS;
1041 dwc2_device_start(xfer);
1042 mutex_exit(&sc->sc_lock);
1043
1044 return USBD_IN_PROGRESS;
1045 }
1046
1047 Static void
1048 dwc2_device_bulk_abort(usbd_xfer_handle xfer)
1049 {
1050 #ifdef DIAGNOSTIC
1051 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1052 #endif
1053 KASSERT(mutex_owned(&sc->sc_lock));
1054
1055 DPRINTF("xfer=%p\n", xfer);
1056 dwc2_abort_xfer(xfer, USBD_CANCELLED);
1057 }
1058
1059 Static void
1060 dwc2_device_bulk_close(usbd_pipe_handle pipe)
1061 {
1062
1063 DPRINTF("pipe=%p\n", pipe);
1064
1065 dwc2_close_pipe(pipe);
1066 }
1067
1068 Static void
1069 dwc2_device_bulk_done(usbd_xfer_handle xfer)
1070 {
1071
1072 DPRINTF("xfer=%p\n", xfer);
1073 }
1074
1075 /***********************************************************************/
1076
1077 Static usbd_status
1078 dwc2_device_intr_transfer(usbd_xfer_handle xfer)
1079 {
1080 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1081 usbd_status err;
1082
1083 DPRINTF("xfer=%p\n", xfer);
1084
1085 /* Insert last in queue. */
1086 mutex_enter(&sc->sc_lock);
1087 err = usb_insert_transfer(xfer);
1088 mutex_exit(&sc->sc_lock);
1089 if (err)
1090 return err;
1091
1092 /* Pipe isn't running, start first */
1093 return dwc2_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
1094 }
1095
1096 Static usbd_status
1097 dwc2_device_intr_start(usbd_xfer_handle xfer)
1098 {
1099 struct dwc2_pipe *dpipe = (struct dwc2_pipe *)xfer->pipe;
1100 usbd_device_handle dev = dpipe->pipe.device;
1101 struct dwc2_softc *sc = dev->bus->hci_private;
1102
1103 mutex_enter(&sc->sc_lock);
1104 xfer->status = USBD_IN_PROGRESS;
1105 dwc2_device_start(xfer);
1106 mutex_exit(&sc->sc_lock);
1107
1108 if (sc->sc_bus.use_polling)
1109 dwc2_waitintr(sc, xfer);
1110
1111 return USBD_IN_PROGRESS;
1112 }
1113
1114 /* Abort a device interrupt request. */
1115 Static void
1116 dwc2_device_intr_abort(usbd_xfer_handle xfer)
1117 {
1118 #ifdef DIAGNOSTIC
1119 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1120 #endif
1121
1122 KASSERT(mutex_owned(&sc->sc_lock));
1123
1124 if (xfer->pipe->intrxfer == xfer) {
1125 DPRINTF("remove\n");
1126 xfer->pipe->intrxfer = NULL;
1127 }
1128 DPRINTF("xfer=%p\n", xfer);
1129 dwc2_abort_xfer(xfer, USBD_CANCELLED);
1130 }
1131
1132 Static void
1133 dwc2_device_intr_close(usbd_pipe_handle pipe)
1134 {
1135
1136 DPRINTF("pipe=%p\n", pipe);
1137
1138 dwc2_close_pipe(pipe);
1139 }
1140
1141 Static void
1142 dwc2_device_intr_done(usbd_xfer_handle xfer)
1143 {
1144
1145 DPRINTF("\n");
1146
1147 if (xfer->pipe->repeat) {
1148 xfer->status = USBD_IN_PROGRESS;
1149 dwc2_device_start(xfer);
1150 }
1151 }
1152
1153 /***********************************************************************/
1154
1155 usbd_status
1156 dwc2_device_isoc_transfer(usbd_xfer_handle xfer)
1157 {
1158 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1159 usbd_status err;
1160
1161 DPRINTF("xfer=%p\n", xfer);
1162
1163 /* Insert last in queue. */
1164 mutex_enter(&sc->sc_lock);
1165 err = usb_insert_transfer(xfer);
1166 mutex_exit(&sc->sc_lock);
1167 if (err)
1168 return err;
1169
1170 /* Pipe isn't running, start first */
1171 return dwc2_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
1172 }
1173
1174 usbd_status
1175 dwc2_device_isoc_start(usbd_xfer_handle xfer)
1176 {
1177 struct dwc2_pipe *dpipe = (struct dwc2_pipe *)xfer->pipe;
1178 usbd_device_handle dev = dpipe->pipe.device;
1179 struct dwc2_softc *sc = dev->bus->hci_private;
1180
1181 mutex_enter(&sc->sc_lock);
1182 xfer->status = USBD_IN_PROGRESS;
1183 dwc2_device_start(xfer);
1184 mutex_exit(&sc->sc_lock);
1185
1186 if (sc->sc_bus.use_polling)
1187 dwc2_waitintr(sc, xfer);
1188
1189 return USBD_IN_PROGRESS;
1190 }
1191
1192 void
1193 dwc2_device_isoc_abort(usbd_xfer_handle xfer)
1194 {
1195 #ifdef DIAGNOSTIC
1196 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1197 #endif
1198 KASSERT(mutex_owned(&sc->sc_lock));
1199
1200 DPRINTF("xfer=%p\n", xfer);
1201 dwc2_abort_xfer(xfer, USBD_CANCELLED);
1202 }
1203
1204 void
1205 dwc2_device_isoc_close(usbd_pipe_handle pipe)
1206 {
1207 DPRINTF("\n");
1208
1209 dwc2_close_pipe(pipe);
1210 }
1211
1212 void
1213 dwc2_device_isoc_done(usbd_xfer_handle xfer)
1214 {
1215
1216 DPRINTF("\n");
1217 }
1218
1219
1220 usbd_status
1221 dwc2_device_start(usbd_xfer_handle xfer)
1222 {
1223 struct dwc2_xfer *dxfer = DWC2_XFER2DXFER(xfer);
1224 struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
1225 struct dwc2_softc *sc = DWC2_XFER2SC(xfer);
1226 struct dwc2_hsotg *hsotg = sc->sc_hsotg;
1227 struct dwc2_hcd_urb *dwc2_urb;
1228
1229 usbd_device_handle dev = xfer->pipe->device;
1230 usb_endpoint_descriptor_t *ed = xfer->pipe->endpoint->edesc;
1231 uint8_t addr = dev->address;
1232 uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
1233 uint8_t epnum = UE_GET_ADDR(ed->bEndpointAddress);
1234 uint8_t dir = UE_GET_DIR(ed->bEndpointAddress);
1235 uint16_t mps = UE_GET_SIZE(UGETW(ed->wMaxPacketSize));
1236 uint32_t len;
1237
1238 uint32_t flags = 0;
1239 uint32_t off = 0;
1240 int retval, err = USBD_IN_PROGRESS;
1241 int alloc_bandwidth = 0;
1242 int i;
1243
1244 DPRINTFN(1, "xfer=%p pipe=%p\n", xfer, xfer->pipe);
1245
1246 if (xfertype == UE_ISOCHRONOUS ||
1247 xfertype == UE_INTERRUPT) {
1248 mutex_spin_enter(&hsotg->lock);
1249 if (!dwc2_hcd_is_bandwidth_allocated(hsotg, xfer))
1250 alloc_bandwidth = 1;
1251 mutex_spin_exit(&hsotg->lock);
1252 }
1253
1254 /*
1255 * For Control pipe the direction is from the request, all other
1256 * transfers have been set correctly at pipe open time.
1257 */
1258 if (xfertype == UE_CONTROL) {
1259 usb_device_request_t *req = &xfer->request;
1260
1261 DPRINTFN(3, "xfer=%p type=0x%02x request=0x%02x wValue=0x%04x "
1262 "wIndex=0x%04x len=%d addr=%d endpt=%d dir=%s speed=%d "
1263 "mps=%d\n",
1264 xfer, req->bmRequestType, req->bRequest, UGETW(req->wValue),
1265 UGETW(req->wIndex), UGETW(req->wLength), dev->address,
1266 epnum, dir == UT_READ ? "in" :"out", dev->speed, mps);
1267
1268 /* Copy request packet to our DMA buffer */
1269 memcpy(KERNADDR(&dpipe->req_dma, 0), req, sizeof(*req));
1270 usb_syncmem(&dpipe->req_dma, 0, sizeof(*req),
1271 BUS_DMASYNC_PREWRITE);
1272 len = UGETW(req->wLength);
1273 if ((req->bmRequestType & UT_READ) == UT_READ) {
1274 dir = UE_DIR_IN;
1275 } else {
1276 dir = UE_DIR_OUT;
1277 }
1278
1279 DPRINTFN(3, "req = %p dma = %lx len %d dir %s\n",
1280 KERNADDR(&dpipe->req_dma, 0), DMAADDR(&dpipe->req_dma, 0),
1281 len, dir == UE_DIR_IN ? "in" : "out");
1282 } else {
1283 DPRINTFN(3, "xfer=%p len=%d flags=%d addr=%d endpt=%d,"
1284 " mps=%d dir %s\n", xfer, xfer->length, xfer->flags, addr,
1285 epnum, mps, dir == UT_READ ? "in" :"out");
1286
1287 len = xfer->length;
1288 }
1289
1290 dwc2_urb = dxfer->urb;
1291 if (!dwc2_urb)
1292 return USBD_NOMEM;
1293
1294 memset(dwc2_urb, 0, sizeof(*dwc2_urb));
1295
1296 dwc2_urb->priv = xfer;
1297 dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, addr, epnum, xfertype, dir,
1298 mps);
1299
1300 if (xfertype == UE_CONTROL) {
1301 dwc2_urb->setup_usbdma = &dpipe->req_dma;
1302 dwc2_urb->setup_packet = KERNADDR(&dpipe->req_dma, 0);
1303 dwc2_urb->setup_dma = DMAADDR(&dpipe->req_dma, 0);
1304 } else {
1305 /* XXXNH - % mps required? */
1306 if ((xfer->flags & USBD_FORCE_SHORT_XFER) && (len % mps) == 0)
1307 flags |= URB_SEND_ZERO_PACKET;
1308 }
1309 flags |= URB_GIVEBACK_ASAP;
1310
1311 /* XXXNH this shouldn't be required */
1312 if (xfertype == UE_CONTROL && len == 0) {
1313 KASSERT(xfertype == UE_CONTROL);
1314 dwc2_urb->usbdma = &dpipe->req_dma;
1315 } else {
1316 KASSERT(xfertype != UE_CONTROL || len != 0);
1317 dwc2_urb->usbdma = &xfer->dmabuf;
1318 }
1319 dwc2_urb->buf = KERNADDR(dwc2_urb->usbdma, 0);
1320 dwc2_urb->dma = DMAADDR(dwc2_urb->usbdma, 0);
1321 dwc2_urb->length = len;
1322 dwc2_urb->flags = flags;
1323 dwc2_urb->status = -EINPROGRESS;
1324 dwc2_urb->packet_count = xfer->nframes;
1325
1326 if (xfertype == UE_INTERRUPT)
1327 dwc2_urb->interval = dpipe->pipe.interval;
1328 else if (xfertype == UE_ISOCHRONOUS)
1329 dwc2_urb->interval = dpipe->pipe.endpoint->edesc->bInterval;
1330
1331 /* XXXNH bring down from callers?? */
1332 // mutex_enter(&sc->sc_lock);
1333
1334 xfer->actlen = 0;
1335
1336 KASSERT(xfertype != UE_ISOCHRONOUS ||
1337 xfer->nframes < DWC2_MAXISOCPACKETS);
1338 KASSERTMSG(xfer->nframes == 0 || xfertype == UE_ISOCHRONOUS,
1339 "nframes %d xfertype %d\n", xfer->nframes, xfertype);
1340
1341 for (off = i = 0; i < xfer->nframes; ++i) {
1342 DPRINTFN(3, "xfer=%p frame=%d offset=%d length=%d\n", xfer, i,
1343 off, xfer->frlengths[i]);
1344
1345 dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i, off,
1346 xfer->frlengths[i]);
1347 off += xfer->frlengths[i];
1348 }
1349
1350 /* might need to check cpu_intr_p */
1351 retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, &dpipe->priv, 0);
1352 if (retval)
1353 goto fail2;
1354
1355 if (xfer->timeout && !sc->sc_bus.use_polling) {
1356 callout_reset(&xfer->timeout_handle, mstohz(xfer->timeout),
1357 dwc2_timeout, xfer);
1358 }
1359
1360 if (alloc_bandwidth) {
1361 mutex_spin_enter(&hsotg->lock);
1362 dwc2_allocate_bus_bandwidth(hsotg,
1363 dwc2_hcd_get_ep_bandwidth(hsotg, dpipe),
1364 xfer);
1365 mutex_spin_exit(&hsotg->lock);
1366 }
1367
1368 fail2:
1369 // mutex_exit(&sc->sc_lock);
1370
1371 switch (retval) {
1372 case 0:
1373 break;
1374 case -ENODEV:
1375 err = USBD_NOMEM;
1376 break;
1377 case -ENOMEM:
1378 err = USBD_NOMEM;
1379 break;
1380 default:
1381 err = USBD_IOERROR;
1382 }
1383
1384 // fail1:
1385 if (err) {
1386 dpipe->xfer = NULL;
1387 }
1388
1389 return err;
1390
1391 }
1392
1393 void
1394 dwc2_worker(struct work *wk, void *priv)
1395 {
1396 struct dwc2_softc *sc = priv;
1397 struct dwc2_hsotg *hsotg = sc->sc_hsotg;
1398
1399 Debugger();
1400 #if 0
1401 usbd_xfer_handle xfer = dwork->xfer;
1402 struct dwc2_xfer *dxfer = DWC2_XFER2DXFER(xfer);
1403
1404 dwc2_hcd_endpoint_disable(sc->dwc_dev.hcd, dpipe->priv, 250);
1405 dwc_free(NULL, dpipe->urb);
1406 #endif
1407
1408 mutex_enter(&sc->sc_lock);
1409 if (wk == &hsotg->wf_otg) {
1410 dwc2_conn_id_status_change(wk);
1411 } else if (wk == &hsotg->start_work.work) {
1412 dwc2_hcd_start_func(wk);
1413 } else if (wk == &hsotg->reset_work.work) {
1414 dwc2_hcd_reset_func(wk);
1415 } else {
1416 #if 0
1417 KASSERT(dwork->xfer != NULL);
1418 KASSERT(dxfer->queued == true);
1419
1420 if (!(xfer->hcflags & UXFER_ABORTING)) {
1421 dwc2_start_standard_chain(xfer);
1422 }
1423 dxfer->queued = false;
1424 cv_broadcast(&xfer->hccv);
1425 #endif
1426 }
1427 mutex_exit(&sc->sc_lock);
1428 }
1429
1430 int dwc2_intr(void *p)
1431 {
1432 struct dwc2_softc *sc = p;
1433 struct dwc2_hsotg *hsotg;
1434 int ret = 0;
1435
1436 if (sc == NULL)
1437 return 0;
1438
1439 hsotg = sc->sc_hsotg;
1440 mutex_spin_enter(&hsotg->lock);
1441
1442 if (sc->sc_dying || !device_has_power(sc->sc_dev))
1443 goto done;
1444
1445 if (sc->sc_bus.use_polling) {
1446 uint32_t intrs;
1447
1448 intrs = dwc2_read_core_intr(hsotg);
1449 DWC2_WRITE_4(hsotg, GINTSTS, intrs);
1450 } else {
1451 ret = dwc2_interrupt(sc);
1452 }
1453
1454 done:
1455 mutex_spin_exit(&hsotg->lock);
1456
1457 return ret;
1458 }
1459
1460 int
1461 dwc2_interrupt(struct dwc2_softc *sc)
1462 {
1463 int ret = 0;
1464
1465 if (sc->sc_hcdenabled) {
1466 ret |= dwc2_handle_hcd_intr(sc->sc_hsotg);
1467 }
1468
1469 ret |= dwc2_handle_common_intr(sc->sc_hsotg);
1470
1471 return ret;
1472 }
1473
1474 /***********************************************************************/
1475
1476 int
1477 dwc2_detach(struct dwc2_softc *sc, int flags)
1478 {
1479 int rv = 0;
1480
1481 if (sc->sc_child != NULL)
1482 rv = config_detach(sc->sc_child, flags);
1483
1484 return rv;
1485 }
1486
1487 bool
1488 dwc2_shutdown(device_t self, int flags)
1489 {
1490 struct dwc2_softc *sc = device_private(self);
1491
1492 sc = sc;
1493
1494 return true;
1495 }
1496
1497 void
1498 dwc2_childdet(device_t self, device_t child)
1499 {
1500 struct dwc2_softc *sc = device_private(self);
1501
1502 sc = sc;
1503 }
1504
1505 int
1506 dwc2_activate(device_t self, enum devact act)
1507 {
1508 struct dwc2_softc *sc = device_private(self);
1509
1510 sc = sc;
1511
1512 return 0;
1513 }
1514
1515 bool
1516 dwc2_resume(device_t dv, const pmf_qual_t *qual)
1517 {
1518 struct dwc2_softc *sc = device_private(dv);
1519
1520 sc = sc;
1521
1522 return true;
1523 }
1524
1525 bool
1526 dwc2_suspend(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 /***********************************************************************/
1536 Static int
1537 dwc2_init(struct dwc2_softc *sc)
1538 {
1539 int err = 0;
1540
1541 sc->sc_bus.hci_private = sc;
1542 sc->sc_bus.usbrev = USBREV_2_0;
1543 sc->sc_bus.methods = &dwc2_bus_methods;
1544 sc->sc_bus.pipe_size = sizeof(struct dwc2_pipe);
1545 sc->sc_hcdenabled = false;
1546
1547 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
1548
1549 TAILQ_INIT(&sc->sc_complete);
1550
1551 sc->sc_rhc_si = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
1552 dwc2_rhc, sc);
1553
1554 usb_setup_reserve(sc->sc_dev, &sc->sc_dma_reserve, sc->sc_bus.dmatag,
1555 USB_MEM_RESERVE);
1556
1557 sc->sc_xferpool = pool_cache_init(sizeof(struct dwc2_xfer), 0, 0, 0,
1558 "dwc2xfer", NULL, IPL_USB, NULL, NULL, NULL);
1559 sc->sc_qhpool = pool_cache_init(sizeof(struct dwc2_qh), 0, 0, 0,
1560 "dwc2qh", NULL, IPL_USB, NULL, NULL, NULL);
1561 sc->sc_qtdpool = pool_cache_init(sizeof(struct dwc2_qtd), 0, 0, 0,
1562 "dwc2qtd", NULL, IPL_USB, NULL, NULL, NULL);
1563
1564 sc->sc_hsotg = kmem_zalloc(sizeof(struct dwc2_hsotg), KM_SLEEP);
1565 if (sc->sc_hsotg == NULL) {
1566 err = ENOMEM;
1567 goto fail1;
1568 }
1569
1570 sc->sc_hsotg->hsotg_sc = sc;
1571 sc->sc_hsotg->dev = sc->sc_dev;
1572 sc->sc_hcdenabled = true;
1573
1574 err = dwc2_hcd_init(sc->sc_hsotg, sc->sc_params);
1575 if (err) {
1576 err = -err;
1577 goto fail2;
1578 }
1579
1580 return 0;
1581
1582 fail2:
1583 kmem_free(sc->sc_hsotg, sizeof(struct dwc2_hsotg));
1584 fail1:
1585 softint_disestablish(sc->sc_rhc_si);
1586
1587 return err;
1588 }
1589
1590 #if 0
1591 /*
1592 * curmode is a mode indication bit 0 = device, 1 = host
1593 */
1594 static const char * const intnames[32] = {
1595 "curmode", "modemis", "otgint", "sof",
1596 "rxflvl", "nptxfemp", "ginnakeff", "goutnakeff",
1597 "ulpickint", "i2cint", "erlysusp", "usbsusp",
1598 "usbrst", "enumdone", "isooutdrop", "eopf",
1599 "restore_done", "epmis", "iepint", "oepint",
1600 "incompisoin", "incomplp", "fetsusp", "resetdet",
1601 "prtint", "hchint", "ptxfemp", "lpm",
1602 "conidstschng", "disconnint", "sessreqint", "wkupint"
1603 };
1604
1605
1606 /***********************************************************************/
1607
1608 #endif
1609
1610
1611 void
1612 dw_callout(void *arg)
1613 {
1614 struct delayed_work *dw = arg;
1615
1616 workqueue_enqueue(dw->dw_wq, &dw->work, NULL);
1617 }
1618
1619 void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context, int *hub_addr,
1620 int *hub_port)
1621 {
1622 usbd_xfer_handle xfer = context;
1623 struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
1624 usbd_device_handle dev = dpipe->pipe.device;
1625
1626 *hub_addr = dev->myhsport->parent->address;
1627 *hub_port = dev->myhsport->portno;
1628 }
1629
1630 int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
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 return dev->speed;
1637 }
1638
1639 /*
1640 * Sets the final status of an URB and returns it to the upper layer. Any
1641 * required cleanup of the URB is performed.
1642 *
1643 * Must be called with interrupt disabled and spinlock held
1644 */
1645 void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
1646 int status)
1647 {
1648 usbd_xfer_handle xfer;
1649 struct dwc2_xfer *dxfer;
1650 struct dwc2_softc *sc;
1651 usb_endpoint_descriptor_t *ed;
1652 uint8_t xfertype;
1653
1654 if (!qtd) {
1655 dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
1656 return;
1657 }
1658
1659 if (!qtd->urb) {
1660 dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
1661 return;
1662 }
1663
1664 xfer = qtd->urb->priv;
1665 if (!xfer) {
1666 dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
1667 return;
1668 }
1669
1670 dxfer = DWC2_XFER2DXFER(xfer);
1671 sc = DWC2_XFER2SC(xfer);
1672 ed = xfer->pipe->endpoint->edesc;
1673 xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
1674
1675 xfer->actlen = dwc2_hcd_urb_get_actual_length(qtd->urb);
1676
1677 if (xfertype == UE_ISOCHRONOUS && dbg_perio()) {
1678 int i;
1679
1680 for (i = 0; i < xfer->nframes; i++)
1681 dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
1682 i, qtd->urb->iso_descs[i].status);
1683 }
1684
1685 if (xfertype == UE_ISOCHRONOUS) {
1686 int i;
1687
1688 xfer->actlen = 0;
1689 for (i = 0; i < xfer->nframes; ++i) {
1690 xfer->frlengths[i] =
1691 dwc2_hcd_urb_get_iso_desc_actual_length(
1692 qtd->urb, i);
1693 xfer->actlen += xfer->frlengths[i];
1694 }
1695 }
1696
1697 if (!status) {
1698 if (!(xfer->flags & USBD_SHORT_XFER_OK) &&
1699 xfer->actlen < xfer->length)
1700 status = -EIO;
1701 }
1702
1703 switch (status) {
1704 case 0:
1705 xfer->status = USBD_NORMAL_COMPLETION;
1706 break;
1707 case -EPIPE:
1708 xfer->status = USBD_STALLED;
1709 break;
1710 case -ETIMEDOUT:
1711 xfer->status = USBD_TIMEOUT;
1712 break;
1713 case -EPROTO:
1714 xfer->status = USBD_INVAL;
1715 break;
1716 case -EIO:
1717 xfer->status = USBD_IOERROR;
1718 break;
1719 case -EOVERFLOW:
1720 xfer->status = USBD_IOERROR;
1721 break;
1722 default:
1723 printf("%s: unknown error status %d\n", __func__, status);
1724 }
1725
1726 if (xfertype == UE_ISOCHRONOUS ||
1727 xfertype == UE_INTERRUPT) {
1728 struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
1729
1730 dwc2_free_bus_bandwidth(hsotg,
1731 dwc2_hcd_get_ep_bandwidth(hsotg, dpipe),
1732 xfer);
1733 }
1734
1735 qtd->urb = NULL;
1736 callout_stop(&xfer->timeout_handle);
1737
1738 KASSERT(mutex_owned(&hsotg->lock));
1739
1740 TAILQ_INSERT_TAIL(&sc->sc_complete, dxfer, xnext);
1741
1742 usb_schedsoftintr(&sc->sc_bus);
1743 }
1744
1745
1746 int
1747 _dwc2_hcd_start(struct dwc2_hsotg *hsotg)
1748 {
1749 dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
1750
1751 mutex_spin_enter(&hsotg->lock);
1752
1753 hsotg->op_state = OTG_STATE_A_HOST;
1754
1755 dwc2_hcd_reinit(hsotg);
1756
1757 /*XXXNH*/
1758 delay(50);
1759
1760 mutex_spin_exit(&hsotg->lock);
1761 return 0;
1762 }
1763
1764 int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
1765 {
1766
1767 return false;
1768 }
1769