Home | History | Annotate | Line # | Download | only in ic
sl811hs.c revision 1.102
      1 /*	$NetBSD: sl811hs.c,v 1.102 2019/12/27 09:41:50 msaitoh Exp $	*/
      2 
      3 /*
      4  * Not (c) 2007 Matthew Orgass
      5  * This file is public domain, meaning anyone can make any use of part or all
      6  * of this file including copying into other works without credit.  Any use,
      7  * modified or not, is solely the responsibility of the user.  If this file is
      8  * part of a collection then use in the collection is governed by the terms of
      9  * the collection.
     10  */
     11 
     12 /*
     13  * Cypress/ScanLogic SL811HS/T USB Host Controller
     14  * Datasheet, Errata, and App Note available at www.cypress.com
     15  *
     16  * Uses: Ratoc CFU1U PCMCIA USB Host Controller, Nereid X68k USB HC, ISA
     17  * HCs.  The Ratoc CFU2 uses a different chip.
     18  *
     19  * This chip puts the serial in USB.  It implements USB by means of an eight
     20  * bit I/O interface.  It can be used for ISA, PCMCIA/CF, parallel port,
     21  * serial port, or any eight bit interface.  It has 256 bytes of memory, the
     22  * first 16 of which are used for register access.  There are two sets of
     23  * registers for sending individual bus transactions.  Because USB is polled,
     24  * this organization means that some amount of card access must often be made
     25  * when devices are attached, even if when they are not directly being used.
     26  * A per-ms frame interrupt is necessary and many devices will poll with a
     27  * per-frame bulk transfer.
     28  *
     29  * It is possible to write a little over two bytes to the chip (auto
     30  * incremented) per full speed byte time on the USB.  Unfortunately,
     31  * auto-increment does not work reliably so write and bus speed is
     32  * approximately the same for full speed devices.
     33  *
     34  * In addition to the 240 byte packet size limit for isochronous transfers,
     35  * this chip has no means of determining the current frame number other than
     36  * getting all 1ms SOF interrupts, which is not always possible even on a fast
     37  * system.  Isochronous transfers guarantee that transfers will never be
     38  * retried in a later frame, so this can cause problems with devices beyond
     39  * the difficulty in actually performing the transfer most frames.  I tried
     40  * implementing isoc transfers and was able to play CD-derrived audio via an
     41  * iMic on a 2GHz PC, however it would still be interrupted at times and
     42  * once interrupted, would stay out of sync.  All isoc support has been
     43  * removed.
     44  *
     45  * BUGS: all chip revisions have problems with low speed devices through hubs.
     46  * The chip stops generating SOF with hubs that send SE0 during SOF.  See
     47  * comment in dointr().  All performance enhancing features of this chip seem
     48  * not to work properly, most confirmed buggy in errata doc.
     49  *
     50  */
     51 
     52 /*
     53  * The hard interrupt is the main entry point.  Start, callbacks, and repeat
     54  * are the only others called frequently.
     55  *
     56  * Since this driver attaches to pcmcia, card removal at any point should be
     57  * expected and not cause panics or infinite loops.
     58  */
     59 
     60 /*
     61  * XXX TODO:
     62  *   copy next output packet while transfering
     63  *   usb suspend
     64  *   could keep track of known values of all buffer space?
     65  *   combined print/log function for errors
     66  *
     67  *   ub_usepolling support is untested and may not work
     68  */
     69 
     70 #include <sys/cdefs.h>
     71 __KERNEL_RCSID(0, "$NetBSD: sl811hs.c,v 1.102 2019/12/27 09:41:50 msaitoh Exp $");
     72 
     73 #ifdef _KERNEL_OPT
     74 #include "opt_slhci.h"
     75 #include "opt_usb.h"
     76 #endif
     77 
     78 #include <sys/param.h>
     79 
     80 #include <sys/bus.h>
     81 #include <sys/cpu.h>
     82 #include <sys/device.h>
     83 #include <sys/gcq.h>
     84 #include <sys/intr.h>
     85 #include <sys/kernel.h>
     86 #include <sys/kmem.h>
     87 #include <sys/proc.h>
     88 #include <sys/queue.h>
     89 #include <sys/sysctl.h>
     90 #include <sys/systm.h>
     91 
     92 #include <dev/usb/usb.h>
     93 #include <dev/usb/usbdi.h>
     94 #include <dev/usb/usbdivar.h>
     95 #include <dev/usb/usbhist.h>
     96 #include <dev/usb/usb_mem.h>
     97 #include <dev/usb/usbdevs.h>
     98 #include <dev/usb/usbroothub.h>
     99 
    100 #include <dev/ic/sl811hsreg.h>
    101 #include <dev/ic/sl811hsvar.h>
    102 
    103 #define Q_CB 0				/* Control/Bulk */
    104 #define Q_NEXT_CB 1
    105 #define Q_MAX_XFER Q_CB
    106 #define Q_CALLBACKS 2
    107 #define Q_MAX Q_CALLBACKS
    108 
    109 #define F_AREADY		(0x00000001)
    110 #define F_BREADY		(0x00000002)
    111 #define F_AINPROG		(0x00000004)
    112 #define F_BINPROG		(0x00000008)
    113 #define F_LOWSPEED		(0x00000010)
    114 #define F_UDISABLED		(0x00000020) /* Consider disabled for USB */
    115 #define F_NODEV			(0x00000040)
    116 #define F_ROOTINTR		(0x00000080)
    117 #define F_REALPOWER		(0x00000100) /* Actual power state */
    118 #define F_POWER			(0x00000200) /* USB reported power state */
    119 #define F_ACTIVE		(0x00000400)
    120 #define F_CALLBACK		(0x00000800) /* Callback scheduled */
    121 #define F_SOFCHECK1		(0x00001000)
    122 #define F_SOFCHECK2		(0x00002000)
    123 #define F_CRESET		(0x00004000) /* Reset done not reported */
    124 #define F_CCONNECT		(0x00008000) /* Connect change not reported */
    125 #define F_RESET			(0x00010000)
    126 #define F_ISOC_WARNED		(0x00020000)
    127 #define F_LSVH_WARNED		(0x00040000)
    128 
    129 #define F_DISABLED		(F_NODEV|F_UDISABLED)
    130 #define F_CHANGE		(F_CRESET|F_CCONNECT)
    131 
    132 #ifdef SLHCI_TRY_LSVH
    133 unsigned int slhci_try_lsvh = 1;
    134 #else
    135 unsigned int slhci_try_lsvh = 0;
    136 #endif
    137 
    138 #define ADR 0
    139 #define LEN 1
    140 #define PID 2
    141 #define DEV 3
    142 #define STAT 2
    143 #define CONT 3
    144 
    145 #define A 0
    146 #define B 1
    147 
    148 static const uint8_t slhci_tregs[2][4] =
    149 {{SL11_E0ADDR, SL11_E0LEN, SL11_E0PID, SL11_E0DEV },
    150  {SL11_E1ADDR, SL11_E1LEN, SL11_E1PID, SL11_E1DEV }};
    151 
    152 #define PT_ROOT_CTRL	0
    153 #define PT_ROOT_INTR	1
    154 #define PT_CTRL_SETUP	2
    155 #define PT_CTRL_DATA	3
    156 #define PT_CTRL_STATUS	4
    157 #define PT_INTR		5
    158 #define PT_BULK		6
    159 #define PT_MAX		6
    160 
    161 #ifdef SLHCI_DEBUG
    162 #define SLHCI_MEM_ACCOUNTING
    163 #endif
    164 
    165 /*
    166  * Maximum allowable reserved bus time.  Since intr/isoc transfers have
    167  * unconditional priority, this is all that ensures control and bulk transfers
    168  * get a chance.  It is a single value for all frames since all transfers can
    169  * use multiple consecutive frames if an error is encountered.  Note that it
    170  * is not really possible to fill the bus with transfers, so this value should
    171  * be on the low side.  Defaults to giving a warning unless SLHCI_NO_OVERTIME
    172  * is defined.  Full time is 12000 - END_BUSTIME.
    173  */
    174 #ifndef SLHCI_RESERVED_BUSTIME
    175 #define SLHCI_RESERVED_BUSTIME 5000
    176 #endif
    177 
    178 /*
    179  * Rate for "exceeds reserved bus time" warnings (default) or errors.
    180  * Warnings only happen when an endpoint open causes the time to go above
    181  * SLHCI_RESERVED_BUSTIME, not if it is already above.
    182  */
    183 #ifndef SLHCI_OVERTIME_WARNING_RATE
    184 #define SLHCI_OVERTIME_WARNING_RATE { 60, 0 } /* 60 seconds */
    185 #endif
    186 static const struct timeval reserved_warn_rate = SLHCI_OVERTIME_WARNING_RATE;
    187 
    188 /*
    189  * For EOF, the spec says 42 bit times, plus (I think) a possible hub skew of
    190  * 20 bit times.  By default leave 66 bit times to start the transfer beyond
    191  * the required time.  Units are full-speed bit times (a bit over 5us per 64).
    192  * Only multiples of 64 are significant.
    193  */
    194 #define SLHCI_STANDARD_END_BUSTIME 128
    195 #ifndef SLHCI_EXTRA_END_BUSTIME
    196 #define SLHCI_EXTRA_END_BUSTIME 0
    197 #endif
    198 
    199 #define SLHCI_END_BUSTIME (SLHCI_STANDARD_END_BUSTIME+SLHCI_EXTRA_END_BUSTIME)
    200 
    201 /*
    202  * This is an approximation of the USB worst-case timings presented on p. 54 of
    203  * the USB 1.1 spec translated to full speed bit times.
    204  * FS = full speed with handshake, FSII = isoc in, FSIO = isoc out,
    205  * FSI = isoc (worst case), LS = low speed
    206  */
    207 #define SLHCI_FS_CONST		114
    208 #define SLHCI_FSII_CONST	92
    209 #define SLHCI_FSIO_CONST	80
    210 #define SLHCI_FSI_CONST		92
    211 #define SLHCI_LS_CONST		804
    212 #ifndef SLHCI_PRECICE_BUSTIME
    213 /*
    214  * These values are < 3% too high (compared to the multiply and divide) for
    215  * max sized packets.
    216  */
    217 #define SLHCI_FS_DATA_TIME(len) (((u_int)(len)<<3)+(len)+((len)>>1))
    218 #define SLHCI_LS_DATA_TIME(len) (((u_int)(len)<<6)+((u_int)(len)<<4))
    219 #else
    220 #define SLHCI_FS_DATA_TIME(len) (56*(len)/6)
    221 #define SLHCI_LS_DATA_TIME(len) (449*(len)/6)
    222 #endif
    223 
    224 /*
    225  * Set SLHCI_WAIT_SIZE to the desired maximum size of single FS transfer
    226  * to poll for after starting a transfer.  64 gets all full speed transfers.
    227  * Note that even if 0 polling will occur if data equal or greater than the
    228  * transfer size is copied to the chip while the transfer is in progress.
    229  * Setting SLHCI_WAIT_TIME to -12000 will disable polling.
    230  */
    231 #ifndef SLHCI_WAIT_SIZE
    232 #define SLHCI_WAIT_SIZE 8
    233 #endif
    234 #ifndef SLHCI_WAIT_TIME
    235 #define SLHCI_WAIT_TIME (SLHCI_FS_CONST + \
    236     SLHCI_FS_DATA_TIME(SLHCI_WAIT_SIZE))
    237 #endif
    238 const int slhci_wait_time = SLHCI_WAIT_TIME;
    239 
    240 #ifndef SLHCI_MAX_RETRIES
    241 #define SLHCI_MAX_RETRIES 3
    242 #endif
    243 
    244 /* Check IER values for corruption after this many unrecognized interrupts. */
    245 #ifndef SLHCI_IER_CHECK_FREQUENCY
    246 #ifdef SLHCI_DEBUG
    247 #define SLHCI_IER_CHECK_FREQUENCY 1
    248 #else
    249 #define SLHCI_IER_CHECK_FREQUENCY 100
    250 #endif
    251 #endif
    252 
    253 /* Note that buffer points to the start of the buffer for this transfer.  */
    254 struct slhci_pipe {
    255 	struct usbd_pipe pipe;
    256 	struct usbd_xfer *xfer;		/* xfer in progress */
    257 	uint8_t		*buffer;	/* I/O buffer (if needed) */
    258 	struct gcq 	ap;		/* All pipes */
    259 	struct gcq 	to;		/* Timeout list */
    260 	struct gcq 	xq;		/* Xfer queues */
    261 	unsigned int	pflags;		/* Pipe flags */
    262 #define PF_GONE		(0x01)		/* Pipe is on disabled device */
    263 #define PF_TOGGLE 	(0x02)		/* Data toggle status */
    264 #define PF_LS		(0x04)		/* Pipe is low speed */
    265 #define PF_PREAMBLE	(0x08)		/* Needs preamble */
    266 	Frame		to_frame;	/* Frame number for timeout */
    267 	Frame		frame;		/* Frame number for intr xfer */
    268 	Frame		lastframe;	/* Previous frame number for intr */
    269 	uint16_t	bustime;	/* Worst case bus time usage */
    270 	uint16_t	newbustime[2];	/* new bustimes (see index below) */
    271 	uint8_t		tregs[4];	/* ADR, LEN, PID, DEV */
    272 	uint8_t		newlen[2];	/* 0 = short data, 1 = ctrl data */
    273 	uint8_t		newpid;		/* for ctrl */
    274 	uint8_t		wantshort;	/* last xfer must be short */
    275 	uint8_t		control;	/* Host control register settings */
    276 	uint8_t		nerrs;		/* Current number of errors */
    277 	uint8_t 	ptype;		/* Pipe type */
    278 };
    279 
    280 #define SLHCI_BUS2SC(bus)	((bus)->ub_hcpriv)
    281 #define SLHCI_PIPE2SC(pipe)	SLHCI_BUS2SC((pipe)->up_dev->ud_bus)
    282 #define SLHCI_XFER2SC(xfer)	SLHCI_BUS2SC((xfer)->ux_bus)
    283 
    284 #define SLHCI_PIPE2SPIPE(pipe)	((struct slhci_pipe *)(pipe))
    285 #define SLHCI_XFER2SPIPE(xfer)	SLHCI_PIPE2SPIPE((xfer)->ux_pipe)
    286 
    287 #define SLHCI_XFER_TYPE(x)	(SLHCI_XFER2SPIPE(xfer)->ptype)
    288 
    289 #ifdef SLHCI_PROFILE_TRANSFER
    290 #if defined(__mips__)
    291 /*
    292  * MIPS cycle counter does not directly count cpu cycles but is a different
    293  * fraction of cpu cycles depending on the cpu.
    294  */
    295 typedef uint32_t cc_type;
    296 #define CC_TYPE_FMT "%u"
    297 #define slhci_cc_set(x) __asm volatile ("mfc0 %[cc], $9\n\tnop\n\tnop\n\tnop" \
    298     : [cc] "=r"(x))
    299 #elif defined(__i386__)
    300 typedef uint64_t cc_type;
    301 #define CC_TYPE_FMT "%llu"
    302 #define slhci_cc_set(x) __asm volatile ("rdtsc" : "=A"(x))
    303 #else
    304 #error "SLHCI_PROFILE_TRANSFER not implemented on this MACHINE_ARCH (see sys/dev/ic/sl811hs.c)"
    305 #endif
    306 struct slhci_cc_time {
    307 	cc_type start;
    308 	cc_type stop;
    309 	unsigned int miscdata;
    310 };
    311 #ifndef SLHCI_N_TIMES
    312 #define SLHCI_N_TIMES 200
    313 #endif
    314 struct slhci_cc_times {
    315 	struct slhci_cc_time times[SLHCI_N_TIMES];
    316 	int current;
    317 	int wraparound;
    318 };
    319 
    320 static struct slhci_cc_times t_ab[2];
    321 static struct slhci_cc_times t_abdone;
    322 static struct slhci_cc_times t_copy_to_dev;
    323 static struct slhci_cc_times t_copy_from_dev;
    324 static struct slhci_cc_times t_intr;
    325 static struct slhci_cc_times t_lock;
    326 static struct slhci_cc_times t_delay;
    327 static struct slhci_cc_times t_hard_int;
    328 static struct slhci_cc_times t_callback;
    329 
    330 static inline void
    331 start_cc_time(struct slhci_cc_times *times, unsigned int misc) {
    332 	times->times[times->current].miscdata = misc;
    333 	slhci_cc_set(times->times[times->current].start);
    334 }
    335 static inline void
    336 stop_cc_time(struct slhci_cc_times *times) {
    337 	slhci_cc_set(times->times[times->current].stop);
    338 	if (++times->current >= SLHCI_N_TIMES) {
    339 		times->current = 0;
    340 		times->wraparound = 1;
    341 	}
    342 }
    343 
    344 void slhci_dump_cc_times(int);
    345 
    346 void
    347 slhci_dump_cc_times(int n) {
    348 	struct slhci_cc_times *times;
    349 	int i;
    350 
    351 	switch (n) {
    352 	default:
    353 	case 0:
    354 		printf("USBA start transfer to intr:\n");
    355 		times = &t_ab[A];
    356 		break;
    357 	case 1:
    358 		printf("USBB start transfer to intr:\n");
    359 		times = &t_ab[B];
    360 		break;
    361 	case 2:
    362 		printf("abdone:\n");
    363 		times = &t_abdone;
    364 		break;
    365 	case 3:
    366 		printf("copy to device:\n");
    367 		times = &t_copy_to_dev;
    368 		break;
    369 	case 4:
    370 		printf("copy from device:\n");
    371 		times = &t_copy_from_dev;
    372 		break;
    373 	case 5:
    374 		printf("intr to intr:\n");
    375 		times = &t_intr;
    376 		break;
    377 	case 6:
    378 		printf("lock to release:\n");
    379 		times = &t_lock;
    380 		break;
    381 	case 7:
    382 		printf("delay time:\n");
    383 		times = &t_delay;
    384 		break;
    385 	case 8:
    386 		printf("hard interrupt enter to exit:\n");
    387 		times = &t_hard_int;
    388 		break;
    389 	case 9:
    390 		printf("callback:\n");
    391 		times = &t_callback;
    392 		break;
    393 	}
    394 
    395 	if (times->wraparound)
    396 		for (i = times->current + 1; i < SLHCI_N_TIMES; i++)
    397 			printf("start " CC_TYPE_FMT " stop " CC_TYPE_FMT
    398 			    " difference %8i miscdata %#x\n",
    399 			    times->times[i].start, times->times[i].stop,
    400 			    (int)(times->times[i].stop -
    401 			    times->times[i].start), times->times[i].miscdata);
    402 
    403 	for (i = 0; i < times->current; i++)
    404 		printf("start " CC_TYPE_FMT " stop " CC_TYPE_FMT
    405 		    " difference %8i miscdata %#x\n", times->times[i].start,
    406 		    times->times[i].stop, (int)(times->times[i].stop -
    407 		    times->times[i].start), times->times[i].miscdata);
    408 }
    409 #else
    410 #define start_cc_time(x, y)
    411 #define stop_cc_time(x)
    412 #endif /* SLHCI_PROFILE_TRANSFER */
    413 
    414 typedef usbd_status (*LockCallFunc)(struct slhci_softc *, struct slhci_pipe
    415     *, struct usbd_xfer *);
    416 
    417 struct usbd_xfer * slhci_allocx(struct usbd_bus *, unsigned int);
    418 void slhci_freex(struct usbd_bus *, struct usbd_xfer *);
    419 static void slhci_get_lock(struct usbd_bus *, kmutex_t **);
    420 
    421 usbd_status slhci_transfer(struct usbd_xfer *);
    422 usbd_status slhci_start(struct usbd_xfer *);
    423 usbd_status slhci_root_start(struct usbd_xfer *);
    424 usbd_status slhci_open(struct usbd_pipe *);
    425 
    426 static int slhci_roothub_ctrl(struct usbd_bus *, usb_device_request_t *,
    427     void *, int);
    428 
    429 /*
    430  * slhci_supported_rev, slhci_preinit, slhci_attach, slhci_detach,
    431  * slhci_activate
    432  */
    433 
    434 void slhci_abort(struct usbd_xfer *);
    435 void slhci_close(struct usbd_pipe *);
    436 void slhci_clear_toggle(struct usbd_pipe *);
    437 void slhci_poll(struct usbd_bus *);
    438 void slhci_done(struct usbd_xfer *);
    439 void slhci_void(void *);
    440 
    441 /* lock entry functions */
    442 
    443 #ifdef SLHCI_MEM_ACCOUNTING
    444 void slhci_mem_use(struct usbd_bus *, int);
    445 #endif
    446 
    447 void slhci_reset_entry(void *);
    448 usbd_status slhci_lock_call(struct slhci_softc *, LockCallFunc,
    449     struct slhci_pipe *, struct usbd_xfer *);
    450 void slhci_start_entry(struct slhci_softc *, struct slhci_pipe *);
    451 void slhci_callback_entry(void *arg);
    452 void slhci_do_callback(struct slhci_softc *, struct usbd_xfer *);
    453 
    454 /* slhci_intr */
    455 
    456 void slhci_main(struct slhci_softc *);
    457 
    458 /* in lock functions */
    459 
    460 static void slhci_write(struct slhci_softc *, uint8_t, uint8_t);
    461 static uint8_t slhci_read(struct slhci_softc *, uint8_t);
    462 static void slhci_write_multi(struct slhci_softc *, uint8_t, uint8_t *, int);
    463 static void slhci_read_multi(struct slhci_softc *, uint8_t, uint8_t *, int);
    464 
    465 static void slhci_waitintr(struct slhci_softc *, int);
    466 static int slhci_dointr(struct slhci_softc *);
    467 static void slhci_abdone(struct slhci_softc *, int);
    468 static void slhci_tstart(struct slhci_softc *);
    469 static void slhci_dotransfer(struct slhci_softc *);
    470 
    471 static void slhci_callback(struct slhci_softc *);
    472 static void slhci_enter_xfer(struct slhci_softc *, struct slhci_pipe *);
    473 static void slhci_enter_xfers(struct slhci_softc *);
    474 static void slhci_queue_timed(struct slhci_softc *, struct slhci_pipe *);
    475 static void slhci_xfer_timer(struct slhci_softc *, struct slhci_pipe *);
    476 
    477 static void slhci_callback_schedule(struct slhci_softc *);
    478 static void slhci_do_callback_schedule(struct slhci_softc *);
    479 #if 0
    480 void slhci_pollxfer(struct slhci_softc *, struct usbd_xfer *); /* XXX */
    481 #endif
    482 
    483 static usbd_status slhci_do_poll(struct slhci_softc *, struct slhci_pipe *,
    484     struct usbd_xfer *);
    485 static usbd_status slhci_lsvh_warn(struct slhci_softc *, struct slhci_pipe *,
    486     struct usbd_xfer *);
    487 static usbd_status slhci_isoc_warn(struct slhci_softc *, struct slhci_pipe *,
    488     struct usbd_xfer *);
    489 static usbd_status slhci_open_pipe(struct slhci_softc *, struct slhci_pipe *,
    490     struct usbd_xfer *);
    491 static usbd_status slhci_close_pipe(struct slhci_softc *, struct slhci_pipe *,
    492     struct usbd_xfer *);
    493 static usbd_status slhci_do_abort(struct slhci_softc *, struct slhci_pipe *,
    494     struct usbd_xfer *);
    495 static usbd_status slhci_halt(struct slhci_softc *, struct slhci_pipe *,
    496     struct usbd_xfer *);
    497 
    498 static void slhci_intrchange(struct slhci_softc *, uint8_t);
    499 static void slhci_drain(struct slhci_softc *);
    500 static void slhci_reset(struct slhci_softc *);
    501 static int slhci_reserve_bustime(struct slhci_softc *, struct slhci_pipe *,
    502     int);
    503 static void slhci_insert(struct slhci_softc *);
    504 
    505 static usbd_status slhci_clear_feature(struct slhci_softc *, unsigned int);
    506 static usbd_status slhci_set_feature(struct slhci_softc *, unsigned int);
    507 static void slhci_get_status(struct slhci_softc *, usb_port_status_t *);
    508 
    509 #define	SLHCIHIST_FUNC()	USBHIST_FUNC()
    510 #define	SLHCIHIST_CALLED()	USBHIST_CALLED(slhcidebug)
    511 
    512 #ifdef SLHCI_DEBUG
    513 static int slhci_memtest(struct slhci_softc *);
    514 
    515 void slhci_log_buffer(struct usbd_xfer *);
    516 void slhci_log_req(usb_device_request_t *);
    517 void slhci_log_dumpreg(void);
    518 void slhci_log_xfer(struct usbd_xfer *);
    519 void slhci_log_spipe(struct slhci_pipe *);
    520 void slhci_print_intr(void);
    521 void slhci_log_sc(void);
    522 void slhci_log_slreq(struct slhci_pipe *);
    523 
    524 /* Constified so you can read the values from ddb */
    525 const int SLHCI_D_TRACE =	0x0001;
    526 const int SLHCI_D_MSG = 	0x0002;
    527 const int SLHCI_D_XFER =	0x0004;
    528 const int SLHCI_D_MEM = 	0x0008;
    529 const int SLHCI_D_INTR =	0x0010;
    530 const int SLHCI_D_SXFER =	0x0020;
    531 const int SLHCI_D_ERR = 	0x0080;
    532 const int SLHCI_D_BUF = 	0x0100;
    533 const int SLHCI_D_SOFT =	0x0200;
    534 const int SLHCI_D_WAIT =	0x0400;
    535 const int SLHCI_D_ROOT =	0x0800;
    536 /* SOF/NAK alone normally ignored, SOF also needs D_INTR */
    537 const int SLHCI_D_SOF =		0x1000;
    538 const int SLHCI_D_NAK =		0x2000;
    539 
    540 int slhcidebug = 0x1cbc; /* 0xc8c; */ /* 0xffff; */ /* 0xd8c; */
    541 
    542 SYSCTL_SETUP(sysctl_hw_slhci_setup, "sysctl hw.slhci setup")
    543 {
    544 	int err;
    545 	const struct sysctlnode *rnode;
    546 	const struct sysctlnode *cnode;
    547 
    548 	err = sysctl_createv(clog, 0, NULL, &rnode,
    549 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "slhci",
    550 	    SYSCTL_DESCR("slhci global controls"),
    551 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
    552 
    553 	if (err)
    554 		goto fail;
    555 
    556 	/* control debugging printfs */
    557 	err = sysctl_createv(clog, 0, &rnode, &cnode,
    558 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    559 	    "debug", SYSCTL_DESCR("Enable debugging output"),
    560 	    NULL, 0, &slhcidebug, sizeof(slhcidebug), CTL_CREATE, CTL_EOL);
    561 	if (err)
    562 		goto fail;
    563 
    564 	return;
    565 fail:
    566 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    567 }
    568 
    569 struct slhci_softc *ssc;
    570 
    571 #define SLHCI_DEXEC(x, y) do { if ((slhcidebug & SLHCI_ ## x)) { y; } \
    572 } while (/*CONSTCOND*/ 0)
    573 #define DDOLOG(f, a, b, c, d) do { KERNHIST_LOG(usbhist, f, a, b, c, d); \
    574 } while (/*CONSTCOND*/0)
    575 #define DLOG(x, f, a, b, c, d) SLHCI_DEXEC(x, DDOLOG(f, a, b, c, d))
    576 
    577 /*
    578  * DDOLOGBUF logs a buffer up to 8 bytes at a time. No identifier so that we
    579  * can make it a real function.
    580  */
    581 static void
    582 DDOLOGBUF(uint8_t *buf, unsigned int length)
    583 {
    584 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
    585 	int i;
    586 
    587 	for(i=0; i+8 <= length; i+=8)
    588 		DDOLOG("%.4x %.4x %.4x %.4x", (buf[i] << 8) | buf[i+1],
    589 		    (buf[i+2] << 8) | buf[i+3], (buf[i+4] << 8) | buf[i+5],
    590 		    (buf[i+6] << 8) | buf[i+7]);
    591 	if (length == i+7)
    592 		DDOLOG("%.4x %.4x %.4x %.2x", (buf[i] << 8) | buf[i+1],
    593 		    (buf[i+2] << 8) | buf[i+3], (buf[i+4] << 8) | buf[i+5],
    594 		    buf[i+6]);
    595 	else if (length == i+6)
    596 		DDOLOG("%.4x %.4x %.4x", (buf[i] << 8) | buf[i+1],
    597 		    (buf[i+2] << 8) | buf[i+3], (buf[i+4] << 8) | buf[i+5], 0);
    598 	else if (length == i+5)
    599 		DDOLOG("%.4x %.4x %.2x", (buf[i] << 8) | buf[i+1],
    600 		    (buf[i+2] << 8) | buf[i+3], buf[i+4], 0);
    601 	else if (length == i+4)
    602 		DDOLOG("%.4x %.4x", (buf[i] << 8) | buf[i+1],
    603 		    (buf[i+2] << 8) | buf[i+3], 0,0);
    604 	else if (length == i+3)
    605 		DDOLOG("%.4x %.2x", (buf[i] << 8) | buf[i+1], buf[i+2], 0,0);
    606 	else if (length == i+2)
    607 		DDOLOG("%.4x", (buf[i] << 8) | buf[i+1], 0,0,0);
    608 	else if (length == i+1)
    609 		DDOLOG("%.2x", buf[i], 0,0,0);
    610 }
    611 #define DLOGBUF(x, b, l) SLHCI_DEXEC(x, DDOLOGBUF(b, l))
    612 
    613 #define DDOLOGCTRL(x)	do {						\
    614     DDOLOG("CTRL suspend=%jd", !!((x) & SL11_CTRL_SUSPEND), 0, 0, 0);	\
    615     DDOLOG("CTRL ls     =%jd  jk     =%jd  reset  =%jd  sof    =%jd",	\
    616 	!!((x) & SL11_CTRL_LOWSPEED), !!((x) & SL11_CTRL_JKSTATE),	\
    617 	!!((x) & SL11_CTRL_RESETENGINE), !!((x) & SL11_CTRL_ENABLESOF));\
    618 } while (0)
    619 
    620 #define DDOLOGISR(r)	do {						\
    621     DDOLOG("ISR  data   =%jd  det/res=%jd  insert =%jd  sof    =%jd",	\
    622 	!!((r) & SL11_ISR_DATA), !!((r) & SL11_ISR_RESUME),		\
    623 	!!((r) & SL11_ISR_INSERT), !!!!((r) & SL11_ISR_SOF));		\
    624     DDOLOG("ISR             babble =%jd  usbb   =%jd  usba   =%jd",	\
    625 	!!((r) & SL11_ISR_BABBLE), !!((r) & SL11_ISR_USBB),		\
    626 	!!((r) & SL11_ISR_USBA), 0);					\
    627 } while (0)
    628 
    629 #define DDOLOGIER(r)	do {						\
    630     DDOLOG("IER              det/res=%d  insert =%d  sof    =%d",	\
    631 	!!((r) & SL11_IER_RESUME),					\
    632 	!!((r) & SL11_IER_INSERT), !!!!((r) & SL11_IER_SOF), 0);		\
    633     DDOLOG("IER              babble =%d  usbb   =%d  usba   =%d",	\
    634 	!!((r) & SL11_IER_BABBLE), !!((r) & SL11_IER_USBB),		\
    635 	!!((r) & SL11_IER_USBA), 0);					\
    636 } while (0)
    637 
    638 #define DDOLOGSTATUS(s)	do {						\
    639     DDOLOG("STAT stall   =%d  nak     =%d  overflow =%d  setup   =%d",	\
    640 	!!((s) & SL11_EPSTAT_STALL), !!((s) & SL11_EPSTAT_NAK),		\
    641 	!!((s) & SL11_EPSTAT_OVERFLOW), !!((s) & SL11_EPSTAT_SETUP));	\
    642     DDOLOG("STAT sequence=%d  timeout =%d  error    =%d  ack     =%d",	\
    643 	!!((s) & SL11_EPSTAT_SEQUENCE),	!!((s) & SL11_EPSTAT_TIMEOUT),	\
    644 	!!((s) & SL11_EPSTAT_ERROR), !!((s) & SL11_EPSTAT_ACK));	\
    645 } while (0)
    646 
    647 #define DDOLOGEPCTRL(r)	do {						\
    648     DDOLOG("CTRL preamble=%d  toggle  =%d  sof     =%d  iso     =%d",	\
    649 	!!((r) & SL11_EPCTRL_PREAMBLE), !!((r) & SL11_EPCTRL_DATATOGGLE),\
    650 	!!((r) & SL11_EPCTRL_SOF), !!((r) & SL11_EPCTRL_ISO));		\
    651     DDOLOG("CTRL              out     =%d  enable  =%d  arm     =%d",	\
    652 	!!((r) & SL11_EPCTRL_DIRECTION),				\
    653 	!!((r) & SL11_EPCTRL_ENABLE), !!((r) & SL11_EPCTRL_ARM), 0);	\
    654 } while (0)
    655 
    656 #define DDOLOGEPSTAT(r)	do {						\
    657     DDOLOG("STAT stall   =%d  nak     =%d  overflow =%d  setup   =%d",	\
    658 	!!((r) & SL11_EPSTAT_STALL), !!((r) & SL11_EPSTAT_NAK),		\
    659 	!!((r) & SL11_EPSTAT_OVERFLOW), !!((r) & SL11_EPSTAT_SETUP));	\
    660     DDOLOG("STAT sequence=%d  timeout =%d  error    =%d  ack   =%d",	\
    661 	!!((r) & SL11_EPSTAT_SEQUENCE), !!((r) & SL11_EPSTAT_TIMEOUT),	\
    662 	!!((r) & SL11_EPSTAT_ERROR), !!((r) & SL11_EPSTAT_ACK));	\
    663 } while (0)
    664 #else /* now !SLHCI_DEBUG */
    665 #define slhcidebug 0
    666 #define slhci_log_spipe(spipe) ((void)0)
    667 #define slhci_log_xfer(xfer) ((void)0)
    668 #define SLHCI_DEXEC(x, y) ((void)0)
    669 #define DDOLOG(f, a, b, c, d) ((void)0)
    670 #define DLOG(x, f, a, b, c, d) ((void)0)
    671 #define DDOLOGBUF(b, l) ((void)0)
    672 #define DLOGBUF(x, b, l) ((void)0)
    673 #define DDOLOGCTRL(x) ((void)0)
    674 #define DDOLOGISR(r) ((void)0)
    675 #define DDOLOGIER(r) ((void)0)
    676 #define DDOLOGSTATUS(s) ((void)0)
    677 #define DDOLOGEPCTRL(r) ((void)0)
    678 #define DDOLOGEPSTAT(r) ((void)0)
    679 #endif /* SLHCI_DEBUG */
    680 
    681 #ifdef DIAGNOSTIC
    682 #define LK_SLASSERT(exp, sc, spipe, xfer, ext) do {			\
    683 	if (!(exp)) {							\
    684 		printf("%s: assertion %s failed line %u function %s!"	\
    685 		" halted\n", SC_NAME(sc), #exp, __LINE__, __func__);\
    686 		slhci_halt(sc, spipe, xfer);				\
    687 		ext;							\
    688 	}								\
    689 } while (/*CONSTCOND*/0)
    690 #define UL_SLASSERT(exp, sc, spipe, xfer, ext) do {			\
    691 	if (!(exp)) {							\
    692 		printf("%s: assertion %s failed line %u function %s!"	\
    693 		" halted\n", SC_NAME(sc), #exp, __LINE__, __func__);	\
    694 		slhci_lock_call(sc, &slhci_halt, spipe, xfer);		\
    695 		ext;							\
    696 	}								\
    697 } while (/*CONSTCOND*/0)
    698 #else
    699 #define LK_SLASSERT(exp, sc, spipe, xfer, ext) ((void)0)
    700 #define UL_SLASSERT(exp, sc, spipe, xfer, ext) ((void)0)
    701 #endif
    702 
    703 const struct usbd_bus_methods slhci_bus_methods = {
    704 	.ubm_open = slhci_open,
    705 	.ubm_softint= slhci_void,
    706 	.ubm_dopoll = slhci_poll,
    707 	.ubm_allocx = slhci_allocx,
    708 	.ubm_freex = slhci_freex,
    709 	.ubm_getlock = slhci_get_lock,
    710 	.ubm_rhctrl = slhci_roothub_ctrl,
    711 };
    712 
    713 const struct usbd_pipe_methods slhci_pipe_methods = {
    714 	.upm_transfer = slhci_transfer,
    715 	.upm_start = slhci_start,
    716 	.upm_abort = slhci_abort,
    717 	.upm_close = slhci_close,
    718 	.upm_cleartoggle = slhci_clear_toggle,
    719 	.upm_done = slhci_done,
    720 };
    721 
    722 const struct usbd_pipe_methods slhci_root_methods = {
    723 	.upm_transfer = slhci_transfer,
    724 	.upm_start = slhci_root_start,
    725 	.upm_abort = slhci_abort,
    726 	.upm_close = (void (*)(struct usbd_pipe *))slhci_void, /* XXX safe? */
    727 	.upm_cleartoggle = slhci_clear_toggle,
    728 	.upm_done = slhci_done,
    729 };
    730 
    731 /* Queue inlines */
    732 
    733 #define GOT_FIRST_TO(tvar, t) \
    734     GCQ_GOT_FIRST_TYPED(tvar, &(t)->to, struct slhci_pipe, to)
    735 
    736 #define FIND_TO(var, t, tvar, cond) \
    737     GCQ_FIND_TYPED(var, &(t)->to, tvar, struct slhci_pipe, to, cond)
    738 
    739 #define FOREACH_AP(var, t, tvar) \
    740     GCQ_FOREACH_TYPED(var, &(t)->ap, tvar, struct slhci_pipe, ap)
    741 
    742 #define GOT_FIRST_TIMED_COND(tvar, t, cond) \
    743     GCQ_GOT_FIRST_COND_TYPED(tvar, &(t)->timed, struct slhci_pipe, xq, cond)
    744 
    745 #define GOT_FIRST_CB(tvar, t) \
    746     GCQ_GOT_FIRST_TYPED(tvar, &(t)->q[Q_CB], struct slhci_pipe, xq)
    747 
    748 #define DEQUEUED_CALLBACK(tvar, t) \
    749     GCQ_DEQUEUED_FIRST_TYPED(tvar, &(t)->q[Q_CALLBACKS], struct slhci_pipe, xq)
    750 
    751 #define FIND_TIMED(var, t, tvar, cond) \
    752    GCQ_FIND_TYPED(var, &(t)->timed, tvar, struct slhci_pipe, xq, cond)
    753 
    754 #define DEQUEUED_WAITQ(tvar, sc) \
    755     GCQ_DEQUEUED_FIRST_TYPED(tvar, &(sc)->sc_waitq, struct slhci_pipe, xq)
    756 
    757 static inline void
    758 enter_waitq(struct slhci_softc *sc, struct slhci_pipe *spipe)
    759 {
    760 	gcq_insert_tail(&sc->sc_waitq, &spipe->xq);
    761 }
    762 
    763 static inline void
    764 enter_q(struct slhci_transfers *t, struct slhci_pipe *spipe, int i)
    765 {
    766 	gcq_insert_tail(&t->q[i], &spipe->xq);
    767 }
    768 
    769 static inline void
    770 enter_callback(struct slhci_transfers *t, struct slhci_pipe *spipe)
    771 {
    772 	gcq_insert_tail(&t->q[Q_CALLBACKS], &spipe->xq);
    773 }
    774 
    775 static inline void
    776 enter_all_pipes(struct slhci_transfers *t, struct slhci_pipe *spipe)
    777 {
    778 	gcq_insert_tail(&t->ap, &spipe->ap);
    779 }
    780 
    781 /* Start out of lock functions. */
    782 
    783 struct usbd_xfer *
    784 slhci_allocx(struct usbd_bus *bus, unsigned int nframes)
    785 {
    786 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
    787 	struct usbd_xfer *xfer;
    788 
    789 	xfer = kmem_zalloc(sizeof(*xfer), KM_SLEEP);
    790 
    791 	DLOG(D_MEM, "allocx %#jx", (uintptr_t)xfer, 0,0,0);
    792 
    793 #ifdef SLHCI_MEM_ACCOUNTING
    794 	slhci_mem_use(bus, 1);
    795 #endif
    796 #ifdef DIAGNOSTIC
    797 	if (xfer != NULL)
    798 		xfer->ux_state = XFER_BUSY;
    799 #endif
    800 	return xfer;
    801 }
    802 
    803 void
    804 slhci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
    805 {
    806 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
    807 	DLOG(D_MEM, "freex xfer %#jx spipe %#jx",
    808 	    (uintptr_t)xfer, (uintptr_t)xfer->ux_pipe,0,0);
    809 
    810 #ifdef SLHCI_MEM_ACCOUNTING
    811 	slhci_mem_use(bus, -1);
    812 #endif
    813 #ifdef DIAGNOSTIC
    814 	if (xfer->ux_state != XFER_BUSY &&
    815 	    xfer->ux_status != USBD_NOT_STARTED) {
    816 		struct slhci_softc *sc = SLHCI_BUS2SC(bus);
    817 		printf("%s: slhci_freex: xfer=%p not busy, %#08x halted\n",
    818 		    SC_NAME(sc), xfer, xfer->ux_state);
    819 		DDOLOG("xfer=%p not busy, %#08x halted\n", xfer,
    820 		    xfer->ux_state, 0, 0);
    821 		slhci_lock_call(sc, &slhci_halt, NULL, NULL);
    822 		return;
    823 	}
    824 	xfer->ux_state = XFER_FREE;
    825 #endif
    826 
    827 	kmem_free(xfer, sizeof(*xfer));
    828 }
    829 
    830 static void
    831 slhci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
    832 {
    833 	struct slhci_softc *sc = SLHCI_BUS2SC(bus);
    834 
    835 	*lock = &sc->sc_lock;
    836 }
    837 
    838 usbd_status
    839 slhci_transfer(struct usbd_xfer *xfer)
    840 {
    841 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
    842 	struct slhci_softc *sc = SLHCI_XFER2SC(xfer);
    843 	usbd_status error;
    844 
    845 	DLOG(D_TRACE, "transfer type %jd xfer %#jx spipe %#jx ",
    846 	    SLHCI_XFER_TYPE(xfer), (uintptr_t)xfer, (uintptr_t)xfer->ux_pipe,
    847 	    0);
    848 
    849 	/* Insert last in queue */
    850 	mutex_enter(&sc->sc_lock);
    851 	error = usb_insert_transfer(xfer);
    852 	mutex_exit(&sc->sc_lock);
    853 	if (error) {
    854 		if (error != USBD_IN_PROGRESS)
    855 			DLOG(D_ERR, "usb_insert_transfer returns %jd!", error,
    856 			    0,0,0);
    857 		return error;
    858 	}
    859 
    860 	/*
    861 	 * Pipe isn't running (otherwise error would be USBD_INPROG),
    862 	 * so start it first.
    863 	 */
    864 
    865 	/*
    866 	 * Start will take the lock.
    867 	 */
    868 	error = xfer->ux_pipe->up_methods->upm_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
    869 
    870 	return error;
    871 }
    872 
    873 /* It is not safe for start to return anything other than USBD_INPROG. */
    874 usbd_status
    875 slhci_start(struct usbd_xfer *xfer)
    876 {
    877 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
    878 	struct slhci_softc *sc = SLHCI_XFER2SC(xfer);
    879 	struct usbd_pipe *pipe = xfer->ux_pipe;
    880 	struct slhci_pipe *spipe = SLHCI_PIPE2SPIPE(pipe);
    881 	struct slhci_transfers *t = &sc->sc_transfers;
    882 	usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
    883 	unsigned int max_packet;
    884 
    885 	mutex_enter(&sc->sc_lock);
    886 
    887 	max_packet = UGETW(ed->wMaxPacketSize);
    888 
    889 	DLOG(D_TRACE, "transfer type %jd start xfer %#jx spipe %#jx length %jd",
    890 	    spipe->ptype, (uintptr_t)xfer, (uintptr_t)spipe, xfer->ux_length);
    891 
    892 	/* root transfers use slhci_root_start */
    893 
    894 	KASSERT(spipe->xfer == NULL); /* not SLASSERT */
    895 
    896 	xfer->ux_actlen = 0;
    897 	xfer->ux_status = USBD_IN_PROGRESS;
    898 
    899 	spipe->xfer = xfer;
    900 
    901 	spipe->nerrs = 0;
    902 	spipe->frame = t->frame;
    903 	spipe->control = SL11_EPCTRL_ARM_ENABLE;
    904 	spipe->tregs[DEV] = pipe->up_dev->ud_addr;
    905 	spipe->tregs[PID] = spipe->newpid = UE_GET_ADDR(ed->bEndpointAddress)
    906 	    | (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? SL11_PID_IN :
    907 	    SL11_PID_OUT);
    908 	spipe->newlen[0] = xfer->ux_length % max_packet;
    909 	spipe->newlen[1] = uimin(xfer->ux_length, max_packet);
    910 
    911 	if (spipe->ptype == PT_BULK || spipe->ptype == PT_INTR) {
    912 		if (spipe->pflags & PF_TOGGLE)
    913 			spipe->control |= SL11_EPCTRL_DATATOGGLE;
    914 		spipe->tregs[LEN] = spipe->newlen[1];
    915 		if (spipe->tregs[LEN])
    916 			spipe->buffer = xfer->ux_buf;
    917 		else
    918 			spipe->buffer = NULL;
    919 		spipe->lastframe = t->frame;
    920 		if (spipe->ptype == PT_INTR) {
    921 			spipe->frame = spipe->lastframe +
    922 			    spipe->pipe.up_interval;
    923 		}
    924 
    925 #if defined(DEBUG) || defined(SLHCI_DEBUG)
    926 		if (__predict_false(spipe->ptype == PT_INTR &&
    927 		    xfer->ux_length > spipe->tregs[LEN])) {
    928 			printf("%s: Long INTR transfer not supported!\n",
    929 			    SC_NAME(sc));
    930 			DDOLOG("Long INTR transfer not supported!", 0, 0, 0, 0);
    931 			xfer->ux_status = USBD_INVAL;
    932 		}
    933 #endif
    934 	} else {
    935 		/* ptype may be currently set to any control transfer type. */
    936 		SLHCI_DEXEC(D_TRACE, slhci_log_xfer(xfer));
    937 
    938 		/* SETUP contains IN/OUT bits also */
    939 		spipe->tregs[PID] |= SL11_PID_SETUP;
    940 		spipe->tregs[LEN] = 8;
    941 		spipe->buffer = (uint8_t *)&xfer->ux_request;
    942 		DLOGBUF(D_XFER, spipe->buffer, spipe->tregs[LEN]);
    943 		spipe->ptype = PT_CTRL_SETUP;
    944 		spipe->newpid &= ~SL11_PID_BITS;
    945 		if (xfer->ux_length == 0 ||
    946 		    (xfer->ux_request.bmRequestType & UT_READ))
    947 			spipe->newpid |= SL11_PID_IN;
    948 		else
    949 			spipe->newpid |= SL11_PID_OUT;
    950 	}
    951 
    952 	if (xfer->ux_flags & USBD_FORCE_SHORT_XFER &&
    953 	    spipe->tregs[LEN] == max_packet &&
    954 	    (spipe->newpid & SL11_PID_BITS) == SL11_PID_OUT)
    955 		spipe->wantshort = 1;
    956 	else
    957 		spipe->wantshort = 0;
    958 
    959 	/*
    960 	 * The goal of newbustime and newlen is to avoid bustime calculation
    961 	 * in the interrupt.  The calculations are not too complex, but they
    962 	 * complicate the conditional logic somewhat and doing them all in the
    963 	 * same place shares constants. Index 0 is "short length" for bulk and
    964 	 * ctrl data and 1 is "full length" for ctrl data (bulk/intr are
    965 	 * already set to full length).
    966 	 */
    967 	if (spipe->pflags & PF_LS) {
    968 		/*
    969 		 * Setting PREAMBLE for directly connected LS devices will
    970 		 * lock up the chip.
    971 		 */
    972 		if (spipe->pflags & PF_PREAMBLE)
    973 			spipe->control |= SL11_EPCTRL_PREAMBLE;
    974 		if (max_packet <= 8) {
    975 			spipe->bustime = SLHCI_LS_CONST +
    976 			    SLHCI_LS_DATA_TIME(spipe->tregs[LEN]);
    977 			spipe->newbustime[0] = SLHCI_LS_CONST +
    978 			    SLHCI_LS_DATA_TIME(spipe->newlen[0]);
    979 			spipe->newbustime[1] = SLHCI_LS_CONST +
    980 			    SLHCI_LS_DATA_TIME(spipe->newlen[1]);
    981 		} else
    982 			xfer->ux_status = USBD_INVAL;
    983 	} else {
    984 		UL_SLASSERT(pipe->up_dev->ud_speed == USB_SPEED_FULL, sc,
    985 		    spipe, xfer, return USBD_IN_PROGRESS);
    986 		if (max_packet <= SL11_MAX_PACKET_SIZE) {
    987 			spipe->bustime = SLHCI_FS_CONST +
    988 			    SLHCI_FS_DATA_TIME(spipe->tregs[LEN]);
    989 			spipe->newbustime[0] = SLHCI_FS_CONST +
    990 			    SLHCI_FS_DATA_TIME(spipe->newlen[0]);
    991 			spipe->newbustime[1] = SLHCI_FS_CONST +
    992 			    SLHCI_FS_DATA_TIME(spipe->newlen[1]);
    993 		} else
    994 			xfer->ux_status = USBD_INVAL;
    995 	}
    996 
    997 	/*
    998 	 * The datasheet incorrectly indicates that DIRECTION is for
    999 	 * "transmit to host".  It is for OUT and SETUP.  The app note
   1000 	 * describes its use correctly.
   1001 	 */
   1002 	if ((spipe->tregs[PID] & SL11_PID_BITS) != SL11_PID_IN)
   1003 		spipe->control |= SL11_EPCTRL_DIRECTION;
   1004 
   1005 	slhci_start_entry(sc, spipe);
   1006 
   1007 	mutex_exit(&sc->sc_lock);
   1008 
   1009 	return USBD_IN_PROGRESS;
   1010 }
   1011 
   1012 usbd_status
   1013 slhci_root_start(struct usbd_xfer *xfer)
   1014 {
   1015 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1016 	struct slhci_softc *sc;
   1017 	struct slhci_pipe *spipe __diagused;
   1018 
   1019 	spipe = SLHCI_PIPE2SPIPE(xfer->ux_pipe);
   1020 	sc = SLHCI_XFER2SC(xfer);
   1021 
   1022 	struct slhci_transfers *t = &sc->sc_transfers;
   1023 
   1024 	LK_SLASSERT(spipe != NULL && xfer != NULL, sc, spipe, xfer, return
   1025 	    USBD_CANCELLED);
   1026 
   1027 	DLOG(D_TRACE, "transfer type %jd start",
   1028 	    SLHCI_XFER_TYPE(xfer), 0, 0, 0);
   1029 
   1030 	KASSERT(spipe->ptype == PT_ROOT_INTR);
   1031 
   1032 	mutex_enter(&sc->sc_intr_lock);
   1033 	t->rootintr = xfer;
   1034 	mutex_exit(&sc->sc_intr_lock);
   1035 
   1036 	return USBD_IN_PROGRESS;
   1037 }
   1038 
   1039 usbd_status
   1040 slhci_open(struct usbd_pipe *pipe)
   1041 {
   1042 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1043 	struct usbd_device *dev;
   1044 	struct slhci_softc *sc;
   1045 	struct slhci_pipe *spipe;
   1046 	usb_endpoint_descriptor_t *ed;
   1047 	unsigned int max_packet, pmaxpkt;
   1048 	uint8_t rhaddr;
   1049 
   1050 	dev = pipe->up_dev;
   1051 	sc = SLHCI_PIPE2SC(pipe);
   1052 	spipe = SLHCI_PIPE2SPIPE(pipe);
   1053 	ed = pipe->up_endpoint->ue_edesc;
   1054 	rhaddr = dev->ud_bus->ub_rhaddr;
   1055 
   1056 	DLOG(D_TRACE, "slhci_open(addr=%jd,ep=%jd,rootaddr=%jd)",
   1057 		dev->ud_addr, ed->bEndpointAddress, rhaddr, 0);
   1058 
   1059 	spipe->pflags = 0;
   1060 	spipe->frame = 0;
   1061 	spipe->lastframe = 0;
   1062 	spipe->xfer = NULL;
   1063 	spipe->buffer = NULL;
   1064 
   1065 	gcq_init(&spipe->ap);
   1066 	gcq_init(&spipe->to);
   1067 	gcq_init(&spipe->xq);
   1068 
   1069 	/*
   1070 	 * The endpoint descriptor will not have been set up yet in the case
   1071 	 * of the standard control pipe, so the max packet checks are also
   1072 	 * necessary in start.
   1073 	 */
   1074 
   1075 	max_packet = UGETW(ed->wMaxPacketSize);
   1076 
   1077 	if (dev->ud_speed == USB_SPEED_LOW) {
   1078 		spipe->pflags |= PF_LS;
   1079 		if (dev->ud_myhub->ud_addr != rhaddr) {
   1080 			spipe->pflags |= PF_PREAMBLE;
   1081 			if (!slhci_try_lsvh)
   1082 				return slhci_lock_call(sc, &slhci_lsvh_warn,
   1083 				    spipe, NULL);
   1084 		}
   1085 		pmaxpkt = 8;
   1086 	} else
   1087 		pmaxpkt = SL11_MAX_PACKET_SIZE;
   1088 
   1089 	if (max_packet > pmaxpkt) {
   1090 		DLOG(D_ERR, "packet too large! size %jd spipe %#jx", max_packet,
   1091 		    (uintptr_t)spipe, 0,0);
   1092 		return USBD_INVAL;
   1093 	}
   1094 
   1095 	if (dev->ud_addr == rhaddr) {
   1096 		switch (ed->bEndpointAddress) {
   1097 		case USB_CONTROL_ENDPOINT:
   1098 			spipe->ptype = PT_ROOT_CTRL;
   1099 			pipe->up_interval = 0;
   1100 			pipe->up_methods = &roothub_ctrl_methods;
   1101 			break;
   1102 		case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
   1103 			spipe->ptype = PT_ROOT_INTR;
   1104 			pipe->up_interval = 1;
   1105 			pipe->up_methods = &slhci_root_methods;
   1106 			break;
   1107 		default:
   1108 			printf("%s: Invalid root endpoint!\n", SC_NAME(sc));
   1109 			DDOLOG("Invalid root endpoint", 0, 0, 0, 0);
   1110 			return USBD_INVAL;
   1111 		}
   1112 		return USBD_NORMAL_COMPLETION;
   1113 	} else {
   1114 		switch (ed->bmAttributes & UE_XFERTYPE) {
   1115 		case UE_CONTROL:
   1116 			spipe->ptype = PT_CTRL_SETUP;
   1117 			pipe->up_interval = 0;
   1118 			break;
   1119 		case UE_INTERRUPT:
   1120 			spipe->ptype = PT_INTR;
   1121 			if (pipe->up_interval == USBD_DEFAULT_INTERVAL)
   1122 				pipe->up_interval = ed->bInterval;
   1123 			break;
   1124 		case UE_ISOCHRONOUS:
   1125 			return slhci_lock_call(sc, &slhci_isoc_warn, spipe,
   1126 			    NULL);
   1127 		case UE_BULK:
   1128 			spipe->ptype = PT_BULK;
   1129 			pipe->up_interval = 0;
   1130 			break;
   1131 		}
   1132 
   1133 		DLOG(D_MSG, "open pipe type %jd interval %jd", spipe->ptype,
   1134 		    pipe->up_interval, 0,0);
   1135 
   1136 		pipe->up_methods = __UNCONST(&slhci_pipe_methods);
   1137 
   1138 		return slhci_lock_call(sc, &slhci_open_pipe, spipe, NULL);
   1139 	}
   1140 }
   1141 
   1142 int
   1143 slhci_supported_rev(uint8_t rev)
   1144 {
   1145 	return rev >= SLTYPE_SL811HS_R12 && rev <= SLTYPE_SL811HS_R15;
   1146 }
   1147 
   1148 /*
   1149  * Must be called before the ISR is registered. Interrupts can be shared so
   1150  * slhci_intr could be called as soon as the ISR is registered.
   1151  * Note max_current argument is actual current, but stored as current/2
   1152  */
   1153 void
   1154 slhci_preinit(struct slhci_softc *sc, PowerFunc pow, bus_space_tag_t iot,
   1155     bus_space_handle_t ioh, uint16_t max_current, uint32_t stride)
   1156 {
   1157 	struct slhci_transfers *t;
   1158 	int i;
   1159 
   1160 	t = &sc->sc_transfers;
   1161 
   1162 #ifdef SLHCI_DEBUG
   1163 	ssc = sc;
   1164 #endif
   1165 
   1166 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1167 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
   1168 
   1169 	/* sc->sc_ier = 0;	*/
   1170 	/* t->rootintr = NULL;	*/
   1171 	t->flags = F_NODEV|F_UDISABLED;
   1172 	t->pend = INT_MAX;
   1173 	KASSERT(slhci_wait_time != INT_MAX);
   1174 	t->len[0] = t->len[1] = -1;
   1175 	if (max_current > 500)
   1176 		max_current = 500;
   1177 	t->max_current = (uint8_t)(max_current / 2);
   1178 	sc->sc_enable_power = pow;
   1179 	sc->sc_iot = iot;
   1180 	sc->sc_ioh = ioh;
   1181 	sc->sc_stride = stride;
   1182 
   1183 	KASSERT(Q_MAX+1 == sizeof(t->q) / sizeof(t->q[0]));
   1184 
   1185 	for (i = 0; i <= Q_MAX; i++)
   1186 		gcq_init_head(&t->q[i]);
   1187 	gcq_init_head(&t->timed);
   1188 	gcq_init_head(&t->to);
   1189 	gcq_init_head(&t->ap);
   1190 	gcq_init_head(&sc->sc_waitq);
   1191 }
   1192 
   1193 int
   1194 slhci_attach(struct slhci_softc *sc)
   1195 {
   1196 	struct slhci_transfers *t;
   1197 	const char *rev;
   1198 
   1199 	t = &sc->sc_transfers;
   1200 
   1201 	/* Detect and check the controller type */
   1202 	t->sltype = SL11_GET_REV(slhci_read(sc, SL11_REV));
   1203 
   1204 	/* SL11H not supported */
   1205 	if (!slhci_supported_rev(t->sltype)) {
   1206 		if (t->sltype == SLTYPE_SL11H)
   1207 			printf("%s: SL11H unsupported or bus error!\n",
   1208 			    SC_NAME(sc));
   1209 		else
   1210 			printf("%s: Unknown chip revision!\n", SC_NAME(sc));
   1211 		return -1;
   1212 	}
   1213 
   1214 #ifdef SLHCI_DEBUG
   1215 	if (slhci_memtest(sc)) {
   1216 		printf("%s: memory/bus error!\n", SC_NAME(sc));
   1217 		return -1;
   1218 	}
   1219 #endif
   1220 
   1221 	callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
   1222 	callout_setfunc(&sc->sc_timer, slhci_reset_entry, sc);
   1223 
   1224 	/*
   1225 	 * It is not safe to call the soft interrupt directly as
   1226 	 * usb_schedsoftintr does in the ub_usepolling case (due to locking).
   1227 	 */
   1228 	sc->sc_cb_softintr = softint_establish(SOFTINT_NET,
   1229 	    slhci_callback_entry, sc);
   1230 
   1231 	if (t->sltype == SLTYPE_SL811HS_R12)
   1232 		rev = "(rev 1.2)";
   1233 	else if (t->sltype == SLTYPE_SL811HS_R14)
   1234 		rev = "(rev 1.4 or 1.5)";
   1235 	else
   1236 		rev = "(unknown revision)";
   1237 
   1238 	aprint_normal("%s: ScanLogic SL811HS/T USB Host Controller %s\n",
   1239 	    SC_NAME(sc), rev);
   1240 
   1241 	aprint_normal("%s: Max Current %u mA (value by code, not by probe)\n",
   1242 	    SC_NAME(sc), t->max_current * 2);
   1243 
   1244 #if defined(SLHCI_DEBUG) || defined(SLHCI_NO_OVERTIME) || \
   1245     defined(SLHCI_TRY_LSVH) || defined(SLHCI_PROFILE_TRANSFER)
   1246 	aprint_normal("%s: driver options:"
   1247 #ifdef SLHCI_DEBUG
   1248 	" SLHCI_DEBUG"
   1249 #endif
   1250 #ifdef SLHCI_TRY_LSVH
   1251 	" SLHCI_TRY_LSVH"
   1252 #endif
   1253 #ifdef SLHCI_NO_OVERTIME
   1254 	" SLHCI_NO_OVERTIME"
   1255 #endif
   1256 #ifdef SLHCI_PROFILE_TRANSFER
   1257 	" SLHCI_PROFILE_TRANSFER"
   1258 #endif
   1259 	"\n", SC_NAME(sc));
   1260 #endif
   1261 	sc->sc_bus.ub_revision = USBREV_1_1;
   1262 	sc->sc_bus.ub_methods = __UNCONST(&slhci_bus_methods);
   1263 	sc->sc_bus.ub_pipesize = sizeof(struct slhci_pipe);
   1264 	sc->sc_bus.ub_usedma = false;
   1265 
   1266 	if (!sc->sc_enable_power)
   1267 		t->flags |= F_REALPOWER;
   1268 
   1269 	t->flags |= F_ACTIVE;
   1270 
   1271 	/* Attach usb and uhub. */
   1272 	sc->sc_child = config_found(SC_DEV(sc), &sc->sc_bus, usbctlprint);
   1273 
   1274 	if (!sc->sc_child)
   1275 		return -1;
   1276 	else
   1277 		return 0;
   1278 }
   1279 
   1280 int
   1281 slhci_detach(struct slhci_softc *sc, int flags)
   1282 {
   1283 	struct slhci_transfers *t;
   1284 	int ret;
   1285 
   1286 	t = &sc->sc_transfers;
   1287 
   1288 	/* By this point bus access is no longer allowed. */
   1289 
   1290 	KASSERT(!(t->flags & F_ACTIVE));
   1291 
   1292 	/*
   1293 	 * To be MPSAFE is not sufficient to cancel callouts and soft
   1294 	 * interrupts and assume they are dead since the code could already be
   1295 	 * running or about to run.  Wait until they are known to be done.
   1296 	 */
   1297 	while (t->flags & (F_RESET|F_CALLBACK))
   1298 		tsleep(&sc, PPAUSE, "slhci_detach", hz);
   1299 
   1300 	softint_disestablish(sc->sc_cb_softintr);
   1301 
   1302 	mutex_destroy(&sc->sc_lock);
   1303 	mutex_destroy(&sc->sc_intr_lock);
   1304 
   1305 	ret = 0;
   1306 
   1307 	if (sc->sc_child)
   1308 		ret = config_detach(sc->sc_child, flags);
   1309 
   1310 #ifdef SLHCI_MEM_ACCOUNTING
   1311 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1312 	if (sc->sc_mem_use) {
   1313 		printf("%s: Memory still in use after detach! mem_use (count)"
   1314 		    " = %d\n", SC_NAME(sc), sc->sc_mem_use);
   1315 		DDOLOG("Memory still in use after detach! mem_use (count)"
   1316 		    " = %d", sc->sc_mem_use, 0, 0, 0);
   1317 	}
   1318 #endif
   1319 
   1320 	return ret;
   1321 }
   1322 
   1323 int
   1324 slhci_activate(device_t self, enum devact act)
   1325 {
   1326 	struct slhci_softc *sc = device_private(self);
   1327 
   1328 	switch (act) {
   1329 	case DVACT_DEACTIVATE:
   1330 		slhci_lock_call(sc, &slhci_halt, NULL, NULL);
   1331 		return 0;
   1332 	default:
   1333 		return EOPNOTSUPP;
   1334 	}
   1335 }
   1336 
   1337 void
   1338 slhci_abort(struct usbd_xfer *xfer)
   1339 {
   1340 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1341 	struct slhci_softc *sc;
   1342 	struct slhci_pipe *spipe;
   1343 
   1344 	spipe = SLHCI_PIPE2SPIPE(xfer->ux_pipe);
   1345 
   1346 	if (spipe == NULL)
   1347 		goto callback;
   1348 
   1349 	sc = SLHCI_XFER2SC(xfer);
   1350 	KASSERT(mutex_owned(&sc->sc_lock));
   1351 
   1352 	DLOG(D_TRACE, "transfer type %jd abort xfer %#jx spipe %#jx "
   1353 	    " spipe->xfer %#jx", spipe->ptype, (uintptr_t)xfer,
   1354 	    (uintptr_t)spipe, (uintptr_t)spipe->xfer);
   1355 
   1356 	slhci_lock_call(sc, &slhci_do_abort, spipe, xfer);
   1357 
   1358 callback:
   1359 	xfer->ux_status = USBD_CANCELLED;
   1360 	usb_transfer_complete(xfer);
   1361 }
   1362 
   1363 void
   1364 slhci_close(struct usbd_pipe *pipe)
   1365 {
   1366 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1367 	struct slhci_softc *sc;
   1368 	struct slhci_pipe *spipe;
   1369 
   1370 	sc = SLHCI_PIPE2SC(pipe);
   1371 	spipe = SLHCI_PIPE2SPIPE(pipe);
   1372 
   1373 	DLOG(D_TRACE, "transfer type %jd close spipe %#jx spipe->xfer %#jx",
   1374 	    spipe->ptype, (uintptr_t)spipe, (uintptr_t)spipe->xfer, 0);
   1375 
   1376 	slhci_lock_call(sc, &slhci_close_pipe, spipe, NULL);
   1377 }
   1378 
   1379 void
   1380 slhci_clear_toggle(struct usbd_pipe *pipe)
   1381 {
   1382 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1383 	struct slhci_pipe *spipe;
   1384 
   1385 	spipe = SLHCI_PIPE2SPIPE(pipe);
   1386 
   1387 	DLOG(D_TRACE, "transfer type %jd toggle spipe %#jx", spipe->ptype,
   1388 	    (uintptr_t)spipe, 0, 0);
   1389 
   1390 	spipe->pflags &= ~PF_TOGGLE;
   1391 
   1392 #ifdef DIAGNOSTIC
   1393 	if (spipe->xfer != NULL) {
   1394 		struct slhci_softc *sc = (struct slhci_softc
   1395 		    *)pipe->up_dev->ud_bus;
   1396 
   1397 		printf("%s: Clear toggle on transfer in progress! halted\n",
   1398 		    SC_NAME(sc));
   1399 		DDOLOG("Clear toggle on transfer in progress! halted",
   1400 		    0, 0, 0, 0);
   1401 		slhci_halt(sc, NULL, NULL);
   1402 	}
   1403 #endif
   1404 }
   1405 
   1406 void
   1407 slhci_poll(struct usbd_bus *bus) /* XXX necessary? */
   1408 {
   1409 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1410 	struct slhci_softc *sc;
   1411 
   1412 	sc = SLHCI_BUS2SC(bus);
   1413 
   1414 	DLOG(D_TRACE, "slhci_poll", 0,0,0,0);
   1415 
   1416 	slhci_lock_call(sc, &slhci_do_poll, NULL, NULL);
   1417 }
   1418 
   1419 void
   1420 slhci_done(struct usbd_xfer *xfer)
   1421 {
   1422 }
   1423 
   1424 void
   1425 slhci_void(void *v) {}
   1426 
   1427 /* End out of lock functions. Start lock entry functions. */
   1428 
   1429 #ifdef SLHCI_MEM_ACCOUNTING
   1430 void
   1431 slhci_mem_use(struct usbd_bus *bus, int val)
   1432 {
   1433 	struct slhci_softc *sc = SLHCI_BUS2SC(bus);
   1434 
   1435 	mutex_enter(&sc->sc_intr_lock);
   1436 	sc->sc_mem_use += val;
   1437 	mutex_exit(&sc->sc_intr_lock);
   1438 }
   1439 #endif
   1440 
   1441 void
   1442 slhci_reset_entry(void *arg)
   1443 {
   1444 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1445 	struct slhci_softc *sc = arg;
   1446 
   1447 	mutex_enter(&sc->sc_intr_lock);
   1448 	slhci_reset(sc);
   1449 	/*
   1450 	 * We cannot call the callback directly since we could then be reset
   1451 	 * again before finishing and need the callout delay for timing.
   1452 	 * Scheduling the callout again before we exit would defeat the reap
   1453 	 * mechanism since we could be unlocked while the reset flag is not
   1454 	 * set. The callback code will check the wait queue.
   1455 	 */
   1456 	slhci_callback_schedule(sc);
   1457 	mutex_exit(&sc->sc_intr_lock);
   1458 }
   1459 
   1460 usbd_status
   1461 slhci_lock_call(struct slhci_softc *sc, LockCallFunc lcf, struct slhci_pipe
   1462     *spipe, struct usbd_xfer *xfer)
   1463 {
   1464 	usbd_status ret;
   1465 
   1466 	mutex_enter(&sc->sc_intr_lock);
   1467 	ret = (*lcf)(sc, spipe, xfer);
   1468 	slhci_main(sc);
   1469 	mutex_exit(&sc->sc_intr_lock);
   1470 
   1471 	return ret;
   1472 }
   1473 
   1474 void
   1475 slhci_start_entry(struct slhci_softc *sc, struct slhci_pipe *spipe)
   1476 {
   1477 	struct slhci_transfers *t;
   1478 
   1479 	mutex_enter(&sc->sc_intr_lock);
   1480 	t = &sc->sc_transfers;
   1481 
   1482 	if (!(t->flags & (F_AINPROG|F_BINPROG))) {
   1483 		slhci_enter_xfer(sc, spipe);
   1484 		slhci_dotransfer(sc);
   1485 		slhci_main(sc);
   1486 	} else {
   1487 		enter_waitq(sc, spipe);
   1488 	}
   1489 	mutex_exit(&sc->sc_intr_lock);
   1490 }
   1491 
   1492 void
   1493 slhci_callback_entry(void *arg)
   1494 {
   1495 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1496 	struct slhci_softc *sc;
   1497 	struct slhci_transfers *t;
   1498 
   1499 	sc = (struct slhci_softc *)arg;
   1500 
   1501 	mutex_enter(&sc->sc_intr_lock);
   1502 	t = &sc->sc_transfers;
   1503 	DLOG(D_SOFT, "callback_entry flags %#jx", t->flags, 0,0,0);
   1504 
   1505 repeat:
   1506 	slhci_callback(sc);
   1507 
   1508 	if (!gcq_empty(&sc->sc_waitq)) {
   1509 		slhci_enter_xfers(sc);
   1510 		slhci_dotransfer(sc);
   1511 		slhci_waitintr(sc, 0);
   1512 		goto repeat;
   1513 	}
   1514 
   1515 	t->flags &= ~F_CALLBACK;
   1516 	mutex_exit(&sc->sc_intr_lock);
   1517 }
   1518 
   1519 void
   1520 slhci_do_callback(struct slhci_softc *sc, struct usbd_xfer *xfer)
   1521 {
   1522 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1523 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   1524 
   1525 	start_cc_time(&t_callback, (u_int)xfer);
   1526 	mutex_exit(&sc->sc_intr_lock);
   1527 
   1528 	mutex_enter(&sc->sc_lock);
   1529 	usb_transfer_complete(xfer);
   1530 	mutex_exit(&sc->sc_lock);
   1531 
   1532 	mutex_enter(&sc->sc_intr_lock);
   1533 	stop_cc_time(&t_callback);
   1534 }
   1535 
   1536 int
   1537 slhci_intr(void *arg)
   1538 {
   1539 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1540 	struct slhci_softc *sc = arg;
   1541 	int ret = 0;
   1542 	int irq;
   1543 
   1544 	start_cc_time(&t_hard_int, (unsigned int)arg);
   1545 	mutex_enter(&sc->sc_intr_lock);
   1546 
   1547 	do {
   1548 		irq = slhci_dointr(sc);
   1549 		ret |= irq;
   1550 		slhci_main(sc);
   1551 	} while (irq);
   1552 	mutex_exit(&sc->sc_intr_lock);
   1553 
   1554 	stop_cc_time(&t_hard_int);
   1555 	return ret;
   1556 }
   1557 
   1558 /* called with interrupt lock only held. */
   1559 void
   1560 slhci_main(struct slhci_softc *sc)
   1561 {
   1562 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1563 	struct slhci_transfers *t;
   1564 
   1565 	t = &sc->sc_transfers;
   1566 
   1567 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   1568 
   1569 waitcheck:
   1570 	slhci_waitintr(sc, slhci_wait_time);
   1571 
   1572 	/*
   1573 	 * The direct call is needed in the ub_usepolling and disabled cases
   1574 	 * since the soft interrupt is not available.  In the disabled case,
   1575 	 * this code can be reached from the usb detach, after the reaping of
   1576 	 * the soft interrupt.  That test could be !F_ACTIVE, but there is no
   1577 	 * reason not to make the callbacks directly in the other DISABLED
   1578 	 * cases.
   1579 	 */
   1580 	if ((t->flags & F_ROOTINTR) || !gcq_empty(&t->q[Q_CALLBACKS])) {
   1581 		if (__predict_false(sc->sc_bus.ub_usepolling ||
   1582 		    t->flags & F_DISABLED))
   1583 			slhci_callback(sc);
   1584 		else
   1585 			slhci_callback_schedule(sc);
   1586 	}
   1587 
   1588 	if (!gcq_empty(&sc->sc_waitq)) {
   1589 		slhci_enter_xfers(sc);
   1590 		slhci_dotransfer(sc);
   1591 		goto waitcheck;
   1592 	}
   1593 	DLOG(D_INTR, "... done", 0, 0, 0, 0);
   1594 }
   1595 
   1596 /* End lock entry functions. Start in lock function. */
   1597 
   1598 /* Register read/write routines and barriers. */
   1599 #ifdef SLHCI_BUS_SPACE_BARRIERS
   1600 #define BSB(a, b, c, d, e) bus_space_barrier(a, b, c, d, BUS_SPACE_BARRIER_ # e)
   1601 #define BSB_SYNC(a, b, c, d) bus_space_barrier(a, b, c, d, BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
   1602 #else /* now !SLHCI_BUS_SPACE_BARRIERS */
   1603 #define BSB(a, b, c, d, e) __USE(d)
   1604 #define BSB_SYNC(a, b, c, d)
   1605 #endif /* SLHCI_BUS_SPACE_BARRIERS */
   1606 
   1607 static void
   1608 slhci_write(struct slhci_softc *sc, uint8_t addr, uint8_t data)
   1609 {
   1610 	bus_size_t paddr, pdata, pst, psz;
   1611 	bus_space_tag_t iot;
   1612 	bus_space_handle_t ioh;
   1613 
   1614 	paddr = pst = 0;
   1615 	pdata = sc->sc_stride;
   1616 	psz = pdata * 2;
   1617 	iot = sc->sc_iot;
   1618 	ioh = sc->sc_ioh;
   1619 
   1620 	bus_space_write_1(iot, ioh, paddr, addr);
   1621 	BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
   1622 	bus_space_write_1(iot, ioh, pdata, data);
   1623 	BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
   1624 }
   1625 
   1626 static uint8_t
   1627 slhci_read(struct slhci_softc *sc, uint8_t addr)
   1628 {
   1629 	bus_size_t paddr, pdata, pst, psz;
   1630 	bus_space_tag_t iot;
   1631 	bus_space_handle_t ioh;
   1632 	uint8_t data;
   1633 
   1634 	paddr = pst = 0;
   1635 	pdata = sc->sc_stride;
   1636 	psz = pdata * 2;
   1637 	iot = sc->sc_iot;
   1638 	ioh = sc->sc_ioh;
   1639 
   1640 	bus_space_write_1(iot, ioh, paddr, addr);
   1641 	BSB(iot, ioh, pst, psz, WRITE_BEFORE_READ);
   1642 	data = bus_space_read_1(iot, ioh, pdata);
   1643 	BSB(iot, ioh, pst, psz, READ_BEFORE_WRITE);
   1644 	return data;
   1645 }
   1646 
   1647 #if 0 /* auto-increment mode broken, see errata doc */
   1648 static void
   1649 slhci_write_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
   1650 {
   1651 	bus_size_t paddr, pdata, pst, psz;
   1652 	bus_space_tag_t iot;
   1653 	bus_space_handle_t ioh;
   1654 
   1655 	paddr = pst = 0;
   1656 	pdata = sc->sc_stride;
   1657 	psz = pdata * 2;
   1658 	iot = sc->sc_iot;
   1659 	ioh = sc->sc_ioh;
   1660 
   1661 	bus_space_write_1(iot, ioh, paddr, addr);
   1662 	BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
   1663 	bus_space_write_multi_1(iot, ioh, pdata, buf, l);
   1664 	BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
   1665 }
   1666 
   1667 static void
   1668 slhci_read_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
   1669 {
   1670 	bus_size_t paddr, pdata, pst, psz;
   1671 	bus_space_tag_t iot;
   1672 	bus_space_handle_t ioh;
   1673 
   1674 	paddr = pst = 0;
   1675 	pdata = sc->sc_stride;
   1676 	psz = pdata * 2;
   1677 	iot = sc->sc_iot;
   1678 	ioh = sc->sc_ioh;
   1679 
   1680 	bus_space_write_1(iot, ioh, paddr, addr);
   1681 	BSB(iot, ioh, pst, psz, WRITE_BEFORE_READ);
   1682 	bus_space_read_multi_1(iot, ioh, pdata, buf, l);
   1683 	BSB(iot, ioh, pst, psz, READ_BEFORE_WRITE);
   1684 }
   1685 #else
   1686 static void
   1687 slhci_write_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
   1688 {
   1689 #if 1
   1690 	for (; l; addr++, buf++, l--)
   1691 		slhci_write(sc, addr, *buf);
   1692 #else
   1693 	bus_size_t paddr, pdata, pst, psz;
   1694 	bus_space_tag_t iot;
   1695 	bus_space_handle_t ioh;
   1696 
   1697 	paddr = pst = 0;
   1698 	pdata = sc->sc_stride;
   1699 	psz = pdata * 2;
   1700 	iot = sc->sc_iot;
   1701 	ioh = sc->sc_ioh;
   1702 
   1703 	for (; l; addr++, buf++, l--) {
   1704 		bus_space_write_1(iot, ioh, paddr, addr);
   1705 		BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
   1706 		bus_space_write_1(iot, ioh, pdata, *buf);
   1707 		BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
   1708 	}
   1709 #endif
   1710 }
   1711 
   1712 static void
   1713 slhci_read_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
   1714 {
   1715 #if 1
   1716 	for (; l; addr++, buf++, l--)
   1717 		*buf = slhci_read(sc, addr);
   1718 #else
   1719 	bus_size_t paddr, pdata, pst, psz;
   1720 	bus_space_tag_t iot;
   1721 	bus_space_handle_t ioh;
   1722 
   1723 	paddr = pst = 0;
   1724 	pdata = sc->sc_stride;
   1725 	psz = pdata * 2;
   1726 	iot = sc->sc_iot;
   1727 	ioh = sc->sc_ioh;
   1728 
   1729 	for (; l; addr++, buf++, l--) {
   1730 		bus_space_write_1(iot, ioh, paddr, addr);
   1731 		BSB(iot, ioh, pst, psz, WRITE_BEFORE_READ);
   1732 		*buf = bus_space_read_1(iot, ioh, pdata);
   1733 		BSB(iot, ioh, pst, psz, READ_BEFORE_WRITE);
   1734 	}
   1735 #endif
   1736 }
   1737 #endif
   1738 
   1739 /*
   1740  * After calling waitintr it is necessary to either call slhci_callback or
   1741  * schedule the callback if necessary.  The callback cannot be called directly
   1742  * from the hard interrupt since it interrupts at a high IPL and callbacks
   1743  * can do copyout and such.
   1744  */
   1745 static void
   1746 slhci_waitintr(struct slhci_softc *sc, int wait_time)
   1747 {
   1748 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1749 	struct slhci_transfers *t;
   1750 
   1751 	t = &sc->sc_transfers;
   1752 
   1753 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   1754 
   1755 	if (__predict_false(sc->sc_bus.ub_usepolling))
   1756 		wait_time = 12000;
   1757 
   1758 	while (t->pend <= wait_time) {
   1759 		DLOG(D_WAIT, "waiting... frame %jd pend %jd flags %#jx",
   1760 		    t->frame, t->pend, t->flags, 0);
   1761 		LK_SLASSERT(t->flags & F_ACTIVE, sc, NULL, NULL, return);
   1762 		LK_SLASSERT(t->flags & (F_AINPROG|F_BINPROG), sc, NULL, NULL,
   1763 		    return);
   1764 		slhci_dointr(sc);
   1765 	}
   1766 	DLOG(D_WAIT, "... done", 0, 0, 0, 0);
   1767 }
   1768 
   1769 static int
   1770 slhci_dointr(struct slhci_softc *sc)
   1771 {
   1772 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1773 	struct slhci_transfers *t;
   1774 	struct slhci_pipe *tosp;
   1775 	uint8_t r;
   1776 
   1777 	t = &sc->sc_transfers;
   1778 
   1779 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   1780 
   1781 	if (sc->sc_ier == 0) {
   1782 		DLOG(D_INTR, "sc_ier is zero", 0, 0, 0, 0);
   1783 		return 0;
   1784 	}
   1785 
   1786 	r = slhci_read(sc, SL11_ISR);
   1787 
   1788 #ifdef SLHCI_DEBUG
   1789 	if (slhcidebug & SLHCI_D_INTR && r & sc->sc_ier &&
   1790 	    ((r & ~(SL11_ISR_SOF|SL11_ISR_DATA)) || slhcidebug & SLHCI_D_SOF)) {
   1791 		uint8_t e, f;
   1792 
   1793 		e = slhci_read(sc, SL11_IER);
   1794 		f = slhci_read(sc, SL11_CTRL);
   1795 		DDOLOG("Flags=%#x IER=%#x ISR=%#x CTRL=%#x", t->flags, e, r, f);
   1796 		DDOLOGCTRL(f);
   1797 		DDOLOGISR(r);
   1798 	}
   1799 #endif
   1800 
   1801 	/*
   1802 	 * check IER for corruption occasionally.  Assume that the above
   1803 	 * sc_ier == 0 case works correctly.
   1804 	 */
   1805 	if (__predict_false(sc->sc_ier_check++ > SLHCI_IER_CHECK_FREQUENCY)) {
   1806 		sc->sc_ier_check = 0;
   1807 		if (sc->sc_ier != slhci_read(sc, SL11_IER)) {
   1808 			printf("%s: IER value corrupted! halted\n",
   1809 			    SC_NAME(sc));
   1810 			DDOLOG("IER value corrupted! halted", 0, 0, 0, 0);
   1811 			slhci_halt(sc, NULL, NULL);
   1812 			return 1;
   1813 		}
   1814 	}
   1815 
   1816 	r &= sc->sc_ier;
   1817 
   1818 	if (r == 0) {
   1819 		DLOG(D_INTR, "r is zero", 0, 0, 0, 0);
   1820 		return 0;
   1821 	}
   1822 
   1823 	sc->sc_ier_check = 0;
   1824 
   1825 	slhci_write(sc, SL11_ISR, r);
   1826 	BSB_SYNC(sc->iot, sc->ioh, sc->pst, sc->psz);
   1827 
   1828 	/* If we have an insertion event we do not care about anything else. */
   1829 	if (__predict_false(r & SL11_ISR_INSERT)) {
   1830 		slhci_insert(sc);
   1831 		DLOG(D_INTR, "... done", 0, 0, 0, 0);
   1832 		return 1;
   1833 	}
   1834 
   1835 	stop_cc_time(&t_intr);
   1836 	start_cc_time(&t_intr, r);
   1837 
   1838 	if (r & SL11_ISR_SOF) {
   1839 		t->frame++;
   1840 
   1841 		gcq_merge_tail(&t->q[Q_CB], &t->q[Q_NEXT_CB]);
   1842 
   1843 		/*
   1844 		 * SOFCHECK flags are cleared in tstart.  Two flags are needed
   1845 		 * since the first SOF interrupt processed after the transfer
   1846 		 * is started might have been generated before the transfer
   1847 		 * was started.
   1848 		 */
   1849 		if (__predict_false(t->flags & F_SOFCHECK2 && t->flags &
   1850 		    (F_AINPROG|F_BINPROG))) {
   1851 			printf("%s: Missed transfer completion. halted\n",
   1852 			    SC_NAME(sc));
   1853 			DDOLOG("Missed transfer completion. halted", 0, 0, 0,
   1854 			    0);
   1855 			slhci_halt(sc, NULL, NULL);
   1856 			return 1;
   1857 		} else if (t->flags & F_SOFCHECK1) {
   1858 			t->flags |= F_SOFCHECK2;
   1859 		} else
   1860 			t->flags |= F_SOFCHECK1;
   1861 
   1862 		if (t->flags & F_CHANGE)
   1863 			t->flags |= F_ROOTINTR;
   1864 
   1865 		while (__predict_true(GOT_FIRST_TO(tosp, t)) &&
   1866 		    __predict_false(tosp->to_frame <= t->frame)) {
   1867 			tosp->xfer->ux_status = USBD_TIMEOUT;
   1868 			slhci_do_abort(sc, tosp, tosp->xfer);
   1869 			enter_callback(t, tosp);
   1870 		}
   1871 
   1872 		/*
   1873 		 * Start any waiting transfers right away.  If none, we will
   1874 		 * start any new transfers later.
   1875 		 */
   1876 		slhci_tstart(sc);
   1877 	}
   1878 
   1879 	if (r & (SL11_ISR_USBA|SL11_ISR_USBB)) {
   1880 		int ab;
   1881 
   1882 		if ((r & (SL11_ISR_USBA|SL11_ISR_USBB)) ==
   1883 		    (SL11_ISR_USBA|SL11_ISR_USBB)) {
   1884 			if (!(t->flags & (F_AINPROG|F_BINPROG)))
   1885 				return 1; /* presume card pulled */
   1886 
   1887 			LK_SLASSERT((t->flags & (F_AINPROG|F_BINPROG)) !=
   1888 			    (F_AINPROG|F_BINPROG), sc, NULL, NULL, return 1);
   1889 
   1890 			/*
   1891 			 * This should never happen (unless card removal just
   1892 			 * occurred) but appeared frequently when both
   1893 			 * transfers were started at the same time and was
   1894 			 * accompanied by data corruption.  It still happens
   1895 			 * at times.  I have not seen data correption except
   1896 			 * when the STATUS bit gets set, which now causes the
   1897 			 * driver to halt, however this should still not
   1898 			 * happen so the warning is kept.  See comment in
   1899 			 * abdone, below.
   1900 			 */
   1901 			printf("%s: Transfer reported done but not started! "
   1902 			    "Verify data integrity if not detaching. "
   1903 			    " flags %#x r %x\n", SC_NAME(sc), t->flags, r);
   1904 
   1905 			if (!(t->flags & F_AINPROG))
   1906 				r &= ~SL11_ISR_USBA;
   1907 			else
   1908 				r &= ~SL11_ISR_USBB;
   1909 		}
   1910 		t->pend = INT_MAX;
   1911 
   1912 		if (r & SL11_ISR_USBA)
   1913 			ab = A;
   1914 		else
   1915 			ab = B;
   1916 
   1917 		/*
   1918 		 * This happens when a low speed device is attached to
   1919 		 * a hub with chip rev 1.5.  SOF stops, but a few transfers
   1920 		 * still work before causing this error.
   1921 		 */
   1922 		if (!(t->flags & (ab ? F_BINPROG : F_AINPROG))) {
   1923 			printf("%s: %s done but not in progress! halted\n",
   1924 			    SC_NAME(sc), ab ? "B" : "A");
   1925 			DDOLOG("AB=%d done but not in progress! halted", ab,
   1926 			    0, 0, 0);
   1927 			slhci_halt(sc, NULL, NULL);
   1928 			return 1;
   1929 		}
   1930 
   1931 		t->flags &= ~(ab ? F_BINPROG : F_AINPROG);
   1932 		slhci_tstart(sc);
   1933 		stop_cc_time(&t_ab[ab]);
   1934 		start_cc_time(&t_abdone, t->flags);
   1935 		slhci_abdone(sc, ab);
   1936 		stop_cc_time(&t_abdone);
   1937 	}
   1938 
   1939 	slhci_dotransfer(sc);
   1940 
   1941 	DLOG(D_INTR, "... done", 0, 0, 0, 0);
   1942 
   1943 	return 1;
   1944 }
   1945 
   1946 static void
   1947 slhci_abdone(struct slhci_softc *sc, int ab)
   1948 {
   1949 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1950 	struct slhci_transfers *t;
   1951 	struct slhci_pipe *spipe;
   1952 	struct usbd_xfer *xfer;
   1953 	uint8_t status, buf_start;
   1954 	uint8_t *target_buf;
   1955 	unsigned int actlen;
   1956 	int head;
   1957 
   1958 	t = &sc->sc_transfers;
   1959 
   1960 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   1961 
   1962 	DLOG(D_TRACE, "ABDONE flags %#jx", t->flags, 0,0,0);
   1963 
   1964 	DLOG(D_MSG, "DONE AB=%jd spipe %#jx len %jd xfer %#jx", ab,
   1965 	    t->spipe[ab], (uintptr_t)t->len[ab],
   1966 	    (uintptr_t)(t->spipe[ab] ? t->spipe[ab]->xfer : NULL));
   1967 
   1968 	spipe = t->spipe[ab];
   1969 
   1970 	/*
   1971 	 * skip this one if aborted; do not call return from the rest of the
   1972 	 * function unless halting, else t->len will not be cleared.
   1973 	 */
   1974 	if (spipe == NULL)
   1975 		goto done;
   1976 
   1977 	t->spipe[ab] = NULL;
   1978 
   1979 	xfer = spipe->xfer;
   1980 
   1981 	gcq_remove(&spipe->to);
   1982 
   1983 	LK_SLASSERT(xfer != NULL, sc, spipe, NULL, return);
   1984 
   1985 	status = slhci_read(sc, slhci_tregs[ab][STAT]);
   1986 
   1987 	/*
   1988 	 * I saw no status or remaining length greater than the requested
   1989 	 * length in early driver versions in circumstances I assumed caused
   1990 	 * excess power draw.  I am no longer able to reproduce this when
   1991 	 * causing excess power draw circumstances.
   1992 	 *
   1993 	 * Disabling a power check and attaching aue to a keyboard and hub
   1994 	 * that is directly attached (to CFU1U, 100mA max, aue 160mA, keyboard
   1995 	 * 98mA) sometimes works and sometimes fails to configure.  After
   1996 	 * removing the aue and attaching a self-powered umass dvd reader
   1997 	 * (unknown if it draws power from the host also) soon a single Error
   1998 	 * status occurs then only timeouts. The controller soon halts freeing
   1999 	 * memory due to being ONQU instead of BUSY.  This may be the same
   2000 	 * basic sequence that caused the no status/bad length errors.  The
   2001 	 * umass device seems to work (better at least) with the keyboard hub
   2002 	 * when not first attaching aue (tested once reading an approximately
   2003 	 * 200MB file).
   2004 	 *
   2005 	 * Overflow can indicate that the device and host disagree about how
   2006 	 * much data has been transferred.  This may indicate a problem at any
   2007 	 * point during the transfer, not just when the error occurs.  It may
   2008 	 * indicate data corruption.  A warning message is printed.
   2009 	 *
   2010 	 * Trying to use both A and B transfers at the same time results in
   2011 	 * incorrect transfer completion ISR reports and the status will then
   2012 	 * include SL11_EPSTAT_SETUP, which is apparently set while the
   2013 	 * transfer is in progress.  I also noticed data corruption, even
   2014 	 * after waiting for the transfer to complete. The driver now avoids
   2015 	 * trying to start both at the same time.
   2016 	 *
   2017 	 * I had accidently initialized the B registers before they were valid
   2018 	 * in some driver versions.  Since every other performance enhancing
   2019 	 * feature has been confirmed buggy in the errata doc, I have not
   2020 	 * tried both transfers at once again with the documented
   2021 	 * initialization order.
   2022 	 *
   2023 	 * However, I have seen this problem again ("done but not started"
   2024 	 * errors), which in some cases cases the SETUP status bit to remain
   2025 	 * set on future transfers.  In other cases, the SETUP bit is not set
   2026 	 * and no data corruption occurs.  This occured while using both umass
   2027 	 * and aue on a powered hub (maybe triggered by some local activity
   2028 	 * also) and needs several reads of the 200MB file to trigger.  The
   2029 	 * driver now halts if SETUP is detected.
   2030  	 */
   2031 
   2032 	actlen = 0;
   2033 
   2034 	if (__predict_false(!status)) {
   2035 		DDOLOG("no status! xfer %p spipe %p", xfer, spipe, 0,0);
   2036 		printf("%s: no status! halted\n", SC_NAME(sc));
   2037 		slhci_halt(sc, spipe, xfer);
   2038 		return;
   2039 	}
   2040 
   2041 #ifdef SLHCI_DEBUG
   2042 	if ((slhcidebug & SLHCI_D_NAK) ||
   2043 	    (status & SL11_EPSTAT_ERRBITS) != SL11_EPSTAT_NAK) {
   2044 	    	DDOLOG("USB Status = %#.2x", status, 0, 0, 0);
   2045 		DDOLOGSTATUS(status);
   2046 	}
   2047 #endif
   2048 
   2049 	if (!(status & SL11_EPSTAT_ERRBITS)) {
   2050 		unsigned int cont = slhci_read(sc, slhci_tregs[ab][CONT]);
   2051 		unsigned int len = spipe->tregs[LEN];
   2052 		DLOG(D_XFER, "cont %jd len %jd", cont, len, 0, 0);
   2053 		if ((status & SL11_EPSTAT_OVERFLOW) || cont > len) {
   2054 			DDOLOG("overflow - cont %d len %d xfer->ux_length %d "
   2055 			    "xfer->actlen %d", cont, len, xfer->ux_length,
   2056 			    xfer->ux_actlen);
   2057 			printf("%s: overflow cont %d len %d xfer->ux_length"
   2058 			    " %d xfer->ux_actlen %d\n", SC_NAME(sc), cont,
   2059 			    len, xfer->ux_length, xfer->ux_actlen);
   2060 			actlen = len;
   2061 		} else {
   2062 			actlen = len - cont;
   2063 		}
   2064 		spipe->nerrs = 0;
   2065 	}
   2066 
   2067 	/* Actual copyin done after starting next transfer. */
   2068 	if (actlen && (spipe->tregs[PID] & SL11_PID_BITS) == SL11_PID_IN) {
   2069 		target_buf = spipe->buffer;
   2070 		buf_start = spipe->tregs[ADR];
   2071 	} else {
   2072 		target_buf = NULL;
   2073 		buf_start = 0; /* XXX gcc uninitialized warnings */
   2074 	}
   2075 
   2076 	if (status & SL11_EPSTAT_ERRBITS) {
   2077 		status &= SL11_EPSTAT_ERRBITS;
   2078 		if (status & SL11_EPSTAT_SETUP) {
   2079 			printf("%s: Invalid controller state detected! "
   2080 			    "halted\n", SC_NAME(sc));
   2081 			DDOLOG("Invalid controller state detected! "
   2082 			    "halted", 0, 0, 0, 0);
   2083 			slhci_halt(sc, spipe, xfer);
   2084 			return;
   2085 		} else if (__predict_false(sc->sc_bus.ub_usepolling)) {
   2086 			head = Q_CALLBACKS;
   2087 			if (status & SL11_EPSTAT_STALL)
   2088 				xfer->ux_status = USBD_STALLED;
   2089 			else if (status & SL11_EPSTAT_TIMEOUT)
   2090 				xfer->ux_status = USBD_TIMEOUT;
   2091 			else if (status & SL11_EPSTAT_NAK)
   2092 				head = Q_NEXT_CB;
   2093 			else
   2094 				xfer->ux_status = USBD_IOERROR;
   2095 		} else if (status & SL11_EPSTAT_NAK) {
   2096 			int i = spipe->pipe.up_interval;
   2097 			if (i == 0)
   2098 				i = 1;
   2099 			DDOLOG("xfer %p spipe %p NAK delay by %d", xfer, spipe,
   2100 			    i, 0);
   2101 			spipe->lastframe = spipe->frame = t->frame + i;
   2102 			slhci_queue_timed(sc, spipe);
   2103 			goto queued;
   2104 		} else if (++spipe->nerrs > SLHCI_MAX_RETRIES ||
   2105 		    (status & SL11_EPSTAT_STALL)) {
   2106 			DDOLOG("xfer %p spipe %p nerrs %d", xfer, spipe,
   2107 			    spipe->nerrs, 0);
   2108 			if (status & SL11_EPSTAT_STALL)
   2109 				xfer->ux_status = USBD_STALLED;
   2110 			else if (status & SL11_EPSTAT_TIMEOUT)
   2111 				xfer->ux_status = USBD_TIMEOUT;
   2112 			else
   2113 				xfer->ux_status = USBD_IOERROR;
   2114 
   2115 			DLOG(D_ERR, "Max retries reached! status %#jx "
   2116 			    "xfer->ux_status %jd", status, xfer->ux_status, 0,
   2117 			    0);
   2118 			DDOLOGSTATUS(status);
   2119 
   2120 			head = Q_CALLBACKS;
   2121 		} else {
   2122 			head = Q_NEXT_CB;
   2123 		}
   2124 	} else if (spipe->ptype == PT_CTRL_SETUP) {
   2125 		spipe->tregs[PID] = spipe->newpid;
   2126 
   2127 		if (xfer->ux_length) {
   2128 			LK_SLASSERT(spipe->newlen[1] != 0, sc, spipe, xfer,
   2129 			    return);
   2130 			spipe->tregs[LEN] = spipe->newlen[1];
   2131 			spipe->bustime = spipe->newbustime[1];
   2132 			spipe->buffer = xfer->ux_buf;
   2133 			spipe->ptype = PT_CTRL_DATA;
   2134 		} else {
   2135 status_setup:
   2136 			/* CTRL_DATA swaps direction in PID then jumps here */
   2137 			spipe->tregs[LEN] = 0;
   2138 			if (spipe->pflags & PF_LS)
   2139 				spipe->bustime = SLHCI_LS_CONST;
   2140 			else
   2141 				spipe->bustime = SLHCI_FS_CONST;
   2142 			spipe->ptype = PT_CTRL_STATUS;
   2143 			spipe->buffer = NULL;
   2144 		}
   2145 
   2146 		/* Status or first data packet must be DATA1. */
   2147 		spipe->control |= SL11_EPCTRL_DATATOGGLE;
   2148 		if ((spipe->tregs[PID] & SL11_PID_BITS) == SL11_PID_IN)
   2149 			spipe->control &= ~SL11_EPCTRL_DIRECTION;
   2150 		else
   2151 			spipe->control |= SL11_EPCTRL_DIRECTION;
   2152 
   2153 		head = Q_CB;
   2154 	} else if (spipe->ptype == PT_CTRL_STATUS) {
   2155 		head = Q_CALLBACKS;
   2156 	} else { /* bulk, intr, control data */
   2157 		xfer->ux_actlen += actlen;
   2158 		spipe->control ^= SL11_EPCTRL_DATATOGGLE;
   2159 
   2160 		if (actlen == spipe->tregs[LEN] &&
   2161 		    (xfer->ux_length > xfer->ux_actlen || spipe->wantshort)) {
   2162 			spipe->buffer += actlen;
   2163 			LK_SLASSERT(xfer->ux_length >= xfer->ux_actlen, sc,
   2164 			    spipe, xfer, return);
   2165 			if (xfer->ux_length - xfer->ux_actlen < actlen) {
   2166 				spipe->wantshort = 0;
   2167 				spipe->tregs[LEN] = spipe->newlen[0];
   2168 				spipe->bustime = spipe->newbustime[0];
   2169 				LK_SLASSERT(xfer->ux_actlen +
   2170 				    spipe->tregs[LEN] == xfer->ux_length, sc,
   2171 				    spipe, xfer, return);
   2172 			}
   2173 			head = Q_CB;
   2174 		} else if (spipe->ptype == PT_CTRL_DATA) {
   2175 			spipe->tregs[PID] ^= SLHCI_PID_SWAP_IN_OUT;
   2176 			goto status_setup;
   2177 		} else {
   2178 			if (spipe->ptype == PT_INTR) {
   2179 				spipe->lastframe +=
   2180 				    spipe->pipe.up_interval;
   2181 				/*
   2182 				 * If ack, we try to keep the
   2183 				 * interrupt rate by using lastframe
   2184 				 * instead of the current frame.
   2185 				 */
   2186 				spipe->frame = spipe->lastframe +
   2187 				    spipe->pipe.up_interval;
   2188 			}
   2189 
   2190 			/*
   2191 			 * Set the toggle for the next transfer.  It
   2192 			 * has already been toggled above, so the
   2193 			 * current setting will apply to the next
   2194 			 * transfer.
   2195 			 */
   2196 			if (spipe->control & SL11_EPCTRL_DATATOGGLE)
   2197 				spipe->pflags |= PF_TOGGLE;
   2198 			else
   2199 				spipe->pflags &= ~PF_TOGGLE;
   2200 
   2201 			head = Q_CALLBACKS;
   2202 		}
   2203 	}
   2204 
   2205 	if (head == Q_CALLBACKS) {
   2206 		gcq_remove(&spipe->to);
   2207 
   2208 	 	if (xfer->ux_status == USBD_IN_PROGRESS) {
   2209 			LK_SLASSERT(xfer->ux_actlen <= xfer->ux_length, sc,
   2210 			    spipe, xfer, return);
   2211 			xfer->ux_status = USBD_NORMAL_COMPLETION;
   2212 		}
   2213 	}
   2214 
   2215 	enter_q(t, spipe, head);
   2216 
   2217 queued:
   2218 	if (target_buf != NULL) {
   2219 		slhci_dotransfer(sc);
   2220 		start_cc_time(&t_copy_from_dev, actlen);
   2221 		slhci_read_multi(sc, buf_start, target_buf, actlen);
   2222 		stop_cc_time(&t_copy_from_dev);
   2223 		DLOGBUF(D_BUF, target_buf, actlen);
   2224 		t->pend -= SLHCI_FS_CONST + SLHCI_FS_DATA_TIME(actlen);
   2225 	}
   2226 
   2227 done:
   2228 	t->len[ab] = -1;
   2229 }
   2230 
   2231 static void
   2232 slhci_tstart(struct slhci_softc *sc)
   2233 {
   2234 	struct slhci_transfers *t;
   2235 	struct slhci_pipe *spipe;
   2236 	int remaining_bustime;
   2237 
   2238 	t = &sc->sc_transfers;
   2239 
   2240 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2241 
   2242 	if (!(t->flags & (F_AREADY|F_BREADY)))
   2243 		return;
   2244 
   2245 	if (t->flags & (F_AINPROG|F_BINPROG|F_DISABLED))
   2246 		return;
   2247 
   2248 	/*
   2249 	 * We have about 6 us to get from the bus time check to
   2250 	 * starting the transfer or we might babble or the chip might fail to
   2251 	 * signal transfer complete.  This leaves no time for any other
   2252 	 * interrupts.
   2253 	 */
   2254 	remaining_bustime = (int)(slhci_read(sc, SL811_CSOF)) << 6;
   2255 	remaining_bustime -= SLHCI_END_BUSTIME;
   2256 
   2257 	/*
   2258 	 * Start one transfer only, clearing any aborted transfers that are
   2259 	 * not yet in progress and skipping missed isoc. It is easier to copy
   2260 	 * & paste most of the A/B sections than to make the logic work
   2261 	 * otherwise and this allows better constant use.
   2262 	 */
   2263 	if (t->flags & F_AREADY) {
   2264 		spipe = t->spipe[A];
   2265 		if (spipe == NULL) {
   2266 			t->flags &= ~F_AREADY;
   2267 			t->len[A] = -1;
   2268 		} else if (remaining_bustime >= spipe->bustime) {
   2269 			t->flags &= ~(F_AREADY|F_SOFCHECK1|F_SOFCHECK2);
   2270 			t->flags |= F_AINPROG;
   2271 			start_cc_time(&t_ab[A], spipe->tregs[LEN]);
   2272 			slhci_write(sc, SL11_E0CTRL, spipe->control);
   2273 			goto pend;
   2274 		}
   2275 	}
   2276 	if (t->flags & F_BREADY) {
   2277 		spipe = t->spipe[B];
   2278 		if (spipe == NULL) {
   2279 			t->flags &= ~F_BREADY;
   2280 			t->len[B] = -1;
   2281 		} else if (remaining_bustime >= spipe->bustime) {
   2282 			t->flags &= ~(F_BREADY|F_SOFCHECK1|F_SOFCHECK2);
   2283 			t->flags |= F_BINPROG;
   2284 			start_cc_time(&t_ab[B], spipe->tregs[LEN]);
   2285 			slhci_write(sc, SL11_E1CTRL, spipe->control);
   2286 pend:
   2287 			t->pend = spipe->bustime;
   2288 		}
   2289 	}
   2290 }
   2291 
   2292 static void
   2293 slhci_dotransfer(struct slhci_softc *sc)
   2294 {
   2295 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2296 	struct slhci_transfers *t;
   2297 	struct slhci_pipe *spipe;
   2298 	int ab, i;
   2299 
   2300 	t = &sc->sc_transfers;
   2301 
   2302 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2303 
   2304  	while ((t->len[A] == -1 || t->len[B] == -1) &&
   2305 	    (GOT_FIRST_TIMED_COND(spipe, t, spipe->frame <= t->frame) ||
   2306 	    GOT_FIRST_CB(spipe, t))) {
   2307 		LK_SLASSERT(spipe->xfer != NULL, sc, spipe, NULL, return);
   2308 		LK_SLASSERT(spipe->ptype != PT_ROOT_CTRL && spipe->ptype !=
   2309 		    PT_ROOT_INTR, sc, spipe, NULL, return);
   2310 
   2311 		/* Check that this transfer can fit in the remaining memory. */
   2312 		if (t->len[A] + t->len[B] + spipe->tregs[LEN] + 1 >
   2313 		    SL11_MAX_PACKET_SIZE) {
   2314 			DLOG(D_XFER, "Transfer does not fit. alen %jd blen %jd "
   2315 			    "len %jd", t->len[A], t->len[B], spipe->tregs[LEN],
   2316 			    0);
   2317 			return;
   2318 		}
   2319 
   2320 		gcq_remove(&spipe->xq);
   2321 
   2322 		if (t->len[A] == -1) {
   2323 			ab = A;
   2324 			spipe->tregs[ADR] = SL11_BUFFER_START;
   2325 		} else {
   2326 			ab = B;
   2327 			spipe->tregs[ADR] = SL11_BUFFER_END -
   2328 			    spipe->tregs[LEN];
   2329 		}
   2330 
   2331 		t->len[ab] = spipe->tregs[LEN];
   2332 
   2333 		if (spipe->tregs[LEN] && (spipe->tregs[PID] & SL11_PID_BITS)
   2334 		    != SL11_PID_IN) {
   2335 			start_cc_time(&t_copy_to_dev,
   2336 			    spipe->tregs[LEN]);
   2337 			slhci_write_multi(sc, spipe->tregs[ADR],
   2338 			    spipe->buffer, spipe->tregs[LEN]);
   2339 			stop_cc_time(&t_copy_to_dev);
   2340 			t->pend -= SLHCI_FS_CONST +
   2341 			    SLHCI_FS_DATA_TIME(spipe->tregs[LEN]);
   2342 		}
   2343 
   2344 		DLOG(D_MSG, "NEW TRANSFER AB=%jd flags %#jx alen %jd blen %jd",
   2345 		    ab, t->flags, t->len[0], t->len[1]);
   2346 
   2347 		if (spipe->tregs[LEN])
   2348 			i = 0;
   2349 		else
   2350 			i = 1;
   2351 
   2352 		for (; i <= 3; i++)
   2353 			if (t->current_tregs[ab][i] != spipe->tregs[i]) {
   2354 				t->current_tregs[ab][i] = spipe->tregs[i];
   2355 				slhci_write(sc, slhci_tregs[ab][i],
   2356 				    spipe->tregs[i]);
   2357 			}
   2358 
   2359 		DLOG(D_SXFER, "Transfer len %jd pid %#jx dev %jd type %jd",
   2360 		    spipe->tregs[LEN], spipe->tregs[PID], spipe->tregs[DEV],
   2361 	    	    spipe->ptype);
   2362 
   2363 		t->spipe[ab] = spipe;
   2364 		t->flags |= ab ? F_BREADY : F_AREADY;
   2365 
   2366 		slhci_tstart(sc);
   2367 	}
   2368 }
   2369 
   2370 /*
   2371  * slhci_callback is called after the lock is taken.
   2372  */
   2373 static void
   2374 slhci_callback(struct slhci_softc *sc)
   2375 {
   2376 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2377 	struct slhci_transfers *t;
   2378 	struct slhci_pipe *spipe;
   2379 	struct usbd_xfer *xfer;
   2380 
   2381 	t = &sc->sc_transfers;
   2382 
   2383 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2384 
   2385 	DLOG(D_SOFT, "CB flags %#jx", t->flags, 0,0,0);
   2386 	for (;;) {
   2387 		if (__predict_false(t->flags & F_ROOTINTR)) {
   2388 			t->flags &= ~F_ROOTINTR;
   2389 			if (t->rootintr != NULL) {
   2390 				u_char *p;
   2391 
   2392 				p = t->rootintr->ux_buf;
   2393 				p[0] = 2;
   2394 				t->rootintr->ux_actlen = 1;
   2395 				t->rootintr->ux_status = USBD_NORMAL_COMPLETION;
   2396 				xfer = t->rootintr;
   2397 				goto do_callback;
   2398 			}
   2399 		}
   2400 
   2401 
   2402 		if (!DEQUEUED_CALLBACK(spipe, t))
   2403 			return;
   2404 
   2405 		xfer = spipe->xfer;
   2406 		LK_SLASSERT(xfer != NULL, sc, spipe, NULL, return);
   2407 		spipe->xfer = NULL;
   2408 		DLOG(D_XFER, "xfer callback length %jd actlen %jd spipe %#jx "
   2409 		    "type %jd", xfer->ux_length, (uintptr_t)xfer->ux_actlen,
   2410 		    (uintptr_t)spipe, spipe->ptype);
   2411 do_callback:
   2412 		slhci_do_callback(sc, xfer);
   2413 	}
   2414 }
   2415 
   2416 static void
   2417 slhci_enter_xfer(struct slhci_softc *sc, struct slhci_pipe *spipe)
   2418 {
   2419 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2420 	struct slhci_transfers *t;
   2421 
   2422 	t = &sc->sc_transfers;
   2423 
   2424 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2425 
   2426 	if (__predict_false(t->flags & F_DISABLED) ||
   2427 	    __predict_false(spipe->pflags & PF_GONE)) {
   2428 		DLOG(D_MSG, "slhci_enter_xfer: DISABLED or GONE", 0,0,0,0);
   2429 		spipe->xfer->ux_status = USBD_CANCELLED;
   2430 	}
   2431 
   2432 	if (spipe->xfer->ux_status == USBD_IN_PROGRESS) {
   2433 		if (spipe->xfer->ux_timeout) {
   2434 			spipe->to_frame = t->frame + spipe->xfer->ux_timeout;
   2435 			slhci_xfer_timer(sc, spipe);
   2436 		}
   2437 		if (spipe->pipe.up_interval)
   2438 			slhci_queue_timed(sc, spipe);
   2439 		else
   2440 			enter_q(t, spipe, Q_CB);
   2441 	} else
   2442 		enter_callback(t, spipe);
   2443 }
   2444 
   2445 static void
   2446 slhci_enter_xfers(struct slhci_softc *sc)
   2447 {
   2448 	struct slhci_pipe *spipe;
   2449 
   2450 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2451 
   2452 	while (DEQUEUED_WAITQ(spipe, sc))
   2453 		slhci_enter_xfer(sc, spipe);
   2454 }
   2455 
   2456 static void
   2457 slhci_queue_timed(struct slhci_softc *sc, struct slhci_pipe *spipe)
   2458 {
   2459 	struct slhci_transfers *t;
   2460 	struct gcq *q;
   2461 	struct slhci_pipe *spp;
   2462 
   2463 	t = &sc->sc_transfers;
   2464 
   2465 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2466 
   2467 	FIND_TIMED(q, t, spp, spp->frame > spipe->frame);
   2468 	gcq_insert_before(q, &spipe->xq);
   2469 }
   2470 
   2471 static void
   2472 slhci_xfer_timer(struct slhci_softc *sc, struct slhci_pipe *spipe)
   2473 {
   2474 	struct slhci_transfers *t;
   2475 	struct gcq *q;
   2476 	struct slhci_pipe *spp;
   2477 
   2478 	t = &sc->sc_transfers;
   2479 
   2480 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2481 
   2482 	FIND_TO(q, t, spp, spp->to_frame >= spipe->to_frame);
   2483 	gcq_insert_before(q, &spipe->to);
   2484 }
   2485 
   2486 static void
   2487 slhci_callback_schedule(struct slhci_softc *sc)
   2488 {
   2489 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2490 	struct slhci_transfers *t;
   2491 
   2492 	t = &sc->sc_transfers;
   2493 
   2494 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2495 
   2496 	if (t->flags & F_ACTIVE)
   2497 		slhci_do_callback_schedule(sc);
   2498 }
   2499 
   2500 static void
   2501 slhci_do_callback_schedule(struct slhci_softc *sc)
   2502 {
   2503 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2504 	struct slhci_transfers *t;
   2505 
   2506 	t = &sc->sc_transfers;
   2507 
   2508 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2509 
   2510 	DLOG(D_MSG, "flags %#jx", t->flags, 0, 0, 0);
   2511 	if (!(t->flags & F_CALLBACK)) {
   2512 		t->flags |= F_CALLBACK;
   2513 		softint_schedule(sc->sc_cb_softintr);
   2514 	}
   2515 }
   2516 
   2517 #if 0
   2518 /* must be called with lock taken. */
   2519 /* XXX static */ void
   2520 slhci_pollxfer(struct slhci_softc *sc, struct usbd_xfer *xfer)
   2521 {
   2522 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2523 	slhci_dotransfer(sc);
   2524 	do {
   2525 		slhci_dointr(sc);
   2526 	} while (xfer->ux_status == USBD_IN_PROGRESS);
   2527 	slhci_do_callback(sc, xfer);
   2528 }
   2529 #endif
   2530 
   2531 static usbd_status
   2532 slhci_do_poll(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
   2533     usbd_xfer *xfer)
   2534 {
   2535 	slhci_waitintr(sc, 0);
   2536 
   2537 	return USBD_NORMAL_COMPLETION;
   2538 }
   2539 
   2540 static usbd_status
   2541 slhci_lsvh_warn(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
   2542     usbd_xfer *xfer)
   2543 {
   2544 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2545 	struct slhci_transfers *t;
   2546 
   2547 	t = &sc->sc_transfers;
   2548 
   2549 	if (!(t->flags & F_LSVH_WARNED)) {
   2550 		printf("%s: Low speed device via hub disabled, "
   2551 		    "see slhci(4)\n", SC_NAME(sc));
   2552 		DDOLOG("Low speed device via hub disabled, "
   2553 		    "see slhci(4)", SC_NAME(sc), 0,0,0);
   2554 		t->flags |= F_LSVH_WARNED;
   2555 	}
   2556 	return USBD_INVAL;
   2557 }
   2558 
   2559 static usbd_status
   2560 slhci_isoc_warn(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
   2561     usbd_xfer *xfer)
   2562 {
   2563 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2564 	struct slhci_transfers *t;
   2565 
   2566 	t = &sc->sc_transfers;
   2567 
   2568 	if (!(t->flags & F_ISOC_WARNED)) {
   2569 		printf("%s: ISOC transfer not supported "
   2570 		    "(see slhci(4))\n", SC_NAME(sc));
   2571 		DDOLOG("ISOC transfer not supported "
   2572 		    "(see slhci(4))", 0, 0, 0, 0);
   2573 		t->flags |= F_ISOC_WARNED;
   2574 	}
   2575 	return USBD_INVAL;
   2576 }
   2577 
   2578 static usbd_status
   2579 slhci_open_pipe(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
   2580     usbd_xfer *xfer)
   2581 {
   2582 	struct slhci_transfers *t;
   2583 	struct usbd_pipe *pipe;
   2584 
   2585 	t = &sc->sc_transfers;
   2586 	pipe = &spipe->pipe;
   2587 
   2588 	if (t->flags & F_DISABLED)
   2589 		return USBD_CANCELLED;
   2590 	else if (pipe->up_interval && !slhci_reserve_bustime(sc, spipe, 1))
   2591 		return USBD_PENDING_REQUESTS;
   2592 	else {
   2593 		enter_all_pipes(t, spipe);
   2594 		return USBD_NORMAL_COMPLETION;
   2595 	}
   2596 }
   2597 
   2598 static usbd_status
   2599 slhci_close_pipe(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
   2600     usbd_xfer *xfer)
   2601 {
   2602 	struct usbd_pipe *pipe;
   2603 
   2604 	pipe = &spipe->pipe;
   2605 
   2606 	if (pipe->up_interval && spipe->ptype != PT_ROOT_INTR)
   2607 		slhci_reserve_bustime(sc, spipe, 0);
   2608 	gcq_remove(&spipe->ap);
   2609 	return USBD_NORMAL_COMPLETION;
   2610 }
   2611 
   2612 static usbd_status
   2613 slhci_do_abort(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
   2614     usbd_xfer *xfer)
   2615 {
   2616 	struct slhci_transfers *t;
   2617 
   2618 	t = &sc->sc_transfers;
   2619 
   2620 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2621 
   2622 	if (spipe->xfer == xfer) {
   2623 		if (spipe->ptype == PT_ROOT_INTR) {
   2624 			if (t->rootintr == spipe->xfer) /* XXX assert? */
   2625 				t->rootintr = NULL;
   2626 		} else {
   2627 			gcq_remove(&spipe->to);
   2628 			gcq_remove(&spipe->xq);
   2629 
   2630 			if (t->spipe[A] == spipe) {
   2631 				t->spipe[A] = NULL;
   2632 				if (!(t->flags & F_AINPROG))
   2633 					t->len[A] = -1;
   2634 			} else if (t->spipe[B] == spipe) {
   2635 					t->spipe[B] = NULL;
   2636 				if (!(t->flags & F_BINPROG))
   2637 					t->len[B] = -1;
   2638 			}
   2639 		}
   2640 
   2641 		if (xfer->ux_status != USBD_TIMEOUT) {
   2642 			spipe->xfer = NULL;
   2643 			spipe->pipe.up_repeat = 0; /* XXX timeout? */
   2644 		}
   2645 	}
   2646 
   2647 	return USBD_NORMAL_COMPLETION;
   2648 }
   2649 
   2650 /*
   2651  * Called to deactivate or stop use of the controller instead of panicking.
   2652  * Will cancel the xfer correctly even when not on a list.
   2653  */
   2654 static usbd_status
   2655 slhci_halt(struct slhci_softc *sc, struct slhci_pipe *spipe,
   2656     struct usbd_xfer *xfer)
   2657 {
   2658 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2659 	struct slhci_transfers *t;
   2660 
   2661 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2662 
   2663 	t = &sc->sc_transfers;
   2664 
   2665 	DDOLOG("Halt! sc %p spipe %p xfer %p", sc, spipe, xfer, 0);
   2666 
   2667 	if (spipe != NULL)
   2668 		slhci_log_spipe(spipe);
   2669 
   2670 	if (xfer != NULL)
   2671 		slhci_log_xfer(xfer);
   2672 
   2673 	if (spipe != NULL && xfer != NULL && spipe->xfer == xfer &&
   2674 	    !gcq_onlist(&spipe->xq) && t->spipe[A] != spipe && t->spipe[B] !=
   2675 	    spipe) {
   2676 		xfer->ux_status = USBD_CANCELLED;
   2677 		enter_callback(t, spipe);
   2678 	}
   2679 
   2680 	if (t->flags & F_ACTIVE) {
   2681 		slhci_intrchange(sc, 0);
   2682 		/*
   2683 		 * leave power on when halting in case flash devices or disks
   2684 		 * are attached, which may be writing and could be damaged
   2685 		 * by abrupt power loss.  The root hub clear power feature
   2686 		 * should still work after halting.
   2687 		 */
   2688 	}
   2689 
   2690 	t->flags &= ~F_ACTIVE;
   2691 	t->flags |= F_UDISABLED;
   2692 	if (!(t->flags & F_NODEV))
   2693 		t->flags |= F_NODEV|F_CCONNECT|F_ROOTINTR;
   2694 	slhci_drain(sc);
   2695 
   2696 	/* One last callback for the drain and device removal. */
   2697 	slhci_do_callback_schedule(sc);
   2698 
   2699 	return USBD_NORMAL_COMPLETION;
   2700 }
   2701 
   2702 /*
   2703  * There are three interrupt states: no interrupts during reset and after
   2704  * device deactivation, INSERT only for no device present but power on, and
   2705  * SOF, INSERT, ADONE, and BDONE when device is present.
   2706  */
   2707 static void
   2708 slhci_intrchange(struct slhci_softc *sc, uint8_t new_ier)
   2709 {
   2710 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2711 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2712 	if (sc->sc_ier != new_ier) {
   2713 		DLOG(D_INTR, "New IER %#jx", new_ier, 0, 0, 0);
   2714 		sc->sc_ier = new_ier;
   2715 		slhci_write(sc, SL11_IER, new_ier);
   2716 		BSB_SYNC(sc->iot, sc->ioh, sc->pst, sc->psz);
   2717 	}
   2718 }
   2719 
   2720 /*
   2721  * Drain: cancel all pending transfers and put them on the callback list and
   2722  * set the UDISABLED flag.  UDISABLED is cleared only by reset.
   2723  */
   2724 static void
   2725 slhci_drain(struct slhci_softc *sc)
   2726 {
   2727 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2728 	struct slhci_transfers *t;
   2729 	struct slhci_pipe *spipe;
   2730 	struct gcq *q;
   2731 	int i;
   2732 
   2733  	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2734 
   2735 	t = &sc->sc_transfers;
   2736 
   2737 	DLOG(D_MSG, "DRAIN flags %#jx", t->flags, 0,0,0);
   2738 
   2739 	t->pend = INT_MAX;
   2740 
   2741 	for (i=0; i<=1; i++) {
   2742 		t->len[i] = -1;
   2743 		if (t->spipe[i] != NULL) {
   2744 			enter_callback(t, t->spipe[i]);
   2745 			t->spipe[i] = NULL;
   2746 		}
   2747 	}
   2748 
   2749 	/* Merge the queues into the callback queue. */
   2750 	gcq_merge_tail(&t->q[Q_CALLBACKS], &t->q[Q_CB]);
   2751 	gcq_merge_tail(&t->q[Q_CALLBACKS], &t->q[Q_NEXT_CB]);
   2752 	gcq_merge_tail(&t->q[Q_CALLBACKS], &t->timed);
   2753 
   2754 	/*
   2755 	 * Cancel all pipes.  Note that not all of these may be on the
   2756 	 * callback queue yet; some could be in slhci_start, for example.
   2757 	 */
   2758 	FOREACH_AP(q, t, spipe) {
   2759 		spipe->pflags |= PF_GONE;
   2760 		spipe->pipe.up_repeat = 0;
   2761 		spipe->pipe.up_aborting = 1;
   2762 		if (spipe->xfer != NULL)
   2763 			spipe->xfer->ux_status = USBD_CANCELLED;
   2764 	}
   2765 
   2766 	gcq_remove_all(&t->to);
   2767 
   2768 	t->flags |= F_UDISABLED;
   2769 	t->flags &= ~(F_AREADY|F_BREADY|F_AINPROG|F_BINPROG|F_LOWSPEED);
   2770 }
   2771 
   2772 /*
   2773  * RESET: SL11_CTRL_RESETENGINE=1 and SL11_CTRL_JKSTATE=0 for 50ms
   2774  * reconfigure SOF after reset, must wait 2.5us before USB bus activity (SOF)
   2775  * check attached device speed.
   2776  * must wait 100ms before USB transaction according to app note, 10ms
   2777  * by spec.  uhub does this delay
   2778  *
   2779  * Started from root hub set feature reset, which does step one.
   2780  * ub_usepolling will call slhci_reset directly, otherwise the callout goes
   2781  * through slhci_reset_entry.
   2782  */
   2783 void
   2784 slhci_reset(struct slhci_softc *sc)
   2785 {
   2786 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2787 	struct slhci_transfers *t;
   2788 	struct slhci_pipe *spipe;
   2789 	struct gcq *q;
   2790 	uint8_t r, pol, ctrl;
   2791 
   2792 	t = &sc->sc_transfers;
   2793 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2794 
   2795 	stop_cc_time(&t_delay);
   2796 
   2797 	KASSERT(t->flags & F_ACTIVE);
   2798 
   2799 	start_cc_time(&t_delay, 0);
   2800 	stop_cc_time(&t_delay);
   2801 
   2802 	slhci_write(sc, SL11_CTRL, 0);
   2803 	start_cc_time(&t_delay, 3);
   2804 	DELAY(3);
   2805 	stop_cc_time(&t_delay);
   2806 	slhci_write(sc, SL11_ISR, 0xff);
   2807 
   2808 	r = slhci_read(sc, SL11_ISR);
   2809 
   2810 	if (r & SL11_ISR_INSERT)
   2811 		slhci_write(sc, SL11_ISR, SL11_ISR_INSERT);
   2812 
   2813 	if (r & SL11_ISR_NODEV) {
   2814 		DLOG(D_MSG, "NC", 0,0,0,0);
   2815 		/*
   2816 		 * Normally, the hard interrupt insert routine will issue
   2817 		 * CCONNECT, however we need to do it here if the detach
   2818 		 * happened during reset.
   2819 		 */
   2820 		if (!(t->flags & F_NODEV))
   2821 			t->flags |= F_CCONNECT|F_ROOTINTR|F_NODEV;
   2822 		slhci_intrchange(sc, SL11_IER_INSERT);
   2823 	} else {
   2824 		if (t->flags & F_NODEV)
   2825 			t->flags |= F_CCONNECT;
   2826 		t->flags &= ~(F_NODEV|F_LOWSPEED);
   2827 		if (r & SL11_ISR_DATA) {
   2828 			DLOG(D_MSG, "FS", 0,0,0,0);
   2829 			pol = ctrl = 0;
   2830 		} else {
   2831 			DLOG(D_MSG, "LS", 0,0,0,0);
   2832 			pol  = SL811_CSOF_POLARITY;
   2833 			ctrl = SL11_CTRL_LOWSPEED;
   2834 			t->flags |= F_LOWSPEED;
   2835 		}
   2836 
   2837 		/* Enable SOF auto-generation */
   2838 		t->frame = 0;	/* write to SL811_CSOF will reset frame */
   2839 		slhci_write(sc, SL11_SOFTIME, 0xe0);
   2840 		slhci_write(sc, SL811_CSOF, pol|SL811_CSOF_MASTER|0x2e);
   2841 		slhci_write(sc, SL11_CTRL, ctrl|SL11_CTRL_ENABLESOF);
   2842 
   2843 		/*
   2844 		 * According to the app note, ARM must be set
   2845 		 * for SOF generation to work.  We initialize all
   2846 		 * USBA registers here for current_tregs.
   2847 		 */
   2848 		slhci_write(sc, SL11_E0ADDR, SL11_BUFFER_START);
   2849 		slhci_write(sc, SL11_E0LEN, 0);
   2850 		slhci_write(sc, SL11_E0PID, SL11_PID_SOF);
   2851 		slhci_write(sc, SL11_E0DEV, 0);
   2852 		slhci_write(sc, SL11_E0CTRL, SL11_EPCTRL_ARM);
   2853 
   2854 		/*
   2855 		 * Initialize B registers.  This can't be done earlier since
   2856 		 * they are not valid until the SL811_CSOF register is written
   2857 		 * above due to SL11H compatability.
   2858 		 */
   2859 		slhci_write(sc, SL11_E1ADDR, SL11_BUFFER_END - 8);
   2860 		slhci_write(sc, SL11_E1LEN, 0);
   2861 		slhci_write(sc, SL11_E1PID, 0);
   2862 		slhci_write(sc, SL11_E1DEV, 0);
   2863 
   2864 		t->current_tregs[0][ADR] = SL11_BUFFER_START;
   2865 		t->current_tregs[0][LEN] = 0;
   2866 		t->current_tregs[0][PID] = SL11_PID_SOF;
   2867 		t->current_tregs[0][DEV] = 0;
   2868 		t->current_tregs[1][ADR] = SL11_BUFFER_END - 8;
   2869 		t->current_tregs[1][LEN] = 0;
   2870 		t->current_tregs[1][PID] = 0;
   2871 		t->current_tregs[1][DEV] = 0;
   2872 
   2873 		/* SOF start will produce USBA interrupt */
   2874 		t->len[A] = 0;
   2875 		t->flags |= F_AINPROG;
   2876 
   2877 		slhci_intrchange(sc, SLHCI_NORMAL_INTERRUPTS);
   2878 	}
   2879 
   2880 	t->flags &= ~(F_UDISABLED|F_RESET);
   2881 	t->flags |= F_CRESET|F_ROOTINTR;
   2882 	FOREACH_AP(q, t, spipe) {
   2883 		spipe->pflags &= ~PF_GONE;
   2884 		spipe->pipe.up_aborting = 0;
   2885 	}
   2886 	DLOG(D_MSG, "RESET done flags %#jx", t->flags, 0,0,0);
   2887 }
   2888 
   2889 
   2890 #ifdef SLHCI_DEBUG
   2891 static int
   2892 slhci_memtest(struct slhci_softc *sc)
   2893 {
   2894 	enum { ASC, DESC, EITHER = ASC };	/* direction */
   2895 	enum { READ, WRITE };			/* operation */
   2896 	const char *ptr, *elem;
   2897 	size_t i;
   2898 	const int low = SL11_BUFFER_START, high = SL11_BUFFER_END;
   2899 	int addr = 0, dir = ASC, op = READ;
   2900 	/* Extended March C- test algorithm (SOFs also) */
   2901 	const char test[] = "E(w0) A(r0w1r1) A(r1w0r0) D(r0w1) D(r1w0) E(r0)";
   2902 	char c;
   2903 	const uint8_t dbs[] = { 0x00, 0x0f, 0x33, 0x55 }; /* data backgrounds */
   2904 	uint8_t db;
   2905 
   2906 	/* Perform memory test for all data backgrounds. */
   2907 	for (i = 0; i < __arraycount(dbs); i++) {
   2908 		ptr = test;
   2909 		elem = ptr;
   2910 		/* Walk test algorithm string. */
   2911 		while ((c = *ptr++) != '\0')
   2912 			switch (tolower((int)c)) {
   2913 			case 'a':
   2914 				/* Address sequence is in ascending order. */
   2915 				dir = ASC;
   2916 				break;
   2917 			case 'd':
   2918 				/* Address sequence is in descending order. */
   2919 				dir = DESC;
   2920 				break;
   2921 			case 'e':
   2922 				/* Address sequence is in either order. */
   2923 				dir = EITHER;
   2924 				break;
   2925 			case '(':
   2926 				/* Start of test element (sequence). */
   2927 				elem = ptr;
   2928 				addr = (dir == ASC) ? low : high;
   2929 				break;
   2930 			case 'r':
   2931 				/* read operation */
   2932 				op = READ;
   2933 				break;
   2934 			case 'w':
   2935 				/* write operation */
   2936 				op = WRITE;
   2937 				break;
   2938 			case '0':
   2939 			case '1':
   2940 				/*
   2941 				 * Execute previously set-up operation by
   2942 				 * reading/writing non-inverted ('0') or
   2943 				 * inverted ('1') data background.
   2944 				 */
   2945 				db = (c - '0') ? ~dbs[i] : dbs[i];
   2946 				if (op == READ) {
   2947 					if (slhci_read(sc, addr) != db)
   2948 						return -1;
   2949 				} else
   2950 					slhci_write(sc, addr, db);
   2951 				break;
   2952 			case ')':
   2953 				/*
   2954 				 * End of element: Repeat same element with next
   2955 				 * address or continue to next element.
   2956 				 */
   2957 				addr = (dir == ASC) ? addr + 1 : addr - 1;
   2958 				if (addr >= low && addr <= high)
   2959 					ptr = elem;
   2960 				break;
   2961 			default:
   2962 				/* Do nothing. */
   2963 				break;
   2964 			}
   2965 	}
   2966 
   2967 	return 0;
   2968 }
   2969 #endif
   2970 
   2971 /* returns 1 if succeeded, 0 if failed, reserve == 0 is unreserve */
   2972 static int
   2973 slhci_reserve_bustime(struct slhci_softc *sc, struct slhci_pipe *spipe, int
   2974     reserve)
   2975 {
   2976 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2977 	struct slhci_transfers *t;
   2978 	int bustime, max_packet;
   2979 
   2980 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2981 
   2982 	t = &sc->sc_transfers;
   2983 	max_packet = UGETW(spipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize);
   2984 
   2985 	if (spipe->pflags & PF_LS)
   2986 		bustime = SLHCI_LS_CONST + SLHCI_LS_DATA_TIME(max_packet);
   2987 	else
   2988 		bustime = SLHCI_FS_CONST + SLHCI_FS_DATA_TIME(max_packet);
   2989 
   2990 	if (!reserve) {
   2991 		t->reserved_bustime -= bustime;
   2992 #ifdef DIAGNOSTIC
   2993 		if (t->reserved_bustime < 0) {
   2994 			printf("%s: reserved_bustime %d < 0!\n",
   2995 			    SC_NAME(sc), t->reserved_bustime);
   2996 			DDOLOG("reserved_bustime %d < 0!",
   2997 			    t->reserved_bustime, 0, 0, 0);
   2998 			t->reserved_bustime = 0;
   2999 		}
   3000 #endif
   3001 		return 1;
   3002 	}
   3003 
   3004 	if (t->reserved_bustime + bustime > SLHCI_RESERVED_BUSTIME) {
   3005 		if (ratecheck(&sc->sc_reserved_warn_rate,
   3006 		    &reserved_warn_rate))
   3007 #ifdef SLHCI_NO_OVERTIME
   3008 		{
   3009 			printf("%s: Max reserved bus time exceeded! "
   3010 			    "Erroring request.\n", SC_NAME(sc));
   3011 			DDOLOG("%s: Max reserved bus time exceeded! "
   3012 			    "Erroring request.", 0, 0, 0, 0);
   3013 		}
   3014 		return 0;
   3015 #else
   3016 		{
   3017 			printf("%s: Reserved bus time exceeds %d!\n",
   3018 			    SC_NAME(sc), SLHCI_RESERVED_BUSTIME);
   3019 			DDOLOG("Reserved bus time exceeds %d!",
   3020 			    SLHCI_RESERVED_BUSTIME, 0, 0, 0);
   3021 		}
   3022 #endif
   3023 	}
   3024 
   3025 	t->reserved_bustime += bustime;
   3026 	return 1;
   3027 }
   3028 
   3029 /* Device insertion/removal interrupt */
   3030 static void
   3031 slhci_insert(struct slhci_softc *sc)
   3032 {
   3033 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3034 	struct slhci_transfers *t;
   3035 
   3036 	t = &sc->sc_transfers;
   3037 
   3038 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   3039 
   3040 	if (t->flags & F_NODEV)
   3041 		slhci_intrchange(sc, 0);
   3042 	else {
   3043 		slhci_drain(sc);
   3044 		slhci_intrchange(sc, SL11_IER_INSERT);
   3045 	}
   3046 	t->flags ^= F_NODEV;
   3047 	t->flags |= F_ROOTINTR|F_CCONNECT;
   3048 	DLOG(D_MSG, "INSERT intr: flags after %#jx", t->flags, 0,0,0);
   3049 }
   3050 
   3051 /*
   3052  * Data structures and routines to emulate the root hub.
   3053  */
   3054 
   3055 static usbd_status
   3056 slhci_clear_feature(struct slhci_softc *sc, unsigned int what)
   3057 {
   3058 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3059 	struct slhci_transfers *t;
   3060 	usbd_status error;
   3061 
   3062 	t = &sc->sc_transfers;
   3063 	error = USBD_NORMAL_COMPLETION;
   3064 
   3065 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   3066 
   3067 	if (what == UHF_PORT_POWER) {
   3068 		DLOG(D_MSG, "POWER_OFF", 0,0,0,0);
   3069 		t->flags &= ~F_POWER;
   3070 		if (!(t->flags & F_NODEV))
   3071 			t->flags |= F_NODEV|F_CCONNECT|F_ROOTINTR;
   3072 		/* for x68k Nereid USB controller */
   3073 		if (sc->sc_enable_power && (t->flags & F_REALPOWER)) {
   3074 			t->flags &= ~F_REALPOWER;
   3075 			sc->sc_enable_power(sc, POWER_OFF);
   3076 		}
   3077 		slhci_intrchange(sc, 0);
   3078 		slhci_drain(sc);
   3079 	} else if (what == UHF_C_PORT_CONNECTION) {
   3080 		t->flags &= ~F_CCONNECT;
   3081 	} else if (what == UHF_C_PORT_RESET) {
   3082 		t->flags &= ~F_CRESET;
   3083 	} else if (what == UHF_PORT_ENABLE) {
   3084 		slhci_drain(sc);
   3085 	} else if (what != UHF_PORT_SUSPEND) {
   3086 		DDOLOG("ClrPortFeatERR:value=%#.4x", what, 0,0,0);
   3087 		error = USBD_IOERROR;
   3088 	}
   3089 
   3090 	return error;
   3091 }
   3092 
   3093 static usbd_status
   3094 slhci_set_feature(struct slhci_softc *sc, unsigned int what)
   3095 {
   3096 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3097 	struct slhci_transfers *t;
   3098 	uint8_t r;
   3099 
   3100 	t = &sc->sc_transfers;
   3101 
   3102 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   3103 
   3104 	if (what == UHF_PORT_RESET) {
   3105 		if (!(t->flags & F_ACTIVE)) {
   3106 			DDOLOG("SET PORT_RESET when not ACTIVE!",
   3107 			    0,0,0,0);
   3108 			return USBD_INVAL;
   3109 		}
   3110 		if (!(t->flags & F_POWER)) {
   3111 			DDOLOG("SET PORT_RESET without PORT_POWER! flags %p",
   3112 			    t->flags, 0,0,0);
   3113 			return USBD_INVAL;
   3114 		}
   3115 		if (t->flags & F_RESET)
   3116 			return USBD_NORMAL_COMPLETION;
   3117 		DLOG(D_MSG, "RESET flags %#jx", t->flags, 0,0,0);
   3118 		slhci_intrchange(sc, 0);
   3119 		slhci_drain(sc);
   3120 		slhci_write(sc, SL11_CTRL, SL11_CTRL_RESETENGINE);
   3121 		/* usb spec says delay >= 10ms, app note 50ms */
   3122  		start_cc_time(&t_delay, 50000);
   3123 		if (sc->sc_bus.ub_usepolling) {
   3124 			DELAY(50000);
   3125 			slhci_reset(sc);
   3126 		} else {
   3127 			t->flags |= F_RESET;
   3128 			callout_schedule(&sc->sc_timer, uimax(mstohz(50), 2));
   3129 		}
   3130 	} else if (what == UHF_PORT_SUSPEND) {
   3131 		printf("%s: USB Suspend not implemented!\n", SC_NAME(sc));
   3132 		DDOLOG("USB Suspend not implemented!", 0, 0, 0, 0);
   3133 	} else if (what == UHF_PORT_POWER) {
   3134 		DLOG(D_MSG, "PORT_POWER", 0,0,0,0);
   3135 		/* for x68k Nereid USB controller */
   3136 		if (!(t->flags & F_ACTIVE))
   3137 			return USBD_INVAL;
   3138 		if (t->flags & F_POWER)
   3139 			return USBD_NORMAL_COMPLETION;
   3140 		if (!(t->flags & F_REALPOWER)) {
   3141 			if (sc->sc_enable_power)
   3142 				sc->sc_enable_power(sc, POWER_ON);
   3143 			t->flags |= F_REALPOWER;
   3144 		}
   3145 		t->flags |= F_POWER;
   3146 		r = slhci_read(sc, SL11_ISR);
   3147 		if (r & SL11_ISR_INSERT)
   3148 			slhci_write(sc, SL11_ISR, SL11_ISR_INSERT);
   3149 		if (r & SL11_ISR_NODEV) {
   3150 			slhci_intrchange(sc, SL11_IER_INSERT);
   3151 			t->flags |= F_NODEV;
   3152 		} else {
   3153 			t->flags &= ~F_NODEV;
   3154 			t->flags |= F_CCONNECT|F_ROOTINTR;
   3155 		}
   3156 	} else {
   3157 		DDOLOG("SetPortFeatERR=%#.8x", what, 0,0,0);
   3158 		return USBD_IOERROR;
   3159 	}
   3160 
   3161 	return USBD_NORMAL_COMPLETION;
   3162 }
   3163 
   3164 static void
   3165 slhci_get_status(struct slhci_softc *sc, usb_port_status_t *ps)
   3166 {
   3167 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3168 	struct slhci_transfers *t;
   3169 	unsigned int status, change;
   3170 
   3171 	t = &sc->sc_transfers;
   3172 
   3173 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   3174 
   3175 	/*
   3176 	 * We do not have a way to detect over current or babble and
   3177 	 * suspend is currently not implemented, so connect and reset
   3178 	 * are the only changes that need to be reported.
   3179 	 */
   3180 	change = 0;
   3181 	if (t->flags & F_CCONNECT)
   3182 		change |= UPS_C_CONNECT_STATUS;
   3183 	if (t->flags & F_CRESET)
   3184 		change |= UPS_C_PORT_RESET;
   3185 
   3186 	status = 0;
   3187 	if (!(t->flags & F_NODEV))
   3188 		status |= UPS_CURRENT_CONNECT_STATUS;
   3189 	if (!(t->flags & F_UDISABLED))
   3190 		status |= UPS_PORT_ENABLED;
   3191 	if (t->flags & F_RESET)
   3192 		status |= UPS_RESET;
   3193 	if (t->flags & F_POWER)
   3194 		status |= UPS_PORT_POWER;
   3195 	if (t->flags & F_LOWSPEED)
   3196 		status |= UPS_LOW_SPEED;
   3197 	USETW(ps->wPortStatus, status);
   3198 	USETW(ps->wPortChange, change);
   3199 	DLOG(D_ROOT, "status=%#.4jx, change=%#.4jx", status, change, 0,0);
   3200 }
   3201 
   3202 static int
   3203 slhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
   3204     void *buf, int buflen)
   3205 {
   3206 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3207 	struct slhci_softc *sc = SLHCI_BUS2SC(bus);
   3208 	struct slhci_transfers *t = &sc->sc_transfers;
   3209 	usbd_status error = USBD_IOERROR; /* XXX should be STALL */
   3210 	uint16_t len, value, index;
   3211 	uint8_t type;
   3212 	int actlen = 0;
   3213 
   3214 	len = UGETW(req->wLength);
   3215 	value = UGETW(req->wValue);
   3216 	index = UGETW(req->wIndex);
   3217 
   3218 	type = req->bmRequestType;
   3219 
   3220 	SLHCI_DEXEC(D_TRACE, slhci_log_req(req));
   3221 
   3222 	/*
   3223 	 * USB requests for hubs have two basic types, standard and class.
   3224 	 * Each could potentially have recipients of device, interface,
   3225 	 * endpoint, or other.  For the hub class, CLASS_OTHER means the port
   3226 	 * and CLASS_DEVICE means the hub.  For standard requests, OTHER
   3227 	 * is not used.  Standard request are described in section 9.4 of the
   3228 	 * standard, hub class requests in 11.16.  Each request is either read
   3229 	 * or write.
   3230 	 *
   3231 	 * Clear Feature, Set Feature, and Status are defined for each of the
   3232 	 * used recipients.  Get Descriptor and Set Descriptor are defined for
   3233 	 * both standard and hub class types with different descriptors.
   3234 	 * Other requests have only one defined recipient and type.  These
   3235 	 * include: Get/Set Address, Get/Set Configuration, Get/Set Interface,
   3236 	 * and Synch Frame for standard requests and Get Bus State for hub
   3237 	 * class.
   3238 	 *
   3239 	 * When a device is first powered up it has address 0 until the
   3240 	 * address is set.
   3241 	 *
   3242 	 * Hubs are only allowed to support one interface and may not have
   3243 	 * isochronous endpoints.  The results of the related requests are
   3244 	 * undefined.
   3245 	 *
   3246 	 * The standard requires invalid or unsupported requests to return
   3247 	 * STALL in the data stage, however this does not work well with
   3248 	 * current error handling. XXX
   3249 	 *
   3250 	 * Some unsupported fields:
   3251 	 * Clear Hub Feature is for C_HUB_LOCAL_POWER and C_HUB_OVER_CURRENT
   3252 	 * Set Device Features is for ENDPOINT_HALT and DEVICE_REMOTE_WAKEUP
   3253 	 * Get Bus State is optional sample of D- and D+ at EOF2
   3254 	 */
   3255 
   3256 	switch (req->bRequest) {
   3257 	/* Write Requests */
   3258 	case UR_CLEAR_FEATURE:
   3259 		if (type == UT_WRITE_CLASS_OTHER) {
   3260 			if (index == 1 /* Port */) {
   3261 				mutex_enter(&sc->sc_intr_lock);
   3262 				error = slhci_clear_feature(sc, value);
   3263 				mutex_exit(&sc->sc_intr_lock);
   3264 			} else
   3265 				DLOG(D_ROOT, "Clear Port Feature "
   3266 				    "index = %#.4jx", index, 0,0,0);
   3267 		}
   3268 		break;
   3269 	case UR_SET_FEATURE:
   3270 		if (type == UT_WRITE_CLASS_OTHER) {
   3271 			if (index == 1 /* Port */) {
   3272 				mutex_enter(&sc->sc_intr_lock);
   3273 				error = slhci_set_feature(sc, value);
   3274 				mutex_exit(&sc->sc_intr_lock);
   3275 			} else
   3276 				DLOG(D_ROOT, "Set Port Feature "
   3277 				    "index = %#.4jx", index, 0,0,0);
   3278 		} else if (type != UT_WRITE_CLASS_DEVICE)
   3279 			DLOG(D_ROOT, "Set Device Feature "
   3280 			    "ENDPOINT_HALT or DEVICE_REMOTE_WAKEUP "
   3281 			    "not supported", 0,0,0,0);
   3282 		break;
   3283 
   3284 	/* Read Requests */
   3285 	case UR_GET_STATUS:
   3286 		if (type == UT_READ_CLASS_OTHER) {
   3287 			if (index == 1 /* Port */ && len == /* XXX >=? */
   3288 			    sizeof(usb_port_status_t)) {
   3289 				mutex_enter(&sc->sc_intr_lock);
   3290 				slhci_get_status(sc, (usb_port_status_t *)
   3291 				    buf);
   3292 				mutex_exit(&sc->sc_intr_lock);
   3293 				actlen = sizeof(usb_port_status_t);
   3294 				error = USBD_NORMAL_COMPLETION;
   3295 			} else
   3296 				DLOG(D_ROOT, "Get Port Status index = %#.4jx "
   3297 				    "len = %#.4jx", index, len, 0,0);
   3298 		} else if (type == UT_READ_CLASS_DEVICE) { /* XXX index? */
   3299 			if (len == sizeof(usb_hub_status_t)) {
   3300 				DLOG(D_ROOT, "Get Hub Status",
   3301 				    0,0,0,0);
   3302 				actlen = sizeof(usb_hub_status_t);
   3303 				memset(buf, 0, actlen);
   3304 				error = USBD_NORMAL_COMPLETION;
   3305 			} else
   3306 				DLOG(D_ROOT, "Get Hub Status bad len %#.4jx",
   3307 				    len, 0,0,0);
   3308 		}
   3309 		break;
   3310 	case UR_GET_DESCRIPTOR:
   3311 		if (type == UT_READ_DEVICE) {
   3312 			/* value is type (&0xff00) and index (0xff) */
   3313 			if (value == (UDESC_DEVICE<<8)) {
   3314 				actlen = buflen;
   3315 				error = USBD_NORMAL_COMPLETION;
   3316 			} else if (value == (UDESC_CONFIG<<8)) {
   3317 				struct usb_roothub_descriptors confd;
   3318 
   3319 				actlen = uimin(buflen, sizeof(confd));
   3320 				memcpy(&confd, buf, actlen);
   3321 
   3322 				/* 2 mA units */
   3323 				confd.urh_confd.bMaxPower = t->max_current;
   3324 				memcpy(buf, &confd, actlen);
   3325 				error = USBD_NORMAL_COMPLETION;
   3326 			} else if (value == ((UDESC_STRING<<8)|1)) {
   3327 				/* Vendor */
   3328 				actlen = buflen;
   3329 				error = USBD_NORMAL_COMPLETION;
   3330 			} else if (value == ((UDESC_STRING<<8)|2)) {
   3331 				/* Product */
   3332 				actlen = usb_makestrdesc((usb_string_descriptor_t *)
   3333 				    buf, len, "SL811HS/T root hub");
   3334 				error = USBD_NORMAL_COMPLETION;
   3335 			} else
   3336 				DDOLOG("Unknown Get Descriptor %#.4x",
   3337 				    value, 0,0,0);
   3338 		} else if (type == UT_READ_CLASS_DEVICE) {
   3339 			/* Descriptor number is 0 */
   3340 			if (value == (UDESC_HUB<<8)) {
   3341 				usb_hub_descriptor_t hubd;
   3342 
   3343 				actlen = uimin(buflen, sizeof(hubd));
   3344 				memcpy(&hubd, buf, actlen);
   3345 				hubd.bHubContrCurrent =
   3346 				    500 - t->max_current;
   3347 				memcpy(buf, &hubd, actlen);
   3348 				error = USBD_NORMAL_COMPLETION;
   3349 			} else
   3350 				DDOLOG("Unknown Get Hub Descriptor %#.4x",
   3351 				    value, 0,0,0);
   3352 		}
   3353 		break;
   3354 	default:
   3355 		/* default from usbroothub */
   3356 		return buflen;
   3357 	}
   3358 
   3359 	if (error == USBD_NORMAL_COMPLETION)
   3360 		return actlen;
   3361 
   3362 	return -1;
   3363 }
   3364 
   3365 /* End in lock functions. Start debug functions. */
   3366 
   3367 #ifdef SLHCI_DEBUG
   3368 void
   3369 slhci_log_buffer(struct usbd_xfer *xfer)
   3370 {
   3371 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3372 	u_char *buf;
   3373 
   3374 	if(xfer->ux_length > 0 &&
   3375 	    UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) ==
   3376 	    UE_DIR_IN) {
   3377 		buf = xfer->ux_buf;
   3378 		DDOLOGBUF(buf, xfer->ux_actlen);
   3379 		DDOLOG("len %d actlen %d short %d", xfer->ux_length,
   3380 		    xfer->ux_actlen, xfer->ux_length - xfer->ux_actlen, 0);
   3381 	}
   3382 }
   3383 
   3384 void
   3385 slhci_log_req(usb_device_request_t *r)
   3386 {
   3387 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3388 	int req, type, value, index, len;
   3389 
   3390 	req   = r->bRequest;
   3391 	type  = r->bmRequestType;
   3392 	value = UGETW(r->wValue);
   3393 	index = UGETW(r->wIndex);
   3394 	len   = UGETW(r->wLength);
   3395 
   3396 	DDOLOG("request: type %#x", type, 0, 0, 0);
   3397 	DDOLOG("request: r=%d,v=%d,i=%d,l=%d ", req, value, index, len);
   3398 }
   3399 
   3400 void
   3401 slhci_log_dumpreg(void)
   3402 {
   3403 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3404 	uint8_t r;
   3405 	unsigned int aaddr, alen, baddr, blen;
   3406 	static u_char buf[240];
   3407 
   3408 	r = slhci_read(ssc, SL11_E0CTRL);
   3409 	DDOLOG("USB A Host Control = %#.2x", r, 0, 0, 0);
   3410 	DDOLOGEPCTRL(r);
   3411 
   3412 	aaddr = slhci_read(ssc, SL11_E0ADDR);
   3413 	DDOLOG("USB A Base Address = %u", aaddr, 0,0,0);
   3414 	alen = slhci_read(ssc, SL11_E0LEN);
   3415 	DDOLOG("USB A Length = %u", alen, 0,0,0);
   3416 	r = slhci_read(ssc, SL11_E0STAT);
   3417 	DDOLOG("USB A Status = %#.2x", r, 0,0,0);
   3418 	DDOLOGEPSTAT(r);
   3419 
   3420 	r = slhci_read(ssc, SL11_E0CONT);
   3421 	DDOLOG("USB A Remaining or Overflow Length = %u", r, 0,0,0);
   3422 	r = slhci_read(ssc, SL11_E1CTRL);
   3423 	DDOLOG("USB B Host Control = %#.2x", r, 0,0,0);
   3424 	DDOLOGEPCTRL(r);
   3425 
   3426 	baddr = slhci_read(ssc, SL11_E1ADDR);
   3427 	DDOLOG("USB B Base Address = %u", baddr, 0,0,0);
   3428 	blen = slhci_read(ssc, SL11_E1LEN);
   3429 	DDOLOG("USB B Length = %u", blen, 0,0,0);
   3430 	r = slhci_read(ssc, SL11_E1STAT);
   3431 	DDOLOG("USB B Status = %#.2x", r, 0,0,0);
   3432 	DDOLOGEPSTAT(r);
   3433 
   3434 	r = slhci_read(ssc, SL11_E1CONT);
   3435 	DDOLOG("USB B Remaining or Overflow Length = %u", r, 0,0,0);
   3436 
   3437 	r = slhci_read(ssc, SL11_CTRL);
   3438 	DDOLOG("Control = %#.2x", r, 0,0,0);
   3439 	DDOLOGCTRL(r);
   3440 
   3441 	r = slhci_read(ssc, SL11_IER);
   3442 	DDOLOG("Interrupt Enable = %#.2x", r, 0,0,0);
   3443 	DDOLOGIER(r);
   3444 
   3445 	r = slhci_read(ssc, SL11_ISR);
   3446 	DDOLOG("Interrupt Status = %#.2x", r, 0,0,0);
   3447 	DDOLOGISR(r);
   3448 
   3449 	r = slhci_read(ssc, SL11_REV);
   3450 	DDOLOG("Revision = %#.2x", r, 0,0,0);
   3451 	r = slhci_read(ssc, SL811_CSOF);
   3452 	DDOLOG("SOF Counter = %#.2x", r, 0,0,0);
   3453 
   3454 	if (alen && aaddr >= SL11_BUFFER_START && aaddr < SL11_BUFFER_END &&
   3455 	    alen <= SL11_MAX_PACKET_SIZE && aaddr + alen <= SL11_BUFFER_END) {
   3456 		slhci_read_multi(ssc, aaddr, buf, alen);
   3457 		DDOLOG("USBA Buffer: start %u len %u", aaddr, alen, 0,0);
   3458 		DDOLOGBUF(buf, alen);
   3459 	} else if (alen)
   3460 		DDOLOG("USBA Buffer Invalid", 0,0,0,0);
   3461 
   3462 	if (blen && baddr >= SL11_BUFFER_START && baddr < SL11_BUFFER_END &&
   3463 	    blen <= SL11_MAX_PACKET_SIZE && baddr + blen <= SL11_BUFFER_END) {
   3464 		slhci_read_multi(ssc, baddr, buf, blen);
   3465 		DDOLOG("USBB Buffer: start %u len %u", baddr, blen, 0,0);
   3466 		DDOLOGBUF(buf, blen);
   3467 	} else if (blen)
   3468 		DDOLOG("USBB Buffer Invalid", 0,0,0,0);
   3469 }
   3470 
   3471 void
   3472 slhci_log_xfer(struct usbd_xfer *xfer)
   3473 {
   3474 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3475 	DDOLOG("xfer: length=%u, actlen=%u, flags=%#x, timeout=%u,",
   3476 		xfer->ux_length, xfer->ux_actlen, xfer->ux_flags, xfer->ux_timeout);
   3477 	DDOLOG("buffer=%p", xfer->ux_buf, 0,0,0);
   3478 	slhci_log_req(&xfer->ux_request);
   3479 }
   3480 
   3481 void
   3482 slhci_log_spipe(struct slhci_pipe *spipe)
   3483 {
   3484 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3485 	DDOLOG("spipe %p onlists: AP=%d TO=%d XQ=%d", spipe,
   3486 	    gcq_onlist(&spipe->ap) ? 1 : 0,
   3487 	    gcq_onlist(&spipe->to) ? 1 : 0,
   3488 	    gcq_onlist(&spipe->xq) ? 1 : 0);
   3489 	DDOLOG("spipe: xfer %p buffer %p pflags %#x ptype %d",
   3490 	    spipe->xfer, spipe->buffer, spipe->pflags, spipe->ptype);
   3491 }
   3492 
   3493 void
   3494 slhci_print_intr(void)
   3495 {
   3496 	unsigned int ier, isr;
   3497 	ier = slhci_read(ssc, SL11_IER);
   3498 	isr = slhci_read(ssc, SL11_ISR);
   3499 	printf("IER: %#x ISR: %#x \n", ier, isr);
   3500 }
   3501 
   3502 #if 0
   3503 void
   3504 slhci_log_sc(void)
   3505 {
   3506 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3507 
   3508 	struct slhci_transfers *t;
   3509 	int i;
   3510 
   3511 	t = &ssc->sc_transfers;
   3512 
   3513 	DDOLOG("Flags=%#x", t->flags, 0,0,0);
   3514 	DDOLOG("a = %p Alen=%d b = %p Blen=%d", t->spipe[0], t->len[0],
   3515 	    t->spipe[1], t->len[1]);
   3516 
   3517 	for (i=0; i<=Q_MAX; i++)
   3518 		DDOLOG("Q %d: %p", i, gcq_hq(&t->q[i]), 0,0);
   3519 
   3520 	DDOLOG("TIMED: %p", GCQ_ITEM(gcq_hq(&t->to),
   3521 	    struct slhci_pipe, to), 0,0,0);
   3522 
   3523 	DDOLOG("frame=%d rootintr=%p", t->frame, t->rootintr, 0,0);
   3524 
   3525 	DDOLOG("ub_usepolling=%d", ssc->sc_bus.ub_usepolling, 0, 0, 0);
   3526 }
   3527 
   3528 void
   3529 slhci_log_slreq(struct slhci_pipe *r)
   3530 {
   3531 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3532 	DDOLOG("xfer: %p", r->xfer, 0,0,0);
   3533 	DDOLOG("buffer: %p", r->buffer, 0,0,0);
   3534 	DDOLOG("bustime: %u", r->bustime, 0,0,0);
   3535 	DDOLOG("control: %#x", r->control, 0,0,0);
   3536 	DDOLOGEPCTRL(r->control);
   3537 
   3538 	DDOLOG("pid: %#x", r->tregs[PID], 0,0,0);
   3539 	DDOLOG("dev: %u", r->tregs[DEV], 0,0,0);
   3540 	DDOLOG("len: %u", r->tregs[LEN], 0,0,0);
   3541 
   3542 	if (r->xfer)
   3543 		slhci_log_xfer(r->xfer);
   3544 }
   3545 #endif
   3546 #endif /* SLHCI_DEBUG */
   3547 /* End debug functions. */
   3548