Merge branch 'rmobile-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / staging / usbip / stub_tx.c
1 /*
2  * Copyright (C) 2003-2008 Takahiro Hirofuchi
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <linux/slab.h>
21 #include <linux/kthread.h>
22
23 #include "usbip_common.h"
24 #include "stub.h"
25
26
27 static void stub_free_priv_and_urb(struct stub_priv *priv)
28 {
29         struct urb *urb = priv->urb;
30
31         kfree(urb->setup_packet);
32         kfree(urb->transfer_buffer);
33         list_del(&priv->list);
34         kmem_cache_free(stub_priv_cache, priv);
35         usb_free_urb(urb);
36 }
37
38 /* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
39 void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
40                              __u32 status)
41 {
42         struct stub_unlink *unlink;
43
44         unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC);
45         if (!unlink) {
46                 dev_err(&sdev->interface->dev, "alloc stub_unlink\n");
47                 usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
48                 return;
49         }
50
51         unlink->seqnum = seqnum;
52         unlink->status = status;
53
54         list_add_tail(&unlink->list, &sdev->unlink_tx);
55 }
56
57 /**
58  * stub_complete - completion handler of a usbip urb
59  * @urb: pointer to the urb completed
60  *
61  * When a urb has completed, the USB core driver calls this function mostly in
62  * the interrupt context. To return the result of a urb, the completed urb is
63  * linked to the pending list of returning.
64  *
65  */
66 void stub_complete(struct urb *urb)
67 {
68         struct stub_priv *priv = (struct stub_priv *) urb->context;
69         struct stub_device *sdev = priv->sdev;
70         unsigned long flags;
71
72         usbip_dbg_stub_tx("complete! status %d\n", urb->status);
73
74
75         switch (urb->status) {
76         case 0:
77                 /* OK */
78                 break;
79         case -ENOENT:
80                 usbip_uinfo("stopped by a call of usb_kill_urb() because of"
81                                         "cleaning up a virtual connection\n");
82                 return;
83         case -ECONNRESET:
84                 usbip_uinfo("unlinked by a call of usb_unlink_urb()\n");
85                 break;
86         case -EPIPE:
87                 usbip_uinfo("endpoint %d is stalled\n",
88                                                 usb_pipeendpoint(urb->pipe));
89                 break;
90         case -ESHUTDOWN:
91                 usbip_uinfo("device removed?\n");
92                 break;
93         default:
94                 usbip_uinfo("urb completion with non-zero status %d\n",
95                                                         urb->status);
96         }
97
98         /* link a urb to the queue of tx. */
99         spin_lock_irqsave(&sdev->priv_lock, flags);
100
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);
106
107
108         spin_unlock_irqrestore(&sdev->priv_lock, flags);
109
110         /* wake up tx_thread */
111         wake_up(&sdev->tx_waitq);
112 }
113
114
115 /*-------------------------------------------------------------------------*/
116 /* fill PDU */
117
118 static inline void setup_base_pdu(struct usbip_header_basic *base,
119                 __u32 command, __u32 seqnum)
120 {
121         base->command = command;
122         base->seqnum  = seqnum;
123         base->devid   = 0;
124         base->ep      = 0;
125         base->direction   = 0;
126 }
127
128 static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
129 {
130         struct stub_priv *priv = (struct stub_priv *) urb->context;
131
132         setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
133
134         usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
135 }
136
137 static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
138                 struct stub_unlink *unlink)
139 {
140         setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
141
142         rpdu->u.ret_unlink.status = unlink->status;
143 }
144
145
146 /*-------------------------------------------------------------------------*/
147 /* send RET_SUBMIT */
148
149 static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
150 {
151         unsigned long flags;
152         struct stub_priv *priv, *tmp;
153
154         spin_lock_irqsave(&sdev->priv_lock, flags);
155
156         list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
157                 list_move_tail(&priv->list, &sdev->priv_free);
158                 spin_unlock_irqrestore(&sdev->priv_lock, flags);
159                 return priv;
160         }
161
162         spin_unlock_irqrestore(&sdev->priv_lock, flags);
163
164         return NULL;
165 }
166
167 static int stub_send_ret_submit(struct stub_device *sdev)
168 {
169         unsigned long flags;
170         struct stub_priv *priv, *tmp;
171
172         struct msghdr msg;
173         size_t txsize;
174
175         size_t total_size = 0;
176
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;
182                 struct kvec *iov = NULL;
183                 int iovnum = 0;
184
185                 txsize = 0;
186                 memset(&pdu_header, 0, sizeof(pdu_header));
187                 memset(&msg, 0, sizeof(msg));
188
189                 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
190                         iovnum = 2 + urb->number_of_packets;
191                 else
192                         iovnum = 2;
193
194                 iov = kzalloc(iovnum * sizeof(struct kvec), GFP_KERNEL);
195
196                 if (!iov) {
197                         usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_MALLOC);
198                         return -1;
199                 }
200
201                 iovnum = 0;
202
203                 /* 1. setup usbip_header */
204                 setup_ret_submit_pdu(&pdu_header, urb);
205                 usbip_dbg_stub_tx("setup txdata seqnum: %d urb: %p\n",
206                                                 pdu_header.base.seqnum, urb);
207                 /*usbip_dump_header(pdu_header);*/
208                 usbip_header_correct_endian(&pdu_header, 1);
209
210                 iov[iovnum].iov_base = &pdu_header;
211                 iov[iovnum].iov_len  = sizeof(pdu_header);
212                 iovnum++;
213                 txsize += sizeof(pdu_header);
214
215                 /* 2. setup transfer buffer */
216                 if (usb_pipein(urb->pipe) &&
217                                 usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS &&
218                                         urb->actual_length > 0) {
219                         iov[iovnum].iov_base = urb->transfer_buffer;
220                         iov[iovnum].iov_len  = urb->actual_length;
221                         iovnum++;
222                         txsize += urb->actual_length;
223                 } else if (usb_pipein(urb->pipe) &&
224                                 usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
225                         /*
226                          * For isochronous packets: actual length is the sum of
227                          * the actual length of the individual, packets, but as
228                          * the packet offsets are not changed there will be
229                          * padding between the packets. To optimally use the
230                          * bandwidth the padding is not transmitted.
231                          */
232
233                         int i;
234                         for (i = 0; i < urb->number_of_packets; i++) {
235                                 iov[iovnum].iov_base = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
236                                 iov[iovnum].iov_len = urb->iso_frame_desc[i].actual_length;
237                                 iovnum++;
238                                 txsize += urb->iso_frame_desc[i].actual_length;
239                         }
240
241                         if (txsize != sizeof(pdu_header) + urb->actual_length) {
242                                 dev_err(&sdev->interface->dev,
243                                         "actual length of urb (%d) does not match iso packet sizes (%d)\n",
244                                         urb->actual_length, txsize-sizeof(pdu_header));
245                                 kfree(iov);
246                                 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
247                            return -1;
248                         }
249                 }
250
251                 /* 3. setup iso_packet_descriptor */
252                 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
253                         ssize_t len = 0;
254
255                         iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
256                         if (!iso_buffer) {
257                                 usbip_event_add(&sdev->ud,
258                                                 SDEV_EVENT_ERROR_MALLOC);
259                                 kfree(iov);
260                                 return -1;
261                         }
262
263                         iov[iovnum].iov_base = iso_buffer;
264                         iov[iovnum].iov_len  = len;
265                         txsize += len;
266                         iovnum++;
267                 }
268
269                 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg,
270                                                 iov,  iovnum, txsize);
271                 if (ret != txsize) {
272                         dev_err(&sdev->interface->dev,
273                                 "sendmsg failed!, retval %d for %zd\n",
274                                 ret, txsize);
275                         kfree(iov);
276                         kfree(iso_buffer);
277                         usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
278                         return -1;
279                 }
280
281                 kfree(iov);
282                 kfree(iso_buffer);
283
284                 total_size += txsize;
285         }
286
287         spin_lock_irqsave(&sdev->priv_lock, flags);
288
289         list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
290                 stub_free_priv_and_urb(priv);
291         }
292
293         spin_unlock_irqrestore(&sdev->priv_lock, flags);
294
295         return total_size;
296 }
297
298
299 /*-------------------------------------------------------------------------*/
300 /* send RET_UNLINK */
301
302 static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
303 {
304         unsigned long flags;
305         struct stub_unlink *unlink, *tmp;
306
307         spin_lock_irqsave(&sdev->priv_lock, flags);
308
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;
313         }
314
315         spin_unlock_irqrestore(&sdev->priv_lock, flags);
316
317         return NULL;
318 }
319
320
321 static int stub_send_ret_unlink(struct stub_device *sdev)
322 {
323         unsigned long flags;
324         struct stub_unlink *unlink, *tmp;
325
326         struct msghdr msg;
327         struct kvec iov[1];
328         size_t txsize;
329
330         size_t total_size = 0;
331
332         while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
333                 int ret;
334                 struct usbip_header pdu_header;
335
336                 txsize = 0;
337                 memset(&pdu_header, 0, sizeof(pdu_header));
338                 memset(&msg, 0, sizeof(msg));
339                 memset(&iov, 0, sizeof(iov));
340
341                 usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
342
343                 /* 1. setup usbip_header */
344                 setup_ret_unlink_pdu(&pdu_header, unlink);
345                 usbip_header_correct_endian(&pdu_header, 1);
346
347                 iov[0].iov_base = &pdu_header;
348                 iov[0].iov_len  = sizeof(pdu_header);
349                 txsize += sizeof(pdu_header);
350
351                 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
352                                      1, txsize);
353                 if (ret != txsize) {
354                         dev_err(&sdev->interface->dev,
355                                 "sendmsg failed!, retval %d for %zd\n",
356                                 ret, txsize);
357                         usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
358                         return -1;
359                 }
360
361
362                 usbip_dbg_stub_tx("send txdata\n");
363
364                 total_size += txsize;
365         }
366
367
368         spin_lock_irqsave(&sdev->priv_lock, flags);
369
370         list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
371                 list_del(&unlink->list);
372                 kfree(unlink);
373         }
374
375         spin_unlock_irqrestore(&sdev->priv_lock, flags);
376
377         return total_size;
378 }
379
380
381 /*-------------------------------------------------------------------------*/
382
383 int stub_tx_loop(void *data)
384 {
385         struct usbip_device *ud = data;
386         struct stub_device *sdev = container_of(ud, struct stub_device, ud);
387
388         while (!kthread_should_stop()) {
389                 if (usbip_event_happened(ud))
390                         break;
391
392                 /*
393                  * send_ret_submit comes earlier than send_ret_unlink.  stub_rx
394                  * looks at only priv_init queue. If the completion of a URB is
395                  * earlier than the receive of CMD_UNLINK, priv is moved to
396                  * priv_tx queue and stub_rx does not find the target priv. In
397                  * this case, vhci_rx receives the result of the submit request
398                  * and then receives the result of the unlink request. The
399                  * result of the submit is given back to the usbcore as the
400                  * completion of the unlink request. The request of the
401                  * unlink is ignored. This is ok because a driver who calls
402                  * usb_unlink_urb() understands the unlink was too late by
403                  * getting the status of the given-backed URB which has the
404                  * status of usb_submit_urb().
405                  */
406                 if (stub_send_ret_submit(sdev) < 0)
407                         break;
408
409                 if (stub_send_ret_unlink(sdev) < 0)
410                         break;
411
412                 wait_event_interruptible(sdev->tx_waitq,
413                                 (!list_empty(&sdev->priv_tx) ||
414                                  !list_empty(&sdev->unlink_tx) ||
415                                  kthread_should_stop()));
416         }
417
418         return 0;
419 }