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