Files
kernel_xiaomi_raphael/drivers/usb/misc/lvstest.c
UtsavBalar1231 e529165a4f Merge remote-tracking branch 'aosp/android-4.14-stable' into auto-kernel-base
* aosp/android-4.14-stable:
  Linux 4.14.196
  ALSA: usb-audio: Update documentation comment for MS2109 quirk
  HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
  tpm: Unify the mismatching TPM space buffer sizes
  btrfs: check the right error variable in btrfs_del_dir_entries_in_log
  usb: storage: Add unusual_uas entry for Sony PSZ drives
  USB: cdc-acm: rework notification_buffer resizing
  USB: gadget: u_f: Unbreak offset calculation in VLAs
  USB: gadget: f_ncm: add bounds checks to ncm_unwrap_ntb()
  USB: gadget: u_f: add overflow checks to VLA macros
  overflow.h: Add allocation size calculation helpers
  usb: host: ohci-exynos: Fix error handling in exynos_ohci_probe()
  USB: Ignore UAS for JMicron JMS567 ATA/ATAPI Bridge
  USB: quirks: Add no-lpm quirk for another Raydium touchscreen
  usb: uas: Add quirk for PNY Pro Elite
  USB: yurex: Fix bad gfp argument
  drm/amdgpu: Fix buffer overflow in INFO ioctl
  device property: Fix the secondary firmware node handling in set_primary_fwnode()
  PM: sleep: core: Fix the handling of pending runtime resume requests
  xhci: Do warm-reset when both CAS and XDEV_RESUME are set
  XEN uses irqdesc::irq_data_common::handler_data to store a per interrupt XEN data pointer which contains XEN specific information.
  writeback: Fix sync livelock due to b_dirty_time processing
  writeback: Avoid skipping inode writeback
  writeback: Protect inode->i_io_list with inode->i_lock
  serial: 8250: change lock order in serial8250_do_startup()
  serial: 8250_exar: Fix number of ports for Commtech PCIe cards
  serial: pl011: Don't leak amba_ports entry on driver register error
  serial: pl011: Fix oops on -EPROBE_DEFER
  serial: samsung: Removes the IRQ not found warning
  vt_ioctl: change VT_RESIZEX ioctl to check for error return from vc_resize()
  vt: defer kfree() of vc_screenbuf in vc_do_resize()
  USB: lvtest: return proper error code in probe
  fbcon: prevent user font height or width change from causing potential out-of-bounds access
  btrfs: fix space cache memory leak after transaction abort
  HID: i2c-hid: Always sleep 60ms after I2C_HID_PWR_ON commands
  powerpc/perf: Fix soft lockups due to missed interrupt accounting
  net: gianfar: Add of_node_put() before goto statement
  scsi: ufs: Clean up completed request without interrupt notification
  scsi: ufs: Improve interrupt handling for shared interrupts
  scsi: ufs: Fix possible infinite loop in ufshcd_hold
  s390/cio: add cond_resched() in the slow_eval_known_fn() loop
  spi: stm32: fix stm32_spi_prepare_mbr in case of odd clk_rate
  fs: prevent BUG_ON in submit_bh_wbc()
  jbd2: abort journal if free a async write error metadata buffer
  ext4: don't BUG on inconsistent journal feature
  jbd2: make sure jh have b_transaction set in refile/unfile_buffer
  usb: gadget: f_tcm: Fix some resource leaks in some error paths
  i2c: rcar: in slave mode, clear NACK earlier
  null_blk: fix passing of REQ_FUA flag in null_handle_rq
  nvme-fc: Fix wrong return value in __nvme_fc_init_request()
  media: gpio-ir-tx: improve precision of transmitted signal due to scheduling
  Revert "ath10k: fix DMA related firmware crashes on multiple devices"
  efi: provide empty efi_enter_virtual_mode implementation
  USB: sisusbvga: Fix a potential UB casued by left shifting a negative value
  powerpc/spufs: add CONFIG_COREDUMP dependency
  KVM: arm64: Fix symbol dependency in __hyp_call_panic_nvhe
  media: davinci: vpif_capture: fix potential double free
  EDAC/ie31200: Fallback if host bridge device is already initialized
  scsi: fcoe: Memory leak fix in fcoe_sysfs_fcf_del()
  ceph: fix potential mdsc use-after-free crash
  scsi: iscsi: Do not put host in iscsi_set_flashnode_param()
  locking/lockdep: Fix overflow in presentation of average lock-time
  drm/nouveau: Fix reference count leak in nouveau_connector_detect
  drm/nouveau/drm/noveau: fix reference count leak in nouveau_fbcon_open
  f2fs: fix use-after-free issue
  cec-api: prevent leaking memory through hole in structure
  mips/vdso: Fix resource leaks in genvdso.c
  rtlwifi: rtl8192cu: Prevent leaking urb
  PCI: Fix pci_create_slot() reference count leak
  omapfb: fix multiple reference count leaks due to pm_runtime_get_sync
  selftests/powerpc: Purge extra count_pmc() calls of ebb selftests
  scsi: lpfc: Fix shost refcount mismatch when deleting vport
  drm/amdgpu/display: fix ref count leak when pm_runtime_get_sync fails
  drm/amdgpu: fix ref count leak in amdgpu_display_crtc_set_config
  drm/amd/display: fix ref count leak in amdgpu_drm_ioctl
  drm/amdgpu: fix ref count leak in amdgpu_driver_open_kms
  drm/radeon: fix multiple reference count leak
  drm/amdkfd: Fix reference count leaks.
  iommu/iova: Don't BUG on invalid PFNs
  scsi: target: tcmu: Fix crash on ARM during cmd completion
  blktrace: ensure our debugfs dir exists
  media: pci: ttpci: av7110: fix possible buffer overflow caused by bad DMA value in debiirq()
  powerpc/xive: Ignore kmemleak false positives
  arm64: dts: qcom: msm8916: Pull down PDM GPIOs during sleep
  mfd: intel-lpss: Add Intel Emmitsburg PCH PCI IDs
  ASoC: tegra: Fix reference count leaks.
  ALSA: pci: delete repeated words in comments
  gre6: Fix reception with IP6_TNL_F_RCV_DSCP_COPY
  ipvlan: fix device features
  tipc: fix uninit skb->data in tipc_nl_compat_dumpit()
  net: Fix potential wrong skb->protocol in skb_vlan_untag()
  powerpc/64s: Don't init FSCR_DSCR in __init_FSCR()
  ANDROID: cuttlefish_defconfig: initialize locals with zeroes
  BACKPORT: security: allow using Clang's zero initialization for stack variables
  Revert "binder: Prevent context manager from incrementing ref 0"

Signed-off-by: UtsavBalar1231 <utsavbalar1231@gmail.com>
2020-09-04 20:22:00 +05:30

498 lines
12 KiB
C

/*
* drivers/usb/misc/lvstest.c
*
* Test pattern generation for Link Layer Validation System Tests
*
* Copyright (C) 2014 ST Microelectronics
* Pratyush Anand <pratyush.anand@gmail.com>
*
* This file is licensed under the terms of the GNU General Public
* License version 2. This program is licensed "as is" without any
* warranty of any kind, whether express or implied.
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/usb.h>
#include <linux/usb/ch11.h>
#include <linux/usb/hcd.h>
#include <linux/usb/phy.h>
struct lvs_rh {
/* root hub interface */
struct usb_interface *intf;
/* if lvs device connected */
bool present;
/* port no at which lvs device is present */
int portnum;
/* urb buffer */
u8 buffer[8];
/* class descriptor */
struct usb_hub_descriptor descriptor;
/* urb for polling interrupt pipe */
struct urb *urb;
/* LVH RH work */
struct work_struct rh_work;
/* RH port status */
struct usb_port_status port_status;
};
static struct usb_device *create_lvs_device(struct usb_interface *intf)
{
struct usb_device *udev, *hdev;
struct usb_hcd *hcd;
struct lvs_rh *lvs = usb_get_intfdata(intf);
if (!lvs->present) {
dev_err(&intf->dev, "No LVS device is present\n");
return NULL;
}
hdev = interface_to_usbdev(intf);
hcd = bus_to_hcd(hdev->bus);
udev = usb_alloc_dev(hdev, hdev->bus, lvs->portnum);
if (!udev) {
dev_err(&intf->dev, "Could not allocate lvs udev\n");
return NULL;
}
udev->speed = USB_SPEED_SUPER;
udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
usb_set_device_state(udev, USB_STATE_DEFAULT);
if (hcd->driver->enable_device) {
if (hcd->driver->enable_device(hcd, udev) < 0) {
dev_err(&intf->dev, "Failed to enable\n");
usb_put_dev(udev);
return NULL;
}
}
return udev;
}
static void destroy_lvs_device(struct usb_device *udev)
{
struct usb_device *hdev = udev->parent;
struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
if (hcd->driver->free_dev)
hcd->driver->free_dev(hcd, udev);
usb_put_dev(udev);
}
static int lvs_rh_clear_port_feature(struct usb_device *hdev,
int port1, int feature)
{
return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
NULL, 0, 1000);
}
static int lvs_rh_set_port_feature(struct usb_device *hdev,
int port1, int feature)
{
return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
NULL, 0, 1000);
}
static ssize_t u3_entry_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct usb_interface *intf = to_usb_interface(dev);
struct usb_device *hdev = interface_to_usbdev(intf);
struct lvs_rh *lvs = usb_get_intfdata(intf);
struct usb_device *udev;
int ret;
udev = create_lvs_device(intf);
if (!udev) {
dev_err(dev, "failed to create lvs device\n");
return -ENOMEM;
}
ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
USB_PORT_FEAT_SUSPEND);
if (ret < 0)
dev_err(dev, "can't issue U3 entry %d\n", ret);
destroy_lvs_device(udev);
if (ret < 0)
return ret;
return count;
}
static DEVICE_ATTR_WO(u3_entry);
static ssize_t u3_exit_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct usb_interface *intf = to_usb_interface(dev);
struct usb_device *hdev = interface_to_usbdev(intf);
struct lvs_rh *lvs = usb_get_intfdata(intf);
struct usb_device *udev;
int ret;
udev = create_lvs_device(intf);
if (!udev) {
dev_err(dev, "failed to create lvs device\n");
return -ENOMEM;
}
ret = lvs_rh_clear_port_feature(hdev, lvs->portnum,
USB_PORT_FEAT_SUSPEND);
if (ret < 0)
dev_err(dev, "can't issue U3 exit %d\n", ret);
destroy_lvs_device(udev);
if (ret < 0)
return ret;
return count;
}
static DEVICE_ATTR_WO(u3_exit);
static ssize_t hot_reset_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct usb_interface *intf = to_usb_interface(dev);
struct usb_device *hdev = interface_to_usbdev(intf);
struct lvs_rh *lvs = usb_get_intfdata(intf);
int ret;
ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
USB_PORT_FEAT_RESET);
if (ret < 0) {
dev_err(dev, "can't issue hot reset %d\n", ret);
return ret;
}
return count;
}
static DEVICE_ATTR_WO(hot_reset);
static ssize_t warm_reset_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct usb_interface *intf = to_usb_interface(dev);
struct usb_device *hdev = interface_to_usbdev(intf);
struct lvs_rh *lvs = usb_get_intfdata(intf);
int port;
int ret;
if (kstrtoint(buf, 0, &port) || port < 1 || port > 255)
port = lvs->portnum;
ret = lvs_rh_set_port_feature(hdev, port, USB_PORT_FEAT_BH_PORT_RESET);
if (ret < 0) {
dev_err(dev, "can't issue warm reset %d\n", ret);
return ret;
}
return count;
}
static DEVICE_ATTR_WO(warm_reset);
static ssize_t u2_timeout_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct usb_interface *intf = to_usb_interface(dev);
struct usb_device *hdev = interface_to_usbdev(intf);
struct lvs_rh *lvs = usb_get_intfdata(intf);
unsigned long val;
int ret;
ret = kstrtoul(buf, 10, &val);
if (ret < 0) {
dev_err(dev, "couldn't parse string %d\n", ret);
return ret;
}
if (val > 127)
return -EINVAL;
ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
USB_PORT_FEAT_U2_TIMEOUT);
if (ret < 0) {
dev_err(dev, "Error %d while setting U2 timeout %ld\n", ret, val);
return ret;
}
return count;
}
static DEVICE_ATTR_WO(u2_timeout);
static ssize_t u1_timeout_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct usb_interface *intf = to_usb_interface(dev);
struct usb_device *hdev = interface_to_usbdev(intf);
struct lvs_rh *lvs = usb_get_intfdata(intf);
unsigned long val;
int ret;
ret = kstrtoul(buf, 10, &val);
if (ret < 0) {
dev_err(dev, "couldn't parse string %d\n", ret);
return ret;
}
if (val > 127)
return -EINVAL;
ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
USB_PORT_FEAT_U1_TIMEOUT);
if (ret < 0) {
dev_err(dev, "Error %d while setting U1 timeout %ld\n", ret, val);
return ret;
}
return count;
}
static DEVICE_ATTR_WO(u1_timeout);
static ssize_t get_dev_desc_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct usb_interface *intf = to_usb_interface(dev);
struct usb_device *udev;
struct usb_device_descriptor *descriptor;
int ret;
descriptor = kmalloc(sizeof(*descriptor), GFP_KERNEL);
if (!descriptor)
return -ENOMEM;
udev = create_lvs_device(intf);
if (!udev) {
dev_err(dev, "failed to create lvs device\n");
ret = -ENOMEM;
goto free_desc;
}
ret = usb_control_msg(udev, (PIPE_CONTROL << 30) | USB_DIR_IN,
USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, USB_DT_DEVICE << 8,
0, descriptor, sizeof(*descriptor),
USB_CTRL_GET_TIMEOUT);
if (ret < 0)
dev_err(dev, "can't read device descriptor %d\n", ret);
destroy_lvs_device(udev);
free_desc:
kfree(descriptor);
if (ret < 0)
return ret;
return count;
}
static DEVICE_ATTR_WO(get_dev_desc);
static ssize_t enable_compliance_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct usb_interface *intf = to_usb_interface(dev);
struct usb_device *hdev = interface_to_usbdev(intf);
struct lvs_rh *lvs = usb_get_intfdata(intf);
int port;
int ret;
if (kstrtoint(buf, 0, &port) || port < 1 || port > 255)
port = lvs->portnum;
ret = lvs_rh_set_port_feature(hdev,
port | (USB_SS_PORT_LS_COMP_MOD << 3),
USB_PORT_FEAT_LINK_STATE);
if (ret < 0) {
dev_err(dev, "can't enable compliance mode %d\n", ret);
return ret;
}
return count;
}
static DEVICE_ATTR_WO(enable_compliance);
static struct attribute *lvs_attributes[] = {
&dev_attr_get_dev_desc.attr,
&dev_attr_u1_timeout.attr,
&dev_attr_u2_timeout.attr,
&dev_attr_hot_reset.attr,
&dev_attr_warm_reset.attr,
&dev_attr_u3_entry.attr,
&dev_attr_u3_exit.attr,
&dev_attr_enable_compliance.attr,
NULL
};
static const struct attribute_group lvs_attr_group = {
.attrs = lvs_attributes,
};
static void lvs_rh_work(struct work_struct *work)
{
struct lvs_rh *lvs = container_of(work, struct lvs_rh, rh_work);
struct usb_interface *intf = lvs->intf;
struct usb_device *hdev = interface_to_usbdev(intf);
struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
struct usb_hub_descriptor *descriptor = &lvs->descriptor;
struct usb_port_status *port_status = &lvs->port_status;
int i, ret = 0;
u16 portchange;
/* Examine each root port */
for (i = 1; i <= descriptor->bNbrPorts; i++) {
ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, i,
port_status, sizeof(*port_status), 1000);
if (ret < 4)
continue;
portchange = le16_to_cpu(port_status->wPortChange);
if (portchange & USB_PORT_STAT_C_LINK_STATE)
lvs_rh_clear_port_feature(hdev, i,
USB_PORT_FEAT_C_PORT_LINK_STATE);
if (portchange & USB_PORT_STAT_C_ENABLE)
lvs_rh_clear_port_feature(hdev, i,
USB_PORT_FEAT_C_ENABLE);
if (portchange & USB_PORT_STAT_C_RESET)
lvs_rh_clear_port_feature(hdev, i,
USB_PORT_FEAT_C_RESET);
if (portchange & USB_PORT_STAT_C_BH_RESET)
lvs_rh_clear_port_feature(hdev, i,
USB_PORT_FEAT_C_BH_PORT_RESET);
if (portchange & USB_PORT_STAT_C_CONNECTION) {
lvs_rh_clear_port_feature(hdev, i,
USB_PORT_FEAT_C_CONNECTION);
if (le16_to_cpu(port_status->wPortStatus) &
USB_PORT_STAT_CONNECTION) {
lvs->present = true;
lvs->portnum = i;
if (hcd->usb_phy)
usb_phy_notify_connect(hcd->usb_phy,
USB_SPEED_SUPER);
} else {
lvs->present = false;
if (hcd->usb_phy)
usb_phy_notify_disconnect(hcd->usb_phy,
USB_SPEED_SUPER);
}
break;
}
}
ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
if (ret != 0 && ret != -ENODEV && ret != -EPERM)
dev_err(&intf->dev, "urb resubmit error %d\n", ret);
}
static void lvs_rh_irq(struct urb *urb)
{
struct lvs_rh *lvs = urb->context;
schedule_work(&lvs->rh_work);
}
static int lvs_rh_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct usb_device *hdev;
struct usb_host_interface *desc;
struct usb_endpoint_descriptor *endpoint;
struct lvs_rh *lvs;
unsigned int pipe;
int ret, maxp;
hdev = interface_to_usbdev(intf);
desc = intf->cur_altsetting;
ret = usb_find_int_in_endpoint(desc, &endpoint);
if (ret)
return ret;
/* valid only for SS root hub */
if (hdev->descriptor.bDeviceProtocol != USB_HUB_PR_SS || hdev->parent) {
dev_err(&intf->dev, "Bind LVS driver with SS root Hub only\n");
return -EINVAL;
}
lvs = devm_kzalloc(&intf->dev, sizeof(*lvs), GFP_KERNEL);
if (!lvs)
return -ENOMEM;
lvs->intf = intf;
usb_set_intfdata(intf, lvs);
/* how many number of ports this root hub has */
ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
USB_DT_SS_HUB << 8, 0, &lvs->descriptor,
USB_DT_SS_HUB_SIZE, USB_CTRL_GET_TIMEOUT);
if (ret < (USB_DT_HUB_NONVAR_SIZE + 2)) {
dev_err(&hdev->dev, "wrong root hub descriptor read %d\n", ret);
return ret < 0 ? ret : -EINVAL;
}
/* submit urb to poll interrupt endpoint */
lvs->urb = usb_alloc_urb(0, GFP_KERNEL);
if (!lvs->urb)
return -ENOMEM;
INIT_WORK(&lvs->rh_work, lvs_rh_work);
ret = sysfs_create_group(&intf->dev.kobj, &lvs_attr_group);
if (ret < 0) {
dev_err(&intf->dev, "Failed to create sysfs node %d\n", ret);
goto free_urb;
}
pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
usb_fill_int_urb(lvs->urb, hdev, pipe, &lvs->buffer[0], maxp,
lvs_rh_irq, lvs, endpoint->bInterval);
ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
if (ret < 0) {
dev_err(&intf->dev, "couldn't submit lvs urb %d\n", ret);
goto sysfs_remove;
}
return ret;
sysfs_remove:
sysfs_remove_group(&intf->dev.kobj, &lvs_attr_group);
free_urb:
usb_free_urb(lvs->urb);
return ret;
}
static void lvs_rh_disconnect(struct usb_interface *intf)
{
struct lvs_rh *lvs = usb_get_intfdata(intf);
sysfs_remove_group(&intf->dev.kobj, &lvs_attr_group);
usb_poison_urb(lvs->urb); /* used in scheduled work */
flush_work(&lvs->rh_work);
usb_free_urb(lvs->urb);
}
static struct usb_driver lvs_driver = {
.name = "lvs",
.probe = lvs_rh_probe,
.disconnect = lvs_rh_disconnect,
};
module_usb_driver(lvs_driver);
MODULE_DESCRIPTION("Link Layer Validation System Driver");
MODULE_LICENSE("GPL");