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