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