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