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 stub_free_priv_and_urb(struct stub_priv
*priv
)
26 struct urb
*urb
= priv
->urb
;
28 kfree(urb
->setup_packet
);
29 kfree(urb
->transfer_buffer
);
30 list_del(&priv
->list
);
31 kmem_cache_free(stub_priv_cache
, priv
);
35 /* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
36 void stub_enqueue_ret_unlink(struct stub_device
*sdev
, __u32 seqnum
,
39 struct stub_unlink
*unlink
;
41 unlink
= kzalloc(sizeof(struct stub_unlink
), GFP_ATOMIC
);
43 dev_err(&sdev
->interface
->dev
, "alloc stub_unlink\n");
44 usbip_event_add(&sdev
->ud
, VDEV_EVENT_ERROR_MALLOC
);
48 unlink
->seqnum
= seqnum
;
49 unlink
->status
= status
;
51 list_add_tail(&unlink
->list
, &sdev
->unlink_tx
);
55 * stub_complete - completion handler of a usbip urb
56 * @urb: pointer to the urb completed
58 * When a urb has completed, the USB core driver calls this function mostly in
59 * the interrupt context. To return the result of a urb, the completed urb is
60 * linked to the pending list of returning.
63 void stub_complete(struct urb
*urb
)
65 struct stub_priv
*priv
= (struct stub_priv
*) urb
->context
;
66 struct stub_device
*sdev
= priv
->sdev
;
69 usbip_dbg_stub_tx("complete! status %d\n", urb
->status
);
72 switch (urb
->status
) {
77 usbip_uinfo("stopped by a call of usb_kill_urb() because of"
78 "cleaning up a virtual connection\n");
81 usbip_uinfo("unlinked by a call of usb_unlink_urb()\n");
84 usbip_uinfo("endpoint %d is stalled\n",
85 usb_pipeendpoint(urb
->pipe
));
88 usbip_uinfo("device removed?\n");
91 usbip_uinfo("urb completion with non-zero status %d\n",
95 /* link a urb to the queue of tx. */
96 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
98 if (priv
->unlinking
) {
99 stub_enqueue_ret_unlink(sdev
, priv
->seqnum
, urb
->status
);
100 stub_free_priv_and_urb(priv
);
102 list_move_tail(&priv
->list
, &sdev
->priv_tx
);
105 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
107 /* wake up tx_thread */
108 wake_up(&sdev
->tx_waitq
);
112 /*-------------------------------------------------------------------------*/
115 static inline void setup_base_pdu(struct usbip_header_basic
*base
,
116 __u32 command
, __u32 seqnum
)
118 base
->command
= command
;
119 base
->seqnum
= seqnum
;
125 static void setup_ret_submit_pdu(struct usbip_header
*rpdu
, struct urb
*urb
)
127 struct stub_priv
*priv
= (struct stub_priv
*) urb
->context
;
129 setup_base_pdu(&rpdu
->base
, USBIP_RET_SUBMIT
, priv
->seqnum
);
131 usbip_pack_pdu(rpdu
, urb
, USBIP_RET_SUBMIT
, 1);
134 static void setup_ret_unlink_pdu(struct usbip_header
*rpdu
,
135 struct stub_unlink
*unlink
)
137 setup_base_pdu(&rpdu
->base
, USBIP_RET_UNLINK
, unlink
->seqnum
);
139 rpdu
->u
.ret_unlink
.status
= unlink
->status
;
143 /*-------------------------------------------------------------------------*/
144 /* send RET_SUBMIT */
146 static struct stub_priv
*dequeue_from_priv_tx(struct stub_device
*sdev
)
149 struct stub_priv
*priv
, *tmp
;
151 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
153 list_for_each_entry_safe(priv
, tmp
, &sdev
->priv_tx
, list
) {
154 list_move_tail(&priv
->list
, &sdev
->priv_free
);
155 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
159 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
164 static int stub_send_ret_submit(struct stub_device
*sdev
)
167 struct stub_priv
*priv
, *tmp
;
173 size_t total_size
= 0;
175 while ((priv
= dequeue_from_priv_tx(sdev
)) != NULL
) {
177 struct urb
*urb
= priv
->urb
;
178 struct usbip_header pdu_header
;
179 void *iso_buffer
= NULL
;
182 memset(&pdu_header
, 0, sizeof(pdu_header
));
183 memset(&msg
, 0, sizeof(msg
));
184 memset(&iov
, 0, sizeof(iov
));
186 usbip_dbg_stub_tx("setup txdata urb %p\n", urb
);
189 /* 1. setup usbip_header */
190 setup_ret_submit_pdu(&pdu_header
, urb
);
191 usbip_header_correct_endian(&pdu_header
, 1);
193 iov
[0].iov_base
= &pdu_header
;
194 iov
[0].iov_len
= sizeof(pdu_header
);
195 txsize
+= sizeof(pdu_header
);
197 /* 2. setup transfer buffer */
198 if (usb_pipein(urb
->pipe
) && urb
->actual_length
> 0) {
199 iov
[1].iov_base
= urb
->transfer_buffer
;
200 iov
[1].iov_len
= urb
->actual_length
;
201 txsize
+= urb
->actual_length
;
204 /* 3. setup iso_packet_descriptor */
205 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
) {
208 iso_buffer
= usbip_alloc_iso_desc_pdu(urb
, &len
);
210 usbip_event_add(&sdev
->ud
,
211 SDEV_EVENT_ERROR_MALLOC
);
215 iov
[2].iov_base
= iso_buffer
;
216 iov
[2].iov_len
= len
;
220 ret
= kernel_sendmsg(sdev
->ud
.tcp_socket
, &msg
, iov
,
223 dev_err(&sdev
->interface
->dev
,
224 "sendmsg failed!, retval %d for %zd\n",
227 usbip_event_add(&sdev
->ud
, SDEV_EVENT_ERROR_TCP
);
232 usbip_dbg_stub_tx("send txdata\n");
234 total_size
+= txsize
;
238 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
240 list_for_each_entry_safe(priv
, tmp
, &sdev
->priv_free
, list
) {
241 stub_free_priv_and_urb(priv
);
244 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
250 /*-------------------------------------------------------------------------*/
251 /* send RET_UNLINK */
253 static struct stub_unlink
*dequeue_from_unlink_tx(struct stub_device
*sdev
)
256 struct stub_unlink
*unlink
, *tmp
;
258 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
260 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_tx
, list
) {
261 list_move_tail(&unlink
->list
, &sdev
->unlink_free
);
262 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
266 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
272 static int stub_send_ret_unlink(struct stub_device
*sdev
)
275 struct stub_unlink
*unlink
, *tmp
;
281 size_t total_size
= 0;
283 while ((unlink
= dequeue_from_unlink_tx(sdev
)) != NULL
) {
285 struct usbip_header pdu_header
;
288 memset(&pdu_header
, 0, sizeof(pdu_header
));
289 memset(&msg
, 0, sizeof(msg
));
290 memset(&iov
, 0, sizeof(iov
));
292 usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink
->seqnum
);
294 /* 1. setup usbip_header */
295 setup_ret_unlink_pdu(&pdu_header
, unlink
);
296 usbip_header_correct_endian(&pdu_header
, 1);
298 iov
[0].iov_base
= &pdu_header
;
299 iov
[0].iov_len
= sizeof(pdu_header
);
300 txsize
+= sizeof(pdu_header
);
302 ret
= kernel_sendmsg(sdev
->ud
.tcp_socket
, &msg
, iov
,
305 dev_err(&sdev
->interface
->dev
,
306 "sendmsg failed!, retval %d for %zd\n",
308 usbip_event_add(&sdev
->ud
, SDEV_EVENT_ERROR_TCP
);
313 usbip_dbg_stub_tx("send txdata\n");
315 total_size
+= txsize
;
319 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
321 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_free
, list
) {
322 list_del(&unlink
->list
);
326 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
332 /*-------------------------------------------------------------------------*/
334 void stub_tx_loop(struct usbip_task
*ut
)
336 struct usbip_device
*ud
= container_of(ut
, struct usbip_device
, tcp_tx
);
337 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
340 if (signal_pending(current
)) {
341 usbip_dbg_stub_tx("signal catched\n");
345 if (usbip_event_happened(ud
))
349 * send_ret_submit comes earlier than send_ret_unlink. stub_rx
350 * looks at only priv_init queue. If the completion of a URB is
351 * earlier than the receive of CMD_UNLINK, priv is moved to
352 * priv_tx queue and stub_rx does not find the target priv. In
353 * this case, vhci_rx receives the result of the submit request
354 * and then receives the result of the unlink request. The
355 * result of the submit is given back to the usbcore as the
356 * completion of the unlink request. The request of the
357 * unlink is ignored. This is ok because a driver who calls
358 * usb_unlink_urb() understands the unlink was too late by
359 * getting the status of the given-backed URB which has the
360 * status of usb_submit_urb().
362 if (stub_send_ret_submit(sdev
) < 0)
365 if (stub_send_ret_unlink(sdev
) < 0)
368 wait_event_interruptible(sdev
->tx_waitq
,
369 (!list_empty(&sdev
->priv_tx
) ||
370 !list_empty(&sdev
->unlink_tx
)));