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