Exemple #1
0
// Funzione main eseguita da rundll32 64bit
void __stdcall H64_sMain(void)
{
	SetGlobalVariables();
	//GetAdmin(GetCurrentProcessId());
	//GetAdmin(g_core_pid);
	StartPolling();
}
// --------------------------------------------------------------------------
// CDevEncStarterMemoryEntity::DoPollTick()
// 
// --------------------------------------------------------------------------
void CDevEncStarterMemoryEntity::DoPollTick()
    {
    TInt nfeDiskStatus( EUnmounted );
    TInt err = iSession->DiskStatus( nfeDiskStatus );
    if ( err )
        {
        DFLOG2( "DevEncStarter: Could not get disk status, error %d", err );
        // Ignore error
        return;
        }
    if ( ( nfeDiskStatus == EEncrypting ) ||
         ( nfeDiskStatus == EDecrypting ) )
        {
        // Some other component has started an encryption operation.
        // Indicate this to the UI and start polling the progress.
        SetState( nfeDiskStatus );
        StartPolling( KProgressInterval );
        }
    else
        {
        if ( iState != nfeDiskStatus )
            {
            // The Mmc status has changed, but we are not in the middle of
            // any operation. Just set the new state.
            SetState( nfeDiskStatus );
            }
        }
    }
 void ScnCheckBchannelsBusy::OnPollingTimer(iCore::MsgTimer* pTimer)
 {
     //  огда установленны все соединени¤:
     if (m_channelCatchers.AllComplete())
     {
         pTimer->Stop();            
         AddSubsAndRun<SubBusyCheck>(Sub::leftSender, false);
         AddSubsAndRun<SubBusyCheck>(Sub::rightSender, false);            
         
         StartPolling();
     }
 }
// --------------------------------------------------------------------------
// CDevEncStarterMemoryEntity::ConstructL()
// 
// --------------------------------------------------------------------------
void CDevEncStarterMemoryEntity::ConstructL()
	{
    DFLOG( ">>CDevEncStarterMemoryEntity::ConstructL" );
    switch( iType )
        	{
        	case EPhoneMemory: iSession = new ( ELeave ) CDevEncSession( /*EDriveC*/EDriveE );
                               iDiskStatusObserver = CDiskStatusObserver::NewL( this, /*EDriveC*/EDriveE ); 
        		               break;
        		               
        	case EPrimaryPhoneMemory: iSession = new ( ELeave ) CDevEncSession( EDriveC );
                              iDiskStatusObserver = CDiskStatusObserver::NewL( this, EDriveC );
        	    		      break;
        	    		      
        	case EMemoryCard: iSession = new ( ELeave ) CDevEncSession( /*EDriveE*/EDriveF );
                              iDiskStatusObserver = CDiskStatusObserver::NewL( this, /*EDriveE*/EDriveF );
        	    		      break;    	    		               
            }
   
    User::LeaveIfError( iSession->Connect() );

    // Get initial memory state
    TInt nfeDiskStatus( EUnmounted );
#ifndef __WINS__
    TInt err = iSession->DiskStatus( nfeDiskStatus );
    if ( err )
        {
        DFLOG2( "DevEncStarter: Could not get disk status, error %d", err );
        nfeDiskStatus = EDecrypted;
        //User::Leave( err );
        }
#else
    nfeDiskStatus = EDecrypted;
#endif
    SetState( nfeDiskStatus );

    if ( ( iState == EEncrypting ) ||
         ( iState == EDecrypting ) )
        {
        // If the app was started in the middle of an ongoing operation,
        // start polling the progress
        StartPolling( KProgressInterval );
        }
    else
        {
        // Otherwise poll every once in a while to see if the status changes
        //StartPolling( KPollInterval );
        }

    DFLOG( "<<CDevEncStarterMemoryEntity::ConstructL" );
	}
VOID RecoveryThread(PVOID Arg)
{
    PIO_STACK_LOCATION stack;
    IO_STATUS_BLOCK    IoStatus;
    KEVENT             Event;
    PIRP               Irp;
    NTSTATUS           status;

    PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION)Arg;

    while(1)
    {
        // the main part of the driver will signal us when we need to fix things
        KeWaitForSingleObject(&pdx->RecoveryEvent, Executive, KernelMode, FALSE, NULL);
    
        if(pdx->RecoveryExit) break;
        
        KeInitializeEvent(&Event, SynchronizationEvent, FALSE);

        // reset our device
        Irp = IoBuildDeviceIoControlRequest(
                                             IOCTL_INTERNAL_USB_RESET_PORT,
                                             pdx->LowerDeviceObject,
                                             NULL,
                                             0,
                                             NULL,
                                             0,
                                             TRUE,
                                             &Event,
                                             &IoStatus
                                           );

        stack = IoGetNextIrpStackLocation(Irp);
        
        status = IoCallDriver(pdx->LowerDeviceObject, Irp);

        if(STATUS_PENDING == status)
        {
            KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
            
            status = IoStatus.Status;
        }

        // start polling again
        StartPolling(pdx);
    }

    PsTerminateSystemThread(STATUS_SUCCESS);
}
NTSTATUS OnPollComplete(PDEVICE_OBJECT fdo, PIRP Irp, PDEVICE_EXTENSION pdx)
{
    PIO_STACK_LOCATION stack;
    KIRQL              OldIrql;
    PIRP               ReadIrp;
    ULONG              len, count;
    NTSTATUS           status;

    stack = IoGetNextIrpStackLocation(Irp);

    KeAcquireSpinLock(&pdx->PollLock, &OldIrql);
    pdx->PollPending = FALSE;
    KeReleaseSpinLock(&pdx->PollLock, OldIrql);

    if(NT_SUCCESS(Irp->IoStatus.Status)){
        len = pdx->PollingUrb.UrbBulkOrInterruptTransfer.TransferBufferLength;
        
        // Data comes into the driver here, where we write it into the fifo.
        // The fifo will complete queued read IRPs if needed 
        count = CopyToFifo(pdx, &pdx->PollingBuffer[0], len);

        CompleteQueuedReads(pdx);

        if(FALSE == Irp->Cancel)
        {
            // if we haven't been cancelled then keep going
            status = StartPolling(pdx);
        }
    } else {
        // fail current outstanding reads
        CleanupReads(pdx, NULL, Irp->IoStatus.Status);
        
        // wake up the recovery thread
        KeSetEvent(&pdx->RecoveryEvent, 0, FALSE);
    }

    IoReleaseRemoveLock(&pdx->RemoveLock, Irp);

    // tell the io manager not to perform default handling of irp 
    return STATUS_MORE_PROCESSING_REQUIRED;
}
// --------------------------------------------------------------------------
// CDevEncStarterMemoryEntity::DiskStatusChangedL()
// From MDiskStatusObserver
// --------------------------------------------------------------------------
void CDevEncStarterMemoryEntity::DiskStatusChangedL( TInt aNfeStatus )
    {
    if ( ( aNfeStatus == EEncrypting ) ||
         ( aNfeStatus == EDecrypting ) )
        {
        // Some other component has started an encryption operation.
        // Indicate this to the UI and start polling the progress.
        DFLOG2( "CDevEncStarterMemoryEntity::DiskStatusChangedL => status = %d",
        		aNfeStatus );
        SetState( aNfeStatus );
        StartPolling( KProgressInterval );
        }
    else
        {        
        DFLOG2( "CDevEncStarterMemoryEntity::DiskStatusChangedL => status = %d",
            	 aNfeStatus );
        
        if( iState != aNfeStatus )
        	{
        	DFLOG2( "CDevEncStarterMemoryEntity::DiskStatusChangedL => status changed, iState was %d", iState );
            SetState( aNfeStatus );
        	}
        }
    }
 void ScnDeactivationProcedure::DssDeactivated(ISDN::IsdnStack* from)
 {
     if (from == m_from && m_waitDeactivation)
         StartPolling(); // test complete
 }
NTSTATUS StartDevice( PDEVICE_OBJECT fdo, PCM_PARTIAL_RESOURCE_LIST raw, PCM_PARTIAL_RESOURCE_LIST translated )
{
    USB_CONFIGURATION_DESCRIPTOR  tcd;
    PUSB_CONFIGURATION_DESCRIPTOR pcd;
    PUSB_STRING_DESCRIPTOR        desc;            
    HANDLE                        RecoveryHandle;
    PURB                          selurb;
    URB                           urb;     
    NTSTATUS                      status;

    status = STATUS_SUCCESS; 
    
    PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;

    selurb = NULL;
    pcd    = NULL;

    // Read our device descriptor. The only real purpose to this would be to find out how many
    // configurations there are so we can read their descriptors. In this simplest of examples,
    // there's only one configuration.
    
    KdPrint((DRIVERNAME " - Start device\n"));

    UsbBuildGetDescriptorRequest(
                                  &urb, 
                                  sizeof(_URB_CONTROL_DESCRIPTOR_REQUEST), 
                                  USB_DEVICE_DESCRIPTOR_TYPE,
                                  0, 
                                  LangId, 
                                  &pdx->dd, 
                                  NULL, 
                                  sizeof(pdx->dd), 
                                  NULL
                                );
 
    status = SendAwaitUrb( fdo, &urb );
    
    if(!NT_SUCCESS(status))
    {
        KdPrint((DRIVERNAME " - Error %X trying to retrieve device descriptor\n", status));
        goto cleanup;
    }

    // allocate the buffer for the descriptor; take extra space to null terminate the bString member
    desc = (PUSB_STRING_DESCRIPTOR)ExAllocatePoolWithTag(NonPagedPool, sizeof(USB_STRING_DESCRIPTOR) + StringDescriptorBytes, SPOT_TAG );

    if(!desc)
    {
        KdPrint((DRIVERNAME " - Unable to allocate %X bytes for string descriptor\n", sizeof(USB_STRING_DESCRIPTOR) + StringDescriptorBytes));
        status = STATUS_INSUFFICIENT_RESOURCES;
        goto cleanup;
    }
    
    pdx->devHash = desc;

    UsbBuildGetDescriptorRequest(&urb, 
                                 sizeof(_URB_CONTROL_DESCRIPTOR_REQUEST), 
                                 USB_STRING_DESCRIPTOR_TYPE,
                                 DeviceId, 
                                 LangId, 
                                 desc, 
                                 NULL, 
                                 sizeof(USB_STRING_DESCRIPTOR) + StringDescriptorBytes,
                                 NULL);
    
    status = SendAwaitUrb(fdo, &urb);
    
    if(!NT_SUCCESS(status))
    {
        KdPrint((DRIVERNAME " - Error %X trying to retrieve string descriptor for DeviceId\n", status));
        goto cleanup;
    }
        
    // null terminate the buffer; we allocated one more wchar_t for the purpose 
    desc->bString[ (desc->bLength / 2) - 1 ] = L'\0';

    UpdateDeviceInformation( fdo );

    // Read the descriptor of the first configuration. This requires two steps. The first step
    // reads the fixed-size configuration descriptor alone. The second step reads the
    // configuration descriptor plus all imbedded interface and endpoint descriptors.

    UsbBuildGetDescriptorRequest(&urb, 
                                 sizeof(_URB_CONTROL_DESCRIPTOR_REQUEST), 
                                 USB_CONFIGURATION_DESCRIPTOR_TYPE,
                                 0, 
                                 LangId, 
                                 &tcd, 
                                 NULL, 
                                 sizeof(tcd), 
                                 NULL);

    status = SendAwaitUrb(fdo, &urb);
    
    if(!NT_SUCCESS(status))
    {
        KdPrint((DRIVERNAME " - Error %X trying to read configuration descriptor 1\n", status));
        goto cleanup;
    }

    ULONG size = tcd.wTotalLength;
    
    pcd = (PUSB_CONFIGURATION_DESCRIPTOR) ExAllocatePoolWithTag(NonPagedPool, size, SPOT_TAG);
    
    if(!pcd)
    {
        KdPrint((DRIVERNAME " - Unable to allocate %X bytes for configuration descriptor\n", size));
        status = STATUS_INSUFFICIENT_RESOURCES;
        goto cleanup;
    }

    UsbBuildGetDescriptorRequest(&urb, 
                                 sizeof(_URB_CONTROL_DESCRIPTOR_REQUEST), 
                                 USB_CONFIGURATION_DESCRIPTOR_TYPE,
                                 0, 
                                 LangId, 
                                 pcd, 
                                 NULL, 
                                 size, 
                                 NULL);

    status = SendAwaitUrb(fdo, &urb);
    if(!NT_SUCCESS(status))
    {
        KdPrint((DRIVERNAME " - Error %X trying to read configuration descriptor 1\n", status));
        goto cleanup;
    }
                           
    // Locate the descriptor for the one and only interface we expect to find

    PUSB_INTERFACE_DESCRIPTOR pid = USBD_ParseConfigurationDescriptorEx(pcd, 
                                                                        pcd,
                                                                        -1, 
                                                                        -1, 
                                                                        -1, 
                                                                        -1, 
                                                                        -1);

    ASSERT(pid);
                           
    // Create a URB to use in selecting a configuration.

    USBD_INTERFACE_LIST_ENTRY interfaces[2] = {
        {pid, NULL},
        {NULL, NULL},        // fence to terminate the array
    };

    selurb = USBD_CreateConfigurationRequestEx(pcd, interfaces);
    if(!selurb)
    {
        KdPrint((DRIVERNAME " - Unable to create configuration request\n"));
        status = STATUS_INSUFFICIENT_RESOURCES;
        goto cleanup;
    }

    // Verify that the interface describes exactly the endpoints we expect
    if(pid->bNumEndpoints != 2)
    {
        KdPrint((DRIVERNAME " - %d is the wrong number of endpoints\n", pid->bNumEndpoints));
        status = STATUS_DEVICE_CONFIGURATION_ERROR;
        goto cleanup;
    }

    PUSB_ENDPOINT_DESCRIPTOR ped = (PUSB_ENDPOINT_DESCRIPTOR) pid;
    ped = (PUSB_ENDPOINT_DESCRIPTOR) USBD_ParseDescriptors(pcd, tcd.wTotalLength, ped, USB_ENDPOINT_DESCRIPTOR_TYPE);
    if(!ped || 0 == (ped->bEndpointAddress & 0x80) || ped->bmAttributes != USB_ENDPOINT_TYPE_BULK || ped->wMaxPacketSize > 64)
    {
        KdPrint((DRIVERNAME " - Endpoint has wrong attributes\n"));
        status = STATUS_DEVICE_CONFIGURATION_ERROR;
        goto cleanup;
    }
    ++ped;
    if(!ped || 0 != (ped->bEndpointAddress & 0x80) || ped->bmAttributes != USB_ENDPOINT_TYPE_BULK || ped->wMaxPacketSize > 64)
    {
        KdPrint((DRIVERNAME " - Endpoint has wrong attributes\n"));
        status = STATUS_DEVICE_CONFIGURATION_ERROR;
        goto cleanup;
    }
    ++ped;

    PUSBD_INTERFACE_INFORMATION pii = interfaces[0].Interface;
    ASSERT(pii->NumberOfPipes == pid->bNumEndpoints);

    // Initialize the maximum transfer size for each of the endpoints. The
    // default would be PAGE_SIZE. The firmware itself only has a 4096-byte
    // ring buffer, though. We need to restrict the test applet to that many
    // bytes. In order to exercise the multi-segment aspect of the transfer code,
    // therefore, reduce the maximum transfer size to 1024 bytes.

    pii->Pipes[0].MaximumTransferSize = USBD_DEFAULT_MAXIMUM_TRANSFER_SIZE;
    pii->Pipes[1].MaximumTransferSize = USBD_DEFAULT_MAXIMUM_TRANSFER_SIZE;
    pdx->maxtransfer = USBD_DEFAULT_MAXIMUM_TRANSFER_SIZE;

    // Submit the set-configuration request

    status = SendAwaitUrb(fdo, selurb);
    if(!NT_SUCCESS(status))
    {
        KdPrint((DRIVERNAME " - Error %X trying to select configuration\n", status));
        goto cleanup;
    }

    // Save the configuration and pipe handles
    pdx->hconfig = selurb->UrbSelectConfiguration.ConfigurationHandle;
    pdx->hinpipe = pii->Pipes[0].PipeHandle;
    pdx->houtpipe = pii->Pipes[1].PipeHandle;

    // Transfer ownership of the configuration descriptor to the device extension

    pdx->pcd = pcd;
    pcd = NULL;

    // Enable the interface
    IoSetDeviceInterfaceState(&pdx->operationsInterfaceName, TRUE);        

    // Enable the interface
    IoSetDeviceInterfaceState(&pdx->inquiriesInterfaceName, TRUE);                    

    // create recovery thread                     
    status = PsCreateSystemThread(&RecoveryHandle, 0, NULL, NULL, NULL, RecoveryThread, (PVOID)pdx);

    if(!NT_SUCCESS(status))
    {
        KdPrint((DRIVERNAME " - PsCreateSystemThread failed with error %08x\n", status));
        goto cleanup;
    }
    
    status = ObReferenceObjectByHandle( RecoveryHandle,
                                        SYNCHRONIZE,
                                        NULL,
                                        KernelMode,
                                        (PVOID*)&pdx->RecoveryThread,
                                        NULL );

    ASSERT(NT_SUCCESS(status));

    ZwClose(RecoveryHandle);
    
    // Start polling
    status = StartPolling(pdx);
    
    if(!NT_SUCCESS(status))
    {   
        KdPrint((DRIVERNAME " - StartPolling failed 0x%08x\n", status));

        if(pdx->RecoveryThread)
        {
            // shutdown recovery thread
            pdx->RecoveryExit = TRUE; 
            KeSetEvent(&pdx->RecoveryEvent, 0, FALSE);    
            // wait for polling thread to exit 
            KeWaitForSingleObject(pdx->RecoveryThread, Executive, KernelMode, FALSE, NULL);     
            ObDereferenceObject(pdx->RecoveryThread);
            pdx->RecoveryThread = NULL;
        }
        
        goto cleanup;
    }
    
cleanup:
    if(selurb) ExFreePool(selurb);
    if(pcd   ) ExFreePool(pcd   );

    // get rid of return codes like STATUS_PENDING
    if(NT_SUCCESS(status)) 
    {
        return STATUS_SUCCESS;
    }
    
    return status;
}