NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

libusb-1.0, does it even work?



I'm looking into why the package misc/calibre doesn't seem to see any
connected USB devices, and I'm beginning to think that libusb1 simply
doesn't work.

Below is a test program I've reduced from what Calibre does to scan for
usb devices (it requires libusb-1.0 from pkgsrc).

It always prints this, run as root or not, even though I have at least a
USB mouse plugged in:

$ ./usb
libusb_init: 0
count = 0

ktracing and looking at NAMI lines, it looks at

 23605      1 usb      NAMI  "/dev/ugen0.00"
 23605      1 usb      NAMI  "/dev/ugen1.00"
 23605      1 usb      NAMI  "/dev/ugen2.00"
 23605      1 usb      NAMI  "/dev/ugen3.00"

etc. 

Is this even a way to detect plugged-in devices?
Why does it ignore /dev/ugen0.01 ... /dev/ugen0.15, etc?

Why doesn't the devel/libusb1 package install a manual page?

Test program: run it as a shell script to compile.

# /*
gcc -I/usr/pkg/include -L/usr/pkg/lib -R/usr/pkg/lib usb.c -lusb-1.0 -o usb
exit $?
*/

#include <stdio.h>
#include <libusb-1.0/libusb.h>

static void format_err(int err) {
    printf("error: %s\n", libusb_error_name(err));
}

static unsigned char *read_string_property(libusb_device_handle *dev, uint8_t idx) {
    static unsigned char buf[301];
    int err;

    err = libusb_get_string_descriptor_ascii(dev, idx, buf, 300);

    //printf("read_string_property: %s\n", buf);
    return buf;
}

static void read_string_data(libusb_device *dev, uint8_t manufacturer, uint8_t product, uint8_t serial) {
    libusb_device_handle *handle;
    int err;
    char *p;

    err = libusb_open(dev, &handle);

    if (err == 0) {
        p = read_string_property(handle, manufacturer);
        if (p != NULL) { printf("manufacturer: %s", p); }

        p = read_string_property(handle, product);
        if (p != NULL) { printf("product: %s", p); }

        p = read_string_property(handle, serial);
        if (p != NULL) { printf("serial: %s", p); }

        libusb_close(handle);
    }
}

static void get_devices(void) {
    int err, i = 0;
    libusb_device **devs = NULL, *dev = NULL;
    ssize_t count;
    libusb_context *ctx = NULL;

    err = libusb_init(NULL);	// &ctx or NULL
    printf("libusb_init: %d\n", err);
    if (err) {
	format_err(count);
    }
    count = libusb_get_device_list(ctx, &devs);
    printf("count = %d\n", count);
    if (count < 0) {
	format_err(count);
	return;
    }

    while ( (dev = devs[i++]) != NULL ) {
        struct libusb_device_descriptor desc;
        err = libusb_get_device_descriptor(dev, &desc);
        if (err != 0) { format_err(err); break; }
        if (desc.bDeviceClass == LIBUSB_CLASS_HUB) continue;

	printf("libusb_get_bus_number: %d\n", libusb_get_bus_number(dev));
	printf("libusb_get_device_address: %d\n", libusb_get_device_address(dev));
	printf("idVendor: %d\n", desc.idVendor);
	printf("idProduct: %d\n", desc.idProduct);
	printf("bcdDevice: %d\n", desc.bcdDevice);

	read_string_data(dev, desc.iManufacturer, desc.iProduct, desc.iSerialNumber);
    }

    if (devs != NULL) libusb_free_device_list(devs, 1);
    libusb_exit(ctx);
}

int main(int argc, char *argv[])
{
    get_devices();

    return 0;
}


-Olaf.
-- 
___ "Buying carbon credits is a bit like a serial killer paying someone else to
\X/  have kids to make his activity cost neutral." -The BOFH    falu.nl@rhialto

Attachment: signature.asc
Description: PGP signature



Home | Main Index | Thread Index | Old Index