1 year ago

#156234

test-img

E.Wolf

PyUSB error (Python3, Linux, PyUSB, AudioInterface)

I'm using python3 and PyUSB library for communicate with my USB soundcard. OS: Linux Ubuntu 20.04. Python3 code:

import usb.core
import usb.util

dev = usb.core.find(idVendor=0x2708, idProduct=0x0008)

if dev is None:
    raise ValueError('Device not found')

reattach = False
if dev.is_kernel_driver_active(0):
    reattach = True
    dev.detach_kernel_driver(0)

usb.util.claim_interface(dev, 0)
dev.reset()

dev.set_configuration()

dataFragment = b'\x00'
assert dev.ctrl_transfer(0x21, 0x1, 0x0203, 0x0a00, dataFragment) == len(dataFragment)
assert dev.ctrl_transfer(0x21, 0x1, 0x0204, 0x0a00, dataFragment) == len(dataFragment)


usb.util.dispose_resources(dev)


if reattach:
    dev.attach_kernel_driver(0)

Content of my /etc/udev/rules.d/99-audio.rules file:

SUBSYSTEM=="usb", ATTR{idVendor}=="2708", ATTR{idProduct}=="0008", MODE="0666"

Try to run it from user (non root), PyUSB debug enabled:

2022-02-09 10:42:55,046 DEBUG:usb.backend.libusb1:_LibUSB.__init__(<CDLL 'libusb-1.0.so.0', handle 1a1f4d0 at 0x7f48d54d4910>)
2022-02-09 10:42:55,048 INFO:usb.core:find(): using backend "usb.backend.libusb1"
2022-02-09 10:42:55,048 DEBUG:usb.backend.libusb1:_LibUSB.enumerate_devices()
2022-02-09 10:42:55,048 DEBUG:usb.backend.libusb1:_LibUSB.get_device_descriptor(<usb.backend.libusb1._Device object at 0x7f48d53172e0>)
2022-02-09 10:42:55,049 DEBUG:usb.backend.libusb1:_LibUSB.open_device(<usb.backend.libusb1._Device object at 0x7f48d53172e0>)
2022-02-09 10:42:55,049 DEBUG:usb.backend.libusb1:_LibUSB.is_kernel_driver_active(<usb.backend.libusb1._DeviceHandle object at 0x7f48d568ef10>, 0)
2022-02-09 10:42:55,049 DEBUG:usb.backend.libusb1:_LibUSB.claim_interface(<usb.backend.libusb1._DeviceHandle object at 0x7f48d568ef10>, 0)
2022-02-09 10:42:55,049 DEBUG:usb.backend.libusb1:_LibUSB.release_interface(<usb.backend.libusb1._DeviceHandle object at 0x7f48d568ef10>, 0)
2022-02-09 10:42:55,049 DEBUG:usb.backend.libusb1:_LibUSB.reset_device(<usb.backend.libusb1._DeviceHandle object at 0x7f48d568ef10>)
2022-02-09 10:42:56,011 DEBUG:usb.backend.libusb1:_LibUSB.close_device(<usb.backend.libusb1._DeviceHandle object at 0x7f48d568ef10>)
2022-02-09 10:42:56,011 DEBUG:usb.backend.libusb1:_LibUSB.get_configuration_descriptor(<usb.backend.libusb1._Device object at 0x7f48d53172e0>, 0)
2022-02-09 10:42:56,011 DEBUG:usb.backend.libusb1:_LibUSB.open_device(<usb.backend.libusb1._Device object at 0x7f48d53172e0>)
2022-02-09 10:42:56,011 DEBUG:usb.backend.libusb1:_LibUSB.set_configuration(<usb.backend.libusb1._DeviceHandle object at 0x7f48d56a61f0>, 1)
Traceback (most recent call last):
  File "evo4-settings.py", line 18, in <module>
    dev.set_configuration()
  File "/home/ewolf/.local/lib/python3.8/site-packages/usb/core.py", line 915, in set_configuration
    self._ctx.managed_set_configuration(self, configuration)
  File "/home/ewolf/.local/lib/python3.8/site-packages/usb/core.py", line 113, in wrapper
    return f(self, *args, **kwargs)
  File "/home/ewolf/.local/lib/python3.8/site-packages/usb/core.py", line 159, in managed_set_configuration
    self.backend.set_configuration(self.handle, cfg.bConfigurationValue)
  File "/home/ewolf/.local/lib/python3.8/site-packages/usb/_debug.py", line 62, in do_trace
    return f(*args, **named_args)
  File "/home/ewolf/.local/lib/python3.8/site-packages/usb/backend/libusb1.py", line 812, in set_configuration
    _check(self.lib.libusb_set_configuration(dev_handle.handle, config_value))
  File "/home/ewolf/.local/lib/python3.8/site-packages/usb/backend/libusb1.py", line 604, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 16] Resource busy
2022-02-09 10:42:56,078 DEBUG:usb.backend.libusb1:_LibUSB.close_device(<usb.backend.libusb1._DeviceHandle object at 0x7f48d56a61f0>)
2022-02-09 10:42:56,079 DEBUG:usb.backend.libusb1:_LibUSB._finalize_object()

Try to run it from root (sudo):

Traceback (most recent call last):
  File "evo4-settings.py", line 18, in <module>
    dev.set_configuration()
  File "/usr/local/lib/python3.8/dist-packages/usb/core.py", line 915, in set_configuration
    self._ctx.managed_set_configuration(self, configuration)
  File "/usr/local/lib/python3.8/dist-packages/usb/core.py", line 113, in wrapper
    return f(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/usb/core.py", line 159, in managed_set_configuration
    self.backend.set_configuration(self.handle, cfg.bConfigurationValue)
  File "/usr/local/lib/python3.8/dist-packages/usb/backend/libusb1.py", line 812, in set_configuration
    _check(self.lib.libusb_set_configuration(dev_handle.handle, config_value))
  File "/usr/local/lib/python3.8/dist-packages/usb/backend/libusb1.py", line 604, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 16] Resource busy

My code based on python this example: https://github.com/vijay-prema/audient-evo-linux-tools/blob/main/evo4-settings.py and on that https://github.com/r00tman/mymixer/blob/master/main.c

I change only idProduct code from 6 to 8 and dataFragments for my device.

Example of C does not work too, fallen on line 91: assert(devh != NULL); without details.

How i can fix it error "Error 16 Resource busy"?

python

ubuntu

usb

pyusb

0 Answers

Your Answer

Accepted video resources