usb: usbip tool: Fix parse_status()
[linux-2.6/btrfs-unstable.git] / tools / usb / usbip / src / usbip_attach.c
blob62a297ff647ad365f34e8e667ca2a235ae9c8d1a
1 /*
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/stat.h>
24 #include <limits.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include <fcntl.h>
30 #include <getopt.h>
31 #include <unistd.h>
32 #include <errno.h>
34 #include "vhci_driver.h"
35 #include "usbip_common.h"
36 #include "usbip_network.h"
37 #include "usbip.h"
39 static const char usbip_attach_usage_string[] =
40 "usbip attach <args>\n"
41 " -r, --remote=<host> The machine with exported USB devices\n"
42 " -b, --busid=<busid> Busid of the device on <host>\n"
43 " -d, --device=<devid> Id of the virtual UDC on <host>\n";
45 void usbip_attach_usage(void)
47 printf("usage: %s", usbip_attach_usage_string);
50 #define MAX_BUFF 100
51 static int record_connection(char *host, char *port, char *busid, int rhport)
53 int fd;
54 char path[PATH_MAX+1];
55 char buff[MAX_BUFF+1];
56 int ret;
58 ret = mkdir(VHCI_STATE_PATH, 0700);
59 if (ret < 0) {
60 /* if VHCI_STATE_PATH exists, then it better be a directory */
61 if (errno == EEXIST) {
62 struct stat s;
64 ret = stat(VHCI_STATE_PATH, &s);
65 if (ret < 0)
66 return -1;
67 if (!(s.st_mode & S_IFDIR))
68 return -1;
69 } else
70 return -1;
73 snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
75 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
76 if (fd < 0)
77 return -1;
79 snprintf(buff, MAX_BUFF, "%s %s %s\n",
80 host, port, busid);
82 ret = write(fd, buff, strlen(buff));
83 if (ret != (ssize_t) strlen(buff)) {
84 close(fd);
85 return -1;
88 close(fd);
90 return 0;
93 static int import_device(int sockfd, struct usbip_usb_device *udev)
95 int rc;
96 int port;
98 rc = usbip_vhci_driver_open();
99 if (rc < 0) {
100 err("open vhci_driver");
101 return -1;
104 port = usbip_vhci_get_free_port();
105 if (port < 0) {
106 err("no free port");
107 usbip_vhci_driver_close();
108 return -1;
111 dbg("got free port %d", port);
113 rc = usbip_vhci_attach_device(port, sockfd, udev->busnum,
114 udev->devnum, udev->speed);
115 if (rc < 0) {
116 err("import device");
117 usbip_vhci_driver_close();
118 return -1;
121 usbip_vhci_driver_close();
123 return port;
126 static int query_import_device(int sockfd, char *busid)
128 int rc;
129 struct op_import_request request;
130 struct op_import_reply reply;
131 uint16_t code = OP_REP_IMPORT;
133 memset(&request, 0, sizeof(request));
134 memset(&reply, 0, sizeof(reply));
136 /* send a request */
137 rc = usbip_net_send_op_common(sockfd, OP_REQ_IMPORT, 0);
138 if (rc < 0) {
139 err("send op_common");
140 return -1;
143 strncpy(request.busid, busid, SYSFS_BUS_ID_SIZE-1);
145 PACK_OP_IMPORT_REQUEST(0, &request);
147 rc = usbip_net_send(sockfd, (void *) &request, sizeof(request));
148 if (rc < 0) {
149 err("send op_import_request");
150 return -1;
153 /* receive a reply */
154 rc = usbip_net_recv_op_common(sockfd, &code);
155 if (rc < 0) {
156 err("recv op_common");
157 return -1;
160 rc = usbip_net_recv(sockfd, (void *) &reply, sizeof(reply));
161 if (rc < 0) {
162 err("recv op_import_reply");
163 return -1;
166 PACK_OP_IMPORT_REPLY(0, &reply);
168 /* check the reply */
169 if (strncmp(reply.udev.busid, busid, SYSFS_BUS_ID_SIZE)) {
170 err("recv different busid %s", reply.udev.busid);
171 return -1;
174 /* import a device */
175 return import_device(sockfd, &reply.udev);
178 static int attach_device(char *host, char *busid)
180 int sockfd;
181 int rc;
182 int rhport;
184 sockfd = usbip_net_tcp_connect(host, usbip_port_string);
185 if (sockfd < 0) {
186 err("tcp connect");
187 return -1;
190 rhport = query_import_device(sockfd, busid);
191 if (rhport < 0) {
192 err("query");
193 return -1;
196 close(sockfd);
198 rc = record_connection(host, usbip_port_string, busid, rhport);
199 if (rc < 0) {
200 err("record connection");
201 return -1;
204 return 0;
207 int usbip_attach(int argc, char *argv[])
209 static const struct option opts[] = {
210 { "remote", required_argument, NULL, 'r' },
211 { "busid", required_argument, NULL, 'b' },
212 { "device", required_argument, NULL, 'd' },
213 { NULL, 0, NULL, 0 }
215 char *host = NULL;
216 char *busid = NULL;
217 int opt;
218 int ret = -1;
220 for (;;) {
221 opt = getopt_long(argc, argv, "d:r:b:", opts, NULL);
223 if (opt == -1)
224 break;
226 switch (opt) {
227 case 'r':
228 host = optarg;
229 break;
230 case 'd':
231 case 'b':
232 busid = optarg;
233 break;
234 default:
235 goto err_out;
239 if (!host || !busid)
240 goto err_out;
242 ret = attach_device(host, busid);
243 goto out;
245 err_out:
246 usbip_attach_usage();
247 out:
248 return ret;