libgui 2.01
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
WutUsbProbe.cpp
1/****************************************************************************
2 * libgui - drivers/wut
3 * Daryl Borth 2026
4 * WutUsbProbe.cpp
5 *
6 * Intentionally does NOT touch Mocha_usbX_disc_interface
7 ***************************************************************************/
8#include <nsysuhs/uhs.h>
9#include <nsysuhs/uhs_usbspec.h>
10#include <malloc.h>
11#include <string.h>
12
13#include "WutUsbProbe.h"
14
15#define MAX_PROFILES 4
16
17void ScanUsbHardwareSignature(UsbHardwareSignature & out)
18{
19 out.count = 0;
20
21 void * cfgBuffer = memalign(0x40, UHS_CONFIG_BUFFER_SIZE);
22 if(!cfgBuffer)
23 return;
24
25 UhsConfig config;
26 memset(&config, 0, sizeof(config));
27 config.controller_num = 0;
28 config.buffer = cfgBuffer;
29 config.buffer_size = UHS_CONFIG_BUFFER_SIZE;
30
31 UhsHandle handle;
32 memset(&handle, 0, sizeof(handle));
33
34 UHSStatus openStatus = UhsClientOpen(&handle, &config);
35 if(openStatus == UHS_STATUS_OK)
36 {
37 UhsInterfaceFilter filter;
38 memset(&filter, 0, sizeof(filter));
39 filter.match_params = MATCH_ANY;
40
41 UhsInterfaceProfile * profiles = (UhsInterfaceProfile *)memalign(0x40, sizeof(UhsInterfaceProfile) * MAX_PROFILES);
42 if(profiles)
43 {
44 memset(profiles, 0, sizeof(UhsInterfaceProfile) * MAX_PROFILES);
45
46 // UhsQueryInterfaces()'s return value is the count of interfaces
47 // written into profiles. Use it directly as the loop bound.
48 int foundCount = (int)UhsQueryInterfaces(&handle, &filter, profiles, MAX_PROFILES);
49
50 for(int i = 0; i < foundCount; i++)
51 {
52 bool isStorage = (profiles[i].if_desc.bInterfaceClass == USBCLASS_STORAGE);
53
54 if(isStorage && out.count < UsbHardwareSignature::kMaxInterfaces)
55 {
56 UsbHardwareInterfaceInfo & entry = out.interfaces[out.count++];
57 entry.ifHandle = profiles[i].if_handle;
58 entry.vid = profiles[i].dev_desc.idVendor;
59 entry.pid = profiles[i].dev_desc.idProduct;
60 }
61 }
62 free(profiles);
63 }
64 UhsClientClose(&handle);
65 }
66 free(cfgBuffer);
67}
68
69bool UsbHardwareSignatureChanged(const UsbHardwareSignature & a, const UsbHardwareSignature & b)
70{
71 if(a.count != b.count)
72 return true;
73
74 for(int i = 0; i < a.count; i++)
75 {
76 bool foundInB = false;
77 for(int j = 0; j < b.count; j++)
78 {
79 if(a.interfaces[i].ifHandle == b.interfaces[j].ifHandle)
80 {
81 foundInB = true;
82 break;
83 }
84 }
85 if(!foundInB)
86 return true;
87 }
88
89 return false;
90}
One USBCLASS_STORAGE interface as seen by a single ScanUsbHardwareSignature() call.
Definition WutUsbProbe.h:17
uint16_t pid
USB product id.
Definition WutUsbProbe.h:20
uint16_t vid
USB vendor id.
Definition WutUsbProbe.h:19
uint32_t ifHandle
nsysuhs interface handle; only meaningful within one boot session
Definition WutUsbProbe.h:18
The set of USBCLASS_STORAGE interfaces nsysuhs currently reports across every controller_num probed.
Definition WutUsbProbe.h:28
static const int kMaxInterfaces
Capacity of interfaces[].
Definition WutUsbProbe.h:29
int count
Number of valid entries in interfaces[].
Definition WutUsbProbe.h:31
UsbHardwareInterfaceInfo interfaces[kMaxInterfaces]
Attached USBCLASS_STORAGE interfaces; only the first count entries are valid.
Definition WutUsbProbe.h:30