uhci.c revision 1.126 1 /* $NetBSD: uhci.c,v 1.126 2000/11/10 14:11:49 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, len;
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 len = UHCI_TD_GET_ACTLEN(status);
1351 xfer->frlengths[i] = len;
1352 actlen += len;
1353 }
1354 upipe->u.iso.inuse -= nframes;
1355 xfer->actlen = actlen;
1356 xfer->status = USBD_NORMAL_COMPLETION;
1357 usb_transfer_complete(xfer);
1358 return;
1359 }
1360
1361 #ifdef UHCI_DEBUG
1362 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1363 ii, xfer, upipe));
1364 if (uhcidebug > 10)
1365 uhci_dump_tds(ii->stdstart);
1366 #endif
1367
1368 /* The transfer is done, compute actual length and status. */
1369 actlen = 0;
1370 for (std = ii->stdstart; std != NULL; std = std->link.std) {
1371 nstatus = le32toh(std->td.td_status);
1372 if (nstatus & UHCI_TD_ACTIVE)
1373 break;
1374
1375 status = nstatus;
1376 if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
1377 UHCI_TD_PID_SETUP)
1378 actlen += UHCI_TD_GET_ACTLEN(status);
1379 }
1380 /* If there are left over TDs we need to update the toggle. */
1381 if (std != NULL)
1382 upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
1383
1384 status &= UHCI_TD_ERROR;
1385 DPRINTFN(10, ("uhci_check_intr: actlen=%d, status=0x%x\n",
1386 actlen, status));
1387 xfer->actlen = actlen;
1388 if (status != 0) {
1389 #ifdef UHCI_DEBUG
1390 char sbuf[128];
1391
1392 bitmask_snprintf((int)status, "\20\22BITSTUFF\23CRCTO\24NAK\25"
1393 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
1394 sbuf, sizeof(sbuf));
1395
1396 DPRINTFN((status == UHCI_TD_STALLED)*10,
1397 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1398 "status 0x%s\n",
1399 xfer->pipe->device->address,
1400 xfer->pipe->endpoint->edesc->bEndpointAddress,
1401 sbuf));
1402 #endif
1403
1404 if (status == UHCI_TD_STALLED)
1405 xfer->status = USBD_STALLED;
1406 else
1407 xfer->status = USBD_IOERROR; /* more info XXX */
1408 } else {
1409 xfer->status = USBD_NORMAL_COMPLETION;
1410 }
1411 usb_transfer_complete(xfer);
1412 }
1413
1414 /*
1415 * Called when a request does not complete.
1416 */
1417 void
1418 uhci_timeout(void *addr)
1419 {
1420 uhci_intr_info_t *ii = addr;
1421
1422 DPRINTF(("uhci_timeout: ii=%p\n", ii));
1423
1424 #ifdef UHCI_DEBUG
1425 if (uhcidebug > 10)
1426 uhci_dump_tds(ii->stdstart);
1427 #endif
1428
1429 ii->xfer->device->bus->intr_context++;
1430 uhci_abort_xfer(ii->xfer, USBD_TIMEOUT);
1431 ii->xfer->device->bus->intr_context--;
1432 }
1433
1434 /*
1435 * Wait here until controller claims to have an interrupt.
1436 * Then call uhci_intr and return. Use timeout to avoid waiting
1437 * too long.
1438 * Only used during boot when interrupts are not enabled yet.
1439 */
1440 void
1441 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
1442 {
1443 int timo = xfer->timeout;
1444 uhci_intr_info_t *ii;
1445
1446 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1447
1448 xfer->status = USBD_IN_PROGRESS;
1449 for (; timo >= 0; timo--) {
1450 usb_delay_ms(&sc->sc_bus, 1);
1451 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1452 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
1453 uhci_intr(sc);
1454 if (xfer->status != USBD_IN_PROGRESS)
1455 return;
1456 }
1457 }
1458
1459 /* Timeout */
1460 DPRINTF(("uhci_waitintr: timeout\n"));
1461 for (ii = LIST_FIRST(&sc->sc_intrhead);
1462 ii != NULL && ii->xfer != xfer;
1463 ii = LIST_NEXT(ii, list))
1464 ;
1465 #ifdef DIAGNOSTIC
1466 if (ii == NULL)
1467 panic("uhci_waitintr: lost intr_info\n");
1468 #endif
1469 uhci_idone(ii);
1470 }
1471
1472 void
1473 uhci_poll(struct usbd_bus *bus)
1474 {
1475 uhci_softc_t *sc = (uhci_softc_t *)bus;
1476
1477 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
1478 uhci_intr(sc);
1479 }
1480
1481 #if 0
1482 void
1483 uhci_reset(uhci_softc_t *sc)
1484 {
1485 int n;
1486
1487 UHCICMD(sc, UHCI_CMD_HCRESET);
1488 /* The reset bit goes low when the controller is done. */
1489 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1490 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1491 usb_delay_ms(&sc->sc_bus, 1);
1492 if (n >= UHCI_RESET_TIMEOUT)
1493 printf("%s: controller did not reset\n",
1494 USBDEVNAME(sc->sc_bus.bdev));
1495 }
1496 #endif
1497
1498 usbd_status
1499 uhci_run(uhci_softc_t *sc, int run)
1500 {
1501 int s, n, running;
1502 u_int16_t cmd;
1503
1504 run = run != 0;
1505 s = splusb();
1506 DPRINTF(("uhci_run: setting run=%d\n", run));
1507 cmd = UREAD2(sc, UHCI_CMD);
1508 if (run)
1509 cmd |= UHCI_CMD_RS;
1510 else
1511 cmd &= ~UHCI_CMD_RS;
1512 UHCICMD(sc, cmd);
1513 for(n = 0; n < 10; n++) {
1514 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1515 /* return when we've entered the state we want */
1516 if (run == running) {
1517 splx(s);
1518 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1519 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1520 return (USBD_NORMAL_COMPLETION);
1521 }
1522 usb_delay_ms(&sc->sc_bus, 1);
1523 }
1524 splx(s);
1525 printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
1526 run ? "start" : "stop");
1527 return (USBD_IOERROR);
1528 }
1529
1530 /*
1531 * Memory management routines.
1532 * uhci_alloc_std allocates TDs
1533 * uhci_alloc_sqh allocates QHs
1534 * These two routines do their own free list management,
1535 * partly for speed, partly because allocating DMAable memory
1536 * has page size granularaity so much memory would be wasted if
1537 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1538 */
1539
1540 uhci_soft_td_t *
1541 uhci_alloc_std(uhci_softc_t *sc)
1542 {
1543 uhci_soft_td_t *std;
1544 usbd_status err;
1545 int i, offs;
1546 usb_dma_t dma;
1547
1548 if (sc->sc_freetds == NULL) {
1549 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1550 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1551 UHCI_TD_ALIGN, &dma);
1552 if (err)
1553 return (0);
1554 for(i = 0; i < UHCI_STD_CHUNK; i++) {
1555 offs = i * UHCI_STD_SIZE;
1556 std = (uhci_soft_td_t *)((char *)KERNADDR(&dma) +offs);
1557 std->physaddr = DMAADDR(&dma) + offs;
1558 std->link.std = sc->sc_freetds;
1559 sc->sc_freetds = std;
1560 }
1561 }
1562 std = sc->sc_freetds;
1563 sc->sc_freetds = std->link.std;
1564 memset(&std->td, 0, sizeof(uhci_td_t));
1565 return std;
1566 }
1567
1568 void
1569 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
1570 {
1571 #ifdef DIAGNOSTIC
1572 #define TD_IS_FREE 0x12345678
1573 if (le32toh(std->td.td_token) == TD_IS_FREE) {
1574 printf("uhci_free_std: freeing free TD %p\n", std);
1575 return;
1576 }
1577 std->td.td_token = htole32(TD_IS_FREE);
1578 #endif
1579 std->link.std = sc->sc_freetds;
1580 sc->sc_freetds = std;
1581 }
1582
1583 uhci_soft_qh_t *
1584 uhci_alloc_sqh(uhci_softc_t *sc)
1585 {
1586 uhci_soft_qh_t *sqh;
1587 usbd_status err;
1588 int i, offs;
1589 usb_dma_t dma;
1590
1591 if (sc->sc_freeqhs == NULL) {
1592 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1593 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1594 UHCI_QH_ALIGN, &dma);
1595 if (err)
1596 return (0);
1597 for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1598 offs = i * UHCI_SQH_SIZE;
1599 sqh = (uhci_soft_qh_t *)((char *)KERNADDR(&dma) +offs);
1600 sqh->physaddr = DMAADDR(&dma) + offs;
1601 sqh->hlink = sc->sc_freeqhs;
1602 sc->sc_freeqhs = sqh;
1603 }
1604 }
1605 sqh = sc->sc_freeqhs;
1606 sc->sc_freeqhs = sqh->hlink;
1607 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1608 return (sqh);
1609 }
1610
1611 void
1612 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1613 {
1614 sqh->hlink = sc->sc_freeqhs;
1615 sc->sc_freeqhs = sqh;
1616 }
1617
1618 void
1619 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
1620 uhci_soft_td_t *stdend)
1621 {
1622 uhci_soft_td_t *p;
1623
1624 for (; std != stdend; std = p) {
1625 p = std->link.std;
1626 uhci_free_std(sc, std);
1627 }
1628 }
1629
1630 usbd_status
1631 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len,
1632 int rd, u_int16_t flags, usb_dma_t *dma,
1633 uhci_soft_td_t **sp, uhci_soft_td_t **ep)
1634 {
1635 uhci_soft_td_t *p, *lastp;
1636 uhci_physaddr_t lastlink;
1637 int i, ntd, l, tog, maxp;
1638 u_int32_t status;
1639 int addr = upipe->pipe.device->address;
1640 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1641
1642 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d ls=%d "
1643 "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
1644 upipe->pipe.device->lowspeed, flags));
1645 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1646 if (maxp == 0) {
1647 printf("uhci_alloc_std_chain: maxp=0\n");
1648 return (USBD_INVAL);
1649 }
1650 ntd = (len + maxp - 1) / maxp;
1651 if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
1652 ntd++;
1653 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1654 if (ntd == 0) {
1655 *sp = *ep = 0;
1656 DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
1657 return (USBD_NORMAL_COMPLETION);
1658 }
1659 tog = upipe->nexttoggle;
1660 if (ntd % 2 == 0)
1661 tog ^= 1;
1662 upipe->nexttoggle = tog ^ 1;
1663 lastp = NULL;
1664 lastlink = UHCI_PTR_T;
1665 ntd--;
1666 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1667 if (upipe->pipe.device->lowspeed)
1668 status |= UHCI_TD_LS;
1669 if (flags & USBD_SHORT_XFER_OK)
1670 status |= UHCI_TD_SPD;
1671 for (i = ntd; i >= 0; i--) {
1672 p = uhci_alloc_std(sc);
1673 if (p == NULL) {
1674 uhci_free_std_chain(sc, lastp, 0);
1675 return (USBD_NOMEM);
1676 }
1677 p->link.std = lastp;
1678 p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD);
1679 lastp = p;
1680 lastlink = p->physaddr;
1681 p->td.td_status = htole32(status);
1682 if (i == ntd) {
1683 /* last TD */
1684 l = len % maxp;
1685 if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
1686 l = maxp;
1687 *ep = p;
1688 } else
1689 l = maxp;
1690 p->td.td_token =
1691 htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1692 UHCI_TD_OUT(l, endpt, addr, tog));
1693 p->td.td_buffer = htole32(DMAADDR(dma) + i * maxp);
1694 tog ^= 1;
1695 }
1696 *sp = lastp;
1697 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1698 upipe->nexttoggle));
1699 return (USBD_NORMAL_COMPLETION);
1700 }
1701
1702 void
1703 uhci_device_clear_toggle(usbd_pipe_handle pipe)
1704 {
1705 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1706 upipe->nexttoggle = 0;
1707 }
1708
1709 void
1710 uhci_noop(usbd_pipe_handle pipe)
1711 {
1712 }
1713
1714 usbd_status
1715 uhci_device_bulk_transfer(usbd_xfer_handle xfer)
1716 {
1717 usbd_status err;
1718
1719 /* Insert last in queue. */
1720 err = usb_insert_transfer(xfer);
1721 if (err)
1722 return (err);
1723
1724 /*
1725 * Pipe isn't running (otherwise err would be USBD_INPROG),
1726 * so start it first.
1727 */
1728 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1729 }
1730
1731 usbd_status
1732 uhci_device_bulk_start(usbd_xfer_handle xfer)
1733 {
1734 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1735 usbd_device_handle dev = upipe->pipe.device;
1736 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1737 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1738 uhci_soft_td_t *data, *dataend;
1739 uhci_soft_qh_t *sqh;
1740 usbd_status err;
1741 int len, isread, endpt;
1742 int s;
1743
1744 DPRINTFN(3, ("uhci_device_bulk_transfer: xfer=%p len=%d flags=%d\n",
1745 xfer, xfer->length, xfer->flags));
1746
1747 if (sc->sc_dying)
1748 return (USBD_IOERROR);
1749
1750 #ifdef DIAGNOSTIC
1751 if (xfer->rqflags & URQ_REQUEST)
1752 panic("uhci_device_bulk_transfer: a request\n");
1753 #endif
1754
1755 len = xfer->length;
1756 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1757 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1758 sqh = upipe->u.bulk.sqh;
1759
1760 upipe->u.bulk.isread = isread;
1761 upipe->u.bulk.length = len;
1762
1763 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
1764 &xfer->dmabuf, &data, &dataend);
1765 if (err)
1766 return (err);
1767 dataend->td.td_status |= htole32(UHCI_TD_IOC);
1768
1769 #ifdef UHCI_DEBUG
1770 if (uhcidebug > 8) {
1771 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1772 uhci_dump_tds(data);
1773 }
1774 #endif
1775
1776 /* Set up interrupt info. */
1777 ii->xfer = xfer;
1778 ii->stdstart = data;
1779 ii->stdend = dataend;
1780 #ifdef DIAGNOSTIC
1781 if (!ii->isdone) {
1782 printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
1783 }
1784 ii->isdone = 0;
1785 #endif
1786
1787 sqh->elink = data;
1788 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
1789
1790 s = splusb();
1791 uhci_add_bulk(sc, sqh);
1792 uhci_add_intr_info(sc, ii);
1793
1794 if (xfer->timeout && !sc->sc_bus.use_polling) {
1795 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1796 uhci_timeout, ii);
1797 }
1798 xfer->status = USBD_IN_PROGRESS;
1799 splx(s);
1800
1801 #ifdef UHCI_DEBUG
1802 if (uhcidebug > 10) {
1803 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1804 uhci_dump_tds(data);
1805 }
1806 #endif
1807
1808 if (sc->sc_bus.use_polling)
1809 uhci_waitintr(sc, xfer);
1810
1811 return (USBD_IN_PROGRESS);
1812 }
1813
1814 /* Abort a device bulk request. */
1815 void
1816 uhci_device_bulk_abort(usbd_xfer_handle xfer)
1817 {
1818 DPRINTF(("uhci_device_bulk_abort:\n"));
1819 uhci_abort_xfer(xfer, USBD_CANCELLED);
1820 }
1821
1822 /*
1823 * XXX This way of aborting is neither safe, nor good.
1824 * But it will have to do until I figure out what to do.
1825 * I apologize for the delay().
1826 */
1827 void
1828 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1829 {
1830 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1831 uhci_soft_td_t *std;
1832 int s;
1833
1834 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1835
1836 s = splusb();
1837
1838 /* Transfer is already done. */
1839 if (xfer->status != USBD_NOT_STARTED &&
1840 xfer->status != USBD_IN_PROGRESS) {
1841 splx(s);
1842 return;
1843 }
1844
1845 /* Make interrupt routine ignore it, */
1846 xfer->status = status;
1847
1848 /* don't timeout, */
1849 usb_uncallout(xfer->timeout_handle, uhci_timeout, ii);
1850
1851 /* make hardware ignore it, */
1852 for (std = ii->stdstart; std != NULL; std = std->link.std)
1853 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1854
1855 xfer->hcpriv = ii;
1856
1857 splx(s);
1858
1859 delay(1000);
1860
1861 s = splusb();
1862 #ifdef DIAGNOSTIC
1863 ii->isdone = 1;
1864 #endif
1865 usb_transfer_complete(xfer);
1866 splx(s);
1867 }
1868
1869 /* Close a device bulk pipe. */
1870 void
1871 uhci_device_bulk_close(usbd_pipe_handle pipe)
1872 {
1873 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1874 usbd_device_handle dev = upipe->pipe.device;
1875 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1876
1877 uhci_free_sqh(sc, upipe->u.bulk.sqh);
1878 }
1879
1880 usbd_status
1881 uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
1882 {
1883 usbd_status err;
1884
1885 /* Insert last in queue. */
1886 err = usb_insert_transfer(xfer);
1887 if (err)
1888 return (err);
1889
1890 /*
1891 * Pipe isn't running (otherwise err would be USBD_INPROG),
1892 * so start it first.
1893 */
1894 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1895 }
1896
1897 usbd_status
1898 uhci_device_ctrl_start(usbd_xfer_handle xfer)
1899 {
1900 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
1901 usbd_status err;
1902
1903 if (sc->sc_dying)
1904 return (USBD_IOERROR);
1905
1906 #ifdef DIAGNOSTIC
1907 if (!(xfer->rqflags & URQ_REQUEST))
1908 panic("uhci_device_ctrl_transfer: not a request\n");
1909 #endif
1910
1911 err = uhci_device_request(xfer);
1912 if (err)
1913 return (err);
1914
1915 if (sc->sc_bus.use_polling)
1916 uhci_waitintr(sc, xfer);
1917 return (USBD_IN_PROGRESS);
1918 }
1919
1920 usbd_status
1921 uhci_device_intr_transfer(usbd_xfer_handle xfer)
1922 {
1923 usbd_status err;
1924
1925 /* Insert last in queue. */
1926 err = usb_insert_transfer(xfer);
1927 if (err)
1928 return (err);
1929
1930 /*
1931 * Pipe isn't running (otherwise err would be USBD_INPROG),
1932 * so start it first.
1933 */
1934 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1935 }
1936
1937 usbd_status
1938 uhci_device_intr_start(usbd_xfer_handle xfer)
1939 {
1940 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1941 usbd_device_handle dev = upipe->pipe.device;
1942 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1943 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1944 uhci_soft_td_t *data, *dataend;
1945 uhci_soft_qh_t *sqh;
1946 usbd_status err;
1947 int i, s;
1948
1949 if (sc->sc_dying)
1950 return (USBD_IOERROR);
1951
1952 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
1953 xfer, xfer->length, xfer->flags));
1954
1955 #ifdef DIAGNOSTIC
1956 if (xfer->rqflags & URQ_REQUEST)
1957 panic("uhci_device_intr_transfer: a request\n");
1958 #endif
1959
1960 err = uhci_alloc_std_chain(upipe, sc, xfer->length, 1, xfer->flags,
1961 &xfer->dmabuf, &data, &dataend);
1962 if (err)
1963 return (err);
1964 dataend->td.td_status |= htole32(UHCI_TD_IOC);
1965
1966 #ifdef UHCI_DEBUG
1967 if (uhcidebug > 10) {
1968 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
1969 uhci_dump_tds(data);
1970 uhci_dump_qh(upipe->u.intr.qhs[0]);
1971 }
1972 #endif
1973
1974 s = splusb();
1975 /* Set up interrupt info. */
1976 ii->xfer = xfer;
1977 ii->stdstart = data;
1978 ii->stdend = dataend;
1979 #ifdef DIAGNOSTIC
1980 if (!ii->isdone) {
1981 printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
1982 }
1983 ii->isdone = 0;
1984 #endif
1985
1986 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
1987 upipe->u.intr.qhs[0]));
1988 for (i = 0; i < upipe->u.intr.npoll; i++) {
1989 sqh = upipe->u.intr.qhs[i];
1990 sqh->elink = data;
1991 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
1992 }
1993 uhci_add_intr_info(sc, ii);
1994 xfer->status = USBD_IN_PROGRESS;
1995 splx(s);
1996
1997 #ifdef UHCI_DEBUG
1998 if (uhcidebug > 10) {
1999 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
2000 uhci_dump_tds(data);
2001 uhci_dump_qh(upipe->u.intr.qhs[0]);
2002 }
2003 #endif
2004
2005 return (USBD_IN_PROGRESS);
2006 }
2007
2008 /* Abort a device control request. */
2009 void
2010 uhci_device_ctrl_abort(usbd_xfer_handle xfer)
2011 {
2012 DPRINTF(("uhci_device_ctrl_abort:\n"));
2013 uhci_abort_xfer(xfer, USBD_CANCELLED);
2014 }
2015
2016 /* Close a device control pipe. */
2017 void
2018 uhci_device_ctrl_close(usbd_pipe_handle pipe)
2019 {
2020 }
2021
2022 /* Abort a device interrupt request. */
2023 void
2024 uhci_device_intr_abort(usbd_xfer_handle xfer)
2025 {
2026 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
2027 if (xfer->pipe->intrxfer == xfer) {
2028 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
2029 xfer->pipe->intrxfer = 0;
2030 }
2031 uhci_abort_xfer(xfer, USBD_CANCELLED);
2032 }
2033
2034 /* Close a device interrupt pipe. */
2035 void
2036 uhci_device_intr_close(usbd_pipe_handle pipe)
2037 {
2038 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2039 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2040 int i, npoll;
2041 int s;
2042
2043 /* Unlink descriptors from controller data structures. */
2044 npoll = upipe->u.intr.npoll;
2045 s = splusb();
2046 for (i = 0; i < npoll; i++)
2047 uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
2048 splx(s);
2049
2050 /*
2051 * We now have to wait for any activity on the physical
2052 * descriptors to stop.
2053 */
2054 usb_delay_ms(&sc->sc_bus, 2);
2055
2056 for(i = 0; i < npoll; i++)
2057 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
2058 free(upipe->u.intr.qhs, M_USBHC);
2059
2060 /* XXX free other resources */
2061 }
2062
2063 usbd_status
2064 uhci_device_request(usbd_xfer_handle xfer)
2065 {
2066 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2067 usb_device_request_t *req = &xfer->request;
2068 usbd_device_handle dev = upipe->pipe.device;
2069 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2070 int addr = dev->address;
2071 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2072 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2073 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
2074 uhci_soft_qh_t *sqh;
2075 int len;
2076 u_int32_t ls;
2077 usbd_status err;
2078 int isread;
2079 int s;
2080
2081 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
2082 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2083 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2084 UGETW(req->wIndex), UGETW(req->wLength),
2085 addr, endpt));
2086
2087 ls = dev->lowspeed ? UHCI_TD_LS : 0;
2088 isread = req->bmRequestType & UT_READ;
2089 len = UGETW(req->wLength);
2090
2091 setup = upipe->u.ctl.setup;
2092 stat = upipe->u.ctl.stat;
2093 sqh = upipe->u.ctl.sqh;
2094
2095 /* Set up data transaction */
2096 if (len != 0) {
2097 upipe->nexttoggle = 1;
2098 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
2099 &xfer->dmabuf, &data, &dataend);
2100 if (err)
2101 return (err);
2102 next = data;
2103 dataend->link.std = stat;
2104 dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2105 } else {
2106 next = stat;
2107 }
2108 upipe->u.ctl.length = len;
2109
2110 memcpy(KERNADDR(&upipe->u.ctl.reqdma), req, sizeof *req);
2111
2112 setup->link.std = next;
2113 setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2114 setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2115 UHCI_TD_ACTIVE);
2116 setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr));
2117 setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma));
2118
2119 stat->link.std = NULL;
2120 stat->td.td_link = htole32(UHCI_PTR_T);
2121 stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2122 UHCI_TD_ACTIVE | UHCI_TD_IOC);
2123 stat->td.td_token =
2124 htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
2125 UHCI_TD_IN (0, endpt, addr, 1));
2126 stat->td.td_buffer = htole32(0);
2127
2128 #ifdef UHCI_DEBUG
2129 if (uhcidebug > 10) {
2130 DPRINTF(("uhci_device_request: before transfer\n"));
2131 uhci_dump_tds(setup);
2132 }
2133 #endif
2134
2135 /* Set up interrupt info. */
2136 ii->xfer = xfer;
2137 ii->stdstart = setup;
2138 ii->stdend = stat;
2139 #ifdef DIAGNOSTIC
2140 if (!ii->isdone) {
2141 printf("uhci_device_request: not done, ii=%p\n", ii);
2142 }
2143 ii->isdone = 0;
2144 #endif
2145
2146 sqh->elink = setup;
2147 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
2148
2149 s = splusb();
2150 if (dev->lowspeed)
2151 uhci_add_ls_ctrl(sc, sqh);
2152 else
2153 uhci_add_hs_ctrl(sc, sqh);
2154 uhci_add_intr_info(sc, ii);
2155 #ifdef UHCI_DEBUG
2156 if (uhcidebug > 12) {
2157 uhci_soft_td_t *std;
2158 uhci_soft_qh_t *xqh;
2159 uhci_soft_qh_t *sxqh;
2160 int maxqh = 0;
2161 uhci_physaddr_t link;
2162 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
2163 for (std = sc->sc_vframes[0].htd, link = 0;
2164 (link & UHCI_PTR_QH) == 0;
2165 std = std->link.std) {
2166 link = le32toh(std->td.td_link);
2167 uhci_dump_td(std);
2168 }
2169 sxqh = (uhci_soft_qh_t *)std;
2170 uhci_dump_qh(sxqh);
2171 for (xqh = sxqh;
2172 xqh != NULL;
2173 xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
2174 xqh->hlink == xqh ? NULL : xqh->hlink)) {
2175 uhci_dump_qh(xqh);
2176 }
2177 DPRINTF(("Enqueued QH:\n"));
2178 uhci_dump_qh(sqh);
2179 uhci_dump_tds(sqh->elink);
2180 }
2181 #endif
2182 if (xfer->timeout && !sc->sc_bus.use_polling) {
2183 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2184 uhci_timeout, ii);
2185 }
2186 xfer->status = USBD_IN_PROGRESS;
2187 splx(s);
2188
2189 return (USBD_NORMAL_COMPLETION);
2190 }
2191
2192 usbd_status
2193 uhci_device_isoc_transfer(usbd_xfer_handle xfer)
2194 {
2195 usbd_status err;
2196
2197 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
2198
2199 /* Put it on our queue, */
2200 err = usb_insert_transfer(xfer);
2201
2202 /* bail out on error, */
2203 if (err && err != USBD_IN_PROGRESS)
2204 return (err);
2205
2206 /* XXX should check inuse here */
2207
2208 /* insert into schedule, */
2209 uhci_device_isoc_enter(xfer);
2210
2211 /* and start if the pipe wasn't running */
2212 if (!err)
2213 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2214
2215 return (err);
2216 }
2217
2218 void
2219 uhci_device_isoc_enter(usbd_xfer_handle xfer)
2220 {
2221 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2222 usbd_device_handle dev = upipe->pipe.device;
2223 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2224 struct iso *iso = &upipe->u.iso;
2225 uhci_soft_td_t *std;
2226 u_int32_t buf, len, status;
2227 int s, i, next, nframes;
2228
2229 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
2230 "nframes=%d\n",
2231 iso->inuse, iso->next, xfer, xfer->nframes));
2232
2233 if (sc->sc_dying)
2234 return;
2235
2236 if (xfer->status == USBD_IN_PROGRESS) {
2237 /* This request has already been entered into the frame list */
2238 printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
2239 /* XXX */
2240 }
2241
2242 #ifdef DIAGNOSTIC
2243 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
2244 printf("uhci_device_isoc_enter: overflow!\n");
2245 #endif
2246
2247 next = iso->next;
2248 if (next == -1) {
2249 /* Not in use yet, schedule it a few frames ahead. */
2250 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2251 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2252 }
2253
2254 xfer->status = USBD_IN_PROGRESS;
2255 UXFER(xfer)->curframe = next;
2256
2257 buf = DMAADDR(&xfer->dmabuf);
2258 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2259 UHCI_TD_ACTIVE |
2260 UHCI_TD_IOS);
2261 nframes = xfer->nframes;
2262 s = splusb();
2263 for (i = 0; i < nframes; i++) {
2264 std = iso->stds[next];
2265 if (++next >= UHCI_VFRAMELIST_COUNT)
2266 next = 0;
2267 len = xfer->frlengths[i];
2268 std->td.td_buffer = htole32(buf);
2269 if (i == nframes - 1)
2270 status |= UHCI_TD_IOC;
2271 std->td.td_status = htole32(status);
2272 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2273 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
2274 #ifdef UHCI_DEBUG
2275 if (uhcidebug > 5) {
2276 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2277 uhci_dump_td(std);
2278 }
2279 #endif
2280 buf += len;
2281 }
2282 iso->next = next;
2283 iso->inuse += xfer->nframes;
2284
2285 splx(s);
2286 }
2287
2288 usbd_status
2289 uhci_device_isoc_start(usbd_xfer_handle xfer)
2290 {
2291 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2292 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2293 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2294 uhci_soft_td_t *end;
2295 int s, i;
2296
2297 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
2298
2299 if (sc->sc_dying)
2300 return (USBD_IOERROR);
2301
2302 #ifdef DIAGNOSTIC
2303 if (xfer->status != USBD_IN_PROGRESS)
2304 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2305 #endif
2306
2307 /* Find the last TD */
2308 i = UXFER(xfer)->curframe + xfer->nframes;
2309 if (i >= UHCI_VFRAMELIST_COUNT)
2310 i -= UHCI_VFRAMELIST_COUNT;
2311 end = upipe->u.iso.stds[i];
2312
2313 #ifdef DIAGNOSTIC
2314 if (end == NULL) {
2315 printf("uhci_device_isoc_start: end == NULL\n");
2316 return (USBD_INVAL);
2317 }
2318 #endif
2319
2320 s = splusb();
2321
2322 /* Set up interrupt info. */
2323 ii->xfer = xfer;
2324 ii->stdstart = end;
2325 ii->stdend = end;
2326 #ifdef DIAGNOSTIC
2327 if (!ii->isdone)
2328 printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
2329 ii->isdone = 0;
2330 #endif
2331 uhci_add_intr_info(sc, ii);
2332
2333 splx(s);
2334
2335 return (USBD_IN_PROGRESS);
2336 }
2337
2338 void
2339 uhci_device_isoc_abort(usbd_xfer_handle xfer)
2340 {
2341 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2342 uhci_soft_td_t **stds = upipe->u.iso.stds;
2343 uhci_soft_td_t *std;
2344 int i, n, s, nframes, maxlen, len;
2345
2346 s = splusb();
2347
2348 /* Transfer is already done. */
2349 if (xfer->status != USBD_NOT_STARTED &&
2350 xfer->status != USBD_IN_PROGRESS) {
2351 splx(s);
2352 return;
2353 }
2354
2355 /* Give xfer the requested abort code. */
2356 xfer->status = USBD_CANCELLED;
2357
2358 /* make hardware ignore it, */
2359 nframes = xfer->nframes;
2360 n = UXFER(xfer)->curframe;
2361 maxlen = 0;
2362 for (i = 0; i < nframes; i++) {
2363 std = stds[n];
2364 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2365 len = UHCI_TD_GET_MAXLEN(std->td.td_token);
2366 if (len > maxlen)
2367 maxlen = len;
2368 if (++n >= UHCI_VFRAMELIST_COUNT)
2369 n = 0;
2370 }
2371
2372 /* and wait until we are sure the hardware has finished. */
2373 delay(maxlen);
2374
2375 #ifdef DIAGNOSTIC
2376 UXFER(xfer)->iinfo.isdone = 1;
2377 #endif
2378 /* Run callback and remove from interrupt list. */
2379 usb_transfer_complete(xfer);
2380
2381 splx(s);
2382 }
2383
2384 void
2385 uhci_device_isoc_close(usbd_pipe_handle pipe)
2386 {
2387 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2388 usbd_device_handle dev = upipe->pipe.device;
2389 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2390 uhci_soft_td_t *std, *vstd;
2391 struct iso *iso;
2392 int i, s;
2393
2394 /*
2395 * Make sure all TDs are marked as inactive.
2396 * Wait for completion.
2397 * Unschedule.
2398 * Deallocate.
2399 */
2400 iso = &upipe->u.iso;
2401
2402 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2403 iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE);
2404 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2405
2406 s = splusb();
2407 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2408 std = iso->stds[i];
2409 for (vstd = sc->sc_vframes[i].htd;
2410 vstd != NULL && vstd->link.std != std;
2411 vstd = vstd->link.std)
2412 ;
2413 if (vstd == NULL) {
2414 /*panic*/
2415 printf("uhci_device_isoc_close: %p not found\n", std);
2416 splx(s);
2417 return;
2418 }
2419 vstd->link = std->link;
2420 vstd->td.td_link = std->td.td_link;
2421 uhci_free_std(sc, std);
2422 }
2423 splx(s);
2424
2425 free(iso->stds, M_USBHC);
2426 }
2427
2428 usbd_status
2429 uhci_setup_isoc(usbd_pipe_handle pipe)
2430 {
2431 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2432 usbd_device_handle dev = upipe->pipe.device;
2433 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2434 int addr = upipe->pipe.device->address;
2435 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2436 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2437 uhci_soft_td_t *std, *vstd;
2438 u_int32_t token;
2439 struct iso *iso;
2440 int i, s;
2441
2442 iso = &upipe->u.iso;
2443 iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2444 M_USBHC, M_WAITOK);
2445
2446 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2447 UHCI_TD_OUT(0, endpt, addr, 0);
2448
2449 /* Allocate the TDs and mark as inactive; */
2450 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2451 std = uhci_alloc_std(sc);
2452 if (std == 0)
2453 goto bad;
2454 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
2455 std->td.td_token = htole32(token);
2456 iso->stds[i] = std;
2457 }
2458
2459 /* Insert TDs into schedule. */
2460 s = splusb();
2461 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2462 std = iso->stds[i];
2463 vstd = sc->sc_vframes[i].htd;
2464 std->link = vstd->link;
2465 std->td.td_link = vstd->td.td_link;
2466 vstd->link.std = std;
2467 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
2468 }
2469 splx(s);
2470
2471 iso->next = -1;
2472 iso->inuse = 0;
2473
2474 return (USBD_NORMAL_COMPLETION);
2475
2476 bad:
2477 while (--i >= 0)
2478 uhci_free_std(sc, iso->stds[i]);
2479 free(iso->stds, M_USBHC);
2480 return (USBD_NOMEM);
2481 }
2482
2483 void
2484 uhci_device_isoc_done(usbd_xfer_handle xfer)
2485 {
2486 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2487
2488 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2489
2490 if (ii->xfer != xfer)
2491 /* Not on interrupt list, ignore it. */
2492 return;
2493
2494 #ifdef DIAGNOSTIC
2495 if (xfer->busy_free != XFER_BUSY) {
2496 printf("uhci_device_isoc_done: xfer=%p not busy 0x%08x\n",
2497 xfer, xfer->busy_free);
2498 return;
2499 }
2500
2501 if (ii->stdend == NULL) {
2502 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2503 #ifdef UHCI_DEBUG
2504 uhci_dump_ii(ii);
2505 #endif
2506 return;
2507 }
2508 #endif
2509
2510 /* Turn off the interrupt since it is active even if the TD is not. */
2511 ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC);
2512
2513 uhci_del_intr_info(ii); /* remove from active list */
2514 }
2515
2516 void
2517 uhci_device_intr_done(usbd_xfer_handle xfer)
2518 {
2519 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2520 uhci_softc_t *sc = ii->sc;
2521 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2522 uhci_soft_qh_t *sqh;
2523 int i, npoll;
2524
2525 DPRINTFN(5, ("uhci_intr_done: length=%d\n", xfer->actlen));
2526
2527 npoll = upipe->u.intr.npoll;
2528 for(i = 0; i < npoll; i++) {
2529 sqh = upipe->u.intr.qhs[i];
2530 sqh->elink = NULL;
2531 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2532 }
2533 uhci_free_std_chain(sc, ii->stdstart, 0);
2534
2535 /* XXX Wasteful. */
2536 if (xfer->pipe->repeat) {
2537 uhci_soft_td_t *data, *dataend;
2538
2539 DPRINTFN(5,("uhci_device_intr_done: requeing\n"));
2540
2541 /* This alloc cannot fail since we freed the chain above. */
2542 uhci_alloc_std_chain(upipe, sc, xfer->length, 1, xfer->flags,
2543 &xfer->dmabuf, &data, &dataend);
2544 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2545
2546 #ifdef UHCI_DEBUG
2547 if (uhcidebug > 10) {
2548 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2549 uhci_dump_tds(data);
2550 uhci_dump_qh(upipe->u.intr.qhs[0]);
2551 }
2552 #endif
2553
2554 ii->stdstart = data;
2555 ii->stdend = dataend;
2556 #ifdef DIAGNOSTIC
2557 if (!ii->isdone) {
2558 printf("uhci_device_intr_done: not done, ii=%p\n", ii);
2559 }
2560 ii->isdone = 0;
2561 #endif
2562 for (i = 0; i < npoll; i++) {
2563 sqh = upipe->u.intr.qhs[i];
2564 sqh->elink = data;
2565 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2566 }
2567 xfer->status = USBD_IN_PROGRESS;
2568 /* The ii is already on the examined list, just leave it. */
2569 } else {
2570 DPRINTFN(5,("uhci_device_intr_done: removing\n"));
2571 uhci_del_intr_info(ii);
2572 }
2573 }
2574
2575 /* Deallocate request data structures */
2576 void
2577 uhci_device_ctrl_done(usbd_xfer_handle xfer)
2578 {
2579 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2580 uhci_softc_t *sc = ii->sc;
2581 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2582
2583 #ifdef DIAGNOSTIC
2584 if (!(xfer->rqflags & URQ_REQUEST))
2585 panic("uhci_ctrl_done: not a request\n");
2586 #endif
2587
2588 uhci_del_intr_info(ii); /* remove from active list */
2589
2590 if (upipe->pipe.device->lowspeed)
2591 uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
2592 else
2593 uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh);
2594
2595 if (upipe->u.ctl.length != 0)
2596 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2597
2598 DPRINTFN(5, ("uhci_ctrl_done: length=%d\n", xfer->actlen));
2599 }
2600
2601 /* Deallocate request data structures */
2602 void
2603 uhci_device_bulk_done(usbd_xfer_handle xfer)
2604 {
2605 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2606 uhci_softc_t *sc = ii->sc;
2607 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2608
2609 uhci_del_intr_info(ii); /* remove from active list */
2610
2611 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2612
2613 uhci_free_std_chain(sc, ii->stdstart, 0);
2614
2615 DPRINTFN(5, ("uhci_bulk_done: length=%d\n", xfer->actlen));
2616 }
2617
2618 /* Add interrupt QH, called with vflock. */
2619 void
2620 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2621 {
2622 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2623 uhci_soft_qh_t *eqh;
2624
2625 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2626
2627 eqh = vf->eqh;
2628 sqh->hlink = eqh->hlink;
2629 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2630 eqh->hlink = sqh;
2631 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
2632 vf->eqh = sqh;
2633 vf->bandwidth++;
2634 }
2635
2636 /* Remove interrupt QH. */
2637 void
2638 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2639 {
2640 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2641 uhci_soft_qh_t *pqh;
2642
2643 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2644
2645 /* See comment in uhci_remove_ctrl() */
2646 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
2647 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2648 delay(UHCI_QH_REMOVE_DELAY);
2649 }
2650
2651 pqh = uhci_find_prev_qh(vf->hqh, sqh);
2652 pqh->hlink = sqh->hlink;
2653 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2654 delay(UHCI_QH_REMOVE_DELAY);
2655 if (vf->eqh == sqh)
2656 vf->eqh = pqh;
2657 vf->bandwidth--;
2658 }
2659
2660 usbd_status
2661 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
2662 {
2663 uhci_soft_qh_t *sqh;
2664 int i, npoll, s;
2665 u_int bestbw, bw, bestoffs, offs;
2666
2667 DPRINTFN(2, ("uhci_setintr: pipe=%p\n", upipe));
2668 if (ival == 0) {
2669 printf("uhci_setintr: 0 interval\n");
2670 return (USBD_INVAL);
2671 }
2672
2673 if (ival > UHCI_VFRAMELIST_COUNT)
2674 ival = UHCI_VFRAMELIST_COUNT;
2675 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2676 DPRINTFN(2, ("uhci_setintr: ival=%d npoll=%d\n", ival, npoll));
2677
2678 upipe->u.intr.npoll = npoll;
2679 upipe->u.intr.qhs =
2680 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
2681
2682 /*
2683 * Figure out which offset in the schedule that has most
2684 * bandwidth left over.
2685 */
2686 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2687 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2688 for (bw = i = 0; i < npoll; i++)
2689 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2690 if (bw < bestbw) {
2691 bestbw = bw;
2692 bestoffs = offs;
2693 }
2694 }
2695 DPRINTFN(1, ("uhci_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2696
2697 for(i = 0; i < npoll; i++) {
2698 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2699 sqh->elink = NULL;
2700 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2701 sqh->pos = MOD(i * ival + bestoffs);
2702 }
2703 #undef MOD
2704
2705 s = splusb();
2706 /* Enter QHs into the controller data structures. */
2707 for(i = 0; i < npoll; i++)
2708 uhci_add_intr(sc, upipe->u.intr.qhs[i]);
2709 splx(s);
2710
2711 DPRINTFN(5, ("uhci_setintr: returns %p\n", upipe));
2712 return (USBD_NORMAL_COMPLETION);
2713 }
2714
2715 /* Open a new pipe. */
2716 usbd_status
2717 uhci_open(usbd_pipe_handle pipe)
2718 {
2719 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2720 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2721 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2722 usbd_status err;
2723 int ival;
2724
2725 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2726 pipe, pipe->device->address,
2727 ed->bEndpointAddress, sc->sc_addr));
2728
2729 upipe->aborting = 0;
2730 upipe->nexttoggle = 0;
2731
2732 if (pipe->device->address == sc->sc_addr) {
2733 switch (ed->bEndpointAddress) {
2734 case USB_CONTROL_ENDPOINT:
2735 pipe->methods = &uhci_root_ctrl_methods;
2736 break;
2737 case UE_DIR_IN | UHCI_INTR_ENDPT:
2738 pipe->methods = &uhci_root_intr_methods;
2739 break;
2740 default:
2741 return (USBD_INVAL);
2742 }
2743 } else {
2744 switch (ed->bmAttributes & UE_XFERTYPE) {
2745 case UE_CONTROL:
2746 pipe->methods = &uhci_device_ctrl_methods;
2747 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2748 if (upipe->u.ctl.sqh == NULL)
2749 goto bad;
2750 upipe->u.ctl.setup = uhci_alloc_std(sc);
2751 if (upipe->u.ctl.setup == NULL) {
2752 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2753 goto bad;
2754 }
2755 upipe->u.ctl.stat = uhci_alloc_std(sc);
2756 if (upipe->u.ctl.stat == NULL) {
2757 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2758 uhci_free_std(sc, upipe->u.ctl.setup);
2759 goto bad;
2760 }
2761 err = usb_allocmem(&sc->sc_bus,
2762 sizeof(usb_device_request_t),
2763 0, &upipe->u.ctl.reqdma);
2764 if (err) {
2765 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2766 uhci_free_std(sc, upipe->u.ctl.setup);
2767 uhci_free_std(sc, upipe->u.ctl.stat);
2768 goto bad;
2769 }
2770 break;
2771 case UE_INTERRUPT:
2772 pipe->methods = &uhci_device_intr_methods;
2773 ival = pipe->interval;
2774 if (ival == USBD_DEFAULT_INTERVAL)
2775 ival = ed->bInterval;
2776 return (uhci_device_setintr(sc, upipe, ival));
2777 case UE_ISOCHRONOUS:
2778 pipe->methods = &uhci_device_isoc_methods;
2779 return (uhci_setup_isoc(pipe));
2780 case UE_BULK:
2781 pipe->methods = &uhci_device_bulk_methods;
2782 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2783 if (upipe->u.bulk.sqh == NULL)
2784 goto bad;
2785 break;
2786 }
2787 }
2788 return (USBD_NORMAL_COMPLETION);
2789
2790 bad:
2791 return (USBD_NOMEM);
2792 }
2793
2794 /*
2795 * Data structures and routines to emulate the root hub.
2796 */
2797 usb_device_descriptor_t uhci_devd = {
2798 USB_DEVICE_DESCRIPTOR_SIZE,
2799 UDESC_DEVICE, /* type */
2800 {0x00, 0x01}, /* USB version */
2801 UDCLASS_HUB, /* class */
2802 UDSUBCLASS_HUB, /* subclass */
2803 0, /* protocol */
2804 64, /* max packet */
2805 {0},{0},{0x00,0x01}, /* device id */
2806 1,2,0, /* string indicies */
2807 1 /* # of configurations */
2808 };
2809
2810 usb_config_descriptor_t uhci_confd = {
2811 USB_CONFIG_DESCRIPTOR_SIZE,
2812 UDESC_CONFIG,
2813 {USB_CONFIG_DESCRIPTOR_SIZE +
2814 USB_INTERFACE_DESCRIPTOR_SIZE +
2815 USB_ENDPOINT_DESCRIPTOR_SIZE},
2816 1,
2817 1,
2818 0,
2819 UC_SELF_POWERED,
2820 0 /* max power */
2821 };
2822
2823 usb_interface_descriptor_t uhci_ifcd = {
2824 USB_INTERFACE_DESCRIPTOR_SIZE,
2825 UDESC_INTERFACE,
2826 0,
2827 0,
2828 1,
2829 UICLASS_HUB,
2830 UISUBCLASS_HUB,
2831 0,
2832 0
2833 };
2834
2835 usb_endpoint_descriptor_t uhci_endpd = {
2836 USB_ENDPOINT_DESCRIPTOR_SIZE,
2837 UDESC_ENDPOINT,
2838 UE_DIR_IN | UHCI_INTR_ENDPT,
2839 UE_INTERRUPT,
2840 {8},
2841 255
2842 };
2843
2844 usb_hub_descriptor_t uhci_hubd_piix = {
2845 USB_HUB_DESCRIPTOR_SIZE,
2846 UDESC_HUB,
2847 2,
2848 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
2849 50, /* power on to power good */
2850 0,
2851 { 0x00 }, /* both ports are removable */
2852 };
2853
2854 int
2855 uhci_str(usb_string_descriptor_t *p, int l, char *s)
2856 {
2857 int i;
2858
2859 if (l == 0)
2860 return (0);
2861 p->bLength = 2 * strlen(s) + 2;
2862 if (l == 1)
2863 return (1);
2864 p->bDescriptorType = UDESC_STRING;
2865 l -= 2;
2866 for (i = 0; s[i] && l > 1; i++, l -= 2)
2867 USETW2(p->bString[i], 0, s[i]);
2868 return (2*i+2);
2869 }
2870
2871 /*
2872 * Simulate a hardware hub by handling all the necessary requests.
2873 */
2874 usbd_status
2875 uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
2876 {
2877 usbd_status err;
2878
2879 /* Insert last in queue. */
2880 err = usb_insert_transfer(xfer);
2881 if (err)
2882 return (err);
2883
2884 /*
2885 * Pipe isn't running (otherwise err would be USBD_INPROG),
2886 * so start it first.
2887 */
2888 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2889 }
2890
2891 usbd_status
2892 uhci_root_ctrl_start(usbd_xfer_handle xfer)
2893 {
2894 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2895 usb_device_request_t *req;
2896 void *buf = NULL;
2897 int port, x;
2898 int s, len, value, index, status, change, l, totlen = 0;
2899 usb_port_status_t ps;
2900 usbd_status err;
2901
2902 if (sc->sc_dying)
2903 return (USBD_IOERROR);
2904
2905 #ifdef DIAGNOSTIC
2906 if (!(xfer->rqflags & URQ_REQUEST))
2907 panic("uhci_root_ctrl_transfer: not a request\n");
2908 #endif
2909 req = &xfer->request;
2910
2911 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
2912 req->bmRequestType, req->bRequest));
2913
2914 len = UGETW(req->wLength);
2915 value = UGETW(req->wValue);
2916 index = UGETW(req->wIndex);
2917
2918 if (len != 0)
2919 buf = KERNADDR(&xfer->dmabuf);
2920
2921 #define C(x,y) ((x) | ((y) << 8))
2922 switch(C(req->bRequest, req->bmRequestType)) {
2923 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2924 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2925 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2926 /*
2927 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2928 * for the integrated root hub.
2929 */
2930 break;
2931 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2932 if (len > 0) {
2933 *(u_int8_t *)buf = sc->sc_conf;
2934 totlen = 1;
2935 }
2936 break;
2937 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2938 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
2939 switch(value >> 8) {
2940 case UDESC_DEVICE:
2941 if ((value & 0xff) != 0) {
2942 err = USBD_IOERROR;
2943 goto ret;
2944 }
2945 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2946 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
2947 memcpy(buf, &uhci_devd, l);
2948 break;
2949 case UDESC_CONFIG:
2950 if ((value & 0xff) != 0) {
2951 err = USBD_IOERROR;
2952 goto ret;
2953 }
2954 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2955 memcpy(buf, &uhci_confd, l);
2956 buf = (char *)buf + l;
2957 len -= l;
2958 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2959 totlen += l;
2960 memcpy(buf, &uhci_ifcd, l);
2961 buf = (char *)buf + l;
2962 len -= l;
2963 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2964 totlen += l;
2965 memcpy(buf, &uhci_endpd, l);
2966 break;
2967 case UDESC_STRING:
2968 if (len == 0)
2969 break;
2970 *(u_int8_t *)buf = 0;
2971 totlen = 1;
2972 switch (value & 0xff) {
2973 case 1: /* Vendor */
2974 totlen = uhci_str(buf, len, sc->sc_vendor);
2975 break;
2976 case 2: /* Product */
2977 totlen = uhci_str(buf, len, "UHCI root hub");
2978 break;
2979 }
2980 break;
2981 default:
2982 err = USBD_IOERROR;
2983 goto ret;
2984 }
2985 break;
2986 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2987 if (len > 0) {
2988 *(u_int8_t *)buf = 0;
2989 totlen = 1;
2990 }
2991 break;
2992 case C(UR_GET_STATUS, UT_READ_DEVICE):
2993 if (len > 1) {
2994 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2995 totlen = 2;
2996 }
2997 break;
2998 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2999 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3000 if (len > 1) {
3001 USETW(((usb_status_t *)buf)->wStatus, 0);
3002 totlen = 2;
3003 }
3004 break;
3005 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3006 if (value >= USB_MAX_DEVICES) {
3007 err = USBD_IOERROR;
3008 goto ret;
3009 }
3010 sc->sc_addr = value;
3011 break;
3012 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3013 if (value != 0 && value != 1) {
3014 err = USBD_IOERROR;
3015 goto ret;
3016 }
3017 sc->sc_conf = value;
3018 break;
3019 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3020 break;
3021 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3022 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3023 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3024 err = USBD_IOERROR;
3025 goto ret;
3026 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3027 break;
3028 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3029 break;
3030 /* Hub requests */
3031 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3032 break;
3033 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3034 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
3035 "port=%d feature=%d\n",
3036 index, value));
3037 if (index == 1)
3038 port = UHCI_PORTSC1;
3039 else if (index == 2)
3040 port = UHCI_PORTSC2;
3041 else {
3042 err = USBD_IOERROR;
3043 goto ret;
3044 }
3045 switch(value) {
3046 case UHF_PORT_ENABLE:
3047 x = UREAD2(sc, port);
3048 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
3049 break;
3050 case UHF_PORT_SUSPEND:
3051 x = UREAD2(sc, port);
3052 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
3053 break;
3054 case UHF_PORT_RESET:
3055 x = UREAD2(sc, port);
3056 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3057 break;
3058 case UHF_C_PORT_CONNECTION:
3059 x = UREAD2(sc, port);
3060 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
3061 break;
3062 case UHF_C_PORT_ENABLE:
3063 x = UREAD2(sc, port);
3064 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
3065 break;
3066 case UHF_C_PORT_OVER_CURRENT:
3067 x = UREAD2(sc, port);
3068 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
3069 break;
3070 case UHF_C_PORT_RESET:
3071 sc->sc_isreset = 0;
3072 err = USBD_NORMAL_COMPLETION;
3073 goto ret;
3074 case UHF_PORT_CONNECTION:
3075 case UHF_PORT_OVER_CURRENT:
3076 case UHF_PORT_POWER:
3077 case UHF_PORT_LOW_SPEED:
3078 case UHF_C_PORT_SUSPEND:
3079 default:
3080 err = USBD_IOERROR;
3081 goto ret;
3082 }
3083 break;
3084 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
3085 if (index == 1)
3086 port = UHCI_PORTSC1;
3087 else if (index == 2)
3088 port = UHCI_PORTSC2;
3089 else {
3090 err = USBD_IOERROR;
3091 goto ret;
3092 }
3093 if (len > 0) {
3094 *(u_int8_t *)buf =
3095 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
3096 UHCI_PORTSC_LS_SHIFT;
3097 totlen = 1;
3098 }
3099 break;
3100 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3101 if (value != 0) {
3102 err = USBD_IOERROR;
3103 goto ret;
3104 }
3105 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
3106 totlen = l;
3107 memcpy(buf, &uhci_hubd_piix, l);
3108 break;
3109 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3110 if (len != 4) {
3111 err = USBD_IOERROR;
3112 goto ret;
3113 }
3114 memset(buf, 0, len);
3115 totlen = len;
3116 break;
3117 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3118 if (index == 1)
3119 port = UHCI_PORTSC1;
3120 else if (index == 2)
3121 port = UHCI_PORTSC2;
3122 else {
3123 err = USBD_IOERROR;
3124 goto ret;
3125 }
3126 if (len != 4) {
3127 err = USBD_IOERROR;
3128 goto ret;
3129 }
3130 x = UREAD2(sc, port);
3131 status = change = 0;
3132 if (x & UHCI_PORTSC_CCS )
3133 status |= UPS_CURRENT_CONNECT_STATUS;
3134 if (x & UHCI_PORTSC_CSC )
3135 change |= UPS_C_CONNECT_STATUS;
3136 if (x & UHCI_PORTSC_PE )
3137 status |= UPS_PORT_ENABLED;
3138 if (x & UHCI_PORTSC_POEDC)
3139 change |= UPS_C_PORT_ENABLED;
3140 if (x & UHCI_PORTSC_OCI )
3141 status |= UPS_OVERCURRENT_INDICATOR;
3142 if (x & UHCI_PORTSC_OCIC )
3143 change |= UPS_C_OVERCURRENT_INDICATOR;
3144 if (x & UHCI_PORTSC_SUSP )
3145 status |= UPS_SUSPEND;
3146 if (x & UHCI_PORTSC_LSDA )
3147 status |= UPS_LOW_SPEED;
3148 status |= UPS_PORT_POWER;
3149 if (sc->sc_isreset)
3150 change |= UPS_C_PORT_RESET;
3151 USETW(ps.wPortStatus, status);
3152 USETW(ps.wPortChange, change);
3153 l = min(len, sizeof ps);
3154 memcpy(buf, &ps, l);
3155 totlen = l;
3156 break;
3157 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3158 err = USBD_IOERROR;
3159 goto ret;
3160 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3161 break;
3162 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3163 if (index == 1)
3164 port = UHCI_PORTSC1;
3165 else if (index == 2)
3166 port = UHCI_PORTSC2;
3167 else {
3168 err = USBD_IOERROR;
3169 goto ret;
3170 }
3171 switch(value) {
3172 case UHF_PORT_ENABLE:
3173 x = UREAD2(sc, port);
3174 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3175 break;
3176 case UHF_PORT_SUSPEND:
3177 x = UREAD2(sc, port);
3178 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
3179 break;
3180 case UHF_PORT_RESET:
3181 x = UREAD2(sc, port);
3182 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
3183 usb_delay_ms(&sc->sc_bus, 50); /*XXX USB v1.1 7.1.7.3 */
3184 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3185 delay(100);
3186 x = UREAD2(sc, port);
3187 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3188 usb_delay_ms(&sc->sc_bus, 10); /* XXX */
3189 DPRINTFN(3,("uhci port %d reset, status = 0x%04x\n",
3190 index, UREAD2(sc, port)));
3191 sc->sc_isreset = 1;
3192 break;
3193 case UHF_PORT_POWER:
3194 /* Pretend we turned on power */
3195 err = USBD_NORMAL_COMPLETION;
3196 goto ret;
3197 case UHF_C_PORT_CONNECTION:
3198 case UHF_C_PORT_ENABLE:
3199 case UHF_C_PORT_OVER_CURRENT:
3200 case UHF_PORT_CONNECTION:
3201 case UHF_PORT_OVER_CURRENT:
3202 case UHF_PORT_LOW_SPEED:
3203 case UHF_C_PORT_SUSPEND:
3204 case UHF_C_PORT_RESET:
3205 default:
3206 err = USBD_IOERROR;
3207 goto ret;
3208 }
3209 break;
3210 default:
3211 err = USBD_IOERROR;
3212 goto ret;
3213 }
3214 xfer->actlen = totlen;
3215 err = USBD_NORMAL_COMPLETION;
3216 ret:
3217 xfer->status = err;
3218 s = splusb();
3219 usb_transfer_complete(xfer);
3220 splx(s);
3221 return (USBD_IN_PROGRESS);
3222 }
3223
3224 /* Abort a root control request. */
3225 void
3226 uhci_root_ctrl_abort(usbd_xfer_handle xfer)
3227 {
3228 /* Nothing to do, all transfers are synchronous. */
3229 }
3230
3231 /* Close the root pipe. */
3232 void
3233 uhci_root_ctrl_close(usbd_pipe_handle pipe)
3234 {
3235 DPRINTF(("uhci_root_ctrl_close\n"));
3236 }
3237
3238 /* Abort a root interrupt request. */
3239 void
3240 uhci_root_intr_abort(usbd_xfer_handle xfer)
3241 {
3242 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3243
3244 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, xfer);
3245 sc->sc_intr_xfer = NULL;
3246
3247 if (xfer->pipe->intrxfer == xfer) {
3248 DPRINTF(("uhci_root_intr_abort: remove\n"));
3249 xfer->pipe->intrxfer = 0;
3250 }
3251 xfer->status = USBD_CANCELLED;
3252 #ifdef DIAGNOSTIC
3253 UXFER(xfer)->iinfo.isdone = 1;
3254 #endif
3255 usb_transfer_complete(xfer);
3256 }
3257
3258 usbd_status
3259 uhci_root_intr_transfer(usbd_xfer_handle xfer)
3260 {
3261 usbd_status err;
3262
3263 /* Insert last in queue. */
3264 err = usb_insert_transfer(xfer);
3265 if (err)
3266 return (err);
3267
3268 /* Pipe isn't running (otherwise err would be USBD_INPROG),
3269 * start first
3270 */
3271 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3272 }
3273
3274 /* Start a transfer on the root interrupt pipe */
3275 usbd_status
3276 uhci_root_intr_start(usbd_xfer_handle xfer)
3277 {
3278 usbd_pipe_handle pipe = xfer->pipe;
3279 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3280
3281 DPRINTFN(3, ("uhci_root_intr_transfer: xfer=%p len=%d flags=%d\n",
3282 xfer, xfer->length, xfer->flags));
3283
3284 if (sc->sc_dying)
3285 return (USBD_IOERROR);
3286
3287 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
3288 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
3289 sc->sc_intr_xfer = xfer;
3290 return (USBD_IN_PROGRESS);
3291 }
3292
3293 /* Close the root interrupt pipe. */
3294 void
3295 uhci_root_intr_close(usbd_pipe_handle pipe)
3296 {
3297 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3298
3299 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, sc->sc_intr_xfer);
3300 sc->sc_intr_xfer = NULL;
3301 DPRINTF(("uhci_root_intr_close\n"));
3302 }
3303