USB: ks_bridge: Fix PM usage counter mismatch

The runtime PM is disabled for a device during suspend.  It is
enabled again just before resuming  the device.  The resume method
of USB interface drivers are called while resuming a USB device.  As
USB device is the parent of an interface, the interface resume is
blocked till USB device resume is completed.  If interface driver
call any runtime PM API before interface runtime PM is enabled, it
fails with -EACCESS error.

The USB runtime wrapper usb_autopm_get_interface() decrements PM usage
counter when get_sync() is failed.  The current code is decrementing
the PM usage counter even when an error code is returned from
usb_autopm_get_interface().  Due to this double decrementing, the KS
bridge interface PM usage counter is becoming negative after several
suspend and resume cycles.  Once the PM usage counter becomes negative,
the interface runtime PM state will never become active.  That means,
USB device suspend can happen even when a Tx URB is pending from ks
bridge driver.

The fix is trivial.  Don't decrement PM usage counter when error code
is returned from usb_autopm_get_interface().

(cherry picked from commit 361d9af92470ebeeb0a42f73e6eb457ab279b282)

Conflicts:

	drivers/usb/misc/ks_bridge.c

CRs-Fixed: 437214
Change-Id: I96c9e1797c137cd158c424ebf6342f607cee6d78
Signed-off-by: Pavankumar Kondeti <pkondeti@codeaurora.org>
usb: ks_bridge: Fix memory leaks in the driver.

This change addresses two memory leaks:-
1) misc device is not getting deregistered during device
disconnect. Hence call misc_deregister() in disconnect.

2) Driver returns without freeing previous instances of
the driver data structure if memory allocation fails. Hence
free up memory allocated for ks_bridge structure.

Signed-off-by: Hemant Kumar <hemantk@codeaurora.org>
(cherry picked from commit da86bd2faa9cfbf065c7a1f0adba124d7e8335e0)

Conflicts:

	drivers/usb/misc/ks_bridge.c

Change-Id: I5e1c357fa29b455bae5f3dc71b4d65d5cc216bb3
Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
usb: ks_bridge: Add support for multiple instance of driver

ks bridver needs to get loaded for two mdm9x15 device enumeration
to support flashless boot and efs sync.

Change-Id: Iabeab32f05592032b13ec6ab28da67093cc23531
Signed-off-by: Hemant Kumar <hemantk@codeaurora.org>
Signed-off-by: Shashank Mittal <mittals@codeaurora.org>

Conflicts:
	drivers/usb/misc/ks_bridge.c
usb: ks_bridge: Synchronize disconnect with ongoing IO

Following scenarios possible for interface disconnect racing
with ongoing IO:-

1) Interface disconnect setting interface pointer to NULL and
rx work is executing on other core dereferencing interface
pointer, causing kernel panic. Cancel a work and wait for it
to finish during disconnect.

2) Interface disconnect setting interface pointer to NULL and
tx completion running on other core dereferencing interface
pointer while calling usb_autopm_put_interface_async() API.
Check for USB_DEV_CONNECTED flag(cleared at the time of
disconnect) before dereferencing the interface pointer.

3) Interface unbound and driver is accessing already freed
interface pointer in tx completion or usb device pointer to
re-submit rx urb in rx completion. Add rx and tx urb pending
counters and wait for them to become zero (or timeout) during
disconnect.

CRs-Fixed: 448142
Change-Id: I50341173b94200bcfa60715b4b26b2117fc37c2c
Signed-off-by: Hemant Kumar <hemantk@codeaurora.org>
Signed-off-by: Pavankumar Kondeti <pkondeti@codeaurora.org>
usb: ks_bridge: Free up stale buffers in probe

Driver can add data buffer to the link list of rx/tx
buffers after emptying the list during disconnect.
These stale buffers can confuse efs sync server when
mdm device connects back. Hence free up the rx/tx
buffers again during probe. Avoid further addition of
tx buffers after disconnect by checking device
connection status in write API. Also, move
misc_deregister() after cancelling the work in order
to remove device node early during disconnect to avoid
any write call further.

CRs-Fixed: 448142
Change-Id: Ifff58d2adc113257809eb80b7f91c1a0f4a0adeb
Signed-off-by: Hemant Kumar <hemantk@codeaurora.org>
Signed-off-by: Pavankumar Kondeti <pkondeti@codeaurora.org>

Conflicts:
	drivers/usb/misc/ks_bridge.c
USB: ks_bridge: Prevent freeing buffers while copy is in progress

The completed read buffers are added to to_ks_list in Rx URB
completion handler.  The buffer is deleted from this list by
reader process after copying to user space buffer.  If disconnect
happens during this copy, the buffer is freed from disconnect
context.  Hence detach the buffer from to_ks_list before copying
to user space buffer.

CRs-Fixed: 448142
Change-Id: Ia4484b2709065162b0d32782a30e2b2fd36f5fda
Signed-off-by: Pavankumar Kondeti <pkondeti@codeaurora.org>
USB: ks_bridge: Abort suspend when data is received during unlink

All the submitted URBs are unlinked during suspend.  Abort suspend
if data is received during unlink.  This avoids unnecessary suspend
and resume of remaining interfaces.

CRs-Fixed: 449097
Change-Id: Ia4009775ec29df0222b475581649c3a9f4f1899f
Signed-off-by: Pavankumar Kondeti <pkondeti@codeaurora.org>
USB: ks_bridge: Enable autosuspend only for remote wakeup capable devices

USB core marks a device wakeup capable if it supports remote wakeup.
Check this wakeup capable flag before enabling autosuspend.

CRs-Fixed: 449100
Change-Id: I055bf5801859e4d83782916c71e0245c455becbf
Signed-off-by: Pavankumar Kondeti <pkondeti@codeaurora.org>

Conflicts:
	drivers/usb/misc/ks_bridge.c

Conflicts:
	drivers/usb/misc/ks_bridge.c
mmc: Keep track of current hard partition

eMMC may have several hard partitions such as BOOT and RPMB in addition
to the main USER partition. On resume(if card is reinitialized), the
current hard partition is always switched to the default USER partition.
But this switch to default partition is not propagated to the MMC block
driver, which may have set the hard partition to any partition(BOOT/RPMB)
other than USER before suspend. After resume, the MMC block driver still
assumes it is accessing the previously set partition(BOOT/RPMB), and
instead overwrites the USER partition(the current selected partition on
the eMMC device). To prevent this, make MMC block driver aware of the
partition switches done as part of card reinitialization.

CRs-Fixed: 439313
Change-Id: I3e959101a1c56c1e6631da3d660f4b914e100503
Signed-off-by: Oluwafemi Adeyemi <aadeyemi@codeaurora.org>
Signed-off-by: Venkat Gopalakrishnan <venkatg@codeaurora.org>