Linux 4.14.51
[linux/fpc-iii.git] / drivers / usb / misc / lvstest.c
blobddddd6387f6642e5792479c535adf347f2bfb962
1 /*
2 * drivers/usb/misc/lvstest.c
4 * Test pattern generation for Link Layer Validation System Tests
6 * Copyright (C) 2014 ST Microelectronics
7 * Pratyush Anand <pratyush.anand@gmail.com>
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.
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>
24 struct 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;
37 /* LVH RH work */
38 struct work_struct rh_work;
39 /* RH port status */
40 struct usb_port_status port_status;
43 static struct usb_device *create_lvs_device(struct usb_interface *intf)
45 struct usb_device *udev, *hdev;
46 struct usb_hcd *hcd;
47 struct lvs_rh *lvs = usb_get_intfdata(intf);
49 if (!lvs->present) {
50 dev_err(&intf->dev, "No LVS device is present\n");
51 return NULL;
54 hdev = interface_to_usbdev(intf);
55 hcd = bus_to_hcd(hdev->bus);
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;
62 udev->speed = USB_SPEED_SUPER;
63 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
64 usb_set_device_state(udev, USB_STATE_DEFAULT);
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;
74 return udev;
77 static void destroy_lvs_device(struct usb_device *udev)
79 struct usb_device *hdev = udev->parent;
80 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
82 if (hcd->driver->free_dev)
83 hcd->driver->free_dev(hcd, udev);
85 usb_put_dev(udev);
88 static int lvs_rh_clear_port_feature(struct usb_device *hdev,
89 int port1, int feature)
91 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
92 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
93 NULL, 0, 1000);
96 static int lvs_rh_set_port_feature(struct usb_device *hdev,
97 int port1, int feature)
99 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
100 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
101 NULL, 0, 1000);
104 static ssize_t u3_entry_store(struct device *dev,
105 struct device_attribute *attr, const char *buf, size_t count)
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;
113 udev = create_lvs_device(intf);
114 if (!udev) {
115 dev_err(dev, "failed to create lvs device\n");
116 return -ENOMEM;
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);
124 destroy_lvs_device(udev);
126 if (ret < 0)
127 return ret;
129 return count;
131 static DEVICE_ATTR_WO(u3_entry);
133 static ssize_t u3_exit_store(struct device *dev,
134 struct device_attribute *attr, const char *buf, size_t count)
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;
142 udev = create_lvs_device(intf);
143 if (!udev) {
144 dev_err(dev, "failed to create lvs device\n");
145 return -ENOMEM;
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);
153 destroy_lvs_device(udev);
155 if (ret < 0)
156 return ret;
158 return count;
160 static DEVICE_ATTR_WO(u3_exit);
162 static ssize_t hot_reset_store(struct device *dev,
163 struct device_attribute *attr, const char *buf, size_t count)
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;
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;
177 return count;
179 static DEVICE_ATTR_WO(hot_reset);
181 static ssize_t warm_reset_store(struct device *dev,
182 struct device_attribute *attr, const char *buf, size_t count)
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);
187 int ret;
189 ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
190 USB_PORT_FEAT_BH_PORT_RESET);
191 if (ret < 0) {
192 dev_err(dev, "can't issue warm reset %d\n", ret);
193 return ret;
196 return count;
198 static DEVICE_ATTR_WO(warm_reset);
200 static ssize_t u2_timeout_store(struct device *dev,
201 struct device_attribute *attr, const char *buf, size_t count)
203 struct usb_interface *intf = to_usb_interface(dev);
204 struct usb_device *hdev = interface_to_usbdev(intf);
205 struct lvs_rh *lvs = usb_get_intfdata(intf);
206 unsigned long val;
207 int ret;
209 ret = kstrtoul(buf, 10, &val);
210 if (ret < 0) {
211 dev_err(dev, "couldn't parse string %d\n", ret);
212 return ret;
215 if (val > 127)
216 return -EINVAL;
218 ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
219 USB_PORT_FEAT_U2_TIMEOUT);
220 if (ret < 0) {
221 dev_err(dev, "Error %d while setting U2 timeout %ld\n", ret, val);
222 return ret;
225 return count;
227 static DEVICE_ATTR_WO(u2_timeout);
229 static ssize_t u1_timeout_store(struct device *dev,
230 struct device_attribute *attr, const char *buf, size_t count)
232 struct usb_interface *intf = to_usb_interface(dev);
233 struct usb_device *hdev = interface_to_usbdev(intf);
234 struct lvs_rh *lvs = usb_get_intfdata(intf);
235 unsigned long val;
236 int ret;
238 ret = kstrtoul(buf, 10, &val);
239 if (ret < 0) {
240 dev_err(dev, "couldn't parse string %d\n", ret);
241 return ret;
244 if (val > 127)
245 return -EINVAL;
247 ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
248 USB_PORT_FEAT_U1_TIMEOUT);
249 if (ret < 0) {
250 dev_err(dev, "Error %d while setting U1 timeout %ld\n", ret, val);
251 return ret;
254 return count;
256 static DEVICE_ATTR_WO(u1_timeout);
258 static ssize_t get_dev_desc_store(struct device *dev,
259 struct device_attribute *attr, const char *buf, size_t count)
261 struct usb_interface *intf = to_usb_interface(dev);
262 struct usb_device *udev;
263 struct usb_device_descriptor *descriptor;
264 int ret;
266 descriptor = kmalloc(sizeof(*descriptor), GFP_KERNEL);
267 if (!descriptor)
268 return -ENOMEM;
270 udev = create_lvs_device(intf);
271 if (!udev) {
272 dev_err(dev, "failed to create lvs device\n");
273 ret = -ENOMEM;
274 goto free_desc;
277 ret = usb_control_msg(udev, (PIPE_CONTROL << 30) | USB_DIR_IN,
278 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, USB_DT_DEVICE << 8,
279 0, descriptor, sizeof(*descriptor),
280 USB_CTRL_GET_TIMEOUT);
281 if (ret < 0)
282 dev_err(dev, "can't read device descriptor %d\n", ret);
284 destroy_lvs_device(udev);
286 free_desc:
287 kfree(descriptor);
289 if (ret < 0)
290 return ret;
292 return count;
294 static DEVICE_ATTR_WO(get_dev_desc);
296 static ssize_t enable_compliance_store(struct device *dev,
297 struct device_attribute *attr, const char *buf, size_t count)
299 struct usb_interface *intf = to_usb_interface(dev);
300 struct usb_device *hdev = interface_to_usbdev(intf);
301 struct lvs_rh *lvs = usb_get_intfdata(intf);
302 int ret;
304 ret = lvs_rh_set_port_feature(hdev,
305 lvs->portnum | USB_SS_PORT_LS_COMP_MOD << 3,
306 USB_PORT_FEAT_LINK_STATE);
307 if (ret < 0) {
308 dev_err(dev, "can't enable compliance mode %d\n", ret);
309 return ret;
312 return count;
314 static DEVICE_ATTR_WO(enable_compliance);
316 static struct attribute *lvs_attributes[] = {
317 &dev_attr_get_dev_desc.attr,
318 &dev_attr_u1_timeout.attr,
319 &dev_attr_u2_timeout.attr,
320 &dev_attr_hot_reset.attr,
321 &dev_attr_warm_reset.attr,
322 &dev_attr_u3_entry.attr,
323 &dev_attr_u3_exit.attr,
324 &dev_attr_enable_compliance.attr,
325 NULL
328 static const struct attribute_group lvs_attr_group = {
329 .attrs = lvs_attributes,
332 static void lvs_rh_work(struct work_struct *work)
334 struct lvs_rh *lvs = container_of(work, struct lvs_rh, rh_work);
335 struct usb_interface *intf = lvs->intf;
336 struct usb_device *hdev = interface_to_usbdev(intf);
337 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
338 struct usb_hub_descriptor *descriptor = &lvs->descriptor;
339 struct usb_port_status *port_status = &lvs->port_status;
340 int i, ret = 0;
341 u16 portchange;
343 /* Examine each root port */
344 for (i = 1; i <= descriptor->bNbrPorts; i++) {
345 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
346 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, i,
347 port_status, sizeof(*port_status), 1000);
348 if (ret < 4)
349 continue;
351 portchange = le16_to_cpu(port_status->wPortChange);
353 if (portchange & USB_PORT_STAT_C_LINK_STATE)
354 lvs_rh_clear_port_feature(hdev, i,
355 USB_PORT_FEAT_C_PORT_LINK_STATE);
356 if (portchange & USB_PORT_STAT_C_ENABLE)
357 lvs_rh_clear_port_feature(hdev, i,
358 USB_PORT_FEAT_C_ENABLE);
359 if (portchange & USB_PORT_STAT_C_RESET)
360 lvs_rh_clear_port_feature(hdev, i,
361 USB_PORT_FEAT_C_RESET);
362 if (portchange & USB_PORT_STAT_C_BH_RESET)
363 lvs_rh_clear_port_feature(hdev, i,
364 USB_PORT_FEAT_C_BH_PORT_RESET);
365 if (portchange & USB_PORT_STAT_C_CONNECTION) {
366 lvs_rh_clear_port_feature(hdev, i,
367 USB_PORT_FEAT_C_CONNECTION);
369 if (le16_to_cpu(port_status->wPortStatus) &
370 USB_PORT_STAT_CONNECTION) {
371 lvs->present = true;
372 lvs->portnum = i;
373 if (hcd->usb_phy)
374 usb_phy_notify_connect(hcd->usb_phy,
375 USB_SPEED_SUPER);
376 } else {
377 lvs->present = false;
378 if (hcd->usb_phy)
379 usb_phy_notify_disconnect(hcd->usb_phy,
380 USB_SPEED_SUPER);
382 break;
386 ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
387 if (ret != 0 && ret != -ENODEV && ret != -EPERM)
388 dev_err(&intf->dev, "urb resubmit error %d\n", ret);
391 static void lvs_rh_irq(struct urb *urb)
393 struct lvs_rh *lvs = urb->context;
395 schedule_work(&lvs->rh_work);
398 static int lvs_rh_probe(struct usb_interface *intf,
399 const struct usb_device_id *id)
401 struct usb_device *hdev;
402 struct usb_host_interface *desc;
403 struct usb_endpoint_descriptor *endpoint;
404 struct lvs_rh *lvs;
405 unsigned int pipe;
406 int ret, maxp;
408 hdev = interface_to_usbdev(intf);
409 desc = intf->cur_altsetting;
411 ret = usb_find_int_in_endpoint(desc, &endpoint);
412 if (ret)
413 return ret;
415 /* valid only for SS root hub */
416 if (hdev->descriptor.bDeviceProtocol != USB_HUB_PR_SS || hdev->parent) {
417 dev_err(&intf->dev, "Bind LVS driver with SS root Hub only\n");
418 return -EINVAL;
421 lvs = devm_kzalloc(&intf->dev, sizeof(*lvs), GFP_KERNEL);
422 if (!lvs)
423 return -ENOMEM;
425 lvs->intf = intf;
426 usb_set_intfdata(intf, lvs);
428 /* how many number of ports this root hub has */
429 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
430 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
431 USB_DT_SS_HUB << 8, 0, &lvs->descriptor,
432 USB_DT_SS_HUB_SIZE, USB_CTRL_GET_TIMEOUT);
433 if (ret < (USB_DT_HUB_NONVAR_SIZE + 2)) {
434 dev_err(&hdev->dev, "wrong root hub descriptor read %d\n", ret);
435 return ret;
438 /* submit urb to poll interrupt endpoint */
439 lvs->urb = usb_alloc_urb(0, GFP_KERNEL);
440 if (!lvs->urb)
441 return -ENOMEM;
443 INIT_WORK(&lvs->rh_work, lvs_rh_work);
445 ret = sysfs_create_group(&intf->dev.kobj, &lvs_attr_group);
446 if (ret < 0) {
447 dev_err(&intf->dev, "Failed to create sysfs node %d\n", ret);
448 goto free_urb;
451 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
452 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
453 usb_fill_int_urb(lvs->urb, hdev, pipe, &lvs->buffer[0], maxp,
454 lvs_rh_irq, lvs, endpoint->bInterval);
456 ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
457 if (ret < 0) {
458 dev_err(&intf->dev, "couldn't submit lvs urb %d\n", ret);
459 goto sysfs_remove;
462 return ret;
464 sysfs_remove:
465 sysfs_remove_group(&intf->dev.kobj, &lvs_attr_group);
466 free_urb:
467 usb_free_urb(lvs->urb);
468 return ret;
471 static void lvs_rh_disconnect(struct usb_interface *intf)
473 struct lvs_rh *lvs = usb_get_intfdata(intf);
475 sysfs_remove_group(&intf->dev.kobj, &lvs_attr_group);
476 usb_poison_urb(lvs->urb); /* used in scheduled work */
477 flush_work(&lvs->rh_work);
478 usb_free_urb(lvs->urb);
481 static struct usb_driver lvs_driver = {
482 .name = "lvs",
483 .probe = lvs_rh_probe,
484 .disconnect = lvs_rh_disconnect,
487 module_usb_driver(lvs_driver);
489 MODULE_DESCRIPTION("Link Layer Validation System Driver");
490 MODULE_LICENSE("GPL");