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 <linux/kernel.h>
21 #include <linux/file.h>
22 #include <linux/tcp.h>
24 #include <linux/kthread.h>
25 #include <linux/slab.h>
26 #include "usbip_common.h"
28 /* version information */
29 #define DRIVER_VERSION "1.0"
30 #define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi _at_ users.sourceforge.net>"
31 #define DRIVER_DESC "usbip common driver"
33 /*-------------------------------------------------------------------------*/
36 #ifdef CONFIG_USB_IP_DEBUG_ENABLE
37 unsigned long usbip_debug_flag = 0xffffffff;
39 unsigned long usbip_debug_flag;
41 EXPORT_SYMBOL_GPL(usbip_debug_flag);
45 struct device_attribute dev_attr_usbip_debug;
46 EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
49 static ssize_t show_flag(struct device *dev, struct device_attribute *attr,
52 return sprintf(buf, "%lx\n", usbip_debug_flag);
55 static ssize_t store_flag(struct device *dev, struct device_attribute *attr,
56 const char *buf, size_t count)
58 sscanf(buf, "%lx", &usbip_debug_flag);
62 DEVICE_ATTR(usbip_debug, (S_IRUGO | S_IWUSR), show_flag, store_flag);
64 static void usbip_dump_buffer(char *buff, int bufflen)
66 print_hex_dump(KERN_DEBUG, "usb-ip", DUMP_PREFIX_OFFSET, 16, 4,
67 buff, bufflen, false);
70 static void usbip_dump_pipe(unsigned int p)
72 unsigned char type = usb_pipetype(p);
73 unsigned char ep = usb_pipeendpoint(p);
74 unsigned char dev = usb_pipedevice(p);
75 unsigned char dir = usb_pipein(p);
77 printk(KERN_DEBUG "dev(%d) ", dev);
78 printk(KERN_DEBUG "ep(%d) ", ep);
79 printk(KERN_DEBUG "%s ", dir ? "IN" : "OUT");
82 case PIPE_ISOCHRONOUS:
83 printk(KERN_DEBUG "%s ", "ISO");
86 printk(KERN_DEBUG "%s ", "INT");
89 printk(KERN_DEBUG "%s ", "CTL");
92 printk(KERN_DEBUG "%s ", "BLK");
95 printk(KERN_DEBUG "ERR");
98 printk(KERN_DEBUG "\n");
102 static void usbip_dump_usb_device(struct usb_device *udev)
104 struct device *dev = &udev->dev;
107 dev_dbg(dev, " devnum(%d) devpath(%s)",
108 udev->devnum, udev->devpath);
110 switch (udev->speed) {
112 printk(KERN_DEBUG " SPD_HIGH");
115 printk(KERN_DEBUG " SPD_FULL");
118 printk(KERN_DEBUG " SPD_LOW");
120 case USB_SPEED_UNKNOWN:
121 printk(KERN_DEBUG " SPD_UNKNOWN");
124 printk(KERN_DEBUG " SPD_ERROR");
127 printk(KERN_DEBUG " tt %p, ttport %d", udev->tt, udev->ttport);
128 printk(KERN_DEBUG "\n");
131 for (i = 0; i < 16; i++)
132 printk(KERN_DEBUG " %2u", i);
133 printk(KERN_DEBUG "\n");
135 dev_dbg(dev, " toggle0(IN) :");
136 for (i = 0; i < 16; i++)
137 printk(KERN_DEBUG " %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
138 printk(KERN_DEBUG "\n");
140 dev_dbg(dev, " toggle1(OUT):");
141 for (i = 0; i < 16; i++)
142 printk(KERN_DEBUG " %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
143 printk(KERN_DEBUG "\n");
146 dev_dbg(dev, " epmaxp_in :");
147 for (i = 0; i < 16; i++) {
149 printk(KERN_DEBUG " %2u",
150 le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
152 printk(KERN_DEBUG "\n");
154 dev_dbg(dev, " epmaxp_out :");
155 for (i = 0; i < 16; i++) {
157 printk(KERN_DEBUG " %2u",
158 le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
160 printk(KERN_DEBUG "\n");
162 dev_dbg(dev, "parent %p, bus %p\n", udev->parent, udev->bus);
164 dev_dbg(dev, "descriptor %p, config %p, actconfig %p, "
165 "rawdescriptors %p\n", &udev->descriptor, udev->config,
166 udev->actconfig, udev->rawdescriptors);
168 dev_dbg(dev, "have_langid %d, string_langid %d\n",
169 udev->have_langid, udev->string_langid);
171 dev_dbg(dev, "maxchild %d, children %p\n",
172 udev->maxchild, udev->children);
175 static void usbip_dump_request_type(__u8 rt)
177 switch (rt & USB_RECIP_MASK) {
178 case USB_RECIP_DEVICE:
179 printk(KERN_DEBUG "DEVICE");
181 case USB_RECIP_INTERFACE:
182 printk(KERN_DEBUG "INTERF");
184 case USB_RECIP_ENDPOINT:
185 printk(KERN_DEBUG "ENDPOI");
187 case USB_RECIP_OTHER:
188 printk(KERN_DEBUG "OTHER ");
191 printk(KERN_DEBUG "------");
195 static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
198 printk(KERN_DEBUG " %s : null pointer\n", __func__);
202 printk(KERN_DEBUG " ");
203 printk(KERN_DEBUG "bRequestType(%02X) ", cmd->bRequestType);
204 printk(KERN_DEBUG "bRequest(%02X) " , cmd->bRequest);
205 printk(KERN_DEBUG "wValue(%04X) ", cmd->wValue);
206 printk(KERN_DEBUG "wIndex(%04X) ", cmd->wIndex);
207 printk(KERN_DEBUG "wLength(%04X) ", cmd->wLength);
209 printk(KERN_DEBUG "\n ");
211 if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
212 printk(KERN_DEBUG "STANDARD ");
213 switch (cmd->bRequest) {
214 case USB_REQ_GET_STATUS:
215 printk(KERN_DEBUG "GET_STATUS");
217 case USB_REQ_CLEAR_FEATURE:
218 printk(KERN_DEBUG "CLEAR_FEAT");
220 case USB_REQ_SET_FEATURE:
221 printk(KERN_DEBUG "SET_FEAT ");
223 case USB_REQ_SET_ADDRESS:
224 printk(KERN_DEBUG "SET_ADDRRS");
226 case USB_REQ_GET_DESCRIPTOR:
227 printk(KERN_DEBUG "GET_DESCRI");
229 case USB_REQ_SET_DESCRIPTOR:
230 printk(KERN_DEBUG "SET_DESCRI");
232 case USB_REQ_GET_CONFIGURATION:
233 printk(KERN_DEBUG "GET_CONFIG");
235 case USB_REQ_SET_CONFIGURATION:
236 printk(KERN_DEBUG "SET_CONFIG");
238 case USB_REQ_GET_INTERFACE:
239 printk(KERN_DEBUG "GET_INTERF");
241 case USB_REQ_SET_INTERFACE:
242 printk(KERN_DEBUG "SET_INTERF");
244 case USB_REQ_SYNCH_FRAME:
245 printk(KERN_DEBUG "SYNC_FRAME");
248 printk(KERN_DEBUG "REQ(%02X) ", cmd->bRequest);
251 printk(KERN_DEBUG " ");
252 usbip_dump_request_type(cmd->bRequestType);
254 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
255 printk(KERN_DEBUG "CLASS ");
257 else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR)
258 printk(KERN_DEBUG "VENDOR ");
260 else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED)
261 printk(KERN_DEBUG "RESERVED");
263 printk(KERN_DEBUG "\n");
266 void usbip_dump_urb(struct urb *urb)
271 printk(KERN_DEBUG KBUILD_MODNAME
272 ":%s: urb: null pointer!!\n", __func__);
277 printk(KERN_DEBUG KBUILD_MODNAME
278 ":%s: urb->dev: null pointer!!\n", __func__);
281 dev = &urb->dev->dev;
283 dev_dbg(dev, " urb :%p\n", urb);
284 dev_dbg(dev, " dev :%p\n", urb->dev);
286 usbip_dump_usb_device(urb->dev);
288 dev_dbg(dev, " pipe :%08x ", urb->pipe);
290 usbip_dump_pipe(urb->pipe);
292 dev_dbg(dev, " status :%d\n", urb->status);
293 dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
294 dev_dbg(dev, " transfer_buffer :%p\n", urb->transfer_buffer);
295 dev_dbg(dev, " transfer_buffer_length:%d\n",
296 urb->transfer_buffer_length);
297 dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
298 dev_dbg(dev, " setup_packet :%p\n", urb->setup_packet);
300 if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
301 usbip_dump_usb_ctrlrequest(
302 (struct usb_ctrlrequest *)urb->setup_packet);
304 dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
305 dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
306 dev_dbg(dev, " interval :%d\n", urb->interval);
307 dev_dbg(dev, " error_count :%d\n", urb->error_count);
308 dev_dbg(dev, " context :%p\n", urb->context);
309 dev_dbg(dev, " complete :%p\n", urb->complete);
311 EXPORT_SYMBOL_GPL(usbip_dump_urb);
313 void usbip_dump_header(struct usbip_header *pdu)
315 usbip_udbg("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
322 switch (pdu->base.command) {
323 case USBIP_CMD_SUBMIT:
324 usbip_udbg("CMD_SUBMIT: "
325 "x_flags %u x_len %u sf %u #p %u iv %u\n",
326 pdu->u.cmd_submit.transfer_flags,
327 pdu->u.cmd_submit.transfer_buffer_length,
328 pdu->u.cmd_submit.start_frame,
329 pdu->u.cmd_submit.number_of_packets,
330 pdu->u.cmd_submit.interval);
332 case USBIP_CMD_UNLINK:
333 usbip_udbg("CMD_UNLINK: seq %u\n", pdu->u.cmd_unlink.seqnum);
335 case USBIP_RET_SUBMIT:
336 usbip_udbg("RET_SUBMIT: st %d al %u sf %d ec %d\n",
337 pdu->u.ret_submit.status,
338 pdu->u.ret_submit.actual_length,
339 pdu->u.ret_submit.start_frame,
340 pdu->u.ret_submit.error_count);
341 case USBIP_RET_UNLINK:
342 usbip_udbg("RET_UNLINK: status %d\n", pdu->u.ret_unlink.status);
346 usbip_udbg("UNKNOWN\n");
349 EXPORT_SYMBOL_GPL(usbip_dump_header);
351 /*-------------------------------------------------------------------------*/
352 /* socket routines */
354 /* Send/receive messages over TCP/IP. I refer drivers/block/nbd.c */
355 int usbip_xmit(int send, struct socket *sock, char *buf,
356 int size, int msg_flags)
363 /* for blocks of if (usbip_dbg_flag_xmit) */
367 usbip_dbg_xmit("enter\n");
369 if (!sock || !buf || !size) {
370 printk(KERN_ERR "%s: invalid arg, sock %p buff %p size %d\n",
371 __func__, sock, buf, size);
376 if (usbip_dbg_flag_xmit) {
379 printk(KERN_DEBUG "%-10s:", current->comm);
381 printk(KERN_DEBUG "interrupt :");
383 printk(KERN_DEBUG "%s: sending... , sock %p, buf %p, "
384 "size %d, msg_flags %d\n", __func__,
385 sock, buf, size, msg_flags);
386 usbip_dump_buffer(buf, size);
392 sock->sk->sk_allocation = GFP_NOIO;
397 msg.msg_control = NULL;
398 msg.msg_controllen = 0;
400 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
403 result = kernel_sendmsg(sock, &msg, &iov, 1, size);
405 result = kernel_recvmsg(sock, &msg, &iov, 1, size,
409 usbip_udbg("usbip_xmit: %s sock %p buf %p size %u ret "
411 send ? "send" : "receive", sock, buf,
412 size, result, total);
423 if (usbip_dbg_flag_xmit) {
426 printk(KERN_DEBUG "%-10s:", current->comm);
428 printk(KERN_DEBUG "interrupt :");
430 printk(KERN_DEBUG "usbip_xmit: receiving....\n");
431 usbip_dump_buffer(bp, osize);
432 printk(KERN_DEBUG "usbip_xmit: received, osize %d ret "
433 "%d size %d total %d\n", osize, result,
438 printk(KERN_DEBUG "usbip_xmit: send, total %d\n",
447 EXPORT_SYMBOL_GPL(usbip_xmit);
449 struct socket *sockfd_to_socket(unsigned int sockfd)
451 struct socket *socket;
457 printk(KERN_ERR "%s: invalid sockfd\n", __func__);
461 inode = file->f_dentry->d_inode;
463 if (!inode || !S_ISSOCK(inode->i_mode))
466 socket = SOCKET_I(inode);
470 EXPORT_SYMBOL_GPL(sockfd_to_socket);
474 /*-------------------------------------------------------------------------*/
477 /* there may be more cases to tweak the flags. */
478 static unsigned int tweak_transfer_flags(unsigned int flags)
480 flags &= ~URB_NO_TRANSFER_DMA_MAP;
484 static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
487 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
490 * Some members are not still implemented in usbip. I hope this issue
491 * will be discussed when usbip is ported to other operating systems.
495 spdu->transfer_flags =
496 tweak_transfer_flags(urb->transfer_flags);
497 spdu->transfer_buffer_length = urb->transfer_buffer_length;
498 spdu->start_frame = urb->start_frame;
499 spdu->number_of_packets = urb->number_of_packets;
500 spdu->interval = urb->interval;
503 urb->transfer_flags = spdu->transfer_flags;
505 urb->transfer_buffer_length = spdu->transfer_buffer_length;
506 urb->start_frame = spdu->start_frame;
507 urb->number_of_packets = spdu->number_of_packets;
508 urb->interval = spdu->interval;
512 static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
515 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
520 rpdu->status = urb->status;
521 rpdu->actual_length = urb->actual_length;
522 rpdu->start_frame = urb->start_frame;
523 rpdu->error_count = urb->error_count;
527 urb->status = rpdu->status;
528 urb->actual_length = rpdu->actual_length;
529 urb->start_frame = rpdu->start_frame;
530 urb->error_count = rpdu->error_count;
535 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
539 case USBIP_CMD_SUBMIT:
540 usbip_pack_cmd_submit(pdu, urb, pack);
542 case USBIP_RET_SUBMIT:
543 usbip_pack_ret_submit(pdu, urb, pack);
546 err("unknown command");
551 EXPORT_SYMBOL_GPL(usbip_pack_pdu);
554 static void correct_endian_basic(struct usbip_header_basic *base, int send)
557 base->command = cpu_to_be32(base->command);
558 base->seqnum = cpu_to_be32(base->seqnum);
559 base->devid = cpu_to_be32(base->devid);
560 base->direction = cpu_to_be32(base->direction);
561 base->ep = cpu_to_be32(base->ep);
563 base->command = be32_to_cpu(base->command);
564 base->seqnum = be32_to_cpu(base->seqnum);
565 base->devid = be32_to_cpu(base->devid);
566 base->direction = be32_to_cpu(base->direction);
567 base->ep = be32_to_cpu(base->ep);
571 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
575 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
577 cpu_to_be32s(&pdu->transfer_buffer_length);
578 cpu_to_be32s(&pdu->start_frame);
579 cpu_to_be32s(&pdu->number_of_packets);
580 cpu_to_be32s(&pdu->interval);
582 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
584 be32_to_cpus(&pdu->transfer_buffer_length);
585 be32_to_cpus(&pdu->start_frame);
586 be32_to_cpus(&pdu->number_of_packets);
587 be32_to_cpus(&pdu->interval);
591 static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
595 cpu_to_be32s(&pdu->status);
596 cpu_to_be32s(&pdu->actual_length);
597 cpu_to_be32s(&pdu->start_frame);
598 cpu_to_be32s(&pdu->error_count);
600 be32_to_cpus(&pdu->status);
601 be32_to_cpus(&pdu->actual_length);
602 be32_to_cpus(&pdu->start_frame);
603 be32_to_cpus(&pdu->error_count);
607 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
611 pdu->seqnum = cpu_to_be32(pdu->seqnum);
613 pdu->seqnum = be32_to_cpu(pdu->seqnum);
616 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
620 cpu_to_be32s(&pdu->status);
622 be32_to_cpus(&pdu->status);
625 void usbip_header_correct_endian(struct usbip_header *pdu, int send)
630 cmd = pdu->base.command;
632 correct_endian_basic(&pdu->base, send);
635 cmd = pdu->base.command;
638 case USBIP_CMD_SUBMIT:
639 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
641 case USBIP_RET_SUBMIT:
642 correct_endian_ret_submit(&pdu->u.ret_submit, send);
644 case USBIP_CMD_UNLINK:
645 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
647 case USBIP_RET_UNLINK:
648 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
652 err("unknown command in pdu header: %d", cmd);
656 EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
658 static void usbip_iso_pakcet_correct_endian(
659 struct usbip_iso_packet_descriptor *iso,
662 /* does not need all members. but copy all simply. */
664 iso->offset = cpu_to_be32(iso->offset);
665 iso->length = cpu_to_be32(iso->length);
666 iso->status = cpu_to_be32(iso->status);
667 iso->actual_length = cpu_to_be32(iso->actual_length);
669 iso->offset = be32_to_cpu(iso->offset);
670 iso->length = be32_to_cpu(iso->length);
671 iso->status = be32_to_cpu(iso->status);
672 iso->actual_length = be32_to_cpu(iso->actual_length);
676 static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
677 struct usb_iso_packet_descriptor *uiso, int pack)
680 iso->offset = uiso->offset;
681 iso->length = uiso->length;
682 iso->status = uiso->status;
683 iso->actual_length = uiso->actual_length;
685 uiso->offset = iso->offset;
686 uiso->length = iso->length;
687 uiso->status = iso->status;
688 uiso->actual_length = iso->actual_length;
693 /* must free buffer */
694 void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
697 struct usbip_iso_packet_descriptor *iso;
698 int np = urb->number_of_packets;
699 ssize_t size = np * sizeof(*iso);
702 buff = kzalloc(size, GFP_KERNEL);
706 for (i = 0; i < np; i++) {
707 iso = buff + (i * sizeof(*iso));
709 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 1);
710 usbip_iso_pakcet_correct_endian(iso, 1);
717 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
719 /* some members of urb must be substituted before. */
720 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
723 struct usbip_iso_packet_descriptor *iso;
724 int np = urb->number_of_packets;
725 int size = np * sizeof(*iso);
729 if (!usb_pipeisoc(urb->pipe))
732 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
734 /* usbip_uinfo("iso np == 0\n"); */
735 /* usbip_dump_urb(urb); */
739 buff = kzalloc(size, GFP_KERNEL);
743 ret = usbip_xmit(0, ud->tcp_socket, buff, size, 0);
745 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
749 if (ud->side == USBIP_STUB)
750 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
752 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
757 for (i = 0; i < np; i++) {
758 iso = buff + (i * sizeof(*iso));
760 usbip_iso_pakcet_correct_endian(iso, 0);
761 usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0);
768 EXPORT_SYMBOL_GPL(usbip_recv_iso);
771 /* some members of urb must be substituted before. */
772 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
777 if (ud->side == USBIP_STUB) {
779 /* the direction of urb must be OUT. */
780 if (usb_pipein(urb->pipe))
783 size = urb->transfer_buffer_length;
786 /* the direction of urb must be IN. */
787 if (usb_pipeout(urb->pipe))
790 size = urb->actual_length;
793 /* no need to recv xbuff */
797 ret = usbip_xmit(0, ud->tcp_socket, (char *)urb->transfer_buffer,
800 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
801 if (ud->side == USBIP_STUB) {
802 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
804 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
811 EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
814 /*-------------------------------------------------------------------------*/
816 static int __init usbip_common_init(void)
818 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "" DRIVER_VERSION);
823 static void __exit usbip_common_exit(void)
831 module_init(usbip_common_init);
832 module_exit(usbip_common_exit);
834 MODULE_AUTHOR(DRIVER_AUTHOR);
835 MODULE_DESCRIPTION(DRIVER_DESC);
836 MODULE_LICENSE("GPL");