User mode access to Brother PT-P700 label printer¶
The problem¶
I wanted to use the Python Labelprinterkit library to print labels from a Brother PT-P700 label printer. But I kept getting this error when trying to print:
usb.core.USBError: [Errno 16] Resource busy
The solution¶
After some digging I found that a kernel line-printer driver had attached
to the device, and I needed to find a way to detach it. A simple way might
have been to blacklist the usblp driver, but that seemed like a clumsy
way to do it.
It literally took me hours to figure out how to do it. I don't know if this was my failing or just that search engines are horrible these days. I find it hard to believe that nobody else has run into this issue. Eventually I found this post which put me on the right path.
It still seems like an overcomplicated, wonky way to do it. It shouldn't have to take running a shell command to say something as basic as "do not bind a kernel driver to this device"!
Anyway, once the kernel driver didn't bind to the device any more, I could
access it, but then I got an error saying that the device had no active USB
configuration. A little more digging and experimenting eventually got me
the correct magic incantation. The result is a file
/etc/udev/rules.d/71-brother-pt-p700.rules that contains:
ACTION!="remove", SUBSYSTEMS=="usb", ATTRS{idVendor}=="04f9", ATTRS{idProduct}=="2061", MODE="0660", TAG+="uaccess", RUN+="/bin/sh -c 'echo -n %k >%S%p/driver/unbind'", ATTR{bConfigurationValue}="1"
The TAG thing gives the user access, the RUN part unbinds the kernel
driver and the ATTR{bConfigurationValue} assignment takes care of selecting
an active USB configuration.
Hopefully this can save some other poor sap a few hours of frustration!