1852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake/*
21eff220474f63d7ea7f8f99bef2a3da9da5324ebhjelmn@cs.unm.edu * libusb example program to list devices on the bus
3a544e5972bf2b0ac9e006439576f681a8439d311Pete Batard * Copyright © 2007 Daniel Drake <dsd@gentoo.org>
4852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake *
5852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake * This library is free software; you can redistribute it and/or
6852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake * modify it under the terms of the GNU Lesser General Public
7852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake * License as published by the Free Software Foundation; either
8852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake * version 2.1 of the License, or (at your option) any later version.
9852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake *
10852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake * This library is distributed in the hope that it will be useful,
11852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake * but WITHOUT ANY WARRANTY; without even the implied warranty of
12852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake * Lesser General Public License for more details.
14852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake *
15852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake * You should have received a copy of the GNU Lesser General Public
16852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake * License along with this library; if not, write to the Free Software
17852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake */
19852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake
20852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake#include <stdio.h>
21852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake
2238e6eb86b2ceb301d67fd4754c5a1493f7911eceLudovic Rousseau#include "libusb.h"
23852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake
248392ff22136fccaf1e15d186157609b8dd127bc5Ludovic Rousseaustatic void print_devs(libusb_device **devs)
25852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake{
269cfdb494fccac53a4277da7c8b6d15f1a72a4959Daniel Drake	libusb_device *dev;
27ebac6ac1b30aea6e9190cce336eb75dc0f1c5ce7Hans de Goede	int i = 0, j = 0;
28ebac6ac1b30aea6e9190cce336eb75dc0f1c5ce7Hans de Goede	uint8_t path[8];
29852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake
309cfdb494fccac53a4277da7c8b6d15f1a72a4959Daniel Drake	while ((dev = devs[i++]) != NULL) {
31fe4adcc99e30115204ab832ad3e0170c9aca7629Daniel Drake		struct libusb_device_descriptor desc;
32fe4adcc99e30115204ab832ad3e0170c9aca7629Daniel Drake		int r = libusb_get_device_descriptor(dev, &desc);
33fe4adcc99e30115204ab832ad3e0170c9aca7629Daniel Drake		if (r < 0) {
34fe4adcc99e30115204ab832ad3e0170c9aca7629Daniel Drake			fprintf(stderr, "failed to get device descriptor");
35fe4adcc99e30115204ab832ad3e0170c9aca7629Daniel Drake			return;
36fe4adcc99e30115204ab832ad3e0170c9aca7629Daniel Drake		}
37fe4adcc99e30115204ab832ad3e0170c9aca7629Daniel Drake
38ebac6ac1b30aea6e9190cce336eb75dc0f1c5ce7Hans de Goede		printf("%04x:%04x (bus %d, device %d)",
39fe4adcc99e30115204ab832ad3e0170c9aca7629Daniel Drake			desc.idVendor, desc.idProduct,
403675e978fb7a1042f8601931255658bcd14a2298Daniel Drake			libusb_get_bus_number(dev), libusb_get_device_address(dev));
41ebac6ac1b30aea6e9190cce336eb75dc0f1c5ce7Hans de Goede
42f3fcf8402609d946d16eb2d9686bf76d7b0b8ae0Hans de Goede		r = libusb_get_port_numbers(dev, path, sizeof(path));
43f3fcf8402609d946d16eb2d9686bf76d7b0b8ae0Hans de Goede		if (r > 0) {
44f3fcf8402609d946d16eb2d9686bf76d7b0b8ae0Hans de Goede			printf(" path: %d", path[0]);
45f3fcf8402609d946d16eb2d9686bf76d7b0b8ae0Hans de Goede			for (j = 1; j < r; j++)
46f3fcf8402609d946d16eb2d9686bf76d7b0b8ae0Hans de Goede				printf(".%d", path[j]);
47ebac6ac1b30aea6e9190cce336eb75dc0f1c5ce7Hans de Goede		}
48ebac6ac1b30aea6e9190cce336eb75dc0f1c5ce7Hans de Goede		printf("\n");
49852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake	}
50852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake}
51852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake
52852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drakeint main(void)
53852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake{
549cfdb494fccac53a4277da7c8b6d15f1a72a4959Daniel Drake	libusb_device **devs;
55637a8d7ff8a11a23588925d9d3003a609bda8075Daniel Drake	int r;
565741bfe01a2481b8c3830c80edc3637bf62a7e16Daniel Drake	ssize_t cnt;
57637a8d7ff8a11a23588925d9d3003a609bda8075Daniel Drake
581df713d622ab4f0b03aad72d903ac7beb8fb3b90Daniel Drake	r = libusb_init(NULL);
59637a8d7ff8a11a23588925d9d3003a609bda8075Daniel Drake	if (r < 0)
60637a8d7ff8a11a23588925d9d3003a609bda8075Daniel Drake		return r;
61637a8d7ff8a11a23588925d9d3003a609bda8075Daniel Drake
621df713d622ab4f0b03aad72d903ac7beb8fb3b90Daniel Drake	cnt = libusb_get_device_list(NULL, &devs);
635878daa85e3364bb3885190425d4f9deaa2d8c36Daniel Drake	if (cnt < 0)
645878daa85e3364bb3885190425d4f9deaa2d8c36Daniel Drake		return (int) cnt;
65852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake
66852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake	print_devs(devs);
679cfdb494fccac53a4277da7c8b6d15f1a72a4959Daniel Drake	libusb_free_device_list(devs, 1);
68852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake
691df713d622ab4f0b03aad72d903ac7beb8fb3b90Daniel Drake	libusb_exit(NULL);
70852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake	return 0;
71852bba4754ec57679c823f33e8feba6e4a564cbDaniel Drake}
72