]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/patches/suse-2.6.27.31/patches.drivers/0013-Staging-USB-IP-add-host-driver.patch
Merge branch 'master' into next
[people/pmueller/ipfire-2.x.git] / src / patches / suse-2.6.27.31 / patches.drivers / 0013-Staging-USB-IP-add-host-driver.patch
CommitLineData
2cb7cef9
BS
1From 4d7b5c7f8ad49b7f01fb8aed83c560ac43cfbda8 Mon Sep 17 00:00:00 2001
2From: Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>
3Date: Wed, 9 Jul 2008 14:56:51 -0600
4Subject: [PATCH 13/23] Staging: USB/IP: add host driver
5Patch-mainline: 2.6.28
6
7This adds the USB IP client driver
8
9Brian Merrell cleaned up a lot of this code and submitted it for
10inclusion. Greg also did a lot of cleanup.
11
12Signed-off-by: Brian G. Merrell <bgmerrell@novell.com>
13Cc: Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>
14Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
15---
16 drivers/staging/usbip/Kconfig | 11 +
17 drivers/staging/usbip/Makefile | 3 +
18 drivers/staging/usbip/stub.h | 95 ++++++
19 drivers/staging/usbip/stub_dev.c | 483 +++++++++++++++++++++++++++++
20 drivers/staging/usbip/stub_main.c | 300 ++++++++++++++++++
21 drivers/staging/usbip/stub_rx.c | 615 +++++++++++++++++++++++++++++++++++++
22 drivers/staging/usbip/stub_tx.c | 371 ++++++++++++++++++++++
23 7 files changed, 1878 insertions(+), 0 deletions(-)
24 create mode 100644 drivers/staging/usbip/stub.h
25 create mode 100644 drivers/staging/usbip/stub_dev.c
26 create mode 100644 drivers/staging/usbip/stub_main.c
27 create mode 100644 drivers/staging/usbip/stub_rx.c
28 create mode 100644 drivers/staging/usbip/stub_tx.c
29
30diff --git a/drivers/staging/usbip/Kconfig b/drivers/staging/usbip/Kconfig
31index c4d68e1..7426235 100644
32--- a/drivers/staging/usbip/Kconfig
33+++ b/drivers/staging/usbip/Kconfig
34@@ -23,3 +23,14 @@ config USB_IP_VHCI_HCD
35
36 To compile this driver as a module, choose M here: the
37 module will be called vhci_hcd.
38+
39+config USB_IP_HOST
40+ tristate "USB IP host driver"
41+ depends on USB_IP_COMMON
42+ default N
43+ ---help---
44+ This enables the USB IP device driver which will run on the
45+ host machine.
46+
47+ To compile this driver as a module, choose M here: the
48+ module will be called usbip.
49diff --git a/drivers/staging/usbip/Makefile b/drivers/staging/usbip/Makefile
50index 6ef4c39..179f421 100644
51--- a/drivers/staging/usbip/Makefile
52+++ b/drivers/staging/usbip/Makefile
53@@ -4,6 +4,9 @@ usbip_common_mod-objs := usbip_common.o usbip_event.o
54 obj-$(CONFIG_USB_IP_VHCI_HCD) += vhci-hcd.o
55 vhci-hcd-objs := vhci_sysfs.o vhci_tx.o vhci_rx.o vhci_hcd.o
56
57+obj-$(CONFIG_USB_IP_HOST) += usbip.o
58+usbip-objs := stub_dev.o stub_main.o stub_rx.o stub_tx.o
59+
60 ifeq ($(CONFIG_USB_DEBUG),y)
61 EXTRA_CFLAGS += -DDEBUG
62 endif
63diff --git a/drivers/staging/usbip/stub.h b/drivers/staging/usbip/stub.h
64new file mode 100644
65index 0000000..f541a3a
66--- /dev/null
67+++ b/drivers/staging/usbip/stub.h
68@@ -0,0 +1,95 @@
69+/*
70+ * Copyright (C) 2003-2008 Takahiro Hirofuchi
71+ *
72+ * This is free software; you can redistribute it and/or modify
73+ * it under the terms of the GNU General Public License as published by
74+ * the Free Software Foundation; either version 2 of the License, or
75+ * (at your option) any later version.
76+ *
77+ * This is distributed in the hope that it will be useful,
78+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
79+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
80+ * GNU General Public License for more details.
81+ *
82+ * You should have received a copy of the GNU General Public License
83+ * along with this program; if not, write to the Free Software
84+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
85+ * USA.
86+ */
87+
88+#include <linux/kernel.h>
89+#include <linux/list.h>
90+#include <linux/spinlock.h>
91+#include <linux/slab.h>
92+#include <linux/string.h>
93+#include <linux/module.h>
94+#include <linux/net.h>
95+
96+struct stub_device {
97+ struct usb_interface *interface;
98+ struct list_head list;
99+
100+ struct usbip_device ud;
101+ __u32 devid;
102+
103+ /*
104+ * stub_priv preserves private data of each urb.
105+ * It is allocated as stub_priv_cache and assigned to urb->context.
106+ *
107+ * stub_priv is always linked to any one of 3 lists;
108+ * priv_init: linked to this until the comletion of a urb.
109+ * priv_tx : linked to this after the completion of a urb.
110+ * priv_free: linked to this after the sending of the result.
111+ *
112+ * Any of these list operations should be locked by priv_lock.
113+ */
114+ spinlock_t priv_lock;
115+ struct list_head priv_init;
116+ struct list_head priv_tx;
117+ struct list_head priv_free;
118+
119+ /* see comments for unlinking in stub_rx.c */
120+ struct list_head unlink_tx;
121+ struct list_head unlink_free;
122+
123+
124+ wait_queue_head_t tx_waitq;
125+};
126+
127+/* private data into urb->priv */
128+struct stub_priv {
129+ unsigned long seqnum;
130+ struct list_head list;
131+ struct stub_device *sdev;
132+ struct urb *urb;
133+
134+ int unlinking;
135+};
136+
137+struct stub_unlink {
138+ unsigned long seqnum;
139+ struct list_head list;
140+ __u32 status;
141+};
142+
143+
144+extern struct kmem_cache *stub_priv_cache;
145+
146+
147+/*-------------------------------------------------------------------------*/
148+/* prototype declarations */
149+
150+/* stub_tx.c */
151+void stub_complete(struct urb *);
152+void stub_tx_loop(struct usbip_task *);
153+
154+/* stub_dev.c */
155+extern struct usb_driver stub_driver;
156+
157+/* stub_rx.c */
158+void stub_rx_loop(struct usbip_task *);
159+void stub_enqueue_ret_unlink(struct stub_device *, __u32, __u32);
160+
161+/* stub_main.c */
162+int match_busid(char *busid);
163+void stub_device_cleanup_urbs(struct stub_device *sdev);
164diff --git a/drivers/staging/usbip/stub_dev.c b/drivers/staging/usbip/stub_dev.c
165new file mode 100644
166index 0000000..ee455a0
167--- /dev/null
168+++ b/drivers/staging/usbip/stub_dev.c
169@@ -0,0 +1,483 @@
170+/*
171+ * Copyright (C) 2003-2008 Takahiro Hirofuchi
172+ *
173+ * This is free software; you can redistribute it and/or modify
174+ * it under the terms of the GNU General Public License as published by
175+ * the Free Software Foundation; either version 2 of the License, or
176+ * (at your option) any later version.
177+ *
178+ * This is distributed in the hope that it will be useful,
179+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
180+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
181+ * GNU General Public License for more details.
182+ *
183+ * You should have received a copy of the GNU General Public License
184+ * along with this program; if not, write to the Free Software
185+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
186+ * USA.
187+ */
188+
189+#include "usbip_common.h"
190+#include "stub.h"
191+
192+
193+
194+static int stub_probe(struct usb_interface *interface,
195+ const struct usb_device_id *id);
196+static void stub_disconnect(struct usb_interface *interface);
197+
198+
199+/*
200+ * Define device IDs here if you want to explicitly limit exportable devices.
201+ * In the most cases, wild card matching will be ok because driver binding can
202+ * be changed dynamically by a userland program.
203+ */
204+static struct usb_device_id stub_table[] = {
205+#if 0
206+ /* just an example */
207+ { USB_DEVICE(0x05ac, 0x0301) }, /* Mac 1 button mouse */
208+ { USB_DEVICE(0x0430, 0x0009) }, /* Plat Home Keyboard */
209+ { USB_DEVICE(0x059b, 0x0001) }, /* Iomega USB Zip 100 */
210+ { USB_DEVICE(0x04b3, 0x4427) }, /* IBM USB CD-ROM */
211+ { USB_DEVICE(0x05a9, 0xa511) }, /* LifeView USB cam */
212+ { USB_DEVICE(0x55aa, 0x0201) }, /* Imation card reader */
213+ { USB_DEVICE(0x046d, 0x0870) }, /* Qcam Express(QV-30) */
214+ { USB_DEVICE(0x04bb, 0x0101) }, /* IO-DATA HD 120GB */
215+ { USB_DEVICE(0x04bb, 0x0904) }, /* IO-DATA USB-ET/TX */
216+ { USB_DEVICE(0x04bb, 0x0201) }, /* IO-DATA USB-ET/TX */
217+ { USB_DEVICE(0x08bb, 0x2702) }, /* ONKYO USB Speaker */
218+ { USB_DEVICE(0x046d, 0x08b2) }, /* Logicool Qcam 4000 Pro */
219+#endif
220+ /* magic for wild card */
221+ { .driver_info = 1 },
222+ { 0, } /* Terminating entry */
223+};
224+MODULE_DEVICE_TABLE(usb, stub_table);
225+
226+struct usb_driver stub_driver = {
227+ .name = "usbip",
228+ .probe = stub_probe,
229+ .disconnect = stub_disconnect,
230+ .id_table = stub_table,
231+};
232+
233+
234+/*-------------------------------------------------------------------------*/
235+
236+/* Define sysfs entries for a usbip-bound device */
237+
238+
239+/*
240+ * usbip_status shows status of usbip as long as this driver is bound to the
241+ * target device.
242+ */
243+static ssize_t show_status(struct device *dev, struct device_attribute *attr,
244+ char *buf)
245+{
246+ struct stub_device *sdev = dev_get_drvdata(dev);
247+ int status;
248+
249+ if (!sdev) {
250+ dev_err(dev, "sdev is null\n");
251+ return -ENODEV;
252+ }
253+
254+ spin_lock(&sdev->ud.lock);
255+ status = sdev->ud.status;
256+ spin_unlock(&sdev->ud.lock);
257+
258+ return snprintf(buf, PAGE_SIZE, "%d\n", status);
259+}
260+static DEVICE_ATTR(usbip_status, S_IRUGO, show_status, NULL);
261+
262+/*
263+ * usbip_sockfd gets a socket descriptor of an established TCP connection that
264+ * is used to transfer usbip requests by kernel threads. -1 is a magic number
265+ * by which usbip connection is finished.
266+ */
267+static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
268+ const char *buf, size_t count)
269+{
270+ struct stub_device *sdev = dev_get_drvdata(dev);
271+ int sockfd = 0;
272+ struct socket *socket;
273+
274+ if (!sdev) {
275+ dev_err(dev, "sdev is null\n");
276+ return -ENODEV;
277+ }
278+
279+ sscanf(buf, "%d", &sockfd);
280+
281+ if (sockfd != -1) {
282+ dev_info(dev, "stub up\n");
283+
284+ spin_lock(&sdev->ud.lock);
285+
286+ if (sdev->ud.status != SDEV_ST_AVAILABLE) {
287+ dev_err(dev, "not ready\n");
288+ spin_unlock(&sdev->ud.lock);
289+ return -EINVAL;
290+ }
291+
292+ socket = sockfd_to_socket(sockfd);
293+ if (!socket) {
294+ spin_unlock(&sdev->ud.lock);
295+ return -EINVAL;
296+ }
297+
298+#if 0
299+ setnodelay(socket);
300+ setkeepalive(socket);
301+ setreuse(socket);
302+#endif
303+
304+ sdev->ud.tcp_socket = socket;
305+
306+ spin_unlock(&sdev->ud.lock);
307+
308+ usbip_start_threads(&sdev->ud);
309+
310+ spin_lock(&sdev->ud.lock);
311+ sdev->ud.status = SDEV_ST_USED;
312+ spin_unlock(&sdev->ud.lock);
313+
314+ } else {
315+ dev_info(dev, "stub down\n");
316+
317+ spin_lock(&sdev->ud.lock);
318+ if (sdev->ud.status != SDEV_ST_USED) {
319+ spin_unlock(&sdev->ud.lock);
320+ return -EINVAL;
321+ }
322+ spin_unlock(&sdev->ud.lock);
323+
324+ usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
325+ }
326+
327+ return count;
328+}
329+static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
330+
331+static int stub_add_files(struct device *dev)
332+{
333+ int err = 0;
334+
335+ err = device_create_file(dev, &dev_attr_usbip_status);
336+ if (err)
337+ goto err_status;
338+
339+ err = device_create_file(dev, &dev_attr_usbip_sockfd);
340+ if (err)
341+ goto err_sockfd;
342+
343+ err = device_create_file(dev, &dev_attr_usbip_debug);
344+ if (err)
345+ goto err_debug;
346+
347+ return 0;
348+
349+err_debug:
350+ device_remove_file(dev, &dev_attr_usbip_sockfd);
351+
352+err_sockfd:
353+ device_remove_file(dev, &dev_attr_usbip_status);
354+
355+err_status:
356+ return err;
357+}
358+
359+static void stub_remove_files(struct device *dev)
360+{
361+ device_remove_file(dev, &dev_attr_usbip_status);
362+ device_remove_file(dev, &dev_attr_usbip_sockfd);
363+ device_remove_file(dev, &dev_attr_usbip_debug);
364+}
365+
366+
367+
368+/*-------------------------------------------------------------------------*/
369+
370+/* Event handler functions called by an event handler thread */
371+
372+static void stub_shutdown_connection(struct usbip_device *ud)
373+{
374+ struct stub_device *sdev = container_of(ud, struct stub_device, ud);
375+
376+ /*
377+ * When removing an exported device, kernel panic sometimes occurred
378+ * and then EIP was sk_wait_data of stub_rx thread. Is this because
379+ * sk_wait_data returned though stub_rx thread was already finished by
380+ * step 1?
381+ */
382+ if (ud->tcp_socket) {
383+ udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
384+ kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
385+ }
386+
387+ /* 1. stop threads */
388+ usbip_stop_threads(ud);
389+
390+ /* 2. close the socket */
391+ /*
392+ * tcp_socket is freed after threads are killed.
393+ * So usbip_xmit do not touch NULL socket.
394+ */
395+ if (ud->tcp_socket) {
396+ sock_release(ud->tcp_socket);
397+ ud->tcp_socket = NULL;
398+ }
399+
400+ /* 3. free used data */
401+ stub_device_cleanup_urbs(sdev);
402+
403+ /* 4. free stub_unlink */
404+ {
405+ unsigned long flags;
406+ struct stub_unlink *unlink, *tmp;
407+
408+ spin_lock_irqsave(&sdev->priv_lock, flags);
409+
410+ list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
411+ list_del(&unlink->list);
412+ kfree(unlink);
413+ }
414+
415+ list_for_each_entry_safe(unlink, tmp,
416+ &sdev->unlink_free, list) {
417+ list_del(&unlink->list);
418+ kfree(unlink);
419+ }
420+
421+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
422+ }
423+}
424+
425+static void stub_device_reset(struct usbip_device *ud)
426+{
427+ struct stub_device *sdev = container_of(ud, struct stub_device, ud);
428+ struct usb_device *udev = interface_to_usbdev(sdev->interface);
429+ int ret;
430+
431+ udbg("device reset");
432+ ret = usb_lock_device_for_reset(udev, sdev->interface);
433+ if (ret < 0) {
434+ dev_err(&udev->dev, "lock for reset\n");
435+
436+ spin_lock(&ud->lock);
437+ ud->status = SDEV_ST_ERROR;
438+ spin_unlock(&ud->lock);
439+
440+ return;
441+ }
442+
443+ /* try to reset the device */
444+ ret = usb_reset_device(udev);
445+
446+ usb_unlock_device(udev);
447+
448+ spin_lock(&ud->lock);
449+ if (ret) {
450+ dev_err(&udev->dev, "device reset\n");
451+ ud->status = SDEV_ST_ERROR;
452+
453+ } else {
454+ dev_info(&udev->dev, "device reset\n");
455+ ud->status = SDEV_ST_AVAILABLE;
456+
457+ }
458+ spin_unlock(&ud->lock);
459+
460+ return;
461+}
462+
463+static void stub_device_unusable(struct usbip_device *ud)
464+{
465+ spin_lock(&ud->lock);
466+ ud->status = SDEV_ST_ERROR;
467+ spin_unlock(&ud->lock);
468+}
469+
470+
471+/*-------------------------------------------------------------------------*/
472+
473+/**
474+ * stub_device_alloc - allocate a new stub_device struct
475+ * @interface: usb_interface of a new device
476+ *
477+ * Allocates and initializes a new stub_device struct.
478+ */
479+static struct stub_device *stub_device_alloc(struct usb_interface *interface)
480+{
481+ struct stub_device *sdev;
482+ int busnum = interface_to_busnum(interface);
483+ int devnum = interface_to_devnum(interface);
484+
485+ dev_dbg(&interface->dev, "allocating stub device");
486+
487+ /* yes, it's a new device */
488+ sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
489+ if (!sdev) {
490+ dev_err(&interface->dev, "no memory for stub_device\n");
491+ return NULL;
492+ }
493+
494+ sdev->interface = interface;
495+
496+ /*
497+ * devid is defined with devnum when this driver is first allocated.
498+ * devnum may change later if a device is reset. However, devid never
499+ * changes during a usbip connection.
500+ */
501+ sdev->devid = (busnum << 16) | devnum;
502+
503+ usbip_task_init(&sdev->ud.tcp_rx, "stub_rx", stub_rx_loop);
504+ usbip_task_init(&sdev->ud.tcp_tx, "stub_tx", stub_tx_loop);
505+
506+ sdev->ud.side = USBIP_STUB;
507+ sdev->ud.status = SDEV_ST_AVAILABLE;
508+ /* sdev->ud.lock = SPIN_LOCK_UNLOCKED; */
509+ spin_lock_init(&sdev->ud.lock);
510+ sdev->ud.tcp_socket = NULL;
511+
512+ INIT_LIST_HEAD(&sdev->priv_init);
513+ INIT_LIST_HEAD(&sdev->priv_tx);
514+ INIT_LIST_HEAD(&sdev->priv_free);
515+ INIT_LIST_HEAD(&sdev->unlink_free);
516+ INIT_LIST_HEAD(&sdev->unlink_tx);
517+ /* sdev->priv_lock = SPIN_LOCK_UNLOCKED; */
518+ spin_lock_init(&sdev->priv_lock);
519+
520+ init_waitqueue_head(&sdev->tx_waitq);
521+
522+ sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
523+ sdev->ud.eh_ops.reset = stub_device_reset;
524+ sdev->ud.eh_ops.unusable = stub_device_unusable;
525+
526+ usbip_start_eh(&sdev->ud);
527+
528+ udbg("register new interface\n");
529+ return sdev;
530+}
531+
532+static int stub_device_free(struct stub_device *sdev)
533+{
534+ if (!sdev)
535+ return -EINVAL;
536+
537+ kfree(sdev);
538+ udbg("kfree udev ok\n");
539+
540+ return 0;
541+}
542+
543+
544+/*-------------------------------------------------------------------------*/
545+
546+/*
547+ * If a usb device has multiple active interfaces, this driver is bound to all
548+ * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
549+ * active interface). Currently, a userland program must ensure that it
550+ * looks at the usbip's sysfs entries of only the first active interface.
551+ *
552+ * TODO: use "struct usb_device_driver" to bind a usb device.
553+ * However, it seems it is not fully supported in mainline kernel yet
554+ * (2.6.19.2).
555+ */
556+static int stub_probe(struct usb_interface *interface,
557+ const struct usb_device_id *id)
558+{
559+ struct usb_device *udev = interface_to_usbdev(interface);
560+ struct stub_device *sdev = NULL;
561+ char *udev_busid = interface->dev.parent->bus_id;
562+ int err = 0;
563+
564+ dev_dbg(&interface->dev, "Enter\n");
565+
566+ /* check we should claim or not by busid_table */
567+ if (match_busid(udev_busid)) {
568+ dev_info(&interface->dev,
569+ "this device %s is not in match_busid table. skip!\n",
570+ udev_busid);
571+
572+ /*
573+ * Return value should be ENODEV or ENOXIO to continue trying
574+ * other matched drivers by the driver core.
575+ * See driver_probe_device() in driver/base/dd.c
576+ */
577+ return -ENODEV;
578+ }
579+
580+ if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
581+ udbg("this device %s is a usb hub device. skip!\n",
582+ udev_busid);
583+ return -ENODEV;
584+ }
585+
586+ if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
587+ udbg("this device %s is attached on vhci_hcd. skip!\n",
588+ udev_busid);
589+ return -ENODEV;
590+ }
591+
592+ /* ok. this is my device. */
593+ sdev = stub_device_alloc(interface);
594+ if (!sdev)
595+ return -ENOMEM;
596+
597+ dev_info(&interface->dev, "USB/IP Stub: register a new interface "
598+ "(bus %u dev %u ifn %u)\n", udev->bus->busnum, udev->devnum,
599+ interface->cur_altsetting->desc.bInterfaceNumber);
600+
601+ /* set private data to usb_interface */
602+ usb_set_intfdata(interface, sdev);
603+
604+ err = stub_add_files(&interface->dev);
605+ if (err) {
606+ dev_err(&interface->dev, "create sysfs files for %s\n",
607+ udev_busid);
608+ return err;
609+ }
610+
611+ return 0;
612+}
613+
614+
615+/*
616+ * called in usb_disconnect() or usb_deregister()
617+ * but only if actconfig(active configuration) exists
618+ */
619+static void stub_disconnect(struct usb_interface *interface)
620+{
621+ struct stub_device *sdev = usb_get_intfdata(interface);
622+
623+ udbg("Enter\n");
624+
625+ /* get stub_device */
626+ if (!sdev) {
627+ err(" could not get device from inteface data");
628+ /* BUG(); */
629+ return;
630+ }
631+
632+ usb_set_intfdata(interface, NULL);
633+
634+
635+ /*
636+ * NOTE:
637+ * rx/tx threads are invoked for each usb_device.
638+ */
639+ stub_remove_files(&interface->dev);
640+
641+ /* 1. shutdown the current connection */
642+ usbip_event_add(&sdev->ud, SDEV_EVENT_REMOVED);
643+
644+ /* 2. wait for the stop of the event handler */
645+ usbip_stop_eh(&sdev->ud);
646+
647+ /* 3. free sdev */
648+ stub_device_free(sdev);
649+
650+
651+ udbg("bye\n");
652+}
653diff --git a/drivers/staging/usbip/stub_main.c b/drivers/staging/usbip/stub_main.c
654new file mode 100644
655index 0000000..c665d7f
656--- /dev/null
657+++ b/drivers/staging/usbip/stub_main.c
658@@ -0,0 +1,300 @@
659+/*
660+ * Copyright (C) 2003-2008 Takahiro Hirofuchi
661+ *
662+ * This is free software; you can redistribute it and/or modify
663+ * it under the terms of the GNU General Public License as published by
664+ * the Free Software Foundation; either version 2 of the License, or
665+ * (at your option) any later version.
666+ *
667+ * This is distributed in the hope that it will be useful,
668+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
669+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
670+ * GNU General Public License for more details.
671+ *
672+ * You should have received a copy of the GNU General Public License
673+ * along with this program; if not, write to the Free Software
674+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
675+ * USA.
676+ */
677+
678+
679+#include "usbip_common.h"
680+#include "stub.h"
681+
682+/* Version Information */
683+#define DRIVER_VERSION "1.0"
684+#define DRIVER_AUTHOR "Takahiro Hirofuchi"
685+#define DRIVER_DESC "Stub Driver for USB/IP"
686+
687+/* stub_priv is allocated from stub_priv_cache */
688+struct kmem_cache *stub_priv_cache;
689+
690+/*-------------------------------------------------------------------------*/
691+
692+/* Define sysfs entries for the usbip driver */
693+
694+
695+/*
696+ * busid_tables defines matching busids that usbip can grab. A user can change
697+ * dynamically what device is locally used and what device is exported to a
698+ * remote host.
699+ */
700+#define MAX_BUSID 16
701+static char busid_table[MAX_BUSID][BUS_ID_SIZE];
702+static spinlock_t busid_table_lock;
703+
704+
705+int match_busid(char *busid)
706+{
707+ int i;
708+
709+ spin_lock(&busid_table_lock);
710+
711+ for (i = 0; i < MAX_BUSID; i++)
712+ if (busid_table[i][0])
713+ if (!strncmp(busid_table[i], busid, BUS_ID_SIZE)) {
714+ /* already registerd */
715+ spin_unlock(&busid_table_lock);
716+ return 0;
717+ }
718+
719+ spin_unlock(&busid_table_lock);
720+
721+ return 1;
722+}
723+
724+static ssize_t show_match_busid(struct device_driver *drv, char *buf)
725+{
726+ int i;
727+ char *out = buf;
728+
729+ spin_lock(&busid_table_lock);
730+
731+ for (i = 0; i < MAX_BUSID; i++)
732+ if (busid_table[i][0])
733+ out += sprintf(out, "%s ", busid_table[i]);
734+
735+ spin_unlock(&busid_table_lock);
736+
737+ out += sprintf(out, "\n");
738+
739+ return out - buf;
740+}
741+
742+static int add_match_busid(char *busid)
743+{
744+ int i;
745+
746+ if (!match_busid(busid))
747+ return 0;
748+
749+ spin_lock(&busid_table_lock);
750+
751+ for (i = 0; i < MAX_BUSID; i++)
752+ if (!busid_table[i][0]) {
753+ strncpy(busid_table[i], busid, BUS_ID_SIZE);
754+ spin_unlock(&busid_table_lock);
755+ return 0;
756+ }
757+
758+ spin_unlock(&busid_table_lock);
759+
760+ return -1;
761+}
762+
763+static int del_match_busid(char *busid)
764+{
765+ int i;
766+
767+ spin_lock(&busid_table_lock);
768+
769+ for (i = 0; i < MAX_BUSID; i++)
770+ if (!strncmp(busid_table[i], busid, BUS_ID_SIZE)) {
771+ /* found */
772+ memset(busid_table[i], 0, BUS_ID_SIZE);
773+ spin_unlock(&busid_table_lock);
774+ return 0;
775+ }
776+
777+ spin_unlock(&busid_table_lock);
778+
779+ return -1;
780+}
781+
782+static ssize_t store_match_busid(struct device_driver *dev, const char *buf,
783+ size_t count)
784+{
785+ int len;
786+ char busid[BUS_ID_SIZE];
787+
788+ if (count < 5)
789+ return -EINVAL;
790+
791+ /* strnlen() does not include \0 */
792+ len = strnlen(buf + 4, BUS_ID_SIZE);
793+
794+ /* busid needs to include \0 termination */
795+ if (!(len < BUS_ID_SIZE))
796+ return -EINVAL;
797+
798+ strncpy(busid, buf + 4, BUS_ID_SIZE);
799+
800+
801+ if (!strncmp(buf, "add ", 4)) {
802+ if (add_match_busid(busid) < 0)
803+ return -ENOMEM;
804+ else {
805+ udbg("add busid %s\n", busid);
806+ return count;
807+ }
808+ } else if (!strncmp(buf, "del ", 4)) {
809+ if (del_match_busid(busid) < 0)
810+ return -ENODEV;
811+ else {
812+ udbg("del busid %s\n", busid);
813+ return count;
814+ }
815+ } else
816+ return -EINVAL;
817+}
818+
819+static DRIVER_ATTR(match_busid, S_IRUSR|S_IWUSR, show_match_busid,
820+ store_match_busid);
821+
822+
823+
824+/*-------------------------------------------------------------------------*/
825+
826+/* Cleanup functions used to free private data */
827+
828+static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
829+{
830+ struct stub_priv *priv, *tmp;
831+
832+ list_for_each_entry_safe(priv, tmp, listhead, list) {
833+ list_del(&priv->list);
834+ return priv;
835+ }
836+
837+ return NULL;
838+}
839+
840+static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
841+{
842+ unsigned long flags;
843+ struct stub_priv *priv;
844+
845+ spin_lock_irqsave(&sdev->priv_lock, flags);
846+
847+ priv = stub_priv_pop_from_listhead(&sdev->priv_init);
848+ if (priv) {
849+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
850+ return priv;
851+ }
852+
853+ priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
854+ if (priv) {
855+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
856+ return priv;
857+ }
858+
859+ priv = stub_priv_pop_from_listhead(&sdev->priv_free);
860+ if (priv) {
861+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
862+ return priv;
863+ }
864+
865+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
866+ return NULL;
867+}
868+
869+void stub_device_cleanup_urbs(struct stub_device *sdev)
870+{
871+ struct stub_priv *priv;
872+
873+ udbg("free sdev %p\n", sdev);
874+
875+ while ((priv = stub_priv_pop(sdev))) {
876+ struct urb *urb = priv->urb;
877+
878+ udbg(" free urb %p\n", urb);
879+ usb_kill_urb(urb);
880+
881+ kmem_cache_free(stub_priv_cache, priv);
882+
883+ if (urb->transfer_buffer != NULL)
884+ kfree(urb->transfer_buffer);
885+
886+ if (urb->setup_packet != NULL)
887+ kfree(urb->setup_packet);
888+
889+ usb_free_urb(urb);
890+ }
891+}
892+
893+
894+/*-------------------------------------------------------------------------*/
895+
896+static int __init usb_stub_init(void)
897+{
898+ int ret;
899+
900+ stub_priv_cache = kmem_cache_create("stub_priv",
901+ sizeof(struct stub_priv), 0,
902+ SLAB_HWCACHE_ALIGN, NULL);
903+
904+ if (!stub_priv_cache) {
905+ printk(KERN_ERR KBUILD_MODNAME
906+ ": create stub_priv_cache error\n");
907+ return -ENOMEM;
908+ }
909+
910+ ret = usb_register(&stub_driver);
911+ if (ret) {
912+ printk(KERN_ERR KBUILD_MODNAME ": usb_register failed %d\n",
913+ ret);
914+ goto error_usb_register;
915+ }
916+
917+ printk(KERN_INFO KBUILD_MODNAME ":"
918+ DRIVER_DESC ":" DRIVER_VERSION "\n");
919+
920+ memset(busid_table, 0, sizeof(busid_table));
921+ spin_lock_init(&busid_table_lock);
922+
923+ ret = driver_create_file(&stub_driver.drvwrap.driver,
924+ &driver_attr_match_busid);
925+
926+ if (ret) {
927+ printk(KERN_ERR KBUILD_MODNAME ": create driver sysfs\n");
928+ goto error_create_file;
929+ }
930+
931+ return ret;
932+error_create_file:
933+ usb_deregister(&stub_driver);
934+error_usb_register:
935+ kmem_cache_destroy(stub_priv_cache);
936+ return ret;
937+}
938+
939+static void __exit usb_stub_exit(void)
940+{
941+ driver_remove_file(&stub_driver.drvwrap.driver,
942+ &driver_attr_match_busid);
943+
944+ /*
945+ * deregister() calls stub_disconnect() for all devices. Device
946+ * specific data is cleared in stub_disconnect().
947+ */
948+ usb_deregister(&stub_driver);
949+
950+ kmem_cache_destroy(stub_priv_cache);
951+}
952+
953+module_init(usb_stub_init);
954+module_exit(usb_stub_exit);
955+
956+MODULE_AUTHOR(DRIVER_AUTHOR);
957+MODULE_DESCRIPTION(DRIVER_DESC);
958+MODULE_LICENSE("GPL");
959diff --git a/drivers/staging/usbip/stub_rx.c b/drivers/staging/usbip/stub_rx.c
960new file mode 100644
961index 0000000..36ce898
962--- /dev/null
963+++ b/drivers/staging/usbip/stub_rx.c
964@@ -0,0 +1,615 @@
965+/*
966+ * Copyright (C) 2003-2008 Takahiro Hirofuchi
967+ *
968+ * This is free software; you can redistribute it and/or modify
969+ * it under the terms of the GNU General Public License as published by
970+ * the Free Software Foundation; either version 2 of the License, or
971+ * (at your option) any later version.
972+ *
973+ * This is distributed in the hope that it will be useful,
974+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
975+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
976+ * GNU General Public License for more details.
977+ *
978+ * You should have received a copy of the GNU General Public License
979+ * along with this program; if not, write to the Free Software
980+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
981+ * USA.
982+ */
983+
984+#include "usbip_common.h"
985+#include "stub.h"
986+#include "../../usb/core/hcd.h"
987+
988+
989+static int is_clear_halt_cmd(struct urb *urb)
990+{
991+ struct usb_ctrlrequest *req;
992+
993+ req = (struct usb_ctrlrequest *) urb->setup_packet;
994+
995+ return (req->bRequest == USB_REQ_CLEAR_FEATURE) &&
996+ (req->bRequestType == USB_RECIP_ENDPOINT) &&
997+ (req->wValue == USB_ENDPOINT_HALT);
998+}
999+
1000+static int is_set_interface_cmd(struct urb *urb)
1001+{
1002+ struct usb_ctrlrequest *req;
1003+
1004+ req = (struct usb_ctrlrequest *) urb->setup_packet;
1005+
1006+ return (req->bRequest == USB_REQ_SET_INTERFACE) &&
1007+ (req->bRequestType == USB_RECIP_INTERFACE);
1008+}
1009+
1010+static int is_set_configuration_cmd(struct urb *urb)
1011+{
1012+ struct usb_ctrlrequest *req;
1013+
1014+ req = (struct usb_ctrlrequest *) urb->setup_packet;
1015+
1016+ return (req->bRequest == USB_REQ_SET_CONFIGURATION) &&
1017+ (req->bRequestType == USB_RECIP_DEVICE);
1018+}
1019+
1020+static int is_reset_device_cmd(struct urb *urb)
1021+{
1022+ struct usb_ctrlrequest *req;
1023+ __u16 value;
1024+ __u16 index;
1025+
1026+ req = (struct usb_ctrlrequest *) urb->setup_packet;
1027+ value = le16_to_cpu(req->wValue);
1028+ index = le16_to_cpu(req->wIndex);
1029+
1030+ if ((req->bRequest == USB_REQ_SET_FEATURE) &&
1031+ (req->bRequestType == USB_RT_PORT) &&
1032+ (value = USB_PORT_FEAT_RESET)) {
1033+ dbg_stub_rx("reset_device_cmd, port %u\n", index);
1034+ return 1;
1035+ } else
1036+ return 0;
1037+}
1038+
1039+static int tweak_clear_halt_cmd(struct urb *urb)
1040+{
1041+ struct usb_ctrlrequest *req;
1042+ int target_endp;
1043+ int target_dir;
1044+ int target_pipe;
1045+ int ret;
1046+
1047+ req = (struct usb_ctrlrequest *) urb->setup_packet;
1048+
1049+ /*
1050+ * The stalled endpoint is specified in the wIndex value. The endpoint
1051+ * of the urb is the target of this clear_halt request (i.e., control
1052+ * endpoint).
1053+ */
1054+ target_endp = le16_to_cpu(req->wIndex) & 0x000f;
1055+
1056+ /* the stalled endpoint direction is IN or OUT?. USB_DIR_IN is 0x80. */
1057+ target_dir = le16_to_cpu(req->wIndex) & 0x0080;
1058+
1059+ if (target_dir)
1060+ target_pipe = usb_rcvctrlpipe(urb->dev, target_endp);
1061+ else
1062+ target_pipe = usb_sndctrlpipe(urb->dev, target_endp);
1063+
1064+ ret = usb_clear_halt(urb->dev, target_pipe);
1065+ if (ret < 0)
1066+ uinfo("clear_halt error: devnum %d endp %d, %d\n",
1067+ urb->dev->devnum, target_endp, ret);
1068+ else
1069+ uinfo("clear_halt done: devnum %d endp %d\n",
1070+ urb->dev->devnum, target_endp);
1071+
1072+ return ret;
1073+}
1074+
1075+static int tweak_set_interface_cmd(struct urb *urb)
1076+{
1077+ struct usb_ctrlrequest *req;
1078+ __u16 alternate;
1079+ __u16 interface;
1080+ int ret;
1081+
1082+ req = (struct usb_ctrlrequest *) urb->setup_packet;
1083+ alternate = le16_to_cpu(req->wValue);
1084+ interface = le16_to_cpu(req->wIndex);
1085+
1086+ dbg_stub_rx("set_interface: inf %u alt %u\n", interface, alternate);
1087+
1088+ ret = usb_set_interface(urb->dev, interface, alternate);
1089+ if (ret < 0)
1090+ uinfo("set_interface error: inf %u alt %u, %d\n",
1091+ interface, alternate, ret);
1092+ else
1093+ uinfo("set_interface done: inf %u alt %u\n",
1094+ interface,
1095+ alternate);
1096+
1097+ return ret;
1098+}
1099+
1100+static int tweak_set_configuration_cmd(struct urb *urb)
1101+{
1102+ struct usb_ctrlrequest *req;
1103+ __u16 config;
1104+
1105+ req = (struct usb_ctrlrequest *) urb->setup_packet;
1106+ config = le16_to_cpu(req->wValue);
1107+
1108+ /*
1109+ * I have never seen a multi-config device. Very rare.
1110+ * For most devices, this will be called to choose a default
1111+ * configuration only once in an initialization phase.
1112+ *
1113+ * set_configuration may change a device configuration and its device
1114+ * drivers will be unbound and assigned for a new device configuration.
1115+ * This means this usbip driver will be also unbound when called, then
1116+ * eventually reassigned to the device as far as driver matching
1117+ * condition is kept.
1118+ *
1119+ * Unfortunatelly, an existing usbip connection will be dropped
1120+ * due to this driver unbinding. So, skip here.
1121+ * A user may need to set a special configuration value before
1122+ * exporting the device.
1123+ */
1124+ uinfo("set_configuration (%d) to %s\n", config, urb->dev->dev.bus_id);
1125+ uinfo("but, skip!\n");
1126+
1127+ return 0;
1128+ /* return usb_driver_set_configuration(urb->dev, config); */
1129+}
1130+
1131+static int tweak_reset_device_cmd(struct urb *urb)
1132+{
1133+ struct usb_ctrlrequest *req;
1134+ __u16 value;
1135+ __u16 index;
1136+ int ret;
1137+
1138+ req = (struct usb_ctrlrequest *) urb->setup_packet;
1139+ value = le16_to_cpu(req->wValue);
1140+ index = le16_to_cpu(req->wIndex);
1141+
1142+ uinfo("reset_device (port %d) to %s\n", index, urb->dev->dev.bus_id);
1143+
1144+ /* all interfaces should be owned by usbip driver, so just reset it. */
1145+ ret = usb_lock_device_for_reset(urb->dev, NULL);
1146+ if (ret < 0) {
1147+ dev_err(&urb->dev->dev, "lock for reset\n");
1148+ return ret;
1149+ }
1150+
1151+ /* try to reset the device */
1152+ ret = usb_reset_device(urb->dev);
1153+ if (ret < 0)
1154+ dev_err(&urb->dev->dev, "device reset\n");
1155+
1156+ usb_unlock_device(urb->dev);
1157+
1158+ return ret;
1159+}
1160+
1161+/*
1162+ * clear_halt, set_interface, and set_configuration require special tricks.
1163+ */
1164+static void tweak_special_requests(struct urb *urb)
1165+{
1166+ if (!urb || !urb->setup_packet)
1167+ return;
1168+
1169+ if (usb_pipetype(urb->pipe) != PIPE_CONTROL)
1170+ return;
1171+
1172+ if (is_clear_halt_cmd(urb))
1173+ /* tweak clear_halt */
1174+ tweak_clear_halt_cmd(urb);
1175+
1176+ else if (is_set_interface_cmd(urb))
1177+ /* tweak set_interface */
1178+ tweak_set_interface_cmd(urb);
1179+
1180+ else if (is_set_configuration_cmd(urb))
1181+ /* tweak set_configuration */
1182+ tweak_set_configuration_cmd(urb);
1183+
1184+ else if (is_reset_device_cmd(urb))
1185+ tweak_reset_device_cmd(urb);
1186+ else
1187+ dbg_stub_rx("no need to tweak\n");
1188+}
1189+
1190+/*
1191+ * stub_recv_unlink() unlinks the URB by a call to usb_unlink_urb().
1192+ * By unlinking the urb asynchronously, stub_rx can continuously
1193+ * process coming urbs. Even if the urb is unlinked, its completion
1194+ * handler will be called and stub_tx will send a return pdu.
1195+ *
1196+ * See also comments about unlinking strategy in vhci_hcd.c.
1197+ */
1198+static int stub_recv_cmd_unlink(struct stub_device *sdev,
1199+ struct usbip_header *pdu)
1200+{
1201+ struct list_head *listhead = &sdev->priv_init;
1202+ struct list_head *ptr;
1203+ unsigned long flags;
1204+
1205+ struct stub_priv *priv;
1206+
1207+
1208+ spin_lock_irqsave(&sdev->priv_lock, flags);
1209+
1210+ for (ptr = listhead->next; ptr != listhead; ptr = ptr->next) {
1211+ priv = list_entry(ptr, struct stub_priv, list);
1212+ if (priv->seqnum == pdu->u.cmd_unlink.seqnum) {
1213+ int ret;
1214+
1215+ dev_info(&priv->urb->dev->dev, "unlink urb %p\n",
1216+ priv->urb);
1217+
1218+ /*
1219+ * This matched urb is not completed yet (i.e., be in
1220+ * flight in usb hcd hardware/driver). Now we are
1221+ * cancelling it. The unlinking flag means that we are
1222+ * now not going to return the normal result pdu of a
1223+ * submission request, but going to return a result pdu
1224+ * of the unlink request.
1225+ */
1226+ priv->unlinking = 1;
1227+
1228+ /*
1229+ * In the case that unlinking flag is on, prev->seqnum
1230+ * is changed from the seqnum of the cancelling urb to
1231+ * the seqnum of the unlink request. This will be used
1232+ * to make the result pdu of the unlink request.
1233+ */
1234+ priv->seqnum = pdu->base.seqnum;
1235+
1236+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
1237+
1238+ /*
1239+ * usb_unlink_urb() is now out of spinlocking to avoid
1240+ * spinlock recursion since stub_complete() is
1241+ * sometimes called in this context but not in the
1242+ * interrupt context. If stub_complete() is executed
1243+ * before we call usb_unlink_urb(), usb_unlink_urb()
1244+ * will return an error value. In this case, stub_tx
1245+ * will return the result pdu of this unlink request
1246+ * though submission is completed and actual unlinking
1247+ * is not executed. OK?
1248+ */
1249+ /* In the above case, urb->status is not -ECONNRESET,
1250+ * so a driver in a client host will know the failure
1251+ * of the unlink request ?
1252+ */
1253+ ret = usb_unlink_urb(priv->urb);
1254+ if (ret != -EINPROGRESS)
1255+ dev_err(&priv->urb->dev->dev,
1256+ "failed to unlink a urb %p, ret %d\n",
1257+ priv->urb, ret);
1258+ return 0;
1259+ }
1260+ }
1261+
1262+ dbg_stub_rx("seqnum %d is not pending\n", pdu->u.cmd_unlink.seqnum);
1263+
1264+ /*
1265+ * The urb of the unlink target is not found in priv_init queue. It was
1266+ * already completed and its results is/was going to be sent by a
1267+ * CMD_RET pdu. In this case, usb_unlink_urb() is not needed. We only
1268+ * return the completeness of this unlink request to vhci_hcd.
1269+ */
1270+ stub_enqueue_ret_unlink(sdev, pdu->base.seqnum, 0);
1271+
1272+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
1273+
1274+
1275+ return 0;
1276+}
1277+
1278+static int valid_request(struct stub_device *sdev, struct usbip_header *pdu)
1279+{
1280+ struct usbip_device *ud = &sdev->ud;
1281+
1282+ if (pdu->base.devid == sdev->devid) {
1283+ spin_lock(&ud->lock);
1284+ if (ud->status == SDEV_ST_USED) {
1285+ /* A request is valid. */
1286+ spin_unlock(&ud->lock);
1287+ return 1;
1288+ }
1289+ spin_unlock(&ud->lock);
1290+ }
1291+
1292+ return 0;
1293+}
1294+
1295+static struct stub_priv *stub_priv_alloc(struct stub_device *sdev,
1296+ struct usbip_header *pdu)
1297+{
1298+ struct stub_priv *priv;
1299+ struct usbip_device *ud = &sdev->ud;
1300+ unsigned long flags;
1301+
1302+ spin_lock_irqsave(&sdev->priv_lock, flags);
1303+
1304+ priv = kmem_cache_alloc(stub_priv_cache, GFP_ATOMIC);
1305+ if (!priv) {
1306+ dev_err(&sdev->interface->dev, "alloc stub_priv\n");
1307+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
1308+ usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1309+ return NULL;
1310+ }
1311+
1312+ memset(priv, 0, sizeof(struct stub_priv));
1313+
1314+ priv->seqnum = pdu->base.seqnum;
1315+ priv->sdev = sdev;
1316+
1317+ /*
1318+ * After a stub_priv is linked to a list_head,
1319+ * our error handler can free allocated data.
1320+ */
1321+ list_add_tail(&priv->list, &sdev->priv_init);
1322+
1323+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
1324+
1325+ return priv;
1326+}
1327+
1328+
1329+static struct usb_host_endpoint *get_ep_from_epnum(struct usb_device *udev,
1330+ int epnum0)
1331+{
1332+ struct usb_host_config *config;
1333+ int i = 0, j = 0;
1334+ struct usb_host_endpoint *ep = NULL;
1335+ int epnum;
1336+ int found = 0;
1337+
1338+ if (epnum0 == 0)
1339+ return &udev->ep0;
1340+
1341+ config = udev->actconfig;
1342+ if (!config)
1343+ return NULL;
1344+
1345+ for (i = 0; i < config->desc.bNumInterfaces; i++) {
1346+ struct usb_host_interface *setting;
1347+
1348+ setting = config->interface[i]->cur_altsetting;
1349+
1350+ for (j = 0; j < setting->desc.bNumEndpoints; j++) {
1351+ ep = &setting->endpoint[j];
1352+ epnum = (ep->desc.bEndpointAddress & 0x7f);
1353+
1354+ if (epnum == epnum0) {
1355+ /* uinfo("found epnum %d\n", epnum0); */
1356+ found = 1;
1357+ break;
1358+ }
1359+ }
1360+ }
1361+
1362+ if (found)
1363+ return ep;
1364+ else
1365+ return NULL;
1366+}
1367+
1368+
1369+static int get_pipe(struct stub_device *sdev, int epnum, int dir)
1370+{
1371+ struct usb_device *udev = interface_to_usbdev(sdev->interface);
1372+ struct usb_host_endpoint *ep;
1373+ struct usb_endpoint_descriptor *epd = NULL;
1374+
1375+ ep = get_ep_from_epnum(udev, epnum);
1376+ if (!ep) {
1377+ dev_err(&sdev->interface->dev, "no such endpoint?, %d\n",
1378+ epnum);
1379+ BUG();
1380+ }
1381+
1382+ epd = &ep->desc;
1383+
1384+
1385+#if 0
1386+ /* epnum 0 is always control */
1387+ if (epnum == 0) {
1388+ if (dir == USBIP_DIR_OUT)
1389+ return usb_sndctrlpipe(udev, 0);
1390+ else
1391+ return usb_rcvctrlpipe(udev, 0);
1392+ }
1393+#endif
1394+
1395+ if (usb_endpoint_xfer_control(epd)) {
1396+ if (dir == USBIP_DIR_OUT)
1397+ return usb_sndctrlpipe(udev, epnum);
1398+ else
1399+ return usb_rcvctrlpipe(udev, epnum);
1400+ }
1401+
1402+ if (usb_endpoint_xfer_bulk(epd)) {
1403+ if (dir == USBIP_DIR_OUT)
1404+ return usb_sndbulkpipe(udev, epnum);
1405+ else
1406+ return usb_rcvbulkpipe(udev, epnum);
1407+ }
1408+
1409+ if (usb_endpoint_xfer_int(epd)) {
1410+ if (dir == USBIP_DIR_OUT)
1411+ return usb_sndintpipe(udev, epnum);
1412+ else
1413+ return usb_rcvintpipe(udev, epnum);
1414+ }
1415+
1416+ if (usb_endpoint_xfer_isoc(epd)) {
1417+ if (dir == USBIP_DIR_OUT)
1418+ return usb_sndisocpipe(udev, epnum);
1419+ else
1420+ return usb_rcvisocpipe(udev, epnum);
1421+ }
1422+
1423+ /* NOT REACHED */
1424+ dev_err(&sdev->interface->dev, "get pipe, epnum %d\n", epnum);
1425+ return 0;
1426+}
1427+
1428+static void stub_recv_cmd_submit(struct stub_device *sdev,
1429+ struct usbip_header *pdu)
1430+{
1431+ int ret;
1432+ struct stub_priv *priv;
1433+ struct usbip_device *ud = &sdev->ud;
1434+ struct usb_device *udev = interface_to_usbdev(sdev->interface);
1435+ int pipe = get_pipe(sdev, pdu->base.ep, pdu->base.direction);
1436+
1437+
1438+ priv = stub_priv_alloc(sdev, pdu);
1439+ if (!priv)
1440+ return;
1441+
1442+ /* setup a urb */
1443+ if (usb_pipeisoc(pipe))
1444+ priv->urb = usb_alloc_urb(pdu->u.cmd_submit.number_of_packets,
1445+ GFP_KERNEL);
1446+ else
1447+ priv->urb = usb_alloc_urb(0, GFP_KERNEL);
1448+
1449+ if (!priv->urb) {
1450+ dev_err(&sdev->interface->dev, "malloc urb\n");
1451+ usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1452+ return;
1453+ }
1454+
1455+ /* set priv->urb->transfer_buffer */
1456+ if (pdu->u.cmd_submit.transfer_buffer_length > 0) {
1457+ priv->urb->transfer_buffer =
1458+ kzalloc(pdu->u.cmd_submit.transfer_buffer_length,
1459+ GFP_KERNEL);
1460+ if (!priv->urb->transfer_buffer) {
1461+ dev_err(&sdev->interface->dev, "malloc x_buff\n");
1462+ usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1463+ return;
1464+ }
1465+ }
1466+
1467+ /* set priv->urb->setup_packet */
1468+ priv->urb->setup_packet = kzalloc(8, GFP_KERNEL);
1469+ if (!priv->urb->setup_packet) {
1470+ dev_err(&sdev->interface->dev, "allocate setup_packet\n");
1471+ usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1472+ return;
1473+ }
1474+ memcpy(priv->urb->setup_packet, &pdu->u.cmd_submit.setup, 8);
1475+
1476+ /* set other members from the base header of pdu */
1477+ priv->urb->context = (void *) priv;
1478+ priv->urb->dev = udev;
1479+ priv->urb->pipe = pipe;
1480+ priv->urb->complete = stub_complete;
1481+
1482+ usbip_pack_pdu(pdu, priv->urb, USBIP_CMD_SUBMIT, 0);
1483+
1484+
1485+ if (usbip_recv_xbuff(ud, priv->urb) < 0)
1486+ return;
1487+
1488+ if (usbip_recv_iso(ud, priv->urb) < 0)
1489+ return;
1490+
1491+ /* no need to submit an intercepted request, but harmless? */
1492+ tweak_special_requests(priv->urb);
1493+
1494+ /* urb is now ready to submit */
1495+ ret = usb_submit_urb(priv->urb, GFP_KERNEL);
1496+
1497+ if (ret == 0)
1498+ dbg_stub_rx("submit urb ok, seqnum %u\n", pdu->base.seqnum);
1499+ else {
1500+ dev_err(&sdev->interface->dev, "submit_urb error, %d\n", ret);
1501+ usbip_dump_header(pdu);
1502+ usbip_dump_urb(priv->urb);
1503+
1504+ /*
1505+ * Pessimistic.
1506+ * This connection will be discarded.
1507+ */
1508+ usbip_event_add(ud, SDEV_EVENT_ERROR_SUBMIT);
1509+ }
1510+
1511+ dbg_stub_rx("Leave\n");
1512+ return;
1513+}
1514+
1515+/* recv a pdu */
1516+static void stub_rx_pdu(struct usbip_device *ud)
1517+{
1518+ int ret;
1519+ struct usbip_header pdu;
1520+ struct stub_device *sdev = container_of(ud, struct stub_device, ud);
1521+ struct device *dev = &sdev->interface->dev;
1522+
1523+ dbg_stub_rx("Enter\n");
1524+
1525+ memset(&pdu, 0, sizeof(pdu));
1526+
1527+ /* 1. receive a pdu header */
1528+ ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
1529+ if (ret != sizeof(pdu)) {
1530+ dev_err(dev, "recv a header, %d\n", ret);
1531+ usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
1532+ return;
1533+ }
1534+
1535+ usbip_header_correct_endian(&pdu, 0);
1536+
1537+ if (dbg_flag_stub_rx)
1538+ usbip_dump_header(&pdu);
1539+
1540+ if (!valid_request(sdev, &pdu)) {
1541+ dev_err(dev, "recv invalid request\n");
1542+ usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
1543+ return;
1544+ }
1545+
1546+ switch (pdu.base.command) {
1547+ case USBIP_CMD_UNLINK:
1548+ stub_recv_cmd_unlink(sdev, &pdu);
1549+ break;
1550+
1551+ case USBIP_CMD_SUBMIT:
1552+ stub_recv_cmd_submit(sdev, &pdu);
1553+ break;
1554+
1555+ default:
1556+ /* NOTREACHED */
1557+ dev_err(dev, "unknown pdu\n");
1558+ usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
1559+ return;
1560+ }
1561+
1562+}
1563+
1564+void stub_rx_loop(struct usbip_task *ut)
1565+{
1566+ struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_rx);
1567+
1568+ while (1) {
1569+ if (signal_pending(current)) {
1570+ dbg_stub_rx("signal caught!\n");
1571+ break;
1572+ }
1573+
1574+ if (usbip_event_happend(ud))
1575+ break;
1576+
1577+ stub_rx_pdu(ud);
1578+ }
1579+}
1580diff --git a/drivers/staging/usbip/stub_tx.c b/drivers/staging/usbip/stub_tx.c
1581new file mode 100644
1582index 0000000..d5563cd
1583--- /dev/null
1584+++ b/drivers/staging/usbip/stub_tx.c
1585@@ -0,0 +1,371 @@
1586+/*
1587+ * Copyright (C) 2003-2008 Takahiro Hirofuchi
1588+ *
1589+ * This is free software; you can redistribute it and/or modify
1590+ * it under the terms of the GNU General Public License as published by
1591+ * the Free Software Foundation; either version 2 of the License, or
1592+ * (at your option) any later version.
1593+ *
1594+ * This is distributed in the hope that it will be useful,
1595+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
1596+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1597+ * GNU General Public License for more details.
1598+ *
1599+ * You should have received a copy of the GNU General Public License
1600+ * along with this program; if not, write to the Free Software
1601+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1602+ * USA.
1603+ */
1604+
1605+#include "usbip_common.h"
1606+#include "stub.h"
1607+
1608+
1609+static void stub_free_priv_and_urb(struct stub_priv *priv)
1610+{
1611+ struct urb *urb = priv->urb;
1612+
1613+ kfree(urb->setup_packet);
1614+ kfree(urb->transfer_buffer);
1615+ list_del(&priv->list);
1616+ kmem_cache_free(stub_priv_cache, priv);
1617+ usb_free_urb(urb);
1618+}
1619+
1620+/* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
1621+void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
1622+ __u32 status)
1623+{
1624+ struct stub_unlink *unlink;
1625+
1626+ unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC);
1627+ if (!unlink) {
1628+ dev_err(&sdev->interface->dev, "alloc stub_unlink\n");
1629+ usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
1630+ return;
1631+ }
1632+
1633+ unlink->seqnum = seqnum;
1634+ unlink->status = status;
1635+
1636+ list_add_tail(&unlink->list, &sdev->unlink_tx);
1637+}
1638+
1639+/**
1640+ * stub_complete - completion handler of a usbip urb
1641+ * @urb: pointer to the urb completed
1642+ * @regs:
1643+ *
1644+ * When a urb has completed, the USB core driver calls this function mostly in
1645+ * the interrupt context. To return the result of a urb, the completed urb is
1646+ * linked to the pending list of returning.
1647+ *
1648+ */
1649+void stub_complete(struct urb *urb)
1650+{
1651+ struct stub_priv *priv = (struct stub_priv *) urb->context;
1652+ struct stub_device *sdev = priv->sdev;
1653+ unsigned long flags;
1654+
1655+ dbg_stub_tx("complete! status %d\n", urb->status);
1656+
1657+
1658+ switch (urb->status) {
1659+ case 0:
1660+ /* OK */
1661+ break;
1662+ case -ENOENT:
1663+ uinfo("stopped by a call of usb_kill_urb() because of"
1664+ "cleaning up a virtual connection\n");
1665+ return;
1666+ case -ECONNRESET:
1667+ uinfo("unlinked by a call of usb_unlink_urb()\n");
1668+ break;
1669+ case -EPIPE:
1670+ uinfo("endpoint %d is stalled\n", usb_pipeendpoint(urb->pipe));
1671+ break;
1672+ case -ESHUTDOWN:
1673+ uinfo("device removed?\n");
1674+ break;
1675+ default:
1676+ uinfo("urb completion with non-zero status %d\n", urb->status);
1677+ }
1678+
1679+ /* link a urb to the queue of tx. */
1680+ spin_lock_irqsave(&sdev->priv_lock, flags);
1681+
1682+ if (priv->unlinking) {
1683+ stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
1684+ stub_free_priv_and_urb(priv);
1685+ } else
1686+ list_move_tail(&priv->list, &sdev->priv_tx);
1687+
1688+
1689+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
1690+
1691+ /* wake up tx_thread */
1692+ wake_up(&sdev->tx_waitq);
1693+}
1694+
1695+
1696+/*-------------------------------------------------------------------------*/
1697+/* fill PDU */
1698+
1699+static inline void setup_base_pdu(struct usbip_header_basic *base,
1700+ __u32 command, __u32 seqnum)
1701+{
1702+ base->command = command;
1703+ base->seqnum = seqnum;
1704+ base->devid = 0;
1705+ base->ep = 0;
1706+ base->direction = 0;
1707+}
1708+
1709+static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
1710+{
1711+ struct stub_priv *priv = (struct stub_priv *) urb->context;
1712+
1713+ setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
1714+
1715+ usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
1716+}
1717+
1718+static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
1719+ struct stub_unlink *unlink)
1720+{
1721+ setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
1722+
1723+ rpdu->u.ret_unlink.status = unlink->status;
1724+}
1725+
1726+
1727+/*-------------------------------------------------------------------------*/
1728+/* send RET_SUBMIT */
1729+
1730+static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
1731+{
1732+ unsigned long flags;
1733+ struct stub_priv *priv, *tmp;
1734+
1735+ spin_lock_irqsave(&sdev->priv_lock, flags);
1736+
1737+ list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
1738+ list_move_tail(&priv->list, &sdev->priv_free);
1739+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
1740+ return priv;
1741+ }
1742+
1743+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
1744+
1745+ return NULL;
1746+}
1747+
1748+static int stub_send_ret_submit(struct stub_device *sdev)
1749+{
1750+ unsigned long flags;
1751+ struct stub_priv *priv, *tmp;
1752+
1753+ struct msghdr msg;
1754+ struct kvec iov[3];
1755+ size_t txsize;
1756+
1757+ size_t total_size = 0;
1758+
1759+ while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
1760+ int ret;
1761+ struct urb *urb = priv->urb;
1762+ struct usbip_header pdu_header;
1763+ void *iso_buffer = NULL;
1764+
1765+ txsize = 0;
1766+ memset(&pdu_header, 0, sizeof(pdu_header));
1767+ memset(&msg, 0, sizeof(msg));
1768+ memset(&iov, 0, sizeof(iov));
1769+
1770+ dbg_stub_tx("setup txdata urb %p\n", urb);
1771+
1772+
1773+ /* 1. setup usbip_header */
1774+ setup_ret_submit_pdu(&pdu_header, urb);
1775+ usbip_header_correct_endian(&pdu_header, 1);
1776+
1777+ iov[0].iov_base = &pdu_header;
1778+ iov[0].iov_len = sizeof(pdu_header);
1779+ txsize += sizeof(pdu_header);
1780+
1781+ /* 2. setup transfer buffer */
1782+ if (usb_pipein(urb->pipe) && urb->actual_length > 0) {
1783+ iov[1].iov_base = urb->transfer_buffer;
1784+ iov[1].iov_len = urb->actual_length;
1785+ txsize += urb->actual_length;
1786+ }
1787+
1788+ /* 3. setup iso_packet_descriptor */
1789+ if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1790+ ssize_t len = 0;
1791+
1792+ iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
1793+ if (!iso_buffer) {
1794+ usbip_event_add(&sdev->ud,
1795+ SDEV_EVENT_ERROR_MALLOC);
1796+ return -1;
1797+ }
1798+
1799+ iov[2].iov_base = iso_buffer;
1800+ iov[2].iov_len = len;
1801+ txsize += len;
1802+ }
1803+
1804+ ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
1805+ 3, txsize);
1806+ if (ret != txsize) {
1807+ dev_err(&sdev->interface->dev,
1808+ "sendmsg failed!, retval %d for %zd\n",
1809+ ret, txsize);
1810+ kfree(iso_buffer);
1811+ usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
1812+ return -1;
1813+ }
1814+
1815+ kfree(iso_buffer);
1816+ dbg_stub_tx("send txdata\n");
1817+
1818+ total_size += txsize;
1819+ }
1820+
1821+
1822+ spin_lock_irqsave(&sdev->priv_lock, flags);
1823+
1824+ list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
1825+ stub_free_priv_and_urb(priv);
1826+ }
1827+
1828+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
1829+
1830+ return total_size;
1831+}
1832+
1833+
1834+/*-------------------------------------------------------------------------*/
1835+/* send RET_UNLINK */
1836+
1837+static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
1838+{
1839+ unsigned long flags;
1840+ struct stub_unlink *unlink, *tmp;
1841+
1842+ spin_lock_irqsave(&sdev->priv_lock, flags);
1843+
1844+ list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
1845+ list_move_tail(&unlink->list, &sdev->unlink_free);
1846+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
1847+ return unlink;
1848+ }
1849+
1850+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
1851+
1852+ return NULL;
1853+}
1854+
1855+
1856+static int stub_send_ret_unlink(struct stub_device *sdev)
1857+{
1858+ unsigned long flags;
1859+ struct stub_unlink *unlink, *tmp;
1860+
1861+ struct msghdr msg;
1862+ struct kvec iov[1];
1863+ size_t txsize;
1864+
1865+ size_t total_size = 0;
1866+
1867+ while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
1868+ int ret;
1869+ struct usbip_header pdu_header;
1870+
1871+ txsize = 0;
1872+ memset(&pdu_header, 0, sizeof(pdu_header));
1873+ memset(&msg, 0, sizeof(msg));
1874+ memset(&iov, 0, sizeof(iov));
1875+
1876+ dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
1877+
1878+ /* 1. setup usbip_header */
1879+ setup_ret_unlink_pdu(&pdu_header, unlink);
1880+ usbip_header_correct_endian(&pdu_header, 1);
1881+
1882+ iov[0].iov_base = &pdu_header;
1883+ iov[0].iov_len = sizeof(pdu_header);
1884+ txsize += sizeof(pdu_header);
1885+
1886+ ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
1887+ 1, txsize);
1888+ if (ret != txsize) {
1889+ dev_err(&sdev->interface->dev,
1890+ "sendmsg failed!, retval %d for %zd\n",
1891+ ret, txsize);
1892+ usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
1893+ return -1;
1894+ }
1895+
1896+
1897+ dbg_stub_tx("send txdata\n");
1898+
1899+ total_size += txsize;
1900+ }
1901+
1902+
1903+ spin_lock_irqsave(&sdev->priv_lock, flags);
1904+
1905+ list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
1906+ list_del(&unlink->list);
1907+ kfree(unlink);
1908+ }
1909+
1910+ spin_unlock_irqrestore(&sdev->priv_lock, flags);
1911+
1912+ return total_size;
1913+}
1914+
1915+
1916+/*-------------------------------------------------------------------------*/
1917+
1918+void stub_tx_loop(struct usbip_task *ut)
1919+{
1920+ struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_tx);
1921+ struct stub_device *sdev = container_of(ud, struct stub_device, ud);
1922+
1923+ while (1) {
1924+ if (signal_pending(current)) {
1925+ dbg_stub_tx("signal catched\n");
1926+ break;
1927+ }
1928+
1929+ if (usbip_event_happend(ud))
1930+ break;
1931+
1932+ /*
1933+ * send_ret_submit comes earlier than send_ret_unlink. stub_rx
1934+ * looks at only priv_init queue. If the completion of a URB is
1935+ * earlier than the receive of CMD_UNLINK, priv is moved to
1936+ * priv_tx queue and stub_rx does not find the target priv. In
1937+ * this case, vhci_rx receives the result of the submit request
1938+ * and then receives the result of the unlink request. The
1939+ * result of the submit is given back to the usbcore as the
1940+ * completion of the unlink request. The request of the
1941+ * unlink is ignored. This is ok because a driver who calls
1942+ * usb_unlink_urb() understands the unlink was too late by
1943+ * getting the status of the given-backed URB which has the
1944+ * status of usb_submit_urb().
1945+ */
1946+ if (stub_send_ret_submit(sdev) < 0)
1947+ break;
1948+
1949+ if (stub_send_ret_unlink(sdev) < 0)
1950+ break;
1951+
1952+ wait_event_interruptible(sdev->tx_waitq,
1953+ (!list_empty(&sdev->priv_tx) ||
1954+ !list_empty(&sdev->unlink_tx)));
1955+ }
1956+}
1957--
19581.6.0.2
1959