1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
6 #include <linux/kthread.h>
7 #include <linux/slab.h>
9 #include "usbip_common.h"
12 static void setup_cmd_submit_pdu(struct usbip_header
*pdup
, struct urb
*urb
)
14 struct vhci_priv
*priv
= ((struct vhci_priv
*)urb
->hcpriv
);
15 struct vhci_device
*vdev
= priv
->vdev
;
17 usbip_dbg_vhci_tx("URB, local devnum %u, remote devid %u\n",
18 usb_pipedevice(urb
->pipe
), vdev
->devid
);
20 pdup
->base
.command
= USBIP_CMD_SUBMIT
;
21 pdup
->base
.seqnum
= priv
->seqnum
;
22 pdup
->base
.devid
= vdev
->devid
;
23 pdup
->base
.direction
= usb_pipein(urb
->pipe
) ?
24 USBIP_DIR_IN
: USBIP_DIR_OUT
;
25 pdup
->base
.ep
= usb_pipeendpoint(urb
->pipe
);
27 usbip_pack_pdu(pdup
, urb
, USBIP_CMD_SUBMIT
, 1);
29 if (urb
->setup_packet
)
30 memcpy(pdup
->u
.cmd_submit
.setup
, urb
->setup_packet
, 8);
33 static struct vhci_priv
*dequeue_from_priv_tx(struct vhci_device
*vdev
)
35 struct vhci_priv
*priv
, *tmp
;
38 spin_lock_irqsave(&vdev
->priv_lock
, flags
);
40 list_for_each_entry_safe(priv
, tmp
, &vdev
->priv_tx
, list
) {
41 list_move_tail(&priv
->list
, &vdev
->priv_rx
);
42 spin_unlock_irqrestore(&vdev
->priv_lock
, flags
);
46 spin_unlock_irqrestore(&vdev
->priv_lock
, flags
);
51 static int vhci_send_cmd_submit(struct vhci_device
*vdev
)
53 struct vhci_priv
*priv
= NULL
;
59 size_t total_size
= 0;
61 while ((priv
= dequeue_from_priv_tx(vdev
)) != NULL
) {
63 struct urb
*urb
= priv
->urb
;
64 struct usbip_header pdu_header
;
65 struct usbip_iso_packet_descriptor
*iso_buffer
= NULL
;
68 memset(&pdu_header
, 0, sizeof(pdu_header
));
69 memset(&msg
, 0, sizeof(msg
));
70 memset(&iov
, 0, sizeof(iov
));
72 usbip_dbg_vhci_tx("setup txdata urb seqnum %lu\n",
75 /* 1. setup usbip_header */
76 setup_cmd_submit_pdu(&pdu_header
, urb
);
77 usbip_header_correct_endian(&pdu_header
, 1);
79 iov
[0].iov_base
= &pdu_header
;
80 iov
[0].iov_len
= sizeof(pdu_header
);
81 txsize
+= sizeof(pdu_header
);
83 /* 2. setup transfer buffer */
84 if (!usb_pipein(urb
->pipe
) && urb
->transfer_buffer_length
> 0) {
85 iov
[1].iov_base
= urb
->transfer_buffer
;
86 iov
[1].iov_len
= urb
->transfer_buffer_length
;
87 txsize
+= urb
->transfer_buffer_length
;
90 /* 3. setup iso_packet_descriptor */
91 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
) {
94 iso_buffer
= usbip_alloc_iso_desc_pdu(urb
, &len
);
96 usbip_event_add(&vdev
->ud
,
97 SDEV_EVENT_ERROR_MALLOC
);
101 iov
[2].iov_base
= iso_buffer
;
102 iov
[2].iov_len
= len
;
106 ret
= kernel_sendmsg(vdev
->ud
.tcp_socket
, &msg
, iov
, 3, txsize
);
108 pr_err("sendmsg failed!, ret=%d for %zd\n", ret
,
111 usbip_event_add(&vdev
->ud
, VDEV_EVENT_ERROR_TCP
);
116 usbip_dbg_vhci_tx("send txdata\n");
118 total_size
+= txsize
;
124 static struct vhci_unlink
*dequeue_from_unlink_tx(struct vhci_device
*vdev
)
126 struct vhci_unlink
*unlink
, *tmp
;
129 spin_lock_irqsave(&vdev
->priv_lock
, flags
);
131 list_for_each_entry_safe(unlink
, tmp
, &vdev
->unlink_tx
, list
) {
132 list_move_tail(&unlink
->list
, &vdev
->unlink_rx
);
133 spin_unlock_irqrestore(&vdev
->priv_lock
, flags
);
137 spin_unlock_irqrestore(&vdev
->priv_lock
, flags
);
142 static int vhci_send_cmd_unlink(struct vhci_device
*vdev
)
144 struct vhci_unlink
*unlink
= NULL
;
149 size_t total_size
= 0;
151 while ((unlink
= dequeue_from_unlink_tx(vdev
)) != NULL
) {
153 struct usbip_header pdu_header
;
155 memset(&pdu_header
, 0, sizeof(pdu_header
));
156 memset(&msg
, 0, sizeof(msg
));
157 memset(&iov
, 0, sizeof(iov
));
159 usbip_dbg_vhci_tx("setup cmd unlink, %lu\n", unlink
->seqnum
);
161 /* 1. setup usbip_header */
162 pdu_header
.base
.command
= USBIP_CMD_UNLINK
;
163 pdu_header
.base
.seqnum
= unlink
->seqnum
;
164 pdu_header
.base
.devid
= vdev
->devid
;
165 pdu_header
.base
.ep
= 0;
166 pdu_header
.u
.cmd_unlink
.seqnum
= unlink
->unlink_seqnum
;
168 usbip_header_correct_endian(&pdu_header
, 1);
170 iov
.iov_base
= &pdu_header
;
171 iov
.iov_len
= sizeof(pdu_header
);
172 txsize
= sizeof(pdu_header
);
174 ret
= kernel_sendmsg(vdev
->ud
.tcp_socket
, &msg
, &iov
, 1, txsize
);
176 pr_err("sendmsg failed!, ret=%d for %zd\n", ret
,
178 usbip_event_add(&vdev
->ud
, VDEV_EVENT_ERROR_TCP
);
182 usbip_dbg_vhci_tx("send txdata\n");
184 total_size
+= txsize
;
190 int vhci_tx_loop(void *data
)
192 struct usbip_device
*ud
= data
;
193 struct vhci_device
*vdev
= container_of(ud
, struct vhci_device
, ud
);
195 while (!kthread_should_stop()) {
196 if (vhci_send_cmd_submit(vdev
) < 0)
199 if (vhci_send_cmd_unlink(vdev
) < 0)
202 wait_event_interruptible(vdev
->waitq_tx
,
203 (!list_empty(&vdev
->priv_tx
) ||
204 !list_empty(&vdev
->unlink_tx
) ||
205 kthread_should_stop()));
207 usbip_dbg_vhci_tx("pending urbs ?, now wake up\n");