]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/patches/suse-2.6.27.25/patches.drivers/0011-Staging-USB-IP-add-common-functions-needed.patch
Revert "Move xen patchset to new version's subdir."
[people/pmueller/ipfire-2.x.git] / src / patches / suse-2.6.27.25 / patches.drivers / 0011-Staging-USB-IP-add-common-functions-needed.patch
CommitLineData
00e5a55c
BS
1From 05a1f28e879e3b4d6a9c08e30b1898943f77b6e7 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 11/23] Staging: USB/IP: add common functions needed
5Patch-mainline: 2.6.28
6
7This adds the common functions needed by both the host and client side
8of the USB/IP code.
9
10Brian Merrell cleaned up a lot of this code and submitted it for
11inclusion. Greg also did a lot of cleanup.
12
13Signed-off-by: Brian G. Merrell <bgmerrell@novell.com>
14Cc: Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>
15Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
16---
17 drivers/staging/Kconfig | 2 +
18 drivers/staging/Makefile | 1 +
19 drivers/staging/usbip/Kconfig | 14 +
20 drivers/staging/usbip/Makefile | 6 +
21 drivers/staging/usbip/README | 6 +
22 drivers/staging/usbip/usbip_common.c | 997 ++++++++++++++++++++++++++++++++++
23 drivers/staging/usbip/usbip_common.h | 406 ++++++++++++++
24 drivers/staging/usbip/usbip_event.c | 141 +++++
25 8 files changed, 1573 insertions(+), 0 deletions(-)
26 create mode 100644 drivers/staging/usbip/Kconfig
27 create mode 100644 drivers/staging/usbip/Makefile
28 create mode 100644 drivers/staging/usbip/README
29 create mode 100644 drivers/staging/usbip/usbip_common.c
30 create mode 100644 drivers/staging/usbip/usbip_common.h
31 create mode 100644 drivers/staging/usbip/usbip_event.c
32
33diff --git a/drivers/staging/Kconfig b/drivers/staging/Kconfig
34index f16bc9c..4dbf795 100644
35--- a/drivers/staging/Kconfig
36+++ b/drivers/staging/Kconfig
37@@ -33,4 +33,6 @@ source "drivers/staging/me4000/Kconfig"
38
39 source "drivers/staging/go7007/Kconfig"
40
41+source "drivers/staging/usbip/Kconfig"
42+
43 endif # STAGING
44diff --git a/drivers/staging/Makefile b/drivers/staging/Makefile
45index aa61662..be42c0d 100644
46--- a/drivers/staging/Makefile
47+++ b/drivers/staging/Makefile
48@@ -5,3 +5,4 @@ obj-$(CONFIG_SLICOSS) += slicoss/
49 obj-$(CONFIG_SXG) += sxg/
50 obj-$(CONFIG_ME4000) += me4000/
51 obj-$(CONFIG_VIDEO_GO7007) += go7007/
52+obj-$(CONFIG_USB_IP_COMMON) += usbip/
53diff --git a/drivers/staging/usbip/Kconfig b/drivers/staging/usbip/Kconfig
54new file mode 100644
55index 0000000..37efb5e
56--- /dev/null
57+++ b/drivers/staging/usbip/Kconfig
58@@ -0,0 +1,14 @@
59+config USB_IP_COMMON
60+ tristate "USB IP support (EXPERIMENTAL)"
61+ depends on USB && EXPERIMENTAL
62+ default N
63+ ---help---
64+ This enables pushing USB packets over IP to allow remote
65+ machines access to USB devices directly. For more details,
66+ and links to the userspace utility programs to let this work
67+ properly, see http://usbip.naist.jp/
68+
69+ To compile this driver as a module, choose M here: the
70+ module will be called usbip_common_mod.
71+
72+ If unsure, say N.
73diff --git a/drivers/staging/usbip/Makefile b/drivers/staging/usbip/Makefile
74new file mode 100644
75index 0000000..ce925ca
76--- /dev/null
77+++ b/drivers/staging/usbip/Makefile
78@@ -0,0 +1,6 @@
79+obj-$(CONFIG_USB_IP_COMMON) += usbip_common_mod.o
80+usbip_common_mod-objs := usbip_common.o usbip_event.o
81+
82+ifeq ($(CONFIG_USB_DEBUG),y)
83+ EXTRA_CFLAGS += -DDEBUG
84+endif
85diff --git a/drivers/staging/usbip/README b/drivers/staging/usbip/README
86new file mode 100644
87index 0000000..c11be57
88--- /dev/null
89+++ b/drivers/staging/usbip/README
90@@ -0,0 +1,6 @@
91+TODO:
92+ - more discussion about the protocol
93+ - testing
94+ - review of the userspace interface
95+
96+Please send patches for this code to Greg Kroah-Hartman <greg@kroah.com>
97diff --git a/drivers/staging/usbip/usbip_common.c b/drivers/staging/usbip/usbip_common.c
98new file mode 100644
99index 0000000..e64918f
100--- /dev/null
101+++ b/drivers/staging/usbip/usbip_common.c
102@@ -0,0 +1,997 @@
103+/*
104+ * Copyright (C) 2003-2008 Takahiro Hirofuchi
105+ *
106+ * This is free software; you can redistribute it and/or modify
107+ * it under the terms of the GNU General Public License as published by
108+ * the Free Software Foundation; either version 2 of the License, or
109+ * (at your option) any later version.
110+ *
111+ * This is distributed in the hope that it will be useful,
112+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
113+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
114+ * GNU General Public License for more details.
115+ *
116+ * You should have received a copy of the GNU General Public License
117+ * along with this program; if not, write to the Free Software
118+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
119+ * USA.
120+ */
121+
122+#include <linux/kernel.h>
123+#include <linux/file.h>
124+#include <linux/tcp.h>
125+#include <linux/in.h>
126+#include "usbip_common.h"
127+
128+/* version information */
129+#define DRIVER_VERSION "1.0"
130+#define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi _at_ users.sourceforge.net>"
131+#define DRIVER_DESC "usbip common driver"
132+
133+/*-------------------------------------------------------------------------*/
134+/* debug routines */
135+
136+#ifdef CONFIG_USB_DEBUG
137+unsigned long usbip_debug_flag = 0xffffffff;
138+#else
139+unsigned long usbip_debug_flag;
140+#endif
141+EXPORT_SYMBOL_GPL(usbip_debug_flag);
142+
143+
144+/* FIXME */
145+struct device_attribute dev_attr_usbip_debug;
146+EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
147+
148+
149+static ssize_t show_flag(struct device *dev, struct device_attribute *attr,
150+ char *buf)
151+{
152+ return sprintf(buf, "%lx\n", usbip_debug_flag);
153+}
154+
155+static ssize_t store_flag(struct device *dev, struct device_attribute *attr,
156+ const char *buf, size_t count)
157+{
158+ unsigned long flag;
159+
160+ sscanf(buf, "%lx", &flag);
161+ usbip_debug_flag = flag;
162+
163+ return count;
164+}
165+DEVICE_ATTR(usbip_debug, (S_IRUGO | S_IWUSR), show_flag, store_flag);
166+
167+static void usbip_dump_buffer(char *buff, int bufflen)
168+{
169+ int i;
170+
171+ if (bufflen > 128) {
172+ for (i = 0; i < 128; i++) {
173+ if (i%24 == 0)
174+ printk(" ");
175+ printk("%02x ", (unsigned char) buff[i]);
176+ if (i%4 == 3)
177+ printk("| ");
178+ if (i%24 == 23)
179+ printk("\n");
180+ }
181+ printk("... (%d byte)\n", bufflen);
182+ return;
183+ }
184+
185+ for (i = 0; i < bufflen; i++) {
186+ if (i%24 == 0)
187+ printk(" ");
188+ printk("%02x ", (unsigned char) buff[i]);
189+ if (i%4 == 3)
190+ printk("| ");
191+ if (i%24 == 23)
192+ printk("\n");
193+ }
194+ printk("\n");
195+
196+}
197+
198+static void usbip_dump_pipe(unsigned int p)
199+{
200+ unsigned char type = usb_pipetype(p);
201+ unsigned char ep = usb_pipeendpoint(p);
202+ unsigned char dev = usb_pipedevice(p);
203+ unsigned char dir = usb_pipein(p);
204+
205+ printk("dev(%d) ", dev);
206+ printk("ep(%d) ", ep);
207+ printk("%s ", dir ? "IN" : "OUT");
208+
209+ switch (type) {
210+ case PIPE_ISOCHRONOUS:
211+ printk("%s ", "ISO");
212+ break;
213+ case PIPE_INTERRUPT:
214+ printk("%s ", "INT");
215+ break;
216+ case PIPE_CONTROL:
217+ printk("%s ", "CTL");
218+ break;
219+ case PIPE_BULK:
220+ printk("%s ", "BLK");
221+ break;
222+ default:
223+ printk("ERR");
224+ }
225+
226+ printk("\n");
227+
228+}
229+
230+static void usbip_dump_usb_device(struct usb_device *udev)
231+{
232+ struct device *dev = &udev->dev;
233+ int i;
234+
235+ dev_dbg(dev, " devnum(%d) devpath(%s)",
236+ udev->devnum, udev->devpath);
237+
238+ switch (udev->speed) {
239+ case USB_SPEED_HIGH:
240+ printk(" SPD_HIGH");
241+ break;
242+ case USB_SPEED_FULL:
243+ printk(" SPD_FULL");
244+ break;
245+ case USB_SPEED_LOW:
246+ printk(" SPD_LOW");
247+ break;
248+ case USB_SPEED_UNKNOWN:
249+ printk(" SPD_UNKNOWN");
250+ break;
251+ default:
252+ printk(" SPD_ERROR");
253+ }
254+
255+ printk(" tt %p, ttport %d", udev->tt, udev->ttport);
256+ printk("\n");
257+
258+ dev_dbg(dev, " ");
259+ for (i = 0; i < 16; i++)
260+ printk(" %2u", i);
261+ printk("\n");
262+
263+ dev_dbg(dev, " toggle0(IN) :");
264+ for (i = 0; i < 16; i++)
265+ printk(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
266+ printk("\n");
267+
268+ dev_dbg(dev, " toggle1(OUT):");
269+ for (i = 0; i < 16; i++)
270+ printk(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
271+ printk("\n");
272+
273+
274+ dev_dbg(dev, " epmaxp_in :");
275+ for (i = 0; i < 16; i++) {
276+ if (udev->ep_in[i])
277+ printk(" %2u",
278+ le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
279+ }
280+ printk("\n");
281+
282+ dev_dbg(dev, " epmaxp_out :");
283+ for (i = 0; i < 16; i++) {
284+ if (udev->ep_out[i])
285+ printk(" %2u",
286+ le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
287+ }
288+ printk("\n");
289+
290+ dev_dbg(dev, "parent %p, bus %p\n", udev->parent, udev->bus);
291+
292+ dev_dbg(dev, "descriptor %p, config %p, actconfig %p, "
293+ "rawdescriptors %p\n", &udev->descriptor, udev->config,
294+ udev->actconfig, udev->rawdescriptors);
295+
296+ dev_dbg(dev, "have_langid %d, string_langid %d\n",
297+ udev->have_langid, udev->string_langid);
298+
299+ dev_dbg(dev, "maxchild %d, children %p\n",
300+ udev->maxchild, udev->children);
301+}
302+
303+static void usbip_dump_request_type(__u8 rt)
304+{
305+ switch (rt & USB_RECIP_MASK) {
306+ case USB_RECIP_DEVICE:
307+ printk("DEVICE");
308+ break;
309+ case USB_RECIP_INTERFACE:
310+ printk("INTERF");
311+ break;
312+ case USB_RECIP_ENDPOINT:
313+ printk("ENDPOI");
314+ break;
315+ case USB_RECIP_OTHER:
316+ printk("OTHER ");
317+ break;
318+ default:
319+ printk("------");
320+ }
321+}
322+
323+static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
324+{
325+ if (!cmd) {
326+ printk(" %s : null pointer\n", __FUNCTION__);
327+ return;
328+ }
329+
330+ printk(" ");
331+ printk("bRequestType(%02X) ", cmd->bRequestType);
332+ printk("bRequest(%02X) " , cmd->bRequest);
333+ printk("wValue(%04X) ", cmd->wValue);
334+ printk("wIndex(%04X) ", cmd->wIndex);
335+ printk("wLength(%04X) ", cmd->wLength);
336+
337+ printk("\n ");
338+
339+ if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
340+ printk("STANDARD ");
341+ switch (cmd->bRequest) {
342+ case USB_REQ_GET_STATUS:
343+ printk("GET_STATUS");
344+ break;
345+ case USB_REQ_CLEAR_FEATURE:
346+ printk("CLEAR_FEAT");
347+ break;
348+ case USB_REQ_SET_FEATURE:
349+ printk("SET_FEAT ");
350+ break;
351+ case USB_REQ_SET_ADDRESS:
352+ printk("SET_ADDRRS");
353+ break;
354+ case USB_REQ_GET_DESCRIPTOR:
355+ printk("GET_DESCRI");
356+ break;
357+ case USB_REQ_SET_DESCRIPTOR:
358+ printk("SET_DESCRI");
359+ break;
360+ case USB_REQ_GET_CONFIGURATION:
361+ printk("GET_CONFIG");
362+ break;
363+ case USB_REQ_SET_CONFIGURATION:
364+ printk("SET_CONFIG");
365+ break;
366+ case USB_REQ_GET_INTERFACE:
367+ printk("GET_INTERF");
368+ break;
369+ case USB_REQ_SET_INTERFACE:
370+ printk("SET_INTERF");
371+ break;
372+ case USB_REQ_SYNCH_FRAME:
373+ printk("SYNC_FRAME");
374+ break;
375+ default:
376+ printk("REQ(%02X) ", cmd->bRequest);
377+ }
378+
379+ printk(" ");
380+ usbip_dump_request_type(cmd->bRequestType);
381+
382+ } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
383+ printk("CLASS ");
384+
385+ else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR)
386+ printk("VENDOR ");
387+
388+ else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED)
389+ printk("RESERVED");
390+
391+ printk("\n");
392+}
393+
394+void usbip_dump_urb(struct urb *urb)
395+{
396+ struct device *dev;
397+
398+ if (!urb) {
399+ printk(KERN_DEBUG KBUILD_MODNAME
400+ ":%s: urb: null pointer!!\n", __func__);
401+ return;
402+ }
403+
404+ if (!urb->dev) {
405+ printk(KERN_DEBUG KBUILD_MODNAME
406+ ":%s: urb->dev: null pointer!!\n", __func__);
407+ return;
408+ }
409+ dev = &urb->dev->dev;
410+
411+ dev_dbg(dev, " urb :%p\n", urb);
412+ dev_dbg(dev, " dev :%p\n", urb->dev);
413+
414+ usbip_dump_usb_device(urb->dev);
415+
416+ dev_dbg(dev, " pipe :%08x ", urb->pipe);
417+
418+ usbip_dump_pipe(urb->pipe);
419+
420+ dev_dbg(dev, " status :%d\n", urb->status);
421+ dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
422+ dev_dbg(dev, " transfer_buffer :%p\n", urb->transfer_buffer);
423+ dev_dbg(dev, " transfer_buffer_length:%d\n", urb->transfer_buffer_length);
424+ dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
425+ dev_dbg(dev, " setup_packet :%p\n", urb->setup_packet);
426+
427+ if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
428+ usbip_dump_usb_ctrlrequest(
429+ (struct usb_ctrlrequest *)urb->setup_packet);
430+
431+ dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
432+ dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
433+ dev_dbg(dev, " interval :%d\n", urb->interval);
434+ dev_dbg(dev, " error_count :%d\n", urb->error_count);
435+ dev_dbg(dev, " context :%p\n", urb->context);
436+ dev_dbg(dev, " complete :%p\n", urb->complete);
437+}
438+EXPORT_SYMBOL_GPL(usbip_dump_urb);
439+
440+void usbip_dump_header(struct usbip_header *pdu)
441+{
442+ udbg("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
443+ pdu->base.command,
444+ pdu->base.seqnum,
445+ pdu->base.devid,
446+ pdu->base.direction,
447+ pdu->base.ep);
448+
449+ switch (pdu->base.command) {
450+ case USBIP_CMD_SUBMIT:
451+ udbg("CMD_SUBMIT: x_flags %u x_len %u sf %u #p %u iv %u\n",
452+ pdu->u.cmd_submit.transfer_flags,
453+ pdu->u.cmd_submit.transfer_buffer_length,
454+ pdu->u.cmd_submit.start_frame,
455+ pdu->u.cmd_submit.number_of_packets,
456+ pdu->u.cmd_submit.interval);
457+ break;
458+ case USBIP_CMD_UNLINK:
459+ udbg("CMD_UNLINK: seq %u\n", pdu->u.cmd_unlink.seqnum);
460+ break;
461+ case USBIP_RET_SUBMIT:
462+ udbg("RET_SUBMIT: st %d al %u sf %d ec %d\n",
463+ pdu->u.ret_submit.status,
464+ pdu->u.ret_submit.actual_length,
465+ pdu->u.ret_submit.start_frame,
466+ pdu->u.ret_submit.error_count);
467+ case USBIP_RET_UNLINK:
468+ udbg("RET_UNLINK: status %d\n", pdu->u.ret_unlink.status);
469+ break;
470+ default:
471+ /* NOT REACHED */
472+ udbg("UNKNOWN\n");
473+ }
474+}
475+EXPORT_SYMBOL_GPL(usbip_dump_header);
476+
477+
478+/*-------------------------------------------------------------------------*/
479+/* thread routines */
480+
481+int usbip_thread(void *param)
482+{
483+ struct usbip_task *ut = param;
484+
485+ if (!ut)
486+ return -EINVAL;
487+
488+ lock_kernel();
489+ daemonize(ut->name);
490+ allow_signal(SIGKILL);
491+ ut->thread = current;
492+ unlock_kernel();
493+
494+ /* srv.rb must wait for rx_thread starting */
495+ complete(&ut->thread_done);
496+
497+ /* start of while loop */
498+ ut->loop_ops(ut);
499+
500+ /* end of loop */
501+ ut->thread = NULL;
502+
503+ complete_and_exit(&ut->thread_done, 0);
504+}
505+
506+void usbip_start_threads(struct usbip_device *ud)
507+{
508+ /*
509+ * threads are invoked per one device (per one connection).
510+ */
511+ kernel_thread(usbip_thread, (void *)&ud->tcp_rx, 0);
512+ kernel_thread(usbip_thread, (void *)&ud->tcp_tx, 0);
513+
514+ /* confirm threads are starting */
515+ wait_for_completion(&ud->tcp_rx.thread_done);
516+ wait_for_completion(&ud->tcp_tx.thread_done);
517+}
518+EXPORT_SYMBOL_GPL(usbip_start_threads);
519+
520+void usbip_stop_threads(struct usbip_device *ud)
521+{
522+ /* kill threads related to this sdev, if v.c. exists */
523+ if (ud->tcp_rx.thread != NULL) {
524+ send_sig(SIGKILL, ud->tcp_rx.thread, 1);
525+ wait_for_completion(&ud->tcp_rx.thread_done);
526+ udbg("rx_thread for ud %p has finished\n", ud);
527+ }
528+
529+ if (ud->tcp_tx.thread != NULL) {
530+ send_sig(SIGKILL, ud->tcp_tx.thread, 1);
531+ wait_for_completion(&ud->tcp_tx.thread_done);
532+ udbg("tx_thread for ud %p has finished\n", ud);
533+ }
534+}
535+EXPORT_SYMBOL_GPL(usbip_stop_threads);
536+
537+void usbip_task_init(struct usbip_task *ut, char *name,
538+ void (*loop_ops)(struct usbip_task *))
539+{
540+ ut->thread = NULL;
541+ init_completion(&ut->thread_done);
542+ ut->name = name;
543+ ut->loop_ops = loop_ops;
544+}
545+EXPORT_SYMBOL_GPL(usbip_task_init);
546+
547+
548+/*-------------------------------------------------------------------------*/
549+/* socket routines */
550+
551+ /* Send/receive messages over TCP/IP. I refer drivers/block/nbd.c */
552+int usbip_xmit(int send, struct socket *sock, char *buf,
553+ int size, int msg_flags)
554+{
555+ int result;
556+ struct msghdr msg;
557+ struct kvec iov;
558+ int total = 0;
559+
560+ /* for blocks of if (dbg_flag_xmit) */
561+ char *bp = buf;
562+ int osize = size;
563+
564+ dbg_xmit("enter\n");
565+
566+ if (!sock || !buf || !size) {
567+ printk(KERN_ERR "%s: invalid arg, sock %p buff %p size %d\n",
568+ __func__, sock, buf, size);
569+ return -EINVAL;
570+ }
571+
572+
573+ if (dbg_flag_xmit) {
574+ if (send) {
575+ if (!in_interrupt())
576+ printk(KERN_DEBUG "%-10s:", current->comm);
577+ else
578+ printk(KERN_DEBUG "interupt :");
579+
580+ printk("%s: sending... , sock %p, buf %p, "
581+ "size %d, msg_flags %d\n", __func__,
582+ sock, buf, size, msg_flags);
583+ usbip_dump_buffer(buf, size);
584+ }
585+ }
586+
587+
588+ do {
589+ sock->sk->sk_allocation = GFP_NOIO;
590+ iov.iov_base = buf;
591+ iov.iov_len = size;
592+ msg.msg_name = NULL;
593+ msg.msg_namelen = 0;
594+ msg.msg_control = NULL;
595+ msg.msg_controllen = 0;
596+ msg.msg_namelen = 0;
597+ msg.msg_flags = msg_flags | MSG_NOSIGNAL;
598+
599+ if (send)
600+ result = kernel_sendmsg(sock, &msg, &iov, 1, size);
601+ else
602+ result = kernel_recvmsg(sock, &msg, &iov, 1, size,
603+ MSG_WAITALL);
604+
605+ if (result <= 0) {
606+ udbg("usbip_xmit: %s sock %p buf %p size %u ret %d"
607+ " total %d\n",
608+ send ? "send" : "receive", sock, buf,
609+ size, result, total);
610+ goto err;
611+ }
612+
613+ size -= result;
614+ buf += result;
615+ total += result;
616+
617+ } while (size > 0);
618+
619+
620+ if (dbg_flag_xmit) {
621+ if (!send) {
622+ if (!in_interrupt())
623+ printk(KERN_DEBUG "%-10s:", current->comm);
624+ else
625+ printk(KERN_DEBUG "interupt :");
626+
627+ printk("usbip_xmit: receiving....\n");
628+ usbip_dump_buffer(bp, osize);
629+ printk("usbip_xmit: received, osize %d ret %d size %d "
630+ "total %d\n", osize, result, size,
631+ total);
632+ }
633+
634+ if (send)
635+ printk("usbip_xmit: send, total %d\n", total);
636+ }
637+
638+ return total;
639+
640+err:
641+ return result;
642+}
643+EXPORT_SYMBOL_GPL(usbip_xmit);
644+
645+
646+/* now a usrland utility should set options. */
647+#if 0
648+int setquickack(struct socket *socket)
649+{
650+ mm_segment_t oldfs;
651+ int val = 1;
652+ int ret;
653+
654+ oldfs = get_fs();
655+ set_fs(get_ds());
656+ ret = socket->ops->setsockopt(socket, SOL_TCP, TCP_QUICKACK,
657+ (char __user *) &val, sizeof(ret));
658+ set_fs(oldfs);
659+
660+ return ret;
661+}
662+
663+int setnodelay(struct socket *socket)
664+{
665+ mm_segment_t oldfs;
666+ int val = 1;
667+ int ret;
668+
669+ oldfs = get_fs();
670+ set_fs(get_ds());
671+ ret = socket->ops->setsockopt(socket, SOL_TCP, TCP_NODELAY,
672+ (char __user *) &val, sizeof(ret));
673+ set_fs(oldfs);
674+
675+ return ret;
676+}
677+
678+int setkeepalive(struct socket *socket)
679+{
680+ mm_segment_t oldfs;
681+ int val = 1;
682+ int ret;
683+
684+ oldfs = get_fs();
685+ set_fs(get_ds());
686+ ret = socket->ops->setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE,
687+ (char __user *) &val, sizeof(ret));
688+ set_fs(oldfs);
689+
690+ return ret;
691+}
692+
693+void setreuse(struct socket *socket)
694+{
695+ socket->sk->sk_reuse = 1;
696+}
697+#endif
698+
699+struct socket *sockfd_to_socket(unsigned int sockfd)
700+{
701+ struct socket *socket;
702+ struct file *file;
703+ struct inode *inode;
704+
705+ file = fget(sockfd);
706+ if (!file) {
707+ printk(KERN_ERR "%s: invalid sockfd\n", __func__);
708+ return NULL;
709+ }
710+
711+ inode = file->f_dentry->d_inode;
712+
713+ if (!inode || !S_ISSOCK(inode->i_mode))
714+ return NULL;
715+
716+ socket = SOCKET_I(inode);
717+
718+ return socket;
719+}
720+EXPORT_SYMBOL_GPL(sockfd_to_socket);
721+
722+
723+
724+/*-------------------------------------------------------------------------*/
725+/* pdu routines */
726+
727+/* there may be more cases to tweak the flags. */
728+static unsigned int tweak_transfer_flags(unsigned int flags)
729+{
730+
731+ if (flags & URB_NO_TRANSFER_DMA_MAP)
732+ /*
733+ * vhci_hcd does not provide DMA-mapped I/O. The upper
734+ * driver does not need to set this flag. The remote
735+ * usbip.ko does not still perform DMA-mapped I/O for
736+ * DMA-caplable host controllers. So, clear this flag.
737+ */
738+ flags &= ~URB_NO_TRANSFER_DMA_MAP;
739+
740+ if (flags & URB_NO_SETUP_DMA_MAP)
741+ flags &= ~URB_NO_SETUP_DMA_MAP;
742+
743+ return flags;
744+}
745+
746+static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
747+ int pack)
748+{
749+ struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
750+
751+ /*
752+ * Some members are not still implemented in usbip. I hope this issue
753+ * will be discussed when usbip is ported to other operating systems.
754+ */
755+ if (pack) {
756+ /* vhci_tx.c */
757+ spdu->transfer_flags =
758+ tweak_transfer_flags(urb->transfer_flags);
759+ spdu->transfer_buffer_length = urb->transfer_buffer_length;
760+ spdu->start_frame = urb->start_frame;
761+ spdu->number_of_packets = urb->number_of_packets;
762+ spdu->interval = urb->interval;
763+ } else {
764+ /* stub_rx.c */
765+ urb->transfer_flags = spdu->transfer_flags;
766+
767+ urb->transfer_buffer_length = spdu->transfer_buffer_length;
768+ urb->start_frame = spdu->start_frame;
769+ urb->number_of_packets = spdu->number_of_packets;
770+ urb->interval = spdu->interval;
771+ }
772+}
773+
774+static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
775+ int pack)
776+{
777+ struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
778+
779+ if (pack) {
780+ /* stub_tx.c */
781+
782+ rpdu->status = urb->status;
783+ rpdu->actual_length = urb->actual_length;
784+ rpdu->start_frame = urb->start_frame;
785+ rpdu->error_count = urb->error_count;
786+ } else {
787+ /* vhci_rx.c */
788+
789+ urb->status = rpdu->status;
790+ urb->actual_length = rpdu->actual_length;
791+ urb->start_frame = rpdu->start_frame;
792+ urb->error_count = rpdu->error_count;
793+ }
794+}
795+
796+
797+void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
798+ int pack)
799+{
800+ switch (cmd) {
801+ case USBIP_CMD_SUBMIT:
802+ usbip_pack_cmd_submit(pdu, urb, pack);
803+ break;
804+ case USBIP_RET_SUBMIT:
805+ usbip_pack_ret_submit(pdu, urb, pack);
806+ break;
807+ default:
808+ err("unknown command");
809+ /* NOTREACHED */
810+ /* BUG(); */
811+ }
812+}
813+EXPORT_SYMBOL_GPL(usbip_pack_pdu);
814+
815+
816+static void correct_endian_basic(struct usbip_header_basic *base, int send)
817+{
818+ if (send) {
819+ base->command = cpu_to_be32(base->command);
820+ base->seqnum = cpu_to_be32(base->seqnum);
821+ base->devid = cpu_to_be32(base->devid);
822+ base->direction = cpu_to_be32(base->direction);
823+ base->ep = cpu_to_be32(base->ep);
824+ } else {
825+ base->command = be32_to_cpu(base->command);
826+ base->seqnum = be32_to_cpu(base->seqnum);
827+ base->devid = be32_to_cpu(base->devid);
828+ base->direction = be32_to_cpu(base->direction);
829+ base->ep = be32_to_cpu(base->ep);
830+ }
831+}
832+
833+static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
834+ int send)
835+{
836+ if (send) {
837+ pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
838+
839+ cpu_to_be32s(&pdu->transfer_buffer_length);
840+ cpu_to_be32s(&pdu->start_frame);
841+ cpu_to_be32s(&pdu->number_of_packets);
842+ cpu_to_be32s(&pdu->interval);
843+ } else {
844+ pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
845+
846+ be32_to_cpus(&pdu->transfer_buffer_length);
847+ be32_to_cpus(&pdu->start_frame);
848+ be32_to_cpus(&pdu->number_of_packets);
849+ be32_to_cpus(&pdu->interval);
850+ }
851+}
852+
853+static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
854+ int send)
855+{
856+ if (send) {
857+ cpu_to_be32s(&pdu->status);
858+ cpu_to_be32s(&pdu->actual_length);
859+ cpu_to_be32s(&pdu->start_frame);
860+ cpu_to_be32s(&pdu->error_count);
861+ } else {
862+ be32_to_cpus(&pdu->status);
863+ be32_to_cpus(&pdu->actual_length);
864+ be32_to_cpus(&pdu->start_frame);
865+ be32_to_cpus(&pdu->error_count);
866+ }
867+}
868+
869+static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
870+ int send)
871+{
872+ if (send)
873+ pdu->seqnum = cpu_to_be32(pdu->seqnum);
874+ else
875+ pdu->seqnum = be32_to_cpu(pdu->seqnum);
876+}
877+
878+static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
879+ int send)
880+{
881+ if (send)
882+ cpu_to_be32s(&pdu->status);
883+ else
884+ be32_to_cpus(&pdu->status);
885+}
886+
887+void usbip_header_correct_endian(struct usbip_header *pdu, int send)
888+{
889+ __u32 cmd = 0;
890+
891+ if (send)
892+ cmd = pdu->base.command;
893+
894+ correct_endian_basic(&pdu->base, send);
895+
896+ if (!send)
897+ cmd = pdu->base.command;
898+
899+ switch (cmd) {
900+ case USBIP_CMD_SUBMIT:
901+ correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
902+ break;
903+ case USBIP_RET_SUBMIT:
904+ correct_endian_ret_submit(&pdu->u.ret_submit, send);
905+ break;
906+ case USBIP_CMD_UNLINK:
907+ correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
908+ break;
909+ case USBIP_RET_UNLINK:
910+ correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
911+ break;
912+ default:
913+ /* NOTREACHED */
914+ err("unknown command in pdu header: %d", cmd);
915+ /* BUG(); */
916+ }
917+}
918+EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
919+
920+static void usbip_iso_pakcet_correct_endian(
921+ struct usbip_iso_packet_descriptor *iso,
922+ int send)
923+{
924+ /* does not need all members. but copy all simply. */
925+ if (send) {
926+ iso->offset = cpu_to_be32(iso->offset);
927+ iso->length = cpu_to_be32(iso->length);
928+ iso->status = cpu_to_be32(iso->status);
929+ iso->actual_length = cpu_to_be32(iso->actual_length);
930+ } else {
931+ iso->offset = be32_to_cpu(iso->offset);
932+ iso->length = be32_to_cpu(iso->length);
933+ iso->status = be32_to_cpu(iso->status);
934+ iso->actual_length = be32_to_cpu(iso->actual_length);
935+ }
936+}
937+
938+static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
939+ struct usb_iso_packet_descriptor *uiso, int pack)
940+{
941+ if (pack) {
942+ iso->offset = uiso->offset;
943+ iso->length = uiso->length;
944+ iso->status = uiso->status;
945+ iso->actual_length = uiso->actual_length;
946+ } else {
947+ uiso->offset = iso->offset;
948+ uiso->length = iso->length;
949+ uiso->status = iso->status;
950+ uiso->actual_length = iso->actual_length;
951+ }
952+}
953+
954+
955+/* must free buffer */
956+void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
957+{
958+ void *buff;
959+ struct usbip_iso_packet_descriptor *iso;
960+ int np = urb->number_of_packets;
961+ ssize_t size = np * sizeof(*iso);
962+ int i;
963+
964+ buff = kzalloc(size, GFP_KERNEL);
965+ if (!buff)
966+ return NULL;
967+
968+ for (i = 0; i < np; i++) {
969+ iso = buff + (i * sizeof(*iso));
970+
971+ usbip_pack_iso(iso, &urb->iso_frame_desc[i], 1);
972+ usbip_iso_pakcet_correct_endian(iso, 1);
973+ }
974+
975+ *bufflen = size;
976+
977+ return buff;
978+}
979+EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
980+
981+/* some members of urb must be substituted before. */
982+int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
983+{
984+ void *buff;
985+ struct usbip_iso_packet_descriptor *iso;
986+ int np = urb->number_of_packets;
987+ int size = np * sizeof(*iso);
988+ int i;
989+ int ret;
990+
991+ if (!usb_pipeisoc(urb->pipe))
992+ return 0;
993+
994+ /* my Bluetooth dongle gets ISO URBs which are np = 0 */
995+ if (np == 0) {
996+ /* uinfo("iso np == 0\n"); */
997+ /* usbip_dump_urb(urb); */
998+ return 0;
999+ }
1000+
1001+ buff = kzalloc(size, GFP_KERNEL);
1002+ if (!buff)
1003+ return -ENOMEM;
1004+
1005+ ret = usbip_xmit(0, ud->tcp_socket, buff, size, 0);
1006+ if (ret != size) {
1007+ dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
1008+ ret);
1009+ kfree(buff);
1010+
1011+ if (ud->side == USBIP_STUB)
1012+ usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
1013+ else
1014+ usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
1015+
1016+ return -EPIPE;
1017+ }
1018+
1019+ for (i = 0; i < np; i++) {
1020+ iso = buff + (i * sizeof(*iso));
1021+
1022+ usbip_iso_pakcet_correct_endian(iso, 0);
1023+ usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0);
1024+ }
1025+
1026+
1027+ kfree(buff);
1028+
1029+ return ret;
1030+}
1031+EXPORT_SYMBOL_GPL(usbip_recv_iso);
1032+
1033+
1034+/* some members of urb must be substituted before. */
1035+int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
1036+{
1037+ int ret;
1038+ int size;
1039+
1040+ if (ud->side == USBIP_STUB) {
1041+ /* stub_rx.c */
1042+ /* the direction of urb must be OUT. */
1043+ if (usb_pipein(urb->pipe))
1044+ return 0;
1045+
1046+ size = urb->transfer_buffer_length;
1047+ } else {
1048+ /* vhci_rx.c */
1049+ /* the direction of urb must be IN. */
1050+ if (usb_pipeout(urb->pipe))
1051+ return 0;
1052+
1053+ size = urb->actual_length;
1054+ }
1055+
1056+ /* no need to recv xbuff */
1057+ if (!(size > 0))
1058+ return 0;
1059+
1060+ ret = usbip_xmit(0, ud->tcp_socket, (char *)urb->transfer_buffer,
1061+ size, 0);
1062+ if (ret != size) {
1063+ dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
1064+ if (ud->side == USBIP_STUB) {
1065+ usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
1066+ } else {
1067+ usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
1068+ return -EPIPE;
1069+ }
1070+ }
1071+
1072+ return ret;
1073+}
1074+EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
1075+
1076+
1077+/*-------------------------------------------------------------------------*/
1078+
1079+static int __init usbip_common_init(void)
1080+{
1081+ printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "" DRIVER_VERSION);
1082+
1083+ return 0;
1084+}
1085+
1086+static void __exit usbip_common_exit(void)
1087+{
1088+ return;
1089+}
1090+
1091+
1092+
1093+
1094+module_init(usbip_common_init);
1095+module_exit(usbip_common_exit);
1096+
1097+MODULE_AUTHOR(DRIVER_AUTHOR);
1098+MODULE_DESCRIPTION(DRIVER_DESC);
1099+MODULE_LICENSE("GPL");
1100diff --git a/drivers/staging/usbip/usbip_common.h b/drivers/staging/usbip/usbip_common.h
1101new file mode 100644
1102index 0000000..b0186b7
1103--- /dev/null
1104+++ b/drivers/staging/usbip/usbip_common.h
1105@@ -0,0 +1,406 @@
1106+/*
1107+ * Copyright (C) 2003-2008 Takahiro Hirofuchi
1108+ *
1109+ * This is free software; you can redistribute it and/or modify
1110+ * it under the terms of the GNU General Public License as published by
1111+ * the Free Software Foundation; either version 2 of the License, or
1112+ * (at your option) any later version.
1113+ *
1114+ * This is distributed in the hope that it will be useful,
1115+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
1116+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1117+ * GNU General Public License for more details.
1118+ *
1119+ * You should have received a copy of the GNU General Public License
1120+ * along with this program; if not, write to the Free Software
1121+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1122+ * USA.
1123+ */
1124+
1125+#ifndef __VHCI_COMMON_H
1126+#define __VHCI_COMMON_H
1127+
1128+
1129+#include <linux/version.h>
1130+#include <linux/usb.h>
1131+#include <asm/byteorder.h>
1132+#include <net/sock.h>
1133+
1134+/*-------------------------------------------------------------------------*/
1135+
1136+/*
1137+ * define macros to print messages
1138+ */
1139+
1140+/**
1141+ * udbg - print debug messages if CONFIG_USB_DEBUG is defined
1142+ * @fmt:
1143+ * @args:
1144+ */
1145+
1146+#ifdef CONFIG_USB_DEBUG
1147+
1148+#define udbg(fmt, args...) \
1149+ do { \
1150+ printk(KERN_DEBUG "%-10s:(%s,%d) %s: " fmt, \
1151+ (in_interrupt() ? "interrupt" : (current)->comm),\
1152+ __FILE__, __LINE__, __func__, ##args); \
1153+ } while (0)
1154+
1155+#else /* CONFIG_USB_DEBUG */
1156+
1157+#define udbg(fmt, args...) do { } while (0)
1158+
1159+#endif /* CONFIG_USB_DEBUG */
1160+
1161+
1162+enum {
1163+ usbip_debug_xmit = (1 << 0),
1164+ usbip_debug_sysfs = (1 << 1),
1165+ usbip_debug_urb = (1 << 2),
1166+ usbip_debug_eh = (1 << 3),
1167+
1168+ usbip_debug_stub_cmp = (1 << 8),
1169+ usbip_debug_stub_dev = (1 << 9),
1170+ usbip_debug_stub_rx = (1 << 10),
1171+ usbip_debug_stub_tx = (1 << 11),
1172+
1173+ usbip_debug_vhci_rh = (1 << 8),
1174+ usbip_debug_vhci_hc = (1 << 9),
1175+ usbip_debug_vhci_rx = (1 << 10),
1176+ usbip_debug_vhci_tx = (1 << 11),
1177+ usbip_debug_vhci_sysfs = (1 << 12)
1178+};
1179+
1180+#define dbg_flag_xmit (usbip_debug_flag & usbip_debug_xmit)
1181+#define dbg_flag_vhci_rh (usbip_debug_flag & usbip_debug_vhci_rh)
1182+#define dbg_flag_vhci_hc (usbip_debug_flag & usbip_debug_vhci_hc)
1183+#define dbg_flag_vhci_rx (usbip_debug_flag & usbip_debug_vhci_rx)
1184+#define dbg_flag_vhci_tx (usbip_debug_flag & usbip_debug_vhci_tx)
1185+#define dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs)
1186+#define dbg_flag_stub_rx (usbip_debug_flag & usbip_debug_stub_rx)
1187+#define dbg_flag_stub_tx (usbip_debug_flag & usbip_debug_stub_tx)
1188+
1189+extern unsigned long usbip_debug_flag;
1190+extern struct device_attribute dev_attr_usbip_debug;
1191+
1192+#define dbg_with_flag(flag, fmt, args...) \
1193+ do { \
1194+ if (flag & usbip_debug_flag) \
1195+ udbg(fmt , ##args); \
1196+ } while (0)
1197+
1198+#define dbg_sysfs(fmt, args...) \
1199+ dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
1200+#define dbg_xmit(fmt, args...) \
1201+ dbg_with_flag(usbip_debug_xmit, fmt , ##args)
1202+#define dbg_urb(fmt, args...) \
1203+ dbg_with_flag(usbip_debug_urb, fmt , ##args)
1204+#define dbg_eh(fmt, args...) \
1205+ dbg_with_flag(usbip_debug_eh, fmt , ##args)
1206+
1207+#define dbg_vhci_rh(fmt, args...) \
1208+ dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args)
1209+#define dbg_vhci_hc(fmt, args...) \
1210+ dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args)
1211+#define dbg_vhci_rx(fmt, args...) \
1212+ dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args)
1213+#define dbg_vhci_tx(fmt, args...) \
1214+ dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args)
1215+#define dbg_vhci_sysfs(fmt, args...) \
1216+ dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
1217+
1218+#define dbg_stub_cmp(fmt, args...) \
1219+ dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
1220+#define dbg_stub_rx(fmt, args...) \
1221+ dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
1222+#define dbg_stub_tx(fmt, args...) \
1223+ dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
1224+
1225+
1226+/**
1227+ * uerr - print error messages
1228+ * @fmt:
1229+ * @args:
1230+ */
1231+#define uerr(fmt, args...) \
1232+ do { \
1233+ printk(KERN_ERR "%-10s: ***ERROR*** (%s,%d) %s: " fmt, \
1234+ (in_interrupt() ? "interrupt" : (current)->comm),\
1235+ __FILE__, __LINE__, __func__, ##args); \
1236+ } while (0)
1237+
1238+/**
1239+ * uinfo - print information messages
1240+ * @fmt:
1241+ * @args:
1242+ */
1243+#define uinfo(fmt, args...) \
1244+ do { \
1245+ printk(KERN_INFO "usbip: " fmt , ## args); \
1246+ } while (0)
1247+
1248+
1249+/*-------------------------------------------------------------------------*/
1250+
1251+/*
1252+ * USB/IP request headers.
1253+ * Currently, we define 4 request types:
1254+ *
1255+ * - CMD_SUBMIT transfers a USB request, corresponding to usb_submit_urb().
1256+ * (client to server)
1257+ * - RET_RETURN transfers the result of CMD_SUBMIT.
1258+ * (server to client)
1259+ * - CMD_UNLINK transfers an unlink request of a pending USB request.
1260+ * (client to server)
1261+ * - RET_UNLINK transfers the result of CMD_UNLINK.
1262+ * (server to client)
1263+ *
1264+ * Note: The below request formats are based on the USB subsystem of Linux. Its
1265+ * details will be defined when other implementations come.
1266+ *
1267+ *
1268+ */
1269+
1270+/*
1271+ * A basic header followed by other additional headers.
1272+ */
1273+struct usbip_header_basic {
1274+#define USBIP_CMD_SUBMIT 0x0001
1275+#define USBIP_CMD_UNLINK 0x0002
1276+#define USBIP_RET_SUBMIT 0x0003
1277+#define USBIP_RET_UNLINK 0x0004
1278+ __u32 command;
1279+
1280+ /* sequencial number which identifies requests.
1281+ * incremented per connections */
1282+ __u32 seqnum;
1283+
1284+ /* devid is used to specify a remote USB device uniquely instead
1285+ * of busnum and devnum in Linux. In the case of Linux stub_driver,
1286+ * this value is ((busnum << 16) | devnum) */
1287+ __u32 devid;
1288+
1289+#define USBIP_DIR_OUT 0
1290+#define USBIP_DIR_IN 1
1291+ __u32 direction;
1292+ __u32 ep; /* endpoint number */
1293+} __attribute__ ((packed));
1294+
1295+/*
1296+ * An additional header for a CMD_SUBMIT packet.
1297+ */
1298+struct usbip_header_cmd_submit {
1299+ /* these values are basically the same as in a URB. */
1300+
1301+ /* the same in a URB. */
1302+ __u32 transfer_flags;
1303+
1304+ /* set the following data size (out),
1305+ * or expected reading data size (in) */
1306+ __s32 transfer_buffer_length;
1307+
1308+ /* it is difficult for usbip to sync frames (reserved only?) */
1309+ __s32 start_frame;
1310+
1311+ /* the number of iso descriptors that follows this header */
1312+ __s32 number_of_packets;
1313+
1314+ /* the maximum time within which this request works in a host
1315+ * controller of a server side */
1316+ __s32 interval;
1317+
1318+ /* set setup packet data for a CTRL request */
1319+ unsigned char setup[8];
1320+} __attribute__ ((packed));
1321+
1322+/*
1323+ * An additional header for a RET_SUBMIT packet.
1324+ */
1325+struct usbip_header_ret_submit {
1326+ __s32 status;
1327+ __s32 actual_length; /* returned data length */
1328+ __s32 start_frame; /* ISO and INT */
1329+ __s32 number_of_packets; /* ISO only */
1330+ __s32 error_count; /* ISO only */
1331+} __attribute__ ((packed));
1332+
1333+/*
1334+ * An additional header for a CMD_UNLINK packet.
1335+ */
1336+struct usbip_header_cmd_unlink {
1337+ __u32 seqnum; /* URB's seqnum which will be unlinked */
1338+} __attribute__ ((packed));
1339+
1340+
1341+/*
1342+ * An additional header for a RET_UNLINK packet.
1343+ */
1344+struct usbip_header_ret_unlink {
1345+ __s32 status;
1346+} __attribute__ ((packed));
1347+
1348+
1349+/* the same as usb_iso_packet_descriptor but packed for pdu */
1350+struct usbip_iso_packet_descriptor {
1351+ __u32 offset;
1352+ __u32 length; /* expected length */
1353+ __u32 actual_length;
1354+ __u32 status;
1355+} __attribute__ ((packed));
1356+
1357+
1358+/*
1359+ * All usbip packets use a common header to keep code simple.
1360+ */
1361+struct usbip_header {
1362+ struct usbip_header_basic base;
1363+
1364+ union {
1365+ struct usbip_header_cmd_submit cmd_submit;
1366+ struct usbip_header_ret_submit ret_submit;
1367+ struct usbip_header_cmd_unlink cmd_unlink;
1368+ struct usbip_header_ret_unlink ret_unlink;
1369+ } u;
1370+} __attribute__ ((packed));
1371+
1372+
1373+
1374+
1375+/*-------------------------------------------------------------------------*/
1376+
1377+
1378+int usbip_xmit(int, struct socket *, char *, int, int);
1379+int usbip_sendmsg(struct socket *, struct msghdr *, int);
1380+
1381+
1382+static inline int interface_to_busnum(struct usb_interface *interface)
1383+{
1384+ struct usb_device *udev = interface_to_usbdev(interface);
1385+ return udev->bus->busnum;
1386+}
1387+
1388+static inline int interface_to_devnum(struct usb_interface *interface)
1389+{
1390+ struct usb_device *udev = interface_to_usbdev(interface);
1391+ return udev->devnum;
1392+}
1393+
1394+static inline int interface_to_infnum(struct usb_interface *interface)
1395+{
1396+ return interface->cur_altsetting->desc.bInterfaceNumber;
1397+}
1398+
1399+#if 0
1400+int setnodelay(struct socket *);
1401+int setquickack(struct socket *);
1402+int setkeepalive(struct socket *socket);
1403+void setreuse(struct socket *);
1404+#endif
1405+
1406+struct socket *sockfd_to_socket(unsigned int);
1407+int set_sockaddr(struct socket *socket, struct sockaddr_storage *ss);
1408+
1409+void usbip_dump_urb(struct urb *purb);
1410+void usbip_dump_header(struct usbip_header *pdu);
1411+
1412+
1413+struct usbip_device;
1414+
1415+struct usbip_task {
1416+ struct task_struct *thread;
1417+ struct completion thread_done;
1418+ char *name;
1419+ void (*loop_ops)(struct usbip_task *);
1420+};
1421+
1422+enum usbip_side {
1423+ USBIP_VHCI,
1424+ USBIP_STUB,
1425+};
1426+
1427+enum usbip_status {
1428+ /* sdev is available. */
1429+ SDEV_ST_AVAILABLE = 0x01,
1430+ /* sdev is now used. */
1431+ SDEV_ST_USED,
1432+ /* sdev is unusable because of a fatal error. */
1433+ SDEV_ST_ERROR,
1434+
1435+ /* vdev does not connect a remote device. */
1436+ VDEV_ST_NULL,
1437+ /* vdev is used, but the USB address is not assigned yet */
1438+ VDEV_ST_NOTASSIGNED,
1439+ VDEV_ST_USED,
1440+ VDEV_ST_ERROR
1441+};
1442+
1443+/* a common structure for stub_device and vhci_device */
1444+struct usbip_device {
1445+ enum usbip_side side;
1446+
1447+ enum usbip_status status;
1448+
1449+ /* lock for status */
1450+ spinlock_t lock;
1451+
1452+ struct socket *tcp_socket;
1453+
1454+ struct usbip_task tcp_rx;
1455+ struct usbip_task tcp_tx;
1456+
1457+ /* event handler */
1458+#define USBIP_EH_SHUTDOWN (1 << 0)
1459+#define USBIP_EH_BYE (1 << 1)
1460+#define USBIP_EH_RESET (1 << 2)
1461+#define USBIP_EH_UNUSABLE (1 << 3)
1462+
1463+#define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
1464+#define SDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
1465+#define SDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
1466+#define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
1467+#define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
1468+
1469+#define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
1470+#define VDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
1471+#define VDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
1472+#define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
1473+
1474+ unsigned long event;
1475+ struct usbip_task eh;
1476+ wait_queue_head_t eh_waitq;
1477+
1478+ struct eh_ops {
1479+ void (*shutdown)(struct usbip_device *);
1480+ void (*reset)(struct usbip_device *);
1481+ void (*unusable)(struct usbip_device *);
1482+ } eh_ops;
1483+};
1484+
1485+
1486+void usbip_task_init(struct usbip_task *ut, char *,
1487+ void (*loop_ops)(struct usbip_task *));
1488+
1489+void usbip_start_threads(struct usbip_device *ud);
1490+void usbip_stop_threads(struct usbip_device *ud);
1491+int usbip_thread(void *param);
1492+
1493+void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
1494+ int pack);
1495+
1496+void usbip_header_correct_endian(struct usbip_header *pdu, int send);
1497+/* some members of urb must be substituted before. */
1498+int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
1499+/* some members of urb must be substituted before. */
1500+int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
1501+void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
1502+
1503+
1504+/* usbip_event.c */
1505+void usbip_start_eh(struct usbip_device *ud);
1506+void usbip_stop_eh(struct usbip_device *ud);
1507+void usbip_event_add(struct usbip_device *ud, unsigned long event);
1508+int usbip_event_happend(struct usbip_device *ud);
1509+
1510+
1511+#endif
1512diff --git a/drivers/staging/usbip/usbip_event.c b/drivers/staging/usbip/usbip_event.c
1513new file mode 100644
1514index 0000000..4318553
1515--- /dev/null
1516+++ b/drivers/staging/usbip/usbip_event.c
1517@@ -0,0 +1,141 @@
1518+/*
1519+ * Copyright (C) 2003-2008 Takahiro Hirofuchi
1520+ *
1521+ * This is free software; you can redistribute it and/or modify
1522+ * it under the terms of the GNU General Public License as published by
1523+ * the Free Software Foundation; either version 2 of the License, or
1524+ * (at your option) any later version.
1525+ *
1526+ * This is distributed in the hope that it will be useful,
1527+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
1528+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1529+ * GNU General Public License for more details.
1530+ *
1531+ * You should have received a copy of the GNU General Public License
1532+ * along with this program; if not, write to the Free Software
1533+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1534+ * USA.
1535+ */
1536+
1537+#include "usbip_common.h"
1538+
1539+static int event_handler(struct usbip_device *ud)
1540+{
1541+ dbg_eh("enter\n");
1542+
1543+ /*
1544+ * Events are handled by only this thread.
1545+ */
1546+ while (usbip_event_happend(ud)) {
1547+ dbg_eh("pending event %lx\n", ud->event);
1548+
1549+ /*
1550+ * NOTE: shutdown must come first.
1551+ * Shutdown the device.
1552+ */
1553+ if (ud->event & USBIP_EH_SHUTDOWN) {
1554+ ud->eh_ops.shutdown(ud);
1555+
1556+ ud->event &= ~USBIP_EH_SHUTDOWN;
1557+
1558+ break;
1559+ }
1560+
1561+ /* Stop the error handler. */
1562+ if (ud->event & USBIP_EH_BYE)
1563+ return -1;
1564+
1565+ /* Reset the device. */
1566+ if (ud->event & USBIP_EH_RESET) {
1567+ ud->eh_ops.reset(ud);
1568+
1569+ ud->event &= ~USBIP_EH_RESET;
1570+
1571+ break;
1572+ }
1573+
1574+ /* Mark the device as unusable. */
1575+ if (ud->event & USBIP_EH_UNUSABLE) {
1576+ ud->eh_ops.unusable(ud);
1577+
1578+ ud->event &= ~USBIP_EH_UNUSABLE;
1579+
1580+ break;
1581+ }
1582+
1583+ /* NOTREACHED */
1584+ printk(KERN_ERR "%s: unknown event\n", __func__);
1585+ return -1;
1586+ }
1587+
1588+ return 0;
1589+}
1590+
1591+static void event_handler_loop(struct usbip_task *ut)
1592+{
1593+ struct usbip_device *ud = container_of(ut, struct usbip_device, eh);
1594+
1595+ while (1) {
1596+ if (signal_pending(current)) {
1597+ dbg_eh("signal catched!\n");
1598+ break;
1599+ }
1600+
1601+ if (event_handler(ud) < 0)
1602+ break;
1603+
1604+ wait_event_interruptible(ud->eh_waitq, usbip_event_happend(ud));
1605+ dbg_eh("wakeup\n");
1606+ }
1607+}
1608+
1609+void usbip_start_eh(struct usbip_device *ud)
1610+{
1611+ struct usbip_task *eh = &ud->eh;
1612+
1613+ init_waitqueue_head(&ud->eh_waitq);
1614+ ud->event = 0;
1615+
1616+ usbip_task_init(eh, "usbip_eh", event_handler_loop);
1617+
1618+ kernel_thread(usbip_thread, (void *)eh, 0);
1619+
1620+ wait_for_completion(&eh->thread_done);
1621+}
1622+EXPORT_SYMBOL_GPL(usbip_start_eh);
1623+
1624+void usbip_stop_eh(struct usbip_device *ud)
1625+{
1626+ struct usbip_task *eh = &ud->eh;
1627+
1628+ wait_for_completion(&eh->thread_done);
1629+ dbg_eh("usbip_eh has finished\n");
1630+}
1631+EXPORT_SYMBOL_GPL(usbip_stop_eh);
1632+
1633+void usbip_event_add(struct usbip_device *ud, unsigned long event)
1634+{
1635+ spin_lock(&ud->lock);
1636+
1637+ ud->event |= event;
1638+
1639+ wake_up(&ud->eh_waitq);
1640+
1641+ spin_unlock(&ud->lock);
1642+}
1643+EXPORT_SYMBOL_GPL(usbip_event_add);
1644+
1645+int usbip_event_happend(struct usbip_device *ud)
1646+{
1647+ int happend = 0;
1648+
1649+ spin_lock(&ud->lock);
1650+
1651+ if (ud->event != 0)
1652+ happend = 1;
1653+
1654+ spin_unlock(&ud->lock);
1655+
1656+ return happend;
1657+}
1658+EXPORT_SYMBOL_GPL(usbip_event_happend);
1659--
16601.6.0.2
1661