]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/usb/controller/musb_otg.c
MFC r276798:
[FreeBSD/stable/10.git] / sys / dev / usb / controller / musb_otg.c
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 /*
28  * Thanks to Mentor Graphics for providing a reference driver for this USB chip
29  * at their homepage.
30  */
31
32 /*
33  * This file contains the driver for the Mentor Graphics Inventra USB
34  * 2.0 High Speed Dual-Role controller.
35  *
36  * NOTE: The current implementation only supports Device Side Mode!
37  */
38
39 #ifdef USB_GLOBAL_INCLUDE_FILE
40 #include USB_GLOBAL_INCLUDE_FILE
41 #else
42 #include <sys/stdint.h>
43 #include <sys/stddef.h>
44 #include <sys/param.h>
45 #include <sys/queue.h>
46 #include <sys/types.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/bus.h>
50 #include <sys/module.h>
51 #include <sys/lock.h>
52 #include <sys/mutex.h>
53 #include <sys/condvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/sx.h>
56 #include <sys/unistd.h>
57 #include <sys/callout.h>
58 #include <sys/malloc.h>
59 #include <sys/priv.h>
60
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbdi.h>
63
64 #define USB_DEBUG_VAR musbotgdebug
65
66 #include <dev/usb/usb_core.h>
67 #include <dev/usb/usb_debug.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_process.h>
70 #include <dev/usb/usb_transfer.h>
71 #include <dev/usb/usb_device.h>
72 #include <dev/usb/usb_hub.h>
73 #include <dev/usb/usb_util.h>
74
75 #include <dev/usb/usb_controller.h>
76 #include <dev/usb/usb_bus.h>
77 #endif                  /* USB_GLOBAL_INCLUDE_FILE */
78
79 #include <dev/usb/controller/musb_otg.h>
80
81 #define MUSBOTG_INTR_ENDPT 1
82
83 #define MUSBOTG_BUS2SC(bus) \
84    ((struct musbotg_softc *)(((uint8_t *)(bus)) - \
85    USB_P2U(&(((struct musbotg_softc *)0)->sc_bus))))
86
87 #define MUSBOTG_PC2SC(pc) \
88    MUSBOTG_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus)
89
90 #ifdef USB_DEBUG
91 static int musbotgdebug = 0;
92
93 static SYSCTL_NODE(_hw_usb, OID_AUTO, musbotg, CTLFLAG_RW, 0, "USB musbotg");
94 SYSCTL_INT(_hw_usb_musbotg, OID_AUTO, debug, CTLFLAG_RW,
95     &musbotgdebug, 0, "Debug level");
96 #endif
97
98 #define MAX_NAK_TO      16
99
100 /* prototypes */
101
102 struct usb_bus_methods musbotg_bus_methods;
103 struct usb_pipe_methods musbotg_device_bulk_methods;
104 struct usb_pipe_methods musbotg_device_ctrl_methods;
105 struct usb_pipe_methods musbotg_device_intr_methods;
106 struct usb_pipe_methods musbotg_device_isoc_methods;
107
108 /* Control transfers: Device mode */
109 static musbotg_cmd_t musbotg_dev_ctrl_setup_rx;
110 static musbotg_cmd_t musbotg_dev_ctrl_data_rx;
111 static musbotg_cmd_t musbotg_dev_ctrl_data_tx;
112 static musbotg_cmd_t musbotg_dev_ctrl_status;
113
114 /* Control transfers: Host mode */
115 static musbotg_cmd_t musbotg_host_ctrl_setup_tx;
116 static musbotg_cmd_t musbotg_host_ctrl_data_rx;
117 static musbotg_cmd_t musbotg_host_ctrl_data_tx;
118 static musbotg_cmd_t musbotg_host_ctrl_status_rx;
119 static musbotg_cmd_t musbotg_host_ctrl_status_tx;
120
121 /* Bulk, Interrupt, Isochronous: Device mode */
122 static musbotg_cmd_t musbotg_dev_data_rx;
123 static musbotg_cmd_t musbotg_dev_data_tx;
124
125 /* Bulk, Interrupt, Isochronous: Host mode */
126 static musbotg_cmd_t musbotg_host_data_rx;
127 static musbotg_cmd_t musbotg_host_data_tx;
128
129 static void     musbotg_device_done(struct usb_xfer *, usb_error_t);
130 static void     musbotg_do_poll(struct usb_bus *);
131 static void     musbotg_standard_done(struct usb_xfer *);
132 static void     musbotg_interrupt_poll(struct musbotg_softc *);
133 static void     musbotg_root_intr(struct musbotg_softc *);
134 static int      musbotg_channel_alloc(struct musbotg_softc *, struct musbotg_td *td);
135 static void     musbotg_channel_free(struct musbotg_softc *, struct musbotg_td *td);
136 static void     musbotg_ep_int_set(struct musbotg_softc *sc, int channel, int on);
137
138 /*
139  * Here is a configuration that the chip supports.
140  */
141 static const struct usb_hw_ep_profile musbotg_ep_profile[1] = {
142
143         [0] = {
144                 .max_in_frame_size = 64,/* fixed */
145                 .max_out_frame_size = 64,       /* fixed */
146                 .is_simplex = 1,
147                 .support_control = 1,
148         }
149 };
150
151 static int
152 musbotg_channel_alloc(struct musbotg_softc *sc, struct musbotg_td *td)
153 {
154         int ch;
155         int ep;
156
157         ep = td->ep_no;
158
159         /* In device mode each EP got its own channel */
160         if (sc->sc_mode == MUSB2_DEVICE_MODE) {
161                 musbotg_ep_int_set(sc, ep, 1);
162                 return (ep);
163         }
164
165         /*
166          * All control transactions go through EP0
167          */
168         if (ep == 0) {
169                 if (sc->sc_channel_mask & (1 << 0))
170                         return (-1);
171                 sc->sc_channel_mask |= (1 << 0);
172                 musbotg_ep_int_set(sc, ep, 1);
173                 return (0);
174         }
175
176         for (ch = 1; ch < MUSB2_EP_MAX; ch++) {
177                 if (!(sc->sc_channel_mask & (1 << ch))) {
178                         sc->sc_channel_mask |= (1 << ch);
179                         musbotg_ep_int_set(sc, ch, 1);
180                         return (ch);
181                 }
182         }
183
184         DPRINTFN(-1, "No available channels. Mask: %04x\n",  sc->sc_channel_mask);
185
186         return (-1);
187 }
188
189 static void     
190 musbotg_channel_free(struct musbotg_softc *sc, struct musbotg_td *td)
191 {
192
193         DPRINTFN(1, "ep_no=%d\n", td->channel);
194
195         if (sc->sc_mode == MUSB2_DEVICE_MODE)
196                 return;
197
198         if (td == NULL)
199                 return;
200         if (td->channel == -1)
201                 return;
202
203         musbotg_ep_int_set(sc, td->channel, 0);
204         sc->sc_channel_mask &= ~(1 << td->channel);
205
206         td->channel = -1;
207 }
208
209 static void
210 musbotg_get_hw_ep_profile(struct usb_device *udev,
211     const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
212 {
213         struct musbotg_softc *sc;
214
215         sc = MUSBOTG_BUS2SC(udev->bus);
216
217         if (ep_addr == 0) {
218                 /* control endpoint */
219                 *ppf = musbotg_ep_profile;
220         } else if (ep_addr <= sc->sc_ep_max) {
221                 /* other endpoints */
222                 *ppf = sc->sc_hw_ep_profile + ep_addr;
223         } else {
224                 *ppf = NULL;
225         }
226 }
227
228 static void
229 musbotg_clocks_on(struct musbotg_softc *sc)
230 {
231         if (sc->sc_flags.clocks_off &&
232             sc->sc_flags.port_powered) {
233
234                 DPRINTFN(4, "\n");
235
236                 if (sc->sc_clocks_on) {
237                         (sc->sc_clocks_on) (sc->sc_clocks_arg);
238                 }
239                 sc->sc_flags.clocks_off = 0;
240
241                 /* XXX enable Transceiver */
242         }
243 }
244
245 static void
246 musbotg_clocks_off(struct musbotg_softc *sc)
247 {
248         if (!sc->sc_flags.clocks_off) {
249
250                 DPRINTFN(4, "\n");
251
252                 /* XXX disable Transceiver */
253
254                 if (sc->sc_clocks_off) {
255                         (sc->sc_clocks_off) (sc->sc_clocks_arg);
256                 }
257                 sc->sc_flags.clocks_off = 1;
258         }
259 }
260
261 static void
262 musbotg_pull_common(struct musbotg_softc *sc, uint8_t on)
263 {
264         uint8_t temp;
265
266         temp = MUSB2_READ_1(sc, MUSB2_REG_POWER);
267         if (on)
268                 temp |= MUSB2_MASK_SOFTC;
269         else
270                 temp &= ~MUSB2_MASK_SOFTC;
271
272         MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp);
273 }
274
275 static void
276 musbotg_pull_up(struct musbotg_softc *sc)
277 {
278         /* pullup D+, if possible */
279
280         if (!sc->sc_flags.d_pulled_up &&
281             sc->sc_flags.port_powered) {
282                 sc->sc_flags.d_pulled_up = 1;
283                 musbotg_pull_common(sc, 1);
284         }
285 }
286
287 static void
288 musbotg_pull_down(struct musbotg_softc *sc)
289 {
290         /* pulldown D+, if possible */
291
292         if (sc->sc_flags.d_pulled_up) {
293                 sc->sc_flags.d_pulled_up = 0;
294                 musbotg_pull_common(sc, 0);
295         }
296 }
297
298 static void
299 musbotg_suspend_host(struct musbotg_softc *sc)
300 {
301         uint8_t temp;
302
303         if (sc->sc_flags.status_suspend) {
304                 return;
305         }
306
307         temp = MUSB2_READ_1(sc, MUSB2_REG_POWER);
308         temp |= MUSB2_MASK_SUSPMODE;
309         MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp);
310         sc->sc_flags.status_suspend = 1;
311 }
312
313 static void
314 musbotg_wakeup_host(struct musbotg_softc *sc)
315 {
316         uint8_t temp;
317
318         if (!(sc->sc_flags.status_suspend)) {
319                 return;
320         }
321
322         temp = MUSB2_READ_1(sc, MUSB2_REG_POWER);
323         temp &= ~MUSB2_MASK_SUSPMODE;
324         temp |= MUSB2_MASK_RESUME;
325         MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp);
326
327         /* wait 20 milliseconds */
328         /* Wait for reset to complete. */
329         usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
330
331         temp = MUSB2_READ_1(sc, MUSB2_REG_POWER);
332         temp &= ~MUSB2_MASK_RESUME;
333         MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp);
334
335         sc->sc_flags.status_suspend = 0;
336 }
337
338 static void
339 musbotg_wakeup_peer(struct musbotg_softc *sc)
340 {
341         uint8_t temp;
342
343         if (!(sc->sc_flags.status_suspend)) {
344                 return;
345         }
346
347         temp = MUSB2_READ_1(sc, MUSB2_REG_POWER);
348         temp |= MUSB2_MASK_RESUME;
349         MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp);
350
351         /* wait 8 milliseconds */
352         /* Wait for reset to complete. */
353         usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
354
355         temp = MUSB2_READ_1(sc, MUSB2_REG_POWER);
356         temp &= ~MUSB2_MASK_RESUME;
357         MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp);
358 }
359
360 static void
361 musbotg_set_address(struct musbotg_softc *sc, uint8_t addr)
362 {
363         DPRINTFN(4, "addr=%d\n", addr);
364         addr &= 0x7F;
365         MUSB2_WRITE_1(sc, MUSB2_REG_FADDR, addr);
366 }
367
368 static uint8_t
369 musbotg_dev_ctrl_setup_rx(struct musbotg_td *td)
370 {
371         struct musbotg_softc *sc;
372         struct usb_device_request req;
373         uint16_t count;
374         uint8_t csr;
375
376         /* get pointer to softc */
377         sc = MUSBOTG_PC2SC(td->pc);
378
379         if (td->channel == -1)
380                 td->channel = musbotg_channel_alloc(sc, td);
381
382         /* EP0 is busy, wait */
383         if (td->channel == -1)
384                 return (1);
385
386         DPRINTFN(1, "ep_no=%d\n", td->channel);
387
388         /* select endpoint 0 */
389         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
390
391         /* read out FIFO status */
392         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
393
394         DPRINTFN(4, "csr=0x%02x\n", csr);
395
396         /*
397          * NOTE: If DATAEND is set we should not call the
398          * callback, hence the status stage is not complete.
399          */
400         if (csr & MUSB2_MASK_CSR0L_DATAEND) {
401                 /* do not stall at this point */
402                 td->did_stall = 1;
403                 /* wait for interrupt */
404                 DPRINTFN(1, "CSR0 DATAEND\n");
405                 goto not_complete;
406         }
407
408         if (csr & MUSB2_MASK_CSR0L_SENTSTALL) {
409                 /* clear SENTSTALL */
410                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
411                 /* get latest status */
412                 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
413                 /* update EP0 state */
414                 sc->sc_ep0_busy = 0;
415         }
416         if (csr & MUSB2_MASK_CSR0L_SETUPEND) {
417                 /* clear SETUPEND */
418                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
419                     MUSB2_MASK_CSR0L_SETUPEND_CLR);
420                 /* get latest status */
421                 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
422                 /* update EP0 state */
423                 sc->sc_ep0_busy = 0;
424         }
425         if (sc->sc_ep0_busy) {
426                 DPRINTFN(1, "EP0 BUSY\n");
427                 goto not_complete;
428         }
429         if (!(csr & MUSB2_MASK_CSR0L_RXPKTRDY)) {
430                 goto not_complete;
431         }
432         /* get the packet byte count */
433         count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT);
434
435         /* verify data length */
436         if (count != td->remainder) {
437                 DPRINTFN(1, "Invalid SETUP packet "
438                     "length, %d bytes\n", count);
439                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
440                       MUSB2_MASK_CSR0L_RXPKTRDY_CLR);
441                 /* don't clear stall */
442                 td->did_stall = 1;
443                 goto not_complete;
444         }
445         if (count != sizeof(req)) {
446                 DPRINTFN(1, "Unsupported SETUP packet "
447                     "length, %d bytes\n", count);
448                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
449                       MUSB2_MASK_CSR0L_RXPKTRDY_CLR);
450                 /* don't clear stall */
451                 td->did_stall = 1;
452                 goto not_complete;
453         }
454         /* clear did stall flag */
455         td->did_stall = 0;
456
457         /* receive data */
458         bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
459             MUSB2_REG_EPFIFO(0), (void *)&req, sizeof(req));
460
461         /* copy data into real buffer */
462         usbd_copy_in(td->pc, 0, &req, sizeof(req));
463
464         td->offset = sizeof(req);
465         td->remainder = 0;
466
467         /* set pending command */
468         sc->sc_ep0_cmd = MUSB2_MASK_CSR0L_RXPKTRDY_CLR;
469
470         /* we need set stall or dataend after this */
471         sc->sc_ep0_busy = 1;
472
473         /* sneak peek the set address */
474         if ((req.bmRequestType == UT_WRITE_DEVICE) &&
475             (req.bRequest == UR_SET_ADDRESS)) {
476                 sc->sc_dv_addr = req.wValue[0] & 0x7F;
477         } else {
478                 sc->sc_dv_addr = 0xFF;
479         }
480
481         musbotg_channel_free(sc, td);
482         return (0);                     /* complete */
483
484 not_complete:
485         /* abort any ongoing transfer */
486         if (!td->did_stall) {
487                 DPRINTFN(4, "stalling\n");
488                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
489                     MUSB2_MASK_CSR0L_SENDSTALL);
490                 td->did_stall = 1;
491         }
492         return (1);                     /* not complete */
493 }
494
495 static uint8_t
496 musbotg_host_ctrl_setup_tx(struct musbotg_td *td)
497 {
498         struct musbotg_softc *sc;
499         struct usb_device_request req;
500         uint8_t csr, csrh;
501
502         /* get pointer to softc */
503         sc = MUSBOTG_PC2SC(td->pc);
504
505         if (td->channel == -1)
506                 td->channel = musbotg_channel_alloc(sc, td);
507
508         /* EP0 is busy, wait */
509         if (td->channel == -1)
510                 return (1);
511
512         DPRINTFN(1, "ep_no=%d\n", td->channel);
513
514         /* select endpoint 0 */
515         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
516
517         /* read out FIFO status */
518         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
519         DPRINTFN(4, "csr=0x%02x\n", csr);
520
521         /* Not ready yet yet */
522         if (csr & MUSB2_MASK_CSR0L_TXPKTRDY)
523                 return (1);
524
525         /* Failed */
526         if (csr & (MUSB2_MASK_CSR0L_RXSTALL |
527             MUSB2_MASK_CSR0L_ERROR))
528         {
529                 /* Clear status bit */
530                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
531                 DPRINTFN(1, "error bit set, csr=0x%02x\n", csr);
532                 td->error = 1;
533         }
534
535         if (csr & MUSB2_MASK_CSR0L_NAKTIMO) {
536                 DPRINTFN(1, "NAK timeout\n");
537
538                 if (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY) {
539                         csrh = MUSB2_READ_1(sc, MUSB2_REG_TXCSRH);
540                         csrh |= MUSB2_MASK_CSR0H_FFLUSH;
541                         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, csrh);
542                         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
543                         if (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY) {
544                                 csrh = MUSB2_READ_1(sc, MUSB2_REG_TXCSRH);
545                                 csrh |= MUSB2_MASK_CSR0H_FFLUSH;
546                                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, csrh);
547                                 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
548                         }
549                 }
550
551                 csr &= ~MUSB2_MASK_CSR0L_NAKTIMO;
552                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
553
554                 td->error = 1;
555         }
556
557         if (td->error) {
558                 musbotg_channel_free(sc, td);
559                 return (0);
560         }
561
562         /* Fifo is not empty and there is no NAK timeout */
563         if (csr & MUSB2_MASK_CSR0L_TXPKTRDY)
564                 return (1);
565
566         /* check if we are complete */
567         if (td->remainder == 0) {
568                 /* we are complete */
569                 musbotg_channel_free(sc, td);
570                 return (0);
571         }
572
573         /* copy data into real buffer */
574         usbd_copy_out(td->pc, 0, &req, sizeof(req));
575
576         /* send data */
577         bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
578             MUSB2_REG_EPFIFO(0), (void *)&req, sizeof(req));
579
580         /* update offset and remainder */
581         td->offset += sizeof(req);
582         td->remainder -= sizeof(req);
583
584
585         MUSB2_WRITE_1(sc, MUSB2_REG_TXNAKLIMIT, MAX_NAK_TO);
586         MUSB2_WRITE_1(sc, MUSB2_REG_TXFADDR(0), td->dev_addr);
587         MUSB2_WRITE_1(sc, MUSB2_REG_TXHADDR(0), td->haddr);
588         MUSB2_WRITE_1(sc, MUSB2_REG_TXHUBPORT(0), td->hport);
589         MUSB2_WRITE_1(sc, MUSB2_REG_TXTI, td->transfer_type);
590
591         /* write command */
592         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
593             MUSB2_MASK_CSR0L_TXPKTRDY | 
594             MUSB2_MASK_CSR0L_SETUPPKT);
595
596         /* Just to be consistent, not used above */
597         td->transaction_started = 1;
598
599         return (1);                     /* in progress */
600 }
601
602 /* Control endpoint only data handling functions (RX/TX/SYNC) */
603
604 static uint8_t
605 musbotg_dev_ctrl_data_rx(struct musbotg_td *td)
606 {
607         struct usb_page_search buf_res;
608         struct musbotg_softc *sc;
609         uint16_t count;
610         uint8_t csr;
611         uint8_t got_short;
612
613         /* get pointer to softc */
614         sc = MUSBOTG_PC2SC(td->pc);
615
616         /* select endpoint 0 */
617         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
618
619         /* check if a command is pending */
620         if (sc->sc_ep0_cmd) {
621                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, sc->sc_ep0_cmd);
622                 sc->sc_ep0_cmd = 0;
623         }
624         /* read out FIFO status */
625         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
626
627         DPRINTFN(4, "csr=0x%02x\n", csr);
628
629         got_short = 0;
630
631         if (csr & (MUSB2_MASK_CSR0L_SETUPEND |
632             MUSB2_MASK_CSR0L_SENTSTALL)) {
633                 if (td->remainder == 0) {
634                         /*
635                          * We are actually complete and have
636                          * received the next SETUP
637                          */
638                         DPRINTFN(4, "faking complete\n");
639                         return (0);     /* complete */
640                 }
641                 /*
642                  * USB Host Aborted the transfer.
643                  */
644                 td->error = 1;
645                 return (0);             /* complete */
646         }
647         if (!(csr & MUSB2_MASK_CSR0L_RXPKTRDY)) {
648                 return (1);             /* not complete */
649         }
650         /* get the packet byte count */
651         count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT);
652
653         /* verify the packet byte count */
654         if (count != td->max_frame_size) {
655                 if (count < td->max_frame_size) {
656                         /* we have a short packet */
657                         td->short_pkt = 1;
658                         got_short = 1;
659                 } else {
660                         /* invalid USB packet */
661                         td->error = 1;
662                         return (0);     /* we are complete */
663                 }
664         }
665         /* verify the packet byte count */
666         if (count > td->remainder) {
667                 /* invalid USB packet */
668                 td->error = 1;
669                 return (0);             /* we are complete */
670         }
671         while (count > 0) {
672                 uint32_t temp;
673
674                 usbd_get_page(td->pc, td->offset, &buf_res);
675
676                 /* get correct length */
677                 if (buf_res.length > count) {
678                         buf_res.length = count;
679                 }
680                 /* check for unaligned memory address */
681                 if (USB_P2U(buf_res.buffer) & 3) {
682
683                         temp = count & ~3;
684
685                         if (temp) {
686                                 /* receive data 4 bytes at a time */
687                                 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
688                                     MUSB2_REG_EPFIFO(0), sc->sc_bounce_buf,
689                                     temp / 4);
690                         }
691                         temp = count & 3;
692                         if (temp) {
693                                 /* receive data 1 byte at a time */
694                                 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
695                                     MUSB2_REG_EPFIFO(0),
696                                     (void *)(&sc->sc_bounce_buf[count / 4]), temp);
697                         }
698                         usbd_copy_in(td->pc, td->offset,
699                             sc->sc_bounce_buf, count);
700
701                         /* update offset and remainder */
702                         td->offset += count;
703                         td->remainder -= count;
704                         break;
705                 }
706                 /* check if we can optimise */
707                 if (buf_res.length >= 4) {
708
709                         /* receive data 4 bytes at a time */
710                         bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
711                             MUSB2_REG_EPFIFO(0), buf_res.buffer,
712                             buf_res.length / 4);
713
714                         temp = buf_res.length & ~3;
715
716                         /* update counters */
717                         count -= temp;
718                         td->offset += temp;
719                         td->remainder -= temp;
720                         continue;
721                 }
722                 /* receive data */
723                 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
724                     MUSB2_REG_EPFIFO(0), buf_res.buffer, buf_res.length);
725
726                 /* update counters */
727                 count -= buf_res.length;
728                 td->offset += buf_res.length;
729                 td->remainder -= buf_res.length;
730         }
731
732         /* check if we are complete */
733         if ((td->remainder == 0) || got_short) {
734                 if (td->short_pkt) {
735                         /* we are complete */
736                         sc->sc_ep0_cmd = MUSB2_MASK_CSR0L_RXPKTRDY_CLR;
737                         return (0);
738                 }
739                 /* else need to receive a zero length packet */
740         }
741         /* write command - need more data */
742         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
743             MUSB2_MASK_CSR0L_RXPKTRDY_CLR);
744         return (1);                     /* not complete */
745 }
746
747 static uint8_t
748 musbotg_dev_ctrl_data_tx(struct musbotg_td *td)
749 {
750         struct usb_page_search buf_res;
751         struct musbotg_softc *sc;
752         uint16_t count;
753         uint8_t csr;
754
755         /* get pointer to softc */
756         sc = MUSBOTG_PC2SC(td->pc);
757
758         /* select endpoint 0 */
759         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
760
761         /* check if a command is pending */
762         if (sc->sc_ep0_cmd) {
763                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, sc->sc_ep0_cmd);
764                 sc->sc_ep0_cmd = 0;
765         }
766         /* read out FIFO status */
767         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
768
769         DPRINTFN(4, "csr=0x%02x\n", csr);
770
771         if (csr & (MUSB2_MASK_CSR0L_SETUPEND |
772             MUSB2_MASK_CSR0L_SENTSTALL)) {
773                 /*
774                  * The current transfer was aborted
775                  * by the USB Host
776                  */
777                 td->error = 1;
778                 return (0);             /* complete */
779         }
780         if (csr & MUSB2_MASK_CSR0L_TXPKTRDY) {
781                 return (1);             /* not complete */
782         }
783         count = td->max_frame_size;
784         if (td->remainder < count) {
785                 /* we have a short packet */
786                 td->short_pkt = 1;
787                 count = td->remainder;
788         }
789         while (count > 0) {
790                 uint32_t temp;
791
792                 usbd_get_page(td->pc, td->offset, &buf_res);
793
794                 /* get correct length */
795                 if (buf_res.length > count) {
796                         buf_res.length = count;
797                 }
798                 /* check for unaligned memory address */
799                 if (USB_P2U(buf_res.buffer) & 3) {
800
801                         usbd_copy_out(td->pc, td->offset,
802                             sc->sc_bounce_buf, count);
803
804                         temp = count & ~3;
805
806                         if (temp) {
807                                 /* transmit data 4 bytes at a time */
808                                 bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
809                                     MUSB2_REG_EPFIFO(0), sc->sc_bounce_buf,
810                                     temp / 4);
811                         }
812                         temp = count & 3;
813                         if (temp) {
814                                 /* receive data 1 byte at a time */
815                                 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
816                                     MUSB2_REG_EPFIFO(0),
817                                     ((void *)&sc->sc_bounce_buf[count / 4]), temp);
818                         }
819                         /* update offset and remainder */
820                         td->offset += count;
821                         td->remainder -= count;
822                         break;
823                 }
824                 /* check if we can optimise */
825                 if (buf_res.length >= 4) {
826
827                         /* transmit data 4 bytes at a time */
828                         bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
829                             MUSB2_REG_EPFIFO(0), buf_res.buffer,
830                             buf_res.length / 4);
831
832                         temp = buf_res.length & ~3;
833
834                         /* update counters */
835                         count -= temp;
836                         td->offset += temp;
837                         td->remainder -= temp;
838                         continue;
839                 }
840                 /* transmit data */
841                 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
842                     MUSB2_REG_EPFIFO(0), buf_res.buffer, buf_res.length);
843
844                 /* update counters */
845                 count -= buf_res.length;
846                 td->offset += buf_res.length;
847                 td->remainder -= buf_res.length;
848         }
849
850         /* check remainder */
851         if (td->remainder == 0) {
852                 if (td->short_pkt) {
853                         sc->sc_ep0_cmd = MUSB2_MASK_CSR0L_TXPKTRDY;
854                         return (0);     /* complete */
855                 }
856                 /* else we need to transmit a short packet */
857         }
858         /* write command */
859         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
860             MUSB2_MASK_CSR0L_TXPKTRDY);
861
862         return (1);                     /* not complete */
863 }
864
865 static uint8_t
866 musbotg_host_ctrl_data_rx(struct musbotg_td *td)
867 {
868         struct usb_page_search buf_res;
869         struct musbotg_softc *sc;
870         uint16_t count;
871         uint8_t csr;
872         uint8_t got_short;
873
874         /* get pointer to softc */
875         sc = MUSBOTG_PC2SC(td->pc);
876
877         if (td->channel == -1)
878                 td->channel = musbotg_channel_alloc(sc, td);
879
880         /* EP0 is busy, wait */
881         if (td->channel == -1)
882                 return (1);
883
884         DPRINTFN(1, "ep_no=%d\n", td->channel);
885
886         /* select endpoint 0 */
887         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
888
889         /* read out FIFO status */
890         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
891
892         DPRINTFN(4, "csr=0x%02x\n", csr);
893
894         got_short = 0;
895         if (!td->transaction_started) {
896                 td->transaction_started = 1;
897
898                 MUSB2_WRITE_1(sc, MUSB2_REG_RXNAKLIMIT, MAX_NAK_TO);
899
900                 MUSB2_WRITE_1(sc, MUSB2_REG_RXFADDR(0),
901                     td->dev_addr);
902                 MUSB2_WRITE_1(sc, MUSB2_REG_RXHADDR(0), td->haddr);
903                 MUSB2_WRITE_1(sc, MUSB2_REG_RXHUBPORT(0), td->hport);
904                 MUSB2_WRITE_1(sc, MUSB2_REG_RXTI, td->transfer_type);
905
906                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
907                     MUSB2_MASK_CSR0L_REQPKT);
908
909                 return (1);
910         }
911
912         if (csr & MUSB2_MASK_CSR0L_NAKTIMO) {
913                 csr &= ~MUSB2_MASK_CSR0L_REQPKT;
914                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
915
916                 csr &= ~MUSB2_MASK_CSR0L_NAKTIMO;
917                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
918
919                 td->error = 1;
920         }
921
922         /* Failed */
923         if (csr & (MUSB2_MASK_CSR0L_RXSTALL |
924             MUSB2_MASK_CSR0L_ERROR))
925         {
926                 /* Clear status bit */
927                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
928                 DPRINTFN(1, "error bit set, csr=0x%02x\n", csr);
929                 td->error = 1;
930         }
931
932         if (td->error) {
933                 musbotg_channel_free(sc, td);
934                 return (0);     /* we are complete */
935         }
936
937         if (!(csr & MUSB2_MASK_CSR0L_RXPKTRDY))
938                 return (1); /* not yet */
939
940         /* get the packet byte count */
941         count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT);
942
943         /* verify the packet byte count */
944         if (count != td->max_frame_size) {
945                 if (count < td->max_frame_size) {
946                         /* we have a short packet */
947                         td->short_pkt = 1;
948                         got_short = 1;
949                 } else {
950                         /* invalid USB packet */
951                         td->error = 1;
952                         musbotg_channel_free(sc, td);
953                         return (0);     /* we are complete */
954                 }
955         }
956         /* verify the packet byte count */
957         if (count > td->remainder) {
958                 /* invalid USB packet */
959                 td->error = 1;
960                 musbotg_channel_free(sc, td);
961                 return (0);             /* we are complete */
962         }
963         while (count > 0) {
964                 uint32_t temp;
965
966                 usbd_get_page(td->pc, td->offset, &buf_res);
967
968                 /* get correct length */
969                 if (buf_res.length > count) {
970                         buf_res.length = count;
971                 }
972                 /* check for unaligned memory address */
973                 if (USB_P2U(buf_res.buffer) & 3) {
974
975                         temp = count & ~3;
976
977                         if (temp) {
978                                 /* receive data 4 bytes at a time */
979                                 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
980                                     MUSB2_REG_EPFIFO(0), sc->sc_bounce_buf,
981                                     temp / 4);
982                         }
983                         temp = count & 3;
984                         if (temp) {
985                                 /* receive data 1 byte at a time */
986                                 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
987                                     MUSB2_REG_EPFIFO(0),
988                                     (void *)(&sc->sc_bounce_buf[count / 4]), temp);
989                         }
990                         usbd_copy_in(td->pc, td->offset,
991                             sc->sc_bounce_buf, count);
992
993                         /* update offset and remainder */
994                         td->offset += count;
995                         td->remainder -= count;
996                         break;
997                 }
998                 /* check if we can optimise */
999                 if (buf_res.length >= 4) {
1000
1001                         /* receive data 4 bytes at a time */
1002                         bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
1003                             MUSB2_REG_EPFIFO(0), buf_res.buffer,
1004                             buf_res.length / 4);
1005
1006                         temp = buf_res.length & ~3;
1007
1008                         /* update counters */
1009                         count -= temp;
1010                         td->offset += temp;
1011                         td->remainder -= temp;
1012                         continue;
1013                 }
1014                 /* receive data */
1015                 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
1016                     MUSB2_REG_EPFIFO(0), buf_res.buffer, buf_res.length);
1017
1018                 /* update counters */
1019                 count -= buf_res.length;
1020                 td->offset += buf_res.length;
1021                 td->remainder -= buf_res.length;
1022         }
1023
1024         csr &= ~MUSB2_MASK_CSR0L_RXPKTRDY;
1025         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
1026
1027         /* check if we are complete */
1028         if ((td->remainder == 0) || got_short) {
1029                 if (td->short_pkt) {
1030                         /* we are complete */
1031
1032                         musbotg_channel_free(sc, td);
1033                         return (0);
1034                 }
1035                 /* else need to receive a zero length packet */
1036         }
1037
1038         td->transaction_started = 1;
1039         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
1040             MUSB2_MASK_CSR0L_REQPKT);
1041
1042         return (1);                     /* not complete */
1043 }
1044
1045 static uint8_t
1046 musbotg_host_ctrl_data_tx(struct musbotg_td *td)
1047 {
1048         struct usb_page_search buf_res;
1049         struct musbotg_softc *sc;
1050         uint16_t count;
1051         uint8_t csr, csrh;
1052
1053         /* get pointer to softc */
1054         sc = MUSBOTG_PC2SC(td->pc);
1055
1056         if (td->channel == -1)
1057                 td->channel = musbotg_channel_alloc(sc, td);
1058
1059         /* No free EPs */
1060         if (td->channel == -1)
1061                 return (1);
1062
1063         DPRINTFN(1, "ep_no=%d\n", td->channel);
1064
1065         /* select endpoint */
1066         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
1067
1068         /* read out FIFO status */
1069         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1070         DPRINTFN(4, "csr=0x%02x\n", csr);
1071
1072         if (csr & (MUSB2_MASK_CSR0L_RXSTALL |
1073             MUSB2_MASK_CSR0L_ERROR)) {
1074                 /* clear status bits */
1075                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
1076                 td->error = 1;
1077         }
1078
1079         if (csr & MUSB2_MASK_CSR0L_NAKTIMO ) {
1080
1081                 if (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY) {
1082                         csrh = MUSB2_READ_1(sc, MUSB2_REG_TXCSRH);
1083                         csrh |= MUSB2_MASK_CSR0H_FFLUSH;
1084                         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, csrh);
1085                         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1086                         if (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY) {
1087                                 csrh = MUSB2_READ_1(sc, MUSB2_REG_TXCSRH);
1088                                 csrh |= MUSB2_MASK_CSR0H_FFLUSH;
1089                                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, csrh);
1090                                 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1091                         }
1092                 }
1093
1094                 csr &= ~MUSB2_MASK_CSR0L_NAKTIMO;
1095                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
1096
1097                 td->error = 1;
1098         }
1099
1100
1101         if (td->error) {
1102                 musbotg_channel_free(sc, td);
1103                 return (0);     /* complete */
1104         }
1105
1106         /*
1107          * Wait while FIFO is empty. 
1108          * Do not flush it because it will cause transactions
1109          * with size more then packet size. It might upset
1110          * some devices
1111          */
1112         if (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY)
1113                 return (1);
1114
1115         /* Packet still being processed */
1116         if (csr & MUSB2_MASK_CSR0L_TXPKTRDY)
1117                 return (1);
1118
1119         if (td->transaction_started) {
1120                 /* check remainder */
1121                 if (td->remainder == 0) {
1122                         if (td->short_pkt) {
1123                                 musbotg_channel_free(sc, td);
1124                                 return (0);     /* complete */
1125                         }
1126                         /* else we need to transmit a short packet */
1127                 }
1128
1129                 /* We're not complete - more transactions required */
1130                 td->transaction_started = 0;
1131         }
1132
1133         /* check for short packet */
1134         count = td->max_frame_size;
1135         if (td->remainder < count) {
1136                 /* we have a short packet */
1137                 td->short_pkt = 1;
1138                 count = td->remainder;
1139         }
1140
1141         while (count > 0) {
1142                 uint32_t temp;
1143
1144                 usbd_get_page(td->pc, td->offset, &buf_res);
1145
1146                 /* get correct length */
1147                 if (buf_res.length > count) {
1148                         buf_res.length = count;
1149                 }
1150                 /* check for unaligned memory address */
1151                 if (USB_P2U(buf_res.buffer) & 3) {
1152
1153                         usbd_copy_out(td->pc, td->offset,
1154                             sc->sc_bounce_buf, count);
1155
1156                         temp = count & ~3;
1157
1158                         if (temp) {
1159                                 /* transmit data 4 bytes at a time */
1160                                 bus_space_write_multi_4(sc->sc_io_tag,
1161                                     sc->sc_io_hdl, MUSB2_REG_EPFIFO(0),
1162                                     sc->sc_bounce_buf, temp / 4);
1163                         }
1164                         temp = count & 3;
1165                         if (temp) {
1166                                 /* receive data 1 byte at a time */
1167                                 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
1168                                     MUSB2_REG_EPFIFO(0),
1169                                     ((void *)&sc->sc_bounce_buf[count / 4]), temp);
1170                         }
1171                         /* update offset and remainder */
1172                         td->offset += count;
1173                         td->remainder -= count;
1174                         break;
1175                 }
1176                 /* check if we can optimise */
1177                 if (buf_res.length >= 4) {
1178
1179                         /* transmit data 4 bytes at a time */
1180                         bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
1181                             MUSB2_REG_EPFIFO(0), buf_res.buffer,
1182                             buf_res.length / 4);
1183
1184                         temp = buf_res.length & ~3;
1185
1186                         /* update counters */
1187                         count -= temp;
1188                         td->offset += temp;
1189                         td->remainder -= temp;
1190                         continue;
1191                 }
1192                 /* transmit data */
1193                 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
1194                     MUSB2_REG_EPFIFO(0), buf_res.buffer,
1195                     buf_res.length);
1196
1197                 /* update counters */
1198                 count -= buf_res.length;
1199                 td->offset += buf_res.length;
1200                 td->remainder -= buf_res.length;
1201         }
1202
1203         /* Function address */
1204         MUSB2_WRITE_1(sc, MUSB2_REG_TXFADDR(0), td->dev_addr);
1205         MUSB2_WRITE_1(sc, MUSB2_REG_TXHADDR(0), td->haddr);
1206         MUSB2_WRITE_1(sc, MUSB2_REG_TXHUBPORT(0), td->hport);
1207         MUSB2_WRITE_1(sc, MUSB2_REG_TXTI, td->transfer_type);
1208
1209         /* TX NAK timeout */
1210         MUSB2_WRITE_1(sc, MUSB2_REG_TXNAKLIMIT, MAX_NAK_TO);
1211
1212         /* write command */
1213         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
1214             MUSB2_MASK_CSR0L_TXPKTRDY);
1215
1216         td->transaction_started = 1;
1217
1218         return (1);                     /* not complete */
1219 }
1220
1221 static uint8_t
1222 musbotg_dev_ctrl_status(struct musbotg_td *td)
1223 {
1224         struct musbotg_softc *sc;
1225         uint8_t csr;
1226
1227         /* get pointer to softc */
1228         sc = MUSBOTG_PC2SC(td->pc);
1229
1230         /* select endpoint 0 */
1231         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
1232
1233         if (sc->sc_ep0_busy) {
1234                 sc->sc_ep0_busy = 0;
1235                 sc->sc_ep0_cmd |= MUSB2_MASK_CSR0L_DATAEND;
1236                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, sc->sc_ep0_cmd);
1237                 sc->sc_ep0_cmd = 0;
1238         }
1239         /* read out FIFO status */
1240         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1241
1242         DPRINTFN(4, "csr=0x%02x\n", csr);
1243
1244         if (csr & MUSB2_MASK_CSR0L_DATAEND) {
1245                 /* wait for interrupt */
1246                 return (1);             /* not complete */
1247         }
1248         if (sc->sc_dv_addr != 0xFF) {
1249                 /* write function address */
1250                 musbotg_set_address(sc, sc->sc_dv_addr);
1251         }
1252
1253         musbotg_channel_free(sc, td);
1254         return (0);                     /* complete */
1255 }
1256
1257 static uint8_t
1258 musbotg_host_ctrl_status_rx(struct musbotg_td *td)
1259 {
1260         struct musbotg_softc *sc;
1261         uint8_t csr, csrh;
1262
1263         /* get pointer to softc */
1264         sc = MUSBOTG_PC2SC(td->pc);
1265
1266         if (td->channel == -1)
1267                 td->channel = musbotg_channel_alloc(sc, td);
1268
1269         /* EP0 is busy, wait */
1270         if (td->channel == -1)
1271                 return (1);
1272
1273         DPRINTFN(1, "ep_no=%d\n", td->channel);
1274
1275         /* select endpoint 0 */
1276         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
1277
1278         if (!td->transaction_started) {
1279                 MUSB2_WRITE_1(sc, MUSB2_REG_RXFADDR(0),
1280                     td->dev_addr);
1281
1282                 MUSB2_WRITE_1(sc, MUSB2_REG_RXHADDR(0), td->haddr);
1283                 MUSB2_WRITE_1(sc, MUSB2_REG_RXHUBPORT(0), td->hport);
1284                 MUSB2_WRITE_1(sc, MUSB2_REG_RXTI, td->transfer_type);
1285
1286                 /* RX NAK timeout */
1287                 MUSB2_WRITE_1(sc, MUSB2_REG_RXNAKLIMIT, MAX_NAK_TO);
1288
1289                 td->transaction_started = 1;
1290
1291                 /* Disable PING */
1292                 csrh = MUSB2_READ_1(sc, MUSB2_REG_RXCSRH);
1293                 csrh |= MUSB2_MASK_CSR0H_PING_DIS;
1294                 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH, csrh);
1295
1296                 /* write command */
1297                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
1298                     MUSB2_MASK_CSR0L_STATUSPKT | 
1299                     MUSB2_MASK_CSR0L_REQPKT);
1300
1301                 return (1); /* Just started */
1302
1303         }
1304
1305         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1306
1307         DPRINTFN(4, "IN STATUS csr=0x%02x\n", csr);
1308
1309         if (csr & MUSB2_MASK_CSR0L_RXPKTRDY) {
1310                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
1311                     MUSB2_MASK_CSR0L_RXPKTRDY_CLR);
1312                 musbotg_channel_free(sc, td);
1313                 return (0); /* complete */
1314         }
1315
1316         if (csr & MUSB2_MASK_CSR0L_NAKTIMO) {
1317                 csr &= ~ (MUSB2_MASK_CSR0L_STATUSPKT |
1318                     MUSB2_MASK_CSR0L_REQPKT);
1319                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
1320
1321                 csr &= ~MUSB2_MASK_CSR0L_NAKTIMO;
1322                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
1323                 td->error = 1;
1324         }
1325
1326         /* Failed */
1327         if (csr & (MUSB2_MASK_CSR0L_RXSTALL |
1328             MUSB2_MASK_CSR0L_ERROR))
1329         {
1330                 /* Clear status bit */
1331                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
1332                 DPRINTFN(1, "error bit set, csr=0x%02x\n", csr);
1333                 td->error = 1;
1334         }
1335
1336         if (td->error) {
1337                 musbotg_channel_free(sc, td);
1338                 return (0);
1339         }
1340
1341         return (1);                     /* Not ready yet */
1342 }
1343
1344 static uint8_t
1345 musbotg_host_ctrl_status_tx(struct musbotg_td *td)
1346 {
1347         struct musbotg_softc *sc;
1348         uint8_t csr;
1349
1350         /* get pointer to softc */
1351         sc = MUSBOTG_PC2SC(td->pc);
1352
1353         if (td->channel == -1)
1354                 td->channel = musbotg_channel_alloc(sc, td);
1355
1356         /* EP0 is busy, wait */
1357         if (td->channel == -1)
1358                 return (1);
1359
1360         DPRINTFN(1, "ep_no=%d/%d [%d@%d.%d/%02x]\n", td->channel, td->transaction_started, 
1361                         td->dev_addr,td->haddr,td->hport, td->transfer_type);
1362
1363         /* select endpoint 0 */
1364         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
1365
1366         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1367         DPRINTFN(4, "csr=0x%02x\n", csr);
1368
1369         /* Not yet */
1370         if (csr & MUSB2_MASK_CSR0L_TXPKTRDY)
1371                 return (1);
1372
1373         /* Failed */
1374         if (csr & (MUSB2_MASK_CSR0L_RXSTALL |
1375             MUSB2_MASK_CSR0L_ERROR))
1376         {
1377                 /* Clear status bit */
1378                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
1379                 DPRINTFN(1, "error bit set, csr=0x%02x\n", csr);
1380                 td->error = 1;
1381                 musbotg_channel_free(sc, td);
1382                 return (0); /* complete */
1383         }
1384
1385         if (td->transaction_started) {
1386                 musbotg_channel_free(sc, td);
1387                 return (0); /* complete */
1388         } 
1389
1390         MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH, MUSB2_MASK_CSR0H_PING_DIS);
1391
1392         MUSB2_WRITE_1(sc, MUSB2_REG_TXFADDR(0), td->dev_addr);
1393         MUSB2_WRITE_1(sc, MUSB2_REG_TXHADDR(0), td->haddr);
1394         MUSB2_WRITE_1(sc, MUSB2_REG_TXHUBPORT(0), td->hport);
1395         MUSB2_WRITE_1(sc, MUSB2_REG_TXTI, td->transfer_type);
1396
1397         /* TX NAK timeout */
1398         MUSB2_WRITE_1(sc, MUSB2_REG_TXNAKLIMIT, MAX_NAK_TO);
1399
1400         td->transaction_started = 1;
1401
1402         /* write command */
1403         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
1404             MUSB2_MASK_CSR0L_STATUSPKT | 
1405             MUSB2_MASK_CSR0L_TXPKTRDY);
1406
1407         return (1);                     /* wait for interrupt */
1408 }
1409
1410 static uint8_t
1411 musbotg_dev_data_rx(struct musbotg_td *td)
1412 {
1413         struct usb_page_search buf_res;
1414         struct musbotg_softc *sc;
1415         uint16_t count;
1416         uint8_t csr;
1417         uint8_t to;
1418         uint8_t got_short;
1419
1420         to = 8;                         /* don't loop forever! */
1421         got_short = 0;
1422
1423         /* get pointer to softc */
1424         sc = MUSBOTG_PC2SC(td->pc);
1425
1426         if (td->channel == -1)
1427                 td->channel = musbotg_channel_alloc(sc, td);
1428
1429         /* EP0 is busy, wait */
1430         if (td->channel == -1)
1431                 return (1);
1432
1433         /* select endpoint */
1434         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, td->channel);
1435
1436 repeat:
1437         /* read out FIFO status */
1438         csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL);
1439
1440         DPRINTFN(4, "csr=0x%02x\n", csr);
1441
1442         /* clear overrun */
1443         if (csr & MUSB2_MASK_CSRL_RXOVERRUN) {
1444                 /* make sure we don't clear "RXPKTRDY" */
1445                 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL,
1446                     MUSB2_MASK_CSRL_RXPKTRDY);
1447         }
1448
1449         /* check status */
1450         if (!(csr & MUSB2_MASK_CSRL_RXPKTRDY))
1451                 return (1); /* not complete */
1452
1453         /* get the packet byte count */
1454         count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT);
1455
1456         DPRINTFN(4, "count=0x%04x\n", count);
1457
1458         /*
1459          * Check for short or invalid packet:
1460          */
1461         if (count != td->max_frame_size) {
1462                 if (count < td->max_frame_size) {
1463                         /* we have a short packet */
1464                         td->short_pkt = 1;
1465                         got_short = 1;
1466                 } else {
1467                         /* invalid USB packet */
1468                         td->error = 1;
1469                         musbotg_channel_free(sc, td);
1470                         return (0);     /* we are complete */
1471                 }
1472         }
1473         /* verify the packet byte count */
1474         if (count > td->remainder) {
1475                 /* invalid USB packet */
1476                 td->error = 1;
1477                 musbotg_channel_free(sc, td);
1478                 return (0);             /* we are complete */
1479         }
1480         while (count > 0) {
1481                 uint32_t temp;
1482
1483                 usbd_get_page(td->pc, td->offset, &buf_res);
1484
1485                 /* get correct length */
1486                 if (buf_res.length > count) {
1487                         buf_res.length = count;
1488                 }
1489                 /* check for unaligned memory address */
1490                 if (USB_P2U(buf_res.buffer) & 3) {
1491
1492                         temp = count & ~3;
1493
1494                         if (temp) {
1495                                 /* receive data 4 bytes at a time */
1496                                 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
1497                                     MUSB2_REG_EPFIFO(td->channel), sc->sc_bounce_buf,
1498                                     temp / 4);
1499                         }
1500                         temp = count & 3;
1501                         if (temp) {
1502                                 /* receive data 1 byte at a time */
1503                                 bus_space_read_multi_1(sc->sc_io_tag,
1504                                     sc->sc_io_hdl, MUSB2_REG_EPFIFO(td->channel),
1505                                     ((void *)&sc->sc_bounce_buf[count / 4]), temp);
1506                         }
1507                         usbd_copy_in(td->pc, td->offset,
1508                             sc->sc_bounce_buf, count);
1509
1510                         /* update offset and remainder */
1511                         td->offset += count;
1512                         td->remainder -= count;
1513                         break;
1514                 }
1515                 /* check if we can optimise */
1516                 if (buf_res.length >= 4) {
1517
1518                         /* receive data 4 bytes at a time */
1519                         bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
1520                             MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
1521                             buf_res.length / 4);
1522
1523                         temp = buf_res.length & ~3;
1524
1525                         /* update counters */
1526                         count -= temp;
1527                         td->offset += temp;
1528                         td->remainder -= temp;
1529                         continue;
1530                 }
1531                 /* receive data */
1532                 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
1533                     MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
1534                     buf_res.length);
1535
1536                 /* update counters */
1537                 count -= buf_res.length;
1538                 td->offset += buf_res.length;
1539                 td->remainder -= buf_res.length;
1540         }
1541
1542         /* clear status bits */
1543         MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 0);
1544
1545         /* check if we are complete */
1546         if ((td->remainder == 0) || got_short) {
1547                 if (td->short_pkt) {
1548                         /* we are complete */
1549                         musbotg_channel_free(sc, td);
1550                         return (0);
1551                 }
1552                 /* else need to receive a zero length packet */
1553         }
1554         if (--to) {
1555                 goto repeat;
1556         }
1557         return (1);                     /* not complete */
1558 }
1559
1560 static uint8_t
1561 musbotg_dev_data_tx(struct musbotg_td *td)
1562 {
1563         struct usb_page_search buf_res;
1564         struct musbotg_softc *sc;
1565         uint16_t count;
1566         uint8_t csr;
1567         uint8_t to;
1568
1569         to = 8;                         /* don't loop forever! */
1570
1571         /* get pointer to softc */
1572         sc = MUSBOTG_PC2SC(td->pc);
1573
1574         if (td->channel == -1)
1575                 td->channel = musbotg_channel_alloc(sc, td);
1576
1577         /* EP0 is busy, wait */
1578         if (td->channel == -1)
1579                 return (1);
1580
1581         /* select endpoint */
1582         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, td->channel);
1583
1584 repeat:
1585
1586         /* read out FIFO status */
1587         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1588
1589         DPRINTFN(4, "csr=0x%02x\n", csr);
1590
1591         if (csr & (MUSB2_MASK_CSRL_TXINCOMP |
1592             MUSB2_MASK_CSRL_TXUNDERRUN)) {
1593                 /* clear status bits */
1594                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
1595         }
1596         if (csr & MUSB2_MASK_CSRL_TXPKTRDY) {
1597                 return (1);             /* not complete */
1598         }
1599         /* check for short packet */
1600         count = td->max_frame_size;
1601         if (td->remainder < count) {
1602                 /* we have a short packet */
1603                 td->short_pkt = 1;
1604                 count = td->remainder;
1605         }
1606         while (count > 0) {
1607                 uint32_t temp;
1608
1609                 usbd_get_page(td->pc, td->offset, &buf_res);
1610
1611                 /* get correct length */
1612                 if (buf_res.length > count) {
1613                         buf_res.length = count;
1614                 }
1615                 /* check for unaligned memory address */
1616                 if (USB_P2U(buf_res.buffer) & 3) {
1617
1618                         usbd_copy_out(td->pc, td->offset,
1619                             sc->sc_bounce_buf, count);
1620
1621                         temp = count & ~3;
1622
1623                         if (temp) {
1624                                 /* transmit data 4 bytes at a time */
1625                                 bus_space_write_multi_4(sc->sc_io_tag,
1626                                     sc->sc_io_hdl, MUSB2_REG_EPFIFO(td->channel),
1627                                     sc->sc_bounce_buf, temp / 4);
1628                         }
1629                         temp = count & 3;
1630                         if (temp) {
1631                                 /* receive data 1 byte at a time */
1632                                 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
1633                                     MUSB2_REG_EPFIFO(td->channel),
1634                                     ((void *)&sc->sc_bounce_buf[count / 4]), temp);
1635                         }
1636                         /* update offset and remainder */
1637                         td->offset += count;
1638                         td->remainder -= count;
1639                         break;
1640                 }
1641                 /* check if we can optimise */
1642                 if (buf_res.length >= 4) {
1643
1644                         /* transmit data 4 bytes at a time */
1645                         bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
1646                             MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
1647                             buf_res.length / 4);
1648
1649                         temp = buf_res.length & ~3;
1650
1651                         /* update counters */
1652                         count -= temp;
1653                         td->offset += temp;
1654                         td->remainder -= temp;
1655                         continue;
1656                 }
1657                 /* transmit data */
1658                 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
1659                     MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
1660                     buf_res.length);
1661
1662                 /* update counters */
1663                 count -= buf_res.length;
1664                 td->offset += buf_res.length;
1665                 td->remainder -= buf_res.length;
1666         }
1667
1668         /* Max packet size */
1669         MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, td->reg_max_packet);
1670
1671         /* write command */
1672         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
1673             MUSB2_MASK_CSRL_TXPKTRDY);
1674
1675         /* check remainder */
1676         if (td->remainder == 0) {
1677                 if (td->short_pkt) {
1678                         musbotg_channel_free(sc, td);
1679                         return (0);     /* complete */
1680                 }
1681                 /* else we need to transmit a short packet */
1682         }
1683         if (--to) {
1684                 goto repeat;
1685         }
1686         return (1);                     /* not complete */
1687 }
1688
1689 static uint8_t
1690 musbotg_host_data_rx(struct musbotg_td *td)
1691 {
1692         struct usb_page_search buf_res;
1693         struct musbotg_softc *sc;
1694         uint16_t count;
1695         uint8_t csr, csrh;
1696         uint8_t to;
1697         uint8_t got_short;
1698
1699         /* get pointer to softc */
1700         sc = MUSBOTG_PC2SC(td->pc);
1701
1702         if (td->channel == -1)
1703                 td->channel = musbotg_channel_alloc(sc, td);
1704
1705         /* No free EPs */
1706         if (td->channel == -1)
1707                 return (1);
1708
1709         DPRINTFN(1, "ep_no=%d\n", td->channel);
1710
1711         to = 8;                         /* don't loop forever! */
1712         got_short = 0;
1713
1714         /* select endpoint */
1715         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, td->channel);
1716
1717 repeat:
1718         /* read out FIFO status */
1719         csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL);
1720         DPRINTFN(4, "csr=0x%02x\n", csr);
1721
1722         if (!td->transaction_started) {
1723                 /* Function address */
1724                 MUSB2_WRITE_1(sc, MUSB2_REG_RXFADDR(td->channel),
1725                     td->dev_addr);
1726
1727                 /* SPLIT transaction */
1728                 MUSB2_WRITE_1(sc, MUSB2_REG_RXHADDR(td->channel), 
1729                     td->haddr);
1730                 MUSB2_WRITE_1(sc, MUSB2_REG_RXHUBPORT(td->channel), 
1731                     td->hport);
1732
1733                 /* RX NAK timeout */
1734                 if (td->transfer_type & MUSB2_MASK_TI_PROTO_ISOC)
1735                         MUSB2_WRITE_1(sc, MUSB2_REG_RXNAKLIMIT, 0);
1736                 else
1737                         MUSB2_WRITE_1(sc, MUSB2_REG_RXNAKLIMIT, MAX_NAK_TO);
1738
1739                 /* Protocol, speed, device endpoint */
1740                 MUSB2_WRITE_1(sc, MUSB2_REG_RXTI, td->transfer_type);
1741
1742                 /* Max packet size */
1743                 MUSB2_WRITE_2(sc, MUSB2_REG_RXMAXP, td->reg_max_packet);
1744
1745                 /* Data Toggle */
1746                 csrh = MUSB2_READ_1(sc, MUSB2_REG_RXCSRH);
1747                 DPRINTFN(4, "csrh=0x%02x\n", csrh);
1748
1749                 csrh |= MUSB2_MASK_CSRH_RXDT_WREN;
1750                 if (td->toggle)
1751                         csrh |= MUSB2_MASK_CSRH_RXDT_VAL;
1752                 else
1753                         csrh &= ~MUSB2_MASK_CSRH_RXDT_VAL;
1754
1755                 /* Set data toggle */
1756                 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH, csrh);
1757
1758                 /* write command */
1759                 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL,
1760                     MUSB2_MASK_CSRL_RXREQPKT);
1761
1762                 td->transaction_started = 1;
1763                 return (1);
1764         }
1765
1766         /* clear NAK timeout */
1767         if (csr & MUSB2_MASK_CSRL_RXNAKTO) {
1768                 DPRINTFN(4, "NAK Timeout\n");
1769                 if (csr & MUSB2_MASK_CSRL_RXREQPKT) {
1770                         csr &= ~MUSB2_MASK_CSRL_RXREQPKT;
1771                         MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, csr);
1772
1773                         csr &= ~MUSB2_MASK_CSRL_RXNAKTO;
1774                         MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, csr);
1775                 }
1776
1777                 td->error = 1;
1778         }
1779
1780         if (csr & MUSB2_MASK_CSRL_RXERROR) {
1781                 DPRINTFN(4, "RXERROR\n");
1782                 td->error = 1;
1783         }
1784
1785         if (csr & MUSB2_MASK_CSRL_RXSTALL) {
1786                 DPRINTFN(4, "RXSTALL\n");
1787                 td->error = 1;
1788         }
1789
1790         if (td->error) {
1791                 musbotg_channel_free(sc, td);
1792                 return (0);     /* we are complete */
1793         }
1794
1795         if (!(csr & MUSB2_MASK_CSRL_RXPKTRDY)) {
1796                 /* No data available yet */
1797                 return (1);
1798         }
1799
1800         td->toggle ^= 1;
1801         /* get the packet byte count */
1802         count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT);
1803         DPRINTFN(4, "count=0x%04x\n", count);
1804
1805         /*
1806          * Check for short or invalid packet:
1807          */
1808         if (count != td->max_frame_size) {
1809                 if (count < td->max_frame_size) {
1810                         /* we have a short packet */
1811                         td->short_pkt = 1;
1812                         got_short = 1;
1813                 } else {
1814                         /* invalid USB packet */
1815                         td->error = 1;
1816                         musbotg_channel_free(sc, td);
1817                         return (0);     /* we are complete */
1818                 }
1819         }
1820
1821         /* verify the packet byte count */
1822         if (count > td->remainder) {
1823                 /* invalid USB packet */
1824                 td->error = 1;
1825                 musbotg_channel_free(sc, td);
1826                 return (0);             /* we are complete */
1827         }
1828
1829         while (count > 0) {
1830                 uint32_t temp;
1831
1832                 usbd_get_page(td->pc, td->offset, &buf_res);
1833
1834                 /* get correct length */
1835                 if (buf_res.length > count) {
1836                         buf_res.length = count;
1837                 }
1838                 /* check for unaligned memory address */
1839                 if (USB_P2U(buf_res.buffer) & 3) {
1840
1841                         temp = count & ~3;
1842
1843                         if (temp) {
1844                                 /* receive data 4 bytes at a time */
1845                                 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
1846                                     MUSB2_REG_EPFIFO(td->channel), sc->sc_bounce_buf,
1847                                     temp / 4);
1848                         }
1849                         temp = count & 3;
1850                         if (temp) {
1851                                 /* receive data 1 byte at a time */
1852                                 bus_space_read_multi_1(sc->sc_io_tag,
1853                                     sc->sc_io_hdl, MUSB2_REG_EPFIFO(td->channel),
1854                                     ((void *)&sc->sc_bounce_buf[count / 4]), temp);
1855                         }
1856                         usbd_copy_in(td->pc, td->offset,
1857                             sc->sc_bounce_buf, count);
1858
1859                         /* update offset and remainder */
1860                         td->offset += count;
1861                         td->remainder -= count;
1862                         break;
1863                 }
1864                 /* check if we can optimise */
1865                 if (buf_res.length >= 4) {
1866
1867                         /* receive data 4 bytes at a time */
1868                         bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
1869                             MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
1870                             buf_res.length / 4);
1871
1872                         temp = buf_res.length & ~3;
1873
1874                         /* update counters */
1875                         count -= temp;
1876                         td->offset += temp;
1877                         td->remainder -= temp;
1878                         continue;
1879                 }
1880                 /* receive data */
1881                 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
1882                     MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
1883                     buf_res.length);
1884
1885                 /* update counters */
1886                 count -= buf_res.length;
1887                 td->offset += buf_res.length;
1888                 td->remainder -= buf_res.length;
1889         }
1890
1891         /* clear status bits */
1892         MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 0);
1893
1894         /* check if we are complete */
1895         if ((td->remainder == 0) || got_short) {
1896                 if (td->short_pkt) {
1897                         /* we are complete */
1898                         musbotg_channel_free(sc, td);
1899                         return (0);
1900                 }
1901                 /* else need to receive a zero length packet */
1902         }
1903
1904         /* Reset transaction state and restart */
1905         td->transaction_started = 0;
1906
1907         if (--to)
1908                 goto repeat;
1909
1910         return (1);                     /* not complete */
1911 }
1912
1913 static uint8_t
1914 musbotg_host_data_tx(struct musbotg_td *td)
1915 {
1916         struct usb_page_search buf_res;
1917         struct musbotg_softc *sc;
1918         uint16_t count;
1919         uint8_t csr, csrh;
1920
1921         /* get pointer to softc */
1922         sc = MUSBOTG_PC2SC(td->pc);
1923
1924         if (td->channel == -1)
1925                 td->channel = musbotg_channel_alloc(sc, td);
1926
1927         /* No free EPs */
1928         if (td->channel == -1)
1929                 return (1);
1930
1931         DPRINTFN(1, "ep_no=%d\n", td->channel);
1932
1933         /* select endpoint */
1934         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, td->channel);
1935
1936         /* read out FIFO status */
1937         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1938         DPRINTFN(4, "csr=0x%02x\n", csr);
1939
1940         if (csr & (MUSB2_MASK_CSRL_TXSTALLED |
1941             MUSB2_MASK_CSRL_TXERROR)) {
1942                 /* clear status bits */
1943                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
1944                 td->error = 1;
1945                 musbotg_channel_free(sc, td);
1946                 return (0);     /* complete */
1947         }
1948
1949         if (csr & MUSB2_MASK_CSRL_TXNAKTO) {
1950                 /* 
1951                  * Flush TX FIFO before clearing NAK TO
1952                  */
1953                 if (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) {
1954                         csr |= MUSB2_MASK_CSRL_TXFFLUSH;
1955                         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
1956                         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1957                         if (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) {
1958                                 csr |= MUSB2_MASK_CSRL_TXFFLUSH;
1959                                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
1960                                 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1961                         }
1962                 }
1963
1964                 csr &= ~MUSB2_MASK_CSRL_TXNAKTO;
1965                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
1966
1967                 td->error = 1;
1968                 musbotg_channel_free(sc, td);
1969                 return (0);     /* complete */
1970         }
1971
1972         /*
1973          * Wait while FIFO is empty. 
1974          * Do not flush it because it will cause transactions
1975          * with size more then packet size. It might upset
1976          * some devices
1977          */
1978         if (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY)
1979                 return (1);
1980
1981         /* Packet still being processed */
1982         if (csr & MUSB2_MASK_CSRL_TXPKTRDY)
1983                 return (1);
1984
1985         if (td->transaction_started) {
1986                 /* check remainder */
1987                 if (td->remainder == 0) {
1988                         if (td->short_pkt) {
1989                                 musbotg_channel_free(sc, td);
1990                                 return (0);     /* complete */
1991                         }
1992                         /* else we need to transmit a short packet */
1993                 }
1994
1995                 /* We're not complete - more transactions required */
1996                 td->transaction_started = 0;
1997         }
1998
1999         /* check for short packet */
2000         count = td->max_frame_size;
2001         if (td->remainder < count) {
2002                 /* we have a short packet */
2003                 td->short_pkt = 1;
2004                 count = td->remainder;
2005         }
2006
2007         while (count > 0) {
2008                 uint32_t temp;
2009
2010                 usbd_get_page(td->pc, td->offset, &buf_res);
2011
2012                 /* get correct length */
2013                 if (buf_res.length > count) {
2014                         buf_res.length = count;
2015                 }
2016                 /* check for unaligned memory address */
2017                 if (USB_P2U(buf_res.buffer) & 3) {
2018
2019                         usbd_copy_out(td->pc, td->offset,
2020                             sc->sc_bounce_buf, count);
2021
2022                         temp = count & ~3;
2023
2024                         if (temp) {
2025                                 /* transmit data 4 bytes at a time */
2026                                 bus_space_write_multi_4(sc->sc_io_tag,
2027                                     sc->sc_io_hdl, MUSB2_REG_EPFIFO(td->channel),
2028                                     sc->sc_bounce_buf, temp / 4);
2029                         }
2030                         temp = count & 3;
2031                         if (temp) {
2032                                 /* receive data 1 byte at a time */
2033                                 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
2034                                     MUSB2_REG_EPFIFO(td->channel),
2035                                     ((void *)&sc->sc_bounce_buf[count / 4]), temp);
2036                         }
2037                         /* update offset and remainder */
2038                         td->offset += count;
2039                         td->remainder -= count;
2040                         break;
2041                 }
2042                 /* check if we can optimise */
2043                 if (buf_res.length >= 4) {
2044
2045                         /* transmit data 4 bytes at a time */
2046                         bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
2047                             MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
2048                             buf_res.length / 4);
2049
2050                         temp = buf_res.length & ~3;
2051
2052                         /* update counters */
2053                         count -= temp;
2054                         td->offset += temp;
2055                         td->remainder -= temp;
2056                         continue;
2057                 }
2058                 /* transmit data */
2059                 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
2060                     MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
2061                     buf_res.length);
2062
2063                 /* update counters */
2064                 count -= buf_res.length;
2065                 td->offset += buf_res.length;
2066                 td->remainder -= buf_res.length;
2067         }
2068
2069         /* Function address */
2070         MUSB2_WRITE_1(sc, MUSB2_REG_TXFADDR(td->channel),
2071             td->dev_addr);
2072
2073         /* SPLIT transaction */
2074         MUSB2_WRITE_1(sc, MUSB2_REG_TXHADDR(td->channel), 
2075             td->haddr);
2076         MUSB2_WRITE_1(sc, MUSB2_REG_TXHUBPORT(td->channel), 
2077             td->hport);
2078
2079         /* TX NAK timeout */
2080         if (td->transfer_type & MUSB2_MASK_TI_PROTO_ISOC)
2081                 MUSB2_WRITE_1(sc, MUSB2_REG_TXNAKLIMIT, 0);
2082         else
2083                 MUSB2_WRITE_1(sc, MUSB2_REG_TXNAKLIMIT, MAX_NAK_TO);
2084
2085         /* Protocol, speed, device endpoint */
2086         MUSB2_WRITE_1(sc, MUSB2_REG_TXTI, td->transfer_type);
2087
2088         /* Max packet size */
2089         MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, td->reg_max_packet);
2090
2091         if (!td->transaction_started) {
2092                 csrh = MUSB2_READ_1(sc, MUSB2_REG_TXCSRH);
2093                 DPRINTFN(4, "csrh=0x%02x\n", csrh);
2094
2095                 csrh |= MUSB2_MASK_CSRH_TXDT_WREN;
2096                 if (td->toggle)
2097                         csrh |= MUSB2_MASK_CSRH_TXDT_VAL;
2098                 else
2099                         csrh &= ~MUSB2_MASK_CSRH_TXDT_VAL;
2100
2101                 /* Set data toggle */
2102                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, csrh);
2103         }
2104
2105         /* write command */
2106         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
2107             MUSB2_MASK_CSRL_TXPKTRDY);
2108
2109         /* Update Data Toggle */
2110         td->toggle ^= 1;
2111         td->transaction_started = 1;
2112
2113         return (1);                     /* not complete */
2114 }
2115
2116 static uint8_t
2117 musbotg_xfer_do_fifo(struct usb_xfer *xfer)
2118 {
2119         struct musbotg_softc *sc;
2120         struct musbotg_td *td;
2121
2122         DPRINTFN(8, "\n");
2123         sc = MUSBOTG_BUS2SC(xfer->xroot->bus);
2124
2125         td = xfer->td_transfer_cache;
2126         while (1) {
2127
2128                 if ((td->func) (td)) {
2129                         /* operation in progress */
2130                         break;
2131                 }
2132
2133                 if (((void *)td) == xfer->td_transfer_last) {
2134                         goto done;
2135                 }
2136                 if (td->error) {
2137                         goto done;
2138                 } else if (td->remainder > 0) {
2139                         /*
2140                          * We had a short transfer. If there is no alternate
2141                          * next, stop processing !
2142                          */
2143                         if (!td->alt_next) {
2144                                 goto done;
2145                         }
2146                 }
2147                 /*
2148                  * Fetch the next transfer descriptor and transfer
2149                  * some flags to the next transfer descriptor
2150                  */
2151                 td = td->obj_next;
2152                 xfer->td_transfer_cache = td;
2153         }
2154
2155         return (1);                     /* not complete */
2156 done:
2157         /* compute all actual lengths */
2158         musbotg_standard_done(xfer);
2159
2160         return (0);                     /* complete */
2161 }
2162
2163 static void
2164 musbotg_interrupt_poll(struct musbotg_softc *sc)
2165 {
2166         struct usb_xfer *xfer;
2167
2168 repeat:
2169         TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
2170                 if (!musbotg_xfer_do_fifo(xfer)) {
2171                         /* queue has been modified */
2172                         goto repeat;
2173                 }
2174         }
2175 }
2176
2177 void
2178 musbotg_vbus_interrupt(struct musbotg_softc *sc, uint8_t is_on)
2179 {
2180         DPRINTFN(4, "vbus = %u\n", is_on);
2181
2182         USB_BUS_LOCK(&sc->sc_bus);
2183         if (is_on) {
2184                 if (!sc->sc_flags.status_vbus) {
2185                         sc->sc_flags.status_vbus = 1;
2186
2187                         /* complete root HUB interrupt endpoint */
2188                         musbotg_root_intr(sc);
2189                 }
2190         } else {
2191                 if (sc->sc_flags.status_vbus) {
2192                         sc->sc_flags.status_vbus = 0;
2193                         sc->sc_flags.status_bus_reset = 0;
2194                         sc->sc_flags.status_suspend = 0;
2195                         sc->sc_flags.change_suspend = 0;
2196                         sc->sc_flags.change_connect = 1;
2197
2198                         /* complete root HUB interrupt endpoint */
2199                         musbotg_root_intr(sc);
2200                 }
2201         }
2202
2203         USB_BUS_UNLOCK(&sc->sc_bus);
2204 }
2205
2206 void
2207 musbotg_connect_interrupt(struct musbotg_softc *sc)
2208 {
2209         USB_BUS_LOCK(&sc->sc_bus);
2210         sc->sc_flags.change_connect = 1;
2211
2212         /* complete root HUB interrupt endpoint */
2213         musbotg_root_intr(sc);
2214         USB_BUS_UNLOCK(&sc->sc_bus);
2215 }
2216
2217 void
2218 musbotg_interrupt(struct musbotg_softc *sc,
2219     uint16_t rxstat, uint16_t txstat, uint8_t stat)
2220 {
2221         uint16_t rx_status;
2222         uint16_t tx_status;
2223         uint8_t usb_status;
2224         uint8_t temp;
2225         uint8_t to = 2;
2226
2227         USB_BUS_LOCK(&sc->sc_bus);
2228
2229 repeat:
2230
2231         /* read all interrupt registers */
2232         usb_status = MUSB2_READ_1(sc, MUSB2_REG_INTUSB);
2233
2234         /* read all FIFO interrupts */
2235         rx_status = MUSB2_READ_2(sc, MUSB2_REG_INTRX);
2236         tx_status = MUSB2_READ_2(sc, MUSB2_REG_INTTX);
2237         rx_status |= rxstat;
2238         tx_status |= txstat;
2239         usb_status |= stat;
2240
2241         /* Clear platform flags after first time */
2242         rxstat = 0;
2243         txstat = 0;
2244         stat = 0;
2245
2246         /* check for any bus state change interrupts */
2247
2248         if (usb_status & (MUSB2_MASK_IRESET |
2249             MUSB2_MASK_IRESUME | MUSB2_MASK_ISUSP | 
2250             MUSB2_MASK_ICONN | MUSB2_MASK_IDISC)) {
2251
2252                 DPRINTFN(4, "real bus interrupt 0x%08x\n", usb_status);
2253
2254                 if (usb_status & MUSB2_MASK_IRESET) {
2255
2256                         /* set correct state */
2257                         sc->sc_flags.status_bus_reset = 1;
2258                         sc->sc_flags.status_suspend = 0;
2259                         sc->sc_flags.change_suspend = 0;
2260                         sc->sc_flags.change_connect = 1;
2261
2262                         /* determine line speed */
2263                         temp = MUSB2_READ_1(sc, MUSB2_REG_POWER);
2264                         if (temp & MUSB2_MASK_HSMODE)
2265                                 sc->sc_flags.status_high_speed = 1;
2266                         else
2267                                 sc->sc_flags.status_high_speed = 0;
2268
2269                         /*
2270                          * After reset all interrupts are on and we need to
2271                          * turn them off!
2272                          */
2273                         temp = MUSB2_MASK_IRESET;
2274                         /* disable resume interrupt */
2275                         temp &= ~MUSB2_MASK_IRESUME;
2276                         /* enable suspend interrupt */
2277                         temp |= MUSB2_MASK_ISUSP;
2278                         MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, temp);
2279                         /* disable TX and RX interrupts */
2280                         MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, 0);
2281                         MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, 0);
2282                 }
2283                 /*
2284                  * If RXRSM and RXSUSP is set at the same time we interpret
2285                  * that like RESUME. Resume is set when there is at least 3
2286                  * milliseconds of inactivity on the USB BUS.
2287                  */
2288                 if (usb_status & MUSB2_MASK_IRESUME) {
2289                         if (sc->sc_flags.status_suspend) {
2290                                 sc->sc_flags.status_suspend = 0;
2291                                 sc->sc_flags.change_suspend = 1;
2292
2293                                 temp = MUSB2_READ_1(sc, MUSB2_REG_INTUSBE);
2294                                 /* disable resume interrupt */
2295                                 temp &= ~MUSB2_MASK_IRESUME;
2296                                 /* enable suspend interrupt */
2297                                 temp |= MUSB2_MASK_ISUSP;
2298                                 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, temp);
2299                         }
2300                 } else if (usb_status & MUSB2_MASK_ISUSP) {
2301                         if (!sc->sc_flags.status_suspend) {
2302                                 sc->sc_flags.status_suspend = 1;
2303                                 sc->sc_flags.change_suspend = 1;
2304
2305                                 temp = MUSB2_READ_1(sc, MUSB2_REG_INTUSBE);
2306                                 /* disable suspend interrupt */
2307                                 temp &= ~MUSB2_MASK_ISUSP;
2308                                 /* enable resume interrupt */
2309                                 temp |= MUSB2_MASK_IRESUME;
2310                                 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, temp);
2311                         }
2312                 }
2313                 if (usb_status & 
2314                     (MUSB2_MASK_ICONN | MUSB2_MASK_IDISC))
2315                         sc->sc_flags.change_connect = 1;
2316
2317                 /* 
2318                  * Host Mode: There is no IRESET so assume bus is 
2319                  * always in reset state once device is connected.
2320                  */
2321                 if (sc->sc_mode == MUSB2_HOST_MODE) {
2322                     if (usb_status & MUSB2_MASK_ICONN)
2323                         sc->sc_flags.status_bus_reset = 1;
2324                     if (usb_status & MUSB2_MASK_IDISC)
2325                         sc->sc_flags.status_bus_reset = 0;
2326                 }
2327
2328                 /* complete root HUB interrupt endpoint */
2329                 musbotg_root_intr(sc);
2330         }
2331         /* check for any endpoint interrupts */
2332
2333         if (rx_status || tx_status) {
2334                 DPRINTFN(4, "real endpoint interrupt "
2335                     "rx=0x%04x, tx=0x%04x\n", rx_status, tx_status);
2336         }
2337         /* poll one time regardless of FIFO status */
2338
2339         musbotg_interrupt_poll(sc);
2340
2341         if (--to)
2342                 goto repeat;
2343
2344         USB_BUS_UNLOCK(&sc->sc_bus);
2345 }
2346
2347 static void
2348 musbotg_setup_standard_chain_sub(struct musbotg_std_temp *temp)
2349 {
2350         struct musbotg_td *td;
2351
2352         /* get current Transfer Descriptor */
2353         td = temp->td_next;
2354         temp->td = td;
2355
2356         /* prepare for next TD */
2357         temp->td_next = td->obj_next;
2358
2359         /* fill out the Transfer Descriptor */
2360         td->func = temp->func;
2361         td->pc = temp->pc;
2362         td->offset = temp->offset;
2363         td->remainder = temp->len;
2364         td->error = 0;
2365         td->transaction_started = 0;
2366         td->did_stall = temp->did_stall;
2367         td->short_pkt = temp->short_pkt;
2368         td->alt_next = temp->setup_alt_next;
2369         td->channel = temp->channel;
2370         td->dev_addr = temp->dev_addr;
2371         td->haddr = temp->haddr;
2372         td->hport = temp->hport;
2373         td->transfer_type = temp->transfer_type;
2374 }
2375
2376 static void
2377 musbotg_setup_standard_chain(struct usb_xfer *xfer)
2378 {
2379         struct musbotg_std_temp temp;
2380         struct musbotg_softc *sc;
2381         struct musbotg_td *td;
2382         uint32_t x;
2383         uint8_t ep_no;
2384         uint8_t xfer_type;
2385         enum usb_dev_speed speed;
2386         int tx;
2387         int dev_addr;
2388
2389         DPRINTFN(8, "addr=%d endpt=%d sumlen=%d speed=%d\n",
2390             xfer->address, UE_GET_ADDR(xfer->endpointno),
2391             xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
2392
2393         sc = MUSBOTG_BUS2SC(xfer->xroot->bus);
2394         ep_no = (xfer->endpointno & UE_ADDR);
2395
2396         temp.max_frame_size = xfer->max_frame_size;
2397
2398         td = xfer->td_start[0];
2399         xfer->td_transfer_first = td;
2400         xfer->td_transfer_cache = td;
2401
2402         /* setup temp */
2403         dev_addr = xfer->address;
2404
2405         xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2406
2407         temp.pc = NULL;
2408         temp.td = NULL;
2409         temp.td_next = xfer->td_start[0];
2410         temp.offset = 0;
2411         temp.setup_alt_next = xfer->flags_int.short_frames_ok;
2412         temp.did_stall = !xfer->flags_int.control_stall;
2413         temp.channel = -1;
2414         temp.dev_addr = dev_addr;
2415         temp.haddr = xfer->xroot->udev->hs_hub_addr;
2416         temp.hport = xfer->xroot->udev->hs_port_no;
2417
2418         if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2419                 speed =  usbd_get_speed(xfer->xroot->udev);
2420
2421                 switch (speed) {
2422                         case USB_SPEED_LOW:
2423                                 temp.transfer_type = MUSB2_MASK_TI_SPEED_LO;
2424                                 break;
2425                         case USB_SPEED_FULL:
2426                                 temp.transfer_type = MUSB2_MASK_TI_SPEED_FS;
2427                                 break;
2428                         case USB_SPEED_HIGH:
2429                                 temp.transfer_type = MUSB2_MASK_TI_SPEED_HS;
2430                                 break;
2431                         default:
2432                                 temp.transfer_type = 0;
2433                                 DPRINTFN(-1, "Invalid USB speed: %d\n", speed);
2434                                 break;
2435                 }
2436
2437                 switch (xfer_type) {
2438                         case UE_CONTROL:
2439                                 temp.transfer_type |= MUSB2_MASK_TI_PROTO_CTRL;
2440                                 break;
2441                         case UE_ISOCHRONOUS:
2442                                 temp.transfer_type |= MUSB2_MASK_TI_PROTO_ISOC;
2443                                 break;
2444                         case UE_BULK:
2445                                 temp.transfer_type |= MUSB2_MASK_TI_PROTO_BULK;
2446                                 break;
2447                         case UE_INTERRUPT:
2448                                 temp.transfer_type |= MUSB2_MASK_TI_PROTO_INTR;
2449                                 break;
2450                         default:
2451                                 DPRINTFN(-1, "Invalid USB transfer type: %d\n",
2452                                                 xfer_type);
2453                                 break;
2454                 }
2455
2456                 temp.transfer_type |= ep_no;
2457                 td->toggle = xfer->endpoint->toggle_next;
2458         }
2459
2460         /* check if we should prepend a setup message */
2461
2462         if (xfer->flags_int.control_xfr) {
2463                 if (xfer->flags_int.control_hdr) {
2464
2465                         if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
2466                                 temp.func = &musbotg_dev_ctrl_setup_rx;
2467                         else
2468                                 temp.func = &musbotg_host_ctrl_setup_tx;
2469
2470                         temp.len = xfer->frlengths[0];
2471                         temp.pc = xfer->frbuffers + 0;
2472                         temp.short_pkt = temp.len ? 1 : 0;
2473
2474                         musbotg_setup_standard_chain_sub(&temp);
2475                 }
2476                 x = 1;
2477         } else {
2478                 x = 0;
2479         }
2480
2481         tx = 0;
2482
2483         if (x != xfer->nframes) {
2484                 if (xfer->endpointno & UE_DIR_IN)
2485                         tx = 1;
2486
2487                 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2488                         tx = !tx;
2489
2490                         if (tx) {
2491                                 if (xfer->flags_int.control_xfr)
2492                                         temp.func = &musbotg_host_ctrl_data_tx;
2493                                 else
2494                                         temp.func = &musbotg_host_data_tx;
2495                         } else {
2496                                 if (xfer->flags_int.control_xfr)
2497                                         temp.func = &musbotg_host_ctrl_data_rx;
2498                                 else
2499                                         temp.func = &musbotg_host_data_rx;
2500                         }
2501
2502                 } else {
2503                         if (tx) {
2504                                 if (xfer->flags_int.control_xfr)
2505                                         temp.func = &musbotg_dev_ctrl_data_tx;
2506                                 else
2507                                         temp.func = &musbotg_dev_data_tx;
2508                         } else {
2509                                 if (xfer->flags_int.control_xfr)
2510                                         temp.func = &musbotg_dev_ctrl_data_rx;
2511                                 else
2512                                         temp.func = &musbotg_dev_data_rx;
2513                         }
2514                 }
2515
2516                 /* setup "pc" pointer */
2517                 temp.pc = xfer->frbuffers + x;
2518         }
2519         while (x != xfer->nframes) {
2520
2521                 /* DATA0 / DATA1 message */
2522
2523                 temp.len = xfer->frlengths[x];
2524
2525                 x++;
2526
2527                 if (x == xfer->nframes) {
2528                         if (xfer->flags_int.control_xfr) {
2529                                 if (xfer->flags_int.control_act) {
2530                                         temp.setup_alt_next = 0;
2531                                 }
2532                         } else {
2533                                 temp.setup_alt_next = 0;
2534                         }
2535                 }
2536                 if (temp.len == 0) {
2537
2538                         /* make sure that we send an USB packet */
2539
2540                         temp.short_pkt = 0;
2541
2542                 } else {
2543
2544                         if (xfer->flags_int.isochronous_xfr) {
2545                                 /* isochronous data transfer */
2546                                 /* don't force short */
2547                                 temp.short_pkt = 1;
2548                         } else {
2549                                 /* regular data transfer */
2550                                 temp.short_pkt = (xfer->flags.force_short_xfer ? 0 : 1);
2551                         }
2552                 }
2553
2554                 musbotg_setup_standard_chain_sub(&temp);
2555
2556                 if (xfer->flags_int.isochronous_xfr) {
2557                         temp.offset += temp.len;
2558                 } else {
2559                         /* get next Page Cache pointer */
2560                         temp.pc = xfer->frbuffers + x;
2561                 }
2562         }
2563
2564         /* check for control transfer */
2565         if (xfer->flags_int.control_xfr) {
2566
2567                 /* always setup a valid "pc" pointer for status and sync */
2568                 temp.pc = xfer->frbuffers + 0;
2569                 temp.len = 0;
2570                 temp.short_pkt = 0;
2571                 temp.setup_alt_next = 0;
2572
2573                 /* check if we should append a status stage */
2574                 if (!xfer->flags_int.control_act) {
2575                         /*
2576                          * Send a DATA1 message and invert the current
2577                          * endpoint direction.
2578                          */
2579                         if (sc->sc_mode == MUSB2_DEVICE_MODE)
2580                                 temp.func = &musbotg_dev_ctrl_status;
2581                         else {
2582                                 if (xfer->endpointno & UE_DIR_IN)
2583                                         temp.func = musbotg_host_ctrl_status_tx;
2584                                 else
2585                                         temp.func = musbotg_host_ctrl_status_rx;
2586                         }
2587                         musbotg_setup_standard_chain_sub(&temp);
2588                 }
2589         }
2590         /* must have at least one frame! */
2591         td = temp.td;
2592         xfer->td_transfer_last = td;
2593 }
2594
2595 static void
2596 musbotg_timeout(void *arg)
2597 {
2598         struct usb_xfer *xfer = arg;
2599
2600         DPRINTFN(1, "xfer=%p\n", xfer);
2601
2602         USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2603
2604         /* transfer is transferred */
2605         musbotg_device_done(xfer, USB_ERR_TIMEOUT);
2606 }
2607
2608 static void
2609 musbotg_ep_int_set(struct musbotg_softc *sc, int channel, int on)
2610 {
2611         uint16_t temp;
2612
2613         /*
2614          * Only enable the endpoint interrupt when we are
2615          * actually waiting for data, hence we are dealing
2616          * with level triggered interrupts !
2617          */
2618         DPRINTFN(1, "ep_no=%d, on=%d\n", channel, on);
2619
2620         if (channel == -1)
2621                 return;
2622
2623         if (channel == 0) {
2624                 temp = MUSB2_READ_2(sc, MUSB2_REG_INTTXE);
2625                 if (on)
2626                         temp |= MUSB2_MASK_EPINT(0);
2627                 else
2628                         temp &= ~MUSB2_MASK_EPINT(0);
2629
2630                 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, temp);
2631         } else {
2632                 temp = MUSB2_READ_2(sc, MUSB2_REG_INTRXE);
2633                 if (on)
2634                         temp |= MUSB2_MASK_EPINT(channel);
2635                 else
2636                         temp &= ~MUSB2_MASK_EPINT(channel);
2637                 MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, temp);
2638
2639                 temp = MUSB2_READ_2(sc, MUSB2_REG_INTTXE);
2640                 if (on)
2641                         temp |= MUSB2_MASK_EPINT(channel);
2642                 else
2643                         temp &= ~MUSB2_MASK_EPINT(channel);
2644                 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, temp);
2645         }
2646
2647         if (sc->sc_ep_int_set)
2648                 sc->sc_ep_int_set(sc, channel, on);
2649 }
2650
2651 static void
2652 musbotg_start_standard_chain(struct usb_xfer *xfer)
2653 {
2654         DPRINTFN(8, "\n");
2655
2656         /* poll one time */
2657         if (musbotg_xfer_do_fifo(xfer)) {
2658
2659                 DPRINTFN(14, "enabled interrupts on endpoint\n");
2660
2661                 /* put transfer on interrupt queue */
2662                 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
2663
2664                 /* start timeout, if any */
2665                 if (xfer->timeout != 0) {
2666                         usbd_transfer_timeout_ms(xfer,
2667                             &musbotg_timeout, xfer->timeout);
2668                 }
2669         }
2670 }
2671
2672 static void
2673 musbotg_root_intr(struct musbotg_softc *sc)
2674 {
2675         DPRINTFN(8, "\n");
2676
2677         USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2678
2679         /* set port bit */
2680         sc->sc_hub_idata[0] = 0x02;     /* we only have one port */
2681
2682         uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2683             sizeof(sc->sc_hub_idata));
2684 }
2685
2686 static usb_error_t
2687 musbotg_standard_done_sub(struct usb_xfer *xfer)
2688 {
2689         struct musbotg_td *td;
2690         uint32_t len;
2691         uint8_t error;
2692
2693         DPRINTFN(8, "\n");
2694
2695         td = xfer->td_transfer_cache;
2696
2697         do {
2698                 len = td->remainder;
2699
2700                 xfer->endpoint->toggle_next = td->toggle;
2701
2702                 if (xfer->aframes != xfer->nframes) {
2703                         /*
2704                          * Verify the length and subtract
2705                          * the remainder from "frlengths[]":
2706                          */
2707                         if (len > xfer->frlengths[xfer->aframes]) {
2708                                 td->error = 1;
2709                         } else {
2710                                 xfer->frlengths[xfer->aframes] -= len;
2711                         }
2712                 }
2713                 /* Check for transfer error */
2714                 if (td->error) {
2715                         /* the transfer is finished */
2716                         error = 1;
2717                         td = NULL;
2718                         break;
2719                 }
2720                 /* Check for short transfer */
2721                 if (len > 0) {
2722                         if (xfer->flags_int.short_frames_ok) {
2723                                 /* follow alt next */
2724                                 if (td->alt_next) {
2725                                         td = td->obj_next;
2726                                 } else {
2727                                         td = NULL;
2728                                 }
2729                         } else {
2730                                 /* the transfer is finished */
2731                                 td = NULL;
2732                         }
2733                         error = 0;
2734                         break;
2735                 }
2736                 td = td->obj_next;
2737
2738                 /* this USB frame is complete */
2739                 error = 0;
2740                 break;
2741
2742         } while (0);
2743
2744         /* update transfer cache */
2745
2746         xfer->td_transfer_cache = td;
2747
2748         return (error ?
2749             USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
2750 }
2751
2752 static void
2753 musbotg_standard_done(struct usb_xfer *xfer)
2754 {
2755         usb_error_t err = 0;
2756
2757         DPRINTFN(12, "xfer=%p endpoint=%p transfer done\n",
2758             xfer, xfer->endpoint);
2759
2760         /* reset scanner */
2761
2762         xfer->td_transfer_cache = xfer->td_transfer_first;
2763
2764         if (xfer->flags_int.control_xfr) {
2765
2766                 if (xfer->flags_int.control_hdr) {
2767
2768                         err = musbotg_standard_done_sub(xfer);
2769                 }
2770                 xfer->aframes = 1;
2771
2772                 if (xfer->td_transfer_cache == NULL) {
2773                         goto done;
2774                 }
2775         }
2776         while (xfer->aframes != xfer->nframes) {
2777
2778                 err = musbotg_standard_done_sub(xfer);
2779                 xfer->aframes++;
2780
2781                 if (xfer->td_transfer_cache == NULL) {
2782                         goto done;
2783                 }
2784         }
2785
2786         if (xfer->flags_int.control_xfr &&
2787             !xfer->flags_int.control_act) {
2788
2789                 err = musbotg_standard_done_sub(xfer);
2790         }
2791 done:
2792         musbotg_device_done(xfer, err);
2793 }
2794
2795 /*------------------------------------------------------------------------*
2796  *      musbotg_device_done
2797  *
2798  * NOTE: this function can be called more than one time on the
2799  * same USB transfer!
2800  *------------------------------------------------------------------------*/
2801 static void
2802 musbotg_device_done(struct usb_xfer *xfer, usb_error_t error)
2803 {
2804         struct musbotg_td *td;
2805         struct musbotg_softc *sc;
2806
2807         USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2808
2809         DPRINTFN(1, "xfer=%p, endpoint=%p, error=%d\n",
2810             xfer, xfer->endpoint, error);
2811
2812         DPRINTFN(14, "disabled interrupts on endpoint\n");
2813
2814         sc = MUSBOTG_BUS2SC(xfer->xroot->bus);
2815         td = xfer->td_transfer_cache;
2816
2817         if (td && (td->channel != -1))
2818                 musbotg_channel_free(sc, td);
2819
2820         /* dequeue transfer and start next transfer */
2821         usbd_transfer_done(xfer, error);
2822 }
2823
2824 static void
2825 musbotg_xfer_stall(struct usb_xfer *xfer)
2826 {
2827         musbotg_device_done(xfer, USB_ERR_STALLED);
2828 }
2829
2830 static void
2831 musbotg_set_stall(struct usb_device *udev,
2832     struct usb_endpoint *ep, uint8_t *did_stall)
2833 {
2834         struct musbotg_softc *sc;
2835         uint8_t ep_no;
2836
2837         USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
2838
2839         DPRINTFN(4, "endpoint=%p\n", ep);
2840
2841         /* set FORCESTALL */
2842         sc = MUSBOTG_BUS2SC(udev->bus);
2843
2844         ep_no = (ep->edesc->bEndpointAddress & UE_ADDR);
2845
2846         /* select endpoint */
2847         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, ep_no);
2848
2849         if (ep->edesc->bEndpointAddress & UE_DIR_IN) {
2850                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
2851                     MUSB2_MASK_CSRL_TXSENDSTALL);
2852         } else {
2853                 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL,
2854                     MUSB2_MASK_CSRL_RXSENDSTALL);
2855         }
2856 }
2857
2858 static void
2859 musbotg_clear_stall_sub(struct musbotg_softc *sc, uint16_t wMaxPacket,
2860     uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
2861 {
2862         uint16_t mps;
2863         uint16_t temp;
2864         uint8_t csr;
2865
2866         if (ep_type == UE_CONTROL) {
2867                 /* clearing stall is not needed */
2868                 return;
2869         }
2870         /* select endpoint */
2871         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, ep_no);
2872
2873         /* compute max frame size */
2874         mps = wMaxPacket & 0x7FF;
2875         switch ((wMaxPacket >> 11) & 3) {
2876         case 1:
2877                 mps *= 2;
2878                 break;
2879         case 2:
2880                 mps *= 3;
2881                 break;
2882         default:
2883                 break;
2884         }
2885
2886         if (ep_dir == UE_DIR_IN) {
2887
2888                 temp = 0;
2889
2890                 /* Configure endpoint */
2891                 switch (ep_type) {
2892                 case UE_INTERRUPT:
2893                         MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, wMaxPacket);
2894                         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH,
2895                             MUSB2_MASK_CSRH_TXMODE | temp);
2896                         break;
2897                 case UE_ISOCHRONOUS:
2898                         MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, wMaxPacket);
2899                         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH,
2900                             MUSB2_MASK_CSRH_TXMODE |
2901                             MUSB2_MASK_CSRH_TXISO | temp);
2902                         break;
2903                 case UE_BULK:
2904                         MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, wMaxPacket);
2905                         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH,
2906                             MUSB2_MASK_CSRH_TXMODE | temp);
2907                         break;
2908                 default:
2909                         break;
2910                 }
2911
2912                 /* Need to flush twice in case of double bufring */
2913                 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
2914                 if (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) {
2915                         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
2916                             MUSB2_MASK_CSRL_TXFFLUSH);
2917                         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
2918                         if (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) {
2919                                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
2920                                     MUSB2_MASK_CSRL_TXFFLUSH);
2921                                 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
2922                         }
2923                 }
2924                 /* reset data toggle */
2925                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
2926                     MUSB2_MASK_CSRL_TXDT_CLR);
2927                 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
2928                 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
2929
2930                 /* set double/single buffering */
2931                 temp = MUSB2_READ_2(sc, MUSB2_REG_TXDBDIS);
2932                 if (mps <= (sc->sc_hw_ep_profile[ep_no].
2933                     max_in_frame_size / 2)) {
2934                         /* double buffer */
2935                         temp &= ~(1 << ep_no);
2936                 } else {
2937                         /* single buffer */
2938                         temp |= (1 << ep_no);
2939                 }
2940                 MUSB2_WRITE_2(sc, MUSB2_REG_TXDBDIS, temp);
2941
2942                 /* clear sent stall */
2943                 if (csr & MUSB2_MASK_CSRL_TXSENTSTALL) {
2944                         MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
2945                         csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
2946                 }
2947         } else {
2948
2949                 temp = 0;
2950
2951                 /* Configure endpoint */
2952                 switch (ep_type) {
2953                 case UE_INTERRUPT:
2954                         MUSB2_WRITE_2(sc, MUSB2_REG_RXMAXP, wMaxPacket);
2955                         MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH,
2956                             MUSB2_MASK_CSRH_RXNYET | temp);
2957                         break;
2958                 case UE_ISOCHRONOUS:
2959                         MUSB2_WRITE_2(sc, MUSB2_REG_RXMAXP, wMaxPacket);
2960                         MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH,
2961                             MUSB2_MASK_CSRH_RXNYET |
2962                             MUSB2_MASK_CSRH_RXISO | temp);
2963                         break;
2964                 case UE_BULK:
2965                         MUSB2_WRITE_2(sc, MUSB2_REG_RXMAXP, wMaxPacket);
2966                         MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH, temp);
2967                         break;
2968                 default:
2969                         break;
2970                 }
2971
2972                 /* Need to flush twice in case of double bufring */
2973                 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL);
2974                 if (csr & MUSB2_MASK_CSRL_RXPKTRDY) {
2975                         MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL,
2976                             MUSB2_MASK_CSRL_RXFFLUSH);
2977                         csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL);
2978                         if (csr & MUSB2_MASK_CSRL_RXPKTRDY) {
2979                                 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL,
2980                                     MUSB2_MASK_CSRL_RXFFLUSH);
2981                                 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL);
2982                         }
2983                 }
2984                 /* reset data toggle */
2985                 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL,
2986                     MUSB2_MASK_CSRL_RXDT_CLR);
2987                 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 0);
2988                 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL);
2989
2990                 /* set double/single buffering */
2991                 temp = MUSB2_READ_2(sc, MUSB2_REG_RXDBDIS);
2992                 if (mps <= (sc->sc_hw_ep_profile[ep_no].
2993                     max_out_frame_size / 2)) {
2994                         /* double buffer */
2995                         temp &= ~(1 << ep_no);
2996                 } else {
2997                         /* single buffer */
2998                         temp |= (1 << ep_no);
2999                 }
3000                 MUSB2_WRITE_2(sc, MUSB2_REG_RXDBDIS, temp);
3001
3002                 /* clear sent stall */
3003                 if (csr & MUSB2_MASK_CSRL_RXSENTSTALL) {
3004                         MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 0);
3005                 }
3006         }
3007 }
3008
3009 static void
3010 musbotg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3011 {
3012         struct musbotg_softc *sc;
3013         struct usb_endpoint_descriptor *ed;
3014
3015         DPRINTFN(4, "endpoint=%p\n", ep);
3016
3017         USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3018
3019         /* check mode */
3020         if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3021                 /* not supported */
3022                 return;
3023         }
3024         /* get softc */
3025         sc = MUSBOTG_BUS2SC(udev->bus);
3026
3027         /* get endpoint descriptor */
3028         ed = ep->edesc;
3029
3030         /* reset endpoint */
3031         musbotg_clear_stall_sub(sc,
3032             UGETW(ed->wMaxPacketSize),
3033             (ed->bEndpointAddress & UE_ADDR),
3034             (ed->bmAttributes & UE_XFERTYPE),
3035             (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
3036 }
3037
3038 usb_error_t
3039 musbotg_init(struct musbotg_softc *sc)
3040 {
3041         struct usb_hw_ep_profile *pf;
3042         uint16_t offset;
3043         uint8_t nrx;
3044         uint8_t ntx;
3045         uint8_t temp;
3046         uint8_t fsize;
3047         uint8_t frx;
3048         uint8_t ftx;
3049         uint8_t dynfifo;
3050
3051         DPRINTFN(1, "start\n");
3052
3053         /* set up the bus structure */
3054         sc->sc_bus.usbrev = USB_REV_2_0;
3055         sc->sc_bus.methods = &musbotg_bus_methods;
3056
3057         USB_BUS_LOCK(&sc->sc_bus);
3058
3059         /* turn on clocks */
3060
3061         if (sc->sc_clocks_on) {
3062                 (sc->sc_clocks_on) (sc->sc_clocks_arg);
3063         }
3064
3065         /* wait a little for things to stabilise */
3066         usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
3067
3068         /* disable all interrupts */
3069
3070         temp = MUSB2_READ_1(sc, MUSB2_REG_DEVCTL);
3071         DPRINTF("pre-DEVCTL=0x%02x\n", temp);
3072
3073         MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, 0);
3074         MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, 0);
3075         MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, 0);
3076
3077         /* disable pullup */
3078
3079         musbotg_pull_common(sc, 0);
3080
3081         /* wait a little bit (10ms) */
3082         usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3083
3084
3085         /* disable double packet buffering */
3086         MUSB2_WRITE_2(sc, MUSB2_REG_RXDBDIS, 0xFFFF);
3087         MUSB2_WRITE_2(sc, MUSB2_REG_TXDBDIS, 0xFFFF);
3088
3089         /* enable HighSpeed and ISO Update flags */
3090
3091         MUSB2_WRITE_1(sc, MUSB2_REG_POWER,
3092             MUSB2_MASK_HSENAB | MUSB2_MASK_ISOUPD);
3093
3094         if (sc->sc_mode == MUSB2_DEVICE_MODE) {
3095                 /* clear Session bit, if set */
3096                 temp = MUSB2_READ_1(sc, MUSB2_REG_DEVCTL);
3097                 temp &= ~MUSB2_MASK_SESS;
3098                 MUSB2_WRITE_1(sc, MUSB2_REG_DEVCTL, temp);
3099         } else {
3100                 /* Enter session for Host mode */
3101                 temp = MUSB2_READ_1(sc, MUSB2_REG_DEVCTL);
3102                 temp |= MUSB2_MASK_SESS;
3103                 MUSB2_WRITE_1(sc, MUSB2_REG_DEVCTL, temp);
3104         }
3105
3106         /* wait a little for things to stabilise */
3107         usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 10);
3108
3109         DPRINTF("DEVCTL=0x%02x\n", temp);
3110
3111         /* disable testmode */
3112
3113         MUSB2_WRITE_1(sc, MUSB2_REG_TESTMODE, 0);
3114
3115         /* set default value */
3116
3117         MUSB2_WRITE_1(sc, MUSB2_REG_MISC, 0);
3118
3119         /* select endpoint index 0 */
3120
3121         MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
3122
3123         /* read out number of endpoints */
3124
3125         nrx =
3126             (MUSB2_READ_1(sc, MUSB2_REG_EPINFO) / 16);
3127
3128         ntx =
3129             (MUSB2_READ_1(sc, MUSB2_REG_EPINFO) % 16);
3130
3131         /* these numbers exclude the control endpoint */
3132
3133         DPRINTFN(2, "RX/TX endpoints: %u/%u\n", nrx, ntx);
3134
3135         sc->sc_ep_max = (nrx > ntx) ? nrx : ntx;
3136         if (sc->sc_ep_max == 0) {
3137                 DPRINTFN(2, "ERROR: Looks like the clocks are off!\n");
3138         }
3139         /* read out configuration data */
3140
3141         sc->sc_conf_data = MUSB2_READ_1(sc, MUSB2_REG_CONFDATA);
3142
3143         DPRINTFN(2, "Config Data: 0x%02x\n",
3144             sc->sc_conf_data);
3145
3146         dynfifo = (sc->sc_conf_data & MUSB2_MASK_CD_DYNFIFOSZ) ? 1 : 0;
3147
3148         if (dynfifo) {
3149                 device_printf(sc->sc_bus.bdev, "Dynamic FIFO sizing detected, "
3150                     "assuming 16Kbytes of FIFO RAM\n");
3151         }
3152
3153         DPRINTFN(2, "HW version: 0x%04x\n",
3154             MUSB2_READ_1(sc, MUSB2_REG_HWVERS));
3155
3156         /* initialise endpoint profiles */
3157
3158         offset = 0;
3159
3160         for (temp = 1; temp <= sc->sc_ep_max; temp++) {
3161                 pf = sc->sc_hw_ep_profile + temp;
3162
3163                 /* select endpoint */
3164                 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, temp);
3165
3166                 fsize = MUSB2_READ_1(sc, MUSB2_REG_FSIZE);
3167                 frx = (fsize & MUSB2_MASK_RX_FSIZE) / 16;
3168                 ftx = (fsize & MUSB2_MASK_TX_FSIZE);
3169
3170                 DPRINTF("Endpoint %u FIFO size: IN=%u, OUT=%u, DYN=%d\n",
3171                     temp, ftx, frx, dynfifo);
3172
3173                 if (dynfifo) {
3174                         if (frx && (temp <= nrx)) {
3175                                 if (temp == 1) {
3176                                         frx = 12;       /* 4K */
3177                                         MUSB2_WRITE_1(sc, MUSB2_REG_RXFIFOSZ, 
3178                                             MUSB2_VAL_FIFOSZ_4096 |
3179                                             MUSB2_MASK_FIFODB);
3180                                 } else if (temp < 8) {
3181                                         frx = 10;       /* 1K */
3182                                         MUSB2_WRITE_1(sc, MUSB2_REG_RXFIFOSZ, 
3183                                             MUSB2_VAL_FIFOSZ_512 |
3184                                             MUSB2_MASK_FIFODB);
3185                                 } else {
3186                                         frx = 7;        /* 128 bytes */
3187                                         MUSB2_WRITE_1(sc, MUSB2_REG_RXFIFOSZ, 
3188                                             MUSB2_VAL_FIFOSZ_128);
3189                                 }
3190
3191                                 MUSB2_WRITE_2(sc, MUSB2_REG_RXFIFOADD,
3192                                     offset >> 3);
3193
3194                                 offset += (1 << frx);
3195                         }
3196                         if (ftx && (temp <= ntx)) {
3197                                 if (temp == 1) {
3198                                         ftx = 12;       /* 4K */
3199                                         MUSB2_WRITE_1(sc, MUSB2_REG_TXFIFOSZ,
3200                                             MUSB2_VAL_FIFOSZ_4096 |
3201                                             MUSB2_MASK_FIFODB);
3202                                 } else if (temp < 8) {
3203                                         ftx = 10;       /* 1K */
3204                                         MUSB2_WRITE_1(sc, MUSB2_REG_TXFIFOSZ,
3205                                             MUSB2_VAL_FIFOSZ_512 |
3206                                             MUSB2_MASK_FIFODB);
3207                                 } else {
3208                                         ftx = 7;        /* 128 bytes */
3209                                         MUSB2_WRITE_1(sc, MUSB2_REG_TXFIFOSZ,
3210                                             MUSB2_VAL_FIFOSZ_128);
3211                                 }
3212
3213                                 MUSB2_WRITE_2(sc, MUSB2_REG_TXFIFOADD,
3214                                     offset >> 3);
3215
3216                                 offset += (1 << ftx);
3217                         }
3218                 }
3219
3220                 if (frx && ftx && (temp <= nrx) && (temp <= ntx)) {
3221                         pf->max_in_frame_size = 1 << ftx;
3222                         pf->max_out_frame_size = 1 << frx;
3223                         pf->is_simplex = 0;     /* duplex */
3224                         pf->support_multi_buffer = 1;
3225                         pf->support_bulk = 1;
3226                         pf->support_interrupt = 1;
3227                         pf->support_isochronous = 1;
3228                         pf->support_in = 1;
3229                         pf->support_out = 1;
3230                 } else if (frx && (temp <= nrx)) {
3231                         pf->max_out_frame_size = 1 << frx;
3232                         pf->is_simplex = 1;     /* simplex */
3233                         pf->support_multi_buffer = 1;
3234                         pf->support_bulk = 1;
3235                         pf->support_interrupt = 1;
3236                         pf->support_isochronous = 1;
3237                         pf->support_out = 1;
3238                 } else if (ftx && (temp <= ntx)) {
3239                         pf->max_in_frame_size = 1 << ftx;
3240                         pf->is_simplex = 1;     /* simplex */
3241                         pf->support_multi_buffer = 1;
3242                         pf->support_bulk = 1;
3243                         pf->support_interrupt = 1;
3244                         pf->support_isochronous = 1;
3245                         pf->support_in = 1;
3246                 }
3247         }
3248
3249         DPRINTFN(2, "Dynamic FIFO size = %d bytes\n", offset);
3250
3251         /* turn on default interrupts */
3252
3253         if (sc->sc_mode == MUSB2_HOST_MODE)
3254                 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, 0xff);
3255         else
3256                 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE,
3257                     MUSB2_MASK_IRESET);
3258
3259         musbotg_clocks_off(sc);
3260
3261         USB_BUS_UNLOCK(&sc->sc_bus);
3262
3263         /* catch any lost interrupts */
3264
3265         musbotg_do_poll(&sc->sc_bus);
3266
3267         return (0);                     /* success */
3268 }
3269
3270 void
3271 musbotg_uninit(struct musbotg_softc *sc)
3272 {
3273         USB_BUS_LOCK(&sc->sc_bus);
3274
3275         /* disable all interrupts */
3276         MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, 0);
3277         MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, 0);
3278         MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, 0);
3279
3280         sc->sc_flags.port_powered = 0;
3281         sc->sc_flags.status_vbus = 0;
3282         sc->sc_flags.status_bus_reset = 0;
3283         sc->sc_flags.status_suspend = 0;
3284         sc->sc_flags.change_suspend = 0;
3285         sc->sc_flags.change_connect = 1;
3286
3287         musbotg_pull_down(sc);
3288         musbotg_clocks_off(sc);
3289         USB_BUS_UNLOCK(&sc->sc_bus);
3290 }
3291
3292 static void
3293 musbotg_suspend(struct musbotg_softc *sc)
3294 {
3295         /* TODO */
3296 }
3297
3298 static void
3299 musbotg_resume(struct musbotg_softc *sc)
3300 {
3301         /* TODO */
3302 }
3303
3304 static void
3305 musbotg_do_poll(struct usb_bus *bus)
3306 {
3307         struct musbotg_softc *sc = MUSBOTG_BUS2SC(bus);
3308
3309         USB_BUS_LOCK(&sc->sc_bus);
3310         musbotg_interrupt_poll(sc);
3311         USB_BUS_UNLOCK(&sc->sc_bus);
3312 }
3313
3314 /*------------------------------------------------------------------------*
3315  * musbotg bulk support
3316  *------------------------------------------------------------------------*/
3317 static void
3318 musbotg_device_bulk_open(struct usb_xfer *xfer)
3319 {
3320         return;
3321 }
3322
3323 static void
3324 musbotg_device_bulk_close(struct usb_xfer *xfer)
3325 {
3326         musbotg_device_done(xfer, USB_ERR_CANCELLED);
3327 }
3328
3329 static void
3330 musbotg_device_bulk_enter(struct usb_xfer *xfer)
3331 {
3332         return;
3333 }
3334
3335 static void
3336 musbotg_device_bulk_start(struct usb_xfer *xfer)
3337 {
3338         /* setup TDs */
3339         musbotg_setup_standard_chain(xfer);
3340         musbotg_start_standard_chain(xfer);
3341 }
3342
3343 struct usb_pipe_methods musbotg_device_bulk_methods =
3344 {
3345         .open = musbotg_device_bulk_open,
3346         .close = musbotg_device_bulk_close,
3347         .enter = musbotg_device_bulk_enter,
3348         .start = musbotg_device_bulk_start,
3349 };
3350
3351 /*------------------------------------------------------------------------*
3352  * musbotg control support
3353  *------------------------------------------------------------------------*/
3354 static void
3355 musbotg_device_ctrl_open(struct usb_xfer *xfer)
3356 {
3357         return;
3358 }
3359
3360 static void
3361 musbotg_device_ctrl_close(struct usb_xfer *xfer)
3362 {
3363         musbotg_device_done(xfer, USB_ERR_CANCELLED);
3364 }
3365
3366 static void
3367 musbotg_device_ctrl_enter(struct usb_xfer *xfer)
3368 {
3369         return;
3370 }
3371
3372 static void
3373 musbotg_device_ctrl_start(struct usb_xfer *xfer)
3374 {
3375         /* setup TDs */
3376         musbotg_setup_standard_chain(xfer);
3377         musbotg_start_standard_chain(xfer);
3378 }
3379
3380 struct usb_pipe_methods musbotg_device_ctrl_methods =
3381 {
3382         .open = musbotg_device_ctrl_open,
3383         .close = musbotg_device_ctrl_close,
3384         .enter = musbotg_device_ctrl_enter,
3385         .start = musbotg_device_ctrl_start,
3386 };
3387
3388 /*------------------------------------------------------------------------*
3389  * musbotg interrupt support
3390  *------------------------------------------------------------------------*/
3391 static void
3392 musbotg_device_intr_open(struct usb_xfer *xfer)
3393 {
3394         return;
3395 }
3396
3397 static void
3398 musbotg_device_intr_close(struct usb_xfer *xfer)
3399 {
3400         musbotg_device_done(xfer, USB_ERR_CANCELLED);
3401 }
3402
3403 static void
3404 musbotg_device_intr_enter(struct usb_xfer *xfer)
3405 {
3406         return;
3407 }
3408
3409 static void
3410 musbotg_device_intr_start(struct usb_xfer *xfer)
3411 {
3412         /* setup TDs */
3413         musbotg_setup_standard_chain(xfer);
3414         musbotg_start_standard_chain(xfer);
3415 }
3416
3417 struct usb_pipe_methods musbotg_device_intr_methods =
3418 {
3419         .open = musbotg_device_intr_open,
3420         .close = musbotg_device_intr_close,
3421         .enter = musbotg_device_intr_enter,
3422         .start = musbotg_device_intr_start,
3423 };
3424
3425 /*------------------------------------------------------------------------*
3426  * musbotg full speed isochronous support
3427  *------------------------------------------------------------------------*/
3428 static void
3429 musbotg_device_isoc_open(struct usb_xfer *xfer)
3430 {
3431         return;
3432 }
3433
3434 static void
3435 musbotg_device_isoc_close(struct usb_xfer *xfer)
3436 {
3437         musbotg_device_done(xfer, USB_ERR_CANCELLED);
3438 }
3439
3440 static void
3441 musbotg_device_isoc_enter(struct usb_xfer *xfer)
3442 {
3443         struct musbotg_softc *sc = MUSBOTG_BUS2SC(xfer->xroot->bus);
3444         uint32_t temp;
3445         uint32_t nframes;
3446         uint32_t fs_frames;
3447
3448         DPRINTFN(5, "xfer=%p next=%d nframes=%d\n",
3449             xfer, xfer->endpoint->isoc_next, xfer->nframes);
3450
3451         /* get the current frame index */
3452
3453         nframes = MUSB2_READ_2(sc, MUSB2_REG_FRAME);
3454
3455         /*
3456          * check if the frame index is within the window where the frames
3457          * will be inserted
3458          */
3459         temp = (nframes - xfer->endpoint->isoc_next) & MUSB2_MASK_FRAME;
3460
3461         if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
3462                 fs_frames = (xfer->nframes + 7) / 8;
3463         } else {
3464                 fs_frames = xfer->nframes;
3465         }
3466
3467         if ((xfer->endpoint->is_synced == 0) ||
3468             (temp < fs_frames)) {
3469                 /*
3470                  * If there is data underflow or the pipe queue is
3471                  * empty we schedule the transfer a few frames ahead
3472                  * of the current frame position. Else two isochronous
3473                  * transfers might overlap.
3474                  */
3475                 xfer->endpoint->isoc_next = (nframes + 3) & MUSB2_MASK_FRAME;
3476                 xfer->endpoint->is_synced = 1;
3477                 DPRINTFN(2, "start next=%d\n", xfer->endpoint->isoc_next);
3478         }
3479         /*
3480          * compute how many milliseconds the insertion is ahead of the
3481          * current frame position:
3482          */
3483         temp = (xfer->endpoint->isoc_next - nframes) & MUSB2_MASK_FRAME;
3484
3485         /*
3486          * pre-compute when the isochronous transfer will be finished:
3487          */
3488         xfer->isoc_time_complete =
3489             usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
3490             fs_frames;
3491
3492         /* compute frame number for next insertion */
3493         xfer->endpoint->isoc_next += fs_frames;
3494
3495         /* setup TDs */
3496         musbotg_setup_standard_chain(xfer);
3497 }
3498
3499 static void
3500 musbotg_device_isoc_start(struct usb_xfer *xfer)
3501 {
3502         /* start TD chain */
3503         musbotg_start_standard_chain(xfer);
3504 }
3505
3506 struct usb_pipe_methods musbotg_device_isoc_methods =
3507 {
3508         .open = musbotg_device_isoc_open,
3509         .close = musbotg_device_isoc_close,
3510         .enter = musbotg_device_isoc_enter,
3511         .start = musbotg_device_isoc_start,
3512 };
3513
3514 /*------------------------------------------------------------------------*
3515  * musbotg root control support
3516  *------------------------------------------------------------------------*
3517  * Simulate a hardware HUB by handling all the necessary requests.
3518  *------------------------------------------------------------------------*/
3519
3520 static const struct usb_device_descriptor musbotg_devd = {
3521         .bLength = sizeof(struct usb_device_descriptor),
3522         .bDescriptorType = UDESC_DEVICE,
3523         .bcdUSB = {0x00, 0x02},
3524         .bDeviceClass = UDCLASS_HUB,
3525         .bDeviceSubClass = UDSUBCLASS_HUB,
3526         .bDeviceProtocol = UDPROTO_HSHUBSTT,
3527         .bMaxPacketSize = 64,
3528         .bcdDevice = {0x00, 0x01},
3529         .iManufacturer = 1,
3530         .iProduct = 2,
3531         .bNumConfigurations = 1,
3532 };
3533
3534 static const struct usb_device_qualifier musbotg_odevd = {
3535         .bLength = sizeof(struct usb_device_qualifier),
3536         .bDescriptorType = UDESC_DEVICE_QUALIFIER,
3537         .bcdUSB = {0x00, 0x02},
3538         .bDeviceClass = UDCLASS_HUB,
3539         .bDeviceSubClass = UDSUBCLASS_HUB,
3540         .bDeviceProtocol = UDPROTO_FSHUB,
3541         .bMaxPacketSize0 = 0,
3542         .bNumConfigurations = 0,
3543 };
3544
3545 static const struct musbotg_config_desc musbotg_confd = {
3546         .confd = {
3547                 .bLength = sizeof(struct usb_config_descriptor),
3548                 .bDescriptorType = UDESC_CONFIG,
3549                 .wTotalLength[0] = sizeof(musbotg_confd),
3550                 .bNumInterface = 1,
3551                 .bConfigurationValue = 1,
3552                 .iConfiguration = 0,
3553                 .bmAttributes = UC_SELF_POWERED,
3554                 .bMaxPower = 0,
3555         },
3556         .ifcd = {
3557                 .bLength = sizeof(struct usb_interface_descriptor),
3558                 .bDescriptorType = UDESC_INTERFACE,
3559                 .bNumEndpoints = 1,
3560                 .bInterfaceClass = UICLASS_HUB,
3561                 .bInterfaceSubClass = UISUBCLASS_HUB,
3562                 .bInterfaceProtocol = 0,
3563         },
3564         .endpd = {
3565                 .bLength = sizeof(struct usb_endpoint_descriptor),
3566                 .bDescriptorType = UDESC_ENDPOINT,
3567                 .bEndpointAddress = (UE_DIR_IN | MUSBOTG_INTR_ENDPT),
3568                 .bmAttributes = UE_INTERRUPT,
3569                 .wMaxPacketSize[0] = 8,
3570                 .bInterval = 255,
3571         },
3572 };
3573
3574 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
3575
3576 static const struct usb_hub_descriptor_min musbotg_hubd = {
3577         .bDescLength = sizeof(musbotg_hubd),
3578         .bDescriptorType = UDESC_HUB,
3579         .bNbrPorts = 1,
3580         HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
3581         .bPwrOn2PwrGood = 50,
3582         .bHubContrCurrent = 0,
3583         .DeviceRemovable = {0},         /* port is removable */
3584 };
3585
3586 #define STRING_VENDOR \
3587   "M\0e\0n\0t\0o\0r\0 \0G\0r\0a\0p\0h\0i\0c\0s"
3588
3589 #define STRING_PRODUCT \
3590   "O\0T\0G\0 \0R\0o\0o\0t\0 \0H\0U\0B"
3591
3592 USB_MAKE_STRING_DESC(STRING_VENDOR, musbotg_vendor);
3593 USB_MAKE_STRING_DESC(STRING_PRODUCT, musbotg_product);
3594
3595 static usb_error_t
3596 musbotg_roothub_exec(struct usb_device *udev,
3597     struct usb_device_request *req, const void **pptr, uint16_t *plength)
3598 {
3599         struct musbotg_softc *sc = MUSBOTG_BUS2SC(udev->bus);
3600         const void *ptr;
3601         uint16_t len;
3602         uint16_t value;
3603         uint16_t index;
3604         uint8_t reg;
3605         usb_error_t err;
3606
3607         USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3608
3609         /* buffer reset */
3610         ptr = (const void *)&sc->sc_hub_temp;
3611         len = 0;
3612         err = 0;
3613
3614         value = UGETW(req->wValue);
3615         index = UGETW(req->wIndex);
3616
3617         /* demultiplex the control request */
3618
3619         switch (req->bmRequestType) {
3620         case UT_READ_DEVICE:
3621                 switch (req->bRequest) {
3622                 case UR_GET_DESCRIPTOR:
3623                         goto tr_handle_get_descriptor;
3624                 case UR_GET_CONFIG:
3625                         goto tr_handle_get_config;
3626                 case UR_GET_STATUS:
3627                         goto tr_handle_get_status;
3628                 default:
3629                         goto tr_stalled;
3630                 }
3631                 break;
3632
3633         case UT_WRITE_DEVICE:
3634                 switch (req->bRequest) {
3635                 case UR_SET_ADDRESS:
3636                         goto tr_handle_set_address;
3637                 case UR_SET_CONFIG:
3638                         goto tr_handle_set_config;
3639                 case UR_CLEAR_FEATURE:
3640                         goto tr_valid;  /* nop */
3641                 case UR_SET_DESCRIPTOR:
3642                         goto tr_valid;  /* nop */
3643                 case UR_SET_FEATURE:
3644                 default:
3645                         goto tr_stalled;
3646                 }
3647                 break;
3648
3649         case UT_WRITE_ENDPOINT:
3650                 switch (req->bRequest) {
3651                 case UR_CLEAR_FEATURE:
3652                         switch (UGETW(req->wValue)) {
3653                         case UF_ENDPOINT_HALT:
3654                                 goto tr_handle_clear_halt;
3655                         case UF_DEVICE_REMOTE_WAKEUP:
3656                                 goto tr_handle_clear_wakeup;
3657                         default:
3658                                 goto tr_stalled;
3659                         }
3660                         break;
3661                 case UR_SET_FEATURE:
3662                         switch (UGETW(req->wValue)) {
3663                         case UF_ENDPOINT_HALT:
3664                                 goto tr_handle_set_halt;
3665                         case UF_DEVICE_REMOTE_WAKEUP:
3666                                 goto tr_handle_set_wakeup;
3667                         default:
3668                                 goto tr_stalled;
3669                         }
3670                         break;
3671                 case UR_SYNCH_FRAME:
3672                         goto tr_valid;  /* nop */
3673                 default:
3674                         goto tr_stalled;
3675                 }
3676                 break;
3677
3678         case UT_READ_ENDPOINT:
3679                 switch (req->bRequest) {
3680                 case UR_GET_STATUS:
3681                         goto tr_handle_get_ep_status;
3682                 default:
3683                         goto tr_stalled;
3684                 }
3685                 break;
3686
3687         case UT_WRITE_INTERFACE:
3688                 switch (req->bRequest) {
3689                 case UR_SET_INTERFACE:
3690                         goto tr_handle_set_interface;
3691                 case UR_CLEAR_FEATURE:
3692                         goto tr_valid;  /* nop */
3693                 case UR_SET_FEATURE:
3694                 default:
3695                         goto tr_stalled;
3696                 }
3697                 break;
3698
3699         case UT_READ_INTERFACE:
3700                 switch (req->bRequest) {
3701                 case UR_GET_INTERFACE:
3702                         goto tr_handle_get_interface;
3703                 case UR_GET_STATUS:
3704                         goto tr_handle_get_iface_status;
3705                 default:
3706                         goto tr_stalled;
3707                 }
3708                 break;
3709
3710         case UT_WRITE_CLASS_INTERFACE:
3711         case UT_WRITE_VENDOR_INTERFACE:
3712                 /* XXX forward */
3713                 break;
3714
3715         case UT_READ_CLASS_INTERFACE:
3716         case UT_READ_VENDOR_INTERFACE:
3717                 /* XXX forward */
3718                 break;
3719
3720         case UT_WRITE_CLASS_DEVICE:
3721                 switch (req->bRequest) {
3722                 case UR_CLEAR_FEATURE:
3723                         goto tr_valid;
3724                 case UR_SET_DESCRIPTOR:
3725                 case UR_SET_FEATURE:
3726                         break;
3727                 default:
3728                         goto tr_stalled;
3729                 }
3730                 break;
3731
3732         case UT_WRITE_CLASS_OTHER:
3733                 switch (req->bRequest) {
3734                 case UR_CLEAR_FEATURE:
3735                         goto tr_handle_clear_port_feature;
3736                 case UR_SET_FEATURE:
3737                         goto tr_handle_set_port_feature;
3738                 case UR_CLEAR_TT_BUFFER:
3739                 case UR_RESET_TT:
3740                 case UR_STOP_TT:
3741                         goto tr_valid;
3742
3743                 default:
3744                         goto tr_stalled;
3745                 }
3746                 break;
3747
3748         case UT_READ_CLASS_OTHER:
3749                 switch (req->bRequest) {
3750                 case UR_GET_TT_STATE:
3751                         goto tr_handle_get_tt_state;
3752                 case UR_GET_STATUS:
3753                         goto tr_handle_get_port_status;
3754                 default:
3755                         goto tr_stalled;
3756                 }
3757                 break;
3758
3759         case UT_READ_CLASS_DEVICE:
3760                 switch (req->bRequest) {
3761                 case UR_GET_DESCRIPTOR:
3762                         goto tr_handle_get_class_descriptor;
3763                 case UR_GET_STATUS:
3764                         goto tr_handle_get_class_status;
3765
3766                 default:
3767                         goto tr_stalled;
3768                 }
3769                 break;
3770         default:
3771                 goto tr_stalled;
3772         }
3773         goto tr_valid;
3774
3775 tr_handle_get_descriptor:
3776         switch (value >> 8) {
3777         case UDESC_DEVICE:
3778                 if (value & 0xff) {
3779                         goto tr_stalled;
3780                 }
3781                 len = sizeof(musbotg_devd);
3782                 ptr = (const void *)&musbotg_devd;
3783                 goto tr_valid;
3784         case UDESC_DEVICE_QUALIFIER:
3785                 if (value & 0xff) {
3786                         goto tr_stalled;
3787                 }
3788                 len = sizeof(musbotg_odevd);
3789                 ptr = (const void *)&musbotg_odevd;
3790                 goto tr_valid;
3791         case UDESC_CONFIG:
3792                 if (value & 0xff) {
3793                         goto tr_stalled;
3794                 }
3795                 len = sizeof(musbotg_confd);
3796                 ptr = (const void *)&musbotg_confd;
3797                 goto tr_valid;
3798         case UDESC_STRING:
3799                 switch (value & 0xff) {
3800                 case 0:         /* Language table */
3801                         len = sizeof(usb_string_lang_en);
3802                         ptr = (const void *)&usb_string_lang_en;
3803                         goto tr_valid;
3804
3805                 case 1:         /* Vendor */
3806                         len = sizeof(musbotg_vendor);
3807                         ptr = (const void *)&musbotg_vendor;
3808                         goto tr_valid;
3809
3810                 case 2:         /* Product */
3811                         len = sizeof(musbotg_product);
3812                         ptr = (const void *)&musbotg_product;
3813                         goto tr_valid;
3814                 default:
3815                         break;
3816                 }
3817                 break;
3818         default:
3819                 goto tr_stalled;
3820         }
3821         goto tr_stalled;
3822
3823 tr_handle_get_config:
3824         len = 1;
3825         sc->sc_hub_temp.wValue[0] = sc->sc_conf;
3826         goto tr_valid;
3827
3828 tr_handle_get_status:
3829         len = 2;
3830         USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
3831         goto tr_valid;
3832
3833 tr_handle_set_address:
3834         if (value & 0xFF00) {
3835                 goto tr_stalled;
3836         }
3837         sc->sc_rt_addr = value;
3838         goto tr_valid;
3839
3840 tr_handle_set_config:
3841         if (value >= 2) {
3842                 goto tr_stalled;
3843         }
3844         sc->sc_conf = value;
3845         goto tr_valid;
3846
3847 tr_handle_get_interface:
3848         len = 1;
3849         sc->sc_hub_temp.wValue[0] = 0;
3850         goto tr_valid;
3851
3852 tr_handle_get_tt_state:
3853 tr_handle_get_class_status:
3854 tr_handle_get_iface_status:
3855 tr_handle_get_ep_status:
3856         len = 2;
3857         USETW(sc->sc_hub_temp.wValue, 0);
3858         goto tr_valid;
3859
3860 tr_handle_set_halt:
3861 tr_handle_set_interface:
3862 tr_handle_set_wakeup:
3863 tr_handle_clear_wakeup:
3864 tr_handle_clear_halt:
3865         goto tr_valid;
3866
3867 tr_handle_clear_port_feature:
3868         if (index != 1) {
3869                 goto tr_stalled;
3870         }
3871         DPRINTFN(8, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
3872
3873         switch (value) {
3874         case UHF_PORT_SUSPEND:
3875                 if (sc->sc_mode == MUSB2_HOST_MODE)
3876                         musbotg_wakeup_host(sc);
3877                 else
3878                         musbotg_wakeup_peer(sc);
3879                 break;
3880
3881         case UHF_PORT_ENABLE:
3882                 sc->sc_flags.port_enabled = 0;
3883                 break;
3884
3885         case UHF_C_PORT_ENABLE:
3886                 sc->sc_flags.change_enabled = 0;
3887                 break;
3888
3889         case UHF_C_PORT_OVER_CURRENT:
3890                 sc->sc_flags.change_over_current = 0;
3891                 break;
3892
3893         case UHF_C_PORT_RESET:
3894                 sc->sc_flags.change_reset = 0;
3895                 break;
3896
3897         case UHF_PORT_TEST:
3898         case UHF_PORT_INDICATOR:
3899                 /* nops */
3900                 break;
3901
3902         case UHF_PORT_POWER:
3903                 sc->sc_flags.port_powered = 0;
3904                 musbotg_pull_down(sc);
3905                 musbotg_clocks_off(sc);
3906                 break;
3907         case UHF_C_PORT_CONNECTION:
3908                 sc->sc_flags.change_connect = 0;
3909                 break;
3910         case UHF_C_PORT_SUSPEND:
3911                 sc->sc_flags.change_suspend = 0;
3912                 break;
3913         default:
3914                 err = USB_ERR_IOERROR;
3915                 goto done;
3916         }
3917         goto tr_valid;
3918
3919 tr_handle_set_port_feature:
3920         if (index != 1) {
3921                 goto tr_stalled;
3922         }
3923         DPRINTFN(8, "UR_SET_PORT_FEATURE\n");
3924
3925         switch (value) {
3926         case UHF_PORT_ENABLE:
3927                 sc->sc_flags.port_enabled = 1;
3928                 break;
3929         case UHF_PORT_SUSPEND:
3930                 if (sc->sc_mode == MUSB2_HOST_MODE)
3931                         musbotg_suspend_host(sc);
3932                 break;
3933
3934         case UHF_PORT_RESET:
3935                 if (sc->sc_mode == MUSB2_HOST_MODE) {
3936                         reg = MUSB2_READ_1(sc, MUSB2_REG_POWER);
3937                         reg |= MUSB2_MASK_RESET;
3938                         MUSB2_WRITE_1(sc, MUSB2_REG_POWER, reg);
3939
3940                         /* Wait for 20 msec */
3941                         usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 5);
3942
3943                         reg = MUSB2_READ_1(sc, MUSB2_REG_POWER);
3944                         reg &= ~MUSB2_MASK_RESET;
3945                         MUSB2_WRITE_1(sc, MUSB2_REG_POWER, reg);
3946
3947                         /* determine line speed */
3948                         reg = MUSB2_READ_1(sc, MUSB2_REG_POWER);
3949                         if (reg & MUSB2_MASK_HSMODE)
3950                                 sc->sc_flags.status_high_speed = 1;
3951                         else
3952                                 sc->sc_flags.status_high_speed = 0;
3953
3954                         sc->sc_flags.change_reset = 1;
3955                 } else
3956                         err = USB_ERR_IOERROR;
3957                 break;
3958
3959         case UHF_PORT_TEST:
3960         case UHF_PORT_INDICATOR:
3961                 /* nops */
3962                 break;
3963         case UHF_PORT_POWER:
3964                 sc->sc_flags.port_powered = 1;
3965                 break;
3966         default:
3967                 err = USB_ERR_IOERROR;
3968                 goto done;
3969         }
3970         goto tr_valid;
3971
3972 tr_handle_get_port_status:
3973
3974         DPRINTFN(8, "UR_GET_PORT_STATUS\n");
3975
3976         if (index != 1) {
3977                 goto tr_stalled;
3978         }
3979         if (sc->sc_flags.status_vbus) {
3980                 musbotg_clocks_on(sc);
3981                 musbotg_pull_up(sc);
3982         } else {
3983                 musbotg_pull_down(sc);
3984                 musbotg_clocks_off(sc);
3985         }
3986
3987         /* Select Device Side Mode */
3988         if (sc->sc_mode == MUSB2_DEVICE_MODE)
3989                 value = UPS_PORT_MODE_DEVICE;
3990         else
3991                 value = 0;
3992
3993         if (sc->sc_flags.status_high_speed) {
3994                 value |= UPS_HIGH_SPEED;
3995         }
3996         if (sc->sc_flags.port_powered) {
3997                 value |= UPS_PORT_POWER;
3998         }
3999         if (sc->sc_flags.port_enabled) {
4000                 value |= UPS_PORT_ENABLED;
4001         }
4002
4003         if (sc->sc_flags.port_over_current)
4004                 value |= UPS_OVERCURRENT_INDICATOR;
4005
4006         if (sc->sc_flags.status_vbus &&
4007             sc->sc_flags.status_bus_reset) {
4008                 value |= UPS_CURRENT_CONNECT_STATUS;
4009         }
4010         if (sc->sc_flags.status_suspend) {
4011                 value |= UPS_SUSPEND;
4012         }
4013         USETW(sc->sc_hub_temp.ps.wPortStatus, value);
4014
4015         value = 0;
4016
4017         if (sc->sc_flags.change_connect) {
4018                 value |= UPS_C_CONNECT_STATUS;
4019
4020                 if (sc->sc_mode == MUSB2_DEVICE_MODE) {
4021                         if (sc->sc_flags.status_vbus &&
4022                             sc->sc_flags.status_bus_reset) {
4023                                 /* reset EP0 state */
4024                                 sc->sc_ep0_busy = 0;
4025                                 sc->sc_ep0_cmd = 0;
4026                         }
4027                 }
4028         }
4029         if (sc->sc_flags.change_suspend)
4030                 value |= UPS_C_SUSPEND;
4031         if (sc->sc_flags.change_reset)
4032                 value |= UPS_C_PORT_RESET;
4033         if (sc->sc_flags.change_over_current)
4034                 value |= UPS_C_OVERCURRENT_INDICATOR;
4035
4036         USETW(sc->sc_hub_temp.ps.wPortChange, value);
4037         len = sizeof(sc->sc_hub_temp.ps);
4038         goto tr_valid;
4039
4040 tr_handle_get_class_descriptor:
4041         if (value & 0xFF) {
4042                 goto tr_stalled;
4043         }
4044         ptr = (const void *)&musbotg_hubd;
4045         len = sizeof(musbotg_hubd);
4046         goto tr_valid;
4047
4048 tr_stalled:
4049         err = USB_ERR_STALLED;
4050 tr_valid:
4051 done:
4052         *plength = len;
4053         *pptr = ptr;
4054         return (err);
4055 }
4056
4057 static void
4058 musbotg_xfer_setup(struct usb_setup_params *parm)
4059 {
4060         struct musbotg_softc *sc;
4061         struct usb_xfer *xfer;
4062         void *last_obj;
4063         uint32_t ntd;
4064         uint32_t n;
4065         uint8_t ep_no;
4066
4067         sc = MUSBOTG_BUS2SC(parm->udev->bus);
4068         xfer = parm->curr_xfer;
4069
4070         /*
4071          * NOTE: This driver does not use any of the parameters that
4072          * are computed from the following values. Just set some
4073          * reasonable dummies:
4074          */
4075         parm->hc_max_packet_size = 0x400;
4076         parm->hc_max_frame_size = 0xc00;
4077
4078         if ((parm->methods == &musbotg_device_isoc_methods) ||
4079             (parm->methods == &musbotg_device_intr_methods))
4080                 parm->hc_max_packet_count = 3;
4081         else
4082                 parm->hc_max_packet_count = 1;
4083
4084         usbd_transfer_setup_sub(parm);
4085
4086         /*
4087          * compute maximum number of TDs
4088          */
4089         if (parm->methods == &musbotg_device_ctrl_methods) {
4090
4091                 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC */ ;
4092
4093         } else if (parm->methods == &musbotg_device_bulk_methods) {
4094
4095                 ntd = xfer->nframes + 1 /* SYNC */ ;
4096
4097         } else if (parm->methods == &musbotg_device_intr_methods) {
4098
4099                 ntd = xfer->nframes + 1 /* SYNC */ ;
4100
4101         } else if (parm->methods == &musbotg_device_isoc_methods) {
4102
4103                 ntd = xfer->nframes + 1 /* SYNC */ ;
4104
4105         } else {
4106
4107                 ntd = 0;
4108         }
4109
4110         /*
4111          * check if "usbd_transfer_setup_sub" set an error
4112          */
4113         if (parm->err) {
4114                 return;
4115         }
4116         /*
4117          * allocate transfer descriptors
4118          */
4119         last_obj = NULL;
4120
4121         ep_no = xfer->endpointno & UE_ADDR;
4122
4123         /*
4124          * Check for a valid endpoint profile in USB device mode:
4125          */
4126         if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
4127                 const struct usb_hw_ep_profile *pf;
4128
4129                 musbotg_get_hw_ep_profile(parm->udev, &pf, ep_no);
4130
4131                 if (pf == NULL) {
4132                         /* should not happen */
4133                         parm->err = USB_ERR_INVAL;
4134                         return;
4135                 }
4136         }
4137
4138         /* align data */
4139         parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
4140
4141         for (n = 0; n != ntd; n++) {
4142
4143                 struct musbotg_td *td;
4144
4145                 if (parm->buf) {
4146
4147                         td = USB_ADD_BYTES(parm->buf, parm->size[0]);
4148
4149                         /* init TD */
4150                         td->max_frame_size = xfer->max_frame_size;
4151                         td->reg_max_packet = xfer->max_packet_size |
4152                             ((xfer->max_packet_count - 1) << 11);
4153                         td->ep_no = ep_no;
4154                         td->obj_next = last_obj;
4155
4156                         last_obj = td;
4157                 }
4158                 parm->size[0] += sizeof(*td);
4159         }
4160
4161         xfer->td_start[0] = last_obj;
4162 }
4163
4164 static void
4165 musbotg_xfer_unsetup(struct usb_xfer *xfer)
4166 {
4167         return;
4168 }
4169
4170 static void
4171 musbotg_get_dma_delay(struct usb_device *udev, uint32_t *pus)
4172 {
4173         struct musbotg_softc *sc = MUSBOTG_BUS2SC(udev->bus);
4174
4175         if (sc->sc_mode == MUSB2_HOST_MODE)
4176                 *pus = 2000;                   /* microseconds */
4177         else
4178                 *pus = 0;
4179 }
4180
4181 static void
4182 musbotg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
4183     struct usb_endpoint *ep)
4184 {
4185         struct musbotg_softc *sc = MUSBOTG_BUS2SC(udev->bus);
4186
4187         DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
4188             ep, udev->address,
4189             edesc->bEndpointAddress, udev->flags.usb_mode,
4190             sc->sc_rt_addr);
4191
4192         if (udev->device_index != sc->sc_rt_addr) {
4193                 switch (edesc->bmAttributes & UE_XFERTYPE) {
4194                 case UE_CONTROL:
4195                         ep->methods = &musbotg_device_ctrl_methods;
4196                         break;
4197                 case UE_INTERRUPT:
4198                         ep->methods = &musbotg_device_intr_methods;
4199                         break;
4200                 case UE_ISOCHRONOUS:
4201                         ep->methods = &musbotg_device_isoc_methods;
4202                         break;
4203                 case UE_BULK:
4204                         ep->methods = &musbotg_device_bulk_methods;
4205                         break;
4206                 default:
4207                         /* do nothing */
4208                         break;
4209                 }
4210         }
4211 }
4212
4213 static void
4214 musbotg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
4215 {
4216         struct musbotg_softc *sc = MUSBOTG_BUS2SC(bus);
4217
4218         switch (state) {
4219         case USB_HW_POWER_SUSPEND:
4220                 musbotg_suspend(sc);
4221                 break;
4222         case USB_HW_POWER_SHUTDOWN:
4223                 musbotg_uninit(sc);
4224                 break;
4225         case USB_HW_POWER_RESUME:
4226                 musbotg_resume(sc);
4227                 break;
4228         default:
4229                 break;
4230         }
4231 }
4232
4233 struct usb_bus_methods musbotg_bus_methods =
4234 {
4235         .endpoint_init = &musbotg_ep_init,
4236         .get_dma_delay = &musbotg_get_dma_delay,
4237         .xfer_setup = &musbotg_xfer_setup,
4238         .xfer_unsetup = &musbotg_xfer_unsetup,
4239         .get_hw_ep_profile = &musbotg_get_hw_ep_profile,
4240         .xfer_stall = &musbotg_xfer_stall,
4241         .set_stall = &musbotg_set_stall,
4242         .clear_stall = &musbotg_clear_stall,
4243         .roothub_exec = &musbotg_roothub_exec,
4244         .xfer_poll = &musbotg_do_poll,
4245         .set_hw_power_sleep = &musbotg_set_hw_power_sleep,
4246 };