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