WIP FPC-III support
[linux/fpc-iii.git] / tools / usb / usbip / src / usbip_attach.c
blobb4aeb9f1f4930623cb2fded5ad62b2da34e43b48
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
4 * 2005-2007 Takahiro Hirofuchi
5 * Copyright (C) 2015-2016 Samsung Electronics
6 * Igor Kotrasinski <i.kotrasinsk@samsung.com>
7 * Krzysztof Opasiak <k.opasiak@samsung.com>
8 */
10 #include <sys/stat.h>
12 #include <limits.h>
13 #include <stdint.h>
14 #include <stdio.h>
15 #include <string.h>
17 #include <fcntl.h>
18 #include <getopt.h>
19 #include <unistd.h>
20 #include <errno.h>
22 #include "vhci_driver.h"
23 #include "usbip_common.h"
24 #include "usbip_network.h"
25 #include "usbip.h"
27 static const char usbip_attach_usage_string[] =
28 "usbip attach <args>\n"
29 " -r, --remote=<host> The machine with exported USB devices\n"
30 " -b, --busid=<busid> Busid of the device on <host>\n"
31 " -d, --device=<devid> Id of the virtual UDC on <host>\n";
33 void usbip_attach_usage(void)
35 printf("usage: %s", usbip_attach_usage_string);
38 #define MAX_BUFF 100
39 static int record_connection(char *host, char *port, char *busid, int rhport)
41 int fd;
42 char path[PATH_MAX+1];
43 char buff[MAX_BUFF+1];
44 int ret;
46 ret = mkdir(VHCI_STATE_PATH, 0700);
47 if (ret < 0) {
48 /* if VHCI_STATE_PATH exists, then it better be a directory */
49 if (errno == EEXIST) {
50 struct stat s;
52 ret = stat(VHCI_STATE_PATH, &s);
53 if (ret < 0)
54 return -1;
55 if (!(s.st_mode & S_IFDIR))
56 return -1;
57 } else
58 return -1;
61 snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
63 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
64 if (fd < 0)
65 return -1;
67 snprintf(buff, MAX_BUFF, "%s %s %s\n",
68 host, port, busid);
70 ret = write(fd, buff, strlen(buff));
71 if (ret != (ssize_t) strlen(buff)) {
72 close(fd);
73 return -1;
76 close(fd);
78 return 0;
81 static int import_device(int sockfd, struct usbip_usb_device *udev)
83 int rc;
84 int port;
85 uint32_t speed = udev->speed;
87 rc = usbip_vhci_driver_open();
88 if (rc < 0) {
89 err("open vhci_driver");
90 goto err_out;
93 do {
94 port = usbip_vhci_get_free_port(speed);
95 if (port < 0) {
96 err("no free port");
97 goto err_driver_close;
100 dbg("got free port %d", port);
102 rc = usbip_vhci_attach_device(port, sockfd, udev->busnum,
103 udev->devnum, udev->speed);
104 if (rc < 0 && errno != EBUSY) {
105 err("import device");
106 goto err_driver_close;
108 } while (rc < 0);
110 usbip_vhci_driver_close();
112 return port;
114 err_driver_close:
115 usbip_vhci_driver_close();
116 err_out:
117 return -1;
120 static int query_import_device(int sockfd, char *busid)
122 int rc;
123 struct op_import_request request;
124 struct op_import_reply reply;
125 uint16_t code = OP_REP_IMPORT;
126 int status;
128 memset(&request, 0, sizeof(request));
129 memset(&reply, 0, sizeof(reply));
131 /* send a request */
132 rc = usbip_net_send_op_common(sockfd, OP_REQ_IMPORT, 0);
133 if (rc < 0) {
134 err("send op_common");
135 return -1;
138 strncpy(request.busid, busid, SYSFS_BUS_ID_SIZE-1);
140 PACK_OP_IMPORT_REQUEST(0, &request);
142 rc = usbip_net_send(sockfd, (void *) &request, sizeof(request));
143 if (rc < 0) {
144 err("send op_import_request");
145 return -1;
148 /* receive a reply */
149 rc = usbip_net_recv_op_common(sockfd, &code, &status);
150 if (rc < 0) {
151 err("Attach Request for %s failed - %s\n",
152 busid, usbip_op_common_status_string(status));
153 return -1;
156 rc = usbip_net_recv(sockfd, (void *) &reply, sizeof(reply));
157 if (rc < 0) {
158 err("recv op_import_reply");
159 return -1;
162 PACK_OP_IMPORT_REPLY(0, &reply);
164 /* check the reply */
165 if (strncmp(reply.udev.busid, busid, SYSFS_BUS_ID_SIZE)) {
166 err("recv different busid %s", reply.udev.busid);
167 return -1;
170 /* import a device */
171 return import_device(sockfd, &reply.udev);
174 static int attach_device(char *host, char *busid)
176 int sockfd;
177 int rc;
178 int rhport;
180 sockfd = usbip_net_tcp_connect(host, usbip_port_string);
181 if (sockfd < 0) {
182 err("tcp connect");
183 return -1;
186 rhport = query_import_device(sockfd, busid);
187 if (rhport < 0)
188 return -1;
190 close(sockfd);
192 rc = record_connection(host, usbip_port_string, busid, rhport);
193 if (rc < 0) {
194 err("record connection");
195 return -1;
198 return 0;
201 int usbip_attach(int argc, char *argv[])
203 static const struct option opts[] = {
204 { "remote", required_argument, NULL, 'r' },
205 { "busid", required_argument, NULL, 'b' },
206 { "device", required_argument, NULL, 'd' },
207 { NULL, 0, NULL, 0 }
209 char *host = NULL;
210 char *busid = NULL;
211 int opt;
212 int ret = -1;
214 for (;;) {
215 opt = getopt_long(argc, argv, "d:r:b:", opts, NULL);
217 if (opt == -1)
218 break;
220 switch (opt) {
221 case 'r':
222 host = optarg;
223 break;
224 case 'd':
225 case 'b':
226 busid = optarg;
227 break;
228 default:
229 goto err_out;
233 if (!host || !busid)
234 goto err_out;
236 ret = attach_device(host, busid);
237 goto out;
239 err_out:
240 usbip_attach_usage();
241 out:
242 return ret;