aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging
diff options
context:
space:
mode:
authorTakahiro Hirofuchi <hirofuchi@users.sourceforge.net>2008-07-09 16:56:51 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2008-10-10 18:31:08 -0400
commit05a1f28e879e3b4d6a9c08e30b1898943f77b6e7 (patch)
tree12d9e45cd655996050620befba1bbeee77798765 /drivers/staging
parent866b8695d67e83f47194731d3a7ba55826a7ec70 (diff)
Staging: USB/IP: add common functions needed
This adds the common functions needed by both the host and client side of the USB/IP code. Brian Merrell cleaned up a lot of this code and submitted it for inclusion. Greg also did a lot of cleanup. Signed-off-by: Brian G. Merrell <bgmerrell@novell.com> Cc: Takahiro Hirofuchi <hirofuchi@users.sourceforge.net> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/staging')
-rw-r--r--drivers/staging/Kconfig2
-rw-r--r--drivers/staging/Makefile1
-rw-r--r--drivers/staging/usbip/Kconfig14
-rw-r--r--drivers/staging/usbip/Makefile6
-rw-r--r--drivers/staging/usbip/README6
-rw-r--r--drivers/staging/usbip/usbip_common.c997
-rw-r--r--drivers/staging/usbip/usbip_common.h406
-rw-r--r--drivers/staging/usbip/usbip_event.c141
8 files changed, 1573 insertions, 0 deletions
diff --git a/drivers/staging/Kconfig b/drivers/staging/Kconfig
index f16bc9cdaf33..4dbf795df744 100644
--- a/drivers/staging/Kconfig
+++ b/drivers/staging/Kconfig
@@ -33,4 +33,6 @@ source "drivers/staging/me4000/Kconfig"
33 33
34source "drivers/staging/go7007/Kconfig" 34source "drivers/staging/go7007/Kconfig"
35 35
36source "drivers/staging/usbip/Kconfig"
37
36endif # STAGING 38endif # STAGING
diff --git a/drivers/staging/Makefile b/drivers/staging/Makefile
index aa61662e6321..be42c0d4db0e 100644
--- a/drivers/staging/Makefile
+++ b/drivers/staging/Makefile
@@ -5,3 +5,4 @@ obj-$(CONFIG_SLICOSS) += slicoss/
5obj-$(CONFIG_SXG) += sxg/ 5obj-$(CONFIG_SXG) += sxg/
6obj-$(CONFIG_ME4000) += me4000/ 6obj-$(CONFIG_ME4000) += me4000/
7obj-$(CONFIG_VIDEO_GO7007) += go7007/ 7obj-$(CONFIG_VIDEO_GO7007) += go7007/
8obj-$(CONFIG_USB_IP_COMMON) += usbip/
diff --git a/drivers/staging/usbip/Kconfig b/drivers/staging/usbip/Kconfig
new file mode 100644
index 000000000000..37efb5e1bb82
--- /dev/null
+++ b/drivers/staging/usbip/Kconfig
@@ -0,0 +1,14 @@
1config USB_IP_COMMON
2 tristate "USB IP support (EXPERIMENTAL)"
3 depends on USB && EXPERIMENTAL
4 default N
5 ---help---
6 This enables pushing USB packets over IP to allow remote
7 machines access to USB devices directly. For more details,
8 and links to the userspace utility programs to let this work
9 properly, see http://usbip.naist.jp/
10
11 To compile this driver as a module, choose M here: the
12 module will be called usbip_common_mod.
13
14 If unsure, say N.
diff --git a/drivers/staging/usbip/Makefile b/drivers/staging/usbip/Makefile
new file mode 100644
index 000000000000..ce925caa6083
--- /dev/null
+++ b/drivers/staging/usbip/Makefile
@@ -0,0 +1,6 @@
1obj-$(CONFIG_USB_IP_COMMON) += usbip_common_mod.o
2usbip_common_mod-objs := usbip_common.o usbip_event.o
3
4ifeq ($(CONFIG_USB_DEBUG),y)
5 EXTRA_CFLAGS += -DDEBUG
6endif
diff --git a/drivers/staging/usbip/README b/drivers/staging/usbip/README
new file mode 100644
index 000000000000..c11be5735485
--- /dev/null
+++ b/drivers/staging/usbip/README
@@ -0,0 +1,6 @@
1TODO:
2 - more discussion about the protocol
3 - testing
4 - review of the userspace interface
5
6Please send patches for this code to Greg Kroah-Hartman <greg@kroah.com>
diff --git a/drivers/staging/usbip/usbip_common.c b/drivers/staging/usbip/usbip_common.c
new file mode 100644
index 000000000000..e64918f42ff7
--- /dev/null
+++ b/drivers/staging/usbip/usbip_common.c
@@ -0,0 +1,997 @@
1/*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
20#include <linux/kernel.h>
21#include <linux/file.h>
22#include <linux/tcp.h>
23#include <linux/in.h>
24#include "usbip_common.h"
25
26/* version information */
27#define DRIVER_VERSION "1.0"
28#define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi _at_ users.sourceforge.net>"
29#define DRIVER_DESC "usbip common driver"
30
31/*-------------------------------------------------------------------------*/
32/* debug routines */
33
34#ifdef CONFIG_USB_DEBUG
35unsigned long usbip_debug_flag = 0xffffffff;
36#else
37unsigned long usbip_debug_flag;
38#endif
39EXPORT_SYMBOL_GPL(usbip_debug_flag);
40
41
42/* FIXME */
43struct device_attribute dev_attr_usbip_debug;
44EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
45
46
47static ssize_t show_flag(struct device *dev, struct device_attribute *attr,
48 char *buf)
49{
50 return sprintf(buf, "%lx\n", usbip_debug_flag);
51}
52
53static ssize_t store_flag(struct device *dev, struct device_attribute *attr,
54 const char *buf, size_t count)
55{
56 unsigned long flag;
57
58 sscanf(buf, "%lx", &flag);
59 usbip_debug_flag = flag;
60
61 return count;
62}
63DEVICE_ATTR(usbip_debug, (S_IRUGO | S_IWUSR), show_flag, store_flag);
64
65static void usbip_dump_buffer(char *buff, int bufflen)
66{
67 int i;
68
69 if (bufflen > 128) {
70 for (i = 0; i < 128; i++) {
71 if (i%24 == 0)
72 printk(" ");
73 printk("%02x ", (unsigned char) buff[i]);
74 if (i%4 == 3)
75 printk("| ");
76 if (i%24 == 23)
77 printk("\n");
78 }
79 printk("... (%d byte)\n", bufflen);
80 return;
81 }
82
83 for (i = 0; i < bufflen; i++) {
84 if (i%24 == 0)
85 printk(" ");
86 printk("%02x ", (unsigned char) buff[i]);
87 if (i%4 == 3)
88 printk("| ");
89 if (i%24 == 23)
90 printk("\n");
91 }
92 printk("\n");
93
94}
95
96static void usbip_dump_pipe(unsigned int p)
97{
98 unsigned char type = usb_pipetype(p);
99 unsigned char ep = usb_pipeendpoint(p);
100 unsigned char dev = usb_pipedevice(p);
101 unsigned char dir = usb_pipein(p);
102
103 printk("dev(%d) ", dev);
104 printk("ep(%d) ", ep);
105 printk("%s ", dir ? "IN" : "OUT");
106
107 switch (type) {
108 case PIPE_ISOCHRONOUS:
109 printk("%s ", "ISO");
110 break;
111 case PIPE_INTERRUPT:
112 printk("%s ", "INT");
113 break;
114 case PIPE_CONTROL:
115 printk("%s ", "CTL");
116 break;
117 case PIPE_BULK:
118 printk("%s ", "BLK");
119 break;
120 default:
121 printk("ERR");
122 }
123
124 printk("\n");
125
126}
127
128static void usbip_dump_usb_device(struct usb_device *udev)
129{
130 struct device *dev = &udev->dev;
131 int i;
132
133 dev_dbg(dev, " devnum(%d) devpath(%s)",
134 udev->devnum, udev->devpath);
135
136 switch (udev->speed) {
137 case USB_SPEED_HIGH:
138 printk(" SPD_HIGH");
139 break;
140 case USB_SPEED_FULL:
141 printk(" SPD_FULL");
142 break;
143 case USB_SPEED_LOW:
144 printk(" SPD_LOW");
145 break;
146 case USB_SPEED_UNKNOWN:
147 printk(" SPD_UNKNOWN");
148 break;
149 default:
150 printk(" SPD_ERROR");
151 }
152
153 printk(" tt %p, ttport %d", udev->tt, udev->ttport);
154 printk("\n");
155
156 dev_dbg(dev, " ");
157 for (i = 0; i < 16; i++)
158 printk(" %2u", i);
159 printk("\n");
160
161 dev_dbg(dev, " toggle0(IN) :");
162 for (i = 0; i < 16; i++)
163 printk(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
164 printk("\n");
165
166 dev_dbg(dev, " toggle1(OUT):");
167 for (i = 0; i < 16; i++)
168 printk(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
169 printk("\n");
170
171
172 dev_dbg(dev, " epmaxp_in :");
173 for (i = 0; i < 16; i++) {
174 if (udev->ep_in[i])
175 printk(" %2u",
176 le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
177 }
178 printk("\n");
179
180 dev_dbg(dev, " epmaxp_out :");
181 for (i = 0; i < 16; i++) {
182 if (udev->ep_out[i])
183 printk(" %2u",
184 le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
185 }
186 printk("\n");
187
188 dev_dbg(dev, "parent %p, bus %p\n", udev->parent, udev->bus);
189
190 dev_dbg(dev, "descriptor %p, config %p, actconfig %p, "
191 "rawdescriptors %p\n", &udev->descriptor, udev->config,
192 udev->actconfig, udev->rawdescriptors);
193
194 dev_dbg(dev, "have_langid %d, string_langid %d\n",
195 udev->have_langid, udev->string_langid);
196
197 dev_dbg(dev, "maxchild %d, children %p\n",
198 udev->maxchild, udev->children);
199}
200
201static void usbip_dump_request_type(__u8 rt)
202{
203 switch (rt & USB_RECIP_MASK) {
204 case USB_RECIP_DEVICE:
205 printk("DEVICE");
206 break;
207 case USB_RECIP_INTERFACE:
208 printk("INTERF");
209 break;
210 case USB_RECIP_ENDPOINT:
211 printk("ENDPOI");
212 break;
213 case USB_RECIP_OTHER:
214 printk("OTHER ");
215 break;
216 default:
217 printk("------");
218 }
219}
220
221static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
222{
223 if (!cmd) {
224 printk(" %s : null pointer\n", __FUNCTION__);
225 return;
226 }
227
228 printk(" ");
229 printk("bRequestType(%02X) ", cmd->bRequestType);
230 printk("bRequest(%02X) " , cmd->bRequest);
231 printk("wValue(%04X) ", cmd->wValue);
232 printk("wIndex(%04X) ", cmd->wIndex);
233 printk("wLength(%04X) ", cmd->wLength);
234
235 printk("\n ");
236
237 if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
238 printk("STANDARD ");
239 switch (cmd->bRequest) {
240 case USB_REQ_GET_STATUS:
241 printk("GET_STATUS");
242 break;
243 case USB_REQ_CLEAR_FEATURE:
244 printk("CLEAR_FEAT");
245 break;
246 case USB_REQ_SET_FEATURE:
247 printk("SET_FEAT ");
248 break;
249 case USB_REQ_SET_ADDRESS:
250 printk("SET_ADDRRS");
251 break;
252 case USB_REQ_GET_DESCRIPTOR:
253 printk("GET_DESCRI");
254 break;
255 case USB_REQ_SET_DESCRIPTOR:
256 printk("SET_DESCRI");
257 break;
258 case USB_REQ_GET_CONFIGURATION:
259 printk("GET_CONFIG");
260 break;
261 case USB_REQ_SET_CONFIGURATION:
262 printk("SET_CONFIG");
263 break;
264 case USB_REQ_GET_INTERFACE:
265 printk("GET_INTERF");
266 break;
267 case USB_REQ_SET_INTERFACE:
268 printk("SET_INTERF");
269 break;
270 case USB_REQ_SYNCH_FRAME:
271 printk("SYNC_FRAME");
272 break;
273 default:
274 printk("REQ(%02X) ", cmd->bRequest);
275 }
276
277 printk(" ");
278 usbip_dump_request_type(cmd->bRequestType);
279
280 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
281 printk("CLASS ");
282
283 else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR)
284 printk("VENDOR ");
285
286 else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED)
287 printk("RESERVED");
288
289 printk("\n");
290}
291
292void usbip_dump_urb(struct urb *urb)
293{
294 struct device *dev;
295
296 if (!urb) {
297 printk(KERN_DEBUG KBUILD_MODNAME
298 ":%s: urb: null pointer!!\n", __func__);
299 return;
300 }
301
302 if (!urb->dev) {
303 printk(KERN_DEBUG KBUILD_MODNAME
304 ":%s: urb->dev: null pointer!!\n", __func__);
305 return;
306 }
307 dev = &urb->dev->dev;
308
309 dev_dbg(dev, " urb :%p\n", urb);
310 dev_dbg(dev, " dev :%p\n", urb->dev);
311
312 usbip_dump_usb_device(urb->dev);
313
314 dev_dbg(dev, " pipe :%08x ", urb->pipe);
315
316 usbip_dump_pipe(urb->pipe);
317
318 dev_dbg(dev, " status :%d\n", urb->status);
319 dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
320 dev_dbg(dev, " transfer_buffer :%p\n", urb->transfer_buffer);
321 dev_dbg(dev, " transfer_buffer_length:%d\n", urb->transfer_buffer_length);
322 dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
323 dev_dbg(dev, " setup_packet :%p\n", urb->setup_packet);
324
325 if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
326 usbip_dump_usb_ctrlrequest(
327 (struct usb_ctrlrequest *)urb->setup_packet);
328
329 dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
330 dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
331 dev_dbg(dev, " interval :%d\n", urb->interval);
332 dev_dbg(dev, " error_count :%d\n", urb->error_count);
333 dev_dbg(dev, " context :%p\n", urb->context);
334 dev_dbg(dev, " complete :%p\n", urb->complete);
335}
336EXPORT_SYMBOL_GPL(usbip_dump_urb);
337
338void usbip_dump_header(struct usbip_header *pdu)
339{
340 udbg("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
341 pdu->base.command,
342 pdu->base.seqnum,
343 pdu->base.devid,
344 pdu->base.direction,
345 pdu->base.ep);
346
347 switch (pdu->base.command) {
348 case USBIP_CMD_SUBMIT:
349 udbg("CMD_SUBMIT: x_flags %u x_len %u sf %u #p %u iv %u\n",
350 pdu->u.cmd_submit.transfer_flags,
351 pdu->u.cmd_submit.transfer_buffer_length,
352 pdu->u.cmd_submit.start_frame,
353 pdu->u.cmd_submit.number_of_packets,
354 pdu->u.cmd_submit.interval);
355 break;
356 case USBIP_CMD_UNLINK:
357 udbg("CMD_UNLINK: seq %u\n", pdu->u.cmd_unlink.seqnum);
358 break;
359 case USBIP_RET_SUBMIT:
360 udbg("RET_SUBMIT: st %d al %u sf %d ec %d\n",
361 pdu->u.ret_submit.status,
362 pdu->u.ret_submit.actual_length,
363 pdu->u.ret_submit.start_frame,
364 pdu->u.ret_submit.error_count);
365 case USBIP_RET_UNLINK:
366 udbg("RET_UNLINK: status %d\n", pdu->u.ret_unlink.status);
367 break;
368 default:
369 /* NOT REACHED */
370 udbg("UNKNOWN\n");
371 }
372}
373EXPORT_SYMBOL_GPL(usbip_dump_header);
374
375
376/*-------------------------------------------------------------------------*/
377/* thread routines */
378
379int usbip_thread(void *param)
380{
381 struct usbip_task *ut = param;
382
383 if (!ut)
384 return -EINVAL;
385
386 lock_kernel();
387 daemonize(ut->name);
388 allow_signal(SIGKILL);
389 ut->thread = current;
390 unlock_kernel();
391
392 /* srv.rb must wait for rx_thread starting */
393 complete(&ut->thread_done);
394
395 /* start of while loop */
396 ut->loop_ops(ut);
397
398 /* end of loop */
399 ut->thread = NULL;
400
401 complete_and_exit(&ut->thread_done, 0);
402}
403
404void usbip_start_threads(struct usbip_device *ud)
405{
406 /*
407 * threads are invoked per one device (per one connection).
408 */
409 kernel_thread(usbip_thread, (void *)&ud->tcp_rx, 0);
410 kernel_thread(usbip_thread, (void *)&ud->tcp_tx, 0);
411
412 /* confirm threads are starting */
413 wait_for_completion(&ud->tcp_rx.thread_done);
414 wait_for_completion(&ud->tcp_tx.thread_done);
415}
416EXPORT_SYMBOL_GPL(usbip_start_threads);
417
418void usbip_stop_threads(struct usbip_device *ud)
419{
420 /* kill threads related to this sdev, if v.c. exists */
421 if (ud->tcp_rx.thread != NULL) {
422 send_sig(SIGKILL, ud->tcp_rx.thread, 1);
423 wait_for_completion(&ud->tcp_rx.thread_done);
424 udbg("rx_thread for ud %p has finished\n", ud);
425 }
426
427 if (ud->tcp_tx.thread != NULL) {
428 send_sig(SIGKILL, ud->tcp_tx.thread, 1);
429 wait_for_completion(&ud->tcp_tx.thread_done);
430 udbg("tx_thread for ud %p has finished\n", ud);
431 }
432}
433EXPORT_SYMBOL_GPL(usbip_stop_threads);
434
435void usbip_task_init(struct usbip_task *ut, char *name,
436 void (*loop_ops)(struct usbip_task *))
437{
438 ut->thread = NULL;
439 init_completion(&ut->thread_done);
440 ut->name = name;
441 ut->loop_ops = loop_ops;
442}
443EXPORT_SYMBOL_GPL(usbip_task_init);
444
445
446/*-------------------------------------------------------------------------*/
447/* socket routines */
448
449 /* Send/receive messages over TCP/IP. I refer drivers/block/nbd.c */
450int usbip_xmit(int send, struct socket *sock, char *buf,
451 int size, int msg_flags)
452{
453 int result;
454 struct msghdr msg;
455 struct kvec iov;
456 int total = 0;
457
458 /* for blocks of if (dbg_flag_xmit) */
459 char *bp = buf;
460 int osize = size;
461
462 dbg_xmit("enter\n");
463
464 if (!sock || !buf || !size) {
465 printk(KERN_ERR "%s: invalid arg, sock %p buff %p size %d\n",
466 __func__, sock, buf, size);
467 return -EINVAL;
468 }
469
470
471 if (dbg_flag_xmit) {
472 if (send) {
473 if (!in_interrupt())
474 printk(KERN_DEBUG "%-10s:", current->comm);
475 else
476 printk(KERN_DEBUG "interupt :");
477
478 printk("%s: sending... , sock %p, buf %p, "
479 "size %d, msg_flags %d\n", __func__,
480 sock, buf, size, msg_flags);
481 usbip_dump_buffer(buf, size);
482 }
483 }
484
485
486 do {
487 sock->sk->sk_allocation = GFP_NOIO;
488 iov.iov_base = buf;
489 iov.iov_len = size;
490 msg.msg_name = NULL;
491 msg.msg_namelen = 0;
492 msg.msg_control = NULL;
493 msg.msg_controllen = 0;
494 msg.msg_namelen = 0;
495 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
496
497 if (send)
498 result = kernel_sendmsg(sock, &msg, &iov, 1, size);
499 else
500 result = kernel_recvmsg(sock, &msg, &iov, 1, size,
501 MSG_WAITALL);
502
503 if (result <= 0) {
504 udbg("usbip_xmit: %s sock %p buf %p size %u ret %d"
505 " total %d\n",
506 send ? "send" : "receive", sock, buf,
507 size, result, total);
508 goto err;
509 }
510
511 size -= result;
512 buf += result;
513 total += result;
514
515 } while (size > 0);
516
517
518 if (dbg_flag_xmit) {
519 if (!send) {
520 if (!in_interrupt())
521 printk(KERN_DEBUG "%-10s:", current->comm);
522 else
523 printk(KERN_DEBUG "interupt :");
524
525 printk("usbip_xmit: receiving....\n");
526 usbip_dump_buffer(bp, osize);
527 printk("usbip_xmit: received, osize %d ret %d size %d "
528 "total %d\n", osize, result, size,
529 total);
530 }
531
532 if (send)
533 printk("usbip_xmit: send, total %d\n", total);
534 }
535
536 return total;
537
538err:
539 return result;
540}
541EXPORT_SYMBOL_GPL(usbip_xmit);
542
543
544/* now a usrland utility should set options. */
545#if 0
546int setquickack(struct socket *socket)
547{
548 mm_segment_t oldfs;
549 int val = 1;
550 int ret;
551
552 oldfs = get_fs();
553 set_fs(get_ds());
554 ret = socket->ops->setsockopt(socket, SOL_TCP, TCP_QUICKACK,
555 (char __user *) &val, sizeof(ret));
556 set_fs(oldfs);
557
558 return ret;
559}
560
561int setnodelay(struct socket *socket)
562{
563 mm_segment_t oldfs;
564 int val = 1;
565 int ret;
566
567 oldfs = get_fs();
568 set_fs(get_ds());
569 ret = socket->ops->setsockopt(socket, SOL_TCP, TCP_NODELAY,
570 (char __user *) &val, sizeof(ret));
571 set_fs(oldfs);
572
573 return ret;
574}
575
576int setkeepalive(struct socket *socket)
577{
578 mm_segment_t oldfs;
579 int val = 1;
580 int ret;
581
582 oldfs = get_fs();
583 set_fs(get_ds());
584 ret = socket->ops->setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE,
585 (char __user *) &val, sizeof(ret));
586 set_fs(oldfs);
587
588 return ret;
589}
590
591void setreuse(struct socket *socket)
592{
593 socket->sk->sk_reuse = 1;
594}
595#endif
596
597struct socket *sockfd_to_socket(unsigned int sockfd)
598{
599 struct socket *socket;
600 struct file *file;
601 struct inode *inode;
602
603 file = fget(sockfd);
604 if (!file) {
605 printk(KERN_ERR "%s: invalid sockfd\n", __func__);
606 return NULL;
607 }
608
609 inode = file->f_dentry->d_inode;
610
611 if (!inode || !S_ISSOCK(inode->i_mode))
612 return NULL;
613
614 socket = SOCKET_I(inode);
615
616 return socket;
617}
618EXPORT_SYMBOL_GPL(sockfd_to_socket);
619
620
621
622/*-------------------------------------------------------------------------*/
623/* pdu routines */
624
625/* there may be more cases to tweak the flags. */
626static unsigned int tweak_transfer_flags(unsigned int flags)
627{
628
629 if (flags & URB_NO_TRANSFER_DMA_MAP)
630 /*
631 * vhci_hcd does not provide DMA-mapped I/O. The upper
632 * driver does not need to set this flag. The remote
633 * usbip.ko does not still perform DMA-mapped I/O for
634 * DMA-caplable host controllers. So, clear this flag.
635 */
636 flags &= ~URB_NO_TRANSFER_DMA_MAP;
637
638 if (flags & URB_NO_SETUP_DMA_MAP)
639 flags &= ~URB_NO_SETUP_DMA_MAP;
640
641 return flags;
642}
643
644static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
645 int pack)
646{
647 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
648
649 /*
650 * Some members are not still implemented in usbip. I hope this issue
651 * will be discussed when usbip is ported to other operating systems.
652 */
653 if (pack) {
654 /* vhci_tx.c */
655 spdu->transfer_flags =
656 tweak_transfer_flags(urb->transfer_flags);
657 spdu->transfer_buffer_length = urb->transfer_buffer_length;
658 spdu->start_frame = urb->start_frame;
659 spdu->number_of_packets = urb->number_of_packets;
660 spdu->interval = urb->interval;
661 } else {
662 /* stub_rx.c */
663 urb->transfer_flags = spdu->transfer_flags;
664
665 urb->transfer_buffer_length = spdu->transfer_buffer_length;
666 urb->start_frame = spdu->start_frame;
667 urb->number_of_packets = spdu->number_of_packets;
668 urb->interval = spdu->interval;
669 }
670}
671
672static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
673 int pack)
674{
675 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
676
677 if (pack) {
678 /* stub_tx.c */
679
680 rpdu->status = urb->status;
681 rpdu->actual_length = urb->actual_length;
682 rpdu->start_frame = urb->start_frame;
683 rpdu->error_count = urb->error_count;
684 } else {
685 /* vhci_rx.c */
686
687 urb->status = rpdu->status;
688 urb->actual_length = rpdu->actual_length;
689 urb->start_frame = rpdu->start_frame;
690 urb->error_count = rpdu->error_count;
691 }
692}
693
694
695void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
696 int pack)
697{
698 switch (cmd) {
699 case USBIP_CMD_SUBMIT:
700 usbip_pack_cmd_submit(pdu, urb, pack);
701 break;
702 case USBIP_RET_SUBMIT:
703 usbip_pack_ret_submit(pdu, urb, pack);
704 break;
705 default:
706 err("unknown command");
707 /* NOTREACHED */
708 /* BUG(); */
709 }
710}
711EXPORT_SYMBOL_GPL(usbip_pack_pdu);
712
713
714static void correct_endian_basic(struct usbip_header_basic *base, int send)
715{
716 if (send) {
717 base->command = cpu_to_be32(base->command);
718 base->seqnum = cpu_to_be32(base->seqnum);
719 base->devid = cpu_to_be32(base->devid);
720 base->direction = cpu_to_be32(base->direction);
721 base->ep = cpu_to_be32(base->ep);
722 } else {
723 base->command = be32_to_cpu(base->command);
724 base->seqnum = be32_to_cpu(base->seqnum);
725 base->devid = be32_to_cpu(base->devid);
726 base->direction = be32_to_cpu(base->direction);
727 base->ep = be32_to_cpu(base->ep);
728 }
729}
730
731static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
732 int send)
733{
734 if (send) {
735 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
736
737 cpu_to_be32s(&pdu->transfer_buffer_length);
738 cpu_to_be32s(&pdu->start_frame);
739 cpu_to_be32s(&pdu->number_of_packets);
740 cpu_to_be32s(&pdu->interval);
741 } else {
742 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
743
744 be32_to_cpus(&pdu->transfer_buffer_length);
745 be32_to_cpus(&pdu->start_frame);
746 be32_to_cpus(&pdu->number_of_packets);
747 be32_to_cpus(&pdu->interval);
748 }
749}
750
751static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
752 int send)
753{
754 if (send) {
755 cpu_to_be32s(&pdu->status);
756 cpu_to_be32s(&pdu->actual_length);
757 cpu_to_be32s(&pdu->start_frame);
758 cpu_to_be32s(&pdu->error_count);
759 } else {
760 be32_to_cpus(&pdu->status);
761 be32_to_cpus(&pdu->actual_length);
762 be32_to_cpus(&pdu->start_frame);
763 be32_to_cpus(&pdu->error_count);
764 }
765}
766
767static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
768 int send)
769{
770 if (send)
771 pdu->seqnum = cpu_to_be32(pdu->seqnum);
772 else
773 pdu->seqnum = be32_to_cpu(pdu->seqnum);
774}
775
776static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
777 int send)
778{
779 if (send)
780 cpu_to_be32s(&pdu->status);
781 else
782 be32_to_cpus(&pdu->status);
783}
784
785void usbip_header_correct_endian(struct usbip_header *pdu, int send)
786{
787 __u32 cmd = 0;
788
789 if (send)
790 cmd = pdu->base.command;
791
792 correct_endian_basic(&pdu->base, send);
793
794 if (!send)
795 cmd = pdu->base.command;
796
797 switch (cmd) {
798 case USBIP_CMD_SUBMIT:
799 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
800 break;
801 case USBIP_RET_SUBMIT:
802 correct_endian_ret_submit(&pdu->u.ret_submit, send);
803 break;
804 case USBIP_CMD_UNLINK:
805 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
806 break;
807 case USBIP_RET_UNLINK:
808 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
809 break;
810 default:
811 /* NOTREACHED */
812 err("unknown command in pdu header: %d", cmd);
813 /* BUG(); */
814 }
815}
816EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
817
818static void usbip_iso_pakcet_correct_endian(
819 struct usbip_iso_packet_descriptor *iso,
820 int send)
821{
822 /* does not need all members. but copy all simply. */
823 if (send) {
824 iso->offset = cpu_to_be32(iso->offset);
825 iso->length = cpu_to_be32(iso->length);
826 iso->status = cpu_to_be32(iso->status);
827 iso->actual_length = cpu_to_be32(iso->actual_length);
828 } else {
829 iso->offset = be32_to_cpu(iso->offset);
830 iso->length = be32_to_cpu(iso->length);
831 iso->status = be32_to_cpu(iso->status);
832 iso->actual_length = be32_to_cpu(iso->actual_length);
833 }
834}
835
836static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
837 struct usb_iso_packet_descriptor *uiso, int pack)
838{
839 if (pack) {
840 iso->offset = uiso->offset;
841 iso->length = uiso->length;
842 iso->status = uiso->status;
843 iso->actual_length = uiso->actual_length;
844 } else {
845 uiso->offset = iso->offset;
846 uiso->length = iso->length;
847 uiso->status = iso->status;
848 uiso->actual_length = iso->actual_length;
849 }
850}
851
852
853/* must free buffer */
854void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
855{
856 void *buff;
857 struct usbip_iso_packet_descriptor *iso;
858 int np = urb->number_of_packets;
859 ssize_t size = np * sizeof(*iso);
860 int i;
861
862 buff = kzalloc(size, GFP_KERNEL);
863 if (!buff)
864 return NULL;
865
866 for (i = 0; i < np; i++) {
867 iso = buff + (i * sizeof(*iso));
868
869 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 1);
870 usbip_iso_pakcet_correct_endian(iso, 1);
871 }
872
873 *bufflen = size;
874
875 return buff;
876}
877EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
878
879/* some members of urb must be substituted before. */
880int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
881{
882 void *buff;
883 struct usbip_iso_packet_descriptor *iso;
884 int np = urb->number_of_packets;
885 int size = np * sizeof(*iso);
886 int i;
887 int ret;
888
889 if (!usb_pipeisoc(urb->pipe))
890 return 0;
891
892 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
893 if (np == 0) {
894 /* uinfo("iso np == 0\n"); */
895 /* usbip_dump_urb(urb); */
896 return 0;
897 }
898
899 buff = kzalloc(size, GFP_KERNEL);
900 if (!buff)
901 return -ENOMEM;
902
903 ret = usbip_xmit(0, ud->tcp_socket, buff, size, 0);
904 if (ret != size) {
905 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
906 ret);
907 kfree(buff);
908
909 if (ud->side == USBIP_STUB)
910 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
911 else
912 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
913
914 return -EPIPE;
915 }
916
917 for (i = 0; i < np; i++) {
918 iso = buff + (i * sizeof(*iso));
919
920 usbip_iso_pakcet_correct_endian(iso, 0);
921 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0);
922 }
923
924
925 kfree(buff);
926
927 return ret;
928}
929EXPORT_SYMBOL_GPL(usbip_recv_iso);
930
931
932/* some members of urb must be substituted before. */
933int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
934{
935 int ret;
936 int size;
937
938 if (ud->side == USBIP_STUB) {
939 /* stub_rx.c */
940 /* the direction of urb must be OUT. */
941 if (usb_pipein(urb->pipe))
942 return 0;
943
944 size = urb->transfer_buffer_length;
945 } else {
946 /* vhci_rx.c */
947 /* the direction of urb must be IN. */
948 if (usb_pipeout(urb->pipe))
949 return 0;
950
951 size = urb->actual_length;
952 }
953
954 /* no need to recv xbuff */
955 if (!(size > 0))
956 return 0;
957
958 ret = usbip_xmit(0, ud->tcp_socket, (char *)urb->transfer_buffer,
959 size, 0);
960 if (ret != size) {
961 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
962 if (ud->side == USBIP_STUB) {
963 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
964 } else {
965 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
966 return -EPIPE;
967 }
968 }
969
970 return ret;
971}
972EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
973
974
975/*-------------------------------------------------------------------------*/
976
977static int __init usbip_common_init(void)
978{
979 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "" DRIVER_VERSION);
980
981 return 0;
982}
983
984static void __exit usbip_common_exit(void)
985{
986 return;
987}
988
989
990
991
992module_init(usbip_common_init);
993module_exit(usbip_common_exit);
994
995MODULE_AUTHOR(DRIVER_AUTHOR);
996MODULE_DESCRIPTION(DRIVER_DESC);
997MODULE_LICENSE("GPL");
diff --git a/drivers/staging/usbip/usbip_common.h b/drivers/staging/usbip/usbip_common.h
new file mode 100644
index 000000000000..b0186b766375
--- /dev/null
+++ b/drivers/staging/usbip/usbip_common.h
@@ -0,0 +1,406 @@
1/*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
20#ifndef __VHCI_COMMON_H
21#define __VHCI_COMMON_H
22
23
24#include <linux/version.h>
25#include <linux/usb.h>
26#include <asm/byteorder.h>
27#include <net/sock.h>
28
29/*-------------------------------------------------------------------------*/
30
31/*
32 * define macros to print messages
33 */
34
35/**
36 * udbg - print debug messages if CONFIG_USB_DEBUG is defined
37 * @fmt:
38 * @args:
39 */
40
41#ifdef CONFIG_USB_DEBUG
42
43#define udbg(fmt, args...) \
44 do { \
45 printk(KERN_DEBUG "%-10s:(%s,%d) %s: " fmt, \
46 (in_interrupt() ? "interrupt" : (current)->comm),\
47 __FILE__, __LINE__, __func__, ##args); \
48 } while (0)
49
50#else /* CONFIG_USB_DEBUG */
51
52#define udbg(fmt, args...) do { } while (0)
53
54#endif /* CONFIG_USB_DEBUG */
55
56
57enum {
58 usbip_debug_xmit = (1 << 0),
59 usbip_debug_sysfs = (1 << 1),
60 usbip_debug_urb = (1 << 2),
61 usbip_debug_eh = (1 << 3),
62
63 usbip_debug_stub_cmp = (1 << 8),
64 usbip_debug_stub_dev = (1 << 9),
65 usbip_debug_stub_rx = (1 << 10),
66 usbip_debug_stub_tx = (1 << 11),
67
68 usbip_debug_vhci_rh = (1 << 8),
69 usbip_debug_vhci_hc = (1 << 9),
70 usbip_debug_vhci_rx = (1 << 10),
71 usbip_debug_vhci_tx = (1 << 11),
72 usbip_debug_vhci_sysfs = (1 << 12)
73};
74
75#define dbg_flag_xmit (usbip_debug_flag & usbip_debug_xmit)
76#define dbg_flag_vhci_rh (usbip_debug_flag & usbip_debug_vhci_rh)
77#define dbg_flag_vhci_hc (usbip_debug_flag & usbip_debug_vhci_hc)
78#define dbg_flag_vhci_rx (usbip_debug_flag & usbip_debug_vhci_rx)
79#define dbg_flag_vhci_tx (usbip_debug_flag & usbip_debug_vhci_tx)
80#define dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs)
81#define dbg_flag_stub_rx (usbip_debug_flag & usbip_debug_stub_rx)
82#define dbg_flag_stub_tx (usbip_debug_flag & usbip_debug_stub_tx)
83
84extern unsigned long usbip_debug_flag;
85extern struct device_attribute dev_attr_usbip_debug;
86
87#define dbg_with_flag(flag, fmt, args...) \
88 do { \
89 if (flag & usbip_debug_flag) \
90 udbg(fmt , ##args); \
91 } while (0)
92
93#define dbg_sysfs(fmt, args...) \
94 dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
95#define dbg_xmit(fmt, args...) \
96 dbg_with_flag(usbip_debug_xmit, fmt , ##args)
97#define dbg_urb(fmt, args...) \
98 dbg_with_flag(usbip_debug_urb, fmt , ##args)
99#define dbg_eh(fmt, args...) \
100 dbg_with_flag(usbip_debug_eh, fmt , ##args)
101
102#define dbg_vhci_rh(fmt, args...) \
103 dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args)
104#define dbg_vhci_hc(fmt, args...) \
105 dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args)
106#define dbg_vhci_rx(fmt, args...) \
107 dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args)
108#define dbg_vhci_tx(fmt, args...) \
109 dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args)
110#define dbg_vhci_sysfs(fmt, args...) \
111 dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
112
113#define dbg_stub_cmp(fmt, args...) \
114 dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
115#define dbg_stub_rx(fmt, args...) \
116 dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
117#define dbg_stub_tx(fmt, args...) \
118 dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
119
120
121/**
122 * uerr - print error messages
123 * @fmt:
124 * @args:
125 */
126#define uerr(fmt, args...) \
127 do { \
128 printk(KERN_ERR "%-10s: ***ERROR*** (%s,%d) %s: " fmt, \
129 (in_interrupt() ? "interrupt" : (current)->comm),\
130 __FILE__, __LINE__, __func__, ##args); \
131 } while (0)
132
133/**
134 * uinfo - print information messages
135 * @fmt:
136 * @args:
137 */
138#define uinfo(fmt, args...) \
139 do { \
140 printk(KERN_INFO "usbip: " fmt , ## args); \
141 } while (0)
142
143
144/*-------------------------------------------------------------------------*/
145
146/*
147 * USB/IP request headers.
148 * Currently, we define 4 request types:
149 *
150 * - CMD_SUBMIT transfers a USB request, corresponding to usb_submit_urb().
151 * (client to server)
152 * - RET_RETURN transfers the result of CMD_SUBMIT.
153 * (server to client)
154 * - CMD_UNLINK transfers an unlink request of a pending USB request.
155 * (client to server)
156 * - RET_UNLINK transfers the result of CMD_UNLINK.
157 * (server to client)
158 *
159 * Note: The below request formats are based on the USB subsystem of Linux. Its
160 * details will be defined when other implementations come.
161 *
162 *
163 */
164
165/*
166 * A basic header followed by other additional headers.
167 */
168struct usbip_header_basic {
169#define USBIP_CMD_SUBMIT 0x0001
170#define USBIP_CMD_UNLINK 0x0002
171#define USBIP_RET_SUBMIT 0x0003
172#define USBIP_RET_UNLINK 0x0004
173 __u32 command;
174
175 /* sequencial number which identifies requests.
176 * incremented per connections */
177 __u32 seqnum;
178
179 /* devid is used to specify a remote USB device uniquely instead
180 * of busnum and devnum in Linux. In the case of Linux stub_driver,
181 * this value is ((busnum << 16) | devnum) */
182 __u32 devid;
183
184#define USBIP_DIR_OUT 0
185#define USBIP_DIR_IN 1
186 __u32 direction;
187 __u32 ep; /* endpoint number */
188} __attribute__ ((packed));
189
190/*
191 * An additional header for a CMD_SUBMIT packet.
192 */
193struct usbip_header_cmd_submit {
194 /* these values are basically the same as in a URB. */
195
196 /* the same in a URB. */
197 __u32 transfer_flags;
198
199 /* set the following data size (out),
200 * or expected reading data size (in) */
201 __s32 transfer_buffer_length;
202
203 /* it is difficult for usbip to sync frames (reserved only?) */
204 __s32 start_frame;
205
206 /* the number of iso descriptors that follows this header */
207 __s32 number_of_packets;
208
209 /* the maximum time within which this request works in a host
210 * controller of a server side */
211 __s32 interval;
212
213 /* set setup packet data for a CTRL request */
214 unsigned char setup[8];
215} __attribute__ ((packed));
216
217/*
218 * An additional header for a RET_SUBMIT packet.
219 */
220struct usbip_header_ret_submit {
221 __s32 status;
222 __s32 actual_length; /* returned data length */
223 __s32 start_frame; /* ISO and INT */
224 __s32 number_of_packets; /* ISO only */
225 __s32 error_count; /* ISO only */
226} __attribute__ ((packed));
227
228/*
229 * An additional header for a CMD_UNLINK packet.
230 */
231struct usbip_header_cmd_unlink {
232 __u32 seqnum; /* URB's seqnum which will be unlinked */
233} __attribute__ ((packed));
234
235
236/*
237 * An additional header for a RET_UNLINK packet.
238 */
239struct usbip_header_ret_unlink {
240 __s32 status;
241} __attribute__ ((packed));
242
243
244/* the same as usb_iso_packet_descriptor but packed for pdu */
245struct usbip_iso_packet_descriptor {
246 __u32 offset;
247 __u32 length; /* expected length */
248 __u32 actual_length;
249 __u32 status;
250} __attribute__ ((packed));
251
252
253/*
254 * All usbip packets use a common header to keep code simple.
255 */
256struct usbip_header {
257 struct usbip_header_basic base;
258
259 union {
260 struct usbip_header_cmd_submit cmd_submit;
261 struct usbip_header_ret_submit ret_submit;
262 struct usbip_header_cmd_unlink cmd_unlink;
263 struct usbip_header_ret_unlink ret_unlink;
264 } u;
265} __attribute__ ((packed));
266
267
268
269
270/*-------------------------------------------------------------------------*/
271
272
273int usbip_xmit(int, struct socket *, char *, int, int);
274int usbip_sendmsg(struct socket *, struct msghdr *, int);
275
276
277static inline int interface_to_busnum(struct usb_interface *interface)
278{
279 struct usb_device *udev = interface_to_usbdev(interface);
280 return udev->bus->busnum;
281}
282
283static inline int interface_to_devnum(struct usb_interface *interface)
284{
285 struct usb_device *udev = interface_to_usbdev(interface);
286 return udev->devnum;
287}
288
289static inline int interface_to_infnum(struct usb_interface *interface)
290{
291 return interface->cur_altsetting->desc.bInterfaceNumber;
292}
293
294#if 0
295int setnodelay(struct socket *);
296int setquickack(struct socket *);
297int setkeepalive(struct socket *socket);
298void setreuse(struct socket *);
299#endif
300
301struct socket *sockfd_to_socket(unsigned int);
302int set_sockaddr(struct socket *socket, struct sockaddr_storage *ss);
303
304void usbip_dump_urb(struct urb *purb);
305void usbip_dump_header(struct usbip_header *pdu);
306
307
308struct usbip_device;
309
310struct usbip_task {
311 struct task_struct *thread;
312 struct completion thread_done;
313 char *name;
314 void (*loop_ops)(struct usbip_task *);
315};
316
317enum usbip_side {
318 USBIP_VHCI,
319 USBIP_STUB,
320};
321
322enum usbip_status {
323 /* sdev is available. */
324 SDEV_ST_AVAILABLE = 0x01,
325 /* sdev is now used. */
326 SDEV_ST_USED,
327 /* sdev is unusable because of a fatal error. */
328 SDEV_ST_ERROR,
329
330 /* vdev does not connect a remote device. */
331 VDEV_ST_NULL,
332 /* vdev is used, but the USB address is not assigned yet */
333 VDEV_ST_NOTASSIGNED,
334 VDEV_ST_USED,
335 VDEV_ST_ERROR
336};
337
338/* a common structure for stub_device and vhci_device */
339struct usbip_device {
340 enum usbip_side side;
341
342 enum usbip_status status;
343
344 /* lock for status */
345 spinlock_t lock;
346
347 struct socket *tcp_socket;
348
349 struct usbip_task tcp_rx;
350 struct usbip_task tcp_tx;
351
352 /* event handler */
353#define USBIP_EH_SHUTDOWN (1 << 0)
354#define USBIP_EH_BYE (1 << 1)
355#define USBIP_EH_RESET (1 << 2)
356#define USBIP_EH_UNUSABLE (1 << 3)
357
358#define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
359#define SDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
360#define SDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
361#define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
362#define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
363
364#define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
365#define VDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
366#define VDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
367#define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
368
369 unsigned long event;
370 struct usbip_task eh;
371 wait_queue_head_t eh_waitq;
372
373 struct eh_ops {
374 void (*shutdown)(struct usbip_device *);
375 void (*reset)(struct usbip_device *);
376 void (*unusable)(struct usbip_device *);
377 } eh_ops;
378};
379
380
381void usbip_task_init(struct usbip_task *ut, char *,
382 void (*loop_ops)(struct usbip_task *));
383
384void usbip_start_threads(struct usbip_device *ud);
385void usbip_stop_threads(struct usbip_device *ud);
386int usbip_thread(void *param);
387
388void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
389 int pack);
390
391void usbip_header_correct_endian(struct usbip_header *pdu, int send);
392/* some members of urb must be substituted before. */
393int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
394/* some members of urb must be substituted before. */
395int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
396void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
397
398
399/* usbip_event.c */
400void usbip_start_eh(struct usbip_device *ud);
401void usbip_stop_eh(struct usbip_device *ud);
402void usbip_event_add(struct usbip_device *ud, unsigned long event);
403int usbip_event_happend(struct usbip_device *ud);
404
405
406#endif
diff --git a/drivers/staging/usbip/usbip_event.c b/drivers/staging/usbip/usbip_event.c
new file mode 100644
index 000000000000..4318553ab20c
--- /dev/null
+++ b/drivers/staging/usbip/usbip_event.c
@@ -0,0 +1,141 @@
1/*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
20#include "usbip_common.h"
21
22static int event_handler(struct usbip_device *ud)
23{
24 dbg_eh("enter\n");
25
26 /*
27 * Events are handled by only this thread.
28 */
29 while (usbip_event_happend(ud)) {
30 dbg_eh("pending event %lx\n", ud->event);
31
32 /*
33 * NOTE: shutdown must come first.
34 * Shutdown the device.
35 */
36 if (ud->event & USBIP_EH_SHUTDOWN) {
37 ud->eh_ops.shutdown(ud);
38
39 ud->event &= ~USBIP_EH_SHUTDOWN;
40
41 break;
42 }
43
44 /* Stop the error handler. */
45 if (ud->event & USBIP_EH_BYE)
46 return -1;
47
48 /* Reset the device. */
49 if (ud->event & USBIP_EH_RESET) {
50 ud->eh_ops.reset(ud);
51
52 ud->event &= ~USBIP_EH_RESET;
53
54 break;
55 }
56
57 /* Mark the device as unusable. */
58 if (ud->event & USBIP_EH_UNUSABLE) {
59 ud->eh_ops.unusable(ud);
60
61 ud->event &= ~USBIP_EH_UNUSABLE;
62
63 break;
64 }
65
66 /* NOTREACHED */
67 printk(KERN_ERR "%s: unknown event\n", __func__);
68 return -1;
69 }
70
71 return 0;
72}
73
74static void event_handler_loop(struct usbip_task *ut)
75{
76 struct usbip_device *ud = container_of(ut, struct usbip_device, eh);
77
78 while (1) {
79 if (signal_pending(current)) {
80 dbg_eh("signal catched!\n");
81 break;
82 }
83
84 if (event_handler(ud) < 0)
85 break;
86
87 wait_event_interruptible(ud->eh_waitq, usbip_event_happend(ud));
88 dbg_eh("wakeup\n");
89 }
90}
91
92void usbip_start_eh(struct usbip_device *ud)
93{
94 struct usbip_task *eh = &ud->eh;
95
96 init_waitqueue_head(&ud->eh_waitq);
97 ud->event = 0;
98
99 usbip_task_init(eh, "usbip_eh", event_handler_loop);
100
101 kernel_thread(usbip_thread, (void *)eh, 0);
102
103 wait_for_completion(&eh->thread_done);
104}
105EXPORT_SYMBOL_GPL(usbip_start_eh);
106
107void usbip_stop_eh(struct usbip_device *ud)
108{
109 struct usbip_task *eh = &ud->eh;
110
111 wait_for_completion(&eh->thread_done);
112 dbg_eh("usbip_eh has finished\n");
113}
114EXPORT_SYMBOL_GPL(usbip_stop_eh);
115
116void usbip_event_add(struct usbip_device *ud, unsigned long event)
117{
118 spin_lock(&ud->lock);
119
120 ud->event |= event;
121
122 wake_up(&ud->eh_waitq);
123
124 spin_unlock(&ud->lock);
125}
126EXPORT_SYMBOL_GPL(usbip_event_add);
127
128int usbip_event_happend(struct usbip_device *ud)
129{
130 int happend = 0;
131
132 spin_lock(&ud->lock);
133
134 if (ud->event != 0)
135 happend = 1;
136
137 spin_unlock(&ud->lock);
138
139 return happend;
140}
141EXPORT_SYMBOL_GPL(usbip_event_happend);