Home | History | Annotate | Line # | Download | only in dev
ahci.c revision 1.12.6.11
      1 /*	$NetBSD: ahci.c,v 1.12.6.11 2014/12/05 09:37:49 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2007 Ruslan Ermilov and Vsevolod Lobko.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or
      8  * without modification, are permitted provided that the following
      9  * conditions are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above
     13  *    copyright notice, this list of conditions and the following
     14  *    disclaimer in the documentation and/or other materials provided
     15  *    with the distribution.
     16  * 3. The names of the authors may not be used to endorse or promote
     17  *    products derived from this software without specific prior
     18  *    written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY
     21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     25  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
     27  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
     29  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
     31  * OF SUCH DAMAGE.
     32  */
     33 /*
     34  * Copyright (c) 2001 The NetBSD Foundation, Inc.
     35  * All rights reserved.
     36  *
     37  * This code is derived from software contributed to The NetBSD Foundation
     38  * by Tetsuya Isaki.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     50  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     51  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     52  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     53  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     54  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     55  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     56  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     57  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     58  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     59  * POSSIBILITY OF SUCH DAMAGE.
     60  */
     61 
     62 /*
     63  * !! HIGHLY EXPERIMENTAL CODE !!
     64  */
     65 
     66 #include <sys/cdefs.h>
     67 __KERNEL_RCSID(0, "$NetBSD: ahci.c,v 1.12.6.11 2014/12/05 09:37:49 skrll Exp $");
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/kernel.h>
     72 #include <sys/proc.h>
     73 #include <sys/device.h>
     74 #include <sys/kmem.h>
     75 
     76 #include <sys/bus.h>
     77 #include <machine/cpu.h>
     78 
     79 #include <dev/usb/usb.h>
     80 #include <dev/usb/usbdi.h>
     81 #include <dev/usb/usbdivar.h>
     82 #include <dev/usb/usb_mem.h>
     83 #include <dev/usb/usbdevs.h>
     84 #include <dev/usb/usbroothub.h>
     85 
     86 #include <mips/adm5120/include/adm5120reg.h>
     87 #include <mips/adm5120/include/adm5120var.h>
     88 #include <mips/adm5120/include/adm5120_obiovar.h>
     89 
     90 #include <mips/adm5120/dev/ahcireg.h>
     91 #include <mips/adm5120/dev/ahcivar.h>
     92 
     93 static usbd_status	ahci_open(usbd_pipe_handle);
     94 static void		ahci_softintr(void *);
     95 static void		ahci_poll(struct usbd_bus *);
     96 static void		ahci_poll_hub(void *);
     97 static void		ahci_poll_device(void *arg);
     98 static usbd_xfer_handle ahci_allocx(struct usbd_bus *);
     99 static void		ahci_freex(struct usbd_bus *, usbd_xfer_handle);
    100 
    101 static void		ahci_get_lock(struct usbd_bus *, kmutex_t **);
    102 static int		ahci_roothub_ctrl(struct usbd_bus *, usb_device_request_t *,
    103     void *, int);
    104 
    105 static usbd_status	ahci_root_intr_transfer(usbd_xfer_handle);
    106 static usbd_status	ahci_root_intr_start(usbd_xfer_handle);
    107 static void		ahci_root_intr_abort(usbd_xfer_handle);
    108 static void		ahci_root_intr_close(usbd_pipe_handle);
    109 static void		ahci_root_intr_done(usbd_xfer_handle);
    110 
    111 static usbd_status	ahci_device_ctrl_transfer(usbd_xfer_handle);
    112 static usbd_status	ahci_device_ctrl_start(usbd_xfer_handle);
    113 static void		ahci_device_ctrl_abort(usbd_xfer_handle);
    114 static void		ahci_device_ctrl_close(usbd_pipe_handle);
    115 static void		ahci_device_ctrl_done(usbd_xfer_handle);
    116 
    117 static usbd_status	ahci_device_intr_transfer(usbd_xfer_handle);
    118 static usbd_status	ahci_device_intr_start(usbd_xfer_handle);
    119 static void		ahci_device_intr_abort(usbd_xfer_handle);
    120 static void		ahci_device_intr_close(usbd_pipe_handle);
    121 static void		ahci_device_intr_done(usbd_xfer_handle);
    122 
    123 static usbd_status	ahci_device_isoc_transfer(usbd_xfer_handle);
    124 static usbd_status	ahci_device_isoc_start(usbd_xfer_handle);
    125 static void		ahci_device_isoc_abort(usbd_xfer_handle);
    126 static void		ahci_device_isoc_close(usbd_pipe_handle);
    127 static void		ahci_device_isoc_done(usbd_xfer_handle);
    128 
    129 static usbd_status	ahci_device_bulk_transfer(usbd_xfer_handle);
    130 static usbd_status	ahci_device_bulk_start(usbd_xfer_handle);
    131 static void		ahci_device_bulk_abort(usbd_xfer_handle);
    132 static void		ahci_device_bulk_close(usbd_pipe_handle);
    133 static void		ahci_device_bulk_done(usbd_xfer_handle);
    134 
    135 static int		ahci_transaction(struct ahci_softc *,
    136 	usbd_pipe_handle, uint8_t, int, u_char *, uint8_t);
    137 static void		ahci_noop(usbd_pipe_handle);
    138 static void		ahci_abort_xfer(usbd_xfer_handle, usbd_status);
    139 static void		ahci_device_clear_toggle(usbd_pipe_handle);
    140 
    141 extern int usbdebug;
    142 extern int uhubdebug;
    143 extern int umassdebug;
    144 int ahci_dummy;
    145 
    146 #define AHCI_DEBUG
    147 
    148 #ifdef AHCI_DEBUG
    149 #define D_TRACE	(0x0001)	/* function trace */
    150 #define D_MSG	(0x0002)	/* debug messages */
    151 #define D_XFER	(0x0004)	/* transfer messages (noisy!) */
    152 #define D_MEM	(0x0008)	/* memory allocation */
    153 
    154 int ahci_debug = 0;
    155 #define DPRINTF(z,x)	if((ahci_debug&(z))!=0)printf x
    156 void		print_req(usb_device_request_t *);
    157 void		print_req_hub(usb_device_request_t *);
    158 void		print_dumpreg(struct ahci_softc *);
    159 void		print_xfer(usbd_xfer_handle);
    160 #else
    161 #define DPRINTF(z,x)
    162 #endif
    163 
    164 
    165 struct usbd_bus_methods ahci_bus_methods = {
    166 	.ubm_open = ahci_open,
    167 	.ubm_softint = ahci_softintr,
    168 	.ubm_dopoll = ahci_poll,
    169 	.ubm_allocx = ahci_allocx,
    170 	.ubm_freex = ahci_freex,
    171 	.ubm_getlock = ahci_get_lock,
    172 	.ubm_rhctrl = ahci_roothub_ctrl,
    173 };
    174 
    175 struct usbd_pipe_methods ahci_root_intr_methods = {
    176 	.upm_transfer = ahci_root_intr_transfer,
    177 	.upm_start = ahci_root_intr_start,
    178 	.upm_abort = ahci_root_intr_abort,
    179 	.upm_close = ahci_root_intr_close,
    180 	.upm_cleartoggle = ahci_noop,
    181 	.upm_done = ahci_root_intr_done,
    182 };
    183 
    184 struct usbd_pipe_methods ahci_device_ctrl_methods = {
    185 	.upm_transfer = ahci_device_ctrl_transfer,
    186 	.upm_start = ahci_device_ctrl_start,
    187 	.upm_abort = ahci_device_ctrl_abort,
    188 	.upm_close = ahci_device_ctrl_close,
    189 	.upm_cleartoggle = ahci_noop,
    190 	.upm_done = ahci_device_ctrl_done,
    191 };
    192 
    193 struct usbd_pipe_methods ahci_device_intr_methods = {
    194 	.upm_transfer = ahci_device_intr_transfer,
    195 	.upm_start = ahci_device_intr_start,
    196 	.upm_abort = ahci_device_intr_abort,
    197 	.upm_close = ahci_device_intr_close,
    198 	.upm_cleartoggle = ahci_device_clear_toggle,
    199 	.upm_done = ahci_device_intr_done,
    200 };
    201 
    202 struct usbd_pipe_methods ahci_device_isoc_methods = {
    203 	.upm_transfer = ahci_device_isoc_transfer,
    204 	.upm_start = ahci_device_isoc_start,
    205 	.upm_abort = ahci_device_isoc_abort,
    206 	.upm_close = ahci_device_isoc_close,
    207 	.upm_cleartoggle = ahci_noop,
    208 	.upm_done = ahci_device_isoc_done,
    209 };
    210 
    211 struct usbd_pipe_methods ahci_device_bulk_methods = {
    212 	.upm_transfer = ahci_device_bulk_transfer,
    213 	.upm_start = ahci_device_bulk_start,
    214 	.upm_abort = ahci_device_bulk_abort,
    215 	.upm_close = ahci_device_bulk_close,
    216 	.upm_cleartoggle = ahci_device_clear_toggle,
    217 	.upm_done = ahci_device_bulk_done,
    218 };
    219 
    220 struct ahci_pipe {
    221 	struct usbd_pipe pipe;
    222 	uint32_t toggle;
    223 };
    224 
    225 static int	ahci_match(device_t, cfdata_t, void *);
    226 static void	ahci_attach(device_t, device_t, void *);
    227 
    228 CFATTACH_DECL_NEW(ahci, sizeof(struct ahci_softc),
    229     ahci_match, ahci_attach, NULL, NULL);
    230 
    231 static int
    232 ahci_match(device_t parent, struct cfdata *cf, void *aux)
    233 {
    234 	struct obio_attach_args *aa = aux;
    235 
    236 	if (strcmp(aa->oba_name, cf->cf_name) == 0)
    237 		return 1;
    238 
    239 	return 0;
    240 }
    241 
    242 #define	REG_READ(o)	bus_space_read_4(sc->sc_st, sc->sc_ioh, (o))
    243 #define	REG_WRITE(o,v)	bus_space_write_4(sc->sc_st, sc->sc_ioh, (o),(v))
    244 
    245 /*
    246  * Attach SL11H/SL811HS. Return 0 if success.
    247  */
    248 void
    249 ahci_attach(device_t parent, device_t self, void *aux)
    250 {
    251 	struct obio_attach_args *aa = aux;
    252 	struct ahci_softc *sc = device_private(self);
    253 
    254 	printf("\n");
    255 	sc->sc_dmat = aa->oba_dt;
    256 	sc->sc_st = aa->oba_st;
    257 
    258 	/* Initialize sc */
    259 	sc->sc_bus.ub_revision = USBREV_1_1;
    260 	sc->sc_bus.ub_methods = &ahci_bus_methods;
    261 	sc->sc_bus.ub_pipesize = sizeof(struct ahci_pipe);
    262 	sc->sc_bus.ub_dmatag = sc->sc_dmat;
    263 	sc->sc_bus.ub_usedma = true;
    264 
    265 	/* Map the device. */
    266 	if (bus_space_map(sc->sc_st, aa->oba_addr,
    267 	    512, 0, &sc->sc_ioh) != 0) {
    268 		aprint_error_dev(self, "unable to map device\n");
    269 		return;
    270 	}
    271 
    272 	/* Hook up the interrupt handler. */
    273 	sc->sc_ih = adm5120_intr_establish(aa->oba_irq, INTR_IRQ, ahci_intr, sc);
    274 
    275 	if (sc->sc_ih == NULL) {
    276 		aprint_error_dev(self,
    277 		    "unable to register interrupt handler\n");
    278 		return;
    279 	}
    280 
    281 	SIMPLEQ_INIT(&sc->sc_free_xfers);
    282 
    283 	callout_init(&sc->sc_poll_handle, 0);
    284 
    285 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    286 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED /* XXXNH */);
    287 
    288 	REG_WRITE(ADMHCD_REG_INTENABLE, 0); /* disable interrupts */
    289 	REG_WRITE(ADMHCD_REG_CONTROL, ADMHCD_SW_RESET); /* reset */
    290 	delay_ms(10);
    291 	while (REG_READ(ADMHCD_REG_CONTROL) & ADMHCD_SW_RESET)
    292 		delay_ms(1);
    293 
    294 	REG_WRITE(ADMHCD_REG_CONTROL, ADMHCD_HOST_EN);
    295 	REG_WRITE(ADMHCD_REG_HOSTHEAD, 0x00000000);
    296 	REG_WRITE(ADMHCD_REG_FMINTERVAL, 0x20002edf);
    297 	REG_WRITE(ADMHCD_REG_LSTHRESH, 0x628);
    298 	REG_WRITE(ADMHCD_REG_RHDESCR, ADMHCD_NPS | ADMHCD_LPSC);
    299 	REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP);
    300 
    301 	REG_WRITE(ADMHCD_REG_INTENABLE, 0); /* XXX: enable interrupts */
    302 
    303 #ifdef USB_DEBUG
    304 	/* usbdebug = 0x7f;
    305 	uhubdebug = 0x7f;
    306 	umassdebug = 0xffffffff; */
    307 #endif
    308 
    309 	/* Attach USB devices */
    310 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
    311 
    312 }
    313 
    314 int
    315 ahci_intr(void *arg)
    316 {
    317 #if 0
    318 	struct ahci_softc *sc = arg;
    319 	uint8_t r;
    320 #ifdef AHCI_DEBUG
    321 	char bitbuf[256];
    322 #endif
    323 
    324 	r = sl11read(sc, SL11_ISR);
    325 
    326 	sl11write(sc, SL11_ISR, SL11_ISR_DATA | SL11_ISR_SOFTIMER);
    327 
    328 	if ((r & SL11_ISR_RESET)) {
    329 		sc->sc_flags |= AHCDF_RESET;
    330 		sl11write(sc, SL11_ISR, SL11_ISR_RESET);
    331 	}
    332 	if ((r & SL11_ISR_INSERT)) {
    333 		sc->sc_flags |= AHCDF_INSERT;
    334 		sl11write(sc, SL11_ISR, SL11_ISR_INSERT);
    335 	}
    336 
    337 #ifdef AHCI_DEBUG
    338 	snprintb(bitbuf, sizeof(bitbuf),
    339 	    ((sl11read(sc, SL11_CTRL) & SL11_CTRL_SUSPEND)
    340 	    ? "\20\x8""D+\7RESUME\6INSERT\5SOF\4res\3""BABBLE\2USBB\1USBA"
    341 	    : "\20\x8""D+\7RESET\6INSERT\5SOF\4res\3""BABBLE\2USBB\1USBA"),
    342 	    r);
    343 
    344 	DPRINTF(D_XFER, ("I=%s ", bitbuf));
    345 #endif /* AHCI_DEBUG */
    346 #endif
    347 
    348 	return 0;
    349 }
    350 
    351 usbd_status
    352 ahci_open(usbd_pipe_handle pipe)
    353 {
    354 	usbd_device_handle dev = pipe->up_dev;
    355 	struct ahci_pipe *apipe = (struct ahci_pipe *)pipe;
    356 	usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
    357 	uint8_t rhaddr = dev->ud_bus->ub_rhaddr;
    358 
    359 	DPRINTF(D_TRACE, ("ahci_open(addr=%d,ep=%d,scaddr=%d)",
    360 		dev->ud_addr, ed->bEndpointAddress, rhaddr));
    361 
    362 	apipe->toggle=0;
    363 
    364 	if (dev->ud_addr == rhaddr) {
    365 		switch (ed->bEndpointAddress) {
    366 		case USB_CONTROL_ENDPOINT:
    367 			pipe->up_methods = &roothub_ctrl_methods;
    368 			break;
    369 		case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
    370 			pipe->up_methods = &ahci_root_intr_methods;
    371 			break;
    372 		default:
    373 			printf("open:endpointErr!\n");
    374 			return USBD_INVAL;
    375 		}
    376 	} else {
    377 		switch (ed->bmAttributes & UE_XFERTYPE) {
    378 		case UE_CONTROL:
    379 			DPRINTF(D_MSG, ("control "));
    380 			pipe->up_methods = &ahci_device_ctrl_methods;
    381 			break;
    382 		case UE_INTERRUPT:
    383 			DPRINTF(D_MSG, ("interrupt "));
    384 			pipe->up_methods = &ahci_device_intr_methods;
    385 			break;
    386 		case UE_ISOCHRONOUS:
    387 			DPRINTF(D_MSG, ("isochronous "));
    388 			pipe->up_methods = &ahci_device_isoc_methods;
    389 			break;
    390 		case UE_BULK:
    391 			DPRINTF(D_MSG, ("bluk "));
    392 			pipe->up_methods = &ahci_device_bulk_methods;
    393 			break;
    394 		}
    395 	}
    396 	return USBD_NORMAL_COMPLETION;
    397 }
    398 
    399 void
    400 ahci_softintr(void *arg)
    401 {
    402 	DPRINTF(D_TRACE, ("%s()", __func__));
    403 }
    404 
    405 void
    406 ahci_poll(struct usbd_bus *bus)
    407 {
    408 	DPRINTF(D_TRACE, ("%s()", __func__));
    409 }
    410 
    411 /*
    412  * Emulation of interrupt transfer for status change endpoint
    413  * of root hub.
    414  */
    415 void
    416 ahci_poll_hub(void *arg)
    417 {
    418 	usbd_xfer_handle xfer = arg;
    419 	usbd_pipe_handle pipe = xfer->ux_pipe;
    420 	struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
    421 	u_char *p;
    422 	static int p0_state=0;
    423 	static int p1_state=0;
    424 
    425 	callout_reset(&sc->sc_poll_handle, sc->sc_interval, ahci_poll_hub, xfer);
    426 
    427 	/* USB spec 11.13.3 (p.260) */
    428 	p = KERNADDR(&xfer->ux_dmabuf, 0);
    429 	p[0] = 0;
    430 	if ((REG_READ(ADMHCD_REG_PORTSTATUS0) & ADMHCD_CCS) != p0_state) {
    431 		p[0] = 2;
    432 		DPRINTF(D_TRACE, ("!"));
    433 		p0_state=(REG_READ(ADMHCD_REG_PORTSTATUS0) & ADMHCD_CCS);
    434 	};
    435 	if ((REG_READ(ADMHCD_REG_PORTSTATUS1) & ADMHCD_CCS) != p1_state) {
    436 		p[0] = 2;
    437 		DPRINTF(D_TRACE, ("@"));
    438 		p1_state=(REG_READ(ADMHCD_REG_PORTSTATUS1) & ADMHCD_CCS);
    439 	};
    440 
    441 	/* no change, return NAK */
    442 	if (p[0] == 0)
    443 		return;
    444 
    445 	xfer->ux_actlen = 1;
    446 	xfer->ux_status = USBD_NORMAL_COMPLETION;
    447 	mutex_enter(&sc->sc_lock);
    448 	usb_transfer_complete(xfer);
    449 	mutex_exit(&sc->sc_lock);
    450 }
    451 
    452 usbd_xfer_handle
    453 ahci_allocx(struct usbd_bus *bus)
    454 {
    455 	struct ahci_softc *sc = (struct ahci_softc *)bus;
    456 	usbd_xfer_handle xfer;
    457 
    458 	DPRINTF(D_MEM, ("SLallocx"));
    459 
    460 	xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
    461 	if (xfer) {
    462 		SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, ux_next);
    463 #ifdef DIAGNOSTIC
    464 		if (xfer->ux_state != XFER_FREE) {
    465 			printf("ahci_allocx: xfer=%p not free, 0x%08x\n",
    466 				xfer, xfer->ux_state);
    467 		}
    468 #endif
    469 	} else {
    470 		xfer = kmem_alloc(sizeof(*xfer), KM_SLEEP);
    471 	}
    472 
    473 	if (xfer) {
    474 		memset(xfer, 0, sizeof(*xfer));
    475 #ifdef DIAGNOSTIC
    476 		xfer->ux_state = XFER_BUSY;
    477 #endif
    478 	}
    479 
    480 	return xfer;
    481 }
    482 
    483 void
    484 ahci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
    485 {
    486 	struct ahci_softc *sc = (struct ahci_softc *)bus;
    487 
    488 	DPRINTF(D_MEM, ("SLfreex"));
    489 
    490 #ifdef DIAGNOSTIC
    491 	if (xfer->ux_state != XFER_BUSY) {
    492 		printf("ahci_freex: xfer=%p not busy, 0x%08x\n",
    493 			xfer, xfer->ux_state);
    494 		return;
    495 	}
    496 	xfer->ux_state = XFER_FREE;
    497 #endif
    498 	SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, ux_next);
    499 }
    500 
    501 static void
    502 ahci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
    503 {
    504 	struct ahci_softc *sc = bus->ub_hcpriv;
    505 
    506 	*lock = &sc->sc_lock;
    507 }
    508 
    509 void
    510 ahci_noop(usbd_pipe_handle pipe)
    511 {
    512 	DPRINTF(D_TRACE, ("%s()", __func__));
    513 }
    514 
    515 /*
    516  * Data structures and routines to emulate the root hub.
    517  */
    518 
    519 static int
    520 ahci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
    521     void *buf, int buflen)
    522 {
    523 	struct ahci_softc *sc = bus->ub_hcpriv;
    524 	uint16_t len, value, index;
    525 	usb_port_status_t ps;
    526 	int totlen = 0;
    527 	int status;
    528 
    529 	DPRINTF(D_TRACE, ("SLRCstart "));
    530 
    531 	len = UGETW(req->wLength);
    532 	value = UGETW(req->wValue);
    533 	index = UGETW(req->wIndex);
    534 
    535 #define C(x,y) ((x) | ((y) << 8))
    536 	switch (C(req->bRequest, req->bmRequestType)) {
    537 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
    538 		switch (value) {
    539 		case C(0, UDESC_DEVICE): {
    540 			usb_device_descriptor_t devd;
    541 
    542 			DPRINTF(D_MSG, ("UDESC_DEVICE "));
    543 			totlen = min(buflen, sizeof(devd));
    544 			memcpy(&devd, buf, totlen);
    545 			USETW(devd.idVendor, USB_VENDOR_SCANLOGIC);
    546 			memcpy(buf, &devd, totlen);
    547 			break;
    548 		}
    549 #define sd ((usb_string_descriptor_t *)buf)
    550 		case C(1, UDESC_STRING):
    551 			/* Vendor */
    552 			totlen = usb_makestrdesc(sd, len, "ADMTek");
    553 			break;
    554 		case C(2, UDESC_STRING):
    555 			/* Product */
    556 			totlen = usb_makestrdesc(sd, len, "ADM5120 root hub");
    557 			break;
    558 		default:
    559 			printf("unknownGetDescriptor=%x", value);
    560 			/* default from usbroothub */
    561 			return buflen;
    562 		}
    563 		break;
    564 	/*
    565 	 * Hub specific requests
    566 	 */
    567 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
    568 		/* Clear Hub Feature, 11.16.2.1, not supported */
    569 		DPRINTF(D_MSG, ("ClearHubFeature not supported\n"));
    570 		break;
    571 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
    572 
    573 #define WPS(x) REG_WRITE(ADMHCD_REG_PORTSTATUS0+(index-1)*4, (x))
    574 		/* Clear Port Feature, 11.16.2.2 */
    575 		if (index != 1 && index != 2 ) {
    576 			return -1;
    577 		}
    578 		switch (value) {
    579 		case UHF_PORT_POWER:
    580 			DPRINTF(D_MSG, ("POWER_OFF "));
    581 			WPS(ADMHCD_LSDA);
    582 			break;
    583 		case UHF_PORT_SUSPEND:
    584 			DPRINTF(D_MSG, ("SUSPEND "));
    585 			WPS(ADMHCD_POCI);
    586 			break;
    587 		case UHF_PORT_ENABLE:
    588 			DPRINTF(D_MSG, ("ENABLE "));
    589 			WPS(ADMHCD_CCS);
    590 			break;
    591 		case UHF_C_PORT_CONNECTION:
    592 			WPS(ADMHCD_CSC);
    593 			break;
    594 		case UHF_C_PORT_RESET:
    595 			WPS(ADMHCD_PRSC);
    596 			break;
    597 		case UHF_C_PORT_SUSPEND:
    598 			WPS(ADMHCD_PSSC);
    599 			break;
    600 		case UHF_C_PORT_ENABLE:
    601 			WPS(ADMHCD_PESC);
    602 			break;
    603 		case UHF_C_PORT_OVER_CURRENT:
    604 			WPS(ADMHCD_OCIC);
    605 			break;
    606 		default:
    607 			printf("ClrPortFeatERR:value=0x%x ", value);
    608 			return -1;
    609 		}
    610 		//DPRINTF(D_XFER, ("CH=%04x ", sc->sc_change));
    611 #undef WPS
    612 		break;
    613 	case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
    614 		/* Get Bus State, 11.16.2.3, not supported */
    615 		/* shall return a STALL... */
    616 		break;
    617 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
    618 		/* Get Hub Descriptor, 11.16.2.4 */
    619 		DPRINTF(D_MSG, ("UR_GET_DESCRIPTOR RCD"));
    620 		if ((value&0xff) != 0) {
    621 			return -1;
    622 		}
    623 		usb_hub_descriptor_t hubd;
    624 
    625 		totlen = min(buflen, sizeof(hubd));
    626 		memcpy(&hubd, buf, totlen);
    627 		hubd.bNbrPorts = 2;
    628 		USETW(hubd.wHubCharacteristics, 0);
    629 		hubd.bPwrOn2PwrGood = 0;
    630 		memcpy(buf, &hubd, totlen);
    631 		break;
    632 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
    633 		/* Get Hub Status, 11.16.2.5 */
    634 		DPRINTF(D_MSG, ("UR_GET_STATUS RCD"));
    635 		if (len != 4) {
    636 			return -1;
    637 		}
    638 		memset(buf, 0, len);
    639 		totlen = len;
    640 		break;
    641 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
    642 		/* Get Port Status, 11.16.2.6 */
    643 		if ((index != 1 && index != 2)  || len != 4) {
    644 			printf("index=%d,len=%d ", index, len);
    645 			return -1;
    646 		}
    647 		status = REG_READ(ADMHCD_REG_PORTSTATUS0+(index-1)*4);
    648 		DPRINTF(D_MSG, ("UR_GET_STATUS RCO=%x ", status));
    649 
    650 		//DPRINTF(D_XFER, ("ST=%04x,CH=%04x ", status, sc->sc_change));
    651 		USETW(ps.wPortStatus, status  & (UPS_CURRENT_CONNECT_STATUS|UPS_PORT_ENABLED|UPS_SUSPEND|UPS_OVERCURRENT_INDICATOR|UPS_RESET|UPS_PORT_POWER|UPS_LOW_SPEED));
    652 		USETW(ps.wPortChange, (status>>16) & (UPS_C_CONNECT_STATUS|UPS_C_PORT_ENABLED|UPS_C_SUSPEND|UPS_C_OVERCURRENT_INDICATOR|UPS_C_PORT_RESET));
    653 		totlen = min(len, sizeof(ps));
    654 		memcpy(buf, &ps, totlen);
    655 		break;
    656 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
    657 		/* Set Hub Descriptor, 11.16.2.7, not supported */
    658 		/* STALL ? */
    659 		return -1;
    660 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
    661 		/* Set Hub Feature, 11.16.2.8, not supported */
    662 		break;
    663 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
    664 #define WPS(x) REG_WRITE(ADMHCD_REG_PORTSTATUS0+(index-1)*4, (x))
    665 		/* Set Port Feature, 11.16.2.9 */
    666 		if ((index != 1) && (index !=2)) {
    667 			printf("index=%d ", index);
    668 			return -1;
    669 		}
    670 		switch (value) {
    671 		case UHF_PORT_RESET:
    672 			DPRINTF(D_MSG, ("PORT_RESET "));
    673 			WPS(ADMHCD_PRS);
    674 			break;
    675 		case UHF_PORT_POWER:
    676 			DPRINTF(D_MSG, ("PORT_POWER "));
    677 			WPS(ADMHCD_PPS);
    678 			break;
    679 		case UHF_PORT_ENABLE:
    680 			DPRINTF(D_MSG, ("PORT_ENABLE "));
    681 			WPS(ADMHCD_PES);
    682 			break;
    683 		default:
    684 			printf("SetPortFeatERR=0x%x ", value);
    685 			return -1;
    686 		}
    687 #undef WPS
    688 		break;
    689 	default:
    690 		DPRINTF(D_MSG, ("ioerr(UR=%02x,UT=%02x) ",
    691 			req->bRequest, req->bmRequestType));
    692 		/* default from usbroothub */
    693 		return buflen;
    694 	}
    695 
    696 	return totlen;
    697 }
    698 
    699 static usbd_status
    700 ahci_root_intr_transfer(usbd_xfer_handle xfer)
    701 {
    702 	struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
    703 	usbd_status error;
    704 
    705 	DPRINTF(D_TRACE, ("SLRItransfer "));
    706 
    707 	/* Insert last in queue */
    708 	mutex_enter(&sc->sc_lock);
    709 	error = usb_insert_transfer(xfer);
    710 	mutex_exit(&sc->sc_lock);
    711 	if (error)
    712 		return error;
    713 
    714 	/*
    715 	 * Pipe isn't running (otherwise error would be USBD_INPROG),
    716 	 * start first.
    717 	 */
    718 	return ahci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
    719 }
    720 
    721 static usbd_status
    722 ahci_root_intr_start(usbd_xfer_handle xfer)
    723 {
    724 	usbd_pipe_handle pipe = xfer->ux_pipe;
    725 	struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
    726 
    727 	DPRINTF(D_TRACE, ("SLRIstart "));
    728 
    729 	sc->sc_interval = MS_TO_TICKS(xfer->ux_pipe->up_endpoint->ue_edesc->bInterval);
    730 	callout_reset(&sc->sc_poll_handle, sc->sc_interval, ahci_poll_hub, xfer);
    731 	sc->sc_intr_xfer = xfer;
    732 	return USBD_IN_PROGRESS;
    733 }
    734 
    735 static void
    736 ahci_root_intr_abort(usbd_xfer_handle xfer)
    737 {
    738 	DPRINTF(D_TRACE, ("SLRIabort "));
    739 }
    740 
    741 static void
    742 ahci_root_intr_close(usbd_pipe_handle pipe)
    743 {
    744 	struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
    745 
    746 	DPRINTF(D_TRACE, ("SLRIclose "));
    747 
    748 	callout_stop(&sc->sc_poll_handle);
    749 	sc->sc_intr_xfer = NULL;
    750 }
    751 
    752 static void
    753 ahci_root_intr_done(usbd_xfer_handle xfer)
    754 {
    755 	//DPRINTF(D_XFER, ("RIdn "));
    756 }
    757 
    758 static usbd_status
    759 ahci_device_ctrl_transfer(usbd_xfer_handle xfer)
    760 {
    761 	struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
    762 	usbd_status error;
    763 
    764 	DPRINTF(D_TRACE, ("C"));
    765 
    766 	mutex_enter(&sc->sc_lock);
    767 	error = usb_insert_transfer(xfer);
    768 	mutex_exit(&sc->sc_lock);
    769 	if (error)
    770 		return error;
    771 
    772 	return ahci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
    773 }
    774 
    775 static usbd_status
    776 ahci_device_ctrl_start(usbd_xfer_handle xfer)
    777 {
    778 	usbd_status status =  USBD_NORMAL_COMPLETION;
    779 	int s, err;
    780 	static struct admhcd_ed ep_v __attribute__((aligned(16))), *ep;
    781 	static struct admhcd_td td_v[4] __attribute__((aligned(16))), *td, *td1, *td2, *td3;
    782 	static usb_dma_t reqdma;
    783 	usbd_pipe_handle pipe = xfer->ux_pipe;
    784 	usb_device_request_t *req = &xfer->ux_request;
    785 	struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
    786 	int len, isread;
    787 
    788 
    789 #if 0
    790 	struct ahci_pipe *apipe = (struct ahci_pipe *)xfer->ux_pipe;
    791 #endif
    792 	mutex_enter(&sc->sc_lock);
    793 /*	printf("ctrl_start>>>\n"); */
    794 
    795 #ifdef DIAGNOSTIC
    796 	if (!(xfer->ux_rqflags & URQ_REQUEST)) {
    797 		/* XXX panic */
    798 		printf("ahci_device_ctrl_transfer: not a request\n");
    799 		return USBD_INVAL;
    800 	}
    801 #endif
    802 
    803 #define KSEG1ADDR(x) (0xa0000000 | (((uint32_t)x) & 0x1fffffff))
    804 	DPRINTF(D_TRACE, ("st "));
    805 	if (!ep) {
    806 		ep = (struct admhcd_ed *)KSEG1ADDR(&ep_v);
    807 		td = (struct admhcd_td *)KSEG1ADDR(&td_v[0]);
    808 		td1 = (struct admhcd_td *)KSEG1ADDR(&td_v[1]);
    809 		td2 = (struct admhcd_td *)KSEG1ADDR(&td_v[2]);
    810 		td3 = (struct admhcd_td *)KSEG1ADDR(&td_v[3]);
    811 		err = usb_allocmem(&sc->sc_bus,
    812 			sizeof(usb_device_request_t),
    813 			0, &reqdma);
    814 		if (err)
    815 			return USBD_NOMEM;
    816 
    817 		/* printf("ep: %p\n",ep); */
    818 	};
    819 
    820 	ep->control =  pipe->up_dev->ud_addr | \
    821 		((pipe->up_dev->ud_speed==USB_SPEED_FULL)?ADMHCD_ED_SPEED:0) | \
    822 		((UGETW(pipe->up_endpoint->ue_edesc->wMaxPacketSize))<<ADMHCD_ED_MAXSHIFT);
    823 	memcpy(KERNADDR(&reqdma, 0), req, sizeof *req);
    824 /* 	printf("status: %x\n",REG_READ(ADMHCD_REG_PORTSTATUS0));
    825 	printf("ep_control: %x\n",ep->control);
    826 	printf("speed: %x\n",pipe->up_dev->ud_speed);
    827 	printf("req: %p\n",req);
    828 	printf("dmabuf: %p\n",xfer->ux_dmabuf.block); */
    829 
    830 	isread = req->bmRequestType & UT_READ;
    831 	len = UGETW(req->wLength);
    832 
    833 	ep->next = ep;
    834 
    835 	td->buffer = DMAADDR(&reqdma,0) | 0xa0000000;
    836 	td->buflen=sizeof(*req);
    837 	td->control=ADMHCD_TD_SETUP | ADMHCD_TD_DATA0 | ADMHCD_TD_OWN;
    838 
    839 	if (len) {
    840 		td->next = td1;
    841 
    842 		td1->buffer = DMAADDR(&xfer->ux_dmabuf,0) | 0xa0000000;
    843 		td1->buflen = len;
    844 		td1->next = td2;
    845 		td1->control= (isread?ADMHCD_TD_IN:ADMHCD_TD_OUT) | ADMHCD_TD_DATA1 | ADMHCD_TD_R | ADMHCD_TD_OWN;
    846 	} else {
    847 		td1->control = 0;
    848 		td->next = td2;
    849 	};
    850 
    851 	td2->buffer = 0;
    852 	td2->buflen= 0;
    853 	td2->next = td3;
    854 	td2->control = (isread?ADMHCD_TD_OUT:ADMHCD_TD_IN) | ADMHCD_TD_DATA1 | ADMHCD_TD_OWN;
    855 
    856 	td3->buffer = 0;
    857 	td3->buflen= 0;
    858 	td3->next = 0;
    859 	td3->control = 0;
    860 
    861 	ep->head = td;
    862 	ep->tail = td3;
    863 /*
    864 	printf("ep: %p\n",ep);
    865 	printf("ep->next: %p\n",ep->next);
    866 	printf("ep->head: %p\n",ep->head);
    867 	printf("ep->tail: %p\n",ep->tail);
    868 	printf("td: %p\n",td);
    869 	printf("td->next: %p\n",td->next);
    870 	printf("td->buffer: %x\n",td->buffer);
    871 	printf("td->buflen: %x\n",td->buflen);
    872 	printf("td1: %p\n",td1);
    873 	printf("td1->next: %p\n",td1->next);
    874 	printf("td2: %p\n",td2);
    875 	printf("td2->next: %p\n",td2->next);
    876 	printf("td3: %p\n",td3);
    877 	printf("td3->next: %p\n",td3->next);
    878 */
    879 
    880 	REG_WRITE(ADMHCD_REG_HOSTHEAD, (uint32_t)ep);
    881 	REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP | ADMHCD_DMA_EN);
    882 /*	printf("1: %x %x %x %x\n", ep->control, td->control, td1->control, td2->control); */
    883 	s=100;
    884 	while (s--) {
    885 		delay_ms(10);
    886 /*                printf("%x %x %x %x\n", ep->control, td->control, td1->control, td2->control);*/
    887 		status = USBD_TIMEOUT;
    888 		if (td->control & ADMHCD_TD_OWN) continue;
    889 
    890 		err = (td->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
    891 		if (err) {
    892 			status = USBD_IOERROR;
    893 			break;
    894 		};
    895 
    896 		status = USBD_TIMEOUT;
    897 		if (td1->control & ADMHCD_TD_OWN) continue;
    898 		err = (td1->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
    899 		if (err) {
    900 			status = USBD_IOERROR;
    901 			break;
    902 		};
    903 
    904 		status = USBD_TIMEOUT;
    905 		if (td2->control & ADMHCD_TD_OWN) continue;
    906 		err = (td2->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
    907 		if (err) {
    908 			status = USBD_IOERROR;
    909 		};
    910 		status = USBD_NORMAL_COMPLETION;
    911 		break;
    912 
    913 	};
    914 	REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP);
    915 
    916 	xfer->ux_actlen = len;
    917 	xfer->ux_status = status;
    918 
    919 /* 	printf("ctrl_start<<<\n"); */
    920 
    921 	usb_transfer_complete(xfer);
    922 	mutex_exit(&sc->sc_lock);
    923 	return USBD_NORMAL_COMPLETION;
    924 }
    925 
    926 static void
    927 ahci_device_ctrl_abort(usbd_xfer_handle xfer)
    928 {
    929 	DPRINTF(D_TRACE, ("Cab "));
    930 	ahci_abort_xfer(xfer, USBD_CANCELLED);
    931 }
    932 
    933 static void
    934 ahci_device_ctrl_close(usbd_pipe_handle pipe)
    935 {
    936 	DPRINTF(D_TRACE, ("Ccl "));
    937 }
    938 
    939 static void
    940 ahci_device_ctrl_done(usbd_xfer_handle xfer)
    941 {
    942 	DPRINTF(D_TRACE, ("Cdn "));
    943 }
    944 
    945 static usbd_status
    946 ahci_device_intr_transfer(usbd_xfer_handle xfer)
    947 {
    948 	struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
    949 	usbd_status error;
    950 
    951 	DPRINTF(D_TRACE, ("INTRtrans "));
    952 
    953 	mutex_enter(&sc->sc_lock);
    954 	error = usb_insert_transfer(xfer);
    955 	mutex_exit(&sc->sc_lock);
    956 	if (error)
    957 		return error;
    958 
    959 	return ahci_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
    960 }
    961 
    962 static usbd_status
    963 ahci_device_intr_start(usbd_xfer_handle xfer)
    964 {
    965 	usbd_pipe_handle pipe = xfer->ux_pipe;
    966 	struct ahci_xfer *sx;
    967 
    968 	DPRINTF(D_TRACE, ("INTRstart "));
    969 
    970 	sx = kmem_intr_alloc(sizeof(*sx), KM_NOSLEEP);
    971 	if (sx == NULL)
    972 		goto reterr;
    973 	memset(sx, 0, sizeof(*sx));
    974 	sx->sx_xfer  = xfer;
    975 	xfer->ux_hcpriv = sx;
    976 
    977 	/* initialize callout */
    978 	callout_init(&sx->sx_callout_t, 0);
    979 	callout_reset(&sx->sx_callout_t,
    980 		MS_TO_TICKS(pipe->up_endpoint->ue_edesc->bInterval),
    981 		ahci_poll_device, sx);
    982 
    983 	/* ACK */
    984 	return USBD_IN_PROGRESS;
    985 
    986  reterr:
    987 	return USBD_IOERROR;
    988 }
    989 
    990 static void
    991 ahci_poll_device(void *arg)
    992 {
    993 	struct ahci_xfer *sx = (struct ahci_xfer *)arg;
    994 	usbd_xfer_handle xfer = sx->sx_xfer;
    995 	usbd_pipe_handle pipe = xfer->ux_pipe;
    996 	struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
    997 	void *buf;
    998 	int pid;
    999 	int r;
   1000 
   1001 	DPRINTF(D_TRACE, ("pldev"));
   1002 
   1003 	callout_reset(&sx->sx_callout_t,
   1004 		MS_TO_TICKS(pipe->up_endpoint->ue_edesc->bInterval),
   1005 		ahci_poll_device, sx);
   1006 
   1007 	/* interrupt transfer */
   1008 	pid = (UE_GET_DIR(pipe->up_endpoint->ue_edesc->bEndpointAddress) == UE_DIR_IN)
   1009 	    ? ADMHCD_TD_IN : ADMHCD_TD_OUT;
   1010 	buf = KERNADDR(&xfer->ux_dmabuf, 0);
   1011 
   1012 	r = ahci_transaction(sc, pipe, pid, xfer->ux_length, buf, 0/*toggle*/);
   1013 	if (r < 0) {
   1014 		DPRINTF(D_MSG, ("%s error", __func__));
   1015 		return;
   1016 	}
   1017 	/* no change, return NAK */
   1018 	if (r == 0)
   1019 		return;
   1020 
   1021 	xfer->ux_status = USBD_NORMAL_COMPLETION;
   1022 	mutex_enter(&sc->sc_lock);
   1023 	usb_transfer_complete(xfer);
   1024 	mutex_exit(&sc->sc_lock);
   1025 }
   1026 
   1027 static void
   1028 ahci_device_intr_abort(usbd_xfer_handle xfer)
   1029 {
   1030 	struct ahci_xfer *sx;
   1031 
   1032 	DPRINTF(D_TRACE, ("INTRabort "));
   1033 
   1034 	sx = xfer->ux_hcpriv;
   1035 	if (sx) {
   1036 		callout_stop(&sx->sx_callout_t);
   1037 		kmem_intr_free(sx, sizeof(*sx));
   1038 		xfer->ux_hcpriv = NULL;
   1039 	} else {
   1040 		printf("%s: sx == NULL!\n", __func__);
   1041 	}
   1042 	ahci_abort_xfer(xfer, USBD_CANCELLED);
   1043 }
   1044 
   1045 static void
   1046 ahci_device_intr_close(usbd_pipe_handle pipe)
   1047 {
   1048 	DPRINTF(D_TRACE, ("INTRclose "));
   1049 }
   1050 
   1051 static void
   1052 ahci_device_intr_done(usbd_xfer_handle xfer)
   1053 {
   1054 	DPRINTF(D_TRACE, ("INTRdone "));
   1055 }
   1056 
   1057 static usbd_status
   1058 ahci_device_isoc_transfer(usbd_xfer_handle xfer)
   1059 {
   1060 	DPRINTF(D_TRACE, ("S"));
   1061 	return USBD_NORMAL_COMPLETION;
   1062 }
   1063 
   1064 static usbd_status
   1065 ahci_device_isoc_start(usbd_xfer_handle xfer)
   1066 {
   1067 	DPRINTF(D_TRACE, ("st "));
   1068 	return USBD_NORMAL_COMPLETION;
   1069 }
   1070 
   1071 static void
   1072 ahci_device_isoc_abort(usbd_xfer_handle xfer)
   1073 {
   1074 	DPRINTF(D_TRACE, ("Sab "));
   1075 }
   1076 
   1077 static void
   1078 ahci_device_isoc_close(usbd_pipe_handle pipe)
   1079 {
   1080 	DPRINTF(D_TRACE, ("Scl "));
   1081 }
   1082 
   1083 static void
   1084 ahci_device_isoc_done(usbd_xfer_handle xfer)
   1085 {
   1086 	DPRINTF(D_TRACE, ("Sdn "));
   1087 }
   1088 
   1089 static usbd_status
   1090 ahci_device_bulk_transfer(usbd_xfer_handle xfer)
   1091 {
   1092 	struct ahci_softc *sc = (struct ahci_softc *)xfer->ux_pipe->up_dev->ud_bus;
   1093 	usbd_status error;
   1094 
   1095 	DPRINTF(D_TRACE, ("B"));
   1096 
   1097 	mutex_enter(&sc->sc_lock);
   1098 	error = usb_insert_transfer(xfer);
   1099 	mutex_exit(&sc->sc_lock);
   1100 	if (error)
   1101 		return error;
   1102 
   1103 	return ahci_device_bulk_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   1104 }
   1105 
   1106 static usbd_status
   1107 ahci_device_bulk_start(usbd_xfer_handle xfer)
   1108 {
   1109 #define NBULK_TDS 32
   1110 	static volatile int level = 0;
   1111 	usbd_status status =  USBD_NORMAL_COMPLETION;
   1112 	int s, err;
   1113 	static struct admhcd_ed ep_v __attribute__((aligned(16))), *ep;
   1114 	static struct admhcd_td td_v[NBULK_TDS] __attribute__((aligned(16))), *td[NBULK_TDS];
   1115 	usbd_pipe_handle pipe = xfer->ux_pipe;
   1116 	struct ahci_softc *sc = (struct ahci_softc *)pipe->up_dev->ud_bus;
   1117 	int endpt, i, len, tlen, segs, offset, isread, toggle, short_ok;
   1118 	struct ahci_pipe *apipe = (struct ahci_pipe *)xfer->ux_pipe;
   1119 
   1120 #define KSEG1ADDR(x) (0xa0000000 | (((uint32_t)x) & 0x1fffffff))
   1121 	DPRINTF(D_TRACE, ("st "));
   1122 
   1123 #ifdef DIAGNOSTIC
   1124 	if (xfer->ux_rqflags & URQ_REQUEST) {
   1125 		/* XXX panic */
   1126 		printf("ohci_device_bulk_start: a request\n");
   1127 		return USBD_INVAL;
   1128 	}
   1129 #endif
   1130 
   1131 	mutex_enter(&sc->sc_lock);
   1132 	level++;
   1133 /* 	printf("bulk_start>>>\n"); */
   1134 
   1135 	if (!ep) {
   1136 		ep = (struct admhcd_ed *)KSEG1ADDR(&ep_v);
   1137 		for (i=0; i<NBULK_TDS; i++) {
   1138 			td[i] = (struct admhcd_td *)KSEG1ADDR(&td_v[i]);
   1139 		};
   1140 /*		printf("ep: %p\n",ep);*/
   1141 	};
   1142 	if (apipe->toggle == 0) {
   1143 		toggle = ADMHCD_TD_DATA0;
   1144 	} else {
   1145 		toggle = apipe->toggle;
   1146 	};
   1147 
   1148 	endpt = pipe->up_endpoint->ue_edesc->bEndpointAddress;
   1149 	ep->control = pipe->up_dev->ud_addr | ((endpt & 0xf) << ADMHCD_ED_EPSHIFT)|\
   1150 		((pipe->up_dev->ud_speed==USB_SPEED_FULL)?ADMHCD_ED_SPEED:0) | \
   1151 		((UGETW(pipe->up_endpoint->ue_edesc->wMaxPacketSize))<<ADMHCD_ED_MAXSHIFT);
   1152 
   1153 	short_ok = xfer->ux_flags & USBD_SHORT_XFER_OK?ADMHCD_TD_R:0;
   1154 /*	printf("level: %d\n",level);
   1155 	printf("short_xfer: %x\n",short_ok);
   1156 	printf("ep_control: %x\n",ep->control);
   1157 	printf("speed: %x\n",pipe->up_dev->ud_speed);
   1158 	printf("dmabuf: %p\n",xfer->ux_dmabuf.block); */
   1159 
   1160 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   1161 	len = xfer->ux_length;
   1162 
   1163 	ep->next = ep;
   1164 
   1165 	i = 0;
   1166 	offset = 0;
   1167 	while ((len>0) || (i==0)) {
   1168 		tlen = min(len,4096);
   1169 		td[i]->buffer = DMAADDR(&xfer->ux_dmabuf,offset) | 0xa0000000;
   1170 		td[i]->buflen=tlen;
   1171 		td[i]->control=(isread?ADMHCD_TD_IN:ADMHCD_TD_OUT) | toggle | ADMHCD_TD_OWN | short_ok;
   1172 		td[i]->len=tlen;
   1173 		toggle = ADMHCD_TD_TOGGLE;
   1174 		len -= tlen;
   1175 		offset += tlen;
   1176 		td[i]->next = td[i+1];
   1177 		i++;
   1178 	};
   1179 
   1180 	td[i]->buffer = 0;
   1181 	td[i]->buflen = 0;
   1182 	td[i]->control = 0;
   1183 	td[i]->next = 0;
   1184 
   1185 	ep->head = td[0];
   1186 	ep->tail = td[i];
   1187 	segs = i;
   1188 	len = 0;
   1189 
   1190 /*	printf("segs: %d\n",segs);
   1191 	printf("ep: %p\n",ep);
   1192 	printf("ep->control: %x\n",ep->control);
   1193 	printf("ep->next: %p\n",ep->next);
   1194 	printf("ep->head: %p\n",ep->head);
   1195 	printf("ep->tail: %p\n",ep->tail);
   1196 	for (i=0; i<segs; i++) {
   1197 		printf("td[%d]: %p\n",i,td[i]);
   1198 		printf("td[%d]->control: %x\n",i,td[i]->control);
   1199 		printf("td[%d]->next: %p\n",i,td[i]->next);
   1200 		printf("td[%d]->buffer: %x\n",i,td[i]->buffer);
   1201 		printf("td[%d]->buflen: %x\n",i,td[i]->buflen);
   1202 	}; */
   1203 
   1204 	REG_WRITE(ADMHCD_REG_HOSTHEAD, (uint32_t)ep);
   1205 	REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP | ADMHCD_DMA_EN);
   1206 	i = 0;
   1207 /*	printf("1: %x %d %x %x\n", ep->control, i, td[i]->control, td[i]->buflen); */
   1208 	s=100;
   1209 	err = 0;
   1210 	while (s--) {
   1211 /*                printf("%x %d %x %x\n", ep->control, i, td[i]->control, td[i]->buflen); */
   1212 		status = USBD_TIMEOUT;
   1213 		if (td[i]->control & ADMHCD_TD_OWN) {
   1214 			delay_ms(3);
   1215 			continue;
   1216 		};
   1217 
   1218 		len += td[i]->len - td[i]->buflen;
   1219 
   1220 		err = (td[i]->control & ADMHCD_TD_ERRMASK)>>ADMHCD_TD_ERRSHIFT;
   1221 		if (err) {
   1222 			status = USBD_IOERROR;
   1223 			break;
   1224 		};
   1225 
   1226 		i++;
   1227 		if (i==segs) {
   1228 			status = USBD_NORMAL_COMPLETION;
   1229 			break;
   1230 		};
   1231 
   1232 	};
   1233 	REG_WRITE(ADMHCD_REG_HOSTCONTROL, ADMHCD_STATE_OP);
   1234 
   1235 	apipe->toggle = ((uint32_t)ep->head & 2)?ADMHCD_TD_DATA1:ADMHCD_TD_DATA0;
   1236 /*	printf("bulk_transfer_done: status: %x, err: %x, len: %x, toggle: %x\n", status,err,len,apipe->toggle); */
   1237 
   1238 	if (short_ok && (err == 0x9 || err == 0xd)) {
   1239 /*		printf("bulk_transfer_done: short_transfer fix\n"); */
   1240 		status = USBD_NORMAL_COMPLETION;
   1241 	};
   1242 	xfer->ux_actlen = len;
   1243 	xfer->ux_status = status;
   1244 
   1245 	level--;
   1246 /*	printf("bulk_start<<<\n"); */
   1247 
   1248 	usb_transfer_complete(xfer);
   1249 	mutex_exit(&sc->sc_lock);
   1250 
   1251 	return USBD_NORMAL_COMPLETION;
   1252 }
   1253 
   1254 static void
   1255 ahci_device_bulk_abort(usbd_xfer_handle xfer)
   1256 {
   1257 	DPRINTF(D_TRACE, ("Bab "));
   1258 	ahci_abort_xfer(xfer, USBD_CANCELLED);
   1259 }
   1260 
   1261 static void
   1262 ahci_device_bulk_close(usbd_pipe_handle pipe)
   1263 {
   1264 	DPRINTF(D_TRACE, ("Bcl "));
   1265 }
   1266 
   1267 static void
   1268 ahci_device_bulk_done(usbd_xfer_handle xfer)
   1269 {
   1270 	DPRINTF(D_TRACE, ("Bdn "));
   1271 }
   1272 
   1273 #define DATA0_RD	(0x03)
   1274 #define DATA0_WR	(0x07)
   1275 #define AHCI_TIMEOUT	(5000)
   1276 
   1277 /*
   1278  * Do a transaction.
   1279  * return 1 if ACK, 0 if NAK, -1 if error.
   1280  */
   1281 static int
   1282 ahci_transaction(struct ahci_softc *sc, usbd_pipe_handle pipe,
   1283 	uint8_t pid, int len, u_char *buf, uint8_t toggle)
   1284 {
   1285 	return -1;
   1286 #if 0
   1287 #ifdef AHCI_DEBUG
   1288 	char str[64];
   1289 	int i;
   1290 #endif
   1291 	int timeout;
   1292 	int ls_via_hub = 0;
   1293 	int pl;
   1294 	uint8_t isr;
   1295 	uint8_t result = 0;
   1296 	uint8_t devaddr = pipe->up_dev->ud_addr;
   1297 	uint8_t endpointaddr = pipe->up_endpoint->ue_edesc->bEndpointAddress;
   1298 	uint8_t endpoint;
   1299 	uint8_t cmd = DATA0_RD;
   1300 
   1301 	endpoint = UE_GET_ADDR(endpointaddr);
   1302 	DPRINTF(D_XFER, ("\n(%x,%d%s%d,%d) ",
   1303 		pid, len, (pid == SL11_PID_IN) ? "<-" : "->", devaddr, endpoint));
   1304 
   1305 	/* Set registers */
   1306 	sl11write(sc, SL11_E0ADDR, 0x40);
   1307 	sl11write(sc, SL11_E0LEN,  len);
   1308 	sl11write(sc, SL11_E0PID,  (pid << 4) + endpoint);
   1309 	sl11write(sc, SL11_E0DEV,  devaddr);
   1310 
   1311 	/* Set buffer unless PID_IN */
   1312 	if (pid != SL11_PID_IN) {
   1313 		if (len > 0)
   1314 			sl11write_region(sc, 0x40, buf, len);
   1315 		cmd = DATA0_WR;
   1316 	}
   1317 
   1318 	/* timing ? */
   1319 	pl = (len >> 3) + 3;
   1320 
   1321 	/* Low speed device via HUB */
   1322 	/* XXX does not work... */
   1323 	if ((sc->sc_fullspeed) && pipe->up_dev->ud_speed == USB_SPEED_LOW) {
   1324 		pl = len + 16;
   1325 		cmd |= SL11_EPCTRL_PREAMBLE;
   1326 
   1327 		/*
   1328 		 * SL811HS/T rev 1.2 has a bug, when it got PID_IN
   1329 		 * from LowSpeed device via HUB.
   1330 		 */
   1331 		if (sc->sc_sltype == SLTYPE_SL811HS_R12 && pid == SL11_PID_IN) {
   1332 			ls_via_hub = 1;
   1333 			DPRINTF(D_MSG, ("LSvH "));
   1334 		}
   1335 	}
   1336 
   1337 	/* timing ? */
   1338 	if (sl11read(sc, SL811_CSOF) <= (uint8_t)pl)
   1339 		cmd |= SL11_EPCTRL_SOF;
   1340 
   1341 	/* Transfer */
   1342 	sl11write(sc, SL11_ISR, 0xff);
   1343 	sl11write(sc, SL11_E0CTRL, cmd | toggle);
   1344 
   1345 	/* Polling */
   1346 	for (timeout = AHCI_TIMEOUT; timeout; timeout--) {
   1347 		isr = sl11read(sc, SL11_ISR);
   1348 		if ((isr & SL11_ISR_USBA))
   1349 			break;
   1350 	}
   1351 
   1352 	/* Check result status */
   1353 	result = sl11read(sc, SL11_E0STAT);
   1354 	if (!(result & SL11_EPSTAT_NAK) && ls_via_hub) {
   1355 		/* Resend PID_IN within 20usec */
   1356 		sl11write(sc, SL11_ISR, 0xff);
   1357 		sl11write(sc, SL11_E0CTRL, SL11_EPCTRL_ARM);
   1358 	}
   1359 
   1360 	sl11write(sc, SL11_ISR, 0xff);
   1361 
   1362 	DPRINTF(D_XFER, ("t=%d i=%x ", AHCI_TIMEOUT - timeout, isr));
   1363 #if AHCI_DEBUG
   1364 	snprintb(str, sizeof(str),
   1365 	    "\20\x8STALL\7NAK\6OV\5SETUP\4DATA1\3TIMEOUT\2ERR\1ACK", result);
   1366 	DPRINTF(D_XFER, ("STAT=%s ", str));
   1367 #endif
   1368 
   1369 	if ((result & SL11_EPSTAT_ERROR))
   1370 		return -1;
   1371 
   1372 	if ((result & SL11_EPSTAT_NAK))
   1373 		return 0;
   1374 
   1375 	/* Read buffer if PID_IN */
   1376 	if (pid == SL11_PID_IN && len > 0) {
   1377 		sl11read_region(sc, buf, 0x40, len);
   1378 #if AHCI_DEBUG
   1379 		for (i = 0; i < len; i++)
   1380 			DPRINTF(D_XFER, ("%02X ", buf[i]));
   1381 #endif
   1382 	}
   1383 
   1384 	return 1;
   1385 #endif
   1386 }
   1387 
   1388 void
   1389 ahci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
   1390 {
   1391 	xfer->ux_status = status;
   1392 	usb_transfer_complete(xfer);
   1393 }
   1394 
   1395 void
   1396 ahci_device_clear_toggle(usbd_pipe_handle pipe)
   1397 {
   1398 	struct ahci_pipe *apipe = (struct ahci_pipe *)pipe;
   1399 	apipe->toggle = 0;
   1400 }
   1401 
   1402 #ifdef AHCI_DEBUG
   1403 void
   1404 print_req(usb_device_request_t *r)
   1405 {
   1406 	const char *xmes[]={
   1407 		"GETSTAT",
   1408 		"CLRFEAT",
   1409 		"res",
   1410 		"SETFEAT",
   1411 		"res",
   1412 		"SETADDR",
   1413 		"GETDESC",
   1414 		"SETDESC",
   1415 		"GETCONF",
   1416 		"SETCONF",
   1417 		"GETIN/F",
   1418 		"SETIN/F",
   1419 		"SYNC_FR"
   1420 	};
   1421 	int req, type, value, index, len;
   1422 
   1423 	req   = r->bRequest;
   1424 	type  = r->bmRequestType;
   1425 	value = UGETW(r->wValue);
   1426 	index = UGETW(r->wIndex);
   1427 	len   = UGETW(r->wLength);
   1428 
   1429 	printf("%x,%s,v=%d,i=%d,l=%d ",
   1430 		type, xmes[req], value, index, len);
   1431 }
   1432 
   1433 void
   1434 print_req_hub(usb_device_request_t *r)
   1435 {
   1436 	struct {
   1437 		int req;
   1438 		int type;
   1439 		const char *str;
   1440 	} conf[] = {
   1441 		{ 1, 0x20, "ClrHubFeat"  },
   1442 		{ 1, 0x23, "ClrPortFeat" },
   1443 		{ 2, 0xa3, "GetBusState" },
   1444 		{ 6, 0xa0, "GetHubDesc"  },
   1445 		{ 0, 0xa0, "GetHubStat"  },
   1446 		{ 0, 0xa3, "GetPortStat" },
   1447 		{ 7, 0x20, "SetHubDesc"  },
   1448 		{ 3, 0x20, "SetHubFeat"  },
   1449 		{ 3, 0x23, "SetPortFeat" },
   1450 		{-1, 0, NULL},
   1451 	};
   1452 	int i;
   1453 	int value, index, len;
   1454 
   1455 	value = UGETW(r->wValue);
   1456 	index = UGETW(r->wIndex);
   1457 	len   = UGETW(r->wLength);
   1458 	for (i = 0; ; i++) {
   1459 		if (conf[i].req == -1 )
   1460 			return print_req(r);
   1461 		if (r->bmRequestType == conf[i].type && r->bRequest == conf[i].req) {
   1462 			printf("%s", conf[i].str);
   1463 			break;
   1464 		}
   1465 	}
   1466 	printf(",v=%d,i=%d,l=%d ", value, index, len);
   1467 }
   1468 
   1469 void
   1470 print_dumpreg(struct ahci_softc *sc)
   1471 {
   1472 #if 0
   1473 	printf("00=%02x,01=%02x,02=%02x,03=%02x,04=%02x,"
   1474 	       "08=%02x,09=%02x,0A=%02x,0B=%02x,0C=%02x,",
   1475 		sl11read(sc, 0),  sl11read(sc, 1),
   1476 		sl11read(sc, 2),  sl11read(sc, 3),
   1477 		sl11read(sc, 4),  sl11read(sc, 8),
   1478 		sl11read(sc, 9),  sl11read(sc, 10),
   1479 		sl11read(sc, 11), sl11read(sc, 12)
   1480 	);
   1481 	printf("CR1=%02x,IER=%02x,0D=%02x,0E=%02x,0F=%02x ",
   1482 		sl11read(sc, 5), sl11read(sc, 6),
   1483 		sl11read(sc, 13), sl11read(sc, 14), sl11read(sc, 15)
   1484 	);
   1485 #endif
   1486 }
   1487 
   1488 void
   1489 print_xfer(usbd_xfer_handle xfer)
   1490 {
   1491 	printf("xfer: length=%d, actlen=%d, flags=%x, timeout=%d,",
   1492 		xfer->ux_length, xfer->ux_actlen, xfer->ux_flags, xfer->ux_timeout);
   1493 	printf("request{ ");
   1494 	print_req_hub(&xfer->ux_request);
   1495 	printf("} ");
   1496 }
   1497 #endif /* AHCI_DEBUG */
   1498