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