📄 usbddrv.cpp
字号:
LPTRANSFER_NOTIFY_ROUTINE lpStartAddress, // @parm [IN] - Address of callback routine (may be NULL)
LPVOID lpvNotifyParameter, // @parm [IN] - Parameter to pass to callback routine
DWORD dwFlags, // @parm [IN] - USB_NO_WAIT, or 0
UCHAR bType, // @parm [IN] - Descriptor type (one of
// USB_DEVICE_DESCRIPTOR_TYPE,
// USB_CONFIGURATION_DESCRIPTOR_TYPE,
// USB_STRING_DESCRIPTOR_TYPE, or vendor
// specific descriptor type)
UCHAR bIndex, // @parm [IN] - Index within descriptor
WORD wLanguage, // @parm [IN] - Language Id for string descriptors, 0 for others
WORD wLength, // @parm [IN] - Size of buffer
LPVOID lpvBuffer) // @parm [IN] - Buffer for descriptor data
{
USB_DEVICE_REQUEST DeviceRequest;
DEBUGMSG(ZONE_API|ZONE_CONFIG,(TEXT("+USBD:GetDescriptor\r\n")));
DeviceRequest.bmRequestType = USB_REQUEST_DEVICE_TO_HOST |
USB_REQUEST_STANDARD | USB_REQUEST_FOR_DEVICE;
DeviceRequest.bRequest = USB_REQUEST_GET_DESCRIPTOR;
DeviceRequest.wValue = ((WORD)bType) << 8 | bIndex;
DeviceRequest.wIndex = wLanguage;
DeviceRequest.wLength = wLength;
dwFlags |= USB_IN_TRANSFER;
dwFlags &= ~USB_SHORT_TRANSFER_OK;
return IssueVendorTransfer(hDevice, lpStartAddress, lpvNotifyParameter,
dwFlags, &DeviceRequest, lpvBuffer, 0);
}
/*
* @func USB_TRANSFER | SetDescriptor | Send a SET_DESCRIPTOR request to USB device.
* @rdesc Returns a USB_TRANSFER handle, or NULL if an error occurs.
* @comm Initiates a control transfer to USB device setting device
* descriptor information.
* @xref <f GetDescriptor> <f IsTransferComplete> <f GetTransferStatus> <f AbortTransfer>
*/
extern "C" USB_TRANSFER
SetDescriptor(
USB_HANDLE hDevice, // @parm [IN] - USB device handle
LPTRANSFER_NOTIFY_ROUTINE lpStartAddress, // @parm [IN] - Address of callback routine (may be NULL)
LPVOID lpvNotifyParameter, // @parm [IN] - Parameter to pass to callback routine
DWORD dwFlags, // @parm [IN] - USB_NO_WAIT, or 0
UCHAR bType, // @parm [IN] - Descriptor type (one of
// USB_DEVICE_DESCRIPTOR_TYPE,
// USB_CONFIGURATION_DESCRIPTOR_TYPE,
// or USB_STRING_DESCRIPTOR_TYPE, or vendor
// specific descriptor type)
UCHAR bIndex, // @parm [IN] - Index within descriptor
WORD wLanguage, // @parm [IN] - Language Id for string descriptors, 0 for others
WORD wLength, // @parm [IN] - Descriptor length
LPVOID lpvBuffer) // @parm [IN] - Buffer for descriptor data
{
USB_DEVICE_REQUEST DeviceRequest;
DEBUGMSG(ZONE_API|ZONE_CONFIG,(TEXT("+USBD:SetDescriptor\r\n")));
DeviceRequest.bmRequestType = USB_REQUEST_HOST_TO_DEVICE |
USB_REQUEST_STANDARD | USB_REQUEST_FOR_DEVICE;
DeviceRequest.bRequest = USB_REQUEST_SET_DESCRIPTOR;
DeviceRequest.wValue = ((WORD)bType) << 8 | bIndex;
DeviceRequest.wIndex = wLanguage;
DeviceRequest.wLength = wLength;
dwFlags &= ~USB_IN_TRANSFER;
dwFlags &= ~USB_SHORT_TRANSFER_OK;
return IssueVendorTransfer(hDevice, lpStartAddress, lpvNotifyParameter,
dwFlags, &DeviceRequest, lpvBuffer, 0);
}
/*
* @func USB_TRANSFER | SetFeature | Send a SET_FEATURE request to USB device.
* @rdesc Returns a USB_TRANSFER handle, or NULL if an error occurs.
* @comm Initiates a control transfer to USB device requesting specified
* feature be enabled.
* @xref <f ClearFeature> <f IsTransferComplete> <f GetTransferStatus> <f AbortTransfer>
*/
extern "C" USB_TRANSFER
SetFeature(
USB_HANDLE hDevice, // @parm [IN] - USB device handle
LPTRANSFER_NOTIFY_ROUTINE lpStartAddress, // @parm [IN] - Address of callback routine (may be NULL)
LPVOID lpvNotifyParameter, // @parm [IN] - Parameter to pass to callback routine
DWORD dwFlags, // @parm [IN] - USB_NO_WAIT or 0, plus one of the following:
// USB_SEND_TO_DEVICE: Request for device
// USB_SEND_TO_INTERFACE: Request for interface
// USB_SEND_TO_ENDPOINT: Request for endpoint (default)
WORD wFeature, // @parm [IN] - Feature selector - one of USB_FEATURE_xxx defs in usb100.h
UCHAR bIndex) // @parm [IN] - 0 for DEVICE, or interface/endpoint number
{
USB_DEVICE_REQUEST DeviceRequest;
DEBUGMSG(ZONE_API|ZONE_CONFIG,(TEXT("+USBD:SetFeature\r\n")));
DeviceRequest.bmRequestType = USB_REQUEST_HOST_TO_DEVICE |
USB_REQUEST_STANDARD;
if(dwFlags & USB_SEND_TO_DEVICE)
DeviceRequest.bmRequestType |= USB_REQUEST_FOR_DEVICE;
else if(dwFlags & USB_SEND_TO_INTERFACE)
DeviceRequest.bmRequestType |= USB_REQUEST_FOR_INTERFACE;
else
DeviceRequest.bmRequestType |= USB_REQUEST_FOR_ENDPOINT;
DeviceRequest.bRequest = USB_REQUEST_SET_FEATURE;
DeviceRequest.wValue = wFeature;
DeviceRequest.wIndex = dwFlags & USB_SEND_TO_DEVICE ? 0 : bIndex;
DeviceRequest.wLength = 0;
dwFlags &= ~USB_IN_TRANSFER;
dwFlags &= ~USB_SHORT_TRANSFER_OK;
return IssueVendorTransfer(hDevice, lpStartAddress, lpvNotifyParameter,
dwFlags, &DeviceRequest, NULL, 0);
}
/*
* @func USB_TRANSFER | ClearFeature | Send a CLEAR_FEATURE request to USB device
* @rdesc Returns a USB_TRANSFER handle, or NULL if an error occurs.
* @comm Initiates a control transfer to USB device requesting specified
* feature be disabled.
* @xref <f SetFeature> <f IsTransferComplete> <f GetTransferStatus> <f AbortTransfer>
*/
extern "C" USB_TRANSFER
ClearFeature(
USB_HANDLE hDevice, // @parm [IN] - USB device handle
LPTRANSFER_NOTIFY_ROUTINE lpStartAddress, // @parm [IN] - Address of callback routine (may be NULL)
LPVOID lpvNotifyParameter, // @parm [IN] - Parameter to pass to callback routine
DWORD dwFlags, // @parm [IN] - USB_NO_WAIT or 0, plus one of the following:
// USB_SEND_TO_DEVICE: Request for device
// USB_SEND_TO_INTERFACE: Request for interface
// USB_SEND_TO_ENDPOINT: Request for endpoint (default)
WORD wFeature, // @parm [IN] - Feature selector - one of USB_FEATURE_xxx defs in usb100.h
UCHAR bIndex) // @parm [IN] - 0 for DEVICE, or interface/endpoint number
{
USB_DEVICE_REQUEST DeviceRequest;
DEBUGMSG(ZONE_API|ZONE_CONFIG,(TEXT("+USBD:ClearFeature\r\n")));
DeviceRequest.bmRequestType = USB_REQUEST_HOST_TO_DEVICE |
USB_REQUEST_STANDARD;
if(dwFlags & USB_SEND_TO_DEVICE)
DeviceRequest.bmRequestType |= USB_REQUEST_FOR_DEVICE;
else if(dwFlags & USB_SEND_TO_INTERFACE)
DeviceRequest.bmRequestType |= USB_REQUEST_FOR_INTERFACE;
else
DeviceRequest.bmRequestType |= USB_REQUEST_FOR_ENDPOINT;
DeviceRequest.bRequest = USB_REQUEST_CLEAR_FEATURE;
DeviceRequest.wValue = wFeature;
DeviceRequest.wIndex = dwFlags & USB_SEND_TO_DEVICE ? 0 : bIndex;
DeviceRequest.wLength = 0;
dwFlags &= ~USB_IN_TRANSFER;
dwFlags &= ~USB_SHORT_TRANSFER_OK;
return IssueVendorTransfer(hDevice, lpStartAddress, lpvNotifyParameter,
dwFlags, &DeviceRequest, NULL, 0);
}
/*
* @func USB_TRANSFER | GetStatus | Send a GET_STATUS request to USB device.
* @rdesc Returns a USB_TRANSFER handle, or NULL if an error occurs.
* @comm Initiates a control transfer to USB device requesting device,
* interface, or endpoint status.
* @xref <f GetStatus> <f IsTransferComplete> <f GetTransferStatus> <f AbortTransfer>
*/
extern "C" USB_TRANSFER
GetStatus(
USB_HANDLE hDevice, // @parm [IN] - USB device handle
LPTRANSFER_NOTIFY_ROUTINE lpStartAddress, // @parm [IN] - Address of callback routine (may be NULL)
LPVOID lpvNotifyParameter, // @parm [IN] - Parameter to pass to callback routine
DWORD dwFlags, // @parm [IN] - USB_NO_WAIT or 0, plus one of the following:
// USB_SEND_TO_DEVICE: Request for device
// USB_SEND_TO_INTERFACE: Request for interface
// USB_SEND_TO_ENDPOINT: Request for endpoint (default)
UCHAR bIndex, // @parm [IN] - 0 for DEVICE, or interface/endpoint number
LPWORD lpwStatus) // @parm [OUT]- Filled in with status word
{
USB_DEVICE_REQUEST DeviceRequest;
DEBUGMSG(ZONE_API|ZONE_CONFIG,(TEXT("+USBD:GetStatus\r\n")));
DeviceRequest.bmRequestType = USB_REQUEST_DEVICE_TO_HOST |
USB_REQUEST_STANDARD;
if(dwFlags & USB_SEND_TO_DEVICE)
DeviceRequest.bmRequestType |= USB_REQUEST_FOR_DEVICE;
else if(dwFlags & USB_SEND_TO_INTERFACE)
DeviceRequest.bmRequestType |= USB_REQUEST_FOR_INTERFACE;
else
DeviceRequest.bmRequestType |= USB_REQUEST_FOR_ENDPOINT;
DeviceRequest.bRequest = USB_REQUEST_GET_STATUS;
DeviceRequest.wValue = 0;
DeviceRequest.wIndex = dwFlags & USB_SEND_TO_DEVICE ? 0 : bIndex;
DeviceRequest.wLength = 2;
dwFlags |= USB_IN_TRANSFER;
dwFlags &= ~USB_SHORT_TRANSFER_OK;
return IssueVendorTransfer(hDevice, lpStartAddress, lpvNotifyParameter,
dwFlags, &DeviceRequest, lpwStatus, 0);
}
/*
* @func USB_TRANSFER | SyncFrame | Send a SYNC_FRAME request to USB device.
* @rdesc Returns a USB_TRANSFER handle, or NULL if an error occurs.
* @comm Initiates a control transfer to USB device requesting a Sync
* Frame be sent (used by isoch endpoints to synchronize a data
* stream).
* @xref <f IsTransferComplete> <f GetTransferStatus> <f AbortTransfer>
*/
extern "C" USB_TRANSFER
SyncFrame(
USB_HANDLE hDevice, // @parm [IN] - USB device handle
LPTRANSFER_NOTIFY_ROUTINE lpStartAddress, // @parm [IN] - Address of callback routine (may be NULL)
LPVOID lpvNotifyParameter, // @parm [IN] - Parameter to pass to callback routine
DWORD dwFlags, // @parm [IN] - USB_NO_WAIT, or 0
UCHAR bEndpoint, // @parm [IN] - Endpoint number
LPWORD lpwFrame) // @parm [OUT]- Frame number
{
USB_DEVICE_REQUEST DeviceRequest;
DEBUGMSG(ZONE_API|ZONE_CONFIG,(TEXT("+USBD:SyncFrame\r\n")));
DeviceRequest.bmRequestType = USB_REQUEST_HOST_TO_DEVICE |
USB_REQUEST_STANDARD | USB_REQUEST_FOR_ENDPOINT;
DeviceRequest.bRequest = USB_REQUEST_SYNC_FRAME;
DeviceRequest.wValue = 0;
DeviceRequest.wIndex = bEndpoint;
DeviceRequest.wLength = 2;
dwFlags |= USB_IN_TRANSFER;
dwFlags &= ~USB_SHORT_TRANSFER_OK;
return IssueVendorTransfer(hDevice, lpStartAddress, lpvNotifyParameter,
dwFlags, &DeviceRequest, lpwFrame, 0);
}
/*
* @func USB_TRANSFER | IssueVendorTransfer | Send a vendor specific control transfer to USB device.
* @rdesc Returns a USB_TRANSFER handle, or NULL if an error occurs.
* @comm Initiates a control transfer to USB device on the default
* endpoint (0). Intended for vendor specific transfers (for standard
* transfers, use corresponding function (e.g. <f GetInterface>,
* <f SetInterface>, etc).
* @xref <f IsTransferComplete> <f GetTransferStatus> <f AbortTransfer>
*/
extern "C" USB_TRANSFER
IssueVendorTransfer(
USB_HANDLE hDevice, // @parm [IN] - USB device handle
LPTRANSFER_NOTIFY_ROUTINE lpStartAddress, // @parm [IN] - Address of callback routine (may be NULL)
LPVOID lpvNotifyParameter, // @parm [IN] - Parameter to pass to callback routine
DWORD dwFlags, // @parm [IN] - Transfer flags (see usbtypes.h)
LPCUSB_DEVICE_REQUEST lpControlHeader, // @parm [IN] - Pointer to device request header
LPVOID lpvBuffer, // @parm [IN] - Data buffer (if physical buffer address is
// specified, this must contain the virtual
// address of the buffer)
ULONG uBufferPhysicalAddress) // @parm [IN] - Physical address of data buffer (may be NULL)
{
SDevice * pDev = (SDevice *)hDevice;
if (!ValidateDeviceHandle(pDev))
{
DEBUGMSG(ZONE_WARNING,(TEXT("!USBD:IssueVendorTransfer - Invalid device handle\r\n")));
SetLastError(ERROR_INVALID_HANDLE);
return NULL;
}
DEBUGMSG(ZONE_API|ZONE_VENDOR,(TEXT("+USBD:IssueVendorTransfer, dwFlags:0x%X, Control:0x%X, vBuf:0x%X, pBuf:0x%X\r\n"),
dwFlags,lpControlHeader,lpvBuffer,uBufferPhysicalAddress));
EnterCriticalSection(&pDev->csPipeLock);
SPipe * pPipe = pDev->apPipes[gcEndpointZero];
LeaveCriticalSection(&pDev->csPipeLock);
if (!ReferencePipeHandle(pPipe))
{
DEBUGMSG(ZONE_WARNING,(TEXT("!USBD:IssueVendorTransfer - Invalid EP0 handle\r\n")));
SetLastError(ERROR_INVALID_HANDLE);
return NULL;
}
SHcd * pHcd = pDev->pHcd;
LPHCD_ISSUE_TRANSFER pFunc = pHcd->pHcdFuncs->lpIssueTransfer;
BOOL fWait = FALSE;
SWait * pWait;
BOOL fRet;
if(!lpStartAddress)
fWait = dwFlags & USB_NO_WAIT ? FALSE : TRUE;
STransfer * pTransfer = GetTransferObject(pPipe,0);
if (pTransfer == NULL)
{
DereferencePipeHandle(pPipe);
SetLastError(ERROR_OUTOFMEMORY);
return NULL;
}
pTransfer->pvBuffer = lpvBuffer;
if(fWait)
{
pWait = GetWaitObject();
lpStartAddress = &SignalEventFunc;
lpvNotifyParameter = &pWait->hEvent;
}
fRet = (*pFunc)(pHcd->pvHcd, pDev->iDevice, pPipe->iEndpointIndex,
lpStartAddress, lpvNotifyParameter, dwFlags, lpControlHeader, 0, 0,
NULL, lpControlHeader->wLength, lpvBuffer, uBufferPhysicalAddress,
pTransfer, NULL, NULL, &pTransfer->fComplete,
&pTransfer->dwBytesTransfered, &pTransfer->dwError);
if (fRet && AddTransfer(pPipe,pTransfer))
{
if (fWait)
WaitForSingleObject(pWait->hEvent, INFINITE);
}
else
{
FreeTransferObject(pTransfer);
pTransfer = NULL;
}
if(fWait)
FreeWaitObject(pWait);
DereferencePipeHandle(pPipe);
DEBUGMSG(ZONE_API|ZONE_VENDOR,(TEXT("-USBD:IssueVendorTransfer, pTransfer:0x%X\r\n"),pTransfer));
return (USB_TRANSFER)pTransfer;
}
/*
* @func USB_TRANSFER | IssueControlTransfer | Initiate control transfer with USB device.
* @rdesc Returns a USB_TRANSFER handle, or NULL if an error occurs.
* @comm Initiates a control transfer to USB device on the specified
* endpoint. Note that control transfers to the default endpoint (0) are issued
* using <f IssueVendorTransfer>.
* @xref <f IsTransferComplete> <f GetTransferStatus> <f AbortTransfer>
*/
extern "C" USB_TRANSFER
IssueControlTransfer(
USB_PIPE hPipe, // @parm [IN] - Open USB pipe handle
LPTRANSFER_NOTIFY_RO
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -