Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.git] / drivers / usb / usbip / stub_dev.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0+
4d7b5c7f
TH
2/*
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
4d7b5c7f
TH
4 */
5
7aaacb43 6#include <linux/device.h>
3d0a2a22 7#include <linux/file.h>
9720b4bc 8#include <linux/kthread.h>
99c97852 9#include <linux/module.h>
5a0e3ad6 10
4d7b5c7f
TH
11#include "usbip_common.h"
12#include "stub.h"
13
4d7b5c7f 14/*
d012c2a5 15 * usbip_status shows the status of usbip-host as long as this driver is bound
16 * to the target device.
4d7b5c7f 17 */
b1f56aca
GKH
18static ssize_t usbip_status_show(struct device *dev,
19 struct device_attribute *attr, char *buf)
4d7b5c7f
TH
20{
21 struct stub_device *sdev = dev_get_drvdata(dev);
22 int status;
23
24 if (!sdev) {
25 dev_err(dev, "sdev is null\n");
26 return -ENODEV;
27 }
28
dcf14779 29 spin_lock_irq(&sdev->ud.lock);
4d7b5c7f 30 status = sdev->ud.status;
dcf14779 31 spin_unlock_irq(&sdev->ud.lock);
4d7b5c7f 32
27ef01e3 33 return sysfs_emit(buf, "%d\n", status);
4d7b5c7f 34}
b1f56aca 35static DEVICE_ATTR_RO(usbip_status);
4d7b5c7f
TH
36
37/*
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.
41 */
ca35910a 42static ssize_t usbip_sockfd_store(struct device *dev, struct device_attribute *attr,
4d7b5c7f
TH
43 const char *buf, size_t count)
44{
45 struct stub_device *sdev = dev_get_drvdata(dev);
46 int sockfd = 0;
47 struct socket *socket;
f8cfc023 48 int rv;
9380afd6
SK
49 struct task_struct *tcp_rx = NULL;
50 struct task_struct *tcp_tx = NULL;
4d7b5c7f
TH
51
52 if (!sdev) {
53 dev_err(dev, "sdev is null\n");
54 return -ENODEV;
55 }
56
f8cfc023
EO
57 rv = sscanf(buf, "%d", &sockfd);
58 if (rv != 1)
59 return -EINVAL;
4d7b5c7f
TH
60
61 if (sockfd != -1) {
964ea96e 62 int err;
3eed8c03 63
4d7b5c7f
TH
64 dev_info(dev, "stub up\n");
65
9dbf34a8 66 mutex_lock(&sdev->ud.sysfs_lock);
dcf14779 67 spin_lock_irq(&sdev->ud.lock);
4d7b5c7f
TH
68
69 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
70 dev_err(dev, "not ready\n");
31398f63 71 goto err;
4d7b5c7f
TH
72 }
73
964ea96e 74 socket = sockfd_lookup(sockfd, &err);
47ccc8fc
SK
75 if (!socket) {
76 dev_err(dev, "failed to lookup sock");
31398f63 77 goto err;
47ccc8fc
SK
78 }
79
80 if (socket->type != SOCK_STREAM) {
81 dev_err(dev, "Expecting SOCK_STREAM - found %d",
82 socket->type);
83 goto sock_err;
84 }
31398f63 85
9380afd6 86 /* unlock and create threads and get tasks */
dcf14779 87 spin_unlock_irq(&sdev->ud.lock);
9380afd6
SK
88 tcp_rx = kthread_create(stub_rx_loop, &sdev->ud, "stub_rx");
89 if (IS_ERR(tcp_rx)) {
90 sockfd_put(socket);
9dbf34a8 91 goto unlock_mutex;
9380afd6
SK
92 }
93 tcp_tx = kthread_create(stub_tx_loop, &sdev->ud, "stub_tx");
94 if (IS_ERR(tcp_tx)) {
95 kthread_stop(tcp_rx);
96 sockfd_put(socket);
9dbf34a8 97 goto unlock_mutex;
9380afd6 98 }
4d7b5c7f 99
9380afd6
SK
100 /* get task structs now */
101 get_task_struct(tcp_rx);
102 get_task_struct(tcp_tx);
4d7b5c7f 103
9380afd6 104 /* lock and update sdev->ud state */
dcf14779 105 spin_lock_irq(&sdev->ud.lock);
9380afd6
SK
106 sdev->ud.tcp_socket = socket;
107 sdev->ud.sockfd = sockfd;
108 sdev->ud.tcp_rx = tcp_rx;
109 sdev->ud.tcp_tx = tcp_tx;
4d7b5c7f 110 sdev->ud.status = SDEV_ST_USED;
dcf14779 111 spin_unlock_irq(&sdev->ud.lock);
4d7b5c7f 112
9380afd6
SK
113 wake_up_process(sdev->ud.tcp_rx);
114 wake_up_process(sdev->ud.tcp_tx);
115
9dbf34a8
SK
116 mutex_unlock(&sdev->ud.sysfs_lock);
117
4d7b5c7f
TH
118 } else {
119 dev_info(dev, "stub down\n");
120
9b6447e0
JITM
121 mutex_lock(&sdev->ud.sysfs_lock);
122
dcf14779 123 spin_lock_irq(&sdev->ud.lock);
31398f63
KK
124 if (sdev->ud.status != SDEV_ST_USED)
125 goto err;
126
dcf14779 127 spin_unlock_irq(&sdev->ud.lock);
4d7b5c7f
TH
128
129 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
9dbf34a8 130 mutex_unlock(&sdev->ud.sysfs_lock);
4d7b5c7f
TH
131 }
132
133 return count;
31398f63 134
47ccc8fc
SK
135sock_err:
136 sockfd_put(socket);
31398f63
KK
137err:
138 spin_unlock_irq(&sdev->ud.lock);
9dbf34a8
SK
139unlock_mutex:
140 mutex_unlock(&sdev->ud.sysfs_lock);
964ea96e 141 return -EINVAL;
4d7b5c7f 142}
ca35910a 143static DEVICE_ATTR_WO(usbip_sockfd);
4d7b5c7f 144
c5501d23
GKH
145static struct attribute *usbip_attrs[] = {
146 &dev_attr_usbip_status.attr,
147 &dev_attr_usbip_sockfd.attr,
148 &dev_attr_usbip_debug.attr,
149 NULL,
150};
151ATTRIBUTE_GROUPS(usbip);
4d7b5c7f 152
4d7b5c7f
TH
153static void stub_shutdown_connection(struct usbip_device *ud)
154{
155 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
156
157 /*
158 * When removing an exported device, kernel panic sometimes occurred
159 * and then EIP was sk_wait_data of stub_rx thread. Is this because
160 * sk_wait_data returned though stub_rx thread was already finished by
161 * step 1?
162 */
163 if (ud->tcp_socket) {
90120d15 164 dev_dbg(&sdev->udev->dev, "shutdown sockfd %d\n", ud->sockfd);
4d7b5c7f
TH
165 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
166 }
167
168 /* 1. stop threads */
b7caecb8 169 if (ud->tcp_rx) {
ba46ce30 170 kthread_stop_put(ud->tcp_rx);
b7caecb8 171 ud->tcp_rx = NULL;
172 }
173 if (ud->tcp_tx) {
ba46ce30 174 kthread_stop_put(ud->tcp_tx);
b7caecb8 175 ud->tcp_tx = NULL;
176 }
4d7b5c7f 177
4d7b5c7f 178 /*
87352760 179 * 2. close the socket
180 *
181 * tcp_socket is freed after threads are killed so that usbip_xmit does
182 * not touch NULL socket.
4d7b5c7f
TH
183 */
184 if (ud->tcp_socket) {
964ea96e 185 sockfd_put(ud->tcp_socket);
4d7b5c7f 186 ud->tcp_socket = NULL;
009f41ae 187 ud->sockfd = -1;
4d7b5c7f
TH
188 }
189
190 /* 3. free used data */
191 stub_device_cleanup_urbs(sdev);
192
193 /* 4. free stub_unlink */
194 {
195 unsigned long flags;
196 struct stub_unlink *unlink, *tmp;
197
198 spin_lock_irqsave(&sdev->priv_lock, flags);
4d7b5c7f
TH
199 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
200 list_del(&unlink->list);
201 kfree(unlink);
202 }
87352760 203 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
204 list) {
4d7b5c7f
TH
205 list_del(&unlink->list);
206 kfree(unlink);
207 }
4d7b5c7f
TH
208 spin_unlock_irqrestore(&sdev->priv_lock, flags);
209 }
210}
211
212static void stub_device_reset(struct usbip_device *ud)
213{
214 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
2d8f4595 215 struct usb_device *udev = sdev->udev;
4d7b5c7f
TH
216 int ret;
217
1a4b6f66 218 dev_dbg(&udev->dev, "device reset");
2d8f4595 219
8c7003a3 220 ret = usb_lock_device_for_reset(udev, NULL);
4d7b5c7f
TH
221 if (ret < 0) {
222 dev_err(&udev->dev, "lock for reset\n");
dcf14779 223 spin_lock_irq(&ud->lock);
4d7b5c7f 224 ud->status = SDEV_ST_ERROR;
dcf14779 225 spin_unlock_irq(&ud->lock);
4d7b5c7f
TH
226 return;
227 }
228
229 /* try to reset the device */
230 ret = usb_reset_device(udev);
4d7b5c7f
TH
231 usb_unlock_device(udev);
232
dcf14779 233 spin_lock_irq(&ud->lock);
4d7b5c7f
TH
234 if (ret) {
235 dev_err(&udev->dev, "device reset\n");
236 ud->status = SDEV_ST_ERROR;
4d7b5c7f
TH
237 } else {
238 dev_info(&udev->dev, "device reset\n");
239 ud->status = SDEV_ST_AVAILABLE;
4d7b5c7f 240 }
dcf14779 241 spin_unlock_irq(&ud->lock);
4d7b5c7f
TH
242}
243
244static void stub_device_unusable(struct usbip_device *ud)
245{
dcf14779 246 spin_lock_irq(&ud->lock);
4d7b5c7f 247 ud->status = SDEV_ST_ERROR;
dcf14779 248 spin_unlock_irq(&ud->lock);
4d7b5c7f
TH
249}
250
4d7b5c7f
TH
251/**
252 * stub_device_alloc - allocate a new stub_device struct
8c7003a3 253 * @udev: usb_device of a new device
4d7b5c7f
TH
254 *
255 * Allocates and initializes a new stub_device struct.
256 */
b7945b77 257static struct stub_device *stub_device_alloc(struct usb_device *udev)
4d7b5c7f
TH
258{
259 struct stub_device *sdev;
b7945b77
VM
260 int busnum = udev->bus->busnum;
261 int devnum = udev->devnum;
4d7b5c7f 262
b7945b77 263 dev_dbg(&udev->dev, "allocating stub device");
4d7b5c7f
TH
264
265 /* yes, it's a new device */
266 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
78110bb8 267 if (!sdev)
4d7b5c7f 268 return NULL;
4d7b5c7f 269
2d8f4595 270 sdev->udev = usb_get_dev(udev);
4d7b5c7f
TH
271
272 /*
273 * devid is defined with devnum when this driver is first allocated.
274 * devnum may change later if a device is reset. However, devid never
275 * changes during a usbip connection.
276 */
b744a45f 277 sdev->devid = (busnum << 16) | devnum;
278 sdev->ud.side = USBIP_STUB;
279 sdev->ud.status = SDEV_ST_AVAILABLE;
4d7b5c7f 280 spin_lock_init(&sdev->ud.lock);
9dbf34a8 281 mutex_init(&sdev->ud.sysfs_lock);
b744a45f 282 sdev->ud.tcp_socket = NULL;
009f41ae 283 sdev->ud.sockfd = -1;
4d7b5c7f
TH
284
285 INIT_LIST_HEAD(&sdev->priv_init);
286 INIT_LIST_HEAD(&sdev->priv_tx);
287 INIT_LIST_HEAD(&sdev->priv_free);
288 INIT_LIST_HEAD(&sdev->unlink_free);
289 INIT_LIST_HEAD(&sdev->unlink_tx);
4d7b5c7f
TH
290 spin_lock_init(&sdev->priv_lock);
291
292 init_waitqueue_head(&sdev->tx_waitq);
293
294 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
295 sdev->ud.eh_ops.reset = stub_device_reset;
296 sdev->ud.eh_ops.unusable = stub_device_unusable;
297
298 usbip_start_eh(&sdev->ud);
299
b7945b77 300 dev_dbg(&udev->dev, "register new device\n");
1a4b6f66 301
4d7b5c7f
TH
302 return sdev;
303}
304
b94b3a62 305static void stub_device_free(struct stub_device *sdev)
4d7b5c7f 306{
4d7b5c7f 307 kfree(sdev);
4d7b5c7f
TH
308}
309
b7945b77 310static int stub_probe(struct usb_device *udev)
4d7b5c7f 311{
4d7b5c7f 312 struct stub_device *sdev = NULL;
b7945b77 313 const char *udev_busid = dev_name(&udev->dev);
aa5873e9 314 struct bus_id_priv *busid_priv;
22076557 315 int rc = 0;
0c9e8b3c 316 char save_status;
4d7b5c7f 317
28b68acc 318 dev_dbg(&udev->dev, "Enter probe\n");
4d7b5c7f 319
0c9e8b3c
SK
320 /* Not sure if this is our device. Allocate here to avoid
321 * calling alloc while holding busid_table lock.
322 */
323 sdev = stub_device_alloc(udev);
324 if (!sdev)
325 return -ENOMEM;
326
4d7b5c7f 327 /* check we should claim or not by busid_table */
aa5873e9 328 busid_priv = get_busid_priv(udev_busid);
87352760 329 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
b744a45f 330 (busid_priv->status == STUB_BUSID_OTHER)) {
b7945b77 331 dev_info(&udev->dev,
6165cc51
EW
332 "%s is not in match_busid table... skip!\n",
333 udev_busid);
4d7b5c7f
TH
334
335 /*
336 * Return value should be ENODEV or ENOXIO to continue trying
337 * other matched drivers by the driver core.
338 * See driver_probe_device() in driver/base/dd.c
339 */
22076557 340 rc = -ENODEV;
3ea3091f
SK
341 if (!busid_priv)
342 goto sdev_free;
343
344 goto call_put_busid_priv;
4d7b5c7f
TH
345 }
346
1a4b6f66 347 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
348 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
349 udev_busid);
22076557 350 rc = -ENODEV;
3ea3091f 351 goto call_put_busid_priv;
4d7b5c7f
TH
352 }
353
354 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
6165cc51
EW
355 dev_dbg(&udev->dev,
356 "%s is attached on vhci_hcd... skip!\n",
357 udev_busid);
358
22076557 359 rc = -ENODEV;
3ea3091f 360 goto call_put_busid_priv;
4d7b5c7f
TH
361 }
362
4d7b5c7f 363
b7945b77
VM
364 dev_info(&udev->dev,
365 "usbip-host: register new device (bus %u dev %u)\n",
366 udev->bus->busnum, udev->devnum);
4d7b5c7f 367
aa5873e9
EK
368 busid_priv->shutdown_busid = 0;
369
b7945b77
VM
370 /* set private data to usb_device */
371 dev_set_drvdata(&udev->dev, sdev);
0c9e8b3c 372
aa5873e9 373 busid_priv->sdev = sdev;
a46034ca 374 busid_priv->udev = udev;
4d7b5c7f 375
0c9e8b3c
SK
376 save_status = busid_priv->status;
377 busid_priv->status = STUB_BUSID_ALLOC;
378
3ea3091f
SK
379 /* release the busid_lock */
380 put_busid_priv(busid_priv);
381
6080cd0e
VM
382 /*
383 * Claim this hub port.
384 * It doesn't matter what value we pass as owner
385 * (struct dev_state) as long as it is unique.
386 */
387 rc = usb_hub_claim_port(udev->parent, udev->portnum,
9b6f0c4b 388 (struct usb_dev_state *) udev);
6080cd0e
VM
389 if (rc) {
390 dev_dbg(&udev->dev, "unable to claim port\n");
3ff67445 391 goto err_port;
6080cd0e
VM
392 }
393
0c9e8b3c 394 return 0;
22076557 395
3ff67445
AK
396err_port:
397 dev_set_drvdata(&udev->dev, NULL);
3ff67445 398
0c9e8b3c
SK
399 /* we already have busid_priv, just lock busid_lock */
400 spin_lock(&busid_priv->busid_lock);
3ff67445 401 busid_priv->sdev = NULL;
0c9e8b3c 402 busid_priv->status = save_status;
3ea3091f
SK
403 spin_unlock(&busid_priv->busid_lock);
404 /* lock is released - go to free */
405 goto sdev_free;
406
407call_put_busid_priv:
0c9e8b3c 408 /* release the busid_lock */
22076557 409 put_busid_priv(busid_priv);
0c9e8b3c 410
3ea3091f 411sdev_free:
9ec4cbf1 412 usb_put_dev(udev);
3ea3091f
SK
413 stub_device_free(sdev);
414
3ff67445 415 return rc;
4d7b5c7f
TH
416}
417
aa5873e9
EK
418static void shutdown_busid(struct bus_id_priv *busid_priv)
419{
0c9e8b3c 420 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
aa5873e9 421
0c9e8b3c
SK
422 /* wait for the stop of the event handler */
423 usbip_stop_eh(&busid_priv->sdev->ud);
aa5873e9
EK
424}
425
4d7b5c7f
TH
426/*
427 * called in usb_disconnect() or usb_deregister()
428 * but only if actconfig(active configuration) exists
429 */
b7945b77 430static void stub_disconnect(struct usb_device *udev)
4d7b5c7f 431{
aa5873e9 432 struct stub_device *sdev;
b7945b77 433 const char *udev_busid = dev_name(&udev->dev);
aa5873e9 434 struct bus_id_priv *busid_priv;
6080cd0e 435 int rc;
aa5873e9 436
28b68acc 437 dev_dbg(&udev->dev, "Enter disconnect\n");
4d7b5c7f 438
1a4b6f66 439 busid_priv = get_busid_priv(udev_busid);
aa5873e9
EK
440 if (!busid_priv) {
441 BUG();
442 return;
443 }
444
b7945b77 445 sdev = dev_get_drvdata(&udev->dev);
aa5873e9 446
4d7b5c7f
TH
447 /* get stub_device */
448 if (!sdev) {
b7945b77 449 dev_err(&udev->dev, "could not get device");
3ea3091f
SK
450 /* release busid_lock */
451 put_busid_priv(busid_priv);
452 return;
4d7b5c7f
TH
453 }
454
b7945b77 455 dev_set_drvdata(&udev->dev, NULL);
4d7b5c7f 456
0c9e8b3c
SK
457 /* release busid_lock before call to remove device files */
458 put_busid_priv(busid_priv);
459
4d7b5c7f 460 /*
c7f00899 461 * NOTE: rx/tx threads are invoked for each usb_device.
4d7b5c7f 462 */
4d7b5c7f 463
6080cd0e
VM
464 /* release port */
465 rc = usb_hub_release_port(udev->parent, udev->portnum,
9b6f0c4b 466 (struct usb_dev_state *) udev);
97475763
JB
467 /*
468 * NOTE: If a HUB disconnect triggered disconnect of the down stream
469 * device usb_hub_release_port will return -ENODEV so we can safely ignore
470 * that error here.
471 */
472 if (rc && (rc != -ENODEV)) {
473 dev_dbg(&udev->dev, "unable to release port (%i)\n", rc);
0c9e8b3c 474 return;
6080cd0e
VM
475 }
476
c7f00899 477 /* If usb reset is called from event handler */
bb7871ad 478 if (usbip_in_eh(current))
0c9e8b3c
SK
479 return;
480
481 /* we already have busid_priv, just lock busid_lock */
482 spin_lock(&busid_priv->busid_lock);
483 if (!busid_priv->shutdown_busid)
484 busid_priv->shutdown_busid = 1;
485 /* release busid_lock */
3ea3091f 486 spin_unlock(&busid_priv->busid_lock);
4d7b5c7f 487
c7f00899 488 /* shutdown the current connection */
aa5873e9 489 shutdown_busid(busid_priv);
4d7b5c7f 490
2d8f4595 491 usb_put_dev(sdev->udev);
2d8f4595 492
0c9e8b3c
SK
493 /* we already have busid_priv, just lock busid_lock */
494 spin_lock(&busid_priv->busid_lock);
c7f00899 495 /* free sdev */
aa5873e9 496 busid_priv->sdev = NULL;
4d7b5c7f
TH
497 stub_device_free(sdev);
498
7510df3f 499 if (busid_priv->status == STUB_BUSID_ALLOC)
aa5873e9 500 busid_priv->status = STUB_BUSID_ADDED;
0c9e8b3c 501 /* release busid_lock */
3ea3091f
SK
502 spin_unlock(&busid_priv->busid_lock);
503 return;
4d7b5c7f 504}
d012c2a5 505
b7945b77 506#ifdef CONFIG_PM
553a1a50 507
b7945b77
VM
508/* These functions need usb_port_suspend and usb_port_resume,
509 * which reside in drivers/usb/core/usb.h. Skip for now. */
510
511static int stub_suspend(struct usb_device *udev, pm_message_t message)
1aee199c 512{
b7945b77
VM
513 dev_dbg(&udev->dev, "stub_suspend\n");
514
1aee199c
AM
515 return 0;
516}
517
b7945b77 518static int stub_resume(struct usb_device *udev, pm_message_t message)
1aee199c 519{
b7945b77
VM
520 dev_dbg(&udev->dev, "stub_resume\n");
521
1aee199c
AM
522 return 0;
523}
524
b7945b77
VM
525#endif /* CONFIG_PM */
526
527struct usb_device_driver stub_driver = {
d012c2a5 528 .name = "usbip-host",
529 .probe = stub_probe,
530 .disconnect = stub_disconnect,
b7945b77
VM
531#ifdef CONFIG_PM
532 .suspend = stub_suspend,
533 .resume = stub_resume,
534#endif
535 .supports_autosuspend = 0,
c5501d23 536 .dev_groups = usbip_groups,
97c451ca 537};