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 store_sockfd(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;
51 dev_err(dev, "sdev is null\n");
55 rv = sscanf(buf, "%d", &sockfd);
62 dev_info(dev, "stub up\n");
64 spin_lock_irq(&sdev->ud.lock);
66 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
67 dev_err(dev, "not ready\n");
71 socket = sockfd_lookup(sockfd, &err);
75 sdev->ud.tcp_socket = socket;
77 spin_unlock_irq(&sdev->ud.lock);
79 sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud,
81 sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud,
84 spin_lock_irq(&sdev->ud.lock);
85 sdev->ud.status = SDEV_ST_USED;
86 spin_unlock_irq(&sdev->ud.lock);
89 dev_info(dev, "stub down\n");
91 spin_lock_irq(&sdev->ud.lock);
92 if (sdev->ud.status != SDEV_ST_USED)
95 spin_unlock_irq(&sdev->ud.lock);
97 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
103 spin_unlock_irq(&sdev->ud.lock);
106 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
108 static int stub_add_files(struct device *dev)
112 err = device_create_file(dev, &dev_attr_usbip_status);
116 err = device_create_file(dev, &dev_attr_usbip_sockfd);
120 err = device_create_file(dev, &dev_attr_usbip_debug);
127 device_remove_file(dev, &dev_attr_usbip_sockfd);
129 device_remove_file(dev, &dev_attr_usbip_status);
134 static void stub_remove_files(struct device *dev)
136 device_remove_file(dev, &dev_attr_usbip_status);
137 device_remove_file(dev, &dev_attr_usbip_sockfd);
138 device_remove_file(dev, &dev_attr_usbip_debug);
141 static void stub_shutdown_connection(struct usbip_device *ud)
143 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
146 * When removing an exported device, kernel panic sometimes occurred
147 * and then EIP was sk_wait_data of stub_rx thread. Is this because
148 * sk_wait_data returned though stub_rx thread was already finished by
151 if (ud->tcp_socket) {
152 dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n",
154 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
157 /* 1. stop threads */
159 kthread_stop_put(ud->tcp_rx);
163 kthread_stop_put(ud->tcp_tx);
168 * 2. close the socket
170 * tcp_socket is freed after threads are killed so that usbip_xmit does
171 * not touch NULL socket.
173 if (ud->tcp_socket) {
174 sockfd_put(ud->tcp_socket);
175 ud->tcp_socket = NULL;
178 /* 3. free used data */
179 stub_device_cleanup_urbs(sdev);
181 /* 4. free stub_unlink */
184 struct stub_unlink *unlink, *tmp;
186 spin_lock_irqsave(&sdev->priv_lock, flags);
187 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
188 list_del(&unlink->list);
191 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
193 list_del(&unlink->list);
196 spin_unlock_irqrestore(&sdev->priv_lock, flags);
200 static void stub_device_reset(struct usbip_device *ud)
202 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
203 struct usb_device *udev = sdev->udev;
206 dev_dbg(&udev->dev, "device reset");
208 ret = usb_lock_device_for_reset(udev, NULL);
210 dev_err(&udev->dev, "lock for reset\n");
211 spin_lock_irq(&ud->lock);
212 ud->status = SDEV_ST_ERROR;
213 spin_unlock_irq(&ud->lock);
217 /* try to reset the device */
218 ret = usb_reset_device(udev);
219 usb_unlock_device(udev);
221 spin_lock_irq(&ud->lock);
223 dev_err(&udev->dev, "device reset\n");
224 ud->status = SDEV_ST_ERROR;
226 dev_info(&udev->dev, "device reset\n");
227 ud->status = SDEV_ST_AVAILABLE;
229 spin_unlock_irq(&ud->lock);
232 static void stub_device_unusable(struct usbip_device *ud)
234 spin_lock_irq(&ud->lock);
235 ud->status = SDEV_ST_ERROR;
236 spin_unlock_irq(&ud->lock);
240 * stub_device_alloc - allocate a new stub_device struct
241 * @udev: usb_device of a new device
243 * Allocates and initializes a new stub_device struct.
245 static struct stub_device *stub_device_alloc(struct usb_device *udev)
247 struct stub_device *sdev;
248 int busnum = udev->bus->busnum;
249 int devnum = udev->devnum;
251 dev_dbg(&udev->dev, "allocating stub device");
253 /* yes, it's a new device */
254 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
258 sdev->udev = usb_get_dev(udev);
261 * devid is defined with devnum when this driver is first allocated.
262 * devnum may change later if a device is reset. However, devid never
263 * changes during a usbip connection.
265 sdev->devid = (busnum << 16) | devnum;
266 sdev->ud.side = USBIP_STUB;
267 sdev->ud.status = SDEV_ST_AVAILABLE;
268 spin_lock_init(&sdev->ud.lock);
269 sdev->ud.tcp_socket = NULL;
271 INIT_LIST_HEAD(&sdev->priv_init);
272 INIT_LIST_HEAD(&sdev->priv_tx);
273 INIT_LIST_HEAD(&sdev->priv_free);
274 INIT_LIST_HEAD(&sdev->unlink_free);
275 INIT_LIST_HEAD(&sdev->unlink_tx);
276 spin_lock_init(&sdev->priv_lock);
278 init_waitqueue_head(&sdev->tx_waitq);
280 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
281 sdev->ud.eh_ops.reset = stub_device_reset;
282 sdev->ud.eh_ops.unusable = stub_device_unusable;
284 usbip_start_eh(&sdev->ud);
286 dev_dbg(&udev->dev, "register new device\n");
291 static void stub_device_free(struct stub_device *sdev)
296 static int stub_probe(struct usb_device *udev)
298 struct stub_device *sdev = NULL;
299 const char *udev_busid = dev_name(&udev->dev);
300 struct bus_id_priv *busid_priv;
303 dev_dbg(&udev->dev, "Enter\n");
305 /* check we should claim or not by busid_table */
306 busid_priv = get_busid_priv(udev_busid);
307 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
308 (busid_priv->status == STUB_BUSID_OTHER)) {
310 "%s is not in match_busid table... skip!\n",
314 * Return value should be ENODEV or ENOXIO to continue trying
315 * other matched drivers by the driver core.
316 * See driver_probe_device() in driver/base/dd.c
321 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
322 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
327 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
329 "%s is attached on vhci_hcd... skip!\n",
335 /* ok, this is my device */
336 sdev = stub_device_alloc(udev);
341 "usbip-host: register new device (bus %u dev %u)\n",
342 udev->bus->busnum, udev->devnum);
344 busid_priv->shutdown_busid = 0;
346 /* set private data to usb_device */
347 dev_set_drvdata(&udev->dev, sdev);
348 busid_priv->sdev = sdev;
349 busid_priv->udev = udev;
352 * Claim this hub port.
353 * It doesn't matter what value we pass as owner
354 * (struct dev_state) as long as it is unique.
356 rc = usb_hub_claim_port(udev->parent, udev->portnum,
357 (struct usb_dev_state *) udev);
359 dev_dbg(&udev->dev, "unable to claim port\n");
363 rc = stub_add_files(&udev->dev);
365 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
368 busid_priv->status = STUB_BUSID_ALLOC;
372 usb_hub_release_port(udev->parent, udev->portnum,
373 (struct usb_dev_state *) udev);
375 dev_set_drvdata(&udev->dev, NULL);
378 busid_priv->sdev = NULL;
379 stub_device_free(sdev);
383 static void shutdown_busid(struct bus_id_priv *busid_priv)
385 if (busid_priv->sdev && !busid_priv->shutdown_busid) {
386 busid_priv->shutdown_busid = 1;
387 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
389 /* wait for the stop of the event handler */
390 usbip_stop_eh(&busid_priv->sdev->ud);
395 * called in usb_disconnect() or usb_deregister()
396 * but only if actconfig(active configuration) exists
398 static void stub_disconnect(struct usb_device *udev)
400 struct stub_device *sdev;
401 const char *udev_busid = dev_name(&udev->dev);
402 struct bus_id_priv *busid_priv;
405 dev_dbg(&udev->dev, "Enter\n");
407 busid_priv = get_busid_priv(udev_busid);
413 sdev = dev_get_drvdata(&udev->dev);
415 /* get stub_device */
417 dev_err(&udev->dev, "could not get device");
421 dev_set_drvdata(&udev->dev, NULL);
424 * NOTE: rx/tx threads are invoked for each usb_device.
426 stub_remove_files(&udev->dev);
429 rc = usb_hub_release_port(udev->parent, udev->portnum,
430 (struct usb_dev_state *) udev);
432 dev_dbg(&udev->dev, "unable to release port\n");
436 /* If usb reset is called from event handler */
437 if (usbip_in_eh(current))
440 /* shutdown the current connection */
441 shutdown_busid(busid_priv);
443 usb_put_dev(sdev->udev);
446 busid_priv->sdev = NULL;
447 stub_device_free(sdev);
449 if (busid_priv->status == STUB_BUSID_ALLOC) {
450 busid_priv->status = STUB_BUSID_ADDED;
452 busid_priv->status = STUB_BUSID_OTHER;
453 del_match_busid((char *)udev_busid);
459 /* These functions need usb_port_suspend and usb_port_resume,
460 * which reside in drivers/usb/core/usb.h. Skip for now. */
462 static int stub_suspend(struct usb_device *udev, pm_message_t message)
464 dev_dbg(&udev->dev, "stub_suspend\n");
469 static int stub_resume(struct usb_device *udev, pm_message_t message)
471 dev_dbg(&udev->dev, "stub_resume\n");
476 #endif /* CONFIG_PM */
478 struct usb_device_driver stub_driver = {
479 .name = "usbip-host",
481 .disconnect = stub_disconnect,
483 .suspend = stub_suspend,
484 .resume = stub_resume,
486 .supports_autosuspend = 0,