Merge branch 'for-linus' of git://git.kernel.dk/linux-block
[pandora-kernel.git] / drivers / staging / usbip / userspace / src / usbip_attach.c
1 /*
2  * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
3  *               2005-2007 Takahiro Hirofuchi
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <sys/stat.h>
20 #include <sysfs/libsysfs.h>
21
22 #include <limits.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include <fcntl.h>
28 #include <getopt.h>
29 #include <unistd.h>
30
31 #include "vhci_driver.h"
32 #include "usbip_common.h"
33 #include "usbip_network.h"
34 #include "usbip.h"
35
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";
40
41 void usbip_attach_usage(void)
42 {
43         printf("usage: %s", usbip_attach_usage_string);
44 }
45
46 #define MAX_BUFF 100
47 static int record_connection(char *host, char *port, char *busid, int rhport)
48 {
49         int fd;
50         char path[PATH_MAX+1];
51         char buff[MAX_BUFF+1];
52         int ret;
53
54         mkdir(VHCI_STATE_PATH, 0700);
55
56         snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
57
58         fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
59         if (fd < 0)
60                 return -1;
61
62         snprintf(buff, MAX_BUFF, "%s %s %s\n",
63                         host, port, busid);
64
65         ret = write(fd, buff, strlen(buff));
66         if (ret != (ssize_t) strlen(buff)) {
67                 close(fd);
68                 return -1;
69         }
70
71         close(fd);
72
73         return 0;
74 }
75
76 static int import_device(int sockfd, struct usbip_usb_device *udev)
77 {
78         int rc;
79         int port;
80
81         rc = usbip_vhci_driver_open();
82         if (rc < 0) {
83                 err("open vhci_driver");
84                 return -1;
85         }
86
87         port = usbip_vhci_get_free_port();
88         if (port < 0) {
89                 err("no free port");
90                 usbip_vhci_driver_close();
91                 return -1;
92         }
93
94         rc = usbip_vhci_attach_device(port, sockfd, udev->busnum,
95                                       udev->devnum, udev->speed);
96         if (rc < 0) {
97                 err("import device");
98                 usbip_vhci_driver_close();
99                 return -1;
100         }
101
102         usbip_vhci_driver_close();
103
104         return port;
105 }
106
107 static int query_import_device(int sockfd, char *busid)
108 {
109         int rc;
110         struct op_import_request request;
111         struct op_import_reply   reply;
112         uint16_t code = OP_REP_IMPORT;
113
114         memset(&request, 0, sizeof(request));
115         memset(&reply, 0, sizeof(reply));
116
117         /* send a request */
118         rc = usbip_net_send_op_common(sockfd, OP_REQ_IMPORT, 0);
119         if (rc < 0) {
120                 err("send op_common");
121                 return -1;
122         }
123
124         strncpy(request.busid, busid, SYSFS_BUS_ID_SIZE-1);
125
126         PACK_OP_IMPORT_REQUEST(0, &request);
127
128         rc = usbip_net_send(sockfd, (void *) &request, sizeof(request));
129         if (rc < 0) {
130                 err("send op_import_request");
131                 return -1;
132         }
133
134         /* recieve a reply */
135         rc = usbip_net_recv_op_common(sockfd, &code);
136         if (rc < 0) {
137                 err("recv op_common");
138                 return -1;
139         }
140
141         rc = usbip_net_recv(sockfd, (void *) &reply, sizeof(reply));
142         if (rc < 0) {
143                 err("recv op_import_reply");
144                 return -1;
145         }
146
147         PACK_OP_IMPORT_REPLY(0, &reply);
148
149         /* check the reply */
150         if (strncmp(reply.udev.busid, busid, SYSFS_BUS_ID_SIZE)) {
151                 err("recv different busid %s", reply.udev.busid);
152                 return -1;
153         }
154
155         /* import a device */
156         return import_device(sockfd, &reply.udev);
157 }
158
159 static int attach_device(char *host, char *busid)
160 {
161         int sockfd;
162         int rc;
163         int rhport;
164
165         sockfd = usbip_net_tcp_connect(host, USBIP_PORT_STRING);
166         if (sockfd < 0) {
167                 err("tcp connect");
168                 return -1;
169         }
170
171         rhport = query_import_device(sockfd, busid);
172         if (rhport < 0) {
173                 err("query");
174                 return -1;
175         }
176
177         close(sockfd);
178
179         rc = record_connection(host, USBIP_PORT_STRING, busid, rhport);
180         if (rc < 0) {
181                 err("record connection");
182                 return -1;
183         }
184
185         return 0;
186 }
187
188 int usbip_attach(int argc, char *argv[])
189 {
190         static const struct option opts[] = {
191                 { "host", required_argument, NULL, 'h' },
192                 { "busid", required_argument, NULL, 'b' },
193                 { NULL, 0, NULL, 0 }
194         };
195         char *host = NULL;
196         char *busid = NULL;
197         int opt;
198         int ret = -1;
199
200         for (;;) {
201                 opt = getopt_long(argc, argv, "h:b:", opts, NULL);
202
203                 if (opt == -1)
204                         break;
205
206                 switch (opt) {
207                 case 'h':
208                         host = optarg;
209                         break;
210                 case 'b':
211                         busid = optarg;
212                         break;
213                 default:
214                         goto err_out;
215                 }
216         }
217
218         if (!host || !busid)
219                 goto err_out;
220
221         ret = attach_device(host, busid);
222         goto out;
223
224 err_out:
225         usbip_attach_usage();
226 out:
227         return ret;
228 }