2 * drivers/usb/misc/lvstest.c
4 * Test pattern generation for Link Layer Validation System Tests
6 * Copyright (C) 2014 ST Microelectronics
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>
25 /* root hub interface */
26 struct usb_interface *intf;
27 /* if lvs device connected */
29 /* port no at which lvs device is present */
33 /* class descriptor */
34 struct usb_hub_descriptor descriptor;
35 /* urb for polling interrupt pipe */
38 struct work_struct rh_work;
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;
47 struct lvs_rh *lvs = usb_get_intfdata(intf);
50 dev_err(&intf->dev, "No LVS device is present\n");
54 hdev = interface_to_usbdev(intf);
55 hcd = bus_to_hcd(hdev->bus);
57 udev = usb_alloc_dev(hdev, hdev->bus, lvs->portnum);
59 dev_err(&intf->dev, "Could not allocate lvs udev\n");
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");
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);
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,
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,
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;
113 udev = create_lvs_device(intf);
115 dev_err(dev, "failed to create lvs device\n");
119 ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
120 USB_PORT_FEAT_SUSPEND);
122 dev_err(dev, "can't issue U3 entry %d\n", ret);
124 destroy_lvs_device(udev);
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;
142 udev = create_lvs_device(intf);
144 dev_err(dev, "failed to create lvs device\n");
148 ret = lvs_rh_clear_port_feature(hdev, lvs->portnum,
149 USB_PORT_FEAT_SUSPEND);
151 dev_err(dev, "can't issue U3 exit %d\n", ret);
153 destroy_lvs_device(udev);
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);
170 ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
171 USB_PORT_FEAT_RESET);
173 dev_err(dev, "can't issue hot reset %d\n", ret);
179 static DEVICE_ATTR_WO(hot_reset);
181 static ssize_t u2_timeout_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);
190 ret = kstrtoul(buf, 10, &val);
192 dev_err(dev, "couldn't parse string %d\n", ret);
196 if (val < 0 || val > 127)
199 ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
200 USB_PORT_FEAT_U2_TIMEOUT);
202 dev_err(dev, "Error %d while setting U2 timeout %ld\n", ret, val);
208 static DEVICE_ATTR_WO(u2_timeout);
210 static ssize_t u1_timeout_store(struct device *dev,
211 struct device_attribute *attr, const char *buf, size_t count)
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);
219 ret = kstrtoul(buf, 10, &val);
221 dev_err(dev, "couldn't parse string %d\n", ret);
225 if (val < 0 || val > 127)
228 ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
229 USB_PORT_FEAT_U1_TIMEOUT);
231 dev_err(dev, "Error %d while setting U1 timeout %ld\n", ret, val);
237 static DEVICE_ATTR_WO(u1_timeout);
239 static ssize_t get_dev_desc_store(struct device *dev,
240 struct device_attribute *attr, const char *buf, size_t count)
242 struct usb_interface *intf = to_usb_interface(dev);
243 struct usb_device *udev;
244 struct usb_device_descriptor *descriptor;
247 descriptor = kmalloc(sizeof(*descriptor), GFP_KERNEL);
251 udev = create_lvs_device(intf);
253 dev_err(dev, "failed to create lvs device\n");
258 ret = usb_control_msg(udev, (PIPE_CONTROL << 30) | USB_DIR_IN,
259 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, USB_DT_DEVICE << 8,
260 0, descriptor, sizeof(*descriptor),
261 USB_CTRL_GET_TIMEOUT);
263 dev_err(dev, "can't read device descriptor %d\n", ret);
265 destroy_lvs_device(udev);
275 static DEVICE_ATTR_WO(get_dev_desc);
277 static struct attribute *lvs_attributes[] = {
278 &dev_attr_get_dev_desc.attr,
279 &dev_attr_u1_timeout.attr,
280 &dev_attr_u2_timeout.attr,
281 &dev_attr_hot_reset.attr,
282 &dev_attr_u3_entry.attr,
283 &dev_attr_u3_exit.attr,
287 static const struct attribute_group lvs_attr_group = {
288 .attrs = lvs_attributes,
291 static void lvs_rh_work(struct work_struct *work)
293 struct lvs_rh *lvs = container_of(work, struct lvs_rh, rh_work);
294 struct usb_interface *intf = lvs->intf;
295 struct usb_device *hdev = interface_to_usbdev(intf);
296 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
297 struct usb_hub_descriptor *descriptor = &lvs->descriptor;
298 struct usb_port_status *port_status = &lvs->port_status;
302 /* Examine each root port */
303 for (i = 1; i <= descriptor->bNbrPorts; i++) {
304 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
305 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, i,
306 port_status, sizeof(*port_status), 1000);
310 portchange = le16_to_cpu(port_status->wPortChange);
312 if (portchange & USB_PORT_STAT_C_LINK_STATE)
313 lvs_rh_clear_port_feature(hdev, i,
314 USB_PORT_FEAT_C_PORT_LINK_STATE);
315 if (portchange & USB_PORT_STAT_C_ENABLE)
316 lvs_rh_clear_port_feature(hdev, i,
317 USB_PORT_FEAT_C_ENABLE);
318 if (portchange & USB_PORT_STAT_C_RESET)
319 lvs_rh_clear_port_feature(hdev, i,
320 USB_PORT_FEAT_C_RESET);
321 if (portchange & USB_PORT_STAT_C_BH_RESET)
322 lvs_rh_clear_port_feature(hdev, i,
323 USB_PORT_FEAT_C_BH_PORT_RESET);
324 if (portchange & USB_PORT_STAT_C_CONNECTION) {
325 lvs_rh_clear_port_feature(hdev, i,
326 USB_PORT_FEAT_C_CONNECTION);
328 if (le16_to_cpu(port_status->wPortStatus) &
329 USB_PORT_STAT_CONNECTION) {
333 usb_phy_notify_connect(hcd->usb_phy,
336 lvs->present = false;
338 usb_phy_notify_disconnect(hcd->usb_phy,
345 ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
346 if (ret != 0 && ret != -ENODEV && ret != -EPERM)
347 dev_err(&intf->dev, "urb resubmit error %d\n", ret);
350 static void lvs_rh_irq(struct urb *urb)
352 struct lvs_rh *lvs = urb->context;
354 schedule_work(&lvs->rh_work);
357 static int lvs_rh_probe(struct usb_interface *intf,
358 const struct usb_device_id *id)
360 struct usb_device *hdev;
361 struct usb_host_interface *desc;
362 struct usb_endpoint_descriptor *endpoint;
367 hdev = interface_to_usbdev(intf);
368 desc = intf->cur_altsetting;
369 endpoint = &desc->endpoint[0].desc;
371 /* valid only for SS root hub */
372 if (hdev->descriptor.bDeviceProtocol != USB_HUB_PR_SS || hdev->parent) {
373 dev_err(&intf->dev, "Bind LVS driver with SS root Hub only\n");
377 lvs = devm_kzalloc(&intf->dev, sizeof(*lvs), GFP_KERNEL);
382 usb_set_intfdata(intf, lvs);
384 /* how many number of ports this root hub has */
385 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
386 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
387 USB_DT_SS_HUB << 8, 0, &lvs->descriptor,
388 USB_DT_SS_HUB_SIZE, USB_CTRL_GET_TIMEOUT);
389 if (ret < (USB_DT_HUB_NONVAR_SIZE + 2)) {
390 dev_err(&hdev->dev, "wrong root hub descriptor read %d\n", ret);
394 /* submit urb to poll interrupt endpoint */
395 lvs->urb = usb_alloc_urb(0, GFP_KERNEL);
399 INIT_WORK(&lvs->rh_work, lvs_rh_work);
401 ret = sysfs_create_group(&intf->dev.kobj, &lvs_attr_group);
403 dev_err(&intf->dev, "Failed to create sysfs node %d\n", ret);
407 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
408 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
409 usb_fill_int_urb(lvs->urb, hdev, pipe, &lvs->buffer[0], maxp,
410 lvs_rh_irq, lvs, endpoint->bInterval);
412 ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
414 dev_err(&intf->dev, "couldn't submit lvs urb %d\n", ret);
421 sysfs_remove_group(&intf->dev.kobj, &lvs_attr_group);
423 usb_free_urb(lvs->urb);
427 static void lvs_rh_disconnect(struct usb_interface *intf)
429 struct lvs_rh *lvs = usb_get_intfdata(intf);
431 sysfs_remove_group(&intf->dev.kobj, &lvs_attr_group);
432 flush_work(&lvs->rh_work);
433 usb_free_urb(lvs->urb);
436 static struct usb_driver lvs_driver = {
438 .probe = lvs_rh_probe,
439 .disconnect = lvs_rh_disconnect,
442 module_usb_driver(lvs_driver);
444 MODULE_DESCRIPTION("Link Layer Validation System Driver");
445 MODULE_LICENSE("GPL");