سؤال

I'm attempting to do bulk reads and writes from a USB device on Ubuntu using PyUSB. However, i've been unsuccessful at getting that far.

import usb.core
import usb.util

dev = usb.core.find(idVendor=0xXXXX,idProduct=0xYYYY)
if dev is None:
    raise ValueError('Device not found.')

try:
    dev.detach_kernel_driver(0)
except:
    print "exception dev.detach_kernel_driver(0)"
    pass

dev.set_configuration()
print "all done"

This is the simple script I'm using. I've created /etc/udev/rules.d/40-basic-rules.rules which contains

SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device",SYSFS{idVendor}=="XXXX" , SYSFS{idProduct}=="YYYY", MODE="0666"

for my appropriate device.

Running the script as is as root raises a usb.core.USBError: [Errno 16] Resource busy error because the dev.detach_kernel_driver(0) throws the exception usb.core.USBError: [Errno 2] Entity not found

in dmesg I see these messages,

[  638.007886] usb 1-1: usbfs: interface 1 claimed by usb-storage while 'python' sets config #1
[  643.425802] usb 1-1: usbfs: interface 1 claimed by usb-storage while 'python' sets config #1
[  647.957932] usb 1-1: usbfs: interface 1 claimed by usb-storage while 'python' sets config #1

Any thoughts on what I'm missing to get access to this device?

هل كانت مفيدة؟

المحلول

Your problem, like mine, is that you need to detach the kernel from each and every interface before you can set_configuration(). Here's the code I am using right now (including some scaffolding) for connecting to a USB audio device:

import usb.core
import usb.util

scarlet = usb.core.find(idVendor = 0x1235)  # Focusrite
if not scarlet: print"No Scarlet"

c = 1
for config in scarlet:
    print 'config', c
    print 'Interfaces', config.bNumInterfaces
    for i in range(config.bNumInterfaces):
        if scarlet.is_kernel_driver_active(i):
            scarlet.detach_kernel_driver(i)
        print i
    c+=1

scarlet.set_configuration()
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top