uhci.c revision 1.270 1 /* $NetBSD: uhci.c,v 1.270 2016/04/25 20:06:51 joerg Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2004, 2011, 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) at
9 * Carlstedt Research & Technology, Jared D. McNeill (jmcneill (at) invisible.ca)
10 * 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 Universal Host Controller driver.
36 * Handles e.g. PIIX3 and PIIX4.
37 *
38 * UHCI spec: http://www.intel.com/technology/usb/spec.htm
39 * USB spec: http://www.usb.org/developers/docs/
40 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
41 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: uhci.c,v 1.270 2016/04/25 20:06:51 joerg Exp $");
46
47 #include "opt_usb.h"
48
49 #include <sys/param.h>
50
51 #include <sys/bus.h>
52 #include <sys/cpu.h>
53 #include <sys/device.h>
54 #include <sys/kernel.h>
55 #include <sys/kmem.h>
56 #include <sys/mutex.h>
57 #include <sys/proc.h>
58 #include <sys/queue.h>
59 #include <sys/select.h>
60 #include <sys/sysctl.h>
61 #include <sys/systm.h>
62
63 #include <machine/endian.h>
64
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdivar.h>
68 #include <dev/usb/usb_mem.h>
69
70 #include <dev/usb/uhcireg.h>
71 #include <dev/usb/uhcivar.h>
72 #include <dev/usb/usbroothub.h>
73 #include <dev/usb/usbhist.h>
74
75 /* Use bandwidth reclamation for control transfers. Some devices choke on it. */
76 /*#define UHCI_CTL_LOOP */
77
78 #ifdef UHCI_DEBUG
79 uhci_softc_t *thesc;
80 int uhcinoloop = 0;
81 #endif
82
83 #ifdef USB_DEBUG
84 #ifndef UHCI_DEBUG
85 #define uhcidebug 0
86 #else
87 static int uhcidebug = 0;
88
89 SYSCTL_SETUP(sysctl_hw_uhci_setup, "sysctl hw.uhci setup")
90 {
91 int err;
92 const struct sysctlnode *rnode;
93 const struct sysctlnode *cnode;
94
95 err = sysctl_createv(clog, 0, NULL, &rnode,
96 CTLFLAG_PERMANENT, CTLTYPE_NODE, "uhci",
97 SYSCTL_DESCR("uhci global controls"),
98 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
99
100 if (err)
101 goto fail;
102
103 /* control debugging printfs */
104 err = sysctl_createv(clog, 0, &rnode, &cnode,
105 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
106 "debug", SYSCTL_DESCR("Enable debugging output"),
107 NULL, 0, &uhcidebug, sizeof(uhcidebug), CTL_CREATE, CTL_EOL);
108 if (err)
109 goto fail;
110
111 return;
112 fail:
113 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
114 }
115
116 #endif /* UHCI_DEBUG */
117 #endif /* USB_DEBUG */
118
119 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(uhcidebug,1,FMT,A,B,C,D)
120 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(uhcidebug,N,FMT,A,B,C,D)
121 #define UHCIHIST_FUNC() USBHIST_FUNC()
122 #define UHCIHIST_CALLED(name) USBHIST_CALLED(uhcidebug)
123
124 /*
125 * The UHCI controller is little endian, so on big endian machines
126 * the data stored in memory needs to be swapped.
127 */
128
129 struct uhci_pipe {
130 struct usbd_pipe pipe;
131 int nexttoggle;
132
133 u_char aborting;
134 struct usbd_xfer *abortstart, abortend;
135
136 /* Info needed for different pipe kinds. */
137 union {
138 /* Control pipe */
139 struct {
140 uhci_soft_qh_t *sqh;
141 usb_dma_t reqdma;
142 uhci_soft_td_t *setup;
143 uhci_soft_td_t *stat;
144 } ctrl;
145 /* Interrupt pipe */
146 struct {
147 int npoll;
148 uhci_soft_qh_t **qhs;
149 } intr;
150 /* Bulk pipe */
151 struct {
152 uhci_soft_qh_t *sqh;
153 } bulk;
154 /* Isochronous pipe */
155 struct isoc {
156 uhci_soft_td_t **stds;
157 int next, inuse;
158 } isoc;
159 };
160 };
161
162 typedef TAILQ_HEAD(ux_completeq, uhci_xfer) ux_completeq_t;
163
164 Static void uhci_globalreset(uhci_softc_t *);
165 Static usbd_status uhci_portreset(uhci_softc_t*, int);
166 Static void uhci_reset(uhci_softc_t *);
167 Static usbd_status uhci_run(uhci_softc_t *, int, int);
168 Static uhci_soft_td_t *uhci_alloc_std(uhci_softc_t *);
169 Static void uhci_free_std(uhci_softc_t *, uhci_soft_td_t *);
170 Static void uhci_free_std_locked(uhci_softc_t *, uhci_soft_td_t *);
171 Static uhci_soft_qh_t *uhci_alloc_sqh(uhci_softc_t *);
172 Static void uhci_free_sqh(uhci_softc_t *, uhci_soft_qh_t *);
173 #if 0
174 Static void uhci_enter_ctl_q(uhci_softc_t *, uhci_soft_qh_t *,
175 uhci_intr_info_t *);
176 Static void uhci_exit_ctl_q(uhci_softc_t *, uhci_soft_qh_t *);
177 #endif
178
179 #if 0
180 Static void uhci_free_std_chain(uhci_softc_t *, uhci_soft_td_t *,
181 uhci_soft_td_t *);
182 #endif
183 Static int uhci_alloc_std_chain(uhci_softc_t *, struct usbd_xfer *,
184 int, int, uhci_soft_td_t **);
185 Static void uhci_free_stds(uhci_softc_t *, struct uhci_xfer *);
186
187 Static void uhci_reset_std_chain(uhci_softc_t *, struct usbd_xfer *,
188 int, int, int *, uhci_soft_td_t **);
189
190 Static void uhci_poll_hub(void *);
191 Static void uhci_waitintr(uhci_softc_t *, struct usbd_xfer *);
192 Static void uhci_check_intr(uhci_softc_t *, struct uhci_xfer *,
193 ux_completeq_t *);
194 Static void uhci_idone(struct uhci_xfer *, ux_completeq_t *);
195
196 Static void uhci_abort_xfer(struct usbd_xfer *, usbd_status);
197
198 Static void uhci_timeout(void *);
199 Static void uhci_timeout_task(void *);
200 Static void uhci_add_ls_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
201 Static void uhci_add_hs_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
202 Static void uhci_add_bulk(uhci_softc_t *, uhci_soft_qh_t *);
203 Static void uhci_remove_ls_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
204 Static void uhci_remove_hs_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
205 Static void uhci_remove_bulk(uhci_softc_t *,uhci_soft_qh_t *);
206 Static void uhci_add_loop(uhci_softc_t *);
207 Static void uhci_rem_loop(uhci_softc_t *);
208
209 Static usbd_status uhci_setup_isoc(struct usbd_pipe *);
210
211 Static struct usbd_xfer *
212 uhci_allocx(struct usbd_bus *, unsigned int);
213 Static void uhci_freex(struct usbd_bus *, struct usbd_xfer *);
214 Static void uhci_get_lock(struct usbd_bus *, kmutex_t **);
215 Static int uhci_roothub_ctrl(struct usbd_bus *,
216 usb_device_request_t *, void *, int);
217
218 Static int uhci_device_ctrl_init(struct usbd_xfer *);
219 Static void uhci_device_ctrl_fini(struct usbd_xfer *);
220 Static usbd_status uhci_device_ctrl_transfer(struct usbd_xfer *);
221 Static usbd_status uhci_device_ctrl_start(struct usbd_xfer *);
222 Static void uhci_device_ctrl_abort(struct usbd_xfer *);
223 Static void uhci_device_ctrl_close(struct usbd_pipe *);
224 Static void uhci_device_ctrl_done(struct usbd_xfer *);
225
226 Static int uhci_device_intr_init(struct usbd_xfer *);
227 Static void uhci_device_intr_fini(struct usbd_xfer *);
228 Static usbd_status uhci_device_intr_transfer(struct usbd_xfer *);
229 Static usbd_status uhci_device_intr_start(struct usbd_xfer *);
230 Static void uhci_device_intr_abort(struct usbd_xfer *);
231 Static void uhci_device_intr_close(struct usbd_pipe *);
232 Static void uhci_device_intr_done(struct usbd_xfer *);
233
234 Static int uhci_device_bulk_init(struct usbd_xfer *);
235 Static void uhci_device_bulk_fini(struct usbd_xfer *);
236 Static usbd_status uhci_device_bulk_transfer(struct usbd_xfer *);
237 Static usbd_status uhci_device_bulk_start(struct usbd_xfer *);
238 Static void uhci_device_bulk_abort(struct usbd_xfer *);
239 Static void uhci_device_bulk_close(struct usbd_pipe *);
240 Static void uhci_device_bulk_done(struct usbd_xfer *);
241
242 Static int uhci_device_isoc_init(struct usbd_xfer *);
243 Static void uhci_device_isoc_fini(struct usbd_xfer *);
244 Static usbd_status uhci_device_isoc_transfer(struct usbd_xfer *);
245 Static void uhci_device_isoc_abort(struct usbd_xfer *);
246 Static void uhci_device_isoc_close(struct usbd_pipe *);
247 Static void uhci_device_isoc_done(struct usbd_xfer *);
248
249 Static usbd_status uhci_root_intr_transfer(struct usbd_xfer *);
250 Static usbd_status uhci_root_intr_start(struct usbd_xfer *);
251 Static void uhci_root_intr_abort(struct usbd_xfer *);
252 Static void uhci_root_intr_close(struct usbd_pipe *);
253 Static void uhci_root_intr_done(struct usbd_xfer *);
254
255 Static usbd_status uhci_open(struct usbd_pipe *);
256 Static void uhci_poll(struct usbd_bus *);
257 Static void uhci_softintr(void *);
258
259 Static void uhci_add_intr(uhci_softc_t *, uhci_soft_qh_t *);
260 Static void uhci_remove_intr(uhci_softc_t *, uhci_soft_qh_t *);
261 Static usbd_status uhci_device_setintr(uhci_softc_t *,
262 struct uhci_pipe *, int);
263
264 Static void uhci_device_clear_toggle(struct usbd_pipe *);
265 Static void uhci_noop(struct usbd_pipe *);
266
267 static inline uhci_soft_qh_t *
268 uhci_find_prev_qh(uhci_soft_qh_t *, uhci_soft_qh_t *);
269
270 #ifdef UHCI_DEBUG
271 Static void uhci_dump_all(uhci_softc_t *);
272 Static void uhci_dumpregs(uhci_softc_t *);
273 Static void uhci_dump_qhs(uhci_soft_qh_t *);
274 Static void uhci_dump_qh(uhci_soft_qh_t *);
275 Static void uhci_dump_tds(uhci_soft_td_t *);
276 Static void uhci_dump_td(uhci_soft_td_t *);
277 Static void uhci_dump_ii(struct uhci_xfer *);
278 void uhci_dump(void);
279 #endif
280
281 #define UBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
282 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
283 #define UWRITE1(sc, r, x) \
284 do { UBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); \
285 } while (/*CONSTCOND*/0)
286 #define UWRITE2(sc, r, x) \
287 do { UBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); \
288 } while (/*CONSTCOND*/0)
289 #define UWRITE4(sc, r, x) \
290 do { UBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); \
291 } while (/*CONSTCOND*/0)
292
293 static __inline uint8_t
294 UREAD1(uhci_softc_t *sc, bus_size_t r)
295 {
296
297 UBARR(sc);
298 return bus_space_read_1(sc->iot, sc->ioh, r);
299 }
300
301 static __inline uint16_t
302 UREAD2(uhci_softc_t *sc, bus_size_t r)
303 {
304
305 UBARR(sc);
306 return bus_space_read_2(sc->iot, sc->ioh, r);
307 }
308
309 #ifdef UHCI_DEBUG
310 static __inline uint32_t
311 UREAD4(uhci_softc_t *sc, bus_size_t r)
312 {
313
314 UBARR(sc);
315 return bus_space_read_4(sc->iot, sc->ioh, r);
316 }
317 #endif
318
319 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
320 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
321
322 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
323
324 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
325
326 const struct usbd_bus_methods uhci_bus_methods = {
327 .ubm_open = uhci_open,
328 .ubm_softint = uhci_softintr,
329 .ubm_dopoll = uhci_poll,
330 .ubm_allocx = uhci_allocx,
331 .ubm_freex = uhci_freex,
332 .ubm_getlock = uhci_get_lock,
333 .ubm_rhctrl = uhci_roothub_ctrl,
334 };
335
336 const struct usbd_pipe_methods uhci_root_intr_methods = {
337 .upm_transfer = uhci_root_intr_transfer,
338 .upm_start = uhci_root_intr_start,
339 .upm_abort = uhci_root_intr_abort,
340 .upm_close = uhci_root_intr_close,
341 .upm_cleartoggle = uhci_noop,
342 .upm_done = uhci_root_intr_done,
343 };
344
345 const struct usbd_pipe_methods uhci_device_ctrl_methods = {
346 .upm_init = uhci_device_ctrl_init,
347 .upm_fini = uhci_device_ctrl_fini,
348 .upm_transfer = uhci_device_ctrl_transfer,
349 .upm_start = uhci_device_ctrl_start,
350 .upm_abort = uhci_device_ctrl_abort,
351 .upm_close = uhci_device_ctrl_close,
352 .upm_cleartoggle = uhci_noop,
353 .upm_done = uhci_device_ctrl_done,
354 };
355
356 const struct usbd_pipe_methods uhci_device_intr_methods = {
357 .upm_init = uhci_device_intr_init,
358 .upm_fini = uhci_device_intr_fini,
359 .upm_transfer = uhci_device_intr_transfer,
360 .upm_start = uhci_device_intr_start,
361 .upm_abort = uhci_device_intr_abort,
362 .upm_close = uhci_device_intr_close,
363 .upm_cleartoggle = uhci_device_clear_toggle,
364 .upm_done = uhci_device_intr_done,
365 };
366
367 const struct usbd_pipe_methods uhci_device_bulk_methods = {
368 .upm_init = uhci_device_bulk_init,
369 .upm_fini = uhci_device_bulk_fini,
370 .upm_transfer = uhci_device_bulk_transfer,
371 .upm_start = uhci_device_bulk_start,
372 .upm_abort = uhci_device_bulk_abort,
373 .upm_close = uhci_device_bulk_close,
374 .upm_cleartoggle = uhci_device_clear_toggle,
375 .upm_done = uhci_device_bulk_done,
376 };
377
378 const struct usbd_pipe_methods uhci_device_isoc_methods = {
379 .upm_init = uhci_device_isoc_init,
380 .upm_fini = uhci_device_isoc_fini,
381 .upm_transfer = uhci_device_isoc_transfer,
382 .upm_abort = uhci_device_isoc_abort,
383 .upm_close = uhci_device_isoc_close,
384 .upm_cleartoggle = uhci_noop,
385 .upm_done = uhci_device_isoc_done,
386 };
387
388 static inline void
389 uhci_add_intr_list(uhci_softc_t *sc, struct uhci_xfer *ux)
390 {
391
392 TAILQ_INSERT_TAIL(&sc->sc_intrhead, ux, ux_list);
393 }
394
395 static inline void
396 uhci_del_intr_list(uhci_softc_t *sc, struct uhci_xfer *ux)
397 {
398
399 TAILQ_REMOVE(&sc->sc_intrhead, ux, ux_list);
400 }
401
402 static inline uhci_soft_qh_t *
403 uhci_find_prev_qh(uhci_soft_qh_t *pqh, uhci_soft_qh_t *sqh)
404 {
405 UHCIHIST_FUNC(); UHCIHIST_CALLED();
406 DPRINTFN(15, "pqh=%p sqh=%p", pqh, sqh, 0, 0);
407
408 for (; pqh->hlink != sqh; pqh = pqh->hlink) {
409 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
410 usb_syncmem(&pqh->dma,
411 pqh->offs + offsetof(uhci_qh_t, qh_hlink),
412 sizeof(pqh->qh.qh_hlink),
413 BUS_DMASYNC_POSTWRITE);
414 if (le32toh(pqh->qh.qh_hlink) & UHCI_PTR_T) {
415 printf("%s: QH not found\n", __func__);
416 return NULL;
417 }
418 #endif
419 }
420 return pqh;
421 }
422
423 void
424 uhci_globalreset(uhci_softc_t *sc)
425 {
426 UHCICMD(sc, UHCI_CMD_GRESET); /* global reset */
427 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
428 UHCICMD(sc, 0); /* do nothing */
429 }
430
431 int
432 uhci_init(uhci_softc_t *sc)
433 {
434 usbd_status err;
435 int i, j;
436 uhci_soft_qh_t *clsqh, *chsqh, *bsqh, *sqh, *lsqh;
437 uhci_soft_td_t *std;
438
439 UHCIHIST_FUNC(); UHCIHIST_CALLED();
440
441 #ifdef UHCI_DEBUG
442 thesc = sc;
443
444 if (uhcidebug >= 2)
445 uhci_dumpregs(sc);
446 #endif
447
448 sc->sc_suspend = PWR_RESUME;
449
450 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
451 uhci_globalreset(sc); /* reset the controller */
452 uhci_reset(sc);
453
454 /* Allocate and initialize real frame array. */
455 err = usb_allocmem(&sc->sc_bus,
456 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
457 UHCI_FRAMELIST_ALIGN, &sc->sc_dma);
458 if (err)
459 return err;
460 sc->sc_pframes = KERNADDR(&sc->sc_dma, 0);
461 UWRITE2(sc, UHCI_FRNUM, 0); /* set frame number to 0 */
462 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0)); /* set frame list*/
463
464 /* Initialise mutex early for uhci_alloc_* */
465 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
466 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
467
468 /*
469 * Allocate a TD, inactive, that hangs from the last QH.
470 * This is to avoid a bug in the PIIX that makes it run berserk
471 * otherwise.
472 */
473 std = uhci_alloc_std(sc);
474 if (std == NULL)
475 return ENOMEM;
476 std->link.std = NULL;
477 std->td.td_link = htole32(UHCI_PTR_T);
478 std->td.td_status = htole32(0); /* inactive */
479 std->td.td_token = htole32(0);
480 std->td.td_buffer = htole32(0);
481 usb_syncmem(&std->dma, std->offs, sizeof(std->td),
482 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
483
484 /* Allocate the dummy QH marking the end and used for looping the QHs.*/
485 lsqh = uhci_alloc_sqh(sc);
486 if (lsqh == NULL)
487 goto fail1;
488 lsqh->hlink = NULL;
489 lsqh->qh.qh_hlink = htole32(UHCI_PTR_T); /* end of QH chain */
490 lsqh->elink = std;
491 lsqh->qh.qh_elink = htole32(std->physaddr | UHCI_PTR_TD);
492 sc->sc_last_qh = lsqh;
493 usb_syncmem(&lsqh->dma, lsqh->offs, sizeof(lsqh->qh),
494 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
495
496 /* Allocate the dummy QH where bulk traffic will be queued. */
497 bsqh = uhci_alloc_sqh(sc);
498 if (bsqh == NULL)
499 goto fail2;
500 bsqh->hlink = lsqh;
501 bsqh->qh.qh_hlink = htole32(lsqh->physaddr | UHCI_PTR_QH);
502 bsqh->elink = NULL;
503 bsqh->qh.qh_elink = htole32(UHCI_PTR_T);
504 sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
505 usb_syncmem(&bsqh->dma, bsqh->offs, sizeof(bsqh->qh),
506 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
507
508 /* Allocate dummy QH where high speed control traffic will be queued. */
509 chsqh = uhci_alloc_sqh(sc);
510 if (chsqh == NULL)
511 goto fail3;
512 chsqh->hlink = bsqh;
513 chsqh->qh.qh_hlink = htole32(bsqh->physaddr | UHCI_PTR_QH);
514 chsqh->elink = NULL;
515 chsqh->qh.qh_elink = htole32(UHCI_PTR_T);
516 sc->sc_hctl_start = sc->sc_hctl_end = chsqh;
517 usb_syncmem(&chsqh->dma, chsqh->offs, sizeof(chsqh->qh),
518 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
519
520 /* Allocate dummy QH where control traffic will be queued. */
521 clsqh = uhci_alloc_sqh(sc);
522 if (clsqh == NULL)
523 goto fail4;
524 clsqh->hlink = chsqh;
525 clsqh->qh.qh_hlink = htole32(chsqh->physaddr | UHCI_PTR_QH);
526 clsqh->elink = NULL;
527 clsqh->qh.qh_elink = htole32(UHCI_PTR_T);
528 sc->sc_lctl_start = sc->sc_lctl_end = clsqh;
529 usb_syncmem(&clsqh->dma, clsqh->offs, sizeof(clsqh->qh),
530 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
531
532 /*
533 * Make all (virtual) frame list pointers point to the interrupt
534 * queue heads and the interrupt queue heads at the control
535 * queue head and point the physical frame list to the virtual.
536 */
537 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
538 std = uhci_alloc_std(sc);
539 sqh = uhci_alloc_sqh(sc);
540 if (std == NULL || sqh == NULL)
541 return USBD_NOMEM;
542 std->link.sqh = sqh;
543 std->td.td_link = htole32(sqh->physaddr | UHCI_PTR_QH);
544 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
545 std->td.td_token = htole32(0);
546 std->td.td_buffer = htole32(0);
547 usb_syncmem(&std->dma, std->offs, sizeof(std->td),
548 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
549 sqh->hlink = clsqh;
550 sqh->qh.qh_hlink = htole32(clsqh->physaddr | UHCI_PTR_QH);
551 sqh->elink = NULL;
552 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
553 usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
554 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
555 sc->sc_vframes[i].htd = std;
556 sc->sc_vframes[i].etd = std;
557 sc->sc_vframes[i].hqh = sqh;
558 sc->sc_vframes[i].eqh = sqh;
559 for (j = i;
560 j < UHCI_FRAMELIST_COUNT;
561 j += UHCI_VFRAMELIST_COUNT)
562 sc->sc_pframes[j] = htole32(std->physaddr);
563 }
564 usb_syncmem(&sc->sc_dma, 0,
565 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
566 BUS_DMASYNC_PREWRITE);
567
568
569 TAILQ_INIT(&sc->sc_intrhead);
570
571 sc->sc_xferpool = pool_cache_init(sizeof(struct uhci_xfer), 0, 0, 0,
572 "uhcixfer", NULL, IPL_USB, NULL, NULL, NULL);
573
574 callout_init(&sc->sc_poll_handle, CALLOUT_MPSAFE);
575
576 cv_init(&sc->sc_softwake_cv, "uhciab");
577
578 /* Set up the bus struct. */
579 sc->sc_bus.ub_methods = &uhci_bus_methods;
580 sc->sc_bus.ub_pipesize = sizeof(struct uhci_pipe);
581 sc->sc_bus.ub_usedma = true;
582
583 UHCICMD(sc, UHCI_CMD_MAXP); /* Assume 64 byte packets at frame end */
584
585 DPRINTF("Enabling...", 0, 0, 0, 0);
586
587 err = uhci_run(sc, 1, 0); /* and here we go... */
588 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
589 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */
590 return err;
591
592 fail4:
593 uhci_free_sqh(sc, chsqh);
594 fail3:
595 uhci_free_sqh(sc, lsqh);
596 fail2:
597 uhci_free_sqh(sc, lsqh);
598 fail1:
599 uhci_free_std(sc, std);
600
601 return ENOMEM;
602 }
603
604 int
605 uhci_activate(device_t self, enum devact act)
606 {
607 struct uhci_softc *sc = device_private(self);
608
609 switch (act) {
610 case DVACT_DEACTIVATE:
611 sc->sc_dying = 1;
612 return 0;
613 default:
614 return EOPNOTSUPP;
615 }
616 }
617
618 void
619 uhci_childdet(device_t self, device_t child)
620 {
621 struct uhci_softc *sc = device_private(self);
622
623 KASSERT(sc->sc_child == child);
624 sc->sc_child = NULL;
625 }
626
627 int
628 uhci_detach(struct uhci_softc *sc, int flags)
629 {
630 int rv = 0;
631
632 if (sc->sc_child != NULL)
633 rv = config_detach(sc->sc_child, flags);
634
635 if (rv != 0)
636 return rv;
637
638 callout_halt(&sc->sc_poll_handle, NULL);
639 callout_destroy(&sc->sc_poll_handle);
640
641 cv_destroy(&sc->sc_softwake_cv);
642
643 mutex_destroy(&sc->sc_lock);
644 mutex_destroy(&sc->sc_intr_lock);
645
646 pool_cache_destroy(sc->sc_xferpool);
647
648 /* XXX free other data structures XXX */
649
650 return rv;
651 }
652
653 struct usbd_xfer *
654 uhci_allocx(struct usbd_bus *bus, unsigned int nframes)
655 {
656 struct uhci_softc *sc = UHCI_BUS2SC(bus);
657 struct usbd_xfer *xfer;
658
659 xfer = pool_cache_get(sc->sc_xferpool, PR_NOWAIT);
660 if (xfer != NULL) {
661 memset(xfer, 0, sizeof(struct uhci_xfer));
662
663 #ifdef DIAGNOSTIC
664 struct uhci_xfer *uxfer = UHCI_XFER2UXFER(xfer);
665 uxfer->ux_isdone = true;
666 xfer->ux_state = XFER_BUSY;
667 #endif
668 }
669 return xfer;
670 }
671
672 void
673 uhci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
674 {
675 struct uhci_softc *sc = UHCI_BUS2SC(bus);
676 struct uhci_xfer *uxfer __diagused = UHCI_XFER2UXFER(xfer);
677
678 KASSERTMSG(xfer->ux_state == XFER_BUSY, "xfer %p state %d\n", xfer,
679 xfer->ux_state);
680 KASSERTMSG(uxfer->ux_isdone, "xfer %p not done\n", xfer);
681 #ifdef DIAGNOSTIC
682 xfer->ux_state = XFER_FREE;
683 #endif
684 pool_cache_put(sc->sc_xferpool, xfer);
685 }
686
687 Static void
688 uhci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
689 {
690 struct uhci_softc *sc = UHCI_BUS2SC(bus);
691
692 *lock = &sc->sc_lock;
693 }
694
695
696 /*
697 * Handle suspend/resume.
698 *
699 * We need to switch to polling mode here, because this routine is
700 * called from an interrupt context. This is all right since we
701 * are almost suspended anyway.
702 */
703 bool
704 uhci_resume(device_t dv, const pmf_qual_t *qual)
705 {
706 uhci_softc_t *sc = device_private(dv);
707 int cmd;
708
709 mutex_spin_enter(&sc->sc_intr_lock);
710
711 cmd = UREAD2(sc, UHCI_CMD);
712 sc->sc_bus.ub_usepolling++;
713 UWRITE2(sc, UHCI_INTR, 0);
714 uhci_globalreset(sc);
715 uhci_reset(sc);
716 if (cmd & UHCI_CMD_RS)
717 uhci_run(sc, 0, 1);
718
719 /* restore saved state */
720 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0));
721 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
722 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
723
724 UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force resume */
725 usb_delay_ms_locked(&sc->sc_bus, USB_RESUME_DELAY, &sc->sc_intr_lock);
726 UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
727 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE |
728 UHCI_INTR_RIE | UHCI_INTR_IOCE | UHCI_INTR_SPIE);
729 UHCICMD(sc, UHCI_CMD_MAXP);
730 uhci_run(sc, 1, 1); /* and start traffic again */
731 usb_delay_ms_locked(&sc->sc_bus, USB_RESUME_RECOVERY, &sc->sc_intr_lock);
732 sc->sc_bus.ub_usepolling--;
733 if (sc->sc_intr_xfer != NULL)
734 callout_reset(&sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub,
735 sc->sc_intr_xfer);
736 #ifdef UHCI_DEBUG
737 if (uhcidebug >= 2)
738 uhci_dumpregs(sc);
739 #endif
740
741 sc->sc_suspend = PWR_RESUME;
742 mutex_spin_exit(&sc->sc_intr_lock);
743
744 return true;
745 }
746
747 bool
748 uhci_suspend(device_t dv, const pmf_qual_t *qual)
749 {
750 uhci_softc_t *sc = device_private(dv);
751 int cmd;
752
753 mutex_spin_enter(&sc->sc_intr_lock);
754
755 cmd = UREAD2(sc, UHCI_CMD);
756
757 #ifdef UHCI_DEBUG
758 if (uhcidebug >= 2)
759 uhci_dumpregs(sc);
760 #endif
761 if (sc->sc_intr_xfer != NULL)
762 callout_stop(&sc->sc_poll_handle);
763 sc->sc_suspend = PWR_SUSPEND;
764 sc->sc_bus.ub_usepolling++;
765
766 uhci_run(sc, 0, 1); /* stop the controller */
767 cmd &= ~UHCI_CMD_RS;
768
769 /* save some state if BIOS doesn't */
770 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
771 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
772
773 UWRITE2(sc, UHCI_INTR, 0); /* disable intrs */
774
775 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter suspend */
776 usb_delay_ms_locked(&sc->sc_bus, USB_RESUME_WAIT, &sc->sc_intr_lock);
777 sc->sc_bus.ub_usepolling--;
778
779 mutex_spin_exit(&sc->sc_intr_lock);
780
781 return true;
782 }
783
784 #ifdef UHCI_DEBUG
785 Static void
786 uhci_dumpregs(uhci_softc_t *sc)
787 {
788 UHCIHIST_FUNC(); UHCIHIST_CALLED();
789 DPRINTF("cmd =%04x sts =%04x intr =%04x frnum =%04x",
790 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS),
791 UREAD2(sc, UHCI_INTR), UREAD2(sc, UHCI_FRNUM));
792 DPRINTF("sof =%04x portsc1=%04x portsc2=%04x flbase=%08x",
793 UREAD1(sc, UHCI_SOF), UREAD2(sc, UHCI_PORTSC1),
794 UREAD2(sc, UHCI_PORTSC2), UREAD4(sc, UHCI_FLBASEADDR));
795 }
796
797 void
798 uhci_dump_td(uhci_soft_td_t *p)
799 {
800 UHCIHIST_FUNC(); UHCIHIST_CALLED();
801
802 usb_syncmem(&p->dma, p->offs, sizeof(p->td),
803 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
804
805 DPRINTF("TD(%p) at 0x%08x", p, p->physaddr, 0, 0);
806 DPRINTF(" link=0x%08x status=0x%08x "
807 "token=0x%08x buffer=0x%08x",
808 le32toh(p->td.td_link),
809 le32toh(p->td.td_status),
810 le32toh(p->td.td_token),
811 le32toh(p->td.td_buffer));
812
813 DPRINTF("bitstuff=%d crcto =%d nak =%d babble =%d",
814 !!(le32toh(p->td.td_status) & UHCI_TD_BITSTUFF),
815 !!(le32toh(p->td.td_status) & UHCI_TD_CRCTO),
816 !!(le32toh(p->td.td_status) & UHCI_TD_NAK),
817 !!(le32toh(p->td.td_status) & UHCI_TD_BABBLE));
818 DPRINTF("dbuffer =%d stalled =%d active =%d ioc =%d",
819 !!(le32toh(p->td.td_status) & UHCI_TD_DBUFFER),
820 !!(le32toh(p->td.td_status) & UHCI_TD_STALLED),
821 !!(le32toh(p->td.td_status) & UHCI_TD_ACTIVE),
822 !!(le32toh(p->td.td_status) & UHCI_TD_IOC));
823 DPRINTF("ios =%d ls =%d spd =%d",
824 !!(le32toh(p->td.td_status) & UHCI_TD_IOS),
825 !!(le32toh(p->td.td_status) & UHCI_TD_LS),
826 !!(le32toh(p->td.td_status) & UHCI_TD_SPD), 0);
827 DPRINTF("errcnt =%d actlen =%d pid=%02x",
828 UHCI_TD_GET_ERRCNT(le32toh(p->td.td_status)),
829 UHCI_TD_GET_ACTLEN(le32toh(p->td.td_status)),
830 UHCI_TD_GET_PID(le32toh(p->td.td_token)), 0);
831 DPRINTF("addr=%d endpt=%d D=%d maxlen=%d,",
832 UHCI_TD_GET_DEVADDR(le32toh(p->td.td_token)),
833 UHCI_TD_GET_ENDPT(le32toh(p->td.td_token)),
834 UHCI_TD_GET_DT(le32toh(p->td.td_token)),
835 UHCI_TD_GET_MAXLEN(le32toh(p->td.td_token)));
836 }
837
838 void
839 uhci_dump_qh(uhci_soft_qh_t *sqh)
840 {
841 UHCIHIST_FUNC(); UHCIHIST_CALLED();
842
843 usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
844 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
845
846 DPRINTF("QH(%p) at 0x%08x: hlink=%08x elink=%08x", sqh,
847 (int)sqh->physaddr, le32toh(sqh->qh.qh_hlink),
848 le32toh(sqh->qh.qh_elink));
849
850 usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh), BUS_DMASYNC_PREREAD);
851 }
852
853
854 #if 1
855 void
856 uhci_dump(void)
857 {
858 uhci_dump_all(thesc);
859 }
860 #endif
861
862 void
863 uhci_dump_all(uhci_softc_t *sc)
864 {
865 uhci_dumpregs(sc);
866 /*printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);*/
867 uhci_dump_qhs(sc->sc_lctl_start);
868 }
869
870
871 void
872 uhci_dump_qhs(uhci_soft_qh_t *sqh)
873 {
874 UHCIHIST_FUNC(); UHCIHIST_CALLED();
875
876 uhci_dump_qh(sqh);
877
878 /*
879 * uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
880 * Traverses sideways first, then down.
881 *
882 * QH1
883 * QH2
884 * No QH
885 * TD2.1
886 * TD2.2
887 * TD1.1
888 * etc.
889 *
890 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
891 */
892
893 usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
894 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
895 if (sqh->hlink != NULL && !(le32toh(sqh->qh.qh_hlink) & UHCI_PTR_T))
896 uhci_dump_qhs(sqh->hlink);
897 else
898 DPRINTF("No QH", 0, 0, 0, 0);
899 usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh), BUS_DMASYNC_PREREAD);
900
901 if (sqh->elink != NULL && !(le32toh(sqh->qh.qh_elink) & UHCI_PTR_T))
902 uhci_dump_tds(sqh->elink);
903 else
904 DPRINTF("No QH", 0, 0, 0, 0);
905 }
906
907 void
908 uhci_dump_tds(uhci_soft_td_t *std)
909 {
910 uhci_soft_td_t *td;
911 int stop;
912
913 for (td = std; td != NULL; td = td->link.std) {
914 uhci_dump_td(td);
915
916 /*
917 * Check whether the link pointer in this TD marks
918 * the link pointer as end of queue. This avoids
919 * printing the free list in case the queue/TD has
920 * already been moved there (seatbelt).
921 */
922 usb_syncmem(&td->dma, td->offs + offsetof(uhci_td_t, td_link),
923 sizeof(td->td.td_link),
924 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
925 stop = (le32toh(td->td.td_link) & UHCI_PTR_T ||
926 le32toh(td->td.td_link) == 0);
927 usb_syncmem(&td->dma, td->offs + offsetof(uhci_td_t, td_link),
928 sizeof(td->td.td_link), BUS_DMASYNC_PREREAD);
929 if (stop)
930 break;
931 }
932 }
933
934 Static void
935 uhci_dump_ii(struct uhci_xfer *ux)
936 {
937 struct usbd_pipe *pipe;
938 usb_endpoint_descriptor_t *ed;
939 struct usbd_device *dev;
940
941 if (ux == NULL) {
942 printf("ux NULL\n");
943 return;
944 }
945 pipe = ux->ux_xfer.ux_pipe;
946 if (pipe == NULL) {
947 printf("ux %p: done=%d pipe=NULL\n", ux, ux->ux_isdone);
948 return;
949 }
950 if (pipe->up_endpoint == NULL) {
951 printf("ux %p: done=%d pipe=%p pipe->up_endpoint=NULL\n",
952 ux, ux->ux_isdone, pipe);
953 return;
954 }
955 if (pipe->up_dev == NULL) {
956 printf("ux %p: done=%d pipe=%p pipe->up_dev=NULL\n",
957 ux, ux->ux_isdone, pipe);
958 return;
959 }
960 ed = pipe->up_endpoint->ue_edesc;
961 dev = pipe->up_dev;
962 printf("ux %p: done=%d dev=%p vid=0x%04x pid=0x%04x addr=%d pipe=%p ep=0x%02x attr=0x%02x\n",
963 ux, ux->ux_isdone, dev,
964 UGETW(dev->ud_ddesc.idVendor),
965 UGETW(dev->ud_ddesc.idProduct),
966 dev->ud_addr, pipe,
967 ed->bEndpointAddress, ed->bmAttributes);
968 }
969
970 void uhci_dump_iis(struct uhci_softc *sc);
971 void
972 uhci_dump_iis(struct uhci_softc *sc)
973 {
974 struct uhci_xfer *ux;
975
976 printf("interrupt list:\n");
977 TAILQ_FOREACH(ux, &sc->sc_intrhead, ux_list)
978 uhci_dump_ii(ux);
979 }
980
981 void iidump(void);
982 void iidump(void) { uhci_dump_iis(thesc); }
983
984 #endif
985
986 /*
987 * This routine is executed periodically and simulates interrupts
988 * from the root controller interrupt pipe for port status change.
989 */
990 void
991 uhci_poll_hub(void *addr)
992 {
993 struct usbd_xfer *xfer = addr;
994 struct usbd_pipe *pipe = xfer->ux_pipe;
995 uhci_softc_t *sc;
996 u_char *p;
997
998 UHCIHIST_FUNC(); UHCIHIST_CALLED();
999
1000 if (__predict_false(pipe->up_dev == NULL || pipe->up_dev->ud_bus == NULL))
1001 return; /* device has detached */
1002 sc = UHCI_PIPE2SC(pipe);
1003 callout_reset(&sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
1004
1005 p = xfer->ux_buf;
1006 p[0] = 0;
1007 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
1008 p[0] |= 1<<1;
1009 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
1010 p[0] |= 1<<2;
1011 if (p[0] == 0)
1012 /* No change, try again in a while */
1013 return;
1014
1015 xfer->ux_actlen = 1;
1016 xfer->ux_status = USBD_NORMAL_COMPLETION;
1017 mutex_enter(&sc->sc_lock);
1018 usb_transfer_complete(xfer);
1019 mutex_exit(&sc->sc_lock);
1020 }
1021
1022 void
1023 uhci_root_intr_done(struct usbd_xfer *xfer)
1024 {
1025 }
1026
1027 /*
1028 * Let the last QH loop back to the high speed control transfer QH.
1029 * This is what intel calls "bandwidth reclamation" and improves
1030 * USB performance a lot for some devices.
1031 * If we are already looping, just count it.
1032 */
1033 void
1034 uhci_add_loop(uhci_softc_t *sc)
1035 {
1036 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1037
1038 #ifdef UHCI_DEBUG
1039 if (uhcinoloop)
1040 return;
1041 #endif
1042 if (++sc->sc_loops == 1) {
1043 DPRINTFN(5, "add loop", 0, 0, 0, 0);
1044 /* Note, we don't loop back the soft pointer. */
1045 sc->sc_last_qh->qh.qh_hlink =
1046 htole32(sc->sc_hctl_start->physaddr | UHCI_PTR_QH);
1047 usb_syncmem(&sc->sc_last_qh->dma,
1048 sc->sc_last_qh->offs + offsetof(uhci_qh_t, qh_hlink),
1049 sizeof(sc->sc_last_qh->qh.qh_hlink),
1050 BUS_DMASYNC_PREWRITE);
1051 }
1052 }
1053
1054 void
1055 uhci_rem_loop(uhci_softc_t *sc)
1056 {
1057 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1058
1059 #ifdef UHCI_DEBUG
1060 if (uhcinoloop)
1061 return;
1062 #endif
1063 if (--sc->sc_loops == 0) {
1064 DPRINTFN(5, "remove loop", 0, 0, 0, 0);
1065 sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T);
1066 usb_syncmem(&sc->sc_last_qh->dma,
1067 sc->sc_last_qh->offs + offsetof(uhci_qh_t, qh_hlink),
1068 sizeof(sc->sc_last_qh->qh.qh_hlink),
1069 BUS_DMASYNC_PREWRITE);
1070 }
1071 }
1072
1073 /* Add high speed control QH, called with lock held. */
1074 void
1075 uhci_add_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1076 {
1077 uhci_soft_qh_t *eqh;
1078
1079 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1080
1081 KASSERT(mutex_owned(&sc->sc_lock));
1082
1083 DPRINTFN(10, "sqh %p", sqh, 0, 0, 0);
1084 eqh = sc->sc_hctl_end;
1085 usb_syncmem(&eqh->dma, eqh->offs + offsetof(uhci_qh_t, qh_hlink),
1086 sizeof(eqh->qh.qh_hlink),
1087 BUS_DMASYNC_POSTWRITE);
1088 sqh->hlink = eqh->hlink;
1089 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1090 usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
1091 BUS_DMASYNC_PREWRITE);
1092 eqh->hlink = sqh;
1093 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1094 sc->sc_hctl_end = sqh;
1095 usb_syncmem(&eqh->dma, eqh->offs + offsetof(uhci_qh_t, qh_hlink),
1096 sizeof(eqh->qh.qh_hlink), BUS_DMASYNC_PREWRITE);
1097 #ifdef UHCI_CTL_LOOP
1098 uhci_add_loop(sc);
1099 #endif
1100 }
1101
1102 /* Remove high speed control QH, called with lock held. */
1103 void
1104 uhci_remove_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1105 {
1106 uhci_soft_qh_t *pqh;
1107 uint32_t elink;
1108
1109 KASSERT(mutex_owned(&sc->sc_lock));
1110
1111 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1112 DPRINTFN(10, "sqh %p", sqh, 0, 0, 0);
1113 #ifdef UHCI_CTL_LOOP
1114 uhci_rem_loop(sc);
1115 #endif
1116 /*
1117 * The T bit should be set in the elink of the QH so that the HC
1118 * doesn't follow the pointer. This condition may fail if the
1119 * the transferred packet was short so that the QH still points
1120 * at the last used TD.
1121 * In this case we set the T bit and wait a little for the HC
1122 * to stop looking at the TD.
1123 * Note that if the TD chain is large enough, the controller
1124 * may still be looking at the chain at the end of this function.
1125 * uhci_free_std_chain() will make sure the controller stops
1126 * looking at it quickly, but until then we should not change
1127 * sqh->hlink.
1128 */
1129 usb_syncmem(&sqh->dma, sqh->offs + offsetof(uhci_qh_t, qh_elink),
1130 sizeof(sqh->qh.qh_elink),
1131 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1132 elink = le32toh(sqh->qh.qh_elink);
1133 usb_syncmem(&sqh->dma, sqh->offs + offsetof(uhci_qh_t, qh_elink),
1134 sizeof(sqh->qh.qh_elink), BUS_DMASYNC_PREREAD);
1135 if (!(elink & UHCI_PTR_T)) {
1136 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1137 usb_syncmem(&sqh->dma,
1138 sqh->offs + offsetof(uhci_qh_t, qh_elink),
1139 sizeof(sqh->qh.qh_elink),
1140 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
1141 delay(UHCI_QH_REMOVE_DELAY);
1142 }
1143
1144 pqh = uhci_find_prev_qh(sc->sc_hctl_start, sqh);
1145 usb_syncmem(&sqh->dma, sqh->offs + offsetof(uhci_qh_t, qh_hlink),
1146 sizeof(sqh->qh.qh_hlink), BUS_DMASYNC_POSTWRITE);
1147 pqh->hlink = sqh->hlink;
1148 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1149 usb_syncmem(&pqh->dma, pqh->offs + offsetof(uhci_qh_t, qh_hlink),
1150 sizeof(pqh->qh.qh_hlink),
1151 BUS_DMASYNC_PREWRITE);
1152 delay(UHCI_QH_REMOVE_DELAY);
1153 if (sc->sc_hctl_end == sqh)
1154 sc->sc_hctl_end = pqh;
1155 }
1156
1157 /* Add low speed control QH, called with lock held. */
1158 void
1159 uhci_add_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1160 {
1161 uhci_soft_qh_t *eqh;
1162
1163 KASSERT(mutex_owned(&sc->sc_lock));
1164
1165 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1166 DPRINTFN(10, "sqh %p", sqh, 0, 0, 0);
1167
1168 eqh = sc->sc_lctl_end;
1169 usb_syncmem(&eqh->dma, eqh->offs + offsetof(uhci_qh_t, qh_hlink),
1170 sizeof(eqh->qh.qh_hlink), BUS_DMASYNC_POSTWRITE);
1171 sqh->hlink = eqh->hlink;
1172 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1173 usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
1174 BUS_DMASYNC_PREWRITE);
1175 eqh->hlink = sqh;
1176 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1177 usb_syncmem(&eqh->dma, eqh->offs + offsetof(uhci_qh_t, qh_hlink),
1178 sizeof(eqh->qh.qh_hlink), BUS_DMASYNC_PREWRITE);
1179 sc->sc_lctl_end = sqh;
1180 }
1181
1182 /* Remove low speed control QH, called with lock held. */
1183 void
1184 uhci_remove_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1185 {
1186 uhci_soft_qh_t *pqh;
1187 uint32_t elink;
1188
1189 KASSERT(mutex_owned(&sc->sc_lock));
1190
1191 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1192 DPRINTFN(10, "sqh %p", sqh, 0, 0, 0);
1193
1194 /* See comment in uhci_remove_hs_ctrl() */
1195 usb_syncmem(&sqh->dma, sqh->offs + offsetof(uhci_qh_t, qh_elink),
1196 sizeof(sqh->qh.qh_elink),
1197 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1198 elink = le32toh(sqh->qh.qh_elink);
1199 usb_syncmem(&sqh->dma, sqh->offs + offsetof(uhci_qh_t, qh_elink),
1200 sizeof(sqh->qh.qh_elink), BUS_DMASYNC_PREREAD);
1201 if (!(elink & UHCI_PTR_T)) {
1202 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1203 usb_syncmem(&sqh->dma,
1204 sqh->offs + offsetof(uhci_qh_t, qh_elink),
1205 sizeof(sqh->qh.qh_elink),
1206 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
1207 delay(UHCI_QH_REMOVE_DELAY);
1208 }
1209 pqh = uhci_find_prev_qh(sc->sc_lctl_start, sqh);
1210 usb_syncmem(&sqh->dma, sqh->offs + offsetof(uhci_qh_t, qh_hlink),
1211 sizeof(sqh->qh.qh_hlink), BUS_DMASYNC_POSTWRITE);
1212 pqh->hlink = sqh->hlink;
1213 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1214 usb_syncmem(&pqh->dma, pqh->offs + offsetof(uhci_qh_t, qh_hlink),
1215 sizeof(pqh->qh.qh_hlink),
1216 BUS_DMASYNC_PREWRITE);
1217 delay(UHCI_QH_REMOVE_DELAY);
1218 if (sc->sc_lctl_end == sqh)
1219 sc->sc_lctl_end = pqh;
1220 }
1221
1222 /* Add bulk QH, called with lock held. */
1223 void
1224 uhci_add_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1225 {
1226 uhci_soft_qh_t *eqh;
1227
1228 KASSERT(mutex_owned(&sc->sc_lock));
1229
1230 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1231 DPRINTFN(10, "sqh %p", sqh, 0, 0, 0);
1232
1233 eqh = sc->sc_bulk_end;
1234 usb_syncmem(&eqh->dma, eqh->offs + offsetof(uhci_qh_t, qh_hlink),
1235 sizeof(eqh->qh.qh_hlink), BUS_DMASYNC_POSTWRITE);
1236 sqh->hlink = eqh->hlink;
1237 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1238 usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
1239 BUS_DMASYNC_PREWRITE);
1240 eqh->hlink = sqh;
1241 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1242 usb_syncmem(&eqh->dma, eqh->offs + offsetof(uhci_qh_t, qh_hlink),
1243 sizeof(eqh->qh.qh_hlink), BUS_DMASYNC_PREWRITE);
1244 sc->sc_bulk_end = sqh;
1245 uhci_add_loop(sc);
1246 }
1247
1248 /* Remove bulk QH, called with lock held. */
1249 void
1250 uhci_remove_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1251 {
1252 uhci_soft_qh_t *pqh;
1253
1254 KASSERT(mutex_owned(&sc->sc_lock));
1255
1256 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1257 DPRINTFN(10, "sqh %p", sqh, 0, 0, 0);
1258
1259 uhci_rem_loop(sc);
1260 /* See comment in uhci_remove_hs_ctrl() */
1261 usb_syncmem(&sqh->dma, sqh->offs + offsetof(uhci_qh_t, qh_elink),
1262 sizeof(sqh->qh.qh_elink),
1263 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1264 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1265 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1266 usb_syncmem(&sqh->dma,
1267 sqh->offs + offsetof(uhci_qh_t, qh_elink),
1268 sizeof(sqh->qh.qh_elink),
1269 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
1270 delay(UHCI_QH_REMOVE_DELAY);
1271 }
1272 pqh = uhci_find_prev_qh(sc->sc_bulk_start, sqh);
1273 usb_syncmem(&sqh->dma, sqh->offs + offsetof(uhci_qh_t, qh_hlink),
1274 sizeof(sqh->qh.qh_hlink), BUS_DMASYNC_POSTWRITE);
1275 pqh->hlink = sqh->hlink;
1276 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1277 usb_syncmem(&pqh->dma, pqh->offs + offsetof(uhci_qh_t, qh_hlink),
1278 sizeof(pqh->qh.qh_hlink), BUS_DMASYNC_PREWRITE);
1279 delay(UHCI_QH_REMOVE_DELAY);
1280 if (sc->sc_bulk_end == sqh)
1281 sc->sc_bulk_end = pqh;
1282 }
1283
1284 Static int uhci_intr1(uhci_softc_t *);
1285
1286 int
1287 uhci_intr(void *arg)
1288 {
1289 uhci_softc_t *sc = arg;
1290 int ret = 0;
1291
1292 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1293
1294 mutex_spin_enter(&sc->sc_intr_lock);
1295
1296 if (sc->sc_dying || !device_has_power(sc->sc_dev))
1297 goto done;
1298
1299 if (sc->sc_bus.ub_usepolling || UREAD2(sc, UHCI_INTR) == 0) {
1300 DPRINTFN(16, "ignored interrupt while polling", 0, 0, 0, 0);
1301 goto done;
1302 }
1303
1304 ret = uhci_intr1(sc);
1305
1306 done:
1307 mutex_spin_exit(&sc->sc_intr_lock);
1308 return ret;
1309 }
1310
1311 int
1312 uhci_intr1(uhci_softc_t *sc)
1313 {
1314 int status;
1315 int ack;
1316
1317 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1318
1319 #ifdef UHCI_DEBUG
1320 if (uhcidebug >= 15) {
1321 DPRINTF("sc %p", sc, 0, 0, 0);
1322 uhci_dumpregs(sc);
1323 }
1324 #endif
1325
1326 KASSERT(mutex_owned(&sc->sc_intr_lock));
1327
1328 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1329 /* Check if the interrupt was for us. */
1330 if (status == 0)
1331 return 0;
1332
1333 if (sc->sc_suspend != PWR_RESUME) {
1334 #ifdef DIAGNOSTIC
1335 printf("%s: interrupt while not operating ignored\n",
1336 device_xname(sc->sc_dev));
1337 #endif
1338 UWRITE2(sc, UHCI_STS, status); /* acknowledge the ints */
1339 return 0;
1340 }
1341
1342 ack = 0;
1343 if (status & UHCI_STS_USBINT)
1344 ack |= UHCI_STS_USBINT;
1345 if (status & UHCI_STS_USBEI)
1346 ack |= UHCI_STS_USBEI;
1347 if (status & UHCI_STS_RD) {
1348 ack |= UHCI_STS_RD;
1349 #ifdef UHCI_DEBUG
1350 printf("%s: resume detect\n", device_xname(sc->sc_dev));
1351 #endif
1352 }
1353 if (status & UHCI_STS_HSE) {
1354 ack |= UHCI_STS_HSE;
1355 printf("%s: host system error\n", device_xname(sc->sc_dev));
1356 }
1357 if (status & UHCI_STS_HCPE) {
1358 ack |= UHCI_STS_HCPE;
1359 printf("%s: host controller process error\n",
1360 device_xname(sc->sc_dev));
1361 }
1362
1363 /* When HCHalted=1 and Run/Stop=0 , it is normal */
1364 if ((status & UHCI_STS_HCH) && (UREAD2(sc, UHCI_CMD) & UHCI_CMD_RS)) {
1365 /* no acknowledge needed */
1366 if (!sc->sc_dying) {
1367 printf("%s: host controller halted\n",
1368 device_xname(sc->sc_dev));
1369 #ifdef UHCI_DEBUG
1370 uhci_dump_all(sc);
1371 #endif
1372 }
1373 sc->sc_dying = 1;
1374 }
1375
1376 if (!ack)
1377 return 0; /* nothing to acknowledge */
1378 UWRITE2(sc, UHCI_STS, ack); /* acknowledge the ints */
1379
1380 usb_schedsoftintr(&sc->sc_bus);
1381
1382 DPRINTFN(15, "sc %p done", sc, 0, 0, 0);
1383
1384 return 1;
1385 }
1386
1387 void
1388 uhci_softintr(void *v)
1389 {
1390 struct usbd_bus *bus = v;
1391 uhci_softc_t *sc = UHCI_BUS2SC(bus);
1392 struct uhci_xfer *ux, *nextux;
1393 ux_completeq_t cq;
1394
1395 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1396 DPRINTF("sc %p", sc, 0, 0, 0);
1397
1398 KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
1399
1400 TAILQ_INIT(&cq);
1401 /*
1402 * Interrupts on UHCI really suck. When the host controller
1403 * interrupts because a transfer is completed there is no
1404 * way of knowing which transfer it was. You can scan down
1405 * the TDs and QHs of the previous frame to limit the search,
1406 * but that assumes that the interrupt was not delayed by more
1407 * than 1 ms, which may not always be true (e.g. after debug
1408 * output on a slow console).
1409 * We scan all interrupt descriptors to see if any have
1410 * completed.
1411 */
1412 TAILQ_FOREACH_SAFE(ux, &sc->sc_intrhead, ux_list, nextux) {
1413 uhci_check_intr(sc, ux, &cq);
1414 }
1415
1416 /*
1417 * We abuse ux_list for the interrupt and complete lists and
1418 * interrupt transfers will get re-added here so use
1419 * the _SAFE version of TAILQ_FOREACH.
1420 */
1421 TAILQ_FOREACH_SAFE(ux, &cq, ux_list, nextux) {
1422 DPRINTF("ux %p", ux, 0, 0, 0);
1423 usb_transfer_complete(&ux->ux_xfer);
1424 }
1425
1426 if (sc->sc_softwake) {
1427 sc->sc_softwake = 0;
1428 cv_broadcast(&sc->sc_softwake_cv);
1429 }
1430 }
1431
1432 /* Check for an interrupt. */
1433 void
1434 uhci_check_intr(uhci_softc_t *sc, struct uhci_xfer *ux, ux_completeq_t *cqp)
1435 {
1436 uhci_soft_td_t *std, *fstd = NULL, *lstd = NULL;
1437 uint32_t status;
1438
1439 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1440 DPRINTFN(15, "ux %p", ux, 0, 0, 0);
1441
1442 KASSERT(ux != NULL);
1443
1444 struct usbd_xfer *xfer = &ux->ux_xfer;
1445 if (xfer->ux_status == USBD_CANCELLED ||
1446 xfer->ux_status == USBD_TIMEOUT) {
1447 DPRINTF("aborted xfer %p", xfer, 0, 0, 0);
1448 return;
1449 }
1450
1451 switch (ux->ux_type) {
1452 case UX_CTRL:
1453 fstd = ux->ux_setup;
1454 lstd = ux->ux_stat;
1455 break;
1456 case UX_BULK:
1457 case UX_INTR:
1458 case UX_ISOC:
1459 fstd = ux->ux_stdstart;
1460 lstd = ux->ux_stdend;
1461 break;
1462 default:
1463 KASSERT(false);
1464 break;
1465 }
1466 if (fstd == NULL)
1467 return;
1468
1469 KASSERT(lstd != NULL);
1470
1471 usb_syncmem(&lstd->dma,
1472 lstd->offs + offsetof(uhci_td_t, td_status),
1473 sizeof(lstd->td.td_status),
1474 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1475 status = le32toh(lstd->td.td_status);
1476 usb_syncmem(&lstd->dma,
1477 lstd->offs + offsetof(uhci_td_t, td_status),
1478 sizeof(lstd->td.td_status),
1479 BUS_DMASYNC_PREREAD);
1480
1481 /* If the last TD is not marked active we can complete */
1482 if (!(status & UHCI_TD_ACTIVE)) {
1483 done:
1484 DPRINTFN(12, "ux=%p done", ux, 0, 0, 0);
1485
1486 callout_stop(&xfer->ux_callout);
1487 uhci_idone(ux, cqp);
1488 return;
1489 }
1490
1491 /*
1492 * If the last TD is still active we need to check whether there
1493 * is an error somewhere in the middle, or whether there was a
1494 * short packet (SPD and not ACTIVE).
1495 */
1496 DPRINTFN(12, "active ux=%p", ux, 0, 0, 0);
1497 for (std = fstd; std != lstd; std = std->link.std) {
1498 usb_syncmem(&std->dma,
1499 std->offs + offsetof(uhci_td_t, td_status),
1500 sizeof(std->td.td_status),
1501 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1502 status = le32toh(std->td.td_status);
1503 usb_syncmem(&std->dma,
1504 std->offs + offsetof(uhci_td_t, td_status),
1505 sizeof(std->td.td_status), BUS_DMASYNC_PREREAD);
1506
1507 /* If there's an active TD the xfer isn't done. */
1508 if (status & UHCI_TD_ACTIVE) {
1509 DPRINTFN(12, "ux=%p std=%p still active",
1510 ux, std, 0, 0);
1511 return;
1512 }
1513
1514 /* Any kind of error makes the xfer done. */
1515 if (status & UHCI_TD_STALLED)
1516 goto done;
1517
1518 /*
1519 * If the data phase of a control transfer is short, we need
1520 * to complete the status stage
1521 */
1522
1523 if ((status & UHCI_TD_SPD) && ux->ux_type == UX_CTRL) {
1524 struct uhci_pipe *upipe =
1525 UHCI_PIPE2UPIPE(xfer->ux_pipe);
1526 uhci_soft_qh_t *sqh = upipe->ctrl.sqh;
1527 uhci_soft_td_t *stat = upipe->ctrl.stat;
1528
1529 DPRINTFN(12, "ux=%p std=%p control status"
1530 "phase needs completion", ux, ux->ux_stdstart, 0, 0);
1531
1532 sqh->qh.qh_elink =
1533 htole32(stat->physaddr | UHCI_PTR_TD);
1534 usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
1535 BUS_DMASYNC_PREWRITE);
1536 break;
1537 }
1538
1539 /* We want short packets, and it is short: it's done */
1540 usb_syncmem(&std->dma,
1541 std->offs + offsetof(uhci_td_t, td_token),
1542 sizeof(std->td.td_token),
1543 BUS_DMASYNC_POSTWRITE);
1544
1545 if ((status & UHCI_TD_SPD) &&
1546 UHCI_TD_GET_ACTLEN(status) <
1547 UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token))) {
1548 goto done;
1549 }
1550 }
1551 }
1552
1553 /* Called with USB lock held. */
1554 void
1555 uhci_idone(struct uhci_xfer *ux, ux_completeq_t *cqp)
1556 {
1557 struct usbd_xfer *xfer = &ux->ux_xfer;
1558 uhci_softc_t *sc __diagused = UHCI_XFER2SC(xfer);
1559 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(xfer->ux_pipe);
1560 uhci_soft_td_t *std;
1561 uint32_t status = 0, nstatus;
1562 int actlen;
1563
1564 KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
1565
1566 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1567 DPRINTFN(12, "ux=%p", ux, 0, 0, 0);
1568
1569 #ifdef DIAGNOSTIC
1570 #ifdef UHCI_DEBUG
1571 if (ux->ux_isdone) {
1572 DPRINTF("--- dump start ---", 0, 0, 0, 0);
1573 uhci_dump_ii(ux);
1574 DPRINTF("--- dump end ---", 0, 0, 0, 0);
1575 }
1576 #endif
1577 KASSERT(!ux->ux_isdone);
1578 KASSERTMSG(!ux->ux_isdone, "xfer %p type %d status %d", xfer,
1579 ux->ux_type, xfer->ux_status);
1580 ux->ux_isdone = true;
1581 #endif
1582
1583 if (xfer->ux_nframes != 0) {
1584 /* Isoc transfer, do things differently. */
1585 uhci_soft_td_t **stds = upipe->isoc.stds;
1586 int i, n, nframes, len;
1587
1588 DPRINTFN(5, "ux=%p isoc ready", ux, 0, 0, 0);
1589
1590 nframes = xfer->ux_nframes;
1591 actlen = 0;
1592 n = ux->ux_curframe;
1593 for (i = 0; i < nframes; i++) {
1594 std = stds[n];
1595 #ifdef UHCI_DEBUG
1596 if (uhcidebug >= 5) {
1597 DPRINTF("isoc TD %d", i, 0, 0, 0);
1598 DPRINTF("--- dump start ---", 0, 0, 0, 0);
1599 uhci_dump_td(std);
1600 DPRINTF("--- dump end ---", 0, 0, 0, 0);
1601 }
1602 #endif
1603 if (++n >= UHCI_VFRAMELIST_COUNT)
1604 n = 0;
1605 usb_syncmem(&std->dma,
1606 std->offs + offsetof(uhci_td_t, td_status),
1607 sizeof(std->td.td_status),
1608 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1609 status = le32toh(std->td.td_status);
1610 len = UHCI_TD_GET_ACTLEN(status);
1611 xfer->ux_frlengths[i] = len;
1612 actlen += len;
1613 }
1614 upipe->isoc.inuse -= nframes;
1615 xfer->ux_actlen = actlen;
1616 xfer->ux_status = USBD_NORMAL_COMPLETION;
1617 goto end;
1618 }
1619
1620 #ifdef UHCI_DEBUG
1621 DPRINTFN(10, "ux=%p, xfer=%p, pipe=%p ready", ux, xfer, upipe, 0);
1622 if (uhcidebug >= 10) {
1623 DPRINTF("--- dump start ---", 0, 0, 0, 0);
1624 uhci_dump_tds(ux->ux_stdstart);
1625 DPRINTF("--- dump end ---", 0, 0, 0, 0);
1626 }
1627 #endif
1628
1629 /* The transfer is done, compute actual length and status. */
1630 actlen = 0;
1631 for (std = ux->ux_stdstart; std != NULL; std = std->link.std) {
1632 usb_syncmem(&std->dma, std->offs, sizeof(std->td),
1633 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1634 nstatus = le32toh(std->td.td_status);
1635 if (nstatus & UHCI_TD_ACTIVE)
1636 break;
1637
1638 status = nstatus;
1639 if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
1640 UHCI_TD_PID_SETUP)
1641 actlen += UHCI_TD_GET_ACTLEN(status);
1642 else {
1643 /*
1644 * UHCI will report CRCTO in addition to a STALL or NAK
1645 * for a SETUP transaction. See section 3.2.2, "TD
1646 * CONTROL AND STATUS".
1647 */
1648 if (status & (UHCI_TD_STALLED | UHCI_TD_NAK))
1649 status &= ~UHCI_TD_CRCTO;
1650 }
1651 }
1652 /* If there are left over TDs we need to update the toggle. */
1653 if (std != NULL)
1654 upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
1655
1656 status &= UHCI_TD_ERROR;
1657 DPRINTFN(10, "actlen=%d, status=0x%x", actlen, status, 0, 0);
1658 xfer->ux_actlen = actlen;
1659 if (status != 0) {
1660
1661 DPRINTFN((status == UHCI_TD_STALLED) * 10,
1662 "error, addr=%d, endpt=0x%02x",
1663 xfer->ux_pipe->up_dev->ud_addr,
1664 xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress,
1665 0, 0);
1666 DPRINTFN((status == UHCI_TD_STALLED) * 10,
1667 "bitstuff=%d crcto =%d nak =%d babble =%d",
1668 !!(status & UHCI_TD_BITSTUFF),
1669 !!(status & UHCI_TD_CRCTO),
1670 !!(status & UHCI_TD_NAK),
1671 !!(status & UHCI_TD_BABBLE));
1672 DPRINTFN((status == UHCI_TD_STALLED) * 10,
1673 "dbuffer =%d stalled =%d active =%d",
1674 !!(status & UHCI_TD_DBUFFER),
1675 !!(status & UHCI_TD_STALLED),
1676 !!(status & UHCI_TD_ACTIVE),
1677 0);
1678
1679 if (status == UHCI_TD_STALLED)
1680 xfer->ux_status = USBD_STALLED;
1681 else
1682 xfer->ux_status = USBD_IOERROR; /* more info XXX */
1683 } else {
1684 xfer->ux_status = USBD_NORMAL_COMPLETION;
1685 }
1686
1687 end:
1688 uhci_del_intr_list(sc, ux);
1689 if (cqp)
1690 TAILQ_INSERT_TAIL(cqp, ux, ux_list);
1691
1692 KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
1693 DPRINTFN(12, "ux=%p done", ux, 0, 0, 0);
1694 }
1695
1696 /*
1697 * Called when a request does not complete.
1698 */
1699 void
1700 uhci_timeout(void *addr)
1701 {
1702 struct usbd_xfer *xfer = addr;
1703 struct uhci_xfer *uxfer = UHCI_XFER2UXFER(xfer);
1704 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
1705
1706 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1707
1708 DPRINTF("uxfer %p", uxfer, 0, 0, 0);
1709
1710 if (sc->sc_dying) {
1711 mutex_enter(&sc->sc_lock);
1712 uhci_abort_xfer(xfer, USBD_TIMEOUT);
1713 mutex_exit(&sc->sc_lock);
1714 return;
1715 }
1716
1717 /* Execute the abort in a process context. */
1718 usb_init_task(&uxfer->ux_aborttask, uhci_timeout_task, xfer,
1719 USB_TASKQ_MPSAFE);
1720 usb_add_task(uxfer->ux_xfer.ux_pipe->up_dev, &uxfer->ux_aborttask,
1721 USB_TASKQ_HC);
1722 }
1723
1724 void
1725 uhci_timeout_task(void *addr)
1726 {
1727 struct usbd_xfer *xfer = addr;
1728 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
1729
1730 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1731
1732 DPRINTF("xfer=%p", xfer, 0, 0, 0);
1733
1734 mutex_enter(&sc->sc_lock);
1735 uhci_abort_xfer(xfer, USBD_TIMEOUT);
1736 mutex_exit(&sc->sc_lock);
1737 }
1738
1739 /*
1740 * Wait here until controller claims to have an interrupt.
1741 * Then call uhci_intr and return. Use timeout to avoid waiting
1742 * too long.
1743 * Only used during boot when interrupts are not enabled yet.
1744 */
1745 void
1746 uhci_waitintr(uhci_softc_t *sc, struct usbd_xfer *xfer)
1747 {
1748 int timo = xfer->ux_timeout;
1749 struct uhci_xfer *ux;
1750
1751 mutex_enter(&sc->sc_lock);
1752
1753 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1754 DPRINTFN(10, "timeout = %dms", timo, 0, 0, 0);
1755
1756 xfer->ux_status = USBD_IN_PROGRESS;
1757 for (; timo >= 0; timo--) {
1758 usb_delay_ms_locked(&sc->sc_bus, 1, &sc->sc_lock);
1759 DPRINTFN(20, "0x%04x",
1760 UREAD2(sc, UHCI_STS), 0, 0, 0);
1761 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
1762 mutex_spin_enter(&sc->sc_intr_lock);
1763 uhci_intr1(sc);
1764 mutex_spin_exit(&sc->sc_intr_lock);
1765 if (xfer->ux_status != USBD_IN_PROGRESS)
1766 goto done;
1767 }
1768 }
1769
1770 /* Timeout */
1771 DPRINTF("timeout", 0, 0, 0, 0);
1772 TAILQ_FOREACH(ux, &sc->sc_intrhead, ux_list)
1773 if (&ux->ux_xfer == xfer)
1774 break;
1775
1776 KASSERT(ux != NULL);
1777
1778 uhci_idone(ux, NULL);
1779 usb_transfer_complete(&ux->ux_xfer);
1780
1781 done:
1782 mutex_exit(&sc->sc_lock);
1783 }
1784
1785 void
1786 uhci_poll(struct usbd_bus *bus)
1787 {
1788 uhci_softc_t *sc = UHCI_BUS2SC(bus);
1789
1790 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
1791 mutex_spin_enter(&sc->sc_intr_lock);
1792 uhci_intr1(sc);
1793 mutex_spin_exit(&sc->sc_intr_lock);
1794 }
1795 }
1796
1797 void
1798 uhci_reset(uhci_softc_t *sc)
1799 {
1800 int n;
1801
1802 UHCICMD(sc, UHCI_CMD_HCRESET);
1803 /* The reset bit goes low when the controller is done. */
1804 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1805 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1806 usb_delay_ms(&sc->sc_bus, 1);
1807 if (n >= UHCI_RESET_TIMEOUT)
1808 printf("%s: controller did not reset\n",
1809 device_xname(sc->sc_dev));
1810 }
1811
1812 usbd_status
1813 uhci_run(uhci_softc_t *sc, int run, int locked)
1814 {
1815 int n, running;
1816 uint16_t cmd;
1817
1818 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1819
1820 run = run != 0;
1821 if (!locked)
1822 mutex_spin_enter(&sc->sc_intr_lock);
1823
1824 DPRINTF("setting run=%d", run, 0, 0, 0);
1825 cmd = UREAD2(sc, UHCI_CMD);
1826 if (run)
1827 cmd |= UHCI_CMD_RS;
1828 else
1829 cmd &= ~UHCI_CMD_RS;
1830 UHCICMD(sc, cmd);
1831 for (n = 0; n < 10; n++) {
1832 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1833 /* return when we've entered the state we want */
1834 if (run == running) {
1835 if (!locked)
1836 mutex_spin_exit(&sc->sc_intr_lock);
1837 DPRINTF("done cmd=0x%x sts=0x%x",
1838 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS), 0, 0);
1839 return USBD_NORMAL_COMPLETION;
1840 }
1841 usb_delay_ms_locked(&sc->sc_bus, 1, &sc->sc_intr_lock);
1842 }
1843 if (!locked)
1844 mutex_spin_exit(&sc->sc_intr_lock);
1845 printf("%s: cannot %s\n", device_xname(sc->sc_dev),
1846 run ? "start" : "stop");
1847 return USBD_IOERROR;
1848 }
1849
1850 /*
1851 * Memory management routines.
1852 * uhci_alloc_std allocates TDs
1853 * uhci_alloc_sqh allocates QHs
1854 * These two routines do their own free list management,
1855 * partly for speed, partly because allocating DMAable memory
1856 * has page size granularity so much memory would be wasted if
1857 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1858 */
1859
1860 uhci_soft_td_t *
1861 uhci_alloc_std(uhci_softc_t *sc)
1862 {
1863 uhci_soft_td_t *std;
1864 usbd_status err;
1865 int i, offs;
1866 usb_dma_t dma;
1867
1868 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1869
1870 mutex_enter(&sc->sc_lock);
1871 if (sc->sc_freetds == NULL) {
1872 DPRINTFN(2, "allocating chunk", 0, 0, 0, 0);
1873 mutex_exit(&sc->sc_lock);
1874
1875 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1876 UHCI_TD_ALIGN, &dma);
1877 if (err)
1878 return NULL;
1879
1880 mutex_enter(&sc->sc_lock);
1881 for (i = 0; i < UHCI_STD_CHUNK; i++) {
1882 offs = i * UHCI_STD_SIZE;
1883 std = KERNADDR(&dma, offs);
1884 std->physaddr = DMAADDR(&dma, offs);
1885 std->dma = dma;
1886 std->offs = offs;
1887 std->link.std = sc->sc_freetds;
1888 sc->sc_freetds = std;
1889 }
1890 }
1891 std = sc->sc_freetds;
1892 sc->sc_freetds = std->link.std;
1893 mutex_exit(&sc->sc_lock);
1894
1895 memset(&std->td, 0, sizeof(uhci_td_t));
1896
1897 return std;
1898 }
1899
1900 #define TD_IS_FREE 0x12345678
1901
1902 void
1903 uhci_free_std_locked(uhci_softc_t *sc, uhci_soft_td_t *std)
1904 {
1905 KASSERT(mutex_owned(&sc->sc_lock));
1906
1907 #ifdef DIAGNOSTIC
1908 if (le32toh(std->td.td_token) == TD_IS_FREE) {
1909 printf("%s: freeing free TD %p\n", __func__, std);
1910 return;
1911 }
1912 std->td.td_token = htole32(TD_IS_FREE);
1913 #endif
1914
1915 std->link.std = sc->sc_freetds;
1916 sc->sc_freetds = std;
1917 }
1918
1919 void
1920 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
1921 {
1922 mutex_enter(&sc->sc_lock);
1923 uhci_free_std_locked(sc, std);
1924 mutex_exit(&sc->sc_lock);
1925 }
1926
1927 uhci_soft_qh_t *
1928 uhci_alloc_sqh(uhci_softc_t *sc)
1929 {
1930 uhci_soft_qh_t *sqh;
1931 usbd_status err;
1932 int i, offs;
1933 usb_dma_t dma;
1934
1935 UHCIHIST_FUNC(); UHCIHIST_CALLED();
1936
1937 mutex_enter(&sc->sc_lock);
1938 if (sc->sc_freeqhs == NULL) {
1939 DPRINTFN(2, "allocating chunk", 0, 0, 0, 0);
1940 mutex_exit(&sc->sc_lock);
1941
1942 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1943 UHCI_QH_ALIGN, &dma);
1944 if (err)
1945 return NULL;
1946
1947 mutex_enter(&sc->sc_lock);
1948 for (i = 0; i < UHCI_SQH_CHUNK; i++) {
1949 offs = i * UHCI_SQH_SIZE;
1950 sqh = KERNADDR(&dma, offs);
1951 sqh->physaddr = DMAADDR(&dma, offs);
1952 sqh->dma = dma;
1953 sqh->offs = offs;
1954 sqh->hlink = sc->sc_freeqhs;
1955 sc->sc_freeqhs = sqh;
1956 }
1957 }
1958 sqh = sc->sc_freeqhs;
1959 sc->sc_freeqhs = sqh->hlink;
1960 mutex_exit(&sc->sc_lock);
1961
1962 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1963
1964 return sqh;
1965 }
1966
1967 void
1968 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1969 {
1970 KASSERT(mutex_owned(&sc->sc_lock));
1971
1972 sqh->hlink = sc->sc_freeqhs;
1973 sc->sc_freeqhs = sqh;
1974 }
1975
1976 #if 0
1977 void
1978 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
1979 uhci_soft_td_t *stdend)
1980 {
1981 uhci_soft_td_t *p;
1982 uint32_t td_link;
1983
1984 /*
1985 * to avoid race condition with the controller which may be looking
1986 * at this chain, we need to first invalidate all links, and
1987 * then wait for the controller to move to another queue
1988 */
1989 for (p = std; p != stdend; p = p->link.std) {
1990 usb_syncmem(&p->dma,
1991 p->offs + offsetof(uhci_td_t, td_link),
1992 sizeof(p->td.td_link),
1993 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1994 td_link = le32toh(p->td.td_link);
1995 usb_syncmem(&p->dma,
1996 p->offs + offsetof(uhci_td_t, td_link),
1997 sizeof(p->td.td_link),
1998 BUS_DMASYNC_PREREAD);
1999 if ((td_link & UHCI_PTR_T) == 0) {
2000 p->td.td_link = htole32(UHCI_PTR_T);
2001 usb_syncmem(&p->dma,
2002 p->offs + offsetof(uhci_td_t, td_link),
2003 sizeof(p->td.td_link),
2004 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2005 }
2006 }
2007 delay(UHCI_QH_REMOVE_DELAY);
2008
2009 for (; std != stdend; std = p) {
2010 p = std->link.std;
2011 uhci_free_std(sc, std);
2012 }
2013 }
2014 #endif
2015
2016 int
2017 uhci_alloc_std_chain(uhci_softc_t *sc, struct usbd_xfer *xfer, int len,
2018 int rd, uhci_soft_td_t **sp)
2019 {
2020 struct uhci_xfer *uxfer = UHCI_XFER2UXFER(xfer);
2021 uint16_t flags = xfer->ux_flags;
2022 uhci_soft_td_t *p;
2023
2024 UHCIHIST_FUNC(); UHCIHIST_CALLED();
2025
2026 DPRINTFN(8, "xfer=%p pipe=%p", xfer, xfer->ux_pipe, 0, 0);
2027
2028 ASSERT_SLEEPABLE();
2029 KASSERT(sp);
2030
2031 int maxp = UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize);
2032 if (maxp == 0) {
2033 printf("%s: maxp=0\n", __func__);
2034 return EINVAL;
2035 }
2036 size_t ntd = (len + maxp - 1) / maxp;
2037 if (!rd && (flags & USBD_FORCE_SHORT_XFER)) {
2038 ntd++;
2039 }
2040 DPRINTFN(10, "maxp=%d ntd=%d", maxp, ntd, 0, 0);
2041
2042 uxfer->ux_stds = NULL;
2043 uxfer->ux_nstd = ntd;
2044 p = NULL;
2045 if (ntd == 0) {
2046 *sp = NULL;
2047 DPRINTF("ntd=0", 0, 0, 0, 0);
2048 return 0;
2049 }
2050 uxfer->ux_stds = kmem_alloc(sizeof(uhci_soft_td_t *) * ntd,
2051 KM_SLEEP);
2052
2053 ntd--;
2054 for (int i = ntd; i >= 0; i--) {
2055 p = uhci_alloc_std(sc);
2056 if (p == NULL) {
2057 uhci_free_stds(sc, uxfer);
2058 kmem_free(uxfer->ux_stds,
2059 sizeof(uhci_soft_td_t *) * ntd);
2060 return ENOMEM;
2061 }
2062 uxfer->ux_stds[i] = p;
2063 }
2064
2065 *sp = uxfer->ux_stds[0];
2066
2067 return 0;
2068 }
2069
2070 Static void
2071 uhci_free_stds(uhci_softc_t *sc, struct uhci_xfer *ux)
2072 {
2073 UHCIHIST_FUNC(); UHCIHIST_CALLED();
2074
2075 DPRINTFN(8, "ux=%p", ux, 0, 0, 0);
2076
2077 mutex_enter(&sc->sc_lock);
2078 for (size_t i = 0; i < ux->ux_nstd; i++) {
2079 uhci_soft_td_t *std = ux->ux_stds[i];
2080 #ifdef DIAGNOSTIC
2081 if (le32toh(std->td.td_token) == TD_IS_FREE) {
2082 printf("%s: freeing free TD %p\n", __func__, std);
2083 return;
2084 }
2085 std->td.td_token = htole32(TD_IS_FREE);
2086 #endif
2087 ux->ux_stds[i]->link.std = sc->sc_freetds;
2088 sc->sc_freetds = std;
2089 }
2090 mutex_exit(&sc->sc_lock);
2091 }
2092
2093
2094 Static void
2095 uhci_reset_std_chain(uhci_softc_t *sc, struct usbd_xfer *xfer,
2096 int length, int isread, int *toggle, uhci_soft_td_t **lstd)
2097 {
2098 struct uhci_xfer *uxfer = UHCI_XFER2UXFER(xfer);
2099 struct usbd_pipe *pipe = xfer->ux_pipe;
2100 usb_dma_t *dma = &xfer->ux_dmabuf;
2101 uint16_t flags = xfer->ux_flags;
2102 uhci_soft_td_t *std, *prev;
2103 int len = length;
2104 int tog = *toggle;
2105 int maxp;
2106 uint32_t status;
2107 size_t i;
2108
2109 UHCIHIST_FUNC(); UHCIHIST_CALLED();
2110 DPRINTFN(8, "xfer=%p len %d isread %d toggle %d", xfer,
2111 len, isread, *toggle);
2112
2113 KASSERT(len != 0 || (!isread && (flags & USBD_FORCE_SHORT_XFER)));
2114
2115 maxp = UGETW(pipe->up_endpoint->ue_edesc->wMaxPacketSize);
2116 KASSERT(maxp != 0);
2117
2118 int addr = xfer->ux_pipe->up_dev->ud_addr;
2119 int endpt = xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress;
2120
2121 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
2122 if (pipe->up_dev->ud_speed == USB_SPEED_LOW)
2123 status |= UHCI_TD_LS;
2124 if (flags & USBD_SHORT_XFER_OK)
2125 status |= UHCI_TD_SPD;
2126 usb_syncmem(dma, 0, len,
2127 isread ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2128 std = prev = NULL;
2129 for (i = 0; len != 0 && i < uxfer->ux_nstd; i++, prev = std) {
2130 int l = len;
2131 std = uxfer->ux_stds[i];
2132 if (l > maxp)
2133 l = maxp;
2134
2135 if (prev) {
2136 prev->link.std = std;
2137 prev->td.td_link = htole32(
2138 std->physaddr | UHCI_PTR_VF | UHCI_PTR_TD
2139 );
2140 usb_syncmem(&prev->dma, prev->offs, sizeof(prev->td),
2141 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2142 }
2143
2144 usb_syncmem(&std->dma, std->offs, sizeof(std->td),
2145 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
2146
2147 std->td.td_link = htole32(UHCI_PTR_T | UHCI_PTR_VF | UHCI_PTR_TD);
2148 std->td.td_status = htole32(status);
2149 std->td.td_token = htole32(
2150 UHCI_TD_SET_ENDPT(UE_GET_ADDR(endpt)) |
2151 UHCI_TD_SET_DEVADDR(addr) |
2152 UHCI_TD_SET_PID(isread ? UHCI_TD_PID_IN : UHCI_TD_PID_OUT) |
2153 UHCI_TD_SET_DT(tog) |
2154 UHCI_TD_SET_MAXLEN(l)
2155 );
2156 std->td.td_buffer = htole32(DMAADDR(dma, i * maxp));
2157
2158 std->link.std = NULL;
2159
2160 usb_syncmem(&std->dma, std->offs, sizeof(std->td),
2161 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2162 tog ^= 1;
2163
2164 len -= l;
2165 }
2166 KASSERTMSG(len == 0, "xfer %p alen %d len %d mps %d ux_nqtd %zu i %zu",
2167 xfer, length, len, maxp, uxfer->ux_nstd, i);
2168
2169 if (!isread &&
2170 (flags & USBD_FORCE_SHORT_XFER) &&
2171 length % maxp == 0) {
2172 /* Force a 0 length transfer at the end. */
2173 KASSERTMSG(i < uxfer->ux_nstd, "i=%zu nstd=%zu", i,
2174 uxfer->ux_nstd);
2175 std = uxfer->ux_stds[i++];
2176
2177 std->td.td_link = htole32(UHCI_PTR_T | UHCI_PTR_VF | UHCI_PTR_TD);
2178 std->td.td_status = htole32(status);
2179 std->td.td_token = htole32(
2180 UHCI_TD_SET_ENDPT(UE_GET_ADDR(endpt)) |
2181 UHCI_TD_SET_DEVADDR(addr) |
2182 UHCI_TD_SET_PID(UHCI_TD_PID_OUT) |
2183 UHCI_TD_SET_DT(tog) |
2184 UHCI_TD_SET_MAXLEN(0)
2185 );
2186 std->td.td_buffer = 0;
2187 usb_syncmem(&std->dma, std->offs, sizeof(std->td),
2188 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2189
2190 std->link.std = NULL;
2191 if (prev) {
2192 prev->link.std = std;
2193 prev->td.td_link = htole32(
2194 std->physaddr | UHCI_PTR_VF | UHCI_PTR_TD
2195 );
2196 usb_syncmem(&prev->dma, prev->offs, sizeof(prev->td),
2197 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2198 }
2199 tog ^= 1;
2200 }
2201 *lstd = std;
2202 *toggle = tog;
2203 }
2204
2205 void
2206 uhci_device_clear_toggle(struct usbd_pipe *pipe)
2207 {
2208 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(pipe);
2209 upipe->nexttoggle = 0;
2210 }
2211
2212 void
2213 uhci_noop(struct usbd_pipe *pipe)
2214 {
2215 }
2216
2217 int
2218 uhci_device_bulk_init(struct usbd_xfer *xfer)
2219 {
2220 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
2221 struct uhci_xfer *uxfer = UHCI_XFER2UXFER(xfer);
2222 usb_endpoint_descriptor_t *ed = xfer->ux_pipe->up_endpoint->ue_edesc;
2223 int endpt = ed->bEndpointAddress;
2224 int isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2225 int len = xfer->ux_bufsize;
2226 int err = 0;
2227
2228
2229 UHCIHIST_FUNC(); UHCIHIST_CALLED();
2230 DPRINTFN(3, "xfer=%p len=%d flags=%d", xfer, len, xfer->ux_flags, 0);
2231
2232 if (sc->sc_dying)
2233 return USBD_IOERROR;
2234
2235 KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
2236
2237 uxfer->ux_type = UX_BULK;
2238 err = uhci_alloc_std_chain(sc, xfer, len, isread, &uxfer->ux_stdstart);
2239 if (err)
2240 return err;
2241
2242 #ifdef UHCI_DEBUG
2243 if (uhcidebug >= 10) {
2244 DPRINTF("--- dump start ---", 0, 0, 0, 0);
2245 uhci_dump_tds(uxfer->ux_stdstart);
2246 DPRINTF("--- dump end ---", 0, 0, 0, 0);
2247 }
2248 #endif
2249
2250 return 0;
2251 }
2252
2253 Static void
2254 uhci_device_bulk_fini(struct usbd_xfer *xfer)
2255 {
2256 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
2257 struct uhci_xfer *ux = UHCI_XFER2UXFER(xfer);
2258
2259 KASSERT(ux->ux_type == UX_BULK);
2260
2261 uhci_free_stds(sc, ux);
2262 if (ux->ux_nstd)
2263 kmem_free(ux->ux_stds, sizeof(uhci_soft_td_t *) * ux->ux_nstd);
2264 }
2265
2266 usbd_status
2267 uhci_device_bulk_transfer(struct usbd_xfer *xfer)
2268 {
2269 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
2270 usbd_status err;
2271
2272 /* Insert last in queue. */
2273 mutex_enter(&sc->sc_lock);
2274 err = usb_insert_transfer(xfer);
2275 mutex_exit(&sc->sc_lock);
2276 if (err)
2277 return err;
2278
2279 /*
2280 * Pipe isn't running (otherwise err would be USBD_INPROG),
2281 * so start it first.
2282 */
2283 return uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
2284 }
2285
2286 usbd_status
2287 uhci_device_bulk_start(struct usbd_xfer *xfer)
2288 {
2289 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(xfer->ux_pipe);
2290 struct uhci_xfer *ux = UHCI_XFER2UXFER(xfer);
2291 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
2292 uhci_soft_td_t *data, *dataend;
2293 uhci_soft_qh_t *sqh;
2294 int len;
2295 int endpt;
2296 int isread;
2297
2298 UHCIHIST_FUNC(); UHCIHIST_CALLED();
2299 DPRINTFN(3, "xfer=%p len=%d flags=%d", xfer, xfer->ux_length,
2300 xfer->ux_flags, 0);
2301
2302 if (sc->sc_dying)
2303 return USBD_IOERROR;
2304
2305 KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
2306 KASSERT(xfer->ux_length <= xfer->ux_bufsize);
2307
2308 len = xfer->ux_length;
2309 endpt = upipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
2310 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2311 sqh = upipe->bulk.sqh;
2312
2313 /* Take lock here to protect nexttoggle */
2314 mutex_enter(&sc->sc_lock);
2315
2316 uhci_reset_std_chain(sc, xfer, len, isread, &upipe->nexttoggle,
2317 &dataend);
2318
2319 data = ux->ux_stdstart;
2320 ux->ux_stdend = dataend;
2321 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2322 usb_syncmem(&dataend->dma,
2323 dataend->offs + offsetof(uhci_td_t, td_status),
2324 sizeof(dataend->td.td_status),
2325 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2326
2327 #ifdef UHCI_DEBUG
2328 if (uhcidebug >= 10) {
2329 DPRINTF("--- dump start ---", 0, 0, 0, 0);
2330 DPRINTFN(10, "before transfer", 0, 0, 0, 0);
2331 uhci_dump_tds(data);
2332 DPRINTF("--- dump end ---", 0, 0, 0, 0);
2333 }
2334 #endif
2335
2336 KASSERT(ux->ux_isdone);
2337 #ifdef DIAGNOSTIC
2338 ux->ux_isdone = false;
2339 #endif
2340
2341 sqh->elink = data;
2342 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2343 /* uhci_add_bulk() will do usb_syncmem(sqh) */
2344
2345 uhci_add_bulk(sc, sqh);
2346 uhci_add_intr_list(sc, ux);
2347
2348 if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
2349 callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
2350 uhci_timeout, xfer);
2351 }
2352 xfer->ux_status = USBD_IN_PROGRESS;
2353 mutex_exit(&sc->sc_lock);
2354
2355 if (sc->sc_bus.ub_usepolling)
2356 uhci_waitintr(sc, xfer);
2357
2358 return USBD_IN_PROGRESS;
2359 }
2360
2361 /* Abort a device bulk request. */
2362 void
2363 uhci_device_bulk_abort(struct usbd_xfer *xfer)
2364 {
2365 uhci_softc_t *sc __diagused = UHCI_XFER2SC(xfer);
2366
2367 KASSERT(mutex_owned(&sc->sc_lock));
2368
2369 UHCIHIST_FUNC(); UHCIHIST_CALLED();
2370
2371 uhci_abort_xfer(xfer, USBD_CANCELLED);
2372 }
2373
2374 /*
2375 * Abort a device request.
2376 * If this routine is called at splusb() it guarantees that the request
2377 * will be removed from the hardware scheduling and that the callback
2378 * for it will be called with USBD_CANCELLED status.
2379 * It's impossible to guarantee that the requested transfer will not
2380 * have happened since the hardware runs concurrently.
2381 * If the transaction has already happened we rely on the ordinary
2382 * interrupt processing to process it.
2383 * XXX This is most probably wrong.
2384 * XXXMRG this doesn't make sense anymore.
2385 */
2386 void
2387 uhci_abort_xfer(struct usbd_xfer *xfer, usbd_status status)
2388 {
2389 struct uhci_xfer *ux = UHCI_XFER2UXFER(xfer);
2390 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(xfer->ux_pipe);
2391 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
2392 uhci_soft_td_t *std;
2393 int wake;
2394
2395 UHCIHIST_FUNC(); UHCIHIST_CALLED();
2396 DPRINTFN(1,"xfer=%p, status=%d", xfer, status, 0, 0);
2397
2398 KASSERT(mutex_owned(&sc->sc_lock));
2399 ASSERT_SLEEPABLE();
2400
2401 if (sc->sc_dying) {
2402 /* If we're dying, just do the software part. */
2403 xfer->ux_status = status; /* make software ignore it */
2404 callout_stop(&xfer->ux_callout);
2405 usb_transfer_complete(xfer);
2406 return;
2407 }
2408
2409 /*
2410 * If an abort is already in progress then just wait for it to
2411 * complete and return.
2412 */
2413 if (xfer->ux_hcflags & UXFER_ABORTING) {
2414 DPRINTFN(2, "already aborting", 0, 0, 0, 0);
2415 #ifdef DIAGNOSTIC
2416 if (status == USBD_TIMEOUT)
2417 printf("%s: TIMEOUT while aborting\n", __func__);
2418 #endif
2419 /* Override the status which might be USBD_TIMEOUT. */
2420 xfer->ux_status = status;
2421 DPRINTFN(2, "waiting for abort to finish", 0, 0, 0, 0);
2422 xfer->ux_hcflags |= UXFER_ABORTWAIT;
2423 while (xfer->ux_hcflags & UXFER_ABORTING)
2424 cv_wait(&xfer->ux_hccv, &sc->sc_lock);
2425 goto done;
2426 }
2427 xfer->ux_hcflags |= UXFER_ABORTING;
2428
2429 /*
2430 * Step 1: Make interrupt routine and hardware ignore xfer.
2431 */
2432 xfer->ux_status = status; /* make software ignore it */
2433 callout_stop(&xfer->ux_callout);
2434 uhci_del_intr_list(sc, ux);
2435
2436 DPRINTF("stop ux=%p", ux, 0, 0, 0);
2437 for (std = ux->ux_stdstart; std != NULL; std = std->link.std) {
2438 usb_syncmem(&std->dma,
2439 std->offs + offsetof(uhci_td_t, td_status),
2440 sizeof(std->td.td_status),
2441 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
2442 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2443 usb_syncmem(&std->dma,
2444 std->offs + offsetof(uhci_td_t, td_status),
2445 sizeof(std->td.td_status),
2446 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2447 }
2448
2449 /*
2450 * Step 2: Wait until we know hardware has finished any possible
2451 * use of the xfer. Also make sure the soft interrupt routine
2452 * has run.
2453 */
2454 /* Hardware finishes in 1ms */
2455 usb_delay_ms_locked(upipe->pipe.up_dev->ud_bus, 2, &sc->sc_lock);
2456 sc->sc_softwake = 1;
2457 usb_schedsoftintr(&sc->sc_bus);
2458 DPRINTF("cv_wait", 0, 0, 0, 0);
2459 cv_wait(&sc->sc_softwake_cv, &sc->sc_lock);
2460
2461 /*
2462 * Step 3: Execute callback.
2463 */
2464 DPRINTF("callback", 0, 0, 0, 0);
2465 #ifdef DIAGNOSTIC
2466 ux->ux_isdone = true;
2467 #endif
2468 wake = xfer->ux_hcflags & UXFER_ABORTWAIT;
2469 xfer->ux_hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT);
2470 usb_transfer_complete(xfer);
2471 if (wake)
2472 cv_broadcast(&xfer->ux_hccv);
2473 done:
2474 KASSERT(mutex_owned(&sc->sc_lock));
2475 }
2476
2477 /* Close a device bulk pipe. */
2478 void
2479 uhci_device_bulk_close(struct usbd_pipe *pipe)
2480 {
2481 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(pipe);
2482 uhci_softc_t *sc = UHCI_PIPE2SC(pipe);
2483
2484 KASSERT(mutex_owned(&sc->sc_lock));
2485
2486 uhci_free_sqh(sc, upipe->bulk.sqh);
2487
2488 pipe->up_endpoint->ue_toggle = upipe->nexttoggle;
2489 }
2490
2491 int
2492 uhci_device_ctrl_init(struct usbd_xfer *xfer)
2493 {
2494 struct uhci_xfer *uxfer = UHCI_XFER2UXFER(xfer);
2495 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(xfer->ux_pipe);
2496 usb_device_request_t *req = &xfer->ux_request;
2497 struct usbd_device *dev = upipe->pipe.up_dev;
2498 uhci_softc_t *sc = dev->ud_bus->ub_hcpriv;
2499 uhci_soft_td_t *data = NULL;
2500 int len;
2501 usbd_status err;
2502 int isread;
2503
2504 UHCIHIST_FUNC(); UHCIHIST_CALLED();
2505 DPRINTFN(3, "xfer=%p len=%d, addr=%d, endpt=%d", xfer, xfer->ux_bufsize,
2506 dev->ud_addr, upipe->pipe.up_endpoint->ue_edesc->bEndpointAddress);
2507
2508 isread = req->bmRequestType & UT_READ;
2509 len = xfer->ux_bufsize;
2510
2511 uxfer->ux_type = UX_CTRL;
2512 /* Set up data transaction */
2513 if (len != 0) {
2514 err = uhci_alloc_std_chain(sc, xfer, len, isread, &data);
2515 if (err)
2516 return err;
2517 }
2518 /* Set up interrupt info. */
2519 uxfer->ux_setup = upipe->ctrl.setup;
2520 uxfer->ux_stat = upipe->ctrl.stat;
2521 uxfer->ux_data = data;
2522
2523 return 0;
2524 }
2525
2526 Static void
2527 uhci_device_ctrl_fini(struct usbd_xfer *xfer)
2528 {
2529 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
2530 struct uhci_xfer *ux = UHCI_XFER2UXFER(xfer);
2531
2532 KASSERT(ux->ux_type == UX_CTRL);
2533
2534 uhci_free_stds(sc, ux);
2535 if (ux->ux_nstd)
2536 kmem_free(ux->ux_stds, sizeof(uhci_soft_td_t *) * ux->ux_nstd);
2537 }
2538
2539 usbd_status
2540 uhci_device_ctrl_transfer(struct usbd_xfer *xfer)
2541 {
2542 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
2543 usbd_status err;
2544
2545 /* Insert last in queue. */
2546 mutex_enter(&sc->sc_lock);
2547 err = usb_insert_transfer(xfer);
2548 mutex_exit(&sc->sc_lock);
2549 if (err)
2550 return err;
2551
2552 /*
2553 * Pipe isn't running (otherwise err would be USBD_INPROG),
2554 * so start it first.
2555 */
2556 return uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
2557 }
2558
2559 usbd_status
2560 uhci_device_ctrl_start(struct usbd_xfer *xfer)
2561 {
2562 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
2563 struct uhci_xfer *uxfer = UHCI_XFER2UXFER(xfer);
2564 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(xfer->ux_pipe);
2565 usb_device_request_t *req = &xfer->ux_request;
2566 struct usbd_device *dev = upipe->pipe.up_dev;
2567 int addr = dev->ud_addr;
2568 int endpt = upipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
2569 uhci_soft_td_t *setup, *stat, *next, *dataend;
2570 uhci_soft_qh_t *sqh;
2571 int len;
2572 int isread;
2573
2574 UHCIHIST_FUNC(); UHCIHIST_CALLED();
2575
2576 if (sc->sc_dying)
2577 return USBD_IOERROR;
2578
2579 KASSERT(xfer->ux_rqflags & URQ_REQUEST);
2580
2581 DPRINTFN(3, "type=0x%02x, request=0x%02x, "
2582 "wValue=0x%04x, wIndex=0x%04x",
2583 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2584 UGETW(req->wIndex));
2585 DPRINTFN(3, "len=%d, addr=%d, endpt=%d",
2586 UGETW(req->wLength), dev->ud_addr, endpt, 0);
2587
2588 isread = req->bmRequestType & UT_READ;
2589 len = UGETW(req->wLength);
2590
2591 setup = upipe->ctrl.setup;
2592 stat = upipe->ctrl.stat;
2593 sqh = upipe->ctrl.sqh;
2594
2595 memcpy(KERNADDR(&upipe->ctrl.reqdma, 0), req, sizeof(*req));
2596 usb_syncmem(&upipe->ctrl.reqdma, 0, sizeof(*req), BUS_DMASYNC_PREWRITE);
2597
2598 mutex_enter(&sc->sc_lock);
2599
2600 /* Set up data transaction */
2601 if (len != 0) {
2602 upipe->nexttoggle = 1;
2603 next = uxfer->ux_data;
2604 uhci_reset_std_chain(sc, xfer, len, isread,
2605 &upipe->nexttoggle, &dataend);
2606 dataend->link.std = stat;
2607 dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_TD);
2608 usb_syncmem(&dataend->dma,
2609 dataend->offs + offsetof(uhci_td_t, td_link),
2610 sizeof(dataend->td.td_link),
2611 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2612 } else {
2613 next = stat;
2614 }
2615
2616 const uint32_t status = UHCI_TD_ZERO_ACTLEN(
2617 UHCI_TD_SET_ERRCNT(3) |
2618 UHCI_TD_ACTIVE |
2619 (dev->ud_speed == USB_SPEED_LOW ? UHCI_TD_LS : 0)
2620 );
2621 setup->link.std = next;
2622 setup->td.td_link = htole32(next->physaddr | UHCI_PTR_TD);
2623 setup->td.td_status = htole32(status);
2624 setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof(*req), endpt, addr));
2625 setup->td.td_buffer = htole32(DMAADDR(&upipe->ctrl.reqdma, 0));
2626
2627 usb_syncmem(&setup->dma, setup->offs, sizeof(setup->td),
2628 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2629
2630 stat->link.std = NULL;
2631 stat->td.td_link = htole32(UHCI_PTR_T);
2632 stat->td.td_status = htole32(status | UHCI_TD_IOC);
2633 stat->td.td_token =
2634 htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
2635 UHCI_TD_IN (0, endpt, addr, 1));
2636 stat->td.td_buffer = htole32(0);
2637 usb_syncmem(&stat->dma, stat->offs, sizeof(stat->td),
2638 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2639
2640 #ifdef UHCI_DEBUG
2641 if (uhcidebug >= 10) {
2642 DPRINTF("--- dump start ---", 0, 0, 0, 0);
2643 DPRINTF("before transfer", 0, 0, 0, 0);
2644 uhci_dump_tds(setup);
2645 DPRINTF("--- dump end ---", 0, 0, 0, 0);
2646 }
2647 #endif
2648
2649 /* Set up interrupt info. */
2650 uxfer->ux_setup = setup;
2651 uxfer->ux_stat = stat;
2652 KASSERT(uxfer->ux_isdone);
2653 #ifdef DIAGNOSTIC
2654 uxfer->ux_isdone = false;
2655 #endif
2656
2657 sqh->elink = setup;
2658 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
2659 /* uhci_add_?s_ctrl() will do usb_syncmem(sqh) */
2660
2661 if (dev->ud_speed == USB_SPEED_LOW)
2662 uhci_add_ls_ctrl(sc, sqh);
2663 else
2664 uhci_add_hs_ctrl(sc, sqh);
2665 uhci_add_intr_list(sc, uxfer);
2666 #ifdef UHCI_DEBUG
2667 if (uhcidebug >= 12) {
2668 uhci_soft_td_t *std;
2669 uhci_soft_qh_t *xqh;
2670 uhci_soft_qh_t *sxqh;
2671 int maxqh = 0;
2672 uhci_physaddr_t link;
2673 DPRINTFN(12, "--- dump start ---", 0, 0, 0, 0);
2674 DPRINTFN(12, "follow from [0]", 0, 0, 0, 0);
2675 for (std = sc->sc_vframes[0].htd, link = 0;
2676 (link & UHCI_PTR_QH) == 0;
2677 std = std->link.std) {
2678 link = le32toh(std->td.td_link);
2679 uhci_dump_td(std);
2680 }
2681 sxqh = (uhci_soft_qh_t *)std;
2682 uhci_dump_qh(sxqh);
2683 for (xqh = sxqh;
2684 xqh != NULL;
2685 xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
2686 xqh->hlink == xqh ? NULL : xqh->hlink)) {
2687 uhci_dump_qh(xqh);
2688 }
2689 DPRINTFN(12, "Enqueued QH:", 0, 0, 0, 0);
2690 uhci_dump_qh(sqh);
2691 uhci_dump_tds(sqh->elink);
2692 DPRINTF("--- dump end ---", 0, 0, 0, 0);
2693 }
2694 #endif
2695 if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
2696 callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
2697 uhci_timeout, xfer);
2698 }
2699 xfer->ux_status = USBD_IN_PROGRESS;
2700 mutex_exit(&sc->sc_lock);
2701
2702 if (sc->sc_bus.ub_usepolling)
2703 uhci_waitintr(sc, xfer);
2704
2705 return USBD_IN_PROGRESS;
2706 }
2707
2708 int
2709 uhci_device_intr_init(struct usbd_xfer *xfer)
2710 {
2711 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
2712 struct uhci_xfer *ux = UHCI_XFER2UXFER(xfer);
2713 usb_endpoint_descriptor_t *ed = xfer->ux_pipe->up_endpoint->ue_edesc;
2714 int endpt = ed->bEndpointAddress;
2715 int isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2716 int len = xfer->ux_bufsize;
2717 int err;
2718
2719 UHCIHIST_FUNC(); UHCIHIST_CALLED();
2720
2721 DPRINTFN(3, "xfer=%p len=%d flags=%d", xfer, xfer->ux_length,
2722 xfer->ux_flags, 0);
2723
2724 KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
2725 KASSERT(len != 0);
2726
2727 ux->ux_type = UX_INTR;
2728 ux->ux_nstd = 0;
2729 err = uhci_alloc_std_chain(sc, xfer, len, isread, &ux->ux_stdstart);
2730
2731 return err;
2732 }
2733
2734 Static void
2735 uhci_device_intr_fini(struct usbd_xfer *xfer)
2736 {
2737 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
2738 struct uhci_xfer *ux = UHCI_XFER2UXFER(xfer);
2739
2740 KASSERT(ux->ux_type == UX_INTR);
2741
2742 uhci_free_stds(sc, ux);
2743 if (ux->ux_nstd)
2744 kmem_free(ux->ux_stds, sizeof(uhci_soft_td_t *) * ux->ux_nstd);
2745 }
2746
2747 usbd_status
2748 uhci_device_intr_transfer(struct usbd_xfer *xfer)
2749 {
2750 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
2751 usbd_status err;
2752
2753 /* Insert last in queue. */
2754 mutex_enter(&sc->sc_lock);
2755 err = usb_insert_transfer(xfer);
2756 mutex_exit(&sc->sc_lock);
2757 if (err)
2758 return err;
2759
2760 /*
2761 * Pipe isn't running (otherwise err would be USBD_INPROG),
2762 * so start it first.
2763 */
2764 return uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
2765 }
2766
2767 usbd_status
2768 uhci_device_intr_start(struct usbd_xfer *xfer)
2769 {
2770 struct uhci_xfer *ux = UHCI_XFER2UXFER(xfer);
2771 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(xfer->ux_pipe);
2772 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
2773 uhci_soft_td_t *data, *dataend;
2774 uhci_soft_qh_t *sqh;
2775 int isread, endpt;
2776 int i;
2777
2778 if (sc->sc_dying)
2779 return USBD_IOERROR;
2780
2781 UHCIHIST_FUNC(); UHCIHIST_CALLED();
2782
2783 DPRINTFN(3, "xfer=%p len=%d flags=%d", xfer, xfer->ux_length,
2784 xfer->ux_flags, 0);
2785
2786 KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
2787 KASSERT(xfer->ux_length <= xfer->ux_bufsize);
2788
2789 endpt = upipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
2790 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2791
2792 data = ux->ux_stdstart;
2793
2794 KASSERT(ux->ux_isdone);
2795 #ifdef DIAGNOSTIC
2796 ux->ux_isdone = false;
2797 #endif
2798
2799 /* Take lock to protect nexttoggle */
2800 mutex_enter(&sc->sc_lock);
2801 uhci_reset_std_chain(sc, xfer, xfer->ux_length, isread,
2802 &upipe->nexttoggle, &dataend);
2803
2804 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2805 usb_syncmem(&dataend->dma,
2806 dataend->offs + offsetof(uhci_td_t, td_status),
2807 sizeof(dataend->td.td_status),
2808 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2809 ux->ux_stdend = dataend;
2810
2811 #ifdef UHCI_DEBUG
2812 if (uhcidebug >= 10) {
2813 DPRINTF("--- dump start ---", 0, 0, 0, 0);
2814 uhci_dump_tds(data);
2815 uhci_dump_qh(upipe->intr.qhs[0]);
2816 DPRINTF("--- dump end ---", 0, 0, 0, 0);
2817 }
2818 #endif
2819
2820 DPRINTFN(10, "qhs[0]=%p", upipe->intr.qhs[0], 0, 0, 0);
2821 for (i = 0; i < upipe->intr.npoll; i++) {
2822 sqh = upipe->intr.qhs[i];
2823 sqh->elink = data;
2824 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2825 usb_syncmem(&sqh->dma,
2826 sqh->offs + offsetof(uhci_qh_t, qh_elink),
2827 sizeof(sqh->qh.qh_elink),
2828 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2829 }
2830 uhci_add_intr_list(sc, ux);
2831 xfer->ux_status = USBD_IN_PROGRESS;
2832 mutex_exit(&sc->sc_lock);
2833
2834 #ifdef UHCI_DEBUG
2835 if (uhcidebug >= 10) {
2836 DPRINTF("--- dump start ---", 0, 0, 0, 0);
2837 uhci_dump_tds(data);
2838 uhci_dump_qh(upipe->intr.qhs[0]);
2839 DPRINTF("--- dump end ---", 0, 0, 0, 0);
2840 }
2841 #endif
2842
2843 return USBD_IN_PROGRESS;
2844 }
2845
2846 /* Abort a device control request. */
2847 void
2848 uhci_device_ctrl_abort(struct usbd_xfer *xfer)
2849 {
2850 uhci_softc_t *sc __diagused = UHCI_XFER2SC(xfer);
2851
2852 KASSERT(mutex_owned(&sc->sc_lock));
2853
2854 UHCIHIST_FUNC(); UHCIHIST_CALLED();
2855 uhci_abort_xfer(xfer, USBD_CANCELLED);
2856 }
2857
2858 /* Close a device control pipe. */
2859 void
2860 uhci_device_ctrl_close(struct usbd_pipe *pipe)
2861 {
2862 uhci_softc_t *sc = UHCI_PIPE2SC(pipe);
2863 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(pipe);
2864
2865 uhci_free_sqh(sc, upipe->ctrl.sqh);
2866 uhci_free_std_locked(sc, upipe->ctrl.setup);
2867 uhci_free_std_locked(sc, upipe->ctrl.stat);
2868
2869 }
2870
2871 /* Abort a device interrupt request. */
2872 void
2873 uhci_device_intr_abort(struct usbd_xfer *xfer)
2874 {
2875 uhci_softc_t *sc __diagused = UHCI_XFER2SC(xfer);
2876
2877 KASSERT(mutex_owned(&sc->sc_lock));
2878 KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
2879
2880 UHCIHIST_FUNC(); UHCIHIST_CALLED();
2881 DPRINTF("xfer=%p", xfer, 0, 0, 0);
2882
2883 uhci_abort_xfer(xfer, USBD_CANCELLED);
2884 }
2885
2886 /* Close a device interrupt pipe. */
2887 void
2888 uhci_device_intr_close(struct usbd_pipe *pipe)
2889 {
2890 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(pipe);
2891 uhci_softc_t *sc = UHCI_PIPE2SC(pipe);
2892 int i, npoll;
2893
2894 KASSERT(mutex_owned(&sc->sc_lock));
2895
2896 /* Unlink descriptors from controller data structures. */
2897 npoll = upipe->intr.npoll;
2898 for (i = 0; i < npoll; i++)
2899 uhci_remove_intr(sc, upipe->intr.qhs[i]);
2900
2901 /*
2902 * We now have to wait for any activity on the physical
2903 * descriptors to stop.
2904 */
2905 usb_delay_ms_locked(&sc->sc_bus, 2, &sc->sc_lock);
2906
2907 for (i = 0; i < npoll; i++)
2908 uhci_free_sqh(sc, upipe->intr.qhs[i]);
2909 kmem_free(upipe->intr.qhs, npoll * sizeof(uhci_soft_qh_t *));
2910 }
2911
2912 int
2913 uhci_device_isoc_init(struct usbd_xfer *xfer)
2914 {
2915 struct uhci_xfer *ux = UHCI_XFER2UXFER(xfer);
2916
2917 KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
2918 KASSERT(xfer->ux_nframes != 0);
2919 KASSERT(ux->ux_isdone);
2920
2921 ux->ux_type = UX_ISOC;
2922 return 0;
2923 }
2924
2925 Static void
2926 uhci_device_isoc_fini(struct usbd_xfer *xfer)
2927 {
2928 struct uhci_xfer *ux __diagused = UHCI_XFER2UXFER(xfer);
2929
2930 KASSERT(ux->ux_type == UX_ISOC);
2931 }
2932
2933 usbd_status
2934 uhci_device_isoc_transfer(struct usbd_xfer *xfer)
2935 {
2936 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
2937 usbd_status err __diagused;
2938
2939 UHCIHIST_FUNC(); UHCIHIST_CALLED();
2940 DPRINTFN(5, "xfer=%p", xfer, 0, 0, 0);
2941
2942 /* Put it on our queue, */
2943 mutex_enter(&sc->sc_lock);
2944 err = usb_insert_transfer(xfer);
2945 mutex_exit(&sc->sc_lock);
2946
2947 KASSERT(err == USBD_NORMAL_COMPLETION);
2948
2949 /* insert into schedule, */
2950
2951 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(xfer->ux_pipe);
2952 struct uhci_xfer *ux = UHCI_XFER2UXFER(xfer);
2953 struct isoc *isoc = &upipe->isoc;
2954 uhci_soft_td_t *std = NULL;
2955 uint32_t buf, len, status, offs;
2956 int i, next, nframes;
2957 int rd = UE_GET_DIR(upipe->pipe.up_endpoint->ue_edesc->bEndpointAddress) == UE_DIR_IN;
2958
2959 DPRINTFN(5, "used=%d next=%d xfer=%p nframes=%d",
2960 isoc->inuse, isoc->next, xfer, xfer->ux_nframes);
2961
2962 if (sc->sc_dying)
2963 return USBD_IOERROR;
2964
2965 if (xfer->ux_status == USBD_IN_PROGRESS) {
2966 /* This request has already been entered into the frame list */
2967 printf("%s: xfer=%p in frame list\n", __func__, xfer);
2968 /* XXX */
2969 }
2970
2971 #ifdef DIAGNOSTIC
2972 if (isoc->inuse >= UHCI_VFRAMELIST_COUNT)
2973 printf("%s: overflow!\n", __func__);
2974 #endif
2975
2976 KASSERT(xfer->ux_nframes != 0);
2977
2978 mutex_enter(&sc->sc_lock);
2979 next = isoc->next;
2980 if (next == -1) {
2981 /* Not in use yet, schedule it a few frames ahead. */
2982 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2983 DPRINTFN(2, "start next=%d", next, 0, 0, 0);
2984 }
2985
2986 xfer->ux_status = USBD_IN_PROGRESS;
2987 ux->ux_curframe = next;
2988
2989 buf = DMAADDR(&xfer->ux_dmabuf, 0);
2990 offs = 0;
2991 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2992 UHCI_TD_ACTIVE |
2993 UHCI_TD_IOS);
2994 nframes = xfer->ux_nframes;
2995 for (i = 0; i < nframes; i++) {
2996 std = isoc->stds[next];
2997 if (++next >= UHCI_VFRAMELIST_COUNT)
2998 next = 0;
2999 len = xfer->ux_frlengths[i];
3000 std->td.td_buffer = htole32(buf);
3001 usb_syncmem(&xfer->ux_dmabuf, offs, len,
3002 rd ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
3003 if (i == nframes - 1)
3004 status |= UHCI_TD_IOC;
3005 std->td.td_status = htole32(status);
3006 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
3007 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
3008 usb_syncmem(&std->dma, std->offs, sizeof(std->td),
3009 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3010 #ifdef UHCI_DEBUG
3011 if (uhcidebug >= 5) {
3012 DPRINTF("--- dump start ---", 0, 0, 0, 0);
3013 DPRINTF("TD %d", i, 0, 0, 0);
3014 uhci_dump_td(std);
3015 DPRINTF("--- dump end ---", 0, 0, 0, 0);
3016 }
3017 #endif
3018 buf += len;
3019 offs += len;
3020 }
3021 isoc->next = next;
3022 isoc->inuse += xfer->ux_nframes;
3023
3024 /* Set up interrupt info. */
3025 ux->ux_stdstart = std;
3026 ux->ux_stdend = std;
3027
3028 KASSERT(ux->ux_isdone);
3029 #ifdef DIAGNOSTIC
3030 ux->ux_isdone = false;
3031 #endif
3032 uhci_add_intr_list(sc, ux);
3033
3034 mutex_exit(&sc->sc_lock);
3035
3036 return USBD_IN_PROGRESS;
3037 }
3038
3039 void
3040 uhci_device_isoc_abort(struct usbd_xfer *xfer)
3041 {
3042 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
3043 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(xfer->ux_pipe);
3044 struct uhci_xfer *ux = UHCI_XFER2UXFER(xfer);
3045 uhci_soft_td_t **stds = upipe->isoc.stds;
3046 uhci_soft_td_t *std;
3047 int i, n, nframes, maxlen, len;
3048
3049 KASSERT(mutex_owned(&sc->sc_lock));
3050
3051 /* Transfer is already done. */
3052 if (xfer->ux_status != USBD_NOT_STARTED &&
3053 xfer->ux_status != USBD_IN_PROGRESS) {
3054 return;
3055 }
3056
3057 /* Give xfer the requested abort code. */
3058 xfer->ux_status = USBD_CANCELLED;
3059
3060 /* make hardware ignore it, */
3061 nframes = xfer->ux_nframes;
3062 n = ux->ux_curframe;
3063 maxlen = 0;
3064 for (i = 0; i < nframes; i++) {
3065 std = stds[n];
3066 usb_syncmem(&std->dma,
3067 std->offs + offsetof(uhci_td_t, td_status),
3068 sizeof(std->td.td_status),
3069 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
3070 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
3071 usb_syncmem(&std->dma,
3072 std->offs + offsetof(uhci_td_t, td_status),
3073 sizeof(std->td.td_status),
3074 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3075 usb_syncmem(&std->dma,
3076 std->offs + offsetof(uhci_td_t, td_token),
3077 sizeof(std->td.td_token),
3078 BUS_DMASYNC_POSTWRITE);
3079 len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token));
3080 if (len > maxlen)
3081 maxlen = len;
3082 if (++n >= UHCI_VFRAMELIST_COUNT)
3083 n = 0;
3084 }
3085
3086 /* and wait until we are sure the hardware has finished. */
3087 delay(maxlen);
3088
3089 #ifdef DIAGNOSTIC
3090 ux->ux_isdone = true;
3091 #endif
3092 /* Remove from interrupt list. */
3093 uhci_del_intr_list(sc, ux);
3094
3095 /* Run callback. */
3096 usb_transfer_complete(xfer);
3097
3098 KASSERT(mutex_owned(&sc->sc_lock));
3099 }
3100
3101 void
3102 uhci_device_isoc_close(struct usbd_pipe *pipe)
3103 {
3104 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(pipe);
3105 uhci_softc_t *sc = UHCI_PIPE2SC(pipe);
3106 uhci_soft_td_t *std, *vstd;
3107 struct isoc *isoc;
3108 int i;
3109
3110 KASSERT(mutex_owned(&sc->sc_lock));
3111
3112 /*
3113 * Make sure all TDs are marked as inactive.
3114 * Wait for completion.
3115 * Unschedule.
3116 * Deallocate.
3117 */
3118 isoc = &upipe->isoc;
3119
3120 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
3121 std = isoc->stds[i];
3122 usb_syncmem(&std->dma,
3123 std->offs + offsetof(uhci_td_t, td_status),
3124 sizeof(std->td.td_status),
3125 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
3126 std->td.td_status &= htole32(~UHCI_TD_ACTIVE);
3127 usb_syncmem(&std->dma,
3128 std->offs + offsetof(uhci_td_t, td_status),
3129 sizeof(std->td.td_status),
3130 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3131 }
3132 /* wait for completion */
3133 usb_delay_ms_locked(&sc->sc_bus, 2, &sc->sc_lock);
3134
3135 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
3136 std = isoc->stds[i];
3137 for (vstd = sc->sc_vframes[i].htd;
3138 vstd != NULL && vstd->link.std != std;
3139 vstd = vstd->link.std)
3140 ;
3141 if (vstd == NULL) {
3142 /*panic*/
3143 printf("%s: %p not found\n", __func__, std);
3144 mutex_exit(&sc->sc_lock);
3145 return;
3146 }
3147 vstd->link = std->link;
3148 usb_syncmem(&std->dma,
3149 std->offs + offsetof(uhci_td_t, td_link),
3150 sizeof(std->td.td_link),
3151 BUS_DMASYNC_POSTWRITE);
3152 vstd->td.td_link = std->td.td_link;
3153 usb_syncmem(&vstd->dma,
3154 vstd->offs + offsetof(uhci_td_t, td_link),
3155 sizeof(vstd->td.td_link),
3156 BUS_DMASYNC_PREWRITE);
3157 uhci_free_std_locked(sc, std);
3158 }
3159
3160 kmem_free(isoc->stds, UHCI_VFRAMELIST_COUNT * sizeof(uhci_soft_td_t *));
3161 }
3162
3163 usbd_status
3164 uhci_setup_isoc(struct usbd_pipe *pipe)
3165 {
3166 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(pipe);
3167 uhci_softc_t *sc = UHCI_PIPE2SC(pipe);
3168 int addr = upipe->pipe.up_dev->ud_addr;
3169 int endpt = upipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
3170 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
3171 uhci_soft_td_t *std, *vstd;
3172 uint32_t token;
3173 struct isoc *isoc;
3174 int i;
3175
3176 isoc = &upipe->isoc;
3177
3178 isoc->stds = kmem_alloc(
3179 UHCI_VFRAMELIST_COUNT * sizeof(uhci_soft_td_t *), KM_SLEEP);
3180 if (isoc->stds == NULL)
3181 return USBD_NOMEM;
3182
3183 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
3184 UHCI_TD_OUT(0, endpt, addr, 0);
3185
3186 /* Allocate the TDs and mark as inactive; */
3187 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
3188 std = uhci_alloc_std(sc);
3189 if (std == 0)
3190 goto bad;
3191 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
3192 std->td.td_token = htole32(token);
3193 usb_syncmem(&std->dma, std->offs, sizeof(std->td),
3194 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3195 isoc->stds[i] = std;
3196 }
3197
3198 mutex_enter(&sc->sc_lock);
3199
3200 /* Insert TDs into schedule. */
3201 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
3202 std = isoc->stds[i];
3203 vstd = sc->sc_vframes[i].htd;
3204 usb_syncmem(&vstd->dma,
3205 vstd->offs + offsetof(uhci_td_t, td_link),
3206 sizeof(vstd->td.td_link),
3207 BUS_DMASYNC_POSTWRITE);
3208 std->link = vstd->link;
3209 std->td.td_link = vstd->td.td_link;
3210 usb_syncmem(&std->dma,
3211 std->offs + offsetof(uhci_td_t, td_link),
3212 sizeof(std->td.td_link),
3213 BUS_DMASYNC_PREWRITE);
3214 vstd->link.std = std;
3215 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
3216 usb_syncmem(&vstd->dma,
3217 vstd->offs + offsetof(uhci_td_t, td_link),
3218 sizeof(vstd->td.td_link),
3219 BUS_DMASYNC_PREWRITE);
3220 }
3221 mutex_exit(&sc->sc_lock);
3222
3223 isoc->next = -1;
3224 isoc->inuse = 0;
3225
3226 return USBD_NORMAL_COMPLETION;
3227
3228 bad:
3229 while (--i >= 0)
3230 uhci_free_std(sc, isoc->stds[i]);
3231 kmem_free(isoc->stds, UHCI_VFRAMELIST_COUNT * sizeof(uhci_soft_td_t *));
3232 return USBD_NOMEM;
3233 }
3234
3235 void
3236 uhci_device_isoc_done(struct usbd_xfer *xfer)
3237 {
3238 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(xfer->ux_pipe);
3239 struct uhci_xfer *ux = UHCI_XFER2UXFER(xfer);
3240 int i, offs;
3241 int rd = UE_GET_DIR(upipe->pipe.up_endpoint->ue_edesc->bEndpointAddress) == UE_DIR_IN;
3242
3243 UHCIHIST_FUNC(); UHCIHIST_CALLED();
3244 DPRINTFN(4, "length=%d, ux_state=0x%08x",
3245 xfer->ux_actlen, xfer->ux_state, 0, 0);
3246
3247 #ifdef DIAGNOSTIC
3248 if (ux->ux_stdend == NULL) {
3249 printf("%s: xfer=%p stdend==NULL\n", __func__, xfer);
3250 #ifdef UHCI_DEBUG
3251 DPRINTF("--- dump start ---", 0, 0, 0, 0);
3252 uhci_dump_ii(ux);
3253 DPRINTF("--- dump end ---", 0, 0, 0, 0);
3254 #endif
3255 return;
3256 }
3257 #endif
3258
3259 /* Turn off the interrupt since it is active even if the TD is not. */
3260 usb_syncmem(&ux->ux_stdend->dma,
3261 ux->ux_stdend->offs + offsetof(uhci_td_t, td_status),
3262 sizeof(ux->ux_stdend->td.td_status),
3263 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
3264 ux->ux_stdend->td.td_status &= htole32(~UHCI_TD_IOC);
3265 usb_syncmem(&ux->ux_stdend->dma,
3266 ux->ux_stdend->offs + offsetof(uhci_td_t, td_status),
3267 sizeof(ux->ux_stdend->td.td_status),
3268 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3269
3270 offs = 0;
3271 for (i = 0; i < xfer->ux_nframes; i++) {
3272 usb_syncmem(&xfer->ux_dmabuf, offs, xfer->ux_frlengths[i],
3273 rd ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
3274 offs += xfer->ux_frlengths[i];
3275 }
3276 }
3277
3278 void
3279 uhci_device_intr_done(struct usbd_xfer *xfer)
3280 {
3281 uhci_softc_t *sc __diagused = UHCI_XFER2SC(xfer);
3282 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(xfer->ux_pipe);
3283 uhci_soft_qh_t *sqh;
3284 int i, npoll;
3285
3286 UHCIHIST_FUNC(); UHCIHIST_CALLED();
3287 DPRINTFN(5, "length=%d", xfer->ux_actlen, 0, 0, 0);
3288
3289 KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
3290
3291 npoll = upipe->intr.npoll;
3292 for (i = 0; i < npoll; i++) {
3293 sqh = upipe->intr.qhs[i];
3294 sqh->elink = NULL;
3295 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
3296 usb_syncmem(&sqh->dma,
3297 sqh->offs + offsetof(uhci_qh_t, qh_elink),
3298 sizeof(sqh->qh.qh_elink),
3299 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3300 }
3301 const int endpt = upipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
3302 const bool isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3303 usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
3304 isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
3305 }
3306
3307 /* Deallocate request data structures */
3308 void
3309 uhci_device_ctrl_done(struct usbd_xfer *xfer)
3310 {
3311 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
3312 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(xfer->ux_pipe);
3313 int len = UGETW(xfer->ux_request.wLength);
3314 int isread = (xfer->ux_request.bmRequestType & UT_READ);
3315
3316 KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
3317
3318 UHCIHIST_FUNC(); UHCIHIST_CALLED();
3319
3320 KASSERT(xfer->ux_rqflags & URQ_REQUEST);
3321
3322 /* XXXNH move to uhci_idone??? */
3323 if (upipe->pipe.up_dev->ud_speed == USB_SPEED_LOW)
3324 uhci_remove_ls_ctrl(sc, upipe->ctrl.sqh);
3325 else
3326 uhci_remove_hs_ctrl(sc, upipe->ctrl.sqh);
3327
3328 if (len) {
3329 usb_syncmem(&xfer->ux_dmabuf, 0, len,
3330 isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
3331 }
3332 usb_syncmem(&upipe->ctrl.reqdma, 0,
3333 sizeof(usb_device_request_t), BUS_DMASYNC_POSTWRITE);
3334
3335 DPRINTF("length=%d", xfer->ux_actlen, 0, 0, 0);
3336 }
3337
3338 /* Deallocate request data structures */
3339 void
3340 uhci_device_bulk_done(struct usbd_xfer *xfer)
3341 {
3342 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
3343 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(xfer->ux_pipe);
3344 usb_endpoint_descriptor_t *ed = xfer->ux_pipe->up_endpoint->ue_edesc;
3345 int endpt = ed->bEndpointAddress;
3346 int isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3347
3348 UHCIHIST_FUNC(); UHCIHIST_CALLED();
3349 DPRINTFN(5, "xfer=%p sc=%p upipe=%p", xfer, sc, upipe, 0);
3350
3351 KASSERT(mutex_owned(&sc->sc_lock));
3352
3353 uhci_remove_bulk(sc, upipe->bulk.sqh);
3354
3355 if (xfer->ux_length) {
3356 usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
3357 isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
3358 }
3359
3360 DPRINTFN(5, "length=%d", xfer->ux_actlen, 0, 0, 0);
3361 }
3362
3363 /* Add interrupt QH, called with vflock. */
3364 void
3365 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
3366 {
3367 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
3368 uhci_soft_qh_t *eqh;
3369
3370 UHCIHIST_FUNC(); UHCIHIST_CALLED();
3371 DPRINTFN(4, "n=%d sqh=%p", sqh->pos, sqh, 0, 0);
3372
3373 eqh = vf->eqh;
3374 usb_syncmem(&eqh->dma, eqh->offs + offsetof(uhci_qh_t, qh_hlink),
3375 sizeof(eqh->qh.qh_hlink),
3376 BUS_DMASYNC_POSTWRITE);
3377 sqh->hlink = eqh->hlink;
3378 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
3379 usb_syncmem(&sqh->dma, sqh->offs + offsetof(uhci_qh_t, qh_hlink),
3380 sizeof(sqh->qh.qh_hlink),
3381 BUS_DMASYNC_PREWRITE);
3382 eqh->hlink = sqh;
3383 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
3384 usb_syncmem(&eqh->dma, eqh->offs + offsetof(uhci_qh_t, qh_hlink),
3385 sizeof(eqh->qh.qh_hlink),
3386 BUS_DMASYNC_PREWRITE);
3387 vf->eqh = sqh;
3388 vf->bandwidth++;
3389 }
3390
3391 /* Remove interrupt QH. */
3392 void
3393 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
3394 {
3395 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
3396 uhci_soft_qh_t *pqh;
3397
3398 UHCIHIST_FUNC(); UHCIHIST_CALLED();
3399 DPRINTFN(4, "n=%d sqh=%p", sqh->pos, sqh, 0, 0);
3400
3401 /* See comment in uhci_remove_ctrl() */
3402
3403 usb_syncmem(&sqh->dma, sqh->offs + offsetof(uhci_qh_t, qh_elink),
3404 sizeof(sqh->qh.qh_elink),
3405 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
3406 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
3407 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
3408 usb_syncmem(&sqh->dma,
3409 sqh->offs + offsetof(uhci_qh_t, qh_elink),
3410 sizeof(sqh->qh.qh_elink),
3411 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3412 delay(UHCI_QH_REMOVE_DELAY);
3413 }
3414
3415 pqh = uhci_find_prev_qh(vf->hqh, sqh);
3416 usb_syncmem(&sqh->dma, sqh->offs + offsetof(uhci_qh_t, qh_hlink),
3417 sizeof(sqh->qh.qh_hlink),
3418 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
3419 pqh->hlink = sqh->hlink;
3420 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
3421 usb_syncmem(&pqh->dma, pqh->offs + offsetof(uhci_qh_t, qh_hlink),
3422 sizeof(pqh->qh.qh_hlink),
3423 BUS_DMASYNC_PREWRITE);
3424 delay(UHCI_QH_REMOVE_DELAY);
3425 if (vf->eqh == sqh)
3426 vf->eqh = pqh;
3427 vf->bandwidth--;
3428 }
3429
3430 usbd_status
3431 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
3432 {
3433 uhci_soft_qh_t *sqh;
3434 int i, npoll;
3435 u_int bestbw, bw, bestoffs, offs;
3436
3437 UHCIHIST_FUNC(); UHCIHIST_CALLED();
3438 DPRINTFN(2, "pipe=%p", upipe, 0, 0, 0);
3439 if (ival == 0) {
3440 printf("%s: 0 interval\n", __func__);
3441 return USBD_INVAL;
3442 }
3443
3444 if (ival > UHCI_VFRAMELIST_COUNT)
3445 ival = UHCI_VFRAMELIST_COUNT;
3446 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
3447 DPRINTF("ival=%d npoll=%d", ival, npoll, 0, 0);
3448
3449 upipe->intr.npoll = npoll;
3450 upipe->intr.qhs =
3451 kmem_alloc(npoll * sizeof(uhci_soft_qh_t *), KM_SLEEP);
3452 if (upipe->intr.qhs == NULL)
3453 return USBD_NOMEM;
3454
3455 /*
3456 * Figure out which offset in the schedule that has most
3457 * bandwidth left over.
3458 */
3459 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
3460 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
3461 for (bw = i = 0; i < npoll; i++)
3462 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
3463 if (bw < bestbw) {
3464 bestbw = bw;
3465 bestoffs = offs;
3466 }
3467 }
3468 DPRINTF("bw=%d offs=%d", bestbw, bestoffs, 0, 0);
3469 for (i = 0; i < npoll; i++) {
3470 upipe->intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
3471 sqh->elink = NULL;
3472 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
3473 usb_syncmem(&sqh->dma,
3474 sqh->offs + offsetof(uhci_qh_t, qh_elink),
3475 sizeof(sqh->qh.qh_elink),
3476 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3477 sqh->pos = MOD(i * ival + bestoffs);
3478 }
3479 #undef MOD
3480
3481 mutex_enter(&sc->sc_lock);
3482 /* Enter QHs into the controller data structures. */
3483 for (i = 0; i < npoll; i++)
3484 uhci_add_intr(sc, upipe->intr.qhs[i]);
3485 mutex_exit(&sc->sc_lock);
3486
3487 DPRINTFN(5, "returns %p", upipe, 0, 0, 0);
3488
3489 return USBD_NORMAL_COMPLETION;
3490 }
3491
3492 /* Open a new pipe. */
3493 usbd_status
3494 uhci_open(struct usbd_pipe *pipe)
3495 {
3496 uhci_softc_t *sc = UHCI_PIPE2SC(pipe);
3497 struct usbd_bus *bus = pipe->up_dev->ud_bus;
3498 struct uhci_pipe *upipe = UHCI_PIPE2UPIPE(pipe);
3499 usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
3500 usbd_status err = USBD_NOMEM;
3501 int ival;
3502
3503 UHCIHIST_FUNC(); UHCIHIST_CALLED();
3504 DPRINTF("pipe=%p, addr=%d, endpt=%d (%d)",
3505 pipe, pipe->up_dev->ud_addr, ed->bEndpointAddress, bus->ub_rhaddr);
3506
3507 if (sc->sc_dying)
3508 return USBD_IOERROR;
3509
3510 upipe->aborting = 0;
3511 /* toggle state needed for bulk endpoints */
3512 upipe->nexttoggle = pipe->up_endpoint->ue_toggle;
3513
3514 if (pipe->up_dev->ud_addr == bus->ub_rhaddr) {
3515 switch (ed->bEndpointAddress) {
3516 case USB_CONTROL_ENDPOINT:
3517 pipe->up_methods = &roothub_ctrl_methods;
3518 break;
3519 case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
3520 pipe->up_methods = &uhci_root_intr_methods;
3521 break;
3522 default:
3523 return USBD_INVAL;
3524 }
3525 } else {
3526 switch (ed->bmAttributes & UE_XFERTYPE) {
3527 case UE_CONTROL:
3528 pipe->up_methods = &uhci_device_ctrl_methods;
3529 upipe->ctrl.sqh = uhci_alloc_sqh(sc);
3530 if (upipe->ctrl.sqh == NULL)
3531 goto bad;
3532 upipe->ctrl.setup = uhci_alloc_std(sc);
3533 if (upipe->ctrl.setup == NULL) {
3534 uhci_free_sqh(sc, upipe->ctrl.sqh);
3535 goto bad;
3536 }
3537 upipe->ctrl.stat = uhci_alloc_std(sc);
3538 if (upipe->ctrl.stat == NULL) {
3539 uhci_free_sqh(sc, upipe->ctrl.sqh);
3540 uhci_free_std(sc, upipe->ctrl.setup);
3541 goto bad;
3542 }
3543 err = usb_allocmem(&sc->sc_bus,
3544 sizeof(usb_device_request_t),
3545 0, &upipe->ctrl.reqdma);
3546 if (err) {
3547 uhci_free_sqh(sc, upipe->ctrl.sqh);
3548 uhci_free_std(sc, upipe->ctrl.setup);
3549 uhci_free_std(sc, upipe->ctrl.stat);
3550 goto bad;
3551 }
3552 break;
3553 case UE_INTERRUPT:
3554 pipe->up_methods = &uhci_device_intr_methods;
3555 ival = pipe->up_interval;
3556 if (ival == USBD_DEFAULT_INTERVAL)
3557 ival = ed->bInterval;
3558 return uhci_device_setintr(sc, upipe, ival);
3559 case UE_ISOCHRONOUS:
3560 pipe->up_serialise = false;
3561 pipe->up_methods = &uhci_device_isoc_methods;
3562 return uhci_setup_isoc(pipe);
3563 case UE_BULK:
3564 pipe->up_methods = &uhci_device_bulk_methods;
3565 upipe->bulk.sqh = uhci_alloc_sqh(sc);
3566 if (upipe->bulk.sqh == NULL)
3567 goto bad;
3568 break;
3569 }
3570 }
3571 return USBD_NORMAL_COMPLETION;
3572
3573 bad:
3574 return USBD_NOMEM;
3575 }
3576
3577 /*
3578 * Data structures and routines to emulate the root hub.
3579 */
3580 /*
3581 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
3582 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
3583 * should not be used by the USB subsystem. As we cannot issue a
3584 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
3585 * will be enabled as part of the reset.
3586 *
3587 * On the VT83C572, the port cannot be successfully enabled until the
3588 * outstanding "port enable change" and "connection status change"
3589 * events have been reset.
3590 */
3591 Static usbd_status
3592 uhci_portreset(uhci_softc_t *sc, int index)
3593 {
3594 int lim, port, x;
3595 UHCIHIST_FUNC(); UHCIHIST_CALLED();
3596
3597 if (index == 1)
3598 port = UHCI_PORTSC1;
3599 else if (index == 2)
3600 port = UHCI_PORTSC2;
3601 else
3602 return USBD_IOERROR;
3603
3604 x = URWMASK(UREAD2(sc, port));
3605 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
3606
3607 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
3608
3609 DPRINTF("uhci port %d reset, status0 = 0x%04x", index,
3610 UREAD2(sc, port), 0, 0);
3611
3612 x = URWMASK(UREAD2(sc, port));
3613 UWRITE2(sc, port, x & ~(UHCI_PORTSC_PR | UHCI_PORTSC_SUSP));
3614
3615 delay(100);
3616
3617 DPRINTF("uhci port %d reset, status1 = 0x%04x", index,
3618 UREAD2(sc, port), 0, 0);
3619
3620 x = URWMASK(UREAD2(sc, port));
3621 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3622
3623 for (lim = 10; --lim > 0;) {
3624 usb_delay_ms(&sc->sc_bus, USB_PORT_RESET_DELAY);
3625
3626 x = UREAD2(sc, port);
3627 DPRINTF("uhci port %d iteration %u, status = 0x%04x", index,
3628 lim, x, 0);
3629
3630 if (!(x & UHCI_PORTSC_CCS)) {
3631 /*
3632 * No device is connected (or was disconnected
3633 * during reset). Consider the port reset.
3634 * The delay must be long enough to ensure on
3635 * the initial iteration that the device
3636 * connection will have been registered. 50ms
3637 * appears to be sufficient, but 20ms is not.
3638 */
3639 DPRINTFN(3, "uhci port %d loop %u, device detached",
3640 index, lim, 0, 0);
3641 break;
3642 }
3643
3644 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
3645 /*
3646 * Port enabled changed and/or connection
3647 * status changed were set. Reset either or
3648 * both raised flags (by writing a 1 to that
3649 * bit), and wait again for state to settle.
3650 */
3651 UWRITE2(sc, port, URWMASK(x) |
3652 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
3653 continue;
3654 }
3655
3656 if (x & UHCI_PORTSC_PE)
3657 /* Port is enabled */
3658 break;
3659
3660 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
3661 }
3662
3663 DPRINTFN(3, "uhci port %d reset, status2 = 0x%04x", index,
3664 UREAD2(sc, port), 0, 0);
3665
3666 if (lim <= 0) {
3667 DPRINTF("uhci port %d reset timed out", index,
3668 0, 0, 0);
3669 return USBD_TIMEOUT;
3670 }
3671
3672 sc->sc_isreset = 1;
3673 return USBD_NORMAL_COMPLETION;
3674 }
3675
3676 Static int
3677 uhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
3678 void *buf, int buflen)
3679 {
3680 uhci_softc_t *sc = UHCI_BUS2SC(bus);
3681 int port, x;
3682 int status, change, totlen = 0;
3683 uint16_t len, value, index;
3684 usb_port_status_t ps;
3685 usbd_status err;
3686
3687 UHCIHIST_FUNC(); UHCIHIST_CALLED();
3688
3689 if (sc->sc_dying)
3690 return -1;
3691
3692 DPRINTF("type=0x%02x request=%02x", req->bmRequestType,
3693 req->bRequest, 0, 0);
3694
3695 len = UGETW(req->wLength);
3696 value = UGETW(req->wValue);
3697 index = UGETW(req->wIndex);
3698
3699 #define C(x,y) ((x) | ((y) << 8))
3700 switch (C(req->bRequest, req->bmRequestType)) {
3701 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3702 DPRINTF("wValue=0x%04x", value, 0, 0, 0);
3703 if (len == 0)
3704 break;
3705 switch (value) {
3706 case C(0, UDESC_DEVICE): {
3707 usb_device_descriptor_t devd;
3708
3709 totlen = min(buflen, sizeof(devd));
3710 memcpy(&devd, buf, totlen);
3711 USETW(devd.idVendor, sc->sc_id_vendor);
3712 memcpy(buf, &devd, totlen);
3713 break;
3714 }
3715 case C(1, UDESC_STRING):
3716 #define sd ((usb_string_descriptor_t *)buf)
3717 /* Vendor */
3718 totlen = usb_makestrdesc(sd, len, sc->sc_vendor);
3719 break;
3720 case C(2, UDESC_STRING):
3721 /* Product */
3722 totlen = usb_makestrdesc(sd, len, "UHCI root hub");
3723 break;
3724 #undef sd
3725 default:
3726 /* default from usbroothub */
3727 return buflen;
3728 }
3729 break;
3730
3731 /* Hub requests */
3732 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3733 break;
3734 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3735 DPRINTF("UR_CLEAR_PORT_FEATURE port=%d feature=%d", index,
3736 value, 0, 0);
3737 if (index == 1)
3738 port = UHCI_PORTSC1;
3739 else if (index == 2)
3740 port = UHCI_PORTSC2;
3741 else {
3742 return -1;
3743 }
3744 switch(value) {
3745 case UHF_PORT_ENABLE:
3746 x = URWMASK(UREAD2(sc, port));
3747 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
3748 break;
3749 case UHF_PORT_SUSPEND:
3750 x = URWMASK(UREAD2(sc, port));
3751 if (!(x & UHCI_PORTSC_SUSP)) /* not suspended */
3752 break;
3753 UWRITE2(sc, port, x | UHCI_PORTSC_RD);
3754 /* see USB2 spec ch. 7.1.7.7 */
3755 usb_delay_ms(&sc->sc_bus, 20);
3756 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
3757 /* 10ms resume delay must be provided by caller */
3758 break;
3759 case UHF_PORT_RESET:
3760 x = URWMASK(UREAD2(sc, port));
3761 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3762 break;
3763 case UHF_C_PORT_CONNECTION:
3764 x = URWMASK(UREAD2(sc, port));
3765 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
3766 break;
3767 case UHF_C_PORT_ENABLE:
3768 x = URWMASK(UREAD2(sc, port));
3769 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
3770 break;
3771 case UHF_C_PORT_OVER_CURRENT:
3772 x = URWMASK(UREAD2(sc, port));
3773 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
3774 break;
3775 case UHF_C_PORT_RESET:
3776 sc->sc_isreset = 0;
3777 break;
3778 case UHF_PORT_CONNECTION:
3779 case UHF_PORT_OVER_CURRENT:
3780 case UHF_PORT_POWER:
3781 case UHF_PORT_LOW_SPEED:
3782 case UHF_C_PORT_SUSPEND:
3783 default:
3784 return -1;
3785 }
3786 break;
3787 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
3788 if (index == 1)
3789 port = UHCI_PORTSC1;
3790 else if (index == 2)
3791 port = UHCI_PORTSC2;
3792 else {
3793 return -1;
3794 }
3795 if (len > 0) {
3796 *(uint8_t *)buf =
3797 UHCI_PORTSC_GET_LS(UREAD2(sc, port));
3798 totlen = 1;
3799 }
3800 break;
3801 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3802 if (len == 0)
3803 break;
3804 if ((value & 0xff) != 0) {
3805 return -1;
3806 }
3807 usb_hub_descriptor_t hubd;
3808
3809 totlen = min(buflen, sizeof(hubd));
3810 memcpy(&hubd, buf, totlen);
3811 hubd.bNbrPorts = 2;
3812 memcpy(buf, &hubd, totlen);
3813 break;
3814 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3815 if (len != 4) {
3816 return -1;
3817 }
3818 memset(buf, 0, len);
3819 totlen = len;
3820 break;
3821 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3822 if (index == 1)
3823 port = UHCI_PORTSC1;
3824 else if (index == 2)
3825 port = UHCI_PORTSC2;
3826 else {
3827 return -1;
3828 }
3829 if (len != 4) {
3830 return -1;
3831 }
3832 x = UREAD2(sc, port);
3833 status = change = 0;
3834 if (x & UHCI_PORTSC_CCS)
3835 status |= UPS_CURRENT_CONNECT_STATUS;
3836 if (x & UHCI_PORTSC_CSC)
3837 change |= UPS_C_CONNECT_STATUS;
3838 if (x & UHCI_PORTSC_PE)
3839 status |= UPS_PORT_ENABLED;
3840 if (x & UHCI_PORTSC_POEDC)
3841 change |= UPS_C_PORT_ENABLED;
3842 if (x & UHCI_PORTSC_OCI)
3843 status |= UPS_OVERCURRENT_INDICATOR;
3844 if (x & UHCI_PORTSC_OCIC)
3845 change |= UPS_C_OVERCURRENT_INDICATOR;
3846 if (x & UHCI_PORTSC_SUSP)
3847 status |= UPS_SUSPEND;
3848 if (x & UHCI_PORTSC_LSDA)
3849 status |= UPS_LOW_SPEED;
3850 status |= UPS_PORT_POWER;
3851 if (sc->sc_isreset)
3852 change |= UPS_C_PORT_RESET;
3853 USETW(ps.wPortStatus, status);
3854 USETW(ps.wPortChange, change);
3855 totlen = min(len, sizeof(ps));
3856 memcpy(buf, &ps, totlen);
3857 break;
3858 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3859 return -1;
3860 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3861 break;
3862 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3863 if (index == 1)
3864 port = UHCI_PORTSC1;
3865 else if (index == 2)
3866 port = UHCI_PORTSC2;
3867 else {
3868 return -1;
3869 }
3870 switch(value) {
3871 case UHF_PORT_ENABLE:
3872 x = URWMASK(UREAD2(sc, port));
3873 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3874 break;
3875 case UHF_PORT_SUSPEND:
3876 x = URWMASK(UREAD2(sc, port));
3877 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
3878 break;
3879 case UHF_PORT_RESET:
3880 err = uhci_portreset(sc, index);
3881 if (err != USBD_NORMAL_COMPLETION)
3882 return -1;
3883 return 0;
3884 case UHF_PORT_POWER:
3885 /* Pretend we turned on power */
3886 return 0;
3887 case UHF_C_PORT_CONNECTION:
3888 case UHF_C_PORT_ENABLE:
3889 case UHF_C_PORT_OVER_CURRENT:
3890 case UHF_PORT_CONNECTION:
3891 case UHF_PORT_OVER_CURRENT:
3892 case UHF_PORT_LOW_SPEED:
3893 case UHF_C_PORT_SUSPEND:
3894 case UHF_C_PORT_RESET:
3895 default:
3896 return -1;
3897 }
3898 break;
3899 default:
3900 /* default from usbroothub */
3901 DPRINTF("returning %d (usbroothub default)",
3902 buflen, 0, 0, 0);
3903 return buflen;
3904 }
3905
3906 DPRINTF("returning %d", totlen, 0, 0, 0);
3907
3908 return totlen;
3909 }
3910
3911 /* Abort a root interrupt request. */
3912 void
3913 uhci_root_intr_abort(struct usbd_xfer *xfer)
3914 {
3915 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
3916
3917 KASSERT(mutex_owned(&sc->sc_lock));
3918 KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
3919
3920 callout_stop(&sc->sc_poll_handle);
3921 sc->sc_intr_xfer = NULL;
3922
3923 xfer->ux_status = USBD_CANCELLED;
3924 #ifdef DIAGNOSTIC
3925 UHCI_XFER2UXFER(xfer)->ux_isdone = true;
3926 #endif
3927 usb_transfer_complete(xfer);
3928 }
3929
3930 usbd_status
3931 uhci_root_intr_transfer(struct usbd_xfer *xfer)
3932 {
3933 uhci_softc_t *sc = UHCI_XFER2SC(xfer);
3934 usbd_status err;
3935
3936 /* Insert last in queue. */
3937 mutex_enter(&sc->sc_lock);
3938 err = usb_insert_transfer(xfer);
3939 mutex_exit(&sc->sc_lock);
3940 if (err)
3941 return err;
3942
3943 /*
3944 * Pipe isn't running (otherwise err would be USBD_INPROG),
3945 * start first
3946 */
3947 return uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
3948 }
3949
3950 /* Start a transfer on the root interrupt pipe */
3951 usbd_status
3952 uhci_root_intr_start(struct usbd_xfer *xfer)
3953 {
3954 struct usbd_pipe *pipe = xfer->ux_pipe;
3955 uhci_softc_t *sc = UHCI_PIPE2SC(pipe);
3956 unsigned int ival;
3957
3958 UHCIHIST_FUNC(); UHCIHIST_CALLED();
3959 DPRINTF("xfer=%p len=%d flags=%d", xfer, xfer->ux_length,
3960 xfer->ux_flags, 0);
3961
3962 if (sc->sc_dying)
3963 return USBD_IOERROR;
3964
3965 /* XXX temporary variable needed to avoid gcc3 warning */
3966 ival = xfer->ux_pipe->up_endpoint->ue_edesc->bInterval;
3967 sc->sc_ival = mstohz(ival);
3968 callout_reset(&sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
3969 sc->sc_intr_xfer = xfer;
3970 return USBD_IN_PROGRESS;
3971 }
3972
3973 /* Close the root interrupt pipe. */
3974 void
3975 uhci_root_intr_close(struct usbd_pipe *pipe)
3976 {
3977 uhci_softc_t *sc = UHCI_PIPE2SC(pipe);
3978 UHCIHIST_FUNC(); UHCIHIST_CALLED();
3979
3980 KASSERT(mutex_owned(&sc->sc_lock));
3981
3982 callout_stop(&sc->sc_poll_handle);
3983 sc->sc_intr_xfer = NULL;
3984 }
3985