2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
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.
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.
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,
20 #include <linux/kthread.h>
21 #include <linux/net.h>
23 #include "usbip_common.h"
26 /* TODO: refine locking ?*/
28 /* Sysfs entry to show port status */
29 static ssize_t
show_status(struct device
*dev
, struct device_attribute
*attr
,
35 BUG_ON(!the_controller
|| !out
);
37 spin_lock(&the_controller
->lock
);
41 * prt sta spd dev socket local_busid
42 * 000 004 000 000 c5a7bb80 1-2.3
43 * 001 004 000 000 d8cee980 2-3.4
45 * IP address can be retrieved from a socket pointer address by looking
46 * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
47 * port number and its peer IP address.
49 out
+= sprintf(out
, "prt sta spd bus dev socket "
52 for (i
= 0; i
< VHCI_NPORTS
; i
++) {
53 struct vhci_device
*vdev
= port_to_vdev(i
);
55 spin_lock(&vdev
->ud
.lock
);
56 out
+= sprintf(out
, "%03u %03u ", i
, vdev
->ud
.status
);
58 if (vdev
->ud
.status
== VDEV_ST_USED
) {
59 out
+= sprintf(out
, "%03u %08x ",
60 vdev
->speed
, vdev
->devid
);
61 out
+= sprintf(out
, "%16p ", vdev
->ud
.tcp_socket
);
62 out
+= sprintf(out
, "%s", dev_name(&vdev
->udev
->dev
));
65 out
+= sprintf(out
, "000 000 000 0000000000000000 0-0");
68 out
+= sprintf(out
, "\n");
69 spin_unlock(&vdev
->ud
.lock
);
72 spin_unlock(&the_controller
->lock
);
76 static DEVICE_ATTR(status
, S_IRUGO
, show_status
, NULL
);
78 /* Sysfs entry to shutdown a virtual connection */
79 static int vhci_port_disconnect(__u32 rhport
)
81 struct vhci_device
*vdev
;
83 usbip_dbg_vhci_sysfs("enter\n");
86 spin_lock(&the_controller
->lock
);
88 vdev
= port_to_vdev(rhport
);
90 spin_lock(&vdev
->ud
.lock
);
91 if (vdev
->ud
.status
== VDEV_ST_NULL
) {
92 pr_err("not connected %d\n", vdev
->ud
.status
);
95 spin_unlock(&vdev
->ud
.lock
);
96 spin_unlock(&the_controller
->lock
);
102 spin_unlock(&vdev
->ud
.lock
);
103 spin_unlock(&the_controller
->lock
);
105 usbip_event_add(&vdev
->ud
, VDEV_EVENT_DOWN
);
110 static ssize_t
store_detach(struct device
*dev
, struct device_attribute
*attr
,
111 const char *buf
, size_t count
)
116 sscanf(buf
, "%u", &rhport
);
119 if (rhport
>= VHCI_NPORTS
) {
120 dev_err(dev
, "invalid port %u\n", rhport
);
124 err
= vhci_port_disconnect(rhport
);
128 usbip_dbg_vhci_sysfs("Leave\n");
132 static DEVICE_ATTR(detach
, S_IWUSR
, NULL
, store_detach
);
134 /* Sysfs entry to establish a virtual connection */
135 static int valid_args(__u32 rhport
, enum usb_device_speed speed
)
138 if (rhport
>= VHCI_NPORTS
) {
139 pr_err("port %u\n", rhport
);
148 case USB_SPEED_WIRELESS
:
151 pr_err("speed %d\n", speed
);
159 * To start a new USB/IP attachment, a userland program needs to setup a TCP
160 * connection and then write its socket descriptor with remote device
161 * information into this sysfs file.
163 * A remote device is virtually attached to the root-hub port of @rhport with
164 * @speed. @devid is embedded into a request to specify the remote device in a
167 * write() returns 0 on success, else negative errno.
169 static ssize_t
store_attach(struct device
*dev
, struct device_attribute
*attr
,
170 const char *buf
, size_t count
)
172 struct vhci_device
*vdev
;
173 struct socket
*socket
;
175 __u32 rhport
= 0, devid
= 0, speed
= 0;
178 * @rhport: port number of vhci_hcd
179 * @sockfd: socket descriptor of an established TCP connection
180 * @devid: unique device identifier in a remote host
181 * @speed: usb device speed in a remote host
183 sscanf(buf
, "%u %u %u %u", &rhport
, &sockfd
, &devid
, &speed
);
185 usbip_dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n",
186 rhport
, sockfd
, devid
, speed
);
188 /* check received parameters */
189 if (valid_args(rhport
, speed
) < 0)
193 socket
= sockfd_to_socket(sockfd
);
197 /* now need lock until setting vdev status as used */
200 spin_lock(&the_controller
->lock
);
201 vdev
= port_to_vdev(rhport
);
202 spin_lock(&vdev
->ud
.lock
);
204 if (vdev
->ud
.status
!= VDEV_ST_NULL
) {
205 /* end of the lock */
206 spin_unlock(&vdev
->ud
.lock
);
207 spin_unlock(&the_controller
->lock
);
209 dev_err(dev
, "port %d already used\n", rhport
);
213 dev_info(dev
, "rhport(%u) sockfd(%d) devid(%u) speed(%u)\n",
214 rhport
, sockfd
, devid
, speed
);
218 vdev
->ud
.tcp_socket
= socket
;
219 vdev
->ud
.status
= VDEV_ST_NOTASSIGNED
;
221 spin_unlock(&vdev
->ud
.lock
);
222 spin_unlock(&the_controller
->lock
);
225 vdev
->ud
.tcp_rx
= kthread_run(vhci_rx_loop
, &vdev
->ud
, "vhci_rx");
226 vdev
->ud
.tcp_tx
= kthread_run(vhci_tx_loop
, &vdev
->ud
, "vhci_tx");
228 rh_port_connect(rhport
, speed
);
232 static DEVICE_ATTR(attach
, S_IWUSR
, NULL
, store_attach
);
234 static struct attribute
*dev_attrs
[] = {
235 &dev_attr_status
.attr
,
236 &dev_attr_detach
.attr
,
237 &dev_attr_attach
.attr
,
238 &dev_attr_usbip_debug
.attr
,
242 const struct attribute_group dev_attr_group
= {