usb-uhci-mem.c 19.5 KB
Newer Older
Georg Acher's avatar
Georg Acher committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*  
    UHCI HCD (Host Controller Driver) for USB
    UHCI memory allocation and basic descriptor handling
   
    (c) 1999-2002 
    Georg Acher      +    Deti Fliegl    +    Thomas Sailer
    georg@acher.org      deti@fliegl.de   sailer@ife.ee.ethz.ch
   
    with the help of
    David Brownell, david-b@pacbell.net
    Adam Richter, adam@yggdrasil.com
    Roman Weissgaerber, weissg@vienna.at    
    
    HW-initalization based on material of
    Randy Dunlap + Johannes Erdfelt + Gregory P. Smith + Linus Torvalds 

17
    $Id: usb-uhci-mem.c,v 1.3 2002/05/25 16:42:41 acher Exp $
Georg Acher's avatar
Georg Acher committed
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 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 348 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 378 379 380 381 382 383 384 385 386 387 388 389 390 391
 */

/*###########################################################################*/
//                        UHCI STRUCTURE
/*###########################################################################*/
static struct usb_hcd *uhci_hcd_alloc (void)
{
	struct uhci_hcd *uhci;
	int len;

	len=sizeof (struct uhci_hcd);
	uhci = (struct uhci_hcd *) kmalloc (len, GFP_KERNEL);
	if (uhci == 0) 
		return NULL;
	
	memset (uhci, 0, len);
	init_dbg("uhci @ %p, hcd @ %p",uhci, &(uhci->hcd));
	INIT_LIST_HEAD (&uhci->free_desc_qh);
	INIT_LIST_HEAD (&uhci->free_desc_td);
	INIT_LIST_HEAD (&uhci->urb_list);
	INIT_LIST_HEAD (&uhci->urb_unlinked);
	spin_lock_init (&uhci->urb_list_lock);
	spin_lock_init (&uhci->qh_lock);
	spin_lock_init (&uhci->td_lock);
	atomic_set(&uhci->avoid_bulk, 0);		
	return &(uhci->hcd);
}
/*-------------------------------------------------------------------*/
static void uhci_hcd_free (struct usb_hcd *hcd)
{
	kfree (hcd_to_uhci (hcd));
}
/*###########################################################################*/
//                   DMA/PCI CONSISTENCY
/*###########################################################################*/

static void uhci_urb_dma_sync(struct uhci_hcd *uhci, struct urb *urb, urb_priv_t *urb_priv)
{
	if (urb_priv->setup_packet_dma)
		pci_dma_sync_single(uhci->uhci_pci, urb_priv->setup_packet_dma,
				    sizeof(struct usb_ctrlrequest), PCI_DMA_TODEVICE);

	if (urb_priv->transfer_buffer_dma)
		pci_dma_sync_single(uhci->uhci_pci, urb_priv->transfer_buffer_dma,
				    urb->transfer_buffer_length,
				    usb_pipein(urb->pipe) ?
				    PCI_DMA_FROMDEVICE :
				    PCI_DMA_TODEVICE);
}
/*-------------------------------------------------------------------*/
static void uhci_urb_dma_unmap(struct uhci_hcd *uhci, struct urb *urb, urb_priv_t *urb_priv)
{
	if (urb_priv->setup_packet_dma) {
		pci_unmap_single(uhci->uhci_pci, urb_priv->setup_packet_dma,
				 sizeof(struct usb_ctrlrequest), PCI_DMA_TODEVICE);
		urb_priv->setup_packet_dma = 0;
	}
	if (urb_priv->transfer_buffer_dma) {
		pci_unmap_single(uhci->uhci_pci, urb_priv->transfer_buffer_dma,
				 urb->transfer_buffer_length,
				 usb_pipein(urb->pipe) ?
				 PCI_DMA_FROMDEVICE :
				 PCI_DMA_TODEVICE);
		urb_priv->transfer_buffer_dma = 0;
	}
}
/*###########################################################################*/
//                       TRANSFER DESCRIPTORS (TD)
/*###########################################################################*/

static void fill_td (uhci_desc_t *td, int status, int info, __u32 buffer)
{
	td->hw.td.status = cpu_to_le32(status);
	td->hw.td.info = cpu_to_le32(info);
	td->hw.td.buffer = cpu_to_le32(buffer);
}
/*-------------------------------------------------------------------*/
static int alloc_td (struct uhci_hcd *uhci, uhci_desc_t ** new, int flags)
{
	dma_addr_t dma_handle;

	*new = pci_pool_alloc(uhci->desc_pool, GFP_DMA | GFP_ATOMIC, &dma_handle);
	if (!*new)
		return -ENOMEM;
	memset (*new, 0, sizeof (uhci_desc_t));
	(*new)->dma_addr = dma_handle;
	set_td_link((*new), UHCI_PTR_TERM | (flags & UHCI_PTR_BITS));	// last by default
	(*new)->type = TD_TYPE;
	mb();
	INIT_LIST_HEAD (&(*new)->vertical);
	INIT_LIST_HEAD (&(*new)->horizontal);
	
	return 0;
}
/*-------------------------------------------------------------------*/
/* insert td at last position in td-list of qh (vertical) */
static int insert_td (struct uhci_hcd *uhci, uhci_desc_t *qh, uhci_desc_t* new, int flags)
{
	uhci_desc_t *prev;
	unsigned long cpuflags;
	
	spin_lock_irqsave (&uhci->td_lock, cpuflags);

	list_add_tail (&new->vertical, &qh->vertical);

	prev = list_entry (new->vertical.prev, uhci_desc_t, vertical);

	if (qh == prev ) {
		// virgin qh without any tds
		set_qh_element(qh, new->dma_addr | UHCI_PTR_TERM);
	}
	else {
		// already tds inserted, implicitely remove TERM bit of prev
		set_td_link(prev, new->dma_addr | (flags & UHCI_PTR_DEPTH));
	}
	mb();
	spin_unlock_irqrestore (&uhci->td_lock, cpuflags);
	
	return 0;
}
/*-------------------------------------------------------------------*/
/* insert new_td after td (horizontal) */
static int insert_td_horizontal (struct uhci_hcd *uhci, uhci_desc_t *td, uhci_desc_t* new)
{
	uhci_desc_t *next;
	unsigned long flags;
	
	spin_lock_irqsave (&uhci->td_lock, flags);

	next = list_entry (td->horizontal.next, uhci_desc_t, horizontal);
	list_add (&new->horizontal, &td->horizontal);
	new->hw.td.link = td->hw.td.link;
	mb();
	set_td_link(td, new->dma_addr);
	mb();
	spin_unlock_irqrestore (&uhci->td_lock, flags);	
	
	return 0;
}
/*-------------------------------------------------------------------*/
static int unlink_td (struct uhci_hcd *uhci, uhci_desc_t *element, int phys_unlink)
{
	uhci_desc_t *next, *prev;
	int dir = 0;
	unsigned long flags;
	
	spin_lock_irqsave (&uhci->td_lock, flags);
	
	next = list_entry (element->vertical.next, uhci_desc_t, vertical);
	
	if (next == element) {
		dir = 1;
		prev = list_entry (element->horizontal.prev, uhci_desc_t, horizontal);
	}
	else 
		prev = list_entry (element->vertical.prev, uhci_desc_t, vertical);
	
	if (phys_unlink) {
		// really remove HW linking
		if (prev->type == TD_TYPE) {
			prev->hw.td.link = element->hw.td.link;
		}
		else
			prev->hw.qh.element = element->hw.td.link;
	}
 	mb ();

	if (dir == 0)
		list_del (&element->vertical);
	else
		list_del (&element->horizontal);
	
	spin_unlock_irqrestore (&uhci->td_lock, flags);	
	
	return 0;
}
/*###########################################################################*/
//               QUEUE HEADS (QH)
/*###########################################################################*/
// Allocates qh element
static int alloc_qh (struct uhci_hcd *uhci, uhci_desc_t ** new)
{
	dma_addr_t dma_handle;

	*new = pci_pool_alloc(uhci->desc_pool, GFP_DMA | GFP_ATOMIC, &dma_handle);
	if (!*new)
		return -ENOMEM;
	memset (*new, 0, sizeof (uhci_desc_t));
	(*new)->dma_addr = dma_handle;
	set_qh_head(*new, UHCI_PTR_TERM);
	set_qh_element(*new, UHCI_PTR_TERM);
	(*new)->type = QH_TYPE;
	
	mb();
	INIT_LIST_HEAD (&(*new)->horizontal);
	INIT_LIST_HEAD (&(*new)->vertical);
	
	dbg("Allocated qh @ %p", *new);
	return 0;
}
/*-------------------------------------------------------------------*/
// inserts new qh before/after the qh at pos
// flags: 0: insert before pos, 1: insert after pos (for low speed transfers)
static int insert_qh (struct uhci_hcd *uhci, uhci_desc_t *pos, uhci_desc_t *new, int order)
{
	uhci_desc_t *old;
	unsigned long flags;

	spin_lock_irqsave (&uhci->qh_lock, flags);

	if (!order) {
		// (OLD) (POS) -> (OLD) (NEW) (POS)
		old = list_entry (pos->horizontal.prev, uhci_desc_t, horizontal);
		list_add_tail (&new->horizontal, &pos->horizontal);
		set_qh_head(new, MAKE_QH_ADDR (pos)) ;
		mb();
		if (!(old->hw.qh.head & cpu_to_le32(UHCI_PTR_TERM)))
			set_qh_head(old, MAKE_QH_ADDR (new)) ;
	}
	else {
		// (POS) (OLD) -> (POS) (NEW) (OLD)
		old = list_entry (pos->horizontal.next, uhci_desc_t, horizontal);
		list_add (&new->horizontal, &pos->horizontal);
		set_qh_head(new, MAKE_QH_ADDR (old));
		mb();
		set_qh_head(pos, MAKE_QH_ADDR (new)) ;
	}
 	mb ();
	
	spin_unlock_irqrestore (&uhci->qh_lock, flags);

	return 0;
}
/*-------------------------------------------------------------------*/
// append a qh to td.link physically, the SW linkage is not affected
static void append_qh(struct uhci_hcd *uhci, uhci_desc_t *td, uhci_desc_t* qh, int  flags)
{
	unsigned long cpuflags;
	
	spin_lock_irqsave (&uhci->td_lock, cpuflags);

	set_td_link(td, qh->dma_addr | (flags & UHCI_PTR_DEPTH) | UHCI_PTR_QH);
       
	mb();
	spin_unlock_irqrestore (&uhci->td_lock, cpuflags);
}
/*-------------------------------------------------------------------*/
static int unlink_qh (struct uhci_hcd *uhci, uhci_desc_t *element)
{
	uhci_desc_t  *prev;
	unsigned long flags;
	__u32 old_head;
	spin_lock_irqsave (&uhci->qh_lock, flags);
	
	prev = list_entry (element->horizontal.prev, uhci_desc_t, horizontal);

	old_head = element->hw.qh.head;
	element->hw.qh.head = UHCI_PTR_TERM;
	mb();
	prev->hw.qh.head = old_head;

	dbg("unlink qh %p, pqh %p, nxqh %p, to %08x", element, prev, 
	    list_entry (element->horizontal.next, uhci_desc_t, horizontal),
	    le32_to_cpu(element->hw.qh.head) &~15);
	
	list_del(&element->horizontal);

	mb ();
	spin_unlock_irqrestore (&uhci->qh_lock, flags);
	
	return 0;
}
/*-------------------------------------------------------------------*/
static int delete_desc (struct uhci_hcd *uhci, uhci_desc_t *element)
{
	pci_pool_free(uhci->desc_pool, element, element->dma_addr);
	return 0;
}
/*-------------------------------------------------------------------*/
static int delete_qh (struct uhci_hcd *uhci, uhci_desc_t *qh)
{
	uhci_desc_t *td;
	struct list_head *p;
	int n=0;

	list_del (&qh->horizontal);

	while ((p = qh->vertical.next) != &qh->vertical && n<10000) {
		td = list_entry (p, uhci_desc_t, vertical);
		dbg("unlink td @ %p",td);
		unlink_td (uhci, td, 0); // no physical unlink
		delete_desc (uhci, td);
		n++;
	}
	// never trust any software, not even your own...
	if (n>=10000)
		err("delete_qh: Garbage in QH list, giving up");

	delete_desc (uhci, qh);
	
	return 0;
}
/*###########################################################################*/
//             DESCRIPTOR CHAINING HELPERS
/*###########################################################################*/

static void clean_td_chain (struct uhci_hcd *uhci, uhci_desc_t *td)
{
	struct list_head *p;
	uhci_desc_t *td1;

	if (!td)
		return;
	
	while ((p = td->horizontal.next) != &td->horizontal) {
		td1 = list_entry (p, uhci_desc_t, horizontal);
		delete_desc (uhci, td1);
	}
	
	delete_desc (uhci, td);
}
/*-------------------------------------------------------------------*/
// Cleans up collected QHs/TDs, but not more than 100 in one go
void clean_descs(struct uhci_hcd *uhci, int force)
{
	struct list_head *q;
	uhci_desc_t *qh,*td;
	int now=UHCI_GET_CURRENT_FRAME(uhci), n=0;

	q=uhci->free_desc_qh.prev;
	
	while (q != &uhci->free_desc_qh && (force || n<100)) {
		qh = list_entry (q, uhci_desc_t, horizontal);		
		q=qh->horizontal.prev;

		if ((qh->last_used!=now) || force) {
			delete_qh(uhci,qh);
		}
		n++;
	}

	q=uhci->free_desc_td.prev;
	n=0;

	while (q != &uhci->free_desc_td && (force || n<100)) {

		td = list_entry (q, uhci_desc_t, horizontal);		
		q=td->horizontal.prev;

		if (((td->last_used!=now)&&(td->last_used+1!=now)) || force) {
			list_del (&td->horizontal);
			delete_desc(uhci,td);
		}
		n++;
	}
}
/*-------------------------------------------------------------------*/
static void uhci_switch_timer_int(struct uhci_hcd *uhci)
{

	if (!list_empty(&uhci->urb_unlinked))
		set_td_ioc(uhci->td1ms);
	else
		clr_td_ioc(uhci->td1ms);

	if (uhci->timeout_urbs)
		set_td_ioc(uhci->td32ms);
	else
		clr_td_ioc(uhci->td32ms);
	wmb();
}
/*-------------------------------------------------------------------*/
static void enable_desc_loop(struct uhci_hcd *uhci, struct urb *urb)
{
392
	unsigned long flags;
Georg Acher's avatar
Georg Acher committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406

	if (urb->transfer_flags & USB_NO_FSBR)
		return;

	spin_lock_irqsave (&uhci->qh_lock, flags);
	uhci->chain_end->hw.qh.head&=cpu_to_le32(~UHCI_PTR_TERM);
	mb();
	uhci->loop_usage++;
	((urb_priv_t*)urb->hcpriv)->use_loop=1;
	spin_unlock_irqrestore (&uhci->qh_lock, flags);
}
/*-------------------------------------------------------------------*/
static void disable_desc_loop(struct uhci_hcd *uhci, struct urb *urb)
{
407
	unsigned long flags;
Georg Acher's avatar
Georg Acher committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

	if (urb->transfer_flags & USB_NO_FSBR)
		return;

	spin_lock_irqsave (&uhci->qh_lock, flags);
	if (((urb_priv_t*)urb->hcpriv)->use_loop) {
		uhci->loop_usage--;

		if (!uhci->loop_usage) {
			uhci->chain_end->hw.qh.head|=cpu_to_le32(UHCI_PTR_TERM);
			mb();
		}
		((urb_priv_t*)urb->hcpriv)->use_loop=0;
	}
	spin_unlock_irqrestore (&uhci->qh_lock, flags);
}
/*-------------------------------------------------------------------*/
static void queue_urb_unlocked (struct uhci_hcd *uhci, struct urb *urb)
{
	urb_priv_t *priv=(urb_priv_t*)urb->hcpriv;
	int type;
	type=usb_pipetype (urb->pipe);
	
431
	if (high_bw && ((type == PIPE_BULK) || (type == PIPE_CONTROL)))
Georg Acher's avatar
Georg Acher committed
432
			enable_desc_loop(uhci, urb);
433

Georg Acher's avatar
Georg Acher committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
	urb->status = -EINPROGRESS;
	priv->started=jiffies;

	list_add (&priv->urb_list, &uhci->urb_list);
	if (urb->timeout)
		uhci->timeout_urbs++;
	uhci_switch_timer_int(uhci);
}
/*-------------------------------------------------------------------*/
static void queue_urb (struct uhci_hcd *uhci, struct urb *urb)
{
	unsigned long flags=0;

	spin_lock_irqsave (&uhci->urb_list_lock, flags);
	queue_urb_unlocked(uhci,urb);
	spin_unlock_irqrestore (&uhci->urb_list_lock, flags);
}
/*-------------------------------------------------------------------*/
static void dequeue_urb (struct uhci_hcd *uhci, struct urb *urb)
{
	urb_priv_t *priv=(urb_priv_t*)urb->hcpriv;
	int type;
	dbg("dequeue URB %p",urb);
	type=usb_pipetype (urb->pipe);

459
	if (high_bw && ((type == PIPE_BULK) || (type == PIPE_CONTROL)))
Georg Acher's avatar
Georg Acher committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
		disable_desc_loop(uhci, urb);

	list_del (&priv->urb_list);
	if (urb->timeout && uhci->timeout_urbs)
		uhci->timeout_urbs--;

}
/*###########################################################################*/
//                  INIT/FREE FRAME LAYOUT IN MEMORY
/*###########################################################################*/

// Removes ALL qhs in chain (paranoia!)
static void cleanup_skel (struct uhci_hcd *uhci)
{
	unsigned int n;
	uhci_desc_t *td;

	dbg("cleanup_skel");

	clean_descs(uhci,1);
	dbg("clean_descs done");

	if (uhci->td32ms) {	
		unlink_td(uhci,uhci->td32ms,1);
		delete_desc(uhci, uhci->td32ms);
	}

	if (uhci->td128ms) {	
		unlink_td(uhci,uhci->td128ms,1);
		delete_desc(uhci, uhci->td128ms);
	}

	for (n = 0; n < 8; n++) {
		td = uhci->int_chain[n];
		clean_td_chain (uhci, td);
	}

	if (uhci->iso_td) {
		for (n = 0; n < 1024; n++) {
			td = uhci->iso_td[n];
			clean_td_chain (uhci, td);
		}
		kfree (uhci->iso_td);
	}

	if (uhci->framelist)
		pci_free_consistent(uhci->uhci_pci, PAGE_SIZE,
				    uhci->framelist, uhci->framelist_dma);

	if (uhci->control_chain) {
		// completed init_skel?
		struct list_head *p;
		uhci_desc_t *qh, *qh1;

		qh = uhci->control_chain;
		while ((p = qh->horizontal.next) != &qh->horizontal) {
			qh1 = list_entry (p, uhci_desc_t, horizontal);
			delete_qh (uhci, qh1);
		}

		delete_qh (uhci, qh);
	}
	else {
		if (uhci->ls_control_chain)
			delete_desc (uhci, uhci->ls_control_chain);
		if (uhci->control_chain)
			delete_desc (uhci, uhci->control_chain);
		if (uhci->bulk_chain)
			delete_desc (uhci, uhci->bulk_chain);
		if (uhci->chain_end)
			delete_desc (uhci, uhci->chain_end);
	}

	if (uhci->desc_pool) {
		pci_pool_destroy(uhci->desc_pool);
		uhci->desc_pool = NULL;
	}

	uhci->ls_control_chain = NULL;
	uhci->control_chain = NULL;
	uhci->bulk_chain = NULL;
	uhci->chain_end = NULL;
	for (n = 0; n < 8; n++)
		uhci->int_chain[n] = NULL;
	dbg("cleanup_skel finished");	
}
/*-------------------------------------------------------------------*/
// allocates framelist and qh-skeletons
// only HW-links provide continous linking, SW-links stay in their domain (ISO/INT)
static int init_skel (struct uhci_hcd *uhci)
{
	int n, ret;
	uhci_desc_t *qh, *td;
	
	init_dbg("init_skel");
	
	uhci->framelist = pci_alloc_consistent(uhci->uhci_pci, PAGE_SIZE,
					    &uhci->framelist_dma);

	if (!uhci->framelist)
		return -ENOMEM;

	memset (uhci->framelist, 0, 4096);

	init_dbg("creating descriptor pci_pool");
	uhci->desc_pool = pci_pool_create("uhci_desc", uhci->uhci_pci,
				       sizeof(uhci_desc_t), 16, 0,
				       GFP_DMA | GFP_ATOMIC);	
	if (!uhci->desc_pool)
		goto init_skel_cleanup;

	init_dbg("allocating iso desc pointer list");
	uhci->iso_td = (uhci_desc_t **) kmalloc (1024 * sizeof (uhci_desc_t*), GFP_KERNEL);
	
	if (!uhci->iso_td)
		goto init_skel_cleanup;

	uhci->ls_control_chain = NULL;
	uhci->control_chain = NULL;
	uhci->bulk_chain = NULL;
	uhci->chain_end = NULL;
	for (n = 0; n < 8; n++)
		uhci->int_chain[n] = NULL;

	init_dbg("allocating iso descs");
	for (n = 0; n < 1024; n++) {
	 	// allocate skeleton iso/irq-tds
		if (alloc_td (uhci, &td, 0))
			goto init_skel_cleanup;

		uhci->iso_td[n] = td;
		uhci->framelist[n] = cpu_to_le32((__u32) td->dma_addr);
	}

	init_dbg("allocating qh: chain_end");
	if (alloc_qh (uhci, &qh))	
		goto init_skel_cleanup;
				
	uhci->chain_end = qh;

	if (alloc_td (uhci, &td, 0))
		goto init_skel_cleanup;
	
	fill_td (td, 0 * TD_CTRL_IOC, 0, 0); // generate 1ms interrupt (enabled on demand)
	insert_td (uhci, qh, td, 0);
	qh->hw.qh.element &= cpu_to_le32(~UHCI_PTR_TERM); // remove TERM bit
	uhci->td1ms=td;

	dbg("allocating qh: bulk_chain");
	if (alloc_qh (uhci, &qh))
		goto init_skel_cleanup;
	
	insert_qh (uhci, uhci->chain_end, qh, 0);
	uhci->bulk_chain = qh;

	dbg("allocating qh: control_chain");
	if ((ret = alloc_qh (uhci, &qh)))
		goto init_skel_cleanup;
	
	insert_qh (uhci, uhci->bulk_chain, qh, 0);
	uhci->control_chain = qh;

	// disabled reclamation loop
623 624 625
	if (high_bw)
		set_qh_head(uhci->chain_end, uhci->control_chain->dma_addr | UHCI_PTR_QH | UHCI_PTR_TERM);

Georg Acher's avatar
Georg Acher committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717

	init_dbg("allocating qh: ls_control_chain");
	if (alloc_qh (uhci, &qh))
		goto init_skel_cleanup;
	
	insert_qh (uhci, uhci->control_chain, qh, 0);
	uhci->ls_control_chain = qh;

	init_dbg("allocating skeleton INT-TDs");
	
	for (n = 0; n < 8; n++) {
		uhci_desc_t *td;

		if (alloc_td (uhci, &td, 0))
			goto init_skel_cleanup;

		uhci->int_chain[n] = td;
		if (n == 0) {
			set_td_link(uhci->int_chain[0], uhci->ls_control_chain->dma_addr | UHCI_PTR_QH);
		}
		else {
			set_td_link(uhci->int_chain[n], uhci->int_chain[0]->dma_addr);
		}
	}

	init_dbg("Linking skeleton INT-TDs");
	
	for (n = 0; n < 1024; n++) {
		// link all iso-tds to the interrupt chains
		int m, o;
		dbg("framelist[%i]=%x",n,le32_to_cpu(uhci->framelist[n]));
		if ((n&127)==127) 
			((uhci_desc_t*) uhci->iso_td[n])->hw.td.link = cpu_to_le32(uhci->int_chain[0]->dma_addr);
		else 
			for (o = 1, m = 2; m <= 128; o++, m += m)
				if ((n & (m - 1)) == ((m - 1) / 2))
					set_td_link(((uhci_desc_t*) uhci->iso_td[n]), uhci->int_chain[o]->dma_addr);
	}

	if (alloc_td (uhci, &td, 0))
		goto init_skel_cleanup;
	
	fill_td (td, 0 * TD_CTRL_IOC, 0, 0); // generate 32ms interrupt (activated later)
	uhci->td32ms=td;
	insert_td_horizontal (uhci, uhci->int_chain[5], td);

	if (alloc_td (uhci, &td, 0))
		goto init_skel_cleanup;
	
	fill_td (td, 0 * TD_CTRL_IOC, 0, 0); // generate 128ms interrupt (activated later)
	uhci->td128ms=td;
	insert_td_horizontal (uhci, uhci->int_chain[7], td);

	mb();
	init_dbg("init_skel exit");
	return 0;

  init_skel_cleanup:
	cleanup_skel (uhci);
	return -ENOMEM;
}
/*###########################################################################*/
//                 UHCI PRIVATE DATA
/*###########################################################################*/

urb_priv_t *uhci_alloc_priv(int mem_flags)
{
	urb_priv_t *p;
#ifdef DEBUG_SLAB
	p = kmem_cache_alloc(urb_priv_kmem, SLAB_FLAG);
#else
	p = kmalloc (sizeof (urb_priv_t), mem_flags);
#endif
	if (p) {
		memset(p, 0, sizeof(urb_priv_t));
		INIT_LIST_HEAD (&p->urb_list);
	}
	return p;
}
/*-------------------------------------------------------------------*/
void uhci_free_priv(struct uhci_hcd *uhci, struct urb *urb, urb_priv_t* p)
{
		uhci_urb_dma_unmap(uhci, urb, p);
#ifdef DEBUG_SLAB
		err("free_priv %p",p);
		kmem_cache_free(urb_priv_kmem, p);
#else
		kfree (p);
#endif
		urb->hcpriv = NULL;
}