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 */
37 /* LVS RH work queue */
38 struct workqueue_struct *rh_queue;
40 struct work_struct rh_work;
42 struct usb_port_status port_status;
45 static struct usb_device *create_lvs_device(struct usb_interface *intf)
47 struct usb_device *udev, *hdev;
49 struct lvs_rh *lvs = usb_get_intfdata(intf);
52 dev_err(&intf->dev, "No LVS device is present\n");
56 hdev = interface_to_usbdev(intf);
57 hcd = bus_to_hcd(hdev->bus);
59 udev = usb_alloc_dev(hdev, hdev->bus, lvs->portnum);
61 dev_err(&intf->dev, "Could not allocate lvs udev\n");
64 udev->speed = USB_SPEED_SUPER;
65 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
66 usb_set_device_state(udev, USB_STATE_DEFAULT);
68 if (hcd->driver->enable_device) {
69 if (hcd->driver->enable_device(hcd, udev) < 0) {
70 dev_err(&intf->dev, "Failed to enable\n");
79 static void destroy_lvs_device(struct usb_device *udev)
81 struct usb_device *hdev = udev->parent;
82 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
84 if (hcd->driver->free_dev)
85 hcd->driver->free_dev(hcd, udev);
90 static int lvs_rh_clear_port_feature(struct usb_device *hdev,
91 int port1, int feature)
93 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
94 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
98 static int lvs_rh_set_port_feature(struct usb_device *hdev,
99 int port1, int feature)
101 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
102 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
106 static ssize_t u3_entry_store(struct device *dev,
107 struct device_attribute *attr, const char *buf, size_t count)
109 struct usb_interface *intf = to_usb_interface(dev);
110 struct usb_device *hdev = interface_to_usbdev(intf);
111 struct lvs_rh *lvs = usb_get_intfdata(intf);
112 struct usb_device *udev;
115 udev = create_lvs_device(intf);
117 dev_err(dev, "failed to create lvs device\n");
121 ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
122 USB_PORT_FEAT_SUSPEND);
124 dev_err(dev, "can't issue U3 entry %d\n", ret);
126 destroy_lvs_device(udev);
133 static DEVICE_ATTR_WO(u3_entry);
135 static ssize_t u3_exit_store(struct device *dev,
136 struct device_attribute *attr, const char *buf, size_t count)
138 struct usb_interface *intf = to_usb_interface(dev);
139 struct usb_device *hdev = interface_to_usbdev(intf);
140 struct lvs_rh *lvs = usb_get_intfdata(intf);
141 struct usb_device *udev;
144 udev = create_lvs_device(intf);
146 dev_err(dev, "failed to create lvs device\n");
150 ret = lvs_rh_clear_port_feature(hdev, lvs->portnum,
151 USB_PORT_FEAT_SUSPEND);
153 dev_err(dev, "can't issue U3 exit %d\n", ret);
155 destroy_lvs_device(udev);
162 static DEVICE_ATTR_WO(u3_exit);
164 static ssize_t hot_reset_store(struct device *dev,
165 struct device_attribute *attr, const char *buf, size_t count)
167 struct usb_interface *intf = to_usb_interface(dev);
168 struct usb_device *hdev = interface_to_usbdev(intf);
169 struct lvs_rh *lvs = usb_get_intfdata(intf);
172 ret = lvs_rh_set_port_feature(hdev, lvs->portnum,
173 USB_PORT_FEAT_RESET);
175 dev_err(dev, "can't issue hot reset %d\n", ret);
181 static DEVICE_ATTR_WO(hot_reset);
183 static ssize_t u2_timeout_store(struct device *dev,
184 struct device_attribute *attr, const char *buf, size_t count)
186 struct usb_interface *intf = to_usb_interface(dev);
187 struct usb_device *hdev = interface_to_usbdev(intf);
188 struct lvs_rh *lvs = usb_get_intfdata(intf);
192 ret = kstrtoul(buf, 10, &val);
194 dev_err(dev, "couldn't parse string %d\n", ret);
198 if (val < 0 || val > 127)
201 ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
202 USB_PORT_FEAT_U2_TIMEOUT);
204 dev_err(dev, "Error %d while setting U2 timeout %ld\n", ret, val);
210 static DEVICE_ATTR_WO(u2_timeout);
212 static ssize_t u1_timeout_store(struct device *dev,
213 struct device_attribute *attr, const char *buf, size_t count)
215 struct usb_interface *intf = to_usb_interface(dev);
216 struct usb_device *hdev = interface_to_usbdev(intf);
217 struct lvs_rh *lvs = usb_get_intfdata(intf);
221 ret = kstrtoul(buf, 10, &val);
223 dev_err(dev, "couldn't parse string %d\n", ret);
227 if (val < 0 || val > 127)
230 ret = lvs_rh_set_port_feature(hdev, lvs->portnum | (val << 8),
231 USB_PORT_FEAT_U1_TIMEOUT);
233 dev_err(dev, "Error %d while setting U1 timeout %ld\n", ret, val);
239 static DEVICE_ATTR_WO(u1_timeout);
241 static ssize_t get_dev_desc_store(struct device *dev,
242 struct device_attribute *attr, const char *buf, size_t count)
244 struct usb_interface *intf = to_usb_interface(dev);
245 struct usb_device *udev;
246 struct usb_device_descriptor *descriptor;
249 descriptor = kmalloc(sizeof(*descriptor), GFP_KERNEL);
251 dev_err(dev, "failed to allocate descriptor memory\n");
255 udev = create_lvs_device(intf);
257 dev_err(dev, "failed to create lvs device\n");
262 ret = usb_control_msg(udev, (PIPE_CONTROL << 30) | USB_DIR_IN,
263 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, USB_DT_DEVICE << 8,
264 0, descriptor, sizeof(*descriptor),
265 USB_CTRL_GET_TIMEOUT);
267 dev_err(dev, "can't read device descriptor %d\n", ret);
269 destroy_lvs_device(udev);
279 static DEVICE_ATTR_WO(get_dev_desc);
281 static struct attribute *lvs_attributes[] = {
282 &dev_attr_get_dev_desc.attr,
283 &dev_attr_u1_timeout.attr,
284 &dev_attr_u2_timeout.attr,
285 &dev_attr_hot_reset.attr,
286 &dev_attr_u3_entry.attr,
287 &dev_attr_u3_exit.attr,
291 static const struct attribute_group lvs_attr_group = {
292 .attrs = lvs_attributes,
295 static void lvs_rh_work(struct work_struct *work)
297 struct lvs_rh *lvs = container_of(work, struct lvs_rh, rh_work);
298 struct usb_interface *intf = lvs->intf;
299 struct usb_device *hdev = interface_to_usbdev(intf);
300 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
301 struct usb_hub_descriptor *descriptor = &lvs->descriptor;
302 struct usb_port_status *port_status = &lvs->port_status;
306 /* Examine each root port */
307 for (i = 1; i <= descriptor->bNbrPorts; i++) {
308 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
309 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, i,
310 port_status, sizeof(*port_status), 1000);
314 portchange = le16_to_cpu(port_status->wPortChange);
316 if (portchange & USB_PORT_STAT_C_LINK_STATE)
317 lvs_rh_clear_port_feature(hdev, i,
318 USB_PORT_FEAT_C_PORT_LINK_STATE);
319 if (portchange & USB_PORT_STAT_C_ENABLE)
320 lvs_rh_clear_port_feature(hdev, i,
321 USB_PORT_FEAT_C_ENABLE);
322 if (portchange & USB_PORT_STAT_C_RESET)
323 lvs_rh_clear_port_feature(hdev, i,
324 USB_PORT_FEAT_C_RESET);
325 if (portchange & USB_PORT_STAT_C_BH_RESET)
326 lvs_rh_clear_port_feature(hdev, i,
327 USB_PORT_FEAT_C_BH_PORT_RESET);
328 if (portchange & USB_PORT_STAT_C_CONNECTION) {
329 lvs_rh_clear_port_feature(hdev, i,
330 USB_PORT_FEAT_C_CONNECTION);
332 if (le16_to_cpu(port_status->wPortStatus) &
333 USB_PORT_STAT_CONNECTION) {
337 usb_phy_notify_connect(hcd->usb_phy,
340 lvs->present = false;
342 usb_phy_notify_disconnect(hcd->usb_phy,
349 ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
350 if (ret != 0 && ret != -ENODEV && ret != -EPERM)
351 dev_err(&intf->dev, "urb resubmit error %d\n", ret);
354 static void lvs_rh_irq(struct urb *urb)
356 struct lvs_rh *lvs = urb->context;
358 queue_work(lvs->rh_queue, &lvs->rh_work);
361 static int lvs_rh_probe(struct usb_interface *intf,
362 const struct usb_device_id *id)
364 struct usb_device *hdev;
365 struct usb_host_interface *desc;
366 struct usb_endpoint_descriptor *endpoint;
371 hdev = interface_to_usbdev(intf);
372 desc = intf->cur_altsetting;
373 endpoint = &desc->endpoint[0].desc;
375 /* valid only for SS root hub */
376 if (hdev->descriptor.bDeviceProtocol != USB_HUB_PR_SS || hdev->parent) {
377 dev_err(&intf->dev, "Bind LVS driver with SS root Hub only\n");
381 lvs = devm_kzalloc(&intf->dev, sizeof(*lvs), GFP_KERNEL);
386 usb_set_intfdata(intf, lvs);
388 /* how many number of ports this root hub has */
389 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
390 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
391 USB_DT_SS_HUB << 8, 0, &lvs->descriptor,
392 USB_DT_SS_HUB_SIZE, USB_CTRL_GET_TIMEOUT);
393 if (ret < (USB_DT_HUB_NONVAR_SIZE + 2)) {
394 dev_err(&hdev->dev, "wrong root hub descriptor read %d\n", ret);
398 /* submit urb to poll interrupt endpoint */
399 lvs->urb = usb_alloc_urb(0, GFP_KERNEL);
401 dev_err(&intf->dev, "couldn't allocate lvs urb\n");
405 lvs->rh_queue = create_singlethread_workqueue("lvs_rh_queue");
406 if (!lvs->rh_queue) {
407 dev_err(&intf->dev, "couldn't create workqueue\n");
412 INIT_WORK(&lvs->rh_work, lvs_rh_work);
414 ret = sysfs_create_group(&intf->dev.kobj, &lvs_attr_group);
416 dev_err(&intf->dev, "Failed to create sysfs node %d\n", ret);
420 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
421 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
422 usb_fill_int_urb(lvs->urb, hdev, pipe, &lvs->buffer[0], maxp,
423 lvs_rh_irq, lvs, endpoint->bInterval);
425 ret = usb_submit_urb(lvs->urb, GFP_KERNEL);
427 dev_err(&intf->dev, "couldn't submit lvs urb %d\n", ret);
434 sysfs_remove_group(&intf->dev.kobj, &lvs_attr_group);
436 destroy_workqueue(lvs->rh_queue);
438 usb_free_urb(lvs->urb);
442 static void lvs_rh_disconnect(struct usb_interface *intf)
444 struct lvs_rh *lvs = usb_get_intfdata(intf);
446 sysfs_remove_group(&intf->dev.kobj, &lvs_attr_group);
447 destroy_workqueue(lvs->rh_queue);
448 usb_free_urb(lvs->urb);
451 static struct usb_driver lvs_driver = {
453 .probe = lvs_rh_probe,
454 .disconnect = lvs_rh_disconnect,
457 module_usb_driver(lvs_driver);
459 MODULE_DESCRIPTION("Link Layer Validation System Driver");
460 MODULE_LICENSE("GPL");