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