Home | History | Annotate | Line # | Download | only in ic
sl811hs.c revision 1.105
      1 /*	$NetBSD: sl811hs.c,v 1.105 2021/04/24 23:36:55 thorpej 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.105 2021/04/24 23:36:55 thorpej 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 	KASSERT(t->rootintr == NULL);
   1034 	t->rootintr = xfer;
   1035 	xfer->ux_status = USBD_IN_PROGRESS;
   1036 	mutex_exit(&sc->sc_intr_lock);
   1037 
   1038 	return USBD_IN_PROGRESS;
   1039 }
   1040 
   1041 usbd_status
   1042 slhci_open(struct usbd_pipe *pipe)
   1043 {
   1044 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1045 	struct usbd_device *dev;
   1046 	struct slhci_softc *sc;
   1047 	struct slhci_pipe *spipe;
   1048 	usb_endpoint_descriptor_t *ed;
   1049 	unsigned int max_packet, pmaxpkt;
   1050 	uint8_t rhaddr;
   1051 
   1052 	dev = pipe->up_dev;
   1053 	sc = SLHCI_PIPE2SC(pipe);
   1054 	spipe = SLHCI_PIPE2SPIPE(pipe);
   1055 	ed = pipe->up_endpoint->ue_edesc;
   1056 	rhaddr = dev->ud_bus->ub_rhaddr;
   1057 
   1058 	DLOG(D_TRACE, "slhci_open(addr=%jd,ep=%jd,rootaddr=%jd)",
   1059 		dev->ud_addr, ed->bEndpointAddress, rhaddr, 0);
   1060 
   1061 	spipe->pflags = 0;
   1062 	spipe->frame = 0;
   1063 	spipe->lastframe = 0;
   1064 	spipe->xfer = NULL;
   1065 	spipe->buffer = NULL;
   1066 
   1067 	gcq_init(&spipe->ap);
   1068 	gcq_init(&spipe->to);
   1069 	gcq_init(&spipe->xq);
   1070 
   1071 	/*
   1072 	 * The endpoint descriptor will not have been set up yet in the case
   1073 	 * of the standard control pipe, so the max packet checks are also
   1074 	 * necessary in start.
   1075 	 */
   1076 
   1077 	max_packet = UGETW(ed->wMaxPacketSize);
   1078 
   1079 	if (dev->ud_speed == USB_SPEED_LOW) {
   1080 		spipe->pflags |= PF_LS;
   1081 		if (dev->ud_myhub->ud_addr != rhaddr) {
   1082 			spipe->pflags |= PF_PREAMBLE;
   1083 			if (!slhci_try_lsvh)
   1084 				return slhci_lock_call(sc, &slhci_lsvh_warn,
   1085 				    spipe, NULL);
   1086 		}
   1087 		pmaxpkt = 8;
   1088 	} else
   1089 		pmaxpkt = SL11_MAX_PACKET_SIZE;
   1090 
   1091 	if (max_packet > pmaxpkt) {
   1092 		DLOG(D_ERR, "packet too large! size %jd spipe %#jx", max_packet,
   1093 		    (uintptr_t)spipe, 0,0);
   1094 		return USBD_INVAL;
   1095 	}
   1096 
   1097 	if (dev->ud_addr == rhaddr) {
   1098 		switch (ed->bEndpointAddress) {
   1099 		case USB_CONTROL_ENDPOINT:
   1100 			spipe->ptype = PT_ROOT_CTRL;
   1101 			pipe->up_interval = 0;
   1102 			pipe->up_methods = &roothub_ctrl_methods;
   1103 			break;
   1104 		case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
   1105 			spipe->ptype = PT_ROOT_INTR;
   1106 			pipe->up_interval = 1;
   1107 			pipe->up_methods = &slhci_root_methods;
   1108 			break;
   1109 		default:
   1110 			printf("%s: Invalid root endpoint!\n", SC_NAME(sc));
   1111 			DDOLOG("Invalid root endpoint", 0, 0, 0, 0);
   1112 			return USBD_INVAL;
   1113 		}
   1114 		return USBD_NORMAL_COMPLETION;
   1115 	} else {
   1116 		switch (ed->bmAttributes & UE_XFERTYPE) {
   1117 		case UE_CONTROL:
   1118 			spipe->ptype = PT_CTRL_SETUP;
   1119 			pipe->up_interval = 0;
   1120 			break;
   1121 		case UE_INTERRUPT:
   1122 			spipe->ptype = PT_INTR;
   1123 			if (pipe->up_interval == USBD_DEFAULT_INTERVAL)
   1124 				pipe->up_interval = ed->bInterval;
   1125 			break;
   1126 		case UE_ISOCHRONOUS:
   1127 			return slhci_lock_call(sc, &slhci_isoc_warn, spipe,
   1128 			    NULL);
   1129 		case UE_BULK:
   1130 			spipe->ptype = PT_BULK;
   1131 			pipe->up_interval = 0;
   1132 			break;
   1133 		}
   1134 
   1135 		DLOG(D_MSG, "open pipe type %jd interval %jd", spipe->ptype,
   1136 		    pipe->up_interval, 0,0);
   1137 
   1138 		pipe->up_methods = __UNCONST(&slhci_pipe_methods);
   1139 
   1140 		return slhci_lock_call(sc, &slhci_open_pipe, spipe, NULL);
   1141 	}
   1142 }
   1143 
   1144 int
   1145 slhci_supported_rev(uint8_t rev)
   1146 {
   1147 	return rev >= SLTYPE_SL811HS_R12 && rev <= SLTYPE_SL811HS_R15;
   1148 }
   1149 
   1150 /*
   1151  * Must be called before the ISR is registered. Interrupts can be shared so
   1152  * slhci_intr could be called as soon as the ISR is registered.
   1153  * Note max_current argument is actual current, but stored as current/2
   1154  */
   1155 void
   1156 slhci_preinit(struct slhci_softc *sc, PowerFunc pow, bus_space_tag_t iot,
   1157     bus_space_handle_t ioh, uint16_t max_current, uint32_t stride)
   1158 {
   1159 	struct slhci_transfers *t;
   1160 	int i;
   1161 
   1162 	t = &sc->sc_transfers;
   1163 
   1164 #ifdef SLHCI_DEBUG
   1165 	ssc = sc;
   1166 #endif
   1167 
   1168 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
   1169 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
   1170 
   1171 	/* sc->sc_ier = 0;	*/
   1172 	/* t->rootintr = NULL;	*/
   1173 	t->flags = F_NODEV|F_UDISABLED;
   1174 	t->pend = INT_MAX;
   1175 	KASSERT(slhci_wait_time != INT_MAX);
   1176 	t->len[0] = t->len[1] = -1;
   1177 	if (max_current > 500)
   1178 		max_current = 500;
   1179 	t->max_current = (uint8_t)(max_current / 2);
   1180 	sc->sc_enable_power = pow;
   1181 	sc->sc_iot = iot;
   1182 	sc->sc_ioh = ioh;
   1183 	sc->sc_stride = stride;
   1184 
   1185 	KASSERT(Q_MAX+1 == sizeof(t->q) / sizeof(t->q[0]));
   1186 
   1187 	for (i = 0; i <= Q_MAX; i++)
   1188 		gcq_init_head(&t->q[i]);
   1189 	gcq_init_head(&t->timed);
   1190 	gcq_init_head(&t->to);
   1191 	gcq_init_head(&t->ap);
   1192 	gcq_init_head(&sc->sc_waitq);
   1193 }
   1194 
   1195 int
   1196 slhci_attach(struct slhci_softc *sc)
   1197 {
   1198 	struct slhci_transfers *t;
   1199 	const char *rev;
   1200 
   1201 	t = &sc->sc_transfers;
   1202 
   1203 	/* Detect and check the controller type */
   1204 	t->sltype = SL11_GET_REV(slhci_read(sc, SL11_REV));
   1205 
   1206 	/* SL11H not supported */
   1207 	if (!slhci_supported_rev(t->sltype)) {
   1208 		if (t->sltype == SLTYPE_SL11H)
   1209 			printf("%s: SL11H unsupported or bus error!\n",
   1210 			    SC_NAME(sc));
   1211 		else
   1212 			printf("%s: Unknown chip revision!\n", SC_NAME(sc));
   1213 		return -1;
   1214 	}
   1215 
   1216 #ifdef SLHCI_DEBUG
   1217 	if (slhci_memtest(sc)) {
   1218 		printf("%s: memory/bus error!\n", SC_NAME(sc));
   1219 		return -1;
   1220 	}
   1221 #endif
   1222 
   1223 	callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
   1224 	callout_setfunc(&sc->sc_timer, slhci_reset_entry, sc);
   1225 
   1226 	/*
   1227 	 * It is not safe to call the soft interrupt directly as
   1228 	 * usb_schedsoftintr does in the ub_usepolling case (due to locking).
   1229 	 */
   1230 	sc->sc_cb_softintr = softint_establish(SOFTINT_NET,
   1231 	    slhci_callback_entry, sc);
   1232 
   1233 	if (t->sltype == SLTYPE_SL811HS_R12)
   1234 		rev = "(rev 1.2)";
   1235 	else if (t->sltype == SLTYPE_SL811HS_R14)
   1236 		rev = "(rev 1.4 or 1.5)";
   1237 	else
   1238 		rev = "(unknown revision)";
   1239 
   1240 	aprint_normal("%s: ScanLogic SL811HS/T USB Host Controller %s\n",
   1241 	    SC_NAME(sc), rev);
   1242 
   1243 	aprint_normal("%s: Max Current %u mA (value by code, not by probe)\n",
   1244 	    SC_NAME(sc), t->max_current * 2);
   1245 
   1246 #if defined(SLHCI_DEBUG) || defined(SLHCI_NO_OVERTIME) || \
   1247     defined(SLHCI_TRY_LSVH) || defined(SLHCI_PROFILE_TRANSFER)
   1248 	aprint_normal("%s: driver options:"
   1249 #ifdef SLHCI_DEBUG
   1250 	" SLHCI_DEBUG"
   1251 #endif
   1252 #ifdef SLHCI_TRY_LSVH
   1253 	" SLHCI_TRY_LSVH"
   1254 #endif
   1255 #ifdef SLHCI_NO_OVERTIME
   1256 	" SLHCI_NO_OVERTIME"
   1257 #endif
   1258 #ifdef SLHCI_PROFILE_TRANSFER
   1259 	" SLHCI_PROFILE_TRANSFER"
   1260 #endif
   1261 	"\n", SC_NAME(sc));
   1262 #endif
   1263 	sc->sc_bus.ub_revision = USBREV_1_1;
   1264 	sc->sc_bus.ub_methods = __UNCONST(&slhci_bus_methods);
   1265 	sc->sc_bus.ub_pipesize = sizeof(struct slhci_pipe);
   1266 	sc->sc_bus.ub_usedma = false;
   1267 
   1268 	if (!sc->sc_enable_power)
   1269 		t->flags |= F_REALPOWER;
   1270 
   1271 	t->flags |= F_ACTIVE;
   1272 
   1273 	/* Attach usb and uhub. */
   1274 	sc->sc_child = config_found(SC_DEV(sc), &sc->sc_bus, usbctlprint,
   1275 	    CFARG_EOL);
   1276 
   1277 	if (!sc->sc_child)
   1278 		return -1;
   1279 	else
   1280 		return 0;
   1281 }
   1282 
   1283 int
   1284 slhci_detach(struct slhci_softc *sc, int flags)
   1285 {
   1286 	struct slhci_transfers *t;
   1287 	int ret;
   1288 
   1289 	t = &sc->sc_transfers;
   1290 
   1291 	/* By this point bus access is no longer allowed. */
   1292 
   1293 	KASSERT(!(t->flags & F_ACTIVE));
   1294 
   1295 	/*
   1296 	 * To be MPSAFE is not sufficient to cancel callouts and soft
   1297 	 * interrupts and assume they are dead since the code could already be
   1298 	 * running or about to run.  Wait until they are known to be done.
   1299 	 */
   1300 	while (t->flags & (F_RESET|F_CALLBACK))
   1301 		tsleep(&sc, PPAUSE, "slhci_detach", hz);
   1302 
   1303 	softint_disestablish(sc->sc_cb_softintr);
   1304 
   1305 	mutex_destroy(&sc->sc_lock);
   1306 	mutex_destroy(&sc->sc_intr_lock);
   1307 
   1308 	ret = 0;
   1309 
   1310 	if (sc->sc_child)
   1311 		ret = config_detach(sc->sc_child, flags);
   1312 
   1313 #ifdef SLHCI_MEM_ACCOUNTING
   1314 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1315 	if (sc->sc_mem_use) {
   1316 		printf("%s: Memory still in use after detach! mem_use (count)"
   1317 		    " = %d\n", SC_NAME(sc), sc->sc_mem_use);
   1318 		DDOLOG("Memory still in use after detach! mem_use (count)"
   1319 		    " = %d", sc->sc_mem_use, 0, 0, 0);
   1320 	}
   1321 #endif
   1322 
   1323 	return ret;
   1324 }
   1325 
   1326 int
   1327 slhci_activate(device_t self, enum devact act)
   1328 {
   1329 	struct slhci_softc *sc = device_private(self);
   1330 
   1331 	switch (act) {
   1332 	case DVACT_DEACTIVATE:
   1333 		slhci_lock_call(sc, &slhci_halt, NULL, NULL);
   1334 		return 0;
   1335 	default:
   1336 		return EOPNOTSUPP;
   1337 	}
   1338 }
   1339 
   1340 void
   1341 slhci_abort(struct usbd_xfer *xfer)
   1342 {
   1343 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1344 	struct slhci_softc *sc;
   1345 	struct slhci_pipe *spipe;
   1346 
   1347 	spipe = SLHCI_PIPE2SPIPE(xfer->ux_pipe);
   1348 
   1349 	if (spipe == NULL)
   1350 		goto callback;
   1351 
   1352 	sc = SLHCI_XFER2SC(xfer);
   1353 	KASSERT(mutex_owned(&sc->sc_lock));
   1354 
   1355 	DLOG(D_TRACE, "transfer type %jd abort xfer %#jx spipe %#jx "
   1356 	    " spipe->xfer %#jx", spipe->ptype, (uintptr_t)xfer,
   1357 	    (uintptr_t)spipe, (uintptr_t)spipe->xfer);
   1358 
   1359 	slhci_lock_call(sc, &slhci_do_abort, spipe, xfer);
   1360 
   1361 callback:
   1362 	xfer->ux_status = USBD_CANCELLED;
   1363 	usb_transfer_complete(xfer);
   1364 }
   1365 
   1366 void
   1367 slhci_close(struct usbd_pipe *pipe)
   1368 {
   1369 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1370 	struct slhci_softc *sc;
   1371 	struct slhci_pipe *spipe;
   1372 
   1373 	sc = SLHCI_PIPE2SC(pipe);
   1374 	spipe = SLHCI_PIPE2SPIPE(pipe);
   1375 
   1376 	DLOG(D_TRACE, "transfer type %jd close spipe %#jx spipe->xfer %#jx",
   1377 	    spipe->ptype, (uintptr_t)spipe, (uintptr_t)spipe->xfer, 0);
   1378 
   1379 	slhci_lock_call(sc, &slhci_close_pipe, spipe, NULL);
   1380 }
   1381 
   1382 void
   1383 slhci_clear_toggle(struct usbd_pipe *pipe)
   1384 {
   1385 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1386 	struct slhci_pipe *spipe;
   1387 
   1388 	spipe = SLHCI_PIPE2SPIPE(pipe);
   1389 
   1390 	DLOG(D_TRACE, "transfer type %jd toggle spipe %#jx", spipe->ptype,
   1391 	    (uintptr_t)spipe, 0, 0);
   1392 
   1393 	spipe->pflags &= ~PF_TOGGLE;
   1394 
   1395 #ifdef DIAGNOSTIC
   1396 	if (spipe->xfer != NULL) {
   1397 		struct slhci_softc *sc = (struct slhci_softc
   1398 		    *)pipe->up_dev->ud_bus;
   1399 
   1400 		printf("%s: Clear toggle on transfer in progress! halted\n",
   1401 		    SC_NAME(sc));
   1402 		DDOLOG("Clear toggle on transfer in progress! halted",
   1403 		    0, 0, 0, 0);
   1404 		slhci_halt(sc, NULL, NULL);
   1405 	}
   1406 #endif
   1407 }
   1408 
   1409 void
   1410 slhci_poll(struct usbd_bus *bus) /* XXX necessary? */
   1411 {
   1412 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1413 	struct slhci_softc *sc;
   1414 
   1415 	sc = SLHCI_BUS2SC(bus);
   1416 
   1417 	DLOG(D_TRACE, "slhci_poll", 0,0,0,0);
   1418 
   1419 	slhci_lock_call(sc, &slhci_do_poll, NULL, NULL);
   1420 }
   1421 
   1422 void
   1423 slhci_done(struct usbd_xfer *xfer)
   1424 {
   1425 }
   1426 
   1427 void
   1428 slhci_void(void *v) {}
   1429 
   1430 /* End out of lock functions. Start lock entry functions. */
   1431 
   1432 #ifdef SLHCI_MEM_ACCOUNTING
   1433 void
   1434 slhci_mem_use(struct usbd_bus *bus, int val)
   1435 {
   1436 	struct slhci_softc *sc = SLHCI_BUS2SC(bus);
   1437 
   1438 	mutex_enter(&sc->sc_intr_lock);
   1439 	sc->sc_mem_use += val;
   1440 	mutex_exit(&sc->sc_intr_lock);
   1441 }
   1442 #endif
   1443 
   1444 void
   1445 slhci_reset_entry(void *arg)
   1446 {
   1447 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1448 	struct slhci_softc *sc = arg;
   1449 
   1450 	mutex_enter(&sc->sc_intr_lock);
   1451 	slhci_reset(sc);
   1452 	/*
   1453 	 * We cannot call the callback directly since we could then be reset
   1454 	 * again before finishing and need the callout delay for timing.
   1455 	 * Scheduling the callout again before we exit would defeat the reap
   1456 	 * mechanism since we could be unlocked while the reset flag is not
   1457 	 * set. The callback code will check the wait queue.
   1458 	 */
   1459 	slhci_callback_schedule(sc);
   1460 	mutex_exit(&sc->sc_intr_lock);
   1461 }
   1462 
   1463 usbd_status
   1464 slhci_lock_call(struct slhci_softc *sc, LockCallFunc lcf, struct slhci_pipe
   1465     *spipe, struct usbd_xfer *xfer)
   1466 {
   1467 	usbd_status ret;
   1468 
   1469 	mutex_enter(&sc->sc_intr_lock);
   1470 	ret = (*lcf)(sc, spipe, xfer);
   1471 	slhci_main(sc);
   1472 	mutex_exit(&sc->sc_intr_lock);
   1473 
   1474 	return ret;
   1475 }
   1476 
   1477 void
   1478 slhci_start_entry(struct slhci_softc *sc, struct slhci_pipe *spipe)
   1479 {
   1480 	struct slhci_transfers *t;
   1481 
   1482 	mutex_enter(&sc->sc_intr_lock);
   1483 	t = &sc->sc_transfers;
   1484 
   1485 	if (!(t->flags & (F_AINPROG|F_BINPROG))) {
   1486 		slhci_enter_xfer(sc, spipe);
   1487 		slhci_dotransfer(sc);
   1488 		slhci_main(sc);
   1489 	} else {
   1490 		enter_waitq(sc, spipe);
   1491 	}
   1492 	mutex_exit(&sc->sc_intr_lock);
   1493 }
   1494 
   1495 void
   1496 slhci_callback_entry(void *arg)
   1497 {
   1498 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1499 	struct slhci_softc *sc;
   1500 	struct slhci_transfers *t;
   1501 
   1502 	sc = (struct slhci_softc *)arg;
   1503 
   1504 	mutex_enter(&sc->sc_intr_lock);
   1505 	t = &sc->sc_transfers;
   1506 	DLOG(D_SOFT, "callback_entry flags %#jx", t->flags, 0,0,0);
   1507 
   1508 repeat:
   1509 	slhci_callback(sc);
   1510 
   1511 	if (!gcq_empty(&sc->sc_waitq)) {
   1512 		slhci_enter_xfers(sc);
   1513 		slhci_dotransfer(sc);
   1514 		slhci_waitintr(sc, 0);
   1515 		goto repeat;
   1516 	}
   1517 
   1518 	t->flags &= ~F_CALLBACK;
   1519 	mutex_exit(&sc->sc_intr_lock);
   1520 }
   1521 
   1522 void
   1523 slhci_do_callback(struct slhci_softc *sc, struct usbd_xfer *xfer)
   1524 {
   1525 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1526 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   1527 
   1528 	start_cc_time(&t_callback, (u_int)xfer);
   1529 	mutex_exit(&sc->sc_intr_lock);
   1530 
   1531 	mutex_enter(&sc->sc_lock);
   1532 	usb_transfer_complete(xfer);
   1533 	mutex_exit(&sc->sc_lock);
   1534 
   1535 	mutex_enter(&sc->sc_intr_lock);
   1536 	stop_cc_time(&t_callback);
   1537 }
   1538 
   1539 int
   1540 slhci_intr(void *arg)
   1541 {
   1542 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1543 	struct slhci_softc *sc = arg;
   1544 	int ret = 0;
   1545 	int irq;
   1546 
   1547 	start_cc_time(&t_hard_int, (unsigned int)arg);
   1548 	mutex_enter(&sc->sc_intr_lock);
   1549 
   1550 	do {
   1551 		irq = slhci_dointr(sc);
   1552 		ret |= irq;
   1553 		slhci_main(sc);
   1554 	} while (irq);
   1555 	mutex_exit(&sc->sc_intr_lock);
   1556 
   1557 	stop_cc_time(&t_hard_int);
   1558 	return ret;
   1559 }
   1560 
   1561 /* called with interrupt lock only held. */
   1562 void
   1563 slhci_main(struct slhci_softc *sc)
   1564 {
   1565 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1566 	struct slhci_transfers *t;
   1567 
   1568 	t = &sc->sc_transfers;
   1569 
   1570 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   1571 
   1572 waitcheck:
   1573 	slhci_waitintr(sc, slhci_wait_time);
   1574 
   1575 	/*
   1576 	 * The direct call is needed in the ub_usepolling and disabled cases
   1577 	 * since the soft interrupt is not available.  In the disabled case,
   1578 	 * this code can be reached from the usb detach, after the reaping of
   1579 	 * the soft interrupt.  That test could be !F_ACTIVE, but there is no
   1580 	 * reason not to make the callbacks directly in the other DISABLED
   1581 	 * cases.
   1582 	 */
   1583 	if ((t->flags & F_ROOTINTR) || !gcq_empty(&t->q[Q_CALLBACKS])) {
   1584 		if (__predict_false(sc->sc_bus.ub_usepolling ||
   1585 		    t->flags & F_DISABLED))
   1586 			slhci_callback(sc);
   1587 		else
   1588 			slhci_callback_schedule(sc);
   1589 	}
   1590 
   1591 	if (!gcq_empty(&sc->sc_waitq)) {
   1592 		slhci_enter_xfers(sc);
   1593 		slhci_dotransfer(sc);
   1594 		goto waitcheck;
   1595 	}
   1596 	DLOG(D_INTR, "... done", 0, 0, 0, 0);
   1597 }
   1598 
   1599 /* End lock entry functions. Start in lock function. */
   1600 
   1601 /* Register read/write routines and barriers. */
   1602 #ifdef SLHCI_BUS_SPACE_BARRIERS
   1603 #define BSB(a, b, c, d, e) bus_space_barrier(a, b, c, d, BUS_SPACE_BARRIER_ # e)
   1604 #define BSB_SYNC(a, b, c, d) bus_space_barrier(a, b, c, d, BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
   1605 #else /* now !SLHCI_BUS_SPACE_BARRIERS */
   1606 #define BSB(a, b, c, d, e) __USE(d)
   1607 #define BSB_SYNC(a, b, c, d)
   1608 #endif /* SLHCI_BUS_SPACE_BARRIERS */
   1609 
   1610 static void
   1611 slhci_write(struct slhci_softc *sc, uint8_t addr, uint8_t data)
   1612 {
   1613 	bus_size_t paddr, pdata, pst, psz;
   1614 	bus_space_tag_t iot;
   1615 	bus_space_handle_t ioh;
   1616 
   1617 	paddr = pst = 0;
   1618 	pdata = sc->sc_stride;
   1619 	psz = pdata * 2;
   1620 	iot = sc->sc_iot;
   1621 	ioh = sc->sc_ioh;
   1622 
   1623 	bus_space_write_1(iot, ioh, paddr, addr);
   1624 	BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
   1625 	bus_space_write_1(iot, ioh, pdata, data);
   1626 	BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
   1627 }
   1628 
   1629 static uint8_t
   1630 slhci_read(struct slhci_softc *sc, uint8_t addr)
   1631 {
   1632 	bus_size_t paddr, pdata, pst, psz;
   1633 	bus_space_tag_t iot;
   1634 	bus_space_handle_t ioh;
   1635 	uint8_t data;
   1636 
   1637 	paddr = pst = 0;
   1638 	pdata = sc->sc_stride;
   1639 	psz = pdata * 2;
   1640 	iot = sc->sc_iot;
   1641 	ioh = sc->sc_ioh;
   1642 
   1643 	bus_space_write_1(iot, ioh, paddr, addr);
   1644 	BSB(iot, ioh, pst, psz, WRITE_BEFORE_READ);
   1645 	data = bus_space_read_1(iot, ioh, pdata);
   1646 	BSB(iot, ioh, pst, psz, READ_BEFORE_WRITE);
   1647 	return data;
   1648 }
   1649 
   1650 #if 0 /* auto-increment mode broken, see errata doc */
   1651 static void
   1652 slhci_write_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
   1653 {
   1654 	bus_size_t paddr, pdata, pst, psz;
   1655 	bus_space_tag_t iot;
   1656 	bus_space_handle_t ioh;
   1657 
   1658 	paddr = pst = 0;
   1659 	pdata = sc->sc_stride;
   1660 	psz = pdata * 2;
   1661 	iot = sc->sc_iot;
   1662 	ioh = sc->sc_ioh;
   1663 
   1664 	bus_space_write_1(iot, ioh, paddr, addr);
   1665 	BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
   1666 	bus_space_write_multi_1(iot, ioh, pdata, buf, l);
   1667 	BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
   1668 }
   1669 
   1670 static void
   1671 slhci_read_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
   1672 {
   1673 	bus_size_t paddr, pdata, pst, psz;
   1674 	bus_space_tag_t iot;
   1675 	bus_space_handle_t ioh;
   1676 
   1677 	paddr = pst = 0;
   1678 	pdata = sc->sc_stride;
   1679 	psz = pdata * 2;
   1680 	iot = sc->sc_iot;
   1681 	ioh = sc->sc_ioh;
   1682 
   1683 	bus_space_write_1(iot, ioh, paddr, addr);
   1684 	BSB(iot, ioh, pst, psz, WRITE_BEFORE_READ);
   1685 	bus_space_read_multi_1(iot, ioh, pdata, buf, l);
   1686 	BSB(iot, ioh, pst, psz, READ_BEFORE_WRITE);
   1687 }
   1688 #else
   1689 static void
   1690 slhci_write_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
   1691 {
   1692 #if 1
   1693 	for (; l; addr++, buf++, l--)
   1694 		slhci_write(sc, addr, *buf);
   1695 #else
   1696 	bus_size_t paddr, pdata, pst, psz;
   1697 	bus_space_tag_t iot;
   1698 	bus_space_handle_t ioh;
   1699 
   1700 	paddr = pst = 0;
   1701 	pdata = sc->sc_stride;
   1702 	psz = pdata * 2;
   1703 	iot = sc->sc_iot;
   1704 	ioh = sc->sc_ioh;
   1705 
   1706 	for (; l; addr++, buf++, l--) {
   1707 		bus_space_write_1(iot, ioh, paddr, addr);
   1708 		BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
   1709 		bus_space_write_1(iot, ioh, pdata, *buf);
   1710 		BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
   1711 	}
   1712 #endif
   1713 }
   1714 
   1715 static void
   1716 slhci_read_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
   1717 {
   1718 #if 1
   1719 	for (; l; addr++, buf++, l--)
   1720 		*buf = slhci_read(sc, addr);
   1721 #else
   1722 	bus_size_t paddr, pdata, pst, psz;
   1723 	bus_space_tag_t iot;
   1724 	bus_space_handle_t ioh;
   1725 
   1726 	paddr = pst = 0;
   1727 	pdata = sc->sc_stride;
   1728 	psz = pdata * 2;
   1729 	iot = sc->sc_iot;
   1730 	ioh = sc->sc_ioh;
   1731 
   1732 	for (; l; addr++, buf++, l--) {
   1733 		bus_space_write_1(iot, ioh, paddr, addr);
   1734 		BSB(iot, ioh, pst, psz, WRITE_BEFORE_READ);
   1735 		*buf = bus_space_read_1(iot, ioh, pdata);
   1736 		BSB(iot, ioh, pst, psz, READ_BEFORE_WRITE);
   1737 	}
   1738 #endif
   1739 }
   1740 #endif
   1741 
   1742 /*
   1743  * After calling waitintr it is necessary to either call slhci_callback or
   1744  * schedule the callback if necessary.  The callback cannot be called directly
   1745  * from the hard interrupt since it interrupts at a high IPL and callbacks
   1746  * can do copyout and such.
   1747  */
   1748 static void
   1749 slhci_waitintr(struct slhci_softc *sc, int wait_time)
   1750 {
   1751 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1752 	struct slhci_transfers *t;
   1753 
   1754 	t = &sc->sc_transfers;
   1755 
   1756 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   1757 
   1758 	if (__predict_false(sc->sc_bus.ub_usepolling))
   1759 		wait_time = 12000;
   1760 
   1761 	while (t->pend <= wait_time) {
   1762 		DLOG(D_WAIT, "waiting... frame %jd pend %jd flags %#jx",
   1763 		    t->frame, t->pend, t->flags, 0);
   1764 		LK_SLASSERT(t->flags & F_ACTIVE, sc, NULL, NULL, return);
   1765 		LK_SLASSERT(t->flags & (F_AINPROG|F_BINPROG), sc, NULL, NULL,
   1766 		    return);
   1767 		slhci_dointr(sc);
   1768 	}
   1769 	DLOG(D_WAIT, "... done", 0, 0, 0, 0);
   1770 }
   1771 
   1772 static int
   1773 slhci_dointr(struct slhci_softc *sc)
   1774 {
   1775 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1776 	struct slhci_transfers *t;
   1777 	struct slhci_pipe *tosp;
   1778 	uint8_t r;
   1779 
   1780 	t = &sc->sc_transfers;
   1781 
   1782 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   1783 
   1784 	if (sc->sc_ier == 0) {
   1785 		DLOG(D_INTR, "sc_ier is zero", 0, 0, 0, 0);
   1786 		return 0;
   1787 	}
   1788 
   1789 	r = slhci_read(sc, SL11_ISR);
   1790 
   1791 #ifdef SLHCI_DEBUG
   1792 	if (slhcidebug & SLHCI_D_INTR && r & sc->sc_ier &&
   1793 	    ((r & ~(SL11_ISR_SOF|SL11_ISR_DATA)) || slhcidebug & SLHCI_D_SOF)) {
   1794 		uint8_t e, f;
   1795 
   1796 		e = slhci_read(sc, SL11_IER);
   1797 		f = slhci_read(sc, SL11_CTRL);
   1798 		DDOLOG("Flags=%#x IER=%#x ISR=%#x CTRL=%#x", t->flags, e, r, f);
   1799 		DDOLOGCTRL(f);
   1800 		DDOLOGISR(r);
   1801 	}
   1802 #endif
   1803 
   1804 	/*
   1805 	 * check IER for corruption occasionally.  Assume that the above
   1806 	 * sc_ier == 0 case works correctly.
   1807 	 */
   1808 	if (__predict_false(sc->sc_ier_check++ > SLHCI_IER_CHECK_FREQUENCY)) {
   1809 		sc->sc_ier_check = 0;
   1810 		if (sc->sc_ier != slhci_read(sc, SL11_IER)) {
   1811 			printf("%s: IER value corrupted! halted\n",
   1812 			    SC_NAME(sc));
   1813 			DDOLOG("IER value corrupted! halted", 0, 0, 0, 0);
   1814 			slhci_halt(sc, NULL, NULL);
   1815 			return 1;
   1816 		}
   1817 	}
   1818 
   1819 	r &= sc->sc_ier;
   1820 
   1821 	if (r == 0) {
   1822 		DLOG(D_INTR, "r is zero", 0, 0, 0, 0);
   1823 		return 0;
   1824 	}
   1825 
   1826 	sc->sc_ier_check = 0;
   1827 
   1828 	slhci_write(sc, SL11_ISR, r);
   1829 	BSB_SYNC(sc->iot, sc->ioh, sc->pst, sc->psz);
   1830 
   1831 	/* If we have an insertion event we do not care about anything else. */
   1832 	if (__predict_false(r & SL11_ISR_INSERT)) {
   1833 		slhci_insert(sc);
   1834 		DLOG(D_INTR, "... done", 0, 0, 0, 0);
   1835 		return 1;
   1836 	}
   1837 
   1838 	stop_cc_time(&t_intr);
   1839 	start_cc_time(&t_intr, r);
   1840 
   1841 	if (r & SL11_ISR_SOF) {
   1842 		t->frame++;
   1843 
   1844 		gcq_merge_tail(&t->q[Q_CB], &t->q[Q_NEXT_CB]);
   1845 
   1846 		/*
   1847 		 * SOFCHECK flags are cleared in tstart.  Two flags are needed
   1848 		 * since the first SOF interrupt processed after the transfer
   1849 		 * is started might have been generated before the transfer
   1850 		 * was started.
   1851 		 */
   1852 		if (__predict_false(t->flags & F_SOFCHECK2 && t->flags &
   1853 		    (F_AINPROG|F_BINPROG))) {
   1854 			printf("%s: Missed transfer completion. halted\n",
   1855 			    SC_NAME(sc));
   1856 			DDOLOG("Missed transfer completion. halted", 0, 0, 0,
   1857 			    0);
   1858 			slhci_halt(sc, NULL, NULL);
   1859 			return 1;
   1860 		} else if (t->flags & F_SOFCHECK1) {
   1861 			t->flags |= F_SOFCHECK2;
   1862 		} else
   1863 			t->flags |= F_SOFCHECK1;
   1864 
   1865 		if (t->flags & F_CHANGE)
   1866 			t->flags |= F_ROOTINTR;
   1867 
   1868 		while (__predict_true(GOT_FIRST_TO(tosp, t)) &&
   1869 		    __predict_false(tosp->to_frame <= t->frame)) {
   1870 			tosp->xfer->ux_status = USBD_TIMEOUT;
   1871 			slhci_do_abort(sc, tosp, tosp->xfer);
   1872 			enter_callback(t, tosp);
   1873 		}
   1874 
   1875 		/*
   1876 		 * Start any waiting transfers right away.  If none, we will
   1877 		 * start any new transfers later.
   1878 		 */
   1879 		slhci_tstart(sc);
   1880 	}
   1881 
   1882 	if (r & (SL11_ISR_USBA|SL11_ISR_USBB)) {
   1883 		int ab;
   1884 
   1885 		if ((r & (SL11_ISR_USBA|SL11_ISR_USBB)) ==
   1886 		    (SL11_ISR_USBA|SL11_ISR_USBB)) {
   1887 			if (!(t->flags & (F_AINPROG|F_BINPROG)))
   1888 				return 1; /* presume card pulled */
   1889 
   1890 			LK_SLASSERT((t->flags & (F_AINPROG|F_BINPROG)) !=
   1891 			    (F_AINPROG|F_BINPROG), sc, NULL, NULL, return 1);
   1892 
   1893 			/*
   1894 			 * This should never happen (unless card removal just
   1895 			 * occurred) but appeared frequently when both
   1896 			 * transfers were started at the same time and was
   1897 			 * accompanied by data corruption.  It still happens
   1898 			 * at times.  I have not seen data correption except
   1899 			 * when the STATUS bit gets set, which now causes the
   1900 			 * driver to halt, however this should still not
   1901 			 * happen so the warning is kept.  See comment in
   1902 			 * abdone, below.
   1903 			 */
   1904 			printf("%s: Transfer reported done but not started! "
   1905 			    "Verify data integrity if not detaching. "
   1906 			    " flags %#x r %x\n", SC_NAME(sc), t->flags, r);
   1907 
   1908 			if (!(t->flags & F_AINPROG))
   1909 				r &= ~SL11_ISR_USBA;
   1910 			else
   1911 				r &= ~SL11_ISR_USBB;
   1912 		}
   1913 		t->pend = INT_MAX;
   1914 
   1915 		if (r & SL11_ISR_USBA)
   1916 			ab = A;
   1917 		else
   1918 			ab = B;
   1919 
   1920 		/*
   1921 		 * This happens when a low speed device is attached to
   1922 		 * a hub with chip rev 1.5.  SOF stops, but a few transfers
   1923 		 * still work before causing this error.
   1924 		 */
   1925 		if (!(t->flags & (ab ? F_BINPROG : F_AINPROG))) {
   1926 			printf("%s: %s done but not in progress! halted\n",
   1927 			    SC_NAME(sc), ab ? "B" : "A");
   1928 			DDOLOG("AB=%d done but not in progress! halted", ab,
   1929 			    0, 0, 0);
   1930 			slhci_halt(sc, NULL, NULL);
   1931 			return 1;
   1932 		}
   1933 
   1934 		t->flags &= ~(ab ? F_BINPROG : F_AINPROG);
   1935 		slhci_tstart(sc);
   1936 		stop_cc_time(&t_ab[ab]);
   1937 		start_cc_time(&t_abdone, t->flags);
   1938 		slhci_abdone(sc, ab);
   1939 		stop_cc_time(&t_abdone);
   1940 	}
   1941 
   1942 	slhci_dotransfer(sc);
   1943 
   1944 	DLOG(D_INTR, "... done", 0, 0, 0, 0);
   1945 
   1946 	return 1;
   1947 }
   1948 
   1949 static void
   1950 slhci_abdone(struct slhci_softc *sc, int ab)
   1951 {
   1952 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   1953 	struct slhci_transfers *t;
   1954 	struct slhci_pipe *spipe;
   1955 	struct usbd_xfer *xfer;
   1956 	uint8_t status, buf_start;
   1957 	uint8_t *target_buf;
   1958 	unsigned int actlen;
   1959 	int head;
   1960 
   1961 	t = &sc->sc_transfers;
   1962 
   1963 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   1964 
   1965 	DLOG(D_TRACE, "ABDONE flags %#jx", t->flags, 0,0,0);
   1966 
   1967 	DLOG(D_MSG, "DONE AB=%jd spipe %#jx len %jd xfer %#jx", ab,
   1968 	    t->spipe[ab], (uintptr_t)t->len[ab],
   1969 	    (uintptr_t)(t->spipe[ab] ? t->spipe[ab]->xfer : NULL));
   1970 
   1971 	spipe = t->spipe[ab];
   1972 
   1973 	/*
   1974 	 * skip this one if aborted; do not call return from the rest of the
   1975 	 * function unless halting, else t->len will not be cleared.
   1976 	 */
   1977 	if (spipe == NULL)
   1978 		goto done;
   1979 
   1980 	t->spipe[ab] = NULL;
   1981 
   1982 	xfer = spipe->xfer;
   1983 
   1984 	gcq_remove(&spipe->to);
   1985 
   1986 	LK_SLASSERT(xfer != NULL, sc, spipe, NULL, return);
   1987 
   1988 	status = slhci_read(sc, slhci_tregs[ab][STAT]);
   1989 
   1990 	/*
   1991 	 * I saw no status or remaining length greater than the requested
   1992 	 * length in early driver versions in circumstances I assumed caused
   1993 	 * excess power draw.  I am no longer able to reproduce this when
   1994 	 * causing excess power draw circumstances.
   1995 	 *
   1996 	 * Disabling a power check and attaching aue to a keyboard and hub
   1997 	 * that is directly attached (to CFU1U, 100mA max, aue 160mA, keyboard
   1998 	 * 98mA) sometimes works and sometimes fails to configure.  After
   1999 	 * removing the aue and attaching a self-powered umass dvd reader
   2000 	 * (unknown if it draws power from the host also) soon a single Error
   2001 	 * status occurs then only timeouts. The controller soon halts freeing
   2002 	 * memory due to being ONQU instead of BUSY.  This may be the same
   2003 	 * basic sequence that caused the no status/bad length errors.  The
   2004 	 * umass device seems to work (better at least) with the keyboard hub
   2005 	 * when not first attaching aue (tested once reading an approximately
   2006 	 * 200MB file).
   2007 	 *
   2008 	 * Overflow can indicate that the device and host disagree about how
   2009 	 * much data has been transferred.  This may indicate a problem at any
   2010 	 * point during the transfer, not just when the error occurs.  It may
   2011 	 * indicate data corruption.  A warning message is printed.
   2012 	 *
   2013 	 * Trying to use both A and B transfers at the same time results in
   2014 	 * incorrect transfer completion ISR reports and the status will then
   2015 	 * include SL11_EPSTAT_SETUP, which is apparently set while the
   2016 	 * transfer is in progress.  I also noticed data corruption, even
   2017 	 * after waiting for the transfer to complete. The driver now avoids
   2018 	 * trying to start both at the same time.
   2019 	 *
   2020 	 * I had accidently initialized the B registers before they were valid
   2021 	 * in some driver versions.  Since every other performance enhancing
   2022 	 * feature has been confirmed buggy in the errata doc, I have not
   2023 	 * tried both transfers at once again with the documented
   2024 	 * initialization order.
   2025 	 *
   2026 	 * However, I have seen this problem again ("done but not started"
   2027 	 * errors), which in some cases cases the SETUP status bit to remain
   2028 	 * set on future transfers.  In other cases, the SETUP bit is not set
   2029 	 * and no data corruption occurs.  This occured while using both umass
   2030 	 * and aue on a powered hub (maybe triggered by some local activity
   2031 	 * also) and needs several reads of the 200MB file to trigger.  The
   2032 	 * driver now halts if SETUP is detected.
   2033  	 */
   2034 
   2035 	actlen = 0;
   2036 
   2037 	if (__predict_false(!status)) {
   2038 		DDOLOG("no status! xfer %p spipe %p", xfer, spipe, 0,0);
   2039 		printf("%s: no status! halted\n", SC_NAME(sc));
   2040 		slhci_halt(sc, spipe, xfer);
   2041 		return;
   2042 	}
   2043 
   2044 #ifdef SLHCI_DEBUG
   2045 	if ((slhcidebug & SLHCI_D_NAK) ||
   2046 	    (status & SL11_EPSTAT_ERRBITS) != SL11_EPSTAT_NAK) {
   2047 	    	DDOLOG("USB Status = %#.2x", status, 0, 0, 0);
   2048 		DDOLOGSTATUS(status);
   2049 	}
   2050 #endif
   2051 
   2052 	if (!(status & SL11_EPSTAT_ERRBITS)) {
   2053 		unsigned int cont = slhci_read(sc, slhci_tregs[ab][CONT]);
   2054 		unsigned int len = spipe->tregs[LEN];
   2055 		DLOG(D_XFER, "cont %jd len %jd", cont, len, 0, 0);
   2056 		if ((status & SL11_EPSTAT_OVERFLOW) || cont > len) {
   2057 			DDOLOG("overflow - cont %d len %d xfer->ux_length %d "
   2058 			    "xfer->actlen %d", cont, len, xfer->ux_length,
   2059 			    xfer->ux_actlen);
   2060 			printf("%s: overflow cont %d len %d xfer->ux_length"
   2061 			    " %d xfer->ux_actlen %d\n", SC_NAME(sc), cont,
   2062 			    len, xfer->ux_length, xfer->ux_actlen);
   2063 			actlen = len;
   2064 		} else {
   2065 			actlen = len - cont;
   2066 		}
   2067 		spipe->nerrs = 0;
   2068 	}
   2069 
   2070 	/* Actual copyin done after starting next transfer. */
   2071 	if (actlen && (spipe->tregs[PID] & SL11_PID_BITS) == SL11_PID_IN) {
   2072 		target_buf = spipe->buffer;
   2073 		buf_start = spipe->tregs[ADR];
   2074 	} else {
   2075 		target_buf = NULL;
   2076 		buf_start = 0; /* XXX gcc uninitialized warnings */
   2077 	}
   2078 
   2079 	if (status & SL11_EPSTAT_ERRBITS) {
   2080 		status &= SL11_EPSTAT_ERRBITS;
   2081 		if (status & SL11_EPSTAT_SETUP) {
   2082 			printf("%s: Invalid controller state detected! "
   2083 			    "halted\n", SC_NAME(sc));
   2084 			DDOLOG("Invalid controller state detected! "
   2085 			    "halted", 0, 0, 0, 0);
   2086 			slhci_halt(sc, spipe, xfer);
   2087 			return;
   2088 		} else if (__predict_false(sc->sc_bus.ub_usepolling)) {
   2089 			head = Q_CALLBACKS;
   2090 			if (status & SL11_EPSTAT_STALL)
   2091 				xfer->ux_status = USBD_STALLED;
   2092 			else if (status & SL11_EPSTAT_TIMEOUT)
   2093 				xfer->ux_status = USBD_TIMEOUT;
   2094 			else if (status & SL11_EPSTAT_NAK)
   2095 				head = Q_NEXT_CB;
   2096 			else
   2097 				xfer->ux_status = USBD_IOERROR;
   2098 		} else if (status & SL11_EPSTAT_NAK) {
   2099 			int i = spipe->pipe.up_interval;
   2100 			if (i == 0)
   2101 				i = 1;
   2102 			DDOLOG("xfer %p spipe %p NAK delay by %d", xfer, spipe,
   2103 			    i, 0);
   2104 			spipe->lastframe = spipe->frame = t->frame + i;
   2105 			slhci_queue_timed(sc, spipe);
   2106 			goto queued;
   2107 		} else if (++spipe->nerrs > SLHCI_MAX_RETRIES ||
   2108 		    (status & SL11_EPSTAT_STALL)) {
   2109 			DDOLOG("xfer %p spipe %p nerrs %d", xfer, spipe,
   2110 			    spipe->nerrs, 0);
   2111 			if (status & SL11_EPSTAT_STALL)
   2112 				xfer->ux_status = USBD_STALLED;
   2113 			else if (status & SL11_EPSTAT_TIMEOUT)
   2114 				xfer->ux_status = USBD_TIMEOUT;
   2115 			else
   2116 				xfer->ux_status = USBD_IOERROR;
   2117 
   2118 			DLOG(D_ERR, "Max retries reached! status %#jx "
   2119 			    "xfer->ux_status %jd", status, xfer->ux_status, 0,
   2120 			    0);
   2121 			DDOLOGSTATUS(status);
   2122 
   2123 			head = Q_CALLBACKS;
   2124 		} else {
   2125 			head = Q_NEXT_CB;
   2126 		}
   2127 	} else if (spipe->ptype == PT_CTRL_SETUP) {
   2128 		spipe->tregs[PID] = spipe->newpid;
   2129 
   2130 		if (xfer->ux_length) {
   2131 			LK_SLASSERT(spipe->newlen[1] != 0, sc, spipe, xfer,
   2132 			    return);
   2133 			spipe->tregs[LEN] = spipe->newlen[1];
   2134 			spipe->bustime = spipe->newbustime[1];
   2135 			spipe->buffer = xfer->ux_buf;
   2136 			spipe->ptype = PT_CTRL_DATA;
   2137 		} else {
   2138 status_setup:
   2139 			/* CTRL_DATA swaps direction in PID then jumps here */
   2140 			spipe->tregs[LEN] = 0;
   2141 			if (spipe->pflags & PF_LS)
   2142 				spipe->bustime = SLHCI_LS_CONST;
   2143 			else
   2144 				spipe->bustime = SLHCI_FS_CONST;
   2145 			spipe->ptype = PT_CTRL_STATUS;
   2146 			spipe->buffer = NULL;
   2147 		}
   2148 
   2149 		/* Status or first data packet must be DATA1. */
   2150 		spipe->control |= SL11_EPCTRL_DATATOGGLE;
   2151 		if ((spipe->tregs[PID] & SL11_PID_BITS) == SL11_PID_IN)
   2152 			spipe->control &= ~SL11_EPCTRL_DIRECTION;
   2153 		else
   2154 			spipe->control |= SL11_EPCTRL_DIRECTION;
   2155 
   2156 		head = Q_CB;
   2157 	} else if (spipe->ptype == PT_CTRL_STATUS) {
   2158 		head = Q_CALLBACKS;
   2159 	} else { /* bulk, intr, control data */
   2160 		xfer->ux_actlen += actlen;
   2161 		spipe->control ^= SL11_EPCTRL_DATATOGGLE;
   2162 
   2163 		if (actlen == spipe->tregs[LEN] &&
   2164 		    (xfer->ux_length > xfer->ux_actlen || spipe->wantshort)) {
   2165 			spipe->buffer += actlen;
   2166 			LK_SLASSERT(xfer->ux_length >= xfer->ux_actlen, sc,
   2167 			    spipe, xfer, return);
   2168 			if (xfer->ux_length - xfer->ux_actlen < actlen) {
   2169 				spipe->wantshort = 0;
   2170 				spipe->tregs[LEN] = spipe->newlen[0];
   2171 				spipe->bustime = spipe->newbustime[0];
   2172 				LK_SLASSERT(xfer->ux_actlen +
   2173 				    spipe->tregs[LEN] == xfer->ux_length, sc,
   2174 				    spipe, xfer, return);
   2175 			}
   2176 			head = Q_CB;
   2177 		} else if (spipe->ptype == PT_CTRL_DATA) {
   2178 			spipe->tregs[PID] ^= SLHCI_PID_SWAP_IN_OUT;
   2179 			goto status_setup;
   2180 		} else {
   2181 			if (spipe->ptype == PT_INTR) {
   2182 				spipe->lastframe +=
   2183 				    spipe->pipe.up_interval;
   2184 				/*
   2185 				 * If ack, we try to keep the
   2186 				 * interrupt rate by using lastframe
   2187 				 * instead of the current frame.
   2188 				 */
   2189 				spipe->frame = spipe->lastframe +
   2190 				    spipe->pipe.up_interval;
   2191 			}
   2192 
   2193 			/*
   2194 			 * Set the toggle for the next transfer.  It
   2195 			 * has already been toggled above, so the
   2196 			 * current setting will apply to the next
   2197 			 * transfer.
   2198 			 */
   2199 			if (spipe->control & SL11_EPCTRL_DATATOGGLE)
   2200 				spipe->pflags |= PF_TOGGLE;
   2201 			else
   2202 				spipe->pflags &= ~PF_TOGGLE;
   2203 
   2204 			head = Q_CALLBACKS;
   2205 		}
   2206 	}
   2207 
   2208 	if (head == Q_CALLBACKS) {
   2209 		gcq_remove(&spipe->to);
   2210 
   2211 	 	if (xfer->ux_status == USBD_IN_PROGRESS) {
   2212 			LK_SLASSERT(xfer->ux_actlen <= xfer->ux_length, sc,
   2213 			    spipe, xfer, return);
   2214 			xfer->ux_status = USBD_NORMAL_COMPLETION;
   2215 		}
   2216 	}
   2217 
   2218 	enter_q(t, spipe, head);
   2219 
   2220 queued:
   2221 	if (target_buf != NULL) {
   2222 		slhci_dotransfer(sc);
   2223 		start_cc_time(&t_copy_from_dev, actlen);
   2224 		slhci_read_multi(sc, buf_start, target_buf, actlen);
   2225 		stop_cc_time(&t_copy_from_dev);
   2226 		DLOGBUF(D_BUF, target_buf, actlen);
   2227 		t->pend -= SLHCI_FS_CONST + SLHCI_FS_DATA_TIME(actlen);
   2228 	}
   2229 
   2230 done:
   2231 	t->len[ab] = -1;
   2232 }
   2233 
   2234 static void
   2235 slhci_tstart(struct slhci_softc *sc)
   2236 {
   2237 	struct slhci_transfers *t;
   2238 	struct slhci_pipe *spipe;
   2239 	int remaining_bustime;
   2240 
   2241 	t = &sc->sc_transfers;
   2242 
   2243 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2244 
   2245 	if (!(t->flags & (F_AREADY|F_BREADY)))
   2246 		return;
   2247 
   2248 	if (t->flags & (F_AINPROG|F_BINPROG|F_DISABLED))
   2249 		return;
   2250 
   2251 	/*
   2252 	 * We have about 6 us to get from the bus time check to
   2253 	 * starting the transfer or we might babble or the chip might fail to
   2254 	 * signal transfer complete.  This leaves no time for any other
   2255 	 * interrupts.
   2256 	 */
   2257 	remaining_bustime = (int)(slhci_read(sc, SL811_CSOF)) << 6;
   2258 	remaining_bustime -= SLHCI_END_BUSTIME;
   2259 
   2260 	/*
   2261 	 * Start one transfer only, clearing any aborted transfers that are
   2262 	 * not yet in progress and skipping missed isoc. It is easier to copy
   2263 	 * & paste most of the A/B sections than to make the logic work
   2264 	 * otherwise and this allows better constant use.
   2265 	 */
   2266 	if (t->flags & F_AREADY) {
   2267 		spipe = t->spipe[A];
   2268 		if (spipe == NULL) {
   2269 			t->flags &= ~F_AREADY;
   2270 			t->len[A] = -1;
   2271 		} else if (remaining_bustime >= spipe->bustime) {
   2272 			t->flags &= ~(F_AREADY|F_SOFCHECK1|F_SOFCHECK2);
   2273 			t->flags |= F_AINPROG;
   2274 			start_cc_time(&t_ab[A], spipe->tregs[LEN]);
   2275 			slhci_write(sc, SL11_E0CTRL, spipe->control);
   2276 			goto pend;
   2277 		}
   2278 	}
   2279 	if (t->flags & F_BREADY) {
   2280 		spipe = t->spipe[B];
   2281 		if (spipe == NULL) {
   2282 			t->flags &= ~F_BREADY;
   2283 			t->len[B] = -1;
   2284 		} else if (remaining_bustime >= spipe->bustime) {
   2285 			t->flags &= ~(F_BREADY|F_SOFCHECK1|F_SOFCHECK2);
   2286 			t->flags |= F_BINPROG;
   2287 			start_cc_time(&t_ab[B], spipe->tregs[LEN]);
   2288 			slhci_write(sc, SL11_E1CTRL, spipe->control);
   2289 pend:
   2290 			t->pend = spipe->bustime;
   2291 		}
   2292 	}
   2293 }
   2294 
   2295 static void
   2296 slhci_dotransfer(struct slhci_softc *sc)
   2297 {
   2298 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2299 	struct slhci_transfers *t;
   2300 	struct slhci_pipe *spipe;
   2301 	int ab, i;
   2302 
   2303 	t = &sc->sc_transfers;
   2304 
   2305 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2306 
   2307  	while ((t->len[A] == -1 || t->len[B] == -1) &&
   2308 	    (GOT_FIRST_TIMED_COND(spipe, t, spipe->frame <= t->frame) ||
   2309 	    GOT_FIRST_CB(spipe, t))) {
   2310 		LK_SLASSERT(spipe->xfer != NULL, sc, spipe, NULL, return);
   2311 		LK_SLASSERT(spipe->ptype != PT_ROOT_CTRL && spipe->ptype !=
   2312 		    PT_ROOT_INTR, sc, spipe, NULL, return);
   2313 
   2314 		/* Check that this transfer can fit in the remaining memory. */
   2315 		if (t->len[A] + t->len[B] + spipe->tregs[LEN] + 1 >
   2316 		    SL11_MAX_PACKET_SIZE) {
   2317 			DLOG(D_XFER, "Transfer does not fit. alen %jd blen %jd "
   2318 			    "len %jd", t->len[A], t->len[B], spipe->tregs[LEN],
   2319 			    0);
   2320 			return;
   2321 		}
   2322 
   2323 		gcq_remove(&spipe->xq);
   2324 
   2325 		if (t->len[A] == -1) {
   2326 			ab = A;
   2327 			spipe->tregs[ADR] = SL11_BUFFER_START;
   2328 		} else {
   2329 			ab = B;
   2330 			spipe->tregs[ADR] = SL11_BUFFER_END -
   2331 			    spipe->tregs[LEN];
   2332 		}
   2333 
   2334 		t->len[ab] = spipe->tregs[LEN];
   2335 
   2336 		if (spipe->tregs[LEN] && (spipe->tregs[PID] & SL11_PID_BITS)
   2337 		    != SL11_PID_IN) {
   2338 			start_cc_time(&t_copy_to_dev,
   2339 			    spipe->tregs[LEN]);
   2340 			slhci_write_multi(sc, spipe->tregs[ADR],
   2341 			    spipe->buffer, spipe->tregs[LEN]);
   2342 			stop_cc_time(&t_copy_to_dev);
   2343 			t->pend -= SLHCI_FS_CONST +
   2344 			    SLHCI_FS_DATA_TIME(spipe->tregs[LEN]);
   2345 		}
   2346 
   2347 		DLOG(D_MSG, "NEW TRANSFER AB=%jd flags %#jx alen %jd blen %jd",
   2348 		    ab, t->flags, t->len[0], t->len[1]);
   2349 
   2350 		if (spipe->tregs[LEN])
   2351 			i = 0;
   2352 		else
   2353 			i = 1;
   2354 
   2355 		for (; i <= 3; i++)
   2356 			if (t->current_tregs[ab][i] != spipe->tregs[i]) {
   2357 				t->current_tregs[ab][i] = spipe->tregs[i];
   2358 				slhci_write(sc, slhci_tregs[ab][i],
   2359 				    spipe->tregs[i]);
   2360 			}
   2361 
   2362 		DLOG(D_SXFER, "Transfer len %jd pid %#jx dev %jd type %jd",
   2363 		    spipe->tregs[LEN], spipe->tregs[PID], spipe->tregs[DEV],
   2364 	    	    spipe->ptype);
   2365 
   2366 		t->spipe[ab] = spipe;
   2367 		t->flags |= ab ? F_BREADY : F_AREADY;
   2368 
   2369 		slhci_tstart(sc);
   2370 	}
   2371 }
   2372 
   2373 /*
   2374  * slhci_callback is called after the lock is taken.
   2375  */
   2376 static void
   2377 slhci_callback(struct slhci_softc *sc)
   2378 {
   2379 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2380 	struct slhci_transfers *t;
   2381 	struct slhci_pipe *spipe;
   2382 	struct usbd_xfer *xfer;
   2383 
   2384 	t = &sc->sc_transfers;
   2385 
   2386 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2387 
   2388 	DLOG(D_SOFT, "CB flags %#jx", t->flags, 0,0,0);
   2389 	for (;;) {
   2390 		if (__predict_false(t->flags & F_ROOTINTR)) {
   2391 			t->flags &= ~F_ROOTINTR;
   2392 			if (t->rootintr != NULL) {
   2393 				u_char *p;
   2394 
   2395 				KASSERT(t->rootintr->ux_status ==
   2396 				    USBD_IN_PROGRESS);
   2397 				p = t->rootintr->ux_buf;
   2398 				p[0] = 2;
   2399 				t->rootintr->ux_actlen = 1;
   2400 				t->rootintr->ux_status = USBD_NORMAL_COMPLETION;
   2401 				xfer = t->rootintr;
   2402 				goto do_callback;
   2403 			}
   2404 		}
   2405 
   2406 
   2407 		if (!DEQUEUED_CALLBACK(spipe, t))
   2408 			return;
   2409 
   2410 		xfer = spipe->xfer;
   2411 		LK_SLASSERT(xfer != NULL, sc, spipe, NULL, return);
   2412 		spipe->xfer = NULL;
   2413 		DLOG(D_XFER, "xfer callback length %jd actlen %jd spipe %#jx "
   2414 		    "type %jd", xfer->ux_length, (uintptr_t)xfer->ux_actlen,
   2415 		    (uintptr_t)spipe, spipe->ptype);
   2416 do_callback:
   2417 		slhci_do_callback(sc, xfer);
   2418 	}
   2419 }
   2420 
   2421 static void
   2422 slhci_enter_xfer(struct slhci_softc *sc, struct slhci_pipe *spipe)
   2423 {
   2424 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2425 	struct slhci_transfers *t;
   2426 
   2427 	t = &sc->sc_transfers;
   2428 
   2429 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2430 
   2431 	if (__predict_false(t->flags & F_DISABLED) ||
   2432 	    __predict_false(spipe->pflags & PF_GONE)) {
   2433 		DLOG(D_MSG, "slhci_enter_xfer: DISABLED or GONE", 0,0,0,0);
   2434 		spipe->xfer->ux_status = USBD_CANCELLED;
   2435 	}
   2436 
   2437 	if (spipe->xfer->ux_status == USBD_IN_PROGRESS) {
   2438 		if (spipe->xfer->ux_timeout) {
   2439 			spipe->to_frame = t->frame + spipe->xfer->ux_timeout;
   2440 			slhci_xfer_timer(sc, spipe);
   2441 		}
   2442 		if (spipe->pipe.up_interval)
   2443 			slhci_queue_timed(sc, spipe);
   2444 		else
   2445 			enter_q(t, spipe, Q_CB);
   2446 	} else
   2447 		enter_callback(t, spipe);
   2448 }
   2449 
   2450 static void
   2451 slhci_enter_xfers(struct slhci_softc *sc)
   2452 {
   2453 	struct slhci_pipe *spipe;
   2454 
   2455 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2456 
   2457 	while (DEQUEUED_WAITQ(spipe, sc))
   2458 		slhci_enter_xfer(sc, spipe);
   2459 }
   2460 
   2461 static void
   2462 slhci_queue_timed(struct slhci_softc *sc, struct slhci_pipe *spipe)
   2463 {
   2464 	struct slhci_transfers *t;
   2465 	struct gcq *q;
   2466 	struct slhci_pipe *spp;
   2467 
   2468 	t = &sc->sc_transfers;
   2469 
   2470 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2471 
   2472 	FIND_TIMED(q, t, spp, spp->frame > spipe->frame);
   2473 	gcq_insert_before(q, &spipe->xq);
   2474 }
   2475 
   2476 static void
   2477 slhci_xfer_timer(struct slhci_softc *sc, struct slhci_pipe *spipe)
   2478 {
   2479 	struct slhci_transfers *t;
   2480 	struct gcq *q;
   2481 	struct slhci_pipe *spp;
   2482 
   2483 	t = &sc->sc_transfers;
   2484 
   2485 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2486 
   2487 	FIND_TO(q, t, spp, spp->to_frame >= spipe->to_frame);
   2488 	gcq_insert_before(q, &spipe->to);
   2489 }
   2490 
   2491 static void
   2492 slhci_callback_schedule(struct slhci_softc *sc)
   2493 {
   2494 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2495 	struct slhci_transfers *t;
   2496 
   2497 	t = &sc->sc_transfers;
   2498 
   2499 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2500 
   2501 	if (t->flags & F_ACTIVE)
   2502 		slhci_do_callback_schedule(sc);
   2503 }
   2504 
   2505 static void
   2506 slhci_do_callback_schedule(struct slhci_softc *sc)
   2507 {
   2508 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2509 	struct slhci_transfers *t;
   2510 
   2511 	t = &sc->sc_transfers;
   2512 
   2513 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2514 
   2515 	DLOG(D_MSG, "flags %#jx", t->flags, 0, 0, 0);
   2516 	if (!(t->flags & F_CALLBACK)) {
   2517 		t->flags |= F_CALLBACK;
   2518 		softint_schedule(sc->sc_cb_softintr);
   2519 	}
   2520 }
   2521 
   2522 #if 0
   2523 /* must be called with lock taken. */
   2524 /* XXX static */ void
   2525 slhci_pollxfer(struct slhci_softc *sc, struct usbd_xfer *xfer)
   2526 {
   2527 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2528 	slhci_dotransfer(sc);
   2529 	do {
   2530 		slhci_dointr(sc);
   2531 	} while (xfer->ux_status == USBD_IN_PROGRESS);
   2532 	slhci_do_callback(sc, xfer);
   2533 }
   2534 #endif
   2535 
   2536 static usbd_status
   2537 slhci_do_poll(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
   2538     usbd_xfer *xfer)
   2539 {
   2540 	slhci_waitintr(sc, 0);
   2541 
   2542 	return USBD_NORMAL_COMPLETION;
   2543 }
   2544 
   2545 static usbd_status
   2546 slhci_lsvh_warn(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
   2547     usbd_xfer *xfer)
   2548 {
   2549 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2550 	struct slhci_transfers *t;
   2551 
   2552 	t = &sc->sc_transfers;
   2553 
   2554 	if (!(t->flags & F_LSVH_WARNED)) {
   2555 		printf("%s: Low speed device via hub disabled, "
   2556 		    "see slhci(4)\n", SC_NAME(sc));
   2557 		DDOLOG("Low speed device via hub disabled, "
   2558 		    "see slhci(4)", SC_NAME(sc), 0,0,0);
   2559 		t->flags |= F_LSVH_WARNED;
   2560 	}
   2561 	return USBD_INVAL;
   2562 }
   2563 
   2564 static usbd_status
   2565 slhci_isoc_warn(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
   2566     usbd_xfer *xfer)
   2567 {
   2568 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2569 	struct slhci_transfers *t;
   2570 
   2571 	t = &sc->sc_transfers;
   2572 
   2573 	if (!(t->flags & F_ISOC_WARNED)) {
   2574 		printf("%s: ISOC transfer not supported "
   2575 		    "(see slhci(4))\n", SC_NAME(sc));
   2576 		DDOLOG("ISOC transfer not supported "
   2577 		    "(see slhci(4))", 0, 0, 0, 0);
   2578 		t->flags |= F_ISOC_WARNED;
   2579 	}
   2580 	return USBD_INVAL;
   2581 }
   2582 
   2583 static usbd_status
   2584 slhci_open_pipe(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
   2585     usbd_xfer *xfer)
   2586 {
   2587 	struct slhci_transfers *t;
   2588 	struct usbd_pipe *pipe;
   2589 
   2590 	t = &sc->sc_transfers;
   2591 	pipe = &spipe->pipe;
   2592 
   2593 	if (t->flags & F_DISABLED)
   2594 		return USBD_CANCELLED;
   2595 	else if (pipe->up_interval && !slhci_reserve_bustime(sc, spipe, 1))
   2596 		return USBD_PENDING_REQUESTS;
   2597 	else {
   2598 		enter_all_pipes(t, spipe);
   2599 		return USBD_NORMAL_COMPLETION;
   2600 	}
   2601 }
   2602 
   2603 static usbd_status
   2604 slhci_close_pipe(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
   2605     usbd_xfer *xfer)
   2606 {
   2607 	struct usbd_pipe *pipe;
   2608 
   2609 	pipe = &spipe->pipe;
   2610 
   2611 	if (pipe->up_interval && spipe->ptype != PT_ROOT_INTR)
   2612 		slhci_reserve_bustime(sc, spipe, 0);
   2613 	gcq_remove(&spipe->ap);
   2614 	return USBD_NORMAL_COMPLETION;
   2615 }
   2616 
   2617 static usbd_status
   2618 slhci_do_abort(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
   2619     usbd_xfer *xfer)
   2620 {
   2621 	struct slhci_transfers *t;
   2622 
   2623 	t = &sc->sc_transfers;
   2624 
   2625 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2626 
   2627 	if (spipe->xfer == xfer) {
   2628 		if (spipe->ptype == PT_ROOT_INTR) {
   2629 			if (t->rootintr == spipe->xfer) /* XXX assert? */
   2630 				t->rootintr = NULL;
   2631 		} else {
   2632 			gcq_remove(&spipe->to);
   2633 			gcq_remove(&spipe->xq);
   2634 
   2635 			if (t->spipe[A] == spipe) {
   2636 				t->spipe[A] = NULL;
   2637 				if (!(t->flags & F_AINPROG))
   2638 					t->len[A] = -1;
   2639 			} else if (t->spipe[B] == spipe) {
   2640 					t->spipe[B] = NULL;
   2641 				if (!(t->flags & F_BINPROG))
   2642 					t->len[B] = -1;
   2643 			}
   2644 		}
   2645 
   2646 		if (xfer->ux_status != USBD_TIMEOUT) {
   2647 			spipe->xfer = NULL;
   2648 			spipe->pipe.up_repeat = 0; /* XXX timeout? */
   2649 		}
   2650 	}
   2651 
   2652 	return USBD_NORMAL_COMPLETION;
   2653 }
   2654 
   2655 /*
   2656  * Called to deactivate or stop use of the controller instead of panicking.
   2657  * Will cancel the xfer correctly even when not on a list.
   2658  */
   2659 static usbd_status
   2660 slhci_halt(struct slhci_softc *sc, struct slhci_pipe *spipe,
   2661     struct usbd_xfer *xfer)
   2662 {
   2663 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2664 	struct slhci_transfers *t;
   2665 
   2666 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2667 
   2668 	t = &sc->sc_transfers;
   2669 
   2670 	DDOLOG("Halt! sc %p spipe %p xfer %p", sc, spipe, xfer, 0);
   2671 
   2672 	if (spipe != NULL)
   2673 		slhci_log_spipe(spipe);
   2674 
   2675 	if (xfer != NULL)
   2676 		slhci_log_xfer(xfer);
   2677 
   2678 	if (spipe != NULL && xfer != NULL && spipe->xfer == xfer &&
   2679 	    !gcq_onlist(&spipe->xq) && t->spipe[A] != spipe && t->spipe[B] !=
   2680 	    spipe) {
   2681 		xfer->ux_status = USBD_CANCELLED;
   2682 		enter_callback(t, spipe);
   2683 	}
   2684 
   2685 	if (t->flags & F_ACTIVE) {
   2686 		slhci_intrchange(sc, 0);
   2687 		/*
   2688 		 * leave power on when halting in case flash devices or disks
   2689 		 * are attached, which may be writing and could be damaged
   2690 		 * by abrupt power loss.  The root hub clear power feature
   2691 		 * should still work after halting.
   2692 		 */
   2693 	}
   2694 
   2695 	t->flags &= ~F_ACTIVE;
   2696 	t->flags |= F_UDISABLED;
   2697 	if (!(t->flags & F_NODEV))
   2698 		t->flags |= F_NODEV|F_CCONNECT|F_ROOTINTR;
   2699 	slhci_drain(sc);
   2700 
   2701 	/* One last callback for the drain and device removal. */
   2702 	slhci_do_callback_schedule(sc);
   2703 
   2704 	return USBD_NORMAL_COMPLETION;
   2705 }
   2706 
   2707 /*
   2708  * There are three interrupt states: no interrupts during reset and after
   2709  * device deactivation, INSERT only for no device present but power on, and
   2710  * SOF, INSERT, ADONE, and BDONE when device is present.
   2711  */
   2712 static void
   2713 slhci_intrchange(struct slhci_softc *sc, uint8_t new_ier)
   2714 {
   2715 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2716 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2717 	if (sc->sc_ier != new_ier) {
   2718 		DLOG(D_INTR, "New IER %#jx", new_ier, 0, 0, 0);
   2719 		sc->sc_ier = new_ier;
   2720 		slhci_write(sc, SL11_IER, new_ier);
   2721 		BSB_SYNC(sc->iot, sc->ioh, sc->pst, sc->psz);
   2722 	}
   2723 }
   2724 
   2725 /*
   2726  * Drain: cancel all pending transfers and put them on the callback list and
   2727  * set the UDISABLED flag.  UDISABLED is cleared only by reset.
   2728  */
   2729 static void
   2730 slhci_drain(struct slhci_softc *sc)
   2731 {
   2732 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2733 	struct slhci_transfers *t;
   2734 	struct slhci_pipe *spipe;
   2735 	struct gcq *q;
   2736 	int i;
   2737 
   2738  	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2739 
   2740 	t = &sc->sc_transfers;
   2741 
   2742 	DLOG(D_MSG, "DRAIN flags %#jx", t->flags, 0,0,0);
   2743 
   2744 	t->pend = INT_MAX;
   2745 
   2746 	for (i = 0; i <= 1; i++) {
   2747 		t->len[i] = -1;
   2748 		if (t->spipe[i] != NULL) {
   2749 			enter_callback(t, t->spipe[i]);
   2750 			t->spipe[i] = NULL;
   2751 		}
   2752 	}
   2753 
   2754 	/* Merge the queues into the callback queue. */
   2755 	gcq_merge_tail(&t->q[Q_CALLBACKS], &t->q[Q_CB]);
   2756 	gcq_merge_tail(&t->q[Q_CALLBACKS], &t->q[Q_NEXT_CB]);
   2757 	gcq_merge_tail(&t->q[Q_CALLBACKS], &t->timed);
   2758 
   2759 	/*
   2760 	 * Cancel all pipes.  Note that not all of these may be on the
   2761 	 * callback queue yet; some could be in slhci_start, for example.
   2762 	 */
   2763 	FOREACH_AP(q, t, spipe) {
   2764 		spipe->pflags |= PF_GONE;
   2765 		spipe->pipe.up_repeat = 0;
   2766 		spipe->pipe.up_aborting = 1;
   2767 		if (spipe->xfer != NULL)
   2768 			spipe->xfer->ux_status = USBD_CANCELLED;
   2769 	}
   2770 
   2771 	gcq_remove_all(&t->to);
   2772 
   2773 	t->flags |= F_UDISABLED;
   2774 	t->flags &= ~(F_AREADY|F_BREADY|F_AINPROG|F_BINPROG|F_LOWSPEED);
   2775 }
   2776 
   2777 /*
   2778  * RESET: SL11_CTRL_RESETENGINE=1 and SL11_CTRL_JKSTATE=0 for 50ms
   2779  * reconfigure SOF after reset, must wait 2.5us before USB bus activity (SOF)
   2780  * check attached device speed.
   2781  * must wait 100ms before USB transaction according to app note, 10ms
   2782  * by spec.  uhub does this delay
   2783  *
   2784  * Started from root hub set feature reset, which does step one.
   2785  * ub_usepolling will call slhci_reset directly, otherwise the callout goes
   2786  * through slhci_reset_entry.
   2787  */
   2788 void
   2789 slhci_reset(struct slhci_softc *sc)
   2790 {
   2791 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2792 	struct slhci_transfers *t;
   2793 	struct slhci_pipe *spipe;
   2794 	struct gcq *q;
   2795 	uint8_t r, pol, ctrl;
   2796 
   2797 	t = &sc->sc_transfers;
   2798 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2799 
   2800 	stop_cc_time(&t_delay);
   2801 
   2802 	KASSERT(t->flags & F_ACTIVE);
   2803 
   2804 	start_cc_time(&t_delay, 0);
   2805 	stop_cc_time(&t_delay);
   2806 
   2807 	slhci_write(sc, SL11_CTRL, 0);
   2808 	start_cc_time(&t_delay, 3);
   2809 	DELAY(3);
   2810 	stop_cc_time(&t_delay);
   2811 	slhci_write(sc, SL11_ISR, 0xff);
   2812 
   2813 	r = slhci_read(sc, SL11_ISR);
   2814 
   2815 	if (r & SL11_ISR_INSERT)
   2816 		slhci_write(sc, SL11_ISR, SL11_ISR_INSERT);
   2817 
   2818 	if (r & SL11_ISR_NODEV) {
   2819 		DLOG(D_MSG, "NC", 0,0,0,0);
   2820 		/*
   2821 		 * Normally, the hard interrupt insert routine will issue
   2822 		 * CCONNECT, however we need to do it here if the detach
   2823 		 * happened during reset.
   2824 		 */
   2825 		if (!(t->flags & F_NODEV))
   2826 			t->flags |= F_CCONNECT|F_ROOTINTR|F_NODEV;
   2827 		slhci_intrchange(sc, SL11_IER_INSERT);
   2828 	} else {
   2829 		if (t->flags & F_NODEV)
   2830 			t->flags |= F_CCONNECT;
   2831 		t->flags &= ~(F_NODEV|F_LOWSPEED);
   2832 		if (r & SL11_ISR_DATA) {
   2833 			DLOG(D_MSG, "FS", 0,0,0,0);
   2834 			pol = ctrl = 0;
   2835 		} else {
   2836 			DLOG(D_MSG, "LS", 0,0,0,0);
   2837 			pol  = SL811_CSOF_POLARITY;
   2838 			ctrl = SL11_CTRL_LOWSPEED;
   2839 			t->flags |= F_LOWSPEED;
   2840 		}
   2841 
   2842 		/* Enable SOF auto-generation */
   2843 		t->frame = 0;	/* write to SL811_CSOF will reset frame */
   2844 		slhci_write(sc, SL11_SOFTIME, 0xe0);
   2845 		slhci_write(sc, SL811_CSOF, pol|SL811_CSOF_MASTER|0x2e);
   2846 		slhci_write(sc, SL11_CTRL, ctrl|SL11_CTRL_ENABLESOF);
   2847 
   2848 		/*
   2849 		 * According to the app note, ARM must be set
   2850 		 * for SOF generation to work.  We initialize all
   2851 		 * USBA registers here for current_tregs.
   2852 		 */
   2853 		slhci_write(sc, SL11_E0ADDR, SL11_BUFFER_START);
   2854 		slhci_write(sc, SL11_E0LEN, 0);
   2855 		slhci_write(sc, SL11_E0PID, SL11_PID_SOF);
   2856 		slhci_write(sc, SL11_E0DEV, 0);
   2857 		slhci_write(sc, SL11_E0CTRL, SL11_EPCTRL_ARM);
   2858 
   2859 		/*
   2860 		 * Initialize B registers.  This can't be done earlier since
   2861 		 * they are not valid until the SL811_CSOF register is written
   2862 		 * above due to SL11H compatability.
   2863 		 */
   2864 		slhci_write(sc, SL11_E1ADDR, SL11_BUFFER_END - 8);
   2865 		slhci_write(sc, SL11_E1LEN, 0);
   2866 		slhci_write(sc, SL11_E1PID, 0);
   2867 		slhci_write(sc, SL11_E1DEV, 0);
   2868 
   2869 		t->current_tregs[0][ADR] = SL11_BUFFER_START;
   2870 		t->current_tregs[0][LEN] = 0;
   2871 		t->current_tregs[0][PID] = SL11_PID_SOF;
   2872 		t->current_tregs[0][DEV] = 0;
   2873 		t->current_tregs[1][ADR] = SL11_BUFFER_END - 8;
   2874 		t->current_tregs[1][LEN] = 0;
   2875 		t->current_tregs[1][PID] = 0;
   2876 		t->current_tregs[1][DEV] = 0;
   2877 
   2878 		/* SOF start will produce USBA interrupt */
   2879 		t->len[A] = 0;
   2880 		t->flags |= F_AINPROG;
   2881 
   2882 		slhci_intrchange(sc, SLHCI_NORMAL_INTERRUPTS);
   2883 	}
   2884 
   2885 	t->flags &= ~(F_UDISABLED|F_RESET);
   2886 	t->flags |= F_CRESET|F_ROOTINTR;
   2887 	FOREACH_AP(q, t, spipe) {
   2888 		spipe->pflags &= ~PF_GONE;
   2889 		spipe->pipe.up_aborting = 0;
   2890 	}
   2891 	DLOG(D_MSG, "RESET done flags %#jx", t->flags, 0,0,0);
   2892 }
   2893 
   2894 
   2895 #ifdef SLHCI_DEBUG
   2896 static int
   2897 slhci_memtest(struct slhci_softc *sc)
   2898 {
   2899 	enum { ASC, DESC, EITHER = ASC };	/* direction */
   2900 	enum { READ, WRITE };			/* operation */
   2901 	const char *ptr, *elem;
   2902 	size_t i;
   2903 	const int low = SL11_BUFFER_START, high = SL11_BUFFER_END;
   2904 	int addr = 0, dir = ASC, op = READ;
   2905 	/* Extended March C- test algorithm (SOFs also) */
   2906 	const char test[] = "E(w0) A(r0w1r1) A(r1w0r0) D(r0w1) D(r1w0) E(r0)";
   2907 	char c;
   2908 	const uint8_t dbs[] = { 0x00, 0x0f, 0x33, 0x55 }; /* data backgrounds */
   2909 	uint8_t db;
   2910 
   2911 	/* Perform memory test for all data backgrounds. */
   2912 	for (i = 0; i < __arraycount(dbs); i++) {
   2913 		ptr = test;
   2914 		elem = ptr;
   2915 		/* Walk test algorithm string. */
   2916 		while ((c = *ptr++) != '\0')
   2917 			switch (tolower((int)c)) {
   2918 			case 'a':
   2919 				/* Address sequence is in ascending order. */
   2920 				dir = ASC;
   2921 				break;
   2922 			case 'd':
   2923 				/* Address sequence is in descending order. */
   2924 				dir = DESC;
   2925 				break;
   2926 			case 'e':
   2927 				/* Address sequence is in either order. */
   2928 				dir = EITHER;
   2929 				break;
   2930 			case '(':
   2931 				/* Start of test element (sequence). */
   2932 				elem = ptr;
   2933 				addr = (dir == ASC) ? low : high;
   2934 				break;
   2935 			case 'r':
   2936 				/* read operation */
   2937 				op = READ;
   2938 				break;
   2939 			case 'w':
   2940 				/* write operation */
   2941 				op = WRITE;
   2942 				break;
   2943 			case '0':
   2944 			case '1':
   2945 				/*
   2946 				 * Execute previously set-up operation by
   2947 				 * reading/writing non-inverted ('0') or
   2948 				 * inverted ('1') data background.
   2949 				 */
   2950 				db = (c - '0') ? ~dbs[i] : dbs[i];
   2951 				if (op == READ) {
   2952 					if (slhci_read(sc, addr) != db)
   2953 						return -1;
   2954 				} else
   2955 					slhci_write(sc, addr, db);
   2956 				break;
   2957 			case ')':
   2958 				/*
   2959 				 * End of element: Repeat same element with next
   2960 				 * address or continue to next element.
   2961 				 */
   2962 				addr = (dir == ASC) ? addr + 1 : addr - 1;
   2963 				if (addr >= low && addr <= high)
   2964 					ptr = elem;
   2965 				break;
   2966 			default:
   2967 				/* Do nothing. */
   2968 				break;
   2969 			}
   2970 	}
   2971 
   2972 	return 0;
   2973 }
   2974 #endif
   2975 
   2976 /* returns 1 if succeeded, 0 if failed, reserve == 0 is unreserve */
   2977 static int
   2978 slhci_reserve_bustime(struct slhci_softc *sc, struct slhci_pipe *spipe, int
   2979     reserve)
   2980 {
   2981 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   2982 	struct slhci_transfers *t;
   2983 	int bustime, max_packet;
   2984 
   2985 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   2986 
   2987 	t = &sc->sc_transfers;
   2988 	max_packet = UGETW(spipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize);
   2989 
   2990 	if (spipe->pflags & PF_LS)
   2991 		bustime = SLHCI_LS_CONST + SLHCI_LS_DATA_TIME(max_packet);
   2992 	else
   2993 		bustime = SLHCI_FS_CONST + SLHCI_FS_DATA_TIME(max_packet);
   2994 
   2995 	if (!reserve) {
   2996 		t->reserved_bustime -= bustime;
   2997 #ifdef DIAGNOSTIC
   2998 		if (t->reserved_bustime < 0) {
   2999 			printf("%s: reserved_bustime %d < 0!\n",
   3000 			    SC_NAME(sc), t->reserved_bustime);
   3001 			DDOLOG("reserved_bustime %d < 0!",
   3002 			    t->reserved_bustime, 0, 0, 0);
   3003 			t->reserved_bustime = 0;
   3004 		}
   3005 #endif
   3006 		return 1;
   3007 	}
   3008 
   3009 	if (t->reserved_bustime + bustime > SLHCI_RESERVED_BUSTIME) {
   3010 		if (ratecheck(&sc->sc_reserved_warn_rate,
   3011 		    &reserved_warn_rate))
   3012 #ifdef SLHCI_NO_OVERTIME
   3013 		{
   3014 			printf("%s: Max reserved bus time exceeded! "
   3015 			    "Erroring request.\n", SC_NAME(sc));
   3016 			DDOLOG("%s: Max reserved bus time exceeded! "
   3017 			    "Erroring request.", 0, 0, 0, 0);
   3018 		}
   3019 		return 0;
   3020 #else
   3021 		{
   3022 			printf("%s: Reserved bus time exceeds %d!\n",
   3023 			    SC_NAME(sc), SLHCI_RESERVED_BUSTIME);
   3024 			DDOLOG("Reserved bus time exceeds %d!",
   3025 			    SLHCI_RESERVED_BUSTIME, 0, 0, 0);
   3026 		}
   3027 #endif
   3028 	}
   3029 
   3030 	t->reserved_bustime += bustime;
   3031 	return 1;
   3032 }
   3033 
   3034 /* Device insertion/removal interrupt */
   3035 static void
   3036 slhci_insert(struct slhci_softc *sc)
   3037 {
   3038 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3039 	struct slhci_transfers *t;
   3040 
   3041 	t = &sc->sc_transfers;
   3042 
   3043 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   3044 
   3045 	if (t->flags & F_NODEV)
   3046 		slhci_intrchange(sc, 0);
   3047 	else {
   3048 		slhci_drain(sc);
   3049 		slhci_intrchange(sc, SL11_IER_INSERT);
   3050 	}
   3051 	t->flags ^= F_NODEV;
   3052 	t->flags |= F_ROOTINTR|F_CCONNECT;
   3053 	DLOG(D_MSG, "INSERT intr: flags after %#jx", t->flags, 0,0,0);
   3054 }
   3055 
   3056 /*
   3057  * Data structures and routines to emulate the root hub.
   3058  */
   3059 
   3060 static usbd_status
   3061 slhci_clear_feature(struct slhci_softc *sc, unsigned int what)
   3062 {
   3063 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3064 	struct slhci_transfers *t;
   3065 	usbd_status error;
   3066 
   3067 	t = &sc->sc_transfers;
   3068 	error = USBD_NORMAL_COMPLETION;
   3069 
   3070 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   3071 
   3072 	if (what == UHF_PORT_POWER) {
   3073 		DLOG(D_MSG, "POWER_OFF", 0,0,0,0);
   3074 		t->flags &= ~F_POWER;
   3075 		if (!(t->flags & F_NODEV))
   3076 			t->flags |= F_NODEV|F_CCONNECT|F_ROOTINTR;
   3077 		/* for x68k Nereid USB controller */
   3078 		if (sc->sc_enable_power && (t->flags & F_REALPOWER)) {
   3079 			t->flags &= ~F_REALPOWER;
   3080 			sc->sc_enable_power(sc, POWER_OFF);
   3081 		}
   3082 		slhci_intrchange(sc, 0);
   3083 		slhci_drain(sc);
   3084 	} else if (what == UHF_C_PORT_CONNECTION) {
   3085 		t->flags &= ~F_CCONNECT;
   3086 	} else if (what == UHF_C_PORT_RESET) {
   3087 		t->flags &= ~F_CRESET;
   3088 	} else if (what == UHF_PORT_ENABLE) {
   3089 		slhci_drain(sc);
   3090 	} else if (what != UHF_PORT_SUSPEND) {
   3091 		DDOLOG("ClrPortFeatERR:value=%#.4x", what, 0,0,0);
   3092 		error = USBD_IOERROR;
   3093 	}
   3094 
   3095 	return error;
   3096 }
   3097 
   3098 static usbd_status
   3099 slhci_set_feature(struct slhci_softc *sc, unsigned int what)
   3100 {
   3101 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3102 	struct slhci_transfers *t;
   3103 	uint8_t r;
   3104 
   3105 	t = &sc->sc_transfers;
   3106 
   3107 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   3108 
   3109 	if (what == UHF_PORT_RESET) {
   3110 		if (!(t->flags & F_ACTIVE)) {
   3111 			DDOLOG("SET PORT_RESET when not ACTIVE!",
   3112 			    0,0,0,0);
   3113 			return USBD_INVAL;
   3114 		}
   3115 		if (!(t->flags & F_POWER)) {
   3116 			DDOLOG("SET PORT_RESET without PORT_POWER! flags %p",
   3117 			    t->flags, 0,0,0);
   3118 			return USBD_INVAL;
   3119 		}
   3120 		if (t->flags & F_RESET)
   3121 			return USBD_NORMAL_COMPLETION;
   3122 		DLOG(D_MSG, "RESET flags %#jx", t->flags, 0,0,0);
   3123 		slhci_intrchange(sc, 0);
   3124 		slhci_drain(sc);
   3125 		slhci_write(sc, SL11_CTRL, SL11_CTRL_RESETENGINE);
   3126 		/* usb spec says delay >= 10ms, app note 50ms */
   3127  		start_cc_time(&t_delay, 50000);
   3128 		if (sc->sc_bus.ub_usepolling) {
   3129 			DELAY(50000);
   3130 			slhci_reset(sc);
   3131 		} else {
   3132 			t->flags |= F_RESET;
   3133 			callout_schedule(&sc->sc_timer, uimax(mstohz(50), 2));
   3134 		}
   3135 	} else if (what == UHF_PORT_SUSPEND) {
   3136 		printf("%s: USB Suspend not implemented!\n", SC_NAME(sc));
   3137 		DDOLOG("USB Suspend not implemented!", 0, 0, 0, 0);
   3138 	} else if (what == UHF_PORT_POWER) {
   3139 		DLOG(D_MSG, "PORT_POWER", 0,0,0,0);
   3140 		/* for x68k Nereid USB controller */
   3141 		if (!(t->flags & F_ACTIVE))
   3142 			return USBD_INVAL;
   3143 		if (t->flags & F_POWER)
   3144 			return USBD_NORMAL_COMPLETION;
   3145 		if (!(t->flags & F_REALPOWER)) {
   3146 			if (sc->sc_enable_power)
   3147 				sc->sc_enable_power(sc, POWER_ON);
   3148 			t->flags |= F_REALPOWER;
   3149 		}
   3150 		t->flags |= F_POWER;
   3151 		r = slhci_read(sc, SL11_ISR);
   3152 		if (r & SL11_ISR_INSERT)
   3153 			slhci_write(sc, SL11_ISR, SL11_ISR_INSERT);
   3154 		if (r & SL11_ISR_NODEV) {
   3155 			slhci_intrchange(sc, SL11_IER_INSERT);
   3156 			t->flags |= F_NODEV;
   3157 		} else {
   3158 			t->flags &= ~F_NODEV;
   3159 			t->flags |= F_CCONNECT|F_ROOTINTR;
   3160 		}
   3161 	} else {
   3162 		DDOLOG("SetPortFeatERR=%#.8x", what, 0,0,0);
   3163 		return USBD_IOERROR;
   3164 	}
   3165 
   3166 	return USBD_NORMAL_COMPLETION;
   3167 }
   3168 
   3169 static void
   3170 slhci_get_status(struct slhci_softc *sc, usb_port_status_t *ps)
   3171 {
   3172 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3173 	struct slhci_transfers *t;
   3174 	unsigned int status, change;
   3175 
   3176 	t = &sc->sc_transfers;
   3177 
   3178 	KASSERT(mutex_owned(&sc->sc_intr_lock));
   3179 
   3180 	/*
   3181 	 * We do not have a way to detect over current or babble and
   3182 	 * suspend is currently not implemented, so connect and reset
   3183 	 * are the only changes that need to be reported.
   3184 	 */
   3185 	change = 0;
   3186 	if (t->flags & F_CCONNECT)
   3187 		change |= UPS_C_CONNECT_STATUS;
   3188 	if (t->flags & F_CRESET)
   3189 		change |= UPS_C_PORT_RESET;
   3190 
   3191 	status = 0;
   3192 	if (!(t->flags & F_NODEV))
   3193 		status |= UPS_CURRENT_CONNECT_STATUS;
   3194 	if (!(t->flags & F_UDISABLED))
   3195 		status |= UPS_PORT_ENABLED;
   3196 	if (t->flags & F_RESET)
   3197 		status |= UPS_RESET;
   3198 	if (t->flags & F_POWER)
   3199 		status |= UPS_PORT_POWER;
   3200 	if (t->flags & F_LOWSPEED)
   3201 		status |= UPS_LOW_SPEED;
   3202 	USETW(ps->wPortStatus, status);
   3203 	USETW(ps->wPortChange, change);
   3204 	DLOG(D_ROOT, "status=%#.4jx, change=%#.4jx", status, change, 0,0);
   3205 }
   3206 
   3207 static int
   3208 slhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
   3209     void *buf, int buflen)
   3210 {
   3211 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3212 	struct slhci_softc *sc = SLHCI_BUS2SC(bus);
   3213 	struct slhci_transfers *t = &sc->sc_transfers;
   3214 	usbd_status error = USBD_IOERROR; /* XXX should be STALL */
   3215 	uint16_t len, value, index;
   3216 	uint8_t type;
   3217 	int actlen = 0;
   3218 
   3219 	len = UGETW(req->wLength);
   3220 	value = UGETW(req->wValue);
   3221 	index = UGETW(req->wIndex);
   3222 
   3223 	type = req->bmRequestType;
   3224 
   3225 	SLHCI_DEXEC(D_TRACE, slhci_log_req(req));
   3226 
   3227 	/*
   3228 	 * USB requests for hubs have two basic types, standard and class.
   3229 	 * Each could potentially have recipients of device, interface,
   3230 	 * endpoint, or other.  For the hub class, CLASS_OTHER means the port
   3231 	 * and CLASS_DEVICE means the hub.  For standard requests, OTHER
   3232 	 * is not used.  Standard request are described in section 9.4 of the
   3233 	 * standard, hub class requests in 11.16.  Each request is either read
   3234 	 * or write.
   3235 	 *
   3236 	 * Clear Feature, Set Feature, and Status are defined for each of the
   3237 	 * used recipients.  Get Descriptor and Set Descriptor are defined for
   3238 	 * both standard and hub class types with different descriptors.
   3239 	 * Other requests have only one defined recipient and type.  These
   3240 	 * include: Get/Set Address, Get/Set Configuration, Get/Set Interface,
   3241 	 * and Synch Frame for standard requests and Get Bus State for hub
   3242 	 * class.
   3243 	 *
   3244 	 * When a device is first powered up it has address 0 until the
   3245 	 * address is set.
   3246 	 *
   3247 	 * Hubs are only allowed to support one interface and may not have
   3248 	 * isochronous endpoints.  The results of the related requests are
   3249 	 * undefined.
   3250 	 *
   3251 	 * The standard requires invalid or unsupported requests to return
   3252 	 * STALL in the data stage, however this does not work well with
   3253 	 * current error handling. XXX
   3254 	 *
   3255 	 * Some unsupported fields:
   3256 	 * Clear Hub Feature is for C_HUB_LOCAL_POWER and C_HUB_OVER_CURRENT
   3257 	 * Set Device Features is for ENDPOINT_HALT and DEVICE_REMOTE_WAKEUP
   3258 	 * Get Bus State is optional sample of D- and D+ at EOF2
   3259 	 */
   3260 
   3261 	switch (req->bRequest) {
   3262 	/* Write Requests */
   3263 	case UR_CLEAR_FEATURE:
   3264 		if (type == UT_WRITE_CLASS_OTHER) {
   3265 			if (index == 1 /* Port */) {
   3266 				mutex_enter(&sc->sc_intr_lock);
   3267 				error = slhci_clear_feature(sc, value);
   3268 				mutex_exit(&sc->sc_intr_lock);
   3269 			} else
   3270 				DLOG(D_ROOT, "Clear Port Feature "
   3271 				    "index = %#.4jx", index, 0,0,0);
   3272 		}
   3273 		break;
   3274 	case UR_SET_FEATURE:
   3275 		if (type == UT_WRITE_CLASS_OTHER) {
   3276 			if (index == 1 /* Port */) {
   3277 				mutex_enter(&sc->sc_intr_lock);
   3278 				error = slhci_set_feature(sc, value);
   3279 				mutex_exit(&sc->sc_intr_lock);
   3280 			} else
   3281 				DLOG(D_ROOT, "Set Port Feature "
   3282 				    "index = %#.4jx", index, 0,0,0);
   3283 		} else if (type != UT_WRITE_CLASS_DEVICE)
   3284 			DLOG(D_ROOT, "Set Device Feature "
   3285 			    "ENDPOINT_HALT or DEVICE_REMOTE_WAKEUP "
   3286 			    "not supported", 0,0,0,0);
   3287 		break;
   3288 
   3289 	/* Read Requests */
   3290 	case UR_GET_STATUS:
   3291 		if (type == UT_READ_CLASS_OTHER) {
   3292 			if (index == 1 /* Port */ && len == /* XXX >=? */
   3293 			    sizeof(usb_port_status_t)) {
   3294 				mutex_enter(&sc->sc_intr_lock);
   3295 				slhci_get_status(sc, (usb_port_status_t *)
   3296 				    buf);
   3297 				mutex_exit(&sc->sc_intr_lock);
   3298 				actlen = sizeof(usb_port_status_t);
   3299 				error = USBD_NORMAL_COMPLETION;
   3300 			} else
   3301 				DLOG(D_ROOT, "Get Port Status index = %#.4jx "
   3302 				    "len = %#.4jx", index, len, 0,0);
   3303 		} else if (type == UT_READ_CLASS_DEVICE) { /* XXX index? */
   3304 			if (len == sizeof(usb_hub_status_t)) {
   3305 				DLOG(D_ROOT, "Get Hub Status",
   3306 				    0,0,0,0);
   3307 				actlen = sizeof(usb_hub_status_t);
   3308 				memset(buf, 0, actlen);
   3309 				error = USBD_NORMAL_COMPLETION;
   3310 			} else
   3311 				DLOG(D_ROOT, "Get Hub Status bad len %#.4jx",
   3312 				    len, 0,0,0);
   3313 		}
   3314 		break;
   3315 	case UR_GET_DESCRIPTOR:
   3316 		if (type == UT_READ_DEVICE) {
   3317 			/* value is type (&0xff00) and index (0xff) */
   3318 			if (value == (UDESC_DEVICE<<8)) {
   3319 				actlen = buflen;
   3320 				error = USBD_NORMAL_COMPLETION;
   3321 			} else if (value == (UDESC_CONFIG<<8)) {
   3322 				struct usb_roothub_descriptors confd;
   3323 
   3324 				actlen = uimin(buflen, sizeof(confd));
   3325 				memcpy(&confd, buf, actlen);
   3326 
   3327 				/* 2 mA units */
   3328 				confd.urh_confd.bMaxPower = t->max_current;
   3329 				memcpy(buf, &confd, actlen);
   3330 				error = USBD_NORMAL_COMPLETION;
   3331 			} else if (value == ((UDESC_STRING<<8)|1)) {
   3332 				/* Vendor */
   3333 				actlen = buflen;
   3334 				error = USBD_NORMAL_COMPLETION;
   3335 			} else if (value == ((UDESC_STRING<<8)|2)) {
   3336 				/* Product */
   3337 				actlen = usb_makestrdesc((usb_string_descriptor_t *)
   3338 				    buf, len, "SL811HS/T root hub");
   3339 				error = USBD_NORMAL_COMPLETION;
   3340 			} else
   3341 				DDOLOG("Unknown Get Descriptor %#.4x",
   3342 				    value, 0,0,0);
   3343 		} else if (type == UT_READ_CLASS_DEVICE) {
   3344 			/* Descriptor number is 0 */
   3345 			if (value == (UDESC_HUB<<8)) {
   3346 				usb_hub_descriptor_t hubd;
   3347 
   3348 				actlen = uimin(buflen, sizeof(hubd));
   3349 				memcpy(&hubd, buf, actlen);
   3350 				hubd.bHubContrCurrent =
   3351 				    500 - t->max_current;
   3352 				memcpy(buf, &hubd, actlen);
   3353 				error = USBD_NORMAL_COMPLETION;
   3354 			} else
   3355 				DDOLOG("Unknown Get Hub Descriptor %#.4x",
   3356 				    value, 0,0,0);
   3357 		}
   3358 		break;
   3359 	default:
   3360 		/* default from usbroothub */
   3361 		return buflen;
   3362 	}
   3363 
   3364 	if (error == USBD_NORMAL_COMPLETION)
   3365 		return actlen;
   3366 
   3367 	return -1;
   3368 }
   3369 
   3370 /* End in lock functions. Start debug functions. */
   3371 
   3372 #ifdef SLHCI_DEBUG
   3373 void
   3374 slhci_log_buffer(struct usbd_xfer *xfer)
   3375 {
   3376 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3377 	u_char *buf;
   3378 
   3379 	if(xfer->ux_length > 0 &&
   3380 	    UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) ==
   3381 	    UE_DIR_IN) {
   3382 		buf = xfer->ux_buf;
   3383 		DDOLOGBUF(buf, xfer->ux_actlen);
   3384 		DDOLOG("len %d actlen %d short %d", xfer->ux_length,
   3385 		    xfer->ux_actlen, xfer->ux_length - xfer->ux_actlen, 0);
   3386 	}
   3387 }
   3388 
   3389 void
   3390 slhci_log_req(usb_device_request_t *r)
   3391 {
   3392 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3393 	int req, type, value, index, len;
   3394 
   3395 	req   = r->bRequest;
   3396 	type  = r->bmRequestType;
   3397 	value = UGETW(r->wValue);
   3398 	index = UGETW(r->wIndex);
   3399 	len   = UGETW(r->wLength);
   3400 
   3401 	DDOLOG("request: type %#x", type, 0, 0, 0);
   3402 	DDOLOG("request: r=%d,v=%d,i=%d,l=%d ", req, value, index, len);
   3403 }
   3404 
   3405 void
   3406 slhci_log_dumpreg(void)
   3407 {
   3408 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3409 	uint8_t r;
   3410 	unsigned int aaddr, alen, baddr, blen;
   3411 	static u_char buf[240];
   3412 
   3413 	r = slhci_read(ssc, SL11_E0CTRL);
   3414 	DDOLOG("USB A Host Control = %#.2x", r, 0, 0, 0);
   3415 	DDOLOGEPCTRL(r);
   3416 
   3417 	aaddr = slhci_read(ssc, SL11_E0ADDR);
   3418 	DDOLOG("USB A Base Address = %u", aaddr, 0,0,0);
   3419 	alen = slhci_read(ssc, SL11_E0LEN);
   3420 	DDOLOG("USB A Length = %u", alen, 0,0,0);
   3421 	r = slhci_read(ssc, SL11_E0STAT);
   3422 	DDOLOG("USB A Status = %#.2x", r, 0,0,0);
   3423 	DDOLOGEPSTAT(r);
   3424 
   3425 	r = slhci_read(ssc, SL11_E0CONT);
   3426 	DDOLOG("USB A Remaining or Overflow Length = %u", r, 0,0,0);
   3427 	r = slhci_read(ssc, SL11_E1CTRL);
   3428 	DDOLOG("USB B Host Control = %#.2x", r, 0,0,0);
   3429 	DDOLOGEPCTRL(r);
   3430 
   3431 	baddr = slhci_read(ssc, SL11_E1ADDR);
   3432 	DDOLOG("USB B Base Address = %u", baddr, 0,0,0);
   3433 	blen = slhci_read(ssc, SL11_E1LEN);
   3434 	DDOLOG("USB B Length = %u", blen, 0,0,0);
   3435 	r = slhci_read(ssc, SL11_E1STAT);
   3436 	DDOLOG("USB B Status = %#.2x", r, 0,0,0);
   3437 	DDOLOGEPSTAT(r);
   3438 
   3439 	r = slhci_read(ssc, SL11_E1CONT);
   3440 	DDOLOG("USB B Remaining or Overflow Length = %u", r, 0,0,0);
   3441 
   3442 	r = slhci_read(ssc, SL11_CTRL);
   3443 	DDOLOG("Control = %#.2x", r, 0,0,0);
   3444 	DDOLOGCTRL(r);
   3445 
   3446 	r = slhci_read(ssc, SL11_IER);
   3447 	DDOLOG("Interrupt Enable = %#.2x", r, 0,0,0);
   3448 	DDOLOGIER(r);
   3449 
   3450 	r = slhci_read(ssc, SL11_ISR);
   3451 	DDOLOG("Interrupt Status = %#.2x", r, 0,0,0);
   3452 	DDOLOGISR(r);
   3453 
   3454 	r = slhci_read(ssc, SL11_REV);
   3455 	DDOLOG("Revision = %#.2x", r, 0,0,0);
   3456 	r = slhci_read(ssc, SL811_CSOF);
   3457 	DDOLOG("SOF Counter = %#.2x", r, 0,0,0);
   3458 
   3459 	if (alen && aaddr >= SL11_BUFFER_START && aaddr < SL11_BUFFER_END &&
   3460 	    alen <= SL11_MAX_PACKET_SIZE && aaddr + alen <= SL11_BUFFER_END) {
   3461 		slhci_read_multi(ssc, aaddr, buf, alen);
   3462 		DDOLOG("USBA Buffer: start %u len %u", aaddr, alen, 0,0);
   3463 		DDOLOGBUF(buf, alen);
   3464 	} else if (alen)
   3465 		DDOLOG("USBA Buffer Invalid", 0,0,0,0);
   3466 
   3467 	if (blen && baddr >= SL11_BUFFER_START && baddr < SL11_BUFFER_END &&
   3468 	    blen <= SL11_MAX_PACKET_SIZE && baddr + blen <= SL11_BUFFER_END) {
   3469 		slhci_read_multi(ssc, baddr, buf, blen);
   3470 		DDOLOG("USBB Buffer: start %u len %u", baddr, blen, 0,0);
   3471 		DDOLOGBUF(buf, blen);
   3472 	} else if (blen)
   3473 		DDOLOG("USBB Buffer Invalid", 0,0,0,0);
   3474 }
   3475 
   3476 void
   3477 slhci_log_xfer(struct usbd_xfer *xfer)
   3478 {
   3479 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3480 	DDOLOG("xfer: length=%u, actlen=%u, flags=%#x, timeout=%u,",
   3481 		xfer->ux_length, xfer->ux_actlen, xfer->ux_flags, xfer->ux_timeout);
   3482 	DDOLOG("buffer=%p", xfer->ux_buf, 0,0,0);
   3483 	slhci_log_req(&xfer->ux_request);
   3484 }
   3485 
   3486 void
   3487 slhci_log_spipe(struct slhci_pipe *spipe)
   3488 {
   3489 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3490 	DDOLOG("spipe %p onlists: AP=%d TO=%d XQ=%d", spipe,
   3491 	    gcq_onlist(&spipe->ap) ? 1 : 0,
   3492 	    gcq_onlist(&spipe->to) ? 1 : 0,
   3493 	    gcq_onlist(&spipe->xq) ? 1 : 0);
   3494 	DDOLOG("spipe: xfer %p buffer %p pflags %#x ptype %d",
   3495 	    spipe->xfer, spipe->buffer, spipe->pflags, spipe->ptype);
   3496 }
   3497 
   3498 void
   3499 slhci_print_intr(void)
   3500 {
   3501 	unsigned int ier, isr;
   3502 	ier = slhci_read(ssc, SL11_IER);
   3503 	isr = slhci_read(ssc, SL11_ISR);
   3504 	printf("IER: %#x ISR: %#x \n", ier, isr);
   3505 }
   3506 
   3507 #if 0
   3508 void
   3509 slhci_log_sc(void)
   3510 {
   3511 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3512 
   3513 	struct slhci_transfers *t;
   3514 	int i;
   3515 
   3516 	t = &ssc->sc_transfers;
   3517 
   3518 	DDOLOG("Flags=%#x", t->flags, 0,0,0);
   3519 	DDOLOG("a = %p Alen=%d b = %p Blen=%d", t->spipe[0], t->len[0],
   3520 	    t->spipe[1], t->len[1]);
   3521 
   3522 	for (i = 0; i <= Q_MAX; i++)
   3523 		DDOLOG("Q %d: %p", i, gcq_hq(&t->q[i]), 0,0);
   3524 
   3525 	DDOLOG("TIMED: %p", GCQ_ITEM(gcq_hq(&t->to),
   3526 	    struct slhci_pipe, to), 0,0,0);
   3527 
   3528 	DDOLOG("frame=%d rootintr=%p", t->frame, t->rootintr, 0,0);
   3529 
   3530 	DDOLOG("ub_usepolling=%d", ssc->sc_bus.ub_usepolling, 0, 0, 0);
   3531 }
   3532 
   3533 void
   3534 slhci_log_slreq(struct slhci_pipe *r)
   3535 {
   3536 	SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
   3537 	DDOLOG("xfer: %p", r->xfer, 0,0,0);
   3538 	DDOLOG("buffer: %p", r->buffer, 0,0,0);
   3539 	DDOLOG("bustime: %u", r->bustime, 0,0,0);
   3540 	DDOLOG("control: %#x", r->control, 0,0,0);
   3541 	DDOLOGEPCTRL(r->control);
   3542 
   3543 	DDOLOG("pid: %#x", r->tregs[PID], 0,0,0);
   3544 	DDOLOG("dev: %u", r->tregs[DEV], 0,0,0);
   3545 	DDOLOG("len: %u", r->tregs[LEN], 0,0,0);
   3546 
   3547 	if (r->xfer)
   3548 		slhci_log_xfer(r->xfer);
   3549 }
   3550 #endif
   3551 #endif /* SLHCI_DEBUG */
   3552 /* End debug functions. */
   3553