lvstest.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * drivers/usb/misc/lvstest.c
  3. *
  4. * Test pattern generation for Link Layer Validation System Tests
  5. *
  6. * Copyright (C) 2014 ST Microelectronics
  7. * Pratyush Anand <pratyush.anand@gmail.com>
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/usb.h>
  19. #include <linux/usb/ch11.h>
  20. #include <linux/usb/hcd.h>
  21. #include <linux/usb/phy.h>
  22. struct lvs_rh {
  23. /* root hub interface */
  24. struct usb_interface *intf;
  25. /* if lvs device connected */
  26. bool present;
  27. /* port no at which lvs device is present */
  28. int portnum;
  29. /* urb buffer */
  30. u8 buffer[8];
  31. /* class descriptor */
  32. struct usb_hub_descriptor descriptor;
  33. /* urb for polling interrupt pipe */
  34. struct urb *urb;
  35. /* LVH RH work */
  36. struct work_struct rh_work;
  37. /* RH port status */
  38. struct usb_port_status port_status;
  39. };
  40. static struct usb_device *create_lvs_device(struct usb_interface *intf)
  41. {
  42. struct usb_device *udev, *hdev;
  43. struct usb_hcd *hcd;
  44. struct lvs_rh *lvs = usb_get_intfdata(intf);
  45. if (!lvs->present) {
  46. dev_err(&intf->dev, "No LVS device is present\n");
  47. return NULL;
  48. }
  49. hdev = interface_to_usbdev(intf);
  50. hcd = bus_to_hcd(hdev->bus);
  51. udev = usb_alloc_dev(hdev, hdev->bus, lvs->portnum);
  52. if (!udev) {
  53. dev_err(&intf->dev, "Could not allocate lvs udev\n");
  54. return NULL;
  55. }
  56. udev->speed = USB_SPEED_SUPER;
  57. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
  58. usb_set_device_state(udev, USB_STATE_DEFAULT);
  59. if (hcd->driver->enable_device) {
  60. if (hcd->driver->enable_device(hcd, udev) < 0) {
  61. dev_err(&intf->dev, "Failed to enable\n");
  62. usb_put_dev(udev);
  63. return NULL;
  64. }
  65. }
  66. return udev;
  67. }
  68. static void destroy_lvs_device(struct usb_device *udev)
  69. {
  70. struct usb_device *hdev = udev->parent;
  71. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  72. if (hcd->driver->free_dev)
  73. hcd->driver->free_dev(hcd, udev);
  74. usb_put_dev(udev);
  75. }
  76. static int lvs_rh_clear_port_feature(struct usb_device *hdev,
  77. int port1, int feature)
  78. {
  79. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  80. USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
  81. NULL, 0, 1000);
  82. }
  83. static int lvs_rh_set_port_feature(struct usb_device *hdev,
  84. int port1, int feature)
  85. {
  86. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  87. USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
  88. NULL, 0, 1000);
  89. }
  90. static ssize_t u3_entry_store(struct device *dev,
  91. struct device_attribute *attr, const char *buf, size_t count)
  92. {
  93. struct usb_interface *intf = to_usb_interface(dev);
  94. struct usb_device *hdev = interface_to_usbdev(intf);
  95. struct lvs_rh *lvs = usb_get_intfdata(intf);
  96. struct usb_device *udev;
  97. int ret;
  98. udev = create_lvs_device(intf);
  99. if (!udev) {
  100. dev_err(dev, "failed to create lvs device\n");
  101. return -ENOMEM;
  102. }
  103. ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
  104. USB_PORT_FEAT_SUSPEND);
  105. if (ret < 0)
  106. dev_err(dev, "can't issue U3 entry %d\n", ret);
  107. destroy_lvs_device(udev);
  108. if (ret < 0)
  109. return ret;
  110. return count;
  111. }
  112. static DEVICE_ATTR_WO(u3_entry);
  113. static ssize_t u3_exit_store(struct device *dev,
  114. struct device_attribute *attr, const char *buf, size_t count)
  115. {
  116. struct usb_interface *intf = to_usb_interface(dev);
  117. struct usb_device *hdev = interface_to_usbdev(intf);
  118. struct lvs_rh *lvs = usb_get_intfdata(intf);
  119. struct usb_device *udev;
  120. int ret;
  121. udev = create_lvs_device(intf);
  122. if (!udev) {
  123. dev_err(dev, "failed to create lvs device\n");
  124. return -ENOMEM;
  125. }
  126. ret = lvs_rh_clear_port_feature(hdev, lvs->portnum,
  127. USB_PORT_FEAT_SUSPEND);
  128. if (ret < 0)
  129. dev_err(dev, "can't issue U3 exit %d\n", ret);
  130. destroy_lvs_device(udev);
  131. if (ret < 0)
  132. return ret;
  133. return count;
  134. }
  135. static DEVICE_ATTR_WO(u3_exit);
  136. static ssize_t hot_reset_store(struct device *dev,
  137. struct device_attribute *attr, const char *buf, size_t count)
  138. {
  139. struct usb_interface *intf = to_usb_interface(dev);
  140. struct usb_device *hdev = interface_to_usbdev(intf);
  141. struct lvs_rh *lvs = usb_get_intfdata(intf);
  142. int ret;
  143. ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
  144. USB_PORT_FEAT_RESET);
  145. if (ret < 0) {
  146. dev_err(dev, "can't issue hot reset %d\n", ret);
  147. return ret;
  148. }
  149. return count;
  150. }
  151. static DEVICE_ATTR_WO(hot_reset);
  152. static ssize_t warm_reset_store(struct device *dev,
  153. struct device_attribute *attr, const char *buf, size_t count)
  154. {
  155. struct usb_interface *intf = to_usb_interface(dev);
  156. struct usb_device *hdev = interface_to_usbdev(intf);
  157. struct lvs_rh *lvs = usb_get_intfdata(intf);
  158. int ret;
  159. ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
  160. USB_PORT_FEAT_BH_PORT_RESET);
  161. if (ret < 0) {
  162. dev_err(dev, "can't issue warm reset %d\n", ret);
  163. return ret;
  164. }
  165. return count;
  166. }
  167. static DEVICE_ATTR_WO(warm_reset);
  168. static ssize_t u2_timeout_store(struct device *dev,
  169. struct device_attribute *attr, const char *buf, size_t count)
  170. {
  171. struct usb_interface *intf = to_usb_interface(dev);
  172. struct usb_device *hdev = interface_to_usbdev(intf);
  173. struct lvs_rh *lvs = usb_get_intfdata(intf);
  174. unsigned long val;
  175. int ret;
  176. ret = kstrtoul(buf, 10, &val);
  177. if (ret < 0) {
  178. dev_err(dev, "couldn't parse string %d\n", ret);
  179. return ret;
  180. }
  181. if (val > 127)
  182. return -EINVAL;
  183. ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
  184. USB_PORT_FEAT_U2_TIMEOUT);
  185. if (ret < 0) {
  186. dev_err(dev, "Error %d while setting U2 timeout %ld\n", ret, val);
  187. return ret;
  188. }
  189. return count;
  190. }
  191. static DEVICE_ATTR_WO(u2_timeout);
  192. static ssize_t u1_timeout_store(struct device *dev,
  193. struct device_attribute *attr, const char *buf, size_t count)
  194. {
  195. struct usb_interface *intf = to_usb_interface(dev);
  196. struct usb_device *hdev = interface_to_usbdev(intf);
  197. struct lvs_rh *lvs = usb_get_intfdata(intf);
  198. unsigned long val;
  199. int ret;
  200. ret = kstrtoul(buf, 10, &val);
  201. if (ret < 0) {
  202. dev_err(dev, "couldn't parse string %d\n", ret);
  203. return ret;
  204. }
  205. if (val > 127)
  206. return -EINVAL;
  207. ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
  208. USB_PORT_FEAT_U1_TIMEOUT);
  209. if (ret < 0) {
  210. dev_err(dev, "Error %d while setting U1 timeout %ld\n", ret, val);
  211. return ret;
  212. }
  213. return count;
  214. }
  215. static DEVICE_ATTR_WO(u1_timeout);
  216. static ssize_t get_dev_desc_store(struct device *dev,
  217. struct device_attribute *attr, const char *buf, size_t count)
  218. {
  219. struct usb_interface *intf = to_usb_interface(dev);
  220. struct usb_device *udev;
  221. struct usb_device_descriptor *descriptor;
  222. int ret;
  223. descriptor = kmalloc(sizeof(*descriptor), GFP_KERNEL);
  224. if (!descriptor)
  225. return -ENOMEM;
  226. udev = create_lvs_device(intf);
  227. if (!udev) {
  228. dev_err(dev, "failed to create lvs device\n");
  229. ret = -ENOMEM;
  230. goto free_desc;
  231. }
  232. ret = usb_control_msg(udev, (PIPE_CONTROL << 30) | USB_DIR_IN,
  233. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, USB_DT_DEVICE << 8,
  234. 0, descriptor, sizeof(*descriptor),
  235. USB_CTRL_GET_TIMEOUT);
  236. if (ret < 0)
  237. dev_err(dev, "can't read device descriptor %d\n", ret);
  238. destroy_lvs_device(udev);
  239. free_desc:
  240. kfree(descriptor);
  241. if (ret < 0)
  242. return ret;
  243. return count;
  244. }
  245. static DEVICE_ATTR_WO(get_dev_desc);
  246. static ssize_t enable_compliance_store(struct device *dev,
  247. struct device_attribute *attr, const char *buf, size_t count)
  248. {
  249. struct usb_interface *intf = to_usb_interface(dev);
  250. struct usb_device *hdev = interface_to_usbdev(intf);
  251. struct lvs_rh *lvs = usb_get_intfdata(intf);
  252. int ret;
  253. ret = lvs_rh_set_port_feature(hdev,
  254. lvs->portnum | USB_SS_PORT_LS_COMP_MOD << 3,
  255. USB_PORT_FEAT_LINK_STATE);
  256. if (ret < 0) {
  257. dev_err(dev, "can't enable compliance mode %d\n", ret);
  258. return ret;
  259. }
  260. return count;
  261. }
  262. static DEVICE_ATTR_WO(enable_compliance);
  263. static struct attribute *lvs_attributes[] = {
  264. &dev_attr_get_dev_desc.attr,
  265. &dev_attr_u1_timeout.attr,
  266. &dev_attr_u2_timeout.attr,
  267. &dev_attr_hot_reset.attr,
  268. &dev_attr_warm_reset.attr,
  269. &dev_attr_u3_entry.attr,
  270. &dev_attr_u3_exit.attr,
  271. &dev_attr_enable_compliance.attr,
  272. NULL
  273. };
  274. static const struct attribute_group lvs_attr_group = {
  275. .attrs = lvs_attributes,
  276. };
  277. static void lvs_rh_work(struct work_struct *work)
  278. {
  279. struct lvs_rh *lvs = container_of(work, struct lvs_rh, rh_work);
  280. struct usb_interface *intf = lvs->intf;
  281. struct usb_device *hdev = interface_to_usbdev(intf);
  282. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  283. struct usb_hub_descriptor *descriptor = &lvs->descriptor;
  284. struct usb_port_status *port_status = &lvs->port_status;
  285. int i, ret = 0;
  286. u16 portchange;
  287. /* Examine each root port */
  288. for (i = 1; i <= descriptor->bNbrPorts; i++) {
  289. ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  290. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, i,
  291. port_status, sizeof(*port_status), 1000);
  292. if (ret < 4)
  293. continue;
  294. portchange = le16_to_cpu(port_status->wPortChange);
  295. if (portchange & USB_PORT_STAT_C_LINK_STATE)
  296. lvs_rh_clear_port_feature(hdev, i,
  297. USB_PORT_FEAT_C_PORT_LINK_STATE);
  298. if (portchange & USB_PORT_STAT_C_ENABLE)
  299. lvs_rh_clear_port_feature(hdev, i,
  300. USB_PORT_FEAT_C_ENABLE);
  301. if (portchange & USB_PORT_STAT_C_RESET)
  302. lvs_rh_clear_port_feature(hdev, i,
  303. USB_PORT_FEAT_C_RESET);
  304. if (portchange & USB_PORT_STAT_C_BH_RESET)
  305. lvs_rh_clear_port_feature(hdev, i,
  306. USB_PORT_FEAT_C_BH_PORT_RESET);
  307. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  308. lvs_rh_clear_port_feature(hdev, i,
  309. USB_PORT_FEAT_C_CONNECTION);
  310. if (le16_to_cpu(port_status->wPortStatus) &
  311. USB_PORT_STAT_CONNECTION) {
  312. lvs->present = true;
  313. lvs->portnum = i;
  314. if (hcd->usb_phy)
  315. usb_phy_notify_connect(hcd->usb_phy,
  316. USB_SPEED_SUPER);
  317. } else {
  318. lvs->present = false;
  319. if (hcd->usb_phy)
  320. usb_phy_notify_disconnect(hcd->usb_phy,
  321. USB_SPEED_SUPER);
  322. }
  323. break;
  324. }
  325. }
  326. ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
  327. if (ret != 0 && ret != -ENODEV && ret != -EPERM)
  328. dev_err(&intf->dev, "urb resubmit error %d\n", ret);
  329. }
  330. static void lvs_rh_irq(struct urb *urb)
  331. {
  332. struct lvs_rh *lvs = urb->context;
  333. schedule_work(&lvs->rh_work);
  334. }
  335. static int lvs_rh_probe(struct usb_interface *intf,
  336. const struct usb_device_id *id)
  337. {
  338. struct usb_device *hdev;
  339. struct usb_host_interface *desc;
  340. struct usb_endpoint_descriptor *endpoint;
  341. struct lvs_rh *lvs;
  342. unsigned int pipe;
  343. int ret, maxp;
  344. hdev = interface_to_usbdev(intf);
  345. desc = intf->cur_altsetting;
  346. ret = usb_find_int_in_endpoint(desc, &endpoint);
  347. if (ret)
  348. return ret;
  349. /* valid only for SS root hub */
  350. if (hdev->descriptor.bDeviceProtocol != USB_HUB_PR_SS || hdev->parent) {
  351. dev_err(&intf->dev, "Bind LVS driver with SS root Hub only\n");
  352. return -EINVAL;
  353. }
  354. lvs = devm_kzalloc(&intf->dev, sizeof(*lvs), GFP_KERNEL);
  355. if (!lvs)
  356. return -ENOMEM;
  357. lvs->intf = intf;
  358. usb_set_intfdata(intf, lvs);
  359. /* how many number of ports this root hub has */
  360. ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  361. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
  362. USB_DT_SS_HUB << 8, 0, &lvs->descriptor,
  363. USB_DT_SS_HUB_SIZE, USB_CTRL_GET_TIMEOUT);
  364. if (ret < (USB_DT_HUB_NONVAR_SIZE + 2)) {
  365. dev_err(&hdev->dev, "wrong root hub descriptor read %d\n", ret);
  366. return ret;
  367. }
  368. /* submit urb to poll interrupt endpoint */
  369. lvs->urb = usb_alloc_urb(0, GFP_KERNEL);
  370. if (!lvs->urb)
  371. return -ENOMEM;
  372. INIT_WORK(&lvs->rh_work, lvs_rh_work);
  373. ret = sysfs_create_group(&intf->dev.kobj, &lvs_attr_group);
  374. if (ret < 0) {
  375. dev_err(&intf->dev, "Failed to create sysfs node %d\n", ret);
  376. goto free_urb;
  377. }
  378. pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
  379. maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
  380. usb_fill_int_urb(lvs->urb, hdev, pipe, &lvs->buffer[0], maxp,
  381. lvs_rh_irq, lvs, endpoint->bInterval);
  382. ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
  383. if (ret < 0) {
  384. dev_err(&intf->dev, "couldn't submit lvs urb %d\n", ret);
  385. goto sysfs_remove;
  386. }
  387. return ret;
  388. sysfs_remove:
  389. sysfs_remove_group(&intf->dev.kobj, &lvs_attr_group);
  390. free_urb:
  391. usb_free_urb(lvs->urb);
  392. return ret;
  393. }
  394. static void lvs_rh_disconnect(struct usb_interface *intf)
  395. {
  396. struct lvs_rh *lvs = usb_get_intfdata(intf);
  397. sysfs_remove_group(&intf->dev.kobj, &lvs_attr_group);
  398. usb_poison_urb(lvs->urb); /* used in scheduled work */
  399. flush_work(&lvs->rh_work);
  400. usb_free_urb(lvs->urb);
  401. }
  402. static struct usb_driver lvs_driver = {
  403. .name = "lvs",
  404. .probe = lvs_rh_probe,
  405. .disconnect = lvs_rh_disconnect,
  406. };
  407. module_usb_driver(lvs_driver);
  408. MODULE_DESCRIPTION("Link Layer Validation System Driver");
  409. MODULE_LICENSE("GPL");