#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sl811hs.c,v 1.112 2022/05/03 20:52:32 andvar Exp $");
#ifdef _KERNEL_OPT
#include "opt_slhci.h"
#include "opt_usb.h"
#endif
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/device.h>
#include <sys/gcq.h>
#include <sys/intr.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usbhist.h>
#include <dev/usb/usb_mem.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/usbroothub.h>
#include <dev/ic/sl811hsreg.h>
#include <dev/ic/sl811hsvar.h>
#define Q_CB 0
#define Q_NEXT_CB 1
#define Q_MAX_XFER Q_CB
#define Q_CALLBACKS 2
#define Q_MAX Q_CALLBACKS
#define F_AREADY (0x00000001)
#define F_BREADY (0x00000002)
#define F_AINPROG (0x00000004)
#define F_BINPROG (0x00000008)
#define F_LOWSPEED (0x00000010)
#define F_UDISABLED (0x00000020)
#define F_NODEV (0x00000040)
#define F_ROOTINTR (0x00000080)
#define F_REALPOWER (0x00000100)
#define F_POWER (0x00000200)
#define F_ACTIVE (0x00000400)
#define F_CALLBACK (0x00000800)
#define F_SOFCHECK1 (0x00001000)
#define F_SOFCHECK2 (0x00002000)
#define F_CRESET (0x00004000)
#define F_CCONNECT (0x00008000)
#define F_RESET (0x00010000)
#define F_ISOC_WARNED (0x00020000)
#define F_LSVH_WARNED (0x00040000)
#define F_DISABLED (F_NODEV|F_UDISABLED)
#define F_CHANGE (F_CRESET|F_CCONNECT)
#ifdef SLHCI_TRY_LSVH
unsigned int slhci_try_lsvh = 1;
#else
unsigned int slhci_try_lsvh = 0;
#endif
#define ADR 0
#define LEN 1
#define PID 2
#define DEV 3
#define STAT 2
#define CONT 3
#define A 0
#define B 1
static const uint8_t slhci_tregs[2][4] =
{{SL11_E0ADDR, SL11_E0LEN, SL11_E0PID, SL11_E0DEV },
{SL11_E1ADDR, SL11_E1LEN, SL11_E1PID, SL11_E1DEV }};
#define PT_ROOT_CTRL 0
#define PT_ROOT_INTR 1
#define PT_CTRL_SETUP 2
#define PT_CTRL_DATA 3
#define PT_CTRL_STATUS 4
#define PT_INTR 5
#define PT_BULK 6
#define PT_MAX 6
#ifdef SLHCI_DEBUG
#define SLHCI_MEM_ACCOUNTING
#endif
#ifndef SLHCI_RESERVED_BUSTIME
#define SLHCI_RESERVED_BUSTIME 5000
#endif
#ifndef SLHCI_OVERTIME_WARNING_RATE
#define SLHCI_OVERTIME_WARNING_RATE { 60, 0 }
#endif
static const struct timeval reserved_warn_rate = SLHCI_OVERTIME_WARNING_RATE;
#define SLHCI_STANDARD_END_BUSTIME 128
#ifndef SLHCI_EXTRA_END_BUSTIME
#define SLHCI_EXTRA_END_BUSTIME 0
#endif
#define SLHCI_END_BUSTIME (SLHCI_STANDARD_END_BUSTIME+SLHCI_EXTRA_END_BUSTIME)
#define SLHCI_FS_CONST 114
#define SLHCI_FSII_CONST 92
#define SLHCI_FSIO_CONST 80
#define SLHCI_FSI_CONST 92
#define SLHCI_LS_CONST 804
#ifndef SLHCI_PRECICE_BUSTIME
#define SLHCI_FS_DATA_TIME(len) (((u_int)(len)<<3)+(len)+((len)>>1))
#define SLHCI_LS_DATA_TIME(len) (((u_int)(len)<<6)+((u_int)(len)<<4))
#else
#define SLHCI_FS_DATA_TIME(len) (56*(len)/6)
#define SLHCI_LS_DATA_TIME(len) (449*(len)/6)
#endif
#ifndef SLHCI_WAIT_SIZE
#define SLHCI_WAIT_SIZE 8
#endif
#ifndef SLHCI_WAIT_TIME
#define SLHCI_WAIT_TIME (SLHCI_FS_CONST + \
SLHCI_FS_DATA_TIME(SLHCI_WAIT_SIZE))
#endif
const int slhci_wait_time = SLHCI_WAIT_TIME;
#ifndef SLHCI_MAX_RETRIES
#define SLHCI_MAX_RETRIES 3
#endif
#ifndef SLHCI_IER_CHECK_FREQUENCY
#ifdef SLHCI_DEBUG
#define SLHCI_IER_CHECK_FREQUENCY 1
#else
#define SLHCI_IER_CHECK_FREQUENCY 100
#endif
#endif
struct slhci_pipe {
struct usbd_pipe pipe;
struct usbd_xfer *xfer;
uint8_t *buffer;
struct gcq ap;
struct gcq to;
struct gcq xq;
unsigned int pflags;
#define PF_GONE (0x01)
#define PF_TOGGLE (0x02)
#define PF_LS (0x04)
#define PF_PREAMBLE (0x08)
Frame to_frame;
Frame frame;
Frame lastframe;
uint16_t bustime;
uint16_t newbustime[2];
uint8_t tregs[4];
uint8_t newlen[2];
uint8_t newpid;
uint8_t wantshort;
uint8_t control;
uint8_t nerrs;
uint8_t ptype;
};
#define SLHCI_BUS2SC(bus) ((bus)->ub_hcpriv)
#define SLHCI_PIPE2SC(pipe) SLHCI_BUS2SC((pipe)->up_dev->ud_bus)
#define SLHCI_XFER2SC(xfer) SLHCI_BUS2SC((xfer)->ux_bus)
#define SLHCI_PIPE2SPIPE(pipe) ((struct slhci_pipe *)(pipe))
#define SLHCI_XFER2SPIPE(xfer) SLHCI_PIPE2SPIPE((xfer)->ux_pipe)
#define SLHCI_XFER_TYPE(x) (SLHCI_XFER2SPIPE(xfer)->ptype)
#ifdef SLHCI_PROFILE_TRANSFER
#if defined(__mips__)
typedef uint32_t cc_type;
#define CC_TYPE_FMT "%u"
#define slhci_cc_set(x) __asm volatile ("mfc0 %[cc], $9\n\tnop\n\tnop\n\tnop" \
: [cc] "=r"(x))
#elif defined(__i386__)
typedef uint64_t cc_type;
#define CC_TYPE_FMT "%llu"
#define slhci_cc_set(x) __asm volatile ("rdtsc" : "=A"(x))
#else
#error "SLHCI_PROFILE_TRANSFER not implemented on this MACHINE_ARCH (see sys/dev/ic/sl811hs.c)"
#endif
struct slhci_cc_time {
cc_type start;
cc_type stop;
unsigned int miscdata;
};
#ifndef SLHCI_N_TIMES
#define SLHCI_N_TIMES 200
#endif
struct slhci_cc_times {
struct slhci_cc_time times[SLHCI_N_TIMES];
int current;
int wraparound;
};
static struct slhci_cc_times t_ab[2];
static struct slhci_cc_times t_abdone;
static struct slhci_cc_times t_copy_to_dev;
static struct slhci_cc_times t_copy_from_dev;
static struct slhci_cc_times t_intr;
static struct slhci_cc_times t_lock;
static struct slhci_cc_times t_delay;
static struct slhci_cc_times t_hard_int;
static struct slhci_cc_times t_callback;
static inline void
start_cc_time(struct slhci_cc_times *times, unsigned int misc) {
times->times[times->current].miscdata = misc;
slhci_cc_set(times->times[times->current].start);
}
static inline void
stop_cc_time(struct slhci_cc_times *times) {
slhci_cc_set(times->times[times->current].stop);
if (++times->current >= SLHCI_N_TIMES) {
times->current = 0;
times->wraparound = 1;
}
}
void slhci_dump_cc_times(int);
void
slhci_dump_cc_times(int n) {
struct slhci_cc_times *times;
int i;
switch (n) {
default:
case 0:
printf("USBA start transfer to intr:\n");
times = &t_ab[A];
break;
case 1:
printf("USBB start transfer to intr:\n");
times = &t_ab[B];
break;
case 2:
printf("abdone:\n");
times = &t_abdone;
break;
case 3:
printf("copy to device:\n");
times = &t_copy_to_dev;
break;
case 4:
printf("copy from device:\n");
times = &t_copy_from_dev;
break;
case 5:
printf("intr to intr:\n");
times = &t_intr;
break;
case 6:
printf("lock to release:\n");
times = &t_lock;
break;
case 7:
printf("delay time:\n");
times = &t_delay;
break;
case 8:
printf("hard interrupt enter to exit:\n");
times = &t_hard_int;
break;
case 9:
printf("callback:\n");
times = &t_callback;
break;
}
if (times->wraparound)
for (i = times->current + 1; i < SLHCI_N_TIMES; i++)
printf("start " CC_TYPE_FMT " stop " CC_TYPE_FMT
" difference %8i miscdata %#x\n",
times->times[i].start, times->times[i].stop,
(int)(times->times[i].stop -
times->times[i].start), times->times[i].miscdata);
for (i = 0; i < times->current; i++)
printf("start " CC_TYPE_FMT " stop " CC_TYPE_FMT
" difference %8i miscdata %#x\n", times->times[i].start,
times->times[i].stop, (int)(times->times[i].stop -
times->times[i].start), times->times[i].miscdata);
}
#else
#define start_cc_time(x, y)
#define stop_cc_time(x)
#endif
typedef usbd_status (*LockCallFunc)(struct slhci_softc *, struct slhci_pipe
*, struct usbd_xfer *);
struct usbd_xfer * slhci_allocx(struct usbd_bus *, unsigned int);
void slhci_freex(struct usbd_bus *, struct usbd_xfer *);
static void slhci_get_lock(struct usbd_bus *, kmutex_t **);
usbd_status slhci_transfer(struct usbd_xfer *);
usbd_status slhci_start(struct usbd_xfer *);
usbd_status slhci_root_start(struct usbd_xfer *);
usbd_status slhci_open(struct usbd_pipe *);
static int slhci_roothub_ctrl(struct usbd_bus *, usb_device_request_t *,
void *, int);
void slhci_abort(struct usbd_xfer *);
void slhci_close(struct usbd_pipe *);
void slhci_clear_toggle(struct usbd_pipe *);
void slhci_poll(struct usbd_bus *);
void slhci_done(struct usbd_xfer *);
void slhci_void(void *);
#ifdef SLHCI_MEM_ACCOUNTING
void slhci_mem_use(struct usbd_bus *, int);
#endif
void slhci_reset_entry(void *);
usbd_status slhci_lock_call(struct slhci_softc *, LockCallFunc,
struct slhci_pipe *, struct usbd_xfer *);
void slhci_start_entry(struct slhci_softc *, struct slhci_pipe *);
void slhci_callback_entry(void *arg);
void slhci_do_callback(struct slhci_softc *, struct usbd_xfer *);
void slhci_main(struct slhci_softc *);
static void slhci_write(struct slhci_softc *, uint8_t, uint8_t);
static uint8_t slhci_read(struct slhci_softc *, uint8_t);
static void slhci_write_multi(struct slhci_softc *, uint8_t, uint8_t *, int);
static void slhci_read_multi(struct slhci_softc *, uint8_t, uint8_t *, int);
static void slhci_waitintr(struct slhci_softc *, int);
static int slhci_dointr(struct slhci_softc *);
static void slhci_abdone(struct slhci_softc *, int);
static void slhci_tstart(struct slhci_softc *);
static void slhci_dotransfer(struct slhci_softc *);
static void slhci_callback(struct slhci_softc *);
static void slhci_enter_xfer(struct slhci_softc *, struct slhci_pipe *);
static void slhci_enter_xfers(struct slhci_softc *);
static void slhci_queue_timed(struct slhci_softc *, struct slhci_pipe *);
static void slhci_xfer_timer(struct slhci_softc *, struct slhci_pipe *);
static void slhci_callback_schedule(struct slhci_softc *);
static void slhci_do_callback_schedule(struct slhci_softc *);
#if 0
void slhci_pollxfer(struct slhci_softc *, struct usbd_xfer *);
#endif
static usbd_status slhci_do_poll(struct slhci_softc *, struct slhci_pipe *,
struct usbd_xfer *);
static usbd_status slhci_lsvh_warn(struct slhci_softc *, struct slhci_pipe *,
struct usbd_xfer *);
static usbd_status slhci_isoc_warn(struct slhci_softc *, struct slhci_pipe *,
struct usbd_xfer *);
static usbd_status slhci_open_pipe(struct slhci_softc *, struct slhci_pipe *,
struct usbd_xfer *);
static usbd_status slhci_close_pipe(struct slhci_softc *, struct slhci_pipe *,
struct usbd_xfer *);
static usbd_status slhci_do_abort(struct slhci_softc *, struct slhci_pipe *,
struct usbd_xfer *);
static usbd_status slhci_halt(struct slhci_softc *, struct slhci_pipe *,
struct usbd_xfer *);
static void slhci_intrchange(struct slhci_softc *, uint8_t);
static void slhci_drain(struct slhci_softc *);
static void slhci_reset(struct slhci_softc *);
static int slhci_reserve_bustime(struct slhci_softc *, struct slhci_pipe *,
int);
static void slhci_insert(struct slhci_softc *);
static usbd_status slhci_clear_feature(struct slhci_softc *, unsigned int);
static usbd_status slhci_set_feature(struct slhci_softc *, unsigned int);
static void slhci_get_status(struct slhci_softc *, usb_port_status_t *);
#define SLHCIHIST_FUNC() USBHIST_FUNC()
#define SLHCIHIST_CALLED() USBHIST_CALLED(slhcidebug)
#ifdef SLHCI_DEBUG
static int slhci_memtest(struct slhci_softc *);
void slhci_log_buffer(struct usbd_xfer *);
void slhci_log_req(usb_device_request_t *);
void slhci_log_dumpreg(void);
void slhci_log_xfer(struct usbd_xfer *);
void slhci_log_spipe(struct slhci_pipe *);
void slhci_print_intr(void);
void slhci_log_sc(void);
void slhci_log_slreq(struct slhci_pipe *);
const int SLHCI_D_TRACE = 0x0001;
const int SLHCI_D_MSG = 0x0002;
const int SLHCI_D_XFER = 0x0004;
const int SLHCI_D_MEM = 0x0008;
const int SLHCI_D_INTR = 0x0010;
const int SLHCI_D_SXFER = 0x0020;
const int SLHCI_D_ERR = 0x0080;
const int SLHCI_D_BUF = 0x0100;
const int SLHCI_D_SOFT = 0x0200;
const int SLHCI_D_WAIT = 0x0400;
const int SLHCI_D_ROOT = 0x0800;
const int SLHCI_D_SOF = 0x1000;
const int SLHCI_D_NAK = 0x2000;
int slhcidebug = 0x1cbc;
SYSCTL_SETUP(sysctl_hw_slhci_setup, "sysctl hw.slhci setup")
{
int err;
const struct sysctlnode *rnode;
const struct sysctlnode *cnode;
err = sysctl_createv(clog, 0, NULL, &rnode,
CTLFLAG_PERMANENT, CTLTYPE_NODE, "slhci",
SYSCTL_DESCR("slhci global controls"),
NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
if (err)
goto fail;
err = sysctl_createv(clog, 0, &rnode, &cnode,
CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
"debug", SYSCTL_DESCR("Enable debugging output"),
NULL, 0, &slhcidebug, sizeof(slhcidebug), CTL_CREATE, CTL_EOL);
if (err)
goto fail;
return;
fail:
aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
}
struct slhci_softc *ssc;
#define SLHCI_DEXEC(x, y) do { if ((slhcidebug & SLHCI_ ## x)) { y; } \
} while ( 0)
#define DDOLOG(f, a, b, c, d) do { KERNHIST_LOG(usbhist, f, a, b, c, d); \
} while (0)
#define DLOG(x, f, a, b, c, d) SLHCI_DEXEC(x, DDOLOG(f, a, b, c, d))
static void
DDOLOGBUF(uint8_t *buf, unsigned int length)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
int i;
for(i = 0; i + 8 <= length; i += 8)
DDOLOG("%.4x %.4x %.4x %.4x", (buf[i] << 8) | buf[i+1],
(buf[i+2] << 8) | buf[i+3], (buf[i+4] << 8) | buf[i+5],
(buf[i+6] << 8) | buf[i+7]);
if (length == i + 7)
DDOLOG("%.4x %.4x %.4x %.2x", (buf[i] << 8) | buf[i+1],
(buf[i+2] << 8) | buf[i+3], (buf[i+4] << 8) | buf[i+5],
buf[i+6]);
else if (length == i + 6)
DDOLOG("%.4x %.4x %.4x", (buf[i] << 8) | buf[i+1],
(buf[i+2] << 8) | buf[i+3], (buf[i+4] << 8) | buf[i+5], 0);
else if (length == i + 5)
DDOLOG("%.4x %.4x %.2x", (buf[i] << 8) | buf[i+1],
(buf[i+2] << 8) | buf[i+3], buf[i+4], 0);
else if (length == i + 4)
DDOLOG("%.4x %.4x", (buf[i] << 8) | buf[i+1],
(buf[i+2] << 8) | buf[i+3], 0,0);
else if (length == i + 3)
DDOLOG("%.4x %.2x", (buf[i] << 8) | buf[i+1], buf[i+2], 0,0);
else if (length == i + 2)
DDOLOG("%.4x", (buf[i] << 8) | buf[i+1], 0,0,0);
else if (length == i + 1)
DDOLOG("%.2x", buf[i], 0,0,0);
}
#define DLOGBUF(x, b, l) SLHCI_DEXEC(x, DDOLOGBUF(b, l))
#define DDOLOGCTRL(x) do { \
DDOLOG("CTRL suspend=%jd", !!((x) & SL11_CTRL_SUSPEND), 0, 0, 0); \
DDOLOG("CTRL ls =%jd jk =%jd reset =%jd sof =%jd", \
!!((x) & SL11_CTRL_LOWSPEED), !!((x) & SL11_CTRL_JKSTATE), \
!!((x) & SL11_CTRL_RESETENGINE), !!((x) & SL11_CTRL_ENABLESOF));\
} while (0)
#define DDOLOGISR(r) do { \
DDOLOG("ISR data =%jd det/res=%jd insert =%jd sof =%jd", \
!!((r) & SL11_ISR_DATA), !!((r) & SL11_ISR_RESUME), \
!!((r) & SL11_ISR_INSERT), !!!!((r) & SL11_ISR_SOF)); \
DDOLOG("ISR babble =%jd usbb =%jd usba =%jd", \
!!((r) & SL11_ISR_BABBLE), !!((r) & SL11_ISR_USBB), \
!!((r) & SL11_ISR_USBA), 0); \
} while (0)
#define DDOLOGIER(r) do { \
DDOLOG("IER det/res=%d insert =%d sof =%d", \
!!((r) & SL11_IER_RESUME), \
!!((r) & SL11_IER_INSERT), !!!!((r) & SL11_IER_SOF), 0); \
DDOLOG("IER babble =%d usbb =%d usba =%d", \
!!((r) & SL11_IER_BABBLE), !!((r) & SL11_IER_USBB), \
!!((r) & SL11_IER_USBA), 0); \
} while (0)
#define DDOLOGSTATUS(s) do { \
DDOLOG("STAT stall =%d nak =%d overflow =%d setup =%d", \
!!((s) & SL11_EPSTAT_STALL), !!((s) & SL11_EPSTAT_NAK), \
!!((s) & SL11_EPSTAT_OVERFLOW), !!((s) & SL11_EPSTAT_SETUP)); \
DDOLOG("STAT sequence=%d timeout =%d error =%d ack =%d", \
!!((s) & SL11_EPSTAT_SEQUENCE), !!((s) & SL11_EPSTAT_TIMEOUT), \
!!((s) & SL11_EPSTAT_ERROR), !!((s) & SL11_EPSTAT_ACK)); \
} while (0)
#define DDOLOGEPCTRL(r) do { \
DDOLOG("CTRL preamble=%d toggle =%d sof =%d iso =%d", \
!!((r) & SL11_EPCTRL_PREAMBLE), !!((r) & SL11_EPCTRL_DATATOGGLE),\
!!((r) & SL11_EPCTRL_SOF), !!((r) & SL11_EPCTRL_ISO)); \
DDOLOG("CTRL out =%d enable =%d arm =%d", \
!!((r) & SL11_EPCTRL_DIRECTION), \
!!((r) & SL11_EPCTRL_ENABLE), !!((r) & SL11_EPCTRL_ARM), 0); \
} while (0)
#define DDOLOGEPSTAT(r) do { \
DDOLOG("STAT stall =%d nak =%d overflow =%d setup =%d", \
!!((r) & SL11_EPSTAT_STALL), !!((r) & SL11_EPSTAT_NAK), \
!!((r) & SL11_EPSTAT_OVERFLOW), !!((r) & SL11_EPSTAT_SETUP)); \
DDOLOG("STAT sequence=%d timeout =%d error =%d ack =%d", \
!!((r) & SL11_EPSTAT_SEQUENCE), !!((r) & SL11_EPSTAT_TIMEOUT), \
!!((r) & SL11_EPSTAT_ERROR), !!((r) & SL11_EPSTAT_ACK)); \
} while (0)
#else
#define slhcidebug 0
#define slhci_log_spipe(spipe) ((void)0)
#define slhci_log_xfer(xfer) ((void)0)
#define SLHCI_DEXEC(x, y) ((void)0)
#define DDOLOG(f, a, b, c, d) ((void)0)
#define DLOG(x, f, a, b, c, d) ((void)0)
#define DDOLOGBUF(b, l) ((void)0)
#define DLOGBUF(x, b, l) ((void)0)
#define DDOLOGCTRL(x) ((void)0)
#define DDOLOGISR(r) ((void)0)
#define DDOLOGIER(r) ((void)0)
#define DDOLOGSTATUS(s) ((void)0)
#define DDOLOGEPCTRL(r) ((void)0)
#define DDOLOGEPSTAT(r) ((void)0)
#endif
#ifdef DIAGNOSTIC
#define LK_SLASSERT(exp, sc, spipe, xfer, ext) do { \
if (!(exp)) { \
printf("%s: assertion %s failed line %u function %s!" \
" halted\n", SC_NAME(sc), #exp, __LINE__, __func__);\
slhci_halt(sc, spipe, xfer); \
ext; \
} \
} while (0)
#define UL_SLASSERT(exp, sc, spipe, xfer, ext) do { \
if (!(exp)) { \
printf("%s: assertion %s failed line %u function %s!" \
" halted\n", SC_NAME(sc), #exp, __LINE__, __func__); \
slhci_lock_call(sc, &slhci_halt, spipe, xfer); \
ext; \
} \
} while (0)
#else
#define LK_SLASSERT(exp, sc, spipe, xfer, ext) ((void)0)
#define UL_SLASSERT(exp, sc, spipe, xfer, ext) ((void)0)
#endif
const struct usbd_bus_methods slhci_bus_methods = {
.ubm_open = slhci_open,
.ubm_softint = slhci_void,
.ubm_dopoll = slhci_poll,
.ubm_allocx = slhci_allocx,
.ubm_freex = slhci_freex,
.ubm_getlock = slhci_get_lock,
.ubm_rhctrl = slhci_roothub_ctrl,
};
const struct usbd_pipe_methods slhci_pipe_methods = {
.upm_transfer = slhci_transfer,
.upm_start = slhci_start,
.upm_abort = slhci_abort,
.upm_close = slhci_close,
.upm_cleartoggle = slhci_clear_toggle,
.upm_done = slhci_done,
};
const struct usbd_pipe_methods slhci_root_methods = {
.upm_transfer = slhci_transfer,
.upm_start = slhci_root_start,
.upm_abort = slhci_abort,
.upm_close = (void (*)(struct usbd_pipe *))slhci_void,
.upm_cleartoggle = slhci_clear_toggle,
.upm_done = slhci_done,
};
#define GOT_FIRST_TO(tvar, t) \
GCQ_GOT_FIRST_TYPED(tvar, &(t)->to, struct slhci_pipe, to)
#define FIND_TO(var, t, tvar, cond) \
GCQ_FIND_TYPED(var, &(t)->to, tvar, struct slhci_pipe, to, cond)
#define FOREACH_AP(var, t, tvar) \
GCQ_FOREACH_TYPED(var, &(t)->ap, tvar, struct slhci_pipe, ap)
#define GOT_FIRST_TIMED_COND(tvar, t, cond) \
GCQ_GOT_FIRST_COND_TYPED(tvar, &(t)->timed, struct slhci_pipe, xq, cond)
#define GOT_FIRST_CB(tvar, t) \
GCQ_GOT_FIRST_TYPED(tvar, &(t)->q[Q_CB], struct slhci_pipe, xq)
#define DEQUEUED_CALLBACK(tvar, t) \
GCQ_DEQUEUED_FIRST_TYPED(tvar, &(t)->q[Q_CALLBACKS], struct slhci_pipe, xq)
#define FIND_TIMED(var, t, tvar, cond) \
GCQ_FIND_TYPED(var, &(t)->timed, tvar, struct slhci_pipe, xq, cond)
#define DEQUEUED_WAITQ(tvar, sc) \
GCQ_DEQUEUED_FIRST_TYPED(tvar, &(sc)->sc_waitq, struct slhci_pipe, xq)
static inline void
enter_waitq(struct slhci_softc *sc, struct slhci_pipe *spipe)
{
gcq_insert_tail(&sc->sc_waitq, &spipe->xq);
}
static inline void
enter_q(struct slhci_transfers *t, struct slhci_pipe *spipe, int i)
{
gcq_insert_tail(&t->q[i], &spipe->xq);
}
static inline void
enter_callback(struct slhci_transfers *t, struct slhci_pipe *spipe)
{
gcq_insert_tail(&t->q[Q_CALLBACKS], &spipe->xq);
}
static inline void
enter_all_pipes(struct slhci_transfers *t, struct slhci_pipe *spipe)
{
gcq_insert_tail(&t->ap, &spipe->ap);
}
struct usbd_xfer *
slhci_allocx(struct usbd_bus *bus, unsigned int nframes)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct usbd_xfer *xfer;
xfer = kmem_zalloc(sizeof(*xfer), KM_SLEEP);
DLOG(D_MEM, "allocx %#jx", (uintptr_t)xfer, 0,0,0);
#ifdef SLHCI_MEM_ACCOUNTING
slhci_mem_use(bus, 1);
#endif
#ifdef DIAGNOSTIC
if (xfer != NULL)
xfer->ux_state = XFER_BUSY;
#endif
return xfer;
}
void
slhci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
DLOG(D_MEM, "freex xfer %#jx spipe %#jx",
(uintptr_t)xfer, (uintptr_t)xfer->ux_pipe,0,0);
#ifdef SLHCI_MEM_ACCOUNTING
slhci_mem_use(bus, -1);
#endif
#ifdef DIAGNOSTIC
if (xfer->ux_state != XFER_BUSY &&
xfer->ux_status != USBD_NOT_STARTED) {
struct slhci_softc *sc = SLHCI_BUS2SC(bus);
printf("%s: slhci_freex: xfer=%p not busy, %#08x halted\n",
SC_NAME(sc), xfer, xfer->ux_state);
DDOLOG("xfer=%p not busy, %#08x halted\n", xfer,
xfer->ux_state, 0, 0);
slhci_lock_call(sc, &slhci_halt, NULL, NULL);
return;
}
xfer->ux_state = XFER_FREE;
#endif
kmem_free(xfer, sizeof(*xfer));
}
static void
slhci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
{
struct slhci_softc *sc = SLHCI_BUS2SC(bus);
*lock = &sc->sc_lock;
}
usbd_status
slhci_transfer(struct usbd_xfer *xfer)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
usbd_status error;
DLOG(D_TRACE, "transfer type %jd xfer %#jx spipe %#jx ",
SLHCI_XFER_TYPE(xfer), (uintptr_t)xfer, (uintptr_t)xfer->ux_pipe,
0);
error = xfer->ux_pipe->up_methods->upm_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
return error;
}
usbd_status
slhci_start(struct usbd_xfer *xfer)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_softc *sc = SLHCI_XFER2SC(xfer);
struct usbd_pipe *pipe = xfer->ux_pipe;
struct slhci_pipe *spipe = SLHCI_PIPE2SPIPE(pipe);
struct slhci_transfers *t = &sc->sc_transfers;
usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
unsigned int max_packet;
KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
max_packet = UGETW(ed->wMaxPacketSize);
DLOG(D_TRACE, "transfer type %jd start xfer %#jx spipe %#jx length %jd",
spipe->ptype, (uintptr_t)xfer, (uintptr_t)spipe, xfer->ux_length);
KASSERT(spipe->xfer == NULL);
xfer->ux_actlen = 0;
xfer->ux_status = USBD_IN_PROGRESS;
spipe->xfer = xfer;
spipe->nerrs = 0;
spipe->frame = t->frame;
spipe->control = SL11_EPCTRL_ARM_ENABLE;
spipe->tregs[DEV] = pipe->up_dev->ud_addr;
spipe->tregs[PID] = spipe->newpid = UE_GET_ADDR(ed->bEndpointAddress)
| (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? SL11_PID_IN :
SL11_PID_OUT);
spipe->newlen[0] = xfer->ux_length % max_packet;
spipe->newlen[1] = uimin(xfer->ux_length, max_packet);
if (spipe->ptype == PT_BULK || spipe->ptype == PT_INTR) {
if (spipe->pflags & PF_TOGGLE)
spipe->control |= SL11_EPCTRL_DATATOGGLE;
spipe->tregs[LEN] = spipe->newlen[1];
if (spipe->tregs[LEN])
spipe->buffer = xfer->ux_buf;
else
spipe->buffer = NULL;
spipe->lastframe = t->frame;
if (spipe->ptype == PT_INTR) {
spipe->frame = spipe->lastframe +
spipe->pipe.up_interval;
}
#if defined(DEBUG) || defined(SLHCI_DEBUG)
if (__predict_false(spipe->ptype == PT_INTR &&
xfer->ux_length > spipe->tregs[LEN])) {
printf("%s: Long INTR transfer not supported!\n",
SC_NAME(sc));
DDOLOG("Long INTR transfer not supported!", 0, 0, 0, 0);
xfer->ux_status = USBD_INVAL;
}
#endif
} else {
SLHCI_DEXEC(D_TRACE, slhci_log_xfer(xfer));
spipe->tregs[PID] |= SL11_PID_SETUP;
spipe->tregs[LEN] = 8;
spipe->buffer = (uint8_t *)&xfer->ux_request;
DLOGBUF(D_XFER, spipe->buffer, spipe->tregs[LEN]);
spipe->ptype = PT_CTRL_SETUP;
spipe->newpid &= ~SL11_PID_BITS;
if (xfer->ux_length == 0 ||
(xfer->ux_request.bmRequestType & UT_READ))
spipe->newpid |= SL11_PID_IN;
else
spipe->newpid |= SL11_PID_OUT;
}
if (xfer->ux_flags & USBD_FORCE_SHORT_XFER &&
spipe->tregs[LEN] == max_packet &&
(spipe->newpid & SL11_PID_BITS) == SL11_PID_OUT)
spipe->wantshort = 1;
else
spipe->wantshort = 0;
if (spipe->pflags & PF_LS) {
if (spipe->pflags & PF_PREAMBLE)
spipe->control |= SL11_EPCTRL_PREAMBLE;
if (max_packet <= 8) {
spipe->bustime = SLHCI_LS_CONST +
SLHCI_LS_DATA_TIME(spipe->tregs[LEN]);
spipe->newbustime[0] = SLHCI_LS_CONST +
SLHCI_LS_DATA_TIME(spipe->newlen[0]);
spipe->newbustime[1] = SLHCI_LS_CONST +
SLHCI_LS_DATA_TIME(spipe->newlen[1]);
} else
xfer->ux_status = USBD_INVAL;
} else {
UL_SLASSERT(pipe->up_dev->ud_speed == USB_SPEED_FULL, sc,
spipe, xfer, return USBD_IN_PROGRESS);
if (max_packet <= SL11_MAX_PACKET_SIZE) {
spipe->bustime = SLHCI_FS_CONST +
SLHCI_FS_DATA_TIME(spipe->tregs[LEN]);
spipe->newbustime[0] = SLHCI_FS_CONST +
SLHCI_FS_DATA_TIME(spipe->newlen[0]);
spipe->newbustime[1] = SLHCI_FS_CONST +
SLHCI_FS_DATA_TIME(spipe->newlen[1]);
} else
xfer->ux_status = USBD_INVAL;
}
if ((spipe->tregs[PID] & SL11_PID_BITS) != SL11_PID_IN)
spipe->control |= SL11_EPCTRL_DIRECTION;
slhci_start_entry(sc, spipe);
return USBD_IN_PROGRESS;
}
usbd_status
slhci_root_start(struct usbd_xfer *xfer)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_softc *sc;
struct slhci_pipe *spipe __diagused;
spipe = SLHCI_PIPE2SPIPE(xfer->ux_pipe);
sc = SLHCI_XFER2SC(xfer);
struct slhci_transfers *t = &sc->sc_transfers;
LK_SLASSERT(spipe != NULL && xfer != NULL, sc, spipe, xfer, return
USBD_CANCELLED);
DLOG(D_TRACE, "transfer type %jd start",
SLHCI_XFER_TYPE(xfer), 0, 0, 0);
KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
KASSERT(spipe->ptype == PT_ROOT_INTR);
KASSERT(t->rootintr == NULL);
t->rootintr = xfer;
xfer->ux_status = USBD_IN_PROGRESS;
return USBD_IN_PROGRESS;
}
usbd_status
slhci_open(struct usbd_pipe *pipe)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct usbd_device *dev;
struct slhci_softc *sc;
struct slhci_pipe *spipe;
usb_endpoint_descriptor_t *ed;
unsigned int max_packet, pmaxpkt;
uint8_t rhaddr;
dev = pipe->up_dev;
sc = SLHCI_PIPE2SC(pipe);
spipe = SLHCI_PIPE2SPIPE(pipe);
ed = pipe->up_endpoint->ue_edesc;
rhaddr = dev->ud_bus->ub_rhaddr;
DLOG(D_TRACE, "slhci_open(addr=%jd,ep=%jd,rootaddr=%jd)",
dev->ud_addr, ed->bEndpointAddress, rhaddr, 0);
spipe->pflags = 0;
spipe->frame = 0;
spipe->lastframe = 0;
spipe->xfer = NULL;
spipe->buffer = NULL;
gcq_init(&spipe->ap);
gcq_init(&spipe->to);
gcq_init(&spipe->xq);
max_packet = UGETW(ed->wMaxPacketSize);
if (dev->ud_speed == USB_SPEED_LOW) {
spipe->pflags |= PF_LS;
if (dev->ud_myhub->ud_addr != rhaddr) {
spipe->pflags |= PF_PREAMBLE;
if (!slhci_try_lsvh)
return slhci_lock_call(sc, &slhci_lsvh_warn,
spipe, NULL);
}
pmaxpkt = 8;
} else
pmaxpkt = SL11_MAX_PACKET_SIZE;
if (max_packet > pmaxpkt) {
DLOG(D_ERR, "packet too large! size %jd spipe %#jx", max_packet,
(uintptr_t)spipe, 0,0);
return USBD_INVAL;
}
if (dev->ud_addr == rhaddr) {
switch (ed->bEndpointAddress) {
case USB_CONTROL_ENDPOINT:
spipe->ptype = PT_ROOT_CTRL;
pipe->up_interval = 0;
pipe->up_methods = &roothub_ctrl_methods;
break;
case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
spipe->ptype = PT_ROOT_INTR;
pipe->up_interval = 1;
pipe->up_methods = &slhci_root_methods;
break;
default:
printf("%s: Invalid root endpoint!\n", SC_NAME(sc));
DDOLOG("Invalid root endpoint", 0, 0, 0, 0);
return USBD_INVAL;
}
return USBD_NORMAL_COMPLETION;
} else {
switch (ed->bmAttributes & UE_XFERTYPE) {
case UE_CONTROL:
spipe->ptype = PT_CTRL_SETUP;
pipe->up_interval = 0;
break;
case UE_INTERRUPT:
spipe->ptype = PT_INTR;
if (pipe->up_interval == USBD_DEFAULT_INTERVAL)
pipe->up_interval = ed->bInterval;
break;
case UE_ISOCHRONOUS:
return slhci_lock_call(sc, &slhci_isoc_warn, spipe,
NULL);
case UE_BULK:
spipe->ptype = PT_BULK;
pipe->up_interval = 0;
break;
}
DLOG(D_MSG, "open pipe type %jd interval %jd", spipe->ptype,
pipe->up_interval, 0,0);
pipe->up_methods = __UNCONST(&slhci_pipe_methods);
return slhci_lock_call(sc, &slhci_open_pipe, spipe, NULL);
}
}
int
slhci_supported_rev(uint8_t rev)
{
return rev >= SLTYPE_SL811HS_R12 && rev <= SLTYPE_SL811HS_R15;
}
void
slhci_preinit(struct slhci_softc *sc, PowerFunc pow, bus_space_tag_t iot,
bus_space_handle_t ioh, uint16_t max_current, uint32_t stride)
{
struct slhci_transfers *t;
int i;
t = &sc->sc_transfers;
#ifdef SLHCI_DEBUG
ssc = sc;
#endif
mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
t->flags = F_NODEV|F_UDISABLED;
t->pend = INT_MAX;
KASSERT(slhci_wait_time != INT_MAX);
t->len[0] = t->len[1] = -1;
if (max_current > 500)
max_current = 500;
t->max_current = (uint8_t)(max_current / 2);
sc->sc_enable_power = pow;
sc->sc_iot = iot;
sc->sc_ioh = ioh;
sc->sc_stride = stride;
KASSERT(Q_MAX+1 == sizeof(t->q) / sizeof(t->q[0]));
for (i = 0; i <= Q_MAX; i++)
gcq_init_head(&t->q[i]);
gcq_init_head(&t->timed);
gcq_init_head(&t->to);
gcq_init_head(&t->ap);
gcq_init_head(&sc->sc_waitq);
}
int
slhci_attach(struct slhci_softc *sc)
{
struct slhci_transfers *t;
const char *rev;
t = &sc->sc_transfers;
t->sltype = SL11_GET_REV(slhci_read(sc, SL11_REV));
if (!slhci_supported_rev(t->sltype)) {
if (t->sltype == SLTYPE_SL11H)
printf("%s: SL11H unsupported or bus error!\n",
SC_NAME(sc));
else
printf("%s: Unknown chip revision!\n", SC_NAME(sc));
return -1;
}
#ifdef SLHCI_DEBUG
if (slhci_memtest(sc)) {
printf("%s: memory/bus error!\n", SC_NAME(sc));
return -1;
}
#endif
callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
callout_setfunc(&sc->sc_timer, slhci_reset_entry, sc);
sc->sc_cb_softintr = softint_establish(SOFTINT_NET,
slhci_callback_entry, sc);
if (t->sltype == SLTYPE_SL811HS_R12)
rev = "(rev 1.2)";
else if (t->sltype == SLTYPE_SL811HS_R14)
rev = "(rev 1.4 or 1.5)";
else
rev = "(unknown revision)";
aprint_normal("%s: ScanLogic SL811HS/T USB Host Controller %s\n",
SC_NAME(sc), rev);
aprint_normal("%s: Max Current %u mA (value by code, not by probe)\n",
SC_NAME(sc), t->max_current * 2);
#if defined(SLHCI_DEBUG) || defined(SLHCI_NO_OVERTIME) || \
defined(SLHCI_TRY_LSVH) || defined(SLHCI_PROFILE_TRANSFER)
aprint_normal("%s: driver options:"
#ifdef SLHCI_DEBUG
" SLHCI_DEBUG"
#endif
#ifdef SLHCI_TRY_LSVH
" SLHCI_TRY_LSVH"
#endif
#ifdef SLHCI_NO_OVERTIME
" SLHCI_NO_OVERTIME"
#endif
#ifdef SLHCI_PROFILE_TRANSFER
" SLHCI_PROFILE_TRANSFER"
#endif
"\n", SC_NAME(sc));
#endif
sc->sc_bus.ub_revision = USBREV_1_1;
sc->sc_bus.ub_methods = __UNCONST(&slhci_bus_methods);
sc->sc_bus.ub_pipesize = sizeof(struct slhci_pipe);
sc->sc_bus.ub_usedma = false;
if (!sc->sc_enable_power)
t->flags |= F_REALPOWER;
t->flags |= F_ACTIVE;
sc->sc_child = config_found(SC_DEV(sc), &sc->sc_bus, usbctlprint,
CFARGS_NONE);
if (!sc->sc_child)
return -1;
else
return 0;
}
int
slhci_detach(struct slhci_softc *sc, int flags)
{
struct slhci_transfers *t;
int ret;
t = &sc->sc_transfers;
KASSERT(!(t->flags & F_ACTIVE));
while (t->flags & (F_RESET|F_CALLBACK))
tsleep(&sc, PPAUSE, "slhci_detach", hz);
softint_disestablish(sc->sc_cb_softintr);
mutex_destroy(&sc->sc_lock);
mutex_destroy(&sc->sc_intr_lock);
ret = 0;
if (sc->sc_child)
ret = config_detach(sc->sc_child, flags);
#ifdef SLHCI_MEM_ACCOUNTING
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
if (sc->sc_mem_use) {
printf("%s: Memory still in use after detach! mem_use (count)"
" = %d\n", SC_NAME(sc), sc->sc_mem_use);
DDOLOG("Memory still in use after detach! mem_use (count)"
" = %d", sc->sc_mem_use, 0, 0, 0);
}
#endif
return ret;
}
int
slhci_activate(device_t self, enum devact act)
{
struct slhci_softc *sc = device_private(self);
switch (act) {
case DVACT_DEACTIVATE:
slhci_lock_call(sc, &slhci_halt, NULL, NULL);
return 0;
default:
return EOPNOTSUPP;
}
}
void
slhci_abort(struct usbd_xfer *xfer)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_softc *sc;
struct slhci_pipe *spipe;
spipe = SLHCI_PIPE2SPIPE(xfer->ux_pipe);
if (spipe == NULL)
goto callback;
sc = SLHCI_XFER2SC(xfer);
KASSERT(mutex_owned(&sc->sc_lock));
DLOG(D_TRACE, "transfer type %jd abort xfer %#jx spipe %#jx "
" spipe->xfer %#jx", spipe->ptype, (uintptr_t)xfer,
(uintptr_t)spipe, (uintptr_t)spipe->xfer);
slhci_lock_call(sc, &slhci_do_abort, spipe, xfer);
callback:
xfer->ux_status = USBD_CANCELLED;
usb_transfer_complete(xfer);
}
void
slhci_close(struct usbd_pipe *pipe)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_softc *sc;
struct slhci_pipe *spipe;
sc = SLHCI_PIPE2SC(pipe);
spipe = SLHCI_PIPE2SPIPE(pipe);
DLOG(D_TRACE, "transfer type %jd close spipe %#jx spipe->xfer %#jx",
spipe->ptype, (uintptr_t)spipe, (uintptr_t)spipe->xfer, 0);
slhci_lock_call(sc, &slhci_close_pipe, spipe, NULL);
}
void
slhci_clear_toggle(struct usbd_pipe *pipe)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_pipe *spipe;
spipe = SLHCI_PIPE2SPIPE(pipe);
DLOG(D_TRACE, "transfer type %jd toggle spipe %#jx", spipe->ptype,
(uintptr_t)spipe, 0, 0);
spipe->pflags &= ~PF_TOGGLE;
#ifdef DIAGNOSTIC
if (spipe->xfer != NULL) {
struct slhci_softc *sc = (struct slhci_softc
*)pipe->up_dev->ud_bus;
printf("%s: Clear toggle on transfer in progress! halted\n",
SC_NAME(sc));
DDOLOG("Clear toggle on transfer in progress! halted",
0, 0, 0, 0);
slhci_halt(sc, NULL, NULL);
}
#endif
}
void
slhci_poll(struct usbd_bus *bus)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_softc *sc;
sc = SLHCI_BUS2SC(bus);
DLOG(D_TRACE, "slhci_poll", 0,0,0,0);
slhci_lock_call(sc, &slhci_do_poll, NULL, NULL);
}
void
slhci_done(struct usbd_xfer *xfer)
{
}
void
slhci_void(void *v) {}
#ifdef SLHCI_MEM_ACCOUNTING
void
slhci_mem_use(struct usbd_bus *bus, int val)
{
struct slhci_softc *sc = SLHCI_BUS2SC(bus);
mutex_enter(&sc->sc_intr_lock);
sc->sc_mem_use += val;
mutex_exit(&sc->sc_intr_lock);
}
#endif
void
slhci_reset_entry(void *arg)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_softc *sc = arg;
mutex_enter(&sc->sc_intr_lock);
slhci_reset(sc);
slhci_callback_schedule(sc);
mutex_exit(&sc->sc_intr_lock);
}
usbd_status
slhci_lock_call(struct slhci_softc *sc, LockCallFunc lcf, struct slhci_pipe
*spipe, struct usbd_xfer *xfer)
{
usbd_status ret;
mutex_enter(&sc->sc_intr_lock);
ret = (*lcf)(sc, spipe, xfer);
slhci_main(sc);
mutex_exit(&sc->sc_intr_lock);
return ret;
}
void
slhci_start_entry(struct slhci_softc *sc, struct slhci_pipe *spipe)
{
struct slhci_transfers *t;
mutex_enter(&sc->sc_intr_lock);
t = &sc->sc_transfers;
if (!(t->flags & (F_AINPROG|F_BINPROG))) {
slhci_enter_xfer(sc, spipe);
slhci_dotransfer(sc);
slhci_main(sc);
} else {
enter_waitq(sc, spipe);
}
mutex_exit(&sc->sc_intr_lock);
}
void
slhci_callback_entry(void *arg)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_softc *sc;
struct slhci_transfers *t;
sc = (struct slhci_softc *)arg;
mutex_enter(&sc->sc_intr_lock);
t = &sc->sc_transfers;
DLOG(D_SOFT, "callback_entry flags %#jx", t->flags, 0,0,0);
repeat:
slhci_callback(sc);
if (!gcq_empty(&sc->sc_waitq)) {
slhci_enter_xfers(sc);
slhci_dotransfer(sc);
slhci_waitintr(sc, 0);
goto repeat;
}
t->flags &= ~F_CALLBACK;
mutex_exit(&sc->sc_intr_lock);
}
void
slhci_do_callback(struct slhci_softc *sc, struct usbd_xfer *xfer)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
KASSERT(mutex_owned(&sc->sc_intr_lock));
start_cc_time(&t_callback, (u_int)xfer);
mutex_exit(&sc->sc_intr_lock);
mutex_enter(&sc->sc_lock);
usb_transfer_complete(xfer);
mutex_exit(&sc->sc_lock);
mutex_enter(&sc->sc_intr_lock);
stop_cc_time(&t_callback);
}
int
slhci_intr(void *arg)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_softc *sc = arg;
int ret = 0;
int irq;
start_cc_time(&t_hard_int, (unsigned int)arg);
mutex_enter(&sc->sc_intr_lock);
do {
irq = slhci_dointr(sc);
ret |= irq;
slhci_main(sc);
} while (irq);
mutex_exit(&sc->sc_intr_lock);
stop_cc_time(&t_hard_int);
return ret;
}
void
slhci_main(struct slhci_softc *sc)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
waitcheck:
slhci_waitintr(sc, slhci_wait_time);
if ((t->flags & F_ROOTINTR) || !gcq_empty(&t->q[Q_CALLBACKS])) {
if (__predict_false(sc->sc_bus.ub_usepolling ||
t->flags & F_DISABLED))
slhci_callback(sc);
else
slhci_callback_schedule(sc);
}
if (!gcq_empty(&sc->sc_waitq)) {
slhci_enter_xfers(sc);
slhci_dotransfer(sc);
goto waitcheck;
}
DLOG(D_INTR, "... done", 0, 0, 0, 0);
}
#ifdef SLHCI_BUS_SPACE_BARRIERS
#define BSB(a, b, c, d, e) bus_space_barrier(a, b, c, d, BUS_SPACE_BARRIER_ # e)
#define BSB_SYNC(a, b, c, d) bus_space_barrier(a, b, c, d, BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
#else
#define BSB(a, b, c, d, e) __USE(d)
#define BSB_SYNC(a, b, c, d)
#endif
static void
slhci_write(struct slhci_softc *sc, uint8_t addr, uint8_t data)
{
bus_size_t paddr, pdata, pst, psz;
bus_space_tag_t iot;
bus_space_handle_t ioh;
paddr = pst = 0;
pdata = sc->sc_stride;
psz = pdata * 2;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
bus_space_write_1(iot, ioh, paddr, addr);
BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
bus_space_write_1(iot, ioh, pdata, data);
BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
}
static uint8_t
slhci_read(struct slhci_softc *sc, uint8_t addr)
{
bus_size_t paddr, pdata, pst, psz;
bus_space_tag_t iot;
bus_space_handle_t ioh;
uint8_t data;
paddr = pst = 0;
pdata = sc->sc_stride;
psz = pdata * 2;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
bus_space_write_1(iot, ioh, paddr, addr);
BSB(iot, ioh, pst, psz, WRITE_BEFORE_READ);
data = bus_space_read_1(iot, ioh, pdata);
BSB(iot, ioh, pst, psz, READ_BEFORE_WRITE);
return data;
}
#if 0
static void
slhci_write_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
{
bus_size_t paddr, pdata, pst, psz;
bus_space_tag_t iot;
bus_space_handle_t ioh;
paddr = pst = 0;
pdata = sc->sc_stride;
psz = pdata * 2;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
bus_space_write_1(iot, ioh, paddr, addr);
BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
bus_space_write_multi_1(iot, ioh, pdata, buf, l);
BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
}
static void
slhci_read_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
{
bus_size_t paddr, pdata, pst, psz;
bus_space_tag_t iot;
bus_space_handle_t ioh;
paddr = pst = 0;
pdata = sc->sc_stride;
psz = pdata * 2;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
bus_space_write_1(iot, ioh, paddr, addr);
BSB(iot, ioh, pst, psz, WRITE_BEFORE_READ);
bus_space_read_multi_1(iot, ioh, pdata, buf, l);
BSB(iot, ioh, pst, psz, READ_BEFORE_WRITE);
}
#else
static void
slhci_write_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
{
#if 1
for (; l; addr++, buf++, l--)
slhci_write(sc, addr, *buf);
#else
bus_size_t paddr, pdata, pst, psz;
bus_space_tag_t iot;
bus_space_handle_t ioh;
paddr = pst = 0;
pdata = sc->sc_stride;
psz = pdata * 2;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
for (; l; addr++, buf++, l--) {
bus_space_write_1(iot, ioh, paddr, addr);
BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
bus_space_write_1(iot, ioh, pdata, *buf);
BSB(iot, ioh, pst, psz, WRITE_BEFORE_WRITE);
}
#endif
}
static void
slhci_read_multi(struct slhci_softc *sc, uint8_t addr, uint8_t *buf, int l)
{
#if 1
for (; l; addr++, buf++, l--)
*buf = slhci_read(sc, addr);
#else
bus_size_t paddr, pdata, pst, psz;
bus_space_tag_t iot;
bus_space_handle_t ioh;
paddr = pst = 0;
pdata = sc->sc_stride;
psz = pdata * 2;
iot = sc->sc_iot;
ioh = sc->sc_ioh;
for (; l; addr++, buf++, l--) {
bus_space_write_1(iot, ioh, paddr, addr);
BSB(iot, ioh, pst, psz, WRITE_BEFORE_READ);
*buf = bus_space_read_1(iot, ioh, pdata);
BSB(iot, ioh, pst, psz, READ_BEFORE_WRITE);
}
#endif
}
#endif
static void
slhci_waitintr(struct slhci_softc *sc, int wait_time)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
if (__predict_false(sc->sc_bus.ub_usepolling))
wait_time = 12000;
while (t->pend <= wait_time) {
DLOG(D_WAIT, "waiting... frame %jd pend %jd flags %#jx",
t->frame, t->pend, t->flags, 0);
LK_SLASSERT(t->flags & F_ACTIVE, sc, NULL, NULL, return);
LK_SLASSERT(t->flags & (F_AINPROG|F_BINPROG), sc, NULL, NULL,
return);
slhci_dointr(sc);
}
DLOG(D_WAIT, "... done", 0, 0, 0, 0);
}
static int
slhci_dointr(struct slhci_softc *sc)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
struct slhci_pipe *tosp;
uint8_t r;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
if (sc->sc_ier == 0) {
DLOG(D_INTR, "sc_ier is zero", 0, 0, 0, 0);
return 0;
}
r = slhci_read(sc, SL11_ISR);
#ifdef SLHCI_DEBUG
if (slhcidebug & SLHCI_D_INTR && r & sc->sc_ier &&
((r & ~(SL11_ISR_SOF|SL11_ISR_DATA)) || slhcidebug & SLHCI_D_SOF)) {
uint8_t e, f;
e = slhci_read(sc, SL11_IER);
f = slhci_read(sc, SL11_CTRL);
DDOLOG("Flags=%#x IER=%#x ISR=%#x CTRL=%#x", t->flags, e, r, f);
DDOLOGCTRL(f);
DDOLOGISR(r);
}
#endif
if (__predict_false(sc->sc_ier_check++ > SLHCI_IER_CHECK_FREQUENCY)) {
sc->sc_ier_check = 0;
if (sc->sc_ier != slhci_read(sc, SL11_IER)) {
printf("%s: IER value corrupted! halted\n",
SC_NAME(sc));
DDOLOG("IER value corrupted! halted", 0, 0, 0, 0);
slhci_halt(sc, NULL, NULL);
return 1;
}
}
r &= sc->sc_ier;
if (r == 0) {
DLOG(D_INTR, "r is zero", 0, 0, 0, 0);
return 0;
}
sc->sc_ier_check = 0;
slhci_write(sc, SL11_ISR, r);
BSB_SYNC(sc->iot, sc->ioh, sc->pst, sc->psz);
if (__predict_false(r & SL11_ISR_INSERT)) {
slhci_insert(sc);
DLOG(D_INTR, "... done", 0, 0, 0, 0);
return 1;
}
stop_cc_time(&t_intr);
start_cc_time(&t_intr, r);
if (r & SL11_ISR_SOF) {
t->frame++;
gcq_merge_tail(&t->q[Q_CB], &t->q[Q_NEXT_CB]);
if (__predict_false(t->flags & F_SOFCHECK2 && t->flags &
(F_AINPROG|F_BINPROG))) {
printf("%s: Missed transfer completion. halted\n",
SC_NAME(sc));
DDOLOG("Missed transfer completion. halted", 0, 0, 0,
0);
slhci_halt(sc, NULL, NULL);
return 1;
} else if (t->flags & F_SOFCHECK1) {
t->flags |= F_SOFCHECK2;
} else
t->flags |= F_SOFCHECK1;
if (t->flags & F_CHANGE)
t->flags |= F_ROOTINTR;
while (__predict_true(GOT_FIRST_TO(tosp, t)) &&
__predict_false(tosp->to_frame <= t->frame)) {
tosp->xfer->ux_status = USBD_TIMEOUT;
slhci_do_abort(sc, tosp, tosp->xfer);
enter_callback(t, tosp);
}
slhci_tstart(sc);
}
if (r & (SL11_ISR_USBA|SL11_ISR_USBB)) {
int ab;
if ((r & (SL11_ISR_USBA|SL11_ISR_USBB)) ==
(SL11_ISR_USBA|SL11_ISR_USBB)) {
if (!(t->flags & (F_AINPROG|F_BINPROG)))
return 1;
LK_SLASSERT((t->flags & (F_AINPROG|F_BINPROG)) !=
(F_AINPROG|F_BINPROG), sc, NULL, NULL, return 1);
printf("%s: Transfer reported done but not started! "
"Verify data integrity if not detaching. "
" flags %#x r %x\n", SC_NAME(sc), t->flags, r);
if (!(t->flags & F_AINPROG))
r &= ~SL11_ISR_USBA;
else
r &= ~SL11_ISR_USBB;
}
t->pend = INT_MAX;
if (r & SL11_ISR_USBA)
ab = A;
else
ab = B;
if (!(t->flags & (ab ? F_BINPROG : F_AINPROG))) {
printf("%s: %s done but not in progress! halted\n",
SC_NAME(sc), ab ? "B" : "A");
DDOLOG("AB=%d done but not in progress! halted", ab,
0, 0, 0);
slhci_halt(sc, NULL, NULL);
return 1;
}
t->flags &= ~(ab ? F_BINPROG : F_AINPROG);
slhci_tstart(sc);
stop_cc_time(&t_ab[ab]);
start_cc_time(&t_abdone, t->flags);
slhci_abdone(sc, ab);
stop_cc_time(&t_abdone);
}
slhci_dotransfer(sc);
DLOG(D_INTR, "... done", 0, 0, 0, 0);
return 1;
}
static void
slhci_abdone(struct slhci_softc *sc, int ab)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
struct slhci_pipe *spipe;
struct usbd_xfer *xfer;
uint8_t status, buf_start;
uint8_t *target_buf;
unsigned int actlen;
int head;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
DLOG(D_TRACE, "ABDONE flags %#jx", t->flags, 0,0,0);
DLOG(D_MSG, "DONE AB=%jd spipe %#jx len %jd xfer %#jx", ab,
t->spipe[ab], (uintptr_t)t->len[ab],
(uintptr_t)(t->spipe[ab] ? t->spipe[ab]->xfer : NULL));
spipe = t->spipe[ab];
if (spipe == NULL)
goto done;
t->spipe[ab] = NULL;
xfer = spipe->xfer;
gcq_remove(&spipe->to);
LK_SLASSERT(xfer != NULL, sc, spipe, NULL, return);
status = slhci_read(sc, slhci_tregs[ab][STAT]);
actlen = 0;
if (__predict_false(!status)) {
DDOLOG("no status! xfer %p spipe %p", xfer, spipe, 0,0);
printf("%s: no status! halted\n", SC_NAME(sc));
slhci_halt(sc, spipe, xfer);
return;
}
#ifdef SLHCI_DEBUG
if ((slhcidebug & SLHCI_D_NAK) ||
(status & SL11_EPSTAT_ERRBITS) != SL11_EPSTAT_NAK) {
DDOLOG("USB Status = %#.2x", status, 0, 0, 0);
DDOLOGSTATUS(status);
}
#endif
if (!(status & SL11_EPSTAT_ERRBITS)) {
unsigned int cont = slhci_read(sc, slhci_tregs[ab][CONT]);
unsigned int len = spipe->tregs[LEN];
DLOG(D_XFER, "cont %jd len %jd", cont, len, 0, 0);
if ((status & SL11_EPSTAT_OVERFLOW) || cont > len) {
DDOLOG("overflow - cont %d len %d xfer->ux_length %d "
"xfer->actlen %d", cont, len, xfer->ux_length,
xfer->ux_actlen);
printf("%s: overflow cont %d len %d xfer->ux_length"
" %d xfer->ux_actlen %d\n", SC_NAME(sc), cont,
len, xfer->ux_length, xfer->ux_actlen);
actlen = len;
} else {
actlen = len - cont;
}
spipe->nerrs = 0;
}
if (actlen && (spipe->tregs[PID] & SL11_PID_BITS) == SL11_PID_IN) {
target_buf = spipe->buffer;
buf_start = spipe->tregs[ADR];
} else {
target_buf = NULL;
buf_start = 0;
}
if (status & SL11_EPSTAT_ERRBITS) {
status &= SL11_EPSTAT_ERRBITS;
if (status & SL11_EPSTAT_SETUP) {
printf("%s: Invalid controller state detected! "
"halted\n", SC_NAME(sc));
DDOLOG("Invalid controller state detected! "
"halted", 0, 0, 0, 0);
slhci_halt(sc, spipe, xfer);
return;
} else if (__predict_false(sc->sc_bus.ub_usepolling)) {
head = Q_CALLBACKS;
if (status & SL11_EPSTAT_STALL)
xfer->ux_status = USBD_STALLED;
else if (status & SL11_EPSTAT_TIMEOUT)
xfer->ux_status = USBD_TIMEOUT;
else if (status & SL11_EPSTAT_NAK)
head = Q_NEXT_CB;
else
xfer->ux_status = USBD_IOERROR;
} else if (status & SL11_EPSTAT_NAK) {
int i = spipe->pipe.up_interval;
if (i == 0)
i = 1;
DDOLOG("xfer %p spipe %p NAK delay by %d", xfer, spipe,
i, 0);
spipe->lastframe = spipe->frame = t->frame + i;
slhci_queue_timed(sc, spipe);
goto queued;
} else if (++spipe->nerrs > SLHCI_MAX_RETRIES ||
(status & SL11_EPSTAT_STALL)) {
DDOLOG("xfer %p spipe %p nerrs %d", xfer, spipe,
spipe->nerrs, 0);
if (status & SL11_EPSTAT_STALL)
xfer->ux_status = USBD_STALLED;
else if (status & SL11_EPSTAT_TIMEOUT)
xfer->ux_status = USBD_TIMEOUT;
else
xfer->ux_status = USBD_IOERROR;
DLOG(D_ERR, "Max retries reached! status %#jx "
"xfer->ux_status %jd", status, xfer->ux_status, 0,
0);
DDOLOGSTATUS(status);
head = Q_CALLBACKS;
} else {
head = Q_NEXT_CB;
}
} else if (spipe->ptype == PT_CTRL_SETUP) {
spipe->tregs[PID] = spipe->newpid;
if (xfer->ux_length) {
LK_SLASSERT(spipe->newlen[1] != 0, sc, spipe, xfer,
return);
spipe->tregs[LEN] = spipe->newlen[1];
spipe->bustime = spipe->newbustime[1];
spipe->buffer = xfer->ux_buf;
spipe->ptype = PT_CTRL_DATA;
} else {
status_setup:
spipe->tregs[LEN] = 0;
if (spipe->pflags & PF_LS)
spipe->bustime = SLHCI_LS_CONST;
else
spipe->bustime = SLHCI_FS_CONST;
spipe->ptype = PT_CTRL_STATUS;
spipe->buffer = NULL;
}
spipe->control |= SL11_EPCTRL_DATATOGGLE;
if ((spipe->tregs[PID] & SL11_PID_BITS) == SL11_PID_IN)
spipe->control &= ~SL11_EPCTRL_DIRECTION;
else
spipe->control |= SL11_EPCTRL_DIRECTION;
head = Q_CB;
} else if (spipe->ptype == PT_CTRL_STATUS) {
head = Q_CALLBACKS;
} else {
xfer->ux_actlen += actlen;
spipe->control ^= SL11_EPCTRL_DATATOGGLE;
if (actlen == spipe->tregs[LEN] &&
(xfer->ux_length > xfer->ux_actlen || spipe->wantshort)) {
spipe->buffer += actlen;
LK_SLASSERT(xfer->ux_length >= xfer->ux_actlen, sc,
spipe, xfer, return);
if (xfer->ux_length - xfer->ux_actlen < actlen) {
spipe->wantshort = 0;
spipe->tregs[LEN] = spipe->newlen[0];
spipe->bustime = spipe->newbustime[0];
LK_SLASSERT(xfer->ux_actlen +
spipe->tregs[LEN] == xfer->ux_length, sc,
spipe, xfer, return);
}
head = Q_CB;
} else if (spipe->ptype == PT_CTRL_DATA) {
spipe->tregs[PID] ^= SLHCI_PID_SWAP_IN_OUT;
goto status_setup;
} else {
if (spipe->ptype == PT_INTR) {
spipe->lastframe +=
spipe->pipe.up_interval;
spipe->frame = spipe->lastframe +
spipe->pipe.up_interval;
}
if (spipe->control & SL11_EPCTRL_DATATOGGLE)
spipe->pflags |= PF_TOGGLE;
else
spipe->pflags &= ~PF_TOGGLE;
head = Q_CALLBACKS;
}
}
if (head == Q_CALLBACKS) {
gcq_remove(&spipe->to);
if (xfer->ux_status == USBD_IN_PROGRESS) {
LK_SLASSERT(xfer->ux_actlen <= xfer->ux_length, sc,
spipe, xfer, return);
xfer->ux_status = USBD_NORMAL_COMPLETION;
}
}
enter_q(t, spipe, head);
queued:
if (target_buf != NULL) {
slhci_dotransfer(sc);
start_cc_time(&t_copy_from_dev, actlen);
slhci_read_multi(sc, buf_start, target_buf, actlen);
stop_cc_time(&t_copy_from_dev);
DLOGBUF(D_BUF, target_buf, actlen);
t->pend -= SLHCI_FS_CONST + SLHCI_FS_DATA_TIME(actlen);
}
done:
t->len[ab] = -1;
}
static void
slhci_tstart(struct slhci_softc *sc)
{
struct slhci_transfers *t;
struct slhci_pipe *spipe;
int remaining_bustime;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
if (!(t->flags & (F_AREADY|F_BREADY)))
return;
if (t->flags & (F_AINPROG|F_BINPROG|F_DISABLED))
return;
remaining_bustime = (int)(slhci_read(sc, SL811_CSOF)) << 6;
remaining_bustime -= SLHCI_END_BUSTIME;
if (t->flags & F_AREADY) {
spipe = t->spipe[A];
if (spipe == NULL) {
t->flags &= ~F_AREADY;
t->len[A] = -1;
} else if (remaining_bustime >= spipe->bustime) {
t->flags &= ~(F_AREADY|F_SOFCHECK1|F_SOFCHECK2);
t->flags |= F_AINPROG;
start_cc_time(&t_ab[A], spipe->tregs[LEN]);
slhci_write(sc, SL11_E0CTRL, spipe->control);
goto pend;
}
}
if (t->flags & F_BREADY) {
spipe = t->spipe[B];
if (spipe == NULL) {
t->flags &= ~F_BREADY;
t->len[B] = -1;
} else if (remaining_bustime >= spipe->bustime) {
t->flags &= ~(F_BREADY|F_SOFCHECK1|F_SOFCHECK2);
t->flags |= F_BINPROG;
start_cc_time(&t_ab[B], spipe->tregs[LEN]);
slhci_write(sc, SL11_E1CTRL, spipe->control);
pend:
t->pend = spipe->bustime;
}
}
}
static void
slhci_dotransfer(struct slhci_softc *sc)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
struct slhci_pipe *spipe;
int ab, i;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
while ((t->len[A] == -1 || t->len[B] == -1) &&
(GOT_FIRST_TIMED_COND(spipe, t, spipe->frame <= t->frame) ||
GOT_FIRST_CB(spipe, t))) {
LK_SLASSERT(spipe->xfer != NULL, sc, spipe, NULL, return);
LK_SLASSERT(spipe->ptype != PT_ROOT_CTRL && spipe->ptype !=
PT_ROOT_INTR, sc, spipe, NULL, return);
if (t->len[A] + t->len[B] + spipe->tregs[LEN] + 1 >
SL11_MAX_PACKET_SIZE) {
DLOG(D_XFER, "Transfer does not fit. alen %jd blen %jd "
"len %jd", t->len[A], t->len[B], spipe->tregs[LEN],
0);
return;
}
gcq_remove(&spipe->xq);
if (t->len[A] == -1) {
ab = A;
spipe->tregs[ADR] = SL11_BUFFER_START;
} else {
ab = B;
spipe->tregs[ADR] = SL11_BUFFER_END -
spipe->tregs[LEN];
}
t->len[ab] = spipe->tregs[LEN];
if (spipe->tregs[LEN] && (spipe->tregs[PID] & SL11_PID_BITS)
!= SL11_PID_IN) {
start_cc_time(&t_copy_to_dev,
spipe->tregs[LEN]);
slhci_write_multi(sc, spipe->tregs[ADR],
spipe->buffer, spipe->tregs[LEN]);
stop_cc_time(&t_copy_to_dev);
t->pend -= SLHCI_FS_CONST +
SLHCI_FS_DATA_TIME(spipe->tregs[LEN]);
}
DLOG(D_MSG, "NEW TRANSFER AB=%jd flags %#jx alen %jd blen %jd",
ab, t->flags, t->len[0], t->len[1]);
if (spipe->tregs[LEN])
i = 0;
else
i = 1;
for (; i <= 3; i++)
if (t->current_tregs[ab][i] != spipe->tregs[i]) {
t->current_tregs[ab][i] = spipe->tregs[i];
slhci_write(sc, slhci_tregs[ab][i],
spipe->tregs[i]);
}
DLOG(D_SXFER, "Transfer len %jd pid %#jx dev %jd type %jd",
spipe->tregs[LEN], spipe->tregs[PID], spipe->tregs[DEV],
spipe->ptype);
t->spipe[ab] = spipe;
t->flags |= ab ? F_BREADY : F_AREADY;
slhci_tstart(sc);
}
}
static void
slhci_callback(struct slhci_softc *sc)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
struct slhci_pipe *spipe;
struct usbd_xfer *xfer;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
DLOG(D_SOFT, "CB flags %#jx", t->flags, 0,0,0);
for (;;) {
if (__predict_false(t->flags & F_ROOTINTR)) {
t->flags &= ~F_ROOTINTR;
if (t->rootintr != NULL) {
u_char *p;
KASSERT(t->rootintr->ux_status ==
USBD_IN_PROGRESS);
p = t->rootintr->ux_buf;
p[0] = 2;
t->rootintr->ux_actlen = 1;
t->rootintr->ux_status = USBD_NORMAL_COMPLETION;
xfer = t->rootintr;
goto do_callback;
}
}
if (!DEQUEUED_CALLBACK(spipe, t))
return;
xfer = spipe->xfer;
LK_SLASSERT(xfer != NULL, sc, spipe, NULL, return);
spipe->xfer = NULL;
DLOG(D_XFER, "xfer callback length %jd actlen %jd spipe %#jx "
"type %jd", xfer->ux_length, (uintptr_t)xfer->ux_actlen,
(uintptr_t)spipe, spipe->ptype);
do_callback:
slhci_do_callback(sc, xfer);
}
}
static void
slhci_enter_xfer(struct slhci_softc *sc, struct slhci_pipe *spipe)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
if (__predict_false(t->flags & F_DISABLED) ||
__predict_false(spipe->pflags & PF_GONE)) {
DLOG(D_MSG, "slhci_enter_xfer: DISABLED or GONE", 0,0,0,0);
spipe->xfer->ux_status = USBD_CANCELLED;
}
if (spipe->xfer->ux_status == USBD_IN_PROGRESS) {
if (spipe->xfer->ux_timeout) {
spipe->to_frame = t->frame + spipe->xfer->ux_timeout;
slhci_xfer_timer(sc, spipe);
}
if (spipe->pipe.up_interval)
slhci_queue_timed(sc, spipe);
else
enter_q(t, spipe, Q_CB);
} else
enter_callback(t, spipe);
}
static void
slhci_enter_xfers(struct slhci_softc *sc)
{
struct slhci_pipe *spipe;
KASSERT(mutex_owned(&sc->sc_intr_lock));
while (DEQUEUED_WAITQ(spipe, sc))
slhci_enter_xfer(sc, spipe);
}
static void
slhci_queue_timed(struct slhci_softc *sc, struct slhci_pipe *spipe)
{
struct slhci_transfers *t;
struct gcq *q;
struct slhci_pipe *spp;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
FIND_TIMED(q, t, spp, spp->frame > spipe->frame);
gcq_insert_before(q, &spipe->xq);
}
static void
slhci_xfer_timer(struct slhci_softc *sc, struct slhci_pipe *spipe)
{
struct slhci_transfers *t;
struct gcq *q;
struct slhci_pipe *spp;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
FIND_TO(q, t, spp, spp->to_frame >= spipe->to_frame);
gcq_insert_before(q, &spipe->to);
}
static void
slhci_callback_schedule(struct slhci_softc *sc)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
if (t->flags & F_ACTIVE)
slhci_do_callback_schedule(sc);
}
static void
slhci_do_callback_schedule(struct slhci_softc *sc)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
DLOG(D_MSG, "flags %#jx", t->flags, 0, 0, 0);
if (!(t->flags & F_CALLBACK)) {
t->flags |= F_CALLBACK;
softint_schedule(sc->sc_cb_softintr);
}
}
#if 0
void
slhci_pollxfer(struct slhci_softc *sc, struct usbd_xfer *xfer)
{
KASSERT(mutex_owned(&sc->sc_intr_lock));
slhci_dotransfer(sc);
do {
slhci_dointr(sc);
} while (xfer->ux_status == USBD_IN_PROGRESS);
slhci_do_callback(sc, xfer);
}
#endif
static usbd_status
slhci_do_poll(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
usbd_xfer *xfer)
{
slhci_waitintr(sc, 0);
return USBD_NORMAL_COMPLETION;
}
static usbd_status
slhci_lsvh_warn(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
usbd_xfer *xfer)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
t = &sc->sc_transfers;
if (!(t->flags & F_LSVH_WARNED)) {
printf("%s: Low speed device via hub disabled, "
"see slhci(4)\n", SC_NAME(sc));
DDOLOG("Low speed device via hub disabled, "
"see slhci(4)", SC_NAME(sc), 0,0,0);
t->flags |= F_LSVH_WARNED;
}
return USBD_INVAL;
}
static usbd_status
slhci_isoc_warn(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
usbd_xfer *xfer)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
t = &sc->sc_transfers;
if (!(t->flags & F_ISOC_WARNED)) {
printf("%s: ISOC transfer not supported "
"(see slhci(4))\n", SC_NAME(sc));
DDOLOG("ISOC transfer not supported "
"(see slhci(4))", 0, 0, 0, 0);
t->flags |= F_ISOC_WARNED;
}
return USBD_INVAL;
}
static usbd_status
slhci_open_pipe(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
usbd_xfer *xfer)
{
struct slhci_transfers *t;
struct usbd_pipe *pipe;
t = &sc->sc_transfers;
pipe = &spipe->pipe;
if (t->flags & F_DISABLED)
return USBD_CANCELLED;
else if (pipe->up_interval && !slhci_reserve_bustime(sc, spipe, 1))
return USBD_PENDING_REQUESTS;
else {
enter_all_pipes(t, spipe);
return USBD_NORMAL_COMPLETION;
}
}
static usbd_status
slhci_close_pipe(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
usbd_xfer *xfer)
{
struct usbd_pipe *pipe;
pipe = &spipe->pipe;
if (pipe->up_interval && spipe->ptype != PT_ROOT_INTR)
slhci_reserve_bustime(sc, spipe, 0);
gcq_remove(&spipe->ap);
return USBD_NORMAL_COMPLETION;
}
static usbd_status
slhci_do_abort(struct slhci_softc *sc, struct slhci_pipe *spipe, struct
usbd_xfer *xfer)
{
struct slhci_transfers *t;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
if (spipe->xfer == xfer) {
if (spipe->ptype == PT_ROOT_INTR) {
if (t->rootintr == spipe->xfer)
t->rootintr = NULL;
} else {
gcq_remove(&spipe->to);
gcq_remove(&spipe->xq);
if (t->spipe[A] == spipe) {
t->spipe[A] = NULL;
if (!(t->flags & F_AINPROG))
t->len[A] = -1;
} else if (t->spipe[B] == spipe) {
t->spipe[B] = NULL;
if (!(t->flags & F_BINPROG))
t->len[B] = -1;
}
}
if (xfer->ux_status != USBD_TIMEOUT) {
spipe->xfer = NULL;
spipe->pipe.up_repeat = 0;
}
}
return USBD_NORMAL_COMPLETION;
}
static usbd_status
slhci_halt(struct slhci_softc *sc, struct slhci_pipe *spipe,
struct usbd_xfer *xfer)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
KASSERT(mutex_owned(&sc->sc_intr_lock));
t = &sc->sc_transfers;
DDOLOG("Halt! sc %p spipe %p xfer %p", sc, spipe, xfer, 0);
if (spipe != NULL)
slhci_log_spipe(spipe);
if (xfer != NULL)
slhci_log_xfer(xfer);
if (spipe != NULL && xfer != NULL && spipe->xfer == xfer &&
!gcq_onlist(&spipe->xq) && t->spipe[A] != spipe && t->spipe[B] !=
spipe) {
xfer->ux_status = USBD_CANCELLED;
enter_callback(t, spipe);
}
if (t->flags & F_ACTIVE) {
slhci_intrchange(sc, 0);
}
t->flags &= ~F_ACTIVE;
t->flags |= F_UDISABLED;
if (!(t->flags & F_NODEV))
t->flags |= F_NODEV|F_CCONNECT|F_ROOTINTR;
slhci_drain(sc);
slhci_do_callback_schedule(sc);
return USBD_NORMAL_COMPLETION;
}
static void
slhci_intrchange(struct slhci_softc *sc, uint8_t new_ier)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
KASSERT(mutex_owned(&sc->sc_intr_lock));
if (sc->sc_ier != new_ier) {
DLOG(D_INTR, "New IER %#jx", new_ier, 0, 0, 0);
sc->sc_ier = new_ier;
slhci_write(sc, SL11_IER, new_ier);
BSB_SYNC(sc->iot, sc->ioh, sc->pst, sc->psz);
}
}
static void
slhci_drain(struct slhci_softc *sc)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
struct slhci_pipe *spipe;
struct gcq *q;
int i;
KASSERT(mutex_owned(&sc->sc_intr_lock));
t = &sc->sc_transfers;
DLOG(D_MSG, "DRAIN flags %#jx", t->flags, 0,0,0);
t->pend = INT_MAX;
for (i = 0; i <= 1; i++) {
t->len[i] = -1;
if (t->spipe[i] != NULL) {
enter_callback(t, t->spipe[i]);
t->spipe[i] = NULL;
}
}
gcq_merge_tail(&t->q[Q_CALLBACKS], &t->q[Q_CB]);
gcq_merge_tail(&t->q[Q_CALLBACKS], &t->q[Q_NEXT_CB]);
gcq_merge_tail(&t->q[Q_CALLBACKS], &t->timed);
FOREACH_AP(q, t, spipe) {
spipe->pflags |= PF_GONE;
spipe->pipe.up_repeat = 0;
spipe->pipe.up_aborting = 1;
if (spipe->xfer != NULL)
spipe->xfer->ux_status = USBD_CANCELLED;
}
gcq_remove_all(&t->to);
t->flags |= F_UDISABLED;
t->flags &= ~(F_AREADY|F_BREADY|F_AINPROG|F_BINPROG|F_LOWSPEED);
}
void
slhci_reset(struct slhci_softc *sc)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
struct slhci_pipe *spipe;
struct gcq *q;
uint8_t r, pol, ctrl;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
stop_cc_time(&t_delay);
KASSERT(t->flags & F_ACTIVE);
start_cc_time(&t_delay, 0);
stop_cc_time(&t_delay);
slhci_write(sc, SL11_CTRL, 0);
start_cc_time(&t_delay, 3);
DELAY(3);
stop_cc_time(&t_delay);
slhci_write(sc, SL11_ISR, 0xff);
r = slhci_read(sc, SL11_ISR);
if (r & SL11_ISR_INSERT)
slhci_write(sc, SL11_ISR, SL11_ISR_INSERT);
if (r & SL11_ISR_NODEV) {
DLOG(D_MSG, "NC", 0,0,0,0);
if (!(t->flags & F_NODEV))
t->flags |= F_CCONNECT|F_ROOTINTR|F_NODEV;
slhci_intrchange(sc, SL11_IER_INSERT);
} else {
if (t->flags & F_NODEV)
t->flags |= F_CCONNECT;
t->flags &= ~(F_NODEV|F_LOWSPEED);
if (r & SL11_ISR_DATA) {
DLOG(D_MSG, "FS", 0,0,0,0);
pol = ctrl = 0;
} else {
DLOG(D_MSG, "LS", 0,0,0,0);
pol = SL811_CSOF_POLARITY;
ctrl = SL11_CTRL_LOWSPEED;
t->flags |= F_LOWSPEED;
}
t->frame = 0;
slhci_write(sc, SL11_SOFTIME, 0xe0);
slhci_write(sc, SL811_CSOF, pol|SL811_CSOF_MASTER|0x2e);
slhci_write(sc, SL11_CTRL, ctrl|SL11_CTRL_ENABLESOF);
slhci_write(sc, SL11_E0ADDR, SL11_BUFFER_START);
slhci_write(sc, SL11_E0LEN, 0);
slhci_write(sc, SL11_E0PID, SL11_PID_SOF);
slhci_write(sc, SL11_E0DEV, 0);
slhci_write(sc, SL11_E0CTRL, SL11_EPCTRL_ARM);
slhci_write(sc, SL11_E1ADDR, SL11_BUFFER_END - 8);
slhci_write(sc, SL11_E1LEN, 0);
slhci_write(sc, SL11_E1PID, 0);
slhci_write(sc, SL11_E1DEV, 0);
t->current_tregs[0][ADR] = SL11_BUFFER_START;
t->current_tregs[0][LEN] = 0;
t->current_tregs[0][PID] = SL11_PID_SOF;
t->current_tregs[0][DEV] = 0;
t->current_tregs[1][ADR] = SL11_BUFFER_END - 8;
t->current_tregs[1][LEN] = 0;
t->current_tregs[1][PID] = 0;
t->current_tregs[1][DEV] = 0;
t->len[A] = 0;
t->flags |= F_AINPROG;
slhci_intrchange(sc, SLHCI_NORMAL_INTERRUPTS);
}
t->flags &= ~(F_UDISABLED|F_RESET);
t->flags |= F_CRESET|F_ROOTINTR;
FOREACH_AP(q, t, spipe) {
spipe->pflags &= ~PF_GONE;
spipe->pipe.up_aborting = 0;
}
DLOG(D_MSG, "RESET done flags %#jx", t->flags, 0,0,0);
}
#ifdef SLHCI_DEBUG
static int
slhci_memtest(struct slhci_softc *sc)
{
enum { ASC, DESC, EITHER = ASC };
enum { READ, WRITE };
const char *ptr, *elem;
size_t i;
const int low = SL11_BUFFER_START, high = SL11_BUFFER_END;
int addr = 0, dir = ASC, op = READ;
const char test[] = "E(w0) A(r0w1r1) A(r1w0r0) D(r0w1) D(r1w0) E(r0)";
char c;
const uint8_t dbs[] = { 0x00, 0x0f, 0x33, 0x55 };
uint8_t db;
for (i = 0; i < __arraycount(dbs); i++) {
ptr = test;
elem = ptr;
while ((c = *ptr++) != '\0')
switch (tolower((int)c)) {
case 'a':
dir = ASC;
break;
case 'd':
dir = DESC;
break;
case 'e':
dir = EITHER;
break;
case '(':
elem = ptr;
addr = (dir == ASC) ? low : high;
break;
case 'r':
op = READ;
break;
case 'w':
op = WRITE;
break;
case '0':
case '1':
db = (c - '0') ? ~dbs[i] : dbs[i];
if (op == READ) {
if (slhci_read(sc, addr) != db)
return -1;
} else
slhci_write(sc, addr, db);
break;
case ')':
addr = (dir == ASC) ? addr + 1 : addr - 1;
if (addr >= low && addr <= high)
ptr = elem;
break;
default:
break;
}
}
return 0;
}
#endif
static int
slhci_reserve_bustime(struct slhci_softc *sc, struct slhci_pipe *spipe, int
reserve)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
int bustime, max_packet;
KASSERT(mutex_owned(&sc->sc_intr_lock));
t = &sc->sc_transfers;
max_packet = UGETW(spipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize);
if (spipe->pflags & PF_LS)
bustime = SLHCI_LS_CONST + SLHCI_LS_DATA_TIME(max_packet);
else
bustime = SLHCI_FS_CONST + SLHCI_FS_DATA_TIME(max_packet);
if (!reserve) {
t->reserved_bustime -= bustime;
#ifdef DIAGNOSTIC
if (t->reserved_bustime < 0) {
printf("%s: reserved_bustime %d < 0!\n",
SC_NAME(sc), t->reserved_bustime);
DDOLOG("reserved_bustime %d < 0!",
t->reserved_bustime, 0, 0, 0);
t->reserved_bustime = 0;
}
#endif
return 1;
}
if (t->reserved_bustime + bustime > SLHCI_RESERVED_BUSTIME) {
if (ratecheck(&sc->sc_reserved_warn_rate,
&reserved_warn_rate))
#ifdef SLHCI_NO_OVERTIME
{
printf("%s: Max reserved bus time exceeded! "
"Erroring request.\n", SC_NAME(sc));
DDOLOG("%s: Max reserved bus time exceeded! "
"Erroring request.", 0, 0, 0, 0);
}
return 0;
#else
{
printf("%s: Reserved bus time exceeds %d!\n",
SC_NAME(sc), SLHCI_RESERVED_BUSTIME);
DDOLOG("Reserved bus time exceeds %d!",
SLHCI_RESERVED_BUSTIME, 0, 0, 0);
}
#endif
}
t->reserved_bustime += bustime;
return 1;
}
static void
slhci_insert(struct slhci_softc *sc)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
if (t->flags & F_NODEV)
slhci_intrchange(sc, 0);
else {
slhci_drain(sc);
slhci_intrchange(sc, SL11_IER_INSERT);
}
t->flags ^= F_NODEV;
t->flags |= F_ROOTINTR|F_CCONNECT;
DLOG(D_MSG, "INSERT intr: flags after %#jx", t->flags, 0,0,0);
}
static usbd_status
slhci_clear_feature(struct slhci_softc *sc, unsigned int what)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
usbd_status error;
t = &sc->sc_transfers;
error = USBD_NORMAL_COMPLETION;
KASSERT(mutex_owned(&sc->sc_intr_lock));
if (what == UHF_PORT_POWER) {
DLOG(D_MSG, "POWER_OFF", 0,0,0,0);
t->flags &= ~F_POWER;
if (!(t->flags & F_NODEV))
t->flags |= F_NODEV|F_CCONNECT|F_ROOTINTR;
if (sc->sc_enable_power && (t->flags & F_REALPOWER)) {
t->flags &= ~F_REALPOWER;
sc->sc_enable_power(sc, POWER_OFF);
}
slhci_intrchange(sc, 0);
slhci_drain(sc);
} else if (what == UHF_C_PORT_CONNECTION) {
t->flags &= ~F_CCONNECT;
} else if (what == UHF_C_PORT_RESET) {
t->flags &= ~F_CRESET;
} else if (what == UHF_PORT_ENABLE) {
slhci_drain(sc);
} else if (what != UHF_PORT_SUSPEND) {
DDOLOG("ClrPortFeatERR:value=%#.4x", what, 0,0,0);
error = USBD_IOERROR;
}
return error;
}
static usbd_status
slhci_set_feature(struct slhci_softc *sc, unsigned int what)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
uint8_t r;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
if (what == UHF_PORT_RESET) {
if (!(t->flags & F_ACTIVE)) {
DDOLOG("SET PORT_RESET when not ACTIVE!",
0,0,0,0);
return USBD_INVAL;
}
if (!(t->flags & F_POWER)) {
DDOLOG("SET PORT_RESET without PORT_POWER! flags %p",
t->flags, 0,0,0);
return USBD_INVAL;
}
if (t->flags & F_RESET)
return USBD_NORMAL_COMPLETION;
DLOG(D_MSG, "RESET flags %#jx", t->flags, 0,0,0);
slhci_intrchange(sc, 0);
slhci_drain(sc);
slhci_write(sc, SL11_CTRL, SL11_CTRL_RESETENGINE);
start_cc_time(&t_delay, 50000);
if (sc->sc_bus.ub_usepolling) {
DELAY(50000);
slhci_reset(sc);
} else {
t->flags |= F_RESET;
callout_schedule(&sc->sc_timer, uimax(mstohz(50), 2));
}
} else if (what == UHF_PORT_SUSPEND) {
printf("%s: USB Suspend not implemented!\n", SC_NAME(sc));
DDOLOG("USB Suspend not implemented!", 0, 0, 0, 0);
} else if (what == UHF_PORT_POWER) {
DLOG(D_MSG, "PORT_POWER", 0,0,0,0);
if (!(t->flags & F_ACTIVE))
return USBD_INVAL;
if (t->flags & F_POWER)
return USBD_NORMAL_COMPLETION;
if (!(t->flags & F_REALPOWER)) {
if (sc->sc_enable_power)
sc->sc_enable_power(sc, POWER_ON);
t->flags |= F_REALPOWER;
}
t->flags |= F_POWER;
r = slhci_read(sc, SL11_ISR);
if (r & SL11_ISR_INSERT)
slhci_write(sc, SL11_ISR, SL11_ISR_INSERT);
if (r & SL11_ISR_NODEV) {
slhci_intrchange(sc, SL11_IER_INSERT);
t->flags |= F_NODEV;
} else {
t->flags &= ~F_NODEV;
t->flags |= F_CCONNECT|F_ROOTINTR;
}
} else {
DDOLOG("SetPortFeatERR=%#.8x", what, 0,0,0);
return USBD_IOERROR;
}
return USBD_NORMAL_COMPLETION;
}
static void
slhci_get_status(struct slhci_softc *sc, usb_port_status_t *ps)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
unsigned int status, change;
t = &sc->sc_transfers;
KASSERT(mutex_owned(&sc->sc_intr_lock));
change = 0;
if (t->flags & F_CCONNECT)
change |= UPS_C_CONNECT_STATUS;
if (t->flags & F_CRESET)
change |= UPS_C_PORT_RESET;
status = 0;
if (!(t->flags & F_NODEV))
status |= UPS_CURRENT_CONNECT_STATUS;
if (!(t->flags & F_UDISABLED))
status |= UPS_PORT_ENABLED;
if (t->flags & F_RESET)
status |= UPS_RESET;
if (t->flags & F_POWER)
status |= UPS_PORT_POWER;
if (t->flags & F_LOWSPEED)
status |= UPS_LOW_SPEED;
USETW(ps->wPortStatus, status);
USETW(ps->wPortChange, change);
DLOG(D_ROOT, "status=%#.4jx, change=%#.4jx", status, change, 0,0);
}
static int
slhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
void *buf, int buflen)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_softc *sc = SLHCI_BUS2SC(bus);
struct slhci_transfers *t = &sc->sc_transfers;
usbd_status error = USBD_IOERROR;
uint16_t len, value, index;
uint8_t type;
int actlen = 0;
len = UGETW(req->wLength);
value = UGETW(req->wValue);
index = UGETW(req->wIndex);
type = req->bmRequestType;
SLHCI_DEXEC(D_TRACE, slhci_log_req(req));
switch (req->bRequest) {
case UR_CLEAR_FEATURE:
if (type == UT_WRITE_CLASS_OTHER) {
if (index == 1 ) {
mutex_enter(&sc->sc_intr_lock);
error = slhci_clear_feature(sc, value);
mutex_exit(&sc->sc_intr_lock);
} else
DLOG(D_ROOT, "Clear Port Feature "
"index = %#.4jx", index, 0,0,0);
}
break;
case UR_SET_FEATURE:
if (type == UT_WRITE_CLASS_OTHER) {
if (index == 1 ) {
mutex_enter(&sc->sc_intr_lock);
error = slhci_set_feature(sc, value);
mutex_exit(&sc->sc_intr_lock);
} else
DLOG(D_ROOT, "Set Port Feature "
"index = %#.4jx", index, 0,0,0);
} else if (type != UT_WRITE_CLASS_DEVICE)
DLOG(D_ROOT, "Set Device Feature "
"ENDPOINT_HALT or DEVICE_REMOTE_WAKEUP "
"not supported", 0,0,0,0);
break;
case UR_GET_STATUS:
if (type == UT_READ_CLASS_OTHER) {
if (index == 1 && len ==
sizeof(usb_port_status_t)) {
mutex_enter(&sc->sc_intr_lock);
slhci_get_status(sc, (usb_port_status_t *)
buf);
mutex_exit(&sc->sc_intr_lock);
actlen = sizeof(usb_port_status_t);
error = USBD_NORMAL_COMPLETION;
} else
DLOG(D_ROOT, "Get Port Status index = %#.4jx "
"len = %#.4jx", index, len, 0,0);
} else if (type == UT_READ_CLASS_DEVICE) {
if (len == sizeof(usb_hub_status_t)) {
DLOG(D_ROOT, "Get Hub Status",
0,0,0,0);
actlen = sizeof(usb_hub_status_t);
memset(buf, 0, actlen);
error = USBD_NORMAL_COMPLETION;
} else
DLOG(D_ROOT, "Get Hub Status bad len %#.4jx",
len, 0,0,0);
}
break;
case UR_GET_DESCRIPTOR:
if (type == UT_READ_DEVICE) {
if (value == (UDESC_DEVICE<<8)) {
actlen = buflen;
error = USBD_NORMAL_COMPLETION;
} else if (value == (UDESC_CONFIG<<8)) {
struct usb_roothub_descriptors confd;
actlen = uimin(buflen, sizeof(confd));
memcpy(&confd, buf, actlen);
confd.urh_confd.bMaxPower = t->max_current;
memcpy(buf, &confd, actlen);
error = USBD_NORMAL_COMPLETION;
} else if (value == ((UDESC_STRING<<8)|1)) {
actlen = buflen;
error = USBD_NORMAL_COMPLETION;
} else if (value == ((UDESC_STRING<<8)|2)) {
actlen = usb_makestrdesc((usb_string_descriptor_t *)
buf, len, "SL811HS/T root hub");
error = USBD_NORMAL_COMPLETION;
} else
DDOLOG("Unknown Get Descriptor %#.4x",
value, 0,0,0);
} else if (type == UT_READ_CLASS_DEVICE) {
if (value == (UDESC_HUB<<8)) {
usb_hub_descriptor_t hubd;
actlen = uimin(buflen, sizeof(hubd));
memcpy(&hubd, buf, actlen);
hubd.bHubContrCurrent =
500 - t->max_current;
memcpy(buf, &hubd, actlen);
error = USBD_NORMAL_COMPLETION;
} else
DDOLOG("Unknown Get Hub Descriptor %#.4x",
value, 0,0,0);
}
break;
default:
return buflen;
}
if (error == USBD_NORMAL_COMPLETION)
return actlen;
return -1;
}
#ifdef SLHCI_DEBUG
void
slhci_log_buffer(struct usbd_xfer *xfer)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
u_char *buf;
if(xfer->ux_length > 0 &&
UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) ==
UE_DIR_IN) {
buf = xfer->ux_buf;
DDOLOGBUF(buf, xfer->ux_actlen);
DDOLOG("len %d actlen %d short %d", xfer->ux_length,
xfer->ux_actlen, xfer->ux_length - xfer->ux_actlen, 0);
}
}
void
slhci_log_req(usb_device_request_t *r)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
int req, type, value, index, len;
req = r->bRequest;
type = r->bmRequestType;
value = UGETW(r->wValue);
index = UGETW(r->wIndex);
len = UGETW(r->wLength);
DDOLOG("request: type %#x", type, 0, 0, 0);
DDOLOG("request: r=%d,v=%d,i=%d,l=%d ", req, value, index, len);
}
void
slhci_log_dumpreg(void)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
uint8_t r;
unsigned int aaddr, alen, baddr, blen;
static u_char buf[240];
r = slhci_read(ssc, SL11_E0CTRL);
DDOLOG("USB A Host Control = %#.2x", r, 0, 0, 0);
DDOLOGEPCTRL(r);
aaddr = slhci_read(ssc, SL11_E0ADDR);
DDOLOG("USB A Base Address = %u", aaddr, 0,0,0);
alen = slhci_read(ssc, SL11_E0LEN);
DDOLOG("USB A Length = %u", alen, 0,0,0);
r = slhci_read(ssc, SL11_E0STAT);
DDOLOG("USB A Status = %#.2x", r, 0,0,0);
DDOLOGEPSTAT(r);
r = slhci_read(ssc, SL11_E0CONT);
DDOLOG("USB A Remaining or Overflow Length = %u", r, 0,0,0);
r = slhci_read(ssc, SL11_E1CTRL);
DDOLOG("USB B Host Control = %#.2x", r, 0,0,0);
DDOLOGEPCTRL(r);
baddr = slhci_read(ssc, SL11_E1ADDR);
DDOLOG("USB B Base Address = %u", baddr, 0,0,0);
blen = slhci_read(ssc, SL11_E1LEN);
DDOLOG("USB B Length = %u", blen, 0,0,0);
r = slhci_read(ssc, SL11_E1STAT);
DDOLOG("USB B Status = %#.2x", r, 0,0,0);
DDOLOGEPSTAT(r);
r = slhci_read(ssc, SL11_E1CONT);
DDOLOG("USB B Remaining or Overflow Length = %u", r, 0,0,0);
r = slhci_read(ssc, SL11_CTRL);
DDOLOG("Control = %#.2x", r, 0,0,0);
DDOLOGCTRL(r);
r = slhci_read(ssc, SL11_IER);
DDOLOG("Interrupt Enable = %#.2x", r, 0,0,0);
DDOLOGIER(r);
r = slhci_read(ssc, SL11_ISR);
DDOLOG("Interrupt Status = %#.2x", r, 0,0,0);
DDOLOGISR(r);
r = slhci_read(ssc, SL11_REV);
DDOLOG("Revision = %#.2x", r, 0,0,0);
r = slhci_read(ssc, SL811_CSOF);
DDOLOG("SOF Counter = %#.2x", r, 0,0,0);
if (alen && aaddr >= SL11_BUFFER_START && aaddr < SL11_BUFFER_END &&
alen <= SL11_MAX_PACKET_SIZE && aaddr + alen <= SL11_BUFFER_END) {
slhci_read_multi(ssc, aaddr, buf, alen);
DDOLOG("USBA Buffer: start %u len %u", aaddr, alen, 0,0);
DDOLOGBUF(buf, alen);
} else if (alen)
DDOLOG("USBA Buffer Invalid", 0,0,0,0);
if (blen && baddr >= SL11_BUFFER_START && baddr < SL11_BUFFER_END &&
blen <= SL11_MAX_PACKET_SIZE && baddr + blen <= SL11_BUFFER_END) {
slhci_read_multi(ssc, baddr, buf, blen);
DDOLOG("USBB Buffer: start %u len %u", baddr, blen, 0,0);
DDOLOGBUF(buf, blen);
} else if (blen)
DDOLOG("USBB Buffer Invalid", 0,0,0,0);
}
void
slhci_log_xfer(struct usbd_xfer *xfer)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
DDOLOG("xfer: length=%u, actlen=%u, flags=%#x, timeout=%u,",
xfer->ux_length, xfer->ux_actlen, xfer->ux_flags, xfer->ux_timeout);
DDOLOG("buffer=%p", xfer->ux_buf, 0,0,0);
slhci_log_req(&xfer->ux_request);
}
void
slhci_log_spipe(struct slhci_pipe *spipe)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
DDOLOG("spipe %p onlists: AP=%d TO=%d XQ=%d", spipe,
gcq_onlist(&spipe->ap) ? 1 : 0,
gcq_onlist(&spipe->to) ? 1 : 0,
gcq_onlist(&spipe->xq) ? 1 : 0);
DDOLOG("spipe: xfer %p buffer %p pflags %#x ptype %d",
spipe->xfer, spipe->buffer, spipe->pflags, spipe->ptype);
}
void
slhci_print_intr(void)
{
unsigned int ier, isr;
ier = slhci_read(ssc, SL11_IER);
isr = slhci_read(ssc, SL11_ISR);
printf("IER: %#x ISR: %#x \n", ier, isr);
}
#if 0
void
slhci_log_sc(void)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
struct slhci_transfers *t;
int i;
t = &ssc->sc_transfers;
DDOLOG("Flags=%#x", t->flags, 0,0,0);
DDOLOG("a = %p Alen=%d b = %p Blen=%d", t->spipe[0], t->len[0],
t->spipe[1], t->len[1]);
for (i = 0; i <= Q_MAX; i++)
DDOLOG("Q %d: %p", i, gcq_hq(&t->q[i]), 0,0);
DDOLOG("TIMED: %p", GCQ_ITEM(gcq_hq(&t->to),
struct slhci_pipe, to), 0,0,0);
DDOLOG("frame=%d rootintr=%p", t->frame, t->rootintr, 0,0);
DDOLOG("ub_usepolling=%d", ssc->sc_bus.ub_usepolling, 0, 0, 0);
}
void
slhci_log_slreq(struct slhci_pipe *r)
{
SLHCIHIST_FUNC(); SLHCIHIST_CALLED();
DDOLOG("xfer: %p", r->xfer, 0,0,0);
DDOLOG("buffer: %p", r->buffer, 0,0,0);
DDOLOG("bustime: %u", r->bustime, 0,0,0);
DDOLOG("control: %#x", r->control, 0,0,0);
DDOLOGEPCTRL(r->control);
DDOLOG("pid: %#x", r->tregs[PID], 0,0,0);
DDOLOG("dev: %u", r->tregs[DEV], 0,0,0);
DDOLOG("len: %u", r->tregs[LEN], 0,0,0);
if (r->xfer)
slhci_log_xfer(r->xfer);
}
#endif
#endif