blob: 5b5462eb16657b03d30b293caac2e6facfbb2226 [file] [log] [blame]
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -06001/*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
matt mooney7aaacb42011-05-11 22:33:43 -070020#include <linux/kthread.h>
Bernard Blackham3d0a2a22012-10-22 06:45:00 +110021#include <linux/file.h>
matt mooney7aaacb42011-05-11 22:33:43 -070022#include <linux/net.h>
23
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060024#include "usbip_common.h"
25#include "vhci.h"
26
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060027/* TODO: refine locking ?*/
28
29/* Sysfs entry to show port status */
Greg Kroah-Hartmanb1f56ac2013-08-26 12:02:54 -070030static ssize_t status_show(struct device *dev, struct device_attribute *attr,
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060031 char *out)
32{
33 char *s = out;
34 int i = 0;
Andrew Goodbody21619792016-02-02 17:36:39 +000035 unsigned long flags;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060036
Stoyan Gaydarov2961f242009-03-10 00:10:27 -050037 BUG_ON(!the_controller || !out);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060038
Andrew Goodbody21619792016-02-02 17:36:39 +000039 spin_lock_irqsave(&the_controller->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060040
41 /*
42 * output example:
43 * prt sta spd dev socket local_busid
44 * 000 004 000 000 c5a7bb80 1-2.3
45 * 001 004 000 000 d8cee980 2-3.4
46 *
47 * IP address can be retrieved from a socket pointer address by looking
48 * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
49 * port number and its peer IP address.
50 */
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +010051 out += sprintf(out,
52 "prt sta spd bus dev socket local_busid\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060053
54 for (i = 0; i < VHCI_NPORTS; i++) {
55 struct vhci_device *vdev = port_to_vdev(i);
56
57 spin_lock(&vdev->ud.lock);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060058 out += sprintf(out, "%03u %03u ", i, vdev->ud.status);
59
60 if (vdev->ud.status == VDEV_ST_USED) {
61 out += sprintf(out, "%03u %08x ",
matt mooneybd608f62011-05-06 03:47:52 -070062 vdev->speed, vdev->devid);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060063 out += sprintf(out, "%16p ", vdev->ud.tcp_socket);
Kay Sieverse9133972008-10-30 01:59:32 +010064 out += sprintf(out, "%s", dev_name(&vdev->udev->dev));
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060065
matt mooneybd608f62011-05-06 03:47:52 -070066 } else {
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060067 out += sprintf(out, "000 000 000 0000000000000000 0-0");
matt mooneybd608f62011-05-06 03:47:52 -070068 }
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060069
70 out += sprintf(out, "\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060071 spin_unlock(&vdev->ud.lock);
72 }
73
Andrew Goodbody21619792016-02-02 17:36:39 +000074 spin_unlock_irqrestore(&the_controller->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060075
76 return out - s;
77}
Greg Kroah-Hartmanb1f56ac2013-08-26 12:02:54 -070078static DEVICE_ATTR_RO(status);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060079
80/* Sysfs entry to shutdown a virtual connection */
81static int vhci_port_disconnect(__u32 rhport)
82{
83 struct vhci_device *vdev;
Andrew Goodbody21619792016-02-02 17:36:39 +000084 unsigned long flags;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060085
Brian G. Merrellb8868e42009-07-21 00:46:13 -060086 usbip_dbg_vhci_sysfs("enter\n");
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060087
88 /* lock */
Andrew Goodbody21619792016-02-02 17:36:39 +000089 spin_lock_irqsave(&the_controller->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060090
91 vdev = port_to_vdev(rhport);
92
93 spin_lock(&vdev->ud.lock);
94 if (vdev->ud.status == VDEV_ST_NULL) {
matt mooney1a4b6f62011-05-19 16:47:32 -070095 pr_err("not connected %d\n", vdev->ud.status);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -060096
97 /* unlock */
98 spin_unlock(&vdev->ud.lock);
Andrew Goodbody21619792016-02-02 17:36:39 +000099 spin_unlock_irqrestore(&the_controller->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600100
101 return -EINVAL;
102 }
103
104 /* unlock */
105 spin_unlock(&vdev->ud.lock);
Andrew Goodbody21619792016-02-02 17:36:39 +0000106 spin_unlock_irqrestore(&the_controller->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600107
108 usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
109
110 return 0;
111}
112
113static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
114 const char *buf, size_t count)
115{
116 int err;
117 __u32 rhport = 0;
118
John de la Garza88fa1eb2014-03-06 10:36:34 -0800119 if (sscanf(buf, "%u", &rhport) != 1)
120 return -EINVAL;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600121
122 /* check rhport */
123 if (rhport >= VHCI_NPORTS) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700124 dev_err(dev, "invalid port %u\n", rhport);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600125 return -EINVAL;
126 }
127
128 err = vhci_port_disconnect(rhport);
129 if (err < 0)
130 return -EINVAL;
131
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600132 usbip_dbg_vhci_sysfs("Leave\n");
matt mooneybd608f62011-05-06 03:47:52 -0700133
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600134 return count;
135}
136static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
137
138/* Sysfs entry to establish a virtual connection */
139static int valid_args(__u32 rhport, enum usb_device_speed speed)
140{
141 /* check rhport */
Márton Németh988e7522011-05-26 09:24:45 +0200142 if (rhport >= VHCI_NPORTS) {
matt mooney1a4b6f62011-05-19 16:47:32 -0700143 pr_err("port %u\n", rhport);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600144 return -EINVAL;
145 }
146
147 /* check speed */
148 switch (speed) {
149 case USB_SPEED_LOW:
150 case USB_SPEED_FULL:
151 case USB_SPEED_HIGH:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -0800152 case USB_SPEED_WIRELESS:
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600153 break;
154 default:
Shuah Khan8360fb02014-01-22 09:38:14 -0700155 pr_err("Failed attach request for unsupported USB speed: %s\n",
156 usb_speed_string(speed));
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600157 return -EINVAL;
158 }
159
160 return 0;
161}
162
163/*
164 * To start a new USB/IP attachment, a userland program needs to setup a TCP
165 * connection and then write its socket descriptor with remote device
166 * information into this sysfs file.
167 *
168 * A remote device is virtually attached to the root-hub port of @rhport with
169 * @speed. @devid is embedded into a request to specify the remote device in a
170 * server host.
171 *
172 * write() returns 0 on success, else negative errno.
173 */
174static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
175 const char *buf, size_t count)
176{
177 struct vhci_device *vdev;
178 struct socket *socket;
179 int sockfd = 0;
180 __u32 rhport = 0, devid = 0, speed = 0;
Al Viro964ea962014-03-05 20:33:08 -0500181 int err;
Andrew Goodbody21619792016-02-02 17:36:39 +0000182 unsigned long flags;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600183
184 /*
185 * @rhport: port number of vhci_hcd
186 * @sockfd: socket descriptor of an established TCP connection
187 * @devid: unique device identifier in a remote host
188 * @speed: usb device speed in a remote host
189 */
Shuah Khande4734b2014-03-25 06:47:13 -0600190 if (sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed) != 4)
John de la Garza88fa1eb2014-03-06 10:36:34 -0800191 return -EINVAL;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600192
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600193 usbip_dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n",
matt mooneybd608f62011-05-06 03:47:52 -0700194 rhport, sockfd, devid, speed);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600195
196 /* check received parameters */
197 if (valid_args(rhport, speed) < 0)
198 return -EINVAL;
199
Bernard Blackham3d0a2a22012-10-22 06:45:00 +1100200 /* Extract socket from fd. */
Al Viro964ea962014-03-05 20:33:08 -0500201 socket = sockfd_lookup(sockfd, &err);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600202 if (!socket)
Márton Németha6d81814a2011-05-27 06:18:48 +0200203 return -EINVAL;
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600204
205 /* now need lock until setting vdev status as used */
206
207 /* begin a lock */
Andrew Goodbody21619792016-02-02 17:36:39 +0000208 spin_lock_irqsave(&the_controller->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600209 vdev = port_to_vdev(rhport);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600210 spin_lock(&vdev->ud.lock);
211
212 if (vdev->ud.status != VDEV_ST_NULL) {
213 /* end of the lock */
214 spin_unlock(&vdev->ud.lock);
Andrew Goodbody21619792016-02-02 17:36:39 +0000215 spin_unlock_irqrestore(&the_controller->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600216
Al Viro964ea962014-03-05 20:33:08 -0500217 sockfd_put(socket);
Bernard Blackham3d0a2a22012-10-22 06:45:00 +1100218
matt mooney1a4b6f62011-05-19 16:47:32 -0700219 dev_err(dev, "port %d already used\n", rhport);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600220 return -EINVAL;
221 }
222
Shuah Khana6646ea2014-01-22 14:24:29 -0700223 dev_info(dev,
224 "rhport(%u) sockfd(%d) devid(%u) speed(%u) speed_str(%s)\n",
225 rhport, sockfd, devid, speed, usb_speed_string(speed));
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600226
227 vdev->devid = devid;
228 vdev->speed = speed;
229 vdev->ud.tcp_socket = socket;
230 vdev->ud.status = VDEV_ST_NOTASSIGNED;
231
232 spin_unlock(&vdev->ud.lock);
Andrew Goodbody21619792016-02-02 17:36:39 +0000233 spin_unlock_irqrestore(&the_controller->lock, flags);
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600234 /* end the lock */
235
Oleg Nesterovba46ce32012-03-13 19:07:18 +0100236 vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx");
237 vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx");
Max Vozelerd1b2e952011-04-18 21:44:10 +0200238
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600239 rh_port_connect(rhport, speed);
240
241 return count;
242}
243static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
244
245static struct attribute *dev_attrs[] = {
246 &dev_attr_status.attr,
247 &dev_attr_detach.attr,
248 &dev_attr_attach.attr,
249 &dev_attr_usbip_debug.attr,
250 NULL,
251};
252
Márton Némethcf77acf2011-05-30 21:50:26 +0200253const struct attribute_group dev_attr_group = {
Takahiro Hirofuchi04679b32008-07-09 14:56:51 -0600254 .attrs = dev_attrs,
255};