dev_snd.c 3.74 KB
/*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             
***
*** $Workfile:devapi.c$
*** $Revision: 1.6 $
*** $Date: 2003/06/08 00:08:28 $
***
*** Comments:      
***   This file contains the USB device API.
***                                                               
**************************************************************************
*END*********************************************************************/

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

/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : _usb_device_send_data
* Returned Value : USB_OK or error code
* Comments       :
*  The Send Data routine is non-blocking routine that causes a block of data up 
*  to 64k bytes long to be made available for transmission to the USB host. The 
*  inputs to send data are bEP a byte endpoint number in the range 0-15 
*  Send_Data returns a status byte indicating whether the transfer 
*  will be initiated. 
*
*END*--------------------------------------------------------------------*/
uint_8 _usb_device_send_data
   (
      /* [IN] handle to USB device */
      _usb_device_handle handle,
      
      /* [IN] Endpoint to send data to */
      uint_8               endpoint_number,
      
      /* [IN] Pointer to data to send */
      uchar_ptr            buff_ptr,
      
      /* [IN] Length of data to send */
      uint_32              size
   )
{
   XD_STRUCT_PTR              pxd;
   USB_DEV_STATE_STRUCT_PTR   usb_dev_ptr;
   
   usb_dev_ptr = (USB_DEV_STATE_STRUCT_PTR)handle;

   USB_lock();
   pxd = &usb_dev_ptr->XDSEND[endpoint_number];

   /*
   ** Check if the endpoint is disabled
   */
   if (pxd->STATUS == USB_STATUS_DISABLED) {
      USB_unlock();
      return USBERR_ENDPOINT_DISABLED;
   }
   
   /*
   ** Check if any transfer is in progress on this endpoint
   */
   if (pxd->STATUS == USB_STATUS_TRANSFER_IN_PROGRESS) {
      USB_unlock();
      return USBERR_TRANSFER_IN_PROGRESS;
   }

   if (pxd->TYPE == USB_ISOCHRONOUS_ENDPOINT) {
      /* if Isochronous */
      if (size > pxd->MAXPACKET) {
         /* 
         ** if asked for more than maxPacket
         ** limit request to maxPacket
         */
         size = pxd->MAXPACKET;
      }
   }

   /* The endpoint is enabled and no transfers are pending so queue the 
   ** transmit transfer. 
   */
   pxd->STARTADDRESS = buff_ptr;
   pxd->NEXTADDRESS  = buff_ptr;
   pxd->TODO = size;
   pxd->SOFAR = 0;
   pxd->UNACKNOWLEDGEDBYTES = size;
   pxd->DIRECTION = USB_SEND;

#if BBPLAYER
   /*
    * Writeback the cache, since hw is not io coherent
    */
   osWritebackDCache(buff_ptr, size);

   //PRINTF("dev_send_data: ep %d, buf 0x%x, size %d\n", endpoint_number, buff_ptr, size);
#endif

   /* return successful transfer initiation status. */
   pxd->STATUS = USB_STATUS_TRANSFER_PENDING;

   _usb_dci_vusb11_submit_transfer(handle, USB_SEND, endpoint_number);

   /*
   ** If the endpoint is currently stalled notify the user after the
   ** transfer has been queued.  By queuing the transfer the
   ** operation to revert the stall condition may be separated from
   ** the the data transfer code.
   */
   if (pxd->STATUS == USB_STATUS_STALLED) {
      USB_unlock();
      return USBERR_ENDPOINT_STALLED;
   }

   USB_unlock();
   return USB_OK;
}