vusbd11ma.c 24.8 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 698 699 700 701 702 703 704 705 706 707 708 709
/*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: vusbd11.c
***
*** Comments:      
***   This file contains the low-level VUSB1.1 routines.
***                                                               
**************************************************************************
*END*********************************************************************/
#ifdef __USB_OS_MQX__
#include "mqx.h"
#include "bsp.h"
#else
#include "devapi.h"
#include "usb.h"
#include "vusb11.h"
#endif
#include "usbprv.h"
#include "usbdprv.h"

#ifndef __USB_OS_MQX__
USB_DEV_STATE_STRUCT_PTR global_usb_device_state_struct_ptr;
extern volatile boolean IN_ISR;
#endif

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_dci_vusb11_init
* Returned Value : USB_OK or error code
* Comments       :
*     Initializes the device layer, i.e. the VUSB, the VUSB DMA Buffer 
*     Descriptor Tables after which the VUSB device will be able to respond
*     to events on the bus. The initial
*     state for the USB device after initialization is the powered state.
* 
*END*--------------------------------------------------------------------*/
uint_8 _usb_dci_vusb11_init
   (
      /* [IN] handle to the usb device */
      _usb_device_handle handle
   )
{ /* Body */
   uint_32 i;
   
   USB_DEV_STATE_STRUCT_PTR usb_dev_ptr;
   
   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
   
   usb_dev_ptr->USB = (USB_STRUCT_PTR)_bsp_get_usb_base(usb_dev_ptr->DEV_NUM);
   usb_dev_ptr->ENDPT_REGS = (USB_REGISTER_PTR)((uint_32)usb_dev_ptr->USB + 
      0x40);
   
   usb_dev_ptr->USB_BDT_PAGE_BLOCK =
      USB_memalloc(sizeof(VUSB_ENDPOINT_BDT_STRUCT) * 
         usb_dev_ptr->MAX_ENDPOINTS * 2 * 2 + USB_BDT_ALIGN(1));
   if (usb_dev_ptr->USB_BDT_PAGE_BLOCK == NULL) {
      return USBERR_ALLOC;
   } /* Endif */
      
   usb_dev_ptr->USB_BDT_PAGE = (VUSB_ENDPOINT_BDT_STRUCT_PTR)
      USB_BDT_ALIGN((uint_32)&usb_dev_ptr->USB_BDT_PAGE_BLOCK[0]);
   
   /* 
   ** _usb_device_init initializes the USB interface.
   ** The BDTs are NOT intialized here. They
   ** are initialized as they are enabled.
   */

   /* disable the VUSB */
   usb_dev_ptr->USB->CONTROL = 0;
   /* mask all interupts */
   usb_dev_ptr->USB->INTENABLE = 0;
   /* Clear all interrupt */
   usb_dev_ptr->USB->INTSTATUS = 0xFF;

   /* mask no errors */
   usb_dev_ptr->USB->ERRORENABLE = 0xff;

   /* Current USB state (POWERED,DEFAULT,ADDRESS,CONFIG,SUSPEND) */
   usb_dev_ptr->USB_STATE = USB_STATE_POWERED;
   /* Current USB configuration selected */
   usb_dev_ptr->USB_CURR_CONFIG = 0;

   /* Set the Buffer Descriptor Table location in the USB regs */
   USB_SET_BDT_PAGE(usb_dev_ptr, usb_dev_ptr->USB_BDT_PAGE);

   /* disable all endpoints */
   for (i = 0; i < usb_dev_ptr->MAX_ENDPOINTS; i++) {
      usb_dev_ptr->ENDPT_REGS[i] = VUSB_ENDPT_DISABLE;
   } /* Endfor */

   /* reserve 2 bytes for a frame counter */
   usb_dev_ptr->USB_SOF_COUNT = 0xffff;

#ifndef __USB_OTG__

#ifdef __USB_OS_MQX__
   if (!(USB_install_isr(_bsp_get_usb_vector(usb_dev_ptr->DEV_NUM), 
      _usb_dci_vusb11_isr, (pointer)usb_dev_ptr)))
   {
      return USBERR_INSTALL_ISR;
   } /* Endbody */
#else
   USB_install_isr(_bsp_get_usb_vector(usb_dev_ptr->DEV_NUM), 
      _usb_dci_vusb11_isr, (pointer)usb_dev_ptr);
#endif /* USB_OS_MQX */

#endif /* USB_OTG */

   /* Reset the BDT Odd ping/pong bits to 0 */   
   usb_dev_ptr->USB->CONTROL = VUSB_CTL_ODD_RST;

   /* enable the USB */
   usb_dev_ptr->USB->CONTROL = VUSB_CTL_USB_EN;
   
   /* Init the service structure */
   usb_dev_ptr->SERVICE_HEAD_PTR = NULL;
   
   /* enable only bus reset interrupts at this point */
   usb_dev_ptr->USB->INTENABLE = VUSB_INT_ENABLE_RESET;

#ifndef __USB_OS_MQX__
   global_usb_device_state_struct_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
#endif

   return USB_OK;
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_dci_vusb11_isr
* Returned Value : None
* Comments       :
*     Catchall ISR for USB interrupts.  Handles all aspects of USB
*     interrupts.
* 
*END*--------------------------------------------------------------------*/
#ifndef __USB_OS_MQX__
DEVICE_INTERRUPT_ROUTINE_KEYWORD void _usb_dci_vusb11_isr
   (
      void
   )
#else
void _usb_dci_vusb11_isr
   (
      _usb_device_handle handle
   )
#endif
{ /* Body */
   XD_STRUCT_PTR                 pxd;
   BDT_STRUCT_PTR                temp_BDT_PTR;
   USB_DEV_STATE_STRUCT_PTR      usb_dev_ptr;
   uint_8                        direction, status, odd_even;
   uint_16                       wBytes;

#ifdef __USB_OS_MQX__
   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
#else
   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)global_usb_device_state_struct_ptr;
#ifndef __USB_OTG__   
   IN_ISR = TRUE;
#endif
#endif
   
   while (TRUE) {
      /* clear error bits that we've seen */
      status = usb_dev_ptr->USB->ERRORSTATUS;
      usb_dev_ptr->USB->ERRORSTATUS = status;
      
      status = (usb_dev_ptr->USB->INTSTATUS & usb_dev_ptr->USB->INTENABLE);
      
      if (!status) {
         break;
      } /* Endif */
      
      direction = USB_REGISTER_STATUS_TXRX(usb_dev_ptr->USB);
      odd_even = USB_REGISTER_STATUS_ODDEVEN(usb_dev_ptr->USB);
      usb_dev_ptr->EP = USB_REGISTER_STATUS_ENDPOINT(usb_dev_ptr->USB);
      usb_dev_ptr->BDT_PTR = &usb_dev_ptr->USB_BDT_PAGE\
         [usb_dev_ptr->EP][direction][odd_even];
         
      wBytes = VUSB_GET_BC(usb_dev_ptr->BDT_PTR);
         
      /* Clear all enabled interrupts */
      usb_dev_ptr->USB->INTSTATUS = status;
         
      if (status & VUSB_INT_STAT_RESET) {
         /* A USB Bus Reset has occurred */
         _usb_dci_vusb11_bus_reset((_usb_device_handle)usb_dev_ptr);
         _usb_device_call_service((_usb_device_handle)usb_dev_ptr, 
            USB_SERVICE_BUS_RESET, 0, 0, 0, 0);
         /* Enqueue a recv buffer for receiving setup packet */
         usb_dev_ptr->XDRECV[0].SETUP_BUFFER_QUEUED = FALSE;
         usb_dev_ptr->XDSEND[0].SETUP_BUFFER_QUEUED = FALSE;
         _usb_device_recv_data((_usb_device_handle)usb_dev_ptr, 0, 
            (uchar_ptr)&usb_dev_ptr->XDRECV[0].SETUP_BUFFER, 8);
         usb_dev_ptr->XDRECV[0].SETUP_BUFFER_QUEUED = TRUE;
         usb_dev_ptr->XDSEND[0].SETUP_BUFFER_QUEUED = TRUE;
      } else if (status & VUSB_INT_STAT_TOKEN_DONE) {
         /* Usb Check For Suspend */
         if (usb_dev_ptr->BDT_PTR->REGISTER.BITMAP.PID == TOKEN_SETUP) {
            /* and the txd_suspend bit is set */
            if (usb_dev_ptr->USB->CONTROL & VUSB_CTL_TXD_SUSPEND) {
               
               temp_BDT_PTR = &usb_dev_ptr->USB_BDT_PAGE\
                  [usb_dev_ptr->EP][USB_SEND][0];
                  
               if (temp_BDT_PTR->REGISTER.BITMAP.OWNS) {
                  if (temp_BDT_PTR->REGISTER.BDTCTL & VUSB_BDT_STALL_BIT) {
                     temp_BDT_PTR->REGISTER.BDTCTL &= ~VUSB_BDT_STALL_BIT;
                     temp_BDT_PTR->REGISTER.BITMAP.OWNS = 0;
                  } /* Endif */
               } /* Endif */
               
               temp_BDT_PTR = &usb_dev_ptr->USB_BDT_PAGE\
                  [usb_dev_ptr->EP][USB_SEND][1];
                  
               if (temp_BDT_PTR->REGISTER.BITMAP.OWNS) {
                  if (temp_BDT_PTR->REGISTER.BDTCTL & VUSB_BDT_STALL_BIT) {
                     temp_BDT_PTR->REGISTER.BDTCTL &= ~VUSB_BDT_STALL_BIT;
                     temp_BDT_PTR->REGISTER.BITMAP.OWNS = 0;
                  } /* Endif */
               } /* Endif */
               
               /* Setup packet received so clear endpoint stall bit here in 
               ** case of a protcol stall 
               */
               
               usb_dev_ptr->ENDPT_REGS[usb_dev_ptr->EP] &= ~VUSB_EP_CTRL_STALL;
               usb_dev_ptr->XDSEND[usb_dev_ptr->EP].CTL &= ~VUSB_EP_CTRL_STALL;
               usb_dev_ptr->XDRECV[usb_dev_ptr->EP].CTL &= ~VUSB_EP_CTRL_STALL;
               
               /* dequeue any pending packets */
               if (usb_dev_ptr->XDSEND[usb_dev_ptr->EP].PACKETPENDING) {
                  _usb_device_cancel_transfer((_usb_device_handle)usb_dev_ptr, 
                     usb_dev_ptr->EP, USB_SEND);
               } /* Endif */
               
               /* clear the txd_suspend bit */
               usb_dev_ptr->USB->CONTROL &= ~VUSB_CTL_TXD_SUSPEND;
            } /* Endif */
         } /* Endif */
         
         if (direction == USB_SEND) {
            pxd = &usb_dev_ptr->XDSEND[usb_dev_ptr->EP];
         } else {
            pxd = &usb_dev_ptr->XDRECV[usb_dev_ptr->EP];
         } /* Endif */

         pxd->SOFAR += wBytes;
         pxd->UNACKNOWLEDGEDBYTES -= wBytes;
         
         if ((usb_dev_ptr->BDT_PTR->REGISTER.BITMAP.PID == TOKEN_SETUP) ||
            (!(pxd->CTL & VUSB_ENDPT_HSHK_BIT)))
         {

            /* clear endpoint stall bit here in case of a protcol stall */
            usb_dev_ptr->ENDPT_REGS[0] &= ~VUSB_EP_CTRL_STALL;
            
            /*
            ** Setup packets always arrive in their entirety, as they're only 8
            ** bytes and require no terminating null.  The same is true for
            ** isochronous endpoints (which don't even get acknowledgements).
            */
            /* The transfer has completed.  Reset the XD. */
            VUSB_CLEAR_CURRENT_BDT(usb_dev_ptr->BDT_PTR, pxd);
            if (pxd->PACKETPENDING) {
               /*
               ** Any remaining BDT should be removed, as it's not needed to
               ** complete this transaction.
               */
               VUSB_CLEAR_OTHER_BDT(usb_dev_ptr->BDT_PTR, pxd);
            } /* Endif */
            
            pxd->BDTCTL = 0;
            pxd->STATUS = USB_STATUS_IDLE;
            _usb_device_call_service((_usb_device_handle)usb_dev_ptr, 
               usb_dev_ptr->EP,
               /* If we have handshakes, it is a control endpoint -> setup */
               ((pxd->CTL & VUSB_ENDPT_HSHK_BIT) ? 1 : 0),
               direction, pxd->STARTADDRESS, pxd->SOFAR);
               
            /* Check if its Isochronous packet or Setup packet */
            if (pxd->CTL & VUSB_ENDPT_HSHK_BIT) {
               pxd->SETUP_BUFFER_QUEUED = FALSE;
               
               if (direction == USB_SEND) {
                  usb_dev_ptr->XDRECV[usb_dev_ptr->EP].SETUP_BUFFER_QUEUED = 
                     FALSE;
               } else {
                  usb_dev_ptr->XDSEND[usb_dev_ptr->EP].SETUP_BUFFER_QUEUED = 
                     FALSE;
               } /* Endif */
         
               /* Enqueue a recv buffer for receiving setup packet */
               _usb_device_recv_data((_usb_device_handle)usb_dev_ptr, 
                  usb_dev_ptr->EP,
                  (uchar_ptr)&usb_dev_ptr->XDRECV[usb_dev_ptr->EP].SETUP_BUFFER,\
                  8);
               pxd->SETUP_BUFFER_QUEUED = TRUE;
               
               if (direction == USB_SEND) {
                  usb_dev_ptr->XDRECV[usb_dev_ptr->EP].SETUP_BUFFER_QUEUED = 
                     TRUE;
               } else {
                  usb_dev_ptr->XDSEND[usb_dev_ptr->EP].SETUP_BUFFER_QUEUED = 
                     TRUE;
               } /* Endif */
            } /* Endif */
         } else if (usb_dev_ptr->BDT_PTR->BC == pxd->MAXPACKET) {
            VUSB_CLEAR_CURRENT_BDT(usb_dev_ptr->BDT_PTR, pxd);
            /*
            ** If there is still data for which a BDT has not been sent, we must
            ** send another BDT.  This includes zero-length BDTs on non-control
            ** endpoints for transactions that are a multiple of the endpoint's
            ** max packet size.
            */
            if (pxd->TODO || pxd->MUSTSENDNULL) {
               pxd->STATUS = USB_STATUS_TRANSFER_IN_PROGRESS;
               _usb_dci_vusb11_submit_BDT(usb_dev_ptr->BDT_PTR, pxd);
            } else if (pxd->PACKETPENDING  && pxd->UNACKNOWLEDGEDBYTES) {
               pxd->STATUS = USB_STATUS_TRANSFER_IN_PROGRESS;
            } else { 
               pxd->BDTCTL = 0;
               pxd->STATUS = USB_STATUS_IDLE;
            
               _usb_device_call_service((_usb_device_handle)usb_dev_ptr, 
                  usb_dev_ptr->EP, 0, direction, pxd->STARTADDRESS, pxd->SOFAR);
            } /* Endif */
         } else if (usb_dev_ptr->BDT_PTR->BC < pxd->MAXPACKET) {
            /*
            ** If we have received a short packet, the transaction is complete
            ** regardless of endpoint or the expected size.
            */
            VUSB_CLEAR_CURRENT_BDT(usb_dev_ptr->BDT_PTR, pxd);
            
            if (pxd->PACKETPENDING) {
               /*
               ** Any remaining BDT should be removed, as it's not needed to
               ** complete this transaction.
               */
               VUSB_CLEAR_OTHER_BDT(usb_dev_ptr->BDT_PTR, pxd);
            } /* Endif */
            
            pxd->BDTCTL = 0;
            pxd->STATUS = USB_STATUS_IDLE;
            
            _usb_device_call_service((_usb_device_handle)usb_dev_ptr, 
               usb_dev_ptr->EP, 0, direction, pxd->STARTADDRESS, pxd->SOFAR);
            
            if (pxd->TYPE == USB_CONTROL_ENDPOINT) {
               /* protocol stall - stall the other BD */
                  temp_BDT_PTR = VUSB_TOGGLE_BDT(usb_dev_ptr->BDT_PTR);
                  temp_BDT_PTR->REGISTER.BDTCTL = VUSB_BDT_STALL_BIT;
                  temp_BDT_PTR->REGISTER.BITMAP.OWNS = 1;
               if ((!direction) && (!wBytes)) {
                  pxd->CTL |= VUSB_EP_CTRL_STALL;
                  pxd->SETUP_BUFFER_QUEUED = FALSE;
                  usb_dev_ptr->XDSEND[usb_dev_ptr->EP].SETUP_BUFFER_QUEUED = 
                     FALSE;
                  /* Enqueue a recv buffer for receiving setup packet */
                  _usb_device_recv_data((_usb_device_handle)usb_dev_ptr, 
                     usb_dev_ptr->EP, (uchar_ptr)&pxd->SETUP_BUFFER, 8);
                  pxd->SETUP_BUFFER_QUEUED = TRUE;
                  usb_dev_ptr->XDSEND[usb_dev_ptr->EP].SETUP_BUFFER_QUEUED = 
                     TRUE;
               } /* Endif */
            } /* Endif */
            
         } /* Endif */
      } /* Endif */ 
      
      if (status & VUSB_INT_STAT_RESUME) {
         _usb_device_call_service((_usb_device_handle)usb_dev_ptr, 
            USB_SERVICE_RESUME, 0 , 0, NULL, 0);
      }  /* Endif */
      
      if (status & VUSB_INT_STAT_SOF) {
         _usb_device_call_service((_usb_device_handle)usb_dev_ptr, 
            USB_SERVICE_SOF, 0 , 0, NULL, 0);
      }  /* Endif */
      
      if (status & VUSB_INT_STAT_SLEEP) {
         _usb_device_call_service((_usb_device_handle)usb_dev_ptr, 
            USB_SERVICE_SLEEP, 0 , 0, NULL, 0);
      } /* Endif */ 
      
      if (status & VUSB_INT_STAT_ERROR) {
         _usb_device_call_service((_usb_device_handle)usb_dev_ptr, 
            USB_SERVICE_ERROR, 0 , 0, NULL, usb_dev_ptr->USB->ERRORSTATUS);
      } /* Endif */
      
      if (status & VUSB_INT_STAT_STALL) {
         _usb_device_call_service((_usb_device_handle)usb_dev_ptr, 
            USB_SERVICE_STALL, 0 , 0, NULL, 
            (usb_dev_ptr->USB->INTSTATUS & VUSB_INT_STAT_STALL));
      } /* Endif */
      
   } /* Endwhile */

#ifndef __USB_OS_MQX__
#ifndef __USB_OTG__
   IN_ISR = FALSE;
#endif   
#endif

} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_dci_vusb11_submit_BDT
* Returned Value : None
* Comments       :
*     Submits a BDT (along with packet buffer) to the VUSB.
* 
*END*--------------------------------------------------------------------*/
void _usb_dci_vusb11_submit_BDT
   (
      /* [IN] Pointer to the BDT to be released to the VUSB */
      BDT_STRUCT_PTR pBDT,
      
      /* [IN] Pointer to the transfer descriptor (XD) for this BDT */
      XD_STRUCT_PTR pxd
   )
{ /* Body */
   uint_16 wActualBytes;
   BDT_STRUCT pid;

   pxd->NEXTODDEVEN = VUSB_TOGGLE_BIT(pxd->NEXTODDEVEN);

   VUSB_SET_ADDRESS(pBDT, pxd->NEXTADDRESS);

   /* Use the specified size */
   wActualBytes = pxd->TODO;
   if (pxd->TODO > pxd->MAXPACKET) {
      /* Unless we can't.  Then just use max packet size */
      wActualBytes = pxd->MAXPACKET;
   } /* Endif */

   /* compute the BDT */
   /* Copy everything from the XD */
   pid.REGISTER.BDTCTL = pxd->BDTCTL;
   /* Set Data Toggle Synch */
   pid.REGISTER.BDTCTL |= VUSB_BDT_DTS_BIT;
   /* Sumbit the BDT */
   pid.REGISTER.BITMAP.OWNS = 1;

   /* if DATA01 is clear */
   if ((pxd->BDTCTL & VUSB_BDT_DATA01_BIT) == 0) {
      /* get value from bNextData01 */
      pid.REGISTER.BITMAP.DATA01 = pxd->NEXTDATA01;
   } /* Endif */

   /* clear DATA01 flag so we'll use bNextData01 next time */
   pxd->BDTCTL &= ~VUSB_BDT_DATA01_BIT;

   /* toggle bit for next packet */
   pxd->NEXTDATA01 = VUSB_TOGGLE_BIT(pid.REGISTER.BITMAP.DATA01);

   /* inc count of packets queued */
   pxd->PACKETPENDING++;

   /* Default is not to terminate with a null packet. */
   pxd->MUSTSENDNULL = FALSE;

   /*
   ** if it is not an ISO endpoint
   ** check if we need to 
   ** terminate with a null packet
   */
   if (pxd->CTL & VUSB_ENDPT_HSHK_BIT) {
      if (pxd->TODO == pxd->MAXPACKET) {
         /* Don't set the MUSTSENDNULL if it is a Setup packet */
         if (((pxd->TYPE != USB_CONTROL_ENDPOINT) && 
            (!pxd->DONT_ZERO_TERMINATE)) || 
            ((pxd->TYPE == USB_CONTROL_ENDPOINT) &&  
            (pxd->SETUP_BUFFER_QUEUED))) 
         {
            pxd->MUSTSENDNULL = TRUE;
         } /* Endif */
      } /* Endif */
   } /* Endif */

   pxd->NEXTADDRESS += wActualBytes;
   pxd->TODO -= wActualBytes;

   /* Actually write the BDT PID/DATA01/OWNS/BC data */
   VUSB_SET_BC(pBDT, wActualBytes);
   VUSB_COPY_OWN_DATA_PID(pBDT, &pid);

   return;
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_dci_vusb11_read_setup
* Returned Value : None
* Comments       :
*     Reads the setup packet into the buffer provided by the user.
* 
*END*--------------------------------------------------------------------*/
void _usb_dci_vusb11_read_setup
   (
      /* [IN] Handle to the USB device */
      _usb_device_handle      handle,

      /* [IN] the Endpoint number */      
      uint_8                  ep_num,
            
      /* [IN] buffer for receiving Setup packet */
      uchar_ptr               buff_ptr
   )
{ /* Body */
   USB_DEV_STATE_STRUCT_PTR usb_dev_ptr;
   
   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
   
   /* Copy the setup packet from the BD to the user's buffer */
   USB_memcopy((uchar_ptr)usb_dev_ptr->BDT_PTR->ADDRESS, buff_ptr, 8);
   
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_dci_vusb11_submit_transfer
* Returned Value : None
* Comments       :
*     Submits a prepared transfer to the VUSB
* 
*END*--------------------------------------------------------------------*/
void _usb_dci_vusb11_submit_transfer
   (
      /* [IN] Handle to the USB device */
      _usb_device_handle handle,
      
      /* [IN] Direction of transfer.  Is it transmit? */
      uint_8             direction,
      
      /* [IN] Endpoint to submit transfer on */
      uint_8             ep
   )
{ /* Body */
   USB_DEV_STATE_STRUCT_PTR usb_dev_ptr;
   XD_STRUCT_PTR pxd;
   
   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
   
   if (direction == USB_SEND) {
      pxd = &usb_dev_ptr->XDSEND[ep];
   } else { 
      pxd = &usb_dev_ptr->XDRECV[ep];
   } /* Endif */
   
   if (pxd->TYPE == USB_CONTROL_ENDPOINT) {
      if ((pxd->CTL & VUSB_EP_CTRL_STALL) && (!direction)) {
         pxd->BDTCTL = (VUSB_BDT_DATA01_BIT | VUSB_EP_CTRL_STALL);
      } else {
         pxd->BDTCTL = VUSB_BDT_DATA01_BIT;
      } /* Endif */
   } else { 
      pxd->BDTCTL = 0;
   } /* Endif */
   
   /* Set the owns bit in the transfer descriptor */
   pxd->BDTCTL |= VUSB_BDT_OWNS_BIT;
   
   _usb_dci_vusb11_submit_BDT(
      &usb_dev_ptr->USB_BDT_PAGE[ep][direction][pxd->NEXTODDEVEN & 1], pxd);
      
   if (pxd->TODO || pxd->MUSTSENDNULL) {
      _usb_dci_vusb11_submit_BDT(
         &usb_dev_ptr->USB_BDT_PAGE[ep][direction][pxd->NEXTODDEVEN & 1], pxd);
   } /* Endif */
   
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_dci_vusb11_bus_reset
* Returned Value : None
* Comments       :
*     Called when a USB bus reset is asserted by the host
* 
*END*--------------------------------------------------------------------*/
void _usb_dci_vusb11_bus_reset
   (
      /* [IN] Handle to the USB device */
      _usb_device_handle handle
   )
{ /* Body */
   USB_DEV_STATE_STRUCT_PTR   usb_dev_ptr;
   uchar                      i;

   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
   
   /* saw a USB Bus Reset
   **
   ** we got the USB RESET indication. We simply need to go to the
   ** DEFAULT state and enable endpoint 0 at addr 0.
   ** First, assign EP0 an RX buffer
   */

   /* Reset BDT page. */
   USB_memzero(usb_dev_ptr->USB_BDT_PAGE, sizeof(VUSB_ENDPOINT_BDT_STRUCT) *
      usb_dev_ptr->MAX_ENDPOINTS * 2 * 2);

   usb_dev_ptr->USB_STATE = USB_STATE_DEFAULT;
   /* Current USB configuration is reset back to zero */
   usb_dev_ptr->USB_CURR_CONFIG = 0;

   /*
   ** These are special hardware writes that clear the status FIFO which
   ** is four bytes deep.
   **
   ** clear pending token done
   */
   usb_dev_ptr->USB->INTSTATUS = VUSB_INT_STAT_TOKEN_DONE;
   usb_dev_ptr->USB->INTSTATUS = VUSB_INT_STAT_TOKEN_DONE;
   usb_dev_ptr->USB->INTSTATUS = VUSB_INT_STAT_TOKEN_DONE;
   usb_dev_ptr->USB->INTSTATUS = VUSB_INT_STAT_TOKEN_DONE;

   /* USB addr=0 (default) */
   usb_dev_ptr->USB->ADDRESS = 0;
   
   /* clear the BDT ODD bits in the VUSB and keep VUSB enabled */
   usb_dev_ptr->USB->CONTROL = (VUSB_CTL_ODD_RST | VUSB_CTL_USB_EN);
   
   USB_memzero((uchar_ptr)usb_dev_ptr->XDSEND, 
      sizeof(XD_STRUCT) * usb_dev_ptr->MAX_ENDPOINTS);

   USB_memzero((uchar_ptr)usb_dev_ptr->XDRECV, 
      sizeof(XD_STRUCT) * usb_dev_ptr->MAX_ENDPOINTS);

   for (i = 0; i < usb_dev_ptr->MAX_ENDPOINTS; i++) {
      /*
      ** initialize the endpoint status of all endpoints to Disabled
      ** on Bus reset.
      */
      usb_dev_ptr->XDSEND[i].STATUS = USB_STATUS_DISABLED;
      usb_dev_ptr->XDRECV[i].STATUS = USB_STATUS_DISABLED;
   } /* Endfor */
   
   /* enable the VUSB */
   usb_dev_ptr->USB->CONTROL = VUSB_CTL_USB_EN;

   /*
   ** enable all ints
   */
    usb_dev_ptr->USB->INTENABLE = 0xFF; 

   return;
} /* Endbody */

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_dci_vusb11_init_endpoint
* Returned Value : None
* Comments       :
*     Sets the endpoint registers e.g. to enable TX, RX, control
* 
*END*--------------------------------------------------------------------*/
void _usb_dci_vusb11_init_endpoint
   (
      /* [IN] Handle to the USB device */
      _usb_device_handle   handle,
            
      /* [IN] the endpoint number */
      uint_8               ep,
      
      /* [IN] The transfer descriptor for this endpoint */
      XD_STRUCT_PTR        pxd
   )
{ /* Body */
      USB_DEV_STATE_STRUCT_PTR usb_dev_ptr;
      
      usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
      
      switch (pxd->TYPE) {
         case USB_ISOCHRONOUS_ENDPOINT:
            pxd->CTL = VUSB_ENDPT_ISO_BIDIR;
            usb_dev_ptr->ENDPT_REGS[ep] = VUSB_ENDPT_ISO_BIDIR;
            break;
         case USB_INTERRUPT_ENDPOINT:
            pxd->CTL = VUSB_ENDPT_BULK_BIDIR;
            usb_dev_ptr->ENDPT_REGS[ep] = VUSB_ENDPT_BULK_BIDIR;
            break;
         case USB_CONTROL_ENDPOINT:
            pxd->CTL = VUSB_ENDPT_CONTROL;
            usb_dev_ptr->ENDPT_REGS[ep] = VUSB_ENDPT_CONTROL;
            break;
         case USB_BULK_ENDPOINT:
            pxd->CTL = VUSB_ENDPT_BULK_BIDIR;
            usb_dev_ptr->ENDPT_REGS[ep] = VUSB_ENDPT_BULK_BIDIR;
         default:
            break;
      } /* Endswitch */
      
} /* EndBody */

/* EOF */