1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
6 #include <linux/kthread.h>
7 #include <linux/socket.h>
8 #include <linux/scatterlist.h>
10 #include "usbip_common.h"
13 /* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
14 void stub_enqueue_ret_unlink(struct stub_device
*sdev
, __u32 seqnum
,
17 struct stub_unlink
*unlink
;
19 unlink
= kzalloc(sizeof(struct stub_unlink
), GFP_ATOMIC
);
21 usbip_event_add(&sdev
->ud
, VDEV_EVENT_ERROR_MALLOC
);
25 unlink
->seqnum
= seqnum
;
26 unlink
->status
= status
;
28 list_add_tail(&unlink
->list
, &sdev
->unlink_tx
);
32 * stub_complete - completion handler of a usbip urb
33 * @urb: pointer to the urb completed
35 * When a urb has completed, the USB core driver calls this function mostly in
36 * the interrupt context. To return the result of a urb, the completed urb is
37 * linked to the pending list of returning.
40 void stub_complete(struct urb
*urb
)
42 struct stub_priv
*priv
= (struct stub_priv
*) urb
->context
;
43 struct stub_device
*sdev
= priv
->sdev
;
46 usbip_dbg_stub_tx("complete! status %d\n", urb
->status
);
48 switch (urb
->status
) {
53 dev_info(&urb
->dev
->dev
,
54 "stopped by a call to usb_kill_urb() because of cleaning up a virtual connection\n");
57 dev_info(&urb
->dev
->dev
,
58 "unlinked by a call to usb_unlink_urb()\n");
61 dev_info(&urb
->dev
->dev
, "endpoint %d is stalled\n",
62 usb_pipeendpoint(urb
->pipe
));
65 dev_info(&urb
->dev
->dev
, "device removed?\n");
68 dev_info(&urb
->dev
->dev
,
69 "urb completion with non-zero status %d\n",
75 * If the server breaks single SG request into the several URBs, the
76 * URBs must be reassembled before sending completed URB to the vhci.
77 * Don't wake up the tx thread until all the URBs are completed.
80 priv
->completed_urbs
++;
82 /* Only save the first error status */
83 if (urb
->status
&& !priv
->urb_status
)
84 priv
->urb_status
= urb
->status
;
86 if (priv
->completed_urbs
< priv
->num_urbs
)
90 /* link a urb to the queue of tx. */
91 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
92 if (sdev
->ud
.tcp_socket
== NULL
) {
93 usbip_dbg_stub_tx("ignore urb for closed connection\n");
94 /* It will be freed in stub_device_cleanup_urbs(). */
95 } else if (priv
->unlinking
) {
96 stub_enqueue_ret_unlink(sdev
, priv
->seqnum
, urb
->status
);
97 stub_free_priv_and_urb(priv
);
99 list_move_tail(&priv
->list
, &sdev
->priv_tx
);
101 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
103 /* wake up tx_thread */
104 wake_up(&sdev
->tx_waitq
);
107 static inline void setup_base_pdu(struct usbip_header_basic
*base
,
108 __u32 command
, __u32 seqnum
)
110 base
->command
= command
;
111 base
->seqnum
= seqnum
;
117 static void setup_ret_submit_pdu(struct usbip_header
*rpdu
, struct urb
*urb
)
119 struct stub_priv
*priv
= (struct stub_priv
*) urb
->context
;
121 setup_base_pdu(&rpdu
->base
, USBIP_RET_SUBMIT
, priv
->seqnum
);
122 usbip_pack_pdu(rpdu
, urb
, USBIP_RET_SUBMIT
, 1);
125 static void setup_ret_unlink_pdu(struct usbip_header
*rpdu
,
126 struct stub_unlink
*unlink
)
128 setup_base_pdu(&rpdu
->base
, USBIP_RET_UNLINK
, unlink
->seqnum
);
129 rpdu
->u
.ret_unlink
.status
= unlink
->status
;
132 static struct stub_priv
*dequeue_from_priv_tx(struct stub_device
*sdev
)
135 struct stub_priv
*priv
, *tmp
;
137 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
139 list_for_each_entry_safe(priv
, tmp
, &sdev
->priv_tx
, list
) {
140 list_move_tail(&priv
->list
, &sdev
->priv_free
);
141 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
145 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
150 static int stub_send_ret_submit(struct stub_device
*sdev
)
153 struct stub_priv
*priv
, *tmp
;
158 size_t total_size
= 0;
160 while ((priv
= dequeue_from_priv_tx(sdev
)) != NULL
) {
161 struct urb
*urb
= priv
->urbs
[0];
162 struct usbip_header pdu_header
;
163 struct usbip_iso_packet_descriptor
*iso_buffer
= NULL
;
164 struct kvec
*iov
= NULL
;
165 struct scatterlist
*sg
;
166 u32 actual_length
= 0;
172 memset(&pdu_header
, 0, sizeof(pdu_header
));
173 memset(&msg
, 0, sizeof(msg
));
175 if (urb
->actual_length
> 0 && !urb
->transfer_buffer
&&
177 dev_err(&sdev
->udev
->dev
,
178 "urb: actual_length %d transfer_buffer null\n",
183 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
)
184 iovnum
= 2 + urb
->number_of_packets
;
185 else if (usb_pipein(urb
->pipe
) && urb
->actual_length
> 0 &&
187 iovnum
= 1 + urb
->num_sgs
;
188 else if (usb_pipein(urb
->pipe
) && priv
->sgl
)
189 iovnum
= 1 + priv
->num_urbs
;
193 iov
= kcalloc(iovnum
, sizeof(struct kvec
), GFP_KERNEL
);
196 usbip_event_add(&sdev
->ud
, SDEV_EVENT_ERROR_MALLOC
);
202 /* 1. setup usbip_header */
203 setup_ret_submit_pdu(&pdu_header
, urb
);
204 usbip_dbg_stub_tx("setup txdata seqnum: %d\n",
205 pdu_header
.base
.seqnum
);
208 for (i
= 0; i
< priv
->num_urbs
; i
++)
209 actual_length
+= priv
->urbs
[i
]->actual_length
;
211 pdu_header
.u
.ret_submit
.status
= priv
->urb_status
;
212 pdu_header
.u
.ret_submit
.actual_length
= actual_length
;
215 usbip_header_correct_endian(&pdu_header
, 1);
217 iov
[iovnum
].iov_base
= &pdu_header
;
218 iov
[iovnum
].iov_len
= sizeof(pdu_header
);
220 txsize
+= sizeof(pdu_header
);
222 /* 2. setup transfer buffer */
223 if (usb_pipein(urb
->pipe
) && priv
->sgl
) {
224 /* If the server split a single SG request into several
225 * URBs because the server's HCD doesn't support SG,
226 * reassemble the split URB buffers into a single
229 for (i
= 0; i
< priv
->num_urbs
; i
++) {
230 iov
[iovnum
].iov_base
=
231 priv
->urbs
[i
]->transfer_buffer
;
232 iov
[iovnum
].iov_len
=
233 priv
->urbs
[i
]->actual_length
;
236 txsize
+= actual_length
;
237 } else if (usb_pipein(urb
->pipe
) &&
238 usb_pipetype(urb
->pipe
) != PIPE_ISOCHRONOUS
&&
239 urb
->actual_length
> 0) {
241 unsigned int copy
= urb
->actual_length
;
244 for_each_sg(urb
->sg
, sg
, urb
->num_sgs
, i
) {
248 if (copy
< sg
->length
)
253 iov
[iovnum
].iov_base
= sg_virt(sg
);
254 iov
[iovnum
].iov_len
= size
;
260 iov
[iovnum
].iov_base
= urb
->transfer_buffer
;
261 iov
[iovnum
].iov_len
= urb
->actual_length
;
264 txsize
+= urb
->actual_length
;
265 } else if (usb_pipein(urb
->pipe
) &&
266 usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
) {
268 * For isochronous packets: actual length is the sum of
269 * the actual length of the individual, packets, but as
270 * the packet offsets are not changed there will be
271 * padding between the packets. To optimally use the
272 * bandwidth the padding is not transmitted.
277 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
278 iov
[iovnum
].iov_base
= urb
->transfer_buffer
+
279 urb
->iso_frame_desc
[i
].offset
;
280 iov
[iovnum
].iov_len
=
281 urb
->iso_frame_desc
[i
].actual_length
;
283 txsize
+= urb
->iso_frame_desc
[i
].actual_length
;
286 if (txsize
!= sizeof(pdu_header
) + urb
->actual_length
) {
287 dev_err(&sdev
->udev
->dev
,
288 "actual length of urb %d does not match iso packet sizes %zu\n",
290 txsize
-sizeof(pdu_header
));
292 usbip_event_add(&sdev
->ud
,
293 SDEV_EVENT_ERROR_TCP
);
298 /* 3. setup iso_packet_descriptor */
299 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
) {
302 iso_buffer
= usbip_alloc_iso_desc_pdu(urb
, &len
);
304 usbip_event_add(&sdev
->ud
,
305 SDEV_EVENT_ERROR_MALLOC
);
310 iov
[iovnum
].iov_base
= iso_buffer
;
311 iov
[iovnum
].iov_len
= len
;
316 ret
= kernel_sendmsg(sdev
->ud
.tcp_socket
, &msg
,
317 iov
, iovnum
, txsize
);
319 dev_err(&sdev
->udev
->dev
,
320 "sendmsg failed!, retval %d for %zd\n",
324 usbip_event_add(&sdev
->ud
, SDEV_EVENT_ERROR_TCP
);
331 total_size
+= txsize
;
334 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
335 list_for_each_entry_safe(priv
, tmp
, &sdev
->priv_free
, list
) {
336 stub_free_priv_and_urb(priv
);
338 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
343 static struct stub_unlink
*dequeue_from_unlink_tx(struct stub_device
*sdev
)
346 struct stub_unlink
*unlink
, *tmp
;
348 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
350 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_tx
, list
) {
351 list_move_tail(&unlink
->list
, &sdev
->unlink_free
);
352 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
356 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
361 static int stub_send_ret_unlink(struct stub_device
*sdev
)
364 struct stub_unlink
*unlink
, *tmp
;
370 size_t total_size
= 0;
372 while ((unlink
= dequeue_from_unlink_tx(sdev
)) != NULL
) {
374 struct usbip_header pdu_header
;
377 memset(&pdu_header
, 0, sizeof(pdu_header
));
378 memset(&msg
, 0, sizeof(msg
));
379 memset(&iov
, 0, sizeof(iov
));
381 usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink
->seqnum
);
383 /* 1. setup usbip_header */
384 setup_ret_unlink_pdu(&pdu_header
, unlink
);
385 usbip_header_correct_endian(&pdu_header
, 1);
387 iov
[0].iov_base
= &pdu_header
;
388 iov
[0].iov_len
= sizeof(pdu_header
);
389 txsize
+= sizeof(pdu_header
);
391 ret
= kernel_sendmsg(sdev
->ud
.tcp_socket
, &msg
, iov
,
394 dev_err(&sdev
->udev
->dev
,
395 "sendmsg failed!, retval %d for %zd\n",
397 usbip_event_add(&sdev
->ud
, SDEV_EVENT_ERROR_TCP
);
401 usbip_dbg_stub_tx("send txdata\n");
402 total_size
+= txsize
;
405 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
407 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_free
, list
) {
408 list_del(&unlink
->list
);
412 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
417 int stub_tx_loop(void *data
)
419 struct usbip_device
*ud
= data
;
420 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
422 while (!kthread_should_stop()) {
423 if (usbip_event_happened(ud
))
427 * send_ret_submit comes earlier than send_ret_unlink. stub_rx
428 * looks at only priv_init queue. If the completion of a URB is
429 * earlier than the receive of CMD_UNLINK, priv is moved to
430 * priv_tx queue and stub_rx does not find the target priv. In
431 * this case, vhci_rx receives the result of the submit request
432 * and then receives the result of the unlink request. The
433 * result of the submit is given back to the usbcore as the
434 * completion of the unlink request. The request of the
435 * unlink is ignored. This is ok because a driver who calls
436 * usb_unlink_urb() understands the unlink was too late by
437 * getting the status of the given-backed URB which has the
438 * status of usb_submit_urb().
440 if (stub_send_ret_submit(sdev
) < 0)
443 if (stub_send_ret_unlink(sdev
) < 0)
446 wait_event_interruptible(sdev
->tx_waitq
,
447 (!list_empty(&sdev
->priv_tx
) ||
448 !list_empty(&sdev
->unlink_tx
) ||
449 kthread_should_stop()));