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