Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libusb0 backend kernel driver detach for macOS with libusb 1.0.25 #374

Closed
mcuee opened this issue Jun 27, 2021 · 3 comments
Closed

libusb0 backend kernel driver detach for macOS with libusb 1.0.25 #374

mcuee opened this issue Jun 27, 2021 · 3 comments

Comments

@mcuee
Copy link
Member

mcuee commented Jun 27, 2021

One of the major change for the upcoming libusb 1.0.25 release is to add kernel driver detaching for macOS. This change should not affect libusb1 backend of pyusb. However, the current libusb0 has hard coded codes saying the feature is only available for Linux. This may need to be fixed.

libusb/libusb#911
https://github.com/libusb/libusb/blob/master/ChangeLog
2021-05-16: v1.0.25-pre

  • Darwin (macOS): Add support for detaching kernel drivers with authorization.
@mcuee mcuee changed the title libusb0 backend kernel driver detach for macOS with latest git libusb libusb0 backend kernel driver detach for macOS with libusb 1.0.25 Jun 27, 2021
@mcuee
Copy link
Member Author

mcuee commented Jun 27, 2021

Take note macOS users will use libusb-compat-0.1 which is just a wrapper of libusb-1.0.
https://github.com/libusb/libusb-compat-0.1/blob/master/libusb/core.c

@jonasmalacofilho
Copy link
Member

We don't restrict kernel detachment on Darwin, even on libusb0: we only check that usb_detach_kernel_driver_np() is present and delegate everything to it.

@methodtrace(_logger)
def detach_kernel_driver(self, dev_handle, intf):
if not hasattr(_lib, 'usb_detach_kernel_driver_np'):
raise NotImplementedError(self.detach_kernel_driver.__name__)
_check(_lib.usb_detach_kernel_driver_np(dev_handle, intf))

What I think will be an issue is our is_kernel_driver_active() backend method, because there we do have some custom logic, to handle all the different semantics usb_get_driver_np() may have depending on the implementation.

@methodtrace(_logger)
def is_kernel_driver_active(self, dev_handle, intf):
if sys.platform == 'linux':
# based on the implementation of libusb_kernel_driver_active()
# (see op_kernel_driver_active() in libusb/os/linux_usbfs.c)
# and the fact that usb_get_driver_np() is a wrapper for
# IOCTL_USBFS_GETDRIVER
try:
driver_name = self.__get_driver_name(dev_handle, intf)
# 'usbfs' is not considered a [foreign] kernel driver because
# it is what we use to access the device from userspace
return driver_name != b'usbfs'
except USBError as err:
# ENODATA means that no kernel driver is attached
if err.backend_error_code == -errno.ENODATA:
return False
raise
elif sys.platform.startswith('freebsd') or sys.platform.startswith('dragonfly'):
# this is similar to the Linux implementation, but the generic
# driver is called 'ugen' and usb_get_driver_np() simply returns an
# empty string is no driver is attached (see comments on PR #366)
driver_name = self.__get_driver_name(dev_handle, intf)
# 'ugen' is not considered a [foreign] kernel driver because
# it is what we use to access the device from userspace
return driver_name != b'ugen'
else:
raise NotImplementedError(self.is_kernel_driver_active.__name__)

Can we assume all Darwin users will be running libusb-compat-0.1? Because if that's the case, the required change is trivial ('dummy' => True, -ENODATA => False).

@mcuee
Copy link
Member Author

mcuee commented Jun 27, 2021

Yes we can safe to assume all Darwin users to use libusb-compat-0.1 now (or even since many years ago). Nathan has been with the libusb project since the 0.1 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants