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