1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/usb/misc/lvstest.c
5 * Test pattern generation for Link Layer Validation System Tests
7 * Copyright (C) 2014 ST Microelectronics
8 * Pratyush Anand <pratyush.anand@gmail.com>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/usb.h>
17 #include <linux/usb/ch11.h>
18 #include <linux/usb/hcd.h>
19 #include <linux/usb/phy.h>
22 /* root hub interface */
23 struct usb_interface
*intf
;
24 /* if lvs device connected */
26 /* port no at which lvs device is present */
30 /* class descriptor */
31 struct usb_hub_descriptor descriptor
;
32 /* urb for polling interrupt pipe */
35 struct work_struct rh_work
;
37 struct usb_port_status port_status
;
40 static struct usb_device
*create_lvs_device(struct usb_interface
*intf
)
42 struct usb_device
*udev
, *hdev
;
44 struct lvs_rh
*lvs
= usb_get_intfdata(intf
);
47 dev_err(&intf
->dev
, "No LVS device is present\n");
51 hdev
= interface_to_usbdev(intf
);
52 hcd
= bus_to_hcd(hdev
->bus
);
54 udev
= usb_alloc_dev(hdev
, hdev
->bus
, lvs
->portnum
);
56 dev_err(&intf
->dev
, "Could not allocate lvs udev\n");
59 udev
->speed
= USB_SPEED_SUPER
;
60 udev
->ep0
.desc
.wMaxPacketSize
= cpu_to_le16(512);
61 usb_set_device_state(udev
, USB_STATE_DEFAULT
);
63 if (hcd
->driver
->enable_device
) {
64 if (hcd
->driver
->enable_device(hcd
, udev
) < 0) {
65 dev_err(&intf
->dev
, "Failed to enable\n");
74 static void destroy_lvs_device(struct usb_device
*udev
)
76 struct usb_device
*hdev
= udev
->parent
;
77 struct usb_hcd
*hcd
= bus_to_hcd(hdev
->bus
);
79 if (hcd
->driver
->free_dev
)
80 hcd
->driver
->free_dev(hcd
, udev
);
85 static int lvs_rh_clear_port_feature(struct usb_device
*hdev
,
86 int port1
, int feature
)
88 return usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
89 USB_REQ_CLEAR_FEATURE
, USB_RT_PORT
, feature
, port1
,
93 static int lvs_rh_set_port_feature(struct usb_device
*hdev
,
94 int port1
, int feature
)
96 return usb_control_msg(hdev
, usb_sndctrlpipe(hdev
, 0),
97 USB_REQ_SET_FEATURE
, USB_RT_PORT
, feature
, port1
,
101 static ssize_t
u3_entry_store(struct device
*dev
,
102 struct device_attribute
*attr
, const char *buf
, size_t count
)
104 struct usb_interface
*intf
= to_usb_interface(dev
);
105 struct usb_device
*hdev
= interface_to_usbdev(intf
);
106 struct lvs_rh
*lvs
= usb_get_intfdata(intf
);
107 struct usb_device
*udev
;
110 udev
= create_lvs_device(intf
);
112 dev_err(dev
, "failed to create lvs device\n");
116 ret
= lvs_rh_set_port_feature(hdev
, lvs
->portnum
,
117 USB_PORT_FEAT_SUSPEND
);
119 dev_err(dev
, "can't issue U3 entry %d\n", ret
);
121 destroy_lvs_device(udev
);
128 static DEVICE_ATTR_WO(u3_entry
);
130 static ssize_t
u3_exit_store(struct device
*dev
,
131 struct device_attribute
*attr
, const char *buf
, size_t count
)
133 struct usb_interface
*intf
= to_usb_interface(dev
);
134 struct usb_device
*hdev
= interface_to_usbdev(intf
);
135 struct lvs_rh
*lvs
= usb_get_intfdata(intf
);
136 struct usb_device
*udev
;
139 udev
= create_lvs_device(intf
);
141 dev_err(dev
, "failed to create lvs device\n");
145 ret
= lvs_rh_clear_port_feature(hdev
, lvs
->portnum
,
146 USB_PORT_FEAT_SUSPEND
);
148 dev_err(dev
, "can't issue U3 exit %d\n", ret
);
150 destroy_lvs_device(udev
);
157 static DEVICE_ATTR_WO(u3_exit
);
159 static ssize_t
hot_reset_store(struct device
*dev
,
160 struct device_attribute
*attr
, const char *buf
, size_t count
)
162 struct usb_interface
*intf
= to_usb_interface(dev
);
163 struct usb_device
*hdev
= interface_to_usbdev(intf
);
164 struct lvs_rh
*lvs
= usb_get_intfdata(intf
);
167 ret
= lvs_rh_set_port_feature(hdev
, lvs
->portnum
,
168 USB_PORT_FEAT_RESET
);
170 dev_err(dev
, "can't issue hot reset %d\n", ret
);
176 static DEVICE_ATTR_WO(hot_reset
);
178 static ssize_t
warm_reset_store(struct device
*dev
,
179 struct device_attribute
*attr
, const char *buf
, size_t count
)
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
);
186 ret
= lvs_rh_set_port_feature(hdev
, lvs
->portnum
,
187 USB_PORT_FEAT_BH_PORT_RESET
);
189 dev_err(dev
, "can't issue warm reset %d\n", ret
);
195 static DEVICE_ATTR_WO(warm_reset
);
197 static ssize_t
u2_timeout_store(struct device
*dev
,
198 struct device_attribute
*attr
, const char *buf
, size_t count
)
200 struct usb_interface
*intf
= to_usb_interface(dev
);
201 struct usb_device
*hdev
= interface_to_usbdev(intf
);
202 struct lvs_rh
*lvs
= usb_get_intfdata(intf
);
206 ret
= kstrtoul(buf
, 10, &val
);
208 dev_err(dev
, "couldn't parse string %d\n", ret
);
215 ret
= lvs_rh_set_port_feature(hdev
, lvs
->portnum
| (val
<< 8),
216 USB_PORT_FEAT_U2_TIMEOUT
);
218 dev_err(dev
, "Error %d while setting U2 timeout %ld\n", ret
, val
);
224 static DEVICE_ATTR_WO(u2_timeout
);
226 static ssize_t
u1_timeout_store(struct device
*dev
,
227 struct device_attribute
*attr
, const char *buf
, size_t count
)
229 struct usb_interface
*intf
= to_usb_interface(dev
);
230 struct usb_device
*hdev
= interface_to_usbdev(intf
);
231 struct lvs_rh
*lvs
= usb_get_intfdata(intf
);
235 ret
= kstrtoul(buf
, 10, &val
);
237 dev_err(dev
, "couldn't parse string %d\n", ret
);
244 ret
= lvs_rh_set_port_feature(hdev
, lvs
->portnum
| (val
<< 8),
245 USB_PORT_FEAT_U1_TIMEOUT
);
247 dev_err(dev
, "Error %d while setting U1 timeout %ld\n", ret
, val
);
253 static DEVICE_ATTR_WO(u1_timeout
);
255 static ssize_t
get_dev_desc_store(struct device
*dev
,
256 struct device_attribute
*attr
, const char *buf
, size_t count
)
258 struct usb_interface
*intf
= to_usb_interface(dev
);
259 struct usb_device
*udev
;
260 struct usb_device_descriptor
*descriptor
;
263 descriptor
= kmalloc(sizeof(*descriptor
), GFP_KERNEL
);
267 udev
= create_lvs_device(intf
);
269 dev_err(dev
, "failed to create lvs device\n");
274 ret
= usb_control_msg(udev
, (PIPE_CONTROL
<< 30) | USB_DIR_IN
,
275 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
, USB_DT_DEVICE
<< 8,
276 0, descriptor
, sizeof(*descriptor
),
277 USB_CTRL_GET_TIMEOUT
);
279 dev_err(dev
, "can't read device descriptor %d\n", ret
);
281 destroy_lvs_device(udev
);
291 static DEVICE_ATTR_WO(get_dev_desc
);
293 static ssize_t
enable_compliance_store(struct device
*dev
,
294 struct device_attribute
*attr
, const char *buf
, size_t count
)
296 struct usb_interface
*intf
= to_usb_interface(dev
);
297 struct usb_device
*hdev
= interface_to_usbdev(intf
);
298 struct lvs_rh
*lvs
= usb_get_intfdata(intf
);
301 ret
= lvs_rh_set_port_feature(hdev
,
302 lvs
->portnum
| USB_SS_PORT_LS_COMP_MOD
<< 3,
303 USB_PORT_FEAT_LINK_STATE
);
305 dev_err(dev
, "can't enable compliance mode %d\n", ret
);
311 static DEVICE_ATTR_WO(enable_compliance
);
313 static struct attribute
*lvs_attributes
[] = {
314 &dev_attr_get_dev_desc
.attr
,
315 &dev_attr_u1_timeout
.attr
,
316 &dev_attr_u2_timeout
.attr
,
317 &dev_attr_hot_reset
.attr
,
318 &dev_attr_warm_reset
.attr
,
319 &dev_attr_u3_entry
.attr
,
320 &dev_attr_u3_exit
.attr
,
321 &dev_attr_enable_compliance
.attr
,
325 static const struct attribute_group lvs_attr_group
= {
326 .attrs
= lvs_attributes
,
329 static void lvs_rh_work(struct work_struct
*work
)
331 struct lvs_rh
*lvs
= container_of(work
, struct lvs_rh
, rh_work
);
332 struct usb_interface
*intf
= lvs
->intf
;
333 struct usb_device
*hdev
= interface_to_usbdev(intf
);
334 struct usb_hcd
*hcd
= bus_to_hcd(hdev
->bus
);
335 struct usb_hub_descriptor
*descriptor
= &lvs
->descriptor
;
336 struct usb_port_status
*port_status
= &lvs
->port_status
;
340 /* Examine each root port */
341 for (i
= 1; i
<= descriptor
->bNbrPorts
; i
++) {
342 ret
= usb_control_msg(hdev
, usb_rcvctrlpipe(hdev
, 0),
343 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RT_PORT
, 0, i
,
344 port_status
, sizeof(*port_status
), 1000);
348 portchange
= le16_to_cpu(port_status
->wPortChange
);
350 if (portchange
& USB_PORT_STAT_C_LINK_STATE
)
351 lvs_rh_clear_port_feature(hdev
, i
,
352 USB_PORT_FEAT_C_PORT_LINK_STATE
);
353 if (portchange
& USB_PORT_STAT_C_ENABLE
)
354 lvs_rh_clear_port_feature(hdev
, i
,
355 USB_PORT_FEAT_C_ENABLE
);
356 if (portchange
& USB_PORT_STAT_C_RESET
)
357 lvs_rh_clear_port_feature(hdev
, i
,
358 USB_PORT_FEAT_C_RESET
);
359 if (portchange
& USB_PORT_STAT_C_BH_RESET
)
360 lvs_rh_clear_port_feature(hdev
, i
,
361 USB_PORT_FEAT_C_BH_PORT_RESET
);
362 if (portchange
& USB_PORT_STAT_C_CONNECTION
) {
363 lvs_rh_clear_port_feature(hdev
, i
,
364 USB_PORT_FEAT_C_CONNECTION
);
366 if (le16_to_cpu(port_status
->wPortStatus
) &
367 USB_PORT_STAT_CONNECTION
) {
371 usb_phy_notify_connect(hcd
->usb_phy
,
374 lvs
->present
= false;
376 usb_phy_notify_disconnect(hcd
->usb_phy
,
383 ret
= usb_submit_urb(lvs
->urb
, GFP_KERNEL
);
384 if (ret
!= 0 && ret
!= -ENODEV
&& ret
!= -EPERM
)
385 dev_err(&intf
->dev
, "urb resubmit error %d\n", ret
);
388 static void lvs_rh_irq(struct urb
*urb
)
390 struct lvs_rh
*lvs
= urb
->context
;
392 schedule_work(&lvs
->rh_work
);
395 static int lvs_rh_probe(struct usb_interface
*intf
,
396 const struct usb_device_id
*id
)
398 struct usb_device
*hdev
;
399 struct usb_host_interface
*desc
;
400 struct usb_endpoint_descriptor
*endpoint
;
405 hdev
= interface_to_usbdev(intf
);
406 desc
= intf
->cur_altsetting
;
408 ret
= usb_find_int_in_endpoint(desc
, &endpoint
);
412 /* valid only for SS root hub */
413 if (hdev
->descriptor
.bDeviceProtocol
!= USB_HUB_PR_SS
|| hdev
->parent
) {
414 dev_err(&intf
->dev
, "Bind LVS driver with SS root Hub only\n");
418 lvs
= devm_kzalloc(&intf
->dev
, sizeof(*lvs
), GFP_KERNEL
);
423 usb_set_intfdata(intf
, lvs
);
425 /* how many number of ports this root hub has */
426 ret
= usb_control_msg(hdev
, usb_rcvctrlpipe(hdev
, 0),
427 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
| USB_RT_HUB
,
428 USB_DT_SS_HUB
<< 8, 0, &lvs
->descriptor
,
429 USB_DT_SS_HUB_SIZE
, USB_CTRL_GET_TIMEOUT
);
430 if (ret
< (USB_DT_HUB_NONVAR_SIZE
+ 2)) {
431 dev_err(&hdev
->dev
, "wrong root hub descriptor read %d\n", ret
);
435 /* submit urb to poll interrupt endpoint */
436 lvs
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
440 INIT_WORK(&lvs
->rh_work
, lvs_rh_work
);
442 ret
= sysfs_create_group(&intf
->dev
.kobj
, &lvs_attr_group
);
444 dev_err(&intf
->dev
, "Failed to create sysfs node %d\n", ret
);
448 pipe
= usb_rcvintpipe(hdev
, endpoint
->bEndpointAddress
);
449 maxp
= usb_maxpacket(hdev
, pipe
, usb_pipeout(pipe
));
450 usb_fill_int_urb(lvs
->urb
, hdev
, pipe
, &lvs
->buffer
[0], maxp
,
451 lvs_rh_irq
, lvs
, endpoint
->bInterval
);
453 ret
= usb_submit_urb(lvs
->urb
, GFP_KERNEL
);
455 dev_err(&intf
->dev
, "couldn't submit lvs urb %d\n", ret
);
462 sysfs_remove_group(&intf
->dev
.kobj
, &lvs_attr_group
);
464 usb_free_urb(lvs
->urb
);
468 static void lvs_rh_disconnect(struct usb_interface
*intf
)
470 struct lvs_rh
*lvs
= usb_get_intfdata(intf
);
472 sysfs_remove_group(&intf
->dev
.kobj
, &lvs_attr_group
);
473 usb_poison_urb(lvs
->urb
); /* used in scheduled work */
474 flush_work(&lvs
->rh_work
);
475 usb_free_urb(lvs
->urb
);
478 static struct usb_driver lvs_driver
= {
480 .probe
= lvs_rh_probe
,
481 .disconnect
= lvs_rh_disconnect
,
484 module_usb_driver(lvs_driver
);
486 MODULE_DESCRIPTION("Link Layer Validation System Driver");
487 MODULE_LICENSE("GPL");