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