lvstest.c 12 KB

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