From 21b0ed46ae7526407db2238f8cf41e3ee9997a36 Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Wed, 23 May 2018 10:08:10 +0200 Subject: [PATCH] Set alternate setting for the interface only if required. (#38) Spec says devices must support "set interface" if they have more than one alternate setting. --- config.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index 70e2330..eae1828 100644 --- a/config.go +++ b/config.go @@ -124,9 +124,12 @@ func (c *Config) Interface(num, alt int) (*Interface, error) { return nil, fmt.Errorf("failed to claim interface %d on %s: %v", num, c, err) } - if err := c.dev.ctx.libusb.setAlt(c.dev.handle, uint8(num), uint8(alt)); err != nil { - c.dev.ctx.libusb.release(c.dev.handle, uint8(num)) - return nil, fmt.Errorf("failed to set alternate config %d on interface %d of %s: %v", alt, num, c, err) + // Select an alternate setting if needed (device has multiple alternate settings). + if len(c.Desc.Interfaces[num].AltSettings) > 1 { + if err := c.dev.ctx.libusb.setAlt(c.dev.handle, uint8(num), uint8(alt)); err != nil { + c.dev.ctx.libusb.release(c.dev.handle, uint8(num)) + return nil, fmt.Errorf("failed to set alternate config %d on interface %d of %s: %v", alt, num, c, err) + } } c.claimed[num] = true