1 /*	$OpenBSD: uhci.c,v 1.38 2005/04/19 08:33:26 damien Exp $	*/
2 /*	$NetBSD: uhci.c,v 1.172 2003/02/23 04:19:26 simonb Exp $	*/
3 /*	$FreeBSD: src/sys/dev/usb/uhci.c,v 1.33 1999/11/17 22:33:41 n_hibma Exp $	*/
4 
5 /*
6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Lennart Augustsson (lennart@augustsson.net) at
11  * Carlstedt Research & Technology.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *        This product includes software developed by the NetBSD
24  *        Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 /*
43  * USB Universal Host Controller driver.
44  * Handles e.g. PIIX3 and PIIX4.
45  *
46  * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm
47  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
48  * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
49  *             ftp://download.intel.com/design/intarch/datashts/29056201.pdf
50  */
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #if defined(__NetBSD__) || defined(__OpenBSD__)
57 #include <sys/device.h>
58 #include <sys/select.h>
59 #elif defined(__FreeBSD__)
60 #include <sys/module.h>
61 #include <sys/bus.h>
62 #include <machine/bus_pio.h>
63 #if defined(DIAGNOSTIC) && defined(__i386__)
64 #include <machine/cpu.h>
65 #endif
66 #endif
67 #include <sys/proc.h>
68 #include <sys/queue.h>
69 
70 #include <machine/bus.h>
71 #include <machine/endian.h>
72 
73 #include <dev/usb/usb.h>
74 #include <dev/usb/usbdi.h>
75 #include <dev/usb/usbdivar.h>
76 #include <dev/usb/usb_mem.h>
77 #include <dev/usb/usb_quirks.h>
78 
79 #include <dev/usb/uhcireg.h>
80 #include <dev/usb/uhcivar.h>
81 
82 /* Use bandwidth reclamation for control transfers. Some devices choke on it. */
83 /*#define UHCI_CTL_LOOP */
84 
85 #if defined(__FreeBSD__)
86 #include <machine/clock.h>
87 
88 #define delay(d)		DELAY(d)
89 #endif
90 
91 #if defined(__OpenBSD__)
92 struct cfdriver uhci_cd = {
93 	NULL, "uhci", DV_DULL
94 };
95 #endif
96 
97 #ifdef UHCI_DEBUG
98 uhci_softc_t *thesc;
99 #define DPRINTF(x)	if (uhcidebug) printf x
100 #define DPRINTFN(n,x)	if (uhcidebug>(n)) printf x
101 int uhcidebug = 0;
102 int uhcinoloop = 0;
103 #ifndef __NetBSD__
104 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
105 #endif
106 #else
107 #define DPRINTF(x)
108 #define DPRINTFN(n,x)
109 #endif
110 
111 /*
112  * The UHCI controller is little endian, so on big endian machines
113  * the data stored in memory needs to be swapped.
114  */
115 #if defined(__FreeBSD__)
116 #if BYTE_ORDER == BIG_ENDIAN
117 #define htole32(x) (bswap32(x))
118 #define le32toh(x) (bswap32(x))
119 #else
120 #define htole32(x) (x)
121 #define le32toh(x) (x)
122 #endif
123 #endif
124 
125 struct uhci_pipe {
126 	struct usbd_pipe pipe;
127 	int nexttoggle;
128 
129 	u_char aborting;
130 	usbd_xfer_handle abortstart, abortend;
131 
132 	/* Info needed for different pipe kinds. */
133 	union {
134 		/* Control pipe */
135 		struct {
136 			uhci_soft_qh_t *sqh;
137 			usb_dma_t reqdma;
138 			uhci_soft_td_t *setup, *stat;
139 			u_int length;
140 		} ctl;
141 		/* Interrupt pipe */
142 		struct {
143 			int npoll;
144 			uhci_soft_qh_t **qhs;
145 		} intr;
146 		/* Bulk pipe */
147 		struct {
148 			uhci_soft_qh_t *sqh;
149 			u_int length;
150 			int isread;
151 		} bulk;
152 		/* Iso pipe */
153 		struct iso {
154 			uhci_soft_td_t **stds;
155 			int next, inuse;
156 		} iso;
157 	} u;
158 };
159 
160 Static void		uhci_globalreset(uhci_softc_t *);
161 Static usbd_status	uhci_portreset(uhci_softc_t*, int);
162 Static void		uhci_reset(uhci_softc_t *);
163 Static void		uhci_shutdown(void *v);
164 Static void		uhci_power(int, void *);
165 Static usbd_status	uhci_run(uhci_softc_t *, int run);
166 Static uhci_soft_td_t  *uhci_alloc_std(uhci_softc_t *);
167 Static void		uhci_free_std(uhci_softc_t *, uhci_soft_td_t *);
168 Static uhci_soft_qh_t  *uhci_alloc_sqh(uhci_softc_t *);
169 Static void		uhci_free_sqh(uhci_softc_t *, uhci_soft_qh_t *);
170 #if 0
171 Static void		uhci_enter_ctl_q(uhci_softc_t *, uhci_soft_qh_t *,
172 					 uhci_intr_info_t *);
173 Static void		uhci_exit_ctl_q(uhci_softc_t *, uhci_soft_qh_t *);
174 #endif
175 
176 Static void		uhci_free_std_chain(uhci_softc_t *,
177 					    uhci_soft_td_t *, uhci_soft_td_t *);
178 Static usbd_status	uhci_alloc_std_chain(struct uhci_pipe *,
179 			    uhci_softc_t *, int, int, u_int16_t, usb_dma_t *,
180 			    uhci_soft_td_t **, uhci_soft_td_t **);
181 Static void		uhci_poll_hub(void *);
182 Static void		uhci_waitintr(uhci_softc_t *, usbd_xfer_handle);
183 Static void		uhci_check_intr(uhci_softc_t *, uhci_intr_info_t *);
184 Static void		uhci_idone(uhci_intr_info_t *);
185 
186 Static void		uhci_abort_xfer(usbd_xfer_handle, usbd_status status);
187 
188 Static void		uhci_timeout(void *);
189 Static void		uhci_timeout_task(void *);
190 Static void		uhci_add_ls_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
191 Static void		uhci_add_hs_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
192 Static void		uhci_add_bulk(uhci_softc_t *, uhci_soft_qh_t *);
193 Static void		uhci_remove_ls_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
194 Static void		uhci_remove_hs_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
195 Static void		uhci_remove_bulk(uhci_softc_t *,uhci_soft_qh_t *);
196 Static int		uhci_str(usb_string_descriptor_t *, int, char *);
197 Static void		uhci_add_loop(uhci_softc_t *sc);
198 Static void		uhci_rem_loop(uhci_softc_t *sc);
199 
200 Static usbd_status	uhci_setup_isoc(usbd_pipe_handle pipe);
201 Static void		uhci_device_isoc_enter(usbd_xfer_handle);
202 
203 Static usbd_status	uhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
204 Static void		uhci_freem(struct usbd_bus *, usb_dma_t *);
205 
206 Static usbd_xfer_handle	uhci_allocx(struct usbd_bus *);
207 Static void		uhci_freex(struct usbd_bus *, usbd_xfer_handle);
208 
209 Static usbd_status	uhci_device_ctrl_transfer(usbd_xfer_handle);
210 Static usbd_status	uhci_device_ctrl_start(usbd_xfer_handle);
211 Static void		uhci_device_ctrl_abort(usbd_xfer_handle);
212 Static void		uhci_device_ctrl_close(usbd_pipe_handle);
213 Static void		uhci_device_ctrl_done(usbd_xfer_handle);
214 
215 Static usbd_status	uhci_device_intr_transfer(usbd_xfer_handle);
216 Static usbd_status	uhci_device_intr_start(usbd_xfer_handle);
217 Static void		uhci_device_intr_abort(usbd_xfer_handle);
218 Static void		uhci_device_intr_close(usbd_pipe_handle);
219 Static void		uhci_device_intr_done(usbd_xfer_handle);
220 
221 Static usbd_status	uhci_device_bulk_transfer(usbd_xfer_handle);
222 Static usbd_status	uhci_device_bulk_start(usbd_xfer_handle);
223 Static void		uhci_device_bulk_abort(usbd_xfer_handle);
224 Static void		uhci_device_bulk_close(usbd_pipe_handle);
225 Static void		uhci_device_bulk_done(usbd_xfer_handle);
226 
227 Static usbd_status	uhci_device_isoc_transfer(usbd_xfer_handle);
228 Static usbd_status	uhci_device_isoc_start(usbd_xfer_handle);
229 Static void		uhci_device_isoc_abort(usbd_xfer_handle);
230 Static void		uhci_device_isoc_close(usbd_pipe_handle);
231 Static void		uhci_device_isoc_done(usbd_xfer_handle);
232 
233 Static usbd_status	uhci_root_ctrl_transfer(usbd_xfer_handle);
234 Static usbd_status	uhci_root_ctrl_start(usbd_xfer_handle);
235 Static void		uhci_root_ctrl_abort(usbd_xfer_handle);
236 Static void		uhci_root_ctrl_close(usbd_pipe_handle);
237 Static void		uhci_root_ctrl_done(usbd_xfer_handle);
238 
239 Static usbd_status	uhci_root_intr_transfer(usbd_xfer_handle);
240 Static usbd_status	uhci_root_intr_start(usbd_xfer_handle);
241 Static void		uhci_root_intr_abort(usbd_xfer_handle);
242 Static void		uhci_root_intr_close(usbd_pipe_handle);
243 Static void		uhci_root_intr_done(usbd_xfer_handle);
244 
245 Static usbd_status	uhci_open(usbd_pipe_handle);
246 Static void		uhci_poll(struct usbd_bus *);
247 Static void		uhci_softintr(void *);
248 
249 Static usbd_status	uhci_device_request(usbd_xfer_handle xfer);
250 
251 Static void		uhci_add_intr(uhci_softc_t *, uhci_soft_qh_t *);
252 Static void		uhci_remove_intr(uhci_softc_t *, uhci_soft_qh_t *);
253 Static usbd_status	uhci_device_setintr(uhci_softc_t *sc,
254 			    struct uhci_pipe *pipe, int ival);
255 
256 Static void		uhci_device_clear_toggle(usbd_pipe_handle pipe);
257 Static void		uhci_noop(usbd_pipe_handle pipe);
258 
259 Static __inline__ uhci_soft_qh_t *uhci_find_prev_qh(uhci_soft_qh_t *,
260 						    uhci_soft_qh_t *);
261 
262 #ifdef UHCI_DEBUG
263 Static void		uhci_dump_all(uhci_softc_t *);
264 Static void		uhci_dumpregs(uhci_softc_t *);
265 Static void		uhci_dump_qhs(uhci_soft_qh_t *);
266 Static void		uhci_dump_qh(uhci_soft_qh_t *);
267 Static void		uhci_dump_tds(uhci_soft_td_t *);
268 Static void		uhci_dump_td(uhci_soft_td_t *);
269 Static void		uhci_dump_ii(uhci_intr_info_t *ii);
270 void			uhci_dump(void);
271 #endif
272 
273 #define UBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
274 			BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
275 #define UWRITE1(sc, r, x) \
276  do { UBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); \
277  } while (/*CONSTCOND*/0)
278 #define UWRITE2(sc, r, x) \
279  do { UBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); \
280  } while (/*CONSTCOND*/0)
281 #define UWRITE4(sc, r, x) \
282  do { UBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); \
283  } while (/*CONSTCOND*/0)
284 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
285 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
286 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
287 
288 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
289 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
290 
291 #define UHCI_RESET_TIMEOUT 100	/* ms, reset timeout */
292 
293 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
294 
295 #define UHCI_INTR_ENDPT 1
296 
297 struct usbd_bus_methods uhci_bus_methods = {
298 	uhci_open,
299 	uhci_softintr,
300 	uhci_poll,
301 	uhci_allocm,
302 	uhci_freem,
303 	uhci_allocx,
304 	uhci_freex,
305 };
306 
307 struct usbd_pipe_methods uhci_root_ctrl_methods = {
308 	uhci_root_ctrl_transfer,
309 	uhci_root_ctrl_start,
310 	uhci_root_ctrl_abort,
311 	uhci_root_ctrl_close,
312 	uhci_noop,
313 	uhci_root_ctrl_done,
314 };
315 
316 struct usbd_pipe_methods uhci_root_intr_methods = {
317 	uhci_root_intr_transfer,
318 	uhci_root_intr_start,
319 	uhci_root_intr_abort,
320 	uhci_root_intr_close,
321 	uhci_noop,
322 	uhci_root_intr_done,
323 };
324 
325 struct usbd_pipe_methods uhci_device_ctrl_methods = {
326 	uhci_device_ctrl_transfer,
327 	uhci_device_ctrl_start,
328 	uhci_device_ctrl_abort,
329 	uhci_device_ctrl_close,
330 	uhci_noop,
331 	uhci_device_ctrl_done,
332 };
333 
334 struct usbd_pipe_methods uhci_device_intr_methods = {
335 	uhci_device_intr_transfer,
336 	uhci_device_intr_start,
337 	uhci_device_intr_abort,
338 	uhci_device_intr_close,
339 	uhci_device_clear_toggle,
340 	uhci_device_intr_done,
341 };
342 
343 struct usbd_pipe_methods uhci_device_bulk_methods = {
344 	uhci_device_bulk_transfer,
345 	uhci_device_bulk_start,
346 	uhci_device_bulk_abort,
347 	uhci_device_bulk_close,
348 	uhci_device_clear_toggle,
349 	uhci_device_bulk_done,
350 };
351 
352 struct usbd_pipe_methods uhci_device_isoc_methods = {
353 	uhci_device_isoc_transfer,
354 	uhci_device_isoc_start,
355 	uhci_device_isoc_abort,
356 	uhci_device_isoc_close,
357 	uhci_noop,
358 	uhci_device_isoc_done,
359 };
360 
361 #define uhci_add_intr_info(sc, ii) \
362 	LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ii), list)
363 #define uhci_del_intr_info(ii) \
364 	do { \
365 		LIST_REMOVE((ii), list); \
366 		(ii)->list.le_prev = NULL; \
367 	} while (0)
368 #define uhci_active_intr_info(ii) ((ii)->list.le_prev != NULL)
369 
370 Static __inline__ uhci_soft_qh_t *
uhci_find_prev_qh(uhci_soft_qh_t * pqh,uhci_soft_qh_t * sqh)371 uhci_find_prev_qh(uhci_soft_qh_t *pqh, uhci_soft_qh_t *sqh)
372 {
373 	DPRINTFN(15,("uhci_find_prev_qh: pqh=%p sqh=%p\n", pqh, sqh));
374 
375 	for (; pqh->hlink != sqh; pqh = pqh->hlink) {
376 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
377 		if (le32toh(pqh->qh.qh_hlink) & UHCI_PTR_T) {
378 			printf("uhci_find_prev_qh: QH not found\n");
379 			return (NULL);
380 		}
381 #endif
382 	}
383 	return (pqh);
384 }
385 
386 void
uhci_globalreset(uhci_softc_t * sc)387 uhci_globalreset(uhci_softc_t *sc)
388 {
389 	UHCICMD(sc, UHCI_CMD_GRESET);	/* global reset */
390 	usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
391 	UHCICMD(sc, 0);			/* do nothing */
392 }
393 
394 usbd_status
uhci_init(uhci_softc_t * sc)395 uhci_init(uhci_softc_t *sc)
396 {
397 	usbd_status err;
398 	int i, j;
399 	uhci_soft_qh_t *clsqh, *chsqh, *bsqh, *sqh, *lsqh;
400 	uhci_soft_td_t *std;
401 
402 	DPRINTFN(1,("uhci_init: start\n"));
403 
404 #ifdef UHCI_DEBUG
405 	thesc = sc;
406 
407 	if (uhcidebug > 2)
408 		uhci_dumpregs(sc);
409 #endif
410 
411 	UWRITE2(sc, UHCI_INTR, 0);		/* disable interrupts */
412 	uhci_globalreset(sc);			/* reset the controller */
413 	uhci_reset(sc);
414 
415 	/* Allocate and initialize real frame array. */
416 	err = usb_allocmem(&sc->sc_bus,
417 		  UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
418 		  UHCI_FRAMELIST_ALIGN, &sc->sc_dma);
419 	if (err)
420 		return (err);
421 	sc->sc_pframes = KERNADDR(&sc->sc_dma, 0);
422 	UWRITE2(sc, UHCI_FRNUM, 0);		/* set frame number to 0 */
423 	UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0)); /* set frame list*/
424 
425 	/*
426 	 * Allocate a TD, inactive, that hangs from the last QH.
427 	 * This is to avoid a bug in the PIIX that makes it run berserk
428 	 * otherwise.
429 	 */
430 	std = uhci_alloc_std(sc);
431 	if (std == NULL)
432 		return (USBD_NOMEM);
433 	std->link.std = NULL;
434 	std->td.td_link = htole32(UHCI_PTR_T);
435 	std->td.td_status = htole32(0); /* inactive */
436 	std->td.td_token = htole32(0);
437 	std->td.td_buffer = htole32(0);
438 
439 	/* Allocate the dummy QH marking the end and used for looping the QHs.*/
440 	lsqh = uhci_alloc_sqh(sc);
441 	if (lsqh == NULL)
442 		return (USBD_NOMEM);
443 	lsqh->hlink = NULL;
444 	lsqh->qh.qh_hlink = htole32(UHCI_PTR_T);	/* end of QH chain */
445 	lsqh->elink = std;
446 	lsqh->qh.qh_elink = htole32(std->physaddr | UHCI_PTR_TD);
447 	sc->sc_last_qh = lsqh;
448 
449 	/* Allocate the dummy QH where bulk traffic will be queued. */
450 	bsqh = uhci_alloc_sqh(sc);
451 	if (bsqh == NULL)
452 		return (USBD_NOMEM);
453 	bsqh->hlink = lsqh;
454 	bsqh->qh.qh_hlink = htole32(lsqh->physaddr | UHCI_PTR_QH);
455 	bsqh->elink = NULL;
456 	bsqh->qh.qh_elink = htole32(UHCI_PTR_T);
457 	sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
458 
459 	/* Allocate dummy QH where high speed control traffic will be queued. */
460 	chsqh = uhci_alloc_sqh(sc);
461 	if (chsqh == NULL)
462 		return (USBD_NOMEM);
463 	chsqh->hlink = bsqh;
464 	chsqh->qh.qh_hlink = htole32(bsqh->physaddr | UHCI_PTR_QH);
465 	chsqh->elink = NULL;
466 	chsqh->qh.qh_elink = htole32(UHCI_PTR_T);
467 	sc->sc_hctl_start = sc->sc_hctl_end = chsqh;
468 
469 	/* Allocate dummy QH where control traffic will be queued. */
470 	clsqh = uhci_alloc_sqh(sc);
471 	if (clsqh == NULL)
472 		return (USBD_NOMEM);
473 	clsqh->hlink = bsqh;
474 	clsqh->qh.qh_hlink = htole32(chsqh->physaddr | UHCI_PTR_QH);
475 	clsqh->elink = NULL;
476 	clsqh->qh.qh_elink = htole32(UHCI_PTR_T);
477 	sc->sc_lctl_start = sc->sc_lctl_end = clsqh;
478 
479 	/*
480 	 * Make all (virtual) frame list pointers point to the interrupt
481 	 * queue heads and the interrupt queue heads at the control
482 	 * queue head and point the physical frame list to the virtual.
483 	 */
484 	for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
485 		std = uhci_alloc_std(sc);
486 		sqh = uhci_alloc_sqh(sc);
487 		if (std == NULL || sqh == NULL)
488 			return (USBD_NOMEM);
489 		std->link.sqh = sqh;
490 		std->td.td_link = htole32(sqh->physaddr | UHCI_PTR_QH);
491 		std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
492 		std->td.td_token = htole32(0);
493 		std->td.td_buffer = htole32(0);
494 		sqh->hlink = clsqh;
495 		sqh->qh.qh_hlink = htole32(clsqh->physaddr | UHCI_PTR_QH);
496 		sqh->elink = NULL;
497 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
498 		sc->sc_vframes[i].htd = std;
499 		sc->sc_vframes[i].etd = std;
500 		sc->sc_vframes[i].hqh = sqh;
501 		sc->sc_vframes[i].eqh = sqh;
502 		for (j = i;
503 		     j < UHCI_FRAMELIST_COUNT;
504 		     j += UHCI_VFRAMELIST_COUNT)
505 			sc->sc_pframes[j] = htole32(std->physaddr);
506 	}
507 
508 	LIST_INIT(&sc->sc_intrhead);
509 
510 	SIMPLEQ_INIT(&sc->sc_free_xfers);
511 
512 	usb_callout_init(sc->sc_poll_handle);
513 
514 	/* Set up the bus struct. */
515 	sc->sc_bus.methods = &uhci_bus_methods;
516 	sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
517 
518 #if defined(__NetBSD__) || defined(__OpenBSD__)
519 	sc->sc_suspend = PWR_RESUME;
520 	sc->sc_powerhook = powerhook_establish(uhci_power, sc);
521 	sc->sc_shutdownhook = shutdownhook_establish(uhci_shutdown, sc);
522 #endif
523 
524 	DPRINTFN(1,("uhci_init: enabling\n"));
525 	UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
526 		UHCI_INTR_IOCE | UHCI_INTR_SPIE);	/* enable interrupts */
527 
528 	UHCICMD(sc, UHCI_CMD_MAXP); /* Assume 64 byte packets at frame end */
529 
530 	return (uhci_run(sc, 1));		/* and here we go... */
531 }
532 
533 #if defined(__NetBSD__) || defined(__OpenBSD__)
534 int
uhci_activate(device_ptr_t self,enum devact act)535 uhci_activate(device_ptr_t self, enum devact act)
536 {
537 	struct uhci_softc *sc = (struct uhci_softc *)self;
538 	int rv = 0;
539 
540 	switch (act) {
541 	case DVACT_ACTIVATE:
542 		return (EOPNOTSUPP);
543 
544 	case DVACT_DEACTIVATE:
545 		if (sc->sc_child != NULL)
546 			rv = config_deactivate(sc->sc_child);
547 		break;
548 	}
549 	return (rv);
550 }
551 
552 int
uhci_detach(struct uhci_softc * sc,int flags)553 uhci_detach(struct uhci_softc *sc, int flags)
554 {
555 	usbd_xfer_handle xfer;
556 	int rv = 0;
557 
558 	if (sc->sc_child != NULL)
559 		rv = config_detach(sc->sc_child, flags);
560 
561 	if (rv != 0)
562 		return (rv);
563 
564 #if defined(__NetBSD__) || defined(__OpenBSD__)
565 	powerhook_disestablish(sc->sc_powerhook);
566 	shutdownhook_disestablish(sc->sc_shutdownhook);
567 #endif
568 
569 	/* Free all xfers associated with this HC. */
570 	for (;;) {
571 		xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
572 		if (xfer == NULL)
573 			break;
574 		SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
575 		free(xfer, M_USB);
576 	}
577 
578 	/* XXX free other data structures XXX */
579 
580 	return (rv);
581 }
582 #endif
583 
584 usbd_status
uhci_allocm(struct usbd_bus * bus,usb_dma_t * dma,u_int32_t size)585 uhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
586 {
587 	struct uhci_softc *sc = (struct uhci_softc *)bus;
588 	u_int32_t n;
589 
590 	/*
591 	 * XXX
592 	 * Since we are allocating a buffer we can assume that we will
593 	 * need TDs for it.  Since we don't want to allocate those from
594 	 * an interrupt context, we allocate them here and free them again.
595 	 * This is no guarantee that we'll get the TDs next time...
596 	 */
597 	n = size / 8;
598 	if (n > 16) {
599 		u_int32_t i;
600 		uhci_soft_td_t **stds;
601 		DPRINTF(("uhci_allocm: get %d TDs\n", n));
602 		stds = malloc(sizeof(uhci_soft_td_t *) * n, M_TEMP,
603 			      M_NOWAIT);
604 		if (stds == NULL)
605 			panic("uhci_allocm");
606 		memset(stds, 0, sizeof(uhci_soft_td_t *) * n);
607 		for(i=0; i < n; i++)
608 			stds[i] = uhci_alloc_std(sc);
609 		for(i=0; i < n; i++)
610 			if (stds[i] != NULL)
611 				uhci_free_std(sc, stds[i]);
612 		free(stds, M_TEMP);
613 	}
614 
615 	return (usb_allocmem(&sc->sc_bus, size, 0, dma));
616 }
617 
618 void
uhci_freem(struct usbd_bus * bus,usb_dma_t * dma)619 uhci_freem(struct usbd_bus *bus, usb_dma_t *dma)
620 {
621 	usb_freemem(&((struct uhci_softc *)bus)->sc_bus, dma);
622 }
623 
624 usbd_xfer_handle
uhci_allocx(struct usbd_bus * bus)625 uhci_allocx(struct usbd_bus *bus)
626 {
627 	struct uhci_softc *sc = (struct uhci_softc *)bus;
628 	usbd_xfer_handle xfer;
629 
630 	xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
631 	if (xfer != NULL) {
632 		SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
633 #ifdef DIAGNOSTIC
634 		if (xfer->busy_free != XFER_FREE) {
635 			printf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer,
636 			       xfer->busy_free);
637 		}
638 #endif
639 	} else {
640 		xfer = malloc(sizeof(struct uhci_xfer), M_USB, M_NOWAIT);
641 	}
642 	if (xfer != NULL) {
643 		memset(xfer, 0, sizeof (struct uhci_xfer));
644 		UXFER(xfer)->iinfo.sc = sc;
645 #ifdef DIAGNOSTIC
646 		UXFER(xfer)->iinfo.isdone = 1;
647 		xfer->busy_free = XFER_BUSY;
648 #endif
649 	}
650 	return (xfer);
651 }
652 
653 void
uhci_freex(struct usbd_bus * bus,usbd_xfer_handle xfer)654 uhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
655 {
656 	struct uhci_softc *sc = (struct uhci_softc *)bus;
657 
658 #ifdef DIAGNOSTIC
659 	if (xfer->busy_free != XFER_BUSY) {
660 		printf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer,
661 		       xfer->busy_free);
662 		return;
663 	}
664 	xfer->busy_free = XFER_FREE;
665 	if (!UXFER(xfer)->iinfo.isdone) {
666 		printf("uhci_freex: !isdone\n");
667 		return;
668 	}
669 #endif
670 	SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
671 }
672 
673 /*
674  * Shut down the controller when the system is going down.
675  */
676 void
uhci_shutdown(void * v)677 uhci_shutdown(void *v)
678 {
679 	uhci_softc_t *sc = v;
680 
681 	DPRINTF(("uhci_shutdown: stopping the HC\n"));
682 	uhci_run(sc, 0); /* stop the controller */
683 }
684 
685 /*
686  * Handle suspend/resume.
687  *
688  * We need to switch to polling mode here, because this routine is
689  * called from an interrupt context.  This is all right since we
690  * are almost suspended anyway.
691  */
692 void
uhci_power(int why,void * v)693 uhci_power(int why, void *v)
694 {
695 	uhci_softc_t *sc = v;
696 	int cmd;
697 	int s;
698 
699 	s = splhardusb();
700 	cmd = UREAD2(sc, UHCI_CMD);
701 
702 	DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
703 		 sc, why, sc->sc_suspend, cmd));
704 
705 	switch (why) {
706 	case PWR_SUSPEND:
707 	case PWR_STANDBY:
708 #ifdef UHCI_DEBUG
709 		if (uhcidebug > 2)
710 			uhci_dumpregs(sc);
711 #endif
712 		if (sc->sc_intr_xfer != NULL)
713 			usb_uncallout(sc->sc_poll_handle, uhci_poll_hub,
714 			    sc->sc_intr_xfer);
715 		sc->sc_bus.use_polling++;
716 		uhci_run(sc, 0); /* stop the controller */
717 
718 		/* save some state if BIOS doesn't */
719 		sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
720 		sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
721 
722 		UWRITE2(sc, UHCI_INTR, 0); /* disable intrs */
723 
724 		UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter global suspend */
725 		usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
726 		sc->sc_suspend = why;
727 		sc->sc_bus.use_polling--;
728 		DPRINTF(("uhci_power: cmd=0x%x\n", UREAD2(sc, UHCI_CMD)));
729 		break;
730 	case PWR_RESUME:
731 #ifdef DIAGNOSTIC
732 		if (sc->sc_suspend == PWR_RESUME)
733 			printf("uhci_power: weird, resume without suspend.\n");
734 #endif
735 		sc->sc_bus.use_polling++;
736 		sc->sc_suspend = why;
737 		if (cmd & UHCI_CMD_RS)
738 			uhci_run(sc, 0); /* in case BIOS has started it */
739 
740 		/* restore saved state */
741 		UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0));
742 		UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
743 		UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
744 
745 		UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force global resume */
746 		usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
747 		UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
748 		UHCICMD(sc, UHCI_CMD_MAXP);
749 		UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
750 			UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* re-enable intrs */
751 		uhci_run(sc, 1); /* and start traffic again */
752 		usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
753 		sc->sc_bus.use_polling--;
754 		if (sc->sc_intr_xfer != NULL)
755 			usb_callout(sc->sc_poll_handle, sc->sc_ival,
756 				    uhci_poll_hub, sc->sc_intr_xfer);
757 #ifdef UHCI_DEBUG
758 		if (uhcidebug > 2)
759 			uhci_dumpregs(sc);
760 #endif
761 		break;
762 #if defined(__NetBSD__)
763 	case PWR_SOFTSUSPEND:
764 	case PWR_SOFTSTANDBY:
765 	case PWR_SOFTRESUME:
766 		break;
767 #endif
768 	}
769 	splx(s);
770 }
771 
772 #ifdef UHCI_DEBUG
773 Static void
uhci_dumpregs(uhci_softc_t * sc)774 uhci_dumpregs(uhci_softc_t *sc)
775 {
776 	DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
777 		     "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
778 		     USBDEVNAME(sc->sc_bus.bdev),
779 		     UREAD2(sc, UHCI_CMD),
780 		     UREAD2(sc, UHCI_STS),
781 		     UREAD2(sc, UHCI_INTR),
782 		     UREAD2(sc, UHCI_FRNUM),
783 		     UREAD4(sc, UHCI_FLBASEADDR),
784 		     UREAD1(sc, UHCI_SOF),
785 		     UREAD2(sc, UHCI_PORTSC1),
786 		     UREAD2(sc, UHCI_PORTSC2)));
787 }
788 
789 void
uhci_dump_td(uhci_soft_td_t * p)790 uhci_dump_td(uhci_soft_td_t *p)
791 {
792 	char sbuf[128], sbuf2[128];
793 
794 	DPRINTFN(-1,("TD(%p) at %08lx = link=0x%08lx status=0x%08lx "
795 		     "token=0x%08lx buffer=0x%08lx\n",
796 		     p, (long)p->physaddr,
797 		     (long)le32toh(p->td.td_link),
798 		     (long)le32toh(p->td.td_status),
799 		     (long)le32toh(p->td.td_token),
800 		     (long)le32toh(p->td.td_buffer)));
801 
802 	bitmask_snprintf((u_int32_t)le32toh(p->td.td_link), "\20\1T\2Q\3VF",
803 			 sbuf, sizeof(sbuf));
804 	bitmask_snprintf((u_int32_t)le32toh(p->td.td_status),
805 			 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
806 			 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
807 			 sbuf2, sizeof(sbuf2));
808 
809 	DPRINTFN(-1,("  %s %s,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
810 		     "D=%d,maxlen=%d\n", sbuf, sbuf2,
811 		     UHCI_TD_GET_ERRCNT(le32toh(p->td.td_status)),
812 		     UHCI_TD_GET_ACTLEN(le32toh(p->td.td_status)),
813 		     UHCI_TD_GET_PID(le32toh(p->td.td_token)),
814 		     UHCI_TD_GET_DEVADDR(le32toh(p->td.td_token)),
815 		     UHCI_TD_GET_ENDPT(le32toh(p->td.td_token)),
816 		     UHCI_TD_GET_DT(le32toh(p->td.td_token)),
817 		     UHCI_TD_GET_MAXLEN(le32toh(p->td.td_token))));
818 }
819 
820 void
uhci_dump_qh(uhci_soft_qh_t * sqh)821 uhci_dump_qh(uhci_soft_qh_t *sqh)
822 {
823 	DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh,
824 	    (int)sqh->physaddr, le32toh(sqh->qh.qh_hlink),
825 	    le32toh(sqh->qh.qh_elink)));
826 }
827 
828 
829 void
uhci_dump(void)830 uhci_dump(void)
831 {
832 	uhci_dump_all(thesc);
833 }
834 
835 void
uhci_dump_all(uhci_softc_t * sc)836 uhci_dump_all(uhci_softc_t *sc)
837 {
838 	uhci_dumpregs(sc);
839 	printf("intrs=%d\n", sc->sc_bus.no_intrs);
840 	/*printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);*/
841 	uhci_dump_qh(sc->sc_lctl_start);
842 }
843 
844 
845 void
uhci_dump_qhs(uhci_soft_qh_t * sqh)846 uhci_dump_qhs(uhci_soft_qh_t *sqh)
847 {
848 	uhci_dump_qh(sqh);
849 
850 	/* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
851 	 * Traverses sideways first, then down.
852 	 *
853 	 * QH1
854 	 * QH2
855 	 * No QH
856 	 * TD2.1
857 	 * TD2.2
858 	 * TD1.1
859 	 * etc.
860 	 *
861 	 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
862 	 */
863 
864 
865 	if (sqh->hlink != NULL && !(le32toh(sqh->qh.qh_hlink) & UHCI_PTR_T))
866 		uhci_dump_qhs(sqh->hlink);
867 	else
868 		DPRINTF(("No QH\n"));
869 
870 	if (sqh->elink != NULL && !(le32toh(sqh->qh.qh_elink) & UHCI_PTR_T))
871 		uhci_dump_tds(sqh->elink);
872 	else
873 		DPRINTF(("No TD\n"));
874 }
875 
876 void
uhci_dump_tds(uhci_soft_td_t * std)877 uhci_dump_tds(uhci_soft_td_t *std)
878 {
879 	uhci_soft_td_t *td;
880 
881 	for(td = std; td != NULL; td = td->link.std) {
882 		uhci_dump_td(td);
883 
884 		/* Check whether the link pointer in this TD marks
885 		 * the link pointer as end of queue. This avoids
886 		 * printing the free list in case the queue/TD has
887 		 * already been moved there (seatbelt).
888 		 */
889 		if (le32toh(td->td.td_link) & UHCI_PTR_T ||
890 		    le32toh(td->td.td_link) == 0)
891 			break;
892 	}
893 }
894 
895 Static void
uhci_dump_ii(uhci_intr_info_t * ii)896 uhci_dump_ii(uhci_intr_info_t *ii)
897 {
898 	usbd_pipe_handle pipe;
899 	usb_endpoint_descriptor_t *ed;
900 	usbd_device_handle dev;
901 
902 #ifdef DIAGNOSTIC
903 #define DONE ii->isdone
904 #else
905 #define DONE 0
906 #endif
907         if (ii == NULL) {
908                 printf("ii NULL\n");
909                 return;
910         }
911         if (ii->xfer == NULL) {
912 		printf("ii %p: done=%d xfer=NULL\n",
913 		       ii, DONE);
914                 return;
915         }
916         pipe = ii->xfer->pipe;
917         if (pipe == NULL) {
918 		printf("ii %p: done=%d xfer=%p pipe=NULL\n",
919 		       ii, DONE, ii->xfer);
920                 return;
921 	}
922         if (pipe->endpoint == NULL) {
923 		printf("ii %p: done=%d xfer=%p pipe=%p pipe->endpoint=NULL\n",
924 		       ii, DONE, ii->xfer, pipe);
925                 return;
926 	}
927         if (pipe->device == NULL) {
928 		printf("ii %p: done=%d xfer=%p pipe=%p pipe->device=NULL\n",
929 		       ii, DONE, ii->xfer, pipe);
930                 return;
931 	}
932         ed = pipe->endpoint->edesc;
933         dev = pipe->device;
934 	printf("ii %p: done=%d xfer=%p dev=%p vid=0x%04x pid=0x%04x addr=%d pipe=%p ep=0x%02x attr=0x%02x\n",
935 	       ii, DONE, ii->xfer, dev,
936 	       UGETW(dev->ddesc.idVendor),
937 	       UGETW(dev->ddesc.idProduct),
938 	       dev->address, pipe,
939 	       ed->bEndpointAddress, ed->bmAttributes);
940 #undef DONE
941 }
942 
943 void uhci_dump_iis(struct uhci_softc *sc);
944 void
uhci_dump_iis(struct uhci_softc * sc)945 uhci_dump_iis(struct uhci_softc *sc)
946 {
947 	uhci_intr_info_t *ii;
948 
949 	printf("intr_info list:\n");
950 	for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
951 		uhci_dump_ii(ii);
952 }
953 
954 void iidump(void);
iidump(void)955 void iidump(void) { uhci_dump_iis(thesc); }
956 
957 #endif
958 
959 /*
960  * This routine is executed periodically and simulates interrupts
961  * from the root controller interrupt pipe for port status change.
962  */
963 void
uhci_poll_hub(void * addr)964 uhci_poll_hub(void *addr)
965 {
966 	usbd_xfer_handle xfer = addr;
967 	usbd_pipe_handle pipe = xfer->pipe;
968 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
969 	int s;
970 	u_char *p;
971 
972 	DPRINTFN(20, ("uhci_poll_hub\n"));
973 
974 	usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
975 
976 	p = KERNADDR(&xfer->dmabuf, 0);
977 	p[0] = 0;
978 	if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
979 		p[0] |= 1<<1;
980 	if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
981 		p[0] |= 1<<2;
982 	if (p[0] == 0)
983 		/* No change, try again in a while */
984 		return;
985 
986 	xfer->actlen = 1;
987 	xfer->status = USBD_NORMAL_COMPLETION;
988 	s = splusb();
989 	xfer->device->bus->intr_context++;
990 	usb_transfer_complete(xfer);
991 	xfer->device->bus->intr_context--;
992 	splx(s);
993 }
994 
995 void
uhci_root_intr_done(usbd_xfer_handle xfer)996 uhci_root_intr_done(usbd_xfer_handle xfer)
997 {
998 }
999 
1000 void
uhci_root_ctrl_done(usbd_xfer_handle xfer)1001 uhci_root_ctrl_done(usbd_xfer_handle xfer)
1002 {
1003 }
1004 
1005 /*
1006  * Let the last QH loop back to the high speed control transfer QH.
1007  * This is what intel calls "bandwidth reclamation" and improves
1008  * USB performance a lot for some devices.
1009  * If we are already looping, just count it.
1010  */
1011 void
uhci_add_loop(uhci_softc_t * sc)1012 uhci_add_loop(uhci_softc_t *sc) {
1013 #ifdef UHCI_DEBUG
1014 	if (uhcinoloop)
1015 		return;
1016 #endif
1017 	if (++sc->sc_loops == 1) {
1018 		DPRINTFN(5,("uhci_start_loop: add\n"));
1019 		/* Note, we don't loop back the soft pointer. */
1020 		sc->sc_last_qh->qh.qh_hlink =
1021 		    htole32(sc->sc_hctl_start->physaddr | UHCI_PTR_QH);
1022 	}
1023 }
1024 
1025 void
uhci_rem_loop(uhci_softc_t * sc)1026 uhci_rem_loop(uhci_softc_t *sc) {
1027 #ifdef UHCI_DEBUG
1028 	if (uhcinoloop)
1029 		return;
1030 #endif
1031 	if (--sc->sc_loops == 0) {
1032 		DPRINTFN(5,("uhci_end_loop: remove\n"));
1033 		sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T);
1034 	}
1035 }
1036 
1037 /* Add high speed control QH, called at splusb(). */
1038 void
uhci_add_hs_ctrl(uhci_softc_t * sc,uhci_soft_qh_t * sqh)1039 uhci_add_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1040 {
1041 	uhci_soft_qh_t *eqh;
1042 
1043 	SPLUSBCHECK;
1044 
1045 	DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
1046 	eqh = sc->sc_hctl_end;
1047 	sqh->hlink       = eqh->hlink;
1048 	sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1049 	eqh->hlink       = sqh;
1050 	eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1051 	sc->sc_hctl_end = sqh;
1052 #ifdef UHCI_CTL_LOOP
1053 	uhci_add_loop(sc);
1054 #endif
1055 }
1056 
1057 /* Remove high speed control QH, called at splusb(). */
1058 void
uhci_remove_hs_ctrl(uhci_softc_t * sc,uhci_soft_qh_t * sqh)1059 uhci_remove_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1060 {
1061 	uhci_soft_qh_t *pqh;
1062 
1063 	SPLUSBCHECK;
1064 
1065 	DPRINTFN(10, ("uhci_remove_hs_ctrl: sqh=%p\n", sqh));
1066 #ifdef UHCI_CTL_LOOP
1067 	uhci_rem_loop(sc);
1068 #endif
1069 	/*
1070 	 * The T bit should be set in the elink of the QH so that the HC
1071 	 * doesn't follow the pointer.  This condition may fail if the
1072 	 * the transferred packet was short so that the QH still points
1073 	 * at the last used TD.
1074 	 * In this case we set the T bit and wait a little for the HC
1075 	 * to stop looking at the TD.
1076 	 */
1077 	if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1078 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1079 		delay(UHCI_QH_REMOVE_DELAY);
1080 	}
1081 
1082 	pqh = uhci_find_prev_qh(sc->sc_hctl_start, sqh);
1083 	pqh->hlink = sqh->hlink;
1084 	pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1085 	delay(UHCI_QH_REMOVE_DELAY);
1086 	if (sc->sc_hctl_end == sqh)
1087 		sc->sc_hctl_end = pqh;
1088 }
1089 
1090 /* Add low speed control QH, called at splusb(). */
1091 void
uhci_add_ls_ctrl(uhci_softc_t * sc,uhci_soft_qh_t * sqh)1092 uhci_add_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1093 {
1094 	uhci_soft_qh_t *eqh;
1095 
1096 	SPLUSBCHECK;
1097 
1098 	DPRINTFN(10, ("uhci_add_ls_ctrl: sqh=%p\n", sqh));
1099 	eqh = sc->sc_lctl_end;
1100 	sqh->hlink = eqh->hlink;
1101 	sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1102 	eqh->hlink = sqh;
1103 	eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1104 	sc->sc_lctl_end = sqh;
1105 }
1106 
1107 /* Remove low speed control QH, called at splusb(). */
1108 void
uhci_remove_ls_ctrl(uhci_softc_t * sc,uhci_soft_qh_t * sqh)1109 uhci_remove_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1110 {
1111 	uhci_soft_qh_t *pqh;
1112 
1113 	SPLUSBCHECK;
1114 
1115 	DPRINTFN(10, ("uhci_remove_ls_ctrl: sqh=%p\n", sqh));
1116 	/* See comment in uhci_remove_hs_ctrl() */
1117 	if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1118 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1119 		delay(UHCI_QH_REMOVE_DELAY);
1120 	}
1121 	pqh = uhci_find_prev_qh(sc->sc_lctl_start, sqh);
1122 	pqh->hlink = sqh->hlink;
1123 	pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1124 	delay(UHCI_QH_REMOVE_DELAY);
1125 	if (sc->sc_lctl_end == sqh)
1126 		sc->sc_lctl_end = pqh;
1127 }
1128 
1129 /* Add bulk QH, called at splusb(). */
1130 void
uhci_add_bulk(uhci_softc_t * sc,uhci_soft_qh_t * sqh)1131 uhci_add_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1132 {
1133 	uhci_soft_qh_t *eqh;
1134 
1135 	SPLUSBCHECK;
1136 
1137 	DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
1138 	eqh = sc->sc_bulk_end;
1139 	sqh->hlink = eqh->hlink;
1140 	sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1141 	eqh->hlink = sqh;
1142 	eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1143 	sc->sc_bulk_end = sqh;
1144 	uhci_add_loop(sc);
1145 }
1146 
1147 /* Remove bulk QH, called at splusb(). */
1148 void
uhci_remove_bulk(uhci_softc_t * sc,uhci_soft_qh_t * sqh)1149 uhci_remove_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1150 {
1151 	uhci_soft_qh_t *pqh;
1152 
1153 	SPLUSBCHECK;
1154 
1155 	DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
1156 	uhci_rem_loop(sc);
1157 	/* See comment in uhci_remove_hs_ctrl() */
1158 	if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1159 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1160 		delay(UHCI_QH_REMOVE_DELAY);
1161 	}
1162 	pqh = uhci_find_prev_qh(sc->sc_bulk_start, sqh);
1163 	pqh->hlink       = sqh->hlink;
1164 	pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1165 	delay(UHCI_QH_REMOVE_DELAY);
1166 	if (sc->sc_bulk_end == sqh)
1167 		sc->sc_bulk_end = pqh;
1168 }
1169 
1170 Static int uhci_intr1(uhci_softc_t *);
1171 
1172 int
uhci_intr(void * arg)1173 uhci_intr(void *arg)
1174 {
1175 	uhci_softc_t *sc = arg;
1176 
1177 	if (sc->sc_dying)
1178 		return (0);
1179 
1180 	if (sc->sc_bus.use_polling) {
1181 #ifdef DIAGNOSTIC
1182 		DPRINTFN(16, ("uhci_intr: ignored interrupt while polling\n"));
1183 #endif
1184 		return (0);
1185 	}
1186 	return (uhci_intr1(sc));
1187 }
1188 
1189 int
uhci_intr1(uhci_softc_t * sc)1190 uhci_intr1(uhci_softc_t *sc)
1191 {
1192 	int status;
1193 	int ack;
1194 
1195 #ifdef UHCI_DEBUG
1196 	if (uhcidebug > 15) {
1197 		DPRINTF(("%s: uhci_intr1\n", USBDEVNAME(sc->sc_bus.bdev)));
1198 		uhci_dumpregs(sc);
1199 	}
1200 #endif
1201 
1202 	status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1203 	if (status == 0)	/* The interrupt was not for us. */
1204 		return (0);
1205 
1206 	if (sc->sc_suspend != PWR_RESUME) {
1207 		printf("%s: interrupt while not operating ignored\n",
1208 		       USBDEVNAME(sc->sc_bus.bdev));
1209 		UWRITE2(sc, UHCI_STS, status); /* acknowledge the ints */
1210 		return (0);
1211 	}
1212 
1213 	ack = 0;
1214 	if (status & UHCI_STS_USBINT)
1215 		ack |= UHCI_STS_USBINT;
1216 	if (status & UHCI_STS_USBEI)
1217 		ack |= UHCI_STS_USBEI;
1218 	if (status & UHCI_STS_RD) {
1219 		ack |= UHCI_STS_RD;
1220 #ifdef UHCI_DEBUG
1221 		printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1222 #endif
1223 	}
1224 	if (status & UHCI_STS_HSE) {
1225 		ack |= UHCI_STS_HSE;
1226 		printf("%s: host system error\n", USBDEVNAME(sc->sc_bus.bdev));
1227 	}
1228 	if (status & UHCI_STS_HCPE) {
1229 		ack |= UHCI_STS_HCPE;
1230 		printf("%s: host controller process error\n",
1231 		       USBDEVNAME(sc->sc_bus.bdev));
1232 	}
1233 	if (status & UHCI_STS_HCH) {
1234 		/* no acknowledge needed */
1235 		if (!sc->sc_dying) {
1236 			printf("%s: host controller halted\n",
1237 			    USBDEVNAME(sc->sc_bus.bdev));
1238 #ifdef UHCI_DEBUG
1239 			uhci_dump_all(sc);
1240 #endif
1241 		}
1242 		sc->sc_dying = 1;
1243 	}
1244 
1245 	if (!ack)
1246 		return (0);	/* nothing to acknowledge */
1247 	UWRITE2(sc, UHCI_STS, ack); /* acknowledge the ints */
1248 
1249 	sc->sc_bus.no_intrs++;
1250 	usb_schedsoftintr(&sc->sc_bus);
1251 
1252 	DPRINTFN(15, ("%s: uhci_intr: exit\n", USBDEVNAME(sc->sc_bus.bdev)));
1253 
1254 	return (1);
1255 }
1256 
1257 void
uhci_softintr(void * v)1258 uhci_softintr(void *v)
1259 {
1260 	uhci_softc_t *sc = v;
1261 	uhci_intr_info_t *ii, *nextii;
1262 
1263 	DPRINTFN(10,("%s: uhci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev),
1264 		     sc->sc_bus.intr_context));
1265 
1266 	sc->sc_bus.intr_context++;
1267 
1268 	/*
1269 	 * Interrupts on UHCI really suck.  When the host controller
1270 	 * interrupts because a transfer is completed there is no
1271 	 * way of knowing which transfer it was.  You can scan down
1272 	 * the TDs and QHs of the previous frame to limit the search,
1273 	 * but that assumes that the interrupt was not delayed by more
1274 	 * than 1 ms, which may not always be true (e.g. after debug
1275 	 * output on a slow console).
1276 	 * We scan all interrupt descriptors to see if any have
1277 	 * completed.
1278 	 */
1279 	for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = nextii) {
1280 		nextii = LIST_NEXT(ii, list);
1281 		uhci_check_intr(sc, ii);
1282 	}
1283 
1284 #ifdef USB_USE_SOFTINTR
1285 	if (sc->sc_softwake) {
1286 		sc->sc_softwake = 0;
1287 		wakeup(&sc->sc_softwake);
1288 	}
1289 #endif /* USB_USE_SOFTINTR */
1290 
1291 	sc->sc_bus.intr_context--;
1292 }
1293 
1294 /* Check for an interrupt. */
1295 void
uhci_check_intr(uhci_softc_t * sc,uhci_intr_info_t * ii)1296 uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii)
1297 {
1298 	uhci_soft_td_t *std, *lstd;
1299 	u_int32_t status;
1300 
1301 	DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
1302 #ifdef DIAGNOSTIC
1303 	if (ii == NULL) {
1304 		printf("uhci_check_intr: no ii? %p\n", ii);
1305 		return;
1306 	}
1307 #endif
1308 	if (ii->xfer->status == USBD_CANCELLED ||
1309 	    ii->xfer->status == USBD_TIMEOUT) {
1310 		DPRINTF(("uhci_check_intr: aborted xfer=%p\n", ii->xfer));
1311 		return;
1312 	}
1313 
1314 	if (ii->stdstart == NULL)
1315 		return;
1316 	lstd = ii->stdend;
1317 #ifdef DIAGNOSTIC
1318 	if (lstd == NULL) {
1319 		printf("uhci_check_intr: std==0\n");
1320 		return;
1321 	}
1322 #endif
1323 	/*
1324 	 * If the last TD is still active we need to check whether there
1325 	 * is a an error somewhere in the middle, or whether there was a
1326 	 * short packet (SPD and not ACTIVE).
1327 	 */
1328 	if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) {
1329 		DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii));
1330 		for (std = ii->stdstart; std != lstd; std = std->link.std) {
1331 			status = le32toh(std->td.td_status);
1332 			/* If there's an active TD the xfer isn't done. */
1333 			if (status & UHCI_TD_ACTIVE)
1334 				break;
1335 			/* Any kind of error makes the xfer done. */
1336 			if (status & UHCI_TD_STALLED)
1337 				goto done;
1338 			/* We want short packets, and it is short: it's done */
1339 			if ((status & UHCI_TD_SPD) &&
1340 			      UHCI_TD_GET_ACTLEN(status) <
1341 			      UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token)))
1342 				goto done;
1343 		}
1344 		DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n",
1345 			      ii, ii->stdstart));
1346 		return;
1347 	}
1348  done:
1349 	DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii));
1350 	usb_uncallout(ii->xfer->timeout_handle, uhci_timeout, ii);
1351 	uhci_idone(ii);
1352 }
1353 
1354 /* Called at splusb() */
1355 void
uhci_idone(uhci_intr_info_t * ii)1356 uhci_idone(uhci_intr_info_t *ii)
1357 {
1358 	usbd_xfer_handle xfer = ii->xfer;
1359 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1360 	uhci_soft_td_t *std;
1361 	u_int32_t status = 0, nstatus;
1362 	int actlen;
1363 
1364 	DPRINTFN(12, ("uhci_idone: ii=%p\n", ii));
1365 #ifdef DIAGNOSTIC
1366 	{
1367 		int s = splhigh();
1368 		if (ii->isdone) {
1369 			splx(s);
1370 #ifdef UHCI_DEBUG
1371 			printf("uhci_idone: ii is done!\n   ");
1372 			uhci_dump_ii(ii);
1373 #else
1374 			printf("uhci_idone: ii=%p is done!\n", ii);
1375 #endif
1376 			return;
1377 		}
1378 		ii->isdone = 1;
1379 		splx(s);
1380 	}
1381 #endif
1382 
1383 	if (xfer->nframes != 0) {
1384 		/* Isoc transfer, do things differently. */
1385 		uhci_soft_td_t **stds = upipe->u.iso.stds;
1386 		int i, n, nframes, len;
1387 
1388 		DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
1389 
1390 		nframes = xfer->nframes;
1391 		actlen = 0;
1392 		n = UXFER(xfer)->curframe;
1393 		for (i = 0; i < nframes; i++) {
1394 			std = stds[n];
1395 #ifdef UHCI_DEBUG
1396 			if (uhcidebug > 5) {
1397 				DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
1398 				uhci_dump_td(std);
1399 			}
1400 #endif
1401 			if (++n >= UHCI_VFRAMELIST_COUNT)
1402 				n = 0;
1403 			status = le32toh(std->td.td_status);
1404 			len = UHCI_TD_GET_ACTLEN(status);
1405 			xfer->frlengths[i] = len;
1406 			actlen += len;
1407 		}
1408 		upipe->u.iso.inuse -= nframes;
1409 		xfer->actlen = actlen;
1410 		xfer->status = USBD_NORMAL_COMPLETION;
1411 		goto end;
1412 	}
1413 
1414 #ifdef UHCI_DEBUG
1415 	DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1416 		      ii, xfer, upipe));
1417 	if (uhcidebug > 10)
1418 		uhci_dump_tds(ii->stdstart);
1419 #endif
1420 
1421 	/* The transfer is done, compute actual length and status. */
1422 	actlen = 0;
1423 	for (std = ii->stdstart; std != NULL; std = std->link.std) {
1424 		nstatus = le32toh(std->td.td_status);
1425 		if (nstatus & UHCI_TD_ACTIVE)
1426 			break;
1427 
1428 		status = nstatus;
1429 		if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
1430 		    UHCI_TD_PID_SETUP)
1431 			actlen += UHCI_TD_GET_ACTLEN(status);
1432 		else {
1433 			/*
1434 			 * UHCI will report CRCTO in addition to a STALL or NAK
1435 			 * for a SETUP transaction.  See section 3.2.2, "TD
1436 			 * CONTROL AND STATUS".
1437 			 */
1438 			if (status & (UHCI_TD_STALLED | UHCI_TD_NAK))
1439 				status &= ~UHCI_TD_CRCTO;
1440 		}
1441 	}
1442 	/* If there are left over TDs we need to update the toggle. */
1443 	if (std != NULL)
1444 		upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
1445 
1446 	status &= UHCI_TD_ERROR;
1447 	DPRINTFN(10, ("uhci_idone: actlen=%d, status=0x%x\n",
1448 		      actlen, status));
1449 	xfer->actlen = actlen;
1450 	if (status != 0) {
1451 #ifdef UHCI_DEBUG
1452 		char sbuf[128];
1453 
1454 		bitmask_snprintf((u_int32_t)status,
1455 				 "\20\22BITSTUFF\23CRCTO\24NAK\25"
1456 				 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
1457 				 sbuf, sizeof(sbuf));
1458 
1459 		DPRINTFN((status == UHCI_TD_STALLED)*10,
1460 			 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1461 			  "status 0x%s\n",
1462 			  xfer->pipe->device->address,
1463 			  xfer->pipe->endpoint->edesc->bEndpointAddress,
1464 			  sbuf));
1465 #endif
1466 
1467 		if (status == UHCI_TD_STALLED)
1468 			xfer->status = USBD_STALLED;
1469 		else
1470 			xfer->status = USBD_IOERROR; /* more info XXX */
1471 	} else {
1472 		xfer->status = USBD_NORMAL_COMPLETION;
1473 	}
1474 
1475  end:
1476 	usb_transfer_complete(xfer);
1477 	DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii));
1478 }
1479 
1480 /*
1481  * Called when a request does not complete.
1482  */
1483 void
uhci_timeout(void * addr)1484 uhci_timeout(void *addr)
1485 {
1486 	uhci_intr_info_t *ii = addr;
1487 	struct uhci_xfer *uxfer = UXFER(ii->xfer);
1488 	struct uhci_pipe *upipe = (struct uhci_pipe *)uxfer->xfer.pipe;
1489 	uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1490 
1491 	DPRINTF(("uhci_timeout: uxfer=%p\n", uxfer));
1492 
1493 	if (sc->sc_dying) {
1494 		uhci_abort_xfer(&uxfer->xfer, USBD_TIMEOUT);
1495 		return;
1496 	}
1497 
1498 	/* Execute the abort in a process context. */
1499 	usb_init_task(&uxfer->abort_task, uhci_timeout_task, ii->xfer);
1500 	usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task);
1501 }
1502 
1503 void
uhci_timeout_task(void * addr)1504 uhci_timeout_task(void *addr)
1505 {
1506 	usbd_xfer_handle xfer = addr;
1507 	int s;
1508 
1509 	DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer));
1510 
1511 	s = splusb();
1512 	uhci_abort_xfer(xfer, USBD_TIMEOUT);
1513 	splx(s);
1514 }
1515 
1516 /*
1517  * Wait here until controller claims to have an interrupt.
1518  * Then call uhci_intr and return.  Use timeout to avoid waiting
1519  * too long.
1520  * Only used during boot when interrupts are not enabled yet.
1521  */
1522 void
uhci_waitintr(uhci_softc_t * sc,usbd_xfer_handle xfer)1523 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
1524 {
1525 	int timo = xfer->timeout;
1526 	uhci_intr_info_t *ii;
1527 
1528 	DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1529 
1530 	xfer->status = USBD_IN_PROGRESS;
1531 	for (; timo >= 0; timo--) {
1532 		usb_delay_ms(&sc->sc_bus, 1);
1533 		DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1534 		if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS) {
1535 			uhci_intr1(sc);
1536 			if (xfer->status != USBD_IN_PROGRESS)
1537 				return;
1538 		}
1539 	}
1540 
1541 	/* Timeout */
1542 	DPRINTF(("uhci_waitintr: timeout\n"));
1543 	for (ii = LIST_FIRST(&sc->sc_intrhead);
1544 	     ii != NULL && ii->xfer != xfer;
1545 	     ii = LIST_NEXT(ii, list))
1546 		;
1547 #ifdef DIAGNOSTIC
1548 	if (ii == NULL)
1549 		panic("uhci_waitintr: lost intr_info");
1550 #endif
1551 	uhci_idone(ii);
1552 }
1553 
1554 void
uhci_poll(struct usbd_bus * bus)1555 uhci_poll(struct usbd_bus *bus)
1556 {
1557 	uhci_softc_t *sc = (uhci_softc_t *)bus;
1558 
1559 	if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1560 		uhci_intr1(sc);
1561 }
1562 
1563 void
uhci_reset(uhci_softc_t * sc)1564 uhci_reset(uhci_softc_t *sc)
1565 {
1566 	int n;
1567 
1568 	UHCICMD(sc, UHCI_CMD_HCRESET);
1569 	/* The reset bit goes low when the controller is done. */
1570 	for (n = 0; n < UHCI_RESET_TIMEOUT &&
1571 		    (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1572 		usb_delay_ms(&sc->sc_bus, 1);
1573 	if (n >= UHCI_RESET_TIMEOUT)
1574 		printf("%s: controller did not reset\n",
1575 		       USBDEVNAME(sc->sc_bus.bdev));
1576 }
1577 
1578 usbd_status
uhci_run(uhci_softc_t * sc,int run)1579 uhci_run(uhci_softc_t *sc, int run)
1580 {
1581 	int s, n, running;
1582 	u_int16_t cmd;
1583 
1584 	run = run != 0;
1585 	s = splhardusb();
1586 	DPRINTF(("uhci_run: setting run=%d\n", run));
1587 	cmd = UREAD2(sc, UHCI_CMD);
1588 	if (run)
1589 		cmd |= UHCI_CMD_RS;
1590 	else
1591 		cmd &= ~UHCI_CMD_RS;
1592 	UHCICMD(sc, cmd);
1593 	for(n = 0; n < 10; n++) {
1594 		running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1595 		/* return when we've entered the state we want */
1596 		if (run == running) {
1597 			splx(s);
1598 			DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1599 				 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1600 			return (USBD_NORMAL_COMPLETION);
1601 		}
1602 		usb_delay_ms(&sc->sc_bus, 1);
1603 	}
1604 	splx(s);
1605 	printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
1606 	       run ? "start" : "stop");
1607 	return (USBD_IOERROR);
1608 }
1609 
1610 /*
1611  * Memory management routines.
1612  *  uhci_alloc_std allocates TDs
1613  *  uhci_alloc_sqh allocates QHs
1614  * These two routines do their own free list management,
1615  * partly for speed, partly because allocating DMAable memory
1616  * has page size granularaity so much memory would be wasted if
1617  * only one TD/QH (32 bytes) was placed in each allocated chunk.
1618  */
1619 
1620 uhci_soft_td_t *
uhci_alloc_std(uhci_softc_t * sc)1621 uhci_alloc_std(uhci_softc_t *sc)
1622 {
1623 	uhci_soft_td_t *std;
1624 	usbd_status err;
1625 	int i, offs;
1626 	usb_dma_t dma;
1627 
1628 	if (sc->sc_freetds == NULL) {
1629 		DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1630 		err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1631 			  UHCI_TD_ALIGN, &dma);
1632 		if (err)
1633 			return (0);
1634 		for(i = 0; i < UHCI_STD_CHUNK; i++) {
1635 			offs = i * UHCI_STD_SIZE;
1636 			std = KERNADDR(&dma, offs);
1637 			std->physaddr = DMAADDR(&dma, offs);
1638 			std->link.std = sc->sc_freetds;
1639 			sc->sc_freetds = std;
1640 		}
1641 	}
1642 	std = sc->sc_freetds;
1643 	sc->sc_freetds = std->link.std;
1644 	memset(&std->td, 0, sizeof(uhci_td_t));
1645 	return std;
1646 }
1647 
1648 void
uhci_free_std(uhci_softc_t * sc,uhci_soft_td_t * std)1649 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
1650 {
1651 #ifdef DIAGNOSTIC
1652 #define TD_IS_FREE 0x12345678
1653 	if (le32toh(std->td.td_token) == TD_IS_FREE) {
1654 		printf("uhci_free_std: freeing free TD %p\n", std);
1655 		return;
1656 	}
1657 	std->td.td_token = htole32(TD_IS_FREE);
1658 #endif
1659 	std->link.std = sc->sc_freetds;
1660 	sc->sc_freetds = std;
1661 }
1662 
1663 uhci_soft_qh_t *
uhci_alloc_sqh(uhci_softc_t * sc)1664 uhci_alloc_sqh(uhci_softc_t *sc)
1665 {
1666 	uhci_soft_qh_t *sqh;
1667 	usbd_status err;
1668 	int i, offs;
1669 	usb_dma_t dma;
1670 
1671 	if (sc->sc_freeqhs == NULL) {
1672 		DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1673 		err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1674 			  UHCI_QH_ALIGN, &dma);
1675 		if (err)
1676 			return (0);
1677 		for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1678 			offs = i * UHCI_SQH_SIZE;
1679 			sqh = KERNADDR(&dma, offs);
1680 			sqh->physaddr = DMAADDR(&dma, offs);
1681 			sqh->hlink = sc->sc_freeqhs;
1682 			sc->sc_freeqhs = sqh;
1683 		}
1684 	}
1685 	sqh = sc->sc_freeqhs;
1686 	sc->sc_freeqhs = sqh->hlink;
1687 	memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1688 	return (sqh);
1689 }
1690 
1691 void
uhci_free_sqh(uhci_softc_t * sc,uhci_soft_qh_t * sqh)1692 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1693 {
1694 	sqh->hlink = sc->sc_freeqhs;
1695 	sc->sc_freeqhs = sqh;
1696 }
1697 
1698 void
uhci_free_std_chain(uhci_softc_t * sc,uhci_soft_td_t * std,uhci_soft_td_t * stdend)1699 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
1700 		    uhci_soft_td_t *stdend)
1701 {
1702 	uhci_soft_td_t *p;
1703 
1704 	for (; std != stdend; std = p) {
1705 		p = std->link.std;
1706 		uhci_free_std(sc, std);
1707 	}
1708 }
1709 
1710 usbd_status
uhci_alloc_std_chain(struct uhci_pipe * upipe,uhci_softc_t * sc,int len,int rd,u_int16_t flags,usb_dma_t * dma,uhci_soft_td_t ** sp,uhci_soft_td_t ** ep)1711 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len,
1712 		     int rd, u_int16_t flags, usb_dma_t *dma,
1713 		     uhci_soft_td_t **sp, uhci_soft_td_t **ep)
1714 {
1715 	uhci_soft_td_t *p, *lastp;
1716 	uhci_physaddr_t lastlink;
1717 	int i, ntd, l, tog, maxp;
1718 	u_int32_t status;
1719 	int addr = upipe->pipe.device->address;
1720 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1721 
1722 	DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d "
1723 		      "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
1724 		      upipe->pipe.device->speed, flags));
1725 	maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1726 	if (maxp == 0) {
1727 		printf("uhci_alloc_std_chain: maxp=0\n");
1728 		return (USBD_INVAL);
1729 	}
1730 	ntd = (len + maxp - 1) / maxp;
1731 	if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
1732 		ntd++;
1733 	DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1734 	if (ntd == 0) {
1735 		*sp = *ep = 0;
1736 		DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
1737 		return (USBD_NORMAL_COMPLETION);
1738 	}
1739 	tog = upipe->nexttoggle;
1740 	if (ntd % 2 == 0)
1741 		tog ^= 1;
1742 	upipe->nexttoggle = tog ^ 1;
1743 	lastp = NULL;
1744 	lastlink = UHCI_PTR_T;
1745 	ntd--;
1746 	status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1747 	if (upipe->pipe.device->speed == USB_SPEED_LOW)
1748 		status |= UHCI_TD_LS;
1749 	if (flags & USBD_SHORT_XFER_OK)
1750 		status |= UHCI_TD_SPD;
1751 	for (i = ntd; i >= 0; i--) {
1752 		p = uhci_alloc_std(sc);
1753 		if (p == NULL) {
1754 			uhci_free_std_chain(sc, lastp, NULL);
1755 			return (USBD_NOMEM);
1756 		}
1757 		p->link.std = lastp;
1758 		p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD);
1759 		lastp = p;
1760 		lastlink = p->physaddr;
1761 		p->td.td_status = htole32(status);
1762 		if (i == ntd) {
1763 			/* last TD */
1764 			l = len % maxp;
1765 			if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
1766 				l = maxp;
1767 			*ep = p;
1768 		} else
1769 			l = maxp;
1770 		p->td.td_token =
1771 		    htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1772 				 UHCI_TD_OUT(l, endpt, addr, tog));
1773 		p->td.td_buffer = htole32(DMAADDR(dma, i * maxp));
1774 		tog ^= 1;
1775 	}
1776 	*sp = lastp;
1777 	DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1778 		      upipe->nexttoggle));
1779 	return (USBD_NORMAL_COMPLETION);
1780 }
1781 
1782 void
uhci_device_clear_toggle(usbd_pipe_handle pipe)1783 uhci_device_clear_toggle(usbd_pipe_handle pipe)
1784 {
1785 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1786 	upipe->nexttoggle = 0;
1787 }
1788 
1789 void
uhci_noop(usbd_pipe_handle pipe)1790 uhci_noop(usbd_pipe_handle pipe)
1791 {
1792 }
1793 
1794 usbd_status
uhci_device_bulk_transfer(usbd_xfer_handle xfer)1795 uhci_device_bulk_transfer(usbd_xfer_handle xfer)
1796 {
1797 	usbd_status err;
1798 
1799 	/* Insert last in queue. */
1800 	err = usb_insert_transfer(xfer);
1801 	if (err)
1802 		return (err);
1803 
1804 	/*
1805 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
1806 	 * so start it first.
1807 	 */
1808 	return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1809 }
1810 
1811 usbd_status
uhci_device_bulk_start(usbd_xfer_handle xfer)1812 uhci_device_bulk_start(usbd_xfer_handle xfer)
1813 {
1814 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1815 	usbd_device_handle dev = upipe->pipe.device;
1816 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1817 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1818 	uhci_soft_td_t *data, *dataend;
1819 	uhci_soft_qh_t *sqh;
1820 	usbd_status err;
1821 	int len, isread, endpt;
1822 	int s;
1823 
1824 	DPRINTFN(3, ("uhci_device_bulk_start: xfer=%p len=%d flags=%d ii=%p\n",
1825 		     xfer, xfer->length, xfer->flags, ii));
1826 
1827 	if (sc->sc_dying)
1828 		return (USBD_IOERROR);
1829 
1830 #ifdef DIAGNOSTIC
1831 	if (xfer->rqflags & URQ_REQUEST)
1832 		panic("uhci_device_bulk_transfer: a request");
1833 #endif
1834 
1835 	len = xfer->length;
1836 	endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1837 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1838 	sqh = upipe->u.bulk.sqh;
1839 
1840 	upipe->u.bulk.isread = isread;
1841 	upipe->u.bulk.length = len;
1842 
1843 	err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
1844 				   &xfer->dmabuf, &data, &dataend);
1845 	if (err)
1846 		return (err);
1847 	dataend->td.td_status |= htole32(UHCI_TD_IOC);
1848 
1849 #ifdef UHCI_DEBUG
1850 	if (uhcidebug > 8) {
1851 		DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1852 		uhci_dump_tds(data);
1853 	}
1854 #endif
1855 
1856 	/* Set up interrupt info. */
1857 	ii->xfer = xfer;
1858 	ii->stdstart = data;
1859 	ii->stdend = dataend;
1860 #ifdef DIAGNOSTIC
1861 	if (!ii->isdone) {
1862 		printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
1863 	}
1864 	ii->isdone = 0;
1865 #endif
1866 
1867 	sqh->elink = data;
1868 	sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
1869 
1870 	s = splusb();
1871 	uhci_add_bulk(sc, sqh);
1872 	uhci_add_intr_info(sc, ii);
1873 
1874 	if (xfer->timeout && !sc->sc_bus.use_polling) {
1875 		usb_callout(xfer->timeout_handle, mstohz(xfer->timeout),
1876 			    uhci_timeout, ii);
1877 	}
1878 	xfer->status = USBD_IN_PROGRESS;
1879 	splx(s);
1880 
1881 #ifdef UHCI_DEBUG
1882 	if (uhcidebug > 10) {
1883 		DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1884 		uhci_dump_tds(data);
1885 	}
1886 #endif
1887 
1888 	if (sc->sc_bus.use_polling)
1889 		uhci_waitintr(sc, xfer);
1890 
1891 	return (USBD_IN_PROGRESS);
1892 }
1893 
1894 /* Abort a device bulk request. */
1895 void
uhci_device_bulk_abort(usbd_xfer_handle xfer)1896 uhci_device_bulk_abort(usbd_xfer_handle xfer)
1897 {
1898 	DPRINTF(("uhci_device_bulk_abort:\n"));
1899 	uhci_abort_xfer(xfer, USBD_CANCELLED);
1900 }
1901 
1902 /*
1903  * Abort a device request.
1904  * If this routine is called at splusb() it guarantees that the request
1905  * will be removed from the hardware scheduling and that the callback
1906  * for it will be called with USBD_CANCELLED status.
1907  * It's impossible to guarantee that the requested transfer will not
1908  * have happened since the hardware runs concurrently.
1909  * If the transaction has already happened we rely on the ordinary
1910  * interrupt processing to process it.
1911  */
1912 void
uhci_abort_xfer(usbd_xfer_handle xfer,usbd_status status)1913 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1914 {
1915 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1916 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1917 	uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1918 	uhci_soft_td_t *std;
1919 	int s;
1920 
1921 	DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1922 
1923 	if (sc->sc_dying) {
1924 		/* If we're dying, just do the software part. */
1925 		s = splusb();
1926 		xfer->status = status;	/* make software ignore it */
1927 		usb_uncallout(xfer->timeout_handle, uhci_timeout, xfer);
1928 		usb_transfer_complete(xfer);
1929 		splx(s);
1930 	}
1931 
1932 	if (xfer->device->bus->intr_context || !curproc)
1933 		panic("uhci_abort_xfer: not in process context");
1934 
1935 	/*
1936 	 * Step 1: Make interrupt routine and hardware ignore xfer.
1937 	 */
1938 	s = splusb();
1939 	xfer->status = status;	/* make software ignore it */
1940 	usb_uncallout(xfer->timeout_handle, uhci_timeout, ii);
1941 	DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii));
1942 	for (std = ii->stdstart; std != NULL; std = std->link.std)
1943 		std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1944 	splx(s);
1945 
1946 	/*
1947 	 * Step 2: Wait until we know hardware has finished any possible
1948 	 * use of the xfer.  Also make sure the soft interrupt routine
1949 	 * has run.
1950 	 */
1951 	usb_delay_ms(upipe->pipe.device->bus, 2); /* Hardware finishes in 1ms */
1952 	s = splusb();
1953 #ifdef USB_USE_SOFTINTR
1954 	sc->sc_softwake = 1;
1955 #endif /* USB_USE_SOFTINTR */
1956 	usb_schedsoftintr(&sc->sc_bus);
1957 #ifdef USB_USE_SOFTINTR
1958 	DPRINTFN(1,("uhci_abort_xfer: tsleep\n"));
1959 	tsleep(&sc->sc_softwake, PZERO, "uhciab", 0);
1960 #endif /* USB_USE_SOFTINTR */
1961 	splx(s);
1962 
1963 	/*
1964 	 * Step 3: Execute callback.
1965 	 */
1966 	DPRINTFN(1,("uhci_abort_xfer: callback\n"));
1967 	s = splusb();
1968 #ifdef DIAGNOSTIC
1969 	ii->isdone = 1;
1970 #endif
1971 	usb_transfer_complete(xfer);
1972 	splx(s);
1973 }
1974 
1975 /* Close a device bulk pipe. */
1976 void
uhci_device_bulk_close(usbd_pipe_handle pipe)1977 uhci_device_bulk_close(usbd_pipe_handle pipe)
1978 {
1979 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1980 	usbd_device_handle dev = upipe->pipe.device;
1981 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1982 
1983 	uhci_free_sqh(sc, upipe->u.bulk.sqh);
1984 }
1985 
1986 usbd_status
uhci_device_ctrl_transfer(usbd_xfer_handle xfer)1987 uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
1988 {
1989 	usbd_status err;
1990 
1991 	/* Insert last in queue. */
1992 	err = usb_insert_transfer(xfer);
1993 	if (err)
1994 		return (err);
1995 
1996 	/*
1997 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
1998 	 * so start it first.
1999 	 */
2000 	return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2001 }
2002 
2003 usbd_status
uhci_device_ctrl_start(usbd_xfer_handle xfer)2004 uhci_device_ctrl_start(usbd_xfer_handle xfer)
2005 {
2006 	uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2007 	usbd_status err;
2008 
2009 	if (sc->sc_dying)
2010 		return (USBD_IOERROR);
2011 
2012 #ifdef DIAGNOSTIC
2013 	if (!(xfer->rqflags & URQ_REQUEST))
2014 		panic("uhci_device_ctrl_transfer: not a request");
2015 #endif
2016 
2017 	err = uhci_device_request(xfer);
2018 	if (err)
2019 		return (err);
2020 
2021 	if (sc->sc_bus.use_polling)
2022 		uhci_waitintr(sc, xfer);
2023 	return (USBD_IN_PROGRESS);
2024 }
2025 
2026 usbd_status
uhci_device_intr_transfer(usbd_xfer_handle xfer)2027 uhci_device_intr_transfer(usbd_xfer_handle xfer)
2028 {
2029 	usbd_status err;
2030 
2031 	/* Insert last in queue. */
2032 	err = usb_insert_transfer(xfer);
2033 	if (err)
2034 		return (err);
2035 
2036 	/*
2037 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
2038 	 * so start it first.
2039 	 */
2040 	return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2041 }
2042 
2043 usbd_status
uhci_device_intr_start(usbd_xfer_handle xfer)2044 uhci_device_intr_start(usbd_xfer_handle xfer)
2045 {
2046 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2047 	usbd_device_handle dev = upipe->pipe.device;
2048 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2049 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2050 	uhci_soft_td_t *data, *dataend;
2051 	uhci_soft_qh_t *sqh;
2052 	usbd_status err;
2053 	int i, s;
2054 
2055 	if (sc->sc_dying)
2056 		return (USBD_IOERROR);
2057 
2058 	DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
2059 		    xfer, xfer->length, xfer->flags));
2060 
2061 #ifdef DIAGNOSTIC
2062 	if (xfer->rqflags & URQ_REQUEST)
2063 		panic("uhci_device_intr_transfer: a request");
2064 #endif
2065 
2066 	err = uhci_alloc_std_chain(upipe, sc, xfer->length, 1, xfer->flags,
2067 				   &xfer->dmabuf, &data, &dataend);
2068 	if (err)
2069 		return (err);
2070 	dataend->td.td_status |= htole32(UHCI_TD_IOC);
2071 
2072 #ifdef UHCI_DEBUG
2073 	if (uhcidebug > 10) {
2074 		DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
2075 		uhci_dump_tds(data);
2076 		uhci_dump_qh(upipe->u.intr.qhs[0]);
2077 	}
2078 #endif
2079 
2080 	s = splusb();
2081 	/* Set up interrupt info. */
2082 	ii->xfer = xfer;
2083 	ii->stdstart = data;
2084 	ii->stdend = dataend;
2085 #ifdef DIAGNOSTIC
2086 	if (!ii->isdone) {
2087 		printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
2088 	}
2089 	ii->isdone = 0;
2090 #endif
2091 
2092 	DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
2093 		     upipe->u.intr.qhs[0]));
2094 	for (i = 0; i < upipe->u.intr.npoll; i++) {
2095 		sqh = upipe->u.intr.qhs[i];
2096 		sqh->elink = data;
2097 		sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2098 	}
2099 	uhci_add_intr_info(sc, ii);
2100 	xfer->status = USBD_IN_PROGRESS;
2101 	splx(s);
2102 
2103 #ifdef UHCI_DEBUG
2104 	if (uhcidebug > 10) {
2105 		DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
2106 		uhci_dump_tds(data);
2107 		uhci_dump_qh(upipe->u.intr.qhs[0]);
2108 	}
2109 #endif
2110 
2111 	return (USBD_IN_PROGRESS);
2112 }
2113 
2114 /* Abort a device control request. */
2115 void
uhci_device_ctrl_abort(usbd_xfer_handle xfer)2116 uhci_device_ctrl_abort(usbd_xfer_handle xfer)
2117 {
2118 	DPRINTF(("uhci_device_ctrl_abort:\n"));
2119 	uhci_abort_xfer(xfer, USBD_CANCELLED);
2120 }
2121 
2122 /* Close a device control pipe. */
2123 void
uhci_device_ctrl_close(usbd_pipe_handle pipe)2124 uhci_device_ctrl_close(usbd_pipe_handle pipe)
2125 {
2126 }
2127 
2128 /* Abort a device interrupt request. */
2129 void
uhci_device_intr_abort(usbd_xfer_handle xfer)2130 uhci_device_intr_abort(usbd_xfer_handle xfer)
2131 {
2132 	DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
2133 	if (xfer->pipe->intrxfer == xfer) {
2134 		DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
2135 		xfer->pipe->intrxfer = NULL;
2136 	}
2137 	uhci_abort_xfer(xfer, USBD_CANCELLED);
2138 }
2139 
2140 /* Close a device interrupt pipe. */
2141 void
uhci_device_intr_close(usbd_pipe_handle pipe)2142 uhci_device_intr_close(usbd_pipe_handle pipe)
2143 {
2144 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2145 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2146 	int i, npoll;
2147 	int s;
2148 
2149 	/* Unlink descriptors from controller data structures. */
2150 	npoll = upipe->u.intr.npoll;
2151 	s = splusb();
2152 	for (i = 0; i < npoll; i++)
2153 		uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
2154 	splx(s);
2155 
2156 	/*
2157 	 * We now have to wait for any activity on the physical
2158 	 * descriptors to stop.
2159 	 */
2160 	usb_delay_ms(&sc->sc_bus, 2);
2161 
2162 	for(i = 0; i < npoll; i++)
2163 		uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
2164 	free(upipe->u.intr.qhs, M_USBHC);
2165 
2166 	/* XXX free other resources */
2167 }
2168 
2169 usbd_status
uhci_device_request(usbd_xfer_handle xfer)2170 uhci_device_request(usbd_xfer_handle xfer)
2171 {
2172 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2173 	usb_device_request_t *req = &xfer->request;
2174 	usbd_device_handle dev = upipe->pipe.device;
2175 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2176 	int addr = dev->address;
2177 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2178 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2179 	uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
2180 	uhci_soft_qh_t *sqh;
2181 	int len;
2182 	u_int32_t ls;
2183 	usbd_status err;
2184 	int isread;
2185 	int s;
2186 
2187 	DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
2188 		    "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2189 		    req->bmRequestType, req->bRequest, UGETW(req->wValue),
2190 		    UGETW(req->wIndex), UGETW(req->wLength),
2191 		    addr, endpt));
2192 
2193 	ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0;
2194 	isread = req->bmRequestType & UT_READ;
2195 	len = UGETW(req->wLength);
2196 
2197 	setup = upipe->u.ctl.setup;
2198 	stat = upipe->u.ctl.stat;
2199 	sqh = upipe->u.ctl.sqh;
2200 
2201 	/* Set up data transaction */
2202 	if (len != 0) {
2203 		upipe->nexttoggle = 1;
2204 		err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
2205 					   &xfer->dmabuf, &data, &dataend);
2206 		if (err)
2207 			return (err);
2208 		next = data;
2209 		dataend->link.std = stat;
2210 		dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2211 	} else {
2212 		next = stat;
2213 	}
2214 	upipe->u.ctl.length = len;
2215 
2216 	memcpy(KERNADDR(&upipe->u.ctl.reqdma, 0), req, sizeof *req);
2217 
2218 	setup->link.std = next;
2219 	setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2220 	setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2221 		UHCI_TD_ACTIVE);
2222 	setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr));
2223 	setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma, 0));
2224 
2225 	stat->link.std = NULL;
2226 	stat->td.td_link = htole32(UHCI_PTR_T);
2227 	stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2228 		UHCI_TD_ACTIVE | UHCI_TD_IOC);
2229 	stat->td.td_token =
2230 		htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
2231 		                 UHCI_TD_IN (0, endpt, addr, 1));
2232 	stat->td.td_buffer = htole32(0);
2233 
2234 #ifdef UHCI_DEBUG
2235 	if (uhcidebug > 10) {
2236 		DPRINTF(("uhci_device_request: before transfer\n"));
2237 		uhci_dump_tds(setup);
2238 	}
2239 #endif
2240 
2241 	/* Set up interrupt info. */
2242 	ii->xfer = xfer;
2243 	ii->stdstart = setup;
2244 	ii->stdend = stat;
2245 #ifdef DIAGNOSTIC
2246 	if (!ii->isdone) {
2247 		printf("uhci_device_request: not done, ii=%p\n", ii);
2248 	}
2249 	ii->isdone = 0;
2250 #endif
2251 
2252 	sqh->elink = setup;
2253 	sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
2254 
2255 	s = splusb();
2256 	if (dev->speed == USB_SPEED_LOW)
2257 		uhci_add_ls_ctrl(sc, sqh);
2258 	else
2259 		uhci_add_hs_ctrl(sc, sqh);
2260 	uhci_add_intr_info(sc, ii);
2261 #ifdef UHCI_DEBUG
2262 	if (uhcidebug > 12) {
2263 		uhci_soft_td_t *std;
2264 		uhci_soft_qh_t *xqh;
2265 		uhci_soft_qh_t *sxqh;
2266 		int maxqh = 0;
2267 		uhci_physaddr_t link;
2268 		DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
2269 		for (std = sc->sc_vframes[0].htd, link = 0;
2270 		     (link & UHCI_PTR_QH) == 0;
2271 		     std = std->link.std) {
2272 			link = le32toh(std->td.td_link);
2273 			uhci_dump_td(std);
2274 		}
2275 		sxqh = (uhci_soft_qh_t *)std;
2276 		uhci_dump_qh(sxqh);
2277 		for (xqh = sxqh;
2278 		     xqh != NULL;
2279 		     xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
2280                             xqh->hlink == xqh ? NULL : xqh->hlink)) {
2281 			uhci_dump_qh(xqh);
2282 		}
2283 		DPRINTF(("Enqueued QH:\n"));
2284 		uhci_dump_qh(sqh);
2285 		uhci_dump_tds(sqh->elink);
2286 	}
2287 #endif
2288 	if (xfer->timeout && !sc->sc_bus.use_polling) {
2289 		usb_callout(xfer->timeout_handle, mstohz(xfer->timeout),
2290 			    uhci_timeout, ii);
2291 	}
2292 	xfer->status = USBD_IN_PROGRESS;
2293 	splx(s);
2294 
2295 	return (USBD_NORMAL_COMPLETION);
2296 }
2297 
2298 usbd_status
uhci_device_isoc_transfer(usbd_xfer_handle xfer)2299 uhci_device_isoc_transfer(usbd_xfer_handle xfer)
2300 {
2301 	usbd_status err;
2302 
2303 	DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
2304 
2305 	/* Put it on our queue, */
2306 	err = usb_insert_transfer(xfer);
2307 
2308 	/* bail out on error, */
2309 	if (err && err != USBD_IN_PROGRESS)
2310 		return (err);
2311 
2312 	/* XXX should check inuse here */
2313 
2314 	/* insert into schedule, */
2315 	uhci_device_isoc_enter(xfer);
2316 
2317 	/* and start if the pipe wasn't running */
2318 	if (!err)
2319 		uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2320 
2321 	return (err);
2322 }
2323 
2324 void
uhci_device_isoc_enter(usbd_xfer_handle xfer)2325 uhci_device_isoc_enter(usbd_xfer_handle xfer)
2326 {
2327 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2328 	usbd_device_handle dev = upipe->pipe.device;
2329 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2330 	struct iso *iso = &upipe->u.iso;
2331 	uhci_soft_td_t *std;
2332 	u_int32_t buf, len, status;
2333 	int s, i, next, nframes;
2334 
2335 	DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
2336 		    "nframes=%d\n",
2337 		    iso->inuse, iso->next, xfer, xfer->nframes));
2338 
2339 	if (sc->sc_dying)
2340 		return;
2341 
2342 	if (xfer->status == USBD_IN_PROGRESS) {
2343 		/* This request has already been entered into the frame list */
2344 		printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
2345 		/* XXX */
2346 	}
2347 
2348 #ifdef DIAGNOSTIC
2349 	if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
2350 		printf("uhci_device_isoc_enter: overflow!\n");
2351 #endif
2352 
2353 	next = iso->next;
2354 	if (next == -1) {
2355 		/* Not in use yet, schedule it a few frames ahead. */
2356 		next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2357 		DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2358 	}
2359 
2360 	xfer->status = USBD_IN_PROGRESS;
2361 	UXFER(xfer)->curframe = next;
2362 
2363 	buf = DMAADDR(&xfer->dmabuf, 0);
2364 	status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2365 				     UHCI_TD_ACTIVE |
2366 				     UHCI_TD_IOS);
2367 	nframes = xfer->nframes;
2368 	s = splusb();
2369 	for (i = 0; i < nframes; i++) {
2370 		std = iso->stds[next];
2371 		if (++next >= UHCI_VFRAMELIST_COUNT)
2372 			next = 0;
2373 		len = xfer->frlengths[i];
2374 		std->td.td_buffer = htole32(buf);
2375 		if (i == nframes - 1)
2376 			status |= UHCI_TD_IOC;
2377 		std->td.td_status = htole32(status);
2378 		std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2379 		std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
2380 #ifdef UHCI_DEBUG
2381 		if (uhcidebug > 5) {
2382 			DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2383 			uhci_dump_td(std);
2384 		}
2385 #endif
2386 		buf += len;
2387 	}
2388 	iso->next = next;
2389 	iso->inuse += xfer->nframes;
2390 
2391 	splx(s);
2392 }
2393 
2394 usbd_status
uhci_device_isoc_start(usbd_xfer_handle xfer)2395 uhci_device_isoc_start(usbd_xfer_handle xfer)
2396 {
2397 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2398 	uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2399 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2400 	uhci_soft_td_t *end;
2401 	int s, i;
2402 
2403 	DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
2404 
2405 	if (sc->sc_dying)
2406 		return (USBD_IOERROR);
2407 
2408 #ifdef DIAGNOSTIC
2409 	if (xfer->status != USBD_IN_PROGRESS)
2410 		printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2411 #endif
2412 
2413 	/* Find the last TD */
2414 	i = UXFER(xfer)->curframe + xfer->nframes;
2415 	if (i >= UHCI_VFRAMELIST_COUNT)
2416 		i -= UHCI_VFRAMELIST_COUNT;
2417 	end = upipe->u.iso.stds[i];
2418 
2419 #ifdef DIAGNOSTIC
2420 	if (end == NULL) {
2421 		printf("uhci_device_isoc_start: end == NULL\n");
2422 		return (USBD_INVAL);
2423 	}
2424 #endif
2425 
2426 	s = splusb();
2427 
2428 	/* Set up interrupt info. */
2429 	ii->xfer = xfer;
2430 	ii->stdstart = end;
2431 	ii->stdend = end;
2432 #ifdef DIAGNOSTIC
2433 	if (!ii->isdone)
2434 		printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
2435 	ii->isdone = 0;
2436 #endif
2437 	uhci_add_intr_info(sc, ii);
2438 
2439 	splx(s);
2440 
2441 	return (USBD_IN_PROGRESS);
2442 }
2443 
2444 void
uhci_device_isoc_abort(usbd_xfer_handle xfer)2445 uhci_device_isoc_abort(usbd_xfer_handle xfer)
2446 {
2447 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2448 	uhci_soft_td_t **stds = upipe->u.iso.stds;
2449 	uhci_soft_td_t *std;
2450 	int i, n, s, nframes, maxlen, len;
2451 
2452 	s = splusb();
2453 
2454 	/* Transfer is already done. */
2455 	if (xfer->status != USBD_NOT_STARTED &&
2456 	    xfer->status != USBD_IN_PROGRESS) {
2457 		splx(s);
2458 		return;
2459 	}
2460 
2461 	/* Give xfer the requested abort code. */
2462 	xfer->status = USBD_CANCELLED;
2463 
2464 	/* make hardware ignore it, */
2465 	nframes = xfer->nframes;
2466 	n = UXFER(xfer)->curframe;
2467 	maxlen = 0;
2468 	for (i = 0; i < nframes; i++) {
2469 		std = stds[n];
2470 		std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2471 		len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token));
2472 		if (len > maxlen)
2473 			maxlen = len;
2474 		if (++n >= UHCI_VFRAMELIST_COUNT)
2475 			n = 0;
2476 	}
2477 
2478 	/* and wait until we are sure the hardware has finished. */
2479 	delay(maxlen);
2480 
2481 #ifdef DIAGNOSTIC
2482 	UXFER(xfer)->iinfo.isdone = 1;
2483 #endif
2484 	/* Run callback and remove from interrupt list. */
2485 	usb_transfer_complete(xfer);
2486 
2487 	splx(s);
2488 }
2489 
2490 void
uhci_device_isoc_close(usbd_pipe_handle pipe)2491 uhci_device_isoc_close(usbd_pipe_handle pipe)
2492 {
2493 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2494 	usbd_device_handle dev = upipe->pipe.device;
2495 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2496 	uhci_soft_td_t *std, *vstd;
2497 	struct iso *iso;
2498 	int i, s;
2499 
2500 	/*
2501 	 * Make sure all TDs are marked as inactive.
2502 	 * Wait for completion.
2503 	 * Unschedule.
2504 	 * Deallocate.
2505 	 */
2506 	iso = &upipe->u.iso;
2507 
2508 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2509 		iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE);
2510 	usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2511 
2512 	s = splusb();
2513 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2514 		std = iso->stds[i];
2515 		for (vstd = sc->sc_vframes[i].htd;
2516 		     vstd != NULL && vstd->link.std != std;
2517 		     vstd = vstd->link.std)
2518 			;
2519 		if (vstd == NULL) {
2520 			/*panic*/
2521 			printf("uhci_device_isoc_close: %p not found\n", std);
2522 			splx(s);
2523 			return;
2524 		}
2525 		vstd->link = std->link;
2526 		vstd->td.td_link = std->td.td_link;
2527 		uhci_free_std(sc, std);
2528 	}
2529 	splx(s);
2530 
2531 	free(iso->stds, M_USBHC);
2532 }
2533 
2534 usbd_status
uhci_setup_isoc(usbd_pipe_handle pipe)2535 uhci_setup_isoc(usbd_pipe_handle pipe)
2536 {
2537 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2538 	usbd_device_handle dev = upipe->pipe.device;
2539 	uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2540 	int addr = upipe->pipe.device->address;
2541 	int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2542 	int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2543 	uhci_soft_td_t *std, *vstd;
2544 	u_int32_t token;
2545 	struct iso *iso;
2546 	int i, s;
2547 
2548 	iso = &upipe->u.iso;
2549 	iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2550 			   M_USBHC, M_WAITOK);
2551 
2552 	token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2553 		     UHCI_TD_OUT(0, endpt, addr, 0);
2554 
2555 	/* Allocate the TDs and mark as inactive; */
2556 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2557 		std = uhci_alloc_std(sc);
2558 		if (std == 0)
2559 			goto bad;
2560 		std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
2561 		std->td.td_token = htole32(token);
2562 		iso->stds[i] = std;
2563 	}
2564 
2565 	/* Insert TDs into schedule. */
2566 	s = splusb();
2567 	for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2568 		std = iso->stds[i];
2569 		vstd = sc->sc_vframes[i].htd;
2570 		std->link = vstd->link;
2571 		std->td.td_link = vstd->td.td_link;
2572 		vstd->link.std = std;
2573 		vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
2574 	}
2575 	splx(s);
2576 
2577 	iso->next = -1;
2578 	iso->inuse = 0;
2579 
2580 	return (USBD_NORMAL_COMPLETION);
2581 
2582  bad:
2583 	while (--i >= 0)
2584 		uhci_free_std(sc, iso->stds[i]);
2585 	free(iso->stds, M_USBHC);
2586 	return (USBD_NOMEM);
2587 }
2588 
2589 void
uhci_device_isoc_done(usbd_xfer_handle xfer)2590 uhci_device_isoc_done(usbd_xfer_handle xfer)
2591 {
2592 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2593 
2594 	DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2595 
2596 	if (ii->xfer != xfer)
2597 		/* Not on interrupt list, ignore it. */
2598 		return;
2599 
2600 	if (!uhci_active_intr_info(ii))
2601 		return;
2602 
2603 #ifdef DIAGNOSTIC
2604 	if (xfer->busy_free == XFER_FREE) {
2605 		printf("uhci_device_isoc_done: xfer=%p is free\n", xfer);
2606 		return;
2607 	}
2608 
2609         if (ii->stdend == NULL) {
2610                 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2611 #ifdef UHCI_DEBUG
2612 		uhci_dump_ii(ii);
2613 #endif
2614 		return;
2615 	}
2616 #endif
2617 
2618 	/* Turn off the interrupt since it is active even if the TD is not. */
2619 	ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC);
2620 
2621 	uhci_del_intr_info(ii);	/* remove from active list */
2622 }
2623 
2624 void
uhci_device_intr_done(usbd_xfer_handle xfer)2625 uhci_device_intr_done(usbd_xfer_handle xfer)
2626 {
2627 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2628 	uhci_softc_t *sc = ii->sc;
2629 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2630 	uhci_soft_qh_t *sqh;
2631 	int i, npoll;
2632 
2633 	DPRINTFN(5, ("uhci_device_intr_done: length=%d\n", xfer->actlen));
2634 
2635 	npoll = upipe->u.intr.npoll;
2636 	for(i = 0; i < npoll; i++) {
2637 		sqh = upipe->u.intr.qhs[i];
2638 		sqh->elink = NULL;
2639 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2640 	}
2641 	uhci_free_std_chain(sc, ii->stdstart, NULL);
2642 
2643 	/* XXX Wasteful. */
2644 	if (xfer->pipe->repeat) {
2645 		uhci_soft_td_t *data, *dataend;
2646 
2647 		DPRINTFN(5,("uhci_device_intr_done: requeing\n"));
2648 
2649 		/* This alloc cannot fail since we freed the chain above. */
2650 		uhci_alloc_std_chain(upipe, sc, xfer->length, 1, xfer->flags,
2651 				     &xfer->dmabuf, &data, &dataend);
2652 		dataend->td.td_status |= htole32(UHCI_TD_IOC);
2653 
2654 #ifdef UHCI_DEBUG
2655 		if (uhcidebug > 10) {
2656 			DPRINTF(("uhci_device_intr_done: data(1)\n"));
2657 			uhci_dump_tds(data);
2658 			uhci_dump_qh(upipe->u.intr.qhs[0]);
2659 		}
2660 #endif
2661 
2662 		ii->stdstart = data;
2663 		ii->stdend = dataend;
2664 #ifdef DIAGNOSTIC
2665 		if (!ii->isdone) {
2666 			printf("uhci_device_intr_done: not done, ii=%p\n", ii);
2667 		}
2668 		ii->isdone = 0;
2669 #endif
2670 		for (i = 0; i < npoll; i++) {
2671 			sqh = upipe->u.intr.qhs[i];
2672 			sqh->elink = data;
2673 			sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2674 		}
2675 		xfer->status = USBD_IN_PROGRESS;
2676 		/* The ii is already on the examined list, just leave it. */
2677 	} else {
2678 		DPRINTFN(5,("uhci_device_intr_done: removing\n"));
2679 		if (uhci_active_intr_info(ii))
2680 			uhci_del_intr_info(ii);
2681 	}
2682 }
2683 
2684 /* Deallocate request data structures */
2685 void
uhci_device_ctrl_done(usbd_xfer_handle xfer)2686 uhci_device_ctrl_done(usbd_xfer_handle xfer)
2687 {
2688 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2689 	uhci_softc_t *sc = ii->sc;
2690 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2691 
2692 #ifdef DIAGNOSTIC
2693 	if (!(xfer->rqflags & URQ_REQUEST))
2694 		panic("uhci_device_ctrl_done: not a request");
2695 #endif
2696 
2697 	if (!uhci_active_intr_info(ii))
2698 		return;
2699 
2700 	uhci_del_intr_info(ii);	/* remove from active list */
2701 
2702 	if (upipe->pipe.device->speed == USB_SPEED_LOW)
2703 		uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
2704 	else
2705 		uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh);
2706 
2707 	if (upipe->u.ctl.length != 0)
2708 		uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2709 
2710 	DPRINTFN(5, ("uhci_device_ctrl_done: length=%d\n", xfer->actlen));
2711 }
2712 
2713 /* Deallocate request data structures */
2714 void
uhci_device_bulk_done(usbd_xfer_handle xfer)2715 uhci_device_bulk_done(usbd_xfer_handle xfer)
2716 {
2717 	uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2718 	uhci_softc_t *sc = ii->sc;
2719 	struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2720 
2721 	DPRINTFN(5,("uhci_device_bulk_done: xfer=%p ii=%p sc=%p upipe=%p\n",
2722 		    xfer, ii, sc, upipe));
2723 
2724 	if (!uhci_active_intr_info(ii))
2725 		return;
2726 
2727 	uhci_del_intr_info(ii);	/* remove from active list */
2728 
2729 	uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2730 
2731 	uhci_free_std_chain(sc, ii->stdstart, NULL);
2732 
2733 	DPRINTFN(5, ("uhci_device_bulk_done: length=%d\n", xfer->actlen));
2734 }
2735 
2736 /* Add interrupt QH, called with vflock. */
2737 void
uhci_add_intr(uhci_softc_t * sc,uhci_soft_qh_t * sqh)2738 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2739 {
2740 	struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2741 	uhci_soft_qh_t *eqh;
2742 
2743 	DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2744 
2745 	eqh = vf->eqh;
2746 	sqh->hlink       = eqh->hlink;
2747 	sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2748 	eqh->hlink       = sqh;
2749 	eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
2750 	vf->eqh = sqh;
2751 	vf->bandwidth++;
2752 }
2753 
2754 /* Remove interrupt QH. */
2755 void
uhci_remove_intr(uhci_softc_t * sc,uhci_soft_qh_t * sqh)2756 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2757 {
2758 	struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2759 	uhci_soft_qh_t *pqh;
2760 
2761 	DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2762 
2763 	/* See comment in uhci_remove_ctrl() */
2764 	if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
2765 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2766 		delay(UHCI_QH_REMOVE_DELAY);
2767 	}
2768 
2769 	pqh = uhci_find_prev_qh(vf->hqh, sqh);
2770 	pqh->hlink       = sqh->hlink;
2771 	pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2772 	delay(UHCI_QH_REMOVE_DELAY);
2773 	if (vf->eqh == sqh)
2774 		vf->eqh = pqh;
2775 	vf->bandwidth--;
2776 }
2777 
2778 usbd_status
uhci_device_setintr(uhci_softc_t * sc,struct uhci_pipe * upipe,int ival)2779 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
2780 {
2781 	uhci_soft_qh_t *sqh;
2782 	int i, npoll, s;
2783 	u_int bestbw, bw, bestoffs, offs;
2784 
2785 	DPRINTFN(2, ("uhci_device_setintr: pipe=%p\n", upipe));
2786 	if (ival == 0) {
2787 		printf("uhci_device_setintr: 0 interval\n");
2788 		return (USBD_INVAL);
2789 	}
2790 
2791 	if (ival > UHCI_VFRAMELIST_COUNT)
2792 		ival = UHCI_VFRAMELIST_COUNT;
2793 	npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2794 	DPRINTFN(2, ("uhci_device_setintr: ival=%d npoll=%d\n", ival, npoll));
2795 
2796 	upipe->u.intr.npoll = npoll;
2797 	upipe->u.intr.qhs =
2798 		malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
2799 
2800 	/*
2801 	 * Figure out which offset in the schedule that has most
2802 	 * bandwidth left over.
2803 	 */
2804 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2805 	for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2806 		for (bw = i = 0; i < npoll; i++)
2807 			bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2808 		if (bw < bestbw) {
2809 			bestbw = bw;
2810 			bestoffs = offs;
2811 		}
2812 	}
2813 	DPRINTFN(1, ("uhci_device_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2814 
2815 	for(i = 0; i < npoll; i++) {
2816 		upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2817 		sqh->elink = NULL;
2818 		sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2819 		sqh->pos = MOD(i * ival + bestoffs);
2820 	}
2821 #undef MOD
2822 
2823 	s = splusb();
2824 	/* Enter QHs into the controller data structures. */
2825 	for(i = 0; i < npoll; i++)
2826 		uhci_add_intr(sc, upipe->u.intr.qhs[i]);
2827 	splx(s);
2828 
2829 	DPRINTFN(5, ("uhci_device_setintr: returns %p\n", upipe));
2830 	return (USBD_NORMAL_COMPLETION);
2831 }
2832 
2833 /* Open a new pipe. */
2834 usbd_status
uhci_open(usbd_pipe_handle pipe)2835 uhci_open(usbd_pipe_handle pipe)
2836 {
2837 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2838 	struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2839 	usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2840 	usbd_status err;
2841 	int ival;
2842 
2843 	DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2844 		     pipe, pipe->device->address,
2845 		     ed->bEndpointAddress, sc->sc_addr));
2846 
2847 	upipe->aborting = 0;
2848 	upipe->nexttoggle = 0;
2849 
2850 	if (pipe->device->address == sc->sc_addr) {
2851 		switch (ed->bEndpointAddress) {
2852 		case USB_CONTROL_ENDPOINT:
2853 			pipe->methods = &uhci_root_ctrl_methods;
2854 			break;
2855 		case UE_DIR_IN | UHCI_INTR_ENDPT:
2856 			pipe->methods = &uhci_root_intr_methods;
2857 			break;
2858 		default:
2859 			return (USBD_INVAL);
2860 		}
2861 	} else {
2862 		switch (ed->bmAttributes & UE_XFERTYPE) {
2863 		case UE_CONTROL:
2864 			pipe->methods = &uhci_device_ctrl_methods;
2865 			upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2866 			if (upipe->u.ctl.sqh == NULL)
2867 				goto bad;
2868 			upipe->u.ctl.setup = uhci_alloc_std(sc);
2869 			if (upipe->u.ctl.setup == NULL) {
2870 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
2871 				goto bad;
2872 			}
2873 			upipe->u.ctl.stat = uhci_alloc_std(sc);
2874 			if (upipe->u.ctl.stat == NULL) {
2875 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
2876 				uhci_free_std(sc, upipe->u.ctl.setup);
2877 				goto bad;
2878 			}
2879 			err = usb_allocmem(&sc->sc_bus,
2880 				  sizeof(usb_device_request_t),
2881 				  0, &upipe->u.ctl.reqdma);
2882 			if (err) {
2883 				uhci_free_sqh(sc, upipe->u.ctl.sqh);
2884 				uhci_free_std(sc, upipe->u.ctl.setup);
2885 				uhci_free_std(sc, upipe->u.ctl.stat);
2886 				goto bad;
2887 			}
2888 			break;
2889 		case UE_INTERRUPT:
2890 			pipe->methods = &uhci_device_intr_methods;
2891 			ival = pipe->interval;
2892 			if (ival == USBD_DEFAULT_INTERVAL)
2893 				ival = ed->bInterval;
2894 			return (uhci_device_setintr(sc, upipe, ival));
2895 		case UE_ISOCHRONOUS:
2896 			pipe->methods = &uhci_device_isoc_methods;
2897 			return (uhci_setup_isoc(pipe));
2898 		case UE_BULK:
2899 			pipe->methods = &uhci_device_bulk_methods;
2900 			upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2901 			if (upipe->u.bulk.sqh == NULL)
2902 				goto bad;
2903 			break;
2904 		}
2905 	}
2906 	return (USBD_NORMAL_COMPLETION);
2907 
2908  bad:
2909 	return (USBD_NOMEM);
2910 }
2911 
2912 /*
2913  * Data structures and routines to emulate the root hub.
2914  */
2915 usb_device_descriptor_t uhci_devd = {
2916 	USB_DEVICE_DESCRIPTOR_SIZE,
2917 	UDESC_DEVICE,		/* type */
2918 	{0x00, 0x01},		/* USB version */
2919 	UDCLASS_HUB,		/* class */
2920 	UDSUBCLASS_HUB,		/* subclass */
2921 	UDPROTO_FSHUB,		/* protocol */
2922 	64,			/* max packet */
2923 	{0},{0},{0x00,0x01},	/* device id */
2924 	1,2,0,			/* string indicies */
2925 	1			/* # of configurations */
2926 };
2927 
2928 usb_config_descriptor_t uhci_confd = {
2929 	USB_CONFIG_DESCRIPTOR_SIZE,
2930 	UDESC_CONFIG,
2931 	{USB_CONFIG_DESCRIPTOR_SIZE +
2932 	 USB_INTERFACE_DESCRIPTOR_SIZE +
2933 	 USB_ENDPOINT_DESCRIPTOR_SIZE},
2934 	1,
2935 	1,
2936 	0,
2937 	UC_SELF_POWERED,
2938 	0			/* max power */
2939 };
2940 
2941 usb_interface_descriptor_t uhci_ifcd = {
2942 	USB_INTERFACE_DESCRIPTOR_SIZE,
2943 	UDESC_INTERFACE,
2944 	0,
2945 	0,
2946 	1,
2947 	UICLASS_HUB,
2948 	UISUBCLASS_HUB,
2949 	UIPROTO_FSHUB,
2950 	0
2951 };
2952 
2953 usb_endpoint_descriptor_t uhci_endpd = {
2954 	USB_ENDPOINT_DESCRIPTOR_SIZE,
2955 	UDESC_ENDPOINT,
2956 	UE_DIR_IN | UHCI_INTR_ENDPT,
2957 	UE_INTERRUPT,
2958 	{8},
2959 	255
2960 };
2961 
2962 usb_hub_descriptor_t uhci_hubd_piix = {
2963 	USB_HUB_DESCRIPTOR_SIZE,
2964 	UDESC_HUB,
2965 	2,
2966 	{ UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
2967 	50,			/* power on to power good */
2968 	0,
2969 	{ 0x00 },		/* both ports are removable */
2970 };
2971 
2972 int
uhci_str(usb_string_descriptor_t * p,int l,char * s)2973 uhci_str(usb_string_descriptor_t *p, int l, char *s)
2974 {
2975 	int i;
2976 
2977 	if (l == 0)
2978 		return (0);
2979 	p->bLength = 2 * strlen(s) + 2;
2980 	if (l == 1)
2981 		return (1);
2982 	p->bDescriptorType = UDESC_STRING;
2983 	l -= 2;
2984 	for (i = 0; s[i] && l > 1; i++, l -= 2)
2985 		USETW2(p->bString[i], 0, s[i]);
2986 	return (2*i+2);
2987 }
2988 
2989 /*
2990  * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
2991  * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
2992  * should not be used by the USB subsystem.  As we cannot issue a
2993  * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
2994  * will be enabled as part of the reset.
2995  *
2996  * On the VT83C572, the port cannot be successfully enabled until the
2997  * outstanding "port enable change" and "connection status change"
2998  * events have been reset.
2999  */
3000 Static usbd_status
uhci_portreset(uhci_softc_t * sc,int index)3001 uhci_portreset(uhci_softc_t *sc, int index)
3002 {
3003 	int lim, port, x;
3004 
3005 	if (index == 1)
3006 		port = UHCI_PORTSC1;
3007 	else if (index == 2)
3008 		port = UHCI_PORTSC2;
3009 	else
3010 		return (USBD_IOERROR);
3011 
3012 	x = URWMASK(UREAD2(sc, port));
3013 	UWRITE2(sc, port, x | UHCI_PORTSC_PR);
3014 
3015 	usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
3016 
3017 	DPRINTFN(3,("uhci port %d reset, status0 = 0x%04x\n",
3018 		    index, UREAD2(sc, port)));
3019 
3020 	x = URWMASK(UREAD2(sc, port));
3021 	UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3022 
3023 	delay(100);
3024 
3025 	DPRINTFN(3,("uhci port %d reset, status1 = 0x%04x\n",
3026 		    index, UREAD2(sc, port)));
3027 
3028 	x = URWMASK(UREAD2(sc, port));
3029 	UWRITE2(sc, port, x  | UHCI_PORTSC_PE);
3030 
3031 	for (lim = 10; --lim > 0;) {
3032 		usb_delay_ms(&sc->sc_bus, USB_PORT_RESET_DELAY);
3033 
3034 		x = UREAD2(sc, port);
3035 
3036 		DPRINTFN(3,("uhci port %d iteration %u, status = 0x%04x\n",
3037 			    index, lim, x));
3038 
3039 		if (!(x & UHCI_PORTSC_CCS)) {
3040 			/*
3041 			 * No device is connected (or was disconnected
3042 			 * during reset).  Consider the port reset.
3043 			 * The delay must be long enough to ensure on
3044 			 * the initial iteration that the device
3045 			 * connection will have been registered.  50ms
3046 			 * appears to be sufficient, but 20ms is not.
3047 			 */
3048 			DPRINTFN(3,("uhci port %d loop %u, device detached\n",
3049 				    index, lim));
3050 			break;
3051 		}
3052 
3053 		if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
3054 			/*
3055 			 * Port enabled changed and/or connection
3056 			 * status changed were set.  Reset either or
3057 			 * both raised flags (by writing a 1 to that
3058 			 * bit), and wait again for state to settle.
3059 			 */
3060 			UWRITE2(sc, port, URWMASK(x) |
3061 				(x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
3062 			continue;
3063 		}
3064 
3065 		if (x & UHCI_PORTSC_PE)
3066 			/* Port is enabled */
3067 			break;
3068 
3069 		UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
3070 	}
3071 
3072 	DPRINTFN(3,("uhci port %d reset, status2 = 0x%04x\n",
3073 		    index, UREAD2(sc, port)));
3074 
3075 	if (lim <= 0) {
3076 		DPRINTFN(1,("uhci port %d reset timed out\n", index));
3077 		return (USBD_TIMEOUT);
3078 	}
3079 
3080 	sc->sc_isreset = 1;
3081 	return (USBD_NORMAL_COMPLETION);
3082 }
3083 
3084 /*
3085  * Simulate a hardware hub by handling all the necessary requests.
3086  */
3087 usbd_status
uhci_root_ctrl_transfer(usbd_xfer_handle xfer)3088 uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
3089 {
3090 	usbd_status err;
3091 
3092 	/* Insert last in queue. */
3093 	err = usb_insert_transfer(xfer);
3094 	if (err)
3095 		return (err);
3096 
3097 	/*
3098 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
3099 	 * so start it first.
3100 	 */
3101 	return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3102 }
3103 
3104 usbd_status
uhci_root_ctrl_start(usbd_xfer_handle xfer)3105 uhci_root_ctrl_start(usbd_xfer_handle xfer)
3106 {
3107 	uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3108 	usb_device_request_t *req;
3109 	void *buf = NULL;
3110 	int port, x;
3111 	int s, len, value, index, status, change, l, totlen = 0;
3112 	usb_port_status_t ps;
3113 	usbd_status err;
3114 
3115 	if (sc->sc_dying)
3116 		return (USBD_IOERROR);
3117 
3118 #ifdef DIAGNOSTIC
3119 	if (!(xfer->rqflags & URQ_REQUEST))
3120 		panic("uhci_root_ctrl_transfer: not a request");
3121 #endif
3122 	req = &xfer->request;
3123 
3124 	DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
3125 		    req->bmRequestType, req->bRequest));
3126 
3127 	len = UGETW(req->wLength);
3128 	value = UGETW(req->wValue);
3129 	index = UGETW(req->wIndex);
3130 
3131 	if (len != 0)
3132 		buf = KERNADDR(&xfer->dmabuf, 0);
3133 
3134 #define C(x,y) ((x) | ((y) << 8))
3135 	switch(C(req->bRequest, req->bmRequestType)) {
3136 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3137 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3138 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3139 		/*
3140 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3141 		 * for the integrated root hub.
3142 		 */
3143 		break;
3144 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
3145 		if (len > 0) {
3146 			*(u_int8_t *)buf = sc->sc_conf;
3147 			totlen = 1;
3148 		}
3149 		break;
3150 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3151 		DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
3152 		switch(value >> 8) {
3153 		case UDESC_DEVICE:
3154 			if ((value & 0xff) != 0) {
3155 				err = USBD_IOERROR;
3156 				goto ret;
3157 			}
3158 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
3159 			USETW(uhci_devd.idVendor, sc->sc_id_vendor);
3160 			memcpy(buf, &uhci_devd, l);
3161 			break;
3162 		case UDESC_CONFIG:
3163 			if ((value & 0xff) != 0) {
3164 				err = USBD_IOERROR;
3165 				goto ret;
3166 			}
3167 			totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
3168 			memcpy(buf, &uhci_confd, l);
3169 			buf = (char *)buf + l;
3170 			len -= l;
3171 			l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
3172 			totlen += l;
3173 			memcpy(buf, &uhci_ifcd, l);
3174 			buf = (char *)buf + l;
3175 			len -= l;
3176 			l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
3177 			totlen += l;
3178 			memcpy(buf, &uhci_endpd, l);
3179 			break;
3180 		case UDESC_STRING:
3181 			if (len == 0)
3182 				break;
3183 			*(u_int8_t *)buf = 0;
3184 			totlen = 1;
3185 			switch (value & 0xff) {
3186 			case 1: /* Vendor */
3187 				totlen = uhci_str(buf, len, sc->sc_vendor);
3188 				break;
3189 			case 2: /* Product */
3190 				totlen = uhci_str(buf, len, "UHCI root hub");
3191 				break;
3192 			}
3193 			break;
3194 		default:
3195 			err = USBD_IOERROR;
3196 			goto ret;
3197 		}
3198 		break;
3199 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3200 		if (len > 0) {
3201 			*(u_int8_t *)buf = 0;
3202 			totlen = 1;
3203 		}
3204 		break;
3205 	case C(UR_GET_STATUS, UT_READ_DEVICE):
3206 		if (len > 1) {
3207 			USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
3208 			totlen = 2;
3209 		}
3210 		break;
3211 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
3212 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3213 		if (len > 1) {
3214 			USETW(((usb_status_t *)buf)->wStatus, 0);
3215 			totlen = 2;
3216 		}
3217 		break;
3218 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3219 		if (value >= USB_MAX_DEVICES) {
3220 			err = USBD_IOERROR;
3221 			goto ret;
3222 		}
3223 		sc->sc_addr = value;
3224 		break;
3225 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3226 		if (value != 0 && value != 1) {
3227 			err = USBD_IOERROR;
3228 			goto ret;
3229 		}
3230 		sc->sc_conf = value;
3231 		break;
3232 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3233 		break;
3234 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3235 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3236 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3237 		err = USBD_IOERROR;
3238 		goto ret;
3239 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3240 		break;
3241 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3242 		break;
3243 	/* Hub requests */
3244 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3245 		break;
3246 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3247 		DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
3248 			     "port=%d feature=%d\n",
3249 			     index, value));
3250 		if (index == 1)
3251 			port = UHCI_PORTSC1;
3252 		else if (index == 2)
3253 			port = UHCI_PORTSC2;
3254 		else {
3255 			err = USBD_IOERROR;
3256 			goto ret;
3257 		}
3258 		switch(value) {
3259 		case UHF_PORT_ENABLE:
3260 			x = URWMASK(UREAD2(sc, port));
3261 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
3262 			break;
3263 		case UHF_PORT_SUSPEND:
3264 			x = URWMASK(UREAD2(sc, port));
3265 			UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
3266 			break;
3267 		case UHF_PORT_RESET:
3268 			x = URWMASK(UREAD2(sc, port));
3269 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3270 			break;
3271 		case UHF_C_PORT_CONNECTION:
3272 			x = URWMASK(UREAD2(sc, port));
3273 			UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
3274 			break;
3275 		case UHF_C_PORT_ENABLE:
3276 			x = URWMASK(UREAD2(sc, port));
3277 			UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
3278 			break;
3279 		case UHF_C_PORT_OVER_CURRENT:
3280 			x = URWMASK(UREAD2(sc, port));
3281 			UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
3282 			break;
3283 		case UHF_C_PORT_RESET:
3284 			sc->sc_isreset = 0;
3285 			err = USBD_NORMAL_COMPLETION;
3286 			goto ret;
3287 		case UHF_PORT_CONNECTION:
3288 		case UHF_PORT_OVER_CURRENT:
3289 		case UHF_PORT_POWER:
3290 		case UHF_PORT_LOW_SPEED:
3291 		case UHF_C_PORT_SUSPEND:
3292 		default:
3293 			err = USBD_IOERROR;
3294 			goto ret;
3295 		}
3296 		break;
3297 	case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
3298 		if (index == 1)
3299 			port = UHCI_PORTSC1;
3300 		else if (index == 2)
3301 			port = UHCI_PORTSC2;
3302 		else {
3303 			err = USBD_IOERROR;
3304 			goto ret;
3305 		}
3306 		if (len > 0) {
3307 			*(u_int8_t *)buf =
3308 				(UREAD2(sc, port) & UHCI_PORTSC_LS) >>
3309 				UHCI_PORTSC_LS_SHIFT;
3310 			totlen = 1;
3311 		}
3312 		break;
3313 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3314 		if ((value & 0xff) != 0) {
3315 			err = USBD_IOERROR;
3316 			goto ret;
3317 		}
3318 		l = min(len, USB_HUB_DESCRIPTOR_SIZE);
3319 		totlen = l;
3320 		memcpy(buf, &uhci_hubd_piix, l);
3321 		break;
3322 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3323 		if (len != 4) {
3324 			err = USBD_IOERROR;
3325 			goto ret;
3326 		}
3327 		memset(buf, 0, len);
3328 		totlen = len;
3329 		break;
3330 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3331 		if (index == 1)
3332 			port = UHCI_PORTSC1;
3333 		else if (index == 2)
3334 			port = UHCI_PORTSC2;
3335 		else {
3336 			err = USBD_IOERROR;
3337 			goto ret;
3338 		}
3339 		if (len != 4) {
3340 			err = USBD_IOERROR;
3341 			goto ret;
3342 		}
3343 		x = UREAD2(sc, port);
3344 		status = change = 0;
3345 		if (x & UHCI_PORTSC_CCS)
3346 			status |= UPS_CURRENT_CONNECT_STATUS;
3347 		if (x & UHCI_PORTSC_CSC)
3348 			change |= UPS_C_CONNECT_STATUS;
3349 		if (x & UHCI_PORTSC_PE)
3350 			status |= UPS_PORT_ENABLED;
3351 		if (x & UHCI_PORTSC_POEDC)
3352 			change |= UPS_C_PORT_ENABLED;
3353 		if (x & UHCI_PORTSC_OCI)
3354 			status |= UPS_OVERCURRENT_INDICATOR;
3355 		if (x & UHCI_PORTSC_OCIC)
3356 			change |= UPS_C_OVERCURRENT_INDICATOR;
3357 		if (x & UHCI_PORTSC_SUSP)
3358 			status |= UPS_SUSPEND;
3359 		if (x & UHCI_PORTSC_LSDA)
3360 			status |= UPS_LOW_SPEED;
3361 		status |= UPS_PORT_POWER;
3362 		if (sc->sc_isreset)
3363 			change |= UPS_C_PORT_RESET;
3364 		USETW(ps.wPortStatus, status);
3365 		USETW(ps.wPortChange, change);
3366 		l = min(len, sizeof ps);
3367 		memcpy(buf, &ps, l);
3368 		totlen = l;
3369 		break;
3370 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3371 		err = USBD_IOERROR;
3372 		goto ret;
3373 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3374 		break;
3375 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3376 		if (index == 1)
3377 			port = UHCI_PORTSC1;
3378 		else if (index == 2)
3379 			port = UHCI_PORTSC2;
3380 		else {
3381 			err = USBD_IOERROR;
3382 			goto ret;
3383 		}
3384 		switch(value) {
3385 		case UHF_PORT_ENABLE:
3386 			x = URWMASK(UREAD2(sc, port));
3387 			UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3388 			break;
3389 		case UHF_PORT_SUSPEND:
3390 			x = URWMASK(UREAD2(sc, port));
3391 			UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
3392 			break;
3393 		case UHF_PORT_RESET:
3394 			err = uhci_portreset(sc, index);
3395 			goto ret;
3396 		case UHF_PORT_POWER:
3397 			/* Pretend we turned on power */
3398 			err = USBD_NORMAL_COMPLETION;
3399 			goto ret;
3400 		case UHF_C_PORT_CONNECTION:
3401 		case UHF_C_PORT_ENABLE:
3402 		case UHF_C_PORT_OVER_CURRENT:
3403 		case UHF_PORT_CONNECTION:
3404 		case UHF_PORT_OVER_CURRENT:
3405 		case UHF_PORT_LOW_SPEED:
3406 		case UHF_C_PORT_SUSPEND:
3407 		case UHF_C_PORT_RESET:
3408 		default:
3409 			err = USBD_IOERROR;
3410 			goto ret;
3411 		}
3412 		break;
3413 	default:
3414 		err = USBD_IOERROR;
3415 		goto ret;
3416 	}
3417 	xfer->actlen = totlen;
3418 	err = USBD_NORMAL_COMPLETION;
3419  ret:
3420 	xfer->status = err;
3421 	s = splusb();
3422 	usb_transfer_complete(xfer);
3423 	splx(s);
3424 	return (USBD_IN_PROGRESS);
3425 }
3426 
3427 /* Abort a root control request. */
3428 void
uhci_root_ctrl_abort(usbd_xfer_handle xfer)3429 uhci_root_ctrl_abort(usbd_xfer_handle xfer)
3430 {
3431 	/* Nothing to do, all transfers are synchronous. */
3432 }
3433 
3434 /* Close the root pipe. */
3435 void
uhci_root_ctrl_close(usbd_pipe_handle pipe)3436 uhci_root_ctrl_close(usbd_pipe_handle pipe)
3437 {
3438 	DPRINTF(("uhci_root_ctrl_close\n"));
3439 }
3440 
3441 /* Abort a root interrupt request. */
3442 void
uhci_root_intr_abort(usbd_xfer_handle xfer)3443 uhci_root_intr_abort(usbd_xfer_handle xfer)
3444 {
3445 	uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3446 
3447 	usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, xfer);
3448 	sc->sc_intr_xfer = NULL;
3449 
3450 	if (xfer->pipe->intrxfer == xfer) {
3451 		DPRINTF(("uhci_root_intr_abort: remove\n"));
3452 		xfer->pipe->intrxfer = 0;
3453 	}
3454 	xfer->status = USBD_CANCELLED;
3455 #ifdef DIAGNOSTIC
3456 	UXFER(xfer)->iinfo.isdone = 1;
3457 #endif
3458 	usb_transfer_complete(xfer);
3459 }
3460 
3461 usbd_status
uhci_root_intr_transfer(usbd_xfer_handle xfer)3462 uhci_root_intr_transfer(usbd_xfer_handle xfer)
3463 {
3464 	usbd_status err;
3465 
3466 	/* Insert last in queue. */
3467 	err = usb_insert_transfer(xfer);
3468 	if (err)
3469 		return (err);
3470 
3471 	/* Pipe isn't running (otherwise err would be USBD_INPROG),
3472 	 * start first
3473 	 */
3474 	return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3475 }
3476 
3477 /* Start a transfer on the root interrupt pipe */
3478 usbd_status
uhci_root_intr_start(usbd_xfer_handle xfer)3479 uhci_root_intr_start(usbd_xfer_handle xfer)
3480 {
3481 	usbd_pipe_handle pipe = xfer->pipe;
3482 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3483 
3484 	DPRINTFN(3, ("uhci_root_intr_start: xfer=%p len=%d flags=%d\n",
3485 		     xfer, xfer->length, xfer->flags));
3486 
3487 	if (sc->sc_dying)
3488 		return (USBD_IOERROR);
3489 
3490 	sc->sc_ival = mstohz(xfer->pipe->endpoint->edesc->bInterval);
3491 	usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
3492 	sc->sc_intr_xfer = xfer;
3493 	return (USBD_IN_PROGRESS);
3494 }
3495 
3496 /* Close the root interrupt pipe. */
3497 void
uhci_root_intr_close(usbd_pipe_handle pipe)3498 uhci_root_intr_close(usbd_pipe_handle pipe)
3499 {
3500 	uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3501 
3502 	usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, sc->sc_intr_xfer);
3503 	sc->sc_intr_xfer = NULL;
3504 	DPRINTF(("uhci_root_intr_close\n"));
3505 }
3506