// SPDX-License-Identifier: GPL-2.0+ /* * Copyright (c) 2008 Rodolfo Giometti <giometti@linux.it> * Copyright (c) 2008 Eurotech S.p.A. <info@eurtech.it> * * This code is *strongly* based on EHCI-HCD code by David Brownell since * the chip is a quasi-EHCI compatible.
*/
/* Type tag from {qh, itd, sitd, fstn}->hw_next */ #define Q_NEXT_TYPE(dma) ((dma) & cpu_to_le32 (3 << 1))
/* values for that type tag */ #define Q_TYPE_QH cpu_to_le32 (1 << 1)
/* next async queue entry, or pointer to interrupt/periodic QH */ #define QH_NEXT(dma) (cpu_to_le32(((u32)dma)&~0x01f)|Q_TYPE_QH)
/* for periodic/async schedules and qtd lists, mark end of list */ #define EHCI_LIST_END cpu_to_le32(1) /* "null pointer" to hw */
/* * Entries in periodic shadow table are pointers to one of four kinds * of data structure. That's dictated by the hardware; a type tag is * encoded in the low bits of the hardware's periodic schedule. Use * Q_NEXT_TYPE to get the tag. * * For entries in the async schedule, the type tag always says "qh".
*/ union ehci_shadow { struct ehci_qh *qh; /* Q_TYPE_QH */
__le32 *hw_next; /* (all types) */ void *ptr;
};
/* * EHCI Specification 0.95 Section 3.6 * QH: describes control/bulk/interrupt endpoints * See Fig 3-7 "Queue Head Structure Layout". * * These appear in both the async and (for interrupt) periodic schedules.
*/
struct ehci_qh { /* first part defined by EHCI spec */
__le32 hw_next; /* see EHCI 3.6.1 */
__le32 hw_info1; /* see EHCI 3.6.2 */ #define QH_HEAD 0x00008000
__le32 hw_info2; /* see EHCI 3.6.2 */ #define QH_SMASK 0x000000ff #define QH_CMASK 0x0000ff00 #define QH_HUBADDR 0x007f0000 #define QH_HUBPORT 0x3f800000 #define QH_MULT 0xc0000000
__le32 hw_current; /* qtd list - see EHCI 3.6.4 */
/* qtd overlay (hardware parts of a struct ehci_qtd) */
__le32 hw_qtd_next;
__le32 hw_alt_next;
__le32 hw_token;
__le32 hw_buf[5];
__le32 hw_buf_hi[5];
/* the rest is HCD-private */
dma_addr_t qh_dma; /* address of qh */ union ehci_shadow qh_next; /* ptr to qh; or periodic */ struct list_head qtd_list; /* sw qtd list */ struct ehci_qtd *dummy; struct ehci_qh *reclaim; /* next to reclaim */
u8 qh_state; #define QH_STATE_LINKED 1 /* HC sees this */ #define QH_STATE_UNLINK 2 /* HC may still see this */ #define QH_STATE_IDLE 3 /* HC doesn't see this */ #define QH_STATE_UNLINK_WAIT 4 /* LINKED and on reclaim q */ #define QH_STATE_COMPLETING 5 /* don't touch token.HALT */
/* Only how many elements & element structure are specifies here. */ /* 2 host controllers are enabled - total size <= 28 kbytes */ #define DEFAULT_I_TDPS 1024 #define QHEAD_NUM 16 #define QTD_NUM 32 #define SITD_NUM 8 #define MURB_NUM 8
union ehci_shadow *pshadow; /* mirror hw periodic table */ int next_uframe; /* scan periodic, start here */ unsignedint periodic_sched; /* periodic activity count */
/* per root hub port */ unsignedlong reset_done[EHCI_MAX_ROOT_PORTS]; /* bit vectors (one bit per port) */ unsignedlong bus_suspended; /* which ports were * already suspended at the * start of a bus suspend
*/ unsignedlong companion_ports;/* which ports are dedicated * to the companion controller
*/
/* SILICON QUIRKS */ struct list_head urb_list; /* this is the head to urb * queue that didn't get enough * resources
*/ struct oxu_murb *murb_pool; /* murb per split big urb */ unsignedint urb_len;
switch (action) { case TIMER_IAA_WATCHDOG:
t = EHCI_IAA_JIFFIES; break; case TIMER_IO_WATCHDOG:
t = EHCI_IO_JIFFIES; break; case TIMER_ASYNC_OFF:
t = EHCI_ASYNC_JIFFIES; break; case TIMER_ASYNC_SHRINK: default:
t = EHCI_SHRINK_JIFFIES; break;
}
t += jiffies; /* all timings except IAA watchdog can be overridden. * async queue SHRINK often precedes IAA. while it's ready * to go OFF neither can matter, and afterwards the IO * watchdog stops unless there's still periodic traffic.
*/ if (action != TIMER_IAA_WATCHDOG
&& t > oxu->watchdog.expires
&& timer_pending(&oxu->watchdog)) return;
mod_timer(&oxu->watchdog, t);
}
}
/* * handshake - spin reading hc until handshake completes or fails * @ptr: address of hc register to be read * @mask: bits to look at in result of read * @done: value of those bits when handshake succeeds * @usec: timeout in microseconds * * Returns negative errno, or zero on success * * Success happens when the "mask" bits have the specified value (hardware * handshake done). There are two failure modes: "usec" have passed (major * hardware flakeout), or the register reads as all-ones (hardware removed). * * That last failure should_only happen in cases like physical cardbus eject * before driver shutdown. But it also seems to be caused by bugs in cardbus * bridge shutdown: shutting down the bridge before the devices using it.
*/ staticint handshake(struct oxu_hcd *oxu, void __iomem *ptr,
u32 mask, u32 done, int usec)
{
u32 result; int ret;
ret = readl_poll_timeout_atomic(ptr, result,
((result & mask) == done ||
result == U32_MAX),
1, usec); if (result == U32_MAX) /* card removed */ return -ENODEV;
return ret;
}
/* Force HC to halt state from unknown (EHCI spec section 2.3) */ staticint ehci_halt(struct oxu_hcd *oxu)
{
u32 temp = readl(&oxu->regs->status);
/* disable any irqs left enabled by previous code */
writel(0, &oxu->regs->intr_enable);
/* wait for any schedule enables/disables to take effect */
temp = readl(&oxu->regs->command) << 10;
temp &= STS_ASS | STS_PSS; if (handshake(oxu, &oxu->regs->status, STS_ASS | STS_PSS,
temp, 16 * 125) != 0) {
oxu_to_hcd(oxu)->state = HC_STATE_HALT; return;
}
/* then disable anything that's still active */
temp = readl(&oxu->regs->command);
temp &= ~(CMD_ASE | CMD_IAAD | CMD_PSE);
writel(temp, &oxu->regs->command);
/* hardware can take 16 microframes to turn off ... */ if (handshake(oxu, &oxu->regs->status, STS_ASS | STS_PSS,
0, 16 * 125) != 0) {
oxu_to_hcd(oxu)->state = HC_STATE_HALT; return;
}
}
staticint check_reset_complete(struct oxu_hcd *oxu, int index,
u32 __iomem *status_reg, int port_status)
{ if (!(port_status & PORT_CONNECT)) {
oxu->reset_done[index] = 0; return port_status;
}
/* if reset finished and it's still not enabled -- handoff */ if (!(port_status & PORT_PE)) {
oxu_dbg(oxu, "Failed to enable port %d on root hub TT\n",
index+1); return port_status;
} else
oxu_dbg(oxu, "port %d high speed\n", index + 1);
/* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
temp = HUB_CHAR_INDV_PORT_OCPM; /* per-port overcurrent reporting */ if (HCS_PPC(oxu->hcs_params))
temp |= HUB_CHAR_INDV_PORT_LPSM; /* per-port power control */ else
temp |= HUB_CHAR_NO_LPSM; /* no power switching */
desc->wHubCharacteristics = (__force __u16)cpu_to_le16(temp);
}
/* Allocate an OXU210HP on-chip memory data buffer * * An on-chip memory data buffer is required for each OXU210HP USB transfer. * Each transfer descriptor has one or more on-chip memory data buffers. * * Data buffers are allocated from a fix sized pool of data blocks. * To minimise fragmentation and give reasonable memory utlisation, * data buffers are allocated with sizes the power of 2 multiples of * the block size, starting on an address a multiple of the allocated size. * * FIXME: callers of this function require a buffer to be allocated for * len=0. This is a waste of on-chip memory and should be fix. Then this * function should be changed to not allocate a buffer for len=0.
*/ staticint oxu_buf_alloc(struct oxu_hcd *oxu, struct ehci_qtd *qtd, int len)
{ int n_blocks; /* minium blocks needed to hold len */ int a_blocks; /* blocks allocated */ int i, j;
/* Don't allocate bigger than supported */ if (len > BUFFER_SIZE * BUFFER_NUM) {
oxu_err(oxu, "buffer too big (%d)\n", len); return -ENOMEM;
}
spin_lock(&oxu->mem_lock);
/* Number of blocks needed to hold len */
n_blocks = (len + BUFFER_SIZE - 1) / BUFFER_SIZE;
/* Round the number of blocks up to the power of 2 */ for (a_blocks = 1; a_blocks < n_blocks; a_blocks <<= 1)
;
/* Find a suitable available data buffer */ for (i = 0; i < BUFFER_NUM;
i += max_t(int, a_blocks, oxu->db_used[i])) {
/* Check all the required blocks are available */ for (j = 0; j < a_blocks; j++) if (oxu->db_used[i + j]) break;
/* clean qtds first, and know this is not linked */ if (!list_empty(&qh->qtd_list) || qh->qh_next.ptr) {
oxu_dbg(oxu, "unused qh not empty!\n");
BUG();
} if (qh->dummy)
oxu_qtd_free(oxu, qh->dummy);
oxu_qh_free(oxu, qh);
}
for (i = 0; i < MURB_NUM; i++) if (!oxu->murb_used[i]) break;
if (i < MURB_NUM) {
murb = &(oxu->murb_pool)[i];
oxu->murb_used[i] = 1;
}
spin_unlock(&oxu->mem_lock);
return murb;
}
/* The queue heads and transfer descriptors are managed from pools tied * to each of the "per device" structures. * This is the initialisation and cleanup code.
*/ staticvoid ehci_mem_cleanup(struct oxu_hcd *oxu)
{
kfree(oxu->murb_pool);
oxu->murb_pool = NULL;
if (oxu->async)
qh_put(oxu->async);
oxu->async = NULL;
/* Remember to add cleanup code (above) if you add anything here.
*/ staticint ehci_mem_init(struct oxu_hcd *oxu, gfp_t flags)
{ int i;
for (i = 0; i < oxu->periodic_size; i++)
oxu->mem->frame_list[i] = EHCI_LIST_END; for (i = 0; i < QHEAD_NUM; i++)
oxu->qh_used[i] = 0; for (i = 0; i < QTD_NUM; i++)
oxu->qtd_used[i] = 0;
oxu->murb_pool = kcalloc(MURB_NUM, sizeof(struct oxu_murb), flags); if (!oxu->murb_pool) goto fail;
for (i = 0; i < MURB_NUM; i++)
oxu->murb_used[i] = 0;
oxu->async = oxu_qh_alloc(oxu); if (!oxu->async) goto fail;
/* Fill a qtd, returning how much of the buffer we were able to queue up.
*/ staticint qtd_fill(struct ehci_qtd *qtd, dma_addr_t buf, size_t len, int token, int maxpacket)
{ int i, count;
u64 addr = buf;
/* one buffer entry per 4K ... first might be short or unaligned */
qtd->hw_buf[0] = cpu_to_le32((u32)addr);
qtd->hw_buf_hi[0] = cpu_to_le32((u32)(addr >> 32));
count = 0x1000 - (buf & 0x0fff); /* rest of that page */ if (likely(len < count)) /* ... iff needed */
count = len; else {
buf += 0x1000;
buf &= ~0x0fff;
/* per-qtd limit: from 16K to 20K (best alignment) */ for (i = 1; count < len && i < 5; i++) {
addr = buf;
qtd->hw_buf[i] = cpu_to_le32((u32)addr);
qtd->hw_buf_hi[i] = cpu_to_le32((u32)(addr >> 32));
buf += 0x1000; if ((count + 0x1000) < len)
count += 0x1000; else
count = len;
}
/* short packets may only terminate transfers */ if (count != len)
count -= (count % maxpacket);
}
qtd->hw_token = cpu_to_le32((count << 16) | token);
qtd->length = count;
return count;
}
staticinlinevoid qh_update(struct oxu_hcd *oxu, struct ehci_qh *qh, struct ehci_qtd *qtd)
{ /* writes to an active overlay are unsafe */
BUG_ON(qh->qh_state != QH_STATE_IDLE);
/* Except for control endpoints, we make hardware maintain data * toggle (like OHCI) ... here (re)initialize the toggle in the QH, * and set the pseudo-toggle in udev. Only usb_clear_halt() will * ever clear it.
*/ if (!(qh->hw_info1 & cpu_to_le32(1 << 14))) { unsigned is_out, epnum;
/* HC must see latest qtd and qh data before we clear ACTIVE+HALT */
wmb();
qh->hw_token &= cpu_to_le32(QTD_TOGGLE | QTD_STS_PING);
}
/* If it weren't for a common silicon quirk (writing the dummy into the qh * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault * recovery (including urb dequeue) would need software changes to a QH...
*/ staticvoid qh_refresh(struct oxu_hcd *oxu, struct ehci_qh *qh)
{ struct ehci_qtd *qtd;
if (list_empty(&qh->qtd_list))
qtd = qh->dummy; else {
qtd = list_entry(qh->qtd_list.next, struct ehci_qtd, qtd_list); /* first qtd may already be partially processed */ if (cpu_to_le32(qtd->qtd_dma) == qh->hw_current)
qtd = NULL;
}
/* Process and free completed qtds for a qh, returning URBs to drivers. * Chases up to qh->hw_current. Returns number of completions called, * indicating how much "real" work we did.
*/ staticunsigned qh_completions(struct oxu_hcd *oxu, struct ehci_qh *qh)
{ struct ehci_qtd *last = NULL, *end = qh->dummy; struct ehci_qtd *qtd, *tmp; int stopped; unsigned count = 0; int do_status = 0;
u8 state; struct oxu_murb *murb = NULL;
if (unlikely(list_empty(&qh->qtd_list))) return count;
/* completions (or tasks on other cpus) must never clobber HALT * till we've gone through and cleaned everything up, even when * they add urbs to this qh's queue or mark them for unlinking. * * NOTE: unlinking expects to be done in queue order.
*/
state = qh->qh_state;
qh->qh_state = QH_STATE_COMPLETING;
stopped = (state == QH_STATE_IDLE);
/* remove de-activated QTDs from front of queue. * after faults (including short reads), cleanup this urb * then let the queue advance. * if queue is stopped, handles unlinks.
*/
list_for_each_entry_safe(qtd, tmp, &qh->qtd_list, qtd_list) { struct urb *urb;
u32 token = 0;
urb = qtd->urb;
/* Clean up any state from previous QTD ...*/ if (last) { if (likely(last->urb != urb)) { if (last->urb->complete == NULL) {
murb = (struct oxu_murb *) last->urb;
last->urb = murb->main; if (murb->last) {
ehci_urb_done(oxu, last->urb);
count++;
}
oxu_murb_free(oxu, murb);
} else {
ehci_urb_done(oxu, last->urb);
count++;
}
}
oxu_qtd_free(oxu, last);
last = NULL;
}
/* ignore urbs submitted during completions we reported */ if (qtd == end) break;
/* hardware copies qtd out of qh overlay */
rmb();
token = le32_to_cpu(qtd->hw_token);
/* always clean up qtds the hc de-activated */ if ((token & QTD_STS_ACTIVE) == 0) {
if ((token & QTD_STS_HALT) != 0) {
stopped = 1;
/* magic dummy for some short reads; qh won't advance. * that silicon quirk can kick in with this dummy too.
*/
} elseif (IS_SHORT_READ(token) &&
!(qtd->hw_alt_next & EHCI_LIST_END)) {
stopped = 1; goto halt;
}
/* stop scanning when we reach qtds the hc is using */
} elseif (likely(!stopped &&
HC_IS_RUNNING(oxu_to_hcd(oxu)->state))) { break;
} else {
stopped = 1;
if (unlikely(!HC_IS_RUNNING(oxu_to_hcd(oxu)->state)))
urb->status = -ESHUTDOWN;
/* ignore active urbs unless some previous qtd * for the urb faulted (including short read) or * its urb was canceled. we may patch qh or qtds.
*/ if (likely(urb->status == -EINPROGRESS)) continue;
/* issue status after short control reads */ if (unlikely(do_status != 0)
&& QTD_PID(token) == 0 /* OUT */) {
do_status = 0; continue;
}
/* token in overlay may be most current */ if (state == QH_STATE_IDLE
&& cpu_to_le32(qtd->qtd_dma)
== qh->hw_current)
token = le32_to_cpu(qh->hw_token);
/* force halt for unlinked or blocked qh, so we'll * patch the qh later and so that completions can't * activate it while we "know" it's stopped.
*/ if ((HALT_BIT & qh->hw_token) == 0) {
halt:
qh->hw_token |= HALT_BIT;
wmb();
}
}
/* Remove it from the queue */
qtd_copy_status(oxu, urb->complete ?
urb : ((struct oxu_murb *) urb)->main,
qtd->length, token); if ((usb_pipein(qtd->urb->pipe)) &&
(NULL != qtd->transfer_buffer))
memcpy(qtd->transfer_buffer, qtd->buffer, qtd->length);
do_status = (urb->status == -EREMOTEIO)
&& usb_pipecontrol(urb->pipe);
if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
last = list_entry(qtd->qtd_list.prev, struct ehci_qtd, qtd_list);
last->hw_next = qtd->hw_next;
}
list_del(&qtd->qtd_list);
last = qtd;
}
/* last urb's completion might still need calling */ if (likely(last != NULL)) { if (last->urb->complete == NULL) {
murb = (struct oxu_murb *) last->urb;
last->urb = murb->main; if (murb->last) {
ehci_urb_done(oxu, last->urb);
count++;
}
oxu_murb_free(oxu, murb);
} else {
ehci_urb_done(oxu, last->urb);
count++;
}
oxu_qtd_free(oxu, last);
}
/* restore original state; caller must unlink or relink */
qh->qh_state = state;
/* be sure the hardware's done with the qh before refreshing * it after fault cleanup, or recovering from silicon wrongly * overlaying the dummy qtd (which reduces DMA chatter).
*/ if (stopped != 0 || qh->hw_qtd_next == EHCI_LIST_END) { switch (state) { case QH_STATE_IDLE:
qh_refresh(oxu, qh); break; case QH_STATE_LINKED: /* should be rare for periodic transfers, * except maybe high bandwidth ...
*/ if ((cpu_to_le32(QH_SMASK)
& qh->hw_info2) != 0) {
intr_deschedule(oxu, qh);
(void) qh_schedule(oxu, qh);
} else
unlink_async(oxu, qh); break; /* otherwise, unlink already started */
}
}
return count;
}
/* High bandwidth multiplier, as encoded in highspeed endpoint descriptors */ #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03)) /* ... and packet size, for any kind of endpoint descriptor */ #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
/* Reverse of qh_urb_transaction: free a list of TDs. * used for cleanup after errors, before HC sees an URB's TDs.
*/ staticvoid qtd_list_free(struct oxu_hcd *oxu, struct urb *urb, struct list_head *head)
{ struct ehci_qtd *qtd, *temp;
/* Create a list of filled qtds for this URB; won't link into qh.
*/ staticstruct list_head *qh_urb_transaction(struct oxu_hcd *oxu, struct urb *urb, struct list_head *head,
gfp_t flags)
{ struct ehci_qtd *qtd, *qtd_prev;
dma_addr_t buf; int len, maxpacket; int is_input;
u32 token; void *transfer_buf = NULL; int ret;
/* * URBs map to sequences of QTDs: one logical transaction
*/
qtd = ehci_qtd_alloc(oxu); if (unlikely(!qtd)) return NULL;
list_add_tail(&qtd->qtd_list, head);
qtd->urb = urb;
token = QTD_STS_ACTIVE;
token |= (EHCI_TUNE_CERR << 10); /* for split transactions, SplitXState initialized to zero */
len = urb->transfer_buffer_length;
is_input = usb_pipein(urb->pipe); if (!urb->transfer_buffer && urb->transfer_buffer_length && is_input)
urb->transfer_buffer = phys_to_virt(urb->transfer_dma);
if (usb_pipecontrol(urb->pipe)) { /* SETUP pid */
ret = oxu_buf_alloc(oxu, qtd, sizeof(struct usb_ctrlrequest)); if (ret) goto cleanup;
/* * buffer gets wrapped in one or more qtds; * last one may be "short" (including zero len) * and may serve as a control status ack
*/ for (;;) { int this_qtd_len;
/* qh makes control packets use qtd toggle; maybe switch it */ if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
token ^= QTD_TOGGLE;
if (likely(len <= 0)) break;
qtd_prev = qtd;
qtd = ehci_qtd_alloc(oxu); if (unlikely(!qtd)) goto cleanup; if (likely(len > 0)) {
ret = oxu_buf_alloc(oxu, qtd, len); if (ret) goto cleanup;
}
qtd->urb = urb;
qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma);
list_add_tail(&qtd->qtd_list, head);
}
/* unless the bulk/interrupt caller wants a chance to clean * up after short reads, hc should advance qh past this urb
*/ if (likely((urb->transfer_flags & URB_SHORT_NOT_OK) == 0
|| usb_pipecontrol(urb->pipe)))
qtd->hw_alt_next = EHCI_LIST_END;
/* * control requests may need a terminating data "status" ack; * bulk ones may need a terminating short packet (zero length).
*/ if (likely(urb->transfer_buffer_length != 0)) { int one_more = 0;
/* Each QH holds a qtd list; a QH is used for everything except iso. * * For interrupt urbs, the scheduler must set the microframe scheduling * mask(s) each time the QH gets scheduled. For highspeed, that's * just one microframe in the s-mask. For split interrupt transactions * there are additional complications: c-mask, maybe FSTNs.
*/ staticstruct ehci_qh *qh_make(struct oxu_hcd *oxu, struct urb *urb, gfp_t flags)
{ struct ehci_qh *qh = oxu_qh_alloc(oxu);
u32 info1 = 0, info2 = 0; int is_input, type; int maxp = 0;
if (!qh) return qh;
/* * init endpoint/device data for this QH
*/
info1 |= usb_pipeendpoint(urb->pipe) << 8;
info1 |= usb_pipedevice(urb->pipe) << 0;
is_input = usb_pipein(urb->pipe);
type = usb_pipetype(urb->pipe);
maxp = usb_maxpacket(urb->dev, urb->pipe);
/* Compute interrupt scheduling parameters just once, and save. * - allowing for high bandwidth, how many nsec/uframe are used? * - split transactions need a second CSPLIT uframe; same question * - splits also need a schedule gap (for full/low speed I/O) * - qh has a polling interval * * For control/bulk requests, the HC or TT handles these.
*/ if (type == PIPE_INTERRUPT) {
qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
is_input, 0,
hb_mult(maxp) * max_packet(maxp)));
qh->start = NO_FRAME;
/* (re)start the async schedule? */
head = oxu->async;
timer_action_done(oxu, TIMER_ASYNC_OFF); if (!head->qh_next.qh) {
u32 cmd = readl(&oxu->regs->command);
if (!(cmd & CMD_ASE)) { /* in case a clear of CMD_ASE didn't take yet */
(void)handshake(oxu, &oxu->regs->status,
STS_ASS, 0, 150);
cmd |= CMD_ASE | CMD_RUN;
writel(cmd, &oxu->regs->command);
oxu_to_hcd(oxu)->state = HC_STATE_RUNNING; /* posted write need not be known to HC yet ... */
}
}
/* clear halt and/or toggle; and maybe recover from silicon quirk */ if (qh->qh_state == QH_STATE_IDLE)
qh_refresh(oxu, qh);
/* splice right after start */
qh->qh_next = head->qh_next;
qh->hw_next = head->hw_next;
wmb();
head->qh_next.qh = qh;
head->hw_next = dma;
qh->qh_state = QH_STATE_LINKED; /* qtd completions reported later by interrupt */
}
#define QH_ADDR_MASK cpu_to_le32(0x7f)
/* * For control/bulk/interrupt, return QH with these TDs appended. * Allocates and initializes the QH if necessary. * Returns null if it can't allocate a QH it needs to. * If the QH has TDs (urbs) already, that's great.
*/ staticstruct ehci_qh *qh_append_tds(struct oxu_hcd *oxu, struct urb *urb, struct list_head *qtd_list, int epnum, void **ptr)
{ struct ehci_qh *qh = NULL;
qh = (struct ehci_qh *) *ptr; if (unlikely(qh == NULL)) { /* can't sleep here, we have oxu->lock... */
qh = qh_make(oxu, urb, GFP_ATOMIC);
*ptr = qh;
} if (likely(qh != NULL)) { struct ehci_qtd *qtd;
/* control qh may need patching ... */ if (unlikely(epnum == 0)) {
/* usb_reset_device() briefly reverts to address 0 */ if (usb_pipedevice(urb->pipe) == 0)
qh->hw_info1 &= ~QH_ADDR_MASK;
}
/* just one way to queue requests: swap with the dummy qtd. * only hc or qh_refresh() ever modify the overlay.
*/ if (likely(qtd != NULL)) { struct ehci_qtd *dummy;
dma_addr_t dma;
__le32 token;
/* to avoid racing the HC, use the dummy td instead of * the first td of our list (becomes new dummy). both * tds stay deactivated until we're done, when the * HC is allowed to fetch the old dummy (4.10.2).
*/
token = qtd->hw_token;
qtd->hw_token = HALT_BIT;
wmb();
dummy = qh->dummy;
/* hc must see the new dummy at list end */
dma = qtd->qtd_dma;
qtd = list_entry(qh->qtd_list.prev, struct ehci_qtd, qtd_list);
qtd->hw_next = QTD_NEXT(dma);
/* let the hc process these next qtds */
dummy->hw_token = (token & ~(0x80));
wmb();
dummy->hw_token = token;
/* Control/bulk operations through TTs don't need scheduling, * the HC and TT handle it when the TT has a buffer ready.
*/ if (likely(qh->qh_state == QH_STATE_IDLE))
qh_link_async(oxu, qh_get(qh));
done:
spin_unlock_irqrestore(&oxu->lock, flags); if (unlikely(qh == NULL))
qtd_list_free(oxu, urb, qtd_list); return rc;
}
/* The async qh for the qtds being reclaimed are now unlinked from the HC */
/* other unlink(s) may be pending (in QH_STATE_UNLINK_WAIT) */
next = qh->reclaim;
oxu->reclaim = next;
oxu->reclaim_ready = 0;
qh->reclaim = NULL;
qh_completions(oxu, qh);
if (!list_empty(&qh->qtd_list)
&& HC_IS_RUNNING(oxu_to_hcd(oxu)->state))
qh_link_async(oxu, qh); else {
qh_put(qh); /* refcount from async list */
/* it's not free to turn the async schedule on/off; leave it * active but idle for a while once it empties.
*/ if (HC_IS_RUNNING(oxu_to_hcd(oxu)->state)
&& oxu->async->qh_next.qh == NULL)
timer_action(oxu, TIMER_ASYNC_OFF);
}
if (next) {
oxu->reclaim = NULL;
start_unlink_async(oxu, next);
}
}
/* makes sure the async qh will become idle */ /* caller must own oxu->lock */
/* stop async schedule right now? */ if (unlikely(qh == oxu->async)) { /* can't get here without STS_ASS set */ if (oxu_to_hcd(oxu)->state != HC_STATE_HALT
&& !oxu->reclaim) { /* ... and CMD_IAAD clear */
writel(cmd & ~CMD_ASE, &oxu->regs->command);
wmb(); /* handshake later, if we need to */
timer_action_done(oxu, TIMER_ASYNC_OFF);
} return;
}
if (unlikely(oxu_to_hcd(oxu)->state == HC_STATE_HALT)) { /* if (unlikely(qh->reclaim != 0)) * this will recurse, probably not much
*/
end_unlink_async(oxu); return;
}
if (!++(oxu->stamp))
oxu->stamp++;
timer_action_done(oxu, TIMER_ASYNC_SHRINK);
rescan:
qh = oxu->async->qh_next.qh; if (likely(qh != NULL)) { do { /* clean any finished work for this qh */ if (!list_empty(&qh->qtd_list)
&& qh->stamp != oxu->stamp) { int temp;
/* unlinks could happen here; completion * reporting drops the lock. rescan using * the latest schedule, but don't rescan * qhs we already finished (no looping).
*/
qh = qh_get(qh);
qh->stamp = oxu->stamp;
temp = qh_completions(oxu, qh);
qh_put(qh); if (temp != 0) goto rescan;
}
/* unlink idle entries, reducing HC PCI usage as well * as HCD schedule-scanning costs. delay for any qh * we just scanned, there's a not-unusual case that it * doesn't stay idle for long. * (plus, avoids some kind of re-activation race.)
*/ if (list_empty(&qh->qtd_list)) { if (qh->stamp == oxu->stamp)
action = TIMER_ASYNC_SHRINK; elseif (!oxu->reclaim
&& qh->qh_state == QH_STATE_LINKED)
start_unlink_async(oxu, qh);
}
qh = qh->qh_next.qh;
} while (qh);
} if (action == TIMER_ASYNC_SHRINK)
timer_action(oxu, TIMER_ASYNC_SHRINK);
}
/* * periodic_next_shadow - return "next" pointer on shadow list * @periodic: host pointer to qh/itd/sitd * @tag: hardware tag for type of this record
*/ staticunion ehci_shadow *periodic_next_shadow(union ehci_shadow *periodic,
__le32 tag)
{ switch (tag) { default: case Q_TYPE_QH: return &periodic->qh->qh_next;
}
}
/* caller must hold oxu->lock */ staticvoid periodic_unlink(struct oxu_hcd *oxu, unsigned frame, void *ptr)
{ union ehci_shadow *prev_p = &oxu->pshadow[frame];
__le32 *hw_p = &oxu->periodic[frame]; union ehci_shadow here = *prev_p;
/* find predecessor of "ptr"; hw and shadow lists are in sync */ while (here.ptr && here.ptr != ptr) {
prev_p = periodic_next_shadow(prev_p, Q_NEXT_TYPE(*hw_p));
hw_p = here.hw_next;
here = *prev_p;
} /* an interrupt entry (at list end) could have been shared */ if (!here.ptr) return;
/* update shadow and hardware lists ... the old "next" pointers * from ptr may still be in use, the caller updates them.
*/
*prev_p = *periodic_next_shadow(&here, Q_NEXT_TYPE(*hw_p));
*hw_p = *here.hw_next;
}
/* how many of the uframe's 125 usecs are allocated? */ staticunsignedshort periodic_usecs(struct oxu_hcd *oxu, unsigned frame, unsigned uframe)
{
__le32 *hw_p = &oxu->periodic[frame]; union ehci_shadow *q = &oxu->pshadow[frame]; unsigned usecs = 0;
while (q->ptr) { switch (Q_NEXT_TYPE(*hw_p)) { case Q_TYPE_QH: default: /* is it in the S-mask? */ if (q->qh->hw_info2 & cpu_to_le32(1 << uframe))
usecs += q->qh->usecs; /* ... or C-mask? */ if (q->qh->hw_info2 & cpu_to_le32(1 << (8 + uframe)))
usecs += q->qh->c_usecs;
hw_p = &q->qh->hw_next;
q = &q->qh->qh_next; break;
}
} #ifdef DEBUG if (usecs > 100)
oxu_err(oxu, "uframe %d sched overrun: %d usecs\n",
frame * 8 + uframe, usecs); #endif return usecs;
}
staticint enable_periodic(struct oxu_hcd *oxu)
{
u32 cmd; int status;
/* did clearing PSE did take effect yet? * takes effect only at frame boundaries...
*/
status = handshake(oxu, &oxu->regs->status, STS_PSS, 0, 9 * 125); if (status != 0) {
oxu_to_hcd(oxu)->state = HC_STATE_HALT;
usb_hc_died(oxu_to_hcd(oxu)); return status;
}
/* make sure ehci_work scans these */
oxu->next_uframe = readl(&oxu->regs->frame_index)
% (oxu->periodic_size << 3); return 0;
}
staticint disable_periodic(struct oxu_hcd *oxu)
{
u32 cmd; int status;
/* did setting PSE not take effect yet? * takes effect only at frame boundaries...
*/
status = handshake(oxu, &oxu->regs->status, STS_PSS, STS_PSS, 9 * 125); if (status != 0) {
oxu_to_hcd(oxu)->state = HC_STATE_HALT;
usb_hc_died(oxu_to_hcd(oxu)); return status;
}
/* periodic schedule slots have iso tds (normal or split) first, then a * sparse tree for active interrupt transfers. * * this just links in a qh; caller guarantees uframe masks are set right. * no FSTN support (yet; oxu 0.96+)
*/ staticint qh_link_periodic(struct oxu_hcd *oxu, struct ehci_qh *qh)
{ unsigned i; unsigned period = qh->period;
/* high bandwidth, or otherwise every microframe */ if (period == 0)
period = 1;
for (i = qh->start; i < oxu->periodic_size; i += period) { union ehci_shadow *prev = &oxu->pshadow[i];
__le32 *hw_p = &oxu->periodic[i]; union ehci_shadow here = *prev;
__le32 type = 0;
/* skip the iso nodes at list head */ while (here.ptr) {
type = Q_NEXT_TYPE(*hw_p); if (type == Q_TYPE_QH) break;
prev = periodic_next_shadow(prev, type);
hw_p = &here.qh->hw_next;
here = *prev;
}
/* sorting each branch by period (slow-->fast) * enables sharing interior tree nodes
*/ while (here.ptr && qh != here.qh) { if (qh->period > here.qh->period) break;
prev = &here.qh->qh_next;
hw_p = &here.qh->hw_next;
here = *prev;
} /* link in this qh, unless some earlier pass did that */ if (qh != here.qh) {
qh->qh_next = here; if (here.qh)
qh->hw_next = *hw_p;
wmb();
prev->qh = qh;
*hw_p = QH_NEXT(qh->qh_dma);
}
}
qh->qh_state = QH_STATE_LINKED;
qh_get(qh);
/* FIXME: * IF this isn't high speed * and this qh is active in the current uframe * (and overlay token SplitXstate is false?) * THEN * qh->hw_info1 |= cpu_to_le32(1 << 7 "ignore");
*/
/* high bandwidth, or otherwise part of every microframe */
period = qh->period; if (period == 0)
period = 1;
for (i = qh->start; i < oxu->periodic_size; i += period)
periodic_unlink(oxu, i, qh);
/* simple/paranoid: always delay, expecting the HC needs to read * qh->hw_next or finish a writeback after SPLIT/CSPLIT ... and * expect hub_wq to clean up after any CSPLITs we won't issue. * active high speed queues may need bigger delays...
*/ if (list_empty(&qh->qtd_list)
|| (cpu_to_le32(QH_CMASK) & qh->hw_info2) != 0)
wait = 2; else
wait = 55; /* worst case: 3 * 1024 */
/* complete split running into next frame? * given FSTN support, we could sometimes check...
*/ if (uframe >= 8) return 0;
/* * 80% periodic == 100 usec/uframe available * convert "usecs we need" to "max already claimed"
*/
usecs = 100 - usecs;
/* we "know" 2 and 4 uframe intervals were rejected; so * for period 0, check _every_ microframe in the schedule.
*/ if (unlikely(period == 0)) { do { for (uframe = 0; uframe < 7; uframe++) {
claimed = periodic_usecs(oxu, frame, uframe); if (claimed > usecs) return 0;
}
} while ((frame += 1) < oxu->periodic_size);
/* just check the specified uframe, at that period */
} else { do {
claimed = periodic_usecs(oxu, frame, uframe); if (claimed > usecs) return 0;
} while ((frame += period) < oxu->periodic_size);
}
if (!check_period(oxu, frame, uframe, qh->period, qh->usecs)) goto done; if (!qh->c_usecs) {
retval = 0;
*c_maskp = 0; goto done;
}
done: return retval;
}
/* "first fit" scheduling policy used the first time through, * or when the previous schedule slot can't be re-used.
*/ staticint qh_schedule(struct oxu_hcd *oxu, struct ehci_qh *qh)
{ int status; unsigned uframe;
__le32 c_mask; unsigned frame; /* 0..(qh->period - 1), or NO_FRAME */
/* reuse the previous schedule slots, if we can */ if (frame < qh->period) {
uframe = ffs(le32_to_cpup(&qh->hw_info2) & QH_SMASK);
status = check_intr_schedule(oxu, frame, --uframe,
qh, &c_mask);
} else {
uframe = 0;
c_mask = 0;
status = -ENOSPC;
}
/* else scan the schedule to find a group of slots such that all * uframes have enough periodic bandwidth available.
*/ if (status) { /* "normal" case, uframing flexible except with splits */ if (qh->period) {
frame = qh->period - 1; do { for (uframe = 0; uframe < 8; uframe++) {
status = check_intr_schedule(oxu,
frame, uframe, qh,
&c_mask); if (status == 0) break;
}
} while (status && frame--);
/* qh->period == 0 means every uframe */
} else {
frame = 0;
status = check_intr_schedule(oxu, 0, 0, qh, &c_mask);
} if (status) goto done;
qh->start = frame;
/* get endpoint and transfer/schedule data */
epnum = urb->ep->desc.bEndpointAddress;
spin_lock_irqsave(&oxu->lock, flags);
if (unlikely(!HCD_HW_ACCESSIBLE(oxu_to_hcd(oxu)))) {
status = -ESHUTDOWN; goto done;
}
/* get qh and force any scheduling errors */
INIT_LIST_HEAD(&empty);
qh = qh_append_tds(oxu, urb, &empty, epnum, &urb->ep->hcpriv); if (qh == NULL) {
status = -ENOMEM; goto done;
} if (qh->qh_state == QH_STATE_IDLE) {
status = qh_schedule(oxu, qh); if (status != 0) goto done;
}
/* then queue the urb's tds to the qh */
qh = qh_append_tds(oxu, urb, qtd_list, epnum, &urb->ep->hcpriv);
BUG_ON(qh == NULL);
/* * When running, scan from last scan point up to "now" * else clean up by scanning everything that's left. * Touches as few pages as possible: cache-friendly.
*/
now_uframe = oxu->next_uframe; if (HC_IS_RUNNING(oxu_to_hcd(oxu)->state))
clock = readl(&oxu->regs->frame_index); else
clock = now_uframe + mod - 1;
clock %= mod;
for (;;) { union ehci_shadow q, *q_p;
__le32 type, *hw_p;
/* don't scan past the live uframe */
frame = now_uframe >> 3; if (frame != (clock >> 3)) { /* safe to scan the whole frame at once */
now_uframe |= 0x07;
}
restart: /* scan each element in frame's queue for completions */
q_p = &oxu->pshadow[frame];
hw_p = &oxu->periodic[frame];
q.ptr = q_p->ptr;
type = Q_NEXT_TYPE(*hw_p);
modified = 0;
while (q.ptr != NULL) { union ehci_shadow temp;
switch (type) { case Q_TYPE_QH: /* handle any completions */
temp.qh = qh_get(q.qh);
type = Q_NEXT_TYPE(q.qh->hw_next);
q = q.qh->qh_next;
modified = qh_completions(oxu, temp.qh); if (unlikely(list_empty(&temp.qh->qtd_list)))
intr_deschedule(oxu, temp.qh);
qh_put(temp.qh); break; default:
oxu_dbg(oxu, "corrupt type %d frame %d shadow %p\n",
type, frame, q.ptr);
q.ptr = NULL;
}
/* assume completion callbacks modify the queue */ if (unlikely(modified)) goto restart;
}
/* Stop when we catch up to the HC */
/* FIXME: this assumes we won't get lapped when * latencies climb; that should be rare, but... * detect it, and just go all the way around. * FLR might help detect this case, so long as latencies * don't exceed periodic_size msec (default 1.024 sec).
*/
/* FIXME: likewise assumes HC doesn't halt mid-scan */
if (now_uframe == clock) { unsigned now;
if (!HC_IS_RUNNING(oxu_to_hcd(oxu)->state)) break;
oxu->next_uframe = now_uframe;
now = readl(&oxu->regs->frame_index) % mod; if (now_uframe == now) break;
/* rescan the rest of this frame, then ... */
clock = now;
} else {
now_uframe++;
now_uframe %= mod;
}
}
}
/* On some systems, leaving remote wakeup enabled prevents system shutdown. * The firmware seems to think that powering off is a wakeup event! * This routine turns off remote wakeup and everything else, on all ports.
*/ staticvoid ehci_turn_off_all_ports(struct oxu_hcd *oxu)
{ int port = HCS_N_PORTS(oxu->hcs_params);
while (port--)
writel(PORT_RWC_BITS, &oxu->regs->port_status[port]);
}
staticvoid ehci_port_power(struct oxu_hcd *oxu, int is_on)
{ unsigned port;
if (!HCS_PPC(oxu->hcs_params)) return;
oxu_dbg(oxu, "...power%s ports...\n", str_up_down(is_on)); for (port = HCS_N_PORTS(oxu->hcs_params); port > 0; ) { if (is_on)
oxu_hub_control(oxu_to_hcd(oxu), SetPortFeature,
USB_PORT_FEAT_POWER, port--, NULL, 0); else
oxu_hub_control(oxu_to_hcd(oxu), ClearPortFeature,
USB_PORT_FEAT_POWER, port--, NULL, 0);
}
msleep(20);
}
/* Called from some interrupts, timers, and so on. * It calls driver completion functions, after dropping oxu->lock.
*/ staticvoid ehci_work(struct oxu_hcd *oxu)
{
timer_action_done(oxu, TIMER_IO_WATCHDOG); if (oxu->reclaim_ready)
end_unlink_async(oxu);
/* another CPU may drop oxu->lock during a schedule scan while * it reports urb completions. this flag guards against bogus * attempts at re-entrant schedule scanning.
*/ if (oxu->scanning) return;
oxu->scanning = 1;
scan_async(oxu); if (oxu->next_uframe != -1)
scan_periodic(oxu);
oxu->scanning = 0;
/* the IO watchdog guards against hardware or driver bugs that * misplace IRQs, and should let us run completely without IRQs. * such lossage has been observed on both VT6202 and VT8235.
*/ if (HC_IS_RUNNING(oxu_to_hcd(oxu)->state) &&
(oxu->async->qh_next.ptr != NULL ||
oxu->periodic_sched != 0))
timer_action(oxu, TIMER_IO_WATCHDOG);
}
staticvoid unlink_async(struct oxu_hcd *oxu, struct ehci_qh *qh)
{ /* if we need to use IAA and it's busy, defer */ if (qh->qh_state == QH_STATE_LINKED
&& oxu->reclaim
&& HC_IS_RUNNING(oxu_to_hcd(oxu)->state)) { struct ehci_qh *last;
for (last = oxu->reclaim;
last->reclaim;
last = last->reclaim) continue;
qh->qh_state = QH_STATE_UNLINK_WAIT;
last->reclaim = qh;
/* bypass IAA if the hc can't care */
} elseif (!HC_IS_RUNNING(oxu_to_hcd(oxu)->state) && oxu->reclaim)
end_unlink_async(oxu);
/* something else might have unlinked the qh by now */ if (qh->qh_state == QH_STATE_LINKED)
start_unlink_async(oxu, qh);
}
/* lost IAA irqs wedge things badly; seen with a vt8235 */ if (oxu->reclaim) {
u32 status = readl(&oxu->regs->status); if (status & STS_IAA) {
oxu_vdbg(oxu, "lost IAA\n");
writel(STS_IAA, &oxu->regs->status);
oxu->reclaim_ready = 1;
}
}
/* stop async processing after it's idled a bit */ if (test_bit(TIMER_ASYNC_OFF, &oxu->actions))
start_unlink_async(oxu, oxu->async);
/* oxu could run by timer, without IRQs ... */
ehci_work(oxu);
spin_unlock_irqrestore(&oxu->lock, flags);
}
/* One-time init, only for memory state.
*/ staticint oxu_hcd_init(struct usb_hcd *hcd)
{ struct oxu_hcd *oxu = hcd_to_oxu(hcd);
u32 temp; int retval;
u32 hcc_params;
spin_lock_init(&oxu->lock);
timer_setup(&oxu->watchdog, oxu_watchdog, 0);
/* * hw default: 1K periodic list heads, one per frame. * periodic_size can shrink by USBCMD update if hcc_params allows.
*/
oxu->periodic_size = DEFAULT_I_TDPS;
retval = ehci_mem_init(oxu, GFP_KERNEL); if (retval < 0) return retval;
/* controllers may cache some of the periodic schedule ... */
hcc_params = readl(&oxu->caps->hcc_params); if (HCC_ISOC_CACHE(hcc_params)) /* full frame cache */
oxu->i_thresh = 8; else/* N microframes cached */
oxu->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
/* * dedicate a qh for the async ring head, since we couldn't unlink * a 'real' qh without stopping the async schedule [4.8]. use it * as the 'reclamation list head' too. * its dummy is used in hw_alt_next of many tds, to prevent the qh * from automatically advancing to the next td after short reads.
*/
oxu->async->qh_next.qh = NULL;
oxu->async->hw_next = QH_NEXT(oxu->async->qh_dma);
oxu->async->hw_info1 = cpu_to_le32(QH_HEAD);
oxu->async->hw_token = cpu_to_le32(QTD_STS_HALT);
oxu->async->hw_qtd_next = EHCI_LIST_END;
oxu->async->qh_state = QH_STATE_LINKED;
oxu->async->hw_alt_next = QTD_NEXT(oxu->async->dummy->qtd_dma);
/* clear interrupt enables, set irq latency */ if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
log2_irq_thresh = 0;
temp = 1 << (16 + log2_irq_thresh); if (HCC_CANPARK(hcc_params)) { /* HW default park == 3, on hardware that supports it (like * NVidia and ALI silicon), maximizes throughput on the async * schedule by avoiding QH fetches between transfers. * * With fast usb storage devices and NForce2, "park" seems to * make problems: throughput reduction (!), data errors...
*/ if (park) {
park = min_t(unsignedint, park, 3);
temp |= CMD_PARK;
temp |= park << 8;
}
oxu_dbg(oxu, "park %d\n", park);
} if (HCC_PGM_FRAMELISTLEN(hcc_params)) { /* periodic schedule size can be smaller than default */
temp &= ~(3 << 2);
temp |= (EHCI_TUNE_FLS << 2);
}
oxu->command = temp;
return 0;
}
/* Called during probe() after chip reset completes.
*/ staticint oxu_reset(struct usb_hcd *hcd)
{ struct oxu_hcd *oxu = hcd_to_oxu(hcd);
/* hcc_params controls whether oxu->regs->segment must (!!!) * be used; it constrains QH/ITD/SITD and QTD locations. * dma_pool consistent memory always uses segment zero. * streaming mappings for I/O buffers, like dma_map_single(), * can return segments above 4GB, if the device allows. * * NOTE: the dma mask is visible through dev->dma_mask, so * drivers can pass this info along ... like NETIF_F_HIGHDMA, * Scsi_Host.highmem_io, and so forth. It's readonly to all * host side drivers though.
*/
hcc_params = readl(&oxu->caps->hcc_params); if (HCC_64BIT_ADDR(hcc_params))
writel(0, &oxu->regs->segment);
/* * Start, enabling full USB 2.0 functionality ... usb 1.1 devices * are explicitly handed to companion controller(s), so no TT is * involved with the root hub. (Except where one is integrated, * and there's no companion controller unless maybe for USB OTG.)
*/
hcd->state = HC_STATE_RUNNING;
writel(FLAG_CF, &oxu->regs->configured_flag);
readl(&oxu->regs->command); /* unblock posted writes */
/* let companion controllers work when we aren't */
writel(0, &oxu->regs->configured_flag);
/* root hub is shut down separately (first, when possible) */
spin_lock_irq(&oxu->lock); if (oxu->async)
ehci_work(oxu);
spin_unlock_irq(&oxu->lock);
ehci_mem_cleanup(oxu);
/* Kick in for silicon on any bus (not just pci, etc). * This forcibly disables dma and IRQs, helping kexec and other cases * where the next system software may expect clean state.
*/ staticvoid oxu_shutdown(struct usb_hcd *hcd)
{ struct oxu_hcd *oxu = hcd_to_oxu(hcd);
/* Non-error returns are a promise to giveback() the urb later * we drop ownership so next owner (or urb unlink) can get it * * urb + dev is in hcd.self.controller.urb_list * we're queueing TDs onto software and hardware lists * * hcd-specific init for hcpriv hasn't been done yet * * NOTE: control, bulk, and interrupt share the same code to append TDs * to a (possibly active) QH, and the same QH scanning code.
*/ staticint __oxu_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
gfp_t mem_flags)
{ struct oxu_hcd *oxu = hcd_to_oxu(hcd); struct list_head qtd_list;
INIT_LIST_HEAD(&qtd_list);
switch (usb_pipetype(urb->pipe)) { case PIPE_CONTROL: case PIPE_BULK: default: if (!qh_urb_transaction(oxu, urb, &qtd_list, mem_flags)) return -ENOMEM; return submit_async(oxu, urb, &qtd_list, mem_flags);
case PIPE_INTERRUPT: if (!qh_urb_transaction(oxu, urb, &qtd_list, mem_flags)) return -ENOMEM; return intr_submit(oxu, urb, &qtd_list, mem_flags);
case PIPE_ISOCHRONOUS: if (urb->dev->speed == USB_SPEED_HIGH) return itd_submit(oxu, urb, mem_flags); else return sitd_submit(oxu, urb, mem_flags);
}
}
/* This function is responsible for breaking URBs with big data size * into smaller size and processing small urbs in sequence.
*/ staticint oxu_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
gfp_t mem_flags)
{ struct oxu_hcd *oxu = hcd_to_oxu(hcd); int num, rem; void *transfer_buffer; struct urb *murb; int i, ret;
/* If not bulk pipe just enqueue the URB */ if (!usb_pipebulk(urb->pipe)) return __oxu_urb_enqueue(hcd, urb, mem_flags);
/* Otherwise we should verify the USB transfer buffer size! */
transfer_buffer = urb->transfer_buffer;
num = urb->transfer_buffer_length / 4096;
rem = urb->transfer_buffer_length % 4096; if (rem != 0)
num++;
/* If URB is smaller than 4096 bytes just enqueue it! */ if (num == 1) return __oxu_urb_enqueue(hcd, urb, mem_flags);
/* Ok, we have more job to do! :) */
for (i = 0; i < num - 1; i++) { /* Get free micro URB poll till a free urb is received */
do {
murb = (struct urb *) oxu_murb_alloc(oxu); if (!murb)
schedule();
} while (!murb);
/* Coping the urb */
memcpy(murb, urb, sizeof(struct urb));
murb->transfer_buffer_length = 4096;
murb->transfer_buffer = transfer_buffer + i * 4096;
/* Null pointer for the encodes that this is a micro urb */
murb->complete = NULL;
/* This loop is to guarantee urb to be processed when there's * not enough resources at a particular time by retrying.
*/ do {
ret = __oxu_urb_enqueue(hcd, murb, mem_flags); if (ret)
schedule();
} while (ret);
}
/* Last urb requires special handling */
/* Get free micro URB poll till a free urb is received */ do {
murb = (struct urb *) oxu_murb_alloc(oxu); if (!murb)
schedule();
} while (!murb);
/* Coping the urb */
memcpy(murb, urb, sizeof(struct urb));
murb->transfer_buffer_length = rem > 0 ? rem : 4096;
murb->transfer_buffer = transfer_buffer + (num - 1) * 4096;
/* Null pointer for the encodes that this is a micro urb */
murb->complete = NULL;
/* ASSERT: any requests/urbs are being unlinked */ /* ASSERT: nobody can be submitting urbs for this any more */
rescan:
spin_lock_irqsave(&oxu->lock, flags);
qh = ep->hcpriv; if (!qh) goto done;
/* endpoints can be iso streams. for now, we don't * accelerate iso completions ... so spin a while.
*/ if (qh->hw_info1 == 0) {
oxu_vdbg(oxu, "iso delay\n"); goto idle_timeout;
}
if (!HC_IS_RUNNING(hcd->state))
qh->qh_state = QH_STATE_IDLE; switch (qh->qh_state) { case QH_STATE_LINKED: for (tmp = oxu->async->qh_next.qh;
tmp && tmp != qh;
tmp = tmp->qh_next.qh) continue; /* periodic qh self-unlinks on empty */ if (!tmp) goto nogood;
unlink_async(oxu, qh);
fallthrough; case QH_STATE_UNLINK: /* wait for hw to finish? */
idle_timeout:
spin_unlock_irqrestore(&oxu->lock, flags);
schedule_timeout_uninterruptible(1); goto rescan; case QH_STATE_IDLE: /* fully unlinked */ if (list_empty(&qh->qtd_list)) {
qh_put(qh); break;
}
fallthrough; default:
nogood: /* caller was supposed to have unlinked any requests; * that's not our job. just leak this memory.
*/
oxu_err(oxu, "qh %p (#%02x) state %d%s\n",
qh, ep->desc.bEndpointAddress, qh->qh_state,
list_empty(&qh->qtd_list) ? "" : "(has tds)"); break;
}
ep->hcpriv = NULL;
done:
spin_unlock_irqrestore(&oxu->lock, flags);
}
/* Build "status change" packet (one or two bytes) from HC registers */ staticint oxu_hub_status_data(struct usb_hcd *hcd, char *buf)
{ struct oxu_hcd *oxu = hcd_to_oxu(hcd);
u32 temp, mask, status = 0; int ports, i, retval = 1; unsignedlong flags;
/* if !PM, root hub timers won't get shut down ... */ if (!HC_IS_RUNNING(hcd->state)) return 0;
/* init status to no-changes */
buf[0] = 0;
ports = HCS_N_PORTS(oxu->hcs_params); if (ports > 7) {
buf[1] = 0;
retval++;
}
/* Some boards (mostly VIA?) report bogus overcurrent indications, * causing massive log spam unless we completely ignore them. It * may be relevant that VIA VT8235 controllers, where PORT_POWER is * always set, seem to clear PORT_OCC and PORT_CSC when writing to * PORT_POWER; that's surprising, but maybe within-spec.
*/ if (!ignore_oc)
mask = PORT_CSC | PORT_PEC | PORT_OCC; else
mask = PORT_CSC | PORT_PEC;
/* no hub change reports (bit 0) for now (power, ...) */
/* port N changes (bit N)? */
spin_lock_irqsave(&oxu->lock, flags); for (i = 0; i < ports; i++) {
temp = readl(&oxu->regs->port_status[i]);
/* * Return status information even for ports with OWNER set. * Otherwise hub_wq wouldn't see the disconnect event when a * high-speed device is switched over to the companion * controller by the user.
*/
if (!(temp & PORT_CONNECT))
oxu->reset_done[i] = 0; if ((temp & mask) != 0 || ((temp & PORT_RESUME) != 0 &&
time_after_eq(jiffies, oxu->reset_done[i]))) { if (i < 7)
buf[0] |= 1 << (i + 1); else
buf[1] |= 1 << (i - 7);
status = STS_PCD;
}
} /* FIXME autosuspend idle root hubs */
spin_unlock_irqrestore(&oxu->lock, flags); return status ? retval : 0;
}
/* Returns the speed of a device attached to a port on the root hub. */ staticinlineunsignedint oxu_port_speed(struct oxu_hcd *oxu, unsignedint portsc)
{ switch ((portsc >> 26) & 3) { case 0: return 0; case 1: return USB_PORT_STAT_LOW_SPEED; case 2: default: return USB_PORT_STAT_HIGH_SPEED;
}
}
/* * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR. * HCS_INDICATOR may say we can change LEDs to off/amber/green. * (track current state ourselves) ... blink for diagnostics, * power, "this is the one", etc. EHCI spec supports this.
*/
spin_lock_irqsave(&oxu->lock, flags); switch (typeReq) { case ClearHubFeature: switch (wValue) { case C_HUB_LOCAL_POWER: case C_HUB_OVER_CURRENT: /* no hub-wide feature/status flags */ break; default: goto error;
} break; case ClearPortFeature: if (!wIndex || wIndex > ports) goto error;
wIndex--;
temp = readl(status_reg);
/* * Even if OWNER is set, so the port is owned by the * companion controller, hub_wq needs to be able to clear * the port-change status bits (especially * USB_PORT_STAT_C_CONNECTION).
*/
switch (wValue) { case USB_PORT_FEAT_ENABLE:
writel(temp & ~PORT_PE, status_reg); break; case USB_PORT_FEAT_C_ENABLE:
writel((temp & ~PORT_RWC_BITS) | PORT_PEC, status_reg); break; case USB_PORT_FEAT_SUSPEND: if (temp & PORT_RESET) goto error; if (temp & PORT_SUSPEND) { if ((temp & PORT_PE) == 0) goto error; /* resume signaling for 20 msec */
temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
writel(temp | PORT_RESUME, status_reg);
oxu->reset_done[wIndex] = jiffies
+ msecs_to_jiffies(20);
} break; case USB_PORT_FEAT_C_SUSPEND: /* we auto-clear this feature */ break; case USB_PORT_FEAT_POWER: if (HCS_PPC(oxu->hcs_params))
writel(temp & ~(PORT_RWC_BITS | PORT_POWER),
status_reg); break; case USB_PORT_FEAT_C_CONNECTION:
writel((temp & ~PORT_RWC_BITS) | PORT_CSC, status_reg); break; case USB_PORT_FEAT_C_OVER_CURRENT:
writel((temp & ~PORT_RWC_BITS) | PORT_OCC, status_reg); break; case USB_PORT_FEAT_C_RESET: /* GetPortStatus clears reset */ break; default: goto error;
}
readl(&oxu->regs->command); /* unblock posted write */ break; case GetHubDescriptor:
ehci_hub_descriptor(oxu, (struct usb_hub_descriptor *)
buf); break; case GetHubStatus: /* no hub-wide feature/status flags */
memset(buf, 0, 4); break; case GetPortStatus: if (!wIndex || wIndex > ports) goto error;
wIndex--;
status = 0;
temp = readl(status_reg);
/* wPortChange bits */ if (temp & PORT_CSC)
status |= USB_PORT_STAT_C_CONNECTION << 16; if (temp & PORT_PEC)
status |= USB_PORT_STAT_C_ENABLE << 16; if ((temp & PORT_OCC) && !ignore_oc)
status |= USB_PORT_STAT_C_OVERCURRENT << 16;
/* whoever resumes must GetPortStatus to complete it!! */ if (temp & PORT_RESUME) {
/* Remote Wakeup received? */ if (!oxu->reset_done[wIndex]) { /* resume signaling for 20 msec */
oxu->reset_done[wIndex] = jiffies
+ msecs_to_jiffies(20); /* check the port again */
mod_timer(&oxu_to_hcd(oxu)->rh_timer,
oxu->reset_done[wIndex]);
}
/* whoever resets must GetPortStatus to complete it!! */ if ((temp & PORT_RESET)
&& time_after_eq(jiffies,
oxu->reset_done[wIndex])) {
status |= USB_PORT_STAT_C_RESET << 16;
oxu->reset_done[wIndex] = 0;
/* force reset to complete */
writel(temp & ~(PORT_RWC_BITS | PORT_RESET),
status_reg); /* REVISIT: some hardware needs 550+ usec to clear * this bit; seems too long to spin routinely...
*/
retval = handshake(oxu, status_reg,
PORT_RESET, 0, 750); if (retval != 0) {
oxu_err(oxu, "port %d reset error %d\n",
wIndex + 1, retval); goto error;
}
/* see what we found out */
temp = check_reset_complete(oxu, wIndex, status_reg,
readl(status_reg));
}
/* transfer dedicated ports to the companion hc */ if ((temp & PORT_CONNECT) &&
test_bit(wIndex, &oxu->companion_ports)) {
temp &= ~PORT_RWC_BITS;
temp |= PORT_OWNER;
writel(temp, status_reg);
oxu_dbg(oxu, "port %d --> companion\n", wIndex + 1);
temp = readl(status_reg);
}
/* * Even if OWNER is set, there's no harm letting hub_wq * see the wPortStatus values (they should all be 0 except * for PORT_POWER anyway).
*/
if (temp & PORT_CONNECT) {
status |= USB_PORT_STAT_CONNECTION; /* status may be from integrated TT */
status |= oxu_port_speed(oxu, temp);
} if (temp & PORT_PE)
status |= USB_PORT_STAT_ENABLE; if (temp & (PORT_SUSPEND|PORT_RESUME))
status |= USB_PORT_STAT_SUSPEND; if (temp & PORT_OC)
status |= USB_PORT_STAT_OVERCURRENT; if (temp & PORT_RESET)
status |= USB_PORT_STAT_RESET; if (temp & PORT_POWER)
status |= USB_PORT_STAT_POWER;
#ifndef OXU_VERBOSE_DEBUG if (status & ~0xffff) /* only if wPortChange is interesting */ #endif
dbg_port(oxu, "GetStatus", wIndex + 1, temp);
put_unaligned(cpu_to_le32(status), (__le32 *) buf); break; case SetHubFeature: switch (wValue) { case C_HUB_LOCAL_POWER: case C_HUB_OVER_CURRENT: /* no hub-wide feature/status flags */ break; default: goto error;
} break; case SetPortFeature:
selector = wIndex >> 8;
wIndex &= 0xff; if (!wIndex || wIndex > ports) goto error;
wIndex--;
temp = readl(status_reg); if (temp & PORT_OWNER) break;
temp &= ~PORT_RWC_BITS; switch (wValue) { case USB_PORT_FEAT_SUSPEND: if ((temp & PORT_PE) == 0
|| (temp & PORT_RESET) != 0) goto error; if (device_may_wakeup(&hcd->self.root_hub->dev))
temp |= PORT_WAKE_BITS;
writel(temp | PORT_SUSPEND, status_reg); break; case USB_PORT_FEAT_POWER: if (HCS_PPC(oxu->hcs_params))
writel(temp | PORT_POWER, status_reg); break; case USB_PORT_FEAT_RESET: if (temp & PORT_RESUME) goto error; /* line status bits may report this as low speed, * which can be fine if this root hub has a * transaction translator built in.
*/
oxu_vdbg(oxu, "port %d reset\n", wIndex + 1);
temp |= PORT_RESET;
temp &= ~PORT_PE;
/* * caller must wait, then call GetPortStatus * usb 2.0 spec says 50 ms resets on root
*/
oxu->reset_done[wIndex] = jiffies
+ msecs_to_jiffies(50);
writel(temp, status_reg); break;
/* For downstream facing ports (these): one hub port is put * into test mode according to USB2 11.24.2.13, then the hub * must be reset (which for root hub now means rmmod+modprobe, * or else system reboot). See EHCI 2.3.9 and 4.14 for info * about the EHCI-specific stuff.
*/ case USB_PORT_FEAT_TEST: if (!selector || selector > 5) goto error;
ehci_quiesce(oxu);
ehci_halt(oxu);
temp |= selector << 16;
writel(temp, status_reg); break;
staticint oxu_bus_suspend(struct usb_hcd *hcd)
{ struct oxu_hcd *oxu = hcd_to_oxu(hcd); int port; int mask;
oxu_dbg(oxu, "suspend root hub\n");
if (time_before(jiffies, oxu->next_statechange))
msleep(5);
port = HCS_N_PORTS(oxu->hcs_params);
spin_lock_irq(&oxu->lock);
/* stop schedules, clean any completed work */ if (HC_IS_RUNNING(hcd->state)) {
ehci_quiesce(oxu);
hcd->state = HC_STATE_QUIESCING;
}
oxu->command = readl(&oxu->regs->command); if (oxu->reclaim)
oxu->reclaim_ready = 1;
ehci_work(oxu);
/* Unlike other USB host controller types, EHCI doesn't have * any notion of "global" or bus-wide suspend. The driver has * to manually suspend all the active unsuspended ports, and * then manually resume them in the bus_resume() routine.
*/
oxu->bus_suspended = 0; while (port--) {
u32 __iomem *reg = &oxu->regs->port_status[port];
u32 t1 = readl(reg) & ~PORT_RWC_BITS;
u32 t2 = t1;
/* keep track of which ports we suspend */ if ((t1 & PORT_PE) && !(t1 & PORT_OWNER) &&
!(t1 & PORT_SUSPEND)) {
t2 |= PORT_SUSPEND;
set_bit(port, &oxu->bus_suspended);
}
/* enable remote wakeup on all ports */ if (device_may_wakeup(&hcd->self.root_hub->dev))
t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E; else
t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
if (t1 != t2) {
oxu_vdbg(oxu, "port %d, %08x -> %08x\n",
port + 1, t1, t2);
writel(t2, reg);
}
}
spin_unlock_irq(&oxu->lock); /* turn off now-idle HC */
timer_delete_sync(&oxu->watchdog);
spin_lock_irq(&oxu->lock);
ehci_halt(oxu);
hcd->state = HC_STATE_SUSPENDED;
/* Caller has locked the root hub, and should reset/reinit on error */ staticint oxu_bus_resume(struct usb_hcd *hcd)
{ struct oxu_hcd *oxu = hcd_to_oxu(hcd);
u32 temp; int i;
if (time_before(jiffies, oxu->next_statechange))
msleep(5);
spin_lock_irq(&oxu->lock);
/* Ideally and we've got a real resume here, and no port's power * was lost. (For PCI, that means Vaux was maintained.) But we * could instead be restoring a swsusp snapshot -- so that BIOS was * the last user of the controller, not reset/pm hardware keeping * state we gave to it.
*/
temp = readl(&oxu->regs->intr_enable);
oxu_dbg(oxu, "resume root hub%s\n", temp ? "" : " after power loss");
/* at least some APM implementations will try to deliver * IRQs right away, so delay them until we're ready.
*/
writel(0, &oxu->regs->intr_enable);
/* Read controller signature register to find a match */
id = oxu_readl(base, OXU_DEVICEID);
dev_info(&pdev->dev, "device ID %x\n", id); if ((id & OXU_REV_MASK) != (OXU_REV_2100 << OXU_REV_SHIFT)) return -1;
dev_info(&pdev->dev, "found device %x %s (%04x:%04x)\n",
id >> OXU_REV_SHIFT,
bo[(id & OXU_BO_MASK) >> OXU_BO_SHIFT],
(id & OXU_MAJ_REV_MASK) >> OXU_MAJ_REV_SHIFT,
(id & OXU_MIN_REV_MASK) >> OXU_MIN_REV_SHIFT);
/* * Get the platform resources
*/
irq = platform_get_irq(pdev, 0); if (irq < 0) return irq;
dev_dbg(&pdev->dev, "IRQ resource %d\n", irq);
base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); if (IS_ERR(base)) {
ret = PTR_ERR(base); goto error;
}
memstart = res->start;
memlen = resource_size(res);
ret = irq_set_irq_type(irq, IRQF_TRIGGER_FALLING); if (ret) {
dev_err(&pdev->dev, "error setting irq type\n");
ret = -EFAULT; goto error;
}
/* Allocate a driver data struct to hold useful info for both * SPH & OTG devices
*/
info = devm_kzalloc(&pdev->dev, sizeof(struct oxu_info), GFP_KERNEL); if (!info) {
ret = -EFAULT; goto error;
}
platform_set_drvdata(pdev, info);
ret = oxu_init(pdev, memstart, memlen, base, irq); if (ret < 0) {
dev_dbg(&pdev->dev, "cannot init USB devices\n"); goto error;
}
dev_info(&pdev->dev, "devices enabled and running\n");
platform_set_drvdata(pdev, info);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.