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