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