Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,17 @@ func (d *Device) SetAutoDetach(autodetach bool) error {
}
return d.ctx.libusb.setAutoDetach(d.handle, autodetachInt)
}

// SetAutoDetachLibOnly is similar to SetAutoDetach, except it will not
// flag the Device instance to detach all interfaces when changing Configs.
// This will only call the libusb implementation of auto detach.
func (d *Device) SetAutoDetachLibOnly(autodetach bool) error {
Copy link
Collaborator

@zagrodzki zagrodzki Jun 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not add a new method. We can fit this into the current function though, using variadic options:

// DetachOption allow fine-tuning of the SetAutoDetach behavior.
type DetachOption interface {
  update(d *Device)
}

// DetachAllInterfaces is a DetachOption that controls the behavior of Config(). If SetAutoDetach is called
// with DetachAllInterfaces(true), subsequent calls to Config() will proactively detach all interfaces of the
// specified device configuration.
// If SetAutoDetach is called with DetachAllInterfaces(bool), interfaces will not be explicitly detached and
// only the kernel auto-detach is enabled.
// The default behavior is to detach all interfaces if autodetach is enabled and
// not detach all interfaces if autodetach is disabled.
type DetachAllInterfaces bool

func (val DetachAllInterfaces) update(d *Device) {
  d.autodetach = bool(val)
}

func (d *Device) SetAutoDetach(autodetach bool, opts ...DetachOption) error {
  if d.handle == nil { ... }
  d.autodetach = autodetach
  for _, o := range opts {
    o.update(d)
  }
  ...
  ...setAutoDetach(...)
}

Also perhaps add a TODO to offer a better interface for the next major release - autodetach should only set autodetach, while controlling the interfaces explicitly should be done through another option.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then your use case is represented as:

d.SetAutoDetach(true, DetachAllInterfaces(false))

if d.handle == nil {
return fmt.Errorf("SetAutoDetachLibOnly(%v) called on %s after Close", autodetach, d)
}
var autodetachInt int
if autodetach {
autodetachInt = 1
}
return d.ctx.libusb.setAutoDetach(d.handle, autodetachInt)
}