3 * - By John Hodge (thePowersGang)
12 #define MAX_PORTS 32 // Not actually a max, but used for DeviceRemovable
16 #define CLEAR_FEATURE 1
20 #define PORT_CONNECTION 0
22 #define PORT_SUSPEND 2
23 #define PORT_OVER_CURRENT 3
26 #define PORT_LOW_SPEED 9
27 #define C_PORT_CONNECTION 16
28 #define C_PORT_ENABLE 17
29 #define C_PORT_SUSPEND 18
30 #define C_PORT_OVER_CURRENT 19
31 #define C_PORT_RESET 20
33 #define PORT_INDICATOR 21
38 Uint8 DescType; // = 0x29
40 Uint16 HubCharacteristics;
41 Uint8 PwrOn2PwrGood; // 2 ms intervals
42 Uint8 HubContrCurrent; // Max internal current (mA)
43 Uint8 DeviceRemovable[MAX_PORTS];
49 int PowerOnDelay; // in ms
51 Uint8 DeviceRemovable[];
55 void Hub_Connected(tUSBInterface *Dev, void *Descriptors, size_t Length);
56 void Hub_Disconnected(tUSBInterface *Dev);
57 void Hub_PortStatusChange(tUSBInterface *Dev, int Endpoint, int Length, void *Data);
58 void Hub_int_HandleChange(tUSBInterface *Dev, int Port);
61 tUSBDriver gUSBHub_Driver = {
63 .Match = {.Class = {0x090000, 0xFF0000}},
64 .Connected = Hub_Connected,
65 .Disconnected = Hub_Disconnected,
68 {0x83, Hub_PortStatusChange}
74 int Hub_DriverInitialise(char **Arguments)
76 USB_RegisterDriver( &gUSBHub_Driver );
81 void Hub_Connected(tUSBInterface *Dev, void *Descriptors, size_t Length)
83 struct sHubDescriptor hub_desc;
84 struct sHubInfo *info;
86 // Read hub descriptor (Class descriptor 0x9, destined for device)
87 USB_ReadDescriptor(Dev, 0x0129, 0, sizeof(hub_desc), &hub_desc);
89 LOG("%i Ports", hub_desc.NbrPorts);
90 LOG("Takes %i ms for power to stabilise", hub_desc.PwrOn2PwrGood*2);
92 // Allocate infomation structure
93 info = malloc(sizeof(*info) + (hub_desc.NbrPorts+7)/8);
95 Log_Error("USBHub", "malloc() failed");
98 USB_SetDeviceDataPtr(Dev, info);
101 info->nPorts = hub_desc.NbrPorts;
102 info->PowerOnDelay = hub_desc.PwrOn2PwrGood * 2;
103 memcpy(info->DeviceRemovable, hub_desc.DeviceRemovable, (hub_desc.NbrPorts+7)/8);
105 info->HubPtr = USB_RegisterHub(Dev, info->nPorts);
107 // Register poll on endpoint
108 USB_StartPollingEndpoint(Dev, 1);
111 void Hub_Disconnected(tUSBInterface *Dev)
113 struct sHubInfo *info = USB_GetDeviceDataPtr(Dev);
114 USB_RemoveHub(info->HubPtr);
118 void Hub_PortStatusChange(tUSBInterface *Dev, int Endpoint, int Length, void *Data)
120 Uint8 *status = Data;
121 struct sHubInfo *info = USB_GetDeviceDataPtr(Dev);
123 for( i = 0; i < info->nPorts; i += 8, status ++ )
125 if( i/8 >= Length ) break;
126 LOG("status[%i] = %x", i/8, *status);
127 if( *status == 0 ) continue;
129 for( int j = 0; j < 8; j ++ )
130 if( *status & (1 << j) )
131 Hub_int_HandleChange(Dev, i+j);
135 void Hub_int_HandleChange(tUSBInterface *Dev, int Port)
137 struct sHubInfo *info = USB_GetDeviceDataPtr(Dev);
138 Uint16 status[2]; // Status, Change
141 USB_Request(Dev, 0, 0xA3, GET_STATUS, 0, Port, 4, status);
143 LOG("Port %i: status = {0b%b, 0b%b}", Port, status[0], status[1]);
145 // Handle connections / disconnections
146 if( status[1] & 0x0001 )
148 if( status[0] & 0x0001 ) {
151 USB_Request(Dev, 0, 0x23, SET_FEATURE, PORT_POWER, Port, 0, NULL);
152 Time_Delay(info->PowerOnDelay);
154 USB_Request(Dev, 0, 0x23, SET_FEATURE, PORT_RESET, Port, 0, NULL);
155 Time_Delay(20); // Spec says 10ms after reset, but how long is reset?
157 USB_Request(Dev, 0, 0x23, SET_FEATURE, PORT_ENABLE, Port, 0, NULL);
159 USB_DeviceConnected(info->HubPtr, Port);
163 USB_DeviceDisconnected(info->HubPtr, Port);
166 USB_Request(Dev, 0, 0x23, CLEAR_FEATURE, C_PORT_CONNECTION, Port, 0, NULL);
170 if( status[1] & 0x0010 )
172 if( status[0] & 0x0010 ) {
179 USB_Request(Dev, 0, 0x23, CLEAR_FEATURE, C_PORT_RESET, Port, 0, NULL);