1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
6 #include <linux/kthread.h>
7 #include <linux/slab.h>
8 #include <linux/scatterlist.h>
10 #include "usbip_common.h"
13 static void setup_cmd_submit_pdu(struct usbip_header
*pdup
, struct urb
*urb
)
15 struct vhci_priv
*priv
= ((struct vhci_priv
*)urb
->hcpriv
);
16 struct vhci_device
*vdev
= priv
->vdev
;
18 usbip_dbg_vhci_tx("URB, local devnum %u, remote devid %u\n",
19 usb_pipedevice(urb
->pipe
), vdev
->devid
);
21 pdup
->base
.command
= USBIP_CMD_SUBMIT
;
22 pdup
->base
.seqnum
= priv
->seqnum
;
23 pdup
->base
.devid
= vdev
->devid
;
24 pdup
->base
.direction
= usb_pipein(urb
->pipe
) ?
25 USBIP_DIR_IN
: USBIP_DIR_OUT
;
26 pdup
->base
.ep
= usb_pipeendpoint(urb
->pipe
);
28 usbip_pack_pdu(pdup
, urb
, USBIP_CMD_SUBMIT
, 1);
30 if (urb
->setup_packet
)
31 memcpy(pdup
->u
.cmd_submit
.setup
, urb
->setup_packet
, 8);
34 static struct vhci_priv
*dequeue_from_priv_tx(struct vhci_device
*vdev
)
36 struct vhci_priv
*priv
, *tmp
;
39 spin_lock_irqsave(&vdev
->priv_lock
, flags
);
41 list_for_each_entry_safe(priv
, tmp
, &vdev
->priv_tx
, list
) {
42 list_move_tail(&priv
->list
, &vdev
->priv_rx
);
43 spin_unlock_irqrestore(&vdev
->priv_lock
, flags
);
47 spin_unlock_irqrestore(&vdev
->priv_lock
, flags
);
52 static int vhci_send_cmd_submit(struct vhci_device
*vdev
)
54 struct usbip_iso_packet_descriptor
*iso_buffer
= NULL
;
55 struct vhci_priv
*priv
= NULL
;
56 struct scatterlist
*sg
;
62 size_t total_size
= 0;
67 while ((priv
= dequeue_from_priv_tx(vdev
)) != NULL
) {
69 struct urb
*urb
= priv
->urb
;
70 struct usbip_header pdu_header
;
73 memset(&pdu_header
, 0, sizeof(pdu_header
));
74 memset(&msg
, 0, sizeof(msg
));
75 memset(&iov
, 0, sizeof(iov
));
77 usbip_dbg_vhci_tx("setup txdata urb seqnum %lu\n",
80 if (urb
->num_sgs
&& usb_pipeout(urb
->pipe
))
81 iovnum
= 2 + urb
->num_sgs
;
85 iov
= kcalloc(iovnum
, sizeof(*iov
), GFP_KERNEL
);
87 usbip_event_add(&vdev
->ud
, SDEV_EVENT_ERROR_MALLOC
);
92 urb
->transfer_flags
|= URB_DMA_MAP_SG
;
94 /* 1. setup usbip_header */
95 setup_cmd_submit_pdu(&pdu_header
, urb
);
96 usbip_header_correct_endian(&pdu_header
, 1);
99 iov
[iovnum
].iov_base
= &pdu_header
;
100 iov
[iovnum
].iov_len
= sizeof(pdu_header
);
101 txsize
+= sizeof(pdu_header
);
104 /* 2. setup transfer buffer */
105 if (!usb_pipein(urb
->pipe
) && urb
->transfer_buffer_length
> 0) {
107 !usb_endpoint_xfer_isoc(&urb
->ep
->desc
)) {
108 for_each_sg(urb
->sg
, sg
, urb
->num_sgs
, i
) {
109 iov
[iovnum
].iov_base
= sg_virt(sg
);
110 iov
[iovnum
].iov_len
= sg
->length
;
114 iov
[iovnum
].iov_base
= urb
->transfer_buffer
;
115 iov
[iovnum
].iov_len
=
116 urb
->transfer_buffer_length
;
119 txsize
+= urb
->transfer_buffer_length
;
122 /* 3. setup iso_packet_descriptor */
123 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
) {
126 iso_buffer
= usbip_alloc_iso_desc_pdu(urb
, &len
);
128 usbip_event_add(&vdev
->ud
,
129 SDEV_EVENT_ERROR_MALLOC
);
133 iov
[iovnum
].iov_base
= iso_buffer
;
134 iov
[iovnum
].iov_len
= len
;
139 ret
= kernel_sendmsg(vdev
->ud
.tcp_socket
, &msg
, iov
, iovnum
,
142 pr_err("sendmsg failed!, ret=%d for %zd\n", ret
,
144 usbip_event_add(&vdev
->ud
, VDEV_EVENT_ERROR_TCP
);
150 /* This is only for isochronous case */
154 usbip_dbg_vhci_tx("send txdata\n");
156 total_size
+= txsize
;
169 static struct vhci_unlink
*dequeue_from_unlink_tx(struct vhci_device
*vdev
)
171 struct vhci_unlink
*unlink
, *tmp
;
174 spin_lock_irqsave(&vdev
->priv_lock
, flags
);
176 list_for_each_entry_safe(unlink
, tmp
, &vdev
->unlink_tx
, list
) {
177 list_move_tail(&unlink
->list
, &vdev
->unlink_rx
);
178 spin_unlock_irqrestore(&vdev
->priv_lock
, flags
);
182 spin_unlock_irqrestore(&vdev
->priv_lock
, flags
);
187 static int vhci_send_cmd_unlink(struct vhci_device
*vdev
)
189 struct vhci_unlink
*unlink
= NULL
;
194 size_t total_size
= 0;
196 while ((unlink
= dequeue_from_unlink_tx(vdev
)) != NULL
) {
198 struct usbip_header pdu_header
;
200 memset(&pdu_header
, 0, sizeof(pdu_header
));
201 memset(&msg
, 0, sizeof(msg
));
202 memset(&iov
, 0, sizeof(iov
));
204 usbip_dbg_vhci_tx("setup cmd unlink, %lu\n", unlink
->seqnum
);
206 /* 1. setup usbip_header */
207 pdu_header
.base
.command
= USBIP_CMD_UNLINK
;
208 pdu_header
.base
.seqnum
= unlink
->seqnum
;
209 pdu_header
.base
.devid
= vdev
->devid
;
210 pdu_header
.base
.ep
= 0;
211 pdu_header
.u
.cmd_unlink
.seqnum
= unlink
->unlink_seqnum
;
213 usbip_header_correct_endian(&pdu_header
, 1);
215 iov
.iov_base
= &pdu_header
;
216 iov
.iov_len
= sizeof(pdu_header
);
217 txsize
= sizeof(pdu_header
);
219 ret
= kernel_sendmsg(vdev
->ud
.tcp_socket
, &msg
, &iov
, 1, txsize
);
221 pr_err("sendmsg failed!, ret=%d for %zd\n", ret
,
223 usbip_event_add(&vdev
->ud
, VDEV_EVENT_ERROR_TCP
);
227 usbip_dbg_vhci_tx("send txdata\n");
229 total_size
+= txsize
;
235 int vhci_tx_loop(void *data
)
237 struct usbip_device
*ud
= data
;
238 struct vhci_device
*vdev
= container_of(ud
, struct vhci_device
, ud
);
240 while (!kthread_should_stop()) {
241 if (vhci_send_cmd_submit(vdev
) < 0)
244 if (vhci_send_cmd_unlink(vdev
) < 0)
247 wait_event_interruptible(vdev
->waitq_tx
,
248 (!list_empty(&vdev
->priv_tx
) ||
249 !list_empty(&vdev
->unlink_tx
) ||
250 kthread_should_stop()));
252 usbip_dbg_vhci_tx("pending urbs ?, now wake up\n");