lvstest.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/usb/misc/lvstest.c
  4. *
  5. * Test pattern generation for Link Layer Validation System Tests
  6. *
  7. * Copyright (C) 2014 ST Microelectronics
  8. * Pratyush Anand <[email protected]>
  9. */
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/usb.h>
  16. #include <linux/usb/ch11.h>
  17. #include <linux/usb/hcd.h>
  18. #include <linux/usb/phy.h>
  19. #if IS_ENABLED(CONFIG_IF_CB_MANAGER)
  20. #include <linux/usb/typec/manager/if_cb_manager.h>
  21. #endif
  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. int test_mode;
  40. int test_stat;
  41. #if IS_ENABLED(CONFIG_IF_CB_MANAGER)
  42. struct lvs_dev lvs_d;
  43. struct if_cb_manager *man;
  44. #endif
  45. };
  46. enum lvs_status {
  47. STAT_DISC = 0,
  48. STAT_CON,
  49. STAT_RESET
  50. };
  51. static int lvs_rh_set_port_feature(struct usb_device *hdev,
  52. int port1, int feature)
  53. {
  54. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  55. USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
  56. NULL, 0, 1000);
  57. }
  58. static int send_hot_reset(struct usb_interface *intf)
  59. {
  60. struct usb_device *hdev = interface_to_usbdev(intf);
  61. struct lvs_rh *lvs = usb_get_intfdata(intf);
  62. int ret;
  63. pr_info("lvs:%s\n", __func__);
  64. ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
  65. USB_PORT_FEAT_RESET);
  66. if (ret < 0)
  67. pr_err("lvs:can't issue hot reset %d\n", ret);
  68. return ret;
  69. }
  70. static void prepare_speed_info(struct usb_device *udev, int speed)
  71. {
  72. switch (speed) {
  73. case USB_SPEED_SUPER:
  74. pr_info("lvs:%s:super speed\n", __func__);
  75. udev->speed = USB_SPEED_SUPER;
  76. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
  77. break;
  78. case USB_SPEED_HIGH:
  79. pr_info("lvs:%s: high speed\n", __func__);
  80. udev->speed = USB_SPEED_HIGH;
  81. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  82. break;
  83. case USB_SPEED_FULL:
  84. pr_info("lvs:%s:full speed\n", __func__);
  85. udev->speed = USB_SPEED_FULL;
  86. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  87. break;
  88. case USB_SPEED_LOW:
  89. pr_info("lvs:%s:low speed\n", __func__);
  90. udev->speed = USB_SPEED_LOW;
  91. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
  92. break;
  93. default:
  94. pr_info("lvs:%s:default super speed\n", __func__);
  95. udev->speed = USB_SPEED_SUPER;
  96. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
  97. break;
  98. }
  99. }
  100. static struct usb_device *create_lvs_device(struct usb_interface *intf)
  101. {
  102. struct usb_device *udev, *hdev;
  103. struct usb_hcd *hcd;
  104. struct lvs_rh *lvs = usb_get_intfdata(intf);
  105. struct usb_port_status *port_status = &lvs->port_status;
  106. u16 portchange;
  107. u16 portstatus;
  108. int ret;
  109. pr_info("%s+\n", __func__);
  110. if (lvs->test_mode)
  111. pr_info("%s test_mode:%d\n", __func__, lvs->test_mode);
  112. if (!lvs->present) {
  113. dev_err(&intf->dev, "No LVS device is present\n");
  114. return NULL;
  115. }
  116. hdev = interface_to_usbdev(intf);
  117. hcd = bus_to_hcd(hdev->bus);
  118. udev = usb_alloc_dev(hdev, hdev->bus, lvs->portnum);
  119. if (!udev) {
  120. dev_err(&intf->dev, "Could not allocate lvs udev\n");
  121. return NULL;
  122. }
  123. prepare_speed_info(udev, lvs->test_mode);
  124. if (lvs->test_mode) {
  125. if (lvs->test_stat == STAT_CON) {
  126. ret = send_hot_reset(intf);
  127. if (ret < 0)
  128. return NULL;
  129. lvs->test_stat = STAT_RESET;
  130. msleep(50);
  131. ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  132. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, lvs->portnum,
  133. port_status, sizeof(*port_status), 1000);
  134. if (ret >= 4) {
  135. portchange = le16_to_cpu(port_status->wPortChange);
  136. portstatus = le16_to_cpu(port_status->wPortStatus);
  137. dev_info(&intf->dev, "%s, after hotreset portchange=0x%x portstatus=0x%x\n",
  138. __func__, portchange, portstatus);
  139. if (lvs->test_mode == USB_SPEED_SUPER)
  140. udev->speed = USB_SPEED_SUPER;
  141. else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
  142. udev->speed = USB_SPEED_HIGH;
  143. else if (portstatus & USB_PORT_STAT_LOW_SPEED)
  144. udev->speed = USB_SPEED_LOW;
  145. else
  146. udev->speed = USB_SPEED_FULL;
  147. prepare_speed_info(udev, udev->speed);
  148. } else {
  149. dev_err(&intf->dev, "after hotreset get status error.\n");
  150. }
  151. }
  152. }
  153. /* add 100 to notify that it is called from the lvs driver. */
  154. usb_set_device_state(udev, USB_STATE_DEFAULT + 100);
  155. if (hcd->driver->enable_device) {
  156. if (hcd->driver->enable_device(hcd, udev) < 0) {
  157. dev_err(&intf->dev, "Failed to enable\n");
  158. if (hcd->driver->free_dev)
  159. hcd->driver->free_dev(hcd, udev);
  160. usb_put_dev(udev);
  161. return NULL;
  162. }
  163. }
  164. pr_info("%s-\n", __func__);
  165. return udev;
  166. }
  167. static void destroy_lvs_device(struct usb_device *udev)
  168. {
  169. struct usb_device *hdev = udev->parent;
  170. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  171. if (hcd->driver->free_dev)
  172. hcd->driver->free_dev(hcd, udev);
  173. usb_put_dev(udev);
  174. }
  175. static int lvs_rh_clear_port_feature(struct usb_device *hdev,
  176. int port1, int feature)
  177. {
  178. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  179. USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
  180. NULL, 0, 1000);
  181. }
  182. static ssize_t u3_entry_store(struct device *dev,
  183. struct device_attribute *attr, const char *buf, size_t count)
  184. {
  185. struct usb_interface *intf = to_usb_interface(dev);
  186. struct usb_device *hdev = interface_to_usbdev(intf);
  187. struct lvs_rh *lvs = usb_get_intfdata(intf);
  188. struct usb_device *udev;
  189. int port;
  190. int ret;
  191. if (!kstrtoint(buf, 0, &port) && port >= 1 && port <= 255) {
  192. lvs->portnum = port;
  193. lvs->present = true;
  194. }
  195. udev = create_lvs_device(intf);
  196. if (!udev) {
  197. dev_err(dev, "failed to create lvs device\n");
  198. return -ENOMEM;
  199. }
  200. ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
  201. USB_PORT_FEAT_SUSPEND);
  202. if (ret < 0)
  203. dev_err(dev, "can't issue U3 entry %d\n", ret);
  204. destroy_lvs_device(udev);
  205. if (ret < 0)
  206. return ret;
  207. return count;
  208. }
  209. static DEVICE_ATTR_WO(u3_entry);
  210. static ssize_t u3_exit_store(struct device *dev,
  211. struct device_attribute *attr, const char *buf, size_t count)
  212. {
  213. struct usb_interface *intf = to_usb_interface(dev);
  214. struct usb_device *hdev = interface_to_usbdev(intf);
  215. struct lvs_rh *lvs = usb_get_intfdata(intf);
  216. struct usb_device *udev;
  217. int port;
  218. int ret;
  219. if (!kstrtoint(buf, 0, &port) && port >= 1 && port <= 255) {
  220. lvs->portnum = port;
  221. lvs->present = true;
  222. }
  223. udev = create_lvs_device(intf);
  224. if (!udev) {
  225. dev_err(dev, "failed to create lvs device\n");
  226. return -ENOMEM;
  227. }
  228. ret = lvs_rh_clear_port_feature(hdev, lvs->portnum,
  229. USB_PORT_FEAT_SUSPEND);
  230. if (ret < 0)
  231. dev_err(dev, "can't issue U3 exit %d\n", ret);
  232. destroy_lvs_device(udev);
  233. if (ret < 0)
  234. return ret;
  235. return count;
  236. }
  237. static DEVICE_ATTR_WO(u3_exit);
  238. static ssize_t hot_reset_store(struct device *dev,
  239. struct device_attribute *attr, const char *buf, size_t count)
  240. {
  241. struct usb_interface *intf = to_usb_interface(dev);
  242. struct usb_device *hdev = interface_to_usbdev(intf);
  243. struct lvs_rh *lvs = usb_get_intfdata(intf);
  244. int port;
  245. int ret;
  246. pr_info("lvs:%s\n", __func__);
  247. if (kstrtoint(buf, 0, &port) || port < 1 || port > 255)
  248. port = lvs->portnum;
  249. ret = lvs_rh_set_port_feature(hdev, port,
  250. USB_PORT_FEAT_RESET);
  251. if (ret < 0) {
  252. dev_err(dev, "can't issue hot reset %d\n", ret);
  253. return ret;
  254. }
  255. return count;
  256. }
  257. static DEVICE_ATTR_WO(hot_reset);
  258. static ssize_t warm_reset_store(struct device *dev,
  259. struct device_attribute *attr, const char *buf, size_t count)
  260. {
  261. struct usb_interface *intf = to_usb_interface(dev);
  262. struct usb_device *hdev = interface_to_usbdev(intf);
  263. struct lvs_rh *lvs = usb_get_intfdata(intf);
  264. int port;
  265. int ret;
  266. pr_info("lvs:%s\n", __func__);
  267. if (kstrtoint(buf, 0, &port) || port < 1 || port > 255)
  268. port = lvs->portnum;
  269. ret = lvs_rh_set_port_feature(hdev, port, USB_PORT_FEAT_BH_PORT_RESET);
  270. if (ret < 0) {
  271. dev_err(dev, "can't issue warm reset %d\n", ret);
  272. return ret;
  273. }
  274. return count;
  275. }
  276. static DEVICE_ATTR_WO(warm_reset);
  277. static ssize_t u2_timeout_store(struct device *dev,
  278. struct device_attribute *attr, const char *buf, size_t count)
  279. {
  280. struct usb_interface *intf = to_usb_interface(dev);
  281. struct usb_device *hdev = interface_to_usbdev(intf);
  282. struct lvs_rh *lvs = usb_get_intfdata(intf);
  283. unsigned long val;
  284. int ret;
  285. ret = kstrtoul(buf, 10, &val);
  286. if (ret < 0) {
  287. dev_err(dev, "couldn't parse string %d\n", ret);
  288. return ret;
  289. }
  290. if (val > 127)
  291. return -EINVAL;
  292. ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
  293. USB_PORT_FEAT_U2_TIMEOUT);
  294. if (ret < 0) {
  295. dev_err(dev, "Error %d while setting U2 timeout %ld\n", ret, val);
  296. return ret;
  297. }
  298. return count;
  299. }
  300. static DEVICE_ATTR_WO(u2_timeout);
  301. static ssize_t u1_timeout_store(struct device *dev,
  302. struct device_attribute *attr, const char *buf, size_t count)
  303. {
  304. struct usb_interface *intf = to_usb_interface(dev);
  305. struct usb_device *hdev = interface_to_usbdev(intf);
  306. struct lvs_rh *lvs = usb_get_intfdata(intf);
  307. unsigned long val;
  308. int ret;
  309. ret = kstrtoul(buf, 10, &val);
  310. if (ret < 0) {
  311. dev_err(dev, "couldn't parse string %d\n", ret);
  312. return ret;
  313. }
  314. if (val > 127)
  315. return -EINVAL;
  316. ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
  317. USB_PORT_FEAT_U1_TIMEOUT);
  318. if (ret < 0) {
  319. dev_err(dev, "Error %d while setting U1 timeout %ld\n", ret, val);
  320. return ret;
  321. }
  322. return count;
  323. }
  324. static DEVICE_ATTR_WO(u1_timeout);
  325. static ssize_t get_dev_desc_store(struct device *dev,
  326. struct device_attribute *attr, const char *buf, size_t count)
  327. {
  328. struct usb_interface *intf = to_usb_interface(dev);
  329. struct lvs_rh *lvs = usb_get_intfdata(intf);
  330. struct usb_device *udev;
  331. struct usb_device_descriptor *descriptor;
  332. int ret, port;
  333. pr_info("lvs: %s\n", __func__);
  334. if (!kstrtoint(buf, 0, &port) && port >= 1 && port <= 255) {
  335. lvs->portnum = port;
  336. lvs->present = true;
  337. }
  338. descriptor = kmalloc(sizeof(*descriptor), GFP_KERNEL);
  339. if (!descriptor)
  340. return -ENOMEM;
  341. udev = create_lvs_device(intf);
  342. if (!udev) {
  343. dev_err(dev, "failed to create lvs device\n");
  344. ret = -ENOMEM;
  345. goto free_desc;
  346. }
  347. ret = usb_control_msg(udev, (PIPE_CONTROL << 30) | USB_DIR_IN,
  348. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, USB_DT_DEVICE << 8,
  349. 0, descriptor, sizeof(*descriptor),
  350. USB_CTRL_GET_TIMEOUT);
  351. if (ret < 0) {
  352. dev_err(dev, "can't read device descriptor %d\n", ret);
  353. if (lvs->test_mode)
  354. lvs->test_stat = STAT_CON;
  355. } else {
  356. #if IS_ENABLED(CONFIG_IF_CB_MANAGER)
  357. usbpd_wait_entermode(lvs->man, 0);
  358. #endif
  359. dev_info(dev, "send device descriptor success\n");
  360. }
  361. destroy_lvs_device(udev);
  362. free_desc:
  363. kfree(descriptor);
  364. if (ret < 0)
  365. return ret;
  366. return count;
  367. }
  368. static DEVICE_ATTR_WO(get_dev_desc);
  369. static ssize_t enable_compliance_store(struct device *dev,
  370. struct device_attribute *attr, const char *buf, size_t count)
  371. {
  372. struct usb_interface *intf = to_usb_interface(dev);
  373. struct usb_device *hdev = interface_to_usbdev(intf);
  374. struct lvs_rh *lvs = usb_get_intfdata(intf);
  375. int port;
  376. int ret;
  377. pr_info("lvs:%s\n", __func__);
  378. if (kstrtoint(buf, 0, &port) || port < 1 || port > 255)
  379. port = lvs->portnum;
  380. ret = lvs_rh_set_port_feature(hdev,
  381. port | (USB_SS_PORT_LS_COMP_MOD << 3),
  382. USB_PORT_FEAT_LINK_STATE);
  383. if (ret < 0) {
  384. dev_err(dev, "can't enable compliance mode %d\n", ret);
  385. return ret;
  386. }
  387. return count;
  388. }
  389. static DEVICE_ATTR_WO(enable_compliance);
  390. static ssize_t test_mode_store(struct device *dev,
  391. struct device_attribute *attr, const char *buf, size_t count)
  392. {
  393. struct usb_interface *intf = to_usb_interface(dev);
  394. struct lvs_rh *lvs = usb_get_intfdata(intf);
  395. int ret;
  396. ret = kstrtoint(buf, 0, &lvs->test_mode);
  397. pr_info("lvs: %s: %d\n", __func__, lvs->test_mode);
  398. return count;
  399. }
  400. static DEVICE_ATTR_WO(test_mode);
  401. static struct attribute *lvs_attrs[] = {
  402. &dev_attr_get_dev_desc.attr,
  403. &dev_attr_u1_timeout.attr,
  404. &dev_attr_u2_timeout.attr,
  405. &dev_attr_hot_reset.attr,
  406. &dev_attr_warm_reset.attr,
  407. &dev_attr_u3_entry.attr,
  408. &dev_attr_u3_exit.attr,
  409. &dev_attr_enable_compliance.attr,
  410. &dev_attr_test_mode.attr,
  411. NULL
  412. };
  413. ATTRIBUTE_GROUPS(lvs);
  414. static void lvs_rh_work(struct work_struct *work)
  415. {
  416. struct lvs_rh *lvs = container_of(work, struct lvs_rh, rh_work);
  417. struct usb_interface *intf = lvs->intf;
  418. struct usb_device *hdev = interface_to_usbdev(intf);
  419. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  420. struct usb_hub_descriptor *descriptor = &lvs->descriptor;
  421. struct usb_port_status *port_status = &lvs->port_status;
  422. int i, ret = 0;
  423. u16 portchange;
  424. u16 portstatus;
  425. pr_info("%s\n", __func__);
  426. /* Examine each root port */
  427. for (i = 1; i <= descriptor->bNbrPorts; i++) {
  428. ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  429. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, i,
  430. port_status, sizeof(*port_status), 1000);
  431. if (ret < 4)
  432. continue;
  433. portchange = le16_to_cpu(port_status->wPortChange);
  434. portstatus = le16_to_cpu(port_status->wPortStatus);
  435. pr_info("%s, portchange=0x%x portstatus=0x%x\n", __func__, portchange, portstatus);
  436. if (portchange & USB_PORT_STAT_C_LINK_STATE)
  437. lvs_rh_clear_port_feature(hdev, i,
  438. USB_PORT_FEAT_C_PORT_LINK_STATE);
  439. if (portchange & USB_PORT_STAT_C_ENABLE)
  440. lvs_rh_clear_port_feature(hdev, i,
  441. USB_PORT_FEAT_C_ENABLE);
  442. if (portchange & USB_PORT_STAT_C_RESET)
  443. lvs_rh_clear_port_feature(hdev, i,
  444. USB_PORT_FEAT_C_RESET);
  445. if (portchange & USB_PORT_STAT_C_BH_RESET)
  446. lvs_rh_clear_port_feature(hdev, i,
  447. USB_PORT_FEAT_C_BH_PORT_RESET);
  448. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  449. lvs_rh_clear_port_feature(hdev, i,
  450. USB_PORT_FEAT_C_CONNECTION);
  451. if (le16_to_cpu(port_status->wPortStatus) &
  452. USB_PORT_STAT_CONNECTION) {
  453. lvs->present = true;
  454. lvs->portnum = i;
  455. lvs->test_stat = STAT_CON;
  456. pr_info("%s, portnum=%d hcd->usb_phy=%pK\n", __func__, i, hcd->usb_phy);
  457. if (hcd->usb_phy)
  458. usb_phy_notify_connect(hcd->usb_phy,
  459. USB_SPEED_SUPER);
  460. } else {
  461. lvs->present = false;
  462. lvs->portnum = 0;
  463. lvs->test_stat = STAT_DISC;
  464. if (hcd->usb_phy)
  465. usb_phy_notify_disconnect(hcd->usb_phy,
  466. USB_SPEED_SUPER);
  467. }
  468. break;
  469. } else {
  470. if (!lvs->present) {
  471. if (le16_to_cpu(port_status->wPortStatus) &
  472. USB_PORT_STAT_CONNECTION) {
  473. lvs->present = true;
  474. lvs->portnum = i;
  475. lvs->test_stat = STAT_CON;
  476. pr_info("%s, portnum=%d hcd->usb_phy=%pK\n", __func__, i, hcd->usb_phy);
  477. if (hcd->usb_phy)
  478. usb_phy_notify_connect(hcd->usb_phy,
  479. USB_SPEED_SUPER);
  480. }
  481. }
  482. }
  483. }
  484. ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
  485. if (ret != 0 && ret != -ENODEV && ret != -EPERM)
  486. dev_err(&intf->dev, "urb resubmit error %d\n", ret);
  487. }
  488. static void lvs_rh_irq(struct urb *urb)
  489. {
  490. struct lvs_rh *lvs = urb->context;
  491. pr_info("%s\n", __func__);
  492. schedule_work(&lvs->rh_work);
  493. }
  494. static int lvs_rh_probe(struct usb_interface *intf,
  495. const struct usb_device_id *id)
  496. {
  497. struct usb_device *hdev;
  498. struct usb_host_interface *desc;
  499. struct usb_endpoint_descriptor *endpoint;
  500. struct lvs_rh *lvs;
  501. unsigned int pipe;
  502. int ret, maxp;
  503. pr_info("%s\n", __func__);
  504. hdev = interface_to_usbdev(intf);
  505. desc = intf->cur_altsetting;
  506. ret = usb_find_int_in_endpoint(desc, &endpoint);
  507. if (ret)
  508. return ret;
  509. #if 0
  510. /* valid only for SS root hub */
  511. if (hdev->descriptor.bDeviceProtocol != USB_HUB_PR_SS || hdev->parent) {
  512. dev_err(&intf->dev, "Bind LVS driver with SS root Hub only\n");
  513. return -EINVAL;
  514. }
  515. #endif
  516. lvs = devm_kzalloc(&intf->dev, sizeof(*lvs), GFP_KERNEL);
  517. if (!lvs)
  518. return -ENOMEM;
  519. lvs->intf = intf;
  520. usb_set_intfdata(intf, lvs);
  521. /* how many number of ports this root hub has */
  522. ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  523. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
  524. USB_DT_SS_HUB << 8, 0, &lvs->descriptor,
  525. USB_DT_SS_HUB_SIZE, USB_CTRL_GET_TIMEOUT);
  526. if (ret < (USB_DT_HUB_NONVAR_SIZE + 2)) {
  527. dev_err(&hdev->dev, "wrong root hub descriptor read %d\n", ret);
  528. return ret < 0 ? ret : -EINVAL;
  529. }
  530. /* submit urb to poll interrupt endpoint */
  531. lvs->urb = usb_alloc_urb(0, GFP_KERNEL);
  532. if (!lvs->urb)
  533. return -ENOMEM;
  534. INIT_WORK(&lvs->rh_work, lvs_rh_work);
  535. pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
  536. maxp = usb_maxpacket(hdev, pipe);
  537. usb_fill_int_urb(lvs->urb, hdev, pipe, &lvs->buffer[0], maxp,
  538. lvs_rh_irq, lvs, endpoint->bInterval);
  539. ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
  540. if (ret < 0) {
  541. dev_err(&intf->dev, "couldn't submit lvs urb %d\n", ret);
  542. goto free_urb;
  543. }
  544. #if IS_ENABLED(CONFIG_IF_CB_MANAGER)
  545. lvs->lvs_d.ops = NULL;
  546. lvs->lvs_d.data = (void *)lvs;
  547. lvs->man = register_lvs(&lvs->lvs_d);
  548. usbpd_wait_entermode(lvs->man, 1);
  549. #endif
  550. return ret;
  551. free_urb:
  552. usb_free_urb(lvs->urb);
  553. return ret;
  554. }
  555. static void lvs_rh_disconnect(struct usb_interface *intf)
  556. {
  557. struct lvs_rh *lvs = usb_get_intfdata(intf);
  558. pr_info("%s\n", __func__);
  559. #if IS_ENABLED(CONFIG_IF_CB_MANAGER)
  560. usbpd_wait_entermode(lvs->man, 0);
  561. register_lvs(NULL);
  562. #endif
  563. usb_poison_urb(lvs->urb); /* used in scheduled work */
  564. flush_work(&lvs->rh_work);
  565. usb_free_urb(lvs->urb);
  566. }
  567. static struct usb_driver lvs_driver = {
  568. .name = "lvs",
  569. .probe = lvs_rh_probe,
  570. .disconnect = lvs_rh_disconnect,
  571. .dev_groups = lvs_groups,
  572. };
  573. module_usb_driver(lvs_driver);
  574. MODULE_DESCRIPTION("Link Layer Validation System Driver");
  575. MODULE_LICENSE("GPL");