pikapython/bsp/ch582/StdPeriphDriver/CH58x_usb2hostClass.c
2022-03-26 20:51:40 +08:00

825 lines
30 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/********************************** (C) COPYRIGHT *******************************
* File Name : CH58x_usbhost.c
* Author : WCH
* Version : V1.2
* Date : 2021/11/17
* Description
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "CH58x_common.h"
#if DISK_LIB_ENABLE
#include "CHRV3UFI.H"
#endif
/* <20><><EFBFBD><EFBFBD>HID<49>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD> */
__attribute__((aligned(4))) const uint8_t SetupSetU2HIDIdle[] = {0x21, HID_SET_IDLE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
/* <20><>ȡHID<49><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
__attribute__((aligned(4))) const uint8_t SetupGetU2HIDDevReport[] = {0x81, USB_GET_DESCRIPTOR, 0x00,
USB_DESCR_TYP_REPORT, 0x00, 0x00, 0x41, 0x00};
/* <20><>ȡHUB<55><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
__attribute__((aligned(4))) const uint8_t SetupGetU2HubDescr[] = {HUB_GET_HUB_DESCRIPTOR, HUB_GET_DESCRIPTOR,
0x00, USB_DESCR_TYP_HUB, 0x00, 0x00, sizeof(USB_HUB_DESCR), 0x00};
__attribute__((aligned(4))) uint8_t U2Com_Buffer[128]; // <20><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><><C3B6>ʱ<EFBFBD><CAB1><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><>ٽ<EFBFBD><D9BD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/*********************************************************************
* @fn AnalyzeU2HidIntEndp
*
* @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><D0B7><EFBFBD><EFBFBD><EFBFBD>HID<49>ж϶˵<CFB6><CBB5>ĵ<EFBFBD>ַ,<2C><><EFBFBD><EFBFBD>HubPortIndex<65><78>0<EFBFBD><30><EFBFBD>浽ROOTHUB<55><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ֵ<EFBFBD>򱣴浽HUB<55>½ṹ<C2BD><E1B9B9>
*
* @param buf - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ HubPortIndex<65><78>0<EFBFBD><30>ʾ<EFBFBD><CABE>HUB<55><42><EFBFBD><EFBFBD>0<EFBFBD><30>ʾ<EFBFBD>ⲿHUB<55>µĶ˿ں<CBBF>
*
* @return <20>˵<EFBFBD><CBB5><EFBFBD>
*/
uint8_t AnalyzeU2HidIntEndp(uint8_t *buf, uint8_t HubPortIndex)
{
uint8_t i, s, l;
s = 0;
if(HubPortIndex)
{
memset(DevOnU2HubPort[HubPortIndex - 1].GpVar, 0, sizeof(DevOnU2HubPort[HubPortIndex - 1].GpVar)); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
else
{
memset(ThisUsb2Dev.GpVar, 0, sizeof(ThisUsb2Dev.GpVar)); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
for(i = 0; i < ((PUSB_CFG_DESCR)buf)->wTotalLength; i += l) // <20><><EFBFBD><EFBFBD><EFBFBD>ж϶˵<CFB6><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͽӿ<CDBD><D3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
if(((PUSB_ENDP_DESCR)(buf + i))->bDescriptorType == USB_DESCR_TYP_ENDP // <20>Ƕ˵<C7B6><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
&& (((PUSB_ENDP_DESCR)(buf + i))->bmAttributes & USB_ENDP_TYPE_MASK) == USB_ENDP_TYPE_INTER // <20><><EFBFBD>ж϶˵<CFB6>
&& (((PUSB_ENDP_DESCR)(buf + i))->bEndpointAddress & USB_ENDP_DIR_MASK)) // <20><>IN<49>˵<EFBFBD>
{ // <20><><EFBFBD><EFBFBD><EFBFBD>ж϶˵<CFB6><CBB5>ĵ<EFBFBD>ַ,λ7<CEBB><37><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD>־λ,<2C><>0
if(HubPortIndex)
{
DevOnU2HubPort[HubPortIndex - 1].GpVar[s] = ((PUSB_ENDP_DESCR)(buf + i))->bEndpointAddress & USB_ENDP_ADDR_MASK;
}
else
{
ThisUsb2Dev.GpVar[s] = ((PUSB_ENDP_DESCR)(buf + i))->bEndpointAddress & USB_ENDP_ADDR_MASK; // <20>ж϶˵<CFB6><CBB5>ĵ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>Ը<EFBFBD><D4B8><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>wMaxPacketSize<7A><65>bInterval
}
PRINT("%02x ", (uint16_t)ThisUsb2Dev.GpVar[s]);
s++;
if(s >= 4)
{
break; //ֻ<><D6BB><EFBFBD><EFBFBD>4<EFBFBD><34><EFBFBD>˵<EFBFBD>
}
}
l = ((PUSB_ENDP_DESCR)(buf + i))->bLength; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>
if(l > 16)
{
break;
}
}
PRINT("\n");
return (s);
}
/*********************************************************************
* @fn AnalyzeU2BulkEndp
*
* @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD>,GpVar[0]<5D><>GpVar[1]<5D><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4>˵㡣GpVar[2]<5D><>GpVar[3]<5D><><EFBFBD><EFBFBD><EFBFBD>´<EFBFBD><C2B4>˵<EFBFBD>
*
* @param buf - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ HubPortIndex<65><78>0<EFBFBD><30>ʾ<EFBFBD><CABE>HUB<55><42><EFBFBD><EFBFBD>0<EFBFBD><30>ʾ<EFBFBD>ⲿHUB<55>µĶ˿ں<CBBF>
*
* @return 0
*/
uint8_t AnalyzeU2BulkEndp(uint8_t *buf, uint8_t HubPortIndex)
{
uint8_t i, s1, s2, l;
s1 = 0;
s2 = 2;
if(HubPortIndex)
{
memset(DevOnU2HubPort[HubPortIndex - 1].GpVar, 0, sizeof(DevOnU2HubPort[HubPortIndex - 1].GpVar)); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
else
{
memset(ThisUsb2Dev.GpVar, 0, sizeof(ThisUsb2Dev.GpVar)); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
for(i = 0; i < ((PUSB_CFG_DESCR)buf)->wTotalLength; i += l) // <20><><EFBFBD><EFBFBD><EFBFBD>ж϶˵<CFB6><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͽӿ<CDBD><D3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
if((((PUSB_ENDP_DESCR)(buf + i))->bDescriptorType == USB_DESCR_TYP_ENDP) // <20>Ƕ˵<C7B6><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
&& ((((PUSB_ENDP_DESCR)(buf + i))->bmAttributes & USB_ENDP_TYPE_MASK) == USB_ENDP_TYPE_BULK)) // <20><><EFBFBD>ж϶˵<CFB6>
{
if(HubPortIndex)
{
if(((PUSB_ENDP_DESCR)(buf + i))->bEndpointAddress & USB_ENDP_DIR_MASK)
{
DevOnU2HubPort[HubPortIndex - 1].GpVar[s1++] = ((PUSB_ENDP_DESCR)(buf + i))->bEndpointAddress & USB_ENDP_ADDR_MASK;
}
else
{
DevOnU2HubPort[HubPortIndex - 1].GpVar[s2++] = ((PUSB_ENDP_DESCR)(buf + i))->bEndpointAddress & USB_ENDP_ADDR_MASK;
}
}
else
{
if(((PUSB_ENDP_DESCR)(buf + i))->bEndpointAddress & USB_ENDP_DIR_MASK)
{
ThisUsb2Dev.GpVar[s1++] = ((PUSB_ENDP_DESCR)(buf + i))->bEndpointAddress & USB_ENDP_ADDR_MASK;
}
else
{
ThisUsb2Dev.GpVar[s2++] = ((PUSB_ENDP_DESCR)(buf + i))->bEndpointAddress & USB_ENDP_ADDR_MASK;
}
}
if(s1 == 2)
{
s1 = 1;
}
if(s2 == 4)
{
s2 = 3;
}
}
l = ((PUSB_ENDP_DESCR)(buf + i))->bLength; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>
if(l > 16)
{
break;
}
}
return (0);
}
/*********************************************************************
* @fn InitRootU2Device
*
* @brief <20><>ʼ<EFBFBD><CABC>ָ<EFBFBD><D6B8>ROOT-HUB<55>˿ڵ<CBBF>USB<53>
*
* @param none
*
* @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
uint8_t InitRootU2Device(void)
{
uint8_t i, s;
uint8_t cfg, dv_cls, if_cls;
PRINT("Reset U2 host port\n");
ResetRootU2HubPort(); // <20><><EFBFBD><EFBFBD><EFBFBD><E8B1B8>,<2C><>λ<EFBFBD><CEBB>Ӧ<EFBFBD>˿ڵ<CBBF>USB<53><42><EFBFBD><EFBFBD>
for(i = 0, s = 0; i < 100; i++)
{ // <20>ȴ<EFBFBD>USB<53><EFBFBD><E8B1B8>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,100mS<6D><53>ʱ
mDelaymS(1);
if(EnableRootU2HubPort() == ERR_SUCCESS)
{ // ʹ<>ܶ˿<DCB6>
i = 0;
s++;
if(s > 100)
break; // <20>Ѿ<EFBFBD><D1BE>ȶ<EFBFBD><C8B6><EFBFBD><EFBFBD><EFBFBD>100mS
}
}
if(i)
{ // <20><>λ<EFBFBD><CEBB><EFBFBD>豸û<E8B1B8><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
DisableRootU2HubPort();
PRINT("Disable U2 host port because of disconnect\n");
return (ERR_USB_DISCON);
}
SetUsb2Speed(ThisUsb2Dev.DeviceSpeed); // <20><><EFBFBD>õ<EFBFBD>ǰUSB<53>ٶ<EFBFBD>
PRINT("GetU2DevDescr: ");
s = CtrlGetU2DeviceDescr(); // <20><>ȡ<EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(s == ERR_SUCCESS)
{
for(i = 0; i < ((PUSB_SETUP_REQ)SetupGetU2DevDescr)->wLength; i++)
PRINT("x%02X ", (uint16_t)(U2Com_Buffer[i]));
PRINT("\n");
ThisUsb2Dev.DeviceVID = ((PUSB_DEV_DESCR)U2Com_Buffer)->idVendor; //<2F><><EFBFBD><EFBFBD>VID PID<49><44>Ϣ
ThisUsb2Dev.DevicePID = ((PUSB_DEV_DESCR)U2Com_Buffer)->idProduct;
dv_cls = ((PUSB_DEV_DESCR)U2Com_Buffer)->bDeviceClass;
s = CtrlSetUsb2Address(((PUSB_SETUP_REQ)SetupSetUsb2Addr)->wValue);
if(s == ERR_SUCCESS)
{
ThisUsb2Dev.DeviceAddress = ((PUSB_SETUP_REQ)SetupSetUsb2Addr)->wValue; // <20><><EFBFBD><EFBFBD>USB<53><42>ַ
PRINT("GetU2CfgDescr: ");
s = CtrlGetU2ConfigDescr();
if(s == ERR_SUCCESS)
{
for(i = 0; i < ((PUSB_CFG_DESCR)U2Com_Buffer)->wTotalLength; i++)
{
PRINT("x%02X ", (uint16_t)(U2Com_Buffer[i]));
}
PRINT("\n");
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><>ȡ<EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD>/<2F><><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>ַ/<2F><><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>С<EFBFBD><D0A1>,<2C><><EFBFBD>±<EFBFBD><C2B1><EFBFBD>endp_addr<64><72>endp_size<7A><65> */
cfg = ((PUSB_CFG_DESCR)U2Com_Buffer)->bConfigurationValue;
if_cls = ((PUSB_CFG_DESCR_LONG)U2Com_Buffer)->itf_descr.bInterfaceClass; // <20>ӿ<EFBFBD><D3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if((dv_cls == 0x00) && (if_cls == USB_DEV_CLASS_STORAGE))
{ // <20><>USB<53><EFBFBD><E6B4A2><EFBFBD>豸,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>U<EFBFBD><55>
#ifdef FOR_ROOT_UDISK_ONLY
CHRV3DiskStatus = DISK_USB_ADDR;
return (ERR_SUCCESS);
}
else
return (ERR_USB_UNSUPPORT);
#else
s = CtrlSetUsb2Config(cfg); // <20><><EFBFBD><EFBFBD>USB<53><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
if(s == ERR_SUCCESS)
{
ThisUsb2Dev.DeviceStatus = ROOT_DEV_SUCCESS;
ThisUsb2Dev.DeviceType = USB_DEV_CLASS_STORAGE;
PRINT("U2 USB-Disk Ready\n");
SetUsb2Speed(1); // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>
return (ERR_SUCCESS);
}
}
else if((dv_cls == 0x00) && (if_cls == USB_DEV_CLASS_PRINTER) && ((PUSB_CFG_DESCR_LONG)U2Com_Buffer)->itf_descr.bInterfaceSubClass == 0x01)
{ // <20>Ǵ<EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD>
s = CtrlSetUsb2Config(cfg); // <20><><EFBFBD><EFBFBD>USB<53><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
if(s == ERR_SUCCESS)
{
// <09><EFBFBD><E8B1A3><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>Ϣ<EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<53><42><EFBFBD><EFBFBD>
ThisUsb2Dev.DeviceStatus = ROOT_DEV_SUCCESS;
ThisUsb2Dev.DeviceType = USB_DEV_CLASS_PRINTER;
PRINT("U2 USB-Print Ready\n");
SetUsb2Speed(1); // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>
return (ERR_SUCCESS);
}
}
else if((dv_cls == 0x00) && (if_cls == USB_DEV_CLASS_HID) && ((PUSB_CFG_DESCR_LONG)U2Com_Buffer)->itf_descr.bInterfaceSubClass <= 0x01)
{ // <20><>HID<49><44><EFBFBD>豸,<2C><><EFBFBD><EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><D0B7><EFBFBD><EFBFBD><EFBFBD>HID<49>ж϶˵<CFB6><CBB5>ĵ<EFBFBD>ַ
s = AnalyzeU2HidIntEndp(U2Com_Buffer, 0); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><D0B7><EFBFBD><EFBFBD><EFBFBD>HID<49>ж϶˵<CFB6><CBB5>ĵ<EFBFBD>ַ
PRINT("AnalyzeU2HidIntEndp %02x\n", (uint16_t)s);
// <20><><EFBFBD><EFBFBD><EFBFBD>ж϶˵<CFB6><CBB5>ĵ<EFBFBD>ַ,λ7<CEBB><37><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD>־λ,<2C><>0
if_cls = ((PUSB_CFG_DESCR_LONG)U2Com_Buffer)->itf_descr.bInterfaceProtocol;
s = CtrlSetUsb2Config(cfg); // <20><><EFBFBD><EFBFBD>USB<53><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
if(s == ERR_SUCCESS)
{
// Set_Idle( );
// <09><EFBFBD><E8B1A3><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>Ϣ<EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<53><42><EFBFBD><EFBFBD>
ThisUsb2Dev.DeviceStatus = ROOT_DEV_SUCCESS;
if(if_cls == 1)
{
ThisUsb2Dev.DeviceType = DEV_TYPE_KEYBOARD;
// <09><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>ָʾ<D6B8><CABE>LED<45><44>
PRINT("U2 USB-Keyboard Ready\n");
SetUsb2Speed(1); // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>
return (ERR_SUCCESS);
}
else if(if_cls == 2)
{
ThisUsb2Dev.DeviceType = DEV_TYPE_MOUSE;
// Ϊ<><CEAA><EFBFBD>Ժ<EFBFBD><D4BA><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>״̬,Ӧ<>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><><C8A1><EFBFBD>ж϶˿ڵĵ<DAB5>ַ,<2C><><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD>Ϣ
PRINT("U2 USB-Mouse Ready\n");
SetUsb2Speed(1); // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>
return (ERR_SUCCESS);
}
s = ERR_USB_UNSUPPORT;
}
}
else if(dv_cls == USB_DEV_CLASS_HUB)
{ // <20><>HUB<55><42><EFBFBD>豸,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
s = CtrlGetU2HubDescr();
if(s == ERR_SUCCESS)
{
PRINT("Max Port:%02X ", (((PXUSB_HUB_DESCR)U2Com_Buffer)->bNbrPorts));
ThisUsb2Dev.GpHUBPortNum = ((PXUSB_HUB_DESCR)U2Com_Buffer)->bNbrPorts; // <20><><EFBFBD><EFBFBD>HUB<55>Ķ˿<C4B6><CBBF><EFBFBD><EFBFBD><EFBFBD>
if(ThisUsb2Dev.GpHUBPortNum > HUB_MAX_PORTS)
{
ThisUsb2Dev.GpHUBPortNum = HUB_MAX_PORTS; // <20><>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>ṹDevOnHubPortʱ<74><CAB1>Ϊ<EFBFBD>ٶ<EFBFBD>ÿ<EFBFBD><C3BF>HUB<55><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD>HUB_MAX_PORTS<54><53><EFBFBD>˿<EFBFBD>
}
s = CtrlSetUsb2Config(cfg); // <20><><EFBFBD><EFBFBD>USB<53><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
if(s == ERR_SUCCESS)
{
ThisUsb2Dev.DeviceStatus = ROOT_DEV_SUCCESS;
ThisUsb2Dev.DeviceType = USB_DEV_CLASS_HUB;
//<2F><EFBFBD><E8B1A3><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>Ϣ<EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<53><42><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD>ж϶˵<CFB6><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>HUB<55>¼<EFBFBD>֪ͨ,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ò<EFBFBD>ѯ״̬<D7B4><CCAC><EFBFBD>ƴ<EFBFBD><C6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><>HUB<55><42><EFBFBD>˿<EFBFBD><CBBF>ϵ<EFBFBD>,<2C><>ѯ<EFBFBD><D1AF><EFBFBD>˿<EFBFBD>״̬,<2C><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD>ӵ<EFBFBD>HUB<55>˿<EFBFBD>,<2C><>ʼ<EFBFBD><CABC><EFBFBD>
for(i = 1; i <= ThisUsb2Dev.GpHUBPortNum; i++) // <20><>HUB<55><42><EFBFBD>˿ڶ<CBBF><DAB6>ϵ<EFBFBD>
{
DevOnU2HubPort[i - 1].DeviceStatus = ROOT_DEV_DISCONNECT; // <20><><EFBFBD>ⲿHUB<55>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD><E8B1B8>״̬
s = U2HubSetPortFeature(i, HUB_PORT_POWER);
if(s != ERR_SUCCESS)
{
PRINT("Ext-HUB Port_%1d# power on error\n", (uint16_t)i); // <20>˿<EFBFBD><CBBF>ϵ<EFBFBD>ʧ<EFBFBD><CAA7>
}
}
PRINT("U2 USB-HUB Ready\n");
SetUsb2Speed(1); // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>
return (ERR_SUCCESS);
}
}
}
else
{ // <20><><EFBFBD>Խ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
s = CtrlSetUsb2Config(cfg); // <20><><EFBFBD><EFBFBD>USB<53><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
if(s == ERR_SUCCESS)
{
// <09><EFBFBD><E8B1A3><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>Ϣ<EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<53><42><EFBFBD><EFBFBD>
ThisUsb2Dev.DeviceStatus = ROOT_DEV_SUCCESS;
ThisUsb2Dev.DeviceType = DEV_TYPE_UNKNOW;
SetUsb2Speed(1); // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>
return (ERR_SUCCESS); /* δ֪<CEB4><EFBFBD><E8B1B8>ʼ<EFBFBD><CABC><EFBFBD>ɹ<EFBFBD> */
}
}
#endif
}
}
}
PRINT("InitRootU2Dev Err = %02X\n", (uint16_t)s);
#ifdef FOR_ROOT_UDISK_ONLY
CHRV3DiskStatus = DISK_CONNECT;
#else
ThisUsb2Dev.DeviceStatus = ROOT_DEV_FAILED;
#endif
SetUsb2Speed(1); // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>
return (s);
}
/*********************************************************************
* @fn InitU2DevOnHub
*
* @brief <20><>ʼ<EFBFBD><CABC>ö<EFBFBD><C3B6><EFBFBD>ⲿHUB<55><42><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>USB<53>
*
* @param HubPortIndex - ָ<><D6B8><EFBFBD>ⲿHUB
*
* @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
uint8_t InitU2DevOnHub(uint8_t HubPortIndex)
{
uint8_t i, s, cfg, dv_cls, if_cls;
uint8_t ifc;
PRINT("Init dev @ExtHub-port_%1d ", (uint16_t)HubPortIndex);
if(HubPortIndex == 0)
{
return (ERR_USB_UNKNOWN);
}
SelectU2HubPort(HubPortIndex); // ѡ<><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ROOT-HUB<55>˿ڵ<CBBF><DAB5>ⲿHUB<55><42>ָ<EFBFBD><D6B8><EFBFBD>˿<EFBFBD>,ѡ<><D1A1><EFBFBD>ٶ<EFBFBD>
PRINT("GetDevDescr: ");
s = CtrlGetU2DeviceDescr(); // <20><>ȡ<EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
DevOnU2HubPort[HubPortIndex - 1].DeviceVID = ((uint16_t)((PUSB_DEV_DESCR)U2Com_Buffer)->idVendor); //<2F><><EFBFBD><EFBFBD>VID PID<49><44>Ϣ
DevOnU2HubPort[HubPortIndex - 1].DevicePID = ((uint16_t)((PUSB_DEV_DESCR)U2Com_Buffer)->idProduct);
dv_cls = ((PUSB_DEV_DESCR)U2Com_Buffer)->bDeviceClass; // <20><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
cfg = (1 << 4) + HubPortIndex; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>USB<53><42>ַ,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>ص<EFBFBD>
s = CtrlSetUsb2Address(cfg); // <20><><EFBFBD><EFBFBD>USB<53><EFBFBD><E8B1B8>ַ
if(s != ERR_SUCCESS)
{
return (s);
}
DevOnU2HubPort[HubPortIndex - 1].DeviceAddress = cfg; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<53><42>ַ
PRINT("GetCfgDescr: ");
s = CtrlGetU2ConfigDescr(); // <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
cfg = ((PUSB_CFG_DESCR)U2Com_Buffer)->bConfigurationValue;
for(i = 0; i < ((PUSB_CFG_DESCR)U2Com_Buffer)->wTotalLength; i++)
{
PRINT("x%02X ", (uint16_t)(U2Com_Buffer[i]));
}
PRINT("\n");
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><>ȡ<EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD>/<2F><><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>ַ/<2F><><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>С<EFBFBD><D0A1>,<2C><><EFBFBD>±<EFBFBD><C2B1><EFBFBD>endp_addr<64><72>endp_size<7A><65> */
if_cls = ((PXUSB_CFG_DESCR_LONG)U2Com_Buffer)->itf_descr.bInterfaceClass; // <20>ӿ<EFBFBD><D3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(dv_cls == 0x00 && if_cls == USB_DEV_CLASS_STORAGE) // <20><>USB<53><EFBFBD><E6B4A2><EFBFBD>豸,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>U<EFBFBD><55>
{
AnalyzeU2BulkEndp(U2Com_Buffer, HubPortIndex);
for(i = 0; i != 4; i++)
{
PRINT("%02x ", (uint16_t)DevOnU2HubPort[HubPortIndex - 1].GpVar[i]);
}
PRINT("\n");
s = CtrlSetUsb2Config(cfg); // <20><><EFBFBD><EFBFBD>USB<53><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
if(s == ERR_SUCCESS)
{
DevOnU2HubPort[HubPortIndex - 1].DeviceStatus = ROOT_DEV_SUCCESS;
DevOnU2HubPort[HubPortIndex - 1].DeviceType = USB_DEV_CLASS_STORAGE;
PRINT("USB-Disk Ready\n");
SetUsb2Speed(1); // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>
return (ERR_SUCCESS);
}
}
else if((dv_cls == 0x00) && (if_cls == USB_DEV_CLASS_HID) && (((PXUSB_CFG_DESCR_LONG)U2Com_Buffer)->itf_descr.bInterfaceSubClass <= 0x01)) // <20><>HID<49><44><EFBFBD>豸,<2C><><EFBFBD><EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
ifc = ((PXUSB_CFG_DESCR_LONG)U2Com_Buffer)->cfg_descr.bNumInterfaces;
s = AnalyzeU2HidIntEndp(U2Com_Buffer, HubPortIndex); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><D0B7><EFBFBD><EFBFBD><EFBFBD>HID<49>ж϶˵<CFB6><CBB5>ĵ<EFBFBD>ַ
PRINT("AnalyzeU2HidIntEndp %02x\n", (uint16_t)s);
if_cls = ((PXUSB_CFG_DESCR_LONG)U2Com_Buffer)->itf_descr.bInterfaceProtocol;
s = CtrlSetUsb2Config(cfg); // <20><><EFBFBD><EFBFBD>USB<53><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
if(s == ERR_SUCCESS)
{
for(dv_cls = 0; dv_cls < ifc; dv_cls++)
{
s = CtrlGetU2HIDDeviceReport(dv_cls); //<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(s == ERR_SUCCESS)
{
for(i = 0; i < 64; i++)
{
PRINT("x%02X ", (uint16_t)(U2Com_Buffer[i]));
}
PRINT("\n");
}
}
//<2F><EFBFBD><E8B1A3><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>Ϣ<EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<53><42><EFBFBD><EFBFBD>
DevOnU2HubPort[HubPortIndex - 1].DeviceStatus = ROOT_DEV_SUCCESS;
if(if_cls == 1)
{
DevOnU2HubPort[HubPortIndex - 1].DeviceType = DEV_TYPE_KEYBOARD;
//<2F><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>ָʾ<D6B8><CABE>LED<45><44>
if(ifc > 1)
{
PRINT("USB_DEV_CLASS_HID Ready\n");
// DevOnU2HubPort[HubPortIndex - 1].DeviceType = USB_DEV_CLASS_HID; //<2F><><EFBFBD><EFBFBD>HID<49>
}
PRINT("USB-Keyboard Ready\n");
SetUsb2Speed(1); // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>
return (ERR_SUCCESS);
}
else if(if_cls == 2)
{
DevOnU2HubPort[HubPortIndex - 1].DeviceType = DEV_TYPE_MOUSE;
//Ϊ<><CEAA><EFBFBD>Ժ<EFBFBD><D4BA><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>״̬,Ӧ<>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><><C8A1><EFBFBD>ж϶˿ڵĵ<DAB5>ַ,<2C><><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD>Ϣ
if(ifc > 1)
{
PRINT("USB_DEV_CLASS_HID Ready\n");
// DevOnU2HubPort[HubPortIndex - 1].DeviceType = USB_DEV_CLASS_HID; //<2F><><EFBFBD><EFBFBD>HID<49>
}
PRINT("USB-Mouse Ready\n");
SetUsb2Speed(1); // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>
return (ERR_SUCCESS);
}
s = ERR_USB_UNSUPPORT;
}
}
else if(dv_cls == USB_DEV_CLASS_HUB) // <20><>HUB<55><42><EFBFBD>豸,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
DevOnU2HubPort[HubPortIndex - 1].DeviceType = USB_DEV_CLASS_HUB;
PRINT("This program don't support Level 2 HUB\n"); // <20><>Ҫ֧<D2AA>ֶ༶HUB<55><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ
s = U2HubClearPortFeature(i, HUB_PORT_ENABLE); // <20><>ֹHUB<55>˿<EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
s = ERR_USB_UNSUPPORT;
}
else //<2F><><EFBFBD><EFBFBD><EFBFBD>
{
AnalyzeU2BulkEndp(U2Com_Buffer, HubPortIndex); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD>
for(i = 0; i != 4; i++)
{
PRINT("%02x ", (uint16_t)DevOnU2HubPort[HubPortIndex - 1].GpVar[i]);
}
PRINT("\n");
s = CtrlSetUsb2Config(cfg); // <20><><EFBFBD><EFBFBD>USB<53><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
if(s == ERR_SUCCESS)
{
//<2F><EFBFBD><E8B1A3><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>Ϣ<EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<53><42><EFBFBD><EFBFBD>
DevOnU2HubPort[HubPortIndex - 1].DeviceStatus = ROOT_DEV_SUCCESS;
DevOnU2HubPort[HubPortIndex - 1].DeviceType = dv_cls ? dv_cls : if_cls;
SetUsb2Speed(1); // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>
return (ERR_SUCCESS); //δ֪<CEB4><EFBFBD><E8B1B8>ʼ<EFBFBD><CABC><EFBFBD>ɹ<EFBFBD>
}
}
PRINT("InitDevOnHub Err = %02X\n", (uint16_t)s);
DevOnU2HubPort[HubPortIndex - 1].DeviceStatus = ROOT_DEV_FAILED;
SetUsb2Speed(1); // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>
return (s);
}
/*********************************************************************
* @fn EnumU2HubPort
*
* @brief ö<><C3B6>ָ<EFBFBD><D6B8>ROOT-HUB<55>˿<EFBFBD><CBBF>ϵ<EFBFBD><CFB5>ⲿHUB<55><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD>˿<EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD><D3BB>Ƴ<EFBFBD><C6B3>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<53>
*
* @param RootHubIndex - ROOT_HUB0<42><30>ROOT_HUB1
*
* @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
uint8_t EnumU2HubPort()
{
uint8_t i, s;
for(i = 1; i <= ThisUsb2Dev.GpHUBPortNum; i++) // <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ˿<C4B6><CBBF>Ƿ<EFBFBD><C7B7>б
{
SelectU2HubPort(0); // ѡ<><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ROOT-HUB<55>˿<EFBFBD>,<2C><><EFBFBD>õ<EFBFBD>ǰUSB<53>ٶ<EFBFBD><D9B6>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8>USB<53><42>ַ
s = U2HubGetPortStatus(i); // <20><>ȡ<EFBFBD>˿<EFBFBD>״̬
if(s != ERR_SUCCESS)
{
return (s); // <20><><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD>HUB<55>Ͽ<EFBFBD><CFBF><EFBFBD>
}
if(((U2Com_Buffer[0] & (1 << (HUB_PORT_CONNECTION & 0x07))) && (U2Com_Buffer[2] & (1 << (HUB_C_PORT_CONNECTION & 0x07)))) || (U2Com_Buffer[2] == 0x10))
{ // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
DevOnU2HubPort[i - 1].DeviceStatus = ROOT_DEV_CONNECTED; // <20><><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
DevOnU2HubPort[i - 1].DeviceAddress = 0x00;
s = U2HubGetPortStatus(i); // <20><>ȡ<EFBFBD>˿<EFBFBD>״̬
if(s != ERR_SUCCESS)
{
return (s); // <20><><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD>HUB<55>Ͽ<EFBFBD><CFBF><EFBFBD>
}
DevOnU2HubPort[i - 1].DeviceSpeed = U2Com_Buffer[1] & (1 << (HUB_PORT_LOW_SPEED & 0x07)) ? 0 : 1; // <20><><EFBFBD>ٻ<EFBFBD><D9BB><EFBFBD>ȫ<EFBFBD><C8AB>
if(DevOnU2HubPort[i - 1].DeviceSpeed)
{
PRINT("Found full speed device on port %1d\n", (uint16_t)i);
}
else
{
PRINT("Found low speed device on port %1d\n", (uint16_t)i);
}
mDelaymS(200); // <20>ȴ<EFBFBD><C8B4><EFBFBD>ϵ<EFBFBD><CFB5>ȶ<EFBFBD>
s = U2HubSetPortFeature(i, HUB_PORT_RESET); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD>ӵĶ˿ڸ<CBBF>λ
if(s != ERR_SUCCESS)
{
return (s); // <20><><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD>HUB<55>Ͽ<EFBFBD><CFBF><EFBFBD>
}
PRINT("Reset port and then wait in\n");
do // <20><>ѯ<EFBFBD><D1AF>λ<EFBFBD>˿<EFBFBD><><D6B1><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD><C9BA><EFBFBD>״̬<D7B4><CCAC>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
{
mDelaymS(1);
s = U2HubGetPortStatus(i);
if(s != ERR_SUCCESS)
{
return (s); // <20><><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD>HUB<55>Ͽ<EFBFBD><CFBF><EFBFBD>
}
} while(U2Com_Buffer[0] & (1 << (HUB_PORT_RESET & 0x07))); // <20>˿<EFBFBD><CBBF><EFBFBD><EFBFBD>ڸ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD>ȴ<EFBFBD>
mDelaymS(100);
s = U2HubClearPortFeature(i, HUB_C_PORT_RESET); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD>ɱ<EFBFBD>־
// s = U2HubSetPortFeature( i, HUB_PORT_ENABLE ); // <20><><EFBFBD><EFBFBD>HUB<55>˿<EFBFBD>
s = U2HubClearPortFeature(i, HUB_C_PORT_CONNECTION); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD><D3BB>Ƴ<EFBFBD><C6B3><EFBFBD><E4BBAF>־
if(s != ERR_SUCCESS)
{
return (s);
}
s = U2HubGetPortStatus(i); // <20>ٶ<EFBFBD>ȡ״̬,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
if((U2Com_Buffer[0] & (1 << (HUB_PORT_CONNECTION & 0x07))) == 0)
{
DevOnU2HubPort[i - 1].DeviceStatus = ROOT_DEV_DISCONNECT; // <20><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
s = InitU2DevOnHub(i); // <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<53>
if(s != ERR_SUCCESS)
{
return (s);
}
SetUsb2Speed(1); // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>
}
else if(U2Com_Buffer[2] & (1 << (HUB_C_PORT_ENABLE & 0x07))) // <20><EFBFBD><E8B1B8><EFBFBD>ӳ<EFBFBD><D3B3><EFBFBD>
{
U2HubClearPortFeature(i, HUB_C_PORT_ENABLE); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><D3B4><EFBFBD><EFBFBD><EFBFBD>־
PRINT("Device on port error\n");
s = U2HubSetPortFeature(i, HUB_PORT_RESET); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD>ӵĶ˿ڸ<CBBF>λ
if(s != ERR_SUCCESS)
return (s); // <20><><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD>HUB<55>Ͽ<EFBFBD><CFBF><EFBFBD>
do // <20><>ѯ<EFBFBD><D1AF>λ<EFBFBD>˿<EFBFBD><><D6B1><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD><C9BA><EFBFBD>״̬<D7B4><CCAC>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
{
mDelaymS(1);
s = U2HubGetPortStatus(i);
if(s != ERR_SUCCESS)
return (s); // <20><><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD>HUB<55>Ͽ<EFBFBD><CFBF><EFBFBD>
} while(U2Com_Buffer[0] & (1 << (HUB_PORT_RESET & 0x07))); // <20>˿<EFBFBD><CBBF><EFBFBD><EFBFBD>ڸ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD>ȴ<EFBFBD>
}
else if((U2Com_Buffer[0] & (1 << (HUB_PORT_CONNECTION & 0x07))) == 0) // <20><EFBFBD>Ѿ<EFBFBD><D1BE>Ͽ<EFBFBD>
{
if(DevOnU2HubPort[i - 1].DeviceStatus >= ROOT_DEV_CONNECTED)
{
PRINT("Device on port %1d removed\n", (uint16_t)i);
}
DevOnU2HubPort[i - 1].DeviceStatus = ROOT_DEV_DISCONNECT; // <20><><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
if(U2Com_Buffer[2] & (1 << (HUB_C_PORT_CONNECTION & 0x07)))
{
U2HubClearPortFeature(i, HUB_C_PORT_CONNECTION); // <20><><EFBFBD><EFBFBD><EFBFBD>Ƴ<EFBFBD><C6B3><EFBFBD><E4BBAF>־
}
}
}
return (ERR_SUCCESS); // <20><><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD>ɹ<EFBFBD>
}
/*********************************************************************
* @fn EnumAllU2HubPort
*
* @brief ö<><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ROOT-HUB<55>˿<EFBFBD><CBBF><EFBFBD><EFBFBD>ⲿHUB<55><42><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>USB<53>
*
* @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
uint8_t EnumAllU2HubPort(void)
{
uint8_t s;
if((ThisUsb2Dev.DeviceStatus >= ROOT_DEV_SUCCESS) && (ThisUsb2Dev.DeviceType == USB_DEV_CLASS_HUB)) // HUBö<42>ٳɹ<D9B3>
{
SelectU2HubPort(0); // ѡ<><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ROOT-HUB<55>˿<EFBFBD>,<2C><><EFBFBD>õ<EFBFBD>ǰUSB<53>ٶ<EFBFBD><D9B6>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8>USB<53><42>ַ
s = EnumU2HubPort(); // ö<><C3B6>ָ<EFBFBD><D6B8>ROOT-HUB<55>˿<EFBFBD><CBBF>ϵ<EFBFBD><CFB5>ⲿHUB<55><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD>˿<EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD><D3BB>Ƴ<EFBFBD><C6B3>¼<EFBFBD>
if(s != ERR_SUCCESS) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>HUB<55>Ͽ<EFBFBD><CFBF><EFBFBD>
{
PRINT("EnumAllHubPort err = %02X\n", (uint16_t)s);
}
SetUsb2Speed(1); // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>
}
return (ERR_SUCCESS);
}
/*********************************************************************
* @fn U2SearchTypeDevice
*
* @brief <20><>ROOT-HUB<55>Լ<EFBFBD><D4BC>ⲿHUB<55><42><EFBFBD>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><E8B1B8><EFBFBD>ڵĶ˿ں<CBBF>,<2C><><EFBFBD><EFBFBD><EFBFBD>˿ں<CBBF>Ϊ0xFFFF<46><46>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
* <20><>ȻҲ<C8BB><D2B2><EFBFBD>Ը<EFBFBD><D4B8><EFBFBD>USB<53>ij<EFBFBD><C4B3><EFBFBD>VID<49><44>ƷPID<49><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><E8B1B8>VID<49><44>PID),<2C>Լ<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @param type - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
*
* @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>8λΪROOT-HUB<55>˿ں<CBBF>,<2C><>8λΪ<CEBB>ⲿHUB<55>Ķ˿ں<CBBF>,<2C><>8λΪ0<CEAA><30><EFBFBD>豸ֱ<E8B1B8><D6B1><EFBFBD><EFBFBD>ROOT-HUB<55>˿<EFBFBD><CBBF><EFBFBD>
*/
uint16_t U2SearchTypeDevice(uint8_t type)
{
uint8_t RootHubIndex; //CH554ֻ<34><D6BB>һ<EFBFBD><D2BB>USB<53><42>,RootHubIndex = 0,ֻ<><EFBFBD><E8BFB4><EFBFBD><EFBFBD>ֵ<EFBFBD>ĵͰ<C4B5>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>
uint8_t HubPortIndex;
RootHubIndex = 0;
if((ThisUsb2Dev.DeviceType == USB_DEV_CLASS_HUB) && (ThisUsb2Dev.DeviceStatus >= ROOT_DEV_SUCCESS)) // <20>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>HUB<55><42>ö<EFBFBD>ٳɹ<D9B3>
{
for(HubPortIndex = 1; HubPortIndex <= ThisUsb2Dev.GpHUBPortNum; HubPortIndex++) // <20><><EFBFBD><EFBFBD><EFBFBD>ⲿHUB<55>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD>˿<EFBFBD>
{
if(DevOnU2HubPort[HubPortIndex - 1].DeviceType == type && DevOnU2HubPort[HubPortIndex - 1].DeviceStatus >= ROOT_DEV_SUCCESS)
{
return (((uint16_t)RootHubIndex << 8) | HubPortIndex); // <20><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>ö<EFBFBD>ٳɹ<D9B3>
}
}
}
if((ThisUsb2Dev.DeviceType == type) && (ThisUsb2Dev.DeviceStatus >= ROOT_DEV_SUCCESS))
{
return ((uint16_t)RootHubIndex << 8); // <20><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>ö<EFBFBD>ٳɹ<D9B3>,<2C><>ROOT-HUB<55>˿<EFBFBD><CBBF><EFBFBD>
}
return (0xFFFF);
}
/*********************************************************************
* @fn U2SETorOFFNumLock
*
* @brief NumLock<63>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD>ж<EFBFBD>
*
* @param buf - <20><><EFBFBD>Ƽ<EFBFBD>ֵ
*
* @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
uint8_t U2SETorOFFNumLock(uint8_t *buf)
{
uint8_t tmp[] = {0x21, 0x09, 0x00, 0x02, 0x00, 0x00, 0x01, 0x00};
uint8_t len, s;
if((buf[2] == 0x53) & (buf[0] | buf[1] | buf[3] | buf[4] | buf[5] | buf[6] | buf[7] == 0))
{
for(s = 0; s != sizeof(tmp); s++)
{
((uint8_t *)pU2SetupReq)[s] = tmp[s];
}
s = U2HostCtrlTransfer(U2Com_Buffer, &len); // ִ<>п<EFBFBD><D0BF>ƴ<EFBFBD><C6B4><EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
}
return (ERR_SUCCESS);
}
/*********************************************************************
* @fn CtrlGetU2HIDDeviceReport
*
* @brief <20><>ȡHID<49><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>TxBuffer<65><72>
*
* @param none
*
* @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
uint8_t CtrlGetU2HIDDeviceReport(uint8_t infc)
{
uint8_t s;
uint8_t len;
CopyU2SetupReqPkg((uint8_t *)SetupSetU2HIDIdle);
pU2SetupReq->wIndex = infc;
s = U2HostCtrlTransfer(U2Com_Buffer, &len); // ִ<>п<EFBFBD><D0BF>ƴ<EFBFBD><C6B4><EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
CopyU2SetupReqPkg((uint8_t *)SetupGetU2HIDDevReport);
pU2SetupReq->wIndex = infc;
s = U2HostCtrlTransfer(U2Com_Buffer, &len); // ִ<>п<EFBFBD><D0BF>ƴ<EFBFBD><C6B4><EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
return (ERR_SUCCESS);
}
/*********************************************************************
* @fn CtrlGetU2HubDescr
*
* @brief <20><>ȡHUB<55><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Com_Buffer<65><72>
*
* @param none
*
* @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
uint8_t CtrlGetU2HubDescr(void)
{
uint8_t s;
uint8_t len;
CopyU2SetupReqPkg((uint8_t *)SetupGetU2HubDescr);
s = U2HostCtrlTransfer(U2Com_Buffer, &len); // ִ<>п<EFBFBD><D0BF>ƴ<EFBFBD><C6B4><EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
if(len < ((PUSB_SETUP_REQ)SetupGetU2HubDescr)->wLength)
{
return (ERR_USB_BUF_OVER); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>
}
// if ( len < 4 ) return( ERR_USB_BUF_OVER ); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>
return (ERR_SUCCESS);
}
/*********************************************************************
* @fn U2HubGetPortStatus
*
* @brief <20><>ѯHUB<55>˿<EFBFBD>״̬,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Com_Buffer<65><72>
*
* @param HubPortIndex - <20>˿ں<CBBF>
*
* @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
uint8_t U2HubGetPortStatus(uint8_t HubPortIndex)
{
uint8_t s;
uint8_t len;
pU2SetupReq->bRequestType = HUB_GET_PORT_STATUS;
pU2SetupReq->bRequest = HUB_GET_STATUS;
pU2SetupReq->wValue = 0x0000;
pU2SetupReq->wIndex = 0x0000 | HubPortIndex;
pU2SetupReq->wLength = 0x0004;
s = U2HostCtrlTransfer(U2Com_Buffer, &len); // ִ<>п<EFBFBD><D0BF>ƴ<EFBFBD><C6B4><EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
if(len < 4)
{
return (ERR_USB_BUF_OVER); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>
}
return (ERR_SUCCESS);
}
/*********************************************************************
* @fn U2HubSetPortFeature
*
* @brief <20><><EFBFBD><EFBFBD>HUB<55>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD>
*
* @param HubPortIndex - <20>˿ں<CBBF>
* @param FeatureSelt - <20>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD>
*
* @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
uint8_t U2HubSetPortFeature(uint8_t HubPortIndex, uint8_t FeatureSelt)
{
pU2SetupReq->bRequestType = HUB_SET_PORT_FEATURE;
pU2SetupReq->bRequest = HUB_SET_FEATURE;
pU2SetupReq->wValue = 0x0000 | FeatureSelt;
pU2SetupReq->wIndex = 0x0000 | HubPortIndex;
pU2SetupReq->wLength = 0x0000;
return (U2HostCtrlTransfer(NULL, NULL)); // ִ<>п<EFBFBD><D0BF>ƴ<EFBFBD><C6B4><EFBFBD>
}
/*********************************************************************
* @fn U2HubClearPortFeature
*
* @brief <20><><EFBFBD><EFBFBD>HUB<55>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD>
*
* @param HubPortIndex - <20>˿ں<CBBF>
* @param FeatureSelt - <20>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD>
*
* @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
uint8_t U2HubClearPortFeature(uint8_t HubPortIndex, uint8_t FeatureSelt)
{
pU2SetupReq->bRequestType = HUB_CLEAR_PORT_FEATURE;
pU2SetupReq->bRequest = HUB_CLEAR_FEATURE;
pU2SetupReq->wValue = 0x0000 | FeatureSelt;
pU2SetupReq->wIndex = 0x0000 | HubPortIndex;
pU2SetupReq->wLength = 0x0000;
return (U2HostCtrlTransfer(NULL, NULL)); // ִ<>п<EFBFBD><D0BF>ƴ<EFBFBD><C6B4><EFBFBD>
}