motg.c revision 1.4 1 /* $NetBSD: motg.c,v 1.4 2014/07/25 21:16:31 joerg Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2004, 2011, 2012, 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net) at
9 * Carlstedt Research & Technology, Jared D. McNeill (jmcneill (at) invisible.ca),
10 * Matthew R. Green (mrg (at) eterna.com.au), and Manuel Bouyer (bouyer (at) netbsd.org).
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34
35 /*
36 * This file contains the driver for the Mentor Graphics Inventra USB
37 * 2.0 High Speed Dual-Role controller.
38 *
39 * NOTE: The current implementation only supports Device Side Mode!
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: motg.c,v 1.4 2014/07/25 21:16:31 joerg Exp $");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/kmem.h>
49 #include <sys/device.h>
50 #include <sys/select.h>
51 #include <sys/extent.h>
52 #include <sys/proc.h>
53 #include <sys/queue.h>
54 #include <sys/bus.h>
55 #include <sys/cpu.h>
56
57 #include <machine/endian.h>
58
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdivar.h>
62 #include <dev/usb/usb_mem.h>
63 #include <dev/usb/usb_quirks.h>
64
65 #include <dev/usb/motgreg.h>
66 #include <dev/usb/motgvar.h>
67 #include <dev/usb/usbroothub_subr.h>
68
69 #define MOTG_DEBUG
70 #ifdef MOTG_DEBUG
71 #define DPRINTF(x) if (motgdebug) printf x
72 #define DPRINTFN(n,x) if (motgdebug & (n)) printf x
73 #define MD_ROOT 0x0002
74 #define MD_CTRL 0x0004
75 #define MD_BULK 0x0008
76 // int motgdebug = MD_ROOT | MD_CTRL | MD_BULK;
77 int motgdebug = 0;
78 #else
79 #define DPRINTF(x)
80 #define DPRINTFN(n,x)
81 #endif
82
83 /* various timeouts, for various speeds */
84 /* control NAK timeouts */
85 #define NAK_TO_CTRL 10 /* 1024 frames, about 1s */
86 #define NAK_TO_CTRL_HIGH 13 /* 8k microframes, about 0.8s */
87
88 /* intr/iso polling intervals */
89 #define POLL_TO 100 /* 100 frames, about 0.1s */
90 #define POLL_TO_HIGH 10 /* 100 microframes, about 0.12s */
91
92 /* bulk NAK timeouts */
93 #define NAK_TO_BULK 0 /* disabled */
94 #define NAK_TO_BULK_HIGH 0
95
96 static void motg_hub_change(struct motg_softc *);
97 static usbd_status motg_root_ctrl_transfer(usbd_xfer_handle);
98 static usbd_status motg_root_ctrl_start(usbd_xfer_handle);
99 static void motg_root_ctrl_abort(usbd_xfer_handle);
100 static void motg_root_ctrl_close(usbd_pipe_handle);
101 static void motg_root_ctrl_done(usbd_xfer_handle);
102
103 static usbd_status motg_root_intr_transfer(usbd_xfer_handle);
104 static usbd_status motg_root_intr_start(usbd_xfer_handle);
105 static void motg_root_intr_abort(usbd_xfer_handle);
106 static void motg_root_intr_close(usbd_pipe_handle);
107 static void motg_root_intr_done(usbd_xfer_handle);
108
109 static usbd_status motg_open(usbd_pipe_handle);
110 static void motg_poll(struct usbd_bus *);
111 static void motg_softintr(void *);
112 static usbd_status motg_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
113 static void motg_freem(struct usbd_bus *, usb_dma_t *);
114 static usbd_xfer_handle motg_allocx(struct usbd_bus *);
115 static void motg_freex(struct usbd_bus *, usbd_xfer_handle);
116 static void motg_get_lock(struct usbd_bus *, kmutex_t **);
117 static void motg_noop(usbd_pipe_handle pipe);
118 static usbd_status motg_portreset(struct motg_softc*);
119
120 static usbd_status motg_device_ctrl_transfer(usbd_xfer_handle);
121 static usbd_status motg_device_ctrl_start(usbd_xfer_handle);
122 static void motg_device_ctrl_abort(usbd_xfer_handle);
123 static void motg_device_ctrl_close(usbd_pipe_handle);
124 static void motg_device_ctrl_done(usbd_xfer_handle);
125 static usbd_status motg_device_ctrl_start1(struct motg_softc *);
126 static void motg_device_ctrl_read(usbd_xfer_handle);
127 static void motg_device_ctrl_intr_rx(struct motg_softc *);
128 static void motg_device_ctrl_intr_tx(struct motg_softc *);
129
130 static usbd_status motg_device_data_transfer(usbd_xfer_handle);
131 static usbd_status motg_device_data_start(usbd_xfer_handle);
132 static usbd_status motg_device_data_start1(struct motg_softc *,
133 struct motg_hw_ep *);
134 static void motg_device_data_abort(usbd_xfer_handle);
135 static void motg_device_data_close(usbd_pipe_handle);
136 static void motg_device_data_done(usbd_xfer_handle);
137 static void motg_device_intr_rx(struct motg_softc *, int);
138 static void motg_device_intr_tx(struct motg_softc *, int);
139 static void motg_device_data_read(usbd_xfer_handle);
140 static void motg_device_data_write(usbd_xfer_handle);
141
142 static void motg_waitintr(struct motg_softc *, usbd_xfer_handle);
143 static void motg_device_clear_toggle(usbd_pipe_handle);
144 static void motg_device_xfer_abort(usbd_xfer_handle);
145
146 #define MOTG_INTR_ENDPT 1
147 #define UBARR(sc) bus_space_barrier((sc)->sc_iot, (sc)->sc_ioh, 0, (sc)->sc_size, \
148 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
149 #define UWRITE1(sc, r, x) \
150 do { UBARR(sc); bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, (r), (x)); \
151 } while (/*CONSTCOND*/0)
152 #define UWRITE2(sc, r, x) \
153 do { UBARR(sc); bus_space_write_2((sc)->sc_iot, (sc)->sc_ioh, (r), (x)); \
154 } while (/*CONSTCOND*/0)
155 #define UWRITE4(sc, r, x) \
156 do { UBARR(sc); bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (r), (x)); \
157 } while (/*CONSTCOND*/0)
158
159 static __inline uint32_t
160 UREAD1(struct motg_softc *sc, bus_size_t r)
161 {
162
163 UBARR(sc);
164 return bus_space_read_1(sc->sc_iot, sc->sc_ioh, r);
165 }
166 static __inline uint32_t
167 UREAD2(struct motg_softc *sc, bus_size_t r)
168 {
169
170 UBARR(sc);
171 return bus_space_read_2(sc->sc_iot, sc->sc_ioh, r);
172 }
173
174 #if 0
175 static __inline uint32_t
176 UREAD4(struct motg_softc *sc, bus_size_t r)
177 {
178
179 UBARR(sc);
180 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, r);
181 }
182 #endif
183
184 static void
185 musbotg_pull_common(struct motg_softc *sc, uint8_t on)
186 {
187 uint8_t val;
188
189 val = UREAD1(sc, MUSB2_REG_POWER);
190 if (on)
191 val |= MUSB2_MASK_SOFTC;
192 else
193 val &= ~MUSB2_MASK_SOFTC;
194
195 UWRITE1(sc, MUSB2_REG_POWER, val);
196 }
197
198 const struct usbd_bus_methods motg_bus_methods = {
199 .open_pipe = motg_open,
200 .soft_intr = motg_softintr,
201 .do_poll = motg_poll,
202 .allocm = motg_allocm,
203 .freem = motg_freem,
204 .allocx = motg_allocx,
205 .freex = motg_freex,
206 .get_lock = motg_get_lock,
207 .new_device = NULL,
208 };
209
210 const struct usbd_pipe_methods motg_root_ctrl_methods = {
211 .transfer = motg_root_ctrl_transfer,
212 .start = motg_root_ctrl_start,
213 .abort = motg_root_ctrl_abort,
214 .close = motg_root_ctrl_close,
215 .cleartoggle = motg_noop,
216 .done = motg_root_ctrl_done,
217 };
218
219 const struct usbd_pipe_methods motg_root_intr_methods = {
220 .transfer = motg_root_intr_transfer,
221 .start = motg_root_intr_start,
222 .abort = motg_root_intr_abort,
223 .close = motg_root_intr_close,
224 .cleartoggle = motg_noop,
225 .done = motg_root_intr_done,
226 };
227
228 const struct usbd_pipe_methods motg_device_ctrl_methods = {
229 .transfer = motg_device_ctrl_transfer,
230 .start = motg_device_ctrl_start,
231 .abort = motg_device_ctrl_abort,
232 .close = motg_device_ctrl_close,
233 .cleartoggle = motg_noop,
234 .done = motg_device_ctrl_done,
235 };
236
237 const struct usbd_pipe_methods motg_device_data_methods = {
238 .transfer = motg_device_data_transfer,
239 .start = motg_device_data_start,
240 .abort = motg_device_data_abort,
241 .close = motg_device_data_close,
242 .cleartoggle = motg_device_clear_toggle,
243 .done = motg_device_data_done,
244 };
245
246 usbd_status
247 motg_init(struct motg_softc *sc)
248 {
249 uint32_t nrx, ntx, val;
250 int dynfifo;
251 int offset, i;
252
253 if (sc->sc_mode == MOTG_MODE_DEVICE)
254 return USBD_NORMAL_COMPLETION; /* not supported */
255
256 /* disable all interrupts */
257 UWRITE1(sc, MUSB2_REG_INTUSBE, 0);
258 UWRITE2(sc, MUSB2_REG_INTTXE, 0);
259 UWRITE2(sc, MUSB2_REG_INTRXE, 0);
260 /* disable pullup */
261
262 musbotg_pull_common(sc, 0);
263
264 /* disable double packet buffering XXX what's this ? */
265 UWRITE2(sc, MUSB2_REG_RXDBDIS, 0xFFFF);
266 UWRITE2(sc, MUSB2_REG_TXDBDIS, 0xFFFF);
267
268 /* enable HighSpeed and ISO Update flags */
269
270 UWRITE1(sc, MUSB2_REG_POWER,
271 MUSB2_MASK_HSENAB | MUSB2_MASK_ISOUPD);
272
273 if (sc->sc_mode == MOTG_MODE_DEVICE) {
274 /* clear Session bit, if set */
275 val = UREAD1(sc, MUSB2_REG_DEVCTL);
276 val &= ~MUSB2_MASK_SESS;
277 UWRITE1(sc, MUSB2_REG_DEVCTL, val);
278 } else {
279 /* Enter session for Host mode */
280 val = UREAD1(sc, MUSB2_REG_DEVCTL);
281 val |= MUSB2_MASK_SESS;
282 UWRITE1(sc, MUSB2_REG_DEVCTL, val);
283 }
284 delay(1000);
285 DPRINTF(("DEVCTL 0x%x\n", UREAD1(sc, MUSB2_REG_DEVCTL)));
286
287 /* disable testmode */
288
289 UWRITE1(sc, MUSB2_REG_TESTMODE, 0);
290
291 /* set default value */
292
293 UWRITE1(sc, MUSB2_REG_MISC, 0);
294
295 /* select endpoint index 0 */
296
297 UWRITE1(sc, MUSB2_REG_EPINDEX, 0);
298
299 /* read out number of endpoints */
300 nrx = (UREAD1(sc, MUSB2_REG_EPINFO) / 16);
301
302 ntx = (UREAD1(sc, MUSB2_REG_EPINFO) % 16);
303
304 /* these numbers exclude the control endpoint */
305
306 DPRINTF(("RX/TX endpoints: %u/%u\n", nrx, ntx));
307
308 sc->sc_ep_max = MAX(nrx, ntx);
309 if (sc->sc_ep_max == 0) {
310 aprint_error_dev(sc->sc_dev, " no endpoints\n");
311 return USBD_INVAL;
312 }
313 KASSERT(sc->sc_ep_max <= MOTG_MAX_HW_EP);
314 /* read out configuration data */
315 val = UREAD1(sc, MUSB2_REG_CONFDATA);
316
317 DPRINTF(("Config Data: 0x%02x\n", val));
318
319 dynfifo = (val & MUSB2_MASK_CD_DYNFIFOSZ) ? 1 : 0;
320
321 if (dynfifo) {
322 aprint_normal_dev(sc->sc_dev, "Dynamic FIFO sizing detected, "
323 "assuming 16Kbytes of FIFO RAM\n");
324 }
325
326 DPRINTF(("HW version: 0x%04x\n", UREAD1(sc, MUSB2_REG_HWVERS)));
327
328 /* initialise endpoint profiles */
329 sc->sc_in_ep[0].ep_fifo_size = 64;
330 sc->sc_out_ep[0].ep_fifo_size = 0; /* not used */
331 sc->sc_out_ep[0].ep_number = sc->sc_in_ep[0].ep_number = 0;
332 SIMPLEQ_INIT(&sc->sc_in_ep[0].ep_pipes);
333 offset = 64;
334
335 for (i = 1; i <= sc->sc_ep_max; i++) {
336 int fiforx_size, fifotx_size, fifo_size;
337
338 /* select endpoint */
339 UWRITE1(sc, MUSB2_REG_EPINDEX, i);
340
341 val = UREAD1(sc, MUSB2_REG_FSIZE);
342 fiforx_size = (val & MUSB2_MASK_RX_FSIZE) >> 4;
343 fifotx_size = (val & MUSB2_MASK_TX_FSIZE);
344
345 DPRINTF(("Endpoint %u FIFO size: IN=%u, OUT=%u, DYN=%d\n",
346 i, fifotx_size, fiforx_size, dynfifo));
347
348 if (dynfifo) {
349 if (i < 3) {
350 fifo_size = 12; /* 4K */
351 } else if (i < 10) {
352 fifo_size = 10; /* 1K */
353 } else {
354 fifo_size = 7; /* 128 bytes */
355 }
356 if (fiforx_size && (i <= nrx)) {
357 fiforx_size = fifo_size;
358 if (fifo_size > 7) {
359 #if 0
360 UWRITE1(sc, MUSB2_REG_RXFIFOSZ,
361 MUSB2_VAL_FIFOSZ(fifo_size) |
362 MUSB2_MASK_FIFODB);
363 #else
364 UWRITE1(sc, MUSB2_REG_RXFIFOSZ,
365 MUSB2_VAL_FIFOSZ(fifo_size));
366 #endif
367 } else {
368 UWRITE1(sc, MUSB2_REG_RXFIFOSZ,
369 MUSB2_VAL_FIFOSZ(fifo_size));
370 }
371 UWRITE2(sc, MUSB2_REG_RXFIFOADD,
372 offset >> 3);
373 offset += (1 << fiforx_size);
374 }
375 if (fifotx_size && (i <= ntx)) {
376 fifotx_size = fifo_size;
377 if (fifo_size > 7) {
378 #if 0
379 UWRITE1(sc, MUSB2_REG_TXFIFOSZ,
380 MUSB2_VAL_FIFOSZ(fifo_size) |
381 MUSB2_MASK_FIFODB);
382 #else
383 UWRITE1(sc, MUSB2_REG_TXFIFOSZ,
384 MUSB2_VAL_FIFOSZ(fifo_size));
385 #endif
386 } else {
387 UWRITE1(sc, MUSB2_REG_TXFIFOSZ,
388 MUSB2_VAL_FIFOSZ(fifo_size));
389 }
390
391 UWRITE2(sc, MUSB2_REG_TXFIFOADD,
392 offset >> 3);
393
394 offset += (1 << fifotx_size);
395 }
396 }
397 if (fiforx_size && (i <= nrx)) {
398 sc->sc_in_ep[i].ep_fifo_size = (1 << fiforx_size);
399 SIMPLEQ_INIT(&sc->sc_in_ep[i].ep_pipes);
400 }
401 if (fifotx_size && (i <= ntx)) {
402 sc->sc_out_ep[i].ep_fifo_size = (1 << fifotx_size);
403 SIMPLEQ_INIT(&sc->sc_out_ep[i].ep_pipes);
404 }
405 sc->sc_out_ep[i].ep_number = sc->sc_in_ep[i].ep_number = i;
406 }
407
408
409 DPRINTF(("Dynamic FIFO size = %d bytes\n", offset));
410
411 /* turn on default interrupts */
412
413 if (sc->sc_mode == MOTG_MODE_HOST) {
414 UWRITE1(sc, MUSB2_REG_INTUSBE, 0xff);
415 UWRITE2(sc, MUSB2_REG_INTTXE, 0xffff);
416 UWRITE2(sc, MUSB2_REG_INTRXE, 0xffff);
417 } else
418 UWRITE1(sc, MUSB2_REG_INTUSBE, MUSB2_MASK_IRESET);
419
420 sc->sc_xferpool = pool_cache_init(sizeof(struct motg_xfer), 0, 0, 0,
421 "motgxfer", NULL, IPL_USB, NULL, NULL, NULL);
422
423 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
424 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
425
426 /* Set up the bus struct. */
427 sc->sc_bus.methods = &motg_bus_methods;
428 sc->sc_bus.pipe_size = sizeof(struct motg_pipe);
429 sc->sc_bus.usbrev = USBREV_2_0;
430 sc->sc_bus.hci_private = sc;
431 snprintf(sc->sc_vendor, sizeof(sc->sc_vendor),
432 "Mentor Graphics");
433 sc->sc_child = config_found(sc->sc_dev, &sc->sc_bus, usbctlprint);
434 return USBD_NORMAL_COMPLETION;
435 }
436
437 static int
438 motg_select_ep(struct motg_softc *sc, usbd_pipe_handle pipe)
439 {
440 struct motg_pipe *otgpipe = (struct motg_pipe *)pipe;
441 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
442 struct motg_hw_ep *ep;
443 int i, size;
444
445 ep = (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) ?
446 sc->sc_in_ep : sc->sc_out_ep;
447 size = UE_GET_SIZE(UGETW(pipe->endpoint->edesc->wMaxPacketSize));
448
449 for (i = sc->sc_ep_max; i >= 1; i--) {
450 DPRINTF(("%s_ep[%d].ep_fifo_size %d size %d ref %d\n",
451 (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) ?
452 "in" : "out", i, ep[i].ep_fifo_size, size, ep[i].refcount));
453 if (ep[i].ep_fifo_size >= size) {
454 /* found a suitable endpoint */
455 otgpipe->hw_ep = &ep[i];
456 mutex_enter(&sc->sc_lock);
457 if (otgpipe->hw_ep->refcount > 0) {
458 /* no luck, try next */
459 mutex_exit(&sc->sc_lock);
460 otgpipe->hw_ep = NULL;
461 } else {
462 otgpipe->hw_ep->refcount++;
463 SIMPLEQ_INSERT_TAIL(&otgpipe->hw_ep->ep_pipes,
464 otgpipe, ep_pipe_list);
465 mutex_exit(&sc->sc_lock);
466 return 0;
467 }
468 }
469 }
470 return -1;
471 }
472
473 /* Open a new pipe. */
474 usbd_status
475 motg_open(usbd_pipe_handle pipe)
476 {
477 struct motg_softc *sc = pipe->device->bus->hci_private;
478 struct motg_pipe *otgpipe = (struct motg_pipe *)pipe;
479 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
480
481 DPRINTF(("motg_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
482 pipe, pipe->device->address,
483 ed->bEndpointAddress, sc->sc_root_addr));
484
485 if (sc->sc_dying)
486 return USBD_IOERROR;
487
488 /* toggle state needed for bulk endpoints */
489 otgpipe->nexttoggle = pipe->endpoint->datatoggle;
490
491 if (pipe->device->address == sc->sc_root_addr) {
492 switch (ed->bEndpointAddress) {
493 case USB_CONTROL_ENDPOINT:
494 pipe->methods = &motg_root_ctrl_methods;
495 break;
496 case UE_DIR_IN | MOTG_INTR_ENDPT:
497 pipe->methods = &motg_root_intr_methods;
498 break;
499 default:
500 return (USBD_INVAL);
501 }
502 } else {
503 switch (ed->bmAttributes & UE_XFERTYPE) {
504 case UE_CONTROL:
505 pipe->methods = &motg_device_ctrl_methods;
506 /* always use sc_in_ep[0] for in and out */
507 otgpipe->hw_ep = &sc->sc_in_ep[0];
508 mutex_enter(&sc->sc_lock);
509 otgpipe->hw_ep->refcount++;
510 SIMPLEQ_INSERT_TAIL(&otgpipe->hw_ep->ep_pipes,
511 otgpipe, ep_pipe_list);
512 mutex_exit(&sc->sc_lock);
513 break;
514 case UE_BULK:
515 case UE_INTERRUPT:
516 DPRINTFN(MD_BULK,
517 ("new %s %s pipe wMaxPacketSize %d\n",
518 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK ?
519 "bulk" : "interrupt",
520 (UE_GET_DIR(pipe->endpoint->edesc->bEndpointAddress) == UE_DIR_IN) ? "read" : "write",
521 UGETW(pipe->endpoint->edesc->wMaxPacketSize)));
522 if (motg_select_ep(sc, pipe) != 0)
523 goto bad;
524 KASSERT(otgpipe->hw_ep != NULL);
525 pipe->methods = &motg_device_data_methods;
526 otgpipe->nexttoggle = pipe->endpoint->datatoggle;
527 break;
528 default:
529 goto bad;
530 #ifdef notyet
531 case UE_ISOCHRONOUS:
532 ...
533 break;
534 #endif /* notyet */
535 }
536 }
537 return (USBD_NORMAL_COMPLETION);
538
539 bad:
540 return (USBD_NOMEM);
541 }
542
543 void
544 motg_softintr(void *v)
545 {
546 struct usbd_bus *bus = v;
547 struct motg_softc *sc = bus->hci_private;
548 uint16_t rx_status, tx_status;
549 uint8_t ctrl_status;
550 uint32_t val;
551 int i;
552
553 KASSERT(sc->sc_bus.use_polling || mutex_owned(&sc->sc_lock));
554
555 DPRINTFN(MD_ROOT | MD_CTRL,
556 ("%s: motg_softintr\n", device_xname(sc->sc_dev)));
557
558 mutex_spin_enter(&sc->sc_intr_lock);
559 rx_status = sc->sc_intr_rx_ep;
560 sc->sc_intr_rx_ep = 0;
561 tx_status = sc->sc_intr_tx_ep;
562 sc->sc_intr_tx_ep = 0;
563 ctrl_status = sc->sc_intr_ctrl;
564 sc->sc_intr_ctrl = 0;
565 mutex_spin_exit(&sc->sc_intr_lock);
566
567 ctrl_status |= UREAD1(sc, MUSB2_REG_INTUSB);
568
569 if (ctrl_status & (MUSB2_MASK_IRESET |
570 MUSB2_MASK_IRESUME | MUSB2_MASK_ISUSP |
571 MUSB2_MASK_ICONN | MUSB2_MASK_IDISC)) {
572 DPRINTFN(MD_ROOT | MD_CTRL, ("motg_softintr bus 0x%x\n",
573 ctrl_status));
574
575 if (ctrl_status & MUSB2_MASK_IRESET) {
576 sc->sc_isreset = 1;
577 sc->sc_port_suspended = 0;
578 sc->sc_port_suspended_change = 1;
579 sc->sc_connected_changed = 1;
580 sc->sc_port_enabled = 1;
581
582 val = UREAD1(sc, MUSB2_REG_POWER);
583 if (val & MUSB2_MASK_HSMODE)
584 sc->sc_high_speed = 1;
585 else
586 sc->sc_high_speed = 0;
587 DPRINTFN(MD_ROOT | MD_CTRL, ("motg_softintr speed %d\n",
588 sc->sc_high_speed));
589
590 /* turn off interrupts */
591 val = MUSB2_MASK_IRESET;
592 val &= ~MUSB2_MASK_IRESUME;
593 val |= MUSB2_MASK_ISUSP;
594 UWRITE1(sc, MUSB2_REG_INTUSBE, val);
595 UWRITE2(sc, MUSB2_REG_INTTXE, 0);
596 UWRITE2(sc, MUSB2_REG_INTRXE, 0);
597 }
598 if (ctrl_status & MUSB2_MASK_IRESUME) {
599 if (sc->sc_port_suspended) {
600 sc->sc_port_suspended = 0;
601 sc->sc_port_suspended_change = 1;
602 val = UREAD1(sc, MUSB2_REG_INTUSBE);
603 /* disable resume interrupt */
604 val &= ~MUSB2_MASK_IRESUME;
605 /* enable suspend interrupt */
606 val |= MUSB2_MASK_ISUSP;
607 UWRITE1(sc, MUSB2_REG_INTUSBE, val);
608 }
609 } else if (ctrl_status & MUSB2_MASK_ISUSP) {
610 if (!sc->sc_port_suspended) {
611 sc->sc_port_suspended = 1;
612 sc->sc_port_suspended_change = 1;
613
614 val = UREAD1(sc, MUSB2_REG_INTUSBE);
615 /* disable suspend interrupt */
616 val &= ~MUSB2_MASK_ISUSP;
617 /* enable resume interrupt */
618 val |= MUSB2_MASK_IRESUME;
619 UWRITE1(sc, MUSB2_REG_INTUSBE, val);
620 }
621 }
622 if (ctrl_status & MUSB2_MASK_ICONN) {
623 sc->sc_connected = 1;
624 sc->sc_connected_changed = 1;
625 sc->sc_isreset = 1;
626 sc->sc_port_enabled = 1;
627 } else if (ctrl_status & MUSB2_MASK_IDISC) {
628 sc->sc_connected = 0;
629 sc->sc_connected_changed = 1;
630 sc->sc_isreset = 0;
631 sc->sc_port_enabled = 0;
632 }
633
634 /* complete root HUB interrupt endpoint */
635
636 motg_hub_change(sc);
637 }
638 /*
639 * read in interrupt status and mix with the status we
640 * got from the wrapper
641 */
642 rx_status |= UREAD2(sc, MUSB2_REG_INTRX);
643 tx_status |= UREAD2(sc, MUSB2_REG_INTTX);
644
645 if (rx_status & 0x01)
646 panic("ctrl_rx");
647 if (tx_status & 0x01)
648 motg_device_ctrl_intr_tx(sc);
649 for (i = 1; i <= sc->sc_ep_max; i++) {
650 if (rx_status & (0x01 << i))
651 motg_device_intr_rx(sc, i);
652 if (tx_status & (0x01 << i))
653 motg_device_intr_tx(sc, i);
654 }
655 return;
656 }
657
658 void
659 motg_poll(struct usbd_bus *bus)
660 {
661 struct motg_softc *sc = bus->hci_private;
662
663 sc->sc_intr_poll(sc->sc_intr_poll_arg);
664 mutex_enter(&sc->sc_lock);
665 motg_softintr(bus);
666 mutex_exit(&sc->sc_lock);
667 }
668
669 int
670 motg_intr(struct motg_softc *sc, uint16_t rx_ep, uint16_t tx_ep,
671 uint8_t ctrl)
672 {
673 KASSERT(mutex_owned(&sc->sc_intr_lock));
674 sc->sc_intr_tx_ep = tx_ep;
675 sc->sc_intr_rx_ep = rx_ep;
676 sc->sc_intr_ctrl = ctrl;
677
678 if (!sc->sc_bus.use_polling) {
679 sc->sc_bus.no_intrs++;
680 usb_schedsoftintr(&sc->sc_bus);
681 }
682 return 1;
683 }
684
685 int
686 motg_intr_vbus(struct motg_softc *sc, int vbus)
687 {
688 uint8_t val;
689 if (sc->sc_mode == MOTG_MODE_HOST && vbus == 0) {
690 DPRINTF(("motg_intr_vbus: vbus down, try to re-enable\n"));
691 /* try to re-enter session for Host mode */
692 val = UREAD1(sc, MUSB2_REG_DEVCTL);
693 val |= MUSB2_MASK_SESS;
694 UWRITE1(sc, MUSB2_REG_DEVCTL, val);
695 }
696 return 1;
697 }
698
699 usbd_status
700 motg_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
701 {
702 struct motg_softc *sc = bus->hci_private;
703 usbd_status status;
704
705 status = usb_allocmem(&sc->sc_bus, size, 0, dma);
706 if (status == USBD_NOMEM)
707 status = usb_reserve_allocm(&sc->sc_dma_reserve, dma, size);
708 return status;
709 }
710
711 void
712 motg_freem(struct usbd_bus *bus, usb_dma_t *dma)
713 {
714 if (dma->block->flags & USB_DMA_RESERVE) {
715 usb_reserve_freem(&((struct motg_softc *)bus)->sc_dma_reserve,
716 dma);
717 return;
718 }
719 usb_freemem(&((struct motg_softc *)bus)->sc_bus, dma);
720 }
721
722 usbd_xfer_handle
723 motg_allocx(struct usbd_bus *bus)
724 {
725 struct motg_softc *sc = bus->hci_private;
726 usbd_xfer_handle xfer;
727
728 xfer = pool_cache_get(sc->sc_xferpool, PR_NOWAIT);
729 if (xfer != NULL) {
730 memset(xfer, 0, sizeof(struct motg_xfer));
731 UXFER(xfer)->sc = sc;
732 #ifdef DIAGNOSTIC
733 // XXX UXFER(xfer)->iinfo.isdone = 1;
734 xfer->busy_free = XFER_BUSY;
735 #endif
736 }
737 return (xfer);
738 }
739
740 void
741 motg_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
742 {
743 struct motg_softc *sc = bus->hci_private;
744
745 #ifdef DIAGNOSTIC
746 if (xfer->busy_free != XFER_BUSY) {
747 printf("motg_freex: xfer=%p not busy, 0x%08x\n", xfer,
748 xfer->busy_free);
749 }
750 xfer->busy_free = XFER_FREE;
751 #endif
752 pool_cache_put(sc->sc_xferpool, xfer);
753 }
754
755 static void
756 motg_get_lock(struct usbd_bus *bus, kmutex_t **lock)
757 {
758 struct motg_softc *sc = bus->hci_private;
759
760 *lock = &sc->sc_lock;
761 }
762
763 /*
764 * Data structures and routines to emulate the root hub.
765 */
766 usb_device_descriptor_t motg_devd = {
767 USB_DEVICE_DESCRIPTOR_SIZE,
768 UDESC_DEVICE, /* type */
769 {0x00, 0x01}, /* USB version */
770 UDCLASS_HUB, /* class */
771 UDSUBCLASS_HUB, /* subclass */
772 UDPROTO_FSHUB, /* protocol */
773 64, /* max packet */
774 {0},{0},{0x00,0x01}, /* device id */
775 1,2,0, /* string indicies */
776 1 /* # of configurations */
777 };
778
779 const usb_config_descriptor_t motg_confd = {
780 USB_CONFIG_DESCRIPTOR_SIZE,
781 UDESC_CONFIG,
782 {USB_CONFIG_DESCRIPTOR_SIZE +
783 USB_INTERFACE_DESCRIPTOR_SIZE +
784 USB_ENDPOINT_DESCRIPTOR_SIZE},
785 1,
786 1,
787 0,
788 UC_ATTR_MBO | UC_SELF_POWERED,
789 0 /* max power */
790 };
791
792 const usb_interface_descriptor_t motg_ifcd = {
793 USB_INTERFACE_DESCRIPTOR_SIZE,
794 UDESC_INTERFACE,
795 0,
796 0,
797 1,
798 UICLASS_HUB,
799 UISUBCLASS_HUB,
800 UIPROTO_FSHUB,
801 0
802 };
803
804 const usb_endpoint_descriptor_t motg_endpd = {
805 USB_ENDPOINT_DESCRIPTOR_SIZE,
806 UDESC_ENDPOINT,
807 UE_DIR_IN | MOTG_INTR_ENDPT,
808 UE_INTERRUPT,
809 {8},
810 255
811 };
812
813 const usb_hub_descriptor_t motg_hubd = {
814 USB_HUB_DESCRIPTOR_SIZE,
815 UDESC_HUB,
816 1,
817 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
818 50, /* power on to power good */
819 0,
820 { 0x00 }, /* port is removable */
821 { 0 },
822 };
823
824 /*
825 * Simulate a hardware hub by handling all the necessary requests.
826 */
827 usbd_status
828 motg_root_ctrl_transfer(usbd_xfer_handle xfer)
829 {
830 struct motg_softc *sc = xfer->pipe->device->bus->hci_private;
831 usbd_status err;
832
833 /* Insert last in queue. */
834 mutex_enter(&sc->sc_lock);
835 err = usb_insert_transfer(xfer);
836 mutex_exit(&sc->sc_lock);
837 if (err)
838 return (err);
839
840 /*
841 * Pipe isn't running (otherwise err would be USBD_INPROG),
842 * so start it first.
843 */
844 return (motg_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
845 }
846
847 usbd_status
848 motg_root_ctrl_start(usbd_xfer_handle xfer)
849 {
850 struct motg_softc *sc = xfer->pipe->device->bus->hci_private;
851 usb_device_request_t *req;
852 void *buf = NULL;
853 int len, value, index, status, change, l, totlen = 0;
854 usb_port_status_t ps;
855 usbd_status err;
856 uint32_t val;
857
858 if (sc->sc_dying)
859 return (USBD_IOERROR);
860
861 #ifdef DIAGNOSTIC
862 if (!(xfer->rqflags & URQ_REQUEST))
863 panic("motg_root_ctrl_start: not a request");
864 #endif
865 req = &xfer->request;
866
867 DPRINTFN(MD_ROOT,("motg_root_ctrl_control type=0x%02x request=%02x\n",
868 req->bmRequestType, req->bRequest));
869
870 len = UGETW(req->wLength);
871 value = UGETW(req->wValue);
872 index = UGETW(req->wIndex);
873
874 if (len != 0)
875 buf = KERNADDR(&xfer->dmabuf, 0);
876
877 #define C(x,y) ((x) | ((y) << 8))
878 switch(C(req->bRequest, req->bmRequestType)) {
879 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
880 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
881 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
882 /*
883 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
884 * for the integrated root hub.
885 */
886 break;
887 case C(UR_GET_CONFIG, UT_READ_DEVICE):
888 if (len > 0) {
889 *(u_int8_t *)buf = sc->sc_root_conf;
890 totlen = 1;
891 }
892 break;
893 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
894 DPRINTFN(MD_ROOT,("motg_root_ctrl_control wValue=0x%04x\n", value));
895 if (len == 0)
896 break;
897 switch(value >> 8) {
898 case UDESC_DEVICE:
899 if ((value & 0xff) != 0) {
900 err = USBD_IOERROR;
901 goto ret;
902 }
903 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
904 USETW(motg_devd.idVendor, sc->sc_id_vendor);
905 memcpy(buf, &motg_devd, l);
906 break;
907 case UDESC_CONFIG:
908 if ((value & 0xff) != 0) {
909 err = USBD_IOERROR;
910 goto ret;
911 }
912 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
913 memcpy(buf, &motg_confd, l);
914 buf = (char *)buf + l;
915 len -= l;
916 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
917 totlen += l;
918 memcpy(buf, &motg_ifcd, l);
919 buf = (char *)buf + l;
920 len -= l;
921 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
922 totlen += l;
923 memcpy(buf, &motg_endpd, l);
924 break;
925 case UDESC_STRING:
926 #define sd ((usb_string_descriptor_t *)buf)
927 switch (value & 0xff) {
928 case 0: /* Language table */
929 totlen = usb_makelangtbl(sd, len);
930 break;
931 case 1: /* Vendor */
932 totlen = usb_makestrdesc(sd, len,
933 sc->sc_vendor);
934 break;
935 case 2: /* Product */
936 totlen = usb_makestrdesc(sd, len,
937 "MOTG root hub");
938 break;
939 }
940 #undef sd
941 break;
942 default:
943 err = USBD_IOERROR;
944 goto ret;
945 }
946 break;
947 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
948 if (len > 0) {
949 *(u_int8_t *)buf = 0;
950 totlen = 1;
951 }
952 break;
953 case C(UR_GET_STATUS, UT_READ_DEVICE):
954 if (len > 1) {
955 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
956 totlen = 2;
957 }
958 break;
959 case C(UR_GET_STATUS, UT_READ_INTERFACE):
960 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
961 if (len > 1) {
962 USETW(((usb_status_t *)buf)->wStatus, 0);
963 totlen = 2;
964 }
965 break;
966 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
967 if (value >= USB_MAX_DEVICES) {
968 err = USBD_IOERROR;
969 goto ret;
970 }
971 sc->sc_root_addr = value;
972 break;
973 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
974 if (value != 0 && value != 1) {
975 err = USBD_IOERROR;
976 goto ret;
977 }
978 sc->sc_root_conf = value;
979 break;
980 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
981 break;
982 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
983 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
984 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
985 err = USBD_IOERROR;
986 goto ret;
987 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
988 break;
989 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
990 break;
991 /* Hub requests */
992 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
993 break;
994 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
995 DPRINTFN(MD_ROOT,
996 ("motg_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
997 "port=%d feature=%d\n",
998 index, value));
999 if (index != 1) {
1000 err = USBD_IOERROR;
1001 goto ret;
1002 }
1003 switch(value) {
1004 case UHF_PORT_ENABLE:
1005 sc->sc_port_enabled = 0;
1006 break;
1007 case UHF_PORT_SUSPEND:
1008 if (sc->sc_port_suspended != 0) {
1009 val = UREAD1(sc, MUSB2_REG_POWER);
1010 val &= ~MUSB2_MASK_SUSPMODE;
1011 val |= MUSB2_MASK_RESUME;
1012 UWRITE1(sc, MUSB2_REG_POWER, val);
1013 /* wait 20 milliseconds */
1014 usb_delay_ms(&sc->sc_bus, 20);
1015 val = UREAD1(sc, MUSB2_REG_POWER);
1016 val &= ~MUSB2_MASK_RESUME;
1017 UWRITE1(sc, MUSB2_REG_POWER, val);
1018 sc->sc_port_suspended = 0;
1019 sc->sc_port_suspended_change = 1;
1020 }
1021 break;
1022 case UHF_PORT_RESET:
1023 break;
1024 case UHF_C_PORT_CONNECTION:
1025 break;
1026 case UHF_C_PORT_ENABLE:
1027 break;
1028 case UHF_C_PORT_OVER_CURRENT:
1029 break;
1030 case UHF_C_PORT_RESET:
1031 sc->sc_isreset = 0;
1032 err = USBD_NORMAL_COMPLETION;
1033 goto ret;
1034 case UHF_PORT_POWER:
1035 /* XXX todo */
1036 break;
1037 case UHF_PORT_CONNECTION:
1038 case UHF_PORT_OVER_CURRENT:
1039 case UHF_PORT_LOW_SPEED:
1040 case UHF_C_PORT_SUSPEND:
1041 default:
1042 err = USBD_IOERROR;
1043 goto ret;
1044 }
1045 break;
1046 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
1047 err = USBD_IOERROR;
1048 goto ret;
1049 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
1050 if (len == 0)
1051 break;
1052 if ((value & 0xff) != 0) {
1053 err = USBD_IOERROR;
1054 goto ret;
1055 }
1056 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
1057 totlen = l;
1058 memcpy(buf, &motg_hubd, l);
1059 break;
1060 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
1061 if (len != 4) {
1062 err = USBD_IOERROR;
1063 goto ret;
1064 }
1065 memset(buf, 0, len);
1066 totlen = len;
1067 break;
1068 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
1069 if (index != 1) {
1070 err = USBD_IOERROR;
1071 goto ret;
1072 }
1073 if (len != 4) {
1074 err = USBD_IOERROR;
1075 goto ret;
1076 }
1077 status = change = 0;
1078 if (sc->sc_connected)
1079 status |= UPS_CURRENT_CONNECT_STATUS;
1080 if (sc->sc_connected_changed) {
1081 change |= UPS_C_CONNECT_STATUS;
1082 sc->sc_connected_changed = 0;
1083 }
1084 if (sc->sc_port_enabled)
1085 status |= UPS_PORT_ENABLED;
1086 if (sc->sc_port_enabled_changed) {
1087 change |= UPS_C_PORT_ENABLED;
1088 sc->sc_port_enabled_changed = 0;
1089 }
1090 if (sc->sc_port_suspended)
1091 status |= UPS_SUSPEND;
1092 if (sc->sc_high_speed)
1093 status |= UPS_HIGH_SPEED;
1094 status |= UPS_PORT_POWER; /* XXX */
1095 if (sc->sc_isreset)
1096 change |= UPS_C_PORT_RESET;
1097 USETW(ps.wPortStatus, status);
1098 USETW(ps.wPortChange, change);
1099 l = min(len, sizeof ps);
1100 memcpy(buf, &ps, l);
1101 totlen = l;
1102 break;
1103 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
1104 err = USBD_IOERROR;
1105 goto ret;
1106 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
1107 break;
1108 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
1109 if (index != 1) {
1110 err = USBD_IOERROR;
1111 goto ret;
1112 }
1113 switch(value) {
1114 case UHF_PORT_ENABLE:
1115 sc->sc_port_enabled = 1;
1116 break;
1117 case UHF_PORT_SUSPEND:
1118 if (sc->sc_port_suspended == 0) {
1119 val = UREAD1(sc, MUSB2_REG_POWER);
1120 val |= MUSB2_MASK_SUSPMODE;
1121 UWRITE1(sc, MUSB2_REG_POWER, val);
1122 /* wait 20 milliseconds */
1123 usb_delay_ms(&sc->sc_bus, 20);
1124 sc->sc_port_suspended = 1;
1125 sc->sc_port_suspended_change = 1;
1126 }
1127 break;
1128 case UHF_PORT_RESET:
1129 err = motg_portreset(sc);
1130 goto ret;
1131 case UHF_PORT_POWER:
1132 /* XXX todo */
1133 err = USBD_NORMAL_COMPLETION;
1134 goto ret;
1135 case UHF_C_PORT_CONNECTION:
1136 case UHF_C_PORT_ENABLE:
1137 case UHF_C_PORT_OVER_CURRENT:
1138 case UHF_PORT_CONNECTION:
1139 case UHF_PORT_OVER_CURRENT:
1140 case UHF_PORT_LOW_SPEED:
1141 case UHF_C_PORT_SUSPEND:
1142 case UHF_C_PORT_RESET:
1143 default:
1144 err = USBD_IOERROR;
1145 goto ret;
1146 }
1147 break;
1148 default:
1149 err = USBD_IOERROR;
1150 goto ret;
1151 }
1152 xfer->actlen = totlen;
1153 err = USBD_NORMAL_COMPLETION;
1154 ret:
1155 xfer->status = err;
1156 mutex_enter(&sc->sc_lock);
1157 usb_transfer_complete(xfer);
1158 mutex_exit(&sc->sc_lock);
1159 return (USBD_IN_PROGRESS);
1160 }
1161
1162 /* Abort a root control request. */
1163 void
1164 motg_root_ctrl_abort(usbd_xfer_handle xfer)
1165 {
1166 /* Nothing to do, all transfers are synchronous. */
1167 }
1168
1169 /* Close the root pipe. */
1170 void
1171 motg_root_ctrl_close(usbd_pipe_handle pipe)
1172 {
1173 DPRINTFN(MD_ROOT, ("motg_root_ctrl_close\n"));
1174 }
1175
1176 void
1177 motg_root_ctrl_done(usbd_xfer_handle xfer)
1178 {
1179 }
1180
1181 /* Abort a root interrupt request. */
1182 void
1183 motg_root_intr_abort(usbd_xfer_handle xfer)
1184 {
1185 struct motg_softc *sc = xfer->pipe->device->bus->hci_private;
1186
1187 KASSERT(mutex_owned(&sc->sc_lock));
1188
1189 sc->sc_intr_xfer = NULL;
1190
1191 if (xfer->pipe->intrxfer == xfer) {
1192 DPRINTFN(MD_ROOT, ("motg_root_intr_abort: remove\n"));
1193 xfer->pipe->intrxfer = 0;
1194 }
1195 xfer->status = USBD_CANCELLED;
1196 #ifdef DIAGNOSTIC
1197 // XXX UXFER(xfer)->iinfo.isdone = 1;
1198 #endif
1199 usb_transfer_complete(xfer);
1200 }
1201
1202 usbd_status
1203 motg_root_intr_transfer(usbd_xfer_handle xfer)
1204 {
1205 struct motg_softc *sc = xfer->pipe->device->bus->hci_private;
1206 usbd_status err;
1207
1208 /* Insert last in queue. */
1209 mutex_enter(&sc->sc_lock);
1210 err = usb_insert_transfer(xfer);
1211 mutex_exit(&sc->sc_lock);
1212 if (err)
1213 return (err);
1214
1215 /*
1216 * Pipe isn't running (otherwise err would be USBD_INPROG),
1217 * start first
1218 */
1219 return (motg_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1220 }
1221
1222 /* Start a transfer on the root interrupt pipe */
1223 usbd_status
1224 motg_root_intr_start(usbd_xfer_handle xfer)
1225 {
1226 usbd_pipe_handle pipe = xfer->pipe;
1227 struct motg_softc *sc = pipe->device->bus->hci_private;
1228
1229 DPRINTFN(MD_ROOT, ("motg_root_intr_start: xfer=%p len=%d flags=%d\n",
1230 xfer, xfer->length, xfer->flags));
1231
1232 if (sc->sc_dying)
1233 return (USBD_IOERROR);
1234
1235 sc->sc_intr_xfer = xfer;
1236 return (USBD_IN_PROGRESS);
1237 }
1238
1239 /* Close the root interrupt pipe. */
1240 void
1241 motg_root_intr_close(usbd_pipe_handle pipe)
1242 {
1243 struct motg_softc *sc = pipe->device->bus->hci_private;
1244
1245 KASSERT(mutex_owned(&sc->sc_lock));
1246
1247 sc->sc_intr_xfer = NULL;
1248 DPRINTFN(MD_ROOT, ("motg_root_intr_close\n"));
1249 }
1250
1251 void
1252 motg_root_intr_done(usbd_xfer_handle xfer)
1253 {
1254 }
1255
1256 void
1257 motg_noop(usbd_pipe_handle pipe)
1258 {
1259 }
1260
1261 static usbd_status
1262 motg_portreset(struct motg_softc *sc)
1263 {
1264 uint32_t val;
1265
1266 val = UREAD1(sc, MUSB2_REG_POWER);
1267 val |= MUSB2_MASK_RESET;
1268 UWRITE1(sc, MUSB2_REG_POWER, val);
1269 /* Wait for 20 msec */
1270 usb_delay_ms(&sc->sc_bus, 20);
1271
1272 val = UREAD1(sc, MUSB2_REG_POWER);
1273 val &= ~MUSB2_MASK_RESET;
1274 UWRITE1(sc, MUSB2_REG_POWER, val);
1275
1276 /* determine line speed */
1277 val = UREAD1(sc, MUSB2_REG_POWER);
1278 if (val & MUSB2_MASK_HSMODE)
1279 sc->sc_high_speed = 1;
1280 else
1281 sc->sc_high_speed = 0;
1282 DPRINTFN(MD_ROOT | MD_CTRL, ("motg_portreset speed %d\n",
1283 sc->sc_high_speed));
1284
1285 sc->sc_isreset = 1;
1286 sc->sc_port_enabled = 1;
1287 return (USBD_NORMAL_COMPLETION);
1288 }
1289
1290 /*
1291 * This routine is executed when an interrupt on the root hub is detected
1292 */
1293 static void
1294 motg_hub_change(struct motg_softc *sc)
1295 {
1296 usbd_xfer_handle xfer = sc->sc_intr_xfer;
1297 usbd_pipe_handle pipe;
1298 u_char *p;
1299
1300 DPRINTFN(MD_ROOT, ("motg_hub_change\n"));
1301
1302 if (xfer == NULL)
1303 return; /* the interrupt pipe is not open */
1304
1305 pipe = xfer->pipe;
1306 if (pipe->device == NULL || pipe->device->bus == NULL)
1307 return; /* device has detached */
1308
1309 p = KERNADDR(&xfer->dmabuf, 0);
1310 p[0] = 1<<1;
1311 xfer->actlen = 1;
1312 xfer->status = USBD_NORMAL_COMPLETION;
1313 usb_transfer_complete(xfer);
1314 }
1315
1316 static uint8_t
1317 motg_speed(u_int8_t speed)
1318 {
1319 switch(speed) {
1320 case USB_SPEED_LOW:
1321 return MUSB2_MASK_TI_SPEED_LO;
1322 case USB_SPEED_FULL:
1323 return MUSB2_MASK_TI_SPEED_FS;
1324 case USB_SPEED_HIGH:
1325 return MUSB2_MASK_TI_SPEED_HS;
1326 default:
1327 panic("motg: unknown speed %d", speed);
1328 /* NOTREACHED */
1329 }
1330 }
1331
1332 static uint8_t
1333 motg_type(u_int8_t type)
1334 {
1335 switch(type) {
1336 case UE_CONTROL:
1337 return MUSB2_MASK_TI_PROTO_CTRL;
1338 case UE_ISOCHRONOUS:
1339 return MUSB2_MASK_TI_PROTO_ISOC;
1340 case UE_BULK:
1341 return MUSB2_MASK_TI_PROTO_BULK;
1342 case UE_INTERRUPT:
1343 return MUSB2_MASK_TI_PROTO_INTR;
1344 default:
1345 panic("motg: unknown type %d", type);
1346 /* NOTREACHED */
1347 }
1348 }
1349
1350 static void
1351 motg_setup_endpoint_tx(usbd_xfer_handle xfer)
1352 {
1353 struct motg_softc *sc = xfer->pipe->device->bus->hci_private;
1354 struct motg_pipe *otgpipe = (struct motg_pipe *)xfer->pipe;
1355 usbd_device_handle dev = otgpipe->pipe.device;
1356 int epnumber = otgpipe->hw_ep->ep_number;
1357
1358 UWRITE1(sc, MUSB2_REG_TXFADDR(epnumber), dev->address);
1359 if (dev->myhsport) {
1360 UWRITE1(sc, MUSB2_REG_TXHADDR(epnumber),
1361 dev->myhsport->parent->address);
1362 UWRITE1(sc, MUSB2_REG_TXHUBPORT(epnumber),
1363 dev->myhsport->portno);
1364 } else {
1365 UWRITE1(sc, MUSB2_REG_TXHADDR(epnumber), 0);
1366 UWRITE1(sc, MUSB2_REG_TXHUBPORT(epnumber), 0);
1367 }
1368 UWRITE1(sc, MUSB2_REG_TXTI,
1369 motg_speed(dev->speed) |
1370 UE_GET_ADDR(xfer->pipe->endpoint->edesc->bEndpointAddress) |
1371 motg_type(UE_GET_XFERTYPE(xfer->pipe->endpoint->edesc->bmAttributes))
1372 );
1373 if (epnumber == 0) {
1374 if (sc->sc_high_speed) {
1375 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT,
1376 NAK_TO_CTRL_HIGH);
1377 } else {
1378 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT, NAK_TO_CTRL);
1379 }
1380 } else {
1381 if ((xfer->pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE)
1382 == UE_BULK) {
1383 if (sc->sc_high_speed) {
1384 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT,
1385 NAK_TO_BULK_HIGH);
1386 } else {
1387 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT, NAK_TO_BULK);
1388 }
1389 } else {
1390 if (sc->sc_high_speed) {
1391 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT, POLL_TO_HIGH);
1392 } else {
1393 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT, POLL_TO);
1394 }
1395 }
1396 }
1397 }
1398
1399 static void
1400 motg_setup_endpoint_rx(usbd_xfer_handle xfer)
1401 {
1402 struct motg_softc *sc = xfer->pipe->device->bus->hci_private;
1403 usbd_device_handle dev = xfer->pipe->device;
1404 struct motg_pipe *otgpipe = (struct motg_pipe *)xfer->pipe;
1405 int epnumber = otgpipe->hw_ep->ep_number;
1406
1407 UWRITE1(sc, MUSB2_REG_RXFADDR(epnumber), dev->address);
1408 if (dev->myhsport) {
1409 UWRITE1(sc, MUSB2_REG_RXHADDR(epnumber),
1410 dev->myhsport->parent->address);
1411 UWRITE1(sc, MUSB2_REG_RXHUBPORT(epnumber),
1412 dev->myhsport->portno);
1413 } else {
1414 UWRITE1(sc, MUSB2_REG_RXHADDR(epnumber), 0);
1415 UWRITE1(sc, MUSB2_REG_RXHUBPORT(epnumber), 0);
1416 }
1417 UWRITE1(sc, MUSB2_REG_RXTI,
1418 motg_speed(dev->speed) |
1419 UE_GET_ADDR(xfer->pipe->endpoint->edesc->bEndpointAddress) |
1420 motg_type(UE_GET_XFERTYPE(xfer->pipe->endpoint->edesc->bmAttributes))
1421 );
1422 if (epnumber == 0) {
1423 if (sc->sc_high_speed) {
1424 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT,
1425 NAK_TO_CTRL_HIGH);
1426 } else {
1427 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT, NAK_TO_CTRL);
1428 }
1429 } else {
1430 if ((xfer->pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE)
1431 == UE_BULK) {
1432 if (sc->sc_high_speed) {
1433 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT,
1434 NAK_TO_BULK_HIGH);
1435 } else {
1436 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT, NAK_TO_BULK);
1437 }
1438 } else {
1439 if (sc->sc_high_speed) {
1440 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT, POLL_TO_HIGH);
1441 } else {
1442 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT, POLL_TO);
1443 }
1444 }
1445 }
1446 }
1447
1448 static usbd_status
1449 motg_device_ctrl_transfer(usbd_xfer_handle xfer)
1450 {
1451 struct motg_softc *sc = xfer->pipe->device->bus->hci_private;
1452 usbd_status err;
1453
1454 /* Insert last in queue. */
1455 mutex_enter(&sc->sc_lock);
1456 err = usb_insert_transfer(xfer);
1457 xfer->status = USBD_NOT_STARTED;
1458 mutex_exit(&sc->sc_lock);
1459 if (err)
1460 return (err);
1461
1462 /*
1463 * Pipe isn't running (otherwise err would be USBD_INPROG),
1464 * so start it first.
1465 */
1466 return (motg_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1467 }
1468
1469 static usbd_status
1470 motg_device_ctrl_start(usbd_xfer_handle xfer)
1471 {
1472 struct motg_softc *sc = xfer->pipe->device->bus->hci_private;
1473 usbd_status err;
1474 mutex_enter(&sc->sc_lock);
1475 err = motg_device_ctrl_start1(sc);
1476 mutex_exit(&sc->sc_lock);
1477 if (err != USBD_IN_PROGRESS)
1478 return err;
1479 if (sc->sc_bus.use_polling)
1480 motg_waitintr(sc, xfer);
1481 return USBD_IN_PROGRESS;
1482 }
1483
1484 static usbd_status
1485 motg_device_ctrl_start1(struct motg_softc *sc)
1486 {
1487 struct motg_hw_ep *ep = &sc->sc_in_ep[0];
1488 usbd_xfer_handle xfer = NULL;
1489 struct motg_pipe *otgpipe;
1490 usbd_status err = 0;
1491
1492 KASSERT(mutex_owned(&sc->sc_lock));
1493 if (sc->sc_dying)
1494 return (USBD_IOERROR);
1495
1496 if (!sc->sc_connected)
1497 return (USBD_IOERROR);
1498
1499 if (ep->xfer != NULL) {
1500 err = USBD_IN_PROGRESS;
1501 goto end;
1502 }
1503 /* locate the first pipe with work to do */
1504 SIMPLEQ_FOREACH(otgpipe, &ep->ep_pipes, ep_pipe_list) {
1505 xfer = SIMPLEQ_FIRST(&otgpipe->pipe.queue);
1506 DPRINTFN(MD_CTRL,
1507 ("motg_device_ctrl_start1 pipe %p xfer %p status %d\n",
1508 otgpipe, xfer, (xfer != NULL) ? xfer->status : 0));
1509
1510 if (xfer != NULL) {
1511 /* move this pipe to the end of the list */
1512 SIMPLEQ_REMOVE(&ep->ep_pipes, otgpipe,
1513 motg_pipe, ep_pipe_list);
1514 SIMPLEQ_INSERT_TAIL(&ep->ep_pipes,
1515 otgpipe, ep_pipe_list);
1516 break;
1517 }
1518 }
1519 if (xfer == NULL) {
1520 err = USBD_NOT_STARTED;
1521 goto end;
1522 }
1523 xfer->status = USBD_IN_PROGRESS;
1524 KASSERT(otgpipe == (struct motg_pipe *)xfer->pipe);
1525 KASSERT(otgpipe->hw_ep == ep);
1526 #ifdef DIAGNOSTIC
1527 if (!(xfer->rqflags & URQ_REQUEST))
1528 panic("motg_device_ctrl_transfer: not a request");
1529 #endif
1530 // KASSERT(xfer->actlen == 0);
1531 xfer->actlen = 0;
1532
1533 ep->xfer = xfer;
1534 ep->datalen = xfer->length;
1535 if (ep->datalen > 0)
1536 ep->data = KERNADDR(&xfer->dmabuf, 0);
1537 else
1538 ep->data = NULL;
1539 if ((xfer->flags & USBD_FORCE_SHORT_XFER) &&
1540 (ep->datalen % 64) == 0)
1541 ep->need_short_xfer = 1;
1542 else
1543 ep->need_short_xfer = 0;
1544 /* now we need send this request */
1545 DPRINTFN(MD_CTRL,
1546 ("motg_device_ctrl_start1(%p) send data %p len %d short %d speed %d to %d\n",
1547 xfer, ep->data, ep->datalen, ep->need_short_xfer, xfer->pipe->device->speed,
1548 xfer->pipe->device->address));
1549 KASSERT(ep->phase == IDLE);
1550 ep->phase = SETUP;
1551 /* select endpoint 0 */
1552 UWRITE1(sc, MUSB2_REG_EPINDEX, 0);
1553 /* fifo should be empty at this point */
1554 KASSERT((UREAD1(sc, MUSB2_REG_TXCSRL) & MUSB2_MASK_CSR0L_TXPKTRDY) == 0);
1555 /* send data */
1556 // KASSERT(((vaddr_t)(&xfer->request) & 3) == 0);
1557 KASSERT(sizeof(xfer->request) == 8);
1558 bus_space_write_multi_1(sc->sc_iot, sc->sc_ioh, MUSB2_REG_EPFIFO(0),
1559 (void *)&xfer->request, sizeof(xfer->request));
1560
1561 motg_setup_endpoint_tx(xfer);
1562 /* start transaction */
1563 UWRITE1(sc, MUSB2_REG_TXCSRL,
1564 MUSB2_MASK_CSR0L_TXPKTRDY | MUSB2_MASK_CSR0L_SETUPPKT);
1565
1566 end:
1567 if (err)
1568 return (err);
1569
1570 return (USBD_IN_PROGRESS);
1571 }
1572
1573 static void
1574 motg_device_ctrl_read(usbd_xfer_handle xfer)
1575 {
1576 struct motg_softc *sc = xfer->pipe->device->bus->hci_private;
1577 struct motg_pipe *otgpipe = (struct motg_pipe *)xfer->pipe;
1578 /* assume endpoint already selected */
1579 motg_setup_endpoint_rx(xfer);
1580 /* start transaction */
1581 UWRITE1(sc, MUSB2_REG_TXCSRL, MUSB2_MASK_CSR0L_REQPKT);
1582 otgpipe->hw_ep->phase = DATA_IN;
1583 }
1584
1585 static void
1586 motg_device_ctrl_intr_rx(struct motg_softc *sc)
1587 {
1588 struct motg_hw_ep *ep = &sc->sc_in_ep[0];
1589 usbd_xfer_handle xfer = ep->xfer;
1590 uint8_t csr;
1591 int datalen, max_datalen;
1592 char *data;
1593 bool got_short;
1594 usbd_status new_status = USBD_IN_PROGRESS;
1595
1596 KASSERT(mutex_owned(&sc->sc_lock));
1597
1598 #ifdef DIAGNOSTIC
1599 if (ep->phase != DATA_IN &&
1600 ep->phase != STATUS_IN)
1601 panic("motg_device_ctrl_intr_rx: bad phase %d", ep->phase);
1602 #endif
1603 /* select endpoint 0 */
1604 UWRITE1(sc, MUSB2_REG_EPINDEX, 0);
1605
1606 /* read out FIFO status */
1607 csr = UREAD1(sc, MUSB2_REG_TXCSRL);
1608 DPRINTFN(MD_CTRL,
1609 ("motg_device_ctrl_intr_rx phase %d csr 0x%x xfer %p status %d\n",
1610 ep->phase, csr, xfer, (xfer != NULL) ? xfer->status : 0));
1611
1612 if (csr & MUSB2_MASK_CSR0L_NAKTIMO) {
1613 csr &= ~MUSB2_MASK_CSR0L_REQPKT;
1614 UWRITE1(sc, MUSB2_REG_TXCSRL, csr);
1615
1616 csr &= ~MUSB2_MASK_CSR0L_NAKTIMO;
1617 UWRITE1(sc, MUSB2_REG_TXCSRL, csr);
1618 new_status = USBD_TIMEOUT; /* XXX */
1619 goto complete;
1620 }
1621 if (csr & (MUSB2_MASK_CSR0L_RXSTALL | MUSB2_MASK_CSR0L_ERROR)) {
1622 if (csr & MUSB2_MASK_CSR0L_RXSTALL)
1623 new_status = USBD_STALLED;
1624 else
1625 new_status = USBD_IOERROR;
1626 /* clear status */
1627 UWRITE1(sc, MUSB2_REG_TXCSRL, 0);
1628 goto complete;
1629 }
1630 if ((csr & MUSB2_MASK_CSR0L_RXPKTRDY) == 0)
1631 return; /* no data yet */
1632
1633 if (xfer == NULL || xfer->status != USBD_IN_PROGRESS)
1634 goto complete;
1635
1636 if (ep->phase == STATUS_IN) {
1637 new_status = USBD_NORMAL_COMPLETION;
1638 UWRITE1(sc, MUSB2_REG_TXCSRL, 0);
1639 goto complete;
1640 }
1641 datalen = UREAD2(sc, MUSB2_REG_RXCOUNT);
1642 DPRINTFN(MD_CTRL,
1643 ("motg_device_ctrl_intr_rx phase %d datalen %d\n",
1644 ep->phase, datalen));
1645 KASSERT(UGETW(xfer->pipe->endpoint->edesc->wMaxPacketSize) > 0);
1646 max_datalen = min(UGETW(xfer->pipe->endpoint->edesc->wMaxPacketSize),
1647 ep->datalen);
1648 if (datalen > max_datalen) {
1649 new_status = USBD_IOERROR;
1650 UWRITE1(sc, MUSB2_REG_TXCSRL, 0);
1651 goto complete;
1652 }
1653 got_short = (datalen < max_datalen);
1654 if (datalen > 0) {
1655 KASSERT(ep->phase == DATA_IN);
1656 data = ep->data;
1657 ep->data += datalen;
1658 ep->datalen -= datalen;
1659 xfer->actlen += datalen;
1660 if (((vaddr_t)data & 0x3) == 0 &&
1661 (datalen >> 2) > 0) {
1662 DPRINTFN(MD_CTRL,
1663 ("motg_device_ctrl_intr_rx r4 data %p len %d\n",
1664 data, datalen));
1665 bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh,
1666 MUSB2_REG_EPFIFO(0), (void *)data, datalen >> 2);
1667 data += (datalen & ~0x3);
1668 datalen -= (datalen & ~0x3);
1669 }
1670 DPRINTFN(MD_CTRL,
1671 ("motg_device_ctrl_intr_rx r1 data %p len %d\n",
1672 data, datalen));
1673 if (datalen) {
1674 bus_space_read_multi_1(sc->sc_iot, sc->sc_ioh,
1675 MUSB2_REG_EPFIFO(0), data, datalen);
1676 }
1677 }
1678 UWRITE1(sc, MUSB2_REG_TXCSRL, csr & ~MUSB2_MASK_CSR0L_RXPKTRDY);
1679 KASSERT(ep->phase == DATA_IN);
1680 if (got_short || (ep->datalen == 0)) {
1681 if (ep->need_short_xfer == 0) {
1682 ep->phase = STATUS_OUT;
1683 UWRITE1(sc, MUSB2_REG_TXCSRH,
1684 UREAD1(sc, MUSB2_REG_TXCSRH) |
1685 MUSB2_MASK_CSR0H_PING_DIS);
1686 motg_setup_endpoint_tx(xfer);
1687 UWRITE1(sc, MUSB2_REG_TXCSRL,
1688 MUSB2_MASK_CSR0L_STATUSPKT |
1689 MUSB2_MASK_CSR0L_TXPKTRDY);
1690 return;
1691 }
1692 ep->need_short_xfer = 0;
1693 }
1694 motg_device_ctrl_read(xfer);
1695 return;
1696 complete:
1697 ep->phase = IDLE;
1698 ep->xfer = NULL;
1699 if (xfer && xfer->status == USBD_IN_PROGRESS) {
1700 KASSERT(new_status != USBD_IN_PROGRESS);
1701 xfer->status = new_status;
1702 usb_transfer_complete(xfer);
1703 }
1704 motg_device_ctrl_start1(sc);
1705 }
1706
1707 static void
1708 motg_device_ctrl_intr_tx(struct motg_softc *sc)
1709 {
1710 struct motg_hw_ep *ep = &sc->sc_in_ep[0];
1711 usbd_xfer_handle xfer = ep->xfer;
1712 uint8_t csr;
1713 int datalen;
1714 char *data;
1715 usbd_status new_status = USBD_IN_PROGRESS;
1716
1717 KASSERT(mutex_owned(&sc->sc_lock));
1718 if (ep->phase == DATA_IN || ep->phase == STATUS_IN) {
1719 motg_device_ctrl_intr_rx(sc);
1720 return;
1721 }
1722
1723 #ifdef DIAGNOSTIC
1724 if (ep->phase != SETUP && ep->phase != DATA_OUT &&
1725 ep->phase != STATUS_OUT)
1726 panic("motg_device_ctrl_intr_tx: bad phase %d", ep->phase);
1727 #endif
1728 /* select endpoint 0 */
1729 UWRITE1(sc, MUSB2_REG_EPINDEX, 0);
1730
1731 csr = UREAD1(sc, MUSB2_REG_TXCSRL);
1732 DPRINTFN(MD_CTRL,
1733 ("motg_device_ctrl_intr_tx phase %d csr 0x%x xfer %p status %d\n",
1734 ep->phase, csr, xfer, (xfer != NULL) ? xfer->status : 0));
1735
1736 if (csr & MUSB2_MASK_CSR0L_RXSTALL) {
1737 /* command not accepted */
1738 new_status = USBD_STALLED;
1739 /* clear status */
1740 UWRITE1(sc, MUSB2_REG_TXCSRL, 0);
1741 goto complete;
1742 }
1743 if (csr & MUSB2_MASK_CSR0L_NAKTIMO) {
1744 new_status = USBD_TIMEOUT; /* XXX */
1745 /* flush fifo */
1746 while (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY) {
1747 UWRITE1(sc, MUSB2_REG_TXCSRH,
1748 UREAD1(sc, MUSB2_REG_TXCSRH) |
1749 MUSB2_MASK_CSR0H_FFLUSH);
1750 csr = UREAD1(sc, MUSB2_REG_TXCSRL);
1751 }
1752 csr &= ~MUSB2_MASK_CSR0L_NAKTIMO;
1753 UWRITE1(sc, MUSB2_REG_TXCSRL, csr);
1754 goto complete;
1755 }
1756 if (csr & MUSB2_MASK_CSR0L_ERROR) {
1757 new_status = USBD_IOERROR;
1758 /* clear status */
1759 UWRITE1(sc, MUSB2_REG_TXCSRL, 0);
1760 goto complete;
1761 }
1762 if (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY) {
1763 /* data still not sent */
1764 return;
1765 }
1766 if (xfer == NULL)
1767 goto complete;
1768 if (ep->phase == STATUS_OUT) {
1769 /*
1770 * we have sent status and got no error;
1771 * declare transfer complete
1772 */
1773 DPRINTFN(MD_CTRL,
1774 ("motg_device_ctrl_intr_tx %p status %d complete\n",
1775 xfer, xfer->status));
1776 new_status = USBD_NORMAL_COMPLETION;
1777 goto complete;
1778 }
1779 if (ep->datalen == 0) {
1780 if (ep->need_short_xfer) {
1781 ep->need_short_xfer = 0;
1782 /* one more data phase */
1783 if (xfer->request.bmRequestType & UT_READ) {
1784 DPRINTFN(MD_CTRL,
1785 ("motg_device_ctrl_intr_tx %p to DATA_IN\n", xfer));
1786 motg_device_ctrl_read(xfer);
1787 return;
1788 } /* else fall back to DATA_OUT */
1789 } else {
1790 DPRINTFN(MD_CTRL,
1791 ("motg_device_ctrl_intr_tx %p to STATUS_IN, csrh 0x%x\n",
1792 xfer, UREAD1(sc, MUSB2_REG_TXCSRH)));
1793 ep->phase = STATUS_IN;
1794 UWRITE1(sc, MUSB2_REG_RXCSRH,
1795 UREAD1(sc, MUSB2_REG_RXCSRH) |
1796 MUSB2_MASK_CSR0H_PING_DIS);
1797 motg_setup_endpoint_rx(xfer);
1798 UWRITE1(sc, MUSB2_REG_TXCSRL,
1799 MUSB2_MASK_CSR0L_STATUSPKT |
1800 MUSB2_MASK_CSR0L_REQPKT);
1801 return;
1802 }
1803 }
1804 if (xfer->request.bmRequestType & UT_READ) {
1805 motg_device_ctrl_read(xfer);
1806 return;
1807 }
1808 /* setup a dataout phase */
1809 datalen = min(ep->datalen,
1810 UGETW(xfer->pipe->endpoint->edesc->wMaxPacketSize));
1811 ep->phase = DATA_OUT;
1812 DPRINTFN(MD_CTRL,
1813 ("motg_device_ctrl_intr_tx %p to DATA_OUT, csrh 0x%x\n", xfer,
1814 UREAD1(sc, MUSB2_REG_TXCSRH)));
1815 if (datalen) {
1816 data = ep->data;
1817 ep->data += datalen;
1818 ep->datalen -= datalen;
1819 xfer->actlen += datalen;
1820 if (((vaddr_t)data & 0x3) == 0 &&
1821 (datalen >> 2) > 0) {
1822 bus_space_write_multi_4(sc->sc_iot, sc->sc_ioh,
1823 MUSB2_REG_EPFIFO(0), (void *)data, datalen >> 2);
1824 data += (datalen & ~0x3);
1825 datalen -= (datalen & ~0x3);
1826 }
1827 if (datalen) {
1828 bus_space_write_multi_1(sc->sc_iot, sc->sc_ioh,
1829 MUSB2_REG_EPFIFO(0), data, datalen);
1830 }
1831 }
1832 /* send data */
1833 motg_setup_endpoint_tx(xfer);
1834 UWRITE1(sc, MUSB2_REG_TXCSRL, MUSB2_MASK_CSR0L_TXPKTRDY);
1835 return;
1836
1837 complete:
1838 ep->phase = IDLE;
1839 ep->xfer = NULL;
1840 if (xfer && xfer->status == USBD_IN_PROGRESS) {
1841 KASSERT(new_status != USBD_IN_PROGRESS);
1842 xfer->status = new_status;
1843 usb_transfer_complete(xfer);
1844 }
1845 motg_device_ctrl_start1(sc);
1846 }
1847
1848 /* Abort a device control request. */
1849 void
1850 motg_device_ctrl_abort(usbd_xfer_handle xfer)
1851 {
1852 DPRINTFN(MD_CTRL, ("motg_device_ctrl_abort:\n"));
1853 motg_device_xfer_abort(xfer);
1854 }
1855
1856 /* Close a device control pipe */
1857 void
1858 motg_device_ctrl_close(usbd_pipe_handle pipe)
1859 {
1860 struct motg_softc *sc = pipe->device->bus->hci_private;
1861 struct motg_pipe *otgpipe = (struct motg_pipe *)pipe;
1862 struct motg_pipe *otgpipeiter;
1863
1864 DPRINTFN(MD_CTRL, ("motg_device_ctrl_close:\n"));
1865 KASSERT(mutex_owned(&sc->sc_lock));
1866 KASSERT(otgpipe->hw_ep->xfer == NULL ||
1867 otgpipe->hw_ep->xfer->pipe != pipe);
1868
1869 SIMPLEQ_FOREACH(otgpipeiter, &otgpipe->hw_ep->ep_pipes, ep_pipe_list) {
1870 if (otgpipeiter == otgpipe) {
1871 /* remove from list */
1872 SIMPLEQ_REMOVE(&otgpipe->hw_ep->ep_pipes, otgpipe,
1873 motg_pipe, ep_pipe_list);
1874 otgpipe->hw_ep->refcount--;
1875 /* we're done */
1876 return;
1877 }
1878 }
1879 panic("motg_device_ctrl_close: not found");
1880 }
1881
1882 void
1883 motg_device_ctrl_done(usbd_xfer_handle xfer)
1884 {
1885 struct motg_pipe *otgpipe = (struct motg_pipe *)xfer->pipe;
1886 DPRINTFN(MD_CTRL, ("motg_device_ctrl_done:\n"));
1887 KASSERT(otgpipe->hw_ep->xfer != xfer);
1888 }
1889
1890 static usbd_status
1891 motg_device_data_transfer(usbd_xfer_handle xfer)
1892 {
1893 struct motg_softc *sc = xfer->pipe->device->bus->hci_private;
1894 usbd_status err;
1895
1896 /* Insert last in queue. */
1897 mutex_enter(&sc->sc_lock);
1898 DPRINTF(("motg_device_data_transfer(%p) status %d\n",
1899 xfer, xfer->status));
1900 err = usb_insert_transfer(xfer);
1901 xfer->status = USBD_NOT_STARTED;
1902 mutex_exit(&sc->sc_lock);
1903 if (err)
1904 return (err);
1905
1906 /*
1907 * Pipe isn't running (otherwise err would be USBD_INPROG),
1908 * so start it first.
1909 */
1910 return (motg_device_data_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1911 }
1912
1913 static usbd_status
1914 motg_device_data_start(usbd_xfer_handle xfer)
1915 {
1916 struct motg_softc *sc = xfer->pipe->device->bus->hci_private;
1917 struct motg_pipe *otgpipe = (struct motg_pipe *)xfer->pipe;
1918 usbd_status err;
1919 mutex_enter(&sc->sc_lock);
1920 DPRINTF(("motg_device_data_start(%p) status %d\n",
1921 xfer, xfer->status));
1922 err = motg_device_data_start1(sc, otgpipe->hw_ep);
1923 mutex_exit(&sc->sc_lock);
1924 if (err != USBD_IN_PROGRESS)
1925 return err;
1926 if (sc->sc_bus.use_polling)
1927 motg_waitintr(sc, xfer);
1928 return USBD_IN_PROGRESS;
1929 }
1930
1931 static usbd_status
1932 motg_device_data_start1(struct motg_softc *sc, struct motg_hw_ep *ep)
1933 {
1934 usbd_xfer_handle xfer = NULL;
1935 struct motg_pipe *otgpipe;
1936 usbd_status err = 0;
1937 uint32_t val;
1938
1939 KASSERT(mutex_owned(&sc->sc_lock));
1940 if (sc->sc_dying)
1941 return (USBD_IOERROR);
1942
1943 if (!sc->sc_connected)
1944 return (USBD_IOERROR);
1945
1946 if (ep->xfer != NULL) {
1947 err = USBD_IN_PROGRESS;
1948 goto end;
1949 }
1950 /* locate the first pipe with work to do */
1951 SIMPLEQ_FOREACH(otgpipe, &ep->ep_pipes, ep_pipe_list) {
1952 xfer = SIMPLEQ_FIRST(&otgpipe->pipe.queue);
1953 DPRINTFN(MD_BULK,
1954 ("motg_device_data_start1 pipe %p xfer %p status %d\n",
1955 otgpipe, xfer, (xfer != NULL) ? xfer->status : 0));
1956 if (xfer != NULL) {
1957 /* move this pipe to the end of the list */
1958 SIMPLEQ_REMOVE(&ep->ep_pipes, otgpipe,
1959 motg_pipe, ep_pipe_list);
1960 SIMPLEQ_INSERT_TAIL(&ep->ep_pipes,
1961 otgpipe, ep_pipe_list);
1962 break;
1963 }
1964 }
1965 if (xfer == NULL) {
1966 err = USBD_NOT_STARTED;
1967 goto end;
1968 }
1969 xfer->status = USBD_IN_PROGRESS;
1970 KASSERT(otgpipe == (struct motg_pipe *)xfer->pipe);
1971 KASSERT(otgpipe->hw_ep == ep);
1972 #ifdef DIAGNOSTIC
1973 if (xfer->rqflags & URQ_REQUEST)
1974 panic("motg_device_data_transfer: a request");
1975 #endif
1976 // KASSERT(xfer->actlen == 0);
1977 xfer->actlen = 0;
1978
1979 ep->xfer = xfer;
1980 ep->datalen = xfer->length;
1981 KASSERT(ep->datalen > 0);
1982 ep->data = KERNADDR(&xfer->dmabuf, 0);
1983 if ((xfer->flags & USBD_FORCE_SHORT_XFER) &&
1984 (ep->datalen % 64) == 0)
1985 ep->need_short_xfer = 1;
1986 else
1987 ep->need_short_xfer = 0;
1988 /* now we need send this request */
1989 DPRINTFN(MD_BULK,
1990 ("motg_device_data_start1(%p) %s data %p len %d short %d speed %d to %d\n",
1991 xfer,
1992 UE_GET_DIR(xfer->pipe->endpoint->edesc->bEndpointAddress) == UE_DIR_IN ? "read" : "write",
1993 ep->data, ep->datalen, ep->need_short_xfer, xfer->pipe->device->speed,
1994 xfer->pipe->device->address));
1995 KASSERT(ep->phase == IDLE);
1996 /* select endpoint */
1997 UWRITE1(sc, MUSB2_REG_EPINDEX, ep->ep_number);
1998 if (UE_GET_DIR(xfer->pipe->endpoint->edesc->bEndpointAddress)
1999 == UE_DIR_IN) {
2000 val = UREAD1(sc, MUSB2_REG_RXCSRL);
2001 KASSERT((val & MUSB2_MASK_CSRL_RXPKTRDY) == 0);
2002 motg_device_data_read(xfer);
2003 } else {
2004 ep->phase = DATA_OUT;
2005 val = UREAD1(sc, MUSB2_REG_TXCSRL);
2006 KASSERT((val & MUSB2_MASK_CSRL_TXPKTRDY) == 0);
2007 motg_device_data_write(xfer);
2008 }
2009 end:
2010 if (err)
2011 return (err);
2012
2013 return (USBD_IN_PROGRESS);
2014 }
2015
2016 static void
2017 motg_device_data_read(usbd_xfer_handle xfer)
2018 {
2019 struct motg_softc *sc = xfer->pipe->device->bus->hci_private;
2020 struct motg_pipe *otgpipe = (struct motg_pipe *)xfer->pipe;
2021 uint32_t val;
2022
2023 KASSERT(mutex_owned(&sc->sc_lock));
2024 /* assume endpoint already selected */
2025 motg_setup_endpoint_rx(xfer);
2026 /* Max packet size */
2027 UWRITE2(sc, MUSB2_REG_RXMAXP,
2028 UGETW(xfer->pipe->endpoint->edesc->wMaxPacketSize));
2029 /* Data Toggle */
2030 val = UREAD1(sc, MUSB2_REG_RXCSRH);
2031 val |= MUSB2_MASK_CSRH_RXDT_WREN;
2032 if (otgpipe->nexttoggle)
2033 val |= MUSB2_MASK_CSRH_RXDT_VAL;
2034 else
2035 val &= ~MUSB2_MASK_CSRH_RXDT_VAL;
2036 UWRITE1(sc, MUSB2_REG_RXCSRH, val);
2037
2038 DPRINTFN(MD_BULK,
2039 ("motg_device_data_read %p to DATA_IN on ep %d, csrh 0x%x\n",
2040 xfer, otgpipe->hw_ep->ep_number, UREAD1(sc, MUSB2_REG_RXCSRH)));
2041 /* start transaction */
2042 UWRITE1(sc, MUSB2_REG_RXCSRL, MUSB2_MASK_CSRL_RXREQPKT);
2043 otgpipe->hw_ep->phase = DATA_IN;
2044 }
2045
2046 static void
2047 motg_device_data_write(usbd_xfer_handle xfer)
2048 {
2049 struct motg_softc *sc = xfer->pipe->device->bus->hci_private;
2050 struct motg_pipe *otgpipe = (struct motg_pipe *)xfer->pipe;
2051 struct motg_hw_ep *ep = otgpipe->hw_ep;
2052 int datalen;
2053 char *data;
2054 uint32_t val;
2055
2056 KASSERT(xfer!=NULL);
2057 KASSERT(mutex_owned(&sc->sc_lock));
2058
2059 datalen = min(ep->datalen,
2060 UGETW(xfer->pipe->endpoint->edesc->wMaxPacketSize));
2061 ep->phase = DATA_OUT;
2062 DPRINTFN(MD_BULK,
2063 ("motg_device_data_write %p to DATA_OUT on ep %d, len %d csrh 0x%x\n",
2064 xfer, ep->ep_number, datalen, UREAD1(sc, MUSB2_REG_TXCSRH)));
2065
2066 /* assume endpoint already selected */
2067 /* write data to fifo */
2068 data = ep->data;
2069 ep->data += datalen;
2070 ep->datalen -= datalen;
2071 xfer->actlen += datalen;
2072 if (((vaddr_t)data & 0x3) == 0 &&
2073 (datalen >> 2) > 0) {
2074 bus_space_write_multi_4(sc->sc_iot, sc->sc_ioh,
2075 MUSB2_REG_EPFIFO(ep->ep_number),
2076 (void *)data, datalen >> 2);
2077 data += (datalen & ~0x3);
2078 datalen -= (datalen & ~0x3);
2079 }
2080 if (datalen) {
2081 bus_space_write_multi_1(sc->sc_iot, sc->sc_ioh,
2082 MUSB2_REG_EPFIFO(ep->ep_number), data, datalen);
2083 }
2084
2085 motg_setup_endpoint_tx(xfer);
2086 /* Max packet size */
2087 UWRITE2(sc, MUSB2_REG_TXMAXP,
2088 UGETW(xfer->pipe->endpoint->edesc->wMaxPacketSize));
2089 /* Data Toggle */
2090 val = UREAD1(sc, MUSB2_REG_TXCSRH);
2091 val |= MUSB2_MASK_CSRH_TXDT_WREN;
2092 if (otgpipe->nexttoggle)
2093 val |= MUSB2_MASK_CSRH_TXDT_VAL;
2094 else
2095 val &= ~MUSB2_MASK_CSRH_TXDT_VAL;
2096 UWRITE1(sc, MUSB2_REG_TXCSRH, val);
2097
2098 /* start transaction */
2099 UWRITE1(sc, MUSB2_REG_TXCSRL, MUSB2_MASK_CSRL_TXPKTRDY);
2100 }
2101
2102 static void
2103 motg_device_intr_rx(struct motg_softc *sc, int epnumber)
2104 {
2105 struct motg_hw_ep *ep = &sc->sc_in_ep[epnumber];
2106 usbd_xfer_handle xfer = ep->xfer;
2107 uint8_t csr;
2108 int datalen, max_datalen;
2109 char *data;
2110 bool got_short;
2111 usbd_status new_status = USBD_IN_PROGRESS;
2112
2113 KASSERT(mutex_owned(&sc->sc_lock));
2114 KASSERT(ep->ep_number == epnumber);
2115
2116 DPRINTFN(MD_BULK,
2117 ("motg_device_intr_rx on ep %d\n", epnumber));
2118 /* select endpoint */
2119 UWRITE1(sc, MUSB2_REG_EPINDEX, epnumber);
2120
2121 /* read out FIFO status */
2122 csr = UREAD1(sc, MUSB2_REG_RXCSRL);
2123 DPRINTFN(MD_BULK,
2124 ("motg_device_intr_rx phase %d csr 0x%x\n",
2125 ep->phase, csr));
2126
2127 if ((csr & (MUSB2_MASK_CSRL_RXNAKTO | MUSB2_MASK_CSRL_RXSTALL |
2128 MUSB2_MASK_CSRL_RXERROR | MUSB2_MASK_CSRL_RXPKTRDY)) == 0)
2129 return;
2130
2131 #ifdef DIAGNOSTIC
2132 if (ep->phase != DATA_IN)
2133 panic("motg_device_intr_rx: bad phase %d", ep->phase);
2134 #endif
2135 if (csr & MUSB2_MASK_CSRL_RXNAKTO) {
2136 csr &= ~MUSB2_MASK_CSRL_RXREQPKT;
2137 UWRITE1(sc, MUSB2_REG_RXCSRL, csr);
2138
2139 csr &= ~MUSB2_MASK_CSRL_RXNAKTO;
2140 UWRITE1(sc, MUSB2_REG_RXCSRL, csr);
2141 new_status = USBD_TIMEOUT; /* XXX */
2142 goto complete;
2143 }
2144 if (csr & (MUSB2_MASK_CSRL_RXSTALL | MUSB2_MASK_CSRL_RXERROR)) {
2145 if (csr & MUSB2_MASK_CSRL_RXSTALL)
2146 new_status = USBD_STALLED;
2147 else
2148 new_status = USBD_IOERROR;
2149 /* clear status */
2150 UWRITE1(sc, MUSB2_REG_RXCSRL, 0);
2151 goto complete;
2152 }
2153 KASSERT(csr & MUSB2_MASK_CSRL_RXPKTRDY);
2154
2155 if (xfer == NULL || xfer->status != USBD_IN_PROGRESS) {
2156 UWRITE1(sc, MUSB2_REG_RXCSRL, 0);
2157 goto complete;
2158 }
2159
2160 struct motg_pipe *otgpipe = (struct motg_pipe *)xfer->pipe;
2161 otgpipe->nexttoggle = otgpipe->nexttoggle ^ 1;
2162
2163 datalen = UREAD2(sc, MUSB2_REG_RXCOUNT);
2164 DPRINTFN(MD_BULK,
2165 ("motg_device_intr_rx phase %d datalen %d\n",
2166 ep->phase, datalen));
2167 KASSERT(UE_GET_SIZE(UGETW(xfer->pipe->endpoint->edesc->wMaxPacketSize)) > 0);
2168 max_datalen = min(
2169 UE_GET_SIZE(UGETW(xfer->pipe->endpoint->edesc->wMaxPacketSize)),
2170 ep->datalen);
2171 if (datalen > max_datalen) {
2172 new_status = USBD_IOERROR;
2173 UWRITE1(sc, MUSB2_REG_RXCSRL, 0);
2174 goto complete;
2175 }
2176 got_short = (datalen < max_datalen);
2177 if (datalen > 0) {
2178 KASSERT(ep->phase == DATA_IN);
2179 data = ep->data;
2180 ep->data += datalen;
2181 ep->datalen -= datalen;
2182 xfer->actlen += datalen;
2183 if (((vaddr_t)data & 0x3) == 0 &&
2184 (datalen >> 2) > 0) {
2185 DPRINTFN(MD_BULK,
2186 ("motg_device_intr_rx r4 data %p len %d\n",
2187 data, datalen));
2188 bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh,
2189 MUSB2_REG_EPFIFO(ep->ep_number),
2190 (void *)data, datalen >> 2);
2191 data += (datalen & ~0x3);
2192 datalen -= (datalen & ~0x3);
2193 }
2194 DPRINTFN(MD_BULK,
2195 ("motg_device_intr_rx r1 data %p len %d\n",
2196 data, datalen));
2197 if (datalen) {
2198 bus_space_read_multi_1(sc->sc_iot, sc->sc_ioh,
2199 MUSB2_REG_EPFIFO(ep->ep_number), data, datalen);
2200 }
2201 }
2202 UWRITE1(sc, MUSB2_REG_RXCSRL, 0);
2203 KASSERT(ep->phase == DATA_IN);
2204 if (got_short || (ep->datalen == 0)) {
2205 if (ep->need_short_xfer == 0) {
2206 new_status = USBD_NORMAL_COMPLETION;
2207 goto complete;
2208 }
2209 ep->need_short_xfer = 0;
2210 }
2211 motg_device_data_read(xfer);
2212 return;
2213 complete:
2214 DPRINTFN(MD_BULK,
2215 ("motg_device_intr_rx xfer %p complete, status %d\n", xfer,
2216 (xfer != NULL) ? xfer->status : 0));
2217 ep->phase = IDLE;
2218 ep->xfer = NULL;
2219 if (xfer && xfer->status == USBD_IN_PROGRESS) {
2220 KASSERT(new_status != USBD_IN_PROGRESS);
2221 xfer->status = new_status;
2222 usb_transfer_complete(xfer);
2223 }
2224 motg_device_data_start1(sc, ep);
2225 }
2226
2227 static void
2228 motg_device_intr_tx(struct motg_softc *sc, int epnumber)
2229 {
2230 struct motg_hw_ep *ep = &sc->sc_out_ep[epnumber];
2231 usbd_xfer_handle xfer = ep->xfer;
2232 uint8_t csr;
2233 struct motg_pipe *otgpipe;
2234 usbd_status new_status = USBD_IN_PROGRESS;
2235
2236 KASSERT(mutex_owned(&sc->sc_lock));
2237 KASSERT(ep->ep_number == epnumber);
2238
2239 DPRINTFN(MD_BULK,
2240 ("motg_device_intr_tx on ep %d\n", epnumber));
2241 /* select endpoint */
2242 UWRITE1(sc, MUSB2_REG_EPINDEX, epnumber);
2243
2244 csr = UREAD1(sc, MUSB2_REG_TXCSRL);
2245 DPRINTFN(MD_BULK,
2246 ("motg_device_intr_tx phase %d csr 0x%x\n",
2247 ep->phase, csr));
2248
2249 if (csr & (MUSB2_MASK_CSRL_TXSTALLED|MUSB2_MASK_CSRL_TXERROR)) {
2250 /* command not accepted */
2251 if (csr & MUSB2_MASK_CSRL_TXSTALLED)
2252 new_status = USBD_STALLED;
2253 else
2254 new_status = USBD_IOERROR;
2255 /* clear status */
2256 UWRITE1(sc, MUSB2_REG_TXCSRL, 0);
2257 goto complete;
2258 }
2259 if (csr & MUSB2_MASK_CSRL_TXNAKTO) {
2260 new_status = USBD_TIMEOUT; /* XXX */
2261 csr &= ~MUSB2_MASK_CSRL_TXNAKTO;
2262 UWRITE1(sc, MUSB2_REG_TXCSRL, csr);
2263 /* flush fifo */
2264 while (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) {
2265 csr |= MUSB2_MASK_CSRL_TXFFLUSH;
2266 csr &= ~MUSB2_MASK_CSRL_TXNAKTO;
2267 UWRITE1(sc, MUSB2_REG_TXCSRL, csr);
2268 delay(1000);
2269 csr = UREAD1(sc, MUSB2_REG_TXCSRL);
2270 DPRINTFN(MD_BULK, ("TX fifo flush ep %d CSR 0x%x\n",
2271 epnumber, csr));
2272 }
2273 goto complete;
2274 }
2275 if (csr & (MUSB2_MASK_CSRL_TXFIFONEMPTY|MUSB2_MASK_CSRL_TXPKTRDY)) {
2276 /* data still not sent */
2277 return;
2278 }
2279 if (xfer == NULL || xfer->status != USBD_IN_PROGRESS)
2280 goto complete;
2281 #ifdef DIAGNOSTIC
2282 if (ep->phase != DATA_OUT)
2283 panic("motg_device_intr_tx: bad phase %d", ep->phase);
2284 #endif
2285
2286 otgpipe = (struct motg_pipe *)xfer->pipe;
2287 otgpipe->nexttoggle = otgpipe->nexttoggle ^ 1;
2288
2289 if (ep->datalen == 0) {
2290 if (ep->need_short_xfer) {
2291 ep->need_short_xfer = 0;
2292 /* one more data phase */
2293 } else {
2294 new_status = USBD_NORMAL_COMPLETION;
2295 goto complete;
2296 }
2297 }
2298 motg_device_data_write(xfer);
2299 return;
2300
2301 complete:
2302 DPRINTFN(MD_BULK,
2303 ("motg_device_intr_tx xfer %p complete, status %d\n", xfer,
2304 (xfer != NULL) ? xfer->status : 0));
2305 #ifdef DIAGNOSTIC
2306 if (xfer && xfer->status == USBD_IN_PROGRESS && ep->phase != DATA_OUT)
2307 panic("motg_device_intr_tx: bad phase %d", ep->phase);
2308 #endif
2309 ep->phase = IDLE;
2310 ep->xfer = NULL;
2311 if (xfer && xfer->status == USBD_IN_PROGRESS) {
2312 KASSERT(new_status != USBD_IN_PROGRESS);
2313 xfer->status = new_status;
2314 usb_transfer_complete(xfer);
2315 }
2316 motg_device_data_start1(sc, ep);
2317 }
2318
2319 /* Abort a device control request. */
2320 void
2321 motg_device_data_abort(usbd_xfer_handle xfer)
2322 {
2323 #ifdef DIAGNOSTIC
2324 struct motg_softc *sc = xfer->pipe->device->bus->hci_private;
2325 #endif
2326 KASSERT(mutex_owned(&sc->sc_lock));
2327
2328 DPRINTFN(MD_BULK, ("motg_device_data_abort:\n"));
2329 motg_device_xfer_abort(xfer);
2330 }
2331
2332 /* Close a device control pipe */
2333 void
2334 motg_device_data_close(usbd_pipe_handle pipe)
2335 {
2336 struct motg_softc *sc = pipe->device->bus->hci_private;
2337 struct motg_pipe *otgpipe = (struct motg_pipe *)pipe;
2338 struct motg_pipe *otgpipeiter;
2339
2340 DPRINTFN(MD_CTRL, ("motg_device_data_close:\n"));
2341 KASSERT(mutex_owned(&sc->sc_lock));
2342 KASSERT(otgpipe->hw_ep->xfer == NULL ||
2343 otgpipe->hw_ep->xfer->pipe != pipe);
2344
2345 pipe->endpoint->datatoggle = otgpipe->nexttoggle;
2346 SIMPLEQ_FOREACH(otgpipeiter, &otgpipe->hw_ep->ep_pipes, ep_pipe_list) {
2347 if (otgpipeiter == otgpipe) {
2348 /* remove from list */
2349 SIMPLEQ_REMOVE(&otgpipe->hw_ep->ep_pipes, otgpipe,
2350 motg_pipe, ep_pipe_list);
2351 otgpipe->hw_ep->refcount--;
2352 /* we're done */
2353 return;
2354 }
2355 }
2356 panic("motg_device_data_close: not found");
2357 }
2358
2359 void
2360 motg_device_data_done(usbd_xfer_handle xfer)
2361 {
2362 struct motg_pipe *otgpipe = (struct motg_pipe *)xfer->pipe;
2363 DPRINTFN(MD_CTRL, ("motg_device_data_done:\n"));
2364 KASSERT(otgpipe->hw_ep->xfer != xfer);
2365 }
2366
2367 /*
2368 * Wait here until controller claims to have an interrupt.
2369 * Then call motg_intr and return. Use timeout to avoid waiting
2370 * too long.
2371 * Only used during boot when interrupts are not enabled yet.
2372 */
2373 void
2374 motg_waitintr(struct motg_softc *sc, usbd_xfer_handle xfer)
2375 {
2376 int timo = xfer->timeout;
2377
2378 mutex_enter(&sc->sc_lock);
2379
2380 DPRINTF(("motg_waitintr: timeout = %dms\n", timo));
2381
2382 for (; timo >= 0; timo--) {
2383 mutex_exit(&sc->sc_lock);
2384 usb_delay_ms(&sc->sc_bus, 1);
2385 mutex_spin_enter(&sc->sc_intr_lock);
2386 motg_poll(&sc->sc_bus);
2387 mutex_spin_exit(&sc->sc_intr_lock);
2388 mutex_enter(&sc->sc_lock);
2389 if (xfer->status != USBD_IN_PROGRESS)
2390 goto done;
2391 }
2392
2393 /* Timeout */
2394 DPRINTF(("motg_waitintr: timeout\n"));
2395 panic("motg_waitintr: timeout");
2396 /* XXX handle timeout ! */
2397
2398 done:
2399 mutex_exit(&sc->sc_lock);
2400 }
2401
2402 void
2403 motg_device_clear_toggle(usbd_pipe_handle pipe)
2404 {
2405 struct motg_pipe *otgpipe = (struct motg_pipe *)pipe;
2406 otgpipe->nexttoggle = 0;
2407 }
2408
2409 /* Abort a device control request. */
2410 static void
2411 motg_device_xfer_abort(usbd_xfer_handle xfer)
2412 {
2413 int wake;
2414 uint8_t csr;
2415 #ifdef DIAGNOSTIC
2416 struct motg_softc *sc = xfer->pipe->device->bus->hci_private;
2417 #endif
2418 struct motg_pipe *otgpipe = (struct motg_pipe *)xfer->pipe;
2419 KASSERT(mutex_owned(&sc->sc_lock));
2420
2421 DPRINTF(("motg_device_xfer_abort:\n"));
2422 if (xfer->hcflags & UXFER_ABORTING) {
2423 DPRINTF(("motg_device_xfer_abort: already aborting\n"));
2424 xfer->hcflags |= UXFER_ABORTWAIT;
2425 while (xfer->hcflags & UXFER_ABORTING)
2426 cv_wait(&xfer->hccv, &sc->sc_lock);
2427 return;
2428 }
2429 xfer->hcflags |= UXFER_ABORTING;
2430 if (otgpipe->hw_ep->xfer == xfer) {
2431 KASSERT(xfer->status == USBD_IN_PROGRESS);
2432 otgpipe->hw_ep->xfer = NULL;
2433 if (otgpipe->hw_ep->ep_number > 0) {
2434 /* select endpoint */
2435 UWRITE1(sc, MUSB2_REG_EPINDEX,
2436 otgpipe->hw_ep->ep_number);
2437 if (otgpipe->hw_ep->phase == DATA_OUT) {
2438 csr = UREAD1(sc, MUSB2_REG_TXCSRL);
2439 while (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) {
2440 csr |= MUSB2_MASK_CSRL_TXFFLUSH;
2441 UWRITE1(sc, MUSB2_REG_TXCSRL, csr);
2442 csr = UREAD1(sc, MUSB2_REG_TXCSRL);
2443 }
2444 UWRITE1(sc, MUSB2_REG_TXCSRL, 0);
2445 } else if (otgpipe->hw_ep->phase == DATA_IN) {
2446 csr = UREAD1(sc, MUSB2_REG_RXCSRL);
2447 while (csr & MUSB2_MASK_CSRL_RXPKTRDY) {
2448 csr |= MUSB2_MASK_CSRL_RXFFLUSH;
2449 UWRITE1(sc, MUSB2_REG_RXCSRL, csr);
2450 csr = UREAD1(sc, MUSB2_REG_RXCSRL);
2451 }
2452 UWRITE1(sc, MUSB2_REG_RXCSRL, 0);
2453 }
2454 otgpipe->hw_ep->phase = IDLE;
2455 }
2456 }
2457 xfer->status = USBD_CANCELLED; /* make software ignore it */
2458 wake = xfer->hcflags & UXFER_ABORTWAIT;
2459 xfer->hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT);
2460 usb_transfer_complete(xfer);
2461 if (wake)
2462 cv_broadcast(&xfer->hccv);
2463 }
2464