Skip to content

Commit 72a1620

Browse files
committed
If Device Info Descriptor can't be obtained, fall back to reasonable default
Canon SELPHY CP1200 fails to return class-specific device descriptor (see #33). As a workaround for hardware with similar problems, any failure to obtain Device Info Descriptor is silently ignored, using the reasonable default for device capabilities
1 parent 1045046 commit 72a1620

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

usbio_libusb.go

+21-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ package main
1111
import (
1212
"encoding/binary"
1313
"errors"
14-
"fmt"
1514
"sync"
1615
"sync/atomic"
1716
"time"
@@ -471,16 +470,10 @@ func (devhandle *UsbDevHandle) UsbDeviceInfo() (UsbDeviceInfo, error) {
471470
return info, UsbError{"libusb_get_device_descriptor", UsbErrCode(rc)}
472471
}
473472

474-
// Obtain device capabilities
475-
caps, err := devhandle.usbIppBasicCaps()
476-
if err != nil {
477-
return info, err
478-
}
479-
480473
// Decode device descriptor
481474
info.Vendor = uint16(c_desc.idVendor)
482475
info.Product = uint16(c_desc.idProduct)
483-
info.BasicCaps = caps
476+
info.BasicCaps = devhandle.usbIppBasicCaps()
484477

485478
buf := make([]byte, 256)
486479

@@ -518,7 +511,16 @@ func (devhandle *UsbDevHandle) UsbDeviceInfo() (UsbDeviceInfo, error) {
518511
// capabilities
519512
//
520513
// See IPP USB specification, section 4.3 for details
521-
func (devhandle *UsbDevHandle) usbIppBasicCaps() (UsbIppBasicCaps, error) {
514+
//
515+
// This function never fails. In a case of errors, it fall backs
516+
// to the reasonable default
517+
func (devhandle *UsbDevHandle) usbIppBasicCaps() (caps UsbIppBasicCaps) {
518+
// Safe default
519+
caps = UsbIppBasicCapsPrint |
520+
UsbIppBasicCapsScan |
521+
UsbIppBasicCapsFax |
522+
UsbIppBasicCapsAnyHTTP
523+
522524
// Buffer length
523525
const bufLen = 256
524526

@@ -532,17 +534,24 @@ func (devhandle *UsbDevHandle) usbIppBasicCaps() (UsbIppBasicCaps, error) {
532534
bufLen)
533535

534536
if rc < 0 {
535-
return 0, UsbError{"libusb_get_descriptor(0x21)", UsbErrCode(rc)}
537+
// Some devices doesn't properly return class-specific
538+
// device descriptor, so ignore an error
539+
return
536540
}
537541

538542
if rc < 10 {
539-
return 0, fmt.Errorf("Bad Device Info Descriptor: %x", buf)
543+
// Malformed response, fall back to default
544+
return
540545
}
541546

542547
// Decode basic capabilities bits
543548
bits := binary.LittleEndian.Uint16(buf[6:8])
549+
if bits == 0 {
550+
// Paranoia. If no caps, return default
551+
return
552+
}
544553

545-
return UsbIppBasicCaps(bits), nil
554+
return UsbIppBasicCaps(bits)
546555
}
547556

548557
// OpenUsbInterface opens an interface

0 commit comments

Comments
 (0)