upload tizen1.0 source
[kernel/linux-2.6.36.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
22 #include "usbip_common.h"
23 #include "stub.h"
24
25
26 static void stub_free_priv_and_urb(struct stub_priv *priv)
27 {
28         struct urb *urb = priv->urb;
29
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);
35 }
36
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)
40 {
41         struct stub_unlink *unlink;
42
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;
48         }
49
50         unlink->seqnum = seqnum;
51         unlink->status = status;
52
53         list_add_tail(&unlink->list, &sdev->unlink_tx);
54 }
55
56 /**
57  * stub_complete - completion handler of a usbip urb
58  * @urb: pointer to the urb completed
59  *
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.
63  *
64  */
65 void stub_complete(struct urb *urb)
66 {
67         struct stub_priv *priv = (struct stub_priv *) urb->context;
68         struct stub_device *sdev = priv->sdev;
69         unsigned long flags;
70
71         usbip_dbg_stub_tx("complete! status %d\n", urb->status);
72
73
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);
95         }
96
97         /* link a urb to the queue of tx. */
98         spin_lock_irqsave(&sdev->priv_lock, flags);
99
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);
105
106
107         spin_unlock_irqrestore(&sdev->priv_lock, flags);
108
109         /* wake up tx_thread */
110         wake_up(&sdev->tx_waitq);
111 }
112
113
114 /*-------------------------------------------------------------------------*/
115 /* fill PDU */
116
117 static inline void setup_base_pdu(struct usbip_header_basic *base,
118                 __u32 command, __u32 seqnum)
119 {
120         base->command = command;
121         base->seqnum  = seqnum;
122         base->devid   = 0;
123         base->ep      = 0;
124         base->direction   = 0;
125 }
126
127 static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
128 {
129         struct stub_priv *priv = (struct stub_priv *) urb->context;
130
131         setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
132
133         usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
134 }
135
136 static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
137                 struct stub_unlink *unlink)
138 {
139         setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
140
141         rpdu->u.ret_unlink.status = unlink->status;
142 }
143
144
145 /*-------------------------------------------------------------------------*/
146 /* send RET_SUBMIT */
147
148 static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
149 {
150         unsigned long flags;
151         struct stub_priv *priv, *tmp;
152
153         spin_lock_irqsave(&sdev->priv_lock, flags);
154
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;
159         }
160
161         spin_unlock_irqrestore(&sdev->priv_lock, flags);
162
163         return NULL;
164 }
165
166 static int stub_send_ret_submit(struct stub_device *sdev)
167 {
168         unsigned long flags;
169         struct stub_priv *priv, *tmp;
170
171         struct msghdr msg;
172         struct kvec iov[3];
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
183                 txsize = 0;
184                 memset(&pdu_header, 0, sizeof(pdu_header));
185                 memset(&msg, 0, sizeof(msg));
186                 memset(&iov, 0, sizeof(iov));
187
188                 usbip_dbg_stub_tx("setup txdata urb %p\n", urb);
189
190
191                 /* 1. setup usbip_header */
192                 setup_ret_submit_pdu(&pdu_header, urb);
193                 usbip_header_correct_endian(&pdu_header, 1);
194
195                 iov[0].iov_base = &pdu_header;
196                 iov[0].iov_len  = sizeof(pdu_header);
197                 txsize += sizeof(pdu_header);
198
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;
204                 }
205
206                 /* 3. setup iso_packet_descriptor */
207                 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
208                         ssize_t len = 0;
209
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;
215                         }
216
217                         iov[2].iov_base = iso_buffer;
218                         iov[2].iov_len  = len;
219                         txsize += len;
220                 }
221
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;
231                 }
232
233                 kfree(iso_buffer);
234                 usbip_dbg_stub_tx("send txdata\n");
235
236                 total_size += txsize;
237         }
238
239
240         spin_lock_irqsave(&sdev->priv_lock, flags);
241
242         list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
243                 stub_free_priv_and_urb(priv);
244         }
245
246         spin_unlock_irqrestore(&sdev->priv_lock, flags);
247
248         return total_size;
249 }
250
251
252 /*-------------------------------------------------------------------------*/
253 /* send RET_UNLINK */
254
255 static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
256 {
257         unsigned long flags;
258         struct stub_unlink *unlink, *tmp;
259
260         spin_lock_irqsave(&sdev->priv_lock, flags);
261
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;
266         }
267
268         spin_unlock_irqrestore(&sdev->priv_lock, flags);
269
270         return NULL;
271 }
272
273
274 static int stub_send_ret_unlink(struct stub_device *sdev)
275 {
276         unsigned long flags;
277         struct stub_unlink *unlink, *tmp;
278
279         struct msghdr msg;
280         struct kvec iov[1];
281         size_t txsize;
282
283         size_t total_size = 0;
284
285         while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
286                 int ret;
287                 struct usbip_header pdu_header;
288
289                 txsize = 0;
290                 memset(&pdu_header, 0, sizeof(pdu_header));
291                 memset(&msg, 0, sizeof(msg));
292                 memset(&iov, 0, sizeof(iov));
293
294                 usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
295
296                 /* 1. setup usbip_header */
297                 setup_ret_unlink_pdu(&pdu_header, unlink);
298                 usbip_header_correct_endian(&pdu_header, 1);
299
300                 iov[0].iov_base = &pdu_header;
301                 iov[0].iov_len  = sizeof(pdu_header);
302                 txsize += sizeof(pdu_header);
303
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;
312                 }
313
314
315                 usbip_dbg_stub_tx("send txdata\n");
316
317                 total_size += txsize;
318         }
319
320
321         spin_lock_irqsave(&sdev->priv_lock, flags);
322
323         list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
324                 list_del(&unlink->list);
325                 kfree(unlink);
326         }
327
328         spin_unlock_irqrestore(&sdev->priv_lock, flags);
329
330         return total_size;
331 }
332
333
334 /*-------------------------------------------------------------------------*/
335
336 void stub_tx_loop(struct usbip_task *ut)
337 {
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);
340
341         while (1) {
342                 if (signal_pending(current)) {
343                         usbip_dbg_stub_tx("signal catched\n");
344                         break;
345                 }
346
347                 if (usbip_event_happened(ud))
348                         break;
349
350                 /*
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().
363                  */
364                 if (stub_send_ret_submit(sdev) < 0)
365                         break;
366
367                 if (stub_send_ret_unlink(sdev) < 0)
368                         break;
369
370                 wait_event_interruptible(sdev->tx_waitq,
371                                 (!list_empty(&sdev->priv_tx) ||
372                                  !list_empty(&sdev->unlink_tx)));
373         }
374 }