void _USBStandardHub (TUSBStandardHub *pThis)
{
	assert (pThis != 0);

	for (unsigned nPort = 0; nPort < pThis->m_nPorts; nPort++)
	{
		if (pThis->m_pStatus[nPort] != 0)
		{
			free (pThis->m_pStatus[nPort]);
			pThis->m_pStatus[nPort] = 0;
		}

		if (pThis->m_pDevice[nPort] != 0)
		{
			_USBDevice (pThis->m_pDevice[nPort]);
			free (pThis->m_pDevice[nPort]);
			pThis->m_pDevice[nPort] = 0;
		}
	}

	pThis->m_nPorts = 0;

	if (pThis->m_pHubDesc != 0)
	{
		free (pThis->m_pHubDesc);
		pThis->m_pHubDesc = 0;
	}

	_USBDevice (&pThis->m_USBDevice);
}
Exemple #2
0
void _SMSC951xDevice (TSMSC951xDevice *pThis)
{
	assert (pThis != 0);

	if (pThis->m_pTxBuffer != 0)
	{
		free (pThis->m_pTxBuffer);
		pThis->m_pTxBuffer = 0;
	}
	
	if (pThis->m_pEndpointBulkOut != 0)
	{
		_USBEndpoint (pThis->m_pEndpointBulkOut);
		free (pThis->m_pEndpointBulkOut);
		pThis->m_pEndpointBulkOut = 0;
	}
	
	if (pThis->m_pEndpointBulkIn != 0)
	{
		_USBEndpoint (pThis->m_pEndpointBulkIn);
		free (pThis->m_pEndpointBulkIn);
		pThis->m_pEndpointBulkIn = 0;
	}
	
	_USBDevice (&pThis->m_USBDevice);
}
void _CUSBKeyboardDevice (TUSBKeyboardDevice *pThis)
{
	assert (pThis != 0);

	if (pThis->m_pReportBuffer != 0)
	{
		free (pThis->m_pReportBuffer);
		pThis->m_pReportBuffer = 0;
	}

	if (pThis->m_pReportEndpoint != 0)
	{
		_USBEndpoint (pThis->m_pReportEndpoint);
		free (pThis->m_pReportEndpoint);
		pThis->m_pReportEndpoint = 0;
	}

	_KeyMap (&pThis->m_KeyMap);
	_USBDevice (&pThis->m_USBDevice);
}
boolean USBStandardHubEnumeratePorts (TUSBStandardHub *pThis)
{
	assert (pThis != 0);

	TUSBHostController *pHost = USBDeviceGetHost (&pThis->m_USBDevice);
	assert (pHost != 0);

	TUSBEndpoint *pEndpoint0 = USBDeviceGetEndpoint0 (&pThis->m_USBDevice);
	assert (pEndpoint0 != 0);

	assert (pThis->m_nPorts > 0);

	// first power on all ports
	for (unsigned nPort = 0; nPort < pThis->m_nPorts; nPort++)
	{
		if (DWHCIDeviceControlMessage (pHost, pEndpoint0,
			REQUEST_OUT | REQUEST_CLASS | REQUEST_TO_OTHER,
			SET_FEATURE, PORT_POWER, nPort+1, 0, 0) < 0)
		{
			LogWrite (FromHub, LOG_ERROR, "Cannot power port %u", nPort+1);

			return FALSE;
		}
	}

	// pThis->m_pHubDesc->bPwrOn2PwrGood delay seems to be not enough
	// for some low speed devices, so we use the maximum here
	MsDelay (510);

	// now detect devices, reset and initialize them
	for (unsigned nPort = 0; nPort < pThis->m_nPorts; nPort++)
	{
		assert (pThis->m_pStatus[nPort] == 0);
		pThis->m_pStatus[nPort] = malloc (sizeof (TUSBPortStatus));
		assert (pThis->m_pStatus[nPort] != 0);

		if (DWHCIDeviceControlMessage (pHost, pEndpoint0,
			REQUEST_IN | REQUEST_CLASS | REQUEST_TO_OTHER,
			GET_STATUS, 0, nPort+1, pThis->m_pStatus[nPort], 4) != 4)
		{
			LogWrite (FromHub, LOG_ERROR, "Cannot get status of port %u", nPort+1);

			continue;
		}

		assert (pThis->m_pStatus[nPort]->wPortStatus & PORT_POWER__MASK);
		if (!(pThis->m_pStatus[nPort]->wPortStatus & PORT_CONNECTION__MASK))
		{
			continue;
		}

		if (DWHCIDeviceControlMessage (pHost, pEndpoint0,
			REQUEST_OUT | REQUEST_CLASS | REQUEST_TO_OTHER,
			SET_FEATURE, PORT_RESET, nPort+1, 0, 0) < 0)
		{
			LogWrite (FromHub, LOG_ERROR, "Cannot reset port %u", nPort+1);

			continue;
		}

		MsDelay (100);

		if (DWHCIDeviceControlMessage (pHost, pEndpoint0,
			REQUEST_IN | REQUEST_CLASS | REQUEST_TO_OTHER,
			GET_STATUS, 0, nPort+1, pThis->m_pStatus[nPort], 4) != 4)
		{
			return FALSE;
		}

		//LogWrite (FromHub, LOG_DEBUG, "Port %u status is 0x%04X", nPort+1, (unsigned) pThis->m_pStatus[nPort]->wPortStatus);

		if (!(pThis->m_pStatus[nPort]->wPortStatus & PORT_ENABLE__MASK))
		{
			LogWrite (FromHub, LOG_ERROR, "Port %u is not enabled", nPort+1);

			continue;
		}

		// check for over-current
		if (pThis->m_pStatus[nPort]->wPortStatus & PORT_OVER_CURRENT__MASK)
		{
			DWHCIDeviceControlMessage (pHost, pEndpoint0,
				REQUEST_OUT | REQUEST_CLASS | REQUEST_TO_OTHER,
				CLEAR_FEATURE, PORT_POWER, nPort+1, 0, 0);

			LogWrite (FromHub, LOG_ERROR, "Over-current condition on port %u", nPort+1);

			return FALSE;
		}

		TUSBSpeed Speed = USBSpeedUnknown;
		if (pThis->m_pStatus[nPort]->wPortStatus & PORT_LOW_SPEED__MASK)
		{
			Speed = USBSpeedLow;
		}
		else if (pThis->m_pStatus[nPort]->wPortStatus & PORT_HIGH_SPEED__MASK)
		{
			Speed = USBSpeedHigh;
		}
		else
		{
			Speed = USBSpeedFull;
		}

		// first create default device
		assert (pThis->m_pDevice[nPort] == 0);
		pThis->m_pDevice[nPort] = malloc (sizeof (TUSBDevice));
		assert (pThis->m_pDevice[nPort] != 0);
		USBDevice (pThis->m_pDevice[nPort], pHost, Speed, USBDeviceGetAddress (&pThis->m_USBDevice), nPort+1);

		if (!USBDeviceInitialize (pThis->m_pDevice[nPort]))
		{
			_USBDevice (pThis->m_pDevice[nPort]);
			free (pThis->m_pDevice[nPort]);
			pThis->m_pDevice[nPort] = 0;

			continue;
		}

		TString *pNames = USBStandardHubGetDeviceNames (pThis->m_pDevice[nPort]);
		assert (pNames != 0);

		LogWrite (FromHub, LOG_NOTICE, "Port %u: Device %s found", nPort+1, StringGet (pNames));

		_String (pNames);
		free (pNames);
	}

	// now configure devices
	for (unsigned nPort = 0; nPort < pThis->m_nPorts; nPort++)
	{
		if (pThis->m_pDevice[nPort] == 0)
		{
			continue;
		}

		// now create specific device from default device
		TUSBDevice *pChild = USBDeviceFactoryGetDevice (pThis->m_pDevice[nPort]);
		if (pChild != 0)
		{
			_USBDevice (pThis->m_pDevice[nPort]);		// delete default device
			free (pThis->m_pDevice[nPort]);
			pThis->m_pDevice[nPort] = pChild;		// assign specific device

			if (!(*pThis->m_pDevice[nPort]->Configure) (pThis->m_pDevice[nPort]))
			{
				LogWrite (FromHub, LOG_ERROR, "Port %u: Cannot configure device", nPort+1);

				continue;
			}

			LogWrite (FromHub, LOG_DEBUG, "Port %u: Device configured", nPort+1);
		}
		else
		{
			LogWrite (FromHub, LOG_NOTICE, "Port %u: Device is not supported", nPort+1);

			_USBDevice (pThis->m_pDevice[nPort]);
			free (pThis->m_pDevice[nPort]);
			pThis->m_pDevice[nPort] = 0;
		}
	}

	// again check for over-current
	TUSBHubStatus *pHubStatus = malloc (sizeof (TUSBHubStatus));
	assert (pHubStatus != 0);

	if (DWHCIDeviceControlMessage (pHost, pEndpoint0,
		REQUEST_IN | REQUEST_CLASS,
		GET_STATUS, 0, 0, pHubStatus, sizeof *pHubStatus) != (int) sizeof *pHubStatus)
	{
		LogWrite (FromHub, LOG_ERROR, "Cannot get hub status");

		free (pHubStatus);

		return FALSE;
	}

	if (pHubStatus->wHubStatus & HUB_OVER_CURRENT__MASK)
	{
		for (unsigned nPort = 0; nPort < pThis->m_nPorts; nPort++)
		{
			DWHCIDeviceControlMessage (pHost, pEndpoint0,
				REQUEST_OUT | REQUEST_CLASS | REQUEST_TO_OTHER,
				CLEAR_FEATURE, PORT_POWER, nPort+1, 0, 0);
		}

		LogWrite (FromHub, LOG_ERROR, "Hub over-current condition");

		free (pHubStatus);

		return FALSE;
	}

	free (pHubStatus);
	pHubStatus = 0;

	boolean bResult = TRUE;

	for (unsigned nPort = 0; nPort < pThis->m_nPorts; nPort++)
	{
		if (DWHCIDeviceControlMessage (pHost, pEndpoint0,
			REQUEST_IN | REQUEST_CLASS | REQUEST_TO_OTHER,
			GET_STATUS, 0, nPort+1, pThis->m_pStatus[nPort], 4) != 4)
		{
			continue;
		}

		if (pThis->m_pStatus[nPort]->wPortStatus & PORT_OVER_CURRENT__MASK)
		{
			DWHCIDeviceControlMessage (pHost, pEndpoint0,
				REQUEST_OUT | REQUEST_CLASS | REQUEST_TO_OTHER,
				CLEAR_FEATURE, PORT_POWER, nPort+1, 0, 0);

			LogWrite (FromHub, LOG_ERROR, "Over-current condition on port %u", nPort+1);

			bResult = FALSE;
		}
	}

	return bResult;
}