VirtualBox

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

Last change on this file since 103914 was 99739, checked in by vboxsync, 19 months ago

*: doxygen corrections (mostly about removing @returns from functions returning void).

  • 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 99739 2023-05-11 01:01:08Z 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-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/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 * @param pClass The gadget class instance data.
247 */
248static void utsGadgetClassTestCleanup(PUTSGADGETCLASSINT pClass)
249{
250 /* Unbind the gadget from the currently assigned UDC first. */
251 int rc = RTLinuxSysFsWriteStrFile("", 0, NULL, "%s/UDC", pClass->pszGadgetPath);
252 AssertRC(rc);
253
254 /* Delete the symlinks, ignore any errors. */
255 utsGadgetClassTestUnlinkFuncFromCfg(pClass, "Loopback.0", "c.2");
256 utsGadgetClassTestUnlinkFuncFromCfg(pClass, "SourceSink.0", "c.1");
257
258 /* Delete configuration strings and then the configuration directories. */
259 utsGadgetClassTestDirRemove("%s/configs/c.2/strings/0x409", pClass->pszGadgetPath);
260 utsGadgetClassTestDirRemove("%s/configs/c.1/strings/0x409", pClass->pszGadgetPath);
261
262 utsGadgetClassTestDirRemove("%s/configs/c.2", pClass->pszGadgetPath);
263 utsGadgetClassTestDirRemove("%s/configs/c.1", pClass->pszGadgetPath);
264
265 /* Delete the functions. */
266 utsGadgetClassTestDirRemove("%s/functions/Loopback.0", pClass->pszGadgetPath);
267 utsGadgetClassTestDirRemove("%s/functions/SourceSink.0", pClass->pszGadgetPath);
268
269 /* Delete the english strings. */
270 utsGadgetClassTestDirRemove("%s/strings/0x409", pClass->pszGadgetPath);
271
272 /* Finally delete the gadget template. */
273 utsGadgetClassTestDirRemove(pClass->pszGadgetPath);
274
275 /* Release the UDC. */
276 if (pClass->pszUdc)
277 {
278 rc = utsPlatformLnxReleaseUDC(pClass->pszUdc);
279 AssertRC(rc);
280 RTStrFree(pClass->pszUdc);
281 }
282}
283
284/**
285 * @interface_method_impl{UTSGADGETCLASSIF,pfnInit}
286 */
287static DECLCALLBACK(int) utsGadgetClassTestInit(PUTSGADGETCLASSINT pClass, PCUTSGADGETCFGITEM paCfg)
288{
289 int rc = VINF_SUCCESS;
290
291 if (RTLinuxSysFsExists(UTS_GADGET_CLASS_CONFIGFS_MNT_DEF))
292 {
293 /* Create the gadget template */
294 unsigned idx = ASMAtomicIncU32(&g_cGadgets);
295
296 int rcStr = RTStrAPrintf(&pClass->pszGadgetPath, "%s/%s%u", UTS_GADGET_CLASS_CONFIGFS_MNT_DEF,
297 UTS_GADGET_TEMPLATE_NAME, idx);
298 if (rcStr == -1)
299 return VERR_NO_STR_MEMORY;
300
301 rc = utsGadgetClassTestDirCreate(pClass->pszGadgetPath);
302 if (RT_SUCCESS(rc))
303 {
304 uint16_t idVendor = 0;
305 uint16_t idProduct = 0;
306 uint8_t bDeviceClass = 0;
307 char *pszSerial = NULL;
308 char *pszManufacturer = NULL;
309 char *pszProduct = NULL;
310 bool fSuperSpeed = false;
311
312 /* Get basic device config. */
313 rc = utsGadgetCfgQueryU16Def(paCfg, "Gadget/idVendor", &idVendor, UTS_GADGET_TEST_VENDOR_ID_DEF);
314 if (RT_SUCCESS(rc))
315 rc = utsGadgetCfgQueryU16Def(paCfg, "Gadget/idProduct", &idProduct, UTS_GADGET_TEST_PRODUCT_ID_DEF);
316 if (RT_SUCCESS(rc))
317 rc = utsGadgetCfgQueryU8Def(paCfg, "Gadget/bDeviceClass", &bDeviceClass, UTS_GADGET_TEST_DEVICE_CLASS_DEF);
318 if (RT_SUCCESS(rc))
319 rc = utsGadgetCfgQueryStringDef(paCfg, "Gadget/SerialNumber", &pszSerial, UTS_GADGET_TEST_SERIALNUMBER_DEF);
320 if (RT_SUCCESS(rc))
321 rc = utsGadgetCfgQueryStringDef(paCfg, "Gadget/Manufacturer", &pszManufacturer, UTS_GADGET_TEST_MANUFACTURER_DEF);
322 if (RT_SUCCESS(rc))
323 rc = utsGadgetCfgQueryStringDef(paCfg, "Gadget/Product", &pszProduct, UTS_GADGET_TEST_PRODUCT_DEF);
324 if (RT_SUCCESS(rc))
325 rc = utsGadgetCfgQueryBoolDef(paCfg, "Gadget/SuperSpeed", &fSuperSpeed, false);
326
327 if (RT_SUCCESS(rc))
328 {
329 /* Write basic attributes. */
330 rc = RTLinuxSysFsWriteU16File(16, idVendor, "%s/idVendor", pClass->pszGadgetPath);
331 if (RT_SUCCESS(rc))
332 rc = RTLinuxSysFsWriteU16File(16, idProduct, "%s/idProduct", pClass->pszGadgetPath);
333 if (RT_SUCCESS(rc))
334 rc = RTLinuxSysFsWriteU16File(16, bDeviceClass, "%s/bDeviceClass", pClass->pszGadgetPath);
335
336 /* Create english language strings. */
337 if (RT_SUCCESS(rc))
338 rc = utsGadgetClassTestDirCreate("%s/strings/0x409", pClass->pszGadgetPath);
339 if (RT_SUCCESS(rc))
340 rc = RTLinuxSysFsWriteStrFile(pszSerial, 0, NULL, "%s/strings/0x409/serialnumber", pClass->pszGadgetPath);
341 if (RT_SUCCESS(rc))
342 rc = RTLinuxSysFsWriteStrFile(pszManufacturer, 0, NULL, "%s/strings/0x409/manufacturer", pClass->pszGadgetPath);
343 if (RT_SUCCESS(rc))
344 rc = RTLinuxSysFsWriteStrFile(pszProduct, 0, NULL, "%s/strings/0x409/product", pClass->pszGadgetPath);
345
346 /* Create the gadget functions. */
347 if (RT_SUCCESS(rc))
348 rc = utsGadgetClassTestDirCreate("%s/functions/SourceSink.0", pClass->pszGadgetPath);
349 if (RT_SUCCESS(rc))
350 rc = utsGadgetClassTestDirCreate("%s/functions/Loopback.0", pClass->pszGadgetPath);
351
352 /* Create the device configs. */
353 if (RT_SUCCESS(rc))
354 rc = utsGadgetClassTestDirCreate("%s/configs/c.1", pClass->pszGadgetPath);
355 if (RT_SUCCESS(rc))
356 rc = utsGadgetClassTestDirCreate("%s/configs/c.2", pClass->pszGadgetPath);
357
358 /* Write configuration strings. */
359 if (RT_SUCCESS(rc))
360 rc = utsGadgetClassTestDirCreate("%s/configs/c.1/strings/0x409", pClass->pszGadgetPath);
361 if (RT_SUCCESS(rc))
362 rc = utsGadgetClassTestDirCreate("%s/configs/c.2/strings/0x409", pClass->pszGadgetPath);
363 if (RT_SUCCESS(rc))
364 rc = RTLinuxSysFsWriteStrFile("source and sink data", 0, NULL, "%s/configs/c.1/strings/0x409/configuration", pClass->pszGadgetPath);
365 if (RT_SUCCESS(rc))
366 rc = RTLinuxSysFsWriteStrFile("loop input to output", 0, NULL, "%s/configs/c.2/strings/0x409/configuration", pClass->pszGadgetPath);
367
368 /* Link the functions into the configurations. */
369 if (RT_SUCCESS(rc))
370 rc = utsGadgetClassTestLinkFuncToCfg(pClass, "SourceSink.0", "c.1");
371 if (RT_SUCCESS(rc))
372 rc = utsGadgetClassTestLinkFuncToCfg(pClass, "Loopback.0", "c.2");
373
374 /* Finally enable the gadget by attaching it to a UDC. */
375 if (RT_SUCCESS(rc))
376 {
377 pClass->pszUdc = NULL;
378
379 rc = utsPlatformLnxAcquireUDC(fSuperSpeed, &pClass->pszUdc, &pClass->uBusId);
380 if (RT_SUCCESS(rc))
381 rc = RTLinuxSysFsWriteStrFile(pClass->pszUdc, 0, NULL, "%s/UDC", pClass->pszGadgetPath);
382 if (RT_SUCCESS(rc))
383 RTThreadSleep(500); /* Fudge: Sleep a bit to give the device a chance to appear on the host so binding succeeds. */
384 }
385 }
386
387 if (pszSerial)
388 RTStrFree(pszSerial);
389 if (pszManufacturer)
390 RTStrFree(pszManufacturer);
391 if (pszProduct)
392 RTStrFree(pszProduct);
393 }
394 }
395 else
396 rc = VERR_NOT_FOUND;
397
398 if (RT_FAILURE(rc))
399 utsGadgetClassTestCleanup(pClass);
400
401 return rc;
402}
403
404
405/**
406 * @interface_method_impl{UTSGADGETCLASSIF,pfnTerm}
407 */
408static DECLCALLBACK(void) utsGadgetClassTestTerm(PUTSGADGETCLASSINT pClass)
409{
410 utsGadgetClassTestCleanup(pClass);
411
412 if (pClass->pszGadgetPath)
413 RTStrFree(pClass->pszGadgetPath);
414}
415
416
417/**
418 * @interface_method_impl{UTSGADGETCLASSIF,pfnGetBusId}
419 */
420static DECLCALLBACK(uint32_t) utsGadgetClassTestGetBusId(PUTSGADGETCLASSINT pClass)
421{
422 return pClass->uBusId;
423}
424
425
426/**
427 * @interface_method_impl{UTSGADGETCLASSIF,pfnConnect}
428 */
429static DECLCALLBACK(int) utsGadgetClassTestConnect(PUTSGADGETCLASSINT pClass)
430{
431 int rc = RTLinuxSysFsWriteStrFile("connect", 0, NULL, "/sys/class/udc/%s/soft_connect", pClass->pszUdc);
432 if (RT_SUCCESS(rc))
433 RTThreadSleep(500); /* Fudge: Sleep a bit to give the device a chance to appear on the host so binding succeeds. */
434
435 return rc;
436}
437
438
439/**
440 * @interface_method_impl{UTSGADGETCLASSIF,pfnDisconnect}
441 */
442static DECLCALLBACK(int) utsGadgetClassTestDisconnect(PUTSGADGETCLASSINT pClass)
443{
444 return RTLinuxSysFsWriteStrFile("disconnect", 0, NULL, "/sys/class/udc/%s/soft_connect", pClass->pszUdc);}
445
446
447
448/**
449 * The gadget host interface callback table.
450 */
451const UTSGADGETCLASSIF g_UtsGadgetClassTest =
452{
453 /** enmType */
454 UTSGADGETCLASS_TEST,
455 /** pszDesc */
456 "UTS test device gadget class",
457 /** cbIf */
458 sizeof(UTSGADGETCLASSINT),
459 /** pfnInit */
460 utsGadgetClassTestInit,
461 /** pfnTerm */
462 utsGadgetClassTestTerm,
463 /** pfnGetBusId */
464 utsGadgetClassTestGetBusId,
465 /** pfnConnect */
466 utsGadgetClassTestConnect,
467 /** pfnDisconnect. */
468 utsGadgetClassTestDisconnect
469};
470
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