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