ohci-q.c 30.3 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4
/*
 * OHCI HCD (Host Controller Driver) for USB.
 * 
 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5
 * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
Linus Torvalds's avatar
Linus Torvalds committed
6
 * 
7
 * This file is licenced under the GPL.
Linus Torvalds's avatar
Linus Torvalds committed
8
 */
9

Linus Torvalds's avatar
Linus Torvalds committed
10 11 12 13 14 15
static void urb_free_priv (struct ohci_hcd *hc, urb_priv_t *urb_priv)
{
	int		last = urb_priv->length - 1;

	if (last >= 0) {
		int		i;
16
		struct td	*td;
Linus Torvalds's avatar
Linus Torvalds committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

		for (i = 0; i <= last; i++) {
			td = urb_priv->td [i];
			if (td)
				td_free (hc, td);
		}
	}

	kfree (urb_priv);
}

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

/*
 * URB goes back to driver, and isn't reissued.
David Brownell's avatar
David Brownell committed
32
 * It's completely gone from HC data structures.
33
 * PRECONDITION:  ohci lock held, irqs blocked.
Linus Torvalds's avatar
Linus Torvalds committed
34
 */
35 36
static void
finish_urb (struct ohci_hcd *ohci, struct urb *urb, struct pt_regs *regs)
Linus Torvalds's avatar
Linus Torvalds committed
37
{
38
	// ASSERT (urb->hcpriv != 0);
Linus Torvalds's avatar
Linus Torvalds committed
39

David Brownell's avatar
David Brownell committed
40 41 42
	urb_free_priv (ohci, urb->hcpriv);
	urb->hcpriv = NULL;

43
	spin_lock (&urb->lock);
David Brownell's avatar
David Brownell committed
44 45
	if (likely (urb->status == -EINPROGRESS))
		urb->status = 0;
46 47 48 49 50 51 52 53 54 55
	/* report short control reads right even though the data TD always
	 * has TD_R set.  (much simpler, but creates the 1-td limit.)
	 */
	if (unlikely (urb->transfer_flags & URB_SHORT_NOT_OK)
			&& unlikely (usb_pipecontrol (urb->pipe))
			&& urb->actual_length < urb->transfer_buffer_length
			&& usb_pipein (urb->pipe)
			&& urb->status == 0) {
		urb->status = -EREMOTEIO;
	}
56
	spin_unlock (&urb->lock);
David Brownell's avatar
David Brownell committed
57

58 59
	switch (usb_pipetype (urb->pipe)) {
	case PIPE_ISOCHRONOUS:
60
		hcd_to_bus (&ohci->hcd)->bandwidth_isoc_reqs--;
61 62
		break;
	case PIPE_INTERRUPT:
63
		hcd_to_bus (&ohci->hcd)->bandwidth_int_reqs--;
64 65 66
		break;
	}

Linus Torvalds's avatar
Linus Torvalds committed
67 68 69
#ifdef OHCI_VERBOSE_DEBUG
	urb_print (urb, "RET", usb_pipeout (urb->pipe));
#endif
70 71 72

	/* urb->complete() can reenter this HCD */
	spin_unlock (&ohci->lock);
David S. Miller's avatar
David S. Miller committed
73
	usb_hcd_giveback_urb (&ohci->hcd, urb, regs);
74 75 76 77 78 79 80 81
	spin_lock (&ohci->lock);

	/* stop periodic dma if it's not needed */
	if (hcd_to_bus (&ohci->hcd)->bandwidth_isoc_reqs == 0
			&& hcd_to_bus (&ohci->hcd)->bandwidth_int_reqs == 0) {
		ohci->hc_control &= ~(OHCI_CTRL_PLE|OHCI_CTRL_IE);
		writel (ohci->hc_control, &ohci->regs->control);
	}
David Brownell's avatar
David Brownell committed
82 83
}

Linus Torvalds's avatar
Linus Torvalds committed
84 85 86 87 88

/*-------------------------------------------------------------------------*
 * ED handling functions
 *-------------------------------------------------------------------------*/  

89 90
/* search for the right schedule branch to use for a periodic ed.
 * does some load balancing; returns the branch, or negative errno.
Linus Torvalds's avatar
Linus Torvalds committed
91
 */
92
static int balance (struct ohci_hcd *ohci, int interval, int load)
Linus Torvalds's avatar
Linus Torvalds committed
93
{
94
	int	i, branch = -ENOSPC;
Linus Torvalds's avatar
Linus Torvalds committed
95

96 97 98
	/* iso periods can be huge; iso tds specify frame numbers */
	if (interval > NUM_INTS)
		interval = NUM_INTS;
Linus Torvalds's avatar
Linus Torvalds committed
99

100 101 102 103 104
	/* search for the least loaded schedule branch of that period
	 * that has enough bandwidth left unreserved.
	 */
	for (i = 0; i < interval ; i++) {
		if (branch < 0 || ohci->load [branch] > ohci->load [i]) {
105
#if 1	/* CONFIG_USB_BANDWIDTH */
106 107 108 109 110 111 112 113 114 115 116 117 118
			int	j;

			/* usb 1.1 says 90% of one frame */
			for (j = i; j < NUM_INTS; j += interval) {
				if ((ohci->load [j] + load) > 900)
					break;
			}
			if (j < NUM_INTS)
				continue;
#endif
			branch = i; 
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
119 120 121 122 123
	return branch;
}

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

124 125 126
/* both iso and interrupt requests have periods; this routine puts them
 * into the schedule tree in the apppropriate place.  most iso devices use
 * 1msec periods, but that's not required.
Linus Torvalds's avatar
Linus Torvalds committed
127
 */
128
static void periodic_link (struct ohci_hcd *ohci, struct ed *ed)
Linus Torvalds's avatar
Linus Torvalds committed
129
{
130
	unsigned	i;
Linus Torvalds's avatar
Linus Torvalds committed
131

132
	ohci_vdbg (ohci, "link %sed %p branch %d [%dus.], interval %d\n",
133 134
		(ed->hwINFO & ED_ISO) ? "iso " : "",
		ed, ed->branch, ed->load, ed->interval);
Linus Torvalds's avatar
Linus Torvalds committed
135

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
		struct ed	**prev = &ohci->periodic [i];
		u32		*prev_p = &ohci->hcca->int_table [i];
		struct ed	*here = *prev;

		/* sorting each branch by period (slow before fast)
		 * lets us share the faster parts of the tree.
		 * (plus maybe: put interrupt eds before iso)
		 */
		while (here && ed != here) {
			if (ed->interval > here->interval)
				break;
			prev = &here->ed_next;
			prev_p = &here->hwNextED;
			here = *prev;
		}
		if (ed != here) {
			ed->ed_next = here;
			if (here)
				ed->hwNextED = *prev_p;
			wmb ();
			*prev = ed;
			*prev_p = cpu_to_le32p (&ed->dma);
159
			wmb();
160 161 162
		}
		ohci->load [i] += ed->load;
	}
163
	hcd_to_bus (&ohci->hcd)->bandwidth_allocated += ed->load / ed->interval;
164
}
Linus Torvalds's avatar
Linus Torvalds committed
165 166 167

/* link an ed into one of the HC chains */

168
static int ed_schedule (struct ohci_hcd *ohci, struct ed *ed)
Linus Torvalds's avatar
Linus Torvalds committed
169
{	 
170
	int	branch;
Linus Torvalds's avatar
Linus Torvalds committed
171 172

	ed->state = ED_OPER;
173 174
	ed->ed_prev = 0;
	ed->ed_next = 0;
175 176 177 178 179 180
	ed->hwNextED = 0;
	wmb ();

	/* we care about rm_list when setting CLE/BLE in case the HC was at
	 * work on some TD when CLE/BLE was turned off, and isn't quiesced
	 * yet.  finish_unlinks() restarts as needed, some upcoming INTR_SF.
181 182 183 184 185 186
	 *
	 * control and bulk EDs are doubly linked (ed_next, ed_prev), but
	 * periodic ones are singly linked (ed_next). that's because the
	 * periodic schedule encodes a tree like figure 3-5 in the ohci
	 * spec:  each qh can have several "previous" nodes, and the tree
	 * doesn't have unused/idle descriptors.
187
	 */
Linus Torvalds's avatar
Linus Torvalds committed
188 189 190
	switch (ed->type) {
	case PIPE_CONTROL:
		if (ohci->ed_controltail == NULL) {
191
			WARN_ON (ohci->hc_control & OHCI_CTRL_CLE);
Linus Torvalds's avatar
Linus Torvalds committed
192 193
			writel (ed->dma, &ohci->regs->ed_controlhead);
		} else {
194
			ohci->ed_controltail->ed_next = ed;
Linus Torvalds's avatar
Linus Torvalds committed
195 196 197
			ohci->ed_controltail->hwNextED = cpu_to_le32 (ed->dma);
		}
		ed->ed_prev = ohci->ed_controltail;
198
		if (!ohci->ed_controltail && !ohci->ed_rm_list) {
199
			wmb();
Linus Torvalds's avatar
Linus Torvalds committed
200
			ohci->hc_control |= OHCI_CTRL_CLE;
201
			writel (0, &ohci->regs->ed_controlcurrent);
Linus Torvalds's avatar
Linus Torvalds committed
202 203
			writel (ohci->hc_control, &ohci->regs->control);
		}
204
		ohci->ed_controltail = ed;
Linus Torvalds's avatar
Linus Torvalds committed
205 206 207 208
		break;

	case PIPE_BULK:
		if (ohci->ed_bulktail == NULL) {
209
			WARN_ON (ohci->hc_control & OHCI_CTRL_BLE);
Linus Torvalds's avatar
Linus Torvalds committed
210 211
			writel (ed->dma, &ohci->regs->ed_bulkhead);
		} else {
212
			ohci->ed_bulktail->ed_next = ed;
Linus Torvalds's avatar
Linus Torvalds committed
213 214 215
			ohci->ed_bulktail->hwNextED = cpu_to_le32 (ed->dma);
		}
		ed->ed_prev = ohci->ed_bulktail;
216
		if (!ohci->ed_bulktail && !ohci->ed_rm_list) {
217
			wmb();
Linus Torvalds's avatar
Linus Torvalds committed
218
			ohci->hc_control |= OHCI_CTRL_BLE;
219
			writel (0, &ohci->regs->ed_bulkcurrent);
Linus Torvalds's avatar
Linus Torvalds committed
220 221
			writel (ohci->hc_control, &ohci->regs->control);
		}
222
		ohci->ed_bulktail = ed;
Linus Torvalds's avatar
Linus Torvalds committed
223 224
		break;

225 226 227 228 229
	// case PIPE_INTERRUPT:
	// case PIPE_ISOCHRONOUS:
	default:
		branch = balance (ohci, ed->interval, ed->load);
		if (branch < 0) {
230
			ohci_dbg (ohci,
231
				"ERR %d, interval %d msecs, load %d\n",
232 233 234
				branch, ed->interval, ed->load);
			// FIXME if there are TDs queued, fail them!
			return branch;
Linus Torvalds's avatar
Linus Torvalds committed
235
		}
236 237
		ed->branch = branch;
		periodic_link (ohci, ed);
Linus Torvalds's avatar
Linus Torvalds committed
238
	}	 	
239 240 241 242

	/* the HC may not see the schedule updates yet, but if it does
	 * then they'll be properly ordered.
	 */
243
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
244 245 246 247
}

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

David Brownell's avatar
David Brownell committed
248
/* scan the periodic table to find and unlink this ED */
249 250 251
static void periodic_unlink (struct ohci_hcd *ohci, struct ed *ed)
{
	int	i;
David Brownell's avatar
David Brownell committed
252

253 254 255 256 257 258 259 260 261 262 263 264
	for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
		struct ed	*temp;
		struct ed	**prev = &ohci->periodic [i];
		u32		*prev_p = &ohci->hcca->int_table [i];

		while (*prev && (temp = *prev) != ed) {
			prev_p = &temp->hwNextED;
			prev = &temp->ed_next;
		}
		if (*prev) {
			*prev_p = ed->hwNextED;
			*prev = ed->ed_next;
David Brownell's avatar
David Brownell committed
265
		}
266
		ohci->load [i] -= ed->load;
David Brownell's avatar
David Brownell committed
267
	}	
268
	hcd_to_bus (&ohci->hcd)->bandwidth_allocated -= ed->load / ed->interval;
269

270
	ohci_vdbg (ohci, "unlink %sed %p branch %d [%dus.], interval %d\n",
271 272
		(ed->hwINFO & ED_ISO) ? "iso " : "",
		ed, ed->branch, ed->load, ed->interval);
David Brownell's avatar
David Brownell committed
273 274
}

Linus Torvalds's avatar
Linus Torvalds committed
275 276 277 278
/* unlink an ed from one of the HC chains. 
 * just the link to the ed is unlinked.
 * the link from the ed still points to another operational ed or 0
 * so the HC can eventually finish the processing of the unlinked ed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
 * (assuming it already started that, which needn't be true).
 *
 * ED_UNLINK is a transient state: the HC may still see this ED, but soon
 * it won't.  ED_SKIP means the HC will finish its current transaction,
 * but won't start anything new.  The TD queue may still grow; device
 * drivers don't know about this HCD-internal state.
 *
 * When the HC can't see the ED, something changes ED_UNLINK to one of:
 *
 *  - ED_OPER: when there's any request queued, the ED gets rescheduled
 *    immediately.  HC should be working on them.
 *
 *  - ED_IDLE:  when there's no TD queue. there's no reason for the HC
 *    to care about this ED; safe to disable the endpoint.
 *
 * When finish_unlinks() runs later, after SOF interrupt, it will often
 * complete one or more URB unlinks before making that state change.
Linus Torvalds's avatar
Linus Torvalds committed
296
 */
297
static void ed_deschedule (struct ohci_hcd *ohci, struct ed *ed) 
Linus Torvalds's avatar
Linus Torvalds committed
298
{
299
	ed->hwINFO |= ED_SKIP;
300 301
	wmb ();
	ed->state = ED_UNLINK;
Linus Torvalds's avatar
Linus Torvalds committed
302

303 304 305 306 307 308 309 310 311 312
	/* To deschedule something from the control or bulk list, just
	 * clear CLE/BLE and wait.  There's no safe way to scrub out list
	 * head/current registers until later, and "later" isn't very
	 * tightly specified.  Figure 6-5 and Section 6.4.2.2 show how
	 * the HC is reading the ED queues (while we modify them).
	 *
	 * For now, ed_schedule() is "later".  It might be good paranoia
	 * to scrub those registers in finish_unlinks(), in case of bugs
	 * that make the HC try to use them.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
313 314
	switch (ed->type) {
	case PIPE_CONTROL:
315
		/* remove ED from the HC's list: */
Linus Torvalds's avatar
Linus Torvalds committed
316 317 318 319
		if (ed->ed_prev == NULL) {
			if (!ed->hwNextED) {
				ohci->hc_control &= ~OHCI_CTRL_CLE;
				writel (ohci->hc_control, &ohci->regs->control);
320 321 322 323
				// a readl() later syncs CLE with the HC
			} else
				writel (le32_to_cpup (&ed->hwNextED),
					&ohci->regs->ed_controlhead);
Linus Torvalds's avatar
Linus Torvalds committed
324
		} else {
325
			ed->ed_prev->ed_next = ed->ed_next;
Linus Torvalds's avatar
Linus Torvalds committed
326 327
			ed->ed_prev->hwNextED = ed->hwNextED;
		}
328
		/* remove ED from the HCD's list: */
Linus Torvalds's avatar
Linus Torvalds committed
329 330
		if (ohci->ed_controltail == ed) {
			ohci->ed_controltail = ed->ed_prev;
331 332
			if (ohci->ed_controltail)
				ohci->ed_controltail->ed_next = 0;
333
		} else if (ed->ed_next) {
334
			ed->ed_next->ed_prev = ed->ed_prev;
Linus Torvalds's avatar
Linus Torvalds committed
335 336 337 338
		}
		break;

	case PIPE_BULK:
339
		/* remove ED from the HC's list: */
Linus Torvalds's avatar
Linus Torvalds committed
340 341 342 343
		if (ed->ed_prev == NULL) {
			if (!ed->hwNextED) {
				ohci->hc_control &= ~OHCI_CTRL_BLE;
				writel (ohci->hc_control, &ohci->regs->control);
344 345 346 347
				// a readl() later syncs BLE with the HC
			} else
				writel (le32_to_cpup (&ed->hwNextED),
					&ohci->regs->ed_bulkhead);
Linus Torvalds's avatar
Linus Torvalds committed
348
		} else {
349
			ed->ed_prev->ed_next = ed->ed_next;
Linus Torvalds's avatar
Linus Torvalds committed
350 351
			ed->ed_prev->hwNextED = ed->hwNextED;
		}
352
		/* remove ED from the HCD's list: */
Linus Torvalds's avatar
Linus Torvalds committed
353 354
		if (ohci->ed_bulktail == ed) {
			ohci->ed_bulktail = ed->ed_prev;
355 356
			if (ohci->ed_bulktail)
				ohci->ed_bulktail->ed_next = 0;
357
		} else if (ed->ed_next) {
358
			ed->ed_next->ed_prev = ed->ed_prev;
Linus Torvalds's avatar
Linus Torvalds committed
359 360 361
		}
		break;

362 363 364 365
	// case PIPE_INTERRUPT:
	// case PIPE_ISOCHRONOUS:
	default:
		periodic_unlink (ohci, ed);
Linus Torvalds's avatar
Linus Torvalds committed
366 367 368 369 370 371 372
		break;
	}
}


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

David Brownell's avatar
David Brownell committed
373 374 375
/* get and maybe (re)init an endpoint. init _should_ be done only as part
 * of usb_set_configuration() or usb_set_interface() ... but the USB stack
 * isn't very stateful, so we re-init whenever the HC isn't looking.
Linus Torvalds's avatar
Linus Torvalds committed
376
 */
David Brownell's avatar
David Brownell committed
377 378
static struct ed *ed_get (
	struct ohci_hcd		*ohci,
Linus Torvalds's avatar
Linus Torvalds committed
379 380
	struct usb_device	*udev,
	unsigned int		pipe,
David Brownell's avatar
David Brownell committed
381
	int			interval
Linus Torvalds's avatar
Linus Torvalds committed
382
) {
David Brownell's avatar
David Brownell committed
383 384
	int			is_out = !usb_pipein (pipe);
	int			type = usb_pipetype (pipe);
Linus Torvalds's avatar
Linus Torvalds committed
385 386 387 388 389 390
	struct hcd_dev		*dev = (struct hcd_dev *) udev->hcpriv;
	struct ed		*ed; 
	unsigned		ep;
	unsigned long		flags;

	ep = usb_pipeendpoint (pipe) << 1;
David Brownell's avatar
David Brownell committed
391
	if (type != PIPE_CONTROL && is_out)
Linus Torvalds's avatar
Linus Torvalds committed
392
		ep |= 1;
David Brownell's avatar
David Brownell committed
393 394 395

	spin_lock_irqsave (&ohci->lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
396
	if (!(ed = dev->ep [ep])) {
397 398
		struct td	*td;

399
		ed = ed_alloc (ohci, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
400 401
		if (!ed) {
			/* out of memory */
David Brownell's avatar
David Brownell committed
402
			goto done;
Linus Torvalds's avatar
Linus Torvalds committed
403 404
		}
		dev->ep [ep] = ed;
David Brownell's avatar
David Brownell committed
405

Linus Torvalds's avatar
Linus Torvalds committed
406
  		/* dummy td; end of td list for ed */
407
		td = td_alloc (ohci, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
408 409
 		if (!td) {
			/* out of memory */
410
			ed_free (ohci, ed);
David Brownell's avatar
David Brownell committed
411 412
			ed = 0;
			goto done;
Linus Torvalds's avatar
Linus Torvalds committed
413
		}
414
		ed->dummy = td;
Linus Torvalds's avatar
Linus Torvalds committed
415
		ed->hwTailP = cpu_to_le32 (td->td_dma);
416
		ed->hwHeadP = ed->hwTailP;	/* ED_C, ED_H zeroed */
417
		ed->state = ED_IDLE;
David Brownell's avatar
David Brownell committed
418
		ed->type = type;
Linus Torvalds's avatar
Linus Torvalds committed
419 420
	}

421 422
	/* NOTE: only ep0 currently needs this "re"init logic, during
	 * enumeration (after set_address, or if ep0 maxpacket >8).
David Brownell's avatar
David Brownell committed
423
	 */
424
  	if (ed->state == ED_IDLE) {
David Brownell's avatar
David Brownell committed
425 426 427 428 429 430 431 432
		u32	info;

		info = usb_pipedevice (pipe);
		info |= (ep >> 1) << 7;
		info |= usb_maxpacket (udev, pipe, is_out) << 16;
		info = cpu_to_le32 (info);
		if (udev->speed == USB_SPEED_LOW)
			info |= ED_LOWSPEED;
433
		/* only control transfers store pids in tds */
David Brownell's avatar
David Brownell committed
434 435
		if (type != PIPE_CONTROL) {
			info |= is_out ? ED_OUT : ED_IN;
436 437 438 439 440
			if (type != PIPE_BULK) {
				/* periodic transfers... */
				if (type == PIPE_ISOCHRONOUS)
					info |= ED_ISO;
				else if (interval > 32)	/* iso can be bigger */
David Brownell's avatar
David Brownell committed
441
					interval = 32;
442 443 444 445 446 447
				ed->interval = interval;
				ed->load = usb_calc_bus_time (
					udev->speed, !is_out,
					type == PIPE_ISOCHRONOUS,
					usb_maxpacket (udev, pipe, is_out))
						/ 1000;
David Brownell's avatar
David Brownell committed
448 449 450 451 452 453
			}
		}
		ed->hwINFO = info;
	}

done:
Linus Torvalds's avatar
Linus Torvalds committed
454 455 456 457 458 459 460
	spin_unlock_irqrestore (&ohci->lock, flags);
	return ed; 
}

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

/* request unlinking of an endpoint from an operational HC.
461
 * put the ep on the rm_list
Linus Torvalds's avatar
Linus Torvalds committed
462
 * real work is done at the next start frame (SF) hardware interrupt
463 464
 * caller guarantees HCD is running, so hardware access is safe,
 * and that ed->state is ED_OPER
Linus Torvalds's avatar
Linus Torvalds committed
465
 */
466
static void start_ed_unlink (struct ohci_hcd *ohci, struct ed *ed)
Linus Torvalds's avatar
Linus Torvalds committed
467
{    
468 469
	ed->hwINFO |= ED_DEQUEUE;
	ed_deschedule (ohci, ed);
Linus Torvalds's avatar
Linus Torvalds committed
470

471 472 473 474 475 476 477 478 479 480 481
	/* rm_list is just singly linked, for simplicity */
	ed->ed_next = ohci->ed_rm_list;
	ed->ed_prev = 0;
	ohci->ed_rm_list = ed;

	/* enable SOF interrupt */
	writel (OHCI_INTR_SF, &ohci->regs->intrstatus);
	writel (OHCI_INTR_SF, &ohci->regs->intrenable);
	// flush those writes, and get latest HCCA contents
	(void) readl (&ohci->regs->control);

482 483 484 485 486
	/* SF interrupt might get delayed; record the frame counter value that
	 * indicates when the HC isn't looking at it, so concurrent unlinks
	 * behave.  frame_no wraps every 2^16 msec, and changes right before
	 * SF is triggered.
	 */
487
	ed->tick = OHCI_FRAME_NO(ohci->hcca) + 1;
488

Linus Torvalds's avatar
Linus Torvalds committed
489 490 491 492 493 494 495 496 497
}

/*-------------------------------------------------------------------------*
 * TD handling functions
 *-------------------------------------------------------------------------*/

/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */

static void
498
td_fill (struct ohci_hcd *ohci, u32 info,
Linus Torvalds's avatar
Linus Torvalds committed
499 500 501
	dma_addr_t data, int len,
	struct urb *urb, int index)
{
502
	struct td		*td, *td_pt;
503
	struct urb_priv		*urb_priv = urb->hcpriv;
504
	int			is_iso = info & TD_ISO;
505
	int			hash;
Linus Torvalds's avatar
Linus Torvalds committed
506

507
	// ASSERT (index < urb_priv->length);
Linus Torvalds's avatar
Linus Torvalds committed
508

509 510
	/* aim for only one interrupt per urb.  mostly applies to control
	 * and iso; other urbs rarely need more than one TD per urb.
David Brownell's avatar
David Brownell committed
511
	 * this way, only final tds (or ones with an error) cause IRQs.
512
	 * at least immediately; use DI=6 in case any control request is
513 514
	 * tempted to die part way through.  (and to force the hc to flush
	 * its donelist soonish, even on unlink paths.)
515 516 517
	 *
	 * NOTE: could delay interrupts even for the last TD, and get fewer
	 * interrupts ... increasing per-urb latency by sharing interrupts.
David Brownell's avatar
David Brownell committed
518
	 * Drivers that queue bulk urbs may request that behavior.
519
	 */
David Brownell's avatar
David Brownell committed
520 521
	if (index != (urb_priv->length - 1)
			|| (urb->transfer_flags & URB_NO_INTERRUPT))
522
		info |= TD_DI_SET (6);
David Brownell's avatar
David Brownell committed
523

Linus Torvalds's avatar
Linus Torvalds committed
524 525 526 527
	/* use this td as the next dummy */
	td_pt = urb_priv->td [index];

	/* fill the old dummy TD */
528 529
	td = urb_priv->td [index] = urb_priv->ed->dummy;
	urb_priv->ed->dummy = td_pt;
Linus Torvalds's avatar
Linus Torvalds committed
530 531 532 533 534 535 536 537 538 539

	td->ed = urb_priv->ed;
	td->next_dl_td = NULL;
	td->index = index;
	td->urb = urb; 
	td->data_dma = data;
	if (!len)
		data = 0;

	td->hwINFO = cpu_to_le32 (info);
540
	if (is_iso) {
Linus Torvalds's avatar
Linus Torvalds committed
541
		td->hwCBP = cpu_to_le32 (data & 0xFFFFF000);
David Brownell's avatar
David Brownell committed
542
		td->hwPSW [0] = cpu_to_le16 ((data & 0x0FFF) | 0xE000);
543
		td->ed->last_iso = info & 0xffff;
Linus Torvalds's avatar
Linus Torvalds committed
544 545 546 547 548 549 550 551 552 553
	} else {
		td->hwCBP = cpu_to_le32 (data); 
	}			
	if (data)
		td->hwBE = cpu_to_le32 (data + len - 1);
	else
		td->hwBE = 0;
	td->hwNextTD = cpu_to_le32 (td_pt->td_dma);

	/* append to queue */
554
	list_add_tail (&td->td_list, &td->ed->td_list);
555 556 557 558 559 560 561 562

	/* hash it for later reverse mapping */
	hash = TD_HASH_FUNC (td->td_dma);
	td->td_hash = ohci->td_hash [hash];
	ohci->td_hash [hash] = td;

	/* HC might read the TD (or cachelines) right away ... */
	wmb ();
Linus Torvalds's avatar
Linus Torvalds committed
563 564 565 566 567
	td->ed->hwTailP = td->hwNextTD;
}

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

568 569 570 571 572 573 574 575 576 577
/* Prepare all TDs of a transfer, and queue them onto the ED.
 * Caller guarantees HC is active.
 * Usually the ED is already on the schedule, so TDs might be
 * processed as soon as they're queued.
 */
static void td_submit_urb (
	struct ohci_hcd	*ohci,
	struct urb	*urb
) {
	struct urb_priv	*urb_priv = urb->hcpriv;
Linus Torvalds's avatar
Linus Torvalds committed
578 579
	dma_addr_t	data;
	int		data_len = urb->transfer_buffer_length;
580 581
	int		cnt = 0;
	u32		info = 0;
David Brownell's avatar
David Brownell committed
582
	int		is_out = usb_pipeout (urb->pipe);
583
	int		periodic = 0;
Linus Torvalds's avatar
Linus Torvalds committed
584

585 586 587
	/* OHCI handles the bulk/interrupt data toggles itself.  We just
	 * use the device toggle bits for resetting, and rely on the fact
	 * that resetting toggle is meaningless if the endpoint is active.
Linus Torvalds's avatar
Linus Torvalds committed
588
	 */
589
  	if (!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), is_out)) {
Linus Torvalds's avatar
Linus Torvalds committed
590
		usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe),
David Brownell's avatar
David Brownell committed
591
			is_out, 1);
592
		urb_priv->ed->hwHeadP &= ~ED_C;
Linus Torvalds's avatar
Linus Torvalds committed
593 594 595 596
	}

	urb_priv->td_cnt = 0;

597 598 599
	if (data_len)
		data = urb->transfer_dma;
	else
Linus Torvalds's avatar
Linus Torvalds committed
600 601
		data = 0;

David Brownell's avatar
David Brownell committed
602 603
	/* NOTE:  TD_CC is set so we can tell which TDs the HC processed by
	 * using TD_CC_GET, as well as by seeing them on the done list.
604
	 * (CC = NotAccessed ... 0x0F, or 0x0E in PSWs for ISO.)
David Brownell's avatar
David Brownell committed
605
	 */
606 607 608 609 610
	switch (urb_priv->ed->type) {

	/* Bulk and interrupt are identical except for where in the schedule
	 * their EDs live.
	 */
611 612
	case PIPE_INTERRUPT:
		/* ... and periodic urbs have extra accounting */
613 614
		periodic = hcd_to_bus (&ohci->hcd)->bandwidth_int_reqs++ == 0
			&& hcd_to_bus (&ohci->hcd)->bandwidth_isoc_reqs == 0;
615 616
		/* FALLTHROUGH */
	case PIPE_BULK:
617 618 619 620 621
		info = is_out
			? TD_T_TOGGLE | TD_CC | TD_DP_OUT
			: TD_T_TOGGLE | TD_CC | TD_DP_IN;
		/* TDs _could_ transfer up to 8K each */
		while (data_len > 4096) {
622
			td_fill (ohci, info, data, 4096, urb, cnt);
623 624
			data += 4096;
			data_len -= 4096;
Linus Torvalds's avatar
Linus Torvalds committed
625
			cnt++;
626 627 628 629
		}
		/* maybe avoid ED halt on final TD short read */
		if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
			info |= TD_R;
630
		td_fill (ohci, info, data, data_len, urb, cnt);
631
		cnt++;
632
		if ((urb->transfer_flags & URB_ZERO_PACKET)
633
				&& cnt < urb_priv->length) {
634
			td_fill (ohci, info, 0, 0, urb, cnt);
635 636 637 638 639 640 641 642 643 644 645 646 647 648
			cnt++;
		}
		/* maybe kickstart bulk list */
		if (urb_priv->ed->type == PIPE_BULK) {
			wmb ();
			writel (OHCI_BLF, &ohci->regs->cmdstatus);
		}
		break;

	/* control manages DATA0/DATA1 toggle per-request; SETUP resets it,
	 * any DATA phase works normally, and the STATUS ack is special.
	 */
	case PIPE_CONTROL:
		info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
649
		td_fill (ohci, info, urb->setup_dma, 8, urb, cnt++);
650 651 652 653
		if (data_len > 0) {
			info = TD_CC | TD_R | TD_T_DATA1;
			info |= is_out ? TD_DP_OUT : TD_DP_IN;
			/* NOTE:  mishandles transfers >8K, some >4K */
654
			td_fill (ohci, info, data, data_len, urb, cnt++);
655 656 657 658
		}
		info = is_out
			? TD_CC | TD_DP_IN | TD_T_DATA1
			: TD_CC | TD_DP_OUT | TD_T_DATA1;
659
		td_fill (ohci, info, data, 0, urb, cnt++);
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
		/* maybe kickstart control list */
		wmb ();
		writel (OHCI_CLF, &ohci->regs->cmdstatus);
		break;

	/* ISO has no retransmit, so no toggle; and it uses special TDs.
	 * Each TD could handle multiple consecutive frames (interval 1);
	 * we could often reduce the number of TDs here.
	 */
	case PIPE_ISOCHRONOUS:
		for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
			int	frame = urb->start_frame;

			// FIXME scheduling should handle frame counter
			// roll-around ... exotic case (and OHCI has
			// a 2^16 iso range, vs other HCs max of 2^10)
			frame += cnt * urb->interval;
			frame &= 0xffff;
678
			td_fill (ohci, TD_CC | TD_ISO | frame,
679 680 681
				data + urb->iso_frame_desc [cnt].offset,
				urb->iso_frame_desc [cnt].length, urb, cnt);
		}
682 683
		periodic = hcd_to_bus (&ohci->hcd)->bandwidth_isoc_reqs++ == 0
			&& hcd_to_bus (&ohci->hcd)->bandwidth_int_reqs == 0;
684 685
		break;
	}
686 687 688

	/* start periodic dma if needed */
	if (periodic) {
689
		wmb ();
690 691 692 693
		ohci->hc_control |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
		writel (ohci->hc_control, &ohci->regs->control);
	}

694
	// ASSERT (urb_priv->length == cnt);
Linus Torvalds's avatar
Linus Torvalds committed
695 696 697 698 699 700
}

/*-------------------------------------------------------------------------*
 * Done List handling functions
 *-------------------------------------------------------------------------*/

David Brownell's avatar
David Brownell committed
701 702 703
/* calculate transfer length/status and update the urb
 * PRECONDITION:  irqsafe (only for urb->status locking)
 */
704
static void td_done (struct ohci_hcd *ohci, struct urb *urb, struct td *td)
Linus Torvalds's avatar
Linus Torvalds committed
705
{
David Brownell's avatar
David Brownell committed
706 707
	u32	tdINFO = le32_to_cpup (&td->hwINFO);
	int	cc = 0;
Linus Torvalds's avatar
Linus Torvalds committed
708

709 710
	list_del (&td->td_list);

David Brownell's avatar
David Brownell committed
711
	/* ISO ... drivers see per-TD length/status */
Linus Torvalds's avatar
Linus Torvalds committed
712
  	if (tdINFO & TD_ISO) {
David Brownell's avatar
David Brownell committed
713 714 715
 		u16	tdPSW = le16_to_cpu (td->hwPSW [0]);
		int	dlen = 0;

716 717
		/* NOTE:  assumes FC in tdINFO == 0 (and MAXPSW == 1) */

Linus Torvalds's avatar
Linus Torvalds committed
718
 		cc = (tdPSW >> 12) & 0xF;
719
  		if (tdINFO & TD_CC)	/* hc didn't touch? */
720 721
			return;

David Brownell's avatar
David Brownell committed
722 723
		if (usb_pipeout (urb->pipe))
			dlen = urb->iso_frame_desc [td->index].length;
724 725 726 727
		else {
			/* short reads are always OK for ISO */
			if (cc == TD_DATAUNDERRUN)
				cc = TD_CC_NOERROR;
David Brownell's avatar
David Brownell committed
728
			dlen = tdPSW & 0x3ff;
729
		}
David Brownell's avatar
David Brownell committed
730 731 732 733
		urb->actual_length += dlen;
		urb->iso_frame_desc [td->index].actual_length = dlen;
		urb->iso_frame_desc [td->index].status = cc_to_error [cc];

734
		if (cc != TD_CC_NOERROR)
735 736
			ohci_vdbg (ohci,
				"urb %p iso td %p (%d) len %d cc %d\n",
David Brownell's avatar
David Brownell committed
737
				urb, td, 1 + td->index, dlen, cc);
David Brownell's avatar
David Brownell committed
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754

	/* BULK, INT, CONTROL ... drivers see aggregate length/status,
	 * except that "setup" bytes aren't counted and "short" transfers
	 * might not be reported as errors.
	 */
	} else {
		int	type = usb_pipetype (urb->pipe);
		u32	tdBE = le32_to_cpup (&td->hwBE);

  		cc = TD_CC_GET (tdINFO);

		/* control endpoints only have soft stalls */
  		if (type != PIPE_CONTROL && cc == TD_CC_STALL)
			usb_endpoint_halt (urb->dev,
				usb_pipeendpoint (urb->pipe),
				usb_pipeout (urb->pipe));

755 756 757
		/* update packet status if needed (short is normally ok) */
		if (cc == TD_DATAUNDERRUN
				&& !(urb->transfer_flags & URB_SHORT_NOT_OK))
David Brownell's avatar
David Brownell committed
758
			cc = TD_CC_NOERROR;
759
		if (cc != TD_CC_NOERROR && cc < 0x0E) {
David Brownell's avatar
David Brownell committed
760 761 762 763
			spin_lock (&urb->lock);
			if (urb->status == -EINPROGRESS)
				urb->status = cc_to_error [cc];
			spin_unlock (&urb->lock);
Linus Torvalds's avatar
Linus Torvalds committed
764
		}
David Brownell's avatar
David Brownell committed
765 766 767 768 769 770 771 772 773 774 775

		/* count all non-empty packets except control SETUP packet */
		if ((type != PIPE_CONTROL || td->index != 0) && tdBE != 0) {
			if (td->hwCBP == 0)
				urb->actual_length += tdBE - td->data_dma + 1;
			else
				urb->actual_length +=
					  le32_to_cpup (&td->hwCBP)
					- td->data_dma;
		}

776
		if (cc != TD_CC_NOERROR && cc < 0x0E)
777 778
			ohci_vdbg (ohci,
				"urb %p td %p (%d) cc %d, len=%d/%d\n",
David Brownell's avatar
David Brownell committed
779 780 781
				urb, td, 1 + td->index, cc,
				urb->actual_length,
				urb->transfer_buffer_length);
Linus Torvalds's avatar
Linus Torvalds committed
782 783 784 785 786
  	}
}

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

787 788 789 790 791 792 793 794 795 796 797 798 799
static inline struct td *
ed_halted (struct ohci_hcd *ohci, struct td *td, int cc, struct td *rev)
{
  	struct urb		*urb = td->urb;
	struct ed		*ed = td->ed;
	struct list_head	*tmp = td->td_list.next;
	u32			toggle = ed->hwHeadP & ED_C;

	/* clear ed halt; this is the td that caused it, but keep it inactive
	 * until its urb->complete() has a chance to clean up.
	 */
	ed->hwINFO |= ED_SKIP;
	wmb ();
800
	ed->hwHeadP &= ~ED_H; 
801

802 803 804 805
	/* put any later tds from this urb onto the donelist, after 'td',
	 * order won't matter here: no errors, and nothing was transferred.
	 * also patch the ed so it looks as if those tds completed normally.
	 */
806 807
	while (tmp != &ed->td_list) {
		struct td	*next;
808
		u32		info;
809 810 811 812

		next = list_entry (tmp, struct td, td_list);
		tmp = next->td_list.next;

813 814 815 816 817 818 819 820 821
		if (next->urb != urb)
			break;

		/* NOTE: if multi-td control DATA segments get supported,
		 * this urb had one of them, this td wasn't the last td
		 * in that segment (TD_R clear), this ed halted because
		 * of a short read, _and_ URB_SHORT_NOT_OK is clear ...
		 * then we need to leave the control STATUS packet queued
		 * and clear ED_SKIP.
822
		 */
823 824 825 826
		info = next->hwINFO;
		info |= cpu_to_le32 (TD_DONE);
		info &= ~cpu_to_le32 (TD_CC);
		next->hwINFO = info;
827

828 829
		next->next_dl_td = rev;	
		rev = next;
830

831 832
		ed->hwHeadP = next->hwNextTD | toggle;
	}
833

834 835 836 837
	/* help for troubleshooting:  report anything that
	 * looks odd ... that doesn't include protocol stalls
	 * (or maybe some other things)
	 */
838 839 840 841 842 843 844 845 846 847
	switch (cc) {
	case TD_DATAUNDERRUN:
		if ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)
			break;
		/* fallthrough */
	case TD_CC_STALL:
		if (usb_pipecontrol (urb->pipe))
			break;
		/* fallthrough */
	default:
848 849 850 851 852 853 854
		ohci_dbg (ohci,
			"urb %p path %s ep%d%s %08x cc %d --> status %d\n",
			urb, urb->dev->devpath,
			usb_pipeendpoint (urb->pipe),
			usb_pipein (urb->pipe) ? "in" : "out",
			le32_to_cpu (td->hwINFO),
			cc, cc_to_error [cc]);
855
	}
856 857 858 859

	return rev;
}

Linus Torvalds's avatar
Linus Torvalds committed
860 861 862 863 864
/* replies to the request have to be on a FIFO basis so
 * we unreverse the hc-reversed done-list
 */
static struct td *dl_reverse_done_list (struct ohci_hcd *ohci)
{
865
	u32		td_dma;
Linus Torvalds's avatar
Linus Torvalds committed
866
	struct td	*td_rev = NULL;
867
	struct td	*td = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
868 869 870 871
  	unsigned long	flags;

  	spin_lock_irqsave (&ohci->lock, flags);

872
	td_dma = le32_to_cpup (&ohci->hcca->done_head);
Linus Torvalds's avatar
Linus Torvalds committed
873
	ohci->hcca->done_head = 0;
874
	wmb();
Linus Torvalds's avatar
Linus Torvalds committed
875

876 877 878 879
	/* get TD from hc's singly linked list, and
	 * prepend to ours.  ed->td_list changes later.
	 */
	while (td_dma) {		
880 881
	    	int		cc;

882
		td = dma_to_td (ohci, td_dma);
David Brownell's avatar
David Brownell committed
883
		if (!td) {
884
			ohci_err (ohci, "bad entry %8x\n", td_dma);
David Brownell's avatar
David Brownell committed
885 886
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
887

888 889 890 891 892 893 894 895 896 897 898 899 900
		td->hwINFO |= cpu_to_le32 (TD_DONE);
		cc = TD_CC_GET (le32_to_cpup (&td->hwINFO));

		/* Non-iso endpoints can halt on error; un-halt,
		 * and dequeue any other TDs from this urb.
		 * No other TD could have caused the halt.
		 */
		if (cc != TD_CC_NOERROR && (td->ed->hwHeadP & ED_H))
			td_rev = ed_halted (ohci, td, cc, td_rev);

		td->next_dl_td = td_rev;	
		td_rev = td;
		td_dma = le32_to_cpup (&td->hwNextTD);
Linus Torvalds's avatar
Linus Torvalds committed
901 902
	}	
	spin_unlock_irqrestore (&ohci->lock, flags);
903
	return td_rev;
Linus Torvalds's avatar
Linus Torvalds committed
904 905 906 907
}

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

908 909 910 911
/* wrap-aware logic stolen from <linux/jiffies.h> */
#define tick_before(t1,t2) ((((s16)(t1))-((s16)(t2))) < 0)

/* there are some urbs/eds to unlink; called in_irq(), with HCD locked */
912 913
static void
finish_unlinks (struct ohci_hcd *ohci, u16 tick, struct pt_regs *regs)
Linus Torvalds's avatar
Linus Torvalds committed
914
{
915
	struct ed	*ed, **last;
Linus Torvalds's avatar
Linus Torvalds committed
916

917
rescan_all:
918
	for (last = &ohci->ed_rm_list, ed = *last; ed != NULL; ed = *last) {
919 920 921
		struct list_head	*entry, *tmp;
		int			completed, modified;
		u32			*prev;
Linus Torvalds's avatar
Linus Torvalds committed
922

923
		/* only take off EDs that the HC isn't using, accounting for
924
		 * frame counter wraps and EDs with partially retired TDs
925
		 */
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
		if (likely (HCD_IS_RUNNING(ohci->hcd.state))) {
			if (tick_before (tick, ed->tick)) {
skip_ed:
				last = &ed->ed_next;
				continue;
			}

			if (!list_empty (&ed->td_list)) {
				struct td	*td;
				u32		head;

				td = list_entry (ed->td_list.next, struct td,
							td_list);
				head = cpu_to_le32 (ed->hwHeadP) & TD_MASK;

				/* INTR_WDH may need to clean up first */
				if (td->td_dma != head)
					goto skip_ed;
			}
945
		}
Linus Torvalds's avatar
Linus Torvalds committed
946

947 948 949
		/* reentrancy:  if we drop the schedule lock, someone might
		 * have modified this list.  normally it's just prepending
		 * entries (which we'd ignore), but paranoia won't hurt.
950
		 */
951 952 953 954 955 956 957
		*last = ed->ed_next;
		ed->ed_next = 0;
		modified = 0;

		/* unlink urbs as requested, but rescan the list after
		 * we call a completion since it might have unlinked
		 * another (earlier) urb
958 959 960 961
		 *
		 * When we get here, the HC doesn't see this ed.  But it
		 * must not be rescheduled until all completed URBs have
		 * been given back to the driver.
962 963 964
		 */
rescan_this:
		completed = 0;
965 966 967 968 969 970 971 972 973 974 975
		prev = &ed->hwHeadP;
		list_for_each_safe (entry, tmp, &ed->td_list) {
			struct td	*td;
			struct urb	*urb;
			urb_priv_t	*urb_priv;
			u32		savebits;

			td = list_entry (entry, struct td, td_list);
			urb = td->urb;
			urb_priv = td->urb->hcpriv;

976
			if (urb->status == -EINPROGRESS) {
977 978 979 980
				prev = &td->hwNextTD;
				continue;
			}

981
			/* patch pointer hc uses */
982
			savebits = *prev & ~cpu_to_le32 (TD_MASK);
983 984 985
			*prev = td->hwNextTD | savebits;

			/* HC may have partly processed this TD */
986
			td_done (ohci, urb, td);
987
			urb_priv->td_cnt++;
988

989 990 991
			/* if URB is done, clean up */
			if (urb_priv->td_cnt == urb_priv->length) {
				modified = completed = 1;
David S. Miller's avatar
David S. Miller committed
992
				finish_urb (ohci, urb, regs);
Linus Torvalds's avatar
Linus Torvalds committed
993 994
			}
		}
995 996
		if (completed && !list_empty (&ed->td_list))
			goto rescan_this;
Linus Torvalds's avatar
Linus Torvalds committed
997

998 999
		/* ED's now officially unlinked, hc doesn't see */
		ed->state = ED_IDLE;
1000
		ed->hwHeadP &= ~ED_H;
1001
		ed->hwNextED = 0;
1002 1003
		wmb ();
		ed->hwINFO &= ~(ED_SKIP | ED_DEQUEUE);
Linus Torvalds's avatar
Linus Torvalds committed
1004

1005
		/* but if there's work queued, reschedule */
1006
		if (!list_empty (&ed->td_list)) {
1007
			if (HCD_IS_RUNNING(ohci->hcd.state))
1008
				ed_schedule (ohci, ed);
Linus Torvalds's avatar
Linus Torvalds committed
1009
		}
1010 1011 1012

		if (modified)
			goto rescan_all;
Linus Torvalds's avatar
Linus Torvalds committed
1013 1014 1015
   	}

	/* maybe reenable control and bulk lists */ 
1016
	if (HCD_IS_RUNNING(ohci->hcd.state) && !ohci->ed_rm_list) {
1017 1018 1019 1020 1021 1022 1023 1024
		u32	command = 0, control = 0;

		if (ohci->ed_controltail) {
			command |= OHCI_CLF;
			if (!(ohci->hc_control & OHCI_CTRL_CLE)) {
				control |= OHCI_CTRL_CLE;
				writel (0, &ohci->regs->ed_controlcurrent);
			}
Linus Torvalds's avatar
Linus Torvalds committed
1025
		}
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
		if (ohci->ed_bulktail) {
			command |= OHCI_BLF;
			if (!(ohci->hc_control & OHCI_CTRL_BLE)) {
				control |= OHCI_CTRL_BLE;
				writel (0, &ohci->regs->ed_bulkcurrent);
			}
		}
		
		/* CLE/BLE to enable, CLF/BLF to (maybe) kickstart */
		if (control) {
			ohci->hc_control |= control;
 			writel (ohci->hc_control, &ohci->regs->control);   
 		}
		if (command)
			writel (command, &ohci->regs->cmdstatus);   
 	}
Linus Torvalds's avatar
Linus Torvalds committed
1042 1043 1044 1045 1046 1047 1048
}



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

/*
David Brownell's avatar
David Brownell committed
1049 1050 1051
 * Process normal completions (error or success) and clean the schedules.
 *
 * This is the main path for handing urbs back to drivers.  The only other
1052
 * path is finish_unlinks(), which unlinks URBs using ed_rm_list, instead of
David Brownell's avatar
David Brownell committed
1053
 * scanning the (re-reversed) donelist as this does.
Linus Torvalds's avatar
Linus Torvalds committed
1054
 */
1055 1056
static void
dl_done_list (struct ohci_hcd *ohci, struct td *td, struct pt_regs *regs)
Linus Torvalds's avatar
Linus Torvalds committed
1057
{
David Brownell's avatar
David Brownell committed
1058
	unsigned long	flags;
Linus Torvalds's avatar
Linus Torvalds committed
1059

David Brownell's avatar
David Brownell committed
1060 1061 1062 1063 1064 1065 1066 1067
  	spin_lock_irqsave (&ohci->lock, flags);
  	while (td) {
		struct td	*td_next = td->next_dl_td;
		struct urb	*urb = td->urb;
		urb_priv_t	*urb_priv = urb->hcpriv;
		struct ed	*ed = td->ed;

		/* update URB's length and status from TD */
1068
   		td_done (ohci, urb, td);
David Brownell's avatar
David Brownell committed
1069 1070
  		urb_priv->td_cnt++;

1071
		/* If all this urb's TDs are done, call complete() */
1072
  		if (urb_priv->td_cnt == urb_priv->length)
David S. Miller's avatar
David S. Miller committed
1073
  			finish_urb (ohci, urb, regs);
Linus Torvalds's avatar
Linus Torvalds committed
1074

David Brownell's avatar
David Brownell committed
1075
		/* clean schedule:  unlink EDs that are no longer busy */
1076
		if (list_empty (&ed->td_list) && ed->state == ED_OPER)
1077
			start_ed_unlink (ohci, ed);
1078
		/* ... reenabling halted EDs only after fault cleanup */
1079
		else if ((ed->hwINFO & (ED_SKIP | ED_DEQUEUE)) == ED_SKIP) {
1080
			td = list_entry (ed->td_list.next, struct td, td_list);
1081
			if (!(td->hwINFO & TD_DONE)) {
1082
				ed->hwINFO &= ~ED_SKIP;
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
				/* ... hc may need waking-up */
				switch (ed->type) {
				case PIPE_CONTROL:
					writel (OHCI_CLF,
						&ohci->regs->cmdstatus);   
					break;
				case PIPE_BULK:
					writel (OHCI_BLF,
						&ohci->regs->cmdstatus);   
					break;
				}
			}
1095 1096
		}

David Brownell's avatar
David Brownell committed
1097
    		td = td_next;
Linus Torvalds's avatar
Linus Torvalds committed
1098
  	}  
David Brownell's avatar
David Brownell committed
1099
	spin_unlock_irqrestore (&ohci->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1100
}