added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / staging / usbip / stub_tx.c
blob78058f5362b4c7811d666bbd3e19bf3c2995a7a4
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 dbg_stub_tx("complete! status %d\n", urb->status);
72 switch (urb->status) {
73 case 0:
74 /* OK */
75 break;
76 case -ENOENT:
77 uinfo("stopped by a call of usb_kill_urb() because of"
78 "cleaning up a virtual connection\n");
79 return;
80 case -ECONNRESET:
81 uinfo("unlinked by a call of usb_unlink_urb()\n");
82 break;
83 case -EPIPE:
84 uinfo("endpoint %d is stalled\n", usb_pipeendpoint(urb->pipe));
85 break;
86 case -ESHUTDOWN:
87 uinfo("device removed?\n");
88 break;
89 default:
90 uinfo("urb completion with non-zero status %d\n", urb->status);
93 /* link a urb to the queue of tx. */
94 spin_lock_irqsave(&sdev->priv_lock, flags);
96 if (priv->unlinking) {
97 stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
98 stub_free_priv_and_urb(priv);
99 } else
100 list_move_tail(&priv->list, &sdev->priv_tx);
103 spin_unlock_irqrestore(&sdev->priv_lock, flags);
105 /* wake up tx_thread */
106 wake_up(&sdev->tx_waitq);
110 /*-------------------------------------------------------------------------*/
111 /* fill PDU */
113 static inline void setup_base_pdu(struct usbip_header_basic *base,
114 __u32 command, __u32 seqnum)
116 base->command = command;
117 base->seqnum = seqnum;
118 base->devid = 0;
119 base->ep = 0;
120 base->direction = 0;
123 static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
125 struct stub_priv *priv = (struct stub_priv *) urb->context;
127 setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
129 usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
132 static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
133 struct stub_unlink *unlink)
135 setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
137 rpdu->u.ret_unlink.status = unlink->status;
141 /*-------------------------------------------------------------------------*/
142 /* send RET_SUBMIT */
144 static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
146 unsigned long flags;
147 struct stub_priv *priv, *tmp;
149 spin_lock_irqsave(&sdev->priv_lock, flags);
151 list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
152 list_move_tail(&priv->list, &sdev->priv_free);
153 spin_unlock_irqrestore(&sdev->priv_lock, flags);
154 return priv;
157 spin_unlock_irqrestore(&sdev->priv_lock, flags);
159 return NULL;
162 static int stub_send_ret_submit(struct stub_device *sdev)
164 unsigned long flags;
165 struct stub_priv *priv, *tmp;
167 struct msghdr msg;
168 struct kvec iov[3];
169 size_t txsize;
171 size_t total_size = 0;
173 while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
174 int ret;
175 struct urb *urb = priv->urb;
176 struct usbip_header pdu_header;
177 void *iso_buffer = NULL;
179 txsize = 0;
180 memset(&pdu_header, 0, sizeof(pdu_header));
181 memset(&msg, 0, sizeof(msg));
182 memset(&iov, 0, sizeof(iov));
184 dbg_stub_tx("setup txdata urb %p\n", urb);
187 /* 1. setup usbip_header */
188 setup_ret_submit_pdu(&pdu_header, urb);
189 usbip_header_correct_endian(&pdu_header, 1);
191 iov[0].iov_base = &pdu_header;
192 iov[0].iov_len = sizeof(pdu_header);
193 txsize += sizeof(pdu_header);
195 /* 2. setup transfer buffer */
196 if (usb_pipein(urb->pipe) && urb->actual_length > 0) {
197 iov[1].iov_base = urb->transfer_buffer;
198 iov[1].iov_len = urb->actual_length;
199 txsize += urb->actual_length;
202 /* 3. setup iso_packet_descriptor */
203 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
204 ssize_t len = 0;
206 iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
207 if (!iso_buffer) {
208 usbip_event_add(&sdev->ud,
209 SDEV_EVENT_ERROR_MALLOC);
210 return -1;
213 iov[2].iov_base = iso_buffer;
214 iov[2].iov_len = len;
215 txsize += len;
218 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
219 3, txsize);
220 if (ret != txsize) {
221 dev_err(&sdev->interface->dev,
222 "sendmsg failed!, retval %d for %zd\n",
223 ret, txsize);
224 kfree(iso_buffer);
225 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
226 return -1;
229 kfree(iso_buffer);
230 dbg_stub_tx("send txdata\n");
232 total_size += txsize;
236 spin_lock_irqsave(&sdev->priv_lock, flags);
238 list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
239 stub_free_priv_and_urb(priv);
242 spin_unlock_irqrestore(&sdev->priv_lock, flags);
244 return total_size;
248 /*-------------------------------------------------------------------------*/
249 /* send RET_UNLINK */
251 static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
253 unsigned long flags;
254 struct stub_unlink *unlink, *tmp;
256 spin_lock_irqsave(&sdev->priv_lock, flags);
258 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
259 list_move_tail(&unlink->list, &sdev->unlink_free);
260 spin_unlock_irqrestore(&sdev->priv_lock, flags);
261 return unlink;
264 spin_unlock_irqrestore(&sdev->priv_lock, flags);
266 return NULL;
270 static int stub_send_ret_unlink(struct stub_device *sdev)
272 unsigned long flags;
273 struct stub_unlink *unlink, *tmp;
275 struct msghdr msg;
276 struct kvec iov[1];
277 size_t txsize;
279 size_t total_size = 0;
281 while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
282 int ret;
283 struct usbip_header pdu_header;
285 txsize = 0;
286 memset(&pdu_header, 0, sizeof(pdu_header));
287 memset(&msg, 0, sizeof(msg));
288 memset(&iov, 0, sizeof(iov));
290 dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
292 /* 1. setup usbip_header */
293 setup_ret_unlink_pdu(&pdu_header, unlink);
294 usbip_header_correct_endian(&pdu_header, 1);
296 iov[0].iov_base = &pdu_header;
297 iov[0].iov_len = sizeof(pdu_header);
298 txsize += sizeof(pdu_header);
300 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
301 1, txsize);
302 if (ret != txsize) {
303 dev_err(&sdev->interface->dev,
304 "sendmsg failed!, retval %d for %zd\n",
305 ret, txsize);
306 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
307 return -1;
311 dbg_stub_tx("send txdata\n");
313 total_size += txsize;
317 spin_lock_irqsave(&sdev->priv_lock, flags);
319 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
320 list_del(&unlink->list);
321 kfree(unlink);
324 spin_unlock_irqrestore(&sdev->priv_lock, flags);
326 return total_size;
330 /*-------------------------------------------------------------------------*/
332 void stub_tx_loop(struct usbip_task *ut)
334 struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_tx);
335 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
337 while (1) {
338 if (signal_pending(current)) {
339 dbg_stub_tx("signal catched\n");
340 break;
343 if (usbip_event_happend(ud))
344 break;
347 * send_ret_submit comes earlier than send_ret_unlink. stub_rx
348 * looks at only priv_init queue. If the completion of a URB is
349 * earlier than the receive of CMD_UNLINK, priv is moved to
350 * priv_tx queue and stub_rx does not find the target priv. In
351 * this case, vhci_rx receives the result of the submit request
352 * and then receives the result of the unlink request. The
353 * result of the submit is given back to the usbcore as the
354 * completion of the unlink request. The request of the
355 * unlink is ignored. This is ok because a driver who calls
356 * usb_unlink_urb() understands the unlink was too late by
357 * getting the status of the given-backed URB which has the
358 * status of usb_submit_urb().
360 if (stub_send_ret_submit(sdev) < 0)
361 break;
363 if (stub_send_ret_unlink(sdev) < 0)
364 break;
366 wait_event_interruptible(sdev->tx_waitq,
367 (!list_empty(&sdev->priv_tx) ||
368 !list_empty(&sdev->unlink_tx)));