Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ecfde814 authored by Ajay Agarwal's avatar Ajay Agarwal
Browse files

usb: misc: Add support for test modes via external hub



Some external hubs cannot enter into test mode if EHSET fixture
is connected to one of its downstream ports. Add support to
enable test modes on any desired downstream port of an external
hub. The port and the test mode can be selected via sysfs nodes.

For example, to send TEST_J packet on downstream port 4 of
external hub 3-1:1.0:
echo 4 > /sys/bus/usb/devices/3-1/3-1\:1.0/test_j_portnum

Change-Id: I145f150bfe8557844cb150cf39fafe03a18bd955
Signed-off-by: default avatarAjay Agarwal <ajaya@codeaurora.org>
parent 0850edf3
Loading
Loading
Loading
Loading
+254 −50
Original line number Original line Diff line number Diff line
/*
/*
 * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
 * Copyright (c) 2010-2013, 2018, The Linux Foundation. All rights reserved.
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * it under the terms of the GNU General Public License version 2 and
@@ -26,69 +26,89 @@
#define TEST_SINGLE_STEP_GET_DEV_DESC		0x0107
#define TEST_SINGLE_STEP_GET_DEV_DESC		0x0107
#define TEST_SINGLE_STEP_SET_FEATURE		0x0108
#define TEST_SINGLE_STEP_SET_FEATURE		0x0108


static int ehset_probe(struct usb_interface *intf,
static u8 numPorts;
		       const struct usb_device_id *id)

static int ehset_get_port_num(struct device *dev, const char *buf,
							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 || *val > numPorts) {
		dev_err(dev, "Invalid port num entered\n");
		return -EINVAL;
	}

	return 0;
}

static int ehset_clear_port_feature(struct usb_device *udev, int feature,
				int port1)
{
	return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
		USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
		NULL, 0, 1000);
}

static int ehset_set_port_feature(struct usb_device *udev, int feature,
			int port1, int timeout)
{
	return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
		USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
		NULL, 0, timeout);
}

static int ehset_set_testmode(struct device *dev, struct usb_device *child_udev,
			struct usb_device *hub_udev, int test_id, int port)
{
{
	int ret = -EINVAL;
	struct usb_device *dev = interface_to_usbdev(intf);
	struct usb_device *hub_udev = dev->parent;
	struct usb_device_descriptor *buf;
	struct usb_device_descriptor *buf;
	u8 portnum = dev->portnum;
	int ret = -EINVAL;
	u16 test_pid = le16_to_cpu(dev->descriptor.idProduct);


	switch (test_pid) {
	switch (test_id) {
	case TEST_SE0_NAK_PID:
	case TEST_SE0_NAK_PID:
		ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
		ret = ehset_set_port_feature(hub_udev, USB_PORT_FEAT_TEST,
					USB_REQ_SET_FEATURE, USB_RT_PORT,
					(TEST_SE0_NAK << 8) | port, 1000);
					USB_PORT_FEAT_TEST,
					(TEST_SE0_NAK << 8) | portnum,
					NULL, 0, 1000);
		break;
		break;
	case TEST_J_PID:
	case TEST_J_PID:
		ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
		ret = ehset_set_port_feature(hub_udev, USB_PORT_FEAT_TEST,
					USB_REQ_SET_FEATURE, USB_RT_PORT,
					(TEST_J << 8) | port, 1000);
					USB_PORT_FEAT_TEST,
					(TEST_J << 8) | portnum,
					NULL, 0, 1000);
		break;
		break;
	case TEST_K_PID:
	case TEST_K_PID:
		ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
		ret = ehset_set_port_feature(hub_udev, USB_PORT_FEAT_TEST,
					USB_REQ_SET_FEATURE, USB_RT_PORT,
					(TEST_K << 8) | port, 1000);
					USB_PORT_FEAT_TEST,
					(TEST_K << 8) | portnum,
					NULL, 0, 1000);
		break;
		break;
	case TEST_PACKET_PID:
	case TEST_PACKET_PID:
		ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
		ret = ehset_set_port_feature(hub_udev, USB_PORT_FEAT_TEST,
					USB_REQ_SET_FEATURE, USB_RT_PORT,
					(TEST_PACKET << 8) | port, 1000);
					USB_PORT_FEAT_TEST,
					(TEST_PACKET << 8) | portnum,
					NULL, 0, 1000);
		break;
		break;
	case TEST_HS_HOST_PORT_SUSPEND_RESUME:
	case TEST_HS_HOST_PORT_SUSPEND_RESUME:
		/* Test: wait for 15secs -> suspend -> 15secs delay -> resume */
		/* Test: wait for 15secs -> suspend -> 15secs delay -> resume */
		msleep(15 * 1000);
		msleep(15 * 1000);
		ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
		ret = ehset_set_port_feature(hub_udev, USB_PORT_FEAT_SUSPEND,
					USB_REQ_SET_FEATURE, USB_RT_PORT,
							port, 1000);
					USB_PORT_FEAT_SUSPEND, portnum,
		if (ret)
					NULL, 0, 1000);
		if (ret < 0)
			break;
			break;


		msleep(15 * 1000);
		msleep(15 * 1000);
		ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
		ret = ehset_clear_port_feature(hub_udev, USB_PORT_FEAT_SUSPEND,
					USB_REQ_CLEAR_FEATURE, USB_RT_PORT,
							port);
					USB_PORT_FEAT_SUSPEND, portnum,
					NULL, 0, 1000);
		break;
		break;
	case TEST_SINGLE_STEP_GET_DEV_DESC:
	case TEST_SINGLE_STEP_GET_DEV_DESC:
		/* Test: wait for 15secs -> GetDescriptor request */
		/* Test: wait for 15secs -> GetDescriptor request */
		msleep(15 * 1000);
		msleep(15 * 1000);
		buf = kmalloc(USB_DT_DEVICE_SIZE, GFP_KERNEL);
		buf = kmalloc(USB_DT_DEVICE_SIZE, GFP_KERNEL);
		if (!buf)
		if (!buf) {
			return -ENOMEM;
			ret = -ENOMEM;
			break;
		}


		ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
		ret = usb_control_msg(child_udev,
					usb_rcvctrlpipe(child_udev, 0),
					USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
					USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
					USB_DT_DEVICE << 8, 0,
					USB_DT_DEVICE << 8, 0,
					buf, USB_DT_DEVICE_SIZE,
					buf, USB_DT_DEVICE_SIZE,
@@ -103,28 +123,212 @@ static int ehset_probe(struct usb_interface *intf,
		 * SetPortFeature handling can only be done inside the HCD's
		 * SetPortFeature handling can only be done inside the HCD's
		 * hub_control callback function.
		 * hub_control callback function.
		 */
		 */
		if (hub_udev != dev->bus->root_hub) {
		if (hub_udev != child_udev->bus->root_hub) {
			dev_err(&intf->dev, "SINGLE_STEP_SET_FEATURE test only supported on root hub\n");
			dev_err(dev, "SINGLE_STEP_SET_FEATURE test only supported on root hub\n");
			break;
			break;
		}
		}


		ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
		ret = ehset_set_port_feature(hub_udev, USB_PORT_FEAT_TEST,
					USB_REQ_SET_FEATURE, USB_RT_PORT,
						(6 << 8) | port, 60 * 1000);
					USB_PORT_FEAT_TEST,
					(6 << 8) | portnum,
					NULL, 0, 60 * 1000);


		break;
		break;
	default:
	default:
		dev_err(&intf->dev, "%s: unsupported PID: 0x%x\n",
		dev_err(dev, "%s: unsupported test ID: 0x%x\n",
			__func__, test_pid);
			__func__, test_id);
	}

	return ret;
}

static ssize_t test_se0_nak_portnum_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 = interface_to_usbdev(intf);
	unsigned long portnum;
	int ret;

	ret = ehset_get_port_num(dev, buf, &portnum);
	if (ret)
		return ret;

	usb_lock_device(udev);
	ret = ehset_set_testmode(dev, NULL, udev, TEST_SE0_NAK_PID, portnum);
	usb_unlock_device(udev);
	if (ret) {
		dev_err(dev, "Error %d while SE0_NAK test\n", ret);
		return ret;
	}

	return count;
}
static DEVICE_ATTR_WO(test_se0_nak_portnum);

static ssize_t test_j_portnum_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 = interface_to_usbdev(intf);
	unsigned long portnum;
	int ret;

	ret = ehset_get_port_num(dev, buf, &portnum);
	if (ret)
		return ret;

	usb_lock_device(udev);
	ret = ehset_set_testmode(dev, NULL, udev, TEST_J_PID, portnum);
	usb_unlock_device(udev);
	if (ret) {
		dev_err(dev, "Error %d while J state test\n", ret);
		return ret;
	}

	return count;
}
static DEVICE_ATTR_WO(test_j_portnum);

static ssize_t test_k_portnum_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 = interface_to_usbdev(intf);
	unsigned long portnum;
	int ret;

	ret = ehset_get_port_num(dev, buf, &portnum);
	if (ret)
		return ret;

	usb_lock_device(udev);
	ret = ehset_set_testmode(dev, NULL, udev, TEST_K_PID, portnum);
	usb_unlock_device(udev);
	if (ret) {
		dev_err(dev, "Error %d while K state test\n", ret);
		return ret;
	}

	return count;
}
static DEVICE_ATTR_WO(test_k_portnum);

static ssize_t test_packet_portnum_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 = interface_to_usbdev(intf);
	unsigned long portnum;
	int ret;

	ret = ehset_get_port_num(dev, buf, &portnum);
	if (ret)
		return ret;

	usb_lock_device(udev);
	ret = ehset_set_testmode(dev, NULL, udev, TEST_PACKET_PID, portnum);
	usb_unlock_device(udev);
	if (ret) {
		dev_err(dev, "Error %d while sending test packets\n", ret);
		return ret;
	}

	return count;
}
static DEVICE_ATTR_WO(test_packet_portnum);

static ssize_t test_port_susp_resume_portnum_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 = interface_to_usbdev(intf);
	unsigned long portnum;
	int ret;

	ret = ehset_get_port_num(dev, buf, &portnum);
	if (ret)
		return ret;

	usb_lock_device(udev);
	ret = ehset_set_testmode(dev, NULL, udev,
				TEST_HS_HOST_PORT_SUSPEND_RESUME, portnum);
	usb_unlock_device(udev);
	if (ret) {
		dev_err(dev, "Error %d while port suspend resume test\n", ret);
		return ret;
	}

	return count;
}
static DEVICE_ATTR_WO(test_port_susp_resume_portnum);

static struct attribute *ehset_attributes[] = {
	&dev_attr_test_se0_nak_portnum.attr,
	&dev_attr_test_j_portnum.attr,
	&dev_attr_test_k_portnum.attr,
	&dev_attr_test_packet_portnum.attr,
	&dev_attr_test_port_susp_resume_portnum.attr,
	NULL
};

static const struct attribute_group ehset_attr_group = {
	.attrs = ehset_attributes,
};

static int ehset_probe(struct usb_interface *intf,
		       const struct usb_device_id *id)
{
	int ret = -EINVAL;
	struct usb_device *dev = interface_to_usbdev(intf);
	struct usb_device *hub_udev = dev->parent;
	u8 portnum = dev->portnum;
	u16 test_pid = le16_to_cpu(dev->descriptor.idProduct);

	/*
	 * If an external hub does not support the EHSET test fixture, then user
	 * can forcefully unbind the external hub from the hub driver (to which
	 * an external hub gets bound by default) and bind it to this driver, so
	 * as to send test signals on any downstream port of the hub.
	 */
	if (dev->descriptor.bDeviceClass == USB_CLASS_HUB) {
		struct usb_hub_descriptor *descriptor;

		descriptor = kzalloc(sizeof(*descriptor), GFP_KERNEL);
		if (!descriptor)
			return -ENOMEM;

		ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
				USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
				USB_DT_HUB << 8, 0, descriptor,
				USB_DT_HUB_NONVAR_SIZE, USB_CTRL_GET_TIMEOUT);
		if (ret < 0) {
			dev_err(&intf->dev, "%s: Failed to get hub desc %d\n",
								__func__, ret);
			kfree(descriptor);
			return ret;
		}

		numPorts = descriptor->bNbrPorts;
		ret = sysfs_create_group(&intf->dev.kobj, &ehset_attr_group);
		if (ret < 0)
			dev_err(&intf->dev, "%s: Failed to create sysfs nodes %d\n",
								__func__, ret);

		kfree(descriptor);
		return ret;
	}
	}


	ret = ehset_set_testmode(&intf->dev, dev, hub_udev, test_pid, portnum);

	return (ret < 0) ? ret : 0;
	return (ret < 0) ? ret : 0;
}
}


static void ehset_disconnect(struct usb_interface *intf)
static void ehset_disconnect(struct usb_interface *intf)
{
{
	struct usb_device *dev = interface_to_usbdev(intf);

	numPorts = 0;
	if (dev->descriptor.bDeviceClass == USB_CLASS_HUB)
		sysfs_remove_group(&intf->dev.kobj, &ehset_attr_group);
}
}


static const struct usb_device_id ehset_id_table[] = {
static const struct usb_device_id ehset_id_table[] = {