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

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