vusbd11ma.c 31.2 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 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
/*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*********************************************************************/

#include "os_bb.h"
#include "osint.h"
#include "../host.h"	/* XXX for printf */

/*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
   )
{
   uint_32 i, which;
   
   USB_DEV_STATE_STRUCT_PTR usb_dev_ptr;
   
   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;

   /*
    * ARC device code does not use the OTG registers, so the base
    * register pointer points into the middle of the full register set.
    */
   which = usb_dev_ptr->CTLR_NUM;
   usb_dev_ptr->USB = (USB_STRUCT_PTR)
           PHYS_TO_K1(((which) ? USB1_BASE_REG : USB0_BASE_REG) +
                       (USB0_INT_STAT_REG - USB0_BASE_REG));
   usb_dev_ptr->ENDPT_REGS = (USB_REGISTER_PTR)((uint_32)usb_dev_ptr->USB + 
           (USB0_ENDPT0_REG - USB0_INT_STAT_REG));
   usb_dev_ptr->USB_BDT_PAGE = (VUSB_ENDPOINT_BDT_STRUCT_PTR)
            PHYS_TO_K1((which) ? USB1_BDT_SRAM : USB0_BDT_SRAM);

   PRINTF("dci_vusb11_init: USB%d, regs 0x%x, epregs 0x%x, bdt 0x%x\n", which,
   	usb_dev_ptr->USB,
        usb_dev_ptr->ENDPT_REGS,
        usb_dev_ptr->USB_BDT_PAGE);
   
   /* 
   ** _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, K1_TO_PHYS(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;
   }

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

   /* 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;

   return USB_OK;
}

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_dci_vusb11_isr
* Returned Value : None
* Comments       :
*     Catchall ISR for USB interrupts.  Handles all aspects of USB
*     interrupts.
* 
*END*--------------------------------------------------------------------*/

#ifdef INSTRUMENT
int __osUsbSOF=0;
int __osUsbBDTMissed=0;
int __osUsbMissedCount=0;
int __osUsbStatus=0;
int __bdt0Owns;
int __bdt0BC;
int __bdt0Addr;
int __bdt1Owns;
int __bdt1BC;
int __bdt1Addr;
int __wbdt0Owns;
int __wbdt0BC;
int __wbdt0Addr;
int __wbdt1Owns;
int __wbdt1BC;
int __wbdt1Addr;
#endif

void _usb_dci_vusb11_isr
   (
      /* [IN] handle to the usb device */
      _usb_device_handle handle
   )
{
   XD_STRUCT_PTR                 pxd;
   BDT_STRUCT_PTR                temp_BDT_PTR;
   BDT_STRUCT                    temp_bdt;
   BDT_STRUCT                    copy_bdt;
   USB_DEV_STATE_STRUCT_PTR      usb_dev_ptr;
   uint_8                        direction, status, odd_even;
   uint_16                       wBytes;
   uint_8                        statusregval;
   uint_8                        *bufaddr;
   int                           owns;
   int                           toggle;

   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
   
   while (TRUE) {
      /* clear error bits that we've seen */
      status = usb_dev_ptr->USB->ERRORSTATUS;
      usb_dev_ptr->USB->ERRORSTATUS = status;
if (status)
PRINTF("dci_isr: ERROR 0x%x\n", status);
      
      status = (usb_dev_ptr->USB->INTSTATUS & usb_dev_ptr->USB->INTENABLE);
      
      if (!status) {
         break;
      }

      // Check for an unhandled BDT
      if ((status & VUSB_INT_STAT_SOF) && !(status & VUSB_INT_STAT_TOKEN_DONE)) {
          int error=0;
#ifdef INSTRUMENT
          __osUsbSOF++;
#endif
          usb_dev_ptr->BDT_PTR = &usb_dev_ptr->USB_BDT_PAGE[2][USB_RECV][0];
          _usb_bdt_copy_swab_d(usb_dev_ptr->BDT_PTR, &copy_bdt); 
          wBytes = copy_bdt.BC;
          owns = (copy_bdt.REGISTER.BDTCTL & VUSB_BDT_OWNS_BIT) != 0;
          if ((owns==0) && (wBytes>0)) {
              error = 1;
          }
#ifdef INSTRUMENT
          __bdt0Owns = owns;
          __bdt0BC = wBytes;
          __bdt0Addr = (int) copy_bdt.ADDRESS;
#endif
          usb_dev_ptr->BDT_PTR = &usb_dev_ptr->USB_BDT_PAGE[2][USB_RECV][1];
          _usb_bdt_copy_swab_d(usb_dev_ptr->BDT_PTR, &copy_bdt); 
          wBytes = copy_bdt.BC;
          owns = (copy_bdt.REGISTER.BDTCTL & VUSB_BDT_OWNS_BIT) != 0;
          if ((owns==0) && (wBytes>0)) {
              error = 2;
          }
#ifdef INSTRUMENT
          __bdt1Owns = owns;
          __bdt1BC = wBytes;
          __bdt1Addr = (int) copy_bdt.ADDRESS;
#endif
          usb_dev_ptr->BDT_PTR = &usb_dev_ptr->USB_BDT_PAGE[2][USB_SEND][0];
          _usb_bdt_copy_swab_d(usb_dev_ptr->BDT_PTR, &copy_bdt); 
          wBytes = copy_bdt.BC;
          owns = (copy_bdt.REGISTER.BDTCTL & VUSB_BDT_OWNS_BIT) != 0;
          if ((owns==0) && (wBytes>0)) {
              error = 3;
          }
#ifdef INSTRUMENT
          __wbdt0Owns = owns;
          __wbdt0BC = wBytes;
          __wbdt0Addr = (int) copy_bdt.ADDRESS;
#endif
          usb_dev_ptr->BDT_PTR = &usb_dev_ptr->USB_BDT_PAGE[2][USB_SEND][1];
          _usb_bdt_copy_swab_d(usb_dev_ptr->BDT_PTR, &copy_bdt); 
          wBytes = copy_bdt.BC;
          owns = (copy_bdt.REGISTER.BDTCTL & VUSB_BDT_OWNS_BIT) != 0;
          if ((owns==0) && (wBytes>0)) {
              error = 4;
          }
#ifdef INSTRUMENT
          __wbdt1Owns = owns;
          __wbdt1BC = wBytes;
          __wbdt1Addr = (int) copy_bdt.ADDRESS;
#endif
          if (error) {
              int newstatus = (usb_dev_ptr->USB->INTSTATUS & usb_dev_ptr->USB->INTENABLE);
              if ((newstatus & VUSB_INT_STAT_TOKEN_DONE) == 0) {
                  // Add token done to status - maybe that would fix everything?
                  status |= VUSB_INT_STAT_TOKEN_DONE;
#ifdef INSTRUMENT
                  __osUsbBDTMissed = error;
                  __osUsbMissedCount++;
                  __osUsbStatus = usb_dev_ptr->USB->STATUS;
#endif
              }
          }
      }

      /*
       * XXX experimental code here:
       * XXX see if it makes a difference only touching the
       * XXX STATUS register when there is a token done
       */
      if (status & VUSB_INT_STAT_TOKEN_DONE) {
#if 0      
      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);
#else
      statusregval = usb_dev_ptr->USB->STATUS;
      direction = (statusregval >> 3) & 0x01;
      odd_even = (statusregval >> 2) & 0x01;
      usb_dev_ptr->EP = (statusregval >> 4) & 0x0F;
#endif
      usb_dev_ptr->BDT_PTR = &usb_dev_ptr->USB_BDT_PAGE\
         [usb_dev_ptr->EP][direction][odd_even];
        
      /* get the BDT from HW and swap the bytes */
     _usb_bdt_copy_swab_d(usb_dev_ptr->BDT_PTR, &copy_bdt); 
      wBytes = copy_bdt.BC;
      bufaddr = copy_bdt.ADDRESS;
      owns = (copy_bdt.REGISTER.BDTCTL & VUSB_BDT_OWNS_BIT) != 0;
      toggle = (copy_bdt.REGISTER.BDTCTL & VUSB_BDT_DATA01_BIT) != 0;
   }
         
      /* Clear all enabled interrupts */
      usb_dev_ptr->USB->INTSTATUS = status;
         
      if (status & VUSB_INT_STAT_RESET) {
PRINTF("dci_isr: RESET\n");
         /* 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;	/* XXX ??? */
         break;	/* XXX */
      } else if (status & VUSB_INT_STAT_TOKEN_DONE) {
PRINTF("dci_isr: TOKEN_DONE for BDT 0x%x owns %d d0/1 %d buf 0x%x count %d at ticks = %d\n", usb_dev_ptr->BDT_PTR, owns, toggle, bufaddr, wBytes, osGetCount());
         /* Usb Check For Suspend */
         if (((copy_bdt.REGISTER.BDTCTL & VUSB_BD_PID_MASKS) >> 2) == TOKEN_SETUP) {
//PRINTF("dci_isr: SETUP_TOKEN\n");
            /* and the txd_suspend bit is set */
            if (usb_dev_ptr->USB->CONTROL & VUSB_CTL_TXD_SUSPEND) {
              
//PRINTF("dci_isr: SUSPEND is set, so clear BDTs\n");
	       /* get BDT from HW */ 
               _usb_bdt_copy_swab_d(&usb_dev_ptr->USB_BDT_PAGE\
                  [usb_dev_ptr->EP][USB_SEND][0], &temp_bdt);
	       temp_BDT_PTR = &temp_bdt;
                  
               if (temp_BDT_PTR->REGISTER.BDTCTL & VUSB_BDT_OWNS_BIT) {
                  if (temp_BDT_PTR->REGISTER.BDTCTL & VUSB_BDT_STALL_BIT) {
                     temp_BDT_PTR->REGISTER.BDTCTL &= ~VUSB_BDT_STALL_BIT;
                     temp_BDT_PTR->REGISTER.BDTCTL &= ~VUSB_BDT_OWNS_BIT;
	       	     /* copy BDT back to HW */ 
                     _usb_bdt_copy_swab_d(&temp_bdt,
                         &usb_dev_ptr->USB_BDT_PAGE\
			    [usb_dev_ptr->EP][USB_SEND][0]);
                  }
               }
               
	       /* get BDT from HW */ 
               _usb_bdt_copy_swab_d(&usb_dev_ptr->USB_BDT_PAGE\
                  [usb_dev_ptr->EP][USB_SEND][1], &temp_bdt);
	       temp_BDT_PTR = &temp_bdt;
                  
               if (temp_BDT_PTR->REGISTER.BDTCTL & VUSB_BDT_OWNS_BIT) {
                  if (temp_BDT_PTR->REGISTER.BDTCTL & VUSB_BDT_STALL_BIT) {
                     temp_BDT_PTR->REGISTER.BDTCTL &= ~VUSB_BDT_STALL_BIT;
                     temp_BDT_PTR->REGISTER.BDTCTL &= ~VUSB_BDT_OWNS_BIT;
	       	     /* copy BDT back to HW */ 
                     _usb_bdt_copy_swab_d(&temp_bdt,
                         &usb_dev_ptr->USB_BDT_PAGE\
			    [usb_dev_ptr->EP][USB_SEND][1]);
                  }
               }
               
               /*
	       ** Setup packet received, so clear endpoint stall bit here in 
               ** case of a protocol 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);
               }
               
               /* clear the txd_suspend bit */
               usb_dev_ptr->USB->CONTROL &= ~VUSB_CTL_TXD_SUSPEND;
            }
         }
         
         if (direction == USB_SEND) {
            pxd = &usb_dev_ptr->XDSEND[usb_dev_ptr->EP];
         } else {
            pxd = &usb_dev_ptr->XDRECV[usb_dev_ptr->EP];
         }

         pxd->SOFAR += wBytes;
         pxd->UNACKNOWLEDGEDBYTES -= wBytes;
         
         if ((((copy_bdt.REGISTER.BDTCTL & VUSB_BD_PID_MASKS) >> 2) == TOKEN_SETUP) ||
            (!(pxd->CTL & VUSB_ENDPT_HSHK_BIT))) {

            /* clear endpoint stall bit here in case of a protocol 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. */
//PRINTF("dci_isr: SETUP_TOKEN clear BDT 0x%x\n", usb_dev_ptr->BDT_PTR);
            VUSB_CLEAR_CURRENT_BDT(&copy_bdt, pxd);
            /* write the BDT back to HW */
            _usb_bdt_copy_swab_d(&copy_bdt, usb_dev_ptr->BDT_PTR);

            if (pxd->PACKETPENDING) {
               /*
               ** Any remaining BDT should be removed, as it's not needed to
               ** complete this transaction.
               */
//PRINTF("dci_isr: SETUP_TOKEN pkt pending, clear other BDT 0x%x\n", VUSB_TOGGLE_BDT(usb_dev_ptr->BDT_PTR));
               _usb_bdt_copy_swab_d(VUSB_TOGGLE_BDT(usb_dev_ptr->BDT_PTR),
		       &copy_bdt);
               VUSB_CLEAR_ALT_BDT(&copy_bdt, pxd);
               _usb_bdt_copy_swab_d(&copy_bdt,
		       VUSB_TOGGLE_BDT(usb_dev_ptr->BDT_PTR));
            }
            pxd->SETUP_BUFFER_QUEUED = FALSE;

            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;
               
//PRINTF("dci_isr: TOKEN_DONE HSHK set, post setup buf\n");
               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;
               }
         
               /* 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;
               }
            }
         } else if (wBytes == pxd->MAXPACKET) {
PRINTF("dci_isr: TOKEN_DONE full packet, clear BDT 0x%x\n", usb_dev_ptr->BDT_PTR);
            VUSB_CLEAR_CURRENT_BDT(&copy_bdt, pxd);
            /* write the BDT back to HW */
            _usb_bdt_copy_swab_d(&copy_bdt, usb_dev_ptr->BDT_PTR);
            /*
            ** 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 { 
	       if (pxd->PACKETPENDING) {
PRINTF("dci_isr: TOKEN_DONE w/TODO == 0, clear other pending BDT 0x%x\n", VUSB_TOGGLE_BDT(usb_dev_ptr->BDT_PTR));
                  _usb_bdt_copy_swab_d(VUSB_TOGGLE_BDT(usb_dev_ptr->BDT_PTR),
		       &copy_bdt);
                  VUSB_CLEAR_ALT_BDT(&copy_bdt, pxd);
                  _usb_bdt_copy_swab_d(&copy_bdt,
		       VUSB_TOGGLE_BDT(usb_dev_ptr->BDT_PTR));
	       }
               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);
            }
         } else if (wBytes < pxd->MAXPACKET) {
            /*
            ** If we have received a short packet, the transaction is complete
            ** regardless of endpoint or the expected size.
            */
PRINTF("dci_isr: TOKEN_DONE short packet, clear BDT 0x%x\n", usb_dev_ptr->BDT_PTR);
            VUSB_CLEAR_CURRENT_BDT(&copy_bdt, pxd);
            /* write the BDT back to HW */
            _usb_bdt_copy_swab_d(&copy_bdt, usb_dev_ptr->BDT_PTR);
            
            if (pxd->PACKETPENDING) {
               /*
               ** Any remaining BDT should be removed, as it's not needed to
               ** complete this transaction.
               */
PRINTF("dci_isr: TOKEN_DONE short packet, clear other BDT 0x%x\n", VUSB_TOGGLE_BDT(usb_dev_ptr->BDT_PTR));
               _usb_bdt_copy_swab_d(VUSB_TOGGLE_BDT(usb_dev_ptr->BDT_PTR),
		       &copy_bdt);
               VUSB_CLEAR_ALT_BDT(&copy_bdt, pxd);
               _usb_bdt_copy_swab_d(&copy_bdt,
		       VUSB_TOGGLE_BDT(usb_dev_ptr->BDT_PTR));
            }
            
            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) {
#if 0
	/* XXX
	 * XXX why should this cause a STALL?
	 * XXX
	 * This bit of code seems wrong and it definitely causes
	 * the driver not to work.  Most setup transactions close
	 * with a 0 length packet as an ACK at the protocol level.
	 * E.g. in a GetDescription request, the last transfer is
	 * a 0 length OUT token packet going from Host to Device to ACK
	 * the Descriptor.  This needs to be handled by calling the
	 * service routine and then posting another setup buffer
	 * for the next transaction.
	 *
	 * Note that there are NAKs that happen during the
	 * DATA transactions that follow the initial SETUP token.
	 * These are handled by the ARC core below the level of
	 * the software:  the core will NAK the host until a
	 * BDT is posted to send or receive as appropriate.
	 * This might be considered a "STALL", but doesn't
	 * need to manifest itself at this level.
	 */
	PRINTF("dci_isr: TOKEN_DONE short packet, stall other BDT 0x%x\n", VUSB_TOGGLE_BDT(usb_dev_ptr->BDT_PTR));
                  /* protocol stall - stall the other BD */
	          /* get BDT from HW */ 
                  _usb_bdt_copy_swab_d(VUSB_TOGGLE_BDT(usb_dev_ptr->BDT_PTR),
		                                     &temp_bdt);
	          temp_BDT_PTR = &temp_bdt;
                  temp_BDT_PTR->REGISTER.BDTCTL = VUSB_BDT_STALL_BIT;
                  temp_BDT_PTR->REGISTER.BITMAP.OWNS = 1;
                  _usb_bdt_copy_swab_d(&temp_bdt,
				  VUSB_TOGGLE_BDT(usb_dev_ptr->BDT_PTR));
#endif
               if ((!direction) && (!wBytes)) {
                  // XXX 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;
               }
            }
            
         }
      }
      
      if (status & VUSB_INT_STAT_RESUME) {
PRINTF("dci_isr: RESUME\n");
         _usb_device_call_service((_usb_device_handle)usb_dev_ptr, 
            USB_SERVICE_RESUME, 0 , 0, NULL, 0);
      }
      
      if (status & VUSB_INT_STAT_SOF) {
// PRINTF("dci_isr: SOF\n");
          _usb_device_call_service((_usb_device_handle)usb_dev_ptr, 
                                   USB_SERVICE_SOF, 0 , 0, NULL, 0);
      }
      
      if (status & VUSB_INT_STAT_SLEEP) {
PRINTF("dci_isr: SLEEP\n");
         _usb_device_call_service((_usb_device_handle)usb_dev_ptr, 
            USB_SERVICE_SLEEP, 0 , 0, NULL, 0);
      }
      
      if (status & VUSB_INT_STAT_ERROR) {
PRINTF("dci_isr: ERROR\n");
         _usb_device_call_service((_usb_device_handle)usb_dev_ptr, 
            USB_SERVICE_ERROR, 0 , 0, NULL, usb_dev_ptr->USB->ERRORSTATUS);
      }
      
      if (status & VUSB_INT_STAT_STALL) {
PRINTF("dci_isr: STALL\n");
         _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));
      }
      
   }

}

/*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
   )
{
   uint_16 wActualBytes;
   BDT_STRUCT pid;

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

   _usb_bdt_copy_swab_d(pBDT, &pid);
   VUSB_SET_ADDRESS(&pid, (uchar_ptr)K0_TO_PHYS(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;
   }

#if BBPLAYER
   /*
    * Invalidate/Writeback the cache since hw is not io coherent
    */
   osInvalDCache(pxd->NEXTADDRESS, wActualBytes);
#endif

   /* compute the BDT */
   /* Copy everything from the XD */
   pid.REGISTER.BDTCTL = pxd->BDTCTL;
   /* Set Data Toggle Synch and Own bits */
   pid.REGISTER.BDTCTL |= (VUSB_BDT_DTS_BIT | VUSB_BDT_OWNS_BIT);

   /* if DATA01 is clear */
   if ((pxd->BDTCTL & VUSB_BDT_DATA01_BIT) == 0) {
      /* get value from bNextData01 */
      pid.REGISTER.BDTCTL &= ~VUSB_BDT_DATA01_BIT;
      pid.REGISTER.BDTCTL |= pxd->NEXTDATA01;
   } else {
      /*
       * clear DATA01 flag so we'll use bNextData01 next time
       * and get 0
       */
      pxd->BDTCTL &= ~VUSB_BDT_DATA01_BIT;
      pxd->NEXTDATA01 = VUSB_BDT_DATA01_BIT;
   }

   /* toggle bit for next packet */
   pxd->NEXTDATA01 ^= VUSB_BDT_DATA01_BIT;

   /* 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;
         }
      }
   }

#if 1
   PRINTF("submit_bdt 0x%x, NEO %d, DTS %d, D0/1 %d, BC %d, buf 0x%x, pend %d, todo %d, sofar %d, sendnull %d\n",
      pBDT, pxd->NEXTODDEVEN,
      (pid.REGISTER.BDTCTL & VUSB_BDT_DTS_BIT) != 0,
      (pid.REGISTER.BDTCTL & 0x40) >> 6,
      wActualBytes, K0_TO_PHYS(pxd->NEXTADDRESS),
      pxd->PACKETPENDING, pxd->TODO,
      pxd->SOFAR, pxd->MUSTSENDNULL);
#endif

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

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

/*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
   )
{
   USB_DEV_STATE_STRUCT_PTR usb_dev_ptr;
   BDT_STRUCT bdt;
   
   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;

   /* Get the BDT from hw and untwist it */
   _usb_bdt_copy_swab_d(usb_dev_ptr->BDT_PTR, &bdt);

   /* Copy the setup packet from the BD to the user's buffer */
   bcopy((void *)PHYS_TO_K1(bdt.ADDRESS), buff_ptr, 8);
   
}

/*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
   )
{
   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];
   }
   
   if (pxd->TYPE == USB_CONTROL_ENDPOINT) {
      if ((pxd->CTL & VUSB_EP_CTRL_STALL) && (!direction)) {
         /* pxd->BDTCTL = (VUSB_BDT_DATA01_BIT | VUSB_EP_CTRL_STALL); XXX?*/
         pxd->BDTCTL = (VUSB_BDT_DATA01_BIT | VUSB_BDT_STALL_BIT); /* XXX?*/
      } else {
         pxd->BDTCTL = VUSB_BDT_DATA01_BIT;
      }
   } else { 
      pxd->BDTCTL = 0;
   }
   
   /* 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);
   }
 
}

extern u32 __Usb_Reset_Count[];

/*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
   )
{
   USB_DEV_STATE_STRUCT_PTR   usb_dev_ptr;
   uchar                      i;

   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;
   PRINTF("dci_bus_reset USB%d\n", usb_dev_ptr->CTLR_NUM);
   
   /* 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
   */
   __Usb_Reset_Count[usb_dev_ptr->CTLR_NUM]++;

   /* Reset BDT page. */
   bzero(usb_dev_ptr->USB_BDT_PAGE, USB_BDT_SIZE);

   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);
   
   bzero((uchar_ptr)usb_dev_ptr->XDSEND, 
      sizeof(XD_STRUCT) * usb_dev_ptr->MAX_ENDPOINTS);

   bzero((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;
   }
   
   /* enable the VUSB */
   usb_dev_ptr->USB->CONTROL = VUSB_CTL_USB_EN;

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

   return;
}

/*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
   )
{
      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;
      }
      
}