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/>.
34 #include "vhci_driver.h"
35 #include "usbip_common.h"
36 #include "usbip_network.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);
51 static int record_connection(char *host, char *port, char *busid, int rhport)
54 char path[PATH_MAX+1];
55 char buff[MAX_BUFF+1];
58 ret = mkdir(VHCI_STATE_PATH, 0700);
60 /* if VHCI_STATE_PATH exists, then it better be a directory */
61 if (errno == EEXIST) {
64 ret = stat(VHCI_STATE_PATH, &s);
67 if (!(s.st_mode & S_IFDIR))
73 snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
75 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
79 snprintf(buff, MAX_BUFF, "%s %s %s\n",
82 ret = write(fd, buff, strlen(buff));
83 if (ret != (ssize_t) strlen(buff)) {
93 static int import_device(int sockfd, struct usbip_usb_device *udev)
98 rc = usbip_vhci_driver_open();
100 err("open vhci_driver");
104 port = usbip_vhci_get_free_port();
107 usbip_vhci_driver_close();
111 rc = usbip_vhci_attach_device(port, sockfd, udev->busnum,
112 udev->devnum, udev->speed);
114 err("import device");
115 usbip_vhci_driver_close();
119 usbip_vhci_driver_close();
124 static int query_import_device(int sockfd, char *busid)
127 struct op_import_request request;
128 struct op_import_reply reply;
129 uint16_t code = OP_REP_IMPORT;
131 memset(&request, 0, sizeof(request));
132 memset(&reply, 0, sizeof(reply));
135 rc = usbip_net_send_op_common(sockfd, OP_REQ_IMPORT, 0);
137 err("send op_common");
141 strncpy(request.busid, busid, SYSFS_BUS_ID_SIZE-1);
143 PACK_OP_IMPORT_REQUEST(0, &request);
145 rc = usbip_net_send(sockfd, (void *) &request, sizeof(request));
147 err("send op_import_request");
151 /* receive a reply */
152 rc = usbip_net_recv_op_common(sockfd, &code);
154 err("recv op_common");
158 rc = usbip_net_recv(sockfd, (void *) &reply, sizeof(reply));
160 err("recv op_import_reply");
164 PACK_OP_IMPORT_REPLY(0, &reply);
166 /* check the reply */
167 if (strncmp(reply.udev.busid, busid, SYSFS_BUS_ID_SIZE)) {
168 err("recv different busid %s", reply.udev.busid);
172 /* import a device */
173 return import_device(sockfd, &reply.udev);
176 static int attach_device(char *host, char *busid)
182 sockfd = usbip_net_tcp_connect(host, usbip_port_string);
188 rhport = query_import_device(sockfd, busid);
196 rc = record_connection(host, usbip_port_string, busid, rhport);
198 err("record connection");
205 int usbip_attach(int argc, char *argv[])
207 static const struct option opts[] = {
208 { "remote", required_argument, NULL, 'r' },
209 { "busid", required_argument, NULL, 'b' },
210 { "device", required_argument, NULL, 'd' },
219 opt = getopt_long(argc, argv, "d:r:b:", opts, NULL);
240 ret = attach_device(host, busid);
244 usbip_attach_usage();