ehset.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. static int ehset_probe(struct usb_interface *intf,
  19. const struct usb_device_id *id)
  20. {
  21. int ret = -EINVAL;
  22. struct usb_device *dev = interface_to_usbdev(intf);
  23. struct usb_device *hub_udev = dev->parent;
  24. struct usb_device_descriptor *buf;
  25. u8 portnum = dev->portnum;
  26. u16 test_pid = le16_to_cpu(dev->descriptor.idProduct);
  27. switch (test_pid) {
  28. case TEST_SE0_NAK_PID:
  29. ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
  30. USB_REQ_SET_FEATURE, USB_RT_PORT,
  31. USB_PORT_FEAT_TEST,
  32. (TEST_SE0_NAK << 8) | portnum,
  33. NULL, 0, 1000);
  34. break;
  35. case TEST_J_PID:
  36. ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
  37. USB_REQ_SET_FEATURE, USB_RT_PORT,
  38. USB_PORT_FEAT_TEST,
  39. (TEST_J << 8) | portnum,
  40. NULL, 0, 1000);
  41. break;
  42. case TEST_K_PID:
  43. ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
  44. USB_REQ_SET_FEATURE, USB_RT_PORT,
  45. USB_PORT_FEAT_TEST,
  46. (TEST_K << 8) | portnum,
  47. NULL, 0, 1000);
  48. break;
  49. case TEST_PACKET_PID:
  50. ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
  51. USB_REQ_SET_FEATURE, USB_RT_PORT,
  52. USB_PORT_FEAT_TEST,
  53. (TEST_PACKET << 8) | portnum,
  54. NULL, 0, 1000);
  55. break;
  56. case TEST_HS_HOST_PORT_SUSPEND_RESUME:
  57. /* Test: wait for 15secs -> suspend -> 15secs delay -> resume */
  58. msleep(15 * 1000);
  59. ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
  60. USB_REQ_SET_FEATURE, USB_RT_PORT,
  61. USB_PORT_FEAT_SUSPEND, portnum,
  62. NULL, 0, 1000);
  63. if (ret < 0)
  64. break;
  65. msleep(15 * 1000);
  66. ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
  67. USB_REQ_CLEAR_FEATURE, USB_RT_PORT,
  68. USB_PORT_FEAT_SUSPEND, portnum,
  69. NULL, 0, 1000);
  70. break;
  71. case TEST_SINGLE_STEP_GET_DEV_DESC:
  72. /* Test: wait for 15secs -> GetDescriptor request */
  73. msleep(15 * 1000);
  74. buf = kmalloc(USB_DT_DEVICE_SIZE, GFP_KERNEL);
  75. if (!buf)
  76. return -ENOMEM;
  77. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  78. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  79. USB_DT_DEVICE << 8, 0,
  80. buf, USB_DT_DEVICE_SIZE,
  81. USB_CTRL_GET_TIMEOUT);
  82. kfree(buf);
  83. break;
  84. case TEST_SINGLE_STEP_SET_FEATURE:
  85. /*
  86. * GetDescriptor SETUP request -> 15secs delay -> IN & STATUS
  87. *
  88. * Note, this test is only supported on root hubs since the
  89. * SetPortFeature handling can only be done inside the HCD's
  90. * hub_control callback function.
  91. */
  92. if (hub_udev != dev->bus->root_hub) {
  93. dev_err(&intf->dev, "SINGLE_STEP_SET_FEATURE test only supported on root hub\n");
  94. break;
  95. }
  96. ret = usb_control_msg(hub_udev, usb_sndctrlpipe(hub_udev, 0),
  97. USB_REQ_SET_FEATURE, USB_RT_PORT,
  98. USB_PORT_FEAT_TEST,
  99. (6 << 8) | portnum,
  100. NULL, 0, 60 * 1000);
  101. break;
  102. default:
  103. dev_err(&intf->dev, "%s: unsupported PID: 0x%x\n",
  104. __func__, test_pid);
  105. }
  106. return (ret < 0) ? ret : 0;
  107. }
  108. static void ehset_disconnect(struct usb_interface *intf)
  109. {
  110. }
  111. static const struct usb_device_id ehset_id_table[] = {
  112. { USB_DEVICE(0x1a0a, TEST_SE0_NAK_PID) },
  113. { USB_DEVICE(0x1a0a, TEST_J_PID) },
  114. { USB_DEVICE(0x1a0a, TEST_K_PID) },
  115. { USB_DEVICE(0x1a0a, TEST_PACKET_PID) },
  116. { USB_DEVICE(0x1a0a, TEST_HS_HOST_PORT_SUSPEND_RESUME) },
  117. { USB_DEVICE(0x1a0a, TEST_SINGLE_STEP_GET_DEV_DESC) },
  118. { USB_DEVICE(0x1a0a, TEST_SINGLE_STEP_SET_FEATURE) },
  119. { } /* Terminating entry */
  120. };
  121. MODULE_DEVICE_TABLE(usb, ehset_id_table);
  122. static struct usb_driver ehset_driver = {
  123. .name = "usb_ehset_test",
  124. .probe = ehset_probe,
  125. .disconnect = ehset_disconnect,
  126. .id_table = ehset_id_table,
  127. };
  128. module_usb_driver(ehset_driver);
  129. MODULE_DESCRIPTION("USB Driver for EHSET Test Fixture");
  130. MODULE_LICENSE("GPL v2");