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 if( Port >= Hub->nPorts ) return ;
29 if( Hub->Devices[Port] ) return ;
31 ENTER("pHub iPort", Hub, Port);
33 // Device should be in 'Default' state
36 dev = malloc(sizeof(tUSBDevice));
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 // 3. Get configurations
97 for( int i = 0; i < 1; i ++ )
99 struct sDescriptor_Configuration desc;
103 USB_int_ReadDescriptor(dev, 0, 2, i, sizeof(desc), &desc);
104 LOG("Configuration Descriptor %i = {", i);
105 LOG(" .Length = %i", desc.Length);
106 LOG(" .Type = %i", desc.Type);
107 LOG(" .TotalLength = 0x%x", LittleEndian16(desc.TotalLength));
108 LOG(" .NumInterfaces = %i", desc.NumInterfaces);
109 LOG(" .ConfigurationValue = %i", desc.ConfigurationValue);
110 LOG(" .ConfigurationStr = %i", desc.ConfigurationStr);
111 LOG(" .AttributesBmp = 0b%b", desc.AttributesBmp);
112 LOG(" .MaxPower = %i (*2mA)", desc.MaxPower);
114 if( desc.ConfigurationStr ) {
115 char *tmp = USB_int_GetDeviceString(dev, 0, desc.ConfigurationStr);
116 LOG("ConfigurationStr = '%s'", tmp);
120 cur_ptr = full_buf = malloc( LittleEndian16(desc.TotalLength) );
121 USB_int_ReadDescriptor(dev, 0, 2, i, desc.TotalLength, full_buf);
123 cur_ptr += desc.Length;
126 for( int j = 0; j < desc.NumInterfaces; j ++ )
128 struct sDescriptor_Interface *iface;
129 iface = (void*)cur_ptr;
130 // TODO: Sanity check with remaining space
131 cur_ptr += sizeof(*iface);
133 LOG("Interface %i/%i = {", i, j);
134 LOG(" .InterfaceNum = %i", iface->InterfaceNum);
135 LOG(" .NumEndpoints = %i", iface->NumEndpoints);
136 LOG(" .InterfaceClass = 0x%x", iface->InterfaceClass);
137 LOG(" .InterfaceSubClass = 0x%x", iface->InterfaceSubClass);
138 LOG(" .InterfaceProcol = 0x%x", iface->InterfaceProtocol);
140 if( iface->InterfaceStr ) {
141 char *tmp = USB_int_GetDeviceString(dev, 0, iface->InterfaceStr);
142 LOG(" .InterfaceStr = %i '%s'", iface->InterfaceStr, tmp);
147 for( int k = 0; k < iface->NumEndpoints; k ++ )
149 struct sDescriptor_Endpoint *endpt;
150 endpt = (void*)cur_ptr;
151 // TODO: Sanity check with remaining space
152 cur_ptr += sizeof(*endpt);
154 LOG("Endpoint %i/%i/%i = {", i, j, k);
155 LOG(" .Address = 0x%2x", endpt->Address);
156 LOG(" .Attributes = 0b%8b", endpt->Attributes);
157 LOG(" .MaxPacketSize = %i", LittleEndian16(endpt->MaxPacketSize));
158 LOG(" .PollingInterval = %i", endpt->PollingInterval);
170 void USB_DeviceDisconnected(tUSBHub *Hub, int Port)
175 void *USB_GetDeviceDataPtr(tUSBInterface *Dev) { return Dev->Data; }
176 void USB_SetDeviceDataPtr(tUSBInterface *Dev, void *Ptr) { Dev->Data = Ptr; }
178 int USB_int_AllocateAddress(tUSBHost *Host)
181 for( i = 1; i < 128; i ++ )
183 if(Host->AddressBitmap[i/8] & (1 << (i%8)))
185 Host->AddressBitmap[i/8] |= 1 << (i%8);
191 void USB_int_DeallocateAddress(tUSBHost *Host, int Address)
193 Host->AddressBitmap[Address/8] &= ~(1 << (Address%8));