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 struct attribute
*usbip_attrs
[] = {
110 &dev_attr_usbip_status
.attr
,
111 &dev_attr_usbip_sockfd
.attr
,
112 &dev_attr_usbip_debug
.attr
,
115 ATTRIBUTE_GROUPS(usbip
);
117 static void stub_shutdown_connection(struct usbip_device
*ud
)
119 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
122 * When removing an exported device, kernel panic sometimes occurred
123 * and then EIP was sk_wait_data of stub_rx thread. Is this because
124 * sk_wait_data returned though stub_rx thread was already finished by
127 if (ud
->tcp_socket
) {
128 dev_dbg(&sdev
->udev
->dev
, "shutdown sockfd %d\n", ud
->sockfd
);
129 kernel_sock_shutdown(ud
->tcp_socket
, SHUT_RDWR
);
132 /* 1. stop threads */
134 kthread_stop_put(ud
->tcp_rx
);
138 kthread_stop_put(ud
->tcp_tx
);
143 * 2. close the socket
145 * tcp_socket is freed after threads are killed so that usbip_xmit does
146 * not touch NULL socket.
148 if (ud
->tcp_socket
) {
149 sockfd_put(ud
->tcp_socket
);
150 ud
->tcp_socket
= NULL
;
154 /* 3. free used data */
155 stub_device_cleanup_urbs(sdev
);
157 /* 4. free stub_unlink */
160 struct stub_unlink
*unlink
, *tmp
;
162 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
163 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_tx
, list
) {
164 list_del(&unlink
->list
);
167 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_free
,
169 list_del(&unlink
->list
);
172 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
176 static void stub_device_reset(struct usbip_device
*ud
)
178 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
179 struct usb_device
*udev
= sdev
->udev
;
182 dev_dbg(&udev
->dev
, "device reset");
184 ret
= usb_lock_device_for_reset(udev
, NULL
);
186 dev_err(&udev
->dev
, "lock for reset\n");
187 spin_lock_irq(&ud
->lock
);
188 ud
->status
= SDEV_ST_ERROR
;
189 spin_unlock_irq(&ud
->lock
);
193 /* try to reset the device */
194 ret
= usb_reset_device(udev
);
195 usb_unlock_device(udev
);
197 spin_lock_irq(&ud
->lock
);
199 dev_err(&udev
->dev
, "device reset\n");
200 ud
->status
= SDEV_ST_ERROR
;
202 dev_info(&udev
->dev
, "device reset\n");
203 ud
->status
= SDEV_ST_AVAILABLE
;
205 spin_unlock_irq(&ud
->lock
);
208 static void stub_device_unusable(struct usbip_device
*ud
)
210 spin_lock_irq(&ud
->lock
);
211 ud
->status
= SDEV_ST_ERROR
;
212 spin_unlock_irq(&ud
->lock
);
216 * stub_device_alloc - allocate a new stub_device struct
217 * @udev: usb_device of a new device
219 * Allocates and initializes a new stub_device struct.
221 static struct stub_device
*stub_device_alloc(struct usb_device
*udev
)
223 struct stub_device
*sdev
;
224 int busnum
= udev
->bus
->busnum
;
225 int devnum
= udev
->devnum
;
227 dev_dbg(&udev
->dev
, "allocating stub device");
229 /* yes, it's a new device */
230 sdev
= kzalloc(sizeof(struct stub_device
), GFP_KERNEL
);
234 sdev
->udev
= usb_get_dev(udev
);
237 * devid is defined with devnum when this driver is first allocated.
238 * devnum may change later if a device is reset. However, devid never
239 * changes during a usbip connection.
241 sdev
->devid
= (busnum
<< 16) | devnum
;
242 sdev
->ud
.side
= USBIP_STUB
;
243 sdev
->ud
.status
= SDEV_ST_AVAILABLE
;
244 spin_lock_init(&sdev
->ud
.lock
);
245 sdev
->ud
.tcp_socket
= NULL
;
246 sdev
->ud
.sockfd
= -1;
248 INIT_LIST_HEAD(&sdev
->priv_init
);
249 INIT_LIST_HEAD(&sdev
->priv_tx
);
250 INIT_LIST_HEAD(&sdev
->priv_free
);
251 INIT_LIST_HEAD(&sdev
->unlink_free
);
252 INIT_LIST_HEAD(&sdev
->unlink_tx
);
253 spin_lock_init(&sdev
->priv_lock
);
255 init_waitqueue_head(&sdev
->tx_waitq
);
257 sdev
->ud
.eh_ops
.shutdown
= stub_shutdown_connection
;
258 sdev
->ud
.eh_ops
.reset
= stub_device_reset
;
259 sdev
->ud
.eh_ops
.unusable
= stub_device_unusable
;
261 usbip_start_eh(&sdev
->ud
);
263 dev_dbg(&udev
->dev
, "register new device\n");
268 static void stub_device_free(struct stub_device
*sdev
)
273 static int stub_probe(struct usb_device
*udev
)
275 struct stub_device
*sdev
= NULL
;
276 const char *udev_busid
= dev_name(&udev
->dev
);
277 struct bus_id_priv
*busid_priv
;
281 dev_dbg(&udev
->dev
, "Enter probe\n");
283 /* Not sure if this is our device. Allocate here to avoid
284 * calling alloc while holding busid_table lock.
286 sdev
= stub_device_alloc(udev
);
290 /* check we should claim or not by busid_table */
291 busid_priv
= get_busid_priv(udev_busid
);
292 if (!busid_priv
|| (busid_priv
->status
== STUB_BUSID_REMOV
) ||
293 (busid_priv
->status
== STUB_BUSID_OTHER
)) {
295 "%s is not in match_busid table... skip!\n",
299 * Return value should be ENODEV or ENOXIO to continue trying
300 * other matched drivers by the driver core.
301 * See driver_probe_device() in driver/base/dd.c
307 goto call_put_busid_priv
;
310 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
) {
311 dev_dbg(&udev
->dev
, "%s is a usb hub device... skip!\n",
314 goto call_put_busid_priv
;
317 if (!strcmp(udev
->bus
->bus_name
, "vhci_hcd")) {
319 "%s is attached on vhci_hcd... skip!\n",
323 goto call_put_busid_priv
;
328 "usbip-host: register new device (bus %u dev %u)\n",
329 udev
->bus
->busnum
, udev
->devnum
);
331 busid_priv
->shutdown_busid
= 0;
333 /* set private data to usb_device */
334 dev_set_drvdata(&udev
->dev
, sdev
);
336 busid_priv
->sdev
= sdev
;
337 busid_priv
->udev
= udev
;
339 save_status
= busid_priv
->status
;
340 busid_priv
->status
= STUB_BUSID_ALLOC
;
342 /* release the busid_lock */
343 put_busid_priv(busid_priv
);
346 * Claim this hub port.
347 * It doesn't matter what value we pass as owner
348 * (struct dev_state) as long as it is unique.
350 rc
= usb_hub_claim_port(udev
->parent
, udev
->portnum
,
351 (struct usb_dev_state
*) udev
);
353 dev_dbg(&udev
->dev
, "unable to claim port\n");
360 dev_set_drvdata(&udev
->dev
, NULL
);
363 /* we already have busid_priv, just lock busid_lock */
364 spin_lock(&busid_priv
->busid_lock
);
365 busid_priv
->sdev
= NULL
;
366 busid_priv
->status
= save_status
;
367 spin_unlock(&busid_priv
->busid_lock
);
368 /* lock is released - go to free */
372 /* release the busid_lock */
373 put_busid_priv(busid_priv
);
376 stub_device_free(sdev
);
381 static void shutdown_busid(struct bus_id_priv
*busid_priv
)
383 usbip_event_add(&busid_priv
->sdev
->ud
, SDEV_EVENT_REMOVED
);
385 /* wait for the stop of the event handler */
386 usbip_stop_eh(&busid_priv
->sdev
->ud
);
390 * called in usb_disconnect() or usb_deregister()
391 * but only if actconfig(active configuration) exists
393 static void stub_disconnect(struct usb_device
*udev
)
395 struct stub_device
*sdev
;
396 const char *udev_busid
= dev_name(&udev
->dev
);
397 struct bus_id_priv
*busid_priv
;
400 dev_dbg(&udev
->dev
, "Enter disconnect\n");
402 busid_priv
= get_busid_priv(udev_busid
);
408 sdev
= dev_get_drvdata(&udev
->dev
);
410 /* get stub_device */
412 dev_err(&udev
->dev
, "could not get device");
413 /* release busid_lock */
414 put_busid_priv(busid_priv
);
418 dev_set_drvdata(&udev
->dev
, NULL
);
420 /* release busid_lock before call to remove device files */
421 put_busid_priv(busid_priv
);
424 * NOTE: rx/tx threads are invoked for each usb_device.
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 /* we already have busid_priv, just lock busid_lock */
440 spin_lock(&busid_priv
->busid_lock
);
441 if (!busid_priv
->shutdown_busid
)
442 busid_priv
->shutdown_busid
= 1;
443 /* release busid_lock */
444 spin_unlock(&busid_priv
->busid_lock
);
446 /* shutdown the current connection */
447 shutdown_busid(busid_priv
);
449 usb_put_dev(sdev
->udev
);
451 /* we already have busid_priv, just lock busid_lock */
452 spin_lock(&busid_priv
->busid_lock
);
454 busid_priv
->sdev
= NULL
;
455 stub_device_free(sdev
);
457 if (busid_priv
->status
== STUB_BUSID_ALLOC
)
458 busid_priv
->status
= STUB_BUSID_ADDED
;
459 /* release busid_lock */
460 spin_unlock(&busid_priv
->busid_lock
);
466 /* These functions need usb_port_suspend and usb_port_resume,
467 * which reside in drivers/usb/core/usb.h. Skip for now. */
469 static int stub_suspend(struct usb_device
*udev
, pm_message_t message
)
471 dev_dbg(&udev
->dev
, "stub_suspend\n");
476 static int stub_resume(struct usb_device
*udev
, pm_message_t message
)
478 dev_dbg(&udev
->dev
, "stub_resume\n");
483 #endif /* CONFIG_PM */
485 struct usb_device_driver stub_driver
= {
486 .name
= "usbip-host",
488 .disconnect
= stub_disconnect
,
490 .suspend
= stub_suspend
,
491 .resume
= stub_resume
,
493 .supports_autosuspend
= 0,
494 .dev_groups
= usbip_groups
,