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/smp_lock.h>
22 #include <linux/file.h>
23 #include <linux/tcp.h>
25 #include <linux/kthread.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_DEBUG
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
)
60 sscanf(buf
, "%lx", &flag
);
61 usbip_debug_flag
= flag
;
65 DEVICE_ATTR(usbip_debug
, (S_IRUGO
| S_IWUSR
), show_flag
, store_flag
);
67 static void usbip_dump_buffer(char *buff
, int bufflen
)
72 for (i
= 0; i
< 128; i
++) {
74 printk(KERN_DEBUG
" ");
75 printk(KERN_DEBUG
"%02x ", (unsigned char) buff
[i
]);
77 printk(KERN_DEBUG
"| ");
79 printk(KERN_DEBUG
"\n");
81 printk(KERN_DEBUG
"... (%d byte)\n", bufflen
);
85 for (i
= 0; i
< bufflen
; i
++) {
87 printk(KERN_DEBUG
" ");
88 printk(KERN_DEBUG
"%02x ", (unsigned char) buff
[i
]);
90 printk(KERN_DEBUG
"| ");
92 printk(KERN_DEBUG
"\n");
94 printk(KERN_DEBUG
"\n");
98 static void usbip_dump_pipe(unsigned int p
)
100 unsigned char type
= usb_pipetype(p
);
101 unsigned char ep
= usb_pipeendpoint(p
);
102 unsigned char dev
= usb_pipedevice(p
);
103 unsigned char dir
= usb_pipein(p
);
105 printk(KERN_DEBUG
"dev(%d) ", dev
);
106 printk(KERN_DEBUG
"ep(%d) ", ep
);
107 printk(KERN_DEBUG
"%s ", dir
? "IN" : "OUT");
110 case PIPE_ISOCHRONOUS
:
111 printk(KERN_DEBUG
"%s ", "ISO");
114 printk(KERN_DEBUG
"%s ", "INT");
117 printk(KERN_DEBUG
"%s ", "CTL");
120 printk(KERN_DEBUG
"%s ", "BLK");
123 printk(KERN_DEBUG
"ERR");
126 printk(KERN_DEBUG
"\n");
130 static void usbip_dump_usb_device(struct usb_device
*udev
)
132 struct device
*dev
= &udev
->dev
;
135 dev_dbg(dev
, " devnum(%d) devpath(%s)",
136 udev
->devnum
, udev
->devpath
);
138 switch (udev
->speed
) {
140 printk(KERN_DEBUG
" SPD_HIGH");
143 printk(KERN_DEBUG
" SPD_FULL");
146 printk(KERN_DEBUG
" SPD_LOW");
148 case USB_SPEED_UNKNOWN
:
149 printk(KERN_DEBUG
" SPD_UNKNOWN");
152 printk(KERN_DEBUG
" SPD_ERROR");
155 printk(KERN_DEBUG
" tt %p, ttport %d", udev
->tt
, udev
->ttport
);
156 printk(KERN_DEBUG
"\n");
159 for (i
= 0; i
< 16; i
++)
160 printk(KERN_DEBUG
" %2u", i
);
161 printk(KERN_DEBUG
"\n");
163 dev_dbg(dev
, " toggle0(IN) :");
164 for (i
= 0; i
< 16; i
++)
165 printk(KERN_DEBUG
" %2u", (udev
->toggle
[0] & (1 << i
)) ? 1 : 0);
166 printk(KERN_DEBUG
"\n");
168 dev_dbg(dev
, " toggle1(OUT):");
169 for (i
= 0; i
< 16; i
++)
170 printk(KERN_DEBUG
" %2u", (udev
->toggle
[1] & (1 << i
)) ? 1 : 0);
171 printk(KERN_DEBUG
"\n");
174 dev_dbg(dev
, " epmaxp_in :");
175 for (i
= 0; i
< 16; i
++) {
177 printk(KERN_DEBUG
" %2u",
178 le16_to_cpu(udev
->ep_in
[i
]->desc
.wMaxPacketSize
));
180 printk(KERN_DEBUG
"\n");
182 dev_dbg(dev
, " epmaxp_out :");
183 for (i
= 0; i
< 16; i
++) {
185 printk(KERN_DEBUG
" %2u",
186 le16_to_cpu(udev
->ep_out
[i
]->desc
.wMaxPacketSize
));
188 printk(KERN_DEBUG
"\n");
190 dev_dbg(dev
, "parent %p, bus %p\n", udev
->parent
, udev
->bus
);
192 dev_dbg(dev
, "descriptor %p, config %p, actconfig %p, "
193 "rawdescriptors %p\n", &udev
->descriptor
, udev
->config
,
194 udev
->actconfig
, udev
->rawdescriptors
);
196 dev_dbg(dev
, "have_langid %d, string_langid %d\n",
197 udev
->have_langid
, udev
->string_langid
);
199 dev_dbg(dev
, "maxchild %d, children %p\n",
200 udev
->maxchild
, udev
->children
);
203 static void usbip_dump_request_type(__u8 rt
)
205 switch (rt
& USB_RECIP_MASK
) {
206 case USB_RECIP_DEVICE
:
207 printk(KERN_DEBUG
"DEVICE");
209 case USB_RECIP_INTERFACE
:
210 printk(KERN_DEBUG
"INTERF");
212 case USB_RECIP_ENDPOINT
:
213 printk(KERN_DEBUG
"ENDPOI");
215 case USB_RECIP_OTHER
:
216 printk(KERN_DEBUG
"OTHER ");
219 printk(KERN_DEBUG
"------");
223 static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest
*cmd
)
226 printk(KERN_DEBUG
" %s : null pointer\n", __func__
);
230 printk(KERN_DEBUG
" ");
231 printk(KERN_DEBUG
"bRequestType(%02X) ", cmd
->bRequestType
);
232 printk(KERN_DEBUG
"bRequest(%02X) " , cmd
->bRequest
);
233 printk(KERN_DEBUG
"wValue(%04X) ", cmd
->wValue
);
234 printk(KERN_DEBUG
"wIndex(%04X) ", cmd
->wIndex
);
235 printk(KERN_DEBUG
"wLength(%04X) ", cmd
->wLength
);
237 printk(KERN_DEBUG
"\n ");
239 if ((cmd
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
240 printk(KERN_DEBUG
"STANDARD ");
241 switch (cmd
->bRequest
) {
242 case USB_REQ_GET_STATUS
:
243 printk(KERN_DEBUG
"GET_STATUS");
245 case USB_REQ_CLEAR_FEATURE
:
246 printk(KERN_DEBUG
"CLEAR_FEAT");
248 case USB_REQ_SET_FEATURE
:
249 printk(KERN_DEBUG
"SET_FEAT ");
251 case USB_REQ_SET_ADDRESS
:
252 printk(KERN_DEBUG
"SET_ADDRRS");
254 case USB_REQ_GET_DESCRIPTOR
:
255 printk(KERN_DEBUG
"GET_DESCRI");
257 case USB_REQ_SET_DESCRIPTOR
:
258 printk(KERN_DEBUG
"SET_DESCRI");
260 case USB_REQ_GET_CONFIGURATION
:
261 printk(KERN_DEBUG
"GET_CONFIG");
263 case USB_REQ_SET_CONFIGURATION
:
264 printk(KERN_DEBUG
"SET_CONFIG");
266 case USB_REQ_GET_INTERFACE
:
267 printk(KERN_DEBUG
"GET_INTERF");
269 case USB_REQ_SET_INTERFACE
:
270 printk(KERN_DEBUG
"SET_INTERF");
272 case USB_REQ_SYNCH_FRAME
:
273 printk(KERN_DEBUG
"SYNC_FRAME");
276 printk(KERN_DEBUG
"REQ(%02X) ", cmd
->bRequest
);
279 printk(KERN_DEBUG
" ");
280 usbip_dump_request_type(cmd
->bRequestType
);
282 } else if ((cmd
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_CLASS
)
283 printk(KERN_DEBUG
"CLASS ");
285 else if ((cmd
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_VENDOR
)
286 printk(KERN_DEBUG
"VENDOR ");
288 else if ((cmd
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_RESERVED
)
289 printk(KERN_DEBUG
"RESERVED");
291 printk(KERN_DEBUG
"\n");
294 void usbip_dump_urb(struct urb
*urb
)
299 printk(KERN_DEBUG KBUILD_MODNAME
300 ":%s: urb: null pointer!!\n", __func__
);
305 printk(KERN_DEBUG KBUILD_MODNAME
306 ":%s: urb->dev: null pointer!!\n", __func__
);
309 dev
= &urb
->dev
->dev
;
311 dev_dbg(dev
, " urb :%p\n", urb
);
312 dev_dbg(dev
, " dev :%p\n", urb
->dev
);
314 usbip_dump_usb_device(urb
->dev
);
316 dev_dbg(dev
, " pipe :%08x ", urb
->pipe
);
318 usbip_dump_pipe(urb
->pipe
);
320 dev_dbg(dev
, " status :%d\n", urb
->status
);
321 dev_dbg(dev
, " transfer_flags :%08X\n", urb
->transfer_flags
);
322 dev_dbg(dev
, " transfer_buffer :%p\n", urb
->transfer_buffer
);
323 dev_dbg(dev
, " transfer_buffer_length:%d\n",
324 urb
->transfer_buffer_length
);
325 dev_dbg(dev
, " actual_length :%d\n", urb
->actual_length
);
326 dev_dbg(dev
, " setup_packet :%p\n", urb
->setup_packet
);
328 if (urb
->setup_packet
&& usb_pipetype(urb
->pipe
) == PIPE_CONTROL
)
329 usbip_dump_usb_ctrlrequest(
330 (struct usb_ctrlrequest
*)urb
->setup_packet
);
332 dev_dbg(dev
, " start_frame :%d\n", urb
->start_frame
);
333 dev_dbg(dev
, " number_of_packets :%d\n", urb
->number_of_packets
);
334 dev_dbg(dev
, " interval :%d\n", urb
->interval
);
335 dev_dbg(dev
, " error_count :%d\n", urb
->error_count
);
336 dev_dbg(dev
, " context :%p\n", urb
->context
);
337 dev_dbg(dev
, " complete :%p\n", urb
->complete
);
339 EXPORT_SYMBOL_GPL(usbip_dump_urb
);
341 void usbip_dump_header(struct usbip_header
*pdu
)
343 usbip_udbg("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
350 switch (pdu
->base
.command
) {
351 case USBIP_CMD_SUBMIT
:
352 usbip_udbg("CMD_SUBMIT: "
353 "x_flags %u x_len %u sf %u #p %u iv %u\n",
354 pdu
->u
.cmd_submit
.transfer_flags
,
355 pdu
->u
.cmd_submit
.transfer_buffer_length
,
356 pdu
->u
.cmd_submit
.start_frame
,
357 pdu
->u
.cmd_submit
.number_of_packets
,
358 pdu
->u
.cmd_submit
.interval
);
360 case USBIP_CMD_UNLINK
:
361 usbip_udbg("CMD_UNLINK: seq %u\n", pdu
->u
.cmd_unlink
.seqnum
);
363 case USBIP_RET_SUBMIT
:
364 usbip_udbg("RET_SUBMIT: st %d al %u sf %d ec %d\n",
365 pdu
->u
.ret_submit
.status
,
366 pdu
->u
.ret_submit
.actual_length
,
367 pdu
->u
.ret_submit
.start_frame
,
368 pdu
->u
.ret_submit
.error_count
);
369 case USBIP_RET_UNLINK
:
370 usbip_udbg("RET_UNLINK: status %d\n", pdu
->u
.ret_unlink
.status
);
374 usbip_udbg("UNKNOWN\n");
377 EXPORT_SYMBOL_GPL(usbip_dump_header
);
380 /*-------------------------------------------------------------------------*/
381 /* thread routines */
383 int usbip_thread(void *param
)
385 struct usbip_task
*ut
= param
;
392 allow_signal(SIGKILL
);
393 ut
->thread
= current
;
396 /* srv.rb must wait for rx_thread starting */
397 complete(&ut
->thread_done
);
399 /* start of while loop */
405 complete_and_exit(&ut
->thread_done
, 0);
408 int usbip_start_threads(struct usbip_device
*ud
)
411 * threads are invoked per one device (per one connection).
413 struct task_struct
*th
;
415 th
= kthread_run(usbip_thread
, (void *)&ud
->tcp_rx
, "usbip");
418 "Unable to start control thread\n");
421 th
= kthread_run(usbip_thread
, (void *)&ud
->tcp_tx
, "usbip");
424 "Unable to start control thread\n");
428 /* confirm threads are starting */
429 wait_for_completion(&ud
->tcp_rx
.thread_done
);
430 wait_for_completion(&ud
->tcp_tx
.thread_done
);
433 EXPORT_SYMBOL_GPL(usbip_start_threads
);
435 void usbip_stop_threads(struct usbip_device
*ud
)
437 /* kill threads related to this sdev, if v.c. exists */
438 if (ud
->tcp_rx
.thread
!= NULL
) {
439 send_sig(SIGKILL
, ud
->tcp_rx
.thread
, 1);
440 wait_for_completion(&ud
->tcp_rx
.thread_done
);
441 usbip_udbg("rx_thread for ud %p has finished\n", ud
);
444 if (ud
->tcp_tx
.thread
!= NULL
) {
445 send_sig(SIGKILL
, ud
->tcp_tx
.thread
, 1);
446 wait_for_completion(&ud
->tcp_tx
.thread_done
);
447 usbip_udbg("tx_thread for ud %p has finished\n", ud
);
450 EXPORT_SYMBOL_GPL(usbip_stop_threads
);
452 void usbip_task_init(struct usbip_task
*ut
, char *name
,
453 void (*loop_ops
)(struct usbip_task
*))
456 init_completion(&ut
->thread_done
);
458 ut
->loop_ops
= loop_ops
;
460 EXPORT_SYMBOL_GPL(usbip_task_init
);
463 /*-------------------------------------------------------------------------*/
464 /* socket routines */
466 /* Send/receive messages over TCP/IP. I refer drivers/block/nbd.c */
467 int usbip_xmit(int send
, struct socket
*sock
, char *buf
,
468 int size
, int msg_flags
)
475 /* for blocks of if (usbip_dbg_flag_xmit) */
479 usbip_dbg_xmit("enter\n");
481 if (!sock
|| !buf
|| !size
) {
482 printk(KERN_ERR
"%s: invalid arg, sock %p buff %p size %d\n",
483 __func__
, sock
, buf
, size
);
488 if (usbip_dbg_flag_xmit
) {
491 printk(KERN_DEBUG
"%-10s:", current
->comm
);
493 printk(KERN_DEBUG
"interrupt :");
495 printk(KERN_DEBUG
"%s: sending... , sock %p, buf %p, "
496 "size %d, msg_flags %d\n", __func__
,
497 sock
, buf
, size
, msg_flags
);
498 usbip_dump_buffer(buf
, size
);
504 sock
->sk
->sk_allocation
= GFP_NOIO
;
509 msg
.msg_control
= NULL
;
510 msg
.msg_controllen
= 0;
512 msg
.msg_flags
= msg_flags
| MSG_NOSIGNAL
;
515 result
= kernel_sendmsg(sock
, &msg
, &iov
, 1, size
);
517 result
= kernel_recvmsg(sock
, &msg
, &iov
, 1, size
,
521 usbip_udbg("usbip_xmit: %s sock %p buf %p size %u ret "
523 send
? "send" : "receive", sock
, buf
,
524 size
, result
, total
);
535 if (usbip_dbg_flag_xmit
) {
538 printk(KERN_DEBUG
"%-10s:", current
->comm
);
540 printk(KERN_DEBUG
"interrupt :");
542 printk(KERN_DEBUG
"usbip_xmit: receiving....\n");
543 usbip_dump_buffer(bp
, osize
);
544 printk(KERN_DEBUG
"usbip_xmit: received, osize %d ret "
545 "%d size %d total %d\n", osize
, result
,
550 printk(KERN_DEBUG
"usbip_xmit: send, total %d\n",
559 EXPORT_SYMBOL_GPL(usbip_xmit
);
562 /* now a usrland utility should set options. */
564 int setquickack(struct socket
*socket
)
572 ret
= socket
->ops
->setsockopt(socket
, SOL_TCP
, TCP_QUICKACK
,
573 (char __user
*) &val
, sizeof(ret
));
579 int setnodelay(struct socket
*socket
)
587 ret
= socket
->ops
->setsockopt(socket
, SOL_TCP
, TCP_NODELAY
,
588 (char __user
*) &val
, sizeof(ret
));
594 int setkeepalive(struct socket
*socket
)
602 ret
= socket
->ops
->setsockopt(socket
, SOL_SOCKET
, SO_KEEPALIVE
,
603 (char __user
*) &val
, sizeof(ret
));
609 void setreuse(struct socket
*socket
)
611 socket
->sk
->sk_reuse
= 1;
615 struct socket
*sockfd_to_socket(unsigned int sockfd
)
617 struct socket
*socket
;
623 printk(KERN_ERR
"%s: invalid sockfd\n", __func__
);
627 inode
= file
->f_dentry
->d_inode
;
629 if (!inode
|| !S_ISSOCK(inode
->i_mode
))
632 socket
= SOCKET_I(inode
);
636 EXPORT_SYMBOL_GPL(sockfd_to_socket
);
640 /*-------------------------------------------------------------------------*/
643 /* there may be more cases to tweak the flags. */
644 static unsigned int tweak_transfer_flags(unsigned int flags
)
646 flags
&= ~(URB_NO_TRANSFER_DMA_MAP
|URB_NO_SETUP_DMA_MAP
);
650 static void usbip_pack_cmd_submit(struct usbip_header
*pdu
, struct urb
*urb
,
653 struct usbip_header_cmd_submit
*spdu
= &pdu
->u
.cmd_submit
;
656 * Some members are not still implemented in usbip. I hope this issue
657 * will be discussed when usbip is ported to other operating systems.
661 spdu
->transfer_flags
=
662 tweak_transfer_flags(urb
->transfer_flags
);
663 spdu
->transfer_buffer_length
= urb
->transfer_buffer_length
;
664 spdu
->start_frame
= urb
->start_frame
;
665 spdu
->number_of_packets
= urb
->number_of_packets
;
666 spdu
->interval
= urb
->interval
;
669 urb
->transfer_flags
= spdu
->transfer_flags
;
671 urb
->transfer_buffer_length
= spdu
->transfer_buffer_length
;
672 urb
->start_frame
= spdu
->start_frame
;
673 urb
->number_of_packets
= spdu
->number_of_packets
;
674 urb
->interval
= spdu
->interval
;
678 static void usbip_pack_ret_submit(struct usbip_header
*pdu
, struct urb
*urb
,
681 struct usbip_header_ret_submit
*rpdu
= &pdu
->u
.ret_submit
;
686 rpdu
->status
= urb
->status
;
687 rpdu
->actual_length
= urb
->actual_length
;
688 rpdu
->start_frame
= urb
->start_frame
;
689 rpdu
->error_count
= urb
->error_count
;
693 urb
->status
= rpdu
->status
;
694 urb
->actual_length
= rpdu
->actual_length
;
695 urb
->start_frame
= rpdu
->start_frame
;
696 urb
->error_count
= rpdu
->error_count
;
701 void usbip_pack_pdu(struct usbip_header
*pdu
, struct urb
*urb
, int cmd
,
705 case USBIP_CMD_SUBMIT
:
706 usbip_pack_cmd_submit(pdu
, urb
, pack
);
708 case USBIP_RET_SUBMIT
:
709 usbip_pack_ret_submit(pdu
, urb
, pack
);
712 err("unknown command");
717 EXPORT_SYMBOL_GPL(usbip_pack_pdu
);
720 static void correct_endian_basic(struct usbip_header_basic
*base
, int send
)
723 base
->command
= cpu_to_be32(base
->command
);
724 base
->seqnum
= cpu_to_be32(base
->seqnum
);
725 base
->devid
= cpu_to_be32(base
->devid
);
726 base
->direction
= cpu_to_be32(base
->direction
);
727 base
->ep
= cpu_to_be32(base
->ep
);
729 base
->command
= be32_to_cpu(base
->command
);
730 base
->seqnum
= be32_to_cpu(base
->seqnum
);
731 base
->devid
= be32_to_cpu(base
->devid
);
732 base
->direction
= be32_to_cpu(base
->direction
);
733 base
->ep
= be32_to_cpu(base
->ep
);
737 static void correct_endian_cmd_submit(struct usbip_header_cmd_submit
*pdu
,
741 pdu
->transfer_flags
= cpu_to_be32(pdu
->transfer_flags
);
743 cpu_to_be32s(&pdu
->transfer_buffer_length
);
744 cpu_to_be32s(&pdu
->start_frame
);
745 cpu_to_be32s(&pdu
->number_of_packets
);
746 cpu_to_be32s(&pdu
->interval
);
748 pdu
->transfer_flags
= be32_to_cpu(pdu
->transfer_flags
);
750 be32_to_cpus(&pdu
->transfer_buffer_length
);
751 be32_to_cpus(&pdu
->start_frame
);
752 be32_to_cpus(&pdu
->number_of_packets
);
753 be32_to_cpus(&pdu
->interval
);
757 static void correct_endian_ret_submit(struct usbip_header_ret_submit
*pdu
,
761 cpu_to_be32s(&pdu
->status
);
762 cpu_to_be32s(&pdu
->actual_length
);
763 cpu_to_be32s(&pdu
->start_frame
);
764 cpu_to_be32s(&pdu
->error_count
);
766 be32_to_cpus(&pdu
->status
);
767 be32_to_cpus(&pdu
->actual_length
);
768 be32_to_cpus(&pdu
->start_frame
);
769 be32_to_cpus(&pdu
->error_count
);
773 static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink
*pdu
,
777 pdu
->seqnum
= cpu_to_be32(pdu
->seqnum
);
779 pdu
->seqnum
= be32_to_cpu(pdu
->seqnum
);
782 static void correct_endian_ret_unlink(struct usbip_header_ret_unlink
*pdu
,
786 cpu_to_be32s(&pdu
->status
);
788 be32_to_cpus(&pdu
->status
);
791 void usbip_header_correct_endian(struct usbip_header
*pdu
, int send
)
796 cmd
= pdu
->base
.command
;
798 correct_endian_basic(&pdu
->base
, send
);
801 cmd
= pdu
->base
.command
;
804 case USBIP_CMD_SUBMIT
:
805 correct_endian_cmd_submit(&pdu
->u
.cmd_submit
, send
);
807 case USBIP_RET_SUBMIT
:
808 correct_endian_ret_submit(&pdu
->u
.ret_submit
, send
);
810 case USBIP_CMD_UNLINK
:
811 correct_endian_cmd_unlink(&pdu
->u
.cmd_unlink
, send
);
813 case USBIP_RET_UNLINK
:
814 correct_endian_ret_unlink(&pdu
->u
.ret_unlink
, send
);
818 err("unknown command in pdu header: %d", cmd
);
822 EXPORT_SYMBOL_GPL(usbip_header_correct_endian
);
824 static void usbip_iso_pakcet_correct_endian(
825 struct usbip_iso_packet_descriptor
*iso
,
828 /* does not need all members. but copy all simply. */
830 iso
->offset
= cpu_to_be32(iso
->offset
);
831 iso
->length
= cpu_to_be32(iso
->length
);
832 iso
->status
= cpu_to_be32(iso
->status
);
833 iso
->actual_length
= cpu_to_be32(iso
->actual_length
);
835 iso
->offset
= be32_to_cpu(iso
->offset
);
836 iso
->length
= be32_to_cpu(iso
->length
);
837 iso
->status
= be32_to_cpu(iso
->status
);
838 iso
->actual_length
= be32_to_cpu(iso
->actual_length
);
842 static void usbip_pack_iso(struct usbip_iso_packet_descriptor
*iso
,
843 struct usb_iso_packet_descriptor
*uiso
, int pack
)
846 iso
->offset
= uiso
->offset
;
847 iso
->length
= uiso
->length
;
848 iso
->status
= uiso
->status
;
849 iso
->actual_length
= uiso
->actual_length
;
851 uiso
->offset
= iso
->offset
;
852 uiso
->length
= iso
->length
;
853 uiso
->status
= iso
->status
;
854 uiso
->actual_length
= iso
->actual_length
;
859 /* must free buffer */
860 void *usbip_alloc_iso_desc_pdu(struct urb
*urb
, ssize_t
*bufflen
)
863 struct usbip_iso_packet_descriptor
*iso
;
864 int np
= urb
->number_of_packets
;
865 ssize_t size
= np
* sizeof(*iso
);
868 buff
= kzalloc(size
, GFP_KERNEL
);
872 for (i
= 0; i
< np
; i
++) {
873 iso
= buff
+ (i
* sizeof(*iso
));
875 usbip_pack_iso(iso
, &urb
->iso_frame_desc
[i
], 1);
876 usbip_iso_pakcet_correct_endian(iso
, 1);
883 EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu
);
885 /* some members of urb must be substituted before. */
886 int usbip_recv_iso(struct usbip_device
*ud
, struct urb
*urb
)
889 struct usbip_iso_packet_descriptor
*iso
;
890 int np
= urb
->number_of_packets
;
891 int size
= np
* sizeof(*iso
);
895 if (!usb_pipeisoc(urb
->pipe
))
898 /* my Bluetooth dongle gets ISO URBs which are np = 0 */
900 /* usbip_uinfo("iso np == 0\n"); */
901 /* usbip_dump_urb(urb); */
905 buff
= kzalloc(size
, GFP_KERNEL
);
909 ret
= usbip_xmit(0, ud
->tcp_socket
, buff
, size
, 0);
911 dev_err(&urb
->dev
->dev
, "recv iso_frame_descriptor, %d\n",
915 if (ud
->side
== USBIP_STUB
)
916 usbip_event_add(ud
, SDEV_EVENT_ERROR_TCP
);
918 usbip_event_add(ud
, VDEV_EVENT_ERROR_TCP
);
923 for (i
= 0; i
< np
; i
++) {
924 iso
= buff
+ (i
* sizeof(*iso
));
926 usbip_iso_pakcet_correct_endian(iso
, 0);
927 usbip_pack_iso(iso
, &urb
->iso_frame_desc
[i
], 0);
934 EXPORT_SYMBOL_GPL(usbip_recv_iso
);
937 /* some members of urb must be substituted before. */
938 int usbip_recv_xbuff(struct usbip_device
*ud
, struct urb
*urb
)
943 if (ud
->side
== USBIP_STUB
) {
945 /* the direction of urb must be OUT. */
946 if (usb_pipein(urb
->pipe
))
949 size
= urb
->transfer_buffer_length
;
952 /* the direction of urb must be IN. */
953 if (usb_pipeout(urb
->pipe
))
956 size
= urb
->actual_length
;
959 /* no need to recv xbuff */
963 ret
= usbip_xmit(0, ud
->tcp_socket
, (char *)urb
->transfer_buffer
,
966 dev_err(&urb
->dev
->dev
, "recv xbuf, %d\n", ret
);
967 if (ud
->side
== USBIP_STUB
) {
968 usbip_event_add(ud
, SDEV_EVENT_ERROR_TCP
);
970 usbip_event_add(ud
, VDEV_EVENT_ERROR_TCP
);
977 EXPORT_SYMBOL_GPL(usbip_recv_xbuff
);
980 /*-------------------------------------------------------------------------*/
982 static int __init
usbip_common_init(void)
984 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_DESC
"" DRIVER_VERSION
);
989 static void __exit
usbip_common_exit(void)
997 module_init(usbip_common_init
);
998 module_exit(usbip_common_exit
);
1000 MODULE_AUTHOR(DRIVER_AUTHOR
);
1001 MODULE_DESCRIPTION(DRIVER_DESC
);
1002 MODULE_LICENSE("GPL");