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