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