VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/utils/usb/UsbTestServiceGadgetClassTest.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.3 KB
Line 
1/* $Id: UsbTestServiceGadgetClassTest.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * UsbTestServ - Remote USB test configuration and execution server, USB gadget class
4 * for the test device.
5 */
6
7/*
8 * Copyright (C) 2016-2022 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/asm.h>
43#include <iprt/ctype.h>
44#include <iprt/dir.h>
45#include <iprt/err.h>
46#include <iprt/env.h>
47#include <iprt/mem.h>
48#include <iprt/path.h>
49#include <iprt/process.h>
50#include <iprt/string.h>
51#include <iprt/symlink.h>
52#include <iprt/thread.h>
53
54#include <iprt/linux/sysfs.h>
55
56#include "UsbTestServiceGadgetInternal.h"
57#include "UsbTestServicePlatform.h"
58
59
60/*********************************************************************************************************************************
61* Constants And Macros, Structures and Typedefs *
62*********************************************************************************************************************************/
63
64/** Default configfs mount point. */
65#define UTS_GADGET_CLASS_CONFIGFS_MNT_DEF "/sys/kernel/config/usb_gadget"
66/** Gadget template name */
67#define UTS_GADGET_TEMPLATE_NAME "gadget_test"
68
69/** Default vendor ID which is recognized by the usbtest driver. */
70#define UTS_GADGET_TEST_VENDOR_ID_DEF UINT16_C(0x0525)
71/** Default product ID which is recognized by the usbtest driver. */
72#define UTS_GADGET_TEST_PRODUCT_ID_DEF UINT16_C(0xa4a0)
73/** Default device class. */
74#define UTS_GADGET_TEST_DEVICE_CLASS_DEF UINT8_C(0xff)
75/** Default serial number string. */
76#define UTS_GADGET_TEST_SERIALNUMBER_DEF "0123456789"
77/** Default manufacturer string. */
78#define UTS_GADGET_TEST_MANUFACTURER_DEF "Oracle Inc."
79/** Default product string. */
80#define UTS_GADGET_TEST_PRODUCT_DEF "USB test device"
81
82/**
83 * Internal UTS gadget host instance data.
84 */
85typedef struct UTSGADGETCLASSINT
86{
87 /** Gadget template path. */
88 char *pszGadgetPath;
89 /** The UDC this gadget is connected to. */
90 char *pszUdc;
91 /** Bus identifier for the used UDC. */
92 uint32_t uBusId;
93 /** Device identifier. */
94 uint32_t uDevId;
95} UTSGADGETCLASSINT;
96
97
98/*********************************************************************************************************************************
99* Global Variables *
100*********************************************************************************************************************************/
101
102/** Number of already created gadgets, used for the template name. */
103static volatile uint32_t g_cGadgets = 0;
104
105
106/*********************************************************************************************************************************
107* Internal Functions *
108*********************************************************************************************************************************/
109
110/**
111 * Creates a new directory pointed to by the given format string.
112 *
113 * @returns IPRT status code.
114 * @param pszFormat The format string.
115 * @param va The arguments.
116 */
117static int utsGadgetClassTestDirCreateV(const char *pszFormat, va_list va)
118{
119 int rc = VINF_SUCCESS;
120 char aszPath[RTPATH_MAX + 1];
121
122 size_t cbStr = RTStrPrintfV(&aszPath[0], sizeof(aszPath), pszFormat, va);
123 if (cbStr <= sizeof(aszPath) - 1)
124 rc = RTDirCreateFullPath(aszPath, 0700);
125 else
126 rc = VERR_BUFFER_OVERFLOW;
127
128 return rc;
129}
130
131
132/**
133 * Creates a new directory pointed to by the given format string.
134 *
135 * @returns IPRT status code.
136 * @param pszFormat The format string.
137 * @param ... The arguments.
138 */
139static int utsGadgetClassTestDirCreate(const char *pszFormat, ...)
140{
141 va_list va;
142 va_start(va, pszFormat);
143 int rc = utsGadgetClassTestDirCreateV(pszFormat, va);
144 va_end(va);
145 return rc;
146}
147
148
149/**
150 * Removes a directory pointed to by the given format string.
151 *
152 * @returns IPRT status code.
153 * @param pszFormat The format string.
154 * @param va The arguments.
155 */
156static int utsGadgetClassTestDirRemoveV(const char *pszFormat, va_list va)
157{
158 int rc = VINF_SUCCESS;
159 char aszPath[RTPATH_MAX + 1];
160
161 size_t cbStr = RTStrPrintfV(&aszPath[0], sizeof(aszPath), pszFormat, va);
162 if (cbStr <= sizeof(aszPath) - 1)
163 rc = RTDirRemove(aszPath);
164 else
165 rc = VERR_BUFFER_OVERFLOW;
166
167 return rc;
168}
169
170
171/**
172 * Removes a directory pointed to by the given format string.
173 *
174 * @returns IPRT status code.
175 * @param pszFormat The format string.
176 * @param ... The arguments.
177 */
178static int utsGadgetClassTestDirRemove(const char *pszFormat, ...)
179{
180 va_list va;
181 va_start(va, pszFormat);
182 int rc = utsGadgetClassTestDirRemoveV(pszFormat, va);
183 va_end(va);
184 return rc;
185}
186
187
188/**
189 * Links the given function to the given config.
190 *
191 * @returns IPRT status code.
192 * @param pClass The gadget class instance data.
193 * @param pszFunc The function to link.
194 * @param pszCfg The configuration which the function will be part of.
195 */
196static int utsGadgetClassTestLinkFuncToCfg(PUTSGADGETCLASSINT pClass, const char *pszFunc, const char *pszCfg)
197{
198 int rc = VINF_SUCCESS;
199 char aszPathFunc[RTPATH_MAX + 1];
200 char aszPathCfg[RTPATH_MAX + 1];
201
202 size_t cbStr = RTStrPrintf(&aszPathFunc[0], sizeof(aszPathFunc), "%s/functions/%s",
203 pClass->pszGadgetPath, pszFunc);
204 if (cbStr <= sizeof(aszPathFunc) - 1)
205 {
206 cbStr = RTStrPrintf(&aszPathCfg[0], sizeof(aszPathCfg), "%s/configs/%s/%s",
207 pClass->pszGadgetPath, pszCfg, pszFunc);
208 if (cbStr <= sizeof(aszPathCfg) - 1)
209 rc = RTSymlinkCreate(&aszPathCfg[0], &aszPathFunc[0], RTSYMLINKTYPE_DIR, 0);
210 else
211 rc = VERR_BUFFER_OVERFLOW;
212 }
213 else
214 rc = VERR_BUFFER_OVERFLOW;
215
216 return rc;
217}
218
219
220/**
221 * Unlinks the given function from the given configuration.
222 *
223 * @returns IPRT status code.
224 * @param pClass The gadget class instance data.
225 * @param pszFunc The function to unlink.
226 * @param pszCfg The configuration which the function is currently part of.
227 */
228static int utsGadgetClassTestUnlinkFuncFromCfg(PUTSGADGETCLASSINT pClass, const char *pszFunc, const char *pszCfg)
229{
230 int rc = VINF_SUCCESS;
231 char aszPath[RTPATH_MAX + 1];
232 size_t cbStr = RTStrPrintf(&aszPath[0], sizeof(aszPath), "%s/configs/%s/%s",
233 pClass->pszGadgetPath, pszCfg, pszFunc);
234 if (cbStr <= sizeof(aszPath) - 1)
235 rc = RTSymlinkDelete(&aszPath[0], 0);
236 else
237 rc = VERR_BUFFER_OVERFLOW;
238
239 return rc;
240}
241
242
243/**
244 * Cleans up any leftover configurations from the gadget class.
245 *
246 * @returns nothing.
247 * @param pClass The gadget class instance data.
248 */
249static void utsGadgetClassTestCleanup(PUTSGADGETCLASSINT pClass)
250{
251 /* Unbind the gadget from the currently assigned UDC first. */
252 int rc = RTLinuxSysFsWriteStrFile("", 0, NULL, "%s/UDC", pClass->pszGadgetPath);
253 AssertRC(rc);
254
255 /* Delete the symlinks, ignore any errors. */
256 utsGadgetClassTestUnlinkFuncFromCfg(pClass, "Loopback.0", "c.2");
257 utsGadgetClassTestUnlinkFuncFromCfg(pClass, "SourceSink.0", "c.1");
258
259 /* Delete configuration strings and then the configuration directories. */
260 utsGadgetClassTestDirRemove("%s/configs/c.2/strings/0x409", pClass->pszGadgetPath);
261 utsGadgetClassTestDirRemove("%s/configs/c.1/strings/0x409", pClass->pszGadgetPath);
262
263 utsGadgetClassTestDirRemove("%s/configs/c.2", pClass->pszGadgetPath);
264 utsGadgetClassTestDirRemove("%s/configs/c.1", pClass->pszGadgetPath);
265
266 /* Delete the functions. */
267 utsGadgetClassTestDirRemove("%s/functions/Loopback.0", pClass->pszGadgetPath);
268 utsGadgetClassTestDirRemove("%s/functions/SourceSink.0", pClass->pszGadgetPath);
269
270 /* Delete the english strings. */
271 utsGadgetClassTestDirRemove("%s/strings/0x409", pClass->pszGadgetPath);
272
273 /* Finally delete the gadget template. */
274 utsGadgetClassTestDirRemove(pClass->pszGadgetPath);
275
276 /* Release the UDC. */
277 if (pClass->pszUdc)
278 {
279 rc = utsPlatformLnxReleaseUDC(pClass->pszUdc);
280 AssertRC(rc);
281 RTStrFree(pClass->pszUdc);
282 }
283}
284
285/**
286 * @interface_method_impl{UTSGADGETCLASSIF,pfnInit}
287 */
288static DECLCALLBACK(int) utsGadgetClassTestInit(PUTSGADGETCLASSINT pClass, PCUTSGADGETCFGITEM paCfg)
289{
290 int rc = VINF_SUCCESS;
291
292 if (RTLinuxSysFsExists(UTS_GADGET_CLASS_CONFIGFS_MNT_DEF))
293 {
294 /* Create the gadget template */
295 unsigned idx = ASMAtomicIncU32(&g_cGadgets);
296
297 int rcStr = RTStrAPrintf(&pClass->pszGadgetPath, "%s/%s%u", UTS_GADGET_CLASS_CONFIGFS_MNT_DEF,
298 UTS_GADGET_TEMPLATE_NAME, idx);
299 if (rcStr == -1)
300 return VERR_NO_STR_MEMORY;
301
302 rc = utsGadgetClassTestDirCreate(pClass->pszGadgetPath);
303 if (RT_SUCCESS(rc))
304 {
305 uint16_t idVendor = 0;
306 uint16_t idProduct = 0;
307 uint8_t bDeviceClass = 0;
308 char *pszSerial = NULL;
309 char *pszManufacturer = NULL;
310 char *pszProduct = NULL;
311 bool fSuperSpeed = false;
312
313 /* Get basic device config. */
314 rc = utsGadgetCfgQueryU16Def(paCfg, "Gadget/idVendor", &idVendor, UTS_GADGET_TEST_VENDOR_ID_DEF);
315 if (RT_SUCCESS(rc))
316 rc = utsGadgetCfgQueryU16Def(paCfg, "Gadget/idProduct", &idProduct, UTS_GADGET_TEST_PRODUCT_ID_DEF);
317 if (RT_SUCCESS(rc))
318 rc = utsGadgetCfgQueryU8Def(paCfg, "Gadget/bDeviceClass", &bDeviceClass, UTS_GADGET_TEST_DEVICE_CLASS_DEF);
319 if (RT_SUCCESS(rc))
320 rc = utsGadgetCfgQueryStringDef(paCfg, "Gadget/SerialNumber", &pszSerial, UTS_GADGET_TEST_SERIALNUMBER_DEF);
321 if (RT_SUCCESS(rc))
322 rc = utsGadgetCfgQueryStringDef(paCfg, "Gadget/Manufacturer", &pszManufacturer, UTS_GADGET_TEST_MANUFACTURER_DEF);
323 if (RT_SUCCESS(rc))
324 rc = utsGadgetCfgQueryStringDef(paCfg, "Gadget/Product", &pszProduct, UTS_GADGET_TEST_PRODUCT_DEF);
325 if (RT_SUCCESS(rc))
326 rc = utsGadgetCfgQueryBoolDef(paCfg, "Gadget/SuperSpeed", &fSuperSpeed, false);
327
328 if (RT_SUCCESS(rc))
329 {
330 /* Write basic attributes. */
331 rc = RTLinuxSysFsWriteU16File(16, idVendor, "%s/idVendor", pClass->pszGadgetPath);
332 if (RT_SUCCESS(rc))
333 rc = RTLinuxSysFsWriteU16File(16, idProduct, "%s/idProduct", pClass->pszGadgetPath);
334 if (RT_SUCCESS(rc))
335 rc = RTLinuxSysFsWriteU16File(16, bDeviceClass, "%s/bDeviceClass", pClass->pszGadgetPath);
336
337 /* Create english language strings. */
338 if (RT_SUCCESS(rc))
339 rc = utsGadgetClassTestDirCreate("%s/strings/0x409", pClass->pszGadgetPath);
340 if (RT_SUCCESS(rc))
341 rc = RTLinuxSysFsWriteStrFile(pszSerial, 0, NULL, "%s/strings/0x409/serialnumber", pClass->pszGadgetPath);
342 if (RT_SUCCESS(rc))
343 rc = RTLinuxSysFsWriteStrFile(pszManufacturer, 0, NULL, "%s/strings/0x409/manufacturer", pClass->pszGadgetPath);
344 if (RT_SUCCESS(rc))
345 rc = RTLinuxSysFsWriteStrFile(pszProduct, 0, NULL, "%s/strings/0x409/product", pClass->pszGadgetPath);
346
347 /* Create the gadget functions. */
348 if (RT_SUCCESS(rc))
349 rc = utsGadgetClassTestDirCreate("%s/functions/SourceSink.0", pClass->pszGadgetPath);
350 if (RT_SUCCESS(rc))
351 rc = utsGadgetClassTestDirCreate("%s/functions/Loopback.0", pClass->pszGadgetPath);
352
353 /* Create the device configs. */
354 if (RT_SUCCESS(rc))
355 rc = utsGadgetClassTestDirCreate("%s/configs/c.1", pClass->pszGadgetPath);
356 if (RT_SUCCESS(rc))
357 rc = utsGadgetClassTestDirCreate("%s/configs/c.2", pClass->pszGadgetPath);
358
359 /* Write configuration strings. */
360 if (RT_SUCCESS(rc))
361 rc = utsGadgetClassTestDirCreate("%s/configs/c.1/strings/0x409", pClass->pszGadgetPath);
362 if (RT_SUCCESS(rc))
363 rc = utsGadgetClassTestDirCreate("%s/configs/c.2/strings/0x409", pClass->pszGadgetPath);
364 if (RT_SUCCESS(rc))
365 rc = RTLinuxSysFsWriteStrFile("source and sink data", 0, NULL, "%s/configs/c.1/strings/0x409/configuration", pClass->pszGadgetPath);
366 if (RT_SUCCESS(rc))
367 rc = RTLinuxSysFsWriteStrFile("loop input to output", 0, NULL, "%s/configs/c.2/strings/0x409/configuration", pClass->pszGadgetPath);
368
369 /* Link the functions into the configurations. */
370 if (RT_SUCCESS(rc))
371 rc = utsGadgetClassTestLinkFuncToCfg(pClass, "SourceSink.0", "c.1");
372 if (RT_SUCCESS(rc))
373 rc = utsGadgetClassTestLinkFuncToCfg(pClass, "Loopback.0", "c.2");
374
375 /* Finally enable the gadget by attaching it to a UDC. */
376 if (RT_SUCCESS(rc))
377 {
378 pClass->pszUdc = NULL;
379
380 rc = utsPlatformLnxAcquireUDC(fSuperSpeed, &pClass->pszUdc, &pClass->uBusId);
381 if (RT_SUCCESS(rc))
382 rc = RTLinuxSysFsWriteStrFile(pClass->pszUdc, 0, NULL, "%s/UDC", pClass->pszGadgetPath);
383 if (RT_SUCCESS(rc))
384 RTThreadSleep(500); /* Fudge: Sleep a bit to give the device a chance to appear on the host so binding succeeds. */
385 }
386 }
387
388 if (pszSerial)
389 RTStrFree(pszSerial);
390 if (pszManufacturer)
391 RTStrFree(pszManufacturer);
392 if (pszProduct)
393 RTStrFree(pszProduct);
394 }
395 }
396 else
397 rc = VERR_NOT_FOUND;
398
399 if (RT_FAILURE(rc))
400 utsGadgetClassTestCleanup(pClass);
401
402 return rc;
403}
404
405
406/**
407 * @interface_method_impl{UTSGADGETCLASSIF,pfnTerm}
408 */
409static DECLCALLBACK(void) utsGadgetClassTestTerm(PUTSGADGETCLASSINT pClass)
410{
411 utsGadgetClassTestCleanup(pClass);
412
413 if (pClass->pszGadgetPath)
414 RTStrFree(pClass->pszGadgetPath);
415}
416
417
418/**
419 * @interface_method_impl{UTSGADGETCLASSIF,pfnGetBusId}
420 */
421static DECLCALLBACK(uint32_t) utsGadgetClassTestGetBusId(PUTSGADGETCLASSINT pClass)
422{
423 return pClass->uBusId;
424}
425
426
427/**
428 * @interface_method_impl{UTSGADGETCLASSIF,pfnConnect}
429 */
430static DECLCALLBACK(int) utsGadgetClassTestConnect(PUTSGADGETCLASSINT pClass)
431{
432 int rc = RTLinuxSysFsWriteStrFile("connect", 0, NULL, "/sys/class/udc/%s/soft_connect", pClass->pszUdc);
433 if (RT_SUCCESS(rc))
434 RTThreadSleep(500); /* Fudge: Sleep a bit to give the device a chance to appear on the host so binding succeeds. */
435
436 return rc;
437}
438
439
440/**
441 * @interface_method_impl{UTSGADGETCLASSIF,pfnDisconnect}
442 */
443static DECLCALLBACK(int) utsGadgetClassTestDisconnect(PUTSGADGETCLASSINT pClass)
444{
445 return RTLinuxSysFsWriteStrFile("disconnect", 0, NULL, "/sys/class/udc/%s/soft_connect", pClass->pszUdc);}
446
447
448
449/**
450 * The gadget host interface callback table.
451 */
452const UTSGADGETCLASSIF g_UtsGadgetClassTest =
453{
454 /** enmType */
455 UTSGADGETCLASS_TEST,
456 /** pszDesc */
457 "UTS test device gadget class",
458 /** cbIf */
459 sizeof(UTSGADGETCLASSINT),
460 /** pfnInit */
461 utsGadgetClassTestInit,
462 /** pfnTerm */
463 utsGadgetClassTestTerm,
464 /** pfnGetBusId */
465 utsGadgetClassTestGetBusId,
466 /** pfnConnect */
467 utsGadgetClassTestConnect,
468 /** pfnDisconnect. */
469 utsGadgetClassTestDisconnect
470};
471
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette