usbip: keep usbip_device sockfd state in sync with tcp_socket
[linux/fpc-iii.git] / drivers / usb / usbip / stub_dev.c
blob86037e5b11015a97266bb7137bd87c31859744b3
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
4 */
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"
12 #include "stub.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);
22 int status;
24 if (!sdev) {
25 dev_err(dev, "sdev is null\n");
26 return -ENODEV;
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);
46 int sockfd = 0;
47 struct socket *socket;
48 int rv;
50 if (!sdev) {
51 dev_err(dev, "sdev is null\n");
52 return -ENODEV;
55 rv = sscanf(buf, "%d", &sockfd);
56 if (rv != 1)
57 return -EINVAL;
59 if (sockfd != -1) {
60 int err;
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");
68 goto err;
71 socket = sockfd_lookup(sockfd, &err);
72 if (!socket)
73 goto 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,
81 "stub_rx");
82 sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud,
83 "stub_tx");
85 spin_lock_irq(&sdev->ud.lock);
86 sdev->ud.status = SDEV_ST_USED;
87 spin_unlock_irq(&sdev->ud.lock);
89 } else {
90 dev_info(dev, "stub down\n");
92 spin_lock_irq(&sdev->ud.lock);
93 if (sdev->ud.status != SDEV_ST_USED)
94 goto err;
96 spin_unlock_irq(&sdev->ud.lock);
98 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
101 return count;
103 err:
104 spin_unlock_irq(&sdev->ud.lock);
105 return -EINVAL;
107 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
109 static int stub_add_files(struct device *dev)
111 int err = 0;
113 err = device_create_file(dev, &dev_attr_usbip_status);
114 if (err)
115 goto err_status;
117 err = device_create_file(dev, &dev_attr_usbip_sockfd);
118 if (err)
119 goto err_sockfd;
121 err = device_create_file(dev, &dev_attr_usbip_debug);
122 if (err)
123 goto err_debug;
125 return 0;
127 err_debug:
128 device_remove_file(dev, &dev_attr_usbip_sockfd);
129 err_sockfd:
130 device_remove_file(dev, &dev_attr_usbip_status);
131 err_status:
132 return err;
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
150 * step 1?
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 */
158 if (ud->tcp_rx) {
159 kthread_stop_put(ud->tcp_rx);
160 ud->tcp_rx = NULL;
162 if (ud->tcp_tx) {
163 kthread_stop_put(ud->tcp_tx);
164 ud->tcp_tx = NULL;
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;
176 ud->sockfd = -1;
179 /* 3. free used data */
180 stub_device_cleanup_urbs(sdev);
182 /* 4. free stub_unlink */
184 unsigned long flags;
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);
190 kfree(unlink);
192 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
193 list) {
194 list_del(&unlink->list);
195 kfree(unlink);
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;
205 int ret;
207 dev_dbg(&udev->dev, "device reset");
209 ret = usb_lock_device_for_reset(udev, NULL);
210 if (ret < 0) {
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);
215 return;
218 /* try to reset the device */
219 ret = usb_reset_device(udev);
220 usb_unlock_device(udev);
222 spin_lock_irq(&ud->lock);
223 if (ret) {
224 dev_err(&udev->dev, "device reset\n");
225 ud->status = SDEV_ST_ERROR;
226 } else {
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);
256 if (!sdev)
257 return NULL;
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");
290 return sdev;
293 static void stub_device_free(struct stub_device *sdev)
295 kfree(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;
303 int rc;
305 dev_dbg(&udev->dev, "Enter\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)) {
311 dev_info(&udev->dev,
312 "%s is not in match_busid table... skip!\n",
313 udev_busid);
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
320 return -ENODEV;
323 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
324 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
325 udev_busid);
326 return -ENODEV;
329 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
330 dev_dbg(&udev->dev,
331 "%s is attached on vhci_hcd... skip!\n",
332 udev_busid);
334 return -ENODEV;
337 /* ok, this is my device */
338 sdev = stub_device_alloc(udev);
339 if (!sdev)
340 return -ENOMEM;
342 dev_info(&udev->dev,
343 "usbip-host: register new device (bus %u dev %u)\n",
344 udev->bus->busnum, udev->devnum);
346 busid_priv->shutdown_busid = 0;
348 /* set private data to usb_device */
349 dev_set_drvdata(&udev->dev, sdev);
350 busid_priv->sdev = sdev;
351 busid_priv->udev = udev;
354 * Claim this hub port.
355 * It doesn't matter what value we pass as owner
356 * (struct dev_state) as long as it is unique.
358 rc = usb_hub_claim_port(udev->parent, udev->portnum,
359 (struct usb_dev_state *) udev);
360 if (rc) {
361 dev_dbg(&udev->dev, "unable to claim port\n");
362 goto err_port;
365 rc = stub_add_files(&udev->dev);
366 if (rc) {
367 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
368 goto err_files;
370 busid_priv->status = STUB_BUSID_ALLOC;
372 return 0;
373 err_files:
374 usb_hub_release_port(udev->parent, udev->portnum,
375 (struct usb_dev_state *) udev);
376 err_port:
377 dev_set_drvdata(&udev->dev, NULL);
378 usb_put_dev(udev);
380 busid_priv->sdev = NULL;
381 stub_device_free(sdev);
382 return rc;
385 static void shutdown_busid(struct bus_id_priv *busid_priv)
387 if (busid_priv->sdev && !busid_priv->shutdown_busid) {
388 busid_priv->shutdown_busid = 1;
389 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
391 /* wait for the stop of the event handler */
392 usbip_stop_eh(&busid_priv->sdev->ud);
397 * called in usb_disconnect() or usb_deregister()
398 * but only if actconfig(active configuration) exists
400 static void stub_disconnect(struct usb_device *udev)
402 struct stub_device *sdev;
403 const char *udev_busid = dev_name(&udev->dev);
404 struct bus_id_priv *busid_priv;
405 int rc;
407 dev_dbg(&udev->dev, "Enter\n");
409 busid_priv = get_busid_priv(udev_busid);
410 if (!busid_priv) {
411 BUG();
412 return;
415 sdev = dev_get_drvdata(&udev->dev);
417 /* get stub_device */
418 if (!sdev) {
419 dev_err(&udev->dev, "could not get device");
420 return;
423 dev_set_drvdata(&udev->dev, NULL);
426 * NOTE: rx/tx threads are invoked for each usb_device.
428 stub_remove_files(&udev->dev);
430 /* release port */
431 rc = usb_hub_release_port(udev->parent, udev->portnum,
432 (struct usb_dev_state *) udev);
433 if (rc) {
434 dev_dbg(&udev->dev, "unable to release port\n");
435 return;
438 /* If usb reset is called from event handler */
439 if (usbip_in_eh(current))
440 return;
442 /* shutdown the current connection */
443 shutdown_busid(busid_priv);
445 usb_put_dev(sdev->udev);
447 /* free sdev */
448 busid_priv->sdev = NULL;
449 stub_device_free(sdev);
451 if (busid_priv->status == STUB_BUSID_ALLOC) {
452 busid_priv->status = STUB_BUSID_ADDED;
453 } else {
454 busid_priv->status = STUB_BUSID_OTHER;
455 del_match_busid((char *)udev_busid);
459 #ifdef CONFIG_PM
461 /* These functions need usb_port_suspend and usb_port_resume,
462 * which reside in drivers/usb/core/usb.h. Skip for now. */
464 static int stub_suspend(struct usb_device *udev, pm_message_t message)
466 dev_dbg(&udev->dev, "stub_suspend\n");
468 return 0;
471 static int stub_resume(struct usb_device *udev, pm_message_t message)
473 dev_dbg(&udev->dev, "stub_resume\n");
475 return 0;
478 #endif /* CONFIG_PM */
480 struct usb_device_driver stub_driver = {
481 .name = "usbip-host",
482 .probe = stub_probe,
483 .disconnect = stub_disconnect,
484 #ifdef CONFIG_PM
485 .suspend = stub_suspend,
486 .resume = stub_resume,
487 #endif
488 .supports_autosuspend = 0,