2017-11-03 18:28:30 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2006-01-23 02:32:37 +08:00
|
|
|
/*
|
|
|
|
* at91_udc -- driver for at91-series USB peripheral controller
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004 by Thomas Rathbone
|
|
|
|
* Copyright (C) 2005 by HP Labs
|
|
|
|
* Copyright (C) 2005 by David Brownell
|
|
|
|
*/
|
|
|
|
|
2008-01-06 05:21:43 +08:00
|
|
|
#undef VERBOSE_DEBUG
|
2006-01-23 02:32:37 +08:00
|
|
|
#undef PACKET_TRACE
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/ioport.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/proc_fs.h>
|
2011-05-29 16:01:48 +08:00
|
|
|
#include <linux/prefetch.h>
|
2006-01-23 02:32:37 +08:00
|
|
|
#include <linux/clk.h>
|
2006-12-17 07:34:53 +08:00
|
|
|
#include <linux/usb/ch9.h>
|
2007-10-05 09:05:17 +08:00
|
|
|
#include <linux/usb/gadget.h>
|
2012-01-28 22:35:36 +08:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_gpio.h>
|
2012-10-30 05:12:23 +08:00
|
|
|
#include <linux/platform_data/atmel.h>
|
2015-01-15 00:22:02 +08:00
|
|
|
#include <linux/regmap.h>
|
|
|
|
#include <linux/mfd/syscon.h>
|
|
|
|
#include <linux/mfd/syscon/atmel-matrix.h>
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
#include "at91_udc.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This controller is simple and PIO-only. It's used in many AT91-series
|
2006-07-05 17:38:56 +08:00
|
|
|
* full speed USB controllers, including the at91rm9200 (arm920T, with MMU),
|
|
|
|
* at91sam926x (arm926ejs, with MMU), and several no-mmu versions.
|
2006-01-23 02:32:37 +08:00
|
|
|
*
|
2012-08-22 17:40:21 +08:00
|
|
|
* This driver expects the board has been wired with two GPIOs supporting
|
2006-01-23 02:32:37 +08:00
|
|
|
* a VBUS sensing IRQ, and a D+ pullup. (They may be omitted, but the
|
2006-07-05 17:38:56 +08:00
|
|
|
* testing hasn't covered such cases.)
|
|
|
|
*
|
|
|
|
* The pullup is most important (so it's integrated on sam926x parts). It
|
2006-01-23 02:32:37 +08:00
|
|
|
* provides software control over whether the host enumerates the device.
|
2006-07-05 17:38:56 +08:00
|
|
|
*
|
2006-01-23 02:32:37 +08:00
|
|
|
* The VBUS sensing helps during enumeration, and allows both USB clocks
|
|
|
|
* (and the transceiver) to stay gated off until they're necessary, saving
|
2006-07-05 17:38:56 +08:00
|
|
|
* power. During USB suspend, the 48 MHz clock is gated off in hardware;
|
|
|
|
* it may also be gated off by software during some Linux sleep states.
|
2006-01-23 02:32:37 +08:00
|
|
|
*/
|
|
|
|
|
2006-07-05 17:38:56 +08:00
|
|
|
#define DRIVER_VERSION "3 May 2006"
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
static const char driver_name [] = "at91_udc";
|
2015-07-31 22:00:21 +08:00
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
const struct usb_ep_caps caps;
|
|
|
|
} ep_info[] = {
|
|
|
|
#define EP_INFO(_name, _caps) \
|
|
|
|
{ \
|
|
|
|
.name = _name, \
|
|
|
|
.caps = _caps, \
|
|
|
|
}
|
|
|
|
|
|
|
|
EP_INFO("ep0",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
|
|
|
|
EP_INFO("ep1",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
|
|
|
|
EP_INFO("ep2",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
|
|
|
|
EP_INFO("ep3-int",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_ALL)),
|
|
|
|
EP_INFO("ep4",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
|
|
|
|
EP_INFO("ep5",
|
|
|
|
USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
|
|
|
|
|
|
|
|
#undef EP_INFO
|
2015-01-15 00:22:04 +08:00
|
|
|
};
|
2015-07-31 22:00:21 +08:00
|
|
|
|
|
|
|
#define ep0name ep_info[0].name
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2010-07-13 12:09:16 +08:00
|
|
|
#define VBUS_POLL_TIMEOUT msecs_to_jiffies(1000)
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2010-03-02 01:01:56 +08:00
|
|
|
#define at91_udp_read(udc, reg) \
|
|
|
|
__raw_readl((udc)->udp_baseaddr + (reg))
|
|
|
|
#define at91_udp_write(udc, reg, val) \
|
|
|
|
__raw_writel((val), (udc)->udp_baseaddr + (reg))
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_USB_GADGET_DEBUG_FILES
|
|
|
|
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
|
|
|
|
static const char debug_filename[] = "driver/udc";
|
|
|
|
|
|
|
|
#define FOURBITS "%s%s%s%s"
|
|
|
|
#define EIGHTBITS FOURBITS FOURBITS
|
|
|
|
|
|
|
|
static void proc_ep_show(struct seq_file *s, struct at91_ep *ep)
|
|
|
|
{
|
|
|
|
static char *types[] = {
|
|
|
|
"control", "out-iso", "out-bulk", "out-int",
|
|
|
|
"BOGUS", "in-iso", "in-bulk", "in-int"};
|
|
|
|
|
|
|
|
u32 csr;
|
|
|
|
struct at91_request *req;
|
|
|
|
unsigned long flags;
|
2010-03-02 01:01:56 +08:00
|
|
|
struct at91_udc *udc = ep->udc;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
csr = __raw_readl(ep->creg);
|
|
|
|
|
|
|
|
/* NOTE: not collecting per-endpoint irq statistics... */
|
|
|
|
|
|
|
|
seq_printf(s, "\n");
|
|
|
|
seq_printf(s, "%s, maxpacket %d %s%s %s%s\n",
|
|
|
|
ep->ep.name, ep->ep.maxpacket,
|
|
|
|
ep->is_in ? "in" : "out",
|
|
|
|
ep->is_iso ? " iso" : "",
|
|
|
|
ep->is_pingpong
|
|
|
|
? (ep->fifo_bank ? "pong" : "ping")
|
|
|
|
: "",
|
|
|
|
ep->stopped ? " stopped" : "");
|
|
|
|
seq_printf(s, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS "\n",
|
|
|
|
csr,
|
|
|
|
(csr & 0x07ff0000) >> 16,
|
|
|
|
(csr & (1 << 15)) ? "enabled" : "disabled",
|
|
|
|
(csr & (1 << 11)) ? "DATA1" : "DATA0",
|
|
|
|
types[(csr & 0x700) >> 8],
|
|
|
|
|
|
|
|
/* iff type is control then print current direction */
|
|
|
|
(!(csr & 0x700))
|
|
|
|
? ((csr & (1 << 7)) ? " IN" : " OUT")
|
|
|
|
: "",
|
|
|
|
(csr & (1 << 6)) ? " rxdatabk1" : "",
|
|
|
|
(csr & (1 << 5)) ? " forcestall" : "",
|
|
|
|
(csr & (1 << 4)) ? " txpktrdy" : "",
|
|
|
|
|
|
|
|
(csr & (1 << 3)) ? " stallsent" : "",
|
|
|
|
(csr & (1 << 2)) ? " rxsetup" : "",
|
|
|
|
(csr & (1 << 1)) ? " rxdatabk0" : "",
|
|
|
|
(csr & (1 << 0)) ? " txcomp" : "");
|
|
|
|
if (list_empty (&ep->queue))
|
|
|
|
seq_printf(s, "\t(queue empty)\n");
|
|
|
|
|
|
|
|
else list_for_each_entry (req, &ep->queue, queue) {
|
|
|
|
unsigned length = req->req.actual;
|
|
|
|
|
|
|
|
seq_printf(s, "\treq %p len %d/%d buf %p\n",
|
|
|
|
&req->req, length,
|
|
|
|
req->req.length, req->req.buf);
|
|
|
|
}
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void proc_irq_show(struct seq_file *s, const char *label, u32 mask)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
seq_printf(s, "%s %04x:%s%s" FOURBITS, label, mask,
|
|
|
|
(mask & (1 << 13)) ? " wakeup" : "",
|
|
|
|
(mask & (1 << 12)) ? " endbusres" : "",
|
|
|
|
|
|
|
|
(mask & (1 << 11)) ? " sofint" : "",
|
|
|
|
(mask & (1 << 10)) ? " extrsm" : "",
|
|
|
|
(mask & (1 << 9)) ? " rxrsm" : "",
|
|
|
|
(mask & (1 << 8)) ? " rxsusp" : "");
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
if (mask & (1 << i))
|
|
|
|
seq_printf(s, " ep%d", i);
|
|
|
|
}
|
|
|
|
seq_printf(s, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int proc_udc_show(struct seq_file *s, void *unused)
|
|
|
|
{
|
|
|
|
struct at91_udc *udc = s->private;
|
|
|
|
struct at91_ep *ep;
|
|
|
|
u32 tmp;
|
|
|
|
|
|
|
|
seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
|
|
|
|
|
|
|
|
seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
|
|
|
|
udc->vbus ? "present" : "off",
|
|
|
|
udc->enabled
|
|
|
|
? (udc->vbus ? "active" : "enabled")
|
|
|
|
: "disabled",
|
2015-01-28 16:32:26 +08:00
|
|
|
udc->gadget.is_selfpowered ? "self" : "VBUS",
|
2006-01-23 02:32:37 +08:00
|
|
|
udc->suspended ? ", suspended" : "",
|
|
|
|
udc->driver ? udc->driver->driver.name : "(none)");
|
|
|
|
|
|
|
|
/* don't access registers when interface isn't clocked */
|
|
|
|
if (!udc->clocked) {
|
|
|
|
seq_printf(s, "(not clocked)\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-08 14:44:33 +08:00
|
|
|
tmp = at91_udp_read(udc, AT91_UDP_FRM_NUM);
|
2006-01-23 02:32:37 +08:00
|
|
|
seq_printf(s, "frame %05x:%s%s frame=%d\n", tmp,
|
|
|
|
(tmp & AT91_UDP_FRM_OK) ? " ok" : "",
|
|
|
|
(tmp & AT91_UDP_FRM_ERR) ? " err" : "",
|
|
|
|
(tmp & AT91_UDP_NUM));
|
|
|
|
|
2006-12-08 14:44:33 +08:00
|
|
|
tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
|
2006-01-23 02:32:37 +08:00
|
|
|
seq_printf(s, "glbstate %02x:%s" FOURBITS "\n", tmp,
|
|
|
|
(tmp & AT91_UDP_RMWUPE) ? " rmwupe" : "",
|
|
|
|
(tmp & AT91_UDP_RSMINPR) ? " rsminpr" : "",
|
|
|
|
(tmp & AT91_UDP_ESR) ? " esr" : "",
|
|
|
|
(tmp & AT91_UDP_CONFG) ? " confg" : "",
|
|
|
|
(tmp & AT91_UDP_FADDEN) ? " fadden" : "");
|
|
|
|
|
2006-12-08 14:44:33 +08:00
|
|
|
tmp = at91_udp_read(udc, AT91_UDP_FADDR);
|
2006-01-23 02:32:37 +08:00
|
|
|
seq_printf(s, "faddr %03x:%s fadd=%d\n", tmp,
|
|
|
|
(tmp & AT91_UDP_FEN) ? " fen" : "",
|
|
|
|
(tmp & AT91_UDP_FADD));
|
|
|
|
|
2006-12-08 14:44:33 +08:00
|
|
|
proc_irq_show(s, "imr ", at91_udp_read(udc, AT91_UDP_IMR));
|
|
|
|
proc_irq_show(s, "isr ", at91_udp_read(udc, AT91_UDP_ISR));
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
if (udc->enabled && udc->vbus) {
|
|
|
|
proc_ep_show(s, &udc->ep[0]);
|
|
|
|
list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
|
2012-03-13 02:25:26 +08:00
|
|
|
if (ep->ep.desc)
|
2006-01-23 02:32:37 +08:00
|
|
|
proc_ep_show(s, ep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int proc_udc_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2013-04-01 06:16:14 +08:00
|
|
|
return single_open(file, proc_udc_show, PDE_DATA(inode));
|
2006-01-23 02:32:37 +08:00
|
|
|
}
|
|
|
|
|
2006-08-06 07:37:11 +08:00
|
|
|
static const struct file_operations proc_ops = {
|
2008-04-29 16:02:19 +08:00
|
|
|
.owner = THIS_MODULE,
|
2006-01-23 02:32:37 +08:00
|
|
|
.open = proc_udc_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = single_release,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void create_debug_file(struct at91_udc *udc)
|
|
|
|
{
|
2008-04-29 16:02:19 +08:00
|
|
|
udc->pde = proc_create_data(debug_filename, 0, NULL, &proc_ops, udc);
|
2006-01-23 02:32:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_debug_file(struct at91_udc *udc)
|
|
|
|
{
|
|
|
|
if (udc->pde)
|
|
|
|
remove_proc_entry(debug_filename, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static inline void create_debug_file(struct at91_udc *udc) {}
|
|
|
|
static inline void remove_debug_file(struct at91_udc *udc) {}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static void done(struct at91_ep *ep, struct at91_request *req, int status)
|
|
|
|
{
|
|
|
|
unsigned stopped = ep->stopped;
|
2006-12-08 14:44:33 +08:00
|
|
|
struct at91_udc *udc = ep->udc;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
list_del_init(&req->queue);
|
|
|
|
if (req->req.status == -EINPROGRESS)
|
|
|
|
req->req.status = status;
|
|
|
|
else
|
|
|
|
status = req->req.status;
|
|
|
|
if (status && status != -ESHUTDOWN)
|
|
|
|
VDBG("%s done %p, status %d\n", ep->ep.name, req, status);
|
|
|
|
|
|
|
|
ep->stopped = 1;
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock(&udc->lock);
|
2014-09-25 04:43:19 +08:00
|
|
|
usb_gadget_giveback_request(&ep->ep, &req->req);
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock(&udc->lock);
|
2006-01-23 02:32:37 +08:00
|
|
|
ep->stopped = stopped;
|
|
|
|
|
|
|
|
/* ep0 is always ready; other endpoints need a non-empty queue */
|
|
|
|
if (list_empty(&ep->queue) && ep->int_mask != (1 << 0))
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_IDR, ep->int_mask);
|
2006-01-23 02:32:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* bits indicating OUT fifo has data ready */
|
|
|
|
#define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
|
|
|
|
* back most of the value you just read (because of side effects, including
|
|
|
|
* bits that may change after reading and before writing).
|
|
|
|
*
|
|
|
|
* Except when changing a specific bit, always write values which:
|
|
|
|
* - clear SET_FX bits (setting them could change something)
|
|
|
|
* - set CLR_FX bits (clearing them could change something)
|
|
|
|
*
|
|
|
|
* There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
|
|
|
|
* that shouldn't normally be changed.
|
2006-07-05 17:38:56 +08:00
|
|
|
*
|
|
|
|
* NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains,
|
|
|
|
* implying a need to wait for one write to complete (test relevant bits)
|
|
|
|
* before starting the next write. This shouldn't be an issue given how
|
|
|
|
* infrequently we write, except maybe for write-then-read idioms.
|
2006-01-23 02:32:37 +08:00
|
|
|
*/
|
|
|
|
#define SET_FX (AT91_UDP_TXPKTRDY)
|
2006-07-05 17:38:56 +08:00
|
|
|
#define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP \
|
|
|
|
| AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
/* pull OUT packet data from the endpoint's fifo */
|
|
|
|
static int read_fifo (struct at91_ep *ep, struct at91_request *req)
|
|
|
|
{
|
|
|
|
u32 __iomem *creg = ep->creg;
|
|
|
|
u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
|
|
|
|
u32 csr;
|
|
|
|
u8 *buf;
|
|
|
|
unsigned int count, bufferspace, is_done;
|
|
|
|
|
|
|
|
buf = req->req.buf + req->req.actual;
|
|
|
|
bufferspace = req->req.length - req->req.actual;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* there might be nothing to read if ep_queue() calls us,
|
|
|
|
* or if we already emptied both pingpong buffers
|
|
|
|
*/
|
|
|
|
rescan:
|
|
|
|
csr = __raw_readl(creg);
|
|
|
|
if ((csr & RX_DATA_READY) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
count = (csr & AT91_UDP_RXBYTECNT) >> 16;
|
|
|
|
if (count > ep->ep.maxpacket)
|
|
|
|
count = ep->ep.maxpacket;
|
|
|
|
if (count > bufferspace) {
|
|
|
|
DBG("%s buffer overflow\n", ep->ep.name);
|
|
|
|
req->req.status = -EOVERFLOW;
|
|
|
|
count = bufferspace;
|
|
|
|
}
|
|
|
|
__raw_readsb(dreg, buf, count);
|
|
|
|
|
|
|
|
/* release and swap pingpong mem bank */
|
|
|
|
csr |= CLR_FX;
|
|
|
|
if (ep->is_pingpong) {
|
|
|
|
if (ep->fifo_bank == 0) {
|
|
|
|
csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
|
|
|
|
ep->fifo_bank = 1;
|
|
|
|
} else {
|
|
|
|
csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1);
|
|
|
|
ep->fifo_bank = 0;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
|
|
|
|
__raw_writel(csr, creg);
|
|
|
|
|
|
|
|
req->req.actual += count;
|
|
|
|
is_done = (count < ep->ep.maxpacket);
|
|
|
|
if (count == bufferspace)
|
|
|
|
is_done = 1;
|
|
|
|
|
|
|
|
PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count,
|
|
|
|
is_done ? " (done)" : "");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* avoid extra trips through IRQ logic for packets already in
|
|
|
|
* the fifo ... maybe preventing an extra (expensive) OUT-NAK
|
|
|
|
*/
|
|
|
|
if (is_done)
|
|
|
|
done(ep, req, 0);
|
|
|
|
else if (ep->is_pingpong) {
|
2010-03-02 00:54:55 +08:00
|
|
|
/*
|
|
|
|
* One dummy read to delay the code because of a HW glitch:
|
|
|
|
* CSR returns bad RXCOUNT when read too soon after updating
|
|
|
|
* RX_DATA_BK flags.
|
|
|
|
*/
|
|
|
|
csr = __raw_readl(creg);
|
|
|
|
|
2006-01-23 02:32:37 +08:00
|
|
|
bufferspace -= count;
|
|
|
|
buf += count;
|
|
|
|
goto rescan;
|
|
|
|
}
|
|
|
|
|
|
|
|
return is_done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load fifo for an IN packet */
|
|
|
|
static int write_fifo(struct at91_ep *ep, struct at91_request *req)
|
|
|
|
{
|
|
|
|
u32 __iomem *creg = ep->creg;
|
|
|
|
u32 csr = __raw_readl(creg);
|
|
|
|
u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
|
|
|
|
unsigned total, count, is_last;
|
2008-04-07 14:32:55 +08:00
|
|
|
u8 *buf;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: allow for writing two packets to the fifo ... that'll
|
|
|
|
* reduce the amount of IN-NAKing, but probably won't affect
|
|
|
|
* throughput much. (Unlike preventing OUT-NAKing!)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If ep_queue() calls us, the queue is empty and possibly in
|
|
|
|
* odd states like TXCOMP not yet cleared (we do it, saving at
|
|
|
|
* least one IRQ) or the fifo not yet being free. Those aren't
|
|
|
|
* issues normally (IRQ handler fast path).
|
|
|
|
*/
|
|
|
|
if (unlikely(csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY))) {
|
|
|
|
if (csr & AT91_UDP_TXCOMP) {
|
|
|
|
csr |= CLR_FX;
|
|
|
|
csr &= ~(SET_FX | AT91_UDP_TXCOMP);
|
|
|
|
__raw_writel(csr, creg);
|
|
|
|
csr = __raw_readl(creg);
|
|
|
|
}
|
|
|
|
if (csr & AT91_UDP_TXPKTRDY)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-07 14:32:55 +08:00
|
|
|
buf = req->req.buf + req->req.actual;
|
|
|
|
prefetch(buf);
|
2006-01-23 02:32:37 +08:00
|
|
|
total = req->req.length - req->req.actual;
|
|
|
|
if (ep->ep.maxpacket < total) {
|
|
|
|
count = ep->ep.maxpacket;
|
|
|
|
is_last = 0;
|
|
|
|
} else {
|
|
|
|
count = total;
|
|
|
|
is_last = (count < ep->ep.maxpacket) || !req->req.zero;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the packet, maybe it's a ZLP.
|
|
|
|
*
|
|
|
|
* NOTE: incrementing req->actual before we receive the ACK means
|
|
|
|
* gadget driver IN bytecounts can be wrong in fault cases. That's
|
|
|
|
* fixable with PIO drivers like this one (save "count" here, and
|
|
|
|
* do the increment later on TX irq), but not for most DMA hardware.
|
|
|
|
*
|
|
|
|
* So all gadget drivers must accept that potential error. Some
|
|
|
|
* hardware supports precise fifo status reporting, letting them
|
|
|
|
* recover when the actual bytecount matters (e.g. for USB Test
|
|
|
|
* and Measurement Class devices).
|
|
|
|
*/
|
2008-04-07 14:32:55 +08:00
|
|
|
__raw_writesb(dreg, buf, count);
|
2006-01-23 02:32:37 +08:00
|
|
|
csr &= ~SET_FX;
|
|
|
|
csr |= CLR_FX | AT91_UDP_TXPKTRDY;
|
|
|
|
__raw_writel(csr, creg);
|
|
|
|
req->req.actual += count;
|
|
|
|
|
|
|
|
PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count,
|
|
|
|
is_last ? " (done)" : "");
|
|
|
|
if (is_last)
|
|
|
|
done(ep, req, 0);
|
|
|
|
return is_last;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nuke(struct at91_ep *ep, int status)
|
|
|
|
{
|
|
|
|
struct at91_request *req;
|
|
|
|
|
2011-10-07 03:36:26 +08:00
|
|
|
/* terminate any request in the queue */
|
2006-01-23 02:32:37 +08:00
|
|
|
ep->stopped = 1;
|
|
|
|
if (list_empty(&ep->queue))
|
|
|
|
return;
|
|
|
|
|
2008-03-04 08:08:34 +08:00
|
|
|
VDBG("%s %s\n", __func__, ep->ep.name);
|
2006-01-23 02:32:37 +08:00
|
|
|
while (!list_empty(&ep->queue)) {
|
|
|
|
req = list_entry(ep->queue.next, struct at91_request, queue);
|
|
|
|
done(ep, req, status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2006-07-05 17:38:56 +08:00
|
|
|
static int at91_ep_enable(struct usb_ep *_ep,
|
|
|
|
const struct usb_endpoint_descriptor *desc)
|
2006-01-23 02:32:37 +08:00
|
|
|
{
|
|
|
|
struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
|
2012-09-07 14:54:25 +08:00
|
|
|
struct at91_udc *udc;
|
2006-01-23 02:32:37 +08:00
|
|
|
u16 maxpacket;
|
|
|
|
u32 tmp;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (!_ep || !ep
|
2012-07-21 02:34:25 +08:00
|
|
|
|| !desc || _ep->name == ep0name
|
2006-01-23 02:32:37 +08:00
|
|
|
|| desc->bDescriptorType != USB_DT_ENDPOINT
|
2011-08-23 18:12:03 +08:00
|
|
|
|| (maxpacket = usb_endpoint_maxp(desc)) == 0
|
2006-01-23 02:32:37 +08:00
|
|
|
|| maxpacket > ep->maxpacket) {
|
|
|
|
DBG("bad ep or descriptor\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-09-07 14:54:25 +08:00
|
|
|
udc = ep->udc;
|
2010-03-02 01:01:56 +08:00
|
|
|
if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
|
2006-01-23 02:32:37 +08:00
|
|
|
DBG("bogus device state\n");
|
|
|
|
return -ESHUTDOWN;
|
|
|
|
}
|
|
|
|
|
2009-04-16 04:28:02 +08:00
|
|
|
tmp = usb_endpoint_type(desc);
|
2006-01-23 02:32:37 +08:00
|
|
|
switch (tmp) {
|
|
|
|
case USB_ENDPOINT_XFER_CONTROL:
|
|
|
|
DBG("only one control endpoint\n");
|
|
|
|
return -EINVAL;
|
|
|
|
case USB_ENDPOINT_XFER_INT:
|
|
|
|
if (maxpacket > 64)
|
|
|
|
goto bogus_max;
|
|
|
|
break;
|
|
|
|
case USB_ENDPOINT_XFER_BULK:
|
|
|
|
switch (maxpacket) {
|
|
|
|
case 8:
|
|
|
|
case 16:
|
|
|
|
case 32:
|
|
|
|
case 64:
|
|
|
|
goto ok;
|
|
|
|
}
|
|
|
|
bogus_max:
|
|
|
|
DBG("bogus maxpacket %d\n", maxpacket);
|
|
|
|
return -EINVAL;
|
|
|
|
case USB_ENDPOINT_XFER_ISOC:
|
|
|
|
if (!ep->is_pingpong) {
|
|
|
|
DBG("iso requires double buffering\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok:
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
/* initialize endpoint to match this descriptor */
|
2009-04-16 04:28:02 +08:00
|
|
|
ep->is_in = usb_endpoint_dir_in(desc);
|
2006-01-23 02:32:37 +08:00
|
|
|
ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
|
|
|
|
ep->stopped = 0;
|
|
|
|
if (ep->is_in)
|
|
|
|
tmp |= 0x04;
|
|
|
|
tmp <<= 8;
|
|
|
|
tmp |= AT91_UDP_EPEDS;
|
|
|
|
__raw_writel(tmp, ep->creg);
|
|
|
|
|
|
|
|
ep->ep.maxpacket = maxpacket;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
|
|
|
|
* since endpoint resets don't reset hw pingpong state.
|
|
|
|
*/
|
2010-03-02 01:01:56 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
|
|
|
|
at91_udp_write(udc, AT91_UDP_RST_EP, 0);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int at91_ep_disable (struct usb_ep * _ep)
|
|
|
|
{
|
|
|
|
struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
|
2006-12-08 14:44:33 +08:00
|
|
|
struct at91_udc *udc = ep->udc;
|
2006-01-23 02:32:37 +08:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (ep == &ep->udc->ep[0])
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
nuke(ep, -ESHUTDOWN);
|
|
|
|
|
|
|
|
/* restore the endpoint's pristine config */
|
2012-02-08 19:56:48 +08:00
|
|
|
ep->ep.desc = NULL;
|
2006-01-23 02:32:37 +08:00
|
|
|
ep->ep.maxpacket = ep->maxpacket;
|
|
|
|
|
|
|
|
/* reset fifos and endpoint */
|
|
|
|
if (ep->udc->clocked) {
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
|
|
|
|
at91_udp_write(udc, AT91_UDP_RST_EP, 0);
|
2006-01-23 02:32:37 +08:00
|
|
|
__raw_writel(0, ep->creg);
|
|
|
|
}
|
|
|
|
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is a PIO-only driver, so there's nothing
|
|
|
|
* interesting for request or buffer allocation.
|
|
|
|
*/
|
|
|
|
|
2006-07-05 17:38:56 +08:00
|
|
|
static struct usb_request *
|
2008-01-06 05:21:43 +08:00
|
|
|
at91_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
|
2006-01-23 02:32:37 +08:00
|
|
|
{
|
|
|
|
struct at91_request *req;
|
|
|
|
|
2006-12-13 16:34:52 +08:00
|
|
|
req = kzalloc(sizeof (struct at91_request), gfp_flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
if (!req)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&req->queue);
|
|
|
|
return &req->req;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
|
|
|
|
{
|
|
|
|
struct at91_request *req;
|
|
|
|
|
|
|
|
req = container_of(_req, struct at91_request, req);
|
|
|
|
BUG_ON(!list_empty(&req->queue));
|
|
|
|
kfree(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int at91_ep_queue(struct usb_ep *_ep,
|
|
|
|
struct usb_request *_req, gfp_t gfp_flags)
|
|
|
|
{
|
|
|
|
struct at91_request *req;
|
|
|
|
struct at91_ep *ep;
|
2010-03-02 01:01:56 +08:00
|
|
|
struct at91_udc *udc;
|
2006-01-23 02:32:37 +08:00
|
|
|
int status;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
req = container_of(_req, struct at91_request, req);
|
|
|
|
ep = container_of(_ep, struct at91_ep, ep);
|
|
|
|
|
|
|
|
if (!_req || !_req->complete
|
|
|
|
|| !_req->buf || !list_empty(&req->queue)) {
|
|
|
|
DBG("invalid request\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-03-13 02:25:26 +08:00
|
|
|
if (!_ep || (!ep->ep.desc && ep->ep.name != ep0name)) {
|
2006-01-23 02:32:37 +08:00
|
|
|
DBG("invalid ep\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2010-03-02 01:01:56 +08:00
|
|
|
udc = ep->udc;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2010-03-02 01:01:56 +08:00
|
|
|
if (!udc || !udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
|
2006-01-23 02:32:37 +08:00
|
|
|
DBG("invalid device\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
_req->status = -EINPROGRESS;
|
|
|
|
_req->actual = 0;
|
|
|
|
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
/* try to kickstart any empty and idle queue */
|
|
|
|
if (list_empty(&ep->queue) && !ep->stopped) {
|
|
|
|
int is_ep0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this control request has a non-empty DATA stage, this
|
|
|
|
* will start that stage. It works just like a non-control
|
|
|
|
* request (until the status stage starts, maybe early).
|
|
|
|
*
|
|
|
|
* If the data stage is empty, then this starts a successful
|
|
|
|
* IN/STATUS stage. (Unsuccessful ones use set_halt.)
|
|
|
|
*/
|
|
|
|
is_ep0 = (ep->ep.name == ep0name);
|
|
|
|
if (is_ep0) {
|
|
|
|
u32 tmp;
|
|
|
|
|
2010-03-02 01:01:56 +08:00
|
|
|
if (!udc->req_pending) {
|
2006-01-23 02:32:37 +08:00
|
|
|
status = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* defer changing CONFG until after the gadget driver
|
|
|
|
* reconfigures the endpoints.
|
|
|
|
*/
|
2010-03-02 01:01:56 +08:00
|
|
|
if (udc->wait_for_config_ack) {
|
|
|
|
tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
|
2006-01-23 02:32:37 +08:00
|
|
|
tmp ^= AT91_UDP_CONFG;
|
|
|
|
VDBG("toggle config\n");
|
2010-03-02 01:01:56 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
|
2006-01-23 02:32:37 +08:00
|
|
|
}
|
|
|
|
if (req->req.length == 0) {
|
|
|
|
ep0_in_status:
|
|
|
|
PACKET("ep0 in/status\n");
|
|
|
|
status = 0;
|
|
|
|
tmp = __raw_readl(ep->creg);
|
|
|
|
tmp &= ~SET_FX;
|
|
|
|
tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
|
|
|
|
__raw_writel(tmp, ep->creg);
|
2010-03-02 01:01:56 +08:00
|
|
|
udc->req_pending = 0;
|
2006-01-23 02:32:37 +08:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ep->is_in)
|
|
|
|
status = write_fifo(ep, req);
|
|
|
|
else {
|
|
|
|
status = read_fifo(ep, req);
|
|
|
|
|
|
|
|
/* IN/STATUS stage is otherwise triggered by irq */
|
|
|
|
if (status && is_ep0)
|
|
|
|
goto ep0_in_status;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
status = 0;
|
|
|
|
|
|
|
|
if (req && !status) {
|
|
|
|
list_add_tail (&req->queue, &ep->queue);
|
2010-03-02 01:01:56 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_IER, ep->int_mask);
|
2006-01-23 02:32:37 +08:00
|
|
|
}
|
|
|
|
done:
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
return (status < 0) ? status : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
|
|
|
|
{
|
2010-03-02 01:01:56 +08:00
|
|
|
struct at91_ep *ep;
|
2006-01-23 02:32:37 +08:00
|
|
|
struct at91_request *req;
|
2010-03-02 01:01:56 +08:00
|
|
|
unsigned long flags;
|
|
|
|
struct at91_udc *udc;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
ep = container_of(_ep, struct at91_ep, ep);
|
|
|
|
if (!_ep || ep->ep.name == ep0name)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-03-02 01:01:56 +08:00
|
|
|
udc = ep->udc;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
|
|
|
|
2006-01-23 02:32:37 +08:00
|
|
|
/* make sure it's actually queued on this endpoint */
|
|
|
|
list_for_each_entry (req, &ep->queue, queue) {
|
|
|
|
if (&req->req == _req)
|
|
|
|
break;
|
|
|
|
}
|
2010-03-02 01:01:56 +08:00
|
|
|
if (&req->req != _req) {
|
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
return -EINVAL;
|
2010-03-02 01:01:56 +08:00
|
|
|
}
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
done(ep, req, -ECONNRESET);
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int at91_ep_set_halt(struct usb_ep *_ep, int value)
|
|
|
|
{
|
|
|
|
struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
|
2006-12-08 14:44:33 +08:00
|
|
|
struct at91_udc *udc = ep->udc;
|
2006-01-23 02:32:37 +08:00
|
|
|
u32 __iomem *creg;
|
|
|
|
u32 csr;
|
|
|
|
unsigned long flags;
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
if (!_ep || ep->is_iso || !ep->udc->clocked)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
creg = ep->creg;
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
csr = __raw_readl(creg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fail with still-busy IN endpoints, ensuring correct sequencing
|
|
|
|
* of data tx then stall. note that the fifo rx bytecount isn't
|
|
|
|
* completely accurate as a tx bytecount.
|
|
|
|
*/
|
|
|
|
if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
|
|
|
|
status = -EAGAIN;
|
|
|
|
else {
|
|
|
|
csr |= CLR_FX;
|
|
|
|
csr &= ~SET_FX;
|
|
|
|
if (value) {
|
|
|
|
csr |= AT91_UDP_FORCESTALL;
|
|
|
|
VDBG("halt %s\n", ep->ep.name);
|
|
|
|
} else {
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
|
|
|
|
at91_udp_write(udc, AT91_UDP_RST_EP, 0);
|
2006-01-23 02:32:37 +08:00
|
|
|
csr &= ~AT91_UDP_FORCESTALL;
|
|
|
|
}
|
|
|
|
__raw_writel(csr, creg);
|
|
|
|
}
|
|
|
|
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2007-02-16 10:47:17 +08:00
|
|
|
static const struct usb_ep_ops at91_ep_ops = {
|
2006-01-23 02:32:37 +08:00
|
|
|
.enable = at91_ep_enable,
|
|
|
|
.disable = at91_ep_disable,
|
|
|
|
.alloc_request = at91_ep_alloc_request,
|
|
|
|
.free_request = at91_ep_free_request,
|
|
|
|
.queue = at91_ep_queue,
|
|
|
|
.dequeue = at91_ep_dequeue,
|
|
|
|
.set_halt = at91_ep_set_halt,
|
2011-10-07 03:36:26 +08:00
|
|
|
/* there's only imprecise fifo status reporting */
|
2006-01-23 02:32:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static int at91_get_frame(struct usb_gadget *gadget)
|
|
|
|
{
|
2006-12-08 14:44:33 +08:00
|
|
|
struct at91_udc *udc = to_udc(gadget);
|
|
|
|
|
2006-01-23 02:32:37 +08:00
|
|
|
if (!to_udc(gadget)->clocked)
|
|
|
|
return -EINVAL;
|
2006-12-08 14:44:33 +08:00
|
|
|
return at91_udp_read(udc, AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
|
2006-01-23 02:32:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int at91_wakeup(struct usb_gadget *gadget)
|
|
|
|
{
|
|
|
|
struct at91_udc *udc = to_udc(gadget);
|
|
|
|
u32 glbstate;
|
|
|
|
int status = -EINVAL;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2008-03-04 08:08:34 +08:00
|
|
|
DBG("%s\n", __func__ );
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
if (!udc->clocked || !udc->suspended)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* NOTE: some "early versions" handle ESR differently ... */
|
|
|
|
|
2006-12-08 14:44:33 +08:00
|
|
|
glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT);
|
2006-01-23 02:32:37 +08:00
|
|
|
if (!(glbstate & AT91_UDP_ESR))
|
|
|
|
goto done;
|
|
|
|
glbstate |= AT91_UDP_ESR;
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
done:
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2011-03-31 09:57:33 +08:00
|
|
|
/* reinit == restore initial software state */
|
2006-01-23 02:32:37 +08:00
|
|
|
static void udc_reinit(struct at91_udc *udc)
|
|
|
|
{
|
|
|
|
u32 i;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&udc->gadget.ep_list);
|
|
|
|
INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
|
2015-07-28 13:19:59 +08:00
|
|
|
udc->gadget.quirk_stall_not_supp = 1;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
for (i = 0; i < NUM_ENDPOINTS; i++) {
|
|
|
|
struct at91_ep *ep = &udc->ep[i];
|
|
|
|
|
|
|
|
if (i != 0)
|
|
|
|
list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
|
2012-03-13 02:25:26 +08:00
|
|
|
ep->ep.desc = NULL;
|
2006-01-23 02:32:37 +08:00
|
|
|
ep->stopped = 0;
|
|
|
|
ep->fifo_bank = 0;
|
2013-12-13 19:23:38 +08:00
|
|
|
usb_ep_set_maxpacket_limit(&ep->ep, ep->maxpacket);
|
2006-12-08 14:44:33 +08:00
|
|
|
ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i);
|
2011-10-07 03:36:26 +08:00
|
|
|
/* initialize one queue per endpoint */
|
2006-01-23 02:32:37 +08:00
|
|
|
INIT_LIST_HEAD(&ep->queue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-06 14:28:02 +08:00
|
|
|
static void reset_gadget(struct at91_udc *udc)
|
|
|
|
{
|
|
|
|
struct usb_gadget_driver *driver = udc->driver;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (udc->gadget.speed == USB_SPEED_UNKNOWN)
|
|
|
|
driver = NULL;
|
|
|
|
udc->gadget.speed = USB_SPEED_UNKNOWN;
|
|
|
|
udc->suspended = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_ENDPOINTS; i++) {
|
|
|
|
struct at91_ep *ep = &udc->ep[i];
|
|
|
|
|
|
|
|
ep->stopped = 1;
|
|
|
|
nuke(ep, -ESHUTDOWN);
|
|
|
|
}
|
|
|
|
if (driver) {
|
|
|
|
spin_unlock(&udc->lock);
|
|
|
|
usb_gadget_udc_reset(&udc->gadget, driver);
|
|
|
|
spin_lock(&udc->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
udc_reinit(udc);
|
|
|
|
}
|
|
|
|
|
2006-01-23 02:32:37 +08:00
|
|
|
static void stop_activity(struct at91_udc *udc)
|
|
|
|
{
|
|
|
|
struct usb_gadget_driver *driver = udc->driver;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (udc->gadget.speed == USB_SPEED_UNKNOWN)
|
|
|
|
driver = NULL;
|
|
|
|
udc->gadget.speed = USB_SPEED_UNKNOWN;
|
2006-07-05 17:38:56 +08:00
|
|
|
udc->suspended = 0;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
for (i = 0; i < NUM_ENDPOINTS; i++) {
|
|
|
|
struct at91_ep *ep = &udc->ep[i];
|
|
|
|
ep->stopped = 1;
|
|
|
|
nuke(ep, -ESHUTDOWN);
|
|
|
|
}
|
2010-03-02 01:01:56 +08:00
|
|
|
if (driver) {
|
|
|
|
spin_unlock(&udc->lock);
|
2006-01-23 02:32:37 +08:00
|
|
|
driver->disconnect(&udc->gadget);
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock(&udc->lock);
|
|
|
|
}
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
udc_reinit(udc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clk_on(struct at91_udc *udc)
|
|
|
|
{
|
|
|
|
if (udc->clocked)
|
|
|
|
return;
|
|
|
|
udc->clocked = 1;
|
2013-08-02 16:37:34 +08:00
|
|
|
|
2014-11-19 23:37:27 +08:00
|
|
|
clk_enable(udc->iclk);
|
|
|
|
clk_enable(udc->fclk);
|
2006-01-23 02:32:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void clk_off(struct at91_udc *udc)
|
|
|
|
{
|
|
|
|
if (!udc->clocked)
|
|
|
|
return;
|
|
|
|
udc->clocked = 0;
|
|
|
|
udc->gadget.speed = USB_SPEED_UNKNOWN;
|
2014-11-19 23:37:27 +08:00
|
|
|
clk_disable(udc->fclk);
|
|
|
|
clk_disable(udc->iclk);
|
2006-01-23 02:32:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* activate/deactivate link with host; minimize power usage for
|
|
|
|
* inactive links by cutting clocks and transceiver power.
|
|
|
|
*/
|
|
|
|
static void pullup(struct at91_udc *udc, int is_on)
|
|
|
|
{
|
|
|
|
if (!udc->enabled || !udc->vbus)
|
|
|
|
is_on = 0;
|
|
|
|
DBG("%sactive\n", is_on ? "" : "in");
|
2006-12-08 14:44:33 +08:00
|
|
|
|
2006-01-23 02:32:37 +08:00
|
|
|
if (is_on) {
|
|
|
|
clk_on(udc);
|
2007-12-14 07:52:58 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_TXVC, 0);
|
|
|
|
} else {
|
2006-01-23 02:32:37 +08:00
|
|
|
stop_activity(udc);
|
2007-12-14 07:52:58 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
|
2006-01-23 02:32:37 +08:00
|
|
|
clk_off(udc);
|
|
|
|
}
|
2015-01-15 00:22:02 +08:00
|
|
|
|
|
|
|
if (udc->caps && udc->caps->pullup)
|
|
|
|
udc->caps->pullup(udc, is_on);
|
2006-01-23 02:32:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vbus is here! turn everything on that's ready */
|
|
|
|
static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
|
|
|
|
{
|
|
|
|
struct at91_udc *udc = to_udc(gadget);
|
|
|
|
unsigned long flags;
|
|
|
|
|
2011-10-07 03:36:26 +08:00
|
|
|
/* VDBG("vbus %s\n", is_active ? "on" : "off"); */
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
udc->vbus = (is_active != 0);
|
2006-12-08 19:26:00 +08:00
|
|
|
if (udc->driver)
|
|
|
|
pullup(udc, is_active);
|
|
|
|
else
|
|
|
|
pullup(udc, 0);
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int at91_pullup(struct usb_gadget *gadget, int is_on)
|
|
|
|
{
|
|
|
|
struct at91_udc *udc = to_udc(gadget);
|
|
|
|
unsigned long flags;
|
|
|
|
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
udc->enabled = is_on = !!is_on;
|
|
|
|
pullup(udc, is_on);
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
|
|
|
|
{
|
|
|
|
struct at91_udc *udc = to_udc(gadget);
|
|
|
|
unsigned long flags;
|
|
|
|
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
2015-01-28 16:32:26 +08:00
|
|
|
gadget->is_selfpowered = (is_on != 0);
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-05 01:55:23 +08:00
|
|
|
static int at91_start(struct usb_gadget *gadget,
|
|
|
|
struct usb_gadget_driver *driver);
|
2014-10-18 01:05:12 +08:00
|
|
|
static int at91_stop(struct usb_gadget *gadget);
|
|
|
|
|
2006-01-23 02:32:37 +08:00
|
|
|
static const struct usb_gadget_ops at91_udc_ops = {
|
|
|
|
.get_frame = at91_get_frame,
|
|
|
|
.wakeup = at91_wakeup,
|
|
|
|
.set_selfpowered = at91_set_selfpowered,
|
|
|
|
.vbus_session = at91_vbus_session,
|
|
|
|
.pullup = at91_pullup,
|
2012-02-05 01:55:23 +08:00
|
|
|
.udc_start = at91_start,
|
|
|
|
.udc_stop = at91_stop,
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* VBUS-powered devices may also also want to support bigger
|
|
|
|
* power budgets after an appropriate SET_CONFIGURATION.
|
|
|
|
*/
|
2011-10-07 03:36:26 +08:00
|
|
|
/* .vbus_power = at91_vbus_power, */
|
2006-01-23 02:32:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static int handle_ep(struct at91_ep *ep)
|
|
|
|
{
|
|
|
|
struct at91_request *req;
|
|
|
|
u32 __iomem *creg = ep->creg;
|
|
|
|
u32 csr = __raw_readl(creg);
|
|
|
|
|
|
|
|
if (!list_empty(&ep->queue))
|
|
|
|
req = list_entry(ep->queue.next,
|
|
|
|
struct at91_request, queue);
|
|
|
|
else
|
|
|
|
req = NULL;
|
|
|
|
|
|
|
|
if (ep->is_in) {
|
|
|
|
if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
|
|
|
|
csr |= CLR_FX;
|
|
|
|
csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
|
|
|
|
__raw_writel(csr, creg);
|
|
|
|
}
|
|
|
|
if (req)
|
|
|
|
return write_fifo(ep, req);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (csr & AT91_UDP_STALLSENT) {
|
|
|
|
/* STALLSENT bit == ISOERR */
|
|
|
|
if (ep->is_iso && req)
|
|
|
|
req->req.status = -EILSEQ;
|
|
|
|
csr |= CLR_FX;
|
|
|
|
csr &= ~(SET_FX | AT91_UDP_STALLSENT);
|
|
|
|
__raw_writel(csr, creg);
|
|
|
|
csr = __raw_readl(creg);
|
|
|
|
}
|
|
|
|
if (req && (csr & RX_DATA_READY))
|
|
|
|
return read_fifo(ep, req);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
union setup {
|
|
|
|
u8 raw[8];
|
|
|
|
struct usb_ctrlrequest r;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
|
|
|
|
{
|
|
|
|
u32 __iomem *creg = ep->creg;
|
|
|
|
u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
|
|
|
|
unsigned rxcount, i = 0;
|
|
|
|
u32 tmp;
|
|
|
|
union setup pkt;
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
/* read and ack SETUP; hard-fail for bogus packets */
|
|
|
|
rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
|
|
|
|
if (likely(rxcount == 8)) {
|
|
|
|
while (rxcount--)
|
|
|
|
pkt.raw[i++] = __raw_readb(dreg);
|
|
|
|
if (pkt.r.bRequestType & USB_DIR_IN) {
|
|
|
|
csr |= AT91_UDP_DIR;
|
|
|
|
ep->is_in = 1;
|
|
|
|
} else {
|
|
|
|
csr &= ~AT91_UDP_DIR;
|
|
|
|
ep->is_in = 0;
|
|
|
|
}
|
|
|
|
} else {
|
2011-10-07 03:36:26 +08:00
|
|
|
/* REVISIT this happens sometimes under load; why?? */
|
2006-01-23 02:32:37 +08:00
|
|
|
ERR("SETUP len %d, csr %08x\n", rxcount, csr);
|
|
|
|
status = -EINVAL;
|
|
|
|
}
|
|
|
|
csr |= CLR_FX;
|
|
|
|
csr &= ~(SET_FX | AT91_UDP_RXSETUP);
|
|
|
|
__raw_writel(csr, creg);
|
|
|
|
udc->wait_for_addr_ack = 0;
|
|
|
|
udc->wait_for_config_ack = 0;
|
|
|
|
ep->stopped = 0;
|
|
|
|
if (unlikely(status != 0))
|
|
|
|
goto stall;
|
|
|
|
|
|
|
|
#define w_index le16_to_cpu(pkt.r.wIndex)
|
|
|
|
#define w_value le16_to_cpu(pkt.r.wValue)
|
|
|
|
#define w_length le16_to_cpu(pkt.r.wLength)
|
|
|
|
|
|
|
|
VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
|
|
|
|
pkt.r.bRequestType, pkt.r.bRequest,
|
|
|
|
w_value, w_index, w_length);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A few standard requests get handled here, ones that touch
|
|
|
|
* hardware ... notably for device and endpoint features.
|
|
|
|
*/
|
|
|
|
udc->req_pending = 1;
|
|
|
|
csr = __raw_readl(creg);
|
|
|
|
csr |= CLR_FX;
|
|
|
|
csr &= ~SET_FX;
|
|
|
|
switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {
|
|
|
|
|
|
|
|
case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
|
|
|
|
| USB_REQ_SET_ADDRESS:
|
|
|
|
__raw_writel(csr | AT91_UDP_TXPKTRDY, creg);
|
|
|
|
udc->addr = w_value;
|
|
|
|
udc->wait_for_addr_ack = 1;
|
|
|
|
udc->req_pending = 0;
|
|
|
|
/* FADDR is set later, when we ack host STATUS */
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
|
|
|
|
| USB_REQ_SET_CONFIGURATION:
|
2006-12-08 14:44:33 +08:00
|
|
|
tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_CONFG;
|
2006-01-23 02:32:37 +08:00
|
|
|
if (pkt.r.wValue)
|
|
|
|
udc->wait_for_config_ack = (tmp == 0);
|
|
|
|
else
|
|
|
|
udc->wait_for_config_ack = (tmp != 0);
|
|
|
|
if (udc->wait_for_config_ack)
|
|
|
|
VDBG("wait for config\n");
|
|
|
|
/* CONFG is toggled later, if gadget driver succeeds */
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hosts may set or clear remote wakeup status, and
|
|
|
|
* devices may report they're VBUS powered.
|
|
|
|
*/
|
|
|
|
case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
|
|
|
|
| USB_REQ_GET_STATUS:
|
2015-01-28 16:32:26 +08:00
|
|
|
tmp = (udc->gadget.is_selfpowered << USB_DEVICE_SELF_POWERED);
|
2006-12-08 14:44:33 +08:00
|
|
|
if (at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_ESR)
|
2006-01-23 02:32:37 +08:00
|
|
|
tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP);
|
|
|
|
PACKET("get device status\n");
|
|
|
|
__raw_writeb(tmp, dreg);
|
|
|
|
__raw_writeb(0, dreg);
|
|
|
|
goto write_in;
|
|
|
|
/* then STATUS starts later, automatically */
|
|
|
|
case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
|
|
|
|
| USB_REQ_SET_FEATURE:
|
|
|
|
if (w_value != USB_DEVICE_REMOTE_WAKEUP)
|
|
|
|
goto stall;
|
2006-12-08 14:44:33 +08:00
|
|
|
tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
|
2006-01-23 02:32:37 +08:00
|
|
|
tmp |= AT91_UDP_ESR;
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
|
2006-01-23 02:32:37 +08:00
|
|
|
goto succeed;
|
|
|
|
case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
|
|
|
|
| USB_REQ_CLEAR_FEATURE:
|
|
|
|
if (w_value != USB_DEVICE_REMOTE_WAKEUP)
|
|
|
|
goto stall;
|
2006-12-08 14:44:33 +08:00
|
|
|
tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
|
2006-01-23 02:32:37 +08:00
|
|
|
tmp &= ~AT91_UDP_ESR;
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
|
2006-01-23 02:32:37 +08:00
|
|
|
goto succeed;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interfaces have no feature settings; this is pretty useless.
|
|
|
|
* we won't even insist the interface exists...
|
|
|
|
*/
|
|
|
|
case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
|
|
|
|
| USB_REQ_GET_STATUS:
|
|
|
|
PACKET("get interface status\n");
|
|
|
|
__raw_writeb(0, dreg);
|
|
|
|
__raw_writeb(0, dreg);
|
|
|
|
goto write_in;
|
|
|
|
/* then STATUS starts later, automatically */
|
|
|
|
case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
|
|
|
|
| USB_REQ_SET_FEATURE:
|
|
|
|
case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
|
|
|
|
| USB_REQ_CLEAR_FEATURE:
|
|
|
|
goto stall;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hosts may clear bulk/intr endpoint halt after the gadget
|
|
|
|
* driver sets it (not widely used); or set it (for testing)
|
|
|
|
*/
|
|
|
|
case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
|
|
|
|
| USB_REQ_GET_STATUS:
|
|
|
|
tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
|
|
|
|
ep = &udc->ep[tmp];
|
2012-03-13 02:25:26 +08:00
|
|
|
if (tmp >= NUM_ENDPOINTS || (tmp && !ep->ep.desc))
|
2006-01-23 02:32:37 +08:00
|
|
|
goto stall;
|
|
|
|
|
|
|
|
if (tmp) {
|
|
|
|
if ((w_index & USB_DIR_IN)) {
|
|
|
|
if (!ep->is_in)
|
|
|
|
goto stall;
|
|
|
|
} else if (ep->is_in)
|
|
|
|
goto stall;
|
|
|
|
}
|
|
|
|
PACKET("get %s status\n", ep->ep.name);
|
|
|
|
if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
|
|
|
|
tmp = (1 << USB_ENDPOINT_HALT);
|
|
|
|
else
|
|
|
|
tmp = 0;
|
|
|
|
__raw_writeb(tmp, dreg);
|
|
|
|
__raw_writeb(0, dreg);
|
|
|
|
goto write_in;
|
|
|
|
/* then STATUS starts later, automatically */
|
|
|
|
case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
|
|
|
|
| USB_REQ_SET_FEATURE:
|
|
|
|
tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
|
|
|
|
ep = &udc->ep[tmp];
|
2007-12-10 14:53:09 +08:00
|
|
|
if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
|
2006-01-23 02:32:37 +08:00
|
|
|
goto stall;
|
2012-03-13 02:25:26 +08:00
|
|
|
if (!ep->ep.desc || ep->is_iso)
|
2006-01-23 02:32:37 +08:00
|
|
|
goto stall;
|
|
|
|
if ((w_index & USB_DIR_IN)) {
|
|
|
|
if (!ep->is_in)
|
|
|
|
goto stall;
|
|
|
|
} else if (ep->is_in)
|
|
|
|
goto stall;
|
|
|
|
|
|
|
|
tmp = __raw_readl(ep->creg);
|
|
|
|
tmp &= ~SET_FX;
|
|
|
|
tmp |= CLR_FX | AT91_UDP_FORCESTALL;
|
|
|
|
__raw_writel(tmp, ep->creg);
|
|
|
|
goto succeed;
|
|
|
|
case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
|
|
|
|
| USB_REQ_CLEAR_FEATURE:
|
|
|
|
tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
|
|
|
|
ep = &udc->ep[tmp];
|
2007-12-10 14:53:09 +08:00
|
|
|
if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
|
2006-01-23 02:32:37 +08:00
|
|
|
goto stall;
|
|
|
|
if (tmp == 0)
|
|
|
|
goto succeed;
|
2012-03-13 02:25:26 +08:00
|
|
|
if (!ep->ep.desc || ep->is_iso)
|
2006-01-23 02:32:37 +08:00
|
|
|
goto stall;
|
|
|
|
if ((w_index & USB_DIR_IN)) {
|
|
|
|
if (!ep->is_in)
|
|
|
|
goto stall;
|
|
|
|
} else if (ep->is_in)
|
|
|
|
goto stall;
|
|
|
|
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
|
|
|
|
at91_udp_write(udc, AT91_UDP_RST_EP, 0);
|
2006-01-23 02:32:37 +08:00
|
|
|
tmp = __raw_readl(ep->creg);
|
|
|
|
tmp |= CLR_FX;
|
|
|
|
tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
|
|
|
|
__raw_writel(tmp, ep->creg);
|
|
|
|
if (!list_empty(&ep->queue))
|
|
|
|
handle_ep(ep);
|
|
|
|
goto succeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef w_value
|
|
|
|
#undef w_index
|
|
|
|
#undef w_length
|
|
|
|
|
|
|
|
/* pass request up to the gadget driver */
|
2010-03-02 01:01:56 +08:00
|
|
|
if (udc->driver) {
|
|
|
|
spin_unlock(&udc->lock);
|
2006-12-08 19:26:00 +08:00
|
|
|
status = udc->driver->setup(&udc->gadget, &pkt.r);
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock(&udc->lock);
|
|
|
|
}
|
2006-12-08 19:26:00 +08:00
|
|
|
else
|
|
|
|
status = -ENODEV;
|
2006-01-23 02:32:37 +08:00
|
|
|
if (status < 0) {
|
|
|
|
stall:
|
|
|
|
VDBG("req %02x.%02x protocol STALL; stat %d\n",
|
|
|
|
pkt.r.bRequestType, pkt.r.bRequest, status);
|
|
|
|
csr |= AT91_UDP_FORCESTALL;
|
|
|
|
__raw_writel(csr, creg);
|
|
|
|
udc->req_pending = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
succeed:
|
|
|
|
/* immediate successful (IN) STATUS after zero length DATA */
|
|
|
|
PACKET("ep0 in/status\n");
|
|
|
|
write_in:
|
|
|
|
csr |= AT91_UDP_TXPKTRDY;
|
|
|
|
__raw_writel(csr, creg);
|
|
|
|
udc->req_pending = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_ep0(struct at91_udc *udc)
|
|
|
|
{
|
|
|
|
struct at91_ep *ep0 = &udc->ep[0];
|
|
|
|
u32 __iomem *creg = ep0->creg;
|
|
|
|
u32 csr = __raw_readl(creg);
|
|
|
|
struct at91_request *req;
|
|
|
|
|
|
|
|
if (unlikely(csr & AT91_UDP_STALLSENT)) {
|
|
|
|
nuke(ep0, -EPROTO);
|
|
|
|
udc->req_pending = 0;
|
|
|
|
csr |= CLR_FX;
|
|
|
|
csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL);
|
|
|
|
__raw_writel(csr, creg);
|
|
|
|
VDBG("ep0 stalled\n");
|
|
|
|
csr = __raw_readl(creg);
|
|
|
|
}
|
|
|
|
if (csr & AT91_UDP_RXSETUP) {
|
|
|
|
nuke(ep0, 0);
|
|
|
|
udc->req_pending = 0;
|
|
|
|
handle_setup(udc, ep0, csr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (list_empty(&ep0->queue))
|
|
|
|
req = NULL;
|
|
|
|
else
|
|
|
|
req = list_entry(ep0->queue.next, struct at91_request, queue);
|
|
|
|
|
|
|
|
/* host ACKed an IN packet that we sent */
|
|
|
|
if (csr & AT91_UDP_TXCOMP) {
|
|
|
|
csr |= CLR_FX;
|
|
|
|
csr &= ~(SET_FX | AT91_UDP_TXCOMP);
|
|
|
|
|
|
|
|
/* write more IN DATA? */
|
|
|
|
if (req && ep0->is_in) {
|
|
|
|
if (handle_ep(ep0))
|
|
|
|
udc->req_pending = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ack after:
|
|
|
|
* - last IN DATA packet (including GET_STATUS)
|
|
|
|
* - IN/STATUS for OUT DATA
|
|
|
|
* - IN/STATUS for any zero-length DATA stage
|
|
|
|
* except for the IN DATA case, the host should send
|
|
|
|
* an OUT status later, which we'll ack.
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
udc->req_pending = 0;
|
|
|
|
__raw_writel(csr, creg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SET_ADDRESS takes effect only after the STATUS
|
|
|
|
* (to the original address) gets acked.
|
|
|
|
*/
|
|
|
|
if (udc->wait_for_addr_ack) {
|
|
|
|
u32 tmp;
|
|
|
|
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_FADDR,
|
2006-07-05 17:38:56 +08:00
|
|
|
AT91_UDP_FEN | udc->addr);
|
2006-12-08 14:44:33 +08:00
|
|
|
tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
|
2006-01-23 02:32:37 +08:00
|
|
|
tmp &= ~AT91_UDP_FADDEN;
|
|
|
|
if (udc->addr)
|
|
|
|
tmp |= AT91_UDP_FADDEN;
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
udc->wait_for_addr_ack = 0;
|
|
|
|
VDBG("address %d\n", udc->addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OUT packet arrived ... */
|
|
|
|
else if (csr & AT91_UDP_RX_DATA_BK0) {
|
|
|
|
csr |= CLR_FX;
|
|
|
|
csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
|
|
|
|
|
|
|
|
/* OUT DATA stage */
|
|
|
|
if (!ep0->is_in) {
|
|
|
|
if (req) {
|
|
|
|
if (handle_ep(ep0)) {
|
|
|
|
/* send IN/STATUS */
|
|
|
|
PACKET("ep0 in/status\n");
|
|
|
|
csr = __raw_readl(creg);
|
|
|
|
csr &= ~SET_FX;
|
|
|
|
csr |= CLR_FX | AT91_UDP_TXPKTRDY;
|
|
|
|
__raw_writel(csr, creg);
|
|
|
|
udc->req_pending = 0;
|
|
|
|
}
|
|
|
|
} else if (udc->req_pending) {
|
|
|
|
/*
|
|
|
|
* AT91 hardware has a hard time with this
|
|
|
|
* "deferred response" mode for control-OUT
|
|
|
|
* transfers. (For control-IN it's fine.)
|
|
|
|
*
|
|
|
|
* The normal solution leaves OUT data in the
|
|
|
|
* fifo until the gadget driver is ready.
|
|
|
|
* We couldn't do that here without disabling
|
|
|
|
* the IRQ that tells about SETUP packets,
|
|
|
|
* e.g. when the host gets impatient...
|
|
|
|
*
|
|
|
|
* Working around it by copying into a buffer
|
|
|
|
* would almost be a non-deferred response,
|
|
|
|
* except that it wouldn't permit reliable
|
|
|
|
* stalling of the request. Instead, demand
|
|
|
|
* that gadget drivers not use this mode.
|
|
|
|
*/
|
|
|
|
DBG("no control-OUT deferred responses!\n");
|
|
|
|
__raw_writel(csr | AT91_UDP_FORCESTALL, creg);
|
|
|
|
udc->req_pending = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* STATUS stage for control-IN; ack. */
|
|
|
|
} else {
|
|
|
|
PACKET("ep0 out/status ACK\n");
|
|
|
|
__raw_writel(csr, creg);
|
|
|
|
|
|
|
|
/* "early" status stage */
|
|
|
|
if (req)
|
|
|
|
done(ep0, req, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
IRQ: Maintain regs pointer globally rather than passing to IRQ handlers
Maintain a per-CPU global "struct pt_regs *" variable which can be used instead
of passing regs around manually through all ~1800 interrupt handlers in the
Linux kernel.
The regs pointer is used in few places, but it potentially costs both stack
space and code to pass it around. On the FRV arch, removing the regs parameter
from all the genirq function results in a 20% speed up of the IRQ exit path
(ie: from leaving timer_interrupt() to leaving do_IRQ()).
Where appropriate, an arch may override the generic storage facility and do
something different with the variable. On FRV, for instance, the address is
maintained in GR28 at all times inside the kernel as part of general exception
handling.
Having looked over the code, it appears that the parameter may be handed down
through up to twenty or so layers of functions. Consider a USB character
device attached to a USB hub, attached to a USB controller that posts its
interrupts through a cascaded auxiliary interrupt controller. A character
device driver may want to pass regs to the sysrq handler through the input
layer which adds another few layers of parameter passing.
I've build this code with allyesconfig for x86_64 and i386. I've runtested the
main part of the code on FRV and i386, though I can't test most of the drivers.
I've also done partial conversion for powerpc and MIPS - these at least compile
with minimal configurations.
This will affect all archs. Mostly the changes should be relatively easy.
Take do_IRQ(), store the regs pointer at the beginning, saving the old one:
struct pt_regs *old_regs = set_irq_regs(regs);
And put the old one back at the end:
set_irq_regs(old_regs);
Don't pass regs through to generic_handle_irq() or __do_IRQ().
In timer_interrupt(), this sort of change will be necessary:
- update_process_times(user_mode(regs));
- profile_tick(CPU_PROFILING, regs);
+ update_process_times(user_mode(get_irq_regs()));
+ profile_tick(CPU_PROFILING);
I'd like to move update_process_times()'s use of get_irq_regs() into itself,
except that i386, alone of the archs, uses something other than user_mode().
Some notes on the interrupt handling in the drivers:
(*) input_dev() is now gone entirely. The regs pointer is no longer stored in
the input_dev struct.
(*) finish_unlinks() in drivers/usb/host/ohci-q.c needs checking. It does
something different depending on whether it's been supplied with a regs
pointer or not.
(*) Various IRQ handler function pointers have been moved to type
irq_handler_t.
Signed-Off-By: David Howells <dhowells@redhat.com>
(cherry picked from 1b16e7ac850969f38b375e511e3fa2f474a33867 commit)
2006-10-05 21:55:46 +08:00
|
|
|
static irqreturn_t at91_udc_irq (int irq, void *_udc)
|
2006-01-23 02:32:37 +08:00
|
|
|
{
|
|
|
|
struct at91_udc *udc = _udc;
|
|
|
|
u32 rescans = 5;
|
2010-03-02 00:38:37 +08:00
|
|
|
int disable_clock = 0;
|
2010-03-02 01:01:56 +08:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
2010-03-02 00:38:37 +08:00
|
|
|
|
|
|
|
if (!udc->clocked) {
|
|
|
|
clk_on(udc);
|
|
|
|
disable_clock = 1;
|
|
|
|
}
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
while (rescans--) {
|
2006-07-05 17:38:56 +08:00
|
|
|
u32 status;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2006-12-08 14:44:33 +08:00
|
|
|
status = at91_udp_read(udc, AT91_UDP_ISR)
|
|
|
|
& at91_udp_read(udc, AT91_UDP_IMR);
|
2006-01-23 02:32:37 +08:00
|
|
|
if (!status)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* USB reset irq: not maskable */
|
|
|
|
if (status & AT91_UDP_ENDBUSRES) {
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
|
|
|
|
at91_udp_write(udc, AT91_UDP_IER, MINIMUS_INTERRUPTUS);
|
2006-01-23 02:32:37 +08:00
|
|
|
/* Atmel code clears this irq twice */
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
|
|
|
|
at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
|
2006-01-23 02:32:37 +08:00
|
|
|
VDBG("end bus reset\n");
|
|
|
|
udc->addr = 0;
|
2014-11-06 14:28:02 +08:00
|
|
|
reset_gadget(udc);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
/* enable ep0 */
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_CSR(0),
|
2006-07-05 17:38:56 +08:00
|
|
|
AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
|
2006-01-23 02:32:37 +08:00
|
|
|
udc->gadget.speed = USB_SPEED_FULL;
|
|
|
|
udc->suspended = 0;
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_EP(0));
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: this driver keeps clocks off unless the
|
2006-07-05 17:38:56 +08:00
|
|
|
* USB host is present. That saves power, but for
|
|
|
|
* boards that don't support VBUS detection, both
|
|
|
|
* clocks need to be active most of the time.
|
2006-01-23 02:32:37 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* host initiated suspend (3+ms bus idle) */
|
|
|
|
} else if (status & AT91_UDP_RXSUSP) {
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP);
|
|
|
|
at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM);
|
|
|
|
at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP);
|
2011-10-07 03:36:26 +08:00
|
|
|
/* VDBG("bus suspend\n"); */
|
2006-01-23 02:32:37 +08:00
|
|
|
if (udc->suspended)
|
|
|
|
continue;
|
|
|
|
udc->suspended = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: when suspending a VBUS-powered device, the
|
|
|
|
* gadget driver should switch into slow clock mode
|
|
|
|
* and then into standby to avoid drawing more than
|
|
|
|
* 500uA power (2500uA for some high-power configs).
|
|
|
|
*/
|
2010-03-02 01:01:56 +08:00
|
|
|
if (udc->driver && udc->driver->suspend) {
|
|
|
|
spin_unlock(&udc->lock);
|
2006-01-23 02:32:37 +08:00
|
|
|
udc->driver->suspend(&udc->gadget);
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock(&udc->lock);
|
|
|
|
}
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
/* host initiated resume */
|
|
|
|
} else if (status & AT91_UDP_RXRSM) {
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
|
|
|
|
at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP);
|
|
|
|
at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
|
2011-10-07 03:36:26 +08:00
|
|
|
/* VDBG("bus resume\n"); */
|
2006-01-23 02:32:37 +08:00
|
|
|
if (!udc->suspended)
|
|
|
|
continue;
|
|
|
|
udc->suspended = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: for a VBUS-powered device, the gadget driver
|
|
|
|
* would normally want to switch out of slow clock
|
|
|
|
* mode into normal mode.
|
|
|
|
*/
|
2010-03-02 01:01:56 +08:00
|
|
|
if (udc->driver && udc->driver->resume) {
|
|
|
|
spin_unlock(&udc->lock);
|
2006-01-23 02:32:37 +08:00
|
|
|
udc->driver->resume(&udc->gadget);
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock(&udc->lock);
|
|
|
|
}
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
/* endpoint IRQs are cleared by handling them */
|
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
unsigned mask = 1;
|
|
|
|
struct at91_ep *ep = &udc->ep[1];
|
|
|
|
|
|
|
|
if (status & mask)
|
|
|
|
handle_ep0(udc);
|
|
|
|
for (i = 1; i < NUM_ENDPOINTS; i++) {
|
|
|
|
mask <<= 1;
|
|
|
|
if (status & mask)
|
|
|
|
handle_ep(ep);
|
|
|
|
ep++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-02 00:38:37 +08:00
|
|
|
if (disable_clock)
|
|
|
|
clk_off(udc);
|
|
|
|
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
|
|
|
|
2006-01-23 02:32:37 +08:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2010-07-13 12:09:16 +08:00
|
|
|
static void at91_vbus_update(struct at91_udc *udc, unsigned value)
|
|
|
|
{
|
|
|
|
value ^= udc->board.vbus_active_low;
|
|
|
|
if (value != udc->vbus)
|
|
|
|
at91_vbus_session(&udc->gadget, value);
|
|
|
|
}
|
|
|
|
|
IRQ: Maintain regs pointer globally rather than passing to IRQ handlers
Maintain a per-CPU global "struct pt_regs *" variable which can be used instead
of passing regs around manually through all ~1800 interrupt handlers in the
Linux kernel.
The regs pointer is used in few places, but it potentially costs both stack
space and code to pass it around. On the FRV arch, removing the regs parameter
from all the genirq function results in a 20% speed up of the IRQ exit path
(ie: from leaving timer_interrupt() to leaving do_IRQ()).
Where appropriate, an arch may override the generic storage facility and do
something different with the variable. On FRV, for instance, the address is
maintained in GR28 at all times inside the kernel as part of general exception
handling.
Having looked over the code, it appears that the parameter may be handed down
through up to twenty or so layers of functions. Consider a USB character
device attached to a USB hub, attached to a USB controller that posts its
interrupts through a cascaded auxiliary interrupt controller. A character
device driver may want to pass regs to the sysrq handler through the input
layer which adds another few layers of parameter passing.
I've build this code with allyesconfig for x86_64 and i386. I've runtested the
main part of the code on FRV and i386, though I can't test most of the drivers.
I've also done partial conversion for powerpc and MIPS - these at least compile
with minimal configurations.
This will affect all archs. Mostly the changes should be relatively easy.
Take do_IRQ(), store the regs pointer at the beginning, saving the old one:
struct pt_regs *old_regs = set_irq_regs(regs);
And put the old one back at the end:
set_irq_regs(old_regs);
Don't pass regs through to generic_handle_irq() or __do_IRQ().
In timer_interrupt(), this sort of change will be necessary:
- update_process_times(user_mode(regs));
- profile_tick(CPU_PROFILING, regs);
+ update_process_times(user_mode(get_irq_regs()));
+ profile_tick(CPU_PROFILING);
I'd like to move update_process_times()'s use of get_irq_regs() into itself,
except that i386, alone of the archs, uses something other than user_mode().
Some notes on the interrupt handling in the drivers:
(*) input_dev() is now gone entirely. The regs pointer is no longer stored in
the input_dev struct.
(*) finish_unlinks() in drivers/usb/host/ohci-q.c needs checking. It does
something different depending on whether it's been supplied with a regs
pointer or not.
(*) Various IRQ handler function pointers have been moved to type
irq_handler_t.
Signed-Off-By: David Howells <dhowells@redhat.com>
(cherry picked from 1b16e7ac850969f38b375e511e3fa2f474a33867 commit)
2006-10-05 21:55:46 +08:00
|
|
|
static irqreturn_t at91_vbus_irq(int irq, void *_udc)
|
2006-01-23 02:32:37 +08:00
|
|
|
{
|
|
|
|
struct at91_udc *udc = _udc;
|
|
|
|
|
|
|
|
/* vbus needs at least brief debouncing */
|
|
|
|
udelay(10);
|
2010-07-13 12:09:16 +08:00
|
|
|
at91_vbus_update(udc, gpio_get_value(udc->board.vbus_pin));
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2010-07-13 12:09:16 +08:00
|
|
|
static void at91_vbus_timer_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct at91_udc *udc = container_of(work, struct at91_udc,
|
|
|
|
vbus_timer_work);
|
|
|
|
|
|
|
|
at91_vbus_update(udc, gpio_get_value_cansleep(udc->board.vbus_pin));
|
|
|
|
|
|
|
|
if (!timer_pending(&udc->vbus_timer))
|
|
|
|
mod_timer(&udc->vbus_timer, jiffies + VBUS_POLL_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-17 05:43:17 +08:00
|
|
|
static void at91_vbus_timer(struct timer_list *t)
|
2010-07-13 12:09:16 +08:00
|
|
|
{
|
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-17 05:43:17 +08:00
|
|
|
struct at91_udc *udc = from_timer(udc, t, vbus_timer);
|
2010-07-13 12:09:16 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are polling vbus it is likely that the gpio is on an
|
|
|
|
* bus such as i2c or spi which may sleep, so schedule some work
|
|
|
|
* to read the vbus gpio
|
|
|
|
*/
|
2012-12-22 09:57:12 +08:00
|
|
|
schedule_work(&udc->vbus_timer_work);
|
2010-07-13 12:09:16 +08:00
|
|
|
}
|
|
|
|
|
2012-02-05 01:55:23 +08:00
|
|
|
static int at91_start(struct usb_gadget *gadget,
|
|
|
|
struct usb_gadget_driver *driver)
|
2006-01-23 02:32:37 +08:00
|
|
|
{
|
2012-02-05 01:55:23 +08:00
|
|
|
struct at91_udc *udc;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2012-02-05 01:55:23 +08:00
|
|
|
udc = container_of(gadget, struct at91_udc, gadget);
|
2006-01-23 02:32:37 +08:00
|
|
|
udc->driver = driver;
|
2012-06-26 22:27:12 +08:00
|
|
|
udc->gadget.dev.of_node = udc->pdev->dev.of_node;
|
2006-01-23 02:32:37 +08:00
|
|
|
udc->enabled = 1;
|
2015-01-28 16:32:26 +08:00
|
|
|
udc->gadget.is_selfpowered = 1;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-18 01:05:12 +08:00
|
|
|
static int at91_stop(struct usb_gadget *gadget)
|
2006-01-23 02:32:37 +08:00
|
|
|
{
|
2012-02-05 01:55:23 +08:00
|
|
|
struct at91_udc *udc;
|
2010-03-02 01:01:56 +08:00
|
|
|
unsigned long flags;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2012-02-05 01:55:23 +08:00
|
|
|
udc = container_of(gadget, struct at91_udc, gadget);
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
udc->enabled = 0;
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_IDR, ~0);
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
udc->driver = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static void at91udc_shutdown(struct platform_device *dev)
|
|
|
|
{
|
2010-03-02 01:01:56 +08:00
|
|
|
struct at91_udc *udc = platform_get_drvdata(dev);
|
|
|
|
unsigned long flags;
|
|
|
|
|
2006-01-23 02:32:37 +08:00
|
|
|
/* force disconnect on reboot */
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
pullup(platform_get_drvdata(dev), 0);
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
}
|
|
|
|
|
2015-01-15 00:22:02 +08:00
|
|
|
static int at91rm9200_udc_init(struct at91_udc *udc)
|
|
|
|
{
|
|
|
|
struct at91_ep *ep;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_ENDPOINTS; i++) {
|
|
|
|
ep = &udc->ep[i];
|
|
|
|
|
|
|
|
switch (i) {
|
|
|
|
case 0:
|
|
|
|
case 3:
|
|
|
|
ep->maxpacket = 8;
|
|
|
|
break;
|
|
|
|
case 1 ... 2:
|
|
|
|
ep->maxpacket = 64;
|
|
|
|
break;
|
|
|
|
case 4 ... 5:
|
|
|
|
ep->maxpacket = 256;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gpio_is_valid(udc->board.pullup_pin)) {
|
|
|
|
DBG("no D+ pullup?\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = devm_gpio_request(&udc->pdev->dev, udc->board.pullup_pin,
|
|
|
|
"udc_pullup");
|
|
|
|
if (ret) {
|
|
|
|
DBG("D+ pullup is busy\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpio_direction_output(udc->board.pullup_pin,
|
|
|
|
udc->board.pullup_active_low);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void at91rm9200_udc_pullup(struct at91_udc *udc, int is_on)
|
|
|
|
{
|
|
|
|
int active = !udc->board.pullup_active_low;
|
|
|
|
|
|
|
|
if (is_on)
|
|
|
|
gpio_set_value(udc->board.pullup_pin, active);
|
|
|
|
else
|
|
|
|
gpio_set_value(udc->board.pullup_pin, !active);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct at91_udc_caps at91rm9200_udc_caps = {
|
|
|
|
.init = at91rm9200_udc_init,
|
|
|
|
.pullup = at91rm9200_udc_pullup,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int at91sam9260_udc_init(struct at91_udc *udc)
|
|
|
|
{
|
|
|
|
struct at91_ep *ep;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_ENDPOINTS; i++) {
|
|
|
|
ep = &udc->ep[i];
|
|
|
|
|
|
|
|
switch (i) {
|
|
|
|
case 0 ... 3:
|
|
|
|
ep->maxpacket = 64;
|
|
|
|
break;
|
|
|
|
case 4 ... 5:
|
|
|
|
ep->maxpacket = 512;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void at91sam9260_udc_pullup(struct at91_udc *udc, int is_on)
|
|
|
|
{
|
|
|
|
u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
|
|
|
|
|
|
|
|
if (is_on)
|
|
|
|
txvc |= AT91_UDP_TXVC_PUON;
|
|
|
|
else
|
|
|
|
txvc &= ~AT91_UDP_TXVC_PUON;
|
|
|
|
|
|
|
|
at91_udp_write(udc, AT91_UDP_TXVC, txvc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct at91_udc_caps at91sam9260_udc_caps = {
|
|
|
|
.init = at91sam9260_udc_init,
|
|
|
|
.pullup = at91sam9260_udc_pullup,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int at91sam9261_udc_init(struct at91_udc *udc)
|
|
|
|
{
|
|
|
|
struct at91_ep *ep;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_ENDPOINTS; i++) {
|
|
|
|
ep = &udc->ep[i];
|
|
|
|
|
|
|
|
switch (i) {
|
|
|
|
case 0:
|
|
|
|
ep->maxpacket = 8;
|
|
|
|
break;
|
|
|
|
case 1 ... 3:
|
|
|
|
ep->maxpacket = 64;
|
|
|
|
break;
|
|
|
|
case 4 ... 5:
|
|
|
|
ep->maxpacket = 256;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
udc->matrix = syscon_regmap_lookup_by_phandle(udc->pdev->dev.of_node,
|
|
|
|
"atmel,matrix");
|
2016-03-29 17:26:06 +08:00
|
|
|
return PTR_ERR_OR_ZERO(udc->matrix);
|
2015-01-15 00:22:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void at91sam9261_udc_pullup(struct at91_udc *udc, int is_on)
|
|
|
|
{
|
|
|
|
u32 usbpucr = 0;
|
|
|
|
|
|
|
|
if (is_on)
|
|
|
|
usbpucr = AT91_MATRIX_USBPUCR_PUON;
|
|
|
|
|
|
|
|
regmap_update_bits(udc->matrix, AT91SAM9261_MATRIX_USBPUCR,
|
|
|
|
AT91_MATRIX_USBPUCR_PUON, usbpucr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct at91_udc_caps at91sam9261_udc_caps = {
|
|
|
|
.init = at91sam9261_udc_init,
|
|
|
|
.pullup = at91sam9261_udc_pullup,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int at91sam9263_udc_init(struct at91_udc *udc)
|
|
|
|
{
|
|
|
|
struct at91_ep *ep;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_ENDPOINTS; i++) {
|
|
|
|
ep = &udc->ep[i];
|
|
|
|
|
|
|
|
switch (i) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
ep->maxpacket = 64;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
ep->maxpacket = 256;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct at91_udc_caps at91sam9263_udc_caps = {
|
|
|
|
.init = at91sam9263_udc_init,
|
|
|
|
.pullup = at91sam9260_udc_pullup,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct of_device_id at91_udc_dt_ids[] = {
|
|
|
|
{
|
|
|
|
.compatible = "atmel,at91rm9200-udc",
|
|
|
|
.data = &at91rm9200_udc_caps,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.compatible = "atmel,at91sam9260-udc",
|
|
|
|
.data = &at91sam9260_udc_caps,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.compatible = "atmel,at91sam9261-udc",
|
|
|
|
.data = &at91sam9261_udc_caps,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.compatible = "atmel,at91sam9263-udc",
|
|
|
|
.data = &at91sam9263_udc_caps,
|
|
|
|
},
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, at91_udc_dt_ids);
|
|
|
|
|
|
|
|
static void at91udc_of_init(struct at91_udc *udc, struct device_node *np)
|
2012-01-28 22:35:36 +08:00
|
|
|
{
|
|
|
|
struct at91_udc_data *board = &udc->board;
|
2015-01-15 00:22:02 +08:00
|
|
|
const struct of_device_id *match;
|
2012-01-28 22:35:36 +08:00
|
|
|
enum of_gpio_flags flags;
|
2015-01-15 00:22:02 +08:00
|
|
|
u32 val;
|
2012-01-28 22:35:36 +08:00
|
|
|
|
|
|
|
if (of_property_read_u32(np, "atmel,vbus-polled", &val) == 0)
|
|
|
|
board->vbus_polled = 1;
|
|
|
|
|
|
|
|
board->vbus_pin = of_get_named_gpio_flags(np, "atmel,vbus-gpio", 0,
|
|
|
|
&flags);
|
|
|
|
board->vbus_active_low = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0;
|
|
|
|
|
|
|
|
board->pullup_pin = of_get_named_gpio_flags(np, "atmel,pullup-gpio", 0,
|
|
|
|
&flags);
|
|
|
|
|
|
|
|
board->pullup_active_low = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0;
|
2015-01-15 00:22:02 +08:00
|
|
|
|
|
|
|
match = of_match_node(at91_udc_dt_ids, np);
|
|
|
|
if (match)
|
|
|
|
udc->caps = match->data;
|
2012-01-28 22:35:36 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 02:21:48 +08:00
|
|
|
static int at91udc_probe(struct platform_device *pdev)
|
2006-01-23 02:32:37 +08:00
|
|
|
{
|
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
struct at91_udc *udc;
|
|
|
|
int retval;
|
2006-12-08 14:44:33 +08:00
|
|
|
struct resource *res;
|
2015-01-15 00:22:02 +08:00
|
|
|
struct at91_ep *ep;
|
|
|
|
int i;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2015-01-15 00:22:04 +08:00
|
|
|
udc = devm_kzalloc(dev, sizeof(*udc), GFP_KERNEL);
|
|
|
|
if (!udc)
|
|
|
|
return -ENOMEM;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
/* init software state */
|
|
|
|
udc->gadget.dev.parent = dev;
|
2015-01-15 00:22:00 +08:00
|
|
|
at91udc_of_init(udc, pdev->dev.of_node);
|
2006-01-23 02:32:37 +08:00
|
|
|
udc->pdev = pdev;
|
|
|
|
udc->enabled = 0;
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock_init(&udc->lock);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2015-01-15 00:22:04 +08:00
|
|
|
udc->gadget.ops = &at91_udc_ops;
|
|
|
|
udc->gadget.ep0 = &udc->ep[0].ep;
|
|
|
|
udc->gadget.name = driver_name;
|
|
|
|
udc->gadget.dev.init_name = "gadget";
|
2008-01-06 05:21:43 +08:00
|
|
|
|
2015-01-15 00:22:04 +08:00
|
|
|
for (i = 0; i < NUM_ENDPOINTS; i++) {
|
|
|
|
ep = &udc->ep[i];
|
2015-07-31 22:00:21 +08:00
|
|
|
ep->ep.name = ep_info[i].name;
|
|
|
|
ep->ep.caps = ep_info[i].caps;
|
2015-01-15 00:22:04 +08:00
|
|
|
ep->ep.ops = &at91_ep_ops;
|
|
|
|
ep->udc = udc;
|
|
|
|
ep->int_mask = BIT(i);
|
|
|
|
if (i != 0 && i != 3)
|
|
|
|
ep->is_pingpong = 1;
|
2008-05-28 10:24:20 +08:00
|
|
|
}
|
|
|
|
|
2015-01-15 00:22:01 +08:00
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
|
|
udc->udp_baseaddr = devm_ioremap_resource(dev, res);
|
|
|
|
if (IS_ERR(udc->udp_baseaddr))
|
|
|
|
return PTR_ERR(udc->udp_baseaddr);
|
2006-12-08 14:44:33 +08:00
|
|
|
|
2015-01-15 00:22:02 +08:00
|
|
|
if (udc->caps && udc->caps->init) {
|
|
|
|
retval = udc->caps->init(udc);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
2006-12-08 14:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
udc_reinit(udc);
|
|
|
|
|
2006-01-23 02:32:37 +08:00
|
|
|
/* get interface and function clocks */
|
2015-01-15 00:22:01 +08:00
|
|
|
udc->iclk = devm_clk_get(dev, "pclk");
|
|
|
|
if (IS_ERR(udc->iclk))
|
|
|
|
return PTR_ERR(udc->iclk);
|
|
|
|
|
|
|
|
udc->fclk = devm_clk_get(dev, "hclk");
|
|
|
|
if (IS_ERR(udc->fclk))
|
|
|
|
return PTR_ERR(udc->fclk);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2006-07-05 17:38:56 +08:00
|
|
|
/* don't do anything until we have both gadget driver and VBUS */
|
2015-01-15 00:21:58 +08:00
|
|
|
clk_set_rate(udc->fclk, 48000000);
|
2014-11-19 23:37:27 +08:00
|
|
|
retval = clk_prepare(udc->fclk);
|
|
|
|
if (retval)
|
2015-01-15 00:22:01 +08:00
|
|
|
return retval;
|
2014-11-19 23:37:27 +08:00
|
|
|
|
2013-06-25 16:12:56 +08:00
|
|
|
retval = clk_prepare_enable(udc->iclk);
|
|
|
|
if (retval)
|
2015-01-15 00:22:01 +08:00
|
|
|
goto err_unprepare_fclk;
|
|
|
|
|
2006-12-08 14:44:33 +08:00
|
|
|
at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
|
|
|
|
at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff);
|
2006-12-08 14:44:38 +08:00
|
|
|
/* Clear all pending interrupts - UDP may be used by bootloader. */
|
|
|
|
at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff);
|
2014-11-19 23:37:27 +08:00
|
|
|
clk_disable(udc->iclk);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
/* request UDC and maybe VBUS irqs */
|
2006-07-05 17:38:56 +08:00
|
|
|
udc->udp_irq = platform_get_irq(pdev, 0);
|
2015-01-15 00:22:01 +08:00
|
|
|
retval = devm_request_irq(dev, udc->udp_irq, at91_udc_irq, 0,
|
|
|
|
driver_name, udc);
|
|
|
|
if (retval) {
|
2006-07-05 17:38:56 +08:00
|
|
|
DBG("request irq %d failed\n", udc->udp_irq);
|
2015-01-15 00:22:01 +08:00
|
|
|
goto err_unprepare_iclk;
|
2006-01-23 02:32:37 +08:00
|
|
|
}
|
2015-01-15 00:22:01 +08:00
|
|
|
|
2011-09-19 15:31:58 +08:00
|
|
|
if (gpio_is_valid(udc->board.vbus_pin)) {
|
2015-01-15 00:22:01 +08:00
|
|
|
retval = devm_gpio_request(dev, udc->board.vbus_pin,
|
|
|
|
"udc_vbus");
|
|
|
|
if (retval) {
|
2008-01-06 05:21:43 +08:00
|
|
|
DBG("request vbus pin failed\n");
|
2015-01-15 00:22:01 +08:00
|
|
|
goto err_unprepare_iclk;
|
2008-01-06 05:21:43 +08:00
|
|
|
}
|
2015-01-15 00:22:01 +08:00
|
|
|
|
2008-01-06 05:21:43 +08:00
|
|
|
gpio_direction_input(udc->board.vbus_pin);
|
|
|
|
|
2006-12-08 14:44:38 +08:00
|
|
|
/*
|
|
|
|
* Get the initial state of VBUS - we cannot expect
|
|
|
|
* a pending interrupt.
|
|
|
|
*/
|
2010-07-13 12:09:16 +08:00
|
|
|
udc->vbus = gpio_get_value_cansleep(udc->board.vbus_pin) ^
|
|
|
|
udc->board.vbus_active_low;
|
|
|
|
|
|
|
|
if (udc->board.vbus_polled) {
|
|
|
|
INIT_WORK(&udc->vbus_timer_work, at91_vbus_timer_work);
|
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-17 05:43:17 +08:00
|
|
|
timer_setup(&udc->vbus_timer, at91_vbus_timer, 0);
|
2010-07-13 12:09:16 +08:00
|
|
|
mod_timer(&udc->vbus_timer,
|
|
|
|
jiffies + VBUS_POLL_TIMEOUT);
|
|
|
|
} else {
|
2015-01-15 00:22:01 +08:00
|
|
|
retval = devm_request_irq(dev,
|
|
|
|
gpio_to_irq(udc->board.vbus_pin),
|
|
|
|
at91_vbus_irq, 0, driver_name, udc);
|
|
|
|
if (retval) {
|
2010-07-13 12:09:16 +08:00
|
|
|
DBG("request vbus irq %d failed\n",
|
|
|
|
udc->board.vbus_pin);
|
2015-01-15 00:22:01 +08:00
|
|
|
goto err_unprepare_iclk;
|
2010-07-13 12:09:16 +08:00
|
|
|
}
|
2006-01-23 02:32:37 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DBG("no VBUS detection, assuming always-on\n");
|
|
|
|
udc->vbus = 1;
|
|
|
|
}
|
2011-06-28 21:33:47 +08:00
|
|
|
retval = usb_add_gadget_udc(dev, &udc->gadget);
|
|
|
|
if (retval)
|
2015-01-15 00:22:01 +08:00
|
|
|
goto err_unprepare_iclk;
|
2006-01-23 02:32:37 +08:00
|
|
|
dev_set_drvdata(dev, udc);
|
2006-07-05 17:38:56 +08:00
|
|
|
device_init_wakeup(dev, 1);
|
2006-01-23 02:32:37 +08:00
|
|
|
create_debug_file(udc);
|
|
|
|
|
|
|
|
INFO("%s version %s\n", driver_name, DRIVER_VERSION);
|
|
|
|
return 0;
|
2015-01-15 00:22:01 +08:00
|
|
|
|
|
|
|
err_unprepare_iclk:
|
2014-11-19 23:37:27 +08:00
|
|
|
clk_unprepare(udc->iclk);
|
2015-01-15 00:22:01 +08:00
|
|
|
err_unprepare_fclk:
|
2014-11-19 23:37:27 +08:00
|
|
|
clk_unprepare(udc->fclk);
|
2015-01-15 00:22:01 +08:00
|
|
|
|
2006-01-23 02:32:37 +08:00
|
|
|
DBG("%s probe failed, %d\n", driver_name, retval);
|
2015-01-15 00:22:01 +08:00
|
|
|
|
2006-01-23 02:32:37 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-04-11 06:14:21 +08:00
|
|
|
static int at91udc_remove(struct platform_device *pdev)
|
2006-01-23 02:32:37 +08:00
|
|
|
{
|
2006-07-05 17:38:56 +08:00
|
|
|
struct at91_udc *udc = platform_get_drvdata(pdev);
|
2010-03-02 01:01:56 +08:00
|
|
|
unsigned long flags;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
|
|
|
DBG("remove\n");
|
|
|
|
|
2011-06-28 21:33:47 +08:00
|
|
|
usb_del_gadget_udc(&udc->gadget);
|
2006-12-05 19:15:33 +08:00
|
|
|
if (udc->driver)
|
|
|
|
return -EBUSY;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
2006-12-05 19:15:33 +08:00
|
|
|
pullup(udc, 0);
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2006-07-05 17:38:56 +08:00
|
|
|
device_init_wakeup(&pdev->dev, 0);
|
2006-01-23 02:32:37 +08:00
|
|
|
remove_debug_file(udc);
|
2014-11-19 23:37:27 +08:00
|
|
|
clk_unprepare(udc->fclk);
|
|
|
|
clk_unprepare(udc->iclk);
|
|
|
|
|
2006-01-23 02:32:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PM
|
2006-07-05 17:38:56 +08:00
|
|
|
static int at91udc_suspend(struct platform_device *pdev, pm_message_t mesg)
|
2006-01-23 02:32:37 +08:00
|
|
|
{
|
2006-07-05 17:38:56 +08:00
|
|
|
struct at91_udc *udc = platform_get_drvdata(pdev);
|
|
|
|
int wake = udc->driver && device_may_wakeup(&pdev->dev);
|
2010-03-02 01:01:56 +08:00
|
|
|
unsigned long flags;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2006-07-05 17:38:56 +08:00
|
|
|
/* Unless we can act normally to the host (letting it wake us up
|
|
|
|
* whenever it has work for us) force disconnect. Wakeup requires
|
|
|
|
* PLLB for USB events (signaling for reset, wakeup, or incoming
|
|
|
|
* tokens) and VBUS irqs (on systems which support them).
|
2006-01-23 02:32:37 +08:00
|
|
|
*/
|
2006-07-05 17:38:56 +08:00
|
|
|
if ((!udc->suspended && udc->addr)
|
|
|
|
|| !wake
|
|
|
|
|| at91_suspend_entering_slow_clock()) {
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
2006-01-23 02:32:37 +08:00
|
|
|
pullup(udc, 0);
|
2007-01-17 04:46:39 +08:00
|
|
|
wake = 0;
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
2006-07-05 17:38:56 +08:00
|
|
|
} else
|
|
|
|
enable_irq_wake(udc->udp_irq);
|
|
|
|
|
2007-01-17 04:46:39 +08:00
|
|
|
udc->active_suspend = wake;
|
2011-09-19 15:31:58 +08:00
|
|
|
if (gpio_is_valid(udc->board.vbus_pin) && !udc->board.vbus_polled && wake)
|
2007-01-17 04:46:39 +08:00
|
|
|
enable_irq_wake(udc->board.vbus_pin);
|
2006-01-23 02:32:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-05 17:38:56 +08:00
|
|
|
static int at91udc_resume(struct platform_device *pdev)
|
2006-01-23 02:32:37 +08:00
|
|
|
{
|
2006-07-05 17:38:56 +08:00
|
|
|
struct at91_udc *udc = platform_get_drvdata(pdev);
|
2010-03-02 01:01:56 +08:00
|
|
|
unsigned long flags;
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2011-09-19 15:31:58 +08:00
|
|
|
if (gpio_is_valid(udc->board.vbus_pin) && !udc->board.vbus_polled &&
|
2010-07-13 12:09:16 +08:00
|
|
|
udc->active_suspend)
|
2007-01-17 04:46:39 +08:00
|
|
|
disable_irq_wake(udc->board.vbus_pin);
|
|
|
|
|
2006-01-23 02:32:37 +08:00
|
|
|
/* maybe reconnect to host; if so, clocks on */
|
2007-01-17 04:46:39 +08:00
|
|
|
if (udc->active_suspend)
|
|
|
|
disable_irq_wake(udc->udp_irq);
|
2010-03-02 01:01:56 +08:00
|
|
|
else {
|
|
|
|
spin_lock_irqsave(&udc->lock, flags);
|
2007-01-17 04:46:39 +08:00
|
|
|
pullup(udc, 1);
|
2010-03-02 01:01:56 +08:00
|
|
|
spin_unlock_irqrestore(&udc->lock, flags);
|
|
|
|
}
|
2006-01-23 02:32:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define at91udc_suspend NULL
|
|
|
|
#define at91udc_resume NULL
|
|
|
|
#endif
|
|
|
|
|
2007-02-25 05:54:56 +08:00
|
|
|
static struct platform_driver at91_udc_driver = {
|
2015-04-11 06:14:21 +08:00
|
|
|
.remove = at91udc_remove,
|
2006-01-23 02:32:37 +08:00
|
|
|
.shutdown = at91udc_shutdown,
|
|
|
|
.suspend = at91udc_suspend,
|
2006-07-05 17:38:56 +08:00
|
|
|
.resume = at91udc_resume,
|
2006-01-23 02:32:37 +08:00
|
|
|
.driver = {
|
|
|
|
.name = (char *) driver_name,
|
2015-01-15 00:22:00 +08:00
|
|
|
.of_match_table = at91_udc_dt_ids,
|
2006-01-23 02:32:37 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2013-01-09 19:15:28 +08:00
|
|
|
module_platform_driver_probe(at91_udc_driver, at91udc_probe);
|
2006-01-23 02:32:37 +08:00
|
|
|
|
2006-07-05 17:38:56 +08:00
|
|
|
MODULE_DESCRIPTION("AT91 udc driver");
|
2006-01-23 02:32:37 +08:00
|
|
|
MODULE_AUTHOR("Thomas Rathbone, David Brownell");
|
|
|
|
MODULE_LICENSE("GPL");
|
2008-04-11 12:29:21 +08:00
|
|
|
MODULE_ALIAS("platform:at91_udc");
|