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/kthread.h>
21 #include <linux/slab.h>
23 #include "usbip_common.h"
26 static void setup_cmd_submit_pdu(struct usbip_header
*pdup
, struct urb
*urb
)
28 struct vhci_priv
*priv
= ((struct vhci_priv
*)urb
->hcpriv
);
29 struct vhci_device
*vdev
= priv
->vdev
;
31 usbip_dbg_vhci_tx("URB, local devnum %u, remote devid %u\n",
32 usb_pipedevice(urb
->pipe
), vdev
->devid
);
34 pdup
->base
.command
= USBIP_CMD_SUBMIT
;
35 pdup
->base
.seqnum
= priv
->seqnum
;
36 pdup
->base
.devid
= vdev
->devid
;
37 pdup
->base
.direction
= usb_pipein(urb
->pipe
) ?
38 USBIP_DIR_IN
: 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
)
49 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
);
65 static int vhci_send_cmd_submit(struct vhci_device
*vdev
)
67 struct vhci_priv
*priv
= NULL
;
73 size_t total_size
= 0;
75 while ((priv
= dequeue_from_priv_tx(vdev
)) != NULL
) {
77 struct urb
*urb
= priv
->urb
;
78 struct usbip_header pdu_header
;
79 struct usbip_iso_packet_descriptor
*iso_buffer
= NULL
;
82 memset(&pdu_header
, 0, sizeof(pdu_header
));
83 memset(&msg
, 0, sizeof(msg
));
84 memset(&iov
, 0, sizeof(iov
));
86 usbip_dbg_vhci_tx("setup txdata urb seqnum %lu\n",
89 /* 1. setup usbip_header */
90 setup_cmd_submit_pdu(&pdu_header
, urb
);
91 usbip_header_correct_endian(&pdu_header
, 1);
93 iov
[0].iov_base
= &pdu_header
;
94 iov
[0].iov_len
= sizeof(pdu_header
);
95 txsize
+= sizeof(pdu_header
);
97 /* 2. setup transfer buffer */
98 if (!usb_pipein(urb
->pipe
) && urb
->transfer_buffer_length
> 0) {
99 iov
[1].iov_base
= urb
->transfer_buffer
;
100 iov
[1].iov_len
= urb
->transfer_buffer_length
;
101 txsize
+= urb
->transfer_buffer_length
;
104 /* 3. setup iso_packet_descriptor */
105 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
) {
108 iso_buffer
= usbip_alloc_iso_desc_pdu(urb
, &len
);
110 usbip_event_add(&vdev
->ud
,
111 SDEV_EVENT_ERROR_MALLOC
);
115 iov
[2].iov_base
= iso_buffer
;
116 iov
[2].iov_len
= len
;
120 ret
= kernel_sendmsg(vdev
->ud
.tcp_socket
, &msg
, iov
, 3, txsize
);
122 pr_err("sendmsg failed!, ret=%d for %zd\n", ret
,
125 usbip_event_add(&vdev
->ud
, VDEV_EVENT_ERROR_TCP
);
130 usbip_dbg_vhci_tx("send txdata\n");
132 total_size
+= txsize
;
138 static struct vhci_unlink
*dequeue_from_unlink_tx(struct vhci_device
*vdev
)
140 struct vhci_unlink
*unlink
, *tmp
;
143 spin_lock_irqsave(&vdev
->priv_lock
, flags
);
145 list_for_each_entry_safe(unlink
, tmp
, &vdev
->unlink_tx
, list
) {
146 list_move_tail(&unlink
->list
, &vdev
->unlink_rx
);
147 spin_unlock_irqrestore(&vdev
->priv_lock
, flags
);
151 spin_unlock_irqrestore(&vdev
->priv_lock
, flags
);
156 static int vhci_send_cmd_unlink(struct vhci_device
*vdev
)
158 struct vhci_unlink
*unlink
= NULL
;
164 size_t total_size
= 0;
166 while ((unlink
= dequeue_from_unlink_tx(vdev
)) != NULL
) {
168 struct usbip_header pdu_header
;
171 memset(&pdu_header
, 0, sizeof(pdu_header
));
172 memset(&msg
, 0, sizeof(msg
));
173 memset(&iov
, 0, sizeof(iov
));
175 usbip_dbg_vhci_tx("setup cmd unlink, %lu\n", unlink
->seqnum
);
177 /* 1. setup usbip_header */
178 pdu_header
.base
.command
= USBIP_CMD_UNLINK
;
179 pdu_header
.base
.seqnum
= unlink
->seqnum
;
180 pdu_header
.base
.devid
= vdev
->devid
;
181 pdu_header
.base
.ep
= 0;
182 pdu_header
.u
.cmd_unlink
.seqnum
= unlink
->unlink_seqnum
;
184 usbip_header_correct_endian(&pdu_header
, 1);
186 iov
[0].iov_base
= &pdu_header
;
187 iov
[0].iov_len
= sizeof(pdu_header
);
188 txsize
+= sizeof(pdu_header
);
190 ret
= kernel_sendmsg(vdev
->ud
.tcp_socket
, &msg
, iov
, 1, txsize
);
192 pr_err("sendmsg failed!, ret=%d for %zd\n", ret
,
194 usbip_event_add(&vdev
->ud
, VDEV_EVENT_ERROR_TCP
);
198 usbip_dbg_vhci_tx("send txdata\n");
200 total_size
+= txsize
;
206 int vhci_tx_loop(void *data
)
208 struct usbip_device
*ud
= data
;
209 struct vhci_device
*vdev
= container_of(ud
, struct vhci_device
, ud
);
211 while (!kthread_should_stop()) {
212 if (vhci_send_cmd_submit(vdev
) < 0)
215 if (vhci_send_cmd_unlink(vdev
) < 0)
218 wait_event_interruptible(vdev
->waitq_tx
,
219 (!list_empty(&vdev
->priv_tx
) ||
220 !list_empty(&vdev
->unlink_tx
) ||
221 kthread_should_stop()));
223 usbip_dbg_vhci_tx("pending urbs ?, now wake up\n");