Skip to content
This repository was archived by the owner on Feb 2, 2026. It is now read-only.

Commit 4a91d49

Browse files
committed
Detach kernel drivers only from required interfaces and restore when disconnecting
1 parent 6b8a83c commit 4a91d49

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

usbtmc/usbtmc.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ def __init__(self, *args, **kwargs):
193193
self.last_rstb_btag = 0
194194

195195
self.connected = False
196+
self.reattach = []
197+
self.old_config = None
196198

197199
resource = None
198200

@@ -273,15 +275,33 @@ def open(self):
273275
if self.iface is None:
274276
raise UsbtmcException("Not a USBTMC device", 'init')
275277

276-
# release kernel driver
277-
if os.name == 'posix':
278-
if self.device.is_kernel_driver_active(self.iface.bInterfaceNumber):
279-
try:
280-
self.device.detach_kernel_driver(self.iface.bInterfaceNumber)
281-
except usb.core.USBError as e:
282-
sys.exit("Could not detatch kernel driver from interface({0}): {1}".format(self.iface.bInterfaceNumber, str(e)))
278+
self.old_config = self.device.get_active_configuration()
279+
280+
if self.device.get_active_configuration().bConfigurationValue == self.cfg.bConfigurationValue:
281+
# already set to correct configuration
283282

284-
if self.device.get_active_configuration().bConfigurationValue != self.cfg.bConfigurationValue:
283+
# release kernel driver on USBTMC interface
284+
if os.name == 'posix':
285+
if self.device.is_kernel_driver_active(self.iface.bInterfaceNumber):
286+
self.reattach.append(self.iface.bInterfaceNumber)
287+
try:
288+
self.device.detach_kernel_driver(self.iface.bInterfaceNumber)
289+
except usb.core.USBError as e:
290+
sys.exit("Could not detatch kernel driver from interface({0}): {1}".format(self.iface.bInterfaceNumber, str(e)))
291+
else:
292+
# wrong configuration
293+
294+
# release all kernel drivers
295+
if os.name == 'posix':
296+
for iface in self.device.get_active_configuration():
297+
if self.device.is_kernel_driver_active(iface.bInterfaceNumber):
298+
self.reattach.append(iface.bInterfaceNumber)
299+
try:
300+
self.device.detach_kernel_driver(iface.bInterfaceNumber)
301+
except usb.core.USBError as e:
302+
sys.exit("Could not detatch kernel driver from interface({0}): {1}".format(iface.bInterfaceNumber, str(e)))
303+
304+
# set proper configuration
285305
self.device.set_configuration(self.cfg)
286306

287307
# don't need to set altsetting - USBTMC devices have 1 altsetting as per the spec
@@ -323,6 +343,22 @@ def close(self):
323343

324344
usb.util.dispose_resources(self.device)
325345

346+
try:
347+
# reset configuration
348+
if self.device.get_active_configuration().bConfigurationValue != self.old_config.bConfigurationValue:
349+
self.device.set_configuration(self.old_config)
350+
351+
# try to reattach kernel driver
352+
for iface in self.reattach:
353+
try:
354+
self.device.attach_kernel_driver(iface)
355+
except:
356+
pass
357+
except:
358+
pass
359+
360+
self.reattach = []
361+
326362
self.connected = False
327363

328364
def is_usb488(self):

0 commit comments

Comments
 (0)