Merge tag 'pci-v3.15-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[deliverable/linux.git] / drivers / staging / usbip / vhci_sysfs.c
CommitLineData
04679b34
TH
1/*
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
7aaacb43 20#include <linux/kthread.h>
3d0a2a22 21#include <linux/file.h>
7aaacb43 22#include <linux/net.h>
23
04679b34
TH
24#include "usbip_common.h"
25#include "vhci.h"
26
04679b34
TH
27/* TODO: refine locking ?*/
28
29/* Sysfs entry to show port status */
b1f56aca 30static ssize_t status_show(struct device *dev, struct device_attribute *attr,
04679b34
TH
31 char *out)
32{
33 char *s = out;
34 int i = 0;
35
2961f24f 36 BUG_ON(!the_controller || !out);
04679b34
TH
37
38 spin_lock(&the_controller->lock);
39
40 /*
41 * output example:
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
45 *
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.
49 */
50 out += sprintf(out, "prt sta spd bus dev socket "
51 "local_busid\n");
52
53 for (i = 0; i < VHCI_NPORTS; i++) {
54 struct vhci_device *vdev = port_to_vdev(i);
55
56 spin_lock(&vdev->ud.lock);
04679b34
TH
57 out += sprintf(out, "%03u %03u ", i, vdev->ud.status);
58
59 if (vdev->ud.status == VDEV_ST_USED) {
60 out += sprintf(out, "%03u %08x ",
bd608f6c 61 vdev->speed, vdev->devid);
04679b34 62 out += sprintf(out, "%16p ", vdev->ud.tcp_socket);
e9133972 63 out += sprintf(out, "%s", dev_name(&vdev->udev->dev));
04679b34 64
bd608f6c 65 } else {
04679b34 66 out += sprintf(out, "000 000 000 0000000000000000 0-0");
bd608f6c 67 }
04679b34
TH
68
69 out += sprintf(out, "\n");
04679b34
TH
70 spin_unlock(&vdev->ud.lock);
71 }
72
73 spin_unlock(&the_controller->lock);
74
75 return out - s;
76}
b1f56aca 77static DEVICE_ATTR_RO(status);
04679b34
TH
78
79/* Sysfs entry to shutdown a virtual connection */
80static int vhci_port_disconnect(__u32 rhport)
81{
82 struct vhci_device *vdev;
83
b8868e45 84 usbip_dbg_vhci_sysfs("enter\n");
04679b34
TH
85
86 /* lock */
87 spin_lock(&the_controller->lock);
88
89 vdev = port_to_vdev(rhport);
90
91 spin_lock(&vdev->ud.lock);
92 if (vdev->ud.status == VDEV_ST_NULL) {
1a4b6f66 93 pr_err("not connected %d\n", vdev->ud.status);
04679b34
TH
94
95 /* unlock */
96 spin_unlock(&vdev->ud.lock);
97 spin_unlock(&the_controller->lock);
98
99 return -EINVAL;
100 }
101
102 /* unlock */
103 spin_unlock(&vdev->ud.lock);
104 spin_unlock(&the_controller->lock);
105
106 usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
107
108 return 0;
109}
110
111static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
112 const char *buf, size_t count)
113{
114 int err;
115 __u32 rhport = 0;
116
117 sscanf(buf, "%u", &rhport);
118
119 /* check rhport */
120 if (rhport >= VHCI_NPORTS) {
1a4b6f66 121 dev_err(dev, "invalid port %u\n", rhport);
04679b34
TH
122 return -EINVAL;
123 }
124
125 err = vhci_port_disconnect(rhport);
126 if (err < 0)
127 return -EINVAL;
128
b8868e45 129 usbip_dbg_vhci_sysfs("Leave\n");
bd608f6c 130
04679b34
TH
131 return count;
132}
133static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
134
135/* Sysfs entry to establish a virtual connection */
136static int valid_args(__u32 rhport, enum usb_device_speed speed)
137{
138 /* check rhport */
988e7520 139 if (rhport >= VHCI_NPORTS) {
1a4b6f66 140 pr_err("port %u\n", rhport);
04679b34
TH
141 return -EINVAL;
142 }
143
144 /* check speed */
145 switch (speed) {
146 case USB_SPEED_LOW:
147 case USB_SPEED_FULL:
148 case USB_SPEED_HIGH:
551cdbbe 149 case USB_SPEED_WIRELESS:
04679b34
TH
150 break;
151 default:
8360fb0d
SK
152 pr_err("Failed attach request for unsupported USB speed: %s\n",
153 usb_speed_string(speed));
04679b34
TH
154 return -EINVAL;
155 }
156
157 return 0;
158}
159
160/*
161 * To start a new USB/IP attachment, a userland program needs to setup a TCP
162 * connection and then write its socket descriptor with remote device
163 * information into this sysfs file.
164 *
165 * A remote device is virtually attached to the root-hub port of @rhport with
166 * @speed. @devid is embedded into a request to specify the remote device in a
167 * server host.
168 *
169 * write() returns 0 on success, else negative errno.
170 */
171static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
172 const char *buf, size_t count)
173{
174 struct vhci_device *vdev;
175 struct socket *socket;
176 int sockfd = 0;
177 __u32 rhport = 0, devid = 0, speed = 0;
178
179 /*
180 * @rhport: port number of vhci_hcd
181 * @sockfd: socket descriptor of an established TCP connection
182 * @devid: unique device identifier in a remote host
183 * @speed: usb device speed in a remote host
184 */
185 sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed);
186
b8868e45 187 usbip_dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n",
bd608f6c 188 rhport, sockfd, devid, speed);
04679b34
TH
189
190 /* check received parameters */
191 if (valid_args(rhport, speed) < 0)
192 return -EINVAL;
193
3d0a2a22
BB
194 /* Extract socket from fd. */
195 /* The correct way to clean this up is to fput(socket->file). */
04679b34
TH
196 socket = sockfd_to_socket(sockfd);
197 if (!socket)
a6d81814 198 return -EINVAL;
04679b34
TH
199
200 /* now need lock until setting vdev status as used */
201
202 /* begin a lock */
203 spin_lock(&the_controller->lock);
04679b34 204 vdev = port_to_vdev(rhport);
04679b34
TH
205 spin_lock(&vdev->ud.lock);
206
207 if (vdev->ud.status != VDEV_ST_NULL) {
208 /* end of the lock */
209 spin_unlock(&vdev->ud.lock);
210 spin_unlock(&the_controller->lock);
211
3d0a2a22
BB
212 fput(socket->file);
213
1a4b6f66 214 dev_err(dev, "port %d already used\n", rhport);
04679b34
TH
215 return -EINVAL;
216 }
217
1a4b6f66 218 dev_info(dev, "rhport(%u) sockfd(%d) devid(%u) speed(%u)\n",
219 rhport, sockfd, devid, speed);
04679b34
TH
220
221 vdev->devid = devid;
222 vdev->speed = speed;
223 vdev->ud.tcp_socket = socket;
224 vdev->ud.status = VDEV_ST_NOTASSIGNED;
225
226 spin_unlock(&vdev->ud.lock);
227 spin_unlock(&the_controller->lock);
228 /* end the lock */
229
ba46ce30
ON
230 vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx");
231 vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx");
d1b2e95a 232
04679b34
TH
233 rh_port_connect(rhport, speed);
234
235 return count;
236}
237static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
238
239static struct attribute *dev_attrs[] = {
240 &dev_attr_status.attr,
241 &dev_attr_detach.attr,
242 &dev_attr_attach.attr,
243 &dev_attr_usbip_debug.attr,
244 NULL,
245};
246
cf77acfc 247const struct attribute_group dev_attr_group = {
04679b34
TH
248 .attrs = dev_attrs,
249};
This page took 0.542929 seconds and 5 git commands to generate.