static usb_status usb_class_cdc_cntrl_common ( /* [IN] The communication device common command structure */ cdc_command_t * com_ptr, /* [IN] The communication device control interface */ usb_acm_class_intf_struct_t * if_ctrl_ptr, /* [IN] Bitmask of the request type */ uint8_t bmrequesttype, /* [IN] Request code */ uint8_t brequest, /* [IN] Value to copy into wvalue field of the REQUEST */ uint16_t wvalue, /* [IN] Length of the data associated with REQUEST */ uint16_t wlength, /* [IN] Pointer to data buffer used to send/recv */ uint8_t * data ) { /* Body */ usb_data_class_intf_struct_t * if_ptr; //usb_setup_t req; usb_status status = USBERR_NO_INTERFACE; usb_pipe_handle pipe_handle; tr_struct_t* tr_ptr; #ifdef _HOST_DEBUG_ DEBUG_LOG_TRACE("usb_class_cdc_cntrl_common"); #endif if_ptr = (usb_data_class_intf_struct_t *) com_ptr->CLASS_PTR->class_intf_handle; /* Save the higher level callback and ID */ if_ptr->ctrl_callback = com_ptr->CALLBACK_FN; if_ptr->ctrl_callback_param = com_ptr->CALLBACK_PARAM; pipe_handle = (usb_pipe_handle)usb_host_dev_mng_get_control_pipe(if_ptr->dev_handle); if (usb_host_get_tr(if_ptr->host_handle, usb_class_cdc_cntrl_callback, if_ptr, &tr_ptr) != USB_OK) { printf("usb_class_cdc_cntrl_common: error to get tr cdc\n"); return USBERR_ERROR; } /* Set TR buffer length as required */ if ((REQ_TYPE_IN & bmrequesttype) != 0) { tr_ptr->rx_buffer = data; tr_ptr->rx_length = wlength; } else { tr_ptr->tx_buffer = data; tr_ptr->tx_length = wlength; } tr_ptr->setup_packet.bmrequesttype = bmrequesttype; tr_ptr->setup_packet.brequest = brequest; *(uint16_t*)tr_ptr->setup_packet.wvalue = USB_HOST_TO_LE_SHORT(wvalue); *(uint16_t*)tr_ptr->setup_packet.windex = USB_HOST_TO_LE_SHORT(if_ctrl_ptr->intf_num); *(uint16_t*)tr_ptr->setup_packet.wlength = USB_HOST_TO_LE_SHORT(wlength); /* Since this function is general, we must distinguish here interface class to perform ** appropriate action. ** Only ACM devices are supported now. */ switch (((interface_descriptor_t*) if_ctrl_ptr->intf_handle)->bInterfaceSubClass) { case USB_SUBCLASS_COM_DIRECT: break; case USB_SUBCLASS_COM_ABSTRACT: if (USB_OK == (status = usb_host_send_setup(if_ptr->host_handle, pipe_handle, tr_ptr))) { status = USB_OK; } else { printf("\nError in usb_class_hid_cntrl_common: %x", status); usb_host_release_tr(if_ptr->host_handle, tr_ptr); status = USBERR_ERROR; } break; case USB_SUBCLASS_COM_TELEPHONE: break; case USB_SUBCLASS_COM_MULTICHAN: break; case USB_SUBCLASS_COM_CAPI: break; case USB_SUBCLASS_COM_ETHERNET: break; case USB_SUBCLASS_COM_ATM_NET: break; } #ifdef _HOST_DEBUG_ if (!status) { DEBUG_LOG_TRACE("usb_class_cdc_cntrl_common, SUCCESSFUL"); } else { DEBUG_LOG_TRACE("usb_class_cdc_cntrl_common, FAILED"); } #endif return status; } /* Endbody */
/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_class_video_cntrl_common * Returned Value : USB_OK if command has been passed on USB. * Comments : * This function is used to send a control request * *END*--------------------------------------------------------------------*/ static usb_status usb_class_video_cntrl_common ( /* [IN] The communication device common command structure */ video_command_t* com_ptr, /* [IN] Bitmask of the request type */ uint8_t bmrequesttype, /* [IN] Request code */ uint8_t brequest, /* [IN] Value to copy into wvalue field of the REQUEST */ uint16_t wvalue, /* [IN] Value to copy into windex field of the REQUEST */ uint16_t windex, /* [IN] Length of the data associated with REQUEST */ uint16_t wlength, /* [IN] Pointer to data buffer used to send/recv */ uint8_t* data ) { /* Body */ usb_video_control_struct_t* video_control = NULL; //usb_setup_t req; usb_status status = USB_OK; usb_pipe_handle pipe_handle; tr_struct_t* tr_ptr; if ((com_ptr == NULL) || (com_ptr->class_control_handle == NULL)) { return USBERR_ERROR; } video_control = (usb_video_control_struct_t*)com_ptr->class_control_handle; if (video_control->in_setup) { return USBERR_TRANSFER_IN_PROGRESS; } if (video_control->dev_handle == NULL) { #ifdef _HOST_DEBUG_ DEBUG_LOG_TRACE("_usb_hostdev_cntrl_request, invalid device handle"); #endif return USBERR_DEVICE_NOT_FOUND; } video_control->ctrl_callback = com_ptr->callback_fn; video_control->ctrl_param = com_ptr->callback_param; pipe_handle = usb_host_dev_mng_get_control_pipe(video_control->dev_handle); if (usb_host_get_tr(video_control->host_handle, usb_class_video_cntrl_callback, video_control, &tr_ptr) != USB_OK) { #ifdef _DEBUG USB_PRINTF("error to get tr video\n"); #endif return USBERR_ERROR; } /* Set TR buffer length as required */ if ((REQ_TYPE_IN & bmrequesttype) != 0) { tr_ptr->rx_buffer = data; tr_ptr->rx_length = wlength; } else { tr_ptr->tx_buffer = data; tr_ptr->tx_length = wlength; } tr_ptr->setup_packet.bmrequesttype = bmrequesttype; tr_ptr->setup_packet.brequest = brequest; *(uint16_t*)tr_ptr->setup_packet.wvalue = USB_HOST_TO_LE_SHORT(wvalue); *(uint16_t*)tr_ptr->setup_packet.windex = USB_HOST_TO_LE_SHORT(windex); *(uint16_t*)tr_ptr->setup_packet.wlength = USB_HOST_TO_LE_SHORT(wlength); video_control->in_setup = TRUE; status = usb_host_send_setup(video_control->host_handle, pipe_handle, tr_ptr); if (status != USB_OK) { #ifdef _DEBUG USB_PRINTF("\nError in usb_class_video_cntrl_common: %x", status); #endif video_control->in_setup = FALSE; usb_host_release_tr(video_control->host_handle, tr_ptr); return USBERR_ERROR; } return status; } /* Endbody */
usb_status usb_class_hub_init ( /* [IN] the device handle related to the class driver */ usb_device_instance_handle dev_handle, /* [IN] the interface handle related to the class driver */ usb_interface_descriptor_handle intf_handle, /* [OUT] printer call struct pointer */ class_handle* class_handle_ptr ) { /* Body */ usb_hub_class_struct_t* hub_class = NULL; usb_device_interface_struct_t* pDeviceIntf = NULL; //interface_descriptor_t* intf = NULL; endpoint_descriptor_t* ep_desc = NULL; uint8_t ep_num; usb_status status = USB_OK; pipe_init_struct_t pipe_init; uint8_t level = 0xFF; #ifdef _HOST_DEBUG_ DEBUG_LOG_TRACE("usb_class_hub_init"); #endif level = usb_host_dev_mng_get_level(dev_handle); if (level > 5) { USB_PRINTF("Too many level of hub attached\n"); *class_handle_ptr = NULL; return USBERR_ERROR; } hub_class = (usb_hub_class_struct_t*)OS_Mem_alloc_zero(sizeof(usb_hub_class_struct_t)); if (hub_class == NULL) { USB_PRINTF("usb_class_hub_init fail on memory allocation\n"); *class_handle_ptr = NULL; return USBERR_ERROR; } hub_class->dev_handle = dev_handle; hub_class->intf_handle = intf_handle; hub_class->host_handle = usb_host_dev_mng_get_host(hub_class->dev_handle); hub_class->level = usb_host_dev_mng_get_level(hub_class->dev_handle); pDeviceIntf = (usb_device_interface_struct_t*)intf_handle; //intf = pDeviceIntf->lpinterfaceDesc; for (ep_num = 0; ep_num < pDeviceIntf->ep_count; ep_num++) { ep_desc = pDeviceIntf->ep[ep_num].lpEndpointDesc; if ((ep_desc->bEndpointAddress & IN_ENDPOINT) && ((ep_desc->bmAttributes & EP_TYPE_MASK) == IRRPT_ENDPOINT)) { pipe_init.endpoint_number = (ep_desc->bEndpointAddress & ENDPOINT_MASK); pipe_init.direction = USB_RECV; pipe_init.pipetype = USB_INTERRUPT_PIPE; pipe_init.max_packet_size = (uint16_t)(USB_SHORT_UNALIGNED_LE_TO_HOST(ep_desc->wMaxPacketSize) & PACKET_SIZE_MASK); pipe_init.interval = ep_desc->iInterval; pipe_init.flags = 0; pipe_init.dev_instance = hub_class->dev_handle; pipe_init.nak_count = USBCFG_HOST_DEFAULT_MAX_NAK_COUNT; status = usb_host_open_pipe(hub_class->host_handle, &hub_class->interrupt_pipe, &pipe_init); if (status != USB_OK) { USB_PRINTF("usb_class_hid_init fail to open in pipe\n"); *class_handle_ptr = (class_handle)hub_class; return USBERR_ERROR; } } } hub_class->control_pipe = usb_host_dev_mng_get_control_pipe(hub_class->dev_handle); *class_handle_ptr = hub_class; #ifdef _HOST_DEBUG_ DEBUG_LOG_TRACE("usb_class_hub_init, SUCCESSFUL"); #endif return USB_OK; } /* Endbody */
/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_class_audio_cntrl_common * Returned Value : USB_OK if command has been passed on USB. * Comments : * This function is used to send a control request. * It must be run with USB locked. * *END*--------------------------------------------------------------------*/ static usb_status usb_class_audio_cntrl_common ( /* [IN] The communication device common command structure */ audio_command_t* com_ptr, /* [IN] The audio control interface */ //audio_control_struct_t* if_ctrl_ptr, /* [IN] Bitmask of the request type */ uint8_t bmrequesttype, /* [IN] Request code */ uint8_t brequest, /* [IN] Value to copy into wvalue field of the REQUEST */ uint16_t wvalue, /* [IN] Value to copy into windex field of the REQUEST */ uint16_t windex, /* [IN] Length of the data associated with REQUEST */ uint16_t wlength, /* [IN] Pointer to data buffer used to send/recv */ uint8_t * data ) { /* Body */ audio_control_struct_t* audio_class = NULL; //usb_setup_t req; usb_status status = USB_OK; usb_pipe_handle pipe_handle; tr_struct_t* tr_ptr; if ((com_ptr == NULL) || (com_ptr->class_control_handle == NULL)) { return USBERR_ERROR; } audio_class = (audio_control_struct_t*)com_ptr->class_control_handle; if (audio_class->in_setup) { return USBERR_TRANSFER_IN_PROGRESS; } if (audio_class->dev_handle == NULL) { return USB_log_error(__FILE__,__LINE__,USBERR_DEVICE_NOT_FOUND); } audio_class->ctrl_callback = com_ptr->callback_fn; audio_class->ctrl_param = com_ptr->callback_param; pipe_handle = usb_host_dev_mng_get_control_pipe(audio_class->dev_handle); if (usb_host_get_tr(audio_class->host_handle, usb_class_audio_cntrl_callback, audio_class, &tr_ptr) != USB_OK) { USB_PRINTF("error to get tr audio \r\n"); return USBERR_ERROR; } /* Set TR buffer length as required */ if ((REQ_TYPE_IN & bmrequesttype) != 0) { tr_ptr->rx_buffer = data; tr_ptr->rx_length = wlength; } else { tr_ptr->tx_buffer = data; tr_ptr->tx_length = wlength; } tr_ptr->setup_packet.bmrequesttype = bmrequesttype; tr_ptr->setup_packet.brequest = brequest; *(uint16_t*)&(tr_ptr->setup_packet.wvalue[0]) = USB_HOST_TO_LE_SHORT(wvalue); *(uint16_t*)&(tr_ptr->setup_packet.windex[0]) = USB_HOST_TO_LE_SHORT(windex); //USB_HOST_TO_LE_SHORT(((interface_descriptor_t*)(audio_class->intf_handle))->bInterfaceNumber); *(uint16_t*)&(tr_ptr->setup_packet.wlength[0]) = USB_HOST_TO_LE_SHORT(wlength); audio_class->in_setup = TRUE; status = usb_host_send_setup(audio_class->host_handle, pipe_handle, tr_ptr); if (status != USB_OK) { audio_class->in_setup = FALSE; USB_PRINTF("\nError in usb_class_audio_cntrl_common: %x", (unsigned int)status); usb_host_release_tr(audio_class->host_handle, tr_ptr); } return status; } /* Endbody */