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