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>
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
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
);
189 ret
= lvs_rh_set_port_feature(hdev
, lvs
->portnum
,
190 USB_PORT_FEAT_BH_PORT_RESET
);
192 dev_err(dev
, "can't issue warm reset %d\n", ret
);
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
);
209 ret
= kstrtoul(buf
, 10, &val
);
211 dev_err(dev
, "couldn't parse string %d\n", ret
);
218 ret
= lvs_rh_set_port_feature(hdev
, lvs
->portnum
| (val
<< 8),
219 USB_PORT_FEAT_U2_TIMEOUT
);
221 dev_err(dev
, "Error %d while setting U2 timeout %ld\n", ret
, val
);
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
);
238 ret
= kstrtoul(buf
, 10, &val
);
240 dev_err(dev
, "couldn't parse string %d\n", ret
);
247 ret
= lvs_rh_set_port_feature(hdev
, lvs
->portnum
| (val
<< 8),
248 USB_PORT_FEAT_U1_TIMEOUT
);
250 dev_err(dev
, "Error %d while setting U1 timeout %ld\n", ret
, val
);
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
;
266 descriptor
= kmalloc(sizeof(*descriptor
), GFP_KERNEL
);
270 udev
= create_lvs_device(intf
);
272 dev_err(dev
, "failed to create lvs device\n");
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
);
282 dev_err(dev
, "can't read device descriptor %d\n", ret
);
284 destroy_lvs_device(udev
);
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
);
304 ret
= lvs_rh_set_port_feature(hdev
,
305 lvs
->portnum
| USB_SS_PORT_LS_COMP_MOD
<< 3,
306 USB_PORT_FEAT_LINK_STATE
);
308 dev_err(dev
, "can't enable compliance mode %d\n", ret
);
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
,
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
;
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);
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
) {
374 usb_phy_notify_connect(hcd
->usb_phy
,
377 lvs
->present
= false;
379 usb_phy_notify_disconnect(hcd
->usb_phy
,
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
;
408 hdev
= interface_to_usbdev(intf
);
409 desc
= intf
->cur_altsetting
;
411 ret
= usb_find_int_in_endpoint(desc
, &endpoint
);
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");
421 lvs
= devm_kzalloc(&intf
->dev
, sizeof(*lvs
), GFP_KERNEL
);
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
);
438 /* submit urb to poll interrupt endpoint */
439 lvs
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
443 INIT_WORK(&lvs
->rh_work
, lvs_rh_work
);
445 ret
= sysfs_create_group(&intf
->dev
.kobj
, &lvs_attr_group
);
447 dev_err(&intf
->dev
, "Failed to create sysfs node %d\n", ret
);
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
);
458 dev_err(&intf
->dev
, "couldn't submit lvs urb %d\n", ret
);
465 sysfs_remove_group(&intf
->dev
.kobj
, &lvs_attr_group
);
467 usb_free_urb(lvs
->urb
);
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
= {
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");