matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 matt mooney <mfm@muteddisk.com> |
| 3 | * 2005-2007 Takahiro Hirofuchi |
Igor Kotrasinski | e0546fd | 2016-03-08 21:49:06 +0100 | [diff] [blame] | 4 | * Copyright (C) 2015-2016 Samsung Electronics |
| 5 | * Igor Kotrasinski <i.kotrasinsk@samsung.com> |
| 6 | * Krzysztof Opasiak <k.opasiak@samsung.com> |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 7 | * |
| 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 Manea | 37765c1 | 2014-03-08 14:53:22 +0200 | [diff] [blame] | 23 | #include <libudev.h> |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 24 | |
| 25 | #include <errno.h> |
| 26 | #include <stdbool.h> |
| 27 | #include <stdint.h> |
| 28 | #include <stdio.h> |
matt mooney | 74ce259 | 2011-05-27 01:44:13 -0700 | [diff] [blame] | 29 | #include <stdlib.h> |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 30 | #include <string.h> |
| 31 | |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 32 | #include <getopt.h> |
| 33 | #include <netdb.h> |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | |
Igor Kotrasinski | e0546fd | 2016-03-08 21:49:06 +0100 | [diff] [blame] | 36 | #include <dirent.h> |
| 37 | |
| 38 | #include <linux/usb/ch9.h> |
| 39 | |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 40 | #include "usbip_common.h" |
| 41 | #include "usbip_network.h" |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 42 | #include "usbip.h" |
| 43 | |
| 44 | static const char usbip_list_usage_string[] = |
| 45 | "usbip list [-p|--parsable] <args>\n" |
| 46 | " -p, --parsable Parsable list format\n" |
matt mooney | 2435ab1 | 2011-07-07 00:31:54 -0700 | [diff] [blame] | 47 | " -r, --remote=<host> List the exportable USB devices on <host>\n" |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 48 | " -l, --local List the local USB devices\n"; |
| 49 | |
| 50 | void usbip_list_usage(void) |
| 51 | { |
| 52 | printf("usage: %s", usbip_list_usage_string); |
| 53 | } |
| 54 | |
matt mooney | b9d65b1 | 2011-06-19 22:44:45 -0700 | [diff] [blame] | 55 | static int get_exported_devices(char *host, int sockfd) |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 56 | { |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 57 | char product_name[100]; |
| 58 | char class_name[100]; |
matt mooney | 622dde8 | 2011-06-19 22:44:44 -0700 | [diff] [blame] | 59 | struct op_devlist_reply reply; |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 60 | uint16_t code = OP_REP_DEVLIST; |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 61 | struct usbip_usb_device udev; |
Valentina Manea | 29a30ad | 2014-03-08 14:53:23 +0200 | [diff] [blame] | 62 | struct usbip_usb_interface uintf; |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 63 | unsigned int i; |
Valentina Manea | 29a30ad | 2014-03-08 14:53:23 +0200 | [diff] [blame] | 64 | int rc, j; |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 65 | |
matt mooney | 3c6e9e8 | 2011-07-07 00:31:51 -0700 | [diff] [blame] | 66 | rc = usbip_net_send_op_common(sockfd, OP_REQ_DEVLIST, 0); |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 67 | if (rc < 0) { |
matt mooney | 3c6e9e8 | 2011-07-07 00:31:51 -0700 | [diff] [blame] | 68 | dbg("usbip_net_send_op_common failed"); |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 69 | return -1; |
| 70 | } |
| 71 | |
matt mooney | 3c6e9e8 | 2011-07-07 00:31:51 -0700 | [diff] [blame] | 72 | rc = usbip_net_recv_op_common(sockfd, &code); |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 73 | if (rc < 0) { |
matt mooney | 3c6e9e8 | 2011-07-07 00:31:51 -0700 | [diff] [blame] | 74 | dbg("usbip_net_recv_op_common failed"); |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 75 | return -1; |
| 76 | } |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 77 | |
matt mooney | 622dde8 | 2011-06-19 22:44:44 -0700 | [diff] [blame] | 78 | memset(&reply, 0, sizeof(reply)); |
matt mooney | 3c6e9e8 | 2011-07-07 00:31:51 -0700 | [diff] [blame] | 79 | rc = usbip_net_recv(sockfd, &reply, sizeof(reply)); |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 80 | if (rc < 0) { |
matt mooney | 3c6e9e8 | 2011-07-07 00:31:51 -0700 | [diff] [blame] | 81 | dbg("usbip_net_recv_op_devlist failed"); |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 82 | return -1; |
| 83 | } |
matt mooney | 622dde8 | 2011-06-19 22:44:44 -0700 | [diff] [blame] | 84 | PACK_OP_DEVLIST_REPLY(0, &reply); |
| 85 | dbg("exportable devices: %d\n", reply.ndev); |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 86 | |
matt mooney | b9d65b1 | 2011-06-19 22:44:45 -0700 | [diff] [blame] | 87 | 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 mooney | 622dde8 | 2011-06-19 22:44:44 -0700 | [diff] [blame] | 96 | for (i = 0; i < reply.ndev; i++) { |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 97 | memset(&udev, 0, sizeof(udev)); |
matt mooney | 3c6e9e8 | 2011-07-07 00:31:51 -0700 | [diff] [blame] | 98 | rc = usbip_net_recv(sockfd, &udev, sizeof(udev)); |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 99 | if (rc < 0) { |
matt mooney | 3c6e9e8 | 2011-07-07 00:31:51 -0700 | [diff] [blame] | 100 | dbg("usbip_net_recv failed: usbip_usb_device[%d]", i); |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 101 | return -1; |
| 102 | } |
matt mooney | 3c6e9e8 | 2011-07-07 00:31:51 -0700 | [diff] [blame] | 103 | usbip_net_pack_usb_device(0, &udev); |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 104 | |
| 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 mooney | fcedd16 | 2011-07-07 00:31:53 -0700 | [diff] [blame] | 110 | printf("%11s: %s\n", udev.busid, product_name); |
| 111 | printf("%11s: %s\n", "", udev.path); |
| 112 | printf("%11s: %s\n", "", class_name); |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 113 | |
Valentina Manea | 29a30ad | 2014-03-08 14:53:23 +0200 | [diff] [blame] | 114 | 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 mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 131 | printf("\n"); |
| 132 | } |
| 133 | |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 134 | return 0; |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 135 | } |
| 136 | |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 137 | static int list_exported_devices(char *host) |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 138 | { |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 139 | int rc; |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 140 | int sockfd; |
| 141 | |
Anthony Foiani | 7182f8f | 2013-08-22 22:06:40 -0600 | [diff] [blame] | 142 | sockfd = usbip_net_tcp_connect(host, usbip_port_string); |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 143 | if (sockfd < 0) { |
matt mooney | 622dde8 | 2011-06-19 22:44:44 -0700 | [diff] [blame] | 144 | err("could not connect to %s:%s: %s", host, |
Anthony Foiani | 7182f8f | 2013-08-22 22:06:40 -0600 | [diff] [blame] | 145 | usbip_port_string, gai_strerror(sockfd)); |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 146 | return -1; |
| 147 | } |
Anthony Foiani | 7182f8f | 2013-08-22 22:06:40 -0600 | [diff] [blame] | 148 | dbg("connected to %s:%s", host, usbip_port_string); |
matt mooney | 622dde8 | 2011-06-19 22:44:44 -0700 | [diff] [blame] | 149 | |
matt mooney | b9d65b1 | 2011-06-19 22:44:45 -0700 | [diff] [blame] | 150 | rc = get_exported_devices(host, sockfd); |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 151 | if (rc < 0) { |
matt mooney | 622dde8 | 2011-06-19 22:44:44 -0700 | [diff] [blame] | 152 | err("failed to get device list from %s", host); |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | close(sockfd); |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 157 | |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
Valentina Manea | 37765c1 | 2014-03-08 14:53:22 +0200 | [diff] [blame] | 161 | static void print_device(const char *busid, const char *vendor, |
| 162 | const char *product, bool parsable) |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 163 | { |
matt mooney | 74ce259 | 2011-05-27 01:44:13 -0700 | [diff] [blame] | 164 | 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 mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Kurt Kanzenbach | 70e90fb | 2013-04-04 16:03:16 +0200 | [diff] [blame] | 170 | static void print_product_name(char *product_name, bool parsable) |
| 171 | { |
| 172 | if (!parsable) |
| 173 | printf(" %s\n", product_name); |
| 174 | } |
| 175 | |
matt mooney | 74ce259 | 2011-05-27 01:44:13 -0700 | [diff] [blame] | 176 | static int list_devices(bool parsable) |
| 177 | { |
Valentina Manea | 37765c1 | 2014-03-08 14:53:22 +0200 | [diff] [blame] | 178 | 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 Kanzenbach | 70e90fb | 2013-04-04 16:03:16 +0200 | [diff] [blame] | 188 | char product_name[128]; |
matt mooney | 74ce259 | 2011-05-27 01:44:13 -0700 | [diff] [blame] | 189 | int ret = -1; |
Shuah Khan | f800795 | 2018-01-17 12:08:03 -0700 | [diff] [blame] | 190 | const char *devpath; |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 191 | |
Valentina Manea | 37765c1 | 2014-03-08 14:53:22 +0200 | [diff] [blame] | 192 | /* Create libudev context. */ |
| 193 | udev = udev_new(); |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 194 | |
Valentina Manea | 37765c1 | 2014-03-08 14:53:22 +0200 | [diff] [blame] | 195 | /* Create libudev device enumeration. */ |
| 196 | enumerate = udev_enumerate_new(udev); |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 197 | |
Valentina Manea | 37765c1 | 2014-03-08 14:53:22 +0200 | [diff] [blame] | 198 | /* Take only USB devices that are not hubs and do not have |
| 199 | * the bInterfaceNumber attribute, i.e. are not interfaces. |
| 200 | */ |
| 201 | udev_enumerate_add_match_subsystem(enumerate, "usb"); |
| 202 | udev_enumerate_add_nomatch_sysattr(enumerate, "bDeviceClass", "09"); |
| 203 | udev_enumerate_add_nomatch_sysattr(enumerate, "bInterfaceNumber", NULL); |
| 204 | udev_enumerate_scan_devices(enumerate); |
matt mooney | 74ce259 | 2011-05-27 01:44:13 -0700 | [diff] [blame] | 205 | |
Valentina Manea | 37765c1 | 2014-03-08 14:53:22 +0200 | [diff] [blame] | 206 | devices = udev_enumerate_get_list_entry(enumerate); |
| 207 | |
| 208 | /* Show information about each device. */ |
| 209 | udev_list_entry_foreach(dev_list_entry, devices) { |
| 210 | path = udev_list_entry_get_name(dev_list_entry); |
| 211 | dev = udev_device_new_from_syspath(udev, path); |
| 212 | |
Shuah Khan | f800795 | 2018-01-17 12:08:03 -0700 | [diff] [blame] | 213 | /* Ignore devices attached to vhci_hcd */ |
| 214 | devpath = udev_device_get_devpath(dev); |
| 215 | if (strstr(devpath, USBIP_VHCI_DRV_NAME)) { |
| 216 | dbg("Skip the device %s already attached to %s\n", |
| 217 | devpath, USBIP_VHCI_DRV_NAME); |
| 218 | continue; |
| 219 | } |
| 220 | |
Valentina Manea | 37765c1 | 2014-03-08 14:53:22 +0200 | [diff] [blame] | 221 | /* Get device information. */ |
| 222 | idVendor = udev_device_get_sysattr_value(dev, "idVendor"); |
| 223 | idProduct = udev_device_get_sysattr_value(dev, "idProduct"); |
Igor Kotrasinski | e0546fd | 2016-03-08 21:49:06 +0100 | [diff] [blame] | 224 | bConfValue = udev_device_get_sysattr_value(dev, |
| 225 | "bConfigurationValue"); |
| 226 | bNumIntfs = udev_device_get_sysattr_value(dev, |
| 227 | "bNumInterfaces"); |
Valentina Manea | 37765c1 | 2014-03-08 14:53:22 +0200 | [diff] [blame] | 228 | busid = udev_device_get_sysname(dev); |
matt mooney | 622dde8 | 2011-06-19 22:44:44 -0700 | [diff] [blame] | 229 | if (!idVendor || !idProduct || !bConfValue || !bNumIntfs) { |
| 230 | err("problem getting device attributes: %s", |
| 231 | strerror(errno)); |
matt mooney | 74ce259 | 2011-05-27 01:44:13 -0700 | [diff] [blame] | 232 | goto err_out; |
matt mooney | 622dde8 | 2011-06-19 22:44:44 -0700 | [diff] [blame] | 233 | } |
matt mooney | 74ce259 | 2011-05-27 01:44:13 -0700 | [diff] [blame] | 234 | |
Valentina Manea | 37765c1 | 2014-03-08 14:53:22 +0200 | [diff] [blame] | 235 | /* Get product name. */ |
Kurt Kanzenbach | 70e90fb | 2013-04-04 16:03:16 +0200 | [diff] [blame] | 236 | usbip_names_get_product(product_name, sizeof(product_name), |
Valentina Manea | 37765c1 | 2014-03-08 14:53:22 +0200 | [diff] [blame] | 237 | strtol(idVendor, NULL, 16), |
| 238 | strtol(idProduct, NULL, 16)); |
| 239 | |
| 240 | /* Print information. */ |
| 241 | print_device(busid, idVendor, idProduct, parsable); |
Kurt Kanzenbach | 70e90fb | 2013-04-04 16:03:16 +0200 | [diff] [blame] | 242 | print_product_name(product_name, parsable); |
matt mooney | 74ce259 | 2011-05-27 01:44:13 -0700 | [diff] [blame] | 243 | |
matt mooney | 74ce259 | 2011-05-27 01:44:13 -0700 | [diff] [blame] | 244 | printf("\n"); |
Valentina Manea | 37765c1 | 2014-03-08 14:53:22 +0200 | [diff] [blame] | 245 | |
| 246 | udev_device_unref(dev); |
matt mooney | 74ce259 | 2011-05-27 01:44:13 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | ret = 0; |
| 250 | |
| 251 | err_out: |
Valentina Manea | 37765c1 | 2014-03-08 14:53:22 +0200 | [diff] [blame] | 252 | udev_enumerate_unref(enumerate); |
| 253 | udev_unref(udev); |
matt mooney | 74ce259 | 2011-05-27 01:44:13 -0700 | [diff] [blame] | 254 | |
| 255 | return ret; |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Igor Kotrasinski | e0546fd | 2016-03-08 21:49:06 +0100 | [diff] [blame] | 258 | static int list_gadget_devices(bool parsable) |
| 259 | { |
| 260 | int ret = -1; |
| 261 | struct udev *udev; |
| 262 | struct udev_enumerate *enumerate; |
| 263 | struct udev_list_entry *devices, *dev_list_entry; |
| 264 | struct udev_device *dev; |
| 265 | const char *path; |
| 266 | const char *driver; |
| 267 | |
| 268 | const struct usb_device_descriptor *d_desc; |
| 269 | const char *descriptors; |
| 270 | char product_name[128]; |
| 271 | |
| 272 | uint16_t idVendor; |
| 273 | char idVendor_buf[8]; |
| 274 | uint16_t idProduct; |
| 275 | char idProduct_buf[8]; |
| 276 | const char *busid; |
| 277 | |
| 278 | udev = udev_new(); |
| 279 | enumerate = udev_enumerate_new(udev); |
| 280 | |
| 281 | udev_enumerate_add_match_subsystem(enumerate, "platform"); |
| 282 | |
| 283 | udev_enumerate_scan_devices(enumerate); |
| 284 | devices = udev_enumerate_get_list_entry(enumerate); |
| 285 | |
| 286 | udev_list_entry_foreach(dev_list_entry, devices) { |
| 287 | path = udev_list_entry_get_name(dev_list_entry); |
| 288 | dev = udev_device_new_from_syspath(udev, path); |
| 289 | |
| 290 | driver = udev_device_get_driver(dev); |
| 291 | /* We only have mechanism to enumerate gadgets bound to vudc */ |
| 292 | if (driver == NULL || strcmp(driver, USBIP_DEVICE_DRV_NAME)) |
| 293 | continue; |
| 294 | |
| 295 | /* Get device information. */ |
| 296 | descriptors = udev_device_get_sysattr_value(dev, |
| 297 | VUDC_DEVICE_DESCR_FILE); |
| 298 | |
| 299 | if (!descriptors) { |
| 300 | err("problem getting device attributes: %s", |
| 301 | strerror(errno)); |
| 302 | goto err_out; |
| 303 | } |
| 304 | |
| 305 | d_desc = (const struct usb_device_descriptor *) descriptors; |
| 306 | |
| 307 | idVendor = le16toh(d_desc->idVendor); |
| 308 | sprintf(idVendor_buf, "0x%4x", idVendor); |
| 309 | idProduct = le16toh(d_desc->idProduct); |
| 310 | sprintf(idProduct_buf, "0x%4x", idVendor); |
| 311 | busid = udev_device_get_sysname(dev); |
| 312 | |
| 313 | /* Get product name. */ |
| 314 | usbip_names_get_product(product_name, sizeof(product_name), |
| 315 | le16toh(idVendor), |
| 316 | le16toh(idProduct)); |
| 317 | |
| 318 | /* Print information. */ |
| 319 | print_device(busid, idVendor_buf, idProduct_buf, parsable); |
| 320 | print_product_name(product_name, parsable); |
| 321 | |
| 322 | printf("\n"); |
| 323 | |
| 324 | udev_device_unref(dev); |
| 325 | } |
| 326 | ret = 0; |
| 327 | |
| 328 | err_out: |
| 329 | udev_enumerate_unref(enumerate); |
| 330 | udev_unref(udev); |
| 331 | |
| 332 | return ret; |
| 333 | } |
| 334 | |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 335 | int usbip_list(int argc, char *argv[]) |
| 336 | { |
| 337 | static const struct option opts[] = { |
matt mooney | 622dde8 | 2011-06-19 22:44:44 -0700 | [diff] [blame] | 338 | { "parsable", no_argument, NULL, 'p' }, |
| 339 | { "remote", required_argument, NULL, 'r' }, |
| 340 | { "local", no_argument, NULL, 'l' }, |
Igor Kotrasinski | e0546fd | 2016-03-08 21:49:06 +0100 | [diff] [blame] | 341 | { "device", no_argument, NULL, 'd' }, |
matt mooney | 622dde8 | 2011-06-19 22:44:44 -0700 | [diff] [blame] | 342 | { NULL, 0, NULL, 0 } |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 343 | }; |
matt mooney | 622dde8 | 2011-06-19 22:44:44 -0700 | [diff] [blame] | 344 | |
matt mooney | 74ce259 | 2011-05-27 01:44:13 -0700 | [diff] [blame] | 345 | bool parsable = false; |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 346 | int opt; |
| 347 | int ret = -1; |
| 348 | |
| 349 | if (usbip_names_init(USBIDS_FILE)) |
matt mooney | 74ce259 | 2011-05-27 01:44:13 -0700 | [diff] [blame] | 350 | err("failed to open %s", USBIDS_FILE); |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 351 | |
| 352 | for (;;) { |
Igor Kotrasinski | e0546fd | 2016-03-08 21:49:06 +0100 | [diff] [blame] | 353 | opt = getopt_long(argc, argv, "pr:ld", opts, NULL); |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 354 | |
| 355 | if (opt == -1) |
| 356 | break; |
| 357 | |
| 358 | switch (opt) { |
| 359 | case 'p': |
matt mooney | 74ce259 | 2011-05-27 01:44:13 -0700 | [diff] [blame] | 360 | parsable = true; |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 361 | break; |
| 362 | case 'r': |
matt mooney | 1109566 | 2011-05-27 01:44:15 -0700 | [diff] [blame] | 363 | ret = list_exported_devices(optarg); |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 364 | goto out; |
| 365 | case 'l': |
matt mooney | 74ce259 | 2011-05-27 01:44:13 -0700 | [diff] [blame] | 366 | ret = list_devices(parsable); |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 367 | goto out; |
Igor Kotrasinski | e0546fd | 2016-03-08 21:49:06 +0100 | [diff] [blame] | 368 | case 'd': |
| 369 | ret = list_gadget_devices(parsable); |
| 370 | goto out; |
matt mooney | e9837bb | 2011-05-26 06:17:11 -0700 | [diff] [blame] | 371 | default: |
| 372 | goto err_out; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | err_out: |
| 377 | usbip_list_usage(); |
| 378 | out: |
| 379 | usbip_names_free(); |
| 380 | |
| 381 | return ret; |
| 382 | } |