18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/kernel.h>
78c2ecf20Sopenharmony_ci#include <linux/errno.h>
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/slab.h>
108c2ecf20Sopenharmony_ci#include <linux/usb.h>
118c2ecf20Sopenharmony_ci#include <linux/usb/ch11.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define TEST_SE0_NAK_PID			0x0101
148c2ecf20Sopenharmony_ci#define TEST_J_PID				0x0102
158c2ecf20Sopenharmony_ci#define TEST_K_PID				0x0103
168c2ecf20Sopenharmony_ci#define TEST_PACKET_PID				0x0104
178c2ecf20Sopenharmony_ci#define TEST_HS_HOST_PORT_SUSPEND_RESUME	0x0106
188c2ecf20Sopenharmony_ci#define TEST_SINGLE_STEP_GET_DEV_DESC		0x0107
198c2ecf20Sopenharmony_ci#define TEST_SINGLE_STEP_SET_FEATURE		0x0108
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic int ehset_probe(struct usb_interface *intf,
228c2ecf20Sopenharmony_ci		       const struct usb_device_id *id)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	int ret = -EINVAL;
258c2ecf20Sopenharmony_ci	struct usb_device *dev = interface_to_usbdev(intf);
268c2ecf20Sopenharmony_ci	struct usb_device *hub_udev = dev->parent;
278c2ecf20Sopenharmony_ci	struct usb_device_descriptor *buf;
288c2ecf20Sopenharmony_ci	u8 portnum = dev->portnum;
298c2ecf20Sopenharmony_ci	u16 test_pid = le16_to_cpu(dev->descriptor.idProduct);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	switch (test_pid) {
328c2ecf20Sopenharmony_ci	case TEST_SE0_NAK_PID:
338c2ecf20Sopenharmony_ci		ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
348c2ecf20Sopenharmony_ci					USB_REQ_SET_FEATURE, USB_RT_PORT,
358c2ecf20Sopenharmony_ci					USB_PORT_FEAT_TEST,
368c2ecf20Sopenharmony_ci					(USB_TEST_SE0_NAK << 8) | portnum,
378c2ecf20Sopenharmony_ci					NULL, 0, 1000);
388c2ecf20Sopenharmony_ci		break;
398c2ecf20Sopenharmony_ci	case TEST_J_PID:
408c2ecf20Sopenharmony_ci		ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
418c2ecf20Sopenharmony_ci					USB_REQ_SET_FEATURE, USB_RT_PORT,
428c2ecf20Sopenharmony_ci					USB_PORT_FEAT_TEST,
438c2ecf20Sopenharmony_ci					(USB_TEST_J << 8) | portnum,
448c2ecf20Sopenharmony_ci					NULL, 0, 1000);
458c2ecf20Sopenharmony_ci		break;
468c2ecf20Sopenharmony_ci	case TEST_K_PID:
478c2ecf20Sopenharmony_ci		ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
488c2ecf20Sopenharmony_ci					USB_REQ_SET_FEATURE, USB_RT_PORT,
498c2ecf20Sopenharmony_ci					USB_PORT_FEAT_TEST,
508c2ecf20Sopenharmony_ci					(USB_TEST_K << 8) | portnum,
518c2ecf20Sopenharmony_ci					NULL, 0, 1000);
528c2ecf20Sopenharmony_ci		break;
538c2ecf20Sopenharmony_ci	case TEST_PACKET_PID:
548c2ecf20Sopenharmony_ci		ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
558c2ecf20Sopenharmony_ci					USB_REQ_SET_FEATURE, USB_RT_PORT,
568c2ecf20Sopenharmony_ci					USB_PORT_FEAT_TEST,
578c2ecf20Sopenharmony_ci					(USB_TEST_PACKET << 8) | portnum,
588c2ecf20Sopenharmony_ci					NULL, 0, 1000);
598c2ecf20Sopenharmony_ci		break;
608c2ecf20Sopenharmony_ci	case TEST_HS_HOST_PORT_SUSPEND_RESUME:
618c2ecf20Sopenharmony_ci		/* Test: wait for 15secs -> suspend -> 15secs delay -> resume */
628c2ecf20Sopenharmony_ci		msleep(15 * 1000);
638c2ecf20Sopenharmony_ci		ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
648c2ecf20Sopenharmony_ci					USB_REQ_SET_FEATURE, USB_RT_PORT,
658c2ecf20Sopenharmony_ci					USB_PORT_FEAT_SUSPEND, portnum,
668c2ecf20Sopenharmony_ci					NULL, 0, 1000);
678c2ecf20Sopenharmony_ci		if (ret < 0)
688c2ecf20Sopenharmony_ci			break;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci		msleep(15 * 1000);
718c2ecf20Sopenharmony_ci		ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
728c2ecf20Sopenharmony_ci					USB_REQ_CLEAR_FEATURE, USB_RT_PORT,
738c2ecf20Sopenharmony_ci					USB_PORT_FEAT_SUSPEND, portnum,
748c2ecf20Sopenharmony_ci					NULL, 0, 1000);
758c2ecf20Sopenharmony_ci		break;
768c2ecf20Sopenharmony_ci	case TEST_SINGLE_STEP_GET_DEV_DESC:
778c2ecf20Sopenharmony_ci		/* Test: wait for 15secs -> GetDescriptor request */
788c2ecf20Sopenharmony_ci		msleep(15 * 1000);
798c2ecf20Sopenharmony_ci		buf = kmalloc(USB_DT_DEVICE_SIZE, GFP_KERNEL);
808c2ecf20Sopenharmony_ci		if (!buf)
818c2ecf20Sopenharmony_ci			return -ENOMEM;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci		ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
848c2ecf20Sopenharmony_ci					USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
858c2ecf20Sopenharmony_ci					USB_DT_DEVICE << 8, 0,
868c2ecf20Sopenharmony_ci					buf, USB_DT_DEVICE_SIZE,
878c2ecf20Sopenharmony_ci					USB_CTRL_GET_TIMEOUT);
888c2ecf20Sopenharmony_ci		kfree(buf);
898c2ecf20Sopenharmony_ci		break;
908c2ecf20Sopenharmony_ci	case TEST_SINGLE_STEP_SET_FEATURE:
918c2ecf20Sopenharmony_ci		/*
928c2ecf20Sopenharmony_ci		 * GetDescriptor SETUP request -> 15secs delay -> IN & STATUS
938c2ecf20Sopenharmony_ci		 *
948c2ecf20Sopenharmony_ci		 * Note, this test is only supported on root hubs since the
958c2ecf20Sopenharmony_ci		 * SetPortFeature handling can only be done inside the HCD's
968c2ecf20Sopenharmony_ci		 * hub_control callback function.
978c2ecf20Sopenharmony_ci		 */
988c2ecf20Sopenharmony_ci		if (hub_udev != dev->bus->root_hub) {
998c2ecf20Sopenharmony_ci			dev_err(&intf->dev, "SINGLE_STEP_SET_FEATURE test only supported on root hub\n");
1008c2ecf20Sopenharmony_ci			break;
1018c2ecf20Sopenharmony_ci		}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci		ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
1048c2ecf20Sopenharmony_ci					USB_REQ_SET_FEATURE, USB_RT_PORT,
1058c2ecf20Sopenharmony_ci					USB_PORT_FEAT_TEST,
1068c2ecf20Sopenharmony_ci					(6 << 8) | portnum,
1078c2ecf20Sopenharmony_ci					NULL, 0, 60 * 1000);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		break;
1108c2ecf20Sopenharmony_ci	default:
1118c2ecf20Sopenharmony_ci		dev_err(&intf->dev, "%s: unsupported PID: 0x%x\n",
1128c2ecf20Sopenharmony_ci			__func__, test_pid);
1138c2ecf20Sopenharmony_ci	}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	return (ret < 0) ? ret : 0;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic void ehset_disconnect(struct usb_interface *intf)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic const struct usb_device_id ehset_id_table[] = {
1238c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1a0a, TEST_SE0_NAK_PID) },
1248c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1a0a, TEST_J_PID) },
1258c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1a0a, TEST_K_PID) },
1268c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1a0a, TEST_PACKET_PID) },
1278c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1a0a, TEST_HS_HOST_PORT_SUSPEND_RESUME) },
1288c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1a0a, TEST_SINGLE_STEP_GET_DEV_DESC) },
1298c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1a0a, TEST_SINGLE_STEP_SET_FEATURE) },
1308c2ecf20Sopenharmony_ci	{ }			/* Terminating entry */
1318c2ecf20Sopenharmony_ci};
1328c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, ehset_id_table);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic struct usb_driver ehset_driver = {
1358c2ecf20Sopenharmony_ci	.name =		"usb_ehset_test",
1368c2ecf20Sopenharmony_ci	.probe =	ehset_probe,
1378c2ecf20Sopenharmony_ci	.disconnect =	ehset_disconnect,
1388c2ecf20Sopenharmony_ci	.id_table =	ehset_id_table,
1398c2ecf20Sopenharmony_ci};
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cimodule_usb_driver(ehset_driver);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("USB Driver for EHSET Test Fixture");
1448c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
145