Skip to content

Commit e7281a1

Browse files
committed
Reference code: detach standard drivers for macOS
1 parent 3755159 commit e7281a1

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

reference/macos/USB/usb_device.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ const usb_endpoint& usb_device::get_endpoint(usb_direction direction, int endpoi
8585
return usb_endpoint::invalid;
8686
}
8787

88+
void usb_device::detach_standard_drivers() {
89+
if (is_open())
90+
throw usb_error("detach_standard_drivers() must not be called when the device is open", 0);
91+
92+
IOReturn ret = (*device_)->USBDeviceReEnumerate(device_, kUSBReEnumerateCaptureDeviceMask);
93+
usb_error::check(ret, "failed to detach standard drivers");
94+
}
95+
96+
void usb_device::attach_standard_drivers() {
97+
if (is_open())
98+
throw usb_error("attach_standard_drivers() must not be called when the device is open", 0);
99+
100+
IOReturn ret = (*device_)->USBDeviceReEnumerate(device_, kUSBReEnumerateReleaseDeviceMask);
101+
usb_error::check(ret, "failed to attach standard drivers");
102+
}
103+
88104

89105
bool usb_device::is_open() const {
90106
return is_open_;

reference/macos/USB/usb_device.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,32 @@ class usb_device {
108108
*/
109109
const usb_interface& get_interface(int interface_number) const;
110110

111+
/**
112+
* Detaches the standard drivers of the operating system.
113+
*
114+
* By detaching the standard drivers, the operating system releases the exclusive access
115+
* to the device and/or its interfaces. It is relevant for USB devices implementing standard
116+
* USB classes such as HID, CDC or mass storage.
117+
*
118+
* Executing this function requires either root privileges or the _com.apple.vm.device-access_ entitlement.
119+
*
120+
* This method should be called before the device is opened. After the device has been closed,
121+
* `attach_standard_drivers()` should be called to restore the initial state.
122+
*/
123+
void detach_standard_drivers();
124+
125+
/**
126+
* Attaches the standard drivers of the operating system.
127+
*
128+
* By attaching the standard drivers, the original state before `detach_standard_drivers()` was called
129+
* is restored.
130+
*
131+
* Executing this function requires either root privileges or the _com.apple.vm.device-access_ entitlement.
132+
*
133+
* This method should be called after the device has been closed.
134+
*/
135+
void attach_standard_drivers();
136+
111137
/**
112138
* Get a USB endpoint.
113139
*

0 commit comments

Comments
 (0)