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