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