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