1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/device.h>
21#include <linux/file.h>
22#include <linux/kthread.h>
23#include <linux/module.h>
24
25#include "usbip_common.h"
26#include "stub.h"
27
28
29
30
31
32static ssize_t usbip_status_show(struct device *dev,
33 struct device_attribute *attr, char *buf)
34{
35 struct stub_device *sdev = dev_get_drvdata(dev);
36 int status;
37
38 if (!sdev) {
39 dev_err(dev, "sdev is null\n");
40 return -ENODEV;
41 }
42
43 spin_lock_irq(&sdev->ud.lock);
44 status = sdev->ud.status;
45 spin_unlock_irq(&sdev->ud.lock);
46
47 return snprintf(buf, PAGE_SIZE, "%d\n", status);
48}
49static DEVICE_ATTR_RO(usbip_status);
50
51
52
53
54
55
56static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
57 const char *buf, size_t count)
58{
59 struct stub_device *sdev = dev_get_drvdata(dev);
60 int sockfd = 0;
61 struct socket *socket;
62 int rv;
63
64 if (!sdev) {
65 dev_err(dev, "sdev is null\n");
66 return -ENODEV;
67 }
68
69 rv = sscanf(buf, "%d", &sockfd);
70 if (rv != 1)
71 return -EINVAL;
72
73 if (sockfd != -1) {
74 int err;
75
76 dev_info(dev, "stub up\n");
77
78 spin_lock_irq(&sdev->ud.lock);
79
80 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
81 dev_err(dev, "not ready\n");
82 goto err;
83 }
84
85 socket = sockfd_lookup(sockfd, &err);
86 if (!socket)
87 goto err;
88
89 sdev->ud.tcp_socket = socket;
90
91 spin_unlock_irq(&sdev->ud.lock);
92
93 sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud,
94 "stub_rx");
95 sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud,
96 "stub_tx");
97
98 spin_lock_irq(&sdev->ud.lock);
99 sdev->ud.status = SDEV_ST_USED;
100 spin_unlock_irq(&sdev->ud.lock);
101
102 } else {
103 dev_info(dev, "stub down\n");
104
105 spin_lock_irq(&sdev->ud.lock);
106 if (sdev->ud.status != SDEV_ST_USED)
107 goto err;
108
109 spin_unlock_irq(&sdev->ud.lock);
110
111 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
112 }
113
114 return count;
115
116err:
117 spin_unlock_irq(&sdev->ud.lock);
118 return -EINVAL;
119}
120static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
121
122static int stub_add_files(struct device *dev)
123{
124 int err = 0;
125
126 err = device_create_file(dev, &dev_attr_usbip_status);
127 if (err)
128 goto err_status;
129
130 err = device_create_file(dev, &dev_attr_usbip_sockfd);
131 if (err)
132 goto err_sockfd;
133
134 err = device_create_file(dev, &dev_attr_usbip_debug);
135 if (err)
136 goto err_debug;
137
138 return 0;
139
140err_debug:
141 device_remove_file(dev, &dev_attr_usbip_sockfd);
142err_sockfd:
143 device_remove_file(dev, &dev_attr_usbip_status);
144err_status:
145 return err;
146}
147
148static void stub_remove_files(struct device *dev)
149{
150 device_remove_file(dev, &dev_attr_usbip_status);
151 device_remove_file(dev, &dev_attr_usbip_sockfd);
152 device_remove_file(dev, &dev_attr_usbip_debug);
153}
154
155static void stub_shutdown_connection(struct usbip_device *ud)
156{
157 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
158
159
160
161
162
163
164
165 if (ud->tcp_socket) {
166 dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n",
167 ud->tcp_socket);
168 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
169 }
170
171
172 if (ud->tcp_rx) {
173 kthread_stop_put(ud->tcp_rx);
174 ud->tcp_rx = NULL;
175 }
176 if (ud->tcp_tx) {
177 kthread_stop_put(ud->tcp_tx);
178 ud->tcp_tx = NULL;
179 }
180
181
182
183
184
185
186
187 if (ud->tcp_socket) {
188 sockfd_put(ud->tcp_socket);
189 ud->tcp_socket = NULL;
190 }
191
192
193 stub_device_cleanup_urbs(sdev);
194
195
196 {
197 unsigned long flags;
198 struct stub_unlink *unlink, *tmp;
199
200 spin_lock_irqsave(&sdev->priv_lock, flags);
201 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
202 list_del(&unlink->list);
203 kfree(unlink);
204 }
205 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
206 list) {
207 list_del(&unlink->list);
208 kfree(unlink);
209 }
210 spin_unlock_irqrestore(&sdev->priv_lock, flags);
211 }
212}
213
214static void stub_device_reset(struct usbip_device *ud)
215{
216 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
217 struct usb_device *udev = sdev->udev;
218 int ret;
219
220 dev_dbg(&udev->dev, "device reset");
221
222 ret = usb_lock_device_for_reset(udev, NULL);
223 if (ret < 0) {
224 dev_err(&udev->dev, "lock for reset\n");
225 spin_lock_irq(&ud->lock);
226 ud->status = SDEV_ST_ERROR;
227 spin_unlock_irq(&ud->lock);
228 return;
229 }
230
231
232 ret = usb_reset_device(udev);
233 usb_unlock_device(udev);
234
235 spin_lock_irq(&ud->lock);
236 if (ret) {
237 dev_err(&udev->dev, "device reset\n");
238 ud->status = SDEV_ST_ERROR;
239 } else {
240 dev_info(&udev->dev, "device reset\n");
241 ud->status = SDEV_ST_AVAILABLE;
242 }
243 spin_unlock_irq(&ud->lock);
244}
245
246static void stub_device_unusable(struct usbip_device *ud)
247{
248 spin_lock_irq(&ud->lock);
249 ud->status = SDEV_ST_ERROR;
250 spin_unlock_irq(&ud->lock);
251}
252
253
254
255
256
257
258
259static struct stub_device *stub_device_alloc(struct usb_device *udev)
260{
261 struct stub_device *sdev;
262 int busnum = udev->bus->busnum;
263 int devnum = udev->devnum;
264
265 dev_dbg(&udev->dev, "allocating stub device");
266
267
268 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
269 if (!sdev)
270 return NULL;
271
272 sdev->udev = usb_get_dev(udev);
273
274
275
276
277
278
279 sdev->devid = (busnum << 16) | devnum;
280 sdev->ud.side = USBIP_STUB;
281 sdev->ud.status = SDEV_ST_AVAILABLE;
282 spin_lock_init(&sdev->ud.lock);
283 sdev->ud.tcp_socket = NULL;
284
285 INIT_LIST_HEAD(&sdev->priv_init);
286 INIT_LIST_HEAD(&sdev->priv_tx);
287 INIT_LIST_HEAD(&sdev->priv_free);
288 INIT_LIST_HEAD(&sdev->unlink_free);
289 INIT_LIST_HEAD(&sdev->unlink_tx);
290 spin_lock_init(&sdev->priv_lock);
291
292 init_waitqueue_head(&sdev->tx_waitq);
293
294 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
295 sdev->ud.eh_ops.reset = stub_device_reset;
296 sdev->ud.eh_ops.unusable = stub_device_unusable;
297
298 usbip_start_eh(&sdev->ud);
299
300 dev_dbg(&udev->dev, "register new device\n");
301
302 return sdev;
303}
304
305static void stub_device_free(struct stub_device *sdev)
306{
307 kfree(sdev);
308}
309
310static int stub_probe(struct usb_device *udev)
311{
312 struct stub_device *sdev = NULL;
313 const char *udev_busid = dev_name(&udev->dev);
314 struct bus_id_priv *busid_priv;
315 int rc;
316
317 dev_dbg(&udev->dev, "Enter\n");
318
319
320 busid_priv = get_busid_priv(udev_busid);
321 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
322 (busid_priv->status == STUB_BUSID_OTHER)) {
323 dev_info(&udev->dev,
324 "%s is not in match_busid table... skip!\n",
325 udev_busid);
326
327
328
329
330
331
332 return -ENODEV;
333 }
334
335 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
336 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
337 udev_busid);
338 return -ENODEV;
339 }
340
341 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
342 dev_dbg(&udev->dev,
343 "%s is attached on vhci_hcd... skip!\n",
344 udev_busid);
345
346 return -ENODEV;
347 }
348
349
350 sdev = stub_device_alloc(udev);
351 if (!sdev)
352 return -ENOMEM;
353
354 dev_info(&udev->dev,
355 "usbip-host: register new device (bus %u dev %u)\n",
356 udev->bus->busnum, udev->devnum);
357
358 busid_priv->shutdown_busid = 0;
359
360
361 dev_set_drvdata(&udev->dev, sdev);
362 busid_priv->sdev = sdev;
363 busid_priv->udev = udev;
364
365
366
367
368
369
370 rc = usb_hub_claim_port(udev->parent, udev->portnum,
371 (struct usb_dev_state *) udev);
372 if (rc) {
373 dev_dbg(&udev->dev, "unable to claim port\n");
374 goto err_port;
375 }
376
377 rc = stub_add_files(&udev->dev);
378 if (rc) {
379 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
380 goto err_files;
381 }
382 busid_priv->status = STUB_BUSID_ALLOC;
383
384 return 0;
385err_files:
386 usb_hub_release_port(udev->parent, udev->portnum,
387 (struct usb_dev_state *) udev);
388err_port:
389 dev_set_drvdata(&udev->dev, NULL);
390 usb_put_dev(udev);
391
392 busid_priv->sdev = NULL;
393 stub_device_free(sdev);
394 return rc;
395}
396
397static void shutdown_busid(struct bus_id_priv *busid_priv)
398{
399 if (busid_priv->sdev && !busid_priv->shutdown_busid) {
400 busid_priv->shutdown_busid = 1;
401 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
402
403
404 usbip_stop_eh(&busid_priv->sdev->ud);
405 }
406}
407
408
409
410
411
412static void stub_disconnect(struct usb_device *udev)
413{
414 struct stub_device *sdev;
415 const char *udev_busid = dev_name(&udev->dev);
416 struct bus_id_priv *busid_priv;
417 int rc;
418
419 dev_dbg(&udev->dev, "Enter\n");
420
421 busid_priv = get_busid_priv(udev_busid);
422 if (!busid_priv) {
423 BUG();
424 return;
425 }
426
427 sdev = dev_get_drvdata(&udev->dev);
428
429
430 if (!sdev) {
431 dev_err(&udev->dev, "could not get device");
432 return;
433 }
434
435 dev_set_drvdata(&udev->dev, NULL);
436
437
438
439
440 stub_remove_files(&udev->dev);
441
442
443 rc = usb_hub_release_port(udev->parent, udev->portnum,
444 (struct usb_dev_state *) udev);
445 if (rc) {
446 dev_dbg(&udev->dev, "unable to release port\n");
447 return;
448 }
449
450
451 if (usbip_in_eh(current))
452 return;
453
454
455 shutdown_busid(busid_priv);
456
457 usb_put_dev(sdev->udev);
458
459
460 busid_priv->sdev = NULL;
461 stub_device_free(sdev);
462
463 if (busid_priv->status == STUB_BUSID_ALLOC) {
464 busid_priv->status = STUB_BUSID_ADDED;
465 } else {
466 busid_priv->status = STUB_BUSID_OTHER;
467 del_match_busid((char *)udev_busid);
468 }
469}
470
471#ifdef CONFIG_PM
472
473
474
475
476static int stub_suspend(struct usb_device *udev, pm_message_t message)
477{
478 dev_dbg(&udev->dev, "stub_suspend\n");
479
480 return 0;
481}
482
483static int stub_resume(struct usb_device *udev, pm_message_t message)
484{
485 dev_dbg(&udev->dev, "stub_resume\n");
486
487 return 0;
488}
489
490#endif
491
492struct usb_device_driver stub_driver = {
493 .name = "usbip-host",
494 .probe = stub_probe,
495 .disconnect = stub_disconnect,
496#ifdef CONFIG_PM
497 .suspend = stub_suspend,
498 .resume = stub_resume,
499#endif
500 .supports_autosuspend = 0,
501};
502