19 #include <sys/types.h>
20 #include <sysfs/libsysfs.h>
33 #include "usbip_common.h"
37 static const char usbip_list_usage_string[] =
38 "usbip list [-p|--parsable] <args>\n"
39 " -p, --parsable Parsable list format\n"
40 " -r, --remote=<host> List the exportable USB devices on <host>\n"
41 " -l, --local List the local USB devices\n";
45 printf(
"usage: %s", usbip_list_usage_string);
48 static int get_exported_devices(
char *
host,
int sockfd)
50 char product_name[100];
61 dbg(
"usbip_net_send_op_common failed");
67 dbg(
"usbip_net_recv_op_common failed");
71 memset(&reply, 0,
sizeof(reply));
74 dbg(
"usbip_net_recv_op_devlist failed");
78 dbg(
"exportable devices: %d\n", reply.ndev);
80 if (reply.ndev == 0) {
81 info(
"no exportable devices found on %s", host);
85 printf(
"Exportable USB devices\n");
86 printf(
"======================\n");
89 for (i = 0; i < reply.ndev; i++) {
93 dbg(
"usbip_net_recv failed: usbip_usb_device[%d]", i);
101 udev.bDeviceClass,
udev.bDeviceSubClass,
102 udev.bDeviceProtocol);
103 printf(
"%11s: %s\n",
udev.busid, product_name);
105 printf(
"%11s: %s\n",
"", class_name);
107 for (j = 0; j <
udev.bNumInterfaces; j++) {
110 dbg(
"usbip_net_recv failed: usbip_usb_intf[%d]",
118 uintf.bInterfaceClass,
119 uintf.bInterfaceSubClass,
120 uintf.bInterfaceProtocol);
121 printf(
"%11s: %2d - %s\n",
"", j, class_name);
129 static int list_exported_devices(
char *host)
136 err(
"could not connect to %s:%s: %s", host,
142 rc = get_exported_devices(host, sockfd);
144 err(
"failed to get device list from %s", host);
157 printf(
"busid=%s#usbid=%.4s:%.4s#", busid, vendor, product);
159 printf(
" - busid %s (%.4s:%.4s)\n", busid, vendor, product);
162 static void print_interface(
char *busid,
char *
driver,
bool parsable)
165 printf(
"%s=%s#", busid, driver);
167 printf(
"%9s%s -> %s\n",
"", busid, driver);
170 static int is_device(
void *
x)
172 struct sysfs_attribute *devpath;
173 struct sysfs_device *
dev =
x;
176 devpath = sysfs_get_device_attr(dev,
"devpath");
177 if (devpath && *devpath->value !=
'0')
183 static int devcmp(
void *
a,
void *
b)
188 static int list_devices(
bool parsable)
191 char busid[SYSFS_BUS_ID_SIZE];
192 struct sysfs_bus *ubus;
193 struct sysfs_device *
dev;
194 struct sysfs_device *
intf;
197 struct sysfs_attribute *bConfValue;
198 struct sysfs_attribute *bNumIntfs;
203 ubus = sysfs_open_bus(bus_type);
205 err(
"could not open %s bus: %s", bus_type,
strerror(errno));
209 devlist = sysfs_get_bus_devices(ubus);
211 err(
"could not get %s bus devices: %s", bus_type,
217 dlist_filter_sort(devlist, is_device, devcmp);
220 printf(
"Local USB devices\n");
221 printf(
"=================\n");
223 dlist_for_each_data(devlist, dev,
struct sysfs_device) {
224 idVendor = sysfs_get_device_attr(dev,
"idVendor");
225 idProduct = sysfs_get_device_attr(dev,
"idProduct");
226 bConfValue = sysfs_get_device_attr(dev,
"bConfigurationValue");
227 bNumIntfs = sysfs_get_device_attr(dev,
"bNumInterfaces");
228 if (!idVendor || !idProduct || !bConfValue || !bNumIntfs) {
229 err(
"problem getting device attributes: %s",
234 print_device(dev->bus_id, idVendor->value, idProduct->value,
237 for (i = 0; i < atoi(bNumIntfs->value); i++) {
238 snprintf(busid,
sizeof(busid),
"%s:%.1s.%d",
239 dev->bus_id, bConfValue->value, i);
240 intf = sysfs_open_device(bus_type, busid);
242 err(
"could not open device interface: %s",
246 print_interface(busid, intf->driver_name, parsable);
247 sysfs_close_device(intf);
255 sysfs_close_bus(ubus);
262 static const struct option opts[] = {
263 {
"parsable", no_argument,
NULL,
'p' },
264 {
"remote", required_argument,
NULL,
'r' },
265 {
"local", no_argument,
NULL,
'l' },
269 bool parsable =
false;
277 opt = getopt_long(argc, argv,
"pr:l", opts,
NULL);
287 ret = list_exported_devices(optarg);
290 ret = list_devices(parsable);