blob: baa57bc2919224f254728423c6c4e7862a440237 [file] [log] [blame]
Pratyush Anandce21bfe2014-07-14 19:27:49 +05301/*
2 * drivers/usb/misc/lvstest.c
3 *
4 * Test pattern generation for Link Layer Validation System Tests
5 *
6 * Copyright (C) 2014 ST Microelectronics
Pratyush Anande34cadd2015-06-25 15:01:08 -07007 * Pratyush Anand <pratyush.anand@gmail.com>
Pratyush Anandce21bfe2014-07-14 19:27:49 +05308 *
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
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19#include <linux/usb.h>
20#include <linux/usb/ch11.h>
21#include <linux/usb/hcd.h>
22#include <linux/usb/phy.h>
23
24struct lvs_rh {
25 /* root hub interface */
26 struct usb_interface *intf;
27 /* if lvs device connected */
28 bool present;
29 /* port no at which lvs device is present */
30 int portnum;
31 /* urb buffer */
32 u8 buffer[8];
33 /* class descriptor */
34 struct usb_hub_descriptor descriptor;
35 /* urb for polling interrupt pipe */
36 struct urb *urb;
Pratyush Anandce21bfe2014-07-14 19:27:49 +053037 /* LVH RH work */
38 struct work_struct rh_work;
39 /* RH port status */
40 struct usb_port_status port_status;
41};
42
43static struct usb_device *create_lvs_device(struct usb_interface *intf)
44{
45 struct usb_device *udev, *hdev;
46 struct usb_hcd *hcd;
47 struct lvs_rh *lvs = usb_get_intfdata(intf);
48
49 if (!lvs->present) {
50 dev_err(&intf->dev, "No LVS device is present\n");
51 return NULL;
52 }
53
54 hdev = interface_to_usbdev(intf);
55 hcd = bus_to_hcd(hdev->bus);
56
57 udev = usb_alloc_dev(hdev, hdev->bus, lvs->portnum);
58 if (!udev) {
59 dev_err(&intf->dev, "Could not allocate lvs udev\n");
60 return NULL;
61 }
62 udev->speed = USB_SPEED_SUPER;
63 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
64 usb_set_device_state(udev, USB_STATE_DEFAULT);
65
66 if (hcd->driver->enable_device) {
67 if (hcd->driver->enable_device(hcd, udev) < 0) {
68 dev_err(&intf->dev, "Failed to enable\n");
69 usb_put_dev(udev);
70 return NULL;
71 }
72 }
73
74 return udev;
75}
76
77static void destroy_lvs_device(struct usb_device *udev)
78{
79 struct usb_device *hdev = udev->parent;
80 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
81
82 if (hcd->driver->free_dev)
83 hcd->driver->free_dev(hcd, udev);
84
85 usb_put_dev(udev);
86}
87
88static int lvs_rh_clear_port_feature(struct usb_device *hdev,
89 int port1, int feature)
90{
91 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
92 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
93 NULL, 0, 1000);
94}
95
96static int lvs_rh_set_port_feature(struct usb_device *hdev,
97 int port1, int feature)
98{
99 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
100 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
101 NULL, 0, 1000);
102}
103
104static ssize_t u3_entry_store(struct device *dev,
105 struct device_attribute *attr, const char *buf, size_t count)
106{
107 struct usb_interface *intf = to_usb_interface(dev);
108 struct usb_device *hdev = interface_to_usbdev(intf);
109 struct lvs_rh *lvs = usb_get_intfdata(intf);
110 struct usb_device *udev;
111 int ret;
112
113 udev = create_lvs_device(intf);
114 if (!udev) {
115 dev_err(dev, "failed to create lvs device\n");
116 return -ENOMEM;
117 }
118
119 ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
120 USB_PORT_FEAT_SUSPEND);
121 if (ret < 0)
122 dev_err(dev, "can't issue U3 entry %d\n", ret);
123
124 destroy_lvs_device(udev);
125
126 if (ret < 0)
127 return ret;
128
129 return count;
130}
131static DEVICE_ATTR_WO(u3_entry);
132
133static ssize_t u3_exit_store(struct device *dev,
134 struct device_attribute *attr, const char *buf, size_t count)
135{
136 struct usb_interface *intf = to_usb_interface(dev);
137 struct usb_device *hdev = interface_to_usbdev(intf);
138 struct lvs_rh *lvs = usb_get_intfdata(intf);
139 struct usb_device *udev;
140 int ret;
141
142 udev = create_lvs_device(intf);
143 if (!udev) {
144 dev_err(dev, "failed to create lvs device\n");
145 return -ENOMEM;
146 }
147
148 ret = lvs_rh_clear_port_feature(hdev, lvs->portnum,
149 USB_PORT_FEAT_SUSPEND);
150 if (ret < 0)
151 dev_err(dev, "can't issue U3 exit %d\n", ret);
152
153 destroy_lvs_device(udev);
154
155 if (ret < 0)
156 return ret;
157
158 return count;
159}
160static DEVICE_ATTR_WO(u3_exit);
161
162static ssize_t hot_reset_store(struct device *dev,
163 struct device_attribute *attr, const char *buf, size_t count)
164{
165 struct usb_interface *intf = to_usb_interface(dev);
166 struct usb_device *hdev = interface_to_usbdev(intf);
167 struct lvs_rh *lvs = usb_get_intfdata(intf);
168 int ret;
169
170 ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
171 USB_PORT_FEAT_RESET);
172 if (ret < 0) {
173 dev_err(dev, "can't issue hot reset %d\n", ret);
174 return ret;
175 }
176
177 return count;
178}
179static DEVICE_ATTR_WO(hot_reset);
180
Jack Pham0fc40df2017-07-27 18:43:12 -0700181static ssize_t warm_reset_store(struct device *dev,
182 struct device_attribute *attr, const char *buf, size_t count)
183{
184 struct usb_interface *intf = to_usb_interface(dev);
185 struct usb_device *hdev = interface_to_usbdev(intf);
186 struct lvs_rh *lvs = usb_get_intfdata(intf);
Jack Phamfc87dad2017-10-02 14:13:47 -0700187 int port;
Jack Pham0fc40df2017-07-27 18:43:12 -0700188 int ret;
189
Jack Phamfc87dad2017-10-02 14:13:47 -0700190 if (kstrtoint(buf, 0, &port) || port < 1 || port > 255)
191 port = lvs->portnum;
192
193 ret = lvs_rh_set_port_feature(hdev, port, USB_PORT_FEAT_BH_PORT_RESET);
Jack Pham0fc40df2017-07-27 18:43:12 -0700194 if (ret < 0) {
195 dev_err(dev, "can't issue warm reset %d\n", ret);
196 return ret;
197 }
198
199 return count;
200}
201static DEVICE_ATTR_WO(warm_reset);
202
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530203static ssize_t u2_timeout_store(struct device *dev,
204 struct device_attribute *attr, const char *buf, size_t count)
205{
206 struct usb_interface *intf = to_usb_interface(dev);
207 struct usb_device *hdev = interface_to_usbdev(intf);
208 struct lvs_rh *lvs = usb_get_intfdata(intf);
209 unsigned long val;
210 int ret;
211
212 ret = kstrtoul(buf, 10, &val);
213 if (ret < 0) {
214 dev_err(dev, "couldn't parse string %d\n", ret);
215 return ret;
216 }
217
218 if (val < 0 || val > 127)
219 return -EINVAL;
220
221 ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
222 USB_PORT_FEAT_U2_TIMEOUT);
223 if (ret < 0) {
224 dev_err(dev, "Error %d while setting U2 timeout %ld\n", ret, val);
225 return ret;
226 }
227
228 return count;
229}
230static DEVICE_ATTR_WO(u2_timeout);
231
232static ssize_t u1_timeout_store(struct device *dev,
233 struct device_attribute *attr, const char *buf, size_t count)
234{
235 struct usb_interface *intf = to_usb_interface(dev);
236 struct usb_device *hdev = interface_to_usbdev(intf);
237 struct lvs_rh *lvs = usb_get_intfdata(intf);
238 unsigned long val;
239 int ret;
240
241 ret = kstrtoul(buf, 10, &val);
242 if (ret < 0) {
243 dev_err(dev, "couldn't parse string %d\n", ret);
244 return ret;
245 }
246
247 if (val < 0 || val > 127)
248 return -EINVAL;
249
250 ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
251 USB_PORT_FEAT_U1_TIMEOUT);
252 if (ret < 0) {
253 dev_err(dev, "Error %d while setting U1 timeout %ld\n", ret, val);
254 return ret;
255 }
256
257 return count;
258}
259static DEVICE_ATTR_WO(u1_timeout);
260
261static ssize_t get_dev_desc_store(struct device *dev,
262 struct device_attribute *attr, const char *buf, size_t count)
263{
264 struct usb_interface *intf = to_usb_interface(dev);
265 struct usb_device *udev;
266 struct usb_device_descriptor *descriptor;
267 int ret;
268
269 descriptor = kmalloc(sizeof(*descriptor), GFP_KERNEL);
Wolfram Sang5c47fd62016-08-25 19:39:20 +0200270 if (!descriptor)
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530271 return -ENOMEM;
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530272
273 udev = create_lvs_device(intf);
274 if (!udev) {
275 dev_err(dev, "failed to create lvs device\n");
276 ret = -ENOMEM;
277 goto free_desc;
278 }
279
280 ret = usb_control_msg(udev, (PIPE_CONTROL << 30) | USB_DIR_IN,
281 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, USB_DT_DEVICE << 8,
282 0, descriptor, sizeof(*descriptor),
283 USB_CTRL_GET_TIMEOUT);
284 if (ret < 0)
285 dev_err(dev, "can't read device descriptor %d\n", ret);
286
287 destroy_lvs_device(udev);
288
289free_desc:
290 kfree(descriptor);
291
292 if (ret < 0)
293 return ret;
294
295 return count;
296}
297static DEVICE_ATTR_WO(get_dev_desc);
298
Jack Pham0fc40df2017-07-27 18:43:12 -0700299static ssize_t enable_compliance_store(struct device *dev,
300 struct device_attribute *attr, const char *buf, size_t count)
301{
302 struct usb_interface *intf = to_usb_interface(dev);
303 struct usb_device *hdev = interface_to_usbdev(intf);
304 struct lvs_rh *lvs = usb_get_intfdata(intf);
Jack Phamfc87dad2017-10-02 14:13:47 -0700305 int port;
Jack Pham0fc40df2017-07-27 18:43:12 -0700306 int ret;
307
Jack Phamfc87dad2017-10-02 14:13:47 -0700308 if (kstrtoint(buf, 0, &port) || port < 1 || port > 255)
309 port = lvs->portnum;
310
Jack Pham0fc40df2017-07-27 18:43:12 -0700311 ret = lvs_rh_set_port_feature(hdev,
Jack Phamfc87dad2017-10-02 14:13:47 -0700312 port | (USB_SS_PORT_LS_COMP_MOD << 3),
Jack Pham0fc40df2017-07-27 18:43:12 -0700313 USB_PORT_FEAT_LINK_STATE);
314 if (ret < 0) {
315 dev_err(dev, "can't enable compliance mode %d\n", ret);
316 return ret;
317 }
318
319 return count;
320}
321static DEVICE_ATTR_WO(enable_compliance);
322
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530323static struct attribute *lvs_attributes[] = {
324 &dev_attr_get_dev_desc.attr,
325 &dev_attr_u1_timeout.attr,
326 &dev_attr_u2_timeout.attr,
327 &dev_attr_hot_reset.attr,
Jack Pham0fc40df2017-07-27 18:43:12 -0700328 &dev_attr_warm_reset.attr,
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530329 &dev_attr_u3_entry.attr,
330 &dev_attr_u3_exit.attr,
Jack Pham0fc40df2017-07-27 18:43:12 -0700331 &dev_attr_enable_compliance.attr,
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530332 NULL
333};
334
335static const struct attribute_group lvs_attr_group = {
336 .attrs = lvs_attributes,
337};
338
339static void lvs_rh_work(struct work_struct *work)
340{
341 struct lvs_rh *lvs = container_of(work, struct lvs_rh, rh_work);
342 struct usb_interface *intf = lvs->intf;
343 struct usb_device *hdev = interface_to_usbdev(intf);
344 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
345 struct usb_hub_descriptor *descriptor = &lvs->descriptor;
346 struct usb_port_status *port_status = &lvs->port_status;
347 int i, ret = 0;
348 u16 portchange;
349
350 /* Examine each root port */
351 for (i = 1; i <= descriptor->bNbrPorts; i++) {
352 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
353 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, i,
354 port_status, sizeof(*port_status), 1000);
355 if (ret < 4)
356 continue;
357
Pratyush Anandb1bd3f12014-07-21 10:16:53 +0530358 portchange = le16_to_cpu(port_status->wPortChange);
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530359
360 if (portchange & USB_PORT_STAT_C_LINK_STATE)
361 lvs_rh_clear_port_feature(hdev, i,
362 USB_PORT_FEAT_C_PORT_LINK_STATE);
363 if (portchange & USB_PORT_STAT_C_ENABLE)
364 lvs_rh_clear_port_feature(hdev, i,
365 USB_PORT_FEAT_C_ENABLE);
366 if (portchange & USB_PORT_STAT_C_RESET)
367 lvs_rh_clear_port_feature(hdev, i,
368 USB_PORT_FEAT_C_RESET);
369 if (portchange & USB_PORT_STAT_C_BH_RESET)
370 lvs_rh_clear_port_feature(hdev, i,
371 USB_PORT_FEAT_C_BH_PORT_RESET);
372 if (portchange & USB_PORT_STAT_C_CONNECTION) {
373 lvs_rh_clear_port_feature(hdev, i,
374 USB_PORT_FEAT_C_CONNECTION);
375
Pratyush Anandb1bd3f12014-07-21 10:16:53 +0530376 if (le16_to_cpu(port_status->wPortStatus) &
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530377 USB_PORT_STAT_CONNECTION) {
378 lvs->present = true;
379 lvs->portnum = i;
Antoine Tenart3d46e732014-09-24 23:05:50 +0400380 if (hcd->usb_phy)
381 usb_phy_notify_connect(hcd->usb_phy,
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530382 USB_SPEED_SUPER);
383 } else {
384 lvs->present = false;
Antoine Tenart3d46e732014-09-24 23:05:50 +0400385 if (hcd->usb_phy)
386 usb_phy_notify_disconnect(hcd->usb_phy,
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530387 USB_SPEED_SUPER);
388 }
389 break;
390 }
391 }
392
393 ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
394 if (ret != 0 && ret != -ENODEV && ret != -EPERM)
395 dev_err(&intf->dev, "urb resubmit error %d\n", ret);
396}
397
398static void lvs_rh_irq(struct urb *urb)
399{
400 struct lvs_rh *lvs = urb->context;
401
Bhaktipriya Shridharbd783102016-07-28 14:15:11 +0530402 schedule_work(&lvs->rh_work);
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530403}
404
405static int lvs_rh_probe(struct usb_interface *intf,
406 const struct usb_device_id *id)
407{
408 struct usb_device *hdev;
409 struct usb_host_interface *desc;
410 struct usb_endpoint_descriptor *endpoint;
411 struct lvs_rh *lvs;
412 unsigned int pipe;
413 int ret, maxp;
414
415 hdev = interface_to_usbdev(intf);
416 desc = intf->cur_altsetting;
Johan Hovold21e06212017-03-13 13:47:49 +0100417
418 if (desc->desc.bNumEndpoints < 1)
419 return -ENODEV;
420
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530421 endpoint = &desc->endpoint[0].desc;
422
423 /* valid only for SS root hub */
424 if (hdev->descriptor.bDeviceProtocol != USB_HUB_PR_SS || hdev->parent) {
425 dev_err(&intf->dev, "Bind LVS driver with SS root Hub only\n");
426 return -EINVAL;
427 }
428
429 lvs = devm_kzalloc(&intf->dev, sizeof(*lvs), GFP_KERNEL);
430 if (!lvs)
431 return -ENOMEM;
432
433 lvs->intf = intf;
434 usb_set_intfdata(intf, lvs);
435
436 /* how many number of ports this root hub has */
437 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
438 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
439 USB_DT_SS_HUB << 8, 0, &lvs->descriptor,
440 USB_DT_SS_HUB_SIZE, USB_CTRL_GET_TIMEOUT);
441 if (ret < (USB_DT_HUB_NONVAR_SIZE + 2)) {
442 dev_err(&hdev->dev, "wrong root hub descriptor read %d\n", ret);
443 return ret;
444 }
445
446 /* submit urb to poll interrupt endpoint */
447 lvs->urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sangda4e20f2016-08-11 23:14:42 +0200448 if (!lvs->urb)
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530449 return -ENOMEM;
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530450
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530451 INIT_WORK(&lvs->rh_work, lvs_rh_work);
452
453 ret = sysfs_create_group(&intf->dev.kobj, &lvs_attr_group);
454 if (ret < 0) {
455 dev_err(&intf->dev, "Failed to create sysfs node %d\n", ret);
Bhaktipriya Shridharbd783102016-07-28 14:15:11 +0530456 goto free_urb;
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530457 }
458
459 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
460 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
461 usb_fill_int_urb(lvs->urb, hdev, pipe, &lvs->buffer[0], maxp,
462 lvs_rh_irq, lvs, endpoint->bInterval);
463
464 ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
465 if (ret < 0) {
466 dev_err(&intf->dev, "couldn't submit lvs urb %d\n", ret);
467 goto sysfs_remove;
468 }
469
470 return ret;
471
472sysfs_remove:
473 sysfs_remove_group(&intf->dev.kobj, &lvs_attr_group);
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530474free_urb:
475 usb_free_urb(lvs->urb);
476 return ret;
477}
478
479static void lvs_rh_disconnect(struct usb_interface *intf)
480{
481 struct lvs_rh *lvs = usb_get_intfdata(intf);
482
483 sysfs_remove_group(&intf->dev.kobj, &lvs_attr_group);
Oliver Neukume6e868c72017-03-14 12:05:07 +0100484 usb_poison_urb(lvs->urb); /* used in scheduled work */
Bhaktipriya Shridharbd783102016-07-28 14:15:11 +0530485 flush_work(&lvs->rh_work);
Pratyush Anandce21bfe2014-07-14 19:27:49 +0530486 usb_free_urb(lvs->urb);
487}
488
489static struct usb_driver lvs_driver = {
490 .name = "lvs",
491 .probe = lvs_rh_probe,
492 .disconnect = lvs_rh_disconnect,
493};
494
495module_usb_driver(lvs_driver);
496
497MODULE_DESCRIPTION("Link Layer Validation System Driver");
498MODULE_LICENSE("GPL");