usbip_attach.c 5.3 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
 *               2005-2007 Takahiro Hirofuchi
4 5 6
 * Copyright (C) 2015-2016 Samsung Electronics
 *               Igor Kotrasinski <i.kotrasinsk@samsung.com>
 *               Krzysztof Opasiak <k.opasiak@samsung.com>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/stat.h>

#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <fcntl.h>
#include <getopt.h>
#include <unistd.h>
32
#include <errno.h>
33 34 35 36 37 38 39 40

#include "vhci_driver.h"
#include "usbip_common.h"
#include "usbip_network.h"
#include "usbip.h"

static const char usbip_attach_usage_string[] =
	"usbip attach <args>\n"
41
	"    -r, --remote=<host>      The machine with exported USB devices\n"
42 43
	"    -b, --busid=<busid>    Busid of the device on <host>\n"
	"    -d, --device=<devid>    Id of the virtual UDC on <host>\n";
44 45 46 47 48 49 50 51 52 53 54 55 56 57

void usbip_attach_usage(void)
{
	printf("usage: %s", usbip_attach_usage_string);
}

#define MAX_BUFF 100
static int record_connection(char *host, char *port, char *busid, int rhport)
{
	int fd;
	char path[PATH_MAX+1];
	char buff[MAX_BUFF+1];
	int ret;

58
	ret = mkdir(VHCI_STATE_PATH, 0700);
59 60 61 62
	if (ret < 0) {
		/* if VHCI_STATE_PATH exists, then it better be a directory */
		if (errno == EEXIST) {
			struct stat s;
63

64 65 66 67 68 69 70 71
			ret = stat(VHCI_STATE_PATH, &s);
			if (ret < 0)
				return -1;
			if (!(s.st_mode & S_IFDIR))
				return -1;
		} else
			return -1;
	}
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

	snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);

	fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
	if (fd < 0)
		return -1;

	snprintf(buff, MAX_BUFF, "%s %s %s\n",
			host, port, busid);

	ret = write(fd, buff, strlen(buff));
	if (ret != (ssize_t) strlen(buff)) {
		close(fd);
		return -1;
	}

	close(fd);

	return 0;
}

93
static int import_device(int sockfd, struct usbip_usb_device *udev)
94 95 96
{
	int rc;
	int port;
97
	uint32_t speed = udev->speed;
98 99 100 101

	rc = usbip_vhci_driver_open();
	if (rc < 0) {
		err("open vhci_driver");
102
		goto err_out;
103 104
	}

105 106 107 108 109 110
	do {
		port = usbip_vhci_get_free_port(speed);
		if (port < 0) {
			err("no free port");
			goto err_driver_close;
		}
111

112
		dbg("got free port %d", port);
Y
Yuyang Du 已提交
113

114 115 116 117 118 119 120
		rc = usbip_vhci_attach_device(port, sockfd, udev->busnum,
					      udev->devnum, udev->speed);
		if (rc < 0 && errno != EBUSY) {
			err("import device");
			goto err_driver_close;
		}
	} while (rc < 0);
121 122 123 124

	usbip_vhci_driver_close();

	return port;
125 126 127 128 129

err_driver_close:
	usbip_vhci_driver_close();
err_out:
	return -1;
130 131 132 133 134 135 136 137
}

static int query_import_device(int sockfd, char *busid)
{
	int rc;
	struct op_import_request request;
	struct op_import_reply   reply;
	uint16_t code = OP_REP_IMPORT;
138
	int status;
139 140 141 142 143

	memset(&request, 0, sizeof(request));
	memset(&reply, 0, sizeof(reply));

	/* send a request */
144
	rc = usbip_net_send_op_common(sockfd, OP_REQ_IMPORT, 0);
145 146 147 148 149 150 151 152 153
	if (rc < 0) {
		err("send op_common");
		return -1;
	}

	strncpy(request.busid, busid, SYSFS_BUS_ID_SIZE-1);

	PACK_OP_IMPORT_REQUEST(0, &request);

154
	rc = usbip_net_send(sockfd, (void *) &request, sizeof(request));
155 156 157 158 159
	if (rc < 0) {
		err("send op_import_request");
		return -1;
	}

160
	/* receive a reply */
161
	rc = usbip_net_recv_op_common(sockfd, &code, &status);
162
	if (rc < 0) {
163 164
		err("Attach Request for %s failed - %s\n",
		    busid, usbip_op_common_status_string(status));
165 166 167
		return -1;
	}

168
	rc = usbip_net_recv(sockfd, (void *) &reply, sizeof(reply));
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	if (rc < 0) {
		err("recv op_import_reply");
		return -1;
	}

	PACK_OP_IMPORT_REPLY(0, &reply);

	/* check the reply */
	if (strncmp(reply.udev.busid, busid, SYSFS_BUS_ID_SIZE)) {
		err("recv different busid %s", reply.udev.busid);
		return -1;
	}

	/* import a device */
	return import_device(sockfd, &reply.udev);
}

static int attach_device(char *host, char *busid)
{
	int sockfd;
	int rc;
	int rhport;

192
	sockfd = usbip_net_tcp_connect(host, usbip_port_string);
193 194 195 196 197 198
	if (sockfd < 0) {
		err("tcp connect");
		return -1;
	}

	rhport = query_import_device(sockfd, busid);
199
	if (rhport < 0)
200 201 202 203
		return -1;

	close(sockfd);

204
	rc = record_connection(host, usbip_port_string, busid, rhport);
205 206 207 208 209 210 211 212 213 214 215
	if (rc < 0) {
		err("record connection");
		return -1;
	}

	return 0;
}

int usbip_attach(int argc, char *argv[])
{
	static const struct option opts[] = {
216 217
		{ "remote", required_argument, NULL, 'r' },
		{ "busid",  required_argument, NULL, 'b' },
218
		{ "device",  required_argument, NULL, 'd' },
219
		{ NULL, 0,  NULL, 0 }
220 221 222 223 224 225 226
	};
	char *host = NULL;
	char *busid = NULL;
	int opt;
	int ret = -1;

	for (;;) {
227
		opt = getopt_long(argc, argv, "d:r:b:", opts, NULL);
228 229 230 231 232

		if (opt == -1)
			break;

		switch (opt) {
233
		case 'r':
234 235
			host = optarg;
			break;
236
		case 'd':
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
		case 'b':
			busid = optarg;
			break;
		default:
			goto err_out;
		}
	}

	if (!host || !busid)
		goto err_out;

	ret = attach_device(host, busid);
	goto out;

err_out:
	usbip_attach_usage();
out:
	return ret;
}