Modules/UHCI - More verbose interrupt messages (x86_64 debugging)
[tpg/acess2.git] / KernelLand / Modules / USB / Core / usb_devinit.c
1 /*
2  * Acess 2 USB Stack
3  * - By John Hodge (thePowersGang)
4  *
5  * usb_devinit.c
6  * - USB Device Initialisation
7  */
8 #define DEBUG   1
9 #include <acess.h>
10 #include <vfs.h>
11 #include <drv_pci.h>
12 #include "usb.h"
13 #include "usb_proto.h"
14 #include "usb_lowlevel.h"
15
16 #define DUMP_DESCRIPTORS        1
17
18 // === PROTOTYPES ===
19 void    USB_DeviceConnected(tUSBHub *Hub, int Port);
20 void    USB_DeviceDisconnected(tUSBHub *Hub, int Port);
21 void    *USB_GetDeviceDataPtr(tUSBInterface *Dev);
22 void    USB_SetDeviceDataPtr(tUSBInterface *Dev, void *Ptr);
23  int    USB_int_AllocateAddress(tUSBHost *Host);
24
25 // === CODE ===
26 void USB_DeviceConnected(tUSBHub *Hub, int Port)
27 {
28         tUSBDevice      tmpdev;
29         tUSBDevice      *dev = &tmpdev;
30         if( Port >= Hub->nPorts )       return ;
31         if( Hub->Devices[Port] )        return ;
32
33         ENTER("pHub iPort", Hub, Port);
34
35         // Device should be in 'Default' state
36         
37         // Create structure
38         dev->ParentHub = Hub;
39         dev->Host = Hub->Interface->Dev->Host;
40         dev->Address = 0;
41
42         // 1. Assign an address
43         dev->Address = USB_int_AllocateAddress(dev->Host);
44         if(dev->Address == 0) {
45                 Log_Error("USB", "No addresses avaliable on host %p", dev->Host);
46                 free(dev);
47                 LEAVE('-');
48                 return ;
49         }
50         USB_int_SendSetupSetAddress(dev->Host, dev->Address);
51         LOG("Assigned address %i", dev->Address);
52         
53         // 2. Get device information
54         {
55                 struct sDescriptor_Device       desc;
56                 LOG("Getting device descriptor");
57                 // Endpoint 0, Desc Type 1, Index 0
58                 USB_int_ReadDescriptor(dev, 0, 1, 0, sizeof(desc), &desc);
59
60                 #if DUMP_DESCRIPTORS            
61                 LOG("Device Descriptor = {");
62                 LOG(" .Length = %i", desc.Length);
63                 LOG(" .Type = %i", desc.Type);
64                 LOG(" .USBVersion = 0x%04x", desc.USBVersion);
65                 LOG(" .DeviceClass = 0x%02x", desc.DeviceClass);
66                 LOG(" .DeviceSubClass = 0x%02x", desc.DeviceSubClass);
67                 LOG(" .DeviceProtocol = 0x%02x", desc.DeviceProtocol);
68                 LOG(" .MaxPacketSize = 0x%02x", desc.MaxPacketSize);
69                 LOG(" .VendorID = 0x%04x", desc.VendorID);
70                 LOG(" .ProductID = 0x%04x", desc.ProductID);
71                 LOG(" .DeviceID = 0x%04x", desc.DeviceID);
72                 LOG(" .ManufacturerStr = Str %i", desc.ManufacturerStr);
73                 LOG(" .ProductStr = Str %i", desc.ProductStr);
74                 LOG(" .SerialNumberStr = Str %i", desc.SerialNumberStr);
75                 LOG(" .NumConfigurations = %i", desc.SerialNumberStr);
76                 LOG("}");
77         
78                 #if DEBUG       
79                 if( desc.ManufacturerStr )
80                 {
81                         char    *tmp = USB_int_GetDeviceString(dev, 0, desc.ManufacturerStr);
82                         if( tmp ) {
83                                 LOG("ManufacturerStr = '%s'", tmp);
84                                 free(tmp);
85                         }
86                 }
87                 if( desc.ProductStr )
88                 {
89                         char    *tmp = USB_int_GetDeviceString(dev, 0, desc.ProductStr);
90                         if( tmp ) {
91                                 LOG("ProductStr = '%s'", tmp);
92                                 free(tmp);
93                         }
94                 }
95                 if( desc.SerialNumberStr )
96                 {
97                         char    *tmp = USB_int_GetDeviceString(dev, 0, desc.SerialNumberStr);
98                         if( tmp ) {
99                                 LOG("SerialNumbertStr = '%s'", tmp);
100                                 free(tmp);
101                         }
102                 }
103                 #endif
104                 #endif
105                 
106                 memcpy(&dev->DevDesc, &desc, sizeof(desc));
107         }
108
109         // TODO: Support alternate configurations
110         
111         // 3. Get configurations
112         for( int i = 0; i < 1; i ++ )
113         {
114                 struct sDescriptor_Configuration        desc;
115                 Uint8   *full_buf;
116                 size_t  ptr_ofs = 0;
117                 size_t  total_length;
118         
119                 USB_int_ReadDescriptor(dev, 0, 2, i, sizeof(desc), &desc);
120                 // TODO: Check return length? (Do we get a length?)
121                 #if DUMP_DESCRIPTORS
122                 LOG("Configuration Descriptor %i = {", i);
123                 LOG(" .Length = %i", desc.Length);
124                 LOG(" .Type = %i", desc.Type);
125                 LOG(" .TotalLength = 0x%x", LittleEndian16(desc.TotalLength));
126                 LOG(" .NumInterfaces = %i", desc.NumInterfaces);
127                 LOG(" .ConfigurationValue = %i", desc.ConfigurationValue);
128                 LOG(" .ConfigurationStr = %i", desc.ConfigurationStr);
129                 LOG(" .AttributesBmp = 0b%b", desc.AttributesBmp);
130                 LOG(" .MaxPower = %i (*2mA)", desc.MaxPower);
131                 LOG("}");
132                 if( desc.ConfigurationStr ) {
133                         char    *tmp = USB_int_GetDeviceString(dev, 0, desc.ConfigurationStr);
134                         LOG("ConfigurationStr = '%s'", tmp);
135                         free(tmp);
136                 }
137                 #endif
138
139                 if( desc.NumInterfaces == 0 ) {
140                         Log_Notice("USB", "Device does not have any interfaces");
141                         continue ;
142                 }
143
144                 // TODO: Split here and allow some method of selection
145
146                 // Allocate device now that we have the configuration
147                 dev = malloc(sizeof(tUSBDevice) + desc.NumInterfaces * sizeof(void*));
148                 memcpy(dev, &tmpdev, sizeof(tUSBDevice));
149                 dev->nInterfaces = desc.NumInterfaces;
150         
151                 // Allocate a temp buffer for config info
152                 total_length = LittleEndian16(desc.TotalLength);
153                 full_buf = malloc( total_length );
154                 USB_int_ReadDescriptor(dev, 0, 2, i, total_length, full_buf);
155                 ptr_ofs += desc.Length;
156
157
158                 // Interfaces!
159                 for( int j = 0; ptr_ofs < total_length && j < desc.NumInterfaces; j ++ )
160                 {
161                         struct sDescriptor_Interface *iface;
162                         tUSBInterface   *dev_if;
163                         size_t  iface_base_ofs;
164
165                         iface = (void*)(full_buf + ptr_ofs);
166                         if( iface->Length == 0 ) {
167                                 Log_Warning("USB", "Bad USB descriptor (length = 0)");
168                                 break ;
169                         }
170                         ptr_ofs += iface->Length;
171                         if( ptr_ofs > total_length ) {
172                                 // Sanity fail
173                                 break;
174                         }
175                         iface_base_ofs = ptr_ofs;
176                         // Check type
177                         if( iface->Type != 4 ) {
178                                 LOG("Not an interface (type = %i)", iface->Type);
179                                 j --;   // Counteract j++ in loop
180                                 continue ;
181                         }
182
183                         #if DUMP_DESCRIPTORS
184                         LOG("Interface %i/%i = {", i, j);
185                         LOG(" .InterfaceNum = %i", iface->InterfaceNum);
186                         LOG(" .NumEndpoints = %i", iface->NumEndpoints);
187                         LOG(" .InterfaceClass = 0x%x", iface->InterfaceClass);
188                         LOG(" .InterfaceSubClass = 0x%x", iface->InterfaceSubClass);
189                         LOG(" .InterfaceProcol = 0x%x", iface->InterfaceProtocol);
190                         if( iface->InterfaceStr ) {
191                                 char    *tmp = USB_int_GetDeviceString(dev, 0, iface->InterfaceStr);
192                                 LOG(" .InterfaceStr = %i '%s'", iface->InterfaceStr, tmp);
193                                 free(tmp);
194                         }
195                         LOG("}");
196                         #endif
197
198                         dev_if = malloc(
199                                 sizeof(tUSBInterface)
200                                 + iface->NumEndpoints*sizeof(dev_if->Endpoints[0])
201                                 );
202                         dev_if->Dev = dev;
203                         dev_if->Driver = NULL;
204                         dev_if->Data = NULL;
205                         dev_if->nEndpoints = iface->NumEndpoints;
206                         memcpy(&dev_if->IfaceDesc, iface, sizeof(*iface));
207                         dev->Interfaces[j] = dev_if;
208
209                         // Copy interface data
210                         for( int k = 0; ptr_ofs < total_length && k < iface->NumEndpoints; k ++ )
211                         {
212                                 struct sDescriptor_Endpoint *endpt;
213                                 
214                                 endpt = (void*)(full_buf + ptr_ofs);
215                                 ptr_ofs += endpt->Length;
216                                 if( ptr_ofs > total_length ) {
217                                         // Sanity fail
218                                         break;
219                                 }
220
221                                 // Check type
222                                 if( endpt->Type != 5 ) {
223                                         // Oops?
224                                         LOG("Not endpoint, Type = %i", endpt->Type);
225                                         k --;
226                                         continue ;
227                                 }
228
229                                 LOG("Endpoint %i/%i/%i = {", i, j, k);
230                                 LOG(" .Address = 0x%2x", endpt->Address);
231                                 LOG(" .Attributes = 0b%8b", endpt->Attributes);
232                                 LOG(" .MaxPacketSize = %i", LittleEndian16(endpt->MaxPacketSize));
233                                 LOG(" .PollingInterval = %i", endpt->PollingInterval);
234                                 LOG("}");
235                                 
236                                 dev_if->Endpoints[k].Next = NULL;
237                                 dev_if->Endpoints[k].Interface = dev_if;
238                                 dev_if->Endpoints[k].EndpointIdx = k;
239                                 dev_if->Endpoints[k].EndpointNum = endpt->Address & 0x7F;
240                                 dev_if->Endpoints[k].PollingPeriod = endpt->PollingInterval;
241                                 dev_if->Endpoints[k].MaxPacketSize = LittleEndian16(endpt->MaxPacketSize);
242                                 dev_if->Endpoints[k].Type = endpt->Attributes | (endpt->Address & 0x80);
243                                 dev_if->Endpoints[k].PollingAtoms = 0;
244                                 dev_if->Endpoints[k].InputData = NULL;
245                         }
246                         
247                         // Initialise driver
248                         dev_if->Driver = USB_int_FindDriverByClass(
249                                  ((int)iface->InterfaceClass << 16)
250                                 |((int)iface->InterfaceSubClass << 8)
251                                 |((int)iface->InterfaceProtocol << 0)
252                                 );
253                         if(!dev_if->Driver) {
254                                 Log_Notice("USB", "No driver for Class %02x:%02x:%02x",
255                                         iface->InterfaceClass, iface->InterfaceSubClass, iface->InterfaceProtocol
256                                         );
257                         }
258                         else {
259                                 LOG("Driver '%s' in use", dev_if->Driver->Name);
260                                 dev_if->Driver->Connected(
261                                         dev_if,
262                                         full_buf + iface_base_ofs, ptr_ofs - iface_base_ofs
263                                         );
264                         }
265                 }
266                 
267                 free(full_buf);
268         }
269
270         // Done.
271         LEAVE('-');
272 }
273
274 void USB_DeviceDisconnected(tUSBHub *Hub, int Port)
275 {
276         
277 }
278
279 void *USB_GetDeviceDataPtr(tUSBInterface *Dev) { return Dev->Data; }
280 void USB_SetDeviceDataPtr(tUSBInterface *Dev, void *Ptr) { Dev->Data = Ptr; }
281
282 int USB_int_AllocateAddress(tUSBHost *Host)
283 {
284          int    i;
285         for( i = 1; i < 128; i ++ )
286         {
287                 if(Host->AddressBitmap[i/8] & (1 << (i%8)))
288                         continue ;
289                 Host->AddressBitmap[i/8] |= 1 << (i%8);
290                 return i;
291         }
292         return 0;
293 }
294
295 void USB_int_DeallocateAddress(tUSBHost *Host, int Address)
296 {
297         Host->AddressBitmap[Address/8] &= ~(1 << (Address%8));
298 }
299

UCC git Repository :: git.ucc.asn.au