📄 hub.c
字号:
/****************************************************************
* MT View Silicon Tech. Inc.
*
* Copyright 2007, MT View Silicon Tech. Inc., ShangHai, China
* All rights reserved.
*
*
* Filename: hub.c
*
* Programmer: Grey
*
* Created: 01/xx/2008
*
* Description: usb hub device driver
*
*
*****************************************************************/
#include "hub.h"
#include <string.h>
HUB XDATA gHub[MAX_HUB_NUM];
BYTE XDATA gHubMap = 0;
extern VOID*
AllocHub(VOID)
{
BYTE DATA i;
HUB* DATA hub;
/* for (i = 0; i < MAX_HUB_NUM; ++i)
{
if ((gHubMap & (0x01 << i)) == 0)
break;
}*/
i = SearchMapZero(&gHubMap, MAX_HUB_NUM);
if (i == MAX_HUB_NUM)
return NULL;
// memset(&gHub[i], 0, sizeof(HUB));
// gHub[i].Set = i;
// gHubMap |= (0x01 << i);
SetMap(&gHubMap, i);
hub = &gHub[i];
memset(hub, 0, sizeof(HUB));
hub->Set = i;
return hub;
}
extern VOID
FreeHub(
VOID *hubpriv
)
{
HUB *hub = (HUB *)hubpriv;
/* free usb device of this hub */
hub->Usb = NULL;
/* free this hub */
// gHubMap &= ~(0x01 << hub->Set);
ClearMap(&gHubMap, hub->Set);
}
extern VOID
HubGetPortStatus(
HUB *hub,
BYTE portNum
)
{
DWORD temp;
WORD status, change;
HUB_PORT *port;
temp = UstGetRootHubPortStatus(hub->Usb, portNum);
status = (WORD)temp;
change = (WORD)(temp >> 16);
port = &hub->Port[portNum];
port->IsConnect = ((status & HUB_PORT_CONNECTION) != 0);
port->IsChange = ((change & HUB_C_PORT_CONNECTION) != 0);
port->IsPowerOn = ((status & HUB_PORT_POWER) != 0);
port->IsEnable = ((status & HUB_PORT_ENABLE) != 0);
}
extern BOOL
HubEnablePort(
HUB *hub,
BYTE portNum
)
{
BOOL retVal;
retVal = UsbEnableRootHubPort(hub->Usb, portNum);
hub->Port[portNum].IsEnable = retVal;
return retVal;
}
extern VOID
OpenHub(
VOID *hubpriv,
VOID *usbpriv
)
{
HUB *hub = (HUB *)hubpriv;
USB_BUS *usb = (USB_BUS *)usbpriv;
HUB_DESCRIPTOR *hubDesp;
/* load hub descirptor */
hubDesp = &hub->HubDescriptor;
memset(hubDesp, 0, sizeof(HUB_DESCRIPTOR));
hubDesp->bDescLength = USB_DT_HUB_SIZE;
hubDesp->bDescriptorType = DESCRIPTOR_HUB;
hubDesp->bNbrPorts = MAX_RH_PORT_NUM;
memset(&hub->Port[0], 0, sizeof(hub->Port));
hub->Usb = usb;
}
extern BYTE
HubProcessPortChange(
HUB *hub,
BYTE portNum,
BOOL isConn
)
{
USB_DEVICE *device;
USB_CONFIG *config;
USB_INTERFACE *interface;
STORAGE *stor;
BYTE i, j;
BYTE err;
if (isConn) /* new device connect */
{
/* enable hub port */
if (!HubEnablePort(hub, portNum))
return ENABLE_HUB_PORT_ERR;
/* allocate usb device */
device = AllocUsbDevice();
if (device == NULL)
return ALLOC_DEVICE_ERR;
hub->Connectivity[portNum] = device;
/* start a usb device */
err = UsbStartDevice(device, hub->Usb);
if (err != NO_ERROR)
{
FreeUsbDevice(device);
return err;
}
/* scan system driver, choose device interface and set device config */
for (i = 0; i < device->DeviceDescriptor.bNumConfigurations; ++i)
{
config = device->Config[i];
for (j = 0; j < config->ConfigDescriptor.bNumInterfaces; ++j)
{
interface = config->Interface[j];
/* check interface mass storage class */
if (interface->InterfaceDescriptor.bInterfaceClass == USB_CLASS_MASS_STORAGE
&& interface->InterfaceDescriptor.bInterfaceSubClass == 0x06
&& interface->InterfaceDescriptor.bInterfaceProtocol == 0x50)
{
/* allocate storage function */
stor = AllocStorage();
if (stor == NULL)
{
FreeUsbDevice(device);
return ALLOC_STOR_ERR;
}
interface->FunctionDevice = stor;
interface->CloseFunctionDevice = FreeStorage;
if (!OpenStor(stor, device, config, interface))
{
FreeUsbDevice(device);
return OPEN_STOR_ERR;
}
else
{
return NO_ERROR;
}
}
else
{
/* check interface other class */
}
}/* end for j */
}/* end for i */
}
else /* device pull out */
{
/* free usb device connect with this hub port */
if (hub->Connectivity[portNum] != NULL)
{
FreeUsbDevice(hub->Connectivity[portNum]);
hub->Connectivity[portNum] = NULL;
}/* end if */
}/* end else */
return NO_ERROR;
}
extern VOID
HubPoll(
VOID *hubpriv
)
{
HUB *hub = (HUB *)hubpriv;
BYTE i;
for (i = 0; i < MAX_HUB_PORT_NUM; ++i)
{
HubGetPortStatus(hub, i);
if (hub->Port[i].IsChange)
{
HubProcessPortChange(hub, i, hub->Port[i].IsConnect);
}
}/* end for */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -