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