blob: 75a2bea62102a08bfab8f57d1848d6d6d984121d [file] [log] [blame]
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001/*
2 * WUSB Wire Adapter
3 * Data transfer and URB enqueing
4 *
5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 *
22 *
23 * How transfers work: get a buffer, break it up in segments (segment
24 * size is a multiple of the maxpacket size). For each segment issue a
25 * segment request (struct wa_xfer_*), then send the data buffer if
26 * out or nothing if in (all over the DTO endpoint).
27 *
28 * For each submitted segment request, a notification will come over
29 * the NEP endpoint and a transfer result (struct xfer_result) will
30 * arrive in the DTI URB. Read it, get the xfer ID, see if there is
31 * data coming (inbound transfer), schedule a read and handle it.
32 *
33 * Sounds simple, it is a pain to implement.
34 *
35 *
36 * ENTRY POINTS
37 *
38 * FIXME
39 *
40 * LIFE CYCLE / STATE DIAGRAM
41 *
42 * FIXME
43 *
44 * THIS CODE IS DISGUSTING
45 *
46 * Warned you are; it's my second try and still not happy with it.
47 *
48 * NOTES:
49 *
50 * - No iso
51 *
52 * - Supports DMA xfers, control, bulk and maybe interrupt
53 *
54 * - Does not recycle unused rpipes
55 *
56 * An rpipe is assigned to an endpoint the first time it is used,
57 * and then it's there, assigned, until the endpoint is disabled
58 * (destroyed [{h,d}wahc_op_ep_disable()]. The assignment of the
59 * rpipe to the endpoint is done under the wa->rpipe_sem semaphore
60 * (should be a mutex).
61 *
62 * Two methods it could be done:
63 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030064 * (a) set up a timer every time an rpipe's use count drops to 1
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010065 * (which means unused) or when a transfer ends. Reset the
66 * timer when a xfer is queued. If the timer expires, release
67 * the rpipe [see rpipe_ep_disable()].
68 *
69 * (b) when looking for free rpipes to attach [rpipe_get_by_ep()],
70 * when none are found go over the list, check their endpoint
71 * and their activity record (if no last-xfer-done-ts in the
72 * last x seconds) take it
73 *
74 * However, due to the fact that we have a set of limited
75 * resources (max-segments-at-the-same-time per xfer,
76 * xfers-per-ripe, blocks-per-rpipe, rpipes-per-host), at the end
77 * we are going to have to rebuild all this based on an scheduler,
78 * to where we have a list of transactions to do and based on the
Gilles Espinassef77f13e2010-03-29 15:41:47 +020079 * availability of the different required components (blocks,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010080 * rpipes, segment slots, etc), we go scheduling them. Painful.
81 */
82#include <linux/init.h>
83#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090084#include <linux/slab.h>
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010085#include <linux/hash.h>
Manuel Zerpies9708cd22011-06-16 14:15:16 +020086#include <linux/ratelimit.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040087#include <linux/export.h>
Thomas Pugliese2b81c082013-06-11 10:39:31 -050088#include <linux/scatterlist.h>
David Vrabelbce83692008-12-22 18:22:50 +000089
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010090#include "wa-hc.h"
91#include "wusbhc.h"
92
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010093enum {
94 WA_SEGS_MAX = 255,
95};
96
97enum wa_seg_status {
98 WA_SEG_NOTREADY,
99 WA_SEG_READY,
100 WA_SEG_DELAYED,
101 WA_SEG_SUBMITTED,
102 WA_SEG_PENDING,
103 WA_SEG_DTI_PENDING,
104 WA_SEG_DONE,
105 WA_SEG_ERROR,
106 WA_SEG_ABORTED,
107};
108
109static void wa_xfer_delayed_run(struct wa_rpipe *);
110
111/*
112 * Life cycle governed by 'struct urb' (the refcount of the struct is
113 * that of the 'struct urb' and usb_free_urb() would free the whole
114 * struct).
115 */
116struct wa_seg {
117 struct urb urb;
118 struct urb *dto_urb; /* for data output? */
119 struct list_head list_node; /* for rpipe->req_list */
120 struct wa_xfer *xfer; /* out xfer */
121 u8 index; /* which segment we are */
122 enum wa_seg_status status;
123 ssize_t result; /* bytes xfered or error */
124 struct wa_xfer_hdr xfer_hdr;
125 u8 xfer_extra[]; /* xtra space for xfer_hdr_ctl */
126};
127
128static void wa_seg_init(struct wa_seg *seg)
129{
130 /* usb_init_urb() repeats a lot of work, so we do it here */
131 kref_init(&seg->urb.kref);
132}
133
134/*
135 * Protected by xfer->lock
136 *
137 */
138struct wa_xfer {
139 struct kref refcnt;
140 struct list_head list_node;
141 spinlock_t lock;
142 u32 id;
143
144 struct wahc *wa; /* Wire adapter we are plugged to */
145 struct usb_host_endpoint *ep;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300146 struct urb *urb; /* URB we are transferring for */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100147 struct wa_seg **seg; /* transfer segments */
148 u8 segs, segs_submitted, segs_done;
149 unsigned is_inbound:1;
150 unsigned is_dma:1;
151 size_t seg_size;
152 int result;
153
154 gfp_t gfp; /* allocation mask */
155
156 struct wusb_dev *wusb_dev; /* for activity timestamps */
157};
158
159static inline void wa_xfer_init(struct wa_xfer *xfer)
160{
161 kref_init(&xfer->refcnt);
162 INIT_LIST_HEAD(&xfer->list_node);
163 spin_lock_init(&xfer->lock);
164}
165
166/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300167 * Destroy a transfer structure
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100168 *
169 * Note that the xfer->seg[index] thingies follow the URB life cycle,
170 * so we need to put them, not free them.
171 */
172static void wa_xfer_destroy(struct kref *_xfer)
173{
174 struct wa_xfer *xfer = container_of(_xfer, struct wa_xfer, refcnt);
175 if (xfer->seg) {
176 unsigned cnt;
177 for (cnt = 0; cnt < xfer->segs; cnt++) {
178 if (xfer->is_inbound)
179 usb_put_urb(xfer->seg[cnt]->dto_urb);
180 usb_put_urb(&xfer->seg[cnt]->urb);
181 }
182 }
183 kfree(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100184}
185
186static void wa_xfer_get(struct wa_xfer *xfer)
187{
188 kref_get(&xfer->refcnt);
189}
190
191static void wa_xfer_put(struct wa_xfer *xfer)
192{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100193 kref_put(&xfer->refcnt, wa_xfer_destroy);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100194}
195
196/*
197 * xfer is referenced
198 *
199 * xfer->lock has to be unlocked
200 *
201 * We take xfer->lock for setting the result; this is a barrier
202 * against drivers/usb/core/hcd.c:unlink1() being called after we call
203 * usb_hcd_giveback_urb() and wa_urb_dequeue() trying to get a
204 * reference to the transfer.
205 */
206static void wa_xfer_giveback(struct wa_xfer *xfer)
207{
208 unsigned long flags;
David Vrabelbce83692008-12-22 18:22:50 +0000209
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100210 spin_lock_irqsave(&xfer->wa->xfer_list_lock, flags);
211 list_del_init(&xfer->list_node);
212 spin_unlock_irqrestore(&xfer->wa->xfer_list_lock, flags);
213 /* FIXME: segmentation broken -- kills DWA */
214 wusbhc_giveback_urb(xfer->wa->wusb, xfer->urb, xfer->result);
215 wa_put(xfer->wa);
216 wa_xfer_put(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100217}
218
219/*
220 * xfer is referenced
221 *
222 * xfer->lock has to be unlocked
223 */
224static void wa_xfer_completion(struct wa_xfer *xfer)
225{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100226 if (xfer->wusb_dev)
227 wusb_dev_put(xfer->wusb_dev);
228 rpipe_put(xfer->ep->hcpriv);
229 wa_xfer_giveback(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100230}
231
232/*
233 * If transfer is done, wrap it up and return true
234 *
235 * xfer->lock has to be locked
236 */
237static unsigned __wa_xfer_is_done(struct wa_xfer *xfer)
238{
David Vrabelbce83692008-12-22 18:22:50 +0000239 struct device *dev = &xfer->wa->usb_iface->dev;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100240 unsigned result, cnt;
241 struct wa_seg *seg;
242 struct urb *urb = xfer->urb;
243 unsigned found_short = 0;
244
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100245 result = xfer->segs_done == xfer->segs_submitted;
246 if (result == 0)
247 goto out;
248 urb->actual_length = 0;
249 for (cnt = 0; cnt < xfer->segs; cnt++) {
250 seg = xfer->seg[cnt];
251 switch (seg->status) {
252 case WA_SEG_DONE:
253 if (found_short && seg->result > 0) {
David Vrabelbce83692008-12-22 18:22:50 +0000254 dev_dbg(dev, "xfer %p#%u: bad short segments (%zu)\n",
255 xfer, cnt, seg->result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100256 urb->status = -EINVAL;
257 goto out;
258 }
259 urb->actual_length += seg->result;
260 if (seg->result < xfer->seg_size
261 && cnt != xfer->segs-1)
262 found_short = 1;
David Vrabelbce83692008-12-22 18:22:50 +0000263 dev_dbg(dev, "xfer %p#%u: DONE short %d "
264 "result %zu urb->actual_length %d\n",
265 xfer, seg->index, found_short, seg->result,
266 urb->actual_length);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100267 break;
268 case WA_SEG_ERROR:
269 xfer->result = seg->result;
David Vrabelbce83692008-12-22 18:22:50 +0000270 dev_dbg(dev, "xfer %p#%u: ERROR result %zu\n",
271 xfer, seg->index, seg->result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100272 goto out;
273 case WA_SEG_ABORTED:
David Vrabelbce83692008-12-22 18:22:50 +0000274 dev_dbg(dev, "xfer %p#%u ABORTED: result %d\n",
275 xfer, seg->index, urb->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100276 xfer->result = urb->status;
277 goto out;
278 default:
David Vrabelbce83692008-12-22 18:22:50 +0000279 dev_warn(dev, "xfer %p#%u: is_done bad state %d\n",
280 xfer, cnt, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100281 xfer->result = -EINVAL;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100282 goto out;
283 }
284 }
285 xfer->result = 0;
286out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100287 return result;
288}
289
290/*
291 * Initialize a transfer's ID
292 *
293 * We need to use a sequential number; if we use the pointer or the
294 * hash of the pointer, it can repeat over sequential transfers and
295 * then it will confuse the HWA....wonder why in hell they put a 32
296 * bit handle in there then.
297 */
298static void wa_xfer_id_init(struct wa_xfer *xfer)
299{
300 xfer->id = atomic_add_return(1, &xfer->wa->xfer_id_count);
301}
302
303/*
304 * Return the xfer's ID associated with xfer
305 *
306 * Need to generate a
307 */
308static u32 wa_xfer_id(struct wa_xfer *xfer)
309{
310 return xfer->id;
311}
312
313/*
314 * Search for a transfer list ID on the HCD's URB list
315 *
316 * For 32 bit architectures, we use the pointer itself; for 64 bits, a
317 * 32-bit hash of the pointer.
318 *
319 * @returns NULL if not found.
320 */
321static struct wa_xfer *wa_xfer_get_by_id(struct wahc *wa, u32 id)
322{
323 unsigned long flags;
324 struct wa_xfer *xfer_itr;
325 spin_lock_irqsave(&wa->xfer_list_lock, flags);
326 list_for_each_entry(xfer_itr, &wa->xfer_list, list_node) {
327 if (id == xfer_itr->id) {
328 wa_xfer_get(xfer_itr);
329 goto out;
330 }
331 }
332 xfer_itr = NULL;
333out:
334 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
335 return xfer_itr;
336}
337
338struct wa_xfer_abort_buffer {
339 struct urb urb;
340 struct wa_xfer_abort cmd;
341};
342
343static void __wa_xfer_abort_cb(struct urb *urb)
344{
345 struct wa_xfer_abort_buffer *b = urb->context;
346 usb_put_urb(&b->urb);
347}
348
349/*
350 * Aborts an ongoing transaction
351 *
352 * Assumes the transfer is referenced and locked and in a submitted
353 * state (mainly that there is an endpoint/rpipe assigned).
354 *
355 * The callback (see above) does nothing but freeing up the data by
356 * putting the URB. Because the URB is allocated at the head of the
357 * struct, the whole space we allocated is kfreed.
358 *
359 * We'll get an 'aborted transaction' xfer result on DTI, that'll
360 * politely ignore because at this point the transaction has been
361 * marked as aborted already.
362 */
363static void __wa_xfer_abort(struct wa_xfer *xfer)
364{
365 int result;
366 struct device *dev = &xfer->wa->usb_iface->dev;
367 struct wa_xfer_abort_buffer *b;
368 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
369
370 b = kmalloc(sizeof(*b), GFP_ATOMIC);
371 if (b == NULL)
372 goto error_kmalloc;
373 b->cmd.bLength = sizeof(b->cmd);
374 b->cmd.bRequestType = WA_XFER_ABORT;
375 b->cmd.wRPipe = rpipe->descr.wRPipeIndex;
376 b->cmd.dwTransferID = wa_xfer_id(xfer);
377
378 usb_init_urb(&b->urb);
379 usb_fill_bulk_urb(&b->urb, xfer->wa->usb_dev,
380 usb_sndbulkpipe(xfer->wa->usb_dev,
381 xfer->wa->dto_epd->bEndpointAddress),
382 &b->cmd, sizeof(b->cmd), __wa_xfer_abort_cb, b);
383 result = usb_submit_urb(&b->urb, GFP_ATOMIC);
384 if (result < 0)
385 goto error_submit;
386 return; /* callback frees! */
387
388
389error_submit:
390 if (printk_ratelimit())
391 dev_err(dev, "xfer %p: Can't submit abort request: %d\n",
392 xfer, result);
393 kfree(b);
394error_kmalloc:
395 return;
396
397}
398
399/*
400 *
401 * @returns < 0 on error, transfer segment request size if ok
402 */
403static ssize_t __wa_xfer_setup_sizes(struct wa_xfer *xfer,
404 enum wa_xfer_type *pxfer_type)
405{
406 ssize_t result;
407 struct device *dev = &xfer->wa->usb_iface->dev;
408 size_t maxpktsize;
409 struct urb *urb = xfer->urb;
410 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
411
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100412 switch (rpipe->descr.bmAttribute & 0x3) {
413 case USB_ENDPOINT_XFER_CONTROL:
414 *pxfer_type = WA_XFER_TYPE_CTL;
415 result = sizeof(struct wa_xfer_ctl);
416 break;
417 case USB_ENDPOINT_XFER_INT:
418 case USB_ENDPOINT_XFER_BULK:
419 *pxfer_type = WA_XFER_TYPE_BI;
420 result = sizeof(struct wa_xfer_bi);
421 break;
422 case USB_ENDPOINT_XFER_ISOC:
423 dev_err(dev, "FIXME: ISOC not implemented\n");
424 result = -ENOSYS;
425 goto error;
426 default:
427 /* never happens */
428 BUG();
429 result = -EINVAL; /* shut gcc up */
430 };
431 xfer->is_inbound = urb->pipe & USB_DIR_IN ? 1 : 0;
432 xfer->is_dma = urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? 1 : 0;
433 xfer->seg_size = le16_to_cpu(rpipe->descr.wBlocks)
434 * 1 << (xfer->wa->wa_descr->bRPipeBlockSize - 1);
435 /* Compute the segment size and make sure it is a multiple of
436 * the maxpktsize (WUSB1.0[8.3.3.1])...not really too much of
437 * a check (FIXME) */
438 maxpktsize = le16_to_cpu(rpipe->descr.wMaxPacketSize);
439 if (xfer->seg_size < maxpktsize) {
440 dev_err(dev, "HW BUG? seg_size %zu smaller than maxpktsize "
441 "%zu\n", xfer->seg_size, maxpktsize);
442 result = -EINVAL;
443 goto error;
444 }
445 xfer->seg_size = (xfer->seg_size / maxpktsize) * maxpktsize;
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500446 xfer->segs = DIV_ROUND_UP(urb->transfer_buffer_length, xfer->seg_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100447 if (xfer->segs >= WA_SEGS_MAX) {
448 dev_err(dev, "BUG? ops, number of segments %d bigger than %d\n",
449 (int)(urb->transfer_buffer_length / xfer->seg_size),
450 WA_SEGS_MAX);
451 result = -EINVAL;
452 goto error;
453 }
454 if (xfer->segs == 0 && *pxfer_type == WA_XFER_TYPE_CTL)
455 xfer->segs = 1;
456error:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100457 return result;
458}
459
David Vrabelbce83692008-12-22 18:22:50 +0000460/* Fill in the common request header and xfer-type specific data. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100461static void __wa_xfer_setup_hdr0(struct wa_xfer *xfer,
462 struct wa_xfer_hdr *xfer_hdr0,
463 enum wa_xfer_type xfer_type,
464 size_t xfer_hdr_size)
465{
466 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
467
468 xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
469 xfer_hdr0->bLength = xfer_hdr_size;
470 xfer_hdr0->bRequestType = xfer_type;
471 xfer_hdr0->wRPipe = rpipe->descr.wRPipeIndex;
472 xfer_hdr0->dwTransferID = wa_xfer_id(xfer);
473 xfer_hdr0->bTransferSegment = 0;
474 switch (xfer_type) {
475 case WA_XFER_TYPE_CTL: {
476 struct wa_xfer_ctl *xfer_ctl =
477 container_of(xfer_hdr0, struct wa_xfer_ctl, hdr);
478 xfer_ctl->bmAttribute = xfer->is_inbound ? 1 : 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100479 memcpy(&xfer_ctl->baSetupData, xfer->urb->setup_packet,
480 sizeof(xfer_ctl->baSetupData));
481 break;
482 }
483 case WA_XFER_TYPE_BI:
484 break;
485 case WA_XFER_TYPE_ISO:
486 printk(KERN_ERR "FIXME: ISOC not implemented\n");
487 default:
488 BUG();
489 };
490}
491
492/*
493 * Callback for the OUT data phase of the segment request
494 *
495 * Check wa_seg_cb(); most comments also apply here because this
496 * function does almost the same thing and they work closely
497 * together.
498 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300499 * If the seg request has failed but this DTO phase has succeeded,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100500 * wa_seg_cb() has already failed the segment and moved the
501 * status to WA_SEG_ERROR, so this will go through 'case 0' and
502 * effectively do nothing.
503 */
504static void wa_seg_dto_cb(struct urb *urb)
505{
506 struct wa_seg *seg = urb->context;
507 struct wa_xfer *xfer = seg->xfer;
508 struct wahc *wa;
509 struct device *dev;
510 struct wa_rpipe *rpipe;
511 unsigned long flags;
512 unsigned rpipe_ready = 0;
513 u8 done = 0;
514
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100515 switch (urb->status) {
516 case 0:
517 spin_lock_irqsave(&xfer->lock, flags);
518 wa = xfer->wa;
519 dev = &wa->usb_iface->dev;
David Vrabelbce83692008-12-22 18:22:50 +0000520 dev_dbg(dev, "xfer %p#%u: data out done (%d bytes)\n",
521 xfer, seg->index, urb->actual_length);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100522 if (seg->status < WA_SEG_PENDING)
523 seg->status = WA_SEG_PENDING;
524 seg->result = urb->actual_length;
525 spin_unlock_irqrestore(&xfer->lock, flags);
526 break;
527 case -ECONNRESET: /* URB unlinked; no need to do anything */
528 case -ENOENT: /* as it was done by the who unlinked us */
529 break;
530 default: /* Other errors ... */
531 spin_lock_irqsave(&xfer->lock, flags);
532 wa = xfer->wa;
533 dev = &wa->usb_iface->dev;
534 rpipe = xfer->ep->hcpriv;
David Vrabelbce83692008-12-22 18:22:50 +0000535 dev_dbg(dev, "xfer %p#%u: data out error %d\n",
536 xfer, seg->index, urb->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100537 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
538 EDC_ERROR_TIMEFRAME)){
539 dev_err(dev, "DTO: URB max acceptable errors "
540 "exceeded, resetting device\n");
541 wa_reset_all(wa);
542 }
543 if (seg->status != WA_SEG_ERROR) {
544 seg->status = WA_SEG_ERROR;
545 seg->result = urb->status;
546 xfer->segs_done++;
547 __wa_xfer_abort(xfer);
548 rpipe_ready = rpipe_avail_inc(rpipe);
549 done = __wa_xfer_is_done(xfer);
550 }
551 spin_unlock_irqrestore(&xfer->lock, flags);
552 if (done)
553 wa_xfer_completion(xfer);
554 if (rpipe_ready)
555 wa_xfer_delayed_run(rpipe);
556 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100557}
558
559/*
560 * Callback for the segment request
561 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200562 * If successful transition state (unless already transitioned or
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100563 * outbound transfer); otherwise, take a note of the error, mark this
564 * segment done and try completion.
565 *
566 * Note we don't access until we are sure that the transfer hasn't
567 * been cancelled (ECONNRESET, ENOENT), which could mean that
568 * seg->xfer could be already gone.
569 *
570 * We have to check before setting the status to WA_SEG_PENDING
571 * because sometimes the xfer result callback arrives before this
572 * callback (geeeeeeze), so it might happen that we are already in
573 * another state. As well, we don't set it if the transfer is inbound,
574 * as in that case, wa_seg_dto_cb will do it when the OUT data phase
575 * finishes.
576 */
577static void wa_seg_cb(struct urb *urb)
578{
579 struct wa_seg *seg = urb->context;
580 struct wa_xfer *xfer = seg->xfer;
581 struct wahc *wa;
582 struct device *dev;
583 struct wa_rpipe *rpipe;
584 unsigned long flags;
585 unsigned rpipe_ready;
586 u8 done = 0;
587
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100588 switch (urb->status) {
589 case 0:
590 spin_lock_irqsave(&xfer->lock, flags);
591 wa = xfer->wa;
592 dev = &wa->usb_iface->dev;
David Vrabelbce83692008-12-22 18:22:50 +0000593 dev_dbg(dev, "xfer %p#%u: request done\n", xfer, seg->index);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100594 if (xfer->is_inbound && seg->status < WA_SEG_PENDING)
595 seg->status = WA_SEG_PENDING;
596 spin_unlock_irqrestore(&xfer->lock, flags);
597 break;
598 case -ECONNRESET: /* URB unlinked; no need to do anything */
599 case -ENOENT: /* as it was done by the who unlinked us */
600 break;
601 default: /* Other errors ... */
602 spin_lock_irqsave(&xfer->lock, flags);
603 wa = xfer->wa;
604 dev = &wa->usb_iface->dev;
605 rpipe = xfer->ep->hcpriv;
606 if (printk_ratelimit())
607 dev_err(dev, "xfer %p#%u: request error %d\n",
608 xfer, seg->index, urb->status);
609 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
610 EDC_ERROR_TIMEFRAME)){
611 dev_err(dev, "DTO: URB max acceptable errors "
612 "exceeded, resetting device\n");
613 wa_reset_all(wa);
614 }
615 usb_unlink_urb(seg->dto_urb);
616 seg->status = WA_SEG_ERROR;
617 seg->result = urb->status;
618 xfer->segs_done++;
619 __wa_xfer_abort(xfer);
620 rpipe_ready = rpipe_avail_inc(rpipe);
621 done = __wa_xfer_is_done(xfer);
622 spin_unlock_irqrestore(&xfer->lock, flags);
623 if (done)
624 wa_xfer_completion(xfer);
625 if (rpipe_ready)
626 wa_xfer_delayed_run(rpipe);
627 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100628}
629
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500630/* allocate an SG list to store bytes_to_transfer bytes and copy the
631 * subset of the in_sg that matches the buffer subset
632 * we are about to transfer. */
633static struct scatterlist *wa_xfer_create_subset_sg(struct scatterlist *in_sg,
634 const unsigned int bytes_transferred,
635 const unsigned int bytes_to_transfer, unsigned int *out_num_sgs)
636{
637 struct scatterlist *out_sg;
638 unsigned int bytes_processed = 0, offset_into_current_page_data = 0,
639 nents;
640 struct scatterlist *current_xfer_sg = in_sg;
641 struct scatterlist *current_seg_sg, *last_seg_sg;
642
643 /* skip previously transferred pages. */
644 while ((current_xfer_sg) &&
645 (bytes_processed < bytes_transferred)) {
646 bytes_processed += current_xfer_sg->length;
647
648 /* advance the sg if current segment starts on or past the
649 next page. */
650 if (bytes_processed <= bytes_transferred)
651 current_xfer_sg = sg_next(current_xfer_sg);
652 }
653
654 /* the data for the current segment starts in current_xfer_sg.
655 calculate the offset. */
656 if (bytes_processed > bytes_transferred) {
657 offset_into_current_page_data = current_xfer_sg->length -
658 (bytes_processed - bytes_transferred);
659 }
660
661 /* calculate the number of pages needed by this segment. */
662 nents = DIV_ROUND_UP((bytes_to_transfer +
663 offset_into_current_page_data +
664 current_xfer_sg->offset),
665 PAGE_SIZE);
666
667 out_sg = kmalloc((sizeof(struct scatterlist) * nents), GFP_ATOMIC);
668 if (out_sg) {
669 sg_init_table(out_sg, nents);
670
671 /* copy the portion of the incoming SG that correlates to the
672 * data to be transferred by this segment to the segment SG. */
673 last_seg_sg = current_seg_sg = out_sg;
674 bytes_processed = 0;
675
676 /* reset nents and calculate the actual number of sg entries
677 needed. */
678 nents = 0;
679 while ((bytes_processed < bytes_to_transfer) &&
680 current_seg_sg && current_xfer_sg) {
681 unsigned int page_len = min((current_xfer_sg->length -
682 offset_into_current_page_data),
683 (bytes_to_transfer - bytes_processed));
684
685 sg_set_page(current_seg_sg, sg_page(current_xfer_sg),
686 page_len,
687 current_xfer_sg->offset +
688 offset_into_current_page_data);
689
690 bytes_processed += page_len;
691
692 last_seg_sg = current_seg_sg;
693 current_seg_sg = sg_next(current_seg_sg);
694 current_xfer_sg = sg_next(current_xfer_sg);
695
696 /* only the first page may require additional offset. */
697 offset_into_current_page_data = 0;
698 nents++;
699 }
700
701 /* update num_sgs and terminate the list since we may have
702 * concatenated pages. */
703 sg_mark_end(last_seg_sg);
704 *out_num_sgs = nents;
705 }
706
707 return out_sg;
708}
709
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100710/*
711 * Allocate the segs array and initialize each of them
712 *
713 * The segments are freed by wa_xfer_destroy() when the xfer use count
714 * drops to zero; however, because each segment is given the same life
715 * cycle as the USB URB it contains, it is actually freed by
716 * usb_put_urb() on the contained USB URB (twisted, eh?).
717 */
718static int __wa_xfer_setup_segs(struct wa_xfer *xfer, size_t xfer_hdr_size)
719{
720 int result, cnt;
721 size_t alloc_size = sizeof(*xfer->seg[0])
722 - sizeof(xfer->seg[0]->xfer_hdr) + xfer_hdr_size;
723 struct usb_device *usb_dev = xfer->wa->usb_dev;
724 const struct usb_endpoint_descriptor *dto_epd = xfer->wa->dto_epd;
725 struct wa_seg *seg;
726 size_t buf_itr, buf_size, buf_itr_size;
727
728 result = -ENOMEM;
David Vrabel92c4d9b2008-10-15 14:50:10 +0100729 xfer->seg = kcalloc(xfer->segs, sizeof(xfer->seg[0]), GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100730 if (xfer->seg == NULL)
731 goto error_segs_kzalloc;
732 buf_itr = 0;
733 buf_size = xfer->urb->transfer_buffer_length;
734 for (cnt = 0; cnt < xfer->segs; cnt++) {
735 seg = xfer->seg[cnt] = kzalloc(alloc_size, GFP_ATOMIC);
736 if (seg == NULL)
737 goto error_seg_kzalloc;
738 wa_seg_init(seg);
739 seg->xfer = xfer;
740 seg->index = cnt;
741 usb_fill_bulk_urb(&seg->urb, usb_dev,
742 usb_sndbulkpipe(usb_dev,
743 dto_epd->bEndpointAddress),
744 &seg->xfer_hdr, xfer_hdr_size,
745 wa_seg_cb, seg);
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500746 buf_itr_size = min(buf_size, xfer->seg_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100747 if (xfer->is_inbound == 0 && buf_size > 0) {
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500748 /* outbound data. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100749 seg->dto_urb = usb_alloc_urb(0, GFP_ATOMIC);
750 if (seg->dto_urb == NULL)
751 goto error_dto_alloc;
752 usb_fill_bulk_urb(
753 seg->dto_urb, usb_dev,
754 usb_sndbulkpipe(usb_dev,
755 dto_epd->bEndpointAddress),
756 NULL, 0, wa_seg_dto_cb, seg);
757 if (xfer->is_dma) {
758 seg->dto_urb->transfer_dma =
759 xfer->urb->transfer_dma + buf_itr;
760 seg->dto_urb->transfer_flags |=
761 URB_NO_TRANSFER_DMA_MAP;
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500762 seg->dto_urb->transfer_buffer = NULL;
763 seg->dto_urb->sg = NULL;
764 seg->dto_urb->num_sgs = 0;
765 } else {
766 /* do buffer or SG processing. */
767 seg->dto_urb->transfer_flags &=
768 ~URB_NO_TRANSFER_DMA_MAP;
769 /* this should always be 0 before a resubmit. */
770 seg->dto_urb->num_mapped_sgs = 0;
771
772 if (xfer->urb->transfer_buffer) {
773 seg->dto_urb->transfer_buffer =
774 xfer->urb->transfer_buffer +
775 buf_itr;
776 seg->dto_urb->sg = NULL;
777 seg->dto_urb->num_sgs = 0;
778 } else {
779 /* allocate an SG list to store seg_size
780 bytes and copy the subset of the
781 xfer->urb->sg that matches the
782 buffer subset we are about to read.
783 */
784 seg->dto_urb->sg =
785 wa_xfer_create_subset_sg(
786 xfer->urb->sg,
787 buf_itr, buf_itr_size,
788 &(seg->dto_urb->num_sgs));
789
790 if (!(seg->dto_urb->sg)) {
791 seg->dto_urb->num_sgs = 0;
792 goto error_sg_alloc;
793 }
794
795 seg->dto_urb->transfer_buffer = NULL;
796 }
797 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100798 seg->dto_urb->transfer_buffer_length = buf_itr_size;
799 }
800 seg->status = WA_SEG_READY;
801 buf_itr += buf_itr_size;
802 buf_size -= buf_itr_size;
803 }
804 return 0;
805
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500806error_sg_alloc:
Thomas Pugliese11b1bf82013-08-15 14:37:41 -0500807 usb_free_urb(xfer->seg[cnt]->dto_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100808error_dto_alloc:
809 kfree(xfer->seg[cnt]);
810 cnt--;
811error_seg_kzalloc:
812 /* use the fact that cnt is left at were it failed */
Dan Carpenterf07af4b2013-02-01 15:53:34 +0300813 for (; cnt >= 0; cnt--) {
Thomas Pugliese11b1bf82013-08-15 14:37:41 -0500814 if (xfer->seg[cnt] && xfer->is_inbound == 0) {
Dan Carpenterf07af4b2013-02-01 15:53:34 +0300815 usb_free_urb(xfer->seg[cnt]->dto_urb);
Thomas Pugliese11b1bf82013-08-15 14:37:41 -0500816 kfree(xfer->seg[cnt]->dto_urb->sg);
817 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100818 kfree(xfer->seg[cnt]);
819 }
820error_segs_kzalloc:
821 return result;
822}
823
824/*
825 * Allocates all the stuff needed to submit a transfer
826 *
827 * Breaks the whole data buffer in a list of segments, each one has a
828 * structure allocated to it and linked in xfer->seg[index]
829 *
830 * FIXME: merge setup_segs() and the last part of this function, no
831 * need to do two for loops when we could run everything in a
832 * single one
833 */
834static int __wa_xfer_setup(struct wa_xfer *xfer, struct urb *urb)
835{
836 int result;
837 struct device *dev = &xfer->wa->usb_iface->dev;
838 enum wa_xfer_type xfer_type = 0; /* shut up GCC */
839 size_t xfer_hdr_size, cnt, transfer_size;
840 struct wa_xfer_hdr *xfer_hdr0, *xfer_hdr;
841
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100842 result = __wa_xfer_setup_sizes(xfer, &xfer_type);
843 if (result < 0)
844 goto error_setup_sizes;
845 xfer_hdr_size = result;
846 result = __wa_xfer_setup_segs(xfer, xfer_hdr_size);
847 if (result < 0) {
848 dev_err(dev, "xfer %p: Failed to allocate %d segments: %d\n",
849 xfer, xfer->segs, result);
850 goto error_setup_segs;
851 }
852 /* Fill the first header */
853 xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
854 wa_xfer_id_init(xfer);
855 __wa_xfer_setup_hdr0(xfer, xfer_hdr0, xfer_type, xfer_hdr_size);
856
857 /* Fill remainig headers */
858 xfer_hdr = xfer_hdr0;
859 transfer_size = urb->transfer_buffer_length;
860 xfer_hdr0->dwTransferLength = transfer_size > xfer->seg_size ?
861 xfer->seg_size : transfer_size;
862 transfer_size -= xfer->seg_size;
863 for (cnt = 1; cnt < xfer->segs; cnt++) {
864 xfer_hdr = &xfer->seg[cnt]->xfer_hdr;
865 memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
866 xfer_hdr->bTransferSegment = cnt;
867 xfer_hdr->dwTransferLength = transfer_size > xfer->seg_size ?
868 cpu_to_le32(xfer->seg_size)
869 : cpu_to_le32(transfer_size);
870 xfer->seg[cnt]->status = WA_SEG_READY;
871 transfer_size -= xfer->seg_size;
872 }
873 xfer_hdr->bTransferSegment |= 0x80; /* this is the last segment */
874 result = 0;
875error_setup_segs:
876error_setup_sizes:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100877 return result;
878}
879
880/*
881 *
882 *
883 * rpipe->seg_lock is held!
884 */
885static int __wa_seg_submit(struct wa_rpipe *rpipe, struct wa_xfer *xfer,
886 struct wa_seg *seg)
887{
888 int result;
889 result = usb_submit_urb(&seg->urb, GFP_ATOMIC);
890 if (result < 0) {
891 printk(KERN_ERR "xfer %p#%u: REQ submit failed: %d\n",
892 xfer, seg->index, result);
893 goto error_seg_submit;
894 }
895 if (seg->dto_urb) {
896 result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
897 if (result < 0) {
898 printk(KERN_ERR "xfer %p#%u: DTO submit failed: %d\n",
899 xfer, seg->index, result);
900 goto error_dto_submit;
901 }
902 }
903 seg->status = WA_SEG_SUBMITTED;
904 rpipe_avail_dec(rpipe);
905 return 0;
906
907error_dto_submit:
908 usb_unlink_urb(&seg->urb);
909error_seg_submit:
910 seg->status = WA_SEG_ERROR;
911 seg->result = result;
912 return result;
913}
914
915/*
916 * Execute more queued request segments until the maximum concurrent allowed
917 *
918 * The ugly unlock/lock sequence on the error path is needed as the
919 * xfer->lock normally nests the seg_lock and not viceversa.
920 *
921 */
922static void wa_xfer_delayed_run(struct wa_rpipe *rpipe)
923{
924 int result;
925 struct device *dev = &rpipe->wa->usb_iface->dev;
926 struct wa_seg *seg;
927 struct wa_xfer *xfer;
928 unsigned long flags;
929
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100930 spin_lock_irqsave(&rpipe->seg_lock, flags);
931 while (atomic_read(&rpipe->segs_available) > 0
932 && !list_empty(&rpipe->seg_list)) {
Thomas Pugliesee9a088f2013-08-12 10:10:53 -0500933 seg = list_first_entry(&(rpipe->seg_list), struct wa_seg,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100934 list_node);
935 list_del(&seg->list_node);
936 xfer = seg->xfer;
937 result = __wa_seg_submit(rpipe, xfer, seg);
David Vrabelbce83692008-12-22 18:22:50 +0000938 dev_dbg(dev, "xfer %p#%u submitted from delayed [%d segments available] %d\n",
939 xfer, seg->index, atomic_read(&rpipe->segs_available), result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100940 if (unlikely(result < 0)) {
941 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
942 spin_lock_irqsave(&xfer->lock, flags);
943 __wa_xfer_abort(xfer);
944 xfer->segs_done++;
945 spin_unlock_irqrestore(&xfer->lock, flags);
946 spin_lock_irqsave(&rpipe->seg_lock, flags);
947 }
948 }
949 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100950}
951
952/*
953 *
954 * xfer->lock is taken
955 *
956 * On failure submitting we just stop submitting and return error;
957 * wa_urb_enqueue_b() will execute the completion path
958 */
959static int __wa_xfer_submit(struct wa_xfer *xfer)
960{
961 int result;
962 struct wahc *wa = xfer->wa;
963 struct device *dev = &wa->usb_iface->dev;
964 unsigned cnt;
965 struct wa_seg *seg;
966 unsigned long flags;
967 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
968 size_t maxrequests = le16_to_cpu(rpipe->descr.wRequests);
969 u8 available;
970 u8 empty;
971
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100972 spin_lock_irqsave(&wa->xfer_list_lock, flags);
973 list_add_tail(&xfer->list_node, &wa->xfer_list);
974 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
975
976 BUG_ON(atomic_read(&rpipe->segs_available) > maxrequests);
977 result = 0;
978 spin_lock_irqsave(&rpipe->seg_lock, flags);
979 for (cnt = 0; cnt < xfer->segs; cnt++) {
980 available = atomic_read(&rpipe->segs_available);
981 empty = list_empty(&rpipe->seg_list);
982 seg = xfer->seg[cnt];
David Vrabelbce83692008-12-22 18:22:50 +0000983 dev_dbg(dev, "xfer %p#%u: available %u empty %u (%s)\n",
984 xfer, cnt, available, empty,
985 available == 0 || !empty ? "delayed" : "submitted");
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100986 if (available == 0 || !empty) {
David Vrabelbce83692008-12-22 18:22:50 +0000987 dev_dbg(dev, "xfer %p#%u: delayed\n", xfer, cnt);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100988 seg->status = WA_SEG_DELAYED;
989 list_add_tail(&seg->list_node, &rpipe->seg_list);
990 } else {
991 result = __wa_seg_submit(rpipe, xfer, seg);
David Vrabelbce83692008-12-22 18:22:50 +0000992 if (result < 0) {
993 __wa_xfer_abort(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100994 goto error_seg_submit;
David Vrabelbce83692008-12-22 18:22:50 +0000995 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100996 }
997 xfer->segs_submitted++;
998 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100999error_seg_submit:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001000 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001001 return result;
1002}
1003
1004/*
1005 * Second part of a URB/transfer enqueuement
1006 *
1007 * Assumes this comes from wa_urb_enqueue() [maybe through
1008 * wa_urb_enqueue_run()]. At this point:
1009 *
1010 * xfer->wa filled and refcounted
1011 * xfer->ep filled with rpipe refcounted if
1012 * delayed == 0
1013 * xfer->urb filled and refcounted (this is the case when called
1014 * from wa_urb_enqueue() as we come from usb_submit_urb()
1015 * and when called by wa_urb_enqueue_run(), as we took an
1016 * extra ref dropped by _run() after we return).
1017 * xfer->gfp filled
1018 *
1019 * If we fail at __wa_xfer_submit(), then we just check if we are done
1020 * and if so, we run the completion procedure. However, if we are not
1021 * yet done, we do nothing and wait for the completion handlers from
1022 * the submitted URBs or from the xfer-result path to kick in. If xfer
1023 * result never kicks in, the xfer will timeout from the USB code and
1024 * dequeue() will be called.
1025 */
1026static void wa_urb_enqueue_b(struct wa_xfer *xfer)
1027{
1028 int result;
1029 unsigned long flags;
1030 struct urb *urb = xfer->urb;
1031 struct wahc *wa = xfer->wa;
1032 struct wusbhc *wusbhc = wa->wusb;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001033 struct wusb_dev *wusb_dev;
1034 unsigned done;
1035
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001036 result = rpipe_get_by_ep(wa, xfer->ep, urb, xfer->gfp);
1037 if (result < 0)
1038 goto error_rpipe_get;
1039 result = -ENODEV;
1040 /* FIXME: segmentation broken -- kills DWA */
1041 mutex_lock(&wusbhc->mutex); /* get a WUSB dev */
Jiri Slaby49fa0922009-03-11 21:47:40 +01001042 if (urb->dev == NULL) {
1043 mutex_unlock(&wusbhc->mutex);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001044 goto error_dev_gone;
Jiri Slaby49fa0922009-03-11 21:47:40 +01001045 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001046 wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev);
1047 if (wusb_dev == NULL) {
1048 mutex_unlock(&wusbhc->mutex);
1049 goto error_dev_gone;
1050 }
1051 mutex_unlock(&wusbhc->mutex);
1052
1053 spin_lock_irqsave(&xfer->lock, flags);
1054 xfer->wusb_dev = wusb_dev;
1055 result = urb->status;
1056 if (urb->status != -EINPROGRESS)
1057 goto error_dequeued;
1058
1059 result = __wa_xfer_setup(xfer, urb);
1060 if (result < 0)
1061 goto error_xfer_setup;
1062 result = __wa_xfer_submit(xfer);
1063 if (result < 0)
1064 goto error_xfer_submit;
1065 spin_unlock_irqrestore(&xfer->lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001066 return;
1067
1068 /* this is basically wa_xfer_completion() broken up wa_xfer_giveback()
1069 * does a wa_xfer_put() that will call wa_xfer_destroy() and clean
1070 * upundo setup().
1071 */
1072error_xfer_setup:
1073error_dequeued:
1074 spin_unlock_irqrestore(&xfer->lock, flags);
1075 /* FIXME: segmentation broken, kills DWA */
1076 if (wusb_dev)
1077 wusb_dev_put(wusb_dev);
1078error_dev_gone:
1079 rpipe_put(xfer->ep->hcpriv);
1080error_rpipe_get:
1081 xfer->result = result;
1082 wa_xfer_giveback(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001083 return;
1084
1085error_xfer_submit:
1086 done = __wa_xfer_is_done(xfer);
1087 xfer->result = result;
1088 spin_unlock_irqrestore(&xfer->lock, flags);
1089 if (done)
1090 wa_xfer_completion(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001091}
1092
1093/*
1094 * Execute the delayed transfers in the Wire Adapter @wa
1095 *
1096 * We need to be careful here, as dequeue() could be called in the
1097 * middle. That's why we do the whole thing under the
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001098 * wa->xfer_list_lock. If dequeue() jumps in, it first locks xfer->lock
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001099 * and then checks the list -- so as we would be acquiring in inverse
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001100 * order, we move the delayed list to a separate list while locked and then
1101 * submit them without the list lock held.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001102 */
1103void wa_urb_enqueue_run(struct work_struct *ws)
1104{
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001105 struct wahc *wa = container_of(ws, struct wahc, xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001106 struct wa_xfer *xfer, *next;
1107 struct urb *urb;
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001108 LIST_HEAD(tmp_list);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001109
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001110 /* Create a copy of the wa->xfer_delayed_list while holding the lock */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001111 spin_lock_irq(&wa->xfer_list_lock);
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001112 list_cut_position(&tmp_list, &wa->xfer_delayed_list,
1113 wa->xfer_delayed_list.prev);
1114 spin_unlock_irq(&wa->xfer_list_lock);
1115
1116 /*
1117 * enqueue from temp list without list lock held since wa_urb_enqueue_b
1118 * can take xfer->lock as well as lock mutexes.
1119 */
1120 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001121 list_del_init(&xfer->list_node);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001122
1123 urb = xfer->urb;
1124 wa_urb_enqueue_b(xfer);
1125 usb_put_urb(urb); /* taken when queuing */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001126 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001127}
1128EXPORT_SYMBOL_GPL(wa_urb_enqueue_run);
1129
1130/*
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001131 * Process the errored transfers on the Wire Adapter outside of interrupt.
1132 */
1133void wa_process_errored_transfers_run(struct work_struct *ws)
1134{
1135 struct wahc *wa = container_of(ws, struct wahc, xfer_error_work);
1136 struct wa_xfer *xfer, *next;
1137 LIST_HEAD(tmp_list);
1138
1139 pr_info("%s: Run delayed STALL processing.\n", __func__);
1140
1141 /* Create a copy of the wa->xfer_errored_list while holding the lock */
1142 spin_lock_irq(&wa->xfer_list_lock);
1143 list_cut_position(&tmp_list, &wa->xfer_errored_list,
1144 wa->xfer_errored_list.prev);
1145 spin_unlock_irq(&wa->xfer_list_lock);
1146
1147 /*
1148 * run rpipe_clear_feature_stalled from temp list without list lock
1149 * held.
1150 */
1151 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
1152 struct usb_host_endpoint *ep;
1153 unsigned long flags;
1154 struct wa_rpipe *rpipe;
1155
1156 spin_lock_irqsave(&xfer->lock, flags);
1157 ep = xfer->ep;
1158 rpipe = ep->hcpriv;
1159 spin_unlock_irqrestore(&xfer->lock, flags);
1160
1161 /* clear RPIPE feature stalled without holding a lock. */
1162 rpipe_clear_feature_stalled(wa, ep);
1163
1164 /* complete the xfer. This removes it from the tmp list. */
1165 wa_xfer_completion(xfer);
1166
1167 /* check for work. */
1168 wa_xfer_delayed_run(rpipe);
1169 }
1170}
1171EXPORT_SYMBOL_GPL(wa_process_errored_transfers_run);
1172
1173/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001174 * Submit a transfer to the Wire Adapter in a delayed way
1175 *
1176 * The process of enqueuing involves possible sleeps() [see
1177 * enqueue_b(), for the rpipe_get() and the mutex_lock()]. If we are
1178 * in an atomic section, we defer the enqueue_b() call--else we call direct.
1179 *
1180 * @urb: We own a reference to it done by the HCI Linux USB stack that
1181 * will be given up by calling usb_hcd_giveback_urb() or by
1182 * returning error from this function -> ergo we don't have to
1183 * refcount it.
1184 */
1185int wa_urb_enqueue(struct wahc *wa, struct usb_host_endpoint *ep,
1186 struct urb *urb, gfp_t gfp)
1187{
1188 int result;
1189 struct device *dev = &wa->usb_iface->dev;
1190 struct wa_xfer *xfer;
1191 unsigned long my_flags;
1192 unsigned cant_sleep = irqs_disabled() | in_atomic();
1193
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001194 if ((urb->transfer_buffer == NULL)
1195 && (urb->sg == NULL)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001196 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1197 && urb->transfer_buffer_length != 0) {
1198 dev_err(dev, "BUG? urb %p: NULL xfer buffer & NODMA\n", urb);
1199 dump_stack();
1200 }
1201
1202 result = -ENOMEM;
1203 xfer = kzalloc(sizeof(*xfer), gfp);
1204 if (xfer == NULL)
1205 goto error_kmalloc;
1206
1207 result = -ENOENT;
1208 if (urb->status != -EINPROGRESS) /* cancelled */
1209 goto error_dequeued; /* before starting? */
1210 wa_xfer_init(xfer);
1211 xfer->wa = wa_get(wa);
1212 xfer->urb = urb;
1213 xfer->gfp = gfp;
1214 xfer->ep = ep;
1215 urb->hcpriv = xfer;
David Vrabelbce83692008-12-22 18:22:50 +00001216
1217 dev_dbg(dev, "xfer %p urb %p pipe 0x%02x [%d bytes] %s %s %s\n",
1218 xfer, urb, urb->pipe, urb->transfer_buffer_length,
1219 urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? "dma" : "nodma",
1220 urb->pipe & USB_DIR_IN ? "inbound" : "outbound",
1221 cant_sleep ? "deferred" : "inline");
1222
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001223 if (cant_sleep) {
1224 usb_get_urb(urb);
1225 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1226 list_add_tail(&xfer->list_node, &wa->xfer_delayed_list);
1227 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001228 queue_work(wusbd, &wa->xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001229 } else {
1230 wa_urb_enqueue_b(xfer);
1231 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001232 return 0;
1233
1234error_dequeued:
1235 kfree(xfer);
1236error_kmalloc:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001237 return result;
1238}
1239EXPORT_SYMBOL_GPL(wa_urb_enqueue);
1240
1241/*
1242 * Dequeue a URB and make sure uwb_hcd_giveback_urb() [completion
1243 * handler] is called.
1244 *
1245 * Until a transfer goes successfully through wa_urb_enqueue() it
1246 * needs to be dequeued with completion calling; when stuck in delayed
1247 * or before wa_xfer_setup() is called, we need to do completion.
1248 *
1249 * not setup If there is no hcpriv yet, that means that that enqueue
1250 * still had no time to set the xfer up. Because
1251 * urb->status should be other than -EINPROGRESS,
1252 * enqueue() will catch that and bail out.
1253 *
1254 * If the transfer has gone through setup, we just need to clean it
1255 * up. If it has gone through submit(), we have to abort it [with an
1256 * asynch request] and then make sure we cancel each segment.
1257 *
1258 */
1259int wa_urb_dequeue(struct wahc *wa, struct urb *urb)
1260{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001261 unsigned long flags, flags2;
1262 struct wa_xfer *xfer;
1263 struct wa_seg *seg;
1264 struct wa_rpipe *rpipe;
1265 unsigned cnt;
1266 unsigned rpipe_ready = 0;
1267
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001268 xfer = urb->hcpriv;
1269 if (xfer == NULL) {
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001270 /*
1271 * Nothing setup yet enqueue will see urb->status !=
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001272 * -EINPROGRESS (by hcd layer) and bail out with
1273 * error, no need to do completion
1274 */
1275 BUG_ON(urb->status == -EINPROGRESS);
1276 goto out;
1277 }
1278 spin_lock_irqsave(&xfer->lock, flags);
1279 rpipe = xfer->ep->hcpriv;
1280 /* Check the delayed list -> if there, release and complete */
1281 spin_lock_irqsave(&wa->xfer_list_lock, flags2);
1282 if (!list_empty(&xfer->list_node) && xfer->seg == NULL)
1283 goto dequeue_delayed;
1284 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1285 if (xfer->seg == NULL) /* still hasn't reached */
1286 goto out_unlock; /* setup(), enqueue_b() completes */
1287 /* Ok, the xfer is in flight already, it's been setup and submitted.*/
1288 __wa_xfer_abort(xfer);
1289 for (cnt = 0; cnt < xfer->segs; cnt++) {
1290 seg = xfer->seg[cnt];
1291 switch (seg->status) {
1292 case WA_SEG_NOTREADY:
1293 case WA_SEG_READY:
1294 printk(KERN_ERR "xfer %p#%u: dequeue bad state %u\n",
1295 xfer, cnt, seg->status);
1296 WARN_ON(1);
1297 break;
1298 case WA_SEG_DELAYED:
1299 seg->status = WA_SEG_ABORTED;
1300 spin_lock_irqsave(&rpipe->seg_lock, flags2);
1301 list_del(&seg->list_node);
1302 xfer->segs_done++;
1303 rpipe_ready = rpipe_avail_inc(rpipe);
1304 spin_unlock_irqrestore(&rpipe->seg_lock, flags2);
1305 break;
1306 case WA_SEG_SUBMITTED:
1307 seg->status = WA_SEG_ABORTED;
1308 usb_unlink_urb(&seg->urb);
1309 if (xfer->is_inbound == 0)
1310 usb_unlink_urb(seg->dto_urb);
1311 xfer->segs_done++;
1312 rpipe_ready = rpipe_avail_inc(rpipe);
1313 break;
1314 case WA_SEG_PENDING:
1315 seg->status = WA_SEG_ABORTED;
1316 xfer->segs_done++;
1317 rpipe_ready = rpipe_avail_inc(rpipe);
1318 break;
1319 case WA_SEG_DTI_PENDING:
1320 usb_unlink_urb(wa->dti_urb);
1321 seg->status = WA_SEG_ABORTED;
1322 xfer->segs_done++;
1323 rpipe_ready = rpipe_avail_inc(rpipe);
1324 break;
1325 case WA_SEG_DONE:
1326 case WA_SEG_ERROR:
1327 case WA_SEG_ABORTED:
1328 break;
1329 }
1330 }
1331 xfer->result = urb->status; /* -ENOENT or -ECONNRESET */
1332 __wa_xfer_is_done(xfer);
1333 spin_unlock_irqrestore(&xfer->lock, flags);
1334 wa_xfer_completion(xfer);
1335 if (rpipe_ready)
1336 wa_xfer_delayed_run(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001337 return 0;
1338
1339out_unlock:
1340 spin_unlock_irqrestore(&xfer->lock, flags);
1341out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001342 return 0;
1343
1344dequeue_delayed:
1345 list_del_init(&xfer->list_node);
1346 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1347 xfer->result = urb->status;
1348 spin_unlock_irqrestore(&xfer->lock, flags);
1349 wa_xfer_giveback(xfer);
1350 usb_put_urb(urb); /* we got a ref in enqueue() */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001351 return 0;
1352}
1353EXPORT_SYMBOL_GPL(wa_urb_dequeue);
1354
1355/*
1356 * Translation from WA status codes (WUSB1.0 Table 8.15) to errno
1357 * codes
1358 *
1359 * Positive errno values are internal inconsistencies and should be
1360 * flagged louder. Negative are to be passed up to the user in the
1361 * normal way.
1362 *
1363 * @status: USB WA status code -- high two bits are stripped.
1364 */
1365static int wa_xfer_status_to_errno(u8 status)
1366{
1367 int errno;
1368 u8 real_status = status;
1369 static int xlat[] = {
1370 [WA_XFER_STATUS_SUCCESS] = 0,
1371 [WA_XFER_STATUS_HALTED] = -EPIPE,
1372 [WA_XFER_STATUS_DATA_BUFFER_ERROR] = -ENOBUFS,
1373 [WA_XFER_STATUS_BABBLE] = -EOVERFLOW,
1374 [WA_XFER_RESERVED] = EINVAL,
1375 [WA_XFER_STATUS_NOT_FOUND] = 0,
1376 [WA_XFER_STATUS_INSUFFICIENT_RESOURCE] = -ENOMEM,
1377 [WA_XFER_STATUS_TRANSACTION_ERROR] = -EILSEQ,
1378 [WA_XFER_STATUS_ABORTED] = -EINTR,
1379 [WA_XFER_STATUS_RPIPE_NOT_READY] = EINVAL,
1380 [WA_XFER_INVALID_FORMAT] = EINVAL,
1381 [WA_XFER_UNEXPECTED_SEGMENT_NUMBER] = EINVAL,
1382 [WA_XFER_STATUS_RPIPE_TYPE_MISMATCH] = EINVAL,
1383 };
1384 status &= 0x3f;
1385
1386 if (status == 0)
1387 return 0;
1388 if (status >= ARRAY_SIZE(xlat)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02001389 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001390 "Unknown WA transfer status 0x%02x\n",
1391 __func__, real_status);
1392 return -EINVAL;
1393 }
1394 errno = xlat[status];
1395 if (unlikely(errno > 0)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02001396 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001397 "Inconsistent WA status: 0x%02x\n",
1398 __func__, real_status);
1399 errno = -errno;
1400 }
1401 return errno;
1402}
1403
1404/*
1405 * Process a xfer result completion message
1406 *
1407 * inbound transfers: need to schedule a DTI read
1408 *
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001409 * FIXME: this function needs to be broken up in parts
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001410 */
1411static void wa_xfer_result_chew(struct wahc *wa, struct wa_xfer *xfer)
1412{
1413 int result;
1414 struct device *dev = &wa->usb_iface->dev;
1415 unsigned long flags;
1416 u8 seg_idx;
1417 struct wa_seg *seg;
1418 struct wa_rpipe *rpipe;
1419 struct wa_xfer_result *xfer_result = wa->xfer_result;
1420 u8 done = 0;
1421 u8 usb_status;
1422 unsigned rpipe_ready = 0;
1423
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001424 spin_lock_irqsave(&xfer->lock, flags);
1425 seg_idx = xfer_result->bTransferSegment & 0x7f;
1426 if (unlikely(seg_idx >= xfer->segs))
1427 goto error_bad_seg;
1428 seg = xfer->seg[seg_idx];
1429 rpipe = xfer->ep->hcpriv;
1430 usb_status = xfer_result->bTransferStatus;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001431 dev_dbg(dev, "xfer %p#%u: bTransferStatus 0x%02x (seg status %u)\n",
David Vrabelbce83692008-12-22 18:22:50 +00001432 xfer, seg_idx, usb_status, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001433 if (seg->status == WA_SEG_ABORTED
1434 || seg->status == WA_SEG_ERROR) /* already handled */
1435 goto segment_aborted;
1436 if (seg->status == WA_SEG_SUBMITTED) /* ops, got here */
1437 seg->status = WA_SEG_PENDING; /* before wa_seg{_dto}_cb() */
1438 if (seg->status != WA_SEG_PENDING) {
1439 if (printk_ratelimit())
1440 dev_err(dev, "xfer %p#%u: Bad segment state %u\n",
1441 xfer, seg_idx, seg->status);
1442 seg->status = WA_SEG_PENDING; /* workaround/"fix" it */
1443 }
1444 if (usb_status & 0x80) {
1445 seg->result = wa_xfer_status_to_errno(usb_status);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001446 dev_err(dev, "DTI: xfer %p#:%08X:%u failed (0x%02x)\n",
1447 xfer, xfer->id, seg->index, usb_status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001448 goto error_complete;
1449 }
1450 /* FIXME: we ignore warnings, tally them for stats */
1451 if (usb_status & 0x40) /* Warning?... */
1452 usb_status = 0; /* ... pass */
1453 if (xfer->is_inbound) { /* IN data phase: read to buffer */
1454 seg->status = WA_SEG_DTI_PENDING;
1455 BUG_ON(wa->buf_in_urb->status == -EINPROGRESS);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001456 /* this should always be 0 before a resubmit. */
1457 wa->buf_in_urb->num_mapped_sgs = 0;
1458
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001459 if (xfer->is_dma) {
1460 wa->buf_in_urb->transfer_dma =
1461 xfer->urb->transfer_dma
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001462 + (seg_idx * xfer->seg_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001463 wa->buf_in_urb->transfer_flags
1464 |= URB_NO_TRANSFER_DMA_MAP;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001465 wa->buf_in_urb->transfer_buffer = NULL;
1466 wa->buf_in_urb->sg = NULL;
1467 wa->buf_in_urb->num_sgs = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001468 } else {
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001469 /* do buffer or SG processing. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001470 wa->buf_in_urb->transfer_flags
1471 &= ~URB_NO_TRANSFER_DMA_MAP;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001472
1473 if (xfer->urb->transfer_buffer) {
1474 wa->buf_in_urb->transfer_buffer =
1475 xfer->urb->transfer_buffer
1476 + (seg_idx * xfer->seg_size);
1477 wa->buf_in_urb->sg = NULL;
1478 wa->buf_in_urb->num_sgs = 0;
1479 } else {
1480 /* allocate an SG list to store seg_size bytes
1481 and copy the subset of the xfer->urb->sg
1482 that matches the buffer subset we are
1483 about to read. */
1484 wa->buf_in_urb->sg = wa_xfer_create_subset_sg(
1485 xfer->urb->sg,
1486 seg_idx * xfer->seg_size,
1487 le32_to_cpu(
1488 xfer_result->dwTransferLength),
1489 &(wa->buf_in_urb->num_sgs));
1490
1491 if (!(wa->buf_in_urb->sg)) {
1492 wa->buf_in_urb->num_sgs = 0;
1493 goto error_sg_alloc;
1494 }
1495 wa->buf_in_urb->transfer_buffer = NULL;
1496 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001497 }
1498 wa->buf_in_urb->transfer_buffer_length =
1499 le32_to_cpu(xfer_result->dwTransferLength);
1500 wa->buf_in_urb->context = seg;
1501 result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
1502 if (result < 0)
1503 goto error_submit_buf_in;
1504 } else {
1505 /* OUT data phase, complete it -- */
1506 seg->status = WA_SEG_DONE;
1507 seg->result = le32_to_cpu(xfer_result->dwTransferLength);
1508 xfer->segs_done++;
1509 rpipe_ready = rpipe_avail_inc(rpipe);
1510 done = __wa_xfer_is_done(xfer);
1511 }
1512 spin_unlock_irqrestore(&xfer->lock, flags);
1513 if (done)
1514 wa_xfer_completion(xfer);
1515 if (rpipe_ready)
1516 wa_xfer_delayed_run(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001517 return;
1518
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001519error_submit_buf_in:
1520 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
1521 dev_err(dev, "DTI: URB max acceptable errors "
1522 "exceeded, resetting device\n");
1523 wa_reset_all(wa);
1524 }
1525 if (printk_ratelimit())
1526 dev_err(dev, "xfer %p#%u: can't submit DTI data phase: %d\n",
1527 xfer, seg_idx, result);
1528 seg->result = result;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001529 kfree(wa->buf_in_urb->sg);
1530error_sg_alloc:
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001531 __wa_xfer_abort(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001532error_complete:
1533 seg->status = WA_SEG_ERROR;
1534 xfer->segs_done++;
1535 rpipe_ready = rpipe_avail_inc(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001536 done = __wa_xfer_is_done(xfer);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001537 /*
1538 * queue work item to clear STALL for control endpoints.
1539 * Otherwise, let endpoint_reset take care of it.
1540 */
1541 if (((usb_status & 0x3f) == WA_XFER_STATUS_HALTED) &&
1542 usb_endpoint_xfer_control(&xfer->ep->desc) &&
1543 done) {
1544
1545 dev_info(dev, "Control EP stall. Queue delayed work.\n");
1546 spin_lock_irq(&wa->xfer_list_lock);
1547 /* remove xfer from xfer_list. */
1548 list_del(&xfer->list_node);
1549 /* add xfer to xfer_errored_list. */
1550 list_add_tail(&xfer->list_node, &wa->xfer_errored_list);
1551 spin_unlock_irq(&wa->xfer_list_lock);
1552 spin_unlock_irqrestore(&xfer->lock, flags);
1553 queue_work(wusbd, &wa->xfer_error_work);
1554 } else {
1555 spin_unlock_irqrestore(&xfer->lock, flags);
1556 if (done)
1557 wa_xfer_completion(xfer);
1558 if (rpipe_ready)
1559 wa_xfer_delayed_run(rpipe);
1560 }
1561
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001562 return;
1563
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001564error_bad_seg:
1565 spin_unlock_irqrestore(&xfer->lock, flags);
1566 wa_urb_dequeue(wa, xfer->urb);
1567 if (printk_ratelimit())
1568 dev_err(dev, "xfer %p#%u: bad segment\n", xfer, seg_idx);
1569 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
1570 dev_err(dev, "DTI: URB max acceptable errors "
1571 "exceeded, resetting device\n");
1572 wa_reset_all(wa);
1573 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001574 return;
1575
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001576segment_aborted:
1577 /* nothing to do, as the aborter did the completion */
1578 spin_unlock_irqrestore(&xfer->lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001579}
1580
1581/*
1582 * Callback for the IN data phase
1583 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001584 * If successful transition state; otherwise, take a note of the
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001585 * error, mark this segment done and try completion.
1586 *
1587 * Note we don't access until we are sure that the transfer hasn't
1588 * been cancelled (ECONNRESET, ENOENT), which could mean that
1589 * seg->xfer could be already gone.
1590 */
1591static void wa_buf_in_cb(struct urb *urb)
1592{
1593 struct wa_seg *seg = urb->context;
1594 struct wa_xfer *xfer = seg->xfer;
1595 struct wahc *wa;
1596 struct device *dev;
1597 struct wa_rpipe *rpipe;
1598 unsigned rpipe_ready;
1599 unsigned long flags;
1600 u8 done = 0;
1601
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001602 /* free the sg if it was used. */
1603 kfree(urb->sg);
1604 urb->sg = NULL;
1605
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001606 switch (urb->status) {
1607 case 0:
1608 spin_lock_irqsave(&xfer->lock, flags);
1609 wa = xfer->wa;
1610 dev = &wa->usb_iface->dev;
1611 rpipe = xfer->ep->hcpriv;
David Vrabelbce83692008-12-22 18:22:50 +00001612 dev_dbg(dev, "xfer %p#%u: data in done (%zu bytes)\n",
1613 xfer, seg->index, (size_t)urb->actual_length);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001614 seg->status = WA_SEG_DONE;
1615 seg->result = urb->actual_length;
1616 xfer->segs_done++;
1617 rpipe_ready = rpipe_avail_inc(rpipe);
1618 done = __wa_xfer_is_done(xfer);
1619 spin_unlock_irqrestore(&xfer->lock, flags);
1620 if (done)
1621 wa_xfer_completion(xfer);
1622 if (rpipe_ready)
1623 wa_xfer_delayed_run(rpipe);
1624 break;
1625 case -ECONNRESET: /* URB unlinked; no need to do anything */
1626 case -ENOENT: /* as it was done by the who unlinked us */
1627 break;
1628 default: /* Other errors ... */
1629 spin_lock_irqsave(&xfer->lock, flags);
1630 wa = xfer->wa;
1631 dev = &wa->usb_iface->dev;
1632 rpipe = xfer->ep->hcpriv;
1633 if (printk_ratelimit())
1634 dev_err(dev, "xfer %p#%u: data in error %d\n",
1635 xfer, seg->index, urb->status);
1636 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
1637 EDC_ERROR_TIMEFRAME)){
1638 dev_err(dev, "DTO: URB max acceptable errors "
1639 "exceeded, resetting device\n");
1640 wa_reset_all(wa);
1641 }
1642 seg->status = WA_SEG_ERROR;
1643 seg->result = urb->status;
1644 xfer->segs_done++;
1645 rpipe_ready = rpipe_avail_inc(rpipe);
1646 __wa_xfer_abort(xfer);
1647 done = __wa_xfer_is_done(xfer);
1648 spin_unlock_irqrestore(&xfer->lock, flags);
1649 if (done)
1650 wa_xfer_completion(xfer);
1651 if (rpipe_ready)
1652 wa_xfer_delayed_run(rpipe);
1653 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001654}
1655
1656/*
1657 * Handle an incoming transfer result buffer
1658 *
1659 * Given a transfer result buffer, it completes the transfer (possibly
1660 * scheduling and buffer in read) and then resubmits the DTI URB for a
1661 * new transfer result read.
1662 *
1663 *
1664 * The xfer_result DTI URB state machine
1665 *
1666 * States: OFF | RXR (Read-Xfer-Result) | RBI (Read-Buffer-In)
1667 *
1668 * We start in OFF mode, the first xfer_result notification [through
1669 * wa_handle_notif_xfer()] moves us to RXR by posting the DTI-URB to
1670 * read.
1671 *
1672 * We receive a buffer -- if it is not a xfer_result, we complain and
1673 * repost the DTI-URB. If it is a xfer_result then do the xfer seg
1674 * request accounting. If it is an IN segment, we move to RBI and post
1675 * a BUF-IN-URB to the right buffer. The BUF-IN-URB callback will
1676 * repost the DTI-URB and move to RXR state. if there was no IN
1677 * segment, it will repost the DTI-URB.
1678 *
1679 * We go back to OFF when we detect a ENOENT or ESHUTDOWN (or too many
1680 * errors) in the URBs.
1681 */
1682static void wa_xfer_result_cb(struct urb *urb)
1683{
1684 int result;
1685 struct wahc *wa = urb->context;
1686 struct device *dev = &wa->usb_iface->dev;
1687 struct wa_xfer_result *xfer_result;
1688 u32 xfer_id;
1689 struct wa_xfer *xfer;
1690 u8 usb_status;
1691
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001692 BUG_ON(wa->dti_urb != urb);
1693 switch (wa->dti_urb->status) {
1694 case 0:
1695 /* We have a xfer result buffer; check it */
David Vrabelbce83692008-12-22 18:22:50 +00001696 dev_dbg(dev, "DTI: xfer result %d bytes at %p\n",
1697 urb->actual_length, urb->transfer_buffer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001698 if (wa->dti_urb->actual_length != sizeof(*xfer_result)) {
1699 dev_err(dev, "DTI Error: xfer result--bad size "
1700 "xfer result (%d bytes vs %zu needed)\n",
1701 urb->actual_length, sizeof(*xfer_result));
1702 break;
1703 }
1704 xfer_result = wa->xfer_result;
1705 if (xfer_result->hdr.bLength != sizeof(*xfer_result)) {
1706 dev_err(dev, "DTI Error: xfer result--"
1707 "bad header length %u\n",
1708 xfer_result->hdr.bLength);
1709 break;
1710 }
1711 if (xfer_result->hdr.bNotifyType != WA_XFER_RESULT) {
1712 dev_err(dev, "DTI Error: xfer result--"
1713 "bad header type 0x%02x\n",
1714 xfer_result->hdr.bNotifyType);
1715 break;
1716 }
1717 usb_status = xfer_result->bTransferStatus & 0x3f;
1718 if (usb_status == WA_XFER_STATUS_ABORTED
1719 || usb_status == WA_XFER_STATUS_NOT_FOUND)
1720 /* taken care of already */
1721 break;
1722 xfer_id = xfer_result->dwTransferID;
1723 xfer = wa_xfer_get_by_id(wa, xfer_id);
1724 if (xfer == NULL) {
1725 /* FIXME: transaction might have been cancelled */
1726 dev_err(dev, "DTI Error: xfer result--"
1727 "unknown xfer 0x%08x (status 0x%02x)\n",
1728 xfer_id, usb_status);
1729 break;
1730 }
1731 wa_xfer_result_chew(wa, xfer);
1732 wa_xfer_put(xfer);
1733 break;
1734 case -ENOENT: /* (we killed the URB)...so, no broadcast */
1735 case -ESHUTDOWN: /* going away! */
1736 dev_dbg(dev, "DTI: going down! %d\n", urb->status);
1737 goto out;
1738 default:
1739 /* Unknown error */
1740 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS,
1741 EDC_ERROR_TIMEFRAME)) {
1742 dev_err(dev, "DTI: URB max acceptable errors "
1743 "exceeded, resetting device\n");
1744 wa_reset_all(wa);
1745 goto out;
1746 }
1747 if (printk_ratelimit())
1748 dev_err(dev, "DTI: URB error %d\n", urb->status);
1749 break;
1750 }
1751 /* Resubmit the DTI URB */
1752 result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
1753 if (result < 0) {
1754 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
1755 "resetting\n", result);
1756 wa_reset_all(wa);
1757 }
1758out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001759 return;
1760}
1761
1762/*
1763 * Transfer complete notification
1764 *
1765 * Called from the notif.c code. We get a notification on EP2 saying
1766 * that some endpoint has some transfer result data available. We are
1767 * about to read it.
1768 *
1769 * To speed up things, we always have a URB reading the DTI URB; we
1770 * don't really set it up and start it until the first xfer complete
1771 * notification arrives, which is what we do here.
1772 *
1773 * Follow up in wa_xfer_result_cb(), as that's where the whole state
1774 * machine starts.
1775 *
1776 * So here we just initialize the DTI URB for reading transfer result
1777 * notifications and also the buffer-in URB, for reading buffers. Then
1778 * we just submit the DTI URB.
1779 *
1780 * @wa shall be referenced
1781 */
1782void wa_handle_notif_xfer(struct wahc *wa, struct wa_notif_hdr *notif_hdr)
1783{
1784 int result;
1785 struct device *dev = &wa->usb_iface->dev;
1786 struct wa_notif_xfer *notif_xfer;
1787 const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd;
1788
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001789 notif_xfer = container_of(notif_hdr, struct wa_notif_xfer, hdr);
1790 BUG_ON(notif_hdr->bNotifyType != WA_NOTIF_TRANSFER);
1791
1792 if ((0x80 | notif_xfer->bEndpoint) != dti_epd->bEndpointAddress) {
1793 /* FIXME: hardcoded limitation, adapt */
1794 dev_err(dev, "BUG: DTI ep is %u, not %u (hack me)\n",
1795 notif_xfer->bEndpoint, dti_epd->bEndpointAddress);
1796 goto error;
1797 }
1798 if (wa->dti_urb != NULL) /* DTI URB already started */
1799 goto out;
1800
1801 wa->dti_urb = usb_alloc_urb(0, GFP_KERNEL);
1802 if (wa->dti_urb == NULL) {
1803 dev_err(dev, "Can't allocate DTI URB\n");
1804 goto error_dti_urb_alloc;
1805 }
1806 usb_fill_bulk_urb(
1807 wa->dti_urb, wa->usb_dev,
1808 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
1809 wa->xfer_result, wa->xfer_result_size,
1810 wa_xfer_result_cb, wa);
1811
1812 wa->buf_in_urb = usb_alloc_urb(0, GFP_KERNEL);
1813 if (wa->buf_in_urb == NULL) {
1814 dev_err(dev, "Can't allocate BUF-IN URB\n");
1815 goto error_buf_in_urb_alloc;
1816 }
1817 usb_fill_bulk_urb(
1818 wa->buf_in_urb, wa->usb_dev,
1819 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
1820 NULL, 0, wa_buf_in_cb, wa);
1821 result = usb_submit_urb(wa->dti_urb, GFP_KERNEL);
1822 if (result < 0) {
1823 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
1824 "resetting\n", result);
1825 goto error_dti_urb_submit;
1826 }
1827out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001828 return;
1829
1830error_dti_urb_submit:
1831 usb_put_urb(wa->buf_in_urb);
1832error_buf_in_urb_alloc:
1833 usb_put_urb(wa->dti_urb);
1834 wa->dti_urb = NULL;
1835error_dti_urb_alloc:
1836error:
1837 wa_reset_all(wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001838}