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