From 29e73e40f5bda71f16ada687015545c79d005e34 Mon Sep 17 00:00:00 2001
From: Ajay Kumar Gupta <ajay.gupta@ti.com>
Date: Wed, 2 Nov 2011 11:15:40 +0530
Subject: [PATCH 1/5] usb: musb_core: kill all global and static variables

This needed for dual instances support.
Changes include:
	- Move fifo_mode, orig_dma_mask, otg_timer, first to musb struct
	- Killed option for use_dma
---
 drivers/usb/musb/musb_core.c |   80 +++++++++++++-----------------------------
 drivers/usb/musb/musb_core.h |    7 ++++
 2 files changed, 32 insertions(+), 55 deletions(-)

diff --git a/drivers/usb/musb/musb_core.c b/drivers/usb/musb/musb_core.c
index 16eec45..86232ae 100644
--- a/drivers/usb/musb/musb_core.c
+++ b/drivers/usb/musb/musb_core.c
@@ -399,8 +399,6 @@ void musb_otg_timer_func(unsigned long data)
 	spin_unlock_irqrestore(&musb->lock, flags);
 }
 
-static DEFINE_TIMER(musb_otg_timer, musb_otg_timer_func, 0, 0);
-
 /*
  * Stops the B-device HNP state. Caller must take care of locking.
  */
@@ -664,8 +662,7 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
 			if (musb->is_active) {
 				musb->xceiv->state = OTG_STATE_B_WAIT_ACON;
 				dev_dbg(musb->controller, "HNP: Setting timer for b_ase0_brst\n");
-				musb_otg_timer.data = (unsigned long)musb;
-				mod_timer(&musb_otg_timer, jiffies
+				mod_timer(&musb->otg_timer, jiffies
 					+ msecs_to_jiffies(TB_ASE0_BRST));
 			}
 			break;
@@ -826,8 +823,7 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
 			case OTG_STATE_A_WAIT_BCON:	/* OPT TD.4.7-900ms */
 				dev_dbg(musb->controller, "HNP: Setting timer as %s\n",
 						otg_state_string(musb->xceiv->state));
-				musb_otg_timer.data = (unsigned long)musb;
-				mod_timer(&musb_otg_timer, jiffies
+				mod_timer(&musb->otg_timer, jiffies
 						+ msecs_to_jiffies(100));
 				break;
 			case OTG_STATE_A_PERIPHERAL:
@@ -1024,22 +1020,6 @@ static void musb_shutdown(struct platform_device *pdev)
 /*-------------------------------------------------------------------------*/
 
 /*
- * The silicon either has hard-wired endpoint configurations, or else
- * "dynamic fifo" sizing.  The driver has support for both, though at this
- * writing only the dynamic sizing is very well tested.   Since we switched
- * away from compile-time hardware parameters, we can no longer rely on
- * dead code elimination to leave only the relevant one in the object file.
- *
- * We don't currently use dynamic fifo setup capability to do anything
- * more than selecting one of a bunch of predefined configurations.
- */
-static short __devinitdata fifo_mode = -1;
-
-/* "modprobe ... fifo_mode=1" etc */
-module_param(fifo_mode, short, 0);
-MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
-
-/*
  * tables defining fifo_mode values.  define more if you like.
  * for host side, make sure both halves of ep1 are set up.
  */
@@ -1253,7 +1233,7 @@ fifo_setup(struct musb *musb, struct musb_hw_ep  *hw_ep,
 	return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
 }
 
-	static struct musb_fifo_cfg __devinitdata ep0_cfg = {
+static struct musb_fifo_cfg __devinitdata ep0_cfg = {
 	.style = FIFO_RXTX, .maxpacket = 64,
 };
 
@@ -1265,16 +1245,16 @@ static int __devinit ep_config_from_table(struct musb *musb)
 	struct musb_hw_ep	*hw_ep = musb->endpoints;
 
 	if (musb->config->fifo_mode)
-		fifo_mode = musb->config->fifo_mode;
+		musb->fifo_mode = musb->config->fifo_mode;
 	else if (musb->config->fifo_cfg) {
 		cfg = musb->config->fifo_cfg;
 		n = musb->config->fifo_cfg_size;
 		goto done;
 	}
 
-	switch (fifo_mode) {
+	switch (musb->fifo_mode) {
 	default:
-		fifo_mode = 0;
+		musb->fifo_mode = 0;
 		/* FALLTHROUGH */
 	case 0:
 		cfg = mode_0_cfg;
@@ -1307,7 +1287,7 @@ static int __devinit ep_config_from_table(struct musb *musb)
 	}
 
 	printk(KERN_DEBUG "%s: setup fifo_mode %d\n",
-			musb_driver_name, fifo_mode);
+			musb_driver_name, musb->fifo_mode);
 
 
 done:
@@ -1642,12 +1622,6 @@ irqreturn_t musb_interrupt(struct musb *musb)
 EXPORT_SYMBOL_GPL(musb_interrupt);
 
 #ifndef CONFIG_MUSB_PIO_ONLY
-static int __devinitdata use_dma = 1;
-
-/* "modprobe ... use_dma=0" etc */
-module_param(use_dma, bool, 0);
-MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
-
 void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
 {
 	u8	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
@@ -1686,9 +1660,6 @@ void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
 	}
 }
 EXPORT_SYMBOL_GPL(musb_dma_completion);
-
-#else
-#define use_dma			0
 #endif
 
 /*-------------------------------------------------------------------------*/
@@ -1813,10 +1784,9 @@ static const struct attribute_group musb_attr_group = {
 static void musb_irq_work(struct work_struct *data)
 {
 	struct musb *musb = container_of(data, struct musb, irq_work);
-	static int old_state;
 
-	if (musb->xceiv->state != old_state) {
-		old_state = musb->xceiv->state;
+	if (musb->xceiv->state != musb->old_state) {
+		musb->old_state = musb->xceiv->state;
 		sysfs_notify(&musb->controller->kobj, NULL, "mode");
 	}
 }
@@ -1895,6 +1865,8 @@ static void musb_free(struct musb *musb)
 	if (musb->gb_queue)
 		destroy_workqueue(musb->gb_queue);
 
+	del_timer_sync(&musb->otg_timer);
+
 	kfree(musb);
 }
 
@@ -1943,10 +1915,13 @@ musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl,
 	musb->min_power = plat->min_power;
 	musb->ops = plat->platform_ops;
 	musb->id = pdev->id;
+	musb->first = 1;
 
-	if (fifo_mode == -1)
-		fifo_mode = musb->ops->fifo_mode;
+	musb->fifo_mode = musb->ops->fifo_mode;
 
+#ifndef CONFIG_MUSB_PIO_ONLY
+	musb->orig_dma_mask = dev->dma_mask;
+#endif
 	if (musb->ops->flags & MUSB_GLUE_TUSB_STYLE) {
 		musb_readb = __tusb_musb_readb;
 		musb_writeb = __tusb_musb_writeb;
@@ -1984,7 +1959,7 @@ musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl,
 	}
 
 #ifndef CONFIG_MUSB_PIO_ONLY
-	if (use_dma && dev->dma_mask) {
+	if (dev->dma_mask) {
 		struct dma_controller	*c;
 
 		if (!musb->ops->dma_controller_create) {
@@ -2123,6 +2098,9 @@ musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl,
 	/* Init giveback workqueue */
 	INIT_WORK(&musb->gb_work, musb_gb_work);
 
+	/* setup otg_timer */
+	setup_timer(&musb->otg_timer, musb_otg_timer_func,
+						(unsigned long) musb);
 	return 0;
 
 fail6:
@@ -2159,11 +2137,6 @@ fail0:
 /* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
  * bridge to a platform device; this driver then suffices.
  */
-
-#ifndef CONFIG_MUSB_PIO_ONLY
-static u64	*orig_dma_mask;
-#endif
-
 static int __devinit musb_probe(struct platform_device *pdev)
 {
 	struct device	*dev = &pdev->dev;
@@ -2196,10 +2169,6 @@ static int __devinit musb_probe(struct platform_device *pdev)
 		return -ENOMEM;
 	}
 
-#ifndef CONFIG_MUSB_PIO_ONLY
-	/* clobbered by use_dma=n */
-	orig_dma_mask = dev->dma_mask;
-#endif
 	status = musb_init_controller(dev, irq, base, iomem->start);
 	if (status < 0)
 		iounmap(base);
@@ -2229,7 +2198,7 @@ static int __exit musb_remove(struct platform_device *pdev)
 	iounmap(ctrl_base);
 	device_init_wakeup(&pdev->dev, 0);
 #ifndef CONFIG_MUSB_PIO_ONLY
-	pdev->dev.dma_mask = orig_dma_mask;
+	pdev->dev.dma_mask = musb->orig_dma_mask;
 #endif
 	return 0;
 }
@@ -2441,7 +2410,6 @@ static int musb_runtime_suspend(struct device *dev)
 static int musb_runtime_resume(struct device *dev)
 {
 	struct musb	*musb = dev_to_musb(dev);
-	static int	first = 1;
 
 	/*
 	 * When pm_runtime_get_sync called for the first time in driver
@@ -2452,9 +2420,11 @@ static int musb_runtime_resume(struct device *dev)
 	 * Also context restore without save does not make
 	 * any sense
 	 */
-	if (!first)
+	if (musb->first)
+		musb->first = 0;
+	else
 		musb_restore_context(musb);
-	first = 0;
+
 
 	return 0;
 }
diff --git a/drivers/usb/musb/musb_core.h b/drivers/usb/musb/musb_core.h
index 919d43b..a34886f 100644
--- a/drivers/usb/musb/musb_core.h
+++ b/drivers/usb/musb/musb_core.h
@@ -464,6 +464,13 @@ struct musb {
 	u8			id;
 	struct	timer_list	otg_workaround;
 	unsigned long		last_timer;
+	int			first;
+	int			old_state;
+	struct	timer_list	otg_timer;
+#ifndef CONFIG_MUSB_PIO_ONLY
+	u64			*orig_dma_mask;
+#endif
+	short			fifo_mode;
 };
 
 static inline struct musb *gadget_to_musb(struct usb_gadget *g)
-- 
1.7.2.5

