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
;
76 sdev
->ud
.sockfd
= sockfd
;
78 spin_unlock_irq(&sdev
->ud
.lock
);
80 sdev
->ud
.tcp_rx
= kthread_get_run(stub_rx_loop
, &sdev
->ud
,
82 sdev
->ud
.tcp_tx
= kthread_get_run(stub_tx_loop
, &sdev
->ud
,
85 spin_lock_irq(&sdev
->ud
.lock
);
86 sdev
->ud
.status
= SDEV_ST_USED
;
87 spin_unlock_irq(&sdev
->ud
.lock
);
90 dev_info(dev
, "stub down\n");
92 spin_lock_irq(&sdev
->ud
.lock
);
93 if (sdev
->ud
.status
!= SDEV_ST_USED
)
96 spin_unlock_irq(&sdev
->ud
.lock
);
98 usbip_event_add(&sdev
->ud
, SDEV_EVENT_DOWN
);
104 spin_unlock_irq(&sdev
->ud
.lock
);
107 static DEVICE_ATTR_WO(usbip_sockfd
);
109 static int stub_add_files(struct device
*dev
)
113 err
= device_create_file(dev
, &dev_attr_usbip_status
);
117 err
= device_create_file(dev
, &dev_attr_usbip_sockfd
);
121 err
= device_create_file(dev
, &dev_attr_usbip_debug
);
128 device_remove_file(dev
, &dev_attr_usbip_sockfd
);
130 device_remove_file(dev
, &dev_attr_usbip_status
);
135 static void stub_remove_files(struct device
*dev
)
137 device_remove_file(dev
, &dev_attr_usbip_status
);
138 device_remove_file(dev
, &dev_attr_usbip_sockfd
);
139 device_remove_file(dev
, &dev_attr_usbip_debug
);
142 static void stub_shutdown_connection(struct usbip_device
*ud
)
144 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
147 * When removing an exported device, kernel panic sometimes occurred
148 * and then EIP was sk_wait_data of stub_rx thread. Is this because
149 * sk_wait_data returned though stub_rx thread was already finished by
152 if (ud
->tcp_socket
) {
153 dev_dbg(&sdev
->udev
->dev
, "shutdown sockfd %d\n", ud
->sockfd
);
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
;
179 /* 3. free used data */
180 stub_device_cleanup_urbs(sdev
);
182 /* 4. free stub_unlink */
185 struct stub_unlink
*unlink
, *tmp
;
187 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
188 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_tx
, list
) {
189 list_del(&unlink
->list
);
192 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_free
,
194 list_del(&unlink
->list
);
197 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
201 static void stub_device_reset(struct usbip_device
*ud
)
203 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
204 struct usb_device
*udev
= sdev
->udev
;
207 dev_dbg(&udev
->dev
, "device reset");
209 ret
= usb_lock_device_for_reset(udev
, NULL
);
211 dev_err(&udev
->dev
, "lock for reset\n");
212 spin_lock_irq(&ud
->lock
);
213 ud
->status
= SDEV_ST_ERROR
;
214 spin_unlock_irq(&ud
->lock
);
218 /* try to reset the device */
219 ret
= usb_reset_device(udev
);
220 usb_unlock_device(udev
);
222 spin_lock_irq(&ud
->lock
);
224 dev_err(&udev
->dev
, "device reset\n");
225 ud
->status
= SDEV_ST_ERROR
;
227 dev_info(&udev
->dev
, "device reset\n");
228 ud
->status
= SDEV_ST_AVAILABLE
;
230 spin_unlock_irq(&ud
->lock
);
233 static void stub_device_unusable(struct usbip_device
*ud
)
235 spin_lock_irq(&ud
->lock
);
236 ud
->status
= SDEV_ST_ERROR
;
237 spin_unlock_irq(&ud
->lock
);
241 * stub_device_alloc - allocate a new stub_device struct
242 * @udev: usb_device of a new device
244 * Allocates and initializes a new stub_device struct.
246 static struct stub_device
*stub_device_alloc(struct usb_device
*udev
)
248 struct stub_device
*sdev
;
249 int busnum
= udev
->bus
->busnum
;
250 int devnum
= udev
->devnum
;
252 dev_dbg(&udev
->dev
, "allocating stub device");
254 /* yes, it's a new device */
255 sdev
= kzalloc(sizeof(struct stub_device
), GFP_KERNEL
);
259 sdev
->udev
= usb_get_dev(udev
);
262 * devid is defined with devnum when this driver is first allocated.
263 * devnum may change later if a device is reset. However, devid never
264 * changes during a usbip connection.
266 sdev
->devid
= (busnum
<< 16) | devnum
;
267 sdev
->ud
.side
= USBIP_STUB
;
268 sdev
->ud
.status
= SDEV_ST_AVAILABLE
;
269 spin_lock_init(&sdev
->ud
.lock
);
270 sdev
->ud
.tcp_socket
= NULL
;
271 sdev
->ud
.sockfd
= -1;
273 INIT_LIST_HEAD(&sdev
->priv_init
);
274 INIT_LIST_HEAD(&sdev
->priv_tx
);
275 INIT_LIST_HEAD(&sdev
->priv_free
);
276 INIT_LIST_HEAD(&sdev
->unlink_free
);
277 INIT_LIST_HEAD(&sdev
->unlink_tx
);
278 spin_lock_init(&sdev
->priv_lock
);
280 init_waitqueue_head(&sdev
->tx_waitq
);
282 sdev
->ud
.eh_ops
.shutdown
= stub_shutdown_connection
;
283 sdev
->ud
.eh_ops
.reset
= stub_device_reset
;
284 sdev
->ud
.eh_ops
.unusable
= stub_device_unusable
;
286 usbip_start_eh(&sdev
->ud
);
288 dev_dbg(&udev
->dev
, "register new device\n");
293 static void stub_device_free(struct stub_device
*sdev
)
298 static int stub_probe(struct usb_device
*udev
)
300 struct stub_device
*sdev
= NULL
;
301 const char *udev_busid
= dev_name(&udev
->dev
);
302 struct bus_id_priv
*busid_priv
;
305 dev_dbg(&udev
->dev
, "Enter probe\n");
307 /* check we should claim or not by busid_table */
308 busid_priv
= get_busid_priv(udev_busid
);
309 if (!busid_priv
|| (busid_priv
->status
== STUB_BUSID_REMOV
) ||
310 (busid_priv
->status
== STUB_BUSID_OTHER
)) {
312 "%s is not in match_busid table... skip!\n",
316 * Return value should be ENODEV or ENOXIO to continue trying
317 * other matched drivers by the driver core.
318 * See driver_probe_device() in driver/base/dd.c
321 goto call_put_busid_priv
;
324 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
) {
325 dev_dbg(&udev
->dev
, "%s is a usb hub device... skip!\n",
328 goto call_put_busid_priv
;
331 if (!strcmp(udev
->bus
->bus_name
, "vhci_hcd")) {
333 "%s is attached on vhci_hcd... skip!\n",
337 goto call_put_busid_priv
;
340 /* ok, this is my device */
341 sdev
= stub_device_alloc(udev
);
344 goto call_put_busid_priv
;
348 "usbip-host: register new device (bus %u dev %u)\n",
349 udev
->bus
->busnum
, udev
->devnum
);
351 busid_priv
->shutdown_busid
= 0;
353 /* set private data to usb_device */
354 dev_set_drvdata(&udev
->dev
, sdev
);
355 busid_priv
->sdev
= sdev
;
356 busid_priv
->udev
= udev
;
359 * Claim this hub port.
360 * It doesn't matter what value we pass as owner
361 * (struct dev_state) as long as it is unique.
363 rc
= usb_hub_claim_port(udev
->parent
, udev
->portnum
,
364 (struct usb_dev_state
*) udev
);
366 dev_dbg(&udev
->dev
, "unable to claim port\n");
370 rc
= stub_add_files(&udev
->dev
);
372 dev_err(&udev
->dev
, "stub_add_files for %s\n", udev_busid
);
375 busid_priv
->status
= STUB_BUSID_ALLOC
;
378 goto call_put_busid_priv
;
381 usb_hub_release_port(udev
->parent
, udev
->portnum
,
382 (struct usb_dev_state
*) udev
);
384 dev_set_drvdata(&udev
->dev
, NULL
);
387 busid_priv
->sdev
= NULL
;
388 stub_device_free(sdev
);
391 put_busid_priv(busid_priv
);
395 static void shutdown_busid(struct bus_id_priv
*busid_priv
)
397 if (busid_priv
->sdev
&& !busid_priv
->shutdown_busid
) {
398 busid_priv
->shutdown_busid
= 1;
399 usbip_event_add(&busid_priv
->sdev
->ud
, SDEV_EVENT_REMOVED
);
401 /* wait for the stop of the event handler */
402 usbip_stop_eh(&busid_priv
->sdev
->ud
);
407 * called in usb_disconnect() or usb_deregister()
408 * but only if actconfig(active configuration) exists
410 static void stub_disconnect(struct usb_device
*udev
)
412 struct stub_device
*sdev
;
413 const char *udev_busid
= dev_name(&udev
->dev
);
414 struct bus_id_priv
*busid_priv
;
417 dev_dbg(&udev
->dev
, "Enter disconnect\n");
419 busid_priv
= get_busid_priv(udev_busid
);
425 sdev
= dev_get_drvdata(&udev
->dev
);
427 /* get stub_device */
429 dev_err(&udev
->dev
, "could not get device");
430 goto call_put_busid_priv
;
433 dev_set_drvdata(&udev
->dev
, NULL
);
436 * NOTE: rx/tx threads are invoked for each usb_device.
438 stub_remove_files(&udev
->dev
);
441 rc
= usb_hub_release_port(udev
->parent
, udev
->portnum
,
442 (struct usb_dev_state
*) udev
);
444 dev_dbg(&udev
->dev
, "unable to release port\n");
445 goto call_put_busid_priv
;
448 /* If usb reset is called from event handler */
449 if (usbip_in_eh(current
))
450 goto call_put_busid_priv
;
452 /* shutdown the current connection */
453 shutdown_busid(busid_priv
);
455 usb_put_dev(sdev
->udev
);
458 busid_priv
->sdev
= NULL
;
459 stub_device_free(sdev
);
461 if (busid_priv
->status
== STUB_BUSID_ALLOC
)
462 busid_priv
->status
= STUB_BUSID_ADDED
;
465 put_busid_priv(busid_priv
);
470 /* These functions need usb_port_suspend and usb_port_resume,
471 * which reside in drivers/usb/core/usb.h. Skip for now. */
473 static int stub_suspend(struct usb_device
*udev
, pm_message_t message
)
475 dev_dbg(&udev
->dev
, "stub_suspend\n");
480 static int stub_resume(struct usb_device
*udev
, pm_message_t message
)
482 dev_dbg(&udev
->dev
, "stub_resume\n");
487 #endif /* CONFIG_PM */
489 struct usb_device_driver stub_driver
= {
490 .name
= "usbip-host",
492 .disconnect
= stub_disconnect
,
494 .suspend
= stub_suspend
,
495 .resume
= stub_resume
,
497 .supports_autosuspend
= 0,