1
0
Files
Greg Kroah-Hartman e51ef120e8 Merge 4.9.233 into android-4.9-q
Changes in 4.9.233
	xfs: catch inode allocation state mismatch corruption
	xfs: validate cached inodes are free when allocated
	xfs: don't call xfs_da_shrink_inode with NULL bp
	net: phy: mdio-bcm-unimac: fix potential NULL dereference in unimac_mdio_probe()
	crypto: ccp - Release all allocated memory if sha type is invalid
	media: rc: prevent memory leak in cx23888_ir_probe
	ath9k_htc: release allocated buffer if timed out
	ath9k: release allocated buffer if timed out
	PCI/ASPM: Disable ASPM on ASMedia ASM1083/1085 PCIe-to-PCI bridge
	ARM: 8986/1: hw_breakpoint: Don't invoke overflow handler on uaccess watchpoints
	drm/amdgpu: Prevent kernel-infoleak in amdgpu_info_ioctl()
	drm: hold gem reference until object is no longer accessed
	f2fs: check memory boundary by insane namelen
	f2fs: check if file namelen exceeds max value
	9p/trans_fd: abort p9_read_work if req status changed
	9p/trans_fd: Fix concurrency del of req_list in p9_fd_cancelled/p9_read_work
	x86/build/lto: Fix truncated .bss with -fdata-sections
	x86, vmlinux.lds: Page-align end of ..page_aligned sections
	fbdev: Detect integer underflow at "struct fbcon_ops"->clear_margins.
	rds: Prevent kernel-infoleak in rds_notify_queue_get()
	xfs: fix missed wakeup on l_flush_wait
	uapi: includes linux/types.h before exporting files
	install several missing uapi headers
	net/x25: Fix x25_neigh refcnt leak when x25 disconnect
	net/x25: Fix null-ptr-deref in x25_disconnect
	sh: Fix validation of system call number
	net: lan78xx: add missing endpoint sanity check
	net: lan78xx: fix transfer-buffer memory leak
	mlx4: disable device on shutdown
	mlxsw: core: Increase scope of RCU read-side critical section
	mlxsw: core: Free EMAD transactions using kfree_rcu()
	ibmvnic: Fix IRQ mapping disposal in error path
	mac80211: mesh: Free ie data when leaving mesh
	mac80211: mesh: Free pending skb when destroying a mpath
	arm64: csum: Fix handling of bad packets
	usb: hso: Fix debug compile warning on sparc32
	qed: Disable "MFW indication via attention" SPAM every 5 minutes
	nfc: s3fwrn5: add missing release on skb in s3fwrn5_recv_frame
	parisc: add support for cmpxchg on u8 pointers
	net: ethernet: ravb: exit if re-initialization fails in tx timeout
	Revert "i2c: cadence: Fix the hold bit setting"
	xen-netfront: fix potential deadlock in xennet_remove()
	KVM: LAPIC: Prevent setting the tscdeadline timer if the lapic is hw disabled
	x86/i8259: Use printk_deferred() to prevent deadlock
	random32: update the net random state on interrupt and activity
	ARM: percpu.h: fix build error
	random: fix circular include dependency on arm64 after addition of percpu.h
	random32: remove net_rand_state from the latent entropy gcc plugin
	random32: move the pseudo-random 32-bit definitions to prandom.h
	ext4: fix direct I/O read error
	USB: serial: qcserial: add EM7305 QDL product ID
	net/mlx5e: Don't support phys switch id if not in switchdev mode
	ALSA: seq: oss: Serialize ioctls
	Bluetooth: Fix slab-out-of-bounds read in hci_extended_inquiry_result_evt()
	Bluetooth: Prevent out-of-bounds read in hci_inquiry_result_evt()
	Bluetooth: Prevent out-of-bounds read in hci_inquiry_result_with_rssi_evt()
	omapfb: dss: Fix max fclk divider for omap36xx
	vgacon: Fix for missing check in scrollback handling
	mtd: properly check all write ioctls for permissions
	leds: wm831x-status: fix use-after-free on unbind
	leds: da903x: fix use-after-free on unbind
	leds: lm3533: fix use-after-free on unbind
	leds: 88pm860x: fix use-after-free on unbind
	net/9p: validate fds in p9_fd_open
	drm/nouveau/fbcon: fix module unload when fbcon init has failed for some reason
	cfg80211: check vendor command doit pointer before use
	igb: reinit_locked() should be called with rtnl_lock
	atm: fix atm_dev refcnt leaks in atmtcp_remove_persistent
	tools lib traceevent: Fix memory leak in process_dynamic_array_len
	xattr: break delegations in {set,remove}xattr
	binder: Prevent context manager from incrementing ref 0
	ipv4: Silence suspicious RCU usage warning
	ipv6: fix memory leaks on IPV6_ADDRFORM path
	vxlan: Ensure FDB dump is performed under RCU
	net: lan78xx: replace bogus endpoint lookup
	Revert "vxlan: fix tos value before xmit"
	usb: hso: check for return value in hso_serial_common_create()
	Smack: fix use-after-free in smk_write_relabel_self()
	tracepoint: Mark __tracepoint_string's __used
	gpio: fix oops resulting from calling of_get_named_gpio(NULL, ...)
	cgroup: add missing skcd->no_refcnt check in cgroup_sk_clone()
	EDAC: Fix reference count leaks
	arm64: dts: qcom: msm8916: Replace invalid bias-pull-none property
	arm64: dts: exynos: Fix silent hang after boot on Espresso
	m68k: mac: Don't send IOP message until channel is idle
	m68k: mac: Fix IOP status/control register writes
	platform/x86: intel-hid: Fix return value check in check_acpi_dev()
	platform/x86: intel-vbtn: Fix return value check in check_acpi_dev()
	ARM: at91: pm: add missing put_device() call in at91_pm_sram_init()
	ARM: socfpga: PM: add missing put_device() call in socfpga_setup_ocram_self_refresh()
	drm/tilcdc: fix leak & null ref in panel_connector_get_modes
	Bluetooth: add a mutex lock to avoid UAF in do_enale_set
	fs/btrfs: Add cond_resched() for try_release_extent_mapping() stalls
	drm/radeon: Fix reference count leaks caused by pm_runtime_get_sync
	video: fbdev: neofb: fix memory leak in neo_scan_monitor()
	md-cluster: fix wild pointer of unlock_all_bitmaps()
	drm/nouveau: fix multiple instances of reference count leaks
	drm/debugfs: fix plain echo to connector "force" attribute
	mm/mmap.c: Add cond_resched() for exit_mmap() CPU stalls
	brcmfmac: To fix Bss Info flag definition Bug
	iwlegacy: Check the return value of pcie_capability_read_*()
	usb: gadget: net2280: fix memory leak on probe error handling paths
	bdc: Fix bug causing crash after multiple disconnects
	dyndbg: fix a BUG_ON in ddebug_describe_flags
	bcache: fix super block seq numbers comparision in register_cache_set()
	ACPICA: Do not increment operation_region reference counts for field units
	agp/intel: Fix a memory leak on module initialisation failure
	video: fbdev: sm712fb: fix an issue about iounmap for a wrong address
	console: newport_con: fix an issue about leak related system resources
	video: pxafb: Fix the function used to balance a 'dma_alloc_coherent()' call
	iio: improve IIO_CONCENTRATION channel type description
	leds: lm355x: avoid enum conversion warning
	media: omap3isp: Add missed v4l2_ctrl_handler_free() for preview_init_entities()
	scsi: cumana_2: Fix different dev_id between request_irq() and free_irq()
	drm/mipi: use dcs write for mipi_dsi_dcs_set_tear_scanline
	cxl: Fix kobject memleak
	drm/radeon: fix array out-of-bounds read and write issues
	scsi: powertec: Fix different dev_id between request_irq() and free_irq()
	scsi: eesox: Fix different dev_id between request_irq() and free_irq()
	media: firewire: Using uninitialized values in node_probe()
	media: exynos4-is: Add missed check for pinctrl_lookup_state()
	xfs: fix reflink quota reservation accounting error
	PCI: Fix pci_cfg_wait queue locking problem
	leds: core: Flush scheduled work for system suspend
	drm: panel: simple: Fix bpc for LG LB070WV8 panel
	scsi: scsi_debug: Add check for sdebug_max_queue during module init
	mwifiex: Prevent memory corruption handling keys
	powerpc/vdso: Fix vdso cpu truncation
	staging: rtl8192u: fix a dubious looking mask before a shift
	PCI/ASPM: Add missing newline in sysfs 'policy'
	drm/imx: tve: fix regulator_disable error path
	USB: serial: iuu_phoenix: fix led-activity helpers
	usb: dwc2: Fix error path in gadget registration
	scsi: mesh: Fix panic after host or bus reset
	Smack: fix another vsscanf out of bounds
	Smack: prevent underflow in smk_set_cipso()
	power: supply: check if calc_soc succeeded in pm860x_init_battery
	selftests/powerpc: Fix CPU affinity for child process
	selftests/powerpc: Fix online CPU selection
	s390/qeth: don't process empty bridge port events
	wl1251: fix always return 0 error
	net: spider_net: Fix the size used in a 'dma_free_coherent()' call
	fsl/fman: use 32-bit unsigned integer
	fsl/fman: fix dereference null return value
	fsl/fman: fix unreachable code
	fsl/fman: check dereferencing null pointer
	fsl/fman: fix eth hash table allocation
	dlm: Fix kobject memleak
	pinctrl-single: fix pcs_parse_pinconf() return value
	drivers/net/wan/lapbether: Added needed_headroom and a skb->len check
	net/nfc/rawsock.c: add CAP_NET_RAW check.
	net: Set fput_needed iff FDPUT_FPUT is set
	USB: serial: cp210x: re-enable auto-RTS on open
	USB: serial: cp210x: enable usb generic throttle/unthrottle
	ALSA: usb-audio: Creative USB X-Fi Pro SB1095 volume knob support
	ALSA: usb-audio: fix overeager device match for MacroSilicon MS2109
	ALSA: usb-audio: add quirk for Pioneer DDJ-RB
	crypto: qat - fix double free in qat_uclo_create_batch_init_list
	crypto: ccp - Fix use of merged scatterlists
	fs/minix: check return value of sb_getblk()
	fs/minix: don't allow getting deleted inodes
	fs/minix: reject too-large maximum file size
	ALSA: usb-audio: work around streaming quirk for MacroSilicon MS2109
	9p: Fix memory leak in v9fs_mount
	parisc: mask out enable and reserved bits from sba imask
	ARM: 8992/1: Fix unwind_frame for clang-built kernels
	xen/balloon: fix accounting in alloc_xenballooned_pages error path
	xen/balloon: make the balloon wait interruptible
	smb3: warn on confusing error scenario with sec=krb5
	PCI: hotplug: ACPI: Fix context refcounting in acpiphp_grab_context()
	btrfs: don't allocate anonymous block device for user invisible roots
	btrfs: only search for left_info if there is no right_info in try_merge_free_space
	btrfs: fix memory leaks after failure to lookup checksums during inode logging
	iio: dac: ad5592r: fix unbalanced mutex unlocks in ad5592r_read_raw()
	xtensa: fix xtensa_pmu_setup prototype
	powerpc: Fix circular dependency between percpu.h and mmu.h
	net: ethernet: stmmac: Disable hardware multicast filter
	net: stmmac: dwmac1000: provide multicast filter fallback
	net/compat: Add missing sock updates for SCM_RIGHTS
	md/raid5: Fix Force reconstruct-write io stuck in degraded raid5
	bcache: allocate meta data pages as compound pages
	mac80211: fix misplaced while instead of if
	MIPS: CPU#0 is not hotpluggable
	ext2: fix missing percpu_counter_inc
	ocfs2: change slot number type s16 to u16
	ftrace: Setup correct FTRACE_FL_REGS flags for module
	kprobes: Fix NULL pointer dereference at kprobe_ftrace_handler
	watchdog: f71808e_wdt: indicate WDIOF_CARDRESET support in watchdog_info.options
	watchdog: f71808e_wdt: remove use of wrong watchdog_info option
	watchdog: f71808e_wdt: clear watchdog timeout occurred flag
	pseries: Fix 64 bit logical memory block panic
	mfd: arizona: Ensure 32k clock is put on driver unbind and error
	USB: serial: ftdi_sio: make process-packet buffer unsigned
	USB: serial: ftdi_sio: clean up receive processing
	gpu: ipu-v3: image-convert: Combine rotate/no-rotate irq handlers
	iommu/omap: Check for failure of a call to omap_iommu_dump_ctx
	iommu/vt-d: Enforce PASID devTLB field mask
	i2c: rcar: slave: only send STOP event when we have been addressed
	clk: clk-atlas6: fix return value check in atlas6_clk_init()
	pwm: bcm-iproc: handle clk_get_rate() return
	Input: sentelic - fix error return when fsp_reg_write fails
	drm/vmwgfx: Fix two list_for_each loop exit tests
	net: qcom/emac: add missed clk_disable_unprepare in error path of emac_clks_phase1_init
	nfs: Fix getxattr kernel panic and memory overflow
	fs/ufs: avoid potential u32 multiplication overflow
	mfd: dln2: Run event handler loop under spinlock
	ALSA: echoaudio: Fix potential Oops in snd_echo_resume()
	sh: landisk: Add missing initialization of sh_io_port_base
	khugepaged: retract_page_tables() remember to test exit
	mm: Avoid calling build_all_zonelists_init under hotplug context
	Linux 4.9.233

Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
Change-Id: Ied62cb0768f5bd8e989d75e7c2ccf6f1e6f2efd4
2020-09-09 21:39:13 +03:00

851 lines
19 KiB
C

/*
* Driver for the Diolan DLN-2 USB adapter
*
* Copyright (c) 2014 Intel Corporation
*
* Derived from:
* i2c-diolan-u2c.c
* Copyright (c) 2010-2011 Ericsson AB
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, version 2.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/usb.h>
#include <linux/i2c.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/mfd/core.h>
#include <linux/mfd/dln2.h>
#include <linux/rculist.h>
struct dln2_header {
__le16 size;
__le16 id;
__le16 echo;
__le16 handle;
};
struct dln2_response {
struct dln2_header hdr;
__le16 result;
};
#define DLN2_GENERIC_MODULE_ID 0x00
#define DLN2_GENERIC_CMD(cmd) DLN2_CMD(cmd, DLN2_GENERIC_MODULE_ID)
#define CMD_GET_DEVICE_VER DLN2_GENERIC_CMD(0x30)
#define CMD_GET_DEVICE_SN DLN2_GENERIC_CMD(0x31)
#define DLN2_HW_ID 0x200
#define DLN2_USB_TIMEOUT 200 /* in ms */
#define DLN2_MAX_RX_SLOTS 16
#define DLN2_MAX_URBS 16
#define DLN2_RX_BUF_SIZE 512
enum dln2_handle {
DLN2_HANDLE_EVENT = 0, /* don't change, hardware defined */
DLN2_HANDLE_CTRL,
DLN2_HANDLE_GPIO,
DLN2_HANDLE_I2C,
DLN2_HANDLE_SPI,
DLN2_HANDLES
};
/*
* Receive context used between the receive demultiplexer and the transfer
* routine. While sending a request the transfer routine will look for a free
* receive context and use it to wait for a response and to receive the URB and
* thus the response data.
*/
struct dln2_rx_context {
/* completion used to wait for a response */
struct completion done;
/* if non-NULL the URB contains the response */
struct urb *urb;
/* if true then this context is used to wait for a response */
bool in_use;
};
/*
* Receive contexts for a particular DLN2 module (i2c, gpio, etc.). We use the
* handle header field to identify the module in dln2_dev.mod_rx_slots and then
* the echo header field to index the slots field and find the receive context
* for a particular request.
*/
struct dln2_mod_rx_slots {
/* RX slots bitmap */
DECLARE_BITMAP(bmap, DLN2_MAX_RX_SLOTS);
/* used to wait for a free RX slot */
wait_queue_head_t wq;
/* used to wait for an RX operation to complete */
struct dln2_rx_context slots[DLN2_MAX_RX_SLOTS];
/* avoid races between alloc/free_rx_slot and dln2_rx_transfer */
spinlock_t lock;
};
enum dln2_endpoint {
DLN2_EP_OUT = 0,
DLN2_EP_IN = 1,
};
struct dln2_dev {
struct usb_device *usb_dev;
struct usb_interface *interface;
u8 ep_in;
u8 ep_out;
struct urb *rx_urb[DLN2_MAX_URBS];
void *rx_buf[DLN2_MAX_URBS];
struct dln2_mod_rx_slots mod_rx_slots[DLN2_HANDLES];
struct list_head event_cb_list;
spinlock_t event_cb_lock;
bool disconnect;
int active_transfers;
wait_queue_head_t disconnect_wq;
spinlock_t disconnect_lock;
};
struct dln2_event_cb_entry {
struct list_head list;
u16 id;
struct platform_device *pdev;
dln2_event_cb_t callback;
};
int dln2_register_event_cb(struct platform_device *pdev, u16 id,
dln2_event_cb_t event_cb)
{
struct dln2_dev *dln2 = dev_get_drvdata(pdev->dev.parent);
struct dln2_event_cb_entry *i, *entry;
unsigned long flags;
int ret = 0;
entry = kzalloc(sizeof(*entry), GFP_KERNEL);
if (!entry)
return -ENOMEM;
entry->id = id;
entry->callback = event_cb;
entry->pdev = pdev;
spin_lock_irqsave(&dln2->event_cb_lock, flags);
list_for_each_entry(i, &dln2->event_cb_list, list) {
if (i->id == id) {
ret = -EBUSY;
break;
}
}
if (!ret)
list_add_rcu(&entry->list, &dln2->event_cb_list);
spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
if (ret)
kfree(entry);
return ret;
}
EXPORT_SYMBOL(dln2_register_event_cb);
void dln2_unregister_event_cb(struct platform_device *pdev, u16 id)
{
struct dln2_dev *dln2 = dev_get_drvdata(pdev->dev.parent);
struct dln2_event_cb_entry *i;
unsigned long flags;
bool found = false;
spin_lock_irqsave(&dln2->event_cb_lock, flags);
list_for_each_entry(i, &dln2->event_cb_list, list) {
if (i->id == id) {
list_del_rcu(&i->list);
found = true;
break;
}
}
spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
if (found) {
synchronize_rcu();
kfree(i);
}
}
EXPORT_SYMBOL(dln2_unregister_event_cb);
/*
* Returns true if a valid transfer slot is found. In this case the URB must not
* be resubmitted immediately in dln2_rx as we need the data when dln2_transfer
* is woke up. It will be resubmitted there.
*/
static bool dln2_transfer_complete(struct dln2_dev *dln2, struct urb *urb,
u16 handle, u16 rx_slot)
{
struct device *dev = &dln2->interface->dev;
struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[handle];
struct dln2_rx_context *rxc;
bool valid_slot = false;
if (rx_slot >= DLN2_MAX_RX_SLOTS)
goto out;
rxc = &rxs->slots[rx_slot];
/*
* No need to disable interrupts as this lock is not taken in interrupt
* context elsewhere in this driver. This function (or its callers) are
* also not exported to other modules.
*/
spin_lock(&rxs->lock);
if (rxc->in_use && !rxc->urb) {
rxc->urb = urb;
complete(&rxc->done);
valid_slot = true;
}
spin_unlock(&rxs->lock);
out:
if (!valid_slot)
dev_warn(dev, "bad/late response %d/%d\n", handle, rx_slot);
return valid_slot;
}
static void dln2_run_event_callbacks(struct dln2_dev *dln2, u16 id, u16 echo,
void *data, int len)
{
struct dln2_event_cb_entry *i;
rcu_read_lock();
list_for_each_entry_rcu(i, &dln2->event_cb_list, list) {
if (i->id == id) {
i->callback(i->pdev, echo, data, len);
break;
}
}
rcu_read_unlock();
}
static void dln2_rx(struct urb *urb)
{
struct dln2_dev *dln2 = urb->context;
struct dln2_header *hdr = urb->transfer_buffer;
struct device *dev = &dln2->interface->dev;
u16 id, echo, handle, size;
u8 *data;
int len;
int err;
switch (urb->status) {
case 0:
/* success */
break;
case -ECONNRESET:
case -ENOENT:
case -ESHUTDOWN:
case -EPIPE:
/* this urb is terminated, clean up */
dev_dbg(dev, "urb shutting down with status %d\n", urb->status);
return;
default:
dev_dbg(dev, "nonzero urb status received %d\n", urb->status);
goto out;
}
if (urb->actual_length < sizeof(struct dln2_header)) {
dev_err(dev, "short response: %d\n", urb->actual_length);
goto out;
}
handle = le16_to_cpu(hdr->handle);
id = le16_to_cpu(hdr->id);
echo = le16_to_cpu(hdr->echo);
size = le16_to_cpu(hdr->size);
if (size != urb->actual_length) {
dev_err(dev, "size mismatch: handle %x cmd %x echo %x size %d actual %d\n",
handle, id, echo, size, urb->actual_length);
goto out;
}
if (handle >= DLN2_HANDLES) {
dev_warn(dev, "invalid handle %d\n", handle);
goto out;
}
data = urb->transfer_buffer + sizeof(struct dln2_header);
len = urb->actual_length - sizeof(struct dln2_header);
if (handle == DLN2_HANDLE_EVENT) {
unsigned long flags;
spin_lock_irqsave(&dln2->event_cb_lock, flags);
dln2_run_event_callbacks(dln2, id, echo, data, len);
spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
} else {
/* URB will be re-submitted in _dln2_transfer (free_rx_slot) */
if (dln2_transfer_complete(dln2, urb, handle, echo))
return;
}
out:
err = usb_submit_urb(urb, GFP_ATOMIC);
if (err < 0)
dev_err(dev, "failed to resubmit RX URB: %d\n", err);
}
static void *dln2_prep_buf(u16 handle, u16 cmd, u16 echo, const void *obuf,
int *obuf_len, gfp_t gfp)
{
int len;
void *buf;
struct dln2_header *hdr;
len = *obuf_len + sizeof(*hdr);
buf = kmalloc(len, gfp);
if (!buf)
return NULL;
hdr = (struct dln2_header *)buf;
hdr->id = cpu_to_le16(cmd);
hdr->size = cpu_to_le16(len);
hdr->echo = cpu_to_le16(echo);
hdr->handle = cpu_to_le16(handle);
memcpy(buf + sizeof(*hdr), obuf, *obuf_len);
*obuf_len = len;
return buf;
}
static int dln2_send_wait(struct dln2_dev *dln2, u16 handle, u16 cmd, u16 echo,
const void *obuf, int obuf_len)
{
int ret = 0;
int len = obuf_len;
void *buf;
int actual;
buf = dln2_prep_buf(handle, cmd, echo, obuf, &len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
ret = usb_bulk_msg(dln2->usb_dev,
usb_sndbulkpipe(dln2->usb_dev, dln2->ep_out),
buf, len, &actual, DLN2_USB_TIMEOUT);
kfree(buf);
return ret;
}
static bool find_free_slot(struct dln2_dev *dln2, u16 handle, int *slot)
{
struct dln2_mod_rx_slots *rxs;
unsigned long flags;
if (dln2->disconnect) {
*slot = -ENODEV;
return true;
}
rxs = &dln2->mod_rx_slots[handle];
spin_lock_irqsave(&rxs->lock, flags);
*slot = find_first_zero_bit(rxs->bmap, DLN2_MAX_RX_SLOTS);
if (*slot < DLN2_MAX_RX_SLOTS) {
struct dln2_rx_context *rxc = &rxs->slots[*slot];
set_bit(*slot, rxs->bmap);
rxc->in_use = true;
}
spin_unlock_irqrestore(&rxs->lock, flags);
return *slot < DLN2_MAX_RX_SLOTS;
}
static int alloc_rx_slot(struct dln2_dev *dln2, u16 handle)
{
int ret;
int slot;
/*
* No need to timeout here, the wait is bounded by the timeout in
* _dln2_transfer.
*/
ret = wait_event_interruptible(dln2->mod_rx_slots[handle].wq,
find_free_slot(dln2, handle, &slot));
if (ret < 0)
return ret;
return slot;
}
static void free_rx_slot(struct dln2_dev *dln2, u16 handle, int slot)
{
struct dln2_mod_rx_slots *rxs;
struct urb *urb = NULL;
unsigned long flags;
struct dln2_rx_context *rxc;
rxs = &dln2->mod_rx_slots[handle];
spin_lock_irqsave(&rxs->lock, flags);
clear_bit(slot, rxs->bmap);
rxc = &rxs->slots[slot];
rxc->in_use = false;
urb = rxc->urb;
rxc->urb = NULL;
reinit_completion(&rxc->done);
spin_unlock_irqrestore(&rxs->lock, flags);
if (urb) {
int err;
struct device *dev = &dln2->interface->dev;
err = usb_submit_urb(urb, GFP_KERNEL);
if (err < 0)
dev_err(dev, "failed to resubmit RX URB: %d\n", err);
}
wake_up_interruptible(&rxs->wq);
}
static int _dln2_transfer(struct dln2_dev *dln2, u16 handle, u16 cmd,
const void *obuf, unsigned obuf_len,
void *ibuf, unsigned *ibuf_len)
{
int ret = 0;
int rx_slot;
struct dln2_response *rsp;
struct dln2_rx_context *rxc;
struct device *dev = &dln2->interface->dev;
const unsigned long timeout = msecs_to_jiffies(DLN2_USB_TIMEOUT);
struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[handle];
int size;
spin_lock(&dln2->disconnect_lock);
if (!dln2->disconnect)
dln2->active_transfers++;
else
ret = -ENODEV;
spin_unlock(&dln2->disconnect_lock);
if (ret)
return ret;
rx_slot = alloc_rx_slot(dln2, handle);
if (rx_slot < 0) {
ret = rx_slot;
goto out_decr;
}
ret = dln2_send_wait(dln2, handle, cmd, rx_slot, obuf, obuf_len);
if (ret < 0) {
dev_err(dev, "USB write failed: %d\n", ret);
goto out_free_rx_slot;
}
rxc = &rxs->slots[rx_slot];
ret = wait_for_completion_interruptible_timeout(&rxc->done, timeout);
if (ret <= 0) {
if (!ret)
ret = -ETIMEDOUT;
goto out_free_rx_slot;
} else {
ret = 0;
}
if (dln2->disconnect) {
ret = -ENODEV;
goto out_free_rx_slot;
}
/* if we got here we know that the response header has been checked */
rsp = rxc->urb->transfer_buffer;
size = le16_to_cpu(rsp->hdr.size);
if (size < sizeof(*rsp)) {
ret = -EPROTO;
goto out_free_rx_slot;
}
if (le16_to_cpu(rsp->result) > 0x80) {
dev_dbg(dev, "%d received response with error %d\n",
handle, le16_to_cpu(rsp->result));
ret = -EREMOTEIO;
goto out_free_rx_slot;
}
if (!ibuf)
goto out_free_rx_slot;
if (*ibuf_len > size - sizeof(*rsp))
*ibuf_len = size - sizeof(*rsp);
memcpy(ibuf, rsp + 1, *ibuf_len);
out_free_rx_slot:
free_rx_slot(dln2, handle, rx_slot);
out_decr:
spin_lock(&dln2->disconnect_lock);
dln2->active_transfers--;
spin_unlock(&dln2->disconnect_lock);
if (dln2->disconnect)
wake_up(&dln2->disconnect_wq);
return ret;
}
int dln2_transfer(struct platform_device *pdev, u16 cmd,
const void *obuf, unsigned obuf_len,
void *ibuf, unsigned *ibuf_len)
{
struct dln2_platform_data *dln2_pdata;
struct dln2_dev *dln2;
u16 handle;
dln2 = dev_get_drvdata(pdev->dev.parent);
dln2_pdata = dev_get_platdata(&pdev->dev);
handle = dln2_pdata->handle;
return _dln2_transfer(dln2, handle, cmd, obuf, obuf_len, ibuf,
ibuf_len);
}
EXPORT_SYMBOL(dln2_transfer);
static int dln2_check_hw(struct dln2_dev *dln2)
{
int ret;
__le32 hw_type;
int len = sizeof(hw_type);
ret = _dln2_transfer(dln2, DLN2_HANDLE_CTRL, CMD_GET_DEVICE_VER,
NULL, 0, &hw_type, &len);
if (ret < 0)
return ret;
if (len < sizeof(hw_type))
return -EREMOTEIO;
if (le32_to_cpu(hw_type) != DLN2_HW_ID) {
dev_err(&dln2->interface->dev, "Device ID 0x%x not supported\n",
le32_to_cpu(hw_type));
return -ENODEV;
}
return 0;
}
static int dln2_print_serialno(struct dln2_dev *dln2)
{
int ret;
__le32 serial_no;
int len = sizeof(serial_no);
struct device *dev = &dln2->interface->dev;
ret = _dln2_transfer(dln2, DLN2_HANDLE_CTRL, CMD_GET_DEVICE_SN, NULL, 0,
&serial_no, &len);
if (ret < 0)
return ret;
if (len < sizeof(serial_no))
return -EREMOTEIO;
dev_info(dev, "Diolan DLN2 serial %u\n", le32_to_cpu(serial_no));
return 0;
}
static int dln2_hw_init(struct dln2_dev *dln2)
{
int ret;
ret = dln2_check_hw(dln2);
if (ret < 0)
return ret;
return dln2_print_serialno(dln2);
}
static void dln2_free_rx_urbs(struct dln2_dev *dln2)
{
int i;
for (i = 0; i < DLN2_MAX_URBS; i++) {
usb_free_urb(dln2->rx_urb[i]);
kfree(dln2->rx_buf[i]);
}
}
static void dln2_stop_rx_urbs(struct dln2_dev *dln2)
{
int i;
for (i = 0; i < DLN2_MAX_URBS; i++)
usb_kill_urb(dln2->rx_urb[i]);
}
static void dln2_free(struct dln2_dev *dln2)
{
dln2_free_rx_urbs(dln2);
usb_put_dev(dln2->usb_dev);
kfree(dln2);
}
static int dln2_setup_rx_urbs(struct dln2_dev *dln2,
struct usb_host_interface *hostif)
{
int i;
const int rx_max_size = DLN2_RX_BUF_SIZE;
for (i = 0; i < DLN2_MAX_URBS; i++) {
dln2->rx_buf[i] = kmalloc(rx_max_size, GFP_KERNEL);
if (!dln2->rx_buf[i])
return -ENOMEM;
dln2->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
if (!dln2->rx_urb[i])
return -ENOMEM;
usb_fill_bulk_urb(dln2->rx_urb[i], dln2->usb_dev,
usb_rcvbulkpipe(dln2->usb_dev, dln2->ep_in),
dln2->rx_buf[i], rx_max_size, dln2_rx, dln2);
}
return 0;
}
static int dln2_start_rx_urbs(struct dln2_dev *dln2, gfp_t gfp)
{
struct device *dev = &dln2->interface->dev;
int ret;
int i;
for (i = 0; i < DLN2_MAX_URBS; i++) {
ret = usb_submit_urb(dln2->rx_urb[i], gfp);
if (ret < 0) {
dev_err(dev, "failed to submit RX URB: %d\n", ret);
return ret;
}
}
return 0;
}
static struct dln2_platform_data dln2_pdata_gpio = {
.handle = DLN2_HANDLE_GPIO,
};
/* Only one I2C port seems to be supported on current hardware */
static struct dln2_platform_data dln2_pdata_i2c = {
.handle = DLN2_HANDLE_I2C,
.port = 0,
};
/* Only one SPI port supported */
static struct dln2_platform_data dln2_pdata_spi = {
.handle = DLN2_HANDLE_SPI,
.port = 0,
};
static const struct mfd_cell dln2_devs[] = {
{
.name = "dln2-gpio",
.platform_data = &dln2_pdata_gpio,
.pdata_size = sizeof(struct dln2_platform_data),
},
{
.name = "dln2-i2c",
.platform_data = &dln2_pdata_i2c,
.pdata_size = sizeof(struct dln2_platform_data),
},
{
.name = "dln2-spi",
.platform_data = &dln2_pdata_spi,
.pdata_size = sizeof(struct dln2_platform_data),
},
};
static void dln2_stop(struct dln2_dev *dln2)
{
int i, j;
/* don't allow starting new transfers */
spin_lock(&dln2->disconnect_lock);
dln2->disconnect = true;
spin_unlock(&dln2->disconnect_lock);
/* cancel in progress transfers */
for (i = 0; i < DLN2_HANDLES; i++) {
struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[i];
unsigned long flags;
spin_lock_irqsave(&rxs->lock, flags);
/* cancel all response waiters */
for (j = 0; j < DLN2_MAX_RX_SLOTS; j++) {
struct dln2_rx_context *rxc = &rxs->slots[j];
if (rxc->in_use)
complete(&rxc->done);
}
spin_unlock_irqrestore(&rxs->lock, flags);
}
/* wait for transfers to end */
wait_event(dln2->disconnect_wq, !dln2->active_transfers);
dln2_stop_rx_urbs(dln2);
}
static void dln2_disconnect(struct usb_interface *interface)
{
struct dln2_dev *dln2 = usb_get_intfdata(interface);
dln2_stop(dln2);
mfd_remove_devices(&interface->dev);
dln2_free(dln2);
}
static int dln2_probe(struct usb_interface *interface,
const struct usb_device_id *usb_id)
{
struct usb_host_interface *hostif = interface->cur_altsetting;
struct usb_endpoint_descriptor *epin;
struct usb_endpoint_descriptor *epout;
struct device *dev = &interface->dev;
struct dln2_dev *dln2;
int ret;
int i, j;
if (hostif->desc.bInterfaceNumber != 0 ||
hostif->desc.bNumEndpoints < 2)
return -ENODEV;
epout = &hostif->endpoint[DLN2_EP_OUT].desc;
if (!usb_endpoint_is_bulk_out(epout))
return -ENODEV;
epin = &hostif->endpoint[DLN2_EP_IN].desc;
if (!usb_endpoint_is_bulk_in(epin))
return -ENODEV;
dln2 = kzalloc(sizeof(*dln2), GFP_KERNEL);
if (!dln2)
return -ENOMEM;
dln2->ep_out = epout->bEndpointAddress;
dln2->ep_in = epin->bEndpointAddress;
dln2->usb_dev = usb_get_dev(interface_to_usbdev(interface));
dln2->interface = interface;
usb_set_intfdata(interface, dln2);
init_waitqueue_head(&dln2->disconnect_wq);
for (i = 0; i < DLN2_HANDLES; i++) {
init_waitqueue_head(&dln2->mod_rx_slots[i].wq);
spin_lock_init(&dln2->mod_rx_slots[i].lock);
for (j = 0; j < DLN2_MAX_RX_SLOTS; j++)
init_completion(&dln2->mod_rx_slots[i].slots[j].done);
}
spin_lock_init(&dln2->event_cb_lock);
spin_lock_init(&dln2->disconnect_lock);
INIT_LIST_HEAD(&dln2->event_cb_list);
ret = dln2_setup_rx_urbs(dln2, hostif);
if (ret)
goto out_free;
ret = dln2_start_rx_urbs(dln2, GFP_KERNEL);
if (ret)
goto out_stop_rx;
ret = dln2_hw_init(dln2);
if (ret < 0) {
dev_err(dev, "failed to initialize hardware\n");
goto out_stop_rx;
}
ret = mfd_add_hotplug_devices(dev, dln2_devs, ARRAY_SIZE(dln2_devs));
if (ret != 0) {
dev_err(dev, "failed to add mfd devices to core\n");
goto out_stop_rx;
}
return 0;
out_stop_rx:
dln2_stop_rx_urbs(dln2);
out_free:
dln2_free(dln2);
return ret;
}
static int dln2_suspend(struct usb_interface *iface, pm_message_t message)
{
struct dln2_dev *dln2 = usb_get_intfdata(iface);
dln2_stop(dln2);
return 0;
}
static int dln2_resume(struct usb_interface *iface)
{
struct dln2_dev *dln2 = usb_get_intfdata(iface);
dln2->disconnect = false;
return dln2_start_rx_urbs(dln2, GFP_NOIO);
}
static const struct usb_device_id dln2_table[] = {
{ USB_DEVICE(0xa257, 0x2013) },
{ }
};
MODULE_DEVICE_TABLE(usb, dln2_table);
static struct usb_driver dln2_driver = {
.name = "dln2",
.probe = dln2_probe,
.disconnect = dln2_disconnect,
.id_table = dln2_table,
.suspend = dln2_suspend,
.resume = dln2_resume,
};
module_usb_driver(dln2_driver);
MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>");
MODULE_DESCRIPTION("Core driver for the Diolan DLN2 interface adapter");
MODULE_LICENSE("GPL v2");