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