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 usbip_debug_show(struct device *dev,
49 struct device_attribute *attr, char *buf)
51 return sprintf(buf, "%lx\n", usbip_debug_flag);
54 static ssize_t usbip_debug_store(struct device *dev,
55 struct device_attribute *attr, const char *buf,
58 if (sscanf(buf, "%lx", &usbip_debug_flag) != 1)
62 DEVICE_ATTR_RW(usbip_debug);
64 static void usbip_dump_buffer(char *buff, int bufflen)
66 print_hex_dump(KERN_DEBUG, "usbip-core", 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 pr_debug("dev(%d) ep(%d) [%s] ", dev, ep, dir ? "IN" : "OUT");
80 case PIPE_ISOCHRONOUS:
98 static void usbip_dump_usb_device(struct usb_device *udev)
100 struct device *dev = &udev->dev;
103 dev_dbg(dev, " devnum(%d) devpath(%s) usb speed(%s)",
104 udev->devnum, udev->devpath, usb_speed_string(udev->speed));
106 pr_debug("tt hub ttport %d\n", udev->ttport);
109 for (i = 0; i < 16; i++)
113 dev_dbg(dev, " toggle0(IN) :");
114 for (i = 0; i < 16; i++)
115 pr_debug(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
118 dev_dbg(dev, " toggle1(OUT):");
119 for (i = 0; i < 16; i++)
120 pr_debug(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
123 dev_dbg(dev, " epmaxp_in :");
124 for (i = 0; i < 16; i++) {
127 le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
131 dev_dbg(dev, " epmaxp_out :");
132 for (i = 0; i < 16; i++) {
135 le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
139 dev_dbg(dev, "parent %s, bus %s\n", dev_name(&udev->parent->dev),
140 udev->bus->bus_name);
142 dev_dbg(dev, "have_langid %d, string_langid %d\n",
143 udev->have_langid, udev->string_langid);
145 dev_dbg(dev, "maxchild %d\n", udev->maxchild);
148 static void usbip_dump_request_type(__u8 rt)
150 switch (rt & USB_RECIP_MASK) {
151 case USB_RECIP_DEVICE:
154 case USB_RECIP_INTERFACE:
157 case USB_RECIP_ENDPOINT:
160 case USB_RECIP_OTHER:
169 static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
172 pr_debug(" : null pointer\n");
177 pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) wLength(%04X) ",
178 cmd->bRequestType, cmd->bRequest,
179 cmd->wValue, cmd->wIndex, cmd->wLength);
182 if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
183 pr_debug("STANDARD ");
184 switch (cmd->bRequest) {
185 case USB_REQ_GET_STATUS:
186 pr_debug("GET_STATUS\n");
188 case USB_REQ_CLEAR_FEATURE:
189 pr_debug("CLEAR_FEAT\n");
191 case USB_REQ_SET_FEATURE:
192 pr_debug("SET_FEAT\n");
194 case USB_REQ_SET_ADDRESS:
195 pr_debug("SET_ADDRRS\n");
197 case USB_REQ_GET_DESCRIPTOR:
198 pr_debug("GET_DESCRI\n");
200 case USB_REQ_SET_DESCRIPTOR:
201 pr_debug("SET_DESCRI\n");
203 case USB_REQ_GET_CONFIGURATION:
204 pr_debug("GET_CONFIG\n");
206 case USB_REQ_SET_CONFIGURATION:
207 pr_debug("SET_CONFIG\n");
209 case USB_REQ_GET_INTERFACE:
210 pr_debug("GET_INTERF\n");
212 case USB_REQ_SET_INTERFACE:
213 pr_debug("SET_INTERF\n");
215 case USB_REQ_SYNCH_FRAME:
216 pr_debug("SYNC_FRAME\n");
219 pr_debug("REQ(%02X)\n", cmd->bRequest);
222 usbip_dump_request_type(cmd->bRequestType);
223 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
225 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
226 pr_debug("VENDOR\n");
227 } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
228 pr_debug("RESERVED\n");
232 void usbip_dump_urb(struct urb *urb)
237 pr_debug("urb: null pointer!!\n");
242 pr_debug("urb->dev: null pointer!!\n");
246 dev = &urb->dev->dev;
248 usbip_dump_usb_device(urb->dev);
250 dev_dbg(dev, " pipe :%08x ", urb->pipe);
252 usbip_dump_pipe(urb->pipe);
254 dev_dbg(dev, " status :%d\n", urb->status);
255 dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
256 dev_dbg(dev, " transfer_buffer_length:%d\n",
257 urb->transfer_buffer_length);
258 dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
260 if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
261 usbip_dump_usb_ctrlrequest(
262 (struct usb_ctrlrequest *)urb->setup_packet);
264 dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
265 dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
266 dev_dbg(dev, " interval :%d\n", urb->interval);
267 dev_dbg(dev, " error_count :%d\n", urb->error_count);
269 EXPORT_SYMBOL_GPL(usbip_dump_urb);
271 void usbip_dump_header(struct usbip_header *pdu)
273 pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
280 switch (pdu->base.command) {
281 case USBIP_CMD_SUBMIT:
282 pr_debug("USBIP_CMD_SUBMIT: x_flags %u x_len %u sf %u #p %d iv %d\n",
283 pdu->u.cmd_submit.transfer_flags,
284 pdu->u.cmd_submit.transfer_buffer_length,
285 pdu->u.cmd_submit.start_frame,
286 pdu->u.cmd_submit.number_of_packets,
287 pdu->u.cmd_submit.interval);
289 case USBIP_CMD_UNLINK:
290 pr_debug("USBIP_CMD_UNLINK: seq %u\n",
291 pdu->u.cmd_unlink.seqnum);
293 case USBIP_RET_SUBMIT:
294 pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
295 pdu->u.ret_submit.status,
296 pdu->u.ret_submit.actual_length,
297 pdu->u.ret_submit.start_frame,
298 pdu->u.ret_submit.number_of_packets,
299 pdu->u.ret_submit.error_count);
301 case USBIP_RET_UNLINK:
302 pr_debug("USBIP_RET_UNLINK: status %d\n",
303 pdu->u.ret_unlink.status);
307 pr_err("unknown command\n");
311 EXPORT_SYMBOL_GPL(usbip_dump_header);
313 /* Receive data over TCP/IP. */
314 int usbip_recv(struct socket *sock, void *buf, int size)
320 /* for blocks of if (usbip_dbg_flag_xmit) */
324 if (!sock || !buf || !size)
327 usbip_dbg_xmit("enter\n");
330 sock->sk->sk_allocation = GFP_NOIO;
335 msg.msg_control = NULL;
336 msg.msg_controllen = 0;
337 msg.msg_flags = MSG_NOSIGNAL;
339 result = kernel_recvmsg(sock, &msg, &iov, 1, size, MSG_WAITALL);
348 if (usbip_dbg_flag_xmit) {
350 pr_debug("%-10s:", current->comm);
352 pr_debug("interrupt :");
354 pr_debug("receiving....\n");
355 usbip_dump_buffer(bp, osize);
356 pr_debug("received, osize %d ret %d size %d total %d\n",
357 osize, result, size, total);
365 EXPORT_SYMBOL_GPL(usbip_recv);
367 /* there may be more cases to tweak the flags. */
368 static unsigned int tweak_transfer_flags(unsigned int flags)
370 flags &= ~URB_NO_TRANSFER_DMA_MAP;
374 static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
377 struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
380 * Some members are not still implemented in usbip. I hope this issue
381 * will be discussed when usbip is ported to other operating systems.
384 spdu->transfer_flags =
385 tweak_transfer_flags(urb->transfer_flags);
386 spdu->transfer_buffer_length = urb->transfer_buffer_length;
387 spdu->start_frame = urb->start_frame;
388 spdu->number_of_packets = urb->number_of_packets;
389 spdu->interval = urb->interval;
391 urb->transfer_flags = spdu->transfer_flags;
392 urb->transfer_buffer_length = spdu->transfer_buffer_length;
393 urb->start_frame = spdu->start_frame;
394 urb->number_of_packets = spdu->number_of_packets;
395 urb->interval = spdu->interval;
399 static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
402 struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
405 rpdu->status = urb->status;
406 rpdu->actual_length = urb->actual_length;
407 rpdu->start_frame = urb->start_frame;
408 rpdu->number_of_packets = urb->number_of_packets;
409 rpdu->error_count = urb->error_count;
411 urb->status = rpdu->status;
412 urb->actual_length = rpdu->actual_length;
413 urb->start_frame = rpdu->start_frame;
414 urb->number_of_packets = rpdu->number_of_packets;
415 urb->error_count = rpdu->error_count;
419 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
423 case USBIP_CMD_SUBMIT:
424 usbip_pack_cmd_submit(pdu, urb, pack);
426 case USBIP_RET_SUBMIT:
427 usbip_pack_ret_submit(pdu, urb, pack);
431 pr_err("unknown command\n");
435 EXPORT_SYMBOL_GPL(usbip_pack_pdu);
437 static void correct_endian_basic(struct usbip_header_basic *base, int send)
440 base->command = cpu_to_be32(base->command);
441 base->seqnum = cpu_to_be32(base->seqnum);
442 base->devid = cpu_to_be32(base->devid);
443 base->direction = cpu_to_be32(base->direction);
444 base->ep = cpu_to_be32(base->ep);
446 base->command = be32_to_cpu(base->command);
447 base->seqnum = be32_to_cpu(base->seqnum);
448 base->devid = be32_to_cpu(base->devid);
449 base->direction = be32_to_cpu(base->direction);
450 base->ep = be32_to_cpu(base->ep);
454 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
458 pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
460 cpu_to_be32s(&pdu->transfer_buffer_length);
461 cpu_to_be32s(&pdu->start_frame);
462 cpu_to_be32s(&pdu->number_of_packets);
463 cpu_to_be32s(&pdu->interval);
465 pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
467 be32_to_cpus(&pdu->transfer_buffer_length);
468 be32_to_cpus(&pdu->start_frame);
469 be32_to_cpus(&pdu->number_of_packets);
470 be32_to_cpus(&pdu->interval);
474 static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
478 cpu_to_be32s(&pdu->status);
479 cpu_to_be32s(&pdu->actual_length);
480 cpu_to_be32s(&pdu->start_frame);
481 cpu_to_be32s(&pdu->number_of_packets);
482 cpu_to_be32s(&pdu->error_count);
484 be32_to_cpus(&pdu->status);
485 be32_to_cpus(&pdu->actual_length);
486 be32_to_cpus(&pdu->start_frame);
487 be32_to_cpus(&pdu->number_of_packets);
488 be32_to_cpus(&pdu->error_count);
492 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
496 pdu->seqnum = cpu_to_be32(pdu->seqnum);
498 pdu->seqnum = be32_to_cpu(pdu->seqnum);
501 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
505 cpu_to_be32s(&pdu->status);
507 be32_to_cpus(&pdu->status);
510 void usbip_header_correct_endian(struct usbip_header *pdu, int send)
515 cmd = pdu->base.command;
517 correct_endian_basic(&pdu->base, send);
520 cmd = pdu->base.command;
523 case USBIP_CMD_SUBMIT:
524 correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
526 case USBIP_RET_SUBMIT:
527 correct_endian_ret_submit(&pdu->u.ret_submit, send);
529 case USBIP_CMD_UNLINK:
530 correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
532 case USBIP_RET_UNLINK:
533 correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
537 pr_err("unknown command\n");
541 EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
543 static void usbip_iso_packet_correct_endian(
544 struct usbip_iso_packet_descriptor *iso, int send)
546 /* does not need all members. but copy all simply. */
548 iso->offset = cpu_to_be32(iso->offset);
549 iso->length = cpu_to_be32(iso->length);
550 iso->status = cpu_to_be32(iso->status);
551 iso->actual_length = cpu_to_be32(iso->actual_length);
553 iso->offset = be32_to_cpu(iso->offset);
554 iso->length = be32_to_cpu(iso->length);
555 iso->status = be32_to_cpu(iso->status);
556 iso->actual_length = be32_to_cpu(iso->actual_length);
560 static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
561 struct usb_iso_packet_descriptor *uiso, int pack)
564 iso->offset = uiso->offset;
565 iso->length = uiso->length;
566 iso->status = uiso->status;
567 iso->actual_length = uiso->actual_length;
569 uiso->offset = iso->offset;
570 uiso->length = iso->length;
571 uiso->status = iso->status;
572 uiso->actual_length = iso->actual_length;
576 /* must free buffer */
577 struct usbip_iso_packet_descriptor*
578 usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
580 struct usbip_iso_packet_descriptor *iso;
581 int np = urb->number_of_packets;
582 ssize_t size = np * sizeof(*iso);
585 iso = kzalloc(size, GFP_KERNEL);
589 for (i = 0; i < np; i++) {
590 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
591 usbip_iso_packet_correct_endian(&iso[i], 1);
598 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
600 /* some members of urb must be substituted before. */
601 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
604 struct usbip_iso_packet_descriptor *iso;
605 int np = urb->number_of_packets;
606 int size = np * sizeof(*iso);
609 int total_length = 0;
611 if (!usb_pipeisoc(urb->pipe))
614 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
618 buff = kzalloc(size, GFP_KERNEL);
622 ret = usbip_recv(ud->tcp_socket, buff, size);
624 dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
628 if (ud->side == USBIP_STUB)
629 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
631 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
636 iso = (struct usbip_iso_packet_descriptor *) buff;
637 for (i = 0; i < np; i++) {
638 usbip_iso_packet_correct_endian(&iso[i], 0);
639 usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
640 total_length += urb->iso_frame_desc[i].actual_length;
645 if (total_length != urb->actual_length) {
646 dev_err(&urb->dev->dev,
647 "total length of iso packets %d not equal to actual length of buffer %d\n",
648 total_length, urb->actual_length);
650 if (ud->side == USBIP_STUB)
651 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
653 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
660 EXPORT_SYMBOL_GPL(usbip_recv_iso);
663 * This functions restores the padding which was removed for optimizing
664 * the bandwidth during transfer over tcp/ip
666 * buffer and iso packets need to be stored and be in propeper endian in urb
667 * before calling this function
669 void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
671 int np = urb->number_of_packets;
673 int actualoffset = urb->actual_length;
675 if (!usb_pipeisoc(urb->pipe))
678 /* if no packets or length of data is 0, then nothing to unpack */
679 if (np == 0 || urb->actual_length == 0)
683 * if actual_length is transfer_buffer_length then no padding is
686 if (urb->actual_length == urb->transfer_buffer_length)
690 * loop over all packets from last to first (to prevent overwritting
691 * memory when padding) and move them into the proper place
693 for (i = np-1; i > 0; i--) {
694 actualoffset -= urb->iso_frame_desc[i].actual_length;
695 memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
696 urb->transfer_buffer + actualoffset,
697 urb->iso_frame_desc[i].actual_length);
700 EXPORT_SYMBOL_GPL(usbip_pad_iso);
702 /* some members of urb must be substituted before. */
703 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
708 if (ud->side == USBIP_STUB) {
709 /* the direction of urb must be OUT. */
710 if (usb_pipein(urb->pipe))
713 size = urb->transfer_buffer_length;
715 /* the direction of urb must be IN. */
716 if (usb_pipeout(urb->pipe))
719 size = urb->actual_length;
722 /* no need to recv xbuff */
726 if (size > urb->transfer_buffer_length) {
727 /* should not happen, probably malicious packet */
728 if (ud->side == USBIP_STUB) {
729 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
732 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
737 ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
739 dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
740 if (ud->side == USBIP_STUB) {
741 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
743 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
750 EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
752 static int __init usbip_core_init(void)
754 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
758 static void __exit usbip_core_exit(void)
763 module_init(usbip_core_init);
764 module_exit(usbip_core_exit);
766 MODULE_AUTHOR(DRIVER_AUTHOR);
767 MODULE_DESCRIPTION(DRIVER_DESC);
768 MODULE_LICENSE("GPL");
769 MODULE_VERSION(USBIP_VERSION);