Example #1
0
/*FUNCTION*----------------------------------------------------------------
* 
* Function Name  : usb_device_call_service
* Returned Value : USB_OK or error code
* Comments       :
*     Calls the appropriate service for the specified type, if one is
*     registered. Used internally only.
* 
*END*--------------------------------------------------------------------*/
usb_status _usb_device_call_service_internal
   (
      /* [IN] pointer to usb device status structure  */ 
      usb_dev_state_struct_t*       usb_dev_ptr,
      /* [IN] pointer to event structure  */ 
      usb_event_struct_t*    event 
   )
{
    service_struct_t*             service_ptr = NULL;
    uint32_t                      i;

    /* Needs mutual exclusion */
    OS_Mutex_lock(usb_dev_ptr->mutex);

    switch (event->type)
    {
        case USB_SERVICE_EP0:
            USB_Control_Service(&usb_dev_ptr->usb_framework, event, service_ptr->arg);
            break;     
        case USB_SERVICE_BUS_RESET:
            USB_Reset_Service(&usb_dev_ptr->usb_framework, event, service_ptr->arg);
            break;
#if USBCFG_DEV_ADVANCED_SUSPEND_RESUME
        case USB_SERVICE_SUSPEND:
            USB_Suspend_Service(&usb_dev_ptr->usb_framework, event, service_ptr->arg);
            break;
        case USB_SERVICE_RESUME:
            USB_Resume_Service(&usb_dev_ptr->usb_framework, event, service_ptr->arg);
            break;
#endif
#if USBCFG_DEV_KHCI_ADVANCED_ERROR_HANDLING
        case USB_SERVICE_ERROR:
            USB_Error_Service(&usb_dev_ptr->usb_framework, event, service_ptr->arg);
            break;
#endif
        default:
            break;
    } /* Endswitch */

    /* Search for an existing entry for type */
    for (i = 0; i < MAX_DEVICE_SERVICE_NUMBER; i++) 
    {
        service_ptr = &usb_dev_ptr->services[i];
        if (service_ptr->type == event->type) 
        {   
            if((event->direction == USB_RECV) && (event->buffer_ptr != NULL))
                OS_dcache_invalidate_mlines((void*)event->buffer_ptr,event->len);
            service_ptr->service(event,service_ptr->arg);
            OS_Mutex_unlock(usb_dev_ptr->mutex);
            return USB_OK;
        }  
    }

	OS_Mutex_unlock(usb_dev_ptr->mutex);
    return USBERR_CLOSED_SERVICE;
} /* EndBody */
/*FUNCTION*-------------------------------------------------------------
 *
 *  Function Name  : usb_device_recv_data
 *  Returned Value : USB_OK or error code
 *  Comments       :
 *        Receives data on a specified endpoint.
 *
 *END*-----------------------------------------------------------------*/
usb_status usb_device_recv_data
(
    /* [IN] the USB_USB_dev_initialize state structure */
    usb_device_handle           handle,
    /* [IN] the Endpoint number */
    uint8_t                     ep_num,
    /* [IN] buffer to receive data */
    uint8_t *                   buff_ptr,
    /* [IN] length of the transfer */
    uint32_t                    size
)
{
    usb_status error = USB_OK;
    xd_struct_t* xd_ptr;
    usb_dev_state_struct_t* usb_dev_ptr;

    if (handle == NULL)
    {
        return USBERR_ERROR;
    }

    usb_dev_ptr = (usb_dev_state_struct_t*)handle;
    if ((usb_dev_ptr->usb_dev_interface)->dev_get_xd != NULL)
    {
        error = (usb_dev_ptr->usb_dev_interface)->dev_get_xd(usb_dev_ptr->controller_handle, &xd_ptr);

        if (USB_OK != error)
        {
#if _DEBUG
            USB_PRINTF("usb_device_recv_data: DEV_GET_XD failed\n");
#endif
            return USBERR_ERROR;
        }
    }
    else
    {
#if _DEBUG
        USB_PRINTF("usb_device_recv_data: DEV_GET_XD is NULL\n");
#endif
        return USBERR_ERROR;
    }

    OS_Mutex_lock(usb_dev_ptr->mutex);

    /* Initialize the new transfer descriptor */
    xd_ptr->ep_num = ep_num;
    xd_ptr->bdirection = USB_RECV;
    xd_ptr->wtotallength = size;
    xd_ptr->wstartaddress = buff_ptr;
    xd_ptr->wsofar = 0;
    xd_ptr->bstatus = USB_STATUS_TRANSFER_ACCEPTED;

    if ((usb_dev_ptr->usb_dev_interface)->dev_recv != NULL)
    {

#if (USBCFG_DEV_BUFF_PROPERTY_CACHEABLE)
        if (size > 0)
        {
            OS_dcache_invalidate_mlines((void*)buff_ptr, size);
        }
#endif
        error = (usb_dev_ptr->usb_dev_interface)->dev_recv(usb_dev_ptr->controller_handle, xd_ptr);
    }
    else
    {
#if _DEBUG
        USB_PRINTF("usb_device_recv_data: DEV_RECV is NULL\n");
#endif
        OS_Mutex_unlock(usb_dev_ptr->mutex);
        return USBERR_ERROR;
    }

    OS_Mutex_unlock(usb_dev_ptr->mutex);
    if (error)
    {
        return USBERR_RX_FAILED;
    } /* Endif */

    return error;
} /* EndBody */