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