1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/kthread.h>
21#include <linux/file.h>
22#include <linux/net.h>
23
24#include "usbip_common.h"
25#include "vhci.h"
26
27
28
29
30static ssize_t show_status(struct device *dev, struct device_attribute *attr,
31 char *out)
32{
33 char *s = out;
34 int i = 0;
35
36 BUG_ON(!the_controller || !out);
37
38 spin_lock(&the_controller->lock);
39
40
41
42
43
44
45
46
47
48
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);
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 ",
61 vdev->speed, vdev->devid);
62 out += sprintf(out, "%16p ", vdev->ud.tcp_socket);
63 out += sprintf(out, "%s", dev_name(&vdev->udev->dev));
64
65 } else {
66 out += sprintf(out, "000 000 000 0000000000000000 0-0");
67 }
68
69 out += sprintf(out, "\n");
70 spin_unlock(&vdev->ud.lock);
71 }
72
73 spin_unlock(&the_controller->lock);
74
75 return out - s;
76}
77static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
78
79
80static int vhci_port_disconnect(__u32 rhport)
81{
82 struct vhci_device *vdev;
83
84 usbip_dbg_vhci_sysfs("enter\n");
85
86
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) {
93 pr_err("not connected %d\n", vdev->ud.status);
94
95
96 spin_unlock(&vdev->ud.lock);
97 spin_unlock(&the_controller->lock);
98
99 return -EINVAL;
100 }
101
102
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
120 if (rhport >= VHCI_NPORTS) {
121 dev_err(dev, "invalid port %u\n", rhport);
122 return -EINVAL;
123 }
124
125 err = vhci_port_disconnect(rhport);
126 if (err < 0)
127 return -EINVAL;
128
129 usbip_dbg_vhci_sysfs("Leave\n");
130
131 return count;
132}
133static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
134
135
136static int valid_args(__u32 rhport, enum usb_device_speed speed)
137{
138
139 if (rhport >= VHCI_NPORTS) {
140 pr_err("port %u\n", rhport);
141 return -EINVAL;
142 }
143
144
145 switch (speed) {
146 case USB_SPEED_LOW:
147 case USB_SPEED_FULL:
148 case USB_SPEED_HIGH:
149 case USB_SPEED_WIRELESS:
150 break;
151 default:
152 pr_err("speed %d\n", speed);
153 return -EINVAL;
154 }
155
156 return 0;
157}
158
159
160
161
162
163
164
165
166
167
168
169
170static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
171 const char *buf, size_t count)
172{
173 struct vhci_device *vdev;
174 struct socket *socket;
175 int sockfd = 0;
176 __u32 rhport = 0, devid = 0, speed = 0;
177
178
179
180
181
182
183
184 sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed);
185
186 usbip_dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n",
187 rhport, sockfd, devid, speed);
188
189
190 if (valid_args(rhport, speed) < 0)
191 return -EINVAL;
192
193
194
195 socket = sockfd_to_socket(sockfd);
196 if (!socket)
197 return -EINVAL;
198
199
200
201
202 spin_lock(&the_controller->lock);
203 vdev = port_to_vdev(rhport);
204 spin_lock(&vdev->ud.lock);
205
206 if (vdev->ud.status != VDEV_ST_NULL) {
207
208 spin_unlock(&vdev->ud.lock);
209 spin_unlock(&the_controller->lock);
210
211 fput(socket->file);
212
213 dev_err(dev, "port %d already used\n", rhport);
214 return -EINVAL;
215 }
216
217 dev_info(dev, "rhport(%u) sockfd(%d) devid(%u) speed(%u)\n",
218 rhport, sockfd, devid, speed);
219
220 vdev->devid = devid;
221 vdev->speed = speed;
222 vdev->ud.tcp_socket = socket;
223 vdev->ud.status = VDEV_ST_NOTASSIGNED;
224
225 spin_unlock(&vdev->ud.lock);
226 spin_unlock(&the_controller->lock);
227
228
229 vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx");
230 vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx");
231
232 rh_port_connect(rhport, speed);
233
234 return count;
235}
236static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
237
238static struct attribute *dev_attrs[] = {
239 &dev_attr_status.attr,
240 &dev_attr_detach.attr,
241 &dev_attr_attach.attr,
242 &dev_attr_usbip_debug.attr,
243 NULL,
244};
245
246const struct attribute_group dev_attr_group = {
247 .attrs = dev_attrs,
248};
249