]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/usb/wusbcore/wa-xfer.c
usb: wusbcore: add more info to debug prints in urb_unlink path
[mirror_ubuntu-artful-kernel.git] / drivers / usb / wusbcore / wa-xfer.c
CommitLineData
df365423
IPG
1/*
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 *
25985edc 64 * (a) set up a timer every time an rpipe's use count drops to 1
df365423
IPG
65 * (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
f77f13e2 79 * availability of the different required components (blocks,
df365423
IPG
80 * rpipes, segment slots, etc), we go scheduling them. Painful.
81 */
82#include <linux/init.h>
83#include <linux/spinlock.h>
5a0e3ad6 84#include <linux/slab.h>
df365423 85#include <linux/hash.h>
9708cd2f 86#include <linux/ratelimit.h>
f940fcd8 87#include <linux/export.h>
2b81c083 88#include <linux/scatterlist.h>
bce83697 89
df365423
IPG
90#include "wa-hc.h"
91#include "wusbhc.h"
92
df365423 93enum {
f74b75e7
TP
94 /* [WUSB] section 8.3.3 allocates 7 bits for the segment index. */
95 WA_SEGS_MAX = 128,
df365423
IPG
96};
97
98enum wa_seg_status {
99 WA_SEG_NOTREADY,
100 WA_SEG_READY,
101 WA_SEG_DELAYED,
102 WA_SEG_SUBMITTED,
103 WA_SEG_PENDING,
104 WA_SEG_DTI_PENDING,
105 WA_SEG_DONE,
106 WA_SEG_ERROR,
107 WA_SEG_ABORTED,
108};
109
110static void wa_xfer_delayed_run(struct wa_rpipe *);
679ee475 111static int __wa_xfer_delayed_run(struct wa_rpipe *rpipe, int *dto_waiting);
df365423
IPG
112
113/*
114 * Life cycle governed by 'struct urb' (the refcount of the struct is
115 * that of the 'struct urb' and usb_free_urb() would free the whole
116 * struct).
117 */
118struct wa_seg {
09d94cbd 119 struct urb tr_urb; /* transfer request urb. */
7a32d9be 120 struct urb *isoc_pack_desc_urb; /* for isoc packet descriptor. */
09d94cbd 121 struct urb *dto_urb; /* for data output. */
df365423
IPG
122 struct list_head list_node; /* for rpipe->req_list */
123 struct wa_xfer *xfer; /* out xfer */
124 u8 index; /* which segment we are */
2101242c
TP
125 int isoc_frame_count; /* number of isoc frames in this segment. */
126 int isoc_frame_offset; /* starting frame offset in the xfer URB. */
127 int isoc_size; /* size of all isoc frames sent by this seg. */
df365423
IPG
128 enum wa_seg_status status;
129 ssize_t result; /* bytes xfered or error */
130 struct wa_xfer_hdr xfer_hdr;
df365423
IPG
131};
132
66591015 133static inline void wa_seg_init(struct wa_seg *seg)
df365423 134{
09d94cbd 135 usb_init_urb(&seg->tr_urb);
66591015
TP
136
137 /* set the remaining memory to 0. */
09d94cbd
TP
138 memset(((void *)seg) + sizeof(seg->tr_urb), 0,
139 sizeof(*seg) - sizeof(seg->tr_urb));
df365423
IPG
140}
141
142/*
143 * Protected by xfer->lock
144 *
145 */
146struct wa_xfer {
147 struct kref refcnt;
148 struct list_head list_node;
149 spinlock_t lock;
150 u32 id;
151
152 struct wahc *wa; /* Wire adapter we are plugged to */
153 struct usb_host_endpoint *ep;
25985edc 154 struct urb *urb; /* URB we are transferring for */
df365423
IPG
155 struct wa_seg **seg; /* transfer segments */
156 u8 segs, segs_submitted, segs_done;
157 unsigned is_inbound:1;
158 unsigned is_dma:1;
159 size_t seg_size;
160 int result;
2101242c
TP
161 /* Isoc frame that the current transfer buffer corresponds to. */
162 int dto_isoc_frame_index;
df365423
IPG
163
164 gfp_t gfp; /* allocation mask */
165
166 struct wusb_dev *wusb_dev; /* for activity timestamps */
167};
168
2101242c
TP
169static void __wa_populate_dto_urb_isoc(struct wa_xfer *xfer,
170 struct wa_seg *seg, int curr_iso_frame);
171
df365423
IPG
172static inline void wa_xfer_init(struct wa_xfer *xfer)
173{
174 kref_init(&xfer->refcnt);
175 INIT_LIST_HEAD(&xfer->list_node);
176 spin_lock_init(&xfer->lock);
177}
178
179/*
25985edc 180 * Destroy a transfer structure
df365423 181 *
7a32d9be 182 * Note that freeing xfer->seg[cnt]->tr_urb will free the containing
79731cbd 183 * xfer->seg[cnt] memory that was allocated by __wa_xfer_setup_segs.
df365423
IPG
184 */
185static void wa_xfer_destroy(struct kref *_xfer)
186{
187 struct wa_xfer *xfer = container_of(_xfer, struct wa_xfer, refcnt);
188 if (xfer->seg) {
189 unsigned cnt;
190 for (cnt = 0; cnt < xfer->segs; cnt++) {
7a32d9be
TP
191 struct wa_seg *seg = xfer->seg[cnt];
192 if (seg) {
193 usb_free_urb(seg->isoc_pack_desc_urb);
194 if (seg->dto_urb) {
195 kfree(seg->dto_urb->sg);
196 usb_free_urb(seg->dto_urb);
d993670c 197 }
7a32d9be 198 usb_free_urb(&seg->tr_urb);
d993670c 199 }
df365423 200 }
d993670c 201 kfree(xfer->seg);
df365423
IPG
202 }
203 kfree(xfer);
df365423
IPG
204}
205
206static void wa_xfer_get(struct wa_xfer *xfer)
207{
208 kref_get(&xfer->refcnt);
209}
210
211static void wa_xfer_put(struct wa_xfer *xfer)
212{
df365423 213 kref_put(&xfer->refcnt, wa_xfer_destroy);
df365423
IPG
214}
215
679ee475
TP
216/*
217 * Try to get exclusive access to the DTO endpoint resource. Return true
218 * if successful.
219 */
220static inline int __wa_dto_try_get(struct wahc *wa)
221{
222 return (test_and_set_bit(0, &wa->dto_in_use) == 0);
223}
224
225/* Release the DTO endpoint resource. */
226static inline void __wa_dto_put(struct wahc *wa)
227{
228 clear_bit_unlock(0, &wa->dto_in_use);
229}
230
231/* Service RPIPEs that are waiting on the DTO resource. */
232static void wa_check_for_delayed_rpipes(struct wahc *wa)
233{
234 unsigned long flags;
235 int dto_waiting = 0;
236 struct wa_rpipe *rpipe;
237
238 spin_lock_irqsave(&wa->rpipe_lock, flags);
239 while (!list_empty(&wa->rpipe_delayed_list) && !dto_waiting) {
240 rpipe = list_first_entry(&wa->rpipe_delayed_list,
241 struct wa_rpipe, list_node);
242 __wa_xfer_delayed_run(rpipe, &dto_waiting);
243 /* remove this RPIPE from the list if it is not waiting. */
244 if (!dto_waiting) {
245 pr_debug("%s: RPIPE %d serviced and removed from delayed list.\n",
246 __func__,
247 le16_to_cpu(rpipe->descr.wRPipeIndex));
248 list_del_init(&rpipe->list_node);
249 }
250 }
251 spin_unlock_irqrestore(&wa->rpipe_lock, flags);
252}
253
254/* add this RPIPE to the end of the delayed RPIPE list. */
255static void wa_add_delayed_rpipe(struct wahc *wa, struct wa_rpipe *rpipe)
256{
257 unsigned long flags;
258
259 spin_lock_irqsave(&wa->rpipe_lock, flags);
260 /* add rpipe to the list if it is not already on it. */
261 if (list_empty(&rpipe->list_node)) {
262 pr_debug("%s: adding RPIPE %d to the delayed list.\n",
263 __func__, le16_to_cpu(rpipe->descr.wRPipeIndex));
264 list_add_tail(&rpipe->list_node, &wa->rpipe_delayed_list);
265 }
266 spin_unlock_irqrestore(&wa->rpipe_lock, flags);
267}
268
df365423
IPG
269/*
270 * xfer is referenced
271 *
272 * xfer->lock has to be unlocked
273 *
274 * We take xfer->lock for setting the result; this is a barrier
275 * against drivers/usb/core/hcd.c:unlink1() being called after we call
276 * usb_hcd_giveback_urb() and wa_urb_dequeue() trying to get a
277 * reference to the transfer.
278 */
279static void wa_xfer_giveback(struct wa_xfer *xfer)
280{
281 unsigned long flags;
bce83697 282
df365423
IPG
283 spin_lock_irqsave(&xfer->wa->xfer_list_lock, flags);
284 list_del_init(&xfer->list_node);
b374487e 285 usb_hcd_unlink_urb_from_ep(&(xfer->wa->wusb->usb_hcd), xfer->urb);
df365423
IPG
286 spin_unlock_irqrestore(&xfer->wa->xfer_list_lock, flags);
287 /* FIXME: segmentation broken -- kills DWA */
288 wusbhc_giveback_urb(xfer->wa->wusb, xfer->urb, xfer->result);
289 wa_put(xfer->wa);
290 wa_xfer_put(xfer);
df365423
IPG
291}
292
293/*
294 * xfer is referenced
295 *
296 * xfer->lock has to be unlocked
297 */
298static void wa_xfer_completion(struct wa_xfer *xfer)
299{
df365423
IPG
300 if (xfer->wusb_dev)
301 wusb_dev_put(xfer->wusb_dev);
302 rpipe_put(xfer->ep->hcpriv);
303 wa_xfer_giveback(xfer);
df365423
IPG
304}
305
b9c84be6
TP
306/*
307 * Initialize a transfer's ID
308 *
309 * We need to use a sequential number; if we use the pointer or the
310 * hash of the pointer, it can repeat over sequential transfers and
311 * then it will confuse the HWA....wonder why in hell they put a 32
312 * bit handle in there then.
313 */
314static void wa_xfer_id_init(struct wa_xfer *xfer)
315{
316 xfer->id = atomic_add_return(1, &xfer->wa->xfer_id_count);
317}
318
319/* Return the xfer's ID. */
320static inline u32 wa_xfer_id(struct wa_xfer *xfer)
321{
322 return xfer->id;
323}
324
325/* Return the xfer's ID in transport format (little endian). */
326static inline __le32 wa_xfer_id_le32(struct wa_xfer *xfer)
327{
328 return cpu_to_le32(xfer->id);
329}
330
df365423
IPG
331/*
332 * If transfer is done, wrap it up and return true
333 *
334 * xfer->lock has to be locked
335 */
336static unsigned __wa_xfer_is_done(struct wa_xfer *xfer)
337{
bce83697 338 struct device *dev = &xfer->wa->usb_iface->dev;
df365423
IPG
339 unsigned result, cnt;
340 struct wa_seg *seg;
341 struct urb *urb = xfer->urb;
342 unsigned found_short = 0;
343
df365423
IPG
344 result = xfer->segs_done == xfer->segs_submitted;
345 if (result == 0)
346 goto out;
347 urb->actual_length = 0;
348 for (cnt = 0; cnt < xfer->segs; cnt++) {
349 seg = xfer->seg[cnt];
350 switch (seg->status) {
351 case WA_SEG_DONE:
352 if (found_short && seg->result > 0) {
b9c84be6
TP
353 dev_dbg(dev, "xfer %p ID %08X#%u: bad short segments (%zu)\n",
354 xfer, wa_xfer_id(xfer), cnt,
355 seg->result);
df365423
IPG
356 urb->status = -EINVAL;
357 goto out;
358 }
359 urb->actual_length += seg->result;
7a32d9be
TP
360 if (!(usb_pipeisoc(xfer->urb->pipe))
361 && seg->result < xfer->seg_size
df365423
IPG
362 && cnt != xfer->segs-1)
363 found_short = 1;
b9c84be6 364 dev_dbg(dev, "xfer %p ID %08X#%u: DONE short %d "
bce83697 365 "result %zu urb->actual_length %d\n",
b9c84be6
TP
366 xfer, wa_xfer_id(xfer), seg->index, found_short,
367 seg->result, urb->actual_length);
df365423
IPG
368 break;
369 case WA_SEG_ERROR:
370 xfer->result = seg->result;
cccd3a25 371 dev_dbg(dev, "xfer %p ID %08X#%u: ERROR result %zu(0x%08zX)\n",
b9c84be6
TP
372 xfer, wa_xfer_id(xfer), seg->index, seg->result,
373 seg->result);
df365423
IPG
374 goto out;
375 case WA_SEG_ABORTED:
bbfc3420
TP
376 xfer->result = seg->result;
377 dev_dbg(dev, "xfer %p ID %08X#%u: ABORTED result %zu(0x%08zX)\n",
378 xfer, wa_xfer_id(xfer), seg->index, seg->result,
379 seg->result);
df365423
IPG
380 goto out;
381 default:
b9c84be6
TP
382 dev_warn(dev, "xfer %p ID %08X#%u: is_done bad state %d\n",
383 xfer, wa_xfer_id(xfer), cnt, seg->status);
df365423 384 xfer->result = -EINVAL;
df365423
IPG
385 goto out;
386 }
387 }
388 xfer->result = 0;
389out:
df365423
IPG
390 return result;
391}
392
df365423
IPG
393/*
394 * Search for a transfer list ID on the HCD's URB list
395 *
396 * For 32 bit architectures, we use the pointer itself; for 64 bits, a
397 * 32-bit hash of the pointer.
398 *
399 * @returns NULL if not found.
400 */
401static struct wa_xfer *wa_xfer_get_by_id(struct wahc *wa, u32 id)
402{
403 unsigned long flags;
404 struct wa_xfer *xfer_itr;
405 spin_lock_irqsave(&wa->xfer_list_lock, flags);
406 list_for_each_entry(xfer_itr, &wa->xfer_list, list_node) {
407 if (id == xfer_itr->id) {
408 wa_xfer_get(xfer_itr);
409 goto out;
410 }
411 }
412 xfer_itr = NULL;
413out:
414 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
415 return xfer_itr;
416}
417
418struct wa_xfer_abort_buffer {
419 struct urb urb;
420 struct wa_xfer_abort cmd;
421};
422
423static void __wa_xfer_abort_cb(struct urb *urb)
424{
425 struct wa_xfer_abort_buffer *b = urb->context;
426 usb_put_urb(&b->urb);
427}
428
429/*
430 * Aborts an ongoing transaction
431 *
432 * Assumes the transfer is referenced and locked and in a submitted
433 * state (mainly that there is an endpoint/rpipe assigned).
434 *
435 * The callback (see above) does nothing but freeing up the data by
436 * putting the URB. Because the URB is allocated at the head of the
14e1d2df 437 * struct, the whole space we allocated is kfreed. *
df365423 438 */
14e1d2df 439static int __wa_xfer_abort(struct wa_xfer *xfer)
df365423 440{
14e1d2df 441 int result = -ENOMEM;
df365423
IPG
442 struct device *dev = &xfer->wa->usb_iface->dev;
443 struct wa_xfer_abort_buffer *b;
444 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
445
446 b = kmalloc(sizeof(*b), GFP_ATOMIC);
447 if (b == NULL)
448 goto error_kmalloc;
449 b->cmd.bLength = sizeof(b->cmd);
450 b->cmd.bRequestType = WA_XFER_ABORT;
451 b->cmd.wRPipe = rpipe->descr.wRPipeIndex;
fdd160c3 452 b->cmd.dwTransferID = wa_xfer_id_le32(xfer);
df365423
IPG
453
454 usb_init_urb(&b->urb);
455 usb_fill_bulk_urb(&b->urb, xfer->wa->usb_dev,
456 usb_sndbulkpipe(xfer->wa->usb_dev,
457 xfer->wa->dto_epd->bEndpointAddress),
458 &b->cmd, sizeof(b->cmd), __wa_xfer_abort_cb, b);
459 result = usb_submit_urb(&b->urb, GFP_ATOMIC);
460 if (result < 0)
461 goto error_submit;
14e1d2df 462 return result; /* callback frees! */
df365423
IPG
463
464
465error_submit:
466 if (printk_ratelimit())
467 dev_err(dev, "xfer %p: Can't submit abort request: %d\n",
468 xfer, result);
469 kfree(b);
470error_kmalloc:
14e1d2df 471 return result;
df365423
IPG
472
473}
474
2101242c
TP
475/*
476 * Calculate the number of isoc frames starting from isoc_frame_offset
477 * that will fit a in transfer segment.
478 */
479static int __wa_seg_calculate_isoc_frame_count(struct wa_xfer *xfer,
480 int isoc_frame_offset, int *total_size)
481{
482 int segment_size = 0, frame_count = 0;
483 int index = isoc_frame_offset;
f07ddb9e
TP
484 struct usb_iso_packet_descriptor *iso_frame_desc =
485 xfer->urb->iso_frame_desc;
2101242c
TP
486
487 while ((index < xfer->urb->number_of_packets)
f07ddb9e 488 && ((segment_size + iso_frame_desc[index].length)
2101242c 489 <= xfer->seg_size)) {
f07ddb9e
TP
490 /*
491 * For Alereon HWA devices, only include an isoc frame in a
492 * segment if it is physically contiguous with the previous
493 * frame. This is required because those devices expect
494 * the isoc frames to be sent as a single USB transaction as
495 * opposed to one transaction per frame with standard HWA.
496 */
497 if ((xfer->wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC)
498 && (index > isoc_frame_offset)
499 && ((iso_frame_desc[index - 1].offset +
500 iso_frame_desc[index - 1].length) !=
501 iso_frame_desc[index].offset))
502 break;
503
2101242c
TP
504 /* this frame fits. count it. */
505 ++frame_count;
f07ddb9e 506 segment_size += iso_frame_desc[index].length;
2101242c
TP
507
508 /* move to the next isoc frame. */
509 ++index;
510 }
511
512 *total_size = segment_size;
513 return frame_count;
514}
515
df365423
IPG
516/*
517 *
518 * @returns < 0 on error, transfer segment request size if ok
519 */
520static ssize_t __wa_xfer_setup_sizes(struct wa_xfer *xfer,
521 enum wa_xfer_type *pxfer_type)
522{
523 ssize_t result;
524 struct device *dev = &xfer->wa->usb_iface->dev;
525 size_t maxpktsize;
526 struct urb *urb = xfer->urb;
527 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
528
df365423
IPG
529 switch (rpipe->descr.bmAttribute & 0x3) {
530 case USB_ENDPOINT_XFER_CONTROL:
531 *pxfer_type = WA_XFER_TYPE_CTL;
532 result = sizeof(struct wa_xfer_ctl);
533 break;
534 case USB_ENDPOINT_XFER_INT:
535 case USB_ENDPOINT_XFER_BULK:
536 *pxfer_type = WA_XFER_TYPE_BI;
537 result = sizeof(struct wa_xfer_bi);
538 break;
539 case USB_ENDPOINT_XFER_ISOC:
7a32d9be
TP
540 if (usb_pipeout(urb->pipe)) {
541 *pxfer_type = WA_XFER_TYPE_ISO;
542 result = sizeof(struct wa_xfer_hwaiso);
543 } else {
544 dev_err(dev, "FIXME: ISOC IN not implemented\n");
545 result = -ENOSYS;
546 goto error;
547 }
548 break;
df365423
IPG
549 default:
550 /* never happens */
551 BUG();
552 result = -EINVAL; /* shut gcc up */
7a32d9be 553 }
df365423
IPG
554 xfer->is_inbound = urb->pipe & USB_DIR_IN ? 1 : 0;
555 xfer->is_dma = urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? 1 : 0;
7a32d9be 556
df365423 557 maxpktsize = le16_to_cpu(rpipe->descr.wMaxPacketSize);
7a32d9be 558 if ((rpipe->descr.bmAttribute & 0x3) == USB_ENDPOINT_XFER_ISOC) {
2101242c
TP
559 int index = 0;
560
7a32d9be 561 xfer->seg_size = maxpktsize;
2101242c
TP
562 xfer->segs = 0;
563 /*
564 * loop over urb->number_of_packets to determine how many
565 * xfer segments will be needed to send the isoc frames.
566 */
567 while (index < urb->number_of_packets) {
568 int seg_size; /* don't care. */
569 index += __wa_seg_calculate_isoc_frame_count(xfer,
570 index, &seg_size);
571 ++xfer->segs;
572 }
7a32d9be
TP
573 } else {
574 xfer->seg_size = le16_to_cpu(rpipe->descr.wBlocks)
575 * 1 << (xfer->wa->wa_descr->bRPipeBlockSize - 1);
576 /* Compute the segment size and make sure it is a multiple of
577 * the maxpktsize (WUSB1.0[8.3.3.1])...not really too much of
578 * a check (FIXME) */
579 if (xfer->seg_size < maxpktsize) {
580 dev_err(dev,
581 "HW BUG? seg_size %zu smaller than maxpktsize %zu\n",
582 xfer->seg_size, maxpktsize);
583 result = -EINVAL;
584 goto error;
585 }
586 xfer->seg_size = (xfer->seg_size / maxpktsize) * maxpktsize;
587 xfer->segs = DIV_ROUND_UP(urb->transfer_buffer_length,
588 xfer->seg_size);
7a32d9be
TP
589 if (xfer->segs == 0 && *pxfer_type == WA_XFER_TYPE_CTL)
590 xfer->segs = 1;
df365423 591 }
2101242c 592
f74b75e7 593 if (xfer->segs > WA_SEGS_MAX) {
2101242c
TP
594 dev_err(dev, "BUG? oops, number of segments %zu bigger than %d\n",
595 (urb->transfer_buffer_length/xfer->seg_size),
596 WA_SEGS_MAX);
597 result = -EINVAL;
598 goto error;
599 }
df365423 600error:
df365423
IPG
601 return result;
602}
603
2101242c
TP
604static void __wa_setup_isoc_packet_descr(
605 struct wa_xfer_packet_info_hwaiso *packet_desc,
606 struct wa_xfer *xfer,
607 struct wa_seg *seg) {
608 struct usb_iso_packet_descriptor *iso_frame_desc =
609 xfer->urb->iso_frame_desc;
610 int frame_index;
611
612 /* populate isoc packet descriptor. */
613 packet_desc->bPacketType = WA_XFER_ISO_PACKET_INFO;
614 packet_desc->wLength = cpu_to_le16(sizeof(*packet_desc) +
615 (sizeof(packet_desc->PacketLength[0]) *
616 seg->isoc_frame_count));
617 for (frame_index = 0; frame_index < seg->isoc_frame_count;
618 ++frame_index) {
619 int offset_index = frame_index + seg->isoc_frame_offset;
620 packet_desc->PacketLength[frame_index] =
621 cpu_to_le16(iso_frame_desc[offset_index].length);
622 }
623}
624
625
bce83697 626/* Fill in the common request header and xfer-type specific data. */
df365423
IPG
627static void __wa_xfer_setup_hdr0(struct wa_xfer *xfer,
628 struct wa_xfer_hdr *xfer_hdr0,
629 enum wa_xfer_type xfer_type,
630 size_t xfer_hdr_size)
631{
632 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
2101242c 633 struct wa_seg *seg = xfer->seg[0];
df365423 634
2101242c 635 xfer_hdr0 = &seg->xfer_hdr;
df365423
IPG
636 xfer_hdr0->bLength = xfer_hdr_size;
637 xfer_hdr0->bRequestType = xfer_type;
638 xfer_hdr0->wRPipe = rpipe->descr.wRPipeIndex;
fdd160c3 639 xfer_hdr0->dwTransferID = wa_xfer_id_le32(xfer);
df365423
IPG
640 xfer_hdr0->bTransferSegment = 0;
641 switch (xfer_type) {
642 case WA_XFER_TYPE_CTL: {
643 struct wa_xfer_ctl *xfer_ctl =
644 container_of(xfer_hdr0, struct wa_xfer_ctl, hdr);
645 xfer_ctl->bmAttribute = xfer->is_inbound ? 1 : 0;
df365423
IPG
646 memcpy(&xfer_ctl->baSetupData, xfer->urb->setup_packet,
647 sizeof(xfer_ctl->baSetupData));
648 break;
649 }
650 case WA_XFER_TYPE_BI:
651 break;
7a32d9be
TP
652 case WA_XFER_TYPE_ISO: {
653 struct wa_xfer_hwaiso *xfer_iso =
654 container_of(xfer_hdr0, struct wa_xfer_hwaiso, hdr);
655 struct wa_xfer_packet_info_hwaiso *packet_desc =
656 ((void *)xfer_iso) + xfer_hdr_size;
2101242c 657
7a32d9be 658 /* populate the isoc section of the transfer request. */
2101242c
TP
659 xfer_iso->dwNumOfPackets = cpu_to_le32(seg->isoc_frame_count);
660 /* populate isoc packet descriptor. */
661 __wa_setup_isoc_packet_descr(packet_desc, xfer, seg);
7a32d9be
TP
662 break;
663 }
df365423
IPG
664 default:
665 BUG();
666 };
667}
668
669/*
670 * Callback for the OUT data phase of the segment request
671 *
09d94cbd 672 * Check wa_seg_tr_cb(); most comments also apply here because this
df365423
IPG
673 * function does almost the same thing and they work closely
674 * together.
675 *
25985edc 676 * If the seg request has failed but this DTO phase has succeeded,
09d94cbd 677 * wa_seg_tr_cb() has already failed the segment and moved the
df365423
IPG
678 * status to WA_SEG_ERROR, so this will go through 'case 0' and
679 * effectively do nothing.
680 */
681static void wa_seg_dto_cb(struct urb *urb)
682{
683 struct wa_seg *seg = urb->context;
684 struct wa_xfer *xfer = seg->xfer;
685 struct wahc *wa;
686 struct device *dev;
687 struct wa_rpipe *rpipe;
688 unsigned long flags;
689 unsigned rpipe_ready = 0;
2101242c 690 int data_send_done = 1, release_dto = 0, holding_dto = 0;
df365423 691 u8 done = 0;
2101242c 692 int result;
df365423 693
d5b5c9f2
TP
694 /* free the sg if it was used. */
695 kfree(urb->sg);
696 urb->sg = NULL;
697
2101242c
TP
698 spin_lock_irqsave(&xfer->lock, flags);
699 wa = xfer->wa;
700 dev = &wa->usb_iface->dev;
701 if (usb_pipeisoc(xfer->urb->pipe)) {
f07ddb9e
TP
702 /* Alereon HWA sends all isoc frames in a single transfer. */
703 if (wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC)
704 xfer->dto_isoc_frame_index += seg->isoc_frame_count;
705 else
706 xfer->dto_isoc_frame_index += 1;
2101242c
TP
707 if (xfer->dto_isoc_frame_index < seg->isoc_frame_count) {
708 data_send_done = 0;
709 holding_dto = 1; /* checked in error cases. */
710 /*
711 * if this is the last isoc frame of the segment, we
712 * can release DTO after sending this frame.
713 */
714 if ((xfer->dto_isoc_frame_index + 1) >=
715 seg->isoc_frame_count)
716 release_dto = 1;
717 }
718 dev_dbg(dev, "xfer 0x%08X#%u: isoc frame = %d, holding_dto = %d, release_dto = %d.\n",
719 wa_xfer_id(xfer), seg->index,
720 xfer->dto_isoc_frame_index, holding_dto, release_dto);
721 }
722 spin_unlock_irqrestore(&xfer->lock, flags);
723
df365423
IPG
724 switch (urb->status) {
725 case 0:
726 spin_lock_irqsave(&xfer->lock, flags);
2101242c
TP
727 seg->result += urb->actual_length;
728 if (data_send_done) {
729 dev_dbg(dev, "xfer 0x%08X#%u: data out done (%zu bytes)\n",
730 wa_xfer_id(xfer), seg->index, seg->result);
731 if (seg->status < WA_SEG_PENDING)
732 seg->status = WA_SEG_PENDING;
733 } else {
734 /* should only hit this for isoc xfers. */
735 /*
736 * Populate the dto URB with the next isoc frame buffer,
737 * send the URB and release DTO if we no longer need it.
738 */
739 __wa_populate_dto_urb_isoc(xfer, seg,
740 seg->isoc_frame_offset +
741 xfer->dto_isoc_frame_index);
742
743 /* resubmit the URB with the next isoc frame. */
744 result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
745 if (result < 0) {
746 dev_err(dev, "xfer 0x%08X#%u: DTO submit failed: %d\n",
747 wa_xfer_id(xfer), seg->index, result);
748 spin_unlock_irqrestore(&xfer->lock, flags);
749 goto error_dto_submit;
750 }
751 }
df365423 752 spin_unlock_irqrestore(&xfer->lock, flags);
2101242c
TP
753 if (release_dto) {
754 __wa_dto_put(wa);
755 wa_check_for_delayed_rpipes(wa);
756 }
df365423
IPG
757 break;
758 case -ECONNRESET: /* URB unlinked; no need to do anything */
759 case -ENOENT: /* as it was done by the who unlinked us */
2101242c
TP
760 if (holding_dto) {
761 __wa_dto_put(wa);
762 wa_check_for_delayed_rpipes(wa);
763 }
df365423
IPG
764 break;
765 default: /* Other errors ... */
2101242c
TP
766 dev_err(dev, "xfer 0x%08X#%u: data out error %d\n",
767 wa_xfer_id(xfer), seg->index, urb->status);
768 goto error_default;
769 }
770
771 return;
772
773error_dto_submit:
774error_default:
775 spin_lock_irqsave(&xfer->lock, flags);
776 rpipe = xfer->ep->hcpriv;
777 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
778 EDC_ERROR_TIMEFRAME)){
779 dev_err(dev, "DTO: URB max acceptable errors exceeded, resetting device\n");
780 wa_reset_all(wa);
781 }
782 if (seg->status != WA_SEG_ERROR) {
783 seg->status = WA_SEG_ERROR;
784 seg->result = urb->status;
785 xfer->segs_done++;
786 __wa_xfer_abort(xfer);
787 rpipe_ready = rpipe_avail_inc(rpipe);
788 done = __wa_xfer_is_done(xfer);
df365423 789 }
2101242c
TP
790 spin_unlock_irqrestore(&xfer->lock, flags);
791 if (holding_dto) {
792 __wa_dto_put(wa);
793 wa_check_for_delayed_rpipes(wa);
794 }
795 if (done)
796 wa_xfer_completion(xfer);
797 if (rpipe_ready)
798 wa_xfer_delayed_run(rpipe);
799
df365423
IPG
800}
801
7a32d9be
TP
802/*
803 * Callback for the isoc packet descriptor phase of the segment request
804 *
805 * Check wa_seg_tr_cb(); most comments also apply here because this
806 * function does almost the same thing and they work closely
807 * together.
808 *
809 * If the seg request has failed but this phase has succeeded,
810 * wa_seg_tr_cb() has already failed the segment and moved the
811 * status to WA_SEG_ERROR, so this will go through 'case 0' and
812 * effectively do nothing.
813 */
814static void wa_seg_iso_pack_desc_cb(struct urb *urb)
815{
816 struct wa_seg *seg = urb->context;
817 struct wa_xfer *xfer = seg->xfer;
818 struct wahc *wa;
819 struct device *dev;
820 struct wa_rpipe *rpipe;
821 unsigned long flags;
822 unsigned rpipe_ready = 0;
823 u8 done = 0;
824
825 switch (urb->status) {
826 case 0:
827 spin_lock_irqsave(&xfer->lock, flags);
828 wa = xfer->wa;
829 dev = &wa->usb_iface->dev;
2101242c
TP
830 dev_dbg(dev, "iso xfer %08X#%u: packet descriptor done\n",
831 wa_xfer_id(xfer), seg->index);
7a32d9be
TP
832 if (xfer->is_inbound && seg->status < WA_SEG_PENDING)
833 seg->status = WA_SEG_PENDING;
834 spin_unlock_irqrestore(&xfer->lock, flags);
835 break;
836 case -ECONNRESET: /* URB unlinked; no need to do anything */
837 case -ENOENT: /* as it was done by the who unlinked us */
838 break;
839 default: /* Other errors ... */
840 spin_lock_irqsave(&xfer->lock, flags);
841 wa = xfer->wa;
842 dev = &wa->usb_iface->dev;
843 rpipe = xfer->ep->hcpriv;
2101242c
TP
844 pr_err_ratelimited("iso xfer %08X#%u: packet descriptor error %d\n",
845 wa_xfer_id(xfer), seg->index, urb->status);
7a32d9be
TP
846 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
847 EDC_ERROR_TIMEFRAME)){
848 dev_err(dev, "DTO: URB max acceptable errors exceeded, resetting device\n");
849 wa_reset_all(wa);
850 }
851 if (seg->status != WA_SEG_ERROR) {
852 usb_unlink_urb(seg->dto_urb);
853 seg->status = WA_SEG_ERROR;
854 seg->result = urb->status;
855 xfer->segs_done++;
856 __wa_xfer_abort(xfer);
857 rpipe_ready = rpipe_avail_inc(rpipe);
858 done = __wa_xfer_is_done(xfer);
859 }
860 spin_unlock_irqrestore(&xfer->lock, flags);
861 if (done)
862 wa_xfer_completion(xfer);
863 if (rpipe_ready)
864 wa_xfer_delayed_run(rpipe);
865 }
866}
867
df365423
IPG
868/*
869 * Callback for the segment request
870 *
af901ca1 871 * If successful transition state (unless already transitioned or
df365423
IPG
872 * outbound transfer); otherwise, take a note of the error, mark this
873 * segment done and try completion.
874 *
875 * Note we don't access until we are sure that the transfer hasn't
876 * been cancelled (ECONNRESET, ENOENT), which could mean that
877 * seg->xfer could be already gone.
878 *
879 * We have to check before setting the status to WA_SEG_PENDING
880 * because sometimes the xfer result callback arrives before this
881 * callback (geeeeeeze), so it might happen that we are already in
7a32d9be 882 * another state. As well, we don't set it if the transfer is not inbound,
df365423
IPG
883 * as in that case, wa_seg_dto_cb will do it when the OUT data phase
884 * finishes.
885 */
09d94cbd 886static void wa_seg_tr_cb(struct urb *urb)
df365423
IPG
887{
888 struct wa_seg *seg = urb->context;
889 struct wa_xfer *xfer = seg->xfer;
890 struct wahc *wa;
891 struct device *dev;
892 struct wa_rpipe *rpipe;
893 unsigned long flags;
894 unsigned rpipe_ready;
895 u8 done = 0;
896
df365423
IPG
897 switch (urb->status) {
898 case 0:
899 spin_lock_irqsave(&xfer->lock, flags);
900 wa = xfer->wa;
901 dev = &wa->usb_iface->dev;
7a32d9be
TP
902 dev_dbg(dev, "xfer %p ID 0x%08X#%u: request done\n",
903 xfer, wa_xfer_id(xfer), seg->index);
904 if (xfer->is_inbound &&
905 seg->status < WA_SEG_PENDING &&
906 !(usb_pipeisoc(xfer->urb->pipe)))
df365423
IPG
907 seg->status = WA_SEG_PENDING;
908 spin_unlock_irqrestore(&xfer->lock, flags);
909 break;
910 case -ECONNRESET: /* URB unlinked; no need to do anything */
911 case -ENOENT: /* as it was done by the who unlinked us */
912 break;
913 default: /* Other errors ... */
914 spin_lock_irqsave(&xfer->lock, flags);
915 wa = xfer->wa;
916 dev = &wa->usb_iface->dev;
917 rpipe = xfer->ep->hcpriv;
918 if (printk_ratelimit())
b9c84be6
TP
919 dev_err(dev, "xfer %p ID 0x%08X#%u: request error %d\n",
920 xfer, wa_xfer_id(xfer), seg->index,
921 urb->status);
df365423
IPG
922 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
923 EDC_ERROR_TIMEFRAME)){
924 dev_err(dev, "DTO: URB max acceptable errors "
925 "exceeded, resetting device\n");
926 wa_reset_all(wa);
927 }
7a32d9be 928 usb_unlink_urb(seg->isoc_pack_desc_urb);
df365423
IPG
929 usb_unlink_urb(seg->dto_urb);
930 seg->status = WA_SEG_ERROR;
931 seg->result = urb->status;
932 xfer->segs_done++;
933 __wa_xfer_abort(xfer);
934 rpipe_ready = rpipe_avail_inc(rpipe);
935 done = __wa_xfer_is_done(xfer);
936 spin_unlock_irqrestore(&xfer->lock, flags);
937 if (done)
938 wa_xfer_completion(xfer);
939 if (rpipe_ready)
940 wa_xfer_delayed_run(rpipe);
941 }
df365423
IPG
942}
943
ffd6d17d
TP
944/*
945 * Allocate an SG list to store bytes_to_transfer bytes and copy the
2b81c083 946 * subset of the in_sg that matches the buffer subset
ffd6d17d
TP
947 * we are about to transfer.
948 */
2b81c083
TP
949static struct scatterlist *wa_xfer_create_subset_sg(struct scatterlist *in_sg,
950 const unsigned int bytes_transferred,
951 const unsigned int bytes_to_transfer, unsigned int *out_num_sgs)
952{
953 struct scatterlist *out_sg;
954 unsigned int bytes_processed = 0, offset_into_current_page_data = 0,
955 nents;
956 struct scatterlist *current_xfer_sg = in_sg;
957 struct scatterlist *current_seg_sg, *last_seg_sg;
958
959 /* skip previously transferred pages. */
960 while ((current_xfer_sg) &&
961 (bytes_processed < bytes_transferred)) {
962 bytes_processed += current_xfer_sg->length;
963
964 /* advance the sg if current segment starts on or past the
965 next page. */
966 if (bytes_processed <= bytes_transferred)
967 current_xfer_sg = sg_next(current_xfer_sg);
968 }
969
970 /* the data for the current segment starts in current_xfer_sg.
971 calculate the offset. */
972 if (bytes_processed > bytes_transferred) {
973 offset_into_current_page_data = current_xfer_sg->length -
974 (bytes_processed - bytes_transferred);
975 }
976
977 /* calculate the number of pages needed by this segment. */
978 nents = DIV_ROUND_UP((bytes_to_transfer +
979 offset_into_current_page_data +
980 current_xfer_sg->offset),
981 PAGE_SIZE);
982
983 out_sg = kmalloc((sizeof(struct scatterlist) * nents), GFP_ATOMIC);
984 if (out_sg) {
985 sg_init_table(out_sg, nents);
986
987 /* copy the portion of the incoming SG that correlates to the
988 * data to be transferred by this segment to the segment SG. */
989 last_seg_sg = current_seg_sg = out_sg;
990 bytes_processed = 0;
991
992 /* reset nents and calculate the actual number of sg entries
993 needed. */
994 nents = 0;
995 while ((bytes_processed < bytes_to_transfer) &&
996 current_seg_sg && current_xfer_sg) {
997 unsigned int page_len = min((current_xfer_sg->length -
998 offset_into_current_page_data),
999 (bytes_to_transfer - bytes_processed));
1000
1001 sg_set_page(current_seg_sg, sg_page(current_xfer_sg),
1002 page_len,
1003 current_xfer_sg->offset +
1004 offset_into_current_page_data);
1005
1006 bytes_processed += page_len;
1007
1008 last_seg_sg = current_seg_sg;
1009 current_seg_sg = sg_next(current_seg_sg);
1010 current_xfer_sg = sg_next(current_xfer_sg);
1011
1012 /* only the first page may require additional offset. */
1013 offset_into_current_page_data = 0;
1014 nents++;
1015 }
1016
1017 /* update num_sgs and terminate the list since we may have
1018 * concatenated pages. */
1019 sg_mark_end(last_seg_sg);
1020 *out_num_sgs = nents;
1021 }
1022
1023 return out_sg;
1024}
1025
7a32d9be
TP
1026/*
1027 * Populate DMA buffer info for the isoc dto urb.
1028 */
2101242c
TP
1029static void __wa_populate_dto_urb_isoc(struct wa_xfer *xfer,
1030 struct wa_seg *seg, int curr_iso_frame)
7a32d9be 1031{
7a32d9be
TP
1032 seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1033 seg->dto_urb->sg = NULL;
1034 seg->dto_urb->num_sgs = 0;
f07ddb9e
TP
1035 /* dto urb buffer address pulled from iso_frame_desc. */
1036 seg->dto_urb->transfer_dma = xfer->urb->transfer_dma +
1037 xfer->urb->iso_frame_desc[curr_iso_frame].offset;
1038 /* The Alereon HWA sends a single URB with all isoc segs. */
1039 if (xfer->wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC)
1040 seg->dto_urb->transfer_buffer_length = seg->isoc_size;
1041 else
1042 seg->dto_urb->transfer_buffer_length =
1043 xfer->urb->iso_frame_desc[curr_iso_frame].length;
7a32d9be
TP
1044}
1045
ffd6d17d
TP
1046/*
1047 * Populate buffer ptr and size, DMA buffer or SG list for the dto urb.
1048 */
1049static int __wa_populate_dto_urb(struct wa_xfer *xfer,
1050 struct wa_seg *seg, size_t buf_itr_offset, size_t buf_itr_size)
1051{
1052 int result = 0;
1053
1054 if (xfer->is_dma) {
1055 seg->dto_urb->transfer_dma =
1056 xfer->urb->transfer_dma + buf_itr_offset;
1057 seg->dto_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1058 seg->dto_urb->sg = NULL;
1059 seg->dto_urb->num_sgs = 0;
1060 } else {
1061 /* do buffer or SG processing. */
1062 seg->dto_urb->transfer_flags &=
1063 ~URB_NO_TRANSFER_DMA_MAP;
1064 /* this should always be 0 before a resubmit. */
1065 seg->dto_urb->num_mapped_sgs = 0;
1066
1067 if (xfer->urb->transfer_buffer) {
1068 seg->dto_urb->transfer_buffer =
1069 xfer->urb->transfer_buffer +
1070 buf_itr_offset;
1071 seg->dto_urb->sg = NULL;
1072 seg->dto_urb->num_sgs = 0;
1073 } else {
1074 seg->dto_urb->transfer_buffer = NULL;
1075
1076 /*
1077 * allocate an SG list to store seg_size bytes
1078 * and copy the subset of the xfer->urb->sg that
1079 * matches the buffer subset we are about to
1080 * read.
1081 */
1082 seg->dto_urb->sg = wa_xfer_create_subset_sg(
1083 xfer->urb->sg,
1084 buf_itr_offset, buf_itr_size,
1085 &(seg->dto_urb->num_sgs));
1086 if (!(seg->dto_urb->sg))
1087 result = -ENOMEM;
1088 }
1089 }
1090 seg->dto_urb->transfer_buffer_length = buf_itr_size;
1091
1092 return result;
1093}
1094
df365423
IPG
1095/*
1096 * Allocate the segs array and initialize each of them
1097 *
1098 * The segments are freed by wa_xfer_destroy() when the xfer use count
1099 * drops to zero; however, because each segment is given the same life
1100 * cycle as the USB URB it contains, it is actually freed by
1101 * usb_put_urb() on the contained USB URB (twisted, eh?).
1102 */
1103static int __wa_xfer_setup_segs(struct wa_xfer *xfer, size_t xfer_hdr_size)
1104{
2101242c 1105 int result, cnt, iso_frame_offset;
df365423
IPG
1106 size_t alloc_size = sizeof(*xfer->seg[0])
1107 - sizeof(xfer->seg[0]->xfer_hdr) + xfer_hdr_size;
1108 struct usb_device *usb_dev = xfer->wa->usb_dev;
1109 const struct usb_endpoint_descriptor *dto_epd = xfer->wa->dto_epd;
1110 struct wa_seg *seg;
2101242c
TP
1111 size_t buf_itr, buf_size, buf_itr_size;
1112 int xfer_isoc_frame_offset = 0;
df365423
IPG
1113
1114 result = -ENOMEM;
92c4d9bd 1115 xfer->seg = kcalloc(xfer->segs, sizeof(xfer->seg[0]), GFP_ATOMIC);
df365423
IPG
1116 if (xfer->seg == NULL)
1117 goto error_segs_kzalloc;
1118 buf_itr = 0;
1119 buf_size = xfer->urb->transfer_buffer_length;
2101242c
TP
1120 iso_frame_offset = 0;
1121 for (cnt = 0; cnt < xfer->segs; cnt++) {
1122 size_t iso_pkt_descr_size = 0;
1123 int seg_isoc_frame_count = 0, seg_isoc_size = 0;
7a32d9be 1124
2101242c
TP
1125 if (usb_pipeisoc(xfer->urb->pipe)) {
1126 seg_isoc_frame_count =
1127 __wa_seg_calculate_isoc_frame_count(xfer,
1128 xfer_isoc_frame_offset, &seg_isoc_size);
7a32d9be 1129
2101242c
TP
1130 iso_pkt_descr_size =
1131 sizeof(struct wa_xfer_packet_info_hwaiso) +
1132 (seg_isoc_frame_count * sizeof(__le16));
1133 }
1134 seg = xfer->seg[cnt] = kmalloc(alloc_size + iso_pkt_descr_size,
1135 GFP_ATOMIC);
df365423 1136 if (seg == NULL)
66591015 1137 goto error_seg_kmalloc;
df365423
IPG
1138 wa_seg_init(seg);
1139 seg->xfer = xfer;
1140 seg->index = cnt;
2101242c
TP
1141 seg->isoc_frame_count = seg_isoc_frame_count;
1142 seg->isoc_frame_offset = xfer_isoc_frame_offset;
1143 seg->isoc_size = seg_isoc_size;
09d94cbd 1144 usb_fill_bulk_urb(&seg->tr_urb, usb_dev,
df365423
IPG
1145 usb_sndbulkpipe(usb_dev,
1146 dto_epd->bEndpointAddress),
1147 &seg->xfer_hdr, xfer_hdr_size,
09d94cbd 1148 wa_seg_tr_cb, seg);
2b81c083 1149 buf_itr_size = min(buf_size, xfer->seg_size);
df365423 1150 if (xfer->is_inbound == 0 && buf_size > 0) {
2b81c083 1151 /* outbound data. */
df365423
IPG
1152 seg->dto_urb = usb_alloc_urb(0, GFP_ATOMIC);
1153 if (seg->dto_urb == NULL)
1154 goto error_dto_alloc;
1155 usb_fill_bulk_urb(
1156 seg->dto_urb, usb_dev,
1157 usb_sndbulkpipe(usb_dev,
1158 dto_epd->bEndpointAddress),
1159 NULL, 0, wa_seg_dto_cb, seg);
ffd6d17d 1160
7a32d9be
TP
1161 if (usb_pipeisoc(xfer->urb->pipe)) {
1162 /* iso packet descriptor. */
1163 seg->isoc_pack_desc_urb =
1164 usb_alloc_urb(0, GFP_ATOMIC);
1165 if (seg->isoc_pack_desc_urb == NULL)
1166 goto error_iso_pack_desc_alloc;
1167 /*
1168 * The buffer for the isoc packet descriptor
1169 * after the transfer request header in the
1170 * segment object memory buffer.
1171 */
1172 usb_fill_bulk_urb(
1173 seg->isoc_pack_desc_urb, usb_dev,
1174 usb_sndbulkpipe(usb_dev,
1175 dto_epd->bEndpointAddress),
1176 (void *)(&seg->xfer_hdr) +
1177 xfer_hdr_size,
1178 iso_pkt_descr_size,
1179 wa_seg_iso_pack_desc_cb, seg);
ffd6d17d 1180
2101242c
TP
1181 /*
1182 * Fill in the xfer buffer information for the
1183 * first isoc frame. Subsequent frames in this
1184 * segment will be filled in and sent from the
1185 * DTO completion routine, if needed.
1186 */
1187 __wa_populate_dto_urb_isoc(xfer, seg,
1188 xfer_isoc_frame_offset);
1189 /* adjust starting frame offset for next seg. */
1190 xfer_isoc_frame_offset += seg_isoc_frame_count;
7a32d9be
TP
1191 } else {
1192 /* fill in the xfer buffer information. */
1193 result = __wa_populate_dto_urb(xfer, seg,
1194 buf_itr, buf_itr_size);
1195 if (result < 0)
1196 goto error_seg_outbound_populate;
1197
1198 buf_itr += buf_itr_size;
1199 buf_size -= buf_itr_size;
1200 }
df365423
IPG
1201 }
1202 seg->status = WA_SEG_READY;
df365423
IPG
1203 }
1204 return 0;
1205
ffd6d17d
TP
1206 /*
1207 * Free the memory for the current segment which failed to init.
1208 * Use the fact that cnt is left at were it failed. The remaining
1209 * segments will be cleaned up by wa_xfer_destroy.
1210 */
7a32d9be 1211error_iso_pack_desc_alloc:
ffd6d17d 1212error_seg_outbound_populate:
11b1bf81 1213 usb_free_urb(xfer->seg[cnt]->dto_urb);
df365423
IPG
1214error_dto_alloc:
1215 kfree(xfer->seg[cnt]);
ffd6d17d 1216 xfer->seg[cnt] = NULL;
66591015 1217error_seg_kmalloc:
df365423
IPG
1218error_segs_kzalloc:
1219 return result;
1220}
1221
1222/*
1223 * Allocates all the stuff needed to submit a transfer
1224 *
1225 * Breaks the whole data buffer in a list of segments, each one has a
1226 * structure allocated to it and linked in xfer->seg[index]
1227 *
1228 * FIXME: merge setup_segs() and the last part of this function, no
1229 * need to do two for loops when we could run everything in a
1230 * single one
1231 */
1232static int __wa_xfer_setup(struct wa_xfer *xfer, struct urb *urb)
1233{
1234 int result;
1235 struct device *dev = &xfer->wa->usb_iface->dev;
1236 enum wa_xfer_type xfer_type = 0; /* shut up GCC */
1237 size_t xfer_hdr_size, cnt, transfer_size;
1238 struct wa_xfer_hdr *xfer_hdr0, *xfer_hdr;
1239
df365423
IPG
1240 result = __wa_xfer_setup_sizes(xfer, &xfer_type);
1241 if (result < 0)
1242 goto error_setup_sizes;
1243 xfer_hdr_size = result;
1244 result = __wa_xfer_setup_segs(xfer, xfer_hdr_size);
1245 if (result < 0) {
1246 dev_err(dev, "xfer %p: Failed to allocate %d segments: %d\n",
1247 xfer, xfer->segs, result);
1248 goto error_setup_segs;
1249 }
1250 /* Fill the first header */
1251 xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
1252 wa_xfer_id_init(xfer);
1253 __wa_xfer_setup_hdr0(xfer, xfer_hdr0, xfer_type, xfer_hdr_size);
1254
7a32d9be 1255 /* Fill remaining headers */
df365423 1256 xfer_hdr = xfer_hdr0;
7a32d9be
TP
1257 if (xfer_type == WA_XFER_TYPE_ISO) {
1258 xfer_hdr0->dwTransferLength =
2101242c 1259 cpu_to_le32(xfer->seg[0]->isoc_size);
7a32d9be 1260 for (cnt = 1; cnt < xfer->segs; cnt++) {
7a32d9be 1261 struct wa_xfer_packet_info_hwaiso *packet_desc;
2101242c 1262 struct wa_seg *seg = xfer->seg[cnt];
7a32d9be 1263
2101242c 1264 xfer_hdr = &seg->xfer_hdr;
7a32d9be
TP
1265 packet_desc = ((void *)xfer_hdr) + xfer_hdr_size;
1266 /*
2101242c
TP
1267 * Copy values from the 0th header. Segment specific
1268 * values are set below.
7a32d9be 1269 */
2101242c 1270 memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
7a32d9be
TP
1271 xfer_hdr->bTransferSegment = cnt;
1272 xfer_hdr->dwTransferLength =
2101242c
TP
1273 cpu_to_le32(seg->isoc_size);
1274 __wa_setup_isoc_packet_descr(packet_desc, xfer, seg);
1275 seg->status = WA_SEG_READY;
7a32d9be
TP
1276 }
1277 } else {
1278 transfer_size = urb->transfer_buffer_length;
1279 xfer_hdr0->dwTransferLength = transfer_size > xfer->seg_size ?
1280 cpu_to_le32(xfer->seg_size) :
1281 cpu_to_le32(transfer_size);
df365423 1282 transfer_size -= xfer->seg_size;
7a32d9be
TP
1283 for (cnt = 1; cnt < xfer->segs; cnt++) {
1284 xfer_hdr = &xfer->seg[cnt]->xfer_hdr;
1285 memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
1286 xfer_hdr->bTransferSegment = cnt;
1287 xfer_hdr->dwTransferLength =
1288 transfer_size > xfer->seg_size ?
1289 cpu_to_le32(xfer->seg_size)
1290 : cpu_to_le32(transfer_size);
1291 xfer->seg[cnt]->status = WA_SEG_READY;
1292 transfer_size -= xfer->seg_size;
1293 }
df365423
IPG
1294 }
1295 xfer_hdr->bTransferSegment |= 0x80; /* this is the last segment */
1296 result = 0;
1297error_setup_segs:
1298error_setup_sizes:
df365423
IPG
1299 return result;
1300}
1301
1302/*
1303 *
1304 *
1305 * rpipe->seg_lock is held!
1306 */
1307static int __wa_seg_submit(struct wa_rpipe *rpipe, struct wa_xfer *xfer,
679ee475 1308 struct wa_seg *seg, int *dto_done)
df365423
IPG
1309{
1310 int result;
679ee475
TP
1311
1312 /* default to done unless we encounter a multi-frame isoc segment. */
1313 *dto_done = 1;
1314
09d94cbd
TP
1315 /* submit the transfer request. */
1316 result = usb_submit_urb(&seg->tr_urb, GFP_ATOMIC);
df365423 1317 if (result < 0) {
7a32d9be
TP
1318 pr_err("%s: xfer %p#%u: REQ submit failed: %d\n",
1319 __func__, xfer, seg->index, result);
df365423
IPG
1320 goto error_seg_submit;
1321 }
7a32d9be
TP
1322 /* submit the isoc packet descriptor if present. */
1323 if (seg->isoc_pack_desc_urb) {
f07ddb9e
TP
1324 struct wahc *wa = xfer->wa;
1325
7a32d9be
TP
1326 result = usb_submit_urb(seg->isoc_pack_desc_urb, GFP_ATOMIC);
1327 if (result < 0) {
1328 pr_err("%s: xfer %p#%u: ISO packet descriptor submit failed: %d\n",
1329 __func__, xfer, seg->index, result);
1330 goto error_iso_pack_desc_submit;
1331 }
2101242c
TP
1332 xfer->dto_isoc_frame_index = 0;
1333 /*
1334 * If this segment contains more than one isoc frame, hold
1335 * onto the dto resource until we send all frames.
f07ddb9e 1336 * Only applies to non-Alereon devices.
2101242c 1337 */
f07ddb9e
TP
1338 if (((wa->quirks & WUSB_QUIRK_ALEREON_HWA_CONCAT_ISOC) == 0)
1339 && (seg->isoc_frame_count > 1))
2101242c 1340 *dto_done = 0;
7a32d9be 1341 }
09d94cbd 1342 /* submit the out data if this is an out request. */
df365423
IPG
1343 if (seg->dto_urb) {
1344 result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
1345 if (result < 0) {
7a32d9be
TP
1346 pr_err("%s: xfer %p#%u: DTO submit failed: %d\n",
1347 __func__, xfer, seg->index, result);
df365423
IPG
1348 goto error_dto_submit;
1349 }
1350 }
1351 seg->status = WA_SEG_SUBMITTED;
1352 rpipe_avail_dec(rpipe);
1353 return 0;
1354
1355error_dto_submit:
7a32d9be
TP
1356 usb_unlink_urb(seg->isoc_pack_desc_urb);
1357error_iso_pack_desc_submit:
09d94cbd 1358 usb_unlink_urb(&seg->tr_urb);
df365423
IPG
1359error_seg_submit:
1360 seg->status = WA_SEG_ERROR;
1361 seg->result = result;
2101242c 1362 *dto_done = 1;
df365423
IPG
1363 return result;
1364}
1365
1366/*
679ee475
TP
1367 * Execute more queued request segments until the maximum concurrent allowed.
1368 * Return true if the DTO resource was acquired and released.
df365423
IPG
1369 *
1370 * The ugly unlock/lock sequence on the error path is needed as the
1371 * xfer->lock normally nests the seg_lock and not viceversa.
df365423 1372 */
679ee475 1373static int __wa_xfer_delayed_run(struct wa_rpipe *rpipe, int *dto_waiting)
df365423 1374{
679ee475 1375 int result, dto_acquired = 0, dto_done = 0;
df365423
IPG
1376 struct device *dev = &rpipe->wa->usb_iface->dev;
1377 struct wa_seg *seg;
1378 struct wa_xfer *xfer;
1379 unsigned long flags;
1380
679ee475
TP
1381 *dto_waiting = 0;
1382
df365423
IPG
1383 spin_lock_irqsave(&rpipe->seg_lock, flags);
1384 while (atomic_read(&rpipe->segs_available) > 0
679ee475
TP
1385 && !list_empty(&rpipe->seg_list)
1386 && (dto_acquired = __wa_dto_try_get(rpipe->wa))) {
e9a088fa 1387 seg = list_first_entry(&(rpipe->seg_list), struct wa_seg,
df365423
IPG
1388 list_node);
1389 list_del(&seg->list_node);
1390 xfer = seg->xfer;
679ee475
TP
1391 result = __wa_seg_submit(rpipe, xfer, seg, &dto_done);
1392 /* release the dto resource if this RPIPE is done with it. */
1393 if (dto_done)
1394 __wa_dto_put(rpipe->wa);
b9c84be6
TP
1395 dev_dbg(dev, "xfer %p ID %08X#%u submitted from delayed [%d segments available] %d\n",
1396 xfer, wa_xfer_id(xfer), seg->index,
1397 atomic_read(&rpipe->segs_available), result);
df365423
IPG
1398 if (unlikely(result < 0)) {
1399 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
1400 spin_lock_irqsave(&xfer->lock, flags);
1401 __wa_xfer_abort(xfer);
1402 xfer->segs_done++;
1403 spin_unlock_irqrestore(&xfer->lock, flags);
1404 spin_lock_irqsave(&rpipe->seg_lock, flags);
1405 }
1406 }
679ee475
TP
1407 /*
1408 * Mark this RPIPE as waiting if dto was not acquired, there are
1409 * delayed segs and no active transfers to wake us up later.
1410 */
1411 if (!dto_acquired && !list_empty(&rpipe->seg_list)
1412 && (atomic_read(&rpipe->segs_available) ==
1413 le16_to_cpu(rpipe->descr.wRequests)))
1414 *dto_waiting = 1;
1415
df365423 1416 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
679ee475
TP
1417
1418 return dto_done;
1419}
1420
1421static void wa_xfer_delayed_run(struct wa_rpipe *rpipe)
1422{
1423 int dto_waiting;
1424 int dto_done = __wa_xfer_delayed_run(rpipe, &dto_waiting);
1425
1426 /*
1427 * If this RPIPE is waiting on the DTO resource, add it to the tail of
1428 * the waiting list.
1429 * Otherwise, if the WA DTO resource was acquired and released by
1430 * __wa_xfer_delayed_run, another RPIPE may have attempted to acquire
1431 * DTO and failed during that time. Check the delayed list and process
1432 * any waiters. Start searching from the next RPIPE index.
1433 */
1434 if (dto_waiting)
1435 wa_add_delayed_rpipe(rpipe->wa, rpipe);
1436 else if (dto_done)
1437 wa_check_for_delayed_rpipes(rpipe->wa);
df365423
IPG
1438}
1439
1440/*
1441 *
1442 * xfer->lock is taken
1443 *
1444 * On failure submitting we just stop submitting and return error;
1445 * wa_urb_enqueue_b() will execute the completion path
1446 */
1447static int __wa_xfer_submit(struct wa_xfer *xfer)
1448{
679ee475 1449 int result, dto_acquired = 0, dto_done = 0, dto_waiting = 0;
df365423
IPG
1450 struct wahc *wa = xfer->wa;
1451 struct device *dev = &wa->usb_iface->dev;
1452 unsigned cnt;
1453 struct wa_seg *seg;
1454 unsigned long flags;
1455 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
1456 size_t maxrequests = le16_to_cpu(rpipe->descr.wRequests);
1457 u8 available;
1458 u8 empty;
1459
df365423
IPG
1460 spin_lock_irqsave(&wa->xfer_list_lock, flags);
1461 list_add_tail(&xfer->list_node, &wa->xfer_list);
1462 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
1463
1464 BUG_ON(atomic_read(&rpipe->segs_available) > maxrequests);
1465 result = 0;
1466 spin_lock_irqsave(&rpipe->seg_lock, flags);
1467 for (cnt = 0; cnt < xfer->segs; cnt++) {
679ee475
TP
1468 int delay_seg = 1;
1469
df365423
IPG
1470 available = atomic_read(&rpipe->segs_available);
1471 empty = list_empty(&rpipe->seg_list);
1472 seg = xfer->seg[cnt];
679ee475
TP
1473 if (available && empty) {
1474 /*
1475 * Only attempt to acquire DTO if we have a segment
1476 * to send.
1477 */
1478 dto_acquired = __wa_dto_try_get(rpipe->wa);
1479 if (dto_acquired) {
1480 delay_seg = 0;
1481 result = __wa_seg_submit(rpipe, xfer, seg,
1482 &dto_done);
2101242c
TP
1483 dev_dbg(dev, "xfer %p ID 0x%08X#%u: available %u empty %u submitted\n",
1484 xfer, wa_xfer_id(xfer), cnt, available,
1485 empty);
679ee475
TP
1486 if (dto_done)
1487 __wa_dto_put(rpipe->wa);
1488
1489 if (result < 0) {
1490 __wa_xfer_abort(xfer);
1491 goto error_seg_submit;
1492 }
1493 }
1494 }
1495
1496 if (delay_seg) {
2101242c
TP
1497 dev_dbg(dev, "xfer %p ID 0x%08X#%u: available %u empty %u delayed\n",
1498 xfer, wa_xfer_id(xfer), cnt, available, empty);
df365423
IPG
1499 seg->status = WA_SEG_DELAYED;
1500 list_add_tail(&seg->list_node, &rpipe->seg_list);
df365423
IPG
1501 }
1502 xfer->segs_submitted++;
1503 }
df365423 1504error_seg_submit:
679ee475
TP
1505 /*
1506 * Mark this RPIPE as waiting if dto was not acquired, there are
1507 * delayed segs and no active transfers to wake us up later.
1508 */
1509 if (!dto_acquired && !list_empty(&rpipe->seg_list)
1510 && (atomic_read(&rpipe->segs_available) ==
1511 le16_to_cpu(rpipe->descr.wRequests)))
1512 dto_waiting = 1;
df365423 1513 spin_unlock_irqrestore(&rpipe->seg_lock, flags);
679ee475
TP
1514
1515 if (dto_waiting)
1516 wa_add_delayed_rpipe(rpipe->wa, rpipe);
1517 else if (dto_done)
1518 wa_check_for_delayed_rpipes(rpipe->wa);
1519
df365423
IPG
1520 return result;
1521}
1522
1523/*
1524 * Second part of a URB/transfer enqueuement
1525 *
1526 * Assumes this comes from wa_urb_enqueue() [maybe through
1527 * wa_urb_enqueue_run()]. At this point:
1528 *
1529 * xfer->wa filled and refcounted
1530 * xfer->ep filled with rpipe refcounted if
1531 * delayed == 0
1532 * xfer->urb filled and refcounted (this is the case when called
1533 * from wa_urb_enqueue() as we come from usb_submit_urb()
1534 * and when called by wa_urb_enqueue_run(), as we took an
1535 * extra ref dropped by _run() after we return).
1536 * xfer->gfp filled
1537 *
1538 * If we fail at __wa_xfer_submit(), then we just check if we are done
1539 * and if so, we run the completion procedure. However, if we are not
1540 * yet done, we do nothing and wait for the completion handlers from
1541 * the submitted URBs or from the xfer-result path to kick in. If xfer
1542 * result never kicks in, the xfer will timeout from the USB code and
1543 * dequeue() will be called.
1544 */
33186c44 1545static int wa_urb_enqueue_b(struct wa_xfer *xfer)
df365423
IPG
1546{
1547 int result;
1548 unsigned long flags;
1549 struct urb *urb = xfer->urb;
1550 struct wahc *wa = xfer->wa;
1551 struct wusbhc *wusbhc = wa->wusb;
df365423
IPG
1552 struct wusb_dev *wusb_dev;
1553 unsigned done;
1554
df365423 1555 result = rpipe_get_by_ep(wa, xfer->ep, urb, xfer->gfp);
33186c44
TP
1556 if (result < 0) {
1557 pr_err("%s: error_rpipe_get\n", __func__);
df365423 1558 goto error_rpipe_get;
33186c44 1559 }
df365423
IPG
1560 result = -ENODEV;
1561 /* FIXME: segmentation broken -- kills DWA */
1562 mutex_lock(&wusbhc->mutex); /* get a WUSB dev */
49fa0921
JS
1563 if (urb->dev == NULL) {
1564 mutex_unlock(&wusbhc->mutex);
33186c44 1565 pr_err("%s: error usb dev gone\n", __func__);
df365423 1566 goto error_dev_gone;
49fa0921 1567 }
df365423
IPG
1568 wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev);
1569 if (wusb_dev == NULL) {
1570 mutex_unlock(&wusbhc->mutex);
bbfc3420
TP
1571 dev_err(&(urb->dev->dev), "%s: error wusb dev gone\n",
1572 __func__);
df365423
IPG
1573 goto error_dev_gone;
1574 }
1575 mutex_unlock(&wusbhc->mutex);
1576
1577 spin_lock_irqsave(&xfer->lock, flags);
1578 xfer->wusb_dev = wusb_dev;
1579 result = urb->status;
33186c44 1580 if (urb->status != -EINPROGRESS) {
bbfc3420 1581 dev_err(&(urb->dev->dev), "%s: error_dequeued\n", __func__);
df365423 1582 goto error_dequeued;
33186c44 1583 }
df365423
IPG
1584
1585 result = __wa_xfer_setup(xfer, urb);
33186c44 1586 if (result < 0) {
bbfc3420 1587 dev_err(&(urb->dev->dev), "%s: error_xfer_setup\n", __func__);
df365423 1588 goto error_xfer_setup;
33186c44 1589 }
df365423 1590 result = __wa_xfer_submit(xfer);
33186c44 1591 if (result < 0) {
bbfc3420 1592 dev_err(&(urb->dev->dev), "%s: error_xfer_submit\n", __func__);
df365423 1593 goto error_xfer_submit;
33186c44 1594 }
df365423 1595 spin_unlock_irqrestore(&xfer->lock, flags);
33186c44 1596 return 0;
df365423 1597
33186c44
TP
1598 /*
1599 * this is basically wa_xfer_completion() broken up wa_xfer_giveback()
1600 * does a wa_xfer_put() that will call wa_xfer_destroy() and undo
1601 * setup().
df365423
IPG
1602 */
1603error_xfer_setup:
1604error_dequeued:
1605 spin_unlock_irqrestore(&xfer->lock, flags);
1606 /* FIXME: segmentation broken, kills DWA */
1607 if (wusb_dev)
1608 wusb_dev_put(wusb_dev);
1609error_dev_gone:
1610 rpipe_put(xfer->ep->hcpriv);
1611error_rpipe_get:
1612 xfer->result = result;
33186c44 1613 return result;
df365423
IPG
1614
1615error_xfer_submit:
1616 done = __wa_xfer_is_done(xfer);
1617 xfer->result = result;
1618 spin_unlock_irqrestore(&xfer->lock, flags);
1619 if (done)
1620 wa_xfer_completion(xfer);
33186c44
TP
1621 /* return success since the completion routine will run. */
1622 return 0;
df365423
IPG
1623}
1624
1625/*
1626 * Execute the delayed transfers in the Wire Adapter @wa
1627 *
1628 * We need to be careful here, as dequeue() could be called in the
1629 * middle. That's why we do the whole thing under the
e9a088fa 1630 * wa->xfer_list_lock. If dequeue() jumps in, it first locks xfer->lock
df365423 1631 * and then checks the list -- so as we would be acquiring in inverse
e9a088fa
TP
1632 * order, we move the delayed list to a separate list while locked and then
1633 * submit them without the list lock held.
df365423
IPG
1634 */
1635void wa_urb_enqueue_run(struct work_struct *ws)
1636{
6d33f7bb 1637 struct wahc *wa = container_of(ws, struct wahc, xfer_enqueue_work);
df365423
IPG
1638 struct wa_xfer *xfer, *next;
1639 struct urb *urb;
e9a088fa 1640 LIST_HEAD(tmp_list);
df365423 1641
e9a088fa 1642 /* Create a copy of the wa->xfer_delayed_list while holding the lock */
df365423 1643 spin_lock_irq(&wa->xfer_list_lock);
e9a088fa
TP
1644 list_cut_position(&tmp_list, &wa->xfer_delayed_list,
1645 wa->xfer_delayed_list.prev);
1646 spin_unlock_irq(&wa->xfer_list_lock);
1647
1648 /*
1649 * enqueue from temp list without list lock held since wa_urb_enqueue_b
1650 * can take xfer->lock as well as lock mutexes.
1651 */
1652 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
df365423 1653 list_del_init(&xfer->list_node);
df365423
IPG
1654
1655 urb = xfer->urb;
33186c44
TP
1656 if (wa_urb_enqueue_b(xfer) < 0)
1657 wa_xfer_giveback(xfer);
df365423 1658 usb_put_urb(urb); /* taken when queuing */
df365423 1659 }
df365423
IPG
1660}
1661EXPORT_SYMBOL_GPL(wa_urb_enqueue_run);
1662
6d33f7bb
TP
1663/*
1664 * Process the errored transfers on the Wire Adapter outside of interrupt.
1665 */
1666void wa_process_errored_transfers_run(struct work_struct *ws)
1667{
1668 struct wahc *wa = container_of(ws, struct wahc, xfer_error_work);
1669 struct wa_xfer *xfer, *next;
1670 LIST_HEAD(tmp_list);
1671
1672 pr_info("%s: Run delayed STALL processing.\n", __func__);
1673
1674 /* Create a copy of the wa->xfer_errored_list while holding the lock */
1675 spin_lock_irq(&wa->xfer_list_lock);
1676 list_cut_position(&tmp_list, &wa->xfer_errored_list,
1677 wa->xfer_errored_list.prev);
1678 spin_unlock_irq(&wa->xfer_list_lock);
1679
1680 /*
1681 * run rpipe_clear_feature_stalled from temp list without list lock
1682 * held.
1683 */
1684 list_for_each_entry_safe(xfer, next, &tmp_list, list_node) {
1685 struct usb_host_endpoint *ep;
1686 unsigned long flags;
1687 struct wa_rpipe *rpipe;
1688
1689 spin_lock_irqsave(&xfer->lock, flags);
1690 ep = xfer->ep;
1691 rpipe = ep->hcpriv;
1692 spin_unlock_irqrestore(&xfer->lock, flags);
1693
1694 /* clear RPIPE feature stalled without holding a lock. */
1695 rpipe_clear_feature_stalled(wa, ep);
1696
1697 /* complete the xfer. This removes it from the tmp list. */
1698 wa_xfer_completion(xfer);
1699
1700 /* check for work. */
1701 wa_xfer_delayed_run(rpipe);
1702 }
1703}
1704EXPORT_SYMBOL_GPL(wa_process_errored_transfers_run);
1705
df365423
IPG
1706/*
1707 * Submit a transfer to the Wire Adapter in a delayed way
1708 *
1709 * The process of enqueuing involves possible sleeps() [see
1710 * enqueue_b(), for the rpipe_get() and the mutex_lock()]. If we are
1711 * in an atomic section, we defer the enqueue_b() call--else we call direct.
1712 *
1713 * @urb: We own a reference to it done by the HCI Linux USB stack that
1714 * will be given up by calling usb_hcd_giveback_urb() or by
1715 * returning error from this function -> ergo we don't have to
1716 * refcount it.
1717 */
1718int wa_urb_enqueue(struct wahc *wa, struct usb_host_endpoint *ep,
1719 struct urb *urb, gfp_t gfp)
1720{
1721 int result;
1722 struct device *dev = &wa->usb_iface->dev;
1723 struct wa_xfer *xfer;
1724 unsigned long my_flags;
1725 unsigned cant_sleep = irqs_disabled() | in_atomic();
1726
2b81c083
TP
1727 if ((urb->transfer_buffer == NULL)
1728 && (urb->sg == NULL)
df365423
IPG
1729 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1730 && urb->transfer_buffer_length != 0) {
1731 dev_err(dev, "BUG? urb %p: NULL xfer buffer & NODMA\n", urb);
1732 dump_stack();
1733 }
1734
b374487e
TP
1735 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1736 result = usb_hcd_link_urb_to_ep(&(wa->wusb->usb_hcd), urb);
1737 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
1738 if (result < 0)
1739 goto error_link_urb;
1740
df365423
IPG
1741 result = -ENOMEM;
1742 xfer = kzalloc(sizeof(*xfer), gfp);
1743 if (xfer == NULL)
1744 goto error_kmalloc;
1745
1746 result = -ENOENT;
1747 if (urb->status != -EINPROGRESS) /* cancelled */
1748 goto error_dequeued; /* before starting? */
1749 wa_xfer_init(xfer);
1750 xfer->wa = wa_get(wa);
1751 xfer->urb = urb;
1752 xfer->gfp = gfp;
1753 xfer->ep = ep;
1754 urb->hcpriv = xfer;
bce83697
DV
1755
1756 dev_dbg(dev, "xfer %p urb %p pipe 0x%02x [%d bytes] %s %s %s\n",
1757 xfer, urb, urb->pipe, urb->transfer_buffer_length,
1758 urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? "dma" : "nodma",
1759 urb->pipe & USB_DIR_IN ? "inbound" : "outbound",
1760 cant_sleep ? "deferred" : "inline");
1761
df365423
IPG
1762 if (cant_sleep) {
1763 usb_get_urb(urb);
1764 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1765 list_add_tail(&xfer->list_node, &wa->xfer_delayed_list);
1766 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
6d33f7bb 1767 queue_work(wusbd, &wa->xfer_enqueue_work);
df365423 1768 } else {
33186c44
TP
1769 result = wa_urb_enqueue_b(xfer);
1770 if (result < 0) {
1771 /*
1772 * URB submit/enqueue failed. Clean up, return an
1773 * error and do not run the callback. This avoids
1774 * an infinite submit/complete loop.
1775 */
1776 dev_err(dev, "%s: URB enqueue failed: %d\n",
1777 __func__, result);
1778 wa_put(xfer->wa);
1779 wa_xfer_put(xfer);
b374487e
TP
1780 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1781 usb_hcd_unlink_urb_from_ep(&(wa->wusb->usb_hcd), urb);
1782 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
33186c44
TP
1783 return result;
1784 }
df365423 1785 }
df365423
IPG
1786 return 0;
1787
1788error_dequeued:
1789 kfree(xfer);
1790error_kmalloc:
b374487e
TP
1791 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1792 usb_hcd_unlink_urb_from_ep(&(wa->wusb->usb_hcd), urb);
1793 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
1794error_link_urb:
df365423
IPG
1795 return result;
1796}
1797EXPORT_SYMBOL_GPL(wa_urb_enqueue);
1798
1799/*
1800 * Dequeue a URB and make sure uwb_hcd_giveback_urb() [completion
1801 * handler] is called.
1802 *
1803 * Until a transfer goes successfully through wa_urb_enqueue() it
1804 * needs to be dequeued with completion calling; when stuck in delayed
1805 * or before wa_xfer_setup() is called, we need to do completion.
1806 *
1807 * not setup If there is no hcpriv yet, that means that that enqueue
1808 * still had no time to set the xfer up. Because
1809 * urb->status should be other than -EINPROGRESS,
1810 * enqueue() will catch that and bail out.
1811 *
1812 * If the transfer has gone through setup, we just need to clean it
1813 * up. If it has gone through submit(), we have to abort it [with an
1814 * asynch request] and then make sure we cancel each segment.
1815 *
1816 */
b374487e 1817int wa_urb_dequeue(struct wahc *wa, struct urb *urb, int status)
df365423 1818{
df365423
IPG
1819 unsigned long flags, flags2;
1820 struct wa_xfer *xfer;
1821 struct wa_seg *seg;
1822 struct wa_rpipe *rpipe;
14e1d2df 1823 unsigned cnt, done = 0, xfer_abort_pending;
df365423 1824 unsigned rpipe_ready = 0;
b374487e
TP
1825 int result;
1826
1827 /* check if it is safe to unlink. */
1828 spin_lock_irqsave(&wa->xfer_list_lock, flags);
1829 result = usb_hcd_check_unlink_urb(&(wa->wusb->usb_hcd), urb, status);
1830 spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
1831 if (result)
1832 return result;
df365423 1833
df365423
IPG
1834 xfer = urb->hcpriv;
1835 if (xfer == NULL) {
6d33f7bb
TP
1836 /*
1837 * Nothing setup yet enqueue will see urb->status !=
df365423
IPG
1838 * -EINPROGRESS (by hcd layer) and bail out with
1839 * error, no need to do completion
1840 */
1841 BUG_ON(urb->status == -EINPROGRESS);
1842 goto out;
1843 }
1844 spin_lock_irqsave(&xfer->lock, flags);
14e1d2df 1845 pr_debug("%s: DEQUEUE xfer id 0x%08X\n", __func__, wa_xfer_id(xfer));
df365423 1846 rpipe = xfer->ep->hcpriv;
ec58fad1 1847 if (rpipe == NULL) {
bbfc3420
TP
1848 pr_debug("%s: xfer %p id 0x%08X has no RPIPE. %s",
1849 __func__, xfer, wa_xfer_id(xfer),
ec58fad1
TP
1850 "Probably already aborted.\n" );
1851 goto out_unlock;
1852 }
df365423
IPG
1853 /* Check the delayed list -> if there, release and complete */
1854 spin_lock_irqsave(&wa->xfer_list_lock, flags2);
1855 if (!list_empty(&xfer->list_node) && xfer->seg == NULL)
1856 goto dequeue_delayed;
1857 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1858 if (xfer->seg == NULL) /* still hasn't reached */
1859 goto out_unlock; /* setup(), enqueue_b() completes */
1860 /* Ok, the xfer is in flight already, it's been setup and submitted.*/
14e1d2df 1861 xfer_abort_pending = __wa_xfer_abort(xfer) >= 0;
df365423
IPG
1862 for (cnt = 0; cnt < xfer->segs; cnt++) {
1863 seg = xfer->seg[cnt];
14e1d2df
TP
1864 pr_debug("%s: xfer id 0x%08X#%d status = %d\n",
1865 __func__, wa_xfer_id(xfer), cnt, seg->status);
df365423
IPG
1866 switch (seg->status) {
1867 case WA_SEG_NOTREADY:
1868 case WA_SEG_READY:
1869 printk(KERN_ERR "xfer %p#%u: dequeue bad state %u\n",
1870 xfer, cnt, seg->status);
1871 WARN_ON(1);
1872 break;
1873 case WA_SEG_DELAYED:
14e1d2df
TP
1874 /*
1875 * delete from rpipe delayed list. If no segments on
1876 * this xfer have been submitted, __wa_xfer_is_done will
1877 * trigger a giveback below. Otherwise, the submitted
1878 * segments will be completed in the DTI interrupt.
1879 */
df365423
IPG
1880 seg->status = WA_SEG_ABORTED;
1881 spin_lock_irqsave(&rpipe->seg_lock, flags2);
1882 list_del(&seg->list_node);
1883 xfer->segs_done++;
df365423
IPG
1884 spin_unlock_irqrestore(&rpipe->seg_lock, flags2);
1885 break;
df365423
IPG
1886 case WA_SEG_DONE:
1887 case WA_SEG_ERROR:
1888 case WA_SEG_ABORTED:
1889 break;
14e1d2df
TP
1890 /*
1891 * In the states below, the HWA device already knows
1892 * about the transfer. If an abort request was sent,
1893 * allow the HWA to process it and wait for the
1894 * results. Otherwise, the DTI state and seg completed
1895 * counts can get out of sync.
1896 */
1897 case WA_SEG_SUBMITTED:
1898 case WA_SEG_PENDING:
1899 case WA_SEG_DTI_PENDING:
1900 /*
1901 * Check if the abort was successfully sent. This could
1902 * be false if the HWA has been removed but we haven't
1903 * gotten the disconnect notification yet.
1904 */
1905 if (!xfer_abort_pending) {
1906 seg->status = WA_SEG_ABORTED;
1907 rpipe_ready = rpipe_avail_inc(rpipe);
1908 xfer->segs_done++;
1909 }
1910 break;
df365423
IPG
1911 }
1912 }
1913 xfer->result = urb->status; /* -ENOENT or -ECONNRESET */
14e1d2df 1914 done = __wa_xfer_is_done(xfer);
df365423 1915 spin_unlock_irqrestore(&xfer->lock, flags);
14e1d2df
TP
1916 if (done)
1917 wa_xfer_completion(xfer);
df365423
IPG
1918 if (rpipe_ready)
1919 wa_xfer_delayed_run(rpipe);
df365423
IPG
1920 return 0;
1921
1922out_unlock:
1923 spin_unlock_irqrestore(&xfer->lock, flags);
1924out:
df365423
IPG
1925 return 0;
1926
1927dequeue_delayed:
1928 list_del_init(&xfer->list_node);
1929 spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1930 xfer->result = urb->status;
1931 spin_unlock_irqrestore(&xfer->lock, flags);
1932 wa_xfer_giveback(xfer);
1933 usb_put_urb(urb); /* we got a ref in enqueue() */
df365423
IPG
1934 return 0;
1935}
1936EXPORT_SYMBOL_GPL(wa_urb_dequeue);
1937
1938/*
1939 * Translation from WA status codes (WUSB1.0 Table 8.15) to errno
1940 * codes
1941 *
1942 * Positive errno values are internal inconsistencies and should be
1943 * flagged louder. Negative are to be passed up to the user in the
1944 * normal way.
1945 *
1946 * @status: USB WA status code -- high two bits are stripped.
1947 */
1948static int wa_xfer_status_to_errno(u8 status)
1949{
1950 int errno;
1951 u8 real_status = status;
1952 static int xlat[] = {
1953 [WA_XFER_STATUS_SUCCESS] = 0,
1954 [WA_XFER_STATUS_HALTED] = -EPIPE,
1955 [WA_XFER_STATUS_DATA_BUFFER_ERROR] = -ENOBUFS,
1956 [WA_XFER_STATUS_BABBLE] = -EOVERFLOW,
1957 [WA_XFER_RESERVED] = EINVAL,
1958 [WA_XFER_STATUS_NOT_FOUND] = 0,
1959 [WA_XFER_STATUS_INSUFFICIENT_RESOURCE] = -ENOMEM,
1960 [WA_XFER_STATUS_TRANSACTION_ERROR] = -EILSEQ,
1961 [WA_XFER_STATUS_ABORTED] = -EINTR,
1962 [WA_XFER_STATUS_RPIPE_NOT_READY] = EINVAL,
1963 [WA_XFER_INVALID_FORMAT] = EINVAL,
1964 [WA_XFER_UNEXPECTED_SEGMENT_NUMBER] = EINVAL,
1965 [WA_XFER_STATUS_RPIPE_TYPE_MISMATCH] = EINVAL,
1966 };
1967 status &= 0x3f;
1968
1969 if (status == 0)
1970 return 0;
1971 if (status >= ARRAY_SIZE(xlat)) {
9708cd2f 1972 printk_ratelimited(KERN_ERR "%s(): BUG? "
df365423
IPG
1973 "Unknown WA transfer status 0x%02x\n",
1974 __func__, real_status);
1975 return -EINVAL;
1976 }
1977 errno = xlat[status];
1978 if (unlikely(errno > 0)) {
9708cd2f 1979 printk_ratelimited(KERN_ERR "%s(): BUG? "
df365423
IPG
1980 "Inconsistent WA status: 0x%02x\n",
1981 __func__, real_status);
1982 errno = -errno;
1983 }
1984 return errno;
1985}
1986
14e1d2df
TP
1987/*
1988 * If a last segment flag and/or a transfer result error is encountered,
1989 * no other segment transfer results will be returned from the device.
1990 * Mark the remaining submitted or pending xfers as completed so that
1991 * the xfer will complete cleanly.
1992 */
1993static void wa_complete_remaining_xfer_segs(struct wa_xfer *xfer,
1994 struct wa_seg *incoming_seg)
1995{
1996 int index;
1997 struct wa_rpipe *rpipe = xfer->ep->hcpriv;
1998
1999 for (index = incoming_seg->index + 1; index < xfer->segs_submitted;
2000 index++) {
2001 struct wa_seg *current_seg = xfer->seg[index];
2002
2003 BUG_ON(current_seg == NULL);
2004
2005 switch (current_seg->status) {
2006 case WA_SEG_SUBMITTED:
2007 case WA_SEG_PENDING:
2008 case WA_SEG_DTI_PENDING:
2009 rpipe_avail_inc(rpipe);
2010 /*
2011 * do not increment RPIPE avail for the WA_SEG_DELAYED case
2012 * since it has not been submitted to the RPIPE.
2013 */
2014 case WA_SEG_DELAYED:
2015 xfer->segs_done++;
2016 current_seg->status = incoming_seg->status;
2017 break;
2018 case WA_SEG_ABORTED:
2019 break;
2020 default:
2021 WARN(1, "%s: xfer 0x%08X#%d. bad seg status = %d\n",
2022 __func__, wa_xfer_id(xfer), index,
2023 current_seg->status);
2024 break;
2025 }
2026 }
2027}
2028
df365423
IPG
2029/*
2030 * Process a xfer result completion message
2031 *
14e1d2df 2032 * inbound transfers: need to schedule a buf_in_urb read
df365423 2033 *
6d33f7bb 2034 * FIXME: this function needs to be broken up in parts
df365423 2035 */
0367eef2
TP
2036static void wa_xfer_result_chew(struct wahc *wa, struct wa_xfer *xfer,
2037 struct wa_xfer_result *xfer_result)
df365423
IPG
2038{
2039 int result;
2040 struct device *dev = &wa->usb_iface->dev;
2041 unsigned long flags;
2042 u8 seg_idx;
2043 struct wa_seg *seg;
2044 struct wa_rpipe *rpipe;
0367eef2 2045 unsigned done = 0;
df365423
IPG
2046 u8 usb_status;
2047 unsigned rpipe_ready = 0;
2048
df365423
IPG
2049 spin_lock_irqsave(&xfer->lock, flags);
2050 seg_idx = xfer_result->bTransferSegment & 0x7f;
2051 if (unlikely(seg_idx >= xfer->segs))
2052 goto error_bad_seg;
2053 seg = xfer->seg[seg_idx];
2054 rpipe = xfer->ep->hcpriv;
2055 usb_status = xfer_result->bTransferStatus;
b9c84be6
TP
2056 dev_dbg(dev, "xfer %p ID 0x%08X#%u: bTransferStatus 0x%02x (seg status %u)\n",
2057 xfer, wa_xfer_id(xfer), seg_idx, usb_status, seg->status);
df365423
IPG
2058 if (seg->status == WA_SEG_ABORTED
2059 || seg->status == WA_SEG_ERROR) /* already handled */
2060 goto segment_aborted;
2061 if (seg->status == WA_SEG_SUBMITTED) /* ops, got here */
2062 seg->status = WA_SEG_PENDING; /* before wa_seg{_dto}_cb() */
2063 if (seg->status != WA_SEG_PENDING) {
2064 if (printk_ratelimit())
2065 dev_err(dev, "xfer %p#%u: Bad segment state %u\n",
2066 xfer, seg_idx, seg->status);
2067 seg->status = WA_SEG_PENDING; /* workaround/"fix" it */
2068 }
2069 if (usb_status & 0x80) {
2070 seg->result = wa_xfer_status_to_errno(usb_status);
2b81c083
TP
2071 dev_err(dev, "DTI: xfer %p#:%08X:%u failed (0x%02x)\n",
2072 xfer, xfer->id, seg->index, usb_status);
14e1d2df
TP
2073 seg->status = ((usb_status & 0x7F) == WA_XFER_STATUS_ABORTED) ?
2074 WA_SEG_ABORTED : WA_SEG_ERROR;
df365423
IPG
2075 goto error_complete;
2076 }
2077 /* FIXME: we ignore warnings, tally them for stats */
2078 if (usb_status & 0x40) /* Warning?... */
2079 usb_status = 0; /* ... pass */
7a32d9be
TP
2080 if (usb_pipeisoc(xfer->urb->pipe)) {
2081 /* set up WA state to read the isoc packet status next. */
2082 wa->dti_isoc_xfer_in_progress = wa_xfer_id(xfer);
2083 wa->dti_isoc_xfer_seg = seg_idx;
2084 wa->dti_state = WA_DTI_ISOC_PACKET_STATUS_PENDING;
2085 } else if (xfer->is_inbound) { /* IN data phase: read to buffer */
df365423
IPG
2086 seg->status = WA_SEG_DTI_PENDING;
2087 BUG_ON(wa->buf_in_urb->status == -EINPROGRESS);
2b81c083
TP
2088 /* this should always be 0 before a resubmit. */
2089 wa->buf_in_urb->num_mapped_sgs = 0;
2090
df365423
IPG
2091 if (xfer->is_dma) {
2092 wa->buf_in_urb->transfer_dma =
2093 xfer->urb->transfer_dma
2b81c083 2094 + (seg_idx * xfer->seg_size);
df365423
IPG
2095 wa->buf_in_urb->transfer_flags
2096 |= URB_NO_TRANSFER_DMA_MAP;
2b81c083
TP
2097 wa->buf_in_urb->transfer_buffer = NULL;
2098 wa->buf_in_urb->sg = NULL;
2099 wa->buf_in_urb->num_sgs = 0;
df365423 2100 } else {
2b81c083 2101 /* do buffer or SG processing. */
df365423
IPG
2102 wa->buf_in_urb->transfer_flags
2103 &= ~URB_NO_TRANSFER_DMA_MAP;
2b81c083
TP
2104
2105 if (xfer->urb->transfer_buffer) {
2106 wa->buf_in_urb->transfer_buffer =
2107 xfer->urb->transfer_buffer
2108 + (seg_idx * xfer->seg_size);
2109 wa->buf_in_urb->sg = NULL;
2110 wa->buf_in_urb->num_sgs = 0;
2111 } else {
2112 /* allocate an SG list to store seg_size bytes
2113 and copy the subset of the xfer->urb->sg
2114 that matches the buffer subset we are
2115 about to read. */
2116 wa->buf_in_urb->sg = wa_xfer_create_subset_sg(
2117 xfer->urb->sg,
2118 seg_idx * xfer->seg_size,
2119 le32_to_cpu(
2120 xfer_result->dwTransferLength),
2121 &(wa->buf_in_urb->num_sgs));
2122
2123 if (!(wa->buf_in_urb->sg)) {
2124 wa->buf_in_urb->num_sgs = 0;
2125 goto error_sg_alloc;
2126 }
2127 wa->buf_in_urb->transfer_buffer = NULL;
2128 }
df365423
IPG
2129 }
2130 wa->buf_in_urb->transfer_buffer_length =
2131 le32_to_cpu(xfer_result->dwTransferLength);
2132 wa->buf_in_urb->context = seg;
2133 result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
2134 if (result < 0)
2135 goto error_submit_buf_in;
2136 } else {
2137 /* OUT data phase, complete it -- */
2138 seg->status = WA_SEG_DONE;
2139 seg->result = le32_to_cpu(xfer_result->dwTransferLength);
2140 xfer->segs_done++;
2141 rpipe_ready = rpipe_avail_inc(rpipe);
2142 done = __wa_xfer_is_done(xfer);
2143 }
2144 spin_unlock_irqrestore(&xfer->lock, flags);
2145 if (done)
2146 wa_xfer_completion(xfer);
2147 if (rpipe_ready)
2148 wa_xfer_delayed_run(rpipe);
df365423
IPG
2149 return;
2150
df365423
IPG
2151error_submit_buf_in:
2152 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
2153 dev_err(dev, "DTI: URB max acceptable errors "
2154 "exceeded, resetting device\n");
2155 wa_reset_all(wa);
2156 }
2157 if (printk_ratelimit())
2158 dev_err(dev, "xfer %p#%u: can't submit DTI data phase: %d\n",
2159 xfer, seg_idx, result);
2160 seg->result = result;
2b81c083 2161 kfree(wa->buf_in_urb->sg);
6741448e 2162 wa->buf_in_urb->sg = NULL;
2b81c083 2163error_sg_alloc:
6d33f7bb 2164 __wa_xfer_abort(xfer);
df365423 2165 seg->status = WA_SEG_ERROR;
14e1d2df 2166error_complete:
df365423
IPG
2167 xfer->segs_done++;
2168 rpipe_ready = rpipe_avail_inc(rpipe);
14e1d2df 2169 wa_complete_remaining_xfer_segs(xfer, seg);
df365423 2170 done = __wa_xfer_is_done(xfer);
6d33f7bb
TP
2171 /*
2172 * queue work item to clear STALL for control endpoints.
2173 * Otherwise, let endpoint_reset take care of it.
2174 */
2175 if (((usb_status & 0x3f) == WA_XFER_STATUS_HALTED) &&
2176 usb_endpoint_xfer_control(&xfer->ep->desc) &&
2177 done) {
2178
2179 dev_info(dev, "Control EP stall. Queue delayed work.\n");
2180 spin_lock_irq(&wa->xfer_list_lock);
8eb41299
WY
2181 /* move xfer from xfer_list to xfer_errored_list. */
2182 list_move_tail(&xfer->list_node, &wa->xfer_errored_list);
6d33f7bb
TP
2183 spin_unlock_irq(&wa->xfer_list_lock);
2184 spin_unlock_irqrestore(&xfer->lock, flags);
2185 queue_work(wusbd, &wa->xfer_error_work);
2186 } else {
2187 spin_unlock_irqrestore(&xfer->lock, flags);
2188 if (done)
2189 wa_xfer_completion(xfer);
2190 if (rpipe_ready)
2191 wa_xfer_delayed_run(rpipe);
2192 }
2193
df365423
IPG
2194 return;
2195
df365423
IPG
2196error_bad_seg:
2197 spin_unlock_irqrestore(&xfer->lock, flags);
b374487e 2198 wa_urb_dequeue(wa, xfer->urb, -ENOENT);
df365423
IPG
2199 if (printk_ratelimit())
2200 dev_err(dev, "xfer %p#%u: bad segment\n", xfer, seg_idx);
2201 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
2202 dev_err(dev, "DTI: URB max acceptable errors "
2203 "exceeded, resetting device\n");
2204 wa_reset_all(wa);
2205 }
df365423
IPG
2206 return;
2207
df365423
IPG
2208segment_aborted:
2209 /* nothing to do, as the aborter did the completion */
2210 spin_unlock_irqrestore(&xfer->lock, flags);
df365423
IPG
2211}
2212
7a32d9be
TP
2213/*
2214 * Process a isochronous packet status message
2215 *
2216 * inbound transfers: need to schedule a buf_in_urb read
2217 */
2218static void wa_process_iso_packet_status(struct wahc *wa, struct urb *urb)
2219{
2220 struct device *dev = &wa->usb_iface->dev;
2221 struct wa_xfer_packet_status_hwaiso *packet_status;
2101242c 2222 struct wa_xfer_packet_status_len_hwaiso *status_array;
7a32d9be
TP
2223 struct wa_xfer *xfer;
2224 unsigned long flags;
2225 struct wa_seg *seg;
2226 struct wa_rpipe *rpipe;
2227 unsigned done = 0;
2101242c
TP
2228 unsigned rpipe_ready = 0, seg_index;
2229 int expected_size;
7a32d9be
TP
2230
2231 /* We have a xfer result buffer; check it */
2232 dev_dbg(dev, "DTI: isoc packet status %d bytes at %p\n",
2233 urb->actual_length, urb->transfer_buffer);
7a32d9be 2234 packet_status = (struct wa_xfer_packet_status_hwaiso *)(wa->dti_buf);
7a32d9be
TP
2235 if (packet_status->bPacketType != WA_XFER_ISO_PACKET_STATUS) {
2236 dev_err(dev, "DTI Error: isoc packet status--bad type 0x%02x\n",
2237 packet_status->bPacketType);
2238 goto error_parse_buffer;
2239 }
2240 xfer = wa_xfer_get_by_id(wa, wa->dti_isoc_xfer_in_progress);
2241 if (xfer == NULL) {
2242 dev_err(dev, "DTI Error: isoc packet status--unknown xfer 0x%08x\n",
2243 wa->dti_isoc_xfer_in_progress);
2244 goto error_parse_buffer;
2245 }
2246 spin_lock_irqsave(&xfer->lock, flags);
2247 if (unlikely(wa->dti_isoc_xfer_seg >= xfer->segs))
2248 goto error_bad_seg;
2249 seg = xfer->seg[wa->dti_isoc_xfer_seg];
2250 rpipe = xfer->ep->hcpriv;
2101242c
TP
2251 expected_size = sizeof(*packet_status) +
2252 (sizeof(packet_status->PacketStatus[0]) *
2253 seg->isoc_frame_count);
2254 if (urb->actual_length != expected_size) {
2255 dev_err(dev, "DTI Error: isoc packet status--bad urb length (%d bytes vs %d needed)\n",
2256 urb->actual_length, expected_size);
2257 goto error_bad_seg;
2258 }
2259 if (le16_to_cpu(packet_status->wLength) != expected_size) {
2260 dev_err(dev, "DTI Error: isoc packet status--bad length %u\n",
2261 le16_to_cpu(packet_status->wLength));
2262 goto error_bad_seg;
2263 }
2264 /* isoc packet status and lengths back xfer urb. */
2265 status_array = packet_status->PacketStatus;
2266 for (seg_index = 0; seg_index < seg->isoc_frame_count; ++seg_index) {
2267 xfer->urb->iso_frame_desc[seg->index].status =
2268 wa_xfer_status_to_errno(
2269 le16_to_cpu(status_array[seg_index].PacketStatus));
2270 xfer->urb->iso_frame_desc[seg->index].actual_length =
2271 le16_to_cpu(status_array[seg_index].PacketLength);
2272 }
7a32d9be
TP
2273
2274 if (!xfer->is_inbound) {
2275 /* OUT transfer, complete it -- */
2276 seg->status = WA_SEG_DONE;
2277 xfer->segs_done++;
2278 rpipe_ready = rpipe_avail_inc(rpipe);
2279 done = __wa_xfer_is_done(xfer);
2280 }
2281 spin_unlock_irqrestore(&xfer->lock, flags);
2282 wa->dti_state = WA_DTI_TRANSFER_RESULT_PENDING;
2283 if (done)
2284 wa_xfer_completion(xfer);
2285 if (rpipe_ready)
2286 wa_xfer_delayed_run(rpipe);
2287 wa_xfer_put(xfer);
2288 return;
2289
2290error_bad_seg:
2291 spin_unlock_irqrestore(&xfer->lock, flags);
2292 wa_xfer_put(xfer);
2293error_parse_buffer:
2294 return;
2295}
2296
df365423
IPG
2297/*
2298 * Callback for the IN data phase
2299 *
af901ca1 2300 * If successful transition state; otherwise, take a note of the
df365423
IPG
2301 * error, mark this segment done and try completion.
2302 *
2303 * Note we don't access until we are sure that the transfer hasn't
2304 * been cancelled (ECONNRESET, ENOENT), which could mean that
2305 * seg->xfer could be already gone.
2306 */
2307static void wa_buf_in_cb(struct urb *urb)
2308{
2309 struct wa_seg *seg = urb->context;
2310 struct wa_xfer *xfer = seg->xfer;
2311 struct wahc *wa;
2312 struct device *dev;
2313 struct wa_rpipe *rpipe;
2314 unsigned rpipe_ready;
2315 unsigned long flags;
2316 u8 done = 0;
2317
2b81c083
TP
2318 /* free the sg if it was used. */
2319 kfree(urb->sg);
2320 urb->sg = NULL;
2321
df365423
IPG
2322 switch (urb->status) {
2323 case 0:
2324 spin_lock_irqsave(&xfer->lock, flags);
2325 wa = xfer->wa;
2326 dev = &wa->usb_iface->dev;
2327 rpipe = xfer->ep->hcpriv;
bce83697
DV
2328 dev_dbg(dev, "xfer %p#%u: data in done (%zu bytes)\n",
2329 xfer, seg->index, (size_t)urb->actual_length);
df365423
IPG
2330 seg->status = WA_SEG_DONE;
2331 seg->result = urb->actual_length;
2332 xfer->segs_done++;
2333 rpipe_ready = rpipe_avail_inc(rpipe);
2334 done = __wa_xfer_is_done(xfer);
2335 spin_unlock_irqrestore(&xfer->lock, flags);
2336 if (done)
2337 wa_xfer_completion(xfer);
2338 if (rpipe_ready)
2339 wa_xfer_delayed_run(rpipe);
2340 break;
2341 case -ECONNRESET: /* URB unlinked; no need to do anything */
2342 case -ENOENT: /* as it was done by the who unlinked us */
2343 break;
2344 default: /* Other errors ... */
2345 spin_lock_irqsave(&xfer->lock, flags);
2346 wa = xfer->wa;
2347 dev = &wa->usb_iface->dev;
2348 rpipe = xfer->ep->hcpriv;
2349 if (printk_ratelimit())
2350 dev_err(dev, "xfer %p#%u: data in error %d\n",
2351 xfer, seg->index, urb->status);
2352 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
2353 EDC_ERROR_TIMEFRAME)){
2354 dev_err(dev, "DTO: URB max acceptable errors "
2355 "exceeded, resetting device\n");
2356 wa_reset_all(wa);
2357 }
2358 seg->status = WA_SEG_ERROR;
2359 seg->result = urb->status;
2360 xfer->segs_done++;
2361 rpipe_ready = rpipe_avail_inc(rpipe);
2362 __wa_xfer_abort(xfer);
2363 done = __wa_xfer_is_done(xfer);
2364 spin_unlock_irqrestore(&xfer->lock, flags);
2365 if (done)
2366 wa_xfer_completion(xfer);
2367 if (rpipe_ready)
2368 wa_xfer_delayed_run(rpipe);
2369 }
df365423
IPG
2370}
2371
2372/*
2373 * Handle an incoming transfer result buffer
2374 *
2375 * Given a transfer result buffer, it completes the transfer (possibly
2376 * scheduling and buffer in read) and then resubmits the DTI URB for a
2377 * new transfer result read.
2378 *
2379 *
2380 * The xfer_result DTI URB state machine
2381 *
2382 * States: OFF | RXR (Read-Xfer-Result) | RBI (Read-Buffer-In)
2383 *
2384 * We start in OFF mode, the first xfer_result notification [through
2385 * wa_handle_notif_xfer()] moves us to RXR by posting the DTI-URB to
2386 * read.
2387 *
2388 * We receive a buffer -- if it is not a xfer_result, we complain and
2389 * repost the DTI-URB. If it is a xfer_result then do the xfer seg
2390 * request accounting. If it is an IN segment, we move to RBI and post
2391 * a BUF-IN-URB to the right buffer. The BUF-IN-URB callback will
2392 * repost the DTI-URB and move to RXR state. if there was no IN
2393 * segment, it will repost the DTI-URB.
2394 *
2395 * We go back to OFF when we detect a ENOENT or ESHUTDOWN (or too many
2396 * errors) in the URBs.
2397 */
0367eef2 2398static void wa_dti_cb(struct urb *urb)
df365423
IPG
2399{
2400 int result;
2401 struct wahc *wa = urb->context;
2402 struct device *dev = &wa->usb_iface->dev;
df365423 2403 u32 xfer_id;
df365423
IPG
2404 u8 usb_status;
2405
df365423
IPG
2406 BUG_ON(wa->dti_urb != urb);
2407 switch (wa->dti_urb->status) {
2408 case 0:
7a32d9be
TP
2409 if (wa->dti_state == WA_DTI_TRANSFER_RESULT_PENDING) {
2410 struct wa_xfer_result *xfer_result;
2411 struct wa_xfer *xfer;
2412
2413 /* We have a xfer result buffer; check it */
2414 dev_dbg(dev, "DTI: xfer result %d bytes at %p\n",
2415 urb->actual_length, urb->transfer_buffer);
2416 if (urb->actual_length != sizeof(*xfer_result)) {
2417 dev_err(dev, "DTI Error: xfer result--bad size xfer result (%d bytes vs %zu needed)\n",
2418 urb->actual_length,
2419 sizeof(*xfer_result));
2420 break;
2421 }
2422 xfer_result = (struct wa_xfer_result *)(wa->dti_buf);
2423 if (xfer_result->hdr.bLength != sizeof(*xfer_result)) {
2424 dev_err(dev, "DTI Error: xfer result--bad header length %u\n",
2425 xfer_result->hdr.bLength);
2426 break;
2427 }
2428 if (xfer_result->hdr.bNotifyType != WA_XFER_RESULT) {
2429 dev_err(dev, "DTI Error: xfer result--bad header type 0x%02x\n",
2430 xfer_result->hdr.bNotifyType);
2431 break;
2432 }
2433 usb_status = xfer_result->bTransferStatus & 0x3f;
2434 if (usb_status == WA_XFER_STATUS_NOT_FOUND)
2435 /* taken care of already */
2436 break;
2437 xfer_id = le32_to_cpu(xfer_result->dwTransferID);
2438 xfer = wa_xfer_get_by_id(wa, xfer_id);
2439 if (xfer == NULL) {
2440 /* FIXME: transaction not found. */
2441 dev_err(dev, "DTI Error: xfer result--unknown xfer 0x%08x (status 0x%02x)\n",
2442 xfer_id, usb_status);
2443 break;
2444 }
2445 wa_xfer_result_chew(wa, xfer, xfer_result);
2446 wa_xfer_put(xfer);
2447 } else if (wa->dti_state == WA_DTI_ISOC_PACKET_STATUS_PENDING) {
2448 wa_process_iso_packet_status(wa, urb);
2449 } else {
2450 dev_err(dev, "DTI Error: unexpected EP state = %d\n",
2451 wa->dti_state);
df365423 2452 }
df365423
IPG
2453 break;
2454 case -ENOENT: /* (we killed the URB)...so, no broadcast */
2455 case -ESHUTDOWN: /* going away! */
2456 dev_dbg(dev, "DTI: going down! %d\n", urb->status);
2457 goto out;
2458 default:
2459 /* Unknown error */
2460 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS,
2461 EDC_ERROR_TIMEFRAME)) {
2462 dev_err(dev, "DTI: URB max acceptable errors "
2463 "exceeded, resetting device\n");
2464 wa_reset_all(wa);
2465 goto out;
2466 }
2467 if (printk_ratelimit())
2468 dev_err(dev, "DTI: URB error %d\n", urb->status);
2469 break;
2470 }
2471 /* Resubmit the DTI URB */
2472 result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
2473 if (result < 0) {
2474 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
2475 "resetting\n", result);
2476 wa_reset_all(wa);
2477 }
2478out:
df365423
IPG
2479 return;
2480}
2481
2482/*
2483 * Transfer complete notification
2484 *
2485 * Called from the notif.c code. We get a notification on EP2 saying
2486 * that some endpoint has some transfer result data available. We are
2487 * about to read it.
2488 *
2489 * To speed up things, we always have a URB reading the DTI URB; we
2490 * don't really set it up and start it until the first xfer complete
2491 * notification arrives, which is what we do here.
2492 *
0367eef2 2493 * Follow up in wa_dti_cb(), as that's where the whole state
df365423
IPG
2494 * machine starts.
2495 *
2496 * So here we just initialize the DTI URB for reading transfer result
2497 * notifications and also the buffer-in URB, for reading buffers. Then
2498 * we just submit the DTI URB.
2499 *
2500 * @wa shall be referenced
2501 */
2502void wa_handle_notif_xfer(struct wahc *wa, struct wa_notif_hdr *notif_hdr)
2503{
2504 int result;
2505 struct device *dev = &wa->usb_iface->dev;
2506 struct wa_notif_xfer *notif_xfer;
2507 const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd;
2508
df365423
IPG
2509 notif_xfer = container_of(notif_hdr, struct wa_notif_xfer, hdr);
2510 BUG_ON(notif_hdr->bNotifyType != WA_NOTIF_TRANSFER);
2511
2512 if ((0x80 | notif_xfer->bEndpoint) != dti_epd->bEndpointAddress) {
2513 /* FIXME: hardcoded limitation, adapt */
2514 dev_err(dev, "BUG: DTI ep is %u, not %u (hack me)\n",
2515 notif_xfer->bEndpoint, dti_epd->bEndpointAddress);
2516 goto error;
2517 }
2518 if (wa->dti_urb != NULL) /* DTI URB already started */
2519 goto out;
2520
2521 wa->dti_urb = usb_alloc_urb(0, GFP_KERNEL);
2522 if (wa->dti_urb == NULL) {
2523 dev_err(dev, "Can't allocate DTI URB\n");
2524 goto error_dti_urb_alloc;
2525 }
2526 usb_fill_bulk_urb(
2527 wa->dti_urb, wa->usb_dev,
2528 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
0367eef2
TP
2529 wa->dti_buf, wa->dti_buf_size,
2530 wa_dti_cb, wa);
df365423
IPG
2531
2532 wa->buf_in_urb = usb_alloc_urb(0, GFP_KERNEL);
2533 if (wa->buf_in_urb == NULL) {
2534 dev_err(dev, "Can't allocate BUF-IN URB\n");
2535 goto error_buf_in_urb_alloc;
2536 }
2537 usb_fill_bulk_urb(
2538 wa->buf_in_urb, wa->usb_dev,
2539 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
2540 NULL, 0, wa_buf_in_cb, wa);
2541 result = usb_submit_urb(wa->dti_urb, GFP_KERNEL);
2542 if (result < 0) {
2543 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
2544 "resetting\n", result);
2545 goto error_dti_urb_submit;
2546 }
2547out:
df365423
IPG
2548 return;
2549
2550error_dti_urb_submit:
2551 usb_put_urb(wa->buf_in_urb);
6741448e 2552 wa->buf_in_urb = NULL;
df365423
IPG
2553error_buf_in_urb_alloc:
2554 usb_put_urb(wa->dti_urb);
2555 wa->dti_urb = NULL;
2556error_dti_urb_alloc:
2557error:
2558 wa_reset_all(wa);
df365423 2559}