usb-storage: redo incorrect reads
[zen-stable.git] / drivers / staging / usbip / stub_tx.c
blobfda2bc95e85987d4be49ff8420f66bdcd2e02ac0
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 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);
73 switch (urb->status) {
74 case 0:
75 /* OK */
76 break;
77 case -ENOENT:
78 dev_info(&urb->dev->dev, "stopped by a call to usb_kill_urb() "
79 "because of cleaning up a virtual connection\n");
80 return;
81 case -ECONNRESET:
82 dev_info(&urb->dev->dev, "unlinked by a call to "
83 "usb_unlink_urb()\n");
84 break;
85 case -EPIPE:
86 dev_info(&urb->dev->dev, "endpoint %d is stalled\n",
87 usb_pipeendpoint(urb->pipe));
88 break;
89 case -ESHUTDOWN:
90 dev_info(&urb->dev->dev, "device removed?\n");
91 break;
92 default:
93 dev_info(&urb->dev->dev, "urb completion with non-zero status "
94 "%d\n", urb->status);
95 break;
98 /* link a urb to the queue of tx. */
99 spin_lock_irqsave(&sdev->priv_lock, flags);
101 if (priv->unlinking) {
102 stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
103 stub_free_priv_and_urb(priv);
104 } else
105 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);
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);
128 usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
131 static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
132 struct stub_unlink *unlink)
134 setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
135 rpdu->u.ret_unlink.status = unlink->status;
138 static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
140 unsigned long flags;
141 struct stub_priv *priv, *tmp;
143 spin_lock_irqsave(&sdev->priv_lock, flags);
145 list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
146 list_move_tail(&priv->list, &sdev->priv_free);
147 spin_unlock_irqrestore(&sdev->priv_lock, flags);
148 return priv;
151 spin_unlock_irqrestore(&sdev->priv_lock, flags);
153 return NULL;
156 static int stub_send_ret_submit(struct stub_device *sdev)
158 unsigned long flags;
159 struct stub_priv *priv, *tmp;
161 struct msghdr msg;
162 size_t txsize;
164 size_t total_size = 0;
166 while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
167 int ret;
168 struct urb *urb = priv->urb;
169 struct usbip_header pdu_header;
170 void *iso_buffer = NULL;
171 struct kvec *iov = NULL;
172 int iovnum = 0;
174 txsize = 0;
175 memset(&pdu_header, 0, sizeof(pdu_header));
176 memset(&msg, 0, sizeof(msg));
178 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
179 iovnum = 2 + urb->number_of_packets;
180 else
181 iovnum = 2;
183 iov = kzalloc(iovnum * sizeof(struct kvec), GFP_KERNEL);
185 if (!iov) {
186 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_MALLOC);
187 return -1;
190 iovnum = 0;
192 /* 1. setup usbip_header */
193 setup_ret_submit_pdu(&pdu_header, urb);
194 usbip_dbg_stub_tx("setup txdata seqnum: %d urb: %p\n",
195 pdu_header.base.seqnum, urb);
196 /*usbip_dump_header(pdu_header);*/
197 usbip_header_correct_endian(&pdu_header, 1);
199 iov[iovnum].iov_base = &pdu_header;
200 iov[iovnum].iov_len = sizeof(pdu_header);
201 iovnum++;
202 txsize += sizeof(pdu_header);
204 /* 2. setup transfer buffer */
205 if (usb_pipein(urb->pipe) &&
206 usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS &&
207 urb->actual_length > 0) {
208 iov[iovnum].iov_base = urb->transfer_buffer;
209 iov[iovnum].iov_len = urb->actual_length;
210 iovnum++;
211 txsize += urb->actual_length;
212 } else if (usb_pipein(urb->pipe) &&
213 usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
215 * For isochronous packets: actual length is the sum of
216 * the actual length of the individual, packets, but as
217 * the packet offsets are not changed there will be
218 * padding between the packets. To optimally use the
219 * bandwidth the padding is not transmitted.
222 int i;
223 for (i = 0; i < urb->number_of_packets; i++) {
224 iov[iovnum].iov_base = urb->transfer_buffer +
225 urb->iso_frame_desc[i].offset;
226 iov[iovnum].iov_len =
227 urb->iso_frame_desc[i].actual_length;
228 iovnum++;
229 txsize += urb->iso_frame_desc[i].actual_length;
232 if (txsize != sizeof(pdu_header) + urb->actual_length) {
233 dev_err(&sdev->interface->dev,
234 "actual length of urb %d does not "
235 "match iso packet sizes %lu\n",
236 urb->actual_length,
237 txsize-sizeof(pdu_header));
238 kfree(iov);
239 usbip_event_add(&sdev->ud,
240 SDEV_EVENT_ERROR_TCP);
241 return -1;
245 /* 3. setup iso_packet_descriptor */
246 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
247 ssize_t len = 0;
249 iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
250 if (!iso_buffer) {
251 usbip_event_add(&sdev->ud,
252 SDEV_EVENT_ERROR_MALLOC);
253 kfree(iov);
254 return -1;
257 iov[iovnum].iov_base = iso_buffer;
258 iov[iovnum].iov_len = len;
259 txsize += len;
260 iovnum++;
263 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg,
264 iov, iovnum, txsize);
265 if (ret != txsize) {
266 dev_err(&sdev->interface->dev,
267 "sendmsg failed!, retval %d for %zd\n",
268 ret, txsize);
269 kfree(iov);
270 kfree(iso_buffer);
271 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
272 return -1;
275 kfree(iov);
276 kfree(iso_buffer);
278 total_size += txsize;
281 spin_lock_irqsave(&sdev->priv_lock, flags);
282 list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
283 stub_free_priv_and_urb(priv);
285 spin_unlock_irqrestore(&sdev->priv_lock, flags);
287 return total_size;
290 static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
292 unsigned long flags;
293 struct stub_unlink *unlink, *tmp;
295 spin_lock_irqsave(&sdev->priv_lock, flags);
297 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
298 list_move_tail(&unlink->list, &sdev->unlink_free);
299 spin_unlock_irqrestore(&sdev->priv_lock, flags);
300 return unlink;
303 spin_unlock_irqrestore(&sdev->priv_lock, flags);
305 return NULL;
308 static int stub_send_ret_unlink(struct stub_device *sdev)
310 unsigned long flags;
311 struct stub_unlink *unlink, *tmp;
313 struct msghdr msg;
314 struct kvec iov[1];
315 size_t txsize;
317 size_t total_size = 0;
319 while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
320 int ret;
321 struct usbip_header pdu_header;
323 txsize = 0;
324 memset(&pdu_header, 0, sizeof(pdu_header));
325 memset(&msg, 0, sizeof(msg));
326 memset(&iov, 0, sizeof(iov));
328 usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
330 /* 1. setup usbip_header */
331 setup_ret_unlink_pdu(&pdu_header, unlink);
332 usbip_header_correct_endian(&pdu_header, 1);
334 iov[0].iov_base = &pdu_header;
335 iov[0].iov_len = sizeof(pdu_header);
336 txsize += sizeof(pdu_header);
338 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
339 1, txsize);
340 if (ret != txsize) {
341 dev_err(&sdev->interface->dev,
342 "sendmsg failed!, retval %d for %zd\n",
343 ret, txsize);
344 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
345 return -1;
348 usbip_dbg_stub_tx("send txdata\n");
349 total_size += txsize;
352 spin_lock_irqsave(&sdev->priv_lock, flags);
354 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
355 list_del(&unlink->list);
356 kfree(unlink);
359 spin_unlock_irqrestore(&sdev->priv_lock, flags);
361 return total_size;
364 int stub_tx_loop(void *data)
366 struct usbip_device *ud = data;
367 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
369 while (!kthread_should_stop()) {
370 if (usbip_event_happened(ud))
371 break;
374 * send_ret_submit comes earlier than send_ret_unlink. stub_rx
375 * looks at only priv_init queue. If the completion of a URB is
376 * earlier than the receive of CMD_UNLINK, priv is moved to
377 * priv_tx queue and stub_rx does not find the target priv. In
378 * this case, vhci_rx receives the result of the submit request
379 * and then receives the result of the unlink request. The
380 * result of the submit is given back to the usbcore as the
381 * completion of the unlink request. The request of the
382 * unlink is ignored. This is ok because a driver who calls
383 * usb_unlink_urb() understands the unlink was too late by
384 * getting the status of the given-backed URB which has the
385 * status of usb_submit_urb().
387 if (stub_send_ret_submit(sdev) < 0)
388 break;
390 if (stub_send_ret_unlink(sdev) < 0)
391 break;
393 wait_event_interruptible(sdev->tx_waitq,
394 (!list_empty(&sdev->priv_tx) ||
395 !list_empty(&sdev->unlink_tx) ||
396 kthread_should_stop()));
399 return 0;