On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / drivers / staging / usbip / stub_tx.c
blobe2ab4f3fdac236e2464b43a9e84c51756361e068
1 /*
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,
17 * USA.
20 #include "usbip_common.h"
21 #include "stub.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);
32 usb_free_urb(urb);
35 /* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
36 void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
37 __u32 status)
39 struct stub_unlink *unlink;
41 unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC);
42 if (!unlink) {
43 dev_err(&sdev->interface->dev, "alloc stub_unlink\n");
44 usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
45 return;
48 unlink->seqnum = seqnum;
49 unlink->status = status;
51 list_add_tail(&unlink->list, &sdev->unlink_tx);
54 /**
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;
67 unsigned long flags;
69 usbip_dbg_stub_tx("complete! status %d\n", urb->status);
72 switch (urb->status) {
73 case 0:
74 /* OK */
75 break;
76 case -ENOENT:
77 usbip_uinfo("stopped by a call of usb_kill_urb() because of"
78 "cleaning up a virtual connection\n");
79 return;
80 case -ECONNRESET:
81 usbip_uinfo("unlinked by a call of usb_unlink_urb()\n");
82 break;
83 case -EPIPE:
84 usbip_uinfo("endpoint %d is stalled\n",
85 usb_pipeendpoint(urb->pipe));
86 break;
87 case -ESHUTDOWN:
88 usbip_uinfo("device removed?\n");
89 break;
90 default:
91 usbip_uinfo("urb completion with non-zero status %d\n",
92 urb->status);
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);
101 } else
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 /*-------------------------------------------------------------------------*/
113 /* fill PDU */
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;
120 base->devid = 0;
121 base->ep = 0;
122 base->direction = 0;
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)
148 unsigned long flags;
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);
156 return priv;
159 spin_unlock_irqrestore(&sdev->priv_lock, flags);
161 return NULL;
164 static int stub_send_ret_submit(struct stub_device *sdev)
166 unsigned long flags;
167 struct stub_priv *priv, *tmp;
169 struct msghdr msg;
170 struct kvec iov[3];
171 size_t txsize;
173 size_t total_size = 0;
175 while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
176 int ret;
177 struct urb *urb = priv->urb;
178 struct usbip_header pdu_header;
179 void *iso_buffer = NULL;
181 txsize = 0;
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) {
206 ssize_t len = 0;
208 iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
209 if (!iso_buffer) {
210 usbip_event_add(&sdev->ud,
211 SDEV_EVENT_ERROR_MALLOC);
212 return -1;
215 iov[2].iov_base = iso_buffer;
216 iov[2].iov_len = len;
217 txsize += len;
220 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
221 3, txsize);
222 if (ret != txsize) {
223 dev_err(&sdev->interface->dev,
224 "sendmsg failed!, retval %d for %zd\n",
225 ret, txsize);
226 kfree(iso_buffer);
227 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
228 return -1;
231 kfree(iso_buffer);
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);
246 return total_size;
250 /*-------------------------------------------------------------------------*/
251 /* send RET_UNLINK */
253 static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
255 unsigned long flags;
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);
263 return unlink;
266 spin_unlock_irqrestore(&sdev->priv_lock, flags);
268 return NULL;
272 static int stub_send_ret_unlink(struct stub_device *sdev)
274 unsigned long flags;
275 struct stub_unlink *unlink, *tmp;
277 struct msghdr msg;
278 struct kvec iov[1];
279 size_t txsize;
281 size_t total_size = 0;
283 while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
284 int ret;
285 struct usbip_header pdu_header;
287 txsize = 0;
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,
303 1, txsize);
304 if (ret != txsize) {
305 dev_err(&sdev->interface->dev,
306 "sendmsg failed!, retval %d for %zd\n",
307 ret, txsize);
308 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
309 return -1;
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);
323 kfree(unlink);
326 spin_unlock_irqrestore(&sdev->priv_lock, flags);
328 return total_size;
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);
339 while (1) {
340 if (signal_pending(current)) {
341 usbip_dbg_stub_tx("signal catched\n");
342 break;
345 if (usbip_event_happened(ud))
346 break;
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)
363 break;
365 if (stub_send_ret_unlink(sdev) < 0)
366 break;
368 wait_event_interruptible(sdev->tx_waitq,
369 (!list_empty(&sdev->priv_tx) ||
370 !list_empty(&sdev->unlink_tx)));