pandora: defconfig: update
[pandora-kernel.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 <asm/byteorder.h>
21 #include <linux/file.h>
22 #include <linux/fs.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include <net/sock.h>
27
28 #include "usbip_common.h"
29
30 #define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>"
31 #define DRIVER_DESC "USB/IP Core"
32
33 #ifdef CONFIG_USBIP_DEBUG
34 unsigned long usbip_debug_flag = 0xffffffff;
35 #else
36 unsigned long usbip_debug_flag;
37 #endif
38 EXPORT_SYMBOL_GPL(usbip_debug_flag);
39
40 /* FIXME */
41 struct device_attribute dev_attr_usbip_debug;
42 EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
43
44 static ssize_t show_flag(struct device *dev, struct device_attribute *attr,
45                          char *buf)
46 {
47         return sprintf(buf, "%lx\n", usbip_debug_flag);
48 }
49
50 static ssize_t store_flag(struct device *dev, struct device_attribute *attr,
51                           const char *buf, size_t count)
52 {
53         sscanf(buf, "%lx", &usbip_debug_flag);
54         return count;
55 }
56 DEVICE_ATTR(usbip_debug, (S_IRUGO | S_IWUSR), show_flag, store_flag);
57
58 static void usbip_dump_buffer(char *buff, int bufflen)
59 {
60         print_hex_dump(KERN_DEBUG, "usbip-core", DUMP_PREFIX_OFFSET, 16, 4,
61                        buff, bufflen, false);
62 }
63
64 static void usbip_dump_pipe(unsigned int p)
65 {
66         unsigned char type = usb_pipetype(p);
67         unsigned char ep   = usb_pipeendpoint(p);
68         unsigned char dev  = usb_pipedevice(p);
69         unsigned char dir  = usb_pipein(p);
70
71         pr_debug("dev(%d) ep(%d) [%s] ", dev, ep, dir ? "IN" : "OUT");
72
73         switch (type) {
74         case PIPE_ISOCHRONOUS:
75                 pr_debug("ISO\n");
76                 break;
77         case PIPE_INTERRUPT:
78                 pr_debug("INT\n");
79                 break;
80         case PIPE_CONTROL:
81                 pr_debug("CTRL\n");
82                 break;
83         case PIPE_BULK:
84                 pr_debug("BULK\n");
85                 break;
86         default:
87                 pr_debug("ERR\n");
88                 break;
89         }
90 }
91
92 static void usbip_dump_usb_device(struct usb_device *udev)
93 {
94         struct device *dev = &udev->dev;
95         int i;
96
97         dev_dbg(dev, "       devnum(%d) devpath(%s) ",
98                 udev->devnum, udev->devpath);
99
100         switch (udev->speed) {
101         case USB_SPEED_HIGH:
102                 pr_debug("SPD_HIGH ");
103                 break;
104         case USB_SPEED_FULL:
105                 pr_debug("SPD_FULL ");
106                 break;
107         case USB_SPEED_LOW:
108                 pr_debug("SPD_LOW ");
109                 break;
110         case USB_SPEED_UNKNOWN:
111                 pr_debug("SPD_UNKNOWN ");
112                 break;
113         default:
114                 pr_debug("SPD_ERROR ");
115                 break;
116         }
117
118         pr_debug("tt hub ttport %d\n", udev->ttport);
119
120         dev_dbg(dev, "                    ");
121         for (i = 0; i < 16; i++)
122                 pr_debug(" %2u", i);
123         pr_debug("\n");
124
125         dev_dbg(dev, "       toggle0(IN) :");
126         for (i = 0; i < 16; i++)
127                 pr_debug(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
128         pr_debug("\n");
129
130         dev_dbg(dev, "       toggle1(OUT):");
131         for (i = 0; i < 16; i++)
132                 pr_debug(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
133         pr_debug("\n");
134
135         dev_dbg(dev, "       epmaxp_in   :");
136         for (i = 0; i < 16; i++) {
137                 if (udev->ep_in[i])
138                         pr_debug(" %2u",
139                             le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
140         }
141         pr_debug("\n");
142
143         dev_dbg(dev, "       epmaxp_out  :");
144         for (i = 0; i < 16; i++) {
145                 if (udev->ep_out[i])
146                         pr_debug(" %2u",
147                             le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
148         }
149         pr_debug("\n");
150
151         dev_dbg(dev, "parent %s, bus %s\n", dev_name(&udev->parent->dev),
152                 udev->bus->bus_name);
153
154         dev_dbg(dev, "have_langid %d, string_langid %d\n",
155                 udev->have_langid, udev->string_langid);
156
157         dev_dbg(dev, "maxchild %d, children %p\n",
158                 udev->maxchild, udev->children);
159 }
160
161 static void usbip_dump_request_type(__u8 rt)
162 {
163         switch (rt & USB_RECIP_MASK) {
164         case USB_RECIP_DEVICE:
165                 pr_debug("DEVICE");
166                 break;
167         case USB_RECIP_INTERFACE:
168                 pr_debug("INTERF");
169                 break;
170         case USB_RECIP_ENDPOINT:
171                 pr_debug("ENDPOI");
172                 break;
173         case USB_RECIP_OTHER:
174                 pr_debug("OTHER ");
175                 break;
176         default:
177                 pr_debug("------");
178                 break;
179         }
180 }
181
182 static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
183 {
184         if (!cmd) {
185                 pr_debug("       : null pointer\n");
186                 return;
187         }
188
189         pr_debug("       ");
190         pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) "
191                  "wLength(%04X) ", cmd->bRequestType, cmd->bRequest,
192                  cmd->wValue, cmd->wIndex, cmd->wLength);
193         pr_debug("\n       ");
194
195         if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
196                 pr_debug("STANDARD ");
197                 switch (cmd->bRequest) {
198                 case USB_REQ_GET_STATUS:
199                         pr_debug("GET_STATUS\n");
200                         break;
201                 case USB_REQ_CLEAR_FEATURE:
202                         pr_debug("CLEAR_FEAT\n");
203                         break;
204                 case USB_REQ_SET_FEATURE:
205                         pr_debug("SET_FEAT\n");
206                         break;
207                 case USB_REQ_SET_ADDRESS:
208                         pr_debug("SET_ADDRRS\n");
209                         break;
210                 case USB_REQ_GET_DESCRIPTOR:
211                         pr_debug("GET_DESCRI\n");
212                         break;
213                 case USB_REQ_SET_DESCRIPTOR:
214                         pr_debug("SET_DESCRI\n");
215                         break;
216                 case USB_REQ_GET_CONFIGURATION:
217                         pr_debug("GET_CONFIG\n");
218                         break;
219                 case USB_REQ_SET_CONFIGURATION:
220                         pr_debug("SET_CONFIG\n");
221                         break;
222                 case USB_REQ_GET_INTERFACE:
223                         pr_debug("GET_INTERF\n");
224                         break;
225                 case USB_REQ_SET_INTERFACE:
226                         pr_debug("SET_INTERF\n");
227                         break;
228                 case USB_REQ_SYNCH_FRAME:
229                         pr_debug("SYNC_FRAME\n");
230                         break;
231                 default:
232                         pr_debug("REQ(%02X)\n", cmd->bRequest);
233                         break;
234                 }
235                 usbip_dump_request_type(cmd->bRequestType);
236         } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
237                 pr_debug("CLASS\n");
238         } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
239                 pr_debug("VENDOR\n");
240         } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
241                 pr_debug("RESERVED\n");
242         }
243 }
244
245 void usbip_dump_urb(struct urb *urb)
246 {
247         struct device *dev;
248
249         if (!urb) {
250                 pr_debug("urb: null pointer!!\n");
251                 return;
252         }
253
254         if (!urb->dev) {
255                 pr_debug("urb->dev: null pointer!!\n");
256                 return;
257         }
258
259         dev = &urb->dev->dev;
260
261         usbip_dump_usb_device(urb->dev);
262
263         dev_dbg(dev, "   pipe                  :%08x ", urb->pipe);
264
265         usbip_dump_pipe(urb->pipe);
266
267         dev_dbg(dev, "   status                :%d\n", urb->status);
268         dev_dbg(dev, "   transfer_flags        :%08X\n", urb->transfer_flags);
269         dev_dbg(dev, "   transfer_buffer_length:%d\n",
270                                                 urb->transfer_buffer_length);
271         dev_dbg(dev, "   actual_length         :%d\n", urb->actual_length);
272
273         if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
274                 usbip_dump_usb_ctrlrequest(
275                         (struct usb_ctrlrequest *)urb->setup_packet);
276
277         dev_dbg(dev, "   start_frame           :%d\n", urb->start_frame);
278         dev_dbg(dev, "   number_of_packets     :%d\n", urb->number_of_packets);
279         dev_dbg(dev, "   interval              :%d\n", urb->interval);
280         dev_dbg(dev, "   error_count           :%d\n", urb->error_count);
281 }
282 EXPORT_SYMBOL_GPL(usbip_dump_urb);
283
284 void usbip_dump_header(struct usbip_header *pdu)
285 {
286         pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
287                  pdu->base.command,
288                  pdu->base.seqnum,
289                  pdu->base.devid,
290                  pdu->base.direction,
291                  pdu->base.ep);
292
293         switch (pdu->base.command) {
294         case USBIP_CMD_SUBMIT:
295                 pr_debug("USBIP_CMD_SUBMIT: "
296                          "x_flags %u x_len %u sf %u #p %d iv %d\n",
297                          pdu->u.cmd_submit.transfer_flags,
298                          pdu->u.cmd_submit.transfer_buffer_length,
299                          pdu->u.cmd_submit.start_frame,
300                          pdu->u.cmd_submit.number_of_packets,
301                          pdu->u.cmd_submit.interval);
302                 break;
303         case USBIP_CMD_UNLINK:
304                 pr_debug("USBIP_CMD_UNLINK: seq %u\n",
305                          pdu->u.cmd_unlink.seqnum);
306                 break;
307         case USBIP_RET_SUBMIT:
308                 pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
309                          pdu->u.ret_submit.status,
310                          pdu->u.ret_submit.actual_length,
311                          pdu->u.ret_submit.start_frame,
312                          pdu->u.ret_submit.number_of_packets,
313                          pdu->u.ret_submit.error_count);
314                 break;
315         case USBIP_RET_UNLINK:
316                 pr_debug("USBIP_RET_UNLINK: status %d\n",
317                          pdu->u.ret_unlink.status);
318                 break;
319         default:
320                 /* NOT REACHED */
321                 pr_err("unknown command\n");
322                 break;
323         }
324 }
325 EXPORT_SYMBOL_GPL(usbip_dump_header);
326
327 /* Receive data over TCP/IP. */
328 int usbip_recv(struct socket *sock, void *buf, int size)
329 {
330         int result;
331         struct msghdr msg;
332         struct kvec iov;
333         int total = 0;
334
335         /* for blocks of if (usbip_dbg_flag_xmit) */
336         char *bp = buf;
337         int osize = size;
338
339         if (!sock || !buf || !size)
340                 return -EINVAL;
341
342         usbip_dbg_xmit("enter\n");
343
344         do {
345                 sock->sk->sk_allocation = GFP_NOIO;
346                 iov.iov_base    = buf;
347                 iov.iov_len     = size;
348                 msg.msg_name    = NULL;
349                 msg.msg_namelen = 0;
350                 msg.msg_control = NULL;
351                 msg.msg_controllen = 0;
352                 msg.msg_namelen    = 0;
353                 msg.msg_flags      = MSG_NOSIGNAL;
354
355                 result = kernel_recvmsg(sock, &msg, &iov, 1, size, MSG_WAITALL);
356                 if (result <= 0)
357                         goto err;
358
359                 size -= result;
360                 buf += result;
361                 total += result;
362         } while (size > 0);
363
364         if (usbip_dbg_flag_xmit) {
365                 if (!in_interrupt())
366                         pr_debug("%-10s:", current->comm);
367                 else
368                         pr_debug("interrupt  :");
369
370                 pr_debug("receiving....\n");
371                 usbip_dump_buffer(bp, osize);
372                 pr_debug("received, osize %d ret %d size %d total %d\n",
373                         osize, result, size, total);
374         }
375
376         return total;
377
378 err:
379         return result;
380 }
381 EXPORT_SYMBOL_GPL(usbip_recv);
382
383 struct socket *sockfd_to_socket(unsigned int sockfd)
384 {
385         struct socket *socket;
386         struct file *file;
387         struct inode *inode;
388
389         file = fget(sockfd);
390         if (!file) {
391                 pr_err("invalid sockfd\n");
392                 return NULL;
393         }
394
395         inode = file->f_dentry->d_inode;
396
397         if (!inode || !S_ISSOCK(inode->i_mode))
398                 return NULL;
399
400         socket = SOCKET_I(inode);
401
402         return socket;
403 }
404 EXPORT_SYMBOL_GPL(sockfd_to_socket);
405
406 /* there may be more cases to tweak the flags. */
407 static unsigned int tweak_transfer_flags(unsigned int flags)
408 {
409         flags &= ~URB_NO_TRANSFER_DMA_MAP;
410         return flags;
411 }
412
413 static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
414                                   int pack)
415 {
416         struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
417
418         /*
419          * Some members are not still implemented in usbip. I hope this issue
420          * will be discussed when usbip is ported to other operating systems.
421          */
422         if (pack) {
423                 /* vhci_tx.c */
424                 spdu->transfer_flags =
425                         tweak_transfer_flags(urb->transfer_flags);
426                 spdu->transfer_buffer_length    = urb->transfer_buffer_length;
427                 spdu->start_frame               = urb->start_frame;
428                 spdu->number_of_packets         = urb->number_of_packets;
429                 spdu->interval                  = urb->interval;
430         } else  {
431                 /* stub_rx.c */
432                 urb->transfer_flags         = spdu->transfer_flags;
433
434                 urb->transfer_buffer_length = spdu->transfer_buffer_length;
435                 urb->start_frame            = spdu->start_frame;
436                 urb->number_of_packets      = spdu->number_of_packets;
437                 urb->interval               = spdu->interval;
438         }
439 }
440
441 static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
442                                   int pack)
443 {
444         struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
445
446         if (pack) {
447                 /* stub_tx.c */
448
449                 rpdu->status            = urb->status;
450                 rpdu->actual_length     = urb->actual_length;
451                 rpdu->start_frame       = urb->start_frame;
452                 rpdu->number_of_packets = urb->number_of_packets;
453                 rpdu->error_count       = urb->error_count;
454         } else {
455                 /* vhci_rx.c */
456
457                 urb->status             = rpdu->status;
458                 urb->actual_length      = rpdu->actual_length;
459                 urb->start_frame        = rpdu->start_frame;
460                 urb->number_of_packets = rpdu->number_of_packets;
461                 urb->error_count        = rpdu->error_count;
462         }
463 }
464
465 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
466                     int pack)
467 {
468         switch (cmd) {
469         case USBIP_CMD_SUBMIT:
470                 usbip_pack_cmd_submit(pdu, urb, pack);
471                 break;
472         case USBIP_RET_SUBMIT:
473                 usbip_pack_ret_submit(pdu, urb, pack);
474                 break;
475         default:
476                 /* NOT REACHED */
477                 pr_err("unknown command\n");
478                 break;
479         }
480 }
481 EXPORT_SYMBOL_GPL(usbip_pack_pdu);
482
483 static void correct_endian_basic(struct usbip_header_basic *base, int send)
484 {
485         if (send) {
486                 base->command   = cpu_to_be32(base->command);
487                 base->seqnum    = cpu_to_be32(base->seqnum);
488                 base->devid     = cpu_to_be32(base->devid);
489                 base->direction = cpu_to_be32(base->direction);
490                 base->ep        = cpu_to_be32(base->ep);
491         } else {
492                 base->command   = be32_to_cpu(base->command);
493                 base->seqnum    = be32_to_cpu(base->seqnum);
494                 base->devid     = be32_to_cpu(base->devid);
495                 base->direction = be32_to_cpu(base->direction);
496                 base->ep        = be32_to_cpu(base->ep);
497         }
498 }
499
500 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
501                                       int send)
502 {
503         if (send) {
504                 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
505
506                 cpu_to_be32s(&pdu->transfer_buffer_length);
507                 cpu_to_be32s(&pdu->start_frame);
508                 cpu_to_be32s(&pdu->number_of_packets);
509                 cpu_to_be32s(&pdu->interval);
510         } else {
511                 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
512
513                 be32_to_cpus(&pdu->transfer_buffer_length);
514                 be32_to_cpus(&pdu->start_frame);
515                 be32_to_cpus(&pdu->number_of_packets);
516                 be32_to_cpus(&pdu->interval);
517         }
518 }
519
520 static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
521                                       int send)
522 {
523         if (send) {
524                 cpu_to_be32s(&pdu->status);
525                 cpu_to_be32s(&pdu->actual_length);
526                 cpu_to_be32s(&pdu->start_frame);
527                 cpu_to_be32s(&pdu->number_of_packets);
528                 cpu_to_be32s(&pdu->error_count);
529         } else {
530                 be32_to_cpus(&pdu->status);
531                 be32_to_cpus(&pdu->actual_length);
532                 be32_to_cpus(&pdu->start_frame);
533                 be32_to_cpus(&pdu->number_of_packets);
534                 be32_to_cpus(&pdu->error_count);
535         }
536 }
537
538 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
539                                       int send)
540 {
541         if (send)
542                 pdu->seqnum = cpu_to_be32(pdu->seqnum);
543         else
544                 pdu->seqnum = be32_to_cpu(pdu->seqnum);
545 }
546
547 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
548                                       int send)
549 {
550         if (send)
551                 cpu_to_be32s(&pdu->status);
552         else
553                 be32_to_cpus(&pdu->status);
554 }
555
556 void usbip_header_correct_endian(struct usbip_header *pdu, int send)
557 {
558         __u32 cmd = 0;
559
560         if (send)
561                 cmd = pdu->base.command;
562
563         correct_endian_basic(&pdu->base, send);
564
565         if (!send)
566                 cmd = pdu->base.command;
567
568         switch (cmd) {
569         case USBIP_CMD_SUBMIT:
570                 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
571                 break;
572         case USBIP_RET_SUBMIT:
573                 correct_endian_ret_submit(&pdu->u.ret_submit, send);
574                 break;
575         case USBIP_CMD_UNLINK:
576                 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
577                 break;
578         case USBIP_RET_UNLINK:
579                 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
580                 break;
581         default:
582                 /* NOT REACHED */
583                 pr_err("unknown command\n");
584                 break;
585         }
586 }
587 EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
588
589 static void usbip_iso_packet_correct_endian(
590                 struct usbip_iso_packet_descriptor *iso, int send)
591 {
592         /* does not need all members. but copy all simply. */
593         if (send) {
594                 iso->offset     = cpu_to_be32(iso->offset);
595                 iso->length     = cpu_to_be32(iso->length);
596                 iso->status     = cpu_to_be32(iso->status);
597                 iso->actual_length = cpu_to_be32(iso->actual_length);
598         } else {
599                 iso->offset     = be32_to_cpu(iso->offset);
600                 iso->length     = be32_to_cpu(iso->length);
601                 iso->status     = be32_to_cpu(iso->status);
602                 iso->actual_length = be32_to_cpu(iso->actual_length);
603         }
604 }
605
606 static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
607                            struct usb_iso_packet_descriptor *uiso, int pack)
608 {
609         if (pack) {
610                 iso->offset             = uiso->offset;
611                 iso->length             = uiso->length;
612                 iso->status             = uiso->status;
613                 iso->actual_length      = uiso->actual_length;
614         } else {
615                 uiso->offset            = iso->offset;
616                 uiso->length            = iso->length;
617                 uiso->status            = iso->status;
618                 uiso->actual_length     = iso->actual_length;
619         }
620 }
621
622 /* must free buffer */
623 void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
624 {
625         void *buff;
626         struct usbip_iso_packet_descriptor *iso;
627         int np = urb->number_of_packets;
628         ssize_t size = np * sizeof(*iso);
629         int i;
630
631         buff = kzalloc(size, GFP_KERNEL);
632         if (!buff)
633                 return NULL;
634
635         for (i = 0; i < np; i++) {
636                 iso = buff + (i * sizeof(*iso));
637
638                 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 1);
639                 usbip_iso_packet_correct_endian(iso, 1);
640         }
641
642         *bufflen = size;
643
644         return buff;
645 }
646 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
647
648 /* some members of urb must be substituted before. */
649 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
650 {
651         void *buff;
652         struct usbip_iso_packet_descriptor *iso;
653         int np = urb->number_of_packets;
654         int size = np * sizeof(*iso);
655         int i;
656         int ret;
657         int total_length = 0;
658
659         if (!usb_pipeisoc(urb->pipe))
660                 return 0;
661
662         /* my Bluetooth dongle gets ISO URBs which are np = 0 */
663         if (np == 0) {
664                 /* pr_info("iso np == 0\n"); */
665                 /* usbip_dump_urb(urb); */
666                 return 0;
667         }
668
669         buff = kzalloc(size, GFP_KERNEL);
670         if (!buff)
671                 return -ENOMEM;
672
673         ret = usbip_recv(ud->tcp_socket, buff, size);
674         if (ret != size) {
675                 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
676                         ret);
677                 kfree(buff);
678
679                 if (ud->side == USBIP_STUB)
680                         usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
681                 else
682                         usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
683
684                 return -EPIPE;
685         }
686
687         for (i = 0; i < np; i++) {
688                 iso = buff + (i * sizeof(*iso));
689
690                 usbip_iso_packet_correct_endian(iso, 0);
691                 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0);
692                 total_length += urb->iso_frame_desc[i].actual_length;
693         }
694
695         kfree(buff);
696
697         if (total_length != urb->actual_length) {
698                 dev_err(&urb->dev->dev,
699                         "total length of iso packets %d not equal to actual "
700                         "length of buffer %d\n",
701                         total_length, urb->actual_length);
702
703                 if (ud->side == USBIP_STUB)
704                         usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
705                 else
706                         usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
707
708                 return -EPIPE;
709         }
710
711         return ret;
712 }
713 EXPORT_SYMBOL_GPL(usbip_recv_iso);
714
715 /*
716  * This functions restores the padding which was removed for optimizing
717  * the bandwidth during transfer over tcp/ip
718  *
719  * buffer and iso packets need to be stored and be in propeper endian in urb
720  * before calling this function
721  */
722 void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
723 {
724         int np = urb->number_of_packets;
725         int i;
726         int actualoffset = urb->actual_length;
727
728         if (!usb_pipeisoc(urb->pipe))
729                 return;
730
731         /* if no packets or length of data is 0, then nothing to unpack */
732         if (np == 0 || urb->actual_length == 0)
733                 return;
734
735         /*
736          * if actual_length is transfer_buffer_length then no padding is
737          * present.
738         */
739         if (urb->actual_length == urb->transfer_buffer_length)
740                 return;
741
742         /*
743          * loop over all packets from last to first (to prevent overwritting
744          * memory when padding) and move them into the proper place
745          */
746         for (i = np-1; i > 0; i--) {
747                 actualoffset -= urb->iso_frame_desc[i].actual_length;
748                 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
749                         urb->transfer_buffer + actualoffset,
750                         urb->iso_frame_desc[i].actual_length);
751         }
752 }
753 EXPORT_SYMBOL_GPL(usbip_pad_iso);
754
755 /* some members of urb must be substituted before. */
756 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
757 {
758         int ret;
759         int size;
760
761         if (ud->side == USBIP_STUB) {
762                 /* stub_rx.c */
763                 /* the direction of urb must be OUT. */
764                 if (usb_pipein(urb->pipe))
765                         return 0;
766
767                 size = urb->transfer_buffer_length;
768         } else {
769                 /* vhci_rx.c */
770                 /* the direction of urb must be IN. */
771                 if (usb_pipeout(urb->pipe))
772                         return 0;
773
774                 size = urb->actual_length;
775         }
776
777         /* no need to recv xbuff */
778         if (!(size > 0))
779                 return 0;
780
781         if (size > urb->transfer_buffer_length) {
782                 /* should not happen, probably malicious packet */
783                 if (ud->side == USBIP_STUB) {
784                         usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
785                         return 0;
786                 } else {
787                         usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
788                         return -EPIPE;
789                 }
790         }
791
792         ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
793         if (ret != size) {
794                 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
795                 if (ud->side == USBIP_STUB) {
796                         usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
797                 } else {
798                         usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
799                         return -EPIPE;
800                 }
801         }
802
803         return ret;
804 }
805 EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
806
807 static int __init usbip_core_init(void)
808 {
809         pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
810         return 0;
811 }
812
813 static void __exit usbip_core_exit(void)
814 {
815         return;
816 }
817
818 module_init(usbip_core_init);
819 module_exit(usbip_core_exit);
820
821 MODULE_AUTHOR(DRIVER_AUTHOR);
822 MODULE_DESCRIPTION(DRIVER_DESC);
823 MODULE_LICENSE("GPL");
824 MODULE_VERSION(USBIP_VERSION);