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/net.h>
22
23#include "usbip_common.h"
24#include "vhci.h"
25
26
27
28
29static ssize_t show_status(struct device *dev, struct device_attribute *attr,
30 char *out)
31{
32 char *s = out;
33 int i = 0;
34
35 BUG_ON(!the_controller || !out);
36
37 spin_lock(&the_controller->lock);
38
39
40
41
42
43
44
45
46
47
48
49 out += sprintf(out, "prt sta spd bus dev socket "
50 "local_busid\n");
51
52 for (i = 0; i < VHCI_NPORTS; i++) {
53 struct vhci_device *vdev = port_to_vdev(i);
54
55 spin_lock(&vdev->ud.lock);
56 out += sprintf(out, "%03u %03u ", i, vdev->ud.status);
57
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));
63
64 } else {
65 out += sprintf(out, "000 000 000 0000000000000000 0-0");
66 }
67
68 out += sprintf(out, "\n");
69 spin_unlock(&vdev->ud.lock);
70 }
71
72 spin_unlock(&the_controller->lock);
73
74 return out - s;
75}
76static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
77
78
79static int vhci_port_disconnect(__u32 rhport)
80{
81 struct vhci_device *vdev;
82
83 usbip_dbg_vhci_sysfs("enter\n");
84
85
86 spin_lock(&the_controller->lock);
87
88 vdev = port_to_vdev(rhport);
89
90 spin_lock(&vdev->ud.lock);
91 if (vdev->ud.status == VDEV_ST_NULL) {
92 pr_err("not connected %d\n", vdev->ud.status);
93
94
95 spin_unlock(&vdev->ud.lock);
96 spin_unlock(&the_controller->lock);
97
98 return -EINVAL;
99 }
100
101
102 spin_unlock(&vdev->ud.lock);
103 spin_unlock(&the_controller->lock);
104
105 usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
106
107 return 0;
108}
109
110static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
111 const char *buf, size_t count)
112{
113 int err;
114 __u32 rhport = 0;
115
116 sscanf(buf, "%u", &rhport);
117
118
119 if (rhport >= VHCI_NPORTS) {
120 dev_err(dev, "invalid port %u\n", rhport);
121 return -EINVAL;
122 }
123
124 err = vhci_port_disconnect(rhport);
125 if (err < 0)
126 return -EINVAL;
127
128 usbip_dbg_vhci_sysfs("Leave\n");
129
130 return count;
131}
132static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
133
134
135static int valid_args(__u32 rhport, enum usb_device_speed speed)
136{
137
138 if ((rhport < 0) || (rhport >= VHCI_NPORTS)) {
139 pr_err("port %u\n", rhport);
140 return -EINVAL;
141 }
142
143
144 switch (speed) {
145 case USB_SPEED_LOW:
146 case USB_SPEED_FULL:
147 case USB_SPEED_HIGH:
148 case USB_SPEED_WIRELESS:
149 break;
150 default:
151 pr_err("speed %d\n", speed);
152 return -EINVAL;
153 }
154
155 return 0;
156}
157
158
159
160
161
162
163
164
165
166
167
168
169static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
170 const char *buf, size_t count)
171{
172 struct vhci_device *vdev;
173 struct socket *socket;
174 int sockfd = 0;
175 __u32 rhport = 0, devid = 0, speed = 0;
176
177
178
179
180
181
182
183 sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed);
184
185 usbip_dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n",
186 rhport, sockfd, devid, speed);
187
188
189 if (valid_args(rhport, speed) < 0)
190 return -EINVAL;
191
192
193 socket = sockfd_to_socket(sockfd);
194 if (!socket)
195 return -EINVAL;
196
197
198
199
200 spin_lock(&the_controller->lock);
201 vdev = port_to_vdev(rhport);
202 spin_lock(&vdev->ud.lock);
203
204 if (vdev->ud.status != VDEV_ST_NULL) {
205
206 spin_unlock(&vdev->ud.lock);
207 spin_unlock(&the_controller->lock);
208
209 dev_err(dev, "port %d already used\n", rhport);
210 return -EINVAL;
211 }
212
213 dev_info(dev, "rhport(%u) sockfd(%d) devid(%u) speed(%u)\n",
214 rhport, sockfd, devid, speed);
215
216 vdev->devid = devid;
217 vdev->speed = speed;
218 vdev->ud.tcp_socket = socket;
219 vdev->ud.status = VDEV_ST_NOTASSIGNED;
220
221 spin_unlock(&vdev->ud.lock);
222 spin_unlock(&the_controller->lock);
223
224
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");
227
228 rh_port_connect(rhport, speed);
229
230 return count;
231}
232static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
233
234static 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,
239 NULL,
240};
241
242struct attribute_group dev_attr_group = {
243 .attrs = dev_attrs,
244};
245