2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 #include <asm/byteorder.h>
21 #include <linux/file.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/stat.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
30 #include "usbip_common.h"
32 #define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>"
33 #define DRIVER_DESC "USB/IP Core"
35 #ifdef CONFIG_USBIP_DEBUG
36 unsigned long usbip_debug_flag = 0xffffffff;
38 unsigned long usbip_debug_flag;
40 EXPORT_SYMBOL_GPL(usbip_debug_flag);
41 module_param(usbip_debug_flag, ulong, S_IRUGO|S_IWUSR);
42 MODULE_PARM_DESC(usbip_debug_flag, "debug flags (defined in usbip_common.h)");
45 struct device_attribute dev_attr_usbip_debug;
46 EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
48 static ssize_t show_flag(struct device *dev, struct device_attribute *attr,
51 return sprintf(buf, "%lx\n", usbip_debug_flag);
54 static ssize_t store_flag(struct device *dev, struct device_attribute *attr,
55 const char *buf, size_t count)
57 sscanf(buf, "%lx", &usbip_debug_flag);
60 DEVICE_ATTR(usbip_debug, (S_IRUGO | S_IWUSR), show_flag, store_flag);
62 static void usbip_dump_buffer(char *buff, int bufflen)
64 print_hex_dump(KERN_DEBUG, "usbip-core", DUMP_PREFIX_OFFSET, 16, 4,
65 buff, bufflen, false);
68 static void usbip_dump_pipe(unsigned int p)
70 unsigned char type = usb_pipetype(p);
71 unsigned char ep = usb_pipeendpoint(p);
72 unsigned char dev = usb_pipedevice(p);
73 unsigned char dir = usb_pipein(p);
75 pr_debug("dev(%d) ep(%d) [%s] ", dev, ep, dir ? "IN" : "OUT");
78 case PIPE_ISOCHRONOUS:
96 static void usbip_dump_usb_device(struct usb_device *udev)
98 struct device *dev = &udev->dev;
101 dev_dbg(dev, " devnum(%d) devpath(%s) ",
102 udev->devnum, udev->devpath);
104 switch (udev->speed) {
106 pr_debug("SPD_HIGH ");
109 pr_debug("SPD_FULL ");
112 pr_debug("SPD_LOW ");
114 case USB_SPEED_UNKNOWN:
115 pr_debug("SPD_UNKNOWN ");
118 pr_debug("SPD_ERROR ");
122 pr_debug("tt %p, ttport %d\n", udev->tt, udev->ttport);
125 for (i = 0; i < 16; i++)
129 dev_dbg(dev, " toggle0(IN) :");
130 for (i = 0; i < 16; i++)
131 pr_debug(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
134 dev_dbg(dev, " toggle1(OUT):");
135 for (i = 0; i < 16; i++)
136 pr_debug(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
139 dev_dbg(dev, " epmaxp_in :");
140 for (i = 0; i < 16; i++) {
143 le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
147 dev_dbg(dev, " epmaxp_out :");
148 for (i = 0; i < 16; i++) {
151 le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
155 dev_dbg(dev, "parent %p, bus %p\n", udev->parent, udev->bus);
157 dev_dbg(dev, "descriptor %p, config %p, actconfig %p, "
158 "rawdescriptors %p\n", &udev->descriptor, udev->config,
159 udev->actconfig, udev->rawdescriptors);
161 dev_dbg(dev, "have_langid %d, string_langid %d\n",
162 udev->have_langid, udev->string_langid);
164 dev_dbg(dev, "maxchild %d\n", udev->maxchild);
167 static void usbip_dump_request_type(__u8 rt)
169 switch (rt & USB_RECIP_MASK) {
170 case USB_RECIP_DEVICE:
173 case USB_RECIP_INTERFACE:
176 case USB_RECIP_ENDPOINT:
179 case USB_RECIP_OTHER:
188 static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
191 pr_debug(" : null pointer\n");
196 pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) "
197 "wLength(%04X) ", cmd->bRequestType, cmd->bRequest,
198 cmd->wValue, cmd->wIndex, cmd->wLength);
201 if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
202 pr_debug("STANDARD ");
203 switch (cmd->bRequest) {
204 case USB_REQ_GET_STATUS:
205 pr_debug("GET_STATUS\n");
207 case USB_REQ_CLEAR_FEATURE:
208 pr_debug("CLEAR_FEAT\n");
210 case USB_REQ_SET_FEATURE:
211 pr_debug("SET_FEAT\n");
213 case USB_REQ_SET_ADDRESS:
214 pr_debug("SET_ADDRRS\n");
216 case USB_REQ_GET_DESCRIPTOR:
217 pr_debug("GET_DESCRI\n");
219 case USB_REQ_SET_DESCRIPTOR:
220 pr_debug("SET_DESCRI\n");
222 case USB_REQ_GET_CONFIGURATION:
223 pr_debug("GET_CONFIG\n");
225 case USB_REQ_SET_CONFIGURATION:
226 pr_debug("SET_CONFIG\n");
228 case USB_REQ_GET_INTERFACE:
229 pr_debug("GET_INTERF\n");
231 case USB_REQ_SET_INTERFACE:
232 pr_debug("SET_INTERF\n");
234 case USB_REQ_SYNCH_FRAME:
235 pr_debug("SYNC_FRAME\n");
238 pr_debug("REQ(%02X)\n", cmd->bRequest);
241 usbip_dump_request_type(cmd->bRequestType);
242 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
244 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
245 pr_debug("VENDOR\n");
246 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
247 pr_debug("RESERVED\n");
251 void usbip_dump_urb(struct urb *urb)
256 pr_debug("urb: null pointer!!\n");
261 pr_debug("urb->dev: null pointer!!\n");
265 dev = &urb->dev->dev;
267 dev_dbg(dev, " urb :%p\n", urb);
268 dev_dbg(dev, " dev :%p\n", urb->dev);
270 usbip_dump_usb_device(urb->dev);
272 dev_dbg(dev, " pipe :%08x ", urb->pipe);
274 usbip_dump_pipe(urb->pipe);
276 dev_dbg(dev, " status :%d\n", urb->status);
277 dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
278 dev_dbg(dev, " transfer_buffer :%p\n", urb->transfer_buffer);
279 dev_dbg(dev, " transfer_buffer_length:%d\n",
280 urb->transfer_buffer_length);
281 dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
282 dev_dbg(dev, " setup_packet :%p\n", urb->setup_packet);
284 if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
285 usbip_dump_usb_ctrlrequest(
286 (struct usb_ctrlrequest *)urb->setup_packet);
288 dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
289 dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
290 dev_dbg(dev, " interval :%d\n", urb->interval);
291 dev_dbg(dev, " error_count :%d\n", urb->error_count);
292 dev_dbg(dev, " context :%p\n", urb->context);
293 dev_dbg(dev, " complete :%p\n", urb->complete);
295 EXPORT_SYMBOL_GPL(usbip_dump_urb);
297 void usbip_dump_header(struct usbip_header *pdu)
299 pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
306 switch (pdu->base.command) {
307 case USBIP_CMD_SUBMIT:
308 pr_debug("USBIP_CMD_SUBMIT: "
309 "x_flags %u x_len %u sf %u #p %d iv %d\n",
310 pdu->u.cmd_submit.transfer_flags,
311 pdu->u.cmd_submit.transfer_buffer_length,
312 pdu->u.cmd_submit.start_frame,
313 pdu->u.cmd_submit.number_of_packets,
314 pdu->u.cmd_submit.interval);
316 case USBIP_CMD_UNLINK:
317 pr_debug("USBIP_CMD_UNLINK: seq %u\n",
318 pdu->u.cmd_unlink.seqnum);
320 case USBIP_RET_SUBMIT:
321 pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
322 pdu->u.ret_submit.status,
323 pdu->u.ret_submit.actual_length,
324 pdu->u.ret_submit.start_frame,
325 pdu->u.ret_submit.number_of_packets,
326 pdu->u.ret_submit.error_count);
328 case USBIP_RET_UNLINK:
329 pr_debug("USBIP_RET_UNLINK: status %d\n",
330 pdu->u.ret_unlink.status);
334 pr_err("unknown command\n");
338 EXPORT_SYMBOL_GPL(usbip_dump_header);
340 /* Receive data over TCP/IP. */
341 int usbip_recv(struct socket *sock, void *buf, int size)
348 /* for blocks of if (usbip_dbg_flag_xmit) */
352 usbip_dbg_xmit("enter\n");
354 if (!sock || !buf || !size) {
355 pr_err("invalid arg, sock %p buff %p size %d\n", sock, buf,
361 sock->sk->sk_allocation = GFP_NOIO;
366 msg.msg_control = NULL;
367 msg.msg_controllen = 0;
369 msg.msg_flags = MSG_NOSIGNAL;
371 result = kernel_recvmsg(sock, &msg, &iov, 1, size, MSG_WAITALL);
373 pr_debug("receive sock %p buf %p size %u ret %d total %d\n",
374 sock, buf, size, result, total);
383 if (usbip_dbg_flag_xmit) {
385 pr_debug("%-10s:", current->comm);
387 pr_debug("interrupt :");
389 pr_debug("receiving....\n");
390 usbip_dump_buffer(bp, osize);
391 pr_debug("received, osize %d ret %d size %d total %d\n",
392 osize, result, size, total);
400 EXPORT_SYMBOL_GPL(usbip_recv);
402 struct socket *sockfd_to_socket(unsigned int sockfd)
404 struct socket *socket;
410 pr_err("invalid sockfd\n");
414 inode = file->f_dentry->d_inode;
416 if (!inode || !S_ISSOCK(inode->i_mode))
419 socket = SOCKET_I(inode);
423 EXPORT_SYMBOL_GPL(sockfd_to_socket);
425 /* there may be more cases to tweak the flags. */
426 static unsigned int tweak_transfer_flags(unsigned int flags)
428 flags &= ~URB_NO_TRANSFER_DMA_MAP;
432 static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
435 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
438 * Some members are not still implemented in usbip. I hope this issue
439 * will be discussed when usbip is ported to other operating systems.
443 spdu->transfer_flags =
444 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;
451 urb->transfer_flags = spdu->transfer_flags;
453 urb->transfer_buffer_length = spdu->transfer_buffer_length;
454 urb->start_frame = spdu->start_frame;
455 urb->number_of_packets = spdu->number_of_packets;
456 urb->interval = spdu->interval;
460 static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
463 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
468 rpdu->status = urb->status;
469 rpdu->actual_length = urb->actual_length;
470 rpdu->start_frame = urb->start_frame;
471 rpdu->number_of_packets = urb->number_of_packets;
472 rpdu->error_count = urb->error_count;
476 urb->status = rpdu->status;
477 urb->actual_length = rpdu->actual_length;
478 urb->start_frame = rpdu->start_frame;
479 urb->number_of_packets = rpdu->number_of_packets;
480 urb->error_count = rpdu->error_count;
484 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
488 case USBIP_CMD_SUBMIT:
489 usbip_pack_cmd_submit(pdu, urb, pack);
491 case USBIP_RET_SUBMIT:
492 usbip_pack_ret_submit(pdu, urb, pack);
496 pr_err("unknown command\n");
500 EXPORT_SYMBOL_GPL(usbip_pack_pdu);
502 static void correct_endian_basic(struct usbip_header_basic *base, int send)
505 base->command = cpu_to_be32(base->command);
506 base->seqnum = cpu_to_be32(base->seqnum);
507 base->devid = cpu_to_be32(base->devid);
508 base->direction = cpu_to_be32(base->direction);
509 base->ep = cpu_to_be32(base->ep);
511 base->command = be32_to_cpu(base->command);
512 base->seqnum = be32_to_cpu(base->seqnum);
513 base->devid = be32_to_cpu(base->devid);
514 base->direction = be32_to_cpu(base->direction);
515 base->ep = be32_to_cpu(base->ep);
519 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
523 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
525 cpu_to_be32s(&pdu->transfer_buffer_length);
526 cpu_to_be32s(&pdu->start_frame);
527 cpu_to_be32s(&pdu->number_of_packets);
528 cpu_to_be32s(&pdu->interval);
530 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
532 be32_to_cpus(&pdu->transfer_buffer_length);
533 be32_to_cpus(&pdu->start_frame);
534 be32_to_cpus(&pdu->number_of_packets);
535 be32_to_cpus(&pdu->interval);
539 static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
543 cpu_to_be32s(&pdu->status);
544 cpu_to_be32s(&pdu->actual_length);
545 cpu_to_be32s(&pdu->start_frame);
546 cpu_to_be32s(&pdu->number_of_packets);
547 cpu_to_be32s(&pdu->error_count);
549 be32_to_cpus(&pdu->status);
550 be32_to_cpus(&pdu->actual_length);
551 be32_to_cpus(&pdu->start_frame);
552 be32_to_cpus(&pdu->number_of_packets);
553 be32_to_cpus(&pdu->error_count);
557 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
561 pdu->seqnum = cpu_to_be32(pdu->seqnum);
563 pdu->seqnum = be32_to_cpu(pdu->seqnum);
566 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
570 cpu_to_be32s(&pdu->status);
572 be32_to_cpus(&pdu->status);
575 void usbip_header_correct_endian(struct usbip_header *pdu, int send)
580 cmd = pdu->base.command;
582 correct_endian_basic(&pdu->base, send);
585 cmd = pdu->base.command;
588 case USBIP_CMD_SUBMIT:
589 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
591 case USBIP_RET_SUBMIT:
592 correct_endian_ret_submit(&pdu->u.ret_submit, send);
594 case USBIP_CMD_UNLINK:
595 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
597 case USBIP_RET_UNLINK:
598 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
602 pr_err("unknown command\n");
606 EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
608 static void usbip_iso_packet_correct_endian(
609 struct usbip_iso_packet_descriptor *iso, int send)
611 /* does not need all members. but copy all simply. */
613 iso->offset = cpu_to_be32(iso->offset);
614 iso->length = cpu_to_be32(iso->length);
615 iso->status = cpu_to_be32(iso->status);
616 iso->actual_length = cpu_to_be32(iso->actual_length);
618 iso->offset = be32_to_cpu(iso->offset);
619 iso->length = be32_to_cpu(iso->length);
620 iso->status = be32_to_cpu(iso->status);
621 iso->actual_length = be32_to_cpu(iso->actual_length);
625 static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
626 struct usb_iso_packet_descriptor *uiso, int pack)
629 iso->offset = uiso->offset;
630 iso->length = uiso->length;
631 iso->status = uiso->status;
632 iso->actual_length = uiso->actual_length;
634 uiso->offset = iso->offset;
635 uiso->length = iso->length;
636 uiso->status = iso->status;
637 uiso->actual_length = iso->actual_length;
641 /* must free buffer */
642 void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
645 struct usbip_iso_packet_descriptor *iso;
646 int np = urb->number_of_packets;
647 ssize_t size = np * sizeof(*iso);
650 buff = kzalloc(size, GFP_KERNEL);
654 for (i = 0; i < np; i++) {
655 iso = buff + (i * sizeof(*iso));
657 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 1);
658 usbip_iso_packet_correct_endian(iso, 1);
665 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
667 /* some members of urb must be substituted before. */
668 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
671 struct usbip_iso_packet_descriptor *iso;
672 int np = urb->number_of_packets;
673 int size = np * sizeof(*iso);
676 int total_length = 0;
678 if (!usb_pipeisoc(urb->pipe))
681 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
683 /* pr_info("iso np == 0\n"); */
684 /* usbip_dump_urb(urb); */
688 buff = kzalloc(size, GFP_KERNEL);
692 ret = usbip_recv(ud->tcp_socket, buff, size);
694 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
698 if (ud->side == USBIP_STUB)
699 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
701 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
706 for (i = 0; i < np; i++) {
707 iso = buff + (i * sizeof(*iso));
709 usbip_iso_packet_correct_endian(iso, 0);
710 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0);
711 total_length += urb->iso_frame_desc[i].actual_length;
716 if (total_length != urb->actual_length) {
717 dev_err(&urb->dev->dev,
718 "total length of iso packets %d not equal to actual "
719 "length of buffer %d\n",
720 total_length, urb->actual_length);
722 if (ud->side == USBIP_STUB)
723 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
725 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
732 EXPORT_SYMBOL_GPL(usbip_recv_iso);
735 * This functions restores the padding which was removed for optimizing
736 * the bandwidth during transfer over tcp/ip
738 * buffer and iso packets need to be stored and be in propeper endian in urb
739 * before calling this function
741 void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
743 int np = urb->number_of_packets;
745 int actualoffset = urb->actual_length;
747 if (!usb_pipeisoc(urb->pipe))
750 /* if no packets or length of data is 0, then nothing to unpack */
751 if (np == 0 || urb->actual_length == 0)
755 * if actual_length is transfer_buffer_length then no padding is
758 if (urb->actual_length == urb->transfer_buffer_length)
762 * loop over all packets from last to first (to prevent overwritting
763 * memory when padding) and move them into the proper place
765 for (i = np-1; i > 0; i--) {
766 actualoffset -= urb->iso_frame_desc[i].actual_length;
767 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
768 urb->transfer_buffer + actualoffset,
769 urb->iso_frame_desc[i].actual_length);
772 EXPORT_SYMBOL_GPL(usbip_pad_iso);
774 /* some members of urb must be substituted before. */
775 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
780 if (ud->side == USBIP_STUB) {
782 /* the direction of urb must be OUT. */
783 if (usb_pipein(urb->pipe))
786 size = urb->transfer_buffer_length;
789 /* the direction of urb must be IN. */
790 if (usb_pipeout(urb->pipe))
793 size = urb->actual_length;
796 /* no need to recv xbuff */
800 ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
802 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
803 if (ud->side == USBIP_STUB) {
804 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
806 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
813 EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
815 static int __init usbip_core_init(void)
817 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
821 static void __exit usbip_core_exit(void)
826 module_init(usbip_core_init);
827 module_exit(usbip_core_exit);
829 MODULE_AUTHOR(DRIVER_AUTHOR);
830 MODULE_DESCRIPTION(DRIVER_DESC);
831 MODULE_LICENSE("GPL");
832 MODULE_VERSION(USBIP_VERSION);