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