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 if (!sock || !buf || !size) 321 return -EINVAL; 322 323 iov_iter_kvec(&msg.msg_iter, READ|ITER_KVEC, &iov, 1, size); 324 325 usbip_dbg_xmit("enter\n"); 326 327 do { 328 msg_data_left(&msg); 329 sock->sk->sk_allocation = GFP_NOIO; 330 331 result = sock_recvmsg(sock, &msg, MSG_WAITALL); 332 if (result <= 0) 333 goto err; 334 335 total += result; 336 } while (msg_data_left(&msg)); 337 338 if (usbip_dbg_flag_xmit) { 339 if (!in_interrupt()) 340 pr_debug("%-10s:", current->comm); 341 else 342 pr_debug("interrupt :"); 343 344 pr_debug("receiving....\n"); 345 usbip_dump_buffer(buf, size); 346 pr_debug("received, osize %d ret %d size %zd total %d\n", 347 size, result, msg_data_left(&msg), total); 348 } 349 350 return total; 351 352 err: 353 return result; 354 } 355 EXPORT_SYMBOL_GPL(usbip_recv); 356 357 /* there may be more cases to tweak the flags. */ 358 static unsigned int tweak_transfer_flags(unsigned int flags) 359 { 360 flags &= ~URB_NO_TRANSFER_DMA_MAP; 361 return flags; 362 } 363 364 static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb, 365 int pack) 366 { 367 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit; 368 369 /* 370 * Some members are not still implemented in usbip. I hope this issue 371 * will be discussed when usbip is ported to other operating systems. 372 */ 373 if (pack) { 374 spdu->transfer_flags = 375 tweak_transfer_flags(urb->transfer_flags); 376 spdu->transfer_buffer_length = urb->transfer_buffer_length; 377 spdu->start_frame = urb->start_frame; 378 spdu->number_of_packets = urb->number_of_packets; 379 spdu->interval = urb->interval; 380 } else { 381 urb->transfer_flags = spdu->transfer_flags; 382 urb->transfer_buffer_length = spdu->transfer_buffer_length; 383 urb->start_frame = spdu->start_frame; 384 urb->number_of_packets = spdu->number_of_packets; 385 urb->interval = spdu->interval; 386 } 387 } 388 389 static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb, 390 int pack) 391 { 392 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit; 393 394 if (pack) { 395 rpdu->status = urb->status; 396 rpdu->actual_length = urb->actual_length; 397 rpdu->start_frame = urb->start_frame; 398 rpdu->number_of_packets = urb->number_of_packets; 399 rpdu->error_count = urb->error_count; 400 } else { 401 urb->status = rpdu->status; 402 urb->actual_length = rpdu->actual_length; 403 urb->start_frame = rpdu->start_frame; 404 urb->number_of_packets = rpdu->number_of_packets; 405 urb->error_count = rpdu->error_count; 406 } 407 } 408 409 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd, 410 int pack) 411 { 412 switch (cmd) { 413 case USBIP_CMD_SUBMIT: 414 usbip_pack_cmd_submit(pdu, urb, pack); 415 break; 416 case USBIP_RET_SUBMIT: 417 usbip_pack_ret_submit(pdu, urb, pack); 418 break; 419 default: 420 /* NOT REACHED */ 421 pr_err("unknown command\n"); 422 break; 423 } 424 } 425 EXPORT_SYMBOL_GPL(usbip_pack_pdu); 426 427 static void correct_endian_basic(struct usbip_header_basic *base, int send) 428 { 429 if (send) { 430 base->command = cpu_to_be32(base->command); 431 base->seqnum = cpu_to_be32(base->seqnum); 432 base->devid = cpu_to_be32(base->devid); 433 base->direction = cpu_to_be32(base->direction); 434 base->ep = cpu_to_be32(base->ep); 435 } else { 436 base->command = be32_to_cpu(base->command); 437 base->seqnum = be32_to_cpu(base->seqnum); 438 base->devid = be32_to_cpu(base->devid); 439 base->direction = be32_to_cpu(base->direction); 440 base->ep = be32_to_cpu(base->ep); 441 } 442 } 443 444 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu, 445 int send) 446 { 447 if (send) { 448 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags); 449 450 cpu_to_be32s(&pdu->transfer_buffer_length); 451 cpu_to_be32s(&pdu->start_frame); 452 cpu_to_be32s(&pdu->number_of_packets); 453 cpu_to_be32s(&pdu->interval); 454 } else { 455 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags); 456 457 be32_to_cpus(&pdu->transfer_buffer_length); 458 be32_to_cpus(&pdu->start_frame); 459 be32_to_cpus(&pdu->number_of_packets); 460 be32_to_cpus(&pdu->interval); 461 } 462 } 463 464 static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu, 465 int send) 466 { 467 if (send) { 468 cpu_to_be32s(&pdu->status); 469 cpu_to_be32s(&pdu->actual_length); 470 cpu_to_be32s(&pdu->start_frame); 471 cpu_to_be32s(&pdu->number_of_packets); 472 cpu_to_be32s(&pdu->error_count); 473 } else { 474 be32_to_cpus(&pdu->status); 475 be32_to_cpus(&pdu->actual_length); 476 be32_to_cpus(&pdu->start_frame); 477 be32_to_cpus(&pdu->number_of_packets); 478 be32_to_cpus(&pdu->error_count); 479 } 480 } 481 482 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu, 483 int send) 484 { 485 if (send) 486 pdu->seqnum = cpu_to_be32(pdu->seqnum); 487 else 488 pdu->seqnum = be32_to_cpu(pdu->seqnum); 489 } 490 491 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu, 492 int send) 493 { 494 if (send) 495 cpu_to_be32s(&pdu->status); 496 else 497 be32_to_cpus(&pdu->status); 498 } 499 500 void usbip_header_correct_endian(struct usbip_header *pdu, int send) 501 { 502 __u32 cmd = 0; 503 504 if (send) 505 cmd = pdu->base.command; 506 507 correct_endian_basic(&pdu->base, send); 508 509 if (!send) 510 cmd = pdu->base.command; 511 512 switch (cmd) { 513 case USBIP_CMD_SUBMIT: 514 correct_endian_cmd_submit(&pdu->u.cmd_submit, send); 515 break; 516 case USBIP_RET_SUBMIT: 517 correct_endian_ret_submit(&pdu->u.ret_submit, send); 518 break; 519 case USBIP_CMD_UNLINK: 520 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send); 521 break; 522 case USBIP_RET_UNLINK: 523 correct_endian_ret_unlink(&pdu->u.ret_unlink, send); 524 break; 525 default: 526 /* NOT REACHED */ 527 pr_err("unknown command\n"); 528 break; 529 } 530 } 531 EXPORT_SYMBOL_GPL(usbip_header_correct_endian); 532 533 static void usbip_iso_packet_correct_endian( 534 struct usbip_iso_packet_descriptor *iso, int send) 535 { 536 /* does not need all members. but copy all simply. */ 537 if (send) { 538 iso->offset = cpu_to_be32(iso->offset); 539 iso->length = cpu_to_be32(iso->length); 540 iso->status = cpu_to_be32(iso->status); 541 iso->actual_length = cpu_to_be32(iso->actual_length); 542 } else { 543 iso->offset = be32_to_cpu(iso->offset); 544 iso->length = be32_to_cpu(iso->length); 545 iso->status = be32_to_cpu(iso->status); 546 iso->actual_length = be32_to_cpu(iso->actual_length); 547 } 548 } 549 550 static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso, 551 struct usb_iso_packet_descriptor *uiso, int pack) 552 { 553 if (pack) { 554 iso->offset = uiso->offset; 555 iso->length = uiso->length; 556 iso->status = uiso->status; 557 iso->actual_length = uiso->actual_length; 558 } else { 559 uiso->offset = iso->offset; 560 uiso->length = iso->length; 561 uiso->status = iso->status; 562 uiso->actual_length = iso->actual_length; 563 } 564 } 565 566 /* must free buffer */ 567 struct usbip_iso_packet_descriptor* 568 usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen) 569 { 570 struct usbip_iso_packet_descriptor *iso; 571 int np = urb->number_of_packets; 572 ssize_t size = np * sizeof(*iso); 573 int i; 574 575 iso = kzalloc(size, GFP_KERNEL); 576 if (!iso) 577 return NULL; 578 579 for (i = 0; i < np; i++) { 580 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1); 581 usbip_iso_packet_correct_endian(&iso[i], 1); 582 } 583 584 *bufflen = size; 585 586 return iso; 587 } 588 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu); 589 590 /* some members of urb must be substituted before. */ 591 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb) 592 { 593 void *buff; 594 struct usbip_iso_packet_descriptor *iso; 595 int np = urb->number_of_packets; 596 int size = np * sizeof(*iso); 597 int i; 598 int ret; 599 int total_length = 0; 600 601 if (!usb_pipeisoc(urb->pipe)) 602 return 0; 603 604 /* my Bluetooth dongle gets ISO URBs which are np = 0 */ 605 if (np == 0) 606 return 0; 607 608 buff = kzalloc(size, GFP_KERNEL); 609 if (!buff) 610 return -ENOMEM; 611 612 ret = usbip_recv(ud->tcp_socket, buff, size); 613 if (ret != size) { 614 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n", 615 ret); 616 kfree(buff); 617 618 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) 619 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); 620 else 621 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); 622 623 return -EPIPE; 624 } 625 626 iso = (struct usbip_iso_packet_descriptor *) buff; 627 for (i = 0; i < np; i++) { 628 usbip_iso_packet_correct_endian(&iso[i], 0); 629 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0); 630 total_length += urb->iso_frame_desc[i].actual_length; 631 } 632 633 kfree(buff); 634 635 if (total_length != urb->actual_length) { 636 dev_err(&urb->dev->dev, 637 "total length of iso packets %d not equal to actual length of buffer %d\n", 638 total_length, urb->actual_length); 639 640 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) 641 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); 642 else 643 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); 644 645 return -EPIPE; 646 } 647 648 return ret; 649 } 650 EXPORT_SYMBOL_GPL(usbip_recv_iso); 651 652 /* 653 * This functions restores the padding which was removed for optimizing 654 * the bandwidth during transfer over tcp/ip 655 * 656 * buffer and iso packets need to be stored and be in propeper endian in urb 657 * before calling this function 658 */ 659 void usbip_pad_iso(struct usbip_device *ud, struct urb *urb) 660 { 661 int np = urb->number_of_packets; 662 int i; 663 int actualoffset = urb->actual_length; 664 665 if (!usb_pipeisoc(urb->pipe)) 666 return; 667 668 /* if no packets or length of data is 0, then nothing to unpack */ 669 if (np == 0 || urb->actual_length == 0) 670 return; 671 672 /* 673 * if actual_length is transfer_buffer_length then no padding is 674 * present. 675 */ 676 if (urb->actual_length == urb->transfer_buffer_length) 677 return; 678 679 /* 680 * loop over all packets from last to first (to prevent overwriting 681 * memory when padding) and move them into the proper place 682 */ 683 for (i = np-1; i > 0; i--) { 684 actualoffset -= urb->iso_frame_desc[i].actual_length; 685 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset, 686 urb->transfer_buffer + actualoffset, 687 urb->iso_frame_desc[i].actual_length); 688 } 689 } 690 EXPORT_SYMBOL_GPL(usbip_pad_iso); 691 692 /* some members of urb must be substituted before. */ 693 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb) 694 { 695 int ret; 696 int size; 697 698 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) { 699 /* the direction of urb must be OUT. */ 700 if (usb_pipein(urb->pipe)) 701 return 0; 702 703 size = urb->transfer_buffer_length; 704 } else { 705 /* the direction of urb must be IN. */ 706 if (usb_pipeout(urb->pipe)) 707 return 0; 708 709 size = urb->actual_length; 710 } 711 712 /* no need to recv xbuff */ 713 if (!(size > 0)) 714 return 0; 715 716 if (size > urb->transfer_buffer_length) { 717 /* should not happen, probably malicious packet */ 718 if (ud->side == USBIP_STUB) { 719 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); 720 return 0; 721 } else { 722 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); 723 return -EPIPE; 724 } 725 } 726 727 ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size); 728 if (ret != size) { 729 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret); 730 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) { 731 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); 732 } else { 733 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); 734 return -EPIPE; 735 } 736 } 737 738 return ret; 739 } 740 EXPORT_SYMBOL_GPL(usbip_recv_xbuff); 741 742 static int __init usbip_core_init(void) 743 { 744 int ret; 745 746 ret = usbip_init_eh(); 747 if (ret) 748 return ret; 749 750 return 0; 751 } 752 753 static void __exit usbip_core_exit(void) 754 { 755 usbip_finish_eh(); 756 return; 757 } 758 759 module_init(usbip_core_init); 760 module_exit(usbip_core_exit); 761 762 MODULE_AUTHOR(DRIVER_AUTHOR); 763 MODULE_DESCRIPTION(DRIVER_DESC); 764 MODULE_LICENSE("GPL"); 765