2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 * Copyright (C) 2015-2016 Samsung Electronics
4 * Krzysztof Opasiak <k.opasiak@samsung.com>
6 * This is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 #include <asm/byteorder.h>
23 #include <linux/file.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/stat.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
32 #include "usbip_common.h"
34 #define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>"
35 #define DRIVER_DESC "USB/IP Core"
37 #ifdef CONFIG_USBIP_DEBUG
38 unsigned long usbip_debug_flag
= 0xffffffff;
40 unsigned long usbip_debug_flag
;
42 EXPORT_SYMBOL_GPL(usbip_debug_flag
);
43 module_param(usbip_debug_flag
, ulong
, S_IRUGO
|S_IWUSR
);
44 MODULE_PARM_DESC(usbip_debug_flag
, "debug flags (defined in usbip_common.h)");
47 struct device_attribute dev_attr_usbip_debug
;
48 EXPORT_SYMBOL_GPL(dev_attr_usbip_debug
);
50 static ssize_t
usbip_debug_show(struct device
*dev
,
51 struct device_attribute
*attr
, char *buf
)
53 return sprintf(buf
, "%lx\n", usbip_debug_flag
);
56 static ssize_t
usbip_debug_store(struct device
*dev
,
57 struct device_attribute
*attr
, const char *buf
,
60 if (sscanf(buf
, "%lx", &usbip_debug_flag
) != 1)
64 DEVICE_ATTR_RW(usbip_debug
);
66 static void usbip_dump_buffer(char *buff
, int bufflen
)
68 print_hex_dump(KERN_DEBUG
, "usbip-core", DUMP_PREFIX_OFFSET
, 16, 4,
69 buff
, bufflen
, false);
72 static void usbip_dump_pipe(unsigned int p
)
74 unsigned char type
= usb_pipetype(p
);
75 unsigned char ep
= usb_pipeendpoint(p
);
76 unsigned char dev
= usb_pipedevice(p
);
77 unsigned char dir
= usb_pipein(p
);
79 pr_debug("dev(%d) ep(%d) [%s] ", dev
, ep
, dir
? "IN" : "OUT");
82 case PIPE_ISOCHRONOUS
:
100 static void usbip_dump_usb_device(struct usb_device
*udev
)
102 struct device
*dev
= &udev
->dev
;
105 dev_dbg(dev
, " devnum(%d) devpath(%s) usb speed(%s)",
106 udev
->devnum
, udev
->devpath
, usb_speed_string(udev
->speed
));
108 pr_debug("tt hub ttport %d\n", udev
->ttport
);
111 for (i
= 0; i
< 16; i
++)
115 dev_dbg(dev
, " toggle0(IN) :");
116 for (i
= 0; i
< 16; i
++)
117 pr_debug(" %2u", (udev
->toggle
[0] & (1 << i
)) ? 1 : 0);
120 dev_dbg(dev
, " toggle1(OUT):");
121 for (i
= 0; i
< 16; i
++)
122 pr_debug(" %2u", (udev
->toggle
[1] & (1 << i
)) ? 1 : 0);
125 dev_dbg(dev
, " epmaxp_in :");
126 for (i
= 0; i
< 16; i
++) {
129 le16_to_cpu(udev
->ep_in
[i
]->desc
.wMaxPacketSize
));
133 dev_dbg(dev
, " epmaxp_out :");
134 for (i
= 0; i
< 16; i
++) {
137 le16_to_cpu(udev
->ep_out
[i
]->desc
.wMaxPacketSize
));
141 dev_dbg(dev
, "parent %s, bus %s\n", dev_name(&udev
->parent
->dev
),
142 udev
->bus
->bus_name
);
144 dev_dbg(dev
, "have_langid %d, string_langid %d\n",
145 udev
->have_langid
, udev
->string_langid
);
147 dev_dbg(dev
, "maxchild %d\n", udev
->maxchild
);
150 static void usbip_dump_request_type(__u8 rt
)
152 switch (rt
& USB_RECIP_MASK
) {
153 case USB_RECIP_DEVICE
:
156 case USB_RECIP_INTERFACE
:
159 case USB_RECIP_ENDPOINT
:
162 case USB_RECIP_OTHER
:
171 static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest
*cmd
)
174 pr_debug(" : null pointer\n");
179 pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) wLength(%04X) ",
180 cmd
->bRequestType
, cmd
->bRequest
,
181 cmd
->wValue
, cmd
->wIndex
, cmd
->wLength
);
184 if ((cmd
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
185 pr_debug("STANDARD ");
186 switch (cmd
->bRequest
) {
187 case USB_REQ_GET_STATUS
:
188 pr_debug("GET_STATUS\n");
190 case USB_REQ_CLEAR_FEATURE
:
191 pr_debug("CLEAR_FEAT\n");
193 case USB_REQ_SET_FEATURE
:
194 pr_debug("SET_FEAT\n");
196 case USB_REQ_SET_ADDRESS
:
197 pr_debug("SET_ADDRRS\n");
199 case USB_REQ_GET_DESCRIPTOR
:
200 pr_debug("GET_DESCRI\n");
202 case USB_REQ_SET_DESCRIPTOR
:
203 pr_debug("SET_DESCRI\n");
205 case USB_REQ_GET_CONFIGURATION
:
206 pr_debug("GET_CONFIG\n");
208 case USB_REQ_SET_CONFIGURATION
:
209 pr_debug("SET_CONFIG\n");
211 case USB_REQ_GET_INTERFACE
:
212 pr_debug("GET_INTERF\n");
214 case USB_REQ_SET_INTERFACE
:
215 pr_debug("SET_INTERF\n");
217 case USB_REQ_SYNCH_FRAME
:
218 pr_debug("SYNC_FRAME\n");
221 pr_debug("REQ(%02X)\n", cmd
->bRequest
);
224 usbip_dump_request_type(cmd
->bRequestType
);
225 } else if ((cmd
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_CLASS
) {
227 } else if ((cmd
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_VENDOR
) {
228 pr_debug("VENDOR\n");
229 } else if ((cmd
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_RESERVED
) {
230 pr_debug("RESERVED\n");
234 void usbip_dump_urb(struct urb
*urb
)
239 pr_debug("urb: null pointer!!\n");
244 pr_debug("urb->dev: null pointer!!\n");
248 dev
= &urb
->dev
->dev
;
250 usbip_dump_usb_device(urb
->dev
);
252 dev_dbg(dev
, " pipe :%08x ", urb
->pipe
);
254 usbip_dump_pipe(urb
->pipe
);
256 dev_dbg(dev
, " status :%d\n", urb
->status
);
257 dev_dbg(dev
, " transfer_flags :%08X\n", urb
->transfer_flags
);
258 dev_dbg(dev
, " transfer_buffer_length:%d\n",
259 urb
->transfer_buffer_length
);
260 dev_dbg(dev
, " actual_length :%d\n", urb
->actual_length
);
262 if (urb
->setup_packet
&& usb_pipetype(urb
->pipe
) == PIPE_CONTROL
)
263 usbip_dump_usb_ctrlrequest(
264 (struct usb_ctrlrequest
*)urb
->setup_packet
);
266 dev_dbg(dev
, " start_frame :%d\n", urb
->start_frame
);
267 dev_dbg(dev
, " number_of_packets :%d\n", urb
->number_of_packets
);
268 dev_dbg(dev
, " interval :%d\n", urb
->interval
);
269 dev_dbg(dev
, " error_count :%d\n", urb
->error_count
);
271 EXPORT_SYMBOL_GPL(usbip_dump_urb
);
273 void usbip_dump_header(struct usbip_header
*pdu
)
275 pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
282 switch (pdu
->base
.command
) {
283 case USBIP_CMD_SUBMIT
:
284 pr_debug("USBIP_CMD_SUBMIT: x_flags %u x_len %u sf %u #p %d iv %d\n",
285 pdu
->u
.cmd_submit
.transfer_flags
,
286 pdu
->u
.cmd_submit
.transfer_buffer_length
,
287 pdu
->u
.cmd_submit
.start_frame
,
288 pdu
->u
.cmd_submit
.number_of_packets
,
289 pdu
->u
.cmd_submit
.interval
);
291 case USBIP_CMD_UNLINK
:
292 pr_debug("USBIP_CMD_UNLINK: seq %u\n",
293 pdu
->u
.cmd_unlink
.seqnum
);
295 case USBIP_RET_SUBMIT
:
296 pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
297 pdu
->u
.ret_submit
.status
,
298 pdu
->u
.ret_submit
.actual_length
,
299 pdu
->u
.ret_submit
.start_frame
,
300 pdu
->u
.ret_submit
.number_of_packets
,
301 pdu
->u
.ret_submit
.error_count
);
303 case USBIP_RET_UNLINK
:
304 pr_debug("USBIP_RET_UNLINK: status %d\n",
305 pdu
->u
.ret_unlink
.status
);
309 pr_err("unknown command\n");
313 EXPORT_SYMBOL_GPL(usbip_dump_header
);
315 /* Receive data over TCP/IP. */
316 int usbip_recv(struct socket
*sock
, void *buf
, int size
)
323 /* for blocks of if (usbip_dbg_flag_xmit) */
327 if (!sock
|| !buf
|| !size
)
330 usbip_dbg_xmit("enter\n");
333 sock
->sk
->sk_allocation
= GFP_NOIO
;
338 msg
.msg_control
= NULL
;
339 msg
.msg_controllen
= 0;
340 msg
.msg_flags
= MSG_NOSIGNAL
;
342 result
= kernel_recvmsg(sock
, &msg
, &iov
, 1, size
, MSG_WAITALL
);
351 if (usbip_dbg_flag_xmit
) {
353 pr_debug("%-10s:", current
->comm
);
355 pr_debug("interrupt :");
357 pr_debug("receiving....\n");
358 usbip_dump_buffer(bp
, osize
);
359 pr_debug("received, osize %d ret %d size %d total %d\n",
360 osize
, result
, size
, total
);
368 EXPORT_SYMBOL_GPL(usbip_recv
);
370 /* there may be more cases to tweak the flags. */
371 static unsigned int tweak_transfer_flags(unsigned int flags
)
373 flags
&= ~URB_NO_TRANSFER_DMA_MAP
;
377 static void usbip_pack_cmd_submit(struct usbip_header
*pdu
, struct urb
*urb
,
380 struct usbip_header_cmd_submit
*spdu
= &pdu
->u
.cmd_submit
;
383 * Some members are not still implemented in usbip. I hope this issue
384 * will be discussed when usbip is ported to other operating systems.
387 spdu
->transfer_flags
=
388 tweak_transfer_flags(urb
->transfer_flags
);
389 spdu
->transfer_buffer_length
= urb
->transfer_buffer_length
;
390 spdu
->start_frame
= urb
->start_frame
;
391 spdu
->number_of_packets
= urb
->number_of_packets
;
392 spdu
->interval
= urb
->interval
;
394 urb
->transfer_flags
= spdu
->transfer_flags
;
395 urb
->transfer_buffer_length
= spdu
->transfer_buffer_length
;
396 urb
->start_frame
= spdu
->start_frame
;
397 urb
->number_of_packets
= spdu
->number_of_packets
;
398 urb
->interval
= spdu
->interval
;
402 static void usbip_pack_ret_submit(struct usbip_header
*pdu
, struct urb
*urb
,
405 struct usbip_header_ret_submit
*rpdu
= &pdu
->u
.ret_submit
;
408 rpdu
->status
= urb
->status
;
409 rpdu
->actual_length
= urb
->actual_length
;
410 rpdu
->start_frame
= urb
->start_frame
;
411 rpdu
->number_of_packets
= urb
->number_of_packets
;
412 rpdu
->error_count
= urb
->error_count
;
414 urb
->status
= rpdu
->status
;
415 urb
->actual_length
= rpdu
->actual_length
;
416 urb
->start_frame
= rpdu
->start_frame
;
417 urb
->number_of_packets
= rpdu
->number_of_packets
;
418 urb
->error_count
= rpdu
->error_count
;
422 void usbip_pack_pdu(struct usbip_header
*pdu
, struct urb
*urb
, int cmd
,
426 case USBIP_CMD_SUBMIT
:
427 usbip_pack_cmd_submit(pdu
, urb
, pack
);
429 case USBIP_RET_SUBMIT
:
430 usbip_pack_ret_submit(pdu
, urb
, pack
);
434 pr_err("unknown command\n");
438 EXPORT_SYMBOL_GPL(usbip_pack_pdu
);
440 static void correct_endian_basic(struct usbip_header_basic
*base
, int send
)
443 base
->command
= cpu_to_be32(base
->command
);
444 base
->seqnum
= cpu_to_be32(base
->seqnum
);
445 base
->devid
= cpu_to_be32(base
->devid
);
446 base
->direction
= cpu_to_be32(base
->direction
);
447 base
->ep
= cpu_to_be32(base
->ep
);
449 base
->command
= be32_to_cpu(base
->command
);
450 base
->seqnum
= be32_to_cpu(base
->seqnum
);
451 base
->devid
= be32_to_cpu(base
->devid
);
452 base
->direction
= be32_to_cpu(base
->direction
);
453 base
->ep
= be32_to_cpu(base
->ep
);
457 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit
*pdu
,
461 pdu
->transfer_flags
= cpu_to_be32(pdu
->transfer_flags
);
463 cpu_to_be32s(&pdu
->transfer_buffer_length
);
464 cpu_to_be32s(&pdu
->start_frame
);
465 cpu_to_be32s(&pdu
->number_of_packets
);
466 cpu_to_be32s(&pdu
->interval
);
468 pdu
->transfer_flags
= be32_to_cpu(pdu
->transfer_flags
);
470 be32_to_cpus(&pdu
->transfer_buffer_length
);
471 be32_to_cpus(&pdu
->start_frame
);
472 be32_to_cpus(&pdu
->number_of_packets
);
473 be32_to_cpus(&pdu
->interval
);
477 static void correct_endian_ret_submit(struct usbip_header_ret_submit
*pdu
,
481 cpu_to_be32s(&pdu
->status
);
482 cpu_to_be32s(&pdu
->actual_length
);
483 cpu_to_be32s(&pdu
->start_frame
);
484 cpu_to_be32s(&pdu
->number_of_packets
);
485 cpu_to_be32s(&pdu
->error_count
);
487 be32_to_cpus(&pdu
->status
);
488 be32_to_cpus(&pdu
->actual_length
);
489 be32_to_cpus(&pdu
->start_frame
);
490 be32_to_cpus(&pdu
->number_of_packets
);
491 be32_to_cpus(&pdu
->error_count
);
495 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink
*pdu
,
499 pdu
->seqnum
= cpu_to_be32(pdu
->seqnum
);
501 pdu
->seqnum
= be32_to_cpu(pdu
->seqnum
);
504 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink
*pdu
,
508 cpu_to_be32s(&pdu
->status
);
510 be32_to_cpus(&pdu
->status
);
513 void usbip_header_correct_endian(struct usbip_header
*pdu
, int send
)
518 cmd
= pdu
->base
.command
;
520 correct_endian_basic(&pdu
->base
, send
);
523 cmd
= pdu
->base
.command
;
526 case USBIP_CMD_SUBMIT
:
527 correct_endian_cmd_submit(&pdu
->u
.cmd_submit
, send
);
529 case USBIP_RET_SUBMIT
:
530 correct_endian_ret_submit(&pdu
->u
.ret_submit
, send
);
532 case USBIP_CMD_UNLINK
:
533 correct_endian_cmd_unlink(&pdu
->u
.cmd_unlink
, send
);
535 case USBIP_RET_UNLINK
:
536 correct_endian_ret_unlink(&pdu
->u
.ret_unlink
, send
);
540 pr_err("unknown command\n");
544 EXPORT_SYMBOL_GPL(usbip_header_correct_endian
);
546 static void usbip_iso_packet_correct_endian(
547 struct usbip_iso_packet_descriptor
*iso
, int send
)
549 /* does not need all members. but copy all simply. */
551 iso
->offset
= cpu_to_be32(iso
->offset
);
552 iso
->length
= cpu_to_be32(iso
->length
);
553 iso
->status
= cpu_to_be32(iso
->status
);
554 iso
->actual_length
= cpu_to_be32(iso
->actual_length
);
556 iso
->offset
= be32_to_cpu(iso
->offset
);
557 iso
->length
= be32_to_cpu(iso
->length
);
558 iso
->status
= be32_to_cpu(iso
->status
);
559 iso
->actual_length
= be32_to_cpu(iso
->actual_length
);
563 static void usbip_pack_iso(struct usbip_iso_packet_descriptor
*iso
,
564 struct usb_iso_packet_descriptor
*uiso
, int pack
)
567 iso
->offset
= uiso
->offset
;
568 iso
->length
= uiso
->length
;
569 iso
->status
= uiso
->status
;
570 iso
->actual_length
= uiso
->actual_length
;
572 uiso
->offset
= iso
->offset
;
573 uiso
->length
= iso
->length
;
574 uiso
->status
= iso
->status
;
575 uiso
->actual_length
= iso
->actual_length
;
579 /* must free buffer */
580 struct usbip_iso_packet_descriptor
*
581 usbip_alloc_iso_desc_pdu(struct urb
*urb
, ssize_t
*bufflen
)
583 struct usbip_iso_packet_descriptor
*iso
;
584 int np
= urb
->number_of_packets
;
585 ssize_t size
= np
* sizeof(*iso
);
588 iso
= kzalloc(size
, GFP_KERNEL
);
592 for (i
= 0; i
< np
; i
++) {
593 usbip_pack_iso(&iso
[i
], &urb
->iso_frame_desc
[i
], 1);
594 usbip_iso_packet_correct_endian(&iso
[i
], 1);
601 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu
);
603 /* some members of urb must be substituted before. */
604 int usbip_recv_iso(struct usbip_device
*ud
, struct urb
*urb
)
607 struct usbip_iso_packet_descriptor
*iso
;
608 int np
= urb
->number_of_packets
;
609 int size
= np
* sizeof(*iso
);
612 int total_length
= 0;
614 if (!usb_pipeisoc(urb
->pipe
))
617 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
621 buff
= kzalloc(size
, GFP_KERNEL
);
625 ret
= usbip_recv(ud
->tcp_socket
, buff
, size
);
627 dev_err(&urb
->dev
->dev
, "recv iso_frame_descriptor, %d\n",
631 if (ud
->side
== USBIP_STUB
|| ud
->side
== USBIP_VUDC
)
632 usbip_event_add(ud
, SDEV_EVENT_ERROR_TCP
);
634 usbip_event_add(ud
, VDEV_EVENT_ERROR_TCP
);
639 iso
= (struct usbip_iso_packet_descriptor
*) buff
;
640 for (i
= 0; i
< np
; i
++) {
641 usbip_iso_packet_correct_endian(&iso
[i
], 0);
642 usbip_pack_iso(&iso
[i
], &urb
->iso_frame_desc
[i
], 0);
643 total_length
+= urb
->iso_frame_desc
[i
].actual_length
;
648 if (total_length
!= urb
->actual_length
) {
649 dev_err(&urb
->dev
->dev
,
650 "total length of iso packets %d not equal to actual length of buffer %d\n",
651 total_length
, urb
->actual_length
);
653 if (ud
->side
== USBIP_STUB
|| ud
->side
== USBIP_VUDC
)
654 usbip_event_add(ud
, SDEV_EVENT_ERROR_TCP
);
656 usbip_event_add(ud
, VDEV_EVENT_ERROR_TCP
);
663 EXPORT_SYMBOL_GPL(usbip_recv_iso
);
666 * This functions restores the padding which was removed for optimizing
667 * the bandwidth during transfer over tcp/ip
669 * buffer and iso packets need to be stored and be in propeper endian in urb
670 * before calling this function
672 void usbip_pad_iso(struct usbip_device
*ud
, struct urb
*urb
)
674 int np
= urb
->number_of_packets
;
676 int actualoffset
= urb
->actual_length
;
678 if (!usb_pipeisoc(urb
->pipe
))
681 /* if no packets or length of data is 0, then nothing to unpack */
682 if (np
== 0 || urb
->actual_length
== 0)
686 * if actual_length is transfer_buffer_length then no padding is
689 if (urb
->actual_length
== urb
->transfer_buffer_length
)
693 * loop over all packets from last to first (to prevent overwritting
694 * memory when padding) and move them into the proper place
696 for (i
= np
-1; i
> 0; i
--) {
697 actualoffset
-= urb
->iso_frame_desc
[i
].actual_length
;
698 memmove(urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
,
699 urb
->transfer_buffer
+ actualoffset
,
700 urb
->iso_frame_desc
[i
].actual_length
);
703 EXPORT_SYMBOL_GPL(usbip_pad_iso
);
705 /* some members of urb must be substituted before. */
706 int usbip_recv_xbuff(struct usbip_device
*ud
, struct urb
*urb
)
711 if (ud
->side
== USBIP_STUB
|| ud
->side
== USBIP_VUDC
) {
712 /* the direction of urb must be OUT. */
713 if (usb_pipein(urb
->pipe
))
716 size
= urb
->transfer_buffer_length
;
718 /* the direction of urb must be IN. */
719 if (usb_pipeout(urb
->pipe
))
722 size
= urb
->actual_length
;
725 /* no need to recv xbuff */
729 if (size
> urb
->transfer_buffer_length
) {
730 /* should not happen, probably malicious packet */
731 if (ud
->side
== USBIP_STUB
) {
732 usbip_event_add(ud
, SDEV_EVENT_ERROR_TCP
);
735 usbip_event_add(ud
, VDEV_EVENT_ERROR_TCP
);
740 ret
= usbip_recv(ud
->tcp_socket
, urb
->transfer_buffer
, size
);
742 dev_err(&urb
->dev
->dev
, "recv xbuf, %d\n", ret
);
743 if (ud
->side
== USBIP_STUB
|| ud
->side
== USBIP_VUDC
) {
744 usbip_event_add(ud
, SDEV_EVENT_ERROR_TCP
);
746 usbip_event_add(ud
, VDEV_EVENT_ERROR_TCP
);
753 EXPORT_SYMBOL_GPL(usbip_recv_xbuff
);
755 static int __init
usbip_core_init(void)
759 pr_info(DRIVER_DESC
" v" USBIP_VERSION
"\n");
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");
779 MODULE_VERSION(USBIP_VERSION
);