Home | History | Annotate | Line # | Download | only in ic
sl811hs.c revision 1.1
      1 /*	$NetBSD: sl811hs.c,v 1.1 2002/08/11 13:17:53 isaki Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tetsuya Isaki.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * ScanLogic SL811HS/T USB Host Controller
     41  */
     42 /*
     43  * !! HIGHLY EXPERIMENTAL CODE !!
     44  */
     45 
     46 #include <sys/cdefs.h>
     47 __KERNEL_RCSID(0, "$NetBSD: sl811hs.c,v 1.1 2002/08/11 13:17:53 isaki Exp $");
     48 
     49 #include "opt_slhci.h"
     50 
     51 #include <sys/param.h>
     52 #include <sys/systm.h>
     53 #include <sys/kernel.h>
     54 #include <sys/proc.h>
     55 #include <sys/device.h>
     56 #include <sys/malloc.h>
     57 
     58 #include <machine/bus.h>
     59 #include <machine/cpu.h>
     60 
     61 #include <dev/usb/usb.h>
     62 #include <dev/usb/usbdi.h>
     63 #include <dev/usb/usbdivar.h>
     64 #include <dev/usb/usb_mem.h>
     65 #include <dev/usb/usbdevs.h>
     66 
     67 #include <dev/ic/sl811hsreg.h>
     68 #include <dev/ic/sl811hsvar.h>
     69 
     70 static inline u_int8_t sl11read(struct slhci_softc *, int);
     71 static inline void     sl11write(struct slhci_softc *, int, u_int8_t);
     72 static inline void sl11read_region(struct slhci_softc *, u_char *, int, int);
     73 static inline void sl11write_region(struct slhci_softc *, int, u_char *, int);
     74 
     75 static void		sl11_reset(struct slhci_softc *);
     76 static void		sl11_speed(struct slhci_softc *);
     77 
     78 static usbd_status	slhci_open(usbd_pipe_handle);
     79 static void		slhci_softintr(void *);
     80 static void		slhci_poll(struct usbd_bus *);
     81 static void		slhci_poll_hub(void *);
     82 static void		slhci_poll_device(void *arg);
     83 static usbd_status	slhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
     84 static void		slhci_freem(struct usbd_bus *, usb_dma_t *);
     85 static usbd_xfer_handle slhci_allocx(struct usbd_bus *);
     86 static void		slhci_freex(struct usbd_bus *, usbd_xfer_handle);
     87 
     88 static int		slhci_str(usb_string_descriptor_t *, int, char *);
     89 
     90 static usbd_status	slhci_root_ctrl_transfer(usbd_xfer_handle);
     91 static usbd_status	slhci_root_ctrl_start(usbd_xfer_handle);
     92 static void		slhci_root_ctrl_abort(usbd_xfer_handle);
     93 static void		slhci_root_ctrl_close(usbd_pipe_handle);
     94 static void		slhci_root_ctrl_done(usbd_xfer_handle);
     95 
     96 static usbd_status	slhci_root_intr_transfer(usbd_xfer_handle);
     97 static usbd_status	slhci_root_intr_start(usbd_xfer_handle);
     98 static void		slhci_root_intr_abort(usbd_xfer_handle);
     99 static void		slhci_root_intr_close(usbd_pipe_handle);
    100 static void		slhci_root_intr_done(usbd_xfer_handle);
    101 
    102 static usbd_status	slhci_device_ctrl_transfer(usbd_xfer_handle);
    103 static usbd_status	slhci_device_ctrl_start(usbd_xfer_handle);
    104 static void		slhci_device_ctrl_abort(usbd_xfer_handle);
    105 static void		slhci_device_ctrl_close(usbd_pipe_handle);
    106 static void		slhci_device_ctrl_done(usbd_xfer_handle);
    107 
    108 static usbd_status	slhci_device_intr_transfer(usbd_xfer_handle);
    109 static usbd_status	slhci_device_intr_start(usbd_xfer_handle);
    110 static void		slhci_device_intr_abort(usbd_xfer_handle);
    111 static void		slhci_device_intr_close(usbd_pipe_handle);
    112 static void		slhci_device_intr_done(usbd_xfer_handle);
    113 
    114 static usbd_status	slhci_device_isoc_transfer(usbd_xfer_handle);
    115 static usbd_status	slhci_device_isoc_start(usbd_xfer_handle);
    116 static void		slhci_device_isoc_abort(usbd_xfer_handle);
    117 static void		slhci_device_isoc_close(usbd_pipe_handle);
    118 static void		slhci_device_isoc_done(usbd_xfer_handle);
    119 
    120 static usbd_status	slhci_device_bulk_transfer(usbd_xfer_handle);
    121 static usbd_status	slhci_device_bulk_start(usbd_xfer_handle);
    122 static void		slhci_device_bulk_abort(usbd_xfer_handle);
    123 static void		slhci_device_bulk_close(usbd_pipe_handle);
    124 static void		slhci_device_bulk_done(usbd_xfer_handle);
    125 
    126 static int		slhci_transaction(struct slhci_softc *,
    127 	usbd_pipe_handle, u_int8_t, int, u_char *, u_int8_t);
    128 static void		slhci_noop(usbd_pipe_handle);
    129 static void		slhci_abort_xfer(usbd_xfer_handle, usbd_status);
    130 static void		slhci_device_clear_toggle(usbd_pipe_handle);
    131 
    132 extern int usbdebug;
    133 int slhci_dummy;
    134 
    135 /* For root hub */
    136 #define SLHCI_INTR_ENDPT	(1)
    137 
    138 #ifdef SLHCI_DEBUG
    139 #define D_TRACE	(0x0001)	/* function trace */
    140 #define D_MSG	(0x0002)	/* debug messages */
    141 #define D_XFER	(0x0004)	/* transfer messages (noisy!) */
    142 #define D_MEM	(0x0008)	/* memory allocation */
    143 
    144 int slhci_debug = D_MSG | D_XFER;
    145 #define DPRINTF(z,x)	if((slhci_debug&(z))!=0)printf x
    146 void		print_req(usb_device_request_t *);
    147 void		print_req_hub(usb_device_request_t *);
    148 void		print_dumpreg(struct slhci_softc *);
    149 void		print_xfer(usbd_xfer_handle);
    150 #else
    151 #define DPRINTF(z,x)
    152 #endif
    153 
    154 
    155 /* XXX: sync with argument */
    156 static char *sltypestr [] = {
    157 	"SL11H/T",
    158 	"SL811HS/T",
    159 };
    160 
    161 
    162 struct usbd_bus_methods slhci_bus_methods = {
    163 	slhci_open,
    164 	slhci_softintr,
    165 	slhci_poll,
    166 	slhci_allocm,
    167 	slhci_freem,
    168 	slhci_allocx,
    169 	slhci_freex,
    170 };
    171 
    172 struct usbd_pipe_methods slhci_root_ctrl_methods = {
    173 	slhci_root_ctrl_transfer,
    174 	slhci_root_ctrl_start,
    175 	slhci_root_ctrl_abort,
    176 	slhci_root_ctrl_close,
    177 	slhci_noop,
    178 	slhci_root_ctrl_done,
    179 };
    180 
    181 struct usbd_pipe_methods slhci_root_intr_methods = {
    182 	slhci_root_intr_transfer,
    183 	slhci_root_intr_start,
    184 	slhci_root_intr_abort,
    185 	slhci_root_intr_close,
    186 	slhci_noop,
    187 	slhci_root_intr_done,
    188 };
    189 
    190 struct usbd_pipe_methods slhci_device_ctrl_methods = {
    191 	slhci_device_ctrl_transfer,
    192 	slhci_device_ctrl_start,
    193 	slhci_device_ctrl_abort,
    194 	slhci_device_ctrl_close,
    195 	slhci_noop,
    196 	slhci_device_ctrl_done,
    197 };
    198 
    199 struct usbd_pipe_methods slhci_device_intr_methods = {
    200 	slhci_device_intr_transfer,
    201 	slhci_device_intr_start,
    202 	slhci_device_intr_abort,
    203 	slhci_device_intr_close,
    204 	slhci_device_clear_toggle,
    205 	slhci_device_intr_done,
    206 };
    207 
    208 struct usbd_pipe_methods slhci_device_isoc_methods = {
    209 	slhci_device_isoc_transfer,
    210 	slhci_device_isoc_start,
    211 	slhci_device_isoc_abort,
    212 	slhci_device_isoc_close,
    213 	slhci_noop,
    214 	slhci_device_isoc_done,
    215 };
    216 
    217 struct usbd_pipe_methods slhci_device_bulk_methods = {
    218 	slhci_device_bulk_transfer,
    219 	slhci_device_bulk_start,
    220 	slhci_device_bulk_abort,
    221 	slhci_device_bulk_close,
    222 	slhci_noop,
    223 	slhci_device_bulk_done,
    224 };
    225 
    226 struct slhci_pipe {
    227 	struct usbd_pipe pipe;
    228 };
    229 
    230 
    231 /*
    232  * SL811HS Register read/write routine
    233  */
    234 static inline u_int8_t
    235 sl11read(struct slhci_softc *sc, int reg)
    236 {
    237 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_ADDR, reg);
    238 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_DATA);
    239 }
    240 
    241 static inline void
    242 sl11write(struct slhci_softc *sc, int reg, u_int8_t data)
    243 {
    244 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_ADDR, reg);
    245 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_DATA, data);
    246 }
    247 
    248 static inline void
    249 sl11read_region(struct slhci_softc *sc, u_char *buf, int reg, int len)
    250 {
    251 	int i;
    252 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_ADDR, reg);
    253 	for (i = 0; i < len; i++)
    254 		buf[i] = bus_space_read_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_DATA);
    255 }
    256 
    257 static inline void
    258 sl11write_region(struct slhci_softc *sc, int reg, u_char *buf, int len)
    259 {
    260 	int i;
    261 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_ADDR, reg);
    262 	for (i = 0; i < len; i++)
    263 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, SL11_IDX_DATA, buf[i]);
    264 }
    265 
    266 /*
    267  * USB bus reset. From sl811hs_appnote.pdf, p22
    268  */
    269 static void
    270 sl11_reset(struct slhci_softc *sc)
    271 {
    272 	u_int8_t r;
    273 
    274 	DPRINTF(D_TRACE, ("%s() ", __FUNCTION__));
    275 	r = sl11read(sc, SL11_CTRL);
    276 	sl11write(sc, SL11_CTRL, r | SL11_CTRL_RESETENGINE);
    277 	delay_ms(250);
    278 	sl11write(sc, SL11_CTRL, r | SL11_CTRL_RESETENGINE | SL11_CTRL_SUSPEND);
    279 	delay_ms(150);
    280 	sl11write(sc, SL11_CTRL, r | SL11_CTRL_RESETENGINE);
    281 	delay_ms(10);
    282 	sl11write(sc, SL11_CTRL, r);
    283 }
    284 
    285 /*
    286  * Detect the speed of attached device.
    287  */
    288 static void
    289 sl11_speed(struct slhci_softc *sc)
    290 {
    291 	u_int8_t r;
    292 
    293 	sl11write(sc, SL11_ISR, 0xff);
    294 	r = sl11read(sc, SL11_ISR);
    295 	if ((r & SL11_ISR_RESET)) {
    296 		DPRINTF(D_MSG, ("NC "));
    297 		sl11write(sc, SL11_ISR, SL11_ISR_RESET);
    298 		sc->sc_connect = 0;
    299 	}
    300 
    301 	if ((sl11read(sc, SL11_ISR) & SL11_ISR_RESET)) {
    302 		sl11write(sc, SL11_ISR, 0xff);
    303 	} else {
    304 		u_int8_t pol = 0, ctrl = 0;
    305 
    306 		sc->sc_connect = 1;
    307 		if (r & SL11_ISR_DATA) {
    308 			DPRINTF(D_MSG, ("FS "));
    309 			pol  = 0;
    310 			ctrl = SL11_CTRL_EOF2;
    311 			sc->sc_fullspeed = 1;
    312 		} else {
    313 			DPRINTF(D_MSG, ("LS "));
    314 			pol  = SL811_CSOF_POLARITY;
    315 			ctrl = SL11_CTRL_LOWSPEED;
    316 			sc->sc_fullspeed = 0;
    317 		}
    318 		sl11write(sc, SL811_CSOF, pol | SL811_CSOF_MASTER | 0x2e);
    319 		sl11write(sc, SL11_DATA, 0xe0);
    320 		sl11write(sc, SL11_CTRL, ctrl | SL11_CTRL_ENABLESOF);
    321 	}
    322 
    323 	sl11write(sc, SL11_E0PID,  (SL11_PID_SOF << 4) + 0);
    324 	sl11write(sc, SL11_E0DEV,  0);
    325 	sl11write(sc, SL11_E0CTRL, SL11_EPCTRL_ARM);
    326 	delay_ms(30);
    327 }
    328 
    329 /*
    330  * If detect some known controller, return the type.
    331  * If does not, return -1.
    332  */
    333 int
    334 sl811hs_find(struct slhci_softc *sc)
    335 {
    336 	int rev;
    337 
    338 	sc->sc_sltype = -1;
    339 	rev = sl11read(sc, SL11_REV) >> 4;
    340 	if (rev >= SLTYPE_SL11H && rev <= SLTYPE_SL811HS_R14)
    341 		sc->sc_sltype = rev;
    342 	return sc->sc_sltype;
    343 }
    344 
    345 /*
    346  * Attach SL11H/SL811HS. Return 0 if success.
    347  */
    348 int
    349 slhci_attach(struct slhci_softc *sc, struct device *self)
    350 {
    351 	int rev;
    352 
    353 	/* Detect and check the controller type */
    354 	rev = sl811hs_find(sc);
    355 	if (rev == -1)
    356 		return -1;
    357 
    358 	printf("%s: ScanLogic %s USB Host Controller",
    359 		sc->sc_bus.bdev.dv_xname, sltypestr[(rev > 0)]);
    360 	switch (rev) {
    361 	case SLTYPE_SL11H:
    362 		break;
    363 	case SLTYPE_SL811HS_R12:
    364 		printf(" (rev 1.2)");
    365 		break;
    366 	case SLTYPE_SL811HS_R14:
    367 		printf(" (rev 1.4)");
    368 		break;
    369 	default:
    370 		printf(" (unknown revision)");
    371 		break;
    372 	}
    373 	printf("\n");
    374 
    375 	/* Initialize sc */
    376 	sc->sc_bus.usbrev = USBREV_1_1;
    377 	sc->sc_bus.methods = &slhci_bus_methods;
    378 	sc->sc_bus.pipe_size = sizeof(struct slhci_pipe);
    379 	sc->sc_bus.dmatag = sc->sc_dmat;
    380 
    381 	SIMPLEQ_INIT(&sc->sc_free_xfers);
    382 
    383 	usb_callout_init(sc->sc_poll_handle);
    384 
    385 	/* Initialize controller */
    386 	sl11write(sc, SL811_CSOF, SL811_CSOF_MASTER | 0x2e);
    387 	sl11write(sc, SL11_ISR, 0xff);
    388 
    389 	/* Disable interrupt, then wait 40msec */
    390 	sl11write(sc, SL11_IER, 0x00);
    391 	delay_ms(40);
    392 
    393 	/* Reset USB engine */
    394 	sl11write(sc, SL11_CTRL, SL11_CTRL_RESETENGINE | SL11_CTRL_SUSPEND);
    395 	delay_ms(40);
    396 	sl11write(sc, SL11_CTRL, 0x00);
    397 	delay_ms(10);
    398 
    399 	/* USB Bus reset for GET_PORT_STATUS */
    400 	sl11_reset(sc);
    401 
    402 	/* Enable interrupt */
    403 	sl11write(sc, SL11_IER, SL11_IER_INSERT);
    404 	/* x68k Nereid USB controller needs it */
    405 	if (sc->sc_enable_intr)
    406 		sc->sc_enable_intr(sc->sc_arg, INTR_ON);
    407 
    408 #ifdef USB_DEBUG
    409 	usbdebug = 0;
    410 #endif
    411 
    412 	/* Attach USB devices */
    413 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
    414 
    415 	return 0;
    416 }
    417 
    418 int
    419 slhci_intr(void *arg)
    420 {
    421 	struct slhci_softc *sc = arg;
    422 	u_int8_t r;
    423 #ifdef SLHCI_DEBUG
    424 	char bitbuf[256];
    425 #endif
    426 
    427 	r = sl11read(sc, SL11_ISR);
    428 
    429 	sl11write(sc, SL11_ISR, SL11_ISR_DATA | SL11_ISR_SOFTIMER);
    430 
    431 	if ((r & SL11_ISR_RESET)) {
    432 		sc->sc_flags |= SLF_RESET;
    433 		sl11write(sc, SL11_ISR, SL11_ISR_RESET);
    434 	}
    435 	if ((r & SL11_ISR_INSERT)) {
    436 		sc->sc_flags |= SLF_INSERT;
    437 		sl11write(sc, SL11_ISR, SL11_ISR_INSERT);
    438 	}
    439 
    440 #ifdef SLHCI_DEBUG
    441 	bitmask_snprintf(r,
    442 		(sl11read(sc, SL11_CTRL) & SL11_CTRL_SUSPEND)
    443 		? "\20\x8""D+\7RESUME\6INSERT\5SOF\4res\3""BABBLE\2USBB\1USBA"
    444 		: "\20\x8""D+\7RESET\6INSERT\5SOF\4res\3""BABBLE\2USBB\1USBA",
    445 		bitbuf, sizeof(bitbuf));
    446 	DPRINTF(D_XFER, ("I=%s ", bitbuf));
    447 #endif /* SLHCI_DEBUG */
    448 
    449 	return 0;
    450 }
    451 
    452 usbd_status
    453 slhci_open(usbd_pipe_handle pipe)
    454 {
    455 	usbd_device_handle dev = pipe->device;
    456 	struct slhci_softc *sc = (struct slhci_softc *)dev->bus;
    457 	usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
    458 
    459 	DPRINTF(D_TRACE, ("slhci_open(addr=%d,ep=%d,scaddr=%d)",
    460 		dev->address, ed->bEndpointAddress, sc->sc_addr));
    461 
    462 	if (dev->address == sc->sc_addr) {
    463 		switch (ed->bEndpointAddress) {
    464 		case USB_CONTROL_ENDPOINT:
    465 			pipe->methods = &slhci_root_ctrl_methods;
    466 			break;
    467 		case UE_DIR_IN | SLHCI_INTR_ENDPT:
    468 			pipe->methods = &slhci_root_intr_methods;
    469 			break;
    470 		default:
    471 			printf("open:endpointErr!\n");
    472 			return USBD_INVAL;
    473 		}
    474 	} else {
    475 		switch (ed->bmAttributes & UE_XFERTYPE) {
    476 		case UE_CONTROL:
    477 			DPRINTF(D_MSG, ("control "));
    478 			pipe->methods = &slhci_device_ctrl_methods;
    479 			break;
    480 		case UE_INTERRUPT:
    481 			DPRINTF(D_MSG, ("interrupt "));
    482 			pipe->methods = &slhci_device_intr_methods;
    483 			break;
    484 		case UE_ISOCHRONOUS:
    485 			DPRINTF(D_MSG, ("isochronous "));
    486 			pipe->methods = &slhci_device_isoc_methods;
    487 			break;
    488 		case UE_BULK:
    489 			DPRINTF(D_MSG, ("bluk "));
    490 			pipe->methods = &slhci_device_bulk_methods;
    491 			break;
    492 		}
    493 	}
    494 	return USBD_NORMAL_COMPLETION;
    495 }
    496 
    497 void
    498 slhci_softintr(void *arg)
    499 {
    500 	DPRINTF(D_TRACE, ("%s()", __FUNCTION__));
    501 }
    502 
    503 void
    504 slhci_poll(struct usbd_bus *bus)
    505 {
    506 	DPRINTF(D_TRACE, ("%s()", __FUNCTION__));
    507 }
    508 
    509 /*
    510  * Emulation of interrupt transfer for status change endpoint
    511  * of root hub.
    512  */
    513 void
    514 slhci_poll_hub(void *arg)
    515 {
    516 	usbd_xfer_handle xfer = arg;
    517 	usbd_pipe_handle pipe = xfer->pipe;
    518 	struct slhci_softc *sc = (struct slhci_softc *)pipe->device->bus;
    519 	int s;
    520 	u_char *p;
    521 
    522 	usb_callout(sc->sc_poll_handle, sc->sc_interval, slhci_poll_hub, xfer);
    523 
    524 	/* USB spec 11.13.3 (p.260) */
    525 	p = KERNADDR(&xfer->dmabuf, 0);
    526 	p[0] = 0;
    527 	if ((sc->sc_flags & (SLF_INSERT | SLF_RESET))) {
    528 		p[0] = 2;
    529 		DPRINTF(D_TRACE, ("!"));
    530 	}
    531 
    532 	/* no change, return NAK */
    533 	if (p[0] == 0)
    534 		return;
    535 
    536 	xfer->actlen = 1;
    537 	xfer->status = USBD_NORMAL_COMPLETION;
    538 	s = splusb();
    539 	xfer->device->bus->intr_context++;
    540 	usb_transfer_complete(xfer);
    541 	xfer->device->bus->intr_context--;
    542 	splx(s);
    543 }
    544 
    545 usbd_status
    546 slhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
    547 {
    548 	struct slhci_softc *sc = (struct slhci_softc *)bus;
    549 
    550 	DPRINTF(D_MEM, ("SLallocm"));
    551 	return usb_allocmem(&sc->sc_bus, size, 0, dma);
    552 }
    553 
    554 void
    555 slhci_freem(struct usbd_bus *bus, usb_dma_t *dma)
    556 {
    557 	struct slhci_softc *sc = (struct slhci_softc *)bus;
    558 
    559 	DPRINTF(D_MEM, ("SLfreem"));
    560 	usb_freemem(&sc->sc_bus, dma);
    561 }
    562 
    563 usbd_xfer_handle
    564 slhci_allocx(struct usbd_bus *bus)
    565 {
    566 	struct slhci_softc *sc = (struct slhci_softc *)bus;
    567 	usbd_xfer_handle xfer;
    568 
    569 	DPRINTF(D_MEM, ("SLallocx"));
    570 
    571 	xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
    572 	if (xfer)
    573 		SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
    574 	else
    575 		xfer = malloc(sizeof(*xfer), M_USB, M_NOWAIT);
    576 
    577 	if (xfer)
    578 		memset(xfer, 0, sizeof(*xfer));
    579 
    580 	return xfer;
    581 }
    582 
    583 void
    584 slhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
    585 {
    586 	struct slhci_softc *sc = (struct slhci_softc *)bus;
    587 
    588 	DPRINTF(D_MEM, ("SLfreex"));
    589 	SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
    590 }
    591 
    592 void
    593 slhci_noop(usbd_pipe_handle pipe)
    594 {
    595 	DPRINTF(D_TRACE, ("%s()", __FUNCTION__));
    596 }
    597 
    598 /*
    599  * Data structures and routines to emulate the root hub.
    600  */
    601 usb_device_descriptor_t slhci_devd = {
    602 	USB_DEVICE_DESCRIPTOR_SIZE,
    603 	UDESC_DEVICE,		/* type */
    604 	{0x01, 0x01},			/* USB version */
    605 	UDCLASS_HUB,		/* class */
    606 	UDSUBCLASS_HUB,		/* subclass */
    607 	0,			/* protocol */
    608 	64,			/* max packet */
    609 	{USB_VENDOR_SCANLOGIC & 0xff,	/* vendor ID (low)  */
    610 	 USB_VENDOR_SCANLOGIC >> 8  },	/* vendor ID (high) */
    611 	{0} /* ? */,		/* product ID */
    612 	{0},			/* device */
    613 	1,			/* index to manufacturer */
    614 	2,			/* index to product */
    615 	0,			/* index to serial number */
    616 	1			/* number of configurations */
    617 };
    618 
    619 usb_config_descriptor_t slhci_confd = {
    620 	USB_CONFIG_DESCRIPTOR_SIZE,
    621 	UDESC_CONFIG,
    622 	{USB_CONFIG_DESCRIPTOR_SIZE +
    623 	 USB_INTERFACE_DESCRIPTOR_SIZE +
    624 	 USB_ENDPOINT_DESCRIPTOR_SIZE},
    625 	1,			/* number of interfaces */
    626 	1,			/* configuration value */
    627 	0,			/* index to configuration */
    628 	UC_SELF_POWERED,	/* attributes */
    629 	15			/* max current is 30mA... */
    630 };
    631 
    632 usb_interface_descriptor_t slhci_ifcd = {
    633 	USB_INTERFACE_DESCRIPTOR_SIZE,
    634 	UDESC_INTERFACE,
    635 	0,			/* interface number */
    636 	0,			/* alternate setting */
    637 	1,			/* number of endpoint */
    638 	UICLASS_HUB,		/* class */
    639 	UISUBCLASS_HUB,		/* subclass */
    640 	0,			/* protocol */
    641 	0			/* index to interface */
    642 };
    643 
    644 usb_endpoint_descriptor_t slhci_endpd = {
    645 	USB_ENDPOINT_DESCRIPTOR_SIZE,
    646 	UDESC_ENDPOINT,
    647 	UE_DIR_IN | SLHCI_INTR_ENDPT,	/* endpoint address */
    648 	UE_INTERRUPT,		/* attributes */
    649 	{8},			/* max packet size */
    650 	255			/* interval */
    651 };
    652 
    653 usb_hub_descriptor_t slhci_hubd = {
    654 	USB_HUB_DESCRIPTOR_SIZE,
    655 	UDESC_HUB,
    656 	1,			/* number of ports */
    657 	{UHD_PWR_INDIVIDUAL | UHD_OC_NONE, 0},	/* hub characteristics */
    658 	20 /* ? */,		/* 5:power on to power good */
    659 	50,			/* 6:maximum current */
    660 	{ 0x00 },		/* both ports are removable */
    661 	{ 0x00 }		/* port power control mask */
    662 };
    663 
    664 static int
    665 slhci_str(usb_string_descriptor_t *p, int l, char *s)
    666 {
    667 	int i;
    668 
    669 	if (l == 0)
    670 		return 0;
    671 	p->bLength = 2 * strlen(s) + 2;
    672 	if (l == 1)
    673 		return 1;
    674 	p->bDescriptorType = UDESC_STRING;
    675 	l -= 2;
    676 	for (i = 0; s[i] && l > 1; i++, l -= 2)
    677 		USETW2(p->bString[i], 0, s[i]);
    678 	return 2 * i + 2;
    679 }
    680 
    681 usbd_status
    682 slhci_root_ctrl_transfer(usbd_xfer_handle xfer)
    683 {
    684 	usbd_status error;
    685 
    686 	DPRINTF(D_TRACE, ("SLRCtrans "));
    687 
    688 	/* Insert last in queue */
    689 	error = usb_insert_transfer(xfer);
    690 	if (error) {
    691 		DPRINTF(D_MSG, ("usb_insert_transfer returns err! "));
    692 		return error;
    693 	}
    694 
    695 	/*
    696 	 * Pipe isn't running (otherwise error would be USBD_INPROG),
    697 	 * so start it first.
    698 	 */
    699 	return slhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
    700 }
    701 
    702 usbd_status
    703 slhci_root_ctrl_start(usbd_xfer_handle xfer)
    704 {
    705 	struct slhci_softc *sc = (struct slhci_softc *)xfer->pipe->device->bus;
    706 	usb_device_request_t *req;
    707 	int len, value, index, l, s, status;
    708 	int totlen = 0;
    709 	void *buf = NULL;
    710 	usb_port_status_t ps;
    711 	usbd_status error;
    712 	char slbuf[50];
    713 	u_int8_t r;
    714 
    715 	DPRINTF(D_TRACE, ("SLRCstart "));
    716 
    717 	req = &xfer->request;
    718 
    719 	len = UGETW(req->wLength);
    720 	value = UGETW(req->wValue);
    721 	index = UGETW(req->wIndex);
    722 
    723 	if (len)
    724 		buf = KERNADDR(&xfer->dmabuf, 0);
    725 
    726 #ifdef SLHCI_DEBUG
    727 	if ((slhci_debud & D_TRACE))
    728 		print_req_hub(req);
    729 #endif
    730 
    731 #define C(x,y) ((x) | ((y) << 8))
    732 	switch (C(req->bRequest, req->bmRequestType)) {
    733 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
    734 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
    735 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
    736 		DPRINTF(D_MSG, ("UR_CLEAR_FEATURE "));
    737 		break;
    738 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
    739 		DPRINTF(D_MSG, ("UR_GET_CONFIG "));
    740 		if (len > 0) {
    741 			*(u_int8_t *)buf = sc->sc_conf;
    742 			totlen = 1;
    743 		}
    744 		break;
    745 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
    746 		switch (value >> 8) {
    747 		case UDESC_DEVICE:
    748 			DPRINTF(D_MSG, ("UDESC_DEVICE "));
    749 			if ((value & 0xff) != 0) {
    750 				error = USBD_IOERROR;
    751 				goto ret;
    752 			}
    753 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
    754 			memcpy(buf, &slhci_devd, l);
    755 			break;
    756 		case UDESC_CONFIG:
    757 			DPRINTF(D_MSG, ("UDESC_CONFIG "));
    758 			if ((value & 0xff) != 0) {
    759 				error = USBD_IOERROR;
    760 				goto ret;
    761 			}
    762 			totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
    763 			memcpy(buf, &slhci_confd, l);
    764 			buf = (char *)buf + l;
    765 			len -= l;
    766 
    767 			l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
    768 			totlen += l;
    769 			memcpy(buf, &slhci_ifcd, l);
    770 			buf = (char *)buf + l;
    771 			len -= l;
    772 
    773 			l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
    774 			totlen += l;
    775 			memcpy(buf, &slhci_endpd, l);
    776 			break;
    777 		case UDESC_STRING:
    778 			DPRINTF(D_MSG, ("UDESC_STR "));
    779 			if (len == 0)
    780 				break;
    781 			*(u_int8_t *)buf = 0;
    782 			totlen = 1;
    783 			switch (value & 0xff) {
    784 			case 0:
    785 				break;
    786 			case 1:	/* Vendor */
    787 				totlen = slhci_str(buf, len, "ScanLogic");
    788 				break;
    789 			case 2:	/* Product */
    790 				sprintf(slbuf, "%s root hub", sltypestr[sc->sc_sltype]);
    791 				totlen = slhci_str(buf, len, slbuf);
    792 				break;
    793 			default:
    794 				printf("strerr%d ", value & 0xff);
    795 				break;
    796 			}
    797 			break;
    798 		default:
    799 			printf("unknownGetDescriptor=%x", value);
    800 			error = USBD_IOERROR;
    801 			break;
    802 		}
    803 		break;
    804 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
    805 		/* Get Interface, 9.4.4 */
    806 		if (len > 0) {
    807 			*(u_int8_t *)buf = 0;
    808 			totlen = 1;
    809 		}
    810 		break;
    811 	case C(UR_GET_STATUS, UT_READ_DEVICE):
    812 		/* Get Status from device, 9.4.5 */
    813 		if (len > 1) {
    814 			USETW(((usb_status_t *)buf)->wStatus, UDS_SELF_POWERED);
    815 			totlen = 2;
    816 		}
    817 		break;
    818 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
    819 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
    820 		/* Get Status from interface, endpoint, 9.4.5 */
    821 		if (len > 1) {
    822 			USETW(((usb_status_t *)buf)->wStatus, 0);
    823 			totlen = 2;
    824 		}
    825 		break;
    826 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
    827 		/* Set Address, 9.4.6 */
    828 		DPRINTF(D_MSG, ("UR_SET_ADDRESS "));
    829 		if (value >= USB_MAX_DEVICES) {
    830 			error = USBD_IOERROR;
    831 			goto ret;
    832 		}
    833 		sc->sc_addr = value;
    834 		break;
    835 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
    836 		/* Set Configuration, 9.4.7 */
    837 		DPRINTF(D_MSG, ("UR_SET_CONFIG "));
    838 		if (value != 0 && value != 1) {
    839 			error = USBD_IOERROR;
    840 			goto ret;
    841 		}
    842 		sc->sc_conf = value;
    843 		break;
    844 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
    845 		/* Set Descriptor, 9.4.8, not supported */
    846 		DPRINTF(D_MSG, ("UR_SET_DESCRIPTOR,WRITE_DEVICE not supported\n"));
    847 		break;
    848 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
    849 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
    850 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
    851 		/* Set Feature, 9.4.9, not supported */
    852 		DPRINTF(D_MSG, ("UR_SET_FEATURE not supported\n"));
    853 		error = USBD_IOERROR;
    854 		break;
    855 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
    856 		/* Set Interface, 9.4.10, not supported */
    857 		break;
    858 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
    859 		/* Synch Frame, 9.4.11, not supported */
    860 		break;
    861 
    862 	/*
    863 	 * Hub specific requests
    864 	 */
    865 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
    866 		/* Clear Hub Feature, 11.16.2.1, not supported */
    867 		DPRINTF(D_MSG, ("ClearHubFeature not supported\n"));
    868 		break;
    869 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
    870 		/* Clear Port Feature, 11.16.2.2 */
    871 		if (index != 1) {
    872 			error = USBD_IOERROR;
    873 			goto ret;
    874 		}
    875 		switch (value) {
    876 		case UHF_PORT_POWER:
    877 			DPRINTF(D_MSG, ("POWER_OFF "));
    878 			sc->sc_powerstat = POWER_OFF;
    879 			/* x68k Nereid USB controller needs it */
    880 			if (sc->sc_enable_power)
    881 				sc->sc_enable_power(sc, sc->sc_powerstat);
    882 			break;
    883 		case UHF_PORT_SUSPEND:
    884 			DPRINTF(D_MSG, ("SUSPEND "));
    885 			sl11write(sc, SL11_CTRL,
    886 				sl11read(sc, SL11_CTRL) & ~SL11_CTRL_SUSPEND);
    887 			break;
    888 		case UHF_C_PORT_CONNECTION:
    889 			sc->sc_change &= ~UPS_C_CONNECT_STATUS;
    890 			break;
    891 		case UHF_C_PORT_RESET:
    892 			sc->sc_change &= ~UPS_C_PORT_RESET;
    893 			break;
    894 		case UHF_PORT_ENABLE:
    895 			break;
    896 		case UHF_C_PORT_SUSPEND:
    897 		case UHF_C_PORT_ENABLE:
    898 		case UHF_C_PORT_OVER_CURRENT:
    899 		default:
    900 			printf("ClrPortFeatERR:value=0x%x ", value);
    901 			error = USBD_IOERROR;
    902 			break;
    903 		}
    904 		//DPRINTF(D_XFER, ("CH=%04x ", sc->sc_change));
    905 		break;
    906 	case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
    907 		/* Get Bus State, 11.16.2.3, not supported */
    908 		/* shall return a STALL... */
    909 		break;
    910 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
    911 		/* Get Hub Descriptor, 11.16.2.4 */
    912 		if (value != 0) {
    913 			error = USBD_IOERROR;
    914 			goto ret;
    915 		}
    916 		l = min(len, USB_HUB_DESCRIPTOR_SIZE);
    917 		totlen = l;
    918 		memcpy(buf, &slhci_hubd, l);
    919 		break;
    920 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
    921 		/* Get Hub Status, 11.16.2.5 */
    922 		DPRINTF(D_MSG, ("UR_GET_STATUS RCD"));
    923 		if (len != 4) {
    924 			error = USBD_IOERROR;
    925 			goto ret;
    926 		}
    927 		memset(buf, 0, len);
    928 		totlen = len;
    929 		break;
    930 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
    931 		/* Get Port Status, 11.16.2.6 */
    932 		if (index != 1 || len != 4) {
    933 			printf("index=%d,len=%d ", index, len);
    934 			error = USBD_IOERROR;
    935 			goto ret;
    936 		}
    937 		/*
    938 		 * change
    939 		 * o port is always enabled.
    940 		 * o cannot detect over current.
    941 		 */
    942 		s = splusb();
    943 		sc->sc_change &= ~(UPS_C_CONNECT_STATUS | UPS_C_PORT_RESET);
    944 		if ((sc->sc_flags & SLF_INSERT)) {
    945 			sc->sc_flags &= ~SLF_INSERT;
    946 			sc->sc_change |= UPS_C_CONNECT_STATUS;
    947 		}
    948 		if ((sc->sc_flags & SLF_RESET)) {
    949 			sc->sc_flags &= ~SLF_RESET;
    950 			sc->sc_change |= UPS_C_PORT_RESET;
    951 		}
    952 		splx(s);
    953 		/*
    954 		 * XXX It can recognize that device is detached,
    955 		 * while there is sl11_speed() here.
    956 		 */
    957 		if (sc->sc_change)
    958 			sl11_speed(sc);
    959 		/*
    960 		 * status
    961 		 * o port is always enabled.
    962 		 * o cannot detect over current.
    963 		 */
    964 		status = 0;
    965 		if (sc->sc_connect)
    966 			status |= UPS_CURRENT_CONNECT_STATUS | UPS_PORT_ENABLED;
    967 		r = sl11read(sc, SL11_CTRL);
    968 		if (r & SL11_CTRL_SUSPEND)
    969 			status |= UPS_SUSPEND;
    970 		if (sc->sc_powerstat)
    971 			status |= UPS_PORT_POWER;
    972 		if (!sc->sc_fullspeed)
    973 			status |= UPS_LOW_SPEED;
    974 
    975 		//DPRINTF(D_XFER, ("ST=%04x,CH=%04x ", status, sc->sc_change));
    976 		USETW(ps.wPortStatus, status);
    977 		USETW(ps.wPortChange, sc->sc_change);
    978 		l = min(len, sizeof(ps));
    979 		memcpy(buf, &ps, l);
    980 		totlen = l;
    981 		break;
    982 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
    983 		/* Set Hub Descriptor, 11.16.2.7, not supported */
    984 		/* STALL ? */
    985 		error = USBD_IOERROR;
    986 		break;
    987 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
    988 		/* Set Hub Feature, 11.16.2.8, not supported */
    989 		break;
    990 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
    991 		/* Set Port Feature, 11.16.2.9 */
    992 		if (index != 1) {
    993 			printf("index=%d ", index);
    994 			error = USBD_IOERROR;
    995 			goto ret;
    996 		}
    997 		switch (value) {
    998 		case UHF_PORT_RESET:
    999 			DPRINTF(D_MSG, ("PORT_RESET "));
   1000 			sl11_reset(sc);
   1001 			sl11_speed(sc);
   1002 			sc->sc_change = 0;
   1003 			break;
   1004 		case UHF_PORT_POWER:
   1005 			DPRINTF(D_MSG, ("PORT_POWER "));
   1006 			sc->sc_powerstat = POWER_ON;
   1007 			/* x68k Nereid USB controller needs it */
   1008 			if (sc->sc_enable_power)
   1009 				sc->sc_enable_power(sc, sc->sc_powerstat);
   1010 			delay_ms(25);
   1011 			break;
   1012 		default:
   1013 			printf("SetPortFeatERR=0x%x ", value);
   1014 			error = USBD_IOERROR;
   1015 			break;
   1016 		}
   1017 		break;
   1018 	default:
   1019 		DPRINTF(D_MSG, ("ioerr(UR=%02x,UT=%02x) ",
   1020 			req->bRequest, req->bmRequestType));
   1021 		error = USBD_IOERROR;
   1022 		goto ret;
   1023 	}
   1024 	xfer->actlen = totlen;
   1025 	error = USBD_NORMAL_COMPLETION;
   1026  ret:
   1027 	xfer->status = error;
   1028 	s = splusb();
   1029 	usb_transfer_complete(xfer);
   1030 	splx(s);
   1031 	return USBD_IN_PROGRESS;
   1032 }
   1033 
   1034 void
   1035 slhci_root_ctrl_abort(usbd_xfer_handle xfer)
   1036 {
   1037 	DPRINTF(D_TRACE, ("SLRCabort "));
   1038 }
   1039 
   1040 void
   1041 slhci_root_ctrl_close(usbd_pipe_handle pipe)
   1042 {
   1043 	DPRINTF(D_TRACE, ("SLRCclose "));
   1044 }
   1045 
   1046 void
   1047 slhci_root_ctrl_done(usbd_xfer_handle xfer)
   1048 {
   1049 	DPRINTF(D_TRACE, ("SLRCdone\n"));
   1050 }
   1051 
   1052 static usbd_status
   1053 slhci_root_intr_transfer(usbd_xfer_handle xfer)
   1054 {
   1055 	usbd_status error;
   1056 
   1057 	DPRINTF(D_TRACE, ("SLRItransfer "));
   1058 
   1059 	/* Insert last in queue */
   1060 	error = usb_insert_transfer(xfer);
   1061 	if (error)
   1062 		return error;
   1063 
   1064 	/*
   1065 	 * Pipe isn't running (otherwise error would be USBD_INPROG),
   1066 	 * start first.
   1067 	 */
   1068 	return slhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
   1069 }
   1070 
   1071 static usbd_status
   1072 slhci_root_intr_start(usbd_xfer_handle xfer)
   1073 {
   1074 	usbd_pipe_handle pipe = xfer->pipe;
   1075 	struct slhci_softc *sc = (struct slhci_softc *)pipe->device->bus;
   1076 
   1077 	DPRINTF(D_TRACE, ("SLRIstart "));
   1078 
   1079 	sc->sc_interval = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
   1080 	usb_callout(sc->sc_poll_handle, sc->sc_interval, slhci_poll_hub, xfer);
   1081 	sc->sc_intr_xfer = xfer;
   1082 	return USBD_IN_PROGRESS;
   1083 }
   1084 
   1085 static void
   1086 slhci_root_intr_abort(usbd_xfer_handle xfer)
   1087 {
   1088 	DPRINTF(D_TRACE, ("SLRIabort "));
   1089 }
   1090 
   1091 static void
   1092 slhci_root_intr_close(usbd_pipe_handle pipe)
   1093 {
   1094 	struct slhci_softc *sc = (struct slhci_softc *)pipe->device->bus;
   1095 
   1096 	DPRINTF(D_TRACE, ("SLRIclose "));
   1097 
   1098 	usb_uncallout(sc->sc_poll_handle, slhci_poll_hub, sc->sc_intr_xfer);
   1099 	sc->sc_intr_xfer = NULL;
   1100 }
   1101 
   1102 static void
   1103 slhci_root_intr_done(usbd_xfer_handle xfer)
   1104 {
   1105 	//DPRINTF(D_XFER, ("RIdn "));
   1106 }
   1107 
   1108 static usbd_status
   1109 slhci_device_ctrl_transfer(usbd_xfer_handle xfer)
   1110 {
   1111 	usbd_status error;
   1112 
   1113 	DPRINTF(D_TRACE, ("C"));
   1114 
   1115 	error = usb_insert_transfer(xfer);
   1116 	if (error)
   1117 		return error;
   1118 
   1119 	return slhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
   1120 }
   1121 
   1122 static usbd_status
   1123 slhci_device_ctrl_start(usbd_xfer_handle xfer)
   1124 {
   1125 	usb_device_request_t *req = &xfer->request;
   1126 	usbd_pipe_handle pipe = xfer->pipe;
   1127 	struct slhci_softc *sc = (struct slhci_softc *)pipe->device->bus;
   1128 	usbd_status status =  USBD_NORMAL_COMPLETION;
   1129 	void *buf;
   1130 	int pid = SL11_PID_OUT;
   1131 	int len, actlen, size;
   1132 	int s;
   1133 	u_int8_t toggle = 0;
   1134 
   1135 	DPRINTF(D_TRACE, ("st "));
   1136 #ifdef SLHCI_DEBUG
   1137 	if ((slhci_debug & D_TRACE))
   1138 		print_req_hub(req));
   1139 #endif
   1140 
   1141 	/* SETUP transaction */
   1142 	if (slhci_transaction(sc, pipe, SL11_PID_SETUP,
   1143 			sizeof(*req), (u_char*)req, toggle) == -1) {
   1144 		status = USBD_IOERROR;
   1145 		goto ret;
   1146 	}
   1147 	toggle ^= SL11_EPCTRL_DATATOGGLE;
   1148 
   1149 	/* DATA transaction */
   1150 	actlen = 0;
   1151 	len = UGETW(req->wLength);
   1152 	if (len) {
   1153 		buf = KERNADDR(&xfer->dmabuf, 0);
   1154 		if (req->bmRequestType & UT_READ)
   1155 			pid = SL11_PID_IN;
   1156 		for (; actlen < len; ) {
   1157 			size = min(len - actlen, 8/* Minimum size */);
   1158 			if (slhci_transaction(sc, pipe, pid, size, buf, toggle) == -1)
   1159 				break;
   1160 			toggle ^= SL11_EPCTRL_DATATOGGLE;
   1161 			(u_char*)buf += size;
   1162 			actlen += size;
   1163 		}
   1164 	}
   1165 	xfer->actlen = actlen;
   1166 
   1167 	/* ACK (status) */
   1168 	if (pid == SL11_PID_IN)
   1169 		pid = SL11_PID_OUT;
   1170 	else
   1171 		pid = SL11_PID_IN;
   1172 	if (slhci_transaction(sc, pipe, pid, 0, NULL, toggle) == -1)
   1173 		status = USBD_IOERROR;
   1174 
   1175  ret:
   1176 	xfer->status = status;
   1177 
   1178 #ifdef SLHCI_DEBUG
   1179 	if((slhci_debug & D_TRACE) && UGETW(req->wLength) > 0){
   1180 		int i;
   1181 		for(i=0; i < UGETW(req->wLength); i++)
   1182 			printf("%02x", *(unsigned char*)(KERNADDR(&xfer->dmabuf, i)));
   1183 		printf(" ");
   1184 	}
   1185 #endif
   1186 	s = splusb();
   1187 	usb_transfer_complete(xfer);
   1188 	splx(s);
   1189 	return USBD_IN_PROGRESS;
   1190 }
   1191 
   1192 static void
   1193 slhci_device_ctrl_abort(usbd_xfer_handle xfer)
   1194 {
   1195 	DPRINTF(D_TRACE, ("Cab "));
   1196 	slhci_abort_xfer(xfer, USBD_CANCELLED);
   1197 }
   1198 
   1199 static void
   1200 slhci_device_ctrl_close(usbd_pipe_handle pipe)
   1201 {
   1202 	DPRINTF(D_TRACE, ("Ccl "));
   1203 }
   1204 
   1205 static void
   1206 slhci_device_ctrl_done(usbd_xfer_handle xfer)
   1207 {
   1208 	DPRINTF(D_TRACE, ("Cdn "));
   1209 }
   1210 
   1211 static usbd_status
   1212 slhci_device_intr_transfer(usbd_xfer_handle xfer)
   1213 {
   1214 	usbd_status error;
   1215 
   1216 	DPRINTF(D_TRACE, ("INTRtrans "));
   1217 
   1218 	error = usb_insert_transfer(xfer);
   1219 	if (error)
   1220 		return error;
   1221 
   1222 	return slhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
   1223 }
   1224 
   1225 static usbd_status
   1226 slhci_device_intr_start(usbd_xfer_handle xfer)
   1227 {
   1228 	usbd_pipe_handle pipe = xfer->pipe;
   1229 	struct slhci_xfer *sx;
   1230 
   1231 	DPRINTF(D_TRACE, ("INTRstart "));
   1232 
   1233 	sx = malloc(sizeof(*sx), M_USB, M_NOWAIT);
   1234 	if (sx == NULL)
   1235 		goto reterr;
   1236 	memset(sx, 0, sizeof(*sx));
   1237 	sx->sx_xfer  = xfer;
   1238 	xfer->hcpriv = sx;
   1239 
   1240 	/* initialize callout */
   1241 	usb_callout_init(sx->sx_callout_t);
   1242 	usb_callout(sx->sx_callout_t,
   1243 		MS_TO_TICKS(pipe->endpoint->edesc->bInterval),
   1244 		slhci_poll_device, sx);
   1245 
   1246 	/* ACK */
   1247 	return USBD_IN_PROGRESS;
   1248 
   1249  reterr:
   1250 	return USBD_IOERROR;
   1251 }
   1252 
   1253 static void
   1254 slhci_poll_device(void *arg)
   1255 {
   1256 	struct slhci_xfer *sx = (struct slhci_xfer *)arg;
   1257 	usbd_xfer_handle xfer = sx->sx_xfer;
   1258 	usbd_pipe_handle pipe = xfer->pipe;
   1259 	struct slhci_softc *sc = (struct slhci_softc *)pipe->device->bus;
   1260 	void *buf;
   1261 	int pid;
   1262 	int r;
   1263 	int s;
   1264 
   1265 	DPRINTF(D_TRACE, ("pldev"));
   1266 
   1267 	usb_callout(sx->sx_callout_t,
   1268 		MS_TO_TICKS(pipe->endpoint->edesc->bInterval),
   1269 		slhci_poll_device, sx);
   1270 
   1271 	/* interrupt transfer */
   1272 	pid = (UE_GET_DIR(pipe->endpoint->edesc->bEndpointAddress) == UE_DIR_IN)
   1273 	    ? SL11_PID_IN : SL11_PID_OUT;
   1274 	buf = KERNADDR(&xfer->dmabuf, 0);
   1275 
   1276 	r = slhci_transaction(sc, pipe, pid, xfer->length, buf, 0/*toggle*/);
   1277 	if (r < 0) {
   1278 		DPRINTF(D_MSG, ("%s error", __FUNCTION__));
   1279 		return;
   1280 	}
   1281 	/* no change, return NAK */
   1282 	if (r == 0)
   1283 		return;
   1284 
   1285 	xfer->status = USBD_NORMAL_COMPLETION;
   1286 	s = splusb();
   1287 	xfer->device->bus->intr_context++;
   1288 	usb_transfer_complete(xfer);
   1289 	xfer->device->bus->intr_context--;
   1290 	splx(s);
   1291 }
   1292 
   1293 static void
   1294 slhci_device_intr_abort(usbd_xfer_handle xfer)
   1295 {
   1296 	struct slhci_xfer *sx;
   1297 
   1298 	DPRINTF(D_TRACE, ("INTRabort "));
   1299 
   1300 	sx = xfer->hcpriv;
   1301 	if (sx) {
   1302 		usb_uncallout(sx->sx_callout_t, slhci_poll_device, sx);
   1303 		free(sx, M_USB);
   1304 		xfer->hcpriv = NULL;
   1305 	} else {
   1306 		printf("%s: sx == NULL!\n", __FUNCTION__);
   1307 	}
   1308 	slhci_abort_xfer(xfer, USBD_CANCELLED);
   1309 }
   1310 
   1311 static void
   1312 slhci_device_intr_close(usbd_pipe_handle pipe)
   1313 {
   1314 	DPRINTF(D_TRACE, ("INTRclose "));
   1315 }
   1316 
   1317 static void
   1318 slhci_device_intr_done(usbd_xfer_handle xfer)
   1319 {
   1320 	DPRINTF(D_TRACE, ("INTRdone "));
   1321 }
   1322 
   1323 static usbd_status
   1324 slhci_device_isoc_transfer(usbd_xfer_handle xfer)
   1325 {
   1326 	DPRINTF(D_TRACE, ("S"));
   1327 	return USBD_NORMAL_COMPLETION;
   1328 }
   1329 
   1330 static usbd_status
   1331 slhci_device_isoc_start(usbd_xfer_handle xfer)
   1332 {
   1333 	DPRINTF(D_TRACE, ("st "));
   1334 	return USBD_NORMAL_COMPLETION;
   1335 }
   1336 
   1337 static void
   1338 slhci_device_isoc_abort(usbd_xfer_handle xfer)
   1339 {
   1340 	DPRINTF(D_TRACE, ("Sab "));
   1341 }
   1342 
   1343 static void
   1344 slhci_device_isoc_close(usbd_pipe_handle pipe)
   1345 {
   1346 	DPRINTF(D_TRACE, ("Scl "));
   1347 }
   1348 
   1349 static void
   1350 slhci_device_isoc_done(usbd_xfer_handle xfer)
   1351 {
   1352 	DPRINTF(D_TRACE, ("Sdn "));
   1353 }
   1354 
   1355 static usbd_status
   1356 slhci_device_bulk_transfer(usbd_xfer_handle xfer)
   1357 {
   1358 	DPRINTF(D_TRACE, ("B"));
   1359 	return USBD_NORMAL_COMPLETION;
   1360 }
   1361 
   1362 static usbd_status
   1363 slhci_device_bulk_start(usbd_xfer_handle xfer)
   1364 {
   1365 	DPRINTF(D_TRACE, ("st "));
   1366 	return USBD_NORMAL_COMPLETION;
   1367 }
   1368 
   1369 static void
   1370 slhci_device_bulk_abort(usbd_xfer_handle xfer)
   1371 {
   1372 	DPRINTF(D_TRACE, ("Bab "));
   1373 }
   1374 
   1375 static void
   1376 slhci_device_bulk_close(usbd_pipe_handle pipe)
   1377 {
   1378 	DPRINTF(D_TRACE, ("Bcl "));
   1379 }
   1380 
   1381 static void
   1382 slhci_device_bulk_done(usbd_xfer_handle xfer)
   1383 {
   1384 	DPRINTF(D_TRACE, ("Bdn "));
   1385 }
   1386 
   1387 #define DATA0_RD	(0x03)
   1388 #define DATA0_WR	(0x07)
   1389 #define SLHCI_TIMEOUT	(5000)
   1390 
   1391 /*
   1392  * Do a transaction.
   1393  * return 1 if ACK, 0 if NAK, -1 if error.
   1394  */
   1395 static int
   1396 slhci_transaction(struct slhci_softc *sc, usbd_pipe_handle pipe,
   1397 	u_int8_t pid, int len, u_char *buf, u_int8_t toggle)
   1398 {
   1399 #ifdef SLHCI_DEBUG
   1400 	char str[64];
   1401 	int i;
   1402 #endif
   1403 	int timeout;
   1404 	int ls_via_hub = 0;
   1405 	int pl;
   1406 	u_int8_t isr;
   1407 	u_int8_t result = 0;
   1408 	u_int8_t devaddr = pipe->device->address;
   1409 	u_int8_t endpointaddr = pipe->endpoint->edesc->bEndpointAddress;
   1410 	u_int8_t endpoint;
   1411 	u_int8_t cmd = DATA0_RD;
   1412 
   1413 	endpoint = UE_GET_ADDR(endpointaddr);
   1414 	DPRINTF(D_XFER, ("\n(%x,%d%s%d,%d) ",
   1415 		pid, len, (pid == SL11_PID_IN) ? "<-" : "->", devaddr, endpoint));
   1416 
   1417 	/* Set registers */
   1418 	sl11write(sc, SL11_E0ADDR, 0x40);
   1419 	sl11write(sc, SL11_E0LEN,  len);
   1420 	sl11write(sc, SL11_E0PID,  (pid << 4) + endpoint);
   1421 	sl11write(sc, SL11_E0DEV,  devaddr);
   1422 
   1423 	/* Set buffer unless PID_IN */
   1424 	if (pid != SL11_PID_IN) {
   1425 		if (len > 0)
   1426 			sl11write_region(sc, 0x40, buf, len);
   1427 		cmd = DATA0_WR;
   1428 	}
   1429 
   1430 	/* timing ? */
   1431 	pl = (len >> 3) + 3;
   1432 
   1433 	/* Low speed device via HUB */
   1434 	/* XXX does not work... */
   1435 	if ((sc->sc_fullspeed) && pipe->device->speed == USB_SPEED_LOW) {
   1436 		pl = len + 16;
   1437 		cmd |= SL11_EPCTRL_PREAMBLE;
   1438 
   1439 		/*
   1440 		 * SL811HS/T rev 1.2 has a bug, when it got PID_IN
   1441 		 * from LowSpeed device via HUB.
   1442 		 */
   1443 		if (sc->sc_sltype == SLTYPE_SL811HS_R12 && pid == SL11_PID_IN) {
   1444 			ls_via_hub = 1;
   1445 			DPRINTF(D_MSG, ("LSvH "));
   1446 		}
   1447 	}
   1448 
   1449 	/* timing ? */
   1450 	if (sl11read(sc, SL811_CSOF) <= (u_int8_t)pl)
   1451 		cmd |= SL11_EPCTRL_SOF;
   1452 
   1453 	/* Transfer */
   1454 	sl11write(sc, SL11_ISR, 0xff);
   1455 	sl11write(sc, SL11_E0CTRL, cmd | toggle);
   1456 
   1457 	/* Polling */
   1458 	for (timeout = SLHCI_TIMEOUT; timeout; timeout--) {
   1459 		isr = sl11read(sc, SL11_ISR);
   1460 		if ((isr & SL11_ISR_USBA))
   1461 			break;
   1462 	}
   1463 
   1464 	/* Check result status */
   1465 	result = sl11read(sc, SL11_E0STAT);
   1466 	if (!(result & SL11_EPSTAT_NAK) && ls_via_hub) {
   1467 		/* Resend PID_IN within 20usec */
   1468 		sl11write(sc, SL11_ISR, 0xff);
   1469 		sl11write(sc, SL11_E0CTRL, SL11_EPCTRL_ARM);
   1470 	}
   1471 
   1472 	sl11write(sc, SL11_ISR, 0xff);
   1473 
   1474 	DPRINTF(D_XFER, ("t=%d i=%x ", SLHCI_TIMEOUT - timeout, isr));
   1475 #if SLHCI_DEBUG
   1476 	bitmask_snprintf(result,
   1477 		"\20\x8STALL\7NAK\6OV\5SETUP\4DATA1\3TIMEOUT\2ERR\1ACK",
   1478 		str, sizeof(str));
   1479 	DPRINTF(D_XFER, ("STAT=%s ", str));
   1480 #endif
   1481 
   1482 	if ((result & SL11_EPSTAT_ERROR))
   1483 		return -1;
   1484 
   1485 	if ((result & SL11_EPSTAT_NAK))
   1486 		return 0;
   1487 
   1488 	/* Read buffer if PID_IN */
   1489 	if (pid == SL11_PID_IN && len > 0) {
   1490 		sl11read_region(sc, buf, 0x40, len);
   1491 #if SLHCI_DEBUG
   1492 		for (i = 0; i < len; i++)
   1493 			DPRINTF(D_XFER, ("%02X ", buf[i]));
   1494 #endif
   1495 	}
   1496 
   1497 	return 1;
   1498 }
   1499 
   1500 void
   1501 slhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
   1502 {
   1503 	xfer->status = status;
   1504 	usb_transfer_complete(xfer);
   1505 }
   1506 
   1507 void
   1508 slhci_device_clear_toggle(usbd_pipe_handle pipe)
   1509 {
   1510 	DPRINTF(D_TRACE, ("SLdevice_clear_toggle "));
   1511 }
   1512 
   1513 #ifdef SLHCI_DEBUG
   1514 void
   1515 print_req(usb_device_request_t *r)
   1516 {
   1517 	char *xmes[]={
   1518 		"GETSTAT",
   1519 		"CLRFEAT",
   1520 		"res",
   1521 		"SETFEAT",
   1522 		"res",
   1523 		"SETADDR",
   1524 		"GETDESC",
   1525 		"SETDESC",
   1526 		"GETCONF",
   1527 		"SETCONF",
   1528 		"GETIN/F",
   1529 		"SETIN/F",
   1530 		"SYNC_FR"
   1531 	};
   1532 	int req, type, value, index, len;
   1533 
   1534 	req   = r->bRequest;
   1535 	type  = r->bmRequestType;
   1536 	value = UGETW(r->wValue);
   1537 	index = UGETW(r->wIndex);
   1538 	len   = UGETW(r->wLength);
   1539 
   1540 	printf("%x,%s,v=%d,i=%d,l=%d ",
   1541 		type, xmes[req], value, index, len);
   1542 }
   1543 
   1544 void
   1545 print_req_hub(usb_device_request_t *r)
   1546 {
   1547 	struct {
   1548 		int req;
   1549 		int type;
   1550 		char *str;
   1551 	} conf[] = {
   1552 		{ 1, 0x20, "ClrHubFeat"  },
   1553 		{ 1, 0x23, "ClrPortFeat" },
   1554 		{ 2, 0xa3, "GetBusState" },
   1555 		{ 6, 0xa0, "GetHubDesc"  },
   1556 		{ 0, 0xa0, "GetHubStat"  },
   1557 		{ 0, 0xa3, "GetPortStat" },
   1558 		{ 7, 0x20, "SetHubDesc"  },
   1559 		{ 3, 0x20, "SetHubFeat"  },
   1560 		{ 3, 0x23, "SetPortFeat" },
   1561 		{-1, 0, NULL},
   1562 	};
   1563 	int i;
   1564 	int value, index, len;
   1565 
   1566 	value = UGETW(r->wValue);
   1567 	index = UGETW(r->wIndex);
   1568 	len   = UGETW(r->wLength);
   1569 	for (i = 0; ; i++) {
   1570 		if (conf[i].req == -1 )
   1571 			return print_req(r);
   1572 		if (r->bmRequestType == conf[i].type && r->bRequest == conf[i].req) {
   1573 			printf("%s", conf[i].str);
   1574 			break;
   1575 		}
   1576 	}
   1577 	printf(",v=%d,i=%d,l=%d ", value, index, len);
   1578 }
   1579 
   1580 void
   1581 print_dumpreg(struct slhci_softc *sc)
   1582 {
   1583 	printf("00=%02x,01=%02x,02=%02x,03=%02x,04=%02x,"
   1584 	       "08=%02x,09=%02x,0A=%02x,0B=%02x,0C=%02x,",
   1585 		sl11read(sc, 0),  sl11read(sc, 1),
   1586 		sl11read(sc, 2),  sl11read(sc, 3),
   1587 		sl11read(sc, 4),  sl11read(sc, 8),
   1588 		sl11read(sc, 9),  sl11read(sc, 10),
   1589 		sl11read(sc, 11), sl11read(sc, 12)
   1590 	);
   1591 	printf("CR1=%02x,IER=%02x,0D=%02x,0E=%02x,0F=%02x ",
   1592 		sl11read(sc, 5), sl11read(sc, 6),
   1593 		sl11read(sc, 13), sl11read(sc, 14), sl11read(sc, 15)
   1594 	);
   1595 }
   1596 
   1597 void
   1598 print_xfer(usbd_xfer_handle xfer)
   1599 {
   1600 	printf("xfer: length=%d, actlen=%d, flags=%x, timeout=%d,",
   1601 		xfer->length, xfer->actlen, xfer->flags, xfer->timeout);
   1602 	printf("request{ ");
   1603 	print_req_hub(&xfer->request);
   1604 	printf("} ");
   1605 }
   1606 #endif /* SLHCI_DEBUG */
   1607