Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzi...
[sfrench/cifs-2.6.git] / drivers / staging / usbip / usbip_common.c
1 /*
2  * Copyright (C) 2003-2008 Takahiro Hirofuchi
3  *
4  * This is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17  * USA.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/smp_lock.h>
22 #include <linux/file.h>
23 #include <linux/tcp.h>
24 #include <linux/in.h>
25 #include <linux/kthread.h>
26 #include <linux/slab.h>
27 #include "usbip_common.h"
28
29 /* version information */
30 #define DRIVER_VERSION "1.0"
31 #define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi _at_ users.sourceforge.net>"
32 #define DRIVER_DESC "usbip common driver"
33
34 /*-------------------------------------------------------------------------*/
35 /* debug routines */
36
37 #ifdef CONFIG_USB_IP_DEBUG_ENABLE
38 unsigned long usbip_debug_flag = 0xffffffff;
39 #else
40 unsigned long usbip_debug_flag;
41 #endif
42 EXPORT_SYMBOL_GPL(usbip_debug_flag);
43
44
45 /* FIXME */
46 struct device_attribute dev_attr_usbip_debug;
47 EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
48
49
50 static ssize_t show_flag(struct device *dev, struct device_attribute *attr,
51                                                                 char *buf)
52 {
53         return sprintf(buf, "%lx\n", usbip_debug_flag);
54 }
55
56 static ssize_t store_flag(struct device *dev, struct device_attribute *attr,
57                 const char *buf, size_t count)
58 {
59         sscanf(buf, "%lx", &usbip_debug_flag);
60
61         return count;
62 }
63 DEVICE_ATTR(usbip_debug, (S_IRUGO | S_IWUSR), show_flag, store_flag);
64
65 static void usbip_dump_buffer(char *buff, int bufflen)
66 {
67         print_hex_dump(KERN_DEBUG, "usb-ip", DUMP_PREFIX_OFFSET, 16, 4,
68                        buff, bufflen, false);
69 }
70
71 static void usbip_dump_pipe(unsigned int p)
72 {
73         unsigned char type = usb_pipetype(p);
74         unsigned char ep = usb_pipeendpoint(p);
75         unsigned char dev = usb_pipedevice(p);
76         unsigned char dir = usb_pipein(p);
77
78         printk(KERN_DEBUG "dev(%d) ", dev);
79         printk(KERN_DEBUG "ep(%d) ",  ep);
80         printk(KERN_DEBUG "%s ", dir ? "IN" : "OUT");
81
82         switch (type) {
83         case PIPE_ISOCHRONOUS:
84                 printk(KERN_DEBUG "%s ", "ISO");
85                 break;
86         case PIPE_INTERRUPT:
87                 printk(KERN_DEBUG "%s ", "INT");
88                 break;
89         case PIPE_CONTROL:
90                 printk(KERN_DEBUG "%s ", "CTL");
91                 break;
92         case PIPE_BULK:
93                 printk(KERN_DEBUG "%s ", "BLK");
94                 break;
95         default:
96                 printk(KERN_DEBUG "ERR");
97         }
98
99         printk(KERN_DEBUG "\n");
100
101 }
102
103 static void usbip_dump_usb_device(struct usb_device *udev)
104 {
105         struct device *dev = &udev->dev;
106         int i;
107
108         dev_dbg(dev, "       devnum(%d) devpath(%s)",
109                 udev->devnum, udev->devpath);
110
111         switch (udev->speed) {
112         case USB_SPEED_HIGH:
113                 printk(KERN_DEBUG " SPD_HIGH");
114                 break;
115         case USB_SPEED_FULL:
116                 printk(KERN_DEBUG " SPD_FULL");
117                 break;
118         case USB_SPEED_LOW:
119                 printk(KERN_DEBUG " SPD_LOW");
120                 break;
121         case USB_SPEED_UNKNOWN:
122                 printk(KERN_DEBUG " SPD_UNKNOWN");
123                 break;
124         default:
125                 printk(KERN_DEBUG " SPD_ERROR");
126         }
127
128         printk(KERN_DEBUG " tt %p, ttport %d", udev->tt, udev->ttport);
129         printk(KERN_DEBUG "\n");
130
131         dev_dbg(dev, "                    ");
132         for (i = 0; i < 16; i++)
133                 printk(KERN_DEBUG " %2u", i);
134         printk(KERN_DEBUG "\n");
135
136         dev_dbg(dev, "       toggle0(IN) :");
137         for (i = 0; i < 16; i++)
138                 printk(KERN_DEBUG " %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
139         printk(KERN_DEBUG "\n");
140
141         dev_dbg(dev, "       toggle1(OUT):");
142         for (i = 0; i < 16; i++)
143                 printk(KERN_DEBUG " %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
144         printk(KERN_DEBUG "\n");
145
146
147         dev_dbg(dev, "       epmaxp_in   :");
148         for (i = 0; i < 16; i++) {
149                 if (udev->ep_in[i])
150                         printk(KERN_DEBUG " %2u",
151                              le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
152         }
153         printk(KERN_DEBUG "\n");
154
155         dev_dbg(dev, "       epmaxp_out  :");
156         for (i = 0; i < 16; i++) {
157                 if (udev->ep_out[i])
158                         printk(KERN_DEBUG " %2u",
159                              le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
160         }
161         printk(KERN_DEBUG "\n");
162
163         dev_dbg(dev, "parent %p, bus %p\n", udev->parent, udev->bus);
164
165         dev_dbg(dev, "descriptor %p, config %p, actconfig %p, "
166                 "rawdescriptors %p\n", &udev->descriptor, udev->config,
167                 udev->actconfig, udev->rawdescriptors);
168
169         dev_dbg(dev, "have_langid %d, string_langid %d\n",
170                 udev->have_langid, udev->string_langid);
171
172         dev_dbg(dev, "maxchild %d, children %p\n",
173                 udev->maxchild, udev->children);
174 }
175
176 static void usbip_dump_request_type(__u8 rt)
177 {
178         switch (rt & USB_RECIP_MASK) {
179         case USB_RECIP_DEVICE:
180                 printk(KERN_DEBUG "DEVICE");
181                 break;
182         case USB_RECIP_INTERFACE:
183                 printk(KERN_DEBUG "INTERF");
184                 break;
185         case USB_RECIP_ENDPOINT:
186                 printk(KERN_DEBUG "ENDPOI");
187                 break;
188         case USB_RECIP_OTHER:
189                 printk(KERN_DEBUG "OTHER ");
190                 break;
191         default:
192                 printk(KERN_DEBUG "------");
193         }
194 }
195
196 static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
197 {
198         if (!cmd) {
199                 printk(KERN_DEBUG "      %s : null pointer\n", __func__);
200                 return;
201         }
202
203         printk(KERN_DEBUG "       ");
204         printk(KERN_DEBUG "bRequestType(%02X) ", cmd->bRequestType);
205         printk(KERN_DEBUG "bRequest(%02X) " , cmd->bRequest);
206         printk(KERN_DEBUG "wValue(%04X) ", cmd->wValue);
207         printk(KERN_DEBUG "wIndex(%04X) ", cmd->wIndex);
208         printk(KERN_DEBUG "wLength(%04X) ", cmd->wLength);
209
210         printk(KERN_DEBUG "\n       ");
211
212         if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
213                 printk(KERN_DEBUG "STANDARD ");
214                 switch (cmd->bRequest) {
215                 case USB_REQ_GET_STATUS:
216                         printk(KERN_DEBUG "GET_STATUS");
217                         break;
218                 case USB_REQ_CLEAR_FEATURE:
219                         printk(KERN_DEBUG "CLEAR_FEAT");
220                         break;
221                 case USB_REQ_SET_FEATURE:
222                         printk(KERN_DEBUG "SET_FEAT  ");
223                         break;
224                 case USB_REQ_SET_ADDRESS:
225                         printk(KERN_DEBUG "SET_ADDRRS");
226                         break;
227                 case USB_REQ_GET_DESCRIPTOR:
228                         printk(KERN_DEBUG "GET_DESCRI");
229                         break;
230                 case USB_REQ_SET_DESCRIPTOR:
231                         printk(KERN_DEBUG "SET_DESCRI");
232                         break;
233                 case USB_REQ_GET_CONFIGURATION:
234                         printk(KERN_DEBUG "GET_CONFIG");
235                         break;
236                 case USB_REQ_SET_CONFIGURATION:
237                         printk(KERN_DEBUG "SET_CONFIG");
238                         break;
239                 case USB_REQ_GET_INTERFACE:
240                         printk(KERN_DEBUG "GET_INTERF");
241                         break;
242                 case USB_REQ_SET_INTERFACE:
243                         printk(KERN_DEBUG "SET_INTERF");
244                         break;
245                 case USB_REQ_SYNCH_FRAME:
246                         printk(KERN_DEBUG "SYNC_FRAME");
247                         break;
248                 default:
249                         printk(KERN_DEBUG "REQ(%02X) ", cmd->bRequest);
250                 }
251
252                 printk(KERN_DEBUG " ");
253                 usbip_dump_request_type(cmd->bRequestType);
254
255         } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
256                 printk(KERN_DEBUG "CLASS   ");
257
258         else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR)
259                 printk(KERN_DEBUG "VENDOR  ");
260
261         else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED)
262                 printk(KERN_DEBUG "RESERVED");
263
264         printk(KERN_DEBUG "\n");
265 }
266
267 void usbip_dump_urb(struct urb *urb)
268 {
269         struct device *dev;
270
271         if (!urb) {
272                 printk(KERN_DEBUG KBUILD_MODNAME
273                        ":%s: urb: null pointer!!\n", __func__);
274                 return;
275         }
276
277         if (!urb->dev) {
278                 printk(KERN_DEBUG KBUILD_MODNAME
279                        ":%s: urb->dev: null pointer!!\n", __func__);
280                 return;
281         }
282         dev = &urb->dev->dev;
283
284         dev_dbg(dev, "   urb                   :%p\n", urb);
285         dev_dbg(dev, "   dev                   :%p\n", urb->dev);
286
287         usbip_dump_usb_device(urb->dev);
288
289         dev_dbg(dev, "   pipe                  :%08x ", urb->pipe);
290
291         usbip_dump_pipe(urb->pipe);
292
293         dev_dbg(dev, "   status                :%d\n", urb->status);
294         dev_dbg(dev, "   transfer_flags        :%08X\n", urb->transfer_flags);
295         dev_dbg(dev, "   transfer_buffer       :%p\n", urb->transfer_buffer);
296         dev_dbg(dev, "   transfer_buffer_length:%d\n",
297                                                 urb->transfer_buffer_length);
298         dev_dbg(dev, "   actual_length         :%d\n", urb->actual_length);
299         dev_dbg(dev, "   setup_packet          :%p\n", urb->setup_packet);
300
301         if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
302                         usbip_dump_usb_ctrlrequest(
303                         (struct usb_ctrlrequest *)urb->setup_packet);
304
305         dev_dbg(dev, "   start_frame           :%d\n", urb->start_frame);
306         dev_dbg(dev, "   number_of_packets     :%d\n", urb->number_of_packets);
307         dev_dbg(dev, "   interval              :%d\n", urb->interval);
308         dev_dbg(dev, "   error_count           :%d\n", urb->error_count);
309         dev_dbg(dev, "   context               :%p\n", urb->context);
310         dev_dbg(dev, "   complete              :%p\n", urb->complete);
311 }
312 EXPORT_SYMBOL_GPL(usbip_dump_urb);
313
314 void usbip_dump_header(struct usbip_header *pdu)
315 {
316         usbip_udbg("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
317                         pdu->base.command,
318                         pdu->base.seqnum,
319                         pdu->base.devid,
320                         pdu->base.direction,
321                         pdu->base.ep);
322
323         switch (pdu->base.command) {
324         case USBIP_CMD_SUBMIT:
325                 usbip_udbg("CMD_SUBMIT: "
326                                 "x_flags %u x_len %u sf %u #p %u iv %u\n",
327                                 pdu->u.cmd_submit.transfer_flags,
328                                 pdu->u.cmd_submit.transfer_buffer_length,
329                                 pdu->u.cmd_submit.start_frame,
330                                 pdu->u.cmd_submit.number_of_packets,
331                                 pdu->u.cmd_submit.interval);
332                                 break;
333         case USBIP_CMD_UNLINK:
334                 usbip_udbg("CMD_UNLINK: seq %u\n", pdu->u.cmd_unlink.seqnum);
335                 break;
336         case USBIP_RET_SUBMIT:
337                 usbip_udbg("RET_SUBMIT: st %d al %u sf %d ec %d\n",
338                                 pdu->u.ret_submit.status,
339                                 pdu->u.ret_submit.actual_length,
340                                 pdu->u.ret_submit.start_frame,
341                                 pdu->u.ret_submit.error_count);
342         case USBIP_RET_UNLINK:
343                 usbip_udbg("RET_UNLINK: status %d\n", pdu->u.ret_unlink.status);
344                 break;
345         default:
346                 /* NOT REACHED */
347                 usbip_udbg("UNKNOWN\n");
348         }
349 }
350 EXPORT_SYMBOL_GPL(usbip_dump_header);
351
352
353 /*-------------------------------------------------------------------------*/
354 /* thread routines */
355
356 int usbip_thread(void *param)
357 {
358         struct usbip_task *ut = param;
359
360         if (!ut)
361                 return -EINVAL;
362
363         lock_kernel();
364         daemonize(ut->name);
365         allow_signal(SIGKILL);
366         ut->thread = current;
367         unlock_kernel();
368
369         /* srv.rb must wait for rx_thread starting */
370         complete(&ut->thread_done);
371
372         /* start of while loop */
373         ut->loop_ops(ut);
374
375         /* end of loop */
376         ut->thread = NULL;
377
378         complete_and_exit(&ut->thread_done, 0);
379 }
380
381 static void stop_rx_thread(struct usbip_device *ud)
382 {
383         if (ud->tcp_rx.thread != NULL) {
384                 send_sig(SIGKILL, ud->tcp_rx.thread, 1);
385                 wait_for_completion(&ud->tcp_rx.thread_done);
386                 usbip_udbg("rx_thread for ud %p has finished\n", ud);
387         }
388 }
389
390 static void stop_tx_thread(struct usbip_device *ud)
391 {
392         if (ud->tcp_tx.thread != NULL) {
393                 send_sig(SIGKILL, ud->tcp_tx.thread, 1);
394                 wait_for_completion(&ud->tcp_tx.thread_done);
395                 usbip_udbg("tx_thread for ud %p has finished\n", ud);
396         }
397 }
398
399 int usbip_start_threads(struct usbip_device *ud)
400 {
401         /*
402          * threads are invoked per one device (per one connection).
403          */
404         struct task_struct *th;
405         int err = 0;
406
407         th = kthread_run(usbip_thread, (void *)&ud->tcp_rx, "usbip");
408         if (IS_ERR(th)) {
409                 printk(KERN_WARNING
410                         "Unable to start control thread\n");
411                 err = PTR_ERR(th);
412                 goto ust_exit;
413         }
414
415         th = kthread_run(usbip_thread, (void *)&ud->tcp_tx, "usbip");
416         if (IS_ERR(th)) {
417                 printk(KERN_WARNING
418                         "Unable to start control thread\n");
419                 err = PTR_ERR(th);
420                 goto tx_thread_err;
421         }
422
423         /* confirm threads are starting */
424         wait_for_completion(&ud->tcp_rx.thread_done);
425         wait_for_completion(&ud->tcp_tx.thread_done);
426
427         return 0;
428
429 tx_thread_err:
430         stop_rx_thread(ud);
431
432 ust_exit:
433         return err;
434 }
435 EXPORT_SYMBOL_GPL(usbip_start_threads);
436
437 void usbip_stop_threads(struct usbip_device *ud)
438 {
439         /* kill threads related to this sdev, if v.c. exists */
440         stop_rx_thread(ud);
441         stop_tx_thread(ud);
442 }
443 EXPORT_SYMBOL_GPL(usbip_stop_threads);
444
445 void usbip_task_init(struct usbip_task *ut, char *name,
446                 void (*loop_ops)(struct usbip_task *))
447 {
448         ut->thread = NULL;
449         init_completion(&ut->thread_done);
450         ut->name = name;
451         ut->loop_ops = loop_ops;
452 }
453 EXPORT_SYMBOL_GPL(usbip_task_init);
454
455
456 /*-------------------------------------------------------------------------*/
457 /* socket routines */
458
459  /*  Send/receive messages over TCP/IP. I refer drivers/block/nbd.c */
460 int usbip_xmit(int send, struct socket *sock, char *buf,
461                int size, int msg_flags)
462 {
463         int result;
464         struct msghdr msg;
465         struct kvec iov;
466         int total = 0;
467
468         /* for blocks of if (usbip_dbg_flag_xmit) */
469         char *bp = buf;
470         int osize = size;
471
472         usbip_dbg_xmit("enter\n");
473
474         if (!sock || !buf || !size) {
475                 printk(KERN_ERR "%s: invalid arg, sock %p buff %p size %d\n",
476                        __func__, sock, buf, size);
477                 return -EINVAL;
478         }
479
480
481         if (usbip_dbg_flag_xmit) {
482                 if (send) {
483                         if (!in_interrupt())
484                                 printk(KERN_DEBUG "%-10s:", current->comm);
485                         else
486                                 printk(KERN_DEBUG "interrupt  :");
487
488                         printk(KERN_DEBUG "%s: sending... , sock %p, buf %p, "
489                                "size %d, msg_flags %d\n", __func__,
490                                sock, buf, size, msg_flags);
491                         usbip_dump_buffer(buf, size);
492                 }
493         }
494
495
496         do {
497                 sock->sk->sk_allocation = GFP_NOIO;
498                 iov.iov_base    = buf;
499                 iov.iov_len     = size;
500                 msg.msg_name    = NULL;
501                 msg.msg_namelen = 0;
502                 msg.msg_control = NULL;
503                 msg.msg_controllen = 0;
504                 msg.msg_namelen    = 0;
505                 msg.msg_flags      = msg_flags | MSG_NOSIGNAL;
506
507                 if (send)
508                         result = kernel_sendmsg(sock, &msg, &iov, 1, size);
509                 else
510                         result = kernel_recvmsg(sock, &msg, &iov, 1, size,
511                                                                 MSG_WAITALL);
512
513                 if (result <= 0) {
514                         usbip_udbg("usbip_xmit: %s sock %p buf %p size %u ret "
515                                         "%d total %d\n",
516                                         send ? "send" : "receive", sock, buf,
517                                         size, result, total);
518                         goto err;
519                 }
520
521                 size -= result;
522                 buf += result;
523                 total += result;
524
525         } while (size > 0);
526
527
528         if (usbip_dbg_flag_xmit) {
529                 if (!send) {
530                         if (!in_interrupt())
531                                 printk(KERN_DEBUG "%-10s:", current->comm);
532                         else
533                                 printk(KERN_DEBUG "interrupt  :");
534
535                         printk(KERN_DEBUG "usbip_xmit: receiving....\n");
536                         usbip_dump_buffer(bp, osize);
537                         printk(KERN_DEBUG "usbip_xmit: received, osize %d ret "
538                                         "%d size %d total %d\n", osize, result,
539                                         size, total);
540                 }
541
542                 if (send)
543                         printk(KERN_DEBUG "usbip_xmit: send, total %d\n",
544                                                                         total);
545         }
546
547         return total;
548
549 err:
550         return result;
551 }
552 EXPORT_SYMBOL_GPL(usbip_xmit);
553
554 struct socket *sockfd_to_socket(unsigned int sockfd)
555 {
556         struct socket *socket;
557         struct file *file;
558         struct inode *inode;
559
560         file = fget(sockfd);
561         if (!file) {
562                 printk(KERN_ERR "%s: invalid sockfd\n", __func__);
563                 return NULL;
564         }
565
566         inode = file->f_dentry->d_inode;
567
568         if (!inode || !S_ISSOCK(inode->i_mode))
569                 return NULL;
570
571         socket = SOCKET_I(inode);
572
573         return socket;
574 }
575 EXPORT_SYMBOL_GPL(sockfd_to_socket);
576
577
578
579 /*-------------------------------------------------------------------------*/
580 /* pdu routines */
581
582 /* there may be more cases to tweak the flags. */
583 static unsigned int tweak_transfer_flags(unsigned int flags)
584 {
585         flags &= ~URB_NO_TRANSFER_DMA_MAP;
586         return flags;
587 }
588
589 static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
590                                                                 int pack)
591 {
592         struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
593
594         /*
595          * Some members are not still implemented in usbip. I hope this issue
596          * will be discussed when usbip is ported to other operating systems.
597          */
598         if (pack) {
599                 /* vhci_tx.c */
600                 spdu->transfer_flags =
601                                 tweak_transfer_flags(urb->transfer_flags);
602                 spdu->transfer_buffer_length    = urb->transfer_buffer_length;
603                 spdu->start_frame               = urb->start_frame;
604                 spdu->number_of_packets         = urb->number_of_packets;
605                 spdu->interval                  = urb->interval;
606         } else  {
607                 /* stub_rx.c */
608                 urb->transfer_flags         = spdu->transfer_flags;
609
610                 urb->transfer_buffer_length = spdu->transfer_buffer_length;
611                 urb->start_frame            = spdu->start_frame;
612                 urb->number_of_packets      = spdu->number_of_packets;
613                 urb->interval               = spdu->interval;
614         }
615 }
616
617 static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
618                                                                 int pack)
619 {
620         struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
621
622         if (pack) {
623                 /* stub_tx.c */
624
625                 rpdu->status            = urb->status;
626                 rpdu->actual_length     = urb->actual_length;
627                 rpdu->start_frame       = urb->start_frame;
628                 rpdu->error_count       = urb->error_count;
629         } else {
630                 /* vhci_rx.c */
631
632                 urb->status             = rpdu->status;
633                 urb->actual_length      = rpdu->actual_length;
634                 urb->start_frame        = rpdu->start_frame;
635                 urb->error_count        = rpdu->error_count;
636         }
637 }
638
639
640 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
641                                                                 int pack)
642 {
643         switch (cmd) {
644         case USBIP_CMD_SUBMIT:
645                 usbip_pack_cmd_submit(pdu, urb, pack);
646                 break;
647         case USBIP_RET_SUBMIT:
648                 usbip_pack_ret_submit(pdu, urb, pack);
649                 break;
650         default:
651                 err("unknown command");
652                 /* NOTREACHED */
653                 /* BUG(); */
654         }
655 }
656 EXPORT_SYMBOL_GPL(usbip_pack_pdu);
657
658
659 static void correct_endian_basic(struct usbip_header_basic *base, int send)
660 {
661         if (send) {
662                 base->command   = cpu_to_be32(base->command);
663                 base->seqnum    = cpu_to_be32(base->seqnum);
664                 base->devid     = cpu_to_be32(base->devid);
665                 base->direction = cpu_to_be32(base->direction);
666                 base->ep        = cpu_to_be32(base->ep);
667         } else {
668                 base->command   = be32_to_cpu(base->command);
669                 base->seqnum    = be32_to_cpu(base->seqnum);
670                 base->devid     = be32_to_cpu(base->devid);
671                 base->direction = be32_to_cpu(base->direction);
672                 base->ep        = be32_to_cpu(base->ep);
673         }
674 }
675
676 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
677                                                                 int send)
678 {
679         if (send) {
680                 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
681
682                 cpu_to_be32s(&pdu->transfer_buffer_length);
683                 cpu_to_be32s(&pdu->start_frame);
684                 cpu_to_be32s(&pdu->number_of_packets);
685                 cpu_to_be32s(&pdu->interval);
686         } else {
687                 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
688
689                 be32_to_cpus(&pdu->transfer_buffer_length);
690                 be32_to_cpus(&pdu->start_frame);
691                 be32_to_cpus(&pdu->number_of_packets);
692                 be32_to_cpus(&pdu->interval);
693         }
694 }
695
696 static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
697                                                                 int send)
698 {
699         if (send) {
700                 cpu_to_be32s(&pdu->status);
701                 cpu_to_be32s(&pdu->actual_length);
702                 cpu_to_be32s(&pdu->start_frame);
703                 cpu_to_be32s(&pdu->error_count);
704         } else {
705                 be32_to_cpus(&pdu->status);
706                 be32_to_cpus(&pdu->actual_length);
707                 be32_to_cpus(&pdu->start_frame);
708                 be32_to_cpus(&pdu->error_count);
709         }
710 }
711
712 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
713                                                                 int send)
714 {
715         if (send)
716                 pdu->seqnum = cpu_to_be32(pdu->seqnum);
717         else
718                 pdu->seqnum = be32_to_cpu(pdu->seqnum);
719 }
720
721 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
722                                                                 int send)
723 {
724         if (send)
725                 cpu_to_be32s(&pdu->status);
726         else
727                 be32_to_cpus(&pdu->status);
728 }
729
730 void usbip_header_correct_endian(struct usbip_header *pdu, int send)
731 {
732         __u32 cmd = 0;
733
734         if (send)
735                 cmd = pdu->base.command;
736
737         correct_endian_basic(&pdu->base, send);
738
739         if (!send)
740                 cmd = pdu->base.command;
741
742         switch (cmd) {
743         case USBIP_CMD_SUBMIT:
744                 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
745                 break;
746         case USBIP_RET_SUBMIT:
747                 correct_endian_ret_submit(&pdu->u.ret_submit, send);
748                 break;
749         case USBIP_CMD_UNLINK:
750                 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
751                 break;
752         case USBIP_RET_UNLINK:
753                 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
754                 break;
755         default:
756                 /* NOTREACHED */
757                 err("unknown command in pdu header: %d", cmd);
758                 /* BUG(); */
759         }
760 }
761 EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
762
763 static void usbip_iso_pakcet_correct_endian(
764                                 struct usbip_iso_packet_descriptor *iso,
765                                 int send)
766 {
767         /* does not need all members. but copy all simply. */
768         if (send) {
769                 iso->offset     = cpu_to_be32(iso->offset);
770                 iso->length     = cpu_to_be32(iso->length);
771                 iso->status     = cpu_to_be32(iso->status);
772                 iso->actual_length = cpu_to_be32(iso->actual_length);
773         } else {
774                 iso->offset     = be32_to_cpu(iso->offset);
775                 iso->length     = be32_to_cpu(iso->length);
776                 iso->status     = be32_to_cpu(iso->status);
777                 iso->actual_length = be32_to_cpu(iso->actual_length);
778         }
779 }
780
781 static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
782                 struct usb_iso_packet_descriptor *uiso, int pack)
783 {
784         if (pack) {
785                 iso->offset             = uiso->offset;
786                 iso->length             = uiso->length;
787                 iso->status             = uiso->status;
788                 iso->actual_length      = uiso->actual_length;
789         } else {
790                 uiso->offset            = iso->offset;
791                 uiso->length            = iso->length;
792                 uiso->status            = iso->status;
793                 uiso->actual_length     = iso->actual_length;
794         }
795 }
796
797
798 /* must free buffer */
799 void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
800 {
801         void *buff;
802         struct usbip_iso_packet_descriptor *iso;
803         int np = urb->number_of_packets;
804         ssize_t size = np * sizeof(*iso);
805         int i;
806
807         buff = kzalloc(size, GFP_KERNEL);
808         if (!buff)
809                 return NULL;
810
811         for (i = 0; i < np; i++) {
812                 iso = buff + (i * sizeof(*iso));
813
814                 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 1);
815                 usbip_iso_pakcet_correct_endian(iso, 1);
816         }
817
818         *bufflen = size;
819
820         return buff;
821 }
822 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
823
824 /* some members of urb must be substituted before. */
825 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
826 {
827         void *buff;
828         struct usbip_iso_packet_descriptor *iso;
829         int np = urb->number_of_packets;
830         int size = np * sizeof(*iso);
831         int i;
832         int ret;
833
834         if (!usb_pipeisoc(urb->pipe))
835                 return 0;
836
837         /* my Bluetooth dongle gets ISO URBs which are np = 0 */
838         if (np == 0) {
839                 /* usbip_uinfo("iso np == 0\n"); */
840                 /* usbip_dump_urb(urb); */
841                 return 0;
842         }
843
844         buff = kzalloc(size, GFP_KERNEL);
845         if (!buff)
846                 return -ENOMEM;
847
848         ret = usbip_xmit(0, ud->tcp_socket, buff, size, 0);
849         if (ret != size) {
850                 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
851                         ret);
852                 kfree(buff);
853
854                 if (ud->side == USBIP_STUB)
855                         usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
856                 else
857                         usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
858
859                 return -EPIPE;
860         }
861
862         for (i = 0; i < np; i++) {
863                 iso = buff + (i * sizeof(*iso));
864
865                 usbip_iso_pakcet_correct_endian(iso, 0);
866                 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0);
867         }
868
869         kfree(buff);
870
871         return ret;
872 }
873 EXPORT_SYMBOL_GPL(usbip_recv_iso);
874
875
876 /* some members of urb must be substituted before. */
877 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
878 {
879         int ret;
880         int size;
881
882         if (ud->side == USBIP_STUB) {
883                 /* stub_rx.c */
884                 /* the direction of urb must be OUT. */
885                 if (usb_pipein(urb->pipe))
886                         return 0;
887
888                 size = urb->transfer_buffer_length;
889         } else {
890                 /* vhci_rx.c */
891                 /* the direction of urb must be IN. */
892                 if (usb_pipeout(urb->pipe))
893                         return 0;
894
895                 size = urb->actual_length;
896         }
897
898         /* no need to recv xbuff */
899         if (!(size > 0))
900                 return 0;
901
902         ret = usbip_xmit(0, ud->tcp_socket, (char *)urb->transfer_buffer,
903                          size, 0);
904         if (ret != size) {
905                 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
906                 if (ud->side == USBIP_STUB) {
907                         usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
908                 } else {
909                         usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
910                         return -EPIPE;
911                 }
912         }
913
914         return ret;
915 }
916 EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
917
918
919 /*-------------------------------------------------------------------------*/
920
921 static int __init usbip_common_init(void)
922 {
923         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "" DRIVER_VERSION);
924
925         return 0;
926 }
927
928 static void __exit usbip_common_exit(void)
929 {
930         return;
931 }
932
933
934
935
936 module_init(usbip_common_init);
937 module_exit(usbip_common_exit);
938
939 MODULE_AUTHOR(DRIVER_AUTHOR);
940 MODULE_DESCRIPTION(DRIVER_DESC);
941 MODULE_LICENSE("GPL");