/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_device_set_status * Returned Value : USB_OK or error code * Comments : * Provides API to set internal state * *END*--------------------------------------------------------------------*/ usb_status _usb_device_set_status ( /* [IN] Handle to the usb device */ usb_device_handle handle, /* [IN] What to set the error of */ uint8_t component, /* [IN] What to set the error to */ uint16_t setting ) { usb_dev_state_struct_t* usb_dev_ptr; uint8_t error = USB_OK; usb_dev_ptr = (usb_dev_state_struct_t*)handle; OS_Mutex_lock(usb_dev_ptr->mutex); if (((usb_dev_interface_functions_struct_t*) usb_dev_ptr->usb_dev_interface)->dev_set_device_status != NULL) { ((usb_dev_interface_functions_struct_t*) usb_dev_ptr->usb_dev_interface)->dev_set_device_status(usb_dev_ptr->controller_handle, (uint8_t)(component),setting); } else { #if _DEBUG printf("usb_device_set_status: dev_set_device_status is NULL\n"); #endif OS_Mutex_unlock(usb_dev_ptr->mutex); return USBERR_ERROR; } OS_Mutex_unlock(usb_dev_ptr->mutex); return error; } /* EndBody */
/*************************************************************************//*! * * @name USB_Cdc_Ep_Mutex_Unlock * * @brief The function unlocks the mutex for RTOS. For BM, enable interrupt. * * @param none. * @return none. * *****************************************************************************/ void USB_Cdc_Ep_Mutex_Unlock(cdc_device_struct_t * cdc_obj_ptr, uint8_t ep_num) { uint8_t index, type, direction; index = USB_Map_Ep_To_Struct_Index(cdc_obj_ptr, ep_num); type = cdc_obj_ptr->ep[index].type; direction = cdc_obj_ptr->ep[index].direction; #if (((OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) && (USE_RTOS))) switch(type) { case USB_BULK_PIPE: if(direction == USB_SEND) { OS_Mutex_unlock(cdc_obj_ptr->send_mutex); } else if(direction == USB_RECV) { OS_Mutex_unlock(cdc_obj_ptr->recv_mutex); } break; case USB_INTERRUPT_PIPE: break; default: break; } #else OS_Unlock(); #endif }
/*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_get_status * Returned Value : USB_OK or error code * Comments : * Provides API to access the USB internal state. * *END*--------------------------------------------------------------------*/ usb_status _usb_device_get_status ( /* [IN] Handle to the USB device */ usb_device_handle handle, /* [IN] What to get the error of */ uint8_t component, /* [OUT] The requested error */ uint16_t* error ) { /* Body */ usb_dev_state_struct_t* usb_dev_ptr; usb_dev_ptr = (usb_dev_state_struct_t*)handle; OS_Mutex_lock(usb_dev_ptr->mutex); if (component & USB_STATUS_ENDPOINT) { if (((usb_dev_interface_functions_struct_t*) usb_dev_ptr->usb_dev_interface)->dev_get_endpoint_status != NULL) { ((usb_dev_interface_functions_struct_t*) usb_dev_ptr->usb_dev_interface)->dev_get_endpoint_status(usb_dev_ptr->controller_handle, (uint8_t)(component),error); } else { #if _DEBUG printf("usb_device_get_status: DEV_GET_ENDPOINT_STATUS is NULL\n"); #endif OS_Mutex_unlock(usb_dev_ptr->mutex); return USBERR_ERROR; } } else { if (((usb_dev_interface_functions_struct_t*) usb_dev_ptr->usb_dev_interface)->dev_get_device_status != NULL) { ((usb_dev_interface_functions_struct_t*) usb_dev_ptr->usb_dev_interface)->dev_get_device_status(usb_dev_ptr->controller_handle, (uint8_t)(component),error); } else { #if _DEBUG printf("usb_device_get_status: DEV_GET_DEVICE_STATUS is NULL\n"); #endif OS_Mutex_unlock(usb_dev_ptr->mutex); return USBERR_ERROR; } } OS_Mutex_unlock(usb_dev_ptr->mutex); return USB_OK; }
/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_device_register_service * Returned Value : USB_OK or error code * Comments : * Registers a callback routine for a specified event or endpoint. * *END*--------------------------------------------------------------------*/ usb_status usb_device_register_service ( /* [IN] Handle to the USB device */ usb_device_handle handle, /* [IN] type of event or endpoint number to service */ uint8_t type, /* [IN] Pointer to the service's callback function */ usb_event_service_t service, /*[IN] User Argument to be passed to Services when invoked.*/ void* arg ) { usb_dev_state_struct_t* usb_dev_ptr; service_struct_t* service_ptr; uint32_t i; if (handle == NULL) { return USBERR_ERROR; } usb_dev_ptr = (usb_dev_state_struct_t*)handle; OS_Mutex_lock(usb_dev_ptr->mutex); for (i = 0; i < MAX_DEVICE_SERVICE_NUMBER; i++) { service_ptr = &usb_dev_ptr->services[i]; if (service_ptr->type == type) { OS_Mutex_unlock(usb_dev_ptr->mutex); return USBERR_OPEN_SERVICE; } } for (i = 0; i < MAX_DEVICE_SERVICE_NUMBER; i++) { service_ptr = &usb_dev_ptr->services[i]; if (service_ptr->type == (uint8_t)-1) { service_ptr->type = type; service_ptr->service = service; service_ptr->arg = arg; OS_Mutex_unlock(usb_dev_ptr->mutex); return USB_OK; } } OS_Mutex_unlock(usb_dev_ptr->mutex); return USBERR_ALLOC; } /* EndBody */
/*************************************************************************//*! * * @name USB_Cdc_Mutex_Unlock * * @brief The function unlocks the mutex for RTOS. For BM, enable interrupt. * * @param none. * @return none. * *****************************************************************************/ void USB_Cdc_Mutex_Unlock(os_mutex_handle handle) { #if (((OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) && (USE_RTOS))) OS_Mutex_unlock(handle); #else OS_Unlock(); #endif }
/*FUNCTION*------------------------------------------------------------- * * Function Name : usb_device_cancel_transfer * Returned Value : USB_OK or error code * Comments : * returns the status of the transaction on the specified endpoint. * *END*-----------------------------------------------------------------*/ usb_status usb_device_cancel_transfer ( /* [IN] the USB_USB_dev_initialize state structure */ usb_device_handle handle, /* [IN] the Endpoint number */ uint8_t ep_num, /* [IN] direction */ uint8_t direction ) { uint8_t error = USB_OK; usb_dev_state_struct_t* usb_dev_ptr; if (handle == NULL) { #if _DEBUG USB_PRINTF("_usb_device_shutdowna: handle is NULL\n"); #endif return USBERR_ERROR; } usb_dev_ptr = (usb_dev_state_struct_t*)handle; OS_Mutex_lock(usb_dev_ptr->mutex); /* Cancel transfer on the specified endpoint for the specified ** direction */ if ((usb_dev_ptr->usb_dev_interface)->dev_cancel_transfer != NULL) { error = (usb_dev_ptr->usb_dev_interface)->dev_cancel_transfer(usb_dev_ptr->controller_handle, ep_num, direction); } else { #if _DEBUG USB_PRINTF("usb_device_cancel_transfer: dev_cancel_transfer is NULL\n"); #endif OS_Mutex_unlock(usb_dev_ptr->mutex); return USBERR_ERROR; } OS_Mutex_unlock(usb_dev_ptr->mutex); return error; }
/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_device_unregister_service * Returned Value : USB_OK or error code * Comments : * Unregisters a callback routine for a specified event or endpoint. * *END*--------------------------------------------------------------------*/ usb_status usb_device_unregister_service ( /* [IN] Handle to the USB device */ usb_device_handle handle, /* [IN] type of event or endpoint number to service */ uint8_t type ) { /* Body */ usb_dev_state_struct_t* usb_dev_ptr; service_struct_t* service_ptr; uint32_t i; if (handle == NULL) { return USBERR_ERROR; } usb_dev_ptr = (usb_dev_state_struct_t*)handle; /* Needs mutual exclusion */ OS_Mutex_lock(usb_dev_ptr->mutex); for (i = 0; i < MAX_DEVICE_SERVICE_NUMBER; i++) { service_ptr = &usb_dev_ptr->services[i]; if (service_ptr->type == type) { service_ptr->type = (uint8_t)-1; service_ptr->service = NULL; service_ptr->arg = NULL; 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_unstall_endpoint * Returned Value : USB_OK or error code * Comments : * Unstalls the endpoint in specified direction * *END*-----------------------------------------------------------------*/ usb_status usb_device_unstall_endpoint ( /* [IN] the USB_USB_dev_initialize state structure */ usb_device_handle handle, /* [IN] the Endpoint number */ uint8_t ep_num, /* [IN] direction */ uint8_t direction ) { usb_status error = USB_OK; usb_dev_state_struct_t* usb_dev_ptr; if (handle == NULL) { #if _DEBUG USB_PRINTF("usb_device_unstall_endpoint: handle is NULL\n"); #endif return USBERR_ERROR; } usb_dev_ptr = (usb_dev_state_struct_t*)handle; OS_Mutex_lock(usb_dev_ptr->mutex); if ((usb_dev_ptr->usb_dev_interface)->dev_unstall_endpoint != NULL) { error= (usb_dev_ptr->usb_dev_interface)->dev_unstall_endpoint(usb_dev_ptr->controller_handle, ep_num, direction); } else { OS_Mutex_unlock(usb_dev_ptr->mutex); return USBERR_ERROR; } OS_Mutex_unlock(usb_dev_ptr->mutex); return error; } /* EndBody */
/*FUNCTION*------------------------------------------------------------- * * Function Name : usb_device_deinit_endpoint * Returned Value : USB_OK or error code * Comments : * Disables the endpoint and the data structures associated with the * endpoint * *END*-----------------------------------------------------------------*/ usb_status usb_device_deinit_endpoint ( /* [IN] the USB_USB_dev_initialize state structure */ usb_device_handle handle, /* [IN] the Endpoint number */ uint8_t ep_num, /* [IN] Direction */ uint8_t direction ) { uint8_t error = 0; usb_dev_state_struct_t* usb_dev_ptr; if (handle == NULL) { return USBERR_ERROR; } usb_dev_ptr = (usb_dev_state_struct_t*)handle; OS_Mutex_lock(usb_dev_ptr->mutex); if (((usb_dev_interface_functions_struct_t*) usb_dev_ptr->usb_dev_interface)->dev_deinit_endoint != NULL) { error = ((usb_dev_interface_functions_struct_t*) usb_dev_ptr->usb_dev_interface)->dev_deinit_endoint(usb_dev_ptr->controller_handle, ep_num, direction); } else { #if _DEBUG printf("usb_device_deinit_endpoint: DEV_DEINIT_ENDPOINT is NULL\n"); #endif return USBERR_ERROR; } OS_Mutex_unlock(usb_dev_ptr->mutex); return error; }
/*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_interface_functions_struct_t*)\ usb_dev_ptr->usb_dev_interface)->dev_get_xd != NULL) { error = ((usb_dev_interface_functions_struct_t*)\ usb_dev_ptr->usb_dev_interface)->dev_get_xd(usb_dev_ptr->controller_handle, &xd_ptr); } else { #if _DEBUG 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_interface_functions_struct_t*)\ usb_dev_ptr->usb_dev_interface)->dev_recv != NULL) { error = ((usb_dev_interface_functions_struct_t*)\ usb_dev_ptr->usb_dev_interface)->dev_recv(usb_dev_ptr->controller_handle, xd_ptr); } else { #if _DEBUG printf("usb_device_recv_data: DEV_RECV is NULL\n"); #endif return USBERR_ERROR; } OS_Mutex_unlock(usb_dev_ptr->mutex); if (error) { return USBERR_RX_FAILED; } /* Endif */ return error; } /* EndBody */
/*FUNCTION*------------------------------------------------------------- * * Function Name : usb_device_send_data * Returned Value : USB_OK or error code * Comments : * Sends data on a specified endpoint. * *END*-----------------------------------------------------------------*/ usb_status usb_device_send_data ( /* [IN] the USB_USB_dev_initialize state structure */ usb_device_handle handle, /* [IN] the Endpoint number */ uint8_t ep_num, /* [IN] buffer to send */ uint8_t * buff_ptr, /* [IN] length of the transfer */ uint32_t size ) { /* Body */ usb_status error; xd_struct_t* xd_ptr; usb_dev_state_struct_t* usb_dev_ptr; if (handle == NULL) { #if _DEBUG printf("usb_device_send_data: handle is NULL\n"); #endif return USBERR_ERROR; } usb_dev_ptr = (usb_dev_state_struct_t*)handle; /******************************************************** If system has a data cache, it is assumed that buffer passed to this routine will be aligned on a cache line boundry. The following code will flush the buffer before passing it to hardware driver. ********************************************************/ OS_dcache_flush_mlines((void*)buff_ptr,size); if (((usb_dev_interface_functions_struct_t*)\ usb_dev_ptr->usb_dev_interface)->dev_get_xd != NULL) { error = ((usb_dev_interface_functions_struct_t*)\ usb_dev_ptr->usb_dev_interface)->dev_get_xd(usb_dev_ptr->controller_handle, &xd_ptr); } else { #if _DEBUG printf("usb_device_send_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_SEND; xd_ptr->wtotallength = size; xd_ptr->wstartaddress = buff_ptr; xd_ptr->wsofar = 0; xd_ptr->bstatus = USB_STATUS_TRANSFER_ACCEPTED; if (((usb_dev_interface_functions_struct_t*)\ usb_dev_ptr->usb_dev_interface)->dev_send != NULL) { error = ((usb_dev_interface_functions_struct_t*)\ usb_dev_ptr->usb_dev_interface)->dev_send(usb_dev_ptr->controller_handle, xd_ptr); } else { #if _DEBUG printf("usb_device_send_data: DEV_SEND is NULL\n"); #endif return USBERR_ERROR; } OS_Mutex_unlock(usb_dev_ptr->mutex); if (error) { #if _DEBUG printf("usb_device_send_data, transfer failed\n"); #endif return USBERR_TX_FAILED; } return error; }