From 3493c1c0878db2b2c367fcdd9e69e4dcc104cad6 Mon Sep 17 00:00:00 2001 From: Amir Hammad Date: Tue, 30 Aug 2016 04:02:27 +0200 Subject: fix bmRequestType field - use of defines Signed-off-by: Amir Hammad --- src/usbh_driver_hub.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src/usbh_driver_hub.c') diff --git a/src/usbh_driver_hub.c b/src/usbh_driver_hub.c index a7448f5..29335a7 100644 --- a/src/usbh_driver_hub.c +++ b/src/usbh_driver_hub.c @@ -179,9 +179,9 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) struct usb_setup_data setup_data; // If regular port event, else hub event if (port) { - setup_data.bmRequestType = 0b10100011; + setup_data.bmRequestType = USB_REQ_TYPE_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE | USB_REQ_TYPE_ENDPOINT; } else { - setup_data.bmRequestType = 0b10100000; + setup_data.bmRequestType = USB_REQ_TYPE_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_DEVICE; } setup_data.bRequest = USB_REQ_GET_STATUS; @@ -246,7 +246,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) struct usb_setup_data setup_data; hub->desc_len = hub->device[0]->packet_size_max0; - setup_data.bmRequestType = 0b10100000; + 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; @@ -297,7 +297,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) struct usb_setup_data setup_data; hub->desc_len = hub_descriptor->head.bDescLength; - setup_data.bmRequestType = 0b10100000; + 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; @@ -361,7 +361,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) struct usb_setup_data setup_data; LOG_PRINTF("[!%d!]",hub->index); - setup_data.bmRequestType = 0b00100011; + setup_data.bmRequestType = USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE | USB_REQ_TYPE_ENDPOINT; setup_data.bRequest = HUB_REQ_SET_FEATURE; setup_data.wValue = HUB_FEATURE_PORT_POWER; setup_data.wIndex = hub->index; @@ -400,7 +400,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) { struct usb_setup_data setup_data; - setup_data.bmRequestType = 0b10100000; + setup_data.bmRequestType = USB_REQ_TYPE_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_DEVICE; setup_data.bRequest = USB_REQ_GET_STATUS; setup_data.wValue = 0; setup_data.wIndex = 0; @@ -445,7 +445,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) { struct usb_setup_data setup_data; - setup_data.bmRequestType = 0b10100011; + setup_data.bmRequestType = USB_REQ_TYPE_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE | USB_REQ_TYPE_ENDPOINT; setup_data.bRequest = USB_REQ_GET_STATUS; setup_data.wValue = 0; setup_data.wIndex = ++hub->index; @@ -564,7 +564,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) // clear feature C_PORT_CONNECTION struct usb_setup_data setup_data; - setup_data.bmRequestType = 0b00100011; + setup_data.bmRequestType = USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE | USB_REQ_TYPE_ENDPOINT; setup_data.bRequest = HUB_REQ_CLEAR_FEATURE; setup_data.wValue = HUB_FEATURE_C_PORT_CONNECTION; setup_data.wIndex = port; @@ -580,7 +580,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) // Reset processing is complete, enumerate device struct usb_setup_data setup_data; - setup_data.bmRequestType = 0b00100011; + setup_data.bmRequestType = USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE | USB_REQ_TYPE_ENDPOINT; setup_data.bRequest = HUB_REQ_CLEAR_FEATURE; setup_data.wValue = HUB_FEATURE_C_PORT_RESET; setup_data.wIndex = port; @@ -622,7 +622,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) if ((stc) & (1<buffer[0]); struct usb_setup_data setup_data; - setup_data.bmRequestType = 0b00000000; + 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; -- cgit From 4aa69b4eaf44757aa71997e4a59f0889ea6e23f4 Mon Sep 17 00:00:00 2001 From: Amir Hammad Date: Thu, 1 Sep 2016 14:16:17 +0200 Subject: make usbh_packet->data of union type out: const void * in: void * Signed-off-by: Amir Hammad --- src/usbh_driver_hub.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/usbh_driver_hub.c') diff --git a/src/usbh_driver_hub.c b/src/usbh_driver_hub.c index 29335a7..1beed01 100644 --- a/src/usbh_driver_hub.c +++ b/src/usbh_driver_hub.c @@ -738,7 +738,7 @@ static void read_ep1(void *drvdata) usbh_packet_t packet; packet.address = hub->device[0]->address; - packet.data = hub->buffer; + packet.data.in = hub->buffer; packet.datalen = hub->endpoint_in_maxpacketsize; packet.endpoint_address = hub->endpoint_in_address; packet.endpoint_size_max = hub->endpoint_in_maxpacketsize; -- cgit From a75535e52b9500a82cd0e9aebe6a987b68390895 Mon Sep 17 00:00:00 2001 From: Amir Hammad Date: Mon, 5 Sep 2016 20:10:17 +0200 Subject: core: Simplify device removes Signed-off-by: Amir Hammad --- src/usbh_driver_hub.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) (limited to 'src/usbh_driver_hub.c') diff --git a/src/usbh_driver_hub.c b/src/usbh_driver_hub.c index 1beed01..c185b6a 100644 --- a/src/usbh_driver_hub.c +++ b/src/usbh_driver_hub.c @@ -835,23 +835,11 @@ static void remove(void *drvdata) hub_device_t *hub = (hub_device_t *)drvdata; uint8_t i; - // Call fast... to avoid polling hub->state = 0; hub->endpoint_in_address = 0; hub->busy = 0; - for (i = 1; i < USBH_HUB_MAX_DEVICES + 1; i++) { - if (hub->device[i]) { - if (hub->device[i]->drv && hub->device[i]->drvdata) { - if (hub->device[i]->drv->remove != remove) { - LOG_PRINTF("\t\t\t\tHUB REMOVE %d\n",hub->device[i]->address); - hub->device[i]->drv->remove(hub->device[i]->drvdata); - } - } - hub->device[i] = 0; - } - hub->device[0]->drv = 0; - hub->device[0]->drvdata = 0; - hub->device[0] = 0; + for (i = 0; i < USBH_HUB_MAX_DEVICES + 1; i++) { + hub->device[i] = 0; } } -- cgit From 58fec7a17ceb6814d64e702e7829556c3ece01e4 Mon Sep 17 00:00:00 2001 From: Amir Hammad Date: Mon, 5 Sep 2016 19:48:56 +0200 Subject: Drop need for the set configuration request in dev drivers Signed-off-by: Amir Hammad --- src/usbh_driver_hub.c | 72 +++++++++++++-------------------------------------- 1 file changed, 18 insertions(+), 54 deletions(-) (limited to 'src/usbh_driver_hub.c') 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: -- cgit From 6d9ee8e9a63208e83adddfe1eeccf7461297dc1b Mon Sep 17 00:00:00 2001 From: Amir Hammad Date: Mon, 5 Sep 2016 20:16:30 +0200 Subject: add DISABLE_LOW_LEVEL ifdef in hub device driver Signed-off-by: Amir Hammad --- src/usbh_driver_hub.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/usbh_driver_hub.c') diff --git a/src/usbh_driver_hub.c b/src/usbh_driver_hub.c index 4fbe643..b1b37e5 100644 --- a/src/usbh_driver_hub.c +++ b/src/usbh_driver_hub.c @@ -638,6 +638,8 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) } if ((sts & (1<<(HUB_FEATURE_PORT_LOWSPEED))) && !(sts & (1<<(HUB_FEATURE_PORT_HIGHSPEED)))) { +#define DISABLE_LOW_SPEED +#ifdef DISABLE_LOW_SPEED LOG_PRINTF("Low speed device"); // Disable Low speed device immediately @@ -655,6 +657,12 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) hub->current_port = CURRENT_PORT_NONE; device_xfer_control_write_setup(&setup_data, sizeof(setup_data), event, dev); +#else + hub->device[port]->speed = USBH_SPEED_LOW; + LOG_PRINTF("Low speed device"); + hub->timestamp_us = hub->time_curr_us; + hub->state = 100; // schedule wait for 500ms +#endif } else if (!(sts & (1<<(HUB_FEATURE_PORT_LOWSPEED))) && !(sts & (1<<(HUB_FEATURE_PORT_HIGHSPEED)))) { hub->device[port]->speed = USBH_SPEED_FULL; -- cgit From fe54644237156b7b19ca8a9cfd699c0f4e6a0c9a Mon Sep 17 00:00:00 2001 From: Amir Hammad Date: Tue, 6 Sep 2016 09:53:20 +0200 Subject: HUB: make use of control wrappers + use enums Until now, hardcoded numbers were used as states. Signed-off-by: Amir Hammad --- src/usbh_driver_hub.c | 285 +++++++++++++------------------------------------- 1 file changed, 70 insertions(+), 215 deletions(-) (limited to 'src/usbh_driver_hub.c') diff --git a/src/usbh_driver_hub.c b/src/usbh_driver_hub.c index b1b37e5..87f8578 100644 --- a/src/usbh_driver_hub.c +++ b/src/usbh_driver_hub.c @@ -27,7 +27,6 @@ #include - static hub_device_t hub_device[USBH_MAX_HUBS]; static bool initialized = false; @@ -41,7 +40,7 @@ void hub_driver_init(void) for (i = 0; i < USBH_MAX_HUBS; i++) { hub_device[i].device[0] = 0; hub_device[i].ports_num = 0; - hub_device[i].current_port = -1; + hub_device[i].current_port = CURRENT_PORT_NONE; } } @@ -60,15 +59,14 @@ static void *init(void *usbh_dev) break; } } - LOG_PRINTF("%{%d}",i); - LOG_FLUSH(); + LOG_PRINTF("{%d}",i); if (i == USBH_MAX_HUBS) { - LOG_PRINTF("ERRRRRRR"); + LOG_PRINTF("Unable to initialize hub driver"); return 0; } drvdata = &hub_device[i]; - drvdata->state = 0; + drvdata->state = EVENT_STATE_NONE; drvdata->ports_num = 0; drvdata->device[0] = (usbh_device_t *)usbh_dev; drvdata->busy = 0; @@ -119,7 +117,7 @@ static bool analyze_descriptor(void *drvdata, void *descriptor) } if (hub->endpoint_in_address) { - hub->state = 1; + hub->state = EVENT_STATE_INITIAL; LOG_PRINTF("end enum"); return true; } @@ -133,7 +131,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) LOG_PRINTF("\nHUB->STATE = %d\n", hub->state); switch (hub->state) { - case 26: + case EVENT_STATE_POLL: switch (cb_data.status) { case USBH_PACKET_CALLBACK_STATUS_OK: { @@ -149,7 +147,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) // Driver error... port not found if (!psc) { // Continue reading status change endpoint - hub->state = 25; + hub->state = EVENT_STATE_POLL_REQ; break; } @@ -164,7 +162,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) if (hub->current_port >= 1) { if (hub->current_port != port) { LOG_PRINTF("X"); - hub->state = 25; + hub->state = EVENT_STATE_POLL_REQ; break; } } @@ -180,67 +178,30 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) setup_data.wValue = 0; setup_data.wIndex = port; setup_data.wLength = 4; - hub->state = 31; + hub->state = EVENT_STATE_GET_STATUS_COMPLETE; hub->current_port = port; LOG_PRINTF("\n\nPORT FOUND: %d\n", port); - device_xfer_control_write_setup(&setup_data, sizeof(setup_data), event, dev); + device_control(dev, event, &setup_data, &hub->hub_and_port_status[port]); } break; case USBH_PACKET_CALLBACK_STATUS_EFATAL: case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: ERROR(cb_data.status); - hub->state = 0; + hub->state = EVENT_STATE_NONE; break; case USBH_PACKET_CALLBACK_STATUS_EAGAIN: // In case of EAGAIN error, retry read on status endpoint - hub->state = 25; + hub->state = EVENT_STATE_POLL_REQ; LOG_PRINTF("HUB: Retrying...\n"); break; } break; - case EMPTY_PACKET_READ_STATE: - { - LOG_PRINTF("|empty packet read|"); - switch (cb_data.status) { - case USBH_PACKET_CALLBACK_STATUS_OK: - device_xfer_control_read(0, 0, event, dev); - hub->state = hub->state_after_empty_read; - hub->state_after_empty_read = 0; - break; - - case USBH_PACKET_CALLBACK_STATUS_EFATAL: - case USBH_PACKET_CALLBACK_STATUS_EAGAIN: - case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: - hub->state = hub->state_after_empty_read; - event(dev, cb_data); - break; - } - } - break; - - case 4: // Get HUB Descriptor read - { - switch (cb_data.status) { - case USBH_PACKET_CALLBACK_STATUS_OK: - hub->state++; - device_xfer_control_read(hub->buffer, hub->desc_len, event, dev); // "error dynamic size" - bad comment, investigate - 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 5:// Hub descriptor found + case EVENT_STATE_READ_HUB_DESCRIPTOR_COMPLETE:// Hub descriptor found { switch (cb_data.status) { case USBH_PACKET_CALLBACK_STATUS_OK: @@ -259,13 +220,13 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) 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); + hub->state = EVENT_STATE_READ_HUB_DESCRIPTOR_COMPLETE; + device_control(dev, event, &setup_data, hub->buffer); break; } else if (hub_descriptor->head.bDescLength == hub->desc_len) { hub->ports_num = hub_descriptor->head.bNbrPorts; - hub->state++; + hub->state = EVENT_STATE_ENABLE_PORTS; hub->index = 0; cb_data.status = USBH_PACKET_CALLBACK_STATUS_OK; event(dev, cb_data); @@ -290,7 +251,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) LOG_PRINTF("INCREASE NUMBER OF ENABLED PORTS\n"); hub->ports_num = USBH_HUB_MAX_DEVICES; } - hub->state++; + hub->state = EVENT_STATE_ENABLE_PORTS; hub->index = 0; cb_data.status = USBH_PACKET_CALLBACK_STATUS_OK; @@ -308,7 +269,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) } break; - case 6:// enable ports + case EVENT_STATE_ENABLE_PORTS:// enable ports { switch (cb_data.status) { case USBH_PACKET_CALLBACK_STATUS_OK: @@ -323,37 +284,15 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) setup_data.wIndex = hub->index; setup_data.wLength = 0; - hub->state_after_empty_read = hub->state; - hub->state = EMPTY_PACKET_READ_STATE; - - device_xfer_control_write_setup(&setup_data, sizeof(setup_data), event, dev); + device_control(dev, event, &setup_data, 0); } else { - hub->state++; // TODO: // Delay Based on hub descriptor field bPwr2PwrGood // delay_ms_busy_loop(200); LOG_PRINTF("\nHUB CONFIGURED & PORTS POWERED\n"); - cb_data.status = USBH_PACKET_CALLBACK_STATUS_OK; - event(dev, cb_data); - } - 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 7: - { - switch (cb_data.status) { - case USBH_PACKET_CALLBACK_STATUS_OK: - { + // get device status struct usb_setup_data setup_data; setup_data.bmRequestType = USB_REQ_TYPE_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_DEVICE; @@ -362,8 +301,9 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) setup_data.wIndex = 0; setup_data.wLength = 4; - hub->state++; - device_xfer_control_write_setup(&setup_data, sizeof(setup_data), event, dev); + hub->state = EVENT_STATE_GET_PORT_STATUS; + hub->index = 0; + device_control(dev, event, &setup_data, hub->buffer); } break; @@ -373,85 +313,32 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) ERROR(cb_data.status); break; } - - } - break; - case 8: - { - switch (cb_data.status) { - case USBH_PACKET_CALLBACK_STATUS_OK: - device_xfer_control_read(hub->buffer, 4, event, dev); - hub->index = 0; - hub->state++; - 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 9: + case EVENT_STATE_GET_PORT_STATUS: { switch (cb_data.status) { case USBH_PACKET_CALLBACK_STATUS_OK: { - struct usb_setup_data setup_data; - - setup_data.bmRequestType = USB_REQ_TYPE_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE | USB_REQ_TYPE_ENDPOINT; - setup_data.bRequest = USB_REQ_GET_STATUS; - setup_data.wValue = 0; - setup_data.wIndex = ++hub->index; - setup_data.wLength = 4; + if (hub->index < hub->ports_num) { + //TODO: process data contained in hub->buffer - hub->state++; - - 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: - case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: - ERROR(cb_data.status); - break; - } - } - break; + struct usb_setup_data setup_data; - case 10: - { - switch (cb_data.status) { - case USBH_PACKET_CALLBACK_STATUS_OK: - device_xfer_control_read(hub->buffer, 4, event, dev); - hub->state++; - break; + setup_data.bmRequestType = USB_REQ_TYPE_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE | USB_REQ_TYPE_ENDPOINT; + setup_data.bRequest = USB_REQ_GET_STATUS; + setup_data.wValue = 0; + setup_data.wIndex = ++hub->index; + setup_data.wLength = 4; - case USBH_PACKET_CALLBACK_STATUS_EFATAL: - case USBH_PACKET_CALLBACK_STATUS_EAGAIN: - case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: - ERROR(cb_data.status); - break; - } - } - break; + hub->state = EVENT_STATE_GET_PORT_STATUS; + device_control(dev, event, &setup_data, hub->buffer); + } else { + hub->busy = 0; + hub->state = EVENT_STATE_POLL_REQ; + } - case 11: - { - switch (cb_data.status) { - case USBH_PACKET_CALLBACK_STATUS_OK: - if (hub->index < hub->ports_num) { - hub->state = 9; - // process data contained in hub->buffer - // TODO: - cb_data.status = USBH_PACKET_CALLBACK_STATUS_OK; - event(dev, cb_data); - } else { - hub->busy = 0; - hub->state = 25; } break; @@ -464,35 +351,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) } break; - case 31: // Read port status - { - switch (cb_data.status) { - case USBH_PACKET_CALLBACK_STATUS_OK: - { - int8_t port = hub->current_port; - hub->state++; - - // TODO: rework to endianess aware, - // (maybe whole library is affected by this) - // Detail: - // Isn't universal. Here is endianess ok, - // but on another architecture may be incorrect - device_xfer_control_read(&hub->hub_and_port_status[port], 4, 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); - // continue - hub->state = 25; - break; - } - - } - break; - case 32: + case EVENT_STATE_GET_STATUS_COMPLETE: { switch (cb_data.status) { case USBH_PACKET_CALLBACK_STATUS_OK: @@ -512,7 +371,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) if (!hub->device[port]) { if (!usbh_enum_available() || hub->busy) { LOG_PRINTF("\n\t\t\tCannot enumerate %d %d\n", !usbh_enum_available(), hub->busy); - hub->state = 25; + hub->state = EVENT_STATE_POLL_REQ; break; } } @@ -526,10 +385,8 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) setup_data.wIndex = port; setup_data.wLength = 0; - hub->state_after_empty_read = 33; - hub->state = EMPTY_PACKET_READ_STATE; - - device_xfer_control_write_setup(&setup_data, sizeof(setup_data), event, dev); + hub->state = EVENT_STATE_PORT_RESET_REQ; + device_control(dev, event, &setup_data, 0); } else if(stc & (1<state_after_empty_read = 35; - hub->state = EMPTY_PACKET_READ_STATE; + hub->state = EVENT_STATE_PORT_RESET_COMPLETE; LOG_PRINTF("RESET"); - device_xfer_control_write_setup(&setup_data, sizeof(setup_data), event, dev); + device_control(dev, event, &setup_data, 0); } else { LOG_PRINTF("another STC %d\n", stc); } } else { - hub->state = 25; + hub->state = EVENT_STATE_POLL_REQ; LOG_PRINTF("HUB status change\n"); } } @@ -562,12 +418,12 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: ERROR(cb_data.status); // continue - hub->state = 25; + hub->state = EVENT_STATE_POLL_REQ; break; } } break; - case 33: + case EVENT_STATE_PORT_RESET_REQ: { switch (cb_data.status) { case USBH_PACKET_CALLBACK_STATUS_OK: @@ -584,13 +440,12 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) setup_data.wIndex = port; setup_data.wLength = 0; - hub->state_after_empty_read = 11; - hub->state = EMPTY_PACKET_READ_STATE; + hub->state = EVENT_STATE_GET_PORT_STATUS; LOG_PRINTF("CONN"); hub->busy = 1; - device_xfer_control_write_setup(&setup_data, sizeof(setup_data), event, dev); + device_control(dev, event, &setup_data, 0); } } else { LOG_PRINTF("\t\t\t\tDISCONNECT EVENT\n"); @@ -603,7 +458,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) hub->device[port]->drvdata = 0; hub->device[port] = 0; hub->current_port = CURRENT_PORT_NONE; - hub->state = 25; + hub->state = EVENT_STATE_POLL_REQ; hub->busy = 0; } } @@ -614,12 +469,12 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: ERROR(cb_data.status); // continue - hub->state = 25; + hub->state = EVENT_STATE_POLL_REQ; break; } } break; - case 35: // RESET COMPLETE, start enumeration + case EVENT_STATE_PORT_RESET_COMPLETE: // RESET COMPLETE, start enumeration { switch (cb_data.status) { case USBH_PACKET_CALLBACK_STATUS_OK: @@ -652,29 +507,29 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) setup_data.wLength = 0; // After write process another devices, poll for events - hub->state_after_empty_read = 11;//Expecting all ports are powered (constant/non-changeable after init) - hub->state = EMPTY_PACKET_READ_STATE; + //Expecting all ports are powered (constant/non-changeable after init) + hub->state = EVENT_STATE_GET_PORT_STATUS; hub->current_port = CURRENT_PORT_NONE; - device_xfer_control_write_setup(&setup_data, sizeof(setup_data), event, dev); + device_control(dev, event, &setup_data, 0); #else hub->device[port]->speed = USBH_SPEED_LOW; LOG_PRINTF("Low speed device"); hub->timestamp_us = hub->time_curr_us; - hub->state = 100; // schedule wait for 500ms + hub->state = EVENT_STATE_SLEEP_500_MS; // schedule wait for 500ms #endif } else if (!(sts & (1<<(HUB_FEATURE_PORT_LOWSPEED))) && !(sts & (1<<(HUB_FEATURE_PORT_HIGHSPEED)))) { hub->device[port]->speed = USBH_SPEED_FULL; LOG_PRINTF("Full speed device"); hub->timestamp_us = hub->time_curr_us; - hub->state = 100; // schedule wait for 500ms + hub->state = EVENT_STATE_SLEEP_500_MS; // schedule wait for 500ms } } else { LOG_PRINTF("%s:%d Do not know what to do, when device is disabled after reset\n", __FILE__, __LINE__); - hub->state = 25; + hub->state = EVENT_STATE_POLL_REQ; return; } } @@ -685,7 +540,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: ERROR(cb_data.status); // continue - hub->state = 25; + hub->state = EVENT_STATE_POLL_REQ; break; } } @@ -712,7 +567,7 @@ static void read_ep1(void *drvdata) packet.callback_arg = hub->device[0]; packet.toggle = &hub->endpoint_in_toggle; - hub->state = 26; + hub->state = EVENT_STATE_POLL; usbh_read(hub->device[0], &packet); LOG_PRINTF("@hub %d/EP1 | \n", hub->device[0]->address); @@ -731,7 +586,7 @@ static void poll(void *drvdata, uint32_t time_curr_us) hub->time_curr_us = time_curr_us; switch (hub->state) { - case 25: + case EVENT_STATE_POLL_REQ: { if (usbh_enum_available()) { read_ep1(hub); @@ -741,11 +596,11 @@ static void poll(void *drvdata, uint32_t time_curr_us) } break; - case 1: + case EVENT_STATE_INITIAL: { if (hub->ports_num) { hub->index = 0; - hub->state = 6; + hub->state = EVENT_STATE_ENABLE_PORTS; LOG_PRINTF("No need to get HUB DESC\n"); event(dev, (usbh_packet_callback_data_t){0, 0}); } else { @@ -760,17 +615,17 @@ static void poll(void *drvdata, uint32_t time_curr_us) 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); + hub->state = EVENT_STATE_READ_HUB_DESCRIPTOR_COMPLETE; + device_control(dev, event, &setup_data, hub->buffer); LOG_PRINTF("DO Need to get HUB DESC\n"); } } break; - case 100: + case EVENT_STATE_SLEEP_500_MS: if (hub->time_curr_us - hub->timestamp_us > 500000) { int8_t port = hub->current_port; - LOG_PRINTF("PORT: %d", port); - LOG_PRINTF("\nNEW device at address: %d\n", hub->device[port]->address); + LOG_PRINTF("PORT: %d\n", port); + LOG_PRINTF("NEW device at address: %d\n", hub->device[port]->address); hub->device[port]->lld = hub->device[0]->lld; device_enumeration_start(hub->device[port]); @@ -784,7 +639,7 @@ static void poll(void *drvdata, uint32_t time_curr_us) // Only one device on bus can have address==0 hub->busy = 0; - hub->state = 25; + hub->state = EVENT_STATE_POLL_REQ; } break; default: @@ -807,7 +662,7 @@ static void remove(void *drvdata) hub_device_t *hub = (hub_device_t *)drvdata; uint8_t i; - hub->state = 0; + hub->state = EVENT_STATE_NONE; hub->endpoint_in_address = 0; hub->busy = 0; for (i = 0; i < USBH_HUB_MAX_DEVICES + 1; i++) { -- cgit From b895498df9be02eba1c285c5cdc58eb088bb86fc Mon Sep 17 00:00:00 2001 From: Amir Hammad Date: Tue, 6 Sep 2016 08:40:32 +0200 Subject: hub: use common code to remove device Signed-off-by: Amir Hammad --- src/usbh_driver_hub.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'src/usbh_driver_hub.c') diff --git a/src/usbh_driver_hub.c b/src/usbh_driver_hub.c index 87f8578..7dc99ea 100644 --- a/src/usbh_driver_hub.c +++ b/src/usbh_driver_hub.c @@ -449,13 +449,8 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) } } else { LOG_PRINTF("\t\t\t\tDISCONNECT EVENT\n"); - if (hub->device[port]->drv && hub->device[port]->drvdata) { - hub->device[port]->drv->remove(hub->device[port]->drvdata); - } - hub->device[port]->address = -1; + device_remove(hub->device[port]); - hub->device[port]->drv = 0; - hub->device[port]->drvdata = 0; hub->device[port] = 0; hub->current_port = CURRENT_PORT_NONE; hub->state = EVENT_STATE_POLL_REQ; -- cgit From 1d1b697bc6b13b5db1594c3dafe5452b6b2c1668 Mon Sep 17 00:00:00 2001 From: Amir Hammad Date: Thu, 8 Sep 2016 06:27:35 +0200 Subject: use NULL instead of 0 for assigning null pointer Signed-off-by: Amir Hammad --- src/usbh_driver_hub.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/usbh_driver_hub.c') diff --git a/src/usbh_driver_hub.c b/src/usbh_driver_hub.c index 7dc99ea..2127158 100644 --- a/src/usbh_driver_hub.c +++ b/src/usbh_driver_hub.c @@ -25,6 +25,7 @@ #include "usart_helpers.h" #include "usbh_config.h" +#include #include static hub_device_t hub_device[USBH_MAX_HUBS]; @@ -52,7 +53,7 @@ static void *init(void *usbh_dev) } uint32_t i; - hub_device_t *drvdata = 0; + hub_device_t *drvdata = NULL; // find free data space for hub device for (i = 0; i < USBH_MAX_HUBS; i++) { if (hub_device[i].device[0] == 0) { -- cgit From decb2d817d806e8ed6e77cf215e4adfef4767a50 Mon Sep 17 00:00:00 2001 From: Amir Hammad Date: Fri, 9 Sep 2016 18:25:28 +0200 Subject: use forward declaration for usbh_dev_driver_t Signed-off-by: Amir Hammad --- src/usbh_driver_hub.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/usbh_driver_hub.c') diff --git a/src/usbh_driver_hub.c b/src/usbh_driver_hub.c index 2127158..959a6c2 100644 --- a/src/usbh_driver_hub.c +++ b/src/usbh_driver_hub.c @@ -45,7 +45,7 @@ void hub_driver_init(void) } } -static void *init(void *usbh_dev) +static void *init(usbh_device_t *usbh_dev) { if (!initialized) { LOG_PRINTF("\n%s/%d : driver not initialized\n", __FILE__, __LINE__); @@ -69,7 +69,7 @@ static void *init(void *usbh_dev) drvdata = &hub_device[i]; drvdata->state = EVENT_STATE_NONE; drvdata->ports_num = 0; - drvdata->device[0] = (usbh_device_t *)usbh_dev; + drvdata->device[0] = usbh_dev; drvdata->busy = 0; drvdata->endpoint_in_address = 0; drvdata->endpoint_in_maxpacketsize = 0; -- cgit From 4415d960c3d1e52573db02be4aee6e22d22809cf Mon Sep 17 00:00:00 2001 From: Amir Hammad Date: Sun, 11 Sep 2016 12:03:20 +0200 Subject: use default case instead of handling other packet statuses explicitely Signed-off-by: Amir Hammad --- src/usbh_driver_hub.c | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) (limited to 'src/usbh_driver_hub.c') diff --git a/src/usbh_driver_hub.c b/src/usbh_driver_hub.c index 959a6c2..3f51c68 100644 --- a/src/usbh_driver_hub.c +++ b/src/usbh_driver_hub.c @@ -187,8 +187,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) } break; - case USBH_PACKET_CALLBACK_STATUS_EFATAL: - case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: + default: ERROR(cb_data.status); hub->state = EVENT_STATE_NONE; break; @@ -262,8 +261,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) } break; - case USBH_PACKET_CALLBACK_STATUS_EFATAL: - case USBH_PACKET_CALLBACK_STATUS_EAGAIN: + default: ERROR(cb_data.status); break; } @@ -308,9 +306,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) } break; - case USBH_PACKET_CALLBACK_STATUS_EFATAL: - case USBH_PACKET_CALLBACK_STATUS_EAGAIN: - case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: + default: ERROR(cb_data.status); break; } @@ -343,9 +339,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) } break; - case USBH_PACKET_CALLBACK_STATUS_EFATAL: - case USBH_PACKET_CALLBACK_STATUS_EAGAIN: - case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: + default: ERROR(cb_data.status); break; } @@ -414,9 +408,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) } break; - case USBH_PACKET_CALLBACK_STATUS_EFATAL: - case USBH_PACKET_CALLBACK_STATUS_EAGAIN: - case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: + default: ERROR(cb_data.status); // continue hub->state = EVENT_STATE_POLL_REQ; @@ -460,9 +452,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) } break; - case USBH_PACKET_CALLBACK_STATUS_EFATAL: - case USBH_PACKET_CALLBACK_STATUS_EAGAIN: - case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: + default: ERROR(cb_data.status); // continue hub->state = EVENT_STATE_POLL_REQ; @@ -531,9 +521,7 @@ static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data) } break; - case USBH_PACKET_CALLBACK_STATUS_EFATAL: - case USBH_PACKET_CALLBACK_STATUS_EAGAIN: - case USBH_PACKET_CALLBACK_STATUS_ERRSIZ: + default: ERROR(cb_data.status); // continue hub->state = EVENT_STATE_POLL_REQ; -- cgit