kusano 7d535a
/*
kusano 7d535a
 * libusb example program to list devices on the bus
kusano 7d535a
 * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org></dsd@gentoo.org>
kusano 7d535a
 *
kusano 7d535a
 * This library is free software; you can redistribute it and/or
kusano 7d535a
 * modify it under the terms of the GNU Lesser General Public
kusano 7d535a
 * License as published by the Free Software Foundation; either
kusano 7d535a
 * version 2.1 of the License, or (at your option) any later version.
kusano 7d535a
 *
kusano 7d535a
 * This library is distributed in the hope that it will be useful,
kusano 7d535a
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
kusano 7d535a
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
kusano 7d535a
 * Lesser General Public License for more details.
kusano 7d535a
 *
kusano 7d535a
 * You should have received a copy of the GNU Lesser General Public
kusano 7d535a
 * License along with this library; if not, write to the Free Software
kusano 7d535a
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#include <stdio.h></stdio.h>
kusano 7d535a
#include <sys types.h=""></sys>
kusano 7d535a
kusano 7d535a
#include <libusb.h></libusb.h>
kusano 7d535a
kusano 7d535a
static void print_devs(libusb_device **devs)
kusano 7d535a
{
kusano 7d535a
	libusb_device *dev;
kusano 7d535a
	int i = 0;
kusano 7d535a
kusano 7d535a
	while ((dev = devs[i++]) != NULL) {
kusano 7d535a
		struct libusb_device_descriptor desc;
kusano 7d535a
		int r = libusb_get_device_descriptor(dev, &desc);
kusano 7d535a
		if (r < 0) {
kusano 7d535a
			fprintf(stderr, "failed to get device descriptor");
kusano 7d535a
			return;
kusano 7d535a
		}
kusano 7d535a
kusano 7d535a
		printf("%04x:%04x (bus %d, device %d)\n",
kusano 7d535a
			desc.idVendor, desc.idProduct,
kusano 7d535a
			libusb_get_bus_number(dev), libusb_get_device_address(dev));
kusano 7d535a
	}
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
int main(void)
kusano 7d535a
{
kusano 7d535a
	libusb_device **devs;
kusano 7d535a
	int r;
kusano 7d535a
	ssize_t cnt;
kusano 7d535a
kusano 7d535a
	r = libusb_init(NULL);
kusano 7d535a
	if (r < 0)
kusano 7d535a
		return r;
kusano 7d535a
kusano 7d535a
	cnt = libusb_get_device_list(NULL, &devs);
kusano 7d535a
	if (cnt < 0)
kusano 7d535a
		return (int) cnt;
kusano 7d535a
kusano 7d535a
	print_devs(devs);
kusano 7d535a
	libusb_free_device_list(devs, 1);
kusano 7d535a
kusano 7d535a
	libusb_exit(NULL);
kusano 7d535a
	return 0;
kusano 7d535a
}
kusano 7d535a