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
;
172 size_t total_size
= 0;
174 while ((priv
= dequeue_from_priv_tx(sdev
)) != NULL
) {
176 struct urb
*urb
= priv
->urb
;
177 struct usbip_header pdu_header
;
178 void *iso_buffer
= NULL
;
179 struct kvec
*iov
= NULL
;
183 memset(&pdu_header
, 0, sizeof(pdu_header
));
184 memset(&msg
, 0, sizeof(msg
));
186 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
)
187 iovnum
= 2 + urb
->number_of_packets
;
191 iov
= kzalloc(iovnum
* sizeof(struct kvec
), GFP_KERNEL
);
194 usbip_event_add(&sdev
->ud
, SDEV_EVENT_ERROR_MALLOC
);
200 /* 1. setup usbip_header */
201 setup_ret_submit_pdu(&pdu_header
, urb
);
202 usbip_dbg_stub_tx("setup txdata seqnum: %d urb: %p\n",
203 pdu_header
.base
.seqnum
, urb
);
204 /*usbip_dump_header(pdu_header);*/
205 usbip_header_correct_endian(&pdu_header
, 1);
207 iov
[iovnum
].iov_base
= &pdu_header
;
208 iov
[iovnum
].iov_len
= sizeof(pdu_header
);
210 txsize
+= sizeof(pdu_header
);
212 /* 2. setup transfer buffer */
213 if (usb_pipein(urb
->pipe
) &&
214 usb_pipetype(urb
->pipe
) != PIPE_ISOCHRONOUS
&&
215 urb
->actual_length
> 0) {
216 iov
[iovnum
].iov_base
= urb
->transfer_buffer
;
217 iov
[iovnum
].iov_len
= urb
->actual_length
;
219 txsize
+= urb
->actual_length
;
220 } else if (usb_pipein(urb
->pipe
) &&
221 usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
) {
223 * For isochronous packets: actual length is the sum of
224 * the actual length of the individual, packets, but as
225 * the packet offsets are not changed there will be
226 * padding between the packets. To optimally use the
227 * bandwidth the padding is not transmitted.
231 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
232 iov
[iovnum
].iov_base
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
233 iov
[iovnum
].iov_len
= urb
->iso_frame_desc
[i
].actual_length
;
235 txsize
+= urb
->iso_frame_desc
[i
].actual_length
;
238 if (txsize
!= sizeof(pdu_header
) + urb
->actual_length
) {
239 dev_err(&sdev
->interface
->dev
,
240 "actual length of urb (%d) does not match iso packet sizes (%d)\n",
241 urb
->actual_length
, txsize
-sizeof(pdu_header
));
243 usbip_event_add(&sdev
->ud
, SDEV_EVENT_ERROR_TCP
);
248 /* 3. setup iso_packet_descriptor */
249 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
) {
252 iso_buffer
= usbip_alloc_iso_desc_pdu(urb
, &len
);
254 usbip_event_add(&sdev
->ud
,
255 SDEV_EVENT_ERROR_MALLOC
);
260 iov
[iovnum
].iov_base
= iso_buffer
;
261 iov
[iovnum
].iov_len
= len
;
266 ret
= kernel_sendmsg(sdev
->ud
.tcp_socket
, &msg
,
267 iov
, iovnum
, txsize
);
269 dev_err(&sdev
->interface
->dev
,
270 "sendmsg failed!, retval %d for %zd\n",
274 usbip_event_add(&sdev
->ud
, SDEV_EVENT_ERROR_TCP
);
281 total_size
+= txsize
;
284 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
286 list_for_each_entry_safe(priv
, tmp
, &sdev
->priv_free
, list
) {
287 stub_free_priv_and_urb(priv
);
290 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
296 /*-------------------------------------------------------------------------*/
297 /* send RET_UNLINK */
299 static struct stub_unlink
*dequeue_from_unlink_tx(struct stub_device
*sdev
)
302 struct stub_unlink
*unlink
, *tmp
;
304 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
306 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_tx
, list
) {
307 list_move_tail(&unlink
->list
, &sdev
->unlink_free
);
308 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
312 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
318 static int stub_send_ret_unlink(struct stub_device
*sdev
)
321 struct stub_unlink
*unlink
, *tmp
;
327 size_t total_size
= 0;
329 while ((unlink
= dequeue_from_unlink_tx(sdev
)) != NULL
) {
331 struct usbip_header pdu_header
;
334 memset(&pdu_header
, 0, sizeof(pdu_header
));
335 memset(&msg
, 0, sizeof(msg
));
336 memset(&iov
, 0, sizeof(iov
));
338 usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink
->seqnum
);
340 /* 1. setup usbip_header */
341 setup_ret_unlink_pdu(&pdu_header
, unlink
);
342 usbip_header_correct_endian(&pdu_header
, 1);
344 iov
[0].iov_base
= &pdu_header
;
345 iov
[0].iov_len
= sizeof(pdu_header
);
346 txsize
+= sizeof(pdu_header
);
348 ret
= kernel_sendmsg(sdev
->ud
.tcp_socket
, &msg
, iov
,
351 dev_err(&sdev
->interface
->dev
,
352 "sendmsg failed!, retval %d for %zd\n",
354 usbip_event_add(&sdev
->ud
, SDEV_EVENT_ERROR_TCP
);
359 usbip_dbg_stub_tx("send txdata\n");
361 total_size
+= txsize
;
365 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
367 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_free
, list
) {
368 list_del(&unlink
->list
);
372 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
378 /*-------------------------------------------------------------------------*/
380 void stub_tx_loop(struct usbip_task
*ut
)
382 struct usbip_device
*ud
= container_of(ut
, struct usbip_device
, tcp_tx
);
383 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
386 if (signal_pending(current
)) {
387 usbip_dbg_stub_tx("signal catched\n");
391 if (usbip_event_happened(ud
))
395 * send_ret_submit comes earlier than send_ret_unlink. stub_rx
396 * looks at only priv_init queue. If the completion of a URB is
397 * earlier than the receive of CMD_UNLINK, priv is moved to
398 * priv_tx queue and stub_rx does not find the target priv. In
399 * this case, vhci_rx receives the result of the submit request
400 * and then receives the result of the unlink request. The
401 * result of the submit is given back to the usbcore as the
402 * completion of the unlink request. The request of the
403 * unlink is ignored. This is ok because a driver who calls
404 * usb_unlink_urb() understands the unlink was too late by
405 * getting the status of the given-backed URB which has the
406 * status of usb_submit_urb().
408 if (stub_send_ret_submit(sdev
) < 0)
411 if (stub_send_ret_unlink(sdev
) < 0)
414 wait_event_interruptible(sdev
->tx_waitq
,
415 (!list_empty(&sdev
->priv_tx
) ||
416 !list_empty(&sdev
->unlink_tx
)));