lvstest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 u2_timeout_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. unsigned long val;
  159. int ret;
  160. ret = kstrtoul(buf, 10, &val);
  161. if (ret < 0) {
  162. dev_err(dev, "couldn't parse string %d\n", ret);
  163. return ret;
  164. }
  165. if (val < 0 || val > 127)
  166. return -EINVAL;
  167. ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
  168. USB_PORT_FEAT_U2_TIMEOUT);
  169. if (ret < 0) {
  170. dev_err(dev, "Error %d while setting U2 timeout %ld\n", ret, val);
  171. return ret;
  172. }
  173. return count;
  174. }
  175. static DEVICE_ATTR_WO(u2_timeout);
  176. static ssize_t u1_timeout_store(struct device *dev,
  177. struct device_attribute *attr, const char *buf, size_t count)
  178. {
  179. struct usb_interface *intf = to_usb_interface(dev);
  180. struct usb_device *hdev = interface_to_usbdev(intf);
  181. struct lvs_rh *lvs = usb_get_intfdata(intf);
  182. unsigned long val;
  183. int ret;
  184. ret = kstrtoul(buf, 10, &val);
  185. if (ret < 0) {
  186. dev_err(dev, "couldn't parse string %d\n", ret);
  187. return ret;
  188. }
  189. if (val < 0 || val > 127)
  190. return -EINVAL;
  191. ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
  192. USB_PORT_FEAT_U1_TIMEOUT);
  193. if (ret < 0) {
  194. dev_err(dev, "Error %d while setting U1 timeout %ld\n", ret, val);
  195. return ret;
  196. }
  197. return count;
  198. }
  199. static DEVICE_ATTR_WO(u1_timeout);
  200. static ssize_t get_dev_desc_store(struct device *dev,
  201. struct device_attribute *attr, const char *buf, size_t count)
  202. {
  203. struct usb_interface *intf = to_usb_interface(dev);
  204. struct usb_device *udev;
  205. struct usb_device_descriptor *descriptor;
  206. int ret;
  207. descriptor = kmalloc(sizeof(*descriptor), GFP_KERNEL);
  208. if (!descriptor)
  209. return -ENOMEM;
  210. udev = create_lvs_device(intf);
  211. if (!udev) {
  212. dev_err(dev, "failed to create lvs device\n");
  213. ret = -ENOMEM;
  214. goto free_desc;
  215. }
  216. ret = usb_control_msg(udev, (PIPE_CONTROL << 30) | USB_DIR_IN,
  217. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, USB_DT_DEVICE << 8,
  218. 0, descriptor, sizeof(*descriptor),
  219. USB_CTRL_GET_TIMEOUT);
  220. if (ret < 0)
  221. dev_err(dev, "can't read device descriptor %d\n", ret);
  222. destroy_lvs_device(udev);
  223. free_desc:
  224. kfree(descriptor);
  225. if (ret < 0)
  226. return ret;
  227. return count;
  228. }
  229. static DEVICE_ATTR_WO(get_dev_desc);
  230. static struct attribute *lvs_attributes[] = {
  231. &dev_attr_get_dev_desc.attr,
  232. &dev_attr_u1_timeout.attr,
  233. &dev_attr_u2_timeout.attr,
  234. &dev_attr_hot_reset.attr,
  235. &dev_attr_u3_entry.attr,
  236. &dev_attr_u3_exit.attr,
  237. NULL
  238. };
  239. static const struct attribute_group lvs_attr_group = {
  240. .attrs = lvs_attributes,
  241. };
  242. static void lvs_rh_work(struct work_struct *work)
  243. {
  244. struct lvs_rh *lvs = container_of(work, struct lvs_rh, rh_work);
  245. struct usb_interface *intf = lvs->intf;
  246. struct usb_device *hdev = interface_to_usbdev(intf);
  247. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  248. struct usb_hub_descriptor *descriptor = &lvs->descriptor;
  249. struct usb_port_status *port_status = &lvs->port_status;
  250. int i, ret = 0;
  251. u16 portchange;
  252. /* Examine each root port */
  253. for (i = 1; i <= descriptor->bNbrPorts; i++) {
  254. ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  255. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, i,
  256. port_status, sizeof(*port_status), 1000);
  257. if (ret < 4)
  258. continue;
  259. portchange = le16_to_cpu(port_status->wPortChange);
  260. if (portchange & USB_PORT_STAT_C_LINK_STATE)
  261. lvs_rh_clear_port_feature(hdev, i,
  262. USB_PORT_FEAT_C_PORT_LINK_STATE);
  263. if (portchange & USB_PORT_STAT_C_ENABLE)
  264. lvs_rh_clear_port_feature(hdev, i,
  265. USB_PORT_FEAT_C_ENABLE);
  266. if (portchange & USB_PORT_STAT_C_RESET)
  267. lvs_rh_clear_port_feature(hdev, i,
  268. USB_PORT_FEAT_C_RESET);
  269. if (portchange & USB_PORT_STAT_C_BH_RESET)
  270. lvs_rh_clear_port_feature(hdev, i,
  271. USB_PORT_FEAT_C_BH_PORT_RESET);
  272. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  273. lvs_rh_clear_port_feature(hdev, i,
  274. USB_PORT_FEAT_C_CONNECTION);
  275. if (le16_to_cpu(port_status->wPortStatus) &
  276. USB_PORT_STAT_CONNECTION) {
  277. lvs->present = true;
  278. lvs->portnum = i;
  279. if (hcd->usb_phy)
  280. usb_phy_notify_connect(hcd->usb_phy,
  281. USB_SPEED_SUPER);
  282. } else {
  283. lvs->present = false;
  284. if (hcd->usb_phy)
  285. usb_phy_notify_disconnect(hcd->usb_phy,
  286. USB_SPEED_SUPER);
  287. }
  288. break;
  289. }
  290. }
  291. ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
  292. if (ret != 0 && ret != -ENODEV && ret != -EPERM)
  293. dev_err(&intf->dev, "urb resubmit error %d\n", ret);
  294. }
  295. static void lvs_rh_irq(struct urb *urb)
  296. {
  297. struct lvs_rh *lvs = urb->context;
  298. schedule_work(&lvs->rh_work);
  299. }
  300. static int lvs_rh_probe(struct usb_interface *intf,
  301. const struct usb_device_id *id)
  302. {
  303. struct usb_device *hdev;
  304. struct usb_host_interface *desc;
  305. struct usb_endpoint_descriptor *endpoint;
  306. struct lvs_rh *lvs;
  307. unsigned int pipe;
  308. int ret, maxp;
  309. hdev = interface_to_usbdev(intf);
  310. desc = intf->cur_altsetting;
  311. if (desc->desc.bNumEndpoints < 1)
  312. return -ENODEV;
  313. endpoint = &desc->endpoint[0].desc;
  314. /* valid only for SS root hub */
  315. if (hdev->descriptor.bDeviceProtocol != USB_HUB_PR_SS || hdev->parent) {
  316. dev_err(&intf->dev, "Bind LVS driver with SS root Hub only\n");
  317. return -EINVAL;
  318. }
  319. lvs = devm_kzalloc(&intf->dev, sizeof(*lvs), GFP_KERNEL);
  320. if (!lvs)
  321. return -ENOMEM;
  322. lvs->intf = intf;
  323. usb_set_intfdata(intf, lvs);
  324. /* how many number of ports this root hub has */
  325. ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  326. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
  327. USB_DT_SS_HUB << 8, 0, &lvs->descriptor,
  328. USB_DT_SS_HUB_SIZE, USB_CTRL_GET_TIMEOUT);
  329. if (ret < (USB_DT_HUB_NONVAR_SIZE + 2)) {
  330. dev_err(&hdev->dev, "wrong root hub descriptor read %d\n", ret);
  331. return ret;
  332. }
  333. /* submit urb to poll interrupt endpoint */
  334. lvs->urb = usb_alloc_urb(0, GFP_KERNEL);
  335. if (!lvs->urb)
  336. return -ENOMEM;
  337. INIT_WORK(&lvs->rh_work, lvs_rh_work);
  338. ret = sysfs_create_group(&intf->dev.kobj, &lvs_attr_group);
  339. if (ret < 0) {
  340. dev_err(&intf->dev, "Failed to create sysfs node %d\n", ret);
  341. goto free_urb;
  342. }
  343. pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
  344. maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
  345. usb_fill_int_urb(lvs->urb, hdev, pipe, &lvs->buffer[0], maxp,
  346. lvs_rh_irq, lvs, endpoint->bInterval);
  347. ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
  348. if (ret < 0) {
  349. dev_err(&intf->dev, "couldn't submit lvs urb %d\n", ret);
  350. goto sysfs_remove;
  351. }
  352. return ret;
  353. sysfs_remove:
  354. sysfs_remove_group(&intf->dev.kobj, &lvs_attr_group);
  355. free_urb:
  356. usb_free_urb(lvs->urb);
  357. return ret;
  358. }
  359. static void lvs_rh_disconnect(struct usb_interface *intf)
  360. {
  361. struct lvs_rh *lvs = usb_get_intfdata(intf);
  362. sysfs_remove_group(&intf->dev.kobj, &lvs_attr_group);
  363. usb_poison_urb(lvs->urb); /* used in scheduled work */
  364. flush_work(&lvs->rh_work);
  365. usb_free_urb(lvs->urb);
  366. }
  367. static struct usb_driver lvs_driver = {
  368. .name = "lvs",
  369. .probe = lvs_rh_probe,
  370. .disconnect = lvs_rh_disconnect,
  371. };
  372. module_usb_driver(lvs_driver);
  373. MODULE_DESCRIPTION("Link Layer Validation System Driver");
  374. MODULE_LICENSE("GPL");