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

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