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