void imxehci_reset(struct imxehci_softc *sc) { uint32_t reg; int i; struct ehci_softc *hsc = &sc->sc_hsc; #define RESET_TIMEOUT 100 reg = EOREAD4(hsc, EHCI_USBCMD); reg &= ~EHCI_CMD_RS; EOWRITE4(hsc, EHCI_USBCMD, reg); for (i=0; i < RESET_TIMEOUT; ++i) { reg = EOREAD4(hsc, EHCI_USBCMD); if ((reg & EHCI_CMD_RS) == 0) break; usb_delay_ms(&hsc->sc_bus, 1); } EOWRITE4(hsc, EHCI_USBCMD, reg | EHCI_CMD_HCRESET); for (i = 0; i < RESET_TIMEOUT; i++) { reg = EOREAD4(hsc, EHCI_USBCMD); if ((reg & EHCI_CMD_HCRESET) == 0) break; usb_delay_ms(&hsc->sc_bus, 1); } if (i >= RESET_TIMEOUT) { aprint_error_dev(hsc->sc_dev, "reset timeout (%x)\n", reg); } usb_delay_ms(&hsc->sc_bus, 100); }
static int rtek_init_device( rtek_softc_t *softc ) { usb_device_descr_t dev_desc; uint16_t vendor_id, product_id; const int *ptr=ID_TBL; int i; usbdev_t *dev = softc->dev; uint8_t val; /* find out which device is connected */ usb_get_device_descriptor( softc->dev, &dev_desc, 0 ); vendor_id = (dev_desc.idVendorHigh << 8) + dev_desc.idVendorLow; product_id = (dev_desc.idProductHigh << 8) + dev_desc.idProductLow; while( *ptr != -1 ) { if( (vendor_id == ptr[0]) && (product_id == ptr[1]) ) { softc->ven_code = ptr[2]; break; } ptr += 3; } if( *ptr == -1 ) { xprintf( "Unrecognized Realtek USB-Ethernet device\n" ); return -1; } /* Reset the adapter */ rtek_set_reg( dev, R_RTEK_CMD, RTEK_RESET ); for( i = 0; i < 10; ++i ) { rtek_get_reg( dev, R_RTEK_CMD, &val, 1 ); if( !(val & RTEK_RESET) ) break; usb_delay_ms( NULL, 1 ); } /* Autoload the internal registers */ rtek_set_reg( dev, R_RTEK_CMD, RTEK_AUTOLOAD ); for( i = 0; i < 50; ++i ) { rtek_get_reg( dev, R_RTEK_CMD, &val, 1 ); if( !(val & RTEK_AUTOLOAD) ) break; usb_delay_ms( NULL, 1 ); } /* Read the adapter's MAC addr */ rtek_get_mac_addr( dev, softc->mac_addr ); /* display adapter info */ xprintf( "%s USB-Ethernet Adapter (%a)\n", VENDOR_NAMES[softc->ven_code], softc->mac_addr); return 0; }
static void usbhub_reset_device(usbdev_t *dev,int port,usb_port_status_t *portstatus) { console_log("USB: Resetting device on bus %d port %d",dev->ud_bus->ub_num,port); #ifndef _CFE_ fflush(stdout); #endif usbhub_set_port_feature(dev,port,USB_PORT_FEATURE_RESET); usbhub_get_port_status(dev,port,portstatus); for (;;) { usbhub_get_port_status(dev,port,portstatus); if ((GETUSBFIELD((portstatus),wPortStatus) & USB_PORT_STATUS_RESET) == 0) break; usb_delay_ms(dev->ud_bus,250); } usb_delay_ms(dev->ud_bus,250); usbhub_clear_port_feature(dev,port,USB_PORT_FEATURE_C_PORT_RESET); }
static void usbhub_scan_ports(usbdev_t *dev,void *arg) { uint16_t current; uint16_t changed; usbhub_softc_t *softc; int idx; int res; int len; uint8_t *buf; usbdev_t *newdev; usb_driver_t *newdrv; int addr; usb_port_status_t *portstatus; usb_config_descr_t cfgdescr; unsigned int powerondelay; if (!IS_HUB(dev)) return; /* should not happen. */ portstatus = usb_dma_alloc(sizeof(usb_port_status_t)); if (portstatus == NULL) return; /* * We know this is a hub. Get the softc back. */ softc = (usbhub_softc_t *) dev->ud_private; powerondelay = ((unsigned int) softc->uhub_descr.bPowerOnToPowerGood)*2 + 20; /* * Turn on the power to the ports whose power is not yet on. */ for (idx = 0; idx < softc->uhub_nports; idx++) { usbhub_get_port_status(dev,idx+1,portstatus); current = GETUSBFIELD(portstatus,wPortStatus); changed = GETUSBFIELD(portstatus,wPortChange); if (usb_noisy > 1) { printf("BeforePowerup: port %d status %04X changed %04X\n",idx+1,current,changed); } if (!(current & USB_PORT_STATUS_POWER)) { if (usb_noisy > 1) console_log("USB: Powering up bus %d port %d", dev->ud_bus->ub_num,idx+1); usbhub_set_port_feature(dev,idx+1,USB_PORT_FEATURE_POWER); usb_delay_ms(dev->ud_bus,powerondelay); } } /* * Begin exploration at this level. */ for (idx = 0; idx < softc->uhub_nports; idx++) { usbhub_get_port_status(dev,idx+1,portstatus); current = GETUSBFIELD(portstatus,wPortStatus); changed = GETUSBFIELD(portstatus,wPortChange); if (usb_noisy > 0) { printf("USB: Explore: Bus %d Hub %d port %d status %04X changed %04X\n", dev->ud_bus->ub_num, dev->ud_address,idx+1,current,changed); usb_dbg_dumpportstatus(idx+1,portstatus,1); } // if (changed & USB_PORT_STATUS_RESET) { // usbhub_clear_port_feature(dev,idx+1,USB_PORT_FEATURE_C_PORT_RESET); // } if (changed & USB_PORT_STATUS_ENABLED) { usbhub_clear_port_feature(dev,idx+1,USB_PORT_FEATURE_C_PORT_ENABLE); } if (changed & USB_PORT_STATUS_CONNECT) { /* * A device was either connected or disconnected. * Clear the status change first. */ usbhub_clear_port_feature(dev,idx+1,USB_PORT_FEATURE_C_PORT_CONNECTION); if (current & USB_PORT_STATUS_CONNECT) { /* * The device has been CONNECTED. */ console_log("USB: New device connected to bus %d hub %d port %d", dev->ud_bus->ub_num, dev->ud_address,idx+1); /* * Reset the device. Reuse our old port status structure * so we get the latest status. Some devices do not report * lowspeed until they are reset. */ usbhub_reset_device(dev,idx+1,portstatus); current = GETUSBFIELD(portstatus,wPortStatus); changed = GETUSBFIELD(portstatus,wPortChange); /* * Create a device for this port. */ newdev = usb_create_device(dev->ud_bus,(current & USB_PORT_STATUS_LOWSPD) ? 1 : 0); /* * Get the device descriptor. */ res = usb_get_device_descriptor(newdev,&newdev->ud_devdescr,TRUE); if (usb_noisy > 1) usb_dbg_dumpdescriptors(newdev,(uint8_t *) &(newdev->ud_devdescr),8); /* * Set up the max packet size for the control endpoint, * then get the rest of the descriptor. */ usb_set_ep0mps(newdev,newdev->ud_devdescr.bMaxPacketSize0); res = usb_get_device_descriptor(newdev,&newdev->ud_devdescr,FALSE); /* * Obtain a new address and set the address of the * root hub to this address. */ addr = usb_new_address(newdev->ud_bus); res = usb_set_address(newdev,addr); /* * Get the configuration descriptor and all the * associated interface and endpoint descriptors. */ res = usb_get_config_descriptor(newdev,&cfgdescr,0, sizeof(usb_config_descr_t)); if (res != sizeof(usb_config_descr_t)) { printf("[a] usb_get_config_descriptor returns %d\n",res); } len = GETUSBFIELD(&cfgdescr,wTotalLength); buf = usb_dma_alloc(len); res = usb_get_config_descriptor(newdev,(usb_config_descr_t *)buf,0,len); if (res != len) { printf("[b] usb_get_config_descriptor returns %d\n",res); } newdev->ud_cfgdescr = (usb_config_descr_t *) buf; if (usb_noisy > 1) usb_dbg_dumpdescriptors(newdev,buf,len); /* * Point the hub at the devices it owns */ softc->uhub_devices[idx] = newdev; /* * Find the driver for this. It had better be the hub * driver. */ newdrv = usb_find_driver(newdev); /* * Call the attach method. */ if (newdrv) { newdev->ud_drv = newdrv; /* remember driver dispatch in device */ (*(newdrv->udrv_attach))(newdev,newdrv); } } else { /* * The device has been DISCONNECTED. */ console_log("USB: Device disconnected from bus %d hub %d port %d", dev->ud_bus->ub_num, dev->ud_address,idx+1); /* * Recover pointer to device below hub and clear * this pointer. */ newdev = softc->uhub_devices[idx]; /* Get device pointer */ usbhub_markdetached(newdev); /* mark device and all subordinate devices as "removing" */ softc->uhub_devices[idx] = NULL; /* remove device from hub */ /* * Deassign the USB device's address and then * call detach method to free resources. Devices that * do not have drivers will not have any methods. */ if (newdev) { if (newdev->ud_drv) { /* close open pipes, cancel reqs */ usb_destroy_all_pipes(newdev); /* * Try to process the done queue. This will complete any * requests that made it out of the pipes while we were * doing the stuff above. */ usb_poll(newdev->ud_bus); /* Call detach method, clean up device softc */ (*(newdev->ud_drv->udrv_detach))(newdev); } else { if (usb_noisy > 0) { console_log("USB: Detached device on bus %d hub %d port %d " "has no methods", dev->ud_bus->ub_num, dev->ud_address,idx+1); } } if (newdev->ud_cfgdescr) usb_dma_free(newdev->ud_cfgdescr); usb_destroy_device(newdev); } } } } usb_dma_free(portstatus); /* * Queue up a request for the interrupt pipe. This will catch further * changes at this port. */ usbhub_queue_intreq(dev,softc); }
__interrupt void usb_general_interrupt() #endif { #if (USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE) U8 i; U8 save_pipe_nb; #endif // ---------- DEVICE events management ----------------------------------- #if (USB_DEVICE_FEATURE == ENABLED) // - Device start of frame received if (Is_usb_sof() && Is_sof_interrupt_enabled()) { Usb_ack_sof(); Usb_sof_action(); } #ifdef WA_USB_SUSPEND_PERTUBATION // - Device Suspend event (no more USB activity detected) if (Is_usb_suspend() && Is_suspend_interrupt_enabled()) { usb_suspended=TRUE; Usb_ack_wake_up(); // clear wake up to detect next event Usb_send_event(EVT_USB_SUSPEND); Usb_ack_suspend(); Usb_enable_wake_up_interrupt(); Usb_disable_resume_interrupt(); Usb_freeze_clock(); Stop_pll(); Usb_suspend_action(); } // - Wake up event (USB activity detected): Used to resume if (Is_usb_wake_up() && Is_wake_up_interrupt_enabled()) { if(Is_pll_ready()==FALSE) { Pll_start_auto(); Wait_pll_ready(); } Usb_unfreeze_clock(); Usb_ack_wake_up(); if(usb_suspended) { Usb_enable_resume_interrupt(); Usb_enable_reset_interrupt(); while(Is_usb_wake_up()) { Usb_ack_wake_up(); } usb_delay_ms(2); if(Is_usb_sof() || Is_usb_resume() || Is_usb_reset() ) { Usb_disable_wake_up_interrupt(); Usb_wake_up_action(); Usb_send_event(EVT_USB_WAKE_UP); Usb_enable_suspend_interrupt(); Usb_enable_resume_interrupt(); Usb_enable_reset_interrupt(); } else // Workarround to make the USB enter power down mode again (spurious transcient detected on the USB lines) { Usb_ack_wake_up(); // clear wake up to detect next event Usb_send_event(EVT_USB_SUSPEND); Usb_enable_wake_up_interrupt(); Usb_disable_resume_interrupt(); Usb_freeze_clock(); Stop_pll(); Usb_suspend_action(); } } } // - Resume state bus detection if (Is_usb_resume() && Is_resume_interrupt_enabled()) { usb_suspended = FALSE; Usb_disable_wake_up_interrupt(); Usb_ack_resume(); Usb_disable_resume_interrupt(); Usb_resume_action(); Usb_send_event(EVT_USB_RESUME); } #else // - Device Suspend event (no more USB activity detected) if (Is_usb_suspend() && Is_suspend_interrupt_enabled()) { // Remote wake-up handler if ((remote_wakeup_feature == ENABLED) && (usb_configuration_nb != 0)) { Usb_disable_suspend_interrupt(); Usb_ack_wake_up(); Usb_enable_wake_up_interrupt(); Stop_pll(); Usb_freeze_clock(); Usb_suspend_action(); // After that user can execute "Usb_initiate_remote_wake_up()" to initiate a remote wake-up // Note that the suspend interrupt flag SUSPI must still be set to enable upstream resume // So the SUSPE enable bit must be cleared to avoid redundant interrupt // **************** // Please note also that is Vbus is lost during an upstream resume (Host disconnection), // the RMWKUP bit (used to initiate remote wake up and that is normally cleared by hardware when sent) // remains set after the event, so that a good way to handle this feature is : // Usb_unfreeze_clock(); // Usb_initiate_remote_wake_up(); // while (Is_usb_pending_remote_wake_up()) // { // if (Is_usb_vbus_low()) // { // // Emergency action (reset macro, etc.) if Vbus lost during resuming // break; // } // } // Usb_ack_remote_wake_up_start(); // **************** } else { // No remote wake-up supported Usb_ack_wake_up(); // clear wake up to detect next event Usb_send_event(EVT_USB_SUSPEND); Usb_ack_suspend(); // must be executed last (after Usb_suspend_action()) to allow upstream resume Usb_enable_wake_up_interrupt(); Usb_freeze_clock(); Stop_pll(); Usb_suspend_action(); } } // - Wake up event (USB activity detected): Used to resume if (Is_usb_wake_up() && Is_wake_up_interrupt_enabled()) { if(Is_pll_ready()==FALSE) { Pll_start_auto(); Wait_pll_ready(); } Usb_unfreeze_clock(); Usb_ack_wake_up(); Usb_disable_wake_up_interrupt(); Usb_wake_up_action(); Usb_send_event(EVT_USB_WAKE_UP); Usb_enable_suspend_interrupt(); } // - Resume state bus detection if (Is_usb_resume() && Is_resume_interrupt_enabled()) { Usb_disable_wake_up_interrupt(); Usb_ack_resume(); Usb_disable_resume_interrupt(); Usb_resume_action(); Usb_send_event(EVT_USB_RESUME); } #endif // - USB bus reset detection if (Is_usb_reset()&& Is_reset_interrupt_enabled()) { Usb_ack_reset(); usb_init_device(); Usb_reset_action(); Usb_send_event(EVT_USB_RESET); } #endif// End DEVICE FEATURE MODE // ---------- HOST events management ----------------------------------- #if (USB_HOST_FEATURE == ENABLED && USB_DEVICE_FEATURE == ENABLED) // - ID pin change detection if(Is_usb_id_transition()&&Is_usb_id_interrupt_enabled()) { if(Is_usb_id_device()) { g_usb_mode=USB_MODE_DEVICE;} else { g_usb_mode=USB_MODE_HOST;} Usb_ack_id_transition(); if( g_usb_mode != g_old_usb_mode) // Basic Debounce { if(Is_usb_id_device()) // Going to device mode { Usb_send_event(EVT_USB_DEVICE_FUNCTION); } else // Going to host mode { Usb_send_event(EVT_USB_HOST_FUNCTION); } Usb_id_transition_action(); LOG_STR_CODE(log_id_change); #if ( ID_PIN_CHANGE_GENERATE_RESET == ENABLE) // Hot ID transition generates wdt reset wdtdrv_enable(WDTO_16MS); while(1); #endif } } #endif #if (USB_HOST_FEATURE == ENABLED) // - The device has been disconnected if(Is_device_disconnection() && Is_host_device_disconnection_interrupt_enabled()) { host_disable_all_pipe(); Host_ack_device_disconnection(); device_state=DEVICE_DISCONNECTED; Usb_send_event(EVT_HOST_DISCONNECTION); init_usb_tree(); LOG_STR_CODE(log_device_disconnect); Host_device_disconnection_action(); } // - Device connection if(Is_device_connection() && Is_host_device_connection_interrupt_enabled()) { Host_ack_device_connection(); host_disable_all_pipe(); Host_device_connection_action(); } // - Host Start of frame has been sent if (Is_host_sof() && Is_host_sof_interrupt_enabled()) { Host_ack_sof(); Usb_send_event(EVT_HOST_SOF); private_sof_counter++; #if (USB_HUB_SUPPORT==ENABLE) hub_interrupt_sof++; #endif // delay timeout management for interrupt tranfer mode in host mode #if ((USB_HOST_PIPE_INTERRUPT_TRANSFER==ENABLE) && (TIMEOUT_DELAY_ENABLE==ENABLE)) if (private_sof_counter>=250) // Count 1/4 sec { private_sof_counter=0; for(i=0;i<MAX_EP_NB;i++) { if(it_pipe_str[i].enable==ENABLE) { save_pipe_nb=Host_get_selected_pipe(); Host_select_pipe(i); if((++it_pipe_str[i].timeout>TIMEOUT_DELAY) && (Host_get_pipe_type()!=TYPE_INTERRUPT)) { it_pipe_str[i].enable=DISABLE; it_pipe_str[i].status=PIPE_DELAY_TIMEOUT; Host_stop_pipe_interrupt(i); if (is_any_interrupt_pipe_active()==FALSE) // If no more transfer is armed { if (g_sav_int_sof_enable==FALSE) { Host_disable_sof_interrupt(); } } it_pipe_str[i].handle(PIPE_DELAY_TIMEOUT,it_pipe_str[i].nb_byte_processed); } Host_select_pipe(save_pipe_nb); } } } #endif // (USB_HOST_PIPE_INTERRUPT_TRANSFER==ENABLE) && (TIMEOUT_DELAY_ENABLE==ENABLE)) Host_sof_action(); } // - Host Wake-up has been received if (Is_host_hwup() && Is_host_hwup_interrupt_enabled()) { Host_disable_hwup_interrupt(); // Wake up interrupt should be disable host is now wake up ! // CAUTION HWUP can be cleared only when USB clock is active (not frozen)! Pll_start_auto(); // First Restart the PLL for USB operation Wait_pll_ready(); // Get sure pll is lock Usb_unfreeze_clock(); // Enable clock on USB interface Host_ack_hwup(); // Clear HWUP interrupt flag Usb_send_event(EVT_HOST_HWUP); // Send software event Host_hwup_action(); // Map custom action } #endif // End HOST FEATURE MODE }
/* Delay given a device handle. */ void usbd_delay_ms(usbd_device_handle dev, u_int ms) { usb_delay_ms(dev->bus, ms); }