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

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