1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
4 * Copyright (C) 2015-2016 Samsung Electronics
5 * Krzysztof Opasiak <k.opasiak@samsung.com>
8 #include <asm/byteorder.h>
9 #include <linux/file.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>
18 #include "usbip_common.h"
20 #define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>"
21 #define DRIVER_DESC "USB/IP Core"
23 #ifdef CONFIG_USBIP_DEBUG
24 unsigned long usbip_debug_flag = 0xffffffff;
26 unsigned long usbip_debug_flag;
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)");
33 struct device_attribute dev_attr_usbip_debug;
34 EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
36 static ssize_t usbip_debug_show(struct device *dev,
37 struct device_attribute *attr, char *buf)
39 return sprintf(buf, "%lx\n", usbip_debug_flag);
42 static ssize_t usbip_debug_store(struct device *dev,
43 struct device_attribute *attr, const char *buf,
46 if (sscanf(buf, "%lx", &usbip_debug_flag) != 1)
50 DEVICE_ATTR_RW(usbip_debug);
52 static void usbip_dump_buffer(char *buff, int bufflen)
54 print_hex_dump(KERN_DEBUG, "usbip-core", DUMP_PREFIX_OFFSET, 16, 4,
55 buff, bufflen, false);
58 static void usbip_dump_pipe(unsigned int p)
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);
65 pr_debug("dev(%d) ep(%d) [%s] ", dev, ep, dir ? "IN" : "OUT");
68 case PIPE_ISOCHRONOUS:
86 static void usbip_dump_usb_device(struct usb_device *udev)
88 struct device *dev = &udev->dev;
91 dev_dbg(dev, " devnum(%d) devpath(%s) usb speed(%s)",
92 udev->devnum, udev->devpath, usb_speed_string(udev->speed));
94 pr_debug("tt %p, ttport %d\n", udev->tt, udev->ttport);
97 for (i = 0; i < 16; i++)
101 dev_dbg(dev, " toggle0(IN) :");
102 for (i = 0; i < 16; i++)
103 pr_debug(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
106 dev_dbg(dev, " toggle1(OUT):");
107 for (i = 0; i < 16; i++)
108 pr_debug(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
111 dev_dbg(dev, " epmaxp_in :");
112 for (i = 0; i < 16; i++) {
115 le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
119 dev_dbg(dev, " epmaxp_out :");
120 for (i = 0; i < 16; i++) {
123 le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
127 dev_dbg(dev, "parent %p, bus %p\n", udev->parent, udev->bus);
130 "descriptor %p, config %p, actconfig %p, rawdescriptors %p\n",
131 &udev->descriptor, udev->config,
132 udev->actconfig, udev->rawdescriptors);
134 dev_dbg(dev, "have_langid %d, string_langid %d\n",
135 udev->have_langid, udev->string_langid);
137 dev_dbg(dev, "maxchild %d\n", udev->maxchild);
140 static void usbip_dump_request_type(__u8 rt)
142 switch (rt & USB_RECIP_MASK) {
143 case USB_RECIP_DEVICE:
146 case USB_RECIP_INTERFACE:
149 case USB_RECIP_ENDPOINT:
152 case USB_RECIP_OTHER:
161 static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
164 pr_debug(" : null pointer\n");
169 pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) wLength(%04X) ",
170 cmd->bRequestType, cmd->bRequest,
171 cmd->wValue, cmd->wIndex, cmd->wLength);
174 if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
175 pr_debug("STANDARD ");
176 switch (cmd->bRequest) {
177 case USB_REQ_GET_STATUS:
178 pr_debug("GET_STATUS\n");
180 case USB_REQ_CLEAR_FEATURE:
181 pr_debug("CLEAR_FEAT\n");
183 case USB_REQ_SET_FEATURE:
184 pr_debug("SET_FEAT\n");
186 case USB_REQ_SET_ADDRESS:
187 pr_debug("SET_ADDRRS\n");
189 case USB_REQ_GET_DESCRIPTOR:
190 pr_debug("GET_DESCRI\n");
192 case USB_REQ_SET_DESCRIPTOR:
193 pr_debug("SET_DESCRI\n");
195 case USB_REQ_GET_CONFIGURATION:
196 pr_debug("GET_CONFIG\n");
198 case USB_REQ_SET_CONFIGURATION:
199 pr_debug("SET_CONFIG\n");
201 case USB_REQ_GET_INTERFACE:
202 pr_debug("GET_INTERF\n");
204 case USB_REQ_SET_INTERFACE:
205 pr_debug("SET_INTERF\n");
207 case USB_REQ_SYNCH_FRAME:
208 pr_debug("SYNC_FRAME\n");
211 pr_debug("REQ(%02X)\n", cmd->bRequest);
214 usbip_dump_request_type(cmd->bRequestType);
215 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
217 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
218 pr_debug("VENDOR\n");
219 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
220 pr_debug("RESERVED\n");
224 void usbip_dump_urb(struct urb *urb)
229 pr_debug("urb: null pointer!!\n");
234 pr_debug("urb->dev: null pointer!!\n");
238 dev = &urb->dev->dev;
240 dev_dbg(dev, " urb :%p\n", urb);
241 dev_dbg(dev, " dev :%p\n", urb->dev);
243 usbip_dump_usb_device(urb->dev);
245 dev_dbg(dev, " pipe :%08x ", urb->pipe);
247 usbip_dump_pipe(urb->pipe);
249 dev_dbg(dev, " status :%d\n", urb->status);
250 dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
251 dev_dbg(dev, " transfer_buffer :%p\n", urb->transfer_buffer);
252 dev_dbg(dev, " transfer_buffer_length:%d\n",
253 urb->transfer_buffer_length);
254 dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
255 dev_dbg(dev, " setup_packet :%p\n", urb->setup_packet);
257 if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
258 usbip_dump_usb_ctrlrequest(
259 (struct usb_ctrlrequest *)urb->setup_packet);
261 dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
262 dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
263 dev_dbg(dev, " interval :%d\n", urb->interval);
264 dev_dbg(dev, " error_count :%d\n", urb->error_count);
265 dev_dbg(dev, " context :%p\n", urb->context);
266 dev_dbg(dev, " complete :%p\n", urb->complete);
268 EXPORT_SYMBOL_GPL(usbip_dump_urb);
270 void usbip_dump_header(struct usbip_header *pdu)
272 pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
279 switch (pdu->base.command) {
280 case USBIP_CMD_SUBMIT:
281 pr_debug("USBIP_CMD_SUBMIT: x_flags %u x_len %u sf %u #p %d iv %d\n",
282 pdu->u.cmd_submit.transfer_flags,
283 pdu->u.cmd_submit.transfer_buffer_length,
284 pdu->u.cmd_submit.start_frame,
285 pdu->u.cmd_submit.number_of_packets,
286 pdu->u.cmd_submit.interval);
288 case USBIP_CMD_UNLINK:
289 pr_debug("USBIP_CMD_UNLINK: seq %u\n",
290 pdu->u.cmd_unlink.seqnum);
292 case USBIP_RET_SUBMIT:
293 pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
294 pdu->u.ret_submit.status,
295 pdu->u.ret_submit.actual_length,
296 pdu->u.ret_submit.start_frame,
297 pdu->u.ret_submit.number_of_packets,
298 pdu->u.ret_submit.error_count);
300 case USBIP_RET_UNLINK:
301 pr_debug("USBIP_RET_UNLINK: status %d\n",
302 pdu->u.ret_unlink.status);
306 pr_err("unknown command\n");
310 EXPORT_SYMBOL_GPL(usbip_dump_header);
312 /* Receive data over TCP/IP. */
313 int usbip_recv(struct socket *sock, void *buf, int size)
316 struct kvec iov = {.iov_base = buf, .iov_len = size};
317 struct msghdr msg = {.msg_flags = MSG_NOSIGNAL};
320 iov_iter_kvec(&msg.msg_iter, READ|ITER_KVEC, &iov, 1, size);
322 usbip_dbg_xmit("enter\n");
324 if (!sock || !buf || !size) {
325 pr_err("invalid arg, sock %p buff %p size %d\n", sock, buf,
331 int sz = msg_data_left(&msg);
332 sock->sk->sk_allocation = GFP_NOIO;
334 result = sock_recvmsg(sock, &msg, MSG_WAITALL);
336 pr_debug("receive sock %p buf %p size %u ret %d total %d\n",
337 sock, buf + total, sz, result, total);
342 } while (msg_data_left(&msg));
344 if (usbip_dbg_flag_xmit) {
346 pr_debug("%-10s:", current->comm);
348 pr_debug("interrupt :");
350 pr_debug("receiving....\n");
351 usbip_dump_buffer(buf, size);
352 pr_debug("received, osize %d ret %d size %zd total %d\n",
353 size, result, msg_data_left(&msg), total);
361 EXPORT_SYMBOL_GPL(usbip_recv);
363 /* there may be more cases to tweak the flags. */
364 static unsigned int tweak_transfer_flags(unsigned int flags)
366 flags &= ~URB_NO_TRANSFER_DMA_MAP;
370 static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
373 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
376 * Some members are not still implemented in usbip. I hope this issue
377 * will be discussed when usbip is ported to other operating systems.
380 spdu->transfer_flags =
381 tweak_transfer_flags(urb->transfer_flags);
382 spdu->transfer_buffer_length = urb->transfer_buffer_length;
383 spdu->start_frame = urb->start_frame;
384 spdu->number_of_packets = urb->number_of_packets;
385 spdu->interval = urb->interval;
387 urb->transfer_flags = spdu->transfer_flags;
388 urb->transfer_buffer_length = spdu->transfer_buffer_length;
389 urb->start_frame = spdu->start_frame;
390 urb->number_of_packets = spdu->number_of_packets;
391 urb->interval = spdu->interval;
395 static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
398 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
401 rpdu->status = urb->status;
402 rpdu->actual_length = urb->actual_length;
403 rpdu->start_frame = urb->start_frame;
404 rpdu->number_of_packets = urb->number_of_packets;
405 rpdu->error_count = urb->error_count;
407 urb->status = rpdu->status;
408 urb->actual_length = rpdu->actual_length;
409 urb->start_frame = rpdu->start_frame;
410 urb->number_of_packets = rpdu->number_of_packets;
411 urb->error_count = rpdu->error_count;
415 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
419 case USBIP_CMD_SUBMIT:
420 usbip_pack_cmd_submit(pdu, urb, pack);
422 case USBIP_RET_SUBMIT:
423 usbip_pack_ret_submit(pdu, urb, pack);
427 pr_err("unknown command\n");
431 EXPORT_SYMBOL_GPL(usbip_pack_pdu);
433 static void correct_endian_basic(struct usbip_header_basic *base, int send)
436 base->command = cpu_to_be32(base->command);
437 base->seqnum = cpu_to_be32(base->seqnum);
438 base->devid = cpu_to_be32(base->devid);
439 base->direction = cpu_to_be32(base->direction);
440 base->ep = cpu_to_be32(base->ep);
442 base->command = be32_to_cpu(base->command);
443 base->seqnum = be32_to_cpu(base->seqnum);
444 base->devid = be32_to_cpu(base->devid);
445 base->direction = be32_to_cpu(base->direction);
446 base->ep = be32_to_cpu(base->ep);
450 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
454 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
456 cpu_to_be32s(&pdu->transfer_buffer_length);
457 cpu_to_be32s(&pdu->start_frame);
458 cpu_to_be32s(&pdu->number_of_packets);
459 cpu_to_be32s(&pdu->interval);
461 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
463 be32_to_cpus(&pdu->transfer_buffer_length);
464 be32_to_cpus(&pdu->start_frame);
465 be32_to_cpus(&pdu->number_of_packets);
466 be32_to_cpus(&pdu->interval);
470 static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
474 cpu_to_be32s(&pdu->status);
475 cpu_to_be32s(&pdu->actual_length);
476 cpu_to_be32s(&pdu->start_frame);
477 cpu_to_be32s(&pdu->number_of_packets);
478 cpu_to_be32s(&pdu->error_count);
480 be32_to_cpus(&pdu->status);
481 be32_to_cpus(&pdu->actual_length);
482 be32_to_cpus(&pdu->start_frame);
483 be32_to_cpus(&pdu->number_of_packets);
484 be32_to_cpus(&pdu->error_count);
488 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
492 pdu->seqnum = cpu_to_be32(pdu->seqnum);
494 pdu->seqnum = be32_to_cpu(pdu->seqnum);
497 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
501 cpu_to_be32s(&pdu->status);
503 be32_to_cpus(&pdu->status);
506 void usbip_header_correct_endian(struct usbip_header *pdu, int send)
511 cmd = pdu->base.command;
513 correct_endian_basic(&pdu->base, send);
516 cmd = pdu->base.command;
519 case USBIP_CMD_SUBMIT:
520 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
522 case USBIP_RET_SUBMIT:
523 correct_endian_ret_submit(&pdu->u.ret_submit, send);
525 case USBIP_CMD_UNLINK:
526 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
528 case USBIP_RET_UNLINK:
529 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
533 pr_err("unknown command\n");
537 EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
539 static void usbip_iso_packet_correct_endian(
540 struct usbip_iso_packet_descriptor *iso, int send)
542 /* does not need all members. but copy all simply. */
544 iso->offset = cpu_to_be32(iso->offset);
545 iso->length = cpu_to_be32(iso->length);
546 iso->status = cpu_to_be32(iso->status);
547 iso->actual_length = cpu_to_be32(iso->actual_length);
549 iso->offset = be32_to_cpu(iso->offset);
550 iso->length = be32_to_cpu(iso->length);
551 iso->status = be32_to_cpu(iso->status);
552 iso->actual_length = be32_to_cpu(iso->actual_length);
556 static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
557 struct usb_iso_packet_descriptor *uiso, int pack)
560 iso->offset = uiso->offset;
561 iso->length = uiso->length;
562 iso->status = uiso->status;
563 iso->actual_length = uiso->actual_length;
565 uiso->offset = iso->offset;
566 uiso->length = iso->length;
567 uiso->status = iso->status;
568 uiso->actual_length = iso->actual_length;
572 /* must free buffer */
573 struct usbip_iso_packet_descriptor*
574 usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
576 struct usbip_iso_packet_descriptor *iso;
577 int np = urb->number_of_packets;
578 ssize_t size = np * sizeof(*iso);
581 iso = kzalloc(size, GFP_KERNEL);
585 for (i = 0; i < np; i++) {
586 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
587 usbip_iso_packet_correct_endian(&iso[i], 1);
594 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
596 /* some members of urb must be substituted before. */
597 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
600 struct usbip_iso_packet_descriptor *iso;
601 int np = urb->number_of_packets;
602 int size = np * sizeof(*iso);
605 int total_length = 0;
607 if (!usb_pipeisoc(urb->pipe))
610 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
614 buff = kzalloc(size, GFP_KERNEL);
618 ret = usbip_recv(ud->tcp_socket, buff, size);
620 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
624 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
625 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
627 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
632 iso = (struct usbip_iso_packet_descriptor *) buff;
633 for (i = 0; i < np; i++) {
634 usbip_iso_packet_correct_endian(&iso[i], 0);
635 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
636 total_length += urb->iso_frame_desc[i].actual_length;
641 if (total_length != urb->actual_length) {
642 dev_err(&urb->dev->dev,
643 "total length of iso packets %d not equal to actual length of buffer %d\n",
644 total_length, urb->actual_length);
646 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
647 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
649 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
656 EXPORT_SYMBOL_GPL(usbip_recv_iso);
659 * This functions restores the padding which was removed for optimizing
660 * the bandwidth during transfer over tcp/ip
662 * buffer and iso packets need to be stored and be in propeper endian in urb
663 * before calling this function
665 void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
667 int np = urb->number_of_packets;
669 int actualoffset = urb->actual_length;
671 if (!usb_pipeisoc(urb->pipe))
674 /* if no packets or length of data is 0, then nothing to unpack */
675 if (np == 0 || urb->actual_length == 0)
679 * if actual_length is transfer_buffer_length then no padding is
682 if (urb->actual_length == urb->transfer_buffer_length)
686 * loop over all packets from last to first (to prevent overwriting
687 * memory when padding) and move them into the proper place
689 for (i = np-1; i > 0; i--) {
690 actualoffset -= urb->iso_frame_desc[i].actual_length;
691 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
692 urb->transfer_buffer + actualoffset,
693 urb->iso_frame_desc[i].actual_length);
696 EXPORT_SYMBOL_GPL(usbip_pad_iso);
698 /* some members of urb must be substituted before. */
699 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
704 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
705 /* the direction of urb must be OUT. */
706 if (usb_pipein(urb->pipe))
709 size = urb->transfer_buffer_length;
711 /* the direction of urb must be IN. */
712 if (usb_pipeout(urb->pipe))
715 size = urb->actual_length;
718 /* no need to recv xbuff */
722 if (size > urb->transfer_buffer_length) {
723 /* should not happen, probably malicious packet */
724 if (ud->side == USBIP_STUB) {
725 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
728 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
733 ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
735 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
736 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
737 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
739 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
746 EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
748 static int __init usbip_core_init(void)
752 ret = usbip_init_eh();
759 static void __exit usbip_core_exit(void)
765 module_init(usbip_core_init);
766 module_exit(usbip_core_exit);
768 MODULE_AUTHOR(DRIVER_AUTHOR);
769 MODULE_DESCRIPTION(DRIVER_DESC);
770 MODULE_LICENSE("GPL");