1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
6 #include <linux/device.h>
7 #include <linux/file.h>
8 #include <linux/kthread.h>
9 #include <linux/module.h>
11 #include "usbip_common.h"
15 * usbip_status shows the status of usbip-host as long as this driver is bound
16 * to the target device.
18 static ssize_t usbip_status_show(struct device *dev,
19 struct device_attribute *attr, char *buf)
21 struct stub_device *sdev = dev_get_drvdata(dev);
25 dev_err(dev, "sdev is null\n");
29 spin_lock_irq(&sdev->ud.lock);
30 status = sdev->ud.status;
31 spin_unlock_irq(&sdev->ud.lock);
33 return snprintf(buf, PAGE_SIZE, "%d\n", status);
35 static DEVICE_ATTR_RO(usbip_status);
38 * usbip_sockfd gets a socket descriptor of an established TCP connection that
39 * is used to transfer usbip requests by kernel threads. -1 is a magic number
40 * by which usbip connection is finished.
42 static ssize_t usbip_sockfd_store(struct device *dev, struct device_attribute *attr,
43 const char *buf, size_t count)
45 struct stub_device *sdev = dev_get_drvdata(dev);
47 struct socket *socket;
49 struct task_struct *tcp_rx = NULL;
50 struct task_struct *tcp_tx = NULL;
53 dev_err(dev, "sdev is null\n");
57 rv = sscanf(buf, "%d", &sockfd);
64 dev_info(dev, "stub up\n");
66 mutex_lock(&sdev->ud.sysfs_lock);
67 spin_lock_irq(&sdev->ud.lock);
69 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
70 dev_err(dev, "not ready\n");
74 socket = sockfd_lookup(sockfd, &err);
76 dev_err(dev, "failed to lookup sock");
80 if (socket->type != SOCK_STREAM) {
81 dev_err(dev, "Expecting SOCK_STREAM - found %d",
86 /* unlock and create threads and get tasks */
87 spin_unlock_irq(&sdev->ud.lock);
88 tcp_rx = kthread_create(stub_rx_loop, &sdev->ud, "stub_rx");
93 tcp_tx = kthread_create(stub_tx_loop, &sdev->ud, "stub_tx");
100 /* get task structs now */
101 get_task_struct(tcp_rx);
102 get_task_struct(tcp_tx);
104 /* lock and update sdev->ud state */
105 spin_lock_irq(&sdev->ud.lock);
106 sdev->ud.tcp_socket = socket;
107 sdev->ud.sockfd = sockfd;
108 sdev->ud.tcp_rx = tcp_rx;
109 sdev->ud.tcp_tx = tcp_tx;
110 sdev->ud.status = SDEV_ST_USED;
111 spin_unlock_irq(&sdev->ud.lock);
113 wake_up_process(sdev->ud.tcp_rx);
114 wake_up_process(sdev->ud.tcp_tx);
116 mutex_unlock(&sdev->ud.sysfs_lock);
119 dev_info(dev, "stub down\n");
121 spin_lock_irq(&sdev->ud.lock);
122 if (sdev->ud.status != SDEV_ST_USED)
125 spin_unlock_irq(&sdev->ud.lock);
127 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
128 mutex_unlock(&sdev->ud.sysfs_lock);
136 spin_unlock_irq(&sdev->ud.lock);
138 mutex_unlock(&sdev->ud.sysfs_lock);
141 static DEVICE_ATTR_WO(usbip_sockfd);
143 static int stub_add_files(struct device *dev)
147 err = device_create_file(dev, &dev_attr_usbip_status);
151 err = device_create_file(dev, &dev_attr_usbip_sockfd);
155 err = device_create_file(dev, &dev_attr_usbip_debug);
162 device_remove_file(dev, &dev_attr_usbip_sockfd);
164 device_remove_file(dev, &dev_attr_usbip_status);
169 static void stub_remove_files(struct device *dev)
171 device_remove_file(dev, &dev_attr_usbip_status);
172 device_remove_file(dev, &dev_attr_usbip_sockfd);
173 device_remove_file(dev, &dev_attr_usbip_debug);
176 static void stub_shutdown_connection(struct usbip_device *ud)
178 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
181 * When removing an exported device, kernel panic sometimes occurred
182 * and then EIP was sk_wait_data of stub_rx thread. Is this because
183 * sk_wait_data returned though stub_rx thread was already finished by
186 if (ud->tcp_socket) {
187 dev_dbg(&sdev->udev->dev, "shutdown sockfd %d\n", ud->sockfd);
188 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
191 /* 1. stop threads */
193 kthread_stop_put(ud->tcp_rx);
197 kthread_stop_put(ud->tcp_tx);
202 * 2. close the socket
204 * tcp_socket is freed after threads are killed so that usbip_xmit does
205 * not touch NULL socket.
207 if (ud->tcp_socket) {
208 sockfd_put(ud->tcp_socket);
209 ud->tcp_socket = NULL;
213 /* 3. free used data */
214 stub_device_cleanup_urbs(sdev);
216 /* 4. free stub_unlink */
219 struct stub_unlink *unlink, *tmp;
221 spin_lock_irqsave(&sdev->priv_lock, flags);
222 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
223 list_del(&unlink->list);
226 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
228 list_del(&unlink->list);
231 spin_unlock_irqrestore(&sdev->priv_lock, flags);
235 static void stub_device_reset(struct usbip_device *ud)
237 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
238 struct usb_device *udev = sdev->udev;
241 dev_dbg(&udev->dev, "device reset");
243 ret = usb_lock_device_for_reset(udev, NULL);
245 dev_err(&udev->dev, "lock for reset\n");
246 spin_lock_irq(&ud->lock);
247 ud->status = SDEV_ST_ERROR;
248 spin_unlock_irq(&ud->lock);
252 /* try to reset the device */
253 ret = usb_reset_device(udev);
254 usb_unlock_device(udev);
256 spin_lock_irq(&ud->lock);
258 dev_err(&udev->dev, "device reset\n");
259 ud->status = SDEV_ST_ERROR;
261 dev_info(&udev->dev, "device reset\n");
262 ud->status = SDEV_ST_AVAILABLE;
264 spin_unlock_irq(&ud->lock);
267 static void stub_device_unusable(struct usbip_device *ud)
269 spin_lock_irq(&ud->lock);
270 ud->status = SDEV_ST_ERROR;
271 spin_unlock_irq(&ud->lock);
275 * stub_device_alloc - allocate a new stub_device struct
276 * @udev: usb_device of a new device
278 * Allocates and initializes a new stub_device struct.
280 static struct stub_device *stub_device_alloc(struct usb_device *udev)
282 struct stub_device *sdev;
283 int busnum = udev->bus->busnum;
284 int devnum = udev->devnum;
286 dev_dbg(&udev->dev, "allocating stub device");
288 /* yes, it's a new device */
289 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
293 sdev->udev = usb_get_dev(udev);
296 * devid is defined with devnum when this driver is first allocated.
297 * devnum may change later if a device is reset. However, devid never
298 * changes during a usbip connection.
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 mutex_init(&sdev->ud.sysfs_lock);
305 sdev->ud.tcp_socket = NULL;
306 sdev->ud.sockfd = -1;
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 spin_lock_init(&sdev->priv_lock);
315 init_waitqueue_head(&sdev->tx_waitq);
317 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
318 sdev->ud.eh_ops.reset = stub_device_reset;
319 sdev->ud.eh_ops.unusable = stub_device_unusable;
321 usbip_start_eh(&sdev->ud);
323 dev_dbg(&udev->dev, "register new device\n");
328 static void stub_device_free(struct stub_device *sdev)
333 static int stub_probe(struct usb_device *udev)
335 struct stub_device *sdev = NULL;
336 const char *udev_busid = dev_name(&udev->dev);
337 struct bus_id_priv *busid_priv;
341 dev_dbg(&udev->dev, "Enter probe\n");
343 /* Not sure if this is our device. Allocate here to avoid
344 * calling alloc while holding busid_table lock.
346 sdev = stub_device_alloc(udev);
350 /* check we should claim or not by busid_table */
351 busid_priv = get_busid_priv(udev_busid);
352 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
353 (busid_priv->status == STUB_BUSID_OTHER)) {
355 "%s is not in match_busid table... skip!\n",
359 * Return value should be ENODEV or ENOXIO to continue trying
360 * other matched drivers by the driver core.
361 * See driver_probe_device() in driver/base/dd.c
367 goto call_put_busid_priv;
370 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
371 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
374 goto call_put_busid_priv;
377 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
379 "%s is attached on vhci_hcd... skip!\n",
383 goto call_put_busid_priv;
388 "usbip-host: register new device (bus %u dev %u)\n",
389 udev->bus->busnum, udev->devnum);
391 busid_priv->shutdown_busid = 0;
393 /* set private data to usb_device */
394 dev_set_drvdata(&udev->dev, sdev);
396 busid_priv->sdev = sdev;
397 busid_priv->udev = udev;
399 save_status = busid_priv->status;
400 busid_priv->status = STUB_BUSID_ALLOC;
402 /* release the busid_lock */
403 put_busid_priv(busid_priv);
406 * Claim this hub port.
407 * It doesn't matter what value we pass as owner
408 * (struct dev_state) as long as it is unique.
410 rc = usb_hub_claim_port(udev->parent, udev->portnum,
411 (struct usb_dev_state *) udev);
413 dev_dbg(&udev->dev, "unable to claim port\n");
417 rc = stub_add_files(&udev->dev);
419 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
426 usb_hub_release_port(udev->parent, udev->portnum,
427 (struct usb_dev_state *) udev);
429 dev_set_drvdata(&udev->dev, NULL);
431 /* we already have busid_priv, just lock busid_lock */
432 spin_lock(&busid_priv->busid_lock);
433 busid_priv->sdev = NULL;
434 busid_priv->status = save_status;
435 spin_unlock(&busid_priv->busid_lock);
436 /* lock is released - go to free */
440 /* release the busid_lock */
441 put_busid_priv(busid_priv);
445 stub_device_free(sdev);
450 static void shutdown_busid(struct bus_id_priv *busid_priv)
452 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
454 /* wait for the stop of the event handler */
455 usbip_stop_eh(&busid_priv->sdev->ud);
459 * called in usb_disconnect() or usb_deregister()
460 * but only if actconfig(active configuration) exists
462 static void stub_disconnect(struct usb_device *udev)
464 struct stub_device *sdev;
465 const char *udev_busid = dev_name(&udev->dev);
466 struct bus_id_priv *busid_priv;
469 dev_dbg(&udev->dev, "Enter disconnect\n");
471 busid_priv = get_busid_priv(udev_busid);
477 sdev = dev_get_drvdata(&udev->dev);
479 /* get stub_device */
481 dev_err(&udev->dev, "could not get device");
482 /* release busid_lock */
483 put_busid_priv(busid_priv);
487 dev_set_drvdata(&udev->dev, NULL);
489 /* release busid_lock before call to remove device files */
490 put_busid_priv(busid_priv);
493 * NOTE: rx/tx threads are invoked for each usb_device.
495 stub_remove_files(&udev->dev);
498 rc = usb_hub_release_port(udev->parent, udev->portnum,
499 (struct usb_dev_state *) udev);
501 dev_dbg(&udev->dev, "unable to release port\n");
505 /* If usb reset is called from event handler */
506 if (usbip_in_eh(current))
509 /* we already have busid_priv, just lock busid_lock */
510 spin_lock(&busid_priv->busid_lock);
511 if (!busid_priv->shutdown_busid)
512 busid_priv->shutdown_busid = 1;
513 /* release busid_lock */
514 spin_unlock(&busid_priv->busid_lock);
516 /* shutdown the current connection */
517 shutdown_busid(busid_priv);
519 usb_put_dev(sdev->udev);
521 /* we already have busid_priv, just lock busid_lock */
522 spin_lock(&busid_priv->busid_lock);
524 busid_priv->sdev = NULL;
525 stub_device_free(sdev);
527 if (busid_priv->status == STUB_BUSID_ALLOC)
528 busid_priv->status = STUB_BUSID_ADDED;
529 /* release busid_lock */
530 spin_unlock(&busid_priv->busid_lock);
536 /* These functions need usb_port_suspend and usb_port_resume,
537 * which reside in drivers/usb/core/usb.h. Skip for now. */
539 static int stub_suspend(struct usb_device *udev, pm_message_t message)
541 dev_dbg(&udev->dev, "stub_suspend\n");
546 static int stub_resume(struct usb_device *udev, pm_message_t message)
548 dev_dbg(&udev->dev, "stub_resume\n");
553 #endif /* CONFIG_PM */
555 struct usb_device_driver stub_driver = {
556 .name = "usbip-host",
558 .disconnect = stub_disconnect,
560 .suspend = stub_suspend,
561 .resume = stub_resume,
563 .supports_autosuspend = 0,