GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / usbip / stub_tx.c
blobd7136e2c86faa16a1e64638047dc0109dca0d533
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 <linux/slab.h>
22 #include "usbip_common.h"
23 #include "stub.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);
34 usb_free_urb(urb);
37 /* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
38 void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
39 __u32 status)
41 struct stub_unlink *unlink;
43 unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC);
44 if (!unlink) {
45 dev_err(&sdev->interface->dev, "alloc stub_unlink\n");
46 usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
47 return;
50 unlink->seqnum = seqnum;
51 unlink->status = status;
53 list_add_tail(&unlink->list, &sdev->unlink_tx);
56 /**
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;
69 unsigned long flags;
71 usbip_dbg_stub_tx("complete! status %d\n", urb->status);
74 switch (urb->status) {
75 case 0:
76 /* OK */
77 break;
78 case -ENOENT:
79 usbip_uinfo("stopped by a call of usb_kill_urb() because of"
80 "cleaning up a virtual connection\n");
81 return;
82 case -ECONNRESET:
83 usbip_uinfo("unlinked by a call of usb_unlink_urb()\n");
84 break;
85 case -EPIPE:
86 usbip_uinfo("endpoint %d is stalled\n",
87 usb_pipeendpoint(urb->pipe));
88 break;
89 case -ESHUTDOWN:
90 usbip_uinfo("device removed?\n");
91 break;
92 default:
93 usbip_uinfo("urb completion with non-zero status %d\n",
94 urb->status);
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);
103 } else
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 /*-------------------------------------------------------------------------*/
115 /* fill PDU */
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;
122 base->devid = 0;
123 base->ep = 0;
124 base->direction = 0;
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)
150 unsigned long flags;
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);
158 return priv;
161 spin_unlock_irqrestore(&sdev->priv_lock, flags);
163 return NULL;
166 static int stub_send_ret_submit(struct stub_device *sdev)
168 unsigned long flags;
169 struct stub_priv *priv, *tmp;
171 struct msghdr msg;
172 struct kvec iov[3];
173 size_t txsize;
175 size_t total_size = 0;
177 while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
178 int ret;
179 struct urb *urb = priv->urb;
180 struct usbip_header pdu_header;
181 void *iso_buffer = NULL;
183 txsize = 0;
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) {
208 ssize_t len = 0;
210 iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
211 if (!iso_buffer) {
212 usbip_event_add(&sdev->ud,
213 SDEV_EVENT_ERROR_MALLOC);
214 return -1;
217 iov[2].iov_base = iso_buffer;
218 iov[2].iov_len = len;
219 txsize += len;
222 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
223 3, txsize);
224 if (ret != txsize) {
225 dev_err(&sdev->interface->dev,
226 "sendmsg failed!, retval %d for %zd\n",
227 ret, txsize);
228 kfree(iso_buffer);
229 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
230 return -1;
233 kfree(iso_buffer);
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);
248 return total_size;
252 /*-------------------------------------------------------------------------*/
253 /* send RET_UNLINK */
255 static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
257 unsigned long flags;
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);
265 return unlink;
268 spin_unlock_irqrestore(&sdev->priv_lock, flags);
270 return NULL;
274 static int stub_send_ret_unlink(struct stub_device *sdev)
276 unsigned long flags;
277 struct stub_unlink *unlink, *tmp;
279 struct msghdr msg;
280 struct kvec iov[1];
281 size_t txsize;
283 size_t total_size = 0;
285 while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
286 int ret;
287 struct usbip_header pdu_header;
289 txsize = 0;
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,
305 1, txsize);
306 if (ret != txsize) {
307 dev_err(&sdev->interface->dev,
308 "sendmsg failed!, retval %d for %zd\n",
309 ret, txsize);
310 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
311 return -1;
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);
325 kfree(unlink);
328 spin_unlock_irqrestore(&sdev->priv_lock, flags);
330 return total_size;
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);
341 while (1) {
342 if (signal_pending(current)) {
343 usbip_dbg_stub_tx("signal catched\n");
344 break;
347 if (usbip_event_happened(ud))
348 break;
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)
365 break;
367 if (stub_send_ret_unlink(sdev) < 0)
368 break;
370 wait_event_interruptible(sdev->tx_waitq,
371 (!list_empty(&sdev->priv_tx) ||
372 !list_empty(&sdev->unlink_tx)));