blob: 5957e486591a6c24ecd57fac402ee473ce8effdb [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:
376 dev_dbg(dev, "xfer %p ID %08X#%u ABORTED: result %d\n",
377 xfer, wa_xfer_id(xfer), seg->index,
378 urb->status);
379 xfer->result = urb->status;
380 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 Pugliese33186c42013-10-01 10:14:56 -05001571 pr_err("%s: error wusb dev gone\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001572 goto error_dev_gone;
1573 }
1574 mutex_unlock(&wusbhc->mutex);
1575
1576 spin_lock_irqsave(&xfer->lock, flags);
1577 xfer->wusb_dev = wusb_dev;
1578 result = urb->status;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001579 if (urb->status != -EINPROGRESS) {
1580 pr_err("%s: error_dequeued\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001581 goto error_dequeued;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001582 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001583
1584 result = __wa_xfer_setup(xfer, urb);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001585 if (result < 0) {
1586 pr_err("%s: error_xfer_setup\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001587 goto error_xfer_setup;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001588 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001589 result = __wa_xfer_submit(xfer);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001590 if (result < 0) {
1591 pr_err("%s: error_xfer_submit\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001592 goto error_xfer_submit;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001593 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001594 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001595 return 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001596
Thomas Pugliese33186c42013-10-01 10:14:56 -05001597 /*
1598 * this is basically wa_xfer_completion() broken up wa_xfer_giveback()
1599 * does a wa_xfer_put() that will call wa_xfer_destroy() and undo
1600 * setup().
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001601 */
1602error_xfer_setup:
1603error_dequeued:
1604 spin_unlock_irqrestore(&xfer->lock, flags);
1605 /* FIXME: segmentation broken, kills DWA */
1606 if (wusb_dev)
1607 wusb_dev_put(wusb_dev);
1608error_dev_gone:
1609 rpipe_put(xfer->ep->hcpriv);
1610error_rpipe_get:
1611 xfer->result = result;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001612 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001613
1614error_xfer_submit:
1615 done = __wa_xfer_is_done(xfer);
1616 xfer->result = result;
1617 spin_unlock_irqrestore(&xfer->lock, flags);
1618 if (done)
1619 wa_xfer_completion(xfer);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001620 /* return success since the completion routine will run. */
1621 return 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001622}
1623
1624/*
1625 * Execute the delayed transfers in the Wire Adapter @wa
1626 *
1627 * We need to be careful here, as dequeue() could be called in the
1628 * middle. That's why we do the whole thing under the
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001629 * wa->xfer_list_lock. If dequeue() jumps in, it first locks xfer->lock
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001630 * and then checks the list -- so as we would be acquiring in inverse
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001631 * order, we move the delayed list to a separate list while locked and then
1632 * submit them without the list lock held.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001633 */
1634void wa_urb_enqueue_run(struct work_struct *ws)
1635{
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001636 struct wahc *wa = container_of(ws, struct wahc, xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001637 struct wa_xfer *xfer, *next;
1638 struct urb *urb;
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001639 LIST_HEAD(tmp_list);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001640
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001641 /* Create a copy of the wa->xfer_delayed_list while holding the lock */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001642 spin_lock_irq(&wa->xfer_list_lock);
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001643 list_cut_position(&tmp_list, &wa->xfer_delayed_list,
1644 wa->xfer_delayed_list.prev);
1645 spin_unlock_irq(&wa->xfer_list_lock);
1646
1647 /*
1648 * enqueue from temp list without list lock held since wa_urb_enqueue_b
1649 * can take xfer->lock as well as lock mutexes.
1650 */
1651 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001652 list_del_init(&xfer->list_node);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001653
1654 urb = xfer->urb;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001655 if (wa_urb_enqueue_b(xfer) < 0)
1656 wa_xfer_giveback(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001657 usb_put_urb(urb); /* taken when queuing */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001658 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001659}
1660EXPORT_SYMBOL_GPL(wa_urb_enqueue_run);
1661
1662/*
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001663 * Process the errored transfers on the Wire Adapter outside of interrupt.
1664 */
1665void wa_process_errored_transfers_run(struct work_struct *ws)
1666{
1667 struct wahc *wa = container_of(ws, struct wahc, xfer_error_work);
1668 struct wa_xfer *xfer, *next;
1669 LIST_HEAD(tmp_list);
1670
1671 pr_info("%s: Run delayed STALL processing.\n", __func__);
1672
1673 /* Create a copy of the wa->xfer_errored_list while holding the lock */
1674 spin_lock_irq(&wa->xfer_list_lock);
1675 list_cut_position(&tmp_list, &wa->xfer_errored_list,
1676 wa->xfer_errored_list.prev);
1677 spin_unlock_irq(&wa->xfer_list_lock);
1678
1679 /*
1680 * run rpipe_clear_feature_stalled from temp list without list lock
1681 * held.
1682 */
1683 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
1684 struct usb_host_endpoint *ep;
1685 unsigned long flags;
1686 struct wa_rpipe *rpipe;
1687
1688 spin_lock_irqsave(&xfer->lock, flags);
1689 ep = xfer->ep;
1690 rpipe = ep->hcpriv;
1691 spin_unlock_irqrestore(&xfer->lock, flags);
1692
1693 /* clear RPIPE feature stalled without holding a lock. */
1694 rpipe_clear_feature_stalled(wa, ep);
1695
1696 /* complete the xfer. This removes it from the tmp list. */
1697 wa_xfer_completion(xfer);
1698
1699 /* check for work. */
1700 wa_xfer_delayed_run(rpipe);
1701 }
1702}
1703EXPORT_SYMBOL_GPL(wa_process_errored_transfers_run);
1704
1705/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001706 * Submit a transfer to the Wire Adapter in a delayed way
1707 *
1708 * The process of enqueuing involves possible sleeps() [see
1709 * enqueue_b(), for the rpipe_get() and the mutex_lock()]. If we are
1710 * in an atomic section, we defer the enqueue_b() call--else we call direct.
1711 *
1712 * @urb: We own a reference to it done by the HCI Linux USB stack that
1713 * will be given up by calling usb_hcd_giveback_urb() or by
1714 * returning error from this function -> ergo we don't have to
1715 * refcount it.
1716 */
1717int wa_urb_enqueue(struct wahc *wa, struct usb_host_endpoint *ep,
1718 struct urb *urb, gfp_t gfp)
1719{
1720 int result;
1721 struct device *dev = &wa->usb_iface->dev;
1722 struct wa_xfer *xfer;
1723 unsigned long my_flags;
1724 unsigned cant_sleep = irqs_disabled() | in_atomic();
1725
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001726 if ((urb->transfer_buffer == NULL)
1727 && (urb->sg == NULL)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001728 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1729 && urb->transfer_buffer_length != 0) {
1730 dev_err(dev, "BUG? urb %p: NULL xfer buffer & NODMA\n", urb);
1731 dump_stack();
1732 }
1733
Thomas Puglieseb3744872013-11-25 16:17:16 -06001734 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1735 result = usb_hcd_link_urb_to_ep(&(wa->wusb->usb_hcd), urb);
1736 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
1737 if (result < 0)
1738 goto error_link_urb;
1739
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001740 result = -ENOMEM;
1741 xfer = kzalloc(sizeof(*xfer), gfp);
1742 if (xfer == NULL)
1743 goto error_kmalloc;
1744
1745 result = -ENOENT;
1746 if (urb->status != -EINPROGRESS) /* cancelled */
1747 goto error_dequeued; /* before starting? */
1748 wa_xfer_init(xfer);
1749 xfer->wa = wa_get(wa);
1750 xfer->urb = urb;
1751 xfer->gfp = gfp;
1752 xfer->ep = ep;
1753 urb->hcpriv = xfer;
David Vrabelbce83692008-12-22 18:22:50 +00001754
1755 dev_dbg(dev, "xfer %p urb %p pipe 0x%02x [%d bytes] %s %s %s\n",
1756 xfer, urb, urb->pipe, urb->transfer_buffer_length,
1757 urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? "dma" : "nodma",
1758 urb->pipe & USB_DIR_IN ? "inbound" : "outbound",
1759 cant_sleep ? "deferred" : "inline");
1760
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001761 if (cant_sleep) {
1762 usb_get_urb(urb);
1763 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1764 list_add_tail(&xfer->list_node, &wa->xfer_delayed_list);
1765 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001766 queue_work(wusbd, &wa->xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001767 } else {
Thomas Pugliese33186c42013-10-01 10:14:56 -05001768 result = wa_urb_enqueue_b(xfer);
1769 if (result < 0) {
1770 /*
1771 * URB submit/enqueue failed. Clean up, return an
1772 * error and do not run the callback. This avoids
1773 * an infinite submit/complete loop.
1774 */
1775 dev_err(dev, "%s: URB enqueue failed: %d\n",
1776 __func__, result);
1777 wa_put(xfer->wa);
1778 wa_xfer_put(xfer);
Thomas Puglieseb3744872013-11-25 16:17:16 -06001779 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1780 usb_hcd_unlink_urb_from_ep(&(wa->wusb->usb_hcd), urb);
1781 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001782 return result;
1783 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001784 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001785 return 0;
1786
1787error_dequeued:
1788 kfree(xfer);
1789error_kmalloc:
Thomas Puglieseb3744872013-11-25 16:17:16 -06001790 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1791 usb_hcd_unlink_urb_from_ep(&(wa->wusb->usb_hcd), urb);
1792 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
1793error_link_urb:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001794 return result;
1795}
1796EXPORT_SYMBOL_GPL(wa_urb_enqueue);
1797
1798/*
1799 * Dequeue a URB and make sure uwb_hcd_giveback_urb() [completion
1800 * handler] is called.
1801 *
1802 * Until a transfer goes successfully through wa_urb_enqueue() it
1803 * needs to be dequeued with completion calling; when stuck in delayed
1804 * or before wa_xfer_setup() is called, we need to do completion.
1805 *
1806 * not setup If there is no hcpriv yet, that means that that enqueue
1807 * still had no time to set the xfer up. Because
1808 * urb->status should be other than -EINPROGRESS,
1809 * enqueue() will catch that and bail out.
1810 *
1811 * If the transfer has gone through setup, we just need to clean it
1812 * up. If it has gone through submit(), we have to abort it [with an
1813 * asynch request] and then make sure we cancel each segment.
1814 *
1815 */
Thomas Puglieseb3744872013-11-25 16:17:16 -06001816int wa_urb_dequeue(struct wahc *wa, struct urb *urb, int status)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001817{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001818 unsigned long flags, flags2;
1819 struct wa_xfer *xfer;
1820 struct wa_seg *seg;
1821 struct wa_rpipe *rpipe;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001822 unsigned cnt, done = 0, xfer_abort_pending;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001823 unsigned rpipe_ready = 0;
Thomas Puglieseb3744872013-11-25 16:17:16 -06001824 int result;
1825
1826 /* check if it is safe to unlink. */
1827 spin_lock_irqsave(&wa->xfer_list_lock, flags);
1828 result = usb_hcd_check_unlink_urb(&(wa->wusb->usb_hcd), urb, status);
1829 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
1830 if (result)
1831 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001832
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001833 xfer = urb->hcpriv;
1834 if (xfer == NULL) {
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001835 /*
1836 * Nothing setup yet enqueue will see urb->status !=
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001837 * -EINPROGRESS (by hcd layer) and bail out with
1838 * error, no need to do completion
1839 */
1840 BUG_ON(urb->status == -EINPROGRESS);
1841 goto out;
1842 }
1843 spin_lock_irqsave(&xfer->lock, flags);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001844 pr_debug("%s: DEQUEUE xfer id 0x%08X\n", __func__, wa_xfer_id(xfer));
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001845 rpipe = xfer->ep->hcpriv;
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001846 if (rpipe == NULL) {
1847 pr_debug("%s: xfer id 0x%08X has no RPIPE. %s",
1848 __func__, wa_xfer_id(xfer),
1849 "Probably already aborted.\n" );
1850 goto out_unlock;
1851 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001852 /* Check the delayed list -> if there, release and complete */
1853 spin_lock_irqsave(&wa->xfer_list_lock, flags2);
1854 if (!list_empty(&xfer->list_node) && xfer->seg == NULL)
1855 goto dequeue_delayed;
1856 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1857 if (xfer->seg == NULL) /* still hasn't reached */
1858 goto out_unlock; /* setup(), enqueue_b() completes */
1859 /* Ok, the xfer is in flight already, it's been setup and submitted.*/
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001860 xfer_abort_pending = __wa_xfer_abort(xfer) >= 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001861 for (cnt = 0; cnt < xfer->segs; cnt++) {
1862 seg = xfer->seg[cnt];
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001863 pr_debug("%s: xfer id 0x%08X#%d status = %d\n",
1864 __func__, wa_xfer_id(xfer), cnt, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001865 switch (seg->status) {
1866 case WA_SEG_NOTREADY:
1867 case WA_SEG_READY:
1868 printk(KERN_ERR "xfer %p#%u: dequeue bad state %u\n",
1869 xfer, cnt, seg->status);
1870 WARN_ON(1);
1871 break;
1872 case WA_SEG_DELAYED:
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001873 /*
1874 * delete from rpipe delayed list. If no segments on
1875 * this xfer have been submitted, __wa_xfer_is_done will
1876 * trigger a giveback below. Otherwise, the submitted
1877 * segments will be completed in the DTI interrupt.
1878 */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001879 seg->status = WA_SEG_ABORTED;
1880 spin_lock_irqsave(&rpipe->seg_lock, flags2);
1881 list_del(&seg->list_node);
1882 xfer->segs_done++;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001883 spin_unlock_irqrestore(&rpipe->seg_lock, flags2);
1884 break;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001885 case WA_SEG_DONE:
1886 case WA_SEG_ERROR:
1887 case WA_SEG_ABORTED:
1888 break;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001889 /*
1890 * In the states below, the HWA device already knows
1891 * about the transfer. If an abort request was sent,
1892 * allow the HWA to process it and wait for the
1893 * results. Otherwise, the DTI state and seg completed
1894 * counts can get out of sync.
1895 */
1896 case WA_SEG_SUBMITTED:
1897 case WA_SEG_PENDING:
1898 case WA_SEG_DTI_PENDING:
1899 /*
1900 * Check if the abort was successfully sent. This could
1901 * be false if the HWA has been removed but we haven't
1902 * gotten the disconnect notification yet.
1903 */
1904 if (!xfer_abort_pending) {
1905 seg->status = WA_SEG_ABORTED;
1906 rpipe_ready = rpipe_avail_inc(rpipe);
1907 xfer->segs_done++;
1908 }
1909 break;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001910 }
1911 }
1912 xfer->result = urb->status; /* -ENOENT or -ECONNRESET */
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001913 done = __wa_xfer_is_done(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001914 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001915 if (done)
1916 wa_xfer_completion(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001917 if (rpipe_ready)
1918 wa_xfer_delayed_run(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001919 return 0;
1920
1921out_unlock:
1922 spin_unlock_irqrestore(&xfer->lock, flags);
1923out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001924 return 0;
1925
1926dequeue_delayed:
1927 list_del_init(&xfer->list_node);
1928 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1929 xfer->result = urb->status;
1930 spin_unlock_irqrestore(&xfer->lock, flags);
1931 wa_xfer_giveback(xfer);
1932 usb_put_urb(urb); /* we got a ref in enqueue() */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001933 return 0;
1934}
1935EXPORT_SYMBOL_GPL(wa_urb_dequeue);
1936
1937/*
1938 * Translation from WA status codes (WUSB1.0 Table 8.15) to errno
1939 * codes
1940 *
1941 * Positive errno values are internal inconsistencies and should be
1942 * flagged louder. Negative are to be passed up to the user in the
1943 * normal way.
1944 *
1945 * @status: USB WA status code -- high two bits are stripped.
1946 */
1947static int wa_xfer_status_to_errno(u8 status)
1948{
1949 int errno;
1950 u8 real_status = status;
1951 static int xlat[] = {
1952 [WA_XFER_STATUS_SUCCESS] = 0,
1953 [WA_XFER_STATUS_HALTED] = -EPIPE,
1954 [WA_XFER_STATUS_DATA_BUFFER_ERROR] = -ENOBUFS,
1955 [WA_XFER_STATUS_BABBLE] = -EOVERFLOW,
1956 [WA_XFER_RESERVED] = EINVAL,
1957 [WA_XFER_STATUS_NOT_FOUND] = 0,
1958 [WA_XFER_STATUS_INSUFFICIENT_RESOURCE] = -ENOMEM,
1959 [WA_XFER_STATUS_TRANSACTION_ERROR] = -EILSEQ,
1960 [WA_XFER_STATUS_ABORTED] = -EINTR,
1961 [WA_XFER_STATUS_RPIPE_NOT_READY] = EINVAL,
1962 [WA_XFER_INVALID_FORMAT] = EINVAL,
1963 [WA_XFER_UNEXPECTED_SEGMENT_NUMBER] = EINVAL,
1964 [WA_XFER_STATUS_RPIPE_TYPE_MISMATCH] = EINVAL,
1965 };
1966 status &= 0x3f;
1967
1968 if (status == 0)
1969 return 0;
1970 if (status >= ARRAY_SIZE(xlat)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02001971 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001972 "Unknown WA transfer status 0x%02x\n",
1973 __func__, real_status);
1974 return -EINVAL;
1975 }
1976 errno = xlat[status];
1977 if (unlikely(errno > 0)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02001978 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001979 "Inconsistent WA status: 0x%02x\n",
1980 __func__, real_status);
1981 errno = -errno;
1982 }
1983 return errno;
1984}
1985
1986/*
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001987 * If a last segment flag and/or a transfer result error is encountered,
1988 * no other segment transfer results will be returned from the device.
1989 * Mark the remaining submitted or pending xfers as completed so that
1990 * the xfer will complete cleanly.
1991 */
1992static void wa_complete_remaining_xfer_segs(struct wa_xfer *xfer,
1993 struct wa_seg *incoming_seg)
1994{
1995 int index;
1996 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
1997
1998 for (index = incoming_seg->index + 1; index < xfer->segs_submitted;
1999 index++) {
2000 struct wa_seg *current_seg = xfer->seg[index];
2001
2002 BUG_ON(current_seg == NULL);
2003
2004 switch (current_seg->status) {
2005 case WA_SEG_SUBMITTED:
2006 case WA_SEG_PENDING:
2007 case WA_SEG_DTI_PENDING:
2008 rpipe_avail_inc(rpipe);
2009 /*
2010 * do not increment RPIPE avail for the WA_SEG_DELAYED case
2011 * since it has not been submitted to the RPIPE.
2012 */
2013 case WA_SEG_DELAYED:
2014 xfer->segs_done++;
2015 current_seg->status = incoming_seg->status;
2016 break;
2017 case WA_SEG_ABORTED:
2018 break;
2019 default:
2020 WARN(1, "%s: xfer 0x%08X#%d. bad seg status = %d\n",
2021 __func__, wa_xfer_id(xfer), index,
2022 current_seg->status);
2023 break;
2024 }
2025 }
2026}
2027
2028/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002029 * Process a xfer result completion message
2030 *
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002031 * inbound transfers: need to schedule a buf_in_urb read
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002032 *
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002033 * FIXME: this function needs to be broken up in parts
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002034 */
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002035static void wa_xfer_result_chew(struct wahc *wa, struct wa_xfer *xfer,
2036 struct wa_xfer_result *xfer_result)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002037{
2038 int result;
2039 struct device *dev = &wa->usb_iface->dev;
2040 unsigned long flags;
2041 u8 seg_idx;
2042 struct wa_seg *seg;
2043 struct wa_rpipe *rpipe;
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002044 unsigned done = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002045 u8 usb_status;
2046 unsigned rpipe_ready = 0;
2047
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002048 spin_lock_irqsave(&xfer->lock, flags);
2049 seg_idx = xfer_result->bTransferSegment & 0x7f;
2050 if (unlikely(seg_idx >= xfer->segs))
2051 goto error_bad_seg;
2052 seg = xfer->seg[seg_idx];
2053 rpipe = xfer->ep->hcpriv;
2054 usb_status = xfer_result->bTransferStatus;
Thomas Puglieseb9c84be2013-09-27 15:33:36 -05002055 dev_dbg(dev, "xfer %p ID 0x%08X#%u: bTransferStatus 0x%02x (seg status %u)\n",
2056 xfer, wa_xfer_id(xfer), seg_idx, usb_status, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002057 if (seg->status == WA_SEG_ABORTED
2058 || seg->status == WA_SEG_ERROR) /* already handled */
2059 goto segment_aborted;
2060 if (seg->status == WA_SEG_SUBMITTED) /* ops, got here */
2061 seg->status = WA_SEG_PENDING; /* before wa_seg{_dto}_cb() */
2062 if (seg->status != WA_SEG_PENDING) {
2063 if (printk_ratelimit())
2064 dev_err(dev, "xfer %p#%u: Bad segment state %u\n",
2065 xfer, seg_idx, seg->status);
2066 seg->status = WA_SEG_PENDING; /* workaround/"fix" it */
2067 }
2068 if (usb_status & 0x80) {
2069 seg->result = wa_xfer_status_to_errno(usb_status);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002070 dev_err(dev, "DTI: xfer %p#:%08X:%u failed (0x%02x)\n",
2071 xfer, xfer->id, seg->index, usb_status);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002072 seg->status = ((usb_status & 0x7F) == WA_XFER_STATUS_ABORTED) ?
2073 WA_SEG_ABORTED : WA_SEG_ERROR;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002074 goto error_complete;
2075 }
2076 /* FIXME: we ignore warnings, tally them for stats */
2077 if (usb_status & 0x40) /* Warning?... */
2078 usb_status = 0; /* ... pass */
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002079 if (usb_pipeisoc(xfer->urb->pipe)) {
2080 /* set up WA state to read the isoc packet status next. */
2081 wa->dti_isoc_xfer_in_progress = wa_xfer_id(xfer);
2082 wa->dti_isoc_xfer_seg = seg_idx;
2083 wa->dti_state = WA_DTI_ISOC_PACKET_STATUS_PENDING;
2084 } else if (xfer->is_inbound) { /* IN data phase: read to buffer */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002085 seg->status = WA_SEG_DTI_PENDING;
2086 BUG_ON(wa->buf_in_urb->status == -EINPROGRESS);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002087 /* this should always be 0 before a resubmit. */
2088 wa->buf_in_urb->num_mapped_sgs = 0;
2089
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002090 if (xfer->is_dma) {
2091 wa->buf_in_urb->transfer_dma =
2092 xfer->urb->transfer_dma
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002093 + (seg_idx * xfer->seg_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002094 wa->buf_in_urb->transfer_flags
2095 |= URB_NO_TRANSFER_DMA_MAP;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002096 wa->buf_in_urb->transfer_buffer = NULL;
2097 wa->buf_in_urb->sg = NULL;
2098 wa->buf_in_urb->num_sgs = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002099 } else {
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002100 /* do buffer or SG processing. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002101 wa->buf_in_urb->transfer_flags
2102 &= ~URB_NO_TRANSFER_DMA_MAP;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002103
2104 if (xfer->urb->transfer_buffer) {
2105 wa->buf_in_urb->transfer_buffer =
2106 xfer->urb->transfer_buffer
2107 + (seg_idx * xfer->seg_size);
2108 wa->buf_in_urb->sg = NULL;
2109 wa->buf_in_urb->num_sgs = 0;
2110 } else {
2111 /* allocate an SG list to store seg_size bytes
2112 and copy the subset of the xfer->urb->sg
2113 that matches the buffer subset we are
2114 about to read. */
2115 wa->buf_in_urb->sg = wa_xfer_create_subset_sg(
2116 xfer->urb->sg,
2117 seg_idx * xfer->seg_size,
2118 le32_to_cpu(
2119 xfer_result->dwTransferLength),
2120 &(wa->buf_in_urb->num_sgs));
2121
2122 if (!(wa->buf_in_urb->sg)) {
2123 wa->buf_in_urb->num_sgs = 0;
2124 goto error_sg_alloc;
2125 }
2126 wa->buf_in_urb->transfer_buffer = NULL;
2127 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002128 }
2129 wa->buf_in_urb->transfer_buffer_length =
2130 le32_to_cpu(xfer_result->dwTransferLength);
2131 wa->buf_in_urb->context = seg;
2132 result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
2133 if (result < 0)
2134 goto error_submit_buf_in;
2135 } else {
2136 /* OUT data phase, complete it -- */
2137 seg->status = WA_SEG_DONE;
2138 seg->result = le32_to_cpu(xfer_result->dwTransferLength);
2139 xfer->segs_done++;
2140 rpipe_ready = rpipe_avail_inc(rpipe);
2141 done = __wa_xfer_is_done(xfer);
2142 }
2143 spin_unlock_irqrestore(&xfer->lock, flags);
2144 if (done)
2145 wa_xfer_completion(xfer);
2146 if (rpipe_ready)
2147 wa_xfer_delayed_run(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002148 return;
2149
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002150error_submit_buf_in:
2151 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
2152 dev_err(dev, "DTI: URB max acceptable errors "
2153 "exceeded, resetting device\n");
2154 wa_reset_all(wa);
2155 }
2156 if (printk_ratelimit())
2157 dev_err(dev, "xfer %p#%u: can't submit DTI data phase: %d\n",
2158 xfer, seg_idx, result);
2159 seg->result = result;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002160 kfree(wa->buf_in_urb->sg);
Thomas Pugliese67414482013-09-26 14:08:16 -05002161 wa->buf_in_urb->sg = NULL;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002162error_sg_alloc:
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002163 __wa_xfer_abort(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002164 seg->status = WA_SEG_ERROR;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002165error_complete:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002166 xfer->segs_done++;
2167 rpipe_ready = rpipe_avail_inc(rpipe);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002168 wa_complete_remaining_xfer_segs(xfer, seg);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002169 done = __wa_xfer_is_done(xfer);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002170 /*
2171 * queue work item to clear STALL for control endpoints.
2172 * Otherwise, let endpoint_reset take care of it.
2173 */
2174 if (((usb_status & 0x3f) == WA_XFER_STATUS_HALTED) &&
2175 usb_endpoint_xfer_control(&xfer->ep->desc) &&
2176 done) {
2177
2178 dev_info(dev, "Control EP stall. Queue delayed work.\n");
2179 spin_lock_irq(&wa->xfer_list_lock);
Wei Yongjun8eb41292013-09-23 14:16:22 +08002180 /* move xfer from xfer_list to xfer_errored_list. */
2181 list_move_tail(&xfer->list_node, &wa->xfer_errored_list);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002182 spin_unlock_irq(&wa->xfer_list_lock);
2183 spin_unlock_irqrestore(&xfer->lock, flags);
2184 queue_work(wusbd, &wa->xfer_error_work);
2185 } else {
2186 spin_unlock_irqrestore(&xfer->lock, flags);
2187 if (done)
2188 wa_xfer_completion(xfer);
2189 if (rpipe_ready)
2190 wa_xfer_delayed_run(rpipe);
2191 }
2192
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002193 return;
2194
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002195error_bad_seg:
2196 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Puglieseb3744872013-11-25 16:17:16 -06002197 wa_urb_dequeue(wa, xfer->urb, -ENOENT);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002198 if (printk_ratelimit())
2199 dev_err(dev, "xfer %p#%u: bad segment\n", xfer, seg_idx);
2200 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
2201 dev_err(dev, "DTI: URB max acceptable errors "
2202 "exceeded, resetting device\n");
2203 wa_reset_all(wa);
2204 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002205 return;
2206
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002207segment_aborted:
2208 /* nothing to do, as the aborter did the completion */
2209 spin_unlock_irqrestore(&xfer->lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002210}
2211
2212/*
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002213 * Process a isochronous packet status message
2214 *
2215 * inbound transfers: need to schedule a buf_in_urb read
2216 */
2217static void wa_process_iso_packet_status(struct wahc *wa, struct urb *urb)
2218{
2219 struct device *dev = &wa->usb_iface->dev;
2220 struct wa_xfer_packet_status_hwaiso *packet_status;
Thomas Pugliese21012422013-10-23 14:44:27 -05002221 struct wa_xfer_packet_status_len_hwaiso *status_array;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002222 struct wa_xfer *xfer;
2223 unsigned long flags;
2224 struct wa_seg *seg;
2225 struct wa_rpipe *rpipe;
2226 unsigned done = 0;
Thomas Pugliese21012422013-10-23 14:44:27 -05002227 unsigned rpipe_ready = 0, seg_index;
2228 int expected_size;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002229
2230 /* We have a xfer result buffer; check it */
2231 dev_dbg(dev, "DTI: isoc packet status %d bytes at %p\n",
2232 urb->actual_length, urb->transfer_buffer);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002233 packet_status = (struct wa_xfer_packet_status_hwaiso *)(wa->dti_buf);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002234 if (packet_status->bPacketType != WA_XFER_ISO_PACKET_STATUS) {
2235 dev_err(dev, "DTI Error: isoc packet status--bad type 0x%02x\n",
2236 packet_status->bPacketType);
2237 goto error_parse_buffer;
2238 }
2239 xfer = wa_xfer_get_by_id(wa, wa->dti_isoc_xfer_in_progress);
2240 if (xfer == NULL) {
2241 dev_err(dev, "DTI Error: isoc packet status--unknown xfer 0x%08x\n",
2242 wa->dti_isoc_xfer_in_progress);
2243 goto error_parse_buffer;
2244 }
2245 spin_lock_irqsave(&xfer->lock, flags);
2246 if (unlikely(wa->dti_isoc_xfer_seg >= xfer->segs))
2247 goto error_bad_seg;
2248 seg = xfer->seg[wa->dti_isoc_xfer_seg];
2249 rpipe = xfer->ep->hcpriv;
Thomas Pugliese21012422013-10-23 14:44:27 -05002250 expected_size = sizeof(*packet_status) +
2251 (sizeof(packet_status->PacketStatus[0]) *
2252 seg->isoc_frame_count);
2253 if (urb->actual_length != expected_size) {
2254 dev_err(dev, "DTI Error: isoc packet status--bad urb length (%d bytes vs %d needed)\n",
2255 urb->actual_length, expected_size);
2256 goto error_bad_seg;
2257 }
2258 if (le16_to_cpu(packet_status->wLength) != expected_size) {
2259 dev_err(dev, "DTI Error: isoc packet status--bad length %u\n",
2260 le16_to_cpu(packet_status->wLength));
2261 goto error_bad_seg;
2262 }
2263 /* isoc packet status and lengths back xfer urb. */
2264 status_array = packet_status->PacketStatus;
2265 for (seg_index = 0; seg_index < seg->isoc_frame_count; ++seg_index) {
2266 xfer->urb->iso_frame_desc[seg->index].status =
2267 wa_xfer_status_to_errno(
2268 le16_to_cpu(status_array[seg_index].PacketStatus));
2269 xfer->urb->iso_frame_desc[seg->index].actual_length =
2270 le16_to_cpu(status_array[seg_index].PacketLength);
2271 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002272
2273 if (!xfer->is_inbound) {
2274 /* OUT transfer, complete it -- */
2275 seg->status = WA_SEG_DONE;
2276 xfer->segs_done++;
2277 rpipe_ready = rpipe_avail_inc(rpipe);
2278 done = __wa_xfer_is_done(xfer);
2279 }
2280 spin_unlock_irqrestore(&xfer->lock, flags);
2281 wa->dti_state = WA_DTI_TRANSFER_RESULT_PENDING;
2282 if (done)
2283 wa_xfer_completion(xfer);
2284 if (rpipe_ready)
2285 wa_xfer_delayed_run(rpipe);
2286 wa_xfer_put(xfer);
2287 return;
2288
2289error_bad_seg:
2290 spin_unlock_irqrestore(&xfer->lock, flags);
2291 wa_xfer_put(xfer);
2292error_parse_buffer:
2293 return;
2294}
2295
2296/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002297 * Callback for the IN data phase
2298 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002299 * If successful transition state; otherwise, take a note of the
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002300 * error, mark this segment done and try completion.
2301 *
2302 * Note we don't access until we are sure that the transfer hasn't
2303 * been cancelled (ECONNRESET, ENOENT), which could mean that
2304 * seg->xfer could be already gone.
2305 */
2306static void wa_buf_in_cb(struct urb *urb)
2307{
2308 struct wa_seg *seg = urb->context;
2309 struct wa_xfer *xfer = seg->xfer;
2310 struct wahc *wa;
2311 struct device *dev;
2312 struct wa_rpipe *rpipe;
2313 unsigned rpipe_ready;
2314 unsigned long flags;
2315 u8 done = 0;
2316
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002317 /* free the sg if it was used. */
2318 kfree(urb->sg);
2319 urb->sg = NULL;
2320
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002321 switch (urb->status) {
2322 case 0:
2323 spin_lock_irqsave(&xfer->lock, flags);
2324 wa = xfer->wa;
2325 dev = &wa->usb_iface->dev;
2326 rpipe = xfer->ep->hcpriv;
David Vrabelbce83692008-12-22 18:22:50 +00002327 dev_dbg(dev, "xfer %p#%u: data in done (%zu bytes)\n",
2328 xfer, seg->index, (size_t)urb->actual_length);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002329 seg->status = WA_SEG_DONE;
2330 seg->result = urb->actual_length;
2331 xfer->segs_done++;
2332 rpipe_ready = rpipe_avail_inc(rpipe);
2333 done = __wa_xfer_is_done(xfer);
2334 spin_unlock_irqrestore(&xfer->lock, flags);
2335 if (done)
2336 wa_xfer_completion(xfer);
2337 if (rpipe_ready)
2338 wa_xfer_delayed_run(rpipe);
2339 break;
2340 case -ECONNRESET: /* URB unlinked; no need to do anything */
2341 case -ENOENT: /* as it was done by the who unlinked us */
2342 break;
2343 default: /* Other errors ... */
2344 spin_lock_irqsave(&xfer->lock, flags);
2345 wa = xfer->wa;
2346 dev = &wa->usb_iface->dev;
2347 rpipe = xfer->ep->hcpriv;
2348 if (printk_ratelimit())
2349 dev_err(dev, "xfer %p#%u: data in error %d\n",
2350 xfer, seg->index, urb->status);
2351 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
2352 EDC_ERROR_TIMEFRAME)){
2353 dev_err(dev, "DTO: URB max acceptable errors "
2354 "exceeded, resetting device\n");
2355 wa_reset_all(wa);
2356 }
2357 seg->status = WA_SEG_ERROR;
2358 seg->result = urb->status;
2359 xfer->segs_done++;
2360 rpipe_ready = rpipe_avail_inc(rpipe);
2361 __wa_xfer_abort(xfer);
2362 done = __wa_xfer_is_done(xfer);
2363 spin_unlock_irqrestore(&xfer->lock, flags);
2364 if (done)
2365 wa_xfer_completion(xfer);
2366 if (rpipe_ready)
2367 wa_xfer_delayed_run(rpipe);
2368 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002369}
2370
2371/*
2372 * Handle an incoming transfer result buffer
2373 *
2374 * Given a transfer result buffer, it completes the transfer (possibly
2375 * scheduling and buffer in read) and then resubmits the DTI URB for a
2376 * new transfer result read.
2377 *
2378 *
2379 * The xfer_result DTI URB state machine
2380 *
2381 * States: OFF | RXR (Read-Xfer-Result) | RBI (Read-Buffer-In)
2382 *
2383 * We start in OFF mode, the first xfer_result notification [through
2384 * wa_handle_notif_xfer()] moves us to RXR by posting the DTI-URB to
2385 * read.
2386 *
2387 * We receive a buffer -- if it is not a xfer_result, we complain and
2388 * repost the DTI-URB. If it is a xfer_result then do the xfer seg
2389 * request accounting. If it is an IN segment, we move to RBI and post
2390 * a BUF-IN-URB to the right buffer. The BUF-IN-URB callback will
2391 * repost the DTI-URB and move to RXR state. if there was no IN
2392 * segment, it will repost the DTI-URB.
2393 *
2394 * We go back to OFF when we detect a ENOENT or ESHUTDOWN (or too many
2395 * errors) in the URBs.
2396 */
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002397static void wa_dti_cb(struct urb *urb)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002398{
2399 int result;
2400 struct wahc *wa = urb->context;
2401 struct device *dev = &wa->usb_iface->dev;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002402 u32 xfer_id;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002403 u8 usb_status;
2404
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002405 BUG_ON(wa->dti_urb != urb);
2406 switch (wa->dti_urb->status) {
2407 case 0:
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002408 if (wa->dti_state == WA_DTI_TRANSFER_RESULT_PENDING) {
2409 struct wa_xfer_result *xfer_result;
2410 struct wa_xfer *xfer;
2411
2412 /* We have a xfer result buffer; check it */
2413 dev_dbg(dev, "DTI: xfer result %d bytes at %p\n",
2414 urb->actual_length, urb->transfer_buffer);
2415 if (urb->actual_length != sizeof(*xfer_result)) {
2416 dev_err(dev, "DTI Error: xfer result--bad size xfer result (%d bytes vs %zu needed)\n",
2417 urb->actual_length,
2418 sizeof(*xfer_result));
2419 break;
2420 }
2421 xfer_result = (struct wa_xfer_result *)(wa->dti_buf);
2422 if (xfer_result->hdr.bLength != sizeof(*xfer_result)) {
2423 dev_err(dev, "DTI Error: xfer result--bad header length %u\n",
2424 xfer_result->hdr.bLength);
2425 break;
2426 }
2427 if (xfer_result->hdr.bNotifyType != WA_XFER_RESULT) {
2428 dev_err(dev, "DTI Error: xfer result--bad header type 0x%02x\n",
2429 xfer_result->hdr.bNotifyType);
2430 break;
2431 }
2432 usb_status = xfer_result->bTransferStatus & 0x3f;
2433 if (usb_status == WA_XFER_STATUS_NOT_FOUND)
2434 /* taken care of already */
2435 break;
2436 xfer_id = le32_to_cpu(xfer_result->dwTransferID);
2437 xfer = wa_xfer_get_by_id(wa, xfer_id);
2438 if (xfer == NULL) {
2439 /* FIXME: transaction not found. */
2440 dev_err(dev, "DTI Error: xfer result--unknown xfer 0x%08x (status 0x%02x)\n",
2441 xfer_id, usb_status);
2442 break;
2443 }
2444 wa_xfer_result_chew(wa, xfer, xfer_result);
2445 wa_xfer_put(xfer);
2446 } else if (wa->dti_state == WA_DTI_ISOC_PACKET_STATUS_PENDING) {
2447 wa_process_iso_packet_status(wa, urb);
2448 } else {
2449 dev_err(dev, "DTI Error: unexpected EP state = %d\n",
2450 wa->dti_state);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002451 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002452 break;
2453 case -ENOENT: /* (we killed the URB)...so, no broadcast */
2454 case -ESHUTDOWN: /* going away! */
2455 dev_dbg(dev, "DTI: going down! %d\n", urb->status);
2456 goto out;
2457 default:
2458 /* Unknown error */
2459 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS,
2460 EDC_ERROR_TIMEFRAME)) {
2461 dev_err(dev, "DTI: URB max acceptable errors "
2462 "exceeded, resetting device\n");
2463 wa_reset_all(wa);
2464 goto out;
2465 }
2466 if (printk_ratelimit())
2467 dev_err(dev, "DTI: URB error %d\n", urb->status);
2468 break;
2469 }
2470 /* Resubmit the DTI URB */
2471 result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
2472 if (result < 0) {
2473 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
2474 "resetting\n", result);
2475 wa_reset_all(wa);
2476 }
2477out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002478 return;
2479}
2480
2481/*
2482 * Transfer complete notification
2483 *
2484 * Called from the notif.c code. We get a notification on EP2 saying
2485 * that some endpoint has some transfer result data available. We are
2486 * about to read it.
2487 *
2488 * To speed up things, we always have a URB reading the DTI URB; we
2489 * don't really set it up and start it until the first xfer complete
2490 * notification arrives, which is what we do here.
2491 *
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002492 * Follow up in wa_dti_cb(), as that's where the whole state
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002493 * machine starts.
2494 *
2495 * So here we just initialize the DTI URB for reading transfer result
2496 * notifications and also the buffer-in URB, for reading buffers. Then
2497 * we just submit the DTI URB.
2498 *
2499 * @wa shall be referenced
2500 */
2501void wa_handle_notif_xfer(struct wahc *wa, struct wa_notif_hdr *notif_hdr)
2502{
2503 int result;
2504 struct device *dev = &wa->usb_iface->dev;
2505 struct wa_notif_xfer *notif_xfer;
2506 const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd;
2507
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002508 notif_xfer = container_of(notif_hdr, struct wa_notif_xfer, hdr);
2509 BUG_ON(notif_hdr->bNotifyType != WA_NOTIF_TRANSFER);
2510
2511 if ((0x80 | notif_xfer->bEndpoint) != dti_epd->bEndpointAddress) {
2512 /* FIXME: hardcoded limitation, adapt */
2513 dev_err(dev, "BUG: DTI ep is %u, not %u (hack me)\n",
2514 notif_xfer->bEndpoint, dti_epd->bEndpointAddress);
2515 goto error;
2516 }
2517 if (wa->dti_urb != NULL) /* DTI URB already started */
2518 goto out;
2519
2520 wa->dti_urb = usb_alloc_urb(0, GFP_KERNEL);
2521 if (wa->dti_urb == NULL) {
2522 dev_err(dev, "Can't allocate DTI URB\n");
2523 goto error_dti_urb_alloc;
2524 }
2525 usb_fill_bulk_urb(
2526 wa->dti_urb, wa->usb_dev,
2527 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002528 wa->dti_buf, wa->dti_buf_size,
2529 wa_dti_cb, wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002530
2531 wa->buf_in_urb = usb_alloc_urb(0, GFP_KERNEL);
2532 if (wa->buf_in_urb == NULL) {
2533 dev_err(dev, "Can't allocate BUF-IN URB\n");
2534 goto error_buf_in_urb_alloc;
2535 }
2536 usb_fill_bulk_urb(
2537 wa->buf_in_urb, wa->usb_dev,
2538 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
2539 NULL, 0, wa_buf_in_cb, wa);
2540 result = usb_submit_urb(wa->dti_urb, GFP_KERNEL);
2541 if (result < 0) {
2542 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
2543 "resetting\n", result);
2544 goto error_dti_urb_submit;
2545 }
2546out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002547 return;
2548
2549error_dti_urb_submit:
2550 usb_put_urb(wa->buf_in_urb);
Thomas Pugliese67414482013-09-26 14:08:16 -05002551 wa->buf_in_urb = NULL;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002552error_buf_in_urb_alloc:
2553 usb_put_urb(wa->dti_urb);
2554 wa->dti_urb = NULL;
2555error_dti_urb_alloc:
2556error:
2557 wa_reset_all(wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002558}