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