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