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/kthread.h>
22
23#include "usbip_common.h"
24#include "stub.h"
25
26
27
28
29
30
31static struct usb_device_id stub_table[] = {
32#if 0
33
34 { USB_DEVICE(0x05ac, 0x0301) },
35 { USB_DEVICE(0x0430, 0x0009) },
36 { USB_DEVICE(0x059b, 0x0001) },
37 { USB_DEVICE(0x04b3, 0x4427) },
38 { USB_DEVICE(0x05a9, 0xa511) },
39 { USB_DEVICE(0x55aa, 0x0201) },
40 { USB_DEVICE(0x046d, 0x0870) },
41 { USB_DEVICE(0x04bb, 0x0101) },
42 { USB_DEVICE(0x04bb, 0x0904) },
43 { USB_DEVICE(0x04bb, 0x0201) },
44 { USB_DEVICE(0x08bb, 0x2702) },
45 { USB_DEVICE(0x046d, 0x08b2) },
46#endif
47
48 { .driver_info = 1 },
49 { 0, }
50};
51MODULE_DEVICE_TABLE(usb, stub_table);
52
53
54
55
56
57static ssize_t show_status(struct device *dev, struct device_attribute *attr,
58 char *buf)
59{
60 struct stub_device *sdev = dev_get_drvdata(dev);
61 int status;
62
63 if (!sdev) {
64 dev_err(dev, "sdev is null\n");
65 return -ENODEV;
66 }
67
68 spin_lock(&sdev->ud.lock);
69 status = sdev->ud.status;
70 spin_unlock(&sdev->ud.lock);
71
72 return snprintf(buf, PAGE_SIZE, "%d\n", status);
73}
74static DEVICE_ATTR(usbip_status, S_IRUGO, show_status, NULL);
75
76
77
78
79
80
81static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
82 const char *buf, size_t count)
83{
84 struct stub_device *sdev = dev_get_drvdata(dev);
85 int sockfd = 0;
86 struct socket *socket;
87
88 if (!sdev) {
89 dev_err(dev, "sdev is null\n");
90 return -ENODEV;
91 }
92
93 sscanf(buf, "%d", &sockfd);
94
95 if (sockfd != -1) {
96 dev_info(dev, "stub up\n");
97
98 spin_lock(&sdev->ud.lock);
99
100 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
101 dev_err(dev, "not ready\n");
102 spin_unlock(&sdev->ud.lock);
103 return -EINVAL;
104 }
105
106 socket = sockfd_to_socket(sockfd);
107 if (!socket) {
108 spin_unlock(&sdev->ud.lock);
109 return -EINVAL;
110 }
111#if 0
112 setnodelay(socket);
113 setkeepalive(socket);
114 setreuse(socket);
115#endif
116 sdev->ud.tcp_socket = socket;
117
118 spin_unlock(&sdev->ud.lock);
119
120 sdev->ud.tcp_rx = kthread_run(stub_rx_loop, &sdev->ud, "stub_rx");
121 sdev->ud.tcp_tx = kthread_run(stub_tx_loop, &sdev->ud, "stub_tx");
122
123 spin_lock(&sdev->ud.lock);
124 sdev->ud.status = SDEV_ST_USED;
125 spin_unlock(&sdev->ud.lock);
126
127 } else {
128 dev_info(dev, "stub down\n");
129
130 spin_lock(&sdev->ud.lock);
131 if (sdev->ud.status != SDEV_ST_USED) {
132 spin_unlock(&sdev->ud.lock);
133 return -EINVAL;
134 }
135 spin_unlock(&sdev->ud.lock);
136
137 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
138 }
139
140 return count;
141}
142static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
143
144static int stub_add_files(struct device *dev)
145{
146 int err = 0;
147
148 err = device_create_file(dev, &dev_attr_usbip_status);
149 if (err)
150 goto err_status;
151
152 err = device_create_file(dev, &dev_attr_usbip_sockfd);
153 if (err)
154 goto err_sockfd;
155
156 err = device_create_file(dev, &dev_attr_usbip_debug);
157 if (err)
158 goto err_debug;
159
160 return 0;
161
162err_debug:
163 device_remove_file(dev, &dev_attr_usbip_sockfd);
164err_sockfd:
165 device_remove_file(dev, &dev_attr_usbip_status);
166err_status:
167 return err;
168}
169
170static void stub_remove_files(struct device *dev)
171{
172 device_remove_file(dev, &dev_attr_usbip_status);
173 device_remove_file(dev, &dev_attr_usbip_sockfd);
174 device_remove_file(dev, &dev_attr_usbip_debug);
175}
176
177static void stub_shutdown_connection(struct usbip_device *ud)
178{
179 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
180
181
182
183
184
185
186
187 if (ud->tcp_socket) {
188 dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n",
189 ud->tcp_socket);
190 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
191 }
192
193
194 if (ud->tcp_rx && !task_is_dead(ud->tcp_rx))
195 kthread_stop(ud->tcp_rx);
196 if (ud->tcp_tx && !task_is_dead(ud->tcp_tx))
197 kthread_stop(ud->tcp_tx);
198
199
200
201
202
203
204
205 if (ud->tcp_socket) {
206 sock_release(ud->tcp_socket);
207 ud->tcp_socket = NULL;
208 }
209
210
211 stub_device_cleanup_urbs(sdev);
212
213
214 {
215 unsigned long flags;
216 struct stub_unlink *unlink, *tmp;
217
218 spin_lock_irqsave(&sdev->priv_lock, flags);
219 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
220 list_del(&unlink->list);
221 kfree(unlink);
222 }
223 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
224 list) {
225 list_del(&unlink->list);
226 kfree(unlink);
227 }
228 spin_unlock_irqrestore(&sdev->priv_lock, flags);
229 }
230}
231
232static void stub_device_reset(struct usbip_device *ud)
233{
234 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
235 struct usb_device *udev = sdev->udev;
236 int ret;
237
238 dev_dbg(&udev->dev, "device reset");
239
240 ret = usb_lock_device_for_reset(udev, sdev->interface);
241 if (ret < 0) {
242 dev_err(&udev->dev, "lock for reset\n");
243 spin_lock(&ud->lock);
244 ud->status = SDEV_ST_ERROR;
245 spin_unlock(&ud->lock);
246 return;
247 }
248
249
250 ret = usb_reset_device(udev);
251 usb_unlock_device(udev);
252
253 spin_lock(&ud->lock);
254 if (ret) {
255 dev_err(&udev->dev, "device reset\n");
256 ud->status = SDEV_ST_ERROR;
257 } else {
258 dev_info(&udev->dev, "device reset\n");
259 ud->status = SDEV_ST_AVAILABLE;
260 }
261 spin_unlock(&ud->lock);
262}
263
264static void stub_device_unusable(struct usbip_device *ud)
265{
266 spin_lock(&ud->lock);
267 ud->status = SDEV_ST_ERROR;
268 spin_unlock(&ud->lock);
269}
270
271
272
273
274
275
276
277static struct stub_device *stub_device_alloc(struct usb_device *udev,
278 struct usb_interface *interface)
279{
280 struct stub_device *sdev;
281 int busnum = interface_to_busnum(interface);
282 int devnum = interface_to_devnum(interface);
283
284 dev_dbg(&interface->dev, "allocating stub device");
285
286
287 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
288 if (!sdev) {
289 dev_err(&interface->dev, "no memory for stub_device\n");
290 return NULL;
291 }
292
293 sdev->interface = usb_get_intf(interface);
294 sdev->udev = usb_get_dev(udev);
295
296
297
298
299
300
301 sdev->devid = (busnum << 16) | devnum;
302 sdev->ud.side = USBIP_STUB;
303 sdev->ud.status = SDEV_ST_AVAILABLE;
304
305 spin_lock_init(&sdev->ud.lock);
306 sdev->ud.tcp_socket = NULL;
307
308 INIT_LIST_HEAD(&sdev->priv_init);
309 INIT_LIST_HEAD(&sdev->priv_tx);
310 INIT_LIST_HEAD(&sdev->priv_free);
311 INIT_LIST_HEAD(&sdev->unlink_free);
312 INIT_LIST_HEAD(&sdev->unlink_tx);
313
314 spin_lock_init(&sdev->priv_lock);
315
316 init_waitqueue_head(&sdev->tx_waitq);
317
318 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
319 sdev->ud.eh_ops.reset = stub_device_reset;
320 sdev->ud.eh_ops.unusable = stub_device_unusable;
321
322 usbip_start_eh(&sdev->ud);
323
324 dev_dbg(&interface->dev, "register new interface\n");
325
326 return sdev;
327}
328
329static int stub_device_free(struct stub_device *sdev)
330{
331 if (!sdev)
332 return -EINVAL;
333
334 kfree(sdev);
335 pr_debug("kfree udev ok\n");
336
337 return 0;
338}
339
340
341
342
343
344
345
346
347
348
349
350static int stub_probe(struct usb_interface *interface,
351 const struct usb_device_id *id)
352{
353 struct usb_device *udev = interface_to_usbdev(interface);
354 struct stub_device *sdev = NULL;
355 const char *udev_busid = dev_name(interface->dev.parent);
356 int err = 0;
357 struct bus_id_priv *busid_priv;
358
359 dev_dbg(&interface->dev, "Enter\n");
360
361
362 busid_priv = get_busid_priv(udev_busid);
363 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
364 (busid_priv->status == STUB_BUSID_OTHER)) {
365 dev_info(&interface->dev, "%s is not in match_busid table... "
366 "skip!\n", udev_busid);
367
368
369
370
371
372
373 return -ENODEV;
374 }
375
376 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
377 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
378 udev_busid);
379 return -ENODEV;
380 }
381
382 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
383 dev_dbg(&udev->dev, "%s is attached on vhci_hcd... skip!\n",
384 udev_busid);
385 return -ENODEV;
386 }
387
388 if (busid_priv->status == STUB_BUSID_ALLOC) {
389 sdev = busid_priv->sdev;
390 if (!sdev)
391 return -ENODEV;
392
393 busid_priv->interf_count++;
394 dev_info(&interface->dev, "usbip-host: register new interface "
395 "(bus %u dev %u ifn %u)\n",
396 udev->bus->busnum, udev->devnum,
397 interface->cur_altsetting->desc.bInterfaceNumber);
398
399
400 usb_set_intfdata(interface, sdev);
401
402 err = stub_add_files(&interface->dev);
403 if (err) {
404 dev_err(&interface->dev, "stub_add_files for %s\n",
405 udev_busid);
406 usb_set_intfdata(interface, NULL);
407 busid_priv->interf_count--;
408 return err;
409 }
410
411 usb_get_intf(interface);
412 return 0;
413 }
414
415
416 sdev = stub_device_alloc(udev, interface);
417 if (!sdev)
418 return -ENOMEM;
419
420 dev_info(&interface->dev, "usbip-host: register new device "
421 "(bus %u dev %u ifn %u)\n", udev->bus->busnum, udev->devnum,
422 interface->cur_altsetting->desc.bInterfaceNumber);
423
424 busid_priv->interf_count = 0;
425 busid_priv->shutdown_busid = 0;
426
427
428 usb_set_intfdata(interface, sdev);
429 busid_priv->interf_count++;
430 busid_priv->sdev = sdev;
431
432 err = stub_add_files(&interface->dev);
433 if (err) {
434 dev_err(&interface->dev, "stub_add_files for %s\n", udev_busid);
435 usb_set_intfdata(interface, NULL);
436 usb_put_intf(interface);
437
438 busid_priv->interf_count = 0;
439 busid_priv->sdev = NULL;
440 stub_device_free(sdev);
441 return err;
442 }
443 busid_priv->status = STUB_BUSID_ALLOC;
444
445 return 0;
446}
447
448static void shutdown_busid(struct bus_id_priv *busid_priv)
449{
450 if (busid_priv->sdev && !busid_priv->shutdown_busid) {
451 busid_priv->shutdown_busid = 1;
452 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
453
454
455 usbip_stop_eh(&busid_priv->sdev->ud);
456 }
457}
458
459
460
461
462
463static void stub_disconnect(struct usb_interface *interface)
464{
465 struct stub_device *sdev;
466 const char *udev_busid = dev_name(interface->dev.parent);
467 struct bus_id_priv *busid_priv;
468
469 dev_dbg(&interface->dev, "Enter\n");
470
471 busid_priv = get_busid_priv(udev_busid);
472 if (!busid_priv) {
473 BUG();
474 return;
475 }
476
477 sdev = usb_get_intfdata(interface);
478
479
480 if (!sdev) {
481 dev_err(&interface->dev, "could not get device");
482
483 return;
484 }
485
486 usb_set_intfdata(interface, NULL);
487
488
489
490
491
492 stub_remove_files(&interface->dev);
493
494
495 if (busid_priv->sdev->ud.eh == current) {
496 busid_priv->interf_count--;
497 return;
498 }
499
500 if (busid_priv->interf_count > 1) {
501 busid_priv->interf_count--;
502 shutdown_busid(busid_priv);
503 usb_put_intf(interface);
504 return;
505 }
506
507 busid_priv->interf_count = 0;
508
509
510 shutdown_busid(busid_priv);
511
512 usb_put_dev(sdev->udev);
513 usb_put_intf(interface);
514
515
516 busid_priv->sdev = NULL;
517 stub_device_free(sdev);
518
519 if (busid_priv->status == STUB_BUSID_ALLOC) {
520 busid_priv->status = STUB_BUSID_ADDED;
521 } else {
522 busid_priv->status = STUB_BUSID_OTHER;
523 del_match_busid((char *)udev_busid);
524 }
525}
526
527
528
529
530
531
532int stub_pre_reset(struct usb_interface *interface)
533{
534 dev_dbg(&interface->dev, "pre_reset\n");
535 return 0;
536}
537
538int stub_post_reset(struct usb_interface *interface)
539{
540 dev_dbg(&interface->dev, "post_reset\n");
541 return 0;
542}
543
544struct usb_driver stub_driver = {
545 .name = "usbip-host",
546 .probe = stub_probe,
547 .disconnect = stub_disconnect,
548 .id_table = stub_table,
549 .pre_reset = stub_pre_reset,
550 .post_reset = stub_post_reset,
551 };
552