diff options
author | Valentina Manea <valentina.manea.m@gmail.com> | 2014-08-20 00:31:00 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2014-08-25 13:40:06 -0400 |
commit | 96c2737716d586a218bc795fcb79d2e2b6003081 (patch) | |
tree | d2cad517eece7a5ee6255e028f3263c2e0b1e23c /drivers/usb/usbip/stub_dev.c | |
parent | 588b48caf65c4a92af567948ec0025065e749ddf (diff) |
usbip: move usbip kernel code out of staging
At this point, USB/IP kernel code is fully functional
and can be moved out of staging.
Signed-off-by: Valentina Manea <valentina.manea.m@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/usb/usbip/stub_dev.c')
-rw-r--r-- | drivers/usb/usbip/stub_dev.c | 525 |
1 files changed, 525 insertions, 0 deletions
diff --git a/drivers/usb/usbip/stub_dev.c b/drivers/usb/usbip/stub_dev.c new file mode 100644 index 000000000000..51d0c7188738 --- /dev/null +++ b/drivers/usb/usbip/stub_dev.c | |||
@@ -0,0 +1,525 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2003-2008 Takahiro Hirofuchi | ||
3 | * | ||
4 | * This is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the Free Software | ||
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | ||
17 | * USA. | ||
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 | * Define device IDs here if you want to explicitly limit exportable devices. | ||
30 | * In most cases, wildcard matching will be okay because driver binding can be | ||
31 | * changed dynamically by a userland program. | ||
32 | */ | ||
33 | static struct usb_device_id stub_table[] = { | ||
34 | #if 0 | ||
35 | /* just an example */ | ||
36 | { USB_DEVICE(0x05ac, 0x0301) }, /* Mac 1 button mouse */ | ||
37 | { USB_DEVICE(0x0430, 0x0009) }, /* Plat Home Keyboard */ | ||
38 | { USB_DEVICE(0x059b, 0x0001) }, /* Iomega USB Zip 100 */ | ||
39 | { USB_DEVICE(0x04b3, 0x4427) }, /* IBM USB CD-ROM */ | ||
40 | { USB_DEVICE(0x05a9, 0xa511) }, /* LifeView USB cam */ | ||
41 | { USB_DEVICE(0x55aa, 0x0201) }, /* Imation card reader */ | ||
42 | { USB_DEVICE(0x046d, 0x0870) }, /* Qcam Express(QV-30) */ | ||
43 | { USB_DEVICE(0x04bb, 0x0101) }, /* IO-DATA HD 120GB */ | ||
44 | { USB_DEVICE(0x04bb, 0x0904) }, /* IO-DATA USB-ET/TX */ | ||
45 | { USB_DEVICE(0x04bb, 0x0201) }, /* IO-DATA USB-ET/TX */ | ||
46 | { USB_DEVICE(0x08bb, 0x2702) }, /* ONKYO USB Speaker */ | ||
47 | { USB_DEVICE(0x046d, 0x08b2) }, /* Logicool Qcam 4000 Pro */ | ||
48 | #endif | ||
49 | /* magic for wild card */ | ||
50 | { .driver_info = 1 }, | ||
51 | { 0, } /* Terminating entry */ | ||
52 | }; | ||
53 | MODULE_DEVICE_TABLE(usb, stub_table); | ||
54 | |||
55 | /* | ||
56 | * usbip_status shows the status of usbip-host as long as this driver is bound | ||
57 | * to the target device. | ||
58 | */ | ||
59 | static ssize_t usbip_status_show(struct device *dev, | ||
60 | struct device_attribute *attr, 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 | } | ||
76 | static DEVICE_ATTR_RO(usbip_status); | ||
77 | |||
78 | /* | ||
79 | * usbip_sockfd gets a socket descriptor of an established TCP connection that | ||
80 | * is used to transfer usbip requests by kernel threads. -1 is a magic number | ||
81 | * by which usbip connection is finished. | ||
82 | */ | ||
83 | static 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 | int rv; | ||
90 | |||
91 | if (!sdev) { | ||
92 | dev_err(dev, "sdev is null\n"); | ||
93 | return -ENODEV; | ||
94 | } | ||
95 | |||
96 | rv = sscanf(buf, "%d", &sockfd); | ||
97 | if (rv != 1) | ||
98 | return -EINVAL; | ||
99 | |||
100 | if (sockfd != -1) { | ||
101 | int err; | ||
102 | |||
103 | dev_info(dev, "stub up\n"); | ||
104 | |||
105 | spin_lock_irq(&sdev->ud.lock); | ||
106 | |||
107 | if (sdev->ud.status != SDEV_ST_AVAILABLE) { | ||
108 | dev_err(dev, "not ready\n"); | ||
109 | goto err; | ||
110 | } | ||
111 | |||
112 | socket = sockfd_lookup(sockfd, &err); | ||
113 | if (!socket) | ||
114 | goto err; | ||
115 | |||
116 | sdev->ud.tcp_socket = socket; | ||
117 | |||
118 | spin_unlock_irq(&sdev->ud.lock); | ||
119 | |||
120 | sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud, | ||
121 | "stub_rx"); | ||
122 | sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud, | ||
123 | "stub_tx"); | ||
124 | |||
125 | spin_lock_irq(&sdev->ud.lock); | ||
126 | sdev->ud.status = SDEV_ST_USED; | ||
127 | spin_unlock_irq(&sdev->ud.lock); | ||
128 | |||
129 | } else { | ||
130 | dev_info(dev, "stub down\n"); | ||
131 | |||
132 | spin_lock_irq(&sdev->ud.lock); | ||
133 | if (sdev->ud.status != SDEV_ST_USED) | ||
134 | goto err; | ||
135 | |||
136 | spin_unlock_irq(&sdev->ud.lock); | ||
137 | |||
138 | usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN); | ||
139 | } | ||
140 | |||
141 | return count; | ||
142 | |||
143 | err: | ||
144 | spin_unlock_irq(&sdev->ud.lock); | ||
145 | return -EINVAL; | ||
146 | } | ||
147 | static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd); | ||
148 | |||
149 | static int stub_add_files(struct device *dev) | ||
150 | { | ||
151 | int err = 0; | ||
152 | |||
153 | err = device_create_file(dev, &dev_attr_usbip_status); | ||
154 | if (err) | ||
155 | goto err_status; | ||
156 | |||
157 | err = device_create_file(dev, &dev_attr_usbip_sockfd); | ||
158 | if (err) | ||
159 | goto err_sockfd; | ||
160 | |||
161 | err = device_create_file(dev, &dev_attr_usbip_debug); | ||
162 | if (err) | ||
163 | goto err_debug; | ||
164 | |||
165 | return 0; | ||
166 | |||
167 | err_debug: | ||
168 | device_remove_file(dev, &dev_attr_usbip_sockfd); | ||
169 | err_sockfd: | ||
170 | device_remove_file(dev, &dev_attr_usbip_status); | ||
171 | err_status: | ||
172 | return err; | ||
173 | } | ||
174 | |||
175 | static void stub_remove_files(struct device *dev) | ||
176 | { | ||
177 | device_remove_file(dev, &dev_attr_usbip_status); | ||
178 | device_remove_file(dev, &dev_attr_usbip_sockfd); | ||
179 | device_remove_file(dev, &dev_attr_usbip_debug); | ||
180 | } | ||
181 | |||
182 | static void stub_shutdown_connection(struct usbip_device *ud) | ||
183 | { | ||
184 | struct stub_device *sdev = container_of(ud, struct stub_device, ud); | ||
185 | |||
186 | /* | ||
187 | * When removing an exported device, kernel panic sometimes occurred | ||
188 | * and then EIP was sk_wait_data of stub_rx thread. Is this because | ||
189 | * sk_wait_data returned though stub_rx thread was already finished by | ||
190 | * step 1? | ||
191 | */ | ||
192 | if (ud->tcp_socket) { | ||
193 | dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n", | ||
194 | ud->tcp_socket); | ||
195 | kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR); | ||
196 | } | ||
197 | |||
198 | /* 1. stop threads */ | ||
199 | if (ud->tcp_rx) { | ||
200 | kthread_stop_put(ud->tcp_rx); | ||
201 | ud->tcp_rx = NULL; | ||
202 | } | ||
203 | if (ud->tcp_tx) { | ||
204 | kthread_stop_put(ud->tcp_tx); | ||
205 | ud->tcp_tx = NULL; | ||
206 | } | ||
207 | |||
208 | /* | ||
209 | * 2. close the socket | ||
210 | * | ||
211 | * tcp_socket is freed after threads are killed so that usbip_xmit does | ||
212 | * not touch NULL socket. | ||
213 | */ | ||
214 | if (ud->tcp_socket) { | ||
215 | sockfd_put(ud->tcp_socket); | ||
216 | ud->tcp_socket = NULL; | ||
217 | } | ||
218 | |||
219 | /* 3. free used data */ | ||
220 | stub_device_cleanup_urbs(sdev); | ||
221 | |||
222 | /* 4. free stub_unlink */ | ||
223 | { | ||
224 | unsigned long flags; | ||
225 | struct stub_unlink *unlink, *tmp; | ||
226 | |||
227 | spin_lock_irqsave(&sdev->priv_lock, flags); | ||
228 | list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) { | ||
229 | list_del(&unlink->list); | ||
230 | kfree(unlink); | ||
231 | } | ||
232 | list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, | ||
233 | list) { | ||
234 | list_del(&unlink->list); | ||
235 | kfree(unlink); | ||
236 | } | ||
237 | spin_unlock_irqrestore(&sdev->priv_lock, flags); | ||
238 | } | ||
239 | } | ||
240 | |||
241 | static void stub_device_reset(struct usbip_device *ud) | ||
242 | { | ||
243 | struct stub_device *sdev = container_of(ud, struct stub_device, ud); | ||
244 | struct usb_device *udev = sdev->udev; | ||
245 | int ret; | ||
246 | |||
247 | dev_dbg(&udev->dev, "device reset"); | ||
248 | |||
249 | ret = usb_lock_device_for_reset(udev, sdev->interface); | ||
250 | if (ret < 0) { | ||
251 | dev_err(&udev->dev, "lock for reset\n"); | ||
252 | spin_lock_irq(&ud->lock); | ||
253 | ud->status = SDEV_ST_ERROR; | ||
254 | spin_unlock_irq(&ud->lock); | ||
255 | return; | ||
256 | } | ||
257 | |||
258 | /* try to reset the device */ | ||
259 | ret = usb_reset_device(udev); | ||
260 | usb_unlock_device(udev); | ||
261 | |||
262 | spin_lock_irq(&ud->lock); | ||
263 | if (ret) { | ||
264 | dev_err(&udev->dev, "device reset\n"); | ||
265 | ud->status = SDEV_ST_ERROR; | ||
266 | } else { | ||
267 | dev_info(&udev->dev, "device reset\n"); | ||
268 | ud->status = SDEV_ST_AVAILABLE; | ||
269 | } | ||
270 | spin_unlock_irq(&ud->lock); | ||
271 | } | ||
272 | |||
273 | static void stub_device_unusable(struct usbip_device *ud) | ||
274 | { | ||
275 | spin_lock_irq(&ud->lock); | ||
276 | ud->status = SDEV_ST_ERROR; | ||
277 | spin_unlock_irq(&ud->lock); | ||
278 | } | ||
279 | |||
280 | /** | ||
281 | * stub_device_alloc - allocate a new stub_device struct | ||
282 | * @interface: usb_interface of a new device | ||
283 | * | ||
284 | * Allocates and initializes a new stub_device struct. | ||
285 | */ | ||
286 | static struct stub_device *stub_device_alloc(struct usb_device *udev) | ||
287 | { | ||
288 | struct stub_device *sdev; | ||
289 | int busnum = udev->bus->busnum; | ||
290 | int devnum = udev->devnum; | ||
291 | |||
292 | dev_dbg(&udev->dev, "allocating stub device"); | ||
293 | |||
294 | /* yes, it's a new device */ | ||
295 | sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL); | ||
296 | if (!sdev) | ||
297 | return NULL; | ||
298 | |||
299 | sdev->udev = usb_get_dev(udev); | ||
300 | |||
301 | /* | ||
302 | * devid is defined with devnum when this driver is first allocated. | ||
303 | * devnum may change later if a device is reset. However, devid never | ||
304 | * changes during a usbip connection. | ||
305 | */ | ||
306 | sdev->devid = (busnum << 16) | devnum; | ||
307 | sdev->ud.side = USBIP_STUB; | ||
308 | sdev->ud.status = SDEV_ST_AVAILABLE; | ||
309 | spin_lock_init(&sdev->ud.lock); | ||
310 | sdev->ud.tcp_socket = NULL; | ||
311 | |||
312 | INIT_LIST_HEAD(&sdev->priv_init); | ||
313 | INIT_LIST_HEAD(&sdev->priv_tx); | ||
314 | INIT_LIST_HEAD(&sdev->priv_free); | ||
315 | INIT_LIST_HEAD(&sdev->unlink_free); | ||
316 | INIT_LIST_HEAD(&sdev->unlink_tx); | ||
317 | spin_lock_init(&sdev->priv_lock); | ||
318 | |||
319 | init_waitqueue_head(&sdev->tx_waitq); | ||
320 | |||
321 | sdev->ud.eh_ops.shutdown = stub_shutdown_connection; | ||
322 | sdev->ud.eh_ops.reset = stub_device_reset; | ||
323 | sdev->ud.eh_ops.unusable = stub_device_unusable; | ||
324 | |||
325 | usbip_start_eh(&sdev->ud); | ||
326 | |||
327 | dev_dbg(&udev->dev, "register new device\n"); | ||
328 | |||
329 | return sdev; | ||
330 | } | ||
331 | |||
332 | static void stub_device_free(struct stub_device *sdev) | ||
333 | { | ||
334 | kfree(sdev); | ||
335 | } | ||
336 | |||
337 | static int stub_probe(struct usb_device *udev) | ||
338 | { | ||
339 | struct stub_device *sdev = NULL; | ||
340 | const char *udev_busid = dev_name(&udev->dev); | ||
341 | int err = 0; | ||
342 | struct bus_id_priv *busid_priv; | ||
343 | int rc; | ||
344 | |||
345 | dev_dbg(&udev->dev, "Enter\n"); | ||
346 | |||
347 | /* check we should claim or not by busid_table */ | ||
348 | busid_priv = get_busid_priv(udev_busid); | ||
349 | if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) || | ||
350 | (busid_priv->status == STUB_BUSID_OTHER)) { | ||
351 | dev_info(&udev->dev, | ||
352 | "%s is not in match_busid table... skip!\n", | ||
353 | udev_busid); | ||
354 | |||
355 | /* | ||
356 | * Return value should be ENODEV or ENOXIO to continue trying | ||
357 | * other matched drivers by the driver core. | ||
358 | * See driver_probe_device() in driver/base/dd.c | ||
359 | */ | ||
360 | return -ENODEV; | ||
361 | } | ||
362 | |||
363 | if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) { | ||
364 | dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n", | ||
365 | udev_busid); | ||
366 | return -ENODEV; | ||
367 | } | ||
368 | |||
369 | if (!strcmp(udev->bus->bus_name, "vhci_hcd")) { | ||
370 | dev_dbg(&udev->dev, | ||
371 | "%s is attached on vhci_hcd... skip!\n", | ||
372 | udev_busid); | ||
373 | |||
374 | return -ENODEV; | ||
375 | } | ||
376 | |||
377 | /* ok, this is my device */ | ||
378 | sdev = stub_device_alloc(udev); | ||
379 | if (!sdev) | ||
380 | return -ENOMEM; | ||
381 | |||
382 | dev_info(&udev->dev, | ||
383 | "usbip-host: register new device (bus %u dev %u)\n", | ||
384 | udev->bus->busnum, udev->devnum); | ||
385 | |||
386 | busid_priv->shutdown_busid = 0; | ||
387 | |||
388 | /* set private data to usb_device */ | ||
389 | dev_set_drvdata(&udev->dev, sdev); | ||
390 | busid_priv->sdev = sdev; | ||
391 | busid_priv->udev = udev; | ||
392 | |||
393 | /* | ||
394 | * Claim this hub port. | ||
395 | * It doesn't matter what value we pass as owner | ||
396 | * (struct dev_state) as long as it is unique. | ||
397 | */ | ||
398 | rc = usb_hub_claim_port(udev->parent, udev->portnum, | ||
399 | (struct usb_dev_state *) udev); | ||
400 | if (rc) { | ||
401 | dev_dbg(&udev->dev, "unable to claim port\n"); | ||
402 | return rc; | ||
403 | } | ||
404 | |||
405 | err = stub_add_files(&udev->dev); | ||
406 | if (err) { | ||
407 | dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid); | ||
408 | dev_set_drvdata(&udev->dev, NULL); | ||
409 | usb_put_dev(udev); | ||
410 | kthread_stop_put(sdev->ud.eh); | ||
411 | |||
412 | busid_priv->sdev = NULL; | ||
413 | stub_device_free(sdev); | ||
414 | return err; | ||
415 | } | ||
416 | busid_priv->status = STUB_BUSID_ALLOC; | ||
417 | |||
418 | return 0; | ||
419 | } | ||
420 | |||
421 | static void shutdown_busid(struct bus_id_priv *busid_priv) | ||
422 | { | ||
423 | if (busid_priv->sdev && !busid_priv->shutdown_busid) { | ||
424 | busid_priv->shutdown_busid = 1; | ||
425 | usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED); | ||
426 | |||
427 | /* wait for the stop of the event handler */ | ||
428 | usbip_stop_eh(&busid_priv->sdev->ud); | ||
429 | } | ||
430 | } | ||
431 | |||
432 | /* | ||
433 | * called in usb_disconnect() or usb_deregister() | ||
434 | * but only if actconfig(active configuration) exists | ||
435 | */ | ||
436 | static void stub_disconnect(struct usb_device *udev) | ||
437 | { | ||
438 | struct stub_device *sdev; | ||
439 | const char *udev_busid = dev_name(&udev->dev); | ||
440 | struct bus_id_priv *busid_priv; | ||
441 | int rc; | ||
442 | |||
443 | dev_dbg(&udev->dev, "Enter\n"); | ||
444 | |||
445 | busid_priv = get_busid_priv(udev_busid); | ||
446 | if (!busid_priv) { | ||
447 | BUG(); | ||
448 | return; | ||
449 | } | ||
450 | |||
451 | sdev = dev_get_drvdata(&udev->dev); | ||
452 | |||
453 | /* get stub_device */ | ||
454 | if (!sdev) { | ||
455 | dev_err(&udev->dev, "could not get device"); | ||
456 | return; | ||
457 | } | ||
458 | |||
459 | dev_set_drvdata(&udev->dev, NULL); | ||
460 | |||
461 | /* | ||
462 | * NOTE: rx/tx threads are invoked for each usb_device. | ||
463 | */ | ||
464 | stub_remove_files(&udev->dev); | ||
465 | |||
466 | /* release port */ | ||
467 | rc = usb_hub_release_port(udev->parent, udev->portnum, | ||
468 | (struct usb_dev_state *) udev); | ||
469 | if (rc) { | ||
470 | dev_dbg(&udev->dev, "unable to release port\n"); | ||
471 | return; | ||
472 | } | ||
473 | |||
474 | /* If usb reset is called from event handler */ | ||
475 | if (busid_priv->sdev->ud.eh == current) | ||
476 | return; | ||
477 | |||
478 | /* shutdown the current connection */ | ||
479 | shutdown_busid(busid_priv); | ||
480 | |||
481 | usb_put_dev(sdev->udev); | ||
482 | |||
483 | /* free sdev */ | ||
484 | busid_priv->sdev = NULL; | ||
485 | stub_device_free(sdev); | ||
486 | |||
487 | if (busid_priv->status == STUB_BUSID_ALLOC) { | ||
488 | busid_priv->status = STUB_BUSID_ADDED; | ||
489 | } else { | ||
490 | busid_priv->status = STUB_BUSID_OTHER; | ||
491 | del_match_busid((char *)udev_busid); | ||
492 | } | ||
493 | } | ||
494 | |||
495 | #ifdef CONFIG_PM | ||
496 | |||
497 | /* These functions need usb_port_suspend and usb_port_resume, | ||
498 | * which reside in drivers/usb/core/usb.h. Skip for now. */ | ||
499 | |||
500 | static int stub_suspend(struct usb_device *udev, pm_message_t message) | ||
501 | { | ||
502 | dev_dbg(&udev->dev, "stub_suspend\n"); | ||
503 | |||
504 | return 0; | ||
505 | } | ||
506 | |||
507 | static int stub_resume(struct usb_device *udev, pm_message_t message) | ||
508 | { | ||
509 | dev_dbg(&udev->dev, "stub_resume\n"); | ||
510 | |||
511 | return 0; | ||
512 | } | ||
513 | |||
514 | #endif /* CONFIG_PM */ | ||
515 | |||
516 | struct usb_device_driver stub_driver = { | ||
517 | .name = "usbip-host", | ||
518 | .probe = stub_probe, | ||
519 | .disconnect = stub_disconnect, | ||
520 | #ifdef CONFIG_PM | ||
521 | .suspend = stub_suspend, | ||
522 | .resume = stub_resume, | ||
523 | #endif | ||
524 | .supports_autosuspend = 0, | ||
525 | }; | ||