Loading...
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
24unsigned long usbip_debug_flag = 0xffffffff;
25#else
26unsigned long usbip_debug_flag;
27#endif
28EXPORT_SYMBOL_GPL(usbip_debug_flag);
29module_param(usbip_debug_flag, ulong, S_IRUGO|S_IWUSR);
30MODULE_PARM_DESC(usbip_debug_flag, "debug flags (defined in usbip_common.h)");
31
32/* FIXME */
33struct device_attribute dev_attr_usbip_debug;
34EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
35
36static 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
42static 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}
50DEVICE_ATTR_RW(usbip_debug);
51
52static 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
58static 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
86static 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
136static 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
157static 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
220void 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}
257EXPORT_SYMBOL_GPL(usbip_dump_urb);
258
259void 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}
299EXPORT_SYMBOL_GPL(usbip_dump_header);
300
301/* Receive data over TCP/IP. */
302int 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, ITER_DEST, &iov, 1, size);
313
314 usbip_dbg_xmit("enter\n");
315
316 do {
317 sock->sk->sk_allocation = GFP_NOIO;
318 sock->sk->sk_use_task_frag = false;
319
320 result = sock_recvmsg(sock, &msg, MSG_WAITALL);
321 if (result <= 0)
322 goto err;
323
324 total += result;
325 } while (msg_data_left(&msg));
326
327 if (usbip_dbg_flag_xmit) {
328 pr_debug("receiving....\n");
329 usbip_dump_buffer(buf, size);
330 pr_debug("received, osize %d ret %d size %zd total %d\n",
331 size, result, msg_data_left(&msg), total);
332 }
333
334 return total;
335
336err:
337 return result;
338}
339EXPORT_SYMBOL_GPL(usbip_recv);
340
341/* there may be more cases to tweak the flags. */
342static unsigned int tweak_transfer_flags(unsigned int flags)
343{
344 flags &= ~URB_NO_TRANSFER_DMA_MAP;
345 return flags;
346}
347
348/*
349 * USBIP driver packs URB transfer flags in PDUs that are exchanged
350 * between Server (usbip_host) and Client (vhci_hcd). URB_* flags
351 * are internal to kernel and could change. Where as USBIP URB flags
352 * exchanged in PDUs are USBIP user API must not change.
353 *
354 * USBIP_URB* flags are exported as explicit API and client and server
355 * do mapping from kernel flags to USBIP_URB*. Details as follows:
356 *
357 * Client tx path (USBIP_CMD_SUBMIT):
358 * - Maps URB_* to USBIP_URB_* when it sends USBIP_CMD_SUBMIT packet.
359 *
360 * Server rx path (USBIP_CMD_SUBMIT):
361 * - Maps USBIP_URB_* to URB_* when it receives USBIP_CMD_SUBMIT packet.
362 *
363 * Flags aren't included in USBIP_CMD_UNLINK and USBIP_RET_SUBMIT packets
364 * and no special handling is needed for them in the following cases:
365 * - Server rx path (USBIP_CMD_UNLINK)
366 * - Client rx path & Server tx path (USBIP_RET_SUBMIT)
367 *
368 * Code paths:
369 * usbip_pack_pdu() is the common routine that handles packing pdu from
370 * urb and unpack pdu to an urb.
371 *
372 * usbip_pack_cmd_submit() and usbip_pack_ret_submit() handle
373 * USBIP_CMD_SUBMIT and USBIP_RET_SUBMIT respectively.
374 *
375 * usbip_map_urb_to_usbip() and usbip_map_usbip_to_urb() are used
376 * by usbip_pack_cmd_submit() and usbip_pack_ret_submit() to map
377 * flags.
378 */
379
380struct urb_to_usbip_flags {
381 u32 urb_flag;
382 u32 usbip_flag;
383};
384
385#define NUM_USBIP_FLAGS 17
386
387static const struct urb_to_usbip_flags flag_map[NUM_USBIP_FLAGS] = {
388 {URB_SHORT_NOT_OK, USBIP_URB_SHORT_NOT_OK},
389 {URB_ISO_ASAP, USBIP_URB_ISO_ASAP},
390 {URB_NO_TRANSFER_DMA_MAP, USBIP_URB_NO_TRANSFER_DMA_MAP},
391 {URB_ZERO_PACKET, USBIP_URB_ZERO_PACKET},
392 {URB_NO_INTERRUPT, USBIP_URB_NO_INTERRUPT},
393 {URB_FREE_BUFFER, USBIP_URB_FREE_BUFFER},
394 {URB_DIR_IN, USBIP_URB_DIR_IN},
395 {URB_DIR_OUT, USBIP_URB_DIR_OUT},
396 {URB_DIR_MASK, USBIP_URB_DIR_MASK},
397 {URB_DMA_MAP_SINGLE, USBIP_URB_DMA_MAP_SINGLE},
398 {URB_DMA_MAP_PAGE, USBIP_URB_DMA_MAP_PAGE},
399 {URB_DMA_MAP_SG, USBIP_URB_DMA_MAP_SG},
400 {URB_MAP_LOCAL, USBIP_URB_MAP_LOCAL},
401 {URB_SETUP_MAP_SINGLE, USBIP_URB_SETUP_MAP_SINGLE},
402 {URB_SETUP_MAP_LOCAL, USBIP_URB_SETUP_MAP_LOCAL},
403 {URB_DMA_SG_COMBINED, USBIP_URB_DMA_SG_COMBINED},
404 {URB_ALIGNED_TEMP_BUFFER, USBIP_URB_ALIGNED_TEMP_BUFFER},
405};
406
407static unsigned int urb_to_usbip(unsigned int flags)
408{
409 unsigned int map_flags = 0;
410 int loop;
411
412 for (loop = 0; loop < NUM_USBIP_FLAGS; loop++) {
413 if (flags & flag_map[loop].urb_flag)
414 map_flags |= flag_map[loop].usbip_flag;
415 }
416
417 return map_flags;
418}
419
420static unsigned int usbip_to_urb(unsigned int flags)
421{
422 unsigned int map_flags = 0;
423 int loop;
424
425 for (loop = 0; loop < NUM_USBIP_FLAGS; loop++) {
426 if (flags & flag_map[loop].usbip_flag)
427 map_flags |= flag_map[loop].urb_flag;
428 }
429
430 return map_flags;
431}
432
433static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
434 int pack)
435{
436 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
437
438 /*
439 * Some members are not still implemented in usbip. I hope this issue
440 * will be discussed when usbip is ported to other operating systems.
441 */
442 if (pack) {
443 /* map after tweaking the urb flags */
444 spdu->transfer_flags = urb_to_usbip(tweak_transfer_flags(urb->transfer_flags));
445 spdu->transfer_buffer_length = urb->transfer_buffer_length;
446 spdu->start_frame = urb->start_frame;
447 spdu->number_of_packets = urb->number_of_packets;
448 spdu->interval = urb->interval;
449 } else {
450 urb->transfer_flags = usbip_to_urb(spdu->transfer_flags);
451 urb->transfer_buffer_length = spdu->transfer_buffer_length;
452 urb->start_frame = spdu->start_frame;
453 urb->number_of_packets = spdu->number_of_packets;
454 urb->interval = spdu->interval;
455 }
456}
457
458static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
459 int pack)
460{
461 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
462
463 if (pack) {
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 urb->status = rpdu->status;
471 urb->actual_length = rpdu->actual_length;
472 urb->start_frame = rpdu->start_frame;
473 urb->number_of_packets = rpdu->number_of_packets;
474 urb->error_count = rpdu->error_count;
475 }
476}
477
478void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
479 int pack)
480{
481 switch (cmd) {
482 case USBIP_CMD_SUBMIT:
483 usbip_pack_cmd_submit(pdu, urb, pack);
484 break;
485 case USBIP_RET_SUBMIT:
486 usbip_pack_ret_submit(pdu, urb, pack);
487 break;
488 default:
489 /* NOT REACHED */
490 pr_err("unknown command\n");
491 break;
492 }
493}
494EXPORT_SYMBOL_GPL(usbip_pack_pdu);
495
496static void correct_endian_basic(struct usbip_header_basic *base, int send)
497{
498 if (send) {
499 base->command = cpu_to_be32(base->command);
500 base->seqnum = cpu_to_be32(base->seqnum);
501 base->devid = cpu_to_be32(base->devid);
502 base->direction = cpu_to_be32(base->direction);
503 base->ep = cpu_to_be32(base->ep);
504 } else {
505 base->command = be32_to_cpu(base->command);
506 base->seqnum = be32_to_cpu(base->seqnum);
507 base->devid = be32_to_cpu(base->devid);
508 base->direction = be32_to_cpu(base->direction);
509 base->ep = be32_to_cpu(base->ep);
510 }
511}
512
513static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
514 int send)
515{
516 if (send) {
517 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
518
519 cpu_to_be32s(&pdu->transfer_buffer_length);
520 cpu_to_be32s(&pdu->start_frame);
521 cpu_to_be32s(&pdu->number_of_packets);
522 cpu_to_be32s(&pdu->interval);
523 } else {
524 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
525
526 be32_to_cpus(&pdu->transfer_buffer_length);
527 be32_to_cpus(&pdu->start_frame);
528 be32_to_cpus(&pdu->number_of_packets);
529 be32_to_cpus(&pdu->interval);
530 }
531}
532
533static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
534 int send)
535{
536 if (send) {
537 cpu_to_be32s(&pdu->status);
538 cpu_to_be32s(&pdu->actual_length);
539 cpu_to_be32s(&pdu->start_frame);
540 cpu_to_be32s(&pdu->number_of_packets);
541 cpu_to_be32s(&pdu->error_count);
542 } else {
543 be32_to_cpus(&pdu->status);
544 be32_to_cpus(&pdu->actual_length);
545 be32_to_cpus(&pdu->start_frame);
546 be32_to_cpus(&pdu->number_of_packets);
547 be32_to_cpus(&pdu->error_count);
548 }
549}
550
551static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
552 int send)
553{
554 if (send)
555 pdu->seqnum = cpu_to_be32(pdu->seqnum);
556 else
557 pdu->seqnum = be32_to_cpu(pdu->seqnum);
558}
559
560static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
561 int send)
562{
563 if (send)
564 cpu_to_be32s(&pdu->status);
565 else
566 be32_to_cpus(&pdu->status);
567}
568
569void usbip_header_correct_endian(struct usbip_header *pdu, int send)
570{
571 __u32 cmd = 0;
572
573 if (send)
574 cmd = pdu->base.command;
575
576 correct_endian_basic(&pdu->base, send);
577
578 if (!send)
579 cmd = pdu->base.command;
580
581 switch (cmd) {
582 case USBIP_CMD_SUBMIT:
583 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
584 break;
585 case USBIP_RET_SUBMIT:
586 correct_endian_ret_submit(&pdu->u.ret_submit, send);
587 break;
588 case USBIP_CMD_UNLINK:
589 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
590 break;
591 case USBIP_RET_UNLINK:
592 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
593 break;
594 default:
595 /* NOT REACHED */
596 pr_err("unknown command\n");
597 break;
598 }
599}
600EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
601
602static void usbip_iso_packet_correct_endian(
603 struct usbip_iso_packet_descriptor *iso, int send)
604{
605 /* does not need all members. but copy all simply. */
606 if (send) {
607 iso->offset = cpu_to_be32(iso->offset);
608 iso->length = cpu_to_be32(iso->length);
609 iso->status = cpu_to_be32(iso->status);
610 iso->actual_length = cpu_to_be32(iso->actual_length);
611 } else {
612 iso->offset = be32_to_cpu(iso->offset);
613 iso->length = be32_to_cpu(iso->length);
614 iso->status = be32_to_cpu(iso->status);
615 iso->actual_length = be32_to_cpu(iso->actual_length);
616 }
617}
618
619static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
620 struct usb_iso_packet_descriptor *uiso, int pack)
621{
622 if (pack) {
623 iso->offset = uiso->offset;
624 iso->length = uiso->length;
625 iso->status = uiso->status;
626 iso->actual_length = uiso->actual_length;
627 } else {
628 uiso->offset = iso->offset;
629 uiso->length = iso->length;
630 uiso->status = iso->status;
631 uiso->actual_length = iso->actual_length;
632 }
633}
634
635/* must free buffer */
636struct usbip_iso_packet_descriptor*
637usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
638{
639 struct usbip_iso_packet_descriptor *iso;
640 int np = urb->number_of_packets;
641 ssize_t size = np * sizeof(*iso);
642 int i;
643
644 iso = kzalloc(size, GFP_KERNEL);
645 if (!iso)
646 return NULL;
647
648 for (i = 0; i < np; i++) {
649 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
650 usbip_iso_packet_correct_endian(&iso[i], 1);
651 }
652
653 *bufflen = size;
654
655 return iso;
656}
657EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
658
659/* some members of urb must be substituted before. */
660int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
661{
662 void *buff;
663 struct usbip_iso_packet_descriptor *iso;
664 int np = urb->number_of_packets;
665 int size = np * sizeof(*iso);
666 int i;
667 int ret;
668 int total_length = 0;
669
670 if (!usb_pipeisoc(urb->pipe))
671 return 0;
672
673 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
674 if (np == 0)
675 return 0;
676
677 buff = kzalloc(size, GFP_KERNEL);
678 if (!buff)
679 return -ENOMEM;
680
681 ret = usbip_recv(ud->tcp_socket, buff, size);
682 if (ret != size) {
683 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
684 ret);
685 kfree(buff);
686
687 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
688 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
689 else
690 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
691
692 return -EPIPE;
693 }
694
695 iso = (struct usbip_iso_packet_descriptor *) buff;
696 for (i = 0; i < np; i++) {
697 usbip_iso_packet_correct_endian(&iso[i], 0);
698 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
699 total_length += urb->iso_frame_desc[i].actual_length;
700 }
701
702 kfree(buff);
703
704 if (total_length != urb->actual_length) {
705 dev_err(&urb->dev->dev,
706 "total length of iso packets %d not equal to actual length of buffer %d\n",
707 total_length, urb->actual_length);
708
709 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
710 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
711 else
712 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
713
714 return -EPIPE;
715 }
716
717 return ret;
718}
719EXPORT_SYMBOL_GPL(usbip_recv_iso);
720
721/*
722 * This functions restores the padding which was removed for optimizing
723 * the bandwidth during transfer over tcp/ip
724 *
725 * buffer and iso packets need to be stored and be in propeper endian in urb
726 * before calling this function
727 */
728void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
729{
730 int np = urb->number_of_packets;
731 int i;
732 int actualoffset = urb->actual_length;
733
734 if (!usb_pipeisoc(urb->pipe))
735 return;
736
737 /* if no packets or length of data is 0, then nothing to unpack */
738 if (np == 0 || urb->actual_length == 0)
739 return;
740
741 /*
742 * if actual_length is transfer_buffer_length then no padding is
743 * present.
744 */
745 if (urb->actual_length == urb->transfer_buffer_length)
746 return;
747
748 /*
749 * loop over all packets from last to first (to prevent overwriting
750 * memory when padding) and move them into the proper place
751 */
752 for (i = np-1; i > 0; i--) {
753 actualoffset -= urb->iso_frame_desc[i].actual_length;
754 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
755 urb->transfer_buffer + actualoffset,
756 urb->iso_frame_desc[i].actual_length);
757 }
758}
759EXPORT_SYMBOL_GPL(usbip_pad_iso);
760
761/* some members of urb must be substituted before. */
762int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
763{
764 struct scatterlist *sg;
765 int ret = 0;
766 int recv;
767 int size;
768 int copy;
769 int i;
770
771 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
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 if (size > urb->transfer_buffer_length)
790 /* should not happen, probably malicious packet */
791 goto error;
792
793 if (urb->num_sgs) {
794 copy = size;
795 for_each_sg(urb->sg, sg, urb->num_sgs, i) {
796 int recv_size;
797
798 if (copy < sg->length)
799 recv_size = copy;
800 else
801 recv_size = sg->length;
802
803 recv = usbip_recv(ud->tcp_socket, sg_virt(sg),
804 recv_size);
805
806 if (recv != recv_size)
807 goto error;
808
809 copy -= recv;
810 ret += recv;
811
812 if (!copy)
813 break;
814 }
815
816 if (ret != size)
817 goto error;
818 } else {
819 ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
820 if (ret != size)
821 goto error;
822 }
823
824 return ret;
825
826error:
827 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
828 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
829 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
830 else
831 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
832
833 return -EPIPE;
834}
835EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
836
837static int __init usbip_core_init(void)
838{
839 return usbip_init_eh();
840}
841
842static void __exit usbip_core_exit(void)
843{
844 usbip_finish_eh();
845 return;
846}
847
848module_init(usbip_core_init);
849module_exit(usbip_core_exit);
850
851MODULE_AUTHOR(DRIVER_AUTHOR);
852MODULE_DESCRIPTION(DRIVER_DESC);
853MODULE_LICENSE("GPL");
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
38unsigned long usbip_debug_flag = 0xffffffff;
39#else
40unsigned long usbip_debug_flag;
41#endif
42EXPORT_SYMBOL_GPL(usbip_debug_flag);
43module_param(usbip_debug_flag, ulong, S_IRUGO|S_IWUSR);
44MODULE_PARM_DESC(usbip_debug_flag, "debug flags (defined in usbip_common.h)");
45
46/* FIXME */
47struct device_attribute dev_attr_usbip_debug;
48EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
49
50static 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
56static 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}
64DEVICE_ATTR_RW(usbip_debug);
65
66static 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
72static 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
100static 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 %p, ttport %d\n", udev->tt, 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 %p, bus %p\n", udev->parent, udev->bus);
142
143 dev_dbg(dev,
144 "descriptor %p, config %p, actconfig %p, rawdescriptors %p\n",
145 &udev->descriptor, udev->config,
146 udev->actconfig, udev->rawdescriptors);
147
148 dev_dbg(dev, "have_langid %d, string_langid %d\n",
149 udev->have_langid, udev->string_langid);
150
151 dev_dbg(dev, "maxchild %d\n", udev->maxchild);
152}
153
154static void usbip_dump_request_type(__u8 rt)
155{
156 switch (rt & USB_RECIP_MASK) {
157 case USB_RECIP_DEVICE:
158 pr_debug("DEVICE");
159 break;
160 case USB_RECIP_INTERFACE:
161 pr_debug("INTERF");
162 break;
163 case USB_RECIP_ENDPOINT:
164 pr_debug("ENDPOI");
165 break;
166 case USB_RECIP_OTHER:
167 pr_debug("OTHER ");
168 break;
169 default:
170 pr_debug("------");
171 break;
172 }
173}
174
175static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
176{
177 if (!cmd) {
178 pr_debug(" : null pointer\n");
179 return;
180 }
181
182 pr_debug(" ");
183 pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) wLength(%04X) ",
184 cmd->bRequestType, cmd->bRequest,
185 cmd->wValue, cmd->wIndex, cmd->wLength);
186 pr_debug("\n ");
187
188 if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
189 pr_debug("STANDARD ");
190 switch (cmd->bRequest) {
191 case USB_REQ_GET_STATUS:
192 pr_debug("GET_STATUS\n");
193 break;
194 case USB_REQ_CLEAR_FEATURE:
195 pr_debug("CLEAR_FEAT\n");
196 break;
197 case USB_REQ_SET_FEATURE:
198 pr_debug("SET_FEAT\n");
199 break;
200 case USB_REQ_SET_ADDRESS:
201 pr_debug("SET_ADDRRS\n");
202 break;
203 case USB_REQ_GET_DESCRIPTOR:
204 pr_debug("GET_DESCRI\n");
205 break;
206 case USB_REQ_SET_DESCRIPTOR:
207 pr_debug("SET_DESCRI\n");
208 break;
209 case USB_REQ_GET_CONFIGURATION:
210 pr_debug("GET_CONFIG\n");
211 break;
212 case USB_REQ_SET_CONFIGURATION:
213 pr_debug("SET_CONFIG\n");
214 break;
215 case USB_REQ_GET_INTERFACE:
216 pr_debug("GET_INTERF\n");
217 break;
218 case USB_REQ_SET_INTERFACE:
219 pr_debug("SET_INTERF\n");
220 break;
221 case USB_REQ_SYNCH_FRAME:
222 pr_debug("SYNC_FRAME\n");
223 break;
224 default:
225 pr_debug("REQ(%02X)\n", cmd->bRequest);
226 break;
227 }
228 usbip_dump_request_type(cmd->bRequestType);
229 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
230 pr_debug("CLASS\n");
231 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
232 pr_debug("VENDOR\n");
233 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
234 pr_debug("RESERVED\n");
235 }
236}
237
238void usbip_dump_urb(struct urb *urb)
239{
240 struct device *dev;
241
242 if (!urb) {
243 pr_debug("urb: null pointer!!\n");
244 return;
245 }
246
247 if (!urb->dev) {
248 pr_debug("urb->dev: null pointer!!\n");
249 return;
250 }
251
252 dev = &urb->dev->dev;
253
254 dev_dbg(dev, " urb :%p\n", urb);
255 dev_dbg(dev, " dev :%p\n", urb->dev);
256
257 usbip_dump_usb_device(urb->dev);
258
259 dev_dbg(dev, " pipe :%08x ", urb->pipe);
260
261 usbip_dump_pipe(urb->pipe);
262
263 dev_dbg(dev, " status :%d\n", urb->status);
264 dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
265 dev_dbg(dev, " transfer_buffer :%p\n", urb->transfer_buffer);
266 dev_dbg(dev, " transfer_buffer_length:%d\n",
267 urb->transfer_buffer_length);
268 dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
269 dev_dbg(dev, " setup_packet :%p\n", urb->setup_packet);
270
271 if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
272 usbip_dump_usb_ctrlrequest(
273 (struct usb_ctrlrequest *)urb->setup_packet);
274
275 dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
276 dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
277 dev_dbg(dev, " interval :%d\n", urb->interval);
278 dev_dbg(dev, " error_count :%d\n", urb->error_count);
279 dev_dbg(dev, " context :%p\n", urb->context);
280 dev_dbg(dev, " complete :%p\n", urb->complete);
281}
282EXPORT_SYMBOL_GPL(usbip_dump_urb);
283
284void usbip_dump_header(struct usbip_header *pdu)
285{
286 pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
287 pdu->base.command,
288 pdu->base.seqnum,
289 pdu->base.devid,
290 pdu->base.direction,
291 pdu->base.ep);
292
293 switch (pdu->base.command) {
294 case USBIP_CMD_SUBMIT:
295 pr_debug("USBIP_CMD_SUBMIT: x_flags %u x_len %u sf %u #p %d iv %d\n",
296 pdu->u.cmd_submit.transfer_flags,
297 pdu->u.cmd_submit.transfer_buffer_length,
298 pdu->u.cmd_submit.start_frame,
299 pdu->u.cmd_submit.number_of_packets,
300 pdu->u.cmd_submit.interval);
301 break;
302 case USBIP_CMD_UNLINK:
303 pr_debug("USBIP_CMD_UNLINK: seq %u\n",
304 pdu->u.cmd_unlink.seqnum);
305 break;
306 case USBIP_RET_SUBMIT:
307 pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
308 pdu->u.ret_submit.status,
309 pdu->u.ret_submit.actual_length,
310 pdu->u.ret_submit.start_frame,
311 pdu->u.ret_submit.number_of_packets,
312 pdu->u.ret_submit.error_count);
313 break;
314 case USBIP_RET_UNLINK:
315 pr_debug("USBIP_RET_UNLINK: status %d\n",
316 pdu->u.ret_unlink.status);
317 break;
318 default:
319 /* NOT REACHED */
320 pr_err("unknown command\n");
321 break;
322 }
323}
324EXPORT_SYMBOL_GPL(usbip_dump_header);
325
326/* Receive data over TCP/IP. */
327int usbip_recv(struct socket *sock, void *buf, int size)
328{
329 int result;
330 struct msghdr msg;
331 struct kvec iov;
332 int total = 0;
333
334 /* for blocks of if (usbip_dbg_flag_xmit) */
335 char *bp = buf;
336 int osize = size;
337
338 usbip_dbg_xmit("enter\n");
339
340 if (!sock || !buf || !size) {
341 pr_err("invalid arg, sock %p buff %p size %d\n", sock, buf,
342 size);
343 return -EINVAL;
344 }
345
346 do {
347 sock->sk->sk_allocation = GFP_NOIO;
348 iov.iov_base = buf;
349 iov.iov_len = size;
350 msg.msg_name = NULL;
351 msg.msg_namelen = 0;
352 msg.msg_control = NULL;
353 msg.msg_controllen = 0;
354 msg.msg_flags = MSG_NOSIGNAL;
355
356 result = kernel_recvmsg(sock, &msg, &iov, 1, size, MSG_WAITALL);
357 if (result <= 0) {
358 pr_debug("receive sock %p buf %p size %u ret %d total %d\n",
359 sock, buf, size, result, total);
360 goto err;
361 }
362
363 size -= result;
364 buf += result;
365 total += result;
366 } while (size > 0);
367
368 if (usbip_dbg_flag_xmit) {
369 if (!in_interrupt())
370 pr_debug("%-10s:", current->comm);
371 else
372 pr_debug("interrupt :");
373
374 pr_debug("receiving....\n");
375 usbip_dump_buffer(bp, osize);
376 pr_debug("received, osize %d ret %d size %d total %d\n",
377 osize, result, size, total);
378 }
379
380 return total;
381
382err:
383 return result;
384}
385EXPORT_SYMBOL_GPL(usbip_recv);
386
387/* there may be more cases to tweak the flags. */
388static unsigned int tweak_transfer_flags(unsigned int flags)
389{
390 flags &= ~URB_NO_TRANSFER_DMA_MAP;
391 return flags;
392}
393
394static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
395 int pack)
396{
397 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
398
399 /*
400 * Some members are not still implemented in usbip. I hope this issue
401 * will be discussed when usbip is ported to other operating systems.
402 */
403 if (pack) {
404 spdu->transfer_flags =
405 tweak_transfer_flags(urb->transfer_flags);
406 spdu->transfer_buffer_length = urb->transfer_buffer_length;
407 spdu->start_frame = urb->start_frame;
408 spdu->number_of_packets = urb->number_of_packets;
409 spdu->interval = urb->interval;
410 } else {
411 urb->transfer_flags = spdu->transfer_flags;
412 urb->transfer_buffer_length = spdu->transfer_buffer_length;
413 urb->start_frame = spdu->start_frame;
414 urb->number_of_packets = spdu->number_of_packets;
415 urb->interval = spdu->interval;
416 }
417}
418
419static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
420 int pack)
421{
422 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
423
424 if (pack) {
425 rpdu->status = urb->status;
426 rpdu->actual_length = urb->actual_length;
427 rpdu->start_frame = urb->start_frame;
428 rpdu->number_of_packets = urb->number_of_packets;
429 rpdu->error_count = urb->error_count;
430 } else {
431 urb->status = rpdu->status;
432 urb->actual_length = rpdu->actual_length;
433 urb->start_frame = rpdu->start_frame;
434 urb->number_of_packets = rpdu->number_of_packets;
435 urb->error_count = rpdu->error_count;
436 }
437}
438
439void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
440 int pack)
441{
442 switch (cmd) {
443 case USBIP_CMD_SUBMIT:
444 usbip_pack_cmd_submit(pdu, urb, pack);
445 break;
446 case USBIP_RET_SUBMIT:
447 usbip_pack_ret_submit(pdu, urb, pack);
448 break;
449 default:
450 /* NOT REACHED */
451 pr_err("unknown command\n");
452 break;
453 }
454}
455EXPORT_SYMBOL_GPL(usbip_pack_pdu);
456
457static void correct_endian_basic(struct usbip_header_basic *base, int send)
458{
459 if (send) {
460 base->command = cpu_to_be32(base->command);
461 base->seqnum = cpu_to_be32(base->seqnum);
462 base->devid = cpu_to_be32(base->devid);
463 base->direction = cpu_to_be32(base->direction);
464 base->ep = cpu_to_be32(base->ep);
465 } else {
466 base->command = be32_to_cpu(base->command);
467 base->seqnum = be32_to_cpu(base->seqnum);
468 base->devid = be32_to_cpu(base->devid);
469 base->direction = be32_to_cpu(base->direction);
470 base->ep = be32_to_cpu(base->ep);
471 }
472}
473
474static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
475 int send)
476{
477 if (send) {
478 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
479
480 cpu_to_be32s(&pdu->transfer_buffer_length);
481 cpu_to_be32s(&pdu->start_frame);
482 cpu_to_be32s(&pdu->number_of_packets);
483 cpu_to_be32s(&pdu->interval);
484 } else {
485 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
486
487 be32_to_cpus(&pdu->transfer_buffer_length);
488 be32_to_cpus(&pdu->start_frame);
489 be32_to_cpus(&pdu->number_of_packets);
490 be32_to_cpus(&pdu->interval);
491 }
492}
493
494static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
495 int send)
496{
497 if (send) {
498 cpu_to_be32s(&pdu->status);
499 cpu_to_be32s(&pdu->actual_length);
500 cpu_to_be32s(&pdu->start_frame);
501 cpu_to_be32s(&pdu->number_of_packets);
502 cpu_to_be32s(&pdu->error_count);
503 } else {
504 be32_to_cpus(&pdu->status);
505 be32_to_cpus(&pdu->actual_length);
506 be32_to_cpus(&pdu->start_frame);
507 be32_to_cpus(&pdu->number_of_packets);
508 be32_to_cpus(&pdu->error_count);
509 }
510}
511
512static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
513 int send)
514{
515 if (send)
516 pdu->seqnum = cpu_to_be32(pdu->seqnum);
517 else
518 pdu->seqnum = be32_to_cpu(pdu->seqnum);
519}
520
521static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
522 int send)
523{
524 if (send)
525 cpu_to_be32s(&pdu->status);
526 else
527 be32_to_cpus(&pdu->status);
528}
529
530void usbip_header_correct_endian(struct usbip_header *pdu, int send)
531{
532 __u32 cmd = 0;
533
534 if (send)
535 cmd = pdu->base.command;
536
537 correct_endian_basic(&pdu->base, send);
538
539 if (!send)
540 cmd = pdu->base.command;
541
542 switch (cmd) {
543 case USBIP_CMD_SUBMIT:
544 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
545 break;
546 case USBIP_RET_SUBMIT:
547 correct_endian_ret_submit(&pdu->u.ret_submit, send);
548 break;
549 case USBIP_CMD_UNLINK:
550 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
551 break;
552 case USBIP_RET_UNLINK:
553 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
554 break;
555 default:
556 /* NOT REACHED */
557 pr_err("unknown command\n");
558 break;
559 }
560}
561EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
562
563static void usbip_iso_packet_correct_endian(
564 struct usbip_iso_packet_descriptor *iso, int send)
565{
566 /* does not need all members. but copy all simply. */
567 if (send) {
568 iso->offset = cpu_to_be32(iso->offset);
569 iso->length = cpu_to_be32(iso->length);
570 iso->status = cpu_to_be32(iso->status);
571 iso->actual_length = cpu_to_be32(iso->actual_length);
572 } else {
573 iso->offset = be32_to_cpu(iso->offset);
574 iso->length = be32_to_cpu(iso->length);
575 iso->status = be32_to_cpu(iso->status);
576 iso->actual_length = be32_to_cpu(iso->actual_length);
577 }
578}
579
580static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
581 struct usb_iso_packet_descriptor *uiso, int pack)
582{
583 if (pack) {
584 iso->offset = uiso->offset;
585 iso->length = uiso->length;
586 iso->status = uiso->status;
587 iso->actual_length = uiso->actual_length;
588 } else {
589 uiso->offset = iso->offset;
590 uiso->length = iso->length;
591 uiso->status = iso->status;
592 uiso->actual_length = iso->actual_length;
593 }
594}
595
596/* must free buffer */
597struct usbip_iso_packet_descriptor*
598usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
599{
600 struct usbip_iso_packet_descriptor *iso;
601 int np = urb->number_of_packets;
602 ssize_t size = np * sizeof(*iso);
603 int i;
604
605 iso = kzalloc(size, GFP_KERNEL);
606 if (!iso)
607 return NULL;
608
609 for (i = 0; i < np; i++) {
610 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
611 usbip_iso_packet_correct_endian(&iso[i], 1);
612 }
613
614 *bufflen = size;
615
616 return iso;
617}
618EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
619
620/* some members of urb must be substituted before. */
621int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
622{
623 void *buff;
624 struct usbip_iso_packet_descriptor *iso;
625 int np = urb->number_of_packets;
626 int size = np * sizeof(*iso);
627 int i;
628 int ret;
629 int total_length = 0;
630
631 if (!usb_pipeisoc(urb->pipe))
632 return 0;
633
634 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
635 if (np == 0)
636 return 0;
637
638 buff = kzalloc(size, GFP_KERNEL);
639 if (!buff)
640 return -ENOMEM;
641
642 ret = usbip_recv(ud->tcp_socket, buff, size);
643 if (ret != size) {
644 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
645 ret);
646 kfree(buff);
647
648 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
649 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
650 else
651 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
652
653 return -EPIPE;
654 }
655
656 iso = (struct usbip_iso_packet_descriptor *) buff;
657 for (i = 0; i < np; i++) {
658 usbip_iso_packet_correct_endian(&iso[i], 0);
659 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
660 total_length += urb->iso_frame_desc[i].actual_length;
661 }
662
663 kfree(buff);
664
665 if (total_length != urb->actual_length) {
666 dev_err(&urb->dev->dev,
667 "total length of iso packets %d not equal to actual length of buffer %d\n",
668 total_length, urb->actual_length);
669
670 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
671 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
672 else
673 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
674
675 return -EPIPE;
676 }
677
678 return ret;
679}
680EXPORT_SYMBOL_GPL(usbip_recv_iso);
681
682/*
683 * This functions restores the padding which was removed for optimizing
684 * the bandwidth during transfer over tcp/ip
685 *
686 * buffer and iso packets need to be stored and be in propeper endian in urb
687 * before calling this function
688 */
689void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
690{
691 int np = urb->number_of_packets;
692 int i;
693 int actualoffset = urb->actual_length;
694
695 if (!usb_pipeisoc(urb->pipe))
696 return;
697
698 /* if no packets or length of data is 0, then nothing to unpack */
699 if (np == 0 || urb->actual_length == 0)
700 return;
701
702 /*
703 * if actual_length is transfer_buffer_length then no padding is
704 * present.
705 */
706 if (urb->actual_length == urb->transfer_buffer_length)
707 return;
708
709 /*
710 * loop over all packets from last to first (to prevent overwritting
711 * memory when padding) and move them into the proper place
712 */
713 for (i = np-1; i > 0; i--) {
714 actualoffset -= urb->iso_frame_desc[i].actual_length;
715 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
716 urb->transfer_buffer + actualoffset,
717 urb->iso_frame_desc[i].actual_length);
718 }
719}
720EXPORT_SYMBOL_GPL(usbip_pad_iso);
721
722/* some members of urb must be substituted before. */
723int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
724{
725 int ret;
726 int size;
727
728 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
729 /* the direction of urb must be OUT. */
730 if (usb_pipein(urb->pipe))
731 return 0;
732
733 size = urb->transfer_buffer_length;
734 } else {
735 /* the direction of urb must be IN. */
736 if (usb_pipeout(urb->pipe))
737 return 0;
738
739 size = urb->actual_length;
740 }
741
742 /* no need to recv xbuff */
743 if (!(size > 0))
744 return 0;
745
746 if (size > urb->transfer_buffer_length) {
747 /* should not happen, probably malicious packet */
748 if (ud->side == USBIP_STUB) {
749 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
750 return 0;
751 } else {
752 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
753 return -EPIPE;
754 }
755 }
756
757 ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
758 if (ret != size) {
759 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
760 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
761 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
762 } else {
763 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
764 return -EPIPE;
765 }
766 }
767
768 return ret;
769}
770EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
771
772static int __init usbip_core_init(void)
773{
774 int ret;
775
776 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
777 ret = usbip_init_eh();
778 if (ret)
779 return ret;
780
781 return 0;
782}
783
784static void __exit usbip_core_exit(void)
785{
786 usbip_finish_eh();
787 return;
788}
789
790module_init(usbip_core_init);
791module_exit(usbip_core_exit);
792
793MODULE_AUTHOR(DRIVER_AUTHOR);
794MODULE_DESCRIPTION(DRIVER_DESC);
795MODULE_LICENSE("GPL");
796MODULE_VERSION(USBIP_VERSION);