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 dbg_stub_tx("complete! status %d\n", urb
->status
);
72 switch (urb
->status
) {
77 uinfo("stopped by a call of usb_kill_urb() because of"
78 "cleaning up a virtual connection\n");
81 uinfo("unlinked by a call of usb_unlink_urb()\n");
84 uinfo("endpoint %d is stalled\n", usb_pipeendpoint(urb
->pipe
));
87 uinfo("device removed?\n");
90 uinfo("urb completion with non-zero status %d\n", urb
->status
);
93 /* link a urb to the queue of tx. */
94 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
96 if (priv
->unlinking
) {
97 stub_enqueue_ret_unlink(sdev
, priv
->seqnum
, urb
->status
);
98 stub_free_priv_and_urb(priv
);
100 list_move_tail(&priv
->list
, &sdev
->priv_tx
);
103 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
105 /* wake up tx_thread */
106 wake_up(&sdev
->tx_waitq
);
110 /*-------------------------------------------------------------------------*/
113 static inline void setup_base_pdu(struct usbip_header_basic
*base
,
114 __u32 command
, __u32 seqnum
)
116 base
->command
= command
;
117 base
->seqnum
= seqnum
;
123 static void setup_ret_submit_pdu(struct usbip_header
*rpdu
, struct urb
*urb
)
125 struct stub_priv
*priv
= (struct stub_priv
*) urb
->context
;
127 setup_base_pdu(&rpdu
->base
, USBIP_RET_SUBMIT
, priv
->seqnum
);
129 usbip_pack_pdu(rpdu
, urb
, USBIP_RET_SUBMIT
, 1);
132 static void setup_ret_unlink_pdu(struct usbip_header
*rpdu
,
133 struct stub_unlink
*unlink
)
135 setup_base_pdu(&rpdu
->base
, USBIP_RET_UNLINK
, unlink
->seqnum
);
137 rpdu
->u
.ret_unlink
.status
= unlink
->status
;
141 /*-------------------------------------------------------------------------*/
142 /* send RET_SUBMIT */
144 static struct stub_priv
*dequeue_from_priv_tx(struct stub_device
*sdev
)
147 struct stub_priv
*priv
, *tmp
;
149 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
151 list_for_each_entry_safe(priv
, tmp
, &sdev
->priv_tx
, list
) {
152 list_move_tail(&priv
->list
, &sdev
->priv_free
);
153 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
157 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
162 static int stub_send_ret_submit(struct stub_device
*sdev
)
165 struct stub_priv
*priv
, *tmp
;
171 size_t total_size
= 0;
173 while ((priv
= dequeue_from_priv_tx(sdev
)) != NULL
) {
175 struct urb
*urb
= priv
->urb
;
176 struct usbip_header pdu_header
;
177 void *iso_buffer
= NULL
;
180 memset(&pdu_header
, 0, sizeof(pdu_header
));
181 memset(&msg
, 0, sizeof(msg
));
182 memset(&iov
, 0, sizeof(iov
));
184 dbg_stub_tx("setup txdata urb %p\n", urb
);
187 /* 1. setup usbip_header */
188 setup_ret_submit_pdu(&pdu_header
, urb
);
189 usbip_header_correct_endian(&pdu_header
, 1);
191 iov
[0].iov_base
= &pdu_header
;
192 iov
[0].iov_len
= sizeof(pdu_header
);
193 txsize
+= sizeof(pdu_header
);
195 /* 2. setup transfer buffer */
196 if (usb_pipein(urb
->pipe
) && urb
->actual_length
> 0) {
197 iov
[1].iov_base
= urb
->transfer_buffer
;
198 iov
[1].iov_len
= urb
->actual_length
;
199 txsize
+= urb
->actual_length
;
202 /* 3. setup iso_packet_descriptor */
203 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
) {
206 iso_buffer
= usbip_alloc_iso_desc_pdu(urb
, &len
);
208 usbip_event_add(&sdev
->ud
,
209 SDEV_EVENT_ERROR_MALLOC
);
213 iov
[2].iov_base
= iso_buffer
;
214 iov
[2].iov_len
= len
;
218 ret
= kernel_sendmsg(sdev
->ud
.tcp_socket
, &msg
, iov
,
221 dev_err(&sdev
->interface
->dev
,
222 "sendmsg failed!, retval %d for %zd\n",
225 usbip_event_add(&sdev
->ud
, SDEV_EVENT_ERROR_TCP
);
230 dbg_stub_tx("send txdata\n");
232 total_size
+= txsize
;
236 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
238 list_for_each_entry_safe(priv
, tmp
, &sdev
->priv_free
, list
) {
239 stub_free_priv_and_urb(priv
);
242 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
248 /*-------------------------------------------------------------------------*/
249 /* send RET_UNLINK */
251 static struct stub_unlink
*dequeue_from_unlink_tx(struct stub_device
*sdev
)
254 struct stub_unlink
*unlink
, *tmp
;
256 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
258 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_tx
, list
) {
259 list_move_tail(&unlink
->list
, &sdev
->unlink_free
);
260 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
264 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
270 static int stub_send_ret_unlink(struct stub_device
*sdev
)
273 struct stub_unlink
*unlink
, *tmp
;
279 size_t total_size
= 0;
281 while ((unlink
= dequeue_from_unlink_tx(sdev
)) != NULL
) {
283 struct usbip_header pdu_header
;
286 memset(&pdu_header
, 0, sizeof(pdu_header
));
287 memset(&msg
, 0, sizeof(msg
));
288 memset(&iov
, 0, sizeof(iov
));
290 dbg_stub_tx("setup ret unlink %lu\n", unlink
->seqnum
);
292 /* 1. setup usbip_header */
293 setup_ret_unlink_pdu(&pdu_header
, unlink
);
294 usbip_header_correct_endian(&pdu_header
, 1);
296 iov
[0].iov_base
= &pdu_header
;
297 iov
[0].iov_len
= sizeof(pdu_header
);
298 txsize
+= sizeof(pdu_header
);
300 ret
= kernel_sendmsg(sdev
->ud
.tcp_socket
, &msg
, iov
,
303 dev_err(&sdev
->interface
->dev
,
304 "sendmsg failed!, retval %d for %zd\n",
306 usbip_event_add(&sdev
->ud
, SDEV_EVENT_ERROR_TCP
);
311 dbg_stub_tx("send txdata\n");
313 total_size
+= txsize
;
317 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
319 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_free
, list
) {
320 list_del(&unlink
->list
);
324 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
330 /*-------------------------------------------------------------------------*/
332 void stub_tx_loop(struct usbip_task
*ut
)
334 struct usbip_device
*ud
= container_of(ut
, struct usbip_device
, tcp_tx
);
335 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
338 if (signal_pending(current
)) {
339 dbg_stub_tx("signal catched\n");
343 if (usbip_event_happend(ud
))
347 * send_ret_submit comes earlier than send_ret_unlink. stub_rx
348 * looks at only priv_init queue. If the completion of a URB is
349 * earlier than the receive of CMD_UNLINK, priv is moved to
350 * priv_tx queue and stub_rx does not find the target priv. In
351 * this case, vhci_rx receives the result of the submit request
352 * and then receives the result of the unlink request. The
353 * result of the submit is given back to the usbcore as the
354 * completion of the unlink request. The request of the
355 * unlink is ignored. This is ok because a driver who calls
356 * usb_unlink_urb() understands the unlink was too late by
357 * getting the status of the given-backed URB which has the
358 * status of usb_submit_urb().
360 if (stub_send_ret_submit(sdev
) < 0)
363 if (stub_send_ret_unlink(sdev
) < 0)
366 wait_event_interruptible(sdev
->tx_waitq
,
367 (!list_empty(&sdev
->priv_tx
) ||
368 !list_empty(&sdev
->unlink_tx
)));