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