blob: 1622563823a39f23781320e88a8cbe71aedd9195 [file] [log] [blame]
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -06001/*
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
Arnd Bergmann9720b4b2011-03-02 00:13:05 +010020#include <linux/kthread.h>
matt mooney7aaacb42011-05-11 22:33:43 -070021#include <linux/socket.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060023#include "usbip_common.h"
24#include "stub.h"
25
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060026static 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) */
38void 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) {
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060045 usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
46 return;
47 }
48
49 unlink->seqnum = seqnum;
50 unlink->status = status;
51
52 list_add_tail(&unlink->list, &sdev->unlink_tx);
53}
54
55/**
56 * stub_complete - completion handler of a usbip urb
57 * @urb: pointer to the urb completed
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060058 *
59 * When a urb has completed, the USB core driver calls this function mostly in
60 * the interrupt context. To return the result of a urb, the completed urb is
61 * linked to the pending list of returning.
62 *
63 */
64void stub_complete(struct urb *urb)
65{
66 struct stub_priv *priv = (struct stub_priv *) urb->context;
67 struct stub_device *sdev = priv->sdev;
68 unsigned long flags;
69
Brian G. Merrellb8868e42009-07-21 00:46:13 -060070 usbip_dbg_stub_tx("complete! status %d\n", urb->status);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060071
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060072 switch (urb->status) {
73 case 0:
74 /* OK */
75 break;
76 case -ENOENT:
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +010077 dev_info(&urb->dev->dev,
78 "stopped by a call to usb_kill_urb() because of cleaning up a virtual connection\n");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060079 return;
80 case -ECONNRESET:
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +010081 dev_info(&urb->dev->dev,
82 "unlinked by a call to usb_unlink_urb()\n");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060083 break;
84 case -EPIPE:
matt mooney1a4b6f62011-05-19 16:47:32 -070085 dev_info(&urb->dev->dev, "endpoint %d is stalled\n",
86 usb_pipeendpoint(urb->pipe));
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060087 break;
88 case -ESHUTDOWN:
matt mooney1a4b6f62011-05-19 16:47:32 -070089 dev_info(&urb->dev->dev, "device removed?\n");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060090 break;
91 default:
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +010092 dev_info(&urb->dev->dev,
93 "urb completion with non-zero status %d\n",
94 urb->status);
matt mooney49aecef2011-05-06 03:47:54 -070095 break;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -060096 }
97
98 /* link a urb to the queue of tx. */
99 spin_lock_irqsave(&sdev->priv_lock, flags);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600100 if (priv->unlinking) {
101 stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
102 stub_free_priv_and_urb(priv);
matt mooney87352762011-05-19 21:36:56 -0700103 } else {
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600104 list_move_tail(&priv->list, &sdev->priv_tx);
matt mooney87352762011-05-19 21:36:56 -0700105 }
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600106 spin_unlock_irqrestore(&sdev->priv_lock, flags);
107
108 /* wake up tx_thread */
109 wake_up(&sdev->tx_waitq);
110}
111
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600112static inline void setup_base_pdu(struct usbip_header_basic *base,
matt mooneyc6956c92011-05-06 03:47:44 -0700113 __u32 command, __u32 seqnum)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600114{
matt mooney87352762011-05-19 21:36:56 -0700115 base->command = command;
116 base->seqnum = seqnum;
117 base->devid = 0;
118 base->ep = 0;
matt mooneyc6956c92011-05-06 03:47:44 -0700119 base->direction = 0;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600120}
121
122static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
123{
124 struct stub_priv *priv = (struct stub_priv *) urb->context;
125
126 setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600127 usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
128}
129
130static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
matt mooneyc6956c92011-05-06 03:47:44 -0700131 struct stub_unlink *unlink)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600132{
133 setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600134 rpdu->u.ret_unlink.status = unlink->status;
135}
136
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600137static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
138{
139 unsigned long flags;
140 struct stub_priv *priv, *tmp;
141
142 spin_lock_irqsave(&sdev->priv_lock, flags);
143
144 list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
145 list_move_tail(&priv->list, &sdev->priv_free);
146 spin_unlock_irqrestore(&sdev->priv_lock, flags);
147 return priv;
148 }
149
150 spin_unlock_irqrestore(&sdev->priv_lock, flags);
151
152 return NULL;
153}
154
155static int stub_send_ret_submit(struct stub_device *sdev)
156{
157 unsigned long flags;
158 struct stub_priv *priv, *tmp;
159
160 struct msghdr msg;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600161 size_t txsize;
162
163 size_t total_size = 0;
164
165 while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
166 int ret;
167 struct urb *urb = priv->urb;
168 struct usbip_header pdu_header;
Bart Westgeest36ac9b02012-10-10 13:34:25 -0400169 struct usbip_iso_packet_descriptor *iso_buffer = NULL;
Arjan Mels28276a22011-04-05 20:26:59 +0200170 struct kvec *iov = NULL;
171 int iovnum = 0;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600172
173 txsize = 0;
174 memset(&pdu_header, 0, sizeof(pdu_header));
175 memset(&msg, 0, sizeof(msg));
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600176
Arjan Mels28276a22011-04-05 20:26:59 +0200177 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
178 iovnum = 2 + urb->number_of_packets;
179 else
180 iovnum = 2;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600181
Arjan Mels28276a22011-04-05 20:26:59 +0200182 iov = kzalloc(iovnum * sizeof(struct kvec), GFP_KERNEL);
183
184 if (!iov) {
185 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_MALLOC);
186 return -1;
187 }
188
189 iovnum = 0;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600190
191 /* 1. setup usbip_header */
192 setup_ret_submit_pdu(&pdu_header, urb);
Arjan Mels28276a22011-04-05 20:26:59 +0200193 usbip_dbg_stub_tx("setup txdata seqnum: %d urb: %p\n",
matt mooneyc6956c92011-05-06 03:47:44 -0700194 pdu_header.base.seqnum, urb);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600195 usbip_header_correct_endian(&pdu_header, 1);
196
Arjan Mels28276a22011-04-05 20:26:59 +0200197 iov[iovnum].iov_base = &pdu_header;
198 iov[iovnum].iov_len = sizeof(pdu_header);
199 iovnum++;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600200 txsize += sizeof(pdu_header);
201
202 /* 2. setup transfer buffer */
Arjan Mels28276a22011-04-05 20:26:59 +0200203 if (usb_pipein(urb->pipe) &&
matt mooneyc6956c92011-05-06 03:47:44 -0700204 usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS &&
205 urb->actual_length > 0) {
Arjan Mels28276a22011-04-05 20:26:59 +0200206 iov[iovnum].iov_base = urb->transfer_buffer;
207 iov[iovnum].iov_len = urb->actual_length;
208 iovnum++;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600209 txsize += urb->actual_length;
Arjan Mels28276a22011-04-05 20:26:59 +0200210 } else if (usb_pipein(urb->pipe) &&
matt mooneyc6956c92011-05-06 03:47:44 -0700211 usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
Arjan Mels28276a22011-04-05 20:26:59 +0200212 /*
213 * For isochronous packets: actual length is the sum of
214 * the actual length of the individual, packets, but as
215 * the packet offsets are not changed there will be
216 * padding between the packets. To optimally use the
217 * bandwidth the padding is not transmitted.
218 */
219
220 int i;
221 for (i = 0; i < urb->number_of_packets; i++) {
matt mooneyc6956c92011-05-06 03:47:44 -0700222 iov[iovnum].iov_base = urb->transfer_buffer +
223 urb->iso_frame_desc[i].offset;
224 iov[iovnum].iov_len =
225 urb->iso_frame_desc[i].actual_length;
Arjan Mels28276a22011-04-05 20:26:59 +0200226 iovnum++;
227 txsize += urb->iso_frame_desc[i].actual_length;
228 }
229
230 if (txsize != sizeof(pdu_header) + urb->actual_length) {
231 dev_err(&sdev->interface->dev,
Cédric Cabessa6bb3ee62014-03-19 23:04:56 +0100232 "actual length of urb %d does not match iso packet sizes %zu\n",
matt mooneyc6956c92011-05-06 03:47:44 -0700233 urb->actual_length,
234 txsize-sizeof(pdu_header));
Arjan Mels28276a22011-04-05 20:26:59 +0200235 kfree(iov);
matt mooneyc6956c92011-05-06 03:47:44 -0700236 usbip_event_add(&sdev->ud,
237 SDEV_EVENT_ERROR_TCP);
Arjan Mels28276a22011-04-05 20:26:59 +0200238 return -1;
239 }
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600240 }
241
242 /* 3. setup iso_packet_descriptor */
243 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
244 ssize_t len = 0;
245
246 iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
247 if (!iso_buffer) {
248 usbip_event_add(&sdev->ud,
249 SDEV_EVENT_ERROR_MALLOC);
Arjan Mels28276a22011-04-05 20:26:59 +0200250 kfree(iov);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600251 return -1;
252 }
253
Arjan Mels28276a22011-04-05 20:26:59 +0200254 iov[iovnum].iov_base = iso_buffer;
255 iov[iovnum].iov_len = len;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600256 txsize += len;
Arjan Mels28276a22011-04-05 20:26:59 +0200257 iovnum++;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600258 }
259
Arjan Mels28276a22011-04-05 20:26:59 +0200260 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg,
261 iov, iovnum, txsize);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600262 if (ret != txsize) {
263 dev_err(&sdev->interface->dev,
264 "sendmsg failed!, retval %d for %zd\n",
265 ret, txsize);
Arjan Mels28276a22011-04-05 20:26:59 +0200266 kfree(iov);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600267 kfree(iso_buffer);
268 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
269 return -1;
270 }
271
Arjan Mels28276a22011-04-05 20:26:59 +0200272 kfree(iov);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600273 kfree(iso_buffer);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600274
275 total_size += txsize;
276 }
277
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600278 spin_lock_irqsave(&sdev->priv_lock, flags);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600279 list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
280 stub_free_priv_and_urb(priv);
281 }
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600282 spin_unlock_irqrestore(&sdev->priv_lock, flags);
283
284 return total_size;
285}
286
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600287static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
288{
289 unsigned long flags;
290 struct stub_unlink *unlink, *tmp;
291
292 spin_lock_irqsave(&sdev->priv_lock, flags);
293
294 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
295 list_move_tail(&unlink->list, &sdev->unlink_free);
296 spin_unlock_irqrestore(&sdev->priv_lock, flags);
297 return unlink;
298 }
299
300 spin_unlock_irqrestore(&sdev->priv_lock, flags);
301
302 return NULL;
303}
304
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600305static int stub_send_ret_unlink(struct stub_device *sdev)
306{
307 unsigned long flags;
308 struct stub_unlink *unlink, *tmp;
309
310 struct msghdr msg;
311 struct kvec iov[1];
312 size_t txsize;
313
314 size_t total_size = 0;
315
316 while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
317 int ret;
318 struct usbip_header pdu_header;
319
320 txsize = 0;
321 memset(&pdu_header, 0, sizeof(pdu_header));
322 memset(&msg, 0, sizeof(msg));
323 memset(&iov, 0, sizeof(iov));
324
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600325 usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600326
327 /* 1. setup usbip_header */
328 setup_ret_unlink_pdu(&pdu_header, unlink);
329 usbip_header_correct_endian(&pdu_header, 1);
330
331 iov[0].iov_base = &pdu_header;
332 iov[0].iov_len = sizeof(pdu_header);
333 txsize += sizeof(pdu_header);
334
335 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
336 1, txsize);
337 if (ret != txsize) {
338 dev_err(&sdev->interface->dev,
339 "sendmsg failed!, retval %d for %zd\n",
340 ret, txsize);
341 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
342 return -1;
343 }
344
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600345 usbip_dbg_stub_tx("send txdata\n");
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600346 total_size += txsize;
347 }
348
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600349 spin_lock_irqsave(&sdev->priv_lock, flags);
350
351 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
352 list_del(&unlink->list);
353 kfree(unlink);
354 }
355
356 spin_unlock_irqrestore(&sdev->priv_lock, flags);
357
358 return total_size;
359}
360
Arnd Bergmann9720b4b2011-03-02 00:13:05 +0100361int stub_tx_loop(void *data)
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600362{
Arnd Bergmann9720b4b2011-03-02 00:13:05 +0100363 struct usbip_device *ud = data;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600364 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
365
Arnd Bergmann9720b4b2011-03-02 00:13:05 +0100366 while (!kthread_should_stop()) {
Brian G. Merrellb8868e42009-07-21 00:46:13 -0600367 if (usbip_event_happened(ud))
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600368 break;
369
370 /*
371 * send_ret_submit comes earlier than send_ret_unlink. stub_rx
372 * looks at only priv_init queue. If the completion of a URB is
373 * earlier than the receive of CMD_UNLINK, priv is moved to
374 * priv_tx queue and stub_rx does not find the target priv. In
375 * this case, vhci_rx receives the result of the submit request
376 * and then receives the result of the unlink request. The
377 * result of the submit is given back to the usbcore as the
378 * completion of the unlink request. The request of the
379 * unlink is ignored. This is ok because a driver who calls
380 * usb_unlink_urb() understands the unlink was too late by
381 * getting the status of the given-backed URB which has the
382 * status of usb_submit_urb().
383 */
384 if (stub_send_ret_submit(sdev) < 0)
385 break;
386
387 if (stub_send_ret_unlink(sdev) < 0)
388 break;
389
390 wait_event_interruptible(sdev->tx_waitq,
matt mooneyc6956c92011-05-06 03:47:44 -0700391 (!list_empty(&sdev->priv_tx) ||
392 !list_empty(&sdev->unlink_tx) ||
393 kthread_should_stop()));
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600394 }
Arnd Bergmann9720b4b2011-03-02 00:13:05 +0100395
396 return 0;
Takahiro Hirofuchi4d7b5c72008-07-09 14:56:51 -0600397}