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