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