blob: f614fb1ed77f8a217899e518bbd4993f709a2262 [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 {
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500117 struct urb tr_urb; /* transfer request urb. */
118 struct urb *dto_urb; /* for data output. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100119 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
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500128static inline void wa_seg_init(struct wa_seg *seg)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100129{
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500130 usb_init_urb(&seg->tr_urb);
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500131
132 /* set the remaining memory to 0. */
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500133 memset(((void *)seg) + sizeof(seg->tr_urb), 0,
134 sizeof(*seg) - sizeof(seg->tr_urb));
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100135}
136
137/*
138 * Protected by xfer->lock
139 *
140 */
141struct wa_xfer {
142 struct kref refcnt;
143 struct list_head list_node;
144 spinlock_t lock;
145 u32 id;
146
147 struct wahc *wa; /* Wire adapter we are plugged to */
148 struct usb_host_endpoint *ep;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300149 struct urb *urb; /* URB we are transferring for */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100150 struct wa_seg **seg; /* transfer segments */
151 u8 segs, segs_submitted, segs_done;
152 unsigned is_inbound:1;
153 unsigned is_dma:1;
154 size_t seg_size;
155 int result;
156
157 gfp_t gfp; /* allocation mask */
158
159 struct wusb_dev *wusb_dev; /* for activity timestamps */
160};
161
162static inline void wa_xfer_init(struct wa_xfer *xfer)
163{
164 kref_init(&xfer->refcnt);
165 INIT_LIST_HEAD(&xfer->list_node);
166 spin_lock_init(&xfer->lock);
167}
168
169/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300170 * Destroy a transfer structure
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100171 *
Thomas Pugliese79731cb2013-08-15 14:37:42 -0500172 * Note that freeing xfer->seg[cnt]->urb will free the containing
173 * xfer->seg[cnt] memory that was allocated by __wa_xfer_setup_segs.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100174 */
175static void wa_xfer_destroy(struct kref *_xfer)
176{
177 struct wa_xfer *xfer = container_of(_xfer, struct wa_xfer, refcnt);
178 if (xfer->seg) {
179 unsigned cnt;
180 for (cnt = 0; cnt < xfer->segs; cnt++) {
Thomas Pugliesed9936702013-09-26 14:08:13 -0500181 if (xfer->seg[cnt]) {
182 if (xfer->seg[cnt]->dto_urb) {
183 kfree(xfer->seg[cnt]->dto_urb->sg);
184 usb_free_urb(xfer->seg[cnt]->dto_urb);
185 }
186 usb_free_urb(&xfer->seg[cnt]->tr_urb);
187 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100188 }
Thomas Pugliesed9936702013-09-26 14:08:13 -0500189 kfree(xfer->seg);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100190 }
191 kfree(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100192}
193
194static void wa_xfer_get(struct wa_xfer *xfer)
195{
196 kref_get(&xfer->refcnt);
197}
198
199static void wa_xfer_put(struct wa_xfer *xfer)
200{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100201 kref_put(&xfer->refcnt, wa_xfer_destroy);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100202}
203
204/*
205 * xfer is referenced
206 *
207 * xfer->lock has to be unlocked
208 *
209 * We take xfer->lock for setting the result; this is a barrier
210 * against drivers/usb/core/hcd.c:unlink1() being called after we call
211 * usb_hcd_giveback_urb() and wa_urb_dequeue() trying to get a
212 * reference to the transfer.
213 */
214static void wa_xfer_giveback(struct wa_xfer *xfer)
215{
216 unsigned long flags;
David Vrabelbce83692008-12-22 18:22:50 +0000217
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100218 spin_lock_irqsave(&xfer->wa->xfer_list_lock, flags);
219 list_del_init(&xfer->list_node);
220 spin_unlock_irqrestore(&xfer->wa->xfer_list_lock, flags);
221 /* FIXME: segmentation broken -- kills DWA */
222 wusbhc_giveback_urb(xfer->wa->wusb, xfer->urb, xfer->result);
223 wa_put(xfer->wa);
224 wa_xfer_put(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100225}
226
227/*
228 * xfer is referenced
229 *
230 * xfer->lock has to be unlocked
231 */
232static void wa_xfer_completion(struct wa_xfer *xfer)
233{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100234 if (xfer->wusb_dev)
235 wusb_dev_put(xfer->wusb_dev);
236 rpipe_put(xfer->ep->hcpriv);
237 wa_xfer_giveback(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100238}
239
240/*
241 * If transfer is done, wrap it up and return true
242 *
243 * xfer->lock has to be locked
244 */
245static unsigned __wa_xfer_is_done(struct wa_xfer *xfer)
246{
David Vrabelbce83692008-12-22 18:22:50 +0000247 struct device *dev = &xfer->wa->usb_iface->dev;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100248 unsigned result, cnt;
249 struct wa_seg *seg;
250 struct urb *urb = xfer->urb;
251 unsigned found_short = 0;
252
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100253 result = xfer->segs_done == xfer->segs_submitted;
254 if (result == 0)
255 goto out;
256 urb->actual_length = 0;
257 for (cnt = 0; cnt < xfer->segs; cnt++) {
258 seg = xfer->seg[cnt];
259 switch (seg->status) {
260 case WA_SEG_DONE:
261 if (found_short && seg->result > 0) {
David Vrabelbce83692008-12-22 18:22:50 +0000262 dev_dbg(dev, "xfer %p#%u: bad short segments (%zu)\n",
263 xfer, cnt, seg->result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100264 urb->status = -EINVAL;
265 goto out;
266 }
267 urb->actual_length += seg->result;
268 if (seg->result < xfer->seg_size
269 && cnt != xfer->segs-1)
270 found_short = 1;
David Vrabelbce83692008-12-22 18:22:50 +0000271 dev_dbg(dev, "xfer %p#%u: DONE short %d "
272 "result %zu urb->actual_length %d\n",
273 xfer, seg->index, found_short, seg->result,
274 urb->actual_length);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100275 break;
276 case WA_SEG_ERROR:
277 xfer->result = seg->result;
David Vrabelbce83692008-12-22 18:22:50 +0000278 dev_dbg(dev, "xfer %p#%u: ERROR result %zu\n",
279 xfer, seg->index, seg->result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100280 goto out;
281 case WA_SEG_ABORTED:
David Vrabelbce83692008-12-22 18:22:50 +0000282 dev_dbg(dev, "xfer %p#%u ABORTED: result %d\n",
283 xfer, seg->index, urb->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100284 xfer->result = urb->status;
285 goto out;
286 default:
David Vrabelbce83692008-12-22 18:22:50 +0000287 dev_warn(dev, "xfer %p#%u: is_done bad state %d\n",
288 xfer, cnt, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100289 xfer->result = -EINVAL;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100290 goto out;
291 }
292 }
293 xfer->result = 0;
294out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100295 return result;
296}
297
298/*
299 * Initialize a transfer's ID
300 *
301 * We need to use a sequential number; if we use the pointer or the
302 * hash of the pointer, it can repeat over sequential transfers and
303 * then it will confuse the HWA....wonder why in hell they put a 32
304 * bit handle in there then.
305 */
306static void wa_xfer_id_init(struct wa_xfer *xfer)
307{
308 xfer->id = atomic_add_return(1, &xfer->wa->xfer_id_count);
309}
310
311/*
312 * Return the xfer's ID associated with xfer
313 *
314 * Need to generate a
315 */
316static u32 wa_xfer_id(struct wa_xfer *xfer)
317{
318 return xfer->id;
319}
320
321/*
322 * Search for a transfer list ID on the HCD's URB list
323 *
324 * For 32 bit architectures, we use the pointer itself; for 64 bits, a
325 * 32-bit hash of the pointer.
326 *
327 * @returns NULL if not found.
328 */
329static struct wa_xfer *wa_xfer_get_by_id(struct wahc *wa, u32 id)
330{
331 unsigned long flags;
332 struct wa_xfer *xfer_itr;
333 spin_lock_irqsave(&wa->xfer_list_lock, flags);
334 list_for_each_entry(xfer_itr, &wa->xfer_list, list_node) {
335 if (id == xfer_itr->id) {
336 wa_xfer_get(xfer_itr);
337 goto out;
338 }
339 }
340 xfer_itr = NULL;
341out:
342 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
343 return xfer_itr;
344}
345
346struct wa_xfer_abort_buffer {
347 struct urb urb;
348 struct wa_xfer_abort cmd;
349};
350
351static void __wa_xfer_abort_cb(struct urb *urb)
352{
353 struct wa_xfer_abort_buffer *b = urb->context;
354 usb_put_urb(&b->urb);
355}
356
357/*
358 * Aborts an ongoing transaction
359 *
360 * Assumes the transfer is referenced and locked and in a submitted
361 * state (mainly that there is an endpoint/rpipe assigned).
362 *
363 * The callback (see above) does nothing but freeing up the data by
364 * putting the URB. Because the URB is allocated at the head of the
365 * struct, the whole space we allocated is kfreed.
366 *
367 * We'll get an 'aborted transaction' xfer result on DTI, that'll
368 * politely ignore because at this point the transaction has been
369 * marked as aborted already.
370 */
371static void __wa_xfer_abort(struct wa_xfer *xfer)
372{
373 int result;
374 struct device *dev = &xfer->wa->usb_iface->dev;
375 struct wa_xfer_abort_buffer *b;
376 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
377
378 b = kmalloc(sizeof(*b), GFP_ATOMIC);
379 if (b == NULL)
380 goto error_kmalloc;
381 b->cmd.bLength = sizeof(b->cmd);
382 b->cmd.bRequestType = WA_XFER_ABORT;
383 b->cmd.wRPipe = rpipe->descr.wRPipeIndex;
384 b->cmd.dwTransferID = wa_xfer_id(xfer);
385
386 usb_init_urb(&b->urb);
387 usb_fill_bulk_urb(&b->urb, xfer->wa->usb_dev,
388 usb_sndbulkpipe(xfer->wa->usb_dev,
389 xfer->wa->dto_epd->bEndpointAddress),
390 &b->cmd, sizeof(b->cmd), __wa_xfer_abort_cb, b);
391 result = usb_submit_urb(&b->urb, GFP_ATOMIC);
392 if (result < 0)
393 goto error_submit;
394 return; /* callback frees! */
395
396
397error_submit:
398 if (printk_ratelimit())
399 dev_err(dev, "xfer %p: Can't submit abort request: %d\n",
400 xfer, result);
401 kfree(b);
402error_kmalloc:
403 return;
404
405}
406
407/*
408 *
409 * @returns < 0 on error, transfer segment request size if ok
410 */
411static ssize_t __wa_xfer_setup_sizes(struct wa_xfer *xfer,
412 enum wa_xfer_type *pxfer_type)
413{
414 ssize_t result;
415 struct device *dev = &xfer->wa->usb_iface->dev;
416 size_t maxpktsize;
417 struct urb *urb = xfer->urb;
418 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
419
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100420 switch (rpipe->descr.bmAttribute & 0x3) {
421 case USB_ENDPOINT_XFER_CONTROL:
422 *pxfer_type = WA_XFER_TYPE_CTL;
423 result = sizeof(struct wa_xfer_ctl);
424 break;
425 case USB_ENDPOINT_XFER_INT:
426 case USB_ENDPOINT_XFER_BULK:
427 *pxfer_type = WA_XFER_TYPE_BI;
428 result = sizeof(struct wa_xfer_bi);
429 break;
430 case USB_ENDPOINT_XFER_ISOC:
431 dev_err(dev, "FIXME: ISOC not implemented\n");
432 result = -ENOSYS;
433 goto error;
434 default:
435 /* never happens */
436 BUG();
437 result = -EINVAL; /* shut gcc up */
438 };
439 xfer->is_inbound = urb->pipe & USB_DIR_IN ? 1 : 0;
440 xfer->is_dma = urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? 1 : 0;
441 xfer->seg_size = le16_to_cpu(rpipe->descr.wBlocks)
442 * 1 << (xfer->wa->wa_descr->bRPipeBlockSize - 1);
443 /* Compute the segment size and make sure it is a multiple of
444 * the maxpktsize (WUSB1.0[8.3.3.1])...not really too much of
445 * a check (FIXME) */
446 maxpktsize = le16_to_cpu(rpipe->descr.wMaxPacketSize);
447 if (xfer->seg_size < maxpktsize) {
448 dev_err(dev, "HW BUG? seg_size %zu smaller than maxpktsize "
449 "%zu\n", xfer->seg_size, maxpktsize);
450 result = -EINVAL;
451 goto error;
452 }
453 xfer->seg_size = (xfer->seg_size / maxpktsize) * maxpktsize;
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500454 xfer->segs = DIV_ROUND_UP(urb->transfer_buffer_length, xfer->seg_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100455 if (xfer->segs >= WA_SEGS_MAX) {
456 dev_err(dev, "BUG? ops, number of segments %d bigger than %d\n",
457 (int)(urb->transfer_buffer_length / xfer->seg_size),
458 WA_SEGS_MAX);
459 result = -EINVAL;
460 goto error;
461 }
462 if (xfer->segs == 0 && *pxfer_type == WA_XFER_TYPE_CTL)
463 xfer->segs = 1;
464error:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100465 return result;
466}
467
David Vrabelbce83692008-12-22 18:22:50 +0000468/* Fill in the common request header and xfer-type specific data. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100469static void __wa_xfer_setup_hdr0(struct wa_xfer *xfer,
470 struct wa_xfer_hdr *xfer_hdr0,
471 enum wa_xfer_type xfer_type,
472 size_t xfer_hdr_size)
473{
474 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
475
476 xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
477 xfer_hdr0->bLength = xfer_hdr_size;
478 xfer_hdr0->bRequestType = xfer_type;
479 xfer_hdr0->wRPipe = rpipe->descr.wRPipeIndex;
480 xfer_hdr0->dwTransferID = wa_xfer_id(xfer);
481 xfer_hdr0->bTransferSegment = 0;
482 switch (xfer_type) {
483 case WA_XFER_TYPE_CTL: {
484 struct wa_xfer_ctl *xfer_ctl =
485 container_of(xfer_hdr0, struct wa_xfer_ctl, hdr);
486 xfer_ctl->bmAttribute = xfer->is_inbound ? 1 : 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100487 memcpy(&xfer_ctl->baSetupData, xfer->urb->setup_packet,
488 sizeof(xfer_ctl->baSetupData));
489 break;
490 }
491 case WA_XFER_TYPE_BI:
492 break;
493 case WA_XFER_TYPE_ISO:
494 printk(KERN_ERR "FIXME: ISOC not implemented\n");
495 default:
496 BUG();
497 };
498}
499
500/*
501 * Callback for the OUT data phase of the segment request
502 *
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500503 * Check wa_seg_tr_cb(); most comments also apply here because this
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100504 * function does almost the same thing and they work closely
505 * together.
506 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300507 * If the seg request has failed but this DTO phase has succeeded,
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500508 * wa_seg_tr_cb() has already failed the segment and moved the
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100509 * status to WA_SEG_ERROR, so this will go through 'case 0' and
510 * effectively do nothing.
511 */
512static void wa_seg_dto_cb(struct urb *urb)
513{
514 struct wa_seg *seg = urb->context;
515 struct wa_xfer *xfer = seg->xfer;
516 struct wahc *wa;
517 struct device *dev;
518 struct wa_rpipe *rpipe;
519 unsigned long flags;
520 unsigned rpipe_ready = 0;
521 u8 done = 0;
522
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100523 switch (urb->status) {
524 case 0:
525 spin_lock_irqsave(&xfer->lock, flags);
526 wa = xfer->wa;
527 dev = &wa->usb_iface->dev;
David Vrabelbce83692008-12-22 18:22:50 +0000528 dev_dbg(dev, "xfer %p#%u: data out done (%d bytes)\n",
529 xfer, seg->index, urb->actual_length);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100530 if (seg->status < WA_SEG_PENDING)
531 seg->status = WA_SEG_PENDING;
532 seg->result = urb->actual_length;
533 spin_unlock_irqrestore(&xfer->lock, flags);
534 break;
535 case -ECONNRESET: /* URB unlinked; no need to do anything */
536 case -ENOENT: /* as it was done by the who unlinked us */
537 break;
538 default: /* Other errors ... */
539 spin_lock_irqsave(&xfer->lock, flags);
540 wa = xfer->wa;
541 dev = &wa->usb_iface->dev;
542 rpipe = xfer->ep->hcpriv;
David Vrabelbce83692008-12-22 18:22:50 +0000543 dev_dbg(dev, "xfer %p#%u: data out error %d\n",
544 xfer, seg->index, urb->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100545 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
546 EDC_ERROR_TIMEFRAME)){
547 dev_err(dev, "DTO: URB max acceptable errors "
548 "exceeded, resetting device\n");
549 wa_reset_all(wa);
550 }
551 if (seg->status != WA_SEG_ERROR) {
552 seg->status = WA_SEG_ERROR;
553 seg->result = urb->status;
554 xfer->segs_done++;
555 __wa_xfer_abort(xfer);
556 rpipe_ready = rpipe_avail_inc(rpipe);
557 done = __wa_xfer_is_done(xfer);
558 }
559 spin_unlock_irqrestore(&xfer->lock, flags);
560 if (done)
561 wa_xfer_completion(xfer);
562 if (rpipe_ready)
563 wa_xfer_delayed_run(rpipe);
564 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100565}
566
567/*
568 * Callback for the segment request
569 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200570 * If successful transition state (unless already transitioned or
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100571 * outbound transfer); otherwise, take a note of the error, mark this
572 * segment done and try completion.
573 *
574 * Note we don't access until we are sure that the transfer hasn't
575 * been cancelled (ECONNRESET, ENOENT), which could mean that
576 * seg->xfer could be already gone.
577 *
578 * We have to check before setting the status to WA_SEG_PENDING
579 * because sometimes the xfer result callback arrives before this
580 * callback (geeeeeeze), so it might happen that we are already in
581 * another state. As well, we don't set it if the transfer is inbound,
582 * as in that case, wa_seg_dto_cb will do it when the OUT data phase
583 * finishes.
584 */
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500585static void wa_seg_tr_cb(struct urb *urb)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100586{
587 struct wa_seg *seg = urb->context;
588 struct wa_xfer *xfer = seg->xfer;
589 struct wahc *wa;
590 struct device *dev;
591 struct wa_rpipe *rpipe;
592 unsigned long flags;
593 unsigned rpipe_ready;
594 u8 done = 0;
595
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100596 switch (urb->status) {
597 case 0:
598 spin_lock_irqsave(&xfer->lock, flags);
599 wa = xfer->wa;
600 dev = &wa->usb_iface->dev;
David Vrabelbce83692008-12-22 18:22:50 +0000601 dev_dbg(dev, "xfer %p#%u: request done\n", xfer, seg->index);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100602 if (xfer->is_inbound && seg->status < WA_SEG_PENDING)
603 seg->status = WA_SEG_PENDING;
604 spin_unlock_irqrestore(&xfer->lock, flags);
605 break;
606 case -ECONNRESET: /* URB unlinked; no need to do anything */
607 case -ENOENT: /* as it was done by the who unlinked us */
608 break;
609 default: /* Other errors ... */
610 spin_lock_irqsave(&xfer->lock, flags);
611 wa = xfer->wa;
612 dev = &wa->usb_iface->dev;
613 rpipe = xfer->ep->hcpriv;
614 if (printk_ratelimit())
615 dev_err(dev, "xfer %p#%u: request error %d\n",
616 xfer, seg->index, urb->status);
617 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
618 EDC_ERROR_TIMEFRAME)){
619 dev_err(dev, "DTO: URB max acceptable errors "
620 "exceeded, resetting device\n");
621 wa_reset_all(wa);
622 }
623 usb_unlink_urb(seg->dto_urb);
624 seg->status = WA_SEG_ERROR;
625 seg->result = urb->status;
626 xfer->segs_done++;
627 __wa_xfer_abort(xfer);
628 rpipe_ready = rpipe_avail_inc(rpipe);
629 done = __wa_xfer_is_done(xfer);
630 spin_unlock_irqrestore(&xfer->lock, flags);
631 if (done)
632 wa_xfer_completion(xfer);
633 if (rpipe_ready)
634 wa_xfer_delayed_run(rpipe);
635 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100636}
637
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500638/*
639 * Allocate an SG list to store bytes_to_transfer bytes and copy the
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500640 * subset of the in_sg that matches the buffer subset
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500641 * we are about to transfer.
642 */
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500643static struct scatterlist *wa_xfer_create_subset_sg(struct scatterlist *in_sg,
644 const unsigned int bytes_transferred,
645 const unsigned int bytes_to_transfer, unsigned int *out_num_sgs)
646{
647 struct scatterlist *out_sg;
648 unsigned int bytes_processed = 0, offset_into_current_page_data = 0,
649 nents;
650 struct scatterlist *current_xfer_sg = in_sg;
651 struct scatterlist *current_seg_sg, *last_seg_sg;
652
653 /* skip previously transferred pages. */
654 while ((current_xfer_sg) &&
655 (bytes_processed < bytes_transferred)) {
656 bytes_processed += current_xfer_sg->length;
657
658 /* advance the sg if current segment starts on or past the
659 next page. */
660 if (bytes_processed <= bytes_transferred)
661 current_xfer_sg = sg_next(current_xfer_sg);
662 }
663
664 /* the data for the current segment starts in current_xfer_sg.
665 calculate the offset. */
666 if (bytes_processed > bytes_transferred) {
667 offset_into_current_page_data = current_xfer_sg->length -
668 (bytes_processed - bytes_transferred);
669 }
670
671 /* calculate the number of pages needed by this segment. */
672 nents = DIV_ROUND_UP((bytes_to_transfer +
673 offset_into_current_page_data +
674 current_xfer_sg->offset),
675 PAGE_SIZE);
676
677 out_sg = kmalloc((sizeof(struct scatterlist) * nents), GFP_ATOMIC);
678 if (out_sg) {
679 sg_init_table(out_sg, nents);
680
681 /* copy the portion of the incoming SG that correlates to the
682 * data to be transferred by this segment to the segment SG. */
683 last_seg_sg = current_seg_sg = out_sg;
684 bytes_processed = 0;
685
686 /* reset nents and calculate the actual number of sg entries
687 needed. */
688 nents = 0;
689 while ((bytes_processed < bytes_to_transfer) &&
690 current_seg_sg && current_xfer_sg) {
691 unsigned int page_len = min((current_xfer_sg->length -
692 offset_into_current_page_data),
693 (bytes_to_transfer - bytes_processed));
694
695 sg_set_page(current_seg_sg, sg_page(current_xfer_sg),
696 page_len,
697 current_xfer_sg->offset +
698 offset_into_current_page_data);
699
700 bytes_processed += page_len;
701
702 last_seg_sg = current_seg_sg;
703 current_seg_sg = sg_next(current_seg_sg);
704 current_xfer_sg = sg_next(current_xfer_sg);
705
706 /* only the first page may require additional offset. */
707 offset_into_current_page_data = 0;
708 nents++;
709 }
710
711 /* update num_sgs and terminate the list since we may have
712 * concatenated pages. */
713 sg_mark_end(last_seg_sg);
714 *out_num_sgs = nents;
715 }
716
717 return out_sg;
718}
719
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100720/*
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500721 * Populate buffer ptr and size, DMA buffer or SG list for the dto urb.
722 */
723static int __wa_populate_dto_urb(struct wa_xfer *xfer,
724 struct wa_seg *seg, size_t buf_itr_offset, size_t buf_itr_size)
725{
726 int result = 0;
727
728 if (xfer->is_dma) {
729 seg->dto_urb->transfer_dma =
730 xfer->urb->transfer_dma + buf_itr_offset;
731 seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
732 seg->dto_urb->sg = NULL;
733 seg->dto_urb->num_sgs = 0;
734 } else {
735 /* do buffer or SG processing. */
736 seg->dto_urb->transfer_flags &=
737 ~URB_NO_TRANSFER_DMA_MAP;
738 /* this should always be 0 before a resubmit. */
739 seg->dto_urb->num_mapped_sgs = 0;
740
741 if (xfer->urb->transfer_buffer) {
742 seg->dto_urb->transfer_buffer =
743 xfer->urb->transfer_buffer +
744 buf_itr_offset;
745 seg->dto_urb->sg = NULL;
746 seg->dto_urb->num_sgs = 0;
747 } else {
748 seg->dto_urb->transfer_buffer = NULL;
749
750 /*
751 * allocate an SG list to store seg_size bytes
752 * and copy the subset of the xfer->urb->sg that
753 * matches the buffer subset we are about to
754 * read.
755 */
756 seg->dto_urb->sg = wa_xfer_create_subset_sg(
757 xfer->urb->sg,
758 buf_itr_offset, buf_itr_size,
759 &(seg->dto_urb->num_sgs));
760 if (!(seg->dto_urb->sg))
761 result = -ENOMEM;
762 }
763 }
764 seg->dto_urb->transfer_buffer_length = buf_itr_size;
765
766 return result;
767}
768
769/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100770 * Allocate the segs array and initialize each of them
771 *
772 * The segments are freed by wa_xfer_destroy() when the xfer use count
773 * drops to zero; however, because each segment is given the same life
774 * cycle as the USB URB it contains, it is actually freed by
775 * usb_put_urb() on the contained USB URB (twisted, eh?).
776 */
777static int __wa_xfer_setup_segs(struct wa_xfer *xfer, size_t xfer_hdr_size)
778{
779 int result, cnt;
780 size_t alloc_size = sizeof(*xfer->seg[0])
781 - sizeof(xfer->seg[0]->xfer_hdr) + xfer_hdr_size;
782 struct usb_device *usb_dev = xfer->wa->usb_dev;
783 const struct usb_endpoint_descriptor *dto_epd = xfer->wa->dto_epd;
784 struct wa_seg *seg;
785 size_t buf_itr, buf_size, buf_itr_size;
786
787 result = -ENOMEM;
David Vrabel92c4d9b2008-10-15 14:50:10 +0100788 xfer->seg = kcalloc(xfer->segs, sizeof(xfer->seg[0]), GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100789 if (xfer->seg == NULL)
790 goto error_segs_kzalloc;
791 buf_itr = 0;
792 buf_size = xfer->urb->transfer_buffer_length;
793 for (cnt = 0; cnt < xfer->segs; cnt++) {
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500794 seg = xfer->seg[cnt] = kmalloc(alloc_size, GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100795 if (seg == NULL)
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500796 goto error_seg_kmalloc;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100797 wa_seg_init(seg);
798 seg->xfer = xfer;
799 seg->index = cnt;
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500800 usb_fill_bulk_urb(&seg->tr_urb, usb_dev,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100801 usb_sndbulkpipe(usb_dev,
802 dto_epd->bEndpointAddress),
803 &seg->xfer_hdr, xfer_hdr_size,
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500804 wa_seg_tr_cb, seg);
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500805 buf_itr_size = min(buf_size, xfer->seg_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100806 if (xfer->is_inbound == 0 && buf_size > 0) {
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500807 /* outbound data. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100808 seg->dto_urb = usb_alloc_urb(0, GFP_ATOMIC);
809 if (seg->dto_urb == NULL)
810 goto error_dto_alloc;
811 usb_fill_bulk_urb(
812 seg->dto_urb, usb_dev,
813 usb_sndbulkpipe(usb_dev,
814 dto_epd->bEndpointAddress),
815 NULL, 0, wa_seg_dto_cb, seg);
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500816
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500817 /* fill in the xfer buffer information. */
818 result = __wa_populate_dto_urb(xfer, seg,
819 buf_itr, buf_itr_size);
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500820
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500821 if (result < 0)
822 goto error_seg_outbound_populate;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100823 }
824 seg->status = WA_SEG_READY;
825 buf_itr += buf_itr_size;
826 buf_size -= buf_itr_size;
827 }
828 return 0;
829
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500830 /*
831 * Free the memory for the current segment which failed to init.
832 * Use the fact that cnt is left at were it failed. The remaining
833 * segments will be cleaned up by wa_xfer_destroy.
834 */
835error_seg_outbound_populate:
Thomas Pugliese11b1bf82013-08-15 14:37:41 -0500836 usb_free_urb(xfer->seg[cnt]->dto_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100837error_dto_alloc:
838 kfree(xfer->seg[cnt]);
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500839 xfer->seg[cnt] = NULL;
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500840error_seg_kmalloc:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100841error_segs_kzalloc:
842 return result;
843}
844
845/*
846 * Allocates all the stuff needed to submit a transfer
847 *
848 * Breaks the whole data buffer in a list of segments, each one has a
849 * structure allocated to it and linked in xfer->seg[index]
850 *
851 * FIXME: merge setup_segs() and the last part of this function, no
852 * need to do two for loops when we could run everything in a
853 * single one
854 */
855static int __wa_xfer_setup(struct wa_xfer *xfer, struct urb *urb)
856{
857 int result;
858 struct device *dev = &xfer->wa->usb_iface->dev;
859 enum wa_xfer_type xfer_type = 0; /* shut up GCC */
860 size_t xfer_hdr_size, cnt, transfer_size;
861 struct wa_xfer_hdr *xfer_hdr0, *xfer_hdr;
862
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100863 result = __wa_xfer_setup_sizes(xfer, &xfer_type);
864 if (result < 0)
865 goto error_setup_sizes;
866 xfer_hdr_size = result;
867 result = __wa_xfer_setup_segs(xfer, xfer_hdr_size);
868 if (result < 0) {
869 dev_err(dev, "xfer %p: Failed to allocate %d segments: %d\n",
870 xfer, xfer->segs, result);
871 goto error_setup_segs;
872 }
873 /* Fill the first header */
874 xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
875 wa_xfer_id_init(xfer);
876 __wa_xfer_setup_hdr0(xfer, xfer_hdr0, xfer_type, xfer_hdr_size);
877
878 /* Fill remainig headers */
879 xfer_hdr = xfer_hdr0;
880 transfer_size = urb->transfer_buffer_length;
881 xfer_hdr0->dwTransferLength = transfer_size > xfer->seg_size ?
882 xfer->seg_size : transfer_size;
883 transfer_size -= xfer->seg_size;
884 for (cnt = 1; cnt < xfer->segs; cnt++) {
885 xfer_hdr = &xfer->seg[cnt]->xfer_hdr;
886 memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
887 xfer_hdr->bTransferSegment = cnt;
888 xfer_hdr->dwTransferLength = transfer_size > xfer->seg_size ?
889 cpu_to_le32(xfer->seg_size)
890 : cpu_to_le32(transfer_size);
891 xfer->seg[cnt]->status = WA_SEG_READY;
892 transfer_size -= xfer->seg_size;
893 }
894 xfer_hdr->bTransferSegment |= 0x80; /* this is the last segment */
895 result = 0;
896error_setup_segs:
897error_setup_sizes:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100898 return result;
899}
900
901/*
902 *
903 *
904 * rpipe->seg_lock is held!
905 */
906static int __wa_seg_submit(struct wa_rpipe *rpipe, struct wa_xfer *xfer,
907 struct wa_seg *seg)
908{
909 int result;
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500910 /* submit the transfer request. */
911 result = usb_submit_urb(&seg->tr_urb, GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100912 if (result < 0) {
913 printk(KERN_ERR "xfer %p#%u: REQ submit failed: %d\n",
914 xfer, seg->index, result);
915 goto error_seg_submit;
916 }
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500917 /* submit the out data if this is an out request. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100918 if (seg->dto_urb) {
919 result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
920 if (result < 0) {
921 printk(KERN_ERR "xfer %p#%u: DTO submit failed: %d\n",
922 xfer, seg->index, result);
923 goto error_dto_submit;
924 }
925 }
926 seg->status = WA_SEG_SUBMITTED;
927 rpipe_avail_dec(rpipe);
928 return 0;
929
930error_dto_submit:
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500931 usb_unlink_urb(&seg->tr_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100932error_seg_submit:
933 seg->status = WA_SEG_ERROR;
934 seg->result = result;
935 return result;
936}
937
938/*
939 * Execute more queued request segments until the maximum concurrent allowed
940 *
941 * The ugly unlock/lock sequence on the error path is needed as the
942 * xfer->lock normally nests the seg_lock and not viceversa.
943 *
944 */
945static void wa_xfer_delayed_run(struct wa_rpipe *rpipe)
946{
947 int result;
948 struct device *dev = &rpipe->wa->usb_iface->dev;
949 struct wa_seg *seg;
950 struct wa_xfer *xfer;
951 unsigned long flags;
952
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100953 spin_lock_irqsave(&rpipe->seg_lock, flags);
954 while (atomic_read(&rpipe->segs_available) > 0
955 && !list_empty(&rpipe->seg_list)) {
Thomas Pugliesee9a088f2013-08-12 10:10:53 -0500956 seg = list_first_entry(&(rpipe->seg_list), struct wa_seg,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100957 list_node);
958 list_del(&seg->list_node);
959 xfer = seg->xfer;
960 result = __wa_seg_submit(rpipe, xfer, seg);
David Vrabelbce83692008-12-22 18:22:50 +0000961 dev_dbg(dev, "xfer %p#%u submitted from delayed [%d segments available] %d\n",
962 xfer, seg->index, atomic_read(&rpipe->segs_available), result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100963 if (unlikely(result < 0)) {
964 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
965 spin_lock_irqsave(&xfer->lock, flags);
966 __wa_xfer_abort(xfer);
967 xfer->segs_done++;
968 spin_unlock_irqrestore(&xfer->lock, flags);
969 spin_lock_irqsave(&rpipe->seg_lock, flags);
970 }
971 }
972 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100973}
974
975/*
976 *
977 * xfer->lock is taken
978 *
979 * On failure submitting we just stop submitting and return error;
980 * wa_urb_enqueue_b() will execute the completion path
981 */
982static int __wa_xfer_submit(struct wa_xfer *xfer)
983{
984 int result;
985 struct wahc *wa = xfer->wa;
986 struct device *dev = &wa->usb_iface->dev;
987 unsigned cnt;
988 struct wa_seg *seg;
989 unsigned long flags;
990 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
991 size_t maxrequests = le16_to_cpu(rpipe->descr.wRequests);
992 u8 available;
993 u8 empty;
994
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100995 spin_lock_irqsave(&wa->xfer_list_lock, flags);
996 list_add_tail(&xfer->list_node, &wa->xfer_list);
997 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
998
999 BUG_ON(atomic_read(&rpipe->segs_available) > maxrequests);
1000 result = 0;
1001 spin_lock_irqsave(&rpipe->seg_lock, flags);
1002 for (cnt = 0; cnt < xfer->segs; cnt++) {
1003 available = atomic_read(&rpipe->segs_available);
1004 empty = list_empty(&rpipe->seg_list);
1005 seg = xfer->seg[cnt];
David Vrabelbce83692008-12-22 18:22:50 +00001006 dev_dbg(dev, "xfer %p#%u: available %u empty %u (%s)\n",
1007 xfer, cnt, available, empty,
1008 available == 0 || !empty ? "delayed" : "submitted");
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001009 if (available == 0 || !empty) {
David Vrabelbce83692008-12-22 18:22:50 +00001010 dev_dbg(dev, "xfer %p#%u: delayed\n", xfer, cnt);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001011 seg->status = WA_SEG_DELAYED;
1012 list_add_tail(&seg->list_node, &rpipe->seg_list);
1013 } else {
1014 result = __wa_seg_submit(rpipe, xfer, seg);
David Vrabelbce83692008-12-22 18:22:50 +00001015 if (result < 0) {
1016 __wa_xfer_abort(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001017 goto error_seg_submit;
David Vrabelbce83692008-12-22 18:22:50 +00001018 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001019 }
1020 xfer->segs_submitted++;
1021 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001022error_seg_submit:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001023 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001024 return result;
1025}
1026
1027/*
1028 * Second part of a URB/transfer enqueuement
1029 *
1030 * Assumes this comes from wa_urb_enqueue() [maybe through
1031 * wa_urb_enqueue_run()]. At this point:
1032 *
1033 * xfer->wa filled and refcounted
1034 * xfer->ep filled with rpipe refcounted if
1035 * delayed == 0
1036 * xfer->urb filled and refcounted (this is the case when called
1037 * from wa_urb_enqueue() as we come from usb_submit_urb()
1038 * and when called by wa_urb_enqueue_run(), as we took an
1039 * extra ref dropped by _run() after we return).
1040 * xfer->gfp filled
1041 *
1042 * If we fail at __wa_xfer_submit(), then we just check if we are done
1043 * and if so, we run the completion procedure. However, if we are not
1044 * yet done, we do nothing and wait for the completion handlers from
1045 * the submitted URBs or from the xfer-result path to kick in. If xfer
1046 * result never kicks in, the xfer will timeout from the USB code and
1047 * dequeue() will be called.
1048 */
1049static void wa_urb_enqueue_b(struct wa_xfer *xfer)
1050{
1051 int result;
1052 unsigned long flags;
1053 struct urb *urb = xfer->urb;
1054 struct wahc *wa = xfer->wa;
1055 struct wusbhc *wusbhc = wa->wusb;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001056 struct wusb_dev *wusb_dev;
1057 unsigned done;
1058
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001059 result = rpipe_get_by_ep(wa, xfer->ep, urb, xfer->gfp);
1060 if (result < 0)
1061 goto error_rpipe_get;
1062 result = -ENODEV;
1063 /* FIXME: segmentation broken -- kills DWA */
1064 mutex_lock(&wusbhc->mutex); /* get a WUSB dev */
Jiri Slaby49fa0922009-03-11 21:47:40 +01001065 if (urb->dev == NULL) {
1066 mutex_unlock(&wusbhc->mutex);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001067 goto error_dev_gone;
Jiri Slaby49fa0922009-03-11 21:47:40 +01001068 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001069 wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev);
1070 if (wusb_dev == NULL) {
1071 mutex_unlock(&wusbhc->mutex);
1072 goto error_dev_gone;
1073 }
1074 mutex_unlock(&wusbhc->mutex);
1075
1076 spin_lock_irqsave(&xfer->lock, flags);
1077 xfer->wusb_dev = wusb_dev;
1078 result = urb->status;
1079 if (urb->status != -EINPROGRESS)
1080 goto error_dequeued;
1081
1082 result = __wa_xfer_setup(xfer, urb);
1083 if (result < 0)
1084 goto error_xfer_setup;
1085 result = __wa_xfer_submit(xfer);
1086 if (result < 0)
1087 goto error_xfer_submit;
1088 spin_unlock_irqrestore(&xfer->lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001089 return;
1090
1091 /* this is basically wa_xfer_completion() broken up wa_xfer_giveback()
1092 * does a wa_xfer_put() that will call wa_xfer_destroy() and clean
1093 * upundo setup().
1094 */
1095error_xfer_setup:
1096error_dequeued:
1097 spin_unlock_irqrestore(&xfer->lock, flags);
1098 /* FIXME: segmentation broken, kills DWA */
1099 if (wusb_dev)
1100 wusb_dev_put(wusb_dev);
1101error_dev_gone:
1102 rpipe_put(xfer->ep->hcpriv);
1103error_rpipe_get:
1104 xfer->result = result;
1105 wa_xfer_giveback(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001106 return;
1107
1108error_xfer_submit:
1109 done = __wa_xfer_is_done(xfer);
1110 xfer->result = result;
1111 spin_unlock_irqrestore(&xfer->lock, flags);
1112 if (done)
1113 wa_xfer_completion(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001114}
1115
1116/*
1117 * Execute the delayed transfers in the Wire Adapter @wa
1118 *
1119 * We need to be careful here, as dequeue() could be called in the
1120 * middle. That's why we do the whole thing under the
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001121 * wa->xfer_list_lock. If dequeue() jumps in, it first locks xfer->lock
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001122 * and then checks the list -- so as we would be acquiring in inverse
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001123 * order, we move the delayed list to a separate list while locked and then
1124 * submit them without the list lock held.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001125 */
1126void wa_urb_enqueue_run(struct work_struct *ws)
1127{
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001128 struct wahc *wa = container_of(ws, struct wahc, xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001129 struct wa_xfer *xfer, *next;
1130 struct urb *urb;
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001131 LIST_HEAD(tmp_list);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001132
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001133 /* Create a copy of the wa->xfer_delayed_list while holding the lock */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001134 spin_lock_irq(&wa->xfer_list_lock);
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001135 list_cut_position(&tmp_list, &wa->xfer_delayed_list,
1136 wa->xfer_delayed_list.prev);
1137 spin_unlock_irq(&wa->xfer_list_lock);
1138
1139 /*
1140 * enqueue from temp list without list lock held since wa_urb_enqueue_b
1141 * can take xfer->lock as well as lock mutexes.
1142 */
1143 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001144 list_del_init(&xfer->list_node);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001145
1146 urb = xfer->urb;
1147 wa_urb_enqueue_b(xfer);
1148 usb_put_urb(urb); /* taken when queuing */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001149 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001150}
1151EXPORT_SYMBOL_GPL(wa_urb_enqueue_run);
1152
1153/*
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001154 * Process the errored transfers on the Wire Adapter outside of interrupt.
1155 */
1156void wa_process_errored_transfers_run(struct work_struct *ws)
1157{
1158 struct wahc *wa = container_of(ws, struct wahc, xfer_error_work);
1159 struct wa_xfer *xfer, *next;
1160 LIST_HEAD(tmp_list);
1161
1162 pr_info("%s: Run delayed STALL processing.\n", __func__);
1163
1164 /* Create a copy of the wa->xfer_errored_list while holding the lock */
1165 spin_lock_irq(&wa->xfer_list_lock);
1166 list_cut_position(&tmp_list, &wa->xfer_errored_list,
1167 wa->xfer_errored_list.prev);
1168 spin_unlock_irq(&wa->xfer_list_lock);
1169
1170 /*
1171 * run rpipe_clear_feature_stalled from temp list without list lock
1172 * held.
1173 */
1174 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
1175 struct usb_host_endpoint *ep;
1176 unsigned long flags;
1177 struct wa_rpipe *rpipe;
1178
1179 spin_lock_irqsave(&xfer->lock, flags);
1180 ep = xfer->ep;
1181 rpipe = ep->hcpriv;
1182 spin_unlock_irqrestore(&xfer->lock, flags);
1183
1184 /* clear RPIPE feature stalled without holding a lock. */
1185 rpipe_clear_feature_stalled(wa, ep);
1186
1187 /* complete the xfer. This removes it from the tmp list. */
1188 wa_xfer_completion(xfer);
1189
1190 /* check for work. */
1191 wa_xfer_delayed_run(rpipe);
1192 }
1193}
1194EXPORT_SYMBOL_GPL(wa_process_errored_transfers_run);
1195
1196/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001197 * Submit a transfer to the Wire Adapter in a delayed way
1198 *
1199 * The process of enqueuing involves possible sleeps() [see
1200 * enqueue_b(), for the rpipe_get() and the mutex_lock()]. If we are
1201 * in an atomic section, we defer the enqueue_b() call--else we call direct.
1202 *
1203 * @urb: We own a reference to it done by the HCI Linux USB stack that
1204 * will be given up by calling usb_hcd_giveback_urb() or by
1205 * returning error from this function -> ergo we don't have to
1206 * refcount it.
1207 */
1208int wa_urb_enqueue(struct wahc *wa, struct usb_host_endpoint *ep,
1209 struct urb *urb, gfp_t gfp)
1210{
1211 int result;
1212 struct device *dev = &wa->usb_iface->dev;
1213 struct wa_xfer *xfer;
1214 unsigned long my_flags;
1215 unsigned cant_sleep = irqs_disabled() | in_atomic();
1216
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001217 if ((urb->transfer_buffer == NULL)
1218 && (urb->sg == NULL)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001219 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1220 && urb->transfer_buffer_length != 0) {
1221 dev_err(dev, "BUG? urb %p: NULL xfer buffer & NODMA\n", urb);
1222 dump_stack();
1223 }
1224
1225 result = -ENOMEM;
1226 xfer = kzalloc(sizeof(*xfer), gfp);
1227 if (xfer == NULL)
1228 goto error_kmalloc;
1229
1230 result = -ENOENT;
1231 if (urb->status != -EINPROGRESS) /* cancelled */
1232 goto error_dequeued; /* before starting? */
1233 wa_xfer_init(xfer);
1234 xfer->wa = wa_get(wa);
1235 xfer->urb = urb;
1236 xfer->gfp = gfp;
1237 xfer->ep = ep;
1238 urb->hcpriv = xfer;
David Vrabelbce83692008-12-22 18:22:50 +00001239
1240 dev_dbg(dev, "xfer %p urb %p pipe 0x%02x [%d bytes] %s %s %s\n",
1241 xfer, urb, urb->pipe, urb->transfer_buffer_length,
1242 urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? "dma" : "nodma",
1243 urb->pipe & USB_DIR_IN ? "inbound" : "outbound",
1244 cant_sleep ? "deferred" : "inline");
1245
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001246 if (cant_sleep) {
1247 usb_get_urb(urb);
1248 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1249 list_add_tail(&xfer->list_node, &wa->xfer_delayed_list);
1250 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001251 queue_work(wusbd, &wa->xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001252 } else {
1253 wa_urb_enqueue_b(xfer);
1254 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001255 return 0;
1256
1257error_dequeued:
1258 kfree(xfer);
1259error_kmalloc:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001260 return result;
1261}
1262EXPORT_SYMBOL_GPL(wa_urb_enqueue);
1263
1264/*
1265 * Dequeue a URB and make sure uwb_hcd_giveback_urb() [completion
1266 * handler] is called.
1267 *
1268 * Until a transfer goes successfully through wa_urb_enqueue() it
1269 * needs to be dequeued with completion calling; when stuck in delayed
1270 * or before wa_xfer_setup() is called, we need to do completion.
1271 *
1272 * not setup If there is no hcpriv yet, that means that that enqueue
1273 * still had no time to set the xfer up. Because
1274 * urb->status should be other than -EINPROGRESS,
1275 * enqueue() will catch that and bail out.
1276 *
1277 * If the transfer has gone through setup, we just need to clean it
1278 * up. If it has gone through submit(), we have to abort it [with an
1279 * asynch request] and then make sure we cancel each segment.
1280 *
1281 */
1282int wa_urb_dequeue(struct wahc *wa, struct urb *urb)
1283{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001284 unsigned long flags, flags2;
1285 struct wa_xfer *xfer;
1286 struct wa_seg *seg;
1287 struct wa_rpipe *rpipe;
1288 unsigned cnt;
1289 unsigned rpipe_ready = 0;
1290
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001291 xfer = urb->hcpriv;
1292 if (xfer == NULL) {
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001293 /*
1294 * Nothing setup yet enqueue will see urb->status !=
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001295 * -EINPROGRESS (by hcd layer) and bail out with
1296 * error, no need to do completion
1297 */
1298 BUG_ON(urb->status == -EINPROGRESS);
1299 goto out;
1300 }
1301 spin_lock_irqsave(&xfer->lock, flags);
1302 rpipe = xfer->ep->hcpriv;
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001303 if (rpipe == NULL) {
1304 pr_debug("%s: xfer id 0x%08X has no RPIPE. %s",
1305 __func__, wa_xfer_id(xfer),
1306 "Probably already aborted.\n" );
1307 goto out_unlock;
1308 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001309 /* Check the delayed list -> if there, release and complete */
1310 spin_lock_irqsave(&wa->xfer_list_lock, flags2);
1311 if (!list_empty(&xfer->list_node) && xfer->seg == NULL)
1312 goto dequeue_delayed;
1313 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1314 if (xfer->seg == NULL) /* still hasn't reached */
1315 goto out_unlock; /* setup(), enqueue_b() completes */
1316 /* Ok, the xfer is in flight already, it's been setup and submitted.*/
1317 __wa_xfer_abort(xfer);
1318 for (cnt = 0; cnt < xfer->segs; cnt++) {
1319 seg = xfer->seg[cnt];
1320 switch (seg->status) {
1321 case WA_SEG_NOTREADY:
1322 case WA_SEG_READY:
1323 printk(KERN_ERR "xfer %p#%u: dequeue bad state %u\n",
1324 xfer, cnt, seg->status);
1325 WARN_ON(1);
1326 break;
1327 case WA_SEG_DELAYED:
1328 seg->status = WA_SEG_ABORTED;
1329 spin_lock_irqsave(&rpipe->seg_lock, flags2);
1330 list_del(&seg->list_node);
1331 xfer->segs_done++;
1332 rpipe_ready = rpipe_avail_inc(rpipe);
1333 spin_unlock_irqrestore(&rpipe->seg_lock, flags2);
1334 break;
1335 case WA_SEG_SUBMITTED:
1336 seg->status = WA_SEG_ABORTED;
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001337 usb_unlink_urb(&seg->tr_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001338 if (xfer->is_inbound == 0)
1339 usb_unlink_urb(seg->dto_urb);
1340 xfer->segs_done++;
1341 rpipe_ready = rpipe_avail_inc(rpipe);
1342 break;
1343 case WA_SEG_PENDING:
1344 seg->status = WA_SEG_ABORTED;
1345 xfer->segs_done++;
1346 rpipe_ready = rpipe_avail_inc(rpipe);
1347 break;
1348 case WA_SEG_DTI_PENDING:
1349 usb_unlink_urb(wa->dti_urb);
1350 seg->status = WA_SEG_ABORTED;
1351 xfer->segs_done++;
1352 rpipe_ready = rpipe_avail_inc(rpipe);
1353 break;
1354 case WA_SEG_DONE:
1355 case WA_SEG_ERROR:
1356 case WA_SEG_ABORTED:
1357 break;
1358 }
1359 }
1360 xfer->result = urb->status; /* -ENOENT or -ECONNRESET */
1361 __wa_xfer_is_done(xfer);
1362 spin_unlock_irqrestore(&xfer->lock, flags);
1363 wa_xfer_completion(xfer);
1364 if (rpipe_ready)
1365 wa_xfer_delayed_run(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001366 return 0;
1367
1368out_unlock:
1369 spin_unlock_irqrestore(&xfer->lock, flags);
1370out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001371 return 0;
1372
1373dequeue_delayed:
1374 list_del_init(&xfer->list_node);
1375 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1376 xfer->result = urb->status;
1377 spin_unlock_irqrestore(&xfer->lock, flags);
1378 wa_xfer_giveback(xfer);
1379 usb_put_urb(urb); /* we got a ref in enqueue() */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001380 return 0;
1381}
1382EXPORT_SYMBOL_GPL(wa_urb_dequeue);
1383
1384/*
1385 * Translation from WA status codes (WUSB1.0 Table 8.15) to errno
1386 * codes
1387 *
1388 * Positive errno values are internal inconsistencies and should be
1389 * flagged louder. Negative are to be passed up to the user in the
1390 * normal way.
1391 *
1392 * @status: USB WA status code -- high two bits are stripped.
1393 */
1394static int wa_xfer_status_to_errno(u8 status)
1395{
1396 int errno;
1397 u8 real_status = status;
1398 static int xlat[] = {
1399 [WA_XFER_STATUS_SUCCESS] = 0,
1400 [WA_XFER_STATUS_HALTED] = -EPIPE,
1401 [WA_XFER_STATUS_DATA_BUFFER_ERROR] = -ENOBUFS,
1402 [WA_XFER_STATUS_BABBLE] = -EOVERFLOW,
1403 [WA_XFER_RESERVED] = EINVAL,
1404 [WA_XFER_STATUS_NOT_FOUND] = 0,
1405 [WA_XFER_STATUS_INSUFFICIENT_RESOURCE] = -ENOMEM,
1406 [WA_XFER_STATUS_TRANSACTION_ERROR] = -EILSEQ,
1407 [WA_XFER_STATUS_ABORTED] = -EINTR,
1408 [WA_XFER_STATUS_RPIPE_NOT_READY] = EINVAL,
1409 [WA_XFER_INVALID_FORMAT] = EINVAL,
1410 [WA_XFER_UNEXPECTED_SEGMENT_NUMBER] = EINVAL,
1411 [WA_XFER_STATUS_RPIPE_TYPE_MISMATCH] = EINVAL,
1412 };
1413 status &= 0x3f;
1414
1415 if (status == 0)
1416 return 0;
1417 if (status >= ARRAY_SIZE(xlat)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02001418 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001419 "Unknown WA transfer status 0x%02x\n",
1420 __func__, real_status);
1421 return -EINVAL;
1422 }
1423 errno = xlat[status];
1424 if (unlikely(errno > 0)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02001425 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001426 "Inconsistent WA status: 0x%02x\n",
1427 __func__, real_status);
1428 errno = -errno;
1429 }
1430 return errno;
1431}
1432
1433/*
1434 * Process a xfer result completion message
1435 *
1436 * inbound transfers: need to schedule a DTI read
1437 *
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001438 * FIXME: this function needs to be broken up in parts
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001439 */
Thomas Pugliese0367eef2013-09-26 10:49:41 -05001440static void wa_xfer_result_chew(struct wahc *wa, struct wa_xfer *xfer,
1441 struct wa_xfer_result *xfer_result)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001442{
1443 int result;
1444 struct device *dev = &wa->usb_iface->dev;
1445 unsigned long flags;
1446 u8 seg_idx;
1447 struct wa_seg *seg;
1448 struct wa_rpipe *rpipe;
Thomas Pugliese0367eef2013-09-26 10:49:41 -05001449 unsigned done = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001450 u8 usb_status;
1451 unsigned rpipe_ready = 0;
1452
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001453 spin_lock_irqsave(&xfer->lock, flags);
1454 seg_idx = xfer_result->bTransferSegment & 0x7f;
1455 if (unlikely(seg_idx >= xfer->segs))
1456 goto error_bad_seg;
1457 seg = xfer->seg[seg_idx];
1458 rpipe = xfer->ep->hcpriv;
1459 usb_status = xfer_result->bTransferStatus;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001460 dev_dbg(dev, "xfer %p#%u: bTransferStatus 0x%02x (seg status %u)\n",
David Vrabelbce83692008-12-22 18:22:50 +00001461 xfer, seg_idx, usb_status, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001462 if (seg->status == WA_SEG_ABORTED
1463 || seg->status == WA_SEG_ERROR) /* already handled */
1464 goto segment_aborted;
1465 if (seg->status == WA_SEG_SUBMITTED) /* ops, got here */
1466 seg->status = WA_SEG_PENDING; /* before wa_seg{_dto}_cb() */
1467 if (seg->status != WA_SEG_PENDING) {
1468 if (printk_ratelimit())
1469 dev_err(dev, "xfer %p#%u: Bad segment state %u\n",
1470 xfer, seg_idx, seg->status);
1471 seg->status = WA_SEG_PENDING; /* workaround/"fix" it */
1472 }
1473 if (usb_status & 0x80) {
1474 seg->result = wa_xfer_status_to_errno(usb_status);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001475 dev_err(dev, "DTI: xfer %p#:%08X:%u failed (0x%02x)\n",
1476 xfer, xfer->id, seg->index, usb_status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001477 goto error_complete;
1478 }
1479 /* FIXME: we ignore warnings, tally them for stats */
1480 if (usb_status & 0x40) /* Warning?... */
1481 usb_status = 0; /* ... pass */
1482 if (xfer->is_inbound) { /* IN data phase: read to buffer */
1483 seg->status = WA_SEG_DTI_PENDING;
1484 BUG_ON(wa->buf_in_urb->status == -EINPROGRESS);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001485 /* this should always be 0 before a resubmit. */
1486 wa->buf_in_urb->num_mapped_sgs = 0;
1487
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001488 if (xfer->is_dma) {
1489 wa->buf_in_urb->transfer_dma =
1490 xfer->urb->transfer_dma
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001491 + (seg_idx * xfer->seg_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001492 wa->buf_in_urb->transfer_flags
1493 |= URB_NO_TRANSFER_DMA_MAP;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001494 wa->buf_in_urb->transfer_buffer = NULL;
1495 wa->buf_in_urb->sg = NULL;
1496 wa->buf_in_urb->num_sgs = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001497 } else {
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001498 /* do buffer or SG processing. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001499 wa->buf_in_urb->transfer_flags
1500 &= ~URB_NO_TRANSFER_DMA_MAP;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001501
1502 if (xfer->urb->transfer_buffer) {
1503 wa->buf_in_urb->transfer_buffer =
1504 xfer->urb->transfer_buffer
1505 + (seg_idx * xfer->seg_size);
1506 wa->buf_in_urb->sg = NULL;
1507 wa->buf_in_urb->num_sgs = 0;
1508 } else {
1509 /* allocate an SG list to store seg_size bytes
1510 and copy the subset of the xfer->urb->sg
1511 that matches the buffer subset we are
1512 about to read. */
1513 wa->buf_in_urb->sg = wa_xfer_create_subset_sg(
1514 xfer->urb->sg,
1515 seg_idx * xfer->seg_size,
1516 le32_to_cpu(
1517 xfer_result->dwTransferLength),
1518 &(wa->buf_in_urb->num_sgs));
1519
1520 if (!(wa->buf_in_urb->sg)) {
1521 wa->buf_in_urb->num_sgs = 0;
1522 goto error_sg_alloc;
1523 }
1524 wa->buf_in_urb->transfer_buffer = NULL;
1525 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001526 }
1527 wa->buf_in_urb->transfer_buffer_length =
1528 le32_to_cpu(xfer_result->dwTransferLength);
1529 wa->buf_in_urb->context = seg;
1530 result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
1531 if (result < 0)
1532 goto error_submit_buf_in;
1533 } else {
1534 /* OUT data phase, complete it -- */
1535 seg->status = WA_SEG_DONE;
1536 seg->result = le32_to_cpu(xfer_result->dwTransferLength);
1537 xfer->segs_done++;
1538 rpipe_ready = rpipe_avail_inc(rpipe);
1539 done = __wa_xfer_is_done(xfer);
1540 }
1541 spin_unlock_irqrestore(&xfer->lock, flags);
1542 if (done)
1543 wa_xfer_completion(xfer);
1544 if (rpipe_ready)
1545 wa_xfer_delayed_run(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001546 return;
1547
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001548error_submit_buf_in:
1549 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
1550 dev_err(dev, "DTI: URB max acceptable errors "
1551 "exceeded, resetting device\n");
1552 wa_reset_all(wa);
1553 }
1554 if (printk_ratelimit())
1555 dev_err(dev, "xfer %p#%u: can't submit DTI data phase: %d\n",
1556 xfer, seg_idx, result);
1557 seg->result = result;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001558 kfree(wa->buf_in_urb->sg);
1559error_sg_alloc:
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001560 __wa_xfer_abort(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001561error_complete:
1562 seg->status = WA_SEG_ERROR;
1563 xfer->segs_done++;
1564 rpipe_ready = rpipe_avail_inc(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001565 done = __wa_xfer_is_done(xfer);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001566 /*
1567 * queue work item to clear STALL for control endpoints.
1568 * Otherwise, let endpoint_reset take care of it.
1569 */
1570 if (((usb_status & 0x3f) == WA_XFER_STATUS_HALTED) &&
1571 usb_endpoint_xfer_control(&xfer->ep->desc) &&
1572 done) {
1573
1574 dev_info(dev, "Control EP stall. Queue delayed work.\n");
1575 spin_lock_irq(&wa->xfer_list_lock);
Wei Yongjun8eb41292013-09-23 14:16:22 +08001576 /* move xfer from xfer_list to xfer_errored_list. */
1577 list_move_tail(&xfer->list_node, &wa->xfer_errored_list);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001578 spin_unlock_irq(&wa->xfer_list_lock);
1579 spin_unlock_irqrestore(&xfer->lock, flags);
1580 queue_work(wusbd, &wa->xfer_error_work);
1581 } else {
1582 spin_unlock_irqrestore(&xfer->lock, flags);
1583 if (done)
1584 wa_xfer_completion(xfer);
1585 if (rpipe_ready)
1586 wa_xfer_delayed_run(rpipe);
1587 }
1588
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001589 return;
1590
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001591error_bad_seg:
1592 spin_unlock_irqrestore(&xfer->lock, flags);
1593 wa_urb_dequeue(wa, xfer->urb);
1594 if (printk_ratelimit())
1595 dev_err(dev, "xfer %p#%u: bad segment\n", xfer, seg_idx);
1596 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
1597 dev_err(dev, "DTI: URB max acceptable errors "
1598 "exceeded, resetting device\n");
1599 wa_reset_all(wa);
1600 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001601 return;
1602
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001603segment_aborted:
1604 /* nothing to do, as the aborter did the completion */
1605 spin_unlock_irqrestore(&xfer->lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001606}
1607
1608/*
1609 * Callback for the IN data phase
1610 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001611 * If successful transition state; otherwise, take a note of the
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001612 * error, mark this segment done and try completion.
1613 *
1614 * Note we don't access until we are sure that the transfer hasn't
1615 * been cancelled (ECONNRESET, ENOENT), which could mean that
1616 * seg->xfer could be already gone.
1617 */
1618static void wa_buf_in_cb(struct urb *urb)
1619{
1620 struct wa_seg *seg = urb->context;
1621 struct wa_xfer *xfer = seg->xfer;
1622 struct wahc *wa;
1623 struct device *dev;
1624 struct wa_rpipe *rpipe;
1625 unsigned rpipe_ready;
1626 unsigned long flags;
1627 u8 done = 0;
1628
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001629 /* free the sg if it was used. */
1630 kfree(urb->sg);
1631 urb->sg = NULL;
1632
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001633 switch (urb->status) {
1634 case 0:
1635 spin_lock_irqsave(&xfer->lock, flags);
1636 wa = xfer->wa;
1637 dev = &wa->usb_iface->dev;
1638 rpipe = xfer->ep->hcpriv;
David Vrabelbce83692008-12-22 18:22:50 +00001639 dev_dbg(dev, "xfer %p#%u: data in done (%zu bytes)\n",
1640 xfer, seg->index, (size_t)urb->actual_length);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001641 seg->status = WA_SEG_DONE;
1642 seg->result = urb->actual_length;
1643 xfer->segs_done++;
1644 rpipe_ready = rpipe_avail_inc(rpipe);
1645 done = __wa_xfer_is_done(xfer);
1646 spin_unlock_irqrestore(&xfer->lock, flags);
1647 if (done)
1648 wa_xfer_completion(xfer);
1649 if (rpipe_ready)
1650 wa_xfer_delayed_run(rpipe);
1651 break;
1652 case -ECONNRESET: /* URB unlinked; no need to do anything */
1653 case -ENOENT: /* as it was done by the who unlinked us */
1654 break;
1655 default: /* Other errors ... */
1656 spin_lock_irqsave(&xfer->lock, flags);
1657 wa = xfer->wa;
1658 dev = &wa->usb_iface->dev;
1659 rpipe = xfer->ep->hcpriv;
1660 if (printk_ratelimit())
1661 dev_err(dev, "xfer %p#%u: data in error %d\n",
1662 xfer, seg->index, urb->status);
1663 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
1664 EDC_ERROR_TIMEFRAME)){
1665 dev_err(dev, "DTO: URB max acceptable errors "
1666 "exceeded, resetting device\n");
1667 wa_reset_all(wa);
1668 }
1669 seg->status = WA_SEG_ERROR;
1670 seg->result = urb->status;
1671 xfer->segs_done++;
1672 rpipe_ready = rpipe_avail_inc(rpipe);
1673 __wa_xfer_abort(xfer);
1674 done = __wa_xfer_is_done(xfer);
1675 spin_unlock_irqrestore(&xfer->lock, flags);
1676 if (done)
1677 wa_xfer_completion(xfer);
1678 if (rpipe_ready)
1679 wa_xfer_delayed_run(rpipe);
1680 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001681}
1682
1683/*
1684 * Handle an incoming transfer result buffer
1685 *
1686 * Given a transfer result buffer, it completes the transfer (possibly
1687 * scheduling and buffer in read) and then resubmits the DTI URB for a
1688 * new transfer result read.
1689 *
1690 *
1691 * The xfer_result DTI URB state machine
1692 *
1693 * States: OFF | RXR (Read-Xfer-Result) | RBI (Read-Buffer-In)
1694 *
1695 * We start in OFF mode, the first xfer_result notification [through
1696 * wa_handle_notif_xfer()] moves us to RXR by posting the DTI-URB to
1697 * read.
1698 *
1699 * We receive a buffer -- if it is not a xfer_result, we complain and
1700 * repost the DTI-URB. If it is a xfer_result then do the xfer seg
1701 * request accounting. If it is an IN segment, we move to RBI and post
1702 * a BUF-IN-URB to the right buffer. The BUF-IN-URB callback will
1703 * repost the DTI-URB and move to RXR state. if there was no IN
1704 * segment, it will repost the DTI-URB.
1705 *
1706 * We go back to OFF when we detect a ENOENT or ESHUTDOWN (or too many
1707 * errors) in the URBs.
1708 */
Thomas Pugliese0367eef2013-09-26 10:49:41 -05001709static void wa_dti_cb(struct urb *urb)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001710{
1711 int result;
1712 struct wahc *wa = urb->context;
1713 struct device *dev = &wa->usb_iface->dev;
1714 struct wa_xfer_result *xfer_result;
1715 u32 xfer_id;
1716 struct wa_xfer *xfer;
1717 u8 usb_status;
1718
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001719 BUG_ON(wa->dti_urb != urb);
1720 switch (wa->dti_urb->status) {
1721 case 0:
1722 /* We have a xfer result buffer; check it */
David Vrabelbce83692008-12-22 18:22:50 +00001723 dev_dbg(dev, "DTI: xfer result %d bytes at %p\n",
1724 urb->actual_length, urb->transfer_buffer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001725 if (wa->dti_urb->actual_length != sizeof(*xfer_result)) {
1726 dev_err(dev, "DTI Error: xfer result--bad size "
1727 "xfer result (%d bytes vs %zu needed)\n",
1728 urb->actual_length, sizeof(*xfer_result));
1729 break;
1730 }
Thomas Pugliese0367eef2013-09-26 10:49:41 -05001731 xfer_result = (struct wa_xfer_result *)(wa->dti_buf);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001732 if (xfer_result->hdr.bLength != sizeof(*xfer_result)) {
1733 dev_err(dev, "DTI Error: xfer result--"
1734 "bad header length %u\n",
1735 xfer_result->hdr.bLength);
1736 break;
1737 }
1738 if (xfer_result->hdr.bNotifyType != WA_XFER_RESULT) {
1739 dev_err(dev, "DTI Error: xfer result--"
1740 "bad header type 0x%02x\n",
1741 xfer_result->hdr.bNotifyType);
1742 break;
1743 }
1744 usb_status = xfer_result->bTransferStatus & 0x3f;
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001745 if (usb_status == WA_XFER_STATUS_NOT_FOUND)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001746 /* taken care of already */
1747 break;
1748 xfer_id = xfer_result->dwTransferID;
1749 xfer = wa_xfer_get_by_id(wa, xfer_id);
1750 if (xfer == NULL) {
1751 /* FIXME: transaction might have been cancelled */
1752 dev_err(dev, "DTI Error: xfer result--"
1753 "unknown xfer 0x%08x (status 0x%02x)\n",
1754 xfer_id, usb_status);
1755 break;
1756 }
Thomas Pugliese0367eef2013-09-26 10:49:41 -05001757 wa_xfer_result_chew(wa, xfer, xfer_result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001758 wa_xfer_put(xfer);
1759 break;
1760 case -ENOENT: /* (we killed the URB)...so, no broadcast */
1761 case -ESHUTDOWN: /* going away! */
1762 dev_dbg(dev, "DTI: going down! %d\n", urb->status);
1763 goto out;
1764 default:
1765 /* Unknown error */
1766 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS,
1767 EDC_ERROR_TIMEFRAME)) {
1768 dev_err(dev, "DTI: URB max acceptable errors "
1769 "exceeded, resetting device\n");
1770 wa_reset_all(wa);
1771 goto out;
1772 }
1773 if (printk_ratelimit())
1774 dev_err(dev, "DTI: URB error %d\n", urb->status);
1775 break;
1776 }
1777 /* Resubmit the DTI URB */
1778 result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
1779 if (result < 0) {
1780 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
1781 "resetting\n", result);
1782 wa_reset_all(wa);
1783 }
1784out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001785 return;
1786}
1787
1788/*
1789 * Transfer complete notification
1790 *
1791 * Called from the notif.c code. We get a notification on EP2 saying
1792 * that some endpoint has some transfer result data available. We are
1793 * about to read it.
1794 *
1795 * To speed up things, we always have a URB reading the DTI URB; we
1796 * don't really set it up and start it until the first xfer complete
1797 * notification arrives, which is what we do here.
1798 *
Thomas Pugliese0367eef2013-09-26 10:49:41 -05001799 * Follow up in wa_dti_cb(), as that's where the whole state
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001800 * machine starts.
1801 *
1802 * So here we just initialize the DTI URB for reading transfer result
1803 * notifications and also the buffer-in URB, for reading buffers. Then
1804 * we just submit the DTI URB.
1805 *
1806 * @wa shall be referenced
1807 */
1808void wa_handle_notif_xfer(struct wahc *wa, struct wa_notif_hdr *notif_hdr)
1809{
1810 int result;
1811 struct device *dev = &wa->usb_iface->dev;
1812 struct wa_notif_xfer *notif_xfer;
1813 const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd;
1814
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001815 notif_xfer = container_of(notif_hdr, struct wa_notif_xfer, hdr);
1816 BUG_ON(notif_hdr->bNotifyType != WA_NOTIF_TRANSFER);
1817
1818 if ((0x80 | notif_xfer->bEndpoint) != dti_epd->bEndpointAddress) {
1819 /* FIXME: hardcoded limitation, adapt */
1820 dev_err(dev, "BUG: DTI ep is %u, not %u (hack me)\n",
1821 notif_xfer->bEndpoint, dti_epd->bEndpointAddress);
1822 goto error;
1823 }
1824 if (wa->dti_urb != NULL) /* DTI URB already started */
1825 goto out;
1826
1827 wa->dti_urb = usb_alloc_urb(0, GFP_KERNEL);
1828 if (wa->dti_urb == NULL) {
1829 dev_err(dev, "Can't allocate DTI URB\n");
1830 goto error_dti_urb_alloc;
1831 }
1832 usb_fill_bulk_urb(
1833 wa->dti_urb, wa->usb_dev,
1834 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
Thomas Pugliese0367eef2013-09-26 10:49:41 -05001835 wa->dti_buf, wa->dti_buf_size,
1836 wa_dti_cb, wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001837
1838 wa->buf_in_urb = usb_alloc_urb(0, GFP_KERNEL);
1839 if (wa->buf_in_urb == NULL) {
1840 dev_err(dev, "Can't allocate BUF-IN URB\n");
1841 goto error_buf_in_urb_alloc;
1842 }
1843 usb_fill_bulk_urb(
1844 wa->buf_in_urb, wa->usb_dev,
1845 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
1846 NULL, 0, wa_buf_in_cb, wa);
1847 result = usb_submit_urb(wa->dti_urb, GFP_KERNEL);
1848 if (result < 0) {
1849 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
1850 "resetting\n", result);
1851 goto error_dti_urb_submit;
1852 }
1853out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001854 return;
1855
1856error_dti_urb_submit:
1857 usb_put_urb(wa->buf_in_urb);
1858error_buf_in_urb_alloc:
1859 usb_put_urb(wa->dti_urb);
1860 wa->dti_urb = NULL;
1861error_dti_urb_alloc:
1862error:
1863 wa_reset_all(wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001864}