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