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