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