rename kernel-power folder to kernel-bfs.
[kernel-bfs] / kernel-bfs-2.6.28 / debian / patches / usbip.diff
1 --- kernel-2.6.28.orig/drivers/usb/Kconfig
2 +++ kernel-2.6.28/drivers/usb/Kconfig
3 @@ -153,4 +153,6 @@
4  
5  source "drivers/usb/otg/Kconfig"
6  
7 +source "drivers/usb/usbip/Kconfig"
8 +
9  endif # USB_SUPPORT
10 --- kernel-2.6.28.orig/drivers/usb/Makefile
11 +++ kernel-2.6.28/drivers/usb/Makefile
12 @@ -37,3 +37,5 @@
13  
14  obj-$(CONFIG_USB_ATM)          += atm/
15  obj-$(CONFIG_USB_SPEEDTOUCH)   += atm/
16 +
17 +obj-$(CONFIG_USB_IP_COMMON)    += usbip/
18 --- /dev/null
19 +++ kernel-2.6.28/drivers/usb/usbip/Kconfig
20 @@ -0,0 +1,36 @@
21 +config USB_IP_COMMON
22 +       tristate "USB IP support (EXPERIMENTAL)"
23 +       depends on USB && NET && EXPERIMENTAL
24 +       default N
25 +       ---help---
26 +         This enables pushing USB packets over IP to allow remote
27 +         machines access to USB devices directly.  For more details,
28 +         and links to the userspace utility programs to let this work
29 +         properly, see http://usbip.naist.jp/
30 +
31 +         To compile this driver as a module, choose M here: the
32 +         module will be called usbip_common_mod.
33 +
34 +         If unsure, say N.
35 +
36 +config USB_IP_VHCI_HCD
37 +       tristate "USB IP client driver"
38 +       depends on USB_IP_COMMON
39 +       default N
40 +       ---help---
41 +        This enables the USB IP host controller driver which will
42 +        run on the client machine.
43 +
44 +        To compile this driver as a module, choose M here: the
45 +        module will be called vhci_hcd.
46 +
47 +config USB_IP_HOST
48 +       tristate "USB IP host driver"
49 +       depends on USB_IP_COMMON
50 +       default N
51 +       ---help---
52 +        This enables the USB IP device driver which will run on the
53 +        host machine.
54 +
55 +        To compile this driver as a module, choose M here: the
56 +        module will be called usbip.
57 --- /dev/null
58 +++ kernel-2.6.28/drivers/usb/usbip/Makefile
59 @@ -0,0 +1,12 @@
60 +obj-$(CONFIG_USB_IP_COMMON) += usbip_common_mod.o
61 +usbip_common_mod-objs := usbip_common.o usbip_event.o
62 +
63 +obj-$(CONFIG_USB_IP_VHCI_HCD) += vhci-hcd.o
64 +vhci-hcd-objs := vhci_sysfs.o vhci_tx.o vhci_rx.o vhci_hcd.o
65 +
66 +obj-$(CONFIG_USB_IP_HOST) += usbip.o
67 +usbip-objs := stub_dev.o stub_main.o stub_rx.o stub_tx.o
68 +
69 +ifeq ($(CONFIG_USB_DEBUG),y)
70 +       EXTRA_CFLAGS += -DDEBUG
71 +endif
72 --- /dev/null
73 +++ kernel-2.6.28/drivers/usb/usbip/README
74 @@ -0,0 +1,6 @@
75 +TODO:
76 +       - more discussion about the protocol
77 +       - testing
78 +       - review of the userspace interface
79 +
80 +Please send patches for this code to Greg Kroah-Hartman <greg@kroah.com>
81 --- /dev/null
82 +++ kernel-2.6.28/drivers/usb/usbip/stub.h
83 @@ -0,0 +1,95 @@
84 +/*
85 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
86 + *
87 + * This is free software; you can redistribute it and/or modify
88 + * it under the terms of the GNU General Public License as published by
89 + * the Free Software Foundation; either version 2 of the License, or
90 + * (at your option) any later version.
91 + *
92 + * This is distributed in the hope that it will be useful,
93 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
94 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
95 + * GNU General Public License for more details.
96 + *
97 + * You should have received a copy of the GNU General Public License
98 + * along with this program; if not, write to the Free Software
99 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
100 + * USA.
101 + */
102 +
103 +#include <linux/kernel.h>
104 +#include <linux/list.h>
105 +#include <linux/spinlock.h>
106 +#include <linux/slab.h>
107 +#include <linux/string.h>
108 +#include <linux/module.h>
109 +#include <linux/net.h>
110 +
111 +struct stub_device {
112 +       struct usb_interface *interface;
113 +       struct list_head list;
114 +
115 +       struct usbip_device ud;
116 +       __u32 devid;
117 +
118 +       /*
119 +        * stub_priv preserves private data of each urb.
120 +        * It is allocated as stub_priv_cache and assigned to urb->context.
121 +        *
122 +        * stub_priv is always linked to any one of 3 lists;
123 +        *      priv_init: linked to this until the comletion of a urb.
124 +        *      priv_tx  : linked to this after the completion of a urb.
125 +        *      priv_free: linked to this after the sending of the result.
126 +        *
127 +        * Any of these list operations should be locked by priv_lock.
128 +        */
129 +       spinlock_t priv_lock;
130 +       struct list_head priv_init;
131 +       struct list_head priv_tx;
132 +       struct list_head priv_free;
133 +
134 +       /* see comments for unlinking in stub_rx.c */
135 +       struct list_head unlink_tx;
136 +       struct list_head unlink_free;
137 +
138 +
139 +       wait_queue_head_t tx_waitq;
140 +};
141 +
142 +/* private data into urb->priv */
143 +struct stub_priv {
144 +       unsigned long seqnum;
145 +       struct list_head list;
146 +       struct stub_device *sdev;
147 +       struct urb *urb;
148 +
149 +       int unlinking;
150 +};
151 +
152 +struct stub_unlink {
153 +       unsigned long seqnum;
154 +       struct list_head list;
155 +       __u32 status;
156 +};
157 +
158 +
159 +extern struct kmem_cache *stub_priv_cache;
160 +
161 +
162 +/*-------------------------------------------------------------------------*/
163 +/* prototype declarations */
164 +
165 +/* stub_tx.c */
166 +void stub_complete(struct urb *);
167 +void stub_tx_loop(struct usbip_task *);
168 +
169 +/* stub_dev.c */
170 +extern struct usb_driver stub_driver;
171 +
172 +/* stub_rx.c */
173 +void stub_rx_loop(struct usbip_task *);
174 +void stub_enqueue_ret_unlink(struct stub_device *, __u32, __u32);
175 +
176 +/* stub_main.c */
177 +int match_busid(char *busid);
178 +void stub_device_cleanup_urbs(struct stub_device *sdev);
179 --- /dev/null
180 +++ kernel-2.6.28/drivers/usb/usbip/stub_dev.c
181 @@ -0,0 +1,483 @@
182 +/*
183 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
184 + *
185 + * This is free software; you can redistribute it and/or modify
186 + * it under the terms of the GNU General Public License as published by
187 + * the Free Software Foundation; either version 2 of the License, or
188 + * (at your option) any later version.
189 + *
190 + * This is distributed in the hope that it will be useful,
191 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
192 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
193 + * GNU General Public License for more details.
194 + *
195 + * You should have received a copy of the GNU General Public License
196 + * along with this program; if not, write to the Free Software
197 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
198 + * USA.
199 + */
200 +
201 +#include "usbip_common.h"
202 +#include "stub.h"
203 +
204 +
205 +
206 +static int stub_probe(struct usb_interface *interface,
207 +                               const struct usb_device_id *id);
208 +static void stub_disconnect(struct usb_interface *interface);
209 +
210 +
211 +/*
212 + * Define device IDs here if you want to explicitly limit exportable devices.
213 + * In the most cases, wild card matching will be ok because driver binding can
214 + * be changed dynamically by a userland program.
215 + */
216 +static struct usb_device_id stub_table[] = {
217 +#if 0
218 +       /* just an example */
219 +       { USB_DEVICE(0x05ac, 0x0301) },   /* Mac 1 button mouse */
220 +       { USB_DEVICE(0x0430, 0x0009) },   /* Plat Home Keyboard */
221 +       { USB_DEVICE(0x059b, 0x0001) },   /* Iomega USB Zip 100 */
222 +       { USB_DEVICE(0x04b3, 0x4427) },   /* IBM USB CD-ROM */
223 +       { USB_DEVICE(0x05a9, 0xa511) },   /* LifeView USB cam */
224 +       { USB_DEVICE(0x55aa, 0x0201) },   /* Imation card reader */
225 +       { USB_DEVICE(0x046d, 0x0870) },   /* Qcam Express(QV-30) */
226 +       { USB_DEVICE(0x04bb, 0x0101) },   /* IO-DATA HD 120GB */
227 +       { USB_DEVICE(0x04bb, 0x0904) },   /* IO-DATA USB-ET/TX */
228 +       { USB_DEVICE(0x04bb, 0x0201) },   /* IO-DATA USB-ET/TX */
229 +       { USB_DEVICE(0x08bb, 0x2702) },   /* ONKYO USB Speaker */
230 +       { USB_DEVICE(0x046d, 0x08b2) },   /* Logicool Qcam 4000 Pro */
231 +#endif
232 +       /* magic for wild card */
233 +       { .driver_info = 1 },
234 +       { 0, }                                     /* Terminating entry */
235 +};
236 +MODULE_DEVICE_TABLE(usb, stub_table);
237 +
238 +struct usb_driver stub_driver = {
239 +       .name           = "usbip",
240 +       .probe          = stub_probe,
241 +       .disconnect     = stub_disconnect,
242 +       .id_table       = stub_table,
243 +};
244 +
245 +
246 +/*-------------------------------------------------------------------------*/
247 +
248 +/* Define sysfs entries for a usbip-bound device */
249 +
250 +
251 +/*
252 + * usbip_status shows status of usbip as long as this driver is bound to the
253 + * target device.
254 + */
255 +static ssize_t show_status(struct device *dev, struct device_attribute *attr,
256 +                          char *buf)
257 +{
258 +       struct stub_device *sdev = dev_get_drvdata(dev);
259 +       int status;
260 +
261 +       if (!sdev) {
262 +               dev_err(dev, "sdev is null\n");
263 +               return -ENODEV;
264 +       }
265 +
266 +       spin_lock(&sdev->ud.lock);
267 +       status = sdev->ud.status;
268 +       spin_unlock(&sdev->ud.lock);
269 +
270 +       return snprintf(buf, PAGE_SIZE, "%d\n", status);
271 +}
272 +static DEVICE_ATTR(usbip_status, S_IRUGO, show_status, NULL);
273 +
274 +/*
275 + * usbip_sockfd gets a socket descriptor of an established TCP connection that
276 + * is used to transfer usbip requests by kernel threads. -1 is a magic number
277 + * by which usbip connection is finished.
278 + */
279 +static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
280 +                           const char *buf, size_t count)
281 +{
282 +       struct stub_device *sdev = dev_get_drvdata(dev);
283 +       int sockfd = 0;
284 +       struct socket *socket;
285 +
286 +       if (!sdev) {
287 +               dev_err(dev, "sdev is null\n");
288 +               return -ENODEV;
289 +       }
290 +
291 +       sscanf(buf, "%d", &sockfd);
292 +
293 +       if (sockfd != -1) {
294 +               dev_info(dev, "stub up\n");
295 +
296 +               spin_lock(&sdev->ud.lock);
297 +
298 +               if (sdev->ud.status != SDEV_ST_AVAILABLE) {
299 +                       dev_err(dev, "not ready\n");
300 +                       spin_unlock(&sdev->ud.lock);
301 +                       return -EINVAL;
302 +               }
303 +
304 +               socket = sockfd_to_socket(sockfd);
305 +               if (!socket) {
306 +                       spin_unlock(&sdev->ud.lock);
307 +                       return -EINVAL;
308 +               }
309 +
310 +#if 0
311 +               setnodelay(socket);
312 +               setkeepalive(socket);
313 +               setreuse(socket);
314 +#endif
315 +
316 +               sdev->ud.tcp_socket = socket;
317 +
318 +               spin_unlock(&sdev->ud.lock);
319 +
320 +               usbip_start_threads(&sdev->ud);
321 +
322 +               spin_lock(&sdev->ud.lock);
323 +               sdev->ud.status = SDEV_ST_USED;
324 +               spin_unlock(&sdev->ud.lock);
325 +
326 +       } else {
327 +               dev_info(dev, "stub down\n");
328 +
329 +               spin_lock(&sdev->ud.lock);
330 +               if (sdev->ud.status != SDEV_ST_USED) {
331 +                       spin_unlock(&sdev->ud.lock);
332 +                       return -EINVAL;
333 +               }
334 +               spin_unlock(&sdev->ud.lock);
335 +
336 +               usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
337 +       }
338 +
339 +       return count;
340 +}
341 +static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
342 +
343 +static int stub_add_files(struct device *dev)
344 +{
345 +       int err = 0;
346 +
347 +       err = device_create_file(dev, &dev_attr_usbip_status);
348 +       if (err)
349 +               goto err_status;
350 +
351 +       err = device_create_file(dev, &dev_attr_usbip_sockfd);
352 +       if (err)
353 +               goto err_sockfd;
354 +
355 +       err = device_create_file(dev, &dev_attr_usbip_debug);
356 +       if (err)
357 +               goto err_debug;
358 +
359 +       return 0;
360 +
361 +err_debug:
362 +       device_remove_file(dev, &dev_attr_usbip_sockfd);
363 +
364 +err_sockfd:
365 +       device_remove_file(dev, &dev_attr_usbip_status);
366 +
367 +err_status:
368 +       return err;
369 +}
370 +
371 +static void stub_remove_files(struct device *dev)
372 +{
373 +       device_remove_file(dev, &dev_attr_usbip_status);
374 +       device_remove_file(dev, &dev_attr_usbip_sockfd);
375 +       device_remove_file(dev, &dev_attr_usbip_debug);
376 +}
377 +
378 +
379 +
380 +/*-------------------------------------------------------------------------*/
381 +
382 +/* Event handler functions called by an event handler thread */
383 +
384 +static void stub_shutdown_connection(struct usbip_device *ud)
385 +{
386 +       struct stub_device *sdev = container_of(ud, struct stub_device, ud);
387 +
388 +       /*
389 +        * When removing an exported device, kernel panic sometimes occurred
390 +        * and then EIP was sk_wait_data of stub_rx thread. Is this because
391 +        * sk_wait_data returned though stub_rx thread was already finished by
392 +        * step 1?
393 +        */
394 +       if (ud->tcp_socket) {
395 +               udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
396 +               kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
397 +       }
398 +
399 +       /* 1. stop threads */
400 +       usbip_stop_threads(ud);
401 +
402 +       /* 2. close the socket */
403 +       /*
404 +        * tcp_socket is freed after threads are killed.
405 +        * So usbip_xmit do not touch NULL socket.
406 +        */
407 +       if (ud->tcp_socket) {
408 +               sock_release(ud->tcp_socket);
409 +               ud->tcp_socket = NULL;
410 +       }
411 +
412 +       /* 3. free used data */
413 +       stub_device_cleanup_urbs(sdev);
414 +
415 +       /* 4. free stub_unlink */
416 +       {
417 +               unsigned long flags;
418 +               struct stub_unlink *unlink, *tmp;
419 +
420 +               spin_lock_irqsave(&sdev->priv_lock, flags);
421 +
422 +               list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
423 +                       list_del(&unlink->list);
424 +                       kfree(unlink);
425 +               }
426 +
427 +               list_for_each_entry_safe(unlink, tmp,
428 +                                                &sdev->unlink_free, list) {
429 +                       list_del(&unlink->list);
430 +                       kfree(unlink);
431 +               }
432 +
433 +               spin_unlock_irqrestore(&sdev->priv_lock, flags);
434 +       }
435 +}
436 +
437 +static void stub_device_reset(struct usbip_device *ud)
438 +{
439 +       struct stub_device *sdev = container_of(ud, struct stub_device, ud);
440 +       struct usb_device *udev = interface_to_usbdev(sdev->interface);
441 +       int ret;
442 +
443 +       udbg("device reset");
444 +       ret = usb_lock_device_for_reset(udev, sdev->interface);
445 +       if (ret < 0) {
446 +               dev_err(&udev->dev, "lock for reset\n");
447 +
448 +               spin_lock(&ud->lock);
449 +               ud->status = SDEV_ST_ERROR;
450 +               spin_unlock(&ud->lock);
451 +
452 +               return;
453 +       }
454 +
455 +       /* try to reset the device */
456 +       ret = usb_reset_device(udev);
457 +
458 +       usb_unlock_device(udev);
459 +
460 +       spin_lock(&ud->lock);
461 +       if (ret) {
462 +               dev_err(&udev->dev, "device reset\n");
463 +               ud->status = SDEV_ST_ERROR;
464 +
465 +       } else {
466 +               dev_info(&udev->dev, "device reset\n");
467 +               ud->status = SDEV_ST_AVAILABLE;
468 +
469 +       }
470 +       spin_unlock(&ud->lock);
471 +
472 +       return;
473 +}
474 +
475 +static void stub_device_unusable(struct usbip_device *ud)
476 +{
477 +       spin_lock(&ud->lock);
478 +       ud->status = SDEV_ST_ERROR;
479 +       spin_unlock(&ud->lock);
480 +}
481 +
482 +
483 +/*-------------------------------------------------------------------------*/
484 +
485 +/**
486 + * stub_device_alloc - allocate a new stub_device struct
487 + * @interface: usb_interface of a new device
488 + *
489 + * Allocates and initializes a new stub_device struct.
490 + */
491 +static struct stub_device *stub_device_alloc(struct usb_interface *interface)
492 +{
493 +       struct stub_device *sdev;
494 +       int busnum = interface_to_busnum(interface);
495 +       int devnum = interface_to_devnum(interface);
496 +
497 +       dev_dbg(&interface->dev, "allocating stub device");
498 +
499 +       /* yes, it's a new device */
500 +       sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
501 +       if (!sdev) {
502 +               dev_err(&interface->dev, "no memory for stub_device\n");
503 +               return NULL;
504 +       }
505 +
506 +       sdev->interface = interface;
507 +
508 +       /*
509 +        * devid is defined with devnum when this driver is first allocated.
510 +        * devnum may change later if a device is reset. However, devid never
511 +        * changes during a usbip connection.
512 +        */
513 +       sdev->devid     = (busnum << 16) | devnum;
514 +
515 +       usbip_task_init(&sdev->ud.tcp_rx, "stub_rx", stub_rx_loop);
516 +       usbip_task_init(&sdev->ud.tcp_tx, "stub_tx", stub_tx_loop);
517 +
518 +       sdev->ud.side = USBIP_STUB;
519 +       sdev->ud.status = SDEV_ST_AVAILABLE;
520 +       /* sdev->ud.lock = SPIN_LOCK_UNLOCKED; */
521 +       spin_lock_init(&sdev->ud.lock);
522 +       sdev->ud.tcp_socket = NULL;
523 +
524 +       INIT_LIST_HEAD(&sdev->priv_init);
525 +       INIT_LIST_HEAD(&sdev->priv_tx);
526 +       INIT_LIST_HEAD(&sdev->priv_free);
527 +       INIT_LIST_HEAD(&sdev->unlink_free);
528 +       INIT_LIST_HEAD(&sdev->unlink_tx);
529 +       /* sdev->priv_lock = SPIN_LOCK_UNLOCKED; */
530 +       spin_lock_init(&sdev->priv_lock);
531 +
532 +       init_waitqueue_head(&sdev->tx_waitq);
533 +
534 +       sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
535 +       sdev->ud.eh_ops.reset    = stub_device_reset;
536 +       sdev->ud.eh_ops.unusable = stub_device_unusable;
537 +
538 +       usbip_start_eh(&sdev->ud);
539 +
540 +       udbg("register new interface\n");
541 +       return sdev;
542 +}
543 +
544 +static int stub_device_free(struct stub_device *sdev)
545 +{
546 +       if (!sdev)
547 +               return -EINVAL;
548 +
549 +       kfree(sdev);
550 +       udbg("kfree udev ok\n");
551 +
552 +       return 0;
553 +}
554 +
555 +
556 +/*-------------------------------------------------------------------------*/
557 +
558 +/*
559 + * If a usb device has multiple active interfaces, this driver is bound to all
560 + * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
561 + * active interface). Currently, a userland program must ensure that it
562 + * looks at the usbip's sysfs entries of only the first active interface.
563 + *
564 + * TODO: use "struct usb_device_driver" to bind a usb device.
565 + * However, it seems it is not fully supported in mainline kernel yet
566 + * (2.6.19.2).
567 + */
568 +static int stub_probe(struct usb_interface *interface,
569 +                     const struct usb_device_id *id)
570 +{
571 +       struct usb_device *udev = interface_to_usbdev(interface);
572 +       struct stub_device *sdev = NULL;
573 +       char *udev_busid = interface->dev.parent->bus_id;
574 +       int err = 0;
575 +
576 +       dev_dbg(&interface->dev, "Enter\n");
577 +
578 +       /* check we should claim or not by busid_table */
579 +       if (match_busid(udev_busid)) {
580 +               dev_info(&interface->dev,
581 +                        "this device %s is not in match_busid table. skip!\n",
582 +                        udev_busid);
583 +
584 +               /*
585 +                * Return value should be ENODEV or ENOXIO to continue trying
586 +                * other matched drivers by the driver core.
587 +                * See driver_probe_device() in driver/base/dd.c
588 +                */
589 +               return -ENODEV;
590 +       }
591 +
592 +       if (udev->descriptor.bDeviceClass ==  USB_CLASS_HUB) {
593 +               udbg("this device %s is a usb hub device. skip!\n",
594 +                                                               udev_busid);
595 +               return -ENODEV;
596 +       }
597 +
598 +       if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
599 +               udbg("this device %s is attached on vhci_hcd. skip!\n",
600 +                                                               udev_busid);
601 +               return -ENODEV;
602 +       }
603 +
604 +       /* ok. this is my device. */
605 +       sdev = stub_device_alloc(interface);
606 +       if (!sdev)
607 +               return -ENOMEM;
608 +
609 +       dev_info(&interface->dev, "USB/IP Stub: register a new interface "
610 +                "(bus %u dev %u ifn %u)\n", udev->bus->busnum, udev->devnum,
611 +                interface->cur_altsetting->desc.bInterfaceNumber);
612 +
613 +       /* set private data to usb_interface */
614 +       usb_set_intfdata(interface, sdev);
615 +
616 +       err = stub_add_files(&interface->dev);
617 +       if (err) {
618 +               dev_err(&interface->dev, "create sysfs files for %s\n",
619 +                       udev_busid);
620 +               return err;
621 +       }
622 +
623 +       return 0;
624 +}
625 +
626 +
627 +/*
628 + * called in usb_disconnect() or usb_deregister()
629 + * but only if actconfig(active configuration) exists
630 + */
631 +static void stub_disconnect(struct usb_interface *interface)
632 +{
633 +       struct stub_device *sdev = usb_get_intfdata(interface);
634 +
635 +       udbg("Enter\n");
636 +
637 +       /* get stub_device */
638 +       if (!sdev) {
639 +               err(" could not get device from inteface data");
640 +               /* BUG(); */
641 +               return;
642 +       }
643 +
644 +       usb_set_intfdata(interface, NULL);
645 +
646 +
647 +       /*
648 +        * NOTE:
649 +        * rx/tx threads are invoked for each usb_device.
650 +        */
651 +       stub_remove_files(&interface->dev);
652 +
653 +       /* 1. shutdown the current connection */
654 +       usbip_event_add(&sdev->ud, SDEV_EVENT_REMOVED);
655 +
656 +       /* 2. wait for the stop of the event handler */
657 +       usbip_stop_eh(&sdev->ud);
658 +
659 +       /* 3. free sdev */
660 +       stub_device_free(sdev);
661 +
662 +
663 +       udbg("bye\n");
664 +}
665 --- /dev/null
666 +++ kernel-2.6.28/drivers/usb/usbip/stub_main.c
667 @@ -0,0 +1,300 @@
668 +/*
669 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
670 + *
671 + * This is free software; you can redistribute it and/or modify
672 + * it under the terms of the GNU General Public License as published by
673 + * the Free Software Foundation; either version 2 of the License, or
674 + * (at your option) any later version.
675 + *
676 + * This is distributed in the hope that it will be useful,
677 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
678 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
679 + * GNU General Public License for more details.
680 + *
681 + * You should have received a copy of the GNU General Public License
682 + * along with this program; if not, write to the Free Software
683 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
684 + * USA.
685 + */
686 +
687 +
688 +#include "usbip_common.h"
689 +#include "stub.h"
690 +
691 +/* Version Information */
692 +#define DRIVER_VERSION "1.0"
693 +#define DRIVER_AUTHOR "Takahiro Hirofuchi"
694 +#define DRIVER_DESC "Stub Driver for USB/IP"
695 +
696 +/* stub_priv is allocated from stub_priv_cache */
697 +struct kmem_cache *stub_priv_cache;
698 +
699 +/*-------------------------------------------------------------------------*/
700 +
701 +/* Define sysfs entries for the usbip driver */
702 +
703 +
704 +/*
705 + * busid_tables defines matching busids that usbip can grab. A user can change
706 + * dynamically what device is locally used and what device is exported to a
707 + * remote host.
708 + */
709 +#define MAX_BUSID 16
710 +static char busid_table[MAX_BUSID][BUS_ID_SIZE];
711 +static spinlock_t busid_table_lock;
712 +
713 +
714 +int match_busid(char *busid)
715 +{
716 +       int i;
717 +
718 +       spin_lock(&busid_table_lock);
719 +
720 +       for (i = 0; i < MAX_BUSID; i++)
721 +               if (busid_table[i][0])
722 +                       if (!strncmp(busid_table[i], busid, BUS_ID_SIZE)) {
723 +                               /* already registerd */
724 +                               spin_unlock(&busid_table_lock);
725 +                               return 0;
726 +                       }
727 +
728 +       spin_unlock(&busid_table_lock);
729 +
730 +       return 1;
731 +}
732 +
733 +static ssize_t show_match_busid(struct device_driver *drv, char *buf)
734 +{
735 +       int i;
736 +       char *out = buf;
737 +
738 +       spin_lock(&busid_table_lock);
739 +
740 +       for (i = 0; i < MAX_BUSID; i++)
741 +               if (busid_table[i][0])
742 +                       out += sprintf(out, "%s ", busid_table[i]);
743 +
744 +       spin_unlock(&busid_table_lock);
745 +
746 +       out += sprintf(out, "\n");
747 +
748 +       return out - buf;
749 +}
750 +
751 +static int add_match_busid(char *busid)
752 +{
753 +       int i;
754 +
755 +       if (!match_busid(busid))
756 +               return 0;
757 +
758 +       spin_lock(&busid_table_lock);
759 +
760 +       for (i = 0; i < MAX_BUSID; i++)
761 +               if (!busid_table[i][0]) {
762 +                       strncpy(busid_table[i], busid, BUS_ID_SIZE);
763 +                       spin_unlock(&busid_table_lock);
764 +                       return 0;
765 +               }
766 +
767 +       spin_unlock(&busid_table_lock);
768 +
769 +       return -1;
770 +}
771 +
772 +static int del_match_busid(char *busid)
773 +{
774 +       int i;
775 +
776 +       spin_lock(&busid_table_lock);
777 +
778 +       for (i = 0; i < MAX_BUSID; i++)
779 +               if (!strncmp(busid_table[i], busid, BUS_ID_SIZE)) {
780 +                       /* found */
781 +                       memset(busid_table[i], 0, BUS_ID_SIZE);
782 +                       spin_unlock(&busid_table_lock);
783 +                       return 0;
784 +               }
785 +
786 +       spin_unlock(&busid_table_lock);
787 +
788 +       return -1;
789 +}
790 +
791 +static ssize_t store_match_busid(struct device_driver *dev, const char *buf,
792 +               size_t count)
793 +{
794 +       int len;
795 +       char busid[BUS_ID_SIZE];
796 +
797 +       if (count < 5)
798 +               return -EINVAL;
799 +
800 +       /* strnlen() does not include \0 */
801 +       len = strnlen(buf + 4, BUS_ID_SIZE);
802 +
803 +       /* busid needs to include \0 termination */
804 +       if (!(len < BUS_ID_SIZE))
805 +               return -EINVAL;
806 +
807 +       strncpy(busid, buf + 4, BUS_ID_SIZE);
808 +
809 +
810 +       if (!strncmp(buf, "add ", 4)) {
811 +               if (add_match_busid(busid) < 0)
812 +                       return -ENOMEM;
813 +               else {
814 +                       udbg("add busid %s\n", busid);
815 +                       return count;
816 +               }
817 +       } else if (!strncmp(buf, "del ", 4)) {
818 +               if (del_match_busid(busid) < 0)
819 +                       return -ENODEV;
820 +               else {
821 +                       udbg("del busid %s\n", busid);
822 +                       return count;
823 +               }
824 +       } else
825 +               return -EINVAL;
826 +}
827 +
828 +static DRIVER_ATTR(match_busid, S_IRUSR|S_IWUSR, show_match_busid,
829 +                                                       store_match_busid);
830 +
831 +
832 +
833 +/*-------------------------------------------------------------------------*/
834 +
835 +/* Cleanup functions used to free private data */
836 +
837 +static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
838 +{
839 +       struct stub_priv *priv, *tmp;
840 +
841 +       list_for_each_entry_safe(priv, tmp, listhead, list) {
842 +               list_del(&priv->list);
843 +               return priv;
844 +       }
845 +
846 +       return NULL;
847 +}
848 +
849 +static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
850 +{
851 +       unsigned long flags;
852 +       struct stub_priv *priv;
853 +
854 +       spin_lock_irqsave(&sdev->priv_lock, flags);
855 +
856 +       priv = stub_priv_pop_from_listhead(&sdev->priv_init);
857 +       if (priv) {
858 +               spin_unlock_irqrestore(&sdev->priv_lock, flags);
859 +               return priv;
860 +       }
861 +
862 +       priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
863 +       if (priv) {
864 +               spin_unlock_irqrestore(&sdev->priv_lock, flags);
865 +               return priv;
866 +       }
867 +
868 +       priv = stub_priv_pop_from_listhead(&sdev->priv_free);
869 +       if (priv) {
870 +               spin_unlock_irqrestore(&sdev->priv_lock, flags);
871 +               return priv;
872 +       }
873 +
874 +       spin_unlock_irqrestore(&sdev->priv_lock, flags);
875 +       return NULL;
876 +}
877 +
878 +void stub_device_cleanup_urbs(struct stub_device *sdev)
879 +{
880 +       struct stub_priv *priv;
881 +
882 +       udbg("free sdev %p\n", sdev);
883 +
884 +       while ((priv = stub_priv_pop(sdev))) {
885 +               struct urb *urb = priv->urb;
886 +
887 +               udbg("   free urb %p\n", urb);
888 +               usb_kill_urb(urb);
889 +
890 +               kmem_cache_free(stub_priv_cache, priv);
891 +
892 +               if (urb->transfer_buffer != NULL)
893 +                       kfree(urb->transfer_buffer);
894 +
895 +               if (urb->setup_packet != NULL)
896 +                       kfree(urb->setup_packet);
897 +
898 +               usb_free_urb(urb);
899 +       }
900 +}
901 +
902 +
903 +/*-------------------------------------------------------------------------*/
904 +
905 +static int __init usb_stub_init(void)
906 +{
907 +       int ret;
908 +
909 +       stub_priv_cache = kmem_cache_create("stub_priv",
910 +                                           sizeof(struct stub_priv), 0,
911 +                                           SLAB_HWCACHE_ALIGN, NULL);
912 +
913 +       if (!stub_priv_cache) {
914 +               printk(KERN_ERR KBUILD_MODNAME
915 +                      ": create stub_priv_cache error\n");
916 +               return -ENOMEM;
917 +       }
918 +
919 +       ret = usb_register(&stub_driver);
920 +       if (ret) {
921 +               printk(KERN_ERR KBUILD_MODNAME ": usb_register failed %d\n",
922 +                      ret);
923 +               goto error_usb_register;
924 +       }
925 +
926 +       printk(KERN_INFO KBUILD_MODNAME ":"
927 +              DRIVER_DESC ":" DRIVER_VERSION "\n");
928 +
929 +       memset(busid_table, 0, sizeof(busid_table));
930 +       spin_lock_init(&busid_table_lock);
931 +
932 +       ret = driver_create_file(&stub_driver.drvwrap.driver,
933 +                                &driver_attr_match_busid);
934 +
935 +       if (ret) {
936 +               printk(KERN_ERR KBUILD_MODNAME ": create driver sysfs\n");
937 +               goto error_create_file;
938 +       }
939 +
940 +       return ret;
941 +error_create_file:
942 +       usb_deregister(&stub_driver);
943 +error_usb_register:
944 +       kmem_cache_destroy(stub_priv_cache);
945 +       return ret;
946 +}
947 +
948 +static void __exit usb_stub_exit(void)
949 +{
950 +       driver_remove_file(&stub_driver.drvwrap.driver,
951 +                          &driver_attr_match_busid);
952 +
953 +       /*
954 +        * deregister() calls stub_disconnect() for all devices. Device
955 +        * specific data is cleared in stub_disconnect().
956 +        */
957 +       usb_deregister(&stub_driver);
958 +
959 +       kmem_cache_destroy(stub_priv_cache);
960 +}
961 +
962 +module_init(usb_stub_init);
963 +module_exit(usb_stub_exit);
964 +
965 +MODULE_AUTHOR(DRIVER_AUTHOR);
966 +MODULE_DESCRIPTION(DRIVER_DESC);
967 +MODULE_LICENSE("GPL");
968 --- /dev/null
969 +++ kernel-2.6.28/drivers/usb/usbip/stub_rx.c
970 @@ -0,0 +1,615 @@
971 +/*
972 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
973 + *
974 + * This is free software; you can redistribute it and/or modify
975 + * it under the terms of the GNU General Public License as published by
976 + * the Free Software Foundation; either version 2 of the License, or
977 + * (at your option) any later version.
978 + *
979 + * This is distributed in the hope that it will be useful,
980 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
981 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
982 + * GNU General Public License for more details.
983 + *
984 + * You should have received a copy of the GNU General Public License
985 + * along with this program; if not, write to the Free Software
986 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
987 + * USA.
988 + */
989 +
990 +#include "usbip_common.h"
991 +#include "stub.h"
992 +#include "../../usb/core/hcd.h"
993 +
994 +
995 +static int is_clear_halt_cmd(struct urb *urb)
996 +{
997 +       struct usb_ctrlrequest *req;
998 +
999 +       req = (struct usb_ctrlrequest *) urb->setup_packet;
1000 +
1001 +        return (req->bRequest == USB_REQ_CLEAR_FEATURE) &&
1002 +                (req->bRequestType == USB_RECIP_ENDPOINT) &&
1003 +                (req->wValue == USB_ENDPOINT_HALT);
1004 +}
1005 +
1006 +static int is_set_interface_cmd(struct urb *urb)
1007 +{
1008 +       struct usb_ctrlrequest *req;
1009 +
1010 +       req = (struct usb_ctrlrequest *) urb->setup_packet;
1011 +
1012 +       return (req->bRequest == USB_REQ_SET_INTERFACE) &&
1013 +                  (req->bRequestType == USB_RECIP_INTERFACE);
1014 +}
1015 +
1016 +static int is_set_configuration_cmd(struct urb *urb)
1017 +{
1018 +       struct usb_ctrlrequest *req;
1019 +
1020 +       req = (struct usb_ctrlrequest *) urb->setup_packet;
1021 +
1022 +       return (req->bRequest == USB_REQ_SET_CONFIGURATION) &&
1023 +                  (req->bRequestType == USB_RECIP_DEVICE);
1024 +}
1025 +
1026 +static int is_reset_device_cmd(struct urb *urb)
1027 +{
1028 +       struct usb_ctrlrequest *req;
1029 +       __u16 value;
1030 +       __u16 index;
1031 +
1032 +       req = (struct usb_ctrlrequest *) urb->setup_packet;
1033 +       value = le16_to_cpu(req->wValue);
1034 +       index = le16_to_cpu(req->wIndex);
1035 +
1036 +       if ((req->bRequest == USB_REQ_SET_FEATURE) &&
1037 +                       (req->bRequestType == USB_RT_PORT) &&
1038 +                       (value = USB_PORT_FEAT_RESET)) {
1039 +               dbg_stub_rx("reset_device_cmd, port %u\n", index);
1040 +               return 1;
1041 +       } else
1042 +               return 0;
1043 +}
1044 +
1045 +static int tweak_clear_halt_cmd(struct urb *urb)
1046 +{
1047 +       struct usb_ctrlrequest *req;
1048 +       int target_endp;
1049 +       int target_dir;
1050 +       int target_pipe;
1051 +       int ret;
1052 +
1053 +       req = (struct usb_ctrlrequest *) urb->setup_packet;
1054 +
1055 +       /*
1056 +        * The stalled endpoint is specified in the wIndex value. The endpoint
1057 +        * of the urb is the target of this clear_halt request (i.e., control
1058 +        * endpoint).
1059 +        */
1060 +       target_endp = le16_to_cpu(req->wIndex) & 0x000f;
1061 +
1062 +       /* the stalled endpoint direction is IN or OUT?. USB_DIR_IN is 0x80.  */
1063 +       target_dir = le16_to_cpu(req->wIndex) & 0x0080;
1064 +
1065 +       if (target_dir)
1066 +               target_pipe = usb_rcvctrlpipe(urb->dev, target_endp);
1067 +       else
1068 +               target_pipe = usb_sndctrlpipe(urb->dev, target_endp);
1069 +
1070 +       ret = usb_clear_halt(urb->dev, target_pipe);
1071 +       if (ret < 0)
1072 +               uinfo("clear_halt error: devnum %d endp %d, %d\n",
1073 +                               urb->dev->devnum, target_endp, ret);
1074 +       else
1075 +               uinfo("clear_halt done: devnum %d endp %d\n",
1076 +                               urb->dev->devnum, target_endp);
1077 +
1078 +       return ret;
1079 +}
1080 +
1081 +static int tweak_set_interface_cmd(struct urb *urb)
1082 +{
1083 +       struct usb_ctrlrequest *req;
1084 +       __u16 alternate;
1085 +       __u16 interface;
1086 +       int ret;
1087 +
1088 +       req = (struct usb_ctrlrequest *) urb->setup_packet;
1089 +       alternate = le16_to_cpu(req->wValue);
1090 +       interface = le16_to_cpu(req->wIndex);
1091 +
1092 +       dbg_stub_rx("set_interface: inf %u alt %u\n", interface, alternate);
1093 +
1094 +       ret = usb_set_interface(urb->dev, interface, alternate);
1095 +       if (ret < 0)
1096 +               uinfo("set_interface error: inf %u alt %u, %d\n",
1097 +                               interface, alternate, ret);
1098 +       else
1099 +               uinfo("set_interface done: inf %u alt %u\n",
1100 +                                                       interface,
1101 +                                                       alternate);
1102 +
1103 +       return ret;
1104 +}
1105 +
1106 +static int tweak_set_configuration_cmd(struct urb *urb)
1107 +{
1108 +       struct usb_ctrlrequest *req;
1109 +       __u16 config;
1110 +
1111 +       req = (struct usb_ctrlrequest *) urb->setup_packet;
1112 +       config = le16_to_cpu(req->wValue);
1113 +
1114 +       /*
1115 +        * I have never seen a multi-config device. Very rare.
1116 +        * For most devices, this will be called to choose a default
1117 +        * configuration only once in an initialization phase.
1118 +        *
1119 +        * set_configuration may change a device configuration and its device
1120 +        * drivers will be unbound and assigned for a new device configuration.
1121 +        * This means this usbip driver will be also unbound when called, then
1122 +        * eventually reassigned to the device as far as driver matching
1123 +        * condition is kept.
1124 +        *
1125 +        * Unfortunatelly, an existing usbip connection will be dropped
1126 +        * due to this driver unbinding. So, skip here.
1127 +        * A user may need to set a special configuration value before
1128 +        * exporting the device.
1129 +        */
1130 +       uinfo("set_configuration (%d) to %s\n", config, urb->dev->dev.bus_id);
1131 +       uinfo("but, skip!\n");
1132 +
1133 +       return 0;
1134 +       /* return usb_driver_set_configuration(urb->dev, config); */
1135 +}
1136 +
1137 +static int tweak_reset_device_cmd(struct urb *urb)
1138 +{
1139 +       struct usb_ctrlrequest *req;
1140 +       __u16 value;
1141 +       __u16 index;
1142 +       int ret;
1143 +
1144 +       req = (struct usb_ctrlrequest *) urb->setup_packet;
1145 +       value = le16_to_cpu(req->wValue);
1146 +       index = le16_to_cpu(req->wIndex);
1147 +
1148 +       uinfo("reset_device (port %d) to %s\n", index, urb->dev->dev.bus_id);
1149 +
1150 +       /* all interfaces should be owned by usbip driver, so just reset it.  */
1151 +       ret = usb_lock_device_for_reset(urb->dev, NULL);
1152 +       if (ret < 0) {
1153 +               dev_err(&urb->dev->dev, "lock for reset\n");
1154 +               return ret;
1155 +       }
1156 +
1157 +       /* try to reset the device */
1158 +       ret = usb_reset_device(urb->dev);
1159 +       if (ret < 0)
1160 +               dev_err(&urb->dev->dev, "device reset\n");
1161 +
1162 +       usb_unlock_device(urb->dev);
1163 +
1164 +       return ret;
1165 +}
1166 +
1167 +/*
1168 + * clear_halt, set_interface, and set_configuration require special tricks.
1169 + */
1170 +static void tweak_special_requests(struct urb *urb)
1171 +{
1172 +       if (!urb || !urb->setup_packet)
1173 +               return;
1174 +
1175 +       if (usb_pipetype(urb->pipe) != PIPE_CONTROL)
1176 +               return;
1177 +
1178 +       if (is_clear_halt_cmd(urb))
1179 +               /* tweak clear_halt */
1180 +                tweak_clear_halt_cmd(urb);
1181 +
1182 +       else if (is_set_interface_cmd(urb))
1183 +               /* tweak set_interface */
1184 +               tweak_set_interface_cmd(urb);
1185 +
1186 +       else if (is_set_configuration_cmd(urb))
1187 +               /* tweak set_configuration */
1188 +               tweak_set_configuration_cmd(urb);
1189 +
1190 +       else if (is_reset_device_cmd(urb))
1191 +               tweak_reset_device_cmd(urb);
1192 +       else
1193 +               dbg_stub_rx("no need to tweak\n");
1194 +}
1195 +
1196 +/*
1197 + * stub_recv_unlink() unlinks the URB by a call to usb_unlink_urb().
1198 + * By unlinking the urb asynchronously, stub_rx can continuously
1199 + * process coming urbs.  Even if the urb is unlinked, its completion
1200 + * handler will be called and stub_tx will send a return pdu.
1201 + *
1202 + * See also comments about unlinking strategy in vhci_hcd.c.
1203 + */
1204 +static int stub_recv_cmd_unlink(struct stub_device *sdev,
1205 +                                               struct usbip_header *pdu)
1206 +{
1207 +       struct list_head *listhead = &sdev->priv_init;
1208 +       struct list_head *ptr;
1209 +       unsigned long flags;
1210 +
1211 +       struct stub_priv *priv;
1212 +
1213 +
1214 +       spin_lock_irqsave(&sdev->priv_lock, flags);
1215 +
1216 +       for (ptr = listhead->next; ptr != listhead; ptr = ptr->next) {
1217 +               priv = list_entry(ptr, struct stub_priv, list);
1218 +               if (priv->seqnum == pdu->u.cmd_unlink.seqnum) {
1219 +                       int ret;
1220 +
1221 +                       dev_info(&priv->urb->dev->dev, "unlink urb %p\n",
1222 +                                priv->urb);
1223 +
1224 +                       /*
1225 +                        * This matched urb is not completed yet (i.e., be in
1226 +                        * flight in usb hcd hardware/driver). Now we are
1227 +                        * cancelling it. The unlinking flag means that we are
1228 +                        * now not going to return the normal result pdu of a
1229 +                        * submission request, but going to return a result pdu
1230 +                        * of the unlink request.
1231 +                        */
1232 +                       priv->unlinking = 1;
1233 +
1234 +                       /*
1235 +                        * In the case that unlinking flag is on, prev->seqnum
1236 +                        * is changed from the seqnum of the cancelling urb to
1237 +                        * the seqnum of the unlink request. This will be used
1238 +                        * to make the result pdu of the unlink request.
1239 +                        */
1240 +                       priv->seqnum = pdu->base.seqnum;
1241 +
1242 +                       spin_unlock_irqrestore(&sdev->priv_lock, flags);
1243 +
1244 +                       /*
1245 +                        * usb_unlink_urb() is now out of spinlocking to avoid
1246 +                        * spinlock recursion since stub_complete() is
1247 +                        * sometimes called in this context but not in the
1248 +                        * interrupt context.  If stub_complete() is executed
1249 +                        * before we call usb_unlink_urb(), usb_unlink_urb()
1250 +                        * will return an error value. In this case, stub_tx
1251 +                        * will return the result pdu of this unlink request
1252 +                        * though submission is completed and actual unlinking
1253 +                        * is not executed. OK?
1254 +                        */
1255 +                       /* In the above case, urb->status is not -ECONNRESET,
1256 +                        * so a driver in a client host will know the failure
1257 +                        * of the unlink request ?
1258 +                        */
1259 +                       ret = usb_unlink_urb(priv->urb);
1260 +                       if (ret != -EINPROGRESS)
1261 +                               dev_err(&priv->urb->dev->dev,
1262 +                                       "failed to unlink a urb %p, ret %d\n",
1263 +                                       priv->urb, ret);
1264 +                       return 0;
1265 +               }
1266 +       }
1267 +
1268 +       dbg_stub_rx("seqnum %d is not pending\n", pdu->u.cmd_unlink.seqnum);
1269 +
1270 +       /*
1271 +        * The urb of the unlink target is not found in priv_init queue. It was
1272 +        * already completed and its results is/was going to be sent by a
1273 +        * CMD_RET pdu. In this case, usb_unlink_urb() is not needed. We only
1274 +        * return the completeness of this unlink request to vhci_hcd.
1275 +        */
1276 +       stub_enqueue_ret_unlink(sdev, pdu->base.seqnum, 0);
1277 +
1278 +       spin_unlock_irqrestore(&sdev->priv_lock, flags);
1279 +
1280 +
1281 +       return 0;
1282 +}
1283 +
1284 +static int valid_request(struct stub_device *sdev, struct usbip_header *pdu)
1285 +{
1286 +       struct usbip_device *ud = &sdev->ud;
1287 +
1288 +       if (pdu->base.devid == sdev->devid) {
1289 +               spin_lock(&ud->lock);
1290 +               if (ud->status == SDEV_ST_USED) {
1291 +                       /* A request is valid. */
1292 +                       spin_unlock(&ud->lock);
1293 +                       return 1;
1294 +               }
1295 +               spin_unlock(&ud->lock);
1296 +       }
1297 +
1298 +       return 0;
1299 +}
1300 +
1301 +static struct stub_priv *stub_priv_alloc(struct stub_device *sdev,
1302 +                                        struct usbip_header *pdu)
1303 +{
1304 +       struct stub_priv *priv;
1305 +       struct usbip_device *ud = &sdev->ud;
1306 +       unsigned long flags;
1307 +
1308 +       spin_lock_irqsave(&sdev->priv_lock, flags);
1309 +
1310 +       priv = kmem_cache_alloc(stub_priv_cache, GFP_ATOMIC);
1311 +       if (!priv) {
1312 +               dev_err(&sdev->interface->dev, "alloc stub_priv\n");
1313 +               spin_unlock_irqrestore(&sdev->priv_lock, flags);
1314 +               usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1315 +               return NULL;
1316 +       }
1317 +
1318 +       memset(priv, 0, sizeof(struct stub_priv));
1319 +
1320 +       priv->seqnum = pdu->base.seqnum;
1321 +       priv->sdev = sdev;
1322 +
1323 +       /*
1324 +        * After a stub_priv is linked to a list_head,
1325 +        * our error handler can free allocated data.
1326 +        */
1327 +       list_add_tail(&priv->list, &sdev->priv_init);
1328 +
1329 +       spin_unlock_irqrestore(&sdev->priv_lock, flags);
1330 +
1331 +       return priv;
1332 +}
1333 +
1334 +
1335 +static struct usb_host_endpoint *get_ep_from_epnum(struct usb_device *udev,
1336 +               int epnum0)
1337 +{
1338 +       struct usb_host_config *config;
1339 +       int i = 0, j = 0;
1340 +       struct usb_host_endpoint *ep = NULL;
1341 +       int epnum;
1342 +       int found = 0;
1343 +
1344 +       if (epnum0 == 0)
1345 +               return &udev->ep0;
1346 +
1347 +       config = udev->actconfig;
1348 +       if (!config)
1349 +               return NULL;
1350 +
1351 +       for (i = 0; i < config->desc.bNumInterfaces; i++) {
1352 +               struct usb_host_interface *setting;
1353 +
1354 +               setting = config->interface[i]->cur_altsetting;
1355 +
1356 +               for (j = 0; j < setting->desc.bNumEndpoints; j++) {
1357 +                       ep = &setting->endpoint[j];
1358 +                       epnum = (ep->desc.bEndpointAddress & 0x7f);
1359 +
1360 +                       if (epnum == epnum0) {
1361 +                               /* uinfo("found epnum %d\n", epnum0); */
1362 +                               found = 1;
1363 +                               break;
1364 +                       }
1365 +               }
1366 +       }
1367 +
1368 +       if (found)
1369 +               return ep;
1370 +       else
1371 +               return NULL;
1372 +}
1373 +
1374 +
1375 +static int get_pipe(struct stub_device *sdev, int epnum, int dir)
1376 +{
1377 +       struct usb_device *udev = interface_to_usbdev(sdev->interface);
1378 +       struct usb_host_endpoint *ep;
1379 +       struct usb_endpoint_descriptor *epd = NULL;
1380 +
1381 +       ep = get_ep_from_epnum(udev, epnum);
1382 +       if (!ep) {
1383 +               dev_err(&sdev->interface->dev, "no such endpoint?, %d\n",
1384 +                       epnum);
1385 +               BUG();
1386 +       }
1387 +
1388 +       epd = &ep->desc;
1389 +
1390 +
1391 +#if 0
1392 +       /* epnum 0 is always control */
1393 +       if (epnum == 0) {
1394 +               if (dir == USBIP_DIR_OUT)
1395 +                       return usb_sndctrlpipe(udev, 0);
1396 +               else
1397 +                       return usb_rcvctrlpipe(udev, 0);
1398 +       }
1399 +#endif
1400 +
1401 +       if (usb_endpoint_xfer_control(epd)) {
1402 +               if (dir == USBIP_DIR_OUT)
1403 +                       return usb_sndctrlpipe(udev, epnum);
1404 +               else
1405 +                       return usb_rcvctrlpipe(udev, epnum);
1406 +       }
1407 +
1408 +       if (usb_endpoint_xfer_bulk(epd)) {
1409 +               if (dir == USBIP_DIR_OUT)
1410 +                       return usb_sndbulkpipe(udev, epnum);
1411 +               else
1412 +                       return usb_rcvbulkpipe(udev, epnum);
1413 +       }
1414 +
1415 +       if (usb_endpoint_xfer_int(epd)) {
1416 +               if (dir == USBIP_DIR_OUT)
1417 +                       return usb_sndintpipe(udev, epnum);
1418 +               else
1419 +                       return usb_rcvintpipe(udev, epnum);
1420 +       }
1421 +
1422 +       if (usb_endpoint_xfer_isoc(epd)) {
1423 +               if (dir == USBIP_DIR_OUT)
1424 +                       return usb_sndisocpipe(udev, epnum);
1425 +               else
1426 +                       return usb_rcvisocpipe(udev, epnum);
1427 +       }
1428 +
1429 +       /* NOT REACHED */
1430 +       dev_err(&sdev->interface->dev, "get pipe, epnum %d\n", epnum);
1431 +       return 0;
1432 +}
1433 +
1434 +static void stub_recv_cmd_submit(struct stub_device *sdev,
1435 +                                struct usbip_header *pdu)
1436 +{
1437 +       int ret;
1438 +       struct stub_priv *priv;
1439 +       struct usbip_device *ud = &sdev->ud;
1440 +       struct usb_device *udev = interface_to_usbdev(sdev->interface);
1441 +       int pipe = get_pipe(sdev, pdu->base.ep, pdu->base.direction);
1442 +
1443 +
1444 +       priv = stub_priv_alloc(sdev, pdu);
1445 +       if (!priv)
1446 +               return;
1447 +
1448 +       /* setup a urb */
1449 +       if (usb_pipeisoc(pipe))
1450 +               priv->urb = usb_alloc_urb(pdu->u.cmd_submit.number_of_packets,
1451 +                                                               GFP_KERNEL);
1452 +       else
1453 +               priv->urb = usb_alloc_urb(0, GFP_KERNEL);
1454 +
1455 +       if (!priv->urb) {
1456 +               dev_err(&sdev->interface->dev, "malloc urb\n");
1457 +               usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1458 +               return;
1459 +       }
1460 +
1461 +       /* set priv->urb->transfer_buffer */
1462 +       if (pdu->u.cmd_submit.transfer_buffer_length > 0) {
1463 +               priv->urb->transfer_buffer =
1464 +                       kzalloc(pdu->u.cmd_submit.transfer_buffer_length,
1465 +                                                               GFP_KERNEL);
1466 +               if (!priv->urb->transfer_buffer) {
1467 +                       dev_err(&sdev->interface->dev, "malloc x_buff\n");
1468 +                       usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1469 +                       return;
1470 +               }
1471 +       }
1472 +
1473 +       /* set priv->urb->setup_packet */
1474 +       priv->urb->setup_packet = kzalloc(8, GFP_KERNEL);
1475 +       if (!priv->urb->setup_packet) {
1476 +               dev_err(&sdev->interface->dev, "allocate setup_packet\n");
1477 +               usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1478 +               return;
1479 +       }
1480 +       memcpy(priv->urb->setup_packet, &pdu->u.cmd_submit.setup, 8);
1481 +
1482 +       /* set other members from the base header of pdu */
1483 +       priv->urb->context                = (void *) priv;
1484 +       priv->urb->dev                    = udev;
1485 +       priv->urb->pipe                   = pipe;
1486 +       priv->urb->complete               = stub_complete;
1487 +
1488 +       usbip_pack_pdu(pdu, priv->urb, USBIP_CMD_SUBMIT, 0);
1489 +
1490 +
1491 +       if (usbip_recv_xbuff(ud, priv->urb) < 0)
1492 +               return;
1493 +
1494 +       if (usbip_recv_iso(ud, priv->urb) < 0)
1495 +               return;
1496 +
1497 +       /* no need to submit an intercepted request, but harmless? */
1498 +       tweak_special_requests(priv->urb);
1499 +
1500 +       /* urb is now ready to submit */
1501 +       ret = usb_submit_urb(priv->urb, GFP_KERNEL);
1502 +
1503 +       if (ret == 0)
1504 +               dbg_stub_rx("submit urb ok, seqnum %u\n", pdu->base.seqnum);
1505 +       else {
1506 +               dev_err(&sdev->interface->dev, "submit_urb error, %d\n", ret);
1507 +               usbip_dump_header(pdu);
1508 +               usbip_dump_urb(priv->urb);
1509 +
1510 +               /*
1511 +                * Pessimistic.
1512 +                * This connection will be discarded.
1513 +                */
1514 +               usbip_event_add(ud, SDEV_EVENT_ERROR_SUBMIT);
1515 +       }
1516 +
1517 +       dbg_stub_rx("Leave\n");
1518 +       return;
1519 +}
1520 +
1521 +/* recv a pdu */
1522 +static void stub_rx_pdu(struct usbip_device *ud)
1523 +{
1524 +       int ret;
1525 +       struct usbip_header pdu;
1526 +       struct stub_device *sdev = container_of(ud, struct stub_device, ud);
1527 +       struct device *dev = &sdev->interface->dev;
1528 +
1529 +       dbg_stub_rx("Enter\n");
1530 +
1531 +       memset(&pdu, 0, sizeof(pdu));
1532 +
1533 +       /* 1. receive a pdu header */
1534 +       ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
1535 +       if (ret != sizeof(pdu)) {
1536 +               dev_err(dev, "recv a header, %d\n", ret);
1537 +               usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
1538 +               return;
1539 +       }
1540 +
1541 +       usbip_header_correct_endian(&pdu, 0);
1542 +
1543 +       if (dbg_flag_stub_rx)
1544 +               usbip_dump_header(&pdu);
1545 +
1546 +       if (!valid_request(sdev, &pdu)) {
1547 +               dev_err(dev, "recv invalid request\n");
1548 +               usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
1549 +               return;
1550 +       }
1551 +
1552 +       switch (pdu.base.command) {
1553 +       case USBIP_CMD_UNLINK:
1554 +               stub_recv_cmd_unlink(sdev, &pdu);
1555 +               break;
1556 +
1557 +       case USBIP_CMD_SUBMIT:
1558 +               stub_recv_cmd_submit(sdev, &pdu);
1559 +               break;
1560 +
1561 +       default:
1562 +               /* NOTREACHED */
1563 +               dev_err(dev, "unknown pdu\n");
1564 +               usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
1565 +               return;
1566 +       }
1567 +
1568 +}
1569 +
1570 +void stub_rx_loop(struct usbip_task *ut)
1571 +{
1572 +       struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_rx);
1573 +
1574 +       while (1) {
1575 +               if (signal_pending(current)) {
1576 +                       dbg_stub_rx("signal caught!\n");
1577 +                       break;
1578 +               }
1579 +
1580 +               if (usbip_event_happend(ud))
1581 +                       break;
1582 +
1583 +               stub_rx_pdu(ud);
1584 +       }
1585 +}
1586 --- /dev/null
1587 +++ kernel-2.6.28/drivers/usb/usbip/stub_tx.c
1588 @@ -0,0 +1,371 @@
1589 +/*
1590 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
1591 + *
1592 + * This is free software; you can redistribute it and/or modify
1593 + * it under the terms of the GNU General Public License as published by
1594 + * the Free Software Foundation; either version 2 of the License, or
1595 + * (at your option) any later version.
1596 + *
1597 + * This is distributed in the hope that it will be useful,
1598 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1599 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1600 + * GNU General Public License for more details.
1601 + *
1602 + * You should have received a copy of the GNU General Public License
1603 + * along with this program; if not, write to the Free Software
1604 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1605 + * USA.
1606 + */
1607 +
1608 +#include "usbip_common.h"
1609 +#include "stub.h"
1610 +
1611 +
1612 +static void stub_free_priv_and_urb(struct stub_priv *priv)
1613 +{
1614 +       struct urb *urb = priv->urb;
1615 +
1616 +       kfree(urb->setup_packet);
1617 +       kfree(urb->transfer_buffer);
1618 +       list_del(&priv->list);
1619 +       kmem_cache_free(stub_priv_cache, priv);
1620 +       usb_free_urb(urb);
1621 +}
1622 +
1623 +/* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
1624 +void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
1625 +                            __u32 status)
1626 +{
1627 +       struct stub_unlink *unlink;
1628 +
1629 +       unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC);
1630 +       if (!unlink) {
1631 +               dev_err(&sdev->interface->dev, "alloc stub_unlink\n");
1632 +               usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
1633 +               return;
1634 +       }
1635 +
1636 +       unlink->seqnum = seqnum;
1637 +       unlink->status = status;
1638 +
1639 +       list_add_tail(&unlink->list, &sdev->unlink_tx);
1640 +}
1641 +
1642 +/**
1643 + * stub_complete - completion handler of a usbip urb
1644 + * @urb: pointer to the urb completed
1645 + * @regs:
1646 + *
1647 + * When a urb has completed, the USB core driver calls this function mostly in
1648 + * the interrupt context. To return the result of a urb, the completed urb is
1649 + * linked to the pending list of returning.
1650 + *
1651 + */
1652 +void stub_complete(struct urb *urb)
1653 +{
1654 +       struct stub_priv *priv = (struct stub_priv *) urb->context;
1655 +       struct stub_device *sdev = priv->sdev;
1656 +       unsigned long flags;
1657 +
1658 +       dbg_stub_tx("complete! status %d\n", urb->status);
1659 +
1660 +
1661 +       switch (urb->status) {
1662 +       case 0:
1663 +               /* OK */
1664 +               break;
1665 +       case -ENOENT:
1666 +               uinfo("stopped by a call of usb_kill_urb() because of"
1667 +                                       "cleaning up a virtual connection\n");
1668 +               return;
1669 +       case -ECONNRESET:
1670 +               uinfo("unlinked by a call of usb_unlink_urb()\n");
1671 +               break;
1672 +       case -EPIPE:
1673 +               uinfo("endpoint %d is stalled\n", usb_pipeendpoint(urb->pipe));
1674 +               break;
1675 +       case -ESHUTDOWN:
1676 +               uinfo("device removed?\n");
1677 +               break;
1678 +       default:
1679 +               uinfo("urb completion with non-zero status %d\n", urb->status);
1680 +       }
1681 +
1682 +       /* link a urb to the queue of tx. */
1683 +       spin_lock_irqsave(&sdev->priv_lock, flags);
1684 +
1685 +       if (priv->unlinking) {
1686 +               stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
1687 +               stub_free_priv_and_urb(priv);
1688 +       } else
1689 +               list_move_tail(&priv->list, &sdev->priv_tx);
1690 +
1691 +
1692 +       spin_unlock_irqrestore(&sdev->priv_lock, flags);
1693 +
1694 +       /* wake up tx_thread */
1695 +       wake_up(&sdev->tx_waitq);
1696 +}
1697 +
1698 +
1699 +/*-------------------------------------------------------------------------*/
1700 +/* fill PDU */
1701 +
1702 +static inline void setup_base_pdu(struct usbip_header_basic *base,
1703 +               __u32 command, __u32 seqnum)
1704 +{
1705 +       base->command = command;
1706 +       base->seqnum  = seqnum;
1707 +       base->devid   = 0;
1708 +       base->ep      = 0;
1709 +       base->direction   = 0;
1710 +}
1711 +
1712 +static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
1713 +{
1714 +       struct stub_priv *priv = (struct stub_priv *) urb->context;
1715 +
1716 +       setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
1717 +
1718 +       usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
1719 +}
1720 +
1721 +static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
1722 +               struct stub_unlink *unlink)
1723 +{
1724 +       setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
1725 +
1726 +       rpdu->u.ret_unlink.status = unlink->status;
1727 +}
1728 +
1729 +
1730 +/*-------------------------------------------------------------------------*/
1731 +/* send RET_SUBMIT */
1732 +
1733 +static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
1734 +{
1735 +       unsigned long flags;
1736 +       struct stub_priv *priv, *tmp;
1737 +
1738 +       spin_lock_irqsave(&sdev->priv_lock, flags);
1739 +
1740 +       list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
1741 +               list_move_tail(&priv->list, &sdev->priv_free);
1742 +               spin_unlock_irqrestore(&sdev->priv_lock, flags);
1743 +               return priv;
1744 +       }
1745 +
1746 +       spin_unlock_irqrestore(&sdev->priv_lock, flags);
1747 +
1748 +       return NULL;
1749 +}
1750 +
1751 +static int stub_send_ret_submit(struct stub_device *sdev)
1752 +{
1753 +       unsigned long flags;
1754 +       struct stub_priv *priv, *tmp;
1755 +
1756 +       struct msghdr msg;
1757 +       struct kvec iov[3];
1758 +       size_t txsize;
1759 +
1760 +       size_t total_size = 0;
1761 +
1762 +       while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
1763 +               int ret;
1764 +               struct urb *urb = priv->urb;
1765 +               struct usbip_header pdu_header;
1766 +               void *iso_buffer = NULL;
1767 +
1768 +               txsize = 0;
1769 +               memset(&pdu_header, 0, sizeof(pdu_header));
1770 +               memset(&msg, 0, sizeof(msg));
1771 +               memset(&iov, 0, sizeof(iov));
1772 +
1773 +               dbg_stub_tx("setup txdata urb %p\n", urb);
1774 +
1775 +
1776 +               /* 1. setup usbip_header */
1777 +               setup_ret_submit_pdu(&pdu_header, urb);
1778 +               usbip_header_correct_endian(&pdu_header, 1);
1779 +
1780 +               iov[0].iov_base = &pdu_header;
1781 +               iov[0].iov_len  = sizeof(pdu_header);
1782 +               txsize += sizeof(pdu_header);
1783 +
1784 +               /* 2. setup transfer buffer */
1785 +               if (usb_pipein(urb->pipe) && urb->actual_length > 0) {
1786 +                       iov[1].iov_base = urb->transfer_buffer;
1787 +                       iov[1].iov_len  = urb->actual_length;
1788 +                       txsize += urb->actual_length;
1789 +               }
1790 +
1791 +               /* 3. setup iso_packet_descriptor */
1792 +               if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1793 +                       ssize_t len = 0;
1794 +
1795 +                       iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
1796 +                       if (!iso_buffer) {
1797 +                               usbip_event_add(&sdev->ud,
1798 +                                               SDEV_EVENT_ERROR_MALLOC);
1799 +                               return -1;
1800 +                       }
1801 +
1802 +                       iov[2].iov_base = iso_buffer;
1803 +                       iov[2].iov_len  = len;
1804 +                       txsize += len;
1805 +               }
1806 +
1807 +               ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
1808 +                                    3, txsize);
1809 +               if (ret != txsize) {
1810 +                       dev_err(&sdev->interface->dev,
1811 +                               "sendmsg failed!, retval %d for %zd\n",
1812 +                               ret, txsize);
1813 +                       kfree(iso_buffer);
1814 +                       usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
1815 +                       return -1;
1816 +               }
1817 +
1818 +               kfree(iso_buffer);
1819 +               dbg_stub_tx("send txdata\n");
1820 +
1821 +               total_size += txsize;
1822 +       }
1823 +
1824 +
1825 +       spin_lock_irqsave(&sdev->priv_lock, flags);
1826 +
1827 +       list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
1828 +               stub_free_priv_and_urb(priv);
1829 +       }
1830 +
1831 +       spin_unlock_irqrestore(&sdev->priv_lock, flags);
1832 +
1833 +       return total_size;
1834 +}
1835 +
1836 +
1837 +/*-------------------------------------------------------------------------*/
1838 +/* send RET_UNLINK */
1839 +
1840 +static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
1841 +{
1842 +       unsigned long flags;
1843 +       struct stub_unlink *unlink, *tmp;
1844 +
1845 +       spin_lock_irqsave(&sdev->priv_lock, flags);
1846 +
1847 +       list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
1848 +               list_move_tail(&unlink->list, &sdev->unlink_free);
1849 +               spin_unlock_irqrestore(&sdev->priv_lock, flags);
1850 +               return unlink;
1851 +       }
1852 +
1853 +       spin_unlock_irqrestore(&sdev->priv_lock, flags);
1854 +
1855 +       return NULL;
1856 +}
1857 +
1858 +
1859 +static int stub_send_ret_unlink(struct stub_device *sdev)
1860 +{
1861 +       unsigned long flags;
1862 +       struct stub_unlink *unlink, *tmp;
1863 +
1864 +       struct msghdr msg;
1865 +       struct kvec iov[1];
1866 +       size_t txsize;
1867 +
1868 +       size_t total_size = 0;
1869 +
1870 +       while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
1871 +               int ret;
1872 +               struct usbip_header pdu_header;
1873 +
1874 +               txsize = 0;
1875 +               memset(&pdu_header, 0, sizeof(pdu_header));
1876 +               memset(&msg, 0, sizeof(msg));
1877 +               memset(&iov, 0, sizeof(iov));
1878 +
1879 +               dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
1880 +
1881 +               /* 1. setup usbip_header */
1882 +               setup_ret_unlink_pdu(&pdu_header, unlink);
1883 +               usbip_header_correct_endian(&pdu_header, 1);
1884 +
1885 +               iov[0].iov_base = &pdu_header;
1886 +               iov[0].iov_len  = sizeof(pdu_header);
1887 +               txsize += sizeof(pdu_header);
1888 +
1889 +               ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
1890 +                                    1, txsize);
1891 +               if (ret != txsize) {
1892 +                       dev_err(&sdev->interface->dev,
1893 +                               "sendmsg failed!, retval %d for %zd\n",
1894 +                               ret, txsize);
1895 +                       usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
1896 +                       return -1;
1897 +               }
1898 +
1899 +
1900 +               dbg_stub_tx("send txdata\n");
1901 +
1902 +               total_size += txsize;
1903 +       }
1904 +
1905 +
1906 +       spin_lock_irqsave(&sdev->priv_lock, flags);
1907 +
1908 +       list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
1909 +               list_del(&unlink->list);
1910 +               kfree(unlink);
1911 +       }
1912 +
1913 +       spin_unlock_irqrestore(&sdev->priv_lock, flags);
1914 +
1915 +       return total_size;
1916 +}
1917 +
1918 +
1919 +/*-------------------------------------------------------------------------*/
1920 +
1921 +void stub_tx_loop(struct usbip_task *ut)
1922 +{
1923 +       struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_tx);
1924 +       struct stub_device *sdev = container_of(ud, struct stub_device, ud);
1925 +
1926 +       while (1) {
1927 +               if (signal_pending(current)) {
1928 +                       dbg_stub_tx("signal catched\n");
1929 +                       break;
1930 +               }
1931 +
1932 +               if (usbip_event_happend(ud))
1933 +                       break;
1934 +
1935 +               /*
1936 +                * send_ret_submit comes earlier than send_ret_unlink.  stub_rx
1937 +                * looks at only priv_init queue. If the completion of a URB is
1938 +                * earlier than the receive of CMD_UNLINK, priv is moved to
1939 +                * priv_tx queue and stub_rx does not find the target priv. In
1940 +                * this case, vhci_rx receives the result of the submit request
1941 +                * and then receives the result of the unlink request. The
1942 +                * result of the submit is given back to the usbcore as the
1943 +                * completion of the unlink request. The request of the
1944 +                * unlink is ignored. This is ok because a driver who calls
1945 +                * usb_unlink_urb() understands the unlink was too late by
1946 +                * getting the status of the given-backed URB which has the
1947 +                * status of usb_submit_urb().
1948 +                */
1949 +               if (stub_send_ret_submit(sdev) < 0)
1950 +                       break;
1951 +
1952 +               if (stub_send_ret_unlink(sdev) < 0)
1953 +                       break;
1954 +
1955 +               wait_event_interruptible(sdev->tx_waitq,
1956 +                               (!list_empty(&sdev->priv_tx) ||
1957 +                                !list_empty(&sdev->unlink_tx)));
1958 +       }
1959 +}
1960 --- /dev/null
1961 +++ kernel-2.6.28/drivers/usb/usbip/usbip_common.c
1962 @@ -0,0 +1,997 @@
1963 +/*
1964 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
1965 + *
1966 + * This is free software; you can redistribute it and/or modify
1967 + * it under the terms of the GNU General Public License as published by
1968 + * the Free Software Foundation; either version 2 of the License, or
1969 + * (at your option) any later version.
1970 + *
1971 + * This is distributed in the hope that it will be useful,
1972 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1973 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1974 + * GNU General Public License for more details.
1975 + *
1976 + * You should have received a copy of the GNU General Public License
1977 + * along with this program; if not, write to the Free Software
1978 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1979 + * USA.
1980 + */
1981 +
1982 +#include <linux/kernel.h>
1983 +#include <linux/file.h>
1984 +#include <linux/tcp.h>
1985 +#include <linux/in.h>
1986 +#include "usbip_common.h"
1987 +
1988 +/* version information */
1989 +#define DRIVER_VERSION "1.0"
1990 +#define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi _at_ users.sourceforge.net>"
1991 +#define DRIVER_DESC "usbip common driver"
1992 +
1993 +/*-------------------------------------------------------------------------*/
1994 +/* debug routines */
1995 +
1996 +#ifdef CONFIG_USB_DEBUG
1997 +unsigned long usbip_debug_flag = 0xffffffff;
1998 +#else
1999 +unsigned long usbip_debug_flag;
2000 +#endif
2001 +EXPORT_SYMBOL_GPL(usbip_debug_flag);
2002 +
2003 +
2004 +/* FIXME */
2005 +struct device_attribute dev_attr_usbip_debug;
2006 +EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
2007 +
2008 +
2009 +static ssize_t show_flag(struct device *dev, struct device_attribute *attr,
2010 +                                                               char *buf)
2011 +{
2012 +       return sprintf(buf, "%lx\n", usbip_debug_flag);
2013 +}
2014 +
2015 +static ssize_t store_flag(struct device *dev, struct device_attribute *attr,
2016 +               const char *buf, size_t count)
2017 +{
2018 +       unsigned long flag;
2019 +
2020 +       sscanf(buf, "%lx", &flag);
2021 +       usbip_debug_flag = flag;
2022 +
2023 +       return count;
2024 +}
2025 +DEVICE_ATTR(usbip_debug, (S_IRUGO | S_IWUSR), show_flag, store_flag);
2026 +
2027 +static void usbip_dump_buffer(char *buff, int bufflen)
2028 +{
2029 +       int i;
2030 +
2031 +       if (bufflen > 128) {
2032 +               for (i = 0; i < 128; i++) {
2033 +                       if (i%24 == 0)
2034 +                               printk("   ");
2035 +                       printk("%02x ", (unsigned char) buff[i]);
2036 +                       if (i%4 == 3)
2037 +                               printk("| ");
2038 +                       if (i%24 == 23)
2039 +                               printk("\n");
2040 +               }
2041 +               printk("... (%d byte)\n", bufflen);
2042 +               return;
2043 +       }
2044 +
2045 +       for (i = 0; i < bufflen; i++) {
2046 +               if (i%24 == 0)
2047 +                       printk("   ");
2048 +               printk("%02x ", (unsigned char) buff[i]);
2049 +               if (i%4 == 3)
2050 +                       printk("| ");
2051 +               if (i%24 == 23)
2052 +                       printk("\n");
2053 +       }
2054 +       printk("\n");
2055 +
2056 +}
2057 +
2058 +static void usbip_dump_pipe(unsigned int p)
2059 +{
2060 +       unsigned char type = usb_pipetype(p);
2061 +       unsigned char ep = usb_pipeendpoint(p);
2062 +       unsigned char dev = usb_pipedevice(p);
2063 +       unsigned char dir = usb_pipein(p);
2064 +
2065 +       printk("dev(%d) ", dev);
2066 +       printk("ep(%d) ",  ep);
2067 +       printk("%s ", dir ? "IN" : "OUT");
2068 +
2069 +       switch (type) {
2070 +       case PIPE_ISOCHRONOUS:
2071 +               printk("%s ", "ISO");
2072 +               break;
2073 +       case PIPE_INTERRUPT:
2074 +               printk("%s ", "INT");
2075 +               break;
2076 +       case PIPE_CONTROL:
2077 +               printk("%s ", "CTL");
2078 +               break;
2079 +       case PIPE_BULK:
2080 +               printk("%s ", "BLK");
2081 +               break;
2082 +       default:
2083 +               printk("ERR");
2084 +       }
2085 +
2086 +       printk("\n");
2087 +
2088 +}
2089 +
2090 +static void usbip_dump_usb_device(struct usb_device *udev)
2091 +{
2092 +       struct device *dev = &udev->dev;
2093 +       int i;
2094 +
2095 +       dev_dbg(dev, "       devnum(%d) devpath(%s)",
2096 +               udev->devnum, udev->devpath);
2097 +
2098 +       switch (udev->speed) {
2099 +       case USB_SPEED_HIGH:
2100 +               printk(" SPD_HIGH");
2101 +               break;
2102 +       case USB_SPEED_FULL:
2103 +               printk(" SPD_FULL");
2104 +               break;
2105 +       case USB_SPEED_LOW:
2106 +               printk(" SPD_LOW");
2107 +               break;
2108 +       case USB_SPEED_UNKNOWN:
2109 +               printk(" SPD_UNKNOWN");
2110 +               break;
2111 +       default:
2112 +               printk(" SPD_ERROR");
2113 +       }
2114 +
2115 +       printk(" tt %p, ttport %d", udev->tt, udev->ttport);
2116 +       printk("\n");
2117 +
2118 +       dev_dbg(dev, "                    ");
2119 +       for (i = 0; i < 16; i++)
2120 +               printk(" %2u", i);
2121 +       printk("\n");
2122 +
2123 +       dev_dbg(dev, "       toggle0(IN) :");
2124 +       for (i = 0; i < 16; i++)
2125 +               printk(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
2126 +       printk("\n");
2127 +
2128 +       dev_dbg(dev, "       toggle1(OUT):");
2129 +       for (i = 0; i < 16; i++)
2130 +               printk(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
2131 +       printk("\n");
2132 +
2133 +
2134 +       dev_dbg(dev, "       epmaxp_in   :");
2135 +       for (i = 0; i < 16; i++) {
2136 +               if (udev->ep_in[i])
2137 +                       printk(" %2u",
2138 +                            le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
2139 +       }
2140 +       printk("\n");
2141 +
2142 +       dev_dbg(dev, "       epmaxp_out  :");
2143 +       for (i = 0; i < 16; i++) {
2144 +               if (udev->ep_out[i])
2145 +                       printk(" %2u",
2146 +                            le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
2147 +       }
2148 +       printk("\n");
2149 +
2150 +       dev_dbg(dev, "parent %p, bus %p\n", udev->parent, udev->bus);
2151 +
2152 +       dev_dbg(dev, "descriptor %p, config %p, actconfig %p, "
2153 +               "rawdescriptors %p\n", &udev->descriptor, udev->config,
2154 +               udev->actconfig, udev->rawdescriptors);
2155 +
2156 +       dev_dbg(dev, "have_langid %d, string_langid %d\n",
2157 +               udev->have_langid, udev->string_langid);
2158 +
2159 +       dev_dbg(dev, "maxchild %d, children %p\n",
2160 +               udev->maxchild, udev->children);
2161 +}
2162 +
2163 +static void usbip_dump_request_type(__u8 rt)
2164 +{
2165 +       switch (rt & USB_RECIP_MASK) {
2166 +       case USB_RECIP_DEVICE:
2167 +               printk("DEVICE");
2168 +               break;
2169 +       case USB_RECIP_INTERFACE:
2170 +               printk("INTERF");
2171 +               break;
2172 +       case USB_RECIP_ENDPOINT:
2173 +               printk("ENDPOI");
2174 +               break;
2175 +       case USB_RECIP_OTHER:
2176 +               printk("OTHER ");
2177 +               break;
2178 +       default:
2179 +               printk("------");
2180 +       }
2181 +}
2182 +
2183 +static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
2184 +{
2185 +       if (!cmd) {
2186 +               printk("      %s : null pointer\n", __func__);
2187 +               return;
2188 +       }
2189 +
2190 +       printk("       ");
2191 +       printk("bRequestType(%02X) ", cmd->bRequestType);
2192 +       printk("bRequest(%02X) " , cmd->bRequest);
2193 +       printk("wValue(%04X) ", cmd->wValue);
2194 +       printk("wIndex(%04X) ", cmd->wIndex);
2195 +       printk("wLength(%04X) ", cmd->wLength);
2196 +
2197 +       printk("\n       ");
2198 +
2199 +       if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
2200 +               printk("STANDARD ");
2201 +               switch (cmd->bRequest) {
2202 +               case USB_REQ_GET_STATUS:
2203 +                       printk("GET_STATUS");
2204 +                       break;
2205 +               case USB_REQ_CLEAR_FEATURE:
2206 +                       printk("CLEAR_FEAT");
2207 +                       break;
2208 +               case USB_REQ_SET_FEATURE:
2209 +                       printk("SET_FEAT  ");
2210 +                       break;
2211 +               case USB_REQ_SET_ADDRESS:
2212 +                       printk("SET_ADDRRS");
2213 +                       break;
2214 +               case USB_REQ_GET_DESCRIPTOR:
2215 +                       printk("GET_DESCRI");
2216 +                       break;
2217 +               case USB_REQ_SET_DESCRIPTOR:
2218 +                       printk("SET_DESCRI");
2219 +                       break;
2220 +               case USB_REQ_GET_CONFIGURATION:
2221 +                       printk("GET_CONFIG");
2222 +                       break;
2223 +               case USB_REQ_SET_CONFIGURATION:
2224 +                       printk("SET_CONFIG");
2225 +                       break;
2226 +               case USB_REQ_GET_INTERFACE:
2227 +                       printk("GET_INTERF");
2228 +                       break;
2229 +               case USB_REQ_SET_INTERFACE:
2230 +                       printk("SET_INTERF");
2231 +                       break;
2232 +               case USB_REQ_SYNCH_FRAME:
2233 +                       printk("SYNC_FRAME");
2234 +                       break;
2235 +               default:
2236 +                       printk("REQ(%02X) ", cmd->bRequest);
2237 +               }
2238 +
2239 +               printk(" ");
2240 +               usbip_dump_request_type(cmd->bRequestType);
2241 +
2242 +       } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
2243 +               printk("CLASS   ");
2244 +
2245 +       else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR)
2246 +               printk("VENDOR  ");
2247 +
2248 +       else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED)
2249 +               printk("RESERVED");
2250 +
2251 +       printk("\n");
2252 +}
2253 +
2254 +void usbip_dump_urb(struct urb *urb)
2255 +{
2256 +       struct device *dev;
2257 +
2258 +       if (!urb) {
2259 +               printk(KERN_DEBUG KBUILD_MODNAME
2260 +                      ":%s: urb: null pointer!!\n", __func__);
2261 +               return;
2262 +       }
2263 +
2264 +       if (!urb->dev) {
2265 +               printk(KERN_DEBUG KBUILD_MODNAME
2266 +                      ":%s: urb->dev: null pointer!!\n", __func__);
2267 +               return;
2268 +       }
2269 +       dev = &urb->dev->dev;
2270 +
2271 +       dev_dbg(dev, "   urb                   :%p\n", urb);
2272 +       dev_dbg(dev, "   dev                   :%p\n", urb->dev);
2273 +
2274 +       usbip_dump_usb_device(urb->dev);
2275 +
2276 +       dev_dbg(dev, "   pipe                  :%08x ", urb->pipe);
2277 +
2278 +       usbip_dump_pipe(urb->pipe);
2279 +
2280 +       dev_dbg(dev, "   status                :%d\n", urb->status);
2281 +       dev_dbg(dev, "   transfer_flags        :%08X\n", urb->transfer_flags);
2282 +       dev_dbg(dev, "   transfer_buffer       :%p\n", urb->transfer_buffer);
2283 +       dev_dbg(dev, "   transfer_buffer_length:%d\n", urb->transfer_buffer_length);
2284 +       dev_dbg(dev, "   actual_length         :%d\n", urb->actual_length);
2285 +       dev_dbg(dev, "   setup_packet          :%p\n", urb->setup_packet);
2286 +
2287 +       if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
2288 +                       usbip_dump_usb_ctrlrequest(
2289 +                       (struct usb_ctrlrequest *)urb->setup_packet);
2290 +
2291 +       dev_dbg(dev, "   start_frame           :%d\n", urb->start_frame);
2292 +       dev_dbg(dev, "   number_of_packets     :%d\n", urb->number_of_packets);
2293 +       dev_dbg(dev, "   interval              :%d\n", urb->interval);
2294 +       dev_dbg(dev, "   error_count           :%d\n", urb->error_count);
2295 +       dev_dbg(dev, "   context               :%p\n", urb->context);
2296 +       dev_dbg(dev, "   complete              :%p\n", urb->complete);
2297 +}
2298 +EXPORT_SYMBOL_GPL(usbip_dump_urb);
2299 +
2300 +void usbip_dump_header(struct usbip_header *pdu)
2301 +{
2302 +       udbg("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
2303 +                       pdu->base.command,
2304 +                       pdu->base.seqnum,
2305 +                       pdu->base.devid,
2306 +                       pdu->base.direction,
2307 +                       pdu->base.ep);
2308 +
2309 +       switch (pdu->base.command) {
2310 +       case USBIP_CMD_SUBMIT:
2311 +               udbg("CMD_SUBMIT: x_flags %u x_len %u sf %u #p %u iv %u\n",
2312 +                               pdu->u.cmd_submit.transfer_flags,
2313 +                               pdu->u.cmd_submit.transfer_buffer_length,
2314 +                               pdu->u.cmd_submit.start_frame,
2315 +                               pdu->u.cmd_submit.number_of_packets,
2316 +                               pdu->u.cmd_submit.interval);
2317 +                               break;
2318 +       case USBIP_CMD_UNLINK:
2319 +               udbg("CMD_UNLINK: seq %u\n", pdu->u.cmd_unlink.seqnum);
2320 +               break;
2321 +       case USBIP_RET_SUBMIT:
2322 +               udbg("RET_SUBMIT: st %d al %u sf %d ec %d\n",
2323 +                               pdu->u.ret_submit.status,
2324 +                               pdu->u.ret_submit.actual_length,
2325 +                               pdu->u.ret_submit.start_frame,
2326 +                               pdu->u.ret_submit.error_count);
2327 +       case USBIP_RET_UNLINK:
2328 +               udbg("RET_UNLINK: status %d\n", pdu->u.ret_unlink.status);
2329 +               break;
2330 +       default:
2331 +               /* NOT REACHED */
2332 +               udbg("UNKNOWN\n");
2333 +       }
2334 +}
2335 +EXPORT_SYMBOL_GPL(usbip_dump_header);
2336 +
2337 +
2338 +/*-------------------------------------------------------------------------*/
2339 +/* thread routines */
2340 +
2341 +int usbip_thread(void *param)
2342 +{
2343 +       struct usbip_task *ut = param;
2344 +
2345 +       if (!ut)
2346 +               return -EINVAL;
2347 +
2348 +       lock_kernel();
2349 +       daemonize(ut->name);
2350 +       allow_signal(SIGKILL);
2351 +       ut->thread = current;
2352 +       unlock_kernel();
2353 +
2354 +       /* srv.rb must wait for rx_thread starting */
2355 +       complete(&ut->thread_done);
2356 +
2357 +       /* start of while loop */
2358 +       ut->loop_ops(ut);
2359 +
2360 +       /* end of loop */
2361 +       ut->thread = NULL;
2362 +
2363 +       complete_and_exit(&ut->thread_done, 0);
2364 +}
2365 +
2366 +void usbip_start_threads(struct usbip_device *ud)
2367 +{
2368 +       /*
2369 +        * threads are invoked per one device (per one connection).
2370 +        */
2371 +       kernel_thread(usbip_thread, (void *)&ud->tcp_rx, 0);
2372 +       kernel_thread(usbip_thread, (void *)&ud->tcp_tx, 0);
2373 +
2374 +       /* confirm threads are starting */
2375 +       wait_for_completion(&ud->tcp_rx.thread_done);
2376 +       wait_for_completion(&ud->tcp_tx.thread_done);
2377 +}
2378 +EXPORT_SYMBOL_GPL(usbip_start_threads);
2379 +
2380 +void usbip_stop_threads(struct usbip_device *ud)
2381 +{
2382 +       /* kill threads related to this sdev, if v.c. exists */
2383 +       if (ud->tcp_rx.thread != NULL) {
2384 +               send_sig(SIGKILL, ud->tcp_rx.thread, 1);
2385 +               wait_for_completion(&ud->tcp_rx.thread_done);
2386 +               udbg("rx_thread for ud %p has finished\n", ud);
2387 +       }
2388 +
2389 +       if (ud->tcp_tx.thread != NULL) {
2390 +               send_sig(SIGKILL, ud->tcp_tx.thread, 1);
2391 +               wait_for_completion(&ud->tcp_tx.thread_done);
2392 +               udbg("tx_thread for ud %p has finished\n", ud);
2393 +       }
2394 +}
2395 +EXPORT_SYMBOL_GPL(usbip_stop_threads);
2396 +
2397 +void usbip_task_init(struct usbip_task *ut, char *name,
2398 +               void (*loop_ops)(struct usbip_task *))
2399 +{
2400 +       ut->thread = NULL;
2401 +       init_completion(&ut->thread_done);
2402 +       ut->name = name;
2403 +       ut->loop_ops = loop_ops;
2404 +}
2405 +EXPORT_SYMBOL_GPL(usbip_task_init);
2406 +
2407 +
2408 +/*-------------------------------------------------------------------------*/
2409 +/* socket routines */
2410 +
2411 + /*  Send/receive messages over TCP/IP. I refer drivers/block/nbd.c */
2412 +int usbip_xmit(int send, struct socket *sock, char *buf,
2413 +              int size, int msg_flags)
2414 +{
2415 +       int result;
2416 +       struct msghdr msg;
2417 +       struct kvec iov;
2418 +       int total = 0;
2419 +
2420 +       /* for blocks of if (dbg_flag_xmit) */
2421 +       char *bp = buf;
2422 +       int osize = size;
2423 +
2424 +       dbg_xmit("enter\n");
2425 +
2426 +       if (!sock || !buf || !size) {
2427 +               printk(KERN_ERR "%s: invalid arg, sock %p buff %p size %d\n",
2428 +                      __func__, sock, buf, size);
2429 +               return -EINVAL;
2430 +       }
2431 +
2432 +
2433 +       if (dbg_flag_xmit) {
2434 +               if (send) {
2435 +                       if (!in_interrupt())
2436 +                               printk(KERN_DEBUG "%-10s:", current->comm);
2437 +                       else
2438 +                               printk(KERN_DEBUG "interupt  :");
2439 +
2440 +                       printk("%s: sending... , sock %p, buf %p, "
2441 +                              "size %d, msg_flags %d\n", __func__,
2442 +                              sock, buf, size, msg_flags);
2443 +                       usbip_dump_buffer(buf, size);
2444 +               }
2445 +       }
2446 +
2447 +
2448 +       do {
2449 +               sock->sk->sk_allocation = GFP_NOIO;
2450 +               iov.iov_base    = buf;
2451 +               iov.iov_len     = size;
2452 +               msg.msg_name    = NULL;
2453 +               msg.msg_namelen = 0;
2454 +               msg.msg_control = NULL;
2455 +               msg.msg_controllen = 0;
2456 +               msg.msg_namelen    = 0;
2457 +               msg.msg_flags      = msg_flags | MSG_NOSIGNAL;
2458 +
2459 +               if (send)
2460 +                       result = kernel_sendmsg(sock, &msg, &iov, 1, size);
2461 +               else
2462 +                       result = kernel_recvmsg(sock, &msg, &iov, 1, size,
2463 +                                                               MSG_WAITALL);
2464 +
2465 +               if (result <= 0) {
2466 +                       udbg("usbip_xmit: %s sock %p buf %p size %u ret %d"
2467 +                                       " total %d\n",
2468 +                                       send ? "send" : "receive", sock, buf,
2469 +                                       size, result, total);
2470 +                       goto err;
2471 +               }
2472 +
2473 +               size -= result;
2474 +               buf += result;
2475 +               total += result;
2476 +
2477 +       } while (size > 0);
2478 +
2479 +
2480 +       if (dbg_flag_xmit) {
2481 +               if (!send) {
2482 +                       if (!in_interrupt())
2483 +                               printk(KERN_DEBUG "%-10s:", current->comm);
2484 +                       else
2485 +                               printk(KERN_DEBUG "interupt  :");
2486 +
2487 +                       printk("usbip_xmit: receiving....\n");
2488 +                       usbip_dump_buffer(bp, osize);
2489 +                       printk("usbip_xmit: received, osize %d ret %d size %d "
2490 +                                       "total %d\n", osize, result, size,
2491 +                                       total);
2492 +               }
2493 +
2494 +               if (send)
2495 +                       printk("usbip_xmit: send, total %d\n", total);
2496 +       }
2497 +
2498 +       return total;
2499 +
2500 +err:
2501 +       return result;
2502 +}
2503 +EXPORT_SYMBOL_GPL(usbip_xmit);
2504 +
2505 +
2506 +/* now a usrland utility should set options. */
2507 +#if 0
2508 +int setquickack(struct socket *socket)
2509 +{
2510 +       mm_segment_t oldfs;
2511 +       int val = 1;
2512 +       int ret;
2513 +
2514 +       oldfs = get_fs();
2515 +       set_fs(get_ds());
2516 +       ret = socket->ops->setsockopt(socket, SOL_TCP, TCP_QUICKACK,
2517 +                       (char __user *) &val, sizeof(ret));
2518 +       set_fs(oldfs);
2519 +
2520 +       return ret;
2521 +}
2522 +
2523 +int setnodelay(struct socket *socket)
2524 +{
2525 +       mm_segment_t oldfs;
2526 +       int val = 1;
2527 +       int ret;
2528 +
2529 +       oldfs = get_fs();
2530 +       set_fs(get_ds());
2531 +       ret = socket->ops->setsockopt(socket, SOL_TCP, TCP_NODELAY,
2532 +                       (char __user *) &val, sizeof(ret));
2533 +       set_fs(oldfs);
2534 +
2535 +       return ret;
2536 +}
2537 +
2538 +int setkeepalive(struct socket *socket)
2539 +{
2540 +       mm_segment_t oldfs;
2541 +       int val = 1;
2542 +       int ret;
2543 +
2544 +       oldfs = get_fs();
2545 +       set_fs(get_ds());
2546 +       ret = socket->ops->setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE,
2547 +                       (char __user *) &val, sizeof(ret));
2548 +       set_fs(oldfs);
2549 +
2550 +       return ret;
2551 +}
2552 +
2553 +void setreuse(struct socket *socket)
2554 +{
2555 +       socket->sk->sk_reuse = 1;
2556 +}
2557 +#endif
2558 +
2559 +struct socket *sockfd_to_socket(unsigned int sockfd)
2560 +{
2561 +       struct socket *socket;
2562 +       struct file *file;
2563 +       struct inode *inode;
2564 +
2565 +       file = fget(sockfd);
2566 +       if (!file) {
2567 +               printk(KERN_ERR "%s: invalid sockfd\n", __func__);
2568 +               return NULL;
2569 +       }
2570 +
2571 +       inode = file->f_dentry->d_inode;
2572 +
2573 +       if (!inode || !S_ISSOCK(inode->i_mode))
2574 +               return NULL;
2575 +
2576 +       socket = SOCKET_I(inode);
2577 +
2578 +       return socket;
2579 +}
2580 +EXPORT_SYMBOL_GPL(sockfd_to_socket);
2581 +
2582 +
2583 +
2584 +/*-------------------------------------------------------------------------*/
2585 +/* pdu routines */
2586 +
2587 +/* there may be more cases to tweak the flags. */
2588 +static unsigned int tweak_transfer_flags(unsigned int flags)
2589 +{
2590 +
2591 +       if (flags & URB_NO_TRANSFER_DMA_MAP)
2592 +               /*
2593 +                * vhci_hcd does not provide DMA-mapped I/O. The upper
2594 +                * driver does not need to set this flag. The remote
2595 +                * usbip.ko does not still perform DMA-mapped I/O for
2596 +                * DMA-caplable host controllers. So, clear this flag.
2597 +                */
2598 +               flags &= ~URB_NO_TRANSFER_DMA_MAP;
2599 +
2600 +       if (flags & URB_NO_SETUP_DMA_MAP)
2601 +               flags &= ~URB_NO_SETUP_DMA_MAP;
2602 +
2603 +       return flags;
2604 +}
2605 +
2606 +static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
2607 +                                                               int pack)
2608 +{
2609 +       struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
2610 +
2611 +       /*
2612 +        * Some members are not still implemented in usbip. I hope this issue
2613 +        * will be discussed when usbip is ported to other operating systems.
2614 +        */
2615 +       if (pack) {
2616 +               /* vhci_tx.c */
2617 +               spdu->transfer_flags =
2618 +                               tweak_transfer_flags(urb->transfer_flags);
2619 +               spdu->transfer_buffer_length    = urb->transfer_buffer_length;
2620 +               spdu->start_frame               = urb->start_frame;
2621 +               spdu->number_of_packets         = urb->number_of_packets;
2622 +               spdu->interval                  = urb->interval;
2623 +       } else  {
2624 +               /* stub_rx.c */
2625 +               urb->transfer_flags         = spdu->transfer_flags;
2626 +
2627 +               urb->transfer_buffer_length = spdu->transfer_buffer_length;
2628 +               urb->start_frame            = spdu->start_frame;
2629 +               urb->number_of_packets      = spdu->number_of_packets;
2630 +               urb->interval               = spdu->interval;
2631 +       }
2632 +}
2633 +
2634 +static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
2635 +                                                               int pack)
2636 +{
2637 +       struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
2638 +
2639 +       if (pack) {
2640 +               /* stub_tx.c */
2641 +
2642 +               rpdu->status            = urb->status;
2643 +               rpdu->actual_length     = urb->actual_length;
2644 +               rpdu->start_frame       = urb->start_frame;
2645 +               rpdu->error_count       = urb->error_count;
2646 +       } else {
2647 +               /* vhci_rx.c */
2648 +
2649 +               urb->status             = rpdu->status;
2650 +               urb->actual_length      = rpdu->actual_length;
2651 +               urb->start_frame        = rpdu->start_frame;
2652 +               urb->error_count        = rpdu->error_count;
2653 +       }
2654 +}
2655 +
2656 +
2657 +void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
2658 +                                                               int pack)
2659 +{
2660 +       switch (cmd) {
2661 +       case USBIP_CMD_SUBMIT:
2662 +               usbip_pack_cmd_submit(pdu, urb, pack);
2663 +               break;
2664 +       case USBIP_RET_SUBMIT:
2665 +               usbip_pack_ret_submit(pdu, urb, pack);
2666 +               break;
2667 +       default:
2668 +               err("unknown command");
2669 +               /* NOTREACHED */
2670 +               /* BUG(); */
2671 +       }
2672 +}
2673 +EXPORT_SYMBOL_GPL(usbip_pack_pdu);
2674 +
2675 +
2676 +static void correct_endian_basic(struct usbip_header_basic *base, int send)
2677 +{
2678 +       if (send) {
2679 +               base->command   = cpu_to_be32(base->command);
2680 +               base->seqnum    = cpu_to_be32(base->seqnum);
2681 +               base->devid     = cpu_to_be32(base->devid);
2682 +               base->direction = cpu_to_be32(base->direction);
2683 +               base->ep        = cpu_to_be32(base->ep);
2684 +       } else {
2685 +               base->command   = be32_to_cpu(base->command);
2686 +               base->seqnum    = be32_to_cpu(base->seqnum);
2687 +               base->devid     = be32_to_cpu(base->devid);
2688 +               base->direction = be32_to_cpu(base->direction);
2689 +               base->ep        = be32_to_cpu(base->ep);
2690 +       }
2691 +}
2692 +
2693 +static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
2694 +                                                               int send)
2695 +{
2696 +       if (send) {
2697 +               pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
2698 +
2699 +               cpu_to_be32s(&pdu->transfer_buffer_length);
2700 +               cpu_to_be32s(&pdu->start_frame);
2701 +               cpu_to_be32s(&pdu->number_of_packets);
2702 +               cpu_to_be32s(&pdu->interval);
2703 +       } else {
2704 +               pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
2705 +
2706 +               be32_to_cpus(&pdu->transfer_buffer_length);
2707 +               be32_to_cpus(&pdu->start_frame);
2708 +               be32_to_cpus(&pdu->number_of_packets);
2709 +               be32_to_cpus(&pdu->interval);
2710 +       }
2711 +}
2712 +
2713 +static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
2714 +                                                               int send)
2715 +{
2716 +       if (send) {
2717 +               cpu_to_be32s(&pdu->status);
2718 +               cpu_to_be32s(&pdu->actual_length);
2719 +               cpu_to_be32s(&pdu->start_frame);
2720 +               cpu_to_be32s(&pdu->error_count);
2721 +       } else {
2722 +               be32_to_cpus(&pdu->status);
2723 +               be32_to_cpus(&pdu->actual_length);
2724 +               be32_to_cpus(&pdu->start_frame);
2725 +               be32_to_cpus(&pdu->error_count);
2726 +       }
2727 +}
2728 +
2729 +static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
2730 +                                                               int send)
2731 +{
2732 +       if (send)
2733 +               pdu->seqnum = cpu_to_be32(pdu->seqnum);
2734 +       else
2735 +               pdu->seqnum = be32_to_cpu(pdu->seqnum);
2736 +}
2737 +
2738 +static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
2739 +                                                               int send)
2740 +{
2741 +       if (send)
2742 +               cpu_to_be32s(&pdu->status);
2743 +       else
2744 +               be32_to_cpus(&pdu->status);
2745 +}
2746 +
2747 +void usbip_header_correct_endian(struct usbip_header *pdu, int send)
2748 +{
2749 +       __u32 cmd = 0;
2750 +
2751 +       if (send)
2752 +               cmd = pdu->base.command;
2753 +
2754 +       correct_endian_basic(&pdu->base, send);
2755 +
2756 +       if (!send)
2757 +               cmd = pdu->base.command;
2758 +
2759 +       switch (cmd) {
2760 +       case USBIP_CMD_SUBMIT:
2761 +               correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
2762 +               break;
2763 +       case USBIP_RET_SUBMIT:
2764 +               correct_endian_ret_submit(&pdu->u.ret_submit, send);
2765 +               break;
2766 +       case USBIP_CMD_UNLINK:
2767 +               correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
2768 +               break;
2769 +       case USBIP_RET_UNLINK:
2770 +               correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
2771 +               break;
2772 +       default:
2773 +               /* NOTREACHED */
2774 +               err("unknown command in pdu header: %d", cmd);
2775 +               /* BUG(); */
2776 +       }
2777 +}
2778 +EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
2779 +
2780 +static void usbip_iso_pakcet_correct_endian(
2781 +                               struct usbip_iso_packet_descriptor *iso,
2782 +                               int send)
2783 +{
2784 +       /* does not need all members. but copy all simply. */
2785 +       if (send) {
2786 +               iso->offset     = cpu_to_be32(iso->offset);
2787 +               iso->length     = cpu_to_be32(iso->length);
2788 +               iso->status     = cpu_to_be32(iso->status);
2789 +               iso->actual_length = cpu_to_be32(iso->actual_length);
2790 +       } else {
2791 +               iso->offset     = be32_to_cpu(iso->offset);
2792 +               iso->length     = be32_to_cpu(iso->length);
2793 +               iso->status     = be32_to_cpu(iso->status);
2794 +               iso->actual_length = be32_to_cpu(iso->actual_length);
2795 +       }
2796 +}
2797 +
2798 +static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
2799 +               struct usb_iso_packet_descriptor *uiso, int pack)
2800 +{
2801 +       if (pack) {
2802 +               iso->offset             = uiso->offset;
2803 +               iso->length             = uiso->length;
2804 +               iso->status             = uiso->status;
2805 +               iso->actual_length      = uiso->actual_length;
2806 +       } else {
2807 +               uiso->offset            = iso->offset;
2808 +               uiso->length            = iso->length;
2809 +               uiso->status            = iso->status;
2810 +               uiso->actual_length     = iso->actual_length;
2811 +       }
2812 +}
2813 +
2814 +
2815 +/* must free buffer */
2816 +void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
2817 +{
2818 +       void *buff;
2819 +       struct usbip_iso_packet_descriptor *iso;
2820 +       int np = urb->number_of_packets;
2821 +       ssize_t size = np * sizeof(*iso);
2822 +       int i;
2823 +
2824 +       buff = kzalloc(size, GFP_KERNEL);
2825 +       if (!buff)
2826 +               return NULL;
2827 +
2828 +       for (i = 0; i < np; i++) {
2829 +               iso = buff + (i * sizeof(*iso));
2830 +
2831 +               usbip_pack_iso(iso, &urb->iso_frame_desc[i], 1);
2832 +               usbip_iso_pakcet_correct_endian(iso, 1);
2833 +       }
2834 +
2835 +       *bufflen = size;
2836 +
2837 +       return buff;
2838 +}
2839 +EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
2840 +
2841 +/* some members of urb must be substituted before. */
2842 +int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
2843 +{
2844 +       void *buff;
2845 +       struct usbip_iso_packet_descriptor *iso;
2846 +       int np = urb->number_of_packets;
2847 +       int size = np * sizeof(*iso);
2848 +       int i;
2849 +       int ret;
2850 +
2851 +       if (!usb_pipeisoc(urb->pipe))
2852 +               return 0;
2853 +
2854 +       /* my Bluetooth dongle gets ISO URBs which are np = 0 */
2855 +       if (np == 0) {
2856 +               /* uinfo("iso np == 0\n"); */
2857 +               /* usbip_dump_urb(urb); */
2858 +               return 0;
2859 +       }
2860 +
2861 +       buff = kzalloc(size, GFP_KERNEL);
2862 +       if (!buff)
2863 +               return -ENOMEM;
2864 +
2865 +       ret = usbip_xmit(0, ud->tcp_socket, buff, size, 0);
2866 +       if (ret != size) {
2867 +               dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
2868 +                       ret);
2869 +               kfree(buff);
2870 +
2871 +               if (ud->side == USBIP_STUB)
2872 +                       usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
2873 +               else
2874 +                       usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
2875 +
2876 +               return -EPIPE;
2877 +       }
2878 +
2879 +       for (i = 0; i < np; i++) {
2880 +               iso = buff + (i * sizeof(*iso));
2881 +
2882 +               usbip_iso_pakcet_correct_endian(iso, 0);
2883 +               usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0);
2884 +       }
2885 +
2886 +
2887 +       kfree(buff);
2888 +
2889 +       return ret;
2890 +}
2891 +EXPORT_SYMBOL_GPL(usbip_recv_iso);
2892 +
2893 +
2894 +/* some members of urb must be substituted before. */
2895 +int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
2896 +{
2897 +       int ret;
2898 +       int size;
2899 +
2900 +       if (ud->side == USBIP_STUB) {
2901 +               /* stub_rx.c */
2902 +               /* the direction of urb must be OUT. */
2903 +               if (usb_pipein(urb->pipe))
2904 +                       return 0;
2905 +
2906 +               size = urb->transfer_buffer_length;
2907 +       } else {
2908 +               /* vhci_rx.c */
2909 +               /* the direction of urb must be IN. */
2910 +               if (usb_pipeout(urb->pipe))
2911 +                       return 0;
2912 +
2913 +               size = urb->actual_length;
2914 +       }
2915 +
2916 +       /* no need to recv xbuff */
2917 +       if (!(size > 0))
2918 +               return 0;
2919 +
2920 +       ret = usbip_xmit(0, ud->tcp_socket, (char *)urb->transfer_buffer,
2921 +                        size, 0);
2922 +       if (ret != size) {
2923 +               dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
2924 +               if (ud->side == USBIP_STUB) {
2925 +                       usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
2926 +               } else {
2927 +                       usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
2928 +                       return -EPIPE;
2929 +               }
2930 +       }
2931 +
2932 +       return ret;
2933 +}
2934 +EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
2935 +
2936 +
2937 +/*-------------------------------------------------------------------------*/
2938 +
2939 +static int __init usbip_common_init(void)
2940 +{
2941 +       printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "" DRIVER_VERSION);
2942 +
2943 +       return 0;
2944 +}
2945 +
2946 +static void __exit usbip_common_exit(void)
2947 +{
2948 +       return;
2949 +}
2950 +
2951 +
2952 +
2953 +
2954 +module_init(usbip_common_init);
2955 +module_exit(usbip_common_exit);
2956 +
2957 +MODULE_AUTHOR(DRIVER_AUTHOR);
2958 +MODULE_DESCRIPTION(DRIVER_DESC);
2959 +MODULE_LICENSE("GPL");
2960 --- /dev/null
2961 +++ kernel-2.6.28/drivers/usb/usbip/usbip_common.h
2962 @@ -0,0 +1,406 @@
2963 +/*
2964 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
2965 + *
2966 + * This is free software; you can redistribute it and/or modify
2967 + * it under the terms of the GNU General Public License as published by
2968 + * the Free Software Foundation; either version 2 of the License, or
2969 + * (at your option) any later version.
2970 + *
2971 + * This is distributed in the hope that it will be useful,
2972 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2973 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2974 + * GNU General Public License for more details.
2975 + *
2976 + * You should have received a copy of the GNU General Public License
2977 + * along with this program; if not, write to the Free Software
2978 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
2979 + * USA.
2980 + */
2981 +
2982 +#ifndef __VHCI_COMMON_H
2983 +#define __VHCI_COMMON_H
2984 +
2985 +
2986 +#include <linux/version.h>
2987 +#include <linux/usb.h>
2988 +#include <asm/byteorder.h>
2989 +#include <net/sock.h>
2990 +
2991 +/*-------------------------------------------------------------------------*/
2992 +
2993 +/*
2994 + * define macros to print messages
2995 + */
2996 +
2997 +/**
2998 + * udbg - print debug messages if CONFIG_USB_DEBUG is defined
2999 + * @fmt:
3000 + * @args:
3001 + */
3002 +
3003 +#ifdef CONFIG_USB_DEBUG
3004 +
3005 +#define udbg(fmt, args...)                                             \
3006 +       do {                                                            \
3007 +               printk(KERN_DEBUG "%-10s:(%s,%d) %s: " fmt,             \
3008 +                       (in_interrupt() ? "interrupt" : (current)->comm),\
3009 +                       __FILE__, __LINE__, __func__, ##args);  \
3010 +       } while (0)
3011 +
3012 +#else  /* CONFIG_USB_DEBUG */
3013 +
3014 +#define udbg(fmt, args...)             do { } while (0)
3015 +
3016 +#endif /* CONFIG_USB_DEBUG */
3017 +
3018 +
3019 +enum {
3020 +       usbip_debug_xmit        = (1 << 0),
3021 +       usbip_debug_sysfs       = (1 << 1),
3022 +       usbip_debug_urb         = (1 << 2),
3023 +       usbip_debug_eh          = (1 << 3),
3024 +
3025 +       usbip_debug_stub_cmp    = (1 << 8),
3026 +       usbip_debug_stub_dev    = (1 << 9),
3027 +       usbip_debug_stub_rx     = (1 << 10),
3028 +       usbip_debug_stub_tx     = (1 << 11),
3029 +
3030 +       usbip_debug_vhci_rh     = (1 << 8),
3031 +       usbip_debug_vhci_hc     = (1 << 9),
3032 +       usbip_debug_vhci_rx     = (1 << 10),
3033 +       usbip_debug_vhci_tx     = (1 << 11),
3034 +       usbip_debug_vhci_sysfs  = (1 << 12)
3035 +};
3036 +
3037 +#define dbg_flag_xmit          (usbip_debug_flag & usbip_debug_xmit)
3038 +#define dbg_flag_vhci_rh       (usbip_debug_flag & usbip_debug_vhci_rh)
3039 +#define dbg_flag_vhci_hc       (usbip_debug_flag & usbip_debug_vhci_hc)
3040 +#define dbg_flag_vhci_rx       (usbip_debug_flag & usbip_debug_vhci_rx)
3041 +#define dbg_flag_vhci_tx       (usbip_debug_flag & usbip_debug_vhci_tx)
3042 +#define dbg_flag_vhci_sysfs    (usbip_debug_flag & usbip_debug_vhci_sysfs)
3043 +#define dbg_flag_stub_rx       (usbip_debug_flag & usbip_debug_stub_rx)
3044 +#define dbg_flag_stub_tx       (usbip_debug_flag & usbip_debug_stub_tx)
3045 +
3046 +extern unsigned long usbip_debug_flag;
3047 +extern struct device_attribute dev_attr_usbip_debug;
3048 +
3049 +#define dbg_with_flag(flag, fmt, args...)              \
3050 +       do {                                            \
3051 +               if (flag & usbip_debug_flag)            \
3052 +                       udbg(fmt , ##args);             \
3053 +       } while (0)
3054 +
3055 +#define dbg_sysfs(fmt, args...)                \
3056 +       dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
3057 +#define dbg_xmit(fmt, args...)         \
3058 +       dbg_with_flag(usbip_debug_xmit, fmt , ##args)
3059 +#define dbg_urb(fmt, args...)          \
3060 +       dbg_with_flag(usbip_debug_urb, fmt , ##args)
3061 +#define dbg_eh(fmt, args...)           \
3062 +       dbg_with_flag(usbip_debug_eh, fmt , ##args)
3063 +
3064 +#define dbg_vhci_rh(fmt, args...)      \
3065 +       dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args)
3066 +#define dbg_vhci_hc(fmt, args...)      \
3067 +       dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args)
3068 +#define dbg_vhci_rx(fmt, args...)      \
3069 +       dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args)
3070 +#define dbg_vhci_tx(fmt, args...)      \
3071 +       dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args)
3072 +#define dbg_vhci_sysfs(fmt, args...)   \
3073 +       dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
3074 +
3075 +#define dbg_stub_cmp(fmt, args...)     \
3076 +       dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
3077 +#define dbg_stub_rx(fmt, args...)      \
3078 +       dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
3079 +#define dbg_stub_tx(fmt, args...)      \
3080 +       dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
3081 +
3082 +
3083 +/**
3084 + * uerr - print error messages
3085 + * @fmt:
3086 + * @args:
3087 + */
3088 +#define uerr(fmt, args...)                                             \
3089 +       do {                                                            \
3090 +               printk(KERN_ERR "%-10s: ***ERROR*** (%s,%d) %s: " fmt,  \
3091 +                       (in_interrupt() ? "interrupt" : (current)->comm),\
3092 +                       __FILE__, __LINE__, __func__, ##args);  \
3093 +       } while (0)
3094 +
3095 +/**
3096 + * uinfo - print information messages
3097 + * @fmt:
3098 + * @args:
3099 + */
3100 +#define uinfo(fmt, args...)                                    \
3101 +       do {                                                    \
3102 +               printk(KERN_INFO "usbip: " fmt , ## args);      \
3103 +       } while (0)
3104 +
3105 +
3106 +/*-------------------------------------------------------------------------*/
3107 +
3108 +/*
3109 + * USB/IP request headers.
3110 + * Currently, we define 4 request types:
3111 + *
3112 + *  - CMD_SUBMIT transfers a USB request, corresponding to usb_submit_urb().
3113 + *    (client to server)
3114 + *  - RET_RETURN transfers the result of CMD_SUBMIT.
3115 + *    (server to client)
3116 + *  - CMD_UNLINK transfers an unlink request of a pending USB request.
3117 + *    (client to server)
3118 + *  - RET_UNLINK transfers the result of CMD_UNLINK.
3119 + *    (server to client)
3120 + *
3121 + * Note: The below request formats are based on the USB subsystem of Linux. Its
3122 + * details will be defined when other implementations come.
3123 + *
3124 + *
3125 + */
3126 +
3127 +/*
3128 + * A basic header followed by other additional headers.
3129 + */
3130 +struct usbip_header_basic {
3131 +#define USBIP_CMD_SUBMIT       0x0001
3132 +#define USBIP_CMD_UNLINK       0x0002
3133 +#define USBIP_RET_SUBMIT       0x0003
3134 +#define USBIP_RET_UNLINK       0x0004
3135 +       __u32 command;
3136 +
3137 +        /* sequencial number which identifies requests.
3138 +         * incremented per connections */
3139 +       __u32 seqnum;
3140 +
3141 +       /* devid is used to specify a remote USB device uniquely instead
3142 +        * of busnum and devnum in Linux. In the case of Linux stub_driver,
3143 +        * this value is ((busnum << 16) | devnum) */
3144 +       __u32 devid;
3145 +
3146 +#define USBIP_DIR_OUT  0
3147 +#define USBIP_DIR_IN   1
3148 +       __u32 direction;
3149 +       __u32 ep;     /* endpoint number */
3150 +} __attribute__ ((packed));
3151 +
3152 +/*
3153 + * An additional header for a CMD_SUBMIT packet.
3154 + */
3155 +struct usbip_header_cmd_submit {
3156 +       /* these values are basically the same as in a URB. */
3157 +
3158 +       /* the same in a URB. */
3159 +       __u32 transfer_flags;
3160 +
3161 +       /* set the following data size (out),
3162 +        * or expected reading data size (in) */
3163 +       __s32 transfer_buffer_length;
3164 +
3165 +       /* it is difficult for usbip to sync frames (reserved only?) */
3166 +       __s32 start_frame;
3167 +
3168 +       /* the number of iso descriptors that follows this header */
3169 +       __s32 number_of_packets;
3170 +
3171 +       /* the maximum time within which this request works in a host
3172 +        * controller of a server side */
3173 +       __s32 interval;
3174 +
3175 +       /* set setup packet data for a CTRL request */
3176 +       unsigned char setup[8];
3177 +} __attribute__ ((packed));
3178 +
3179 +/*
3180 + * An additional header for a RET_SUBMIT packet.
3181 + */
3182 +struct usbip_header_ret_submit {
3183 +       __s32 status;
3184 +       __s32 actual_length; /* returned data length */
3185 +       __s32 start_frame; /* ISO and INT */
3186 +       __s32 number_of_packets;  /* ISO only */
3187 +       __s32 error_count; /* ISO only */
3188 +} __attribute__ ((packed));
3189 +
3190 +/*
3191 + * An additional header for a CMD_UNLINK packet.
3192 + */
3193 +struct usbip_header_cmd_unlink {
3194 +       __u32 seqnum; /* URB's seqnum which will be unlinked */
3195 +} __attribute__ ((packed));
3196 +
3197 +
3198 +/*
3199 + * An additional header for a RET_UNLINK packet.
3200 + */
3201 +struct usbip_header_ret_unlink {
3202 +       __s32 status;
3203 +} __attribute__ ((packed));
3204 +
3205 +
3206 +/* the same as usb_iso_packet_descriptor but packed for pdu */
3207 +struct usbip_iso_packet_descriptor {
3208 +       __u32 offset;
3209 +       __u32 length;            /* expected length */
3210 +       __u32 actual_length;
3211 +       __u32 status;
3212 +} __attribute__ ((packed));
3213 +
3214 +
3215 +/*
3216 + * All usbip packets use a common header to keep code simple.
3217 + */
3218 +struct usbip_header {
3219 +       struct usbip_header_basic base;
3220 +
3221 +       union {
3222 +               struct usbip_header_cmd_submit  cmd_submit;
3223 +               struct usbip_header_ret_submit  ret_submit;
3224 +               struct usbip_header_cmd_unlink  cmd_unlink;
3225 +               struct usbip_header_ret_unlink  ret_unlink;
3226 +       } u;
3227 +} __attribute__ ((packed));
3228 +
3229 +
3230 +
3231 +
3232 +/*-------------------------------------------------------------------------*/
3233 +
3234 +
3235 +int usbip_xmit(int, struct socket *, char *, int, int);
3236 +int usbip_sendmsg(struct socket *, struct msghdr *, int);
3237 +
3238 +
3239 +static inline int interface_to_busnum(struct usb_interface *interface)
3240 +{
3241 +       struct usb_device *udev = interface_to_usbdev(interface);
3242 +       return udev->bus->busnum;
3243 +}
3244 +
3245 +static inline int interface_to_devnum(struct usb_interface *interface)
3246 +{
3247 +       struct usb_device *udev = interface_to_usbdev(interface);
3248 +       return udev->devnum;
3249 +}
3250 +
3251 +static inline int interface_to_infnum(struct usb_interface *interface)
3252 +{
3253 +       return interface->cur_altsetting->desc.bInterfaceNumber;
3254 +}
3255 +
3256 +#if 0
3257 +int setnodelay(struct socket *);
3258 +int setquickack(struct socket *);
3259 +int setkeepalive(struct socket *socket);
3260 +void setreuse(struct socket *);
3261 +#endif
3262 +
3263 +struct socket *sockfd_to_socket(unsigned int);
3264 +int set_sockaddr(struct socket *socket, struct sockaddr_storage *ss);
3265 +
3266 +void usbip_dump_urb(struct urb *purb);
3267 +void usbip_dump_header(struct usbip_header *pdu);
3268 +
3269 +
3270 +struct usbip_device;
3271 +
3272 +struct usbip_task {
3273 +       struct task_struct *thread;
3274 +       struct completion thread_done;
3275 +       char *name;
3276 +       void (*loop_ops)(struct usbip_task *);
3277 +};
3278 +
3279 +enum usbip_side {
3280 +       USBIP_VHCI,
3281 +       USBIP_STUB,
3282 +};
3283 +
3284 +enum usbip_status {
3285 +       /* sdev is available. */
3286 +       SDEV_ST_AVAILABLE = 0x01,
3287 +       /* sdev is now used. */
3288 +       SDEV_ST_USED,
3289 +       /* sdev is unusable because of a fatal error. */
3290 +       SDEV_ST_ERROR,
3291 +
3292 +       /* vdev does not connect a remote device. */
3293 +       VDEV_ST_NULL,
3294 +       /* vdev is used, but the USB address is not assigned yet */
3295 +       VDEV_ST_NOTASSIGNED,
3296 +       VDEV_ST_USED,
3297 +       VDEV_ST_ERROR
3298 +};
3299 +
3300 +/* a common structure for stub_device and vhci_device */
3301 +struct usbip_device {
3302 +       enum usbip_side side;
3303 +
3304 +       enum usbip_status status;
3305 +
3306 +       /* lock for status */
3307 +       spinlock_t lock;
3308 +
3309 +       struct socket *tcp_socket;
3310 +
3311 +       struct usbip_task tcp_rx;
3312 +       struct usbip_task tcp_tx;
3313 +
3314 +       /* event handler */
3315 +#define USBIP_EH_SHUTDOWN      (1 << 0)
3316 +#define USBIP_EH_BYE           (1 << 1)
3317 +#define USBIP_EH_RESET         (1 << 2)
3318 +#define USBIP_EH_UNUSABLE      (1 << 3)
3319 +
3320 +#define SDEV_EVENT_REMOVED     (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
3321 +#define        SDEV_EVENT_DOWN         (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
3322 +#define        SDEV_EVENT_ERROR_TCP    (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
3323 +#define        SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
3324 +#define        SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
3325 +
3326 +#define        VDEV_EVENT_REMOVED      (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
3327 +#define        VDEV_EVENT_DOWN         (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
3328 +#define        VDEV_EVENT_ERROR_TCP    (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
3329 +#define        VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
3330 +
3331 +       unsigned long event;
3332 +       struct usbip_task eh;
3333 +       wait_queue_head_t eh_waitq;
3334 +
3335 +       struct eh_ops {
3336 +               void (*shutdown)(struct usbip_device *);
3337 +               void (*reset)(struct usbip_device *);
3338 +               void (*unusable)(struct usbip_device *);
3339 +       } eh_ops;
3340 +};
3341 +
3342 +
3343 +void usbip_task_init(struct usbip_task *ut, char *,
3344 +                               void (*loop_ops)(struct usbip_task *));
3345 +
3346 +void usbip_start_threads(struct usbip_device *ud);
3347 +void usbip_stop_threads(struct usbip_device *ud);
3348 +int usbip_thread(void *param);
3349 +
3350 +void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
3351 +                                                               int pack);
3352 +
3353 +void usbip_header_correct_endian(struct usbip_header *pdu, int send);
3354 +/* some members of urb must be substituted before. */
3355 +int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
3356 +/* some members of urb must be substituted before. */
3357 +int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
3358 +void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
3359 +
3360 +
3361 +/* usbip_event.c */
3362 +void usbip_start_eh(struct usbip_device *ud);
3363 +void usbip_stop_eh(struct usbip_device *ud);
3364 +void usbip_event_add(struct usbip_device *ud, unsigned long event);
3365 +int usbip_event_happend(struct usbip_device *ud);
3366 +
3367 +
3368 +#endif
3369 --- /dev/null
3370 +++ kernel-2.6.28/drivers/usb/usbip/usbip_event.c
3371 @@ -0,0 +1,141 @@
3372 +/*
3373 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
3374 + *
3375 + * This is free software; you can redistribute it and/or modify
3376 + * it under the terms of the GNU General Public License as published by
3377 + * the Free Software Foundation; either version 2 of the License, or
3378 + * (at your option) any later version.
3379 + *
3380 + * This is distributed in the hope that it will be useful,
3381 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3382 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3383 + * GNU General Public License for more details.
3384 + *
3385 + * You should have received a copy of the GNU General Public License
3386 + * along with this program; if not, write to the Free Software
3387 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
3388 + * USA.
3389 + */
3390 +
3391 +#include "usbip_common.h"
3392 +
3393 +static int event_handler(struct usbip_device *ud)
3394 +{
3395 +       dbg_eh("enter\n");
3396 +
3397 +       /*
3398 +        * Events are handled by only this thread.
3399 +        */
3400 +       while (usbip_event_happend(ud)) {
3401 +               dbg_eh("pending event %lx\n", ud->event);
3402 +
3403 +               /*
3404 +                * NOTE: shutdown must come first.
3405 +                * Shutdown the device.
3406 +                */
3407 +               if (ud->event & USBIP_EH_SHUTDOWN) {
3408 +                       ud->eh_ops.shutdown(ud);
3409 +
3410 +                       ud->event &= ~USBIP_EH_SHUTDOWN;
3411 +
3412 +                       break;
3413 +               }
3414 +
3415 +               /* Stop the error handler. */
3416 +               if (ud->event & USBIP_EH_BYE)
3417 +                       return -1;
3418 +
3419 +               /* Reset the device. */
3420 +               if (ud->event & USBIP_EH_RESET) {
3421 +                       ud->eh_ops.reset(ud);
3422 +
3423 +                       ud->event &= ~USBIP_EH_RESET;
3424 +
3425 +                       break;
3426 +               }
3427 +
3428 +               /* Mark the device as unusable. */
3429 +               if (ud->event & USBIP_EH_UNUSABLE) {
3430 +                       ud->eh_ops.unusable(ud);
3431 +
3432 +                       ud->event &= ~USBIP_EH_UNUSABLE;
3433 +
3434 +                       break;
3435 +               }
3436 +
3437 +               /* NOTREACHED */
3438 +               printk(KERN_ERR "%s: unknown event\n", __func__);
3439 +               return -1;
3440 +       }
3441 +
3442 +       return 0;
3443 +}
3444 +
3445 +static void event_handler_loop(struct usbip_task *ut)
3446 +{
3447 +       struct usbip_device *ud = container_of(ut, struct usbip_device, eh);
3448 +
3449 +       while (1) {
3450 +               if (signal_pending(current)) {
3451 +                       dbg_eh("signal catched!\n");
3452 +                       break;
3453 +               }
3454 +
3455 +               if (event_handler(ud) < 0)
3456 +                       break;
3457 +
3458 +               wait_event_interruptible(ud->eh_waitq, usbip_event_happend(ud));
3459 +               dbg_eh("wakeup\n");
3460 +       }
3461 +}
3462 +
3463 +void usbip_start_eh(struct usbip_device *ud)
3464 +{
3465 +       struct usbip_task *eh = &ud->eh;
3466 +
3467 +       init_waitqueue_head(&ud->eh_waitq);
3468 +       ud->event = 0;
3469 +
3470 +       usbip_task_init(eh, "usbip_eh", event_handler_loop);
3471 +
3472 +       kernel_thread(usbip_thread, (void *)eh, 0);
3473 +
3474 +       wait_for_completion(&eh->thread_done);
3475 +}
3476 +EXPORT_SYMBOL_GPL(usbip_start_eh);
3477 +
3478 +void usbip_stop_eh(struct usbip_device *ud)
3479 +{
3480 +       struct usbip_task *eh = &ud->eh;
3481 +
3482 +       wait_for_completion(&eh->thread_done);
3483 +       dbg_eh("usbip_eh has finished\n");
3484 +}
3485 +EXPORT_SYMBOL_GPL(usbip_stop_eh);
3486 +
3487 +void usbip_event_add(struct usbip_device *ud, unsigned long event)
3488 +{
3489 +       spin_lock(&ud->lock);
3490 +
3491 +       ud->event |= event;
3492 +
3493 +       wake_up(&ud->eh_waitq);
3494 +
3495 +       spin_unlock(&ud->lock);
3496 +}
3497 +EXPORT_SYMBOL_GPL(usbip_event_add);
3498 +
3499 +int usbip_event_happend(struct usbip_device *ud)
3500 +{
3501 +       int happend = 0;
3502 +
3503 +       spin_lock(&ud->lock);
3504 +
3505 +       if (ud->event != 0)
3506 +               happend = 1;
3507 +
3508 +       spin_unlock(&ud->lock);
3509 +
3510 +       return happend;
3511 +}
3512 +EXPORT_SYMBOL_GPL(usbip_event_happend);
3513 --- /dev/null
3514 +++ kernel-2.6.28/drivers/usb/usbip/vhci.h
3515 @@ -0,0 +1,142 @@
3516 +/*
3517 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
3518 + *
3519 + * This is free software; you can redistribute it and/or modify
3520 + * it under the terms of the GNU General Public License as published by
3521 + * the Free Software Foundation; either version 2 of the License, or
3522 + * (at your option) any later version.
3523 + *
3524 + * This is distributed in the hope that it will be useful,
3525 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3526 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3527 + * GNU General Public License for more details.
3528 + *
3529 + * You should have received a copy of the GNU General Public License
3530 + * along with this program; if not, write to the Free Software
3531 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
3532 + * USA.
3533 + */
3534 +
3535 +#include <linux/platform_device.h>
3536 +#include "../../usb/core/hcd.h"
3537 +
3538 +
3539 +struct vhci_device {
3540 +       struct usb_device *udev;
3541 +
3542 +       /*
3543 +        * devid specifies a remote usb device uniquely instead
3544 +        * of combination of busnum and devnum.
3545 +        */
3546 +       __u32 devid;
3547 +
3548 +       /* speed of a remote device */
3549 +       enum usb_device_speed speed;
3550 +
3551 +       /*  vhci root-hub port to which this device is attached  */
3552 +       __u32 rhport;
3553 +
3554 +       struct usbip_device ud;
3555 +
3556 +
3557 +       /* lock for the below link lists */
3558 +       spinlock_t priv_lock;
3559 +
3560 +       /* vhci_priv is linked to one of them. */
3561 +       struct list_head priv_tx;
3562 +       struct list_head priv_rx;
3563 +
3564 +       /* vhci_unlink is linked to one of them */
3565 +       struct list_head unlink_tx;
3566 +       struct list_head unlink_rx;
3567 +
3568 +       /* vhci_tx thread sleeps for this queue */
3569 +       wait_queue_head_t waitq_tx;
3570 +};
3571 +
3572 +
3573 +/* urb->hcpriv, use container_of() */
3574 +struct vhci_priv {
3575 +       unsigned long seqnum;
3576 +       struct list_head list;
3577 +
3578 +       struct vhci_device *vdev;
3579 +       struct urb *urb;
3580 +};
3581 +
3582 +
3583 +struct vhci_unlink {
3584 +       /* seqnum of this request */
3585 +       unsigned long seqnum;
3586 +
3587 +       struct list_head list;
3588 +
3589 +       /* seqnum of the unlink target */
3590 +       unsigned long unlink_seqnum;
3591 +};
3592 +
3593 +/*
3594 + * The number of ports is less than 16 ?
3595 + * USB_MAXCHILDREN is statically defined to 16 in usb.h.  Its maximum value
3596 + * would be 31 because the event_bits[1] of struct usb_hub is defined as
3597 + * unsigned long in hub.h
3598 + */
3599 +#define VHCI_NPORTS 8
3600 +
3601 +/* for usb_bus.hcpriv */
3602 +struct vhci_hcd {
3603 +       spinlock_t      lock;
3604 +
3605 +       u32     port_status[VHCI_NPORTS];
3606 +
3607 +       unsigned        resuming:1;
3608 +       unsigned long   re_timeout;
3609 +
3610 +       atomic_t seqnum;
3611 +
3612 +       /*
3613 +        * NOTE:
3614 +        * wIndex shows the port number and begins from 1.
3615 +        * But, the index of this array begins from 0.
3616 +        */
3617 +       struct vhci_device vdev[VHCI_NPORTS];
3618 +
3619 +       /* vhci_device which has not been assiged its address yet */
3620 +       int pending_port;
3621 +};
3622 +
3623 +
3624 +extern struct vhci_hcd *the_controller;
3625 +extern struct attribute_group dev_attr_group;
3626 +
3627 +
3628 +/*-------------------------------------------------------------------------*/
3629 +/* prototype declaration */
3630 +
3631 +/* vhci_hcd.c */
3632 +void rh_port_connect(int rhport, enum usb_device_speed speed);
3633 +void rh_port_disconnect(int rhport);
3634 +void vhci_rx_loop(struct usbip_task *ut);
3635 +void vhci_tx_loop(struct usbip_task *ut);
3636 +
3637 +#define hardware               (&the_controller->pdev.dev)
3638 +
3639 +static inline struct vhci_device *port_to_vdev(__u32 port)
3640 +{
3641 +       return &the_controller->vdev[port];
3642 +}
3643 +
3644 +static inline struct vhci_hcd *hcd_to_vhci(struct usb_hcd *hcd)
3645 +{
3646 +       return (struct vhci_hcd *) (hcd->hcd_priv);
3647 +}
3648 +
3649 +static inline struct usb_hcd *vhci_to_hcd(struct vhci_hcd *vhci)
3650 +{
3651 +       return container_of((void *) vhci, struct usb_hcd, hcd_priv);
3652 +}
3653 +
3654 +static inline struct device *vhci_dev(struct vhci_hcd *vhci)
3655 +{
3656 +       return vhci_to_hcd(vhci)->self.controller;
3657 +}
3658 --- /dev/null
3659 +++ kernel-2.6.28/drivers/usb/usbip/vhci_hcd.c
3660 @@ -0,0 +1,1275 @@
3661 +/*
3662 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
3663 + *
3664 + * This is free software; you can redistribute it and/or modify
3665 + * it under the terms of the GNU General Public License as published by
3666 + * the Free Software Foundation; either version 2 of the License, or
3667 + * (at your option) any later version.
3668 + *
3669 + * This is distributed in the hope that it will be useful,
3670 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3671 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3672 + * GNU General Public License for more details.
3673 + *
3674 + * You should have received a copy of the GNU General Public License
3675 + * along with this program; if not, write to the Free Software
3676 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
3677 + * USA.
3678 + */
3679 +
3680 +
3681 +#include "usbip_common.h"
3682 +#include "vhci.h"
3683 +
3684 +#define DRIVER_VERSION "1.0"
3685 +#define DRIVER_AUTHOR "Takahiro Hirofuchi"
3686 +#define DRIVER_DESC "Virtual Host Controller Interface Driver for USB/IP"
3687 +#define DRIVER_LICENCE "GPL"
3688 +MODULE_AUTHOR(DRIVER_AUTHOR);
3689 +MODULE_DESCRIPTION(DRIVER_DESC);
3690 +MODULE_LICENSE(DRIVER_LICENCE);
3691 +
3692 +
3693 +
3694 +/*
3695 + * TODO
3696 + *     - update root hub emulation
3697 + *     - move the emulation code to userland ?
3698 + *             porting to other operating systems
3699 + *             minimize kernel code
3700 + *     - add suspend/resume code
3701 + *     - clean up everything
3702 + */
3703 +
3704 +
3705 +/* See usb gadget dummy hcd */
3706 +
3707 +
3708 +static int vhci_hub_status(struct usb_hcd *hcd, char *buff);
3709 +static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
3710 +               u16 wIndex, char *buff, u16 wLength);
3711 +static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
3712 +                                                       gfp_t mem_flags);
3713 +static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
3714 +static int vhci_start(struct usb_hcd *vhci_hcd);
3715 +static void vhci_stop(struct usb_hcd *hcd);
3716 +static int vhci_get_frame_number(struct usb_hcd *hcd);
3717 +
3718 +static const char driver_name[] = "vhci_hcd";
3719 +static const char driver_desc[] = "USB/IP Virtual Host Contoroller";
3720 +
3721 +struct vhci_hcd *the_controller;
3722 +
3723 +static const char *bit_desc[] = {
3724 +       "CONNECTION",           /*0*/
3725 +       "ENABLE",               /*1*/
3726 +       "SUSPEND",              /*2*/
3727 +       "OVER_CURRENT",         /*3*/
3728 +       "RESET",                /*4*/
3729 +       "R5",           /*5*/
3730 +       "R6",           /*6*/
3731 +       "R7",           /*7*/
3732 +       "POWER",                /*8*/
3733 +       "LOWSPEED",             /*9*/
3734 +       "HIGHSPEED",            /*10*/
3735 +       "PORT_TEST",            /*11*/
3736 +       "INDICATOR",            /*12*/
3737 +       "R13",          /*13*/
3738 +       "R14",          /*14*/
3739 +       "R15",          /*15*/
3740 +       "C_CONNECTION",         /*16*/
3741 +       "C_ENABLE",             /*17*/
3742 +       "C_SUSPEND",            /*18*/
3743 +       "C_OVER_CURRENT",       /*19*/
3744 +       "C_RESET",              /*20*/
3745 +       "R21",          /*21*/
3746 +       "R22",          /*22*/
3747 +       "R23",          /*23*/
3748 +       "R24",          /*24*/
3749 +       "R25",          /*25*/
3750 +       "R26",          /*26*/
3751 +       "R27",          /*27*/
3752 +       "R28",          /*28*/
3753 +       "R29",          /*29*/
3754 +       "R30",          /*30*/
3755 +       "R31",          /*31*/
3756 +};
3757 +
3758 +
3759 +static void dump_port_status(u32 status)
3760 +{
3761 +       int i = 0;
3762 +
3763 +       printk(KERN_DEBUG "status %08x:", status);
3764 +       for (i = 0; i < 32; i++) {
3765 +               if (status & (1 << i))
3766 +                       printk(" %s", bit_desc[i]);
3767 +       }
3768 +
3769 +       printk("\n");
3770 +}
3771 +
3772 +
3773 +
3774 +void rh_port_connect(int rhport, enum usb_device_speed speed)
3775 +{
3776 +       unsigned long   flags;
3777 +
3778 +       dbg_vhci_rh("rh_port_connect %d\n", rhport);
3779 +
3780 +       spin_lock_irqsave(&the_controller->lock, flags);
3781 +
3782 +       the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION
3783 +               | (1 << USB_PORT_FEAT_C_CONNECTION);
3784 +
3785 +       switch (speed) {
3786 +       case USB_SPEED_HIGH:
3787 +               the_controller->port_status[rhport] |= USB_PORT_STAT_HIGH_SPEED;
3788 +               break;
3789 +       case USB_SPEED_LOW:
3790 +               the_controller->port_status[rhport] |= USB_PORT_STAT_LOW_SPEED;
3791 +               break;
3792 +       default:
3793 +               break;
3794 +       }
3795 +
3796 +       /* spin_lock(&the_controller->vdev[rhport].ud.lock);
3797 +        * the_controller->vdev[rhport].ud.status = VDEV_CONNECT;
3798 +        * spin_unlock(&the_controller->vdev[rhport].ud.lock); */
3799 +
3800 +       the_controller->pending_port = rhport;
3801 +
3802 +       spin_unlock_irqrestore(&the_controller->lock, flags);
3803 +
3804 +       usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
3805 +}
3806 +
3807 +void rh_port_disconnect(int rhport)
3808 +{
3809 +       unsigned long flags;
3810 +
3811 +       dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
3812 +
3813 +       spin_lock_irqsave(&the_controller->lock, flags);
3814 +       /* stop_activity(dum, driver); */
3815 +       the_controller->port_status[rhport] &= ~USB_PORT_STAT_CONNECTION;
3816 +       the_controller->port_status[rhport] |=
3817 +                                       (1 << USB_PORT_FEAT_C_CONNECTION);
3818 +
3819 +
3820 +       /* not yet complete the disconnection
3821 +        * spin_lock(&vdev->ud.lock);
3822 +        * vdev->ud.status = VHC_ST_DISCONNECT;
3823 +        * spin_unlock(&vdev->ud.lock); */
3824 +
3825 +       spin_unlock_irqrestore(&the_controller->lock, flags);
3826 +}
3827 +
3828 +
3829 +
3830 +/*----------------------------------------------------------------------*/
3831 +
3832 +#define PORT_C_MASK \
3833 +       ((USB_PORT_STAT_C_CONNECTION \
3834 +         | USB_PORT_STAT_C_ENABLE \
3835 +         | USB_PORT_STAT_C_SUSPEND \
3836 +         | USB_PORT_STAT_C_OVERCURRENT \
3837 +         | USB_PORT_STAT_C_RESET) << 16)
3838 +
3839 +/*
3840 + * This function is almostly the same as dummy_hcd.c:dummy_hub_status() without
3841 + * suspend/resume support. But, it is modified to provide multiple ports.
3842 + *
3843 + * @buf: a bitmap to show which port status has been changed.
3844 + *  bit  0: reserved or used for another purpose?
3845 + *  bit  1: the status of port 0 has been changed.
3846 + *  bit  2: the status of port 1 has been changed.
3847 + *  ...
3848 + *  bit  7: the status of port 6 has been changed.
3849 + *  bit  8: the status of port 7 has been changed.
3850 + *  ...
3851 + *  bit 15: the status of port 14 has been changed.
3852 + *
3853 + * So, the maximum number of ports is 31 ( port 0 to port 30) ?
3854 + *
3855 + * The return value is the actual transfered length in byte. If nothing has
3856 + * been changed, return 0. In the case that the number of ports is less than or
3857 + * equal to 6 (VHCI_NPORTS==7), return 1.
3858 + *
3859 + */
3860 +static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
3861 +{
3862 +       struct vhci_hcd *vhci;
3863 +       unsigned long   flags;
3864 +       int             retval = 0;
3865 +
3866 +       /* the enough buffer is allocated according to USB_MAXCHILDREN */
3867 +       unsigned long   *event_bits = (unsigned long *) buf;
3868 +       int             rhport;
3869 +       int             changed = 0;
3870 +
3871 +
3872 +       *event_bits = 0;
3873 +
3874 +       vhci = hcd_to_vhci(hcd);
3875 +
3876 +       spin_lock_irqsave(&vhci->lock, flags);
3877 +       if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
3878 +               dbg_vhci_rh("hw accessible flag in on?\n");
3879 +               goto done;
3880 +       }
3881 +
3882 +       /* check pseudo status register for each port */
3883 +       for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
3884 +               if ((vhci->port_status[rhport] & PORT_C_MASK)) {
3885 +                       /* The status of a port has been changed, */
3886 +                       dbg_vhci_rh("port %d is changed\n", rhport);
3887 +
3888 +                       *event_bits |= 1 << (rhport + 1);
3889 +                       changed = 1;
3890 +               }
3891 +       }
3892 +
3893 +       uinfo("changed %d\n", changed);
3894 +
3895 +       if (hcd->state == HC_STATE_SUSPENDED)
3896 +               usb_hcd_resume_root_hub(hcd);
3897 +
3898 +       if (changed)
3899 +               retval = 1 + (VHCI_NPORTS / 8);
3900 +       else
3901 +               retval = 0;
3902 +
3903 +done:
3904 +       spin_unlock_irqrestore(&vhci->lock, flags);
3905 +       return retval;
3906 +}
3907 +
3908 +/* See hub_configure in hub.c */
3909 +static inline void hub_descriptor(struct usb_hub_descriptor *desc)
3910 +{
3911 +       memset(desc, 0, sizeof(*desc));
3912 +       desc->bDescriptorType = 0x29;
3913 +       desc->bDescLength = 9;
3914 +       desc->wHubCharacteristics = (__force __u16)
3915 +               (__constant_cpu_to_le16(0x0001));
3916 +       desc->bNbrPorts = VHCI_NPORTS;
3917 +       desc->bitmap[0] = 0xff;
3918 +       desc->bitmap[1] = 0xff;
3919 +}
3920 +
3921 +static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
3922 +                           u16 wIndex, char *buf, u16 wLength)
3923 +{
3924 +       struct vhci_hcd *dum;
3925 +       int             retval = 0;
3926 +       unsigned long   flags;
3927 +       int             rhport;
3928 +
3929 +       u32 prev_port_status[VHCI_NPORTS];
3930 +
3931 +       if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
3932 +               return -ETIMEDOUT;
3933 +
3934 +       /*
3935 +        * NOTE:
3936 +        * wIndex shows the port number and begins from 1.
3937 +        */
3938 +       dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
3939 +                                                               wIndex);
3940 +       if (wIndex > VHCI_NPORTS)
3941 +               printk(KERN_ERR "%s: invalid port number %d\n", __func__, wIndex);
3942 +       rhport = ((__u8)(wIndex & 0x00ff)) - 1;
3943 +
3944 +       dum = hcd_to_vhci(hcd);
3945 +
3946 +       spin_lock_irqsave(&dum->lock, flags);
3947 +
3948 +       /* store old status and compare now and old later */
3949 +       if (dbg_flag_vhci_rh) {
3950 +               int i = 0;
3951 +               for (i = 0; i < VHCI_NPORTS; i++)
3952 +                       prev_port_status[i] = dum->port_status[i];
3953 +       }
3954 +
3955 +       switch (typeReq) {
3956 +       case ClearHubFeature:
3957 +               dbg_vhci_rh(" ClearHubFeature\n");
3958 +               break;
3959 +       case ClearPortFeature:
3960 +               switch (wValue) {
3961 +               case USB_PORT_FEAT_SUSPEND:
3962 +                       if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
3963 +                               /* 20msec signaling */
3964 +                               dum->resuming = 1;
3965 +                               dum->re_timeout =
3966 +                                       jiffies + msecs_to_jiffies(20);
3967 +                       }
3968 +                       break;
3969 +               case USB_PORT_FEAT_POWER:
3970 +                       dbg_vhci_rh(" ClearPortFeature: USB_PORT_FEAT_POWER\n");
3971 +                       dum->port_status[rhport] = 0;
3972 +                       /* dum->address = 0; */
3973 +                       /* dum->hdev = 0; */
3974 +                       dum->resuming = 0;
3975 +                       break;
3976 +               case USB_PORT_FEAT_C_RESET:
3977 +                       dbg_vhci_rh(" ClearPortFeature: "
3978 +                                       "USB_PORT_FEAT_C_RESET\n");
3979 +                       switch (dum->vdev[rhport].speed) {
3980 +                       case USB_SPEED_HIGH:
3981 +                               dum->port_status[rhport] |=
3982 +                                               USB_PORT_STAT_HIGH_SPEED;
3983 +                               break;
3984 +                       case USB_SPEED_LOW:
3985 +                               dum->port_status[rhport] |=
3986 +                                               USB_PORT_STAT_LOW_SPEED;
3987 +                               break;
3988 +                       default:
3989 +                               break;
3990 +                       }
3991 +               default:
3992 +                       dbg_vhci_rh(" ClearPortFeature: default %x\n", wValue);
3993 +                       dum->port_status[rhport] &= ~(1 << wValue);
3994 +               }
3995 +               break;
3996 +       case GetHubDescriptor:
3997 +               dbg_vhci_rh(" GetHubDescriptor\n");
3998 +               hub_descriptor((struct usb_hub_descriptor *) buf);
3999 +               break;
4000 +       case GetHubStatus:
4001 +               dbg_vhci_rh(" GetHubStatus\n");
4002 +               *(__le32 *) buf = __constant_cpu_to_le32(0);
4003 +               break;
4004 +       case GetPortStatus:
4005 +               dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
4006 +               if (wIndex > VHCI_NPORTS || wIndex < 1) {
4007 +                       printk(KERN_ERR "%s: invalid port number %d\n",
4008 +                              __func__, wIndex);
4009 +                       retval = -EPIPE;
4010 +               }
4011 +
4012 +               /* we do no care of resume. */
4013 +
4014 +               /* whoever resets or resumes must GetPortStatus to
4015 +                * complete it!!
4016 +                *                                   */
4017 +               if (dum->resuming && time_after(jiffies, dum->re_timeout)) {
4018 +                       printk(KERN_ERR "%s: not yet\n", __func__);
4019 +                       dum->port_status[rhport] |=
4020 +                                       (1 << USB_PORT_FEAT_C_SUSPEND);
4021 +                       dum->port_status[rhport] &=
4022 +                                       ~(1 << USB_PORT_FEAT_SUSPEND);
4023 +                       dum->resuming = 0;
4024 +                       dum->re_timeout = 0;
4025 +                       /* if (dum->driver && dum->driver->resume) {
4026 +                        *      spin_unlock (&dum->lock);
4027 +                        *      dum->driver->resume (&dum->gadget);
4028 +                        *      spin_lock (&dum->lock);
4029 +                        * } */
4030 +               }
4031 +
4032 +               if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
4033 +                               0 && time_after(jiffies, dum->re_timeout)) {
4034 +                       dum->port_status[rhport] |=
4035 +                                               (1 << USB_PORT_FEAT_C_RESET);
4036 +                       dum->port_status[rhport] &=
4037 +                                               ~(1 << USB_PORT_FEAT_RESET);
4038 +                       dum->re_timeout = 0;
4039 +
4040 +                       if (dum->vdev[rhport].ud.status ==
4041 +                                                       VDEV_ST_NOTASSIGNED) {
4042 +                               dbg_vhci_rh(" enable rhport %d (status %u)\n",
4043 +                                               rhport,
4044 +                                               dum->vdev[rhport].ud.status);
4045 +                               dum->port_status[rhport] |=
4046 +                                                       USB_PORT_STAT_ENABLE;
4047 +                       }
4048 +#if 0
4049 +                       if (dum->driver) {
4050 +
4051 +                               dum->port_status[rhport] |=
4052 +                                                       USB_PORT_STAT_ENABLE;
4053 +                               /* give it the best speed we agree on */
4054 +                               dum->gadget.speed = dum->driver->speed;
4055 +                               dum->gadget.ep0->maxpacket = 64;
4056 +                               switch (dum->gadget.speed) {
4057 +                               case USB_SPEED_HIGH:
4058 +                                       dum->port_status[rhport] |=
4059 +                                       USB_PORT_STAT_HIGH_SPEED;
4060 +                                       break;
4061 +                               case USB_SPEED_LOW:
4062 +                                       dum->gadget.ep0->maxpacket = 8;
4063 +                                       dum->port_status[rhport] |=
4064 +                                       USB_PORT_STAT_LOW_SPEED;
4065 +                                       break;
4066 +                               default:
4067 +                                       dum->gadget.speed = USB_SPEED_FULL;
4068 +                                       break;
4069 +                               }
4070 +                       }
4071 +#endif
4072 +
4073 +               }
4074 +               ((u16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]);
4075 +               ((u16 *) buf)[1] =
4076 +                               cpu_to_le16(dum->port_status[rhport] >> 16);
4077 +
4078 +               dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
4079 +                                                       ((u16 *)buf)[1]);
4080 +               break;
4081 +       case SetHubFeature:
4082 +               dbg_vhci_rh(" SetHubFeature\n");
4083 +               retval = -EPIPE;
4084 +               break;
4085 +       case SetPortFeature:
4086 +               switch (wValue) {
4087 +               case USB_PORT_FEAT_SUSPEND:
4088 +                       dbg_vhci_rh(" SetPortFeature: "
4089 +                                       "USB_PORT_FEAT_SUSPEND\n");
4090 +                       printk(KERN_ERR "%s: not yet\n", __func__);
4091 +#if 0
4092 +                       dum->port_status[rhport] |=
4093 +                                               (1 << USB_PORT_FEAT_SUSPEND);
4094 +                       if (dum->driver->suspend) {
4095 +                               spin_unlock(&dum->lock);
4096 +                               dum->driver->suspend(&dum->gadget);
4097 +                               spin_lock(&dum->lock);
4098 +                       }
4099 +#endif
4100 +                       break;
4101 +               case USB_PORT_FEAT_RESET:
4102 +                       dbg_vhci_rh(" SetPortFeature: USB_PORT_FEAT_RESET\n");
4103 +                       /* if it's already running, disconnect first */
4104 +                       if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) {
4105 +                               dum->port_status[rhport] &=
4106 +                                               ~(USB_PORT_STAT_ENABLE |
4107 +                                                 USB_PORT_STAT_LOW_SPEED |
4108 +                                                 USB_PORT_STAT_HIGH_SPEED);
4109 +#if 0
4110 +                               if (dum->driver) {
4111 +                                       dev_dbg(hardware, "disconnect\n");
4112 +                                       stop_activity(dum, dum->driver);
4113 +                               }
4114 +#endif
4115 +
4116 +                               /* FIXME test that code path! */
4117 +                       }
4118 +                       /* 50msec reset signaling */
4119 +                       dum->re_timeout = jiffies + msecs_to_jiffies(50);
4120 +
4121 +                       /* FALLTHROUGH */
4122 +               default:
4123 +                       dbg_vhci_rh(" SetPortFeature: default %d\n", wValue);
4124 +                       dum->port_status[rhport] |= (1 << wValue);
4125 +               }
4126 +               break;
4127 +
4128 +       default:
4129 +               printk(KERN_ERR "%s: default: no such request\n", __func__);
4130 +               /* dev_dbg (hardware,
4131 +                *              "hub control req%04x v%04x i%04x l%d\n",
4132 +                *              typeReq, wValue, wIndex, wLength); */
4133 +
4134 +               /* "protocol stall" on error */
4135 +               retval = -EPIPE;
4136 +       }
4137 +
4138 +       if (dbg_flag_vhci_rh) {
4139 +               printk(KERN_DEBUG "port %d\n", rhport);
4140 +               dump_port_status(prev_port_status[rhport]);
4141 +               dump_port_status(dum->port_status[rhport]);
4142 +       }
4143 +       dbg_vhci_rh(" bye\n");
4144 +
4145 +       spin_unlock_irqrestore(&dum->lock, flags);
4146 +
4147 +       return retval;
4148 +}
4149 +
4150 +
4151 +
4152 +/*----------------------------------------------------------------------*/
4153 +
4154 +static struct vhci_device *get_vdev(struct usb_device *udev)
4155 +{
4156 +       int i;
4157 +
4158 +       if (!udev)
4159 +               return NULL;
4160 +
4161 +       for (i = 0; i < VHCI_NPORTS; i++)
4162 +               if (the_controller->vdev[i].udev == udev)
4163 +                       return port_to_vdev(i);
4164 +
4165 +       return NULL;
4166 +}
4167 +
4168 +static void vhci_tx_urb(struct urb *urb)
4169 +{
4170 +       struct vhci_device *vdev = get_vdev(urb->dev);
4171 +       struct vhci_priv *priv;
4172 +       unsigned long flag;
4173 +
4174 +       if (!vdev) {
4175 +               err("could not get virtual device");
4176 +               /* BUG(); */
4177 +               return;
4178 +       }
4179 +
4180 +       spin_lock_irqsave(&vdev->priv_lock, flag);
4181 +
4182 +       priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
4183 +       if (!priv) {
4184 +               dev_err(&urb->dev->dev, "malloc vhci_priv\n");
4185 +               spin_unlock_irqrestore(&vdev->priv_lock, flag);
4186 +               usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
4187 +               return;
4188 +       }
4189 +
4190 +       priv->seqnum = atomic_inc_return(&the_controller->seqnum);
4191 +       if (priv->seqnum == 0xffff)
4192 +               uinfo("seqnum max\n");
4193 +
4194 +       priv->vdev = vdev;
4195 +       priv->urb = urb;
4196 +
4197 +       urb->hcpriv = (void *) priv;
4198 +
4199 +
4200 +       list_add_tail(&priv->list, &vdev->priv_tx);
4201 +
4202 +       wake_up(&vdev->waitq_tx);
4203 +       spin_unlock_irqrestore(&vdev->priv_lock, flag);
4204 +}
4205 +
4206 +static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
4207 +                           gfp_t mem_flags)
4208 +{
4209 +       struct device *dev = &urb->dev->dev;
4210 +       int ret = 0;
4211 +       unsigned long flags;
4212 +
4213 +       dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
4214 +                   hcd, urb, mem_flags);
4215 +
4216 +       /* patch to usb_sg_init() is in 2.5.60 */
4217 +       BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
4218 +
4219 +       spin_lock_irqsave(&the_controller->lock, flags);
4220 +
4221 +       /* check HC is active or not */
4222 +       if (!HC_IS_RUNNING(hcd->state)) {
4223 +               dev_err(dev, "HC is not running\n");
4224 +               spin_unlock_irqrestore(&the_controller->lock, flags);
4225 +               return -ENODEV;
4226 +       }
4227 +
4228 +       if (urb->status != -EINPROGRESS) {
4229 +               dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
4230 +               spin_unlock_irqrestore(&the_controller->lock, flags);
4231 +               return urb->status;
4232 +       }
4233 +
4234 +       ret = usb_hcd_link_urb_to_ep(hcd, urb);
4235 +       if (ret)
4236 +               goto no_need_unlink;
4237 +
4238 +       /*
4239 +        * The enumelation process is as follows;
4240 +        *
4241 +        *  1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
4242 +        *     to get max packet length of default pipe
4243 +        *
4244 +        *  2. Set_Address request to DevAddr(0) EndPoint(0)
4245 +        *
4246 +        */
4247 +
4248 +       if (usb_pipedevice(urb->pipe) == 0) {
4249 +               __u8 type = usb_pipetype(urb->pipe);
4250 +               struct usb_ctrlrequest *ctrlreq =
4251 +                               (struct usb_ctrlrequest *) urb->setup_packet;
4252 +               struct vhci_device *vdev =
4253 +                               port_to_vdev(the_controller->pending_port);
4254 +
4255 +               if (type != PIPE_CONTROL || !ctrlreq) {
4256 +                       dev_err(dev, "invalid request to devnum 0\n");
4257 +                       ret = EINVAL;
4258 +                       goto no_need_xmit;
4259 +               }
4260 +
4261 +               switch (ctrlreq->bRequest) {
4262 +               case USB_REQ_SET_ADDRESS:
4263 +                       /* set_address may come when a device is reset */
4264 +                       dev_info(dev, "SetAddress Request (%d) to port %d\n",
4265 +                                ctrlreq->wValue, vdev->rhport);
4266 +
4267 +                       vdev->udev = urb->dev;
4268 +
4269 +                       spin_lock(&vdev->ud.lock);
4270 +                       vdev->ud.status = VDEV_ST_USED;
4271 +                       spin_unlock(&vdev->ud.lock);
4272 +
4273 +                       if (urb->status == -EINPROGRESS) {
4274 +                               /* This request is successfully completed. */
4275 +                               /* If not -EINPROGRESS, possibly unlinked. */
4276 +                               urb->status = 0;
4277 +                       }
4278 +
4279 +                       goto no_need_xmit;
4280 +
4281 +               case USB_REQ_GET_DESCRIPTOR:
4282 +                       if (ctrlreq->wValue == (USB_DT_DEVICE << 8))
4283 +                               dbg_vhci_hc("Not yet?: "
4284 +                                               "Get_Descriptor to device 0 "
4285 +                                               "(get max pipe size)\n");
4286 +
4287 +                       /* FIXME: reference count? (usb_get_dev()) */
4288 +                       vdev->udev = urb->dev;
4289 +                       goto out;
4290 +
4291 +               default:
4292 +                       /* NOT REACHED */
4293 +                       dev_err(dev, "invalid request to devnum 0 bRequest %u, "
4294 +                               "wValue %u\n", ctrlreq->bRequest,
4295 +                               ctrlreq->wValue);
4296 +                       ret =  -EINVAL;
4297 +                       goto no_need_xmit;
4298 +               }
4299 +
4300 +       }
4301 +
4302 +out:
4303 +       vhci_tx_urb(urb);
4304 +
4305 +       spin_unlock_irqrestore(&the_controller->lock, flags);
4306 +
4307 +       return 0;
4308 +
4309 +no_need_xmit:
4310 +       usb_hcd_unlink_urb_from_ep(hcd, urb);
4311 +no_need_unlink:
4312 +       spin_unlock_irqrestore(&the_controller->lock, flags);
4313 +
4314 +       usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
4315 +
4316 +       return 0;
4317 +}
4318 +
4319 +/*
4320 + * vhci_rx gives back the urb after receiving the reply of the urb.  If an
4321 + * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
4322 + * back its urb. For the driver unlinking the urb, the content of the urb is
4323 + * not important, but the calling to its completion handler is important; the
4324 + * completion of unlinking is notified by the completion handler.
4325 + *
4326 + *
4327 + * CLIENT SIDE
4328 + *
4329 + * - When vhci_hcd receives RET_SUBMIT,
4330 + *
4331 + *     - case 1a). the urb of the pdu is not unlinking.
4332 + *             - normal case
4333 + *             => just give back the urb
4334 + *
4335 + *     - case 1b). the urb of the pdu is unlinking.
4336 + *             - usbip.ko will return a reply of the unlinking request.
4337 + *             => give back the urb now and go to case 2b).
4338 + *
4339 + * - When vhci_hcd receives RET_UNLINK,
4340 + *
4341 + *     - case 2a). a submit request is still pending in vhci_hcd.
4342 + *             - urb was really pending in usbip.ko and urb_unlink_urb() was
4343 + *               completed there.
4344 + *             => free a pending submit request
4345 + *             => notify unlink completeness by giving back the urb
4346 + *
4347 + *     - case 2b). a submit request is *not* pending in vhci_hcd.
4348 + *             - urb was already given back to the core driver.
4349 + *             => do not give back the urb
4350 + *
4351 + *
4352 + * SERVER SIDE
4353 + *
4354 + * - When usbip receives CMD_UNLINK,
4355 + *
4356 + *     - case 3a). the urb of the unlink request is now in submission.
4357 + *             => do usb_unlink_urb().
4358 + *             => after the unlink is completed, send RET_UNLINK.
4359 + *
4360 + *     - case 3b). the urb of the unlink request is not in submission.
4361 + *             - may be already completed or never be received
4362 + *             => send RET_UNLINK
4363 + *
4364 + */
4365 +static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
4366 +{
4367 +       unsigned long flags;
4368 +       struct vhci_priv *priv;
4369 +       struct vhci_device *vdev;
4370 +
4371 +       uinfo("vhci_hcd: dequeue a urb %p\n", urb);
4372 +
4373 +
4374 +       spin_lock_irqsave(&the_controller->lock, flags);
4375 +
4376 +       priv = urb->hcpriv;
4377 +       if (!priv) {
4378 +               /* URB was never linked! or will be soon given back by
4379 +                * vhci_rx. */
4380 +               spin_unlock_irqrestore(&the_controller->lock, flags);
4381 +               return 0;
4382 +       }
4383 +
4384 +       {
4385 +               int ret = 0;
4386 +               ret = usb_hcd_check_unlink_urb(hcd, urb, status);
4387 +               if (ret) {
4388 +                       spin_unlock_irqrestore(&the_controller->lock, flags);
4389 +                       return 0;
4390 +               }
4391 +       }
4392 +
4393 +        /* send unlink request here? */
4394 +       vdev = priv->vdev;
4395 +
4396 +       if (!vdev->ud.tcp_socket) {
4397 +               /* tcp connection is closed */
4398 +               unsigned long flags2;
4399 +
4400 +               spin_lock_irqsave(&vdev->priv_lock, flags2);
4401 +
4402 +               uinfo("vhci_hcd: device %p seems to be disconnected\n", vdev);
4403 +               list_del(&priv->list);
4404 +               kfree(priv);
4405 +               urb->hcpriv = NULL;
4406 +
4407 +               spin_unlock_irqrestore(&vdev->priv_lock, flags2);
4408 +
4409 +       } else {
4410 +               /* tcp connection is alive */
4411 +               unsigned long flags2;
4412 +               struct vhci_unlink *unlink;
4413 +
4414 +               spin_lock_irqsave(&vdev->priv_lock, flags2);
4415 +
4416 +               /* setup CMD_UNLINK pdu */
4417 +               unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
4418 +               if (!unlink) {
4419 +                       uerr("malloc vhci_unlink\n");
4420 +                       spin_unlock_irqrestore(&vdev->priv_lock, flags2);
4421 +                       spin_unlock_irqrestore(&the_controller->lock, flags);
4422 +                       usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
4423 +                       return -ENOMEM;
4424 +               }
4425 +
4426 +               unlink->seqnum = atomic_inc_return(&the_controller->seqnum);
4427 +               if (unlink->seqnum == 0xffff)
4428 +                       uinfo("seqnum max\n");
4429 +
4430 +               unlink->unlink_seqnum = priv->seqnum;
4431 +
4432 +               uinfo("vhci_hcd: device %p seems to be still connected\n",
4433 +                                                                       vdev);
4434 +
4435 +               /* send cmd_unlink and try to cancel the pending URB in the
4436 +                * peer */
4437 +               list_add_tail(&unlink->list, &vdev->unlink_tx);
4438 +               wake_up(&vdev->waitq_tx);
4439 +
4440 +               spin_unlock_irqrestore(&vdev->priv_lock, flags2);
4441 +       }
4442 +
4443 +
4444 +       /*
4445 +        * If tcp connection is alive, we have sent CMD_UNLINK.
4446 +        * vhci_rx will receive RET_UNLINK and give back the URB.
4447 +        * Otherwise, we give back it here.
4448 +        */
4449 +       if (!vdev->ud.tcp_socket) {
4450 +               /* tcp connection is closed */
4451 +               uinfo("vhci_hcd: vhci_urb_dequeue() gives back urb %p\n", urb);
4452 +
4453 +               usb_hcd_unlink_urb_from_ep(hcd, urb);
4454 +
4455 +               spin_unlock_irqrestore(&the_controller->lock, flags);
4456 +               usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
4457 +                                                               urb->status);
4458 +               spin_lock_irqsave(&the_controller->lock, flags);
4459 +       }
4460 +
4461 +       spin_unlock_irqrestore(&the_controller->lock, flags);
4462 +
4463 +       dbg_vhci_hc("leave\n");
4464 +       return 0;
4465 +}
4466 +
4467 +
4468 +static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
4469 +{
4470 +       struct vhci_unlink *unlink, *tmp;
4471 +
4472 +       spin_lock(&vdev->priv_lock);
4473 +
4474 +       list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
4475 +               list_del(&unlink->list);
4476 +               kfree(unlink);
4477 +       }
4478 +
4479 +       list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
4480 +               list_del(&unlink->list);
4481 +               kfree(unlink);
4482 +       }
4483 +
4484 +       spin_unlock(&vdev->priv_lock);
4485 +}
4486 +
4487 +/*
4488 + * The important thing is that only one context begins cleanup.
4489 + * This is why error handling and cleanup become simple.
4490 + * We do not want to consider race condition as possible.
4491 + */
4492 +static void vhci_shutdown_connection(struct usbip_device *ud)
4493 +{
4494 +       struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
4495 +
4496 +       /* need this? see stub_dev.c */
4497 +       if (ud->tcp_socket) {
4498 +               udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
4499 +               kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
4500 +       }
4501 +
4502 +       usbip_stop_threads(&vdev->ud);
4503 +       uinfo("stop threads\n");
4504 +
4505 +       /* active connection is closed */
4506 +       if (vdev->ud.tcp_socket != NULL) {
4507 +               sock_release(vdev->ud.tcp_socket);
4508 +               vdev->ud.tcp_socket = NULL;
4509 +       }
4510 +       uinfo("release socket\n");
4511 +
4512 +       vhci_device_unlink_cleanup(vdev);
4513 +
4514 +       /*
4515 +        * rh_port_disconnect() is a trigger of ...
4516 +        *   usb_disable_device():
4517 +        *      disable all the endpoints for a USB device.
4518 +        *   usb_disable_endpoint():
4519 +        *      disable endpoints. pending urbs are unlinked(dequeued).
4520 +        *
4521 +        * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
4522 +        * deteched device should release used urbs in a cleanup function(i.e.
4523 +        * xxx_disconnect()). Therefore, vhci_hcd does not need to release
4524 +        * pushed urbs and their private data in this function.
4525 +        *
4526 +        * NOTE: vhci_dequeue() must be considered carefully. When shutdowning
4527 +        * a connection, vhci_shutdown_connection() expects vhci_dequeue()
4528 +        * gives back pushed urbs and frees their private data by request of
4529 +        * the cleanup function of a USB driver. When unlinking a urb with an
4530 +        * active connection, vhci_dequeue() does not give back the urb which
4531 +        * is actually given back by vhci_rx after receiving its return pdu.
4532 +        *
4533 +        */
4534 +       rh_port_disconnect(vdev->rhport);
4535 +
4536 +       uinfo("disconnect device\n");
4537 +}
4538 +
4539 +
4540 +static void vhci_device_reset(struct usbip_device *ud)
4541 +{
4542 +       struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
4543 +
4544 +       spin_lock(&ud->lock);
4545 +
4546 +       vdev->speed  = 0;
4547 +       vdev->devid  = 0;
4548 +
4549 +       ud->tcp_socket = NULL;
4550 +
4551 +       ud->status = VDEV_ST_NULL;
4552 +
4553 +       spin_unlock(&ud->lock);
4554 +}
4555 +
4556 +static void vhci_device_unusable(struct usbip_device *ud)
4557 +{
4558 +       spin_lock(&ud->lock);
4559 +
4560 +       ud->status = VDEV_ST_ERROR;
4561 +
4562 +       spin_unlock(&ud->lock);
4563 +}
4564 +
4565 +static void vhci_device_init(struct vhci_device *vdev)
4566 +{
4567 +       memset(vdev, 0, sizeof(*vdev));
4568 +
4569 +       usbip_task_init(&vdev->ud.tcp_rx, "vhci_rx", vhci_rx_loop);
4570 +       usbip_task_init(&vdev->ud.tcp_tx, "vhci_tx", vhci_tx_loop);
4571 +
4572 +       vdev->ud.side   = USBIP_VHCI;
4573 +       vdev->ud.status = VDEV_ST_NULL;
4574 +       /* vdev->ud.lock   = SPIN_LOCK_UNLOCKED; */
4575 +       spin_lock_init(&vdev->ud.lock);
4576 +
4577 +       INIT_LIST_HEAD(&vdev->priv_rx);
4578 +       INIT_LIST_HEAD(&vdev->priv_tx);
4579 +       INIT_LIST_HEAD(&vdev->unlink_tx);
4580 +       INIT_LIST_HEAD(&vdev->unlink_rx);
4581 +       /* vdev->priv_lock = SPIN_LOCK_UNLOCKED; */
4582 +       spin_lock_init(&vdev->priv_lock);
4583 +
4584 +       init_waitqueue_head(&vdev->waitq_tx);
4585 +
4586 +       vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
4587 +       vdev->ud.eh_ops.reset = vhci_device_reset;
4588 +       vdev->ud.eh_ops.unusable = vhci_device_unusable;
4589 +
4590 +       usbip_start_eh(&vdev->ud);
4591 +}
4592 +
4593 +
4594 +/*----------------------------------------------------------------------*/
4595 +
4596 +static int vhci_start(struct usb_hcd *hcd)
4597 +{
4598 +       struct vhci_hcd *vhci = hcd_to_vhci(hcd);
4599 +       int rhport;
4600 +       int err = 0;
4601 +
4602 +       dbg_vhci_hc("enter vhci_start\n");
4603 +
4604 +
4605 +       /* initialize private data of usb_hcd */
4606 +
4607 +       for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
4608 +               struct vhci_device *vdev = &vhci->vdev[rhport];
4609 +               vhci_device_init(vdev);
4610 +               vdev->rhport = rhport;
4611 +       }
4612 +
4613 +       atomic_set(&vhci->seqnum, 0);
4614 +       spin_lock_init(&vhci->lock);
4615 +
4616 +
4617 +
4618 +       hcd->power_budget = 0; /* no limit */
4619 +       hcd->state  = HC_STATE_RUNNING;
4620 +       hcd->uses_new_polling = 1;
4621 +
4622 +
4623 +       /* vhci_hcd is now ready to be controlled through sysfs */
4624 +       err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
4625 +       if (err) {
4626 +               uerr("create sysfs files\n");
4627 +               return err;
4628 +       }
4629 +
4630 +       return 0;
4631 +}
4632 +
4633 +static void vhci_stop(struct usb_hcd *hcd)
4634 +{
4635 +       struct vhci_hcd *vhci = hcd_to_vhci(hcd);
4636 +       int rhport = 0;
4637 +
4638 +       dbg_vhci_hc("stop VHCI controller\n");
4639 +
4640 +
4641 +       /* 1. remove the userland interface of vhci_hcd */
4642 +       sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
4643 +
4644 +       /* 2. shutdown all the ports of vhci_hcd */
4645 +       for (rhport = 0 ; rhport < VHCI_NPORTS; rhport++) {
4646 +               struct vhci_device *vdev = &vhci->vdev[rhport];
4647 +
4648 +               usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
4649 +               usbip_stop_eh(&vdev->ud);
4650 +       }
4651 +
4652 +
4653 +       uinfo("vhci_stop done\n");
4654 +}
4655 +
4656 +/*----------------------------------------------------------------------*/
4657 +
4658 +static int vhci_get_frame_number(struct usb_hcd *hcd)
4659 +{
4660 +       uerr("Not yet implemented\n");
4661 +       return 0;
4662 +}
4663 +
4664 +
4665 +#ifdef CONFIG_PM
4666 +
4667 +/* FIXME: suspend/resume */
4668 +static int vhci_bus_suspend(struct usb_hcd *hcd)
4669 +{
4670 +       struct vhci_hcd *vhci = hcd_to_vhci(hcd);
4671 +
4672 +       dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
4673 +
4674 +       spin_lock_irq(&vhci->lock);
4675 +       /* vhci->rh_state = DUMMY_RH_SUSPENDED;
4676 +        * set_link_state(vhci); */
4677 +       hcd->state = HC_STATE_SUSPENDED;
4678 +       spin_unlock_irq(&vhci->lock);
4679 +
4680 +       return 0;
4681 +}
4682 +
4683 +static int vhci_bus_resume(struct usb_hcd *hcd)
4684 +{
4685 +       struct vhci_hcd *vhci = hcd_to_vhci(hcd);
4686 +       int rc = 0;
4687 +
4688 +       dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
4689 +
4690 +       spin_lock_irq(&vhci->lock);
4691 +       if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
4692 +               rc = -ESHUTDOWN;
4693 +       } else {
4694 +               /* vhci->rh_state = DUMMY_RH_RUNNING;
4695 +                * set_link_state(vhci);
4696 +                * if (!list_empty(&vhci->urbp_list))
4697 +                *      mod_timer(&vhci->timer, jiffies); */
4698 +               hcd->state = HC_STATE_RUNNING;
4699 +       }
4700 +       spin_unlock_irq(&vhci->lock);
4701 +       return rc;
4702 +
4703 +       return 0;
4704 +}
4705 +
4706 +#else
4707 +
4708 +#define vhci_bus_suspend      NULL
4709 +#define vhci_bus_resume       NULL
4710 +#endif
4711 +
4712 +
4713 +
4714 +static struct hc_driver vhci_hc_driver = {
4715 +       .description    = driver_name,
4716 +       .product_desc   = driver_desc,
4717 +       .hcd_priv_size  = sizeof(struct vhci_hcd),
4718 +
4719 +       .flags          = HCD_USB2,
4720 +
4721 +       .start          = vhci_start,
4722 +       .stop           = vhci_stop,
4723 +
4724 +       .urb_enqueue    = vhci_urb_enqueue,
4725 +       .urb_dequeue    = vhci_urb_dequeue,
4726 +
4727 +       .get_frame_number = vhci_get_frame_number,
4728 +
4729 +       .hub_status_data = vhci_hub_status,
4730 +       .hub_control    = vhci_hub_control,
4731 +       .bus_suspend    = vhci_bus_suspend,
4732 +       .bus_resume     = vhci_bus_resume,
4733 +};
4734 +
4735 +static int vhci_hcd_probe(struct platform_device *pdev)
4736 +{
4737 +       struct usb_hcd          *hcd;
4738 +       int                     ret;
4739 +
4740 +       uinfo("proving...\n");
4741 +
4742 +       dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
4743 +
4744 +       /* will be removed */
4745 +       if (pdev->dev.dma_mask) {
4746 +               dev_info(&pdev->dev, "vhci_hcd DMA not supported\n");
4747 +               return -EINVAL;
4748 +       }
4749 +
4750 +       /*
4751 +        * Allocate and initialize hcd.
4752 +        * Our private data is also allocated automatically.
4753 +        */
4754 +       hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, pdev->dev.bus_id);
4755 +       if (!hcd) {
4756 +               uerr("create hcd failed\n");
4757 +               return -ENOMEM;
4758 +       }
4759 +
4760 +
4761 +       /* this is private data for vhci_hcd */
4762 +       the_controller = hcd_to_vhci(hcd);
4763 +
4764 +       /*
4765 +        * Finish generic HCD structure initialization and register.
4766 +        * Call the driver's reset() and start() routines.
4767 +        */
4768 +       ret = usb_add_hcd(hcd, 0, 0);
4769 +       if (ret != 0) {
4770 +               uerr("usb_add_hcd failed %d\n", ret);
4771 +               usb_put_hcd(hcd);
4772 +               the_controller = NULL;
4773 +               return ret;
4774 +       }
4775 +
4776 +
4777 +       dbg_vhci_hc("bye\n");
4778 +       return 0;
4779 +}
4780 +
4781 +
4782 +static int vhci_hcd_remove(struct platform_device *pdev)
4783 +{
4784 +       struct usb_hcd  *hcd;
4785 +
4786 +       hcd = platform_get_drvdata(pdev);
4787 +       if (!hcd)
4788 +               return 0;
4789 +
4790 +       /*
4791 +        * Disconnects the root hub,
4792 +        * then reverses the effects of usb_add_hcd(),
4793 +        * invoking the HCD's stop() methods.
4794 +        */
4795 +       usb_remove_hcd(hcd);
4796 +       usb_put_hcd(hcd);
4797 +       the_controller = NULL;
4798 +
4799 +
4800 +       return 0;
4801 +}
4802 +
4803 +
4804 +
4805 +#ifdef CONFIG_PM
4806 +
4807 +/* what should happen for USB/IP under suspend/resume? */
4808 +static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
4809 +{
4810 +       struct usb_hcd *hcd;
4811 +       int rhport = 0;
4812 +       int connected = 0;
4813 +       int ret = 0;
4814 +
4815 +       dev_dbg(&pdev->dev, "%s\n", __func__);
4816 +
4817 +       hcd = platform_get_drvdata(pdev);
4818 +
4819 +       spin_lock(&the_controller->lock);
4820 +
4821 +       for (rhport = 0; rhport < VHCI_NPORTS; rhport++)
4822 +               if (the_controller->port_status[rhport] &
4823 +                                               USB_PORT_STAT_CONNECTION)
4824 +                       connected += 1;
4825 +
4826 +       spin_unlock(&the_controller->lock);
4827 +
4828 +       if (connected > 0) {
4829 +               uinfo("We have %d active connection%s. Do not suspend.\n",
4830 +                               connected, (connected == 1 ? "" : "s"));
4831 +               ret =  -EBUSY;
4832 +       } else {
4833 +               uinfo("suspend vhci_hcd");
4834 +               clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4835 +       }
4836 +
4837 +       return ret;
4838 +}
4839 +
4840 +static int vhci_hcd_resume(struct platform_device *pdev)
4841 +{
4842 +       struct usb_hcd *hcd;
4843 +
4844 +       dev_dbg(&pdev->dev, "%s\n", __func__);
4845 +
4846 +       hcd = platform_get_drvdata(pdev);
4847 +       set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4848 +       usb_hcd_poll_rh_status(hcd);
4849 +
4850 +       return 0;
4851 +}
4852 +
4853 +#else
4854 +
4855 +#define vhci_hcd_suspend       NULL
4856 +#define vhci_hcd_resume                NULL
4857 +
4858 +#endif
4859 +
4860 +
4861 +static struct platform_driver vhci_driver = {
4862 +       .probe  = vhci_hcd_probe,
4863 +       .remove = __devexit_p(vhci_hcd_remove),
4864 +       .suspend = vhci_hcd_suspend,
4865 +       .resume = vhci_hcd_resume,
4866 +       .driver = {
4867 +               .name = (char *) driver_name,
4868 +               .owner = THIS_MODULE,
4869 +       },
4870 +};
4871 +
4872 +/*----------------------------------------------------------------------*/
4873 +
4874 +/*
4875 + * The VHCI 'device' is 'virtual'; not a real plug&play hardware.
4876 + * We need to add this virtual device as a platform device arbitrarily:
4877 + *     1. platform_device_register()
4878 + */
4879 +static void the_pdev_release(struct device *dev)
4880 +{
4881 +       return;
4882 +}
4883 +
4884 +static struct platform_device the_pdev = {
4885 +       /* should be the same name as driver_name */
4886 +       .name = (char *) driver_name,
4887 +       .id = -1,
4888 +       .dev = {
4889 +               /* .driver = &vhci_driver, */
4890 +               .release = the_pdev_release,
4891 +       },
4892 +};
4893 +
4894 +static int __init vhci_init(void)
4895 +{
4896 +       int ret;
4897 +
4898 +       dbg_vhci_hc("enter\n");
4899 +       if (usb_disabled())
4900 +               return -ENODEV;
4901 +
4902 +       printk(KERN_INFO KBUILD_MODNAME ": %s, %s\n", driver_name,
4903 +              DRIVER_VERSION);
4904 +
4905 +       ret = platform_driver_register(&vhci_driver);
4906 +       if (ret < 0)
4907 +               goto err_driver_register;
4908 +
4909 +       ret = platform_device_register(&the_pdev);
4910 +       if (ret < 0)
4911 +               goto err_platform_device_register;
4912 +
4913 +       dbg_vhci_hc("bye\n");
4914 +       return ret;
4915 +
4916 +       /* error occurred */
4917 +err_platform_device_register:
4918 +       platform_driver_unregister(&vhci_driver);
4919 +
4920 +err_driver_register:
4921 +       dbg_vhci_hc("bye\n");
4922 +       return ret;
4923 +}
4924 +module_init(vhci_init);
4925 +
4926 +static void __exit vhci_cleanup(void)
4927 +{
4928 +       dbg_vhci_hc("enter\n");
4929 +
4930 +       platform_device_unregister(&the_pdev);
4931 +       platform_driver_unregister(&vhci_driver);
4932 +
4933 +       dbg_vhci_hc("bye\n");
4934 +}
4935 +module_exit(vhci_cleanup);
4936 --- /dev/null
4937 +++ kernel-2.6.28/drivers/usb/usbip/vhci_rx.c
4938 @@ -0,0 +1,251 @@
4939 +/*
4940 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
4941 + *
4942 + * This is free software; you can redistribute it and/or modify
4943 + * it under the terms of the GNU General Public License as published by
4944 + * the Free Software Foundation; either version 2 of the License, or
4945 + * (at your option) any later version.
4946 + *
4947 + * This is distributed in the hope that it will be useful,
4948 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4949 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4950 + * GNU General Public License for more details.
4951 + *
4952 + * You should have received a copy of the GNU General Public License
4953 + * along with this program; if not, write to the Free Software
4954 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
4955 + * USA.
4956 + */
4957 +
4958 +#include "usbip_common.h"
4959 +#include "vhci.h"
4960 +
4961 +
4962 +/* get URB from transmitted urb queue */
4963 +static struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev,
4964 +                                           __u32 seqnum)
4965 +{
4966 +       struct vhci_priv *priv, *tmp;
4967 +       struct urb *urb = NULL;
4968 +       int status;
4969 +
4970 +       spin_lock(&vdev->priv_lock);
4971 +
4972 +       list_for_each_entry_safe(priv, tmp, &vdev->priv_rx, list) {
4973 +               if (priv->seqnum == seqnum) {
4974 +                       urb = priv->urb;
4975 +                       status = urb->status;
4976 +
4977 +                       dbg_vhci_rx("find urb %p vurb %p seqnum %u\n",
4978 +                                   urb, priv, seqnum);
4979 +
4980 +                       /* TODO: fix logic here to improve indent situtation */
4981 +                       if (status != -EINPROGRESS) {
4982 +                               if (status == -ENOENT ||
4983 +                                    status == -ECONNRESET)
4984 +                                       dev_info(&urb->dev->dev,
4985 +                                                "urb %p was unlinked "
4986 +                                                "%ssynchronuously.\n", urb,
4987 +                                                status == -ENOENT ? "" : "a");
4988 +                               else
4989 +                                       dev_info(&urb->dev->dev,
4990 +                                                "urb %p may be in a error, "
4991 +                                                "status %d\n", urb, status);
4992 +                       }
4993 +
4994 +                       list_del(&priv->list);
4995 +                       kfree(priv);
4996 +                       urb->hcpriv = NULL;
4997 +
4998 +                       break;
4999 +               }
5000 +       }
5001 +
5002 +       spin_unlock(&vdev->priv_lock);
5003 +
5004 +       return urb;
5005 +}
5006 +
5007 +static void vhci_recv_ret_submit(struct vhci_device *vdev,
5008 +                                               struct usbip_header *pdu)
5009 +{
5010 +       struct usbip_device *ud = &vdev->ud;
5011 +       struct urb *urb;
5012 +
5013 +
5014 +       urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
5015 +
5016 +
5017 +       if (!urb) {
5018 +               uerr("cannot find a urb of seqnum %u\n", pdu->base.seqnum);
5019 +               uinfo("max seqnum %d\n", atomic_read(&the_controller->seqnum));
5020 +               usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
5021 +               return;
5022 +       }
5023 +
5024 +
5025 +       /* unpack the pdu to a urb */
5026 +       usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0);
5027 +
5028 +
5029 +       /* recv transfer buffer */
5030 +       if (usbip_recv_xbuff(ud, urb) < 0)
5031 +               return;
5032 +
5033 +
5034 +       /* recv iso_packet_descriptor */
5035 +       if (usbip_recv_iso(ud, urb) < 0)
5036 +               return;
5037 +
5038 +
5039 +       if (dbg_flag_vhci_rx)
5040 +               usbip_dump_urb(urb);
5041 +
5042 +
5043 +       dbg_vhci_rx("now giveback urb %p\n", urb);
5044 +
5045 +       spin_lock(&the_controller->lock);
5046 +       usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
5047 +       spin_unlock(&the_controller->lock);
5048 +
5049 +       usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
5050 +
5051 +
5052 +       dbg_vhci_rx("Leave\n");
5053 +
5054 +       return;
5055 +}
5056 +
5057 +
5058 +static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
5059 +               struct usbip_header *pdu)
5060 +{
5061 +       struct vhci_unlink *unlink, *tmp;
5062 +
5063 +       spin_lock(&vdev->priv_lock);
5064 +
5065 +       list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
5066 +               uinfo("unlink->seqnum %lu\n", unlink->seqnum);
5067 +               if (unlink->seqnum == pdu->base.seqnum) {
5068 +                       dbg_vhci_rx("found pending unlink, %lu\n",
5069 +                                                       unlink->seqnum);
5070 +                       list_del(&unlink->list);
5071 +
5072 +                       spin_unlock(&vdev->priv_lock);
5073 +                       return unlink;
5074 +               }
5075 +       }
5076 +
5077 +       spin_unlock(&vdev->priv_lock);
5078 +
5079 +       return NULL;
5080 +}
5081 +
5082 +
5083 +static void vhci_recv_ret_unlink(struct vhci_device *vdev,
5084 +                                               struct usbip_header *pdu)
5085 +{
5086 +       struct vhci_unlink *unlink;
5087 +       struct urb *urb;
5088 +
5089 +       usbip_dump_header(pdu);
5090 +
5091 +       unlink = dequeue_pending_unlink(vdev, pdu);
5092 +       if (!unlink) {
5093 +               uinfo("cannot find the pending unlink %u\n", pdu->base.seqnum);
5094 +               return;
5095 +       }
5096 +
5097 +       urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
5098 +       if (!urb) {
5099 +               /*
5100 +                * I get the result of a unlink request. But, it seems that I
5101 +                * already received the result of its submit result and gave
5102 +                * back the URB.
5103 +                */
5104 +               uinfo("the urb (seqnum %d) was already given backed\n",
5105 +                                                       pdu->base.seqnum);
5106 +       } else {
5107 +               dbg_vhci_rx("now giveback urb %p\n", urb);
5108 +
5109 +               /* If unlink is succeed, status is -ECONNRESET */
5110 +               urb->status = pdu->u.ret_unlink.status;
5111 +               uinfo("%d\n", urb->status);
5112 +
5113 +               spin_lock(&the_controller->lock);
5114 +               usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
5115 +               spin_unlock(&the_controller->lock);
5116 +
5117 +               usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
5118 +                                                               urb->status);
5119 +       }
5120 +
5121 +       kfree(unlink);
5122 +
5123 +       return;
5124 +}
5125 +
5126 +/* recv a pdu */
5127 +static void vhci_rx_pdu(struct usbip_device *ud)
5128 +{
5129 +       int ret;
5130 +       struct usbip_header pdu;
5131 +       struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
5132 +
5133 +
5134 +       dbg_vhci_rx("Enter\n");
5135 +
5136 +       memset(&pdu, 0, sizeof(pdu));
5137 +
5138 +
5139 +       /* 1. receive a pdu header */
5140 +       ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
5141 +       if (ret != sizeof(pdu)) {
5142 +               uerr("receiving pdu failed! size is %d, should be %d\n",
5143 +                               ret, (unsigned int)sizeof(pdu));
5144 +               usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
5145 +               return;
5146 +       }
5147 +
5148 +       usbip_header_correct_endian(&pdu, 0);
5149 +
5150 +       if (dbg_flag_vhci_rx)
5151 +               usbip_dump_header(&pdu);
5152 +
5153 +       switch (pdu.base.command) {
5154 +       case USBIP_RET_SUBMIT:
5155 +               vhci_recv_ret_submit(vdev, &pdu);
5156 +               break;
5157 +       case USBIP_RET_UNLINK:
5158 +               vhci_recv_ret_unlink(vdev, &pdu);
5159 +               break;
5160 +       default:
5161 +               /* NOTREACHED */
5162 +               uerr("unknown pdu %u\n", pdu.base.command);
5163 +               usbip_dump_header(&pdu);
5164 +               usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
5165 +       }
5166 +}
5167 +
5168 +
5169 +/*-------------------------------------------------------------------------*/
5170 +
5171 +void vhci_rx_loop(struct usbip_task *ut)
5172 +{
5173 +       struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_rx);
5174 +
5175 +
5176 +       while (1) {
5177 +               if (signal_pending(current)) {
5178 +                       dbg_vhci_rx("signal catched!\n");
5179 +                       break;
5180 +               }
5181 +
5182 +
5183 +               if (usbip_event_happend(ud))
5184 +                       break;
5185 +
5186 +               vhci_rx_pdu(ud);
5187 +       }
5188 +}
5189 +
5190 --- /dev/null
5191 +++ kernel-2.6.28/drivers/usb/usbip/vhci_sysfs.c
5192 @@ -0,0 +1,250 @@
5193 +/*
5194 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
5195 + *
5196 + * This is free software; you can redistribute it and/or modify
5197 + * it under the terms of the GNU General Public License as published by
5198 + * the Free Software Foundation; either version 2 of the License, or
5199 + * (at your option) any later version.
5200 + *
5201 + * This is distributed in the hope that it will be useful,
5202 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5203 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5204 + * GNU General Public License for more details.
5205 + *
5206 + * You should have received a copy of the GNU General Public License
5207 + * along with this program; if not, write to the Free Software
5208 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
5209 + * USA.
5210 + */
5211 +
5212 +#include "usbip_common.h"
5213 +#include "vhci.h"
5214 +
5215 +#include <linux/in.h>
5216 +
5217 +/* TODO: refine locking ?*/
5218 +
5219 +/* Sysfs entry to show port status */
5220 +static ssize_t show_status(struct device *dev, struct device_attribute *attr,
5221 +                          char *out)
5222 +{
5223 +       char *s = out;
5224 +       int i = 0;
5225 +
5226 +       if (!the_controller || !out)
5227 +               BUG();
5228 +
5229 +       spin_lock(&the_controller->lock);
5230 +
5231 +       /*
5232 +        * output example:
5233 +        * prt sta spd dev socket           local_busid
5234 +        * 000 004 000 000         c5a7bb80 1-2.3
5235 +        * 001 004 000 000         d8cee980 2-3.4
5236 +        *
5237 +        * IP address can be retrieved from a socket pointer address by looking
5238 +        * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
5239 +        * port number and its peer IP address.
5240 +        */
5241 +       out += sprintf(out, "prt sta spd bus dev socket           "
5242 +                      "local_busid\n");
5243 +
5244 +       for (i = 0; i < VHCI_NPORTS; i++) {
5245 +               struct vhci_device *vdev = port_to_vdev(i);
5246 +
5247 +               spin_lock(&vdev->ud.lock);
5248 +
5249 +               out += sprintf(out, "%03u %03u ", i, vdev->ud.status);
5250 +
5251 +               if (vdev->ud.status == VDEV_ST_USED) {
5252 +                       out += sprintf(out, "%03u %08x ",
5253 +                                       vdev->speed, vdev->devid);
5254 +                       out += sprintf(out, "%16p ", vdev->ud.tcp_socket);
5255 +                       out += sprintf(out, "%s", vdev->udev->dev.bus_id);
5256 +
5257 +               } else
5258 +                       out += sprintf(out, "000 000 000 0000000000000000 0-0");
5259 +
5260 +               out += sprintf(out, "\n");
5261 +
5262 +               spin_unlock(&vdev->ud.lock);
5263 +       }
5264 +
5265 +       spin_unlock(&the_controller->lock);
5266 +
5267 +       return out - s;
5268 +}
5269 +static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
5270 +
5271 +/* Sysfs entry to shutdown a virtual connection */
5272 +static int vhci_port_disconnect(__u32 rhport)
5273 +{
5274 +       struct vhci_device *vdev;
5275 +
5276 +       dbg_vhci_sysfs("enter\n");
5277 +
5278 +       /* lock */
5279 +       spin_lock(&the_controller->lock);
5280 +
5281 +       vdev = port_to_vdev(rhport);
5282 +
5283 +       spin_lock(&vdev->ud.lock);
5284 +       if (vdev->ud.status == VDEV_ST_NULL) {
5285 +               uerr("not connected %d\n", vdev->ud.status);
5286 +
5287 +               /* unlock */
5288 +               spin_unlock(&vdev->ud.lock);
5289 +               spin_unlock(&the_controller->lock);
5290 +
5291 +               return -EINVAL;
5292 +       }
5293 +
5294 +       /* unlock */
5295 +       spin_unlock(&vdev->ud.lock);
5296 +       spin_unlock(&the_controller->lock);
5297 +
5298 +       usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
5299 +
5300 +       return 0;
5301 +}
5302 +
5303 +static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
5304 +                           const char *buf, size_t count)
5305 +{
5306 +       int err;
5307 +       __u32 rhport = 0;
5308 +
5309 +       sscanf(buf, "%u", &rhport);
5310 +
5311 +       /* check rhport */
5312 +       if (rhport >= VHCI_NPORTS) {
5313 +               uerr("invalid port %u\n", rhport);
5314 +               return -EINVAL;
5315 +       }
5316 +
5317 +       err = vhci_port_disconnect(rhport);
5318 +       if (err < 0)
5319 +               return -EINVAL;
5320 +
5321 +       dbg_vhci_sysfs("Leave\n");
5322 +       return count;
5323 +}
5324 +static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
5325 +
5326 +/* Sysfs entry to establish a virtual connection */
5327 +static int valid_args(__u32 rhport, enum usb_device_speed speed)
5328 +{
5329 +       /* check rhport */
5330 +       if ((rhport < 0) || (rhport >= VHCI_NPORTS)) {
5331 +               uerr("port %u\n", rhport);
5332 +               return -EINVAL;
5333 +       }
5334 +
5335 +       /* check speed */
5336 +       switch (speed) {
5337 +       case USB_SPEED_LOW:
5338 +       case USB_SPEED_FULL:
5339 +       case USB_SPEED_HIGH:
5340 +       case USB_SPEED_VARIABLE:
5341 +               break;
5342 +       default:
5343 +               uerr("speed %d\n", speed);
5344 +               return -EINVAL;
5345 +       }
5346 +
5347 +       return 0;
5348 +}
5349 +
5350 +/*
5351 + * To start a new USB/IP attachment, a userland program needs to setup a TCP
5352 + * connection and then write its socket descriptor with remote device
5353 + * information into this sysfs file.
5354 + *
5355 + * A remote device is virtually attached to the root-hub port of @rhport with
5356 + * @speed. @devid is embedded into a request to specify the remote device in a
5357 + * server host.
5358 + *
5359 + * write() returns 0 on success, else negative errno.
5360 + */
5361 +static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
5362 +                           const char *buf, size_t count)
5363 +{
5364 +       struct vhci_device *vdev;
5365 +       struct socket *socket;
5366 +       int sockfd = 0;
5367 +       __u32 rhport = 0, devid = 0, speed = 0;
5368 +
5369 +       /*
5370 +        * @rhport: port number of vhci_hcd
5371 +        * @sockfd: socket descriptor of an established TCP connection
5372 +        * @devid: unique device identifier in a remote host
5373 +        * @speed: usb device speed in a remote host
5374 +        */
5375 +       sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed);
5376 +
5377 +       dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n",
5378 +                       rhport, sockfd, devid, speed);
5379 +
5380 +
5381 +       /* check received parameters */
5382 +       if (valid_args(rhport, speed) < 0)
5383 +               return -EINVAL;
5384 +
5385 +       /* check sockfd */
5386 +       socket = sockfd_to_socket(sockfd);
5387 +       if (!socket)
5388 +               return  -EINVAL;
5389 +
5390 +       /* now need lock until setting vdev status as used */
5391 +
5392 +       /* begin a lock */
5393 +       spin_lock(&the_controller->lock);
5394 +
5395 +       vdev = port_to_vdev(rhport);
5396 +
5397 +       spin_lock(&vdev->ud.lock);
5398 +
5399 +       if (vdev->ud.status != VDEV_ST_NULL) {
5400 +               /* end of the lock */
5401 +               spin_unlock(&vdev->ud.lock);
5402 +               spin_unlock(&the_controller->lock);
5403 +
5404 +               uerr("port %d already used\n", rhport);
5405 +               return -EINVAL;
5406 +       }
5407 +
5408 +       uinfo("rhport(%u) sockfd(%d) devid(%u) speed(%u)\n",
5409 +                       rhport, sockfd, devid, speed);
5410 +
5411 +       vdev->devid         = devid;
5412 +       vdev->speed         = speed;
5413 +       vdev->ud.tcp_socket = socket;
5414 +       vdev->ud.status     = VDEV_ST_NOTASSIGNED;
5415 +
5416 +       spin_unlock(&vdev->ud.lock);
5417 +       spin_unlock(&the_controller->lock);
5418 +       /* end the lock */
5419 +
5420 +       /*
5421 +        * this function will sleep, so should be out of the lock. but, it's ok
5422 +        * because we already marked vdev as being used. really?
5423 +        */
5424 +       usbip_start_threads(&vdev->ud);
5425 +
5426 +       rh_port_connect(rhport, speed);
5427 +
5428 +       return count;
5429 +}
5430 +static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
5431 +
5432 +static struct attribute *dev_attrs[] = {
5433 +       &dev_attr_status.attr,
5434 +       &dev_attr_detach.attr,
5435 +       &dev_attr_attach.attr,
5436 +       &dev_attr_usbip_debug.attr,
5437 +       NULL,
5438 +};
5439 +
5440 +struct attribute_group dev_attr_group = {
5441 +       .attrs = dev_attrs,
5442 +};
5443 --- /dev/null
5444 +++ kernel-2.6.28/drivers/usb/usbip/vhci_tx.c
5445 @@ -0,0 +1,239 @@
5446 +/*
5447 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
5448 + *
5449 + * This is free software; you can redistribute it and/or modify
5450 + * it under the terms of the GNU General Public License as published by
5451 + * the Free Software Foundation; either version 2 of the License, or
5452 + * (at your option) any later version.
5453 + *
5454 + * This is distributed in the hope that it will be useful,
5455 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5456 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5457 + * GNU General Public License for more details.
5458 + *
5459 + * You should have received a copy of the GNU General Public License
5460 + * along with this program; if not, write to the Free Software
5461 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
5462 + * USA.
5463 + */
5464 +
5465 +#include "usbip_common.h"
5466 +#include "vhci.h"
5467 +
5468 +
5469 +static void setup_cmd_submit_pdu(struct usbip_header *pdup,  struct urb *urb)
5470 +{
5471 +       struct vhci_priv *priv = ((struct vhci_priv *)urb->hcpriv);
5472 +       struct vhci_device *vdev = priv->vdev;
5473 +
5474 +       dbg_vhci_tx("URB, local devnum %u, remote devid %u\n",
5475 +                               usb_pipedevice(urb->pipe), vdev->devid);
5476 +
5477 +       pdup->base.command = USBIP_CMD_SUBMIT;
5478 +       pdup->base.seqnum  = priv->seqnum;
5479 +       pdup->base.devid   = vdev->devid;
5480 +       if (usb_pipein(urb->pipe))
5481 +               pdup->base.direction = USBIP_DIR_IN;
5482 +       else
5483 +               pdup->base.direction = USBIP_DIR_OUT;
5484 +       pdup->base.ep      = usb_pipeendpoint(urb->pipe);
5485 +
5486 +       usbip_pack_pdu(pdup, urb, USBIP_CMD_SUBMIT, 1);
5487 +
5488 +       if (urb->setup_packet)
5489 +               memcpy(pdup->u.cmd_submit.setup, urb->setup_packet, 8);
5490 +}
5491 +
5492 +static struct vhci_priv *dequeue_from_priv_tx(struct vhci_device *vdev)
5493 +{
5494 +       unsigned long flags;
5495 +       struct vhci_priv *priv, *tmp;
5496 +
5497 +       spin_lock_irqsave(&vdev->priv_lock, flags);
5498 +
5499 +       list_for_each_entry_safe(priv, tmp, &vdev->priv_tx, list) {
5500 +               list_move_tail(&priv->list, &vdev->priv_rx);
5501 +               spin_unlock_irqrestore(&vdev->priv_lock, flags);
5502 +               return priv;
5503 +       }
5504 +
5505 +       spin_unlock_irqrestore(&vdev->priv_lock, flags);
5506 +
5507 +       return NULL;
5508 +}
5509 +
5510 +
5511 +
5512 +static int vhci_send_cmd_submit(struct vhci_device *vdev)
5513 +{
5514 +       struct vhci_priv *priv = NULL;
5515 +
5516 +       struct msghdr msg;
5517 +       struct kvec iov[3];
5518 +       size_t txsize;
5519 +
5520 +       size_t total_size = 0;
5521 +
5522 +       while ((priv = dequeue_from_priv_tx(vdev)) != NULL) {
5523 +               int ret;
5524 +               struct urb *urb = priv->urb;
5525 +               struct usbip_header pdu_header;
5526 +               void *iso_buffer = NULL;
5527 +
5528 +               txsize = 0;
5529 +               memset(&pdu_header, 0, sizeof(pdu_header));
5530 +               memset(&msg, 0, sizeof(msg));
5531 +               memset(&iov, 0, sizeof(iov));
5532 +
5533 +               dbg_vhci_tx("setup txdata urb %p\n", urb);
5534 +
5535 +
5536 +               /* 1. setup usbip_header */
5537 +               setup_cmd_submit_pdu(&pdu_header, urb);
5538 +               usbip_header_correct_endian(&pdu_header, 1);
5539 +
5540 +               iov[0].iov_base = &pdu_header;
5541 +               iov[0].iov_len  = sizeof(pdu_header);
5542 +               txsize += sizeof(pdu_header);
5543 +
5544 +               /* 2. setup transfer buffer */
5545 +               if (!usb_pipein(urb->pipe) && urb->transfer_buffer_length > 0) {
5546 +                       iov[1].iov_base = urb->transfer_buffer;
5547 +                       iov[1].iov_len  = urb->transfer_buffer_length;
5548 +                       txsize += urb->transfer_buffer_length;
5549 +               }
5550 +
5551 +               /* 3. setup iso_packet_descriptor */
5552 +               if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
5553 +                       ssize_t len = 0;
5554 +
5555 +                       iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
5556 +                       if (!iso_buffer) {
5557 +                               usbip_event_add(&vdev->ud,
5558 +                                               SDEV_EVENT_ERROR_MALLOC);
5559 +                               return -1;
5560 +                       }
5561 +
5562 +                       iov[2].iov_base = iso_buffer;
5563 +                       iov[2].iov_len  = len;
5564 +                       txsize += len;
5565 +               }
5566 +
5567 +               ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 3, txsize);
5568 +               if (ret != txsize) {
5569 +                       uerr("sendmsg failed!, retval %d for %zd\n", ret,
5570 +                                                               txsize);
5571 +                       kfree(iso_buffer);
5572 +                       usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP);
5573 +                       return -1;
5574 +               }
5575 +
5576 +               kfree(iso_buffer);
5577 +               dbg_vhci_tx("send txdata\n");
5578 +
5579 +               total_size += txsize;
5580 +       }
5581 +
5582 +       return total_size;
5583 +}
5584 +
5585 +
5586 +/*-------------------------------------------------------------------------*/
5587 +
5588 +static struct vhci_unlink *dequeue_from_unlink_tx(struct vhci_device *vdev)
5589 +{
5590 +       unsigned long flags;
5591 +       struct vhci_unlink *unlink, *tmp;
5592 +
5593 +       spin_lock_irqsave(&vdev->priv_lock, flags);
5594 +
5595 +       list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
5596 +               list_move_tail(&unlink->list, &vdev->unlink_rx);
5597 +               spin_unlock_irqrestore(&vdev->priv_lock, flags);
5598 +               return unlink;
5599 +       }
5600 +
5601 +       spin_unlock_irqrestore(&vdev->priv_lock, flags);
5602 +
5603 +       return NULL;
5604 +}
5605 +
5606 +static int vhci_send_cmd_unlink(struct vhci_device *vdev)
5607 +{
5608 +       struct vhci_unlink *unlink = NULL;
5609 +
5610 +       struct msghdr msg;
5611 +       struct kvec iov[3];
5612 +       size_t txsize;
5613 +
5614 +       size_t total_size = 0;
5615 +
5616 +       while ((unlink = dequeue_from_unlink_tx(vdev)) != NULL) {
5617 +               int ret;
5618 +               struct usbip_header pdu_header;
5619 +
5620 +               txsize = 0;
5621 +               memset(&pdu_header, 0, sizeof(pdu_header));
5622 +               memset(&msg, 0, sizeof(msg));
5623 +               memset(&iov, 0, sizeof(iov));
5624 +
5625 +               dbg_vhci_tx("setup cmd unlink, %lu \n", unlink->seqnum);
5626 +
5627 +
5628 +               /* 1. setup usbip_header */
5629 +               pdu_header.base.command = USBIP_CMD_UNLINK;
5630 +               pdu_header.base.seqnum  = unlink->seqnum;
5631 +               pdu_header.base.devid   = vdev->devid;
5632 +               pdu_header.base.ep      = 0;
5633 +               pdu_header.u.cmd_unlink.seqnum = unlink->unlink_seqnum;
5634 +
5635 +               usbip_header_correct_endian(&pdu_header, 1);
5636 +
5637 +               iov[0].iov_base = &pdu_header;
5638 +               iov[0].iov_len  = sizeof(pdu_header);
5639 +               txsize += sizeof(pdu_header);
5640 +
5641 +               ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 1, txsize);
5642 +               if (ret != txsize) {
5643 +                       uerr("sendmsg failed!, retval %d for %zd\n", ret,
5644 +                                                               txsize);
5645 +                       usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP);
5646 +                       return -1;
5647 +               }
5648 +
5649 +
5650 +               dbg_vhci_tx("send txdata\n");
5651 +
5652 +               total_size += txsize;
5653 +       }
5654 +
5655 +       return total_size;
5656 +}
5657 +
5658 +
5659 +/*-------------------------------------------------------------------------*/
5660 +
5661 +void vhci_tx_loop(struct usbip_task *ut)
5662 +{
5663 +       struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_tx);
5664 +       struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
5665 +
5666 +       while (1) {
5667 +               if (signal_pending(current)) {
5668 +                       uinfo("vhci_tx signal catched\n");
5669 +                       break;
5670 +               }
5671 +
5672 +               if (vhci_send_cmd_submit(vdev) < 0)
5673 +                       break;
5674 +
5675 +               if (vhci_send_cmd_unlink(vdev) < 0)
5676 +                       break;
5677 +
5678 +               wait_event_interruptible(vdev->waitq_tx,
5679 +                               (!list_empty(&vdev->priv_tx) ||
5680 +                                !list_empty(&vdev->unlink_tx)));
5681 +
5682 +               dbg_vhci_tx("pending urbs ?, now wake up\n");
5683 +       }
5684 +}