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