uint_32 _io_usb_mfs_install ( /* [IN] A string that identifies the device for fopen */ char_ptr identifier, /* [IN] Logical unit number which driver need to install */ uint_8 logical_unit, /* [IN] Interface Handle */ CLASS_CALL_STRUCT_PTR ccs_ptr ) { /* Body */ IO_USB_MFS_STRUCT_PTR info_ptr; USB_MASS_CLASS_INTF_STRUCT_PTR intf_ptr = NULL; USB_STATUS error = USBERR_ERROR; /* Pointer validity-checking, clear memory, init header */ USB_lock(); if (usb_host_class_intf_validate(ccs_ptr)) { intf_ptr = (USB_MASS_CLASS_INTF_STRUCT_PTR) ccs_ptr->class_intf_handle; if(intf_ptr != NULL) { error = usb_hostdev_validate (intf_ptr->G.dev_handle); } } /* Endif */ if (USB_OK != error) { /* Device was already detached or intf_ptr is NULL */ USB_unlock(); return MQX_OUT_OF_MEMORY; } if (USB_OK != _usb_hostdev_get_buffer(intf_ptr->G.dev_handle, sizeof(IO_USB_MFS_STRUCT), (pointer *) &info_ptr)) { USB_unlock(); return MQX_OUT_OF_MEMORY; } _mem_zero(info_ptr, sizeof(IO_USB_MFS_STRUCT)); _mem_set_type(info_ptr, MEM_TYPE_USB_MFS_STRUCT); USB_unlock(); /* Fill in the state structure with the info we know */ info_ptr->LUN = logical_unit; info_ptr->BLENGTH = USB_MFS_DEFAULT_SECTOR_SIZE; info_ptr->ERROR_CODE = IO_OK; info_ptr->BLOCK_MODE = TRUE; info_ptr->COMMAND.CBW_PTR = (CBW_STRUCT_PTR) &info_ptr->CBW; info_ptr->COMMAND.CSW_PTR = (CSW_STRUCT_PTR) &info_ptr->CSW; info_ptr->COMMAND.CALL_PTR = ccs_ptr; info_ptr->COMMAND.LUN = logical_unit; info_ptr->COMMAND.CALLBACK = _io_usb_mfs_callback; info_ptr->COMMAND_STATUS = MQX_OK; _lwsem_create(&info_ptr->LWSEM, 1); _lwsem_create(&info_ptr->COMMAND_DONE, 0); return (_io_dev_install_ext(identifier, (_mqx_int (_CODE_PTR_)(MQX_FILE_PTR, char_ptr, char_ptr))_io_usb_mfs_open, (_mqx_int (_CODE_PTR_)(MQX_FILE_PTR) )_io_usb_mfs_close, (_mqx_int (_CODE_PTR_)(MQX_FILE_PTR, char_ptr, int_32) )_io_usb_mfs_read, (_mqx_int (_CODE_PTR_)(MQX_FILE_PTR, char_ptr, int_32) )_io_usb_mfs_write, (_mqx_int (_CODE_PTR_)(MQX_FILE_PTR, _mqx_uint, pointer) ) _io_usb_mfs_ioctl, _io_usb_mfs_uninstall_internal, (pointer)info_ptr )); } /* Endbody */
void Mouse_Task( uint32_t param ) { USB_STATUS status = USB_OK; TR_INIT_PARAM_STRUCT tr; HID_COMMAND_PTR hid_com; unsigned char *buffer; PIPE_STRUCT_PTR pipe; uint32_t e; _usb_host_handle mouse_host_handle = (_usb_host_handle) param; hid_com = (HID_COMMAND_PTR) _mem_alloc(sizeof(HID_COMMAND)); /* event for USB callback signaling */ _lwevent_create(&USB_Mouse_Event, LWEVENT_AUTO_CLEAR); printf("\nMQX USB HID Mouse Demo\nWaiting for USB Mouse to be attached...\n"); fflush(stdout); /* ** Infinite loop, waiting for events requiring action */ for ( ; ; ) { // Wait for insertion or removal event _lwevent_wait_ticks(&USB_Mouse_Event, USB_EVENT_CTRL, FALSE, 0); switch ( mouse_hid_device.DEV_STATE ) { case USB_DEVICE_IDLE: break; case USB_DEVICE_ATTACHED: printf("\nMouse device attached\n"); fflush(stdout); mouse_hid_device.DEV_STATE = USB_DEVICE_SET_INTERFACE_STARTED; status = _usb_hostdev_select_interface(mouse_hid_device.DEV_HANDLE, mouse_hid_device.INTF_HANDLE, (void *)&mouse_hid_device.CLASS_INTF); if (status != USB_OK) { printf("\nError in _usb_hostdev_select_interface: %x", status); fflush(stdout); _task_block(); } /* Endif */ break; case USB_DEVICE_SET_INTERFACE_STARTED: break; case USB_DEVICE_INTERFACED: pipe = _usb_hostdev_find_pipe_handle(mouse_hid_device.DEV_HANDLE, mouse_hid_device.INTF_HANDLE, USB_INTERRUPT_PIPE, USB_RECV); if (pipe == NULL) { printf("\nError getting interrupt pipe."); fflush(stdout); _task_block(); } _usb_hostdev_get_buffer(mouse_hid_device.DEV_HANDLE, pipe->MAX_PACKET_SIZE, (void **) &buffer); if (buffer == NULL) { printf("\nMemory allocation failed. STATUS: %x", status); fflush(stdout); _task_block(); } printf("Mouse interfaced, setting protocol...\n"); /* now we will set the USB Hid standard boot protocol */ mouse_hid_device.DEV_STATE = USB_DEVICE_SETTING_PROTOCOL; hid_com->CLASS_PTR = (CLASS_CALL_STRUCT_PTR)&mouse_hid_device.CLASS_INTF; hid_com->CALLBACK_FN = usb_host_hid_mouse_ctrl_callback; hid_com->CALLBACK_PARAM = 0; status = usb_class_hid_set_protocol(hid_com, USB_PROTOCOL_HID_MOUSE); if (status != USB_STATUS_TRANSFER_QUEUED) { printf("\nError in usb_class_hid_set_protocol: %x", status); fflush(stdout); } break; case USB_DEVICE_INUSE: printf("Mouse device ready, try to move the mouse\n"); while (1) { /****************************************************************** Initiate a transfer request on the interrupt pipe ******************************************************************/ usb_hostdev_tr_init(&tr, usb_host_hid_mouse_recv_callback, NULL); tr.G.RX_BUFFER = buffer; tr.G.RX_LENGTH = pipe->MAX_PACKET_SIZE; status = _usb_host_recv_data(mouse_host_handle, pipe, &tr); if (status != USB_STATUS_TRANSFER_QUEUED) { printf("\nError in _usb_host_recv_data: %x", status); fflush(stdout); } /* Wait untill we get the data from keyboard. */ _lwevent_wait_ticks(&USB_Mouse_Event, USB_EVENT_CTRL | USB_EVENT_DATA | USB_EVENT_DATA_CORRUPTED, FALSE, 0); e = _lwevent_get_signalled(); if (USB_EVENT_DATA == e) { if(mouse_hid_device.DEV_STATE == USB_DEVICE_INUSE) { process_mouse_buffer((unsigned char *)buffer); } } else if (USB_EVENT_CTRL == e) { /* kick the outer loop again to handle the CTRL event */ _lwevent_set(&USB_Mouse_Event, USB_EVENT_CTRL); break; } } break; case USB_DEVICE_DETACHED: printf("Going to idle state\n"); mouse_hid_device.DEV_STATE = USB_DEVICE_IDLE; break; case USB_DEVICE_OTHER: break; default: printf("Unknown Mouse Device State = %d\n", mouse_hid_device.DEV_STATE); fflush(stdout); break; } /* Endswitch */ } /* Endfor */ } /* Endbody */