Version:  2.6.34 2.6.35 2.6.36 2.6.37 2.6.38 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

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         ssize_t err = -EINVAL;
 90 
 91         if (!sdev) {
 92                 dev_err(dev, "sdev is null\n");
 93                 return -ENODEV;
 94         }
 95 
 96         sscanf(buf, "%d", &sockfd);
 97 
 98         if (sockfd != -1) {
 99                 dev_info(dev, "stub up\n");
100 
101                 spin_lock_irq(&sdev->ud.lock);
102 
103                 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
104                         dev_err(dev, "not ready\n");
105                         goto err;
106                 }
107 
108                 socket = sockfd_to_socket(sockfd);
109                 if (!socket)
110                         goto err;
111 
112                 sdev->ud.tcp_socket = socket;
113 
114                 spin_unlock_irq(&sdev->ud.lock);
115 
116                 sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud,
117                                                   "stub_rx");
118                 sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud,
119                                                   "stub_tx");
120 
121                 spin_lock_irq(&sdev->ud.lock);
122                 sdev->ud.status = SDEV_ST_USED;
123                 spin_unlock_irq(&sdev->ud.lock);
124 
125         } else {
126                 dev_info(dev, "stub down\n");
127 
128                 spin_lock_irq(&sdev->ud.lock);
129                 if (sdev->ud.status != SDEV_ST_USED)
130                         goto err;
131 
132                 spin_unlock_irq(&sdev->ud.lock);
133 
134                 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
135         }
136 
137         return count;
138 
139 err:
140         spin_unlock_irq(&sdev->ud.lock);
141         return err;
142 }
143 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
144 
145 static int stub_add_files(struct device *dev)
146 {
147         int err = 0;
148 
149         err = device_create_file(dev, &dev_attr_usbip_status);
150         if (err)
151                 goto err_status;
152 
153         err = device_create_file(dev, &dev_attr_usbip_sockfd);
154         if (err)
155                 goto err_sockfd;
156 
157         err = device_create_file(dev, &dev_attr_usbip_debug);
158         if (err)
159                 goto err_debug;
160 
161         return 0;
162 
163 err_debug:
164         device_remove_file(dev, &dev_attr_usbip_sockfd);
165 err_sockfd:
166         device_remove_file(dev, &dev_attr_usbip_status);
167 err_status:
168         return err;
169 }
170 
171 static void stub_remove_files(struct device *dev)
172 {
173         device_remove_file(dev, &dev_attr_usbip_status);
174         device_remove_file(dev, &dev_attr_usbip_sockfd);
175         device_remove_file(dev, &dev_attr_usbip_debug);
176 }
177 
178 static void stub_shutdown_connection(struct usbip_device *ud)
179 {
180         struct stub_device *sdev = container_of(ud, struct stub_device, ud);
181 
182         /*
183          * When removing an exported device, kernel panic sometimes occurred
184          * and then EIP was sk_wait_data of stub_rx thread. Is this because
185          * sk_wait_data returned though stub_rx thread was already finished by
186          * step 1?
187          */
188         if (ud->tcp_socket) {
189                 dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n",
190                         ud->tcp_socket);
191                 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
192         }
193 
194         /* 1. stop threads */
195         if (ud->tcp_rx) {
196                 kthread_stop_put(ud->tcp_rx);
197                 ud->tcp_rx = NULL;
198         }
199         if (ud->tcp_tx) {
200                 kthread_stop_put(ud->tcp_tx);
201                 ud->tcp_tx = NULL;
202         }
203 
204         /*
205          * 2. close the socket
206          *
207          * tcp_socket is freed after threads are killed so that usbip_xmit does
208          * not touch NULL socket.
209          */
210         if (ud->tcp_socket) {
211                 fput(ud->tcp_socket->file);
212                 ud->tcp_socket = NULL;
213         }
214 
215         /* 3. free used data */
216         stub_device_cleanup_urbs(sdev);
217 
218         /* 4. free stub_unlink */
219         {
220                 unsigned long flags;
221                 struct stub_unlink *unlink, *tmp;
222 
223                 spin_lock_irqsave(&sdev->priv_lock, flags);
224                 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
225                         list_del(&unlink->list);
226                         kfree(unlink);
227                 }
228                 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
229                                          list) {
230                         list_del(&unlink->list);
231                         kfree(unlink);
232                 }
233                 spin_unlock_irqrestore(&sdev->priv_lock, flags);
234         }
235 }
236 
237 static void stub_device_reset(struct usbip_device *ud)
238 {
239         struct stub_device *sdev = container_of(ud, struct stub_device, ud);
240         struct usb_device *udev = sdev->udev;
241         int ret;
242 
243         dev_dbg(&udev->dev, "device reset");
244 
245         ret = usb_lock_device_for_reset(udev, sdev->interface);
246         if (ret < 0) {
247                 dev_err(&udev->dev, "lock for reset\n");
248                 spin_lock_irq(&ud->lock);
249                 ud->status = SDEV_ST_ERROR;
250                 spin_unlock_irq(&ud->lock);
251                 return;
252         }
253 
254         /* try to reset the device */
255         ret = usb_reset_device(udev);
256         usb_unlock_device(udev);
257 
258         spin_lock_irq(&ud->lock);
259         if (ret) {
260                 dev_err(&udev->dev, "device reset\n");
261                 ud->status = SDEV_ST_ERROR;
262         } else {
263                 dev_info(&udev->dev, "device reset\n");
264                 ud->status = SDEV_ST_AVAILABLE;
265         }
266         spin_unlock_irq(&ud->lock);
267 }
268 
269 static void stub_device_unusable(struct usbip_device *ud)
270 {
271         spin_lock_irq(&ud->lock);
272         ud->status = SDEV_ST_ERROR;
273         spin_unlock_irq(&ud->lock);
274 }
275 
276 /**
277  * stub_device_alloc - allocate a new stub_device struct
278  * @interface: usb_interface of a new device
279  *
280  * Allocates and initializes a new stub_device struct.
281  */
282 static struct stub_device *stub_device_alloc(struct usb_device *udev,
283                                              struct usb_interface *interface)
284 {
285         struct stub_device *sdev;
286         int busnum = interface_to_busnum(interface);
287         int devnum = interface_to_devnum(interface);
288 
289         dev_dbg(&interface->dev, "allocating stub device");
290 
291         /* yes, it's a new device */
292         sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
293         if (!sdev)
294                 return NULL;
295 
296         sdev->interface = usb_get_intf(interface);
297         sdev->udev = usb_get_dev(udev);
298 
299         /*
300          * devid is defined with devnum when this driver is first allocated.
301          * devnum may change later if a device is reset. However, devid never
302          * changes during a usbip connection.
303          */
304         sdev->devid             = (busnum << 16) | devnum;
305         sdev->ud.side           = USBIP_STUB;
306         sdev->ud.status         = SDEV_ST_AVAILABLE;
307         spin_lock_init(&sdev->ud.lock);
308         sdev->ud.tcp_socket     = NULL;
309 
310         INIT_LIST_HEAD(&sdev->priv_init);
311         INIT_LIST_HEAD(&sdev->priv_tx);
312         INIT_LIST_HEAD(&sdev->priv_free);
313         INIT_LIST_HEAD(&sdev->unlink_free);
314         INIT_LIST_HEAD(&sdev->unlink_tx);
315         spin_lock_init(&sdev->priv_lock);
316 
317         init_waitqueue_head(&sdev->tx_waitq);
318 
319         sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
320         sdev->ud.eh_ops.reset    = stub_device_reset;
321         sdev->ud.eh_ops.unusable = stub_device_unusable;
322 
323         usbip_start_eh(&sdev->ud);
324 
325         dev_dbg(&interface->dev, "register new interface\n");
326 
327         return sdev;
328 }
329 
330 static void stub_device_free(struct stub_device *sdev)
331 {
332         kfree(sdev);
333 }
334 
335 /*
336  * If a usb device has multiple active interfaces, this driver is bound to all
337  * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
338  * active interface). Currently, a userland program must ensure that it
339  * looks at the usbip's sysfs entries of only the first active interface.
340  *
341  * TODO: use "struct usb_device_driver" to bind a usb device.
342  * However, it seems it is not fully supported in mainline kernel yet
343  * (2.6.19.2).
344  */
345 static int stub_probe(struct usb_interface *interface,
346                       const struct usb_device_id *id)
347 {
348         struct usb_device *udev = interface_to_usbdev(interface);
349         struct stub_device *sdev = NULL;
350         const char *udev_busid = dev_name(interface->dev.parent);
351         int err = 0;
352         struct bus_id_priv *busid_priv;
353 
354         dev_dbg(&interface->dev, "Enter\n");
355 
356         /* check we should claim or not by busid_table */
357         busid_priv = get_busid_priv(udev_busid);
358         if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
359             (busid_priv->status == STUB_BUSID_OTHER)) {
360                 dev_info(&interface->dev,
361                         "%s is not in match_busid table... skip!\n",
362                         udev_busid);
363 
364                 /*
365                  * Return value should be ENODEV or ENOXIO to continue trying
366                  * other matched drivers by the driver core.
367                  * See driver_probe_device() in driver/base/dd.c
368                  */
369                 return -ENODEV;
370         }
371 
372         if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
373                 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
374                          udev_busid);
375                 return -ENODEV;
376         }
377 
378         if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
379                 dev_dbg(&udev->dev,
380                         "%s is attached on vhci_hcd... skip!\n",
381                         udev_busid);
382 
383                 return -ENODEV;
384         }
385 
386         if (busid_priv->status == STUB_BUSID_ALLOC) {
387                 sdev = busid_priv->sdev;
388                 if (!sdev)
389                         return -ENODEV;
390 
391                 busid_priv->interf_count++;
392                 dev_info(&interface->dev,
393                         "usbip-host: register new interface (bus %u dev %u ifn %u)\n",
394                         udev->bus->busnum, udev->devnum,
395                         interface->cur_altsetting->desc.bInterfaceNumber);
396 
397                 /* set private data to usb_interface */
398                 usb_set_intfdata(interface, sdev);
399 
400                 err = stub_add_files(&interface->dev);
401                 if (err) {
402                         dev_err(&interface->dev, "stub_add_files for %s\n",
403                                 udev_busid);
404                         usb_set_intfdata(interface, NULL);
405                         busid_priv->interf_count--;
406                         return err;
407                 }
408 
409                 usb_get_intf(interface);
410                 return 0;
411         }
412 
413         /* ok, this is my device */
414         sdev = stub_device_alloc(udev, interface);
415         if (!sdev)
416                 return -ENOMEM;
417 
418         dev_info(&interface->dev,
419                 "usbip-host: register new device (bus %u dev %u ifn %u)\n",
420                 udev->bus->busnum, udev->devnum,
421                 interface->cur_altsetting->desc.bInterfaceNumber);
422 
423         busid_priv->interf_count = 0;
424         busid_priv->shutdown_busid = 0;
425 
426         /* set private data to usb_interface */
427         usb_set_intfdata(interface, sdev);
428         busid_priv->interf_count++;
429         busid_priv->sdev = sdev;
430 
431         err = stub_add_files(&interface->dev);
432         if (err) {
433                 dev_err(&interface->dev, "stub_add_files for %s\n", udev_busid);
434                 usb_set_intfdata(interface, NULL);
435                 usb_put_intf(interface);
436                 usb_put_dev(udev);
437                 kthread_stop_put(sdev->ud.eh);
438 
439                 busid_priv->interf_count = 0;
440                 busid_priv->sdev = NULL;
441                 stub_device_free(sdev);
442                 return err;
443         }
444         busid_priv->status = STUB_BUSID_ALLOC;
445 
446         return 0;
447 }
448 
449 static void shutdown_busid(struct bus_id_priv *busid_priv)
450 {
451         if (busid_priv->sdev && !busid_priv->shutdown_busid) {
452                 busid_priv->shutdown_busid = 1;
453                 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
454 
455                 /* wait for the stop of the event handler */
456                 usbip_stop_eh(&busid_priv->sdev->ud);
457         }
458 }
459 
460 /*
461  * called in usb_disconnect() or usb_deregister()
462  * but only if actconfig(active configuration) exists
463  */
464 static void stub_disconnect(struct usb_interface *interface)
465 {
466         struct stub_device *sdev;
467         const char *udev_busid = dev_name(interface->dev.parent);
468         struct bus_id_priv *busid_priv;
469 
470         dev_dbg(&interface->dev, "Enter\n");
471 
472         busid_priv = get_busid_priv(udev_busid);
473         if (!busid_priv) {
474                 BUG();
475                 return;
476         }
477 
478         sdev = usb_get_intfdata(interface);
479 
480         /* get stub_device */
481         if (!sdev) {
482                 dev_err(&interface->dev, "could not get device");
483                 return;
484         }
485 
486         usb_set_intfdata(interface, NULL);
487 
488         /*
489          * NOTE: rx/tx threads are invoked for each usb_device.
490          */
491         stub_remove_files(&interface->dev);
492 
493         /* If usb reset is called from event handler */
494         if (busid_priv->sdev->ud.eh == current) {
495                 busid_priv->interf_count--;
496                 return;
497         }
498 
499         if (busid_priv->interf_count > 1) {
500                 busid_priv->interf_count--;
501                 shutdown_busid(busid_priv);
502                 usb_put_intf(interface);
503                 return;
504         }
505 
506         busid_priv->interf_count = 0;
507 
508         /* shutdown the current connection */
509         shutdown_busid(busid_priv);
510 
511         usb_put_dev(sdev->udev);
512         usb_put_intf(interface);
513 
514         /* free sdev */
515         busid_priv->sdev = NULL;
516         stub_device_free(sdev);
517 
518         if (busid_priv->status == STUB_BUSID_ALLOC) {
519                 busid_priv->status = STUB_BUSID_ADDED;
520         } else {
521                 busid_priv->status = STUB_BUSID_OTHER;
522                 del_match_busid((char *)udev_busid);
523         }
524 }
525 
526 /*
527  * Presence of pre_reset and post_reset prevents the driver from being unbound
528  * when the device is being reset
529  */
530 
531 static int stub_pre_reset(struct usb_interface *interface)
532 {
533         dev_dbg(&interface->dev, "pre_reset\n");
534         return 0;
535 }
536 
537 static int stub_post_reset(struct usb_interface *interface)
538 {
539         dev_dbg(&interface->dev, "post_reset\n");
540         return 0;
541 }
542 
543 struct usb_driver stub_driver = {
544         .name           = "usbip-host",
545         .probe          = stub_probe,
546         .disconnect     = stub_disconnect,
547         .id_table       = stub_table,
548         .pre_reset      = stub_pre_reset,
549         .post_reset     = stub_post_reset,
550 };
551 

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