2 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
3 * 2005-2007 Takahiro Hirofuchi
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <sysfs/libsysfs.h>
31 #include "vhci_driver.h"
32 #include "usbip_common.h"
33 #include "usbip_network.h"
36 static const char usbip_attach_usage_string[] =
37 "usbip attach <args>\n"
38 " -h, --host=<host> The machine with exported USB devices\n"
39 " -b, --busid=<busid> Busid of the device on <host>\n";
41 void usbip_attach_usage(void)
43 printf("usage: %s", usbip_attach_usage_string);
47 static int record_connection(char *host, char *port, char *busid, int rhport)
50 char path[PATH_MAX+1];
51 char buff[MAX_BUFF+1];
54 ret = mkdir(VHCI_STATE_PATH, 0700);
58 snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
60 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
64 snprintf(buff, MAX_BUFF, "%s %s %s\n",
67 ret = write(fd, buff, strlen(buff));
68 if (ret != (ssize_t) strlen(buff)) {
78 static int import_device(int sockfd, struct usbip_usb_device *udev)
83 rc = usbip_vhci_driver_open();
85 err("open vhci_driver");
89 port = usbip_vhci_get_free_port();
92 usbip_vhci_driver_close();
96 rc = usbip_vhci_attach_device(port, sockfd, udev->busnum,
97 udev->devnum, udev->speed);
100 usbip_vhci_driver_close();
104 usbip_vhci_driver_close();
109 static int query_import_device(int sockfd, char *busid)
112 struct op_import_request request;
113 struct op_import_reply reply;
114 uint16_t code = OP_REP_IMPORT;
116 memset(&request, 0, sizeof(request));
117 memset(&reply, 0, sizeof(reply));
120 rc = usbip_net_send_op_common(sockfd, OP_REQ_IMPORT, 0);
122 err("send op_common");
126 strncpy(request.busid, busid, SYSFS_BUS_ID_SIZE-1);
128 PACK_OP_IMPORT_REQUEST(0, &request);
130 rc = usbip_net_send(sockfd, (void *) &request, sizeof(request));
132 err("send op_import_request");
136 /* recieve a reply */
137 rc = usbip_net_recv_op_common(sockfd, &code);
139 err("recv op_common");
143 rc = usbip_net_recv(sockfd, (void *) &reply, sizeof(reply));
145 err("recv op_import_reply");
149 PACK_OP_IMPORT_REPLY(0, &reply);
151 /* check the reply */
152 if (strncmp(reply.udev.busid, busid, SYSFS_BUS_ID_SIZE)) {
153 err("recv different busid %s", reply.udev.busid);
157 /* import a device */
158 return import_device(sockfd, &reply.udev);
161 static int attach_device(char *host, char *busid)
167 sockfd = usbip_net_tcp_connect(host, USBIP_PORT_STRING);
173 rhport = query_import_device(sockfd, busid);
181 rc = record_connection(host, USBIP_PORT_STRING, busid, rhport);
183 err("record connection");
190 int usbip_attach(int argc, char *argv[])
192 static const struct option opts[] = {
193 { "host", required_argument, NULL, 'h' },
194 { "busid", required_argument, NULL, 'b' },
203 opt = getopt_long(argc, argv, "h:b:", opts, NULL);
223 ret = attach_device(host, busid);
227 usbip_attach_usage();