usb: musb: fix some runtime_pm issues

When runtime_pm was originally added, it was done in rather confusing
way: omap2430_musb_init() (called from musb_init_controller) would do
runtime_pm_get_sync() and musb_init_controller() itself would do
runtime_pm_put to balance it out. This is not only confusing but also
wrong if non-omap2430 glue layer is used.

This confusion resulted in commit 772aed45b604 "usb: musb: fix
pm_runtime mismatch", that removed runtime_pm_put() from
musb_init_controller as that looked unbalanced, and also happened to
fix unrelated isp1704_charger crash. However this broke runtime PM
functionality (musb is now always powered, even without gadget active).

Avoid these confusing runtime pm dependences by making
musb_init_controller() and omap2430_musb_init() do their own runtime
get/put pairs; also cover error paths. Remove unneeded runtime_pm_put
in omap2430_remove too. isp1704_charger crash that motivated
772aed45b604 will be fixed by following patch.

Cc: Felipe Contreras <felipe.contreras@gmail.com>
Signed-off-by: Grazvydas Ignotas <notasas@gmail.com>
Signed-off-by: Felipe Balbi <balbi@ti.com>

Conflicts:
	drivers/usb/musb/omap2430.c
usb: musb: omap2430: don't loop indefinitely in interrupt.

When called during resume_irqs, omap2430_musb_set_vbus() is run with
interrupts disabled,  In that case 'jiffies' never changes so the loop
can loop forever.

So impose a maximum loop count and add an 'mdelay' to ensure we wait
a reasonable amount of time for bit to be cleared.

This fixes a hang on resume.

[Ruslan]: cherry-picked from upstream
http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;
a=commit;h=12a19b5f836c1a1b838e668f9aa804d017d8c58b

Change-Id: I02aab4597014361a9528d3f90a8f3144057ef51a
Signed-of-by: NeilBrown <neilb@suse.de>
Signed-off-by: Felipe Balbi <balbi@ti.com>
Signed-off-by: Ruslan Bilovol <ruslan.bilovol@ti.com>

Conflicts:
	drivers/usb/musb/omap2430.c
usb: musb: drop __deprecated flag

Looks like we cannot live without that double_buffer_not_ok
flag due to many HW bugs this MUSB core has.

So, let's drop the __deprecated flag to avoid annoying
compile warnings.

Signed-off-by: Felipe Balbi <balbi@ti.com>
usb: musb: drop superfluous pm_runtime calls around musb_shutdown

Since commit 4f9edd2d7e8d "usb: musb: Fix the crash issue during reboot"
musb_shutdown() does pm_runtime_get_sync/pm_runtime_put by itself, so
this no longer needs to be done by the caller. Also, musb_exit_debugfs()
doesn't access the device, so just drop those runtime_pm calls.

Signed-off-by: Grazvydas Ignotas <notasas@gmail.com>
Reviewed-by: Shubhrajyoti D <shubhrajyoti@ti.com>
Signed-off-by: Felipe Balbi <balbi@ti.com>
USB: irq: Remove IRQF_DISABLED

This flag is a NOOP and can be removed now.

Signed-off-by: Yong Zhang <yong.zhang0@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
USB: Remove the SAW_IRQ hcd flag

The HCD_FLAG_SAW_IRQ flag was introduced in order to catch IRQ routing
errors: If an URB was unlinked and the host controller hadn't gotten
any IRQs, it seemed likely that the IRQs were directed to the wrong
vector.

This warning hasn't come up in many years, as far as I know; interrupt
routing now seems to be well under control.  Therefore there's no
reason to keep the flag around any more.  This patch (as1495) finally
removes it.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
usb: musb: fix reset issue with full speed device

TXMAXP register is not getting programmed correctly for a full speed device
as can_bulk_split() have been removed by
"0662481: usb: musb: disable double buffering when it's broken" patch.

Adding back the case for can_bulk_split() to fix the reset message seen with
a full speed stick.

Signed-off-by: Ajay Kumar Gupta <ajay.gupta@ti.com>
Signed-off-by: Felipe Balbi <balbi@ti.com>
usb: musb: fix pm_runtime mismatch

In musb_init_controller() there's a pm_runtime_put(), but there's no
pm_runtime_get(), which creates a mismatch that causes the driver to
sleep when it shouldn't.

This was introduced in 7acc619[1], but it wasn't triggered in my setup
until 18a2689[2] was merged to Linus' branch at point df0914[3]. IOW;
when PM is working as it was supposed to.

However, it seems most of the time this is used in a way that keeps the
counter above 0, so nobody noticed. Also, it seems to depend on the
configuration used in versions before 3.1, but not later (or in it).

I found the problem by loading isp1704_charger before any usb gadgets:
http://article.gmane.org/gmane.linux.kernel/1226122

All versions after 2.6.39 are affected.

[1] usb: musb: Idle path retention and offmode support for OMAP3
[2] OMAP2+: musb: hwmod adaptation for musb registration
[3] Merge branch 'omap-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tmlind/linux-omap-2.6

Cc: stable@vger.kernel.org
Cc: Hema HK <hemahk@ti.com>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
Signed-off-by: Felipe Balbi <balbi@ti.com>

Conflicts:
	drivers/usb/musb/musb_core.c
usb: musb: enable platform HW before monitoring VBUS line state

If the platform HW is not enabled before monitoring VBUS line
state in musb_start(), musb core cannot see current VBUS line
state at least on OMAP4 where MUSB DEVCTL register is driving
through OMAP4 USBOTGHS software control register. This was
resulting in a wrong HOST session starting on each cold boot.

Change-Id: I8aacac0774e124c58c30dc5e40def70818fc0cf1
Signed-off-by: Ruslan Bilovol <ruslan.bilovol@ti.com>

Conflicts:
	drivers/usb/musb/musb_core.c
usb: musb: Fix memory corruption

rx_count, the number of bytes in the rx fifo, may be larger than the
size of the transfer buffer. Make sure we don't overflow the transfer
buffer in that case.

Bug: 6497399

[Ruslan] Ported from k3.0 commit-id 253a265

Change-Id: I5bcf6dd543f9da4ee22944aa6c41673f2cca4c1f
Signed-off-by: Benoit Goby <benoit@android.com>
Signed-off-by: Ruslan Bilovol <ruslan.bilovol@ti.com>
usb: musb: Fix crash when dma is nuked while irq handler is pending

It appears possible for the dma to get nuked while the irq handler
is pending/about to run.  The dma is cancelled by nuke() but the
irq handler will still run and wind up dereferencing NULL, so add
a check.

Crash log:

[ 7629.098815] Unable to handle kernel NULL pointer dereference at virtual address 00000004
[ 7629.099273] pgd = c0004000
[ 7629.099456] [00000004] *pgd=00000000
[ 7629.099761] Internal error: Oops: 17 [#1] PREEMPT SMP
[ 7629.100067] Modules linked in:
[ 7629.100402] CPU: 0    Not tainted  (3.0.8-gcb25bf2 #1)
[ 7629.100585] PC is at txstate+0x74/0x1ec
[ 7629.100891] LR is at musb_g_tx+0x138/0x1d8
[ 7629.101074] pc : [<c033f1cc>]    lr : [<c033fc9c>]    psr: 20000193
[ 7629.101074] sp : c06efd60  ip : c06efd98  fp : c06efd94
[ 7629.101531] r10: 00000018  r9 : 00000000  r8 : 00000200
[ 7629.101684] r7 : 00000000  r6 : fc0ab110  r5 : c7852448  r4 : c7125c00
[ 7629.101867] r3 : 00000000  r2 : 00000000  r1 : 00000002  r0 : c78520e4
...
[ 7629.228668] [<c033f158>] (txstate+0x0/0x1ec) from [<c033fc9c>] (musb_g_tx+0x138/0x1d8)
[ 7629.229003] [<c033fb64>] (musb_g_tx+0x0/0x1d8) from [<c033d35c>] (musb_interrupt+0x104/0x8cc)
[ 7629.229187] [<c033d258>] (musb_interrupt+0x0/0x8cc) from [<c033db7c>] (generic_interrupt+0x58/0x8c)
[ 7629.229522] [<c033db24>] (generic_interrupt+0x0/0x8c) from [<c00da008>] (handle_irq_event_percpu+0x54/0x188)
[ 7629.229827]  r6:00000000 r5:c06f3f4c r4:c78f49c0 r3:c033db24

[Ruslan] Cheery-picked from k3.0 commit-id 0cf6404

Change-Id: I305c80a246f5161f2d3d20839c5603376d5b746c
Signed-off-by: Mike J. Chen <mjchen@google.com>
Signed-off-by: Ruslan Bilovol <ruslan.bilovol@ti.com>
restore omap2430_musb_set_vbus