blob: 6f935d575b07c5859695911381f69d801a5b7d02 [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
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500311/* Return the xfer's ID. */
312static inline u32 wa_xfer_id(struct wa_xfer *xfer)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100313{
314 return xfer->id;
315}
316
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500317/* Return the xfer's ID in transport format (little endian). */
318static inline __le32 wa_xfer_id_le32(struct wa_xfer *xfer)
319{
320 return cpu_to_le32(xfer->id);
321}
322
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100323/*
324 * Search for a transfer list ID on the HCD's URB list
325 *
326 * For 32 bit architectures, we use the pointer itself; for 64 bits, a
327 * 32-bit hash of the pointer.
328 *
329 * @returns NULL if not found.
330 */
331static struct wa_xfer *wa_xfer_get_by_id(struct wahc *wa, u32 id)
332{
333 unsigned long flags;
334 struct wa_xfer *xfer_itr;
335 spin_lock_irqsave(&wa->xfer_list_lock, flags);
336 list_for_each_entry(xfer_itr, &wa->xfer_list, list_node) {
337 if (id == xfer_itr->id) {
338 wa_xfer_get(xfer_itr);
339 goto out;
340 }
341 }
342 xfer_itr = NULL;
343out:
344 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
345 return xfer_itr;
346}
347
348struct wa_xfer_abort_buffer {
349 struct urb urb;
350 struct wa_xfer_abort cmd;
351};
352
353static void __wa_xfer_abort_cb(struct urb *urb)
354{
355 struct wa_xfer_abort_buffer *b = urb->context;
356 usb_put_urb(&b->urb);
357}
358
359/*
360 * Aborts an ongoing transaction
361 *
362 * Assumes the transfer is referenced and locked and in a submitted
363 * state (mainly that there is an endpoint/rpipe assigned).
364 *
365 * The callback (see above) does nothing but freeing up the data by
366 * putting the URB. Because the URB is allocated at the head of the
367 * struct, the whole space we allocated is kfreed.
368 *
369 * We'll get an 'aborted transaction' xfer result on DTI, that'll
370 * politely ignore because at this point the transaction has been
371 * marked as aborted already.
372 */
373static void __wa_xfer_abort(struct wa_xfer *xfer)
374{
375 int result;
376 struct device *dev = &xfer->wa->usb_iface->dev;
377 struct wa_xfer_abort_buffer *b;
378 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
379
380 b = kmalloc(sizeof(*b), GFP_ATOMIC);
381 if (b == NULL)
382 goto error_kmalloc;
383 b->cmd.bLength = sizeof(b->cmd);
384 b->cmd.bRequestType = WA_XFER_ABORT;
385 b->cmd.wRPipe = rpipe->descr.wRPipeIndex;
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500386 b->cmd.dwTransferID = wa_xfer_id_le32(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100387
388 usb_init_urb(&b->urb);
389 usb_fill_bulk_urb(&b->urb, xfer->wa->usb_dev,
390 usb_sndbulkpipe(xfer->wa->usb_dev,
391 xfer->wa->dto_epd->bEndpointAddress),
392 &b->cmd, sizeof(b->cmd), __wa_xfer_abort_cb, b);
393 result = usb_submit_urb(&b->urb, GFP_ATOMIC);
394 if (result < 0)
395 goto error_submit;
396 return; /* callback frees! */
397
398
399error_submit:
400 if (printk_ratelimit())
401 dev_err(dev, "xfer %p: Can't submit abort request: %d\n",
402 xfer, result);
403 kfree(b);
404error_kmalloc:
405 return;
406
407}
408
409/*
410 *
411 * @returns < 0 on error, transfer segment request size if ok
412 */
413static ssize_t __wa_xfer_setup_sizes(struct wa_xfer *xfer,
414 enum wa_xfer_type *pxfer_type)
415{
416 ssize_t result;
417 struct device *dev = &xfer->wa->usb_iface->dev;
418 size_t maxpktsize;
419 struct urb *urb = xfer->urb;
420 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
421
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100422 switch (rpipe->descr.bmAttribute & 0x3) {
423 case USB_ENDPOINT_XFER_CONTROL:
424 *pxfer_type = WA_XFER_TYPE_CTL;
425 result = sizeof(struct wa_xfer_ctl);
426 break;
427 case USB_ENDPOINT_XFER_INT:
428 case USB_ENDPOINT_XFER_BULK:
429 *pxfer_type = WA_XFER_TYPE_BI;
430 result = sizeof(struct wa_xfer_bi);
431 break;
432 case USB_ENDPOINT_XFER_ISOC:
433 dev_err(dev, "FIXME: ISOC not implemented\n");
434 result = -ENOSYS;
435 goto error;
436 default:
437 /* never happens */
438 BUG();
439 result = -EINVAL; /* shut gcc up */
440 };
441 xfer->is_inbound = urb->pipe & USB_DIR_IN ? 1 : 0;
442 xfer->is_dma = urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? 1 : 0;
443 xfer->seg_size = le16_to_cpu(rpipe->descr.wBlocks)
444 * 1 << (xfer->wa->wa_descr->bRPipeBlockSize - 1);
445 /* Compute the segment size and make sure it is a multiple of
446 * the maxpktsize (WUSB1.0[8.3.3.1])...not really too much of
447 * a check (FIXME) */
448 maxpktsize = le16_to_cpu(rpipe->descr.wMaxPacketSize);
449 if (xfer->seg_size < maxpktsize) {
450 dev_err(dev, "HW BUG? seg_size %zu smaller than maxpktsize "
451 "%zu\n", xfer->seg_size, maxpktsize);
452 result = -EINVAL;
453 goto error;
454 }
455 xfer->seg_size = (xfer->seg_size / maxpktsize) * maxpktsize;
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500456 xfer->segs = DIV_ROUND_UP(urb->transfer_buffer_length, xfer->seg_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100457 if (xfer->segs >= WA_SEGS_MAX) {
458 dev_err(dev, "BUG? ops, number of segments %d bigger than %d\n",
459 (int)(urb->transfer_buffer_length / xfer->seg_size),
460 WA_SEGS_MAX);
461 result = -EINVAL;
462 goto error;
463 }
464 if (xfer->segs == 0 && *pxfer_type == WA_XFER_TYPE_CTL)
465 xfer->segs = 1;
466error:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100467 return result;
468}
469
David Vrabelbce83692008-12-22 18:22:50 +0000470/* Fill in the common request header and xfer-type specific data. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100471static void __wa_xfer_setup_hdr0(struct wa_xfer *xfer,
472 struct wa_xfer_hdr *xfer_hdr0,
473 enum wa_xfer_type xfer_type,
474 size_t xfer_hdr_size)
475{
476 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
477
478 xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
479 xfer_hdr0->bLength = xfer_hdr_size;
480 xfer_hdr0->bRequestType = xfer_type;
481 xfer_hdr0->wRPipe = rpipe->descr.wRPipeIndex;
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500482 xfer_hdr0->dwTransferID = wa_xfer_id_le32(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100483 xfer_hdr0->bTransferSegment = 0;
484 switch (xfer_type) {
485 case WA_XFER_TYPE_CTL: {
486 struct wa_xfer_ctl *xfer_ctl =
487 container_of(xfer_hdr0, struct wa_xfer_ctl, hdr);
488 xfer_ctl->bmAttribute = xfer->is_inbound ? 1 : 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100489 memcpy(&xfer_ctl->baSetupData, xfer->urb->setup_packet,
490 sizeof(xfer_ctl->baSetupData));
491 break;
492 }
493 case WA_XFER_TYPE_BI:
494 break;
495 case WA_XFER_TYPE_ISO:
496 printk(KERN_ERR "FIXME: ISOC not implemented\n");
497 default:
498 BUG();
499 };
500}
501
502/*
503 * Callback for the OUT data phase of the segment request
504 *
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500505 * Check wa_seg_tr_cb(); most comments also apply here because this
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100506 * function does almost the same thing and they work closely
507 * together.
508 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300509 * If the seg request has failed but this DTO phase has succeeded,
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500510 * wa_seg_tr_cb() has already failed the segment and moved the
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100511 * status to WA_SEG_ERROR, so this will go through 'case 0' and
512 * effectively do nothing.
513 */
514static void wa_seg_dto_cb(struct urb *urb)
515{
516 struct wa_seg *seg = urb->context;
517 struct wa_xfer *xfer = seg->xfer;
518 struct wahc *wa;
519 struct device *dev;
520 struct wa_rpipe *rpipe;
521 unsigned long flags;
522 unsigned rpipe_ready = 0;
523 u8 done = 0;
524
Thomas Pugliesed5b5c9f2013-09-26 14:08:15 -0500525 /* free the sg if it was used. */
526 kfree(urb->sg);
527 urb->sg = NULL;
528
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100529 switch (urb->status) {
530 case 0:
531 spin_lock_irqsave(&xfer->lock, flags);
532 wa = xfer->wa;
533 dev = &wa->usb_iface->dev;
David Vrabelbce83692008-12-22 18:22:50 +0000534 dev_dbg(dev, "xfer %p#%u: data out done (%d bytes)\n",
535 xfer, seg->index, urb->actual_length);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100536 if (seg->status < WA_SEG_PENDING)
537 seg->status = WA_SEG_PENDING;
538 seg->result = urb->actual_length;
539 spin_unlock_irqrestore(&xfer->lock, flags);
540 break;
541 case -ECONNRESET: /* URB unlinked; no need to do anything */
542 case -ENOENT: /* as it was done by the who unlinked us */
543 break;
544 default: /* Other errors ... */
545 spin_lock_irqsave(&xfer->lock, flags);
546 wa = xfer->wa;
547 dev = &wa->usb_iface->dev;
548 rpipe = xfer->ep->hcpriv;
David Vrabelbce83692008-12-22 18:22:50 +0000549 dev_dbg(dev, "xfer %p#%u: data out error %d\n",
550 xfer, seg->index, urb->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100551 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
552 EDC_ERROR_TIMEFRAME)){
553 dev_err(dev, "DTO: URB max acceptable errors "
554 "exceeded, resetting device\n");
555 wa_reset_all(wa);
556 }
557 if (seg->status != WA_SEG_ERROR) {
558 seg->status = WA_SEG_ERROR;
559 seg->result = urb->status;
560 xfer->segs_done++;
561 __wa_xfer_abort(xfer);
562 rpipe_ready = rpipe_avail_inc(rpipe);
563 done = __wa_xfer_is_done(xfer);
564 }
565 spin_unlock_irqrestore(&xfer->lock, flags);
566 if (done)
567 wa_xfer_completion(xfer);
568 if (rpipe_ready)
569 wa_xfer_delayed_run(rpipe);
570 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100571}
572
573/*
574 * Callback for the segment request
575 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200576 * If successful transition state (unless already transitioned or
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100577 * outbound transfer); otherwise, take a note of the error, mark this
578 * segment done and try completion.
579 *
580 * Note we don't access until we are sure that the transfer hasn't
581 * been cancelled (ECONNRESET, ENOENT), which could mean that
582 * seg->xfer could be already gone.
583 *
584 * We have to check before setting the status to WA_SEG_PENDING
585 * because sometimes the xfer result callback arrives before this
586 * callback (geeeeeeze), so it might happen that we are already in
587 * another state. As well, we don't set it if the transfer is inbound,
588 * as in that case, wa_seg_dto_cb will do it when the OUT data phase
589 * finishes.
590 */
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500591static void wa_seg_tr_cb(struct urb *urb)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100592{
593 struct wa_seg *seg = urb->context;
594 struct wa_xfer *xfer = seg->xfer;
595 struct wahc *wa;
596 struct device *dev;
597 struct wa_rpipe *rpipe;
598 unsigned long flags;
599 unsigned rpipe_ready;
600 u8 done = 0;
601
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100602 switch (urb->status) {
603 case 0:
604 spin_lock_irqsave(&xfer->lock, flags);
605 wa = xfer->wa;
606 dev = &wa->usb_iface->dev;
David Vrabelbce83692008-12-22 18:22:50 +0000607 dev_dbg(dev, "xfer %p#%u: request done\n", xfer, seg->index);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100608 if (xfer->is_inbound && seg->status < WA_SEG_PENDING)
609 seg->status = WA_SEG_PENDING;
610 spin_unlock_irqrestore(&xfer->lock, flags);
611 break;
612 case -ECONNRESET: /* URB unlinked; no need to do anything */
613 case -ENOENT: /* as it was done by the who unlinked us */
614 break;
615 default: /* Other errors ... */
616 spin_lock_irqsave(&xfer->lock, flags);
617 wa = xfer->wa;
618 dev = &wa->usb_iface->dev;
619 rpipe = xfer->ep->hcpriv;
620 if (printk_ratelimit())
621 dev_err(dev, "xfer %p#%u: request error %d\n",
622 xfer, seg->index, urb->status);
623 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
624 EDC_ERROR_TIMEFRAME)){
625 dev_err(dev, "DTO: URB max acceptable errors "
626 "exceeded, resetting device\n");
627 wa_reset_all(wa);
628 }
629 usb_unlink_urb(seg->dto_urb);
630 seg->status = WA_SEG_ERROR;
631 seg->result = urb->status;
632 xfer->segs_done++;
633 __wa_xfer_abort(xfer);
634 rpipe_ready = rpipe_avail_inc(rpipe);
635 done = __wa_xfer_is_done(xfer);
636 spin_unlock_irqrestore(&xfer->lock, flags);
637 if (done)
638 wa_xfer_completion(xfer);
639 if (rpipe_ready)
640 wa_xfer_delayed_run(rpipe);
641 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100642}
643
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500644/*
645 * Allocate an SG list to store bytes_to_transfer bytes and copy the
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500646 * subset of the in_sg that matches the buffer subset
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500647 * we are about to transfer.
648 */
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500649static struct scatterlist *wa_xfer_create_subset_sg(struct scatterlist *in_sg,
650 const unsigned int bytes_transferred,
651 const unsigned int bytes_to_transfer, unsigned int *out_num_sgs)
652{
653 struct scatterlist *out_sg;
654 unsigned int bytes_processed = 0, offset_into_current_page_data = 0,
655 nents;
656 struct scatterlist *current_xfer_sg = in_sg;
657 struct scatterlist *current_seg_sg, *last_seg_sg;
658
659 /* skip previously transferred pages. */
660 while ((current_xfer_sg) &&
661 (bytes_processed < bytes_transferred)) {
662 bytes_processed += current_xfer_sg->length;
663
664 /* advance the sg if current segment starts on or past the
665 next page. */
666 if (bytes_processed <= bytes_transferred)
667 current_xfer_sg = sg_next(current_xfer_sg);
668 }
669
670 /* the data for the current segment starts in current_xfer_sg.
671 calculate the offset. */
672 if (bytes_processed > bytes_transferred) {
673 offset_into_current_page_data = current_xfer_sg->length -
674 (bytes_processed - bytes_transferred);
675 }
676
677 /* calculate the number of pages needed by this segment. */
678 nents = DIV_ROUND_UP((bytes_to_transfer +
679 offset_into_current_page_data +
680 current_xfer_sg->offset),
681 PAGE_SIZE);
682
683 out_sg = kmalloc((sizeof(struct scatterlist) * nents), GFP_ATOMIC);
684 if (out_sg) {
685 sg_init_table(out_sg, nents);
686
687 /* copy the portion of the incoming SG that correlates to the
688 * data to be transferred by this segment to the segment SG. */
689 last_seg_sg = current_seg_sg = out_sg;
690 bytes_processed = 0;
691
692 /* reset nents and calculate the actual number of sg entries
693 needed. */
694 nents = 0;
695 while ((bytes_processed < bytes_to_transfer) &&
696 current_seg_sg && current_xfer_sg) {
697 unsigned int page_len = min((current_xfer_sg->length -
698 offset_into_current_page_data),
699 (bytes_to_transfer - bytes_processed));
700
701 sg_set_page(current_seg_sg, sg_page(current_xfer_sg),
702 page_len,
703 current_xfer_sg->offset +
704 offset_into_current_page_data);
705
706 bytes_processed += page_len;
707
708 last_seg_sg = current_seg_sg;
709 current_seg_sg = sg_next(current_seg_sg);
710 current_xfer_sg = sg_next(current_xfer_sg);
711
712 /* only the first page may require additional offset. */
713 offset_into_current_page_data = 0;
714 nents++;
715 }
716
717 /* update num_sgs and terminate the list since we may have
718 * concatenated pages. */
719 sg_mark_end(last_seg_sg);
720 *out_num_sgs = nents;
721 }
722
723 return out_sg;
724}
725
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100726/*
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500727 * Populate buffer ptr and size, DMA buffer or SG list for the dto urb.
728 */
729static int __wa_populate_dto_urb(struct wa_xfer *xfer,
730 struct wa_seg *seg, size_t buf_itr_offset, size_t buf_itr_size)
731{
732 int result = 0;
733
734 if (xfer->is_dma) {
735 seg->dto_urb->transfer_dma =
736 xfer->urb->transfer_dma + buf_itr_offset;
737 seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
738 seg->dto_urb->sg = NULL;
739 seg->dto_urb->num_sgs = 0;
740 } else {
741 /* do buffer or SG processing. */
742 seg->dto_urb->transfer_flags &=
743 ~URB_NO_TRANSFER_DMA_MAP;
744 /* this should always be 0 before a resubmit. */
745 seg->dto_urb->num_mapped_sgs = 0;
746
747 if (xfer->urb->transfer_buffer) {
748 seg->dto_urb->transfer_buffer =
749 xfer->urb->transfer_buffer +
750 buf_itr_offset;
751 seg->dto_urb->sg = NULL;
752 seg->dto_urb->num_sgs = 0;
753 } else {
754 seg->dto_urb->transfer_buffer = NULL;
755
756 /*
757 * allocate an SG list to store seg_size bytes
758 * and copy the subset of the xfer->urb->sg that
759 * matches the buffer subset we are about to
760 * read.
761 */
762 seg->dto_urb->sg = wa_xfer_create_subset_sg(
763 xfer->urb->sg,
764 buf_itr_offset, buf_itr_size,
765 &(seg->dto_urb->num_sgs));
766 if (!(seg->dto_urb->sg))
767 result = -ENOMEM;
768 }
769 }
770 seg->dto_urb->transfer_buffer_length = buf_itr_size;
771
772 return result;
773}
774
775/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100776 * Allocate the segs array and initialize each of them
777 *
778 * The segments are freed by wa_xfer_destroy() when the xfer use count
779 * drops to zero; however, because each segment is given the same life
780 * cycle as the USB URB it contains, it is actually freed by
781 * usb_put_urb() on the contained USB URB (twisted, eh?).
782 */
783static int __wa_xfer_setup_segs(struct wa_xfer *xfer, size_t xfer_hdr_size)
784{
785 int result, cnt;
786 size_t alloc_size = sizeof(*xfer->seg[0])
787 - sizeof(xfer->seg[0]->xfer_hdr) + xfer_hdr_size;
788 struct usb_device *usb_dev = xfer->wa->usb_dev;
789 const struct usb_endpoint_descriptor *dto_epd = xfer->wa->dto_epd;
790 struct wa_seg *seg;
791 size_t buf_itr, buf_size, buf_itr_size;
792
793 result = -ENOMEM;
David Vrabel92c4d9b2008-10-15 14:50:10 +0100794 xfer->seg = kcalloc(xfer->segs, sizeof(xfer->seg[0]), GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100795 if (xfer->seg == NULL)
796 goto error_segs_kzalloc;
797 buf_itr = 0;
798 buf_size = xfer->urb->transfer_buffer_length;
799 for (cnt = 0; cnt < xfer->segs; cnt++) {
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500800 seg = xfer->seg[cnt] = kmalloc(alloc_size, GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100801 if (seg == NULL)
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500802 goto error_seg_kmalloc;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100803 wa_seg_init(seg);
804 seg->xfer = xfer;
805 seg->index = cnt;
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500806 usb_fill_bulk_urb(&seg->tr_urb, usb_dev,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100807 usb_sndbulkpipe(usb_dev,
808 dto_epd->bEndpointAddress),
809 &seg->xfer_hdr, xfer_hdr_size,
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500810 wa_seg_tr_cb, seg);
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500811 buf_itr_size = min(buf_size, xfer->seg_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100812 if (xfer->is_inbound == 0 && buf_size > 0) {
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500813 /* outbound data. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100814 seg->dto_urb = usb_alloc_urb(0, GFP_ATOMIC);
815 if (seg->dto_urb == NULL)
816 goto error_dto_alloc;
817 usb_fill_bulk_urb(
818 seg->dto_urb, usb_dev,
819 usb_sndbulkpipe(usb_dev,
820 dto_epd->bEndpointAddress),
821 NULL, 0, wa_seg_dto_cb, seg);
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500822
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500823 /* fill in the xfer buffer information. */
824 result = __wa_populate_dto_urb(xfer, seg,
825 buf_itr, buf_itr_size);
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500826
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500827 if (result < 0)
828 goto error_seg_outbound_populate;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100829 }
830 seg->status = WA_SEG_READY;
831 buf_itr += buf_itr_size;
832 buf_size -= buf_itr_size;
833 }
834 return 0;
835
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500836 /*
837 * Free the memory for the current segment which failed to init.
838 * Use the fact that cnt is left at were it failed. The remaining
839 * segments will be cleaned up by wa_xfer_destroy.
840 */
841error_seg_outbound_populate:
Thomas Pugliese11b1bf82013-08-15 14:37:41 -0500842 usb_free_urb(xfer->seg[cnt]->dto_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100843error_dto_alloc:
844 kfree(xfer->seg[cnt]);
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500845 xfer->seg[cnt] = NULL;
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500846error_seg_kmalloc:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100847error_segs_kzalloc:
848 return result;
849}
850
851/*
852 * Allocates all the stuff needed to submit a transfer
853 *
854 * Breaks the whole data buffer in a list of segments, each one has a
855 * structure allocated to it and linked in xfer->seg[index]
856 *
857 * FIXME: merge setup_segs() and the last part of this function, no
858 * need to do two for loops when we could run everything in a
859 * single one
860 */
861static int __wa_xfer_setup(struct wa_xfer *xfer, struct urb *urb)
862{
863 int result;
864 struct device *dev = &xfer->wa->usb_iface->dev;
865 enum wa_xfer_type xfer_type = 0; /* shut up GCC */
866 size_t xfer_hdr_size, cnt, transfer_size;
867 struct wa_xfer_hdr *xfer_hdr0, *xfer_hdr;
868
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100869 result = __wa_xfer_setup_sizes(xfer, &xfer_type);
870 if (result < 0)
871 goto error_setup_sizes;
872 xfer_hdr_size = result;
873 result = __wa_xfer_setup_segs(xfer, xfer_hdr_size);
874 if (result < 0) {
875 dev_err(dev, "xfer %p: Failed to allocate %d segments: %d\n",
876 xfer, xfer->segs, result);
877 goto error_setup_segs;
878 }
879 /* Fill the first header */
880 xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
881 wa_xfer_id_init(xfer);
882 __wa_xfer_setup_hdr0(xfer, xfer_hdr0, xfer_type, xfer_hdr_size);
883
884 /* Fill remainig headers */
885 xfer_hdr = xfer_hdr0;
886 transfer_size = urb->transfer_buffer_length;
887 xfer_hdr0->dwTransferLength = transfer_size > xfer->seg_size ?
888 xfer->seg_size : transfer_size;
889 transfer_size -= xfer->seg_size;
890 for (cnt = 1; cnt < xfer->segs; cnt++) {
891 xfer_hdr = &xfer->seg[cnt]->xfer_hdr;
892 memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
893 xfer_hdr->bTransferSegment = cnt;
894 xfer_hdr->dwTransferLength = transfer_size > xfer->seg_size ?
895 cpu_to_le32(xfer->seg_size)
896 : cpu_to_le32(transfer_size);
897 xfer->seg[cnt]->status = WA_SEG_READY;
898 transfer_size -= xfer->seg_size;
899 }
900 xfer_hdr->bTransferSegment |= 0x80; /* this is the last segment */
901 result = 0;
902error_setup_segs:
903error_setup_sizes:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100904 return result;
905}
906
907/*
908 *
909 *
910 * rpipe->seg_lock is held!
911 */
912static int __wa_seg_submit(struct wa_rpipe *rpipe, struct wa_xfer *xfer,
913 struct wa_seg *seg)
914{
915 int result;
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500916 /* submit the transfer request. */
917 result = usb_submit_urb(&seg->tr_urb, GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100918 if (result < 0) {
919 printk(KERN_ERR "xfer %p#%u: REQ submit failed: %d\n",
920 xfer, seg->index, result);
921 goto error_seg_submit;
922 }
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500923 /* submit the out data if this is an out request. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100924 if (seg->dto_urb) {
925 result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
926 if (result < 0) {
927 printk(KERN_ERR "xfer %p#%u: DTO submit failed: %d\n",
928 xfer, seg->index, result);
929 goto error_dto_submit;
930 }
931 }
932 seg->status = WA_SEG_SUBMITTED;
933 rpipe_avail_dec(rpipe);
934 return 0;
935
936error_dto_submit:
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500937 usb_unlink_urb(&seg->tr_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100938error_seg_submit:
939 seg->status = WA_SEG_ERROR;
940 seg->result = result;
941 return result;
942}
943
944/*
945 * Execute more queued request segments until the maximum concurrent allowed
946 *
947 * The ugly unlock/lock sequence on the error path is needed as the
948 * xfer->lock normally nests the seg_lock and not viceversa.
949 *
950 */
951static void wa_xfer_delayed_run(struct wa_rpipe *rpipe)
952{
953 int result;
954 struct device *dev = &rpipe->wa->usb_iface->dev;
955 struct wa_seg *seg;
956 struct wa_xfer *xfer;
957 unsigned long flags;
958
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100959 spin_lock_irqsave(&rpipe->seg_lock, flags);
960 while (atomic_read(&rpipe->segs_available) > 0
961 && !list_empty(&rpipe->seg_list)) {
Thomas Pugliesee9a088f2013-08-12 10:10:53 -0500962 seg = list_first_entry(&(rpipe->seg_list), struct wa_seg,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100963 list_node);
964 list_del(&seg->list_node);
965 xfer = seg->xfer;
966 result = __wa_seg_submit(rpipe, xfer, seg);
David Vrabelbce83692008-12-22 18:22:50 +0000967 dev_dbg(dev, "xfer %p#%u submitted from delayed [%d segments available] %d\n",
968 xfer, seg->index, atomic_read(&rpipe->segs_available), result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100969 if (unlikely(result < 0)) {
970 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
971 spin_lock_irqsave(&xfer->lock, flags);
972 __wa_xfer_abort(xfer);
973 xfer->segs_done++;
974 spin_unlock_irqrestore(&xfer->lock, flags);
975 spin_lock_irqsave(&rpipe->seg_lock, flags);
976 }
977 }
978 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100979}
980
981/*
982 *
983 * xfer->lock is taken
984 *
985 * On failure submitting we just stop submitting and return error;
986 * wa_urb_enqueue_b() will execute the completion path
987 */
988static int __wa_xfer_submit(struct wa_xfer *xfer)
989{
990 int result;
991 struct wahc *wa = xfer->wa;
992 struct device *dev = &wa->usb_iface->dev;
993 unsigned cnt;
994 struct wa_seg *seg;
995 unsigned long flags;
996 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
997 size_t maxrequests = le16_to_cpu(rpipe->descr.wRequests);
998 u8 available;
999 u8 empty;
1000
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001001 spin_lock_irqsave(&wa->xfer_list_lock, flags);
1002 list_add_tail(&xfer->list_node, &wa->xfer_list);
1003 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
1004
1005 BUG_ON(atomic_read(&rpipe->segs_available) > maxrequests);
1006 result = 0;
1007 spin_lock_irqsave(&rpipe->seg_lock, flags);
1008 for (cnt = 0; cnt < xfer->segs; cnt++) {
1009 available = atomic_read(&rpipe->segs_available);
1010 empty = list_empty(&rpipe->seg_list);
1011 seg = xfer->seg[cnt];
David Vrabelbce83692008-12-22 18:22:50 +00001012 dev_dbg(dev, "xfer %p#%u: available %u empty %u (%s)\n",
1013 xfer, cnt, available, empty,
1014 available == 0 || !empty ? "delayed" : "submitted");
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001015 if (available == 0 || !empty) {
David Vrabelbce83692008-12-22 18:22:50 +00001016 dev_dbg(dev, "xfer %p#%u: delayed\n", xfer, cnt);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001017 seg->status = WA_SEG_DELAYED;
1018 list_add_tail(&seg->list_node, &rpipe->seg_list);
1019 } else {
1020 result = __wa_seg_submit(rpipe, xfer, seg);
David Vrabelbce83692008-12-22 18:22:50 +00001021 if (result < 0) {
1022 __wa_xfer_abort(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001023 goto error_seg_submit;
David Vrabelbce83692008-12-22 18:22:50 +00001024 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001025 }
1026 xfer->segs_submitted++;
1027 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001028error_seg_submit:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001029 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001030 return result;
1031}
1032
1033/*
1034 * Second part of a URB/transfer enqueuement
1035 *
1036 * Assumes this comes from wa_urb_enqueue() [maybe through
1037 * wa_urb_enqueue_run()]. At this point:
1038 *
1039 * xfer->wa filled and refcounted
1040 * xfer->ep filled with rpipe refcounted if
1041 * delayed == 0
1042 * xfer->urb filled and refcounted (this is the case when called
1043 * from wa_urb_enqueue() as we come from usb_submit_urb()
1044 * and when called by wa_urb_enqueue_run(), as we took an
1045 * extra ref dropped by _run() after we return).
1046 * xfer->gfp filled
1047 *
1048 * If we fail at __wa_xfer_submit(), then we just check if we are done
1049 * and if so, we run the completion procedure. However, if we are not
1050 * yet done, we do nothing and wait for the completion handlers from
1051 * the submitted URBs or from the xfer-result path to kick in. If xfer
1052 * result never kicks in, the xfer will timeout from the USB code and
1053 * dequeue() will be called.
1054 */
1055static void wa_urb_enqueue_b(struct wa_xfer *xfer)
1056{
1057 int result;
1058 unsigned long flags;
1059 struct urb *urb = xfer->urb;
1060 struct wahc *wa = xfer->wa;
1061 struct wusbhc *wusbhc = wa->wusb;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001062 struct wusb_dev *wusb_dev;
1063 unsigned done;
1064
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001065 result = rpipe_get_by_ep(wa, xfer->ep, urb, xfer->gfp);
1066 if (result < 0)
1067 goto error_rpipe_get;
1068 result = -ENODEV;
1069 /* FIXME: segmentation broken -- kills DWA */
1070 mutex_lock(&wusbhc->mutex); /* get a WUSB dev */
Jiri Slaby49fa0922009-03-11 21:47:40 +01001071 if (urb->dev == NULL) {
1072 mutex_unlock(&wusbhc->mutex);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001073 goto error_dev_gone;
Jiri Slaby49fa0922009-03-11 21:47:40 +01001074 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001075 wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev);
1076 if (wusb_dev == NULL) {
1077 mutex_unlock(&wusbhc->mutex);
1078 goto error_dev_gone;
1079 }
1080 mutex_unlock(&wusbhc->mutex);
1081
1082 spin_lock_irqsave(&xfer->lock, flags);
1083 xfer->wusb_dev = wusb_dev;
1084 result = urb->status;
1085 if (urb->status != -EINPROGRESS)
1086 goto error_dequeued;
1087
1088 result = __wa_xfer_setup(xfer, urb);
1089 if (result < 0)
1090 goto error_xfer_setup;
1091 result = __wa_xfer_submit(xfer);
1092 if (result < 0)
1093 goto error_xfer_submit;
1094 spin_unlock_irqrestore(&xfer->lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001095 return;
1096
1097 /* this is basically wa_xfer_completion() broken up wa_xfer_giveback()
1098 * does a wa_xfer_put() that will call wa_xfer_destroy() and clean
1099 * upundo setup().
1100 */
1101error_xfer_setup:
1102error_dequeued:
1103 spin_unlock_irqrestore(&xfer->lock, flags);
1104 /* FIXME: segmentation broken, kills DWA */
1105 if (wusb_dev)
1106 wusb_dev_put(wusb_dev);
1107error_dev_gone:
1108 rpipe_put(xfer->ep->hcpriv);
1109error_rpipe_get:
1110 xfer->result = result;
1111 wa_xfer_giveback(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001112 return;
1113
1114error_xfer_submit:
1115 done = __wa_xfer_is_done(xfer);
1116 xfer->result = result;
1117 spin_unlock_irqrestore(&xfer->lock, flags);
1118 if (done)
1119 wa_xfer_completion(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001120}
1121
1122/*
1123 * Execute the delayed transfers in the Wire Adapter @wa
1124 *
1125 * We need to be careful here, as dequeue() could be called in the
1126 * middle. That's why we do the whole thing under the
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001127 * wa->xfer_list_lock. If dequeue() jumps in, it first locks xfer->lock
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001128 * and then checks the list -- so as we would be acquiring in inverse
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001129 * order, we move the delayed list to a separate list while locked and then
1130 * submit them without the list lock held.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001131 */
1132void wa_urb_enqueue_run(struct work_struct *ws)
1133{
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001134 struct wahc *wa = container_of(ws, struct wahc, xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001135 struct wa_xfer *xfer, *next;
1136 struct urb *urb;
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001137 LIST_HEAD(tmp_list);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001138
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001139 /* Create a copy of the wa->xfer_delayed_list while holding the lock */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001140 spin_lock_irq(&wa->xfer_list_lock);
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001141 list_cut_position(&tmp_list, &wa->xfer_delayed_list,
1142 wa->xfer_delayed_list.prev);
1143 spin_unlock_irq(&wa->xfer_list_lock);
1144
1145 /*
1146 * enqueue from temp list without list lock held since wa_urb_enqueue_b
1147 * can take xfer->lock as well as lock mutexes.
1148 */
1149 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001150 list_del_init(&xfer->list_node);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001151
1152 urb = xfer->urb;
1153 wa_urb_enqueue_b(xfer);
1154 usb_put_urb(urb); /* taken when queuing */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001155 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001156}
1157EXPORT_SYMBOL_GPL(wa_urb_enqueue_run);
1158
1159/*
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001160 * Process the errored transfers on the Wire Adapter outside of interrupt.
1161 */
1162void wa_process_errored_transfers_run(struct work_struct *ws)
1163{
1164 struct wahc *wa = container_of(ws, struct wahc, xfer_error_work);
1165 struct wa_xfer *xfer, *next;
1166 LIST_HEAD(tmp_list);
1167
1168 pr_info("%s: Run delayed STALL processing.\n", __func__);
1169
1170 /* Create a copy of the wa->xfer_errored_list while holding the lock */
1171 spin_lock_irq(&wa->xfer_list_lock);
1172 list_cut_position(&tmp_list, &wa->xfer_errored_list,
1173 wa->xfer_errored_list.prev);
1174 spin_unlock_irq(&wa->xfer_list_lock);
1175
1176 /*
1177 * run rpipe_clear_feature_stalled from temp list without list lock
1178 * held.
1179 */
1180 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
1181 struct usb_host_endpoint *ep;
1182 unsigned long flags;
1183 struct wa_rpipe *rpipe;
1184
1185 spin_lock_irqsave(&xfer->lock, flags);
1186 ep = xfer->ep;
1187 rpipe = ep->hcpriv;
1188 spin_unlock_irqrestore(&xfer->lock, flags);
1189
1190 /* clear RPIPE feature stalled without holding a lock. */
1191 rpipe_clear_feature_stalled(wa, ep);
1192
1193 /* complete the xfer. This removes it from the tmp list. */
1194 wa_xfer_completion(xfer);
1195
1196 /* check for work. */
1197 wa_xfer_delayed_run(rpipe);
1198 }
1199}
1200EXPORT_SYMBOL_GPL(wa_process_errored_transfers_run);
1201
1202/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001203 * Submit a transfer to the Wire Adapter in a delayed way
1204 *
1205 * The process of enqueuing involves possible sleeps() [see
1206 * enqueue_b(), for the rpipe_get() and the mutex_lock()]. If we are
1207 * in an atomic section, we defer the enqueue_b() call--else we call direct.
1208 *
1209 * @urb: We own a reference to it done by the HCI Linux USB stack that
1210 * will be given up by calling usb_hcd_giveback_urb() or by
1211 * returning error from this function -> ergo we don't have to
1212 * refcount it.
1213 */
1214int wa_urb_enqueue(struct wahc *wa, struct usb_host_endpoint *ep,
1215 struct urb *urb, gfp_t gfp)
1216{
1217 int result;
1218 struct device *dev = &wa->usb_iface->dev;
1219 struct wa_xfer *xfer;
1220 unsigned long my_flags;
1221 unsigned cant_sleep = irqs_disabled() | in_atomic();
1222
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001223 if ((urb->transfer_buffer == NULL)
1224 && (urb->sg == NULL)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001225 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1226 && urb->transfer_buffer_length != 0) {
1227 dev_err(dev, "BUG? urb %p: NULL xfer buffer & NODMA\n", urb);
1228 dump_stack();
1229 }
1230
1231 result = -ENOMEM;
1232 xfer = kzalloc(sizeof(*xfer), gfp);
1233 if (xfer == NULL)
1234 goto error_kmalloc;
1235
1236 result = -ENOENT;
1237 if (urb->status != -EINPROGRESS) /* cancelled */
1238 goto error_dequeued; /* before starting? */
1239 wa_xfer_init(xfer);
1240 xfer->wa = wa_get(wa);
1241 xfer->urb = urb;
1242 xfer->gfp = gfp;
1243 xfer->ep = ep;
1244 urb->hcpriv = xfer;
David Vrabelbce83692008-12-22 18:22:50 +00001245
1246 dev_dbg(dev, "xfer %p urb %p pipe 0x%02x [%d bytes] %s %s %s\n",
1247 xfer, urb, urb->pipe, urb->transfer_buffer_length,
1248 urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? "dma" : "nodma",
1249 urb->pipe & USB_DIR_IN ? "inbound" : "outbound",
1250 cant_sleep ? "deferred" : "inline");
1251
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001252 if (cant_sleep) {
1253 usb_get_urb(urb);
1254 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1255 list_add_tail(&xfer->list_node, &wa->xfer_delayed_list);
1256 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001257 queue_work(wusbd, &wa->xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001258 } else {
1259 wa_urb_enqueue_b(xfer);
1260 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001261 return 0;
1262
1263error_dequeued:
1264 kfree(xfer);
1265error_kmalloc:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001266 return result;
1267}
1268EXPORT_SYMBOL_GPL(wa_urb_enqueue);
1269
1270/*
1271 * Dequeue a URB and make sure uwb_hcd_giveback_urb() [completion
1272 * handler] is called.
1273 *
1274 * Until a transfer goes successfully through wa_urb_enqueue() it
1275 * needs to be dequeued with completion calling; when stuck in delayed
1276 * or before wa_xfer_setup() is called, we need to do completion.
1277 *
1278 * not setup If there is no hcpriv yet, that means that that enqueue
1279 * still had no time to set the xfer up. Because
1280 * urb->status should be other than -EINPROGRESS,
1281 * enqueue() will catch that and bail out.
1282 *
1283 * If the transfer has gone through setup, we just need to clean it
1284 * up. If it has gone through submit(), we have to abort it [with an
1285 * asynch request] and then make sure we cancel each segment.
1286 *
1287 */
1288int wa_urb_dequeue(struct wahc *wa, struct urb *urb)
1289{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001290 unsigned long flags, flags2;
1291 struct wa_xfer *xfer;
1292 struct wa_seg *seg;
1293 struct wa_rpipe *rpipe;
1294 unsigned cnt;
1295 unsigned rpipe_ready = 0;
1296
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001297 xfer = urb->hcpriv;
1298 if (xfer == NULL) {
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001299 /*
1300 * Nothing setup yet enqueue will see urb->status !=
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001301 * -EINPROGRESS (by hcd layer) and bail out with
1302 * error, no need to do completion
1303 */
1304 BUG_ON(urb->status == -EINPROGRESS);
1305 goto out;
1306 }
1307 spin_lock_irqsave(&xfer->lock, flags);
1308 rpipe = xfer->ep->hcpriv;
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001309 if (rpipe == NULL) {
1310 pr_debug("%s: xfer id 0x%08X has no RPIPE. %s",
1311 __func__, wa_xfer_id(xfer),
1312 "Probably already aborted.\n" );
1313 goto out_unlock;
1314 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001315 /* Check the delayed list -> if there, release and complete */
1316 spin_lock_irqsave(&wa->xfer_list_lock, flags2);
1317 if (!list_empty(&xfer->list_node) && xfer->seg == NULL)
1318 goto dequeue_delayed;
1319 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1320 if (xfer->seg == NULL) /* still hasn't reached */
1321 goto out_unlock; /* setup(), enqueue_b() completes */
1322 /* Ok, the xfer is in flight already, it's been setup and submitted.*/
1323 __wa_xfer_abort(xfer);
1324 for (cnt = 0; cnt < xfer->segs; cnt++) {
1325 seg = xfer->seg[cnt];
1326 switch (seg->status) {
1327 case WA_SEG_NOTREADY:
1328 case WA_SEG_READY:
1329 printk(KERN_ERR "xfer %p#%u: dequeue bad state %u\n",
1330 xfer, cnt, seg->status);
1331 WARN_ON(1);
1332 break;
1333 case WA_SEG_DELAYED:
1334 seg->status = WA_SEG_ABORTED;
1335 spin_lock_irqsave(&rpipe->seg_lock, flags2);
1336 list_del(&seg->list_node);
1337 xfer->segs_done++;
1338 rpipe_ready = rpipe_avail_inc(rpipe);
1339 spin_unlock_irqrestore(&rpipe->seg_lock, flags2);
1340 break;
1341 case WA_SEG_SUBMITTED:
1342 seg->status = WA_SEG_ABORTED;
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001343 usb_unlink_urb(&seg->tr_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001344 if (xfer->is_inbound == 0)
1345 usb_unlink_urb(seg->dto_urb);
1346 xfer->segs_done++;
1347 rpipe_ready = rpipe_avail_inc(rpipe);
1348 break;
1349 case WA_SEG_PENDING:
1350 seg->status = WA_SEG_ABORTED;
1351 xfer->segs_done++;
1352 rpipe_ready = rpipe_avail_inc(rpipe);
1353 break;
1354 case WA_SEG_DTI_PENDING:
1355 usb_unlink_urb(wa->dti_urb);
1356 seg->status = WA_SEG_ABORTED;
1357 xfer->segs_done++;
1358 rpipe_ready = rpipe_avail_inc(rpipe);
1359 break;
1360 case WA_SEG_DONE:
1361 case WA_SEG_ERROR:
1362 case WA_SEG_ABORTED:
1363 break;
1364 }
1365 }
1366 xfer->result = urb->status; /* -ENOENT or -ECONNRESET */
1367 __wa_xfer_is_done(xfer);
1368 spin_unlock_irqrestore(&xfer->lock, flags);
1369 wa_xfer_completion(xfer);
1370 if (rpipe_ready)
1371 wa_xfer_delayed_run(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001372 return 0;
1373
1374out_unlock:
1375 spin_unlock_irqrestore(&xfer->lock, flags);
1376out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001377 return 0;
1378
1379dequeue_delayed:
1380 list_del_init(&xfer->list_node);
1381 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1382 xfer->result = urb->status;
1383 spin_unlock_irqrestore(&xfer->lock, flags);
1384 wa_xfer_giveback(xfer);
1385 usb_put_urb(urb); /* we got a ref in enqueue() */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001386 return 0;
1387}
1388EXPORT_SYMBOL_GPL(wa_urb_dequeue);
1389
1390/*
1391 * Translation from WA status codes (WUSB1.0 Table 8.15) to errno
1392 * codes
1393 *
1394 * Positive errno values are internal inconsistencies and should be
1395 * flagged louder. Negative are to be passed up to the user in the
1396 * normal way.
1397 *
1398 * @status: USB WA status code -- high two bits are stripped.
1399 */
1400static int wa_xfer_status_to_errno(u8 status)
1401{
1402 int errno;
1403 u8 real_status = status;
1404 static int xlat[] = {
1405 [WA_XFER_STATUS_SUCCESS] = 0,
1406 [WA_XFER_STATUS_HALTED] = -EPIPE,
1407 [WA_XFER_STATUS_DATA_BUFFER_ERROR] = -ENOBUFS,
1408 [WA_XFER_STATUS_BABBLE] = -EOVERFLOW,
1409 [WA_XFER_RESERVED] = EINVAL,
1410 [WA_XFER_STATUS_NOT_FOUND] = 0,
1411 [WA_XFER_STATUS_INSUFFICIENT_RESOURCE] = -ENOMEM,
1412 [WA_XFER_STATUS_TRANSACTION_ERROR] = -EILSEQ,
1413 [WA_XFER_STATUS_ABORTED] = -EINTR,
1414 [WA_XFER_STATUS_RPIPE_NOT_READY] = EINVAL,
1415 [WA_XFER_INVALID_FORMAT] = EINVAL,
1416 [WA_XFER_UNEXPECTED_SEGMENT_NUMBER] = EINVAL,
1417 [WA_XFER_STATUS_RPIPE_TYPE_MISMATCH] = EINVAL,
1418 };
1419 status &= 0x3f;
1420
1421 if (status == 0)
1422 return 0;
1423 if (status >= ARRAY_SIZE(xlat)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02001424 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001425 "Unknown WA transfer status 0x%02x\n",
1426 __func__, real_status);
1427 return -EINVAL;
1428 }
1429 errno = xlat[status];
1430 if (unlikely(errno > 0)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02001431 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001432 "Inconsistent WA status: 0x%02x\n",
1433 __func__, real_status);
1434 errno = -errno;
1435 }
1436 return errno;
1437}
1438
1439/*
1440 * Process a xfer result completion message
1441 *
1442 * inbound transfers: need to schedule a DTI read
1443 *
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001444 * FIXME: this function needs to be broken up in parts
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001445 */
Thomas Pugliese0367eef2013-09-26 10:49:41 -05001446static void wa_xfer_result_chew(struct wahc *wa, struct wa_xfer *xfer,
1447 struct wa_xfer_result *xfer_result)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001448{
1449 int result;
1450 struct device *dev = &wa->usb_iface->dev;
1451 unsigned long flags;
1452 u8 seg_idx;
1453 struct wa_seg *seg;
1454 struct wa_rpipe *rpipe;
Thomas Pugliese0367eef2013-09-26 10:49:41 -05001455 unsigned done = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001456 u8 usb_status;
1457 unsigned rpipe_ready = 0;
1458
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001459 spin_lock_irqsave(&xfer->lock, flags);
1460 seg_idx = xfer_result->bTransferSegment & 0x7f;
1461 if (unlikely(seg_idx >= xfer->segs))
1462 goto error_bad_seg;
1463 seg = xfer->seg[seg_idx];
1464 rpipe = xfer->ep->hcpriv;
1465 usb_status = xfer_result->bTransferStatus;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001466 dev_dbg(dev, "xfer %p#%u: bTransferStatus 0x%02x (seg status %u)\n",
David Vrabelbce83692008-12-22 18:22:50 +00001467 xfer, seg_idx, usb_status, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001468 if (seg->status == WA_SEG_ABORTED
1469 || seg->status == WA_SEG_ERROR) /* already handled */
1470 goto segment_aborted;
1471 if (seg->status == WA_SEG_SUBMITTED) /* ops, got here */
1472 seg->status = WA_SEG_PENDING; /* before wa_seg{_dto}_cb() */
1473 if (seg->status != WA_SEG_PENDING) {
1474 if (printk_ratelimit())
1475 dev_err(dev, "xfer %p#%u: Bad segment state %u\n",
1476 xfer, seg_idx, seg->status);
1477 seg->status = WA_SEG_PENDING; /* workaround/"fix" it */
1478 }
1479 if (usb_status & 0x80) {
1480 seg->result = wa_xfer_status_to_errno(usb_status);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001481 dev_err(dev, "DTI: xfer %p#:%08X:%u failed (0x%02x)\n",
1482 xfer, xfer->id, seg->index, usb_status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001483 goto error_complete;
1484 }
1485 /* FIXME: we ignore warnings, tally them for stats */
1486 if (usb_status & 0x40) /* Warning?... */
1487 usb_status = 0; /* ... pass */
1488 if (xfer->is_inbound) { /* IN data phase: read to buffer */
1489 seg->status = WA_SEG_DTI_PENDING;
1490 BUG_ON(wa->buf_in_urb->status == -EINPROGRESS);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001491 /* this should always be 0 before a resubmit. */
1492 wa->buf_in_urb->num_mapped_sgs = 0;
1493
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001494 if (xfer->is_dma) {
1495 wa->buf_in_urb->transfer_dma =
1496 xfer->urb->transfer_dma
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001497 + (seg_idx * xfer->seg_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001498 wa->buf_in_urb->transfer_flags
1499 |= URB_NO_TRANSFER_DMA_MAP;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001500 wa->buf_in_urb->transfer_buffer = NULL;
1501 wa->buf_in_urb->sg = NULL;
1502 wa->buf_in_urb->num_sgs = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001503 } else {
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001504 /* do buffer or SG processing. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001505 wa->buf_in_urb->transfer_flags
1506 &= ~URB_NO_TRANSFER_DMA_MAP;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001507
1508 if (xfer->urb->transfer_buffer) {
1509 wa->buf_in_urb->transfer_buffer =
1510 xfer->urb->transfer_buffer
1511 + (seg_idx * xfer->seg_size);
1512 wa->buf_in_urb->sg = NULL;
1513 wa->buf_in_urb->num_sgs = 0;
1514 } else {
1515 /* allocate an SG list to store seg_size bytes
1516 and copy the subset of the xfer->urb->sg
1517 that matches the buffer subset we are
1518 about to read. */
1519 wa->buf_in_urb->sg = wa_xfer_create_subset_sg(
1520 xfer->urb->sg,
1521 seg_idx * xfer->seg_size,
1522 le32_to_cpu(
1523 xfer_result->dwTransferLength),
1524 &(wa->buf_in_urb->num_sgs));
1525
1526 if (!(wa->buf_in_urb->sg)) {
1527 wa->buf_in_urb->num_sgs = 0;
1528 goto error_sg_alloc;
1529 }
1530 wa->buf_in_urb->transfer_buffer = NULL;
1531 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001532 }
1533 wa->buf_in_urb->transfer_buffer_length =
1534 le32_to_cpu(xfer_result->dwTransferLength);
1535 wa->buf_in_urb->context = seg;
1536 result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
1537 if (result < 0)
1538 goto error_submit_buf_in;
1539 } else {
1540 /* OUT data phase, complete it -- */
1541 seg->status = WA_SEG_DONE;
1542 seg->result = le32_to_cpu(xfer_result->dwTransferLength);
1543 xfer->segs_done++;
1544 rpipe_ready = rpipe_avail_inc(rpipe);
1545 done = __wa_xfer_is_done(xfer);
1546 }
1547 spin_unlock_irqrestore(&xfer->lock, flags);
1548 if (done)
1549 wa_xfer_completion(xfer);
1550 if (rpipe_ready)
1551 wa_xfer_delayed_run(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001552 return;
1553
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001554error_submit_buf_in:
1555 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
1556 dev_err(dev, "DTI: URB max acceptable errors "
1557 "exceeded, resetting device\n");
1558 wa_reset_all(wa);
1559 }
1560 if (printk_ratelimit())
1561 dev_err(dev, "xfer %p#%u: can't submit DTI data phase: %d\n",
1562 xfer, seg_idx, result);
1563 seg->result = result;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001564 kfree(wa->buf_in_urb->sg);
Thomas Pugliese67414482013-09-26 14:08:16 -05001565 wa->buf_in_urb->sg = NULL;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001566error_sg_alloc:
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001567 __wa_xfer_abort(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001568error_complete:
1569 seg->status = WA_SEG_ERROR;
1570 xfer->segs_done++;
1571 rpipe_ready = rpipe_avail_inc(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001572 done = __wa_xfer_is_done(xfer);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001573 /*
1574 * queue work item to clear STALL for control endpoints.
1575 * Otherwise, let endpoint_reset take care of it.
1576 */
1577 if (((usb_status & 0x3f) == WA_XFER_STATUS_HALTED) &&
1578 usb_endpoint_xfer_control(&xfer->ep->desc) &&
1579 done) {
1580
1581 dev_info(dev, "Control EP stall. Queue delayed work.\n");
1582 spin_lock_irq(&wa->xfer_list_lock);
Wei Yongjun8eb41292013-09-23 14:16:22 +08001583 /* move xfer from xfer_list to xfer_errored_list. */
1584 list_move_tail(&xfer->list_node, &wa->xfer_errored_list);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001585 spin_unlock_irq(&wa->xfer_list_lock);
1586 spin_unlock_irqrestore(&xfer->lock, flags);
1587 queue_work(wusbd, &wa->xfer_error_work);
1588 } else {
1589 spin_unlock_irqrestore(&xfer->lock, flags);
1590 if (done)
1591 wa_xfer_completion(xfer);
1592 if (rpipe_ready)
1593 wa_xfer_delayed_run(rpipe);
1594 }
1595
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001596 return;
1597
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001598error_bad_seg:
1599 spin_unlock_irqrestore(&xfer->lock, flags);
1600 wa_urb_dequeue(wa, xfer->urb);
1601 if (printk_ratelimit())
1602 dev_err(dev, "xfer %p#%u: bad segment\n", xfer, seg_idx);
1603 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
1604 dev_err(dev, "DTI: URB max acceptable errors "
1605 "exceeded, resetting device\n");
1606 wa_reset_all(wa);
1607 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001608 return;
1609
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001610segment_aborted:
1611 /* nothing to do, as the aborter did the completion */
1612 spin_unlock_irqrestore(&xfer->lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001613}
1614
1615/*
1616 * Callback for the IN data phase
1617 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001618 * If successful transition state; otherwise, take a note of the
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001619 * error, mark this segment done and try completion.
1620 *
1621 * Note we don't access until we are sure that the transfer hasn't
1622 * been cancelled (ECONNRESET, ENOENT), which could mean that
1623 * seg->xfer could be already gone.
1624 */
1625static void wa_buf_in_cb(struct urb *urb)
1626{
1627 struct wa_seg *seg = urb->context;
1628 struct wa_xfer *xfer = seg->xfer;
1629 struct wahc *wa;
1630 struct device *dev;
1631 struct wa_rpipe *rpipe;
1632 unsigned rpipe_ready;
1633 unsigned long flags;
1634 u8 done = 0;
1635
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001636 /* free the sg if it was used. */
1637 kfree(urb->sg);
1638 urb->sg = NULL;
1639
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001640 switch (urb->status) {
1641 case 0:
1642 spin_lock_irqsave(&xfer->lock, flags);
1643 wa = xfer->wa;
1644 dev = &wa->usb_iface->dev;
1645 rpipe = xfer->ep->hcpriv;
David Vrabelbce83692008-12-22 18:22:50 +00001646 dev_dbg(dev, "xfer %p#%u: data in done (%zu bytes)\n",
1647 xfer, seg->index, (size_t)urb->actual_length);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001648 seg->status = WA_SEG_DONE;
1649 seg->result = urb->actual_length;
1650 xfer->segs_done++;
1651 rpipe_ready = rpipe_avail_inc(rpipe);
1652 done = __wa_xfer_is_done(xfer);
1653 spin_unlock_irqrestore(&xfer->lock, flags);
1654 if (done)
1655 wa_xfer_completion(xfer);
1656 if (rpipe_ready)
1657 wa_xfer_delayed_run(rpipe);
1658 break;
1659 case -ECONNRESET: /* URB unlinked; no need to do anything */
1660 case -ENOENT: /* as it was done by the who unlinked us */
1661 break;
1662 default: /* Other errors ... */
1663 spin_lock_irqsave(&xfer->lock, flags);
1664 wa = xfer->wa;
1665 dev = &wa->usb_iface->dev;
1666 rpipe = xfer->ep->hcpriv;
1667 if (printk_ratelimit())
1668 dev_err(dev, "xfer %p#%u: data in error %d\n",
1669 xfer, seg->index, urb->status);
1670 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
1671 EDC_ERROR_TIMEFRAME)){
1672 dev_err(dev, "DTO: URB max acceptable errors "
1673 "exceeded, resetting device\n");
1674 wa_reset_all(wa);
1675 }
1676 seg->status = WA_SEG_ERROR;
1677 seg->result = urb->status;
1678 xfer->segs_done++;
1679 rpipe_ready = rpipe_avail_inc(rpipe);
1680 __wa_xfer_abort(xfer);
1681 done = __wa_xfer_is_done(xfer);
1682 spin_unlock_irqrestore(&xfer->lock, flags);
1683 if (done)
1684 wa_xfer_completion(xfer);
1685 if (rpipe_ready)
1686 wa_xfer_delayed_run(rpipe);
1687 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001688}
1689
1690/*
1691 * Handle an incoming transfer result buffer
1692 *
1693 * Given a transfer result buffer, it completes the transfer (possibly
1694 * scheduling and buffer in read) and then resubmits the DTI URB for a
1695 * new transfer result read.
1696 *
1697 *
1698 * The xfer_result DTI URB state machine
1699 *
1700 * States: OFF | RXR (Read-Xfer-Result) | RBI (Read-Buffer-In)
1701 *
1702 * We start in OFF mode, the first xfer_result notification [through
1703 * wa_handle_notif_xfer()] moves us to RXR by posting the DTI-URB to
1704 * read.
1705 *
1706 * We receive a buffer -- if it is not a xfer_result, we complain and
1707 * repost the DTI-URB. If it is a xfer_result then do the xfer seg
1708 * request accounting. If it is an IN segment, we move to RBI and post
1709 * a BUF-IN-URB to the right buffer. The BUF-IN-URB callback will
1710 * repost the DTI-URB and move to RXR state. if there was no IN
1711 * segment, it will repost the DTI-URB.
1712 *
1713 * We go back to OFF when we detect a ENOENT or ESHUTDOWN (or too many
1714 * errors) in the URBs.
1715 */
Thomas Pugliese0367eef2013-09-26 10:49:41 -05001716static void wa_dti_cb(struct urb *urb)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001717{
1718 int result;
1719 struct wahc *wa = urb->context;
1720 struct device *dev = &wa->usb_iface->dev;
1721 struct wa_xfer_result *xfer_result;
1722 u32 xfer_id;
1723 struct wa_xfer *xfer;
1724 u8 usb_status;
1725
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001726 BUG_ON(wa->dti_urb != urb);
1727 switch (wa->dti_urb->status) {
1728 case 0:
1729 /* We have a xfer result buffer; check it */
David Vrabelbce83692008-12-22 18:22:50 +00001730 dev_dbg(dev, "DTI: xfer result %d bytes at %p\n",
1731 urb->actual_length, urb->transfer_buffer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001732 if (wa->dti_urb->actual_length != sizeof(*xfer_result)) {
1733 dev_err(dev, "DTI Error: xfer result--bad size "
1734 "xfer result (%d bytes vs %zu needed)\n",
1735 urb->actual_length, sizeof(*xfer_result));
1736 break;
1737 }
Thomas Pugliese0367eef2013-09-26 10:49:41 -05001738 xfer_result = (struct wa_xfer_result *)(wa->dti_buf);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001739 if (xfer_result->hdr.bLength != sizeof(*xfer_result)) {
1740 dev_err(dev, "DTI Error: xfer result--"
1741 "bad header length %u\n",
1742 xfer_result->hdr.bLength);
1743 break;
1744 }
1745 if (xfer_result->hdr.bNotifyType != WA_XFER_RESULT) {
1746 dev_err(dev, "DTI Error: xfer result--"
1747 "bad header type 0x%02x\n",
1748 xfer_result->hdr.bNotifyType);
1749 break;
1750 }
1751 usb_status = xfer_result->bTransferStatus & 0x3f;
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001752 if (usb_status == WA_XFER_STATUS_NOT_FOUND)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001753 /* taken care of already */
1754 break;
Thomas Pugliesefdd160c2013-09-27 15:33:35 -05001755 xfer_id = le32_to_cpu(xfer_result->dwTransferID);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001756 xfer = wa_xfer_get_by_id(wa, xfer_id);
1757 if (xfer == NULL) {
1758 /* FIXME: transaction might have been cancelled */
1759 dev_err(dev, "DTI Error: xfer result--"
1760 "unknown xfer 0x%08x (status 0x%02x)\n",
1761 xfer_id, usb_status);
1762 break;
1763 }
Thomas Pugliese0367eef2013-09-26 10:49:41 -05001764 wa_xfer_result_chew(wa, xfer, xfer_result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001765 wa_xfer_put(xfer);
1766 break;
1767 case -ENOENT: /* (we killed the URB)...so, no broadcast */
1768 case -ESHUTDOWN: /* going away! */
1769 dev_dbg(dev, "DTI: going down! %d\n", urb->status);
1770 goto out;
1771 default:
1772 /* Unknown error */
1773 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS,
1774 EDC_ERROR_TIMEFRAME)) {
1775 dev_err(dev, "DTI: URB max acceptable errors "
1776 "exceeded, resetting device\n");
1777 wa_reset_all(wa);
1778 goto out;
1779 }
1780 if (printk_ratelimit())
1781 dev_err(dev, "DTI: URB error %d\n", urb->status);
1782 break;
1783 }
1784 /* Resubmit the DTI URB */
1785 result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
1786 if (result < 0) {
1787 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
1788 "resetting\n", result);
1789 wa_reset_all(wa);
1790 }
1791out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001792 return;
1793}
1794
1795/*
1796 * Transfer complete notification
1797 *
1798 * Called from the notif.c code. We get a notification on EP2 saying
1799 * that some endpoint has some transfer result data available. We are
1800 * about to read it.
1801 *
1802 * To speed up things, we always have a URB reading the DTI URB; we
1803 * don't really set it up and start it until the first xfer complete
1804 * notification arrives, which is what we do here.
1805 *
Thomas Pugliese0367eef2013-09-26 10:49:41 -05001806 * Follow up in wa_dti_cb(), as that's where the whole state
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001807 * machine starts.
1808 *
1809 * So here we just initialize the DTI URB for reading transfer result
1810 * notifications and also the buffer-in URB, for reading buffers. Then
1811 * we just submit the DTI URB.
1812 *
1813 * @wa shall be referenced
1814 */
1815void wa_handle_notif_xfer(struct wahc *wa, struct wa_notif_hdr *notif_hdr)
1816{
1817 int result;
1818 struct device *dev = &wa->usb_iface->dev;
1819 struct wa_notif_xfer *notif_xfer;
1820 const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd;
1821
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001822 notif_xfer = container_of(notif_hdr, struct wa_notif_xfer, hdr);
1823 BUG_ON(notif_hdr->bNotifyType != WA_NOTIF_TRANSFER);
1824
1825 if ((0x80 | notif_xfer->bEndpoint) != dti_epd->bEndpointAddress) {
1826 /* FIXME: hardcoded limitation, adapt */
1827 dev_err(dev, "BUG: DTI ep is %u, not %u (hack me)\n",
1828 notif_xfer->bEndpoint, dti_epd->bEndpointAddress);
1829 goto error;
1830 }
1831 if (wa->dti_urb != NULL) /* DTI URB already started */
1832 goto out;
1833
1834 wa->dti_urb = usb_alloc_urb(0, GFP_KERNEL);
1835 if (wa->dti_urb == NULL) {
1836 dev_err(dev, "Can't allocate DTI URB\n");
1837 goto error_dti_urb_alloc;
1838 }
1839 usb_fill_bulk_urb(
1840 wa->dti_urb, wa->usb_dev,
1841 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
Thomas Pugliese0367eef2013-09-26 10:49:41 -05001842 wa->dti_buf, wa->dti_buf_size,
1843 wa_dti_cb, wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001844
1845 wa->buf_in_urb = usb_alloc_urb(0, GFP_KERNEL);
1846 if (wa->buf_in_urb == NULL) {
1847 dev_err(dev, "Can't allocate BUF-IN URB\n");
1848 goto error_buf_in_urb_alloc;
1849 }
1850 usb_fill_bulk_urb(
1851 wa->buf_in_urb, wa->usb_dev,
1852 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
1853 NULL, 0, wa_buf_in_cb, wa);
1854 result = usb_submit_urb(wa->dti_urb, GFP_KERNEL);
1855 if (result < 0) {
1856 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
1857 "resetting\n", result);
1858 goto error_dti_urb_submit;
1859 }
1860out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001861 return;
1862
1863error_dti_urb_submit:
1864 usb_put_urb(wa->buf_in_urb);
Thomas Pugliese67414482013-09-26 14:08:16 -05001865 wa->buf_in_urb = NULL;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001866error_buf_in_urb_alloc:
1867 usb_put_urb(wa->dti_urb);
1868 wa->dti_urb = NULL;
1869error_dti_urb_alloc:
1870error:
1871 wa_reset_all(wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001872}