blob: f98c7e91f6bcd2251785975289291a62dc9bef57 [file] [log] [blame]
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001/*
2 * hcd_ddma.c - DesignWare HS OTG Controller descriptor DMA routines
3 *
4 * Copyright (C) 2004-2013 Synopsys, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the above-listed copyright holders may not be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * ALTERNATIVELY, this software may be distributed under the terms of the
20 * GNU General Public License ("GPL") as published by the Free Software
21 * Foundation; either version 2 of the License, or (at your option) any
22 * later version.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37/*
38 * This file contains the Descriptor DMA implementation for Host mode
39 */
40#include <linux/kernel.h>
41#include <linux/module.h>
42#include <linux/spinlock.h>
43#include <linux/interrupt.h>
44#include <linux/dma-mapping.h>
45#include <linux/io.h>
46#include <linux/slab.h>
47#include <linux/usb.h>
48
49#include <linux/usb/hcd.h>
50#include <linux/usb/ch11.h>
51
52#include "core.h"
53#include "hcd.h"
54
55static u16 dwc2_frame_list_idx(u16 frame)
56{
57 return frame & (FRLISTEN_64_SIZE - 1);
58}
59
60static u16 dwc2_desclist_idx_inc(u16 idx, u16 inc, u8 speed)
61{
62 return (idx + inc) &
63 ((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
64 MAX_DMA_DESC_NUM_GENERIC) - 1);
65}
66
67static u16 dwc2_desclist_idx_dec(u16 idx, u16 inc, u8 speed)
68{
69 return (idx - inc) &
70 ((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
71 MAX_DMA_DESC_NUM_GENERIC) - 1);
72}
73
74static u16 dwc2_max_desc_num(struct dwc2_qh *qh)
75{
76 return (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
77 qh->dev_speed == USB_SPEED_HIGH) ?
78 MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC;
79}
80
81static u16 dwc2_frame_incr_val(struct dwc2_qh *qh)
82{
83 return qh->dev_speed == USB_SPEED_HIGH ?
84 (qh->interval + 8 - 1) / 8 : qh->interval;
85}
86
87static int dwc2_desc_list_alloc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
88 gfp_t flags)
89{
90 qh->desc_list = dma_alloc_coherent(hsotg->dev,
91 sizeof(struct dwc2_hcd_dma_desc) *
92 dwc2_max_desc_num(qh), &qh->desc_list_dma,
93 flags);
94
95 if (!qh->desc_list)
96 return -ENOMEM;
97
98 memset(qh->desc_list, 0,
99 sizeof(struct dwc2_hcd_dma_desc) * dwc2_max_desc_num(qh));
100
101 qh->n_bytes = kzalloc(sizeof(u32) * dwc2_max_desc_num(qh), flags);
102 if (!qh->n_bytes) {
103 dma_free_coherent(hsotg->dev, sizeof(struct dwc2_hcd_dma_desc)
104 * dwc2_max_desc_num(qh), qh->desc_list,
105 qh->desc_list_dma);
106 qh->desc_list = NULL;
107 return -ENOMEM;
108 }
109
110 return 0;
111}
112
113static void dwc2_desc_list_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
114{
115 if (qh->desc_list) {
116 dma_free_coherent(hsotg->dev, sizeof(struct dwc2_hcd_dma_desc)
117 * dwc2_max_desc_num(qh), qh->desc_list,
118 qh->desc_list_dma);
119 qh->desc_list = NULL;
120 }
121
122 kfree(qh->n_bytes);
123 qh->n_bytes = NULL;
124}
125
126static int dwc2_frame_list_alloc(struct dwc2_hsotg *hsotg, gfp_t mem_flags)
127{
128 if (hsotg->frame_list)
129 return 0;
130
131 hsotg->frame_list = dma_alloc_coherent(hsotg->dev,
132 4 * FRLISTEN_64_SIZE,
133 &hsotg->frame_list_dma,
134 mem_flags);
135 if (!hsotg->frame_list)
136 return -ENOMEM;
137
138 memset(hsotg->frame_list, 0, 4 * FRLISTEN_64_SIZE);
139 return 0;
140}
141
142static void dwc2_frame_list_free(struct dwc2_hsotg *hsotg)
143{
144 u32 *frame_list;
145 dma_addr_t frame_list_dma;
146 unsigned long flags;
147
148 spin_lock_irqsave(&hsotg->lock, flags);
149
150 if (!hsotg->frame_list) {
151 spin_unlock_irqrestore(&hsotg->lock, flags);
152 return;
153 }
154
155 frame_list = hsotg->frame_list;
156 frame_list_dma = hsotg->frame_list_dma;
157 hsotg->frame_list = NULL;
158
159 spin_unlock_irqrestore(&hsotg->lock, flags);
160
161 dma_free_coherent(hsotg->dev, 4 * FRLISTEN_64_SIZE, frame_list,
162 frame_list_dma);
163}
164
165static void dwc2_per_sched_enable(struct dwc2_hsotg *hsotg, u32 fr_list_en)
166{
167 u32 hcfg;
168 unsigned long flags;
169
170 spin_lock_irqsave(&hsotg->lock, flags);
171
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300172 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700173 if (hcfg & HCFG_PERSCHEDENA) {
174 /* already enabled */
175 spin_unlock_irqrestore(&hsotg->lock, flags);
176 return;
177 }
178
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300179 dwc2_writel(hsotg->frame_list_dma, hsotg->regs + HFLBADDR);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700180
181 hcfg &= ~HCFG_FRLISTEN_MASK;
182 hcfg |= fr_list_en | HCFG_PERSCHEDENA;
183 dev_vdbg(hsotg->dev, "Enabling Periodic schedule\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300184 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700185
186 spin_unlock_irqrestore(&hsotg->lock, flags);
187}
188
189static void dwc2_per_sched_disable(struct dwc2_hsotg *hsotg)
190{
191 u32 hcfg;
192 unsigned long flags;
193
194 spin_lock_irqsave(&hsotg->lock, flags);
195
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300196 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700197 if (!(hcfg & HCFG_PERSCHEDENA)) {
198 /* already disabled */
199 spin_unlock_irqrestore(&hsotg->lock, flags);
200 return;
201 }
202
203 hcfg &= ~HCFG_PERSCHEDENA;
204 dev_vdbg(hsotg->dev, "Disabling Periodic schedule\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300205 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700206
207 spin_unlock_irqrestore(&hsotg->lock, flags);
208}
209
210/*
211 * Activates/Deactivates FrameList entries for the channel based on endpoint
212 * servicing period
213 */
214static void dwc2_update_frame_list(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
215 int enable)
216{
217 struct dwc2_host_chan *chan;
218 u16 i, j, inc;
219
Paul Zimmermanddf58462013-04-22 14:00:16 -0700220 if (!hsotg) {
Paul Zimmermande9169a2013-04-22 14:00:17 -0700221 pr_err("hsotg = %p\n", hsotg);
Paul Zimmermanddf58462013-04-22 14:00:16 -0700222 return;
223 }
224
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700225 if (!qh->channel) {
Paul Zimmermande9169a2013-04-22 14:00:17 -0700226 dev_err(hsotg->dev, "qh->channel = %p\n", qh->channel);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700227 return;
228 }
229
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700230 if (!hsotg->frame_list) {
Paul Zimmermande9169a2013-04-22 14:00:17 -0700231 dev_err(hsotg->dev, "hsotg->frame_list = %p\n",
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700232 hsotg->frame_list);
233 return;
234 }
235
236 chan = qh->channel;
237 inc = dwc2_frame_incr_val(qh);
238 if (qh->ep_type == USB_ENDPOINT_XFER_ISOC)
239 i = dwc2_frame_list_idx(qh->sched_frame);
240 else
241 i = 0;
242
243 j = i;
244 do {
245 if (enable)
246 hsotg->frame_list[j] |= 1 << chan->hc_num;
247 else
248 hsotg->frame_list[j] &= ~(1 << chan->hc_num);
249 j = (j + inc) & (FRLISTEN_64_SIZE - 1);
250 } while (j != i);
251
252 if (!enable)
253 return;
254
255 chan->schinfo = 0;
256 if (chan->speed == USB_SPEED_HIGH && qh->interval) {
257 j = 1;
258 /* TODO - check this */
259 inc = (8 + qh->interval - 1) / qh->interval;
260 for (i = 0; i < inc; i++) {
261 chan->schinfo |= j;
262 j = j << qh->interval;
263 }
264 } else {
265 chan->schinfo = 0xff;
266 }
267}
268
269static void dwc2_release_channel_ddma(struct dwc2_hsotg *hsotg,
270 struct dwc2_qh *qh)
271{
272 struct dwc2_host_chan *chan = qh->channel;
273
Dom Cobley20f2eb92013-09-23 14:23:34 -0700274 if (dwc2_qh_is_non_per(qh)) {
275 if (hsotg->core_params->uframe_sched > 0)
276 hsotg->available_host_channels++;
277 else
278 hsotg->non_periodic_channels--;
279 } else {
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700280 dwc2_update_frame_list(hsotg, qh, 0);
Gregory Herrero3f808bd2015-11-05 09:41:44 +0100281 hsotg->available_host_channels++;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700282 }
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700283
284 /*
285 * The condition is added to prevent double cleanup try in case of
286 * device disconnect. See channel cleanup in dwc2_hcd_disconnect().
287 */
288 if (chan->qh) {
289 if (!list_empty(&chan->hc_list_entry))
290 list_del(&chan->hc_list_entry);
291 dwc2_hc_cleanup(hsotg, chan);
292 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
293 chan->qh = NULL;
294 }
295
296 qh->channel = NULL;
297 qh->ntd = 0;
298
299 if (qh->desc_list)
300 memset(qh->desc_list, 0, sizeof(struct dwc2_hcd_dma_desc) *
301 dwc2_max_desc_num(qh));
302}
303
304/**
305 * dwc2_hcd_qh_init_ddma() - Initializes a QH structure's Descriptor DMA
306 * related members
307 *
308 * @hsotg: The HCD state structure for the DWC OTG controller
309 * @qh: The QH to init
310 *
311 * Return: 0 if successful, negative error code otherwise
312 *
313 * Allocates memory for the descriptor list. For the first periodic QH,
314 * allocates memory for the FrameList and enables periodic scheduling.
315 */
316int dwc2_hcd_qh_init_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
317 gfp_t mem_flags)
318{
319 int retval;
320
321 if (qh->do_split) {
322 dev_err(hsotg->dev,
323 "SPLIT Transfers are not supported in Descriptor DMA mode.\n");
324 retval = -EINVAL;
325 goto err0;
326 }
327
328 retval = dwc2_desc_list_alloc(hsotg, qh, mem_flags);
329 if (retval)
330 goto err0;
331
332 if (qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
333 qh->ep_type == USB_ENDPOINT_XFER_INT) {
334 if (!hsotg->frame_list) {
335 retval = dwc2_frame_list_alloc(hsotg, mem_flags);
336 if (retval)
337 goto err1;
338 /* Enable periodic schedule on first periodic QH */
339 dwc2_per_sched_enable(hsotg, HCFG_FRLISTEN_64);
340 }
341 }
342
343 qh->ntd = 0;
344 return 0;
345
346err1:
347 dwc2_desc_list_free(hsotg, qh);
348err0:
349 return retval;
350}
351
352/**
353 * dwc2_hcd_qh_free_ddma() - Frees a QH structure's Descriptor DMA related
354 * members
355 *
356 * @hsotg: The HCD state structure for the DWC OTG controller
357 * @qh: The QH to free
358 *
359 * Frees descriptor list memory associated with the QH. If QH is periodic and
360 * the last, frees FrameList memory and disables periodic scheduling.
361 */
362void dwc2_hcd_qh_free_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
363{
Gregory Herrero2b046bc2015-11-05 09:41:41 +0100364 unsigned long flags;
365
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700366 dwc2_desc_list_free(hsotg, qh);
367
368 /*
369 * Channel still assigned due to some reasons.
370 * Seen on Isoc URB dequeue. Channel halted but no subsequent
371 * ChHalted interrupt to release the channel. Afterwards
372 * when it comes here from endpoint disable routine
373 * channel remains assigned.
374 */
Gregory Herrero2b046bc2015-11-05 09:41:41 +0100375 spin_lock_irqsave(&hsotg->lock, flags);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700376 if (qh->channel)
377 dwc2_release_channel_ddma(hsotg, qh);
Gregory Herrero2b046bc2015-11-05 09:41:41 +0100378 spin_unlock_irqrestore(&hsotg->lock, flags);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700379
380 if ((qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
381 qh->ep_type == USB_ENDPOINT_XFER_INT) &&
Dom Cobley20f2eb92013-09-23 14:23:34 -0700382 (hsotg->core_params->uframe_sched > 0 ||
383 !hsotg->periodic_channels) && hsotg->frame_list) {
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700384 dwc2_per_sched_disable(hsotg);
385 dwc2_frame_list_free(hsotg);
386 }
387}
388
389static u8 dwc2_frame_to_desc_idx(struct dwc2_qh *qh, u16 frame_idx)
390{
391 if (qh->dev_speed == USB_SPEED_HIGH)
392 /* Descriptor set (8 descriptors) index which is 8-aligned */
393 return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
394 else
395 return frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1);
396}
397
398/*
399 * Determine starting frame for Isochronous transfer.
400 * Few frames skipped to prevent race condition with HC.
401 */
402static u16 dwc2_calc_starting_frame(struct dwc2_hsotg *hsotg,
403 struct dwc2_qh *qh, u16 *skip_frames)
404{
405 u16 frame;
406
407 hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
408
409 /* sched_frame is always frame number (not uFrame) both in FS and HS! */
410
411 /*
412 * skip_frames is used to limit activated descriptors number
413 * to avoid the situation when HC services the last activated
414 * descriptor firstly.
415 * Example for FS:
416 * Current frame is 1, scheduled frame is 3. Since HC always fetches
417 * the descriptor corresponding to curr_frame+1, the descriptor
418 * corresponding to frame 2 will be fetched. If the number of
419 * descriptors is max=64 (or greather) the list will be fully programmed
420 * with Active descriptors and it is possible case (rare) that the
421 * latest descriptor(considering rollback) corresponding to frame 2 will
422 * be serviced first. HS case is more probable because, in fact, up to
423 * 11 uframes (16 in the code) may be skipped.
424 */
425 if (qh->dev_speed == USB_SPEED_HIGH) {
426 /*
427 * Consider uframe counter also, to start xfer asap. If half of
428 * the frame elapsed skip 2 frames otherwise just 1 frame.
429 * Starting descriptor index must be 8-aligned, so if the
430 * current frame is near to complete the next one is skipped as
431 * well.
432 */
433 if (dwc2_micro_frame_num(hsotg->frame_number) >= 5) {
434 *skip_frames = 2 * 8;
435 frame = dwc2_frame_num_inc(hsotg->frame_number,
436 *skip_frames);
437 } else {
438 *skip_frames = 1 * 8;
439 frame = dwc2_frame_num_inc(hsotg->frame_number,
440 *skip_frames);
441 }
442
443 frame = dwc2_full_frame_num(frame);
444 } else {
445 /*
446 * Two frames are skipped for FS - the current and the next.
447 * But for descriptor programming, 1 frame (descriptor) is
448 * enough, see example above.
449 */
450 *skip_frames = 1;
451 frame = dwc2_frame_num_inc(hsotg->frame_number, 2);
452 }
453
454 return frame;
455}
456
457/*
458 * Calculate initial descriptor index for isochronous transfer based on
459 * scheduled frame
460 */
461static u16 dwc2_recalc_initial_desc_idx(struct dwc2_hsotg *hsotg,
462 struct dwc2_qh *qh)
463{
464 u16 frame, fr_idx, fr_idx_tmp, skip_frames;
465
466 /*
467 * With current ISOC processing algorithm the channel is being released
468 * when no more QTDs in the list (qh->ntd == 0). Thus this function is
469 * called only when qh->ntd == 0 and qh->channel == 0.
470 *
471 * So qh->channel != NULL branch is not used and just not removed from
472 * the source file. It is required for another possible approach which
473 * is, do not disable and release the channel when ISOC session
474 * completed, just move QH to inactive schedule until new QTD arrives.
475 * On new QTD, the QH moved back to 'ready' schedule, starting frame and
476 * therefore starting desc_index are recalculated. In this case channel
477 * is released only on ep_disable.
478 */
479
480 /*
481 * Calculate starting descriptor index. For INTERRUPT endpoint it is
482 * always 0.
483 */
484 if (qh->channel) {
485 frame = dwc2_calc_starting_frame(hsotg, qh, &skip_frames);
486 /*
487 * Calculate initial descriptor index based on FrameList current
488 * bitmap and servicing period
489 */
490 fr_idx_tmp = dwc2_frame_list_idx(frame);
491 fr_idx = (FRLISTEN_64_SIZE +
492 dwc2_frame_list_idx(qh->sched_frame) - fr_idx_tmp)
493 % dwc2_frame_incr_val(qh);
494 fr_idx = (fr_idx + fr_idx_tmp) % FRLISTEN_64_SIZE;
495 } else {
496 qh->sched_frame = dwc2_calc_starting_frame(hsotg, qh,
497 &skip_frames);
498 fr_idx = dwc2_frame_list_idx(qh->sched_frame);
499 }
500
501 qh->td_first = qh->td_last = dwc2_frame_to_desc_idx(qh, fr_idx);
502
503 return skip_frames;
504}
505
506#define ISOC_URB_GIVEBACK_ASAP
507
508#define MAX_ISOC_XFER_SIZE_FS 1023
509#define MAX_ISOC_XFER_SIZE_HS 3072
510#define DESCNUM_THRESHOLD 4
511
512static void dwc2_fill_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
513 struct dwc2_qtd *qtd,
514 struct dwc2_qh *qh, u32 max_xfer_size,
515 u16 idx)
516{
517 struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[idx];
518 struct dwc2_hcd_iso_packet_desc *frame_desc;
519
520 memset(dma_desc, 0, sizeof(*dma_desc));
521 frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
522
523 if (frame_desc->length > max_xfer_size)
524 qh->n_bytes[idx] = max_xfer_size;
525 else
526 qh->n_bytes[idx] = frame_desc->length;
527
528 dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
529 dma_desc->status = qh->n_bytes[idx] << HOST_DMA_ISOC_NBYTES_SHIFT &
530 HOST_DMA_ISOC_NBYTES_MASK;
531
Gregory Herrerodde4c1b2015-11-05 09:41:38 +0100532 /* Set active bit */
533 dma_desc->status |= HOST_DMA_A;
534
Gregory Herrero3ac38d22015-11-05 09:41:37 +0100535 qh->ntd++;
536 qtd->isoc_frame_index_last++;
537
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700538#ifdef ISOC_URB_GIVEBACK_ASAP
539 /* Set IOC for each descriptor corresponding to last frame of URB */
540 if (qtd->isoc_frame_index_last == qtd->urb->packet_count)
541 dma_desc->status |= HOST_DMA_IOC;
542#endif
543
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700544}
545
546static void dwc2_init_isoc_dma_desc(struct dwc2_hsotg *hsotg,
547 struct dwc2_qh *qh, u16 skip_frames)
548{
549 struct dwc2_qtd *qtd;
550 u32 max_xfer_size;
Gregory Herreroc17b3372015-11-05 09:41:43 +0100551 u16 idx, inc, n_desc = 0, ntd_max = 0;
552 u16 cur_idx;
553 u16 next_idx;
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700554
555 idx = qh->td_last;
556 inc = qh->interval;
Gregory Herreroc17b3372015-11-05 09:41:43 +0100557 hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
558 cur_idx = dwc2_frame_list_idx(hsotg->frame_number);
559 next_idx = dwc2_desclist_idx_inc(qh->td_last, inc, qh->dev_speed);
560
561 /*
562 * Ensure current frame number didn't overstep last scheduled
563 * descriptor. If it happens, the only way to recover is to move
564 * qh->td_last to current frame number + 1.
565 * So that next isoc descriptor will be scheduled on frame number + 1
566 * and not on a past frame.
567 */
568 if (dwc2_frame_idx_num_gt(cur_idx, next_idx) || (cur_idx == next_idx)) {
569 if (inc < 32) {
570 dev_vdbg(hsotg->dev,
571 "current frame number overstep last descriptor\n");
572 qh->td_last = dwc2_desclist_idx_inc(cur_idx, inc,
573 qh->dev_speed);
574 idx = qh->td_last;
575 }
576 }
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700577
578 if (qh->interval) {
579 ntd_max = (dwc2_max_desc_num(qh) + qh->interval - 1) /
580 qh->interval;
581 if (skip_frames && !qh->channel)
582 ntd_max -= skip_frames / qh->interval;
583 }
584
585 max_xfer_size = qh->dev_speed == USB_SPEED_HIGH ?
586 MAX_ISOC_XFER_SIZE_HS : MAX_ISOC_XFER_SIZE_FS;
587
588 list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
Gregory Herreroc17b3372015-11-05 09:41:43 +0100589 if (qtd->in_process &&
590 qtd->isoc_frame_index_last ==
591 qtd->urb->packet_count)
592 continue;
593
594 qtd->isoc_td_first = idx;
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700595 while (qh->ntd < ntd_max && qtd->isoc_frame_index_last <
596 qtd->urb->packet_count) {
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700597 dwc2_fill_host_isoc_dma_desc(hsotg, qtd, qh,
598 max_xfer_size, idx);
599 idx = dwc2_desclist_idx_inc(idx, inc, qh->dev_speed);
600 n_desc++;
601 }
Gregory Herreroc17b3372015-11-05 09:41:43 +0100602 qtd->isoc_td_last = idx;
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700603 qtd->in_process = 1;
604 }
605
606 qh->td_last = idx;
607
608#ifdef ISOC_URB_GIVEBACK_ASAP
609 /* Set IOC for last descriptor if descriptor list is full */
610 if (qh->ntd == ntd_max) {
611 idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
612 qh->desc_list[idx].status |= HOST_DMA_IOC;
613 }
614#else
615 /*
616 * Set IOC bit only for one descriptor. Always try to be ahead of HW
617 * processing, i.e. on IOC generation driver activates next descriptor
618 * but core continues to process descriptors following the one with IOC
619 * set.
620 */
621
622 if (n_desc > DESCNUM_THRESHOLD)
623 /*
624 * Move IOC "up". Required even if there is only one QTD
625 * in the list, because QTDs might continue to be queued,
626 * but during the activation it was only one queued.
627 * Actually more than one QTD might be in the list if this
628 * function called from XferCompletion - QTDs was queued during
629 * HW processing of the previous descriptor chunk.
630 */
631 idx = dwc2_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2),
632 qh->dev_speed);
633 else
634 /*
635 * Set the IOC for the latest descriptor if either number of
636 * descriptors is not greater than threshold or no more new
637 * descriptors activated
638 */
639 idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
640
641 qh->desc_list[idx].status |= HOST_DMA_IOC;
642#endif
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700643}
644
645static void dwc2_fill_host_dma_desc(struct dwc2_hsotg *hsotg,
646 struct dwc2_host_chan *chan,
647 struct dwc2_qtd *qtd, struct dwc2_qh *qh,
648 int n_desc)
649{
650 struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[n_desc];
651 int len = chan->xfer_len;
652
Paul Zimmerman0b851be2013-11-25 13:42:45 -0800653 if (len > MAX_DMA_DESC_SIZE - (chan->max_packet - 1))
654 len = MAX_DMA_DESC_SIZE - (chan->max_packet - 1);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700655
656 if (chan->ep_is_in) {
657 int num_packets;
658
659 if (len > 0 && chan->max_packet)
660 num_packets = (len + chan->max_packet - 1)
661 / chan->max_packet;
662 else
663 /* Need 1 packet for transfer length of 0 */
664 num_packets = 1;
665
666 /* Always program an integral # of packets for IN transfers */
667 len = num_packets * chan->max_packet;
668 }
669
670 dma_desc->status = len << HOST_DMA_NBYTES_SHIFT & HOST_DMA_NBYTES_MASK;
671 qh->n_bytes[n_desc] = len;
672
673 if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL &&
674 qtd->control_phase == DWC2_CONTROL_SETUP)
675 dma_desc->status |= HOST_DMA_SUP;
676
677 dma_desc->buf = (u32)chan->xfer_dma;
678
679 /*
680 * Last (or only) descriptor of IN transfer with actual size less
681 * than MaxPacket
682 */
683 if (len > chan->xfer_len) {
684 chan->xfer_len = 0;
685 } else {
686 chan->xfer_dma += len;
687 chan->xfer_len -= len;
688 }
689}
690
691static void dwc2_init_non_isoc_dma_desc(struct dwc2_hsotg *hsotg,
692 struct dwc2_qh *qh)
693{
694 struct dwc2_qtd *qtd;
695 struct dwc2_host_chan *chan = qh->channel;
696 int n_desc = 0;
697
698 dev_vdbg(hsotg->dev, "%s(): qh=%p dma=%08lx len=%d\n", __func__, qh,
699 (unsigned long)chan->xfer_dma, chan->xfer_len);
700
701 /*
702 * Start with chan->xfer_dma initialized in assign_and_init_hc(), then
703 * if SG transfer consists of multiple URBs, this pointer is re-assigned
704 * to the buffer of the currently processed QTD. For non-SG request
705 * there is always one QTD active.
706 */
707
708 list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
709 dev_vdbg(hsotg->dev, "qtd=%p\n", qtd);
710
711 if (n_desc) {
712 /* SG request - more than 1 QTD */
713 chan->xfer_dma = qtd->urb->dma +
714 qtd->urb->actual_length;
715 chan->xfer_len = qtd->urb->length -
716 qtd->urb->actual_length;
717 dev_vdbg(hsotg->dev, "buf=%08lx len=%d\n",
718 (unsigned long)chan->xfer_dma, chan->xfer_len);
719 }
720
721 qtd->n_desc = 0;
722 do {
723 if (n_desc > 1) {
724 qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
725 dev_vdbg(hsotg->dev,
726 "set A bit in desc %d (%p)\n",
727 n_desc - 1,
728 &qh->desc_list[n_desc - 1]);
729 }
730 dwc2_fill_host_dma_desc(hsotg, chan, qtd, qh, n_desc);
731 dev_vdbg(hsotg->dev,
732 "desc %d (%p) buf=%08x status=%08x\n",
733 n_desc, &qh->desc_list[n_desc],
734 qh->desc_list[n_desc].buf,
735 qh->desc_list[n_desc].status);
736 qtd->n_desc++;
737 n_desc++;
738 } while (chan->xfer_len > 0 &&
739 n_desc != MAX_DMA_DESC_NUM_GENERIC);
740
741 dev_vdbg(hsotg->dev, "n_desc=%d\n", n_desc);
742 qtd->in_process = 1;
743 if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL)
744 break;
745 if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
746 break;
747 }
748
749 if (n_desc) {
750 qh->desc_list[n_desc - 1].status |=
751 HOST_DMA_IOC | HOST_DMA_EOL | HOST_DMA_A;
752 dev_vdbg(hsotg->dev, "set IOC/EOL/A bits in desc %d (%p)\n",
753 n_desc - 1, &qh->desc_list[n_desc - 1]);
754 if (n_desc > 1) {
755 qh->desc_list[0].status |= HOST_DMA_A;
756 dev_vdbg(hsotg->dev, "set A bit in desc 0 (%p)\n",
757 &qh->desc_list[0]);
758 }
759 chan->ntd = n_desc;
760 }
761}
762
763/**
764 * dwc2_hcd_start_xfer_ddma() - Starts a transfer in Descriptor DMA mode
765 *
766 * @hsotg: The HCD state structure for the DWC OTG controller
767 * @qh: The QH to init
768 *
769 * Return: 0 if successful, negative error code otherwise
770 *
771 * For Control and Bulk endpoints, initializes descriptor list and starts the
772 * transfer. For Interrupt and Isochronous endpoints, initializes descriptor
773 * list then updates FrameList, marking appropriate entries as active.
774 *
775 * For Isochronous endpoints the starting descriptor index is calculated based
776 * on the scheduled frame, but only on the first transfer descriptor within a
777 * session. Then the transfer is started via enabling the channel.
778 *
779 * For Isochronous endpoints the channel is not halted on XferComplete
780 * interrupt so remains assigned to the endpoint(QH) until session is done.
781 */
782void dwc2_hcd_start_xfer_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
783{
784 /* Channel is already assigned */
785 struct dwc2_host_chan *chan = qh->channel;
786 u16 skip_frames = 0;
787
788 switch (chan->ep_type) {
789 case USB_ENDPOINT_XFER_CONTROL:
790 case USB_ENDPOINT_XFER_BULK:
791 dwc2_init_non_isoc_dma_desc(hsotg, qh);
792 dwc2_hc_start_transfer_ddma(hsotg, chan);
793 break;
794 case USB_ENDPOINT_XFER_INT:
795 dwc2_init_non_isoc_dma_desc(hsotg, qh);
796 dwc2_update_frame_list(hsotg, qh, 1);
797 dwc2_hc_start_transfer_ddma(hsotg, chan);
798 break;
799 case USB_ENDPOINT_XFER_ISOC:
800 if (!qh->ntd)
801 skip_frames = dwc2_recalc_initial_desc_idx(hsotg, qh);
802 dwc2_init_isoc_dma_desc(hsotg, qh, skip_frames);
803
804 if (!chan->xfer_started) {
805 dwc2_update_frame_list(hsotg, qh, 1);
806
807 /*
808 * Always set to max, instead of actual size. Otherwise
809 * ntd will be changed with channel being enabled. Not
810 * recommended.
811 */
812 chan->ntd = dwc2_max_desc_num(qh);
813
814 /* Enable channel only once for ISOC */
815 dwc2_hc_start_transfer_ddma(hsotg, chan);
816 }
817
818 break;
819 default:
820 break;
821 }
822}
823
824#define DWC2_CMPL_DONE 1
825#define DWC2_CMPL_STOP 2
826
827static int dwc2_cmpl_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
828 struct dwc2_host_chan *chan,
829 struct dwc2_qtd *qtd,
830 struct dwc2_qh *qh, u16 idx)
831{
832 struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[idx];
833 struct dwc2_hcd_iso_packet_desc *frame_desc;
834 u16 remain = 0;
835 int rc = 0;
836
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700837 if (!qtd->urb)
838 return -EINVAL;
839
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700840 frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
841 dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
842 if (chan->ep_is_in)
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +0200843 remain = (dma_desc->status & HOST_DMA_ISOC_NBYTES_MASK) >>
844 HOST_DMA_ISOC_NBYTES_SHIFT;
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700845
846 if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
847 /*
848 * XactError, or unable to complete all the transactions
849 * in the scheduled micro-frame/frame, both indicated by
850 * HOST_DMA_STS_PKTERR
851 */
852 qtd->urb->error_count++;
853 frame_desc->actual_length = qh->n_bytes[idx] - remain;
854 frame_desc->status = -EPROTO;
855 } else {
856 /* Success */
857 frame_desc->actual_length = qh->n_bytes[idx] - remain;
858 frame_desc->status = 0;
859 }
860
861 if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
862 /*
863 * urb->status is not used for isoc transfers here. The
864 * individual frame_desc status are used instead.
865 */
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700866 dwc2_host_complete(hsotg, qtd, 0);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700867 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
868
869 /*
870 * This check is necessary because urb_dequeue can be called
871 * from urb complete callback (sound driver for example). All
872 * pending URBs are dequeued there, so no need for further
873 * processing.
874 */
875 if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE)
876 return -1;
877 rc = DWC2_CMPL_DONE;
878 }
879
880 qh->ntd--;
881
882 /* Stop if IOC requested descriptor reached */
883 if (dma_desc->status & HOST_DMA_IOC)
884 rc = DWC2_CMPL_STOP;
885
886 return rc;
887}
888
889static void dwc2_complete_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
890 struct dwc2_host_chan *chan,
891 enum dwc2_halt_status halt_status)
892{
893 struct dwc2_hcd_iso_packet_desc *frame_desc;
894 struct dwc2_qtd *qtd, *qtd_tmp;
895 struct dwc2_qh *qh;
896 u16 idx;
897 int rc;
898
899 qh = chan->qh;
900 idx = qh->td_first;
901
902 if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
903 list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
904 qtd->in_process = 0;
905 return;
906 }
907
908 if (halt_status == DWC2_HC_XFER_AHB_ERR ||
909 halt_status == DWC2_HC_XFER_BABBLE_ERR) {
910 /*
911 * Channel is halted in these error cases, considered as serious
912 * issues.
913 * Complete all URBs marking all frames as failed, irrespective
914 * whether some of the descriptors (frames) succeeded or not.
915 * Pass error code to completion routine as well, to update
916 * urb->status, some of class drivers might use it to stop
917 * queing transfer requests.
918 */
919 int err = halt_status == DWC2_HC_XFER_AHB_ERR ?
920 -EIO : -EOVERFLOW;
921
922 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
923 qtd_list_entry) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700924 if (qtd->urb) {
925 for (idx = 0; idx < qtd->urb->packet_count;
926 idx++) {
927 frame_desc = &qtd->urb->iso_descs[idx];
928 frame_desc->status = err;
929 }
930
931 dwc2_host_complete(hsotg, qtd, err);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700932 }
933
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700934 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
935 }
936
937 return;
938 }
939
940 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
941 if (!qtd->in_process)
942 break;
Gregory Herrero762d3a12015-11-05 09:41:45 +0100943
944 /*
945 * Ensure idx corresponds to descriptor where first urb of this
946 * qtd was added. In fact, during isoc desc init, dwc2 may skip
947 * an index if current frame number is already over this index.
948 */
949 if (idx != qtd->isoc_td_first) {
950 dev_vdbg(hsotg->dev,
951 "try to complete %d instead of %d\n",
952 idx, qtd->isoc_td_first);
953 idx = qtd->isoc_td_first;
954 }
955
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700956 do {
Gregory Herrero762d3a12015-11-05 09:41:45 +0100957 struct dwc2_qtd *qtd_next;
958 u16 cur_idx;
959
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700960 rc = dwc2_cmpl_host_isoc_dma_desc(hsotg, chan, qtd, qh,
961 idx);
962 if (rc < 0)
963 return;
964 idx = dwc2_desclist_idx_inc(idx, qh->interval,
965 chan->speed);
Gregory Herrero762d3a12015-11-05 09:41:45 +0100966 if (!rc)
967 continue;
968
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700969 if (rc == DWC2_CMPL_DONE)
970 break;
Gregory Herrero762d3a12015-11-05 09:41:45 +0100971
972 /* rc == DWC2_CMPL_STOP */
973
974 if (qh->interval >= 32)
975 goto stop_scan;
976
977 qh->td_first = idx;
978 cur_idx = dwc2_frame_list_idx(hsotg->frame_number);
979 qtd_next = list_first_entry(&qh->qtd_list,
980 struct dwc2_qtd,
981 qtd_list_entry);
982 if (dwc2_frame_idx_num_gt(cur_idx,
983 qtd_next->isoc_td_last))
984 break;
985
986 goto stop_scan;
987
Paul Zimmermandc4c76e2013-03-11 17:48:00 -0700988 } while (idx != qh->td_first);
989 }
990
991stop_scan:
992 qh->td_first = idx;
993}
994
995static int dwc2_update_non_isoc_urb_state_ddma(struct dwc2_hsotg *hsotg,
996 struct dwc2_host_chan *chan,
997 struct dwc2_qtd *qtd,
998 struct dwc2_hcd_dma_desc *dma_desc,
999 enum dwc2_halt_status halt_status,
1000 u32 n_bytes, int *xfer_done)
1001{
1002 struct dwc2_hcd_urb *urb = qtd->urb;
1003 u16 remain = 0;
1004
1005 if (chan->ep_is_in)
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001006 remain = (dma_desc->status & HOST_DMA_NBYTES_MASK) >>
1007 HOST_DMA_NBYTES_SHIFT;
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001008
1009 dev_vdbg(hsotg->dev, "remain=%d dwc2_urb=%p\n", remain, urb);
1010
1011 if (halt_status == DWC2_HC_XFER_AHB_ERR) {
1012 dev_err(hsotg->dev, "EIO\n");
1013 urb->status = -EIO;
1014 return 1;
1015 }
1016
1017 if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
1018 switch (halt_status) {
1019 case DWC2_HC_XFER_STALL:
1020 dev_vdbg(hsotg->dev, "Stall\n");
1021 urb->status = -EPIPE;
1022 break;
1023 case DWC2_HC_XFER_BABBLE_ERR:
1024 dev_err(hsotg->dev, "Babble\n");
1025 urb->status = -EOVERFLOW;
1026 break;
1027 case DWC2_HC_XFER_XACT_ERR:
1028 dev_err(hsotg->dev, "XactErr\n");
1029 urb->status = -EPROTO;
1030 break;
1031 default:
1032 dev_err(hsotg->dev,
1033 "%s: Unhandled descriptor error status (%d)\n",
1034 __func__, halt_status);
1035 break;
1036 }
1037 return 1;
1038 }
1039
1040 if (dma_desc->status & HOST_DMA_A) {
1041 dev_vdbg(hsotg->dev,
1042 "Active descriptor encountered on channel %d\n",
1043 chan->hc_num);
1044 return 0;
1045 }
1046
1047 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL) {
1048 if (qtd->control_phase == DWC2_CONTROL_DATA) {
1049 urb->actual_length += n_bytes - remain;
1050 if (remain || urb->actual_length >= urb->length) {
1051 /*
1052 * For Control Data stage do not set urb->status
1053 * to 0, to prevent URB callback. Set it when
1054 * Status phase is done. See below.
1055 */
1056 *xfer_done = 1;
1057 }
1058 } else if (qtd->control_phase == DWC2_CONTROL_STATUS) {
1059 urb->status = 0;
1060 *xfer_done = 1;
1061 }
1062 /* No handling for SETUP stage */
1063 } else {
1064 /* BULK and INTR */
1065 urb->actual_length += n_bytes - remain;
1066 dev_vdbg(hsotg->dev, "length=%d actual=%d\n", urb->length,
1067 urb->actual_length);
1068 if (remain || urb->actual_length >= urb->length) {
1069 urb->status = 0;
1070 *xfer_done = 1;
1071 }
1072 }
1073
1074 return 0;
1075}
1076
1077static int dwc2_process_non_isoc_desc(struct dwc2_hsotg *hsotg,
1078 struct dwc2_host_chan *chan,
1079 int chnum, struct dwc2_qtd *qtd,
1080 int desc_num,
1081 enum dwc2_halt_status halt_status,
1082 int *xfer_done)
1083{
1084 struct dwc2_qh *qh = chan->qh;
1085 struct dwc2_hcd_urb *urb = qtd->urb;
1086 struct dwc2_hcd_dma_desc *dma_desc;
1087 u32 n_bytes;
1088 int failed;
1089
1090 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1091
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001092 if (!urb)
1093 return -EINVAL;
1094
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001095 dma_desc = &qh->desc_list[desc_num];
1096 n_bytes = qh->n_bytes[desc_num];
1097 dev_vdbg(hsotg->dev,
1098 "qtd=%p dwc2_urb=%p desc_num=%d desc=%p n_bytes=%d\n",
1099 qtd, urb, desc_num, dma_desc, n_bytes);
1100 failed = dwc2_update_non_isoc_urb_state_ddma(hsotg, chan, qtd, dma_desc,
1101 halt_status, n_bytes,
1102 xfer_done);
Gregory Herrero26a19ea2015-11-05 09:41:40 +01001103 if (*xfer_done && urb->status != -EINPROGRESS)
1104 failed = 1;
1105
1106 if (failed) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001107 dwc2_host_complete(hsotg, qtd, urb->status);
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001108 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1109 dev_vdbg(hsotg->dev, "failed=%1x xfer_done=%1x status=%08x\n",
1110 failed, *xfer_done, urb->status);
1111 return failed;
1112 }
1113
1114 if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL) {
1115 switch (qtd->control_phase) {
1116 case DWC2_CONTROL_SETUP:
1117 if (urb->length > 0)
1118 qtd->control_phase = DWC2_CONTROL_DATA;
1119 else
1120 qtd->control_phase = DWC2_CONTROL_STATUS;
1121 dev_vdbg(hsotg->dev,
1122 " Control setup transaction done\n");
1123 break;
1124 case DWC2_CONTROL_DATA:
1125 if (*xfer_done) {
1126 qtd->control_phase = DWC2_CONTROL_STATUS;
1127 dev_vdbg(hsotg->dev,
1128 " Control data transfer done\n");
1129 } else if (desc_num + 1 == qtd->n_desc) {
1130 /*
1131 * Last descriptor for Control data stage which
1132 * is not completed yet
1133 */
1134 dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
1135 qtd);
1136 }
1137 break;
1138 default:
1139 break;
1140 }
1141 }
1142
1143 return 0;
1144}
1145
1146static void dwc2_complete_non_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
1147 struct dwc2_host_chan *chan,
1148 int chnum,
1149 enum dwc2_halt_status halt_status)
1150{
1151 struct list_head *qtd_item, *qtd_tmp;
1152 struct dwc2_qh *qh = chan->qh;
1153 struct dwc2_qtd *qtd = NULL;
1154 int xfer_done;
1155 int desc_num = 0;
1156
1157 if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
1158 list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
1159 qtd->in_process = 0;
1160 return;
1161 }
1162
1163 list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
1164 int i;
1165
1166 qtd = list_entry(qtd_item, struct dwc2_qtd, qtd_list_entry);
1167 xfer_done = 0;
1168
1169 for (i = 0; i < qtd->n_desc; i++) {
1170 if (dwc2_process_non_isoc_desc(hsotg, chan, chnum, qtd,
1171 desc_num, halt_status,
Paul Zimmermanfbd1cd22013-11-22 16:43:46 -08001172 &xfer_done)) {
1173 qtd = NULL;
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001174 break;
Paul Zimmermanfbd1cd22013-11-22 16:43:46 -08001175 }
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001176 desc_num++;
1177 }
1178 }
1179
1180 if (qh->ep_type != USB_ENDPOINT_XFER_CONTROL) {
1181 /*
1182 * Resetting the data toggle for bulk and interrupt endpoints
1183 * in case of stall. See handle_hc_stall_intr().
1184 */
1185 if (halt_status == DWC2_HC_XFER_STALL)
1186 qh->data_toggle = DWC2_HC_PID_DATA0;
1187 else if (qtd)
1188 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1189 }
1190
1191 if (halt_status == DWC2_HC_XFER_COMPLETE) {
1192 if (chan->hcint & HCINTMSK_NYET) {
1193 /*
1194 * Got a NYET on the last transaction of the transfer.
1195 * It means that the endpoint should be in the PING
1196 * state at the beginning of the next transfer.
1197 */
1198 qh->ping_state = 1;
1199 }
1200 }
1201}
1202
1203/**
1204 * dwc2_hcd_complete_xfer_ddma() - Scans the descriptor list, updates URB's
1205 * status and calls completion routine for the URB if it's done. Called from
1206 * interrupt handlers.
1207 *
1208 * @hsotg: The HCD state structure for the DWC OTG controller
1209 * @chan: Host channel the transfer is completed on
1210 * @chnum: Index of Host channel registers
1211 * @halt_status: Reason the channel is being halted or just XferComplete
1212 * for isochronous transfers
1213 *
1214 * Releases the channel to be used by other transfers.
1215 * In case of Isochronous endpoint the channel is not halted until the end of
1216 * the session, i.e. QTD list is empty.
1217 * If periodic channel released the FrameList is updated accordingly.
1218 * Calls transaction selection routines to activate pending transfers.
1219 */
1220void dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg *hsotg,
1221 struct dwc2_host_chan *chan, int chnum,
1222 enum dwc2_halt_status halt_status)
1223{
1224 struct dwc2_qh *qh = chan->qh;
1225 int continue_isoc_xfer = 0;
1226 enum dwc2_transaction_type tr_type;
1227
1228 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1229 dwc2_complete_isoc_xfer_ddma(hsotg, chan, halt_status);
1230
1231 /* Release the channel if halted or session completed */
1232 if (halt_status != DWC2_HC_XFER_COMPLETE ||
1233 list_empty(&qh->qtd_list)) {
Gregory Herreroc503b382015-11-05 09:41:39 +01001234 struct dwc2_qtd *qtd, *qtd_tmp;
1235
1236 /*
1237 * Kill all remainings QTDs since channel has been
1238 * halted.
1239 */
1240 list_for_each_entry_safe(qtd, qtd_tmp,
1241 &qh->qtd_list,
1242 qtd_list_entry) {
1243 dwc2_host_complete(hsotg, qtd,
1244 -ECONNRESET);
1245 dwc2_hcd_qtd_unlink_and_free(hsotg,
1246 qtd, qh);
1247 }
1248
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001249 /* Halt the channel if session completed */
1250 if (halt_status == DWC2_HC_XFER_COMPLETE)
1251 dwc2_hc_halt(hsotg, chan, halt_status);
1252 dwc2_release_channel_ddma(hsotg, qh);
1253 dwc2_hcd_qh_unlink(hsotg, qh);
1254 } else {
1255 /* Keep in assigned schedule to continue transfer */
1256 list_move(&qh->qh_list_entry,
1257 &hsotg->periodic_sched_assigned);
Gregory Herreroc503b382015-11-05 09:41:39 +01001258 /*
1259 * If channel has been halted during giveback of urb
1260 * then prevent any new scheduling.
1261 */
1262 if (!chan->halt_status)
1263 continue_isoc_xfer = 1;
Paul Zimmermandc4c76e2013-03-11 17:48:00 -07001264 }
1265 /*
1266 * Todo: Consider the case when period exceeds FrameList size.
1267 * Frame Rollover interrupt should be used.
1268 */
1269 } else {
1270 /*
1271 * Scan descriptor list to complete the URB(s), then release
1272 * the channel
1273 */
1274 dwc2_complete_non_isoc_xfer_ddma(hsotg, chan, chnum,
1275 halt_status);
1276 dwc2_release_channel_ddma(hsotg, qh);
1277 dwc2_hcd_qh_unlink(hsotg, qh);
1278
1279 if (!list_empty(&qh->qtd_list)) {
1280 /*
1281 * Add back to inactive non-periodic schedule on normal
1282 * completion
1283 */
1284 dwc2_hcd_qh_add(hsotg, qh);
1285 }
1286 }
1287
1288 tr_type = dwc2_hcd_select_transactions(hsotg);
1289 if (tr_type != DWC2_TRANSACTION_NONE || continue_isoc_xfer) {
1290 if (continue_isoc_xfer) {
1291 if (tr_type == DWC2_TRANSACTION_NONE)
1292 tr_type = DWC2_TRANSACTION_PERIODIC;
1293 else if (tr_type == DWC2_TRANSACTION_NON_PERIODIC)
1294 tr_type = DWC2_TRANSACTION_ALL;
1295 }
1296 dwc2_hcd_queue_transactions(hsotg, tr_type);
1297 }
1298}