ehci-sched.c 60.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * Copyright (c) 2001-2004 by David Brownell
 * Copyright (c) 2003 Michal Sojka, for high-speed iso transfers
4
 *
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* this file is part of ehci-hcd.c */

/*-------------------------------------------------------------------------*/

/*
 * EHCI scheduled transaction support:  interrupt, iso, split iso
 * These are called "periodic" transactions in the EHCI spec.
 *
 * Note that for interrupt transfers, the QH/QTD manipulation is shared
 * with the "asynchronous" transaction support (control/bulk transfers).
 * The only real difference is in how interrupt transfers are scheduled.
 *
 * For ISO, we make an "iso_stream" head to serve the same role as a QH.
 * It keeps track of every ITD (or SITD) that's linked, and holds enough
 * pre-calculated schedule data to make appending to the queue be quick.
 */

static int ehci_get_frame (struct usb_hcd *hcd);

/*
 * periodic_next_shadow - return "next" pointer on shadow list
 * @periodic: host pointer to qh/itd/sitd
 * @tag: hardware tag for type of this record
 */
static union ehci_shadow *
45 46
periodic_next_shadow(struct ehci_hcd *ehci, union ehci_shadow *periodic,
		__hc32 tag)
L
Linus Torvalds 已提交
47
{
48
	switch (hc32_to_cpu(ehci, tag)) {
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56 57 58 59 60
	case Q_TYPE_QH:
		return &periodic->qh->qh_next;
	case Q_TYPE_FSTN:
		return &periodic->fstn->fstn_next;
	case Q_TYPE_ITD:
		return &periodic->itd->itd_next;
	// case Q_TYPE_SITD:
	default:
		return &periodic->sitd->sitd_next;
	}
}

61 62 63 64 65 66 67 68 69 70 71 72 73 74
static __hc32 *
shadow_next_periodic(struct ehci_hcd *ehci, union ehci_shadow *periodic,
		__hc32 tag)
{
	switch (hc32_to_cpu(ehci, tag)) {
	/* our ehci_shadow.qh is actually software part */
	case Q_TYPE_QH:
		return &periodic->qh->hw->hw_next;
	/* others are hw parts */
	default:
		return periodic->hw_next;
	}
}

L
Linus Torvalds 已提交
75 76 77
/* caller must hold ehci->lock */
static void periodic_unlink (struct ehci_hcd *ehci, unsigned frame, void *ptr)
{
78 79
	union ehci_shadow	*prev_p = &ehci->pshadow[frame];
	__hc32			*hw_p = &ehci->periodic[frame];
L
Linus Torvalds 已提交
80 81 82 83
	union ehci_shadow	here = *prev_p;

	/* find predecessor of "ptr"; hw and shadow lists are in sync */
	while (here.ptr && here.ptr != ptr) {
84 85
		prev_p = periodic_next_shadow(ehci, prev_p,
				Q_NEXT_TYPE(ehci, *hw_p));
86 87
		hw_p = shadow_next_periodic(ehci, &here,
				Q_NEXT_TYPE(ehci, *hw_p));
L
Linus Torvalds 已提交
88 89 90 91 92 93 94 95 96
		here = *prev_p;
	}
	/* an interrupt entry (at list end) could have been shared */
	if (!here.ptr)
		return;

	/* update shadow and hardware lists ... the old "next" pointers
	 * from ptr may still be in use, the caller updates them.
	 */
97 98
	*prev_p = *periodic_next_shadow(ehci, &here,
			Q_NEXT_TYPE(ehci, *hw_p));
99 100 101 102 103 104 105 106

	if (!ehci->use_dummy_qh ||
	    *shadow_next_periodic(ehci, &here, Q_NEXT_TYPE(ehci, *hw_p))
			!= EHCI_LIST_END(ehci))
		*hw_p = *shadow_next_periodic(ehci, &here,
				Q_NEXT_TYPE(ehci, *hw_p));
	else
		*hw_p = ehci->dummy->qh_dma;
L
Linus Torvalds 已提交
107 108 109 110 111 112
}

/* how many of the uframe's 125 usecs are allocated? */
static unsigned short
periodic_usecs (struct ehci_hcd *ehci, unsigned frame, unsigned uframe)
{
113
	__hc32			*hw_p = &ehci->periodic [frame];
L
Linus Torvalds 已提交
114 115
	union ehci_shadow	*q = &ehci->pshadow [frame];
	unsigned		usecs = 0;
116
	struct ehci_qh_hw	*hw;
L
Linus Torvalds 已提交
117 118

	while (q->ptr) {
119
		switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
L
Linus Torvalds 已提交
120
		case Q_TYPE_QH:
121
			hw = q->qh->hw;
L
Linus Torvalds 已提交
122
			/* is it in the S-mask? */
123
			if (hw->hw_info2 & cpu_to_hc32(ehci, 1 << uframe))
L
Linus Torvalds 已提交
124 125
				usecs += q->qh->usecs;
			/* ... or C-mask? */
126
			if (hw->hw_info2 & cpu_to_hc32(ehci,
127
					1 << (8 + uframe)))
L
Linus Torvalds 已提交
128
				usecs += q->qh->c_usecs;
129
			hw_p = &hw->hw_next;
L
Linus Torvalds 已提交
130 131 132 133 134 135 136
			q = &q->qh->qh_next;
			break;
		// case Q_TYPE_FSTN:
		default:
			/* for "save place" FSTNs, count the relevant INTR
			 * bandwidth from the previous frame
			 */
137
			if (q->fstn->hw_prev != EHCI_LIST_END(ehci)) {
L
Linus Torvalds 已提交
138 139 140 141 142 143
				ehci_dbg (ehci, "ignoring FSTN cost ...\n");
			}
			hw_p = &q->fstn->hw_next;
			q = &q->fstn->fstn_next;
			break;
		case Q_TYPE_ITD:
144 145
			if (q->itd->hw_transaction[uframe])
				usecs += q->itd->stream->usecs;
L
Linus Torvalds 已提交
146 147 148 149 150
			hw_p = &q->itd->hw_next;
			q = &q->itd->itd_next;
			break;
		case Q_TYPE_SITD:
			/* is it in the S-mask?  (count SPLIT, DATA) */
151 152
			if (q->sitd->hw_uframe & cpu_to_hc32(ehci,
					1 << uframe)) {
L
Linus Torvalds 已提交
153
				if (q->sitd->hw_fullspeed_ep &
154
						cpu_to_hc32(ehci, 1<<31))
L
Linus Torvalds 已提交
155 156 157 158 159 160 161
					usecs += q->sitd->stream->usecs;
				else	/* worst case for OUT start-split */
					usecs += HS_USECS_ISO (188);
			}

			/* ... C-mask?  (count CSPLIT, DATA) */
			if (q->sitd->hw_uframe &
162
					cpu_to_hc32(ehci, 1 << (8 + uframe))) {
L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170 171 172
				/* worst case for IN complete-split */
				usecs += q->sitd->stream->c_usecs;
			}

			hw_p = &q->sitd->hw_next;
			q = &q->sitd->sitd_next;
			break;
		}
	}
#ifdef	DEBUG
173
	if (usecs > ehci->uframe_periodic_max)
L
Linus Torvalds 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
		ehci_err (ehci, "uframe %d sched overrun: %d usecs\n",
			frame * 8 + uframe, usecs);
#endif
	return usecs;
}

/*-------------------------------------------------------------------------*/

static int same_tt (struct usb_device *dev1, struct usb_device *dev2)
{
	if (!dev1->tt || !dev2->tt)
		return 0;
	if (dev1->tt != dev2->tt)
		return 0;
	if (dev1->tt->multi)
		return dev1->ttport == dev2->ttport;
	else
		return 1;
}

194 195 196 197 198 199 200 201 202 203
#ifdef CONFIG_USB_EHCI_TT_NEWSCHED

/* Which uframe does the low/fullspeed transfer start in?
 *
 * The parameter is the mask of ssplits in "H-frame" terms
 * and this returns the transfer start uframe in "B-frame" terms,
 * which allows both to match, e.g. a ssplit in "H-frame" uframe 0
 * will cause a transfer in "B-frame" uframe 0.  "B-frames" lag
 * "H-frames" by 1 uframe.  See the EHCI spec sec 4.5 and figure 4.7.
 */
204
static inline unsigned char tt_start_uframe(struct ehci_hcd *ehci, __hc32 mask)
205
{
206
	unsigned char smask = QH_SMASK & hc32_to_cpu(ehci, mask);
207 208 209 210 211 212 213 214 215
	if (!smask) {
		ehci_err(ehci, "invalid empty smask!\n");
		/* uframe 7 can't have bw so this will indicate failure */
		return 7;
	}
	return ffs(smask) - 1;
}

static const unsigned char
216
max_tt_usecs[] = { 125, 125, 125, 125, 125, 125, 125, 25 };
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

/* carryover low/fullspeed bandwidth that crosses uframe boundries */
static inline void carryover_tt_bandwidth(unsigned short tt_usecs[8])
{
	int i;
	for (i=0; i<7; i++) {
		if (max_tt_usecs[i] < tt_usecs[i]) {
			tt_usecs[i+1] += tt_usecs[i] - max_tt_usecs[i];
			tt_usecs[i] = max_tt_usecs[i];
		}
	}
}

/* How many of the tt's periodic downstream 1000 usecs are allocated?
 *
 * While this measures the bandwidth in terms of usecs/uframe,
 * the low/fullspeed bus has no notion of uframes, so any particular
 * low/fullspeed transfer can "carry over" from one uframe to the next,
 * since the TT just performs downstream transfers in sequence.
 *
J
Joe Perches 已提交
237
 * For example two separate 100 usec transfers can start in the same uframe,
238 239 240 241 242 243 244 245 246 247
 * and the second one would "carry over" 75 usecs into the next uframe.
 */
static void
periodic_tt_usecs (
	struct ehci_hcd *ehci,
	struct usb_device *dev,
	unsigned frame,
	unsigned short tt_usecs[8]
)
{
248
	__hc32			*hw_p = &ehci->periodic [frame];
249 250 251 252 253 254
	union ehci_shadow	*q = &ehci->pshadow [frame];
	unsigned char		uf;

	memset(tt_usecs, 0, 16);

	while (q->ptr) {
255
		switch (hc32_to_cpu(ehci, Q_NEXT_TYPE(ehci, *hw_p))) {
256 257 258 259 260 261
		case Q_TYPE_ITD:
			hw_p = &q->itd->hw_next;
			q = &q->itd->itd_next;
			continue;
		case Q_TYPE_QH:
			if (same_tt(dev, q->qh->dev)) {
262
				uf = tt_start_uframe(ehci, q->qh->hw->hw_info2);
263 264
				tt_usecs[uf] += q->qh->tt_usecs;
			}
265
			hw_p = &q->qh->hw->hw_next;
266 267 268 269 270 271 272 273 274 275 276 277
			q = &q->qh->qh_next;
			continue;
		case Q_TYPE_SITD:
			if (same_tt(dev, q->sitd->urb->dev)) {
				uf = tt_start_uframe(ehci, q->sitd->hw_uframe);
				tt_usecs[uf] += q->sitd->stream->tt_usecs;
			}
			hw_p = &q->sitd->hw_next;
			q = &q->sitd->sitd_next;
			continue;
		// case Q_TYPE_FSTN:
		default:
278 279
			ehci_dbg(ehci, "ignoring periodic frame %d FSTN\n",
					frame);
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
			hw_p = &q->fstn->hw_next;
			q = &q->fstn->fstn_next;
		}
	}

	carryover_tt_bandwidth(tt_usecs);

	if (max_tt_usecs[7] < tt_usecs[7])
		ehci_err(ehci, "frame %d tt sched overrun: %d usecs\n",
			frame, tt_usecs[7] - max_tt_usecs[7]);
}

/*
 * Return true if the device's tt's downstream bus is available for a
 * periodic transfer of the specified length (usecs), starting at the
 * specified frame/uframe.  Note that (as summarized in section 11.19
 * of the usb 2.0 spec) TTs can buffer multiple transactions for each
 * uframe.
 *
 * The uframe parameter is when the fullspeed/lowspeed transfer
 * should be executed in "B-frame" terms, which is the same as the
 * highspeed ssplit's uframe (which is in "H-frame" terms).  For example
 * a ssplit in "H-frame" 0 causes a transfer in "B-frame" 0.
 * See the EHCI spec sec 4.5 and fig 4.7.
 *
 * This checks if the full/lowspeed bus, at the specified starting uframe,
 * has the specified bandwidth available, according to rules listed
 * in USB 2.0 spec section 11.18.1 fig 11-60.
 *
 * This does not check if the transfer would exceed the max ssplit
 * limit of 16, specified in USB 2.0 spec section 11.18.4 requirement #4,
 * since proper scheduling limits ssplits to less than 16 per uframe.
 */
static int tt_available (
	struct ehci_hcd		*ehci,
	unsigned		period,
	struct usb_device	*dev,
	unsigned		frame,
	unsigned		uframe,
	u16			usecs
)
{
	if ((period == 0) || (uframe >= 7))	/* error */
		return 0;

	for (; frame < ehci->periodic_size; frame += period) {
		unsigned short tt_usecs[8];

		periodic_tt_usecs (ehci, dev, frame, tt_usecs);

		ehci_vdbg(ehci, "tt frame %d check %d usecs start uframe %d in"
			" schedule %d/%d/%d/%d/%d/%d/%d/%d\n",
			frame, usecs, uframe,
			tt_usecs[0], tt_usecs[1], tt_usecs[2], tt_usecs[3],
			tt_usecs[4], tt_usecs[5], tt_usecs[6], tt_usecs[7]);

		if (max_tt_usecs[uframe] <= tt_usecs[uframe]) {
			ehci_vdbg(ehci, "frame %d uframe %d fully scheduled\n",
				frame, uframe);
			return 0;
		}

		/* special case for isoc transfers larger than 125us:
		 * the first and each subsequent fully used uframe
		 * must be empty, so as to not illegally delay
		 * already scheduled transactions
		 */
		if (125 < usecs) {
348
			int ufs = (usecs / 125);
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
			int i;
			for (i = uframe; i < (uframe + ufs) && i < 8; i++)
				if (0 < tt_usecs[i]) {
					ehci_vdbg(ehci,
						"multi-uframe xfer can't fit "
						"in frame %d uframe %d\n",
						frame, i);
					return 0;
				}
		}

		tt_usecs[uframe] += usecs;

		carryover_tt_bandwidth(tt_usecs);

		/* fail if the carryover pushed bw past the last uframe's limit */
		if (max_tt_usecs[7] < tt_usecs[7]) {
			ehci_vdbg(ehci,
				"tt unavailable usecs %d frame %d uframe %d\n",
				usecs, frame, uframe);
			return 0;
		}
	}

	return 1;
}

#else

L
Linus Torvalds 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
/* return true iff the device's transaction translator is available
 * for a periodic transfer starting at the specified frame, using
 * all the uframes in the mask.
 */
static int tt_no_collision (
	struct ehci_hcd		*ehci,
	unsigned		period,
	struct usb_device	*dev,
	unsigned		frame,
	u32			uf_mask
)
{
	if (period == 0)	/* error */
		return 0;

	/* note bandwidth wastage:  split never follows csplit
	 * (different dev or endpoint) until the next uframe.
	 * calling convention doesn't make that distinction.
	 */
	for (; frame < ehci->periodic_size; frame += period) {
		union ehci_shadow	here;
399
		__hc32			type;
400
		struct ehci_qh_hw	*hw;
L
Linus Torvalds 已提交
401 402

		here = ehci->pshadow [frame];
403
		type = Q_NEXT_TYPE(ehci, ehci->periodic [frame]);
L
Linus Torvalds 已提交
404
		while (here.ptr) {
405
			switch (hc32_to_cpu(ehci, type)) {
L
Linus Torvalds 已提交
406
			case Q_TYPE_ITD:
407
				type = Q_NEXT_TYPE(ehci, here.itd->hw_next);
L
Linus Torvalds 已提交
408 409 410
				here = here.itd->itd_next;
				continue;
			case Q_TYPE_QH:
411
				hw = here.qh->hw;
L
Linus Torvalds 已提交
412 413 414
				if (same_tt (dev, here.qh->dev)) {
					u32		mask;

415
					mask = hc32_to_cpu(ehci,
416
							hw->hw_info2);
L
Linus Torvalds 已提交
417 418 419 420 421
					/* "knows" no gap is needed */
					mask |= mask >> 8;
					if (mask & uf_mask)
						break;
				}
422
				type = Q_NEXT_TYPE(ehci, hw->hw_next);
L
Linus Torvalds 已提交
423 424 425 426 427 428
				here = here.qh->qh_next;
				continue;
			case Q_TYPE_SITD:
				if (same_tt (dev, here.sitd->urb->dev)) {
					u16		mask;

429
					mask = hc32_to_cpu(ehci, here.sitd
L
Linus Torvalds 已提交
430 431 432 433 434 435
								->hw_uframe);
					/* FIXME assumes no gap for IN! */
					mask |= mask >> 8;
					if (mask & uf_mask)
						break;
				}
436
				type = Q_NEXT_TYPE(ehci, here.sitd->hw_next);
L
Linus Torvalds 已提交
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
				here = here.sitd->sitd_next;
				continue;
			// case Q_TYPE_FSTN:
			default:
				ehci_dbg (ehci,
					"periodic frame %d bogus type %d\n",
					frame, type);
			}

			/* collision or error */
			return 0;
		}
	}

	/* no collision */
	return 1;
}

455 456
#endif /* CONFIG_USB_EHCI_TT_NEWSCHED */

L
Linus Torvalds 已提交
457 458
/*-------------------------------------------------------------------------*/

A
Alan Stern 已提交
459
static void enable_periodic(struct ehci_hcd *ehci)
L
Linus Torvalds 已提交
460
{
461
	if (ehci->periodic_count++)
A
Alan Stern 已提交
462
		return;
463

464 465
	/* Stop waiting to turn off the periodic schedule */
	ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_DISABLE_PERIODIC);
L
Linus Torvalds 已提交
466

467 468
	/* Don't start the schedule until PSS is 0 */
	ehci_poll_PSS(ehci);
469
	turn_on_io_watchdog(ehci);
L
Linus Torvalds 已提交
470 471
}

A
Alan Stern 已提交
472
static void disable_periodic(struct ehci_hcd *ehci)
L
Linus Torvalds 已提交
473
{
474
	if (--ehci->periodic_count)
A
Alan Stern 已提交
475
		return;
476

477 478
	/* Don't turn off the schedule until PSS is 1 */
	ehci_poll_PSS(ehci);
L
Linus Torvalds 已提交
479 480 481 482 483 484 485 486 487 488
}

/*-------------------------------------------------------------------------*/

/* periodic schedule slots have iso tds (normal or split) first, then a
 * sparse tree for active interrupt transfers.
 *
 * this just links in a qh; caller guarantees uframe masks are set right.
 * no FSTN support (yet; ehci 0.96+)
 */
A
Alan Stern 已提交
489
static void qh_link_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
L
Linus Torvalds 已提交
490 491 492 493 494 495
{
	unsigned	i;
	unsigned	period = qh->period;

	dev_dbg (&qh->dev->dev,
		"link qh%d-%04x/%p start %d [%d/%d us]\n",
496 497
		period, hc32_to_cpup(ehci, &qh->hw->hw_info2)
			& (QH_CMASK | QH_SMASK),
L
Linus Torvalds 已提交
498 499 500 501 502 503 504
		qh, qh->start, qh->usecs, qh->c_usecs);

	/* high bandwidth, or otherwise every microframe */
	if (period == 0)
		period = 1;

	for (i = qh->start; i < ehci->periodic_size; i += period) {
505 506
		union ehci_shadow	*prev = &ehci->pshadow[i];
		__hc32			*hw_p = &ehci->periodic[i];
L
Linus Torvalds 已提交
507
		union ehci_shadow	here = *prev;
508
		__hc32			type = 0;
L
Linus Torvalds 已提交
509 510 511

		/* skip the iso nodes at list head */
		while (here.ptr) {
512 513
			type = Q_NEXT_TYPE(ehci, *hw_p);
			if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
L
Linus Torvalds 已提交
514
				break;
515
			prev = periodic_next_shadow(ehci, prev, type);
516
			hw_p = shadow_next_periodic(ehci, &here, type);
L
Linus Torvalds 已提交
517 518 519 520 521 522 523 524 525 526
			here = *prev;
		}

		/* sorting each branch by period (slow-->fast)
		 * enables sharing interior tree nodes
		 */
		while (here.ptr && qh != here.qh) {
			if (qh->period > here.qh->period)
				break;
			prev = &here.qh->qh_next;
527
			hw_p = &here.qh->hw->hw_next;
L
Linus Torvalds 已提交
528 529 530 531 532 533
			here = *prev;
		}
		/* link in this qh, unless some earlier pass did that */
		if (qh != here.qh) {
			qh->qh_next = here;
			if (here.qh)
534
				qh->hw->hw_next = *hw_p;
L
Linus Torvalds 已提交
535 536
			wmb ();
			prev->qh = qh;
537
			*hw_p = QH_NEXT (ehci, qh->qh_dma);
L
Linus Torvalds 已提交
538 539 540
		}
	}
	qh->qh_state = QH_STATE_LINKED;
541
	qh->xacterrs = 0;
L
Linus Torvalds 已提交
542 543 544 545 546 547

	/* update per-qh bandwidth for usbfs */
	ehci_to_hcd(ehci)->self.bandwidth_allocated += qh->period
		? ((qh->usecs + qh->c_usecs) / qh->period)
		: (qh->usecs * 8);

548 549
	list_add(&qh->intr_node, &ehci->intr_qh_list);

L
Linus Torvalds 已提交
550
	/* maybe enable periodic schedule processing */
551
	++ehci->intr_count;
A
Alan Stern 已提交
552
	enable_periodic(ehci);
L
Linus Torvalds 已提交
553 554
}

A
Alan Stern 已提交
555
static void qh_unlink_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh)
L
Linus Torvalds 已提交
556 557 558 559
{
	unsigned	i;
	unsigned	period;

560 561 562 563 564 565 566 567 568 569 570 571 572 573
	/*
	 * If qh is for a low/full-speed device, simply unlinking it
	 * could interfere with an ongoing split transaction.  To unlink
	 * it safely would require setting the QH_INACTIVATE bit and
	 * waiting at least one frame, as described in EHCI 4.12.2.5.
	 *
	 * We won't bother with any of this.  Instead, we assume that the
	 * only reason for unlinking an interrupt QH while the current URB
	 * is still active is to dequeue all the URBs (flush the whole
	 * endpoint queue).
	 *
	 * If rebalancing the periodic schedule is ever implemented, this
	 * approach will no longer be valid.
	 */
L
Linus Torvalds 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

	/* high bandwidth, or otherwise part of every microframe */
	if ((period = qh->period) == 0)
		period = 1;

	for (i = qh->start; i < ehci->periodic_size; i += period)
		periodic_unlink (ehci, i, qh);

	/* update per-qh bandwidth for usbfs */
	ehci_to_hcd(ehci)->self.bandwidth_allocated -= qh->period
		? ((qh->usecs + qh->c_usecs) / qh->period)
		: (qh->usecs * 8);

	dev_dbg (&qh->dev->dev,
		"unlink qh%d-%04x/%p start %d [%d/%d us]\n",
589
		qh->period,
590
		hc32_to_cpup(ehci, &qh->hw->hw_info2) & (QH_CMASK | QH_SMASK),
L
Linus Torvalds 已提交
591 592 593 594 595
		qh, qh->start, qh->usecs, qh->c_usecs);

	/* qh->qh_next still "live" to HC */
	qh->qh_state = QH_STATE_UNLINK;
	qh->qh_next.ptr = NULL;
596 597 598 599 600

	if (ehci->qh_scan_next == qh)
		ehci->qh_scan_next = list_entry(qh->intr_node.next,
				struct ehci_qh, intr_node);
	list_del(&qh->intr_node);
L
Linus Torvalds 已提交
601 602
}

603
static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
L
Linus Torvalds 已提交
604
{
605 606 607 608 609 610 611 612 613
	/* If the QH isn't linked then there's nothing we can do
	 * unless we were called during a giveback, in which case
	 * qh_completions() has to deal with it.
	 */
	if (qh->qh_state != QH_STATE_LINKED) {
		if (qh->qh_state == QH_STATE_COMPLETING)
			qh->needs_rescan = 1;
		return;
	}
L
Linus Torvalds 已提交
614 615 616

	qh_unlink_periodic (ehci, qh);

617 618 619 620 621 622 623
	/* Make sure the unlinks are visible before starting the timer */
	wmb();

	/*
	 * The EHCI spec doesn't say how long it takes the controller to
	 * stop accessing an unlinked interrupt QH.  The timer delay is
	 * 9 uframes; presumably that will be long enough.
L
Linus Torvalds 已提交
624
	 */
625 626 627 628 629
	qh->unlink_cycle = ehci->intr_unlink_cycle;

	/* New entries go at the end of the intr_unlink list */
	if (ehci->intr_unlink)
		ehci->intr_unlink_last->unlink_next = qh;
L
Linus Torvalds 已提交
630
	else
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
		ehci->intr_unlink = qh;
	ehci->intr_unlink_last = qh;

	if (ehci->intr_unlinking)
		;	/* Avoid recursive calls */
	else if (ehci->rh_state < EHCI_RH_RUNNING)
		ehci_handle_intr_unlinks(ehci);
	else if (ehci->intr_unlink == qh) {
		ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true);
		++ehci->intr_unlink_cycle;
	}
}

static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh)
{
	struct ehci_qh_hw	*hw = qh->hw;
	int			rc;
L
Linus Torvalds 已提交
648 649

	qh->qh_state = QH_STATE_IDLE;
650
	hw->hw_next = EHCI_LIST_END(ehci);
651

652 653
	if (!list_empty(&qh->qtd_list))
		qh_completions(ehci, qh);
654 655

	/* reschedule QH iff another request is queued */
656
	if (!list_empty(&qh->qtd_list) && ehci->rh_state == EHCI_RH_RUNNING) {
657 658 659 660 661 662 663 664 665 666 667 668
		rc = qh_schedule(ehci, qh);

		/* An error here likely indicates handshake failure
		 * or no space left in the schedule.  Neither fault
		 * should happen often ...
		 *
		 * FIXME kill the now-dysfunctional queued urbs
		 */
		if (rc != 0)
			ehci_err(ehci, "can't reschedule qh %p, err %d\n",
					qh, rc);
	}
669 670

	/* maybe turn off periodic schedule */
671
	--ehci->intr_count;
672
	disable_periodic(ehci);
L
Linus Torvalds 已提交
673 674 675 676 677
}

/*-------------------------------------------------------------------------*/

static int check_period (
678
	struct ehci_hcd *ehci,
L
Linus Torvalds 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691
	unsigned	frame,
	unsigned	uframe,
	unsigned	period,
	unsigned	usecs
) {
	int		claimed;

	/* complete split running into next frame?
	 * given FSTN support, we could sometimes check...
	 */
	if (uframe >= 8)
		return 0;

692 693
	/* convert "usecs we need" to "max already claimed" */
	usecs = ehci->uframe_periodic_max - usecs;
L
Linus Torvalds 已提交
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720

	/* we "know" 2 and 4 uframe intervals were rejected; so
	 * for period 0, check _every_ microframe in the schedule.
	 */
	if (unlikely (period == 0)) {
		do {
			for (uframe = 0; uframe < 7; uframe++) {
				claimed = periodic_usecs (ehci, frame, uframe);
				if (claimed > usecs)
					return 0;
			}
		} while ((frame += 1) < ehci->periodic_size);

	/* just check the specified uframe, at that period */
	} else {
		do {
			claimed = periodic_usecs (ehci, frame, uframe);
			if (claimed > usecs)
				return 0;
		} while ((frame += period) < ehci->periodic_size);
	}

	// success!
	return 1;
}

static int check_intr_schedule (
721
	struct ehci_hcd		*ehci,
L
Linus Torvalds 已提交
722 723 724
	unsigned		frame,
	unsigned		uframe,
	const struct ehci_qh	*qh,
725
	__hc32			*c_maskp
L
Linus Torvalds 已提交
726 727
)
{
728
	int		retval = -ENOSPC;
729
	u8		mask = 0;
L
Linus Torvalds 已提交
730 731 732 733 734 735 736 737 738 739 740 741

	if (qh->c_usecs && uframe >= 6)		/* FSTN territory? */
		goto done;

	if (!check_period (ehci, frame, uframe, qh->period, qh->usecs))
		goto done;
	if (!qh->c_usecs) {
		retval = 0;
		*c_maskp = 0;
		goto done;
	}

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
	if (tt_available (ehci, qh->period, qh->dev, frame, uframe,
				qh->tt_usecs)) {
		unsigned i;

		/* TODO : this may need FSTN for SSPLIT in uframe 5. */
		for (i=uframe+1; i<8 && i<uframe+4; i++)
			if (!check_period (ehci, frame, i,
						qh->period, qh->c_usecs))
				goto done;
			else
				mask |= 1 << i;

		retval = 0;

757
		*c_maskp = cpu_to_hc32(ehci, mask << 8);
758 759
	}
#else
L
Linus Torvalds 已提交
760 761 762
	/* Make sure this tt's buffer is also available for CSPLITs.
	 * We pessimize a bit; probably the typical full speed case
	 * doesn't need the second CSPLIT.
763
	 *
L
Linus Torvalds 已提交
764 765 766 767
	 * NOTE:  both SPLIT and CSPLIT could be checked in just
	 * one smart pass...
	 */
	mask = 0x03 << (uframe + qh->gap_uf);
768
	*c_maskp = cpu_to_hc32(ehci, mask << 8);
L
Linus Torvalds 已提交
769 770 771 772 773 774 775 776 777 778 779

	mask |= 1 << uframe;
	if (tt_no_collision (ehci, qh->period, qh->dev, frame, mask)) {
		if (!check_period (ehci, frame, uframe + qh->gap_uf + 1,
					qh->period, qh->c_usecs))
			goto done;
		if (!check_period (ehci, frame, uframe + qh->gap_uf,
					qh->period, qh->c_usecs))
			goto done;
		retval = 0;
	}
780
#endif
L
Linus Torvalds 已提交
781 782 783 784 785 786 787
done:
	return retval;
}

/* "first fit" scheduling policy used the first time through,
 * or when the previous schedule slot can't be re-used.
 */
788
static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
L
Linus Torvalds 已提交
789
{
790
	int		status;
L
Linus Torvalds 已提交
791
	unsigned	uframe;
792
	__hc32		c_mask;
L
Linus Torvalds 已提交
793
	unsigned	frame;		/* 0..(qh->period - 1), or NO_FRAME */
794
	struct ehci_qh_hw	*hw = qh->hw;
L
Linus Torvalds 已提交
795

796
	hw->hw_next = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
797 798 799 800
	frame = qh->start;

	/* reuse the previous schedule slots, if we can */
	if (frame < qh->period) {
801
		uframe = ffs(hc32_to_cpup(ehci, &hw->hw_info2) & QH_SMASK);
L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815
		status = check_intr_schedule (ehci, frame, --uframe,
				qh, &c_mask);
	} else {
		uframe = 0;
		c_mask = 0;
		status = -ENOSPC;
	}

	/* else scan the schedule to find a group of slots such that all
	 * uframes have enough periodic bandwidth available.
	 */
	if (status) {
		/* "normal" case, uframing flexible except with splits */
		if (qh->period) {
816 817 818 819
			int		i;

			for (i = qh->period; status && i > 0; --i) {
				frame = ++ehci->random_frame % qh->period;
L
Linus Torvalds 已提交
820 821 822 823 824 825 826
				for (uframe = 0; uframe < 8; uframe++) {
					status = check_intr_schedule (ehci,
							frame, uframe, qh,
							&c_mask);
					if (status == 0)
						break;
				}
827
			}
L
Linus Torvalds 已提交
828 829 830 831 832 833 834 835 836 837 838

		/* qh->period == 0 means every uframe */
		} else {
			frame = 0;
			status = check_intr_schedule (ehci, 0, 0, qh, &c_mask);
		}
		if (status)
			goto done;
		qh->start = frame;

		/* reset S-frame and (maybe) C-frame masks */
839 840
		hw->hw_info2 &= cpu_to_hc32(ehci, ~(QH_CMASK | QH_SMASK));
		hw->hw_info2 |= qh->period
841 842
			? cpu_to_hc32(ehci, 1 << uframe)
			: cpu_to_hc32(ehci, QH_SMASK);
843
		hw->hw_info2 |= c_mask;
L
Linus Torvalds 已提交
844 845 846 847 848 849 850 851 852 853 854
	} else
		ehci_dbg (ehci, "reused qh %p schedule\n", qh);

done:
	return status;
}

static int intr_submit (
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	struct list_head	*qtd_list,
A
Al Viro 已提交
855
	gfp_t			mem_flags
L
Linus Torvalds 已提交
856 857 858 859
) {
	unsigned		epnum;
	unsigned long		flags;
	struct ehci_qh		*qh;
860
	int			status;
L
Linus Torvalds 已提交
861 862 863
	struct list_head	empty;

	/* get endpoint and transfer/schedule data */
864
	epnum = urb->ep->desc.bEndpointAddress;
L
Linus Torvalds 已提交
865 866 867

	spin_lock_irqsave (&ehci->lock, flags);

868
	if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
869
		status = -ESHUTDOWN;
870
		goto done_not_linked;
871
	}
872 873 874
	status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
	if (unlikely(status))
		goto done_not_linked;
875

L
Linus Torvalds 已提交
876 877
	/* get qh and force any scheduling errors */
	INIT_LIST_HEAD (&empty);
878
	qh = qh_append_tds(ehci, urb, &empty, epnum, &urb->ep->hcpriv);
L
Linus Torvalds 已提交
879 880 881 882 883 884 885 886 887 888
	if (qh == NULL) {
		status = -ENOMEM;
		goto done;
	}
	if (qh->qh_state == QH_STATE_IDLE) {
		if ((status = qh_schedule (ehci, qh)) != 0)
			goto done;
	}

	/* then queue the urb's tds to the qh */
889
	qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
L
Linus Torvalds 已提交
890 891
	BUG_ON (qh == NULL);

892 893 894 895 896 897
	/* stuff into the periodic schedule */
	if (qh->qh_state == QH_STATE_IDLE) {
		qh_refresh(ehci, qh);
		qh_link_periodic(ehci, qh);
	}

L
Linus Torvalds 已提交
898 899 900 901
	/* ... update usbfs periodic stats */
	ehci_to_hcd(ehci)->self.bandwidth_int_reqs++;

done:
902 903 904
	if (unlikely(status))
		usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
done_not_linked:
L
Linus Torvalds 已提交
905 906 907 908 909 910 911
	spin_unlock_irqrestore (&ehci->lock, flags);
	if (status)
		qtd_list_free (ehci, urb, qtd_list);

	return status;
}

912 913 914 915 916 917
static void scan_intr(struct ehci_hcd *ehci)
{
	struct ehci_qh		*qh;

	list_for_each_entry_safe(qh, ehci->qh_scan_next, &ehci->intr_qh_list,
			intr_node) {
918

919 920 921 922 923 924 925 926 927 928 929 930
		/* clean any finished work for this qh */
		if (!list_empty(&qh->qtd_list)) {
			int temp;

			/*
			 * Unlinks could happen here; completion reporting
			 * drops the lock.  That's why ehci->qh_scan_next
			 * always holds the next qh to scan; if the next qh
			 * gets unlinked then ehci->qh_scan_next is adjusted
			 * in qh_unlink_periodic().
			 */
			temp = qh_completions(ehci, qh);
931 932
			if (unlikely(temp || (list_empty(&qh->qtd_list) &&
					qh->qh_state == QH_STATE_LINKED)))
933 934 935 936 937
				start_unlink_intr(ehci, qh);
		}
	}
}

L
Linus Torvalds 已提交
938 939 940 941 942
/*-------------------------------------------------------------------------*/

/* ehci_iso_stream ops work with both ITD and SITD */

static struct ehci_iso_stream *
A
Al Viro 已提交
943
iso_stream_alloc (gfp_t mem_flags)
L
Linus Torvalds 已提交
944 945 946
{
	struct ehci_iso_stream *stream;

947
	stream = kzalloc(sizeof *stream, mem_flags);
L
Linus Torvalds 已提交
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
	if (likely (stream != NULL)) {
		INIT_LIST_HEAD(&stream->td_list);
		INIT_LIST_HEAD(&stream->free_list);
		stream->next_uframe = -1;
	}
	return stream;
}

static void
iso_stream_init (
	struct ehci_hcd		*ehci,
	struct ehci_iso_stream	*stream,
	struct usb_device	*dev,
	int			pipe,
	unsigned		interval
)
{
	static const u8 smask_out [] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f };

	u32			buf1;
	unsigned		epnum, maxp;
	int			is_input;
	long			bandwidth;

	/*
	 * this might be a "high bandwidth" highspeed endpoint,
	 * as encoded in the ep descriptor's wMaxPacket field
	 */
	epnum = usb_pipeendpoint (pipe);
	is_input = usb_pipein (pipe) ? USB_DIR_IN : 0;
	maxp = usb_maxpacket(dev, pipe, !is_input);
	if (is_input) {
		buf1 = (1 << 11);
	} else {
		buf1 = 0;
	}

	/* knows about ITD vs SITD */
	if (dev->speed == USB_SPEED_HIGH) {
		unsigned multi = hb_mult(maxp);

		stream->highspeed = 1;

		maxp = max_packet(maxp);
		buf1 |= maxp;
		maxp *= multi;

995 996 997
		stream->buf0 = cpu_to_hc32(ehci, (epnum << 8) | dev->devnum);
		stream->buf1 = cpu_to_hc32(ehci, buf1);
		stream->buf2 = cpu_to_hc32(ehci, multi);
L
Linus Torvalds 已提交
998 999 1000 1001 1002 1003

		/* usbfs wants to report the average usecs per frame tied up
		 * when transfers on this endpoint are scheduled ...
		 */
		stream->usecs = HS_USECS_ISO (maxp);
		bandwidth = stream->usecs * 8;
A
Alan Stern 已提交
1004
		bandwidth /= interval;
L
Linus Torvalds 已提交
1005 1006 1007

	} else {
		u32		addr;
D
david-b@pacbell.net 已提交
1008
		int		think_time;
1009
		int		hs_transfers;
L
Linus Torvalds 已提交
1010 1011 1012 1013 1014 1015 1016 1017 1018

		addr = dev->ttport << 24;
		if (!ehci_is_TDI(ehci)
				|| (dev->tt->hub !=
					ehci_to_hcd(ehci)->self.root_hub))
			addr |= dev->tt->hub->devnum << 16;
		addr |= epnum << 8;
		addr |= dev->devnum;
		stream->usecs = HS_USECS_ISO (maxp);
D
david-b@pacbell.net 已提交
1019 1020 1021
		think_time = dev->tt ? dev->tt->think_time : 0;
		stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
				dev->speed, is_input, 1, maxp));
1022
		hs_transfers = max (1u, (maxp + 187) / 188);
L
Linus Torvalds 已提交
1023 1024 1025 1026 1027 1028 1029 1030
		if (is_input) {
			u32	tmp;

			addr |= 1 << 31;
			stream->c_usecs = stream->usecs;
			stream->usecs = HS_USECS_ISO (1);
			stream->raw_mask = 1;

1031 1032 1033
			/* c-mask as specified in USB 2.0 11.18.4 3.c */
			tmp = (1 << (hs_transfers + 2)) - 1;
			stream->raw_mask |= tmp << (8 + 2);
L
Linus Torvalds 已提交
1034
		} else
1035
			stream->raw_mask = smask_out [hs_transfers - 1];
L
Linus Torvalds 已提交
1036
		bandwidth = stream->usecs + stream->c_usecs;
A
Alan Stern 已提交
1037
		bandwidth /= interval << 3;
L
Linus Torvalds 已提交
1038 1039

		/* stream->splits gets created from raw_mask later */
1040
		stream->address = cpu_to_hc32(ehci, addr);
L
Linus Torvalds 已提交
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	}
	stream->bandwidth = bandwidth;

	stream->udev = dev;

	stream->bEndpointAddress = is_input | epnum;
	stream->interval = interval;
	stream->maxp = maxp;
}

static struct ehci_iso_stream *
iso_stream_find (struct ehci_hcd *ehci, struct urb *urb)
{
	unsigned		epnum;
	struct ehci_iso_stream	*stream;
	struct usb_host_endpoint *ep;
	unsigned long		flags;

	epnum = usb_pipeendpoint (urb->pipe);
	if (usb_pipein(urb->pipe))
		ep = urb->dev->ep_in[epnum];
	else
		ep = urb->dev->ep_out[epnum];

	spin_lock_irqsave (&ehci->lock, flags);
	stream = ep->hcpriv;

	if (unlikely (stream == NULL)) {
		stream = iso_stream_alloc(GFP_ATOMIC);
		if (likely (stream != NULL)) {
			ep->hcpriv = stream;
			stream->ep = ep;
			iso_stream_init(ehci, stream, urb->dev, urb->pipe,
					urb->interval);
		}

1077 1078
	/* if dev->ep [epnum] is a QH, hw is set */
	} else if (unlikely (stream->hw != NULL)) {
L
Linus Torvalds 已提交
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
		ehci_dbg (ehci, "dev %s ep%d%s, not iso??\n",
			urb->dev->devpath, epnum,
			usb_pipein(urb->pipe) ? "in" : "out");
		stream = NULL;
	}

	spin_unlock_irqrestore (&ehci->lock, flags);
	return stream;
}

/*-------------------------------------------------------------------------*/

/* ehci_iso_sched ops can be ITD-only or SITD-only */

static struct ehci_iso_sched *
A
Al Viro 已提交
1094
iso_sched_alloc (unsigned packets, gfp_t mem_flags)
L
Linus Torvalds 已提交
1095 1096 1097 1098 1099
{
	struct ehci_iso_sched	*iso_sched;
	int			size = sizeof *iso_sched;

	size += packets * sizeof (struct ehci_iso_packet);
1100
	iso_sched = kzalloc(size, mem_flags);
L
Linus Torvalds 已提交
1101 1102 1103 1104 1105 1106 1107
	if (likely (iso_sched != NULL)) {
		INIT_LIST_HEAD (&iso_sched->td_list);
	}
	return iso_sched;
}

static inline void
1108 1109
itd_sched_init(
	struct ehci_hcd		*ehci,
L
Linus Torvalds 已提交
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
	struct ehci_iso_sched	*iso_sched,
	struct ehci_iso_stream	*stream,
	struct urb		*urb
)
{
	unsigned	i;
	dma_addr_t	dma = urb->transfer_dma;

	/* how many uframes are needed for these transfers */
	iso_sched->span = urb->number_of_packets * stream->interval;

	/* figure out per-uframe itd fields that we'll need later
	 * when we fit new itds into the schedule.
	 */
	for (i = 0; i < urb->number_of_packets; i++) {
		struct ehci_iso_packet	*uframe = &iso_sched->packet [i];
		unsigned		length;
		dma_addr_t		buf;
		u32			trans;

		length = urb->iso_frame_desc [i].length;
		buf = dma + urb->iso_frame_desc [i].offset;

		trans = EHCI_ISOC_ACTIVE;
		trans |= buf & 0x0fff;
		if (unlikely (((i + 1) == urb->number_of_packets))
				&& !(urb->transfer_flags & URB_NO_INTERRUPT))
			trans |= EHCI_ITD_IOC;
		trans |= length << 16;
1139
		uframe->transaction = cpu_to_hc32(ehci, trans);
L
Linus Torvalds 已提交
1140

1141
		/* might need to cross a buffer page within a uframe */
L
Linus Torvalds 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
		uframe->bufp = (buf & ~(u64)0x0fff);
		buf += length;
		if (unlikely ((uframe->bufp != (buf & ~(u64)0x0fff))))
			uframe->cross = 1;
	}
}

static void
iso_sched_free (
	struct ehci_iso_stream	*stream,
	struct ehci_iso_sched	*iso_sched
)
{
	if (!iso_sched)
		return;
	// caller must hold ehci->lock!
	list_splice (&iso_sched->td_list, &stream->free_list);
	kfree (iso_sched);
}

static int
itd_urb_transaction (
	struct ehci_iso_stream	*stream,
	struct ehci_hcd		*ehci,
	struct urb		*urb,
A
Al Viro 已提交
1167
	gfp_t			mem_flags
L
Linus Torvalds 已提交
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
)
{
	struct ehci_itd		*itd;
	dma_addr_t		itd_dma;
	int			i;
	unsigned		num_itds;
	struct ehci_iso_sched	*sched;
	unsigned long		flags;

	sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
	if (unlikely (sched == NULL))
		return -ENOMEM;

1181
	itd_sched_init(ehci, sched, stream, urb);
L
Linus Torvalds 已提交
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191

	if (urb->interval < 8)
		num_itds = 1 + (sched->span + 7) / 8;
	else
		num_itds = urb->number_of_packets;

	/* allocate/init ITDs */
	spin_lock_irqsave (&ehci->lock, flags);
	for (i = 0; i < num_itds; i++) {

1192 1193 1194
		/*
		 * Use iTDs from the free list, but not iTDs that may
		 * still be in use by the hardware.
L
Linus Torvalds 已提交
1195
		 */
1196 1197
		if (likely(!list_empty(&stream->free_list))) {
			itd = list_first_entry(&stream->free_list,
1198
					struct ehci_itd, itd_list);
1199
			if (itd->frame == ehci->now_frame)
1200
				goto alloc_itd;
L
Linus Torvalds 已提交
1201 1202
			list_del (&itd->itd_list);
			itd_dma = itd->itd_dma;
1203
		} else {
1204
 alloc_itd:
L
Linus Torvalds 已提交
1205 1206 1207 1208
			spin_unlock_irqrestore (&ehci->lock, flags);
			itd = dma_pool_alloc (ehci->itd_pool, mem_flags,
					&itd_dma);
			spin_lock_irqsave (&ehci->lock, flags);
1209 1210 1211 1212 1213
			if (!itd) {
				iso_sched_free(stream, sched);
				spin_unlock_irqrestore(&ehci->lock, flags);
				return -ENOMEM;
			}
L
Linus Torvalds 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
		}

		memset (itd, 0, sizeof *itd);
		itd->itd_dma = itd_dma;
		list_add (&itd->itd_list, &sched->td_list);
	}
	spin_unlock_irqrestore (&ehci->lock, flags);

	/* temporarily store schedule info in hcpriv */
	urb->hcpriv = sched;
	urb->error_count = 0;
	return 0;
}

/*-------------------------------------------------------------------------*/

static inline int
itd_slot_ok (
	struct ehci_hcd		*ehci,
	u32			mod,
	u32			uframe,
	u8			usecs,
	u32			period
)
{
	uframe %= period;
	do {
1241
		/* can't commit more than uframe_periodic_max usec */
L
Linus Torvalds 已提交
1242
		if (periodic_usecs (ehci, uframe >> 3, uframe & 0x7)
1243
				> (ehci->uframe_periodic_max - usecs))
L
Linus Torvalds 已提交
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
			return 0;

		/* we know urb->interval is 2^N uframes */
		uframe += period;
	} while (uframe < mod);
	return 1;
}

static inline int
sitd_slot_ok (
	struct ehci_hcd		*ehci,
	u32			mod,
	struct ehci_iso_stream	*stream,
	u32			uframe,
	struct ehci_iso_sched	*sched,
	u32			period_uframes
)
{
	u32			mask, tmp;
	u32			frame, uf;

	mask = stream->raw_mask << (uframe & 7);

	/* for IN, don't wrap CSPLIT into the next frame */
	if (mask & ~0xffff)
		return 0;

1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
	/* check bandwidth */
	uframe %= period_uframes;
	frame = uframe >> 3;

#ifdef CONFIG_USB_EHCI_TT_NEWSCHED
	/* The tt's fullspeed bus bandwidth must be available.
	 * tt_available scheduling guarantees 10+% for control/bulk.
	 */
	uf = uframe & 7;
	if (!tt_available(ehci, period_uframes >> 3,
			stream->udev, frame, uf, stream->tt_usecs))
		return 0;
#else
	/* tt must be idle for start(s), any gap, and csplit.
	 * assume scheduling slop leaves 10+% for control/bulk.
	 */
	if (!tt_no_collision(ehci, period_uframes >> 3,
			stream->udev, frame, mask))
		return 0;
#endif

L
Linus Torvalds 已提交
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
	/* this multi-pass logic is simple, but performance may
	 * suffer when the schedule data isn't cached.
	 */
	do {
		u32		max_used;

		frame = uframe >> 3;
		uf = uframe & 7;

		/* check starts (OUT uses more than one) */
1302
		max_used = ehci->uframe_periodic_max - stream->usecs;
L
Linus Torvalds 已提交
1303 1304 1305 1306 1307 1308 1309
		for (tmp = stream->raw_mask & 0xff; tmp; tmp >>= 1, uf++) {
			if (periodic_usecs (ehci, frame, uf) > max_used)
				return 0;
		}

		/* for IN, check CSPLIT */
		if (stream->c_usecs) {
1310
			uf = uframe & 7;
1311
			max_used = ehci->uframe_periodic_max - stream->c_usecs;
L
Linus Torvalds 已提交
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
			do {
				tmp = 1 << uf;
				tmp <<= 8;
				if ((stream->raw_mask & tmp) == 0)
					continue;
				if (periodic_usecs (ehci, frame, uf)
						> max_used)
					return 0;
			} while (++uf < 8);
		}

		/* we know urb->interval is 2^N uframes */
		uframe += period_uframes;
	} while (uframe < mod);

1327
	stream->splits = cpu_to_hc32(ehci, stream->raw_mask << (uframe & 7));
L
Linus Torvalds 已提交
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
	return 1;
}

/*
 * This scheduler plans almost as far into the future as it has actual
 * periodic schedule slots.  (Affected by TUNE_FLS, which defaults to
 * "as small as possible" to be cache-friendlier.)  That limits the size
 * transfers you can stream reliably; avoid more than 64 msec per urb.
 * Also avoid queue depths of less than ehci's worst irq latency (affected
 * by the per-urb URB_NO_INTERRUPT hint, the log2_irq_thresh module parameter,
 * and other factors); or more than about 230 msec total (for portability,
 * given EHCI_TUNE_FLS and the slop).  Or, write a smarter scheduler!
 */

1342
#define SCHEDULING_DELAY	40	/* microframes */
L
Linus Torvalds 已提交
1343 1344 1345 1346 1347 1348 1349 1350

static int
iso_stream_schedule (
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	struct ehci_iso_stream	*stream
)
{
1351
	u32			now, base, next, start, period, span;
L
Linus Torvalds 已提交
1352 1353 1354 1355
	int			status;
	unsigned		mod = ehci->periodic_size << 3;
	struct ehci_iso_sched	*sched = urb->hcpriv;

1356 1357 1358 1359 1360 1361 1362
	period = urb->interval;
	span = sched->span;
	if (!stream->highspeed) {
		period <<= 3;
		span <<= 3;
	}

1363
	now = ehci_read_frame_index(ehci) & (mod - 1);
L
Linus Torvalds 已提交
1364

1365 1366
	/* Typical case: reuse current schedule, stream is still active.
	 * Hopefully there are no gaps from the host falling behind
1367 1368
	 * (irq delays etc).  If there are, the behavior depends on
	 * whether URB_ISO_ASAP is set.
L
Linus Torvalds 已提交
1369 1370
	 */
	if (likely (!list_empty (&stream->td_list))) {
1371

1372 1373 1374
		/* Take the isochronous scheduling threshold into account */
		if (ehci->i_thresh)
			next = now + ehci->i_thresh;	/* uframe cache */
1375
		else
1376
			next = (now + 2 + 7) & ~0x07;	/* full frame cache */
1377

1378 1379 1380
		/*
		 * Use ehci->last_iso_frame as the base.  There can't be any
		 * TDs scheduled for earlier than that.
1381
		 */
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
		base = ehci->last_iso_frame << 3;
		next = (next - base) & (mod - 1);
		start = (stream->next_uframe - base) & (mod - 1);

		/* Is the schedule already full? */
		if (unlikely(start < period)) {
			ehci_dbg(ehci, "iso sched full %p (%u-%u < %u mod %u)\n",
					urb, stream->next_uframe, base,
					period, mod);
			status = -ENOSPC;
1392 1393
			goto fail;
		}
1394

1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
		/* Behind the scheduling threshold? */
		if (unlikely(start < next)) {

			/* USB_ISO_ASAP: Round up to the first available slot */
			if (urb->transfer_flags & URB_ISO_ASAP)
				start += (next - start + period - 1) & -period;

			/*
			 * Not ASAP: Use the next slot in the stream.  If
			 * the entire URB falls before the threshold, fail.
			 */
			else if (start + span - period < next) {
				ehci_dbg(ehci, "iso urb late %p (%u+%u < %u)\n",
						urb, start + base,
						span - period, next + base);
				status = -EXDEV;
				goto fail;
			}
		}
1414 1415

		start += base;
L
Linus Torvalds 已提交
1416 1417 1418 1419
	}

	/* need to schedule; when's the next (u)frame we could start?
	 * this is bigger than ehci->i_thresh allows; scheduling itself
1420
	 * isn't free, the delay should handle reasonably slow cpus.  it
L
Linus Torvalds 已提交
1421 1422 1423
	 * can also help high bandwidth if the dma and irq loads don't
	 * jump until after the queue is primed.
	 */
1424
	else {
1425
		int done = 0;
1426

1427 1428
		base = now & ~0x07;
		start = base + SCHEDULING_DELAY;
1429

1430 1431 1432 1433 1434 1435 1436 1437 1438
		/* find a uframe slot with enough bandwidth.
		 * Early uframes are more precious because full-speed
		 * iso IN transfers can't use late uframes,
		 * and therefore they should be allocated last.
		 */
		next = start;
		start += period;
		do {
			start--;
1439 1440 1441 1442
			/* check schedule: enough space? */
			if (stream->highspeed) {
				if (itd_slot_ok(ehci, mod, start,
						stream->usecs, period))
1443
					done = 1;
1444 1445 1446 1447 1448
			} else {
				if ((start % 8) >= 6)
					continue;
				if (sitd_slot_ok(ehci, mod, stream,
						start, sched, period))
1449
					done = 1;
1450
			}
1451
		} while (start > next && !done);
L
Linus Torvalds 已提交
1452

1453
		/* no room in the schedule */
1454
		if (!done) {
1455
			ehci_dbg(ehci, "iso sched full %p", urb);
1456 1457
			status = -ENOSPC;
			goto fail;
L
Linus Torvalds 已提交
1458 1459 1460
		}
	}

1461
	/* Tried to schedule too far into the future? */
1462 1463 1464
	if (unlikely(start - base + span - period >= mod)) {
		ehci_dbg(ehci, "request %p would overflow (%u+%u >= %u)\n",
				urb, start - base, span - period, mod);
1465 1466 1467
		status = -EFBIG;
		goto fail;
	}
L
Linus Torvalds 已提交
1468

1469
	stream->next_uframe = start & (mod - 1);
L
Linus Torvalds 已提交
1470 1471 1472 1473 1474

	/* report high speed start in uframes; full speed, in frames */
	urb->start_frame = stream->next_uframe;
	if (!stream->highspeed)
		urb->start_frame >>= 3;
1475 1476 1477

	/* Make sure scan_isoc() sees these */
	if (ehci->isoc_count == 0)
1478
		ehci->last_iso_frame = now >> 3;
L
Linus Torvalds 已提交
1479
	return 0;
1480 1481 1482 1483 1484

 fail:
	iso_sched_free(stream, sched);
	urb->hcpriv = NULL;
	return status;
L
Linus Torvalds 已提交
1485 1486 1487 1488 1489
}

/*-------------------------------------------------------------------------*/

static inline void
1490 1491
itd_init(struct ehci_hcd *ehci, struct ehci_iso_stream *stream,
		struct ehci_itd *itd)
L
Linus Torvalds 已提交
1492 1493 1494
{
	int i;

1495
	/* it's been recently zeroed */
1496
	itd->hw_next = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
	itd->hw_bufp [0] = stream->buf0;
	itd->hw_bufp [1] = stream->buf1;
	itd->hw_bufp [2] = stream->buf2;

	for (i = 0; i < 8; i++)
		itd->index[i] = -1;

	/* All other fields are filled when scheduling */
}

static inline void
1508 1509
itd_patch(
	struct ehci_hcd		*ehci,
L
Linus Torvalds 已提交
1510 1511 1512
	struct ehci_itd		*itd,
	struct ehci_iso_sched	*iso_sched,
	unsigned		index,
1513
	u16			uframe
L
Linus Torvalds 已提交
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
)
{
	struct ehci_iso_packet	*uf = &iso_sched->packet [index];
	unsigned		pg = itd->pg;

	// BUG_ON (pg == 6 && uf->cross);

	uframe &= 0x07;
	itd->index [uframe] = index;

1524 1525 1526 1527
	itd->hw_transaction[uframe] = uf->transaction;
	itd->hw_transaction[uframe] |= cpu_to_hc32(ehci, pg << 12);
	itd->hw_bufp[pg] |= cpu_to_hc32(ehci, uf->bufp & ~(u32)0);
	itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(uf->bufp >> 32));
L
Linus Torvalds 已提交
1528 1529

	/* iso_frame_desc[].offset must be strictly increasing */
1530
	if (unlikely (uf->cross)) {
L
Linus Torvalds 已提交
1531
		u64	bufp = uf->bufp + 4096;
1532

L
Linus Torvalds 已提交
1533
		itd->pg = ++pg;
1534 1535
		itd->hw_bufp[pg] |= cpu_to_hc32(ehci, bufp & ~(u32)0);
		itd->hw_bufp_hi[pg] |= cpu_to_hc32(ehci, (u32)(bufp >> 32));
L
Linus Torvalds 已提交
1536 1537 1538 1539 1540 1541
	}
}

static inline void
itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
{
C
Clemens Ladisch 已提交
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
	union ehci_shadow	*prev = &ehci->pshadow[frame];
	__hc32			*hw_p = &ehci->periodic[frame];
	union ehci_shadow	here = *prev;
	__hc32			type = 0;

	/* skip any iso nodes which might belong to previous microframes */
	while (here.ptr) {
		type = Q_NEXT_TYPE(ehci, *hw_p);
		if (type == cpu_to_hc32(ehci, Q_TYPE_QH))
			break;
		prev = periodic_next_shadow(ehci, prev, type);
		hw_p = shadow_next_periodic(ehci, &here, type);
		here = *prev;
	}

	itd->itd_next = here;
	itd->hw_next = *hw_p;
	prev->itd = itd;
L
Linus Torvalds 已提交
1560 1561
	itd->frame = frame;
	wmb ();
C
Clemens Ladisch 已提交
1562
	*hw_p = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
L
Linus Torvalds 已提交
1563 1564 1565
}

/* fit urb's itds into the selected schedule slot; activate as needed */
A
Alan Stern 已提交
1566
static void itd_link_urb(
L
Linus Torvalds 已提交
1567 1568 1569 1570 1571 1572
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	unsigned		mod,
	struct ehci_iso_stream	*stream
)
{
1573
	int			packet;
L
Linus Torvalds 已提交
1574 1575 1576 1577
	unsigned		next_uframe, uframe, frame;
	struct ehci_iso_sched	*iso_sched = urb->hcpriv;
	struct ehci_itd		*itd;

1578
	next_uframe = stream->next_uframe & (mod - 1);
L
Linus Torvalds 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589

	if (unlikely (list_empty(&stream->td_list))) {
		ehci_to_hcd(ehci)->self.bandwidth_allocated
				+= stream->bandwidth;
		ehci_vdbg (ehci,
			"schedule devp %s ep%d%s-iso period %d start %d.%d\n",
			urb->dev->devpath, stream->bEndpointAddress & 0x0f,
			(stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
			urb->interval,
			next_uframe >> 3, next_uframe & 0x7);
	}
1590 1591

	if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
1592 1593
		if (ehci->amd_pll_fix == 1)
			usb_amd_quirk_pll_disable();
1594 1595
	}

L
Linus Torvalds 已提交
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
	ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;

	/* fill iTDs uframe by uframe */
	for (packet = 0, itd = NULL; packet < urb->number_of_packets; ) {
		if (itd == NULL) {
			/* ASSERT:  we have all necessary itds */
			// BUG_ON (list_empty (&iso_sched->td_list));

			/* ASSERT:  no itds for this endpoint in this uframe */

			itd = list_entry (iso_sched->td_list.next,
					struct ehci_itd, itd_list);
			list_move_tail (&itd->itd_list, &stream->td_list);
1609
			itd->stream = stream;
1610
			itd->urb = urb;
1611
			itd_init (ehci, stream, itd);
L
Linus Torvalds 已提交
1612 1613 1614 1615 1616
		}

		uframe = next_uframe & 0x07;
		frame = next_uframe >> 3;

1617
		itd_patch(ehci, itd, iso_sched, packet, uframe);
L
Linus Torvalds 已提交
1618 1619

		next_uframe += stream->interval;
1620
		next_uframe &= mod - 1;
L
Linus Torvalds 已提交
1621 1622 1623 1624 1625
		packet++;

		/* link completed itds into the schedule */
		if (((next_uframe >> 3) != frame)
				|| packet == urb->number_of_packets) {
1626
			itd_link(ehci, frame & (ehci->periodic_size - 1), itd);
L
Linus Torvalds 已提交
1627 1628 1629 1630 1631 1632 1633
			itd = NULL;
		}
	}
	stream->next_uframe = next_uframe;

	/* don't need that schedule data any more */
	iso_sched_free (stream, iso_sched);
1634
	urb->hcpriv = stream;
L
Linus Torvalds 已提交
1635

1636
	++ehci->isoc_count;
A
Alan Stern 已提交
1637
	enable_periodic(ehci);
L
Linus Torvalds 已提交
1638 1639 1640 1641
}

#define	ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)

1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
/* Process and recycle a completed ITD.  Return true iff its urb completed,
 * and hence its completion callback probably added things to the hardware
 * schedule.
 *
 * Note that we carefully avoid recycling this descriptor until after any
 * completion callback runs, so that it won't be reused quickly.  That is,
 * assuming (a) no more than two urbs per frame on this endpoint, and also
 * (b) only this endpoint's completions submit URBs.  It seems some silicon
 * corrupts things if you reuse completed descriptors very quickly...
 */
1652 1653
static bool itd_complete(struct ehci_hcd *ehci, struct ehci_itd *itd)
{
L
Linus Torvalds 已提交
1654 1655 1656 1657 1658 1659 1660
	struct urb				*urb = itd->urb;
	struct usb_iso_packet_descriptor	*desc;
	u32					t;
	unsigned				uframe;
	int					urb_index = -1;
	struct ehci_iso_stream			*stream = itd->stream;
	struct usb_device			*dev;
1661
	bool					retval = false;
L
Linus Torvalds 已提交
1662 1663 1664 1665 1666 1667 1668 1669

	/* for each uframe with a packet */
	for (uframe = 0; uframe < 8; uframe++) {
		if (likely (itd->index[uframe] == -1))
			continue;
		urb_index = itd->index[uframe];
		desc = &urb->iso_frame_desc [urb_index];

1670
		t = hc32_to_cpup(ehci, &itd->hw_transaction [uframe]);
L
Linus Torvalds 已提交
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
		itd->hw_transaction [uframe] = 0;

		/* report transfer status */
		if (unlikely (t & ISO_ERRS)) {
			urb->error_count++;
			if (t & EHCI_ISOC_BUF_ERR)
				desc->status = usb_pipein (urb->pipe)
					? -ENOSR  /* hc couldn't read */
					: -ECOMM; /* hc couldn't write */
			else if (t & EHCI_ISOC_BABBLE)
				desc->status = -EOVERFLOW;
			else /* (t & EHCI_ISOC_XACTERR) */
				desc->status = -EPROTO;

			/* HC need not update length with this error */
1686 1687 1688 1689
			if (!(t & EHCI_ISOC_BABBLE)) {
				desc->actual_length = EHCI_ITD_LENGTH(t);
				urb->actual_length += desc->actual_length;
			}
L
Linus Torvalds 已提交
1690 1691
		} else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
			desc->status = 0;
1692 1693
			desc->actual_length = EHCI_ITD_LENGTH(t);
			urb->actual_length += desc->actual_length;
1694 1695
		} else {
			/* URB was too late */
1696
			urb->error_count++;
L
Linus Torvalds 已提交
1697 1698 1699 1700 1701
		}
	}

	/* handle completion now? */
	if (likely ((urb_index + 1) != urb->number_of_packets))
1702
		goto done;
L
Linus Torvalds 已提交
1703 1704 1705 1706 1707 1708

	/* ASSERT: it's really the last itd for this urb
	list_for_each_entry (itd, &stream->td_list, itd_list)
		BUG_ON (itd->urb == urb);
	 */

1709
	/* give urb back to the driver; completion often (re)submits */
1710
	dev = urb->dev;
1711
	ehci_urb_done(ehci, urb, 0);
1712
	retval = true;
L
Linus Torvalds 已提交
1713
	urb = NULL;
1714 1715

	--ehci->isoc_count;
A
Alan Stern 已提交
1716
	disable_periodic(ehci);
L
Linus Torvalds 已提交
1717

1718
	ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
1719
	if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
1720 1721
		if (ehci->amd_pll_fix == 1)
			usb_amd_quirk_pll_enable();
1722 1723
	}

1724
	if (unlikely(list_is_singular(&stream->td_list))) {
L
Linus Torvalds 已提交
1725 1726 1727 1728 1729 1730 1731
		ehci_to_hcd(ehci)->self.bandwidth_allocated
				-= stream->bandwidth;
		ehci_vdbg (ehci,
			"deschedule devp %s ep%d%s-iso\n",
			dev->devpath, stream->bEndpointAddress & 0x0f,
			(stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
	}
K
Karsten Wiese 已提交
1732

1733 1734
done:
	itd->urb = NULL;
1735 1736 1737 1738 1739 1740 1741 1742 1743

	/* Add to the end of the free list for later reuse */
	list_move_tail(&itd->itd_list, &stream->free_list);

	/* Recycle the iTDs when the pipeline is empty (ep no longer in use) */
	if (list_empty(&stream->td_list)) {
		list_splice_tail_init(&stream->free_list,
				&ehci->cached_itd_list);
		start_free_itds(ehci);
K
Karsten Wiese 已提交
1744
	}
1745

1746
	return retval;
L
Linus Torvalds 已提交
1747 1748 1749 1750
}

/*-------------------------------------------------------------------------*/

1751
static int itd_submit (struct ehci_hcd *ehci, struct urb *urb,
A
Al Viro 已提交
1752
	gfp_t mem_flags)
L
Linus Torvalds 已提交
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
{
	int			status = -EINVAL;
	unsigned long		flags;
	struct ehci_iso_stream	*stream;

	/* Get iso_stream head */
	stream = iso_stream_find (ehci, urb);
	if (unlikely (stream == NULL)) {
		ehci_dbg (ehci, "can't get iso stream\n");
		return -ENOMEM;
	}
	if (unlikely (urb->interval != stream->interval)) {
		ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
			stream->interval, urb->interval);
		goto done;
	}

#ifdef EHCI_URB_TRACE
	ehci_dbg (ehci,
		"%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n",
1773
		__func__, urb->dev->devpath, urb,
L
Linus Torvalds 已提交
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
		usb_pipeendpoint (urb->pipe),
		usb_pipein (urb->pipe) ? "in" : "out",
		urb->transfer_buffer_length,
		urb->number_of_packets, urb->interval,
		stream);
#endif

	/* allocate ITDs w/o locking anything */
	status = itd_urb_transaction (stream, ehci, urb, mem_flags);
	if (unlikely (status < 0)) {
		ehci_dbg (ehci, "can't init itds\n");
		goto done;
	}

	/* schedule ... need to lock */
	spin_lock_irqsave (&ehci->lock, flags);
1790
	if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
1791
		status = -ESHUTDOWN;
1792 1793 1794 1795 1796 1797
		goto done_not_linked;
	}
	status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
	if (unlikely(status))
		goto done_not_linked;
	status = iso_stream_schedule(ehci, urb, stream);
1798
	if (likely (status == 0))
L
Linus Torvalds 已提交
1799
		itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
1800 1801
	else
		usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
1802
 done_not_linked:
L
Linus Torvalds 已提交
1803
	spin_unlock_irqrestore (&ehci->lock, flags);
1804
 done:
L
Linus Torvalds 已提交
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
	return status;
}

/*-------------------------------------------------------------------------*/

/*
 * "Split ISO TDs" ... used for USB 1.1 devices going through the
 * TTs in USB 2.0 hubs.  These need microframe scheduling.
 */

static inline void
1816 1817
sitd_sched_init(
	struct ehci_hcd		*ehci,
L
Linus Torvalds 已提交
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
	struct ehci_iso_sched	*iso_sched,
	struct ehci_iso_stream	*stream,
	struct urb		*urb
)
{
	unsigned	i;
	dma_addr_t	dma = urb->transfer_dma;

	/* how many frames are needed for these transfers */
	iso_sched->span = urb->number_of_packets * stream->interval;

	/* figure out per-frame sitd fields that we'll need later
	 * when we fit new sitds into the schedule.
	 */
	for (i = 0; i < urb->number_of_packets; i++) {
		struct ehci_iso_packet	*packet = &iso_sched->packet [i];
		unsigned		length;
		dma_addr_t		buf;
		u32			trans;

		length = urb->iso_frame_desc [i].length & 0x03ff;
		buf = dma + urb->iso_frame_desc [i].offset;

		trans = SITD_STS_ACTIVE;
		if (((i + 1) == urb->number_of_packets)
				&& !(urb->transfer_flags & URB_NO_INTERRUPT))
			trans |= SITD_IOC;
		trans |= length << 16;
1846
		packet->transaction = cpu_to_hc32(ehci, trans);
L
Linus Torvalds 已提交
1847 1848 1849 1850 1851 1852 1853

		/* might need to cross a buffer page within a td */
		packet->bufp = buf;
		packet->buf1 = (buf + length) & ~0x0fff;
		if (packet->buf1 != (buf & ~(u64)0x0fff))
			packet->cross = 1;

1854
		/* OUT uses multiple start-splits */
L
Linus Torvalds 已提交
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
		if (stream->bEndpointAddress & USB_DIR_IN)
			continue;
		length = (length + 187) / 188;
		if (length > 1) /* BEGIN vs ALL */
			length |= 1 << 3;
		packet->buf1 |= length;
	}
}

static int
sitd_urb_transaction (
	struct ehci_iso_stream	*stream,
	struct ehci_hcd		*ehci,
	struct urb		*urb,
A
Al Viro 已提交
1869
	gfp_t			mem_flags
L
Linus Torvalds 已提交
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
)
{
	struct ehci_sitd	*sitd;
	dma_addr_t		sitd_dma;
	int			i;
	struct ehci_iso_sched	*iso_sched;
	unsigned long		flags;

	iso_sched = iso_sched_alloc (urb->number_of_packets, mem_flags);
	if (iso_sched == NULL)
		return -ENOMEM;

1882
	sitd_sched_init(ehci, iso_sched, stream, urb);
L
Linus Torvalds 已提交
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892

	/* allocate/init sITDs */
	spin_lock_irqsave (&ehci->lock, flags);
	for (i = 0; i < urb->number_of_packets; i++) {

		/* NOTE:  for now, we don't try to handle wraparound cases
		 * for IN (using sitd->hw_backpointer, like a FSTN), which
		 * means we never need two sitds for full speed packets.
		 */

1893 1894 1895
		/*
		 * Use siTDs from the free list, but not siTDs that may
		 * still be in use by the hardware.
L
Linus Torvalds 已提交
1896
		 */
1897 1898
		if (likely(!list_empty(&stream->free_list))) {
			sitd = list_first_entry(&stream->free_list,
L
Linus Torvalds 已提交
1899
					 struct ehci_sitd, sitd_list);
1900
			if (sitd->frame == ehci->now_frame)
1901
				goto alloc_sitd;
L
Linus Torvalds 已提交
1902 1903
			list_del (&sitd->sitd_list);
			sitd_dma = sitd->sitd_dma;
1904
		} else {
1905
 alloc_sitd:
L
Linus Torvalds 已提交
1906 1907 1908 1909
			spin_unlock_irqrestore (&ehci->lock, flags);
			sitd = dma_pool_alloc (ehci->sitd_pool, mem_flags,
					&sitd_dma);
			spin_lock_irqsave (&ehci->lock, flags);
1910 1911 1912 1913 1914
			if (!sitd) {
				iso_sched_free(stream, iso_sched);
				spin_unlock_irqrestore(&ehci->lock, flags);
				return -ENOMEM;
			}
L
Linus Torvalds 已提交
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
		}

		memset (sitd, 0, sizeof *sitd);
		sitd->sitd_dma = sitd_dma;
		list_add (&sitd->sitd_list, &iso_sched->td_list);
	}

	/* temporarily store schedule info in hcpriv */
	urb->hcpriv = iso_sched;
	urb->error_count = 0;

	spin_unlock_irqrestore (&ehci->lock, flags);
	return 0;
}

/*-------------------------------------------------------------------------*/

static inline void
1933 1934
sitd_patch(
	struct ehci_hcd		*ehci,
L
Linus Torvalds 已提交
1935 1936 1937 1938 1939 1940 1941 1942 1943
	struct ehci_iso_stream	*stream,
	struct ehci_sitd	*sitd,
	struct ehci_iso_sched	*iso_sched,
	unsigned		index
)
{
	struct ehci_iso_packet	*uf = &iso_sched->packet [index];
	u64			bufp = uf->bufp;

1944
	sitd->hw_next = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
1945 1946 1947
	sitd->hw_fullspeed_ep = stream->address;
	sitd->hw_uframe = stream->splits;
	sitd->hw_results = uf->transaction;
1948
	sitd->hw_backpointer = EHCI_LIST_END(ehci);
L
Linus Torvalds 已提交
1949 1950

	bufp = uf->bufp;
1951 1952
	sitd->hw_buf[0] = cpu_to_hc32(ehci, bufp);
	sitd->hw_buf_hi[0] = cpu_to_hc32(ehci, bufp >> 32);
L
Linus Torvalds 已提交
1953

1954
	sitd->hw_buf[1] = cpu_to_hc32(ehci, uf->buf1);
L
Linus Torvalds 已提交
1955 1956
	if (uf->cross)
		bufp += 4096;
1957
	sitd->hw_buf_hi[1] = cpu_to_hc32(ehci, bufp >> 32);
L
Linus Torvalds 已提交
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
	sitd->index = index;
}

static inline void
sitd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_sitd *sitd)
{
	/* note: sitd ordering could matter (CSPLIT then SSPLIT) */
	sitd->sitd_next = ehci->pshadow [frame];
	sitd->hw_next = ehci->periodic [frame];
	ehci->pshadow [frame].sitd = sitd;
	sitd->frame = frame;
	wmb ();
1970
	ehci->periodic[frame] = cpu_to_hc32(ehci, sitd->sitd_dma | Q_TYPE_SITD);
L
Linus Torvalds 已提交
1971 1972 1973
}

/* fit urb's sitds into the selected schedule slot; activate as needed */
A
Alan Stern 已提交
1974
static void sitd_link_urb(
L
Linus Torvalds 已提交
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
	struct ehci_hcd		*ehci,
	struct urb		*urb,
	unsigned		mod,
	struct ehci_iso_stream	*stream
)
{
	int			packet;
	unsigned		next_uframe;
	struct ehci_iso_sched	*sched = urb->hcpriv;
	struct ehci_sitd	*sitd;

	next_uframe = stream->next_uframe;

	if (list_empty(&stream->td_list)) {
		/* usbfs ignores TT bandwidth */
		ehci_to_hcd(ehci)->self.bandwidth_allocated
				+= stream->bandwidth;
		ehci_vdbg (ehci,
			"sched devp %s ep%d%s-iso [%d] %dms/%04x\n",
			urb->dev->devpath, stream->bEndpointAddress & 0x0f,
			(stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
1996
			(next_uframe >> 3) & (ehci->periodic_size - 1),
1997
			stream->interval, hc32_to_cpu(ehci, stream->splits));
L
Linus Torvalds 已提交
1998
	}
1999 2000

	if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
2001 2002
		if (ehci->amd_pll_fix == 1)
			usb_amd_quirk_pll_disable();
2003 2004
	}

L
Linus Torvalds 已提交
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
	ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs++;

	/* fill sITDs frame by frame */
	for (packet = 0, sitd = NULL;
			packet < urb->number_of_packets;
			packet++) {

		/* ASSERT:  we have all necessary sitds */
		BUG_ON (list_empty (&sched->td_list));

		/* ASSERT:  no itds for this endpoint in this frame */

		sitd = list_entry (sched->td_list.next,
				struct ehci_sitd, sitd_list);
		list_move_tail (&sitd->sitd_list, &stream->td_list);
2020
		sitd->stream = stream;
2021
		sitd->urb = urb;
L
Linus Torvalds 已提交
2022

2023
		sitd_patch(ehci, stream, sitd, sched, packet);
2024
		sitd_link(ehci, (next_uframe >> 3) & (ehci->periodic_size - 1),
L
Linus Torvalds 已提交
2025 2026 2027 2028
				sitd);

		next_uframe += stream->interval << 3;
	}
2029
	stream->next_uframe = next_uframe & (mod - 1);
L
Linus Torvalds 已提交
2030 2031 2032

	/* don't need that schedule data any more */
	iso_sched_free (stream, sched);
2033
	urb->hcpriv = stream;
L
Linus Torvalds 已提交
2034

2035
	++ehci->isoc_count;
A
Alan Stern 已提交
2036
	enable_periodic(ehci);
L
Linus Torvalds 已提交
2037 2038 2039 2040 2041
}

/*-------------------------------------------------------------------------*/

#define	SITD_ERRS (SITD_STS_ERR | SITD_STS_DBE | SITD_STS_BABBLE \
2042
				| SITD_STS_XACT | SITD_STS_MMF)
L
Linus Torvalds 已提交
2043

2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
/* Process and recycle a completed SITD.  Return true iff its urb completed,
 * and hence its completion callback probably added things to the hardware
 * schedule.
 *
 * Note that we carefully avoid recycling this descriptor until after any
 * completion callback runs, so that it won't be reused quickly.  That is,
 * assuming (a) no more than two urbs per frame on this endpoint, and also
 * (b) only this endpoint's completions submit URBs.  It seems some silicon
 * corrupts things if you reuse completed descriptors very quickly...
 */
2054 2055
static bool sitd_complete(struct ehci_hcd *ehci, struct ehci_sitd *sitd)
{
L
Linus Torvalds 已提交
2056 2057 2058 2059 2060 2061
	struct urb				*urb = sitd->urb;
	struct usb_iso_packet_descriptor	*desc;
	u32					t;
	int					urb_index = -1;
	struct ehci_iso_stream			*stream = sitd->stream;
	struct usb_device			*dev;
2062
	bool					retval = false;
L
Linus Torvalds 已提交
2063 2064 2065

	urb_index = sitd->index;
	desc = &urb->iso_frame_desc [urb_index];
2066
	t = hc32_to_cpup(ehci, &sitd->hw_results);
L
Linus Torvalds 已提交
2067 2068

	/* report transfer status */
2069
	if (unlikely(t & SITD_ERRS)) {
L
Linus Torvalds 已提交
2070 2071 2072 2073 2074 2075 2076 2077 2078
		urb->error_count++;
		if (t & SITD_STS_DBE)
			desc->status = usb_pipein (urb->pipe)
				? -ENOSR  /* hc couldn't read */
				: -ECOMM; /* hc couldn't write */
		else if (t & SITD_STS_BABBLE)
			desc->status = -EOVERFLOW;
		else /* XACT, MMF, etc */
			desc->status = -EPROTO;
2079 2080 2081
	} else if (unlikely(t & SITD_STS_ACTIVE)) {
		/* URB was too late */
		urb->error_count++;
L
Linus Torvalds 已提交
2082 2083
	} else {
		desc->status = 0;
2084 2085
		desc->actual_length = desc->length - SITD_LENGTH(t);
		urb->actual_length += desc->actual_length;
L
Linus Torvalds 已提交
2086 2087 2088 2089
	}

	/* handle completion now? */
	if ((urb_index + 1) != urb->number_of_packets)
2090
		goto done;
L
Linus Torvalds 已提交
2091 2092 2093 2094 2095 2096

	/* ASSERT: it's really the last sitd for this urb
	list_for_each_entry (sitd, &stream->td_list, sitd_list)
		BUG_ON (sitd->urb == urb);
	 */

2097
	/* give urb back to the driver; completion often (re)submits */
2098
	dev = urb->dev;
2099
	ehci_urb_done(ehci, urb, 0);
2100
	retval = true;
L
Linus Torvalds 已提交
2101
	urb = NULL;
2102 2103

	--ehci->isoc_count;
A
Alan Stern 已提交
2104
	disable_periodic(ehci);
L
Linus Torvalds 已提交
2105

2106
	ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
2107
	if (ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs == 0) {
2108 2109
		if (ehci->amd_pll_fix == 1)
			usb_amd_quirk_pll_enable();
2110 2111
	}

2112
	if (list_is_singular(&stream->td_list)) {
L
Linus Torvalds 已提交
2113 2114 2115 2116 2117 2118 2119
		ehci_to_hcd(ehci)->self.bandwidth_allocated
				-= stream->bandwidth;
		ehci_vdbg (ehci,
			"deschedule devp %s ep%d%s-iso\n",
			dev->devpath, stream->bEndpointAddress & 0x0f,
			(stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
	}
2120

2121 2122
done:
	sitd->urb = NULL;
2123 2124 2125 2126 2127 2128 2129 2130 2131

	/* Add to the end of the free list for later reuse */
	list_move_tail(&sitd->sitd_list, &stream->free_list);

	/* Recycle the siTDs when the pipeline is empty (ep no longer in use) */
	if (list_empty(&stream->td_list)) {
		list_splice_tail_init(&stream->free_list,
				&ehci->cached_sitd_list);
		start_free_itds(ehci);
2132
	}
2133

2134
	return retval;
L
Linus Torvalds 已提交
2135 2136 2137
}


2138
static int sitd_submit (struct ehci_hcd *ehci, struct urb *urb,
A
Al Viro 已提交
2139
	gfp_t mem_flags)
L
Linus Torvalds 已提交
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
{
	int			status = -EINVAL;
	unsigned long		flags;
	struct ehci_iso_stream	*stream;

	/* Get iso_stream head */
	stream = iso_stream_find (ehci, urb);
	if (stream == NULL) {
		ehci_dbg (ehci, "can't get iso stream\n");
		return -ENOMEM;
	}
	if (urb->interval != stream->interval) {
		ehci_dbg (ehci, "can't change iso interval %d --> %d\n",
			stream->interval, urb->interval);
		goto done;
	}

#ifdef EHCI_URB_TRACE
	ehci_dbg (ehci,
		"submit %p dev%s ep%d%s-iso len %d\n",
		urb, urb->dev->devpath,
		usb_pipeendpoint (urb->pipe),
		usb_pipein (urb->pipe) ? "in" : "out",
		urb->transfer_buffer_length);
#endif

	/* allocate SITDs */
	status = sitd_urb_transaction (stream, ehci, urb, mem_flags);
	if (status < 0) {
		ehci_dbg (ehci, "can't init sitds\n");
		goto done;
	}

	/* schedule ... need to lock */
	spin_lock_irqsave (&ehci->lock, flags);
2175
	if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
2176
		status = -ESHUTDOWN;
2177 2178 2179 2180 2181 2182
		goto done_not_linked;
	}
	status = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
	if (unlikely(status))
		goto done_not_linked;
	status = iso_stream_schedule(ehci, urb, stream);
2183
	if (status == 0)
L
Linus Torvalds 已提交
2184
		sitd_link_urb (ehci, urb, ehci->periodic_size << 3, stream);
2185 2186
	else
		usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
2187
 done_not_linked:
L
Linus Torvalds 已提交
2188
	spin_unlock_irqrestore (&ehci->lock, flags);
2189
 done:
L
Linus Torvalds 已提交
2190 2191 2192 2193 2194
	return status;
}

/*-------------------------------------------------------------------------*/

2195
static void scan_isoc(struct ehci_hcd *ehci)
L
Linus Torvalds 已提交
2196
{
2197 2198 2199
	unsigned	uf, now_frame, frame;
	unsigned	fmask = ehci->periodic_size - 1;
	bool		modified, live;
L
Linus Torvalds 已提交
2200 2201 2202 2203 2204 2205

	/*
	 * When running, scan from last scan point up to "now"
	 * else clean up by scanning everything that's left.
	 * Touches as few pages as possible:  cache-friendly.
	 */
2206
	if (ehci->rh_state >= EHCI_RH_RUNNING) {
2207 2208 2209
		uf = ehci_read_frame_index(ehci);
		now_frame = (uf >> 3) & fmask;
		live = true;
K
Karsten Wiese 已提交
2210
	} else  {
2211
		now_frame = (ehci->last_iso_frame - 1) & fmask;
2212
		live = false;
K
Karsten Wiese 已提交
2213
	}
2214
	ehci->now_frame = now_frame;
L
Linus Torvalds 已提交
2215

2216
	frame = ehci->last_iso_frame;
L
Linus Torvalds 已提交
2217 2218
	for (;;) {
		union ehci_shadow	q, *q_p;
2219
		__hc32			type, *hw_p;
L
Linus Torvalds 已提交
2220 2221 2222 2223 2224 2225

restart:
		/* scan each element in frame's queue for completions */
		q_p = &ehci->pshadow [frame];
		hw_p = &ehci->periodic [frame];
		q.ptr = q_p->ptr;
2226
		type = Q_NEXT_TYPE(ehci, *hw_p);
2227
		modified = false;
L
Linus Torvalds 已提交
2228 2229

		while (q.ptr != NULL) {
2230
			switch (hc32_to_cpu(ehci, type)) {
L
Linus Torvalds 已提交
2231
			case Q_TYPE_ITD:
2232 2233
				/* If this ITD is still active, leave it for
				 * later processing ... check the next entry.
2234 2235
				 * No need to check for activity unless the
				 * frame is current.
2236
				 */
2237
				if (frame == now_frame && live) {
2238 2239 2240 2241 2242 2243 2244 2245 2246 2247
					rmb();
					for (uf = 0; uf < 8; uf++) {
						if (q.itd->hw_transaction[uf] &
							    ITD_ACTIVE(ehci))
							break;
					}
					if (uf < 8) {
						q_p = &q.itd->itd_next;
						hw_p = &q.itd->hw_next;
						type = Q_NEXT_TYPE(ehci,
2248
							q.itd->hw_next);
2249 2250 2251
						q = *q_p;
						break;
					}
L
Linus Torvalds 已提交
2252 2253
				}

2254 2255 2256
				/* Take finished ITDs out of the schedule
				 * and process them:  recycle, maybe report
				 * URB completion.  HC won't cache the
L
Linus Torvalds 已提交
2257 2258 2259
				 * pointer for much longer, if at all.
				 */
				*q_p = q.itd->itd_next;
2260 2261 2262 2263 2264
				if (!ehci->use_dummy_qh ||
				    q.itd->hw_next != EHCI_LIST_END(ehci))
					*hw_p = q.itd->hw_next;
				else
					*hw_p = ehci->dummy->qh_dma;
2265
				type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
L
Linus Torvalds 已提交
2266
				wmb();
2267
				modified = itd_complete (ehci, q.itd);
L
Linus Torvalds 已提交
2268 2269 2270
				q = *q_p;
				break;
			case Q_TYPE_SITD:
2271 2272
				/* If this SITD is still active, leave it for
				 * later processing ... check the next entry.
2273 2274
				 * No need to check for activity unless the
				 * frame is current.
2275
				 */
2276 2277
				if (((frame == now_frame) ||
				     (((frame + 1) & fmask) == now_frame))
2278 2279 2280 2281
				    && live
				    && (q.sitd->hw_results &
					SITD_ACTIVE(ehci))) {

L
Linus Torvalds 已提交
2282 2283
					q_p = &q.sitd->sitd_next;
					hw_p = &q.sitd->hw_next;
2284 2285
					type = Q_NEXT_TYPE(ehci,
							q.sitd->hw_next);
L
Linus Torvalds 已提交
2286 2287 2288
					q = *q_p;
					break;
				}
2289 2290 2291 2292 2293

				/* Take finished SITDs out of the schedule
				 * and process them:  recycle, maybe report
				 * URB completion.
				 */
L
Linus Torvalds 已提交
2294
				*q_p = q.sitd->sitd_next;
2295 2296 2297 2298 2299
				if (!ehci->use_dummy_qh ||
				    q.sitd->hw_next != EHCI_LIST_END(ehci))
					*hw_p = q.sitd->hw_next;
				else
					*hw_p = ehci->dummy->qh_dma;
2300
				type = Q_NEXT_TYPE(ehci, q.sitd->hw_next);
L
Linus Torvalds 已提交
2301
				wmb();
2302
				modified = sitd_complete (ehci, q.sitd);
L
Linus Torvalds 已提交
2303 2304 2305
				q = *q_p;
				break;
			default:
2306
				ehci_dbg(ehci, "corrupt type %d frame %d shadow %p\n",
L
Linus Torvalds 已提交
2307 2308
					type, frame, q.ptr);
				// BUG ();
2309 2310 2311 2312
				/* FALL THROUGH */
			case Q_TYPE_QH:
			case Q_TYPE_FSTN:
				/* End of the iTDs and siTDs */
L
Linus Torvalds 已提交
2313
				q.ptr = NULL;
2314
				break;
L
Linus Torvalds 已提交
2315 2316 2317
			}

			/* assume completion callbacks modify the queue */
2318 2319
			if (unlikely(modified && ehci->isoc_count > 0))
				goto restart;
L
Linus Torvalds 已提交
2320 2321
		}

2322 2323
		/* Stop when we have reached the current frame */
		if (frame == now_frame)
2324
			break;
2325 2326 2327 2328

		/* The last frame may still have active siTDs */
		ehci->last_iso_frame = frame;
		frame = (frame + 1) & fmask;
2329
	}
L
Linus Torvalds 已提交
2330
}