blob: f1b38e866dd7ea48012def1ca8d695deaaea0efb [file] [log] [blame]
matt mooneye9837bb2011-05-26 06:17:11 -07001/*
2 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
3 * 2005-2007 Takahiro Hirofuchi
Igor Kotrasinskie0546fd2016-03-08 21:49:06 +01004 * Copyright (C) 2015-2016 Samsung Electronics
5 * Igor Kotrasinski <i.kotrasinsk@samsung.com>
6 * Krzysztof Opasiak <k.opasiak@samsung.com>
matt mooneye9837bb2011-05-26 06:17:11 -07007 *
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.
12 *
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.
17 *
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/>.
20 */
21
22#include <sys/types.h>
Valentina Manea37765c12014-03-08 14:53:22 +020023#include <libudev.h>
matt mooneye9837bb2011-05-26 06:17:11 -070024
25#include <errno.h>
26#include <stdbool.h>
27#include <stdint.h>
28#include <stdio.h>
matt mooney74ce2592011-05-27 01:44:13 -070029#include <stdlib.h>
matt mooneye9837bb2011-05-26 06:17:11 -070030#include <string.h>
31
matt mooneye9837bb2011-05-26 06:17:11 -070032#include <getopt.h>
33#include <netdb.h>
matt mooneye9837bb2011-05-26 06:17:11 -070034#include <unistd.h>
35
Igor Kotrasinskie0546fd2016-03-08 21:49:06 +010036#include <dirent.h>
37
38#include <linux/usb/ch9.h>
39
matt mooneye9837bb2011-05-26 06:17:11 -070040#include "usbip_common.h"
41#include "usbip_network.h"
matt mooneye9837bb2011-05-26 06:17:11 -070042#include "usbip.h"
43
44static const char usbip_list_usage_string[] =
45 "usbip list [-p|--parsable] <args>\n"
46 " -p, --parsable Parsable list format\n"
matt mooney2435ab12011-07-07 00:31:54 -070047 " -r, --remote=<host> List the exportable USB devices on <host>\n"
matt mooneye9837bb2011-05-26 06:17:11 -070048 " -l, --local List the local USB devices\n";
49
50void usbip_list_usage(void)
51{
52 printf("usage: %s", usbip_list_usage_string);
53}
54
matt mooneyb9d65b12011-06-19 22:44:45 -070055static int get_exported_devices(char *host, int sockfd)
matt mooneye9837bb2011-05-26 06:17:11 -070056{
matt mooney11095662011-05-27 01:44:15 -070057 char product_name[100];
58 char class_name[100];
matt mooney622dde82011-06-19 22:44:44 -070059 struct op_devlist_reply reply;
matt mooneye9837bb2011-05-26 06:17:11 -070060 uint16_t code = OP_REP_DEVLIST;
matt mooney11095662011-05-27 01:44:15 -070061 struct usbip_usb_device udev;
Valentina Manea29a30ad2014-03-08 14:53:23 +020062 struct usbip_usb_interface uintf;
matt mooney11095662011-05-27 01:44:15 -070063 unsigned int i;
Valentina Manea29a30ad2014-03-08 14:53:23 +020064 int rc, j;
matt mooney11095662011-05-27 01:44:15 -070065
matt mooney3c6e9e82011-07-07 00:31:51 -070066 rc = usbip_net_send_op_common(sockfd, OP_REQ_DEVLIST, 0);
matt mooney11095662011-05-27 01:44:15 -070067 if (rc < 0) {
matt mooney3c6e9e82011-07-07 00:31:51 -070068 dbg("usbip_net_send_op_common failed");
matt mooney11095662011-05-27 01:44:15 -070069 return -1;
70 }
71
matt mooney3c6e9e82011-07-07 00:31:51 -070072 rc = usbip_net_recv_op_common(sockfd, &code);
matt mooney11095662011-05-27 01:44:15 -070073 if (rc < 0) {
matt mooney3c6e9e82011-07-07 00:31:51 -070074 dbg("usbip_net_recv_op_common failed");
matt mooney11095662011-05-27 01:44:15 -070075 return -1;
76 }
matt mooneye9837bb2011-05-26 06:17:11 -070077
matt mooney622dde82011-06-19 22:44:44 -070078 memset(&reply, 0, sizeof(reply));
matt mooney3c6e9e82011-07-07 00:31:51 -070079 rc = usbip_net_recv(sockfd, &reply, sizeof(reply));
matt mooney11095662011-05-27 01:44:15 -070080 if (rc < 0) {
matt mooney3c6e9e82011-07-07 00:31:51 -070081 dbg("usbip_net_recv_op_devlist failed");
matt mooneye9837bb2011-05-26 06:17:11 -070082 return -1;
83 }
matt mooney622dde82011-06-19 22:44:44 -070084 PACK_OP_DEVLIST_REPLY(0, &reply);
85 dbg("exportable devices: %d\n", reply.ndev);
matt mooneye9837bb2011-05-26 06:17:11 -070086
matt mooneyb9d65b12011-06-19 22:44:45 -070087 if (reply.ndev == 0) {
88 info("no exportable devices found on %s", host);
89 return 0;
90 }
91
92 printf("Exportable USB devices\n");
93 printf("======================\n");
94 printf(" - %s\n", host);
95
matt mooney622dde82011-06-19 22:44:44 -070096 for (i = 0; i < reply.ndev; i++) {
matt mooneye9837bb2011-05-26 06:17:11 -070097 memset(&udev, 0, sizeof(udev));
matt mooney3c6e9e82011-07-07 00:31:51 -070098 rc = usbip_net_recv(sockfd, &udev, sizeof(udev));
matt mooney11095662011-05-27 01:44:15 -070099 if (rc < 0) {
matt mooney3c6e9e82011-07-07 00:31:51 -0700100 dbg("usbip_net_recv failed: usbip_usb_device[%d]", i);
matt mooneye9837bb2011-05-26 06:17:11 -0700101 return -1;
102 }
matt mooney3c6e9e82011-07-07 00:31:51 -0700103 usbip_net_pack_usb_device(0, &udev);
matt mooneye9837bb2011-05-26 06:17:11 -0700104
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);
matt mooneyfcedd162011-07-07 00:31:53 -0700110 printf("%11s: %s\n", udev.busid, product_name);
111 printf("%11s: %s\n", "", udev.path);
112 printf("%11s: %s\n", "", class_name);
matt mooneye9837bb2011-05-26 06:17:11 -0700113
Valentina Manea29a30ad2014-03-08 14:53:23 +0200114 for (j = 0; j < udev.bNumInterfaces; j++) {
115 rc = usbip_net_recv(sockfd, &uintf, sizeof(uintf));
116 if (rc < 0) {
117 err("usbip_net_recv failed: usbip_usb_intf[%d]",
118 j);
119
120 return -1;
121 }
122 usbip_net_pack_usb_interface(0, &uintf);
123
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);
129 }
130
matt mooneye9837bb2011-05-26 06:17:11 -0700131 printf("\n");
132 }
133
matt mooney11095662011-05-27 01:44:15 -0700134 return 0;
matt mooneye9837bb2011-05-26 06:17:11 -0700135}
136
matt mooney11095662011-05-27 01:44:15 -0700137static int list_exported_devices(char *host)
matt mooneye9837bb2011-05-26 06:17:11 -0700138{
matt mooney11095662011-05-27 01:44:15 -0700139 int rc;
matt mooneye9837bb2011-05-26 06:17:11 -0700140 int sockfd;
141
Anthony Foiani7182f8f2013-08-22 22:06:40 -0600142 sockfd = usbip_net_tcp_connect(host, usbip_port_string);
matt mooneye9837bb2011-05-26 06:17:11 -0700143 if (sockfd < 0) {
matt mooney622dde82011-06-19 22:44:44 -0700144 err("could not connect to %s:%s: %s", host,
Anthony Foiani7182f8f2013-08-22 22:06:40 -0600145 usbip_port_string, gai_strerror(sockfd));
matt mooneye9837bb2011-05-26 06:17:11 -0700146 return -1;
147 }
Anthony Foiani7182f8f2013-08-22 22:06:40 -0600148 dbg("connected to %s:%s", host, usbip_port_string);
matt mooney622dde82011-06-19 22:44:44 -0700149
matt mooneyb9d65b12011-06-19 22:44:45 -0700150 rc = get_exported_devices(host, sockfd);
matt mooney11095662011-05-27 01:44:15 -0700151 if (rc < 0) {
matt mooney622dde82011-06-19 22:44:44 -0700152 err("failed to get device list from %s", host);
matt mooneye9837bb2011-05-26 06:17:11 -0700153 return -1;
154 }
155
156 close(sockfd);
matt mooney11095662011-05-27 01:44:15 -0700157
matt mooneye9837bb2011-05-26 06:17:11 -0700158 return 0;
159}
160
Valentina Manea37765c12014-03-08 14:53:22 +0200161static void print_device(const char *busid, const char *vendor,
162 const char *product, bool parsable)
matt mooneye9837bb2011-05-26 06:17:11 -0700163{
matt mooney74ce2592011-05-27 01:44:13 -0700164 if (parsable)
165 printf("busid=%s#usbid=%.4s:%.4s#", busid, vendor, product);
166 else
167 printf(" - busid %s (%.4s:%.4s)\n", busid, vendor, product);
matt mooneye9837bb2011-05-26 06:17:11 -0700168}
169
Kurt Kanzenbach70e90fb2013-04-04 16:03:16 +0200170static void print_product_name(char *product_name, bool parsable)
171{
172 if (!parsable)
173 printf(" %s\n", product_name);
174}
175
matt mooney74ce2592011-05-27 01:44:13 -0700176static int list_devices(bool parsable)
177{
Valentina Manea37765c12014-03-08 14:53:22 +0200178 struct udev *udev;
179 struct udev_enumerate *enumerate;
180 struct udev_list_entry *devices, *dev_list_entry;
181 struct udev_device *dev;
182 const char *path;
183 const char *idVendor;
184 const char *idProduct;
185 const char *bConfValue;
186 const char *bNumIntfs;
187 const char *busid;
Kurt Kanzenbach70e90fb2013-04-04 16:03:16 +0200188 char product_name[128];
matt mooney74ce2592011-05-27 01:44:13 -0700189 int ret = -1;
matt mooneye9837bb2011-05-26 06:17:11 -0700190
Valentina Manea37765c12014-03-08 14:53:22 +0200191 /* Create libudev context. */
192 udev = udev_new();
matt mooneye9837bb2011-05-26 06:17:11 -0700193
Valentina Manea37765c12014-03-08 14:53:22 +0200194 /* Create libudev device enumeration. */
195 enumerate = udev_enumerate_new(udev);
matt mooneye9837bb2011-05-26 06:17:11 -0700196
Valentina Manea37765c12014-03-08 14:53:22 +0200197 /* Take only USB devices that are not hubs and do not have
198 * the bInterfaceNumber attribute, i.e. are not interfaces.
199 */
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);
matt mooney74ce2592011-05-27 01:44:13 -0700204
Valentina Manea37765c12014-03-08 14:53:22 +0200205 devices = udev_enumerate_get_list_entry(enumerate);
206
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);
211
212 /* Get device information. */
213 idVendor = udev_device_get_sysattr_value(dev, "idVendor");
214 idProduct = udev_device_get_sysattr_value(dev, "idProduct");
Igor Kotrasinskie0546fd2016-03-08 21:49:06 +0100215 bConfValue = udev_device_get_sysattr_value(dev,
216 "bConfigurationValue");
217 bNumIntfs = udev_device_get_sysattr_value(dev,
218 "bNumInterfaces");
Valentina Manea37765c12014-03-08 14:53:22 +0200219 busid = udev_device_get_sysname(dev);
matt mooney622dde82011-06-19 22:44:44 -0700220 if (!idVendor || !idProduct || !bConfValue || !bNumIntfs) {
221 err("problem getting device attributes: %s",
222 strerror(errno));
matt mooney74ce2592011-05-27 01:44:13 -0700223 goto err_out;
matt mooney622dde82011-06-19 22:44:44 -0700224 }
matt mooney74ce2592011-05-27 01:44:13 -0700225
Valentina Manea37765c12014-03-08 14:53:22 +0200226 /* Get product name. */
Kurt Kanzenbach70e90fb2013-04-04 16:03:16 +0200227 usbip_names_get_product(product_name, sizeof(product_name),
Valentina Manea37765c12014-03-08 14:53:22 +0200228 strtol(idVendor, NULL, 16),
229 strtol(idProduct, NULL, 16));
230
231 /* Print information. */
232 print_device(busid, idVendor, idProduct, parsable);
Kurt Kanzenbach70e90fb2013-04-04 16:03:16 +0200233 print_product_name(product_name, parsable);
matt mooney74ce2592011-05-27 01:44:13 -0700234
matt mooney74ce2592011-05-27 01:44:13 -0700235 printf("\n");
Valentina Manea37765c12014-03-08 14:53:22 +0200236
237 udev_device_unref(dev);
matt mooney74ce2592011-05-27 01:44:13 -0700238 }
239
240 ret = 0;
241
242err_out:
Valentina Manea37765c12014-03-08 14:53:22 +0200243 udev_enumerate_unref(enumerate);
244 udev_unref(udev);
matt mooney74ce2592011-05-27 01:44:13 -0700245
246 return ret;
matt mooneye9837bb2011-05-26 06:17:11 -0700247}
248
Igor Kotrasinskie0546fd2016-03-08 21:49:06 +0100249static int list_gadget_devices(bool parsable)
250{
251 int ret = -1;
252 struct udev *udev;
253 struct udev_enumerate *enumerate;
254 struct udev_list_entry *devices, *dev_list_entry;
255 struct udev_device *dev;
256 const char *path;
257 const char *driver;
258
259 const struct usb_device_descriptor *d_desc;
260 const char *descriptors;
261 char product_name[128];
262
263 uint16_t idVendor;
264 char idVendor_buf[8];
265 uint16_t idProduct;
266 char idProduct_buf[8];
267 const char *busid;
268
269 udev = udev_new();
270 enumerate = udev_enumerate_new(udev);
271
272 udev_enumerate_add_match_subsystem(enumerate, "platform");
273
274 udev_enumerate_scan_devices(enumerate);
275 devices = udev_enumerate_get_list_entry(enumerate);
276
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);
280
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))
284 continue;
285
286 /* Get device information. */
287 descriptors = udev_device_get_sysattr_value(dev,
288 VUDC_DEVICE_DESCR_FILE);
289
290 if (!descriptors) {
291 err("problem getting device attributes: %s",
292 strerror(errno));
293 goto err_out;
294 }
295
296 d_desc = (const struct usb_device_descriptor *) descriptors;
297
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);
303
304 /* Get product name. */
305 usbip_names_get_product(product_name, sizeof(product_name),
306 le16toh(idVendor),
307 le16toh(idProduct));
308
309 /* Print information. */
310 print_device(busid, idVendor_buf, idProduct_buf, parsable);
311 print_product_name(product_name, parsable);
312
313 printf("\n");
314
315 udev_device_unref(dev);
316 }
317 ret = 0;
318
319err_out:
320 udev_enumerate_unref(enumerate);
321 udev_unref(udev);
322
323 return ret;
324}
325
matt mooneye9837bb2011-05-26 06:17:11 -0700326int usbip_list(int argc, char *argv[])
327{
328 static const struct option opts[] = {
matt mooney622dde82011-06-19 22:44:44 -0700329 { "parsable", no_argument, NULL, 'p' },
330 { "remote", required_argument, NULL, 'r' },
331 { "local", no_argument, NULL, 'l' },
Igor Kotrasinskie0546fd2016-03-08 21:49:06 +0100332 { "device", no_argument, NULL, 'd' },
matt mooney622dde82011-06-19 22:44:44 -0700333 { NULL, 0, NULL, 0 }
matt mooneye9837bb2011-05-26 06:17:11 -0700334 };
matt mooney622dde82011-06-19 22:44:44 -0700335
matt mooney74ce2592011-05-27 01:44:13 -0700336 bool parsable = false;
matt mooneye9837bb2011-05-26 06:17:11 -0700337 int opt;
338 int ret = -1;
339
340 if (usbip_names_init(USBIDS_FILE))
matt mooney74ce2592011-05-27 01:44:13 -0700341 err("failed to open %s", USBIDS_FILE);
matt mooneye9837bb2011-05-26 06:17:11 -0700342
343 for (;;) {
Igor Kotrasinskie0546fd2016-03-08 21:49:06 +0100344 opt = getopt_long(argc, argv, "pr:ld", opts, NULL);
matt mooneye9837bb2011-05-26 06:17:11 -0700345
346 if (opt == -1)
347 break;
348
349 switch (opt) {
350 case 'p':
matt mooney74ce2592011-05-27 01:44:13 -0700351 parsable = true;
matt mooneye9837bb2011-05-26 06:17:11 -0700352 break;
353 case 'r':
matt mooney11095662011-05-27 01:44:15 -0700354 ret = list_exported_devices(optarg);
matt mooneye9837bb2011-05-26 06:17:11 -0700355 goto out;
356 case 'l':
matt mooney74ce2592011-05-27 01:44:13 -0700357 ret = list_devices(parsable);
matt mooneye9837bb2011-05-26 06:17:11 -0700358 goto out;
Igor Kotrasinskie0546fd2016-03-08 21:49:06 +0100359 case 'd':
360 ret = list_gadget_devices(parsable);
361 goto out;
matt mooneye9837bb2011-05-26 06:17:11 -0700362 default:
363 goto err_out;
364 }
365 }
366
367err_out:
368 usbip_list_usage();
369out:
370 usbip_names_free();
371
372 return ret;
373}