Merge tag 'v3.11-rc7' into stable/for-linus-3.12
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / usb / wusbcore / wa-xfer.c
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  *
64  *     (a) set up a timer every time an rpipe's use count drops to 1
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
79  *     availability of the different required components (blocks,
80  *     rpipes, segment slots, etc), we go scheduling them. Painful.
81  */
82 #include <linux/init.h>
83 #include <linux/spinlock.h>
84 #include <linux/slab.h>
85 #include <linux/hash.h>
86 #include <linux/ratelimit.h>
87 #include <linux/export.h>
88 #include <linux/scatterlist.h>
89
90 #include "wa-hc.h"
91 #include "wusbhc.h"
92
93 enum {
94         WA_SEGS_MAX = 255,
95 };
96
97 enum 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
109 static void wa_xfer_delayed_run(struct wa_rpipe *);
110
111 /*
112  * Life cycle governed by 'struct urb' (the refcount of the struct is
113  * that of the 'struct urb' and usb_free_urb() would free the whole
114  * struct).
115  */
116 struct wa_seg {
117         struct urb urb;
118         struct urb *dto_urb;            /* for data output? */
119         struct list_head list_node;     /* for rpipe->req_list */
120         struct wa_xfer *xfer;           /* out xfer */
121         u8 index;                       /* which segment we are */
122         enum wa_seg_status status;
123         ssize_t result;                 /* bytes xfered or error */
124         struct wa_xfer_hdr xfer_hdr;
125         u8 xfer_extra[];                /* xtra space for xfer_hdr_ctl */
126 };
127
128 static void wa_seg_init(struct wa_seg *seg)
129 {
130         /* usb_init_urb() repeats a lot of work, so we do it here */
131         kref_init(&seg->urb.kref);
132 }
133
134 /*
135  * Protected by xfer->lock
136  *
137  */
138 struct wa_xfer {
139         struct kref refcnt;
140         struct list_head list_node;
141         spinlock_t lock;
142         u32 id;
143
144         struct wahc *wa;                /* Wire adapter we are plugged to */
145         struct usb_host_endpoint *ep;
146         struct urb *urb;                /* URB we are transferring for */
147         struct wa_seg **seg;            /* transfer segments */
148         u8 segs, segs_submitted, segs_done;
149         unsigned is_inbound:1;
150         unsigned is_dma:1;
151         size_t seg_size;
152         int result;
153
154         gfp_t gfp;                      /* allocation mask */
155
156         struct wusb_dev *wusb_dev;      /* for activity timestamps */
157 };
158
159 static inline void wa_xfer_init(struct wa_xfer *xfer)
160 {
161         kref_init(&xfer->refcnt);
162         INIT_LIST_HEAD(&xfer->list_node);
163         spin_lock_init(&xfer->lock);
164 }
165
166 /*
167  * Destroy a transfer structure
168  *
169  * Note that the xfer->seg[index] thingies follow the URB life cycle,
170  * so we need to put them, not free them.
171  */
172 static void wa_xfer_destroy(struct kref *_xfer)
173 {
174         struct wa_xfer *xfer = container_of(_xfer, struct wa_xfer, refcnt);
175         if (xfer->seg) {
176                 unsigned cnt;
177                 for (cnt = 0; cnt < xfer->segs; cnt++) {
178                         if (xfer->is_inbound)
179                                 usb_put_urb(xfer->seg[cnt]->dto_urb);
180                         usb_put_urb(&xfer->seg[cnt]->urb);
181                 }
182         }
183         kfree(xfer);
184 }
185
186 static void wa_xfer_get(struct wa_xfer *xfer)
187 {
188         kref_get(&xfer->refcnt);
189 }
190
191 static void wa_xfer_put(struct wa_xfer *xfer)
192 {
193         kref_put(&xfer->refcnt, wa_xfer_destroy);
194 }
195
196 /*
197  * xfer is referenced
198  *
199  * xfer->lock has to be unlocked
200  *
201  * We take xfer->lock for setting the result; this is a barrier
202  * against drivers/usb/core/hcd.c:unlink1() being called after we call
203  * usb_hcd_giveback_urb() and wa_urb_dequeue() trying to get a
204  * reference to the transfer.
205  */
206 static void wa_xfer_giveback(struct wa_xfer *xfer)
207 {
208         unsigned long flags;
209
210         spin_lock_irqsave(&xfer->wa->xfer_list_lock, flags);
211         list_del_init(&xfer->list_node);
212         spin_unlock_irqrestore(&xfer->wa->xfer_list_lock, flags);
213         /* FIXME: segmentation broken -- kills DWA */
214         wusbhc_giveback_urb(xfer->wa->wusb, xfer->urb, xfer->result);
215         wa_put(xfer->wa);
216         wa_xfer_put(xfer);
217 }
218
219 /*
220  * xfer is referenced
221  *
222  * xfer->lock has to be unlocked
223  */
224 static void wa_xfer_completion(struct wa_xfer *xfer)
225 {
226         if (xfer->wusb_dev)
227                 wusb_dev_put(xfer->wusb_dev);
228         rpipe_put(xfer->ep->hcpriv);
229         wa_xfer_giveback(xfer);
230 }
231
232 /*
233  * If transfer is done, wrap it up and return true
234  *
235  * xfer->lock has to be locked
236  */
237 static unsigned __wa_xfer_is_done(struct wa_xfer *xfer)
238 {
239         struct device *dev = &xfer->wa->usb_iface->dev;
240         unsigned result, cnt;
241         struct wa_seg *seg;
242         struct urb *urb = xfer->urb;
243         unsigned found_short = 0;
244
245         result = xfer->segs_done == xfer->segs_submitted;
246         if (result == 0)
247                 goto out;
248         urb->actual_length = 0;
249         for (cnt = 0; cnt < xfer->segs; cnt++) {
250                 seg = xfer->seg[cnt];
251                 switch (seg->status) {
252                 case WA_SEG_DONE:
253                         if (found_short && seg->result > 0) {
254                                 dev_dbg(dev, "xfer %p#%u: bad short segments (%zu)\n",
255                                         xfer, cnt, seg->result);
256                                 urb->status = -EINVAL;
257                                 goto out;
258                         }
259                         urb->actual_length += seg->result;
260                         if (seg->result < xfer->seg_size
261                             && cnt != xfer->segs-1)
262                                 found_short = 1;
263                         dev_dbg(dev, "xfer %p#%u: DONE short %d "
264                                 "result %zu urb->actual_length %d\n",
265                                 xfer, seg->index, found_short, seg->result,
266                                 urb->actual_length);
267                         break;
268                 case WA_SEG_ERROR:
269                         xfer->result = seg->result;
270                         dev_dbg(dev, "xfer %p#%u: ERROR result %zu\n",
271                                 xfer, seg->index, seg->result);
272                         goto out;
273                 case WA_SEG_ABORTED:
274                         dev_dbg(dev, "xfer %p#%u ABORTED: result %d\n",
275                                 xfer, seg->index, urb->status);
276                         xfer->result = urb->status;
277                         goto out;
278                 default:
279                         dev_warn(dev, "xfer %p#%u: is_done bad state %d\n",
280                                  xfer, cnt, seg->status);
281                         xfer->result = -EINVAL;
282                         goto out;
283                 }
284         }
285         xfer->result = 0;
286 out:
287         return result;
288 }
289
290 /*
291  * Initialize a transfer's ID
292  *
293  * We need to use a sequential number; if we use the pointer or the
294  * hash of the pointer, it can repeat over sequential transfers and
295  * then it will confuse the HWA....wonder why in hell they put a 32
296  * bit handle in there then.
297  */
298 static void wa_xfer_id_init(struct wa_xfer *xfer)
299 {
300         xfer->id = atomic_add_return(1, &xfer->wa->xfer_id_count);
301 }
302
303 /*
304  * Return the xfer's ID associated with xfer
305  *
306  * Need to generate a
307  */
308 static u32 wa_xfer_id(struct wa_xfer *xfer)
309 {
310         return xfer->id;
311 }
312
313 /*
314  * Search for a transfer list ID on the HCD's URB list
315  *
316  * For 32 bit architectures, we use the pointer itself; for 64 bits, a
317  * 32-bit hash of the pointer.
318  *
319  * @returns NULL if not found.
320  */
321 static struct wa_xfer *wa_xfer_get_by_id(struct wahc *wa, u32 id)
322 {
323         unsigned long flags;
324         struct wa_xfer *xfer_itr;
325         spin_lock_irqsave(&wa->xfer_list_lock, flags);
326         list_for_each_entry(xfer_itr, &wa->xfer_list, list_node) {
327                 if (id == xfer_itr->id) {
328                         wa_xfer_get(xfer_itr);
329                         goto out;
330                 }
331         }
332         xfer_itr = NULL;
333 out:
334         spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
335         return xfer_itr;
336 }
337
338 struct wa_xfer_abort_buffer {
339         struct urb urb;
340         struct wa_xfer_abort cmd;
341 };
342
343 static void __wa_xfer_abort_cb(struct urb *urb)
344 {
345         struct wa_xfer_abort_buffer *b = urb->context;
346         usb_put_urb(&b->urb);
347 }
348
349 /*
350  * Aborts an ongoing transaction
351  *
352  * Assumes the transfer is referenced and locked and in a submitted
353  * state (mainly that there is an endpoint/rpipe assigned).
354  *
355  * The callback (see above) does nothing but freeing up the data by
356  * putting the URB. Because the URB is allocated at the head of the
357  * struct, the whole space we allocated is kfreed.
358  *
359  * We'll get an 'aborted transaction' xfer result on DTI, that'll
360  * politely ignore because at this point the transaction has been
361  * marked as aborted already.
362  */
363 static void __wa_xfer_abort(struct wa_xfer *xfer)
364 {
365         int result;
366         struct device *dev = &xfer->wa->usb_iface->dev;
367         struct wa_xfer_abort_buffer *b;
368         struct wa_rpipe *rpipe = xfer->ep->hcpriv;
369
370         b = kmalloc(sizeof(*b), GFP_ATOMIC);
371         if (b == NULL)
372                 goto error_kmalloc;
373         b->cmd.bLength =  sizeof(b->cmd);
374         b->cmd.bRequestType = WA_XFER_ABORT;
375         b->cmd.wRPipe = rpipe->descr.wRPipeIndex;
376         b->cmd.dwTransferID = wa_xfer_id(xfer);
377
378         usb_init_urb(&b->urb);
379         usb_fill_bulk_urb(&b->urb, xfer->wa->usb_dev,
380                 usb_sndbulkpipe(xfer->wa->usb_dev,
381                                 xfer->wa->dto_epd->bEndpointAddress),
382                 &b->cmd, sizeof(b->cmd), __wa_xfer_abort_cb, b);
383         result = usb_submit_urb(&b->urb, GFP_ATOMIC);
384         if (result < 0)
385                 goto error_submit;
386         return;                         /* callback frees! */
387
388
389 error_submit:
390         if (printk_ratelimit())
391                 dev_err(dev, "xfer %p: Can't submit abort request: %d\n",
392                         xfer, result);
393         kfree(b);
394 error_kmalloc:
395         return;
396
397 }
398
399 /*
400  *
401  * @returns < 0 on error, transfer segment request size if ok
402  */
403 static ssize_t __wa_xfer_setup_sizes(struct wa_xfer *xfer,
404                                      enum wa_xfer_type *pxfer_type)
405 {
406         ssize_t result;
407         struct device *dev = &xfer->wa->usb_iface->dev;
408         size_t maxpktsize;
409         struct urb *urb = xfer->urb;
410         struct wa_rpipe *rpipe = xfer->ep->hcpriv;
411
412         switch (rpipe->descr.bmAttribute & 0x3) {
413         case USB_ENDPOINT_XFER_CONTROL:
414                 *pxfer_type = WA_XFER_TYPE_CTL;
415                 result = sizeof(struct wa_xfer_ctl);
416                 break;
417         case USB_ENDPOINT_XFER_INT:
418         case USB_ENDPOINT_XFER_BULK:
419                 *pxfer_type = WA_XFER_TYPE_BI;
420                 result = sizeof(struct wa_xfer_bi);
421                 break;
422         case USB_ENDPOINT_XFER_ISOC:
423                 dev_err(dev, "FIXME: ISOC not implemented\n");
424                 result = -ENOSYS;
425                 goto error;
426         default:
427                 /* never happens */
428                 BUG();
429                 result = -EINVAL;       /* shut gcc up */
430         };
431         xfer->is_inbound = urb->pipe & USB_DIR_IN ? 1 : 0;
432         xfer->is_dma = urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? 1 : 0;
433         xfer->seg_size = le16_to_cpu(rpipe->descr.wBlocks)
434                 * 1 << (xfer->wa->wa_descr->bRPipeBlockSize - 1);
435         /* Compute the segment size and make sure it is a multiple of
436          * the maxpktsize (WUSB1.0[8.3.3.1])...not really too much of
437          * a check (FIXME) */
438         maxpktsize = le16_to_cpu(rpipe->descr.wMaxPacketSize);
439         if (xfer->seg_size < maxpktsize) {
440                 dev_err(dev, "HW BUG? seg_size %zu smaller than maxpktsize "
441                         "%zu\n", xfer->seg_size, maxpktsize);
442                 result = -EINVAL;
443                 goto error;
444         }
445         xfer->seg_size = (xfer->seg_size / maxpktsize) * maxpktsize;
446         xfer->segs = DIV_ROUND_UP(urb->transfer_buffer_length, xfer->seg_size);
447         if (xfer->segs >= WA_SEGS_MAX) {
448                 dev_err(dev, "BUG? ops, number of segments %d bigger than %d\n",
449                         (int)(urb->transfer_buffer_length / xfer->seg_size),
450                         WA_SEGS_MAX);
451                 result = -EINVAL;
452                 goto error;
453         }
454         if (xfer->segs == 0 && *pxfer_type == WA_XFER_TYPE_CTL)
455                 xfer->segs = 1;
456 error:
457         return result;
458 }
459
460 /* Fill in the common request header and xfer-type specific data. */
461 static void __wa_xfer_setup_hdr0(struct wa_xfer *xfer,
462                                  struct wa_xfer_hdr *xfer_hdr0,
463                                  enum wa_xfer_type xfer_type,
464                                  size_t xfer_hdr_size)
465 {
466         struct wa_rpipe *rpipe = xfer->ep->hcpriv;
467
468         xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
469         xfer_hdr0->bLength = xfer_hdr_size;
470         xfer_hdr0->bRequestType = xfer_type;
471         xfer_hdr0->wRPipe = rpipe->descr.wRPipeIndex;
472         xfer_hdr0->dwTransferID = wa_xfer_id(xfer);
473         xfer_hdr0->bTransferSegment = 0;
474         switch (xfer_type) {
475         case WA_XFER_TYPE_CTL: {
476                 struct wa_xfer_ctl *xfer_ctl =
477                         container_of(xfer_hdr0, struct wa_xfer_ctl, hdr);
478                 xfer_ctl->bmAttribute = xfer->is_inbound ? 1 : 0;
479                 memcpy(&xfer_ctl->baSetupData, xfer->urb->setup_packet,
480                        sizeof(xfer_ctl->baSetupData));
481                 break;
482         }
483         case WA_XFER_TYPE_BI:
484                 break;
485         case WA_XFER_TYPE_ISO:
486                 printk(KERN_ERR "FIXME: ISOC not implemented\n");
487         default:
488                 BUG();
489         };
490 }
491
492 /*
493  * Callback for the OUT data phase of the segment request
494  *
495  * Check wa_seg_cb(); most comments also apply here because this
496  * function does almost the same thing and they work closely
497  * together.
498  *
499  * If the seg request has failed but this DTO phase has succeeded,
500  * wa_seg_cb() has already failed the segment and moved the
501  * status to WA_SEG_ERROR, so this will go through 'case 0' and
502  * effectively do nothing.
503  */
504 static void wa_seg_dto_cb(struct urb *urb)
505 {
506         struct wa_seg *seg = urb->context;
507         struct wa_xfer *xfer = seg->xfer;
508         struct wahc *wa;
509         struct device *dev;
510         struct wa_rpipe *rpipe;
511         unsigned long flags;
512         unsigned rpipe_ready = 0;
513         u8 done = 0;
514
515         switch (urb->status) {
516         case 0:
517                 spin_lock_irqsave(&xfer->lock, flags);
518                 wa = xfer->wa;
519                 dev = &wa->usb_iface->dev;
520                 dev_dbg(dev, "xfer %p#%u: data out done (%d bytes)\n",
521                         xfer, seg->index, urb->actual_length);
522                 if (seg->status < WA_SEG_PENDING)
523                         seg->status = WA_SEG_PENDING;
524                 seg->result = urb->actual_length;
525                 spin_unlock_irqrestore(&xfer->lock, flags);
526                 break;
527         case -ECONNRESET:       /* URB unlinked; no need to do anything */
528         case -ENOENT:           /* as it was done by the who unlinked us */
529                 break;
530         default:                /* Other errors ... */
531                 spin_lock_irqsave(&xfer->lock, flags);
532                 wa = xfer->wa;
533                 dev = &wa->usb_iface->dev;
534                 rpipe = xfer->ep->hcpriv;
535                 dev_dbg(dev, "xfer %p#%u: data out error %d\n",
536                         xfer, seg->index, urb->status);
537                 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
538                             EDC_ERROR_TIMEFRAME)){
539                         dev_err(dev, "DTO: URB max acceptable errors "
540                                 "exceeded, resetting device\n");
541                         wa_reset_all(wa);
542                 }
543                 if (seg->status != WA_SEG_ERROR) {
544                         seg->status = WA_SEG_ERROR;
545                         seg->result = urb->status;
546                         xfer->segs_done++;
547                         __wa_xfer_abort(xfer);
548                         rpipe_ready = rpipe_avail_inc(rpipe);
549                         done = __wa_xfer_is_done(xfer);
550                 }
551                 spin_unlock_irqrestore(&xfer->lock, flags);
552                 if (done)
553                         wa_xfer_completion(xfer);
554                 if (rpipe_ready)
555                         wa_xfer_delayed_run(rpipe);
556         }
557 }
558
559 /*
560  * Callback for the segment request
561  *
562  * If successful transition state (unless already transitioned or
563  * outbound transfer); otherwise, take a note of the error, mark this
564  * segment done and try completion.
565  *
566  * Note we don't access until we are sure that the transfer hasn't
567  * been cancelled (ECONNRESET, ENOENT), which could mean that
568  * seg->xfer could be already gone.
569  *
570  * We have to check before setting the status to WA_SEG_PENDING
571  * because sometimes the xfer result callback arrives before this
572  * callback (geeeeeeze), so it might happen that we are already in
573  * another state. As well, we don't set it if the transfer is inbound,
574  * as in that case, wa_seg_dto_cb will do it when the OUT data phase
575  * finishes.
576  */
577 static void wa_seg_cb(struct urb *urb)
578 {
579         struct wa_seg *seg = urb->context;
580         struct wa_xfer *xfer = seg->xfer;
581         struct wahc *wa;
582         struct device *dev;
583         struct wa_rpipe *rpipe;
584         unsigned long flags;
585         unsigned rpipe_ready;
586         u8 done = 0;
587
588         switch (urb->status) {
589         case 0:
590                 spin_lock_irqsave(&xfer->lock, flags);
591                 wa = xfer->wa;
592                 dev = &wa->usb_iface->dev;
593                 dev_dbg(dev, "xfer %p#%u: request done\n", xfer, seg->index);
594                 if (xfer->is_inbound && seg->status < WA_SEG_PENDING)
595                         seg->status = WA_SEG_PENDING;
596                 spin_unlock_irqrestore(&xfer->lock, flags);
597                 break;
598         case -ECONNRESET:       /* URB unlinked; no need to do anything */
599         case -ENOENT:           /* as it was done by the who unlinked us */
600                 break;
601         default:                /* Other errors ... */
602                 spin_lock_irqsave(&xfer->lock, flags);
603                 wa = xfer->wa;
604                 dev = &wa->usb_iface->dev;
605                 rpipe = xfer->ep->hcpriv;
606                 if (printk_ratelimit())
607                         dev_err(dev, "xfer %p#%u: request error %d\n",
608                                 xfer, seg->index, urb->status);
609                 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
610                             EDC_ERROR_TIMEFRAME)){
611                         dev_err(dev, "DTO: URB max acceptable errors "
612                                 "exceeded, resetting device\n");
613                         wa_reset_all(wa);
614                 }
615                 usb_unlink_urb(seg->dto_urb);
616                 seg->status = WA_SEG_ERROR;
617                 seg->result = urb->status;
618                 xfer->segs_done++;
619                 __wa_xfer_abort(xfer);
620                 rpipe_ready = rpipe_avail_inc(rpipe);
621                 done = __wa_xfer_is_done(xfer);
622                 spin_unlock_irqrestore(&xfer->lock, flags);
623                 if (done)
624                         wa_xfer_completion(xfer);
625                 if (rpipe_ready)
626                         wa_xfer_delayed_run(rpipe);
627         }
628 }
629
630 /* allocate an SG list to store bytes_to_transfer bytes and copy the
631  * subset of the in_sg that matches the buffer subset
632  * we are about to transfer. */
633 static struct scatterlist *wa_xfer_create_subset_sg(struct scatterlist *in_sg,
634         const unsigned int bytes_transferred,
635         const unsigned int bytes_to_transfer, unsigned int *out_num_sgs)
636 {
637         struct scatterlist *out_sg;
638         unsigned int bytes_processed = 0, offset_into_current_page_data = 0,
639                 nents;
640         struct scatterlist *current_xfer_sg = in_sg;
641         struct scatterlist *current_seg_sg, *last_seg_sg;
642
643         /* skip previously transferred pages. */
644         while ((current_xfer_sg) &&
645                         (bytes_processed < bytes_transferred)) {
646                 bytes_processed += current_xfer_sg->length;
647
648                 /* advance the sg if current segment starts on or past the
649                         next page. */
650                 if (bytes_processed <= bytes_transferred)
651                         current_xfer_sg = sg_next(current_xfer_sg);
652         }
653
654         /* the data for the current segment starts in current_xfer_sg.
655                 calculate the offset. */
656         if (bytes_processed > bytes_transferred) {
657                 offset_into_current_page_data = current_xfer_sg->length -
658                         (bytes_processed - bytes_transferred);
659         }
660
661         /* calculate the number of pages needed by this segment. */
662         nents = DIV_ROUND_UP((bytes_to_transfer +
663                 offset_into_current_page_data +
664                 current_xfer_sg->offset),
665                 PAGE_SIZE);
666
667         out_sg = kmalloc((sizeof(struct scatterlist) * nents), GFP_ATOMIC);
668         if (out_sg) {
669                 sg_init_table(out_sg, nents);
670
671                 /* copy the portion of the incoming SG that correlates to the
672                  * data to be transferred by this segment to the segment SG. */
673                 last_seg_sg = current_seg_sg = out_sg;
674                 bytes_processed = 0;
675
676                 /* reset nents and calculate the actual number of sg entries
677                         needed. */
678                 nents = 0;
679                 while ((bytes_processed < bytes_to_transfer) &&
680                                 current_seg_sg && current_xfer_sg) {
681                         unsigned int page_len = min((current_xfer_sg->length -
682                                 offset_into_current_page_data),
683                                 (bytes_to_transfer - bytes_processed));
684
685                         sg_set_page(current_seg_sg, sg_page(current_xfer_sg),
686                                 page_len,
687                                 current_xfer_sg->offset +
688                                 offset_into_current_page_data);
689
690                         bytes_processed += page_len;
691
692                         last_seg_sg = current_seg_sg;
693                         current_seg_sg = sg_next(current_seg_sg);
694                         current_xfer_sg = sg_next(current_xfer_sg);
695
696                         /* only the first page may require additional offset. */
697                         offset_into_current_page_data = 0;
698                         nents++;
699                 }
700
701                 /* update num_sgs and terminate the list since we may have
702                  *  concatenated pages. */
703                 sg_mark_end(last_seg_sg);
704                 *out_num_sgs = nents;
705         }
706
707         return out_sg;
708 }
709
710 /*
711  * Allocate the segs array and initialize each of them
712  *
713  * The segments are freed by wa_xfer_destroy() when the xfer use count
714  * drops to zero; however, because each segment is given the same life
715  * cycle as the USB URB it contains, it is actually freed by
716  * usb_put_urb() on the contained USB URB (twisted, eh?).
717  */
718 static int __wa_xfer_setup_segs(struct wa_xfer *xfer, size_t xfer_hdr_size)
719 {
720         int result, cnt;
721         size_t alloc_size = sizeof(*xfer->seg[0])
722                 - sizeof(xfer->seg[0]->xfer_hdr) + xfer_hdr_size;
723         struct usb_device *usb_dev = xfer->wa->usb_dev;
724         const struct usb_endpoint_descriptor *dto_epd = xfer->wa->dto_epd;
725         struct wa_seg *seg;
726         size_t buf_itr, buf_size, buf_itr_size;
727
728         result = -ENOMEM;
729         xfer->seg = kcalloc(xfer->segs, sizeof(xfer->seg[0]), GFP_ATOMIC);
730         if (xfer->seg == NULL)
731                 goto error_segs_kzalloc;
732         buf_itr = 0;
733         buf_size = xfer->urb->transfer_buffer_length;
734         for (cnt = 0; cnt < xfer->segs; cnt++) {
735                 seg = xfer->seg[cnt] = kzalloc(alloc_size, GFP_ATOMIC);
736                 if (seg == NULL)
737                         goto error_seg_kzalloc;
738                 wa_seg_init(seg);
739                 seg->xfer = xfer;
740                 seg->index = cnt;
741                 usb_fill_bulk_urb(&seg->urb, usb_dev,
742                                   usb_sndbulkpipe(usb_dev,
743                                                   dto_epd->bEndpointAddress),
744                                   &seg->xfer_hdr, xfer_hdr_size,
745                                   wa_seg_cb, seg);
746                 buf_itr_size = min(buf_size, xfer->seg_size);
747                 if (xfer->is_inbound == 0 && buf_size > 0) {
748                         /* outbound data. */
749                         seg->dto_urb = usb_alloc_urb(0, GFP_ATOMIC);
750                         if (seg->dto_urb == NULL)
751                                 goto error_dto_alloc;
752                         usb_fill_bulk_urb(
753                                 seg->dto_urb, usb_dev,
754                                 usb_sndbulkpipe(usb_dev,
755                                                 dto_epd->bEndpointAddress),
756                                 NULL, 0, wa_seg_dto_cb, seg);
757                         if (xfer->is_dma) {
758                                 seg->dto_urb->transfer_dma =
759                                         xfer->urb->transfer_dma + buf_itr;
760                                 seg->dto_urb->transfer_flags |=
761                                         URB_NO_TRANSFER_DMA_MAP;
762                                 seg->dto_urb->transfer_buffer = NULL;
763                                 seg->dto_urb->sg = NULL;
764                                 seg->dto_urb->num_sgs = 0;
765                         } else {
766                                 /* do buffer or SG processing. */
767                                 seg->dto_urb->transfer_flags &=
768                                         ~URB_NO_TRANSFER_DMA_MAP;
769                                 /* this should always be 0 before a resubmit. */
770                                 seg->dto_urb->num_mapped_sgs = 0;
771
772                                 if (xfer->urb->transfer_buffer) {
773                                         seg->dto_urb->transfer_buffer =
774                                                 xfer->urb->transfer_buffer +
775                                                 buf_itr;
776                                         seg->dto_urb->sg = NULL;
777                                         seg->dto_urb->num_sgs = 0;
778                                 } else {
779                                         /* allocate an SG list to store seg_size
780                                             bytes and copy the subset of the
781                                             xfer->urb->sg that matches the
782                                             buffer subset we are about to read.
783                                         */
784                                         seg->dto_urb->sg =
785                                                 wa_xfer_create_subset_sg(
786                                                 xfer->urb->sg,
787                                                 buf_itr, buf_itr_size,
788                                                 &(seg->dto_urb->num_sgs));
789
790                                         if (!(seg->dto_urb->sg)) {
791                                                 seg->dto_urb->num_sgs   = 0;
792                                                 goto error_sg_alloc;
793                                         }
794
795                                         seg->dto_urb->transfer_buffer = NULL;
796                                 }
797                         }
798                         seg->dto_urb->transfer_buffer_length = buf_itr_size;
799                 }
800                 seg->status = WA_SEG_READY;
801                 buf_itr += buf_itr_size;
802                 buf_size -= buf_itr_size;
803         }
804         return 0;
805
806 error_sg_alloc:
807         kfree(seg->dto_urb);
808 error_dto_alloc:
809         kfree(xfer->seg[cnt]);
810         cnt--;
811 error_seg_kzalloc:
812         /* use the fact that cnt is left at were it failed */
813         for (; cnt >= 0; cnt--) {
814                 if (xfer->seg[cnt] && xfer->is_inbound == 0)
815                         usb_free_urb(xfer->seg[cnt]->dto_urb);
816                 kfree(xfer->seg[cnt]);
817         }
818 error_segs_kzalloc:
819         return result;
820 }
821
822 /*
823  * Allocates all the stuff needed to submit a transfer
824  *
825  * Breaks the whole data buffer in a list of segments, each one has a
826  * structure allocated to it and linked in xfer->seg[index]
827  *
828  * FIXME: merge setup_segs() and the last part of this function, no
829  *        need to do two for loops when we could run everything in a
830  *        single one
831  */
832 static int __wa_xfer_setup(struct wa_xfer *xfer, struct urb *urb)
833 {
834         int result;
835         struct device *dev = &xfer->wa->usb_iface->dev;
836         enum wa_xfer_type xfer_type = 0; /* shut up GCC */
837         size_t xfer_hdr_size, cnt, transfer_size;
838         struct wa_xfer_hdr *xfer_hdr0, *xfer_hdr;
839
840         result = __wa_xfer_setup_sizes(xfer, &xfer_type);
841         if (result < 0)
842                 goto error_setup_sizes;
843         xfer_hdr_size = result;
844         result = __wa_xfer_setup_segs(xfer, xfer_hdr_size);
845         if (result < 0) {
846                 dev_err(dev, "xfer %p: Failed to allocate %d segments: %d\n",
847                         xfer, xfer->segs, result);
848                 goto error_setup_segs;
849         }
850         /* Fill the first header */
851         xfer_hdr0 = &xfer->seg[0]->xfer_hdr;
852         wa_xfer_id_init(xfer);
853         __wa_xfer_setup_hdr0(xfer, xfer_hdr0, xfer_type, xfer_hdr_size);
854
855         /* Fill remainig headers */
856         xfer_hdr = xfer_hdr0;
857         transfer_size = urb->transfer_buffer_length;
858         xfer_hdr0->dwTransferLength = transfer_size > xfer->seg_size ?
859                 xfer->seg_size : transfer_size;
860         transfer_size -=  xfer->seg_size;
861         for (cnt = 1; cnt < xfer->segs; cnt++) {
862                 xfer_hdr = &xfer->seg[cnt]->xfer_hdr;
863                 memcpy(xfer_hdr, xfer_hdr0, xfer_hdr_size);
864                 xfer_hdr->bTransferSegment = cnt;
865                 xfer_hdr->dwTransferLength = transfer_size > xfer->seg_size ?
866                         cpu_to_le32(xfer->seg_size)
867                         : cpu_to_le32(transfer_size);
868                 xfer->seg[cnt]->status = WA_SEG_READY;
869                 transfer_size -=  xfer->seg_size;
870         }
871         xfer_hdr->bTransferSegment |= 0x80;     /* this is the last segment */
872         result = 0;
873 error_setup_segs:
874 error_setup_sizes:
875         return result;
876 }
877
878 /*
879  *
880  *
881  * rpipe->seg_lock is held!
882  */
883 static int __wa_seg_submit(struct wa_rpipe *rpipe, struct wa_xfer *xfer,
884                            struct wa_seg *seg)
885 {
886         int result;
887         result = usb_submit_urb(&seg->urb, GFP_ATOMIC);
888         if (result < 0) {
889                 printk(KERN_ERR "xfer %p#%u: REQ submit failed: %d\n",
890                        xfer, seg->index, result);
891                 goto error_seg_submit;
892         }
893         if (seg->dto_urb) {
894                 result = usb_submit_urb(seg->dto_urb, GFP_ATOMIC);
895                 if (result < 0) {
896                         printk(KERN_ERR "xfer %p#%u: DTO submit failed: %d\n",
897                                xfer, seg->index, result);
898                         goto error_dto_submit;
899                 }
900         }
901         seg->status = WA_SEG_SUBMITTED;
902         rpipe_avail_dec(rpipe);
903         return 0;
904
905 error_dto_submit:
906         usb_unlink_urb(&seg->urb);
907 error_seg_submit:
908         seg->status = WA_SEG_ERROR;
909         seg->result = result;
910         return result;
911 }
912
913 /*
914  * Execute more queued request segments until the maximum concurrent allowed
915  *
916  * The ugly unlock/lock sequence on the error path is needed as the
917  * xfer->lock normally nests the seg_lock and not viceversa.
918  *
919  */
920 static void wa_xfer_delayed_run(struct wa_rpipe *rpipe)
921 {
922         int result;
923         struct device *dev = &rpipe->wa->usb_iface->dev;
924         struct wa_seg *seg;
925         struct wa_xfer *xfer;
926         unsigned long flags;
927
928         spin_lock_irqsave(&rpipe->seg_lock, flags);
929         while (atomic_read(&rpipe->segs_available) > 0
930               && !list_empty(&rpipe->seg_list)) {
931                 seg = list_entry(rpipe->seg_list.next, struct wa_seg,
932                                  list_node);
933                 list_del(&seg->list_node);
934                 xfer = seg->xfer;
935                 result = __wa_seg_submit(rpipe, xfer, seg);
936                 dev_dbg(dev, "xfer %p#%u submitted from delayed [%d segments available] %d\n",
937                         xfer, seg->index, atomic_read(&rpipe->segs_available), result);
938                 if (unlikely(result < 0)) {
939                         spin_unlock_irqrestore(&rpipe->seg_lock, flags);
940                         spin_lock_irqsave(&xfer->lock, flags);
941                         __wa_xfer_abort(xfer);
942                         xfer->segs_done++;
943                         spin_unlock_irqrestore(&xfer->lock, flags);
944                         spin_lock_irqsave(&rpipe->seg_lock, flags);
945                 }
946         }
947         spin_unlock_irqrestore(&rpipe->seg_lock, flags);
948 }
949
950 /*
951  *
952  * xfer->lock is taken
953  *
954  * On failure submitting we just stop submitting and return error;
955  * wa_urb_enqueue_b() will execute the completion path
956  */
957 static int __wa_xfer_submit(struct wa_xfer *xfer)
958 {
959         int result;
960         struct wahc *wa = xfer->wa;
961         struct device *dev = &wa->usb_iface->dev;
962         unsigned cnt;
963         struct wa_seg *seg;
964         unsigned long flags;
965         struct wa_rpipe *rpipe = xfer->ep->hcpriv;
966         size_t maxrequests = le16_to_cpu(rpipe->descr.wRequests);
967         u8 available;
968         u8 empty;
969
970         spin_lock_irqsave(&wa->xfer_list_lock, flags);
971         list_add_tail(&xfer->list_node, &wa->xfer_list);
972         spin_unlock_irqrestore(&wa->xfer_list_lock, flags);
973
974         BUG_ON(atomic_read(&rpipe->segs_available) > maxrequests);
975         result = 0;
976         spin_lock_irqsave(&rpipe->seg_lock, flags);
977         for (cnt = 0; cnt < xfer->segs; cnt++) {
978                 available = atomic_read(&rpipe->segs_available);
979                 empty = list_empty(&rpipe->seg_list);
980                 seg = xfer->seg[cnt];
981                 dev_dbg(dev, "xfer %p#%u: available %u empty %u (%s)\n",
982                         xfer, cnt, available, empty,
983                         available == 0 || !empty ? "delayed" : "submitted");
984                 if (available == 0 || !empty) {
985                         dev_dbg(dev, "xfer %p#%u: delayed\n", xfer, cnt);
986                         seg->status = WA_SEG_DELAYED;
987                         list_add_tail(&seg->list_node, &rpipe->seg_list);
988                 } else {
989                         result = __wa_seg_submit(rpipe, xfer, seg);
990                         if (result < 0) {
991                                 __wa_xfer_abort(xfer);
992                                 goto error_seg_submit;
993                         }
994                 }
995                 xfer->segs_submitted++;
996         }
997 error_seg_submit:
998         spin_unlock_irqrestore(&rpipe->seg_lock, flags);
999         return result;
1000 }
1001
1002 /*
1003  * Second part of a URB/transfer enqueuement
1004  *
1005  * Assumes this comes from wa_urb_enqueue() [maybe through
1006  * wa_urb_enqueue_run()]. At this point:
1007  *
1008  * xfer->wa     filled and refcounted
1009  * xfer->ep     filled with rpipe refcounted if
1010  *              delayed == 0
1011  * xfer->urb    filled and refcounted (this is the case when called
1012  *              from wa_urb_enqueue() as we come from usb_submit_urb()
1013  *              and when called by wa_urb_enqueue_run(), as we took an
1014  *              extra ref dropped by _run() after we return).
1015  * xfer->gfp    filled
1016  *
1017  * If we fail at __wa_xfer_submit(), then we just check if we are done
1018  * and if so, we run the completion procedure. However, if we are not
1019  * yet done, we do nothing and wait for the completion handlers from
1020  * the submitted URBs or from the xfer-result path to kick in. If xfer
1021  * result never kicks in, the xfer will timeout from the USB code and
1022  * dequeue() will be called.
1023  */
1024 static void wa_urb_enqueue_b(struct wa_xfer *xfer)
1025 {
1026         int result;
1027         unsigned long flags;
1028         struct urb *urb = xfer->urb;
1029         struct wahc *wa = xfer->wa;
1030         struct wusbhc *wusbhc = wa->wusb;
1031         struct wusb_dev *wusb_dev;
1032         unsigned done;
1033
1034         result = rpipe_get_by_ep(wa, xfer->ep, urb, xfer->gfp);
1035         if (result < 0)
1036                 goto error_rpipe_get;
1037         result = -ENODEV;
1038         /* FIXME: segmentation broken -- kills DWA */
1039         mutex_lock(&wusbhc->mutex);             /* get a WUSB dev */
1040         if (urb->dev == NULL) {
1041                 mutex_unlock(&wusbhc->mutex);
1042                 goto error_dev_gone;
1043         }
1044         wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, urb->dev);
1045         if (wusb_dev == NULL) {
1046                 mutex_unlock(&wusbhc->mutex);
1047                 goto error_dev_gone;
1048         }
1049         mutex_unlock(&wusbhc->mutex);
1050
1051         spin_lock_irqsave(&xfer->lock, flags);
1052         xfer->wusb_dev = wusb_dev;
1053         result = urb->status;
1054         if (urb->status != -EINPROGRESS)
1055                 goto error_dequeued;
1056
1057         result = __wa_xfer_setup(xfer, urb);
1058         if (result < 0)
1059                 goto error_xfer_setup;
1060         result = __wa_xfer_submit(xfer);
1061         if (result < 0)
1062                 goto error_xfer_submit;
1063         spin_unlock_irqrestore(&xfer->lock, flags);
1064         return;
1065
1066         /* this is basically wa_xfer_completion() broken up wa_xfer_giveback()
1067          * does a wa_xfer_put() that will call wa_xfer_destroy() and clean
1068          * upundo setup().
1069          */
1070 error_xfer_setup:
1071 error_dequeued:
1072         spin_unlock_irqrestore(&xfer->lock, flags);
1073         /* FIXME: segmentation broken, kills DWA */
1074         if (wusb_dev)
1075                 wusb_dev_put(wusb_dev);
1076 error_dev_gone:
1077         rpipe_put(xfer->ep->hcpriv);
1078 error_rpipe_get:
1079         xfer->result = result;
1080         wa_xfer_giveback(xfer);
1081         return;
1082
1083 error_xfer_submit:
1084         done = __wa_xfer_is_done(xfer);
1085         xfer->result = result;
1086         spin_unlock_irqrestore(&xfer->lock, flags);
1087         if (done)
1088                 wa_xfer_completion(xfer);
1089 }
1090
1091 /*
1092  * Execute the delayed transfers in the Wire Adapter @wa
1093  *
1094  * We need to be careful here, as dequeue() could be called in the
1095  * middle.  That's why we do the whole thing under the
1096  * wa->xfer_list_lock. If dequeue() jumps in, it first locks urb->lock
1097  * and then checks the list -- so as we would be acquiring in inverse
1098  * order, we just drop the lock once we have the xfer and reacquire it
1099  * later.
1100  */
1101 void wa_urb_enqueue_run(struct work_struct *ws)
1102 {
1103         struct wahc *wa = container_of(ws, struct wahc, xfer_work);
1104         struct wa_xfer *xfer, *next;
1105         struct urb *urb;
1106
1107         spin_lock_irq(&wa->xfer_list_lock);
1108         list_for_each_entry_safe(xfer, next, &wa->xfer_delayed_list,
1109                                  list_node) {
1110                 list_del_init(&xfer->list_node);
1111                 spin_unlock_irq(&wa->xfer_list_lock);
1112
1113                 urb = xfer->urb;
1114                 wa_urb_enqueue_b(xfer);
1115                 usb_put_urb(urb);       /* taken when queuing */
1116
1117                 spin_lock_irq(&wa->xfer_list_lock);
1118         }
1119         spin_unlock_irq(&wa->xfer_list_lock);
1120 }
1121 EXPORT_SYMBOL_GPL(wa_urb_enqueue_run);
1122
1123 /*
1124  * Submit a transfer to the Wire Adapter in a delayed way
1125  *
1126  * The process of enqueuing involves possible sleeps() [see
1127  * enqueue_b(), for the rpipe_get() and the mutex_lock()]. If we are
1128  * in an atomic section, we defer the enqueue_b() call--else we call direct.
1129  *
1130  * @urb: We own a reference to it done by the HCI Linux USB stack that
1131  *       will be given up by calling usb_hcd_giveback_urb() or by
1132  *       returning error from this function -> ergo we don't have to
1133  *       refcount it.
1134  */
1135 int wa_urb_enqueue(struct wahc *wa, struct usb_host_endpoint *ep,
1136                    struct urb *urb, gfp_t gfp)
1137 {
1138         int result;
1139         struct device *dev = &wa->usb_iface->dev;
1140         struct wa_xfer *xfer;
1141         unsigned long my_flags;
1142         unsigned cant_sleep = irqs_disabled() | in_atomic();
1143
1144         if ((urb->transfer_buffer == NULL)
1145             && (urb->sg == NULL)
1146             && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1147             && urb->transfer_buffer_length != 0) {
1148                 dev_err(dev, "BUG? urb %p: NULL xfer buffer & NODMA\n", urb);
1149                 dump_stack();
1150         }
1151
1152         result = -ENOMEM;
1153         xfer = kzalloc(sizeof(*xfer), gfp);
1154         if (xfer == NULL)
1155                 goto error_kmalloc;
1156
1157         result = -ENOENT;
1158         if (urb->status != -EINPROGRESS)        /* cancelled */
1159                 goto error_dequeued;            /* before starting? */
1160         wa_xfer_init(xfer);
1161         xfer->wa = wa_get(wa);
1162         xfer->urb = urb;
1163         xfer->gfp = gfp;
1164         xfer->ep = ep;
1165         urb->hcpriv = xfer;
1166
1167         dev_dbg(dev, "xfer %p urb %p pipe 0x%02x [%d bytes] %s %s %s\n",
1168                 xfer, urb, urb->pipe, urb->transfer_buffer_length,
1169                 urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP ? "dma" : "nodma",
1170                 urb->pipe & USB_DIR_IN ? "inbound" : "outbound",
1171                 cant_sleep ? "deferred" : "inline");
1172
1173         if (cant_sleep) {
1174                 usb_get_urb(urb);
1175                 spin_lock_irqsave(&wa->xfer_list_lock, my_flags);
1176                 list_add_tail(&xfer->list_node, &wa->xfer_delayed_list);
1177                 spin_unlock_irqrestore(&wa->xfer_list_lock, my_flags);
1178                 queue_work(wusbd, &wa->xfer_work);
1179         } else {
1180                 wa_urb_enqueue_b(xfer);
1181         }
1182         return 0;
1183
1184 error_dequeued:
1185         kfree(xfer);
1186 error_kmalloc:
1187         return result;
1188 }
1189 EXPORT_SYMBOL_GPL(wa_urb_enqueue);
1190
1191 /*
1192  * Dequeue a URB and make sure uwb_hcd_giveback_urb() [completion
1193  * handler] is called.
1194  *
1195  * Until a transfer goes successfully through wa_urb_enqueue() it
1196  * needs to be dequeued with completion calling; when stuck in delayed
1197  * or before wa_xfer_setup() is called, we need to do completion.
1198  *
1199  *  not setup  If there is no hcpriv yet, that means that that enqueue
1200  *             still had no time to set the xfer up. Because
1201  *             urb->status should be other than -EINPROGRESS,
1202  *             enqueue() will catch that and bail out.
1203  *
1204  * If the transfer has gone through setup, we just need to clean it
1205  * up. If it has gone through submit(), we have to abort it [with an
1206  * asynch request] and then make sure we cancel each segment.
1207  *
1208  */
1209 int wa_urb_dequeue(struct wahc *wa, struct urb *urb)
1210 {
1211         unsigned long flags, flags2;
1212         struct wa_xfer *xfer;
1213         struct wa_seg *seg;
1214         struct wa_rpipe *rpipe;
1215         unsigned cnt;
1216         unsigned rpipe_ready = 0;
1217
1218         xfer = urb->hcpriv;
1219         if (xfer == NULL) {
1220                 /* NOthing setup yet enqueue will see urb->status !=
1221                  * -EINPROGRESS (by hcd layer) and bail out with
1222                  * error, no need to do completion
1223                  */
1224                 BUG_ON(urb->status == -EINPROGRESS);
1225                 goto out;
1226         }
1227         spin_lock_irqsave(&xfer->lock, flags);
1228         rpipe = xfer->ep->hcpriv;
1229         if (rpipe == NULL) {
1230                 pr_debug("%s: xfer id 0x%08X has no RPIPE.  %s",
1231                         __func__, wa_xfer_id(xfer),
1232                         "Probably already aborted.\n" );
1233                 goto out_unlock;
1234         }
1235         /* Check the delayed list -> if there, release and complete */
1236         spin_lock_irqsave(&wa->xfer_list_lock, flags2);
1237         if (!list_empty(&xfer->list_node) && xfer->seg == NULL)
1238                 goto dequeue_delayed;
1239         spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1240         if (xfer->seg == NULL)          /* still hasn't reached */
1241                 goto out_unlock;        /* setup(), enqueue_b() completes */
1242         /* Ok, the xfer is in flight already, it's been setup and submitted.*/
1243         __wa_xfer_abort(xfer);
1244         for (cnt = 0; cnt < xfer->segs; cnt++) {
1245                 seg = xfer->seg[cnt];
1246                 switch (seg->status) {
1247                 case WA_SEG_NOTREADY:
1248                 case WA_SEG_READY:
1249                         printk(KERN_ERR "xfer %p#%u: dequeue bad state %u\n",
1250                                xfer, cnt, seg->status);
1251                         WARN_ON(1);
1252                         break;
1253                 case WA_SEG_DELAYED:
1254                         seg->status = WA_SEG_ABORTED;
1255                         spin_lock_irqsave(&rpipe->seg_lock, flags2);
1256                         list_del(&seg->list_node);
1257                         xfer->segs_done++;
1258                         rpipe_ready = rpipe_avail_inc(rpipe);
1259                         spin_unlock_irqrestore(&rpipe->seg_lock, flags2);
1260                         break;
1261                 case WA_SEG_SUBMITTED:
1262                         seg->status = WA_SEG_ABORTED;
1263                         usb_unlink_urb(&seg->urb);
1264                         if (xfer->is_inbound == 0)
1265                                 usb_unlink_urb(seg->dto_urb);
1266                         xfer->segs_done++;
1267                         rpipe_ready = rpipe_avail_inc(rpipe);
1268                         break;
1269                 case WA_SEG_PENDING:
1270                         seg->status = WA_SEG_ABORTED;
1271                         xfer->segs_done++;
1272                         rpipe_ready = rpipe_avail_inc(rpipe);
1273                         break;
1274                 case WA_SEG_DTI_PENDING:
1275                         usb_unlink_urb(wa->dti_urb);
1276                         seg->status = WA_SEG_ABORTED;
1277                         xfer->segs_done++;
1278                         rpipe_ready = rpipe_avail_inc(rpipe);
1279                         break;
1280                 case WA_SEG_DONE:
1281                 case WA_SEG_ERROR:
1282                 case WA_SEG_ABORTED:
1283                         break;
1284                 }
1285         }
1286         xfer->result = urb->status;     /* -ENOENT or -ECONNRESET */
1287         __wa_xfer_is_done(xfer);
1288         spin_unlock_irqrestore(&xfer->lock, flags);
1289         wa_xfer_completion(xfer);
1290         if (rpipe_ready)
1291                 wa_xfer_delayed_run(rpipe);
1292         return 0;
1293
1294 out_unlock:
1295         spin_unlock_irqrestore(&xfer->lock, flags);
1296 out:
1297         return 0;
1298
1299 dequeue_delayed:
1300         list_del_init(&xfer->list_node);
1301         spin_unlock_irqrestore(&wa->xfer_list_lock, flags2);
1302         xfer->result = urb->status;
1303         spin_unlock_irqrestore(&xfer->lock, flags);
1304         wa_xfer_giveback(xfer);
1305         usb_put_urb(urb);               /* we got a ref in enqueue() */
1306         return 0;
1307 }
1308 EXPORT_SYMBOL_GPL(wa_urb_dequeue);
1309
1310 /*
1311  * Translation from WA status codes (WUSB1.0 Table 8.15) to errno
1312  * codes
1313  *
1314  * Positive errno values are internal inconsistencies and should be
1315  * flagged louder. Negative are to be passed up to the user in the
1316  * normal way.
1317  *
1318  * @status: USB WA status code -- high two bits are stripped.
1319  */
1320 static int wa_xfer_status_to_errno(u8 status)
1321 {
1322         int errno;
1323         u8 real_status = status;
1324         static int xlat[] = {
1325                 [WA_XFER_STATUS_SUCCESS] =              0,
1326                 [WA_XFER_STATUS_HALTED] =               -EPIPE,
1327                 [WA_XFER_STATUS_DATA_BUFFER_ERROR] =    -ENOBUFS,
1328                 [WA_XFER_STATUS_BABBLE] =               -EOVERFLOW,
1329                 [WA_XFER_RESERVED] =                    EINVAL,
1330                 [WA_XFER_STATUS_NOT_FOUND] =            0,
1331                 [WA_XFER_STATUS_INSUFFICIENT_RESOURCE] = -ENOMEM,
1332                 [WA_XFER_STATUS_TRANSACTION_ERROR] =    -EILSEQ,
1333                 [WA_XFER_STATUS_ABORTED] =              -EINTR,
1334                 [WA_XFER_STATUS_RPIPE_NOT_READY] =      EINVAL,
1335                 [WA_XFER_INVALID_FORMAT] =              EINVAL,
1336                 [WA_XFER_UNEXPECTED_SEGMENT_NUMBER] =   EINVAL,
1337                 [WA_XFER_STATUS_RPIPE_TYPE_MISMATCH] =  EINVAL,
1338         };
1339         status &= 0x3f;
1340
1341         if (status == 0)
1342                 return 0;
1343         if (status >= ARRAY_SIZE(xlat)) {
1344                 printk_ratelimited(KERN_ERR "%s(): BUG? "
1345                                "Unknown WA transfer status 0x%02x\n",
1346                                __func__, real_status);
1347                 return -EINVAL;
1348         }
1349         errno = xlat[status];
1350         if (unlikely(errno > 0)) {
1351                 printk_ratelimited(KERN_ERR "%s(): BUG? "
1352                                "Inconsistent WA status: 0x%02x\n",
1353                                __func__, real_status);
1354                 errno = -errno;
1355         }
1356         return errno;
1357 }
1358
1359 /*
1360  * Process a xfer result completion message
1361  *
1362  * inbound transfers: need to schedule a DTI read
1363  *
1364  * FIXME: this functio needs to be broken up in parts
1365  */
1366 static void wa_xfer_result_chew(struct wahc *wa, struct wa_xfer *xfer)
1367 {
1368         int result;
1369         struct device *dev = &wa->usb_iface->dev;
1370         unsigned long flags;
1371         u8 seg_idx;
1372         struct wa_seg *seg;
1373         struct wa_rpipe *rpipe;
1374         struct wa_xfer_result *xfer_result = wa->xfer_result;
1375         u8 done = 0;
1376         u8 usb_status;
1377         unsigned rpipe_ready = 0;
1378
1379         spin_lock_irqsave(&xfer->lock, flags);
1380         seg_idx = xfer_result->bTransferSegment & 0x7f;
1381         if (unlikely(seg_idx >= xfer->segs))
1382                 goto error_bad_seg;
1383         seg = xfer->seg[seg_idx];
1384         rpipe = xfer->ep->hcpriv;
1385         usb_status = xfer_result->bTransferStatus;
1386         dev_dbg(dev, "xfer %p#%u: bTransferStatus 0x%02x (seg status %u)\n",
1387                 xfer, seg_idx, usb_status, seg->status);
1388         if (seg->status == WA_SEG_ABORTED
1389             || seg->status == WA_SEG_ERROR)     /* already handled */
1390                 goto segment_aborted;
1391         if (seg->status == WA_SEG_SUBMITTED)    /* ops, got here */
1392                 seg->status = WA_SEG_PENDING;   /* before wa_seg{_dto}_cb() */
1393         if (seg->status != WA_SEG_PENDING) {
1394                 if (printk_ratelimit())
1395                         dev_err(dev, "xfer %p#%u: Bad segment state %u\n",
1396                                 xfer, seg_idx, seg->status);
1397                 seg->status = WA_SEG_PENDING;   /* workaround/"fix" it */
1398         }
1399         if (usb_status & 0x80) {
1400                 seg->result = wa_xfer_status_to_errno(usb_status);
1401                 dev_err(dev, "DTI: xfer %p#:%08X:%u failed (0x%02x)\n",
1402                         xfer, xfer->id, seg->index, usb_status);
1403                 goto error_complete;
1404         }
1405         /* FIXME: we ignore warnings, tally them for stats */
1406         if (usb_status & 0x40)          /* Warning?... */
1407                 usb_status = 0;         /* ... pass */
1408         if (xfer->is_inbound) { /* IN data phase: read to buffer */
1409                 seg->status = WA_SEG_DTI_PENDING;
1410                 BUG_ON(wa->buf_in_urb->status == -EINPROGRESS);
1411                 /* this should always be 0 before a resubmit. */
1412                 wa->buf_in_urb->num_mapped_sgs  = 0;
1413
1414                 if (xfer->is_dma) {
1415                         wa->buf_in_urb->transfer_dma =
1416                                 xfer->urb->transfer_dma
1417                                 + (seg_idx * xfer->seg_size);
1418                         wa->buf_in_urb->transfer_flags
1419                                 |= URB_NO_TRANSFER_DMA_MAP;
1420                         wa->buf_in_urb->transfer_buffer = NULL;
1421                         wa->buf_in_urb->sg = NULL;
1422                         wa->buf_in_urb->num_sgs = 0;
1423                 } else {
1424                         /* do buffer or SG processing. */
1425                         wa->buf_in_urb->transfer_flags
1426                                 &= ~URB_NO_TRANSFER_DMA_MAP;
1427
1428                         if (xfer->urb->transfer_buffer) {
1429                                 wa->buf_in_urb->transfer_buffer =
1430                                         xfer->urb->transfer_buffer
1431                                         + (seg_idx * xfer->seg_size);
1432                                 wa->buf_in_urb->sg = NULL;
1433                                 wa->buf_in_urb->num_sgs = 0;
1434                         } else {
1435                                 /* allocate an SG list to store seg_size bytes
1436                                         and copy the subset of the xfer->urb->sg
1437                                         that matches the buffer subset we are
1438                                         about to read. */
1439                                 wa->buf_in_urb->sg = wa_xfer_create_subset_sg(
1440                                         xfer->urb->sg,
1441                                         seg_idx * xfer->seg_size,
1442                                         le32_to_cpu(
1443                                                 xfer_result->dwTransferLength),
1444                                         &(wa->buf_in_urb->num_sgs));
1445
1446                                 if (!(wa->buf_in_urb->sg)) {
1447                                         wa->buf_in_urb->num_sgs = 0;
1448                                         goto error_sg_alloc;
1449                                 }
1450                                 wa->buf_in_urb->transfer_buffer = NULL;
1451                         }
1452                 }
1453                 wa->buf_in_urb->transfer_buffer_length =
1454                         le32_to_cpu(xfer_result->dwTransferLength);
1455                 wa->buf_in_urb->context = seg;
1456                 result = usb_submit_urb(wa->buf_in_urb, GFP_ATOMIC);
1457                 if (result < 0)
1458                         goto error_submit_buf_in;
1459         } else {
1460                 /* OUT data phase, complete it -- */
1461                 seg->status = WA_SEG_DONE;
1462                 seg->result = le32_to_cpu(xfer_result->dwTransferLength);
1463                 xfer->segs_done++;
1464                 rpipe_ready = rpipe_avail_inc(rpipe);
1465                 done = __wa_xfer_is_done(xfer);
1466         }
1467         spin_unlock_irqrestore(&xfer->lock, flags);
1468         if (done)
1469                 wa_xfer_completion(xfer);
1470         if (rpipe_ready)
1471                 wa_xfer_delayed_run(rpipe);
1472         return;
1473
1474 error_submit_buf_in:
1475         if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
1476                 dev_err(dev, "DTI: URB max acceptable errors "
1477                         "exceeded, resetting device\n");
1478                 wa_reset_all(wa);
1479         }
1480         if (printk_ratelimit())
1481                 dev_err(dev, "xfer %p#%u: can't submit DTI data phase: %d\n",
1482                         xfer, seg_idx, result);
1483         seg->result = result;
1484         kfree(wa->buf_in_urb->sg);
1485 error_sg_alloc:
1486 error_complete:
1487         seg->status = WA_SEG_ERROR;
1488         xfer->segs_done++;
1489         rpipe_ready = rpipe_avail_inc(rpipe);
1490         __wa_xfer_abort(xfer);
1491         done = __wa_xfer_is_done(xfer);
1492         spin_unlock_irqrestore(&xfer->lock, flags);
1493         if (done)
1494                 wa_xfer_completion(xfer);
1495         if (rpipe_ready)
1496                 wa_xfer_delayed_run(rpipe);
1497         return;
1498
1499 error_bad_seg:
1500         spin_unlock_irqrestore(&xfer->lock, flags);
1501         wa_urb_dequeue(wa, xfer->urb);
1502         if (printk_ratelimit())
1503                 dev_err(dev, "xfer %p#%u: bad segment\n", xfer, seg_idx);
1504         if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
1505                 dev_err(dev, "DTI: URB max acceptable errors "
1506                         "exceeded, resetting device\n");
1507                 wa_reset_all(wa);
1508         }
1509         return;
1510
1511 segment_aborted:
1512         /* nothing to do, as the aborter did the completion */
1513         spin_unlock_irqrestore(&xfer->lock, flags);
1514 }
1515
1516 /*
1517  * Callback for the IN data phase
1518  *
1519  * If successful transition state; otherwise, take a note of the
1520  * error, mark this segment done and try completion.
1521  *
1522  * Note we don't access until we are sure that the transfer hasn't
1523  * been cancelled (ECONNRESET, ENOENT), which could mean that
1524  * seg->xfer could be already gone.
1525  */
1526 static void wa_buf_in_cb(struct urb *urb)
1527 {
1528         struct wa_seg *seg = urb->context;
1529         struct wa_xfer *xfer = seg->xfer;
1530         struct wahc *wa;
1531         struct device *dev;
1532         struct wa_rpipe *rpipe;
1533         unsigned rpipe_ready;
1534         unsigned long flags;
1535         u8 done = 0;
1536
1537         /* free the sg if it was used. */
1538         kfree(urb->sg);
1539         urb->sg = NULL;
1540
1541         switch (urb->status) {
1542         case 0:
1543                 spin_lock_irqsave(&xfer->lock, flags);
1544                 wa = xfer->wa;
1545                 dev = &wa->usb_iface->dev;
1546                 rpipe = xfer->ep->hcpriv;
1547                 dev_dbg(dev, "xfer %p#%u: data in done (%zu bytes)\n",
1548                         xfer, seg->index, (size_t)urb->actual_length);
1549                 seg->status = WA_SEG_DONE;
1550                 seg->result = urb->actual_length;
1551                 xfer->segs_done++;
1552                 rpipe_ready = rpipe_avail_inc(rpipe);
1553                 done = __wa_xfer_is_done(xfer);
1554                 spin_unlock_irqrestore(&xfer->lock, flags);
1555                 if (done)
1556                         wa_xfer_completion(xfer);
1557                 if (rpipe_ready)
1558                         wa_xfer_delayed_run(rpipe);
1559                 break;
1560         case -ECONNRESET:       /* URB unlinked; no need to do anything */
1561         case -ENOENT:           /* as it was done by the who unlinked us */
1562                 break;
1563         default:                /* Other errors ... */
1564                 spin_lock_irqsave(&xfer->lock, flags);
1565                 wa = xfer->wa;
1566                 dev = &wa->usb_iface->dev;
1567                 rpipe = xfer->ep->hcpriv;
1568                 if (printk_ratelimit())
1569                         dev_err(dev, "xfer %p#%u: data in error %d\n",
1570                                 xfer, seg->index, urb->status);
1571                 if (edc_inc(&wa->nep_edc, EDC_MAX_ERRORS,
1572                             EDC_ERROR_TIMEFRAME)){
1573                         dev_err(dev, "DTO: URB max acceptable errors "
1574                                 "exceeded, resetting device\n");
1575                         wa_reset_all(wa);
1576                 }
1577                 seg->status = WA_SEG_ERROR;
1578                 seg->result = urb->status;
1579                 xfer->segs_done++;
1580                 rpipe_ready = rpipe_avail_inc(rpipe);
1581                 __wa_xfer_abort(xfer);
1582                 done = __wa_xfer_is_done(xfer);
1583                 spin_unlock_irqrestore(&xfer->lock, flags);
1584                 if (done)
1585                         wa_xfer_completion(xfer);
1586                 if (rpipe_ready)
1587                         wa_xfer_delayed_run(rpipe);
1588         }
1589 }
1590
1591 /*
1592  * Handle an incoming transfer result buffer
1593  *
1594  * Given a transfer result buffer, it completes the transfer (possibly
1595  * scheduling and buffer in read) and then resubmits the DTI URB for a
1596  * new transfer result read.
1597  *
1598  *
1599  * The xfer_result DTI URB state machine
1600  *
1601  * States: OFF | RXR (Read-Xfer-Result) | RBI (Read-Buffer-In)
1602  *
1603  * We start in OFF mode, the first xfer_result notification [through
1604  * wa_handle_notif_xfer()] moves us to RXR by posting the DTI-URB to
1605  * read.
1606  *
1607  * We receive a buffer -- if it is not a xfer_result, we complain and
1608  * repost the DTI-URB. If it is a xfer_result then do the xfer seg
1609  * request accounting. If it is an IN segment, we move to RBI and post
1610  * a BUF-IN-URB to the right buffer. The BUF-IN-URB callback will
1611  * repost the DTI-URB and move to RXR state. if there was no IN
1612  * segment, it will repost the DTI-URB.
1613  *
1614  * We go back to OFF when we detect a ENOENT or ESHUTDOWN (or too many
1615  * errors) in the URBs.
1616  */
1617 static void wa_xfer_result_cb(struct urb *urb)
1618 {
1619         int result;
1620         struct wahc *wa = urb->context;
1621         struct device *dev = &wa->usb_iface->dev;
1622         struct wa_xfer_result *xfer_result;
1623         u32 xfer_id;
1624         struct wa_xfer *xfer;
1625         u8 usb_status;
1626
1627         BUG_ON(wa->dti_urb != urb);
1628         switch (wa->dti_urb->status) {
1629         case 0:
1630                 /* We have a xfer result buffer; check it */
1631                 dev_dbg(dev, "DTI: xfer result %d bytes at %p\n",
1632                         urb->actual_length, urb->transfer_buffer);
1633                 if (wa->dti_urb->actual_length != sizeof(*xfer_result)) {
1634                         dev_err(dev, "DTI Error: xfer result--bad size "
1635                                 "xfer result (%d bytes vs %zu needed)\n",
1636                                 urb->actual_length, sizeof(*xfer_result));
1637                         break;
1638                 }
1639                 xfer_result = wa->xfer_result;
1640                 if (xfer_result->hdr.bLength != sizeof(*xfer_result)) {
1641                         dev_err(dev, "DTI Error: xfer result--"
1642                                 "bad header length %u\n",
1643                                 xfer_result->hdr.bLength);
1644                         break;
1645                 }
1646                 if (xfer_result->hdr.bNotifyType != WA_XFER_RESULT) {
1647                         dev_err(dev, "DTI Error: xfer result--"
1648                                 "bad header type 0x%02x\n",
1649                                 xfer_result->hdr.bNotifyType);
1650                         break;
1651                 }
1652                 usb_status = xfer_result->bTransferStatus & 0x3f;
1653                 if (usb_status == WA_XFER_STATUS_NOT_FOUND)
1654                         /* taken care of already */
1655                         break;
1656                 xfer_id = xfer_result->dwTransferID;
1657                 xfer = wa_xfer_get_by_id(wa, xfer_id);
1658                 if (xfer == NULL) {
1659                         /* FIXME: transaction might have been cancelled */
1660                         dev_err(dev, "DTI Error: xfer result--"
1661                                 "unknown xfer 0x%08x (status 0x%02x)\n",
1662                                 xfer_id, usb_status);
1663                         break;
1664                 }
1665                 wa_xfer_result_chew(wa, xfer);
1666                 wa_xfer_put(xfer);
1667                 break;
1668         case -ENOENT:           /* (we killed the URB)...so, no broadcast */
1669         case -ESHUTDOWN:        /* going away! */
1670                 dev_dbg(dev, "DTI: going down! %d\n", urb->status);
1671                 goto out;
1672         default:
1673                 /* Unknown error */
1674                 if (edc_inc(&wa->dti_edc, EDC_MAX_ERRORS,
1675                             EDC_ERROR_TIMEFRAME)) {
1676                         dev_err(dev, "DTI: URB max acceptable errors "
1677                                 "exceeded, resetting device\n");
1678                         wa_reset_all(wa);
1679                         goto out;
1680                 }
1681                 if (printk_ratelimit())
1682                         dev_err(dev, "DTI: URB error %d\n", urb->status);
1683                 break;
1684         }
1685         /* Resubmit the DTI URB */
1686         result = usb_submit_urb(wa->dti_urb, GFP_ATOMIC);
1687         if (result < 0) {
1688                 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
1689                         "resetting\n", result);
1690                 wa_reset_all(wa);
1691         }
1692 out:
1693         return;
1694 }
1695
1696 /*
1697  * Transfer complete notification
1698  *
1699  * Called from the notif.c code. We get a notification on EP2 saying
1700  * that some endpoint has some transfer result data available. We are
1701  * about to read it.
1702  *
1703  * To speed up things, we always have a URB reading the DTI URB; we
1704  * don't really set it up and start it until the first xfer complete
1705  * notification arrives, which is what we do here.
1706  *
1707  * Follow up in wa_xfer_result_cb(), as that's where the whole state
1708  * machine starts.
1709  *
1710  * So here we just initialize the DTI URB for reading transfer result
1711  * notifications and also the buffer-in URB, for reading buffers. Then
1712  * we just submit the DTI URB.
1713  *
1714  * @wa shall be referenced
1715  */
1716 void wa_handle_notif_xfer(struct wahc *wa, struct wa_notif_hdr *notif_hdr)
1717 {
1718         int result;
1719         struct device *dev = &wa->usb_iface->dev;
1720         struct wa_notif_xfer *notif_xfer;
1721         const struct usb_endpoint_descriptor *dti_epd = wa->dti_epd;
1722
1723         notif_xfer = container_of(notif_hdr, struct wa_notif_xfer, hdr);
1724         BUG_ON(notif_hdr->bNotifyType != WA_NOTIF_TRANSFER);
1725
1726         if ((0x80 | notif_xfer->bEndpoint) != dti_epd->bEndpointAddress) {
1727                 /* FIXME: hardcoded limitation, adapt */
1728                 dev_err(dev, "BUG: DTI ep is %u, not %u (hack me)\n",
1729                         notif_xfer->bEndpoint, dti_epd->bEndpointAddress);
1730                 goto error;
1731         }
1732         if (wa->dti_urb != NULL)        /* DTI URB already started */
1733                 goto out;
1734
1735         wa->dti_urb = usb_alloc_urb(0, GFP_KERNEL);
1736         if (wa->dti_urb == NULL) {
1737                 dev_err(dev, "Can't allocate DTI URB\n");
1738                 goto error_dti_urb_alloc;
1739         }
1740         usb_fill_bulk_urb(
1741                 wa->dti_urb, wa->usb_dev,
1742                 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
1743                 wa->xfer_result, wa->xfer_result_size,
1744                 wa_xfer_result_cb, wa);
1745
1746         wa->buf_in_urb = usb_alloc_urb(0, GFP_KERNEL);
1747         if (wa->buf_in_urb == NULL) {
1748                 dev_err(dev, "Can't allocate BUF-IN URB\n");
1749                 goto error_buf_in_urb_alloc;
1750         }
1751         usb_fill_bulk_urb(
1752                 wa->buf_in_urb, wa->usb_dev,
1753                 usb_rcvbulkpipe(wa->usb_dev, 0x80 | notif_xfer->bEndpoint),
1754                 NULL, 0, wa_buf_in_cb, wa);
1755         result = usb_submit_urb(wa->dti_urb, GFP_KERNEL);
1756         if (result < 0) {
1757                 dev_err(dev, "DTI Error: Could not submit DTI URB (%d), "
1758                         "resetting\n", result);
1759                 goto error_dti_urb_submit;
1760         }
1761 out:
1762         return;
1763
1764 error_dti_urb_submit:
1765         usb_put_urb(wa->buf_in_urb);
1766 error_buf_in_urb_alloc:
1767         usb_put_urb(wa->dti_urb);
1768         wa->dti_urb = NULL;
1769 error_dti_urb_alloc:
1770 error:
1771         wa_reset_all(wa);
1772 }