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
59 * When a urb has completed, the USB core driver calls this function mostly in
60 * the interrupt context. To return the result of a urb, the completed urb is
61 * linked to the pending list of returning.
64 void stub_complete(struct urb *urb)
66 struct stub_priv *priv = (struct stub_priv *) urb->context;
67 struct stub_device *sdev = priv->sdev;
70 dbg_stub_tx("complete! status %d\n", urb->status);
73 switch (urb->status) {
78 uinfo("stopped by a call of usb_kill_urb() because of"
79 "cleaning up a virtual connection\n");
82 uinfo("unlinked by a call of usb_unlink_urb()\n");
85 uinfo("endpoint %d is stalled\n", usb_pipeendpoint(urb->pipe));
88 uinfo("device removed?\n");
91 uinfo("urb completion with non-zero status %d\n", urb->status);
94 /* link a urb to the queue of tx. */
95 spin_lock_irqsave(&sdev->priv_lock, flags);
97 if (priv->unlinking) {
98 stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
99 stub_free_priv_and_urb(priv);
101 list_move_tail(&priv->list, &sdev->priv_tx);
104 spin_unlock_irqrestore(&sdev->priv_lock, flags);
106 /* wake up tx_thread */
107 wake_up(&sdev->tx_waitq);
111 /*-------------------------------------------------------------------------*/
114 static inline void setup_base_pdu(struct usbip_header_basic *base,
115 __u32 command, __u32 seqnum)
117 base->command = command;
118 base->seqnum = seqnum;
124 static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
126 struct stub_priv *priv = (struct stub_priv *) urb->context;
128 setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
130 usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
133 static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
134 struct stub_unlink *unlink)
136 setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
138 rpdu->u.ret_unlink.status = unlink->status;
142 /*-------------------------------------------------------------------------*/
143 /* send RET_SUBMIT */
145 static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
148 struct stub_priv *priv, *tmp;
150 spin_lock_irqsave(&sdev->priv_lock, flags);
152 list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
153 list_move_tail(&priv->list, &sdev->priv_free);
154 spin_unlock_irqrestore(&sdev->priv_lock, flags);
158 spin_unlock_irqrestore(&sdev->priv_lock, flags);
163 static int stub_send_ret_submit(struct stub_device *sdev)
166 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;
181 memset(&pdu_header, 0, sizeof(pdu_header));
182 memset(&msg, 0, sizeof(msg));
183 memset(&iov, 0, sizeof(iov));
185 dbg_stub_tx("setup txdata urb %p\n", urb);
188 /* 1. setup usbip_header */
189 setup_ret_submit_pdu(&pdu_header, urb);
190 usbip_header_correct_endian(&pdu_header, 1);
192 iov[0].iov_base = &pdu_header;
193 iov[0].iov_len = sizeof(pdu_header);
194 txsize += sizeof(pdu_header);
196 /* 2. setup transfer buffer */
197 if (usb_pipein(urb->pipe) && urb->actual_length > 0) {
198 iov[1].iov_base = urb->transfer_buffer;
199 iov[1].iov_len = urb->actual_length;
200 txsize += urb->actual_length;
203 /* 3. setup iso_packet_descriptor */
204 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
207 iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
209 usbip_event_add(&sdev->ud,
210 SDEV_EVENT_ERROR_MALLOC);
214 iov[2].iov_base = iso_buffer;
215 iov[2].iov_len = len;
219 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
222 dev_err(&sdev->interface->dev,
223 "sendmsg failed!, retval %d for %zd\n",
226 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
231 dbg_stub_tx("send txdata\n");
233 total_size += txsize;
237 spin_lock_irqsave(&sdev->priv_lock, flags);
239 list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
240 stub_free_priv_and_urb(priv);
243 spin_unlock_irqrestore(&sdev->priv_lock, flags);
249 /*-------------------------------------------------------------------------*/
250 /* send RET_UNLINK */
252 static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
255 struct stub_unlink *unlink, *tmp;
257 spin_lock_irqsave(&sdev->priv_lock, flags);
259 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
260 list_move_tail(&unlink->list, &sdev->unlink_free);
261 spin_unlock_irqrestore(&sdev->priv_lock, flags);
265 spin_unlock_irqrestore(&sdev->priv_lock, flags);
271 static int stub_send_ret_unlink(struct stub_device *sdev)
274 struct stub_unlink *unlink, *tmp;
280 size_t total_size = 0;
282 while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
284 struct usbip_header pdu_header;
287 memset(&pdu_header, 0, sizeof(pdu_header));
288 memset(&msg, 0, sizeof(msg));
289 memset(&iov, 0, sizeof(iov));
291 dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
293 /* 1. setup usbip_header */
294 setup_ret_unlink_pdu(&pdu_header, unlink);
295 usbip_header_correct_endian(&pdu_header, 1);
297 iov[0].iov_base = &pdu_header;
298 iov[0].iov_len = sizeof(pdu_header);
299 txsize += sizeof(pdu_header);
301 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
304 dev_err(&sdev->interface->dev,
305 "sendmsg failed!, retval %d for %zd\n",
307 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
312 dbg_stub_tx("send txdata\n");
314 total_size += txsize;
318 spin_lock_irqsave(&sdev->priv_lock, flags);
320 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
321 list_del(&unlink->list);
325 spin_unlock_irqrestore(&sdev->priv_lock, flags);
331 /*-------------------------------------------------------------------------*/
333 void stub_tx_loop(struct usbip_task *ut)
335 struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_tx);
336 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
339 if (signal_pending(current)) {
340 dbg_stub_tx("signal catched\n");
344 if (usbip_event_happend(ud))
348 * send_ret_submit comes earlier than send_ret_unlink. stub_rx
349 * looks at only priv_init queue. If the completion of a URB is
350 * earlier than the receive of CMD_UNLINK, priv is moved to
351 * priv_tx queue and stub_rx does not find the target priv. In
352 * this case, vhci_rx receives the result of the submit request
353 * and then receives the result of the unlink request. The
354 * result of the submit is given back to the usbcore as the
355 * completion of the unlink request. The request of the
356 * unlink is ignored. This is ok because a driver who calls
357 * usb_unlink_urb() understands the unlink was too late by
358 * getting the status of the given-backed URB which has the
359 * status of usb_submit_urb().
361 if (stub_send_ret_submit(sdev) < 0)
364 if (stub_send_ret_unlink(sdev) < 0)
367 wait_event_interruptible(sdev->tx_waitq,
368 (!list_empty(&sdev->priv_tx) ||
369 !list_empty(&sdev->unlink_tx)));