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 "usbip_common.h"
24 static void setup_cmd_submit_pdu(struct usbip_header
*pdup
, struct urb
*urb
)
26 struct vhci_priv
*priv
= ((struct vhci_priv
*)urb
->hcpriv
);
27 struct vhci_device
*vdev
= priv
->vdev
;
29 usbip_dbg_vhci_tx("URB, local devnum %u, remote devid %u\n",
30 usb_pipedevice(urb
->pipe
), vdev
->devid
);
32 pdup
->base
.command
= USBIP_CMD_SUBMIT
;
33 pdup
->base
.seqnum
= priv
->seqnum
;
34 pdup
->base
.devid
= vdev
->devid
;
35 if (usb_pipein(urb
->pipe
))
36 pdup
->base
.direction
= USBIP_DIR_IN
;
38 pdup
->base
.direction
= USBIP_DIR_OUT
;
39 pdup
->base
.ep
= usb_pipeendpoint(urb
->pipe
);
41 usbip_pack_pdu(pdup
, urb
, USBIP_CMD_SUBMIT
, 1);
43 if (urb
->setup_packet
)
44 memcpy(pdup
->u
.cmd_submit
.setup
, urb
->setup_packet
, 8);
47 static struct vhci_priv
*dequeue_from_priv_tx(struct vhci_device
*vdev
)
50 struct vhci_priv
*priv
, *tmp
;
52 spin_lock_irqsave(&vdev
->priv_lock
, flags
);
54 list_for_each_entry_safe(priv
, tmp
, &vdev
->priv_tx
, list
) {
55 list_move_tail(&priv
->list
, &vdev
->priv_rx
);
56 spin_unlock_irqrestore(&vdev
->priv_lock
, flags
);
60 spin_unlock_irqrestore(&vdev
->priv_lock
, flags
);
67 static int vhci_send_cmd_submit(struct vhci_device
*vdev
)
69 struct vhci_priv
*priv
= NULL
;
75 size_t total_size
= 0;
77 while ((priv
= dequeue_from_priv_tx(vdev
)) != NULL
) {
79 struct urb
*urb
= priv
->urb
;
80 struct usbip_header pdu_header
;
81 void *iso_buffer
= NULL
;
84 memset(&pdu_header
, 0, sizeof(pdu_header
));
85 memset(&msg
, 0, sizeof(msg
));
86 memset(&iov
, 0, sizeof(iov
));
88 usbip_dbg_vhci_tx("setup txdata urb %p\n", urb
);
91 /* 1. setup usbip_header */
92 setup_cmd_submit_pdu(&pdu_header
, urb
);
93 usbip_header_correct_endian(&pdu_header
, 1);
95 iov
[0].iov_base
= &pdu_header
;
96 iov
[0].iov_len
= sizeof(pdu_header
);
97 txsize
+= sizeof(pdu_header
);
99 /* 2. setup transfer buffer */
100 if (!usb_pipein(urb
->pipe
) && urb
->transfer_buffer_length
> 0) {
101 iov
[1].iov_base
= urb
->transfer_buffer
;
102 iov
[1].iov_len
= urb
->transfer_buffer_length
;
103 txsize
+= urb
->transfer_buffer_length
;
106 /* 3. setup iso_packet_descriptor */
107 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
) {
110 iso_buffer
= usbip_alloc_iso_desc_pdu(urb
, &len
);
112 usbip_event_add(&vdev
->ud
,
113 SDEV_EVENT_ERROR_MALLOC
);
117 iov
[2].iov_base
= iso_buffer
;
118 iov
[2].iov_len
= len
;
122 ret
= kernel_sendmsg(vdev
->ud
.tcp_socket
, &msg
, iov
, 3, txsize
);
124 usbip_uerr("sendmsg failed!, retval %d for %zd\n", ret
,
127 usbip_event_add(&vdev
->ud
, VDEV_EVENT_ERROR_TCP
);
132 usbip_dbg_vhci_tx("send txdata\n");
134 total_size
+= txsize
;
141 /*-------------------------------------------------------------------------*/
143 static struct vhci_unlink
*dequeue_from_unlink_tx(struct vhci_device
*vdev
)
146 struct vhci_unlink
*unlink
, *tmp
;
148 spin_lock_irqsave(&vdev
->priv_lock
, flags
);
150 list_for_each_entry_safe(unlink
, tmp
, &vdev
->unlink_tx
, list
) {
151 list_move_tail(&unlink
->list
, &vdev
->unlink_rx
);
152 spin_unlock_irqrestore(&vdev
->priv_lock
, flags
);
156 spin_unlock_irqrestore(&vdev
->priv_lock
, flags
);
161 static int vhci_send_cmd_unlink(struct vhci_device
*vdev
)
163 struct vhci_unlink
*unlink
= NULL
;
169 size_t total_size
= 0;
171 while ((unlink
= dequeue_from_unlink_tx(vdev
)) != NULL
) {
173 struct usbip_header pdu_header
;
176 memset(&pdu_header
, 0, sizeof(pdu_header
));
177 memset(&msg
, 0, sizeof(msg
));
178 memset(&iov
, 0, sizeof(iov
));
180 usbip_dbg_vhci_tx("setup cmd unlink, %lu \n", unlink
->seqnum
);
183 /* 1. setup usbip_header */
184 pdu_header
.base
.command
= USBIP_CMD_UNLINK
;
185 pdu_header
.base
.seqnum
= unlink
->seqnum
;
186 pdu_header
.base
.devid
= vdev
->devid
;
187 pdu_header
.base
.ep
= 0;
188 pdu_header
.u
.cmd_unlink
.seqnum
= unlink
->unlink_seqnum
;
190 usbip_header_correct_endian(&pdu_header
, 1);
192 iov
[0].iov_base
= &pdu_header
;
193 iov
[0].iov_len
= sizeof(pdu_header
);
194 txsize
+= sizeof(pdu_header
);
196 ret
= kernel_sendmsg(vdev
->ud
.tcp_socket
, &msg
, iov
, 1, txsize
);
198 usbip_uerr("sendmsg failed!, retval %d for %zd\n", ret
,
200 usbip_event_add(&vdev
->ud
, VDEV_EVENT_ERROR_TCP
);
205 usbip_dbg_vhci_tx("send txdata\n");
207 total_size
+= txsize
;
214 /*-------------------------------------------------------------------------*/
216 void vhci_tx_loop(struct usbip_task
*ut
)
218 struct usbip_device
*ud
= container_of(ut
, struct usbip_device
, tcp_tx
);
219 struct vhci_device
*vdev
= container_of(ud
, struct vhci_device
, ud
);
222 if (signal_pending(current
)) {
223 usbip_uinfo("vhci_tx signal catched\n");
227 if (vhci_send_cmd_submit(vdev
) < 0)
230 if (vhci_send_cmd_unlink(vdev
) < 0)
233 wait_event_interruptible(vdev
->waitq_tx
,
234 (!list_empty(&vdev
->priv_tx
) ||
235 !list_empty(&vdev
->unlink_tx
)));
237 usbip_dbg_vhci_tx("pending urbs ?, now wake up\n");