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 <linux/slab.h>
22 #include "usbip_common.h"
26 static void stub_free_priv_and_urb(struct stub_priv
*priv
)
28 struct urb
*urb
= priv
->urb
;
30 kfree(urb
->setup_packet
);
31 kfree(urb
->transfer_buffer
);
32 list_del(&priv
->list
);
33 kmem_cache_free(stub_priv_cache
, priv
);
37 /* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
38 void stub_enqueue_ret_unlink(struct stub_device
*sdev
, __u32 seqnum
,
41 struct stub_unlink
*unlink
;
43 unlink
= kzalloc(sizeof(struct stub_unlink
), GFP_ATOMIC
);
45 dev_err(&sdev
->interface
->dev
, "alloc stub_unlink\n");
46 usbip_event_add(&sdev
->ud
, VDEV_EVENT_ERROR_MALLOC
);
50 unlink
->seqnum
= seqnum
;
51 unlink
->status
= status
;
53 list_add_tail(&unlink
->list
, &sdev
->unlink_tx
);
57 * stub_complete - completion handler of a usbip urb
58 * @urb: pointer to the urb completed
60 * When a urb has completed, the USB core driver calls this function mostly in
61 * the interrupt context. To return the result of a urb, the completed urb is
62 * linked to the pending list of returning.
65 void stub_complete(struct urb
*urb
)
67 struct stub_priv
*priv
= (struct stub_priv
*) urb
->context
;
68 struct stub_device
*sdev
= priv
->sdev
;
71 usbip_dbg_stub_tx("complete! status %d\n", urb
->status
);
74 switch (urb
->status
) {
79 usbip_uinfo("stopped by a call of usb_kill_urb() because of"
80 "cleaning up a virtual connection\n");
83 usbip_uinfo("unlinked by a call of usb_unlink_urb()\n");
86 usbip_uinfo("endpoint %d is stalled\n",
87 usb_pipeendpoint(urb
->pipe
));
90 usbip_uinfo("device removed?\n");
93 usbip_uinfo("urb completion with non-zero status %d\n",
97 /* link a urb to the queue of tx. */
98 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
100 if (priv
->unlinking
) {
101 stub_enqueue_ret_unlink(sdev
, priv
->seqnum
, urb
->status
);
102 stub_free_priv_and_urb(priv
);
104 list_move_tail(&priv
->list
, &sdev
->priv_tx
);
107 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
109 /* wake up tx_thread */
110 wake_up(&sdev
->tx_waitq
);
114 /*-------------------------------------------------------------------------*/
117 static inline void setup_base_pdu(struct usbip_header_basic
*base
,
118 __u32 command
, __u32 seqnum
)
120 base
->command
= command
;
121 base
->seqnum
= seqnum
;
127 static void setup_ret_submit_pdu(struct usbip_header
*rpdu
, struct urb
*urb
)
129 struct stub_priv
*priv
= (struct stub_priv
*) urb
->context
;
131 setup_base_pdu(&rpdu
->base
, USBIP_RET_SUBMIT
, priv
->seqnum
);
133 usbip_pack_pdu(rpdu
, urb
, USBIP_RET_SUBMIT
, 1);
136 static void setup_ret_unlink_pdu(struct usbip_header
*rpdu
,
137 struct stub_unlink
*unlink
)
139 setup_base_pdu(&rpdu
->base
, USBIP_RET_UNLINK
, unlink
->seqnum
);
141 rpdu
->u
.ret_unlink
.status
= unlink
->status
;
145 /*-------------------------------------------------------------------------*/
146 /* send RET_SUBMIT */
148 static struct stub_priv
*dequeue_from_priv_tx(struct stub_device
*sdev
)
151 struct stub_priv
*priv
, *tmp
;
153 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
155 list_for_each_entry_safe(priv
, tmp
, &sdev
->priv_tx
, list
) {
156 list_move_tail(&priv
->list
, &sdev
->priv_free
);
157 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
161 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
166 static int stub_send_ret_submit(struct stub_device
*sdev
)
169 struct stub_priv
*priv
, *tmp
;
175 size_t total_size
= 0;
177 while ((priv
= dequeue_from_priv_tx(sdev
)) != NULL
) {
179 struct urb
*urb
= priv
->urb
;
180 struct usbip_header pdu_header
;
181 void *iso_buffer
= NULL
;
184 memset(&pdu_header
, 0, sizeof(pdu_header
));
185 memset(&msg
, 0, sizeof(msg
));
186 memset(&iov
, 0, sizeof(iov
));
188 usbip_dbg_stub_tx("setup txdata urb %p\n", urb
);
191 /* 1. setup usbip_header */
192 setup_ret_submit_pdu(&pdu_header
, urb
);
193 usbip_header_correct_endian(&pdu_header
, 1);
195 iov
[0].iov_base
= &pdu_header
;
196 iov
[0].iov_len
= sizeof(pdu_header
);
197 txsize
+= sizeof(pdu_header
);
199 /* 2. setup transfer buffer */
200 if (usb_pipein(urb
->pipe
) && urb
->actual_length
> 0) {
201 iov
[1].iov_base
= urb
->transfer_buffer
;
202 iov
[1].iov_len
= urb
->actual_length
;
203 txsize
+= urb
->actual_length
;
206 /* 3. setup iso_packet_descriptor */
207 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
) {
210 iso_buffer
= usbip_alloc_iso_desc_pdu(urb
, &len
);
212 usbip_event_add(&sdev
->ud
,
213 SDEV_EVENT_ERROR_MALLOC
);
217 iov
[2].iov_base
= iso_buffer
;
218 iov
[2].iov_len
= len
;
222 ret
= kernel_sendmsg(sdev
->ud
.tcp_socket
, &msg
, iov
,
225 dev_err(&sdev
->interface
->dev
,
226 "sendmsg failed!, retval %d for %zd\n",
229 usbip_event_add(&sdev
->ud
, SDEV_EVENT_ERROR_TCP
);
234 usbip_dbg_stub_tx("send txdata\n");
236 total_size
+= txsize
;
240 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
242 list_for_each_entry_safe(priv
, tmp
, &sdev
->priv_free
, list
) {
243 stub_free_priv_and_urb(priv
);
246 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
252 /*-------------------------------------------------------------------------*/
253 /* send RET_UNLINK */
255 static struct stub_unlink
*dequeue_from_unlink_tx(struct stub_device
*sdev
)
258 struct stub_unlink
*unlink
, *tmp
;
260 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
262 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_tx
, list
) {
263 list_move_tail(&unlink
->list
, &sdev
->unlink_free
);
264 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
268 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
274 static int stub_send_ret_unlink(struct stub_device
*sdev
)
277 struct stub_unlink
*unlink
, *tmp
;
283 size_t total_size
= 0;
285 while ((unlink
= dequeue_from_unlink_tx(sdev
)) != NULL
) {
287 struct usbip_header pdu_header
;
290 memset(&pdu_header
, 0, sizeof(pdu_header
));
291 memset(&msg
, 0, sizeof(msg
));
292 memset(&iov
, 0, sizeof(iov
));
294 usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink
->seqnum
);
296 /* 1. setup usbip_header */
297 setup_ret_unlink_pdu(&pdu_header
, unlink
);
298 usbip_header_correct_endian(&pdu_header
, 1);
300 iov
[0].iov_base
= &pdu_header
;
301 iov
[0].iov_len
= sizeof(pdu_header
);
302 txsize
+= sizeof(pdu_header
);
304 ret
= kernel_sendmsg(sdev
->ud
.tcp_socket
, &msg
, iov
,
307 dev_err(&sdev
->interface
->dev
,
308 "sendmsg failed!, retval %d for %zd\n",
310 usbip_event_add(&sdev
->ud
, SDEV_EVENT_ERROR_TCP
);
315 usbip_dbg_stub_tx("send txdata\n");
317 total_size
+= txsize
;
321 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
323 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_free
, list
) {
324 list_del(&unlink
->list
);
328 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
334 /*-------------------------------------------------------------------------*/
336 void stub_tx_loop(struct usbip_task
*ut
)
338 struct usbip_device
*ud
= container_of(ut
, struct usbip_device
, tcp_tx
);
339 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
342 if (signal_pending(current
)) {
343 usbip_dbg_stub_tx("signal catched\n");
347 if (usbip_event_happened(ud
))
351 * send_ret_submit comes earlier than send_ret_unlink. stub_rx
352 * looks at only priv_init queue. If the completion of a URB is
353 * earlier than the receive of CMD_UNLINK, priv is moved to
354 * priv_tx queue and stub_rx does not find the target priv. In
355 * this case, vhci_rx receives the result of the submit request
356 * and then receives the result of the unlink request. The
357 * result of the submit is given back to the usbcore as the
358 * completion of the unlink request. The request of the
359 * unlink is ignored. This is ok because a driver who calls
360 * usb_unlink_urb() understands the unlink was too late by
361 * getting the status of the given-backed URB which has the
362 * status of usb_submit_urb().
364 if (stub_send_ret_submit(sdev
) < 0)
367 if (stub_send_ret_unlink(sdev
) < 0)
370 wait_event_interruptible(sdev
->tx_waitq
,
371 (!list_empty(&sdev
->priv_tx
) ||
372 !list_empty(&sdev
->unlink_tx
)));