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/file.h>
22 #include <linux/net.h>
24 #include "usbip_common.h"
27 /* TODO: refine locking ?*/
29 /* Sysfs entry to show port status */
30 static ssize_t
status_show(struct device
*dev
, struct device_attribute
*attr
,
36 BUG_ON(!the_controller
|| !out
);
38 spin_lock(&the_controller
->lock
);
42 * prt sta spd dev socket local_busid
43 * 000 004 000 000 c5a7bb80 1-2.3
44 * 001 004 000 000 d8cee980 2-3.4
46 * IP address can be retrieved from a socket pointer address by looking
47 * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
48 * port number and its peer IP address.
51 "prt sta spd bus dev socket local_busid\n");
53 for (i
= 0; i
< VHCI_NPORTS
; i
++) {
54 struct vhci_device
*vdev
= port_to_vdev(i
);
56 spin_lock(&vdev
->ud
.lock
);
57 out
+= sprintf(out
, "%03u %03u ", i
, vdev
->ud
.status
);
59 if (vdev
->ud
.status
== VDEV_ST_USED
) {
60 out
+= sprintf(out
, "%03u %08x ",
61 vdev
->speed
, vdev
->devid
);
62 out
+= sprintf(out
, "%16p ", vdev
->ud
.tcp_socket
);
63 out
+= sprintf(out
, "%s", dev_name(&vdev
->udev
->dev
));
66 out
+= sprintf(out
, "000 000 000 0000000000000000 0-0");
69 out
+= sprintf(out
, "\n");
70 spin_unlock(&vdev
->ud
.lock
);
73 spin_unlock(&the_controller
->lock
);
77 static DEVICE_ATTR_RO(status
);
79 /* Sysfs entry to shutdown a virtual connection */
80 static int vhci_port_disconnect(__u32 rhport
)
82 struct vhci_device
*vdev
;
84 usbip_dbg_vhci_sysfs("enter\n");
87 spin_lock(&the_controller
->lock
);
89 vdev
= port_to_vdev(rhport
);
91 spin_lock(&vdev
->ud
.lock
);
92 if (vdev
->ud
.status
== VDEV_ST_NULL
) {
93 pr_err("not connected %d\n", vdev
->ud
.status
);
96 spin_unlock(&vdev
->ud
.lock
);
97 spin_unlock(&the_controller
->lock
);
103 spin_unlock(&vdev
->ud
.lock
);
104 spin_unlock(&the_controller
->lock
);
106 usbip_event_add(&vdev
->ud
, VDEV_EVENT_DOWN
);
111 static ssize_t
store_detach(struct device
*dev
, struct device_attribute
*attr
,
112 const char *buf
, size_t count
)
117 if (sscanf(buf
, "%u", &rhport
) != 1)
121 if (rhport
>= VHCI_NPORTS
) {
122 dev_err(dev
, "invalid port %u\n", rhport
);
126 err
= vhci_port_disconnect(rhport
);
130 usbip_dbg_vhci_sysfs("Leave\n");
134 static DEVICE_ATTR(detach
, S_IWUSR
, NULL
, store_detach
);
136 /* Sysfs entry to establish a virtual connection */
137 static int valid_args(__u32 rhport
, enum usb_device_speed speed
)
140 if (rhport
>= VHCI_NPORTS
) {
141 pr_err("port %u\n", rhport
);
150 case USB_SPEED_WIRELESS
:
153 pr_err("Failed attach request for unsupported USB speed: %s\n",
154 usb_speed_string(speed
));
162 * To start a new USB/IP attachment, a userland program needs to setup a TCP
163 * connection and then write its socket descriptor with remote device
164 * information into this sysfs file.
166 * A remote device is virtually attached to the root-hub port of @rhport with
167 * @speed. @devid is embedded into a request to specify the remote device in a
170 * write() returns 0 on success, else negative errno.
172 static ssize_t
store_attach(struct device
*dev
, struct device_attribute
*attr
,
173 const char *buf
, size_t count
)
175 struct vhci_device
*vdev
;
176 struct socket
*socket
;
178 __u32 rhport
= 0, devid
= 0, speed
= 0;
182 * @rhport: port number of vhci_hcd
183 * @sockfd: socket descriptor of an established TCP connection
184 * @devid: unique device identifier in a remote host
185 * @speed: usb device speed in a remote host
187 if (sscanf(buf
, "%u %u %u %u", &rhport
, &sockfd
, &devid
, &speed
) != 4)
190 usbip_dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n",
191 rhport
, sockfd
, devid
, speed
);
193 /* check received parameters */
194 if (valid_args(rhport
, speed
) < 0)
197 /* Extract socket from fd. */
198 socket
= sockfd_lookup(sockfd
, &err
);
202 /* now need lock until setting vdev status as used */
205 spin_lock(&the_controller
->lock
);
206 vdev
= port_to_vdev(rhport
);
207 spin_lock(&vdev
->ud
.lock
);
209 if (vdev
->ud
.status
!= VDEV_ST_NULL
) {
210 /* end of the lock */
211 spin_unlock(&vdev
->ud
.lock
);
212 spin_unlock(&the_controller
->lock
);
216 dev_err(dev
, "port %d already used\n", rhport
);
221 "rhport(%u) sockfd(%d) devid(%u) speed(%u) speed_str(%s)\n",
222 rhport
, sockfd
, devid
, speed
, usb_speed_string(speed
));
226 vdev
->ud
.tcp_socket
= socket
;
227 vdev
->ud
.status
= VDEV_ST_NOTASSIGNED
;
229 spin_unlock(&vdev
->ud
.lock
);
230 spin_unlock(&the_controller
->lock
);
233 vdev
->ud
.tcp_rx
= kthread_get_run(vhci_rx_loop
, &vdev
->ud
, "vhci_rx");
234 vdev
->ud
.tcp_tx
= kthread_get_run(vhci_tx_loop
, &vdev
->ud
, "vhci_tx");
236 rh_port_connect(rhport
, speed
);
240 static DEVICE_ATTR(attach
, S_IWUSR
, NULL
, store_attach
);
242 static struct attribute
*dev_attrs
[] = {
243 &dev_attr_status
.attr
,
244 &dev_attr_detach
.attr
,
245 &dev_attr_attach
.attr
,
246 &dev_attr_usbip_debug
.attr
,
250 const struct attribute_group dev_attr_group
= {