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 hub ttport %d\n", 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 %s, bus %s\n", dev_name(&udev->parent->dev),
128 udev->bus->bus_name);
130 dev_dbg(dev, "have_langid %d, string_langid %d\n",
131 udev->have_langid, udev->string_langid);
133 dev_dbg(dev, "maxchild %d\n", udev->maxchild);
136 static void usbip_dump_request_type(__u8 rt)
138 switch (rt & USB_RECIP_MASK) {
139 case USB_RECIP_DEVICE:
142 case USB_RECIP_INTERFACE:
145 case USB_RECIP_ENDPOINT:
148 case USB_RECIP_OTHER:
157 static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
160 pr_debug(" : null pointer\n");
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);
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");
176 case USB_REQ_CLEAR_FEATURE:
177 pr_debug("CLEAR_FEAT\n");
179 case USB_REQ_SET_FEATURE:
180 pr_debug("SET_FEAT\n");
182 case USB_REQ_SET_ADDRESS:
183 pr_debug("SET_ADDRRS\n");
185 case USB_REQ_GET_DESCRIPTOR:
186 pr_debug("GET_DESCRI\n");
188 case USB_REQ_SET_DESCRIPTOR:
189 pr_debug("SET_DESCRI\n");
191 case USB_REQ_GET_CONFIGURATION:
192 pr_debug("GET_CONFIG\n");
194 case USB_REQ_SET_CONFIGURATION:
195 pr_debug("SET_CONFIG\n");
197 case USB_REQ_GET_INTERFACE:
198 pr_debug("GET_INTERF\n");
200 case USB_REQ_SET_INTERFACE:
201 pr_debug("SET_INTERF\n");
203 case USB_REQ_SYNCH_FRAME:
204 pr_debug("SYNC_FRAME\n");
207 pr_debug("REQ(%02X)\n", cmd->bRequest);
210 usbip_dump_request_type(cmd->bRequestType);
211 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
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");
220 void usbip_dump_urb(struct urb *urb)
225 pr_debug("urb: null pointer!!\n");
230 pr_debug("urb->dev: null pointer!!\n");
234 dev = &urb->dev->dev;
236 usbip_dump_usb_device(urb->dev);
238 dev_dbg(dev, " pipe :%08x ", urb->pipe);
240 usbip_dump_pipe(urb->pipe);
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);
248 if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
249 usbip_dump_usb_ctrlrequest(
250 (struct usb_ctrlrequest *)urb->setup_packet);
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);
257 EXPORT_SYMBOL_GPL(usbip_dump_urb);
259 void usbip_dump_header(struct usbip_header *pdu)
261 pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
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);
277 case USBIP_CMD_UNLINK:
278 pr_debug("USBIP_CMD_UNLINK: seq %u\n",
279 pdu->u.cmd_unlink.seqnum);
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);
289 case USBIP_RET_UNLINK:
290 pr_debug("USBIP_RET_UNLINK: status %d\n",
291 pdu->u.ret_unlink.status);
295 pr_err("unknown command\n");
299 EXPORT_SYMBOL_GPL(usbip_dump_header);
301 /* Receive data over TCP/IP. */
302 int usbip_recv(struct socket *sock, void *buf, int size)
305 struct kvec iov = {.iov_base = buf, .iov_len = size};
306 struct msghdr msg = {.msg_flags = MSG_NOSIGNAL};
309 if (!sock || !buf || !size)
312 iov_iter_kvec(&msg.msg_iter, READ|ITER_KVEC, &iov, 1, size);
314 usbip_dbg_xmit("enter\n");
317 sock->sk->sk_allocation = GFP_NOIO;
319 result = sock_recvmsg(sock, &msg, MSG_WAITALL);
324 } while (msg_data_left(&msg));
326 if (usbip_dbg_flag_xmit) {
328 pr_debug("%-10s:", current->comm);
330 pr_debug("interrupt :");
332 pr_debug("receiving....\n");
333 usbip_dump_buffer(buf, size);
334 pr_debug("received, osize %d ret %d size %zd total %d\n",
335 size, result, msg_data_left(&msg), total);
343 EXPORT_SYMBOL_GPL(usbip_recv);
345 /* there may be more cases to tweak the flags. */
346 static unsigned int tweak_transfer_flags(unsigned int flags)
348 flags &= ~URB_NO_TRANSFER_DMA_MAP;
352 static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
355 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
358 * Some members are not still implemented in usbip. I hope this issue
359 * will be discussed when usbip is ported to other operating systems.
362 spdu->transfer_flags =
363 tweak_transfer_flags(urb->transfer_flags);
364 spdu->transfer_buffer_length = urb->transfer_buffer_length;
365 spdu->start_frame = urb->start_frame;
366 spdu->number_of_packets = urb->number_of_packets;
367 spdu->interval = urb->interval;
369 urb->transfer_flags = spdu->transfer_flags;
370 urb->transfer_buffer_length = spdu->transfer_buffer_length;
371 urb->start_frame = spdu->start_frame;
372 urb->number_of_packets = spdu->number_of_packets;
373 urb->interval = spdu->interval;
377 static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
380 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
383 rpdu->status = urb->status;
384 rpdu->actual_length = urb->actual_length;
385 rpdu->start_frame = urb->start_frame;
386 rpdu->number_of_packets = urb->number_of_packets;
387 rpdu->error_count = urb->error_count;
389 urb->status = rpdu->status;
390 urb->actual_length = rpdu->actual_length;
391 urb->start_frame = rpdu->start_frame;
392 urb->number_of_packets = rpdu->number_of_packets;
393 urb->error_count = rpdu->error_count;
397 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
401 case USBIP_CMD_SUBMIT:
402 usbip_pack_cmd_submit(pdu, urb, pack);
404 case USBIP_RET_SUBMIT:
405 usbip_pack_ret_submit(pdu, urb, pack);
409 pr_err("unknown command\n");
413 EXPORT_SYMBOL_GPL(usbip_pack_pdu);
415 static void correct_endian_basic(struct usbip_header_basic *base, int send)
418 base->command = cpu_to_be32(base->command);
419 base->seqnum = cpu_to_be32(base->seqnum);
420 base->devid = cpu_to_be32(base->devid);
421 base->direction = cpu_to_be32(base->direction);
422 base->ep = cpu_to_be32(base->ep);
424 base->command = be32_to_cpu(base->command);
425 base->seqnum = be32_to_cpu(base->seqnum);
426 base->devid = be32_to_cpu(base->devid);
427 base->direction = be32_to_cpu(base->direction);
428 base->ep = be32_to_cpu(base->ep);
432 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
436 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
438 cpu_to_be32s(&pdu->transfer_buffer_length);
439 cpu_to_be32s(&pdu->start_frame);
440 cpu_to_be32s(&pdu->number_of_packets);
441 cpu_to_be32s(&pdu->interval);
443 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
445 be32_to_cpus(&pdu->transfer_buffer_length);
446 be32_to_cpus(&pdu->start_frame);
447 be32_to_cpus(&pdu->number_of_packets);
448 be32_to_cpus(&pdu->interval);
452 static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
456 cpu_to_be32s(&pdu->status);
457 cpu_to_be32s(&pdu->actual_length);
458 cpu_to_be32s(&pdu->start_frame);
459 cpu_to_be32s(&pdu->number_of_packets);
460 cpu_to_be32s(&pdu->error_count);
462 be32_to_cpus(&pdu->status);
463 be32_to_cpus(&pdu->actual_length);
464 be32_to_cpus(&pdu->start_frame);
465 be32_to_cpus(&pdu->number_of_packets);
466 be32_to_cpus(&pdu->error_count);
470 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
474 pdu->seqnum = cpu_to_be32(pdu->seqnum);
476 pdu->seqnum = be32_to_cpu(pdu->seqnum);
479 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
483 cpu_to_be32s(&pdu->status);
485 be32_to_cpus(&pdu->status);
488 void usbip_header_correct_endian(struct usbip_header *pdu, int send)
493 cmd = pdu->base.command;
495 correct_endian_basic(&pdu->base, send);
498 cmd = pdu->base.command;
501 case USBIP_CMD_SUBMIT:
502 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
504 case USBIP_RET_SUBMIT:
505 correct_endian_ret_submit(&pdu->u.ret_submit, send);
507 case USBIP_CMD_UNLINK:
508 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
510 case USBIP_RET_UNLINK:
511 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
515 pr_err("unknown command\n");
519 EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
521 static void usbip_iso_packet_correct_endian(
522 struct usbip_iso_packet_descriptor *iso, int send)
524 /* does not need all members. but copy all simply. */
526 iso->offset = cpu_to_be32(iso->offset);
527 iso->length = cpu_to_be32(iso->length);
528 iso->status = cpu_to_be32(iso->status);
529 iso->actual_length = cpu_to_be32(iso->actual_length);
531 iso->offset = be32_to_cpu(iso->offset);
532 iso->length = be32_to_cpu(iso->length);
533 iso->status = be32_to_cpu(iso->status);
534 iso->actual_length = be32_to_cpu(iso->actual_length);
538 static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
539 struct usb_iso_packet_descriptor *uiso, int pack)
542 iso->offset = uiso->offset;
543 iso->length = uiso->length;
544 iso->status = uiso->status;
545 iso->actual_length = uiso->actual_length;
547 uiso->offset = iso->offset;
548 uiso->length = iso->length;
549 uiso->status = iso->status;
550 uiso->actual_length = iso->actual_length;
554 /* must free buffer */
555 struct usbip_iso_packet_descriptor*
556 usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
558 struct usbip_iso_packet_descriptor *iso;
559 int np = urb->number_of_packets;
560 ssize_t size = np * sizeof(*iso);
563 iso = kzalloc(size, GFP_KERNEL);
567 for (i = 0; i < np; i++) {
568 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
569 usbip_iso_packet_correct_endian(&iso[i], 1);
576 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
578 /* some members of urb must be substituted before. */
579 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
582 struct usbip_iso_packet_descriptor *iso;
583 int np = urb->number_of_packets;
584 int size = np * sizeof(*iso);
587 int total_length = 0;
589 if (!usb_pipeisoc(urb->pipe))
592 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
596 buff = kzalloc(size, GFP_KERNEL);
600 ret = usbip_recv(ud->tcp_socket, buff, size);
602 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
606 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
607 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
609 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
614 iso = (struct usbip_iso_packet_descriptor *) buff;
615 for (i = 0; i < np; i++) {
616 usbip_iso_packet_correct_endian(&iso[i], 0);
617 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
618 total_length += urb->iso_frame_desc[i].actual_length;
623 if (total_length != urb->actual_length) {
624 dev_err(&urb->dev->dev,
625 "total length of iso packets %d not equal to actual length of buffer %d\n",
626 total_length, urb->actual_length);
628 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
629 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
631 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
638 EXPORT_SYMBOL_GPL(usbip_recv_iso);
641 * This functions restores the padding which was removed for optimizing
642 * the bandwidth during transfer over tcp/ip
644 * buffer and iso packets need to be stored and be in propeper endian in urb
645 * before calling this function
647 void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
649 int np = urb->number_of_packets;
651 int actualoffset = urb->actual_length;
653 if (!usb_pipeisoc(urb->pipe))
656 /* if no packets or length of data is 0, then nothing to unpack */
657 if (np == 0 || urb->actual_length == 0)
661 * if actual_length is transfer_buffer_length then no padding is
664 if (urb->actual_length == urb->transfer_buffer_length)
668 * loop over all packets from last to first (to prevent overwriting
669 * memory when padding) and move them into the proper place
671 for (i = np-1; i > 0; i--) {
672 actualoffset -= urb->iso_frame_desc[i].actual_length;
673 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
674 urb->transfer_buffer + actualoffset,
675 urb->iso_frame_desc[i].actual_length);
678 EXPORT_SYMBOL_GPL(usbip_pad_iso);
680 /* some members of urb must be substituted before. */
681 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
683 struct scatterlist *sg;
690 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
691 /* the direction of urb must be OUT. */
692 if (usb_pipein(urb->pipe))
695 size = urb->transfer_buffer_length;
697 /* the direction of urb must be IN. */
698 if (usb_pipeout(urb->pipe))
701 size = urb->actual_length;
704 /* no need to recv xbuff */
708 if (size > urb->transfer_buffer_length)
709 /* should not happen, probably malicious packet */
714 for_each_sg(urb->sg, sg, urb->num_sgs, i) {
717 if (copy < sg->length)
720 recv_size = sg->length;
722 recv = usbip_recv(ud->tcp_socket, sg_virt(sg),
725 if (recv != recv_size)
738 ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
746 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
747 if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
748 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
750 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
754 EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
756 static int __init usbip_core_init(void)
760 ret = usbip_init_eh();
767 static void __exit usbip_core_exit(void)
773 module_init(usbip_core_init);
774 module_exit(usbip_core_exit);
776 MODULE_AUTHOR(DRIVER_AUTHOR);
777 MODULE_DESCRIPTION(DRIVER_DESC);
778 MODULE_LICENSE("GPL");