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