2008-07-09 14:56:51 -06:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2008 Takahiro Hirofuchi
|
|
|
|
*
|
|
|
|
* This is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|
|
|
* USA.
|
|
|
|
*/
|
|
|
|
|
2011-05-11 23:33:43 -06:00
|
|
|
#include <linux/kthread.h>
|
2012-10-21 13:45:00 -06:00
|
|
|
#include <linux/file.h>
|
2011-05-11 23:33:43 -06:00
|
|
|
#include <linux/net.h>
|
|
|
|
|
2008-07-09 14:56:51 -06:00
|
|
|
#include "usbip_common.h"
|
|
|
|
#include "vhci.h"
|
|
|
|
|
|
|
|
/* TODO: refine locking ?*/
|
|
|
|
|
|
|
|
/* Sysfs entry to show port status */
|
2013-08-26 13:02:54 -06:00
|
|
|
static ssize_t status_show(struct device *dev, struct device_attribute *attr,
|
2008-07-09 14:56:51 -06:00
|
|
|
char *out)
|
|
|
|
{
|
|
|
|
char *s = out;
|
|
|
|
int i = 0;
|
|
|
|
|
2009-03-09 23:10:27 -06:00
|
|
|
BUG_ON(!the_controller || !out);
|
2008-07-09 14:56:51 -06:00
|
|
|
|
|
|
|
spin_lock(&the_controller->lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* output example:
|
|
|
|
* prt sta spd dev socket local_busid
|
|
|
|
* 000 004 000 000 c5a7bb80 1-2.3
|
|
|
|
* 001 004 000 000 d8cee980 2-3.4
|
|
|
|
*
|
|
|
|
* IP address can be retrieved from a socket pointer address by looking
|
|
|
|
* up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
|
|
|
|
* port number and its peer IP address.
|
|
|
|
*/
|
|
|
|
out += sprintf(out, "prt sta spd bus dev socket "
|
|
|
|
"local_busid\n");
|
|
|
|
|
|
|
|
for (i = 0; i < VHCI_NPORTS; i++) {
|
|
|
|
struct vhci_device *vdev = port_to_vdev(i);
|
|
|
|
|
|
|
|
spin_lock(&vdev->ud.lock);
|
|
|
|
out += sprintf(out, "%03u %03u ", i, vdev->ud.status);
|
|
|
|
|
|
|
|
if (vdev->ud.status == VDEV_ST_USED) {
|
|
|
|
out += sprintf(out, "%03u %08x ",
|
2011-05-06 04:47:52 -06:00
|
|
|
vdev->speed, vdev->devid);
|
2008-07-09 14:56:51 -06:00
|
|
|
out += sprintf(out, "%16p ", vdev->ud.tcp_socket);
|
2008-10-29 18:59:32 -06:00
|
|
|
out += sprintf(out, "%s", dev_name(&vdev->udev->dev));
|
2008-07-09 14:56:51 -06:00
|
|
|
|
2011-05-06 04:47:52 -06:00
|
|
|
} else {
|
2008-07-09 14:56:51 -06:00
|
|
|
out += sprintf(out, "000 000 000 0000000000000000 0-0");
|
2011-05-06 04:47:52 -06:00
|
|
|
}
|
2008-07-09 14:56:51 -06:00
|
|
|
|
|
|
|
out += sprintf(out, "\n");
|
|
|
|
spin_unlock(&vdev->ud.lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(&the_controller->lock);
|
|
|
|
|
|
|
|
return out - s;
|
|
|
|
}
|
2013-08-26 13:02:54 -06:00
|
|
|
static DEVICE_ATTR_RO(status);
|
2008-07-09 14:56:51 -06:00
|
|
|
|
|
|
|
/* Sysfs entry to shutdown a virtual connection */
|
|
|
|
static int vhci_port_disconnect(__u32 rhport)
|
|
|
|
{
|
|
|
|
struct vhci_device *vdev;
|
|
|
|
|
2009-07-21 00:46:13 -06:00
|
|
|
usbip_dbg_vhci_sysfs("enter\n");
|
2008-07-09 14:56:51 -06:00
|
|
|
|
|
|
|
/* lock */
|
|
|
|
spin_lock(&the_controller->lock);
|
|
|
|
|
|
|
|
vdev = port_to_vdev(rhport);
|
|
|
|
|
|
|
|
spin_lock(&vdev->ud.lock);
|
|
|
|
if (vdev->ud.status == VDEV_ST_NULL) {
|
2011-05-19 17:47:32 -06:00
|
|
|
pr_err("not connected %d\n", vdev->ud.status);
|
2008-07-09 14:56:51 -06:00
|
|
|
|
|
|
|
/* unlock */
|
|
|
|
spin_unlock(&vdev->ud.lock);
|
|
|
|
spin_unlock(&the_controller->lock);
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unlock */
|
|
|
|
spin_unlock(&vdev->ud.lock);
|
|
|
|
spin_unlock(&the_controller->lock);
|
|
|
|
|
|
|
|
usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
__u32 rhport = 0;
|
|
|
|
|
|
|
|
sscanf(buf, "%u", &rhport);
|
|
|
|
|
|
|
|
/* check rhport */
|
|
|
|
if (rhport >= VHCI_NPORTS) {
|
2011-05-19 17:47:32 -06:00
|
|
|
dev_err(dev, "invalid port %u\n", rhport);
|
2008-07-09 14:56:51 -06:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = vhci_port_disconnect(rhport);
|
|
|
|
if (err < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2009-07-21 00:46:13 -06:00
|
|
|
usbip_dbg_vhci_sysfs("Leave\n");
|
2011-05-06 04:47:52 -06:00
|
|
|
|
2008-07-09 14:56:51 -06:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
|
|
|
|
|
|
|
|
/* Sysfs entry to establish a virtual connection */
|
|
|
|
static int valid_args(__u32 rhport, enum usb_device_speed speed)
|
|
|
|
{
|
|
|
|
/* check rhport */
|
2011-05-26 01:24:45 -06:00
|
|
|
if (rhport >= VHCI_NPORTS) {
|
2011-05-19 17:47:32 -06:00
|
|
|
pr_err("port %u\n", rhport);
|
2008-07-09 14:56:51 -06:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check speed */
|
|
|
|
switch (speed) {
|
|
|
|
case USB_SPEED_LOW:
|
|
|
|
case USB_SPEED_FULL:
|
|
|
|
case USB_SPEED_HIGH:
|
2010-01-14 12:08:04 -07:00
|
|
|
case USB_SPEED_WIRELESS:
|
2008-07-09 14:56:51 -06:00
|
|
|
break;
|
|
|
|
default:
|
2014-01-22 09:38:14 -07:00
|
|
|
pr_err("Failed attach request for unsupported USB speed: %s\n",
|
|
|
|
usb_speed_string(speed));
|
2008-07-09 14:56:51 -06:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To start a new USB/IP attachment, a userland program needs to setup a TCP
|
|
|
|
* connection and then write its socket descriptor with remote device
|
|
|
|
* information into this sysfs file.
|
|
|
|
*
|
|
|
|
* A remote device is virtually attached to the root-hub port of @rhport with
|
|
|
|
* @speed. @devid is embedded into a request to specify the remote device in a
|
|
|
|
* server host.
|
|
|
|
*
|
|
|
|
* write() returns 0 on success, else negative errno.
|
|
|
|
*/
|
|
|
|
static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct vhci_device *vdev;
|
|
|
|
struct socket *socket;
|
|
|
|
int sockfd = 0;
|
|
|
|
__u32 rhport = 0, devid = 0, speed = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @rhport: port number of vhci_hcd
|
|
|
|
* @sockfd: socket descriptor of an established TCP connection
|
|
|
|
* @devid: unique device identifier in a remote host
|
|
|
|
* @speed: usb device speed in a remote host
|
|
|
|
*/
|
|
|
|
sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed);
|
|
|
|
|
2009-07-21 00:46:13 -06:00
|
|
|
usbip_dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n",
|
2011-05-06 04:47:52 -06:00
|
|
|
rhport, sockfd, devid, speed);
|
2008-07-09 14:56:51 -06:00
|
|
|
|
|
|
|
/* check received parameters */
|
|
|
|
if (valid_args(rhport, speed) < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-10-21 13:45:00 -06:00
|
|
|
/* Extract socket from fd. */
|
|
|
|
/* The correct way to clean this up is to fput(socket->file). */
|
2008-07-09 14:56:51 -06:00
|
|
|
socket = sockfd_to_socket(sockfd);
|
|
|
|
if (!socket)
|
2011-05-26 22:18:48 -06:00
|
|
|
return -EINVAL;
|
2008-07-09 14:56:51 -06:00
|
|
|
|
|
|
|
/* now need lock until setting vdev status as used */
|
|
|
|
|
|
|
|
/* begin a lock */
|
|
|
|
spin_lock(&the_controller->lock);
|
|
|
|
vdev = port_to_vdev(rhport);
|
|
|
|
spin_lock(&vdev->ud.lock);
|
|
|
|
|
|
|
|
if (vdev->ud.status != VDEV_ST_NULL) {
|
|
|
|
/* end of the lock */
|
|
|
|
spin_unlock(&vdev->ud.lock);
|
|
|
|
spin_unlock(&the_controller->lock);
|
|
|
|
|
2012-10-21 13:45:00 -06:00
|
|
|
fput(socket->file);
|
|
|
|
|
2011-05-19 17:47:32 -06:00
|
|
|
dev_err(dev, "port %d already used\n", rhport);
|
2008-07-09 14:56:51 -06:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2011-05-19 17:47:32 -06:00
|
|
|
dev_info(dev, "rhport(%u) sockfd(%d) devid(%u) speed(%u)\n",
|
|
|
|
rhport, sockfd, devid, speed);
|
2008-07-09 14:56:51 -06:00
|
|
|
|
|
|
|
vdev->devid = devid;
|
|
|
|
vdev->speed = speed;
|
|
|
|
vdev->ud.tcp_socket = socket;
|
|
|
|
vdev->ud.status = VDEV_ST_NOTASSIGNED;
|
|
|
|
|
|
|
|
spin_unlock(&vdev->ud.lock);
|
|
|
|
spin_unlock(&the_controller->lock);
|
|
|
|
/* end the lock */
|
|
|
|
|
2012-03-13 12:07:18 -06:00
|
|
|
vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx");
|
|
|
|
vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx");
|
2011-04-18 13:44:10 -06:00
|
|
|
|
2008-07-09 14:56:51 -06:00
|
|
|
rh_port_connect(rhport, speed);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
|
|
|
|
|
|
|
|
static struct attribute *dev_attrs[] = {
|
|
|
|
&dev_attr_status.attr,
|
|
|
|
&dev_attr_detach.attr,
|
|
|
|
&dev_attr_attach.attr,
|
|
|
|
&dev_attr_usbip_debug.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2011-05-30 13:50:26 -06:00
|
|
|
const struct attribute_group dev_attr_group = {
|
2008-07-09 14:56:51 -06:00
|
|
|
.attrs = dev_attrs,
|
|
|
|
};
|