blob: 3220c62de31740f10e0fa7161ed014553925b2d1 [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. */
127 int isoc_size; /* size of all isoc frames sent by this seg. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100128 enum wa_seg_status status;
129 ssize_t result; /* bytes xfered or error */
130 struct wa_xfer_hdr xfer_hdr;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100131};
132
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500133static inline void wa_seg_init(struct wa_seg *seg)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100134{
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500135 usb_init_urb(&seg->tr_urb);
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500136
137 /* set the remaining memory to 0. */
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500138 memset(((void *)seg) + sizeof(seg->tr_urb), 0,
139 sizeof(*seg) - sizeof(seg->tr_urb));
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100140}
141
142/*
143 * Protected by xfer->lock
144 *
145 */
146struct wa_xfer {
147 struct kref refcnt;
148 struct list_head list_node;
149 spinlock_t lock;
150 u32 id;
151
152 struct wahc *wa; /* Wire adapter we are plugged to */
153 struct usb_host_endpoint *ep;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300154 struct urb *urb; /* URB we are transferring for */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100155 struct wa_seg **seg; /* transfer segments */
156 u8 segs, segs_submitted, segs_done;
157 unsigned is_inbound:1;
158 unsigned is_dma:1;
159 size_t seg_size;
160 int result;
Thomas Pugliese21012422013-10-23 14:44:27 -0500161 /* Isoc frame that the current transfer buffer corresponds to. */
162 int dto_isoc_frame_index;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100163
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 /*
491 * For Alereon HWA devices, only include an isoc frame in a
492 * segment if it is physically contiguous with the previous
493 * 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)
498 && (index > isoc_frame_offset)
499 && ((iso_frame_desc[index - 1].offset +
500 iso_frame_desc[index - 1].length) !=
501 iso_frame_desc[index].offset))
502 break;
503
Thomas Pugliese21012422013-10-23 14:44:27 -0500504 /* this frame fits. count it. */
505 ++frame_count;
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500506 segment_size += iso_frame_desc[index].length;
Thomas Pugliese21012422013-10-23 14:44:27 -0500507
508 /* move to the next isoc frame. */
509 ++index;
510 }
511
512 *total_size = segment_size;
513 return frame_count;
514}
515
516/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100517 *
518 * @returns < 0 on error, transfer segment request size if ok
519 */
520static ssize_t __wa_xfer_setup_sizes(struct wa_xfer *xfer,
521 enum wa_xfer_type *pxfer_type)
522{
523 ssize_t result;
524 struct device *dev = &xfer->wa->usb_iface->dev;
525 size_t maxpktsize;
526 struct urb *urb = xfer->urb;
527 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
528
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100529 switch (rpipe->descr.bmAttribute & 0x3) {
530 case USB_ENDPOINT_XFER_CONTROL:
531 *pxfer_type = WA_XFER_TYPE_CTL;
532 result = sizeof(struct wa_xfer_ctl);
533 break;
534 case USB_ENDPOINT_XFER_INT:
535 case USB_ENDPOINT_XFER_BULK:
536 *pxfer_type = WA_XFER_TYPE_BI;
537 result = sizeof(struct wa_xfer_bi);
538 break;
539 case USB_ENDPOINT_XFER_ISOC:
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500540 if (usb_pipeout(urb->pipe)) {
541 *pxfer_type = WA_XFER_TYPE_ISO;
542 result = sizeof(struct wa_xfer_hwaiso);
543 } else {
544 dev_err(dev, "FIXME: ISOC IN not implemented\n");
545 result = -ENOSYS;
546 goto error;
547 }
548 break;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100549 default:
550 /* never happens */
551 BUG();
552 result = -EINVAL; /* shut gcc up */
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500553 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100554 xfer->is_inbound = urb->pipe & USB_DIR_IN ? 1 : 0;
555 xfer->is_dma = urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? 1 : 0;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500556
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100557 maxpktsize = le16_to_cpu(rpipe->descr.wMaxPacketSize);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500558 if ((rpipe->descr.bmAttribute & 0x3) == USB_ENDPOINT_XFER_ISOC) {
Thomas Pugliese21012422013-10-23 14:44:27 -0500559 int index = 0;
560
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500561 xfer->seg_size = maxpktsize;
Thomas Pugliese21012422013-10-23 14:44:27 -0500562 xfer->segs = 0;
563 /*
564 * loop over urb->number_of_packets to determine how many
565 * xfer segments will be needed to send the isoc frames.
566 */
567 while (index < urb->number_of_packets) {
568 int seg_size; /* don't care. */
569 index += __wa_seg_calculate_isoc_frame_count(xfer,
570 index, &seg_size);
571 ++xfer->segs;
572 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500573 } else {
574 xfer->seg_size = le16_to_cpu(rpipe->descr.wBlocks)
575 * 1 << (xfer->wa->wa_descr->bRPipeBlockSize - 1);
576 /* Compute the segment size and make sure it is a multiple of
577 * the maxpktsize (WUSB1.0[8.3.3.1])...not really too much of
578 * a check (FIXME) */
579 if (xfer->seg_size < maxpktsize) {
580 dev_err(dev,
581 "HW BUG? seg_size %zu smaller than maxpktsize %zu\n",
582 xfer->seg_size, maxpktsize);
583 result = -EINVAL;
584 goto error;
585 }
586 xfer->seg_size = (xfer->seg_size / maxpktsize) * maxpktsize;
587 xfer->segs = DIV_ROUND_UP(urb->transfer_buffer_length,
588 xfer->seg_size);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500589 if (xfer->segs == 0 && *pxfer_type == WA_XFER_TYPE_CTL)
590 xfer->segs = 1;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100591 }
Thomas Pugliese21012422013-10-23 14:44:27 -0500592
Thomas Pugliesef74b75e2013-10-23 14:44:29 -0500593 if (xfer->segs > WA_SEGS_MAX) {
Thomas Pugliese21012422013-10-23 14:44:27 -0500594 dev_err(dev, "BUG? oops, number of segments %zu bigger than %d\n",
595 (urb->transfer_buffer_length/xfer->seg_size),
596 WA_SEGS_MAX);
597 result = -EINVAL;
598 goto error;
599 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100600error:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100601 return result;
602}
603
Thomas Pugliese21012422013-10-23 14:44:27 -0500604static void __wa_setup_isoc_packet_descr(
605 struct wa_xfer_packet_info_hwaiso *packet_desc,
606 struct wa_xfer *xfer,
607 struct wa_seg *seg) {
608 struct usb_iso_packet_descriptor *iso_frame_desc =
609 xfer->urb->iso_frame_desc;
610 int frame_index;
611
612 /* populate isoc packet descriptor. */
613 packet_desc->bPacketType = WA_XFER_ISO_PACKET_INFO;
614 packet_desc->wLength = cpu_to_le16(sizeof(*packet_desc) +
615 (sizeof(packet_desc->PacketLength[0]) *
616 seg->isoc_frame_count));
617 for (frame_index = 0; frame_index < seg->isoc_frame_count;
618 ++frame_index) {
619 int offset_index = frame_index + seg->isoc_frame_offset;
620 packet_desc->PacketLength[frame_index] =
621 cpu_to_le16(iso_frame_desc[offset_index].length);
622 }
623}
624
625
David Vrabelbce83692008-12-22 18:22:50 +0000626/* Fill in the common request header and xfer-type specific data. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100627static void __wa_xfer_setup_hdr0(struct wa_xfer *xfer,
628 struct wa_xfer_hdr *xfer_hdr0,
629 enum wa_xfer_type xfer_type,
630 size_t xfer_hdr_size)
631{
632 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
Thomas Pugliese21012422013-10-23 14:44:27 -0500633 struct wa_seg *seg = xfer->seg[0];
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100634
Thomas Pugliese21012422013-10-23 14:44:27 -0500635 xfer_hdr0 = &seg->xfer_hdr;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100636 xfer_hdr0->bLength = xfer_hdr_size;
637 xfer_hdr0->bRequestType = xfer_type;
638 xfer_hdr0->wRPipe = rpipe->descr.wRPipeIndex;
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500639 xfer_hdr0->dwTransferID = wa_xfer_id_le32(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100640 xfer_hdr0->bTransferSegment = 0;
641 switch (xfer_type) {
642 case WA_XFER_TYPE_CTL: {
643 struct wa_xfer_ctl *xfer_ctl =
644 container_of(xfer_hdr0, struct wa_xfer_ctl, hdr);
645 xfer_ctl->bmAttribute = xfer->is_inbound ? 1 : 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100646 memcpy(&xfer_ctl->baSetupData, xfer->urb->setup_packet,
647 sizeof(xfer_ctl->baSetupData));
648 break;
649 }
650 case WA_XFER_TYPE_BI:
651 break;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500652 case WA_XFER_TYPE_ISO: {
653 struct wa_xfer_hwaiso *xfer_iso =
654 container_of(xfer_hdr0, struct wa_xfer_hwaiso, hdr);
655 struct wa_xfer_packet_info_hwaiso *packet_desc =
656 ((void *)xfer_iso) + xfer_hdr_size;
Thomas Pugliese21012422013-10-23 14:44:27 -0500657
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500658 /* populate the isoc section of the transfer request. */
Thomas Pugliese21012422013-10-23 14:44:27 -0500659 xfer_iso->dwNumOfPackets = cpu_to_le32(seg->isoc_frame_count);
660 /* populate isoc packet descriptor. */
661 __wa_setup_isoc_packet_descr(packet_desc, xfer, seg);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500662 break;
663 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100664 default:
665 BUG();
666 };
667}
668
669/*
670 * Callback for the OUT data phase of the segment request
671 *
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500672 * Check wa_seg_tr_cb(); most comments also apply here because this
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100673 * function does almost the same thing and they work closely
674 * together.
675 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300676 * If the seg request has failed but this DTO phase has succeeded,
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500677 * wa_seg_tr_cb() has already failed the segment and moved the
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100678 * status to WA_SEG_ERROR, so this will go through 'case 0' and
679 * effectively do nothing.
680 */
681static void wa_seg_dto_cb(struct urb *urb)
682{
683 struct wa_seg *seg = urb->context;
684 struct wa_xfer *xfer = seg->xfer;
685 struct wahc *wa;
686 struct device *dev;
687 struct wa_rpipe *rpipe;
688 unsigned long flags;
689 unsigned rpipe_ready = 0;
Thomas Pugliese21012422013-10-23 14:44:27 -0500690 int data_send_done = 1, release_dto = 0, holding_dto = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100691 u8 done = 0;
Thomas Pugliese21012422013-10-23 14:44:27 -0500692 int result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100693
Thomas Pugliesed5b5c9f2013-09-26 14:08:15 -0500694 /* free the sg if it was used. */
695 kfree(urb->sg);
696 urb->sg = NULL;
697
Thomas Pugliese21012422013-10-23 14:44:27 -0500698 spin_lock_irqsave(&xfer->lock, flags);
699 wa = xfer->wa;
700 dev = &wa->usb_iface->dev;
701 if (usb_pipeisoc(xfer->urb->pipe)) {
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500702 /* Alereon HWA sends all isoc frames in a single transfer. */
703 if (wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC)
704 xfer->dto_isoc_frame_index += seg->isoc_frame_count;
705 else
706 xfer->dto_isoc_frame_index += 1;
Thomas Pugliese21012422013-10-23 14:44:27 -0500707 if (xfer->dto_isoc_frame_index < seg->isoc_frame_count) {
708 data_send_done = 0;
709 holding_dto = 1; /* checked in error cases. */
710 /*
711 * if this is the last isoc frame of the segment, we
712 * can release DTO after sending this frame.
713 */
714 if ((xfer->dto_isoc_frame_index + 1) >=
715 seg->isoc_frame_count)
716 release_dto = 1;
717 }
718 dev_dbg(dev, "xfer 0x%08X#%u: isoc frame = %d, holding_dto = %d, release_dto = %d.\n",
719 wa_xfer_id(xfer), seg->index,
720 xfer->dto_isoc_frame_index, holding_dto, release_dto);
721 }
722 spin_unlock_irqrestore(&xfer->lock, flags);
723
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100724 switch (urb->status) {
725 case 0:
726 spin_lock_irqsave(&xfer->lock, flags);
Thomas Pugliese21012422013-10-23 14:44:27 -0500727 seg->result += urb->actual_length;
728 if (data_send_done) {
729 dev_dbg(dev, "xfer 0x%08X#%u: data out done (%zu bytes)\n",
730 wa_xfer_id(xfer), seg->index, seg->result);
731 if (seg->status < WA_SEG_PENDING)
732 seg->status = WA_SEG_PENDING;
733 } else {
734 /* should only hit this for isoc xfers. */
735 /*
736 * Populate the dto URB with the next isoc frame buffer,
737 * send the URB and release DTO if we no longer need it.
738 */
739 __wa_populate_dto_urb_isoc(xfer, seg,
740 seg->isoc_frame_offset +
741 xfer->dto_isoc_frame_index);
742
743 /* resubmit the URB with the next isoc frame. */
744 result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
745 if (result < 0) {
746 dev_err(dev, "xfer 0x%08X#%u: DTO submit failed: %d\n",
747 wa_xfer_id(xfer), seg->index, result);
748 spin_unlock_irqrestore(&xfer->lock, flags);
749 goto error_dto_submit;
750 }
751 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100752 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Pugliese21012422013-10-23 14:44:27 -0500753 if (release_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 case -ECONNRESET: /* URB unlinked; no need to do anything */
759 case -ENOENT: /* as it was done by the who unlinked us */
Thomas Pugliese21012422013-10-23 14:44:27 -0500760 if (holding_dto) {
761 __wa_dto_put(wa);
762 wa_check_for_delayed_rpipes(wa);
763 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100764 break;
765 default: /* Other errors ... */
Thomas Pugliese21012422013-10-23 14:44:27 -0500766 dev_err(dev, "xfer 0x%08X#%u: data out error %d\n",
767 wa_xfer_id(xfer), seg->index, urb->status);
768 goto error_default;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100769 }
Thomas Pugliese21012422013-10-23 14:44:27 -0500770
771 return;
772
773error_dto_submit:
774error_default:
775 spin_lock_irqsave(&xfer->lock, flags);
776 rpipe = xfer->ep->hcpriv;
777 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
778 EDC_ERROR_TIMEFRAME)){
779 dev_err(dev, "DTO: URB max acceptable errors exceeded, resetting device\n");
780 wa_reset_all(wa);
781 }
782 if (seg->status != WA_SEG_ERROR) {
783 seg->status = WA_SEG_ERROR;
784 seg->result = urb->status;
785 xfer->segs_done++;
786 __wa_xfer_abort(xfer);
787 rpipe_ready = rpipe_avail_inc(rpipe);
788 done = __wa_xfer_is_done(xfer);
789 }
790 spin_unlock_irqrestore(&xfer->lock, flags);
791 if (holding_dto) {
792 __wa_dto_put(wa);
793 wa_check_for_delayed_rpipes(wa);
794 }
795 if (done)
796 wa_xfer_completion(xfer);
797 if (rpipe_ready)
798 wa_xfer_delayed_run(rpipe);
799
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100800}
801
802/*
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500803 * Callback for the isoc packet descriptor phase of the segment request
804 *
805 * Check wa_seg_tr_cb(); most comments also apply here because this
806 * function does almost the same thing and they work closely
807 * together.
808 *
809 * If the seg request has failed but this phase has succeeded,
810 * wa_seg_tr_cb() has already failed the segment and moved the
811 * status to WA_SEG_ERROR, so this will go through 'case 0' and
812 * effectively do nothing.
813 */
814static void wa_seg_iso_pack_desc_cb(struct urb *urb)
815{
816 struct wa_seg *seg = urb->context;
817 struct wa_xfer *xfer = seg->xfer;
818 struct wahc *wa;
819 struct device *dev;
820 struct wa_rpipe *rpipe;
821 unsigned long flags;
822 unsigned rpipe_ready = 0;
823 u8 done = 0;
824
825 switch (urb->status) {
826 case 0:
827 spin_lock_irqsave(&xfer->lock, flags);
828 wa = xfer->wa;
829 dev = &wa->usb_iface->dev;
Thomas Pugliese21012422013-10-23 14:44:27 -0500830 dev_dbg(dev, "iso xfer %08X#%u: packet descriptor done\n",
831 wa_xfer_id(xfer), seg->index);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500832 if (xfer->is_inbound && seg->status < WA_SEG_PENDING)
833 seg->status = WA_SEG_PENDING;
834 spin_unlock_irqrestore(&xfer->lock, flags);
835 break;
836 case -ECONNRESET: /* URB unlinked; no need to do anything */
837 case -ENOENT: /* as it was done by the who unlinked us */
838 break;
839 default: /* Other errors ... */
840 spin_lock_irqsave(&xfer->lock, flags);
841 wa = xfer->wa;
842 dev = &wa->usb_iface->dev;
843 rpipe = xfer->ep->hcpriv;
Thomas Pugliese21012422013-10-23 14:44:27 -0500844 pr_err_ratelimited("iso xfer %08X#%u: packet descriptor error %d\n",
845 wa_xfer_id(xfer), seg->index, urb->status);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500846 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
847 EDC_ERROR_TIMEFRAME)){
848 dev_err(dev, "DTO: URB max acceptable errors exceeded, resetting device\n");
849 wa_reset_all(wa);
850 }
851 if (seg->status != WA_SEG_ERROR) {
852 usb_unlink_urb(seg->dto_urb);
853 seg->status = WA_SEG_ERROR;
854 seg->result = urb->status;
855 xfer->segs_done++;
856 __wa_xfer_abort(xfer);
857 rpipe_ready = rpipe_avail_inc(rpipe);
858 done = __wa_xfer_is_done(xfer);
859 }
860 spin_unlock_irqrestore(&xfer->lock, flags);
861 if (done)
862 wa_xfer_completion(xfer);
863 if (rpipe_ready)
864 wa_xfer_delayed_run(rpipe);
865 }
866}
867
868/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100869 * Callback for the segment request
870 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200871 * If successful transition state (unless already transitioned or
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100872 * outbound transfer); otherwise, take a note of the error, mark this
873 * segment done and try completion.
874 *
875 * Note we don't access until we are sure that the transfer hasn't
876 * been cancelled (ECONNRESET, ENOENT), which could mean that
877 * seg->xfer could be already gone.
878 *
879 * We have to check before setting the status to WA_SEG_PENDING
880 * because sometimes the xfer result callback arrives before this
881 * callback (geeeeeeze), so it might happen that we are already in
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500882 * another state. As well, we don't set it if the transfer is not inbound,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100883 * as in that case, wa_seg_dto_cb will do it when the OUT data phase
884 * finishes.
885 */
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500886static void wa_seg_tr_cb(struct urb *urb)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100887{
888 struct wa_seg *seg = urb->context;
889 struct wa_xfer *xfer = seg->xfer;
890 struct wahc *wa;
891 struct device *dev;
892 struct wa_rpipe *rpipe;
893 unsigned long flags;
894 unsigned rpipe_ready;
895 u8 done = 0;
896
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100897 switch (urb->status) {
898 case 0:
899 spin_lock_irqsave(&xfer->lock, flags);
900 wa = xfer->wa;
901 dev = &wa->usb_iface->dev;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500902 dev_dbg(dev, "xfer %p ID 0x%08X#%u: request done\n",
903 xfer, wa_xfer_id(xfer), seg->index);
904 if (xfer->is_inbound &&
905 seg->status < WA_SEG_PENDING &&
906 !(usb_pipeisoc(xfer->urb->pipe)))
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100907 seg->status = WA_SEG_PENDING;
908 spin_unlock_irqrestore(&xfer->lock, flags);
909 break;
910 case -ECONNRESET: /* URB unlinked; no need to do anything */
911 case -ENOENT: /* as it was done by the who unlinked us */
912 break;
913 default: /* Other errors ... */
914 spin_lock_irqsave(&xfer->lock, flags);
915 wa = xfer->wa;
916 dev = &wa->usb_iface->dev;
917 rpipe = xfer->ep->hcpriv;
918 if (printk_ratelimit())
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500919 dev_err(dev, "xfer %p ID 0x%08X#%u: request error %d\n",
920 xfer, wa_xfer_id(xfer), seg->index,
921 urb->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100922 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
923 EDC_ERROR_TIMEFRAME)){
924 dev_err(dev, "DTO: URB max acceptable errors "
925 "exceeded, resetting device\n");
926 wa_reset_all(wa);
927 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500928 usb_unlink_urb(seg->isoc_pack_desc_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100929 usb_unlink_urb(seg->dto_urb);
930 seg->status = WA_SEG_ERROR;
931 seg->result = urb->status;
932 xfer->segs_done++;
933 __wa_xfer_abort(xfer);
934 rpipe_ready = rpipe_avail_inc(rpipe);
935 done = __wa_xfer_is_done(xfer);
936 spin_unlock_irqrestore(&xfer->lock, flags);
937 if (done)
938 wa_xfer_completion(xfer);
939 if (rpipe_ready)
940 wa_xfer_delayed_run(rpipe);
941 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100942}
943
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500944/*
945 * Allocate an SG list to store bytes_to_transfer bytes and copy the
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500946 * subset of the in_sg that matches the buffer subset
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500947 * we are about to transfer.
948 */
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500949static struct scatterlist *wa_xfer_create_subset_sg(struct scatterlist *in_sg,
950 const unsigned int bytes_transferred,
951 const unsigned int bytes_to_transfer, unsigned int *out_num_sgs)
952{
953 struct scatterlist *out_sg;
954 unsigned int bytes_processed = 0, offset_into_current_page_data = 0,
955 nents;
956 struct scatterlist *current_xfer_sg = in_sg;
957 struct scatterlist *current_seg_sg, *last_seg_sg;
958
959 /* skip previously transferred pages. */
960 while ((current_xfer_sg) &&
961 (bytes_processed < bytes_transferred)) {
962 bytes_processed += current_xfer_sg->length;
963
964 /* advance the sg if current segment starts on or past the
965 next page. */
966 if (bytes_processed <= bytes_transferred)
967 current_xfer_sg = sg_next(current_xfer_sg);
968 }
969
970 /* the data for the current segment starts in current_xfer_sg.
971 calculate the offset. */
972 if (bytes_processed > bytes_transferred) {
973 offset_into_current_page_data = current_xfer_sg->length -
974 (bytes_processed - bytes_transferred);
975 }
976
977 /* calculate the number of pages needed by this segment. */
978 nents = DIV_ROUND_UP((bytes_to_transfer +
979 offset_into_current_page_data +
980 current_xfer_sg->offset),
981 PAGE_SIZE);
982
983 out_sg = kmalloc((sizeof(struct scatterlist) * nents), GFP_ATOMIC);
984 if (out_sg) {
985 sg_init_table(out_sg, nents);
986
987 /* copy the portion of the incoming SG that correlates to the
988 * data to be transferred by this segment to the segment SG. */
989 last_seg_sg = current_seg_sg = out_sg;
990 bytes_processed = 0;
991
992 /* reset nents and calculate the actual number of sg entries
993 needed. */
994 nents = 0;
995 while ((bytes_processed < bytes_to_transfer) &&
996 current_seg_sg && current_xfer_sg) {
997 unsigned int page_len = min((current_xfer_sg->length -
998 offset_into_current_page_data),
999 (bytes_to_transfer - bytes_processed));
1000
1001 sg_set_page(current_seg_sg, sg_page(current_xfer_sg),
1002 page_len,
1003 current_xfer_sg->offset +
1004 offset_into_current_page_data);
1005
1006 bytes_processed += page_len;
1007
1008 last_seg_sg = current_seg_sg;
1009 current_seg_sg = sg_next(current_seg_sg);
1010 current_xfer_sg = sg_next(current_xfer_sg);
1011
1012 /* only the first page may require additional offset. */
1013 offset_into_current_page_data = 0;
1014 nents++;
1015 }
1016
1017 /* update num_sgs and terminate the list since we may have
1018 * concatenated pages. */
1019 sg_mark_end(last_seg_sg);
1020 *out_num_sgs = nents;
1021 }
1022
1023 return out_sg;
1024}
1025
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001026/*
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001027 * Populate DMA buffer info for the isoc dto urb.
1028 */
Thomas Pugliese21012422013-10-23 14:44:27 -05001029static void __wa_populate_dto_urb_isoc(struct wa_xfer *xfer,
1030 struct wa_seg *seg, int curr_iso_frame)
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001031{
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001032 seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1033 seg->dto_urb->sg = NULL;
1034 seg->dto_urb->num_sgs = 0;
Thomas Pugliesef07ddb92013-10-23 14:44:28 -05001035 /* dto urb buffer address pulled from iso_frame_desc. */
1036 seg->dto_urb->transfer_dma = xfer->urb->transfer_dma +
1037 xfer->urb->iso_frame_desc[curr_iso_frame].offset;
1038 /* The Alereon HWA sends a single URB with all isoc segs. */
1039 if (xfer->wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC)
1040 seg->dto_urb->transfer_buffer_length = seg->isoc_size;
1041 else
1042 seg->dto_urb->transfer_buffer_length =
1043 xfer->urb->iso_frame_desc[curr_iso_frame].length;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001044}
1045
1046/*
Thomas Puglieseffd6d172013-09-26 14:08:14 -05001047 * Populate buffer ptr and size, DMA buffer or SG list for the dto urb.
1048 */
1049static int __wa_populate_dto_urb(struct wa_xfer *xfer,
1050 struct wa_seg *seg, size_t buf_itr_offset, size_t buf_itr_size)
1051{
1052 int result = 0;
1053
1054 if (xfer->is_dma) {
1055 seg->dto_urb->transfer_dma =
1056 xfer->urb->transfer_dma + buf_itr_offset;
1057 seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1058 seg->dto_urb->sg = NULL;
1059 seg->dto_urb->num_sgs = 0;
1060 } else {
1061 /* do buffer or SG processing. */
1062 seg->dto_urb->transfer_flags &=
1063 ~URB_NO_TRANSFER_DMA_MAP;
1064 /* this should always be 0 before a resubmit. */
1065 seg->dto_urb->num_mapped_sgs = 0;
1066
1067 if (xfer->urb->transfer_buffer) {
1068 seg->dto_urb->transfer_buffer =
1069 xfer->urb->transfer_buffer +
1070 buf_itr_offset;
1071 seg->dto_urb->sg = NULL;
1072 seg->dto_urb->num_sgs = 0;
1073 } else {
1074 seg->dto_urb->transfer_buffer = NULL;
1075
1076 /*
1077 * allocate an SG list to store seg_size bytes
1078 * and copy the subset of the xfer->urb->sg that
1079 * matches the buffer subset we are about to
1080 * read.
1081 */
1082 seg->dto_urb->sg = wa_xfer_create_subset_sg(
1083 xfer->urb->sg,
1084 buf_itr_offset, buf_itr_size,
1085 &(seg->dto_urb->num_sgs));
1086 if (!(seg->dto_urb->sg))
1087 result = -ENOMEM;
1088 }
1089 }
1090 seg->dto_urb->transfer_buffer_length = buf_itr_size;
1091
1092 return result;
1093}
1094
1095/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001096 * Allocate the segs array and initialize each of them
1097 *
1098 * The segments are freed by wa_xfer_destroy() when the xfer use count
1099 * drops to zero; however, because each segment is given the same life
1100 * cycle as the USB URB it contains, it is actually freed by
1101 * usb_put_urb() on the contained USB URB (twisted, eh?).
1102 */
1103static int __wa_xfer_setup_segs(struct wa_xfer *xfer, size_t xfer_hdr_size)
1104{
Thomas Pugliese21012422013-10-23 14:44:27 -05001105 int result, cnt, iso_frame_offset;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001106 size_t alloc_size = sizeof(*xfer->seg[0])
1107 - sizeof(xfer->seg[0]->xfer_hdr) + xfer_hdr_size;
1108 struct usb_device *usb_dev = xfer->wa->usb_dev;
1109 const struct usb_endpoint_descriptor *dto_epd = xfer->wa->dto_epd;
1110 struct wa_seg *seg;
Thomas Pugliese21012422013-10-23 14:44:27 -05001111 size_t buf_itr, buf_size, buf_itr_size;
1112 int xfer_isoc_frame_offset = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001113
1114 result = -ENOMEM;
David Vrabel92c4d9b2008-10-15 14:50:10 +01001115 xfer->seg = kcalloc(xfer->segs, sizeof(xfer->seg[0]), GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001116 if (xfer->seg == NULL)
1117 goto error_segs_kzalloc;
1118 buf_itr = 0;
1119 buf_size = xfer->urb->transfer_buffer_length;
Thomas Pugliese21012422013-10-23 14:44:27 -05001120 iso_frame_offset = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001121 for (cnt = 0; cnt < xfer->segs; cnt++) {
Thomas Pugliese21012422013-10-23 14:44:27 -05001122 size_t iso_pkt_descr_size = 0;
1123 int seg_isoc_frame_count = 0, seg_isoc_size = 0;
1124
1125 if (usb_pipeisoc(xfer->urb->pipe)) {
1126 seg_isoc_frame_count =
1127 __wa_seg_calculate_isoc_frame_count(xfer,
1128 xfer_isoc_frame_offset, &seg_isoc_size);
1129
1130 iso_pkt_descr_size =
1131 sizeof(struct wa_xfer_packet_info_hwaiso) +
1132 (seg_isoc_frame_count * sizeof(__le16));
1133 }
1134 seg = xfer->seg[cnt] = kmalloc(alloc_size + iso_pkt_descr_size,
1135 GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001136 if (seg == NULL)
Thomas Pugliese66591015d2013-08-15 14:37:43 -05001137 goto error_seg_kmalloc;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001138 wa_seg_init(seg);
1139 seg->xfer = xfer;
1140 seg->index = cnt;
Thomas Pugliese21012422013-10-23 14:44:27 -05001141 seg->isoc_frame_count = seg_isoc_frame_count;
1142 seg->isoc_frame_offset = xfer_isoc_frame_offset;
1143 seg->isoc_size = seg_isoc_size;
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001144 usb_fill_bulk_urb(&seg->tr_urb, usb_dev,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001145 usb_sndbulkpipe(usb_dev,
1146 dto_epd->bEndpointAddress),
1147 &seg->xfer_hdr, xfer_hdr_size,
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001148 wa_seg_tr_cb, seg);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001149 buf_itr_size = min(buf_size, xfer->seg_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001150 if (xfer->is_inbound == 0 && buf_size > 0) {
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001151 /* outbound data. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001152 seg->dto_urb = usb_alloc_urb(0, GFP_ATOMIC);
1153 if (seg->dto_urb == NULL)
1154 goto error_dto_alloc;
1155 usb_fill_bulk_urb(
1156 seg->dto_urb, usb_dev,
1157 usb_sndbulkpipe(usb_dev,
1158 dto_epd->bEndpointAddress),
1159 NULL, 0, wa_seg_dto_cb, seg);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001160
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001161 if (usb_pipeisoc(xfer->urb->pipe)) {
1162 /* iso packet descriptor. */
1163 seg->isoc_pack_desc_urb =
1164 usb_alloc_urb(0, GFP_ATOMIC);
1165 if (seg->isoc_pack_desc_urb == NULL)
1166 goto error_iso_pack_desc_alloc;
1167 /*
1168 * The buffer for the isoc packet descriptor
1169 * after the transfer request header in the
1170 * segment object memory buffer.
1171 */
1172 usb_fill_bulk_urb(
1173 seg->isoc_pack_desc_urb, usb_dev,
1174 usb_sndbulkpipe(usb_dev,
1175 dto_epd->bEndpointAddress),
1176 (void *)(&seg->xfer_hdr) +
1177 xfer_hdr_size,
1178 iso_pkt_descr_size,
1179 wa_seg_iso_pack_desc_cb, seg);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001180
Thomas Pugliese21012422013-10-23 14:44:27 -05001181 /*
1182 * Fill in the xfer buffer information for the
1183 * first isoc frame. Subsequent frames in this
1184 * segment will be filled in and sent from the
1185 * DTO completion routine, if needed.
1186 */
1187 __wa_populate_dto_urb_isoc(xfer, seg,
1188 xfer_isoc_frame_offset);
1189 /* adjust starting frame offset for next seg. */
1190 xfer_isoc_frame_offset += seg_isoc_frame_count;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001191 } else {
1192 /* fill in the xfer buffer information. */
1193 result = __wa_populate_dto_urb(xfer, seg,
1194 buf_itr, buf_itr_size);
1195 if (result < 0)
1196 goto error_seg_outbound_populate;
1197
1198 buf_itr += buf_itr_size;
1199 buf_size -= buf_itr_size;
1200 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001201 }
1202 seg->status = WA_SEG_READY;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001203 }
1204 return 0;
1205
Thomas Puglieseffd6d172013-09-26 14:08:14 -05001206 /*
1207 * Free the memory for the current segment which failed to init.
1208 * Use the fact that cnt is left at were it failed. The remaining
1209 * segments will be cleaned up by wa_xfer_destroy.
1210 */
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001211error_iso_pack_desc_alloc:
Thomas Puglieseffd6d172013-09-26 14:08:14 -05001212error_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:
1215 kfree(xfer->seg[cnt]);
Thomas Puglieseffd6d172013-09-26 14:08:14 -05001216 xfer->seg[cnt] = NULL;
Thomas Pugliese66591015d2013-08-15 14:37:43 -05001217error_seg_kmalloc:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001218error_segs_kzalloc:
1219 return result;
1220}
1221
1222/*
1223 * Allocates all the stuff needed to submit a transfer
1224 *
1225 * Breaks the whole data buffer in a list of segments, each one has a
1226 * structure allocated to it and linked in xfer->seg[index]
1227 *
1228 * FIXME: merge setup_segs() and the last part of this function, no
1229 * need to do two for loops when we could run everything in a
1230 * single one
1231 */
1232static int __wa_xfer_setup(struct wa_xfer *xfer, struct urb *urb)
1233{
1234 int result;
1235 struct device *dev = &xfer->wa->usb_iface->dev;
1236 enum wa_xfer_type xfer_type = 0; /* shut up GCC */
1237 size_t xfer_hdr_size, cnt, transfer_size;
1238 struct wa_xfer_hdr *xfer_hdr0, *xfer_hdr;
1239
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001240 result = __wa_xfer_setup_sizes(xfer, &xfer_type);
1241 if (result < 0)
1242 goto error_setup_sizes;
1243 xfer_hdr_size = result;
1244 result = __wa_xfer_setup_segs(xfer, xfer_hdr_size);
1245 if (result < 0) {
1246 dev_err(dev, "xfer %p: Failed to allocate %d segments: %d\n",
1247 xfer, xfer->segs, result);
1248 goto error_setup_segs;
1249 }
1250 /* Fill the first header */
1251 xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
1252 wa_xfer_id_init(xfer);
1253 __wa_xfer_setup_hdr0(xfer, xfer_hdr0, xfer_type, xfer_hdr_size);
1254
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001255 /* Fill remaining headers */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001256 xfer_hdr = xfer_hdr0;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001257 if (xfer_type == WA_XFER_TYPE_ISO) {
1258 xfer_hdr0->dwTransferLength =
Thomas Pugliese21012422013-10-23 14:44:27 -05001259 cpu_to_le32(xfer->seg[0]->isoc_size);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001260 for (cnt = 1; cnt < xfer->segs; cnt++) {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001261 struct wa_xfer_packet_info_hwaiso *packet_desc;
Thomas Pugliese21012422013-10-23 14:44:27 -05001262 struct wa_seg *seg = xfer->seg[cnt];
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001263
Thomas Pugliese21012422013-10-23 14:44:27 -05001264 xfer_hdr = &seg->xfer_hdr;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001265 packet_desc = ((void *)xfer_hdr) + xfer_hdr_size;
1266 /*
Thomas Pugliese21012422013-10-23 14:44:27 -05001267 * Copy values from the 0th header. Segment specific
1268 * values are set below.
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001269 */
Thomas Pugliese21012422013-10-23 14:44:27 -05001270 memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001271 xfer_hdr->bTransferSegment = cnt;
1272 xfer_hdr->dwTransferLength =
Thomas Pugliese21012422013-10-23 14:44:27 -05001273 cpu_to_le32(seg->isoc_size);
1274 __wa_setup_isoc_packet_descr(packet_desc, xfer, seg);
1275 seg->status = WA_SEG_READY;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001276 }
1277 } else {
1278 transfer_size = urb->transfer_buffer_length;
1279 xfer_hdr0->dwTransferLength = transfer_size > xfer->seg_size ?
1280 cpu_to_le32(xfer->seg_size) :
1281 cpu_to_le32(transfer_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001282 transfer_size -= xfer->seg_size;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001283 for (cnt = 1; cnt < xfer->segs; cnt++) {
1284 xfer_hdr = &xfer->seg[cnt]->xfer_hdr;
1285 memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
1286 xfer_hdr->bTransferSegment = cnt;
1287 xfer_hdr->dwTransferLength =
1288 transfer_size > xfer->seg_size ?
1289 cpu_to_le32(xfer->seg_size)
1290 : cpu_to_le32(transfer_size);
1291 xfer->seg[cnt]->status = WA_SEG_READY;
1292 transfer_size -= xfer->seg_size;
1293 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001294 }
1295 xfer_hdr->bTransferSegment |= 0x80; /* this is the last segment */
1296 result = 0;
1297error_setup_segs:
1298error_setup_sizes:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001299 return result;
1300}
1301
1302/*
1303 *
1304 *
1305 * rpipe->seg_lock is held!
1306 */
1307static int __wa_seg_submit(struct wa_rpipe *rpipe, struct wa_xfer *xfer,
Thomas Pugliese679ee472013-10-07 10:53:57 -05001308 struct wa_seg *seg, int *dto_done)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001309{
1310 int result;
Thomas Pugliese679ee472013-10-07 10:53:57 -05001311
1312 /* default to done unless we encounter a multi-frame isoc segment. */
1313 *dto_done = 1;
1314
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001315 /* submit the transfer request. */
1316 result = usb_submit_urb(&seg->tr_urb, GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001317 if (result < 0) {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001318 pr_err("%s: xfer %p#%u: REQ submit failed: %d\n",
1319 __func__, xfer, seg->index, result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001320 goto error_seg_submit;
1321 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001322 /* submit the isoc packet descriptor if present. */
1323 if (seg->isoc_pack_desc_urb) {
Thomas Pugliesef07ddb92013-10-23 14:44:28 -05001324 struct wahc *wa = xfer->wa;
1325
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001326 result = usb_submit_urb(seg->isoc_pack_desc_urb, GFP_ATOMIC);
1327 if (result < 0) {
1328 pr_err("%s: xfer %p#%u: ISO packet descriptor submit failed: %d\n",
1329 __func__, xfer, seg->index, result);
1330 goto error_iso_pack_desc_submit;
1331 }
Thomas Pugliese21012422013-10-23 14:44:27 -05001332 xfer->dto_isoc_frame_index = 0;
1333 /*
1334 * If this segment contains more than one isoc frame, hold
1335 * onto the dto resource until we send all frames.
Thomas Pugliesef07ddb92013-10-23 14:44:28 -05001336 * Only applies to non-Alereon devices.
Thomas Pugliese21012422013-10-23 14:44:27 -05001337 */
Thomas Pugliesef07ddb92013-10-23 14:44:28 -05001338 if (((wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC) == 0)
1339 && (seg->isoc_frame_count > 1))
Thomas Pugliese21012422013-10-23 14:44:27 -05001340 *dto_done = 0;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001341 }
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001342 /* submit the out data if this is an out request. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001343 if (seg->dto_urb) {
1344 result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
1345 if (result < 0) {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001346 pr_err("%s: xfer %p#%u: DTO submit failed: %d\n",
1347 __func__, xfer, seg->index, result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001348 goto error_dto_submit;
1349 }
1350 }
1351 seg->status = WA_SEG_SUBMITTED;
1352 rpipe_avail_dec(rpipe);
1353 return 0;
1354
1355error_dto_submit:
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001356 usb_unlink_urb(seg->isoc_pack_desc_urb);
1357error_iso_pack_desc_submit:
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001358 usb_unlink_urb(&seg->tr_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001359error_seg_submit:
1360 seg->status = WA_SEG_ERROR;
1361 seg->result = result;
Thomas Pugliese21012422013-10-23 14:44:27 -05001362 *dto_done = 1;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001363 return result;
1364}
1365
1366/*
Thomas Pugliese679ee472013-10-07 10:53:57 -05001367 * Execute more queued request segments until the maximum concurrent allowed.
1368 * Return true if the DTO resource was acquired and released.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001369 *
1370 * The ugly unlock/lock sequence on the error path is needed as the
1371 * xfer->lock normally nests the seg_lock and not viceversa.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001372 */
Thomas Pugliese679ee472013-10-07 10:53:57 -05001373static int __wa_xfer_delayed_run(struct wa_rpipe *rpipe, int *dto_waiting)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001374{
Thomas Pugliese679ee472013-10-07 10:53:57 -05001375 int result, dto_acquired = 0, dto_done = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001376 struct device *dev = &rpipe->wa->usb_iface->dev;
1377 struct wa_seg *seg;
1378 struct wa_xfer *xfer;
1379 unsigned long flags;
1380
Thomas Pugliese679ee472013-10-07 10:53:57 -05001381 *dto_waiting = 0;
1382
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001383 spin_lock_irqsave(&rpipe->seg_lock, flags);
1384 while (atomic_read(&rpipe->segs_available) > 0
Thomas Pugliese679ee472013-10-07 10:53:57 -05001385 && !list_empty(&rpipe->seg_list)
1386 && (dto_acquired = __wa_dto_try_get(rpipe->wa))) {
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001387 seg = list_first_entry(&(rpipe->seg_list), struct wa_seg,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001388 list_node);
1389 list_del(&seg->list_node);
1390 xfer = seg->xfer;
Thomas Pugliese679ee472013-10-07 10:53:57 -05001391 result = __wa_seg_submit(rpipe, xfer, seg, &dto_done);
1392 /* release the dto resource if this RPIPE is done with it. */
1393 if (dto_done)
1394 __wa_dto_put(rpipe->wa);
Thomas Puglieseb9c84be2013-09-27 15:33:36 -05001395 dev_dbg(dev, "xfer %p ID %08X#%u submitted from delayed [%d segments available] %d\n",
1396 xfer, wa_xfer_id(xfer), seg->index,
1397 atomic_read(&rpipe->segs_available), result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001398 if (unlikely(result < 0)) {
1399 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
1400 spin_lock_irqsave(&xfer->lock, flags);
1401 __wa_xfer_abort(xfer);
1402 xfer->segs_done++;
1403 spin_unlock_irqrestore(&xfer->lock, flags);
1404 spin_lock_irqsave(&rpipe->seg_lock, flags);
1405 }
1406 }
Thomas Pugliese679ee472013-10-07 10:53:57 -05001407 /*
1408 * Mark this RPIPE as waiting if dto was not acquired, there are
1409 * delayed segs and no active transfers to wake us up later.
1410 */
1411 if (!dto_acquired && !list_empty(&rpipe->seg_list)
1412 && (atomic_read(&rpipe->segs_available) ==
1413 le16_to_cpu(rpipe->descr.wRequests)))
1414 *dto_waiting = 1;
1415
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001416 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
Thomas Pugliese679ee472013-10-07 10:53:57 -05001417
1418 return dto_done;
1419}
1420
1421static void wa_xfer_delayed_run(struct wa_rpipe *rpipe)
1422{
1423 int dto_waiting;
1424 int dto_done = __wa_xfer_delayed_run(rpipe, &dto_waiting);
1425
1426 /*
1427 * If this RPIPE is waiting on the DTO resource, add it to the tail of
1428 * the waiting list.
1429 * Otherwise, if the WA DTO resource was acquired and released by
1430 * __wa_xfer_delayed_run, another RPIPE may have attempted to acquire
1431 * DTO and failed during that time. Check the delayed list and process
1432 * any waiters. Start searching from the next RPIPE index.
1433 */
1434 if (dto_waiting)
1435 wa_add_delayed_rpipe(rpipe->wa, rpipe);
1436 else if (dto_done)
1437 wa_check_for_delayed_rpipes(rpipe->wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001438}
1439
1440/*
1441 *
1442 * xfer->lock is taken
1443 *
1444 * On failure submitting we just stop submitting and return error;
1445 * wa_urb_enqueue_b() will execute the completion path
1446 */
1447static int __wa_xfer_submit(struct wa_xfer *xfer)
1448{
Thomas Pugliese679ee472013-10-07 10:53:57 -05001449 int result, dto_acquired = 0, dto_done = 0, dto_waiting = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001450 struct wahc *wa = xfer->wa;
1451 struct device *dev = &wa->usb_iface->dev;
1452 unsigned cnt;
1453 struct wa_seg *seg;
1454 unsigned long flags;
1455 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
1456 size_t maxrequests = le16_to_cpu(rpipe->descr.wRequests);
1457 u8 available;
1458 u8 empty;
1459
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001460 spin_lock_irqsave(&wa->xfer_list_lock, flags);
1461 list_add_tail(&xfer->list_node, &wa->xfer_list);
1462 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
1463
1464 BUG_ON(atomic_read(&rpipe->segs_available) > maxrequests);
1465 result = 0;
1466 spin_lock_irqsave(&rpipe->seg_lock, flags);
1467 for (cnt = 0; cnt < xfer->segs; cnt++) {
Thomas Pugliese679ee472013-10-07 10:53:57 -05001468 int delay_seg = 1;
1469
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001470 available = atomic_read(&rpipe->segs_available);
1471 empty = list_empty(&rpipe->seg_list);
1472 seg = xfer->seg[cnt];
Thomas Pugliese679ee472013-10-07 10:53:57 -05001473 if (available && empty) {
1474 /*
1475 * Only attempt to acquire DTO if we have a segment
1476 * to send.
1477 */
1478 dto_acquired = __wa_dto_try_get(rpipe->wa);
1479 if (dto_acquired) {
1480 delay_seg = 0;
1481 result = __wa_seg_submit(rpipe, xfer, seg,
1482 &dto_done);
Thomas Pugliese21012422013-10-23 14:44:27 -05001483 dev_dbg(dev, "xfer %p ID 0x%08X#%u: available %u empty %u submitted\n",
1484 xfer, wa_xfer_id(xfer), cnt, available,
1485 empty);
Thomas Pugliese679ee472013-10-07 10:53:57 -05001486 if (dto_done)
1487 __wa_dto_put(rpipe->wa);
1488
1489 if (result < 0) {
1490 __wa_xfer_abort(xfer);
1491 goto error_seg_submit;
1492 }
1493 }
1494 }
1495
1496 if (delay_seg) {
Thomas Pugliese21012422013-10-23 14:44:27 -05001497 dev_dbg(dev, "xfer %p ID 0x%08X#%u: available %u empty %u delayed\n",
1498 xfer, wa_xfer_id(xfer), cnt, available, empty);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001499 seg->status = WA_SEG_DELAYED;
1500 list_add_tail(&seg->list_node, &rpipe->seg_list);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001501 }
1502 xfer->segs_submitted++;
1503 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001504error_seg_submit:
Thomas Pugliese679ee472013-10-07 10:53:57 -05001505 /*
1506 * Mark this RPIPE as waiting if dto was not acquired, there are
1507 * delayed segs and no active transfers to wake us up later.
1508 */
1509 if (!dto_acquired && !list_empty(&rpipe->seg_list)
1510 && (atomic_read(&rpipe->segs_available) ==
1511 le16_to_cpu(rpipe->descr.wRequests)))
1512 dto_waiting = 1;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001513 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
Thomas Pugliese679ee472013-10-07 10:53:57 -05001514
1515 if (dto_waiting)
1516 wa_add_delayed_rpipe(rpipe->wa, rpipe);
1517 else if (dto_done)
1518 wa_check_for_delayed_rpipes(rpipe->wa);
1519
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001520 return result;
1521}
1522
1523/*
1524 * Second part of a URB/transfer enqueuement
1525 *
1526 * Assumes this comes from wa_urb_enqueue() [maybe through
1527 * wa_urb_enqueue_run()]. At this point:
1528 *
1529 * xfer->wa filled and refcounted
1530 * xfer->ep filled with rpipe refcounted if
1531 * delayed == 0
1532 * xfer->urb filled and refcounted (this is the case when called
1533 * from wa_urb_enqueue() as we come from usb_submit_urb()
1534 * and when called by wa_urb_enqueue_run(), as we took an
1535 * extra ref dropped by _run() after we return).
1536 * xfer->gfp filled
1537 *
1538 * If we fail at __wa_xfer_submit(), then we just check if we are done
1539 * and if so, we run the completion procedure. However, if we are not
1540 * yet done, we do nothing and wait for the completion handlers from
1541 * the submitted URBs or from the xfer-result path to kick in. If xfer
1542 * result never kicks in, the xfer will timeout from the USB code and
1543 * dequeue() will be called.
1544 */
Thomas Pugliese33186c42013-10-01 10:14:56 -05001545static int wa_urb_enqueue_b(struct wa_xfer *xfer)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001546{
1547 int result;
1548 unsigned long flags;
1549 struct urb *urb = xfer->urb;
1550 struct wahc *wa = xfer->wa;
1551 struct wusbhc *wusbhc = wa->wusb;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001552 struct wusb_dev *wusb_dev;
1553 unsigned done;
1554
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001555 result = rpipe_get_by_ep(wa, xfer->ep, urb, xfer->gfp);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001556 if (result < 0) {
1557 pr_err("%s: error_rpipe_get\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001558 goto error_rpipe_get;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001559 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001560 result = -ENODEV;
1561 /* FIXME: segmentation broken -- kills DWA */
1562 mutex_lock(&wusbhc->mutex); /* get a WUSB dev */
Jiri Slaby49fa0922009-03-11 21:47:40 +01001563 if (urb->dev == NULL) {
1564 mutex_unlock(&wusbhc->mutex);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001565 pr_err("%s: error usb dev gone\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001566 goto error_dev_gone;
Jiri Slaby49fa0922009-03-11 21:47:40 +01001567 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001568 wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev);
1569 if (wusb_dev == NULL) {
1570 mutex_unlock(&wusbhc->mutex);
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001571 dev_err(&(urb->dev->dev), "%s: error wusb dev gone\n",
1572 __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001573 goto error_dev_gone;
1574 }
1575 mutex_unlock(&wusbhc->mutex);
1576
1577 spin_lock_irqsave(&xfer->lock, flags);
1578 xfer->wusb_dev = wusb_dev;
1579 result = urb->status;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001580 if (urb->status != -EINPROGRESS) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001581 dev_err(&(urb->dev->dev), "%s: error_dequeued\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001582 goto error_dequeued;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001583 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001584
1585 result = __wa_xfer_setup(xfer, urb);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001586 if (result < 0) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001587 dev_err(&(urb->dev->dev), "%s: error_xfer_setup\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001588 goto error_xfer_setup;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001589 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001590 result = __wa_xfer_submit(xfer);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001591 if (result < 0) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001592 dev_err(&(urb->dev->dev), "%s: error_xfer_submit\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001593 goto error_xfer_submit;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001594 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001595 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001596 return 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001597
Thomas Pugliese33186c42013-10-01 10:14:56 -05001598 /*
1599 * this is basically wa_xfer_completion() broken up wa_xfer_giveback()
1600 * does a wa_xfer_put() that will call wa_xfer_destroy() and undo
1601 * setup().
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001602 */
1603error_xfer_setup:
1604error_dequeued:
1605 spin_unlock_irqrestore(&xfer->lock, flags);
1606 /* FIXME: segmentation broken, kills DWA */
1607 if (wusb_dev)
1608 wusb_dev_put(wusb_dev);
1609error_dev_gone:
1610 rpipe_put(xfer->ep->hcpriv);
1611error_rpipe_get:
1612 xfer->result = result;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001613 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001614
1615error_xfer_submit:
1616 done = __wa_xfer_is_done(xfer);
1617 xfer->result = result;
1618 spin_unlock_irqrestore(&xfer->lock, flags);
1619 if (done)
1620 wa_xfer_completion(xfer);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001621 /* return success since the completion routine will run. */
1622 return 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001623}
1624
1625/*
1626 * Execute the delayed transfers in the Wire Adapter @wa
1627 *
1628 * We need to be careful here, as dequeue() could be called in the
1629 * middle. That's why we do the whole thing under the
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001630 * wa->xfer_list_lock. If dequeue() jumps in, it first locks xfer->lock
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001631 * and then checks the list -- so as we would be acquiring in inverse
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001632 * order, we move the delayed list to a separate list while locked and then
1633 * submit them without the list lock held.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001634 */
1635void wa_urb_enqueue_run(struct work_struct *ws)
1636{
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001637 struct wahc *wa = container_of(ws, struct wahc, xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001638 struct wa_xfer *xfer, *next;
1639 struct urb *urb;
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001640 LIST_HEAD(tmp_list);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001641
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001642 /* Create a copy of the wa->xfer_delayed_list while holding the lock */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001643 spin_lock_irq(&wa->xfer_list_lock);
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001644 list_cut_position(&tmp_list, &wa->xfer_delayed_list,
1645 wa->xfer_delayed_list.prev);
1646 spin_unlock_irq(&wa->xfer_list_lock);
1647
1648 /*
1649 * enqueue from temp list without list lock held since wa_urb_enqueue_b
1650 * can take xfer->lock as well as lock mutexes.
1651 */
1652 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001653 list_del_init(&xfer->list_node);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001654
1655 urb = xfer->urb;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001656 if (wa_urb_enqueue_b(xfer) < 0)
1657 wa_xfer_giveback(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001658 usb_put_urb(urb); /* taken when queuing */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001659 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001660}
1661EXPORT_SYMBOL_GPL(wa_urb_enqueue_run);
1662
1663/*
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001664 * Process the errored transfers on the Wire Adapter outside of interrupt.
1665 */
1666void wa_process_errored_transfers_run(struct work_struct *ws)
1667{
1668 struct wahc *wa = container_of(ws, struct wahc, xfer_error_work);
1669 struct wa_xfer *xfer, *next;
1670 LIST_HEAD(tmp_list);
1671
1672 pr_info("%s: Run delayed STALL processing.\n", __func__);
1673
1674 /* Create a copy of the wa->xfer_errored_list while holding the lock */
1675 spin_lock_irq(&wa->xfer_list_lock);
1676 list_cut_position(&tmp_list, &wa->xfer_errored_list,
1677 wa->xfer_errored_list.prev);
1678 spin_unlock_irq(&wa->xfer_list_lock);
1679
1680 /*
1681 * run rpipe_clear_feature_stalled from temp list without list lock
1682 * held.
1683 */
1684 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
1685 struct usb_host_endpoint *ep;
1686 unsigned long flags;
1687 struct wa_rpipe *rpipe;
1688
1689 spin_lock_irqsave(&xfer->lock, flags);
1690 ep = xfer->ep;
1691 rpipe = ep->hcpriv;
1692 spin_unlock_irqrestore(&xfer->lock, flags);
1693
1694 /* clear RPIPE feature stalled without holding a lock. */
1695 rpipe_clear_feature_stalled(wa, ep);
1696
1697 /* complete the xfer. This removes it from the tmp list. */
1698 wa_xfer_completion(xfer);
1699
1700 /* check for work. */
1701 wa_xfer_delayed_run(rpipe);
1702 }
1703}
1704EXPORT_SYMBOL_GPL(wa_process_errored_transfers_run);
1705
1706/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001707 * Submit a transfer to the Wire Adapter in a delayed way
1708 *
1709 * The process of enqueuing involves possible sleeps() [see
1710 * enqueue_b(), for the rpipe_get() and the mutex_lock()]. If we are
1711 * in an atomic section, we defer the enqueue_b() call--else we call direct.
1712 *
1713 * @urb: We own a reference to it done by the HCI Linux USB stack that
1714 * will be given up by calling usb_hcd_giveback_urb() or by
1715 * returning error from this function -> ergo we don't have to
1716 * refcount it.
1717 */
1718int wa_urb_enqueue(struct wahc *wa, struct usb_host_endpoint *ep,
1719 struct urb *urb, gfp_t gfp)
1720{
1721 int result;
1722 struct device *dev = &wa->usb_iface->dev;
1723 struct wa_xfer *xfer;
1724 unsigned long my_flags;
1725 unsigned cant_sleep = irqs_disabled() | in_atomic();
1726
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001727 if ((urb->transfer_buffer == NULL)
1728 && (urb->sg == NULL)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001729 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1730 && urb->transfer_buffer_length != 0) {
1731 dev_err(dev, "BUG? urb %p: NULL xfer buffer & NODMA\n", urb);
1732 dump_stack();
1733 }
1734
Thomas Puglieseb3744872013-11-25 16:17:16 -06001735 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1736 result = usb_hcd_link_urb_to_ep(&(wa->wusb->usb_hcd), urb);
1737 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
1738 if (result < 0)
1739 goto error_link_urb;
1740
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001741 result = -ENOMEM;
1742 xfer = kzalloc(sizeof(*xfer), gfp);
1743 if (xfer == NULL)
1744 goto error_kmalloc;
1745
1746 result = -ENOENT;
1747 if (urb->status != -EINPROGRESS) /* cancelled */
1748 goto error_dequeued; /* before starting? */
1749 wa_xfer_init(xfer);
1750 xfer->wa = wa_get(wa);
1751 xfer->urb = urb;
1752 xfer->gfp = gfp;
1753 xfer->ep = ep;
1754 urb->hcpriv = xfer;
David Vrabelbce83692008-12-22 18:22:50 +00001755
1756 dev_dbg(dev, "xfer %p urb %p pipe 0x%02x [%d bytes] %s %s %s\n",
1757 xfer, urb, urb->pipe, urb->transfer_buffer_length,
1758 urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? "dma" : "nodma",
1759 urb->pipe & USB_DIR_IN ? "inbound" : "outbound",
1760 cant_sleep ? "deferred" : "inline");
1761
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001762 if (cant_sleep) {
1763 usb_get_urb(urb);
1764 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1765 list_add_tail(&xfer->list_node, &wa->xfer_delayed_list);
1766 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001767 queue_work(wusbd, &wa->xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001768 } else {
Thomas Pugliese33186c42013-10-01 10:14:56 -05001769 result = wa_urb_enqueue_b(xfer);
1770 if (result < 0) {
1771 /*
1772 * URB submit/enqueue failed. Clean up, return an
1773 * error and do not run the callback. This avoids
1774 * an infinite submit/complete loop.
1775 */
1776 dev_err(dev, "%s: URB enqueue failed: %d\n",
1777 __func__, result);
1778 wa_put(xfer->wa);
1779 wa_xfer_put(xfer);
Thomas Puglieseb3744872013-11-25 16:17:16 -06001780 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1781 usb_hcd_unlink_urb_from_ep(&(wa->wusb->usb_hcd), urb);
1782 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001783 return result;
1784 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001785 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001786 return 0;
1787
1788error_dequeued:
1789 kfree(xfer);
1790error_kmalloc:
Thomas Puglieseb3744872013-11-25 16:17:16 -06001791 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1792 usb_hcd_unlink_urb_from_ep(&(wa->wusb->usb_hcd), urb);
1793 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
1794error_link_urb:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001795 return result;
1796}
1797EXPORT_SYMBOL_GPL(wa_urb_enqueue);
1798
1799/*
1800 * Dequeue a URB and make sure uwb_hcd_giveback_urb() [completion
1801 * handler] is called.
1802 *
1803 * Until a transfer goes successfully through wa_urb_enqueue() it
1804 * needs to be dequeued with completion calling; when stuck in delayed
1805 * or before wa_xfer_setup() is called, we need to do completion.
1806 *
1807 * not setup If there is no hcpriv yet, that means that that enqueue
1808 * still had no time to set the xfer up. Because
1809 * urb->status should be other than -EINPROGRESS,
1810 * enqueue() will catch that and bail out.
1811 *
1812 * If the transfer has gone through setup, we just need to clean it
1813 * up. If it has gone through submit(), we have to abort it [with an
1814 * asynch request] and then make sure we cancel each segment.
1815 *
1816 */
Thomas Puglieseb3744872013-11-25 16:17:16 -06001817int wa_urb_dequeue(struct wahc *wa, struct urb *urb, int status)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001818{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001819 unsigned long flags, flags2;
1820 struct wa_xfer *xfer;
1821 struct wa_seg *seg;
1822 struct wa_rpipe *rpipe;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001823 unsigned cnt, done = 0, xfer_abort_pending;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001824 unsigned rpipe_ready = 0;
Thomas Puglieseb3744872013-11-25 16:17:16 -06001825 int result;
1826
1827 /* check if it is safe to unlink. */
1828 spin_lock_irqsave(&wa->xfer_list_lock, flags);
1829 result = usb_hcd_check_unlink_urb(&(wa->wusb->usb_hcd), urb, status);
1830 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
1831 if (result)
1832 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001833
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001834 xfer = urb->hcpriv;
1835 if (xfer == NULL) {
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001836 /*
1837 * Nothing setup yet enqueue will see urb->status !=
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001838 * -EINPROGRESS (by hcd layer) and bail out with
1839 * error, no need to do completion
1840 */
1841 BUG_ON(urb->status == -EINPROGRESS);
1842 goto out;
1843 }
1844 spin_lock_irqsave(&xfer->lock, flags);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001845 pr_debug("%s: DEQUEUE xfer id 0x%08X\n", __func__, wa_xfer_id(xfer));
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001846 rpipe = xfer->ep->hcpriv;
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001847 if (rpipe == NULL) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001848 pr_debug("%s: xfer %p id 0x%08X has no RPIPE. %s",
1849 __func__, xfer, wa_xfer_id(xfer),
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001850 "Probably already aborted.\n" );
1851 goto out_unlock;
1852 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001853 /* Check the delayed list -> if there, release and complete */
1854 spin_lock_irqsave(&wa->xfer_list_lock, flags2);
1855 if (!list_empty(&xfer->list_node) && xfer->seg == NULL)
1856 goto dequeue_delayed;
1857 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1858 if (xfer->seg == NULL) /* still hasn't reached */
1859 goto out_unlock; /* setup(), enqueue_b() completes */
1860 /* Ok, the xfer is in flight already, it's been setup and submitted.*/
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001861 xfer_abort_pending = __wa_xfer_abort(xfer) >= 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001862 for (cnt = 0; cnt < xfer->segs; cnt++) {
1863 seg = xfer->seg[cnt];
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001864 pr_debug("%s: xfer id 0x%08X#%d status = %d\n",
1865 __func__, wa_xfer_id(xfer), cnt, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001866 switch (seg->status) {
1867 case WA_SEG_NOTREADY:
1868 case WA_SEG_READY:
1869 printk(KERN_ERR "xfer %p#%u: dequeue bad state %u\n",
1870 xfer, cnt, seg->status);
1871 WARN_ON(1);
1872 break;
1873 case WA_SEG_DELAYED:
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001874 /*
1875 * delete from rpipe delayed list. If no segments on
1876 * this xfer have been submitted, __wa_xfer_is_done will
1877 * trigger a giveback below. Otherwise, the submitted
1878 * segments will be completed in the DTI interrupt.
1879 */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001880 seg->status = WA_SEG_ABORTED;
1881 spin_lock_irqsave(&rpipe->seg_lock, flags2);
1882 list_del(&seg->list_node);
1883 xfer->segs_done++;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001884 spin_unlock_irqrestore(&rpipe->seg_lock, flags2);
1885 break;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001886 case WA_SEG_DONE:
1887 case WA_SEG_ERROR:
1888 case WA_SEG_ABORTED:
1889 break;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001890 /*
1891 * In the states below, the HWA device already knows
1892 * about the transfer. If an abort request was sent,
1893 * allow the HWA to process it and wait for the
1894 * results. Otherwise, the DTI state and seg completed
1895 * counts can get out of sync.
1896 */
1897 case WA_SEG_SUBMITTED:
1898 case WA_SEG_PENDING:
1899 case WA_SEG_DTI_PENDING:
1900 /*
1901 * Check if the abort was successfully sent. This could
1902 * be false if the HWA has been removed but we haven't
1903 * gotten the disconnect notification yet.
1904 */
1905 if (!xfer_abort_pending) {
1906 seg->status = WA_SEG_ABORTED;
1907 rpipe_ready = rpipe_avail_inc(rpipe);
1908 xfer->segs_done++;
1909 }
1910 break;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001911 }
1912 }
1913 xfer->result = urb->status; /* -ENOENT or -ECONNRESET */
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001914 done = __wa_xfer_is_done(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001915 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001916 if (done)
1917 wa_xfer_completion(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001918 if (rpipe_ready)
1919 wa_xfer_delayed_run(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001920 return 0;
1921
1922out_unlock:
1923 spin_unlock_irqrestore(&xfer->lock, flags);
1924out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001925 return 0;
1926
1927dequeue_delayed:
1928 list_del_init(&xfer->list_node);
1929 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1930 xfer->result = urb->status;
1931 spin_unlock_irqrestore(&xfer->lock, flags);
1932 wa_xfer_giveback(xfer);
1933 usb_put_urb(urb); /* we got a ref in enqueue() */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001934 return 0;
1935}
1936EXPORT_SYMBOL_GPL(wa_urb_dequeue);
1937
1938/*
1939 * Translation from WA status codes (WUSB1.0 Table 8.15) to errno
1940 * codes
1941 *
1942 * Positive errno values are internal inconsistencies and should be
1943 * flagged louder. Negative are to be passed up to the user in the
1944 * normal way.
1945 *
1946 * @status: USB WA status code -- high two bits are stripped.
1947 */
1948static int wa_xfer_status_to_errno(u8 status)
1949{
1950 int errno;
1951 u8 real_status = status;
1952 static int xlat[] = {
1953 [WA_XFER_STATUS_SUCCESS] = 0,
1954 [WA_XFER_STATUS_HALTED] = -EPIPE,
1955 [WA_XFER_STATUS_DATA_BUFFER_ERROR] = -ENOBUFS,
1956 [WA_XFER_STATUS_BABBLE] = -EOVERFLOW,
1957 [WA_XFER_RESERVED] = EINVAL,
1958 [WA_XFER_STATUS_NOT_FOUND] = 0,
1959 [WA_XFER_STATUS_INSUFFICIENT_RESOURCE] = -ENOMEM,
1960 [WA_XFER_STATUS_TRANSACTION_ERROR] = -EILSEQ,
1961 [WA_XFER_STATUS_ABORTED] = -EINTR,
1962 [WA_XFER_STATUS_RPIPE_NOT_READY] = EINVAL,
1963 [WA_XFER_INVALID_FORMAT] = EINVAL,
1964 [WA_XFER_UNEXPECTED_SEGMENT_NUMBER] = EINVAL,
1965 [WA_XFER_STATUS_RPIPE_TYPE_MISMATCH] = EINVAL,
1966 };
1967 status &= 0x3f;
1968
1969 if (status == 0)
1970 return 0;
1971 if (status >= ARRAY_SIZE(xlat)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02001972 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001973 "Unknown WA transfer status 0x%02x\n",
1974 __func__, real_status);
1975 return -EINVAL;
1976 }
1977 errno = xlat[status];
1978 if (unlikely(errno > 0)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02001979 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001980 "Inconsistent WA status: 0x%02x\n",
1981 __func__, real_status);
1982 errno = -errno;
1983 }
1984 return errno;
1985}
1986
1987/*
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001988 * If a last segment flag and/or a transfer result error is encountered,
1989 * no other segment transfer results will be returned from the device.
1990 * Mark the remaining submitted or pending xfers as completed so that
1991 * the xfer will complete cleanly.
1992 */
1993static void wa_complete_remaining_xfer_segs(struct wa_xfer *xfer,
1994 struct wa_seg *incoming_seg)
1995{
1996 int index;
1997 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
1998
1999 for (index = incoming_seg->index + 1; index < xfer->segs_submitted;
2000 index++) {
2001 struct wa_seg *current_seg = xfer->seg[index];
2002
2003 BUG_ON(current_seg == NULL);
2004
2005 switch (current_seg->status) {
2006 case WA_SEG_SUBMITTED:
2007 case WA_SEG_PENDING:
2008 case WA_SEG_DTI_PENDING:
2009 rpipe_avail_inc(rpipe);
2010 /*
2011 * do not increment RPIPE avail for the WA_SEG_DELAYED case
2012 * since it has not been submitted to the RPIPE.
2013 */
2014 case WA_SEG_DELAYED:
2015 xfer->segs_done++;
2016 current_seg->status = incoming_seg->status;
2017 break;
2018 case WA_SEG_ABORTED:
2019 break;
2020 default:
2021 WARN(1, "%s: xfer 0x%08X#%d. bad seg status = %d\n",
2022 __func__, wa_xfer_id(xfer), index,
2023 current_seg->status);
2024 break;
2025 }
2026 }
2027}
2028
2029/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002030 * Process a xfer result completion message
2031 *
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002032 * inbound transfers: need to schedule a buf_in_urb read
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002033 *
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002034 * FIXME: this function needs to be broken up in parts
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002035 */
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002036static void wa_xfer_result_chew(struct wahc *wa, struct wa_xfer *xfer,
2037 struct wa_xfer_result *xfer_result)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002038{
2039 int result;
2040 struct device *dev = &wa->usb_iface->dev;
2041 unsigned long flags;
2042 u8 seg_idx;
2043 struct wa_seg *seg;
2044 struct wa_rpipe *rpipe;
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002045 unsigned done = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002046 u8 usb_status;
2047 unsigned rpipe_ready = 0;
2048
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002049 spin_lock_irqsave(&xfer->lock, flags);
2050 seg_idx = xfer_result->bTransferSegment & 0x7f;
2051 if (unlikely(seg_idx >= xfer->segs))
2052 goto error_bad_seg;
2053 seg = xfer->seg[seg_idx];
2054 rpipe = xfer->ep->hcpriv;
2055 usb_status = xfer_result->bTransferStatus;
Thomas Puglieseb9c84be2013-09-27 15:33:36 -05002056 dev_dbg(dev, "xfer %p ID 0x%08X#%u: bTransferStatus 0x%02x (seg status %u)\n",
2057 xfer, wa_xfer_id(xfer), seg_idx, usb_status, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002058 if (seg->status == WA_SEG_ABORTED
2059 || seg->status == WA_SEG_ERROR) /* already handled */
2060 goto segment_aborted;
2061 if (seg->status == WA_SEG_SUBMITTED) /* ops, got here */
2062 seg->status = WA_SEG_PENDING; /* before wa_seg{_dto}_cb() */
2063 if (seg->status != WA_SEG_PENDING) {
2064 if (printk_ratelimit())
2065 dev_err(dev, "xfer %p#%u: Bad segment state %u\n",
2066 xfer, seg_idx, seg->status);
2067 seg->status = WA_SEG_PENDING; /* workaround/"fix" it */
2068 }
2069 if (usb_status & 0x80) {
2070 seg->result = wa_xfer_status_to_errno(usb_status);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002071 dev_err(dev, "DTI: xfer %p#:%08X:%u failed (0x%02x)\n",
2072 xfer, xfer->id, seg->index, usb_status);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002073 seg->status = ((usb_status & 0x7F) == WA_XFER_STATUS_ABORTED) ?
2074 WA_SEG_ABORTED : WA_SEG_ERROR;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002075 goto error_complete;
2076 }
2077 /* FIXME: we ignore warnings, tally them for stats */
2078 if (usb_status & 0x40) /* Warning?... */
2079 usb_status = 0; /* ... pass */
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002080 if (usb_pipeisoc(xfer->urb->pipe)) {
2081 /* set up WA state to read the isoc packet status next. */
2082 wa->dti_isoc_xfer_in_progress = wa_xfer_id(xfer);
2083 wa->dti_isoc_xfer_seg = seg_idx;
2084 wa->dti_state = WA_DTI_ISOC_PACKET_STATUS_PENDING;
2085 } else if (xfer->is_inbound) { /* IN data phase: read to buffer */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002086 seg->status = WA_SEG_DTI_PENDING;
2087 BUG_ON(wa->buf_in_urb->status == -EINPROGRESS);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002088 /* this should always be 0 before a resubmit. */
2089 wa->buf_in_urb->num_mapped_sgs = 0;
2090
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002091 if (xfer->is_dma) {
2092 wa->buf_in_urb->transfer_dma =
2093 xfer->urb->transfer_dma
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002094 + (seg_idx * xfer->seg_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002095 wa->buf_in_urb->transfer_flags
2096 |= URB_NO_TRANSFER_DMA_MAP;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002097 wa->buf_in_urb->transfer_buffer = NULL;
2098 wa->buf_in_urb->sg = NULL;
2099 wa->buf_in_urb->num_sgs = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002100 } else {
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002101 /* do buffer or SG processing. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002102 wa->buf_in_urb->transfer_flags
2103 &= ~URB_NO_TRANSFER_DMA_MAP;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002104
2105 if (xfer->urb->transfer_buffer) {
2106 wa->buf_in_urb->transfer_buffer =
2107 xfer->urb->transfer_buffer
2108 + (seg_idx * xfer->seg_size);
2109 wa->buf_in_urb->sg = NULL;
2110 wa->buf_in_urb->num_sgs = 0;
2111 } else {
2112 /* allocate an SG list to store seg_size bytes
2113 and copy the subset of the xfer->urb->sg
2114 that matches the buffer subset we are
2115 about to read. */
2116 wa->buf_in_urb->sg = wa_xfer_create_subset_sg(
2117 xfer->urb->sg,
2118 seg_idx * xfer->seg_size,
2119 le32_to_cpu(
2120 xfer_result->dwTransferLength),
2121 &(wa->buf_in_urb->num_sgs));
2122
2123 if (!(wa->buf_in_urb->sg)) {
2124 wa->buf_in_urb->num_sgs = 0;
2125 goto error_sg_alloc;
2126 }
2127 wa->buf_in_urb->transfer_buffer = NULL;
2128 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002129 }
2130 wa->buf_in_urb->transfer_buffer_length =
2131 le32_to_cpu(xfer_result->dwTransferLength);
2132 wa->buf_in_urb->context = seg;
2133 result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
2134 if (result < 0)
2135 goto error_submit_buf_in;
2136 } else {
2137 /* OUT data phase, complete it -- */
2138 seg->status = WA_SEG_DONE;
2139 seg->result = le32_to_cpu(xfer_result->dwTransferLength);
2140 xfer->segs_done++;
2141 rpipe_ready = rpipe_avail_inc(rpipe);
2142 done = __wa_xfer_is_done(xfer);
2143 }
2144 spin_unlock_irqrestore(&xfer->lock, flags);
2145 if (done)
2146 wa_xfer_completion(xfer);
2147 if (rpipe_ready)
2148 wa_xfer_delayed_run(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002149 return;
2150
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002151error_submit_buf_in:
2152 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
2153 dev_err(dev, "DTI: URB max acceptable errors "
2154 "exceeded, resetting device\n");
2155 wa_reset_all(wa);
2156 }
2157 if (printk_ratelimit())
2158 dev_err(dev, "xfer %p#%u: can't submit DTI data phase: %d\n",
2159 xfer, seg_idx, result);
2160 seg->result = result;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002161 kfree(wa->buf_in_urb->sg);
Thomas Pugliese67414482013-09-26 14:08:16 -05002162 wa->buf_in_urb->sg = NULL;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002163error_sg_alloc:
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002164 __wa_xfer_abort(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002165 seg->status = WA_SEG_ERROR;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002166error_complete:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002167 xfer->segs_done++;
2168 rpipe_ready = rpipe_avail_inc(rpipe);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002169 wa_complete_remaining_xfer_segs(xfer, seg);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002170 done = __wa_xfer_is_done(xfer);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002171 /*
2172 * queue work item to clear STALL for control endpoints.
2173 * Otherwise, let endpoint_reset take care of it.
2174 */
2175 if (((usb_status & 0x3f) == WA_XFER_STATUS_HALTED) &&
2176 usb_endpoint_xfer_control(&xfer->ep->desc) &&
2177 done) {
2178
2179 dev_info(dev, "Control EP stall. Queue delayed work.\n");
2180 spin_lock_irq(&wa->xfer_list_lock);
Wei Yongjun8eb41292013-09-23 14:16:22 +08002181 /* move xfer from xfer_list to xfer_errored_list. */
2182 list_move_tail(&xfer->list_node, &wa->xfer_errored_list);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002183 spin_unlock_irq(&wa->xfer_list_lock);
2184 spin_unlock_irqrestore(&xfer->lock, flags);
2185 queue_work(wusbd, &wa->xfer_error_work);
2186 } else {
2187 spin_unlock_irqrestore(&xfer->lock, flags);
2188 if (done)
2189 wa_xfer_completion(xfer);
2190 if (rpipe_ready)
2191 wa_xfer_delayed_run(rpipe);
2192 }
2193
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002194 return;
2195
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002196error_bad_seg:
2197 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Puglieseb3744872013-11-25 16:17:16 -06002198 wa_urb_dequeue(wa, xfer->urb, -ENOENT);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002199 if (printk_ratelimit())
2200 dev_err(dev, "xfer %p#%u: bad segment\n", xfer, seg_idx);
2201 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
2202 dev_err(dev, "DTI: URB max acceptable errors "
2203 "exceeded, resetting device\n");
2204 wa_reset_all(wa);
2205 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002206 return;
2207
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002208segment_aborted:
2209 /* nothing to do, as the aborter did the completion */
2210 spin_unlock_irqrestore(&xfer->lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002211}
2212
2213/*
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002214 * Process a isochronous packet status message
2215 *
2216 * inbound transfers: need to schedule a buf_in_urb read
2217 */
2218static void wa_process_iso_packet_status(struct wahc *wa, struct urb *urb)
2219{
2220 struct device *dev = &wa->usb_iface->dev;
2221 struct wa_xfer_packet_status_hwaiso *packet_status;
Thomas Pugliese21012422013-10-23 14:44:27 -05002222 struct wa_xfer_packet_status_len_hwaiso *status_array;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002223 struct wa_xfer *xfer;
2224 unsigned long flags;
2225 struct wa_seg *seg;
2226 struct wa_rpipe *rpipe;
2227 unsigned done = 0;
Thomas Pugliese21012422013-10-23 14:44:27 -05002228 unsigned rpipe_ready = 0, seg_index;
2229 int expected_size;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002230
2231 /* We have a xfer result buffer; check it */
2232 dev_dbg(dev, "DTI: isoc packet status %d bytes at %p\n",
2233 urb->actual_length, urb->transfer_buffer);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002234 packet_status = (struct wa_xfer_packet_status_hwaiso *)(wa->dti_buf);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002235 if (packet_status->bPacketType != WA_XFER_ISO_PACKET_STATUS) {
2236 dev_err(dev, "DTI Error: isoc packet status--bad type 0x%02x\n",
2237 packet_status->bPacketType);
2238 goto error_parse_buffer;
2239 }
2240 xfer = wa_xfer_get_by_id(wa, wa->dti_isoc_xfer_in_progress);
2241 if (xfer == NULL) {
2242 dev_err(dev, "DTI Error: isoc packet status--unknown xfer 0x%08x\n",
2243 wa->dti_isoc_xfer_in_progress);
2244 goto error_parse_buffer;
2245 }
2246 spin_lock_irqsave(&xfer->lock, flags);
2247 if (unlikely(wa->dti_isoc_xfer_seg >= xfer->segs))
2248 goto error_bad_seg;
2249 seg = xfer->seg[wa->dti_isoc_xfer_seg];
2250 rpipe = xfer->ep->hcpriv;
Thomas Pugliese21012422013-10-23 14:44:27 -05002251 expected_size = sizeof(*packet_status) +
2252 (sizeof(packet_status->PacketStatus[0]) *
2253 seg->isoc_frame_count);
2254 if (urb->actual_length != expected_size) {
2255 dev_err(dev, "DTI Error: isoc packet status--bad urb length (%d bytes vs %d needed)\n",
2256 urb->actual_length, expected_size);
2257 goto error_bad_seg;
2258 }
2259 if (le16_to_cpu(packet_status->wLength) != expected_size) {
2260 dev_err(dev, "DTI Error: isoc packet status--bad length %u\n",
2261 le16_to_cpu(packet_status->wLength));
2262 goto error_bad_seg;
2263 }
2264 /* isoc packet status and lengths back xfer urb. */
2265 status_array = packet_status->PacketStatus;
2266 for (seg_index = 0; seg_index < seg->isoc_frame_count; ++seg_index) {
2267 xfer->urb->iso_frame_desc[seg->index].status =
2268 wa_xfer_status_to_errno(
2269 le16_to_cpu(status_array[seg_index].PacketStatus));
2270 xfer->urb->iso_frame_desc[seg->index].actual_length =
2271 le16_to_cpu(status_array[seg_index].PacketLength);
2272 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002273
2274 if (!xfer->is_inbound) {
2275 /* OUT transfer, complete it -- */
2276 seg->status = WA_SEG_DONE;
2277 xfer->segs_done++;
2278 rpipe_ready = rpipe_avail_inc(rpipe);
2279 done = __wa_xfer_is_done(xfer);
2280 }
2281 spin_unlock_irqrestore(&xfer->lock, flags);
2282 wa->dti_state = WA_DTI_TRANSFER_RESULT_PENDING;
2283 if (done)
2284 wa_xfer_completion(xfer);
2285 if (rpipe_ready)
2286 wa_xfer_delayed_run(rpipe);
2287 wa_xfer_put(xfer);
2288 return;
2289
2290error_bad_seg:
2291 spin_unlock_irqrestore(&xfer->lock, flags);
2292 wa_xfer_put(xfer);
2293error_parse_buffer:
2294 return;
2295}
2296
2297/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002298 * Callback for the IN data phase
2299 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002300 * If successful transition state; otherwise, take a note of the
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002301 * error, mark this segment done and try completion.
2302 *
2303 * Note we don't access until we are sure that the transfer hasn't
2304 * been cancelled (ECONNRESET, ENOENT), which could mean that
2305 * seg->xfer could be already gone.
2306 */
2307static void wa_buf_in_cb(struct urb *urb)
2308{
2309 struct wa_seg *seg = urb->context;
2310 struct wa_xfer *xfer = seg->xfer;
2311 struct wahc *wa;
2312 struct device *dev;
2313 struct wa_rpipe *rpipe;
2314 unsigned rpipe_ready;
2315 unsigned long flags;
2316 u8 done = 0;
2317
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002318 /* free the sg if it was used. */
2319 kfree(urb->sg);
2320 urb->sg = NULL;
2321
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002322 switch (urb->status) {
2323 case 0:
2324 spin_lock_irqsave(&xfer->lock, flags);
2325 wa = xfer->wa;
2326 dev = &wa->usb_iface->dev;
2327 rpipe = xfer->ep->hcpriv;
David Vrabelbce83692008-12-22 18:22:50 +00002328 dev_dbg(dev, "xfer %p#%u: data in done (%zu bytes)\n",
2329 xfer, seg->index, (size_t)urb->actual_length);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002330 seg->status = WA_SEG_DONE;
2331 seg->result = urb->actual_length;
2332 xfer->segs_done++;
2333 rpipe_ready = rpipe_avail_inc(rpipe);
2334 done = __wa_xfer_is_done(xfer);
2335 spin_unlock_irqrestore(&xfer->lock, flags);
2336 if (done)
2337 wa_xfer_completion(xfer);
2338 if (rpipe_ready)
2339 wa_xfer_delayed_run(rpipe);
2340 break;
2341 case -ECONNRESET: /* URB unlinked; no need to do anything */
2342 case -ENOENT: /* as it was done by the who unlinked us */
2343 break;
2344 default: /* Other errors ... */
2345 spin_lock_irqsave(&xfer->lock, flags);
2346 wa = xfer->wa;
2347 dev = &wa->usb_iface->dev;
2348 rpipe = xfer->ep->hcpriv;
2349 if (printk_ratelimit())
2350 dev_err(dev, "xfer %p#%u: data in error %d\n",
2351 xfer, seg->index, urb->status);
2352 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
2353 EDC_ERROR_TIMEFRAME)){
2354 dev_err(dev, "DTO: URB max acceptable errors "
2355 "exceeded, resetting device\n");
2356 wa_reset_all(wa);
2357 }
2358 seg->status = WA_SEG_ERROR;
2359 seg->result = urb->status;
2360 xfer->segs_done++;
2361 rpipe_ready = rpipe_avail_inc(rpipe);
2362 __wa_xfer_abort(xfer);
2363 done = __wa_xfer_is_done(xfer);
2364 spin_unlock_irqrestore(&xfer->lock, flags);
2365 if (done)
2366 wa_xfer_completion(xfer);
2367 if (rpipe_ready)
2368 wa_xfer_delayed_run(rpipe);
2369 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002370}
2371
2372/*
2373 * Handle an incoming transfer result buffer
2374 *
2375 * Given a transfer result buffer, it completes the transfer (possibly
2376 * scheduling and buffer in read) and then resubmits the DTI URB for a
2377 * new transfer result read.
2378 *
2379 *
2380 * The xfer_result DTI URB state machine
2381 *
2382 * States: OFF | RXR (Read-Xfer-Result) | RBI (Read-Buffer-In)
2383 *
2384 * We start in OFF mode, the first xfer_result notification [through
2385 * wa_handle_notif_xfer()] moves us to RXR by posting the DTI-URB to
2386 * read.
2387 *
2388 * We receive a buffer -- if it is not a xfer_result, we complain and
2389 * repost the DTI-URB. If it is a xfer_result then do the xfer seg
2390 * request accounting. If it is an IN segment, we move to RBI and post
2391 * a BUF-IN-URB to the right buffer. The BUF-IN-URB callback will
2392 * repost the DTI-URB and move to RXR state. if there was no IN
2393 * segment, it will repost the DTI-URB.
2394 *
2395 * We go back to OFF when we detect a ENOENT or ESHUTDOWN (or too many
2396 * errors) in the URBs.
2397 */
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002398static void wa_dti_cb(struct urb *urb)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002399{
2400 int result;
2401 struct wahc *wa = urb->context;
2402 struct device *dev = &wa->usb_iface->dev;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002403 u32 xfer_id;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002404 u8 usb_status;
2405
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002406 BUG_ON(wa->dti_urb != urb);
2407 switch (wa->dti_urb->status) {
2408 case 0:
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002409 if (wa->dti_state == WA_DTI_TRANSFER_RESULT_PENDING) {
2410 struct wa_xfer_result *xfer_result;
2411 struct wa_xfer *xfer;
2412
2413 /* We have a xfer result buffer; check it */
2414 dev_dbg(dev, "DTI: xfer result %d bytes at %p\n",
2415 urb->actual_length, urb->transfer_buffer);
2416 if (urb->actual_length != sizeof(*xfer_result)) {
2417 dev_err(dev, "DTI Error: xfer result--bad size xfer result (%d bytes vs %zu needed)\n",
2418 urb->actual_length,
2419 sizeof(*xfer_result));
2420 break;
2421 }
2422 xfer_result = (struct wa_xfer_result *)(wa->dti_buf);
2423 if (xfer_result->hdr.bLength != sizeof(*xfer_result)) {
2424 dev_err(dev, "DTI Error: xfer result--bad header length %u\n",
2425 xfer_result->hdr.bLength);
2426 break;
2427 }
2428 if (xfer_result->hdr.bNotifyType != WA_XFER_RESULT) {
2429 dev_err(dev, "DTI Error: xfer result--bad header type 0x%02x\n",
2430 xfer_result->hdr.bNotifyType);
2431 break;
2432 }
2433 usb_status = xfer_result->bTransferStatus & 0x3f;
2434 if (usb_status == WA_XFER_STATUS_NOT_FOUND)
2435 /* taken care of already */
2436 break;
2437 xfer_id = le32_to_cpu(xfer_result->dwTransferID);
2438 xfer = wa_xfer_get_by_id(wa, xfer_id);
2439 if (xfer == NULL) {
2440 /* FIXME: transaction not found. */
2441 dev_err(dev, "DTI Error: xfer result--unknown xfer 0x%08x (status 0x%02x)\n",
2442 xfer_id, usb_status);
2443 break;
2444 }
2445 wa_xfer_result_chew(wa, xfer, xfer_result);
2446 wa_xfer_put(xfer);
2447 } else if (wa->dti_state == WA_DTI_ISOC_PACKET_STATUS_PENDING) {
2448 wa_process_iso_packet_status(wa, urb);
2449 } else {
2450 dev_err(dev, "DTI Error: unexpected EP state = %d\n",
2451 wa->dti_state);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002452 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002453 break;
2454 case -ENOENT: /* (we killed the URB)...so, no broadcast */
2455 case -ESHUTDOWN: /* going away! */
2456 dev_dbg(dev, "DTI: going down! %d\n", urb->status);
2457 goto out;
2458 default:
2459 /* Unknown error */
2460 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS,
2461 EDC_ERROR_TIMEFRAME)) {
2462 dev_err(dev, "DTI: URB max acceptable errors "
2463 "exceeded, resetting device\n");
2464 wa_reset_all(wa);
2465 goto out;
2466 }
2467 if (printk_ratelimit())
2468 dev_err(dev, "DTI: URB error %d\n", urb->status);
2469 break;
2470 }
2471 /* Resubmit the DTI URB */
2472 result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
2473 if (result < 0) {
2474 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
2475 "resetting\n", result);
2476 wa_reset_all(wa);
2477 }
2478out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002479 return;
2480}
2481
2482/*
2483 * Transfer complete notification
2484 *
2485 * Called from the notif.c code. We get a notification on EP2 saying
2486 * that some endpoint has some transfer result data available. We are
2487 * about to read it.
2488 *
2489 * To speed up things, we always have a URB reading the DTI URB; we
2490 * don't really set it up and start it until the first xfer complete
2491 * notification arrives, which is what we do here.
2492 *
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002493 * Follow up in wa_dti_cb(), as that's where the whole state
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002494 * machine starts.
2495 *
2496 * So here we just initialize the DTI URB for reading transfer result
2497 * notifications and also the buffer-in URB, for reading buffers. Then
2498 * we just submit the DTI URB.
2499 *
2500 * @wa shall be referenced
2501 */
2502void wa_handle_notif_xfer(struct wahc *wa, struct wa_notif_hdr *notif_hdr)
2503{
2504 int result;
2505 struct device *dev = &wa->usb_iface->dev;
2506 struct wa_notif_xfer *notif_xfer;
2507 const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd;
2508
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002509 notif_xfer = container_of(notif_hdr, struct wa_notif_xfer, hdr);
2510 BUG_ON(notif_hdr->bNotifyType != WA_NOTIF_TRANSFER);
2511
2512 if ((0x80 | notif_xfer->bEndpoint) != dti_epd->bEndpointAddress) {
2513 /* FIXME: hardcoded limitation, adapt */
2514 dev_err(dev, "BUG: DTI ep is %u, not %u (hack me)\n",
2515 notif_xfer->bEndpoint, dti_epd->bEndpointAddress);
2516 goto error;
2517 }
2518 if (wa->dti_urb != NULL) /* DTI URB already started */
2519 goto out;
2520
2521 wa->dti_urb = usb_alloc_urb(0, GFP_KERNEL);
2522 if (wa->dti_urb == NULL) {
2523 dev_err(dev, "Can't allocate DTI URB\n");
2524 goto error_dti_urb_alloc;
2525 }
2526 usb_fill_bulk_urb(
2527 wa->dti_urb, wa->usb_dev,
2528 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002529 wa->dti_buf, wa->dti_buf_size,
2530 wa_dti_cb, wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002531
2532 wa->buf_in_urb = usb_alloc_urb(0, GFP_KERNEL);
2533 if (wa->buf_in_urb == NULL) {
2534 dev_err(dev, "Can't allocate BUF-IN URB\n");
2535 goto error_buf_in_urb_alloc;
2536 }
2537 usb_fill_bulk_urb(
2538 wa->buf_in_urb, wa->usb_dev,
2539 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
2540 NULL, 0, wa_buf_in_cb, wa);
2541 result = usb_submit_urb(wa->dti_urb, GFP_KERNEL);
2542 if (result < 0) {
2543 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
2544 "resetting\n", result);
2545 goto error_dti_urb_submit;
2546 }
2547out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002548 return;
2549
2550error_dti_urb_submit:
2551 usb_put_urb(wa->buf_in_urb);
Thomas Pugliese67414482013-09-26 14:08:16 -05002552 wa->buf_in_urb = NULL;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002553error_buf_in_urb_alloc:
2554 usb_put_urb(wa->dti_urb);
2555 wa->dti_urb = NULL;
2556error_dti_urb_alloc:
2557error:
2558 wa_reset_all(wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002559}