2 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
3 * 2005-2007 Takahiro Hirofuchi
4 * Copyright (C) 2015-2016 Samsung Electronics
5 * Igor Kotrasinski <i.kotrasinsk@samsung.com>
6 * Krzysztof Opasiak <k.opasiak@samsung.com>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
38 #include <linux/usb/ch9.h>
40 #include "usbip_common.h"
41 #include "usbip_network.h"
44 static const char usbip_list_usage_string
[] =
45 "usbip list [-p|--parsable] <args>\n"
46 " -p, --parsable Parsable list format\n"
47 " -r, --remote=<host> List the exportable USB devices on <host>\n"
48 " -l, --local List the local USB devices\n";
50 void usbip_list_usage(void)
52 printf("usage: %s", usbip_list_usage_string
);
55 static int get_exported_devices(char *host
, int sockfd
)
57 char product_name
[100];
59 struct op_devlist_reply reply
;
60 uint16_t code
= OP_REP_DEVLIST
;
61 struct usbip_usb_device udev
;
62 struct usbip_usb_interface uintf
;
66 rc
= usbip_net_send_op_common(sockfd
, OP_REQ_DEVLIST
, 0);
68 dbg("usbip_net_send_op_common failed");
72 rc
= usbip_net_recv_op_common(sockfd
, &code
);
74 dbg("usbip_net_recv_op_common failed");
78 memset(&reply
, 0, sizeof(reply
));
79 rc
= usbip_net_recv(sockfd
, &reply
, sizeof(reply
));
81 dbg("usbip_net_recv_op_devlist failed");
84 PACK_OP_DEVLIST_REPLY(0, &reply
);
85 dbg("exportable devices: %d\n", reply
.ndev
);
87 if (reply
.ndev
== 0) {
88 info("no exportable devices found on %s", host
);
92 printf("Exportable USB devices\n");
93 printf("======================\n");
94 printf(" - %s\n", host
);
96 for (i
= 0; i
< reply
.ndev
; i
++) {
97 memset(&udev
, 0, sizeof(udev
));
98 rc
= usbip_net_recv(sockfd
, &udev
, sizeof(udev
));
100 dbg("usbip_net_recv failed: usbip_usb_device[%d]", i
);
103 usbip_net_pack_usb_device(0, &udev
);
105 usbip_names_get_product(product_name
, sizeof(product_name
),
106 udev
.idVendor
, udev
.idProduct
);
107 usbip_names_get_class(class_name
, sizeof(class_name
),
108 udev
.bDeviceClass
, udev
.bDeviceSubClass
,
109 udev
.bDeviceProtocol
);
110 printf("%11s: %s\n", udev
.busid
, product_name
);
111 printf("%11s: %s\n", "", udev
.path
);
112 printf("%11s: %s\n", "", class_name
);
114 for (j
= 0; j
< udev
.bNumInterfaces
; j
++) {
115 rc
= usbip_net_recv(sockfd
, &uintf
, sizeof(uintf
));
117 err("usbip_net_recv failed: usbip_usb_intf[%d]",
122 usbip_net_pack_usb_interface(0, &uintf
);
124 usbip_names_get_class(class_name
, sizeof(class_name
),
125 uintf
.bInterfaceClass
,
126 uintf
.bInterfaceSubClass
,
127 uintf
.bInterfaceProtocol
);
128 printf("%11s: %2d - %s\n", "", j
, class_name
);
137 static int list_exported_devices(char *host
)
142 sockfd
= usbip_net_tcp_connect(host
, usbip_port_string
);
144 err("could not connect to %s:%s: %s", host
,
145 usbip_port_string
, gai_strerror(sockfd
));
148 dbg("connected to %s:%s", host
, usbip_port_string
);
150 rc
= get_exported_devices(host
, sockfd
);
152 err("failed to get device list from %s", host
);
161 static void print_device(const char *busid
, const char *vendor
,
162 const char *product
, bool parsable
)
165 printf("busid=%s#usbid=%.4s:%.4s#", busid
, vendor
, product
);
167 printf(" - busid %s (%.4s:%.4s)\n", busid
, vendor
, product
);
170 static void print_product_name(char *product_name
, bool parsable
)
173 printf(" %s\n", product_name
);
176 static int list_devices(bool parsable
)
179 struct udev_enumerate
*enumerate
;
180 struct udev_list_entry
*devices
, *dev_list_entry
;
181 struct udev_device
*dev
;
183 const char *idVendor
;
184 const char *idProduct
;
185 const char *bConfValue
;
186 const char *bNumIntfs
;
188 char product_name
[128];
191 /* Create libudev context. */
194 /* Create libudev device enumeration. */
195 enumerate
= udev_enumerate_new(udev
);
197 /* Take only USB devices that are not hubs and do not have
198 * the bInterfaceNumber attribute, i.e. are not interfaces.
200 udev_enumerate_add_match_subsystem(enumerate
, "usb");
201 udev_enumerate_add_nomatch_sysattr(enumerate
, "bDeviceClass", "09");
202 udev_enumerate_add_nomatch_sysattr(enumerate
, "bInterfaceNumber", NULL
);
203 udev_enumerate_scan_devices(enumerate
);
205 devices
= udev_enumerate_get_list_entry(enumerate
);
207 /* Show information about each device. */
208 udev_list_entry_foreach(dev_list_entry
, devices
) {
209 path
= udev_list_entry_get_name(dev_list_entry
);
210 dev
= udev_device_new_from_syspath(udev
, path
);
212 /* Get device information. */
213 idVendor
= udev_device_get_sysattr_value(dev
, "idVendor");
214 idProduct
= udev_device_get_sysattr_value(dev
, "idProduct");
215 bConfValue
= udev_device_get_sysattr_value(dev
,
216 "bConfigurationValue");
217 bNumIntfs
= udev_device_get_sysattr_value(dev
,
219 busid
= udev_device_get_sysname(dev
);
220 if (!idVendor
|| !idProduct
|| !bConfValue
|| !bNumIntfs
) {
221 err("problem getting device attributes: %s",
226 /* Get product name. */
227 usbip_names_get_product(product_name
, sizeof(product_name
),
228 strtol(idVendor
, NULL
, 16),
229 strtol(idProduct
, NULL
, 16));
231 /* Print information. */
232 print_device(busid
, idVendor
, idProduct
, parsable
);
233 print_product_name(product_name
, parsable
);
237 udev_device_unref(dev
);
243 udev_enumerate_unref(enumerate
);
249 static int list_gadget_devices(bool parsable
)
253 struct udev_enumerate
*enumerate
;
254 struct udev_list_entry
*devices
, *dev_list_entry
;
255 struct udev_device
*dev
;
259 const struct usb_device_descriptor
*d_desc
;
260 const char *descriptors
;
261 char product_name
[128];
264 char idVendor_buf
[8];
266 char idProduct_buf
[8];
270 enumerate
= udev_enumerate_new(udev
);
272 udev_enumerate_add_match_subsystem(enumerate
, "platform");
274 udev_enumerate_scan_devices(enumerate
);
275 devices
= udev_enumerate_get_list_entry(enumerate
);
277 udev_list_entry_foreach(dev_list_entry
, devices
) {
278 path
= udev_list_entry_get_name(dev_list_entry
);
279 dev
= udev_device_new_from_syspath(udev
, path
);
281 driver
= udev_device_get_driver(dev
);
282 /* We only have mechanism to enumerate gadgets bound to vudc */
283 if (driver
== NULL
|| strcmp(driver
, USBIP_DEVICE_DRV_NAME
))
286 /* Get device information. */
287 descriptors
= udev_device_get_sysattr_value(dev
,
288 VUDC_DEVICE_DESCR_FILE
);
291 err("problem getting device attributes: %s",
296 d_desc
= (const struct usb_device_descriptor
*) descriptors
;
298 idVendor
= le16toh(d_desc
->idVendor
);
299 sprintf(idVendor_buf
, "0x%4x", idVendor
);
300 idProduct
= le16toh(d_desc
->idProduct
);
301 sprintf(idProduct_buf
, "0x%4x", idVendor
);
302 busid
= udev_device_get_sysname(dev
);
304 /* Get product name. */
305 usbip_names_get_product(product_name
, sizeof(product_name
),
309 /* Print information. */
310 print_device(busid
, idVendor_buf
, idProduct_buf
, parsable
);
311 print_product_name(product_name
, parsable
);
315 udev_device_unref(dev
);
320 udev_enumerate_unref(enumerate
);
326 int usbip_list(int argc
, char *argv
[])
328 static const struct option opts
[] = {
329 { "parsable", no_argument
, NULL
, 'p' },
330 { "remote", required_argument
, NULL
, 'r' },
331 { "local", no_argument
, NULL
, 'l' },
332 { "device", no_argument
, NULL
, 'd' },
336 bool parsable
= false;
340 if (usbip_names_init(USBIDS_FILE
))
341 err("failed to open %s", USBIDS_FILE
);
344 opt
= getopt_long(argc
, argv
, "pr:ld", opts
, NULL
);
354 ret
= list_exported_devices(optarg
);
357 ret
= list_devices(parsable
);
360 ret
= list_gadget_devices(parsable
);