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