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