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