Re: usbutils source tree [was Re: [patch 0/7] Add usb root debugfs...]

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

 



On Sat, Apr 25, 2009 at 12:26:55PM -0700, Greg KH wrote:
> On Sat, Apr 25, 2009 at 02:43:57PM -0400, Alan Stern wrote:
> > Out of curiosity, I took a look at the source files.  lsusb does use 
> > libusb after all!  Are you going to switch it over to pure sysfs and 
> > libudev?
> 
> I wasn't planning on doing that, I was going to switch usbview over to
> just use sysfs and libudev.
> 
> We could switch lsusb to use just sysfs, but I don't think it would make
> much sense to use libudev, do you?  Although it might make enumerating
> the USB devices easier...  Hm, maybe I'll think about it, depending on
> how well the usbview changes go.
> 
> Actually, the difference between usbview and lsusb is pretty small these
> days, maybe we can just make usbview the "gtk" version of lsusb :)
> 
> I have 2 days of sitting with sick kids, watching hours of Sponge Bob,
> so I'll see how it goes...

Damm, libudev makes things very easy, nice job Kay.

Here's the equivalent of a "stock" lsusb output in 70 lines of C code,
using libudev instead of libusb.

"original lsusb":
$ lsusb
Bus 004 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 004: ID 08ff:2580 AuthenTec, Inc. AES2501 Fingerprint Sensor
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

"test program":
$ ./test
Bus 001 Device 001: ID 1d6b:0001 Linux 2.6.29 uhci_hcd UHCI Host Controller
Bus 002 Device 001: ID 1d6b:0001 Linux 2.6.29 uhci_hcd UHCI Host Controller
Bus 003 Device 001: ID 1d6b:0001 Linux 2.6.29 uhci_hcd UHCI Host Controller
Bus 003 Device 004: ID 08ff:2580 Fingerprint Sensor
Bus 004 Device 001: ID 1d6b:0002 Linux 2.6.29 ehci_hcd EHCI Host Controller

Not bad...

I can switch over to just parsing the descriptor directly from the
"descriptors" file in sysfs, and probably not need libusb at all...

fun stuff.

greg k-h

------------------

#include <stdio.h>
#include <string.h>
#include "libudev.h"

static void print_device(struct udev_device *device)
{
	const char *busnum_str;
	const char *devnum_str;
	const char *vendor_id;
	const char *product_id;
	const char *manufacturer_str;
	const char *product_str;
	int busnum, devnum;

	vendor_id = udev_device_get_sysattr_value(device, "idVendor");
	product_id = udev_device_get_sysattr_value(device, "idProduct");
	busnum_str = udev_device_get_sysattr_value(device, "busnum");
	devnum_str = udev_device_get_sysattr_value(device, "devnum");
	manufacturer_str = udev_device_get_sysattr_value(device, "manufacturer");
	product_str = udev_device_get_sysattr_value(device, "product");

	busnum = atoi(busnum_str);
	devnum = atoi(devnum_str);
	printf("Bus %.03d Device %.03d: ID %s:%s",
		busnum, devnum, vendor_id, product_id);
	if (manufacturer_str)
		printf(" %s", manufacturer_str);
	if (product_str)
		printf(" %s", product_str);
	printf("\n");
}

static int test_enumerate_print_list(struct udev_enumerate *enumerate)
{
	struct udev_list_entry *list_entry;
	const char *str;

	udev_list_entry_foreach(list_entry,
				udev_enumerate_get_list_entry(enumerate)) {
		struct udev_device *device;

		device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
						      udev_list_entry_get_name(list_entry));
		if (device != NULL) {
			str = udev_device_get_devtype(device);
			if ((str != NULL) &&
			    (strcmp(str, "usb_device") == 0))
				print_device(device);
			udev_device_unref(device);
		}
	}
	return 0;
}

int main(int argc, char *argv[])
{
	struct udev *udev = NULL;
	struct udev_enumerate *udev_enumerate;

	udev = udev_new();
	udev_enumerate = udev_enumerate_new(udev);
	if (udev_enumerate == NULL)
		return -1;
	udev_enumerate_add_match_subsystem(udev_enumerate, "usb");
	udev_enumerate_scan_devices(udev_enumerate);
	test_enumerate_print_list(udev_enumerate);
	udev_enumerate_unref(udev_enumerate);
	udev_unref(udev);
	return 0;
}
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux