Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Copyright (C) 2003-2008 Takahiro Hirofuchi
0004  * Copyright (C) 2015-2016 Samsung Electronics
0005  *               Krzysztof Opasiak <k.opasiak@samsung.com>
0006  */
0007 
0008 #include <asm/byteorder.h>
0009 #include <linux/file.h>
0010 #include <linux/fs.h>
0011 #include <linux/kernel.h>
0012 #include <linux/slab.h>
0013 #include <linux/stat.h>
0014 #include <linux/module.h>
0015 #include <linux/moduleparam.h>
0016 #include <net/sock.h>
0017 
0018 #include "usbip_common.h"
0019 
0020 #define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>"
0021 #define DRIVER_DESC "USB/IP Core"
0022 
0023 #ifdef CONFIG_USBIP_DEBUG
0024 unsigned long usbip_debug_flag = 0xffffffff;
0025 #else
0026 unsigned long usbip_debug_flag;
0027 #endif
0028 EXPORT_SYMBOL_GPL(usbip_debug_flag);
0029 module_param(usbip_debug_flag, ulong, S_IRUGO|S_IWUSR);
0030 MODULE_PARM_DESC(usbip_debug_flag, "debug flags (defined in usbip_common.h)");
0031 
0032 /* FIXME */
0033 struct device_attribute dev_attr_usbip_debug;
0034 EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
0035 
0036 static ssize_t usbip_debug_show(struct device *dev,
0037                 struct device_attribute *attr, char *buf)
0038 {
0039     return sprintf(buf, "%lx\n", usbip_debug_flag);
0040 }
0041 
0042 static ssize_t usbip_debug_store(struct device *dev,
0043                  struct device_attribute *attr, const char *buf,
0044                  size_t count)
0045 {
0046     if (sscanf(buf, "%lx", &usbip_debug_flag) != 1)
0047         return -EINVAL;
0048     return count;
0049 }
0050 DEVICE_ATTR_RW(usbip_debug);
0051 
0052 static void usbip_dump_buffer(char *buff, int bufflen)
0053 {
0054     print_hex_dump(KERN_DEBUG, "usbip-core", DUMP_PREFIX_OFFSET, 16, 4,
0055                buff, bufflen, false);
0056 }
0057 
0058 static void usbip_dump_pipe(unsigned int p)
0059 {
0060     unsigned char type = usb_pipetype(p);
0061     unsigned char ep   = usb_pipeendpoint(p);
0062     unsigned char dev  = usb_pipedevice(p);
0063     unsigned char dir  = usb_pipein(p);
0064 
0065     pr_debug("dev(%d) ep(%d) [%s] ", dev, ep, dir ? "IN" : "OUT");
0066 
0067     switch (type) {
0068     case PIPE_ISOCHRONOUS:
0069         pr_debug("ISO\n");
0070         break;
0071     case PIPE_INTERRUPT:
0072         pr_debug("INT\n");
0073         break;
0074     case PIPE_CONTROL:
0075         pr_debug("CTRL\n");
0076         break;
0077     case PIPE_BULK:
0078         pr_debug("BULK\n");
0079         break;
0080     default:
0081         pr_debug("ERR\n");
0082         break;
0083     }
0084 }
0085 
0086 static void usbip_dump_usb_device(struct usb_device *udev)
0087 {
0088     struct device *dev = &udev->dev;
0089     int i;
0090 
0091     dev_dbg(dev, "       devnum(%d) devpath(%s) usb speed(%s)",
0092         udev->devnum, udev->devpath, usb_speed_string(udev->speed));
0093 
0094     pr_debug("tt hub ttport %d\n", udev->ttport);
0095 
0096     dev_dbg(dev, "                    ");
0097     for (i = 0; i < 16; i++)
0098         pr_debug(" %2u", i);
0099     pr_debug("\n");
0100 
0101     dev_dbg(dev, "       toggle0(IN) :");
0102     for (i = 0; i < 16; i++)
0103         pr_debug(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
0104     pr_debug("\n");
0105 
0106     dev_dbg(dev, "       toggle1(OUT):");
0107     for (i = 0; i < 16; i++)
0108         pr_debug(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
0109     pr_debug("\n");
0110 
0111     dev_dbg(dev, "       epmaxp_in   :");
0112     for (i = 0; i < 16; i++) {
0113         if (udev->ep_in[i])
0114             pr_debug(" %2u",
0115                 le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
0116     }
0117     pr_debug("\n");
0118 
0119     dev_dbg(dev, "       epmaxp_out  :");
0120     for (i = 0; i < 16; i++) {
0121         if (udev->ep_out[i])
0122             pr_debug(" %2u",
0123                 le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
0124     }
0125     pr_debug("\n");
0126 
0127     dev_dbg(dev, "parent %s, bus %s\n", dev_name(&udev->parent->dev),
0128         udev->bus->bus_name);
0129 
0130     dev_dbg(dev, "have_langid %d, string_langid %d\n",
0131         udev->have_langid, udev->string_langid);
0132 
0133     dev_dbg(dev, "maxchild %d\n", udev->maxchild);
0134 }
0135 
0136 static void usbip_dump_request_type(__u8 rt)
0137 {
0138     switch (rt & USB_RECIP_MASK) {
0139     case USB_RECIP_DEVICE:
0140         pr_debug("DEVICE");
0141         break;
0142     case USB_RECIP_INTERFACE:
0143         pr_debug("INTERF");
0144         break;
0145     case USB_RECIP_ENDPOINT:
0146         pr_debug("ENDPOI");
0147         break;
0148     case USB_RECIP_OTHER:
0149         pr_debug("OTHER ");
0150         break;
0151     default:
0152         pr_debug("------");
0153         break;
0154     }
0155 }
0156 
0157 static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
0158 {
0159     if (!cmd) {
0160         pr_debug("       : null pointer\n");
0161         return;
0162     }
0163 
0164     pr_debug("       ");
0165     pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) wLength(%04X) ",
0166          cmd->bRequestType, cmd->bRequest,
0167          cmd->wValue, cmd->wIndex, cmd->wLength);
0168     pr_debug("\n       ");
0169 
0170     if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
0171         pr_debug("STANDARD ");
0172         switch (cmd->bRequest) {
0173         case USB_REQ_GET_STATUS:
0174             pr_debug("GET_STATUS\n");
0175             break;
0176         case USB_REQ_CLEAR_FEATURE:
0177             pr_debug("CLEAR_FEAT\n");
0178             break;
0179         case USB_REQ_SET_FEATURE:
0180             pr_debug("SET_FEAT\n");
0181             break;
0182         case USB_REQ_SET_ADDRESS:
0183             pr_debug("SET_ADDRRS\n");
0184             break;
0185         case USB_REQ_GET_DESCRIPTOR:
0186             pr_debug("GET_DESCRI\n");
0187             break;
0188         case USB_REQ_SET_DESCRIPTOR:
0189             pr_debug("SET_DESCRI\n");
0190             break;
0191         case USB_REQ_GET_CONFIGURATION:
0192             pr_debug("GET_CONFIG\n");
0193             break;
0194         case USB_REQ_SET_CONFIGURATION:
0195             pr_debug("SET_CONFIG\n");
0196             break;
0197         case USB_REQ_GET_INTERFACE:
0198             pr_debug("GET_INTERF\n");
0199             break;
0200         case USB_REQ_SET_INTERFACE:
0201             pr_debug("SET_INTERF\n");
0202             break;
0203         case USB_REQ_SYNCH_FRAME:
0204             pr_debug("SYNC_FRAME\n");
0205             break;
0206         default:
0207             pr_debug("REQ(%02X)\n", cmd->bRequest);
0208             break;
0209         }
0210         usbip_dump_request_type(cmd->bRequestType);
0211     } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
0212         pr_debug("CLASS\n");
0213     } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
0214         pr_debug("VENDOR\n");
0215     } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
0216         pr_debug("RESERVED\n");
0217     }
0218 }
0219 
0220 void usbip_dump_urb(struct urb *urb)
0221 {
0222     struct device *dev;
0223 
0224     if (!urb) {
0225         pr_debug("urb: null pointer!!\n");
0226         return;
0227     }
0228 
0229     if (!urb->dev) {
0230         pr_debug("urb->dev: null pointer!!\n");
0231         return;
0232     }
0233 
0234     dev = &urb->dev->dev;
0235 
0236     usbip_dump_usb_device(urb->dev);
0237 
0238     dev_dbg(dev, "   pipe                  :%08x ", urb->pipe);
0239 
0240     usbip_dump_pipe(urb->pipe);
0241 
0242     dev_dbg(dev, "   status                :%d\n", urb->status);
0243     dev_dbg(dev, "   transfer_flags        :%08X\n", urb->transfer_flags);
0244     dev_dbg(dev, "   transfer_buffer_length:%d\n",
0245                         urb->transfer_buffer_length);
0246     dev_dbg(dev, "   actual_length         :%d\n", urb->actual_length);
0247 
0248     if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
0249         usbip_dump_usb_ctrlrequest(
0250             (struct usb_ctrlrequest *)urb->setup_packet);
0251 
0252     dev_dbg(dev, "   start_frame           :%d\n", urb->start_frame);
0253     dev_dbg(dev, "   number_of_packets     :%d\n", urb->number_of_packets);
0254     dev_dbg(dev, "   interval              :%d\n", urb->interval);
0255     dev_dbg(dev, "   error_count           :%d\n", urb->error_count);
0256 }
0257 EXPORT_SYMBOL_GPL(usbip_dump_urb);
0258 
0259 void usbip_dump_header(struct usbip_header *pdu)
0260 {
0261     pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
0262          pdu->base.command,
0263          pdu->base.seqnum,
0264          pdu->base.devid,
0265          pdu->base.direction,
0266          pdu->base.ep);
0267 
0268     switch (pdu->base.command) {
0269     case USBIP_CMD_SUBMIT:
0270         pr_debug("USBIP_CMD_SUBMIT: x_flags %u x_len %u sf %u #p %d iv %d\n",
0271              pdu->u.cmd_submit.transfer_flags,
0272              pdu->u.cmd_submit.transfer_buffer_length,
0273              pdu->u.cmd_submit.start_frame,
0274              pdu->u.cmd_submit.number_of_packets,
0275              pdu->u.cmd_submit.interval);
0276         break;
0277     case USBIP_CMD_UNLINK:
0278         pr_debug("USBIP_CMD_UNLINK: seq %u\n",
0279              pdu->u.cmd_unlink.seqnum);
0280         break;
0281     case USBIP_RET_SUBMIT:
0282         pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
0283              pdu->u.ret_submit.status,
0284              pdu->u.ret_submit.actual_length,
0285              pdu->u.ret_submit.start_frame,
0286              pdu->u.ret_submit.number_of_packets,
0287              pdu->u.ret_submit.error_count);
0288         break;
0289     case USBIP_RET_UNLINK:
0290         pr_debug("USBIP_RET_UNLINK: status %d\n",
0291              pdu->u.ret_unlink.status);
0292         break;
0293     default:
0294         /* NOT REACHED */
0295         pr_err("unknown command\n");
0296         break;
0297     }
0298 }
0299 EXPORT_SYMBOL_GPL(usbip_dump_header);
0300 
0301 /* Receive data over TCP/IP. */
0302 int usbip_recv(struct socket *sock, void *buf, int size)
0303 {
0304     int result;
0305     struct kvec iov = {.iov_base = buf, .iov_len = size};
0306     struct msghdr msg = {.msg_flags = MSG_NOSIGNAL};
0307     int total = 0;
0308 
0309     if (!sock || !buf || !size)
0310         return -EINVAL;
0311 
0312     iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, size);
0313 
0314     usbip_dbg_xmit("enter\n");
0315 
0316     do {
0317         sock->sk->sk_allocation = GFP_NOIO;
0318 
0319         result = sock_recvmsg(sock, &msg, MSG_WAITALL);
0320         if (result <= 0)
0321             goto err;
0322 
0323         total += result;
0324     } while (msg_data_left(&msg));
0325 
0326     if (usbip_dbg_flag_xmit) {
0327         pr_debug("receiving....\n");
0328         usbip_dump_buffer(buf, size);
0329         pr_debug("received, osize %d ret %d size %zd total %d\n",
0330              size, result, msg_data_left(&msg), total);
0331     }
0332 
0333     return total;
0334 
0335 err:
0336     return result;
0337 }
0338 EXPORT_SYMBOL_GPL(usbip_recv);
0339 
0340 /* there may be more cases to tweak the flags. */
0341 static unsigned int tweak_transfer_flags(unsigned int flags)
0342 {
0343     flags &= ~URB_NO_TRANSFER_DMA_MAP;
0344     return flags;
0345 }
0346 
0347 static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
0348                   int pack)
0349 {
0350     struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
0351 
0352     /*
0353      * Some members are not still implemented in usbip. I hope this issue
0354      * will be discussed when usbip is ported to other operating systems.
0355      */
0356     if (pack) {
0357         spdu->transfer_flags =
0358             tweak_transfer_flags(urb->transfer_flags);
0359         spdu->transfer_buffer_length    = urb->transfer_buffer_length;
0360         spdu->start_frame       = urb->start_frame;
0361         spdu->number_of_packets     = urb->number_of_packets;
0362         spdu->interval          = urb->interval;
0363     } else  {
0364         urb->transfer_flags         = spdu->transfer_flags;
0365         urb->transfer_buffer_length = spdu->transfer_buffer_length;
0366         urb->start_frame            = spdu->start_frame;
0367         urb->number_of_packets      = spdu->number_of_packets;
0368         urb->interval               = spdu->interval;
0369     }
0370 }
0371 
0372 static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
0373                   int pack)
0374 {
0375     struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
0376 
0377     if (pack) {
0378         rpdu->status        = urb->status;
0379         rpdu->actual_length = urb->actual_length;
0380         rpdu->start_frame   = urb->start_frame;
0381         rpdu->number_of_packets = urb->number_of_packets;
0382         rpdu->error_count   = urb->error_count;
0383     } else {
0384         urb->status     = rpdu->status;
0385         urb->actual_length  = rpdu->actual_length;
0386         urb->start_frame    = rpdu->start_frame;
0387         urb->number_of_packets = rpdu->number_of_packets;
0388         urb->error_count    = rpdu->error_count;
0389     }
0390 }
0391 
0392 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
0393             int pack)
0394 {
0395     switch (cmd) {
0396     case USBIP_CMD_SUBMIT:
0397         usbip_pack_cmd_submit(pdu, urb, pack);
0398         break;
0399     case USBIP_RET_SUBMIT:
0400         usbip_pack_ret_submit(pdu, urb, pack);
0401         break;
0402     default:
0403         /* NOT REACHED */
0404         pr_err("unknown command\n");
0405         break;
0406     }
0407 }
0408 EXPORT_SYMBOL_GPL(usbip_pack_pdu);
0409 
0410 static void correct_endian_basic(struct usbip_header_basic *base, int send)
0411 {
0412     if (send) {
0413         base->command   = cpu_to_be32(base->command);
0414         base->seqnum    = cpu_to_be32(base->seqnum);
0415         base->devid = cpu_to_be32(base->devid);
0416         base->direction = cpu_to_be32(base->direction);
0417         base->ep    = cpu_to_be32(base->ep);
0418     } else {
0419         base->command   = be32_to_cpu(base->command);
0420         base->seqnum    = be32_to_cpu(base->seqnum);
0421         base->devid = be32_to_cpu(base->devid);
0422         base->direction = be32_to_cpu(base->direction);
0423         base->ep    = be32_to_cpu(base->ep);
0424     }
0425 }
0426 
0427 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
0428                       int send)
0429 {
0430     if (send) {
0431         pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
0432 
0433         cpu_to_be32s(&pdu->transfer_buffer_length);
0434         cpu_to_be32s(&pdu->start_frame);
0435         cpu_to_be32s(&pdu->number_of_packets);
0436         cpu_to_be32s(&pdu->interval);
0437     } else {
0438         pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
0439 
0440         be32_to_cpus(&pdu->transfer_buffer_length);
0441         be32_to_cpus(&pdu->start_frame);
0442         be32_to_cpus(&pdu->number_of_packets);
0443         be32_to_cpus(&pdu->interval);
0444     }
0445 }
0446 
0447 static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
0448                       int send)
0449 {
0450     if (send) {
0451         cpu_to_be32s(&pdu->status);
0452         cpu_to_be32s(&pdu->actual_length);
0453         cpu_to_be32s(&pdu->start_frame);
0454         cpu_to_be32s(&pdu->number_of_packets);
0455         cpu_to_be32s(&pdu->error_count);
0456     } else {
0457         be32_to_cpus(&pdu->status);
0458         be32_to_cpus(&pdu->actual_length);
0459         be32_to_cpus(&pdu->start_frame);
0460         be32_to_cpus(&pdu->number_of_packets);
0461         be32_to_cpus(&pdu->error_count);
0462     }
0463 }
0464 
0465 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
0466                       int send)
0467 {
0468     if (send)
0469         pdu->seqnum = cpu_to_be32(pdu->seqnum);
0470     else
0471         pdu->seqnum = be32_to_cpu(pdu->seqnum);
0472 }
0473 
0474 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
0475                       int send)
0476 {
0477     if (send)
0478         cpu_to_be32s(&pdu->status);
0479     else
0480         be32_to_cpus(&pdu->status);
0481 }
0482 
0483 void usbip_header_correct_endian(struct usbip_header *pdu, int send)
0484 {
0485     __u32 cmd = 0;
0486 
0487     if (send)
0488         cmd = pdu->base.command;
0489 
0490     correct_endian_basic(&pdu->base, send);
0491 
0492     if (!send)
0493         cmd = pdu->base.command;
0494 
0495     switch (cmd) {
0496     case USBIP_CMD_SUBMIT:
0497         correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
0498         break;
0499     case USBIP_RET_SUBMIT:
0500         correct_endian_ret_submit(&pdu->u.ret_submit, send);
0501         break;
0502     case USBIP_CMD_UNLINK:
0503         correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
0504         break;
0505     case USBIP_RET_UNLINK:
0506         correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
0507         break;
0508     default:
0509         /* NOT REACHED */
0510         pr_err("unknown command\n");
0511         break;
0512     }
0513 }
0514 EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
0515 
0516 static void usbip_iso_packet_correct_endian(
0517         struct usbip_iso_packet_descriptor *iso, int send)
0518 {
0519     /* does not need all members. but copy all simply. */
0520     if (send) {
0521         iso->offset = cpu_to_be32(iso->offset);
0522         iso->length = cpu_to_be32(iso->length);
0523         iso->status = cpu_to_be32(iso->status);
0524         iso->actual_length = cpu_to_be32(iso->actual_length);
0525     } else {
0526         iso->offset = be32_to_cpu(iso->offset);
0527         iso->length = be32_to_cpu(iso->length);
0528         iso->status = be32_to_cpu(iso->status);
0529         iso->actual_length = be32_to_cpu(iso->actual_length);
0530     }
0531 }
0532 
0533 static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
0534                struct usb_iso_packet_descriptor *uiso, int pack)
0535 {
0536     if (pack) {
0537         iso->offset     = uiso->offset;
0538         iso->length     = uiso->length;
0539         iso->status     = uiso->status;
0540         iso->actual_length  = uiso->actual_length;
0541     } else {
0542         uiso->offset        = iso->offset;
0543         uiso->length        = iso->length;
0544         uiso->status        = iso->status;
0545         uiso->actual_length = iso->actual_length;
0546     }
0547 }
0548 
0549 /* must free buffer */
0550 struct usbip_iso_packet_descriptor*
0551 usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
0552 {
0553     struct usbip_iso_packet_descriptor *iso;
0554     int np = urb->number_of_packets;
0555     ssize_t size = np * sizeof(*iso);
0556     int i;
0557 
0558     iso = kzalloc(size, GFP_KERNEL);
0559     if (!iso)
0560         return NULL;
0561 
0562     for (i = 0; i < np; i++) {
0563         usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
0564         usbip_iso_packet_correct_endian(&iso[i], 1);
0565     }
0566 
0567     *bufflen = size;
0568 
0569     return iso;
0570 }
0571 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
0572 
0573 /* some members of urb must be substituted before. */
0574 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
0575 {
0576     void *buff;
0577     struct usbip_iso_packet_descriptor *iso;
0578     int np = urb->number_of_packets;
0579     int size = np * sizeof(*iso);
0580     int i;
0581     int ret;
0582     int total_length = 0;
0583 
0584     if (!usb_pipeisoc(urb->pipe))
0585         return 0;
0586 
0587     /* my Bluetooth dongle gets ISO URBs which are np = 0 */
0588     if (np == 0)
0589         return 0;
0590 
0591     buff = kzalloc(size, GFP_KERNEL);
0592     if (!buff)
0593         return -ENOMEM;
0594 
0595     ret = usbip_recv(ud->tcp_socket, buff, size);
0596     if (ret != size) {
0597         dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
0598             ret);
0599         kfree(buff);
0600 
0601         if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
0602             usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
0603         else
0604             usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
0605 
0606         return -EPIPE;
0607     }
0608 
0609     iso = (struct usbip_iso_packet_descriptor *) buff;
0610     for (i = 0; i < np; i++) {
0611         usbip_iso_packet_correct_endian(&iso[i], 0);
0612         usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
0613         total_length += urb->iso_frame_desc[i].actual_length;
0614     }
0615 
0616     kfree(buff);
0617 
0618     if (total_length != urb->actual_length) {
0619         dev_err(&urb->dev->dev,
0620             "total length of iso packets %d not equal to actual length of buffer %d\n",
0621             total_length, urb->actual_length);
0622 
0623         if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
0624             usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
0625         else
0626             usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
0627 
0628         return -EPIPE;
0629     }
0630 
0631     return ret;
0632 }
0633 EXPORT_SYMBOL_GPL(usbip_recv_iso);
0634 
0635 /*
0636  * This functions restores the padding which was removed for optimizing
0637  * the bandwidth during transfer over tcp/ip
0638  *
0639  * buffer and iso packets need to be stored and be in propeper endian in urb
0640  * before calling this function
0641  */
0642 void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
0643 {
0644     int np = urb->number_of_packets;
0645     int i;
0646     int actualoffset = urb->actual_length;
0647 
0648     if (!usb_pipeisoc(urb->pipe))
0649         return;
0650 
0651     /* if no packets or length of data is 0, then nothing to unpack */
0652     if (np == 0 || urb->actual_length == 0)
0653         return;
0654 
0655     /*
0656      * if actual_length is transfer_buffer_length then no padding is
0657      * present.
0658      */
0659     if (urb->actual_length == urb->transfer_buffer_length)
0660         return;
0661 
0662     /*
0663      * loop over all packets from last to first (to prevent overwriting
0664      * memory when padding) and move them into the proper place
0665      */
0666     for (i = np-1; i > 0; i--) {
0667         actualoffset -= urb->iso_frame_desc[i].actual_length;
0668         memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
0669             urb->transfer_buffer + actualoffset,
0670             urb->iso_frame_desc[i].actual_length);
0671     }
0672 }
0673 EXPORT_SYMBOL_GPL(usbip_pad_iso);
0674 
0675 /* some members of urb must be substituted before. */
0676 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
0677 {
0678     struct scatterlist *sg;
0679     int ret = 0;
0680     int recv;
0681     int size;
0682     int copy;
0683     int i;
0684 
0685     if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
0686         /* the direction of urb must be OUT. */
0687         if (usb_pipein(urb->pipe))
0688             return 0;
0689 
0690         size = urb->transfer_buffer_length;
0691     } else {
0692         /* the direction of urb must be IN. */
0693         if (usb_pipeout(urb->pipe))
0694             return 0;
0695 
0696         size = urb->actual_length;
0697     }
0698 
0699     /* no need to recv xbuff */
0700     if (!(size > 0))
0701         return 0;
0702 
0703     if (size > urb->transfer_buffer_length)
0704         /* should not happen, probably malicious packet */
0705         goto error;
0706 
0707     if (urb->num_sgs) {
0708         copy = size;
0709         for_each_sg(urb->sg, sg, urb->num_sgs, i) {
0710             int recv_size;
0711 
0712             if (copy < sg->length)
0713                 recv_size = copy;
0714             else
0715                 recv_size = sg->length;
0716 
0717             recv = usbip_recv(ud->tcp_socket, sg_virt(sg),
0718                         recv_size);
0719 
0720             if (recv != recv_size)
0721                 goto error;
0722 
0723             copy -= recv;
0724             ret += recv;
0725 
0726             if (!copy)
0727                 break;
0728         }
0729 
0730         if (ret != size)
0731             goto error;
0732     } else {
0733         ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
0734         if (ret != size)
0735             goto error;
0736     }
0737 
0738     return ret;
0739 
0740 error:
0741     dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
0742     if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
0743         usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
0744     else
0745         usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
0746 
0747     return -EPIPE;
0748 }
0749 EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
0750 
0751 static int __init usbip_core_init(void)
0752 {
0753     return usbip_init_eh();
0754 }
0755 
0756 static void __exit usbip_core_exit(void)
0757 {
0758     usbip_finish_eh();
0759     return;
0760 }
0761 
0762 module_init(usbip_core_init);
0763 module_exit(usbip_core_exit);
0764 
0765 MODULE_AUTHOR(DRIVER_AUTHOR);
0766 MODULE_DESCRIPTION(DRIVER_DESC);
0767 MODULE_LICENSE("GPL");