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