ehset.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/errno.h>
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/usb.h>
  10. #include <linux/usb/ch11.h>
  11. #define TEST_SE0_NAK_PID 0x0101
  12. #define TEST_J_PID 0x0102
  13. #define TEST_K_PID 0x0103
  14. #define TEST_PACKET_PID 0x0104
  15. #define TEST_HS_HOST_PORT_SUSPEND_RESUME 0x0106
  16. #define TEST_SINGLE_STEP_GET_DEV_DESC 0x0107
  17. #define TEST_SINGLE_STEP_SET_FEATURE 0x0108
  18. extern const struct usb_device_id *usb_device_match_id(struct usb_device *udev,
  19. const struct usb_device_id *id);
  20. /*
  21. * A list of USB hubs which requires to disable the power
  22. * to the port before starting the testing procedures.
  23. */
  24. static const struct usb_device_id ehset_hub_list[] = {
  25. { USB_DEVICE(0x0424, 0x4502) },
  26. { USB_DEVICE(0x0424, 0x4913) },
  27. { USB_DEVICE(0x0451, 0x8027) },
  28. { }
  29. };
  30. static int ehset_prepare_port_for_testing(struct usb_device *hub_udev, u16 portnum)
  31. {
  32. int ret = 0;
  33. /*
  34. * The USB2.0 spec chapter 11.24.2.13 says that the USB port which is
  35. * going under test needs to be put in suspend before sending the
  36. * test command. Most hubs don't enforce this precondition, but there
  37. * are some hubs which needs to disable the power to the port before
  38. * starting the test.
  39. */
  40. if (usb_device_match_id(hub_udev, ehset_hub_list)) {
  41. ret = usb_control_msg_send(hub_udev, 0, USB_REQ_CLEAR_FEATURE,
  42. USB_RT_PORT, USB_PORT_FEAT_ENABLE,
  43. portnum, NULL, 0, 1000, GFP_KERNEL);
  44. /*
  45. * Wait for the port to be disabled. It's an arbitrary value
  46. * which worked every time.
  47. */
  48. msleep(100);
  49. } else {
  50. /*
  51. * For the hubs which are compliant with the spec,
  52. * put the port in SUSPEND.
  53. */
  54. ret = usb_control_msg_send(hub_udev, 0, USB_REQ_SET_FEATURE,
  55. USB_RT_PORT, USB_PORT_FEAT_SUSPEND,
  56. portnum, NULL, 0, 1000, GFP_KERNEL);
  57. }
  58. return ret;
  59. }
  60. static int ehset_probe(struct usb_interface *intf,
  61. const struct usb_device_id *id)
  62. {
  63. int ret = -EINVAL;
  64. struct usb_device *dev = interface_to_usbdev(intf);
  65. struct usb_device *hub_udev = dev->parent;
  66. struct usb_device_descriptor buf;
  67. u8 portnum = dev->portnum;
  68. u16 test_pid = le16_to_cpu(dev->descriptor.idProduct);
  69. switch (test_pid) {
  70. case TEST_SE0_NAK_PID:
  71. ret = ehset_prepare_port_for_testing(hub_udev, portnum);
  72. if (ret < 0)
  73. break;
  74. ret = usb_control_msg_send(hub_udev, 0, USB_REQ_SET_FEATURE,
  75. USB_RT_PORT, USB_PORT_FEAT_TEST,
  76. (USB_TEST_SE0_NAK << 8) | portnum,
  77. NULL, 0, 1000, GFP_KERNEL);
  78. break;
  79. case TEST_J_PID:
  80. ret = ehset_prepare_port_for_testing(hub_udev, portnum);
  81. if (ret < 0)
  82. break;
  83. ret = usb_control_msg_send(hub_udev, 0, USB_REQ_SET_FEATURE,
  84. USB_RT_PORT, USB_PORT_FEAT_TEST,
  85. (USB_TEST_J << 8) | portnum, NULL, 0,
  86. 1000, GFP_KERNEL);
  87. break;
  88. case TEST_K_PID:
  89. ret = ehset_prepare_port_for_testing(hub_udev, portnum);
  90. if (ret < 0)
  91. break;
  92. ret = usb_control_msg_send(hub_udev, 0, USB_REQ_SET_FEATURE,
  93. USB_RT_PORT, USB_PORT_FEAT_TEST,
  94. (USB_TEST_K << 8) | portnum, NULL, 0,
  95. 1000, GFP_KERNEL);
  96. break;
  97. case TEST_PACKET_PID:
  98. ret = ehset_prepare_port_for_testing(hub_udev, portnum);
  99. if (ret < 0)
  100. break;
  101. ret = usb_control_msg_send(hub_udev, 0, USB_REQ_SET_FEATURE,
  102. USB_RT_PORT, USB_PORT_FEAT_TEST,
  103. (USB_TEST_PACKET << 8) | portnum,
  104. NULL, 0, 1000, GFP_KERNEL);
  105. break;
  106. case TEST_HS_HOST_PORT_SUSPEND_RESUME:
  107. /* Test: wait for 15secs -> suspend -> 15secs delay -> resume */
  108. msleep(15 * 1000);
  109. ret = usb_control_msg_send(hub_udev, 0, USB_REQ_SET_FEATURE,
  110. USB_RT_PORT, USB_PORT_FEAT_SUSPEND,
  111. portnum, NULL, 0, 1000, GFP_KERNEL);
  112. if (ret < 0)
  113. break;
  114. msleep(15 * 1000);
  115. ret = usb_control_msg_send(hub_udev, 0, USB_REQ_CLEAR_FEATURE,
  116. USB_RT_PORT, USB_PORT_FEAT_SUSPEND,
  117. portnum, NULL, 0, 1000, GFP_KERNEL);
  118. break;
  119. case TEST_SINGLE_STEP_GET_DEV_DESC:
  120. /* Test: wait for 15secs -> GetDescriptor request */
  121. msleep(15 * 1000);
  122. ret = usb_control_msg_recv(dev, 0, USB_REQ_GET_DESCRIPTOR,
  123. USB_DIR_IN, USB_DT_DEVICE << 8, 0,
  124. &buf, USB_DT_DEVICE_SIZE,
  125. USB_CTRL_GET_TIMEOUT, GFP_KERNEL);
  126. break;
  127. case TEST_SINGLE_STEP_SET_FEATURE:
  128. /*
  129. * GetDescriptor SETUP request -> 15secs delay -> IN & STATUS
  130. *
  131. * Note, this test is only supported on root hubs since the
  132. * SetPortFeature handling can only be done inside the HCD's
  133. * hub_control callback function.
  134. */
  135. if (hub_udev != dev->bus->root_hub) {
  136. dev_err(&intf->dev, "SINGLE_STEP_SET_FEATURE test only supported on root hub\n");
  137. break;
  138. }
  139. ret = usb_control_msg_send(hub_udev, 0, USB_REQ_SET_FEATURE,
  140. USB_RT_PORT, USB_PORT_FEAT_TEST,
  141. (6 << 8) | portnum, NULL, 0,
  142. 60 * 1000, GFP_KERNEL);
  143. break;
  144. default:
  145. dev_err(&intf->dev, "%s: unsupported PID: 0x%x\n",
  146. __func__, test_pid);
  147. }
  148. return ret;
  149. }
  150. static void ehset_disconnect(struct usb_interface *intf)
  151. {
  152. }
  153. static const struct usb_device_id ehset_id_table[] = {
  154. { USB_DEVICE(0x1a0a, TEST_SE0_NAK_PID) },
  155. { USB_DEVICE(0x1a0a, TEST_J_PID) },
  156. { USB_DEVICE(0x1a0a, TEST_K_PID) },
  157. { USB_DEVICE(0x1a0a, TEST_PACKET_PID) },
  158. { USB_DEVICE(0x1a0a, TEST_HS_HOST_PORT_SUSPEND_RESUME) },
  159. { USB_DEVICE(0x1a0a, TEST_SINGLE_STEP_GET_DEV_DESC) },
  160. { USB_DEVICE(0x1a0a, TEST_SINGLE_STEP_SET_FEATURE) },
  161. { } /* Terminating entry */
  162. };
  163. MODULE_DEVICE_TABLE(usb, ehset_id_table);
  164. static struct usb_driver ehset_driver = {
  165. .name = "usb_ehset_test",
  166. .probe = ehset_probe,
  167. .disconnect = ehset_disconnect,
  168. .id_table = ehset_id_table,
  169. };
  170. module_usb_driver(ehset_driver);
  171. MODULE_DESCRIPTION("USB Driver for EHSET Test Fixture");
  172. MODULE_LICENSE("GPL v2");