📄 usrusbtool.c
字号:
{
UINT8 bfr [USB_MAX_DESCR_LEN];
pUSB_STRING_DESCR pString = (pUSB_STRING_DESCR) bfr;
UINT16 actLen;
UINT16 i;
char c;
UINT16 languageId=0;
UINT16 id;
int lang_len;
int lang_count;
UINT8 *lang_ptr;
/* Read device descriptor to get string Ids for mfg/model */
/* Find out about language id */
if (usbdDescriptorGet (clientHandle, nodeId,
USB_RT_STANDARD | USB_RT_DEVICE, USB_DESCR_STRING, 0, 0,
sizeof (bfr), (pUINT8) &bfr, &actLen) != OK)
{
fprintf (fout, "<<can't read string>>");
return;
}
lang_len = pString->length - sizeof(pString->length) - sizeof(pString->descriptorType);
lang_count = lang_len/sizeof(languageId);
lang_ptr = pString->string;
for ( i=0; i<lang_len && lang_ptr ; i++,lang_ptr+=2 )
{
if ( lang_ptr[0]==0x09 && lang_ptr[1]==0x04 )
{
/* Select US English if it's available */
languageId = (lang_ptr[1]<<8) | lang_ptr[0];
break;
} /* end of if () */
} /* end of for () */
if ( languageId==0 && lang_len>0 && pString->string )
{
/* Try any first choice */
languageId = (pString->string[1]<<8) | pString->string[0];
} /* end of if () */
if (usbdDescriptorGet (clientHandle, nodeId,
USB_RT_STANDARD | USB_RT_DEVICE, USB_DESCR_STRING, stringId, languageId,
sizeof (bfr), (pUINT8) &bfr, &actLen) != OK)
{
fprintf (fout, "<<can't read string>>");
return;
}
for (i = 0; i + 2 < actLen && i < maxLen * 2; i += 2)
{
c = pString->string [i];
fprintf (fout, "%c", (isprint ((int) c)) ? c : '_');
}
if (i + 2 < actLen)
fprintf (fout, "...");
}
/***************************************************************************
*
* showMfgModel - Display mfg/model strings for a USB device
*
* RETURNS: N/A
*/
LOCAL VOID showMfgModel
(
USBD_CLIENT_HANDLE clientHandle,
USBD_NODE_ID nodeId,
FILE *fout
)
{
USB_DEVICE_DESCR devDescr;
UINT16 actLen;
/* Read device descriptor to get string Ids for mfg/model */
if (usbdDescriptorGet (clientHandle, nodeId,
USB_RT_STANDARD | USB_RT_DEVICE, USB_DESCR_DEVICE, 0, 0,
sizeof (devDescr), (pUINT8) &devDescr, &actLen) != OK ||
actLen < sizeof (devDescr))
return;
if (devDescr.manufacturerIndex != 0 || devDescr.productIndex != 0)
fprintf (fout, " = ");
if (devDescr.manufacturerIndex != 0)
{
showUsbString (clientHandle, nodeId, fout, devDescr.manufacturerIndex,
MAX_MFG_STR_LEN);
fprintf (fout, "/");
}
if (devDescr.productIndex != 0)
{
showUsbString (clientHandle, nodeId, fout, devDescr.productIndex,
MAX_PROD_STR_LEN);
}
}
/***************************************************************************
*
* HubEnumerate - Enumerate all ports on the specified hub
*
* This routine enumerates all devices from the specified HubId down.
* <clientHandle> must be a valid USBD_CLIENT_HANDLE. <hubId> specifies
* the Node Id of the first USB hub to be enumerated.
*
* RETURNS: OK, or ERROR if USBD returns an error.
*/
LOCAL UINT16 HubEnumerate
(
USBD_CLIENT_HANDLE clientHandle, /* Caller抯 USBD client handle */
USBD_NODE_ID hubId, /* Node Id for hub to enumerate */
FILE *fout,
UINT16 indent
)
{
UINT16 portCount; /* Number of ports on this hub */
UINT16 portIndex; /* current port index */
UINT16 nodeType; /* type of node being enumerated */
USBD_NODE_ID nodeId; /* id of node being enumerated */
UINT16 s;
/* Retrieve the number of ports for this hub. */
fprintf (fout, "%*shub 0x%x", indent, "", (UINT32) hubId);
showMfgModel (clientHandle, hubId, fout);
fprintf (fout, "\n");
if ((s = usbdHubPortCountGet (clientHandle, hubId, &portCount))
!= OK)
{
fprintf (fout, "usbdHubPortCountGet() returned %d\n", s);
return ERROR;
}
fprintf (fout, "%*sport count = %d\n", indent+INDENT, "", portCount);
/* See if a device is attached to each of the hub抯 ports. */
for (portIndex = 0; portIndex < portCount; portIndex++)
{
if ((s = usbdNodeIdGet (clientHandle, hubId, portIndex, &nodeType,
&nodeId)) != OK)
{
fprintf (fout, "%*susbdNodeIdGet() returned %d\n", indent+INDENT, "",
s);
return ERROR;
}
switch (nodeType)
{
case USB_NODETYPE_NONE: /* No device attached. */
fprintf (fout, "%*sport %d not connected\n", indent+INDENT, "",
portIndex);
break;
case USB_NODETYPE_HUB: /* Another hub found. */
fprintf (fout, "%*sport %d is hub 0x%x\n", indent+INDENT, "",
portIndex, (UINT32) nodeId);
if (HubEnumerate (clientHandle, nodeId, fout, indent+INDENT)
!= OK)
return ERROR;
break;
case USB_NODETYPE_DEVICE: /* Device attached to port. */
fprintf (fout, "%*sport %d is device 0x%x", indent+INDENT, "",
portIndex, (UINT32) nodeId);
showMfgModel (clientHandle, nodeId, fout);
fprintf (fout, "\n");
break;
default: /* Unknown node type code */
fprintf (fout, "%*snode type not recognized for node 0x%x\n",
indent+INDENT, "", (UINT32) nodeId);
break;
}
}
return OK;
}
/***************************************************************************
*
* USBEnumerate - Enumerate all USB host controllers in the system.
*
* This routine enumerates all USB host controllers, hubs, and devices
* currently connected to the system. The caller must register with the
* USBD prior to calling this function and supply its USBD_CLIENT_HANDLE
* in <clientHandle>.
*
* RETURNS: OK, or ERROR if USBD returns an error.
*/
LOCAL UINT16 USBEnumerate
(
USBD_CLIENT_HANDLE clientHandle, /* Caller抯 USBD client handle */
FILE *fout
)
{
UINT16 busCount; /* Number of USB host controllers */
UINT16 busIndex; /* current bus index */
USBD_NODE_ID rootId; /* Root hub id for current bus */
UINT16 s;
/* Retrieve the number of USB host controllers in the system. */
if ((s = usbdBusCountGet (clientHandle, &busCount)) != OK)
{
fprintf (fout, "usbdBusCountGet() returned %d\n", s);
return ERROR;
}
fprintf (fout, "bus count = %d\n", busCount);
/* Retrieve the root hub id for each host controller and enumerate it. */
for (busIndex = 0; busIndex < busCount; busIndex++)
{
if ((s = usbdRootNodeIdGet (clientHandle, busIndex, &rootId))
!= OK)
{
fprintf (fout, "usbdRootNodeIdGet() returned %d\n", s);
return ERROR;
}
fprintf (fout, "enumerating bus %d\n", busIndex);
if (HubEnumerate (clientHandle, rootId, fout, INDENT) != OK)
return ERROR;
}
return OK;
}
/*************************************************************************
*
* cmdUsbEnum - Enumerate USBs attached to system
*
* RETURNS: RET_CONTINUE;
*/
LOCAL UINT16 cmdUsbEnum
(
pVOID Param, /* Generic parameter passed down */
char **ppCmd, /* Ptr to remainder of cmd line */
FILE *fin, /* stream for input (if any) */
FILE *fout /* stream for output (if any) */
)
{
USBEnumerate (usbdClientHandle, fout);
return RET_CONTINUE;
}
/*************************************************************************
*
* cmdUsbStats - Show bus statistics
*
* RETURNS: RET_CONTINUE;
*/
LOCAL UINT16 cmdUsbStats
(
pVOID Param, /* Generic parameter passed down */
char **ppCmd, /* Ptr to remainder of cmd line */
FILE *fin, /* stream for input (if any) */
FILE *fout /* stream for output (if any) */
)
{
long nodeId;
USBD_STATS stats;
UINT16 s;
/* Extract parameters. */
*ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
/* Validate parameters */
if (nodeId == -1)
{
fprintf (fout, "Must specify a node id.\n");
return RET_CONTINUE;
}
/* Execute function. */
if ((s = usbdStatisticsGet (usbdClientHandle, (GENERIC_HANDLE) nodeId, &stats, sizeof (stats)))
!= OK)
fprintf (fout, "usbdStatisticsGet() returned %d\n", s);
else
{
fprintf (fout, "totalTransfersIn = %ld\n", (long) stats.totalTransfersIn);
fprintf (fout, "totalTransfersout = %ld\n", (long) stats.totalTransfersOut);
fprintf (fout, "totalReceiveErrors = %ld\n", (long) stats.totalReceiveErrors);
fprintf (fout, "totalTransmitErrors = %ld\n", (long) stats.totalTransmitErrors);
}
return RET_CONTINUE;
}
/*************************************************************************
*
* cmdGetConfig - get current configuration value for a device
*
* RETURNS: RET_CONTINUE;
*/
LOCAL UINT16 cmdGetConfig
(
pVOID Param, /* Generic parameter passed down */
char **ppCmd, /* Ptr to remainder of cmd line */
FILE *fin, /* stream for input (if any) */
FILE *fout /* stream for output (if any) */
)
{
long nodeId;
UINT16 configuration;
UINT16 s;
/* Extract parameters. */
*ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
/* Validate parameters */
if (nodeId == -1)
{
fprintf (fout, "Must specify a node id.\n");
return RET_CONTINUE;
}
/* Execute function. */
if ((s = usbdConfigurationGet (usbdClientHandle, (GENERIC_HANDLE) nodeId, &configuration))
!= OK)
fprintf (fout, "usbdConfigurationGet() returned %d\n", s);
else
fprintf (fout, "current configuration = 0x%x\n", configuration);
return RET_CONTINUE;
}
/*************************************************************************
*
* cmdSetConfig - set current configuration value for a device
*
* RETURNS: RET_CONTINUE;
*/
LOCAL UINT16 cmdSetConfig
(
pVOID Param, /* Generic parameter passed down */
char **ppCmd, /* Ptr to remainder of cmd line */
FILE *fin, /* stream for input (if any) */
FILE *fout /* stream for output (if any) */
)
{
long nodeId;
long configuration;
UINT16 s;
/* Extract parameters. */
*ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
*ppCmd = GetHexToken (*ppCmd, &configuration, -1);
/* Validate parameters */
if (nodeId == -1 || configuration == -1)
{
fprintf (fout, "Must specify a node id and configuration value.\n");
return RET_CONTINUE;
}
/* Execute function. */
s = usbdConfigurationSet (usbdClientHandle, (GENERIC_HANDLE) nodeId, (UINT16) configuration, 0);
fprintf (fout, "usbdConfigurationSet() returned %d\n", s);
return RET_CONTINUE;
}
/*************************************************************************
*
* cmdGetInterface - get alt. setting for an interface
*
* RETURNS: RET_CONTINUE;
*/
LOCAL UINT16 cmdGetInterface
(
pVOID Param, /* Generic parameter passed down */
char **ppCmd, /* Ptr to remainder of cmd line */
FILE *fin, /* stream for input (if any) */
FILE *fout /* stream for output (if any) */
)
{
long nodeId;
long index;
UINT16 altSetting;
UINT16 s;
/* Extract parameters. */
*ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
*ppCmd = GetHexToken (*ppCmd, &index, -1);
/* Validate parameters */
if (nodeId == -1 || index == -1)
{
fprintf (fout, "Must specify a node id and interface number.\n");
return RET_CONTINUE;
}
/* Execute function. */
if ((s = usbdInterfaceGet (usbdClientHandle, (GENERIC_HANDLE) nodeId, index, &altSetting))
!= OK)
fprintf (fout, "usbdInterfaceGet() returned %d\n", s);
else
fprintf (fout, "alternate setting = 0x%x\n", altSetting);
return RET_CONTINUE;
}
/*************************************************************************
*
* cmdSetInterface - set alt. setting for an interface
*
* RETURNS: RET_CONTINUE;
*/
LOCAL UINT16 cmdSetInterface
(
pVOID Param, /* Generic parameter passed down */
char **ppCmd, /* Ptr to remainder of cmd line */
FILE *fin, /* stream for input (if any) */
FILE *fout /* stream for output (if any) */
)
{
long nodeId;
long index;
long altSetting;
UINT16 s;
/* Extract parameters. */
*ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
*ppCmd = GetHexToken (*ppCmd, &index, -1);
*ppCmd = GetHexToken (*ppCmd, &altSetting, -1);
/* Validate parameters */
if (nodeId == -1 || index == -1 || altSetting == -1)
{
fprintf (fout, "Must specify a node id, interface number, and alt. setting.\n");
return RET_CONTINUE;
}
/* Execute function. */
s = usbdInterfaceSet (usbdClientHandle, (GENERIC_HANDLE) nodeId, index, altSetting);
fprintf (fout, "usbdInterfaceSet() returned %d\n", s);
return RET_CONTINUE;
}
#endif /* INCLUDE_USB from way above*/
/*************************************************************************
*
* displayMem - dumps memory to display
*
* RETURNS: N/A
*/
LOCAL VOID displayMem
(
UINT16 bfrLen,
pUINT8 pBfr,
FILE *fout
)
{
int i,j;
for (i = 0; i < bfrLen; i += 16)
{
for (j = i; j < bfrLen && j < i+16; j++)
{
fprintf (fout, "%2.2x ", pBfr [j]);
if (j % 16 == 7)
fprintf (fout, " ");
}
fprintf (fout, " ");
for (j = i; j < bfrLen && j < i+16; j++)
{
fprintf (fout, "%c", (pBfr [j] >= 32 && pBfr [j] <= 127) ?
pBfr [j] : '.');
}
fprintf (fout, "\n");
}
}
#ifdef INCLUDE_USB
/*************************************************************************
*
* cmdGetStatus - get status for a device/interface/endpoint
*
* RETURNS: RET_CONTINUE;
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -