inet: frag: enforce memory limits earlier
[linux/fpc-iii.git] / drivers / usb / usbip / stub_tx.c
blob96aa375b80d9cc5d33b9f8780eb17bc3fc126280
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/kthread.h>
21 #include <linux/socket.h>
23 #include "usbip_common.h"
24 #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 urb->setup_packet = NULL;
33 kfree(urb->transfer_buffer);
34 urb->transfer_buffer = NULL;
36 list_del(&priv->list);
37 kmem_cache_free(stub_priv_cache, priv);
38 usb_free_urb(urb);
41 /* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
42 void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
43 __u32 status)
45 struct stub_unlink *unlink;
47 unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC);
48 if (!unlink) {
49 usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
50 return;
53 unlink->seqnum = seqnum;
54 unlink->status = status;
56 list_add_tail(&unlink->list, &sdev->unlink_tx);
59 /**
60 * stub_complete - completion handler of a usbip urb
61 * @urb: pointer to the urb completed
63 * When a urb has completed, the USB core driver calls this function mostly in
64 * the interrupt context. To return the result of a urb, the completed urb is
65 * linked to the pending list of returning.
68 void stub_complete(struct urb *urb)
70 struct stub_priv *priv = (struct stub_priv *) urb->context;
71 struct stub_device *sdev = priv->sdev;
72 unsigned long flags;
74 usbip_dbg_stub_tx("complete! status %d\n", urb->status);
76 switch (urb->status) {
77 case 0:
78 /* OK */
79 break;
80 case -ENOENT:
81 dev_info(&urb->dev->dev,
82 "stopped by a call to usb_kill_urb() because of cleaning up a virtual connection\n");
83 return;
84 case -ECONNRESET:
85 dev_info(&urb->dev->dev,
86 "unlinked by a call to usb_unlink_urb()\n");
87 break;
88 case -EPIPE:
89 dev_info(&urb->dev->dev, "endpoint %d is stalled\n",
90 usb_pipeendpoint(urb->pipe));
91 break;
92 case -ESHUTDOWN:
93 dev_info(&urb->dev->dev, "device removed?\n");
94 break;
95 default:
96 dev_info(&urb->dev->dev,
97 "urb completion with non-zero status %d\n",
98 urb->status);
99 break;
102 /* link a urb to the queue of tx. */
103 spin_lock_irqsave(&sdev->priv_lock, flags);
104 if (sdev->ud.tcp_socket == NULL) {
105 usbip_dbg_stub_tx("ignore urb for closed connection\n");
106 /* It will be freed in stub_device_cleanup_urbs(). */
107 } else if (priv->unlinking) {
108 stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
109 stub_free_priv_and_urb(priv);
110 } else {
111 list_move_tail(&priv->list, &sdev->priv_tx);
113 spin_unlock_irqrestore(&sdev->priv_lock, flags);
115 /* wake up tx_thread */
116 wake_up(&sdev->tx_waitq);
119 static inline void setup_base_pdu(struct usbip_header_basic *base,
120 __u32 command, __u32 seqnum)
122 base->command = command;
123 base->seqnum = seqnum;
124 base->devid = 0;
125 base->ep = 0;
126 base->direction = 0;
129 static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
131 struct stub_priv *priv = (struct stub_priv *) urb->context;
133 setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
134 usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
137 static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
138 struct stub_unlink *unlink)
140 setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
141 rpdu->u.ret_unlink.status = unlink->status;
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 size_t txsize;
170 size_t total_size = 0;
172 while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
173 int ret;
174 struct urb *urb = priv->urb;
175 struct usbip_header pdu_header;
176 struct usbip_iso_packet_descriptor *iso_buffer = NULL;
177 struct kvec *iov = NULL;
178 int iovnum = 0;
180 txsize = 0;
181 memset(&pdu_header, 0, sizeof(pdu_header));
182 memset(&msg, 0, sizeof(msg));
184 if (urb->actual_length > 0 && !urb->transfer_buffer) {
185 dev_err(&sdev->udev->dev,
186 "urb: actual_length %d transfer_buffer null\n",
187 urb->actual_length);
188 return -1;
191 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
192 iovnum = 2 + urb->number_of_packets;
193 else
194 iovnum = 2;
196 iov = kcalloc(iovnum, sizeof(struct kvec), GFP_KERNEL);
198 if (!iov) {
199 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_MALLOC);
200 return -1;
203 iovnum = 0;
205 /* 1. setup usbip_header */
206 setup_ret_submit_pdu(&pdu_header, urb);
207 usbip_dbg_stub_tx("setup txdata seqnum: %d\n",
208 pdu_header.base.seqnum);
209 usbip_header_correct_endian(&pdu_header, 1);
211 iov[iovnum].iov_base = &pdu_header;
212 iov[iovnum].iov_len = sizeof(pdu_header);
213 iovnum++;
214 txsize += sizeof(pdu_header);
216 /* 2. setup transfer buffer */
217 if (usb_pipein(urb->pipe) &&
218 usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS &&
219 urb->actual_length > 0) {
220 iov[iovnum].iov_base = urb->transfer_buffer;
221 iov[iovnum].iov_len = urb->actual_length;
222 iovnum++;
223 txsize += urb->actual_length;
224 } else if (usb_pipein(urb->pipe) &&
225 usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
227 * For isochronous packets: actual length is the sum of
228 * the actual length of the individual, packets, but as
229 * the packet offsets are not changed there will be
230 * padding between the packets. To optimally use the
231 * bandwidth the padding is not transmitted.
234 int i;
236 for (i = 0; i < urb->number_of_packets; i++) {
237 iov[iovnum].iov_base = urb->transfer_buffer +
238 urb->iso_frame_desc[i].offset;
239 iov[iovnum].iov_len =
240 urb->iso_frame_desc[i].actual_length;
241 iovnum++;
242 txsize += urb->iso_frame_desc[i].actual_length;
245 if (txsize != sizeof(pdu_header) + urb->actual_length) {
246 dev_err(&sdev->udev->dev,
247 "actual length of urb %d does not match iso packet sizes %zu\n",
248 urb->actual_length,
249 txsize-sizeof(pdu_header));
250 kfree(iov);
251 usbip_event_add(&sdev->ud,
252 SDEV_EVENT_ERROR_TCP);
253 return -1;
257 /* 3. setup iso_packet_descriptor */
258 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
259 ssize_t len = 0;
261 iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
262 if (!iso_buffer) {
263 usbip_event_add(&sdev->ud,
264 SDEV_EVENT_ERROR_MALLOC);
265 kfree(iov);
266 return -1;
269 iov[iovnum].iov_base = iso_buffer;
270 iov[iovnum].iov_len = len;
271 txsize += len;
272 iovnum++;
275 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg,
276 iov, iovnum, txsize);
277 if (ret != txsize) {
278 dev_err(&sdev->udev->dev,
279 "sendmsg failed!, retval %d for %zd\n",
280 ret, txsize);
281 kfree(iov);
282 kfree(iso_buffer);
283 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
284 return -1;
287 kfree(iov);
288 kfree(iso_buffer);
290 total_size += txsize;
293 spin_lock_irqsave(&sdev->priv_lock, flags);
294 list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
295 stub_free_priv_and_urb(priv);
297 spin_unlock_irqrestore(&sdev->priv_lock, flags);
299 return total_size;
302 static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
304 unsigned long flags;
305 struct stub_unlink *unlink, *tmp;
307 spin_lock_irqsave(&sdev->priv_lock, flags);
309 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
310 list_move_tail(&unlink->list, &sdev->unlink_free);
311 spin_unlock_irqrestore(&sdev->priv_lock, flags);
312 return unlink;
315 spin_unlock_irqrestore(&sdev->priv_lock, flags);
317 return NULL;
320 static int stub_send_ret_unlink(struct stub_device *sdev)
322 unsigned long flags;
323 struct stub_unlink *unlink, *tmp;
325 struct msghdr msg;
326 struct kvec iov[1];
327 size_t txsize;
329 size_t total_size = 0;
331 while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
332 int ret;
333 struct usbip_header pdu_header;
335 txsize = 0;
336 memset(&pdu_header, 0, sizeof(pdu_header));
337 memset(&msg, 0, sizeof(msg));
338 memset(&iov, 0, sizeof(iov));
340 usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
342 /* 1. setup usbip_header */
343 setup_ret_unlink_pdu(&pdu_header, unlink);
344 usbip_header_correct_endian(&pdu_header, 1);
346 iov[0].iov_base = &pdu_header;
347 iov[0].iov_len = sizeof(pdu_header);
348 txsize += sizeof(pdu_header);
350 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
351 1, txsize);
352 if (ret != txsize) {
353 dev_err(&sdev->udev->dev,
354 "sendmsg failed!, retval %d for %zd\n",
355 ret, txsize);
356 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
357 return -1;
360 usbip_dbg_stub_tx("send txdata\n");
361 total_size += txsize;
364 spin_lock_irqsave(&sdev->priv_lock, flags);
366 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
367 list_del(&unlink->list);
368 kfree(unlink);
371 spin_unlock_irqrestore(&sdev->priv_lock, flags);
373 return total_size;
376 int stub_tx_loop(void *data)
378 struct usbip_device *ud = data;
379 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
381 while (!kthread_should_stop()) {
382 if (usbip_event_happened(ud))
383 break;
386 * send_ret_submit comes earlier than send_ret_unlink. stub_rx
387 * looks at only priv_init queue. If the completion of a URB is
388 * earlier than the receive of CMD_UNLINK, priv is moved to
389 * priv_tx queue and stub_rx does not find the target priv. In
390 * this case, vhci_rx receives the result of the submit request
391 * and then receives the result of the unlink request. The
392 * result of the submit is given back to the usbcore as the
393 * completion of the unlink request. The request of the
394 * unlink is ignored. This is ok because a driver who calls
395 * usb_unlink_urb() understands the unlink was too late by
396 * getting the status of the given-backed URB which has the
397 * status of usb_submit_urb().
399 if (stub_send_ret_submit(sdev) < 0)
400 break;
402 if (stub_send_ret_unlink(sdev) < 0)
403 break;
405 wait_event_interruptible(sdev->tx_waitq,
406 (!list_empty(&sdev->priv_tx) ||
407 !list_empty(&sdev->unlink_tx) ||
408 kthread_should_stop()));
411 return 0;