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