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
;
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_WO(usbip_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 sockfd %d\n", ud
->sockfd
);
153 kernel_sock_shutdown(ud
->tcp_socket
, SHUT_RDWR
);
156 /* 1. stop threads */
158 kthread_stop_put(ud
->tcp_rx
);
162 kthread_stop_put(ud
->tcp_tx
);
167 * 2. close the socket
169 * tcp_socket is freed after threads are killed so that usbip_xmit does
170 * not touch NULL socket.
172 if (ud
->tcp_socket
) {
173 sockfd_put(ud
->tcp_socket
);
174 ud
->tcp_socket
= NULL
;
177 /* 3. free used data */
178 stub_device_cleanup_urbs(sdev
);
180 /* 4. free stub_unlink */
183 struct stub_unlink
*unlink
, *tmp
;
185 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
186 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_tx
, list
) {
187 list_del(&unlink
->list
);
190 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_free
,
192 list_del(&unlink
->list
);
195 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
199 static void stub_device_reset(struct usbip_device
*ud
)
201 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
202 struct usb_device
*udev
= sdev
->udev
;
205 dev_dbg(&udev
->dev
, "device reset");
207 ret
= usb_lock_device_for_reset(udev
, NULL
);
209 dev_err(&udev
->dev
, "lock for reset\n");
210 spin_lock_irq(&ud
->lock
);
211 ud
->status
= SDEV_ST_ERROR
;
212 spin_unlock_irq(&ud
->lock
);
216 /* try to reset the device */
217 ret
= usb_reset_device(udev
);
218 usb_unlock_device(udev
);
220 spin_lock_irq(&ud
->lock
);
222 dev_err(&udev
->dev
, "device reset\n");
223 ud
->status
= SDEV_ST_ERROR
;
225 dev_info(&udev
->dev
, "device reset\n");
226 ud
->status
= SDEV_ST_AVAILABLE
;
228 spin_unlock_irq(&ud
->lock
);
231 static void stub_device_unusable(struct usbip_device
*ud
)
233 spin_lock_irq(&ud
->lock
);
234 ud
->status
= SDEV_ST_ERROR
;
235 spin_unlock_irq(&ud
->lock
);
239 * stub_device_alloc - allocate a new stub_device struct
240 * @udev: usb_device of a new device
242 * Allocates and initializes a new stub_device struct.
244 static struct stub_device
*stub_device_alloc(struct usb_device
*udev
)
246 struct stub_device
*sdev
;
247 int busnum
= udev
->bus
->busnum
;
248 int devnum
= udev
->devnum
;
250 dev_dbg(&udev
->dev
, "allocating stub device");
252 /* yes, it's a new device */
253 sdev
= kzalloc(sizeof(struct stub_device
), GFP_KERNEL
);
257 sdev
->udev
= usb_get_dev(udev
);
260 * devid is defined with devnum when this driver is first allocated.
261 * devnum may change later if a device is reset. However, devid never
262 * changes during a usbip connection.
264 sdev
->devid
= (busnum
<< 16) | devnum
;
265 sdev
->ud
.side
= USBIP_STUB
;
266 sdev
->ud
.status
= SDEV_ST_AVAILABLE
;
267 spin_lock_init(&sdev
->ud
.lock
);
268 sdev
->ud
.tcp_socket
= NULL
;
270 INIT_LIST_HEAD(&sdev
->priv_init
);
271 INIT_LIST_HEAD(&sdev
->priv_tx
);
272 INIT_LIST_HEAD(&sdev
->priv_free
);
273 INIT_LIST_HEAD(&sdev
->unlink_free
);
274 INIT_LIST_HEAD(&sdev
->unlink_tx
);
275 spin_lock_init(&sdev
->priv_lock
);
277 init_waitqueue_head(&sdev
->tx_waitq
);
279 sdev
->ud
.eh_ops
.shutdown
= stub_shutdown_connection
;
280 sdev
->ud
.eh_ops
.reset
= stub_device_reset
;
281 sdev
->ud
.eh_ops
.unusable
= stub_device_unusable
;
283 usbip_start_eh(&sdev
->ud
);
285 dev_dbg(&udev
->dev
, "register new device\n");
290 static void stub_device_free(struct stub_device
*sdev
)
295 static int stub_probe(struct usb_device
*udev
)
297 struct stub_device
*sdev
= NULL
;
298 const char *udev_busid
= dev_name(&udev
->dev
);
299 struct bus_id_priv
*busid_priv
;
302 dev_dbg(&udev
->dev
, "Enter\n");
304 /* check we should claim or not by busid_table */
305 busid_priv
= get_busid_priv(udev_busid
);
306 if (!busid_priv
|| (busid_priv
->status
== STUB_BUSID_REMOV
) ||
307 (busid_priv
->status
== STUB_BUSID_OTHER
)) {
309 "%s is not in match_busid table... skip!\n",
313 * Return value should be ENODEV or ENOXIO to continue trying
314 * other matched drivers by the driver core.
315 * See driver_probe_device() in driver/base/dd.c
320 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
) {
321 dev_dbg(&udev
->dev
, "%s is a usb hub device... skip!\n",
326 if (!strcmp(udev
->bus
->bus_name
, "vhci_hcd")) {
328 "%s is attached on vhci_hcd... skip!\n",
334 /* ok, this is my device */
335 sdev
= stub_device_alloc(udev
);
340 "usbip-host: register new device (bus %u dev %u)\n",
341 udev
->bus
->busnum
, udev
->devnum
);
343 busid_priv
->shutdown_busid
= 0;
345 /* set private data to usb_device */
346 dev_set_drvdata(&udev
->dev
, sdev
);
347 busid_priv
->sdev
= sdev
;
348 busid_priv
->udev
= udev
;
351 * Claim this hub port.
352 * It doesn't matter what value we pass as owner
353 * (struct dev_state) as long as it is unique.
355 rc
= usb_hub_claim_port(udev
->parent
, udev
->portnum
,
356 (struct usb_dev_state
*) udev
);
358 dev_dbg(&udev
->dev
, "unable to claim port\n");
362 rc
= stub_add_files(&udev
->dev
);
364 dev_err(&udev
->dev
, "stub_add_files for %s\n", udev_busid
);
367 busid_priv
->status
= STUB_BUSID_ALLOC
;
371 usb_hub_release_port(udev
->parent
, udev
->portnum
,
372 (struct usb_dev_state
*) udev
);
374 dev_set_drvdata(&udev
->dev
, NULL
);
377 busid_priv
->sdev
= NULL
;
378 stub_device_free(sdev
);
382 static void shutdown_busid(struct bus_id_priv
*busid_priv
)
384 if (busid_priv
->sdev
&& !busid_priv
->shutdown_busid
) {
385 busid_priv
->shutdown_busid
= 1;
386 usbip_event_add(&busid_priv
->sdev
->ud
, SDEV_EVENT_REMOVED
);
388 /* wait for the stop of the event handler */
389 usbip_stop_eh(&busid_priv
->sdev
->ud
);
394 * called in usb_disconnect() or usb_deregister()
395 * but only if actconfig(active configuration) exists
397 static void stub_disconnect(struct usb_device
*udev
)
399 struct stub_device
*sdev
;
400 const char *udev_busid
= dev_name(&udev
->dev
);
401 struct bus_id_priv
*busid_priv
;
404 dev_dbg(&udev
->dev
, "Enter\n");
406 busid_priv
= get_busid_priv(udev_busid
);
412 sdev
= dev_get_drvdata(&udev
->dev
);
414 /* get stub_device */
416 dev_err(&udev
->dev
, "could not get device");
420 dev_set_drvdata(&udev
->dev
, NULL
);
423 * NOTE: rx/tx threads are invoked for each usb_device.
425 stub_remove_files(&udev
->dev
);
428 rc
= usb_hub_release_port(udev
->parent
, udev
->portnum
,
429 (struct usb_dev_state
*) udev
);
431 dev_dbg(&udev
->dev
, "unable to release port\n");
435 /* If usb reset is called from event handler */
436 if (usbip_in_eh(current
))
439 /* shutdown the current connection */
440 shutdown_busid(busid_priv
);
442 usb_put_dev(sdev
->udev
);
445 busid_priv
->sdev
= NULL
;
446 stub_device_free(sdev
);
448 if (busid_priv
->status
== STUB_BUSID_ALLOC
) {
449 busid_priv
->status
= STUB_BUSID_ADDED
;
451 busid_priv
->status
= STUB_BUSID_OTHER
;
452 del_match_busid((char *)udev_busid
);
458 /* These functions need usb_port_suspend and usb_port_resume,
459 * which reside in drivers/usb/core/usb.h. Skip for now. */
461 static int stub_suspend(struct usb_device
*udev
, pm_message_t message
)
463 dev_dbg(&udev
->dev
, "stub_suspend\n");
468 static int stub_resume(struct usb_device
*udev
, pm_message_t message
)
470 dev_dbg(&udev
->dev
, "stub_resume\n");
475 #endif /* CONFIG_PM */
477 struct usb_device_driver stub_driver
= {
478 .name
= "usbip-host",
480 .disconnect
= stub_disconnect
,
482 .suspend
= stub_suspend
,
483 .resume
= stub_resume
,
485 .supports_autosuspend
= 0,