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