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 /* for blocks of if (usbip_dbg_flag_xmit) */
321 char *bp = buf;
322 int osize = size;
323
324 if (!sock || !buf || !size)
325 return -EINVAL;
326
327 usbip_dbg_xmit("enter\n");
328
329 do {
330 sock->sk->sk_allocation = GFP_NOIO;
331 iov.iov_base = buf;
332 iov.iov_len = size;
333 msg.msg_name = NULL;
334 msg.msg_namelen = 0;
335 msg.msg_control = NULL;
336 msg.msg_controllen = 0;
337 msg.msg_flags = MSG_NOSIGNAL;
338
339 result = kernel_recvmsg(sock, &msg, &iov, 1, size, MSG_WAITALL);
340 if (result <= 0)
341 goto err;
342
343 size -= result;
344 buf += result;
345 total += result;
346 } while (size > 0);
347
348 if (usbip_dbg_flag_xmit) {
349 if (!in_interrupt())
350 pr_debug("%-10s:", current->comm);
351 else
352 pr_debug("interrupt :");
353
354 pr_debug("receiving....\n");
355 usbip_dump_buffer(bp, osize);
356 pr_debug("received, osize %d ret %d size %d total %d\n",
357 osize, result, size, total);
358 }
359
360 return total;
361
362 err:
363 return result;
364 }
365 EXPORT_SYMBOL_GPL(usbip_recv);
366
367 /* there may be more cases to tweak the flags. */
tweak_transfer_flags(unsigned int flags)368 static unsigned int tweak_transfer_flags(unsigned int flags)
369 {
370 flags &= ~URB_NO_TRANSFER_DMA_MAP;
371 return flags;
372 }
373
usbip_pack_cmd_submit(struct usbip_header * pdu,struct urb * urb,int pack)374 static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
375 int pack)
376 {
377 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
378
379 /*
380 * Some members are not still implemented in usbip. I hope this issue
381 * will be discussed when usbip is ported to other operating systems.
382 */
383 if (pack) {
384 spdu->transfer_flags =
385 tweak_transfer_flags(urb->transfer_flags);
386 spdu->transfer_buffer_length = urb->transfer_buffer_length;
387 spdu->start_frame = urb->start_frame;
388 spdu->number_of_packets = urb->number_of_packets;
389 spdu->interval = urb->interval;
390 } else {
391 urb->transfer_flags = spdu->transfer_flags;
392 urb->transfer_buffer_length = spdu->transfer_buffer_length;
393 urb->start_frame = spdu->start_frame;
394 urb->number_of_packets = spdu->number_of_packets;
395 urb->interval = spdu->interval;
396 }
397 }
398
usbip_pack_ret_submit(struct usbip_header * pdu,struct urb * urb,int pack)399 static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
400 int pack)
401 {
402 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
403
404 if (pack) {
405 rpdu->status = urb->status;
406 rpdu->actual_length = urb->actual_length;
407 rpdu->start_frame = urb->start_frame;
408 rpdu->number_of_packets = urb->number_of_packets;
409 rpdu->error_count = urb->error_count;
410 } else {
411 urb->status = rpdu->status;
412 urb->actual_length = rpdu->actual_length;
413 urb->start_frame = rpdu->start_frame;
414 urb->number_of_packets = rpdu->number_of_packets;
415 urb->error_count = rpdu->error_count;
416 }
417 }
418
usbip_pack_pdu(struct usbip_header * pdu,struct urb * urb,int cmd,int pack)419 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
420 int pack)
421 {
422 switch (cmd) {
423 case USBIP_CMD_SUBMIT:
424 usbip_pack_cmd_submit(pdu, urb, pack);
425 break;
426 case USBIP_RET_SUBMIT:
427 usbip_pack_ret_submit(pdu, urb, pack);
428 break;
429 default:
430 /* NOT REACHED */
431 pr_err("unknown command\n");
432 break;
433 }
434 }
435 EXPORT_SYMBOL_GPL(usbip_pack_pdu);
436
correct_endian_basic(struct usbip_header_basic * base,int send)437 static void correct_endian_basic(struct usbip_header_basic *base, int send)
438 {
439 if (send) {
440 base->command = cpu_to_be32(base->command);
441 base->seqnum = cpu_to_be32(base->seqnum);
442 base->devid = cpu_to_be32(base->devid);
443 base->direction = cpu_to_be32(base->direction);
444 base->ep = cpu_to_be32(base->ep);
445 } else {
446 base->command = be32_to_cpu(base->command);
447 base->seqnum = be32_to_cpu(base->seqnum);
448 base->devid = be32_to_cpu(base->devid);
449 base->direction = be32_to_cpu(base->direction);
450 base->ep = be32_to_cpu(base->ep);
451 }
452 }
453
correct_endian_cmd_submit(struct usbip_header_cmd_submit * pdu,int send)454 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
455 int send)
456 {
457 if (send) {
458 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
459
460 cpu_to_be32s(&pdu->transfer_buffer_length);
461 cpu_to_be32s(&pdu->start_frame);
462 cpu_to_be32s(&pdu->number_of_packets);
463 cpu_to_be32s(&pdu->interval);
464 } else {
465 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
466
467 be32_to_cpus(&pdu->transfer_buffer_length);
468 be32_to_cpus(&pdu->start_frame);
469 be32_to_cpus(&pdu->number_of_packets);
470 be32_to_cpus(&pdu->interval);
471 }
472 }
473
correct_endian_ret_submit(struct usbip_header_ret_submit * pdu,int send)474 static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
475 int send)
476 {
477 if (send) {
478 cpu_to_be32s(&pdu->status);
479 cpu_to_be32s(&pdu->actual_length);
480 cpu_to_be32s(&pdu->start_frame);
481 cpu_to_be32s(&pdu->number_of_packets);
482 cpu_to_be32s(&pdu->error_count);
483 } else {
484 be32_to_cpus(&pdu->status);
485 be32_to_cpus(&pdu->actual_length);
486 be32_to_cpus(&pdu->start_frame);
487 be32_to_cpus(&pdu->number_of_packets);
488 be32_to_cpus(&pdu->error_count);
489 }
490 }
491
correct_endian_cmd_unlink(struct usbip_header_cmd_unlink * pdu,int send)492 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
493 int send)
494 {
495 if (send)
496 pdu->seqnum = cpu_to_be32(pdu->seqnum);
497 else
498 pdu->seqnum = be32_to_cpu(pdu->seqnum);
499 }
500
correct_endian_ret_unlink(struct usbip_header_ret_unlink * pdu,int send)501 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
502 int send)
503 {
504 if (send)
505 cpu_to_be32s(&pdu->status);
506 else
507 be32_to_cpus(&pdu->status);
508 }
509
usbip_header_correct_endian(struct usbip_header * pdu,int send)510 void usbip_header_correct_endian(struct usbip_header *pdu, int send)
511 {
512 __u32 cmd = 0;
513
514 if (send)
515 cmd = pdu->base.command;
516
517 correct_endian_basic(&pdu->base, send);
518
519 if (!send)
520 cmd = pdu->base.command;
521
522 switch (cmd) {
523 case USBIP_CMD_SUBMIT:
524 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
525 break;
526 case USBIP_RET_SUBMIT:
527 correct_endian_ret_submit(&pdu->u.ret_submit, send);
528 break;
529 case USBIP_CMD_UNLINK:
530 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
531 break;
532 case USBIP_RET_UNLINK:
533 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
534 break;
535 default:
536 /* NOT REACHED */
537 pr_err("unknown command\n");
538 break;
539 }
540 }
541 EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
542
usbip_iso_packet_correct_endian(struct usbip_iso_packet_descriptor * iso,int send)543 static void usbip_iso_packet_correct_endian(
544 struct usbip_iso_packet_descriptor *iso, int send)
545 {
546 /* does not need all members. but copy all simply. */
547 if (send) {
548 iso->offset = cpu_to_be32(iso->offset);
549 iso->length = cpu_to_be32(iso->length);
550 iso->status = cpu_to_be32(iso->status);
551 iso->actual_length = cpu_to_be32(iso->actual_length);
552 } else {
553 iso->offset = be32_to_cpu(iso->offset);
554 iso->length = be32_to_cpu(iso->length);
555 iso->status = be32_to_cpu(iso->status);
556 iso->actual_length = be32_to_cpu(iso->actual_length);
557 }
558 }
559
usbip_pack_iso(struct usbip_iso_packet_descriptor * iso,struct usb_iso_packet_descriptor * uiso,int pack)560 static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
561 struct usb_iso_packet_descriptor *uiso, int pack)
562 {
563 if (pack) {
564 iso->offset = uiso->offset;
565 iso->length = uiso->length;
566 iso->status = uiso->status;
567 iso->actual_length = uiso->actual_length;
568 } else {
569 uiso->offset = iso->offset;
570 uiso->length = iso->length;
571 uiso->status = iso->status;
572 uiso->actual_length = iso->actual_length;
573 }
574 }
575
576 /* must free buffer */
577 struct usbip_iso_packet_descriptor*
usbip_alloc_iso_desc_pdu(struct urb * urb,ssize_t * bufflen)578 usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
579 {
580 struct usbip_iso_packet_descriptor *iso;
581 int np = urb->number_of_packets;
582 ssize_t size = np * sizeof(*iso);
583 int i;
584
585 iso = kzalloc(size, GFP_KERNEL);
586 if (!iso)
587 return NULL;
588
589 for (i = 0; i < np; i++) {
590 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
591 usbip_iso_packet_correct_endian(&iso[i], 1);
592 }
593
594 *bufflen = size;
595
596 return iso;
597 }
598 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
599
600 /* some members of urb must be substituted before. */
usbip_recv_iso(struct usbip_device * ud,struct urb * urb)601 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
602 {
603 void *buff;
604 struct usbip_iso_packet_descriptor *iso;
605 int np = urb->number_of_packets;
606 int size = np * sizeof(*iso);
607 int i;
608 int ret;
609 int total_length = 0;
610
611 if (!usb_pipeisoc(urb->pipe))
612 return 0;
613
614 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
615 if (np == 0)
616 return 0;
617
618 buff = kzalloc(size, GFP_KERNEL);
619 if (!buff)
620 return -ENOMEM;
621
622 ret = usbip_recv(ud->tcp_socket, buff, size);
623 if (ret != size) {
624 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
625 ret);
626 kfree(buff);
627
628 if (ud->side == USBIP_STUB)
629 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
630 else
631 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
632
633 return -EPIPE;
634 }
635
636 iso = (struct usbip_iso_packet_descriptor *) buff;
637 for (i = 0; i < np; i++) {
638 usbip_iso_packet_correct_endian(&iso[i], 0);
639 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
640 total_length += urb->iso_frame_desc[i].actual_length;
641 }
642
643 kfree(buff);
644
645 if (total_length != urb->actual_length) {
646 dev_err(&urb->dev->dev,
647 "total length of iso packets %d not equal to actual length of buffer %d\n",
648 total_length, urb->actual_length);
649
650 if (ud->side == USBIP_STUB)
651 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
652 else
653 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
654
655 return -EPIPE;
656 }
657
658 return ret;
659 }
660 EXPORT_SYMBOL_GPL(usbip_recv_iso);
661
662 /*
663 * This functions restores the padding which was removed for optimizing
664 * the bandwidth during transfer over tcp/ip
665 *
666 * buffer and iso packets need to be stored and be in propeper endian in urb
667 * before calling this function
668 */
usbip_pad_iso(struct usbip_device * ud,struct urb * urb)669 void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
670 {
671 int np = urb->number_of_packets;
672 int i;
673 int actualoffset = urb->actual_length;
674
675 if (!usb_pipeisoc(urb->pipe))
676 return;
677
678 /* if no packets or length of data is 0, then nothing to unpack */
679 if (np == 0 || urb->actual_length == 0)
680 return;
681
682 /*
683 * if actual_length is transfer_buffer_length then no padding is
684 * present.
685 */
686 if (urb->actual_length == urb->transfer_buffer_length)
687 return;
688
689 /*
690 * loop over all packets from last to first (to prevent overwritting
691 * memory when padding) and move them into the proper place
692 */
693 for (i = np-1; i > 0; i--) {
694 actualoffset -= urb->iso_frame_desc[i].actual_length;
695 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
696 urb->transfer_buffer + actualoffset,
697 urb->iso_frame_desc[i].actual_length);
698 }
699 }
700 EXPORT_SYMBOL_GPL(usbip_pad_iso);
701
702 /* some members of urb must be substituted before. */
usbip_recv_xbuff(struct usbip_device * ud,struct urb * urb)703 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
704 {
705 int ret;
706 int size;
707
708 if (ud->side == USBIP_STUB) {
709 /* the direction of urb must be OUT. */
710 if (usb_pipein(urb->pipe))
711 return 0;
712
713 size = urb->transfer_buffer_length;
714 } else {
715 /* the direction of urb must be IN. */
716 if (usb_pipeout(urb->pipe))
717 return 0;
718
719 size = urb->actual_length;
720 }
721
722 /* no need to recv xbuff */
723 if (!(size > 0))
724 return 0;
725
726 if (size > urb->transfer_buffer_length) {
727 /* should not happen, probably malicious packet */
728 if (ud->side == USBIP_STUB) {
729 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
730 return 0;
731 } else {
732 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
733 return -EPIPE;
734 }
735 }
736
737 ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
738 if (ret != size) {
739 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
740 if (ud->side == USBIP_STUB) {
741 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
742 } else {
743 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
744 return -EPIPE;
745 }
746 }
747
748 return ret;
749 }
750 EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
751
usbip_core_init(void)752 static int __init usbip_core_init(void)
753 {
754 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
755 return 0;
756 }
757
usbip_core_exit(void)758 static void __exit usbip_core_exit(void)
759 {
760 return;
761 }
762
763 module_init(usbip_core_init);
764 module_exit(usbip_core_exit);
765
766 MODULE_AUTHOR(DRIVER_AUTHOR);
767 MODULE_DESCRIPTION(DRIVER_DESC);
768 MODULE_LICENSE("GPL");
769 MODULE_VERSION(USBIP_VERSION);
770