3 * - By John Hodge (thePowersGang)
6 * - USB Device Initialisation
14 #include "usb_proto.h"
15 #include "usb_lowlevel.h"
18 void USB_DeviceConnected(tUSBHub *Hub, int Port);
19 void USB_DeviceDisconnected(tUSBHub *Hub, int Port);
20 void *USB_GetDeviceDataPtr(tUSBInterface *Dev);
21 void USB_SetDeviceDataPtr(tUSBInterface *Dev, void *Ptr);
22 int USB_int_AllocateAddress(tUSBHost *Host);
25 void USB_DeviceConnected(tUSBHub *Hub, int Port)
28 tUSBDevice *dev = &tmpdev;
29 if( Port >= Hub->nPorts ) return ;
30 if( Hub->Devices[Port] ) return ;
32 ENTER("pHub iPort", Hub, Port);
34 // Device should be in 'Default' state
38 dev->Host = Hub->Interface->Dev->Host;
41 // 1. Assign an address
42 dev->Address = USB_int_AllocateAddress(dev->Host);
43 if(dev->Address == 0) {
44 Log_Error("USB", "No addresses avaliable on host %p", dev->Host);
49 USB_int_SendSetupSetAddress(dev->Host, dev->Address);
50 LOG("Assigned address %i", dev->Address);
52 // 2. Get device information
54 struct sDescriptor_Device desc;
55 LOG("Getting device descriptor");
56 // Endpoint 0, Desc Type 1, Index 0
57 USB_int_ReadDescriptor(dev, 0, 1, 0, sizeof(desc), &desc);
59 LOG("Device Descriptor = {");
60 LOG(" .Length = %i", desc.Length);
61 LOG(" .Type = %i", desc.Type);
62 LOG(" .USBVersion = 0x%04x", desc.USBVersion);
63 LOG(" .DeviceClass = 0x%02x", desc.DeviceClass);
64 LOG(" .DeviceSubClass = 0x%02x", desc.DeviceSubClass);
65 LOG(" .DeviceProtocol = 0x%02x", desc.DeviceProtocol);
66 LOG(" .MaxPacketSize = 0x%02x", desc.MaxPacketSize);
67 LOG(" .VendorID = 0x%04x", desc.VendorID);
68 LOG(" .ProductID = 0x%04x", desc.ProductID);
69 LOG(" .DeviceID = 0x%04x", desc.DeviceID);
70 LOG(" .ManufacturerStr = Str %i", desc.ManufacturerStr);
71 LOG(" .ProductStr = Str %i", desc.ProductStr);
72 LOG(" .SerialNumberStr = Str %i", desc.SerialNumberStr);
73 LOG(" .NumConfigurations = %i", desc.SerialNumberStr);
76 if( desc.ManufacturerStr )
78 char *tmp = USB_int_GetDeviceString(dev, 0, desc.ManufacturerStr);
79 LOG("ManufacturerStr = '%s'", tmp);
84 char *tmp = USB_int_GetDeviceString(dev, 0, desc.ProductStr);
85 LOG("ProductStr = '%s'", tmp);
88 if( desc.SerialNumberStr )
90 char *tmp = USB_int_GetDeviceString(dev, 0, desc.SerialNumberStr);
91 LOG("SerialNumbertStr = '%s'", tmp);
96 // TODO: Support alternate configurations
98 // 3. Get configurations
99 for( int i = 0; i < 1; i ++ )
101 struct sDescriptor_Configuration desc;
106 USB_int_ReadDescriptor(dev, 0, 2, i, sizeof(desc), &desc);
107 LOG("Configuration Descriptor %i = {", i);
108 LOG(" .Length = %i", desc.Length);
109 LOG(" .Type = %i", desc.Type);
110 LOG(" .TotalLength = 0x%x", LittleEndian16(desc.TotalLength));
111 LOG(" .NumInterfaces = %i", desc.NumInterfaces);
112 LOG(" .ConfigurationValue = %i", desc.ConfigurationValue);
113 LOG(" .ConfigurationStr = %i", desc.ConfigurationStr);
114 LOG(" .AttributesBmp = 0b%b", desc.AttributesBmp);
115 LOG(" .MaxPower = %i (*2mA)", desc.MaxPower);
117 if( desc.ConfigurationStr ) {
118 char *tmp = USB_int_GetDeviceString(dev, 0, desc.ConfigurationStr);
119 LOG("ConfigurationStr = '%s'", tmp);
123 // TODO: Split here and allow some method of selection
125 // Allocate device now that we have the configuration
126 dev = malloc(sizeof(tUSBDevice) + desc.NumInterfaces * sizeof(void*));
127 memcpy(dev, &tmpdev, sizeof(tUSBDevice));
128 dev->nInterfaces = desc.NumInterfaces;
130 // Allocate a temp buffer for config info
131 total_length = LittleEndian16(desc.TotalLength);
132 full_buf = malloc( total_length );
133 USB_int_ReadDescriptor(dev, 0, 2, i, total_length, full_buf);
135 ptr_ofs += desc.Length;
138 for( int j = 0; ptr_ofs < total_length && j < desc.NumInterfaces; j ++ )
140 struct sDescriptor_Interface *iface;
141 tUSBInterface *dev_if;
142 size_t iface_base_ofs;
144 iface = (void*)(full_buf + ptr_ofs);
145 ptr_ofs += iface->Length;
146 if( ptr_ofs > total_length ) {
150 iface_base_ofs = ptr_ofs;
152 if( iface->Type != 4 ) {
153 LOG("Not an interface (type = %i)", iface->Type);
154 j --; // Counteract j++ in loop
158 LOG("Interface %i/%i = {", i, j);
159 LOG(" .InterfaceNum = %i", iface->InterfaceNum);
160 LOG(" .NumEndpoints = %i", iface->NumEndpoints);
161 LOG(" .InterfaceClass = 0x%x", iface->InterfaceClass);
162 LOG(" .InterfaceSubClass = 0x%x", iface->InterfaceSubClass);
163 LOG(" .InterfaceProcol = 0x%x", iface->InterfaceProtocol);
164 if( iface->InterfaceStr ) {
165 char *tmp = USB_int_GetDeviceString(dev, 0, iface->InterfaceStr);
166 LOG(" .InterfaceStr = %i '%s'", iface->InterfaceStr, tmp);
171 dev_if = malloc(sizeof(tUSBInterface) + iface->NumEndpoints*sizeof(dev_if->Endpoints[0]));
173 dev_if->Driver = NULL;
175 dev_if->nEndpoints = iface->NumEndpoints;
176 dev->Interfaces[j] = dev_if;
178 // Copy interface data
179 for( int k = 0; ptr_ofs < total_length && k < iface->NumEndpoints; k ++ )
181 struct sDescriptor_Endpoint *endpt;
183 endpt = (void*)(full_buf + ptr_ofs);
184 ptr_ofs += endpt->Length;
185 if( ptr_ofs > total_length ) {
191 if( endpt->Type != 5 ) {
193 LOG("Not endpoint, Type = %i", endpt->Type);
198 LOG("Endpoint %i/%i/%i = {", i, j, k);
199 LOG(" .Address = 0x%2x", endpt->Address);
200 LOG(" .Attributes = 0b%8b", endpt->Attributes);
201 LOG(" .MaxPacketSize = %i", LittleEndian16(endpt->MaxPacketSize));
202 LOG(" .PollingInterval = %i", endpt->PollingInterval);
205 dev_if->Endpoints[k].Next = NULL;
206 dev_if->Endpoints[k].Interface = dev_if;
207 dev_if->Endpoints[k].EndpointIdx = k;
208 dev_if->Endpoints[k].EndpointNum = endpt->Address & 0x7F;
209 dev_if->Endpoints[k].PollingPeriod = endpt->PollingInterval;
210 dev_if->Endpoints[k].MaxPacketSize = LittleEndian16(endpt->MaxPacketSize);
211 dev_if->Endpoints[k].Type = endpt->Attributes | (endpt->Address & 0x80);
212 dev_if->Endpoints[k].PollingAtoms = 0;
213 dev_if->Endpoints[k].InputData = NULL;
217 dev_if->Driver = USB_int_FindDriverByClass(
218 ((int)iface->InterfaceClass << 16)
219 |((int)iface->InterfaceSubClass << 8)
220 |((int)iface->InterfaceProtocol << 0)
222 if(!dev_if->Driver) {
223 Log_Notice("USB", "No driver for Class %02x:%02x:%02x",
224 iface->InterfaceClass, iface->InterfaceSubClass, iface->InterfaceProtocol
228 dev_if->Driver->Connected(
230 full_buf + iface_base_ofs, ptr_ofs - iface_base_ofs
232 // dev_if->Driver->Connected( dev_if );
243 void USB_DeviceDisconnected(tUSBHub *Hub, int Port)
248 void *USB_GetDeviceDataPtr(tUSBInterface *Dev) { return Dev->Data; }
249 void USB_SetDeviceDataPtr(tUSBInterface *Dev, void *Ptr) { Dev->Data = Ptr; }
251 int USB_int_AllocateAddress(tUSBHost *Host)
254 for( i = 1; i < 128; i ++ )
256 if(Host->AddressBitmap[i/8] & (1 << (i%8)))
258 Host->AddressBitmap[i/8] |= 1 << (i%8);
264 void USB_int_DeallocateAddress(tUSBHost *Host, int Address)
266 Host->AddressBitmap[Address/8] &= ~(1 << (Address%8));