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