aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/misc/usbtest.c
diff options
context:
space:
mode:
authorDeepa Dinamani <deepa.kernel@gmail.com>2015-11-04 18:29:11 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2015-12-01 17:52:58 -0500
commit18fc4ebdc7057478497682047db5acfb25b35c49 (patch)
tree24f99fa4fb757a4ebbbdd9da07bdfe8b48f75c23 /drivers/usb/misc/usbtest.c
parentec4dca8bdfb650fd698401e26f586683ec69a942 (diff)
usb: misc: usbtest: Remove timeval usage
timeval is deprecated and not y2038 safe. Its size also changes according to 32 bit/ 64 bit compilation. Replace it with 32 and 64 bit versions of its individual fields, giving two ioctls with different code values. The two ioctls are necessary to maintain the 32 bit and 64 bit userspace compatibility with a 64/32 bit kernel. Change unsigned to __u32 types for a definitive userspace interface. This is in accordance with the psABI that the unsigned type is always 32 bits. Also use motonic timer instead of real time to ensure positive delta values. Refactor usbtest_ioctl for readability to isolate the handling of the testing timing measurement. The official testusb userspace tool can be changed in a separate patch to reflect the __u32 changes as well. It can use the usbtest_param_32 struct, since 32 bit seconds is long enough for test durations. Signed-off-by: Deepa Dinamani <deepa.kernel@gmail.com> Reviewed-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/usb/misc/usbtest.c')
-rw-r--r--drivers/usb/misc/usbtest.c229
1 files changed, 147 insertions, 82 deletions
diff --git a/drivers/usb/misc/usbtest.c b/drivers/usb/misc/usbtest.c
index c1bd1eb548bb..92fdb6e9faff 100644
--- a/drivers/usb/misc/usbtest.c
+++ b/drivers/usb/misc/usbtest.c
@@ -22,18 +22,42 @@ static void complicated_callback(struct urb *urb);
22/*-------------------------------------------------------------------------*/ 22/*-------------------------------------------------------------------------*/
23 23
24/* FIXME make these public somewhere; usbdevfs.h? */ 24/* FIXME make these public somewhere; usbdevfs.h? */
25struct usbtest_param { 25
26/* Parameter for usbtest driver. */
27struct usbtest_param_32 {
26 /* inputs */ 28 /* inputs */
27 unsigned test_num; /* 0..(TEST_CASES-1) */ 29 __u32 test_num; /* 0..(TEST_CASES-1) */
28 unsigned iterations; 30 __u32 iterations;
29 unsigned length; 31 __u32 length;
30 unsigned vary; 32 __u32 vary;
31 unsigned sglen; 33 __u32 sglen;
32 34
33 /* outputs */ 35 /* outputs */
34 struct timeval duration; 36 __s32 duration_sec;
37 __s32 duration_usec;
35}; 38};
36#define USBTEST_REQUEST _IOWR('U', 100, struct usbtest_param) 39
40/*
41 * Compat parameter to the usbtest driver.
42 * This supports older user space binaries compiled with 64 bit compiler.
43 */
44struct usbtest_param_64 {
45 /* inputs */
46 __u32 test_num; /* 0..(TEST_CASES-1) */
47 __u32 iterations;
48 __u32 length;
49 __u32 vary;
50 __u32 sglen;
51
52 /* outputs */
53 __s64 duration_sec;
54 __s64 duration_usec;
55};
56
57/* IOCTL interface to the driver. */
58#define USBTEST_REQUEST_32 _IOWR('U', 100, struct usbtest_param_32)
59/* COMPAT IOCTL interface to the driver. */
60#define USBTEST_REQUEST_64 _IOWR('U', 100, struct usbtest_param_64)
37 61
38/*-------------------------------------------------------------------------*/ 62/*-------------------------------------------------------------------------*/
39 63
@@ -1030,7 +1054,7 @@ struct ctrl_ctx {
1030 unsigned pending; 1054 unsigned pending;
1031 int status; 1055 int status;
1032 struct urb **urb; 1056 struct urb **urb;
1033 struct usbtest_param *param; 1057 struct usbtest_param_32 *param;
1034 int last; 1058 int last;
1035}; 1059};
1036 1060
@@ -1155,7 +1179,7 @@ error:
1155} 1179}
1156 1180
1157static int 1181static int
1158test_ctrl_queue(struct usbtest_dev *dev, struct usbtest_param *param) 1182test_ctrl_queue(struct usbtest_dev *dev, struct usbtest_param_32 *param)
1159{ 1183{
1160 struct usb_device *udev = testdev_to_usbdev(dev); 1184 struct usb_device *udev = testdev_to_usbdev(dev);
1161 struct urb **urb; 1185 struct urb **urb;
@@ -1930,7 +1954,7 @@ static struct urb *iso_alloc_urb(
1930} 1954}
1931 1955
1932static int 1956static int
1933test_queue(struct usbtest_dev *dev, struct usbtest_param *param, 1957test_queue(struct usbtest_dev *dev, struct usbtest_param_32 *param,
1934 int pipe, struct usb_endpoint_descriptor *desc, unsigned offset) 1958 int pipe, struct usb_endpoint_descriptor *desc, unsigned offset)
1935{ 1959{
1936 struct transfer_context context; 1960 struct transfer_context context;
@@ -2049,81 +2073,20 @@ static int test_unaligned_bulk(
2049 return retval; 2073 return retval;
2050} 2074}
2051 2075
2052/*-------------------------------------------------------------------------*/ 2076/* Run tests. */
2053
2054/* We only have this one interface to user space, through usbfs.
2055 * User mode code can scan usbfs to find N different devices (maybe on
2056 * different busses) to use when testing, and allocate one thread per
2057 * test. So discovery is simplified, and we have no device naming issues.
2058 *
2059 * Don't use these only as stress/load tests. Use them along with with
2060 * other USB bus activity: plugging, unplugging, mousing, mp3 playback,
2061 * video capture, and so on. Run different tests at different times, in
2062 * different sequences. Nothing here should interact with other devices,
2063 * except indirectly by consuming USB bandwidth and CPU resources for test
2064 * threads and request completion. But the only way to know that for sure
2065 * is to test when HC queues are in use by many devices.
2066 *
2067 * WARNING: Because usbfs grabs udev->dev.sem before calling this ioctl(),
2068 * it locks out usbcore in certain code paths. Notably, if you disconnect
2069 * the device-under-test, hub_wq will wait block forever waiting for the
2070 * ioctl to complete ... so that usb_disconnect() can abort the pending
2071 * urbs and then call usbtest_disconnect(). To abort a test, you're best
2072 * off just killing the userspace task and waiting for it to exit.
2073 */
2074
2075static int 2077static int
2076usbtest_ioctl(struct usb_interface *intf, unsigned int code, void *buf) 2078usbtest_do_ioctl(struct usb_interface *intf, struct usbtest_param_32 *param)
2077{ 2079{
2078 struct usbtest_dev *dev = usb_get_intfdata(intf); 2080 struct usbtest_dev *dev = usb_get_intfdata(intf);
2079 struct usb_device *udev = testdev_to_usbdev(dev); 2081 struct usb_device *udev = testdev_to_usbdev(dev);
2080 struct usbtest_param *param = buf;
2081 int retval = -EOPNOTSUPP;
2082 struct urb *urb; 2082 struct urb *urb;
2083 struct scatterlist *sg; 2083 struct scatterlist *sg;
2084 struct usb_sg_request req; 2084 struct usb_sg_request req;
2085 struct timeval start;
2086 unsigned i; 2085 unsigned i;
2087 2086 int retval = -EOPNOTSUPP;
2088 /* FIXME USBDEVFS_CONNECTINFO doesn't say how fast the device is. */
2089
2090 pattern = mod_pattern;
2091
2092 if (code != USBTEST_REQUEST)
2093 return -EOPNOTSUPP;
2094 2087
2095 if (param->iterations <= 0) 2088 if (param->iterations <= 0)
2096 return -EINVAL; 2089 return -EINVAL;
2097
2098 if (param->sglen > MAX_SGLEN)
2099 return -EINVAL;
2100
2101 if (mutex_lock_interruptible(&dev->lock))
2102 return -ERESTARTSYS;
2103
2104 /* FIXME: What if a system sleep starts while a test is running? */
2105
2106 /* some devices, like ez-usb default devices, need a non-default
2107 * altsetting to have any active endpoints. some tests change
2108 * altsettings; force a default so most tests don't need to check.
2109 */
2110 if (dev->info->alt >= 0) {
2111 int res;
2112
2113 if (intf->altsetting->desc.bInterfaceNumber) {
2114 mutex_unlock(&dev->lock);
2115 return -ENODEV;
2116 }
2117 res = set_altsetting(dev, dev->info->alt);
2118 if (res) {
2119 dev_err(&intf->dev,
2120 "set altsetting to %d failed, %d\n",
2121 dev->info->alt, res);
2122 mutex_unlock(&dev->lock);
2123 return res;
2124 }
2125 }
2126
2127 /* 2090 /*
2128 * Just a bunch of test cases that every HCD is expected to handle. 2091 * Just a bunch of test cases that every HCD is expected to handle.
2129 * 2092 *
@@ -2133,7 +2096,6 @@ usbtest_ioctl(struct usb_interface *intf, unsigned int code, void *buf)
2133 * FIXME add more tests! cancel requests, verify the data, control 2096 * FIXME add more tests! cancel requests, verify the data, control
2134 * queueing, concurrent read+write threads, and so on. 2097 * queueing, concurrent read+write threads, and so on.
2135 */ 2098 */
2136 do_gettimeofday(&start);
2137 switch (param->test_num) { 2099 switch (param->test_num) {
2138 2100
2139 case 0: 2101 case 0:
@@ -2548,13 +2510,116 @@ usbtest_ioctl(struct usb_interface *intf, unsigned int code, void *buf)
2548 dev->in_pipe, NULL, 0); 2510 dev->in_pipe, NULL, 0);
2549 break; 2511 break;
2550 } 2512 }
2551 do_gettimeofday(&param->duration); 2513 return retval;
2552 param->duration.tv_sec -= start.tv_sec; 2514}
2553 param->duration.tv_usec -= start.tv_usec; 2515
2554 if (param->duration.tv_usec < 0) { 2516/*-------------------------------------------------------------------------*/
2555 param->duration.tv_usec += 1000 * 1000; 2517
2556 param->duration.tv_sec -= 1; 2518/* We only have this one interface to user space, through usbfs.
2519 * User mode code can scan usbfs to find N different devices (maybe on
2520 * different busses) to use when testing, and allocate one thread per
2521 * test. So discovery is simplified, and we have no device naming issues.
2522 *
2523 * Don't use these only as stress/load tests. Use them along with with
2524 * other USB bus activity: plugging, unplugging, mousing, mp3 playback,
2525 * video capture, and so on. Run different tests at different times, in
2526 * different sequences. Nothing here should interact with other devices,
2527 * except indirectly by consuming USB bandwidth and CPU resources for test
2528 * threads and request completion. But the only way to know that for sure
2529 * is to test when HC queues are in use by many devices.
2530 *
2531 * WARNING: Because usbfs grabs udev->dev.sem before calling this ioctl(),
2532 * it locks out usbcore in certain code paths. Notably, if you disconnect
2533 * the device-under-test, hub_wq will wait block forever waiting for the
2534 * ioctl to complete ... so that usb_disconnect() can abort the pending
2535 * urbs and then call usbtest_disconnect(). To abort a test, you're best
2536 * off just killing the userspace task and waiting for it to exit.
2537 */
2538
2539static int
2540usbtest_ioctl(struct usb_interface *intf, unsigned int code, void *buf)
2541{
2542
2543 struct usbtest_dev *dev = usb_get_intfdata(intf);
2544 struct usbtest_param_64 *param_64 = buf;
2545 struct usbtest_param_32 temp;
2546 struct usbtest_param_32 *param_32 = buf;
2547 struct timespec64 start;
2548 struct timespec64 end;
2549 struct timespec64 duration;
2550 int retval = -EOPNOTSUPP;
2551
2552 /* FIXME USBDEVFS_CONNECTINFO doesn't say how fast the device is. */
2553
2554 pattern = mod_pattern;
2555
2556 if (mutex_lock_interruptible(&dev->lock))
2557 return -ERESTARTSYS;
2558
2559 /* FIXME: What if a system sleep starts while a test is running? */
2560
2561 /* some devices, like ez-usb default devices, need a non-default
2562 * altsetting to have any active endpoints. some tests change
2563 * altsettings; force a default so most tests don't need to check.
2564 */
2565 if (dev->info->alt >= 0) {
2566 if (intf->altsetting->desc.bInterfaceNumber) {
2567 retval = -ENODEV;
2568 goto free_mutex;
2569 }
2570 retval = set_altsetting(dev, dev->info->alt);
2571 if (retval) {
2572 dev_err(&intf->dev,
2573 "set altsetting to %d failed, %d\n",
2574 dev->info->alt, retval);
2575 goto free_mutex;
2576 }
2577 }
2578
2579 switch (code) {
2580 case USBTEST_REQUEST_64:
2581 temp.test_num = param_64->test_num;
2582 temp.iterations = param_64->iterations;
2583 temp.length = param_64->length;
2584 temp.sglen = param_64->sglen;
2585 temp.vary = param_64->vary;
2586 param_32 = &temp;
2587 break;
2588
2589 case USBTEST_REQUEST_32:
2590 break;
2591
2592 default:
2593 retval = -EOPNOTSUPP;
2594 goto free_mutex;
2595 }
2596
2597 ktime_get_ts64(&start);
2598
2599 retval = usbtest_do_ioctl(intf, param_32);
2600 if (retval)
2601 goto free_mutex;
2602
2603 ktime_get_ts64(&end);
2604
2605 duration = timespec64_sub(end, start);
2606
2607 temp.duration_sec = duration.tv_sec;
2608 temp.duration_usec = duration.tv_nsec/NSEC_PER_USEC;
2609
2610 switch (code) {
2611 case USBTEST_REQUEST_32:
2612 param_32->duration_sec = temp.duration_sec;
2613 param_32->duration_usec = temp.duration_usec;
2614 break;
2615
2616 case USBTEST_REQUEST_64:
2617 param_64->duration_sec = temp.duration_sec;
2618 param_64->duration_usec = temp.duration_usec;
2619 break;
2557 } 2620 }
2621
2622free_mutex:
2558 mutex_unlock(&dev->lock); 2623 mutex_unlock(&dev->lock);
2559 return retval; 2624 return retval;
2560} 2625}