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