1 /* $NetBSD: uhci.c,v 1.170 2003/02/19 01:35:04 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/uhci.c,v 1.162.2.1 2006/03/01 01:59:04 iedowse Exp $ */
3 /* $DragonFly: src/sys/bus/usb/uhci.c,v 1.21 2007/05/13 22:25:42 swildner Exp $ */
5 /* Also already incorporated from NetBSD:
6 * $NetBSD: uhci.c,v 1.172 2003/02/23 04:19:26 simonb Exp $
7 * $NetBSD: uhci.c,v 1.173 2003/05/13 04:41:59 gson Exp $
8 * $NetBSD: uhci.c,v 1.175 2003/09/12 16:18:08 mycroft Exp $
9 * $NetBSD: uhci.c,v 1.176 2003/11/04 19:11:21 mycroft Exp $
10 * $NetBSD: uhci.c,v 1.177 2003/12/29 08:17:10 toshii Exp $
11 * $NetBSD: uhci.c,v 1.178 2004/03/02 16:32:05 martin Exp $
12 * $NetBSD: uhci.c,v 1.180 2004/07/17 20:12:03 mycroft Exp $
16 * Copyright (c) 1998 The NetBSD Foundation, Inc.
17 * All rights reserved.
19 * This code is derived from software contributed to The NetBSD Foundation
20 * by Lennart Augustsson (lennart@augustsson.net) at
21 * Carlstedt Research & Technology.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * This product includes software developed by the NetBSD
34 * Foundation, Inc. and its contributors.
35 * 4. Neither the name of The NetBSD Foundation nor the names of its
36 * contributors may be used to endorse or promote products derived
37 * from this software without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 * POSSIBILITY OF SUCH DAMAGE.
53 * USB Universal Host Controller driver.
54 * Handles e.g. PIIX3 and PIIX4.
56 * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm
57 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
58 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
59 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/malloc.h>
66 #if defined(__NetBSD__) || defined(__OpenBSD__)
67 #include <sys/device.h>
68 #include <sys/select.h>
69 #elif defined(__FreeBSD__) || defined(__DragonFly__)
70 #include <sys/endian.h>
71 #include <sys/module.h>
73 #if defined(DIAGNOSTIC) && defined(__i386__)
74 #include <machine/cpu.h>
78 #include <sys/queue.h>
79 #include <sys/sysctl.h>
81 #include <sys/thread2.h>
84 #include <machine/endian.h>
86 #include <bus/usb/usb.h>
87 #include <bus/usb/usbdi.h>
88 #include <bus/usb/usbdivar.h>
89 #include <bus/usb/usb_mem.h>
90 #include <bus/usb/usb_quirks.h>
92 #include <bus/usb/uhcireg.h>
93 #include <bus/usb/uhcivar.h>
95 /* Use bandwidth reclamation for control transfers. Some devices choke on it. */
96 /*#define UHCI_CTL_LOOP */
98 #if defined(__FreeBSD__) || defined(__DragonFly__)
99 #define delay(d) DELAY(d)
102 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
104 #if defined(__OpenBSD__)
105 struct cfdriver uhci_cd
= {
106 NULL
, "uhci", DV_DULL
112 #define DPRINTF(x) if (uhcidebug) kprintf x
113 #define DPRINTFN(n,x) if (uhcidebug>(n)) kprintf x
116 SYSCTL_NODE(_hw_usb
, OID_AUTO
, uhci
, CTLFLAG_RW
, 0, "USB uhci");
117 SYSCTL_INT(_hw_usb_uhci
, OID_AUTO
, debug
, CTLFLAG_RW
,
118 &uhcidebug
, 0, "uhci debug level");
119 SYSCTL_INT(_hw_usb_uhci
, OID_AUTO
, loop
, CTLFLAG_RW
,
120 &uhcinoloop
, 0, "uhci noloop");
122 #define bitmask_snprintf(q,f,b,l) ksnprintf((b), (l), "%b", (q), (f))
126 #define DPRINTFN(n,x)
130 * The UHCI controller is little endian, so on big endian machines
131 * the data strored in memory needs to be swapped.
133 #if defined(__OpenBSD__)
134 #if BYTE_ORDER == BIG_ENDIAN
135 #define htole32(x) (bswap32(x))
136 #define le32toh(x) (bswap32(x))
138 #define htole32(x) (x)
139 #define le32toh(x) (x)
144 struct usbd_pipe pipe
;
148 usbd_xfer_handle abortstart
, abortend
;
150 /* Info needed for different pipe kinds. */
156 uhci_soft_td_t
*setup
, *stat
;
163 uhci_soft_qh_t
**qhs
;
173 uhci_soft_td_t
**stds
;
179 Static
void uhci_globalreset(uhci_softc_t
*);
180 Static usbd_status
uhci_portreset(uhci_softc_t
*, int);
181 Static
void uhci_reset(uhci_softc_t
*);
182 #if defined(__NetBSD__) || defined(__OpenBSD__)
183 Static
void uhci_shutdown(void *v
);
184 Static
void uhci_power(int, void *);
186 Static usbd_status
uhci_run(uhci_softc_t
*, int run
);
187 Static uhci_soft_td_t
*uhci_alloc_std(uhci_softc_t
*);
188 Static
void uhci_free_std(uhci_softc_t
*, uhci_soft_td_t
*);
189 Static uhci_soft_qh_t
*uhci_alloc_sqh(uhci_softc_t
*);
190 Static
void uhci_free_sqh(uhci_softc_t
*, uhci_soft_qh_t
*);
192 Static
void uhci_enter_ctl_q(uhci_softc_t
*, uhci_soft_qh_t
*,
194 Static
void uhci_exit_ctl_q(uhci_softc_t
*, uhci_soft_qh_t
*);
197 Static
void uhci_free_std_chain(uhci_softc_t
*,
198 uhci_soft_td_t
*, uhci_soft_td_t
*);
199 Static usbd_status
uhci_alloc_std_chain(struct uhci_pipe
*,
200 uhci_softc_t
*, int, int, u_int16_t
, usb_dma_t
*,
201 uhci_soft_td_t
**, uhci_soft_td_t
**);
202 Static
void uhci_poll_hub(void *);
203 Static
void uhci_waitintr(uhci_softc_t
*, usbd_xfer_handle
);
204 Static
void uhci_check_intr(uhci_softc_t
*, uhci_intr_info_t
*);
205 Static
void uhci_idone(uhci_intr_info_t
*);
207 Static
void uhci_abort_xfer(usbd_xfer_handle
, usbd_status status
);
209 Static
void uhci_timeout(void *);
210 Static
void uhci_timeout_task(void *);
211 Static
void uhci_add_ls_ctrl(uhci_softc_t
*, uhci_soft_qh_t
*);
212 Static
void uhci_add_hs_ctrl(uhci_softc_t
*, uhci_soft_qh_t
*);
213 Static
void uhci_add_bulk(uhci_softc_t
*, uhci_soft_qh_t
*);
214 Static
void uhci_remove_ls_ctrl(uhci_softc_t
*,uhci_soft_qh_t
*);
215 Static
void uhci_remove_hs_ctrl(uhci_softc_t
*,uhci_soft_qh_t
*);
216 Static
void uhci_remove_bulk(uhci_softc_t
*,uhci_soft_qh_t
*);
217 Static
int uhci_str(usb_string_descriptor_t
*, int, char *);
218 Static
void uhci_add_loop(uhci_softc_t
*sc
);
219 Static
void uhci_rem_loop(uhci_softc_t
*sc
);
221 Static usbd_status
uhci_setup_isoc(usbd_pipe_handle pipe
);
222 Static
void uhci_device_isoc_enter(usbd_xfer_handle
);
224 Static usbd_status
uhci_allocm(struct usbd_bus
*, usb_dma_t
*, u_int32_t
);
225 Static
void uhci_freem(struct usbd_bus
*, usb_dma_t
*);
227 Static usbd_xfer_handle
uhci_allocx(struct usbd_bus
*);
228 Static
void uhci_freex(struct usbd_bus
*, usbd_xfer_handle
);
230 Static usbd_status
uhci_device_ctrl_transfer(usbd_xfer_handle
);
231 Static usbd_status
uhci_device_ctrl_start(usbd_xfer_handle
);
232 Static
void uhci_device_ctrl_abort(usbd_xfer_handle
);
233 Static
void uhci_device_ctrl_close(usbd_pipe_handle
);
234 Static
void uhci_device_ctrl_done(usbd_xfer_handle
);
236 Static usbd_status
uhci_device_intr_transfer(usbd_xfer_handle
);
237 Static usbd_status
uhci_device_intr_start(usbd_xfer_handle
);
238 Static
void uhci_device_intr_abort(usbd_xfer_handle
);
239 Static
void uhci_device_intr_close(usbd_pipe_handle
);
240 Static
void uhci_device_intr_done(usbd_xfer_handle
);
242 Static usbd_status
uhci_device_bulk_transfer(usbd_xfer_handle
);
243 Static usbd_status
uhci_device_bulk_start(usbd_xfer_handle
);
244 Static
void uhci_device_bulk_abort(usbd_xfer_handle
);
245 Static
void uhci_device_bulk_close(usbd_pipe_handle
);
246 Static
void uhci_device_bulk_done(usbd_xfer_handle
);
248 Static usbd_status
uhci_device_isoc_transfer(usbd_xfer_handle
);
249 Static usbd_status
uhci_device_isoc_start(usbd_xfer_handle
);
250 Static
void uhci_device_isoc_abort(usbd_xfer_handle
);
251 Static
void uhci_device_isoc_close(usbd_pipe_handle
);
252 Static
void uhci_device_isoc_done(usbd_xfer_handle
);
254 Static usbd_status
uhci_root_ctrl_transfer(usbd_xfer_handle
);
255 Static usbd_status
uhci_root_ctrl_start(usbd_xfer_handle
);
256 Static
void uhci_root_ctrl_abort(usbd_xfer_handle
);
257 Static
void uhci_root_ctrl_close(usbd_pipe_handle
);
258 Static
void uhci_root_ctrl_done(usbd_xfer_handle
);
260 Static usbd_status
uhci_root_intr_transfer(usbd_xfer_handle
);
261 Static usbd_status
uhci_root_intr_start(usbd_xfer_handle
);
262 Static
void uhci_root_intr_abort(usbd_xfer_handle
);
263 Static
void uhci_root_intr_close(usbd_pipe_handle
);
264 Static
void uhci_root_intr_done(usbd_xfer_handle
);
266 Static usbd_status
uhci_open(usbd_pipe_handle
);
267 Static
void uhci_poll(struct usbd_bus
*);
268 Static
void uhci_softintr(void *);
270 Static usbd_status
uhci_device_request(usbd_xfer_handle xfer
);
272 Static
void uhci_add_intr(uhci_softc_t
*, uhci_soft_qh_t
*);
273 Static
void uhci_remove_intr(uhci_softc_t
*, uhci_soft_qh_t
*);
274 Static usbd_status
uhci_device_setintr(uhci_softc_t
*sc
,
275 struct uhci_pipe
*pipe
, int ival
);
277 Static
void uhci_device_clear_toggle(usbd_pipe_handle pipe
);
278 Static
void uhci_noop(usbd_pipe_handle pipe
);
280 Static __inline uhci_soft_qh_t
*uhci_find_prev_qh(uhci_soft_qh_t
*,
284 Static
void uhci_dump_all(uhci_softc_t
*);
285 Static
void uhci_dumpregs(uhci_softc_t
*);
286 Static
void uhci_dump_qhs(uhci_soft_qh_t
*);
287 Static
void uhci_dump_qh(uhci_soft_qh_t
*);
288 Static
void uhci_dump_tds(uhci_soft_td_t
*);
289 Static
void uhci_dump_td(uhci_soft_td_t
*);
290 Static
void uhci_dump_ii(uhci_intr_info_t
*ii
);
291 void uhci_dump(void);
294 #define UBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
295 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
296 #define UWRITE1(sc, r, x) \
297 do { UBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); \
298 } while (/*CONSTCOND*/0)
299 #define UWRITE2(sc, r, x) \
300 do { UBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); \
301 } while (/*CONSTCOND*/0)
302 #define UWRITE4(sc, r, x) \
303 do { UBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); \
304 } while (/*CONSTCOND*/0)
305 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
306 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
307 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
309 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
310 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
312 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
314 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
316 #define UHCI_INTR_ENDPT 1
318 struct usbd_bus_methods uhci_bus_methods
= {
328 struct usbd_pipe_methods uhci_root_ctrl_methods
= {
329 uhci_root_ctrl_transfer
,
330 uhci_root_ctrl_start
,
331 uhci_root_ctrl_abort
,
332 uhci_root_ctrl_close
,
337 struct usbd_pipe_methods uhci_root_intr_methods
= {
338 uhci_root_intr_transfer
,
339 uhci_root_intr_start
,
340 uhci_root_intr_abort
,
341 uhci_root_intr_close
,
346 struct usbd_pipe_methods uhci_device_ctrl_methods
= {
347 uhci_device_ctrl_transfer
,
348 uhci_device_ctrl_start
,
349 uhci_device_ctrl_abort
,
350 uhci_device_ctrl_close
,
352 uhci_device_ctrl_done
,
355 struct usbd_pipe_methods uhci_device_intr_methods
= {
356 uhci_device_intr_transfer
,
357 uhci_device_intr_start
,
358 uhci_device_intr_abort
,
359 uhci_device_intr_close
,
360 uhci_device_clear_toggle
,
361 uhci_device_intr_done
,
364 struct usbd_pipe_methods uhci_device_bulk_methods
= {
365 uhci_device_bulk_transfer
,
366 uhci_device_bulk_start
,
367 uhci_device_bulk_abort
,
368 uhci_device_bulk_close
,
369 uhci_device_clear_toggle
,
370 uhci_device_bulk_done
,
373 struct usbd_pipe_methods uhci_device_isoc_methods
= {
374 uhci_device_isoc_transfer
,
375 uhci_device_isoc_start
,
376 uhci_device_isoc_abort
,
377 uhci_device_isoc_close
,
379 uhci_device_isoc_done
,
382 #define uhci_add_intr_info(sc, ii) \
383 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ii), list)
384 #define uhci_del_intr_info(ii) \
386 LIST_REMOVE((ii), list); \
387 (ii)->list.le_prev = NULL; \
389 #define uhci_active_intr_info(ii) ((ii)->list.le_prev != NULL)
391 Static __inline uhci_soft_qh_t
*
392 uhci_find_prev_qh(uhci_soft_qh_t
*pqh
, uhci_soft_qh_t
*sqh
)
394 DPRINTFN(15,("uhci_find_prev_qh: pqh=%p sqh=%p\n", pqh
, sqh
));
396 for (; pqh
->hlink
!= sqh
; pqh
= pqh
->hlink
) {
397 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
398 if (le32toh(pqh
->qh
.qh_hlink
) & UHCI_PTR_T
) {
399 kprintf("uhci_find_prev_qh: QH not found\n");
408 uhci_globalreset(uhci_softc_t
*sc
)
410 UHCICMD(sc
, UHCI_CMD_GRESET
); /* global reset */
411 usb_delay_ms(&sc
->sc_bus
, USB_BUS_RESET_DELAY
); /* wait a little */
412 UHCICMD(sc
, 0); /* do nothing */
416 uhci_init(uhci_softc_t
*sc
)
420 uhci_soft_qh_t
*clsqh
, *chsqh
, *bsqh
, *sqh
, *lsqh
;
423 DPRINTFN(1,("uhci_init: start\n"));
432 UWRITE2(sc
, UHCI_INTR
, 0); /* disable interrupts */
433 uhci_globalreset(sc
); /* reset the controller */
436 /* Allocate and initialize real frame array. */
437 err
= usb_allocmem(&sc
->sc_bus
,
438 UHCI_FRAMELIST_COUNT
* sizeof(uhci_physaddr_t
),
439 UHCI_FRAMELIST_ALIGN
, &sc
->sc_dma
);
442 sc
->sc_pframes
= KERNADDR(&sc
->sc_dma
, 0);
443 UWRITE2(sc
, UHCI_FRNUM
, 0); /* set frame number to 0 */
444 UWRITE4(sc
, UHCI_FLBASEADDR
, DMAADDR(&sc
->sc_dma
, 0)); /* set frame list*/
447 * Allocate a TD, inactive, that hangs from the last QH.
448 * This is to avoid a bug in the PIIX that makes it run berserk
451 std
= uhci_alloc_std(sc
);
454 std
->link
.std
= NULL
;
455 std
->td
.td_link
= htole32(UHCI_PTR_T
);
456 std
->td
.td_status
= htole32(0); /* inactive */
457 std
->td
.td_token
= htole32(0);
458 std
->td
.td_buffer
= htole32(0);
460 /* Allocate the dummy QH marking the end and used for looping the QHs.*/
461 lsqh
= uhci_alloc_sqh(sc
);
465 lsqh
->qh
.qh_hlink
= htole32(UHCI_PTR_T
); /* end of QH chain */
467 lsqh
->qh
.qh_elink
= htole32(std
->physaddr
| UHCI_PTR_TD
);
468 sc
->sc_last_qh
= lsqh
;
470 /* Allocate the dummy QH where bulk traffic will be queued. */
471 bsqh
= uhci_alloc_sqh(sc
);
475 bsqh
->qh
.qh_hlink
= htole32(lsqh
->physaddr
| UHCI_PTR_QH
);
477 bsqh
->qh
.qh_elink
= htole32(UHCI_PTR_T
);
478 sc
->sc_bulk_start
= sc
->sc_bulk_end
= bsqh
;
480 /* Allocate dummy QH where high speed control traffic will be queued. */
481 chsqh
= uhci_alloc_sqh(sc
);
485 chsqh
->qh
.qh_hlink
= htole32(bsqh
->physaddr
| UHCI_PTR_QH
);
487 chsqh
->qh
.qh_elink
= htole32(UHCI_PTR_T
);
488 sc
->sc_hctl_start
= sc
->sc_hctl_end
= chsqh
;
490 /* Allocate dummy QH where control traffic will be queued. */
491 clsqh
= uhci_alloc_sqh(sc
);
495 clsqh
->qh
.qh_hlink
= htole32(chsqh
->physaddr
| UHCI_PTR_QH
);
497 clsqh
->qh
.qh_elink
= htole32(UHCI_PTR_T
);
498 sc
->sc_lctl_start
= sc
->sc_lctl_end
= clsqh
;
501 * Make all (virtual) frame list pointers point to the interrupt
502 * queue heads and the interrupt queue heads at the control
503 * queue head and point the physical frame list to the virtual.
505 for(i
= 0; i
< UHCI_VFRAMELIST_COUNT
; i
++) {
506 std
= uhci_alloc_std(sc
);
507 sqh
= uhci_alloc_sqh(sc
);
508 if (std
== NULL
|| sqh
== NULL
)
512 std
->td
.td_link
= htole32(sqh
->physaddr
| UHCI_PTR_QH
);
513 std
->td
.td_status
= htole32(UHCI_TD_IOS
); /* iso, inactive */
514 std
->td
.td_token
= htole32(0);
515 std
->td
.td_buffer
= htole32(0);
517 sqh
->qh
.qh_hlink
= htole32(clsqh
->physaddr
| UHCI_PTR_QH
);
519 sqh
->qh
.qh_elink
= htole32(UHCI_PTR_T
);
520 sc
->sc_vframes
[i
].htd
= std
;
521 sc
->sc_vframes
[i
].etd
= std
;
522 sc
->sc_vframes
[i
].hqh
= sqh
;
523 sc
->sc_vframes
[i
].eqh
= sqh
;
525 j
< UHCI_FRAMELIST_COUNT
;
526 j
+= UHCI_VFRAMELIST_COUNT
) {
527 sc
->sc_pframes
[j
] = htole32(std
->physaddr
);
531 LIST_INIT(&sc
->sc_intrhead
);
533 SIMPLEQ_INIT(&sc
->sc_free_xfers
);
535 usb_callout_init(sc
->sc_poll_handle
);
537 /* Set up the bus struct. */
538 sc
->sc_bus
.methods
= &uhci_bus_methods
;
539 sc
->sc_bus
.pipe_size
= sizeof(struct uhci_pipe
);
541 #if defined(__NetBSD__) || defined(__OpenBSD__)
542 sc
->sc_suspend
= PWR_RESUME
;
543 sc
->sc_powerhook
= powerhook_establish(uhci_power
, sc
);
544 sc
->sc_shutdownhook
= shutdownhook_establish(uhci_shutdown
, sc
);
546 DPRINTFN(1,("uhci_init: enabling\n"));
547 UWRITE2(sc
, UHCI_INTR
, UHCI_INTR_TOCRCIE
| UHCI_INTR_RIE
|
548 UHCI_INTR_IOCE
| UHCI_INTR_SPIE
); /* enable interrupts */
550 UHCICMD(sc
, UHCI_CMD_MAXP
); /* Assume 64 byte packets at frame end */
552 return (uhci_run(sc
, 1)); /* and here we go... */
555 #if defined(__NetBSD__) || defined(__OpenBSD__)
557 uhci_activate(device_ptr_t self
, enum devact act
)
559 struct uhci_softc
*sc
= (struct uhci_softc
*)self
;
566 case DVACT_DEACTIVATE
:
567 if (sc
->sc_child
!= NULL
)
568 rv
= config_deactivate(sc
->sc_child
);
576 uhci_detach(struct uhci_softc
*sc
, int flags
)
578 usbd_xfer_handle xfer
;
581 #if defined(__NetBSD__) || defined(__OpenBSD__)
582 if (sc
->sc_child
!= NULL
)
583 rv
= config_detach(sc
->sc_child
, flags
);
589 UWRITE2(sc
, UHCI_INTR
, 0); /* disable interrupts */
592 #if defined(__NetBSD__) || defined(__OpenBSD__)
593 powerhook_disestablish(sc
->sc_powerhook
);
594 shutdownhook_disestablish(sc
->sc_shutdownhook
);
597 /* Free all xfers associated with this HC. */
599 xfer
= SIMPLEQ_FIRST(&sc
->sc_free_xfers
);
602 SIMPLEQ_REMOVE_HEAD(&sc
->sc_free_xfers
, next
);
606 /* XXX free other data structures XXX */
607 usb_freemem(&sc
->sc_bus
, &sc
->sc_dma
);
613 uhci_allocm(struct usbd_bus
*bus
, usb_dma_t
*dma
, u_int32_t size
)
615 return (usb_allocmem(bus
, size
, 0, dma
));
619 uhci_freem(struct usbd_bus
*bus
, usb_dma_t
*dma
)
621 usb_freemem(bus
, dma
);
625 uhci_allocx(struct usbd_bus
*bus
)
627 struct uhci_softc
*sc
= (struct uhci_softc
*)bus
;
628 usbd_xfer_handle xfer
;
630 xfer
= SIMPLEQ_FIRST(&sc
->sc_free_xfers
);
632 SIMPLEQ_REMOVE_HEAD(&sc
->sc_free_xfers
, next
);
634 if (xfer
->busy_free
!= XFER_FREE
) {
635 kprintf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer
,
640 xfer
= kmalloc(sizeof(struct uhci_xfer
), M_USB
, M_INTWAIT
);
643 memset(xfer
, 0, sizeof (struct uhci_xfer
));
644 UXFER(xfer
)->iinfo
.sc
= sc
;
645 usb_init_task(&UXFER(xfer
)->abort_task
, uhci_timeout_task
,
647 UXFER(xfer
)->uhci_xfer_flags
= 0;
649 UXFER(xfer
)->iinfo
.isdone
= 1;
650 xfer
->busy_free
= XFER_BUSY
;
657 uhci_freex(struct usbd_bus
*bus
, usbd_xfer_handle xfer
)
659 struct uhci_softc
*sc
= (struct uhci_softc
*)bus
;
662 if (xfer
->busy_free
!= XFER_BUSY
) {
663 kprintf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer
,
667 xfer
->busy_free
= XFER_FREE
;
668 if (!UXFER(xfer
)->iinfo
.isdone
) {
669 kprintf("uhci_freex: !isdone\n");
673 SIMPLEQ_INSERT_HEAD(&sc
->sc_free_xfers
, xfer
, next
);
677 * Shut down the controller when the system is going down.
680 uhci_shutdown(void *v
)
682 uhci_softc_t
*sc
= v
;
684 DPRINTF(("uhci_shutdown: stopping the HC\n"));
685 uhci_run(sc
, 0); /* stop the controller */
689 * Handle suspend/resume.
691 * We need to switch to polling mode here, because this routine is
692 * called from an interrupt context. This is all right since we
693 * are almost suspended anyway.
696 uhci_power(int why
, void *v
)
698 uhci_softc_t
*sc
= v
;
702 cmd
= UREAD2(sc
, UHCI_CMD
);
704 DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
705 sc
, why
, sc
->sc_suspend
, cmd
));
707 if (why
!= PWR_RESUME
) {
712 if (sc
->sc_intr_xfer
!= NULL
)
713 usb_uncallout(sc
->sc_poll_handle
, uhci_poll_hub
,
715 sc
->sc_bus
.use_polling
++;
716 uhci_run(sc
, 0); /* stop the controller */
718 /* save some state if BIOS doesn't */
719 sc
->sc_saved_frnum
= UREAD2(sc
, UHCI_FRNUM
);
720 sc
->sc_saved_sof
= UREAD1(sc
, UHCI_SOF
);
722 UWRITE2(sc
, UHCI_INTR
, 0); /* disable intrs */
724 UHCICMD(sc
, cmd
| UHCI_CMD_EGSM
); /* enter global suspend */
725 usb_delay_ms(&sc
->sc_bus
, USB_RESUME_WAIT
);
726 sc
->sc_suspend
= why
;
727 sc
->sc_bus
.use_polling
--;
728 DPRINTF(("uhci_power: cmd=0x%x\n", UREAD2(sc
, UHCI_CMD
)));
731 if (sc
->sc_suspend
== PWR_RESUME
)
732 kprintf("uhci_power: weird, resume without suspend.\n");
734 sc
->sc_bus
.use_polling
++;
735 sc
->sc_suspend
= why
;
736 UWRITE2(sc
, UHCI_INTR
, 0); /* disable interrupts */
737 uhci_globalreset(sc
); /* reset the controller */
739 if (cmd
& UHCI_CMD_RS
)
740 uhci_run(sc
, 0); /* in case BIOS has started it */
742 uhci_globalreset(sc
);
745 /* restore saved state */
746 UWRITE4(sc
, UHCI_FLBASEADDR
, DMAADDR(&sc
->sc_dma
, 0));
747 UWRITE2(sc
, UHCI_FRNUM
, sc
->sc_saved_frnum
);
748 UWRITE1(sc
, UHCI_SOF
, sc
->sc_saved_sof
);
750 UHCICMD(sc
, cmd
| UHCI_CMD_FGR
); /* force global resume */
751 usb_delay_ms(&sc
->sc_bus
, USB_RESUME_DELAY
);
752 UHCICMD(sc
, cmd
& ~UHCI_CMD_EGSM
); /* back to normal */
753 UWRITE2(sc
, UHCI_INTR
, UHCI_INTR_TOCRCIE
| UHCI_INTR_RIE
|
754 UHCI_INTR_IOCE
| UHCI_INTR_SPIE
); /* re-enable intrs */
755 UHCICMD(sc
, UHCI_CMD_MAXP
);
756 uhci_run(sc
, 1); /* and start traffic again */
757 usb_delay_ms(&sc
->sc_bus
, USB_RESUME_RECOVERY
);
758 sc
->sc_bus
.use_polling
--;
759 if (sc
->sc_intr_xfer
!= NULL
)
760 usb_callout(sc
->sc_poll_handle
, sc
->sc_ival
,
761 uhci_poll_hub
, sc
->sc_intr_xfer
);
772 uhci_dumpregs(uhci_softc_t
*sc
)
774 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
775 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
776 USBDEVNAME(sc
->sc_bus
.bdev
),
777 UREAD2(sc
, UHCI_CMD
),
778 UREAD2(sc
, UHCI_STS
),
779 UREAD2(sc
, UHCI_INTR
),
780 UREAD2(sc
, UHCI_FRNUM
),
781 UREAD4(sc
, UHCI_FLBASEADDR
),
782 UREAD1(sc
, UHCI_SOF
),
783 UREAD2(sc
, UHCI_PORTSC1
),
784 UREAD2(sc
, UHCI_PORTSC2
)));
788 uhci_dump_td(uhci_soft_td_t
*p
)
790 char sbuf
[128], sbuf2
[128];
792 DPRINTFN(-1,("TD(%p) at %08lx = link=0x%08lx status=0x%08lx "
793 "token=0x%08lx buffer=0x%08lx\n",
794 p
, (long)p
->physaddr
,
795 (long)le32toh(p
->td
.td_link
),
796 (long)le32toh(p
->td
.td_status
),
797 (long)le32toh(p
->td
.td_token
),
798 (long)le32toh(p
->td
.td_buffer
)));
800 bitmask_snprintf((u_int32_t
)le32toh(p
->td
.td_link
), "\20\1T\2Q\3VF",
802 bitmask_snprintf((u_int32_t
)le32toh(p
->td
.td_status
),
803 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
804 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
805 sbuf2
, sizeof(sbuf2
));
807 DPRINTFN(-1,(" %s %s,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
808 "D=%d,maxlen=%d\n", sbuf
, sbuf2
,
809 UHCI_TD_GET_ERRCNT(le32toh(p
->td
.td_status
)),
810 UHCI_TD_GET_ACTLEN(le32toh(p
->td
.td_status
)),
811 UHCI_TD_GET_PID(le32toh(p
->td
.td_token
)),
812 UHCI_TD_GET_DEVADDR(le32toh(p
->td
.td_token
)),
813 UHCI_TD_GET_ENDPT(le32toh(p
->td
.td_token
)),
814 UHCI_TD_GET_DT(le32toh(p
->td
.td_token
)),
815 UHCI_TD_GET_MAXLEN(le32toh(p
->td
.td_token
))));
819 uhci_dump_qh(uhci_soft_qh_t
*sqh
)
821 DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh
,
822 (int)sqh
->physaddr
, le32toh(sqh
->qh
.qh_hlink
),
823 le32toh(sqh
->qh
.qh_elink
)));
830 uhci_dump_all(thesc
);
835 uhci_dump_all(uhci_softc_t
*sc
)
838 kprintf("intrs=%d\n", sc
->sc_bus
.no_intrs
);
839 /*kprintf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);*/
840 uhci_dump_qh(sc
->sc_lctl_start
);
844 uhci_dump_qhs(uhci_soft_qh_t
*sqh
)
848 /* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
849 * Traverses sideways first, then down.
859 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
863 if (sqh
->hlink
!= NULL
&& !(le32toh(sqh
->qh
.qh_hlink
) & UHCI_PTR_T
))
864 uhci_dump_qhs(sqh
->hlink
);
866 DPRINTF(("No QH\n"));
868 if (sqh
->elink
!= NULL
&& !(le32toh(sqh
->qh
.qh_elink
) & UHCI_PTR_T
))
869 uhci_dump_tds(sqh
->elink
);
871 DPRINTF(("No TD\n"));
875 uhci_dump_tds(uhci_soft_td_t
*std
)
879 for(td
= std
; td
!= NULL
; td
= td
->link
.std
) {
882 /* Check whether the link pointer in this TD marks
883 * the link pointer as end of queue. This avoids
884 * printing the free list in case the queue/TD has
885 * already been moved there (seatbelt).
887 if (le32toh(td
->td
.td_link
) & UHCI_PTR_T
||
888 le32toh(td
->td
.td_link
) == 0)
894 uhci_dump_ii(uhci_intr_info_t
*ii
)
896 usbd_pipe_handle pipe
;
897 usb_endpoint_descriptor_t
*ed
;
898 usbd_device_handle dev
;
901 #define DONE ii->isdone
906 kprintf("ii NULL\n");
909 if (ii
->xfer
== NULL
) {
910 kprintf("ii %p: done=%d xfer=NULL\n",
914 pipe
= ii
->xfer
->pipe
;
916 kprintf("ii %p: done=%d xfer=%p pipe=NULL\n",
920 if (pipe
->endpoint
== NULL
) {
921 kprintf("ii %p: done=%d xfer=%p pipe=%p pipe->endpoint=NULL\n",
922 ii
, DONE
, ii
->xfer
, pipe
);
925 if (pipe
->device
== NULL
) {
926 kprintf("ii %p: done=%d xfer=%p pipe=%p pipe->device=NULL\n",
927 ii
, DONE
, ii
->xfer
, pipe
);
930 ed
= pipe
->endpoint
->edesc
;
932 kprintf("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",
933 ii
, DONE
, ii
->xfer
, dev
,
934 UGETW(dev
->ddesc
.idVendor
),
935 UGETW(dev
->ddesc
.idProduct
),
937 ed
->bEndpointAddress
, ed
->bmAttributes
);
941 void uhci_dump_iis(struct uhci_softc
*sc
);
943 uhci_dump_iis(struct uhci_softc
*sc
)
945 uhci_intr_info_t
*ii
;
947 kprintf("intr_info list:\n");
948 for (ii
= LIST_FIRST(&sc
->sc_intrhead
); ii
; ii
= LIST_NEXT(ii
, list
))
953 void iidump(void) { uhci_dump_iis(thesc
); }
958 * This routine is executed periodically and simulates interrupts
959 * from the root controller interrupt pipe for port status change.
962 uhci_poll_hub(void *addr
)
964 usbd_xfer_handle xfer
= addr
;
965 usbd_pipe_handle pipe
= xfer
->pipe
;
966 uhci_softc_t
*sc
= (uhci_softc_t
*)pipe
->device
->bus
;
969 DPRINTFN(20, ("uhci_poll_hub\n"));
971 usb_callout(sc
->sc_poll_handle
, sc
->sc_ival
, uhci_poll_hub
, xfer
);
973 p
= KERNADDR(&xfer
->dmabuf
, 0);
975 if (UREAD2(sc
, UHCI_PORTSC1
) & (UHCI_PORTSC_CSC
|UHCI_PORTSC_OCIC
))
977 if (UREAD2(sc
, UHCI_PORTSC2
) & (UHCI_PORTSC_CSC
|UHCI_PORTSC_OCIC
))
980 /* No change, try again in a while */
984 xfer
->status
= USBD_NORMAL_COMPLETION
;
986 xfer
->device
->bus
->intr_context
++;
987 usb_transfer_complete(xfer
);
988 xfer
->device
->bus
->intr_context
--;
993 uhci_root_intr_done(usbd_xfer_handle xfer
)
998 uhci_root_ctrl_done(usbd_xfer_handle xfer
)
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.
1009 uhci_add_loop(uhci_softc_t
*sc
) {
1014 if (++sc
->sc_loops
== 1) {
1015 DPRINTFN(5,("uhci_start_loop: add\n"));
1016 /* Note, we don't loop back the soft pointer. */
1017 sc
->sc_last_qh
->qh
.qh_hlink
=
1018 htole32(sc
->sc_hctl_start
->physaddr
| UHCI_PTR_QH
);
1023 uhci_rem_loop(uhci_softc_t
*sc
) {
1028 if (--sc
->sc_loops
== 0) {
1029 DPRINTFN(5,("uhci_end_loop: remove\n"));
1030 sc
->sc_last_qh
->qh
.qh_hlink
= htole32(UHCI_PTR_T
);
1034 /* Add high speed control QH, called from a critical section. */
1036 uhci_add_hs_ctrl(uhci_softc_t
*sc
, uhci_soft_qh_t
*sqh
)
1038 uhci_soft_qh_t
*eqh
;
1040 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh
));
1041 eqh
= sc
->sc_hctl_end
;
1042 sqh
->hlink
= eqh
->hlink
;
1043 sqh
->qh
.qh_hlink
= eqh
->qh
.qh_hlink
;
1045 eqh
->qh
.qh_hlink
= htole32(sqh
->physaddr
| UHCI_PTR_QH
);
1046 sc
->sc_hctl_end
= sqh
;
1047 #ifdef UHCI_CTL_LOOP
1052 /* Remove high speed control QH, called from a critical section. */
1054 uhci_remove_hs_ctrl(uhci_softc_t
*sc
, uhci_soft_qh_t
*sqh
)
1056 uhci_soft_qh_t
*pqh
;
1058 DPRINTFN(10, ("uhci_remove_hs_ctrl: sqh=%p\n", sqh
));
1059 #ifdef UHCI_CTL_LOOP
1063 * The T bit should be set in the elink of the QH so that the HC
1064 * doesn't follow the pointer. This condition may fail if the
1065 * the transferred packet was short so that the QH still points
1066 * at the last used TD.
1067 * In this case we set the T bit and wait a little for the HC
1068 * to stop looking at the TD.
1070 if (!(sqh
->qh
.qh_elink
& htole32(UHCI_PTR_T
))) {
1071 sqh
->qh
.qh_elink
= htole32(UHCI_PTR_T
);
1072 delay(UHCI_QH_REMOVE_DELAY
);
1075 pqh
= uhci_find_prev_qh(sc
->sc_hctl_start
, sqh
);
1076 pqh
->hlink
= sqh
->hlink
;
1077 pqh
->qh
.qh_hlink
= sqh
->qh
.qh_hlink
;
1078 delay(UHCI_QH_REMOVE_DELAY
);
1079 if (sc
->sc_hctl_end
== sqh
)
1080 sc
->sc_hctl_end
= pqh
;
1083 /* Add low speed control QH, called from a critical section. */
1085 uhci_add_ls_ctrl(uhci_softc_t
*sc
, uhci_soft_qh_t
*sqh
)
1087 uhci_soft_qh_t
*eqh
;
1089 DPRINTFN(10, ("uhci_add_ls_ctrl: sqh=%p\n", sqh
));
1090 eqh
= sc
->sc_lctl_end
;
1091 sqh
->hlink
= eqh
->hlink
;
1092 sqh
->qh
.qh_hlink
= eqh
->qh
.qh_hlink
;
1094 eqh
->qh
.qh_hlink
= htole32(sqh
->physaddr
| UHCI_PTR_QH
);
1095 sc
->sc_lctl_end
= sqh
;
1098 /* Remove low speed control QH, called from a critical section. */
1100 uhci_remove_ls_ctrl(uhci_softc_t
*sc
, uhci_soft_qh_t
*sqh
)
1102 uhci_soft_qh_t
*pqh
;
1104 DPRINTFN(10, ("uhci_remove_ls_ctrl: sqh=%p\n", sqh
));
1105 /* See comment in uhci_remove_hs_ctrl() */
1106 if (!(sqh
->qh
.qh_elink
& htole32(UHCI_PTR_T
))) {
1107 sqh
->qh
.qh_elink
= htole32(UHCI_PTR_T
);
1108 delay(UHCI_QH_REMOVE_DELAY
);
1110 pqh
= uhci_find_prev_qh(sc
->sc_lctl_start
, sqh
);
1111 pqh
->hlink
= sqh
->hlink
;
1112 pqh
->qh
.qh_hlink
= sqh
->qh
.qh_hlink
;
1113 delay(UHCI_QH_REMOVE_DELAY
);
1114 if (sc
->sc_lctl_end
== sqh
)
1115 sc
->sc_lctl_end
= pqh
;
1118 /* Add bulk QH, called from a critical section. */
1120 uhci_add_bulk(uhci_softc_t
*sc
, uhci_soft_qh_t
*sqh
)
1122 uhci_soft_qh_t
*eqh
;
1124 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh
));
1125 eqh
= sc
->sc_bulk_end
;
1126 sqh
->hlink
= eqh
->hlink
;
1127 sqh
->qh
.qh_hlink
= eqh
->qh
.qh_hlink
;
1129 eqh
->qh
.qh_hlink
= htole32(sqh
->physaddr
| UHCI_PTR_QH
);
1130 sc
->sc_bulk_end
= sqh
;
1134 /* Remove bulk QH, called from a critical section. */
1136 uhci_remove_bulk(uhci_softc_t
*sc
, uhci_soft_qh_t
*sqh
)
1138 uhci_soft_qh_t
*pqh
;
1140 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh
));
1142 /* See comment in uhci_remove_hs_ctrl() */
1143 if (!(sqh
->qh
.qh_elink
& htole32(UHCI_PTR_T
))) {
1144 sqh
->qh
.qh_elink
= htole32(UHCI_PTR_T
);
1145 delay(UHCI_QH_REMOVE_DELAY
);
1147 pqh
= uhci_find_prev_qh(sc
->sc_bulk_start
, sqh
);
1148 pqh
->hlink
= sqh
->hlink
;
1149 pqh
->qh
.qh_hlink
= sqh
->qh
.qh_hlink
;
1150 delay(UHCI_QH_REMOVE_DELAY
);
1151 if (sc
->sc_bulk_end
== sqh
)
1152 sc
->sc_bulk_end
= pqh
;
1155 Static
int uhci_intr1(uhci_softc_t
*);
1158 uhci_intr(void *arg
)
1160 uhci_softc_t
*sc
= arg
;
1162 if (sc
->sc_dying
|| (sc
->sc_flags
& UHCI_SCFLG_DONEINIT
) == 0)
1165 DPRINTFN(15,("uhci_intr: real interrupt\n"));
1166 if (sc
->sc_bus
.use_polling
) {
1168 DPRINTFN(16, ("uhci_intr: ignored interrupt while polling\n"));
1173 return (uhci_intr1(sc
));
1177 uhci_intr1(uhci_softc_t
*sc
)
1184 * It can happen that an interrupt will be delivered to
1185 * us before the device has been fully attached and the
1186 * softc struct has been configured. Usually this happens
1187 * when kldloading the USB support as a module after the
1188 * system has been booted. If we detect this condition,
1189 * we need to squelch the unwanted interrupts until we're
1192 if (sc
->sc_bus
.bdev
== NULL
) {
1193 UWRITE2(sc
, UHCI_STS
, 0xFFFF); /* ack pending interrupts */
1194 uhci_run(sc
, 0); /* stop the controller */
1195 UWRITE2(sc
, UHCI_INTR
, 0); /* disable interrupts */
1200 if (uhcidebug
> 15) {
1201 DPRINTF(("%s: uhci_intr1\n", USBDEVNAME(sc
->sc_bus
.bdev
)));
1205 status
= UREAD2(sc
, UHCI_STS
) & UHCI_STS_ALLINTRS
;
1206 if (status
== 0) /* The interrupt was not for us. */
1209 #if defined(DIAGNOSTIC) && defined(__NetBSD__)
1210 if (sc
->sc_suspend
!= PWR_RESUME
)
1211 kprintf("uhci_intr: suspended sts=0x%x\n", status
);
1214 if (sc
->sc_suspend
!= PWR_RESUME
) {
1215 kprintf("%s: interrupt while not operating ignored\n",
1216 USBDEVNAME(sc
->sc_bus
.bdev
));
1217 UWRITE2(sc
, UHCI_STS
, status
); /* acknowledge the ints */
1222 if (status
& UHCI_STS_USBINT
)
1223 ack
|= UHCI_STS_USBINT
;
1224 if (status
& UHCI_STS_USBEI
)
1225 ack
|= UHCI_STS_USBEI
;
1226 if (status
& UHCI_STS_RD
) {
1229 kprintf("%s: resume detect\n", USBDEVNAME(sc
->sc_bus
.bdev
));
1232 if (status
& UHCI_STS_HSE
) {
1233 ack
|= UHCI_STS_HSE
;
1234 kprintf("%s: host system error\n", USBDEVNAME(sc
->sc_bus
.bdev
));
1236 if (status
& UHCI_STS_HCPE
) {
1237 ack
|= UHCI_STS_HCPE
;
1238 kprintf("%s: host controller process error\n",
1239 USBDEVNAME(sc
->sc_bus
.bdev
));
1241 if (status
& UHCI_STS_HCH
) {
1242 /* no acknowledge needed */
1243 if (!sc
->sc_dying
) {
1244 kprintf("%s: host controller halted\n",
1245 USBDEVNAME(sc
->sc_bus
.bdev
));
1254 return (0); /* nothing to acknowledge */
1255 UWRITE2(sc
, UHCI_STS
, ack
); /* acknowledge the ints */
1257 sc
->sc_bus
.no_intrs
++;
1258 usb_schedsoftintr(&sc
->sc_bus
);
1260 DPRINTFN(15, ("%s: uhci_intr: exit\n", USBDEVNAME(sc
->sc_bus
.bdev
)));
1266 uhci_softintr(void *v
)
1268 uhci_softc_t
*sc
= v
;
1269 uhci_intr_info_t
*ii
, *nextii
;
1271 DPRINTFN(10,("%s: uhci_softintr (%d)\n", USBDEVNAME(sc
->sc_bus
.bdev
),
1272 sc
->sc_bus
.intr_context
));
1274 sc
->sc_bus
.intr_context
++;
1277 * Interrupts on UHCI really suck. When the host controller
1278 * interrupts because a transfer is completed there is no
1279 * way of knowing which transfer it was. You can scan down
1280 * the TDs and QHs of the previous frame to limit the search,
1281 * but that assumes that the interrupt was not delayed by more
1282 * than 1 ms, which may not always be true (e.g. after debug
1283 * output on a slow console).
1284 * We scan all interrupt descriptors to see if any have
1287 LIST_FOREACH_MUTABLE(ii
, &sc
->sc_intrhead
, list
, nextii
)
1288 uhci_check_intr(sc
, ii
);
1290 #ifdef USB_USE_SOFTINTR
1291 if (sc
->sc_softwake
) {
1292 sc
->sc_softwake
= 0;
1293 wakeup(&sc
->sc_softwake
);
1295 #endif /* USB_USE_SOFTINTR */
1297 sc
->sc_bus
.intr_context
--;
1300 /* Check for an interrupt. */
1302 uhci_check_intr(uhci_softc_t
*sc
, uhci_intr_info_t
*ii
)
1304 uhci_soft_td_t
*std
, *lstd
;
1307 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii
));
1310 kprintf("uhci_check_intr: no ii? %p\n", ii
);
1314 if (ii
->xfer
->status
== USBD_CANCELLED
||
1315 ii
->xfer
->status
== USBD_TIMEOUT
) {
1316 DPRINTF(("uhci_check_intr: aborted xfer=%p\n", ii
->xfer
));
1320 if (ii
->stdstart
== NULL
)
1325 kprintf("uhci_check_intr: std==0\n");
1330 * If the last TD is still active we need to check whether there
1331 * is an error somewhere in the middle, or whether there was a
1332 * short packet (SPD and not ACTIVE).
1334 if (le32toh(lstd
->td
.td_status
) & UHCI_TD_ACTIVE
) {
1335 DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii
));
1336 for (std
= ii
->stdstart
; std
!= lstd
; std
= std
->link
.std
) {
1337 status
= le32toh(std
->td
.td_status
);
1338 /* If there's an active TD the xfer isn't done. */
1339 if (status
& UHCI_TD_ACTIVE
)
1341 /* Any kind of error makes the xfer done. */
1342 if (status
& UHCI_TD_STALLED
)
1344 /* We want short packets, and it is short: it's done */
1345 if ((status
& UHCI_TD_SPD
) &&
1346 UHCI_TD_GET_ACTLEN(status
) <
1347 UHCI_TD_GET_MAXLEN(le32toh(std
->td
.td_token
))) {
1351 DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n",
1356 DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii
));
1357 usb_uncallout(ii
->xfer
->timeout_handle
, uhci_timeout
, ii
);
1358 usb_rem_task(ii
->xfer
->pipe
->device
, &UXFER(ii
->xfer
)->abort_task
);
1362 /* Called from a critical section. */
1364 uhci_idone(uhci_intr_info_t
*ii
)
1366 usbd_xfer_handle xfer
= ii
->xfer
;
1367 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)xfer
->pipe
;
1368 uhci_soft_td_t
*std
;
1369 u_int32_t status
= 0, nstatus
;
1372 DPRINTFN(12, ("uhci_idone: ii=%p\n", ii
));
1379 kprintf("uhci_idone: ii is done!\n ");
1382 kprintf("uhci_idone: ii=%p is done!\n", ii
);
1391 if (xfer
->nframes
!= 0) {
1392 /* Isoc transfer, do things differently. */
1393 uhci_soft_td_t
**stds
= upipe
->u
.iso
.stds
;
1394 int i
, n
, nframes
, len
;
1396 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii
));
1398 nframes
= xfer
->nframes
;
1400 n
= UXFER(xfer
)->curframe
;
1401 for (i
= 0; i
< nframes
; i
++) {
1404 if (uhcidebug
> 5) {
1405 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i
));
1409 if (++n
>= UHCI_VFRAMELIST_COUNT
)
1411 status
= le32toh(std
->td
.td_status
);
1412 len
= UHCI_TD_GET_ACTLEN(status
);
1413 xfer
->frlengths
[i
] = len
;
1416 upipe
->u
.iso
.inuse
-= nframes
;
1417 xfer
->actlen
= actlen
;
1418 xfer
->status
= USBD_NORMAL_COMPLETION
;
1423 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1426 uhci_dump_tds(ii
->stdstart
);
1429 /* The transfer is done, compute actual length and status. */
1431 for (std
= ii
->stdstart
; std
!= NULL
; std
= std
->link
.std
) {
1432 nstatus
= le32toh(std
->td
.td_status
);
1433 if (nstatus
& UHCI_TD_ACTIVE
)
1437 if (UHCI_TD_GET_PID(le32toh(std
->td
.td_token
)) !=
1438 UHCI_TD_PID_SETUP
) {
1439 actlen
+= UHCI_TD_GET_ACTLEN(status
);
1442 * UHCI will report CRCTO in addition to a STALL or NAK
1443 * for a SETUP transaction. See section 3.2.2, "TD
1444 * CONTROL AND STATUS".
1446 if (status
& (UHCI_TD_STALLED
| UHCI_TD_NAK
))
1447 status
&= ~UHCI_TD_CRCTO
;
1451 /* If there are left over TDs we need to update the toggle. */
1453 upipe
->nexttoggle
= UHCI_TD_GET_DT(le32toh(std
->td
.td_token
));
1455 status
&= UHCI_TD_ERROR
;
1456 DPRINTFN(10, ("uhci_idone: actlen=%d, status=0x%x\n",
1458 xfer
->actlen
= actlen
;
1463 bitmask_snprintf((u_int32_t
)status
,
1464 "\20\22BITSTUFF\23CRCTO\24NAK\25"
1465 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
1466 sbuf
, sizeof(sbuf
));
1468 DPRINTFN((status
== UHCI_TD_STALLED
)*10,
1469 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1471 xfer
->pipe
->device
->address
,
1472 xfer
->pipe
->endpoint
->edesc
->bEndpointAddress
,
1475 if (status
== UHCI_TD_STALLED
)
1476 xfer
->status
= USBD_STALLED
;
1478 xfer
->status
= USBD_IOERROR
; /* more info XXX */
1480 xfer
->status
= USBD_NORMAL_COMPLETION
;
1484 usb_transfer_complete(xfer
);
1485 DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii
));
1489 * Called when a request does not complete.
1492 uhci_timeout(void *addr
)
1494 uhci_intr_info_t
*ii
= addr
;
1495 struct uhci_xfer
*uxfer
= UXFER(ii
->xfer
);
1496 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)uxfer
->xfer
.pipe
;
1497 uhci_softc_t
*sc
= (uhci_softc_t
*)upipe
->pipe
.device
->bus
;
1499 DPRINTF(("uhci_timeout: uxfer=%p\n", uxfer
));
1502 uhci_abort_xfer(&uxfer
->xfer
, USBD_TIMEOUT
);
1506 /* Execute the abort in a process context. */
1507 usb_add_task(uxfer
->xfer
.pipe
->device
, &uxfer
->abort_task
,
1512 uhci_timeout_task(void *addr
)
1514 usbd_xfer_handle xfer
= addr
;
1516 DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer
));
1519 uhci_abort_xfer(xfer
, USBD_TIMEOUT
);
1524 * Wait here until controller claims to have an interrupt.
1525 * Then call uhci_intr and return. Use timeout to avoid waiting
1527 * Only used during boot when interrupts are not enabled yet.
1530 uhci_waitintr(uhci_softc_t
*sc
, usbd_xfer_handle xfer
)
1532 int timo
= xfer
->timeout
;
1533 uhci_intr_info_t
*ii
;
1535 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo
));
1537 xfer
->status
= USBD_IN_PROGRESS
;
1538 for (; timo
>= 0; timo
--) {
1539 usb_delay_ms(&sc
->sc_bus
, 1);
1540 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc
, UHCI_STS
)));
1541 if (UREAD2(sc
, UHCI_STS
) & UHCI_STS_ALLINTRS
)
1543 if (xfer
->status
!= USBD_IN_PROGRESS
)
1548 DPRINTF(("uhci_waitintr: timeout\n"));
1549 for (ii
= LIST_FIRST(&sc
->sc_intrhead
);
1550 ii
!= NULL
&& ii
->xfer
!= xfer
;
1551 ii
= LIST_NEXT(ii
, list
))
1555 panic("uhci_waitintr: lost intr_info");
1561 uhci_poll(struct usbd_bus
*bus
)
1563 uhci_softc_t
*sc
= (uhci_softc_t
*)bus
;
1565 if (UREAD2(sc
, UHCI_STS
) & UHCI_STS_ALLINTRS
)
1570 uhci_reset(uhci_softc_t
*sc
)
1574 UHCICMD(sc
, UHCI_CMD_HCRESET
);
1575 /* The reset bit goes low when the controller is done. */
1576 for (n
= 0; n
< UHCI_RESET_TIMEOUT
&&
1577 (UREAD2(sc
, UHCI_CMD
) & UHCI_CMD_HCRESET
); n
++)
1578 usb_delay_ms(&sc
->sc_bus
, 1);
1579 if (n
>= UHCI_RESET_TIMEOUT
)
1580 kprintf("%s: controller did not reset\n",
1581 USBDEVNAME(sc
->sc_bus
.bdev
));
1585 uhci_run(uhci_softc_t
*sc
, int run
)
1592 DPRINTF(("uhci_run: setting run=%d\n", run
));
1593 cmd
= UREAD2(sc
, UHCI_CMD
);
1597 cmd
&= ~UHCI_CMD_RS
;
1599 for(n
= 0; n
< 10; n
++) {
1600 running
= !(UREAD2(sc
, UHCI_STS
) & UHCI_STS_HCH
);
1601 /* return when we've entered the state we want */
1602 if (run
== running
) {
1604 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1605 UREAD2(sc
, UHCI_CMD
), UREAD2(sc
, UHCI_STS
)));
1606 return (USBD_NORMAL_COMPLETION
);
1608 usb_delay_ms(&sc
->sc_bus
, 1);
1611 kprintf("%s: cannot %s\n", USBDEVNAME(sc
->sc_bus
.bdev
),
1612 run
? "start" : "stop");
1613 return (USBD_IOERROR
);
1617 * Memory management routines.
1618 * uhci_alloc_std allocates TDs
1619 * uhci_alloc_sqh allocates QHs
1620 * These two routines do their own free list management,
1621 * partly for speed, partly because allocating DMAable memory
1622 * has page size granularaity so much memory would be wasted if
1623 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1627 uhci_alloc_std(uhci_softc_t
*sc
)
1629 uhci_soft_td_t
*std
;
1634 if (sc
->sc_freetds
== NULL
) {
1635 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1636 err
= usb_allocmem(&sc
->sc_bus
, UHCI_STD_SIZE
* UHCI_STD_CHUNK
,
1637 UHCI_TD_ALIGN
, &dma
);
1640 for(i
= 0; i
< UHCI_STD_CHUNK
; i
++) {
1641 offs
= i
* UHCI_STD_SIZE
;
1642 std
= KERNADDR(&dma
, offs
);
1643 std
->physaddr
= DMAADDR(&dma
, offs
);
1644 std
->link
.std
= sc
->sc_freetds
;
1645 sc
->sc_freetds
= std
;
1648 std
= sc
->sc_freetds
;
1649 sc
->sc_freetds
= std
->link
.std
;
1650 memset(&std
->td
, 0, sizeof(uhci_td_t
));
1655 uhci_free_std(uhci_softc_t
*sc
, uhci_soft_td_t
*std
)
1658 #define TD_IS_FREE 0x12345678
1659 if (le32toh(std
->td
.td_token
) == TD_IS_FREE
) {
1660 kprintf("uhci_free_std: freeing free TD %p\n", std
);
1663 std
->td
.td_token
= htole32(TD_IS_FREE
);
1665 std
->link
.std
= sc
->sc_freetds
;
1666 sc
->sc_freetds
= std
;
1670 uhci_alloc_sqh(uhci_softc_t
*sc
)
1672 uhci_soft_qh_t
*sqh
;
1677 if (sc
->sc_freeqhs
== NULL
) {
1678 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1679 err
= usb_allocmem(&sc
->sc_bus
, UHCI_SQH_SIZE
* UHCI_SQH_CHUNK
,
1680 UHCI_QH_ALIGN
, &dma
);
1683 for(i
= 0; i
< UHCI_SQH_CHUNK
; i
++) {
1684 offs
= i
* UHCI_SQH_SIZE
;
1685 sqh
= KERNADDR(&dma
, offs
);
1686 sqh
->physaddr
= DMAADDR(&dma
, offs
);
1687 sqh
->hlink
= sc
->sc_freeqhs
;
1688 sc
->sc_freeqhs
= sqh
;
1691 sqh
= sc
->sc_freeqhs
;
1692 sc
->sc_freeqhs
= sqh
->hlink
;
1693 memset(&sqh
->qh
, 0, sizeof(uhci_qh_t
));
1698 uhci_free_sqh(uhci_softc_t
*sc
, uhci_soft_qh_t
*sqh
)
1700 sqh
->hlink
= sc
->sc_freeqhs
;
1701 sc
->sc_freeqhs
= sqh
;
1705 uhci_free_std_chain(uhci_softc_t
*sc
, uhci_soft_td_t
*std
,
1706 uhci_soft_td_t
*stdend
)
1710 for (; std
!= stdend
; std
= p
) {
1712 uhci_free_std(sc
, std
);
1717 uhci_alloc_std_chain(struct uhci_pipe
*upipe
, uhci_softc_t
*sc
, int len
,
1718 int rd
, u_int16_t flags
, usb_dma_t
*dma
,
1719 uhci_soft_td_t
**sp
, uhci_soft_td_t
**ep
)
1721 uhci_soft_td_t
*p
, *lastp
;
1722 uhci_physaddr_t lastlink
;
1723 int i
, ntd
, l
, tog
, maxp
;
1725 int addr
= upipe
->pipe
.device
->address
;
1726 int endpt
= upipe
->pipe
.endpoint
->edesc
->bEndpointAddress
;
1728 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d "
1729 "flags=0x%x\n", addr
, UE_GET_ADDR(endpt
), len
,
1730 upipe
->pipe
.device
->speed
, flags
));
1731 maxp
= UGETW(upipe
->pipe
.endpoint
->edesc
->wMaxPacketSize
);
1733 kprintf("uhci_alloc_std_chain: maxp=0\n");
1734 return (USBD_INVAL
);
1736 ntd
= (len
+ maxp
- 1) / maxp
;
1737 if ((flags
& USBD_FORCE_SHORT_XFER
) && len
% maxp
== 0)
1739 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp
, ntd
));
1742 DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
1743 return (USBD_NORMAL_COMPLETION
);
1745 tog
= upipe
->nexttoggle
;
1748 upipe
->nexttoggle
= tog
^ 1;
1750 lastlink
= UHCI_PTR_T
;
1752 status
= UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE
);
1753 if (upipe
->pipe
.device
->speed
== USB_SPEED_LOW
)
1754 status
|= UHCI_TD_LS
;
1755 if (flags
& USBD_SHORT_XFER_OK
)
1756 status
|= UHCI_TD_SPD
;
1757 for (i
= ntd
; i
>= 0; i
--) {
1758 p
= uhci_alloc_std(sc
);
1760 uhci_free_std_chain(sc
, lastp
, NULL
);
1761 return (USBD_NOMEM
);
1763 p
->link
.std
= lastp
;
1764 p
->td
.td_link
= htole32(lastlink
| UHCI_PTR_VF
| UHCI_PTR_TD
);
1766 lastlink
= p
->physaddr
;
1767 p
->td
.td_status
= htole32(status
);
1771 if (l
== 0 && !(flags
& USBD_FORCE_SHORT_XFER
))
1777 htole32(rd
? UHCI_TD_IN (l
, endpt
, addr
, tog
) :
1778 UHCI_TD_OUT(l
, endpt
, addr
, tog
));
1779 p
->td
.td_buffer
= htole32(DMAADDR(dma
, i
* maxp
));
1783 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1784 upipe
->nexttoggle
));
1785 return (USBD_NORMAL_COMPLETION
);
1789 uhci_device_clear_toggle(usbd_pipe_handle pipe
)
1791 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)pipe
;
1792 upipe
->nexttoggle
= 0;
1796 uhci_noop(usbd_pipe_handle pipe
)
1801 uhci_device_bulk_transfer(usbd_xfer_handle xfer
)
1805 /* Insert last in queue. */
1806 err
= usb_insert_transfer(xfer
);
1811 * Pipe isn't running (otherwise err would be USBD_INPROG),
1812 * so start it first.
1814 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer
->pipe
->queue
)));
1818 uhci_device_bulk_start(usbd_xfer_handle xfer
)
1820 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)xfer
->pipe
;
1821 usbd_device_handle dev
= upipe
->pipe
.device
;
1822 uhci_softc_t
*sc
= (uhci_softc_t
*)dev
->bus
;
1823 uhci_intr_info_t
*ii
= &UXFER(xfer
)->iinfo
;
1824 uhci_soft_td_t
*data
, *dataend
;
1825 uhci_soft_qh_t
*sqh
;
1827 int len
, isread
, endpt
;
1829 DPRINTFN(3, ("uhci_device_bulk_start: xfer=%p len=%d flags=%d ii=%p\n",
1830 xfer
, xfer
->length
, xfer
->flags
, ii
));
1833 return (USBD_IOERROR
);
1836 if (xfer
->rqflags
& URQ_REQUEST
)
1837 panic("uhci_device_bulk_transfer: a request");
1841 endpt
= upipe
->pipe
.endpoint
->edesc
->bEndpointAddress
;
1842 isread
= UE_GET_DIR(endpt
) == UE_DIR_IN
;
1843 sqh
= upipe
->u
.bulk
.sqh
;
1845 upipe
->u
.bulk
.isread
= isread
;
1846 upipe
->u
.bulk
.length
= len
;
1848 err
= uhci_alloc_std_chain(upipe
, sc
, len
, isread
, xfer
->flags
,
1849 &xfer
->dmabuf
, &data
, &dataend
);
1852 dataend
->td
.td_status
|= htole32(UHCI_TD_IOC
);
1855 if (uhcidebug
> 8) {
1856 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1857 uhci_dump_tds(data
);
1861 /* Set up interrupt info. */
1863 ii
->stdstart
= data
;
1864 ii
->stdend
= dataend
;
1867 kprintf("uhci_device_bulk_transfer: not done, ii=%p\n", ii
);
1873 sqh
->qh
.qh_elink
= htole32(data
->physaddr
| UHCI_PTR_TD
);
1876 uhci_add_bulk(sc
, sqh
);
1877 uhci_add_intr_info(sc
, ii
);
1879 if (xfer
->timeout
&& !sc
->sc_bus
.use_polling
) {
1880 usb_callout(xfer
->timeout_handle
, MS_TO_TICKS(xfer
->timeout
),
1883 xfer
->status
= USBD_IN_PROGRESS
;
1887 if (uhcidebug
> 10) {
1888 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1889 uhci_dump_tds(data
);
1893 if (sc
->sc_bus
.use_polling
)
1894 uhci_waitintr(sc
, xfer
);
1896 return (USBD_IN_PROGRESS
);
1899 /* Abort a device bulk request. */
1901 uhci_device_bulk_abort(usbd_xfer_handle xfer
)
1903 DPRINTF(("uhci_device_bulk_abort:\n"));
1904 uhci_abort_xfer(xfer
, USBD_CANCELLED
);
1908 * Abort a device request.
1909 * If this routine is called from a critical section. It guarantees that
1910 * the request will be removed from the hardware scheduling and that
1911 * the callback for it will be called with USBD_CANCELLED status.
1912 * It's impossible to guarantee that the requested transfer will not
1913 * have happened since the hardware runs concurrently.
1914 * If the transaction has already happened we rely on the ordinary
1915 * interrupt processing to process it.
1918 uhci_abort_xfer(usbd_xfer_handle xfer
, usbd_status status
)
1920 struct uhci_xfer
*uxfer
= UXFER(xfer
);
1921 uhci_intr_info_t
*ii
= &uxfer
->iinfo
;
1922 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)xfer
->pipe
;
1923 uhci_softc_t
*sc
= (uhci_softc_t
*)upipe
->pipe
.device
->bus
;
1924 uhci_soft_td_t
*std
;
1926 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer
, status
));
1929 /* If we're dying, just do the software part. */
1931 xfer
->status
= status
; /* make software ignore it */
1932 usb_uncallout(xfer
->timeout_handle
, uhci_timeout
, xfer
);
1933 usb_rem_task(xfer
->pipe
->device
, &UXFER(xfer
)->abort_task
);
1934 usb_transfer_complete(xfer
);
1939 if (xfer
->device
->bus
->intr_context
/* || !curproc REMOVED DFly */)
1940 panic("uhci_abort_xfer: not in process context");
1943 * If an abort is already in progress then just wait for it to
1944 * complete and return.
1946 if (uxfer
->uhci_xfer_flags
& UHCI_XFER_ABORTING
) {
1947 DPRINTFN(2, ("uhci_abort_xfer: already aborting\n"));
1948 /* No need to wait if we're aborting from a timeout. */
1949 if (status
== USBD_TIMEOUT
)
1951 /* Override the status which might be USBD_TIMEOUT. */
1952 xfer
->status
= status
;
1953 DPRINTFN(2, ("uhci_abort_xfer: waiting for abort to finish\n"));
1954 uxfer
->uhci_xfer_flags
|= UHCI_XFER_ABORTWAIT
;
1955 while (uxfer
->uhci_xfer_flags
& UHCI_XFER_ABORTING
)
1956 tsleep(&uxfer
->uhci_xfer_flags
, 0, "uhciaw", 0);
1961 * Step 1: Make interrupt routine and hardware ignore xfer.
1964 uxfer
->uhci_xfer_flags
|= UHCI_XFER_ABORTING
;
1965 xfer
->status
= status
; /* make software ignore it */
1966 usb_uncallout(xfer
->timeout_handle
, uhci_timeout
, ii
);
1967 usb_rem_task(xfer
->pipe
->device
, &UXFER(xfer
)->abort_task
);
1968 DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii
));
1969 for (std
= ii
->stdstart
; std
!= NULL
; std
= std
->link
.std
)
1970 std
->td
.td_status
&= htole32(~(UHCI_TD_ACTIVE
| UHCI_TD_IOC
));
1974 * Step 2: Wait until we know hardware has finished any possible
1975 * use of the xfer. Also make sure the soft interrupt routine
1978 usb_delay_ms(upipe
->pipe
.device
->bus
, 2); /* Hardware finishes in 1ms */
1980 #ifdef USB_USE_SOFTINTR
1981 sc
->sc_softwake
= 1;
1982 #endif /* USB_USE_SOFTINTR */
1983 usb_schedsoftintr(&sc
->sc_bus
);
1984 #ifdef USB_USE_SOFTINTR
1985 DPRINTFN(1,("uhci_abort_xfer: tsleep\n"));
1986 tsleep(&sc
->sc_softwake
, 0, "uhciab", 0);
1987 #endif /* USB_USE_SOFTINTR */
1990 * Step 3: Execute callback.
1992 DPRINTFN(1,("uhci_abort_xfer: callback\n"));
1996 /* Do the wakeup first to avoid touching the xfer after the callback. */
1997 uxfer
->uhci_xfer_flags
&= ~UHCI_XFER_ABORTING
;
1998 if (uxfer
->uhci_xfer_flags
& UHCI_XFER_ABORTWAIT
) {
1999 uxfer
->uhci_xfer_flags
&= ~UHCI_XFER_ABORTWAIT
;
2000 wakeup(&uxfer
->uhci_xfer_flags
);
2002 usb_transfer_complete(xfer
);
2006 /* Close a device bulk pipe. */
2008 uhci_device_bulk_close(usbd_pipe_handle pipe
)
2010 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)pipe
;
2011 usbd_device_handle dev
= upipe
->pipe
.device
;
2012 uhci_softc_t
*sc
= (uhci_softc_t
*)dev
->bus
;
2014 uhci_free_sqh(sc
, upipe
->u
.bulk
.sqh
);
2015 pipe
->endpoint
->savedtoggle
= upipe
->nexttoggle
;
2019 uhci_device_ctrl_transfer(usbd_xfer_handle xfer
)
2023 /* Insert last in queue. */
2024 err
= usb_insert_transfer(xfer
);
2029 * Pipe isn't running (otherwise err would be USBD_INPROG),
2030 * so start it first.
2032 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer
->pipe
->queue
)));
2036 uhci_device_ctrl_start(usbd_xfer_handle xfer
)
2038 uhci_softc_t
*sc
= (uhci_softc_t
*)xfer
->pipe
->device
->bus
;
2042 return (USBD_IOERROR
);
2045 if (!(xfer
->rqflags
& URQ_REQUEST
))
2046 panic("uhci_device_ctrl_transfer: not a request");
2049 err
= uhci_device_request(xfer
);
2053 if (sc
->sc_bus
.use_polling
)
2054 uhci_waitintr(sc
, xfer
);
2055 return (USBD_IN_PROGRESS
);
2059 uhci_device_intr_transfer(usbd_xfer_handle xfer
)
2063 /* Insert last in queue. */
2064 err
= usb_insert_transfer(xfer
);
2069 * Pipe isn't running (otherwise err would be USBD_INPROG),
2070 * so start it first.
2072 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer
->pipe
->queue
)));
2076 uhci_device_intr_start(usbd_xfer_handle xfer
)
2078 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)xfer
->pipe
;
2079 usbd_device_handle dev
= upipe
->pipe
.device
;
2080 uhci_softc_t
*sc
= (uhci_softc_t
*)dev
->bus
;
2081 uhci_intr_info_t
*ii
= &UXFER(xfer
)->iinfo
;
2082 uhci_soft_td_t
*data
, *dataend
;
2083 uhci_soft_qh_t
*sqh
;
2089 return (USBD_IOERROR
);
2091 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
2092 xfer
, xfer
->length
, xfer
->flags
));
2095 if (xfer
->rqflags
& URQ_REQUEST
)
2096 panic("uhci_device_intr_transfer: a request");
2099 endpt
= upipe
->pipe
.endpoint
->edesc
->bEndpointAddress
;
2100 isread
= UE_GET_DIR(endpt
) == UE_DIR_IN
;
2101 sqh
= upipe
->u
.bulk
.sqh
;
2103 upipe
->u
.intr
.isread
= isread
;
2105 err
= uhci_alloc_std_chain(upipe
, sc
, xfer
->length
, isread
,
2106 xfer
->flags
, &xfer
->dmabuf
, &data
,
2110 dataend
->td
.td_status
|= htole32(UHCI_TD_IOC
);
2113 if (uhcidebug
> 10) {
2114 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
2115 uhci_dump_tds(data
);
2116 uhci_dump_qh(upipe
->u
.intr
.qhs
[0]);
2121 /* Set up interrupt info. */
2123 ii
->stdstart
= data
;
2124 ii
->stdend
= dataend
;
2127 kprintf("uhci_device_intr_transfer: not done, ii=%p\n", ii
);
2132 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
2133 upipe
->u
.intr
.qhs
[0]));
2134 for (i
= 0; i
< upipe
->u
.intr
.npoll
; i
++) {
2135 sqh
= upipe
->u
.intr
.qhs
[i
];
2137 sqh
->qh
.qh_elink
= htole32(data
->physaddr
| UHCI_PTR_TD
);
2139 uhci_add_intr_info(sc
, ii
);
2140 xfer
->status
= USBD_IN_PROGRESS
;
2144 if (uhcidebug
> 10) {
2145 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
2146 uhci_dump_tds(data
);
2147 uhci_dump_qh(upipe
->u
.intr
.qhs
[0]);
2151 return (USBD_IN_PROGRESS
);
2154 /* Abort a device control request. */
2156 uhci_device_ctrl_abort(usbd_xfer_handle xfer
)
2158 DPRINTF(("uhci_device_ctrl_abort:\n"));
2159 uhci_abort_xfer(xfer
, USBD_CANCELLED
);
2162 /* Close a device control pipe. */
2164 uhci_device_ctrl_close(usbd_pipe_handle pipe
)
2168 /* Abort a device interrupt request. */
2170 uhci_device_intr_abort(usbd_xfer_handle xfer
)
2172 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer
));
2173 if (xfer
->pipe
->intrxfer
== xfer
) {
2174 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
2175 xfer
->pipe
->intrxfer
= NULL
;
2177 uhci_abort_xfer(xfer
, USBD_CANCELLED
);
2180 /* Close a device interrupt pipe. */
2182 uhci_device_intr_close(usbd_pipe_handle pipe
)
2184 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)pipe
;
2185 uhci_softc_t
*sc
= (uhci_softc_t
*)pipe
->device
->bus
;
2188 /* Unlink descriptors from controller data structures. */
2189 npoll
= upipe
->u
.intr
.npoll
;
2191 for (i
= 0; i
< npoll
; i
++)
2192 uhci_remove_intr(sc
, upipe
->u
.intr
.qhs
[i
]);
2196 * We now have to wait for any activity on the physical
2197 * descriptors to stop.
2199 usb_delay_ms(&sc
->sc_bus
, 2);
2201 for(i
= 0; i
< npoll
; i
++)
2202 uhci_free_sqh(sc
, upipe
->u
.intr
.qhs
[i
]);
2203 kfree(upipe
->u
.intr
.qhs
, M_USBHC
);
2205 /* XXX free other resources */
2209 uhci_device_request(usbd_xfer_handle xfer
)
2211 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)xfer
->pipe
;
2212 usb_device_request_t
*req
= &xfer
->request
;
2213 usbd_device_handle dev
= upipe
->pipe
.device
;
2214 uhci_softc_t
*sc
= (uhci_softc_t
*)dev
->bus
;
2215 int addr
= dev
->address
;
2216 int endpt
= upipe
->pipe
.endpoint
->edesc
->bEndpointAddress
;
2217 uhci_intr_info_t
*ii
= &UXFER(xfer
)->iinfo
;
2218 uhci_soft_td_t
*setup
, *data
, *stat
, *next
, *dataend
;
2219 uhci_soft_qh_t
*sqh
;
2225 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
2226 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2227 req
->bmRequestType
, req
->bRequest
, UGETW(req
->wValue
),
2228 UGETW(req
->wIndex
), UGETW(req
->wLength
),
2231 ls
= dev
->speed
== USB_SPEED_LOW
? UHCI_TD_LS
: 0;
2232 isread
= req
->bmRequestType
& UT_READ
;
2233 len
= UGETW(req
->wLength
);
2235 setup
= upipe
->u
.ctl
.setup
;
2236 stat
= upipe
->u
.ctl
.stat
;
2237 sqh
= upipe
->u
.ctl
.sqh
;
2239 /* Set up data transaction */
2241 upipe
->nexttoggle
= 1;
2242 err
= uhci_alloc_std_chain(upipe
, sc
, len
, isread
, xfer
->flags
,
2243 &xfer
->dmabuf
, &data
, &dataend
);
2247 dataend
->link
.std
= stat
;
2248 dataend
->td
.td_link
= htole32(stat
->physaddr
| UHCI_PTR_VF
| UHCI_PTR_TD
);
2252 upipe
->u
.ctl
.length
= len
;
2254 memcpy(KERNADDR(&upipe
->u
.ctl
.reqdma
, 0), req
, sizeof *req
);
2256 setup
->link
.std
= next
;
2257 setup
->td
.td_link
= htole32(next
->physaddr
| UHCI_PTR_VF
| UHCI_PTR_TD
);
2258 setup
->td
.td_status
= htole32(UHCI_TD_SET_ERRCNT(3) | ls
|
2260 setup
->td
.td_token
= htole32(UHCI_TD_SETUP(sizeof *req
, endpt
, addr
));
2261 setup
->td
.td_buffer
= htole32(DMAADDR(&upipe
->u
.ctl
.reqdma
, 0));
2262 stat
->link
.std
= NULL
;
2263 stat
->td
.td_link
= htole32(UHCI_PTR_T
);
2264 stat
->td
.td_status
= htole32(UHCI_TD_SET_ERRCNT(3) | ls
|
2265 UHCI_TD_ACTIVE
| UHCI_TD_IOC
);
2268 htole32(isread
? UHCI_TD_OUT(0, endpt
, addr
, 1) :
2269 UHCI_TD_IN (0, endpt
, addr
, 1));
2270 stat
->td
.td_buffer
= htole32(0);
2273 if (uhcidebug
> 10) {
2274 DPRINTF(("uhci_device_request: before transfer\n"));
2275 uhci_dump_tds(setup
);
2279 /* Set up interrupt info. */
2281 ii
->stdstart
= setup
;
2285 kprintf("uhci_device_request: not done, ii=%p\n", ii
);
2291 sqh
->qh
.qh_elink
= htole32(setup
->physaddr
| UHCI_PTR_TD
);
2294 if (dev
->speed
== USB_SPEED_LOW
)
2295 uhci_add_ls_ctrl(sc
, sqh
);
2297 uhci_add_hs_ctrl(sc
, sqh
);
2298 uhci_add_intr_info(sc
, ii
);
2300 if (uhcidebug
> 12) {
2301 uhci_soft_td_t
*std
;
2302 uhci_soft_qh_t
*xqh
;
2303 uhci_soft_qh_t
*sxqh
;
2305 uhci_physaddr_t link
;
2306 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
2307 for (std
= sc
->sc_vframes
[0].htd
, link
= 0;
2308 (link
& UHCI_PTR_QH
) == 0;
2309 std
= std
->link
.std
) {
2310 link
= le32toh(std
->td
.td_link
);
2313 sxqh
= (uhci_soft_qh_t
*)std
;
2317 xqh
= (maxqh
++ == 5 || xqh
->hlink
== sxqh
||
2318 xqh
->hlink
== xqh
? NULL
: xqh
->hlink
)) {
2321 DPRINTF(("Enqueued QH:\n"));
2323 uhci_dump_tds(sqh
->elink
);
2326 if (xfer
->timeout
&& !sc
->sc_bus
.use_polling
) {
2327 usb_callout(xfer
->timeout_handle
, MS_TO_TICKS(xfer
->timeout
),
2330 xfer
->status
= USBD_IN_PROGRESS
;
2333 return (USBD_NORMAL_COMPLETION
);
2337 uhci_device_isoc_transfer(usbd_xfer_handle xfer
)
2341 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer
));
2343 /* Put it on our queue, */
2344 err
= usb_insert_transfer(xfer
);
2346 /* bail out on error, */
2347 if (err
&& err
!= USBD_IN_PROGRESS
)
2350 /* XXX should check inuse here */
2352 /* insert into schedule, */
2353 uhci_device_isoc_enter(xfer
);
2355 /* and start if the pipe wasn't running */
2357 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer
->pipe
->queue
));
2363 uhci_device_isoc_enter(usbd_xfer_handle xfer
)
2365 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)xfer
->pipe
;
2366 usbd_device_handle dev
= upipe
->pipe
.device
;
2367 uhci_softc_t
*sc
= (uhci_softc_t
*)dev
->bus
;
2368 struct iso
*iso
= &upipe
->u
.iso
;
2369 uhci_soft_td_t
*std
;
2370 u_int32_t buf
, len
, status
;
2371 int i
, next
, nframes
;
2373 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
2375 iso
->inuse
, iso
->next
, xfer
, xfer
->nframes
));
2380 if (xfer
->status
== USBD_IN_PROGRESS
) {
2381 /* This request has already been entered into the frame list */
2382 kprintf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer
);
2387 if (iso
->inuse
>= UHCI_VFRAMELIST_COUNT
)
2388 kprintf("uhci_device_isoc_enter: overflow!\n");
2393 /* Not in use yet, schedule it a few frames ahead. */
2394 next
= (UREAD2(sc
, UHCI_FRNUM
) + 3) % UHCI_VFRAMELIST_COUNT
;
2395 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next
));
2398 xfer
->status
= USBD_IN_PROGRESS
;
2399 UXFER(xfer
)->curframe
= next
;
2401 buf
= DMAADDR(&xfer
->dmabuf
, 0);
2402 status
= UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2405 nframes
= xfer
->nframes
;
2407 for (i
= 0; i
< nframes
; i
++) {
2408 std
= iso
->stds
[next
];
2409 if (++next
>= UHCI_VFRAMELIST_COUNT
)
2411 len
= xfer
->frlengths
[i
];
2412 std
->td
.td_buffer
= htole32(buf
);
2413 if (i
== nframes
- 1)
2414 status
|= UHCI_TD_IOC
;
2415 std
->td
.td_status
= htole32(status
);
2416 std
->td
.td_token
&= htole32(~UHCI_TD_MAXLEN_MASK
);
2417 std
->td
.td_token
|= htole32(UHCI_TD_SET_MAXLEN(len
));
2419 if (uhcidebug
> 5) {
2420 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i
));
2427 iso
->inuse
+= xfer
->nframes
;
2433 uhci_device_isoc_start(usbd_xfer_handle xfer
)
2435 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)xfer
->pipe
;
2436 uhci_softc_t
*sc
= (uhci_softc_t
*)upipe
->pipe
.device
->bus
;
2437 uhci_intr_info_t
*ii
= &UXFER(xfer
)->iinfo
;
2438 uhci_soft_td_t
*end
;
2441 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer
));
2444 return (USBD_IOERROR
);
2447 if (xfer
->status
!= USBD_IN_PROGRESS
)
2448 kprintf("uhci_device_isoc_start: not in progress %p\n", xfer
);
2451 /* Find the last TD */
2452 i
= UXFER(xfer
)->curframe
+ xfer
->nframes
;
2453 if (i
>= UHCI_VFRAMELIST_COUNT
)
2454 i
-= UHCI_VFRAMELIST_COUNT
;
2455 end
= upipe
->u
.iso
.stds
[i
];
2459 kprintf("uhci_device_isoc_start: end == NULL\n");
2460 return (USBD_INVAL
);
2466 /* Set up interrupt info. */
2472 kprintf("uhci_device_isoc_start: not done, ii=%p\n", ii
);
2475 uhci_add_intr_info(sc
, ii
);
2479 return (USBD_IN_PROGRESS
);
2483 uhci_device_isoc_abort(usbd_xfer_handle xfer
)
2485 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)xfer
->pipe
;
2486 uhci_soft_td_t
**stds
= upipe
->u
.iso
.stds
;
2487 uhci_soft_td_t
*std
;
2488 int i
, n
, nframes
, maxlen
, len
;
2492 /* Transfer is already done. */
2493 if (xfer
->status
!= USBD_NOT_STARTED
&&
2494 xfer
->status
!= USBD_IN_PROGRESS
) {
2499 /* Give xfer the requested abort code. */
2500 xfer
->status
= USBD_CANCELLED
;
2502 /* make hardware ignore it, */
2503 nframes
= xfer
->nframes
;
2504 n
= UXFER(xfer
)->curframe
;
2506 for (i
= 0; i
< nframes
; i
++) {
2508 std
->td
.td_status
&= htole32(~(UHCI_TD_ACTIVE
| UHCI_TD_IOC
));
2509 len
= UHCI_TD_GET_MAXLEN(le32toh(std
->td
.td_token
));
2512 if (++n
>= UHCI_VFRAMELIST_COUNT
)
2516 /* and wait until we are sure the hardware has finished. */
2520 UXFER(xfer
)->iinfo
.isdone
= 1;
2522 /* Run callback and remove from interrupt list. */
2523 usb_transfer_complete(xfer
);
2529 uhci_device_isoc_close(usbd_pipe_handle pipe
)
2531 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)pipe
;
2532 usbd_device_handle dev
= upipe
->pipe
.device
;
2533 uhci_softc_t
*sc
= (uhci_softc_t
*)dev
->bus
;
2534 uhci_soft_td_t
*std
, *vstd
;
2539 * Make sure all TDs are marked as inactive.
2540 * Wait for completion.
2544 iso
= &upipe
->u
.iso
;
2546 for (i
= 0; i
< UHCI_VFRAMELIST_COUNT
; i
++)
2547 iso
->stds
[i
]->td
.td_status
&= htole32(~UHCI_TD_ACTIVE
);
2548 usb_delay_ms(&sc
->sc_bus
, 2); /* wait for completion */
2551 for (i
= 0; i
< UHCI_VFRAMELIST_COUNT
; i
++) {
2553 for (vstd
= sc
->sc_vframes
[i
].htd
;
2554 vstd
!= NULL
&& vstd
->link
.std
!= std
;
2555 vstd
= vstd
->link
.std
)
2559 kprintf("uhci_device_isoc_close: %p not found\n", std
);
2563 vstd
->link
= std
->link
;
2564 vstd
->td
.td_link
= std
->td
.td_link
;
2565 uhci_free_std(sc
, std
);
2569 kfree(iso
->stds
, M_USBHC
);
2573 uhci_setup_isoc(usbd_pipe_handle pipe
)
2575 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)pipe
;
2576 usbd_device_handle dev
= upipe
->pipe
.device
;
2577 uhci_softc_t
*sc
= (uhci_softc_t
*)dev
->bus
;
2578 int addr
= upipe
->pipe
.device
->address
;
2579 int endpt
= upipe
->pipe
.endpoint
->edesc
->bEndpointAddress
;
2580 int rd
= UE_GET_DIR(endpt
) == UE_DIR_IN
;
2581 uhci_soft_td_t
*std
, *vstd
;
2586 iso
= &upipe
->u
.iso
;
2587 iso
->stds
= kmalloc(UHCI_VFRAMELIST_COUNT
* sizeof (uhci_soft_td_t
*),
2588 M_USBHC
, M_INTWAIT
);
2590 token
= rd
? UHCI_TD_IN (0, endpt
, addr
, 0) :
2591 UHCI_TD_OUT(0, endpt
, addr
, 0);
2593 /* Allocate the TDs and mark as inactive; */
2594 for (i
= 0; i
< UHCI_VFRAMELIST_COUNT
; i
++) {
2595 std
= uhci_alloc_std(sc
);
2598 std
->td
.td_status
= htole32(UHCI_TD_IOS
); /* iso, inactive */
2599 std
->td
.td_token
= htole32(token
);
2603 /* Insert TDs into schedule. */
2605 for (i
= 0; i
< UHCI_VFRAMELIST_COUNT
; i
++) {
2607 vstd
= sc
->sc_vframes
[i
].htd
;
2608 std
->link
= vstd
->link
;
2609 std
->td
.td_link
= vstd
->td
.td_link
;
2610 vstd
->link
.std
= std
;
2611 vstd
->td
.td_link
= htole32(std
->physaddr
| UHCI_PTR_TD
);
2618 return (USBD_NORMAL_COMPLETION
);
2622 uhci_free_std(sc
, iso
->stds
[i
]);
2623 kfree(iso
->stds
, M_USBHC
);
2624 return (USBD_NOMEM
);
2628 uhci_device_isoc_done(usbd_xfer_handle xfer
)
2630 uhci_intr_info_t
*ii
= &UXFER(xfer
)->iinfo
;
2632 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer
->actlen
));
2634 if (ii
->xfer
!= xfer
)
2635 /* Not on interrupt list, ignore it. */
2638 if (!uhci_active_intr_info(ii
))
2642 if (xfer
->busy_free
!= XFER_BUSY
) {
2643 kprintf("uhci_device_isoc_done: xfer=%p not busy 0x%08x\n",
2644 xfer
, xfer
->busy_free
);
2648 if (ii
->stdend
== NULL
) {
2649 kprintf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer
);
2657 /* Turn off the interrupt since it is active even if the TD is not. */
2658 ii
->stdend
->td
.td_status
&= htole32(~UHCI_TD_IOC
);
2660 uhci_del_intr_info(ii
); /* remove from active list */
2663 if (ii
->stdend
== NULL
) {
2664 kprintf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer
);
2674 uhci_device_intr_done(usbd_xfer_handle xfer
)
2676 uhci_intr_info_t
*ii
= &UXFER(xfer
)->iinfo
;
2677 uhci_softc_t
*sc
= ii
->sc
;
2678 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)xfer
->pipe
;
2679 uhci_soft_qh_t
*sqh
;
2682 DPRINTFN(5, ("uhci_device_intr_done: length=%d\n", xfer
->actlen
));
2684 npoll
= upipe
->u
.intr
.npoll
;
2685 for(i
= 0; i
< npoll
; i
++) {
2686 sqh
= upipe
->u
.intr
.qhs
[i
];
2688 sqh
->qh
.qh_elink
= htole32(UHCI_PTR_T
);
2690 uhci_free_std_chain(sc
, ii
->stdstart
, NULL
);
2693 if (xfer
->pipe
->repeat
) {
2694 uhci_soft_td_t
*data
, *dataend
;
2696 DPRINTFN(5,("uhci_device_intr_done: requeueing\n"));
2698 /* This alloc cannot fail since we freed the chain above. */
2699 uhci_alloc_std_chain(upipe
, sc
, xfer
->length
,
2700 upipe
->u
.intr
.isread
, xfer
->flags
,
2701 &xfer
->dmabuf
, &data
, &dataend
);
2702 dataend
->td
.td_status
|= htole32(UHCI_TD_IOC
);
2705 if (uhcidebug
> 10) {
2706 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2707 uhci_dump_tds(data
);
2708 uhci_dump_qh(upipe
->u
.intr
.qhs
[0]);
2712 ii
->stdstart
= data
;
2713 ii
->stdend
= dataend
;
2716 kprintf("uhci_device_intr_done: not done, ii=%p\n", ii
);
2720 for (i
= 0; i
< npoll
; i
++) {
2721 sqh
= upipe
->u
.intr
.qhs
[i
];
2723 sqh
->qh
.qh_elink
= htole32(data
->physaddr
| UHCI_PTR_TD
);
2725 xfer
->status
= USBD_IN_PROGRESS
;
2726 /* The ii is already on the examined list, just leave it. */
2728 DPRINTFN(5,("uhci_device_intr_done: removing\n"));
2729 if (uhci_active_intr_info(ii
))
2730 uhci_del_intr_info(ii
);
2734 /* Deallocate request data structures */
2736 uhci_device_ctrl_done(usbd_xfer_handle xfer
)
2738 uhci_intr_info_t
*ii
= &UXFER(xfer
)->iinfo
;
2739 uhci_softc_t
*sc
= ii
->sc
;
2740 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)xfer
->pipe
;
2743 if (!(xfer
->rqflags
& URQ_REQUEST
))
2744 panic("uhci_device_ctrl_done: not a request");
2747 if (!uhci_active_intr_info(ii
))
2750 uhci_del_intr_info(ii
); /* remove from active list */
2752 if (upipe
->pipe
.device
->speed
== USB_SPEED_LOW
)
2753 uhci_remove_ls_ctrl(sc
, upipe
->u
.ctl
.sqh
);
2755 uhci_remove_hs_ctrl(sc
, upipe
->u
.ctl
.sqh
);
2757 if (upipe
->u
.ctl
.length
!= 0)
2758 uhci_free_std_chain(sc
, ii
->stdstart
->link
.std
, ii
->stdend
);
2760 DPRINTFN(5, ("uhci_device_ctrl_done: length=%d\n", xfer
->actlen
));
2763 /* Deallocate request data structures */
2765 uhci_device_bulk_done(usbd_xfer_handle xfer
)
2767 uhci_intr_info_t
*ii
= &UXFER(xfer
)->iinfo
;
2768 uhci_softc_t
*sc
= ii
->sc
;
2769 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)xfer
->pipe
;
2771 DPRINTFN(5,("uhci_device_bulk_done: xfer=%p ii=%p sc=%p upipe=%p\n",
2772 xfer
, ii
, sc
, upipe
));
2774 if (!uhci_active_intr_info(ii
))
2777 uhci_del_intr_info(ii
); /* remove from active list */
2779 uhci_remove_bulk(sc
, upipe
->u
.bulk
.sqh
);
2781 uhci_free_std_chain(sc
, ii
->stdstart
, NULL
);
2783 DPRINTFN(5, ("uhci_device_bulk_done: length=%d\n", xfer
->actlen
));
2786 /* Add interrupt QH, called with vflock. */
2788 uhci_add_intr(uhci_softc_t
*sc
, uhci_soft_qh_t
*sqh
)
2790 struct uhci_vframe
*vf
= &sc
->sc_vframes
[sqh
->pos
];
2791 uhci_soft_qh_t
*eqh
;
2793 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh
->pos
, sqh
));
2796 sqh
->hlink
= eqh
->hlink
;
2797 sqh
->qh
.qh_hlink
= eqh
->qh
.qh_hlink
;
2799 eqh
->qh
.qh_hlink
= htole32(sqh
->physaddr
| UHCI_PTR_QH
);
2804 /* Remove interrupt QH. */
2806 uhci_remove_intr(uhci_softc_t
*sc
, uhci_soft_qh_t
*sqh
)
2808 struct uhci_vframe
*vf
= &sc
->sc_vframes
[sqh
->pos
];
2809 uhci_soft_qh_t
*pqh
;
2811 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh
->pos
, sqh
));
2813 /* See comment in uhci_remove_ctrl() */
2814 if (!(sqh
->qh
.qh_elink
& htole32(UHCI_PTR_T
))) {
2815 sqh
->qh
.qh_elink
= htole32(UHCI_PTR_T
);
2816 delay(UHCI_QH_REMOVE_DELAY
);
2819 pqh
= uhci_find_prev_qh(vf
->hqh
, sqh
);
2820 pqh
->hlink
= sqh
->hlink
;
2821 pqh
->qh
.qh_hlink
= sqh
->qh
.qh_hlink
;
2822 delay(UHCI_QH_REMOVE_DELAY
);
2829 uhci_device_setintr(uhci_softc_t
*sc
, struct uhci_pipe
*upipe
, int ival
)
2831 uhci_soft_qh_t
*sqh
;
2833 u_int bestbw
, bw
, bestoffs
, offs
;
2835 DPRINTFN(2, ("uhci_device_setintr: pipe=%p\n", upipe
));
2837 kprintf("uhci_setintr: 0 interval\n");
2838 return (USBD_INVAL
);
2841 if (ival
> UHCI_VFRAMELIST_COUNT
)
2842 ival
= UHCI_VFRAMELIST_COUNT
;
2843 npoll
= (UHCI_VFRAMELIST_COUNT
+ ival
- 1) / ival
;
2844 DPRINTFN(2, ("uhci_device_setintr: ival=%d npoll=%d\n", ival
, npoll
));
2846 upipe
->u
.intr
.npoll
= npoll
;
2848 kmalloc(npoll
* sizeof(uhci_soft_qh_t
*), M_USBHC
, M_INTWAIT
);
2851 * Figure out which offset in the schedule that has most
2852 * bandwidth left over.
2854 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2855 for (bestoffs
= offs
= 0, bestbw
= ~0; offs
< ival
; offs
++) {
2856 for (bw
= i
= 0; i
< npoll
; i
++)
2857 bw
+= sc
->sc_vframes
[MOD(i
* ival
+ offs
)].bandwidth
;
2863 DPRINTFN(1, ("uhci_device_setintr: bw=%d offs=%d\n", bestbw
, bestoffs
));
2865 for(i
= 0; i
< npoll
; i
++) {
2866 upipe
->u
.intr
.qhs
[i
] = sqh
= uhci_alloc_sqh(sc
);
2868 sqh
->qh
.qh_elink
= htole32(UHCI_PTR_T
);
2869 sqh
->pos
= MOD(i
* ival
+ bestoffs
);
2874 /* Enter QHs into the controller data structures. */
2875 for(i
= 0; i
< npoll
; i
++)
2876 uhci_add_intr(sc
, upipe
->u
.intr
.qhs
[i
]);
2879 DPRINTFN(5, ("uhci_device_setintr: returns %p\n", upipe
));
2880 return (USBD_NORMAL_COMPLETION
);
2883 /* Open a new pipe. */
2885 uhci_open(usbd_pipe_handle pipe
)
2887 uhci_softc_t
*sc
= (uhci_softc_t
*)pipe
->device
->bus
;
2888 struct uhci_pipe
*upipe
= (struct uhci_pipe
*)pipe
;
2889 usb_endpoint_descriptor_t
*ed
= pipe
->endpoint
->edesc
;
2893 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2894 pipe
, pipe
->device
->address
,
2895 ed
->bEndpointAddress
, sc
->sc_addr
));
2897 upipe
->aborting
= 0;
2898 upipe
->nexttoggle
= pipe
->endpoint
->savedtoggle
;
2900 if (pipe
->device
->address
== sc
->sc_addr
) {
2901 switch (ed
->bEndpointAddress
) {
2902 case USB_CONTROL_ENDPOINT
:
2903 pipe
->methods
= &uhci_root_ctrl_methods
;
2905 case UE_DIR_IN
| UHCI_INTR_ENDPT
:
2906 pipe
->methods
= &uhci_root_intr_methods
;
2909 return (USBD_INVAL
);
2912 switch (ed
->bmAttributes
& UE_XFERTYPE
) {
2914 pipe
->methods
= &uhci_device_ctrl_methods
;
2915 upipe
->u
.ctl
.sqh
= uhci_alloc_sqh(sc
);
2916 if (upipe
->u
.ctl
.sqh
== NULL
)
2918 upipe
->u
.ctl
.setup
= uhci_alloc_std(sc
);
2919 if (upipe
->u
.ctl
.setup
== NULL
) {
2920 uhci_free_sqh(sc
, upipe
->u
.ctl
.sqh
);
2923 upipe
->u
.ctl
.stat
= uhci_alloc_std(sc
);
2924 if (upipe
->u
.ctl
.stat
== NULL
) {
2925 uhci_free_sqh(sc
, upipe
->u
.ctl
.sqh
);
2926 uhci_free_std(sc
, upipe
->u
.ctl
.setup
);
2929 err
= usb_allocmem(&sc
->sc_bus
,
2930 sizeof(usb_device_request_t
),
2931 0, &upipe
->u
.ctl
.reqdma
);
2933 uhci_free_sqh(sc
, upipe
->u
.ctl
.sqh
);
2934 uhci_free_std(sc
, upipe
->u
.ctl
.setup
);
2935 uhci_free_std(sc
, upipe
->u
.ctl
.stat
);
2940 pipe
->methods
= &uhci_device_intr_methods
;
2941 ival
= pipe
->interval
;
2942 if (ival
== USBD_DEFAULT_INTERVAL
)
2943 ival
= ed
->bInterval
;
2944 return (uhci_device_setintr(sc
, upipe
, ival
));
2945 case UE_ISOCHRONOUS
:
2946 pipe
->methods
= &uhci_device_isoc_methods
;
2947 return (uhci_setup_isoc(pipe
));
2949 pipe
->methods
= &uhci_device_bulk_methods
;
2950 upipe
->u
.bulk
.sqh
= uhci_alloc_sqh(sc
);
2951 if (upipe
->u
.bulk
.sqh
== NULL
)
2956 return (USBD_NORMAL_COMPLETION
);
2959 return (USBD_NOMEM
);
2963 * Data structures and routines to emulate the root hub.
2965 usb_device_descriptor_t uhci_devd
= {
2966 USB_DEVICE_DESCRIPTOR_SIZE
,
2967 UDESC_DEVICE
, /* type */
2968 {0x00, 0x01}, /* USB version */
2969 UDCLASS_HUB
, /* class */
2970 UDSUBCLASS_HUB
, /* subclass */
2971 UDPROTO_FSHUB
, /* protocol */
2972 64, /* max packet */
2973 {0},{0},{0x00,0x01}, /* device id */
2974 1,2,0, /* string indicies */
2975 1 /* # of configurations */
2978 usb_config_descriptor_t uhci_confd
= {
2979 USB_CONFIG_DESCRIPTOR_SIZE
,
2981 {USB_CONFIG_DESCRIPTOR_SIZE
+
2982 USB_INTERFACE_DESCRIPTOR_SIZE
+
2983 USB_ENDPOINT_DESCRIPTOR_SIZE
},
2991 usb_interface_descriptor_t uhci_ifcd
= {
2992 USB_INTERFACE_DESCRIPTOR_SIZE
,
3003 usb_endpoint_descriptor_t uhci_endpd
= {
3004 USB_ENDPOINT_DESCRIPTOR_SIZE
,
3006 UE_DIR_IN
| UHCI_INTR_ENDPT
,
3012 usb_hub_descriptor_t uhci_hubd_piix
= {
3013 USB_HUB_DESCRIPTOR_SIZE
,
3016 { UHD_PWR_NO_SWITCH
| UHD_OC_INDIVIDUAL
, 0 },
3017 50, /* power on to power good */
3019 { 0x00 }, /* both ports are removable */
3023 uhci_str(usb_string_descriptor_t
*p
, int l
, char *s
)
3029 p
->bLength
= 2 * strlen(s
) + 2;
3032 p
->bDescriptorType
= UDESC_STRING
;
3034 for (i
= 0; s
[i
] && l
> 1; i
++, l
-= 2)
3035 USETW2(p
->bString
[i
], 0, s
[i
]);
3040 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
3041 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
3042 * should not be used by the USB subsystem. As we cannot issue a
3043 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
3044 * will be enabled as part of the reset.
3046 * On the VT83C572, the port cannot be successfully enabled until the
3047 * outstanding "port enable change" and "connection status change"
3048 * events have been reset.
3051 uhci_portreset(uhci_softc_t
*sc
, int index
)
3056 port
= UHCI_PORTSC1
;
3057 else if (index
== 2)
3058 port
= UHCI_PORTSC2
;
3060 return (USBD_IOERROR
);
3062 x
= URWMASK(UREAD2(sc
, port
));
3063 UWRITE2(sc
, port
, x
| UHCI_PORTSC_PR
);
3065 usb_delay_ms(&sc
->sc_bus
, USB_PORT_ROOT_RESET_DELAY
);
3067 DPRINTFN(3,("uhci port %d reset, status0 = 0x%04x\n",
3068 index
, UREAD2(sc
, port
)));
3070 x
= URWMASK(UREAD2(sc
, port
));
3071 UWRITE2(sc
, port
, x
& ~UHCI_PORTSC_PR
);
3075 DPRINTFN(3,("uhci port %d reset, status1 = 0x%04x\n",
3076 index
, UREAD2(sc
, port
)));
3078 x
= URWMASK(UREAD2(sc
, port
));
3079 UWRITE2(sc
, port
, x
| UHCI_PORTSC_PE
);
3081 for (lim
= 10; --lim
> 0;) {
3082 usb_delay_ms(&sc
->sc_bus
, USB_PORT_RESET_DELAY
);
3084 x
= UREAD2(sc
, port
);
3086 DPRINTFN(3,("uhci port %d iteration %u, status = 0x%04x\n",
3089 if (!(x
& UHCI_PORTSC_CCS
)) {
3091 * No device is connected (or was disconnected
3092 * during reset). Consider the port reset.
3093 * The delay must be long enough to ensure on
3094 * the initial iteration that the device
3095 * connection will have been registered. 50ms
3096 * appears to be sufficient, but 20ms is not.
3098 DPRINTFN(3,("uhci port %d loop %u, device detached\n",
3103 if (x
& (UHCI_PORTSC_POEDC
| UHCI_PORTSC_CSC
)) {
3105 * Port enabled changed and/or connection
3106 * status changed were set. Reset either or
3107 * both raised flags (by writing a 1 to that
3108 * bit), and wait again for state to settle.
3110 UWRITE2(sc
, port
, URWMASK(x
) |
3111 (x
& (UHCI_PORTSC_POEDC
| UHCI_PORTSC_CSC
)));
3115 if (x
& UHCI_PORTSC_PE
)
3116 /* Port is enabled */
3119 UWRITE2(sc
, port
, URWMASK(x
) | UHCI_PORTSC_PE
);
3122 DPRINTFN(3,("uhci port %d reset, status2 = 0x%04x\n",
3123 index
, UREAD2(sc
, port
)));
3126 DPRINTFN(1,("uhci port %d reset timed out\n", index
));
3127 return (USBD_TIMEOUT
);
3131 return (USBD_NORMAL_COMPLETION
);
3135 * Simulate a hardware hub by handling all the necessary requests.
3138 uhci_root_ctrl_transfer(usbd_xfer_handle xfer
)
3142 /* Insert last in queue. */
3143 err
= usb_insert_transfer(xfer
);
3148 * Pipe isn't running (otherwise err would be USBD_INPROG),
3149 * so start it first.
3151 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer
->pipe
->queue
)));
3155 uhci_root_ctrl_start(usbd_xfer_handle xfer
)
3157 uhci_softc_t
*sc
= (uhci_softc_t
*)xfer
->pipe
->device
->bus
;
3158 usb_device_request_t
*req
;
3161 int len
, value
, index
, status
, change
, l
, totlen
= 0;
3162 usb_port_status_t ps
;
3166 return (USBD_IOERROR
);
3169 if (!(xfer
->rqflags
& URQ_REQUEST
))
3170 panic("uhci_root_ctrl_transfer: not a request");
3172 req
= &xfer
->request
;
3174 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
3175 req
->bmRequestType
, req
->bRequest
));
3177 len
= UGETW(req
->wLength
);
3178 value
= UGETW(req
->wValue
);
3179 index
= UGETW(req
->wIndex
);
3182 buf
= KERNADDR(&xfer
->dmabuf
, 0);
3184 #define C(x,y) ((x) | ((y) << 8))
3185 switch(C(req
->bRequest
, req
->bmRequestType
)) {
3186 case C(UR_CLEAR_FEATURE
, UT_WRITE_DEVICE
):
3187 case C(UR_CLEAR_FEATURE
, UT_WRITE_INTERFACE
):
3188 case C(UR_CLEAR_FEATURE
, UT_WRITE_ENDPOINT
):
3190 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3191 * for the integrated root hub.
3194 case C(UR_GET_CONFIG
, UT_READ_DEVICE
):
3196 *(u_int8_t
*)buf
= sc
->sc_conf
;
3200 case C(UR_GET_DESCRIPTOR
, UT_READ_DEVICE
):
3201 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value
));
3202 switch(value
>> 8) {
3204 if ((value
& 0xff) != 0) {
3208 totlen
= l
= min(len
, USB_DEVICE_DESCRIPTOR_SIZE
);
3209 USETW(uhci_devd
.idVendor
, sc
->sc_id_vendor
);
3210 memcpy(buf
, &uhci_devd
, l
);
3213 if ((value
& 0xff) != 0) {
3217 totlen
= l
= min(len
, USB_CONFIG_DESCRIPTOR_SIZE
);
3218 memcpy(buf
, &uhci_confd
, l
);
3219 buf
= (char *)buf
+ l
;
3221 l
= min(len
, USB_INTERFACE_DESCRIPTOR_SIZE
);
3223 memcpy(buf
, &uhci_ifcd
, l
);
3224 buf
= (char *)buf
+ l
;
3226 l
= min(len
, USB_ENDPOINT_DESCRIPTOR_SIZE
);
3228 memcpy(buf
, &uhci_endpd
, l
);
3233 *(u_int8_t
*)buf
= 0;
3235 switch (value
& 0xff) {
3236 case 1: /* Vendor */
3237 totlen
= uhci_str(buf
, len
, sc
->sc_vendor
);
3239 case 2: /* Product */
3240 totlen
= uhci_str(buf
, len
, "UHCI root hub");
3249 case C(UR_GET_INTERFACE
, UT_READ_INTERFACE
):
3251 *(u_int8_t
*)buf
= 0;
3255 case C(UR_GET_STATUS
, UT_READ_DEVICE
):
3257 USETW(((usb_status_t
*)buf
)->wStatus
,UDS_SELF_POWERED
);
3261 case C(UR_GET_STATUS
, UT_READ_INTERFACE
):
3262 case C(UR_GET_STATUS
, UT_READ_ENDPOINT
):
3264 USETW(((usb_status_t
*)buf
)->wStatus
, 0);
3268 case C(UR_SET_ADDRESS
, UT_WRITE_DEVICE
):
3269 if (value
>= USB_MAX_DEVICES
) {
3273 sc
->sc_addr
= value
;
3275 case C(UR_SET_CONFIG
, UT_WRITE_DEVICE
):
3276 if (value
!= 0 && value
!= 1) {
3280 sc
->sc_conf
= value
;
3282 case C(UR_SET_DESCRIPTOR
, UT_WRITE_DEVICE
):
3284 case C(UR_SET_FEATURE
, UT_WRITE_DEVICE
):
3285 case C(UR_SET_FEATURE
, UT_WRITE_INTERFACE
):
3286 case C(UR_SET_FEATURE
, UT_WRITE_ENDPOINT
):
3289 case C(UR_SET_INTERFACE
, UT_WRITE_INTERFACE
):
3291 case C(UR_SYNCH_FRAME
, UT_WRITE_ENDPOINT
):
3294 case C(UR_CLEAR_FEATURE
, UT_WRITE_CLASS_DEVICE
):
3296 case C(UR_CLEAR_FEATURE
, UT_WRITE_CLASS_OTHER
):
3297 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
3298 "port=%d feature=%d\n",
3301 port
= UHCI_PORTSC1
;
3302 else if (index
== 2)
3303 port
= UHCI_PORTSC2
;
3309 case UHF_PORT_ENABLE
:
3310 x
= URWMASK(UREAD2(sc
, port
));
3311 UWRITE2(sc
, port
, x
& ~UHCI_PORTSC_PE
);
3313 case UHF_PORT_SUSPEND
:
3314 x
= URWMASK(UREAD2(sc
, port
));
3315 UWRITE2(sc
, port
, x
& ~UHCI_PORTSC_SUSP
);
3317 case UHF_PORT_RESET
:
3318 x
= URWMASK(UREAD2(sc
, port
));
3319 UWRITE2(sc
, port
, x
& ~UHCI_PORTSC_PR
);
3321 case UHF_C_PORT_CONNECTION
:
3322 x
= URWMASK(UREAD2(sc
, port
));
3323 UWRITE2(sc
, port
, x
| UHCI_PORTSC_CSC
);
3325 case UHF_C_PORT_ENABLE
:
3326 x
= URWMASK(UREAD2(sc
, port
));
3327 UWRITE2(sc
, port
, x
| UHCI_PORTSC_POEDC
);
3329 case UHF_C_PORT_OVER_CURRENT
:
3330 x
= URWMASK(UREAD2(sc
, port
));
3331 UWRITE2(sc
, port
, x
| UHCI_PORTSC_OCIC
);
3333 case UHF_C_PORT_RESET
:
3335 err
= USBD_NORMAL_COMPLETION
;
3337 case UHF_PORT_CONNECTION
:
3338 case UHF_PORT_OVER_CURRENT
:
3339 case UHF_PORT_POWER
:
3340 case UHF_PORT_LOW_SPEED
:
3341 case UHF_C_PORT_SUSPEND
:
3347 case C(UR_GET_BUS_STATE
, UT_READ_CLASS_OTHER
):
3349 port
= UHCI_PORTSC1
;
3350 else if (index
== 2)
3351 port
= UHCI_PORTSC2
;
3358 (UREAD2(sc
, port
) & UHCI_PORTSC_LS
) >>
3359 UHCI_PORTSC_LS_SHIFT
;
3363 case C(UR_GET_DESCRIPTOR
, UT_READ_CLASS_DEVICE
):
3364 if ((value
& 0xff) != 0) {
3368 l
= min(len
, USB_HUB_DESCRIPTOR_SIZE
);
3370 memcpy(buf
, &uhci_hubd_piix
, l
);
3372 case C(UR_GET_STATUS
, UT_READ_CLASS_DEVICE
):
3377 memset(buf
, 0, len
);
3380 case C(UR_GET_STATUS
, UT_READ_CLASS_OTHER
):
3382 port
= UHCI_PORTSC1
;
3383 else if (index
== 2)
3384 port
= UHCI_PORTSC2
;
3393 x
= UREAD2(sc
, port
);
3394 status
= change
= 0;
3395 if (x
& UHCI_PORTSC_CCS
)
3396 status
|= UPS_CURRENT_CONNECT_STATUS
;
3397 if (x
& UHCI_PORTSC_CSC
)
3398 change
|= UPS_C_CONNECT_STATUS
;
3399 if (x
& UHCI_PORTSC_PE
)
3400 status
|= UPS_PORT_ENABLED
;
3401 if (x
& UHCI_PORTSC_POEDC
)
3402 change
|= UPS_C_PORT_ENABLED
;
3403 if (x
& UHCI_PORTSC_OCI
)
3404 status
|= UPS_OVERCURRENT_INDICATOR
;
3405 if (x
& UHCI_PORTSC_OCIC
)
3406 change
|= UPS_C_OVERCURRENT_INDICATOR
;
3407 if (x
& UHCI_PORTSC_SUSP
)
3408 status
|= UPS_SUSPEND
;
3409 if (x
& UHCI_PORTSC_LSDA
)
3410 status
|= UPS_LOW_SPEED
;
3411 status
|= UPS_PORT_POWER
;
3413 change
|= UPS_C_PORT_RESET
;
3414 USETW(ps
.wPortStatus
, status
);
3415 USETW(ps
.wPortChange
, change
);
3416 l
= min(len
, sizeof ps
);
3417 memcpy(buf
, &ps
, l
);
3420 case C(UR_SET_DESCRIPTOR
, UT_WRITE_CLASS_DEVICE
):
3423 case C(UR_SET_FEATURE
, UT_WRITE_CLASS_DEVICE
):
3425 case C(UR_SET_FEATURE
, UT_WRITE_CLASS_OTHER
):
3427 port
= UHCI_PORTSC1
;
3428 else if (index
== 2)
3429 port
= UHCI_PORTSC2
;
3435 case UHF_PORT_ENABLE
:
3436 x
= URWMASK(UREAD2(sc
, port
));
3437 UWRITE2(sc
, port
, x
| UHCI_PORTSC_PE
);
3439 case UHF_PORT_SUSPEND
:
3440 x
= URWMASK(UREAD2(sc
, port
));
3441 UWRITE2(sc
, port
, x
| UHCI_PORTSC_SUSP
);
3443 case UHF_PORT_RESET
:
3444 err
= uhci_portreset(sc
, index
);
3446 case UHF_PORT_POWER
:
3447 /* Pretend we turned on power */
3448 err
= USBD_NORMAL_COMPLETION
;
3450 case UHF_C_PORT_CONNECTION
:
3451 case UHF_C_PORT_ENABLE
:
3452 case UHF_C_PORT_OVER_CURRENT
:
3453 case UHF_PORT_CONNECTION
:
3454 case UHF_PORT_OVER_CURRENT
:
3455 case UHF_PORT_LOW_SPEED
:
3456 case UHF_C_PORT_SUSPEND
:
3457 case UHF_C_PORT_RESET
:
3467 xfer
->actlen
= totlen
;
3468 err
= USBD_NORMAL_COMPLETION
;
3472 usb_transfer_complete(xfer
);
3474 return (USBD_IN_PROGRESS
);
3477 /* Abort a root control request. */
3479 uhci_root_ctrl_abort(usbd_xfer_handle xfer
)
3481 /* Nothing to do, all transfers are synchronous. */
3484 /* Close the root pipe. */
3486 uhci_root_ctrl_close(usbd_pipe_handle pipe
)
3488 DPRINTF(("uhci_root_ctrl_close\n"));
3491 /* Abort a root interrupt request. */
3493 uhci_root_intr_abort(usbd_xfer_handle xfer
)
3495 uhci_softc_t
*sc
= (uhci_softc_t
*)xfer
->pipe
->device
->bus
;
3497 usb_uncallout(sc
->sc_poll_handle
, uhci_poll_hub
, xfer
);
3498 sc
->sc_intr_xfer
= NULL
;
3500 if (xfer
->pipe
->intrxfer
== xfer
) {
3501 DPRINTF(("uhci_root_intr_abort: remove\n"));
3502 xfer
->pipe
->intrxfer
= 0;
3504 xfer
->status
= USBD_CANCELLED
;
3506 UXFER(xfer
)->iinfo
.isdone
= 1;
3508 usb_transfer_complete(xfer
);
3512 uhci_root_intr_transfer(usbd_xfer_handle xfer
)
3516 /* Insert last in queue. */
3517 err
= usb_insert_transfer(xfer
);
3522 * Pipe isn't running (otherwise err would be USBD_INPROG),
3523 * so start it first.
3525 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer
->pipe
->queue
)));
3528 /* Start a transfer on the root interrupt pipe */
3530 uhci_root_intr_start(usbd_xfer_handle xfer
)
3532 usbd_pipe_handle pipe
= xfer
->pipe
;
3533 uhci_softc_t
*sc
= (uhci_softc_t
*)pipe
->device
->bus
;
3535 DPRINTFN(3, ("uhci_root_intr_start: xfer=%p len=%d flags=%d\n",
3536 xfer
, xfer
->length
, xfer
->flags
));
3539 return (USBD_IOERROR
);
3541 sc
->sc_ival
= MS_TO_TICKS(xfer
->pipe
->endpoint
->edesc
->bInterval
);
3542 usb_callout(sc
->sc_poll_handle
, sc
->sc_ival
, uhci_poll_hub
, xfer
);
3543 sc
->sc_intr_xfer
= xfer
;
3544 return (USBD_IN_PROGRESS
);
3547 /* Close the root interrupt pipe. */
3549 uhci_root_intr_close(usbd_pipe_handle pipe
)
3551 uhci_softc_t
*sc
= (uhci_softc_t
*)pipe
->device
->bus
;
3553 usb_uncallout(sc
->sc_poll_handle
, uhci_poll_hub
, sc
->sc_intr_xfer
);
3554 sc
->sc_intr_xfer
= NULL
;
3555 DPRINTF(("uhci_root_intr_close\n"));