Version:  2.0.40 2.2.26 2.4.37 2.6.39 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15

Linux/drivers/staging/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  * Define device IDs here if you want to explicitly limit exportable devices.
 30  * In most cases, wildcard matching will be okay because driver binding can be
 31  * changed dynamically by a userland program.
 32  */
 33 static struct usb_device_id stub_table[] = {
 34 #if 0
 35         /* just an example */
 36         { USB_DEVICE(0x05ac, 0x0301) },   /* Mac 1 button mouse */
 37         { USB_DEVICE(0x0430, 0x0009) },   /* Plat Home Keyboard */
 38         { USB_DEVICE(0x059b, 0x0001) },   /* Iomega USB Zip 100 */
 39         { USB_DEVICE(0x04b3, 0x4427) },   /* IBM USB CD-ROM */
 40         { USB_DEVICE(0x05a9, 0xa511) },   /* LifeView USB cam */
 41         { USB_DEVICE(0x55aa, 0x0201) },   /* Imation card reader */
 42         { USB_DEVICE(0x046d, 0x0870) },   /* Qcam Express(QV-30) */
 43         { USB_DEVICE(0x04bb, 0x0101) },   /* IO-DATA HD 120GB */
 44         { USB_DEVICE(0x04bb, 0x0904) },   /* IO-DATA USB-ET/TX */
 45         { USB_DEVICE(0x04bb, 0x0201) },   /* IO-DATA USB-ET/TX */
 46         { USB_DEVICE(0x08bb, 0x2702) },   /* ONKYO USB Speaker */
 47         { USB_DEVICE(0x046d, 0x08b2) },   /* Logicool Qcam 4000 Pro */
 48 #endif
 49         /* magic for wild card */
 50         { .driver_info = 1 },
 51         { 0, }                                     /* Terminating entry */
 52 };
 53 MODULE_DEVICE_TABLE(usb, stub_table);
 54 
 55 /*
 56  * usbip_status shows the status of usbip-host as long as this driver is bound
 57  * to the target device.
 58  */
 59 static ssize_t usbip_status_show(struct device *dev,
 60                                  struct device_attribute *attr, char *buf)
 61 {
 62         struct stub_device *sdev = dev_get_drvdata(dev);
 63         int status;
 64 
 65         if (!sdev) {
 66                 dev_err(dev, "sdev is null\n");
 67                 return -ENODEV;
 68         }
 69 
 70         spin_lock_irq(&sdev->ud.lock);
 71         status = sdev->ud.status;
 72         spin_unlock_irq(&sdev->ud.lock);
 73 
 74         return snprintf(buf, PAGE_SIZE, "%d\n", status);
 75 }
 76 static DEVICE_ATTR_RO(usbip_status);
 77 
 78 /*
 79  * usbip_sockfd gets a socket descriptor of an established TCP connection that
 80  * is used to transfer usbip requests by kernel threads. -1 is a magic number
 81  * by which usbip connection is finished.
 82  */
 83 static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
 84                             const char *buf, size_t count)
 85 {
 86         struct stub_device *sdev = dev_get_drvdata(dev);
 87         int sockfd = 0;
 88         struct socket *socket;
 89         int rv;
 90 
 91         if (!sdev) {
 92                 dev_err(dev, "sdev is null\n");
 93                 return -ENODEV;
 94         }
 95 
 96         rv = sscanf(buf, "%d", &sockfd);
 97         if (rv != 1)
 98                 return -EINVAL;
 99 
100         if (sockfd != -1) {
101                 int err;
102                 dev_info(dev, "stub up\n");
103 
104                 spin_lock_irq(&sdev->ud.lock);
105 
106                 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
107                         dev_err(dev, "not ready\n");
108                         goto err;
109                 }
110 
111                 socket = sockfd_lookup(sockfd, &err);
112                 if (!socket)
113                         goto err;
114 
115                 sdev->ud.tcp_socket = socket;
116 
117                 spin_unlock_irq(&sdev->ud.lock);
118 
119                 sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud,
120                                                   "stub_rx");
121                 sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud,
122                                                   "stub_tx");
123 
124                 spin_lock_irq(&sdev->ud.lock);
125                 sdev->ud.status = SDEV_ST_USED;
126                 spin_unlock_irq(&sdev->ud.lock);
127 
128         } else {
129                 dev_info(dev, "stub down\n");
130 
131                 spin_lock_irq(&sdev->ud.lock);
132                 if (sdev->ud.status != SDEV_ST_USED)
133                         goto err;
134 
135                 spin_unlock_irq(&sdev->ud.lock);
136 
137                 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
138         }
139 
140         return count;
141 
142 err:
143         spin_unlock_irq(&sdev->ud.lock);
144         return -EINVAL;
145 }
146 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
147 
148 static int stub_add_files(struct device *dev)
149 {
150         int err = 0;
151 
152         err = device_create_file(dev, &dev_attr_usbip_status);
153         if (err)
154                 goto err_status;
155 
156         err = device_create_file(dev, &dev_attr_usbip_sockfd);
157         if (err)
158                 goto err_sockfd;
159 
160         err = device_create_file(dev, &dev_attr_usbip_debug);
161         if (err)
162                 goto err_debug;
163 
164         return 0;
165 
166 err_debug:
167         device_remove_file(dev, &dev_attr_usbip_sockfd);
168 err_sockfd:
169         device_remove_file(dev, &dev_attr_usbip_status);
170 err_status:
171         return err;
172 }
173 
174 static void stub_remove_files(struct device *dev)
175 {
176         device_remove_file(dev, &dev_attr_usbip_status);
177         device_remove_file(dev, &dev_attr_usbip_sockfd);
178         device_remove_file(dev, &dev_attr_usbip_debug);
179 }
180 
181 static void stub_shutdown_connection(struct usbip_device *ud)
182 {
183         struct stub_device *sdev = container_of(ud, struct stub_device, ud);
184 
185         /*
186          * When removing an exported device, kernel panic sometimes occurred
187          * and then EIP was sk_wait_data of stub_rx thread. Is this because
188          * sk_wait_data returned though stub_rx thread was already finished by
189          * step 1?
190          */
191         if (ud->tcp_socket) {
192                 dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n",
193                         ud->tcp_socket);
194                 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
195         }
196 
197         /* 1. stop threads */
198         if (ud->tcp_rx) {
199                 kthread_stop_put(ud->tcp_rx);
200                 ud->tcp_rx = NULL;
201         }
202         if (ud->tcp_tx) {
203                 kthread_stop_put(ud->tcp_tx);
204                 ud->tcp_tx = NULL;
205         }
206 
207         /*
208          * 2. close the socket
209          *
210          * tcp_socket is freed after threads are killed so that usbip_xmit does
211          * not touch NULL socket.
212          */
213         if (ud->tcp_socket) {
214                 sockfd_put(ud->tcp_socket);
215                 ud->tcp_socket = NULL;
216         }
217 
218         /* 3. free used data */
219         stub_device_cleanup_urbs(sdev);
220 
221         /* 4. free stub_unlink */
222         {
223                 unsigned long flags;
224                 struct stub_unlink *unlink, *tmp;
225 
226                 spin_lock_irqsave(&sdev->priv_lock, flags);
227                 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
228                         list_del(&unlink->list);
229                         kfree(unlink);
230                 }
231                 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
232                                          list) {
233                         list_del(&unlink->list);
234                         kfree(unlink);
235                 }
236                 spin_unlock_irqrestore(&sdev->priv_lock, flags);
237         }
238 }
239 
240 static void stub_device_reset(struct usbip_device *ud)
241 {
242         struct stub_device *sdev = container_of(ud, struct stub_device, ud);
243         struct usb_device *udev = sdev->udev;
244         int ret;
245 
246         dev_dbg(&udev->dev, "device reset");
247 
248         ret = usb_lock_device_for_reset(udev, sdev->interface);
249         if (ret < 0) {
250                 dev_err(&udev->dev, "lock for reset\n");
251                 spin_lock_irq(&ud->lock);
252                 ud->status = SDEV_ST_ERROR;
253                 spin_unlock_irq(&ud->lock);
254                 return;
255         }
256 
257         /* try to reset the device */
258         ret = usb_reset_device(udev);
259         usb_unlock_device(udev);
260 
261         spin_lock_irq(&ud->lock);
262         if (ret) {
263                 dev_err(&udev->dev, "device reset\n");
264                 ud->status = SDEV_ST_ERROR;
265         } else {
266                 dev_info(&udev->dev, "device reset\n");
267                 ud->status = SDEV_ST_AVAILABLE;
268         }
269         spin_unlock_irq(&ud->lock);
270 }
271 
272 static void stub_device_unusable(struct usbip_device *ud)
273 {
274         spin_lock_irq(&ud->lock);
275         ud->status = SDEV_ST_ERROR;
276         spin_unlock_irq(&ud->lock);
277 }
278 
279 /**
280  * stub_device_alloc - allocate a new stub_device struct
281  * @interface: usb_interface of a new device
282  *
283  * Allocates and initializes a new stub_device struct.
284  */
285 static struct stub_device *stub_device_alloc(struct usb_device *udev)
286 {
287         struct stub_device *sdev;
288         int busnum = udev->bus->busnum;
289         int devnum = udev->devnum;
290 
291         dev_dbg(&udev->dev, "allocating stub device");
292 
293         /* yes, it's a new device */
294         sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
295         if (!sdev)
296                 return NULL;
297 
298         sdev->udev = usb_get_dev(udev);
299 
300         /*
301          * devid is defined with devnum when this driver is first allocated.
302          * devnum may change later if a device is reset. However, devid never
303          * changes during a usbip connection.
304          */
305         sdev->devid             = (busnum << 16) | devnum;
306         sdev->ud.side           = USBIP_STUB;
307         sdev->ud.status         = SDEV_ST_AVAILABLE;
308         spin_lock_init(&sdev->ud.lock);
309         sdev->ud.tcp_socket     = NULL;
310 
311         INIT_LIST_HEAD(&sdev->priv_init);
312         INIT_LIST_HEAD(&sdev->priv_tx);
313         INIT_LIST_HEAD(&sdev->priv_free);
314         INIT_LIST_HEAD(&sdev->unlink_free);
315         INIT_LIST_HEAD(&sdev->unlink_tx);
316         spin_lock_init(&sdev->priv_lock);
317 
318         init_waitqueue_head(&sdev->tx_waitq);
319 
320         sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
321         sdev->ud.eh_ops.reset    = stub_device_reset;
322         sdev->ud.eh_ops.unusable = stub_device_unusable;
323 
324         usbip_start_eh(&sdev->ud);
325 
326         dev_dbg(&udev->dev, "register new device\n");
327 
328         return sdev;
329 }
330 
331 static void stub_device_free(struct stub_device *sdev)
332 {
333         kfree(sdev);
334 }
335 
336 static int stub_probe(struct usb_device *udev)
337 {
338         struct stub_device *sdev = NULL;
339         const char *udev_busid = dev_name(&udev->dev);
340         int err = 0;
341         struct bus_id_priv *busid_priv;
342         int rc;
343 
344         dev_dbg(&udev->dev, "Enter\n");
345 
346         /* check we should claim or not by busid_table */
347         busid_priv = get_busid_priv(udev_busid);
348         if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
349             (busid_priv->status == STUB_BUSID_OTHER)) {
350                 dev_info(&udev->dev,
351                         "%s is not in match_busid table... skip!\n",
352                         udev_busid);
353 
354                 /*
355                  * Return value should be ENODEV or ENOXIO to continue trying
356                  * other matched drivers by the driver core.
357                  * See driver_probe_device() in driver/base/dd.c
358                  */
359                 return -ENODEV;
360         }
361 
362         if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
363                 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
364                          udev_busid);
365                 return -ENODEV;
366         }
367 
368         if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
369                 dev_dbg(&udev->dev,
370                         "%s is attached on vhci_hcd... skip!\n",
371                         udev_busid);
372 
373                 return -ENODEV;
374         }
375 
376         /* ok, this is my device */
377         sdev = stub_device_alloc(udev);
378         if (!sdev)
379                 return -ENOMEM;
380 
381         dev_info(&udev->dev,
382                 "usbip-host: register new device (bus %u dev %u)\n",
383                 udev->bus->busnum, udev->devnum);
384 
385         busid_priv->shutdown_busid = 0;
386 
387         /* set private data to usb_device */
388         dev_set_drvdata(&udev->dev, sdev);
389         busid_priv->sdev = sdev;
390         busid_priv->udev = udev;
391 
392         /*
393          * Claim this hub port.
394          * It doesn't matter what value we pass as owner
395          * (struct dev_state) as long as it is unique.
396          */
397         rc = usb_hub_claim_port(udev->parent, udev->portnum,
398                         (struct usb_dev_state *) udev);
399         if (rc) {
400                 dev_dbg(&udev->dev, "unable to claim port\n");
401                 return rc;
402         }
403 
404         err = stub_add_files(&udev->dev);
405         if (err) {
406                 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
407                 dev_set_drvdata(&udev->dev, NULL);
408                 usb_put_dev(udev);
409                 kthread_stop_put(sdev->ud.eh);
410 
411                 busid_priv->sdev = NULL;
412                 stub_device_free(sdev);
413                 return err;
414         }
415         busid_priv->status = STUB_BUSID_ALLOC;
416 
417         return 0;
418 }
419 
420 static void shutdown_busid(struct bus_id_priv *busid_priv)
421 {
422         if (busid_priv->sdev && !busid_priv->shutdown_busid) {
423                 busid_priv->shutdown_busid = 1;
424                 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
425 
426                 /* wait for the stop of the event handler */
427                 usbip_stop_eh(&busid_priv->sdev->ud);
428         }
429 }
430 
431 /*
432  * called in usb_disconnect() or usb_deregister()
433  * but only if actconfig(active configuration) exists
434  */
435 static void stub_disconnect(struct usb_device *udev)
436 {
437         struct stub_device *sdev;
438         const char *udev_busid = dev_name(&udev->dev);
439         struct bus_id_priv *busid_priv;
440         int rc;
441 
442         dev_dbg(&udev->dev, "Enter\n");
443 
444         busid_priv = get_busid_priv(udev_busid);
445         if (!busid_priv) {
446                 BUG();
447                 return;
448         }
449 
450         sdev = dev_get_drvdata(&udev->dev);
451 
452         /* get stub_device */
453         if (!sdev) {
454                 dev_err(&udev->dev, "could not get device");
455                 return;
456         }
457 
458         dev_set_drvdata(&udev->dev, NULL);
459 
460         /*
461          * NOTE: rx/tx threads are invoked for each usb_device.
462          */
463         stub_remove_files(&udev->dev);
464 
465         /* release port */
466         rc = usb_hub_release_port(udev->parent, udev->portnum,
467                                   (struct usb_dev_state *) udev);
468         if (rc) {
469                 dev_dbg(&udev->dev, "unable to release port\n");
470                 return;
471         }
472 
473         /* If usb reset is called from event handler */
474         if (busid_priv->sdev->ud.eh == current)
475                 return;
476 
477         /* shutdown the current connection */
478         shutdown_busid(busid_priv);
479 
480         usb_put_dev(sdev->udev);
481 
482         /* free sdev */
483         busid_priv->sdev = NULL;
484         stub_device_free(sdev);
485 
486         if (busid_priv->status == STUB_BUSID_ALLOC) {
487                 busid_priv->status = STUB_BUSID_ADDED;
488         } else {
489                 busid_priv->status = STUB_BUSID_OTHER;
490                 del_match_busid((char *)udev_busid);
491         }
492 }
493 
494 #ifdef CONFIG_PM
495 
496 /* These functions need usb_port_suspend and usb_port_resume,
497  * which reside in drivers/usb/core/usb.h. Skip for now. */
498 
499 static int stub_suspend(struct usb_device *udev, pm_message_t message)
500 {
501         dev_dbg(&udev->dev, "stub_suspend\n");
502 
503         return 0;
504 }
505 
506 static int stub_resume(struct usb_device *udev, pm_message_t message)
507 {
508         dev_dbg(&udev->dev, "stub_resume\n");
509 
510         return 0;
511 }
512 
513 #endif  /* CONFIG_PM */
514 
515 struct usb_device_driver stub_driver = {
516         .name           = "usbip-host",
517         .probe          = stub_probe,
518         .disconnect     = stub_disconnect,
519 #ifdef CONFIG_PM
520         .suspend        = stub_suspend,
521         .resume         = stub_resume,
522 #endif
523         .supports_autosuspend   =       0,
524 };
525 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us