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

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