blob: a70e142da3302ca75432bea0691431ee7f9ef190 [file] [log] [blame]
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001/*
2 * WUSB Wire Adapter
3 * Data transfer and URB enqueing
4 *
5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 *
22 *
23 * How transfers work: get a buffer, break it up in segments (segment
24 * size is a multiple of the maxpacket size). For each segment issue a
25 * segment request (struct wa_xfer_*), then send the data buffer if
26 * out or nothing if in (all over the DTO endpoint).
27 *
28 * For each submitted segment request, a notification will come over
29 * the NEP endpoint and a transfer result (struct xfer_result) will
30 * arrive in the DTI URB. Read it, get the xfer ID, see if there is
31 * data coming (inbound transfer), schedule a read and handle it.
32 *
33 * Sounds simple, it is a pain to implement.
34 *
35 *
36 * ENTRY POINTS
37 *
38 * FIXME
39 *
40 * LIFE CYCLE / STATE DIAGRAM
41 *
42 * FIXME
43 *
44 * THIS CODE IS DISGUSTING
45 *
46 * Warned you are; it's my second try and still not happy with it.
47 *
48 * NOTES:
49 *
50 * - No iso
51 *
52 * - Supports DMA xfers, control, bulk and maybe interrupt
53 *
54 * - Does not recycle unused rpipes
55 *
56 * An rpipe is assigned to an endpoint the first time it is used,
57 * and then it's there, assigned, until the endpoint is disabled
58 * (destroyed [{h,d}wahc_op_ep_disable()]. The assignment of the
59 * rpipe to the endpoint is done under the wa->rpipe_sem semaphore
60 * (should be a mutex).
61 *
62 * Two methods it could be done:
63 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030064 * (a) set up a timer every time an rpipe's use count drops to 1
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010065 * (which means unused) or when a transfer ends. Reset the
66 * timer when a xfer is queued. If the timer expires, release
67 * the rpipe [see rpipe_ep_disable()].
68 *
69 * (b) when looking for free rpipes to attach [rpipe_get_by_ep()],
70 * when none are found go over the list, check their endpoint
71 * and their activity record (if no last-xfer-done-ts in the
72 * last x seconds) take it
73 *
74 * However, due to the fact that we have a set of limited
75 * resources (max-segments-at-the-same-time per xfer,
76 * xfers-per-ripe, blocks-per-rpipe, rpipes-per-host), at the end
77 * we are going to have to rebuild all this based on an scheduler,
78 * to where we have a list of transactions to do and based on the
Gilles Espinassef77f13e2010-03-29 15:41:47 +020079 * availability of the different required components (blocks,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010080 * rpipes, segment slots, etc), we go scheduling them. Painful.
81 */
82#include <linux/init.h>
83#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090084#include <linux/slab.h>
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010085#include <linux/hash.h>
Manuel Zerpies9708cd22011-06-16 14:15:16 +020086#include <linux/ratelimit.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040087#include <linux/export.h>
Thomas Pugliese2b81c082013-06-11 10:39:31 -050088#include <linux/scatterlist.h>
David Vrabelbce83692008-12-22 18:22:50 +000089
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010090#include "wa-hc.h"
91#include "wusbhc.h"
92
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010093enum {
Thomas Pugliesef74b75e2013-10-23 14:44:29 -050094 /* [WUSB] section 8.3.3 allocates 7 bits for the segment index. */
95 WA_SEGS_MAX = 128,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010096};
97
98enum wa_seg_status {
99 WA_SEG_NOTREADY,
100 WA_SEG_READY,
101 WA_SEG_DELAYED,
102 WA_SEG_SUBMITTED,
103 WA_SEG_PENDING,
104 WA_SEG_DTI_PENDING,
105 WA_SEG_DONE,
106 WA_SEG_ERROR,
107 WA_SEG_ABORTED,
108};
109
110static void wa_xfer_delayed_run(struct wa_rpipe *);
Thomas Pugliese679ee472013-10-07 10:53:57 -0500111static int __wa_xfer_delayed_run(struct wa_rpipe *rpipe, int *dto_waiting);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100112
113/*
114 * Life cycle governed by 'struct urb' (the refcount of the struct is
115 * that of the 'struct urb' and usb_free_urb() would free the whole
116 * struct).
117 */
118struct wa_seg {
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500119 struct urb tr_urb; /* transfer request urb. */
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500120 struct urb *isoc_pack_desc_urb; /* for isoc packet descriptor. */
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500121 struct urb *dto_urb; /* for data output. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100122 struct list_head list_node; /* for rpipe->req_list */
123 struct wa_xfer *xfer; /* out xfer */
124 u8 index; /* which segment we are */
Thomas Pugliese21012422013-10-23 14:44:27 -0500125 int isoc_frame_count; /* number of isoc frames in this segment. */
126 int isoc_frame_offset; /* starting frame offset in the xfer URB. */
Thomas Puglieseea1af422013-12-09 14:15:14 -0600127 /* Isoc frame that the current transfer buffer corresponds to. */
128 int isoc_frame_index;
Thomas Pugliese21012422013-10-23 14:44:27 -0500129 int isoc_size; /* size of all isoc frames sent by this seg. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100130 enum wa_seg_status status;
131 ssize_t result; /* bytes xfered or error */
132 struct wa_xfer_hdr xfer_hdr;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100133};
134
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500135static inline void wa_seg_init(struct wa_seg *seg)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100136{
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500137 usb_init_urb(&seg->tr_urb);
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500138
139 /* set the remaining memory to 0. */
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500140 memset(((void *)seg) + sizeof(seg->tr_urb), 0,
141 sizeof(*seg) - sizeof(seg->tr_urb));
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100142}
143
144/*
145 * Protected by xfer->lock
146 *
147 */
148struct wa_xfer {
149 struct kref refcnt;
150 struct list_head list_node;
151 spinlock_t lock;
152 u32 id;
153
154 struct wahc *wa; /* Wire adapter we are plugged to */
155 struct usb_host_endpoint *ep;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300156 struct urb *urb; /* URB we are transferring for */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100157 struct wa_seg **seg; /* transfer segments */
158 u8 segs, segs_submitted, segs_done;
159 unsigned is_inbound:1;
160 unsigned is_dma:1;
161 size_t seg_size;
162 int result;
163
164 gfp_t gfp; /* allocation mask */
165
166 struct wusb_dev *wusb_dev; /* for activity timestamps */
167};
168
Thomas Pugliese21012422013-10-23 14:44:27 -0500169static void __wa_populate_dto_urb_isoc(struct wa_xfer *xfer,
170 struct wa_seg *seg, int curr_iso_frame);
171
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100172static inline void wa_xfer_init(struct wa_xfer *xfer)
173{
174 kref_init(&xfer->refcnt);
175 INIT_LIST_HEAD(&xfer->list_node);
176 spin_lock_init(&xfer->lock);
177}
178
179/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300180 * Destroy a transfer structure
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100181 *
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500182 * Note that freeing xfer->seg[cnt]->tr_urb will free the containing
Thomas Pugliese79731cb2013-08-15 14:37:42 -0500183 * xfer->seg[cnt] memory that was allocated by __wa_xfer_setup_segs.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100184 */
185static void wa_xfer_destroy(struct kref *_xfer)
186{
187 struct wa_xfer *xfer = container_of(_xfer, struct wa_xfer, refcnt);
188 if (xfer->seg) {
189 unsigned cnt;
190 for (cnt = 0; cnt < xfer->segs; cnt++) {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500191 struct wa_seg *seg = xfer->seg[cnt];
192 if (seg) {
193 usb_free_urb(seg->isoc_pack_desc_urb);
194 if (seg->dto_urb) {
195 kfree(seg->dto_urb->sg);
196 usb_free_urb(seg->dto_urb);
Thomas Pugliesed9936702013-09-26 14:08:13 -0500197 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500198 usb_free_urb(&seg->tr_urb);
Thomas Pugliesed9936702013-09-26 14:08:13 -0500199 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100200 }
Thomas Pugliesed9936702013-09-26 14:08:13 -0500201 kfree(xfer->seg);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100202 }
203 kfree(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100204}
205
206static void wa_xfer_get(struct wa_xfer *xfer)
207{
208 kref_get(&xfer->refcnt);
209}
210
211static void wa_xfer_put(struct wa_xfer *xfer)
212{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100213 kref_put(&xfer->refcnt, wa_xfer_destroy);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100214}
215
216/*
Thomas Pugliese679ee472013-10-07 10:53:57 -0500217 * Try to get exclusive access to the DTO endpoint resource. Return true
218 * if successful.
219 */
220static inline int __wa_dto_try_get(struct wahc *wa)
221{
222 return (test_and_set_bit(0, &wa->dto_in_use) == 0);
223}
224
225/* Release the DTO endpoint resource. */
226static inline void __wa_dto_put(struct wahc *wa)
227{
228 clear_bit_unlock(0, &wa->dto_in_use);
229}
230
231/* Service RPIPEs that are waiting on the DTO resource. */
232static void wa_check_for_delayed_rpipes(struct wahc *wa)
233{
234 unsigned long flags;
235 int dto_waiting = 0;
236 struct wa_rpipe *rpipe;
237
238 spin_lock_irqsave(&wa->rpipe_lock, flags);
239 while (!list_empty(&wa->rpipe_delayed_list) && !dto_waiting) {
240 rpipe = list_first_entry(&wa->rpipe_delayed_list,
241 struct wa_rpipe, list_node);
242 __wa_xfer_delayed_run(rpipe, &dto_waiting);
243 /* remove this RPIPE from the list if it is not waiting. */
244 if (!dto_waiting) {
245 pr_debug("%s: RPIPE %d serviced and removed from delayed list.\n",
246 __func__,
247 le16_to_cpu(rpipe->descr.wRPipeIndex));
248 list_del_init(&rpipe->list_node);
249 }
250 }
251 spin_unlock_irqrestore(&wa->rpipe_lock, flags);
252}
253
254/* add this RPIPE to the end of the delayed RPIPE list. */
255static void wa_add_delayed_rpipe(struct wahc *wa, struct wa_rpipe *rpipe)
256{
257 unsigned long flags;
258
259 spin_lock_irqsave(&wa->rpipe_lock, flags);
260 /* add rpipe to the list if it is not already on it. */
261 if (list_empty(&rpipe->list_node)) {
262 pr_debug("%s: adding RPIPE %d to the delayed list.\n",
263 __func__, le16_to_cpu(rpipe->descr.wRPipeIndex));
264 list_add_tail(&rpipe->list_node, &wa->rpipe_delayed_list);
265 }
266 spin_unlock_irqrestore(&wa->rpipe_lock, flags);
267}
268
269/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100270 * xfer is referenced
271 *
272 * xfer->lock has to be unlocked
273 *
274 * We take xfer->lock for setting the result; this is a barrier
275 * against drivers/usb/core/hcd.c:unlink1() being called after we call
276 * usb_hcd_giveback_urb() and wa_urb_dequeue() trying to get a
277 * reference to the transfer.
278 */
279static void wa_xfer_giveback(struct wa_xfer *xfer)
280{
281 unsigned long flags;
David Vrabelbce83692008-12-22 18:22:50 +0000282
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100283 spin_lock_irqsave(&xfer->wa->xfer_list_lock, flags);
284 list_del_init(&xfer->list_node);
Thomas Puglieseb3744872013-11-25 16:17:16 -0600285 usb_hcd_unlink_urb_from_ep(&(xfer->wa->wusb->usb_hcd), xfer->urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100286 spin_unlock_irqrestore(&xfer->wa->xfer_list_lock, flags);
287 /* FIXME: segmentation broken -- kills DWA */
288 wusbhc_giveback_urb(xfer->wa->wusb, xfer->urb, xfer->result);
289 wa_put(xfer->wa);
290 wa_xfer_put(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100291}
292
293/*
294 * xfer is referenced
295 *
296 * xfer->lock has to be unlocked
297 */
298static void wa_xfer_completion(struct wa_xfer *xfer)
299{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100300 if (xfer->wusb_dev)
301 wusb_dev_put(xfer->wusb_dev);
302 rpipe_put(xfer->ep->hcpriv);
303 wa_xfer_giveback(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100304}
305
306/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100307 * Initialize a transfer's ID
308 *
309 * We need to use a sequential number; if we use the pointer or the
310 * hash of the pointer, it can repeat over sequential transfers and
311 * then it will confuse the HWA....wonder why in hell they put a 32
312 * bit handle in there then.
313 */
314static void wa_xfer_id_init(struct wa_xfer *xfer)
315{
316 xfer->id = atomic_add_return(1, &xfer->wa->xfer_id_count);
317}
318
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500319/* Return the xfer's ID. */
320static inline u32 wa_xfer_id(struct wa_xfer *xfer)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100321{
322 return xfer->id;
323}
324
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500325/* Return the xfer's ID in transport format (little endian). */
326static inline __le32 wa_xfer_id_le32(struct wa_xfer *xfer)
327{
328 return cpu_to_le32(xfer->id);
329}
330
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100331/*
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500332 * If transfer is done, wrap it up and return true
333 *
334 * xfer->lock has to be locked
335 */
336static unsigned __wa_xfer_is_done(struct wa_xfer *xfer)
337{
338 struct device *dev = &xfer->wa->usb_iface->dev;
339 unsigned result, cnt;
340 struct wa_seg *seg;
341 struct urb *urb = xfer->urb;
342 unsigned found_short = 0;
343
344 result = xfer->segs_done == xfer->segs_submitted;
345 if (result == 0)
346 goto out;
347 urb->actual_length = 0;
348 for (cnt = 0; cnt < xfer->segs; cnt++) {
349 seg = xfer->seg[cnt];
350 switch (seg->status) {
351 case WA_SEG_DONE:
352 if (found_short && seg->result > 0) {
353 dev_dbg(dev, "xfer %p ID %08X#%u: bad short segments (%zu)\n",
354 xfer, wa_xfer_id(xfer), cnt,
355 seg->result);
356 urb->status = -EINVAL;
357 goto out;
358 }
359 urb->actual_length += seg->result;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500360 if (!(usb_pipeisoc(xfer->urb->pipe))
361 && seg->result < xfer->seg_size
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500362 && cnt != xfer->segs-1)
363 found_short = 1;
364 dev_dbg(dev, "xfer %p ID %08X#%u: DONE short %d "
365 "result %zu urb->actual_length %d\n",
366 xfer, wa_xfer_id(xfer), seg->index, found_short,
367 seg->result, urb->actual_length);
368 break;
369 case WA_SEG_ERROR:
370 xfer->result = seg->result;
Thomas Pugliesecccd3a252013-09-30 22:48:46 -0500371 dev_dbg(dev, "xfer %p ID %08X#%u: ERROR result %zu(0x%08zX)\n",
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500372 xfer, wa_xfer_id(xfer), seg->index, seg->result,
373 seg->result);
374 goto out;
375 case WA_SEG_ABORTED:
Thomas Pugliesebbfc34202013-11-25 16:17:17 -0600376 xfer->result = seg->result;
377 dev_dbg(dev, "xfer %p ID %08X#%u: ABORTED result %zu(0x%08zX)\n",
378 xfer, wa_xfer_id(xfer), seg->index, seg->result,
379 seg->result);
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500380 goto out;
381 default:
382 dev_warn(dev, "xfer %p ID %08X#%u: is_done bad state %d\n",
383 xfer, wa_xfer_id(xfer), cnt, seg->status);
384 xfer->result = -EINVAL;
385 goto out;
386 }
387 }
388 xfer->result = 0;
389out:
390 return result;
391}
392
393/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100394 * Search for a transfer list ID on the HCD's URB list
395 *
396 * For 32 bit architectures, we use the pointer itself; for 64 bits, a
397 * 32-bit hash of the pointer.
398 *
399 * @returns NULL if not found.
400 */
401static struct wa_xfer *wa_xfer_get_by_id(struct wahc *wa, u32 id)
402{
403 unsigned long flags;
404 struct wa_xfer *xfer_itr;
405 spin_lock_irqsave(&wa->xfer_list_lock, flags);
406 list_for_each_entry(xfer_itr, &wa->xfer_list, list_node) {
407 if (id == xfer_itr->id) {
408 wa_xfer_get(xfer_itr);
409 goto out;
410 }
411 }
412 xfer_itr = NULL;
413out:
414 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
415 return xfer_itr;
416}
417
418struct wa_xfer_abort_buffer {
419 struct urb urb;
420 struct wa_xfer_abort cmd;
421};
422
423static void __wa_xfer_abort_cb(struct urb *urb)
424{
425 struct wa_xfer_abort_buffer *b = urb->context;
426 usb_put_urb(&b->urb);
427}
428
429/*
430 * Aborts an ongoing transaction
431 *
432 * Assumes the transfer is referenced and locked and in a submitted
433 * state (mainly that there is an endpoint/rpipe assigned).
434 *
435 * The callback (see above) does nothing but freeing up the data by
436 * putting the URB. Because the URB is allocated at the head of the
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -0500437 * struct, the whole space we allocated is kfreed. *
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100438 */
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -0500439static int __wa_xfer_abort(struct wa_xfer *xfer)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100440{
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -0500441 int result = -ENOMEM;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100442 struct device *dev = &xfer->wa->usb_iface->dev;
443 struct wa_xfer_abort_buffer *b;
444 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
445
446 b = kmalloc(sizeof(*b), GFP_ATOMIC);
447 if (b == NULL)
448 goto error_kmalloc;
449 b->cmd.bLength = sizeof(b->cmd);
450 b->cmd.bRequestType = WA_XFER_ABORT;
451 b->cmd.wRPipe = rpipe->descr.wRPipeIndex;
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500452 b->cmd.dwTransferID = wa_xfer_id_le32(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100453
454 usb_init_urb(&b->urb);
455 usb_fill_bulk_urb(&b->urb, xfer->wa->usb_dev,
456 usb_sndbulkpipe(xfer->wa->usb_dev,
457 xfer->wa->dto_epd->bEndpointAddress),
458 &b->cmd, sizeof(b->cmd), __wa_xfer_abort_cb, b);
459 result = usb_submit_urb(&b->urb, GFP_ATOMIC);
460 if (result < 0)
461 goto error_submit;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -0500462 return result; /* callback frees! */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100463
464
465error_submit:
466 if (printk_ratelimit())
467 dev_err(dev, "xfer %p: Can't submit abort request: %d\n",
468 xfer, result);
469 kfree(b);
470error_kmalloc:
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -0500471 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100472
473}
474
475/*
Thomas Pugliese21012422013-10-23 14:44:27 -0500476 * Calculate the number of isoc frames starting from isoc_frame_offset
477 * that will fit a in transfer segment.
478 */
479static int __wa_seg_calculate_isoc_frame_count(struct wa_xfer *xfer,
480 int isoc_frame_offset, int *total_size)
481{
482 int segment_size = 0, frame_count = 0;
483 int index = isoc_frame_offset;
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500484 struct usb_iso_packet_descriptor *iso_frame_desc =
485 xfer->urb->iso_frame_desc;
Thomas Pugliese21012422013-10-23 14:44:27 -0500486
487 while ((index < xfer->urb->number_of_packets)
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500488 && ((segment_size + iso_frame_desc[index].length)
Thomas Pugliese21012422013-10-23 14:44:27 -0500489 <= xfer->seg_size)) {
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500490 /*
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)
Thomas Puglieseea1af422013-12-09 14:15:14 -0600704 seg->isoc_frame_index += seg->isoc_frame_count;
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500705 else
Thomas Puglieseea1af422013-12-09 14:15:14 -0600706 seg->isoc_frame_index += 1;
707 if (seg->isoc_frame_index < seg->isoc_frame_count) {
Thomas Pugliese21012422013-10-23 14:44:27 -0500708 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 */
Thomas Puglieseea1af422013-12-09 14:15:14 -0600714 if ((seg->isoc_frame_index + 1) >=
Thomas Pugliese21012422013-10-23 14:44:27 -0500715 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",
Thomas Puglieseea1af422013-12-09 14:15:14 -0600719 wa_xfer_id(xfer), seg->index, seg->isoc_frame_index,
720 holding_dto, release_dto);
Thomas Pugliese21012422013-10-23 14:44:27 -0500721 }
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,
Thomas Puglieseea1af422013-12-09 14:15:14 -0600740 seg->isoc_frame_offset + seg->isoc_frame_index);
Thomas Pugliese21012422013-10-23 14:44:27 -0500741
742 /* resubmit the URB with the next isoc frame. */
743 result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
744 if (result < 0) {
745 dev_err(dev, "xfer 0x%08X#%u: DTO submit failed: %d\n",
746 wa_xfer_id(xfer), seg->index, result);
747 spin_unlock_irqrestore(&xfer->lock, flags);
748 goto error_dto_submit;
749 }
750 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100751 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Pugliese21012422013-10-23 14:44:27 -0500752 if (release_dto) {
753 __wa_dto_put(wa);
754 wa_check_for_delayed_rpipes(wa);
755 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100756 break;
757 case -ECONNRESET: /* URB unlinked; no need to do anything */
758 case -ENOENT: /* as it was done by the who unlinked us */
Thomas Pugliese21012422013-10-23 14:44:27 -0500759 if (holding_dto) {
760 __wa_dto_put(wa);
761 wa_check_for_delayed_rpipes(wa);
762 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100763 break;
764 default: /* Other errors ... */
Thomas Pugliese21012422013-10-23 14:44:27 -0500765 dev_err(dev, "xfer 0x%08X#%u: data out error %d\n",
766 wa_xfer_id(xfer), seg->index, urb->status);
767 goto error_default;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100768 }
Thomas Pugliese21012422013-10-23 14:44:27 -0500769
770 return;
771
772error_dto_submit:
773error_default:
774 spin_lock_irqsave(&xfer->lock, flags);
775 rpipe = xfer->ep->hcpriv;
776 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
777 EDC_ERROR_TIMEFRAME)){
778 dev_err(dev, "DTO: URB max acceptable errors exceeded, resetting device\n");
779 wa_reset_all(wa);
780 }
781 if (seg->status != WA_SEG_ERROR) {
782 seg->status = WA_SEG_ERROR;
783 seg->result = urb->status;
784 xfer->segs_done++;
785 __wa_xfer_abort(xfer);
786 rpipe_ready = rpipe_avail_inc(rpipe);
787 done = __wa_xfer_is_done(xfer);
788 }
789 spin_unlock_irqrestore(&xfer->lock, flags);
790 if (holding_dto) {
791 __wa_dto_put(wa);
792 wa_check_for_delayed_rpipes(wa);
793 }
794 if (done)
795 wa_xfer_completion(xfer);
796 if (rpipe_ready)
797 wa_xfer_delayed_run(rpipe);
798
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100799}
800
801/*
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500802 * Callback for the isoc packet descriptor phase of the segment request
803 *
804 * Check wa_seg_tr_cb(); most comments also apply here because this
805 * function does almost the same thing and they work closely
806 * together.
807 *
808 * If the seg request has failed but this phase has succeeded,
809 * wa_seg_tr_cb() has already failed the segment and moved the
810 * status to WA_SEG_ERROR, so this will go through 'case 0' and
811 * effectively do nothing.
812 */
813static void wa_seg_iso_pack_desc_cb(struct urb *urb)
814{
815 struct wa_seg *seg = urb->context;
816 struct wa_xfer *xfer = seg->xfer;
817 struct wahc *wa;
818 struct device *dev;
819 struct wa_rpipe *rpipe;
820 unsigned long flags;
821 unsigned rpipe_ready = 0;
822 u8 done = 0;
823
824 switch (urb->status) {
825 case 0:
826 spin_lock_irqsave(&xfer->lock, flags);
827 wa = xfer->wa;
828 dev = &wa->usb_iface->dev;
Thomas Pugliese21012422013-10-23 14:44:27 -0500829 dev_dbg(dev, "iso xfer %08X#%u: packet descriptor done\n",
830 wa_xfer_id(xfer), seg->index);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500831 if (xfer->is_inbound && seg->status < WA_SEG_PENDING)
832 seg->status = WA_SEG_PENDING;
833 spin_unlock_irqrestore(&xfer->lock, flags);
834 break;
835 case -ECONNRESET: /* URB unlinked; no need to do anything */
836 case -ENOENT: /* as it was done by the who unlinked us */
837 break;
838 default: /* Other errors ... */
839 spin_lock_irqsave(&xfer->lock, flags);
840 wa = xfer->wa;
841 dev = &wa->usb_iface->dev;
842 rpipe = xfer->ep->hcpriv;
Thomas Pugliese21012422013-10-23 14:44:27 -0500843 pr_err_ratelimited("iso xfer %08X#%u: packet descriptor error %d\n",
844 wa_xfer_id(xfer), seg->index, urb->status);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500845 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
846 EDC_ERROR_TIMEFRAME)){
847 dev_err(dev, "DTO: URB max acceptable errors exceeded, resetting device\n");
848 wa_reset_all(wa);
849 }
850 if (seg->status != WA_SEG_ERROR) {
851 usb_unlink_urb(seg->dto_urb);
852 seg->status = WA_SEG_ERROR;
853 seg->result = urb->status;
854 xfer->segs_done++;
855 __wa_xfer_abort(xfer);
856 rpipe_ready = rpipe_avail_inc(rpipe);
857 done = __wa_xfer_is_done(xfer);
858 }
859 spin_unlock_irqrestore(&xfer->lock, flags);
860 if (done)
861 wa_xfer_completion(xfer);
862 if (rpipe_ready)
863 wa_xfer_delayed_run(rpipe);
864 }
865}
866
867/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100868 * Callback for the segment request
869 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200870 * If successful transition state (unless already transitioned or
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100871 * outbound transfer); otherwise, take a note of the error, mark this
872 * segment done and try completion.
873 *
874 * Note we don't access until we are sure that the transfer hasn't
875 * been cancelled (ECONNRESET, ENOENT), which could mean that
876 * seg->xfer could be already gone.
877 *
878 * We have to check before setting the status to WA_SEG_PENDING
879 * because sometimes the xfer result callback arrives before this
880 * callback (geeeeeeze), so it might happen that we are already in
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500881 * another state. As well, we don't set it if the transfer is not inbound,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100882 * as in that case, wa_seg_dto_cb will do it when the OUT data phase
883 * finishes.
884 */
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500885static void wa_seg_tr_cb(struct urb *urb)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100886{
887 struct wa_seg *seg = urb->context;
888 struct wa_xfer *xfer = seg->xfer;
889 struct wahc *wa;
890 struct device *dev;
891 struct wa_rpipe *rpipe;
892 unsigned long flags;
893 unsigned rpipe_ready;
894 u8 done = 0;
895
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100896 switch (urb->status) {
897 case 0:
898 spin_lock_irqsave(&xfer->lock, flags);
899 wa = xfer->wa;
900 dev = &wa->usb_iface->dev;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500901 dev_dbg(dev, "xfer %p ID 0x%08X#%u: request done\n",
902 xfer, wa_xfer_id(xfer), seg->index);
903 if (xfer->is_inbound &&
904 seg->status < WA_SEG_PENDING &&
905 !(usb_pipeisoc(xfer->urb->pipe)))
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100906 seg->status = WA_SEG_PENDING;
907 spin_unlock_irqrestore(&xfer->lock, flags);
908 break;
909 case -ECONNRESET: /* URB unlinked; no need to do anything */
910 case -ENOENT: /* as it was done by the who unlinked us */
911 break;
912 default: /* Other errors ... */
913 spin_lock_irqsave(&xfer->lock, flags);
914 wa = xfer->wa;
915 dev = &wa->usb_iface->dev;
916 rpipe = xfer->ep->hcpriv;
917 if (printk_ratelimit())
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500918 dev_err(dev, "xfer %p ID 0x%08X#%u: request error %d\n",
919 xfer, wa_xfer_id(xfer), seg->index,
920 urb->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100921 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
922 EDC_ERROR_TIMEFRAME)){
923 dev_err(dev, "DTO: URB max acceptable errors "
924 "exceeded, resetting device\n");
925 wa_reset_all(wa);
926 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500927 usb_unlink_urb(seg->isoc_pack_desc_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100928 usb_unlink_urb(seg->dto_urb);
929 seg->status = WA_SEG_ERROR;
930 seg->result = urb->status;
931 xfer->segs_done++;
932 __wa_xfer_abort(xfer);
933 rpipe_ready = rpipe_avail_inc(rpipe);
934 done = __wa_xfer_is_done(xfer);
935 spin_unlock_irqrestore(&xfer->lock, flags);
936 if (done)
937 wa_xfer_completion(xfer);
938 if (rpipe_ready)
939 wa_xfer_delayed_run(rpipe);
940 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100941}
942
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500943/*
944 * Allocate an SG list to store bytes_to_transfer bytes and copy the
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500945 * subset of the in_sg that matches the buffer subset
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500946 * we are about to transfer.
947 */
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500948static struct scatterlist *wa_xfer_create_subset_sg(struct scatterlist *in_sg,
949 const unsigned int bytes_transferred,
950 const unsigned int bytes_to_transfer, unsigned int *out_num_sgs)
951{
952 struct scatterlist *out_sg;
953 unsigned int bytes_processed = 0, offset_into_current_page_data = 0,
954 nents;
955 struct scatterlist *current_xfer_sg = in_sg;
956 struct scatterlist *current_seg_sg, *last_seg_sg;
957
958 /* skip previously transferred pages. */
959 while ((current_xfer_sg) &&
960 (bytes_processed < bytes_transferred)) {
961 bytes_processed += current_xfer_sg->length;
962
963 /* advance the sg if current segment starts on or past the
964 next page. */
965 if (bytes_processed <= bytes_transferred)
966 current_xfer_sg = sg_next(current_xfer_sg);
967 }
968
969 /* the data for the current segment starts in current_xfer_sg.
970 calculate the offset. */
971 if (bytes_processed > bytes_transferred) {
972 offset_into_current_page_data = current_xfer_sg->length -
973 (bytes_processed - bytes_transferred);
974 }
975
976 /* calculate the number of pages needed by this segment. */
977 nents = DIV_ROUND_UP((bytes_to_transfer +
978 offset_into_current_page_data +
979 current_xfer_sg->offset),
980 PAGE_SIZE);
981
982 out_sg = kmalloc((sizeof(struct scatterlist) * nents), GFP_ATOMIC);
983 if (out_sg) {
984 sg_init_table(out_sg, nents);
985
986 /* copy the portion of the incoming SG that correlates to the
987 * data to be transferred by this segment to the segment SG. */
988 last_seg_sg = current_seg_sg = out_sg;
989 bytes_processed = 0;
990
991 /* reset nents and calculate the actual number of sg entries
992 needed. */
993 nents = 0;
994 while ((bytes_processed < bytes_to_transfer) &&
995 current_seg_sg && current_xfer_sg) {
996 unsigned int page_len = min((current_xfer_sg->length -
997 offset_into_current_page_data),
998 (bytes_to_transfer - bytes_processed));
999
1000 sg_set_page(current_seg_sg, sg_page(current_xfer_sg),
1001 page_len,
1002 current_xfer_sg->offset +
1003 offset_into_current_page_data);
1004
1005 bytes_processed += page_len;
1006
1007 last_seg_sg = current_seg_sg;
1008 current_seg_sg = sg_next(current_seg_sg);
1009 current_xfer_sg = sg_next(current_xfer_sg);
1010
1011 /* only the first page may require additional offset. */
1012 offset_into_current_page_data = 0;
1013 nents++;
1014 }
1015
1016 /* update num_sgs and terminate the list since we may have
1017 * concatenated pages. */
1018 sg_mark_end(last_seg_sg);
1019 *out_num_sgs = nents;
1020 }
1021
1022 return out_sg;
1023}
1024
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001025/*
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001026 * Populate DMA buffer info for the isoc dto urb.
1027 */
Thomas Pugliese21012422013-10-23 14:44:27 -05001028static void __wa_populate_dto_urb_isoc(struct wa_xfer *xfer,
1029 struct wa_seg *seg, int curr_iso_frame)
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001030{
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001031 seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1032 seg->dto_urb->sg = NULL;
1033 seg->dto_urb->num_sgs = 0;
Thomas Pugliesef07ddb92013-10-23 14:44:28 -05001034 /* dto urb buffer address pulled from iso_frame_desc. */
1035 seg->dto_urb->transfer_dma = xfer->urb->transfer_dma +
1036 xfer->urb->iso_frame_desc[curr_iso_frame].offset;
1037 /* The Alereon HWA sends a single URB with all isoc segs. */
1038 if (xfer->wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC)
1039 seg->dto_urb->transfer_buffer_length = seg->isoc_size;
1040 else
1041 seg->dto_urb->transfer_buffer_length =
1042 xfer->urb->iso_frame_desc[curr_iso_frame].length;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001043}
1044
1045/*
Thomas Puglieseffd6d172013-09-26 14:08:14 -05001046 * Populate buffer ptr and size, DMA buffer or SG list for the dto urb.
1047 */
1048static int __wa_populate_dto_urb(struct wa_xfer *xfer,
1049 struct wa_seg *seg, size_t buf_itr_offset, size_t buf_itr_size)
1050{
1051 int result = 0;
1052
1053 if (xfer->is_dma) {
1054 seg->dto_urb->transfer_dma =
1055 xfer->urb->transfer_dma + buf_itr_offset;
1056 seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1057 seg->dto_urb->sg = NULL;
1058 seg->dto_urb->num_sgs = 0;
1059 } else {
1060 /* do buffer or SG processing. */
1061 seg->dto_urb->transfer_flags &=
1062 ~URB_NO_TRANSFER_DMA_MAP;
1063 /* this should always be 0 before a resubmit. */
1064 seg->dto_urb->num_mapped_sgs = 0;
1065
1066 if (xfer->urb->transfer_buffer) {
1067 seg->dto_urb->transfer_buffer =
1068 xfer->urb->transfer_buffer +
1069 buf_itr_offset;
1070 seg->dto_urb->sg = NULL;
1071 seg->dto_urb->num_sgs = 0;
1072 } else {
1073 seg->dto_urb->transfer_buffer = NULL;
1074
1075 /*
1076 * allocate an SG list to store seg_size bytes
1077 * and copy the subset of the xfer->urb->sg that
1078 * matches the buffer subset we are about to
1079 * read.
1080 */
1081 seg->dto_urb->sg = wa_xfer_create_subset_sg(
1082 xfer->urb->sg,
1083 buf_itr_offset, buf_itr_size,
1084 &(seg->dto_urb->num_sgs));
1085 if (!(seg->dto_urb->sg))
1086 result = -ENOMEM;
1087 }
1088 }
1089 seg->dto_urb->transfer_buffer_length = buf_itr_size;
1090
1091 return result;
1092}
1093
1094/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001095 * Allocate the segs array and initialize each of them
1096 *
1097 * The segments are freed by wa_xfer_destroy() when the xfer use count
1098 * drops to zero; however, because each segment is given the same life
1099 * cycle as the USB URB it contains, it is actually freed by
1100 * usb_put_urb() on the contained USB URB (twisted, eh?).
1101 */
1102static int __wa_xfer_setup_segs(struct wa_xfer *xfer, size_t xfer_hdr_size)
1103{
Thomas Pugliese21012422013-10-23 14:44:27 -05001104 int result, cnt, iso_frame_offset;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001105 size_t alloc_size = sizeof(*xfer->seg[0])
1106 - sizeof(xfer->seg[0]->xfer_hdr) + xfer_hdr_size;
1107 struct usb_device *usb_dev = xfer->wa->usb_dev;
1108 const struct usb_endpoint_descriptor *dto_epd = xfer->wa->dto_epd;
1109 struct wa_seg *seg;
Thomas Pugliese21012422013-10-23 14:44:27 -05001110 size_t buf_itr, buf_size, buf_itr_size;
1111 int xfer_isoc_frame_offset = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001112
1113 result = -ENOMEM;
David Vrabel92c4d9b2008-10-15 14:50:10 +01001114 xfer->seg = kcalloc(xfer->segs, sizeof(xfer->seg[0]), GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001115 if (xfer->seg == NULL)
1116 goto error_segs_kzalloc;
1117 buf_itr = 0;
1118 buf_size = xfer->urb->transfer_buffer_length;
Thomas Pugliese21012422013-10-23 14:44:27 -05001119 iso_frame_offset = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001120 for (cnt = 0; cnt < xfer->segs; cnt++) {
Thomas Pugliese21012422013-10-23 14:44:27 -05001121 size_t iso_pkt_descr_size = 0;
1122 int seg_isoc_frame_count = 0, seg_isoc_size = 0;
1123
1124 if (usb_pipeisoc(xfer->urb->pipe)) {
1125 seg_isoc_frame_count =
1126 __wa_seg_calculate_isoc_frame_count(xfer,
1127 xfer_isoc_frame_offset, &seg_isoc_size);
1128
1129 iso_pkt_descr_size =
1130 sizeof(struct wa_xfer_packet_info_hwaiso) +
1131 (seg_isoc_frame_count * sizeof(__le16));
1132 }
1133 seg = xfer->seg[cnt] = kmalloc(alloc_size + iso_pkt_descr_size,
1134 GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001135 if (seg == NULL)
Thomas Pugliese66591015d2013-08-15 14:37:43 -05001136 goto error_seg_kmalloc;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001137 wa_seg_init(seg);
1138 seg->xfer = xfer;
1139 seg->index = cnt;
Thomas Pugliese21012422013-10-23 14:44:27 -05001140 seg->isoc_frame_count = seg_isoc_frame_count;
1141 seg->isoc_frame_offset = xfer_isoc_frame_offset;
1142 seg->isoc_size = seg_isoc_size;
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001143 usb_fill_bulk_urb(&seg->tr_urb, usb_dev,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001144 usb_sndbulkpipe(usb_dev,
1145 dto_epd->bEndpointAddress),
1146 &seg->xfer_hdr, xfer_hdr_size,
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001147 wa_seg_tr_cb, seg);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001148 buf_itr_size = min(buf_size, xfer->seg_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001149 if (xfer->is_inbound == 0 && buf_size > 0) {
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001150 /* outbound data. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001151 seg->dto_urb = usb_alloc_urb(0, GFP_ATOMIC);
1152 if (seg->dto_urb == NULL)
1153 goto error_dto_alloc;
1154 usb_fill_bulk_urb(
1155 seg->dto_urb, usb_dev,
1156 usb_sndbulkpipe(usb_dev,
1157 dto_epd->bEndpointAddress),
1158 NULL, 0, wa_seg_dto_cb, seg);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001159
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001160 if (usb_pipeisoc(xfer->urb->pipe)) {
1161 /* iso packet descriptor. */
1162 seg->isoc_pack_desc_urb =
1163 usb_alloc_urb(0, GFP_ATOMIC);
1164 if (seg->isoc_pack_desc_urb == NULL)
1165 goto error_iso_pack_desc_alloc;
1166 /*
1167 * The buffer for the isoc packet descriptor
1168 * after the transfer request header in the
1169 * segment object memory buffer.
1170 */
1171 usb_fill_bulk_urb(
1172 seg->isoc_pack_desc_urb, usb_dev,
1173 usb_sndbulkpipe(usb_dev,
1174 dto_epd->bEndpointAddress),
1175 (void *)(&seg->xfer_hdr) +
1176 xfer_hdr_size,
1177 iso_pkt_descr_size,
1178 wa_seg_iso_pack_desc_cb, seg);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001179
Thomas Pugliese21012422013-10-23 14:44:27 -05001180 /*
1181 * Fill in the xfer buffer information for the
1182 * first isoc frame. Subsequent frames in this
1183 * segment will be filled in and sent from the
1184 * DTO completion routine, if needed.
1185 */
1186 __wa_populate_dto_urb_isoc(xfer, seg,
1187 xfer_isoc_frame_offset);
1188 /* adjust starting frame offset for next seg. */
1189 xfer_isoc_frame_offset += seg_isoc_frame_count;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001190 } else {
1191 /* fill in the xfer buffer information. */
1192 result = __wa_populate_dto_urb(xfer, seg,
1193 buf_itr, buf_itr_size);
1194 if (result < 0)
1195 goto error_seg_outbound_populate;
1196
1197 buf_itr += buf_itr_size;
1198 buf_size -= buf_itr_size;
1199 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001200 }
1201 seg->status = WA_SEG_READY;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001202 }
1203 return 0;
1204
Thomas Puglieseffd6d172013-09-26 14:08:14 -05001205 /*
1206 * Free the memory for the current segment which failed to init.
1207 * Use the fact that cnt is left at were it failed. The remaining
1208 * segments will be cleaned up by wa_xfer_destroy.
1209 */
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001210error_iso_pack_desc_alloc:
Thomas Puglieseffd6d172013-09-26 14:08:14 -05001211error_seg_outbound_populate:
Thomas Pugliese11b1bf82013-08-15 14:37:41 -05001212 usb_free_urb(xfer->seg[cnt]->dto_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001213error_dto_alloc:
1214 kfree(xfer->seg[cnt]);
Thomas Puglieseffd6d172013-09-26 14:08:14 -05001215 xfer->seg[cnt] = NULL;
Thomas Pugliese66591015d2013-08-15 14:37:43 -05001216error_seg_kmalloc:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001217error_segs_kzalloc:
1218 return result;
1219}
1220
1221/*
1222 * Allocates all the stuff needed to submit a transfer
1223 *
1224 * Breaks the whole data buffer in a list of segments, each one has a
1225 * structure allocated to it and linked in xfer->seg[index]
1226 *
1227 * FIXME: merge setup_segs() and the last part of this function, no
1228 * need to do two for loops when we could run everything in a
1229 * single one
1230 */
1231static int __wa_xfer_setup(struct wa_xfer *xfer, struct urb *urb)
1232{
1233 int result;
1234 struct device *dev = &xfer->wa->usb_iface->dev;
1235 enum wa_xfer_type xfer_type = 0; /* shut up GCC */
1236 size_t xfer_hdr_size, cnt, transfer_size;
1237 struct wa_xfer_hdr *xfer_hdr0, *xfer_hdr;
1238
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001239 result = __wa_xfer_setup_sizes(xfer, &xfer_type);
1240 if (result < 0)
1241 goto error_setup_sizes;
1242 xfer_hdr_size = result;
1243 result = __wa_xfer_setup_segs(xfer, xfer_hdr_size);
1244 if (result < 0) {
1245 dev_err(dev, "xfer %p: Failed to allocate %d segments: %d\n",
1246 xfer, xfer->segs, result);
1247 goto error_setup_segs;
1248 }
1249 /* Fill the first header */
1250 xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
1251 wa_xfer_id_init(xfer);
1252 __wa_xfer_setup_hdr0(xfer, xfer_hdr0, xfer_type, xfer_hdr_size);
1253
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001254 /* Fill remaining headers */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001255 xfer_hdr = xfer_hdr0;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001256 if (xfer_type == WA_XFER_TYPE_ISO) {
1257 xfer_hdr0->dwTransferLength =
Thomas Pugliese21012422013-10-23 14:44:27 -05001258 cpu_to_le32(xfer->seg[0]->isoc_size);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001259 for (cnt = 1; cnt < xfer->segs; cnt++) {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001260 struct wa_xfer_packet_info_hwaiso *packet_desc;
Thomas Pugliese21012422013-10-23 14:44:27 -05001261 struct wa_seg *seg = xfer->seg[cnt];
Thomas Pugliese756a2ee2013-12-09 14:15:15 -06001262 struct wa_xfer_hwaiso *xfer_iso;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001263
Thomas Pugliese21012422013-10-23 14:44:27 -05001264 xfer_hdr = &seg->xfer_hdr;
Thomas Pugliese756a2ee2013-12-09 14:15:15 -06001265 xfer_iso = container_of(xfer_hdr,
1266 struct wa_xfer_hwaiso, hdr);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001267 packet_desc = ((void *)xfer_hdr) + xfer_hdr_size;
1268 /*
Thomas Pugliese21012422013-10-23 14:44:27 -05001269 * Copy values from the 0th header. Segment specific
1270 * values are set below.
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001271 */
Thomas Pugliese21012422013-10-23 14:44:27 -05001272 memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001273 xfer_hdr->bTransferSegment = cnt;
1274 xfer_hdr->dwTransferLength =
Thomas Pugliese21012422013-10-23 14:44:27 -05001275 cpu_to_le32(seg->isoc_size);
Thomas Pugliese756a2ee2013-12-09 14:15:15 -06001276 xfer_iso->dwNumOfPackets =
1277 cpu_to_le32(seg->isoc_frame_count);
Thomas Pugliese21012422013-10-23 14:44:27 -05001278 __wa_setup_isoc_packet_descr(packet_desc, xfer, seg);
1279 seg->status = WA_SEG_READY;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001280 }
1281 } else {
1282 transfer_size = urb->transfer_buffer_length;
1283 xfer_hdr0->dwTransferLength = transfer_size > xfer->seg_size ?
1284 cpu_to_le32(xfer->seg_size) :
1285 cpu_to_le32(transfer_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001286 transfer_size -= xfer->seg_size;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001287 for (cnt = 1; cnt < xfer->segs; cnt++) {
1288 xfer_hdr = &xfer->seg[cnt]->xfer_hdr;
1289 memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
1290 xfer_hdr->bTransferSegment = cnt;
1291 xfer_hdr->dwTransferLength =
1292 transfer_size > xfer->seg_size ?
1293 cpu_to_le32(xfer->seg_size)
1294 : cpu_to_le32(transfer_size);
1295 xfer->seg[cnt]->status = WA_SEG_READY;
1296 transfer_size -= xfer->seg_size;
1297 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001298 }
1299 xfer_hdr->bTransferSegment |= 0x80; /* this is the last segment */
1300 result = 0;
1301error_setup_segs:
1302error_setup_sizes:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001303 return result;
1304}
1305
1306/*
1307 *
1308 *
1309 * rpipe->seg_lock is held!
1310 */
1311static int __wa_seg_submit(struct wa_rpipe *rpipe, struct wa_xfer *xfer,
Thomas Pugliese679ee472013-10-07 10:53:57 -05001312 struct wa_seg *seg, int *dto_done)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001313{
1314 int result;
Thomas Pugliese679ee472013-10-07 10:53:57 -05001315
1316 /* default to done unless we encounter a multi-frame isoc segment. */
1317 *dto_done = 1;
1318
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001319 /* submit the transfer request. */
1320 result = usb_submit_urb(&seg->tr_urb, GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001321 if (result < 0) {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001322 pr_err("%s: xfer %p#%u: REQ submit failed: %d\n",
1323 __func__, xfer, seg->index, result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001324 goto error_seg_submit;
1325 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001326 /* submit the isoc packet descriptor if present. */
1327 if (seg->isoc_pack_desc_urb) {
Thomas Pugliesef07ddb92013-10-23 14:44:28 -05001328 struct wahc *wa = xfer->wa;
1329
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001330 result = usb_submit_urb(seg->isoc_pack_desc_urb, GFP_ATOMIC);
Thomas Puglieseea1af422013-12-09 14:15:14 -06001331 seg->isoc_frame_index = 0;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001332 if (result < 0) {
1333 pr_err("%s: xfer %p#%u: ISO packet descriptor submit failed: %d\n",
1334 __func__, xfer, seg->index, result);
1335 goto error_iso_pack_desc_submit;
1336 }
Thomas Pugliese21012422013-10-23 14:44:27 -05001337 /*
1338 * If this segment contains more than one isoc frame, hold
1339 * onto the dto resource until we send all frames.
Thomas Pugliesef07ddb92013-10-23 14:44:28 -05001340 * Only applies to non-Alereon devices.
Thomas Pugliese21012422013-10-23 14:44:27 -05001341 */
Thomas Pugliesef07ddb92013-10-23 14:44:28 -05001342 if (((wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC) == 0)
1343 && (seg->isoc_frame_count > 1))
Thomas Pugliese21012422013-10-23 14:44:27 -05001344 *dto_done = 0;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001345 }
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001346 /* submit the out data if this is an out request. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001347 if (seg->dto_urb) {
1348 result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
1349 if (result < 0) {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001350 pr_err("%s: xfer %p#%u: DTO submit failed: %d\n",
1351 __func__, xfer, seg->index, result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001352 goto error_dto_submit;
1353 }
1354 }
1355 seg->status = WA_SEG_SUBMITTED;
1356 rpipe_avail_dec(rpipe);
1357 return 0;
1358
1359error_dto_submit:
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001360 usb_unlink_urb(seg->isoc_pack_desc_urb);
1361error_iso_pack_desc_submit:
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001362 usb_unlink_urb(&seg->tr_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001363error_seg_submit:
1364 seg->status = WA_SEG_ERROR;
1365 seg->result = result;
Thomas Pugliese21012422013-10-23 14:44:27 -05001366 *dto_done = 1;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001367 return result;
1368}
1369
1370/*
Thomas Pugliese679ee472013-10-07 10:53:57 -05001371 * Execute more queued request segments until the maximum concurrent allowed.
1372 * Return true if the DTO resource was acquired and released.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001373 *
1374 * The ugly unlock/lock sequence on the error path is needed as the
1375 * xfer->lock normally nests the seg_lock and not viceversa.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001376 */
Thomas Pugliese679ee472013-10-07 10:53:57 -05001377static int __wa_xfer_delayed_run(struct wa_rpipe *rpipe, int *dto_waiting)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001378{
Thomas Pugliese679ee472013-10-07 10:53:57 -05001379 int result, dto_acquired = 0, dto_done = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001380 struct device *dev = &rpipe->wa->usb_iface->dev;
1381 struct wa_seg *seg;
1382 struct wa_xfer *xfer;
1383 unsigned long flags;
1384
Thomas Pugliese679ee472013-10-07 10:53:57 -05001385 *dto_waiting = 0;
1386
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001387 spin_lock_irqsave(&rpipe->seg_lock, flags);
1388 while (atomic_read(&rpipe->segs_available) > 0
Thomas Pugliese679ee472013-10-07 10:53:57 -05001389 && !list_empty(&rpipe->seg_list)
1390 && (dto_acquired = __wa_dto_try_get(rpipe->wa))) {
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001391 seg = list_first_entry(&(rpipe->seg_list), struct wa_seg,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001392 list_node);
1393 list_del(&seg->list_node);
1394 xfer = seg->xfer;
Thomas Pugliese679ee472013-10-07 10:53:57 -05001395 result = __wa_seg_submit(rpipe, xfer, seg, &dto_done);
1396 /* release the dto resource if this RPIPE is done with it. */
1397 if (dto_done)
1398 __wa_dto_put(rpipe->wa);
Thomas Puglieseb9c84be2013-09-27 15:33:36 -05001399 dev_dbg(dev, "xfer %p ID %08X#%u submitted from delayed [%d segments available] %d\n",
1400 xfer, wa_xfer_id(xfer), seg->index,
1401 atomic_read(&rpipe->segs_available), result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001402 if (unlikely(result < 0)) {
1403 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
1404 spin_lock_irqsave(&xfer->lock, flags);
1405 __wa_xfer_abort(xfer);
1406 xfer->segs_done++;
1407 spin_unlock_irqrestore(&xfer->lock, flags);
1408 spin_lock_irqsave(&rpipe->seg_lock, flags);
1409 }
1410 }
Thomas Pugliese679ee472013-10-07 10:53:57 -05001411 /*
1412 * Mark this RPIPE as waiting if dto was not acquired, there are
1413 * delayed segs and no active transfers to wake us up later.
1414 */
1415 if (!dto_acquired && !list_empty(&rpipe->seg_list)
1416 && (atomic_read(&rpipe->segs_available) ==
1417 le16_to_cpu(rpipe->descr.wRequests)))
1418 *dto_waiting = 1;
1419
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001420 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
Thomas Pugliese679ee472013-10-07 10:53:57 -05001421
1422 return dto_done;
1423}
1424
1425static void wa_xfer_delayed_run(struct wa_rpipe *rpipe)
1426{
1427 int dto_waiting;
1428 int dto_done = __wa_xfer_delayed_run(rpipe, &dto_waiting);
1429
1430 /*
1431 * If this RPIPE is waiting on the DTO resource, add it to the tail of
1432 * the waiting list.
1433 * Otherwise, if the WA DTO resource was acquired and released by
1434 * __wa_xfer_delayed_run, another RPIPE may have attempted to acquire
1435 * DTO and failed during that time. Check the delayed list and process
1436 * any waiters. Start searching from the next RPIPE index.
1437 */
1438 if (dto_waiting)
1439 wa_add_delayed_rpipe(rpipe->wa, rpipe);
1440 else if (dto_done)
1441 wa_check_for_delayed_rpipes(rpipe->wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001442}
1443
1444/*
1445 *
1446 * xfer->lock is taken
1447 *
1448 * On failure submitting we just stop submitting and return error;
1449 * wa_urb_enqueue_b() will execute the completion path
1450 */
1451static int __wa_xfer_submit(struct wa_xfer *xfer)
1452{
Thomas Pugliese679ee472013-10-07 10:53:57 -05001453 int result, dto_acquired = 0, dto_done = 0, dto_waiting = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001454 struct wahc *wa = xfer->wa;
1455 struct device *dev = &wa->usb_iface->dev;
1456 unsigned cnt;
1457 struct wa_seg *seg;
1458 unsigned long flags;
1459 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
1460 size_t maxrequests = le16_to_cpu(rpipe->descr.wRequests);
1461 u8 available;
1462 u8 empty;
1463
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001464 spin_lock_irqsave(&wa->xfer_list_lock, flags);
1465 list_add_tail(&xfer->list_node, &wa->xfer_list);
1466 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
1467
1468 BUG_ON(atomic_read(&rpipe->segs_available) > maxrequests);
1469 result = 0;
1470 spin_lock_irqsave(&rpipe->seg_lock, flags);
1471 for (cnt = 0; cnt < xfer->segs; cnt++) {
Thomas Pugliese679ee472013-10-07 10:53:57 -05001472 int delay_seg = 1;
1473
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001474 available = atomic_read(&rpipe->segs_available);
1475 empty = list_empty(&rpipe->seg_list);
1476 seg = xfer->seg[cnt];
Thomas Pugliese679ee472013-10-07 10:53:57 -05001477 if (available && empty) {
1478 /*
1479 * Only attempt to acquire DTO if we have a segment
1480 * to send.
1481 */
1482 dto_acquired = __wa_dto_try_get(rpipe->wa);
1483 if (dto_acquired) {
1484 delay_seg = 0;
1485 result = __wa_seg_submit(rpipe, xfer, seg,
1486 &dto_done);
Thomas Pugliese21012422013-10-23 14:44:27 -05001487 dev_dbg(dev, "xfer %p ID 0x%08X#%u: available %u empty %u submitted\n",
1488 xfer, wa_xfer_id(xfer), cnt, available,
1489 empty);
Thomas Pugliese679ee472013-10-07 10:53:57 -05001490 if (dto_done)
1491 __wa_dto_put(rpipe->wa);
1492
1493 if (result < 0) {
1494 __wa_xfer_abort(xfer);
1495 goto error_seg_submit;
1496 }
1497 }
1498 }
1499
1500 if (delay_seg) {
Thomas Pugliese21012422013-10-23 14:44:27 -05001501 dev_dbg(dev, "xfer %p ID 0x%08X#%u: available %u empty %u delayed\n",
1502 xfer, wa_xfer_id(xfer), cnt, available, empty);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001503 seg->status = WA_SEG_DELAYED;
1504 list_add_tail(&seg->list_node, &rpipe->seg_list);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001505 }
1506 xfer->segs_submitted++;
1507 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001508error_seg_submit:
Thomas Pugliese679ee472013-10-07 10:53:57 -05001509 /*
1510 * Mark this RPIPE as waiting if dto was not acquired, there are
1511 * delayed segs and no active transfers to wake us up later.
1512 */
1513 if (!dto_acquired && !list_empty(&rpipe->seg_list)
1514 && (atomic_read(&rpipe->segs_available) ==
1515 le16_to_cpu(rpipe->descr.wRequests)))
1516 dto_waiting = 1;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001517 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
Thomas Pugliese679ee472013-10-07 10:53:57 -05001518
1519 if (dto_waiting)
1520 wa_add_delayed_rpipe(rpipe->wa, rpipe);
1521 else if (dto_done)
1522 wa_check_for_delayed_rpipes(rpipe->wa);
1523
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001524 return result;
1525}
1526
1527/*
1528 * Second part of a URB/transfer enqueuement
1529 *
1530 * Assumes this comes from wa_urb_enqueue() [maybe through
1531 * wa_urb_enqueue_run()]. At this point:
1532 *
1533 * xfer->wa filled and refcounted
1534 * xfer->ep filled with rpipe refcounted if
1535 * delayed == 0
1536 * xfer->urb filled and refcounted (this is the case when called
1537 * from wa_urb_enqueue() as we come from usb_submit_urb()
1538 * and when called by wa_urb_enqueue_run(), as we took an
1539 * extra ref dropped by _run() after we return).
1540 * xfer->gfp filled
1541 *
1542 * If we fail at __wa_xfer_submit(), then we just check if we are done
1543 * and if so, we run the completion procedure. However, if we are not
1544 * yet done, we do nothing and wait for the completion handlers from
1545 * the submitted URBs or from the xfer-result path to kick in. If xfer
1546 * result never kicks in, the xfer will timeout from the USB code and
1547 * dequeue() will be called.
1548 */
Thomas Pugliese33186c42013-10-01 10:14:56 -05001549static int wa_urb_enqueue_b(struct wa_xfer *xfer)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001550{
1551 int result;
1552 unsigned long flags;
1553 struct urb *urb = xfer->urb;
1554 struct wahc *wa = xfer->wa;
1555 struct wusbhc *wusbhc = wa->wusb;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001556 struct wusb_dev *wusb_dev;
1557 unsigned done;
1558
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001559 result = rpipe_get_by_ep(wa, xfer->ep, urb, xfer->gfp);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001560 if (result < 0) {
1561 pr_err("%s: error_rpipe_get\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001562 goto error_rpipe_get;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001563 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001564 result = -ENODEV;
1565 /* FIXME: segmentation broken -- kills DWA */
1566 mutex_lock(&wusbhc->mutex); /* get a WUSB dev */
Jiri Slaby49fa0922009-03-11 21:47:40 +01001567 if (urb->dev == NULL) {
1568 mutex_unlock(&wusbhc->mutex);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001569 pr_err("%s: error usb dev gone\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001570 goto error_dev_gone;
Jiri Slaby49fa0922009-03-11 21:47:40 +01001571 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001572 wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev);
1573 if (wusb_dev == NULL) {
1574 mutex_unlock(&wusbhc->mutex);
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001575 dev_err(&(urb->dev->dev), "%s: error wusb dev gone\n",
1576 __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001577 goto error_dev_gone;
1578 }
1579 mutex_unlock(&wusbhc->mutex);
1580
1581 spin_lock_irqsave(&xfer->lock, flags);
1582 xfer->wusb_dev = wusb_dev;
1583 result = urb->status;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001584 if (urb->status != -EINPROGRESS) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001585 dev_err(&(urb->dev->dev), "%s: error_dequeued\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001586 goto error_dequeued;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001587 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001588
1589 result = __wa_xfer_setup(xfer, urb);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001590 if (result < 0) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001591 dev_err(&(urb->dev->dev), "%s: error_xfer_setup\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001592 goto error_xfer_setup;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001593 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001594 result = __wa_xfer_submit(xfer);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001595 if (result < 0) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001596 dev_err(&(urb->dev->dev), "%s: error_xfer_submit\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001597 goto error_xfer_submit;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001598 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001599 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001600 return 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001601
Thomas Pugliese33186c42013-10-01 10:14:56 -05001602 /*
1603 * this is basically wa_xfer_completion() broken up wa_xfer_giveback()
1604 * does a wa_xfer_put() that will call wa_xfer_destroy() and undo
1605 * setup().
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001606 */
1607error_xfer_setup:
1608error_dequeued:
1609 spin_unlock_irqrestore(&xfer->lock, flags);
1610 /* FIXME: segmentation broken, kills DWA */
1611 if (wusb_dev)
1612 wusb_dev_put(wusb_dev);
1613error_dev_gone:
1614 rpipe_put(xfer->ep->hcpriv);
1615error_rpipe_get:
1616 xfer->result = result;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001617 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001618
1619error_xfer_submit:
1620 done = __wa_xfer_is_done(xfer);
1621 xfer->result = result;
1622 spin_unlock_irqrestore(&xfer->lock, flags);
1623 if (done)
1624 wa_xfer_completion(xfer);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001625 /* return success since the completion routine will run. */
1626 return 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001627}
1628
1629/*
1630 * Execute the delayed transfers in the Wire Adapter @wa
1631 *
1632 * We need to be careful here, as dequeue() could be called in the
1633 * middle. That's why we do the whole thing under the
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001634 * wa->xfer_list_lock. If dequeue() jumps in, it first locks xfer->lock
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001635 * and then checks the list -- so as we would be acquiring in inverse
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001636 * order, we move the delayed list to a separate list while locked and then
1637 * submit them without the list lock held.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001638 */
1639void wa_urb_enqueue_run(struct work_struct *ws)
1640{
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001641 struct wahc *wa = container_of(ws, struct wahc, xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001642 struct wa_xfer *xfer, *next;
1643 struct urb *urb;
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001644 LIST_HEAD(tmp_list);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001645
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001646 /* Create a copy of the wa->xfer_delayed_list while holding the lock */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001647 spin_lock_irq(&wa->xfer_list_lock);
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001648 list_cut_position(&tmp_list, &wa->xfer_delayed_list,
1649 wa->xfer_delayed_list.prev);
1650 spin_unlock_irq(&wa->xfer_list_lock);
1651
1652 /*
1653 * enqueue from temp list without list lock held since wa_urb_enqueue_b
1654 * can take xfer->lock as well as lock mutexes.
1655 */
1656 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001657 list_del_init(&xfer->list_node);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001658
1659 urb = xfer->urb;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001660 if (wa_urb_enqueue_b(xfer) < 0)
1661 wa_xfer_giveback(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001662 usb_put_urb(urb); /* taken when queuing */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001663 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001664}
1665EXPORT_SYMBOL_GPL(wa_urb_enqueue_run);
1666
1667/*
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001668 * Process the errored transfers on the Wire Adapter outside of interrupt.
1669 */
1670void wa_process_errored_transfers_run(struct work_struct *ws)
1671{
1672 struct wahc *wa = container_of(ws, struct wahc, xfer_error_work);
1673 struct wa_xfer *xfer, *next;
1674 LIST_HEAD(tmp_list);
1675
1676 pr_info("%s: Run delayed STALL processing.\n", __func__);
1677
1678 /* Create a copy of the wa->xfer_errored_list while holding the lock */
1679 spin_lock_irq(&wa->xfer_list_lock);
1680 list_cut_position(&tmp_list, &wa->xfer_errored_list,
1681 wa->xfer_errored_list.prev);
1682 spin_unlock_irq(&wa->xfer_list_lock);
1683
1684 /*
1685 * run rpipe_clear_feature_stalled from temp list without list lock
1686 * held.
1687 */
1688 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
1689 struct usb_host_endpoint *ep;
1690 unsigned long flags;
1691 struct wa_rpipe *rpipe;
1692
1693 spin_lock_irqsave(&xfer->lock, flags);
1694 ep = xfer->ep;
1695 rpipe = ep->hcpriv;
1696 spin_unlock_irqrestore(&xfer->lock, flags);
1697
1698 /* clear RPIPE feature stalled without holding a lock. */
1699 rpipe_clear_feature_stalled(wa, ep);
1700
1701 /* complete the xfer. This removes it from the tmp list. */
1702 wa_xfer_completion(xfer);
1703
1704 /* check for work. */
1705 wa_xfer_delayed_run(rpipe);
1706 }
1707}
1708EXPORT_SYMBOL_GPL(wa_process_errored_transfers_run);
1709
1710/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001711 * Submit a transfer to the Wire Adapter in a delayed way
1712 *
1713 * The process of enqueuing involves possible sleeps() [see
1714 * enqueue_b(), for the rpipe_get() and the mutex_lock()]. If we are
1715 * in an atomic section, we defer the enqueue_b() call--else we call direct.
1716 *
1717 * @urb: We own a reference to it done by the HCI Linux USB stack that
1718 * will be given up by calling usb_hcd_giveback_urb() or by
1719 * returning error from this function -> ergo we don't have to
1720 * refcount it.
1721 */
1722int wa_urb_enqueue(struct wahc *wa, struct usb_host_endpoint *ep,
1723 struct urb *urb, gfp_t gfp)
1724{
1725 int result;
1726 struct device *dev = &wa->usb_iface->dev;
1727 struct wa_xfer *xfer;
1728 unsigned long my_flags;
1729 unsigned cant_sleep = irqs_disabled() | in_atomic();
1730
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001731 if ((urb->transfer_buffer == NULL)
1732 && (urb->sg == NULL)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001733 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1734 && urb->transfer_buffer_length != 0) {
1735 dev_err(dev, "BUG? urb %p: NULL xfer buffer & NODMA\n", urb);
1736 dump_stack();
1737 }
1738
Thomas Puglieseb3744872013-11-25 16:17:16 -06001739 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1740 result = usb_hcd_link_urb_to_ep(&(wa->wusb->usb_hcd), urb);
1741 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
1742 if (result < 0)
1743 goto error_link_urb;
1744
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001745 result = -ENOMEM;
1746 xfer = kzalloc(sizeof(*xfer), gfp);
1747 if (xfer == NULL)
1748 goto error_kmalloc;
1749
1750 result = -ENOENT;
1751 if (urb->status != -EINPROGRESS) /* cancelled */
1752 goto error_dequeued; /* before starting? */
1753 wa_xfer_init(xfer);
1754 xfer->wa = wa_get(wa);
1755 xfer->urb = urb;
1756 xfer->gfp = gfp;
1757 xfer->ep = ep;
1758 urb->hcpriv = xfer;
David Vrabelbce83692008-12-22 18:22:50 +00001759
1760 dev_dbg(dev, "xfer %p urb %p pipe 0x%02x [%d bytes] %s %s %s\n",
1761 xfer, urb, urb->pipe, urb->transfer_buffer_length,
1762 urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? "dma" : "nodma",
1763 urb->pipe & USB_DIR_IN ? "inbound" : "outbound",
1764 cant_sleep ? "deferred" : "inline");
1765
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001766 if (cant_sleep) {
1767 usb_get_urb(urb);
1768 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1769 list_add_tail(&xfer->list_node, &wa->xfer_delayed_list);
1770 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001771 queue_work(wusbd, &wa->xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001772 } else {
Thomas Pugliese33186c42013-10-01 10:14:56 -05001773 result = wa_urb_enqueue_b(xfer);
1774 if (result < 0) {
1775 /*
1776 * URB submit/enqueue failed. Clean up, return an
1777 * error and do not run the callback. This avoids
1778 * an infinite submit/complete loop.
1779 */
1780 dev_err(dev, "%s: URB enqueue failed: %d\n",
1781 __func__, result);
1782 wa_put(xfer->wa);
1783 wa_xfer_put(xfer);
Thomas Puglieseb3744872013-11-25 16:17:16 -06001784 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1785 usb_hcd_unlink_urb_from_ep(&(wa->wusb->usb_hcd), urb);
1786 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001787 return result;
1788 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001789 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001790 return 0;
1791
1792error_dequeued:
1793 kfree(xfer);
1794error_kmalloc:
Thomas Puglieseb3744872013-11-25 16:17:16 -06001795 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1796 usb_hcd_unlink_urb_from_ep(&(wa->wusb->usb_hcd), urb);
1797 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
1798error_link_urb:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001799 return result;
1800}
1801EXPORT_SYMBOL_GPL(wa_urb_enqueue);
1802
1803/*
1804 * Dequeue a URB and make sure uwb_hcd_giveback_urb() [completion
1805 * handler] is called.
1806 *
1807 * Until a transfer goes successfully through wa_urb_enqueue() it
1808 * needs to be dequeued with completion calling; when stuck in delayed
1809 * or before wa_xfer_setup() is called, we need to do completion.
1810 *
1811 * not setup If there is no hcpriv yet, that means that that enqueue
1812 * still had no time to set the xfer up. Because
1813 * urb->status should be other than -EINPROGRESS,
1814 * enqueue() will catch that and bail out.
1815 *
1816 * If the transfer has gone through setup, we just need to clean it
1817 * up. If it has gone through submit(), we have to abort it [with an
1818 * asynch request] and then make sure we cancel each segment.
1819 *
1820 */
Thomas Puglieseb3744872013-11-25 16:17:16 -06001821int wa_urb_dequeue(struct wahc *wa, struct urb *urb, int status)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001822{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001823 unsigned long flags, flags2;
1824 struct wa_xfer *xfer;
1825 struct wa_seg *seg;
1826 struct wa_rpipe *rpipe;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001827 unsigned cnt, done = 0, xfer_abort_pending;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001828 unsigned rpipe_ready = 0;
Thomas Puglieseb3744872013-11-25 16:17:16 -06001829 int result;
1830
1831 /* check if it is safe to unlink. */
1832 spin_lock_irqsave(&wa->xfer_list_lock, flags);
1833 result = usb_hcd_check_unlink_urb(&(wa->wusb->usb_hcd), urb, status);
1834 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
1835 if (result)
1836 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001837
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001838 xfer = urb->hcpriv;
1839 if (xfer == NULL) {
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001840 /*
1841 * Nothing setup yet enqueue will see urb->status !=
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001842 * -EINPROGRESS (by hcd layer) and bail out with
1843 * error, no need to do completion
1844 */
1845 BUG_ON(urb->status == -EINPROGRESS);
1846 goto out;
1847 }
1848 spin_lock_irqsave(&xfer->lock, flags);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001849 pr_debug("%s: DEQUEUE xfer id 0x%08X\n", __func__, wa_xfer_id(xfer));
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001850 rpipe = xfer->ep->hcpriv;
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001851 if (rpipe == NULL) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001852 pr_debug("%s: xfer %p id 0x%08X has no RPIPE. %s",
1853 __func__, xfer, wa_xfer_id(xfer),
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001854 "Probably already aborted.\n" );
Thomas Pugliesee05a1fd2013-11-25 16:17:18 -06001855 result = -ENOENT;
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001856 goto out_unlock;
1857 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001858 /* Check the delayed list -> if there, release and complete */
1859 spin_lock_irqsave(&wa->xfer_list_lock, flags2);
1860 if (!list_empty(&xfer->list_node) && xfer->seg == NULL)
1861 goto dequeue_delayed;
1862 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1863 if (xfer->seg == NULL) /* still hasn't reached */
1864 goto out_unlock; /* setup(), enqueue_b() completes */
1865 /* Ok, the xfer is in flight already, it's been setup and submitted.*/
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001866 xfer_abort_pending = __wa_xfer_abort(xfer) >= 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001867 for (cnt = 0; cnt < xfer->segs; cnt++) {
1868 seg = xfer->seg[cnt];
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001869 pr_debug("%s: xfer id 0x%08X#%d status = %d\n",
1870 __func__, wa_xfer_id(xfer), cnt, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001871 switch (seg->status) {
1872 case WA_SEG_NOTREADY:
1873 case WA_SEG_READY:
1874 printk(KERN_ERR "xfer %p#%u: dequeue bad state %u\n",
1875 xfer, cnt, seg->status);
1876 WARN_ON(1);
1877 break;
1878 case WA_SEG_DELAYED:
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001879 /*
1880 * delete from rpipe delayed list. If no segments on
1881 * this xfer have been submitted, __wa_xfer_is_done will
1882 * trigger a giveback below. Otherwise, the submitted
1883 * segments will be completed in the DTI interrupt.
1884 */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001885 seg->status = WA_SEG_ABORTED;
Thomas Pugliesee05a1fd2013-11-25 16:17:18 -06001886 seg->result = -ENOENT;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001887 spin_lock_irqsave(&rpipe->seg_lock, flags2);
1888 list_del(&seg->list_node);
1889 xfer->segs_done++;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001890 spin_unlock_irqrestore(&rpipe->seg_lock, flags2);
1891 break;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001892 case WA_SEG_DONE:
1893 case WA_SEG_ERROR:
1894 case WA_SEG_ABORTED:
1895 break;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001896 /*
1897 * In the states below, the HWA device already knows
1898 * about the transfer. If an abort request was sent,
1899 * allow the HWA to process it and wait for the
1900 * results. Otherwise, the DTI state and seg completed
1901 * counts can get out of sync.
1902 */
1903 case WA_SEG_SUBMITTED:
1904 case WA_SEG_PENDING:
1905 case WA_SEG_DTI_PENDING:
1906 /*
1907 * Check if the abort was successfully sent. This could
1908 * be false if the HWA has been removed but we haven't
1909 * gotten the disconnect notification yet.
1910 */
1911 if (!xfer_abort_pending) {
1912 seg->status = WA_SEG_ABORTED;
1913 rpipe_ready = rpipe_avail_inc(rpipe);
1914 xfer->segs_done++;
1915 }
1916 break;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001917 }
1918 }
1919 xfer->result = urb->status; /* -ENOENT or -ECONNRESET */
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001920 done = __wa_xfer_is_done(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001921 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001922 if (done)
1923 wa_xfer_completion(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001924 if (rpipe_ready)
1925 wa_xfer_delayed_run(rpipe);
Thomas Pugliesee05a1fd2013-11-25 16:17:18 -06001926 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001927
1928out_unlock:
1929 spin_unlock_irqrestore(&xfer->lock, flags);
1930out:
Thomas Pugliesee05a1fd2013-11-25 16:17:18 -06001931 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001932
1933dequeue_delayed:
1934 list_del_init(&xfer->list_node);
1935 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1936 xfer->result = urb->status;
1937 spin_unlock_irqrestore(&xfer->lock, flags);
1938 wa_xfer_giveback(xfer);
1939 usb_put_urb(urb); /* we got a ref in enqueue() */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001940 return 0;
1941}
1942EXPORT_SYMBOL_GPL(wa_urb_dequeue);
1943
1944/*
1945 * Translation from WA status codes (WUSB1.0 Table 8.15) to errno
1946 * codes
1947 *
1948 * Positive errno values are internal inconsistencies and should be
1949 * flagged louder. Negative are to be passed up to the user in the
1950 * normal way.
1951 *
1952 * @status: USB WA status code -- high two bits are stripped.
1953 */
1954static int wa_xfer_status_to_errno(u8 status)
1955{
1956 int errno;
1957 u8 real_status = status;
1958 static int xlat[] = {
1959 [WA_XFER_STATUS_SUCCESS] = 0,
1960 [WA_XFER_STATUS_HALTED] = -EPIPE,
1961 [WA_XFER_STATUS_DATA_BUFFER_ERROR] = -ENOBUFS,
1962 [WA_XFER_STATUS_BABBLE] = -EOVERFLOW,
1963 [WA_XFER_RESERVED] = EINVAL,
1964 [WA_XFER_STATUS_NOT_FOUND] = 0,
1965 [WA_XFER_STATUS_INSUFFICIENT_RESOURCE] = -ENOMEM,
1966 [WA_XFER_STATUS_TRANSACTION_ERROR] = -EILSEQ,
Thomas Pugliesee05a1fd2013-11-25 16:17:18 -06001967 [WA_XFER_STATUS_ABORTED] = -ENOENT,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001968 [WA_XFER_STATUS_RPIPE_NOT_READY] = EINVAL,
1969 [WA_XFER_INVALID_FORMAT] = EINVAL,
1970 [WA_XFER_UNEXPECTED_SEGMENT_NUMBER] = EINVAL,
1971 [WA_XFER_STATUS_RPIPE_TYPE_MISMATCH] = EINVAL,
1972 };
1973 status &= 0x3f;
1974
1975 if (status == 0)
1976 return 0;
1977 if (status >= ARRAY_SIZE(xlat)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02001978 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001979 "Unknown WA transfer status 0x%02x\n",
1980 __func__, real_status);
1981 return -EINVAL;
1982 }
1983 errno = xlat[status];
1984 if (unlikely(errno > 0)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02001985 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001986 "Inconsistent WA status: 0x%02x\n",
1987 __func__, real_status);
1988 errno = -errno;
1989 }
1990 return errno;
1991}
1992
1993/*
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001994 * If a last segment flag and/or a transfer result error is encountered,
1995 * no other segment transfer results will be returned from the device.
1996 * Mark the remaining submitted or pending xfers as completed so that
1997 * the xfer will complete cleanly.
1998 */
1999static void wa_complete_remaining_xfer_segs(struct wa_xfer *xfer,
Thomas Pugliese70052342013-12-09 13:10:41 -06002000 struct wa_seg *incoming_seg, enum wa_seg_status status)
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002001{
2002 int index;
2003 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
2004
2005 for (index = incoming_seg->index + 1; index < xfer->segs_submitted;
2006 index++) {
2007 struct wa_seg *current_seg = xfer->seg[index];
2008
2009 BUG_ON(current_seg == NULL);
2010
2011 switch (current_seg->status) {
2012 case WA_SEG_SUBMITTED:
2013 case WA_SEG_PENDING:
2014 case WA_SEG_DTI_PENDING:
2015 rpipe_avail_inc(rpipe);
2016 /*
2017 * do not increment RPIPE avail for the WA_SEG_DELAYED case
2018 * since it has not been submitted to the RPIPE.
2019 */
2020 case WA_SEG_DELAYED:
2021 xfer->segs_done++;
Thomas Pugliese70052342013-12-09 13:10:41 -06002022 current_seg->status = status;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002023 break;
2024 case WA_SEG_ABORTED:
2025 break;
2026 default:
2027 WARN(1, "%s: xfer 0x%08X#%d. bad seg status = %d\n",
2028 __func__, wa_xfer_id(xfer), index,
2029 current_seg->status);
2030 break;
2031 }
2032 }
2033}
2034
Thomas Pugliese70052342013-12-09 13:10:41 -06002035/* Populate the wa->buf_in_urb based on the current transfer state. */
2036static int wa_populate_buf_in_urb(struct wahc *wa, struct wa_xfer *xfer,
2037 unsigned int seg_idx, unsigned int bytes_transferred)
2038{
2039 int result = 0;
2040 struct wa_seg *seg = xfer->seg[seg_idx];
2041
2042 BUG_ON(wa->buf_in_urb->status == -EINPROGRESS);
2043 /* this should always be 0 before a resubmit. */
2044 wa->buf_in_urb->num_mapped_sgs = 0;
2045
2046 if (xfer->is_dma) {
2047 wa->buf_in_urb->transfer_dma = xfer->urb->transfer_dma
2048 + (seg_idx * xfer->seg_size);
2049 wa->buf_in_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2050 wa->buf_in_urb->transfer_buffer = NULL;
2051 wa->buf_in_urb->sg = NULL;
2052 wa->buf_in_urb->num_sgs = 0;
2053 } else {
2054 /* do buffer or SG processing. */
2055 wa->buf_in_urb->transfer_flags &= ~URB_NO_TRANSFER_DMA_MAP;
2056
2057 if (xfer->urb->transfer_buffer) {
2058 wa->buf_in_urb->transfer_buffer =
2059 xfer->urb->transfer_buffer
2060 + (seg_idx * xfer->seg_size);
2061 wa->buf_in_urb->sg = NULL;
2062 wa->buf_in_urb->num_sgs = 0;
2063 } else {
2064 /* allocate an SG list to store seg_size bytes
2065 and copy the subset of the xfer->urb->sg
2066 that matches the buffer subset we are
2067 about to read. */
2068 wa->buf_in_urb->sg = wa_xfer_create_subset_sg(
2069 xfer->urb->sg,
2070 seg_idx * xfer->seg_size,
2071 bytes_transferred,
2072 &(wa->buf_in_urb->num_sgs));
2073
2074 if (!(wa->buf_in_urb->sg)) {
2075 wa->buf_in_urb->num_sgs = 0;
2076 result = -ENOMEM;
2077 }
2078 wa->buf_in_urb->transfer_buffer = NULL;
2079 }
2080 }
2081 wa->buf_in_urb->transfer_buffer_length = bytes_transferred;
2082 wa->buf_in_urb->context = seg;
2083
2084 return result;
2085}
2086
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002087/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002088 * Process a xfer result completion message
2089 *
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002090 * inbound transfers: need to schedule a buf_in_urb read
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002091 *
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002092 * FIXME: this function needs to be broken up in parts
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002093 */
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002094static void wa_xfer_result_chew(struct wahc *wa, struct wa_xfer *xfer,
2095 struct wa_xfer_result *xfer_result)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002096{
2097 int result;
2098 struct device *dev = &wa->usb_iface->dev;
2099 unsigned long flags;
Thomas Pugliese70052342013-12-09 13:10:41 -06002100 unsigned int seg_idx;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002101 struct wa_seg *seg;
2102 struct wa_rpipe *rpipe;
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002103 unsigned done = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002104 u8 usb_status;
2105 unsigned rpipe_ready = 0;
Thomas Pugliese70052342013-12-09 13:10:41 -06002106 unsigned bytes_transferred = le32_to_cpu(xfer_result->dwTransferLength);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002107
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002108 spin_lock_irqsave(&xfer->lock, flags);
2109 seg_idx = xfer_result->bTransferSegment & 0x7f;
2110 if (unlikely(seg_idx >= xfer->segs))
2111 goto error_bad_seg;
2112 seg = xfer->seg[seg_idx];
2113 rpipe = xfer->ep->hcpriv;
2114 usb_status = xfer_result->bTransferStatus;
Thomas Puglieseb9c84be2013-09-27 15:33:36 -05002115 dev_dbg(dev, "xfer %p ID 0x%08X#%u: bTransferStatus 0x%02x (seg status %u)\n",
2116 xfer, wa_xfer_id(xfer), seg_idx, usb_status, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002117 if (seg->status == WA_SEG_ABORTED
2118 || seg->status == WA_SEG_ERROR) /* already handled */
2119 goto segment_aborted;
2120 if (seg->status == WA_SEG_SUBMITTED) /* ops, got here */
2121 seg->status = WA_SEG_PENDING; /* before wa_seg{_dto}_cb() */
2122 if (seg->status != WA_SEG_PENDING) {
2123 if (printk_ratelimit())
2124 dev_err(dev, "xfer %p#%u: Bad segment state %u\n",
2125 xfer, seg_idx, seg->status);
2126 seg->status = WA_SEG_PENDING; /* workaround/"fix" it */
2127 }
2128 if (usb_status & 0x80) {
2129 seg->result = wa_xfer_status_to_errno(usb_status);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002130 dev_err(dev, "DTI: xfer %p#:%08X:%u failed (0x%02x)\n",
2131 xfer, xfer->id, seg->index, usb_status);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002132 seg->status = ((usb_status & 0x7F) == WA_XFER_STATUS_ABORTED) ?
2133 WA_SEG_ABORTED : WA_SEG_ERROR;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002134 goto error_complete;
2135 }
2136 /* FIXME: we ignore warnings, tally them for stats */
2137 if (usb_status & 0x40) /* Warning?... */
2138 usb_status = 0; /* ... pass */
Thomas Pugliese70052342013-12-09 13:10:41 -06002139 /*
2140 * If the last segment bit is set, complete the remaining segments.
2141 * When the current segment is completed, either in wa_buf_in_cb for
2142 * transfers with data or below for no data, the xfer will complete.
2143 */
2144 if (xfer_result->bTransferSegment & 0x80)
2145 wa_complete_remaining_xfer_segs(xfer, seg, WA_SEG_DONE);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002146 if (usb_pipeisoc(xfer->urb->pipe)) {
2147 /* set up WA state to read the isoc packet status next. */
2148 wa->dti_isoc_xfer_in_progress = wa_xfer_id(xfer);
2149 wa->dti_isoc_xfer_seg = seg_idx;
2150 wa->dti_state = WA_DTI_ISOC_PACKET_STATUS_PENDING;
Thomas Pugliese70052342013-12-09 13:10:41 -06002151 } else if ((xfer->is_inbound)
2152 && (bytes_transferred > 0)) {
2153 /* IN data phase: read to buffer */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002154 seg->status = WA_SEG_DTI_PENDING;
Thomas Pugliese70052342013-12-09 13:10:41 -06002155 result = wa_populate_buf_in_urb(wa, xfer, seg_idx,
2156 bytes_transferred);
2157 if (result < 0)
2158 goto error_buf_in_populate;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002159 result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
2160 if (result < 0)
2161 goto error_submit_buf_in;
2162 } else {
Thomas Pugliese70052342013-12-09 13:10:41 -06002163 /* OUT data phase or no data, complete it -- */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002164 seg->status = WA_SEG_DONE;
Thomas Pugliese70052342013-12-09 13:10:41 -06002165 seg->result = bytes_transferred;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002166 xfer->segs_done++;
2167 rpipe_ready = rpipe_avail_inc(rpipe);
2168 done = __wa_xfer_is_done(xfer);
2169 }
2170 spin_unlock_irqrestore(&xfer->lock, flags);
2171 if (done)
2172 wa_xfer_completion(xfer);
2173 if (rpipe_ready)
2174 wa_xfer_delayed_run(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002175 return;
2176
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002177error_submit_buf_in:
2178 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
2179 dev_err(dev, "DTI: URB max acceptable errors "
2180 "exceeded, resetting device\n");
2181 wa_reset_all(wa);
2182 }
2183 if (printk_ratelimit())
2184 dev_err(dev, "xfer %p#%u: can't submit DTI data phase: %d\n",
2185 xfer, seg_idx, result);
2186 seg->result = result;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002187 kfree(wa->buf_in_urb->sg);
Thomas Pugliese67414482013-09-26 14:08:16 -05002188 wa->buf_in_urb->sg = NULL;
Thomas Pugliese70052342013-12-09 13:10:41 -06002189error_buf_in_populate:
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002190 __wa_xfer_abort(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002191 seg->status = WA_SEG_ERROR;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002192error_complete:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002193 xfer->segs_done++;
2194 rpipe_ready = rpipe_avail_inc(rpipe);
Thomas Pugliese70052342013-12-09 13:10:41 -06002195 wa_complete_remaining_xfer_segs(xfer, seg, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002196 done = __wa_xfer_is_done(xfer);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002197 /*
2198 * queue work item to clear STALL for control endpoints.
2199 * Otherwise, let endpoint_reset take care of it.
2200 */
2201 if (((usb_status & 0x3f) == WA_XFER_STATUS_HALTED) &&
2202 usb_endpoint_xfer_control(&xfer->ep->desc) &&
2203 done) {
2204
2205 dev_info(dev, "Control EP stall. Queue delayed work.\n");
2206 spin_lock_irq(&wa->xfer_list_lock);
Wei Yongjun8eb41292013-09-23 14:16:22 +08002207 /* move xfer from xfer_list to xfer_errored_list. */
2208 list_move_tail(&xfer->list_node, &wa->xfer_errored_list);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002209 spin_unlock_irq(&wa->xfer_list_lock);
2210 spin_unlock_irqrestore(&xfer->lock, flags);
2211 queue_work(wusbd, &wa->xfer_error_work);
2212 } else {
2213 spin_unlock_irqrestore(&xfer->lock, flags);
2214 if (done)
2215 wa_xfer_completion(xfer);
2216 if (rpipe_ready)
2217 wa_xfer_delayed_run(rpipe);
2218 }
2219
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002220 return;
2221
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002222error_bad_seg:
2223 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Puglieseb3744872013-11-25 16:17:16 -06002224 wa_urb_dequeue(wa, xfer->urb, -ENOENT);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002225 if (printk_ratelimit())
2226 dev_err(dev, "xfer %p#%u: bad segment\n", xfer, seg_idx);
2227 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
2228 dev_err(dev, "DTI: URB max acceptable errors "
2229 "exceeded, resetting device\n");
2230 wa_reset_all(wa);
2231 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002232 return;
2233
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002234segment_aborted:
2235 /* nothing to do, as the aborter did the completion */
2236 spin_unlock_irqrestore(&xfer->lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002237}
2238
2239/*
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002240 * Process a isochronous packet status message
2241 *
2242 * inbound transfers: need to schedule a buf_in_urb read
2243 */
2244static void wa_process_iso_packet_status(struct wahc *wa, struct urb *urb)
2245{
2246 struct device *dev = &wa->usb_iface->dev;
2247 struct wa_xfer_packet_status_hwaiso *packet_status;
Thomas Pugliese21012422013-10-23 14:44:27 -05002248 struct wa_xfer_packet_status_len_hwaiso *status_array;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002249 struct wa_xfer *xfer;
2250 unsigned long flags;
2251 struct wa_seg *seg;
2252 struct wa_rpipe *rpipe;
2253 unsigned done = 0;
Thomas Pugliese21012422013-10-23 14:44:27 -05002254 unsigned rpipe_ready = 0, seg_index;
2255 int expected_size;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002256
2257 /* We have a xfer result buffer; check it */
2258 dev_dbg(dev, "DTI: isoc packet status %d bytes at %p\n",
2259 urb->actual_length, urb->transfer_buffer);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002260 packet_status = (struct wa_xfer_packet_status_hwaiso *)(wa->dti_buf);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002261 if (packet_status->bPacketType != WA_XFER_ISO_PACKET_STATUS) {
2262 dev_err(dev, "DTI Error: isoc packet status--bad type 0x%02x\n",
2263 packet_status->bPacketType);
2264 goto error_parse_buffer;
2265 }
2266 xfer = wa_xfer_get_by_id(wa, wa->dti_isoc_xfer_in_progress);
2267 if (xfer == NULL) {
2268 dev_err(dev, "DTI Error: isoc packet status--unknown xfer 0x%08x\n",
2269 wa->dti_isoc_xfer_in_progress);
2270 goto error_parse_buffer;
2271 }
2272 spin_lock_irqsave(&xfer->lock, flags);
2273 if (unlikely(wa->dti_isoc_xfer_seg >= xfer->segs))
2274 goto error_bad_seg;
2275 seg = xfer->seg[wa->dti_isoc_xfer_seg];
2276 rpipe = xfer->ep->hcpriv;
Thomas Pugliese21012422013-10-23 14:44:27 -05002277 expected_size = sizeof(*packet_status) +
2278 (sizeof(packet_status->PacketStatus[0]) *
2279 seg->isoc_frame_count);
2280 if (urb->actual_length != expected_size) {
2281 dev_err(dev, "DTI Error: isoc packet status--bad urb length (%d bytes vs %d needed)\n",
2282 urb->actual_length, expected_size);
2283 goto error_bad_seg;
2284 }
2285 if (le16_to_cpu(packet_status->wLength) != expected_size) {
2286 dev_err(dev, "DTI Error: isoc packet status--bad length %u\n",
2287 le16_to_cpu(packet_status->wLength));
2288 goto error_bad_seg;
2289 }
2290 /* isoc packet status and lengths back xfer urb. */
2291 status_array = packet_status->PacketStatus;
2292 for (seg_index = 0; seg_index < seg->isoc_frame_count; ++seg_index) {
2293 xfer->urb->iso_frame_desc[seg->index].status =
2294 wa_xfer_status_to_errno(
2295 le16_to_cpu(status_array[seg_index].PacketStatus));
2296 xfer->urb->iso_frame_desc[seg->index].actual_length =
2297 le16_to_cpu(status_array[seg_index].PacketLength);
2298 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002299
2300 if (!xfer->is_inbound) {
2301 /* OUT transfer, complete it -- */
2302 seg->status = WA_SEG_DONE;
2303 xfer->segs_done++;
2304 rpipe_ready = rpipe_avail_inc(rpipe);
2305 done = __wa_xfer_is_done(xfer);
2306 }
2307 spin_unlock_irqrestore(&xfer->lock, flags);
2308 wa->dti_state = WA_DTI_TRANSFER_RESULT_PENDING;
2309 if (done)
2310 wa_xfer_completion(xfer);
2311 if (rpipe_ready)
2312 wa_xfer_delayed_run(rpipe);
2313 wa_xfer_put(xfer);
2314 return;
2315
2316error_bad_seg:
2317 spin_unlock_irqrestore(&xfer->lock, flags);
2318 wa_xfer_put(xfer);
2319error_parse_buffer:
2320 return;
2321}
2322
2323/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002324 * Callback for the IN data phase
2325 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002326 * If successful transition state; otherwise, take a note of the
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002327 * error, mark this segment done and try completion.
2328 *
2329 * Note we don't access until we are sure that the transfer hasn't
2330 * been cancelled (ECONNRESET, ENOENT), which could mean that
2331 * seg->xfer could be already gone.
2332 */
2333static void wa_buf_in_cb(struct urb *urb)
2334{
2335 struct wa_seg *seg = urb->context;
2336 struct wa_xfer *xfer = seg->xfer;
2337 struct wahc *wa;
2338 struct device *dev;
2339 struct wa_rpipe *rpipe;
2340 unsigned rpipe_ready;
2341 unsigned long flags;
2342 u8 done = 0;
2343
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002344 /* free the sg if it was used. */
2345 kfree(urb->sg);
2346 urb->sg = NULL;
2347
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002348 switch (urb->status) {
2349 case 0:
2350 spin_lock_irqsave(&xfer->lock, flags);
2351 wa = xfer->wa;
2352 dev = &wa->usb_iface->dev;
2353 rpipe = xfer->ep->hcpriv;
David Vrabelbce83692008-12-22 18:22:50 +00002354 dev_dbg(dev, "xfer %p#%u: data in done (%zu bytes)\n",
2355 xfer, seg->index, (size_t)urb->actual_length);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002356 seg->status = WA_SEG_DONE;
2357 seg->result = urb->actual_length;
2358 xfer->segs_done++;
2359 rpipe_ready = rpipe_avail_inc(rpipe);
2360 done = __wa_xfer_is_done(xfer);
2361 spin_unlock_irqrestore(&xfer->lock, flags);
2362 if (done)
2363 wa_xfer_completion(xfer);
2364 if (rpipe_ready)
2365 wa_xfer_delayed_run(rpipe);
2366 break;
2367 case -ECONNRESET: /* URB unlinked; no need to do anything */
2368 case -ENOENT: /* as it was done by the who unlinked us */
2369 break;
2370 default: /* Other errors ... */
2371 spin_lock_irqsave(&xfer->lock, flags);
2372 wa = xfer->wa;
2373 dev = &wa->usb_iface->dev;
2374 rpipe = xfer->ep->hcpriv;
2375 if (printk_ratelimit())
2376 dev_err(dev, "xfer %p#%u: data in error %d\n",
2377 xfer, seg->index, urb->status);
2378 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
2379 EDC_ERROR_TIMEFRAME)){
2380 dev_err(dev, "DTO: URB max acceptable errors "
2381 "exceeded, resetting device\n");
2382 wa_reset_all(wa);
2383 }
2384 seg->status = WA_SEG_ERROR;
2385 seg->result = urb->status;
2386 xfer->segs_done++;
2387 rpipe_ready = rpipe_avail_inc(rpipe);
2388 __wa_xfer_abort(xfer);
2389 done = __wa_xfer_is_done(xfer);
2390 spin_unlock_irqrestore(&xfer->lock, flags);
2391 if (done)
2392 wa_xfer_completion(xfer);
2393 if (rpipe_ready)
2394 wa_xfer_delayed_run(rpipe);
2395 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002396}
2397
2398/*
2399 * Handle an incoming transfer result buffer
2400 *
2401 * Given a transfer result buffer, it completes the transfer (possibly
2402 * scheduling and buffer in read) and then resubmits the DTI URB for a
2403 * new transfer result read.
2404 *
2405 *
2406 * The xfer_result DTI URB state machine
2407 *
2408 * States: OFF | RXR (Read-Xfer-Result) | RBI (Read-Buffer-In)
2409 *
2410 * We start in OFF mode, the first xfer_result notification [through
2411 * wa_handle_notif_xfer()] moves us to RXR by posting the DTI-URB to
2412 * read.
2413 *
2414 * We receive a buffer -- if it is not a xfer_result, we complain and
2415 * repost the DTI-URB. If it is a xfer_result then do the xfer seg
2416 * request accounting. If it is an IN segment, we move to RBI and post
2417 * a BUF-IN-URB to the right buffer. The BUF-IN-URB callback will
2418 * repost the DTI-URB and move to RXR state. if there was no IN
2419 * segment, it will repost the DTI-URB.
2420 *
2421 * We go back to OFF when we detect a ENOENT or ESHUTDOWN (or too many
2422 * errors) in the URBs.
2423 */
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002424static void wa_dti_cb(struct urb *urb)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002425{
2426 int result;
2427 struct wahc *wa = urb->context;
2428 struct device *dev = &wa->usb_iface->dev;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002429 u32 xfer_id;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002430 u8 usb_status;
2431
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002432 BUG_ON(wa->dti_urb != urb);
2433 switch (wa->dti_urb->status) {
2434 case 0:
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002435 if (wa->dti_state == WA_DTI_TRANSFER_RESULT_PENDING) {
2436 struct wa_xfer_result *xfer_result;
2437 struct wa_xfer *xfer;
2438
2439 /* We have a xfer result buffer; check it */
2440 dev_dbg(dev, "DTI: xfer result %d bytes at %p\n",
2441 urb->actual_length, urb->transfer_buffer);
2442 if (urb->actual_length != sizeof(*xfer_result)) {
2443 dev_err(dev, "DTI Error: xfer result--bad size xfer result (%d bytes vs %zu needed)\n",
2444 urb->actual_length,
2445 sizeof(*xfer_result));
2446 break;
2447 }
2448 xfer_result = (struct wa_xfer_result *)(wa->dti_buf);
2449 if (xfer_result->hdr.bLength != sizeof(*xfer_result)) {
2450 dev_err(dev, "DTI Error: xfer result--bad header length %u\n",
2451 xfer_result->hdr.bLength);
2452 break;
2453 }
2454 if (xfer_result->hdr.bNotifyType != WA_XFER_RESULT) {
2455 dev_err(dev, "DTI Error: xfer result--bad header type 0x%02x\n",
2456 xfer_result->hdr.bNotifyType);
2457 break;
2458 }
2459 usb_status = xfer_result->bTransferStatus & 0x3f;
2460 if (usb_status == WA_XFER_STATUS_NOT_FOUND)
2461 /* taken care of already */
2462 break;
2463 xfer_id = le32_to_cpu(xfer_result->dwTransferID);
2464 xfer = wa_xfer_get_by_id(wa, xfer_id);
2465 if (xfer == NULL) {
2466 /* FIXME: transaction not found. */
2467 dev_err(dev, "DTI Error: xfer result--unknown xfer 0x%08x (status 0x%02x)\n",
2468 xfer_id, usb_status);
2469 break;
2470 }
2471 wa_xfer_result_chew(wa, xfer, xfer_result);
2472 wa_xfer_put(xfer);
2473 } else if (wa->dti_state == WA_DTI_ISOC_PACKET_STATUS_PENDING) {
2474 wa_process_iso_packet_status(wa, urb);
2475 } else {
2476 dev_err(dev, "DTI Error: unexpected EP state = %d\n",
2477 wa->dti_state);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002478 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002479 break;
2480 case -ENOENT: /* (we killed the URB)...so, no broadcast */
2481 case -ESHUTDOWN: /* going away! */
2482 dev_dbg(dev, "DTI: going down! %d\n", urb->status);
2483 goto out;
2484 default:
2485 /* Unknown error */
2486 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS,
2487 EDC_ERROR_TIMEFRAME)) {
2488 dev_err(dev, "DTI: URB max acceptable errors "
2489 "exceeded, resetting device\n");
2490 wa_reset_all(wa);
2491 goto out;
2492 }
2493 if (printk_ratelimit())
2494 dev_err(dev, "DTI: URB error %d\n", urb->status);
2495 break;
2496 }
2497 /* Resubmit the DTI URB */
2498 result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
2499 if (result < 0) {
2500 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
2501 "resetting\n", result);
2502 wa_reset_all(wa);
2503 }
2504out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002505 return;
2506}
2507
2508/*
2509 * Transfer complete notification
2510 *
2511 * Called from the notif.c code. We get a notification on EP2 saying
2512 * that some endpoint has some transfer result data available. We are
2513 * about to read it.
2514 *
2515 * To speed up things, we always have a URB reading the DTI URB; we
2516 * don't really set it up and start it until the first xfer complete
2517 * notification arrives, which is what we do here.
2518 *
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002519 * Follow up in wa_dti_cb(), as that's where the whole state
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002520 * machine starts.
2521 *
2522 * So here we just initialize the DTI URB for reading transfer result
2523 * notifications and also the buffer-in URB, for reading buffers. Then
2524 * we just submit the DTI URB.
2525 *
2526 * @wa shall be referenced
2527 */
2528void wa_handle_notif_xfer(struct wahc *wa, struct wa_notif_hdr *notif_hdr)
2529{
2530 int result;
2531 struct device *dev = &wa->usb_iface->dev;
2532 struct wa_notif_xfer *notif_xfer;
2533 const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd;
2534
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002535 notif_xfer = container_of(notif_hdr, struct wa_notif_xfer, hdr);
2536 BUG_ON(notif_hdr->bNotifyType != WA_NOTIF_TRANSFER);
2537
2538 if ((0x80 | notif_xfer->bEndpoint) != dti_epd->bEndpointAddress) {
2539 /* FIXME: hardcoded limitation, adapt */
2540 dev_err(dev, "BUG: DTI ep is %u, not %u (hack me)\n",
2541 notif_xfer->bEndpoint, dti_epd->bEndpointAddress);
2542 goto error;
2543 }
2544 if (wa->dti_urb != NULL) /* DTI URB already started */
2545 goto out;
2546
2547 wa->dti_urb = usb_alloc_urb(0, GFP_KERNEL);
2548 if (wa->dti_urb == NULL) {
2549 dev_err(dev, "Can't allocate DTI URB\n");
2550 goto error_dti_urb_alloc;
2551 }
2552 usb_fill_bulk_urb(
2553 wa->dti_urb, wa->usb_dev,
2554 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002555 wa->dti_buf, wa->dti_buf_size,
2556 wa_dti_cb, wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002557
2558 wa->buf_in_urb = usb_alloc_urb(0, GFP_KERNEL);
2559 if (wa->buf_in_urb == NULL) {
2560 dev_err(dev, "Can't allocate BUF-IN URB\n");
2561 goto error_buf_in_urb_alloc;
2562 }
2563 usb_fill_bulk_urb(
2564 wa->buf_in_urb, wa->usb_dev,
2565 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
2566 NULL, 0, wa_buf_in_cb, wa);
2567 result = usb_submit_urb(wa->dti_urb, GFP_KERNEL);
2568 if (result < 0) {
2569 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
2570 "resetting\n", result);
2571 goto error_dti_urb_submit;
2572 }
2573out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002574 return;
2575
2576error_dti_urb_submit:
2577 usb_put_urb(wa->buf_in_urb);
Thomas Pugliese67414482013-09-26 14:08:16 -05002578 wa->buf_in_urb = NULL;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002579error_buf_in_urb_alloc:
2580 usb_put_urb(wa->dti_urb);
2581 wa->dti_urb = NULL;
2582error_dti_urb_alloc:
2583error:
2584 wa_reset_all(wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002585}