blob: b5b027cffdf5c674c3aa6368fc8d871cf1f3ddcf [file] [log] [blame]
Daniel Drake852bba42007-11-28 13:48:45 +00001/*
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +00002 * libusb example program to list devices on the bus
Pete Batarda544e592012-05-23 18:25:01 +01003 * Copyright © 2007 Daniel Drake <dsd@gentoo.org>
Daniel Drake852bba42007-11-28 13:48:45 +00004 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <stdio.h>
21
Ludovic Rousseau38e6eb82012-10-12 23:28:51 +020022#include "libusb.h"
Daniel Drake852bba42007-11-28 13:48:45 +000023
Ludovic Rousseau8392ff22009-10-25 10:06:41 +010024static void print_devs(libusb_device **devs)
Daniel Drake852bba42007-11-28 13:48:45 +000025{
Daniel Drake9cfdb492008-03-06 23:25:20 +000026 libusb_device *dev;
Hans de Goedeebac6ac2013-05-16 22:36:26 +020027 int i = 0, j = 0;
28 uint8_t path[8];
Daniel Drake852bba42007-11-28 13:48:45 +000029
Daniel Drake9cfdb492008-03-06 23:25:20 +000030 while ((dev = devs[i++]) != NULL) {
Daniel Drakefe4adcc2008-05-09 14:34:31 +010031 struct libusb_device_descriptor desc;
32 int r = libusb_get_device_descriptor(dev, &desc);
33 if (r < 0) {
34 fprintf(stderr, "failed to get device descriptor");
35 return;
36 }
37
Hans de Goedeebac6ac2013-05-16 22:36:26 +020038 printf("%04x:%04x (bus %d, device %d)",
Daniel Drakefe4adcc2008-05-09 14:34:31 +010039 desc.idVendor, desc.idProduct,
Daniel Drake3675e972008-05-02 12:28:31 +010040 libusb_get_bus_number(dev), libusb_get_device_address(dev));
Hans de Goedeebac6ac2013-05-16 22:36:26 +020041
Hans de Goedef3fcf842013-05-17 10:42:47 +020042 r = libusb_get_port_numbers(dev, path, sizeof(path));
43 if (r > 0) {
44 printf(" path: %d", path[0]);
45 for (j = 1; j < r; j++)
46 printf(".%d", path[j]);
Hans de Goedeebac6ac2013-05-16 22:36:26 +020047 }
48 printf("\n");
Daniel Drake852bba42007-11-28 13:48:45 +000049 }
50}
51
52int main(void)
53{
Daniel Drake9cfdb492008-03-06 23:25:20 +000054 libusb_device **devs;
Daniel Drake637a8d72008-01-30 13:19:10 +000055 int r;
Daniel Drake5741bfe2008-05-09 14:36:14 +010056 ssize_t cnt;
Daniel Drake637a8d72008-01-30 13:19:10 +000057
Daniel Drake1df713d2008-06-24 23:01:51 -050058 r = libusb_init(NULL);
Daniel Drake637a8d72008-01-30 13:19:10 +000059 if (r < 0)
60 return r;
61
Daniel Drake1df713d2008-06-24 23:01:51 -050062 cnt = libusb_get_device_list(NULL, &devs);
gudenau18a5f992018-06-19 20:12:52 -050063 if (cnt < 0){
64 libusb_exit(NULL);
Daniel Drake5878daa2008-05-04 00:51:59 +010065 return (int) cnt;
gudenau18a5f992018-06-19 20:12:52 -050066 }
Daniel Drake852bba42007-11-28 13:48:45 +000067
68 print_devs(devs);
Daniel Drake9cfdb492008-03-06 23:25:20 +000069 libusb_free_device_list(devs, 1);
Daniel Drake852bba42007-11-28 13:48:45 +000070
Daniel Drake1df713d2008-06-24 23:01:51 -050071 libusb_exit(NULL);
Daniel Drake852bba42007-11-28 13:48:45 +000072 return 0;
73}