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