blob: cb061915f051485a3427edd8c22c599d3c5ef968 [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 */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010082#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090083#include <linux/slab.h>
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010084#include <linux/hash.h>
Manuel Zerpies9708cd22011-06-16 14:15:16 +020085#include <linux/ratelimit.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040086#include <linux/export.h>
Thomas Pugliese2b81c082013-06-11 10:39:31 -050087#include <linux/scatterlist.h>
David Vrabelbce83692008-12-22 18:22:50 +000088
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010089#include "wa-hc.h"
90#include "wusbhc.h"
91
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010092enum {
Thomas Pugliesef74b75e2013-10-23 14:44:29 -050093 /* [WUSB] section 8.3.3 allocates 7 bits for the segment index. */
94 WA_SEGS_MAX = 128,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +010095};
96
97enum wa_seg_status {
98 WA_SEG_NOTREADY,
99 WA_SEG_READY,
100 WA_SEG_DELAYED,
101 WA_SEG_SUBMITTED,
102 WA_SEG_PENDING,
103 WA_SEG_DTI_PENDING,
104 WA_SEG_DONE,
105 WA_SEG_ERROR,
106 WA_SEG_ABORTED,
107};
108
109static void wa_xfer_delayed_run(struct wa_rpipe *);
Thomas Pugliese679ee472013-10-07 10:53:57 -0500110static int __wa_xfer_delayed_run(struct wa_rpipe *rpipe, int *dto_waiting);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100111
112/*
113 * Life cycle governed by 'struct urb' (the refcount of the struct is
114 * that of the 'struct urb' and usb_free_urb() would free the whole
115 * struct).
116 */
117struct wa_seg {
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500118 struct urb tr_urb; /* transfer request urb. */
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500119 struct urb *isoc_pack_desc_urb; /* for isoc packet descriptor. */
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500120 struct urb *dto_urb; /* for data output. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100121 struct list_head list_node; /* for rpipe->req_list */
122 struct wa_xfer *xfer; /* out xfer */
123 u8 index; /* which segment we are */
Thomas Pugliese21012422013-10-23 14:44:27 -0500124 int isoc_frame_count; /* number of isoc frames in this segment. */
125 int isoc_frame_offset; /* starting frame offset in the xfer URB. */
Thomas Puglieseea1af422013-12-09 14:15:14 -0600126 /* Isoc frame that the current transfer buffer corresponds to. */
127 int isoc_frame_index;
Thomas Pugliese21012422013-10-23 14:44:27 -0500128 int isoc_size; /* size of all isoc frames sent by this seg. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100129 enum wa_seg_status status;
130 ssize_t result; /* bytes xfered or error */
131 struct wa_xfer_hdr xfer_hdr;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100132};
133
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500134static inline void wa_seg_init(struct wa_seg *seg)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100135{
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500136 usb_init_urb(&seg->tr_urb);
Thomas Pugliese66591015d2013-08-15 14:37:43 -0500137
138 /* set the remaining memory to 0. */
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500139 memset(((void *)seg) + sizeof(seg->tr_urb), 0,
140 sizeof(*seg) - sizeof(seg->tr_urb));
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100141}
142
143/*
144 * Protected by xfer->lock
145 *
146 */
147struct wa_xfer {
148 struct kref refcnt;
149 struct list_head list_node;
150 spinlock_t lock;
151 u32 id;
152
153 struct wahc *wa; /* Wire adapter we are plugged to */
154 struct usb_host_endpoint *ep;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300155 struct urb *urb; /* URB we are transferring for */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100156 struct wa_seg **seg; /* transfer segments */
157 u8 segs, segs_submitted, segs_done;
158 unsigned is_inbound:1;
159 unsigned is_dma:1;
160 size_t seg_size;
161 int result;
162
163 gfp_t gfp; /* allocation mask */
164
165 struct wusb_dev *wusb_dev; /* for activity timestamps */
166};
167
Thomas Pugliese21012422013-10-23 14:44:27 -0500168static void __wa_populate_dto_urb_isoc(struct wa_xfer *xfer,
169 struct wa_seg *seg, int curr_iso_frame);
Thomas Puglieseacfadce2014-02-28 14:31:56 -0600170static void wa_complete_remaining_xfer_segs(struct wa_xfer *xfer,
171 int starting_index, enum wa_seg_status status);
Thomas Pugliese21012422013-10-23 14:44:27 -0500172
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100173static inline void wa_xfer_init(struct wa_xfer *xfer)
174{
175 kref_init(&xfer->refcnt);
176 INIT_LIST_HEAD(&xfer->list_node);
177 spin_lock_init(&xfer->lock);
178}
179
180/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300181 * Destroy a transfer structure
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100182 *
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500183 * Note that freeing xfer->seg[cnt]->tr_urb will free the containing
Thomas Pugliese79731cb2013-08-15 14:37:42 -0500184 * xfer->seg[cnt] memory that was allocated by __wa_xfer_setup_segs.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100185 */
186static void wa_xfer_destroy(struct kref *_xfer)
187{
188 struct wa_xfer *xfer = container_of(_xfer, struct wa_xfer, refcnt);
189 if (xfer->seg) {
190 unsigned cnt;
191 for (cnt = 0; cnt < xfer->segs; cnt++) {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500192 struct wa_seg *seg = xfer->seg[cnt];
193 if (seg) {
194 usb_free_urb(seg->isoc_pack_desc_urb);
195 if (seg->dto_urb) {
196 kfree(seg->dto_urb->sg);
197 usb_free_urb(seg->dto_urb);
Thomas Pugliesed9936702013-09-26 14:08:13 -0500198 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500199 usb_free_urb(&seg->tr_urb);
Thomas Pugliesed9936702013-09-26 14:08:13 -0500200 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100201 }
Thomas Pugliesed9936702013-09-26 14:08:13 -0500202 kfree(xfer->seg);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100203 }
204 kfree(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100205}
206
207static void wa_xfer_get(struct wa_xfer *xfer)
208{
209 kref_get(&xfer->refcnt);
210}
211
212static void wa_xfer_put(struct wa_xfer *xfer)
213{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100214 kref_put(&xfer->refcnt, wa_xfer_destroy);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100215}
216
217/*
Thomas Pugliese679ee472013-10-07 10:53:57 -0500218 * Try to get exclusive access to the DTO endpoint resource. Return true
219 * if successful.
220 */
221static inline int __wa_dto_try_get(struct wahc *wa)
222{
223 return (test_and_set_bit(0, &wa->dto_in_use) == 0);
224}
225
226/* Release the DTO endpoint resource. */
227static inline void __wa_dto_put(struct wahc *wa)
228{
229 clear_bit_unlock(0, &wa->dto_in_use);
230}
231
232/* Service RPIPEs that are waiting on the DTO resource. */
233static void wa_check_for_delayed_rpipes(struct wahc *wa)
234{
235 unsigned long flags;
236 int dto_waiting = 0;
237 struct wa_rpipe *rpipe;
238
239 spin_lock_irqsave(&wa->rpipe_lock, flags);
240 while (!list_empty(&wa->rpipe_delayed_list) && !dto_waiting) {
241 rpipe = list_first_entry(&wa->rpipe_delayed_list,
242 struct wa_rpipe, list_node);
243 __wa_xfer_delayed_run(rpipe, &dto_waiting);
244 /* remove this RPIPE from the list if it is not waiting. */
245 if (!dto_waiting) {
246 pr_debug("%s: RPIPE %d serviced and removed from delayed list.\n",
247 __func__,
248 le16_to_cpu(rpipe->descr.wRPipeIndex));
249 list_del_init(&rpipe->list_node);
250 }
251 }
252 spin_unlock_irqrestore(&wa->rpipe_lock, flags);
253}
254
255/* add this RPIPE to the end of the delayed RPIPE list. */
256static void wa_add_delayed_rpipe(struct wahc *wa, struct wa_rpipe *rpipe)
257{
258 unsigned long flags;
259
260 spin_lock_irqsave(&wa->rpipe_lock, flags);
261 /* add rpipe to the list if it is not already on it. */
262 if (list_empty(&rpipe->list_node)) {
263 pr_debug("%s: adding RPIPE %d to the delayed list.\n",
264 __func__, le16_to_cpu(rpipe->descr.wRPipeIndex));
265 list_add_tail(&rpipe->list_node, &wa->rpipe_delayed_list);
266 }
267 spin_unlock_irqrestore(&wa->rpipe_lock, flags);
268}
269
270/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100271 * xfer is referenced
272 *
273 * xfer->lock has to be unlocked
274 *
275 * We take xfer->lock for setting the result; this is a barrier
276 * against drivers/usb/core/hcd.c:unlink1() being called after we call
277 * usb_hcd_giveback_urb() and wa_urb_dequeue() trying to get a
278 * reference to the transfer.
279 */
280static void wa_xfer_giveback(struct wa_xfer *xfer)
281{
282 unsigned long flags;
David Vrabelbce83692008-12-22 18:22:50 +0000283
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100284 spin_lock_irqsave(&xfer->wa->xfer_list_lock, flags);
285 list_del_init(&xfer->list_node);
Thomas Puglieseb3744872013-11-25 16:17:16 -0600286 usb_hcd_unlink_urb_from_ep(&(xfer->wa->wusb->usb_hcd), xfer->urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100287 spin_unlock_irqrestore(&xfer->wa->xfer_list_lock, flags);
288 /* FIXME: segmentation broken -- kills DWA */
289 wusbhc_giveback_urb(xfer->wa->wusb, xfer->urb, xfer->result);
290 wa_put(xfer->wa);
291 wa_xfer_put(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100292}
293
294/*
295 * xfer is referenced
296 *
297 * xfer->lock has to be unlocked
298 */
299static void wa_xfer_completion(struct wa_xfer *xfer)
300{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100301 if (xfer->wusb_dev)
302 wusb_dev_put(xfer->wusb_dev);
303 rpipe_put(xfer->ep->hcpriv);
304 wa_xfer_giveback(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100305}
306
307/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100308 * Initialize a transfer's ID
309 *
310 * We need to use a sequential number; if we use the pointer or the
311 * hash of the pointer, it can repeat over sequential transfers and
312 * then it will confuse the HWA....wonder why in hell they put a 32
313 * bit handle in there then.
314 */
315static void wa_xfer_id_init(struct wa_xfer *xfer)
316{
317 xfer->id = atomic_add_return(1, &xfer->wa->xfer_id_count);
318}
319
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500320/* Return the xfer's ID. */
321static inline u32 wa_xfer_id(struct wa_xfer *xfer)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100322{
323 return xfer->id;
324}
325
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500326/* Return the xfer's ID in transport format (little endian). */
327static inline __le32 wa_xfer_id_le32(struct wa_xfer *xfer)
328{
329 return cpu_to_le32(xfer->id);
330}
331
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100332/*
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500333 * If transfer is done, wrap it up and return true
334 *
335 * xfer->lock has to be locked
336 */
337static unsigned __wa_xfer_is_done(struct wa_xfer *xfer)
338{
339 struct device *dev = &xfer->wa->usb_iface->dev;
340 unsigned result, cnt;
341 struct wa_seg *seg;
342 struct urb *urb = xfer->urb;
343 unsigned found_short = 0;
344
345 result = xfer->segs_done == xfer->segs_submitted;
346 if (result == 0)
347 goto out;
348 urb->actual_length = 0;
349 for (cnt = 0; cnt < xfer->segs; cnt++) {
350 seg = xfer->seg[cnt];
351 switch (seg->status) {
352 case WA_SEG_DONE:
353 if (found_short && seg->result > 0) {
354 dev_dbg(dev, "xfer %p ID %08X#%u: bad short segments (%zu)\n",
355 xfer, wa_xfer_id(xfer), cnt,
356 seg->result);
357 urb->status = -EINVAL;
358 goto out;
359 }
360 urb->actual_length += seg->result;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500361 if (!(usb_pipeisoc(xfer->urb->pipe))
362 && seg->result < xfer->seg_size
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500363 && cnt != xfer->segs-1)
364 found_short = 1;
365 dev_dbg(dev, "xfer %p ID %08X#%u: DONE short %d "
366 "result %zu urb->actual_length %d\n",
367 xfer, wa_xfer_id(xfer), seg->index, found_short,
368 seg->result, urb->actual_length);
369 break;
370 case WA_SEG_ERROR:
371 xfer->result = seg->result;
Thomas Pugliesecccd3a252013-09-30 22:48:46 -0500372 dev_dbg(dev, "xfer %p ID %08X#%u: ERROR result %zu(0x%08zX)\n",
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500373 xfer, wa_xfer_id(xfer), seg->index, seg->result,
374 seg->result);
375 goto out;
376 case WA_SEG_ABORTED:
Thomas Pugliesebbfc34202013-11-25 16:17:17 -0600377 xfer->result = seg->result;
378 dev_dbg(dev, "xfer %p ID %08X#%u: ABORTED result %zu(0x%08zX)\n",
379 xfer, wa_xfer_id(xfer), seg->index, seg->result,
380 seg->result);
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500381 goto out;
382 default:
383 dev_warn(dev, "xfer %p ID %08X#%u: is_done bad state %d\n",
384 xfer, wa_xfer_id(xfer), cnt, seg->status);
385 xfer->result = -EINVAL;
386 goto out;
387 }
388 }
389 xfer->result = 0;
390out:
391 return result;
392}
393
394/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100395 * Search for a transfer list ID on the HCD's URB list
396 *
397 * For 32 bit architectures, we use the pointer itself; for 64 bits, a
398 * 32-bit hash of the pointer.
399 *
400 * @returns NULL if not found.
401 */
402static struct wa_xfer *wa_xfer_get_by_id(struct wahc *wa, u32 id)
403{
404 unsigned long flags;
405 struct wa_xfer *xfer_itr;
406 spin_lock_irqsave(&wa->xfer_list_lock, flags);
407 list_for_each_entry(xfer_itr, &wa->xfer_list, list_node) {
408 if (id == xfer_itr->id) {
409 wa_xfer_get(xfer_itr);
410 goto out;
411 }
412 }
413 xfer_itr = NULL;
414out:
415 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
416 return xfer_itr;
417}
418
419struct wa_xfer_abort_buffer {
420 struct urb urb;
Thomas Puglieseacfadce2014-02-28 14:31:56 -0600421 struct wahc *wa;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100422 struct wa_xfer_abort cmd;
423};
424
425static void __wa_xfer_abort_cb(struct urb *urb)
426{
427 struct wa_xfer_abort_buffer *b = urb->context;
Thomas Puglieseacfadce2014-02-28 14:31:56 -0600428 struct wahc *wa = b->wa;
429
430 /*
431 * If the abort request URB failed, then the HWA did not get the abort
432 * command. Forcibly clean up the xfer without waiting for a Transfer
433 * Result from the HWA.
434 */
435 if (urb->status < 0) {
436 struct wa_xfer *xfer;
437 struct device *dev = &wa->usb_iface->dev;
438
439 xfer = wa_xfer_get_by_id(wa, le32_to_cpu(b->cmd.dwTransferID));
440 dev_err(dev, "%s: Transfer Abort request failed. result: %d\n",
441 __func__, urb->status);
442 if (xfer) {
443 unsigned long flags;
444 int done;
445 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
446
447 dev_err(dev, "%s: cleaning up xfer %p ID 0x%08X.\n",
448 __func__, xfer, wa_xfer_id(xfer));
449 spin_lock_irqsave(&xfer->lock, flags);
450 /* mark all segs as aborted. */
451 wa_complete_remaining_xfer_segs(xfer, 0,
452 WA_SEG_ABORTED);
453 done = __wa_xfer_is_done(xfer);
454 spin_unlock_irqrestore(&xfer->lock, flags);
455 if (done)
456 wa_xfer_completion(xfer);
457 wa_xfer_delayed_run(rpipe);
458 wa_xfer_put(xfer);
459 } else {
460 dev_err(dev, "%s: xfer ID 0x%08X already gone.\n",
461 __func__, le32_to_cpu(b->cmd.dwTransferID));
462 }
463 }
464
465 wa_put(wa); /* taken in __wa_xfer_abort */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100466 usb_put_urb(&b->urb);
467}
468
469/*
470 * Aborts an ongoing transaction
471 *
472 * Assumes the transfer is referenced and locked and in a submitted
473 * state (mainly that there is an endpoint/rpipe assigned).
474 *
475 * The callback (see above) does nothing but freeing up the data by
476 * putting the URB. Because the URB is allocated at the head of the
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -0500477 * struct, the whole space we allocated is kfreed. *
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100478 */
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -0500479static int __wa_xfer_abort(struct wa_xfer *xfer)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100480{
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -0500481 int result = -ENOMEM;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100482 struct device *dev = &xfer->wa->usb_iface->dev;
483 struct wa_xfer_abort_buffer *b;
484 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
485
486 b = kmalloc(sizeof(*b), GFP_ATOMIC);
487 if (b == NULL)
488 goto error_kmalloc;
489 b->cmd.bLength = sizeof(b->cmd);
490 b->cmd.bRequestType = WA_XFER_ABORT;
491 b->cmd.wRPipe = rpipe->descr.wRPipeIndex;
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500492 b->cmd.dwTransferID = wa_xfer_id_le32(xfer);
Thomas Puglieseacfadce2014-02-28 14:31:56 -0600493 b->wa = wa_get(xfer->wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100494
495 usb_init_urb(&b->urb);
496 usb_fill_bulk_urb(&b->urb, xfer->wa->usb_dev,
497 usb_sndbulkpipe(xfer->wa->usb_dev,
498 xfer->wa->dto_epd->bEndpointAddress),
499 &b->cmd, sizeof(b->cmd), __wa_xfer_abort_cb, b);
500 result = usb_submit_urb(&b->urb, GFP_ATOMIC);
501 if (result < 0)
502 goto error_submit;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -0500503 return result; /* callback frees! */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100504
505
506error_submit:
Thomas Puglieseacfadce2014-02-28 14:31:56 -0600507 wa_put(xfer->wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100508 if (printk_ratelimit())
509 dev_err(dev, "xfer %p: Can't submit abort request: %d\n",
510 xfer, result);
511 kfree(b);
512error_kmalloc:
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -0500513 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100514
515}
516
517/*
Thomas Pugliese21012422013-10-23 14:44:27 -0500518 * Calculate the number of isoc frames starting from isoc_frame_offset
519 * that will fit a in transfer segment.
520 */
521static int __wa_seg_calculate_isoc_frame_count(struct wa_xfer *xfer,
522 int isoc_frame_offset, int *total_size)
523{
524 int segment_size = 0, frame_count = 0;
525 int index = isoc_frame_offset;
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500526 struct usb_iso_packet_descriptor *iso_frame_desc =
527 xfer->urb->iso_frame_desc;
Thomas Pugliese21012422013-10-23 14:44:27 -0500528
529 while ((index < xfer->urb->number_of_packets)
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500530 && ((segment_size + iso_frame_desc[index].length)
Thomas Pugliese21012422013-10-23 14:44:27 -0500531 <= xfer->seg_size)) {
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500532 /*
Thomas Pugliese226b3a22013-12-10 12:10:33 -0600533 * For Alereon HWA devices, only include an isoc frame in an
534 * out segment if it is physically contiguous with the previous
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500535 * frame. This is required because those devices expect
536 * the isoc frames to be sent as a single USB transaction as
537 * opposed to one transaction per frame with standard HWA.
538 */
539 if ((xfer->wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC)
Thomas Pugliese226b3a22013-12-10 12:10:33 -0600540 && (xfer->is_inbound == 0)
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500541 && (index > isoc_frame_offset)
542 && ((iso_frame_desc[index - 1].offset +
543 iso_frame_desc[index - 1].length) !=
544 iso_frame_desc[index].offset))
545 break;
546
Thomas Pugliese21012422013-10-23 14:44:27 -0500547 /* this frame fits. count it. */
548 ++frame_count;
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500549 segment_size += iso_frame_desc[index].length;
Thomas Pugliese21012422013-10-23 14:44:27 -0500550
551 /* move to the next isoc frame. */
552 ++index;
553 }
554
555 *total_size = segment_size;
556 return frame_count;
557}
558
559/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100560 *
561 * @returns < 0 on error, transfer segment request size if ok
562 */
563static ssize_t __wa_xfer_setup_sizes(struct wa_xfer *xfer,
564 enum wa_xfer_type *pxfer_type)
565{
566 ssize_t result;
567 struct device *dev = &xfer->wa->usb_iface->dev;
568 size_t maxpktsize;
569 struct urb *urb = xfer->urb;
570 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
571
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100572 switch (rpipe->descr.bmAttribute & 0x3) {
573 case USB_ENDPOINT_XFER_CONTROL:
574 *pxfer_type = WA_XFER_TYPE_CTL;
575 result = sizeof(struct wa_xfer_ctl);
576 break;
577 case USB_ENDPOINT_XFER_INT:
578 case USB_ENDPOINT_XFER_BULK:
579 *pxfer_type = WA_XFER_TYPE_BI;
580 result = sizeof(struct wa_xfer_bi);
581 break;
582 case USB_ENDPOINT_XFER_ISOC:
Thomas Pugliese226b3a22013-12-10 12:10:33 -0600583 *pxfer_type = WA_XFER_TYPE_ISO;
584 result = sizeof(struct wa_xfer_hwaiso);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500585 break;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100586 default:
587 /* never happens */
588 BUG();
589 result = -EINVAL; /* shut gcc up */
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500590 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100591 xfer->is_inbound = urb->pipe & USB_DIR_IN ? 1 : 0;
592 xfer->is_dma = urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? 1 : 0;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500593
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100594 maxpktsize = le16_to_cpu(rpipe->descr.wMaxPacketSize);
Thomas Pugliese226b3a22013-12-10 12:10:33 -0600595 xfer->seg_size = le16_to_cpu(rpipe->descr.wBlocks)
596 * 1 << (xfer->wa->wa_descr->bRPipeBlockSize - 1);
597 /* Compute the segment size and make sure it is a multiple of
598 * the maxpktsize (WUSB1.0[8.3.3.1])...not really too much of
599 * a check (FIXME) */
600 if (xfer->seg_size < maxpktsize) {
601 dev_err(dev,
602 "HW BUG? seg_size %zu smaller than maxpktsize %zu\n",
603 xfer->seg_size, maxpktsize);
604 result = -EINVAL;
605 goto error;
606 }
607 xfer->seg_size = (xfer->seg_size / maxpktsize) * maxpktsize;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500608 if ((rpipe->descr.bmAttribute & 0x3) == USB_ENDPOINT_XFER_ISOC) {
Thomas Pugliese21012422013-10-23 14:44:27 -0500609 int index = 0;
610
Thomas Pugliese21012422013-10-23 14:44:27 -0500611 xfer->segs = 0;
612 /*
613 * loop over urb->number_of_packets to determine how many
614 * xfer segments will be needed to send the isoc frames.
615 */
616 while (index < urb->number_of_packets) {
617 int seg_size; /* don't care. */
618 index += __wa_seg_calculate_isoc_frame_count(xfer,
619 index, &seg_size);
620 ++xfer->segs;
621 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500622 } else {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500623 xfer->segs = DIV_ROUND_UP(urb->transfer_buffer_length,
624 xfer->seg_size);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500625 if (xfer->segs == 0 && *pxfer_type == WA_XFER_TYPE_CTL)
626 xfer->segs = 1;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100627 }
Thomas Pugliese21012422013-10-23 14:44:27 -0500628
Thomas Pugliesef74b75e2013-10-23 14:44:29 -0500629 if (xfer->segs > WA_SEGS_MAX) {
Thomas Pugliese21012422013-10-23 14:44:27 -0500630 dev_err(dev, "BUG? oops, number of segments %zu bigger than %d\n",
631 (urb->transfer_buffer_length/xfer->seg_size),
632 WA_SEGS_MAX);
633 result = -EINVAL;
634 goto error;
635 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100636error:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100637 return result;
638}
639
Thomas Pugliese21012422013-10-23 14:44:27 -0500640static void __wa_setup_isoc_packet_descr(
641 struct wa_xfer_packet_info_hwaiso *packet_desc,
642 struct wa_xfer *xfer,
643 struct wa_seg *seg) {
644 struct usb_iso_packet_descriptor *iso_frame_desc =
645 xfer->urb->iso_frame_desc;
646 int frame_index;
647
648 /* populate isoc packet descriptor. */
649 packet_desc->bPacketType = WA_XFER_ISO_PACKET_INFO;
650 packet_desc->wLength = cpu_to_le16(sizeof(*packet_desc) +
651 (sizeof(packet_desc->PacketLength[0]) *
652 seg->isoc_frame_count));
653 for (frame_index = 0; frame_index < seg->isoc_frame_count;
654 ++frame_index) {
655 int offset_index = frame_index + seg->isoc_frame_offset;
656 packet_desc->PacketLength[frame_index] =
657 cpu_to_le16(iso_frame_desc[offset_index].length);
658 }
659}
660
661
David Vrabelbce83692008-12-22 18:22:50 +0000662/* Fill in the common request header and xfer-type specific data. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100663static void __wa_xfer_setup_hdr0(struct wa_xfer *xfer,
664 struct wa_xfer_hdr *xfer_hdr0,
665 enum wa_xfer_type xfer_type,
666 size_t xfer_hdr_size)
667{
668 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
Thomas Pugliese21012422013-10-23 14:44:27 -0500669 struct wa_seg *seg = xfer->seg[0];
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100670
Thomas Pugliese21012422013-10-23 14:44:27 -0500671 xfer_hdr0 = &seg->xfer_hdr;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100672 xfer_hdr0->bLength = xfer_hdr_size;
673 xfer_hdr0->bRequestType = xfer_type;
674 xfer_hdr0->wRPipe = rpipe->descr.wRPipeIndex;
Thomas Pugliesefdd160c2013-09-27 15:33:35 -0500675 xfer_hdr0->dwTransferID = wa_xfer_id_le32(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100676 xfer_hdr0->bTransferSegment = 0;
677 switch (xfer_type) {
678 case WA_XFER_TYPE_CTL: {
679 struct wa_xfer_ctl *xfer_ctl =
680 container_of(xfer_hdr0, struct wa_xfer_ctl, hdr);
681 xfer_ctl->bmAttribute = xfer->is_inbound ? 1 : 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100682 memcpy(&xfer_ctl->baSetupData, xfer->urb->setup_packet,
683 sizeof(xfer_ctl->baSetupData));
684 break;
685 }
686 case WA_XFER_TYPE_BI:
687 break;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500688 case WA_XFER_TYPE_ISO: {
689 struct wa_xfer_hwaiso *xfer_iso =
690 container_of(xfer_hdr0, struct wa_xfer_hwaiso, hdr);
691 struct wa_xfer_packet_info_hwaiso *packet_desc =
692 ((void *)xfer_iso) + xfer_hdr_size;
Thomas Pugliese21012422013-10-23 14:44:27 -0500693
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500694 /* populate the isoc section of the transfer request. */
Thomas Pugliese21012422013-10-23 14:44:27 -0500695 xfer_iso->dwNumOfPackets = cpu_to_le32(seg->isoc_frame_count);
696 /* populate isoc packet descriptor. */
697 __wa_setup_isoc_packet_descr(packet_desc, xfer, seg);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500698 break;
699 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100700 default:
701 BUG();
702 };
703}
704
705/*
706 * Callback for the OUT data phase of the segment request
707 *
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500708 * Check wa_seg_tr_cb(); most comments also apply here because this
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100709 * function does almost the same thing and they work closely
710 * together.
711 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300712 * If the seg request has failed but this DTO phase has succeeded,
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500713 * wa_seg_tr_cb() has already failed the segment and moved the
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100714 * status to WA_SEG_ERROR, so this will go through 'case 0' and
715 * effectively do nothing.
716 */
717static void wa_seg_dto_cb(struct urb *urb)
718{
719 struct wa_seg *seg = urb->context;
720 struct wa_xfer *xfer = seg->xfer;
721 struct wahc *wa;
722 struct device *dev;
723 struct wa_rpipe *rpipe;
724 unsigned long flags;
725 unsigned rpipe_ready = 0;
Thomas Pugliese21012422013-10-23 14:44:27 -0500726 int data_send_done = 1, release_dto = 0, holding_dto = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100727 u8 done = 0;
Thomas Pugliese21012422013-10-23 14:44:27 -0500728 int result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100729
Thomas Pugliesed5b5c9f2013-09-26 14:08:15 -0500730 /* free the sg if it was used. */
731 kfree(urb->sg);
732 urb->sg = NULL;
733
Thomas Pugliese21012422013-10-23 14:44:27 -0500734 spin_lock_irqsave(&xfer->lock, flags);
735 wa = xfer->wa;
736 dev = &wa->usb_iface->dev;
737 if (usb_pipeisoc(xfer->urb->pipe)) {
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500738 /* Alereon HWA sends all isoc frames in a single transfer. */
739 if (wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC)
Thomas Puglieseea1af422013-12-09 14:15:14 -0600740 seg->isoc_frame_index += seg->isoc_frame_count;
Thomas Pugliesef07ddb92013-10-23 14:44:28 -0500741 else
Thomas Puglieseea1af422013-12-09 14:15:14 -0600742 seg->isoc_frame_index += 1;
743 if (seg->isoc_frame_index < seg->isoc_frame_count) {
Thomas Pugliese21012422013-10-23 14:44:27 -0500744 data_send_done = 0;
745 holding_dto = 1; /* checked in error cases. */
746 /*
747 * if this is the last isoc frame of the segment, we
748 * can release DTO after sending this frame.
749 */
Thomas Puglieseea1af422013-12-09 14:15:14 -0600750 if ((seg->isoc_frame_index + 1) >=
Thomas Pugliese21012422013-10-23 14:44:27 -0500751 seg->isoc_frame_count)
752 release_dto = 1;
753 }
754 dev_dbg(dev, "xfer 0x%08X#%u: isoc frame = %d, holding_dto = %d, release_dto = %d.\n",
Thomas Puglieseea1af422013-12-09 14:15:14 -0600755 wa_xfer_id(xfer), seg->index, seg->isoc_frame_index,
756 holding_dto, release_dto);
Thomas Pugliese21012422013-10-23 14:44:27 -0500757 }
758 spin_unlock_irqrestore(&xfer->lock, flags);
759
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100760 switch (urb->status) {
761 case 0:
762 spin_lock_irqsave(&xfer->lock, flags);
Thomas Pugliese21012422013-10-23 14:44:27 -0500763 seg->result += urb->actual_length;
764 if (data_send_done) {
765 dev_dbg(dev, "xfer 0x%08X#%u: data out done (%zu bytes)\n",
766 wa_xfer_id(xfer), seg->index, seg->result);
767 if (seg->status < WA_SEG_PENDING)
768 seg->status = WA_SEG_PENDING;
769 } else {
770 /* should only hit this for isoc xfers. */
771 /*
772 * Populate the dto URB with the next isoc frame buffer,
773 * send the URB and release DTO if we no longer need it.
774 */
775 __wa_populate_dto_urb_isoc(xfer, seg,
Thomas Puglieseea1af422013-12-09 14:15:14 -0600776 seg->isoc_frame_offset + seg->isoc_frame_index);
Thomas Pugliese21012422013-10-23 14:44:27 -0500777
778 /* resubmit the URB with the next isoc frame. */
Thomas Pugliese618836cc2014-02-28 14:31:55 -0600779 /* take a ref on resubmit. */
780 wa_xfer_get(xfer);
Thomas Pugliese21012422013-10-23 14:44:27 -0500781 result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
782 if (result < 0) {
783 dev_err(dev, "xfer 0x%08X#%u: DTO submit failed: %d\n",
784 wa_xfer_id(xfer), seg->index, result);
785 spin_unlock_irqrestore(&xfer->lock, flags);
786 goto error_dto_submit;
787 }
788 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100789 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Pugliese21012422013-10-23 14:44:27 -0500790 if (release_dto) {
791 __wa_dto_put(wa);
792 wa_check_for_delayed_rpipes(wa);
793 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100794 break;
795 case -ECONNRESET: /* URB unlinked; no need to do anything */
796 case -ENOENT: /* as it was done by the who unlinked us */
Thomas Pugliese21012422013-10-23 14:44:27 -0500797 if (holding_dto) {
798 __wa_dto_put(wa);
799 wa_check_for_delayed_rpipes(wa);
800 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100801 break;
802 default: /* Other errors ... */
Thomas Pugliese21012422013-10-23 14:44:27 -0500803 dev_err(dev, "xfer 0x%08X#%u: data out error %d\n",
804 wa_xfer_id(xfer), seg->index, urb->status);
805 goto error_default;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100806 }
Thomas Pugliese21012422013-10-23 14:44:27 -0500807
Thomas Pugliese618836cc2014-02-28 14:31:55 -0600808 /* taken when this URB was submitted. */
809 wa_xfer_put(xfer);
Thomas Pugliese21012422013-10-23 14:44:27 -0500810 return;
811
812error_dto_submit:
Thomas Pugliese618836cc2014-02-28 14:31:55 -0600813 /* taken on resubmit attempt. */
814 wa_xfer_put(xfer);
Thomas Pugliese21012422013-10-23 14:44:27 -0500815error_default:
816 spin_lock_irqsave(&xfer->lock, flags);
817 rpipe = xfer->ep->hcpriv;
818 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
819 EDC_ERROR_TIMEFRAME)){
820 dev_err(dev, "DTO: URB max acceptable errors exceeded, resetting device\n");
821 wa_reset_all(wa);
822 }
823 if (seg->status != WA_SEG_ERROR) {
824 seg->status = WA_SEG_ERROR;
825 seg->result = urb->status;
826 xfer->segs_done++;
827 __wa_xfer_abort(xfer);
828 rpipe_ready = rpipe_avail_inc(rpipe);
829 done = __wa_xfer_is_done(xfer);
830 }
831 spin_unlock_irqrestore(&xfer->lock, flags);
832 if (holding_dto) {
833 __wa_dto_put(wa);
834 wa_check_for_delayed_rpipes(wa);
835 }
836 if (done)
837 wa_xfer_completion(xfer);
838 if (rpipe_ready)
839 wa_xfer_delayed_run(rpipe);
Thomas Pugliese618836cc2014-02-28 14:31:55 -0600840 /* taken when this URB was submitted. */
841 wa_xfer_put(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100842}
843
844/*
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500845 * Callback for the isoc packet descriptor phase of the segment request
846 *
847 * Check wa_seg_tr_cb(); most comments also apply here because this
848 * function does almost the same thing and they work closely
849 * together.
850 *
851 * If the seg request has failed but this phase has succeeded,
852 * wa_seg_tr_cb() has already failed the segment and moved the
853 * status to WA_SEG_ERROR, so this will go through 'case 0' and
854 * effectively do nothing.
855 */
856static void wa_seg_iso_pack_desc_cb(struct urb *urb)
857{
858 struct wa_seg *seg = urb->context;
859 struct wa_xfer *xfer = seg->xfer;
860 struct wahc *wa;
861 struct device *dev;
862 struct wa_rpipe *rpipe;
863 unsigned long flags;
864 unsigned rpipe_ready = 0;
865 u8 done = 0;
866
867 switch (urb->status) {
868 case 0:
869 spin_lock_irqsave(&xfer->lock, flags);
870 wa = xfer->wa;
871 dev = &wa->usb_iface->dev;
Thomas Pugliese21012422013-10-23 14:44:27 -0500872 dev_dbg(dev, "iso xfer %08X#%u: packet descriptor done\n",
873 wa_xfer_id(xfer), seg->index);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500874 if (xfer->is_inbound && seg->status < WA_SEG_PENDING)
875 seg->status = WA_SEG_PENDING;
876 spin_unlock_irqrestore(&xfer->lock, flags);
877 break;
878 case -ECONNRESET: /* URB unlinked; no need to do anything */
879 case -ENOENT: /* as it was done by the who unlinked us */
880 break;
881 default: /* Other errors ... */
882 spin_lock_irqsave(&xfer->lock, flags);
883 wa = xfer->wa;
884 dev = &wa->usb_iface->dev;
885 rpipe = xfer->ep->hcpriv;
Thomas Pugliese21012422013-10-23 14:44:27 -0500886 pr_err_ratelimited("iso xfer %08X#%u: packet descriptor error %d\n",
887 wa_xfer_id(xfer), seg->index, urb->status);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500888 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
889 EDC_ERROR_TIMEFRAME)){
Thomas Pugliese226b3a22013-12-10 12:10:33 -0600890 dev_err(dev, "iso xfer: URB max acceptable errors exceeded, resetting device\n");
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500891 wa_reset_all(wa);
892 }
893 if (seg->status != WA_SEG_ERROR) {
894 usb_unlink_urb(seg->dto_urb);
895 seg->status = WA_SEG_ERROR;
896 seg->result = urb->status;
897 xfer->segs_done++;
898 __wa_xfer_abort(xfer);
899 rpipe_ready = rpipe_avail_inc(rpipe);
900 done = __wa_xfer_is_done(xfer);
901 }
902 spin_unlock_irqrestore(&xfer->lock, flags);
903 if (done)
904 wa_xfer_completion(xfer);
905 if (rpipe_ready)
906 wa_xfer_delayed_run(rpipe);
907 }
Thomas Pugliese618836cc2014-02-28 14:31:55 -0600908 /* taken when this URB was submitted. */
909 wa_xfer_put(xfer);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500910}
911
912/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100913 * Callback for the segment request
914 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200915 * If successful transition state (unless already transitioned or
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100916 * outbound transfer); otherwise, take a note of the error, mark this
917 * segment done and try completion.
918 *
919 * Note we don't access until we are sure that the transfer hasn't
920 * been cancelled (ECONNRESET, ENOENT), which could mean that
921 * seg->xfer could be already gone.
922 *
923 * We have to check before setting the status to WA_SEG_PENDING
924 * because sometimes the xfer result callback arrives before this
925 * callback (geeeeeeze), so it might happen that we are already in
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500926 * another state. As well, we don't set it if the transfer is not inbound,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100927 * as in that case, wa_seg_dto_cb will do it when the OUT data phase
928 * finishes.
929 */
Thomas Pugliese09d94cb2013-09-26 10:49:40 -0500930static void wa_seg_tr_cb(struct urb *urb)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100931{
932 struct wa_seg *seg = urb->context;
933 struct wa_xfer *xfer = seg->xfer;
934 struct wahc *wa;
935 struct device *dev;
936 struct wa_rpipe *rpipe;
937 unsigned long flags;
938 unsigned rpipe_ready;
939 u8 done = 0;
940
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100941 switch (urb->status) {
942 case 0:
943 spin_lock_irqsave(&xfer->lock, flags);
944 wa = xfer->wa;
945 dev = &wa->usb_iface->dev;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500946 dev_dbg(dev, "xfer %p ID 0x%08X#%u: request done\n",
947 xfer, wa_xfer_id(xfer), seg->index);
948 if (xfer->is_inbound &&
949 seg->status < WA_SEG_PENDING &&
950 !(usb_pipeisoc(xfer->urb->pipe)))
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100951 seg->status = WA_SEG_PENDING;
952 spin_unlock_irqrestore(&xfer->lock, flags);
953 break;
954 case -ECONNRESET: /* URB unlinked; no need to do anything */
955 case -ENOENT: /* as it was done by the who unlinked us */
956 break;
957 default: /* Other errors ... */
958 spin_lock_irqsave(&xfer->lock, flags);
959 wa = xfer->wa;
960 dev = &wa->usb_iface->dev;
961 rpipe = xfer->ep->hcpriv;
962 if (printk_ratelimit())
Thomas Puglieseb9c84be2013-09-27 15:33:36 -0500963 dev_err(dev, "xfer %p ID 0x%08X#%u: request error %d\n",
964 xfer, wa_xfer_id(xfer), seg->index,
965 urb->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100966 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
967 EDC_ERROR_TIMEFRAME)){
968 dev_err(dev, "DTO: URB max acceptable errors "
969 "exceeded, resetting device\n");
970 wa_reset_all(wa);
971 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -0500972 usb_unlink_urb(seg->isoc_pack_desc_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100973 usb_unlink_urb(seg->dto_urb);
974 seg->status = WA_SEG_ERROR;
975 seg->result = urb->status;
976 xfer->segs_done++;
977 __wa_xfer_abort(xfer);
978 rpipe_ready = rpipe_avail_inc(rpipe);
979 done = __wa_xfer_is_done(xfer);
980 spin_unlock_irqrestore(&xfer->lock, flags);
981 if (done)
982 wa_xfer_completion(xfer);
983 if (rpipe_ready)
984 wa_xfer_delayed_run(rpipe);
985 }
Thomas Pugliese618836cc2014-02-28 14:31:55 -0600986 /* taken when this URB was submitted. */
987 wa_xfer_put(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +0100988}
989
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500990/*
991 * Allocate an SG list to store bytes_to_transfer bytes and copy the
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500992 * subset of the in_sg that matches the buffer subset
Thomas Puglieseffd6d172013-09-26 14:08:14 -0500993 * we are about to transfer.
994 */
Thomas Pugliese2b81c082013-06-11 10:39:31 -0500995static struct scatterlist *wa_xfer_create_subset_sg(struct scatterlist *in_sg,
996 const unsigned int bytes_transferred,
997 const unsigned int bytes_to_transfer, unsigned int *out_num_sgs)
998{
999 struct scatterlist *out_sg;
1000 unsigned int bytes_processed = 0, offset_into_current_page_data = 0,
1001 nents;
1002 struct scatterlist *current_xfer_sg = in_sg;
1003 struct scatterlist *current_seg_sg, *last_seg_sg;
1004
1005 /* skip previously transferred pages. */
1006 while ((current_xfer_sg) &&
1007 (bytes_processed < bytes_transferred)) {
1008 bytes_processed += current_xfer_sg->length;
1009
1010 /* advance the sg if current segment starts on or past the
1011 next page. */
1012 if (bytes_processed <= bytes_transferred)
1013 current_xfer_sg = sg_next(current_xfer_sg);
1014 }
1015
1016 /* the data for the current segment starts in current_xfer_sg.
1017 calculate the offset. */
1018 if (bytes_processed > bytes_transferred) {
1019 offset_into_current_page_data = current_xfer_sg->length -
1020 (bytes_processed - bytes_transferred);
1021 }
1022
1023 /* calculate the number of pages needed by this segment. */
1024 nents = DIV_ROUND_UP((bytes_to_transfer +
1025 offset_into_current_page_data +
1026 current_xfer_sg->offset),
1027 PAGE_SIZE);
1028
1029 out_sg = kmalloc((sizeof(struct scatterlist) * nents), GFP_ATOMIC);
1030 if (out_sg) {
1031 sg_init_table(out_sg, nents);
1032
1033 /* copy the portion of the incoming SG that correlates to the
1034 * data to be transferred by this segment to the segment SG. */
1035 last_seg_sg = current_seg_sg = out_sg;
1036 bytes_processed = 0;
1037
1038 /* reset nents and calculate the actual number of sg entries
1039 needed. */
1040 nents = 0;
1041 while ((bytes_processed < bytes_to_transfer) &&
1042 current_seg_sg && current_xfer_sg) {
1043 unsigned int page_len = min((current_xfer_sg->length -
1044 offset_into_current_page_data),
1045 (bytes_to_transfer - bytes_processed));
1046
1047 sg_set_page(current_seg_sg, sg_page(current_xfer_sg),
1048 page_len,
1049 current_xfer_sg->offset +
1050 offset_into_current_page_data);
1051
1052 bytes_processed += page_len;
1053
1054 last_seg_sg = current_seg_sg;
1055 current_seg_sg = sg_next(current_seg_sg);
1056 current_xfer_sg = sg_next(current_xfer_sg);
1057
1058 /* only the first page may require additional offset. */
1059 offset_into_current_page_data = 0;
1060 nents++;
1061 }
1062
1063 /* update num_sgs and terminate the list since we may have
1064 * concatenated pages. */
1065 sg_mark_end(last_seg_sg);
1066 *out_num_sgs = nents;
1067 }
1068
1069 return out_sg;
1070}
1071
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001072/*
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001073 * Populate DMA buffer info for the isoc dto urb.
1074 */
Thomas Pugliese21012422013-10-23 14:44:27 -05001075static void __wa_populate_dto_urb_isoc(struct wa_xfer *xfer,
1076 struct wa_seg *seg, int curr_iso_frame)
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001077{
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001078 seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1079 seg->dto_urb->sg = NULL;
1080 seg->dto_urb->num_sgs = 0;
Thomas Pugliesef07ddb92013-10-23 14:44:28 -05001081 /* dto urb buffer address pulled from iso_frame_desc. */
1082 seg->dto_urb->transfer_dma = xfer->urb->transfer_dma +
1083 xfer->urb->iso_frame_desc[curr_iso_frame].offset;
1084 /* The Alereon HWA sends a single URB with all isoc segs. */
1085 if (xfer->wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC)
1086 seg->dto_urb->transfer_buffer_length = seg->isoc_size;
1087 else
1088 seg->dto_urb->transfer_buffer_length =
1089 xfer->urb->iso_frame_desc[curr_iso_frame].length;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001090}
1091
1092/*
Thomas Puglieseffd6d172013-09-26 14:08:14 -05001093 * Populate buffer ptr and size, DMA buffer or SG list for the dto urb.
1094 */
1095static int __wa_populate_dto_urb(struct wa_xfer *xfer,
1096 struct wa_seg *seg, size_t buf_itr_offset, size_t buf_itr_size)
1097{
1098 int result = 0;
1099
1100 if (xfer->is_dma) {
1101 seg->dto_urb->transfer_dma =
1102 xfer->urb->transfer_dma + buf_itr_offset;
1103 seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1104 seg->dto_urb->sg = NULL;
1105 seg->dto_urb->num_sgs = 0;
1106 } else {
1107 /* do buffer or SG processing. */
1108 seg->dto_urb->transfer_flags &=
1109 ~URB_NO_TRANSFER_DMA_MAP;
1110 /* this should always be 0 before a resubmit. */
1111 seg->dto_urb->num_mapped_sgs = 0;
1112
1113 if (xfer->urb->transfer_buffer) {
1114 seg->dto_urb->transfer_buffer =
1115 xfer->urb->transfer_buffer +
1116 buf_itr_offset;
1117 seg->dto_urb->sg = NULL;
1118 seg->dto_urb->num_sgs = 0;
1119 } else {
1120 seg->dto_urb->transfer_buffer = NULL;
1121
1122 /*
1123 * allocate an SG list to store seg_size bytes
1124 * and copy the subset of the xfer->urb->sg that
1125 * matches the buffer subset we are about to
1126 * read.
1127 */
1128 seg->dto_urb->sg = wa_xfer_create_subset_sg(
1129 xfer->urb->sg,
1130 buf_itr_offset, buf_itr_size,
1131 &(seg->dto_urb->num_sgs));
1132 if (!(seg->dto_urb->sg))
1133 result = -ENOMEM;
1134 }
1135 }
1136 seg->dto_urb->transfer_buffer_length = buf_itr_size;
1137
1138 return result;
1139}
1140
1141/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001142 * Allocate the segs array and initialize each of them
1143 *
1144 * The segments are freed by wa_xfer_destroy() when the xfer use count
1145 * drops to zero; however, because each segment is given the same life
1146 * cycle as the USB URB it contains, it is actually freed by
1147 * usb_put_urb() on the contained USB URB (twisted, eh?).
1148 */
1149static int __wa_xfer_setup_segs(struct wa_xfer *xfer, size_t xfer_hdr_size)
1150{
Thomas Pugliese21012422013-10-23 14:44:27 -05001151 int result, cnt, iso_frame_offset;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001152 size_t alloc_size = sizeof(*xfer->seg[0])
1153 - sizeof(xfer->seg[0]->xfer_hdr) + xfer_hdr_size;
1154 struct usb_device *usb_dev = xfer->wa->usb_dev;
1155 const struct usb_endpoint_descriptor *dto_epd = xfer->wa->dto_epd;
1156 struct wa_seg *seg;
Thomas Pugliese21012422013-10-23 14:44:27 -05001157 size_t buf_itr, buf_size, buf_itr_size;
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001158 int isoc_frame_offset = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001159
1160 result = -ENOMEM;
David Vrabel92c4d9b2008-10-15 14:50:10 +01001161 xfer->seg = kcalloc(xfer->segs, sizeof(xfer->seg[0]), GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001162 if (xfer->seg == NULL)
1163 goto error_segs_kzalloc;
1164 buf_itr = 0;
1165 buf_size = xfer->urb->transfer_buffer_length;
Thomas Pugliese21012422013-10-23 14:44:27 -05001166 iso_frame_offset = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001167 for (cnt = 0; cnt < xfer->segs; cnt++) {
Thomas Pugliese21012422013-10-23 14:44:27 -05001168 size_t iso_pkt_descr_size = 0;
1169 int seg_isoc_frame_count = 0, seg_isoc_size = 0;
1170
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001171 /*
1172 * Adjust the size of the segment object to contain space for
1173 * the isoc packet descriptor buffer.
1174 */
Thomas Pugliese21012422013-10-23 14:44:27 -05001175 if (usb_pipeisoc(xfer->urb->pipe)) {
1176 seg_isoc_frame_count =
1177 __wa_seg_calculate_isoc_frame_count(xfer,
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001178 isoc_frame_offset, &seg_isoc_size);
Thomas Pugliese21012422013-10-23 14:44:27 -05001179
1180 iso_pkt_descr_size =
1181 sizeof(struct wa_xfer_packet_info_hwaiso) +
1182 (seg_isoc_frame_count * sizeof(__le16));
1183 }
1184 seg = xfer->seg[cnt] = kmalloc(alloc_size + iso_pkt_descr_size,
1185 GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001186 if (seg == NULL)
Thomas Pugliese66591015d2013-08-15 14:37:43 -05001187 goto error_seg_kmalloc;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001188 wa_seg_init(seg);
1189 seg->xfer = xfer;
1190 seg->index = cnt;
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001191 usb_fill_bulk_urb(&seg->tr_urb, usb_dev,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001192 usb_sndbulkpipe(usb_dev,
1193 dto_epd->bEndpointAddress),
1194 &seg->xfer_hdr, xfer_hdr_size,
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001195 wa_seg_tr_cb, seg);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001196 buf_itr_size = min(buf_size, xfer->seg_size);
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001197
1198 if (usb_pipeisoc(xfer->urb->pipe)) {
1199 seg->isoc_frame_count = seg_isoc_frame_count;
1200 seg->isoc_frame_offset = isoc_frame_offset;
1201 seg->isoc_size = seg_isoc_size;
1202 /* iso packet descriptor. */
1203 seg->isoc_pack_desc_urb =
1204 usb_alloc_urb(0, GFP_ATOMIC);
1205 if (seg->isoc_pack_desc_urb == NULL)
1206 goto error_iso_pack_desc_alloc;
1207 /*
1208 * The buffer for the isoc packet descriptor starts
1209 * after the transfer request header in the
1210 * segment object memory buffer.
1211 */
1212 usb_fill_bulk_urb(
1213 seg->isoc_pack_desc_urb, usb_dev,
1214 usb_sndbulkpipe(usb_dev,
1215 dto_epd->bEndpointAddress),
1216 (void *)(&seg->xfer_hdr) +
1217 xfer_hdr_size,
1218 iso_pkt_descr_size,
1219 wa_seg_iso_pack_desc_cb, seg);
1220
1221 /* adjust starting frame offset for next seg. */
1222 isoc_frame_offset += seg_isoc_frame_count;
1223 }
1224
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001225 if (xfer->is_inbound == 0 && buf_size > 0) {
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001226 /* outbound data. */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001227 seg->dto_urb = usb_alloc_urb(0, GFP_ATOMIC);
1228 if (seg->dto_urb == NULL)
1229 goto error_dto_alloc;
1230 usb_fill_bulk_urb(
1231 seg->dto_urb, usb_dev,
1232 usb_sndbulkpipe(usb_dev,
1233 dto_epd->bEndpointAddress),
1234 NULL, 0, wa_seg_dto_cb, seg);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001235
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001236 if (usb_pipeisoc(xfer->urb->pipe)) {
Thomas Pugliese21012422013-10-23 14:44:27 -05001237 /*
1238 * Fill in the xfer buffer information for the
1239 * first isoc frame. Subsequent frames in this
1240 * segment will be filled in and sent from the
1241 * DTO completion routine, if needed.
1242 */
1243 __wa_populate_dto_urb_isoc(xfer, seg,
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001244 seg->isoc_frame_offset);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001245 } else {
1246 /* fill in the xfer buffer information. */
1247 result = __wa_populate_dto_urb(xfer, seg,
1248 buf_itr, buf_itr_size);
1249 if (result < 0)
1250 goto error_seg_outbound_populate;
1251
1252 buf_itr += buf_itr_size;
1253 buf_size -= buf_itr_size;
1254 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001255 }
1256 seg->status = WA_SEG_READY;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001257 }
1258 return 0;
1259
Thomas Puglieseffd6d172013-09-26 14:08:14 -05001260 /*
1261 * Free the memory for the current segment which failed to init.
1262 * Use the fact that cnt is left at were it failed. The remaining
1263 * segments will be cleaned up by wa_xfer_destroy.
1264 */
1265error_seg_outbound_populate:
Thomas Pugliese11b1bf82013-08-15 14:37:41 -05001266 usb_free_urb(xfer->seg[cnt]->dto_urb);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001267error_dto_alloc:
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001268 usb_free_urb(xfer->seg[cnt]->isoc_pack_desc_urb);
1269error_iso_pack_desc_alloc:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001270 kfree(xfer->seg[cnt]);
Thomas Puglieseffd6d172013-09-26 14:08:14 -05001271 xfer->seg[cnt] = NULL;
Thomas Pugliese66591015d2013-08-15 14:37:43 -05001272error_seg_kmalloc:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001273error_segs_kzalloc:
1274 return result;
1275}
1276
1277/*
1278 * Allocates all the stuff needed to submit a transfer
1279 *
1280 * Breaks the whole data buffer in a list of segments, each one has a
1281 * structure allocated to it and linked in xfer->seg[index]
1282 *
1283 * FIXME: merge setup_segs() and the last part of this function, no
1284 * need to do two for loops when we could run everything in a
1285 * single one
1286 */
1287static int __wa_xfer_setup(struct wa_xfer *xfer, struct urb *urb)
1288{
1289 int result;
1290 struct device *dev = &xfer->wa->usb_iface->dev;
1291 enum wa_xfer_type xfer_type = 0; /* shut up GCC */
1292 size_t xfer_hdr_size, cnt, transfer_size;
1293 struct wa_xfer_hdr *xfer_hdr0, *xfer_hdr;
1294
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001295 result = __wa_xfer_setup_sizes(xfer, &xfer_type);
1296 if (result < 0)
1297 goto error_setup_sizes;
1298 xfer_hdr_size = result;
1299 result = __wa_xfer_setup_segs(xfer, xfer_hdr_size);
1300 if (result < 0) {
1301 dev_err(dev, "xfer %p: Failed to allocate %d segments: %d\n",
1302 xfer, xfer->segs, result);
1303 goto error_setup_segs;
1304 }
1305 /* Fill the first header */
1306 xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
1307 wa_xfer_id_init(xfer);
1308 __wa_xfer_setup_hdr0(xfer, xfer_hdr0, xfer_type, xfer_hdr_size);
1309
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001310 /* Fill remaining headers */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001311 xfer_hdr = xfer_hdr0;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001312 if (xfer_type == WA_XFER_TYPE_ISO) {
1313 xfer_hdr0->dwTransferLength =
Thomas Pugliese21012422013-10-23 14:44:27 -05001314 cpu_to_le32(xfer->seg[0]->isoc_size);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001315 for (cnt = 1; cnt < xfer->segs; cnt++) {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001316 struct wa_xfer_packet_info_hwaiso *packet_desc;
Thomas Pugliese21012422013-10-23 14:44:27 -05001317 struct wa_seg *seg = xfer->seg[cnt];
Thomas Pugliese756a2ee2013-12-09 14:15:15 -06001318 struct wa_xfer_hwaiso *xfer_iso;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001319
Thomas Pugliese21012422013-10-23 14:44:27 -05001320 xfer_hdr = &seg->xfer_hdr;
Thomas Pugliese756a2ee2013-12-09 14:15:15 -06001321 xfer_iso = container_of(xfer_hdr,
1322 struct wa_xfer_hwaiso, hdr);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001323 packet_desc = ((void *)xfer_hdr) + xfer_hdr_size;
1324 /*
Thomas Pugliese21012422013-10-23 14:44:27 -05001325 * Copy values from the 0th header. Segment specific
1326 * values are set below.
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001327 */
Thomas Pugliese21012422013-10-23 14:44:27 -05001328 memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001329 xfer_hdr->bTransferSegment = cnt;
1330 xfer_hdr->dwTransferLength =
Thomas Pugliese21012422013-10-23 14:44:27 -05001331 cpu_to_le32(seg->isoc_size);
Thomas Pugliese756a2ee2013-12-09 14:15:15 -06001332 xfer_iso->dwNumOfPackets =
1333 cpu_to_le32(seg->isoc_frame_count);
Thomas Pugliese21012422013-10-23 14:44:27 -05001334 __wa_setup_isoc_packet_descr(packet_desc, xfer, seg);
1335 seg->status = WA_SEG_READY;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001336 }
1337 } else {
1338 transfer_size = urb->transfer_buffer_length;
1339 xfer_hdr0->dwTransferLength = transfer_size > xfer->seg_size ?
1340 cpu_to_le32(xfer->seg_size) :
1341 cpu_to_le32(transfer_size);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001342 transfer_size -= xfer->seg_size;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001343 for (cnt = 1; cnt < xfer->segs; cnt++) {
1344 xfer_hdr = &xfer->seg[cnt]->xfer_hdr;
1345 memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
1346 xfer_hdr->bTransferSegment = cnt;
1347 xfer_hdr->dwTransferLength =
1348 transfer_size > xfer->seg_size ?
1349 cpu_to_le32(xfer->seg_size)
1350 : cpu_to_le32(transfer_size);
1351 xfer->seg[cnt]->status = WA_SEG_READY;
1352 transfer_size -= xfer->seg_size;
1353 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001354 }
1355 xfer_hdr->bTransferSegment |= 0x80; /* this is the last segment */
1356 result = 0;
1357error_setup_segs:
1358error_setup_sizes:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001359 return result;
1360}
1361
1362/*
1363 *
1364 *
1365 * rpipe->seg_lock is held!
1366 */
1367static int __wa_seg_submit(struct wa_rpipe *rpipe, struct wa_xfer *xfer,
Thomas Pugliese679ee472013-10-07 10:53:57 -05001368 struct wa_seg *seg, int *dto_done)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001369{
1370 int result;
Thomas Pugliese679ee472013-10-07 10:53:57 -05001371
1372 /* default to done unless we encounter a multi-frame isoc segment. */
1373 *dto_done = 1;
1374
Thomas Pugliese618836cc2014-02-28 14:31:55 -06001375 /*
1376 * Take a ref for each segment urb so the xfer cannot disappear until
1377 * all of the callbacks run.
1378 */
1379 wa_xfer_get(xfer);
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001380 /* submit the transfer request. */
Thomas Pugliese618836cc2014-02-28 14:31:55 -06001381 seg->status = WA_SEG_SUBMITTED;
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001382 result = usb_submit_urb(&seg->tr_urb, GFP_ATOMIC);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001383 if (result < 0) {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001384 pr_err("%s: xfer %p#%u: REQ submit failed: %d\n",
1385 __func__, xfer, seg->index, result);
Thomas Pugliese618836cc2014-02-28 14:31:55 -06001386 wa_xfer_put(xfer);
1387 goto error_tr_submit;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001388 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001389 /* submit the isoc packet descriptor if present. */
1390 if (seg->isoc_pack_desc_urb) {
Thomas Pugliese618836cc2014-02-28 14:31:55 -06001391 wa_xfer_get(xfer);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001392 result = usb_submit_urb(seg->isoc_pack_desc_urb, GFP_ATOMIC);
Thomas Puglieseea1af422013-12-09 14:15:14 -06001393 seg->isoc_frame_index = 0;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001394 if (result < 0) {
1395 pr_err("%s: xfer %p#%u: ISO packet descriptor submit failed: %d\n",
1396 __func__, xfer, seg->index, result);
Thomas Pugliese618836cc2014-02-28 14:31:55 -06001397 wa_xfer_put(xfer);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001398 goto error_iso_pack_desc_submit;
1399 }
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001400 }
1401 /* submit the out data if this is an out request. */
1402 if (seg->dto_urb) {
1403 struct wahc *wa = xfer->wa;
Thomas Pugliese618836cc2014-02-28 14:31:55 -06001404 wa_xfer_get(xfer);
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001405 result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
1406 if (result < 0) {
1407 pr_err("%s: xfer %p#%u: DTO submit failed: %d\n",
1408 __func__, xfer, seg->index, result);
Thomas Pugliese618836cc2014-02-28 14:31:55 -06001409 wa_xfer_put(xfer);
Thomas Pugliese226b3a22013-12-10 12:10:33 -06001410 goto error_dto_submit;
1411 }
Thomas Pugliese21012422013-10-23 14:44:27 -05001412 /*
1413 * If this segment contains more than one isoc frame, hold
1414 * onto the dto resource until we send all frames.
Thomas Pugliesef07ddb92013-10-23 14:44:28 -05001415 * Only applies to non-Alereon devices.
Thomas Pugliese21012422013-10-23 14:44:27 -05001416 */
Thomas Pugliesef07ddb92013-10-23 14:44:28 -05001417 if (((wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC) == 0)
1418 && (seg->isoc_frame_count > 1))
Thomas Pugliese21012422013-10-23 14:44:27 -05001419 *dto_done = 0;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001420 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001421 rpipe_avail_dec(rpipe);
1422 return 0;
1423
1424error_dto_submit:
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05001425 usb_unlink_urb(seg->isoc_pack_desc_urb);
1426error_iso_pack_desc_submit:
Thomas Pugliese09d94cb2013-09-26 10:49:40 -05001427 usb_unlink_urb(&seg->tr_urb);
Thomas Pugliese618836cc2014-02-28 14:31:55 -06001428error_tr_submit:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001429 seg->status = WA_SEG_ERROR;
1430 seg->result = result;
Thomas Pugliese21012422013-10-23 14:44:27 -05001431 *dto_done = 1;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001432 return result;
1433}
1434
1435/*
Thomas Pugliese679ee472013-10-07 10:53:57 -05001436 * Execute more queued request segments until the maximum concurrent allowed.
1437 * Return true if the DTO resource was acquired and released.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001438 *
1439 * The ugly unlock/lock sequence on the error path is needed as the
1440 * xfer->lock normally nests the seg_lock and not viceversa.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001441 */
Thomas Pugliese679ee472013-10-07 10:53:57 -05001442static int __wa_xfer_delayed_run(struct wa_rpipe *rpipe, int *dto_waiting)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001443{
Thomas Pugliese679ee472013-10-07 10:53:57 -05001444 int result, dto_acquired = 0, dto_done = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001445 struct device *dev = &rpipe->wa->usb_iface->dev;
1446 struct wa_seg *seg;
1447 struct wa_xfer *xfer;
1448 unsigned long flags;
1449
Thomas Pugliese679ee472013-10-07 10:53:57 -05001450 *dto_waiting = 0;
1451
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001452 spin_lock_irqsave(&rpipe->seg_lock, flags);
1453 while (atomic_read(&rpipe->segs_available) > 0
Thomas Pugliese679ee472013-10-07 10:53:57 -05001454 && !list_empty(&rpipe->seg_list)
1455 && (dto_acquired = __wa_dto_try_get(rpipe->wa))) {
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001456 seg = list_first_entry(&(rpipe->seg_list), struct wa_seg,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001457 list_node);
1458 list_del(&seg->list_node);
1459 xfer = seg->xfer;
Thomas Pugliese618836cc2014-02-28 14:31:55 -06001460 /*
1461 * Get a reference to the xfer in case the callbacks for the
1462 * URBs submitted by __wa_seg_submit attempt to complete
1463 * the xfer before this function completes.
1464 */
1465 wa_xfer_get(xfer);
Thomas Pugliese679ee472013-10-07 10:53:57 -05001466 result = __wa_seg_submit(rpipe, xfer, seg, &dto_done);
1467 /* release the dto resource if this RPIPE is done with it. */
1468 if (dto_done)
1469 __wa_dto_put(rpipe->wa);
Thomas Puglieseb9c84be2013-09-27 15:33:36 -05001470 dev_dbg(dev, "xfer %p ID %08X#%u submitted from delayed [%d segments available] %d\n",
1471 xfer, wa_xfer_id(xfer), seg->index,
1472 atomic_read(&rpipe->segs_available), result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001473 if (unlikely(result < 0)) {
1474 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
1475 spin_lock_irqsave(&xfer->lock, flags);
1476 __wa_xfer_abort(xfer);
Thomas Pugliese618836cc2014-02-28 14:31:55 -06001477 /*
1478 * This seg was marked as submitted when it was put on
1479 * the RPIPE seg_list. Mark it done.
1480 */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001481 xfer->segs_done++;
1482 spin_unlock_irqrestore(&xfer->lock, flags);
1483 spin_lock_irqsave(&rpipe->seg_lock, flags);
1484 }
Thomas Pugliese618836cc2014-02-28 14:31:55 -06001485 wa_xfer_put(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001486 }
Thomas Pugliese679ee472013-10-07 10:53:57 -05001487 /*
1488 * Mark this RPIPE as waiting if dto was not acquired, there are
1489 * delayed segs and no active transfers to wake us up later.
1490 */
1491 if (!dto_acquired && !list_empty(&rpipe->seg_list)
1492 && (atomic_read(&rpipe->segs_available) ==
1493 le16_to_cpu(rpipe->descr.wRequests)))
1494 *dto_waiting = 1;
1495
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001496 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
Thomas Pugliese679ee472013-10-07 10:53:57 -05001497
1498 return dto_done;
1499}
1500
1501static void wa_xfer_delayed_run(struct wa_rpipe *rpipe)
1502{
1503 int dto_waiting;
1504 int dto_done = __wa_xfer_delayed_run(rpipe, &dto_waiting);
1505
1506 /*
1507 * If this RPIPE is waiting on the DTO resource, add it to the tail of
1508 * the waiting list.
1509 * Otherwise, if the WA DTO resource was acquired and released by
1510 * __wa_xfer_delayed_run, another RPIPE may have attempted to acquire
1511 * DTO and failed during that time. Check the delayed list and process
1512 * any waiters. Start searching from the next RPIPE index.
1513 */
1514 if (dto_waiting)
1515 wa_add_delayed_rpipe(rpipe->wa, rpipe);
1516 else if (dto_done)
1517 wa_check_for_delayed_rpipes(rpipe->wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001518}
1519
1520/*
1521 *
1522 * xfer->lock is taken
1523 *
1524 * On failure submitting we just stop submitting and return error;
1525 * wa_urb_enqueue_b() will execute the completion path
1526 */
1527static int __wa_xfer_submit(struct wa_xfer *xfer)
1528{
Thomas Pugliese679ee472013-10-07 10:53:57 -05001529 int result, dto_acquired = 0, dto_done = 0, dto_waiting = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001530 struct wahc *wa = xfer->wa;
1531 struct device *dev = &wa->usb_iface->dev;
1532 unsigned cnt;
1533 struct wa_seg *seg;
1534 unsigned long flags;
1535 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
1536 size_t maxrequests = le16_to_cpu(rpipe->descr.wRequests);
1537 u8 available;
1538 u8 empty;
1539
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001540 spin_lock_irqsave(&wa->xfer_list_lock, flags);
1541 list_add_tail(&xfer->list_node, &wa->xfer_list);
1542 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
1543
1544 BUG_ON(atomic_read(&rpipe->segs_available) > maxrequests);
1545 result = 0;
1546 spin_lock_irqsave(&rpipe->seg_lock, flags);
1547 for (cnt = 0; cnt < xfer->segs; cnt++) {
Thomas Pugliese679ee472013-10-07 10:53:57 -05001548 int delay_seg = 1;
1549
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001550 available = atomic_read(&rpipe->segs_available);
1551 empty = list_empty(&rpipe->seg_list);
1552 seg = xfer->seg[cnt];
Thomas Pugliese679ee472013-10-07 10:53:57 -05001553 if (available && empty) {
1554 /*
1555 * Only attempt to acquire DTO if we have a segment
1556 * to send.
1557 */
1558 dto_acquired = __wa_dto_try_get(rpipe->wa);
1559 if (dto_acquired) {
1560 delay_seg = 0;
1561 result = __wa_seg_submit(rpipe, xfer, seg,
1562 &dto_done);
Thomas Pugliese21012422013-10-23 14:44:27 -05001563 dev_dbg(dev, "xfer %p ID 0x%08X#%u: available %u empty %u submitted\n",
1564 xfer, wa_xfer_id(xfer), cnt, available,
1565 empty);
Thomas Pugliese679ee472013-10-07 10:53:57 -05001566 if (dto_done)
1567 __wa_dto_put(rpipe->wa);
1568
1569 if (result < 0) {
1570 __wa_xfer_abort(xfer);
1571 goto error_seg_submit;
1572 }
1573 }
1574 }
1575
1576 if (delay_seg) {
Thomas Pugliese21012422013-10-23 14:44:27 -05001577 dev_dbg(dev, "xfer %p ID 0x%08X#%u: available %u empty %u delayed\n",
1578 xfer, wa_xfer_id(xfer), cnt, available, empty);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001579 seg->status = WA_SEG_DELAYED;
1580 list_add_tail(&seg->list_node, &rpipe->seg_list);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001581 }
1582 xfer->segs_submitted++;
1583 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001584error_seg_submit:
Thomas Pugliese679ee472013-10-07 10:53:57 -05001585 /*
1586 * Mark this RPIPE as waiting if dto was not acquired, there are
1587 * delayed segs and no active transfers to wake us up later.
1588 */
1589 if (!dto_acquired && !list_empty(&rpipe->seg_list)
1590 && (atomic_read(&rpipe->segs_available) ==
1591 le16_to_cpu(rpipe->descr.wRequests)))
1592 dto_waiting = 1;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001593 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
Thomas Pugliese679ee472013-10-07 10:53:57 -05001594
1595 if (dto_waiting)
1596 wa_add_delayed_rpipe(rpipe->wa, rpipe);
1597 else if (dto_done)
1598 wa_check_for_delayed_rpipes(rpipe->wa);
1599
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001600 return result;
1601}
1602
1603/*
1604 * Second part of a URB/transfer enqueuement
1605 *
1606 * Assumes this comes from wa_urb_enqueue() [maybe through
1607 * wa_urb_enqueue_run()]. At this point:
1608 *
1609 * xfer->wa filled and refcounted
1610 * xfer->ep filled with rpipe refcounted if
1611 * delayed == 0
1612 * xfer->urb filled and refcounted (this is the case when called
1613 * from wa_urb_enqueue() as we come from usb_submit_urb()
1614 * and when called by wa_urb_enqueue_run(), as we took an
1615 * extra ref dropped by _run() after we return).
1616 * xfer->gfp filled
1617 *
1618 * If we fail at __wa_xfer_submit(), then we just check if we are done
1619 * and if so, we run the completion procedure. However, if we are not
1620 * yet done, we do nothing and wait for the completion handlers from
1621 * the submitted URBs or from the xfer-result path to kick in. If xfer
1622 * result never kicks in, the xfer will timeout from the USB code and
1623 * dequeue() will be called.
1624 */
Thomas Pugliese33186c42013-10-01 10:14:56 -05001625static int wa_urb_enqueue_b(struct wa_xfer *xfer)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001626{
1627 int result;
1628 unsigned long flags;
1629 struct urb *urb = xfer->urb;
1630 struct wahc *wa = xfer->wa;
1631 struct wusbhc *wusbhc = wa->wusb;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001632 struct wusb_dev *wusb_dev;
1633 unsigned done;
1634
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001635 result = rpipe_get_by_ep(wa, xfer->ep, urb, xfer->gfp);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001636 if (result < 0) {
1637 pr_err("%s: error_rpipe_get\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001638 goto error_rpipe_get;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001639 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001640 result = -ENODEV;
1641 /* FIXME: segmentation broken -- kills DWA */
1642 mutex_lock(&wusbhc->mutex); /* get a WUSB dev */
Jiri Slaby49fa0922009-03-11 21:47:40 +01001643 if (urb->dev == NULL) {
1644 mutex_unlock(&wusbhc->mutex);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001645 pr_err("%s: error usb dev gone\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001646 goto error_dev_gone;
Jiri Slaby49fa0922009-03-11 21:47:40 +01001647 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001648 wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev);
1649 if (wusb_dev == NULL) {
1650 mutex_unlock(&wusbhc->mutex);
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001651 dev_err(&(urb->dev->dev), "%s: error wusb dev gone\n",
1652 __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001653 goto error_dev_gone;
1654 }
1655 mutex_unlock(&wusbhc->mutex);
1656
1657 spin_lock_irqsave(&xfer->lock, flags);
1658 xfer->wusb_dev = wusb_dev;
1659 result = urb->status;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001660 if (urb->status != -EINPROGRESS) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001661 dev_err(&(urb->dev->dev), "%s: error_dequeued\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001662 goto error_dequeued;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001663 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001664
1665 result = __wa_xfer_setup(xfer, urb);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001666 if (result < 0) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001667 dev_err(&(urb->dev->dev), "%s: error_xfer_setup\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001668 goto error_xfer_setup;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001669 }
Thomas Pugliese618836cc2014-02-28 14:31:55 -06001670 /*
1671 * Get a xfer reference since __wa_xfer_submit starts asynchronous
1672 * operations that may try to complete the xfer before this function
1673 * exits.
1674 */
1675 wa_xfer_get(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001676 result = __wa_xfer_submit(xfer);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001677 if (result < 0) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001678 dev_err(&(urb->dev->dev), "%s: error_xfer_submit\n", __func__);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001679 goto error_xfer_submit;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001680 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001681 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Pugliese618836cc2014-02-28 14:31:55 -06001682 wa_xfer_put(xfer);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001683 return 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001684
Thomas Pugliese33186c42013-10-01 10:14:56 -05001685 /*
1686 * this is basically wa_xfer_completion() broken up wa_xfer_giveback()
1687 * does a wa_xfer_put() that will call wa_xfer_destroy() and undo
1688 * setup().
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001689 */
1690error_xfer_setup:
1691error_dequeued:
1692 spin_unlock_irqrestore(&xfer->lock, flags);
1693 /* FIXME: segmentation broken, kills DWA */
1694 if (wusb_dev)
1695 wusb_dev_put(wusb_dev);
1696error_dev_gone:
1697 rpipe_put(xfer->ep->hcpriv);
1698error_rpipe_get:
1699 xfer->result = result;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001700 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001701
1702error_xfer_submit:
1703 done = __wa_xfer_is_done(xfer);
1704 xfer->result = result;
1705 spin_unlock_irqrestore(&xfer->lock, flags);
1706 if (done)
1707 wa_xfer_completion(xfer);
Thomas Pugliese618836cc2014-02-28 14:31:55 -06001708 wa_xfer_put(xfer);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001709 /* return success since the completion routine will run. */
1710 return 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001711}
1712
1713/*
1714 * Execute the delayed transfers in the Wire Adapter @wa
1715 *
1716 * We need to be careful here, as dequeue() could be called in the
1717 * middle. That's why we do the whole thing under the
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001718 * wa->xfer_list_lock. If dequeue() jumps in, it first locks xfer->lock
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001719 * and then checks the list -- so as we would be acquiring in inverse
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001720 * order, we move the delayed list to a separate list while locked and then
1721 * submit them without the list lock held.
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001722 */
1723void wa_urb_enqueue_run(struct work_struct *ws)
1724{
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001725 struct wahc *wa = container_of(ws, struct wahc, xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001726 struct wa_xfer *xfer, *next;
1727 struct urb *urb;
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001728 LIST_HEAD(tmp_list);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001729
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001730 /* Create a copy of the wa->xfer_delayed_list while holding the lock */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001731 spin_lock_irq(&wa->xfer_list_lock);
Thomas Pugliesee9a088f2013-08-12 10:10:53 -05001732 list_cut_position(&tmp_list, &wa->xfer_delayed_list,
1733 wa->xfer_delayed_list.prev);
1734 spin_unlock_irq(&wa->xfer_list_lock);
1735
1736 /*
1737 * enqueue from temp list without list lock held since wa_urb_enqueue_b
1738 * can take xfer->lock as well as lock mutexes.
1739 */
1740 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001741 list_del_init(&xfer->list_node);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001742
1743 urb = xfer->urb;
Thomas Pugliese33186c42013-10-01 10:14:56 -05001744 if (wa_urb_enqueue_b(xfer) < 0)
1745 wa_xfer_giveback(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001746 usb_put_urb(urb); /* taken when queuing */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001747 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001748}
1749EXPORT_SYMBOL_GPL(wa_urb_enqueue_run);
1750
1751/*
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001752 * Process the errored transfers on the Wire Adapter outside of interrupt.
1753 */
1754void wa_process_errored_transfers_run(struct work_struct *ws)
1755{
1756 struct wahc *wa = container_of(ws, struct wahc, xfer_error_work);
1757 struct wa_xfer *xfer, *next;
1758 LIST_HEAD(tmp_list);
1759
1760 pr_info("%s: Run delayed STALL processing.\n", __func__);
1761
1762 /* Create a copy of the wa->xfer_errored_list while holding the lock */
1763 spin_lock_irq(&wa->xfer_list_lock);
1764 list_cut_position(&tmp_list, &wa->xfer_errored_list,
1765 wa->xfer_errored_list.prev);
1766 spin_unlock_irq(&wa->xfer_list_lock);
1767
1768 /*
1769 * run rpipe_clear_feature_stalled from temp list without list lock
1770 * held.
1771 */
1772 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
1773 struct usb_host_endpoint *ep;
1774 unsigned long flags;
1775 struct wa_rpipe *rpipe;
1776
1777 spin_lock_irqsave(&xfer->lock, flags);
1778 ep = xfer->ep;
1779 rpipe = ep->hcpriv;
1780 spin_unlock_irqrestore(&xfer->lock, flags);
1781
1782 /* clear RPIPE feature stalled without holding a lock. */
1783 rpipe_clear_feature_stalled(wa, ep);
1784
1785 /* complete the xfer. This removes it from the tmp list. */
1786 wa_xfer_completion(xfer);
1787
1788 /* check for work. */
1789 wa_xfer_delayed_run(rpipe);
1790 }
1791}
1792EXPORT_SYMBOL_GPL(wa_process_errored_transfers_run);
1793
1794/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001795 * Submit a transfer to the Wire Adapter in a delayed way
1796 *
1797 * The process of enqueuing involves possible sleeps() [see
1798 * enqueue_b(), for the rpipe_get() and the mutex_lock()]. If we are
1799 * in an atomic section, we defer the enqueue_b() call--else we call direct.
1800 *
1801 * @urb: We own a reference to it done by the HCI Linux USB stack that
1802 * will be given up by calling usb_hcd_giveback_urb() or by
1803 * returning error from this function -> ergo we don't have to
1804 * refcount it.
1805 */
1806int wa_urb_enqueue(struct wahc *wa, struct usb_host_endpoint *ep,
1807 struct urb *urb, gfp_t gfp)
1808{
1809 int result;
1810 struct device *dev = &wa->usb_iface->dev;
1811 struct wa_xfer *xfer;
1812 unsigned long my_flags;
1813 unsigned cant_sleep = irqs_disabled() | in_atomic();
1814
Thomas Pugliese2b81c082013-06-11 10:39:31 -05001815 if ((urb->transfer_buffer == NULL)
1816 && (urb->sg == NULL)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001817 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1818 && urb->transfer_buffer_length != 0) {
1819 dev_err(dev, "BUG? urb %p: NULL xfer buffer & NODMA\n", urb);
1820 dump_stack();
1821 }
1822
Thomas Puglieseb3744872013-11-25 16:17:16 -06001823 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1824 result = usb_hcd_link_urb_to_ep(&(wa->wusb->usb_hcd), urb);
1825 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
1826 if (result < 0)
1827 goto error_link_urb;
1828
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001829 result = -ENOMEM;
1830 xfer = kzalloc(sizeof(*xfer), gfp);
1831 if (xfer == NULL)
1832 goto error_kmalloc;
1833
1834 result = -ENOENT;
1835 if (urb->status != -EINPROGRESS) /* cancelled */
1836 goto error_dequeued; /* before starting? */
1837 wa_xfer_init(xfer);
1838 xfer->wa = wa_get(wa);
1839 xfer->urb = urb;
1840 xfer->gfp = gfp;
1841 xfer->ep = ep;
1842 urb->hcpriv = xfer;
David Vrabelbce83692008-12-22 18:22:50 +00001843
1844 dev_dbg(dev, "xfer %p urb %p pipe 0x%02x [%d bytes] %s %s %s\n",
1845 xfer, urb, urb->pipe, urb->transfer_buffer_length,
1846 urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? "dma" : "nodma",
1847 urb->pipe & USB_DIR_IN ? "inbound" : "outbound",
1848 cant_sleep ? "deferred" : "inline");
1849
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001850 if (cant_sleep) {
1851 usb_get_urb(urb);
1852 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1853 list_add_tail(&xfer->list_node, &wa->xfer_delayed_list);
1854 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001855 queue_work(wusbd, &wa->xfer_enqueue_work);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001856 } else {
Thomas Pugliese33186c42013-10-01 10:14:56 -05001857 result = wa_urb_enqueue_b(xfer);
1858 if (result < 0) {
1859 /*
1860 * URB submit/enqueue failed. Clean up, return an
1861 * error and do not run the callback. This avoids
1862 * an infinite submit/complete loop.
1863 */
1864 dev_err(dev, "%s: URB enqueue failed: %d\n",
1865 __func__, result);
1866 wa_put(xfer->wa);
1867 wa_xfer_put(xfer);
Thomas Puglieseb3744872013-11-25 16:17:16 -06001868 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1869 usb_hcd_unlink_urb_from_ep(&(wa->wusb->usb_hcd), urb);
1870 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
Thomas Pugliese33186c42013-10-01 10:14:56 -05001871 return result;
1872 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001873 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001874 return 0;
1875
1876error_dequeued:
1877 kfree(xfer);
1878error_kmalloc:
Thomas Puglieseb3744872013-11-25 16:17:16 -06001879 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1880 usb_hcd_unlink_urb_from_ep(&(wa->wusb->usb_hcd), urb);
1881 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
1882error_link_urb:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001883 return result;
1884}
1885EXPORT_SYMBOL_GPL(wa_urb_enqueue);
1886
1887/*
1888 * Dequeue a URB and make sure uwb_hcd_giveback_urb() [completion
1889 * handler] is called.
1890 *
1891 * Until a transfer goes successfully through wa_urb_enqueue() it
1892 * needs to be dequeued with completion calling; when stuck in delayed
1893 * or before wa_xfer_setup() is called, we need to do completion.
1894 *
1895 * not setup If there is no hcpriv yet, that means that that enqueue
1896 * still had no time to set the xfer up. Because
1897 * urb->status should be other than -EINPROGRESS,
1898 * enqueue() will catch that and bail out.
1899 *
1900 * If the transfer has gone through setup, we just need to clean it
1901 * up. If it has gone through submit(), we have to abort it [with an
1902 * asynch request] and then make sure we cancel each segment.
1903 *
1904 */
Thomas Puglieseb3744872013-11-25 16:17:16 -06001905int wa_urb_dequeue(struct wahc *wa, struct urb *urb, int status)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001906{
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001907 unsigned long flags, flags2;
1908 struct wa_xfer *xfer;
1909 struct wa_seg *seg;
1910 struct wa_rpipe *rpipe;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001911 unsigned cnt, done = 0, xfer_abort_pending;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001912 unsigned rpipe_ready = 0;
Thomas Puglieseb3744872013-11-25 16:17:16 -06001913 int result;
1914
1915 /* check if it is safe to unlink. */
1916 spin_lock_irqsave(&wa->xfer_list_lock, flags);
1917 result = usb_hcd_check_unlink_urb(&(wa->wusb->usb_hcd), urb, status);
1918 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
1919 if (result)
1920 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001921
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001922 xfer = urb->hcpriv;
1923 if (xfer == NULL) {
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05001924 /*
1925 * Nothing setup yet enqueue will see urb->status !=
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001926 * -EINPROGRESS (by hcd layer) and bail out with
1927 * error, no need to do completion
1928 */
1929 BUG_ON(urb->status == -EINPROGRESS);
1930 goto out;
1931 }
1932 spin_lock_irqsave(&xfer->lock, flags);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001933 pr_debug("%s: DEQUEUE xfer id 0x%08X\n", __func__, wa_xfer_id(xfer));
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001934 rpipe = xfer->ep->hcpriv;
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001935 if (rpipe == NULL) {
Thomas Pugliesebbfc34202013-11-25 16:17:17 -06001936 pr_debug("%s: xfer %p id 0x%08X has no RPIPE. %s",
1937 __func__, xfer, wa_xfer_id(xfer),
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001938 "Probably already aborted.\n" );
Thomas Pugliesee05a1fd2013-11-25 16:17:18 -06001939 result = -ENOENT;
Thomas Puglieseec58fad2013-08-09 09:52:13 -05001940 goto out_unlock;
1941 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001942 /* Check the delayed list -> if there, release and complete */
1943 spin_lock_irqsave(&wa->xfer_list_lock, flags2);
1944 if (!list_empty(&xfer->list_node) && xfer->seg == NULL)
1945 goto dequeue_delayed;
1946 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1947 if (xfer->seg == NULL) /* still hasn't reached */
1948 goto out_unlock; /* setup(), enqueue_b() completes */
1949 /* Ok, the xfer is in flight already, it's been setup and submitted.*/
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001950 xfer_abort_pending = __wa_xfer_abort(xfer) >= 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001951 for (cnt = 0; cnt < xfer->segs; cnt++) {
1952 seg = xfer->seg[cnt];
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001953 pr_debug("%s: xfer id 0x%08X#%d status = %d\n",
1954 __func__, wa_xfer_id(xfer), cnt, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001955 switch (seg->status) {
1956 case WA_SEG_NOTREADY:
1957 case WA_SEG_READY:
1958 printk(KERN_ERR "xfer %p#%u: dequeue bad state %u\n",
1959 xfer, cnt, seg->status);
1960 WARN_ON(1);
1961 break;
1962 case WA_SEG_DELAYED:
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001963 /*
1964 * delete from rpipe delayed list. If no segments on
1965 * this xfer have been submitted, __wa_xfer_is_done will
1966 * trigger a giveback below. Otherwise, the submitted
1967 * segments will be completed in the DTI interrupt.
1968 */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001969 seg->status = WA_SEG_ABORTED;
Thomas Pugliesee05a1fd2013-11-25 16:17:18 -06001970 seg->result = -ENOENT;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001971 spin_lock_irqsave(&rpipe->seg_lock, flags2);
1972 list_del(&seg->list_node);
1973 xfer->segs_done++;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001974 spin_unlock_irqrestore(&rpipe->seg_lock, flags2);
1975 break;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01001976 case WA_SEG_DONE:
1977 case WA_SEG_ERROR:
1978 case WA_SEG_ABORTED:
1979 break;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05001980 /*
1981 * In the states below, the HWA device already knows
1982 * about the transfer. If an abort request was sent,
1983 * allow the HWA to process it and wait for the
1984 * results. Otherwise, the DTI state and seg completed
1985 * counts can get out of sync.
1986 */
1987 case WA_SEG_SUBMITTED:
1988 case WA_SEG_PENDING:
1989 case WA_SEG_DTI_PENDING:
1990 /*
1991 * Check if the abort was successfully sent. This could
1992 * be false if the HWA has been removed but we haven't
1993 * gotten the disconnect notification yet.
1994 */
1995 if (!xfer_abort_pending) {
1996 seg->status = WA_SEG_ABORTED;
1997 rpipe_ready = rpipe_avail_inc(rpipe);
1998 xfer->segs_done++;
1999 }
2000 break;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002001 }
2002 }
2003 xfer->result = urb->status; /* -ENOENT or -ECONNRESET */
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002004 done = __wa_xfer_is_done(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002005 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002006 if (done)
2007 wa_xfer_completion(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002008 if (rpipe_ready)
2009 wa_xfer_delayed_run(rpipe);
Thomas Pugliesee05a1fd2013-11-25 16:17:18 -06002010 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002011
2012out_unlock:
2013 spin_unlock_irqrestore(&xfer->lock, flags);
2014out:
Thomas Pugliesee05a1fd2013-11-25 16:17:18 -06002015 return result;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002016
2017dequeue_delayed:
2018 list_del_init(&xfer->list_node);
2019 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
2020 xfer->result = urb->status;
2021 spin_unlock_irqrestore(&xfer->lock, flags);
2022 wa_xfer_giveback(xfer);
2023 usb_put_urb(urb); /* we got a ref in enqueue() */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002024 return 0;
2025}
2026EXPORT_SYMBOL_GPL(wa_urb_dequeue);
2027
2028/*
2029 * Translation from WA status codes (WUSB1.0 Table 8.15) to errno
2030 * codes
2031 *
2032 * Positive errno values are internal inconsistencies and should be
2033 * flagged louder. Negative are to be passed up to the user in the
2034 * normal way.
2035 *
2036 * @status: USB WA status code -- high two bits are stripped.
2037 */
2038static int wa_xfer_status_to_errno(u8 status)
2039{
2040 int errno;
2041 u8 real_status = status;
2042 static int xlat[] = {
2043 [WA_XFER_STATUS_SUCCESS] = 0,
2044 [WA_XFER_STATUS_HALTED] = -EPIPE,
2045 [WA_XFER_STATUS_DATA_BUFFER_ERROR] = -ENOBUFS,
2046 [WA_XFER_STATUS_BABBLE] = -EOVERFLOW,
2047 [WA_XFER_RESERVED] = EINVAL,
2048 [WA_XFER_STATUS_NOT_FOUND] = 0,
2049 [WA_XFER_STATUS_INSUFFICIENT_RESOURCE] = -ENOMEM,
2050 [WA_XFER_STATUS_TRANSACTION_ERROR] = -EILSEQ,
Thomas Pugliesee05a1fd2013-11-25 16:17:18 -06002051 [WA_XFER_STATUS_ABORTED] = -ENOENT,
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002052 [WA_XFER_STATUS_RPIPE_NOT_READY] = EINVAL,
2053 [WA_XFER_INVALID_FORMAT] = EINVAL,
2054 [WA_XFER_UNEXPECTED_SEGMENT_NUMBER] = EINVAL,
2055 [WA_XFER_STATUS_RPIPE_TYPE_MISMATCH] = EINVAL,
2056 };
2057 status &= 0x3f;
2058
2059 if (status == 0)
2060 return 0;
2061 if (status >= ARRAY_SIZE(xlat)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02002062 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002063 "Unknown WA transfer status 0x%02x\n",
2064 __func__, real_status);
2065 return -EINVAL;
2066 }
2067 errno = xlat[status];
2068 if (unlikely(errno > 0)) {
Manuel Zerpies9708cd22011-06-16 14:15:16 +02002069 printk_ratelimited(KERN_ERR "%s(): BUG? "
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002070 "Inconsistent WA status: 0x%02x\n",
2071 __func__, real_status);
2072 errno = -errno;
2073 }
2074 return errno;
2075}
2076
2077/*
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002078 * If a last segment flag and/or a transfer result error is encountered,
2079 * no other segment transfer results will be returned from the device.
2080 * Mark the remaining submitted or pending xfers as completed so that
2081 * the xfer will complete cleanly.
Thomas Puglieseacfadce2014-02-28 14:31:56 -06002082 *
2083 * xfer->lock must be held
2084 *
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002085 */
2086static void wa_complete_remaining_xfer_segs(struct wa_xfer *xfer,
Thomas Puglieseacfadce2014-02-28 14:31:56 -06002087 int starting_index, enum wa_seg_status status)
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002088{
2089 int index;
2090 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
2091
Thomas Puglieseacfadce2014-02-28 14:31:56 -06002092 for (index = starting_index; index < xfer->segs_submitted; index++) {
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002093 struct wa_seg *current_seg = xfer->seg[index];
2094
2095 BUG_ON(current_seg == NULL);
2096
2097 switch (current_seg->status) {
2098 case WA_SEG_SUBMITTED:
2099 case WA_SEG_PENDING:
2100 case WA_SEG_DTI_PENDING:
2101 rpipe_avail_inc(rpipe);
2102 /*
2103 * do not increment RPIPE avail for the WA_SEG_DELAYED case
2104 * since it has not been submitted to the RPIPE.
2105 */
2106 case WA_SEG_DELAYED:
2107 xfer->segs_done++;
Thomas Pugliese70052342013-12-09 13:10:41 -06002108 current_seg->status = status;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002109 break;
2110 case WA_SEG_ABORTED:
2111 break;
2112 default:
2113 WARN(1, "%s: xfer 0x%08X#%d. bad seg status = %d\n",
2114 __func__, wa_xfer_id(xfer), index,
2115 current_seg->status);
2116 break;
2117 }
2118 }
2119}
2120
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002121/* Populate the wa->buf_in_urb based on the current isoc transfer state. */
2122static void __wa_populate_buf_in_urb_isoc(struct wahc *wa, struct wa_xfer *xfer,
2123 struct wa_seg *seg, int curr_iso_frame)
2124{
2125 BUG_ON(wa->buf_in_urb->status == -EINPROGRESS);
2126
2127 /* this should always be 0 before a resubmit. */
2128 wa->buf_in_urb->num_mapped_sgs = 0;
2129 wa->buf_in_urb->transfer_dma = xfer->urb->transfer_dma +
2130 xfer->urb->iso_frame_desc[curr_iso_frame].offset;
2131 wa->buf_in_urb->transfer_buffer_length =
2132 xfer->urb->iso_frame_desc[curr_iso_frame].length;
2133 wa->buf_in_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2134 wa->buf_in_urb->transfer_buffer = NULL;
2135 wa->buf_in_urb->sg = NULL;
2136 wa->buf_in_urb->num_sgs = 0;
2137 wa->buf_in_urb->context = seg;
2138}
2139
Thomas Pugliese70052342013-12-09 13:10:41 -06002140/* Populate the wa->buf_in_urb based on the current transfer state. */
2141static int wa_populate_buf_in_urb(struct wahc *wa, struct wa_xfer *xfer,
2142 unsigned int seg_idx, unsigned int bytes_transferred)
2143{
2144 int result = 0;
2145 struct wa_seg *seg = xfer->seg[seg_idx];
2146
2147 BUG_ON(wa->buf_in_urb->status == -EINPROGRESS);
2148 /* this should always be 0 before a resubmit. */
2149 wa->buf_in_urb->num_mapped_sgs = 0;
2150
2151 if (xfer->is_dma) {
2152 wa->buf_in_urb->transfer_dma = xfer->urb->transfer_dma
2153 + (seg_idx * xfer->seg_size);
2154 wa->buf_in_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2155 wa->buf_in_urb->transfer_buffer = NULL;
2156 wa->buf_in_urb->sg = NULL;
2157 wa->buf_in_urb->num_sgs = 0;
2158 } else {
2159 /* do buffer or SG processing. */
2160 wa->buf_in_urb->transfer_flags &= ~URB_NO_TRANSFER_DMA_MAP;
2161
2162 if (xfer->urb->transfer_buffer) {
2163 wa->buf_in_urb->transfer_buffer =
2164 xfer->urb->transfer_buffer
2165 + (seg_idx * xfer->seg_size);
2166 wa->buf_in_urb->sg = NULL;
2167 wa->buf_in_urb->num_sgs = 0;
2168 } else {
2169 /* allocate an SG list to store seg_size bytes
2170 and copy the subset of the xfer->urb->sg
2171 that matches the buffer subset we are
2172 about to read. */
2173 wa->buf_in_urb->sg = wa_xfer_create_subset_sg(
2174 xfer->urb->sg,
2175 seg_idx * xfer->seg_size,
2176 bytes_transferred,
2177 &(wa->buf_in_urb->num_sgs));
2178
2179 if (!(wa->buf_in_urb->sg)) {
2180 wa->buf_in_urb->num_sgs = 0;
2181 result = -ENOMEM;
2182 }
2183 wa->buf_in_urb->transfer_buffer = NULL;
2184 }
2185 }
2186 wa->buf_in_urb->transfer_buffer_length = bytes_transferred;
2187 wa->buf_in_urb->context = seg;
2188
2189 return result;
2190}
2191
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002192/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002193 * Process a xfer result completion message
2194 *
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002195 * inbound transfers: need to schedule a buf_in_urb read
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002196 *
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002197 * FIXME: this function needs to be broken up in parts
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002198 */
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002199static void wa_xfer_result_chew(struct wahc *wa, struct wa_xfer *xfer,
2200 struct wa_xfer_result *xfer_result)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002201{
2202 int result;
2203 struct device *dev = &wa->usb_iface->dev;
2204 unsigned long flags;
Thomas Pugliese70052342013-12-09 13:10:41 -06002205 unsigned int seg_idx;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002206 struct wa_seg *seg;
2207 struct wa_rpipe *rpipe;
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002208 unsigned done = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002209 u8 usb_status;
2210 unsigned rpipe_ready = 0;
Thomas Pugliese70052342013-12-09 13:10:41 -06002211 unsigned bytes_transferred = le32_to_cpu(xfer_result->dwTransferLength);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002212
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002213 spin_lock_irqsave(&xfer->lock, flags);
2214 seg_idx = xfer_result->bTransferSegment & 0x7f;
2215 if (unlikely(seg_idx >= xfer->segs))
2216 goto error_bad_seg;
2217 seg = xfer->seg[seg_idx];
2218 rpipe = xfer->ep->hcpriv;
2219 usb_status = xfer_result->bTransferStatus;
Thomas Puglieseb9c84be2013-09-27 15:33:36 -05002220 dev_dbg(dev, "xfer %p ID 0x%08X#%u: bTransferStatus 0x%02x (seg status %u)\n",
2221 xfer, wa_xfer_id(xfer), seg_idx, usb_status, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002222 if (seg->status == WA_SEG_ABORTED
2223 || seg->status == WA_SEG_ERROR) /* already handled */
2224 goto segment_aborted;
2225 if (seg->status == WA_SEG_SUBMITTED) /* ops, got here */
2226 seg->status = WA_SEG_PENDING; /* before wa_seg{_dto}_cb() */
2227 if (seg->status != WA_SEG_PENDING) {
2228 if (printk_ratelimit())
2229 dev_err(dev, "xfer %p#%u: Bad segment state %u\n",
2230 xfer, seg_idx, seg->status);
2231 seg->status = WA_SEG_PENDING; /* workaround/"fix" it */
2232 }
2233 if (usb_status & 0x80) {
2234 seg->result = wa_xfer_status_to_errno(usb_status);
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002235 dev_err(dev, "DTI: xfer %p#:%08X:%u failed (0x%02x)\n",
2236 xfer, xfer->id, seg->index, usb_status);
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002237 seg->status = ((usb_status & 0x7F) == WA_XFER_STATUS_ABORTED) ?
2238 WA_SEG_ABORTED : WA_SEG_ERROR;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002239 goto error_complete;
2240 }
2241 /* FIXME: we ignore warnings, tally them for stats */
2242 if (usb_status & 0x40) /* Warning?... */
2243 usb_status = 0; /* ... pass */
Thomas Pugliese70052342013-12-09 13:10:41 -06002244 /*
2245 * If the last segment bit is set, complete the remaining segments.
2246 * When the current segment is completed, either in wa_buf_in_cb for
2247 * transfers with data or below for no data, the xfer will complete.
2248 */
2249 if (xfer_result->bTransferSegment & 0x80)
Thomas Puglieseacfadce2014-02-28 14:31:56 -06002250 wa_complete_remaining_xfer_segs(xfer, seg->index + 1,
2251 WA_SEG_DONE);
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002252 if (usb_pipeisoc(xfer->urb->pipe)
2253 && (le32_to_cpu(xfer_result->dwNumOfPackets) > 0)) {
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002254 /* set up WA state to read the isoc packet status next. */
2255 wa->dti_isoc_xfer_in_progress = wa_xfer_id(xfer);
2256 wa->dti_isoc_xfer_seg = seg_idx;
2257 wa->dti_state = WA_DTI_ISOC_PACKET_STATUS_PENDING;
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002258 } else if (xfer->is_inbound && !usb_pipeisoc(xfer->urb->pipe)
Thomas Pugliese70052342013-12-09 13:10:41 -06002259 && (bytes_transferred > 0)) {
2260 /* IN data phase: read to buffer */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002261 seg->status = WA_SEG_DTI_PENDING;
Thomas Pugliese70052342013-12-09 13:10:41 -06002262 result = wa_populate_buf_in_urb(wa, xfer, seg_idx,
2263 bytes_transferred);
2264 if (result < 0)
2265 goto error_buf_in_populate;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002266 result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
2267 if (result < 0)
2268 goto error_submit_buf_in;
2269 } else {
Thomas Pugliese70052342013-12-09 13:10:41 -06002270 /* OUT data phase or no data, complete it -- */
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002271 seg->status = WA_SEG_DONE;
Thomas Pugliese70052342013-12-09 13:10:41 -06002272 seg->result = bytes_transferred;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002273 xfer->segs_done++;
2274 rpipe_ready = rpipe_avail_inc(rpipe);
2275 done = __wa_xfer_is_done(xfer);
2276 }
2277 spin_unlock_irqrestore(&xfer->lock, flags);
2278 if (done)
2279 wa_xfer_completion(xfer);
2280 if (rpipe_ready)
2281 wa_xfer_delayed_run(rpipe);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002282 return;
2283
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002284error_submit_buf_in:
2285 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
2286 dev_err(dev, "DTI: URB max acceptable errors "
2287 "exceeded, resetting device\n");
2288 wa_reset_all(wa);
2289 }
2290 if (printk_ratelimit())
2291 dev_err(dev, "xfer %p#%u: can't submit DTI data phase: %d\n",
2292 xfer, seg_idx, result);
2293 seg->result = result;
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002294 kfree(wa->buf_in_urb->sg);
Thomas Pugliese67414482013-09-26 14:08:16 -05002295 wa->buf_in_urb->sg = NULL;
Thomas Pugliese70052342013-12-09 13:10:41 -06002296error_buf_in_populate:
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002297 __wa_xfer_abort(xfer);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002298 seg->status = WA_SEG_ERROR;
Thomas Pugliese14e1d2d2013-09-30 15:58:24 -05002299error_complete:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002300 xfer->segs_done++;
2301 rpipe_ready = rpipe_avail_inc(rpipe);
Thomas Puglieseacfadce2014-02-28 14:31:56 -06002302 wa_complete_remaining_xfer_segs(xfer, seg->index + 1, seg->status);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002303 done = __wa_xfer_is_done(xfer);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002304 /*
2305 * queue work item to clear STALL for control endpoints.
2306 * Otherwise, let endpoint_reset take care of it.
2307 */
2308 if (((usb_status & 0x3f) == WA_XFER_STATUS_HALTED) &&
2309 usb_endpoint_xfer_control(&xfer->ep->desc) &&
2310 done) {
2311
2312 dev_info(dev, "Control EP stall. Queue delayed work.\n");
2313 spin_lock_irq(&wa->xfer_list_lock);
Wei Yongjun8eb41292013-09-23 14:16:22 +08002314 /* move xfer from xfer_list to xfer_errored_list. */
2315 list_move_tail(&xfer->list_node, &wa->xfer_errored_list);
Thomas Pugliese6d33f7b2013-08-15 12:21:30 -05002316 spin_unlock_irq(&wa->xfer_list_lock);
2317 spin_unlock_irqrestore(&xfer->lock, flags);
2318 queue_work(wusbd, &wa->xfer_error_work);
2319 } else {
2320 spin_unlock_irqrestore(&xfer->lock, flags);
2321 if (done)
2322 wa_xfer_completion(xfer);
2323 if (rpipe_ready)
2324 wa_xfer_delayed_run(rpipe);
2325 }
2326
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002327 return;
2328
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002329error_bad_seg:
2330 spin_unlock_irqrestore(&xfer->lock, flags);
Thomas Puglieseb3744872013-11-25 16:17:16 -06002331 wa_urb_dequeue(wa, xfer->urb, -ENOENT);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002332 if (printk_ratelimit())
2333 dev_err(dev, "xfer %p#%u: bad segment\n", xfer, seg_idx);
2334 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
2335 dev_err(dev, "DTI: URB max acceptable errors "
2336 "exceeded, resetting device\n");
2337 wa_reset_all(wa);
2338 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002339 return;
2340
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002341segment_aborted:
2342 /* nothing to do, as the aborter did the completion */
2343 spin_unlock_irqrestore(&xfer->lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002344}
2345
2346/*
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002347 * Process a isochronous packet status message
2348 *
2349 * inbound transfers: need to schedule a buf_in_urb read
2350 */
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002351static int wa_process_iso_packet_status(struct wahc *wa, struct urb *urb)
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002352{
2353 struct device *dev = &wa->usb_iface->dev;
2354 struct wa_xfer_packet_status_hwaiso *packet_status;
Thomas Pugliese21012422013-10-23 14:44:27 -05002355 struct wa_xfer_packet_status_len_hwaiso *status_array;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002356 struct wa_xfer *xfer;
2357 unsigned long flags;
2358 struct wa_seg *seg;
2359 struct wa_rpipe *rpipe;
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002360 unsigned done = 0, dti_busy = 0, data_frame_count = 0, seg_index;
2361 unsigned first_frame_index = 0, rpipe_ready = 0;
Thomas Pugliese21012422013-10-23 14:44:27 -05002362 int expected_size;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002363
2364 /* We have a xfer result buffer; check it */
2365 dev_dbg(dev, "DTI: isoc packet status %d bytes at %p\n",
2366 urb->actual_length, urb->transfer_buffer);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002367 packet_status = (struct wa_xfer_packet_status_hwaiso *)(wa->dti_buf);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002368 if (packet_status->bPacketType != WA_XFER_ISO_PACKET_STATUS) {
2369 dev_err(dev, "DTI Error: isoc packet status--bad type 0x%02x\n",
2370 packet_status->bPacketType);
2371 goto error_parse_buffer;
2372 }
2373 xfer = wa_xfer_get_by_id(wa, wa->dti_isoc_xfer_in_progress);
2374 if (xfer == NULL) {
2375 dev_err(dev, "DTI Error: isoc packet status--unknown xfer 0x%08x\n",
2376 wa->dti_isoc_xfer_in_progress);
2377 goto error_parse_buffer;
2378 }
2379 spin_lock_irqsave(&xfer->lock, flags);
2380 if (unlikely(wa->dti_isoc_xfer_seg >= xfer->segs))
2381 goto error_bad_seg;
2382 seg = xfer->seg[wa->dti_isoc_xfer_seg];
2383 rpipe = xfer->ep->hcpriv;
Thomas Pugliese21012422013-10-23 14:44:27 -05002384 expected_size = sizeof(*packet_status) +
2385 (sizeof(packet_status->PacketStatus[0]) *
2386 seg->isoc_frame_count);
2387 if (urb->actual_length != expected_size) {
2388 dev_err(dev, "DTI Error: isoc packet status--bad urb length (%d bytes vs %d needed)\n",
2389 urb->actual_length, expected_size);
2390 goto error_bad_seg;
2391 }
2392 if (le16_to_cpu(packet_status->wLength) != expected_size) {
2393 dev_err(dev, "DTI Error: isoc packet status--bad length %u\n",
2394 le16_to_cpu(packet_status->wLength));
2395 goto error_bad_seg;
2396 }
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002397 /* write isoc packet status and lengths back to the xfer urb. */
Thomas Pugliese21012422013-10-23 14:44:27 -05002398 status_array = packet_status->PacketStatus;
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002399 xfer->urb->start_frame =
2400 wa->wusb->usb_hcd.driver->get_frame_number(&wa->wusb->usb_hcd);
Thomas Pugliese21012422013-10-23 14:44:27 -05002401 for (seg_index = 0; seg_index < seg->isoc_frame_count; ++seg_index) {
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002402 struct usb_iso_packet_descriptor *iso_frame_desc =
2403 xfer->urb->iso_frame_desc;
2404 const int urb_frame_index =
2405 seg->isoc_frame_offset + seg_index;
2406
2407 iso_frame_desc[urb_frame_index].status =
Thomas Pugliese21012422013-10-23 14:44:27 -05002408 wa_xfer_status_to_errno(
2409 le16_to_cpu(status_array[seg_index].PacketStatus));
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002410 iso_frame_desc[urb_frame_index].actual_length =
Thomas Pugliese21012422013-10-23 14:44:27 -05002411 le16_to_cpu(status_array[seg_index].PacketLength);
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002412 /* track the number of frames successfully transferred. */
2413 if (iso_frame_desc[urb_frame_index].actual_length > 0) {
2414 /* save the starting frame index for buf_in_urb. */
2415 if (!data_frame_count)
2416 first_frame_index = seg_index;
2417 ++data_frame_count;
2418 }
Thomas Pugliese21012422013-10-23 14:44:27 -05002419 }
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002420
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002421 if (xfer->is_inbound && data_frame_count) {
2422 int result;
2423
2424 seg->isoc_frame_index = first_frame_index;
2425 /* submit a read URB for the first frame with data. */
2426 __wa_populate_buf_in_urb_isoc(wa, xfer, seg,
2427 seg->isoc_frame_index + seg->isoc_frame_offset);
2428
2429 result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
2430 if (result < 0) {
2431 dev_err(dev, "DTI Error: Could not submit buf in URB (%d)",
2432 result);
2433 wa_reset_all(wa);
2434 } else if (data_frame_count > 1)
2435 /* If we need to read multiple frames, set DTI busy. */
2436 dti_busy = 1;
2437 } else {
2438 /* OUT transfer or no more IN data, complete it -- */
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002439 seg->status = WA_SEG_DONE;
2440 xfer->segs_done++;
2441 rpipe_ready = rpipe_avail_inc(rpipe);
2442 done = __wa_xfer_is_done(xfer);
2443 }
2444 spin_unlock_irqrestore(&xfer->lock, flags);
2445 wa->dti_state = WA_DTI_TRANSFER_RESULT_PENDING;
2446 if (done)
2447 wa_xfer_completion(xfer);
2448 if (rpipe_ready)
2449 wa_xfer_delayed_run(rpipe);
2450 wa_xfer_put(xfer);
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002451 return dti_busy;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002452
2453error_bad_seg:
2454 spin_unlock_irqrestore(&xfer->lock, flags);
2455 wa_xfer_put(xfer);
2456error_parse_buffer:
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002457 return dti_busy;
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002458}
2459
2460/*
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002461 * Callback for the IN data phase
2462 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002463 * If successful transition state; otherwise, take a note of the
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002464 * error, mark this segment done and try completion.
2465 *
2466 * Note we don't access until we are sure that the transfer hasn't
2467 * been cancelled (ECONNRESET, ENOENT), which could mean that
2468 * seg->xfer could be already gone.
2469 */
2470static void wa_buf_in_cb(struct urb *urb)
2471{
2472 struct wa_seg *seg = urb->context;
2473 struct wa_xfer *xfer = seg->xfer;
2474 struct wahc *wa;
2475 struct device *dev;
2476 struct wa_rpipe *rpipe;
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002477 unsigned rpipe_ready = 0, seg_index, isoc_data_frame_count = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002478 unsigned long flags;
2479 u8 done = 0;
2480
Thomas Pugliese2b81c082013-06-11 10:39:31 -05002481 /* free the sg if it was used. */
2482 kfree(urb->sg);
2483 urb->sg = NULL;
2484
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002485 spin_lock_irqsave(&xfer->lock, flags);
2486 wa = xfer->wa;
2487 dev = &wa->usb_iface->dev;
2488
2489 if (usb_pipeisoc(xfer->urb->pipe)) {
2490 /*
2491 * Find the next isoc frame with data. Bail out after
2492 * isoc_data_frame_count > 1 since there is no need to walk
2493 * the entire frame array. We just need to know if
2494 * isoc_data_frame_count is 0, 1, or >1.
2495 */
2496 seg_index = seg->isoc_frame_index + 1;
2497 while ((seg_index < seg->isoc_frame_count)
2498 && (isoc_data_frame_count <= 1)) {
2499 struct usb_iso_packet_descriptor *iso_frame_desc =
2500 xfer->urb->iso_frame_desc;
2501 const int urb_frame_index =
2502 seg->isoc_frame_offset + seg_index;
2503
2504 if (iso_frame_desc[urb_frame_index].actual_length > 0) {
2505 /* save the index of the next frame with data */
2506 if (!isoc_data_frame_count)
2507 seg->isoc_frame_index = seg_index;
2508 ++isoc_data_frame_count;
2509 }
2510 ++seg_index;
2511 }
2512 }
2513 spin_unlock_irqrestore(&xfer->lock, flags);
2514
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002515 switch (urb->status) {
2516 case 0:
2517 spin_lock_irqsave(&xfer->lock, flags);
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002518
2519 seg->result += urb->actual_length;
2520 if (isoc_data_frame_count > 0) {
2521 int result;
2522 /* submit a read URB for the first frame with data. */
2523 __wa_populate_buf_in_urb_isoc(wa, xfer, seg,
2524 seg->isoc_frame_index + seg->isoc_frame_offset);
2525 result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
2526 if (result < 0) {
2527 dev_err(dev, "DTI Error: Could not submit buf in URB (%d)",
2528 result);
2529 wa_reset_all(wa);
2530 }
2531 } else {
2532 rpipe = xfer->ep->hcpriv;
2533 seg->status = WA_SEG_DONE;
2534 dev_dbg(dev, "xfer %p#%u: data in done (%zu bytes)\n",
2535 xfer, seg->index, seg->result);
2536 xfer->segs_done++;
2537 rpipe_ready = rpipe_avail_inc(rpipe);
2538 done = __wa_xfer_is_done(xfer);
2539 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002540 spin_unlock_irqrestore(&xfer->lock, flags);
2541 if (done)
2542 wa_xfer_completion(xfer);
2543 if (rpipe_ready)
2544 wa_xfer_delayed_run(rpipe);
2545 break;
2546 case -ECONNRESET: /* URB unlinked; no need to do anything */
2547 case -ENOENT: /* as it was done by the who unlinked us */
2548 break;
2549 default: /* Other errors ... */
2550 spin_lock_irqsave(&xfer->lock, flags);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002551 rpipe = xfer->ep->hcpriv;
2552 if (printk_ratelimit())
2553 dev_err(dev, "xfer %p#%u: data in error %d\n",
2554 xfer, seg->index, urb->status);
2555 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
2556 EDC_ERROR_TIMEFRAME)){
2557 dev_err(dev, "DTO: URB max acceptable errors "
2558 "exceeded, resetting device\n");
2559 wa_reset_all(wa);
2560 }
2561 seg->status = WA_SEG_ERROR;
2562 seg->result = urb->status;
2563 xfer->segs_done++;
2564 rpipe_ready = rpipe_avail_inc(rpipe);
2565 __wa_xfer_abort(xfer);
2566 done = __wa_xfer_is_done(xfer);
2567 spin_unlock_irqrestore(&xfer->lock, flags);
2568 if (done)
2569 wa_xfer_completion(xfer);
2570 if (rpipe_ready)
2571 wa_xfer_delayed_run(rpipe);
2572 }
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002573 /*
2574 * If we are in this callback and isoc_data_frame_count > 0, it means
2575 * that the dti_urb submission was delayed in wa_dti_cb. Once
2576 * isoc_data_frame_count gets to 1, we can submit the deferred URB
2577 * since the last buf_in_urb was just submitted.
2578 */
2579 if (isoc_data_frame_count == 1) {
2580 int result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
2581 if (result < 0) {
2582 dev_err(dev, "DTI Error: Could not submit DTI URB (%d)\n",
2583 result);
2584 wa_reset_all(wa);
2585 }
2586 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002587}
2588
2589/*
2590 * Handle an incoming transfer result buffer
2591 *
2592 * Given a transfer result buffer, it completes the transfer (possibly
2593 * scheduling and buffer in read) and then resubmits the DTI URB for a
2594 * new transfer result read.
2595 *
2596 *
2597 * The xfer_result DTI URB state machine
2598 *
2599 * States: OFF | RXR (Read-Xfer-Result) | RBI (Read-Buffer-In)
2600 *
2601 * We start in OFF mode, the first xfer_result notification [through
2602 * wa_handle_notif_xfer()] moves us to RXR by posting the DTI-URB to
2603 * read.
2604 *
2605 * We receive a buffer -- if it is not a xfer_result, we complain and
2606 * repost the DTI-URB. If it is a xfer_result then do the xfer seg
2607 * request accounting. If it is an IN segment, we move to RBI and post
2608 * a BUF-IN-URB to the right buffer. The BUF-IN-URB callback will
2609 * repost the DTI-URB and move to RXR state. if there was no IN
2610 * segment, it will repost the DTI-URB.
2611 *
2612 * We go back to OFF when we detect a ENOENT or ESHUTDOWN (or too many
2613 * errors) in the URBs.
2614 */
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002615static void wa_dti_cb(struct urb *urb)
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002616{
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002617 int result, dti_busy = 0;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002618 struct wahc *wa = urb->context;
2619 struct device *dev = &wa->usb_iface->dev;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002620 u32 xfer_id;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002621 u8 usb_status;
2622
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002623 BUG_ON(wa->dti_urb != urb);
2624 switch (wa->dti_urb->status) {
2625 case 0:
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002626 if (wa->dti_state == WA_DTI_TRANSFER_RESULT_PENDING) {
2627 struct wa_xfer_result *xfer_result;
2628 struct wa_xfer *xfer;
2629
2630 /* We have a xfer result buffer; check it */
2631 dev_dbg(dev, "DTI: xfer result %d bytes at %p\n",
2632 urb->actual_length, urb->transfer_buffer);
2633 if (urb->actual_length != sizeof(*xfer_result)) {
2634 dev_err(dev, "DTI Error: xfer result--bad size xfer result (%d bytes vs %zu needed)\n",
2635 urb->actual_length,
2636 sizeof(*xfer_result));
2637 break;
2638 }
2639 xfer_result = (struct wa_xfer_result *)(wa->dti_buf);
2640 if (xfer_result->hdr.bLength != sizeof(*xfer_result)) {
2641 dev_err(dev, "DTI Error: xfer result--bad header length %u\n",
2642 xfer_result->hdr.bLength);
2643 break;
2644 }
2645 if (xfer_result->hdr.bNotifyType != WA_XFER_RESULT) {
2646 dev_err(dev, "DTI Error: xfer result--bad header type 0x%02x\n",
2647 xfer_result->hdr.bNotifyType);
2648 break;
2649 }
2650 usb_status = xfer_result->bTransferStatus & 0x3f;
2651 if (usb_status == WA_XFER_STATUS_NOT_FOUND)
2652 /* taken care of already */
2653 break;
2654 xfer_id = le32_to_cpu(xfer_result->dwTransferID);
2655 xfer = wa_xfer_get_by_id(wa, xfer_id);
2656 if (xfer == NULL) {
2657 /* FIXME: transaction not found. */
2658 dev_err(dev, "DTI Error: xfer result--unknown xfer 0x%08x (status 0x%02x)\n",
2659 xfer_id, usb_status);
2660 break;
2661 }
2662 wa_xfer_result_chew(wa, xfer, xfer_result);
2663 wa_xfer_put(xfer);
2664 } else if (wa->dti_state == WA_DTI_ISOC_PACKET_STATUS_PENDING) {
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002665 dti_busy = wa_process_iso_packet_status(wa, urb);
Thomas Pugliese7a32d9b2013-10-04 10:40:45 -05002666 } else {
2667 dev_err(dev, "DTI Error: unexpected EP state = %d\n",
2668 wa->dti_state);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002669 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002670 break;
2671 case -ENOENT: /* (we killed the URB)...so, no broadcast */
2672 case -ESHUTDOWN: /* going away! */
2673 dev_dbg(dev, "DTI: going down! %d\n", urb->status);
2674 goto out;
2675 default:
2676 /* Unknown error */
2677 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS,
2678 EDC_ERROR_TIMEFRAME)) {
2679 dev_err(dev, "DTI: URB max acceptable errors "
2680 "exceeded, resetting device\n");
2681 wa_reset_all(wa);
2682 goto out;
2683 }
2684 if (printk_ratelimit())
2685 dev_err(dev, "DTI: URB error %d\n", urb->status);
2686 break;
2687 }
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002688
2689 /* Resubmit the DTI URB if we are not busy processing isoc in frames. */
2690 if (!dti_busy) {
2691 result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
2692 if (result < 0) {
2693 dev_err(dev, "DTI Error: Could not submit DTI URB (%d)\n",
2694 result);
2695 wa_reset_all(wa);
2696 }
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002697 }
2698out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002699 return;
2700}
2701
2702/*
2703 * Transfer complete notification
2704 *
2705 * Called from the notif.c code. We get a notification on EP2 saying
2706 * that some endpoint has some transfer result data available. We are
2707 * about to read it.
2708 *
2709 * To speed up things, we always have a URB reading the DTI URB; we
2710 * don't really set it up and start it until the first xfer complete
2711 * notification arrives, which is what we do here.
2712 *
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002713 * Follow up in wa_dti_cb(), as that's where the whole state
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002714 * machine starts.
2715 *
2716 * So here we just initialize the DTI URB for reading transfer result
2717 * notifications and also the buffer-in URB, for reading buffers. Then
2718 * we just submit the DTI URB.
2719 *
2720 * @wa shall be referenced
2721 */
2722void wa_handle_notif_xfer(struct wahc *wa, struct wa_notif_hdr *notif_hdr)
2723{
2724 int result;
2725 struct device *dev = &wa->usb_iface->dev;
2726 struct wa_notif_xfer *notif_xfer;
2727 const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd;
2728
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002729 notif_xfer = container_of(notif_hdr, struct wa_notif_xfer, hdr);
2730 BUG_ON(notif_hdr->bNotifyType != WA_NOTIF_TRANSFER);
2731
2732 if ((0x80 | notif_xfer->bEndpoint) != dti_epd->bEndpointAddress) {
2733 /* FIXME: hardcoded limitation, adapt */
2734 dev_err(dev, "BUG: DTI ep is %u, not %u (hack me)\n",
2735 notif_xfer->bEndpoint, dti_epd->bEndpointAddress);
2736 goto error;
2737 }
2738 if (wa->dti_urb != NULL) /* DTI URB already started */
2739 goto out;
2740
2741 wa->dti_urb = usb_alloc_urb(0, GFP_KERNEL);
2742 if (wa->dti_urb == NULL) {
2743 dev_err(dev, "Can't allocate DTI URB\n");
2744 goto error_dti_urb_alloc;
2745 }
2746 usb_fill_bulk_urb(
2747 wa->dti_urb, wa->usb_dev,
2748 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
Thomas Pugliese0367eef2013-09-26 10:49:41 -05002749 wa->dti_buf, wa->dti_buf_size,
2750 wa_dti_cb, wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002751
2752 wa->buf_in_urb = usb_alloc_urb(0, GFP_KERNEL);
2753 if (wa->buf_in_urb == NULL) {
2754 dev_err(dev, "Can't allocate BUF-IN URB\n");
2755 goto error_buf_in_urb_alloc;
2756 }
2757 usb_fill_bulk_urb(
2758 wa->buf_in_urb, wa->usb_dev,
2759 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
2760 NULL, 0, wa_buf_in_cb, wa);
2761 result = usb_submit_urb(wa->dti_urb, GFP_KERNEL);
2762 if (result < 0) {
Thomas Pugliese226b3a22013-12-10 12:10:33 -06002763 dev_err(dev, "DTI Error: Could not submit DTI URB (%d) resetting\n",
2764 result);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002765 goto error_dti_urb_submit;
2766 }
2767out:
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002768 return;
2769
2770error_dti_urb_submit:
2771 usb_put_urb(wa->buf_in_urb);
Thomas Pugliese67414482013-09-26 14:08:16 -05002772 wa->buf_in_urb = NULL;
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002773error_buf_in_urb_alloc:
2774 usb_put_urb(wa->dti_urb);
2775 wa->dti_urb = NULL;
2776error_dti_urb_alloc:
2777error:
2778 wa_reset_all(wa);
Inaky Perez-Gonzalezdf365422008-09-17 16:34:29 +01002779}