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