1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved. 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 and 7 * only version 2 as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 */ 14 15 #include <linux/kernel.h> 16 #include <linux/errno.h> 17 #include <linux/module.h> 18 #include <linux/slab.h> 19 #include <linux/usb.h> 20 #include <linux/usb/ch11.h> 21 22 #define TEST_SE0_NAK_PID 0x0101 23 #define TEST_J_PID 0x0102 24 #define TEST_K_PID 0x0103 25 #define TEST_PACKET_PID 0x0104 26 #define TEST_HS_HOST_PORT_SUSPEND_RESUME 0x0106 27 #define TEST_SINGLE_STEP_GET_DEV_DESC 0x0107 28 #define TEST_SINGLE_STEP_SET_FEATURE 0x0108 29 30 static int ehset_probe(struct usb_interface *intf, 31 const struct usb_device_id *id) 32 { 33 int ret = -EINVAL; 34 struct usb_device *dev = interface_to_usbdev(intf); 35 struct usb_device *hub_udev = dev->parent; 36 struct usb_device_descriptor *buf; 37 u8 portnum = dev->portnum; 38 u16 test_pid = le16_to_cpu(dev->descriptor.idProduct); 39 40 switch (test_pid) { 41 case TEST_SE0_NAK_PID: 42 ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0), 43 USB_REQ_SET_FEATURE, USB_RT_PORT, 44 USB_PORT_FEAT_TEST, 45 (TEST_SE0_NAK << 8) | portnum, 46 NULL, 0, 1000); 47 break; 48 case TEST_J_PID: 49 ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0), 50 USB_REQ_SET_FEATURE, USB_RT_PORT, 51 USB_PORT_FEAT_TEST, 52 (TEST_J << 8) | portnum, 53 NULL, 0, 1000); 54 break; 55 case TEST_K_PID: 56 ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0), 57 USB_REQ_SET_FEATURE, USB_RT_PORT, 58 USB_PORT_FEAT_TEST, 59 (TEST_K << 8) | portnum, 60 NULL, 0, 1000); 61 break; 62 case TEST_PACKET_PID: 63 ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0), 64 USB_REQ_SET_FEATURE, USB_RT_PORT, 65 USB_PORT_FEAT_TEST, 66 (TEST_PACKET << 8) | portnum, 67 NULL, 0, 1000); 68 break; 69 case TEST_HS_HOST_PORT_SUSPEND_RESUME: 70 /* Test: wait for 15secs -> suspend -> 15secs delay -> resume */ 71 msleep(15 * 1000); 72 ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0), 73 USB_REQ_SET_FEATURE, USB_RT_PORT, 74 USB_PORT_FEAT_SUSPEND, portnum, 75 NULL, 0, 1000); 76 if (ret < 0) 77 break; 78 79 msleep(15 * 1000); 80 ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0), 81 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, 82 USB_PORT_FEAT_SUSPEND, portnum, 83 NULL, 0, 1000); 84 break; 85 case TEST_SINGLE_STEP_GET_DEV_DESC: 86 /* Test: wait for 15secs -> GetDescriptor request */ 87 msleep(15 * 1000); 88 buf = kmalloc(USB_DT_DEVICE_SIZE, GFP_KERNEL); 89 if (!buf) 90 return -ENOMEM; 91 92 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 93 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, 94 USB_DT_DEVICE << 8, 0, 95 buf, USB_DT_DEVICE_SIZE, 96 USB_CTRL_GET_TIMEOUT); 97 kfree(buf); 98 break; 99 case TEST_SINGLE_STEP_SET_FEATURE: 100 /* 101 * GetDescriptor SETUP request -> 15secs delay -> IN & STATUS 102 * 103 * Note, this test is only supported on root hubs since the 104 * SetPortFeature handling can only be done inside the HCD's 105 * hub_control callback function. 106 */ 107 if (hub_udev != dev->bus->root_hub) { 108 dev_err(&intf->dev, "SINGLE_STEP_SET_FEATURE test only supported on root hub\n"); 109 break; 110 } 111 112 ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0), 113 USB_REQ_SET_FEATURE, USB_RT_PORT, 114 USB_PORT_FEAT_TEST, 115 (6 << 8) | portnum, 116 NULL, 0, 60 * 1000); 117 118 break; 119 default: 120 dev_err(&intf->dev, "%s: unsupported PID: 0x%x\n", 121 __func__, test_pid); 122 } 123 124 return (ret < 0) ? ret : 0; 125 } 126 127 static void ehset_disconnect(struct usb_interface *intf) 128 { 129 } 130 131 static const struct usb_device_id ehset_id_table[] = { 132 { USB_DEVICE(0x1a0a, TEST_SE0_NAK_PID) }, 133 { USB_DEVICE(0x1a0a, TEST_J_PID) }, 134 { USB_DEVICE(0x1a0a, TEST_K_PID) }, 135 { USB_DEVICE(0x1a0a, TEST_PACKET_PID) }, 136 { USB_DEVICE(0x1a0a, TEST_HS_HOST_PORT_SUSPEND_RESUME) }, 137 { USB_DEVICE(0x1a0a, TEST_SINGLE_STEP_GET_DEV_DESC) }, 138 { USB_DEVICE(0x1a0a, TEST_SINGLE_STEP_SET_FEATURE) }, 139 { } /* Terminating entry */ 140 }; 141 MODULE_DEVICE_TABLE(usb, ehset_id_table); 142 143 static struct usb_driver ehset_driver = { 144 .name = "usb_ehset_test", 145 .probe = ehset_probe, 146 .disconnect = ehset_disconnect, 147 .id_table = ehset_id_table, 148 }; 149 150 module_usb_driver(ehset_driver); 151 152 MODULE_DESCRIPTION("USB Driver for EHSET Test Fixture"); 153 MODULE_LICENSE("GPL v2"); 154