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