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