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 struct attribute *usbip_attrs[] = {
144 &dev_attr_usbip_status.attr,
145 &dev_attr_usbip_sockfd.attr,
146 &dev_attr_usbip_debug.attr,
149 ATTRIBUTE_GROUPS(usbip);
151 static void stub_shutdown_connection(struct usbip_device *ud)
153 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
156 * When removing an exported device, kernel panic sometimes occurred
157 * and then EIP was sk_wait_data of stub_rx thread. Is this because
158 * sk_wait_data returned though stub_rx thread was already finished by
161 if (ud->tcp_socket) {
162 dev_dbg(&sdev->udev->dev, "shutdown sockfd %d\n", ud->sockfd);
163 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
166 /* 1. stop threads */
168 kthread_stop_put(ud->tcp_rx);
172 kthread_stop_put(ud->tcp_tx);
177 * 2. close the socket
179 * tcp_socket is freed after threads are killed so that usbip_xmit does
180 * not touch NULL socket.
182 if (ud->tcp_socket) {
183 sockfd_put(ud->tcp_socket);
184 ud->tcp_socket = NULL;
188 /* 3. free used data */
189 stub_device_cleanup_urbs(sdev);
191 /* 4. free stub_unlink */
194 struct stub_unlink *unlink, *tmp;
196 spin_lock_irqsave(&sdev->priv_lock, flags);
197 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
198 list_del(&unlink->list);
201 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
203 list_del(&unlink->list);
206 spin_unlock_irqrestore(&sdev->priv_lock, flags);
210 static void stub_device_reset(struct usbip_device *ud)
212 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
213 struct usb_device *udev = sdev->udev;
216 dev_dbg(&udev->dev, "device reset");
218 ret = usb_lock_device_for_reset(udev, NULL);
220 dev_err(&udev->dev, "lock for reset\n");
221 spin_lock_irq(&ud->lock);
222 ud->status = SDEV_ST_ERROR;
223 spin_unlock_irq(&ud->lock);
227 /* try to reset the device */
228 ret = usb_reset_device(udev);
229 usb_unlock_device(udev);
231 spin_lock_irq(&ud->lock);
233 dev_err(&udev->dev, "device reset\n");
234 ud->status = SDEV_ST_ERROR;
236 dev_info(&udev->dev, "device reset\n");
237 ud->status = SDEV_ST_AVAILABLE;
239 spin_unlock_irq(&ud->lock);
242 static void stub_device_unusable(struct usbip_device *ud)
244 spin_lock_irq(&ud->lock);
245 ud->status = SDEV_ST_ERROR;
246 spin_unlock_irq(&ud->lock);
250 * stub_device_alloc - allocate a new stub_device struct
251 * @udev: usb_device of a new device
253 * Allocates and initializes a new stub_device struct.
255 static struct stub_device *stub_device_alloc(struct usb_device *udev)
257 struct stub_device *sdev;
258 int busnum = udev->bus->busnum;
259 int devnum = udev->devnum;
261 dev_dbg(&udev->dev, "allocating stub device");
263 /* yes, it's a new device */
264 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
268 sdev->udev = usb_get_dev(udev);
271 * devid is defined with devnum when this driver is first allocated.
272 * devnum may change later if a device is reset. However, devid never
273 * changes during a usbip connection.
275 sdev->devid = (busnum << 16) | devnum;
276 sdev->ud.side = USBIP_STUB;
277 sdev->ud.status = SDEV_ST_AVAILABLE;
278 spin_lock_init(&sdev->ud.lock);
279 mutex_init(&sdev->ud.sysfs_lock);
280 sdev->ud.tcp_socket = NULL;
281 sdev->ud.sockfd = -1;
283 INIT_LIST_HEAD(&sdev->priv_init);
284 INIT_LIST_HEAD(&sdev->priv_tx);
285 INIT_LIST_HEAD(&sdev->priv_free);
286 INIT_LIST_HEAD(&sdev->unlink_free);
287 INIT_LIST_HEAD(&sdev->unlink_tx);
288 spin_lock_init(&sdev->priv_lock);
290 init_waitqueue_head(&sdev->tx_waitq);
292 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
293 sdev->ud.eh_ops.reset = stub_device_reset;
294 sdev->ud.eh_ops.unusable = stub_device_unusable;
296 usbip_start_eh(&sdev->ud);
298 dev_dbg(&udev->dev, "register new device\n");
303 static void stub_device_free(struct stub_device *sdev)
308 static int stub_probe(struct usb_device *udev)
310 struct stub_device *sdev = NULL;
311 const char *udev_busid = dev_name(&udev->dev);
312 struct bus_id_priv *busid_priv;
316 dev_dbg(&udev->dev, "Enter probe\n");
318 /* Not sure if this is our device. Allocate here to avoid
319 * calling alloc while holding busid_table lock.
321 sdev = stub_device_alloc(udev);
325 /* check we should claim or not by busid_table */
326 busid_priv = get_busid_priv(udev_busid);
327 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
328 (busid_priv->status == STUB_BUSID_OTHER)) {
330 "%s is not in match_busid table... skip!\n",
334 * Return value should be ENODEV or ENOXIO to continue trying
335 * other matched drivers by the driver core.
336 * See driver_probe_device() in driver/base/dd.c
342 goto call_put_busid_priv;
345 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
346 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
349 goto call_put_busid_priv;
352 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
354 "%s is attached on vhci_hcd... skip!\n",
358 goto call_put_busid_priv;
363 "usbip-host: register new device (bus %u dev %u)\n",
364 udev->bus->busnum, udev->devnum);
366 busid_priv->shutdown_busid = 0;
368 /* set private data to usb_device */
369 dev_set_drvdata(&udev->dev, sdev);
371 busid_priv->sdev = sdev;
372 busid_priv->udev = udev;
374 save_status = busid_priv->status;
375 busid_priv->status = STUB_BUSID_ALLOC;
377 /* release the busid_lock */
378 put_busid_priv(busid_priv);
381 * Claim this hub port.
382 * It doesn't matter what value we pass as owner
383 * (struct dev_state) as long as it is unique.
385 rc = usb_hub_claim_port(udev->parent, udev->portnum,
386 (struct usb_dev_state *) udev);
388 dev_dbg(&udev->dev, "unable to claim port\n");
395 dev_set_drvdata(&udev->dev, NULL);
397 /* we already have busid_priv, just lock busid_lock */
398 spin_lock(&busid_priv->busid_lock);
399 busid_priv->sdev = NULL;
400 busid_priv->status = save_status;
401 spin_unlock(&busid_priv->busid_lock);
402 /* lock is released - go to free */
406 /* release the busid_lock */
407 put_busid_priv(busid_priv);
411 stub_device_free(sdev);
416 static void shutdown_busid(struct bus_id_priv *busid_priv)
418 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
420 /* wait for the stop of the event handler */
421 usbip_stop_eh(&busid_priv->sdev->ud);
425 * called in usb_disconnect() or usb_deregister()
426 * but only if actconfig(active configuration) exists
428 static void stub_disconnect(struct usb_device *udev)
430 struct stub_device *sdev;
431 const char *udev_busid = dev_name(&udev->dev);
432 struct bus_id_priv *busid_priv;
435 dev_dbg(&udev->dev, "Enter disconnect\n");
437 busid_priv = get_busid_priv(udev_busid);
443 sdev = dev_get_drvdata(&udev->dev);
445 /* get stub_device */
447 dev_err(&udev->dev, "could not get device");
448 /* release busid_lock */
449 put_busid_priv(busid_priv);
453 dev_set_drvdata(&udev->dev, NULL);
455 /* release busid_lock before call to remove device files */
456 put_busid_priv(busid_priv);
459 * NOTE: rx/tx threads are invoked for each usb_device.
463 rc = usb_hub_release_port(udev->parent, udev->portnum,
464 (struct usb_dev_state *) udev);
466 dev_dbg(&udev->dev, "unable to release port\n");
470 /* If usb reset is called from event handler */
471 if (usbip_in_eh(current))
474 /* we already have busid_priv, just lock busid_lock */
475 spin_lock(&busid_priv->busid_lock);
476 if (!busid_priv->shutdown_busid)
477 busid_priv->shutdown_busid = 1;
478 /* release busid_lock */
479 spin_unlock(&busid_priv->busid_lock);
481 /* shutdown the current connection */
482 shutdown_busid(busid_priv);
484 usb_put_dev(sdev->udev);
486 /* we already have busid_priv, just lock busid_lock */
487 spin_lock(&busid_priv->busid_lock);
489 busid_priv->sdev = NULL;
490 stub_device_free(sdev);
492 if (busid_priv->status == STUB_BUSID_ALLOC)
493 busid_priv->status = STUB_BUSID_ADDED;
494 /* release busid_lock */
495 spin_unlock(&busid_priv->busid_lock);
501 /* These functions need usb_port_suspend and usb_port_resume,
502 * which reside in drivers/usb/core/usb.h. Skip for now. */
504 static int stub_suspend(struct usb_device *udev, pm_message_t message)
506 dev_dbg(&udev->dev, "stub_suspend\n");
511 static int stub_resume(struct usb_device *udev, pm_message_t message)
513 dev_dbg(&udev->dev, "stub_resume\n");
518 #endif /* CONFIG_PM */
520 struct usb_device_driver stub_driver = {
521 .name = "usbip-host",
523 .disconnect = stub_disconnect,
525 .suspend = stub_suspend,
526 .resume = stub_resume,
528 .supports_autosuspend = 0,
529 .dev_groups = usbip_groups,