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