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