Skip to content

Commit

Permalink
Reattach the kernel driver on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
puhitaku committed Feb 7, 2023
1 parent a936fd3 commit 5b37caa
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion mtp/device_direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type DeviceDirect struct {
h *usb.DeviceHandle
dev *usb.Device

claimed bool
claimed bool
detached bool

// split off descriptor?
devDescr usb.DeviceDescriptor
Expand Down Expand Up @@ -75,6 +76,15 @@ func (d *DeviceDirect) Close() error {
log.USB.Debugf("releaseInterface 0x%x, err: %v", d.ifaceDescr.InterfaceNumber, err)
}
}

if d.detached {
log.USB.Infof("Reattaching kernel driver")
err := d.h.AttachKernelDriver(d.ifaceDescr.InterfaceNumber)
if d.Debug.USB {
log.USB.Debugf("attachKernelDriver 0x%x, err: %v", d.ifaceDescr.InterfaceNumber, err)
}
}

err := d.h.Close()
d.h = nil

Expand Down Expand Up @@ -151,6 +161,8 @@ func (d *DeviceDirect) Open() error {
if err != nil {
return fmt.Errorf("failed to detach the kernel driver: %w", err)
}

d.detached = true
}
}

Expand Down

0 comments on commit 5b37caa

Please sign in to comment.