blob: 2afa886148884e792cbbceb429f73c2caf2387a1 [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 {
Thomas Pugliesef74b75e2013-10-23 14:44:29 -050094 /* [WUSB] section 8.3.3 allocates 7 bits for the segment index. */
95 WA_SEGS_MAX = 128,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010096};
97
98enum wa_seg_status {
99 WA_SEG_NOTREADY,
100 WA_SEG_READY,
101 WA_SEG_DELAYED,
102 WA_SEG_SUBMITTED,
103 WA_SEG_PENDING,
104 WA_SEG_DTI_PENDING,
105 WA_SEG_DONE,
106 WA_SEG_ERROR,
107 WA_SEG_ABORTED,
108};
109
110static void wa_xfer_delayed_run(struct wa_rpipe *);
Thomas Pugliese679ee472013-10-07 10:53:57 -0500111static int __wa_xfer_delayed_run(struct wa_rpipe *rpipe, int *dto_waiting);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100112
113/*
114 * Life cycle governed by 'struct urb' (the refcount of the struct is
115 * that of the 'struct urb' and usb_free_urb() would free the whole
116 * struct).
117 */
118struct wa_seg {
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500119 struct urb tr_urb; /* transfer request urb. */
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500120 struct urb *isoc_pack_desc_urb; /* for isoc packet descriptor. */
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500121 struct urb *dto_urb; /* for data output. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100122 struct list_head list_node; /* for rpipe->req_list */
123 struct wa_xfer *xfer; /* out xfer */
124 u8 index; /* which segment we are */
Thomas Pugliese21012422013-10-23 14:44:27 -0500125 int isoc_frame_count; /* number of isoc frames in this segment. */
126 int isoc_frame_offset; /* starting frame offset in the xfer URB. */
Thomas Puglieseea1af422013-12-09 14:15:14 -0600127 /* Isoc frame that the current transfer buffer corresponds to. */
128 int isoc_frame_index;
Thomas Pugliese21012422013-10-23 14:44:27 -0500129 int isoc_size; /* size of all isoc frames sent by this seg. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100130 enum wa_seg_status status;
131 ssize_t result; /* bytes xfered or error */
132 struct wa_xfer_hdr xfer_hdr;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100133};
134
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500135static inline void wa_seg_init(struct wa_seg *seg)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100136{
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500137 usb_init_urb(&seg->tr_urb);
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500138
139 /* set the remaining memory to 0. */
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500140 memset(((void *)seg) + sizeof(seg->tr_urb), 0,
141 sizeof(*seg) - sizeof(seg->tr_urb));
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100142}
143
144/*
145 * Protected by xfer->lock
146 *
147 */
148struct wa_xfer {
149 struct kref refcnt;
150 struct list_head list_node;
151 spinlock_t lock;
152 u32 id;
153
154 struct wahc *wa; /* Wire adapter we are plugged to */
155 struct usb_host_endpoint *ep;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300156 struct urb *urb; /* URB we are transferring for */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100157 struct wa_seg **seg; /* transfer segments */
158 u8 segs, segs_submitted, segs_done;
159 unsigned is_inbound:1;
160 unsigned is_dma:1;
161 size_t seg_size;
162 int result;
163
164 gfp_t gfp; /* allocation mask */
165
166 struct wusb_dev *wusb_dev; /* for activity timestamps */
167};
168
Thomas Pugliese21012422013-10-23 14:44:27 -0500169static void __wa_populate_dto_urb_isoc(struct wa_xfer *xfer,
170 struct wa_seg *seg, int curr_iso_frame);
171
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100172static inline void wa_xfer_init(struct wa_xfer *xfer)
173{
174 kref_init(&xfer->refcnt);
175 INIT_LIST_HEAD(&xfer->list_node);
176 spin_lock_init(&xfer->lock);
177}
178
179/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300180 * Destroy a transfer structure
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100181 *
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500182 * Note that freeing xfer->seg[cnt]->tr_urb will free the containing
Thomas Pugliese79731cb2013-08-15 14:37:42 -0500183 * xfer->seg[cnt] memory that was allocated by __wa_xfer_setup_segs.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100184 */
185static void wa_xfer_destroy(struct kref *_xfer)
186{
187 struct wa_xfer *xfer = container_of(_xfer, struct wa_xfer, refcnt);
188 if (xfer->seg) {
189 unsigned cnt;
190 for (cnt = 0; cnt < xfer->segs; cnt++) {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500191 struct wa_seg *seg = xfer->seg[cnt];
192 if (seg) {
193 usb_free_urb(seg->isoc_pack_desc_urb);
194 if (seg->dto_urb) {
195 kfree(seg->dto_urb->sg);
196 usb_free_urb(seg->dto_urb);
Thomas Pugliesed9936702013-09-26 14:08:13 -0500197 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500198 usb_free_urb(&seg->tr_urb);
Thomas Pugliesed9936702013-09-26 14:08:13 -0500199 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100200 }
Thomas Pugliesed9936702013-09-26 14:08:13 -0500201 kfree(xfer->seg);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100202 }
203 kfree(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100204}
205
206static void wa_xfer_get(struct wa_xfer *xfer)
207{
208 kref_get(&xfer->refcnt);
209}
210
211static void wa_xfer_put(struct wa_xfer *xfer)
212{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100213 kref_put(&xfer->refcnt, wa_xfer_destroy);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100214}
215
216/*
Thomas Pugliese679ee472013-10-07 10:53:57 -0500217 * Try to get exclusive access to the DTO endpoint resource. Return true
218 * if successful.
219 */
220static inline int __wa_dto_try_get(struct wahc *wa)
221{
222 return (test_and_set_bit(0, &wa->dto_in_use) == 0);
223}
224
225/* Release the DTO endpoint resource. */
226static inline void __wa_dto_put(struct wahc *wa)
227{
228 clear_bit_unlock(0, &wa->dto_in_use);
229}
230
231/* Service RPIPEs that are waiting on the DTO resource. */
232static void wa_check_for_delayed_rpipes(struct wahc *wa)
233{
234 unsigned long flags;
235 int dto_waiting = 0;
236 struct wa_rpipe *rpipe;
237
238 spin_lock_irqsave(&wa->rpipe_lock, flags);
239 while (!list_empty(&wa->rpipe_delayed_list) && !dto_waiting) {
240 rpipe = list_first_entry(&wa->rpipe_delayed_list,
241 struct wa_rpipe, list_node);
242 __wa_xfer_delayed_run(rpipe, &dto_waiting);
243 /* remove this RPIPE from the list if it is not waiting. */
244 if (!dto_waiting) {
245 pr_debug("%s: RPIPE %d serviced and removed from delayed list.\n",
246 __func__,
247 le16_to_cpu(rpipe->descr.wRPipeIndex));
248 list_del_init(&rpipe->list_node);
249 }
250 }
251 spin_unlock_irqrestore(&wa->rpipe_lock, flags);
252}
253
254/* add this RPIPE to the end of the delayed RPIPE list. */
255static void wa_add_delayed_rpipe(struct wahc *wa, struct wa_rpipe *rpipe)
256{
257 unsigned long flags;
258
259 spin_lock_irqsave(&wa->rpipe_lock, flags);
260 /* add rpipe to the list if it is not already on it. */
261 if (list_empty(&rpipe->list_node)) {
262 pr_debug("%s: adding RPIPE %d to the delayed list.\n",
263 __func__, le16_to_cpu(rpipe->descr.wRPipeIndex));
264 list_add_tail(&rpipe->list_node, &wa->rpipe_delayed_list);
265 }
266 spin_unlock_irqrestore(&wa->rpipe_lock, flags);
267}
268
269/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100270 * xfer is referenced
271 *
272 * xfer->lock has to be unlocked
273 *
274 * We take xfer->lock for setting the result; this is a barrier
275 * against drivers/usb/core/hcd.c:unlink1() being called after we call
276 * usb_hcd_giveback_urb() and wa_urb_dequeue() trying to get a
277 * reference to the transfer.
278 */
279static void wa_xfer_giveback(struct wa_xfer *xfer)
280{
281 unsigned long flags;
David Vrabelbce83692008-12-22 18:22:50 +0000282
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100283 spin_lock_irqsave(&xfer->wa->xfer_list_lock, flags);
284 list_del_init(&xfer->list_node);
Thomas Puglieseb3744872013-11-25 16:17:16 -0600285 usb_hcd_unlink_urb_from_ep(&(xfer->wa->wusb->usb_hcd), xfer->urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100286 spin_unlock_irqrestore(&xfer->wa->xfer_list_lock, flags);
287 /* FIXME: segmentation broken -- kills DWA */
288 wusbhc_giveback_urb(xfer->wa->wusb, xfer->urb, xfer->result);
289 wa_put(xfer->wa);
290 wa_xfer_put(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100291}
292
293/*
294 * xfer is referenced
295 *
296 * xfer->lock has to be unlocked
297 */
298static void wa_xfer_completion(struct wa_xfer *xfer)
299{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100300 if (xfer->wusb_dev)
301 wusb_dev_put(xfer->wusb_dev);
302 rpipe_put(xfer->ep->hcpriv);
303 wa_xfer_giveback(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100304}
305
306/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100307 * Initialize a transfer's ID
308 *
309 * We need to use a sequential number; if we use the pointer or the
310 * hash of the pointer, it can repeat over sequential transfers and
311 * then it will confuse the HWA....wonder why in hell they put a 32
312 * bit handle in there then.
313 */
314static void wa_xfer_id_init(struct wa_xfer *xfer)
315{
316 xfer->id = atomic_add_return(1, &xfer->wa->xfer_id_count);
317}
318
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500319/* Return the xfer's ID. */
320static inline u32 wa_xfer_id(struct wa_xfer *xfer)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100321{
322 return xfer->id;
323}
324
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500325/* Return the xfer's ID in transport format (little endian). */
326static inline __le32 wa_xfer_id_le32(struct wa_xfer *xfer)
327{
328 return cpu_to_le32(xfer->id);
329}
330
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100331/*
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500332 * If transfer is done, wrap it up and return true
333 *
334 * xfer->lock has to be locked
335 */
336static unsigned __wa_xfer_is_done(struct wa_xfer *xfer)
337{
338 struct device *dev = &xfer->wa->usb_iface->dev;
339 unsigned result, cnt;
340 struct wa_seg *seg;
341 struct urb *urb = xfer->urb;
342 unsigned found_short = 0;
343
344 result = xfer->segs_done == xfer->segs_submitted;
345 if (result == 0)
346 goto out;
347 urb->actual_length = 0;
348 for (cnt = 0; cnt < xfer->segs; cnt++) {
349 seg = xfer->seg[cnt];
350 switch (seg->status) {
351 case WA_SEG_DONE:
352 if (found_short && seg->result > 0) {
353 dev_dbg(dev, "xfer %p ID %08X#%u: bad short segments (%zu)\n",
354 xfer, wa_xfer_id(xfer), cnt,
355 seg->result);
356 urb->status = -EINVAL;
357 goto out;
358 }
359 urb->actual_length += seg->result;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500360 if (!(usb_pipeisoc(xfer->urb->pipe))
361 && seg->result < xfer->seg_size
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500362 && cnt != xfer->segs-1)
363 found_short = 1;
364 dev_dbg(dev, "xfer %p ID %08X#%u: DONE short %d "
365 "result %zu urb->actual_length %d\n",
366 xfer, wa_xfer_id(xfer), seg->index, found_short,
367 seg->result, urb->actual_length);
368 break;
369 case WA_SEG_ERROR:
370 xfer->result = seg->result;
Thomas Pugliesecccd3a252013-09-30 22:48:46 -0500371 dev_dbg(dev, "xfer %p ID %08X#%u: ERROR result %zu(0x%08zX)\n",
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500372 xfer, wa_xfer_id(xfer), seg->index, seg->result,
373 seg->result);
374 goto out;
375 case WA_SEG_ABORTED:
Thomas Pugliesebbfc34202013-11-25 16:17:17 -0600376 xfer->result = seg->result;
377 dev_dbg(dev, "xfer %p ID %08X#%u: ABORTED result %zu(0x%08zX)\n",
378 xfer, wa_xfer_id(xfer), seg->index, seg->result,
379 seg->result);
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500380 goto out;
381 default:
382 dev_warn(dev, "xfer %p ID %08X#%u: is_done bad state %d\n",
383 xfer, wa_xfer_id(xfer), cnt, seg->status);
384 xfer->result = -EINVAL;
385 goto out;
386 }
387 }
388 xfer->result = 0;
389out:
390 return result;
391}
392
393/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100394 * Search for a transfer list ID on the HCD's URB list
395 *
396 * For 32 bit architectures, we use the pointer itself; for 64 bits, a
397 * 32-bit hash of the pointer.
398 *
399 * @returns NULL if not found.
400 */
401static struct wa_xfer *wa_xfer_get_by_id(struct wahc *wa, u32 id)
402{
403 unsigned long flags;
404 struct wa_xfer *xfer_itr;
405 spin_lock_irqsave(&wa->xfer_list_lock, flags);
406 list_for_each_entry(xfer_itr, &wa->xfer_list, list_node) {
407 if (id == xfer_itr->id) {
408 wa_xfer_get(xfer_itr);
409 goto out;
410 }
411 }
412 xfer_itr = NULL;
413out:
414 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
415 return xfer_itr;
416}
417
418struct wa_xfer_abort_buffer {
419 struct urb urb;
420 struct wa_xfer_abort cmd;
421};
422
423static void __wa_xfer_abort_cb(struct urb *urb)
424{
425 struct wa_xfer_abort_buffer *b = urb->context;
426 usb_put_urb(&b->urb);
427}
428
429/*
430 * Aborts an ongoing transaction
431 *
432 * Assumes the transfer is referenced and locked and in a submitted
433 * state (mainly that there is an endpoint/rpipe assigned).
434 *
435 * The callback (see above) does nothing but freeing up the data by
436 * putting the URB. Because the URB is allocated at the head of the
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -0500437 * struct, the whole space we allocated is kfreed. *
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100438 */
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -0500439static int __wa_xfer_abort(struct wa_xfer *xfer)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100440{
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -0500441 int result = -ENOMEM;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100442 struct device *dev = &xfer->wa->usb_iface->dev;
443 struct wa_xfer_abort_buffer *b;
444 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
445
446 b = kmalloc(sizeof(*b), GFP_ATOMIC);
447 if (b == NULL)
448 goto error_kmalloc;
449 b->cmd.bLength = sizeof(b->cmd);
450 b->cmd.bRequestType = WA_XFER_ABORT;
451 b->cmd.wRPipe = rpipe->descr.wRPipeIndex;
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500452 b->cmd.dwTransferID = wa_xfer_id_le32(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100453
454 usb_init_urb(&b->urb);
455 usb_fill_bulk_urb(&b->urb, xfer->wa->usb_dev,
456 usb_sndbulkpipe(xfer->wa->usb_dev,
457 xfer->wa->dto_epd->bEndpointAddress),
458 &b->cmd, sizeof(b->cmd), __wa_xfer_abort_cb, b);
459 result = usb_submit_urb(&b->urb, GFP_ATOMIC);
460 if (result < 0)
461 goto error_submit;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -0500462 return result; /* callback frees! */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100463
464
465error_submit:
466 if (printk_ratelimit())
467 dev_err(dev, "xfer %p: Can't submit abort request: %d\n",
468 xfer, result);
469 kfree(b);
470error_kmalloc:
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -0500471 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100472
473}
474
475/*
Thomas Pugliese21012422013-10-23 14:44:27 -0500476 * Calculate the number of isoc frames starting from isoc_frame_offset
477 * that will fit a in transfer segment.
478 */
479static int __wa_seg_calculate_isoc_frame_count(struct wa_xfer *xfer,
480 int isoc_frame_offset, int *total_size)
481{
482 int segment_size = 0, frame_count = 0;
483 int index = isoc_frame_offset;
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500484 struct usb_iso_packet_descriptor *iso_frame_desc =
485 xfer->urb->iso_frame_desc;
Thomas Pugliese21012422013-10-23 14:44:27 -0500486
487 while ((index < xfer->urb->number_of_packets)
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500488 && ((segment_size + iso_frame_desc[index].length)
Thomas Pugliese21012422013-10-23 14:44:27 -0500489 <= xfer->seg_size)) {
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500490 /*
Thomas Pugliese226b3a22013-12-10 12:10:33 -0600491 * For Alereon HWA devices, only include an isoc frame in an
492 * out segment if it is physically contiguous with the previous
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500493 * frame. This is required because those devices expect
494 * the isoc frames to be sent as a single USB transaction as
495 * opposed to one transaction per frame with standard HWA.
496 */
497 if ((xfer->wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC)
Thomas Pugliese226b3a22013-12-10 12:10:33 -0600498 && (xfer->is_inbound == 0)
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500499 && (index > isoc_frame_offset)
500 && ((iso_frame_desc[index - 1].offset +
501 iso_frame_desc[index - 1].length) !=
502 iso_frame_desc[index].offset))
503 break;
504
Thomas Pugliese21012422013-10-23 14:44:27 -0500505 /* this frame fits. count it. */
506 ++frame_count;
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500507 segment_size += iso_frame_desc[index].length;
Thomas Pugliese21012422013-10-23 14:44:27 -0500508
509 /* move to the next isoc frame. */
510 ++index;
511 }
512
513 *total_size = segment_size;
514 return frame_count;
515}
516
517/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100518 *
519 * @returns < 0 on error, transfer segment request size if ok
520 */
521static ssize_t __wa_xfer_setup_sizes(struct wa_xfer *xfer,
522 enum wa_xfer_type *pxfer_type)
523{
524 ssize_t result;
525 struct device *dev = &xfer->wa->usb_iface->dev;
526 size_t maxpktsize;
527 struct urb *urb = xfer->urb;
528 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
529
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100530 switch (rpipe->descr.bmAttribute & 0x3) {
531 case USB_ENDPOINT_XFER_CONTROL:
532 *pxfer_type = WA_XFER_TYPE_CTL;
533 result = sizeof(struct wa_xfer_ctl);
534 break;
535 case USB_ENDPOINT_XFER_INT:
536 case USB_ENDPOINT_XFER_BULK:
537 *pxfer_type = WA_XFER_TYPE_BI;
538 result = sizeof(struct wa_xfer_bi);
539 break;
540 case USB_ENDPOINT_XFER_ISOC:
Thomas Pugliese226b3a22013-12-10 12:10:33 -0600541 *pxfer_type = WA_XFER_TYPE_ISO;
542 result = sizeof(struct wa_xfer_hwaiso);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500543 break;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100544 default:
545 /* never happens */
546 BUG();
547 result = -EINVAL; /* shut gcc up */
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500548 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100549 xfer->is_inbound = urb->pipe & USB_DIR_IN ? 1 : 0;
550 xfer->is_dma = urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? 1 : 0;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500551
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100552 maxpktsize = le16_to_cpu(rpipe->descr.wMaxPacketSize);
Thomas Pugliese226b3a22013-12-10 12:10:33 -0600553 xfer->seg_size = le16_to_cpu(rpipe->descr.wBlocks)
554 * 1 << (xfer->wa->wa_descr->bRPipeBlockSize - 1);
555 /* Compute the segment size and make sure it is a multiple of
556 * the maxpktsize (WUSB1.0[8.3.3.1])...not really too much of
557 * a check (FIXME) */
558 if (xfer->seg_size < maxpktsize) {
559 dev_err(dev,
560 "HW BUG? seg_size %zu smaller than maxpktsize %zu\n",
561 xfer->seg_size, maxpktsize);
562 result = -EINVAL;
563 goto error;
564 }
565 xfer->seg_size = (xfer->seg_size / maxpktsize) * maxpktsize;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500566 if ((rpipe->descr.bmAttribute & 0x3) == USB_ENDPOINT_XFER_ISOC) {
Thomas Pugliese21012422013-10-23 14:44:27 -0500567 int index = 0;
568
Thomas Pugliese21012422013-10-23 14:44:27 -0500569 xfer->segs = 0;
570 /*
571 * loop over urb->number_of_packets to determine how many
572 * xfer segments will be needed to send the isoc frames.
573 */
574 while (index < urb->number_of_packets) {
575 int seg_size; /* don't care. */
576 index += __wa_seg_calculate_isoc_frame_count(xfer,
577 index, &seg_size);
578 ++xfer->segs;
579 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500580 } else {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500581 xfer->segs = DIV_ROUND_UP(urb->transfer_buffer_length,
582 xfer->seg_size);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500583 if (xfer->segs == 0 && *pxfer_type == WA_XFER_TYPE_CTL)
584 xfer->segs = 1;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100585 }
Thomas Pugliese21012422013-10-23 14:44:27 -0500586
Thomas Pugliesef74b75e2013-10-23 14:44:29 -0500587 if (xfer->segs > WA_SEGS_MAX) {
Thomas Pugliese21012422013-10-23 14:44:27 -0500588 dev_err(dev, "BUG? oops, number of segments %zu bigger than %d\n",
589 (urb->transfer_buffer_length/xfer->seg_size),
590 WA_SEGS_MAX);
591 result = -EINVAL;
592 goto error;
593 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100594error:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100595 return result;
596}
597
Thomas Pugliese21012422013-10-23 14:44:27 -0500598static void __wa_setup_isoc_packet_descr(
599 struct wa_xfer_packet_info_hwaiso *packet_desc,
600 struct wa_xfer *xfer,
601 struct wa_seg *seg) {
602 struct usb_iso_packet_descriptor *iso_frame_desc =
603 xfer->urb->iso_frame_desc;
604 int frame_index;
605
606 /* populate isoc packet descriptor. */
607 packet_desc->bPacketType = WA_XFER_ISO_PACKET_INFO;
608 packet_desc->wLength = cpu_to_le16(sizeof(*packet_desc) +
609 (sizeof(packet_desc->PacketLength[0]) *
610 seg->isoc_frame_count));
611 for (frame_index = 0; frame_index < seg->isoc_frame_count;
612 ++frame_index) {
613 int offset_index = frame_index + seg->isoc_frame_offset;
614 packet_desc->PacketLength[frame_index] =
615 cpu_to_le16(iso_frame_desc[offset_index].length);
616 }
617}
618
619
David Vrabelbce83692008-12-22 18:22:50 +0000620/* Fill in the common request header and xfer-type specific data. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100621static void __wa_xfer_setup_hdr0(struct wa_xfer *xfer,
622 struct wa_xfer_hdr *xfer_hdr0,
623 enum wa_xfer_type xfer_type,
624 size_t xfer_hdr_size)
625{
626 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
Thomas Pugliese21012422013-10-23 14:44:27 -0500627 struct wa_seg *seg = xfer->seg[0];
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100628
Thomas Pugliese21012422013-10-23 14:44:27 -0500629 xfer_hdr0 = &seg->xfer_hdr;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100630 xfer_hdr0->bLength = xfer_hdr_size;
631 xfer_hdr0->bRequestType = xfer_type;
632 xfer_hdr0->wRPipe = rpipe->descr.wRPipeIndex;
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500633 xfer_hdr0->dwTransferID = wa_xfer_id_le32(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100634 xfer_hdr0->bTransferSegment = 0;
635 switch (xfer_type) {
636 case WA_XFER_TYPE_CTL: {
637 struct wa_xfer_ctl *xfer_ctl =
638 container_of(xfer_hdr0, struct wa_xfer_ctl, hdr);
639 xfer_ctl->bmAttribute = xfer->is_inbound ? 1 : 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100640 memcpy(&xfer_ctl->baSetupData, xfer->urb->setup_packet,
641 sizeof(xfer_ctl->baSetupData));
642 break;
643 }
644 case WA_XFER_TYPE_BI:
645 break;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500646 case WA_XFER_TYPE_ISO: {
647 struct wa_xfer_hwaiso *xfer_iso =
648 container_of(xfer_hdr0, struct wa_xfer_hwaiso, hdr);
649 struct wa_xfer_packet_info_hwaiso *packet_desc =
650 ((void *)xfer_iso) + xfer_hdr_size;
Thomas Pugliese21012422013-10-23 14:44:27 -0500651
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500652 /* populate the isoc section of the transfer request. */
Thomas Pugliese21012422013-10-23 14:44:27 -0500653 xfer_iso->dwNumOfPackets = cpu_to_le32(seg->isoc_frame_count);
654 /* populate isoc packet descriptor. */
655 __wa_setup_isoc_packet_descr(packet_desc, xfer, seg);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500656 break;
657 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100658 default:
659 BUG();
660 };
661}
662
663/*
664 * Callback for the OUT data phase of the segment request
665 *
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500666 * Check wa_seg_tr_cb(); most comments also apply here because this
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100667 * function does almost the same thing and they work closely
668 * together.
669 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300670 * If the seg request has failed but this DTO phase has succeeded,
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500671 * wa_seg_tr_cb() has already failed the segment and moved the
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100672 * status to WA_SEG_ERROR, so this will go through 'case 0' and
673 * effectively do nothing.
674 */
675static void wa_seg_dto_cb(struct urb *urb)
676{
677 struct wa_seg *seg = urb->context;
678 struct wa_xfer *xfer = seg->xfer;
679 struct wahc *wa;
680 struct device *dev;
681 struct wa_rpipe *rpipe;
682 unsigned long flags;
683 unsigned rpipe_ready = 0;
Thomas Pugliese21012422013-10-23 14:44:27 -0500684 int data_send_done = 1, release_dto = 0, holding_dto = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100685 u8 done = 0;
Thomas Pugliese21012422013-10-23 14:44:27 -0500686 int result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100687
Thomas Pugliesed5b5c9f2013-09-26 14:08:15 -0500688 /* free the sg if it was used. */
689 kfree(urb->sg);
690 urb->sg = NULL;
691
Thomas Pugliese21012422013-10-23 14:44:27 -0500692 spin_lock_irqsave(&xfer->lock, flags);
693 wa = xfer->wa;
694 dev = &wa->usb_iface->dev;
695 if (usb_pipeisoc(xfer->urb->pipe)) {
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500696 /* Alereon HWA sends all isoc frames in a single transfer. */
697 if (wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC)
Thomas Puglieseea1af422013-12-09 14:15:14 -0600698 seg->isoc_frame_index += seg->isoc_frame_count;
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500699 else
Thomas Puglieseea1af422013-12-09 14:15:14 -0600700 seg->isoc_frame_index += 1;
701 if (seg->isoc_frame_index < seg->isoc_frame_count) {
Thomas Pugliese21012422013-10-23 14:44:27 -0500702 data_send_done = 0;
703 holding_dto = 1; /* checked in error cases. */
704 /*
705 * if this is the last isoc frame of the segment, we
706 * can release DTO after sending this frame.
707 */
Thomas Puglieseea1af422013-12-09 14:15:14 -0600708 if ((seg->isoc_frame_index + 1) >=
Thomas Pugliese21012422013-10-23 14:44:27 -0500709 seg->isoc_frame_count)
710 release_dto = 1;
711 }
712 dev_dbg(dev, "xfer 0x%08X#%u: isoc frame = %d, holding_dto = %d, release_dto = %d.\n",
Thomas Puglieseea1af422013-12-09 14:15:14 -0600713 wa_xfer_id(xfer), seg->index, seg->isoc_frame_index,
714 holding_dto, release_dto);
Thomas Pugliese21012422013-10-23 14:44:27 -0500715 }
716 spin_unlock_irqrestore(&xfer->lock, flags);
717
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100718 switch (urb->status) {
719 case 0:
720 spin_lock_irqsave(&xfer->lock, flags);
Thomas Pugliese21012422013-10-23 14:44:27 -0500721 seg->result += urb->actual_length;
722 if (data_send_done) {
723 dev_dbg(dev, "xfer 0x%08X#%u: data out done (%zu bytes)\n",
724 wa_xfer_id(xfer), seg->index, seg->result);
725 if (seg->status < WA_SEG_PENDING)
726 seg->status = WA_SEG_PENDING;
727 } else {
728 /* should only hit this for isoc xfers. */
729 /*
730 * Populate the dto URB with the next isoc frame buffer,
731 * send the URB and release DTO if we no longer need it.
732 */
733 __wa_populate_dto_urb_isoc(xfer, seg,
Thomas Puglieseea1af422013-12-09 14:15:14 -0600734 seg->isoc_frame_offset + seg->isoc_frame_index);
Thomas Pugliese21012422013-10-23 14:44:27 -0500735
736 /* resubmit the URB with the next isoc frame. */
737 result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
738 if (result < 0) {
739 dev_err(dev, "xfer 0x%08X#%u: DTO submit failed: %d\n",
740 wa_xfer_id(xfer), seg->index, result);
741 spin_unlock_irqrestore(&xfer->lock, flags);
742 goto error_dto_submit;
743 }
744 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100745 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Pugliese21012422013-10-23 14:44:27 -0500746 if (release_dto) {
747 __wa_dto_put(wa);
748 wa_check_for_delayed_rpipes(wa);
749 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100750 break;
751 case -ECONNRESET: /* URB unlinked; no need to do anything */
752 case -ENOENT: /* as it was done by the who unlinked us */
Thomas Pugliese21012422013-10-23 14:44:27 -0500753 if (holding_dto) {
754 __wa_dto_put(wa);
755 wa_check_for_delayed_rpipes(wa);
756 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100757 break;
758 default: /* Other errors ... */
Thomas Pugliese21012422013-10-23 14:44:27 -0500759 dev_err(dev, "xfer 0x%08X#%u: data out error %d\n",
760 wa_xfer_id(xfer), seg->index, urb->status);
761 goto error_default;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100762 }
Thomas Pugliese21012422013-10-23 14:44:27 -0500763
764 return;
765
766error_dto_submit:
767error_default:
768 spin_lock_irqsave(&xfer->lock, flags);
769 rpipe = xfer->ep->hcpriv;
770 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
771 EDC_ERROR_TIMEFRAME)){
772 dev_err(dev, "DTO: URB max acceptable errors exceeded, resetting device\n");
773 wa_reset_all(wa);
774 }
775 if (seg->status != WA_SEG_ERROR) {
776 seg->status = WA_SEG_ERROR;
777 seg->result = urb->status;
778 xfer->segs_done++;
779 __wa_xfer_abort(xfer);
780 rpipe_ready = rpipe_avail_inc(rpipe);
781 done = __wa_xfer_is_done(xfer);
782 }
783 spin_unlock_irqrestore(&xfer->lock, flags);
784 if (holding_dto) {
785 __wa_dto_put(wa);
786 wa_check_for_delayed_rpipes(wa);
787 }
788 if (done)
789 wa_xfer_completion(xfer);
790 if (rpipe_ready)
791 wa_xfer_delayed_run(rpipe);
792
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100793}
794
795/*
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500796 * Callback for the isoc packet descriptor phase of the segment request
797 *
798 * Check wa_seg_tr_cb(); most comments also apply here because this
799 * function does almost the same thing and they work closely
800 * together.
801 *
802 * If the seg request has failed but this phase has succeeded,
803 * wa_seg_tr_cb() has already failed the segment and moved the
804 * status to WA_SEG_ERROR, so this will go through 'case 0' and
805 * effectively do nothing.
806 */
807static void wa_seg_iso_pack_desc_cb(struct urb *urb)
808{
809 struct wa_seg *seg = urb->context;
810 struct wa_xfer *xfer = seg->xfer;
811 struct wahc *wa;
812 struct device *dev;
813 struct wa_rpipe *rpipe;
814 unsigned long flags;
815 unsigned rpipe_ready = 0;
816 u8 done = 0;
817
818 switch (urb->status) {
819 case 0:
820 spin_lock_irqsave(&xfer->lock, flags);
821 wa = xfer->wa;
822 dev = &wa->usb_iface->dev;
Thomas Pugliese21012422013-10-23 14:44:27 -0500823 dev_dbg(dev, "iso xfer %08X#%u: packet descriptor done\n",
824 wa_xfer_id(xfer), seg->index);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500825 if (xfer->is_inbound && seg->status < WA_SEG_PENDING)
826 seg->status = WA_SEG_PENDING;
827 spin_unlock_irqrestore(&xfer->lock, flags);
828 break;
829 case -ECONNRESET: /* URB unlinked; no need to do anything */
830 case -ENOENT: /* as it was done by the who unlinked us */
831 break;
832 default: /* Other errors ... */
833 spin_lock_irqsave(&xfer->lock, flags);
834 wa = xfer->wa;
835 dev = &wa->usb_iface->dev;
836 rpipe = xfer->ep->hcpriv;
Thomas Pugliese21012422013-10-23 14:44:27 -0500837 pr_err_ratelimited("iso xfer %08X#%u: packet descriptor error %d\n",
838 wa_xfer_id(xfer), seg->index, urb->status);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500839 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
840 EDC_ERROR_TIMEFRAME)){
Thomas Pugliese226b3a22013-12-10 12:10:33 -0600841 dev_err(dev, "iso xfer: URB max acceptable errors exceeded, resetting device\n");
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500842 wa_reset_all(wa);
843 }
844 if (seg->status != WA_SEG_ERROR) {
845 usb_unlink_urb(seg->dto_urb);
846 seg->status = WA_SEG_ERROR;
847 seg->result = urb->status;
848 xfer->segs_done++;
849 __wa_xfer_abort(xfer);
850 rpipe_ready = rpipe_avail_inc(rpipe);
851 done = __wa_xfer_is_done(xfer);
852 }
853 spin_unlock_irqrestore(&xfer->lock, flags);
854 if (done)
855 wa_xfer_completion(xfer);
856 if (rpipe_ready)
857 wa_xfer_delayed_run(rpipe);
858 }
859}
860
861/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100862 * Callback for the segment request
863 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200864 * If successful transition state (unless already transitioned or
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100865 * outbound transfer); otherwise, take a note of the error, mark this
866 * segment done and try completion.
867 *
868 * Note we don't access until we are sure that the transfer hasn't
869 * been cancelled (ECONNRESET, ENOENT), which could mean that
870 * seg->xfer could be already gone.
871 *
872 * We have to check before setting the status to WA_SEG_PENDING
873 * because sometimes the xfer result callback arrives before this
874 * callback (geeeeeeze), so it might happen that we are already in
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500875 * another state. As well, we don't set it if the transfer is not inbound,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100876 * as in that case, wa_seg_dto_cb will do it when the OUT data phase
877 * finishes.
878 */
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500879static void wa_seg_tr_cb(struct urb *urb)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100880{
881 struct wa_seg *seg = urb->context;
882 struct wa_xfer *xfer = seg->xfer;
883 struct wahc *wa;
884 struct device *dev;
885 struct wa_rpipe *rpipe;
886 unsigned long flags;
887 unsigned rpipe_ready;
888 u8 done = 0;
889
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100890 switch (urb->status) {
891 case 0:
892 spin_lock_irqsave(&xfer->lock, flags);
893 wa = xfer->wa;
894 dev = &wa->usb_iface->dev;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500895 dev_dbg(dev, "xfer %p ID 0x%08X#%u: request done\n",
896 xfer, wa_xfer_id(xfer), seg->index);
897 if (xfer->is_inbound &&
898 seg->status < WA_SEG_PENDING &&
899 !(usb_pipeisoc(xfer->urb->pipe)))
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100900 seg->status = WA_SEG_PENDING;
901 spin_unlock_irqrestore(&xfer->lock, flags);
902 break;
903 case -ECONNRESET: /* URB unlinked; no need to do anything */
904 case -ENOENT: /* as it was done by the who unlinked us */
905 break;
906 default: /* Other errors ... */
907 spin_lock_irqsave(&xfer->lock, flags);
908 wa = xfer->wa;
909 dev = &wa->usb_iface->dev;
910 rpipe = xfer->ep->hcpriv;
911 if (printk_ratelimit())
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500912 dev_err(dev, "xfer %p ID 0x%08X#%u: request error %d\n",
913 xfer, wa_xfer_id(xfer), seg->index,
914 urb->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100915 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
916 EDC_ERROR_TIMEFRAME)){
917 dev_err(dev, "DTO: URB max acceptable errors "
918 "exceeded, resetting device\n");
919 wa_reset_all(wa);
920 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500921 usb_unlink_urb(seg->isoc_pack_desc_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100922 usb_unlink_urb(seg->dto_urb);
923 seg->status = WA_SEG_ERROR;
924 seg->result = urb->status;
925 xfer->segs_done++;
926 __wa_xfer_abort(xfer);
927 rpipe_ready = rpipe_avail_inc(rpipe);
928 done = __wa_xfer_is_done(xfer);
929 spin_unlock_irqrestore(&xfer->lock, flags);
930 if (done)
931 wa_xfer_completion(xfer);
932 if (rpipe_ready)
933 wa_xfer_delayed_run(rpipe);
934 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100935}
936
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500937/*
938 * Allocate an SG list to store bytes_to_transfer bytes and copy the
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500939 * subset of the in_sg that matches the buffer subset
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500940 * we are about to transfer.
941 */
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500942static struct scatterlist *wa_xfer_create_subset_sg(struct scatterlist *in_sg,
943 const unsigned int bytes_transferred,
944 const unsigned int bytes_to_transfer, unsigned int *out_num_sgs)
945{
946 struct scatterlist *out_sg;
947 unsigned int bytes_processed = 0, offset_into_current_page_data = 0,
948 nents;
949 struct scatterlist *current_xfer_sg = in_sg;
950 struct scatterlist *current_seg_sg, *last_seg_sg;
951
952 /* skip previously transferred pages. */
953 while ((current_xfer_sg) &&
954 (bytes_processed < bytes_transferred)) {
955 bytes_processed += current_xfer_sg->length;
956
957 /* advance the sg if current segment starts on or past the
958 next page. */
959 if (bytes_processed <= bytes_transferred)
960 current_xfer_sg = sg_next(current_xfer_sg);
961 }
962
963 /* the data for the current segment starts in current_xfer_sg.
964 calculate the offset. */
965 if (bytes_processed > bytes_transferred) {
966 offset_into_current_page_data = current_xfer_sg->length -
967 (bytes_processed - bytes_transferred);
968 }
969
970 /* calculate the number of pages needed by this segment. */
971 nents = DIV_ROUND_UP((bytes_to_transfer +
972 offset_into_current_page_data +
973 current_xfer_sg->offset),
974 PAGE_SIZE);
975
976 out_sg = kmalloc((sizeof(struct scatterlist) * nents), GFP_ATOMIC);
977 if (out_sg) {
978 sg_init_table(out_sg, nents);
979
980 /* copy the portion of the incoming SG that correlates to the
981 * data to be transferred by this segment to the segment SG. */
982 last_seg_sg = current_seg_sg = out_sg;
983 bytes_processed = 0;
984
985 /* reset nents and calculate the actual number of sg entries
986 needed. */
987 nents = 0;
988 while ((bytes_processed < bytes_to_transfer) &&
989 current_seg_sg && current_xfer_sg) {
990 unsigned int page_len = min((current_xfer_sg->length -
991 offset_into_current_page_data),
992 (bytes_to_transfer - bytes_processed));
993
994 sg_set_page(current_seg_sg, sg_page(current_xfer_sg),
995 page_len,
996 current_xfer_sg->offset +
997 offset_into_current_page_data);
998
999 bytes_processed += page_len;
1000
1001 last_seg_sg = current_seg_sg;
1002 current_seg_sg = sg_next(current_seg_sg);
1003 current_xfer_sg = sg_next(current_xfer_sg);
1004
1005 /* only the first page may require additional offset. */
1006 offset_into_current_page_data = 0;
1007 nents++;
1008 }
1009
1010 /* update num_sgs and terminate the list since we may have
1011 * concatenated pages. */
1012 sg_mark_end(last_seg_sg);
1013 *out_num_sgs = nents;
1014 }
1015
1016 return out_sg;
1017}
1018
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001019/*
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001020 * Populate DMA buffer info for the isoc dto urb.
1021 */
Thomas Pugliese21012422013-10-23 14:44:27 -05001022static void __wa_populate_dto_urb_isoc(struct wa_xfer *xfer,
1023 struct wa_seg *seg, int curr_iso_frame)
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001024{
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001025 seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1026 seg->dto_urb->sg = NULL;
1027 seg->dto_urb->num_sgs = 0;
Thomas Pugliesef07ddb92013-10-23 14:44:28 -05001028 /* dto urb buffer address pulled from iso_frame_desc. */
1029 seg->dto_urb->transfer_dma = xfer->urb->transfer_dma +
1030 xfer->urb->iso_frame_desc[curr_iso_frame].offset;
1031 /* The Alereon HWA sends a single URB with all isoc segs. */
1032 if (xfer->wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC)
1033 seg->dto_urb->transfer_buffer_length = seg->isoc_size;
1034 else
1035 seg->dto_urb->transfer_buffer_length =
1036 xfer->urb->iso_frame_desc[curr_iso_frame].length;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001037}
1038
1039/*
Thomas Puglieseffd6d172013-09-26 14:08:14 -05001040 * Populate buffer ptr and size, DMA buffer or SG list for the dto urb.
1041 */
1042static int __wa_populate_dto_urb(struct wa_xfer *xfer,
1043 struct wa_seg *seg, size_t buf_itr_offset, size_t buf_itr_size)
1044{
1045 int result = 0;
1046
1047 if (xfer->is_dma) {
1048 seg->dto_urb->transfer_dma =
1049 xfer->urb->transfer_dma + buf_itr_offset;
1050 seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1051 seg->dto_urb->sg = NULL;
1052 seg->dto_urb->num_sgs = 0;
1053 } else {
1054 /* do buffer or SG processing. */
1055 seg->dto_urb->transfer_flags &=
1056 ~URB_NO_TRANSFER_DMA_MAP;
1057 /* this should always be 0 before a resubmit. */
1058 seg->dto_urb->num_mapped_sgs = 0;
1059
1060 if (xfer->urb->transfer_buffer) {
1061 seg->dto_urb->transfer_buffer =
1062 xfer->urb->transfer_buffer +
1063 buf_itr_offset;
1064 seg->dto_urb->sg = NULL;
1065 seg->dto_urb->num_sgs = 0;
1066 } else {
1067 seg->dto_urb->transfer_buffer = NULL;
1068
1069 /*
1070 * allocate an SG list to store seg_size bytes
1071 * and copy the subset of the xfer->urb->sg that
1072 * matches the buffer subset we are about to
1073 * read.
1074 */
1075 seg->dto_urb->sg = wa_xfer_create_subset_sg(
1076 xfer->urb->sg,
1077 buf_itr_offset, buf_itr_size,
1078 &(seg->dto_urb->num_sgs));
1079 if (!(seg->dto_urb->sg))
1080 result = -ENOMEM;
1081 }
1082 }
1083 seg->dto_urb->transfer_buffer_length = buf_itr_size;
1084
1085 return result;
1086}
1087
1088/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001089 * Allocate the segs array and initialize each of them
1090 *
1091 * The segments are freed by wa_xfer_destroy() when the xfer use count
1092 * drops to zero; however, because each segment is given the same life
1093 * cycle as the USB URB it contains, it is actually freed by
1094 * usb_put_urb() on the contained USB URB (twisted, eh?).
1095 */
1096static int __wa_xfer_setup_segs(struct wa_xfer *xfer, size_t xfer_hdr_size)
1097{
Thomas Pugliese21012422013-10-23 14:44:27 -05001098 int result, cnt, iso_frame_offset;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001099 size_t alloc_size = sizeof(*xfer->seg[0])
1100 - sizeof(xfer->seg[0]->xfer_hdr) + xfer_hdr_size;
1101 struct usb_device *usb_dev = xfer->wa->usb_dev;
1102 const struct usb_endpoint_descriptor *dto_epd = xfer->wa->dto_epd;
1103 struct wa_seg *seg;
Thomas Pugliese21012422013-10-23 14:44:27 -05001104 size_t buf_itr, buf_size, buf_itr_size;
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001105 int isoc_frame_offset = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001106
1107 result = -ENOMEM;
David Vrabel92c4d9b2008-10-15 14:50:10 +01001108 xfer->seg = kcalloc(xfer->segs, sizeof(xfer->seg[0]), GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001109 if (xfer->seg == NULL)
1110 goto error_segs_kzalloc;
1111 buf_itr = 0;
1112 buf_size = xfer->urb->transfer_buffer_length;
Thomas Pugliese21012422013-10-23 14:44:27 -05001113 iso_frame_offset = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001114 for (cnt = 0; cnt < xfer->segs; cnt++) {
Thomas Pugliese21012422013-10-23 14:44:27 -05001115 size_t iso_pkt_descr_size = 0;
1116 int seg_isoc_frame_count = 0, seg_isoc_size = 0;
1117
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001118 /*
1119 * Adjust the size of the segment object to contain space for
1120 * the isoc packet descriptor buffer.
1121 */
Thomas Pugliese21012422013-10-23 14:44:27 -05001122 if (usb_pipeisoc(xfer->urb->pipe)) {
1123 seg_isoc_frame_count =
1124 __wa_seg_calculate_isoc_frame_count(xfer,
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001125 isoc_frame_offset, &seg_isoc_size);
Thomas Pugliese21012422013-10-23 14:44:27 -05001126
1127 iso_pkt_descr_size =
1128 sizeof(struct wa_xfer_packet_info_hwaiso) +
1129 (seg_isoc_frame_count * sizeof(__le16));
1130 }
1131 seg = xfer->seg[cnt] = kmalloc(alloc_size + iso_pkt_descr_size,
1132 GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001133 if (seg == NULL)
Thomas Pugliese66591015d2013-08-15 14:37:43 -05001134 goto error_seg_kmalloc;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001135 wa_seg_init(seg);
1136 seg->xfer = xfer;
1137 seg->index = cnt;
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001138 usb_fill_bulk_urb(&seg->tr_urb, usb_dev,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001139 usb_sndbulkpipe(usb_dev,
1140 dto_epd->bEndpointAddress),
1141 &seg->xfer_hdr, xfer_hdr_size,
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001142 wa_seg_tr_cb, seg);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001143 buf_itr_size = min(buf_size, xfer->seg_size);
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001144
1145 if (usb_pipeisoc(xfer->urb->pipe)) {
1146 seg->isoc_frame_count = seg_isoc_frame_count;
1147 seg->isoc_frame_offset = isoc_frame_offset;
1148 seg->isoc_size = seg_isoc_size;
1149 /* iso packet descriptor. */
1150 seg->isoc_pack_desc_urb =
1151 usb_alloc_urb(0, GFP_ATOMIC);
1152 if (seg->isoc_pack_desc_urb == NULL)
1153 goto error_iso_pack_desc_alloc;
1154 /*
1155 * The buffer for the isoc packet descriptor starts
1156 * after the transfer request header in the
1157 * segment object memory buffer.
1158 */
1159 usb_fill_bulk_urb(
1160 seg->isoc_pack_desc_urb, usb_dev,
1161 usb_sndbulkpipe(usb_dev,
1162 dto_epd->bEndpointAddress),
1163 (void *)(&seg->xfer_hdr) +
1164 xfer_hdr_size,
1165 iso_pkt_descr_size,
1166 wa_seg_iso_pack_desc_cb, seg);
1167
1168 /* adjust starting frame offset for next seg. */
1169 isoc_frame_offset += seg_isoc_frame_count;
1170 }
1171
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001172 if (xfer->is_inbound == 0 && buf_size > 0) {
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001173 /* outbound data. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001174 seg->dto_urb = usb_alloc_urb(0, GFP_ATOMIC);
1175 if (seg->dto_urb == NULL)
1176 goto error_dto_alloc;
1177 usb_fill_bulk_urb(
1178 seg->dto_urb, usb_dev,
1179 usb_sndbulkpipe(usb_dev,
1180 dto_epd->bEndpointAddress),
1181 NULL, 0, wa_seg_dto_cb, seg);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001182
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001183 if (usb_pipeisoc(xfer->urb->pipe)) {
Thomas Pugliese21012422013-10-23 14:44:27 -05001184 /*
1185 * Fill in the xfer buffer information for the
1186 * first isoc frame. Subsequent frames in this
1187 * segment will be filled in and sent from the
1188 * DTO completion routine, if needed.
1189 */
1190 __wa_populate_dto_urb_isoc(xfer, seg,
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001191 seg->isoc_frame_offset);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001192 } else {
1193 /* fill in the xfer buffer information. */
1194 result = __wa_populate_dto_urb(xfer, seg,
1195 buf_itr, buf_itr_size);
1196 if (result < 0)
1197 goto error_seg_outbound_populate;
1198
1199 buf_itr += buf_itr_size;
1200 buf_size -= buf_itr_size;
1201 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001202 }
1203 seg->status = WA_SEG_READY;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001204 }
1205 return 0;
1206
Thomas Puglieseffd6d172013-09-26 14:08:14 -05001207 /*
1208 * Free the memory for the current segment which failed to init.
1209 * Use the fact that cnt is left at were it failed. The remaining
1210 * segments will be cleaned up by wa_xfer_destroy.
1211 */
1212error_seg_outbound_populate:
Thomas Pugliese11b1bf82013-08-15 14:37:41 -05001213 usb_free_urb(xfer->seg[cnt]->dto_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001214error_dto_alloc:
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001215 usb_free_urb(xfer->seg[cnt]->isoc_pack_desc_urb);
1216error_iso_pack_desc_alloc:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001217 kfree(xfer->seg[cnt]);
Thomas Puglieseffd6d172013-09-26 14:08:14 -05001218 xfer->seg[cnt] = NULL;
Thomas Pugliese66591015d2013-08-15 14:37:43 -05001219error_seg_kmalloc:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001220error_segs_kzalloc:
1221 return result;
1222}
1223
1224/*
1225 * Allocates all the stuff needed to submit a transfer
1226 *
1227 * Breaks the whole data buffer in a list of segments, each one has a
1228 * structure allocated to it and linked in xfer->seg[index]
1229 *
1230 * FIXME: merge setup_segs() and the last part of this function, no
1231 * need to do two for loops when we could run everything in a
1232 * single one
1233 */
1234static int __wa_xfer_setup(struct wa_xfer *xfer, struct urb *urb)
1235{
1236 int result;
1237 struct device *dev = &xfer->wa->usb_iface->dev;
1238 enum wa_xfer_type xfer_type = 0; /* shut up GCC */
1239 size_t xfer_hdr_size, cnt, transfer_size;
1240 struct wa_xfer_hdr *xfer_hdr0, *xfer_hdr;
1241
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001242 result = __wa_xfer_setup_sizes(xfer, &xfer_type);
1243 if (result < 0)
1244 goto error_setup_sizes;
1245 xfer_hdr_size = result;
1246 result = __wa_xfer_setup_segs(xfer, xfer_hdr_size);
1247 if (result < 0) {
1248 dev_err(dev, "xfer %p: Failed to allocate %d segments: %d\n",
1249 xfer, xfer->segs, result);
1250 goto error_setup_segs;
1251 }
1252 /* Fill the first header */
1253 xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
1254 wa_xfer_id_init(xfer);
1255 __wa_xfer_setup_hdr0(xfer, xfer_hdr0, xfer_type, xfer_hdr_size);
1256
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001257 /* Fill remaining headers */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001258 xfer_hdr = xfer_hdr0;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001259 if (xfer_type == WA_XFER_TYPE_ISO) {
1260 xfer_hdr0->dwTransferLength =
Thomas Pugliese21012422013-10-23 14:44:27 -05001261 cpu_to_le32(xfer->seg[0]->isoc_size);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001262 for (cnt = 1; cnt < xfer->segs; cnt++) {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001263 struct wa_xfer_packet_info_hwaiso *packet_desc;
Thomas Pugliese21012422013-10-23 14:44:27 -05001264 struct wa_seg *seg = xfer->seg[cnt];
Thomas Pugliese756a2ee2013-12-09 14:15:15 -06001265 struct wa_xfer_hwaiso *xfer_iso;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001266
Thomas Pugliese21012422013-10-23 14:44:27 -05001267 xfer_hdr = &seg->xfer_hdr;
Thomas Pugliese756a2ee2013-12-09 14:15:15 -06001268 xfer_iso = container_of(xfer_hdr,
1269 struct wa_xfer_hwaiso, hdr);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001270 packet_desc = ((void *)xfer_hdr) + xfer_hdr_size;
1271 /*
Thomas Pugliese21012422013-10-23 14:44:27 -05001272 * Copy values from the 0th header. Segment specific
1273 * values are set below.
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001274 */
Thomas Pugliese21012422013-10-23 14:44:27 -05001275 memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001276 xfer_hdr->bTransferSegment = cnt;
1277 xfer_hdr->dwTransferLength =
Thomas Pugliese21012422013-10-23 14:44:27 -05001278 cpu_to_le32(seg->isoc_size);
Thomas Pugliese756a2ee2013-12-09 14:15:15 -06001279 xfer_iso->dwNumOfPackets =
1280 cpu_to_le32(seg->isoc_frame_count);
Thomas Pugliese21012422013-10-23 14:44:27 -05001281 __wa_setup_isoc_packet_descr(packet_desc, xfer, seg);
1282 seg->status = WA_SEG_READY;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001283 }
1284 } else {
1285 transfer_size = urb->transfer_buffer_length;
1286 xfer_hdr0->dwTransferLength = transfer_size > xfer->seg_size ?
1287 cpu_to_le32(xfer->seg_size) :
1288 cpu_to_le32(transfer_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001289 transfer_size -= xfer->seg_size;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001290 for (cnt = 1; cnt < xfer->segs; cnt++) {
1291 xfer_hdr = &xfer->seg[cnt]->xfer_hdr;
1292 memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
1293 xfer_hdr->bTransferSegment = cnt;
1294 xfer_hdr->dwTransferLength =
1295 transfer_size > xfer->seg_size ?
1296 cpu_to_le32(xfer->seg_size)
1297 : cpu_to_le32(transfer_size);
1298 xfer->seg[cnt]->status = WA_SEG_READY;
1299 transfer_size -= xfer->seg_size;
1300 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001301 }
1302 xfer_hdr->bTransferSegment |= 0x80; /* this is the last segment */
1303 result = 0;
1304error_setup_segs:
1305error_setup_sizes:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001306 return result;
1307}
1308
1309/*
1310 *
1311 *
1312 * rpipe->seg_lock is held!
1313 */
1314static int __wa_seg_submit(struct wa_rpipe *rpipe, struct wa_xfer *xfer,
Thomas Pugliese679ee472013-10-07 10:53:57 -05001315 struct wa_seg *seg, int *dto_done)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001316{
1317 int result;
Thomas Pugliese679ee472013-10-07 10:53:57 -05001318
1319 /* default to done unless we encounter a multi-frame isoc segment. */
1320 *dto_done = 1;
1321
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001322 /* submit the transfer request. */
1323 result = usb_submit_urb(&seg->tr_urb, GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001324 if (result < 0) {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001325 pr_err("%s: xfer %p#%u: REQ submit failed: %d\n",
1326 __func__, xfer, seg->index, result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001327 goto error_seg_submit;
1328 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001329 /* submit the isoc packet descriptor if present. */
1330 if (seg->isoc_pack_desc_urb) {
1331 result = usb_submit_urb(seg->isoc_pack_desc_urb, GFP_ATOMIC);
Thomas Puglieseea1af422013-12-09 14:15:14 -06001332 seg->isoc_frame_index = 0;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001333 if (result < 0) {
1334 pr_err("%s: xfer %p#%u: ISO packet descriptor submit failed: %d\n",
1335 __func__, xfer, seg->index, result);
1336 goto error_iso_pack_desc_submit;
1337 }
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001338 }
1339 /* submit the out data if this is an out request. */
1340 if (seg->dto_urb) {
1341 struct wahc *wa = xfer->wa;
1342 result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
1343 if (result < 0) {
1344 pr_err("%s: xfer %p#%u: DTO submit failed: %d\n",
1345 __func__, xfer, seg->index, result);
1346 goto error_dto_submit;
1347 }
Thomas Pugliese21012422013-10-23 14:44:27 -05001348 /*
1349 * If this segment contains more than one isoc frame, hold
1350 * onto the dto resource until we send all frames.
Thomas Pugliesef07ddb92013-10-23 14:44:28 -05001351 * Only applies to non-Alereon devices.
Thomas Pugliese21012422013-10-23 14:44:27 -05001352 */
Thomas Pugliesef07ddb92013-10-23 14:44:28 -05001353 if (((wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC) == 0)
1354 && (seg->isoc_frame_count > 1))
Thomas Pugliese21012422013-10-23 14:44:27 -05001355 *dto_done = 0;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001356 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001357 seg->status = WA_SEG_SUBMITTED;
1358 rpipe_avail_dec(rpipe);
1359 return 0;
1360
1361error_dto_submit:
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001362 usb_unlink_urb(seg->isoc_pack_desc_urb);
1363error_iso_pack_desc_submit:
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001364 usb_unlink_urb(&seg->tr_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001365error_seg_submit:
1366 seg->status = WA_SEG_ERROR;
1367 seg->result = result;
Thomas Pugliese21012422013-10-23 14:44:27 -05001368 *dto_done = 1;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001369 return result;
1370}
1371
1372/*
Thomas Pugliese679ee472013-10-07 10:53:57 -05001373 * Execute more queued request segments until the maximum concurrent allowed.
1374 * Return true if the DTO resource was acquired and released.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001375 *
1376 * The ugly unlock/lock sequence on the error path is needed as the
1377 * xfer->lock normally nests the seg_lock and not viceversa.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001378 */
Thomas Pugliese679ee472013-10-07 10:53:57 -05001379static int __wa_xfer_delayed_run(struct wa_rpipe *rpipe, int *dto_waiting)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001380{
Thomas Pugliese679ee472013-10-07 10:53:57 -05001381 int result, dto_acquired = 0, dto_done = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001382 struct device *dev = &rpipe->wa->usb_iface->dev;
1383 struct wa_seg *seg;
1384 struct wa_xfer *xfer;
1385 unsigned long flags;
1386
Thomas Pugliese679ee472013-10-07 10:53:57 -05001387 *dto_waiting = 0;
1388
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001389 spin_lock_irqsave(&rpipe->seg_lock, flags);
1390 while (atomic_read(&rpipe->segs_available) > 0
Thomas Pugliese679ee472013-10-07 10:53:57 -05001391 && !list_empty(&rpipe->seg_list)
1392 && (dto_acquired = __wa_dto_try_get(rpipe->wa))) {
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001393 seg = list_first_entry(&(rpipe->seg_list), struct wa_seg,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001394 list_node);
1395 list_del(&seg->list_node);
1396 xfer = seg->xfer;
Thomas Pugliese679ee472013-10-07 10:53:57 -05001397 result = __wa_seg_submit(rpipe, xfer, seg, &dto_done);
1398 /* release the dto resource if this RPIPE is done with it. */
1399 if (dto_done)
1400 __wa_dto_put(rpipe->wa);
Thomas Puglieseb9c84be2013-09-27 15:33:36 -05001401 dev_dbg(dev, "xfer %p ID %08X#%u submitted from delayed [%d segments available] %d\n",
1402 xfer, wa_xfer_id(xfer), seg->index,
1403 atomic_read(&rpipe->segs_available), result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001404 if (unlikely(result < 0)) {
1405 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
1406 spin_lock_irqsave(&xfer->lock, flags);
1407 __wa_xfer_abort(xfer);
1408 xfer->segs_done++;
1409 spin_unlock_irqrestore(&xfer->lock, flags);
1410 spin_lock_irqsave(&rpipe->seg_lock, flags);
1411 }
1412 }
Thomas Pugliese679ee472013-10-07 10:53:57 -05001413 /*
1414 * Mark this RPIPE as waiting if dto was not acquired, there are
1415 * delayed segs and no active transfers to wake us up later.
1416 */
1417 if (!dto_acquired && !list_empty(&rpipe->seg_list)
1418 && (atomic_read(&rpipe->segs_available) ==
1419 le16_to_cpu(rpipe->descr.wRequests)))
1420 *dto_waiting = 1;
1421
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001422 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
Thomas Pugliese679ee472013-10-07 10:53:57 -05001423
1424 return dto_done;
1425}
1426
1427static void wa_xfer_delayed_run(struct wa_rpipe *rpipe)
1428{
1429 int dto_waiting;
1430 int dto_done = __wa_xfer_delayed_run(rpipe, &dto_waiting);
1431
1432 /*
1433 * If this RPIPE is waiting on the DTO resource, add it to the tail of
1434 * the waiting list.
1435 * Otherwise, if the WA DTO resource was acquired and released by
1436 * __wa_xfer_delayed_run, another RPIPE may have attempted to acquire
1437 * DTO and failed during that time. Check the delayed list and process
1438 * any waiters. Start searching from the next RPIPE index.
1439 */
1440 if (dto_waiting)
1441 wa_add_delayed_rpipe(rpipe->wa, rpipe);
1442 else if (dto_done)
1443 wa_check_for_delayed_rpipes(rpipe->wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001444}
1445
1446/*
1447 *
1448 * xfer->lock is taken
1449 *
1450 * On failure submitting we just stop submitting and return error;
1451 * wa_urb_enqueue_b() will execute the completion path
1452 */
1453static int __wa_xfer_submit(struct wa_xfer *xfer)
1454{
Thomas Pugliese679ee472013-10-07 10:53:57 -05001455 int result, dto_acquired = 0, dto_done = 0, dto_waiting = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001456 struct wahc *wa = xfer->wa;
1457 struct device *dev = &wa->usb_iface->dev;
1458 unsigned cnt;
1459 struct wa_seg *seg;
1460 unsigned long flags;
1461 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
1462 size_t maxrequests = le16_to_cpu(rpipe->descr.wRequests);
1463 u8 available;
1464 u8 empty;
1465
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001466 spin_lock_irqsave(&wa->xfer_list_lock, flags);
1467 list_add_tail(&xfer->list_node, &wa->xfer_list);
1468 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
1469
1470 BUG_ON(atomic_read(&rpipe->segs_available) > maxrequests);
1471 result = 0;
1472 spin_lock_irqsave(&rpipe->seg_lock, flags);
1473 for (cnt = 0; cnt < xfer->segs; cnt++) {
Thomas Pugliese679ee472013-10-07 10:53:57 -05001474 int delay_seg = 1;
1475
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001476 available = atomic_read(&rpipe->segs_available);
1477 empty = list_empty(&rpipe->seg_list);
1478 seg = xfer->seg[cnt];
Thomas Pugliese679ee472013-10-07 10:53:57 -05001479 if (available && empty) {
1480 /*
1481 * Only attempt to acquire DTO if we have a segment
1482 * to send.
1483 */
1484 dto_acquired = __wa_dto_try_get(rpipe->wa);
1485 if (dto_acquired) {
1486 delay_seg = 0;
1487 result = __wa_seg_submit(rpipe, xfer, seg,
1488 &dto_done);
Thomas Pugliese21012422013-10-23 14:44:27 -05001489 dev_dbg(dev, "xfer %p ID 0x%08X#%u: available %u empty %u submitted\n",
1490 xfer, wa_xfer_id(xfer), cnt, available,
1491 empty);
Thomas Pugliese679ee472013-10-07 10:53:57 -05001492 if (dto_done)
1493 __wa_dto_put(rpipe->wa);
1494
1495 if (result < 0) {
1496 __wa_xfer_abort(xfer);
1497 goto error_seg_submit;
1498 }
1499 }
1500 }
1501
1502 if (delay_seg) {
Thomas Pugliese21012422013-10-23 14:44:27 -05001503 dev_dbg(dev, "xfer %p ID 0x%08X#%u: available %u empty %u delayed\n",
1504 xfer, wa_xfer_id(xfer), cnt, available, empty);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001505 seg->status = WA_SEG_DELAYED;
1506 list_add_tail(&seg->list_node, &rpipe->seg_list);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001507 }
1508 xfer->segs_submitted++;
1509 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001510error_seg_submit:
Thomas Pugliese679ee472013-10-07 10:53:57 -05001511 /*
1512 * Mark this RPIPE as waiting if dto was not acquired, there are
1513 * delayed segs and no active transfers to wake us up later.
1514 */
1515 if (!dto_acquired && !list_empty(&rpipe->seg_list)
1516 && (atomic_read(&rpipe->segs_available) ==
1517 le16_to_cpu(rpipe->descr.wRequests)))
1518 dto_waiting = 1;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001519 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
Thomas Pugliese679ee472013-10-07 10:53:57 -05001520
1521 if (dto_waiting)
1522 wa_add_delayed_rpipe(rpipe->wa, rpipe);
1523 else if (dto_done)
1524 wa_check_for_delayed_rpipes(rpipe->wa);
1525
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001526 return result;
1527}
1528
1529/*
1530 * Second part of a URB/transfer enqueuement
1531 *
1532 * Assumes this comes from wa_urb_enqueue() [maybe through
1533 * wa_urb_enqueue_run()]. At this point:
1534 *
1535 * xfer->wa filled and refcounted
1536 * xfer->ep filled with rpipe refcounted if
1537 * delayed == 0
1538 * xfer->urb filled and refcounted (this is the case when called
1539 * from wa_urb_enqueue() as we come from usb_submit_urb()
1540 * and when called by wa_urb_enqueue_run(), as we took an
1541 * extra ref dropped by _run() after we return).
1542 * xfer->gfp filled
1543 *
1544 * If we fail at __wa_xfer_submit(), then we just check if we are done
1545 * and if so, we run the completion procedure. However, if we are not
1546 * yet done, we do nothing and wait for the completion handlers from
1547 * the submitted URBs or from the xfer-result path to kick in. If xfer
1548 * result never kicks in, the xfer will timeout from the USB code and
1549 * dequeue() will be called.
1550 */
Thomas Pugliese33186c42013-10-01 10:14:56 -05001551static int wa_urb_enqueue_b(struct wa_xfer *xfer)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001552{
1553 int result;
1554 unsigned long flags;
1555 struct urb *urb = xfer->urb;
1556 struct wahc *wa = xfer->wa;
1557 struct wusbhc *wusbhc = wa->wusb;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001558 struct wusb_dev *wusb_dev;
1559 unsigned done;
1560
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001561 result = rpipe_get_by_ep(wa, xfer->ep, urb, xfer->gfp);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001562 if (result < 0) {
1563 pr_err("%s: error_rpipe_get\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001564 goto error_rpipe_get;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001565 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001566 result = -ENODEV;
1567 /* FIXME: segmentation broken -- kills DWA */
1568 mutex_lock(&wusbhc->mutex); /* get a WUSB dev */
Jiri Slaby49fa0922009-03-11 21:47:40 +01001569 if (urb->dev == NULL) {
1570 mutex_unlock(&wusbhc->mutex);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001571 pr_err("%s: error usb dev gone\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001572 goto error_dev_gone;
Jiri Slaby49fa0922009-03-11 21:47:40 +01001573 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001574 wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev);
1575 if (wusb_dev == NULL) {
1576 mutex_unlock(&wusbhc->mutex);
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001577 dev_err(&(urb->dev->dev), "%s: error wusb dev gone\n",
1578 __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001579 goto error_dev_gone;
1580 }
1581 mutex_unlock(&wusbhc->mutex);
1582
1583 spin_lock_irqsave(&xfer->lock, flags);
1584 xfer->wusb_dev = wusb_dev;
1585 result = urb->status;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001586 if (urb->status != -EINPROGRESS) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001587 dev_err(&(urb->dev->dev), "%s: error_dequeued\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001588 goto error_dequeued;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001589 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001590
1591 result = __wa_xfer_setup(xfer, urb);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001592 if (result < 0) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001593 dev_err(&(urb->dev->dev), "%s: error_xfer_setup\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001594 goto error_xfer_setup;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001595 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001596 result = __wa_xfer_submit(xfer);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001597 if (result < 0) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001598 dev_err(&(urb->dev->dev), "%s: error_xfer_submit\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001599 goto error_xfer_submit;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001600 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001601 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001602 return 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001603
Thomas Pugliese33186c42013-10-01 10:14:56 -05001604 /*
1605 * this is basically wa_xfer_completion() broken up wa_xfer_giveback()
1606 * does a wa_xfer_put() that will call wa_xfer_destroy() and undo
1607 * setup().
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001608 */
1609error_xfer_setup:
1610error_dequeued:
1611 spin_unlock_irqrestore(&xfer->lock, flags);
1612 /* FIXME: segmentation broken, kills DWA */
1613 if (wusb_dev)
1614 wusb_dev_put(wusb_dev);
1615error_dev_gone:
1616 rpipe_put(xfer->ep->hcpriv);
1617error_rpipe_get:
1618 xfer->result = result;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001619 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001620
1621error_xfer_submit:
1622 done = __wa_xfer_is_done(xfer);
1623 xfer->result = result;
1624 spin_unlock_irqrestore(&xfer->lock, flags);
1625 if (done)
1626 wa_xfer_completion(xfer);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001627 /* return success since the completion routine will run. */
1628 return 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001629}
1630
1631/*
1632 * Execute the delayed transfers in the Wire Adapter @wa
1633 *
1634 * We need to be careful here, as dequeue() could be called in the
1635 * middle. That's why we do the whole thing under the
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001636 * wa->xfer_list_lock. If dequeue() jumps in, it first locks xfer->lock
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001637 * and then checks the list -- so as we would be acquiring in inverse
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001638 * order, we move the delayed list to a separate list while locked and then
1639 * submit them without the list lock held.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001640 */
1641void wa_urb_enqueue_run(struct work_struct *ws)
1642{
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001643 struct wahc *wa = container_of(ws, struct wahc, xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001644 struct wa_xfer *xfer, *next;
1645 struct urb *urb;
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001646 LIST_HEAD(tmp_list);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001647
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001648 /* Create a copy of the wa->xfer_delayed_list while holding the lock */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001649 spin_lock_irq(&wa->xfer_list_lock);
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001650 list_cut_position(&tmp_list, &wa->xfer_delayed_list,
1651 wa->xfer_delayed_list.prev);
1652 spin_unlock_irq(&wa->xfer_list_lock);
1653
1654 /*
1655 * enqueue from temp list without list lock held since wa_urb_enqueue_b
1656 * can take xfer->lock as well as lock mutexes.
1657 */
1658 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001659 list_del_init(&xfer->list_node);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001660
1661 urb = xfer->urb;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001662 if (wa_urb_enqueue_b(xfer) < 0)
1663 wa_xfer_giveback(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001664 usb_put_urb(urb); /* taken when queuing */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001665 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001666}
1667EXPORT_SYMBOL_GPL(wa_urb_enqueue_run);
1668
1669/*
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001670 * Process the errored transfers on the Wire Adapter outside of interrupt.
1671 */
1672void wa_process_errored_transfers_run(struct work_struct *ws)
1673{
1674 struct wahc *wa = container_of(ws, struct wahc, xfer_error_work);
1675 struct wa_xfer *xfer, *next;
1676 LIST_HEAD(tmp_list);
1677
1678 pr_info("%s: Run delayed STALL processing.\n", __func__);
1679
1680 /* Create a copy of the wa->xfer_errored_list while holding the lock */
1681 spin_lock_irq(&wa->xfer_list_lock);
1682 list_cut_position(&tmp_list, &wa->xfer_errored_list,
1683 wa->xfer_errored_list.prev);
1684 spin_unlock_irq(&wa->xfer_list_lock);
1685
1686 /*
1687 * run rpipe_clear_feature_stalled from temp list without list lock
1688 * held.
1689 */
1690 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
1691 struct usb_host_endpoint *ep;
1692 unsigned long flags;
1693 struct wa_rpipe *rpipe;
1694
1695 spin_lock_irqsave(&xfer->lock, flags);
1696 ep = xfer->ep;
1697 rpipe = ep->hcpriv;
1698 spin_unlock_irqrestore(&xfer->lock, flags);
1699
1700 /* clear RPIPE feature stalled without holding a lock. */
1701 rpipe_clear_feature_stalled(wa, ep);
1702
1703 /* complete the xfer. This removes it from the tmp list. */
1704 wa_xfer_completion(xfer);
1705
1706 /* check for work. */
1707 wa_xfer_delayed_run(rpipe);
1708 }
1709}
1710EXPORT_SYMBOL_GPL(wa_process_errored_transfers_run);
1711
1712/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001713 * Submit a transfer to the Wire Adapter in a delayed way
1714 *
1715 * The process of enqueuing involves possible sleeps() [see
1716 * enqueue_b(), for the rpipe_get() and the mutex_lock()]. If we are
1717 * in an atomic section, we defer the enqueue_b() call--else we call direct.
1718 *
1719 * @urb: We own a reference to it done by the HCI Linux USB stack that
1720 * will be given up by calling usb_hcd_giveback_urb() or by
1721 * returning error from this function -> ergo we don't have to
1722 * refcount it.
1723 */
1724int wa_urb_enqueue(struct wahc *wa, struct usb_host_endpoint *ep,
1725 struct urb *urb, gfp_t gfp)
1726{
1727 int result;
1728 struct device *dev = &wa->usb_iface->dev;
1729 struct wa_xfer *xfer;
1730 unsigned long my_flags;
1731 unsigned cant_sleep = irqs_disabled() | in_atomic();
1732
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001733 if ((urb->transfer_buffer == NULL)
1734 && (urb->sg == NULL)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001735 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1736 && urb->transfer_buffer_length != 0) {
1737 dev_err(dev, "BUG? urb %p: NULL xfer buffer & NODMA\n", urb);
1738 dump_stack();
1739 }
1740
Thomas Puglieseb3744872013-11-25 16:17:16 -06001741 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1742 result = usb_hcd_link_urb_to_ep(&(wa->wusb->usb_hcd), urb);
1743 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
1744 if (result < 0)
1745 goto error_link_urb;
1746
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001747 result = -ENOMEM;
1748 xfer = kzalloc(sizeof(*xfer), gfp);
1749 if (xfer == NULL)
1750 goto error_kmalloc;
1751
1752 result = -ENOENT;
1753 if (urb->status != -EINPROGRESS) /* cancelled */
1754 goto error_dequeued; /* before starting? */
1755 wa_xfer_init(xfer);
1756 xfer->wa = wa_get(wa);
1757 xfer->urb = urb;
1758 xfer->gfp = gfp;
1759 xfer->ep = ep;
1760 urb->hcpriv = xfer;
David Vrabelbce83692008-12-22 18:22:50 +00001761
1762 dev_dbg(dev, "xfer %p urb %p pipe 0x%02x [%d bytes] %s %s %s\n",
1763 xfer, urb, urb->pipe, urb->transfer_buffer_length,
1764 urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? "dma" : "nodma",
1765 urb->pipe & USB_DIR_IN ? "inbound" : "outbound",
1766 cant_sleep ? "deferred" : "inline");
1767
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001768 if (cant_sleep) {
1769 usb_get_urb(urb);
1770 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1771 list_add_tail(&xfer->list_node, &wa->xfer_delayed_list);
1772 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001773 queue_work(wusbd, &wa->xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001774 } else {
Thomas Pugliese33186c42013-10-01 10:14:56 -05001775 result = wa_urb_enqueue_b(xfer);
1776 if (result < 0) {
1777 /*
1778 * URB submit/enqueue failed. Clean up, return an
1779 * error and do not run the callback. This avoids
1780 * an infinite submit/complete loop.
1781 */
1782 dev_err(dev, "%s: URB enqueue failed: %d\n",
1783 __func__, result);
1784 wa_put(xfer->wa);
1785 wa_xfer_put(xfer);
Thomas Puglieseb3744872013-11-25 16:17:16 -06001786 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1787 usb_hcd_unlink_urb_from_ep(&(wa->wusb->usb_hcd), urb);
1788 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001789 return result;
1790 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001791 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001792 return 0;
1793
1794error_dequeued:
1795 kfree(xfer);
1796error_kmalloc:
Thomas Puglieseb3744872013-11-25 16:17:16 -06001797 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1798 usb_hcd_unlink_urb_from_ep(&(wa->wusb->usb_hcd), urb);
1799 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
1800error_link_urb:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001801 return result;
1802}
1803EXPORT_SYMBOL_GPL(wa_urb_enqueue);
1804
1805/*
1806 * Dequeue a URB and make sure uwb_hcd_giveback_urb() [completion
1807 * handler] is called.
1808 *
1809 * Until a transfer goes successfully through wa_urb_enqueue() it
1810 * needs to be dequeued with completion calling; when stuck in delayed
1811 * or before wa_xfer_setup() is called, we need to do completion.
1812 *
1813 * not setup If there is no hcpriv yet, that means that that enqueue
1814 * still had no time to set the xfer up. Because
1815 * urb->status should be other than -EINPROGRESS,
1816 * enqueue() will catch that and bail out.
1817 *
1818 * If the transfer has gone through setup, we just need to clean it
1819 * up. If it has gone through submit(), we have to abort it [with an
1820 * asynch request] and then make sure we cancel each segment.
1821 *
1822 */
Thomas Puglieseb3744872013-11-25 16:17:16 -06001823int wa_urb_dequeue(struct wahc *wa, struct urb *urb, int status)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001824{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001825 unsigned long flags, flags2;
1826 struct wa_xfer *xfer;
1827 struct wa_seg *seg;
1828 struct wa_rpipe *rpipe;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001829 unsigned cnt, done = 0, xfer_abort_pending;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001830 unsigned rpipe_ready = 0;
Thomas Puglieseb3744872013-11-25 16:17:16 -06001831 int result;
1832
1833 /* check if it is safe to unlink. */
1834 spin_lock_irqsave(&wa->xfer_list_lock, flags);
1835 result = usb_hcd_check_unlink_urb(&(wa->wusb->usb_hcd), urb, status);
1836 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
1837 if (result)
1838 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001839
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001840 xfer = urb->hcpriv;
1841 if (xfer == NULL) {
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001842 /*
1843 * Nothing setup yet enqueue will see urb->status !=
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001844 * -EINPROGRESS (by hcd layer) and bail out with
1845 * error, no need to do completion
1846 */
1847 BUG_ON(urb->status == -EINPROGRESS);
1848 goto out;
1849 }
1850 spin_lock_irqsave(&xfer->lock, flags);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001851 pr_debug("%s: DEQUEUE xfer id 0x%08X\n", __func__, wa_xfer_id(xfer));
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001852 rpipe = xfer->ep->hcpriv;
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001853 if (rpipe == NULL) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001854 pr_debug("%s: xfer %p id 0x%08X has no RPIPE. %s",
1855 __func__, xfer, wa_xfer_id(xfer),
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001856 "Probably already aborted.\n" );
Thomas Pugliesee05a1fd2013-11-25 16:17:18 -06001857 result = -ENOENT;
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001858 goto out_unlock;
1859 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001860 /* Check the delayed list -> if there, release and complete */
1861 spin_lock_irqsave(&wa->xfer_list_lock, flags2);
1862 if (!list_empty(&xfer->list_node) && xfer->seg == NULL)
1863 goto dequeue_delayed;
1864 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1865 if (xfer->seg == NULL) /* still hasn't reached */
1866 goto out_unlock; /* setup(), enqueue_b() completes */
1867 /* Ok, the xfer is in flight already, it's been setup and submitted.*/
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001868 xfer_abort_pending = __wa_xfer_abort(xfer) >= 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001869 for (cnt = 0; cnt < xfer->segs; cnt++) {
1870 seg = xfer->seg[cnt];
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001871 pr_debug("%s: xfer id 0x%08X#%d status = %d\n",
1872 __func__, wa_xfer_id(xfer), cnt, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001873 switch (seg->status) {
1874 case WA_SEG_NOTREADY:
1875 case WA_SEG_READY:
1876 printk(KERN_ERR "xfer %p#%u: dequeue bad state %u\n",
1877 xfer, cnt, seg->status);
1878 WARN_ON(1);
1879 break;
1880 case WA_SEG_DELAYED:
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001881 /*
1882 * delete from rpipe delayed list. If no segments on
1883 * this xfer have been submitted, __wa_xfer_is_done will
1884 * trigger a giveback below. Otherwise, the submitted
1885 * segments will be completed in the DTI interrupt.
1886 */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001887 seg->status = WA_SEG_ABORTED;
Thomas Pugliesee05a1fd2013-11-25 16:17:18 -06001888 seg->result = -ENOENT;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001889 spin_lock_irqsave(&rpipe->seg_lock, flags2);
1890 list_del(&seg->list_node);
1891 xfer->segs_done++;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001892 spin_unlock_irqrestore(&rpipe->seg_lock, flags2);
1893 break;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001894 case WA_SEG_DONE:
1895 case WA_SEG_ERROR:
1896 case WA_SEG_ABORTED:
1897 break;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001898 /*
1899 * In the states below, the HWA device already knows
1900 * about the transfer. If an abort request was sent,
1901 * allow the HWA to process it and wait for the
1902 * results. Otherwise, the DTI state and seg completed
1903 * counts can get out of sync.
1904 */
1905 case WA_SEG_SUBMITTED:
1906 case WA_SEG_PENDING:
1907 case WA_SEG_DTI_PENDING:
1908 /*
1909 * Check if the abort was successfully sent. This could
1910 * be false if the HWA has been removed but we haven't
1911 * gotten the disconnect notification yet.
1912 */
1913 if (!xfer_abort_pending) {
1914 seg->status = WA_SEG_ABORTED;
1915 rpipe_ready = rpipe_avail_inc(rpipe);
1916 xfer->segs_done++;
1917 }
1918 break;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001919 }
1920 }
1921 xfer->result = urb->status; /* -ENOENT or -ECONNRESET */
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001922 done = __wa_xfer_is_done(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001923 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001924 if (done)
1925 wa_xfer_completion(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001926 if (rpipe_ready)
1927 wa_xfer_delayed_run(rpipe);
Thomas Pugliesee05a1fd2013-11-25 16:17:18 -06001928 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001929
1930out_unlock:
1931 spin_unlock_irqrestore(&xfer->lock, flags);
1932out:
Thomas Pugliesee05a1fd2013-11-25 16:17:18 -06001933 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001934
1935dequeue_delayed:
1936 list_del_init(&xfer->list_node);
1937 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1938 xfer->result = urb->status;
1939 spin_unlock_irqrestore(&xfer->lock, flags);
1940 wa_xfer_giveback(xfer);
1941 usb_put_urb(urb); /* we got a ref in enqueue() */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001942 return 0;
1943}
1944EXPORT_SYMBOL_GPL(wa_urb_dequeue);
1945
1946/*
1947 * Translation from WA status codes (WUSB1.0 Table 8.15) to errno
1948 * codes
1949 *
1950 * Positive errno values are internal inconsistencies and should be
1951 * flagged louder. Negative are to be passed up to the user in the
1952 * normal way.
1953 *
1954 * @status: USB WA status code -- high two bits are stripped.
1955 */
1956static int wa_xfer_status_to_errno(u8 status)
1957{
1958 int errno;
1959 u8 real_status = status;
1960 static int xlat[] = {
1961 [WA_XFER_STATUS_SUCCESS] = 0,
1962 [WA_XFER_STATUS_HALTED] = -EPIPE,
1963 [WA_XFER_STATUS_DATA_BUFFER_ERROR] = -ENOBUFS,
1964 [WA_XFER_STATUS_BABBLE] = -EOVERFLOW,
1965 [WA_XFER_RESERVED] = EINVAL,
1966 [WA_XFER_STATUS_NOT_FOUND] = 0,
1967 [WA_XFER_STATUS_INSUFFICIENT_RESOURCE] = -ENOMEM,
1968 [WA_XFER_STATUS_TRANSACTION_ERROR] = -EILSEQ,
Thomas Pugliesee05a1fd2013-11-25 16:17:18 -06001969 [WA_XFER_STATUS_ABORTED] = -ENOENT,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001970 [WA_XFER_STATUS_RPIPE_NOT_READY] = EINVAL,
1971 [WA_XFER_INVALID_FORMAT] = EINVAL,
1972 [WA_XFER_UNEXPECTED_SEGMENT_NUMBER] = EINVAL,
1973 [WA_XFER_STATUS_RPIPE_TYPE_MISMATCH] = EINVAL,
1974 };
1975 status &= 0x3f;
1976
1977 if (status == 0)
1978 return 0;
1979 if (status >= ARRAY_SIZE(xlat)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02001980 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001981 "Unknown WA transfer status 0x%02x\n",
1982 __func__, real_status);
1983 return -EINVAL;
1984 }
1985 errno = xlat[status];
1986 if (unlikely(errno > 0)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02001987 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001988 "Inconsistent WA status: 0x%02x\n",
1989 __func__, real_status);
1990 errno = -errno;
1991 }
1992 return errno;
1993}
1994
1995/*
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001996 * If a last segment flag and/or a transfer result error is encountered,
1997 * no other segment transfer results will be returned from the device.
1998 * Mark the remaining submitted or pending xfers as completed so that
1999 * the xfer will complete cleanly.
2000 */
2001static void wa_complete_remaining_xfer_segs(struct wa_xfer *xfer,
Thomas Pugliese70052342013-12-09 13:10:41 -06002002 struct wa_seg *incoming_seg, enum wa_seg_status status)
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002003{
2004 int index;
2005 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
2006
2007 for (index = incoming_seg->index + 1; index < xfer->segs_submitted;
2008 index++) {
2009 struct wa_seg *current_seg = xfer->seg[index];
2010
2011 BUG_ON(current_seg == NULL);
2012
2013 switch (current_seg->status) {
2014 case WA_SEG_SUBMITTED:
2015 case WA_SEG_PENDING:
2016 case WA_SEG_DTI_PENDING:
2017 rpipe_avail_inc(rpipe);
2018 /*
2019 * do not increment RPIPE avail for the WA_SEG_DELAYED case
2020 * since it has not been submitted to the RPIPE.
2021 */
2022 case WA_SEG_DELAYED:
2023 xfer->segs_done++;
Thomas Pugliese70052342013-12-09 13:10:41 -06002024 current_seg->status = status;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002025 break;
2026 case WA_SEG_ABORTED:
2027 break;
2028 default:
2029 WARN(1, "%s: xfer 0x%08X#%d. bad seg status = %d\n",
2030 __func__, wa_xfer_id(xfer), index,
2031 current_seg->status);
2032 break;
2033 }
2034 }
2035}
2036
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002037/* Populate the wa->buf_in_urb based on the current isoc transfer state. */
2038static void __wa_populate_buf_in_urb_isoc(struct wahc *wa, struct wa_xfer *xfer,
2039 struct wa_seg *seg, int curr_iso_frame)
2040{
2041 BUG_ON(wa->buf_in_urb->status == -EINPROGRESS);
2042
2043 /* this should always be 0 before a resubmit. */
2044 wa->buf_in_urb->num_mapped_sgs = 0;
2045 wa->buf_in_urb->transfer_dma = xfer->urb->transfer_dma +
2046 xfer->urb->iso_frame_desc[curr_iso_frame].offset;
2047 wa->buf_in_urb->transfer_buffer_length =
2048 xfer->urb->iso_frame_desc[curr_iso_frame].length;
2049 wa->buf_in_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2050 wa->buf_in_urb->transfer_buffer = NULL;
2051 wa->buf_in_urb->sg = NULL;
2052 wa->buf_in_urb->num_sgs = 0;
2053 wa->buf_in_urb->context = seg;
2054}
2055
Thomas Pugliese70052342013-12-09 13:10:41 -06002056/* Populate the wa->buf_in_urb based on the current transfer state. */
2057static int wa_populate_buf_in_urb(struct wahc *wa, struct wa_xfer *xfer,
2058 unsigned int seg_idx, unsigned int bytes_transferred)
2059{
2060 int result = 0;
2061 struct wa_seg *seg = xfer->seg[seg_idx];
2062
2063 BUG_ON(wa->buf_in_urb->status == -EINPROGRESS);
2064 /* this should always be 0 before a resubmit. */
2065 wa->buf_in_urb->num_mapped_sgs = 0;
2066
2067 if (xfer->is_dma) {
2068 wa->buf_in_urb->transfer_dma = xfer->urb->transfer_dma
2069 + (seg_idx * xfer->seg_size);
2070 wa->buf_in_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2071 wa->buf_in_urb->transfer_buffer = NULL;
2072 wa->buf_in_urb->sg = NULL;
2073 wa->buf_in_urb->num_sgs = 0;
2074 } else {
2075 /* do buffer or SG processing. */
2076 wa->buf_in_urb->transfer_flags &= ~URB_NO_TRANSFER_DMA_MAP;
2077
2078 if (xfer->urb->transfer_buffer) {
2079 wa->buf_in_urb->transfer_buffer =
2080 xfer->urb->transfer_buffer
2081 + (seg_idx * xfer->seg_size);
2082 wa->buf_in_urb->sg = NULL;
2083 wa->buf_in_urb->num_sgs = 0;
2084 } else {
2085 /* allocate an SG list to store seg_size bytes
2086 and copy the subset of the xfer->urb->sg
2087 that matches the buffer subset we are
2088 about to read. */
2089 wa->buf_in_urb->sg = wa_xfer_create_subset_sg(
2090 xfer->urb->sg,
2091 seg_idx * xfer->seg_size,
2092 bytes_transferred,
2093 &(wa->buf_in_urb->num_sgs));
2094
2095 if (!(wa->buf_in_urb->sg)) {
2096 wa->buf_in_urb->num_sgs = 0;
2097 result = -ENOMEM;
2098 }
2099 wa->buf_in_urb->transfer_buffer = NULL;
2100 }
2101 }
2102 wa->buf_in_urb->transfer_buffer_length = bytes_transferred;
2103 wa->buf_in_urb->context = seg;
2104
2105 return result;
2106}
2107
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002108/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002109 * Process a xfer result completion message
2110 *
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002111 * inbound transfers: need to schedule a buf_in_urb read
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002112 *
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002113 * FIXME: this function needs to be broken up in parts
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002114 */
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002115static void wa_xfer_result_chew(struct wahc *wa, struct wa_xfer *xfer,
2116 struct wa_xfer_result *xfer_result)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002117{
2118 int result;
2119 struct device *dev = &wa->usb_iface->dev;
2120 unsigned long flags;
Thomas Pugliese70052342013-12-09 13:10:41 -06002121 unsigned int seg_idx;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002122 struct wa_seg *seg;
2123 struct wa_rpipe *rpipe;
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002124 unsigned done = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002125 u8 usb_status;
2126 unsigned rpipe_ready = 0;
Thomas Pugliese70052342013-12-09 13:10:41 -06002127 unsigned bytes_transferred = le32_to_cpu(xfer_result->dwTransferLength);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002128
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002129 spin_lock_irqsave(&xfer->lock, flags);
2130 seg_idx = xfer_result->bTransferSegment & 0x7f;
2131 if (unlikely(seg_idx >= xfer->segs))
2132 goto error_bad_seg;
2133 seg = xfer->seg[seg_idx];
2134 rpipe = xfer->ep->hcpriv;
2135 usb_status = xfer_result->bTransferStatus;
Thomas Puglieseb9c84be2013-09-27 15:33:36 -05002136 dev_dbg(dev, "xfer %p ID 0x%08X#%u: bTransferStatus 0x%02x (seg status %u)\n",
2137 xfer, wa_xfer_id(xfer), seg_idx, usb_status, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002138 if (seg->status == WA_SEG_ABORTED
2139 || seg->status == WA_SEG_ERROR) /* already handled */
2140 goto segment_aborted;
2141 if (seg->status == WA_SEG_SUBMITTED) /* ops, got here */
2142 seg->status = WA_SEG_PENDING; /* before wa_seg{_dto}_cb() */
2143 if (seg->status != WA_SEG_PENDING) {
2144 if (printk_ratelimit())
2145 dev_err(dev, "xfer %p#%u: Bad segment state %u\n",
2146 xfer, seg_idx, seg->status);
2147 seg->status = WA_SEG_PENDING; /* workaround/"fix" it */
2148 }
2149 if (usb_status & 0x80) {
2150 seg->result = wa_xfer_status_to_errno(usb_status);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002151 dev_err(dev, "DTI: xfer %p#:%08X:%u failed (0x%02x)\n",
2152 xfer, xfer->id, seg->index, usb_status);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002153 seg->status = ((usb_status & 0x7F) == WA_XFER_STATUS_ABORTED) ?
2154 WA_SEG_ABORTED : WA_SEG_ERROR;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002155 goto error_complete;
2156 }
2157 /* FIXME: we ignore warnings, tally them for stats */
2158 if (usb_status & 0x40) /* Warning?... */
2159 usb_status = 0; /* ... pass */
Thomas Pugliese70052342013-12-09 13:10:41 -06002160 /*
2161 * If the last segment bit is set, complete the remaining segments.
2162 * When the current segment is completed, either in wa_buf_in_cb for
2163 * transfers with data or below for no data, the xfer will complete.
2164 */
2165 if (xfer_result->bTransferSegment & 0x80)
2166 wa_complete_remaining_xfer_segs(xfer, seg, WA_SEG_DONE);
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002167 if (usb_pipeisoc(xfer->urb->pipe)
2168 && (le32_to_cpu(xfer_result->dwNumOfPackets) > 0)) {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002169 /* set up WA state to read the isoc packet status next. */
2170 wa->dti_isoc_xfer_in_progress = wa_xfer_id(xfer);
2171 wa->dti_isoc_xfer_seg = seg_idx;
2172 wa->dti_state = WA_DTI_ISOC_PACKET_STATUS_PENDING;
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002173 } else if (xfer->is_inbound && !usb_pipeisoc(xfer->urb->pipe)
Thomas Pugliese70052342013-12-09 13:10:41 -06002174 && (bytes_transferred > 0)) {
2175 /* IN data phase: read to buffer */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002176 seg->status = WA_SEG_DTI_PENDING;
Thomas Pugliese70052342013-12-09 13:10:41 -06002177 result = wa_populate_buf_in_urb(wa, xfer, seg_idx,
2178 bytes_transferred);
2179 if (result < 0)
2180 goto error_buf_in_populate;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002181 result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
2182 if (result < 0)
2183 goto error_submit_buf_in;
2184 } else {
Thomas Pugliese70052342013-12-09 13:10:41 -06002185 /* OUT data phase or no data, complete it -- */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002186 seg->status = WA_SEG_DONE;
Thomas Pugliese70052342013-12-09 13:10:41 -06002187 seg->result = bytes_transferred;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002188 xfer->segs_done++;
2189 rpipe_ready = rpipe_avail_inc(rpipe);
2190 done = __wa_xfer_is_done(xfer);
2191 }
2192 spin_unlock_irqrestore(&xfer->lock, flags);
2193 if (done)
2194 wa_xfer_completion(xfer);
2195 if (rpipe_ready)
2196 wa_xfer_delayed_run(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002197 return;
2198
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002199error_submit_buf_in:
2200 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
2201 dev_err(dev, "DTI: URB max acceptable errors "
2202 "exceeded, resetting device\n");
2203 wa_reset_all(wa);
2204 }
2205 if (printk_ratelimit())
2206 dev_err(dev, "xfer %p#%u: can't submit DTI data phase: %d\n",
2207 xfer, seg_idx, result);
2208 seg->result = result;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002209 kfree(wa->buf_in_urb->sg);
Thomas Pugliese67414482013-09-26 14:08:16 -05002210 wa->buf_in_urb->sg = NULL;
Thomas Pugliese70052342013-12-09 13:10:41 -06002211error_buf_in_populate:
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002212 __wa_xfer_abort(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002213 seg->status = WA_SEG_ERROR;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002214error_complete:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002215 xfer->segs_done++;
2216 rpipe_ready = rpipe_avail_inc(rpipe);
Thomas Pugliese70052342013-12-09 13:10:41 -06002217 wa_complete_remaining_xfer_segs(xfer, seg, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002218 done = __wa_xfer_is_done(xfer);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002219 /*
2220 * queue work item to clear STALL for control endpoints.
2221 * Otherwise, let endpoint_reset take care of it.
2222 */
2223 if (((usb_status & 0x3f) == WA_XFER_STATUS_HALTED) &&
2224 usb_endpoint_xfer_control(&xfer->ep->desc) &&
2225 done) {
2226
2227 dev_info(dev, "Control EP stall. Queue delayed work.\n");
2228 spin_lock_irq(&wa->xfer_list_lock);
Wei Yongjun8eb41292013-09-23 14:16:22 +08002229 /* move xfer from xfer_list to xfer_errored_list. */
2230 list_move_tail(&xfer->list_node, &wa->xfer_errored_list);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002231 spin_unlock_irq(&wa->xfer_list_lock);
2232 spin_unlock_irqrestore(&xfer->lock, flags);
2233 queue_work(wusbd, &wa->xfer_error_work);
2234 } else {
2235 spin_unlock_irqrestore(&xfer->lock, flags);
2236 if (done)
2237 wa_xfer_completion(xfer);
2238 if (rpipe_ready)
2239 wa_xfer_delayed_run(rpipe);
2240 }
2241
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002242 return;
2243
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002244error_bad_seg:
2245 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Puglieseb3744872013-11-25 16:17:16 -06002246 wa_urb_dequeue(wa, xfer->urb, -ENOENT);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002247 if (printk_ratelimit())
2248 dev_err(dev, "xfer %p#%u: bad segment\n", xfer, seg_idx);
2249 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
2250 dev_err(dev, "DTI: URB max acceptable errors "
2251 "exceeded, resetting device\n");
2252 wa_reset_all(wa);
2253 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002254 return;
2255
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002256segment_aborted:
2257 /* nothing to do, as the aborter did the completion */
2258 spin_unlock_irqrestore(&xfer->lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002259}
2260
2261/*
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002262 * Process a isochronous packet status message
2263 *
2264 * inbound transfers: need to schedule a buf_in_urb read
2265 */
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002266static int wa_process_iso_packet_status(struct wahc *wa, struct urb *urb)
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002267{
2268 struct device *dev = &wa->usb_iface->dev;
2269 struct wa_xfer_packet_status_hwaiso *packet_status;
Thomas Pugliese21012422013-10-23 14:44:27 -05002270 struct wa_xfer_packet_status_len_hwaiso *status_array;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002271 struct wa_xfer *xfer;
2272 unsigned long flags;
2273 struct wa_seg *seg;
2274 struct wa_rpipe *rpipe;
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002275 unsigned done = 0, dti_busy = 0, data_frame_count = 0, seg_index;
2276 unsigned first_frame_index = 0, rpipe_ready = 0;
Thomas Pugliese21012422013-10-23 14:44:27 -05002277 int expected_size;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002278
2279 /* We have a xfer result buffer; check it */
2280 dev_dbg(dev, "DTI: isoc packet status %d bytes at %p\n",
2281 urb->actual_length, urb->transfer_buffer);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002282 packet_status = (struct wa_xfer_packet_status_hwaiso *)(wa->dti_buf);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002283 if (packet_status->bPacketType != WA_XFER_ISO_PACKET_STATUS) {
2284 dev_err(dev, "DTI Error: isoc packet status--bad type 0x%02x\n",
2285 packet_status->bPacketType);
2286 goto error_parse_buffer;
2287 }
2288 xfer = wa_xfer_get_by_id(wa, wa->dti_isoc_xfer_in_progress);
2289 if (xfer == NULL) {
2290 dev_err(dev, "DTI Error: isoc packet status--unknown xfer 0x%08x\n",
2291 wa->dti_isoc_xfer_in_progress);
2292 goto error_parse_buffer;
2293 }
2294 spin_lock_irqsave(&xfer->lock, flags);
2295 if (unlikely(wa->dti_isoc_xfer_seg >= xfer->segs))
2296 goto error_bad_seg;
2297 seg = xfer->seg[wa->dti_isoc_xfer_seg];
2298 rpipe = xfer->ep->hcpriv;
Thomas Pugliese21012422013-10-23 14:44:27 -05002299 expected_size = sizeof(*packet_status) +
2300 (sizeof(packet_status->PacketStatus[0]) *
2301 seg->isoc_frame_count);
2302 if (urb->actual_length != expected_size) {
2303 dev_err(dev, "DTI Error: isoc packet status--bad urb length (%d bytes vs %d needed)\n",
2304 urb->actual_length, expected_size);
2305 goto error_bad_seg;
2306 }
2307 if (le16_to_cpu(packet_status->wLength) != expected_size) {
2308 dev_err(dev, "DTI Error: isoc packet status--bad length %u\n",
2309 le16_to_cpu(packet_status->wLength));
2310 goto error_bad_seg;
2311 }
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002312 /* write isoc packet status and lengths back to the xfer urb. */
Thomas Pugliese21012422013-10-23 14:44:27 -05002313 status_array = packet_status->PacketStatus;
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002314 xfer->urb->start_frame =
2315 wa->wusb->usb_hcd.driver->get_frame_number(&wa->wusb->usb_hcd);
Thomas Pugliese21012422013-10-23 14:44:27 -05002316 for (seg_index = 0; seg_index < seg->isoc_frame_count; ++seg_index) {
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002317 struct usb_iso_packet_descriptor *iso_frame_desc =
2318 xfer->urb->iso_frame_desc;
2319 const int urb_frame_index =
2320 seg->isoc_frame_offset + seg_index;
2321
2322 iso_frame_desc[urb_frame_index].status =
Thomas Pugliese21012422013-10-23 14:44:27 -05002323 wa_xfer_status_to_errno(
2324 le16_to_cpu(status_array[seg_index].PacketStatus));
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002325 iso_frame_desc[urb_frame_index].actual_length =
Thomas Pugliese21012422013-10-23 14:44:27 -05002326 le16_to_cpu(status_array[seg_index].PacketLength);
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002327 /* track the number of frames successfully transferred. */
2328 if (iso_frame_desc[urb_frame_index].actual_length > 0) {
2329 /* save the starting frame index for buf_in_urb. */
2330 if (!data_frame_count)
2331 first_frame_index = seg_index;
2332 ++data_frame_count;
2333 }
Thomas Pugliese21012422013-10-23 14:44:27 -05002334 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002335
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002336 if (xfer->is_inbound && data_frame_count) {
2337 int result;
2338
2339 seg->isoc_frame_index = first_frame_index;
2340 /* submit a read URB for the first frame with data. */
2341 __wa_populate_buf_in_urb_isoc(wa, xfer, seg,
2342 seg->isoc_frame_index + seg->isoc_frame_offset);
2343
2344 result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
2345 if (result < 0) {
2346 dev_err(dev, "DTI Error: Could not submit buf in URB (%d)",
2347 result);
2348 wa_reset_all(wa);
2349 } else if (data_frame_count > 1)
2350 /* If we need to read multiple frames, set DTI busy. */
2351 dti_busy = 1;
2352 } else {
2353 /* OUT transfer or no more IN data, complete it -- */
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002354 seg->status = WA_SEG_DONE;
2355 xfer->segs_done++;
2356 rpipe_ready = rpipe_avail_inc(rpipe);
2357 done = __wa_xfer_is_done(xfer);
2358 }
2359 spin_unlock_irqrestore(&xfer->lock, flags);
2360 wa->dti_state = WA_DTI_TRANSFER_RESULT_PENDING;
2361 if (done)
2362 wa_xfer_completion(xfer);
2363 if (rpipe_ready)
2364 wa_xfer_delayed_run(rpipe);
2365 wa_xfer_put(xfer);
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002366 return dti_busy;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002367
2368error_bad_seg:
2369 spin_unlock_irqrestore(&xfer->lock, flags);
2370 wa_xfer_put(xfer);
2371error_parse_buffer:
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002372 return dti_busy;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002373}
2374
2375/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002376 * Callback for the IN data phase
2377 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002378 * If successful transition state; otherwise, take a note of the
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002379 * error, mark this segment done and try completion.
2380 *
2381 * Note we don't access until we are sure that the transfer hasn't
2382 * been cancelled (ECONNRESET, ENOENT), which could mean that
2383 * seg->xfer could be already gone.
2384 */
2385static void wa_buf_in_cb(struct urb *urb)
2386{
2387 struct wa_seg *seg = urb->context;
2388 struct wa_xfer *xfer = seg->xfer;
2389 struct wahc *wa;
2390 struct device *dev;
2391 struct wa_rpipe *rpipe;
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002392 unsigned rpipe_ready = 0, seg_index, isoc_data_frame_count = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002393 unsigned long flags;
2394 u8 done = 0;
2395
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002396 /* free the sg if it was used. */
2397 kfree(urb->sg);
2398 urb->sg = NULL;
2399
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002400 spin_lock_irqsave(&xfer->lock, flags);
2401 wa = xfer->wa;
2402 dev = &wa->usb_iface->dev;
2403
2404 if (usb_pipeisoc(xfer->urb->pipe)) {
2405 /*
2406 * Find the next isoc frame with data. Bail out after
2407 * isoc_data_frame_count > 1 since there is no need to walk
2408 * the entire frame array. We just need to know if
2409 * isoc_data_frame_count is 0, 1, or >1.
2410 */
2411 seg_index = seg->isoc_frame_index + 1;
2412 while ((seg_index < seg->isoc_frame_count)
2413 && (isoc_data_frame_count <= 1)) {
2414 struct usb_iso_packet_descriptor *iso_frame_desc =
2415 xfer->urb->iso_frame_desc;
2416 const int urb_frame_index =
2417 seg->isoc_frame_offset + seg_index;
2418
2419 if (iso_frame_desc[urb_frame_index].actual_length > 0) {
2420 /* save the index of the next frame with data */
2421 if (!isoc_data_frame_count)
2422 seg->isoc_frame_index = seg_index;
2423 ++isoc_data_frame_count;
2424 }
2425 ++seg_index;
2426 }
2427 }
2428 spin_unlock_irqrestore(&xfer->lock, flags);
2429
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002430 switch (urb->status) {
2431 case 0:
2432 spin_lock_irqsave(&xfer->lock, flags);
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002433
2434 seg->result += urb->actual_length;
2435 if (isoc_data_frame_count > 0) {
2436 int result;
2437 /* submit a read URB for the first frame with data. */
2438 __wa_populate_buf_in_urb_isoc(wa, xfer, seg,
2439 seg->isoc_frame_index + seg->isoc_frame_offset);
2440 result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
2441 if (result < 0) {
2442 dev_err(dev, "DTI Error: Could not submit buf in URB (%d)",
2443 result);
2444 wa_reset_all(wa);
2445 }
2446 } else {
2447 rpipe = xfer->ep->hcpriv;
2448 seg->status = WA_SEG_DONE;
2449 dev_dbg(dev, "xfer %p#%u: data in done (%zu bytes)\n",
2450 xfer, seg->index, seg->result);
2451 xfer->segs_done++;
2452 rpipe_ready = rpipe_avail_inc(rpipe);
2453 done = __wa_xfer_is_done(xfer);
2454 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002455 spin_unlock_irqrestore(&xfer->lock, flags);
2456 if (done)
2457 wa_xfer_completion(xfer);
2458 if (rpipe_ready)
2459 wa_xfer_delayed_run(rpipe);
2460 break;
2461 case -ECONNRESET: /* URB unlinked; no need to do anything */
2462 case -ENOENT: /* as it was done by the who unlinked us */
2463 break;
2464 default: /* Other errors ... */
2465 spin_lock_irqsave(&xfer->lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002466 rpipe = xfer->ep->hcpriv;
2467 if (printk_ratelimit())
2468 dev_err(dev, "xfer %p#%u: data in error %d\n",
2469 xfer, seg->index, urb->status);
2470 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
2471 EDC_ERROR_TIMEFRAME)){
2472 dev_err(dev, "DTO: URB max acceptable errors "
2473 "exceeded, resetting device\n");
2474 wa_reset_all(wa);
2475 }
2476 seg->status = WA_SEG_ERROR;
2477 seg->result = urb->status;
2478 xfer->segs_done++;
2479 rpipe_ready = rpipe_avail_inc(rpipe);
2480 __wa_xfer_abort(xfer);
2481 done = __wa_xfer_is_done(xfer);
2482 spin_unlock_irqrestore(&xfer->lock, flags);
2483 if (done)
2484 wa_xfer_completion(xfer);
2485 if (rpipe_ready)
2486 wa_xfer_delayed_run(rpipe);
2487 }
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002488 /*
2489 * If we are in this callback and isoc_data_frame_count > 0, it means
2490 * that the dti_urb submission was delayed in wa_dti_cb. Once
2491 * isoc_data_frame_count gets to 1, we can submit the deferred URB
2492 * since the last buf_in_urb was just submitted.
2493 */
2494 if (isoc_data_frame_count == 1) {
2495 int result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
2496 if (result < 0) {
2497 dev_err(dev, "DTI Error: Could not submit DTI URB (%d)\n",
2498 result);
2499 wa_reset_all(wa);
2500 }
2501 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002502}
2503
2504/*
2505 * Handle an incoming transfer result buffer
2506 *
2507 * Given a transfer result buffer, it completes the transfer (possibly
2508 * scheduling and buffer in read) and then resubmits the DTI URB for a
2509 * new transfer result read.
2510 *
2511 *
2512 * The xfer_result DTI URB state machine
2513 *
2514 * States: OFF | RXR (Read-Xfer-Result) | RBI (Read-Buffer-In)
2515 *
2516 * We start in OFF mode, the first xfer_result notification [through
2517 * wa_handle_notif_xfer()] moves us to RXR by posting the DTI-URB to
2518 * read.
2519 *
2520 * We receive a buffer -- if it is not a xfer_result, we complain and
2521 * repost the DTI-URB. If it is a xfer_result then do the xfer seg
2522 * request accounting. If it is an IN segment, we move to RBI and post
2523 * a BUF-IN-URB to the right buffer. The BUF-IN-URB callback will
2524 * repost the DTI-URB and move to RXR state. if there was no IN
2525 * segment, it will repost the DTI-URB.
2526 *
2527 * We go back to OFF when we detect a ENOENT or ESHUTDOWN (or too many
2528 * errors) in the URBs.
2529 */
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002530static void wa_dti_cb(struct urb *urb)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002531{
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002532 int result, dti_busy = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002533 struct wahc *wa = urb->context;
2534 struct device *dev = &wa->usb_iface->dev;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002535 u32 xfer_id;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002536 u8 usb_status;
2537
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002538 BUG_ON(wa->dti_urb != urb);
2539 switch (wa->dti_urb->status) {
2540 case 0:
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002541 if (wa->dti_state == WA_DTI_TRANSFER_RESULT_PENDING) {
2542 struct wa_xfer_result *xfer_result;
2543 struct wa_xfer *xfer;
2544
2545 /* We have a xfer result buffer; check it */
2546 dev_dbg(dev, "DTI: xfer result %d bytes at %p\n",
2547 urb->actual_length, urb->transfer_buffer);
2548 if (urb->actual_length != sizeof(*xfer_result)) {
2549 dev_err(dev, "DTI Error: xfer result--bad size xfer result (%d bytes vs %zu needed)\n",
2550 urb->actual_length,
2551 sizeof(*xfer_result));
2552 break;
2553 }
2554 xfer_result = (struct wa_xfer_result *)(wa->dti_buf);
2555 if (xfer_result->hdr.bLength != sizeof(*xfer_result)) {
2556 dev_err(dev, "DTI Error: xfer result--bad header length %u\n",
2557 xfer_result->hdr.bLength);
2558 break;
2559 }
2560 if (xfer_result->hdr.bNotifyType != WA_XFER_RESULT) {
2561 dev_err(dev, "DTI Error: xfer result--bad header type 0x%02x\n",
2562 xfer_result->hdr.bNotifyType);
2563 break;
2564 }
2565 usb_status = xfer_result->bTransferStatus & 0x3f;
2566 if (usb_status == WA_XFER_STATUS_NOT_FOUND)
2567 /* taken care of already */
2568 break;
2569 xfer_id = le32_to_cpu(xfer_result->dwTransferID);
2570 xfer = wa_xfer_get_by_id(wa, xfer_id);
2571 if (xfer == NULL) {
2572 /* FIXME: transaction not found. */
2573 dev_err(dev, "DTI Error: xfer result--unknown xfer 0x%08x (status 0x%02x)\n",
2574 xfer_id, usb_status);
2575 break;
2576 }
2577 wa_xfer_result_chew(wa, xfer, xfer_result);
2578 wa_xfer_put(xfer);
2579 } else if (wa->dti_state == WA_DTI_ISOC_PACKET_STATUS_PENDING) {
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002580 dti_busy = wa_process_iso_packet_status(wa, urb);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002581 } else {
2582 dev_err(dev, "DTI Error: unexpected EP state = %d\n",
2583 wa->dti_state);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002584 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002585 break;
2586 case -ENOENT: /* (we killed the URB)...so, no broadcast */
2587 case -ESHUTDOWN: /* going away! */
2588 dev_dbg(dev, "DTI: going down! %d\n", urb->status);
2589 goto out;
2590 default:
2591 /* Unknown error */
2592 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS,
2593 EDC_ERROR_TIMEFRAME)) {
2594 dev_err(dev, "DTI: URB max acceptable errors "
2595 "exceeded, resetting device\n");
2596 wa_reset_all(wa);
2597 goto out;
2598 }
2599 if (printk_ratelimit())
2600 dev_err(dev, "DTI: URB error %d\n", urb->status);
2601 break;
2602 }
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002603
2604 /* Resubmit the DTI URB if we are not busy processing isoc in frames. */
2605 if (!dti_busy) {
2606 result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
2607 if (result < 0) {
2608 dev_err(dev, "DTI Error: Could not submit DTI URB (%d)\n",
2609 result);
2610 wa_reset_all(wa);
2611 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002612 }
2613out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002614 return;
2615}
2616
2617/*
2618 * Transfer complete notification
2619 *
2620 * Called from the notif.c code. We get a notification on EP2 saying
2621 * that some endpoint has some transfer result data available. We are
2622 * about to read it.
2623 *
2624 * To speed up things, we always have a URB reading the DTI URB; we
2625 * don't really set it up and start it until the first xfer complete
2626 * notification arrives, which is what we do here.
2627 *
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002628 * Follow up in wa_dti_cb(), as that's where the whole state
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002629 * machine starts.
2630 *
2631 * So here we just initialize the DTI URB for reading transfer result
2632 * notifications and also the buffer-in URB, for reading buffers. Then
2633 * we just submit the DTI URB.
2634 *
2635 * @wa shall be referenced
2636 */
2637void wa_handle_notif_xfer(struct wahc *wa, struct wa_notif_hdr *notif_hdr)
2638{
2639 int result;
2640 struct device *dev = &wa->usb_iface->dev;
2641 struct wa_notif_xfer *notif_xfer;
2642 const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd;
2643
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002644 notif_xfer = container_of(notif_hdr, struct wa_notif_xfer, hdr);
2645 BUG_ON(notif_hdr->bNotifyType != WA_NOTIF_TRANSFER);
2646
2647 if ((0x80 | notif_xfer->bEndpoint) != dti_epd->bEndpointAddress) {
2648 /* FIXME: hardcoded limitation, adapt */
2649 dev_err(dev, "BUG: DTI ep is %u, not %u (hack me)\n",
2650 notif_xfer->bEndpoint, dti_epd->bEndpointAddress);
2651 goto error;
2652 }
2653 if (wa->dti_urb != NULL) /* DTI URB already started */
2654 goto out;
2655
2656 wa->dti_urb = usb_alloc_urb(0, GFP_KERNEL);
2657 if (wa->dti_urb == NULL) {
2658 dev_err(dev, "Can't allocate DTI URB\n");
2659 goto error_dti_urb_alloc;
2660 }
2661 usb_fill_bulk_urb(
2662 wa->dti_urb, wa->usb_dev,
2663 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002664 wa->dti_buf, wa->dti_buf_size,
2665 wa_dti_cb, wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002666
2667 wa->buf_in_urb = usb_alloc_urb(0, GFP_KERNEL);
2668 if (wa->buf_in_urb == NULL) {
2669 dev_err(dev, "Can't allocate BUF-IN URB\n");
2670 goto error_buf_in_urb_alloc;
2671 }
2672 usb_fill_bulk_urb(
2673 wa->buf_in_urb, wa->usb_dev,
2674 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
2675 NULL, 0, wa_buf_in_cb, wa);
2676 result = usb_submit_urb(wa->dti_urb, GFP_KERNEL);
2677 if (result < 0) {
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002678 dev_err(dev, "DTI Error: Could not submit DTI URB (%d) resetting\n",
2679 result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002680 goto error_dti_urb_submit;
2681 }
2682out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002683 return;
2684
2685error_dti_urb_submit:
2686 usb_put_urb(wa->buf_in_urb);
Thomas Pugliese67414482013-09-26 14:08:16 -05002687 wa->buf_in_urb = NULL;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002688error_buf_in_urb_alloc:
2689 usb_put_urb(wa->dti_urb);
2690 wa->dti_urb = NULL;
2691error_dti_urb_alloc:
2692error:
2693 wa_reset_all(wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002694}