194e974051f32159d7aabc601de4331d3bf14f55
[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 %p, ttport %d\n", udev->tt, 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 %p, bus %p\n", udev->parent, udev->bus);
152
153         dev_dbg(dev, "descriptor %p, config %p, actconfig %p, "
154                 "rawdescriptors %p\n", &udev->descriptor, udev->config,
155                 udev->actconfig, udev->rawdescriptors);
156
157         dev_dbg(dev, "have_langid %d, string_langid %d\n",
158                 udev->have_langid, udev->string_langid);
159
160         dev_dbg(dev, "maxchild %d, children %p\n",
161                 udev->maxchild, udev->children);
162 }
163
164 static void usbip_dump_request_type(__u8 rt)
165 {
166         switch (rt & USB_RECIP_MASK) {
167         case USB_RECIP_DEVICE:
168                 pr_debug("DEVICE");
169                 break;
170         case USB_RECIP_INTERFACE:
171                 pr_debug("INTERF");
172                 break;
173         case USB_RECIP_ENDPOINT:
174                 pr_debug("ENDPOI");
175                 break;
176         case USB_RECIP_OTHER:
177                 pr_debug("OTHER ");
178                 break;
179         default:
180                 pr_debug("------");
181                 break;
182         }
183 }
184
185 static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
186 {
187         if (!cmd) {
188                 pr_debug("       : null pointer\n");
189                 return;
190         }
191
192         pr_debug("       ");
193         pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) "
194                  "wLength(%04X) ", cmd->bRequestType, cmd->bRequest,
195                  cmd->wValue, cmd->wIndex, cmd->wLength);
196         pr_debug("\n       ");
197
198         if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
199                 pr_debug("STANDARD ");
200                 switch (cmd->bRequest) {
201                 case USB_REQ_GET_STATUS:
202                         pr_debug("GET_STATUS\n");
203                         break;
204                 case USB_REQ_CLEAR_FEATURE:
205                         pr_debug("CLEAR_FEAT\n");
206                         break;
207                 case USB_REQ_SET_FEATURE:
208                         pr_debug("SET_FEAT\n");
209                         break;
210                 case USB_REQ_SET_ADDRESS:
211                         pr_debug("SET_ADDRRS\n");
212                         break;
213                 case USB_REQ_GET_DESCRIPTOR:
214                         pr_debug("GET_DESCRI\n");
215                         break;
216                 case USB_REQ_SET_DESCRIPTOR:
217                         pr_debug("SET_DESCRI\n");
218                         break;
219                 case USB_REQ_GET_CONFIGURATION:
220                         pr_debug("GET_CONFIG\n");
221                         break;
222                 case USB_REQ_SET_CONFIGURATION:
223                         pr_debug("SET_CONFIG\n");
224                         break;
225                 case USB_REQ_GET_INTERFACE:
226                         pr_debug("GET_INTERF\n");
227                         break;
228                 case USB_REQ_SET_INTERFACE:
229                         pr_debug("SET_INTERF\n");
230                         break;
231                 case USB_REQ_SYNCH_FRAME:
232                         pr_debug("SYNC_FRAME\n");
233                         break;
234                 default:
235                         pr_debug("REQ(%02X)\n", cmd->bRequest);
236                         break;
237                 }
238                 usbip_dump_request_type(cmd->bRequestType);
239         } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
240                 pr_debug("CLASS\n");
241         } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
242                 pr_debug("VENDOR\n");
243         } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
244                 pr_debug("RESERVED\n");
245         }
246 }
247
248 void usbip_dump_urb(struct urb *urb)
249 {
250         struct device *dev;
251
252         if (!urb) {
253                 pr_debug("urb: null pointer!!\n");
254                 return;
255         }
256
257         if (!urb->dev) {
258                 pr_debug("urb->dev: null pointer!!\n");
259                 return;
260         }
261
262         dev = &urb->dev->dev;
263
264         dev_dbg(dev, "   urb                   :%p\n", urb);
265         dev_dbg(dev, "   dev                   :%p\n", urb->dev);
266
267         usbip_dump_usb_device(urb->dev);
268
269         dev_dbg(dev, "   pipe                  :%08x ", urb->pipe);
270
271         usbip_dump_pipe(urb->pipe);
272
273         dev_dbg(dev, "   status                :%d\n", urb->status);
274         dev_dbg(dev, "   transfer_flags        :%08X\n", urb->transfer_flags);
275         dev_dbg(dev, "   transfer_buffer       :%p\n", urb->transfer_buffer);
276         dev_dbg(dev, "   transfer_buffer_length:%d\n",
277                                                 urb->transfer_buffer_length);
278         dev_dbg(dev, "   actual_length         :%d\n", urb->actual_length);
279         dev_dbg(dev, "   setup_packet          :%p\n", urb->setup_packet);
280
281         if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
282                 usbip_dump_usb_ctrlrequest(
283                         (struct usb_ctrlrequest *)urb->setup_packet);
284
285         dev_dbg(dev, "   start_frame           :%d\n", urb->start_frame);
286         dev_dbg(dev, "   number_of_packets     :%d\n", urb->number_of_packets);
287         dev_dbg(dev, "   interval              :%d\n", urb->interval);
288         dev_dbg(dev, "   error_count           :%d\n", urb->error_count);
289         dev_dbg(dev, "   context               :%p\n", urb->context);
290         dev_dbg(dev, "   complete              :%p\n", urb->complete);
291 }
292 EXPORT_SYMBOL_GPL(usbip_dump_urb);
293
294 void usbip_dump_header(struct usbip_header *pdu)
295 {
296         pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
297                  pdu->base.command,
298                  pdu->base.seqnum,
299                  pdu->base.devid,
300                  pdu->base.direction,
301                  pdu->base.ep);
302
303         switch (pdu->base.command) {
304         case USBIP_CMD_SUBMIT:
305                 pr_debug("USBIP_CMD_SUBMIT: "
306                          "x_flags %u x_len %u sf %u #p %d iv %d\n",
307                          pdu->u.cmd_submit.transfer_flags,
308                          pdu->u.cmd_submit.transfer_buffer_length,
309                          pdu->u.cmd_submit.start_frame,
310                          pdu->u.cmd_submit.number_of_packets,
311                          pdu->u.cmd_submit.interval);
312                 break;
313         case USBIP_CMD_UNLINK:
314                 pr_debug("USBIP_CMD_UNLINK: seq %u\n",
315                          pdu->u.cmd_unlink.seqnum);
316                 break;
317         case USBIP_RET_SUBMIT:
318                 pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
319                          pdu->u.ret_submit.status,
320                          pdu->u.ret_submit.actual_length,
321                          pdu->u.ret_submit.start_frame,
322                          pdu->u.ret_submit.number_of_packets,
323                          pdu->u.ret_submit.error_count);
324                 break;
325         case USBIP_RET_UNLINK:
326                 pr_debug("USBIP_RET_UNLINK: status %d\n",
327                          pdu->u.ret_unlink.status);
328                 break;
329         default:
330                 /* NOT REACHED */
331                 pr_err("unknown command\n");
332                 break;
333         }
334 }
335 EXPORT_SYMBOL_GPL(usbip_dump_header);
336
337 /* Send/receive messages over TCP/IP. I refer drivers/block/nbd.c */
338 int usbip_xmit(int send, struct socket *sock, char *buf, int size,
339                int msg_flags)
340 {
341         int result;
342         struct msghdr msg;
343         struct kvec iov;
344         int total = 0;
345
346         /* for blocks of if (usbip_dbg_flag_xmit) */
347         char *bp = buf;
348         int osize = size;
349
350         usbip_dbg_xmit("enter\n");
351
352         if (!sock || !buf || !size) {
353                 pr_err("invalid arg, sock %p buff %p size %d\n", sock, buf,
354                        size);
355                 return -EINVAL;
356         }
357
358         if (usbip_dbg_flag_xmit) {
359                 if (send) {
360                         if (!in_interrupt())
361                                 pr_debug("%-10s:", current->comm);
362                         else
363                                 pr_debug("interrupt  :");
364
365                         pr_debug("sending... , sock %p, buf %p, size %d, "
366                                  "msg_flags %d\n", sock, buf, size, msg_flags);
367                         usbip_dump_buffer(buf, size);
368                 }
369         }
370
371         do {
372                 sock->sk->sk_allocation = GFP_NOIO;
373                 iov.iov_base    = buf;
374                 iov.iov_len     = size;
375                 msg.msg_name    = NULL;
376                 msg.msg_namelen = 0;
377                 msg.msg_control = NULL;
378                 msg.msg_controllen = 0;
379                 msg.msg_namelen    = 0;
380                 msg.msg_flags      = msg_flags | MSG_NOSIGNAL;
381
382                 if (send)
383                         result = kernel_sendmsg(sock, &msg, &iov, 1, size);
384                 else
385                         result = kernel_recvmsg(sock, &msg, &iov, 1, size,
386                                                 MSG_WAITALL);
387
388                 if (result <= 0) {
389                         pr_debug("%s sock %p buf %p size %u ret %d total %d\n",
390                                  send ? "send" : "receive", sock, buf, size,
391                                  result, total);
392                         goto err;
393                 }
394
395                 size -= result;
396                 buf += result;
397                 total += result;
398
399         } while (size > 0);
400
401         if (usbip_dbg_flag_xmit) {
402                 if (!send) {
403                         if (!in_interrupt())
404                                 pr_debug("%-10s:", current->comm);
405                         else
406                                 pr_debug("interrupt  :");
407
408                         pr_debug("receiving....\n");
409                         usbip_dump_buffer(bp, osize);
410                         pr_debug("received, osize %d ret %d size %d total %d\n",
411                                  osize, result, size, total);
412                 }
413
414                 if (send)
415                         pr_debug("send, total %d\n", total);
416         }
417
418         return total;
419
420 err:
421         return result;
422 }
423 EXPORT_SYMBOL_GPL(usbip_xmit);
424
425 struct socket *sockfd_to_socket(unsigned int sockfd)
426 {
427         struct socket *socket;
428         struct file *file;
429         struct inode *inode;
430
431         file = fget(sockfd);
432         if (!file) {
433                 pr_err("invalid sockfd\n");
434                 return NULL;
435         }
436
437         inode = file->f_dentry->d_inode;
438
439         if (!inode || !S_ISSOCK(inode->i_mode))
440                 return NULL;
441
442         socket = SOCKET_I(inode);
443
444         return socket;
445 }
446 EXPORT_SYMBOL_GPL(sockfd_to_socket);
447
448 /* there may be more cases to tweak the flags. */
449 static unsigned int tweak_transfer_flags(unsigned int flags)
450 {
451         flags &= ~URB_NO_TRANSFER_DMA_MAP;
452         return flags;
453 }
454
455 static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
456                                   int pack)
457 {
458         struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
459
460         /*
461          * Some members are not still implemented in usbip. I hope this issue
462          * will be discussed when usbip is ported to other operating systems.
463          */
464         if (pack) {
465                 /* vhci_tx.c */
466                 spdu->transfer_flags =
467                         tweak_transfer_flags(urb->transfer_flags);
468                 spdu->transfer_buffer_length    = urb->transfer_buffer_length;
469                 spdu->start_frame               = urb->start_frame;
470                 spdu->number_of_packets         = urb->number_of_packets;
471                 spdu->interval                  = urb->interval;
472         } else  {
473                 /* stub_rx.c */
474                 urb->transfer_flags         = spdu->transfer_flags;
475
476                 urb->transfer_buffer_length = spdu->transfer_buffer_length;
477                 urb->start_frame            = spdu->start_frame;
478                 urb->number_of_packets      = spdu->number_of_packets;
479                 urb->interval               = spdu->interval;
480         }
481 }
482
483 static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
484                                   int pack)
485 {
486         struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
487
488         if (pack) {
489                 /* stub_tx.c */
490
491                 rpdu->status            = urb->status;
492                 rpdu->actual_length     = urb->actual_length;
493                 rpdu->start_frame       = urb->start_frame;
494                 rpdu->number_of_packets = urb->number_of_packets;
495                 rpdu->error_count       = urb->error_count;
496         } else {
497                 /* vhci_rx.c */
498
499                 urb->status             = rpdu->status;
500                 urb->actual_length      = rpdu->actual_length;
501                 urb->start_frame        = rpdu->start_frame;
502                 urb->number_of_packets = rpdu->number_of_packets;
503                 urb->error_count        = rpdu->error_count;
504         }
505 }
506
507 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
508                     int pack)
509 {
510         switch (cmd) {
511         case USBIP_CMD_SUBMIT:
512                 usbip_pack_cmd_submit(pdu, urb, pack);
513                 break;
514         case USBIP_RET_SUBMIT:
515                 usbip_pack_ret_submit(pdu, urb, pack);
516                 break;
517         default:
518                 /* NOT REACHED */
519                 pr_err("unknown command\n");
520                 break;
521         }
522 }
523 EXPORT_SYMBOL_GPL(usbip_pack_pdu);
524
525 static void correct_endian_basic(struct usbip_header_basic *base, int send)
526 {
527         if (send) {
528                 base->command   = cpu_to_be32(base->command);
529                 base->seqnum    = cpu_to_be32(base->seqnum);
530                 base->devid     = cpu_to_be32(base->devid);
531                 base->direction = cpu_to_be32(base->direction);
532                 base->ep        = cpu_to_be32(base->ep);
533         } else {
534                 base->command   = be32_to_cpu(base->command);
535                 base->seqnum    = be32_to_cpu(base->seqnum);
536                 base->devid     = be32_to_cpu(base->devid);
537                 base->direction = be32_to_cpu(base->direction);
538                 base->ep        = be32_to_cpu(base->ep);
539         }
540 }
541
542 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
543                                       int send)
544 {
545         if (send) {
546                 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
547
548                 cpu_to_be32s(&pdu->transfer_buffer_length);
549                 cpu_to_be32s(&pdu->start_frame);
550                 cpu_to_be32s(&pdu->number_of_packets);
551                 cpu_to_be32s(&pdu->interval);
552         } else {
553                 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
554
555                 be32_to_cpus(&pdu->transfer_buffer_length);
556                 be32_to_cpus(&pdu->start_frame);
557                 be32_to_cpus(&pdu->number_of_packets);
558                 be32_to_cpus(&pdu->interval);
559         }
560 }
561
562 static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
563                                       int send)
564 {
565         if (send) {
566                 cpu_to_be32s(&pdu->status);
567                 cpu_to_be32s(&pdu->actual_length);
568                 cpu_to_be32s(&pdu->start_frame);
569                 cpu_to_be32s(&pdu->number_of_packets);
570                 cpu_to_be32s(&pdu->error_count);
571         } else {
572                 be32_to_cpus(&pdu->status);
573                 be32_to_cpus(&pdu->actual_length);
574                 be32_to_cpus(&pdu->start_frame);
575                 be32_to_cpus(&pdu->number_of_packets);
576                 be32_to_cpus(&pdu->error_count);
577         }
578 }
579
580 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
581                                       int send)
582 {
583         if (send)
584                 pdu->seqnum = cpu_to_be32(pdu->seqnum);
585         else
586                 pdu->seqnum = be32_to_cpu(pdu->seqnum);
587 }
588
589 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
590                                       int send)
591 {
592         if (send)
593                 cpu_to_be32s(&pdu->status);
594         else
595                 be32_to_cpus(&pdu->status);
596 }
597
598 void usbip_header_correct_endian(struct usbip_header *pdu, int send)
599 {
600         __u32 cmd = 0;
601
602         if (send)
603                 cmd = pdu->base.command;
604
605         correct_endian_basic(&pdu->base, send);
606
607         if (!send)
608                 cmd = pdu->base.command;
609
610         switch (cmd) {
611         case USBIP_CMD_SUBMIT:
612                 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
613                 break;
614         case USBIP_RET_SUBMIT:
615                 correct_endian_ret_submit(&pdu->u.ret_submit, send);
616                 break;
617         case USBIP_CMD_UNLINK:
618                 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
619                 break;
620         case USBIP_RET_UNLINK:
621                 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
622                 break;
623         default:
624                 /* NOT REACHED */
625                 pr_err("unknown command\n");
626                 break;
627         }
628 }
629 EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
630
631 static void usbip_iso_packet_correct_endian(
632                 struct usbip_iso_packet_descriptor *iso, int send)
633 {
634         /* does not need all members. but copy all simply. */
635         if (send) {
636                 iso->offset     = cpu_to_be32(iso->offset);
637                 iso->length     = cpu_to_be32(iso->length);
638                 iso->status     = cpu_to_be32(iso->status);
639                 iso->actual_length = cpu_to_be32(iso->actual_length);
640         } else {
641                 iso->offset     = be32_to_cpu(iso->offset);
642                 iso->length     = be32_to_cpu(iso->length);
643                 iso->status     = be32_to_cpu(iso->status);
644                 iso->actual_length = be32_to_cpu(iso->actual_length);
645         }
646 }
647
648 static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
649                            struct usb_iso_packet_descriptor *uiso, int pack)
650 {
651         if (pack) {
652                 iso->offset             = uiso->offset;
653                 iso->length             = uiso->length;
654                 iso->status             = uiso->status;
655                 iso->actual_length      = uiso->actual_length;
656         } else {
657                 uiso->offset            = iso->offset;
658                 uiso->length            = iso->length;
659                 uiso->status            = iso->status;
660                 uiso->actual_length     = iso->actual_length;
661         }
662 }
663
664 /* must free buffer */
665 void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
666 {
667         void *buff;
668         struct usbip_iso_packet_descriptor *iso;
669         int np = urb->number_of_packets;
670         ssize_t size = np * sizeof(*iso);
671         int i;
672
673         buff = kzalloc(size, GFP_KERNEL);
674         if (!buff)
675                 return NULL;
676
677         for (i = 0; i < np; i++) {
678                 iso = buff + (i * sizeof(*iso));
679
680                 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 1);
681                 usbip_iso_packet_correct_endian(iso, 1);
682         }
683
684         *bufflen = size;
685
686         return buff;
687 }
688 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
689
690 /* some members of urb must be substituted before. */
691 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
692 {
693         void *buff;
694         struct usbip_iso_packet_descriptor *iso;
695         int np = urb->number_of_packets;
696         int size = np * sizeof(*iso);
697         int i;
698         int ret;
699         int total_length = 0;
700
701         if (!usb_pipeisoc(urb->pipe))
702                 return 0;
703
704         /* my Bluetooth dongle gets ISO URBs which are np = 0 */
705         if (np == 0) {
706                 /* pr_info("iso np == 0\n"); */
707                 /* usbip_dump_urb(urb); */
708                 return 0;
709         }
710
711         buff = kzalloc(size, GFP_KERNEL);
712         if (!buff)
713                 return -ENOMEM;
714
715         ret = usbip_xmit(0, ud->tcp_socket, buff, size, 0);
716         if (ret != size) {
717                 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
718                         ret);
719                 kfree(buff);
720
721                 if (ud->side == USBIP_STUB)
722                         usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
723                 else
724                         usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
725
726                 return -EPIPE;
727         }
728
729         for (i = 0; i < np; i++) {
730                 iso = buff + (i * sizeof(*iso));
731
732                 usbip_iso_packet_correct_endian(iso, 0);
733                 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0);
734                 total_length += urb->iso_frame_desc[i].actual_length;
735         }
736
737         kfree(buff);
738
739         if (total_length != urb->actual_length) {
740                 dev_err(&urb->dev->dev,
741                         "total length of iso packets %d not equal to actual "
742                         "length of buffer %d\n",
743                         total_length, urb->actual_length);
744
745                 if (ud->side == USBIP_STUB)
746                         usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
747                 else
748                         usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
749
750                 return -EPIPE;
751         }
752
753         return ret;
754 }
755 EXPORT_SYMBOL_GPL(usbip_recv_iso);
756
757 /*
758  * This functions restores the padding which was removed for optimizing
759  * the bandwidth during transfer over tcp/ip
760  *
761  * buffer and iso packets need to be stored and be in propeper endian in urb
762  * before calling this function
763  */
764 void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
765 {
766         int np = urb->number_of_packets;
767         int i;
768         int actualoffset = urb->actual_length;
769
770         if (!usb_pipeisoc(urb->pipe))
771                 return;
772
773         /* if no packets or length of data is 0, then nothing to unpack */
774         if (np == 0 || urb->actual_length == 0)
775                 return;
776
777         /*
778          * if actual_length is transfer_buffer_length then no padding is
779          * present.
780         */
781         if (urb->actual_length == urb->transfer_buffer_length)
782                 return;
783
784         /*
785          * loop over all packets from last to first (to prevent overwritting
786          * memory when padding) and move them into the proper place
787          */
788         for (i = np-1; i > 0; i--) {
789                 actualoffset -= urb->iso_frame_desc[i].actual_length;
790                 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
791                         urb->transfer_buffer + actualoffset,
792                         urb->iso_frame_desc[i].actual_length);
793         }
794 }
795 EXPORT_SYMBOL_GPL(usbip_pad_iso);
796
797 /* some members of urb must be substituted before. */
798 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
799 {
800         int ret;
801         int size;
802
803         if (ud->side == USBIP_STUB) {
804                 /* stub_rx.c */
805                 /* the direction of urb must be OUT. */
806                 if (usb_pipein(urb->pipe))
807                         return 0;
808
809                 size = urb->transfer_buffer_length;
810         } else {
811                 /* vhci_rx.c */
812                 /* the direction of urb must be IN. */
813                 if (usb_pipeout(urb->pipe))
814                         return 0;
815
816                 size = urb->actual_length;
817         }
818
819         /* no need to recv xbuff */
820         if (!(size > 0))
821                 return 0;
822
823         ret = usbip_xmit(0, ud->tcp_socket, (char *)urb->transfer_buffer,
824                          size, 0);
825         if (ret != size) {
826                 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
827                 if (ud->side == USBIP_STUB) {
828                         usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
829                 } else {
830                         usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
831                         return -EPIPE;
832                 }
833         }
834
835         return ret;
836 }
837 EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
838
839 static int __init usbip_core_init(void)
840 {
841         pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
842         return 0;
843 }
844
845 static void __exit usbip_core_exit(void)
846 {
847         return;
848 }
849
850 module_init(usbip_core_init);
851 module_exit(usbip_core_exit);
852
853 MODULE_AUTHOR(DRIVER_AUTHOR);
854 MODULE_DESCRIPTION(DRIVER_DESC);
855 MODULE_LICENSE("GPL");
856 MODULE_VERSION(USBIP_VERSION);