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