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