3 * - By John Hodge (thePowersGang)
11 #define MAX_PORTS 32 // Not actually a max, but used for DeviceRemovable
16 #define PORT_CONNECTION 0
24 Uint8 DescType; // = 0x29
26 Uint16 HubCharacteristics;
27 Uint8 PwrOn2PwrGood; // 2 ms intervals
28 Uint8 HubContrCurrent; // Max internal current (mA)
29 Uint8 DeviceRemovable[MAX_PORTS];
35 int PowerOnDelay; // in ms
37 Uint8 DeviceRemovable[];
41 void Hub_Connected(tUSBInterface *Dev);
42 void Hub_Disconnected(tUSBInterface *Dev);
43 void Hub_PortStatusChange(tUSBInterface *Dev, int Endpoint, int Length, void *Data);
44 void Hub_int_HandleChange(tUSBInterface *Dev, int Port);
47 tUSBDriver gUSBHub_Driver = {
49 .Match = {.Class = {0x090000, 0xFF0000}},
50 .Connected = Hub_Connected,
51 .Disconnected = Hub_Disconnected,
54 {0x83, Hub_PortStatusChange}
60 int Hub_DriverInitialise(char **Arguments)
62 USB_RegisterDriver( &gUSBHub_Driver );
67 void Hub_Connected(tUSBInterface *Dev)
69 struct sHubDescriptor hub_desc;
70 struct sHubInfo *info;
72 // Read hub descriptor (Class descriptor 0x29)
73 USB_ReadDescriptor(Dev, 0x129, 0, sizeof(hub_desc), &hub_desc);
75 LOG("%i Ports", hub_desc.NbrPorts);
76 LOG("Takes %i ms for power to stabilise", hub_desc.PwrOn2PwrGood*2);
78 // Allocate infomation structure
79 info = malloc(sizeof(*info) + (hub_desc.NbrPorts+7)/8);
81 Log_Error("USBHub", "malloc() failed");
84 USB_SetDeviceDataPtr(Dev, info);
87 info->nPorts = hub_desc.NbrPorts;
88 info->PowerOnDelay = hub_desc.PwrOn2PwrGood * 2;
89 memcpy(info->DeviceRemovable, hub_desc.DeviceRemovable, (hub_desc.NbrPorts+7)/8);
91 info->HubPtr = USB_RegisterHub(Dev, info->nPorts);
93 // Register poll on endpoint
94 USB_StartPollingEndpoint(Dev, 1);
97 void Hub_Disconnected(tUSBInterface *Dev)
99 struct sHubInfo *info = USB_GetDeviceDataPtr(Dev);
100 USB_RemoveHub(info->HubPtr);
104 void Hub_PortStatusChange(tUSBInterface *Dev, int Endpoint, int Length, void *Data)
106 Uint8 *status = Data;
107 struct sHubInfo *info = USB_GetDeviceDataPtr(Dev);
109 for( i = 0; i < info->nPorts; i += 8, status ++ )
111 if( i/8 >= Length ) break;
112 if( *status == 0 ) continue;
114 for( int j = 0; j < 8; j ++ )
115 if( *status & (1 << j) )
116 Hub_int_HandleChange(Dev, i+j);
120 void Hub_int_HandleChange(tUSBInterface *Dev, int Port)
122 struct sHubInfo *info = USB_GetDeviceDataPtr(Dev);
123 Uint16 status[2]; // Status, Change
126 USB_Request(Dev, 0, 0xA3, GET_STATUS, 0, Port, 4, status);
128 // Handle connections / disconnections
129 if( status[1] & 0x0001 )
131 if( status[0] & 0x0001 ) {
134 USB_Request(Dev, 0, 0x23, SET_FEATURE, PORT_POWER, Port, 0, NULL);
135 Time_Delay(info->PowerOnDelay);
137 USB_Request(Dev, 0, 0x23, SET_FEATURE, PORT_RESET, Port, 0, NULL);
138 Time_Delay(20); // Spec says 10ms after reset, but how long is reset?
140 USB_Request(Dev, 0, 0x23, SET_FEATURE, PORT_ENABLE, Port, 0, NULL);
142 USB_DeviceConnected(info->HubPtr, Port);
146 USB_DeviceDisconnected(info->HubPtr, Port);