ehci.c revision 1.234.2.110 1 /* $NetBSD: ehci.c,v 1.234.2.110 2017/09/05 07:01:12 skrll Exp $ */
2
3 /*
4 * Copyright (c) 2004-2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net), Charles M. Hannum,
9 * Jeremy Morse (jeremy.morse (at) gmail.com), Jared D. McNeill
10 * (jmcneill (at) invisible.ca) and Matthew R. Green (mrg (at) eterna.com.au).
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
36 *
37 * The EHCI 1.0 spec can be found at
38 * http://www.intel.com/technology/usb/spec.htm
39 * and the USB 2.0 spec at
40 * http://www.usb.org/developers/docs/
41 *
42 */
43
44 /*
45 * TODO:
46 * 1) hold off explorations by companion controllers until ehci has started.
47 *
48 * 2) The hub driver needs to handle and schedule the transaction translator,
49 * to assign place in frame where different devices get to go. See chapter
50 * on hubs in USB 2.0 for details.
51 *
52 * 3) Command failures are not recovered correctly.
53 */
54
55 #include <sys/cdefs.h>
56 __KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.234.2.110 2017/09/05 07:01:12 skrll Exp $");
57
58 #include "ohci.h"
59 #include "uhci.h"
60
61 #ifdef _KERNEL_OPT
62 #include "opt_usb.h"
63 #endif
64
65 #include <sys/param.h>
66
67 #include <sys/bus.h>
68 #include <sys/cpu.h>
69 #include <sys/device.h>
70 #include <sys/kernel.h>
71 #include <sys/kmem.h>
72 #include <sys/mutex.h>
73 #include <sys/proc.h>
74 #include <sys/queue.h>
75 #include <sys/select.h>
76 #include <sys/sysctl.h>
77 #include <sys/systm.h>
78
79 #include <machine/endian.h>
80
81 #include <dev/usb/usb.h>
82 #include <dev/usb/usbdi.h>
83 #include <dev/usb/usbdivar.h>
84 #include <dev/usb/usbhist.h>
85 #include <dev/usb/usb_mem.h>
86 #include <dev/usb/usb_quirks.h>
87
88 #include <dev/usb/ehcireg.h>
89 #include <dev/usb/ehcivar.h>
90 #include <dev/usb/usbroothub.h>
91
92
93 #ifdef USB_DEBUG
94 #ifndef EHCI_DEBUG
95 #define ehcidebug 0
96 #else
97 static int ehcidebug = 0;
98
99 SYSCTL_SETUP(sysctl_hw_ehci_setup, "sysctl hw.ehci setup")
100 {
101 int err;
102 const struct sysctlnode *rnode;
103 const struct sysctlnode *cnode;
104
105 err = sysctl_createv(clog, 0, NULL, &rnode,
106 CTLFLAG_PERMANENT, CTLTYPE_NODE, "ehci",
107 SYSCTL_DESCR("ehci global controls"),
108 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
109
110 if (err)
111 goto fail;
112
113 /* control debugging printfs */
114 err = sysctl_createv(clog, 0, &rnode, &cnode,
115 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
116 "debug", SYSCTL_DESCR("Enable debugging output"),
117 NULL, 0, &ehcidebug, sizeof(ehcidebug), CTL_CREATE, CTL_EOL);
118 if (err)
119 goto fail;
120
121 return;
122 fail:
123 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
124 }
125
126 #endif /* EHCI_DEBUG */
127 #endif /* USB_DEBUG */
128
129 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOG(ehcidebug,FMT,A,B,C,D)
130 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(ehcidebug,N,FMT,A,B,C,D)
131 #define EHCIHIST_FUNC() USBHIST_FUNC()
132 #define EHCIHIST_CALLED() USBHIST_CALLED(ehcidebug)
133
134 struct ehci_pipe {
135 struct usbd_pipe pipe;
136 int nexttoggle;
137
138 ehci_soft_qh_t *sqh;
139 union {
140 /* Control pipe */
141 struct {
142 usb_dma_t reqdma;
143 } ctrl;
144 /* Interrupt pipe */
145 struct {
146 u_int length;
147 } intr;
148 /* Iso pipe */
149 struct {
150 u_int next_frame;
151 u_int cur_xfers;
152 } isoc;
153 };
154 };
155
156 typedef TAILQ_HEAD(ex_completeq, ehci_xfer) ex_completeq_t;
157
158 Static usbd_status ehci_open(struct usbd_pipe *);
159 Static void ehci_poll(struct usbd_bus *);
160 Static void ehci_softintr(void *);
161 Static int ehci_intr1(ehci_softc_t *);
162 Static void ehci_check_qh_intr(ehci_softc_t *, struct ehci_xfer *,
163 ex_completeq_t *);
164 Static void ehci_check_itd_intr(ehci_softc_t *, struct ehci_xfer *,
165 ex_completeq_t *);
166 Static void ehci_check_sitd_intr(ehci_softc_t *, struct ehci_xfer *,
167 ex_completeq_t *);
168 Static void ehci_idone(struct ehci_xfer *, ex_completeq_t *);
169 Static void ehci_timeout(void *);
170 Static void ehci_timeout_task(void *);
171 Static void ehci_intrlist_timeout(void *);
172 Static void ehci_doorbell(void *);
173 Static void ehci_pcd(void *);
174
175 Static struct usbd_xfer *
176 ehci_allocx(struct usbd_bus *, unsigned int);
177 Static void ehci_freex(struct usbd_bus *, struct usbd_xfer *);
178
179 Static void ehci_get_lock(struct usbd_bus *, kmutex_t **);
180 Static int ehci_roothub_ctrl(struct usbd_bus *,
181 usb_device_request_t *, void *, int);
182
183 Static usbd_status ehci_root_intr_transfer(struct usbd_xfer *);
184 Static usbd_status ehci_root_intr_start(struct usbd_xfer *);
185 Static void ehci_root_intr_abort(struct usbd_xfer *);
186 Static void ehci_root_intr_close(struct usbd_pipe *);
187 Static void ehci_root_intr_done(struct usbd_xfer *);
188
189 Static int ehci_device_ctrl_init(struct usbd_xfer *);
190 Static void ehci_device_ctrl_fini(struct usbd_xfer *);
191 Static usbd_status ehci_device_ctrl_transfer(struct usbd_xfer *);
192 Static usbd_status ehci_device_ctrl_start(struct usbd_xfer *);
193 Static void ehci_device_ctrl_abort(struct usbd_xfer *);
194 Static void ehci_device_ctrl_close(struct usbd_pipe *);
195 Static void ehci_device_ctrl_done(struct usbd_xfer *);
196
197 Static int ehci_device_bulk_init(struct usbd_xfer *);
198 Static void ehci_device_bulk_fini(struct usbd_xfer *);
199 Static usbd_status ehci_device_bulk_transfer(struct usbd_xfer *);
200 Static usbd_status ehci_device_bulk_start(struct usbd_xfer *);
201 Static void ehci_device_bulk_abort(struct usbd_xfer *);
202 Static void ehci_device_bulk_close(struct usbd_pipe *);
203 Static void ehci_device_bulk_done(struct usbd_xfer *);
204
205 Static int ehci_device_intr_init(struct usbd_xfer *);
206 Static void ehci_device_intr_fini(struct usbd_xfer *);
207 Static usbd_status ehci_device_intr_transfer(struct usbd_xfer *);
208 Static usbd_status ehci_device_intr_start(struct usbd_xfer *);
209 Static void ehci_device_intr_abort(struct usbd_xfer *);
210 Static void ehci_device_intr_close(struct usbd_pipe *);
211 Static void ehci_device_intr_done(struct usbd_xfer *);
212
213 Static int ehci_device_isoc_init(struct usbd_xfer *);
214 Static void ehci_device_isoc_fini(struct usbd_xfer *);
215 Static usbd_status ehci_device_isoc_transfer(struct usbd_xfer *);
216 Static void ehci_device_isoc_abort(struct usbd_xfer *);
217 Static void ehci_device_isoc_close(struct usbd_pipe *);
218 Static void ehci_device_isoc_done(struct usbd_xfer *);
219
220 Static int ehci_device_fs_isoc_init(struct usbd_xfer *);
221 Static void ehci_device_fs_isoc_fini(struct usbd_xfer *);
222 Static usbd_status ehci_device_fs_isoc_transfer(struct usbd_xfer *);
223 Static void ehci_device_fs_isoc_abort(struct usbd_xfer *);
224 Static void ehci_device_fs_isoc_close(struct usbd_pipe *);
225 Static void ehci_device_fs_isoc_done(struct usbd_xfer *);
226
227 Static void ehci_device_clear_toggle(struct usbd_pipe *);
228 Static void ehci_noop(struct usbd_pipe *);
229
230 Static void ehci_disown(ehci_softc_t *, int, int);
231
232 Static ehci_soft_qh_t * ehci_alloc_sqh(ehci_softc_t *);
233 Static void ehci_free_sqh(ehci_softc_t *, ehci_soft_qh_t *);
234
235 Static ehci_soft_qtd_t *ehci_alloc_sqtd(ehci_softc_t *);
236 Static void ehci_free_sqtd(ehci_softc_t *, ehci_soft_qtd_t *);
237 Static int ehci_alloc_sqtd_chain(ehci_softc_t *,
238 struct usbd_xfer *, int, int, ehci_soft_qtd_t **);
239 Static void ehci_free_sqtds(ehci_softc_t *, struct ehci_xfer *);
240
241 Static void ehci_reset_sqtd_chain(ehci_softc_t *, struct usbd_xfer *,
242 int, int, int *, ehci_soft_qtd_t **);
243 Static void ehci_append_sqtd(ehci_soft_qtd_t *, ehci_soft_qtd_t *);
244
245 Static ehci_soft_itd_t *ehci_alloc_itd(ehci_softc_t *);
246 Static ehci_soft_sitd_t *
247 ehci_alloc_sitd(ehci_softc_t *);
248
249 Static void ehci_remove_itd_chain(ehci_softc_t *, ehci_soft_itd_t *);
250 Static void ehci_remove_sitd_chain(ehci_softc_t *, ehci_soft_sitd_t *);
251 Static void ehci_free_itd_chain(ehci_softc_t *, ehci_soft_itd_t *);
252 Static void ehci_free_sitd_chain(ehci_softc_t *, ehci_soft_sitd_t *);
253
254 static inline void
255 ehci_free_itd_locked(ehci_softc_t *sc, ehci_soft_itd_t *itd)
256 {
257
258 LIST_INSERT_HEAD(&sc->sc_freeitds, itd, free_list);
259 }
260
261 static inline void
262 ehci_free_sitd_locked(ehci_softc_t *sc, ehci_soft_sitd_t *sitd)
263 {
264
265 LIST_INSERT_HEAD(&sc->sc_freesitds, sitd, free_list);
266 }
267
268 Static void ehci_abort_isoc_xfer(struct usbd_xfer *, usbd_status);
269
270 Static usbd_status ehci_device_setintr(ehci_softc_t *, ehci_soft_qh_t *,
271 int);
272
273 Static void ehci_add_qh(ehci_softc_t *, ehci_soft_qh_t *,
274 ehci_soft_qh_t *);
275 Static void ehci_rem_qh(ehci_softc_t *, ehci_soft_qh_t *,
276 ehci_soft_qh_t *);
277 Static void ehci_set_qh_qtd(ehci_soft_qh_t *, ehci_soft_qtd_t *);
278 Static void ehci_sync_hc(ehci_softc_t *);
279
280 Static void ehci_close_pipe(struct usbd_pipe *, ehci_soft_qh_t *);
281 Static void ehci_abort_xfer(struct usbd_xfer *, usbd_status);
282
283 #ifdef EHCI_DEBUG
284 Static ehci_softc_t *theehci;
285 void ehci_dump(void);
286 #endif
287
288 #ifdef EHCI_DEBUG
289 Static void ehci_dump_regs(ehci_softc_t *);
290 Static void ehci_dump_sqtds(ehci_soft_qtd_t *);
291 Static void ehci_dump_sqtd(ehci_soft_qtd_t *);
292 Static void ehci_dump_qtd(ehci_qtd_t *);
293 Static void ehci_dump_sqh(ehci_soft_qh_t *);
294 Static void ehci_dump_sitd(struct ehci_soft_itd *);
295 Static void ehci_dump_itds(ehci_soft_itd_t *);
296 Static void ehci_dump_itd(struct ehci_soft_itd *);
297 Static void ehci_dump_exfer(struct ehci_xfer *);
298 #endif
299
300 #define EHCI_NULL htole32(EHCI_LINK_TERMINATE)
301
302 static inline void
303 ehci_add_intr_list(ehci_softc_t *sc, struct ehci_xfer *ex)
304 {
305
306 TAILQ_INSERT_TAIL(&sc->sc_intrhead, ex, ex_next);
307 }
308
309 static inline void
310 ehci_del_intr_list(ehci_softc_t *sc, struct ehci_xfer *ex)
311 {
312
313 TAILQ_REMOVE(&sc->sc_intrhead, ex, ex_next);
314 }
315
316 Static const struct usbd_bus_methods ehci_bus_methods = {
317 .ubm_open = ehci_open,
318 .ubm_softint = ehci_softintr,
319 .ubm_dopoll = ehci_poll,
320 .ubm_allocx = ehci_allocx,
321 .ubm_freex = ehci_freex,
322 .ubm_getlock = ehci_get_lock,
323 .ubm_rhctrl = ehci_roothub_ctrl,
324 };
325
326 Static const struct usbd_pipe_methods ehci_root_intr_methods = {
327 .upm_transfer = ehci_root_intr_transfer,
328 .upm_start = ehci_root_intr_start,
329 .upm_abort = ehci_root_intr_abort,
330 .upm_close = ehci_root_intr_close,
331 .upm_cleartoggle = ehci_noop,
332 .upm_done = ehci_root_intr_done,
333 };
334
335 Static const struct usbd_pipe_methods ehci_device_ctrl_methods = {
336 .upm_init = ehci_device_ctrl_init,
337 .upm_fini = ehci_device_ctrl_fini,
338 .upm_transfer = ehci_device_ctrl_transfer,
339 .upm_start = ehci_device_ctrl_start,
340 .upm_abort = ehci_device_ctrl_abort,
341 .upm_close = ehci_device_ctrl_close,
342 .upm_cleartoggle = ehci_noop,
343 .upm_done = ehci_device_ctrl_done,
344 };
345
346 Static const struct usbd_pipe_methods ehci_device_intr_methods = {
347 .upm_init = ehci_device_intr_init,
348 .upm_fini = ehci_device_intr_fini,
349 .upm_transfer = ehci_device_intr_transfer,
350 .upm_start = ehci_device_intr_start,
351 .upm_abort = ehci_device_intr_abort,
352 .upm_close = ehci_device_intr_close,
353 .upm_cleartoggle = ehci_device_clear_toggle,
354 .upm_done = ehci_device_intr_done,
355 };
356
357 Static const struct usbd_pipe_methods ehci_device_bulk_methods = {
358 .upm_init = ehci_device_bulk_init,
359 .upm_fini = ehci_device_bulk_fini,
360 .upm_transfer = ehci_device_bulk_transfer,
361 .upm_start = ehci_device_bulk_start,
362 .upm_abort = ehci_device_bulk_abort,
363 .upm_close = ehci_device_bulk_close,
364 .upm_cleartoggle = ehci_device_clear_toggle,
365 .upm_done = ehci_device_bulk_done,
366 };
367
368 Static const struct usbd_pipe_methods ehci_device_isoc_methods = {
369 .upm_init = ehci_device_isoc_init,
370 .upm_fini = ehci_device_isoc_fini,
371 .upm_transfer = ehci_device_isoc_transfer,
372 .upm_abort = ehci_device_isoc_abort,
373 .upm_close = ehci_device_isoc_close,
374 .upm_cleartoggle = ehci_noop,
375 .upm_done = ehci_device_isoc_done,
376 };
377
378 Static const struct usbd_pipe_methods ehci_device_fs_isoc_methods = {
379 .upm_init = ehci_device_fs_isoc_init,
380 .upm_fini = ehci_device_fs_isoc_fini,
381 .upm_transfer = ehci_device_fs_isoc_transfer,
382 .upm_abort = ehci_device_fs_isoc_abort,
383 .upm_close = ehci_device_fs_isoc_close,
384 .upm_cleartoggle = ehci_noop,
385 .upm_done = ehci_device_fs_isoc_done,
386 };
387
388 static const uint8_t revbits[EHCI_MAX_POLLRATE] = {
389 0x00,0x40,0x20,0x60,0x10,0x50,0x30,0x70,0x08,0x48,0x28,0x68,0x18,0x58,0x38,0x78,
390 0x04,0x44,0x24,0x64,0x14,0x54,0x34,0x74,0x0c,0x4c,0x2c,0x6c,0x1c,0x5c,0x3c,0x7c,
391 0x02,0x42,0x22,0x62,0x12,0x52,0x32,0x72,0x0a,0x4a,0x2a,0x6a,0x1a,0x5a,0x3a,0x7a,
392 0x06,0x46,0x26,0x66,0x16,0x56,0x36,0x76,0x0e,0x4e,0x2e,0x6e,0x1e,0x5e,0x3e,0x7e,
393 0x01,0x41,0x21,0x61,0x11,0x51,0x31,0x71,0x09,0x49,0x29,0x69,0x19,0x59,0x39,0x79,
394 0x05,0x45,0x25,0x65,0x15,0x55,0x35,0x75,0x0d,0x4d,0x2d,0x6d,0x1d,0x5d,0x3d,0x7d,
395 0x03,0x43,0x23,0x63,0x13,0x53,0x33,0x73,0x0b,0x4b,0x2b,0x6b,0x1b,0x5b,0x3b,0x7b,
396 0x07,0x47,0x27,0x67,0x17,0x57,0x37,0x77,0x0f,0x4f,0x2f,0x6f,0x1f,0x5f,0x3f,0x7f,
397 };
398
399 int
400 ehci_init(ehci_softc_t *sc)
401 {
402 uint32_t vers, sparams, cparams, hcr;
403 u_int i;
404 usbd_status err;
405 ehci_soft_qh_t *sqh;
406 u_int ncomp;
407
408 EHCIHIST_FUNC(); EHCIHIST_CALLED();
409 #ifdef EHCI_DEBUG
410 theehci = sc;
411 #endif
412
413 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
414 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
415 cv_init(&sc->sc_doorbell, "ehcidb");
416
417 sc->sc_xferpool = pool_cache_init(sizeof(struct ehci_xfer), 0, 0, 0,
418 "ehcixfer", NULL, IPL_USB, NULL, NULL, NULL);
419
420 sc->sc_doorbell_si = softint_establish(SOFTINT_USB | SOFTINT_MPSAFE,
421 ehci_doorbell, sc);
422 KASSERT(sc->sc_doorbell_si != NULL);
423 sc->sc_pcd_si = softint_establish(SOFTINT_USB | SOFTINT_MPSAFE,
424 ehci_pcd, sc);
425 KASSERT(sc->sc_pcd_si != NULL);
426
427 sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
428
429 vers = EREAD2(sc, EHCI_HCIVERSION);
430 aprint_verbose("%s: EHCI version %x.%x\n", device_xname(sc->sc_dev),
431 vers >> 8, vers & 0xff);
432
433 sparams = EREAD4(sc, EHCI_HCSPARAMS);
434 DPRINTF("sparams=%#x", sparams, 0, 0, 0);
435 sc->sc_npcomp = EHCI_HCS_N_PCC(sparams);
436 ncomp = EHCI_HCS_N_CC(sparams);
437 if (ncomp != sc->sc_ncomp) {
438 aprint_verbose("%s: wrong number of companions (%d != %d)\n",
439 device_xname(sc->sc_dev), ncomp, sc->sc_ncomp);
440 #if NOHCI == 0 || NUHCI == 0
441 aprint_error("%s: ohci or uhci probably not configured\n",
442 device_xname(sc->sc_dev));
443 #endif
444 if (ncomp < sc->sc_ncomp)
445 sc->sc_ncomp = ncomp;
446 }
447 if (sc->sc_ncomp > 0) {
448 KASSERT(!(sc->sc_flags & EHCIF_ETTF));
449 aprint_normal("%s: %d companion controller%s, %d port%s%s",
450 device_xname(sc->sc_dev), sc->sc_ncomp,
451 sc->sc_ncomp!=1 ? "s" : "",
452 EHCI_HCS_N_PCC(sparams),
453 EHCI_HCS_N_PCC(sparams)!=1 ? "s" : "",
454 sc->sc_ncomp!=1 ? " each" : "");
455 if (sc->sc_comps[0]) {
456 aprint_normal(":");
457 for (i = 0; i < sc->sc_ncomp; i++)
458 aprint_normal(" %s",
459 device_xname(sc->sc_comps[i]));
460 }
461 aprint_normal("\n");
462 }
463 sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
464 sc->sc_hasppc = EHCI_HCS_PPC(sparams);
465
466 cparams = EREAD4(sc, EHCI_HCCPARAMS);
467 DPRINTF("cparams=%#x", cparams, 0, 0, 0);
468
469 if (EHCI_HCC_64BIT(cparams)) {
470 /* MUST clear segment register if 64 bit capable. */
471 EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
472 }
473
474 if (cparams & EHCI_HCC_IST_FULLFRAME) {
475 sc->sc_istthreshold = 0;
476 } else {
477 sc->sc_istthreshold = EHCI_HCC_GET_IST_THRESHOLD(cparams);
478 }
479
480 sc->sc_bus.ub_revision = USBREV_2_0;
481 sc->sc_bus.ub_usedma = true;
482 sc->sc_bus.ub_dmaflags = USBMALLOC_MULTISEG;
483
484 /* Reset the controller */
485 DPRINTF("resetting", 0, 0, 0, 0);
486 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
487 usb_delay_ms(&sc->sc_bus, 1);
488 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
489 for (i = 0; i < 100; i++) {
490 usb_delay_ms(&sc->sc_bus, 1);
491 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
492 if (!hcr)
493 break;
494 }
495 if (hcr) {
496 aprint_error("%s: reset timeout\n", device_xname(sc->sc_dev));
497 return EIO;
498 }
499 if (sc->sc_vendor_init)
500 sc->sc_vendor_init(sc);
501
502 /* XXX need proper intr scheduling */
503 sc->sc_rand = 96;
504
505 /* frame list size at default, read back what we got and use that */
506 switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) {
507 case 0: sc->sc_flsize = 1024; break;
508 case 1: sc->sc_flsize = 512; break;
509 case 2: sc->sc_flsize = 256; break;
510 case 3: return EIO;
511 }
512 err = usb_allocmem(&sc->sc_bus, sc->sc_flsize * sizeof(ehci_link_t),
513 EHCI_FLALIGN_ALIGN, &sc->sc_fldma);
514 if (err)
515 return err;
516 DPRINTF("flsize=%d", sc->sc_flsize, 0, 0, 0);
517 sc->sc_flist = KERNADDR(&sc->sc_fldma, 0);
518
519 for (i = 0; i < sc->sc_flsize; i++) {
520 sc->sc_flist[i] = EHCI_NULL;
521 }
522
523 EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
524
525 sc->sc_softitds = kmem_zalloc(sc->sc_flsize * sizeof(ehci_soft_itd_t *),
526 KM_SLEEP);
527 if (sc->sc_softitds == NULL)
528 return ENOMEM;
529 LIST_INIT(&sc->sc_freeitds);
530 LIST_INIT(&sc->sc_freesitds);
531 TAILQ_INIT(&sc->sc_intrhead);
532
533 /* Set up the bus struct. */
534 sc->sc_bus.ub_methods = &ehci_bus_methods;
535 sc->sc_bus.ub_pipesize= sizeof(struct ehci_pipe);
536
537 sc->sc_eintrs = EHCI_NORMAL_INTRS;
538
539 /*
540 * Allocate the interrupt dummy QHs. These are arranged to give poll
541 * intervals that are powers of 2 times 1ms.
542 */
543 for (i = 0; i < EHCI_INTRQHS; i++) {
544 sqh = ehci_alloc_sqh(sc);
545 if (sqh == NULL) {
546 err = ENOMEM;
547 goto bad1;
548 }
549 sc->sc_islots[i].sqh = sqh;
550 }
551 for (i = 0; i < EHCI_INTRQHS; i++) {
552 sqh = sc->sc_islots[i].sqh;
553 if (i == 0) {
554 /* The last (1ms) QH terminates. */
555 sqh->qh.qh_link = EHCI_NULL;
556 sqh->next = NULL;
557 } else {
558 /* Otherwise the next QH has half the poll interval */
559 sqh->next = sc->sc_islots[(i + 1) / 2 - 1].sqh;
560 sqh->qh.qh_link = htole32(sqh->next->physaddr |
561 EHCI_LINK_QH);
562 }
563 sqh->qh.qh_endp = htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
564 sqh->qh.qh_endphub = htole32(EHCI_QH_SET_MULT(1));
565 sqh->qh.qh_curqtd = EHCI_NULL;
566 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
567 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
568 sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
569 sqh->sqtd = NULL;
570 usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
571 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
572 }
573 /* Point the frame list at the last level (128ms). */
574 for (i = 0; i < sc->sc_flsize; i++) {
575 int j;
576
577 j = (i & ~(EHCI_MAX_POLLRATE-1)) |
578 revbits[i & (EHCI_MAX_POLLRATE-1)];
579 sc->sc_flist[j] = htole32(EHCI_LINK_QH |
580 sc->sc_islots[EHCI_IQHIDX(EHCI_IPOLLRATES - 1,
581 i)].sqh->physaddr);
582 }
583 usb_syncmem(&sc->sc_fldma, 0, sc->sc_flsize * sizeof(ehci_link_t),
584 BUS_DMASYNC_PREWRITE);
585
586 /* Allocate dummy QH that starts the async list. */
587 sqh = ehci_alloc_sqh(sc);
588 if (sqh == NULL) {
589 err = ENOMEM;
590 goto bad1;
591 }
592 /* Fill the QH */
593 sqh->qh.qh_endp =
594 htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
595 sqh->qh.qh_link =
596 htole32(sqh->physaddr | EHCI_LINK_QH);
597 sqh->qh.qh_curqtd = EHCI_NULL;
598 sqh->next = NULL;
599 /* Fill the overlay qTD */
600 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
601 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
602 sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
603 sqh->sqtd = NULL;
604 usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
605 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
606 #ifdef EHCI_DEBUG
607 DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
608 ehci_dump_sqh(sqh);
609 DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
610 #endif
611
612 /* Point to async list */
613 sc->sc_async_head = sqh;
614 EOWRITE4(sc, EHCI_ASYNCLISTADDR, sqh->physaddr | EHCI_LINK_QH);
615
616 callout_init(&sc->sc_tmo_intrlist, CALLOUT_MPSAFE);
617
618 /* Turn on controller */
619 EOWRITE4(sc, EHCI_USBCMD,
620 EHCI_CMD_ITC_2 | /* 2 microframes interrupt delay */
621 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
622 EHCI_CMD_ASE |
623 EHCI_CMD_PSE |
624 EHCI_CMD_RS);
625
626 /* Take over port ownership */
627 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
628
629 for (i = 0; i < 100; i++) {
630 usb_delay_ms(&sc->sc_bus, 1);
631 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
632 if (!hcr)
633 break;
634 }
635 if (hcr) {
636 aprint_error("%s: run timeout\n", device_xname(sc->sc_dev));
637 return EIO;
638 }
639
640 /* Enable interrupts */
641 DPRINTF("enabling interupts", 0, 0, 0, 0);
642 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
643
644 return 0;
645
646 #if 0
647 bad2:
648 ehci_free_sqh(sc, sc->sc_async_head);
649 #endif
650 bad1:
651 usb_freemem(&sc->sc_bus, &sc->sc_fldma);
652 return err;
653 }
654
655 int
656 ehci_intr(void *v)
657 {
658 ehci_softc_t *sc = v;
659 int ret = 0;
660
661 EHCIHIST_FUNC(); EHCIHIST_CALLED();
662
663 if (sc == NULL)
664 return 0;
665
666 mutex_spin_enter(&sc->sc_intr_lock);
667
668 if (sc->sc_dying || !device_has_power(sc->sc_dev))
669 goto done;
670
671 /* If we get an interrupt while polling, then just ignore it. */
672 if (sc->sc_bus.ub_usepolling) {
673 uint32_t intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
674
675 if (intrs)
676 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
677 DPRINTFN(16, "ignored interrupt while polling", 0, 0, 0, 0);
678 goto done;
679 }
680
681 ret = ehci_intr1(sc);
682
683 done:
684 mutex_spin_exit(&sc->sc_intr_lock);
685 return ret;
686 }
687
688 Static int
689 ehci_intr1(ehci_softc_t *sc)
690 {
691 uint32_t intrs, eintrs;
692
693 EHCIHIST_FUNC(); EHCIHIST_CALLED();
694
695 /* In case the interrupt occurs before initialization has completed. */
696 if (sc == NULL) {
697 #ifdef DIAGNOSTIC
698 printf("ehci_intr1: sc == NULL\n");
699 #endif
700 return 0;
701 }
702
703 KASSERT(mutex_owned(&sc->sc_intr_lock));
704
705 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
706 if (!intrs)
707 return 0;
708
709 eintrs = intrs & sc->sc_eintrs;
710 DPRINTF("sc=%p intrs=%#x(%#x) eintrs=%#x", sc, intrs,
711 EOREAD4(sc, EHCI_USBSTS), eintrs);
712 if (!eintrs)
713 return 0;
714
715 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
716 if (eintrs & EHCI_STS_IAA) {
717 DPRINTF("door bell", 0, 0, 0, 0);
718 kpreempt_disable();
719 KASSERT(sc->sc_doorbell_si != NULL);
720 softint_schedule(sc->sc_doorbell_si);
721 kpreempt_enable();
722 eintrs &= ~EHCI_STS_IAA;
723 }
724 if (eintrs & (EHCI_STS_INT | EHCI_STS_ERRINT)) {
725 DPRINTF("INT=%d ERRINT=%d",
726 eintrs & EHCI_STS_INT ? 1 : 0,
727 eintrs & EHCI_STS_ERRINT ? 1 : 0, 0, 0);
728 usb_schedsoftintr(&sc->sc_bus);
729 eintrs &= ~(EHCI_STS_INT | EHCI_STS_ERRINT);
730 }
731 if (eintrs & EHCI_STS_HSE) {
732 printf("%s: unrecoverable error, controller halted\n",
733 device_xname(sc->sc_dev));
734 /* XXX what else */
735 }
736 if (eintrs & EHCI_STS_PCD) {
737 kpreempt_disable();
738 KASSERT(sc->sc_pcd_si != NULL);
739 softint_schedule(sc->sc_pcd_si);
740 kpreempt_enable();
741 eintrs &= ~EHCI_STS_PCD;
742 }
743
744 if (eintrs != 0) {
745 /* Block unprocessed interrupts. */
746 sc->sc_eintrs &= ~eintrs;
747 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
748 printf("%s: blocking intrs 0x%x\n",
749 device_xname(sc->sc_dev), eintrs);
750 }
751
752 return 1;
753 }
754
755 Static void
756 ehci_doorbell(void *addr)
757 {
758 ehci_softc_t *sc = addr;
759 EHCIHIST_FUNC(); EHCIHIST_CALLED();
760
761 mutex_enter(&sc->sc_lock);
762 sc->sc_dbanswered = true;
763 cv_broadcast(&sc->sc_doorbell);
764 mutex_exit(&sc->sc_lock);
765 }
766
767 Static void
768 ehci_pcd(void *addr)
769 {
770 ehci_softc_t *sc = addr;
771 struct usbd_xfer *xfer;
772 u_char *p;
773 int i, m;
774
775 EHCIHIST_FUNC(); EHCIHIST_CALLED();
776
777 mutex_enter(&sc->sc_lock);
778 xfer = sc->sc_intrxfer;
779
780 if (xfer == NULL) {
781 /* Just ignore the change. */
782 goto done;
783 }
784
785 p = xfer->ux_buf;
786 m = min(sc->sc_noport, xfer->ux_length * 8 - 1);
787 memset(p, 0, xfer->ux_length);
788 for (i = 1; i <= m; i++) {
789 /* Pick out CHANGE bits from the status reg. */
790 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR)
791 p[i/8] |= 1 << (i%8);
792 if (i % 8 == 7)
793 DPRINTF("change(%d)=0x%02x", i / 8, p[i/8], 0, 0);
794 }
795 xfer->ux_actlen = xfer->ux_length;
796 xfer->ux_status = USBD_NORMAL_COMPLETION;
797
798 usb_transfer_complete(xfer);
799
800 done:
801 mutex_exit(&sc->sc_lock);
802 }
803
804 Static void
805 ehci_softintr(void *v)
806 {
807 struct usbd_bus *bus = v;
808 ehci_softc_t *sc = EHCI_BUS2SC(bus);
809 struct ehci_xfer *ex, *nextex;
810
811 KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
812
813 EHCIHIST_FUNC(); EHCIHIST_CALLED();
814
815 ex_completeq_t cq;
816 TAILQ_INIT(&cq);
817
818 /*
819 * The only explanation I can think of for why EHCI is as brain dead
820 * as UHCI interrupt-wise is that Intel was involved in both.
821 * An interrupt just tells us that something is done, we have no
822 * clue what, so we need to scan through all active transfers. :-(
823 */
824
825 /*
826 * ehci_idone will remove transfer from sc->sc_intrhead if it's
827 * complete and add to our cq list
828 *
829 */
830 TAILQ_FOREACH_SAFE(ex, &sc->sc_intrhead, ex_next, nextex) {
831 switch (ex->ex_type) {
832 case EX_CTRL:
833 case EX_BULK:
834 case EX_INTR:
835 ehci_check_qh_intr(sc, ex, &cq);
836 break;
837 case EX_ISOC:
838 ehci_check_itd_intr(sc, ex, &cq);
839 break;
840 case EX_FS_ISOC:
841 ehci_check_sitd_intr(sc, ex, &cq);
842 break;
843 default:
844 KASSERT(false);
845 }
846
847 }
848
849 /*
850 * We abuse ex_next for the interrupt and complete lists and
851 * interrupt transfers will get re-added here so use
852 * the _SAFE version of TAILQ_FOREACH.
853 */
854 TAILQ_FOREACH_SAFE(ex, &cq, ex_next, nextex) {
855 usb_transfer_complete(&ex->ex_xfer);
856 }
857
858 /* Schedule a callout to catch any dropped transactions. */
859 if ((sc->sc_flags & EHCIF_DROPPED_INTR_WORKAROUND) &&
860 !TAILQ_EMPTY(&sc->sc_intrhead))
861 callout_reset(&sc->sc_tmo_intrlist,
862 hz, ehci_intrlist_timeout, sc);
863 }
864
865 Static void
866 ehci_check_qh_intr(ehci_softc_t *sc, struct ehci_xfer *ex, ex_completeq_t *cq)
867 {
868 ehci_soft_qtd_t *sqtd, *fsqtd, *lsqtd;
869 uint32_t status;
870
871 EHCIHIST_FUNC(); EHCIHIST_CALLED();
872
873 KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
874
875 if (ex->ex_type == EX_CTRL) {
876 fsqtd = ex->ex_setup;
877 lsqtd = ex->ex_status;
878 } else {
879 fsqtd = ex->ex_sqtdstart;
880 lsqtd = ex->ex_sqtdend;
881 }
882 KASSERTMSG(fsqtd != NULL && lsqtd != NULL,
883 "xfer %p xt %d fsqtd %p lsqtd %p", ex, ex->ex_type, fsqtd, lsqtd);
884
885 /*
886 * If the last TD is still active we need to check whether there
887 * is an error somewhere in the middle, or whether there was a
888 * short packet (SPD and not ACTIVE).
889 */
890 usb_syncmem(&lsqtd->dma,
891 lsqtd->offs + offsetof(ehci_qtd_t, qtd_status),
892 sizeof(lsqtd->qtd.qtd_status),
893 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
894 status = le32toh(lsqtd->qtd.qtd_status);
895 usb_syncmem(&lsqtd->dma,
896 lsqtd->offs + offsetof(ehci_qtd_t, qtd_status),
897 sizeof(lsqtd->qtd.qtd_status), BUS_DMASYNC_PREREAD);
898 if (status & EHCI_QTD_ACTIVE) {
899 DPRINTFN(10, "active ex=%p", ex, 0, 0, 0);
900
901 /* last qTD has already been checked */
902 for (sqtd = fsqtd; sqtd != lsqtd; sqtd = sqtd->nextqtd) {
903 usb_syncmem(&sqtd->dma,
904 sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
905 sizeof(sqtd->qtd.qtd_status),
906 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
907 status = le32toh(sqtd->qtd.qtd_status);
908 usb_syncmem(&sqtd->dma,
909 sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
910 sizeof(sqtd->qtd.qtd_status), BUS_DMASYNC_PREREAD);
911 /* If there's an active QTD the xfer isn't done. */
912 if (status & EHCI_QTD_ACTIVE)
913 break;
914 /* Any kind of error makes the xfer done. */
915 if (status & EHCI_QTD_HALTED)
916 goto done;
917 /* Handle short packets */
918 if (EHCI_QTD_GET_BYTES(status) != 0) {
919 /*
920 * If we get here for a control transfer then
921 * we need to let the hardware complete the
922 * status phase. That is, we're not done
923 * quite yet.
924 *
925 * Otherwise, we're done.
926 */
927 if (ex->ex_type == EX_CTRL) {
928 break;
929 }
930 goto done;
931 }
932 }
933 DPRINTFN(10, "ex=%p std=%p still active", ex, ex->ex_sqtdstart,
934 0, 0);
935 #ifdef EHCI_DEBUG
936 DPRINTFN(5, "--- still active start ---", 0, 0, 0, 0);
937 ehci_dump_sqtds(ex->ex_sqtdstart);
938 DPRINTFN(5, "--- still active end ---", 0, 0, 0, 0);
939 #endif
940 return;
941 }
942 done:
943 DPRINTFN(10, "ex=%p done", ex, 0, 0, 0);
944 ehci_idone(ex, cq);
945 }
946
947 Static void
948 ehci_check_itd_intr(ehci_softc_t *sc, struct ehci_xfer *ex, ex_completeq_t *cq)
949 {
950 ehci_soft_itd_t *itd;
951 int i;
952
953 EHCIHIST_FUNC(); EHCIHIST_CALLED();
954
955 KASSERT(mutex_owned(&sc->sc_lock));
956
957 if (&ex->ex_xfer != SIMPLEQ_FIRST(&ex->ex_xfer.ux_pipe->up_queue))
958 return;
959
960 KASSERTMSG(ex->ex_itdstart != NULL && ex->ex_itdend != NULL,
961 "xfer %p fitd %p litd %p", ex, ex->ex_itdstart, ex->ex_itdend);
962
963 itd = ex->ex_itdend;
964
965 /*
966 * check no active transfers in last itd, meaning we're finished
967 */
968
969 usb_syncmem(&itd->dma, itd->offs + offsetof(ehci_itd_t, itd_ctl),
970 sizeof(itd->itd.itd_ctl),
971 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
972
973 for (i = 0; i < EHCI_ITD_NUFRAMES; i++) {
974 if (le32toh(itd->itd.itd_ctl[i]) & EHCI_ITD_ACTIVE)
975 break;
976 }
977
978 if (i == EHCI_ITD_NUFRAMES) {
979 goto done; /* All 8 descriptors inactive, it's done */
980 }
981
982 usb_syncmem(&itd->dma, itd->offs + offsetof(ehci_itd_t, itd_ctl),
983 sizeof(itd->itd.itd_ctl), BUS_DMASYNC_PREREAD);
984
985 DPRINTFN(10, "ex %p itd %p still active", ex, ex->ex_itdstart, 0, 0);
986 return;
987 done:
988 DPRINTF("ex %p done", ex, 0, 0, 0);
989 ehci_idone(ex, cq);
990 }
991
992 void
993 ehci_check_sitd_intr(ehci_softc_t *sc, struct ehci_xfer *ex, ex_completeq_t *cq)
994 {
995 ehci_soft_sitd_t *sitd;
996
997 EHCIHIST_FUNC(); EHCIHIST_CALLED();
998
999 KASSERT(mutex_owned(&sc->sc_lock));
1000
1001 if (&ex->ex_xfer != SIMPLEQ_FIRST(&ex->ex_xfer.ux_pipe->up_queue))
1002 return;
1003
1004 KASSERTMSG(ex->ex_sitdstart != NULL && ex->ex_sitdend != NULL,
1005 "xfer %p fsitd %p lsitd %p", ex, ex->ex_sitdstart, ex->ex_sitdend);
1006
1007 sitd = ex->ex_sitdend;
1008
1009 /*
1010 * check no active transfers in last sitd, meaning we're finished
1011 */
1012
1013 usb_syncmem(&sitd->dma, sitd->offs + offsetof(ehci_sitd_t, sitd_trans),
1014 sizeof(sitd->sitd.sitd_trans),
1015 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1016
1017 bool active = ((le32toh(sitd->sitd.sitd_trans) & EHCI_SITD_ACTIVE) != 0);
1018
1019 usb_syncmem(&sitd->dma, sitd->offs + offsetof(ehci_sitd_t, sitd_trans),
1020 sizeof(sitd->sitd.sitd_trans), BUS_DMASYNC_PREREAD);
1021
1022 if (active)
1023 return;
1024
1025 DPRINTFN(10, "ex=%p done", ex, 0, 0, 0);
1026 ehci_idone(ex, cq);
1027 }
1028
1029
1030 Static void
1031 ehci_idone(struct ehci_xfer *ex, ex_completeq_t *cq)
1032 {
1033 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1034 struct usbd_xfer *xfer = &ex->ex_xfer;
1035 struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
1036 struct ehci_softc *sc = EHCI_XFER2SC(xfer);
1037 ehci_soft_qtd_t *sqtd, *fsqtd, *lsqtd;
1038 uint32_t status = 0, nstatus = 0;
1039 int actlen = 0;
1040 bool polling = sc->sc_bus.ub_usepolling;
1041
1042 KASSERT(polling || mutex_owned(&sc->sc_lock));
1043
1044 DPRINTF("ex=%p", ex, 0, 0, 0);
1045
1046 /*
1047 * Make sure the timeout handler didn't run or ran to the end
1048 * and set the transfer status.
1049 */
1050 callout_halt(&ex->ex_xfer.ux_callout, polling ? NULL : &sc->sc_lock);
1051 if (xfer->ux_status == USBD_CANCELLED ||
1052 xfer->ux_status == USBD_TIMEOUT) {
1053 DPRINTF("aborted xfer=%p", xfer, 0, 0, 0);
1054 return;
1055 }
1056
1057 #ifdef DIAGNOSTIC
1058 #ifdef EHCI_DEBUG
1059 if (ex->ex_isdone) {
1060 DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
1061 ehci_dump_exfer(ex);
1062 DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
1063 }
1064 #endif
1065 KASSERTMSG(!ex->ex_isdone, "xfer %p type %d status %d", xfer,
1066 ex->ex_type, xfer->ux_status);
1067 ex->ex_isdone = true;
1068 #endif
1069
1070 DPRINTF("xfer=%p, pipe=%p ready", xfer, epipe, 0, 0);
1071
1072 /* The transfer is done, compute actual length and status. */
1073 if (ex->ex_type == EX_ISOC) {
1074 /* HS isoc transfer */
1075
1076 struct ehci_soft_itd *itd;
1077 int i, nframes, len, uframes;
1078
1079 nframes = 0;
1080
1081 #ifdef EHCI_DEBUG
1082 DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
1083 ehci_dump_itds(ex->ex_itdstart);
1084 DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
1085 #endif
1086
1087 i = xfer->ux_pipe->up_endpoint->ue_edesc->bInterval;
1088 uframes = min(1 << (i - 1), USB_UFRAMES_PER_FRAME);
1089
1090 for (itd = ex->ex_itdstart; itd != NULL; itd = itd->xfer_next) {
1091 usb_syncmem(&itd->dma,
1092 itd->offs + offsetof(ehci_itd_t,itd_ctl),
1093 sizeof(itd->itd.itd_ctl),
1094 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1095
1096 for (i = 0; i < EHCI_ITD_NUFRAMES; i += uframes) {
1097 /*
1098 * XXX - driver didn't fill in the frame full
1099 * of uframes. This leads to scheduling
1100 * inefficiencies, but working around
1101 * this doubles complexity of tracking
1102 * an xfer.
1103 */
1104 if (nframes >= xfer->ux_nframes)
1105 break;
1106
1107 status = le32toh(itd->itd.itd_ctl[i]);
1108 len = EHCI_ITD_GET_LEN(status);
1109 if (EHCI_ITD_GET_STATUS(status) != 0)
1110 len = 0; /*No valid data on error*/
1111
1112 xfer->ux_frlengths[nframes++] = len;
1113 actlen += len;
1114 }
1115 usb_syncmem(&itd->dma,
1116 itd->offs + offsetof(ehci_itd_t,itd_ctl),
1117 sizeof(itd->itd.itd_ctl), BUS_DMASYNC_PREREAD);
1118
1119 if (nframes >= xfer->ux_nframes)
1120 break;
1121 }
1122
1123 xfer->ux_actlen = actlen;
1124 xfer->ux_status = USBD_NORMAL_COMPLETION;
1125 goto end;
1126 } else if (ex->ex_type == EX_FS_ISOC) {
1127 /* FS isoc transfer */
1128 struct ehci_soft_sitd *sitd;
1129 int nframes, len;
1130
1131 nframes = 0;
1132
1133 for (sitd = ex->ex_sitdstart; sitd != NULL;
1134 sitd = sitd->xfer_next) {
1135 usb_syncmem(&sitd->dma,
1136 sitd->offs + offsetof(ehci_sitd_t, sitd_trans),
1137 sizeof(sitd->sitd.sitd_trans),
1138 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1139
1140 /*
1141 * XXX - driver didn't fill in the frame full
1142 * of uframes. This leads to scheduling
1143 * inefficiencies, but working around
1144 * this doubles complexity of tracking
1145 * an xfer.
1146 */
1147 if (nframes >= xfer->ux_nframes)
1148 break;
1149
1150 status = le32toh(sitd->sitd.sitd_trans);
1151 usb_syncmem(&sitd->dma,
1152 sitd->offs + offsetof(ehci_sitd_t, sitd_trans),
1153 sizeof(sitd->sitd.sitd_trans), BUS_DMASYNC_PREREAD);
1154
1155 len = EHCI_SITD_GET_LEN(status);
1156 if (status & (EHCI_SITD_ERR|EHCI_SITD_BUFERR|
1157 EHCI_SITD_BABBLE|EHCI_SITD_XACTERR|EHCI_SITD_MISS)) {
1158 /* No valid data on error */
1159 len = xfer->ux_frlengths[nframes];
1160 }
1161
1162 /*
1163 * frlengths[i]: # of bytes to send
1164 * len: # of bytes host didn't send
1165 */
1166 xfer->ux_frlengths[nframes] -= len;
1167 /* frlengths[i]: # of bytes host sent */
1168 actlen += xfer->ux_frlengths[nframes++];
1169
1170 if (nframes >= xfer->ux_nframes)
1171 break;
1172 }
1173
1174 xfer->ux_actlen = actlen;
1175 xfer->ux_status = USBD_NORMAL_COMPLETION;
1176 goto end;
1177 }
1178 KASSERT(ex->ex_type == EX_CTRL || ex->ex_type == EX_INTR ||
1179 ex->ex_type == EX_BULK);
1180
1181 /* Continue processing xfers using queue heads */
1182 if (ex->ex_type == EX_CTRL) {
1183 fsqtd = ex->ex_setup;
1184 lsqtd = ex->ex_status;
1185 } else {
1186 fsqtd = ex->ex_sqtdstart;
1187 lsqtd = ex->ex_sqtdend;
1188 }
1189 #ifdef EHCI_DEBUG
1190 DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
1191 ehci_dump_sqtds(fsqtd);
1192 DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
1193 #endif
1194
1195 for (sqtd = fsqtd; sqtd != lsqtd->nextqtd; sqtd = sqtd->nextqtd) {
1196 usb_syncmem(&sqtd->dma, sqtd->offs, sizeof(sqtd->qtd),
1197 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1198 nstatus = le32toh(sqtd->qtd.qtd_status);
1199 usb_syncmem(&sqtd->dma, sqtd->offs, sizeof(sqtd->qtd),
1200 BUS_DMASYNC_PREREAD);
1201 if (nstatus & EHCI_QTD_ACTIVE)
1202 break;
1203
1204 status = nstatus;
1205 if (EHCI_QTD_GET_PID(status) != EHCI_QTD_PID_SETUP)
1206 actlen += sqtd->len - EHCI_QTD_GET_BYTES(status);
1207 }
1208
1209 /*
1210 * If there are left over TDs we need to update the toggle.
1211 * The default pipe doesn't need it since control transfers
1212 * start the toggle at 0 every time.
1213 * For a short transfer we need to update the toggle for the missing
1214 * packets within the qTD.
1215 */
1216 if ((sqtd != lsqtd->nextqtd || EHCI_QTD_GET_BYTES(status)) &&
1217 xfer->ux_pipe->up_dev->ud_pipe0 != xfer->ux_pipe) {
1218 DPRINTF("toggle update status=0x%08x nstatus=0x%08x",
1219 status, nstatus, 0, 0);
1220 #if 0
1221 ehci_dump_sqh(epipe->sqh);
1222 ehci_dump_sqtds(ex->ex_sqtdstart);
1223 #endif
1224 epipe->nexttoggle = EHCI_QTD_GET_TOGGLE(nstatus);
1225 }
1226
1227 DPRINTF("len=%d actlen=%d status=0x%08x", xfer->ux_length, actlen,
1228 status, 0);
1229 xfer->ux_actlen = actlen;
1230 if (status & EHCI_QTD_HALTED) {
1231 #ifdef EHCI_DEBUG
1232 DPRINTF("halted addr=%d endpt=0x%02x",
1233 xfer->ux_pipe->up_dev->ud_addr,
1234 xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress,
1235 0, 0);
1236 DPRINTF("cerr=%d pid=%d",
1237 EHCI_QTD_GET_CERR(status), EHCI_QTD_GET_PID(status),
1238 0, 0);
1239 DPRINTF("active =%d halted=%d buferr=%d babble=%d",
1240 status & EHCI_QTD_ACTIVE ? 1 : 0,
1241 status & EHCI_QTD_HALTED ? 1 : 0,
1242 status & EHCI_QTD_BUFERR ? 1 : 0,
1243 status & EHCI_QTD_BABBLE ? 1 : 0);
1244
1245 DPRINTF("xacterr=%d missed=%d split =%d ping =%d",
1246 status & EHCI_QTD_XACTERR ? 1 : 0,
1247 status & EHCI_QTD_MISSEDMICRO ? 1 : 0,
1248 status & EHCI_QTD_SPLITXSTATE ? 1 : 0,
1249 status & EHCI_QTD_PINGSTATE ? 1 : 0);
1250
1251 DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
1252 ehci_dump_sqh(epipe->sqh);
1253 ehci_dump_sqtds(ex->ex_sqtdstart);
1254 DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
1255 #endif
1256 /* low&full speed has an extra error flag */
1257 if (EHCI_QH_GET_EPS(epipe->sqh->qh.qh_endp) !=
1258 EHCI_QH_SPEED_HIGH)
1259 status &= EHCI_QTD_STATERRS | EHCI_QTD_PINGSTATE;
1260 else
1261 status &= EHCI_QTD_STATERRS;
1262 if (status == 0) /* no other errors means a stall */ {
1263 xfer->ux_status = USBD_STALLED;
1264 } else {
1265 xfer->ux_status = USBD_IOERROR; /* more info XXX */
1266 }
1267 /* XXX need to reset TT on missed microframe */
1268 if (status & EHCI_QTD_MISSEDMICRO) {
1269 printf("%s: missed microframe, TT reset not "
1270 "implemented, hub might be inoperational\n",
1271 device_xname(sc->sc_dev));
1272 }
1273 } else {
1274 xfer->ux_status = USBD_NORMAL_COMPLETION;
1275 }
1276
1277 end:
1278
1279 ehci_del_intr_list(sc, ex);
1280 TAILQ_INSERT_TAIL(cq, ex, ex_next);
1281
1282 DPRINTF("ex=%p done", ex, 0, 0, 0);
1283 }
1284
1285 Static void
1286 ehci_poll(struct usbd_bus *bus)
1287 {
1288 ehci_softc_t *sc = EHCI_BUS2SC(bus);
1289
1290 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1291
1292 #ifdef EHCI_DEBUG
1293 static int last;
1294 int new;
1295 new = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
1296 if (new != last) {
1297 DPRINTF("intrs=0x%04x", new, 0, 0, 0);
1298 last = new;
1299 }
1300 #endif
1301
1302 if (EOREAD4(sc, EHCI_USBSTS) & sc->sc_eintrs) {
1303 mutex_spin_enter(&sc->sc_intr_lock);
1304 ehci_intr1(sc);
1305 mutex_spin_exit(&sc->sc_intr_lock);
1306 }
1307 }
1308
1309 void
1310 ehci_childdet(device_t self, device_t child)
1311 {
1312 struct ehci_softc *sc = device_private(self);
1313
1314 KASSERT(sc->sc_child == child);
1315 sc->sc_child = NULL;
1316 }
1317
1318 int
1319 ehci_detach(struct ehci_softc *sc, int flags)
1320 {
1321 int rv = 0;
1322
1323 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1324
1325 if (sc->sc_child != NULL)
1326 rv = config_detach(sc->sc_child, flags);
1327
1328 if (rv != 0)
1329 return rv;
1330
1331 callout_halt(&sc->sc_tmo_intrlist, NULL);
1332 callout_destroy(&sc->sc_tmo_intrlist);
1333
1334 /* XXX free other data structures XXX */
1335 if (sc->sc_softitds)
1336 kmem_free(sc->sc_softitds,
1337 sc->sc_flsize * sizeof(ehci_soft_itd_t *));
1338 cv_destroy(&sc->sc_doorbell);
1339
1340 #if 0
1341 /* XXX destroyed in ehci_pci.c as it controls ehci_intr access */
1342
1343 softint_disestablish(sc->sc_doorbell_si);
1344 softint_disestablish(sc->sc_pcd_si);
1345
1346 mutex_destroy(&sc->sc_lock);
1347 mutex_destroy(&sc->sc_intr_lock);
1348 #endif
1349
1350 pool_cache_destroy(sc->sc_xferpool);
1351
1352 EOWRITE4(sc, EHCI_CONFIGFLAG, 0);
1353
1354 return rv;
1355 }
1356
1357
1358 int
1359 ehci_activate(device_t self, enum devact act)
1360 {
1361 struct ehci_softc *sc = device_private(self);
1362
1363 switch (act) {
1364 case DVACT_DEACTIVATE:
1365 sc->sc_dying = 1;
1366 return 0;
1367 default:
1368 return EOPNOTSUPP;
1369 }
1370 }
1371
1372 /*
1373 * Handle suspend/resume.
1374 *
1375 * We need to switch to polling mode here, because this routine is
1376 * called from an interrupt context. This is all right since we
1377 * are almost suspended anyway.
1378 *
1379 * Note that this power handler isn't to be registered directly; the
1380 * bus glue needs to call out to it.
1381 */
1382 bool
1383 ehci_suspend(device_t dv, const pmf_qual_t *qual)
1384 {
1385 ehci_softc_t *sc = device_private(dv);
1386 int i;
1387 uint32_t cmd, hcr;
1388
1389 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1390
1391 mutex_spin_enter(&sc->sc_intr_lock);
1392 sc->sc_bus.ub_usepolling++;
1393 mutex_spin_exit(&sc->sc_intr_lock);
1394
1395 for (i = 1; i <= sc->sc_noport; i++) {
1396 cmd = EOREAD4(sc, EHCI_PORTSC(i)) & ~EHCI_PS_CLEAR;
1397 if ((cmd & EHCI_PS_PO) == 0 && (cmd & EHCI_PS_PE) == EHCI_PS_PE)
1398 EOWRITE4(sc, EHCI_PORTSC(i), cmd | EHCI_PS_SUSP);
1399 }
1400
1401 sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD);
1402
1403 cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
1404 EOWRITE4(sc, EHCI_USBCMD, cmd);
1405
1406 for (i = 0; i < 100; i++) {
1407 hcr = EOREAD4(sc, EHCI_USBSTS) & (EHCI_STS_ASS | EHCI_STS_PSS);
1408 if (hcr == 0)
1409 break;
1410
1411 usb_delay_ms(&sc->sc_bus, 1);
1412 }
1413 if (hcr != 0)
1414 printf("%s: reset timeout\n", device_xname(dv));
1415
1416 cmd &= ~EHCI_CMD_RS;
1417 EOWRITE4(sc, EHCI_USBCMD, cmd);
1418
1419 for (i = 0; i < 100; i++) {
1420 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
1421 if (hcr == EHCI_STS_HCH)
1422 break;
1423
1424 usb_delay_ms(&sc->sc_bus, 1);
1425 }
1426 if (hcr != EHCI_STS_HCH)
1427 printf("%s: config timeout\n", device_xname(dv));
1428
1429 mutex_spin_enter(&sc->sc_intr_lock);
1430 sc->sc_bus.ub_usepolling--;
1431 mutex_spin_exit(&sc->sc_intr_lock);
1432
1433 return true;
1434 }
1435
1436 bool
1437 ehci_resume(device_t dv, const pmf_qual_t *qual)
1438 {
1439 ehci_softc_t *sc = device_private(dv);
1440 int i;
1441 uint32_t cmd, hcr;
1442
1443 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1444
1445 /* restore things in case the bios sucks */
1446 EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
1447 EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
1448 EOWRITE4(sc, EHCI_ASYNCLISTADDR,
1449 sc->sc_async_head->physaddr | EHCI_LINK_QH);
1450
1451 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs & ~EHCI_INTR_PCIE);
1452
1453 EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
1454
1455 hcr = 0;
1456 for (i = 1; i <= sc->sc_noport; i++) {
1457 cmd = EOREAD4(sc, EHCI_PORTSC(i)) & ~EHCI_PS_CLEAR;
1458 if ((cmd & EHCI_PS_PO) == 0 &&
1459 (cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP) {
1460 EOWRITE4(sc, EHCI_PORTSC(i), cmd | EHCI_PS_FPR);
1461 hcr = 1;
1462 }
1463 }
1464
1465 if (hcr) {
1466 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
1467
1468 for (i = 1; i <= sc->sc_noport; i++) {
1469 cmd = EOREAD4(sc, EHCI_PORTSC(i)) & ~EHCI_PS_CLEAR;
1470 if ((cmd & EHCI_PS_PO) == 0 &&
1471 (cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)
1472 EOWRITE4(sc, EHCI_PORTSC(i),
1473 cmd & ~EHCI_PS_FPR);
1474 }
1475 }
1476
1477 EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
1478 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1479
1480 for (i = 0; i < 100; i++) {
1481 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
1482 if (hcr != EHCI_STS_HCH)
1483 break;
1484
1485 usb_delay_ms(&sc->sc_bus, 1);
1486 }
1487 if (hcr == EHCI_STS_HCH)
1488 printf("%s: config timeout\n", device_xname(dv));
1489
1490 return true;
1491 }
1492
1493 /*
1494 * Shut down the controller when the system is going down.
1495 */
1496 bool
1497 ehci_shutdown(device_t self, int flags)
1498 {
1499 ehci_softc_t *sc = device_private(self);
1500
1501 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1502
1503 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
1504 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
1505 return true;
1506 }
1507
1508 Static struct usbd_xfer *
1509 ehci_allocx(struct usbd_bus *bus, unsigned int nframes)
1510 {
1511 struct ehci_softc *sc = EHCI_BUS2SC(bus);
1512 struct usbd_xfer *xfer;
1513
1514 xfer = pool_cache_get(sc->sc_xferpool, PR_NOWAIT);
1515 if (xfer != NULL) {
1516 memset(xfer, 0, sizeof(struct ehci_xfer));
1517 #ifdef DIAGNOSTIC
1518 struct ehci_xfer *ex = EHCI_XFER2EXFER(xfer);
1519 ex->ex_isdone = true;
1520 xfer->ux_state = XFER_BUSY;
1521 #endif
1522 }
1523 return xfer;
1524 }
1525
1526 Static void
1527 ehci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
1528 {
1529 struct ehci_softc *sc = EHCI_BUS2SC(bus);
1530 struct ehci_xfer *ex __diagused = EHCI_XFER2EXFER(xfer);
1531
1532 KASSERTMSG(xfer->ux_state == XFER_BUSY, "xfer %p state %d\n", xfer,
1533 xfer->ux_state);
1534 KASSERT(ex->ex_isdone);
1535
1536 #ifdef DIAGNOSTIC
1537 xfer->ux_state = XFER_FREE;
1538 #endif
1539
1540 pool_cache_put(sc->sc_xferpool, xfer);
1541 }
1542
1543 Static void
1544 ehci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
1545 {
1546 struct ehci_softc *sc = EHCI_BUS2SC(bus);
1547
1548 *lock = &sc->sc_lock;
1549 }
1550
1551 Static void
1552 ehci_device_clear_toggle(struct usbd_pipe *pipe)
1553 {
1554 struct ehci_pipe *epipe = EHCI_PIPE2EPIPE(pipe);
1555
1556 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1557
1558 DPRINTF("epipe=%p status=0x%08x", epipe,
1559 epipe->sqh->qh.qh_qtd.qtd_status, 0, 0);
1560 #ifdef EHCI_DEBUG
1561 if (ehcidebug)
1562 usbd_dump_pipe(pipe);
1563 #endif
1564 epipe->nexttoggle = 0;
1565 }
1566
1567 Static void
1568 ehci_noop(struct usbd_pipe *pipe)
1569 {
1570 }
1571
1572 #ifdef EHCI_DEBUG
1573 /*
1574 * Unused function - this is meant to be called from a kernel
1575 * debugger.
1576 */
1577 void
1578 ehci_dump(void)
1579 {
1580 ehci_softc_t *sc = theehci;
1581 int i;
1582 printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n",
1583 EOREAD4(sc, EHCI_USBCMD),
1584 EOREAD4(sc, EHCI_USBSTS),
1585 EOREAD4(sc, EHCI_USBINTR));
1586 printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
1587 EOREAD4(sc, EHCI_FRINDEX),
1588 EOREAD4(sc, EHCI_CTRLDSSEGMENT),
1589 EOREAD4(sc, EHCI_PERIODICLISTBASE),
1590 EOREAD4(sc, EHCI_ASYNCLISTADDR));
1591 for (i = 1; i <= sc->sc_noport; i++)
1592 printf("port %d status=0x%08x\n", i,
1593 EOREAD4(sc, EHCI_PORTSC(i)));
1594 }
1595
1596 Static void
1597 ehci_dump_regs(ehci_softc_t *sc)
1598 {
1599 int i;
1600
1601 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1602
1603 DPRINTF("cmd = 0x%08x sts = 0x%08x ien = 0x%08x",
1604 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS),
1605 EOREAD4(sc, EHCI_USBINTR), 0);
1606 DPRINTF("frindex = 0x%08x ctrdsegm = 0x%08x periodic = 0x%08x "
1607 "async = 0x%08x",
1608 EOREAD4(sc, EHCI_FRINDEX), EOREAD4(sc, EHCI_CTRLDSSEGMENT),
1609 EOREAD4(sc, EHCI_PERIODICLISTBASE),
1610 EOREAD4(sc, EHCI_ASYNCLISTADDR));
1611 for (i = 1; i <= sc->sc_noport; i += 2) {
1612 if (i == sc->sc_noport) {
1613 DPRINTF("port %d status = 0x%08x", i,
1614 EOREAD4(sc, EHCI_PORTSC(i)), 0, 0);
1615 } else {
1616 DPRINTF(
1617 "port %d status = 0x%08x port %d status = 0x%08x",
1618 i, EOREAD4(sc, EHCI_PORTSC(i)),
1619 i+1, EOREAD4(sc, EHCI_PORTSC(i+1)));
1620 }
1621 }
1622 }
1623
1624 #define ehci_dump_link(link, type) do { \
1625 DPRINTF(" link 0x%08x (T = %d):", \
1626 link, \
1627 link & EHCI_LINK_TERMINATE ? 1 : 0, 0, 0); \
1628 if (type) { \
1629 DPRINTF( \
1630 " ITD = %d QH = %d SITD = %d FSTN = %d",\
1631 EHCI_LINK_TYPE(link) == EHCI_LINK_ITD ? 1 : 0, \
1632 EHCI_LINK_TYPE(link) == EHCI_LINK_QH ? 1 : 0, \
1633 EHCI_LINK_TYPE(link) == EHCI_LINK_SITD ? 1 : 0, \
1634 EHCI_LINK_TYPE(link) == EHCI_LINK_FSTN ? 1 : 0); \
1635 } \
1636 } while(0)
1637
1638 Static void
1639 ehci_dump_sqtds(ehci_soft_qtd_t *sqtd)
1640 {
1641 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1642 int i;
1643 uint32_t stop = 0;
1644
1645 for (i = 0; sqtd && i < 20 && !stop; sqtd = sqtd->nextqtd, i++) {
1646 ehci_dump_sqtd(sqtd);
1647 usb_syncmem(&sqtd->dma,
1648 sqtd->offs + offsetof(ehci_qtd_t, qtd_next),
1649 sizeof(sqtd->qtd),
1650 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1651 stop = sqtd->qtd.qtd_next & htole32(EHCI_LINK_TERMINATE);
1652 usb_syncmem(&sqtd->dma,
1653 sqtd->offs + offsetof(ehci_qtd_t, qtd_next),
1654 sizeof(sqtd->qtd), BUS_DMASYNC_PREREAD);
1655 }
1656 if (!stop)
1657 DPRINTF("dump aborted, too many TDs", 0, 0, 0, 0);
1658 }
1659
1660 Static void
1661 ehci_dump_sqtd(ehci_soft_qtd_t *sqtd)
1662 {
1663 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1664
1665 usb_syncmem(&sqtd->dma, sqtd->offs,
1666 sizeof(sqtd->qtd), BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1667
1668 DPRINTFN(10, "QTD(%p) at 0x%08x:", sqtd, sqtd->physaddr, 0, 0);
1669 ehci_dump_qtd(&sqtd->qtd);
1670
1671 usb_syncmem(&sqtd->dma, sqtd->offs,
1672 sizeof(sqtd->qtd), BUS_DMASYNC_PREREAD);
1673 }
1674
1675 Static void
1676 ehci_dump_qtd(ehci_qtd_t *qtd)
1677 {
1678 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1679 uint32_t s = le32toh(qtd->qtd_status);
1680
1681 DPRINTFN(10,
1682 " next = 0x%08x altnext = 0x%08x status = 0x%08x",
1683 qtd->qtd_next, qtd->qtd_altnext, s, 0);
1684 DPRINTFN(10,
1685 " toggle = %d ioc = %d bytes = %#x "
1686 "c_page = %#x", EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_IOC(s),
1687 EHCI_QTD_GET_BYTES(s), EHCI_QTD_GET_C_PAGE(s));
1688 DPRINTFN(10,
1689 " cerr = %d pid = %d stat = %x",
1690 EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s), EHCI_QTD_GET_STATUS(s),
1691 0);
1692 DPRINTFN(10,
1693 "active =%d halted=%d buferr=%d babble=%d",
1694 s & EHCI_QTD_ACTIVE ? 1 : 0,
1695 s & EHCI_QTD_HALTED ? 1 : 0,
1696 s & EHCI_QTD_BUFERR ? 1 : 0,
1697 s & EHCI_QTD_BABBLE ? 1 : 0);
1698 DPRINTFN(10,
1699 "xacterr=%d missed=%d split =%d ping =%d",
1700 s & EHCI_QTD_XACTERR ? 1 : 0,
1701 s & EHCI_QTD_MISSEDMICRO ? 1 : 0,
1702 s & EHCI_QTD_SPLITXSTATE ? 1 : 0,
1703 s & EHCI_QTD_PINGSTATE ? 1 : 0);
1704 DPRINTFN(10,
1705 "buffer[0] = %#x buffer[1] = %#x "
1706 "buffer[2] = %#x buffer[3] = %#x",
1707 le32toh(qtd->qtd_buffer[0]), le32toh(qtd->qtd_buffer[1]),
1708 le32toh(qtd->qtd_buffer[2]), le32toh(qtd->qtd_buffer[3]));
1709 DPRINTFN(10,
1710 "buffer[4] = %#x", le32toh(qtd->qtd_buffer[4]), 0, 0, 0);
1711 }
1712
1713 Static void
1714 ehci_dump_sqh(ehci_soft_qh_t *sqh)
1715 {
1716 ehci_qh_t *qh = &sqh->qh;
1717 ehci_link_t link;
1718 uint32_t endp, endphub;
1719 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1720
1721 usb_syncmem(&sqh->dma, sqh->offs,
1722 sizeof(sqh->qh), BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1723
1724 DPRINTFN(10, "QH(%p) at %#x:", sqh, sqh->physaddr, 0, 0);
1725 link = le32toh(qh->qh_link);
1726 ehci_dump_link(link, true);
1727
1728 endp = le32toh(qh->qh_endp);
1729 DPRINTFN(10, " endp = %#x", endp, 0, 0, 0);
1730 DPRINTFN(10, " addr = 0x%02x inact = %d endpt = %d eps = %d",
1731 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
1732 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp));
1733 DPRINTFN(10, " dtc = %d hrecl = %d",
1734 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp), 0, 0);
1735 DPRINTFN(10, " ctl = %d nrl = %d mpl = %#x(%d)",
1736 EHCI_QH_GET_CTL(endp),EHCI_QH_GET_NRL(endp),
1737 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_MPL(endp));
1738
1739 endphub = le32toh(qh->qh_endphub);
1740 DPRINTFN(10, " endphub = %#x", endphub, 0, 0, 0);
1741 DPRINTFN(10, " smask = 0x%02x cmask = 0x%02x",
1742 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub), 1, 0);
1743 DPRINTFN(10, " huba = 0x%02x port = %d mult = %d",
1744 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
1745 EHCI_QH_GET_MULT(endphub), 0);
1746
1747 link = le32toh(qh->qh_curqtd);
1748 ehci_dump_link(link, false);
1749 DPRINTFN(10, "Overlay qTD:", 0, 0, 0, 0);
1750 ehci_dump_qtd(&qh->qh_qtd);
1751
1752 usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
1753 BUS_DMASYNC_PREREAD);
1754 }
1755
1756 Static void
1757 ehci_dump_itds(ehci_soft_itd_t *itd)
1758 {
1759 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1760 int i;
1761 uint32_t stop = 0;
1762
1763 for (i = 0; itd && i < 20 && !stop; itd = itd->xfer_next, i++) {
1764 ehci_dump_itd(itd);
1765 usb_syncmem(&itd->dma,
1766 itd->offs + offsetof(ehci_itd_t, itd_next),
1767 sizeof(itd->itd),
1768 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1769 stop = itd->itd.itd_next & htole32(EHCI_LINK_TERMINATE);
1770 usb_syncmem(&itd->dma,
1771 itd->offs + offsetof(ehci_itd_t, itd_next),
1772 sizeof(itd->itd), BUS_DMASYNC_PREREAD);
1773 }
1774 if (!stop)
1775 DPRINTF("dump aborted, too many TDs", 0, 0, 0, 0);
1776 }
1777
1778 Static void
1779 ehci_dump_itd(struct ehci_soft_itd *itd)
1780 {
1781 ehci_isoc_trans_t t;
1782 ehci_isoc_bufr_ptr_t b, b2, b3;
1783 int i;
1784
1785 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1786
1787 DPRINTF("ITD: next phys = %#x", itd->itd.itd_next, 0, 0, 0);
1788
1789 for (i = 0; i < EHCI_ITD_NUFRAMES; i++) {
1790 t = le32toh(itd->itd.itd_ctl[i]);
1791 DPRINTF("ITDctl %d: stat = %x len = %x",
1792 i, EHCI_ITD_GET_STATUS(t), EHCI_ITD_GET_LEN(t), 0);
1793 DPRINTF(" ioc = %x pg = %x offs = %x",
1794 EHCI_ITD_GET_IOC(t), EHCI_ITD_GET_PG(t),
1795 EHCI_ITD_GET_OFFS(t), 0);
1796 }
1797 DPRINTF("ITDbufr: ", 0, 0, 0, 0);
1798 for (i = 0; i < EHCI_ITD_NBUFFERS; i++)
1799 DPRINTF(" %x",
1800 EHCI_ITD_GET_BPTR(le32toh(itd->itd.itd_bufr[i])), 0, 0, 0);
1801
1802 b = le32toh(itd->itd.itd_bufr[0]);
1803 b2 = le32toh(itd->itd.itd_bufr[1]);
1804 b3 = le32toh(itd->itd.itd_bufr[2]);
1805 DPRINTF(" ep = %x daddr = %x dir = %d",
1806 EHCI_ITD_GET_EP(b), EHCI_ITD_GET_DADDR(b), EHCI_ITD_GET_DIR(b2), 0);
1807 DPRINTF(" maxpkt = %x multi = %x",
1808 EHCI_ITD_GET_MAXPKT(b2), EHCI_ITD_GET_MULTI(b3), 0, 0);
1809 }
1810
1811 Static void
1812 ehci_dump_sitd(struct ehci_soft_itd *itd)
1813 {
1814 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1815
1816 DPRINTF("SITD %p next = %p prev = %p",
1817 itd, itd->frame_list.next, itd->frame_list.prev, 0);
1818 DPRINTF(" xfernext=%p physaddr=%X slot=%d",
1819 itd->xfer_next, itd->physaddr, itd->slot, 0);
1820 }
1821
1822 Static void
1823 ehci_dump_exfer(struct ehci_xfer *ex)
1824 {
1825 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1826
1827 DPRINTF("ex = %p type %d isdone", ex, ex->ex_type,
1828 ex->ex_isdone, 0);
1829
1830 switch (ex->ex_type) {
1831 case EX_CTRL:
1832 DPRINTF(" setup = %p data = %p status = %p",
1833 ex->ex_setup, ex->ex_data, ex->ex_status, 0);
1834 break;
1835 case EX_BULK:
1836 case EX_INTR:
1837 DPRINTF(" qtdstart = %p qtdend = %p",
1838 ex->ex_sqtdstart, ex->ex_sqtdend, 0, 0);
1839 break;
1840 case EX_ISOC:
1841 DPRINTF(" itdstart = %p itdend = %p",
1842 ex->ex_itdstart, ex->ex_itdend, 0, 0);
1843 break;
1844 case EX_FS_ISOC:
1845 DPRINTF(" sitdstart = %p sitdend = %p",
1846 ex->ex_sitdstart, ex->ex_sitdend, 0, 0);
1847 break;
1848 default:
1849 DPRINTF(" unknown type", 0, 0, 0, 0);
1850 }
1851 }
1852 #endif
1853
1854 Static usbd_status
1855 ehci_open(struct usbd_pipe *pipe)
1856 {
1857 struct usbd_device *dev = pipe->up_dev;
1858 ehci_softc_t *sc = EHCI_PIPE2SC(pipe);
1859 usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
1860 uint8_t rhaddr = dev->ud_bus->ub_rhaddr;
1861 uint8_t addr = dev->ud_addr;
1862 uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
1863 struct ehci_pipe *epipe = EHCI_PIPE2EPIPE(pipe);
1864 ehci_soft_qh_t *sqh;
1865 usbd_status err;
1866 int ival, speed, naks;
1867 int hshubaddr, hshubport;
1868
1869 EHCIHIST_FUNC(); EHCIHIST_CALLED();
1870
1871 DPRINTF("pipe=%p, addr=%d, endpt=%d (%d)", pipe, addr,
1872 ed->bEndpointAddress, rhaddr);
1873
1874 if (dev->ud_myhsport) {
1875 /*
1876 * When directly attached FS/LS device while doing embedded
1877 * transaction translations and we are the hub, set the hub
1878 * address to 0 (us).
1879 */
1880 if (!(sc->sc_flags & EHCIF_ETTF)
1881 || (dev->ud_myhsport->up_parent->ud_addr != rhaddr)) {
1882 hshubaddr = dev->ud_myhsport->up_parent->ud_addr;
1883 } else {
1884 hshubaddr = 0;
1885 }
1886 hshubport = dev->ud_myhsport->up_portno;
1887 } else {
1888 hshubaddr = 0;
1889 hshubport = 0;
1890 }
1891
1892 if (sc->sc_dying)
1893 return USBD_IOERROR;
1894
1895 /* toggle state needed for bulk endpoints */
1896 epipe->nexttoggle = pipe->up_endpoint->ue_toggle;
1897
1898 if (addr == rhaddr) {
1899 switch (ed->bEndpointAddress) {
1900 case USB_CONTROL_ENDPOINT:
1901 pipe->up_methods = &roothub_ctrl_methods;
1902 break;
1903 case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
1904 pipe->up_methods = &ehci_root_intr_methods;
1905 break;
1906 default:
1907 DPRINTF("bad bEndpointAddress 0x%02x",
1908 ed->bEndpointAddress, 0, 0, 0);
1909 return USBD_INVAL;
1910 }
1911 return USBD_NORMAL_COMPLETION;
1912 }
1913
1914 /* XXX All this stuff is only valid for async. */
1915 switch (dev->ud_speed) {
1916 case USB_SPEED_LOW: speed = EHCI_QH_SPEED_LOW; break;
1917 case USB_SPEED_FULL: speed = EHCI_QH_SPEED_FULL; break;
1918 case USB_SPEED_HIGH: speed = EHCI_QH_SPEED_HIGH; break;
1919 default: panic("ehci_open: bad device speed %d", dev->ud_speed);
1920 }
1921 if (speed == EHCI_QH_SPEED_LOW && xfertype == UE_ISOCHRONOUS) {
1922 DPRINTF("hshubaddr=%d hshubport=%d", hshubaddr, hshubport, 0,
1923 0);
1924 return USBD_INVAL;
1925 }
1926
1927 /*
1928 * For interrupt transfer, nak throttling must be disabled, but for
1929 * the other transfer type, nak throttling should be enabled from the
1930 * viewpoint that avoids the memory thrashing.
1931 */
1932 naks = (xfertype == UE_INTERRUPT) ? 0
1933 : ((speed == EHCI_QH_SPEED_HIGH) ? 4 : 0);
1934
1935 /* Allocate sqh for everything, save isoc xfers */
1936 if (xfertype != UE_ISOCHRONOUS) {
1937 sqh = ehci_alloc_sqh(sc);
1938 if (sqh == NULL)
1939 return USBD_NOMEM;
1940 /* qh_link filled when the QH is added */
1941 sqh->qh.qh_endp = htole32(
1942 EHCI_QH_SET_ADDR(addr) |
1943 EHCI_QH_SET_ENDPT(UE_GET_ADDR(ed->bEndpointAddress)) |
1944 EHCI_QH_SET_EPS(speed) |
1945 EHCI_QH_DTC |
1946 EHCI_QH_SET_MPL(UGETW(ed->wMaxPacketSize)) |
1947 (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_CONTROL ?
1948 EHCI_QH_CTL : 0) |
1949 EHCI_QH_SET_NRL(naks)
1950 );
1951 sqh->qh.qh_endphub = htole32(
1952 EHCI_QH_SET_MULT(1) |
1953 EHCI_QH_SET_SMASK(xfertype == UE_INTERRUPT ? 0x02 : 0)
1954 );
1955 if (speed != EHCI_QH_SPEED_HIGH)
1956 sqh->qh.qh_endphub |= htole32(
1957 EHCI_QH_SET_PORT(hshubport) |
1958 EHCI_QH_SET_HUBA(hshubaddr) |
1959 (xfertype == UE_INTERRUPT ?
1960 EHCI_QH_SET_CMASK(0x08) : 0)
1961 );
1962 sqh->qh.qh_curqtd = EHCI_NULL;
1963 /* Fill the overlay qTD */
1964 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
1965 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
1966 sqh->qh.qh_qtd.qtd_status = htole32(0);
1967
1968 usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
1969 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
1970 epipe->sqh = sqh;
1971 } else {
1972 sqh = NULL;
1973 } /*xfertype == UE_ISOC*/
1974
1975 switch (xfertype) {
1976 case UE_CONTROL:
1977 err = usb_allocmem(&sc->sc_bus, sizeof(usb_device_request_t),
1978 0, &epipe->ctrl.reqdma);
1979 #ifdef EHCI_DEBUG
1980 if (err)
1981 printf("ehci_open: usb_allocmem()=%d\n", err);
1982 #endif
1983 if (err)
1984 goto bad;
1985 pipe->up_methods = &ehci_device_ctrl_methods;
1986 mutex_enter(&sc->sc_lock);
1987 ehci_add_qh(sc, sqh, sc->sc_async_head);
1988 mutex_exit(&sc->sc_lock);
1989 break;
1990 case UE_BULK:
1991 pipe->up_methods = &ehci_device_bulk_methods;
1992 mutex_enter(&sc->sc_lock);
1993 ehci_add_qh(sc, sqh, sc->sc_async_head);
1994 mutex_exit(&sc->sc_lock);
1995 break;
1996 case UE_INTERRUPT:
1997 pipe->up_methods = &ehci_device_intr_methods;
1998 ival = pipe->up_interval;
1999 if (ival == USBD_DEFAULT_INTERVAL) {
2000 if (speed == EHCI_QH_SPEED_HIGH) {
2001 if (ed->bInterval > 16) {
2002 /*
2003 * illegal with high-speed, but there
2004 * were documentation bugs in the spec,
2005 * so be generous
2006 */
2007 ival = 256;
2008 } else
2009 ival = (1 << (ed->bInterval - 1)) / 8;
2010 } else
2011 ival = ed->bInterval;
2012 }
2013 err = ehci_device_setintr(sc, sqh, ival);
2014 if (err)
2015 goto bad;
2016 break;
2017 case UE_ISOCHRONOUS:
2018 pipe->up_serialise = false;
2019 if (speed == EHCI_QH_SPEED_HIGH)
2020 pipe->up_methods = &ehci_device_isoc_methods;
2021 else
2022 pipe->up_methods = &ehci_device_fs_isoc_methods;
2023 if (ed->bInterval == 0 || ed->bInterval > 16) {
2024 printf("ehci: opening pipe with invalid bInterval\n");
2025 err = USBD_INVAL;
2026 goto bad;
2027 }
2028 if (UGETW(ed->wMaxPacketSize) == 0) {
2029 printf("ehci: zero length endpoint open request\n");
2030 err = USBD_INVAL;
2031 goto bad;
2032 }
2033 epipe->isoc.next_frame = 0;
2034 epipe->isoc.cur_xfers = 0;
2035 break;
2036 default:
2037 DPRINTF("bad xfer type %d", xfertype, 0, 0, 0);
2038 err = USBD_INVAL;
2039 goto bad;
2040 }
2041 return USBD_NORMAL_COMPLETION;
2042
2043 bad:
2044 if (sqh != NULL) {
2045 mutex_enter(&sc->sc_lock);
2046 ehci_free_sqh(sc, sqh);
2047 mutex_exit(&sc->sc_lock);
2048 }
2049 return err;
2050 }
2051
2052 /*
2053 * Add an ED to the schedule. Called with USB lock held.
2054 */
2055 Static void
2056 ehci_add_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
2057 {
2058
2059 KASSERT(mutex_owned(&sc->sc_lock));
2060
2061 EHCIHIST_FUNC(); EHCIHIST_CALLED();
2062
2063 usb_syncmem(&head->dma, head->offs + offsetof(ehci_qh_t, qh_link),
2064 sizeof(head->qh.qh_link), BUS_DMASYNC_POSTWRITE);
2065
2066 sqh->next = head->next;
2067 sqh->qh.qh_link = head->qh.qh_link;
2068
2069 usb_syncmem(&sqh->dma, sqh->offs + offsetof(ehci_qh_t, qh_link),
2070 sizeof(sqh->qh.qh_link), BUS_DMASYNC_PREWRITE);
2071
2072 head->next = sqh;
2073 head->qh.qh_link = htole32(sqh->physaddr | EHCI_LINK_QH);
2074
2075 usb_syncmem(&head->dma, head->offs + offsetof(ehci_qh_t, qh_link),
2076 sizeof(head->qh.qh_link), BUS_DMASYNC_PREWRITE);
2077
2078 #ifdef EHCI_DEBUG
2079 DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
2080 ehci_dump_sqh(sqh);
2081 DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
2082 #endif
2083 }
2084
2085 /*
2086 * Remove an ED from the schedule. Called with USB lock held.
2087 */
2088 Static void
2089 ehci_rem_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
2090 {
2091 ehci_soft_qh_t *p;
2092
2093 KASSERT(mutex_owned(&sc->sc_lock));
2094
2095 /* XXX */
2096 for (p = head; p != NULL && p->next != sqh; p = p->next)
2097 ;
2098 if (p == NULL)
2099 panic("ehci_rem_qh: ED not found");
2100 usb_syncmem(&sqh->dma, sqh->offs + offsetof(ehci_qh_t, qh_link),
2101 sizeof(sqh->qh.qh_link), BUS_DMASYNC_POSTWRITE);
2102 p->next = sqh->next;
2103 p->qh.qh_link = sqh->qh.qh_link;
2104 usb_syncmem(&p->dma, p->offs + offsetof(ehci_qh_t, qh_link),
2105 sizeof(p->qh.qh_link), BUS_DMASYNC_PREWRITE);
2106
2107 ehci_sync_hc(sc);
2108 }
2109
2110 Static void
2111 ehci_set_qh_qtd(ehci_soft_qh_t *sqh, ehci_soft_qtd_t *sqtd)
2112 {
2113 int i;
2114 uint32_t status;
2115
2116 /* Save toggle bit and ping status. */
2117 usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
2118 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
2119 status = sqh->qh.qh_qtd.qtd_status &
2120 htole32(EHCI_QTD_TOGGLE_MASK |
2121 EHCI_QTD_SET_STATUS(EHCI_QTD_PINGSTATE));
2122 /* Set HALTED to make hw leave it alone. */
2123 sqh->qh.qh_qtd.qtd_status =
2124 htole32(EHCI_QTD_SET_STATUS(EHCI_QTD_HALTED));
2125 usb_syncmem(&sqh->dma,
2126 sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
2127 sizeof(sqh->qh.qh_qtd.qtd_status),
2128 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2129 sqh->qh.qh_curqtd = 0;
2130 sqh->qh.qh_qtd.qtd_next = htole32(sqtd->physaddr);
2131 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
2132 for (i = 0; i < EHCI_QTD_NBUFFERS; i++)
2133 sqh->qh.qh_qtd.qtd_buffer[i] = 0;
2134 sqh->sqtd = sqtd;
2135 usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
2136 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2137 /* Set !HALTED && !ACTIVE to start execution, preserve some fields */
2138 sqh->qh.qh_qtd.qtd_status = status;
2139 usb_syncmem(&sqh->dma,
2140 sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
2141 sizeof(sqh->qh.qh_qtd.qtd_status),
2142 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2143 }
2144
2145 /*
2146 * Ensure that the HC has released all references to the QH. We do this
2147 * by asking for a Async Advance Doorbell interrupt and then we wait for
2148 * the interrupt.
2149 * To make this easier we first obtain exclusive use of the doorbell.
2150 */
2151 Static void
2152 ehci_sync_hc(ehci_softc_t *sc)
2153 {
2154 int error __diagused;
2155
2156 KASSERT(mutex_owned(&sc->sc_lock));
2157
2158 EHCIHIST_FUNC(); EHCIHIST_CALLED();
2159
2160 if (sc->sc_dying) {
2161 DPRINTF("dying", 0, 0, 0, 0);
2162 return;
2163 }
2164
2165 /* ask for doorbell */
2166 EOWRITE4(sc, EHCI_USBCMD, EOREAD4(sc, EHCI_USBCMD) | EHCI_CMD_IAAD);
2167 DPRINTF("cmd = 0x%08x sts = 0x%08x",
2168 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS), 0, 0);
2169
2170 sc->sc_dbanswered = false;
2171 /* bell wait */
2172 while (!sc->sc_dbanswered) {
2173 error = cv_timedwait(&sc->sc_doorbell, &sc->sc_lock, hz);
2174
2175 DPRINTF("cmd = 0x%08x sts = 0x%08x ... done",
2176 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS), 0, 0);
2177 #ifdef DIAGNOSTIC
2178 if (error == EWOULDBLOCK) {
2179 printf("%s: timed out\n", __func__);
2180 } else if (error) {
2181 printf("%s: cv_timedwait: error %d\n", __func__, error);
2182 }
2183 #endif
2184 }
2185 }
2186
2187 Static void
2188 ehci_remove_itd_chain(ehci_softc_t *sc, struct ehci_soft_itd *itd)
2189 {
2190
2191 KASSERT(mutex_owned(&sc->sc_lock));
2192
2193 for (; itd != NULL; itd = itd->xfer_next) {
2194 struct ehci_soft_itd *prev = itd->frame_list.prev;
2195
2196 /* Unlink itd from hardware chain, or frame array */
2197 if (prev == NULL) { /* We're at the table head */
2198 sc->sc_softitds[itd->slot] = itd->frame_list.next;
2199 sc->sc_flist[itd->slot] = itd->itd.itd_next;
2200 usb_syncmem(&sc->sc_fldma,
2201 sizeof(ehci_link_t) * itd->slot,
2202 sizeof(ehci_link_t),
2203 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2204
2205 if (itd->frame_list.next != NULL)
2206 itd->frame_list.next->frame_list.prev = NULL;
2207 } else {
2208 /* XXX this part is untested... */
2209 prev->itd.itd_next = itd->itd.itd_next;
2210 usb_syncmem(&itd->dma,
2211 itd->offs + offsetof(ehci_itd_t, itd_next),
2212 sizeof(itd->itd.itd_next), BUS_DMASYNC_PREWRITE);
2213
2214 prev->frame_list.next = itd->frame_list.next;
2215 if (itd->frame_list.next != NULL)
2216 itd->frame_list.next->frame_list.prev = prev;
2217 }
2218 }
2219 }
2220
2221 Static void
2222 ehci_free_itd_chain(ehci_softc_t *sc, struct ehci_soft_itd *itd)
2223 {
2224 struct ehci_soft_itd *next;
2225
2226 mutex_enter(&sc->sc_lock);
2227 next = NULL;
2228 for (; itd != NULL; itd = next) {
2229 next = itd->xfer_next;
2230 ehci_free_itd_locked(sc, itd);
2231 }
2232 mutex_exit(&sc->sc_lock);
2233 }
2234
2235 Static void
2236 ehci_remove_sitd_chain(ehci_softc_t *sc, struct ehci_soft_sitd *sitd)
2237 {
2238
2239 KASSERT(mutex_owned(&sc->sc_lock));
2240
2241 for (; sitd != NULL; sitd = sitd->xfer_next) {
2242 struct ehci_soft_sitd *prev = sitd->frame_list.prev;
2243
2244 /* Unlink sitd from hardware chain, or frame array */
2245 if (prev == NULL) { /* We're at the table head */
2246 sc->sc_softsitds[sitd->slot] = sitd->frame_list.next;
2247 sc->sc_flist[sitd->slot] = sitd->sitd.sitd_next;
2248 usb_syncmem(&sc->sc_fldma,
2249 sizeof(ehci_link_t) * sitd->slot,
2250 sizeof(ehci_link_t),
2251 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2252
2253 if (sitd->frame_list.next != NULL)
2254 sitd->frame_list.next->frame_list.prev = NULL;
2255 } else {
2256 /* XXX this part is untested... */
2257 prev->sitd.sitd_next = sitd->sitd.sitd_next;
2258 usb_syncmem(&sitd->dma,
2259 sitd->offs + offsetof(ehci_sitd_t, sitd_next),
2260 sizeof(sitd->sitd.sitd_next), BUS_DMASYNC_PREWRITE);
2261
2262 prev->frame_list.next = sitd->frame_list.next;
2263 if (sitd->frame_list.next != NULL)
2264 sitd->frame_list.next->frame_list.prev = prev;
2265 }
2266 }
2267 }
2268
2269 Static void
2270 ehci_free_sitd_chain(ehci_softc_t *sc, struct ehci_soft_sitd *sitd)
2271 {
2272
2273 mutex_enter(&sc->sc_lock);
2274 struct ehci_soft_sitd *next = NULL;
2275 for (; sitd != NULL; sitd = next) {
2276 next = sitd->xfer_next;
2277 ehci_free_sitd_locked(sc, sitd);
2278 }
2279 mutex_exit(&sc->sc_lock);
2280 }
2281
2282 /***********/
2283
2284 Static int
2285 ehci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
2286 void *buf, int buflen)
2287 {
2288 ehci_softc_t *sc = EHCI_BUS2SC(bus);
2289 usb_hub_descriptor_t hubd;
2290 usb_port_status_t ps;
2291 uint16_t len, value, index;
2292 int l, totlen = 0;
2293 int port, i;
2294 uint32_t v;
2295
2296 EHCIHIST_FUNC(); EHCIHIST_CALLED();
2297
2298 if (sc->sc_dying)
2299 return -1;
2300
2301 DPRINTF("type=0x%02x request=%02x", req->bmRequestType, req->bRequest,
2302 0, 0);
2303
2304 len = UGETW(req->wLength);
2305 value = UGETW(req->wValue);
2306 index = UGETW(req->wIndex);
2307
2308 #define C(x,y) ((x) | ((y) << 8))
2309 switch (C(req->bRequest, req->bmRequestType)) {
2310 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2311 if (len == 0)
2312 break;
2313 switch (value) {
2314 case C(0, UDESC_DEVICE): {
2315 usb_device_descriptor_t devd;
2316 totlen = min(buflen, sizeof(devd));
2317 memcpy(&devd, buf, totlen);
2318 USETW(devd.idVendor, sc->sc_id_vendor);
2319 memcpy(buf, &devd, totlen);
2320 break;
2321
2322 }
2323 #define sd ((usb_string_descriptor_t *)buf)
2324 case C(1, UDESC_STRING):
2325 /* Vendor */
2326 totlen = usb_makestrdesc(sd, len, sc->sc_vendor);
2327 break;
2328 case C(2, UDESC_STRING):
2329 /* Product */
2330 totlen = usb_makestrdesc(sd, len, "EHCI root hub");
2331 break;
2332 #undef sd
2333 default:
2334 /* default from usbroothub */
2335 return buflen;
2336 }
2337 break;
2338
2339 /* Hub requests */
2340 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2341 break;
2342 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2343 DPRINTF("UR_CLEAR_PORT_FEATURE port=%d feature=%d", index,
2344 value, 0, 0);
2345 if (index < 1 || index > sc->sc_noport) {
2346 return -1;
2347 }
2348 port = EHCI_PORTSC(index);
2349 v = EOREAD4(sc, port);
2350 DPRINTF("portsc=0x%08x", v, 0, 0, 0);
2351 v &= ~EHCI_PS_CLEAR;
2352 switch (value) {
2353 case UHF_PORT_ENABLE:
2354 EOWRITE4(sc, port, v &~ EHCI_PS_PE);
2355 break;
2356 case UHF_PORT_SUSPEND:
2357 if (!(v & EHCI_PS_SUSP)) /* not suspended */
2358 break;
2359 v &= ~EHCI_PS_SUSP;
2360 EOWRITE4(sc, port, v | EHCI_PS_FPR);
2361 /* see USB2 spec ch. 7.1.7.7 */
2362 usb_delay_ms(&sc->sc_bus, 20);
2363 EOWRITE4(sc, port, v);
2364 usb_delay_ms(&sc->sc_bus, 2);
2365 #ifdef DEBUG
2366 v = EOREAD4(sc, port);
2367 if (v & (EHCI_PS_FPR | EHCI_PS_SUSP))
2368 printf("ehci: resume failed: %x\n", v);
2369 #endif
2370 break;
2371 case UHF_PORT_POWER:
2372 if (sc->sc_hasppc)
2373 EOWRITE4(sc, port, v &~ EHCI_PS_PP);
2374 break;
2375 case UHF_PORT_TEST:
2376 DPRINTF("clear port test %d", index, 0, 0, 0);
2377 break;
2378 case UHF_PORT_INDICATOR:
2379 DPRINTF("clear port ind %d", index, 0, 0, 0);
2380 EOWRITE4(sc, port, v &~ EHCI_PS_PIC);
2381 break;
2382 case UHF_C_PORT_CONNECTION:
2383 EOWRITE4(sc, port, v | EHCI_PS_CSC);
2384 break;
2385 case UHF_C_PORT_ENABLE:
2386 EOWRITE4(sc, port, v | EHCI_PS_PEC);
2387 break;
2388 case UHF_C_PORT_SUSPEND:
2389 /* how? */
2390 break;
2391 case UHF_C_PORT_OVER_CURRENT:
2392 EOWRITE4(sc, port, v | EHCI_PS_OCC);
2393 break;
2394 case UHF_C_PORT_RESET:
2395 sc->sc_isreset[index] = 0;
2396 break;
2397 default:
2398 return -1;
2399 }
2400 #if 0
2401 switch(value) {
2402 case UHF_C_PORT_CONNECTION:
2403 case UHF_C_PORT_ENABLE:
2404 case UHF_C_PORT_SUSPEND:
2405 case UHF_C_PORT_OVER_CURRENT:
2406 case UHF_C_PORT_RESET:
2407 default:
2408 break;
2409 }
2410 #endif
2411 break;
2412 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2413 if (len == 0)
2414 break;
2415 if ((value & 0xff) != 0) {
2416 return -1;
2417 }
2418 totlen = min(buflen, sizeof(hubd));
2419 memcpy(&hubd, buf, totlen);
2420 hubd.bNbrPorts = sc->sc_noport;
2421 v = EOREAD4(sc, EHCI_HCSPARAMS);
2422 USETW(hubd.wHubCharacteristics,
2423 EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH |
2424 EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS))
2425 ? UHD_PORT_IND : 0);
2426 hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */
2427 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
2428 hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
2429 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2430 totlen = min(totlen, hubd.bDescLength);
2431 memcpy(buf, &hubd, totlen);
2432 break;
2433 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2434 if (len != 4) {
2435 return -1;
2436 }
2437 memset(buf, 0, len); /* ? XXX */
2438 totlen = len;
2439 break;
2440 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2441 DPRINTF("get port status i=%d", index, 0, 0, 0);
2442 if (index < 1 || index > sc->sc_noport) {
2443 return -1;
2444 }
2445 if (len != 4) {
2446 return -1;
2447 }
2448 v = EOREAD4(sc, EHCI_PORTSC(index));
2449 DPRINTF("port status=0x%04x", v, 0, 0, 0);
2450
2451 i = UPS_HIGH_SPEED;
2452 if (sc->sc_flags & EHCIF_ETTF) {
2453 /*
2454 * If we are doing embedded transaction translation,
2455 * then directly attached LS/FS devices are reset by
2456 * the EHCI controller itself. PSPD is encoded
2457 * the same way as in USBSTATUS.
2458 */
2459 i = __SHIFTOUT(v, EHCI_PS_PSPD) * UPS_LOW_SPEED;
2460 }
2461 if (v & EHCI_PS_CS) i |= UPS_CURRENT_CONNECT_STATUS;
2462 if (v & EHCI_PS_PE) i |= UPS_PORT_ENABLED;
2463 if (v & EHCI_PS_SUSP) i |= UPS_SUSPEND;
2464 if (v & EHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR;
2465 if (v & EHCI_PS_PR) i |= UPS_RESET;
2466 if (v & EHCI_PS_PP) i |= UPS_PORT_POWER;
2467 if (sc->sc_vendor_port_status)
2468 i = sc->sc_vendor_port_status(sc, v, i);
2469 USETW(ps.wPortStatus, i);
2470 i = 0;
2471 if (v & EHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS;
2472 if (v & EHCI_PS_PEC) i |= UPS_C_PORT_ENABLED;
2473 if (v & EHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR;
2474 if (sc->sc_isreset[index]) i |= UPS_C_PORT_RESET;
2475 USETW(ps.wPortChange, i);
2476 totlen = min(len, sizeof(ps));
2477 memcpy(buf, &ps, totlen);
2478 break;
2479 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2480 return -1;
2481 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2482 break;
2483 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2484 if (index < 1 || index > sc->sc_noport) {
2485 return -1;
2486 }
2487 port = EHCI_PORTSC(index);
2488 v = EOREAD4(sc, port);
2489 DPRINTF("portsc=0x%08x", v, 0, 0, 0);
2490 v &= ~EHCI_PS_CLEAR;
2491 switch(value) {
2492 case UHF_PORT_ENABLE:
2493 EOWRITE4(sc, port, v | EHCI_PS_PE);
2494 break;
2495 case UHF_PORT_SUSPEND:
2496 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
2497 break;
2498 case UHF_PORT_RESET:
2499 DPRINTF("reset port %d", index, 0, 0, 0);
2500 if (EHCI_PS_IS_LOWSPEED(v)
2501 && sc->sc_ncomp > 0
2502 && !(sc->sc_flags & EHCIF_ETTF)) {
2503 /*
2504 * Low speed device on non-ETTF controller or
2505 * unaccompanied controller, give up ownership.
2506 */
2507 ehci_disown(sc, index, 1);
2508 break;
2509 }
2510 /* Start reset sequence. */
2511 v &= ~ (EHCI_PS_PE | EHCI_PS_PR);
2512 EOWRITE4(sc, port, v | EHCI_PS_PR);
2513 /* Wait for reset to complete. */
2514 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
2515 if (sc->sc_dying) {
2516 return -1;
2517 }
2518 /*
2519 * An embedded transaction translator will automatically
2520 * terminate the reset sequence so there's no need to
2521 * it.
2522 */
2523 v = EOREAD4(sc, port);
2524 if (v & EHCI_PS_PR) {
2525 /* Terminate reset sequence. */
2526 EOWRITE4(sc, port, v & ~EHCI_PS_PR);
2527 /* Wait for HC to complete reset. */
2528 usb_delay_ms(&sc->sc_bus,
2529 EHCI_PORT_RESET_COMPLETE);
2530 if (sc->sc_dying) {
2531 return -1;
2532 }
2533 }
2534
2535 v = EOREAD4(sc, port);
2536 DPRINTF("ehci after reset, status=0x%08x", v, 0, 0, 0);
2537 if (v & EHCI_PS_PR) {
2538 printf("%s: port reset timeout\n",
2539 device_xname(sc->sc_dev));
2540 return USBD_TIMEOUT;
2541 }
2542 if (!(v & EHCI_PS_PE)) {
2543 /* Not a high speed device, give up ownership.*/
2544 ehci_disown(sc, index, 0);
2545 break;
2546 }
2547 sc->sc_isreset[index] = 1;
2548 DPRINTF("ehci port %d reset, status = 0x%08x", index,
2549 v, 0, 0);
2550 break;
2551 case UHF_PORT_POWER:
2552 DPRINTF("set port power %d (has PPC = %d)", index,
2553 sc->sc_hasppc, 0, 0);
2554 if (sc->sc_hasppc)
2555 EOWRITE4(sc, port, v | EHCI_PS_PP);
2556 break;
2557 case UHF_PORT_TEST:
2558 DPRINTF("set port test %d", index, 0, 0, 0);
2559 break;
2560 case UHF_PORT_INDICATOR:
2561 DPRINTF("set port ind %d", index, 0, 0, 0);
2562 EOWRITE4(sc, port, v | EHCI_PS_PIC);
2563 break;
2564 default:
2565 return -1;
2566 }
2567 break;
2568 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
2569 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
2570 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
2571 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
2572 break;
2573 default:
2574 /* default from usbroothub */
2575 DPRINTF("returning %d (usbroothub default)", buflen, 0, 0, 0);
2576
2577 return buflen;
2578 }
2579
2580 DPRINTF("returning %d", totlen, 0, 0, 0);
2581
2582 return totlen;
2583 }
2584
2585 Static void
2586 ehci_disown(ehci_softc_t *sc, int index, int lowspeed)
2587 {
2588 int port;
2589 uint32_t v;
2590
2591 EHCIHIST_FUNC(); EHCIHIST_CALLED();
2592
2593 DPRINTF("index=%d lowspeed=%d", index, lowspeed, 0, 0);
2594 #ifdef DIAGNOSTIC
2595 if (sc->sc_npcomp != 0) {
2596 int i = (index-1) / sc->sc_npcomp;
2597 if (i >= sc->sc_ncomp)
2598 printf("%s: strange port\n",
2599 device_xname(sc->sc_dev));
2600 else
2601 printf("%s: handing over %s speed device on "
2602 "port %d to %s\n",
2603 device_xname(sc->sc_dev),
2604 lowspeed ? "low" : "full",
2605 index, sc->sc_comps[i] ?
2606 device_xname(sc->sc_comps[i]) :
2607 "companion controller");
2608 } else {
2609 printf("%s: npcomp == 0\n", device_xname(sc->sc_dev));
2610 }
2611 #endif
2612 port = EHCI_PORTSC(index);
2613 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
2614 EOWRITE4(sc, port, v | EHCI_PS_PO);
2615 }
2616
2617 Static usbd_status
2618 ehci_root_intr_transfer(struct usbd_xfer *xfer)
2619 {
2620 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
2621 usbd_status err;
2622
2623 /* Insert last in queue. */
2624 mutex_enter(&sc->sc_lock);
2625 err = usb_insert_transfer(xfer);
2626 mutex_exit(&sc->sc_lock);
2627 if (err)
2628 return err;
2629
2630 /* Pipe isn't running, start first */
2631 return ehci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
2632 }
2633
2634 Static usbd_status
2635 ehci_root_intr_start(struct usbd_xfer *xfer)
2636 {
2637 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
2638
2639 if (sc->sc_dying)
2640 return USBD_IOERROR;
2641
2642 mutex_enter(&sc->sc_lock);
2643 sc->sc_intrxfer = xfer;
2644 mutex_exit(&sc->sc_lock);
2645
2646 return USBD_IN_PROGRESS;
2647 }
2648
2649 /* Abort a root interrupt request. */
2650 Static void
2651 ehci_root_intr_abort(struct usbd_xfer *xfer)
2652 {
2653 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
2654
2655 KASSERT(mutex_owned(&sc->sc_lock));
2656 KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
2657
2658 sc->sc_intrxfer = NULL;
2659
2660 xfer->ux_status = USBD_CANCELLED;
2661 usb_transfer_complete(xfer);
2662 }
2663
2664 /* Close the root pipe. */
2665 Static void
2666 ehci_root_intr_close(struct usbd_pipe *pipe)
2667 {
2668 ehci_softc_t *sc = EHCI_PIPE2SC(pipe);
2669
2670 EHCIHIST_FUNC(); EHCIHIST_CALLED();
2671
2672 KASSERT(mutex_owned(&sc->sc_lock));
2673
2674 sc->sc_intrxfer = NULL;
2675 }
2676
2677 Static void
2678 ehci_root_intr_done(struct usbd_xfer *xfer)
2679 {
2680 }
2681
2682 /************************/
2683
2684 Static ehci_soft_qh_t *
2685 ehci_alloc_sqh(ehci_softc_t *sc)
2686 {
2687 ehci_soft_qh_t *sqh;
2688 usbd_status err;
2689 int i, offs;
2690 usb_dma_t dma;
2691
2692 EHCIHIST_FUNC(); EHCIHIST_CALLED();
2693
2694 mutex_enter(&sc->sc_lock);
2695 if (sc->sc_freeqhs == NULL) {
2696 DPRINTF("allocating chunk", 0, 0, 0, 0);
2697 mutex_exit(&sc->sc_lock);
2698
2699 err = usb_allocmem(&sc->sc_bus, EHCI_SQH_SIZE * EHCI_SQH_CHUNK,
2700 EHCI_PAGE_SIZE, &dma);
2701 #ifdef EHCI_DEBUG
2702 if (err)
2703 printf("ehci_alloc_sqh: usb_allocmem()=%d\n", err);
2704 #endif
2705 if (err)
2706 return NULL;
2707
2708 mutex_enter(&sc->sc_lock);
2709 for (i = 0; i < EHCI_SQH_CHUNK; i++) {
2710 offs = i * EHCI_SQH_SIZE;
2711 sqh = KERNADDR(&dma, offs);
2712 sqh->physaddr = DMAADDR(&dma, offs);
2713 sqh->dma = dma;
2714 sqh->offs = offs;
2715 sqh->next = sc->sc_freeqhs;
2716 sc->sc_freeqhs = sqh;
2717 }
2718 }
2719 sqh = sc->sc_freeqhs;
2720 sc->sc_freeqhs = sqh->next;
2721 mutex_exit(&sc->sc_lock);
2722
2723 memset(&sqh->qh, 0, sizeof(ehci_qh_t));
2724 sqh->next = NULL;
2725 return sqh;
2726 }
2727
2728 Static void
2729 ehci_free_sqh(ehci_softc_t *sc, ehci_soft_qh_t *sqh)
2730 {
2731 KASSERT(mutex_owned(&sc->sc_lock));
2732
2733 sqh->next = sc->sc_freeqhs;
2734 sc->sc_freeqhs = sqh;
2735 }
2736
2737 Static ehci_soft_qtd_t *
2738 ehci_alloc_sqtd(ehci_softc_t *sc)
2739 {
2740 ehci_soft_qtd_t *sqtd = NULL;
2741 usbd_status err;
2742 int i, offs;
2743 usb_dma_t dma;
2744
2745 EHCIHIST_FUNC(); EHCIHIST_CALLED();
2746
2747 mutex_enter(&sc->sc_lock);
2748 if (sc->sc_freeqtds == NULL) {
2749 DPRINTF("allocating chunk", 0, 0, 0, 0);
2750 mutex_exit(&sc->sc_lock);
2751
2752 err = usb_allocmem(&sc->sc_bus, EHCI_SQTD_SIZE*EHCI_SQTD_CHUNK,
2753 EHCI_PAGE_SIZE, &dma);
2754 #ifdef EHCI_DEBUG
2755 if (err)
2756 printf("ehci_alloc_sqtd: usb_allocmem()=%d\n", err);
2757 #endif
2758 if (err)
2759 goto done;
2760
2761 mutex_enter(&sc->sc_lock);
2762 for (i = 0; i < EHCI_SQTD_CHUNK; i++) {
2763 offs = i * EHCI_SQTD_SIZE;
2764 sqtd = KERNADDR(&dma, offs);
2765 sqtd->physaddr = DMAADDR(&dma, offs);
2766 sqtd->dma = dma;
2767 sqtd->offs = offs;
2768
2769 sqtd->nextqtd = sc->sc_freeqtds;
2770 sc->sc_freeqtds = sqtd;
2771 }
2772 }
2773
2774 sqtd = sc->sc_freeqtds;
2775 sc->sc_freeqtds = sqtd->nextqtd;
2776 mutex_exit(&sc->sc_lock);
2777
2778 memset(&sqtd->qtd, 0, sizeof(ehci_qtd_t));
2779 sqtd->nextqtd = NULL;
2780 sqtd->xfer = NULL;
2781
2782 done:
2783 return sqtd;
2784 }
2785
2786 Static void
2787 ehci_free_sqtd(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd)
2788 {
2789
2790 mutex_enter(&sc->sc_lock);
2791 sqtd->nextqtd = sc->sc_freeqtds;
2792 sc->sc_freeqtds = sqtd;
2793 mutex_exit(&sc->sc_lock);
2794 }
2795
2796 Static int
2797 ehci_alloc_sqtd_chain(ehci_softc_t *sc, struct usbd_xfer *xfer,
2798 int alen, int rd, ehci_soft_qtd_t **sp)
2799 {
2800 struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
2801 uint16_t flags = xfer->ux_flags;
2802
2803 EHCIHIST_FUNC(); EHCIHIST_CALLED();
2804
2805 ASSERT_SLEEPABLE();
2806 KASSERT(sp);
2807 KASSERT(alen != 0 || (!rd && (flags & USBD_FORCE_SHORT_XFER)));
2808
2809 size_t nsqtd = (!rd && (flags & USBD_FORCE_SHORT_XFER)) ? 1 : 0;
2810 nsqtd += ((alen + EHCI_PAGE_SIZE - 1) / EHCI_PAGE_SIZE);
2811 exfer->ex_sqtds = kmem_zalloc(sizeof(ehci_soft_qtd_t *) * nsqtd,
2812 KM_SLEEP);
2813 exfer->ex_nsqtd = nsqtd;
2814
2815 DPRINTF("xfer %p len %d nsqtd %d flags %x", xfer, alen, nsqtd, flags);
2816
2817 for (size_t j = 0; j < exfer->ex_nsqtd;) {
2818 ehci_soft_qtd_t *cur = ehci_alloc_sqtd(sc);
2819 if (cur == NULL)
2820 goto nomem;
2821 exfer->ex_sqtds[j++] = cur;
2822
2823 cur->xfer = xfer;
2824 cur->len = 0;
2825
2826 }
2827
2828 *sp = exfer->ex_sqtds[0];
2829 DPRINTF("return sqtd=%p", *sp, 0, 0, 0);
2830
2831 return 0;
2832
2833 nomem:
2834 ehci_free_sqtds(sc, exfer);
2835 kmem_free(exfer->ex_sqtds, sizeof(ehci_soft_qtd_t *) * nsqtd);
2836 DPRINTF("no memory", 0, 0, 0, 0);
2837 return ENOMEM;
2838 }
2839
2840 Static void
2841 ehci_free_sqtds(ehci_softc_t *sc, struct ehci_xfer *exfer)
2842 {
2843 EHCIHIST_FUNC(); EHCIHIST_CALLED();
2844 DPRINTF("exfer=%p", exfer, 0, 0, 0);
2845
2846 mutex_enter(&sc->sc_lock);
2847 for (size_t i = 0; i < exfer->ex_nsqtd; i++) {
2848 ehci_soft_qtd_t *sqtd = exfer->ex_sqtds[i];
2849
2850 if (sqtd == NULL)
2851 break;
2852
2853 sqtd->nextqtd = sc->sc_freeqtds;
2854 sc->sc_freeqtds = sqtd;
2855 }
2856 mutex_exit(&sc->sc_lock);
2857 }
2858
2859 Static void
2860 ehci_append_sqtd(ehci_soft_qtd_t *sqtd, ehci_soft_qtd_t *prev)
2861 {
2862 if (prev) {
2863 prev->nextqtd = sqtd;
2864 prev->qtd.qtd_next = htole32(sqtd->physaddr);
2865 prev->qtd.qtd_altnext = prev->qtd.qtd_next;
2866 usb_syncmem(&prev->dma, prev->offs, sizeof(prev->qtd),
2867 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
2868 }
2869 }
2870
2871 Static void
2872 ehci_reset_sqtd_chain(ehci_softc_t *sc, struct usbd_xfer *xfer,
2873 int length, int isread, int *toggle, ehci_soft_qtd_t **lsqtd)
2874 {
2875 struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
2876 usb_dma_t *dma = &xfer->ux_dmabuf;
2877 uint16_t flags = xfer->ux_flags;
2878 ehci_soft_qtd_t *sqtd, *prev;
2879 int tog = *toggle;
2880 int mps = UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize);
2881 int len = length;
2882
2883 EHCIHIST_FUNC(); EHCIHIST_CALLED();
2884 DPRINTF("xfer=%p len %d isread %d toggle %d", xfer, len, isread, tog);
2885 DPRINTF(" VA %p", KERNADDR(&xfer->ux_dmabuf, 0), 0, 0, 0);
2886
2887 KASSERT(length != 0 || (!isread && (flags & USBD_FORCE_SHORT_XFER)));
2888
2889 const uint32_t qtdstatus = EHCI_QTD_ACTIVE |
2890 EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_IN : EHCI_QTD_PID_OUT) |
2891 EHCI_QTD_SET_CERR(3)
2892 ;
2893
2894 sqtd = prev = NULL;
2895 size_t curoffs = 0;
2896 size_t j = 0;
2897 for (; len != 0 && j < exfer->ex_nsqtd; prev = sqtd) {
2898 sqtd = exfer->ex_sqtds[j++];
2899 DPRINTF("sqtd[%d]=%p prev %p", j, sqtd, prev, 0);
2900
2901 /*
2902 * The EHCI hardware can handle at most 5 pages and they do
2903 * not have to be contiguous
2904 */
2905 vaddr_t va = (vaddr_t)KERNADDR(dma, curoffs);
2906 vaddr_t va_offs = EHCI_PAGE_OFFSET(va);
2907 size_t curlen = len;
2908 if (curlen >= EHCI_QTD_MAXTRANSFER - va_offs) {
2909 /* must use multiple TDs, fill as much as possible. */
2910 curlen = EHCI_QTD_MAXTRANSFER - va_offs;
2911
2912 /* the length must be a multiple of the max size */
2913 curlen -= curlen % mps;
2914 }
2915 KASSERT(curlen != 0);
2916 DPRINTF(" len=%d curlen=%d curoffs=%zu", len, curlen,
2917 curoffs, 0);
2918
2919 /* Fill the qTD */
2920 sqtd->qtd.qtd_next = sqtd->qtd.qtd_altnext = EHCI_NULL;
2921 sqtd->qtd.qtd_status = htole32(
2922 qtdstatus |
2923 EHCI_QTD_SET_BYTES(curlen) |
2924 EHCI_QTD_SET_TOGGLE(tog));
2925
2926 /* Find number of pages we'll be using, insert dma addresses */
2927 size_t pages = EHCI_NPAGES(curlen);
2928 KASSERT(pages <= EHCI_QTD_NBUFFERS);
2929 size_t pageoffs = EHCI_PAGE(curoffs);
2930 for (size_t i = 0; i < pages; i++) {
2931 paddr_t a = DMAADDR(dma,
2932 pageoffs + i * EHCI_PAGE_SIZE);
2933 sqtd->qtd.qtd_buffer[i] = htole32(EHCI_PAGE(a));
2934 /* Cast up to avoid compiler warnings */
2935 sqtd->qtd.qtd_buffer_hi[i] = htole32((uint64_t)a >> 32);
2936 DPRINTF(" buffer[%d/%d] 0x%08x 0x%08x", i, pages,
2937 le32toh(sqtd->qtd.qtd_buffer_hi[i]),
2938 le32toh(sqtd->qtd.qtd_buffer[i]));
2939 }
2940 /* First buffer pointer requires a page offset to start at */
2941 sqtd->qtd.qtd_buffer[0] |= htole32(va_offs);
2942
2943 usb_syncmem(&sqtd->dma, sqtd->offs, sizeof(sqtd->qtd),
2944 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2945
2946 sqtd->len = curlen;
2947
2948 DPRINTF(" va %p pa %p len %d", va,
2949 DMAADDR(&xfer->ux_dmabuf, curoffs), curlen, 0);
2950
2951 ehci_append_sqtd(sqtd, prev);
2952
2953 if (((curlen + mps - 1) / mps) & 1) {
2954 tog ^= 1;
2955 }
2956
2957 curoffs += curlen;
2958 len -= curlen;
2959 }
2960 KASSERTMSG(len == 0, "xfer %p olen %d len %d mps %d ex_nsqtd %zu j %zu",
2961 xfer, length, len, mps, exfer->ex_nsqtd, j);
2962
2963 if (!isread &&
2964 (flags & USBD_FORCE_SHORT_XFER) &&
2965 length % mps == 0) {
2966 /* Force a 0 length transfer at the end. */
2967
2968 KASSERTMSG(j < exfer->ex_nsqtd, "j=%zu nsqtd=%zu", j,
2969 exfer->ex_nsqtd);
2970 prev = sqtd;
2971 sqtd = exfer->ex_sqtds[j++];
2972 memset(&sqtd->qtd, 0, sizeof(sqtd->qtd));
2973 sqtd->qtd.qtd_next = sqtd->qtd.qtd_altnext = EHCI_NULL;
2974 sqtd->qtd.qtd_status = htole32(
2975 qtdstatus |
2976 EHCI_QTD_SET_BYTES(0) |
2977 EHCI_QTD_SET_TOGGLE(tog));
2978
2979 usb_syncmem(&sqtd->dma, sqtd->offs, sizeof(sqtd->qtd),
2980 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2981
2982 ehci_append_sqtd(sqtd, prev);
2983 tog ^= 1;
2984 }
2985
2986 *lsqtd = sqtd;
2987 *toggle = tog;
2988 }
2989
2990 Static ehci_soft_itd_t *
2991 ehci_alloc_itd(ehci_softc_t *sc)
2992 {
2993 struct ehci_soft_itd *itd, *freeitd;
2994 usbd_status err;
2995 usb_dma_t dma;
2996
2997 EHCIHIST_FUNC(); EHCIHIST_CALLED();
2998
2999 mutex_enter(&sc->sc_lock);
3000
3001 freeitd = LIST_FIRST(&sc->sc_freeitds);
3002 if (freeitd == NULL) {
3003 DPRINTF("allocating chunk", 0, 0, 0, 0);
3004 mutex_exit(&sc->sc_lock);
3005 err = usb_allocmem(&sc->sc_bus, EHCI_ITD_SIZE * EHCI_ITD_CHUNK,
3006 EHCI_PAGE_SIZE, &dma);
3007
3008 if (err) {
3009 DPRINTF("alloc returned %d", err, 0, 0, 0);
3010 return NULL;
3011 }
3012 mutex_enter(&sc->sc_lock);
3013
3014 for (int i = 0; i < EHCI_ITD_CHUNK; i++) {
3015 int offs = i * EHCI_ITD_SIZE;
3016 itd = KERNADDR(&dma, offs);
3017 itd->physaddr = DMAADDR(&dma, offs);
3018 itd->dma = dma;
3019 itd->offs = offs;
3020 LIST_INSERT_HEAD(&sc->sc_freeitds, itd, free_list);
3021 }
3022 freeitd = LIST_FIRST(&sc->sc_freeitds);
3023 }
3024
3025 itd = freeitd;
3026 LIST_REMOVE(itd, free_list);
3027 mutex_exit(&sc->sc_lock);
3028 memset(&itd->itd, 0, sizeof(ehci_itd_t));
3029
3030 itd->frame_list.next = NULL;
3031 itd->frame_list.prev = NULL;
3032 itd->xfer_next = NULL;
3033 itd->slot = 0;
3034
3035 return itd;
3036 }
3037
3038 Static ehci_soft_sitd_t *
3039 ehci_alloc_sitd(ehci_softc_t *sc)
3040 {
3041 struct ehci_soft_sitd *sitd, *freesitd;
3042 usbd_status err;
3043 int i, offs;
3044 usb_dma_t dma;
3045
3046 EHCIHIST_FUNC(); EHCIHIST_CALLED();
3047
3048 mutex_enter(&sc->sc_lock);
3049 freesitd = LIST_FIRST(&sc->sc_freesitds);
3050 if (freesitd == NULL) {
3051 DPRINTF("allocating chunk", 0, 0, 0, 0);
3052 mutex_exit(&sc->sc_lock);
3053 err = usb_allocmem(&sc->sc_bus, EHCI_SITD_SIZE * EHCI_SITD_CHUNK,
3054 EHCI_PAGE_SIZE, &dma);
3055
3056 if (err) {
3057 DPRINTF("alloc returned %d", err, 0, 0,
3058 0);
3059 return NULL;
3060 }
3061
3062 mutex_enter(&sc->sc_lock);
3063 for (i = 0; i < EHCI_SITD_CHUNK; i++) {
3064 offs = i * EHCI_SITD_SIZE;
3065 sitd = KERNADDR(&dma, offs);
3066 sitd->physaddr = DMAADDR(&dma, offs);
3067 sitd->dma = dma;
3068 sitd->offs = offs;
3069 LIST_INSERT_HEAD(&sc->sc_freesitds, sitd, free_list);
3070 }
3071 freesitd = LIST_FIRST(&sc->sc_freesitds);
3072 }
3073
3074 sitd = freesitd;
3075 LIST_REMOVE(sitd, free_list);
3076 mutex_exit(&sc->sc_lock);
3077
3078 memset(&sitd->sitd, 0, sizeof(ehci_sitd_t));
3079
3080 sitd->frame_list.next = NULL;
3081 sitd->frame_list.prev = NULL;
3082 sitd->xfer_next = NULL;
3083 sitd->slot = 0;
3084
3085 return sitd;
3086 }
3087
3088 /****************/
3089
3090 /*
3091 * Close a reqular pipe.
3092 * Assumes that there are no pending transactions.
3093 */
3094 Static void
3095 ehci_close_pipe(struct usbd_pipe *pipe, ehci_soft_qh_t *head)
3096 {
3097 struct ehci_pipe *epipe = EHCI_PIPE2EPIPE(pipe);
3098 ehci_softc_t *sc = EHCI_PIPE2SC(pipe);
3099 ehci_soft_qh_t *sqh = epipe->sqh;
3100
3101 KASSERT(mutex_owned(&sc->sc_lock));
3102
3103 ehci_rem_qh(sc, sqh, head);
3104 ehci_free_sqh(sc, epipe->sqh);
3105 }
3106
3107 /*
3108 * Cancel or timeout a device request. We have two cases to deal with
3109 *
3110 * 1) A driver wants to stop scheduled or inflight transfers
3111 * 2) A transfer has timed out
3112 *
3113 * have (partially) happened since the hardware runs concurrently.
3114 *
3115 * Transfer state is protected by the bus lock and we set the transfer status
3116 * as soon as either of the above happens (with bus lock held).
3117 *
3118 * Then we arrange for the hardware to tells us that it is not still
3119 * processing the TDs by setting the QH halted bit and wait for the ehci
3120 * door bell
3121 */
3122 Static void
3123 ehci_abort_xfer(struct usbd_xfer *xfer, usbd_status status)
3124 {
3125 struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
3126 struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
3127 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3128 ehci_soft_qh_t *sqh = epipe->sqh;
3129 ehci_soft_qtd_t *sqtd, *fsqtd, *lsqtd;
3130 ehci_physaddr_t cur;
3131 uint32_t qhstatus;
3132 int hit;
3133 int wake;
3134
3135 EHCIHIST_FUNC(); EHCIHIST_CALLED();
3136
3137 DPRINTF("xfer=%p pipe=%p", xfer, epipe, 0, 0);
3138
3139 KASSERT(mutex_owned(&sc->sc_lock));
3140 ASSERT_SLEEPABLE();
3141
3142 if (sc->sc_dying) {
3143 /* If we're dying, just do the software part. */
3144 xfer->ux_status = status;
3145 callout_halt(&xfer->ux_callout, &sc->sc_lock);
3146 KASSERT(xfer->ux_status == status);
3147 usb_transfer_complete(xfer);
3148 return;
3149 }
3150
3151 /*
3152 * If an abort is already in progress then just wait for it to
3153 * complete and return.
3154 */
3155 if (xfer->ux_hcflags & UXFER_ABORTING) {
3156 DPRINTF("already aborting", 0, 0, 0, 0);
3157 KASSERT(status != USBD_TIMEOUT);
3158
3159 /* Override the status which might be USBD_TIMEOUT. */
3160 xfer->ux_status = status;
3161 DPRINTF("waiting for abort to finish", 0, 0, 0, 0);
3162 xfer->ux_hcflags |= UXFER_ABORTWAIT;
3163 while (xfer->ux_hcflags & UXFER_ABORTING)
3164 cv_wait(&xfer->ux_hccv, &sc->sc_lock);
3165 return;
3166 }
3167 xfer->ux_hcflags |= UXFER_ABORTING;
3168
3169 /*
3170 * Step 1: When cancelling a transfer make sure the timeout handler
3171 * didn't run or ran to the end and saw the USBD_CANCELLED status.
3172 * Otherwise we must have got here via a timeout.
3173 */
3174 if (status == USBD_CANCELLED) {
3175 xfer->ux_status = status;
3176 callout_halt(&xfer->ux_callout, &sc->sc_lock);
3177 } else {
3178 KASSERT(xfer->ux_status == USBD_TIMEOUT);
3179 }
3180
3181 /*
3182 * Step 2: Make interrupt routine and hardware ignore xfer.
3183 */
3184 ehci_del_intr_list(sc, exfer);
3185
3186 usb_syncmem(&sqh->dma,
3187 sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
3188 sizeof(sqh->qh.qh_qtd.qtd_status),
3189 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
3190 qhstatus = sqh->qh.qh_qtd.qtd_status;
3191 if (exfer->ex_type == EX_CTRL) {
3192 fsqtd = exfer->ex_setup;
3193 lsqtd = exfer->ex_status;
3194 } else {
3195 fsqtd = exfer->ex_sqtdstart;
3196 lsqtd = exfer->ex_sqtdend;
3197 }
3198 if (!(qhstatus & htole32(EHCI_QTD_HALTED))) {
3199 sqh->qh.qh_qtd.qtd_status = qhstatus | htole32(EHCI_QTD_HALTED);
3200 usb_syncmem(&sqh->dma,
3201 sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
3202 sizeof(sqh->qh.qh_qtd.qtd_status),
3203 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3204
3205 for (sqtd = fsqtd; ; sqtd = sqtd->nextqtd) {
3206 usb_syncmem(&sqtd->dma,
3207 sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
3208 sizeof(sqtd->qtd.qtd_status),
3209 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
3210 sqtd->qtd.qtd_status |= htole32(EHCI_QTD_HALTED);
3211 usb_syncmem(&sqtd->dma,
3212 sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
3213 sizeof(sqtd->qtd.qtd_status),
3214 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3215 if (sqtd == lsqtd)
3216 break;
3217 }
3218 }
3219
3220 /*
3221 * Step 3: Wait until we know hardware has finished any possible
3222 * use of the xfer.
3223 */
3224 ehci_sync_hc(sc);
3225
3226 /*
3227 * Step 4: Remove any vestiges of the xfer from the hardware.
3228 * The complication here is that the hardware may have executed
3229 * beyond the xfer we're trying to abort. So as we're scanning
3230 * the TDs of this xfer we check if the hardware points to
3231 * any of them.
3232 */
3233
3234 usb_syncmem(&sqh->dma,
3235 sqh->offs + offsetof(ehci_qh_t, qh_curqtd),
3236 sizeof(sqh->qh.qh_curqtd),
3237 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
3238 cur = EHCI_LINK_ADDR(le32toh(sqh->qh.qh_curqtd));
3239 hit = 0;
3240 for (sqtd = fsqtd; ; sqtd = sqtd->nextqtd) {
3241 hit |= cur == sqtd->physaddr;
3242 if (sqtd == lsqtd)
3243 break;
3244 }
3245 sqtd = sqtd->nextqtd;
3246 /* Zap curqtd register if hardware pointed inside the xfer. */
3247 if (hit && sqtd != NULL) {
3248 DPRINTF("cur=0x%08x", sqtd->physaddr, 0, 0, 0);
3249 sqh->qh.qh_curqtd = htole32(sqtd->physaddr); /* unlink qTDs */
3250 usb_syncmem(&sqh->dma,
3251 sqh->offs + offsetof(ehci_qh_t, qh_curqtd),
3252 sizeof(sqh->qh.qh_curqtd),
3253 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3254 sqh->qh.qh_qtd.qtd_status = qhstatus;
3255 usb_syncmem(&sqh->dma,
3256 sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
3257 sizeof(sqh->qh.qh_qtd.qtd_status),
3258 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3259 } else {
3260 DPRINTF("no hit", 0, 0, 0, 0);
3261 usb_syncmem(&sqh->dma,
3262 sqh->offs + offsetof(ehci_qh_t, qh_curqtd),
3263 sizeof(sqh->qh.qh_curqtd),
3264 BUS_DMASYNC_PREREAD);
3265 }
3266
3267 /*
3268 * Step 5: Execute callback.
3269 */
3270 #ifdef DIAGNOSTIC
3271 exfer->ex_isdone = true;
3272 #endif
3273 wake = xfer->ux_hcflags & UXFER_ABORTWAIT;
3274 xfer->ux_hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT);
3275 usb_transfer_complete(xfer);
3276 if (wake) {
3277 cv_broadcast(&xfer->ux_hccv);
3278 }
3279
3280 KASSERT(mutex_owned(&sc->sc_lock));
3281 }
3282
3283 Static void
3284 ehci_abort_isoc_xfer(struct usbd_xfer *xfer, usbd_status status)
3285 {
3286 ehci_isoc_trans_t trans_status;
3287 struct ehci_xfer *exfer;
3288 ehci_softc_t *sc;
3289 struct ehci_soft_itd *itd;
3290 struct ehci_soft_sitd *sitd;
3291 int i, wake;
3292
3293 EHCIHIST_FUNC(); EHCIHIST_CALLED();
3294
3295 exfer = EHCI_XFER2EXFER(xfer);
3296 sc = EHCI_XFER2SC(xfer);
3297
3298 DPRINTF("xfer %p pipe %p", xfer, xfer->ux_pipe, 0, 0);
3299
3300 KASSERT(mutex_owned(&sc->sc_lock));
3301
3302 if (sc->sc_dying) {
3303 /* If we're dying, just do the software part. */
3304 xfer->ux_status = status;
3305 callout_halt(&xfer->ux_callout, &sc->sc_lock);
3306 KASSERT(xfer->ux_status == status);
3307 usb_transfer_complete(xfer);
3308 return;
3309 }
3310
3311 if (xfer->ux_hcflags & UXFER_ABORTING) {
3312 DPRINTF("already aborting", 0, 0, 0, 0);
3313
3314 KASSERT(status != USBD_TIMEOUT);
3315
3316 xfer->ux_status = status;
3317 DPRINTF("waiting for abort to finish", 0, 0, 0, 0);
3318 xfer->ux_hcflags |= UXFER_ABORTWAIT;
3319 while (xfer->ux_hcflags & UXFER_ABORTING)
3320 cv_wait(&xfer->ux_hccv, &sc->sc_lock);
3321 goto done;
3322 }
3323 xfer->ux_hcflags |= UXFER_ABORTING;
3324
3325 /*
3326 * Step 1: When cancelling a transfer make sure the timeout handler
3327 * didn't run or ran to the end and saw the USBD_CANCELLED status.
3328 * Otherwise we must have got here via a timeout.
3329 */
3330 if (status == USBD_CANCELLED) {
3331 xfer->ux_status = status;
3332 callout_halt(&xfer->ux_callout, &sc->sc_lock);
3333 } else {
3334 KASSERT(xfer->ux_status == USBD_TIMEOUT);
3335 }
3336
3337 /*
3338 * Step 2: Make interrupt routine and hardware ignore xfer.
3339 */
3340 ehci_del_intr_list(sc, exfer);
3341
3342 if (xfer->ux_pipe->up_dev->ud_speed == USB_SPEED_HIGH) {
3343 for (itd = exfer->ex_itdstart; itd != NULL;
3344 itd = itd->xfer_next) {
3345 usb_syncmem(&itd->dma,
3346 itd->offs + offsetof(ehci_itd_t, itd_ctl),
3347 sizeof(itd->itd.itd_ctl),
3348 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
3349
3350 for (i = 0; i < 8; i++) {
3351 trans_status = le32toh(itd->itd.itd_ctl[i]);
3352 trans_status &= ~EHCI_ITD_ACTIVE;
3353 itd->itd.itd_ctl[i] = htole32(trans_status);
3354 }
3355
3356 usb_syncmem(&itd->dma,
3357 itd->offs + offsetof(ehci_itd_t, itd_ctl),
3358 sizeof(itd->itd.itd_ctl),
3359 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3360 }
3361 } else {
3362 for (sitd = exfer->ex_sitdstart; sitd != NULL;
3363 sitd = sitd->xfer_next) {
3364 usb_syncmem(&sitd->dma,
3365 sitd->offs + offsetof(ehci_sitd_t, sitd_buffer),
3366 sizeof(sitd->sitd.sitd_buffer),
3367 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
3368
3369 trans_status = le32toh(sitd->sitd.sitd_trans);
3370 trans_status &= ~EHCI_SITD_ACTIVE;
3371 sitd->sitd.sitd_trans = htole32(trans_status);
3372
3373 usb_syncmem(&sitd->dma,
3374 sitd->offs + offsetof(ehci_sitd_t, sitd_buffer),
3375 sizeof(sitd->sitd.sitd_buffer),
3376 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3377 }
3378 }
3379
3380 #ifdef DIAGNOSTIC
3381 exfer->ex_isdone = true;
3382 #endif
3383 wake = xfer->ux_hcflags & UXFER_ABORTWAIT;
3384 xfer->ux_hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT);
3385 usb_transfer_complete(xfer);
3386 if (wake) {
3387 cv_broadcast(&xfer->ux_hccv);
3388 }
3389
3390 done:
3391 KASSERT(mutex_owned(&sc->sc_lock));
3392 return;
3393 }
3394
3395 Static void
3396 ehci_timeout(void *addr)
3397 {
3398 EHCIHIST_FUNC(); EHCIHIST_CALLED();
3399 struct usbd_xfer *xfer = addr;
3400 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3401 bool timeout = false;
3402
3403 DPRINTF("xfer %p", xfer, 0, 0, 0);
3404 #ifdef EHCI_DEBUG
3405 if (ehcidebug >= 2) {
3406 struct usbd_pipe *pipe = xfer->ux_pipe;
3407 usbd_dump_pipe(pipe);
3408 }
3409 #endif
3410
3411 mutex_enter(&sc->sc_lock);
3412 if (sc->sc_dying) {
3413 mutex_exit(&sc->sc_lock);
3414 return;
3415 }
3416 if (xfer->ux_status != USBD_CANCELLED) {
3417 xfer->ux_status = USBD_TIMEOUT;
3418 timeout = true;
3419 }
3420 mutex_exit(&sc->sc_lock);
3421
3422 if (timeout) {
3423 struct usbd_device *dev = xfer->ux_pipe->up_dev;
3424
3425 /* Execute the abort in a process context. */
3426 usb_init_task(&xfer->ux_aborttask, ehci_timeout_task, xfer,
3427 USB_TASKQ_MPSAFE);
3428 usb_add_task(dev, &xfer->ux_aborttask, USB_TASKQ_HC);
3429 }
3430 }
3431
3432 Static void
3433 ehci_timeout_task(void *addr)
3434 {
3435 struct usbd_xfer *xfer = addr;
3436 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3437
3438 EHCIHIST_FUNC(); EHCIHIST_CALLED();
3439
3440 DPRINTF("xfer=%p", xfer, 0, 0, 0);
3441
3442 mutex_enter(&sc->sc_lock);
3443 KASSERT(xfer->ux_status == USBD_TIMEOUT);
3444 ehci_abort_xfer(xfer, USBD_TIMEOUT);
3445 mutex_exit(&sc->sc_lock);
3446 }
3447
3448 /************************/
3449
3450 Static int
3451 ehci_device_ctrl_init(struct usbd_xfer *xfer)
3452 {
3453 struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
3454 struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
3455 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3456 usb_device_request_t *req = &xfer->ux_request;
3457 ehci_soft_qtd_t *setup, *status, *next;
3458 int isread = req->bmRequestType & UT_READ;
3459 int len = xfer->ux_bufsize;
3460 int err;
3461
3462 exfer->ex_type = EX_CTRL;
3463 exfer->ex_status = NULL;
3464 exfer->ex_data = NULL;
3465 exfer->ex_setup = ehci_alloc_sqtd(sc);
3466 if (exfer->ex_setup == NULL) {
3467 err = ENOMEM;
3468 goto bad1;
3469 }
3470 exfer->ex_status = ehci_alloc_sqtd(sc);
3471 if (exfer->ex_status == NULL) {
3472 err = ENOMEM;
3473 goto bad2;
3474 }
3475 setup = exfer->ex_setup;
3476 status = exfer->ex_status;
3477 exfer->ex_nsqtd = 0;
3478 next = status;
3479 /* Set up data transaction */
3480 if (len != 0) {
3481 err = ehci_alloc_sqtd_chain(sc, xfer, len, isread,
3482 &exfer->ex_data);
3483 if (err)
3484 goto bad3;
3485 next = exfer->ex_data;
3486 }
3487
3488 /* Clear toggle */
3489 setup->qtd.qtd_status = htole32(
3490 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
3491 EHCI_QTD_SET_TOGGLE(0) |
3492 EHCI_QTD_SET_BYTES(sizeof(*req))
3493 );
3494 setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->ctrl.reqdma, 0));
3495 setup->qtd.qtd_buffer_hi[0] = 0;
3496 setup->qtd.qtd_next = setup->qtd.qtd_altnext = htole32(next->physaddr);
3497 setup->nextqtd = next;
3498 setup->xfer = xfer;
3499 setup->len = sizeof(*req);
3500
3501 status->qtd.qtd_status = htole32(
3502 EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) |
3503 EHCI_QTD_SET_TOGGLE(1) |
3504 EHCI_QTD_IOC
3505 );
3506 status->qtd.qtd_buffer[0] = 0;
3507 status->qtd.qtd_buffer_hi[0] = 0;
3508 status->qtd.qtd_next = status->qtd.qtd_altnext = EHCI_NULL;
3509 status->nextqtd = NULL;
3510 status->xfer = xfer;
3511 status->len = 0;
3512
3513 return 0;
3514 bad3:
3515 ehci_free_sqtd(sc, exfer->ex_status);
3516 bad2:
3517 ehci_free_sqtd(sc, exfer->ex_setup);
3518 bad1:
3519 return err;
3520 }
3521
3522 Static void
3523 ehci_device_ctrl_fini(struct usbd_xfer *xfer)
3524 {
3525 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3526 struct ehci_xfer *ex = EHCI_XFER2EXFER(xfer);
3527
3528 KASSERT(ex->ex_type == EX_CTRL);
3529
3530 ehci_free_sqtd(sc, ex->ex_setup);
3531 ehci_free_sqtd(sc, ex->ex_status);
3532 ehci_free_sqtds(sc, ex);
3533 if (ex->ex_nsqtd)
3534 kmem_free(ex->ex_sqtds,
3535 sizeof(ehci_soft_qtd_t *) * ex->ex_nsqtd);
3536 }
3537
3538 Static usbd_status
3539 ehci_device_ctrl_transfer(struct usbd_xfer *xfer)
3540 {
3541 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3542 usbd_status err;
3543
3544 /* Insert last in queue. */
3545 mutex_enter(&sc->sc_lock);
3546 err = usb_insert_transfer(xfer);
3547 mutex_exit(&sc->sc_lock);
3548 if (err)
3549 return err;
3550
3551 /* Pipe isn't running, start first */
3552 return ehci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
3553 }
3554
3555 Static usbd_status
3556 ehci_device_ctrl_start(struct usbd_xfer *xfer)
3557 {
3558 struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
3559 struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
3560 usb_device_request_t *req = &xfer->ux_request;
3561 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3562 ehci_soft_qtd_t *setup, *status, *next;
3563 ehci_soft_qh_t *sqh;
3564
3565 EHCIHIST_FUNC(); EHCIHIST_CALLED();
3566
3567 KASSERT(xfer->ux_rqflags & URQ_REQUEST);
3568
3569 if (sc->sc_dying)
3570 return USBD_IOERROR;
3571
3572 const int isread = req->bmRequestType & UT_READ;
3573 const int len = UGETW(req->wLength);
3574
3575 DPRINTF("type=0x%02x, request=0x%02x, wValue=0x%04x, wIndex=0x%04x",
3576 req->bmRequestType, req->bRequest, UGETW(req->wValue),
3577 UGETW(req->wIndex));
3578 DPRINTF("len=%d, addr=%d, endpt=%d", len, epipe->pipe.up_dev->ud_addr,
3579 epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress, 0);
3580
3581 sqh = epipe->sqh;
3582
3583 KASSERTMSG(EHCI_QH_GET_ADDR(le32toh(sqh->qh.qh_endp)) == epipe->pipe.up_dev->ud_addr,
3584 "address QH %" __PRIuBIT " pipe %d\n",
3585 EHCI_QH_GET_ADDR(le32toh(sqh->qh.qh_endp)),
3586 epipe->pipe.up_dev->ud_addr);
3587 KASSERTMSG(EHCI_QH_GET_MPL(le32toh(sqh->qh.qh_endp)) ==
3588 UGETW(epipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize),
3589 "MPS QH %" __PRIuBIT " pipe %d\n",
3590 EHCI_QH_GET_MPL(le32toh(sqh->qh.qh_endp)),
3591 UGETW(epipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize));
3592
3593 setup = exfer->ex_setup;
3594 status = exfer->ex_status;
3595
3596 DPRINTF("setup %p status %p data %p", setup, status, exfer->ex_data, 0);
3597 KASSERTMSG(setup != NULL && status != NULL,
3598 "Failed memory allocation, setup %p status %p",
3599 setup, status);
3600
3601 memcpy(KERNADDR(&epipe->ctrl.reqdma, 0), req, sizeof(*req));
3602 usb_syncmem(&epipe->ctrl.reqdma, 0, sizeof(*req), BUS_DMASYNC_PREWRITE);
3603
3604 /* Clear toggle */
3605 setup->qtd.qtd_status &= ~htole32(
3606 EHCI_QTD_STATUS_MASK |
3607 EHCI_QTD_BYTES_MASK |
3608 EHCI_QTD_TOGGLE_MASK |
3609 EHCI_QTD_CERR_MASK
3610 );
3611 setup->qtd.qtd_status |= htole32(
3612 EHCI_QTD_ACTIVE |
3613 EHCI_QTD_SET_CERR(3) |
3614 EHCI_QTD_SET_TOGGLE(0) |
3615 EHCI_QTD_SET_BYTES(sizeof(*req))
3616 );
3617 setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->ctrl.reqdma, 0));
3618 setup->qtd.qtd_buffer_hi[0] = 0;
3619
3620 next = status;
3621 status->qtd.qtd_status &= ~htole32(
3622 EHCI_QTD_STATUS_MASK |
3623 EHCI_QTD_PID_MASK |
3624 EHCI_QTD_BYTES_MASK |
3625 EHCI_QTD_TOGGLE_MASK |
3626 EHCI_QTD_CERR_MASK
3627 );
3628 status->qtd.qtd_status |= htole32(
3629 EHCI_QTD_ACTIVE |
3630 EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) |
3631 EHCI_QTD_SET_CERR(3) |
3632 EHCI_QTD_SET_TOGGLE(1) |
3633 EHCI_QTD_SET_BYTES(0) |
3634 EHCI_QTD_IOC
3635 );
3636 KASSERT(status->qtd.qtd_status & htole32(EHCI_QTD_TOGGLE_MASK));
3637
3638 KASSERT(exfer->ex_isdone);
3639 #ifdef DIAGNOSTIC
3640 exfer->ex_isdone = false;
3641 #endif
3642
3643 /* Set up data transaction */
3644 if (len != 0) {
3645 ehci_soft_qtd_t *end;
3646
3647 /* Start toggle at 1. */
3648 int toggle = 1;
3649 next = exfer->ex_data;
3650 KASSERTMSG(next != NULL, "Failed memory allocation");
3651 ehci_reset_sqtd_chain(sc, xfer, len, isread, &toggle, &end);
3652 end->nextqtd = status;
3653 end->qtd.qtd_next = end->qtd.qtd_altnext =
3654 htole32(status->physaddr);
3655
3656 usb_syncmem(&end->dma, end->offs, sizeof(end->qtd),
3657 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3658
3659 usb_syncmem(&xfer->ux_dmabuf, 0, len,
3660 isread ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
3661 }
3662
3663 setup->nextqtd = next;
3664 setup->qtd.qtd_next = setup->qtd.qtd_altnext = htole32(next->physaddr);
3665
3666 usb_syncmem(&setup->dma, setup->offs, sizeof(setup->qtd),
3667 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3668
3669 usb_syncmem(&status->dma, status->offs, sizeof(status->qtd),
3670 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3671
3672 KASSERT(status->qtd.qtd_status & htole32(EHCI_QTD_TOGGLE_MASK));
3673
3674 #ifdef EHCI_DEBUG
3675 DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
3676 ehci_dump_sqh(sqh);
3677 ehci_dump_sqtds(setup);
3678 DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
3679 #endif
3680
3681 mutex_enter(&sc->sc_lock);
3682
3683 /* Insert qTD in QH list - also does usb_syncmem(sqh) */
3684 ehci_set_qh_qtd(sqh, setup);
3685 if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
3686 callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
3687 ehci_timeout, xfer);
3688 }
3689 ehci_add_intr_list(sc, exfer);
3690 xfer->ux_status = USBD_IN_PROGRESS;
3691 mutex_exit(&sc->sc_lock);
3692
3693 #if 0
3694 #ifdef EHCI_DEBUG
3695 DPRINTFN(10, "status=%x, dump:", EOREAD4(sc, EHCI_USBSTS), 0, 0, 0);
3696 // delay(10000);
3697 ehci_dump_regs(sc);
3698 ehci_dump_sqh(sc->sc_async_head);
3699 ehci_dump_sqh(sqh);
3700 ehci_dump_sqtds(setup);
3701 #endif
3702 #endif
3703
3704 return USBD_IN_PROGRESS;
3705 }
3706
3707 Static void
3708 ehci_device_ctrl_done(struct usbd_xfer *xfer)
3709 {
3710 ehci_softc_t *sc __diagused = EHCI_XFER2SC(xfer);
3711 struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
3712 usb_device_request_t *req = &xfer->ux_request;
3713 int len = UGETW(req->wLength);
3714 int rd = req->bmRequestType & UT_READ;
3715
3716 EHCIHIST_FUNC(); EHCIHIST_CALLED();
3717 DPRINTF("xfer=%p", xfer, 0, 0, 0);
3718
3719 KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
3720 KASSERT(xfer->ux_rqflags & URQ_REQUEST);
3721
3722 usb_syncmem(&epipe->ctrl.reqdma, 0, sizeof(*req),
3723 BUS_DMASYNC_POSTWRITE);
3724 if (len)
3725 usb_syncmem(&xfer->ux_dmabuf, 0, len,
3726 rd ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
3727
3728 DPRINTF("length=%d", xfer->ux_actlen, 0, 0, 0);
3729 }
3730
3731 /* Abort a device control request. */
3732 Static void
3733 ehci_device_ctrl_abort(struct usbd_xfer *xfer)
3734 {
3735 EHCIHIST_FUNC(); EHCIHIST_CALLED();
3736
3737 DPRINTF("xfer=%p", xfer, 0, 0, 0);
3738 ehci_abort_xfer(xfer, USBD_CANCELLED);
3739 }
3740
3741 /* Close a device control pipe. */
3742 Static void
3743 ehci_device_ctrl_close(struct usbd_pipe *pipe)
3744 {
3745 ehci_softc_t *sc = EHCI_PIPE2SC(pipe);
3746 /*struct ehci_pipe *epipe = EHCI_PIPE2EPIPE(pipe);*/
3747
3748 EHCIHIST_FUNC(); EHCIHIST_CALLED();
3749
3750 KASSERT(mutex_owned(&sc->sc_lock));
3751
3752 DPRINTF("pipe=%p", pipe, 0, 0, 0);
3753
3754 ehci_close_pipe(pipe, sc->sc_async_head);
3755 }
3756
3757 /*
3758 * Some EHCI chips from VIA seem to trigger interrupts before writing back the
3759 * qTD status, or miss signalling occasionally under heavy load. If the host
3760 * machine is too fast, we we can miss transaction completion - when we scan
3761 * the active list the transaction still seems to be active. This generally
3762 * exhibits itself as a umass stall that never recovers.
3763 *
3764 * We work around this behaviour by setting up this callback after any softintr
3765 * that completes with transactions still pending, giving us another chance to
3766 * check for completion after the writeback has taken place.
3767 */
3768 Static void
3769 ehci_intrlist_timeout(void *arg)
3770 {
3771 ehci_softc_t *sc = arg;
3772
3773 EHCIHIST_FUNC(); EHCIHIST_CALLED();
3774
3775 usb_schedsoftintr(&sc->sc_bus);
3776 }
3777
3778 /************************/
3779
3780 Static int
3781 ehci_device_bulk_init(struct usbd_xfer *xfer)
3782 {
3783 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3784 struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
3785 usb_endpoint_descriptor_t *ed = xfer->ux_pipe->up_endpoint->ue_edesc;
3786 int endpt = ed->bEndpointAddress;
3787 int isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3788 int len = xfer->ux_bufsize;
3789 int err = 0;
3790
3791 exfer->ex_type = EX_BULK;
3792 exfer->ex_nsqtd = 0;
3793 err = ehci_alloc_sqtd_chain(sc, xfer, len, isread,
3794 &exfer->ex_sqtdstart);
3795
3796 return err;
3797 }
3798
3799 Static void
3800 ehci_device_bulk_fini(struct usbd_xfer *xfer)
3801 {
3802 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3803 struct ehci_xfer *ex = EHCI_XFER2EXFER(xfer);
3804
3805 KASSERT(ex->ex_type == EX_BULK);
3806
3807 ehci_free_sqtds(sc, ex);
3808 if (ex->ex_nsqtd)
3809 kmem_free(ex->ex_sqtds, sizeof(ehci_soft_qtd_t *) * ex->ex_nsqtd);
3810 }
3811
3812 Static usbd_status
3813 ehci_device_bulk_transfer(struct usbd_xfer *xfer)
3814 {
3815 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3816 usbd_status err;
3817
3818 /* Insert last in queue. */
3819 mutex_enter(&sc->sc_lock);
3820 err = usb_insert_transfer(xfer);
3821 mutex_exit(&sc->sc_lock);
3822 if (err)
3823 return err;
3824
3825 /* Pipe isn't running, start first */
3826 return ehci_device_bulk_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
3827 }
3828
3829 Static usbd_status
3830 ehci_device_bulk_start(struct usbd_xfer *xfer)
3831 {
3832 struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
3833 struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
3834 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3835 ehci_soft_qh_t *sqh;
3836 ehci_soft_qtd_t *end;
3837 int len, isread, endpt;
3838
3839 EHCIHIST_FUNC(); EHCIHIST_CALLED();
3840
3841 DPRINTF("xfer=%p len=%d flags=%d", xfer, xfer->ux_length,
3842 xfer->ux_flags, 0);
3843
3844 if (sc->sc_dying)
3845 return USBD_IOERROR;
3846
3847 KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
3848 KASSERT(xfer->ux_length <= xfer->ux_bufsize);
3849
3850 len = xfer->ux_length;
3851 endpt = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
3852 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3853 sqh = epipe->sqh;
3854
3855 KASSERT(exfer->ex_isdone);
3856 #ifdef DIAGNOSTIC
3857 exfer->ex_isdone = false;
3858 #endif
3859
3860 /* Take lock here to protect nexttoggle */
3861 mutex_enter(&sc->sc_lock);
3862
3863 ehci_reset_sqtd_chain(sc, xfer, len, isread, &epipe->nexttoggle, &end);
3864
3865 exfer->ex_sqtdend = end;
3866 end->qtd.qtd_status |= htole32(EHCI_QTD_IOC);
3867 usb_syncmem(&end->dma, end->offs, sizeof(end->qtd),
3868 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3869
3870 #ifdef EHCI_DEBUG
3871 DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
3872 ehci_dump_sqh(sqh);
3873 ehci_dump_sqtds(exfer->ex_sqtdstart);
3874 DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
3875 #endif
3876
3877 usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
3878 isread ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
3879
3880 /* also does usb_syncmem(sqh) */
3881 ehci_set_qh_qtd(sqh, exfer->ex_sqtdstart);
3882 if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
3883 callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
3884 ehci_timeout, xfer);
3885 }
3886 ehci_add_intr_list(sc, exfer);
3887 xfer->ux_status = USBD_IN_PROGRESS;
3888 mutex_exit(&sc->sc_lock);
3889
3890 #if 0
3891 #ifdef EHCI_DEBUG
3892 DPRINTFN(5, "data(2)", 0, 0, 0, 0);
3893 // delay(10000);
3894 DPRINTFN(5, "data(3)", 0, 0, 0, 0);
3895 ehci_dump_regs(sc);
3896 #if 0
3897 printf("async_head:\n");
3898 ehci_dump_sqh(sc->sc_async_head);
3899 #endif
3900 DPRINTF("sqh:", 0, 0, 0, 0);
3901 ehci_dump_sqh(sqh);
3902 ehci_dump_sqtds(exfer->ex_sqtdstart);
3903 #endif
3904 #endif
3905
3906 return USBD_IN_PROGRESS;
3907 }
3908
3909 Static void
3910 ehci_device_bulk_abort(struct usbd_xfer *xfer)
3911 {
3912 EHCIHIST_FUNC(); EHCIHIST_CALLED();
3913
3914 DPRINTF("xfer %p", xfer, 0, 0, 0);
3915 ehci_abort_xfer(xfer, USBD_CANCELLED);
3916 }
3917
3918 /*
3919 * Close a device bulk pipe.
3920 */
3921 Static void
3922 ehci_device_bulk_close(struct usbd_pipe *pipe)
3923 {
3924 ehci_softc_t *sc = EHCI_PIPE2SC(pipe);
3925 struct ehci_pipe *epipe = EHCI_PIPE2EPIPE(pipe);
3926
3927 EHCIHIST_FUNC(); EHCIHIST_CALLED();
3928
3929 KASSERT(mutex_owned(&sc->sc_lock));
3930
3931 DPRINTF("pipe=%p", pipe, 0, 0, 0);
3932 pipe->up_endpoint->ue_toggle = epipe->nexttoggle;
3933 ehci_close_pipe(pipe, sc->sc_async_head);
3934 }
3935
3936 Static void
3937 ehci_device_bulk_done(struct usbd_xfer *xfer)
3938 {
3939 ehci_softc_t *sc __diagused = EHCI_XFER2SC(xfer);
3940 struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
3941 int endpt = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
3942 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
3943
3944 EHCIHIST_FUNC(); EHCIHIST_CALLED();
3945
3946 DPRINTF("xfer=%p, actlen=%d", xfer, xfer->ux_actlen, 0, 0);
3947
3948 KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
3949
3950 usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
3951 rd ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
3952
3953 DPRINTF("length=%d", xfer->ux_actlen, 0, 0, 0);
3954 }
3955
3956 /************************/
3957
3958 Static usbd_status
3959 ehci_device_setintr(ehci_softc_t *sc, ehci_soft_qh_t *sqh, int ival)
3960 {
3961 struct ehci_soft_islot *isp;
3962 int islot, lev;
3963
3964 /* Find a poll rate that is large enough. */
3965 for (lev = EHCI_IPOLLRATES - 1; lev > 0; lev--)
3966 if (EHCI_ILEV_IVAL(lev) <= ival)
3967 break;
3968
3969 /* Pick an interrupt slot at the right level. */
3970 /* XXX could do better than picking at random */
3971 sc->sc_rand = (sc->sc_rand + 191) % sc->sc_flsize;
3972 islot = EHCI_IQHIDX(lev, sc->sc_rand);
3973
3974 sqh->islot = islot;
3975 isp = &sc->sc_islots[islot];
3976 mutex_enter(&sc->sc_lock);
3977 ehci_add_qh(sc, sqh, isp->sqh);
3978 mutex_exit(&sc->sc_lock);
3979
3980 return USBD_NORMAL_COMPLETION;
3981 }
3982
3983
3984 Static int
3985 ehci_device_intr_init(struct usbd_xfer *xfer)
3986 {
3987 struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
3988 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3989 usb_endpoint_descriptor_t *ed = xfer->ux_pipe->up_endpoint->ue_edesc;
3990 int endpt = ed->bEndpointAddress;
3991 int isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3992 int len = xfer->ux_bufsize;
3993 int err;
3994
3995 EHCIHIST_FUNC(); EHCIHIST_CALLED();
3996
3997 DPRINTF("xfer=%p len=%d flags=%d", xfer, xfer->ux_length,
3998 xfer->ux_flags, 0);
3999
4000 KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
4001 KASSERT(len != 0);
4002
4003 exfer->ex_type = EX_INTR;
4004 exfer->ex_nsqtd = 0;
4005 err = ehci_alloc_sqtd_chain(sc, xfer, len, isread,
4006 &exfer->ex_sqtdstart);
4007
4008 return err;
4009 }
4010
4011 Static void
4012 ehci_device_intr_fini(struct usbd_xfer *xfer)
4013 {
4014 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4015 struct ehci_xfer *ex = EHCI_XFER2EXFER(xfer);
4016
4017 KASSERT(ex->ex_type == EX_INTR);
4018
4019 ehci_free_sqtds(sc, ex);
4020 if (ex->ex_nsqtd)
4021 kmem_free(ex->ex_sqtds, sizeof(ehci_soft_qtd_t *) * ex->ex_nsqtd);
4022 }
4023
4024 Static usbd_status
4025 ehci_device_intr_transfer(struct usbd_xfer *xfer)
4026 {
4027 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4028 usbd_status err;
4029
4030 /* Insert last in queue. */
4031 mutex_enter(&sc->sc_lock);
4032 err = usb_insert_transfer(xfer);
4033 mutex_exit(&sc->sc_lock);
4034 if (err)
4035 return err;
4036
4037 /*
4038 * Pipe isn't running (otherwise err would be USBD_INPROG),
4039 * so start it first.
4040 */
4041 return ehci_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
4042 }
4043
4044 Static usbd_status
4045 ehci_device_intr_start(struct usbd_xfer *xfer)
4046 {
4047 struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
4048 struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
4049 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4050 ehci_soft_qtd_t *end;
4051 ehci_soft_qh_t *sqh;
4052 int len, isread, endpt;
4053
4054 EHCIHIST_FUNC(); EHCIHIST_CALLED();
4055
4056 DPRINTF("xfer=%p len=%d flags=%d", xfer, xfer->ux_length,
4057 xfer->ux_flags, 0);
4058
4059 if (sc->sc_dying)
4060 return USBD_IOERROR;
4061
4062 KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
4063 KASSERT(xfer->ux_length <= xfer->ux_bufsize);
4064
4065 len = xfer->ux_length;
4066 endpt = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
4067 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
4068 sqh = epipe->sqh;
4069
4070 KASSERT(exfer->ex_isdone);
4071 #ifdef DIAGNOSTIC
4072 exfer->ex_isdone = false;
4073 #endif
4074
4075 /* Take lock to protect nexttoggle */
4076 mutex_enter(&sc->sc_lock);
4077
4078 ehci_reset_sqtd_chain(sc, xfer, len, isread, &epipe->nexttoggle, &end);
4079
4080 end->qtd.qtd_status |= htole32(EHCI_QTD_IOC);
4081 usb_syncmem(&end->dma, end->offs, sizeof(end->qtd),
4082 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4083 exfer->ex_sqtdend = end;
4084
4085 #ifdef EHCI_DEBUG
4086 DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
4087 ehci_dump_sqh(sqh);
4088 ehci_dump_sqtds(exfer->ex_sqtdstart);
4089 DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
4090 #endif
4091
4092 usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
4093 isread ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
4094
4095 /* also does usb_syncmem(sqh) */
4096 ehci_set_qh_qtd(sqh, exfer->ex_sqtdstart);
4097 if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
4098 callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
4099 ehci_timeout, xfer);
4100 }
4101 ehci_add_intr_list(sc, exfer);
4102 xfer->ux_status = USBD_IN_PROGRESS;
4103 mutex_exit(&sc->sc_lock);
4104
4105 #if 0
4106 #ifdef EHCI_DEBUG
4107 DPRINTFN(5, "data(2)", 0, 0, 0, 0);
4108 // delay(10000);
4109 DPRINTFN(5, "data(3)", 0, 0, 0, 0);
4110 ehci_dump_regs(sc);
4111 DPRINTFN(5, "sqh:", 0, 0, 0, 0);
4112 ehci_dump_sqh(sqh);
4113 ehci_dump_sqtds(exfer->ex_sqtdstart);
4114 #endif
4115 #endif
4116
4117 return USBD_IN_PROGRESS;
4118 }
4119
4120 Static void
4121 ehci_device_intr_abort(struct usbd_xfer *xfer)
4122 {
4123 EHCIHIST_FUNC(); EHCIHIST_CALLED();
4124
4125 DPRINTF("xfer=%p", xfer, 0, 0, 0);
4126 KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
4127
4128 /*
4129 * XXX - abort_xfer uses ehci_sync_hc, which syncs via the advance
4130 * async doorbell. That's dependent on the async list, wheras
4131 * intr xfers are periodic, should not use this?
4132 */
4133 ehci_abort_xfer(xfer, USBD_CANCELLED);
4134 }
4135
4136 Static void
4137 ehci_device_intr_close(struct usbd_pipe *pipe)
4138 {
4139 ehci_softc_t *sc = EHCI_PIPE2SC(pipe);
4140 struct ehci_pipe *epipe = EHCI_PIPE2EPIPE(pipe);
4141 struct ehci_soft_islot *isp;
4142
4143 KASSERT(mutex_owned(&sc->sc_lock));
4144
4145 isp = &sc->sc_islots[epipe->sqh->islot];
4146 ehci_close_pipe(pipe, isp->sqh);
4147 }
4148
4149 Static void
4150 ehci_device_intr_done(struct usbd_xfer *xfer)
4151 {
4152 ehci_softc_t *sc __diagused = EHCI_XFER2SC(xfer);
4153 struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
4154 int isread, endpt;
4155
4156 EHCIHIST_FUNC(); EHCIHIST_CALLED();
4157
4158 DPRINTF("xfer=%p, actlen=%d", xfer, xfer->ux_actlen,
4159 0, 0);
4160
4161 KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
4162
4163 endpt = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
4164 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
4165 usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
4166 isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
4167 }
4168
4169 /************************/
4170 Static int
4171 ehci_device_fs_isoc_init(struct usbd_xfer *xfer)
4172 {
4173 struct ehci_pipe *epipe = EHCI_PIPE2EPIPE(xfer->ux_pipe);
4174 struct usbd_device *dev = xfer->ux_pipe->up_dev;
4175 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4176 struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
4177 ehci_soft_sitd_t *sitd, *prev, *start, *stop;
4178 int i, k, frames;
4179 u_int huba, dir;
4180 int err;
4181
4182 EHCIHIST_FUNC(); EHCIHIST_CALLED();
4183
4184 start = NULL;
4185 sitd = NULL;
4186
4187 DPRINTF("xfer %p len %d flags %d", xfer, xfer->ux_length,
4188 xfer->ux_flags, 0);
4189
4190 KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
4191 KASSERT(xfer->ux_nframes != 0);
4192 KASSERT(exfer->ex_isdone);
4193
4194 exfer->ex_type = EX_FS_ISOC;
4195 /*
4196 * Step 1: Allocate and initialize sitds.
4197 */
4198 i = epipe->pipe.up_endpoint->ue_edesc->bInterval;
4199 if (i > 16 || i == 0) {
4200 /* Spec page 271 says intervals > 16 are invalid */
4201 DPRINTF("bInterval %d invalid", i, 0, 0, 0);
4202
4203 return EINVAL;
4204 }
4205
4206 frames = xfer->ux_nframes;
4207 for (i = 0, prev = NULL; i < frames; i++, prev = sitd) {
4208 sitd = ehci_alloc_sitd(sc);
4209 if (sitd == NULL) {
4210 err = ENOMEM;
4211 goto fail;
4212 }
4213
4214 if (prev)
4215 prev->xfer_next = sitd;
4216 else
4217 start = sitd;
4218
4219 huba = dev->ud_myhsport->up_parent->ud_addr;
4220
4221 #if 0
4222 if (sc->sc_flags & EHCIF_FREESCALE) {
4223 // Set hub address to 0 if embedded TT is used.
4224 if (huba == sc->sc_addr)
4225 huba = 0;
4226 }
4227 #endif
4228
4229 k = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
4230 dir = UE_GET_DIR(k) ? 1 : 0;
4231 sitd->sitd.sitd_endp =
4232 htole32(EHCI_SITD_SET_ENDPT(UE_GET_ADDR(k)) |
4233 EHCI_SITD_SET_DADDR(dev->ud_addr) |
4234 EHCI_SITD_SET_PORT(dev->ud_myhsport->up_portno) |
4235 EHCI_SITD_SET_HUBA(huba) |
4236 EHCI_SITD_SET_DIR(dir));
4237
4238 sitd->sitd.sitd_back = htole32(EHCI_LINK_TERMINATE);
4239 } /* End of frame */
4240
4241 sitd->sitd.sitd_trans |= htole32(EHCI_SITD_IOC);
4242
4243 stop = sitd;
4244 stop->xfer_next = NULL;
4245 exfer->ex_sitdstart = start;
4246 exfer->ex_sitdend = stop;
4247
4248 return 0;
4249
4250 fail:
4251 mutex_enter(&sc->sc_lock);
4252 ehci_soft_sitd_t *next;
4253 for (sitd = start; sitd; sitd = next) {
4254 next = sitd->xfer_next;
4255 ehci_free_sitd_locked(sc, sitd);
4256 }
4257 mutex_exit(&sc->sc_lock);
4258
4259 return err;
4260 }
4261
4262 Static void
4263 ehci_device_fs_isoc_fini(struct usbd_xfer *xfer)
4264 {
4265 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4266 struct ehci_xfer *ex = EHCI_XFER2EXFER(xfer);
4267
4268 KASSERT(ex->ex_type == EX_FS_ISOC);
4269
4270 ehci_free_sitd_chain(sc, ex->ex_sitdstart);
4271 }
4272
4273 Static usbd_status
4274 ehci_device_fs_isoc_transfer(struct usbd_xfer *xfer)
4275 {
4276 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4277 usbd_status __diagused err;
4278
4279 mutex_enter(&sc->sc_lock);
4280 err = usb_insert_transfer(xfer);
4281 mutex_exit(&sc->sc_lock);
4282
4283 KASSERT(err == USBD_NORMAL_COMPLETION);
4284
4285 struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);;
4286 struct usbd_device *dev = xfer->ux_pipe->up_dev;;
4287 struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
4288 ehci_soft_sitd_t *sitd;
4289 usb_dma_t *dma_buf;
4290 int i, j, k, frames;
4291 int offs, total_length;
4292 int frindex;
4293 u_int dir;
4294
4295 EHCIHIST_FUNC(); EHCIHIST_CALLED();
4296
4297 sitd = NULL;
4298 total_length = 0;
4299
4300
4301 DPRINTF("xfer %p len %d flags %d", xfer, xfer->ux_length,
4302 xfer->ux_flags, 0);
4303
4304 if (sc->sc_dying)
4305 return USBD_IOERROR;
4306
4307 /*
4308 * To avoid complication, don't allow a request right now that'll span
4309 * the entire frame table. To within 4 frames, to allow some leeway
4310 * on either side of where the hc currently is.
4311 */
4312 if (epipe->pipe.up_endpoint->ue_edesc->bInterval *
4313 xfer->ux_nframes >= sc->sc_flsize - 4) {
4314 printf("ehci: isoc descriptor requested that spans the entire"
4315 "frametable, too many frames\n");
4316 return USBD_INVAL;
4317 }
4318
4319 KASSERT(xfer->ux_nframes != 0 && xfer->ux_frlengths);
4320 KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
4321 KASSERT(exfer->ex_isdone);
4322 #ifdef DIAGNOSTIC
4323 exfer->ex_isdone = false;
4324 #endif
4325
4326 /*
4327 * Step 1: Initialize sitds.
4328 */
4329
4330 frames = xfer->ux_nframes;
4331 dma_buf = &xfer->ux_dmabuf;
4332 offs = 0;
4333
4334 for (sitd = exfer->ex_sitdstart, i = 0; i < frames;
4335 i++, sitd = sitd->xfer_next) {
4336 KASSERT(sitd != NULL);
4337 KASSERT(xfer->ux_frlengths[i] <= 0x3ff);
4338
4339 sitd->sitd.sitd_trans = htole32(EHCI_SITD_ACTIVE |
4340 EHCI_SITD_SET_LEN(xfer->ux_frlengths[i]));
4341
4342 /* Set page0 index and offset - TP and T-offset are set below */
4343 sitd->sitd.sitd_buffer[0] = htole32(DMAADDR(dma_buf, offs));
4344
4345 total_length += xfer->ux_frlengths[i];
4346 offs += xfer->ux_frlengths[i];
4347
4348 sitd->sitd.sitd_buffer[1] =
4349 htole32(EHCI_SITD_SET_BPTR(DMAADDR(dma_buf, offs - 1)));
4350
4351 u_int huba __diagused = dev->ud_myhsport->up_parent->ud_addr;
4352
4353 #if 0
4354 if (sc->sc_flags & EHCIF_FREESCALE) {
4355 // Set hub address to 0 if embedded TT is used.
4356 if (huba == sc->sc_addr)
4357 huba = 0;
4358 }
4359 #endif
4360
4361 k = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
4362 dir = UE_GET_DIR(k) ? 1 : 0;
4363 KASSERT(sitd->sitd.sitd_endp == htole32(
4364 EHCI_SITD_SET_ENDPT(UE_GET_ADDR(k)) |
4365 EHCI_SITD_SET_DADDR(dev->ud_addr) |
4366 EHCI_SITD_SET_PORT(dev->ud_myhsport->up_portno) |
4367 EHCI_SITD_SET_HUBA(huba) |
4368 EHCI_SITD_SET_DIR(dir)));
4369 KASSERT(sitd->sitd.sitd_back == htole32(EHCI_LINK_TERMINATE));
4370
4371 uint8_t sa = 0;
4372 uint8_t sb = 0;
4373 u_int temp, tlen;
4374
4375 if (dir == 0) { /* OUT */
4376 temp = 0;
4377 tlen = xfer->ux_frlengths[i];
4378 if (tlen <= 188) {
4379 temp |= 1; /* T-count = 1, TP = ALL */
4380 tlen = 1;
4381 } else {
4382 tlen += 187;
4383 tlen /= 188;
4384 temp |= tlen; /* T-count = [1..6] */
4385 temp |= 8; /* TP = Begin */
4386 }
4387 sitd->sitd.sitd_buffer[1] |= htole32(temp);
4388
4389 tlen += sa;
4390
4391 if (tlen >= 8) {
4392 sb = 0;
4393 } else {
4394 sb = (1 << tlen);
4395 }
4396
4397 sa = (1 << sa);
4398 sa = (sb - sa) & 0x3F;
4399 sb = 0;
4400 } else {
4401 sb = (-(4 << sa)) & 0xFE;
4402 sa = (1 << sa) & 0x3F;
4403 sa = 0x01;
4404 sb = 0xfc;
4405 }
4406
4407 sitd->sitd.sitd_sched = htole32(
4408 EHCI_SITD_SET_SMASK(sa) |
4409 EHCI_SITD_SET_CMASK(sb)
4410 );
4411
4412 usb_syncmem(&sitd->dma, sitd->offs, sizeof(ehci_sitd_t),
4413 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4414 } /* End of frame */
4415
4416 sitd = exfer->ex_sitdend;
4417 sitd->sitd.sitd_trans |= htole32(EHCI_SITD_IOC);
4418
4419 usb_syncmem(&sitd->dma, sitd->offs + offsetof(ehci_sitd_t, sitd_trans),
4420 sizeof(sitd->sitd.sitd_trans),
4421 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4422
4423 usb_syncmem(&exfer->ex_xfer.ux_dmabuf, 0, total_length,
4424 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4425
4426 /*
4427 * Part 2: Transfer descriptors have now been set up, now they must
4428 * be scheduled into the periodic frame list. Erk. Not wanting to
4429 * complicate matters, transfer is denied if the transfer spans
4430 * more than the period frame list.
4431 */
4432
4433 mutex_enter(&sc->sc_lock);
4434
4435 /* Start inserting frames */
4436 if (epipe->isoc.cur_xfers > 0) {
4437 frindex = epipe->isoc.next_frame;
4438 } else {
4439 frindex = EOREAD4(sc, EHCI_FRINDEX);
4440 frindex = frindex >> 3; /* Erase microframe index */
4441 frindex += 2;
4442 }
4443
4444 if (frindex >= sc->sc_flsize)
4445 frindex &= (sc->sc_flsize - 1);
4446
4447 /* Whats the frame interval? */
4448 i = epipe->pipe.up_endpoint->ue_edesc->bInterval;
4449
4450 for (sitd = exfer->ex_sitdstart, j = 0; j < frames;
4451 j++, sitd = sitd->xfer_next) {
4452 KASSERT(sitd);
4453
4454 usb_syncmem(&sc->sc_fldma,
4455 sizeof(ehci_link_t) * frindex,
4456 sizeof(ehci_link_t),
4457 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
4458
4459 sitd->sitd.sitd_next = sc->sc_flist[frindex];
4460 if (sitd->sitd.sitd_next == 0)
4461 /*
4462 * FIXME: frindex table gets initialized to NULL
4463 * or EHCI_NULL?
4464 */
4465 sitd->sitd.sitd_next = EHCI_NULL;
4466
4467 usb_syncmem(&sitd->dma,
4468 sitd->offs + offsetof(ehci_sitd_t, sitd_next),
4469 sizeof(ehci_sitd_t),
4470 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4471
4472 sc->sc_flist[frindex] =
4473 htole32(EHCI_LINK_SITD | sitd->physaddr);
4474
4475 usb_syncmem(&sc->sc_fldma,
4476 sizeof(ehci_link_t) * frindex,
4477 sizeof(ehci_link_t),
4478 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4479
4480 sitd->frame_list.next = sc->sc_softsitds[frindex];
4481 sc->sc_softsitds[frindex] = sitd;
4482 if (sitd->frame_list.next != NULL)
4483 sitd->frame_list.next->frame_list.prev = sitd;
4484 sitd->slot = frindex;
4485 sitd->frame_list.prev = NULL;
4486
4487 frindex += i;
4488 if (frindex >= sc->sc_flsize)
4489 frindex -= sc->sc_flsize;
4490 }
4491
4492 epipe->isoc.cur_xfers++;
4493 epipe->isoc.next_frame = frindex;
4494
4495 ehci_add_intr_list(sc, exfer);
4496 xfer->ux_status = USBD_IN_PROGRESS;
4497 mutex_exit(&sc->sc_lock);
4498
4499 return USBD_IN_PROGRESS;
4500 }
4501
4502 Static void
4503 ehci_device_fs_isoc_abort(struct usbd_xfer *xfer)
4504 {
4505 EHCIHIST_FUNC(); EHCIHIST_CALLED();
4506
4507 DPRINTF("xfer = %p", xfer, 0, 0, 0);
4508 ehci_abort_isoc_xfer(xfer, USBD_CANCELLED);
4509 }
4510
4511 Static void
4512 ehci_device_fs_isoc_close(struct usbd_pipe *pipe)
4513 {
4514 EHCIHIST_FUNC(); EHCIHIST_CALLED();
4515
4516 DPRINTF("nothing in the pipe to free?", 0, 0, 0, 0);
4517 }
4518
4519 Static void
4520 ehci_device_fs_isoc_done(struct usbd_xfer *xfer)
4521 {
4522 struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
4523 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4524 struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
4525
4526 KASSERT(mutex_owned(&sc->sc_lock));
4527
4528 epipe->isoc.cur_xfers--;
4529 ehci_remove_sitd_chain(sc, exfer->ex_itdstart);
4530
4531 usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
4532 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
4533 }
4534
4535
4536 /************************/
4537
4538
4539 Static int
4540 ehci_device_isoc_init(struct usbd_xfer *xfer)
4541 {
4542 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4543 struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
4544 struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
4545 ehci_soft_itd_t *itd, *prev, *start, *stop;
4546 int i, j, k;
4547 int frames, ufrperframe;
4548 int err;
4549
4550 EHCIHIST_FUNC(); EHCIHIST_CALLED();
4551
4552 start = NULL;
4553 prev = NULL;
4554 itd = NULL;
4555
4556 KASSERT(xfer->ux_nframes != 0);
4557 KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
4558 KASSERT(exfer->ex_isdone);
4559
4560 exfer->ex_type = EX_ISOC;
4561
4562 /*
4563 * Step 1: Allocate and initialize itds, how many do we need?
4564 * One per transfer if interval >= 8 microframes, less if we use
4565 * multiple microframes per frame.
4566 */
4567 i = epipe->pipe.up_endpoint->ue_edesc->bInterval;
4568 if (i > 16 || i == 0) {
4569 /* Spec page 271 says intervals > 16 are invalid */
4570 DPRINTF("bInterval %d invalid", i, 0, 0, 0);
4571 return USBD_INVAL;
4572 }
4573
4574 ufrperframe = max(1, USB_UFRAMES_PER_FRAME / (1 << (i - 1)));
4575 frames = (xfer->ux_nframes + (ufrperframe - 1)) / ufrperframe;
4576
4577 for (i = 0, prev = NULL; i < frames; i++, prev = itd) {
4578 itd = ehci_alloc_itd(sc);
4579 if (itd == NULL) {
4580 err = ENOMEM;
4581 goto fail;
4582 }
4583
4584 if (prev != NULL) {
4585 /* Maybe not as it's updated by the scheduling? */
4586 prev->itd.itd_next =
4587 htole32(itd->physaddr | EHCI_LINK_ITD);
4588
4589 prev->xfer_next = itd;
4590 } else {
4591 start = itd;
4592 }
4593
4594 /*
4595 * Other special values
4596 */
4597 k = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
4598 itd->itd.itd_bufr[0] = htole32(
4599 EHCI_ITD_SET_EP(UE_GET_ADDR(k)) |
4600 EHCI_ITD_SET_DADDR(epipe->pipe.up_dev->ud_addr));
4601
4602 k = (UE_GET_DIR(epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress))
4603 ? 1 : 0;
4604 j = UGETW(epipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize);
4605 itd->itd.itd_bufr[1] |= htole32(
4606 EHCI_ITD_SET_DIR(k) |
4607 EHCI_ITD_SET_MAXPKT(UE_GET_SIZE(j)));
4608
4609 /* FIXME: handle invalid trans - should be done in openpipe */
4610 itd->itd.itd_bufr[2] |=
4611 htole32(EHCI_ITD_SET_MULTI(UE_GET_TRANS(j)+1));
4612 } /* End of frame */
4613
4614 stop = itd;
4615 stop->xfer_next = NULL;
4616
4617 exfer->ex_itdstart = start;
4618 exfer->ex_itdend = stop;
4619
4620 return 0;
4621 fail:
4622 mutex_enter(&sc->sc_lock);
4623 ehci_soft_itd_t *next;
4624 for (itd = start; itd; itd = next) {
4625 next = itd->xfer_next;
4626 ehci_free_itd_locked(sc, itd);
4627 }
4628 mutex_exit(&sc->sc_lock);
4629
4630 return err;
4631
4632 }
4633
4634 Static void
4635 ehci_device_isoc_fini(struct usbd_xfer *xfer)
4636 {
4637 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4638 struct ehci_xfer *ex = EHCI_XFER2EXFER(xfer);
4639
4640 KASSERT(ex->ex_type == EX_ISOC);
4641
4642 ehci_free_itd_chain(sc, ex->ex_itdstart);
4643 }
4644
4645 Static usbd_status
4646 ehci_device_isoc_transfer(struct usbd_xfer *xfer)
4647 {
4648 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4649 usbd_status __diagused err;
4650
4651 mutex_enter(&sc->sc_lock);
4652 err = usb_insert_transfer(xfer);
4653 mutex_exit(&sc->sc_lock);
4654
4655 KASSERT(err == USBD_NORMAL_COMPLETION);
4656
4657 struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
4658 struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
4659 ehci_soft_itd_t *itd, *prev;
4660 usb_dma_t *dma_buf;
4661 int i, j;
4662 int frames, uframes, ufrperframe;
4663 int trans_count, offs, total_length;
4664 int frindex;
4665
4666 EHCIHIST_FUNC(); EHCIHIST_CALLED();
4667
4668 prev = NULL;
4669 itd = NULL;
4670 trans_count = 0;
4671 total_length = 0;
4672
4673 DPRINTF("xfer %p flags %d", xfer, xfer->ux_flags, 0, 0);
4674
4675 if (sc->sc_dying)
4676 return USBD_IOERROR;
4677
4678 /*
4679 * To avoid complication, don't allow a request right now that'll span
4680 * the entire frame table. To within 4 frames, to allow some leeway
4681 * on either side of where the hc currently is.
4682 */
4683 if ((1 << (epipe->pipe.up_endpoint->ue_edesc->bInterval)) *
4684 xfer->ux_nframes >= (sc->sc_flsize - 4) * 8) {
4685 DPRINTF(
4686 "isoc descriptor spans entire frametable", 0, 0, 0, 0);
4687 printf("ehci: isoc descriptor requested that spans the entire frametable, too many frames\n");
4688 return USBD_INVAL;
4689 }
4690
4691 KASSERT(xfer->ux_nframes != 0 && xfer->ux_frlengths);
4692 KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
4693 KASSERT(exfer->ex_isdone);
4694 #ifdef DIAGNOSTIC
4695 exfer->ex_isdone = false;
4696 #endif
4697
4698 /*
4699 * Step 1: Re-Initialize itds
4700 */
4701
4702 i = epipe->pipe.up_endpoint->ue_edesc->bInterval;
4703 if (i > 16 || i == 0) {
4704 /* Spec page 271 says intervals > 16 are invalid */
4705 DPRINTF("bInterval %d invalid", i, 0, 0, 0);
4706 return USBD_INVAL;
4707 }
4708
4709 ufrperframe = max(1, USB_UFRAMES_PER_FRAME / (1 << (i - 1)));
4710 frames = (xfer->ux_nframes + (ufrperframe - 1)) / ufrperframe;
4711 uframes = USB_UFRAMES_PER_FRAME / ufrperframe;
4712
4713 if (frames == 0) {
4714 DPRINTF("frames == 0", 0, 0, 0, 0);
4715 return USBD_INVAL;
4716 }
4717
4718 dma_buf = &xfer->ux_dmabuf;
4719 offs = 0;
4720
4721 itd = exfer->ex_itdstart;
4722 for (i = 0; i < frames; i++, itd = itd->xfer_next) {
4723 int froffs = offs;
4724
4725 if (prev != NULL) {
4726 prev->itd.itd_next =
4727 htole32(itd->physaddr | EHCI_LINK_ITD);
4728 usb_syncmem(&prev->dma,
4729 prev->offs + offsetof(ehci_itd_t, itd_next),
4730 sizeof(prev->itd.itd_next), BUS_DMASYNC_POSTWRITE);
4731 prev->xfer_next = itd;
4732 }
4733
4734 /*
4735 * Step 1.5, initialize uframes
4736 */
4737 for (j = 0; j < EHCI_ITD_NUFRAMES; j += uframes) {
4738 /* Calculate which page in the list this starts in */
4739 int addr = DMAADDR(dma_buf, froffs);
4740 addr = EHCI_PAGE_OFFSET(addr);
4741 addr += (offs - froffs);
4742 addr = EHCI_PAGE(addr);
4743 addr /= EHCI_PAGE_SIZE;
4744
4745 /*
4746 * This gets the initial offset into the first page,
4747 * looks how far further along the current uframe
4748 * offset is. Works out how many pages that is.
4749 */
4750
4751 itd->itd.itd_ctl[j] = htole32 ( EHCI_ITD_ACTIVE |
4752 EHCI_ITD_SET_LEN(xfer->ux_frlengths[trans_count]) |
4753 EHCI_ITD_SET_PG(addr) |
4754 EHCI_ITD_SET_OFFS(EHCI_PAGE_OFFSET(DMAADDR(dma_buf,offs))));
4755
4756 total_length += xfer->ux_frlengths[trans_count];
4757 offs += xfer->ux_frlengths[trans_count];
4758 trans_count++;
4759
4760 if (trans_count >= xfer->ux_nframes) { /*Set IOC*/
4761 itd->itd.itd_ctl[j] |= htole32(EHCI_ITD_IOC);
4762 break;
4763 }
4764 }
4765
4766 /*
4767 * Step 1.75, set buffer pointers. To simplify matters, all
4768 * pointers are filled out for the next 7 hardware pages in
4769 * the dma block, so no need to worry what pages to cover
4770 * and what to not.
4771 */
4772
4773 for (j = 0; j < EHCI_ITD_NBUFFERS; j++) {
4774 /*
4775 * Don't try to lookup a page that's past the end
4776 * of buffer
4777 */
4778 int page_offs = EHCI_PAGE(froffs + (EHCI_PAGE_SIZE * j));
4779 if (page_offs >= dma_buf->udma_block->size)
4780 break;
4781
4782 uint64_t page = DMAADDR(dma_buf, page_offs);
4783 page = EHCI_PAGE(page);
4784 itd->itd.itd_bufr[j] = htole32(EHCI_ITD_SET_BPTR(page));
4785 itd->itd.itd_bufr_hi[j] = htole32(page >> 32);
4786 }
4787 /*
4788 * Other special values
4789 */
4790
4791 int k = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
4792 itd->itd.itd_bufr[0] |= htole32(EHCI_ITD_SET_EP(UE_GET_ADDR(k)) |
4793 EHCI_ITD_SET_DADDR(epipe->pipe.up_dev->ud_addr));
4794
4795 k = (UE_GET_DIR(epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress))
4796 ? 1 : 0;
4797 j = UGETW(epipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize);
4798 itd->itd.itd_bufr[1] |= htole32(EHCI_ITD_SET_DIR(k) |
4799 EHCI_ITD_SET_MAXPKT(UE_GET_SIZE(j)));
4800
4801 /* FIXME: handle invalid trans */
4802 itd->itd.itd_bufr[2] |=
4803 htole32(EHCI_ITD_SET_MULTI(UE_GET_TRANS(j)+1));
4804
4805 usb_syncmem(&itd->dma, itd->offs, sizeof(ehci_itd_t),
4806 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4807
4808 prev = itd;
4809 } /* End of frame */
4810
4811 usb_syncmem(&exfer->ex_xfer.ux_dmabuf, 0, total_length,
4812 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4813
4814 /*
4815 * Part 2: Transfer descriptors have now been set up, now they must
4816 * be scheduled into the period frame list. Erk. Not wanting to
4817 * complicate matters, transfer is denied if the transfer spans
4818 * more than the period frame list.
4819 */
4820
4821 mutex_enter(&sc->sc_lock);
4822
4823 /* Start inserting frames */
4824 if (epipe->isoc.cur_xfers > 0) {
4825 frindex = epipe->isoc.next_frame;
4826 } else {
4827 frindex = EOREAD4(sc, EHCI_FRINDEX);
4828 frindex = frindex >> 3; /* Erase microframe index */
4829 frindex += 2;
4830 }
4831
4832 if (frindex >= sc->sc_flsize)
4833 frindex &= (sc->sc_flsize - 1);
4834
4835 /* What's the frame interval? */
4836 i = (1 << (epipe->pipe.up_endpoint->ue_edesc->bInterval - 1));
4837 if (i / USB_UFRAMES_PER_FRAME == 0)
4838 i = 1;
4839 else
4840 i /= USB_UFRAMES_PER_FRAME;
4841
4842 itd = exfer->ex_itdstart;
4843 for (j = 0; j < frames; j++) {
4844 KASSERTMSG(itd != NULL, "frame %d\n", j);
4845
4846 usb_syncmem(&sc->sc_fldma,
4847 sizeof(ehci_link_t) * frindex,
4848 sizeof(ehci_link_t),
4849 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
4850
4851 itd->itd.itd_next = sc->sc_flist[frindex];
4852 if (itd->itd.itd_next == 0)
4853 /*
4854 * FIXME: frindex table gets initialized to NULL
4855 * or EHCI_NULL?
4856 */
4857 itd->itd.itd_next = EHCI_NULL;
4858
4859 usb_syncmem(&itd->dma,
4860 itd->offs + offsetof(ehci_itd_t, itd_next),
4861 sizeof(itd->itd.itd_next),
4862 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4863
4864 sc->sc_flist[frindex] = htole32(EHCI_LINK_ITD | itd->physaddr);
4865
4866 usb_syncmem(&sc->sc_fldma,
4867 sizeof(ehci_link_t) * frindex,
4868 sizeof(ehci_link_t),
4869 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4870
4871 itd->frame_list.next = sc->sc_softitds[frindex];
4872 sc->sc_softitds[frindex] = itd;
4873 if (itd->frame_list.next != NULL)
4874 itd->frame_list.next->frame_list.prev = itd;
4875 itd->slot = frindex;
4876 itd->frame_list.prev = NULL;
4877
4878 frindex += i;
4879 if (frindex >= sc->sc_flsize)
4880 frindex -= sc->sc_flsize;
4881
4882 itd = itd->xfer_next;
4883 }
4884
4885 epipe->isoc.cur_xfers++;
4886 epipe->isoc.next_frame = frindex;
4887
4888 ehci_add_intr_list(sc, exfer);
4889 xfer->ux_status = USBD_IN_PROGRESS;
4890 mutex_exit(&sc->sc_lock);
4891
4892 return USBD_IN_PROGRESS;
4893 }
4894
4895 Static void
4896 ehci_device_isoc_abort(struct usbd_xfer *xfer)
4897 {
4898 EHCIHIST_FUNC(); EHCIHIST_CALLED();
4899
4900 DPRINTF("xfer = %p", xfer, 0, 0, 0);
4901 ehci_abort_isoc_xfer(xfer, USBD_CANCELLED);
4902 }
4903
4904 Static void
4905 ehci_device_isoc_close(struct usbd_pipe *pipe)
4906 {
4907 EHCIHIST_FUNC(); EHCIHIST_CALLED();
4908
4909 DPRINTF("nothing in the pipe to free?", 0, 0, 0, 0);
4910 }
4911
4912 Static void
4913 ehci_device_isoc_done(struct usbd_xfer *xfer)
4914 {
4915 struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
4916 ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4917 struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
4918
4919 KASSERT(mutex_owned(&sc->sc_lock));
4920
4921 epipe->isoc.cur_xfers--;
4922 ehci_remove_itd_chain(sc, exfer->ex_sitdstart);
4923 usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
4924 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
4925 }
4926