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