host_mnd.c 22.1 KB
1 2 3 4 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 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 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 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 459 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 623 624 625 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
/*HEADER******************************************************************
**************************************************************************
*** 
*** Copyright (c) 2001-2002 ARC International.
*** All rights reserved                                          
***                                                              
*** This software embodies materials and concepts which are      
*** confidential to ARC International and is made
*** available solely pursuant to the terms of a written license   
*** agreement with ARC International             
***
*** File: hostapi.c
***
*** Comments:      
***   This file contains the USB Host API specific functions.
***                                                               
**************************************************************************
*END*********************************************************************/

#include "os_bb.h"
#include "osint.h"
#include "../host.h"

USB_HOST_STATE_STRUCT _usb_host_state[OS_USB_MAX_CONTROLLERS];
PIPE_DESCRIPTOR_STRUCT _usb_host_state_pipes[OS_USB_MAX_CONTROLLERS][OS_USB_MAX_PIPES];

/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : _usb_host_init
*  Returned Value : error or USB_OK
*  Comments       :
*        Initializes the USB hardware and installs the USB 
*  interrupt handler
*END*-----------------------------------------------------------------*/

uint_8 _usb_host_init
   (
      /* [IN] USB controller number */
      uint_8                  controller_number,
      
      /* [IN] device number */
      uint_8                  device_number,
      
      /* [IN] maximum number of Pipes */
      uint_8                  max_pipe_num,
      
      /* [OUT] the USB host handle */
      _usb_host_handle _PTR_  handlep
   )
{
   USB_HOST_STATE_STRUCT_PTR usb_host_state_struct_ptr;

   /* Initialize the USB interface. */   

   if (controller_number >= OS_USB_MAX_CONTROLLERS) {
      return USBERR_INVALID_DEVICE_NUM;
   }
   if (device_number >= OS_USB_MAX_DEVICES) {
      return USBERR_INVALID_DEVICE_NUM;
   }

   usb_host_state_struct_ptr = &_usb_host_state[controller_number];
   
   bzero(usb_host_state_struct_ptr, sizeof(USB_HOST_STATE_STRUCT));
   
   usb_host_state_struct_ptr->MAX_PIPES = max_pipe_num;
   usb_host_state_struct_ptr->CTLR_NUM = controller_number;
   usb_host_state_struct_ptr->DEV_NUM = device_number;
   
   /* Allocate the USB Host Pipe Descriptors */   
   usb_host_state_struct_ptr->PIPE_DESCRIPTOR_BASE_PTR = 
	   &_usb_host_state_pipes[controller_number][0];
   
   bzero(usb_host_state_struct_ptr->PIPE_DESCRIPTOR_BASE_PTR, 
      ((sizeof(PIPE_DESCRIPTOR_STRUCT)) *  max_pipe_num));

   /* Initialize the pipe queue heads and tails to -1 - indicating that 
   ** the queue is empty 
   */
   usb_host_state_struct_ptr->CURRENT_ISO_HEAD = -1;
   usb_host_state_struct_ptr->CURRENT_ISO_TAIL = -1;
   usb_host_state_struct_ptr->CURRENT_INTR_HEAD = -1;
   usb_host_state_struct_ptr->CURRENT_INTR_TAIL = -1;
   usb_host_state_struct_ptr->CURRENT_CTRL_HEAD = -1;
   usb_host_state_struct_ptr->CURRENT_CTRL_TAIL = -1;
   usb_host_state_struct_ptr->CURRENT_BULK_HEAD = -1;
   usb_host_state_struct_ptr->CURRENT_BULK_TAIL = -1;

   /*
   ** Initialize the hardware
   */
   *handlep = usb_host_state_struct_ptr;
   _usb_hci_vusb11_init(*handlep);
   
   return USB_OK;

}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_host_register_service
* Returned Value : USB_OK or error code
* Comments       :
*     Registers a callback routine for a specified event or pipe.
* 
*END*--------------------------------------------------------------------*/
uint_8 _usb_host_register_service
   (
      /* [IN] Handle to the USB device */
      _usb_host_handle         handle,
      
      /* [IN] type of event or endpoint number to service */
      uint_8                   type,
      
      /* [IN] Pointer to the service's callback function */
      void(_CODE_PTR_ service)(pointer, uint_32)
   )
{ /* Body */
   USB_HOST_STATE_STRUCT_PTR           usb_host_ptr;
   USB_SERVICE_STRUCT_PTR              service_ptr;
   USB_SERVICE_STRUCT_PTR _PTR_        search_ptr;
   
   usb_host_ptr = (USB_HOST_STATE_STRUCT_PTR)handle;
   
   /* Needs mutual exclusion */
   USB_lock();
   
   /* Search for an existing entry for type */
   for (search_ptr = &usb_host_ptr->SERVICE_HEAD_PTR;
      *search_ptr;
      search_ptr = &(*search_ptr)->NEXT) 
   {
      if ((*search_ptr)->TYPE == type) {
         /* Found an existing entry */
         USB_unlock();
         return USBERR_OPEN_SERVICE;
      } /* Endif */
   } /* Endfor */
   
   /* No existing entry found - create a new one */
   service_ptr = (USB_SERVICE_STRUCT_PTR)osMalloc(__usb_svc_callback_reg);
   
   if (!service_ptr) {
      USB_unlock();
      return USBERR_ALLOC_SERVICE;
   } /* Endif */
   
   service_ptr->TYPE = type;
   service_ptr->SERVICE = service;
   service_ptr->NEXT = NULL;
   *search_ptr = service_ptr;
   
   USB_unlock();
   
   return USB_OK;
} /* EndBody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_host_call_service
* Returned Value : USB_OK or error code
* Comments       :
*     Calls the appropriate service for the specified type, if one is
*     registered.
* 
*END*--------------------------------------------------------------------*/
uint_8 _usb_host_call_service
   (
      /* [IN] Handle to the USB device */
      _usb_host_handle handle,

      /* [IN] Type of service or endpoint */
      uint_8           type,
      
      /* [IN] Number of bytes in transmission */
      uint_32          length
   )
{
   USB_HOST_STATE_STRUCT_PTR           usb_host_ptr;
   USB_SERVICE_STRUCT_PTR              service_ptr;
   
   usb_host_ptr = (USB_HOST_STATE_STRUCT_PTR)handle;
   /* Needs mutual exclusion */
   USB_lock();
   
   /* Search for an existing entry for type */
   for (service_ptr = usb_host_ptr->SERVICE_HEAD_PTR;
      service_ptr;
      service_ptr = service_ptr->NEXT) 
   {
      if (service_ptr->TYPE == type) {
         service_ptr->SERVICE(handle, length);
         USB_unlock();
         return USB_OK;
      }
      
   }

   USB_unlock();
   return USBERR_CLOSED_SERVICE;
}

/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : _usb_host_open_pipe
*  Returned Value : Pipe ID
*  Comments       :
*  _usb_host_open_pipe routine initializes a new pipe descriptor and 
*  returns the pipe ID based on the USB bus address, endpoint number, 
*  direction of transfer, maximum packet size, interval and the type of pipe
*
*END*-----------------------------------------------------------------*/
int_16 _usb_host_open_pipe
   (
      /* [IN] the USB Host state structure */
      _usb_host_handle     handle,
      
      /* USB Address */
      uint_8               address,

      /* Endpoint number */
      uint_8               ep_num,

      /* Direction - Rx or Tx */
      uint_8               direction,

      /* Interval for transfer */
      uint_8               interval,

      /* Pipe type - Ctrl, Iso, Int, Bulk */
      uint_8               pipe_type,

      /* Max packet size */
      uint_16              max_pkt_size,
      
      /* NAK counter */
      uint_8               nak_count,

      /* [IN] After all data is transferred, should we terminate the transfer 
      ** with a zero length packet if the last packet size == MAX_PACKET_SIZE? 
      */
      uint_8               flag
   )
{
   int_16 pipeid = -1, i;
   PIPE_DESCRIPTOR_STRUCT_PTR pipe_descr_ptr;
   USB_HOST_STATE_STRUCT_PTR usb_host_ptr;
   
   usb_host_ptr = (USB_HOST_STATE_STRUCT_PTR)handle;

   USB_lock();
   
   for (i=0; i<usb_host_ptr->MAX_PIPES; i++) {
      /* Check if a pipe is not open */
      if (!(usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR[i].OPEN)) {
         break;
      }
   }

   USB_unlock();

   if (i != usb_host_ptr->MAX_PIPES) {
      pipeid = i;
      pipe_descr_ptr = 
         &usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR[pipeid];
      pipe_descr_ptr->ADDRESS = address;
      pipe_descr_ptr->EP = ep_num;
      pipe_descr_ptr->DIRECTION = direction;
      pipe_descr_ptr->INTERVAL = 
         pipe_descr_ptr->CURRENT_INTERVAL = interval;
      pipe_descr_ptr->NAK_COUNT = 
         pipe_descr_ptr->CURRENT_NAK_COUNT = nak_count;
      pipe_descr_ptr->PIPETYPE = pipe_type;
      pipe_descr_ptr->MAX_PKT_SIZE = max_pkt_size;
      pipe_descr_ptr->OPEN = TRUE;
      pipe_descr_ptr->PIPE_ID = pipeid;
      pipe_descr_ptr->STATUS = USB_STATUS_IDLE;
	  pipe_descr_ptr->DONT_ZERO_TERMINATE = flag;
      return pipeid;
   }
   
   return USBERR_OPEN_PIPE_FAILED;

}

/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : _usb_host_send_setup
*  Returned Value : None
*  Comments       :
*  Sends a Setup packet. Internally, following the SendSetup call
*  this takes care of the whole transaction (including receiving or sending 
*  any data to or from the device.
*
*END*-----------------------------------------------------------------*/
uint_8 _usb_host_send_setup
   (
      /* [IN] the USB Host state structure */
      _usb_host_handle        handle,
      
      /* Index into the Pipe Descriptor Array */
      int_16                  pipeid,
      
      /* Tx buffer for first phase */
      uchar_ptr               tx1_ptr, 
            
      /* Tx buffer for second phase */
      uchar_ptr               tx2_ptr,
      
      /* Rx buffer */
      uchar_ptr               rx_ptr, 
      
      /* Length of the packet for first phase */
      uint_32                 rx_length,
            
      /* Length of the packet for second phase */
      uint_32                 length2
   )
{
   PIPE_DESCRIPTOR_STRUCT_PTR pipe_descr_ptr;
   USB_HOST_STATE_STRUCT_PTR usb_host_ptr;
   
   usb_host_ptr = (USB_HOST_STATE_STRUCT_PTR)handle;

PRINTF("host_send_setup: pipe %d, tx1 0x%x, tx2 0x%x, rlen %d, len %d\n",
		pipeid, tx1_ptr, tx2_ptr, rx_length, length2);

   /* Check if the pipe id is valid */
   if (pipeid < usb_host_ptr->MAX_PIPES) {
      pipe_descr_ptr = &usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR[pipeid];
   } else {
PRINTF("host_send_setup: bad pipe\n");
      return USBERR_INVALID_PIPE_ID;
   }

   USB_lock();

   /* Check if a previously queued transfer is still in progress for this 
   ** pipe 
   */
   if (pipe_descr_ptr->STATUS != USB_STATUS_IDLE) {
      USB_unlock();
PRINTF("host_send_setup: already in progress\n");
      return USB_STATUS_TRANSFER_IN_PROGRESS;
   } else {
      USB_unlock();
      /* Initialize the pipe descriptor for the current transfer request */
      pipe_descr_ptr->SOFAR = 0;
      pipe_descr_ptr->NEXTDATA01 = 0;       /* reset DATA0/1 */
      pipe_descr_ptr->RX_PTR = rx_ptr;
      pipe_descr_ptr->TX1_PTR = tx1_ptr;
      pipe_descr_ptr->TX2_PTR = tx2_ptr;
      pipe_descr_ptr->TODO1 = rx_length;
      pipe_descr_ptr->TODO2 = length2;

      pipe_descr_ptr->SEND_PHASE = FALSE;
      pipe_descr_ptr->FIRST_PHASE = TRUE;
      osWritebackDCache((void *)tx1_ptr, 8);
      if (tx2_ptr)
      	 osWritebackDCache((void *)tx2_ptr, length2);
      if (rx_ptr)
         osInvalDCache((void *)rx_ptr, (s32)rx_length);

      /* Check if this setup packet will have a send data phase */      
      if (!((((INTERNAL_USB_SETUP_PTR)pipe_descr_ptr->TX1_PTR)->BREQUESTTYPE) & 
         USB_SETUP_DATA_XFER_DIRECTION)) 
      {
         pipe_descr_ptr->SEND_PHASE = TRUE;
      }

      USB_lock();
      
      /* return successful transfer initiation status */
      pipe_descr_ptr->STATUS = USB_STATUS_TRANSFER_QUEUED;

      _usb_hci_vusb11_send_setup(handle, pipe_descr_ptr);

      /* Indicate that a transfer is pending on this pipe */
      pipe_descr_ptr->PACKETPENDING = TRUE;

      USB_unlock();
   }
   
   return USB_STATUS_TRANSFER_QUEUED;

}

/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : _usb_host_get_transfer_status
*  Returned Value : Status
*  Comments       :
* _usb_host_get_transfer_status is a routine that returns status of a transfer. 
*
*END*-----------------------------------------------------------------*/
uint_8 _usb_host_get_transfer_status
   (
      /* [IN] the USB Host state structure */
      _usb_host_handle     handle,
      
      /* the Pipe ID */
      int_16               pipeid
   )
{ /* Body */
   USB_HOST_STATE_STRUCT_PTR usb_host_ptr;
   
   usb_host_ptr = (USB_HOST_STATE_STRUCT_PTR)handle;

   /* Check if a valid pipe id */   
   if (pipeid < usb_host_ptr->MAX_PIPES) {
      /* return the status of the transfer on this pipe */
      return (usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR[pipeid].STATUS);
   } else {
      return USBERR_INVALID_PIPE_ID;
   } /* Endif */
} /* Endbody */

/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : _usb_host_queue_pkts
*  Returned Value : None
*  Comments       :
*        Queue the Pipe's transaction for processing
*END*-----------------------------------------------------------------*/

void _usb_host_queue_pkts
   (
      /* [IN] the USB Host state structure */
      _usb_host_handle                  handle,

      /* [IN] Pipe descriptor to queue */      
      PIPE_DESCRIPTOR_STRUCT_PTR        pipe_descr_ptr
   )
{
   int_16 pipe_head, pipe_tail;
   USB_HOST_STATE_STRUCT_PTR usb_host_ptr;
   
   usb_host_ptr = (USB_HOST_STATE_STRUCT_PTR)handle;

   USB_lock();

   pipe_descr_ptr->NEXT_PIPE = -1;
   
   /* Get the relevant Pipe Head and Tail */
   if (pipe_descr_ptr->PIPETYPE == USB_ISOCHRONOUS_PIPE) {
      pipe_head = usb_host_ptr->CURRENT_ISO_HEAD;
      pipe_tail = usb_host_ptr->CURRENT_ISO_TAIL;
   } else if (pipe_descr_ptr->PIPETYPE == USB_INTERRUPT_PIPE) {
      pipe_head = usb_host_ptr->CURRENT_INTR_HEAD;
      pipe_tail = usb_host_ptr->CURRENT_INTR_TAIL;
   } else if (pipe_descr_ptr->PIPETYPE == USB_CONTROL_PIPE) {
      pipe_head = usb_host_ptr->CURRENT_CTRL_HEAD;
      pipe_tail = usb_host_ptr->CURRENT_CTRL_TAIL;
   } else if (pipe_descr_ptr->PIPETYPE == USB_BULK_PIPE) {
      pipe_head = usb_host_ptr->CURRENT_BULK_HEAD;
      pipe_tail = usb_host_ptr->CURRENT_BULK_TAIL;
   }
   
   if (pipe_head == -1) {
      /* First pipe added to the Active Pipe List */
      pipe_head = pipe_descr_ptr->PIPE_ID;
      /* assert(pipe_tail == -1); */
   } else { 
   
      if (pipe_tail != -1) {
         /* Add it to the Next pointer of the Tail */
         usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR[pipe_tail].NEXT_PIPE = 
            pipe_descr_ptr->PIPE_ID;
      } else {
         /* This is only the second Active Pipe added to the Active list. 
         ** Add it to the next pointer of the head.
         */
         usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR[pipe_head].NEXT_PIPE = 
            pipe_descr_ptr->PIPE_ID;
      }
      
      /* New Tail */
      pipe_tail = pipe_descr_ptr->PIPE_ID;
   }
   
   /* Re-set the relevant Pipe Head and Tail */
   if (pipe_descr_ptr->PIPETYPE == USB_ISOCHRONOUS_PIPE) {
      usb_host_ptr->CURRENT_ISO_HEAD = pipe_head;
      usb_host_ptr->CURRENT_ISO_TAIL = pipe_tail;
   } else if (pipe_descr_ptr->PIPETYPE == USB_INTERRUPT_PIPE) {
      usb_host_ptr->CURRENT_INTR_HEAD = pipe_head;
      usb_host_ptr->CURRENT_INTR_TAIL = pipe_tail;
   } else if (pipe_descr_ptr->PIPETYPE == USB_CONTROL_PIPE) {
      usb_host_ptr->CURRENT_CTRL_HEAD = pipe_head;
      usb_host_ptr->CURRENT_CTRL_TAIL = pipe_tail;
   } else if (pipe_descr_ptr->PIPETYPE == USB_BULK_PIPE) {
      usb_host_ptr->CURRENT_BULK_HEAD = pipe_head;
      usb_host_ptr->CURRENT_BULK_TAIL = pipe_tail;
   }

   USB_unlock();
   
}

/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : _usb_host_update_current_head
*  Returned Value : None
*  Comments       :
*        Update the respective (based on the pipe type) Queue Head
*END*-----------------------------------------------------------------*/

void _usb_host_update_current_head
   (
      /* [IN] the USB Host state structure */
      _usb_host_handle           handle,
      
      /* [IN] The pipe type */
      uint_8 pipetype
   )
{ /* Body */
   PIPE_DESCRIPTOR_STRUCT_PTR pipe_descr_ptr;
   int_16 pipe_head, pipe_tail;
   USB_HOST_STATE_STRUCT_PTR usb_host_ptr;
   
   usb_host_ptr = (USB_HOST_STATE_STRUCT_PTR)handle;

   USB_lock();
   
   pipe_descr_ptr = NULL;

   if (pipetype == USB_ISOCHRONOUS_PIPE) {
      /* Check if the type of queue is empty */
      if (usb_host_ptr->CURRENT_ISO_HEAD != -1) {
         pipe_descr_ptr = 
            &usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR\
            [usb_host_ptr->CURRENT_ISO_HEAD];
      } /* Endif */
      
      /* Initialize the new queue head and tail */
      pipe_head = usb_host_ptr->CURRENT_ISO_HEAD;
      pipe_tail = usb_host_ptr->CURRENT_ISO_TAIL;
   } else if (pipetype == USB_INTERRUPT_PIPE) {
      /* Check if the type of queue is empty */
      if (usb_host_ptr->CURRENT_INTR_HEAD != -1) {
         pipe_descr_ptr = 
            &usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR\
            [usb_host_ptr->CURRENT_INTR_HEAD];
      } /* Endif */
      
      /* Initialize the new queue head and tail */
      pipe_head = usb_host_ptr->CURRENT_INTR_HEAD;
      pipe_tail = usb_host_ptr->CURRENT_INTR_TAIL;
   } else if (pipetype == USB_CONTROL_PIPE) {
      /* Check if the type of queue is empty */
      if (usb_host_ptr->CURRENT_CTRL_HEAD != -1) {
         pipe_descr_ptr = 
            &usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR\
            [usb_host_ptr->CURRENT_CTRL_HEAD];
      } /* Endif */
      
      /* Initialize the new queue head and tail */
      pipe_head = usb_host_ptr->CURRENT_CTRL_HEAD;
      pipe_tail = usb_host_ptr->CURRENT_CTRL_TAIL;
   } else if (pipetype == USB_BULK_PIPE) {
      /* Check if the type of queue is empty */
      if (usb_host_ptr->CURRENT_BULK_HEAD != -1) {
         pipe_descr_ptr = &usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR\
         [usb_host_ptr->CURRENT_BULK_HEAD];
      } /* Endif */
      /* Initialize the new queue head and tail */
      pipe_head = usb_host_ptr->CURRENT_BULK_HEAD;
      pipe_tail = usb_host_ptr->CURRENT_BULK_TAIL;
   } /* Endif */

   /* Check if another pipe was found on the respective type queue */
   if (pipe_descr_ptr) {
      /* Check if there is another pipe after this */
      if (pipe_descr_ptr->NEXT_PIPE != -1) {
         if ((pipe_descr_ptr->STATUS != USB_STATUS_IDLE) && 
            (pipe_tail != -1)) 
         {
            usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR[pipe_tail].NEXT_PIPE = 
               pipe_head;
            pipe_tail = pipe_head;
         } /* Endif */
         /* Initialize the new pipe head */
         pipe_head = pipe_descr_ptr->NEXT_PIPE;
         if (pipe_tail != -1) {
            usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR[pipe_tail].NEXT_PIPE = -1;
         } /* Endif */
      } else {
         if (pipe_descr_ptr->STATUS == USB_STATUS_IDLE) {
            pipe_head = -1;
            pipe_tail = -1;
         } /* Endif */
      } /* Endif */
   } /* Endif */

   /* Update the new heads and tails of the respective pipes types */   
   if (pipetype == USB_ISOCHRONOUS_PIPE) {
      usb_host_ptr->CURRENT_ISO_HEAD = pipe_head;
      usb_host_ptr->CURRENT_ISO_TAIL = pipe_tail;
   } else if (pipetype == USB_INTERRUPT_PIPE) {
      usb_host_ptr->CURRENT_INTR_HEAD = pipe_head;
      usb_host_ptr->CURRENT_INTR_TAIL = pipe_tail;
   } else if (pipetype == USB_CONTROL_PIPE) {
      usb_host_ptr->CURRENT_CTRL_HEAD = pipe_head;
      usb_host_ptr->CURRENT_CTRL_TAIL = pipe_tail;
   } else if (pipetype == USB_BULK_PIPE) {
      usb_host_ptr->CURRENT_BULK_HEAD = pipe_head;
      usb_host_ptr->CURRENT_BULK_TAIL = pipe_tail;
   } /* Endif */

   USB_unlock();
      
} /* Endbody */

/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : _usb_host_update_current_interval_on_queue
*  Returned Value : None
*  Comments       :
*        Update the interval for the given pipe
*END*-----------------------------------------------------------------*/

void _usb_host_update_current_interval_on_queue
   (
      /* [IN] the USB Host state structure */
      _usb_host_handle           handle,
      
      /* [IN] the USB Host state structure */
      PIPE_DESCRIPTOR_STRUCT_PTR pipe_descr_ptr
   )
{
   USB_HOST_STATE_STRUCT_PTR usb_host_ptr;
   
   usb_host_ptr = (USB_HOST_STATE_STRUCT_PTR)handle;
   
   while(TRUE) {
      /* Update the current interval for all pipes */
      if (pipe_descr_ptr->CURRENT_INTERVAL) {
         pipe_descr_ptr->CURRENT_INTERVAL--;
         if ((!pipe_descr_ptr->CURRENT_INTERVAL) && (pipe_descr_ptr->PIPETYPE != USB_INTERRUPT_PIPE)) {
            pipe_descr_ptr->CURRENT_NAK_COUNT = pipe_descr_ptr->NAK_COUNT;
         }
      }
      if (pipe_descr_ptr->NEXT_PIPE == -1)
         break;
      pipe_descr_ptr = &usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR[pipe_descr_ptr->NEXT_PIPE];
   }
      
}

/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : _usb_host_update_interval_for_pipes
*  Returned Value : None
*  Comments       :
*        Update the interval for the interrupt pipe
*END*-----------------------------------------------------------------*/

void _usb_host_update_interval_for_pipes
   (
      /* [IN] the USB Host state structure */
      _usb_host_handle           handle
   )
{
   PIPE_DESCRIPTOR_STRUCT_PTR pipe_descr_ptr;
   USB_HOST_STATE_STRUCT_PTR usb_host_ptr;
   
   usb_host_ptr = (USB_HOST_STATE_STRUCT_PTR)handle;
   
   USB_lock();

   /* Check if there are any Interrupt pipes on the Interrupt pipe queue */
   if (usb_host_ptr->CURRENT_INTR_HEAD != -1) {
      pipe_descr_ptr = 
         &usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR\
         [usb_host_ptr->CURRENT_INTR_HEAD];
      _usb_host_update_current_interval_on_queue(usb_host_ptr, pipe_descr_ptr);
   }
   
   /* Check if any control pipes are NAK deferred */
   if (usb_host_ptr->CURRENT_CTRL_HEAD != -1) {
      pipe_descr_ptr = 
         &usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR\
         [usb_host_ptr->CURRENT_CTRL_HEAD];
      _usb_host_update_current_interval_on_queue(usb_host_ptr, pipe_descr_ptr);
   }
   
   /* Check if any bulk pipes are NAK deferred */
   if (usb_host_ptr->CURRENT_BULK_HEAD != -1) {
      pipe_descr_ptr = 
         &usb_host_ptr->PIPE_DESCRIPTOR_BASE_PTR\
         [usb_host_ptr->CURRENT_BULK_HEAD];
      _usb_host_update_current_interval_on_queue(usb_host_ptr, pipe_descr_ptr);
   }

   USB_unlock();
  
}