diff options
-rw-r--r-- | include/driver/usbh_device_driver.h | 3 | ||||
-rw-r--r-- | src/usbh_core.c | 58 | ||||
-rw-r--r-- | src/usbh_driver_ac_midi.c | 44 | ||||
-rw-r--r-- | src/usbh_driver_gp_xbox.c | 66 | ||||
-rw-r--r-- | src/usbh_driver_hid.c | 81 | ||||
-rw-r--r-- | src/usbh_driver_hub.c | 72 |
6 files changed, 112 insertions, 212 deletions
diff --git a/include/driver/usbh_device_driver.h b/include/driver/usbh_device_driver.h index eca8928..c70715b 100644 --- a/include/driver/usbh_device_driver.h +++ b/include/driver/usbh_device_driver.h @@ -75,6 +75,9 @@ enum USBH_ENUM_STATE { USBH_ENUM_STATE_CONFIGURATION_DT_READ_SETUP, USBH_ENUM_STATE_CONFIGURATION_DT_READ, USBH_ENUM_STATE_CONFIGURATION_DT_READ_COMPLETE, + USBH_ENUM_STATE_SET_CONFIGURATION_SETUP, + USBH_ENUM_STATE_SET_CONFIGURATION_EMPTY_READ, + USBH_ENUM_STATE_SET_CONFIGURATION_COMPLETE, USBH_ENUM_STATE_FIND_DRIVER, }; diff --git a/src/usbh_core.c b/src/usbh_core.c index dbdec0c..7071281 100644 --- a/src/usbh_core.c +++ b/src/usbh_core.c @@ -483,7 +483,7 @@ static void device_enumerate(usbh_device_t *dev, usbh_packet_callback_data_t cb_ (struct usb_config_descriptor *)&usbh_buffer[USB_DT_DEVICE_SIZE]; if (cb_data.transferred_length == cdt->wTotalLength) { LOG_PRINTF("Configuration descriptor read complete. length: %d\n", cdt->wTotalLength); - CONTINUE_WITH(USBH_ENUM_STATE_FIND_DRIVER); + CONTINUE_WITH(USBH_ENUM_STATE_SET_CONFIGURATION_SETUP); } } break; @@ -546,7 +546,7 @@ static void device_enumerate(usbh_device_t *dev, usbh_packet_callback_data_t cb_ struct usb_config_descriptor *cdt = (struct usb_config_descriptor *)&usbh_buffer[USB_DT_DEVICE_SIZE]; LOG_PRINTF("Configuration descriptor read complete. length: %d\n", cdt->wTotalLength); - CONTINUE_WITH(USBH_ENUM_STATE_FIND_DRIVER); + CONTINUE_WITH(USBH_ENUM_STATE_SET_CONFIGURATION_SETUP); } break; @@ -562,6 +562,60 @@ static void device_enumerate(usbh_device_t *dev, usbh_packet_callback_data_t cb_ } break; + case USBH_ENUM_STATE_SET_CONFIGURATION_SETUP: + { + struct usb_config_descriptor *cdt = + (struct usb_config_descriptor *)&usbh_buffer[USB_DT_DEVICE_SIZE]; + + struct usb_setup_data setup_data; + + setup_data.bmRequestType = USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_DEVICE; + setup_data.bRequest = USB_REQ_SET_CONFIGURATION; + setup_data.wValue = cdt->bConfigurationValue; + setup_data.wIndex = 0; + setup_data.wLength = 0; + + dev->state = USBH_ENUM_STATE_SET_CONFIGURATION_EMPTY_READ; + + device_xfer_control_write_setup(&setup_data, sizeof(setup_data), device_enumerate, dev); + } + break; + + case USBH_ENUM_STATE_SET_CONFIGURATION_EMPTY_READ: + { + switch (cb_data.status) { + case USBH_PACKET_CALLBACK_STATUS_OK: + dev->state = USBH_ENUM_STATE_SET_CONFIGURATION_COMPLETE; + device_xfer_control_read(0, 0, device_enumerate, dev); + break; + + case USBH_PACKET_CALLBACK_STATUS_EFATAL: + case USBH_PACKET_CALLBACK_STATUS_EAGAIN: + case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: + device_enumeration_terminate(dev); + ERROR(cb_data.status); + break; + } + } + break; + + case USBH_ENUM_STATE_SET_CONFIGURATION_COMPLETE: + { + switch (cb_data.status) { + case USBH_PACKET_CALLBACK_STATUS_OK: + CONTINUE_WITH(USBH_ENUM_STATE_FIND_DRIVER); + break; + + case USBH_PACKET_CALLBACK_STATUS_EFATAL: + case USBH_PACKET_CALLBACK_STATUS_EAGAIN: + case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: + device_enumeration_terminate(dev); + ERROR(cb_data.status); + break; + } + } + break; + case USBH_ENUM_STATE_FIND_DRIVER: { struct usb_config_descriptor *cdt = diff --git a/src/usbh_driver_ac_midi.c b/src/usbh_driver_ac_midi.c index cea40df..87da03b 100644 --- a/src/usbh_driver_ac_midi.c +++ b/src/usbh_driver_ac_midi.c @@ -206,30 +206,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t status) LOG_PRINTF("\n CAN'T TOUCH THIS... ignoring data\n"); } break; - case 2: - { - LOG_PRINTF("|empty packet read|"); - if (status.status == USBH_PACKET_CALLBACK_STATUS_OK) { - midi->state++; - device_xfer_control_read(0, 0, event, dev); - } - } - break; - case 3: // Configured - { - if (status.status == USBH_PACKET_CALLBACK_STATUS_OK) { - midi->state = 100; - - midi->endpoint_in_toggle = 0; - LOG_PRINTF("\nMIDI CONFIGURED\n"); - // Notify user - if (midi_config->notify_connected) { - midi_config->notify_connected(midi->device_id); - } - } - } - break; default: break; } @@ -265,7 +242,6 @@ static void midi_poll(void *drvdata, uint32_t t_us) (void)drvdata; midi_device_t *midi = drvdata; - usbh_device_t *dev = midi->usbh_device; switch (midi->state) { /// Upon configuration, some controllers send additional error data @@ -276,11 +252,13 @@ static void midi_poll(void *drvdata, uint32_t t_us) midi->state = 101; } break; + case 101: { read_midi_in(drvdata, 102); } break; + case 102: { // if elapsed MIDI initial delay microseconds @@ -289,6 +267,7 @@ static void midi_poll(void *drvdata, uint32_t t_us) } } break; + case 25: { read_midi_in(drvdata, 26); @@ -297,18 +276,15 @@ static void midi_poll(void *drvdata, uint32_t t_us) case 1: { - //~ LOG_PRINTF("CFGVAL: %d\n", dev->config_val); - struct usb_setup_data setup_data; + midi->state = 100; - setup_data.bmRequestType = USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_DEVICE; - setup_data.bRequest = USB_REQ_SET_CONFIGURATION; - setup_data.wValue = midi->buffer[0]; - setup_data.wIndex = 0; - setup_data.wLength = 0; + midi->endpoint_in_toggle = 0; + LOG_PRINTF("\nMIDI CONFIGURED\n"); - midi->state++; - - device_xfer_control_write_setup(&setup_data, sizeof(setup_data), event, dev); + // Notify user + if (midi_config->notify_connected) { + midi_config->notify_connected(midi->device_id); + } } break; } diff --git a/src/usbh_driver_gp_xbox.c b/src/usbh_driver_gp_xbox.c index f52621a..92f61b5 100644 --- a/src/usbh_driver_gp_xbox.c +++ b/src/usbh_driver_gp_xbox.c @@ -30,11 +30,9 @@ enum STATES { STATE_INACTIVE, - STATE_READING_COMPLETE, + STATE_INITIAL, STATE_READING_REQUEST, - STATE_SET_CONFIGURATION_REQUEST, - STATE_SET_CONFIGURATION_EMPTY_READ, - STATE_SET_CONFIGURATION_COMPLETE + STATE_READING_COMPLETE, }; #define GP_XBOX_CORRECT_TRANSFERRED_LENGTH 20 @@ -132,7 +130,7 @@ static bool analyze_descriptor(void *drvdata, void *descriptor) } if (gp_xbox->endpoint_in_address) { - gp_xbox->state_next = STATE_SET_CONFIGURATION_REQUEST; + gp_xbox->state_next = STATE_INITIAL; return true; } } @@ -268,45 +266,6 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) } break; - case STATE_SET_CONFIGURATION_EMPTY_READ: - { - LOG_PRINTF("|empty packet read|"); - switch (cb_data.status) { - case USBH_PACKET_CALLBACK_STATUS_OK: - gp_xbox->state_next = STATE_SET_CONFIGURATION_COMPLETE; - device_xfer_control_read(0, 0, event, dev); - break; - case USBH_PACKET_CALLBACK_STATUS_EFATAL: - case USBH_PACKET_CALLBACK_STATUS_EAGAIN: - case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: - ERROR(cb_data.status); - gp_xbox->state_next = STATE_INACTIVE; - break; - } - } - break; - case STATE_SET_CONFIGURATION_COMPLETE: // Configured - { - switch (cb_data.status) { - case USBH_PACKET_CALLBACK_STATUS_OK: - gp_xbox->state_next = STATE_READING_REQUEST; - gp_xbox->endpoint_in_toggle = 0; - LOG_PRINTF("\ngp_xbox CONFIGURED\n"); - if (gp_xbox_config->notify_connected) { - gp_xbox_config->notify_connected(gp_xbox->device_id); - } - break; - - case USBH_PACKET_CALLBACK_STATUS_EFATAL: - case USBH_PACKET_CALLBACK_STATUS_EAGAIN: - case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: - ERROR(cb_data.status); - gp_xbox->state_next = STATE_INACTIVE; - break; - } - } - break; - case STATE_INACTIVE: { LOG_PRINTF("XBOX inactive"); @@ -360,19 +319,14 @@ static void poll(void *drvdata, uint32_t time_curr_us) } break; - case STATE_SET_CONFIGURATION_REQUEST: + case STATE_INITIAL: { - struct usb_setup_data setup_data; - - setup_data.bmRequestType = USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_DEVICE; - setup_data.bRequest = USB_REQ_SET_CONFIGURATION; - setup_data.wValue = gp_xbox->configuration_value; - setup_data.wIndex = 0; - setup_data.wLength = 0; - - gp_xbox->state_next = STATE_SET_CONFIGURATION_EMPTY_READ; - - device_xfer_control_write_setup(&setup_data, sizeof(setup_data), event, dev); + gp_xbox->state_next = STATE_READING_REQUEST; + gp_xbox->endpoint_in_toggle = 0; + LOG_PRINTF("\ngp_xbox CONFIGURED\n"); + if (gp_xbox_config->notify_connected) { + gp_xbox_config->notify_connected(gp_xbox->device_id); + } } break; diff --git a/src/usbh_driver_hid.c b/src/usbh_driver_hid.c index 21fa262..b9b95d7 100644 --- a/src/usbh_driver_hid.c +++ b/src/usbh_driver_hid.c @@ -37,8 +37,6 @@ enum STATES { STATE_READING_REQUEST, STATE_READING_COMPLETE_AND_CHECK_REPORT, STATE_SET_REPORT_EMPTY_READ, - STATE_SET_CONFIGURATION_REQUEST, - STATE_SET_CONFIGURATION_EMPTY_READ, STATE_GET_REPORT_DESCRIPTOR_READ_SETUP,// configuration is complete at this point. We write request STATE_GET_REPORT_DESCRIPTOR_READ, STATE_GET_REPORT_DESCRIPTOR_READ_COMPLETE,// after the read finishes, we parse that descriptor @@ -204,7 +202,7 @@ static bool analyze_descriptor(void *drvdata, void *descriptor) } if (hid->endpoint_in_address && hid->report0_length) { - hid->state_next = STATE_SET_CONFIGURATION_REQUEST; + hid->state_next = STATE_GET_REPORT_DESCRIPTOR_READ_SETUP; return true; } @@ -294,62 +292,6 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) } break; - case STATE_SET_CONFIGURATION_EMPTY_READ: - { - LOG_PRINTF("|empty packet read|"); - switch (cb_data.status) { - case USBH_PACKET_CALLBACK_STATUS_OK: - hid->state_next = STATE_GET_REPORT_DESCRIPTOR_READ_SETUP; - device_xfer_control_read(0, 0, event, dev); - break; - - case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: - case USBH_PACKET_CALLBACK_STATUS_EFATAL: - case USBH_PACKET_CALLBACK_STATUS_EAGAIN: - ERROR(cb_data.status); - hid->state_next = STATE_INACTIVE; - break; - } - } - break; - - case STATE_GET_REPORT_DESCRIPTOR_READ_SETUP: - { - switch (cb_data.status) { - case USBH_PACKET_CALLBACK_STATUS_OK: - case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: - { - hid->endpoint_in_toggle = 0; - // We support only the first report descriptor with index 0 - - // limit the size of the report descriptor! - if (hid->report0_length > USBH_HID_BUFFER) { - hid->report0_length = USBH_HID_BUFFER; - } - - struct usb_setup_data setup_data; - - setup_data.bmRequestType = USB_REQ_TYPE_IN | USB_REQ_TYPE_INTERFACE; - setup_data.bRequest = USB_REQ_GET_DESCRIPTOR; - setup_data.wValue = USB_DT_REPORT << 8; - setup_data.wIndex = 0; - setup_data.wLength = hid->report0_length; - - hid->state_next = STATE_GET_REPORT_DESCRIPTOR_READ; - device_xfer_control_write_setup(&setup_data, sizeof(setup_data), event, dev); - - } - break; - - case USBH_PACKET_CALLBACK_STATUS_EFATAL: - case USBH_PACKET_CALLBACK_STATUS_EAGAIN: - ERROR(cb_data.status); - hid->state_next = STATE_INACTIVE; - break; - } - } - break; - case STATE_GET_REPORT_DESCRIPTOR_READ: { switch (cb_data.status) { @@ -434,18 +376,25 @@ static void poll(void *drvdata, uint32_t time_curr_us) } break; - case STATE_SET_CONFIGURATION_REQUEST: + case STATE_GET_REPORT_DESCRIPTOR_READ_SETUP: { + hid->endpoint_in_toggle = 0; + // We support only the first report descriptor with index 0 + + // limit the size of the report descriptor! + if (hid->report0_length > USBH_HID_BUFFER) { + hid->report0_length = USBH_HID_BUFFER; + } + struct usb_setup_data setup_data; - setup_data.bmRequestType = USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_DEVICE; - setup_data.bRequest = USB_REQ_SET_CONFIGURATION; - setup_data.wValue = hid->configuration_value; + setup_data.bmRequestType = USB_REQ_TYPE_IN | USB_REQ_TYPE_INTERFACE; + setup_data.bRequest = USB_REQ_GET_DESCRIPTOR; + setup_data.wValue = USB_DT_REPORT << 8; setup_data.wIndex = 0; - setup_data.wLength = 0; - - hid->state_next = STATE_SET_CONFIGURATION_EMPTY_READ; + setup_data.wLength = hid->report0_length; + hid->state_next = STATE_GET_REPORT_DESCRIPTOR_READ; device_xfer_control_write_setup(&setup_data, sizeof(setup_data), event, dev); } break; diff --git a/src/usbh_driver_hub.c b/src/usbh_driver_hub.c index c185b6a..4fbe643 100644 --- a/src/usbh_driver_hub.c +++ b/src/usbh_driver_hub.c @@ -86,13 +86,6 @@ static bool analyze_descriptor(void *drvdata, void *descriptor) hub_device_t *hub = (hub_device_t *)drvdata;
uint8_t desc_type = ((uint8_t *)descriptor)[1];
switch (desc_type) {
- case USB_DT_CONFIGURATION:
- {
- struct usb_config_descriptor *cfg = (struct usb_config_descriptor*)descriptor;
- hub->buffer[0] = cfg->bConfigurationValue;
- }
- break;
-
case USB_DT_ENDPOINT:
{
struct usb_endpoint_descriptor *ep = (struct usb_endpoint_descriptor *)descriptor;
@@ -110,7 +103,6 @@ static bool analyze_descriptor(void *drvdata, void *descriptor) case USB_DT_HUB:
{
struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)descriptor;
- //~ hub->ports_num = desc->head.bNbrPorts;
if ( desc->head.bNbrPorts <= USBH_HUB_MAX_DEVICES) {
hub->ports_num = desc->head.bNbrPorts;
} else {
@@ -231,42 +223,6 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) }
break;
- case 3: // Get HUB Descriptor write
- {
- switch (cb_data.status) {
- case USBH_PACKET_CALLBACK_STATUS_OK:
- if (hub->ports_num) {
- hub->index = 0;
- hub->state = 6;
- LOG_PRINTF("No need to get HUB DESC\n");
- event(dev, cb_data);
- } else {
- hub->endpoint_in_toggle = 0;
-
- struct usb_setup_data setup_data;
- hub->desc_len = hub->device[0]->packet_size_max0;
-
- setup_data.bmRequestType = USB_REQ_TYPE_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_DEVICE;
- setup_data.bRequest = USB_REQ_GET_DESCRIPTOR;
- setup_data.wValue = 0x29<<8;
- setup_data.wIndex = 0;
- setup_data.wLength = hub->desc_len;
-
- hub->state++;
- device_xfer_control_write_setup(&setup_data, sizeof(setup_data), event, dev);
- LOG_PRINTF("DO Need to get HUB DESC\n");
- }
- break;
-
- case USBH_PACKET_CALLBACK_STATUS_EFATAL:
- case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
- case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
- ERROR(cb_data.status);
- break;
- }
- }
- break;
-
case 4: // Get HUB Descriptor read
{
switch (cb_data.status) {
@@ -779,19 +735,27 @@ static void poll(void *drvdata, uint32_t time_curr_us) case 1:
{
- LOG_PRINTF("CFGVAL: %d\n", hub->buffer[0]);
- struct usb_setup_data setup_data;
+ if (hub->ports_num) {
+ hub->index = 0;
+ hub->state = 6;
+ LOG_PRINTF("No need to get HUB DESC\n");
+ event(dev, (usbh_packet_callback_data_t){0, 0});
+ } else {
+ hub->endpoint_in_toggle = 0;
- setup_data.bmRequestType = USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_DEVICE;
- setup_data.bRequest = USB_REQ_SET_CONFIGURATION;
- setup_data.wValue = hub->buffer[0];
- setup_data.wIndex = 0;
- setup_data.wLength = 0;
+ struct usb_setup_data setup_data;
+ hub->desc_len = hub->device[0]->packet_size_max0;
- hub->state = EMPTY_PACKET_READ_STATE;
- hub->state_after_empty_read = 3;
- device_xfer_control_write_setup(&setup_data, sizeof(setup_data), event, dev);
+ setup_data.bmRequestType = USB_REQ_TYPE_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_DEVICE;
+ setup_data.bRequest = USB_REQ_GET_DESCRIPTOR;
+ setup_data.wValue = 0x29 << 8;
+ setup_data.wIndex = 0;
+ setup_data.wLength = hub->desc_len;
+ hub->state = 4;
+ device_xfer_control_write_setup(&setup_data, sizeof(setup_data), event, dev);
+ LOG_PRINTF("DO Need to get HUB DESC\n");
+ }
}
break;
case 100:
|