1 | /* $Id: UsbTest.cpp 99739 2023-05-11 01:01:08Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * UsbTest - User frontend for the Linux usbtest USB test and benchmarking module.
|
---|
4 | * Integrates with our test framework for nice outputs.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2014-2023 Oracle and/or its affiliates.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox base platform packages, as
|
---|
11 | * available from https://www.virtualbox.org.
|
---|
12 | *
|
---|
13 | * This program is free software; you can redistribute it and/or
|
---|
14 | * modify it under the terms of the GNU General Public License
|
---|
15 | * as published by the Free Software Foundation, in version 3 of the
|
---|
16 | * License.
|
---|
17 | *
|
---|
18 | * This program is distributed in the hope that it will be useful, but
|
---|
19 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
21 | * General Public License for more details.
|
---|
22 | *
|
---|
23 | * You should have received a copy of the GNU General Public License
|
---|
24 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
25 | *
|
---|
26 | * The contents of this file may alternatively be used under the terms
|
---|
27 | * of the Common Development and Distribution License Version 1.0
|
---|
28 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
29 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
30 | * CDDL are applicable instead of those of the GPL.
|
---|
31 | *
|
---|
32 | * You may elect to license modified versions of this file under the
|
---|
33 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
34 | *
|
---|
35 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
36 | */
|
---|
37 |
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Header Files *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 | #include <iprt/dir.h>
|
---|
43 | #include <iprt/err.h>
|
---|
44 | #include <iprt/file.h>
|
---|
45 | #include <iprt/getopt.h>
|
---|
46 | #include <iprt/path.h>
|
---|
47 | #include <iprt/param.h>
|
---|
48 | #include <iprt/process.h>
|
---|
49 | #include <iprt/stream.h>
|
---|
50 | #include <iprt/string.h>
|
---|
51 | #include <iprt/test.h>
|
---|
52 |
|
---|
53 | #include <iprt/linux/sysfs.h>
|
---|
54 |
|
---|
55 | #include <unistd.h>
|
---|
56 | #include <errno.h>
|
---|
57 | #include <limits.h>
|
---|
58 |
|
---|
59 | #include <sys/types.h>
|
---|
60 | #include <sys/stat.h>
|
---|
61 | #include <fcntl.h>
|
---|
62 |
|
---|
63 | #include <sys/ioctl.h>
|
---|
64 | #include <linux/usbdevice_fs.h>
|
---|
65 |
|
---|
66 |
|
---|
67 | /*********************************************************************************************************************************
|
---|
68 | * Defined Constants And Macros *
|
---|
69 | *********************************************************************************************************************************/
|
---|
70 |
|
---|
71 |
|
---|
72 | /*********************************************************************************************************************************
|
---|
73 | * Structures and Typedefs *
|
---|
74 | *********************************************************************************************************************************/
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * USB test request data.
|
---|
78 | * There is no public header with this information so we define it ourself here.
|
---|
79 | */
|
---|
80 | typedef struct USBTESTPARMS
|
---|
81 | {
|
---|
82 | /** Specifies the test to run. */
|
---|
83 | uint32_t idxTest;
|
---|
84 | /** How many iterations the test should be executed. */
|
---|
85 | uint32_t cIterations;
|
---|
86 | /** Size of the data packets. */
|
---|
87 | uint32_t cbData;
|
---|
88 | /** Size of */
|
---|
89 | uint32_t cbVariation;
|
---|
90 | /** Length of the S/G list for the test. */
|
---|
91 | uint32_t cSgLength;
|
---|
92 | /** Returned time data after completing the test. */
|
---|
93 | struct timeval TimeTest;
|
---|
94 | } USBTESTPARAMS;
|
---|
95 | /** Pointer to a test parameter structure. */
|
---|
96 | typedef USBTESTPARAMS *PUSBTESTPARAMS;
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * USB device descriptor. Used to search for the test device based
|
---|
100 | * on the vendor and product id.
|
---|
101 | */
|
---|
102 | #pragma pack(1)
|
---|
103 | typedef struct USBDEVDESC
|
---|
104 | {
|
---|
105 | uint8_t bLength;
|
---|
106 | uint8_t bDescriptorType;
|
---|
107 | uint16_t bcdUSB;
|
---|
108 | uint8_t bDeviceClass;
|
---|
109 | uint8_t bDeviceSubClass;
|
---|
110 | uint8_t bDeviceProtocol;
|
---|
111 | uint8_t bMaxPacketSize0;
|
---|
112 | uint16_t idVendor;
|
---|
113 | uint16_t idProduct;
|
---|
114 | uint16_t bcdDevice;
|
---|
115 | uint8_t iManufacturer;
|
---|
116 | uint8_t iProduct;
|
---|
117 | uint8_t iSerialNumber;
|
---|
118 | uint8_t bNumConfigurations;
|
---|
119 | } USBDEVDESC;
|
---|
120 | #pragma pack()
|
---|
121 |
|
---|
122 | #define USBTEST_REQUEST _IOWR('U', 100, USBTESTPARMS)
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Callback to set up the test parameters for a specific test.
|
---|
126 | *
|
---|
127 | * @returns IPRT status code.
|
---|
128 | * @retval VINF_SUCCESS if setting the parameters up succeeded. Any other error code
|
---|
129 | * otherwise indicating the kind of error.
|
---|
130 | * @param idxTest The test index.
|
---|
131 | * @param pszTest Test name.
|
---|
132 | * @param pParams The USB test parameters to set up.
|
---|
133 | */
|
---|
134 | typedef DECLCALLBACKTYPE(int, FNUSBTESTPARAMSSETUP,(unsigned idxTest, const char *pszTest, PUSBTESTPARAMS pParams));
|
---|
135 | /** Pointer to a USB test parameters setup callback. */
|
---|
136 | typedef FNUSBTESTPARAMSSETUP *PFNUSBTESTPARAMSSETUP;
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * USB test descriptor.
|
---|
140 | */
|
---|
141 | typedef struct USBTESTDESC
|
---|
142 | {
|
---|
143 | /** (Sort of) Descriptive test name. */
|
---|
144 | const char *pszName;
|
---|
145 | /** Flag whether the test is excluded. */
|
---|
146 | bool fExcluded;
|
---|
147 | /** The parameter setup callback. */
|
---|
148 | PFNUSBTESTPARAMSSETUP pfnParamsSetup;
|
---|
149 | } USBTESTDESC;
|
---|
150 | /** Pointer a USB test descriptor. */
|
---|
151 | typedef USBTESTDESC *PUSBTESTDESC;
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * USB speed values.
|
---|
155 | */
|
---|
156 | typedef enum USBTESTSPEED
|
---|
157 | {
|
---|
158 | USBTESTSPEED_ANY = 0,
|
---|
159 | USBTESTSPEED_UNKNOWN,
|
---|
160 | USBTESTSPEED_LOW,
|
---|
161 | USBTESTSPEED_FULL,
|
---|
162 | USBTESTSPEED_HIGH,
|
---|
163 | USBTESTSPEED_SUPER
|
---|
164 | } USBTESTSPEED;
|
---|
165 |
|
---|
166 |
|
---|
167 | /*********************************************************************************************************************************
|
---|
168 | * Global Variables *
|
---|
169 | *********************************************************************************************************************************/
|
---|
170 |
|
---|
171 | /** Some forward method declarations. */
|
---|
172 | static DECLCALLBACK(int) usbTestParamsSetupReadWrite(unsigned idxTest, const char *pszTest, PUSBTESTPARAMS pParams);
|
---|
173 | static DECLCALLBACK(int) usbTestParamsSetupControlWrites(unsigned idxTest, const char *pszTest, PUSBTESTPARAMS pParams);
|
---|
174 |
|
---|
175 | /** Command line parameters */
|
---|
176 | static const RTGETOPTDEF g_aCmdOptions[] =
|
---|
177 | {
|
---|
178 | {"--device", 'd', RTGETOPT_REQ_STRING },
|
---|
179 | {"--help", 'h', RTGETOPT_REQ_NOTHING},
|
---|
180 | {"--exclude", 'e', RTGETOPT_REQ_UINT32},
|
---|
181 | {"--exclude-all", 'a', RTGETOPT_REQ_NOTHING},
|
---|
182 | {"--include", 'i', RTGETOPT_REQ_UINT32},
|
---|
183 | {"--expected-speed", 's', RTGETOPT_REQ_STRING }
|
---|
184 | };
|
---|
185 |
|
---|
186 | static USBTESTDESC g_aTests[] =
|
---|
187 | {
|
---|
188 | /* pszTest fExcluded pfnParamsSetup */
|
---|
189 | {"NOP", false, usbTestParamsSetupReadWrite},
|
---|
190 | {"Non-queued Bulk write", false, usbTestParamsSetupReadWrite},
|
---|
191 | {"Non-queued Bulk read", false, usbTestParamsSetupReadWrite},
|
---|
192 | {"Non-queued Bulk write variabe size", false, usbTestParamsSetupReadWrite},
|
---|
193 | {"Non-queued Bulk read variabe size", false, usbTestParamsSetupReadWrite},
|
---|
194 | {"Queued Bulk write", false, usbTestParamsSetupReadWrite},
|
---|
195 | {"Queued Bulk read", false, usbTestParamsSetupReadWrite},
|
---|
196 | {"Queued Bulk write variabe size", false, usbTestParamsSetupReadWrite},
|
---|
197 | {"Queued Bulk read variabe size", false, usbTestParamsSetupReadWrite},
|
---|
198 | {"Chapter 9 Control Test", false, usbTestParamsSetupReadWrite},
|
---|
199 | {"Queued control messaging", false, usbTestParamsSetupReadWrite},
|
---|
200 | {"Unlink reads", false, usbTestParamsSetupReadWrite},
|
---|
201 | {"Unlink writes", false, usbTestParamsSetupReadWrite},
|
---|
202 | {"Set/Clear halts", false, usbTestParamsSetupReadWrite},
|
---|
203 | {"Control writes", false, usbTestParamsSetupControlWrites},
|
---|
204 | {"Isochronous write", false, usbTestParamsSetupReadWrite},
|
---|
205 | {"Isochronous read", false, usbTestParamsSetupReadWrite},
|
---|
206 | {"Bulk write unaligned (DMA)", false, usbTestParamsSetupReadWrite},
|
---|
207 | {"Bulk read unaligned (DMA)", false, usbTestParamsSetupReadWrite},
|
---|
208 | {"Bulk write unaligned (no DMA)", false, usbTestParamsSetupReadWrite},
|
---|
209 | {"Bulk read unaligned (no DMA)", false, usbTestParamsSetupReadWrite},
|
---|
210 | {"Control writes unaligned", false, usbTestParamsSetupControlWrites},
|
---|
211 | {"Isochronous write unaligned", false, usbTestParamsSetupReadWrite},
|
---|
212 | {"Isochronous read unaligned", false, usbTestParamsSetupReadWrite},
|
---|
213 | {"Unlink queued Bulk", false, usbTestParamsSetupReadWrite}
|
---|
214 | };
|
---|
215 |
|
---|
216 | /** The test handle. */
|
---|
217 | static RTTEST g_hTest;
|
---|
218 | /** The expected device speed. */
|
---|
219 | static USBTESTSPEED g_enmSpeed = USBTESTSPEED_ANY;
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Setup callback for basic read/write (bulk, isochronous) tests.
|
---|
223 | *
|
---|
224 | * @copydoc FNUSBTESTPARAMSSETUP
|
---|
225 | */
|
---|
226 | static DECLCALLBACK(int) usbTestParamsSetupReadWrite(unsigned idxTest, const char *pszTest, PUSBTESTPARAMS pParams)
|
---|
227 | {
|
---|
228 | NOREF(idxTest);
|
---|
229 | NOREF(pszTest);
|
---|
230 |
|
---|
231 | pParams->cIterations = 1000;
|
---|
232 | pParams->cbData = 512;
|
---|
233 | pParams->cbVariation = 512;
|
---|
234 | pParams->cSgLength = 32;
|
---|
235 |
|
---|
236 | return VINF_SUCCESS;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Setup callback for the control writes test.
|
---|
241 | *
|
---|
242 | * @copydoc FNUSBTESTPARAMSSETUP
|
---|
243 | */
|
---|
244 | static DECLCALLBACK(int) usbTestParamsSetupControlWrites(unsigned idxTest, const char *pszTest, PUSBTESTPARAMS pParams)
|
---|
245 | {
|
---|
246 | NOREF(idxTest);
|
---|
247 | NOREF(pszTest);
|
---|
248 |
|
---|
249 | pParams->cIterations = 1000;
|
---|
250 | pParams->cbData = 512;
|
---|
251 | /*
|
---|
252 | * Must be smaller than cbData or the parameter check in the usbtest module fails,
|
---|
253 | * no idea yet why it must be this.
|
---|
254 | */
|
---|
255 | pParams->cbVariation = 256;
|
---|
256 | pParams->cSgLength = 32;
|
---|
257 |
|
---|
258 | return VINF_SUCCESS;
|
---|
259 | }
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Shows tool usage text.
|
---|
263 | */
|
---|
264 | static void usbTestUsage(PRTSTREAM pStrm)
|
---|
265 | {
|
---|
266 | char szExec[RTPATH_MAX];
|
---|
267 | RTStrmPrintf(pStrm, "usage: %s [options]\n",
|
---|
268 | RTPathFilename(RTProcGetExecutablePath(szExec, sizeof(szExec))));
|
---|
269 | RTStrmPrintf(pStrm, "\n");
|
---|
270 | RTStrmPrintf(pStrm, "options: \n");
|
---|
271 |
|
---|
272 |
|
---|
273 | for (unsigned i = 0; i < RT_ELEMENTS(g_aCmdOptions); i++)
|
---|
274 | {
|
---|
275 | const char *pszHelp;
|
---|
276 | switch (g_aCmdOptions[i].iShort)
|
---|
277 | {
|
---|
278 | case 'h':
|
---|
279 | pszHelp = "Displays this help and exit";
|
---|
280 | break;
|
---|
281 | case 'd':
|
---|
282 | pszHelp = "Use the specified test device";
|
---|
283 | break;
|
---|
284 | case 'e':
|
---|
285 | pszHelp = "Exclude the given test id from the list";
|
---|
286 | break;
|
---|
287 | case 'a':
|
---|
288 | pszHelp = "Exclude all tests from the list (useful to enable single tests later with --include)";
|
---|
289 | break;
|
---|
290 | case 'i':
|
---|
291 | pszHelp = "Include the given test id in the list";
|
---|
292 | break;
|
---|
293 | case 's':
|
---|
294 | pszHelp = "The device speed to expect";
|
---|
295 | break;
|
---|
296 | default:
|
---|
297 | pszHelp = "Option undocumented";
|
---|
298 | break;
|
---|
299 | }
|
---|
300 | char szOpt[256];
|
---|
301 | RTStrPrintf(szOpt, sizeof(szOpt), "%s, -%c", g_aCmdOptions[i].pszLong, g_aCmdOptions[i].iShort);
|
---|
302 | RTStrmPrintf(pStrm, " %-30s%s\n", szOpt, pszHelp);
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Searches for a USB test device and returns the bus and device ID and the device speed.
|
---|
308 | */
|
---|
309 | static int usbTestDeviceQueryBusAndDevId(uint16_t *pu16BusId, uint16_t *pu16DevId, USBTESTSPEED *penmSpeed)
|
---|
310 | {
|
---|
311 | bool fFound = false;
|
---|
312 |
|
---|
313 | #define USBTEST_USB_DEV_SYSFS "/sys/bus/usb/devices/"
|
---|
314 |
|
---|
315 | RTDIR hDirUsb = NULL;
|
---|
316 | int rc = RTDirOpen(&hDirUsb, USBTEST_USB_DEV_SYSFS);
|
---|
317 | if (RT_SUCCESS(rc))
|
---|
318 | {
|
---|
319 | do
|
---|
320 | {
|
---|
321 | RTDIRENTRY DirUsbBus;
|
---|
322 | rc = RTDirRead(hDirUsb, &DirUsbBus, NULL);
|
---|
323 | if ( RT_SUCCESS(rc)
|
---|
324 | && RTStrNCmp(DirUsbBus.szName, "usb", 3)
|
---|
325 | && RTLinuxSysFsExists(USBTEST_USB_DEV_SYSFS "%s/idVendor", DirUsbBus.szName))
|
---|
326 | {
|
---|
327 | int64_t idVendor = 0;
|
---|
328 | int64_t idProduct = 0;
|
---|
329 | int64_t iBusId = 0;
|
---|
330 | int64_t iDevId = 0;
|
---|
331 | char aszSpeed[20];
|
---|
332 |
|
---|
333 | rc = RTLinuxSysFsReadIntFile(16, &idVendor, USBTEST_USB_DEV_SYSFS "%s/idVendor", DirUsbBus.szName);
|
---|
334 | if (RT_SUCCESS(rc))
|
---|
335 | rc = RTLinuxSysFsReadIntFile(16, &idProduct, USBTEST_USB_DEV_SYSFS "%s/idProduct", DirUsbBus.szName);
|
---|
336 | if (RT_SUCCESS(rc))
|
---|
337 | rc = RTLinuxSysFsReadIntFile(16, &iBusId, USBTEST_USB_DEV_SYSFS "%s/busnum", DirUsbBus.szName);
|
---|
338 | if (RT_SUCCESS(rc))
|
---|
339 | rc = RTLinuxSysFsReadIntFile(16, &iDevId, USBTEST_USB_DEV_SYSFS "%s/devnum", DirUsbBus.szName);
|
---|
340 | if (RT_SUCCESS(rc))
|
---|
341 | rc = RTLinuxSysFsReadStrFile(&aszSpeed[0], sizeof(aszSpeed), NULL, USBTEST_USB_DEV_SYSFS "%s/speed", DirUsbBus.szName);
|
---|
342 |
|
---|
343 | if ( RT_SUCCESS(rc)
|
---|
344 | && idVendor == 0x0525
|
---|
345 | && idProduct == 0xa4a0)
|
---|
346 | {
|
---|
347 | if (penmSpeed)
|
---|
348 | {
|
---|
349 | /* Parse the speed. */
|
---|
350 | if (!RTStrCmp(&aszSpeed[0], "1.5"))
|
---|
351 | *penmSpeed = USBTESTSPEED_LOW;
|
---|
352 | else if (!RTStrCmp(&aszSpeed[0], "12"))
|
---|
353 | *penmSpeed = USBTESTSPEED_FULL;
|
---|
354 | else if (!RTStrCmp(&aszSpeed[0], "480"))
|
---|
355 | *penmSpeed = USBTESTSPEED_HIGH;
|
---|
356 | else if ( !RTStrCmp(&aszSpeed[0], "5000")
|
---|
357 | || !RTStrCmp(&aszSpeed[0], "10000"))
|
---|
358 | *penmSpeed = USBTESTSPEED_SUPER;
|
---|
359 | else
|
---|
360 | *penmSpeed = USBTESTSPEED_UNKNOWN;
|
---|
361 | }
|
---|
362 |
|
---|
363 | if (pu16BusId)
|
---|
364 | *pu16BusId = (uint16_t)iBusId;
|
---|
365 | if (pu16DevId)
|
---|
366 | *pu16DevId = (uint16_t)iDevId;
|
---|
367 | fFound = true;
|
---|
368 | break;
|
---|
369 | }
|
---|
370 | }
|
---|
371 | else if (rc != VERR_NO_MORE_FILES)
|
---|
372 | rc = VINF_SUCCESS;
|
---|
373 |
|
---|
374 | } while ( RT_SUCCESS(rc)
|
---|
375 | && !fFound);
|
---|
376 |
|
---|
377 | if (rc == VERR_NO_MORE_FILES)
|
---|
378 | rc = VINF_SUCCESS;
|
---|
379 |
|
---|
380 | RTDirClose(hDirUsb);
|
---|
381 | }
|
---|
382 |
|
---|
383 | if (RT_SUCCESS(rc) && !fFound)
|
---|
384 | rc = VERR_NOT_FOUND;
|
---|
385 |
|
---|
386 | return rc;
|
---|
387 | }
|
---|
388 |
|
---|
389 | /**
|
---|
390 | * Search for a USB test device and return the device path.
|
---|
391 | *
|
---|
392 | * @returns Path to the USB test device or NULL if none was found.
|
---|
393 | */
|
---|
394 | static char *usbTestFindDevice(void)
|
---|
395 | {
|
---|
396 | /*
|
---|
397 | * Very crude and quick way to search for the correct test device.
|
---|
398 | * Assumption is that the path looks like /dev/bus/usb/%3d/%3d.
|
---|
399 | */
|
---|
400 | char *pszDevPath = NULL;
|
---|
401 |
|
---|
402 | RTDIR hDirUsb = NULL;
|
---|
403 | int rc = RTDirOpen(&hDirUsb, "/dev/bus/usb");
|
---|
404 | if (RT_SUCCESS(rc))
|
---|
405 | {
|
---|
406 | do
|
---|
407 | {
|
---|
408 | RTDIRENTRY DirUsbBus;
|
---|
409 | rc = RTDirRead(hDirUsb, &DirUsbBus, NULL);
|
---|
410 | if (RT_SUCCESS(rc))
|
---|
411 | {
|
---|
412 | char aszPath[RTPATH_MAX + 1];
|
---|
413 | RTStrPrintf(&aszPath[0], RT_ELEMENTS(aszPath), "/dev/bus/usb/%s", DirUsbBus.szName);
|
---|
414 |
|
---|
415 | RTDIR hDirUsbBus = NULL;
|
---|
416 | rc = RTDirOpen(&hDirUsbBus, &aszPath[0]);
|
---|
417 | if (RT_SUCCESS(rc))
|
---|
418 | {
|
---|
419 | do
|
---|
420 | {
|
---|
421 | RTDIRENTRY DirUsbDev;
|
---|
422 | rc = RTDirRead(hDirUsbBus, &DirUsbDev, NULL);
|
---|
423 | if (RT_SUCCESS(rc))
|
---|
424 | {
|
---|
425 | char aszPathDev[RTPATH_MAX + 1];
|
---|
426 | RTStrPrintf(&aszPathDev[0], RT_ELEMENTS(aszPathDev), "/dev/bus/usb/%s/%s",
|
---|
427 | DirUsbBus.szName, DirUsbDev.szName);
|
---|
428 |
|
---|
429 | RTFILE hFileDev;
|
---|
430 | rc = RTFileOpen(&hFileDev, aszPathDev, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE);
|
---|
431 | if (RT_SUCCESS(rc))
|
---|
432 | {
|
---|
433 | USBDEVDESC DevDesc;
|
---|
434 |
|
---|
435 | rc = RTFileRead(hFileDev, &DevDesc, sizeof(DevDesc), NULL);
|
---|
436 | RTFileClose(hFileDev);
|
---|
437 |
|
---|
438 | if ( RT_SUCCESS(rc)
|
---|
439 | && DevDesc.idVendor == 0x0525
|
---|
440 | && DevDesc.idProduct == 0xa4a0)
|
---|
441 | pszDevPath = RTStrDup(aszPathDev);
|
---|
442 | }
|
---|
443 |
|
---|
444 | rc = VINF_SUCCESS;
|
---|
445 | }
|
---|
446 | else if (rc != VERR_NO_MORE_FILES)
|
---|
447 | rc = VINF_SUCCESS;
|
---|
448 |
|
---|
449 | } while ( RT_SUCCESS(rc)
|
---|
450 | && !pszDevPath);
|
---|
451 |
|
---|
452 | rc = VINF_SUCCESS;
|
---|
453 | RTDirClose(hDirUsbBus);
|
---|
454 | }
|
---|
455 | }
|
---|
456 | else if (rc != VERR_NO_MORE_FILES)
|
---|
457 | rc = VINF_SUCCESS;
|
---|
458 | } while ( RT_SUCCESS(rc)
|
---|
459 | && !pszDevPath);
|
---|
460 |
|
---|
461 | RTDirClose(hDirUsb);
|
---|
462 | }
|
---|
463 |
|
---|
464 | return pszDevPath;
|
---|
465 | }
|
---|
466 |
|
---|
467 | static int usbTestIoctl(int iDevFd, int iInterface, PUSBTESTPARAMS pParams)
|
---|
468 | {
|
---|
469 | struct usbdevfs_ioctl IoCtlData;
|
---|
470 |
|
---|
471 | IoCtlData.ifno = iInterface;
|
---|
472 | IoCtlData.ioctl_code = (int)USBTEST_REQUEST;
|
---|
473 | IoCtlData.data = pParams;
|
---|
474 | return ioctl(iDevFd, USBDEVFS_IOCTL, &IoCtlData);
|
---|
475 | }
|
---|
476 |
|
---|
477 | /**
|
---|
478 | * Test execution worker.
|
---|
479 | *
|
---|
480 | * @param pszDevice The device to use for testing.
|
---|
481 | */
|
---|
482 | static void usbTestExec(const char *pszDevice)
|
---|
483 | {
|
---|
484 | int iDevFd;
|
---|
485 |
|
---|
486 | RTTestSub(g_hTest, "Opening device");
|
---|
487 | iDevFd = open(pszDevice, O_RDWR);
|
---|
488 | if (iDevFd != -1)
|
---|
489 | {
|
---|
490 | USBTESTPARAMS Params;
|
---|
491 |
|
---|
492 | RTTestPassed(g_hTest, "Opening device successful\n");
|
---|
493 |
|
---|
494 | for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++)
|
---|
495 | {
|
---|
496 | RTTestSub(g_hTest, g_aTests[i].pszName);
|
---|
497 |
|
---|
498 | if (g_aTests[i].fExcluded)
|
---|
499 | {
|
---|
500 | RTTestSkipped(g_hTest, "Excluded from list");
|
---|
501 | continue;
|
---|
502 | }
|
---|
503 |
|
---|
504 | int rc = g_aTests[i].pfnParamsSetup(i, g_aTests[i].pszName, &Params);
|
---|
505 | if (RT_SUCCESS(rc))
|
---|
506 | {
|
---|
507 | Params.idxTest = i;
|
---|
508 |
|
---|
509 | /* Assume the test interface has the number 0 for now. */
|
---|
510 | int rcPosix = usbTestIoctl(iDevFd, 0, &Params);
|
---|
511 | if (rcPosix < 0 && errno == EOPNOTSUPP)
|
---|
512 | {
|
---|
513 | RTTestSkipped(g_hTest, "Not supported");
|
---|
514 | continue;
|
---|
515 | }
|
---|
516 |
|
---|
517 | if (rcPosix < 0)
|
---|
518 | {
|
---|
519 | /*
|
---|
520 | * The error status code of the unlink testcase is
|
---|
521 | * offset by 2000 for the sync and 1000 for the sync code path
|
---|
522 | * (see drivers/usb/misc/usbtest.c in the Linux kernel sources).
|
---|
523 | *
|
---|
524 | * Adjust to the actual status code so converting doesn't assert.
|
---|
525 | */
|
---|
526 | int iTmpErrno = errno;
|
---|
527 | if (iTmpErrno >= 2000)
|
---|
528 | iTmpErrno -= 2000;
|
---|
529 | else if (iTmpErrno >= 1000)
|
---|
530 | iTmpErrno -= 1000;
|
---|
531 | RTTestFailed(g_hTest, "Test failed with %Rrc\n", RTErrConvertFromErrno(iTmpErrno));
|
---|
532 | }
|
---|
533 | else
|
---|
534 | {
|
---|
535 | uint64_t u64Ns = Params.TimeTest.tv_sec * RT_NS_1SEC + Params.TimeTest.tv_usec * RT_NS_1US;
|
---|
536 | RTTestValue(g_hTest, "Runtime", u64Ns, RTTESTUNIT_NS);
|
---|
537 | }
|
---|
538 | }
|
---|
539 | else
|
---|
540 | RTTestFailed(g_hTest, "Setting up test parameters failed with %Rrc\n", rc);
|
---|
541 | RTTestSubDone(g_hTest);
|
---|
542 | }
|
---|
543 |
|
---|
544 | close(iDevFd);
|
---|
545 | }
|
---|
546 | else
|
---|
547 | RTTestFailed(g_hTest, "Opening device failed with %Rrc\n", RTErrConvertFromErrno(errno));
|
---|
548 |
|
---|
549 | }
|
---|
550 |
|
---|
551 | int main(int argc, char *argv[])
|
---|
552 | {
|
---|
553 | /*
|
---|
554 | * Init IPRT and globals.
|
---|
555 | */
|
---|
556 | int rc = RTTestInitAndCreate("UsbTest", &g_hTest);
|
---|
557 | if (rc)
|
---|
558 | return rc;
|
---|
559 |
|
---|
560 | /*
|
---|
561 | * Default values.
|
---|
562 | */
|
---|
563 | const char *pszDevice = NULL;
|
---|
564 |
|
---|
565 | RTGETOPTUNION ValueUnion;
|
---|
566 | RTGETOPTSTATE GetState;
|
---|
567 | RTGetOptInit(&GetState, argc, argv, g_aCmdOptions, RT_ELEMENTS(g_aCmdOptions), 1, 0 /* fFlags */);
|
---|
568 | while ((rc = RTGetOpt(&GetState, &ValueUnion)))
|
---|
569 | {
|
---|
570 | switch (rc)
|
---|
571 | {
|
---|
572 | case 'h':
|
---|
573 | usbTestUsage(g_pStdOut);
|
---|
574 | return RTEXITCODE_SUCCESS;
|
---|
575 | case 'd':
|
---|
576 | pszDevice = ValueUnion.psz;
|
---|
577 | break;
|
---|
578 | case 's':
|
---|
579 | if (!RTStrICmp(ValueUnion.psz, "Low"))
|
---|
580 | g_enmSpeed = USBTESTSPEED_LOW;
|
---|
581 | else if (!RTStrICmp(ValueUnion.psz, "Full"))
|
---|
582 | g_enmSpeed = USBTESTSPEED_FULL;
|
---|
583 | else if (!RTStrICmp(ValueUnion.psz, "High"))
|
---|
584 | g_enmSpeed = USBTESTSPEED_HIGH;
|
---|
585 | else if (!RTStrICmp(ValueUnion.psz, "Super"))
|
---|
586 | g_enmSpeed = USBTESTSPEED_SUPER;
|
---|
587 | else
|
---|
588 | {
|
---|
589 | RTTestPrintf(g_hTest, RTTESTLVL_FAILURE, "Invalid speed passed to --expected-speed\n");
|
---|
590 | RTTestErrorInc(g_hTest);
|
---|
591 | return RTGetOptPrintError(VERR_INVALID_PARAMETER, &ValueUnion);
|
---|
592 | }
|
---|
593 | break;
|
---|
594 | case 'e':
|
---|
595 | if (ValueUnion.u32 < RT_ELEMENTS(g_aTests))
|
---|
596 | g_aTests[ValueUnion.u32].fExcluded = true;
|
---|
597 | else
|
---|
598 | {
|
---|
599 | RTTestPrintf(g_hTest, RTTESTLVL_FAILURE, "Invalid test number passed to --exclude\n");
|
---|
600 | RTTestErrorInc(g_hTest);
|
---|
601 | return RTGetOptPrintError(VERR_INVALID_PARAMETER, &ValueUnion);
|
---|
602 | }
|
---|
603 | break;
|
---|
604 | case 'a':
|
---|
605 | for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++)
|
---|
606 | g_aTests[i].fExcluded = true;
|
---|
607 | break;
|
---|
608 | case 'i':
|
---|
609 | if (ValueUnion.u32 < RT_ELEMENTS(g_aTests))
|
---|
610 | g_aTests[ValueUnion.u32].fExcluded = false;
|
---|
611 | else
|
---|
612 | {
|
---|
613 | RTTestPrintf(g_hTest, RTTESTLVL_FAILURE, "Invalid test number passed to --include\n");
|
---|
614 | RTTestErrorInc(g_hTest);
|
---|
615 | return RTGetOptPrintError(VERR_INVALID_PARAMETER, &ValueUnion);
|
---|
616 | }
|
---|
617 | break;
|
---|
618 | default:
|
---|
619 | return RTGetOptPrintError(rc, &ValueUnion);
|
---|
620 | }
|
---|
621 | }
|
---|
622 |
|
---|
623 | /*
|
---|
624 | * Start testing.
|
---|
625 | */
|
---|
626 | RTTestBanner(g_hTest);
|
---|
627 |
|
---|
628 | /* Find the first test device if none was given. */
|
---|
629 | if (!pszDevice)
|
---|
630 | {
|
---|
631 | RTTestSub(g_hTest, "Detecting device");
|
---|
632 | pszDevice = usbTestFindDevice();
|
---|
633 | if (!pszDevice)
|
---|
634 | RTTestFailed(g_hTest, "Failed to find suitable device\n");
|
---|
635 |
|
---|
636 | RTTestSubDone(g_hTest);
|
---|
637 | }
|
---|
638 |
|
---|
639 | if (pszDevice)
|
---|
640 | {
|
---|
641 | /* First check that the requested speed matches. */
|
---|
642 | if (g_enmSpeed != USBTESTSPEED_ANY)
|
---|
643 | {
|
---|
644 | RTTestSub(g_hTest, "Checking correct device speed");
|
---|
645 |
|
---|
646 | USBTESTSPEED enmSpeed = USBTESTSPEED_UNKNOWN;
|
---|
647 | rc = usbTestDeviceQueryBusAndDevId(NULL, NULL, &enmSpeed);
|
---|
648 | if (RT_SUCCESS(rc))
|
---|
649 | {
|
---|
650 | if (enmSpeed == g_enmSpeed)
|
---|
651 | RTTestPassed(g_hTest, "Reported device speed matches requested speed\n");
|
---|
652 | else
|
---|
653 | RTTestFailed(g_hTest, "Reported device speed doesn'match requested speed (%u vs %u)\n",
|
---|
654 | enmSpeed, g_enmSpeed);
|
---|
655 | }
|
---|
656 | else
|
---|
657 | RTTestFailed(g_hTest, "Failed to query device speed with rc=%Rrc\n", rc);
|
---|
658 |
|
---|
659 | RTTestSubDone(g_hTest);
|
---|
660 | }
|
---|
661 | usbTestExec(pszDevice);
|
---|
662 | }
|
---|
663 |
|
---|
664 | RTEXITCODE rcExit = RTTestSummaryAndDestroy(g_hTest);
|
---|
665 | return rcExit;
|
---|
666 | }
|
---|
667 |
|
---|