/* usb_init */ void usb_init(void) { usb_status status = USB_OK; status = usb_host_init(CONTROLLER_ID, &g_video_camera.host_handle); if (status != USB_OK) { USB_PRINTF("\r\nUSB Host Initialization failed! STATUS: 0x%x", status); return; } /* ** since we are going to act as the host driver, register the driver ** information for wanted class/subclass/protocols */ status = usb_host_register_driver_info(g_video_camera.host_handle, (void *)DriverInfoTable); if (status != USB_OK) { USB_PRINTF("\r\nUSB Initialization driver info failed! STATUS: 0x%x", status); return; } status = usb_host_register_unsupported_device_notify(g_video_camera.host_handle, usb_host_video_unsupported_device_event); if (status != USB_OK) { USB_PRINTF("\r\nUSB Initialization driver info failed! STATUS: 0x%x", status); return; } g_video_camera.video_camera_control_event = OS_Event_create(0);/* manually clear */ if (g_video_camera.video_camera_control_event == NULL) { USB_PRINTF("\r\nOS_Event_create failed!\r\n"); return; } g_video_camera.video_camera_stream_event = OS_Event_create(0);/* manually clear */ if (g_video_camera.video_camera_stream_event == NULL) { USB_PRINTF("\r\nOS_Event_create failed!\r\n"); return; } g_video_camera.video_command_ptr = (video_command_t*)OS_Mem_alloc_zero(sizeof(video_command_t)); if (g_video_camera.video_command_ptr == NULL) { USB_PRINTF("\r\nOS_Mem_alloc_zero failed!\r\n"); return; } g_video_camera.stream_interface_alternate = 0; time_init(); USB_PRINTF("Video camera starting...\r\n"); }
/*FUNCTION*---------------------------------------------------------------- * * Function Name : main (Main_Task if using MQX) * Returned Value : none * Comments : * Execution starts here * *END*--------------------------------------------------------------------*/ void keyboard_task(uint32_t param) { usb_status status = USB_OK; static unsigned char *buffer; if(keyboard_inited == 0) { g_hid_com = (hid_command_t*) OS_Mem_alloc_zero(sizeof(hid_command_t)); buffer = (unsigned char *)OS_Mem_alloc_uncached(HID_KEYBOARD_BUFFER_SIZE); if (buffer == NULL) { printf("\nMemory allocation failed. STATUS: %x", status); //fflush(stdout); return; } keyboard_inited = 1; } if (OS_Event_check_bit(usb_keyboard_event, USB_Keyboard_Event_CTRL)) { OS_Event_clear(usb_keyboard_event, USB_Keyboard_Event_CTRL); switch (g_kbd_hid_device.dev_state) { case USB_DEVICE_CONFIGURED: status = usb_host_open_dev_interface(host_handle, g_kbd_hid_device.dev_handle, g_kbd_hid_device.intf_handle, (void *) & g_kbd_hid_device.class_handle); if(status != USB_OK) { printf("\nError in _usb_hostdev_select_interface: %x", status); //fflush(stdout); return; } break; case USB_DEVICE_IDLE: break; case USB_DEVICE_INTERFACED: g_hid_com->class_ptr = g_kbd_hid_device.class_handle; g_hid_com->callback_fn = usb_host_hid_keyboard_recv_callback; g_hid_com->callback_param = 0; OS_Event_clear(usb_keyboard_event, USB_Keyboard_Event_DATA); status = usb_class_hid_recv_data(g_hid_com, (unsigned char *) buffer, HID_KEYBOARD_BUFFER_SIZE); if(status != USB_OK) { printf("\nError in _usb_host_recv_data: %x", status); //fflush(stdout); } break; } } if (OS_Event_check_bit(usb_keyboard_event, USB_Keyboard_Event_DATA)) { if (g_kbd_hid_device.dev_state == USB_DEVICE_INTERFACED) { process_kbd_buffer((unsigned char *)buffer); OS_Event_clear(usb_keyboard_event, USB_Keyboard_Event_DATA); g_hid_com->class_ptr = g_kbd_hid_device.class_handle; g_hid_com->callback_fn = usb_host_hid_keyboard_recv_callback; g_hid_com->callback_param = 0; //printf("\nClass handle %x", g_kbd_hid_device.class_handle); OS_Event_clear(usb_keyboard_event, USB_Keyboard_Event_DATA); status = usb_class_hid_recv_data(g_hid_com, (unsigned char *) buffer, HID_KEYBOARD_BUFFER_SIZE); if(status != USB_OK) { printf("\nError in _usb_host_recv_data: %x", status); } } } #if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX) OS_Time_delay(1); #endif }
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 */
void APP_init(void) { usb_status status = USB_OK; uint32_t opt = 0; status = usb_host_init(CONTROLLER_ID, usb_host_board_init, &g_host_handle); if (status != USB_OK) { USB_PRINTF("\r\nUSB Host Initialization failed! STATUS: 0x%x", status); return; } /* ** since we are going to act as the host driver, register the driver ** information for wanted class/subclass/protocols */ status = usb_host_register_driver_info(g_host_handle, (void *) DriverInfoTable); if (status != USB_OK) { USB_PRINTF("\r\nUSB Initialization driver info failed! STATUS: 0x%x", status); return; } status = usb_host_register_unsupported_device_notify(g_host_handle, usb_host_hid_unsupported_device_event); if (status != USB_OK) { USB_PRINTF("\r\nUSB Initialization driver info failed! STATUS: 0x%x", status); return; } mouse_usb_event = OS_Event_create(0);/* manually clear */ if (mouse_usb_event == NULL) { USB_PRINTF("mouse_usb_event create failed\r\n"); return; } mouse_hid_com = (hid_command_t*) OS_Mem_alloc_zero(sizeof(hid_command_t)); if (mouse_hid_com == NULL) { USB_PRINTF("mouse_hid_com create failed\r\n"); return; } kbd_usb_event = OS_Event_create(0);/* manually clear */ if (kbd_usb_event == NULL) { USB_PRINTF("kbd_usb_event create failed\r\n"); return; } OS_Event_set(kbd_usb_event, USB_EVEN_INIT); kbd_hid_com = (hid_command_t*) OS_Mem_alloc_zero(sizeof(hid_command_t)); if (kbd_hid_com == NULL) { USB_PRINTF("kbd_hid_com create failed\r\n"); return; } if ((uint32_t) OS_TASK_ERROR == OS_Task_create(USB_KEYBOARD_TASK_ADDRESS, (void*) g_host_handle, (uint32_t) USB_KEYBOARD_TASK_PRIORITY, USB_KEYBOARD_TASK_STACKSIZE, USB_KEYBOARD_TASK_NAME, &opt)) { USB_PRINTF("keyboard task create failed\r\n"); return; } if ((uint32_t) OS_TASK_ERROR == OS_Task_create(USB_MOUSE_TASK_ADDRESS, (void*) g_host_handle, (uint32_t) USB_MOUSE_TASK_PRIORITY, USB_MOUSE_TASK_STACKSIZE, USB_MOUSE_TASK_NAME, &opt)) { USB_PRINTF("mouse task create failed\r\n"); return; } time_init(); USB_PRINTF("\fUSB HID Mouse+Keyboard\r\nWaiting for USB Mouse or Keyboard to be attached...\r\n"); }
/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_class_video_stream_init * Returned Value : None * Comments : * This function is called by common class to initialize the class driver. It * is called in response to a select interface call by application * *END*--------------------------------------------------------------------*/ usb_status usb_class_video_stream_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_video_stream_struct_t* video_stream_ptr = 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; video_stream_ptr = (usb_video_stream_struct_t*)OS_Mem_alloc_zero(sizeof(usb_video_stream_struct_t)); if (video_stream_ptr == NULL) { #ifdef _DEBUG USB_PRINTF("usb_class_video_stream_init fail on memory allocation\n"); #endif return USBERR_ERROR; } video_stream_ptr->dev_handle = dev_handle; video_stream_ptr->intf_handle = intf_handle; video_stream_ptr->host_handle = (usb_host_handle)usb_host_dev_mng_get_host(video_stream_ptr->dev_handle); video_stream_ptr->altrnate_if_ep_num = 0; video_stream_ptr->altrnate_if_ep_descriptor_list = NULL; pDeviceIntf = (usb_device_interface_struct_t*)intf_handle; video_stream_ptr->stream_interface_ptr = pDeviceIntf; video_stream_ptr->current_stream_interface_ptr = pDeviceIntf->lpinterfaceDesc; video_stream_ptr->vs_input_header_ptr = usb_class_video_find_descriptor_index(pDeviceIntf->interfaceEx, pDeviceIntf->interfaceExlength, CS_INTERFACE, VS_INPUT_HEADER); video_stream_ptr->vs_mjpeg_format_ptr = usb_class_video_find_descriptor_index(pDeviceIntf->interfaceEx, pDeviceIntf->interfaceExlength, CS_INTERFACE, VS_FORMAT_MJPEG); video_stream_ptr->vs_mjpeg_frame_list.frame_descriptor_list_ptr = usb_class_video_find_descriptor_index(pDeviceIntf->interfaceEx, pDeviceIntf->interfaceExlength, CS_INTERFACE, VS_FRAME_MJPEG); video_stream_ptr->vs_mjpeg_frame_list.frame_num = usb_class_video_find_descriptor_count(pDeviceIntf->interfaceEx, pDeviceIntf->interfaceExlength, CS_INTERFACE, VS_FRAME_MJPEG); //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) == ISOCH_ENDPOINT)) { pipe_init.endpoint_number = (ep_desc->bEndpointAddress & ENDPOINT_MASK); pipe_init.direction = USB_RECV; pipe_init.pipetype = USB_ISOCHRONOUS_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 = video_stream_ptr->dev_handle; pipe_init.nak_count = USBCFG_HOST_DEFAULT_MAX_NAK_COUNT; status = usb_host_open_pipe(video_stream_ptr->host_handle, &video_stream_ptr->stream_iso_in_pipe, &pipe_init); if (status != USB_OK) { #ifdef _DEBUG USB_PRINTF("usb_class_video_init fail to open in pipe\n"); #endif *class_handle_ptr = (class_handle)video_stream_ptr; return USBERR_ERROR; } } else if ((ep_desc->bEndpointAddress & OUT_ENDPOINT) && ((ep_desc->bmAttributes & EP_TYPE_MASK) == ISOCH_ENDPOINT)) { pipe_init.endpoint_number = (ep_desc->bEndpointAddress & ENDPOINT_MASK); pipe_init.direction = USB_SEND; pipe_init.pipetype = USB_ISOCHRONOUS_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 = video_stream_ptr->dev_handle; pipe_init.nak_count = USBCFG_HOST_DEFAULT_MAX_NAK_COUNT; status = usb_host_open_pipe(video_stream_ptr->host_handle, &video_stream_ptr->stream_iso_out_pipe, &pipe_init); if (status != USB_OK) { #ifdef _DEBUG USB_PRINTF("usb_class_video_init fail to open in pipe\n"); #endif *class_handle_ptr = (class_handle)video_stream_ptr; return USBERR_ERROR; } } } video_stream_ptr->recv_callback = NULL; video_stream_ptr->recv_param = NULL; *class_handle_ptr = (class_handle)video_stream_ptr; //USB_PRINTF("Video class driver initialized\n"); return USB_OK; } /* Endbody */
/*FUNCTION*---------------------------------------------------------------- * * Function Name : usb_class_video_control_init * Returned Value : None * Comments : * This function is called by common class to initialize the class driver. It * is called in response to a select interface call by application * *END*--------------------------------------------------------------------*/ usb_status usb_class_video_control_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_video_control_struct_t* video_control_ptr = 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; video_control_ptr = (usb_video_control_struct_t*)OS_Mem_alloc_zero(sizeof(usb_video_control_struct_t)); if (video_control_ptr == NULL) { #ifdef _DEBUG USB_PRINTF("usb_class_video_control_init fail on memory allocation\n"); #endif return USBERR_ERROR; } video_control_ptr->dev_handle = dev_handle; video_control_ptr->intf_handle = intf_handle; video_control_ptr->host_handle = (usb_host_handle)usb_host_dev_mng_get_host(video_control_ptr->dev_handle); pDeviceIntf = (usb_device_interface_struct_t*)intf_handle; video_control_ptr->control_interface_ptr = pDeviceIntf; video_control_ptr->vc_interface_header_ptr = usb_class_video_find_descriptor_index(pDeviceIntf->interfaceEx, pDeviceIntf->interfaceExlength, CS_INTERFACE, VC_HEADER); video_control_ptr->vc_input_terminal_ptr = usb_class_video_find_descriptor_index(pDeviceIntf->interfaceEx, pDeviceIntf->interfaceExlength, CS_INTERFACE, VC_INPUT_TERMINAL); video_control_ptr->vc_output_terminal_ptr = usb_class_video_find_descriptor_index(pDeviceIntf->interfaceEx, pDeviceIntf->interfaceExlength, CS_INTERFACE, VC_OUTPUT_TERMINAL); video_control_ptr->vc_processing_unit_ptr = usb_class_video_find_descriptor_index(pDeviceIntf->interfaceEx, pDeviceIntf->interfaceExlength, CS_INTERFACE, VC_PROCESSING_UNIT); video_control_ptr->vc_extension_unit_list_ptr.extension_descriptor_list_ptr = usb_class_video_find_descriptor_index(pDeviceIntf->interfaceEx, pDeviceIntf->interfaceExlength, CS_INTERFACE, VC_EXTENSION_UNIT); video_control_ptr->vc_extension_unit_list_ptr.extension_num = usb_class_video_find_descriptor_count(pDeviceIntf->interfaceEx, pDeviceIntf->interfaceExlength, CS_INTERFACE, VC_EXTENSION_UNIT); //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 = video_control_ptr->dev_handle; pipe_init.nak_count = USBCFG_HOST_DEFAULT_MAX_NAK_COUNT; status = usb_host_open_pipe(video_control_ptr->host_handle, &video_control_ptr->control_interrupt_in_pipe, &pipe_init); if (status != USB_OK) { #ifdef _DEBUG USB_PRINTF("usb_class_video_init fail to open in pipe\n"); #endif *class_handle_ptr = (class_handle)video_control_ptr; return USBERR_ERROR; } } } video_control_ptr->in_setup = FALSE; video_control_ptr->ctrl_callback = NULL; video_control_ptr->ctrl_param = NULL; video_control_ptr->recv_callback = NULL; video_control_ptr->recv_param = NULL; *class_handle_ptr = (class_handle)video_control_ptr; //USB_PRINTF("Video class driver initialized\n"); return USB_OK; } /* Endbody */
static usb_status usb_class_video_fill_endpoint_descriptor(usb_video_stream_struct_t* video_stream_ptr,uint8_t if_alternate) { usb_device_interface_struct_t* pDeviceIntf = NULL; descriptor_union_t ptr1; descriptor_union_t ptr2; usb_device_ep_struct_t * pEndpoint = NULL; usb_status ret = USB_OK; uint8_t ep_num = 0; /*--------------------------------------------------------** ** If descriptor type is Configuration, the pointer is ** ** found in ptr1. Other types of descriptor need ** ** to be found by stepping through the config one ** ** descriptor at a time. ** ** To prevent parsing past the config buffer, ptr2 is ** ** set to the starting address plus its total size. ** **--------------------------------------------------------*/ pDeviceIntf = video_stream_ptr->stream_interface_ptr; ptr1.pntr = pDeviceIntf->interfaceEx; ptr2.word = ptr1.word + pDeviceIntf->interfaceExlength; while (ptr1.word < ptr2.word) { /* the first interface descriptor found */ if (ptr1.common->bDescriptorType == USB_DESC_TYPE_IF) { if (ptr1.intf->bAlternateSetting == if_alternate) { if (ptr1.intf->bNumEndpoints > USBCFG_HOST_MAX_EP_PER_INTERFACE) { USB_PRINTF("too many endpoints in one interface, please increase the USBCFG_HOST_MAX_EP_PER_INTERFACE value\n"); ret = USBERR_ERROR; break; } if (ptr1.intf->bNumEndpoints > 0) { video_stream_ptr->altrnate_if_ep_num = ptr1.intf->bNumEndpoints; } else { return USB_OK; } if(video_stream_ptr->altrnate_if_ep_descriptor_list) { OS_Mem_free(video_stream_ptr->altrnate_if_ep_descriptor_list); } video_stream_ptr->altrnate_if_ep_descriptor_list = (usb_device_ep_struct_t*)OS_Mem_alloc_zero(video_stream_ptr->altrnate_if_ep_num * sizeof(usb_device_ep_struct_t)); ptr1.word += ptr1.common->bLength; while (ptr1.word < ptr2.word) { if (ptr1.common->bDescriptorType != USB_DESC_TYPE_EP) { ptr1.word += ptr1.common->bLength; } else { break; } } /* now the ptr1 should point to endpoint descriptor */ if (ptr1.common->bDescriptorType != USB_DESC_TYPE_EP) { USB_PRINTF("interface descriptor error\n"); ret = USBERR_ERROR; break; } for (ep_num = 0; ep_num < video_stream_ptr->altrnate_if_ep_num; ep_num++) { if ((ptr1.ndpt->bDescriptorType != USB_DESC_TYPE_EP) || (ptr1.word >= ptr2.word)) { USB_PRINTF("endpoint descriptor error\n"); ret = USBERR_ERROR; break; } pEndpoint = &video_stream_ptr->altrnate_if_ep_descriptor_list[ep_num]; pEndpoint->lpEndpointDesc = ptr1.ndpt; ptr1.word += ptr1.common->bLength; while (ptr1.word < ptr2.word) { if (ptr1.common->bDescriptorType != USB_DESC_TYPE_EP) { ptr1.word += ptr1.common->bLength; } else { break; } } } if (ret != USB_OK) { break; } } else { ptr1.word += ptr1.common->bLength; } } else { ptr1.word += ptr1.common->bLength; } } return ret; }
usb_status usb_class_cdc_acm_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_acm_class_intf_struct_t * acm_class_intf = 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; acm_class_intf = (usb_acm_class_intf_struct_t *)OS_Mem_alloc_zero(sizeof(usb_acm_class_intf_struct_t)); if (acm_class_intf == NULL) { printf("usb_class_cdc_acm_init fail on memory allocation\n"); return USBERR_ERROR; } acm_class_intf->dev_handle = dev_handle; acm_class_intf->host_handle = (usb_host_handle)usb_host_dev_mng_get_host(acm_class_intf->dev_handle); pDeviceIntf = (usb_device_interface_struct_t*)intf_handle; intf = pDeviceIntf->lpinterfaceDesc; acm_class_intf->intf_handle = intf; acm_class_intf->intf_num = intf->bInterfaceNumber; acm_class_intf->mutex = OS_Mutex_create(); #ifdef _HOST_DEBUG_ DEBUG_LOG_TRACE("usb_class_cdc_acm_init"); #endif 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 = acm_class_intf->dev_handle; pipe_init.nak_count = USBCFG_HOST_DEFAULT_MAX_NAK_COUNT; status = usb_host_open_pipe(acm_class_intf->host_handle, &acm_class_intf->interrupt_pipe, &pipe_init); if (status != USB_OK) { printf("usb_class_cdc_acm_init fail to open in pipe\n"); *class_handle_ptr = (class_handle)acm_class_intf; return USBERR_ERROR; } } } /* Make sure the device is still attached */ #ifdef _HOST_DEBUG_ if (status) { DEBUG_LOG_TRACE("usb_class_cdc_acm_init, SUCCESSFUL"); } else { DEBUG_LOG_TRACE("usb_class_cdc_acm_init, FAILED"); } #endif *class_handle_ptr = (class_handle)acm_class_intf; return status; } /* Endbody */
usb_status usb_class_cdc_data_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_data_class_intf_struct_t * data_class_intf = 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; data_class_intf = (usb_data_class_intf_struct_t *)OS_Mem_alloc_zero(sizeof(usb_data_class_intf_struct_t)); if (data_class_intf == NULL) { printf("usb_class_cdc_data_init fail on memory allocation\n"); return USBERR_ERROR; } data_class_intf->dev_handle = dev_handle; data_class_intf->host_handle = (usb_host_handle)usb_host_dev_mng_get_host(data_class_intf->dev_handle); pDeviceIntf = (usb_device_interface_struct_t*)intf_handle; intf = pDeviceIntf->lpinterfaceDesc; data_class_intf->intf_handle = intf; data_class_intf->intf_num = intf->bInterfaceNumber; data_class_intf->mutex = OS_Mutex_create(); #ifdef _HOST_DEBUG_ DEBUG_LOG_TRACE("usb_class_cdc_data_init"); #endif 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) == BULK_ENDPOINT)) { pipe_init.endpoint_number = (ep_desc->bEndpointAddress & ENDPOINT_MASK); pipe_init.direction = USB_RECV; pipe_init.pipetype = USB_BULK_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 = data_class_intf->dev_handle; pipe_init.nak_count = 30; status = usb_host_open_pipe(data_class_intf->host_handle, (usb_pipe_handle *)&data_class_intf->in_pipe, &pipe_init); } else if(!(ep_desc->bEndpointAddress & IN_ENDPOINT) && ((ep_desc->bmAttributes & EP_TYPE_MASK) == BULK_ENDPOINT)) { pipe_init.endpoint_number = (ep_desc->bEndpointAddress & ENDPOINT_MASK); pipe_init.direction = USB_SEND; pipe_init.pipetype = USB_BULK_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 = data_class_intf->dev_handle; pipe_init.nak_count = 3000; status = usb_host_open_pipe(data_class_intf->host_handle, (usb_pipe_handle *)&data_class_intf->out_pipe, &pipe_init); } if (status != USB_OK) { printf("usb_class_cdc_data_init fail to open in pipe\n"); *class_handle_ptr = (class_handle)data_class_intf; return USBERR_ERROR; } } /* Make sure the device is still attached */ if ((data_class_intf->in_pipe == NULL) && (data_class_intf->out_pipe == NULL)) status = USBERR_OPEN_PIPE_FAILED; if (data_class_intf->out_pipe) { /* Don't use host - predefined constant for nak_count... ** NOTE!!! ** This hack is not very clean. We need to maximize number of retries to minimize the time of ** transaction (minimize task's time while waiting for 1 transaction to be done (with or without data)) ** The time depends on user expecatation of the read() latency, on the delay between 2 NAKs and on number ** of NAKs to be performed. ** The workaround is to limit amount of retries for the pipe maximally to 3. ** Number 3 is hard-coded here for now. */ // if (((pipe_struct_t*)(data_class_intf->in_pipe))->nak_count > 3) // ((pipe_struct_t*)data_class_intf->in_pipe)->nak_count = 3; /* don't use host - predefined constant */ } if (data_class_intf->in_pipe) { /* The same as for OUT pipe applies here */ // if (((pipe_struct_t*)data_class_intf->out_pipe)->nak_count > 3) // ((pipe_struct_t*)data_class_intf->out_pipe)->nak_count = 3; /* don't use host - predefined constant */ /* initialize buffer */ /* size of buffer equals to the size of endpoint data size */ data_class_intf->RX_BUFFER_SIZE = ((pipe_struct_t*)data_class_intf->in_pipe)->max_packet_size; if (NULL == (data_class_intf->rx_buffer = (uint8_t *)OS_Mem_alloc_zero(data_class_intf->RX_BUFFER_SIZE))) { status = USBERR_ALLOC; } else { /* initialize members */ data_class_intf->RX_BUFFER_APP = data_class_intf->RX_BUFFER_DRV = data_class_intf->rx_buffer; } } #ifdef _HOST_DEBUG_ if (status) { DEBUG_LOG_TRACE("usb_class_cdc_data_init, SUCCESSFUL"); } else { DEBUG_LOG_TRACE("usb_class_cdc_data_init, FAILED"); } #endif *class_handle_ptr = (class_handle)data_class_intf; return status; } /* Endbody */