Merge branch 'master' of git://localhost/acess2
[tpg/acess2.git] / KernelLand / Modules / USB / Core / usb_devinit.c
index d260f62..6143bdb 100644 (file)
@@ -5,7 +5,7 @@
  * usb_devinit.c
  * - USB Device Initialisation
  */
-#define DEBUG  1
+#define DEBUG  0
 #include <acess.h>
 #include <vfs.h>
 #include <drv_pci.h>
@@ -21,6 +21,7 @@ void  USB_DeviceDisconnected(tUSBHub *Hub, int Port);
 void   *USB_GetDeviceDataPtr(tUSBInterface *Dev);
 void   USB_SetDeviceDataPtr(tUSBInterface *Dev, void *Ptr);
  int   USB_int_AllocateAddress(tUSBHost *Host);
+void   USB_int_DeallocateAddress(tUSBHost *Host, int Address);
 
 // === CODE ===
 void USB_DeviceConnected(tUSBHub *Hub, int Port)
@@ -49,14 +50,32 @@ void USB_DeviceConnected(tUSBHub *Hub, int Port)
        }
        USB_int_SendSetupSetAddress(dev->Host, dev->Address);
        LOG("Assigned address %i", dev->Address);
-       
+
+       dev->EndpointHandles[0] = dev->Host->HostDef->InitControl(dev->Host->Ptr, dev->Address << 4, 64);
+       for( int i = 1; i < 16; i ++ )
+               dev->EndpointHandles[i] = 0;
+
        // 2. Get device information
        {
                struct sDescriptor_Device       desc;
+               desc.Length = 0;
                LOG("Getting device descriptor");
                // Endpoint 0, Desc Type 1, Index 0
                USB_int_ReadDescriptor(dev, 0, 1, 0, sizeof(desc), &desc);
 
+               if( desc.Length < sizeof(desc) ) {
+                       Log_Error("USB", "Device descriptor undersized (%i<%i)", desc.Length, sizeof(desc));
+                       USB_int_DeallocateAddress(dev->Host, dev->Address);
+                       LEAVE('-');
+                       return;
+               }
+               if( desc.Type != 1 ) {
+                       Log_Error("USB", "Device descriptor type invalid (%i!=1)", desc.Type);
+                       USB_int_DeallocateAddress(dev->Host, dev->Address);
+                       LEAVE('-');
+                       return;
+               }
+
                #if DUMP_DESCRIPTORS            
                LOG("Device Descriptor = {");
                LOG(" .Length = %i", desc.Length);
@@ -75,7 +94,7 @@ void USB_DeviceConnected(tUSBHub *Hub, int Port)
                LOG(" .NumConfigurations = %i", desc.NumConfigurations);
                LOG("}");
        
-               #if DEBUG       
+               #if DEBUG
                if( desc.ManufacturerStr )
                {
                        char    *tmp = USB_int_GetDeviceString(dev, 0, desc.ManufacturerStr);
@@ -106,9 +125,8 @@ void USB_DeviceConnected(tUSBHub *Hub, int Port)
                memcpy(&dev->DevDesc, &desc, sizeof(desc));
        }
 
-       // TODO: Support alternate configurations
-       
        // 3. Get configurations
+       // TODO: Support alternate configurations
        for( int i = 0; i < 1; i ++ )
        {
                struct sDescriptor_Configuration        desc;
@@ -117,6 +135,12 @@ void USB_DeviceConnected(tUSBHub *Hub, int Port)
                size_t  total_length;
        
                USB_int_ReadDescriptor(dev, 0, 2, i, sizeof(desc), &desc);
+               if( desc.Length < sizeof(desc) ) {
+                       // ERROR: 
+               }
+               if( desc.Type != 2 ) {
+                       // ERROR: 
+               }
                // TODO: Check return length? (Do we get a length?)
                #if DUMP_DESCRIPTORS
                LOG("Configuration Descriptor %i = {", i);
@@ -232,7 +256,14 @@ void USB_DeviceConnected(tUSBHub *Hub, int Port)
                                LOG(" .MaxPacketSize = %i", LittleEndian16(endpt->MaxPacketSize));
                                LOG(" .PollingInterval = %i", endpt->PollingInterval);
                                LOG("}");
-                               
+       
+                               // Make sure things don't break
+                               if( !((endpt->Address & 0x7F) < 15) ) {
+                                       Log_Error("USB", "Endpoint number %i>16", endpt->Address & 0x7F);
+                                       k --;
+                                       continue ;
+                               }
+
                                dev_if->Endpoints[k].Next = NULL;
                                dev_if->Endpoints[k].Interface = dev_if;
                                dev_if->Endpoints[k].EndpointIdx = k;
@@ -242,6 +273,33 @@ void USB_DeviceConnected(tUSBHub *Hub, int Port)
                                dev_if->Endpoints[k].Type = endpt->Attributes | (endpt->Address & 0x80);
                                dev_if->Endpoints[k].PollingAtoms = 0;
                                dev_if->Endpoints[k].InputData = NULL;
+                               
+                               // TODO: Register endpoint early
+                                int    ep_num = endpt->Address & 15;
+                               if( dev->EndpointHandles[ep_num] ) {
+                                       Log_Notice("USB", "Device %p:%i ep %i reused", dev->Host->Ptr, dev->Address, ep_num);
+                               }
+                               else {
+                                        int    addr = dev->Address*16+ep_num;
+                                        int    mps = dev_if->Endpoints[k].MaxPacketSize;
+                                       void *handle = NULL;
+                                       switch( endpt->Attributes & 3)
+                                       {
+                                       case 0: // Control
+                                               handle = dev->Host->HostDef->InitControl(dev->Host->Ptr, addr, mps);
+                                               break;
+                                       case 1: // Isochronous
+                                       //      handle = dev->Host->HostDef->InitIsoch(dev->Host->Ptr, addr, mps);
+                                               break;
+                                       case 2: // Bulk
+                                               handle = dev->Host->HostDef->InitBulk(dev->Host->Ptr, addr, mps);
+                                               break;
+                                       case 3: // Interrupt
+                                       //      handle = dev->Host->HostDef->InitInterrupt(dev->Host->Ptr, addr, ...);
+                                               break;
+                                       }
+                                       dev->EndpointHandles[ep_num] = handle;
+                               }
                        }
                        
                        // Initialise driver
@@ -266,14 +324,35 @@ void USB_DeviceConnected(tUSBHub *Hub, int Port)
                
                free(full_buf);
        }
-
+       
+       Hub->Devices[Port] = dev;
+       
        // Done.
        LEAVE('-');
 }
 
 void USB_DeviceDisconnected(tUSBHub *Hub, int Port)
 {
-       
+       tUSBDevice      *dev;
+       if( !Hub->Devices[Port] ) {
+               Log_Error("USB", "Non-registered device disconnected");
+               return;
+       }
+       dev = Hub->Devices[Port];
+
+       // TODO: Free related resources
+       // - Endpoint registrations
+       for( int i = 0; i < 16; i ++ )
+       {
+               if(dev->EndpointHandles[i])
+                       dev->Host->HostDef->RemEndpoint(dev->Host->Ptr, dev->EndpointHandles[i]);
+       }
+
+       // - Bus Address
+       USB_int_DeallocateAddress(dev->Host, dev->Address);
+       // - Inform handler
+       // - Allocate memory
+       free(dev);
 }
 
 void *USB_GetDeviceDataPtr(tUSBInterface *Dev) { return Dev->Data; }

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