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