memory card cleanup
This commit is contained in:
+148
-156
@@ -7,6 +7,7 @@
|
||||
#include "RageLog.h"
|
||||
#include "ScreenManager.h"
|
||||
#include "ProfileManager.h"
|
||||
#include "Foreach.h"
|
||||
|
||||
MemoryCardManager* MEMCARDMAN = NULL; // global and accessable from anywhere in our program
|
||||
|
||||
@@ -18,18 +19,12 @@ MemoryCardManager::MemoryCardManager()
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
m_bTooLate[p] = false;
|
||||
m_bWriteError[p] = false;
|
||||
}
|
||||
m_pDriver->GetStorageDevices( m_vStorageDevices );
|
||||
|
||||
m_bDontPlaySoundsOnce = false;
|
||||
|
||||
m_soundReady.Load( THEME->GetPathToS("MemoryCardManager ready") );
|
||||
m_soundError.Load( THEME->GetPathToS("MemoryCardManager error") );
|
||||
m_soundTooLate.Load( THEME->GetPathToS("MemoryCardManager too late") );
|
||||
m_soundDisconnect.Load( THEME->GetPathToS("MemoryCardManager disconnect") );
|
||||
|
||||
UpdateUnassignedCards();
|
||||
}
|
||||
|
||||
MemoryCardManager::~MemoryCardManager()
|
||||
@@ -49,64 +44,168 @@ void MemoryCardManager::Update( float fDelta )
|
||||
|
||||
|
||||
// check for disconnects
|
||||
{
|
||||
for( unsigned i=0; i<vOld.size(); i++ )
|
||||
FOREACH( UsbStorageDevice, vOld, old )
|
||||
{
|
||||
bool bFoundMatch = false;
|
||||
FOREACH( UsbStorageDevice, vNew, newd )
|
||||
{
|
||||
if( old->IdsMatch(*newd) )
|
||||
{
|
||||
const UsbStorageDevice &old = vOld[i];
|
||||
if( find(vNew.begin(),vNew.end(),old) == vNew.end() ) // didn't find
|
||||
{
|
||||
LOG->Trace( ssprintf("Disconnected bus %d port %d device %d path %s", old.iBus, old.iPort, old.iLevel, old.sOsMountDir.c_str()) );
|
||||
vDisconnects.push_back( old );
|
||||
}
|
||||
bFoundMatch = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( !bFoundMatch )
|
||||
{
|
||||
LOG->Trace( "Disconnected bus %d port %d device %d path %s", old->iBus, old->iPort, old->iLevel, old->sOsMountDir.c_str() );
|
||||
vDisconnects.push_back( *old );
|
||||
}
|
||||
}
|
||||
|
||||
// check for connects
|
||||
{
|
||||
for( unsigned i=0; i<vNew.size(); i++ )
|
||||
{
|
||||
const UsbStorageDevice &newd = vNew[i];
|
||||
if( find(vOld.begin(),vOld.end(),newd) == vOld.end() ) // didn't find
|
||||
{
|
||||
LOG->Trace( ssprintf("Connected bus %d port %d device %d path %s", newd.iBus, newd.iPort, newd.iLevel, newd.sOsMountDir.c_str()) );
|
||||
vConnects.push_back( newd );
|
||||
}
|
||||
}
|
||||
}
|
||||
FOREACH( UsbStorageDevice, vNew, newd )
|
||||
{
|
||||
bool bFoundMatch = false;
|
||||
FOREACH( UsbStorageDevice, vOld, old )
|
||||
{
|
||||
if( old->IdsMatch(*newd) )
|
||||
{
|
||||
bFoundMatch = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( !bFoundMatch )
|
||||
{
|
||||
LOG->Trace( "Connected bus %d port %d device %d path %s", newd->iBus, newd->iPort, newd->iLevel, newd->sOsMountDir.c_str() );
|
||||
vDisconnects.push_back( *newd );
|
||||
}
|
||||
}
|
||||
|
||||
// unassign cards that were disconnected
|
||||
{
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
if( m_Device[p].IsBlank() ) // not assigned a card
|
||||
continue;
|
||||
|
||||
if( find(vDisconnects.begin(),vDisconnects.end(),m_Device[p]) != vDisconnects.end() ) // assigned card was disconnected
|
||||
{
|
||||
m_pDriver->Unmount(&m_Device[p], MEM_CARD_MOUNT_POINT[p]);
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
UsbStorageDevice &assigned_device = m_Device[p];
|
||||
if( assigned_device.IsBlank() ) // not assigned a card
|
||||
continue;
|
||||
|
||||
FOREACH( UsbStorageDevice, vDisconnects, disconnect )
|
||||
{
|
||||
if( disconnect->IdsMatch(assigned_device) )
|
||||
{
|
||||
m_pDriver->Unmount(&assigned_device, MEM_CARD_MOUNT_POINT[p]);
|
||||
|
||||
assigned_device.MakeBlank();
|
||||
m_soundDisconnect.Play();
|
||||
|
||||
if( PROFILEMAN->ProfileWasLoadedFromMemoryCard(p) )
|
||||
PROFILEMAN->UnloadProfile( p );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_Device[p].MakeBlank();
|
||||
m_soundDisconnect.Play();
|
||||
// Update the status of already-assigned cards. It may contain updated info
|
||||
// like WriteTest results and a new sName.
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
UsbStorageDevice &assigned_device = m_Device[p];
|
||||
if( assigned_device.IsBlank() ) // no card assigned to this player
|
||||
continue;
|
||||
|
||||
if( PROFILEMAN->ProfileWasLoadedFromMemoryCard((PlayerNumber)p) )
|
||||
PROFILEMAN->UnloadProfile( (PlayerNumber)p );
|
||||
}
|
||||
}
|
||||
}
|
||||
FOREACH( UsbStorageDevice, m_vStorageDevices, d )
|
||||
{
|
||||
if( d->IdsMatch(assigned_device) )
|
||||
{
|
||||
// play write test error sound if write test failed since we last checked
|
||||
if( assigned_device.bNeedsWriteTest && !d->bNeedsWriteTest && !d->bWriteTestSucceeded )
|
||||
m_soundError.Play();
|
||||
assigned_device = *d;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UpdateUnassignedCards();
|
||||
// make a list of unassigned
|
||||
vector<UsbStorageDevice> vUnassignedDevices = m_vStorageDevices; // copy
|
||||
|
||||
// remove cards that are already assigned
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
UsbStorageDevice &assigned_device = m_Device[p];
|
||||
if( assigned_device.IsBlank() ) // no card assigned to this player
|
||||
continue;
|
||||
|
||||
FOREACH( UsbStorageDevice, vUnassignedDevices, d )
|
||||
{
|
||||
if( d->IdsMatch(assigned_device) )
|
||||
{
|
||||
vUnassignedDevices.erase( d );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// try to assign each device to a player
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
LOG->Trace( "Looking for a card for Player %d", p+1 );
|
||||
|
||||
UsbStorageDevice &assigned_device = m_Device[p];
|
||||
if( !assigned_device.IsBlank() ) // they already have an assigned card
|
||||
{
|
||||
LOG->Trace( "Player %d already has a card: '%s'", p+1, assigned_device.sOsMountDir.c_str() );
|
||||
continue; // skip
|
||||
}
|
||||
|
||||
FOREACH( UsbStorageDevice, vUnassignedDevices, d )
|
||||
{
|
||||
// search for card dir match
|
||||
if( !PREFSMAN->m_sMemoryCardOsMountPoint[p].empty() &&
|
||||
d->sOsMountDir.CompareNoCase(PREFSMAN->m_sMemoryCardOsMountPoint[p]) )
|
||||
continue; // not a match
|
||||
|
||||
// search for USB bus match
|
||||
if( PREFSMAN->m_iMemoryCardUsbBus[p] != -1 &&
|
||||
PREFSMAN->m_iMemoryCardUsbBus[p] != d->iBus )
|
||||
continue; // not a match
|
||||
|
||||
if( PREFSMAN->m_iMemoryCardUsbPort[p] != -1 &&
|
||||
PREFSMAN->m_iMemoryCardUsbPort[p] != d->iPort )
|
||||
continue; // not a match
|
||||
|
||||
if( PREFSMAN->m_iMemoryCardUsbLevel[p] != -1 &&
|
||||
PREFSMAN->m_iMemoryCardUsbLevel[p] != d->iLevel )
|
||||
continue; // not a match
|
||||
|
||||
LOG->Trace( "device match: iScsiIndex: %d, iBus: %d, iLevel: %d, iPort: %d, sOsMountDir: %s",
|
||||
d->iScsiIndex, d->iBus, d->iLevel, d->iPort, d->sOsMountDir.c_str() );
|
||||
|
||||
assigned_device = *d; // save a copy
|
||||
vUnassignedDevices.erase( d ); // remove the device so we don't match it for another player
|
||||
m_bTooLate[p] = m_bCardsLocked; // the device is too late if inserted when cards were locked
|
||||
|
||||
// play sound
|
||||
if( m_bTooLate[p] )
|
||||
m_soundTooLate.Play();
|
||||
else if( !d->bNeedsWriteTest && !d->bWriteTestSucceeded )
|
||||
m_soundError.Play();
|
||||
else
|
||||
m_soundReady.Play();
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
SCREENMAN->RefreshCreditsMessages();
|
||||
}
|
||||
}
|
||||
|
||||
MemoryCardState MemoryCardManager::GetCardState( PlayerNumber pn )
|
||||
{
|
||||
if( m_Device[pn].IsBlank() )
|
||||
UsbStorageDevice &d = m_Device[pn];
|
||||
if( d.IsBlank() )
|
||||
return MEMORY_CARD_STATE_NO_CARD;
|
||||
else if( m_bTooLate[pn] )
|
||||
return MEMORY_CARD_STATE_TOO_LATE;
|
||||
else if( m_bWriteError[pn] )
|
||||
else if( !d.bNeedsWriteTest && !d.bWriteTestSucceeded )
|
||||
return MEMORY_CARD_STATE_WRITE_ERROR;
|
||||
else
|
||||
return MEMORY_CARD_STATE_READY;
|
||||
@@ -133,10 +232,7 @@ void MemoryCardManager::LockCards( bool bLock )
|
||||
if( m_Device[p].IsBlank() ) // they don't have an assigned card
|
||||
continue;
|
||||
|
||||
if( !m_pDriver->MountAndTestWrite(&m_Device[p], MEM_CARD_MOUNT_POINT[p]) )
|
||||
{
|
||||
m_bWriteError[p] = true;
|
||||
}
|
||||
m_pDriver->MountAndTestWrite(&m_Device[p], MEM_CARD_MOUNT_POINT[p]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,123 +243,19 @@ void MemoryCardManager::LockCards( bool bLock )
|
||||
m_pDriver->SetMountThreadState( MemoryCardDriver::detect_and_mount );
|
||||
}
|
||||
|
||||
void MemoryCardManager::UpdateUnassignedCards()
|
||||
{
|
||||
LOG->Trace( "UpdateUnassignedCards" );
|
||||
|
||||
// make a list of unassigned
|
||||
vector<UsbStorageDevice> vUnassignedDevices = m_vStorageDevices; // copy
|
||||
|
||||
// remove cards that are already assigned
|
||||
{
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
if( m_Device[p].IsBlank() ) // no card assigned to this player
|
||||
continue;
|
||||
|
||||
vector<UsbStorageDevice>::iterator it = find(vUnassignedDevices.begin(),vUnassignedDevices.end(),m_Device[p]);
|
||||
if( it != vUnassignedDevices.end() )
|
||||
{
|
||||
// TRICKY: Update m_Device with the latest info we received from the
|
||||
// driver. It may contain updated info like WriteTest results and a
|
||||
// sName.
|
||||
m_Device[p] = *it;
|
||||
|
||||
vUnassignedDevices.erase( it );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// try to assign each device to a player
|
||||
{
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
LOG->Trace( "Looking for a card for Player %d", p+1 );
|
||||
|
||||
if( !m_Device[p].IsBlank() ) // they already have an assigned card
|
||||
{
|
||||
LOG->Trace( "Player %d already has a card: '%s'", p+1, m_Device[p].sOsMountDir.c_str() );
|
||||
continue; // skip
|
||||
}
|
||||
|
||||
unsigned i;
|
||||
|
||||
// search for card dir match
|
||||
if( !PREFSMAN->m_sMemoryCardOsMountPoint[p].empty() )
|
||||
{
|
||||
for( i=0; i<vUnassignedDevices.size(); i++ )
|
||||
{
|
||||
UsbStorageDevice &usbd = vUnassignedDevices[i];
|
||||
if( usbd.sOsMountDir.CompareNoCase(PREFSMAN->m_sMemoryCardOsMountPoint[p]) == 0 ) // match
|
||||
{
|
||||
LOG->Trace( "dir match: iScsiIndex: %d, iBus: %d, iLevel: %d, iPort: %d",
|
||||
usbd.iScsiIndex, usbd.iBus, usbd.iLevel, usbd.iPort );
|
||||
goto match;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// search for USB bus match
|
||||
for( i=0; i<vUnassignedDevices.size(); i++ )
|
||||
{
|
||||
UsbStorageDevice &usbd = vUnassignedDevices[i];
|
||||
|
||||
if( PREFSMAN->m_iMemoryCardUsbBus[p] != -1 &&
|
||||
PREFSMAN->m_iMemoryCardUsbBus[p] != usbd.iBus )
|
||||
continue; // not a match
|
||||
|
||||
if( PREFSMAN->m_iMemoryCardUsbPort[p] != -1 &&
|
||||
PREFSMAN->m_iMemoryCardUsbPort[p] != usbd.iPort )
|
||||
continue; // not a match
|
||||
|
||||
if( PREFSMAN->m_iMemoryCardUsbLevel[p] != -1 &&
|
||||
PREFSMAN->m_iMemoryCardUsbLevel[p] != usbd.iLevel )
|
||||
continue; // not a match
|
||||
|
||||
LOG->Trace( "bus/port match: iScsiIndex: %d, iBus: %d, iLevel: %d, iPort: %d",
|
||||
usbd.iScsiIndex, usbd.iBus, usbd.iLevel, usbd.iPort );
|
||||
goto match;
|
||||
}
|
||||
|
||||
LOG->Trace( "Player %d memory card: none", p+1 );
|
||||
continue; // no matches
|
||||
match:
|
||||
m_Device[p] = vUnassignedDevices[i]; // save a copy
|
||||
LOG->Trace( "Player %d memory card: '%s'", p+1, m_Device[p].sOsMountDir.c_str() );
|
||||
vUnassignedDevices.erase( vUnassignedDevices.begin()+i ); // remove the device so we don't match it for another player
|
||||
m_bTooLate[p] = m_bCardsLocked;
|
||||
m_bWriteError[p] = false;
|
||||
|
||||
if( !m_bDontPlaySoundsOnce )
|
||||
{
|
||||
// play sound
|
||||
if( m_bWriteError[p] )
|
||||
m_soundError.Play();
|
||||
else if( m_bTooLate[p] )
|
||||
m_soundTooLate.Play();
|
||||
else
|
||||
m_soundReady.Play();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_bDontPlaySoundsOnce = false;
|
||||
}
|
||||
|
||||
void MemoryCardManager::FlushAndReset()
|
||||
{
|
||||
FOREACH_PlayerNumber( p )
|
||||
{
|
||||
if( m_Device[p].IsBlank() ) // no card assigned
|
||||
UsbStorageDevice &d = m_Device[p];
|
||||
if( d.IsBlank() ) // no card assigned
|
||||
continue; // skip
|
||||
if( m_bWriteError[p] || m_bTooLate[p] )
|
||||
if( (!d.bNeedsWriteTest && !d.bWriteTestSucceeded) || m_bTooLate[p] )
|
||||
continue; // skip
|
||||
m_pDriver->Flush(&m_Device[p]);
|
||||
}
|
||||
|
||||
m_pDriver->ResetUsbStorage(); // forces cards to be re-detected
|
||||
|
||||
m_bDontPlaySoundsOnce = true;
|
||||
}
|
||||
|
||||
bool MemoryCardManager::PathIsMemCard( CString sDir ) const
|
||||
|
||||
@@ -46,12 +46,8 @@ protected:
|
||||
|
||||
bool m_bCardsLocked;
|
||||
bool m_bTooLate[NUM_PLAYERS]; // card was inserted after lock
|
||||
bool m_bWriteError[NUM_PLAYERS]; // couldn't write to the card
|
||||
UsbStorageDevice m_Device[NUM_PLAYERS]; // device in the memory card slot, NULL if none
|
||||
|
||||
// don't play sounds on the first assign after resetting the driver
|
||||
bool m_bDontPlaySoundsOnce;
|
||||
|
||||
RageSound m_soundReady;
|
||||
RageSound m_soundError;
|
||||
RageSound m_soundTooLate;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#include "arch/arch_platform.h"
|
||||
|
||||
bool UsbStorageDevice::operator==(const UsbStorageDevice& other) const
|
||||
bool UsbStorageDevice::IdsMatch(const UsbStorageDevice& other) const
|
||||
{
|
||||
// LOG->Trace( "Comparing %d %d %d %s %s to %d %d %d %s %s",
|
||||
// iBus, iPort, iLevel, sName.c_str(), sOsMountDir.c_str(),
|
||||
|
||||
@@ -15,6 +15,8 @@ struct UsbStorageDevice
|
||||
sScsiDevice = "";
|
||||
sSerial = "";
|
||||
sOsMountDir = "";
|
||||
bNeedsWriteTest = true;
|
||||
bWriteTestSucceeded = false;
|
||||
sName = "";
|
||||
};
|
||||
int iBus;
|
||||
@@ -24,16 +26,30 @@ struct UsbStorageDevice
|
||||
int iScsiIndex;
|
||||
CString sScsiDevice;
|
||||
CString sOsMountDir; // WITHOUT trailing slash
|
||||
CString sName; // Name in the profile on the memory card.
|
||||
// This is passed here because it's read in the mounting thread.
|
||||
|
||||
bool bNeedsWriteTest;
|
||||
bool bWriteTestSucceeded; // only valid if bNeedsWriteTest == false
|
||||
CString sName; // Name in the profile on the memory card.
|
||||
|
||||
bool IsBlank() { return sOsMountDir.empty(); }
|
||||
|
||||
bool operator==(const UsbStorageDevice& other) const;
|
||||
bool operator!=(const UsbStorageDevice& other) const
|
||||
{
|
||||
return !operator==(other);
|
||||
}
|
||||
|
||||
bool operator==(const UsbStorageDevice& other) const
|
||||
{
|
||||
#define COMPARE(x) if( x != other.x ) return false;
|
||||
COMPARE( iBus );
|
||||
COMPARE( iPort );
|
||||
COMPARE( iLevel );
|
||||
COMPARE( sSerial );
|
||||
COMPARE( iScsiIndex );
|
||||
COMPARE( sScsiDevice );
|
||||
COMPARE( sOsMountDir );
|
||||
COMPARE( sName );
|
||||
COMPARE( bNeedsWriteTest );
|
||||
COMPARE( bWriteTestSucceeded );
|
||||
COMPARE( sOsMountDir );
|
||||
#undef COMPARE
|
||||
return true;
|
||||
}
|
||||
bool IdsMatch(const UsbStorageDevice& other) const;
|
||||
};
|
||||
|
||||
class MemoryCardDriver
|
||||
|
||||
@@ -25,7 +25,6 @@ MemoryCardDriverThreaded::MemoryCardDriverThreaded() :
|
||||
m_mutexStorageDevices("StorageDevices")
|
||||
{
|
||||
m_bShutdownNextUpdate = false;
|
||||
m_bForceRedetectNextUpdate = true;
|
||||
m_bStorageDevicesChanged = false;
|
||||
m_MountThreadState = detect_and_mount;
|
||||
}
|
||||
@@ -65,13 +64,6 @@ void MemoryCardDriverThreaded::SetMountThreadState( MountThreadState mts )
|
||||
if( old == paused && mts != paused )
|
||||
m_mutexPause.Unlock();
|
||||
|
||||
if( old == detect_and_dont_mount && mts == detect_and_mount )
|
||||
{
|
||||
m_bForceRedetectNextUpdate = true;
|
||||
LockMut( m_mutexStorageDevices );
|
||||
m_vStorageDevices.clear();
|
||||
}
|
||||
|
||||
m_MountThreadState = mts;
|
||||
}
|
||||
|
||||
@@ -121,7 +113,7 @@ void MemoryCardDriverThreaded::GetStorageDevices( vector<UsbStorageDevice>& vDev
|
||||
bool MemoryCardDriverThreaded::MountAndTestWrite( UsbStorageDevice* pDevice, CString sMountPoint )
|
||||
{
|
||||
LockMut( m_mutexStorageDevices );
|
||||
vector<UsbStorageDeviceEx>::const_iterator iter = find( m_vStorageDevices.begin(), m_vStorageDevices.end(), *pDevice );
|
||||
vector<UsbStorageDevice>::const_iterator iter = find( m_vStorageDevices.begin(), m_vStorageDevices.end(), *pDevice );
|
||||
if( iter == m_vStorageDevices.end() )
|
||||
{
|
||||
LOG->Warn( "Trying to mount a memory card that is no longer connected. bus %d port %d device %d path %s", pDevice->iBus, pDevice->iPort, pDevice->iLevel, pDevice->sOsMountDir.c_str() );
|
||||
|
||||
@@ -4,19 +4,6 @@
|
||||
#include "MemoryCardDriver.h"
|
||||
#include "RageThreads.h"
|
||||
|
||||
struct UsbStorageDeviceEx : public UsbStorageDevice
|
||||
{
|
||||
UsbStorageDeviceEx() { MakeBlank(); }
|
||||
|
||||
void MakeBlank()
|
||||
{
|
||||
UsbStorageDevice::MakeBlank();
|
||||
bWriteTestSucceeded = false;
|
||||
}
|
||||
|
||||
bool bWriteTestSucceeded;
|
||||
};
|
||||
|
||||
class MemoryCardDriverThreaded : public MemoryCardDriver
|
||||
{
|
||||
public:
|
||||
@@ -49,11 +36,9 @@ protected:
|
||||
virtual void Mount( UsbStorageDevice* pDevice, CString sMountPoint ) = 0;
|
||||
bool ShouldDoOsMount() { return m_MountThreadState==detect_and_mount; }
|
||||
|
||||
vector<UsbStorageDeviceEx> m_vStorageDevices;
|
||||
vector<UsbStorageDevice> m_vStorageDevices;
|
||||
bool m_bStorageDevicesChanged;
|
||||
RageMutex m_mutexStorageDevices; // protects the above two
|
||||
bool m_bForceRedetectNextUpdate; // on the next update, redetect from scratch report new devices found
|
||||
|
||||
|
||||
// placed here for use by derivitives to eliminate duplicate code
|
||||
void UnmountMountPoint( const CString &sMountPoint );
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "RageFileManager.h"
|
||||
#include "Profile.h"
|
||||
#include "PrefsManager.h"
|
||||
#include "Foreach.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
@@ -22,7 +23,7 @@ const CString TEMP_MOUNT_POINT = "@mctemp/";
|
||||
static const char *USB_DEVICE_LIST_FILE = "/proc/bus/usb/devices";
|
||||
static const char *ETC_MTAB = "/etc/mtab";
|
||||
|
||||
void GetNewStorageDevices( vector<UsbStorageDeviceEx>& vDevicesOut );
|
||||
void GetNewStorageDevices( vector<UsbStorageDevice>& vDevicesOut );
|
||||
|
||||
static int RunProgram( const char *path, char *const args[], CString &out )
|
||||
{
|
||||
@@ -147,23 +148,22 @@ void MemoryCardDriverThreaded_Linux::ResetUsbStorage()
|
||||
// if usb-storage gets in a bad state, resetting usb-storage will sometimes fix it.
|
||||
//
|
||||
|
||||
LockMut( m_mutexStorageDevices );
|
||||
|
||||
// unmount all devices before trying to remove the module
|
||||
for( unsigned i=0; i<m_vStorageDevices.size(); i++ )
|
||||
FOREACH( UsbStorageDevice, m_vDevicesLastSeen, d )
|
||||
{
|
||||
UsbStorageDeviceEx &d = m_vStorageDevices[i];
|
||||
CString sCommand = "umount " + d.sOsMountDir;
|
||||
LOG->Trace( "reset unmount %i/%i (%s)", i, m_vStorageDevices.size(), sCommand.c_str() );
|
||||
CString sCommand = "umount " + d->sOsMountDir;
|
||||
LOG->Trace( "reset unmount (%s)", sCommand.c_str() );
|
||||
ExecuteCommand( sCommand );
|
||||
LOG->Trace( "reset unmount %i/%i done", i, m_vStorageDevices.size() );
|
||||
LOG->Trace( "reset unmount done" );
|
||||
|
||||
// force a remount the next time cards aren't locked
|
||||
d->bNeedsWriteTest = true;
|
||||
d->bWriteTestSucceeded = false;
|
||||
}
|
||||
|
||||
ExecuteCommand( "rmmod usb-storage" );
|
||||
ExecuteCommand( "modprobe usb-storage" );
|
||||
|
||||
m_bForceRedetectNextUpdate = true;
|
||||
|
||||
MountThreadDoOneUpdate();
|
||||
}
|
||||
|
||||
@@ -174,31 +174,40 @@ void MemoryCardDriverThreaded_Linux::MountThreadDoOneUpdate()
|
||||
usleep( 50000 );
|
||||
return;
|
||||
}
|
||||
|
||||
if( m_bForceRedetectNextUpdate )
|
||||
{
|
||||
m_vDevicesLastSeen.clear();
|
||||
m_bForceRedetectNextUpdate = false;
|
||||
// fall through
|
||||
}
|
||||
|
||||
bool bNeedToDoAnyMounts = false;
|
||||
for( unsigned i=0; i<m_vDevicesLastSeen.size(); i++ )
|
||||
{
|
||||
UsbStorageDevice &d = m_vDevicesLastSeen[i];
|
||||
if( d.bNeedsWriteTest )
|
||||
{
|
||||
bNeedToDoAnyMounts = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( bNeedToDoAnyMounts )
|
||||
{
|
||||
// fall through
|
||||
}
|
||||
else
|
||||
{
|
||||
pollfd pfd = { m_fd, POLLIN, 0 };
|
||||
int ret = poll( &pfd, 1, 100 );
|
||||
switch( ret )
|
||||
{
|
||||
case 1:
|
||||
// file changed. Fall through.
|
||||
break;
|
||||
case 0: // no change. Poll again.
|
||||
return;
|
||||
case -1:
|
||||
if( errno != EINTR )
|
||||
LOG->Warn( "Error polling: %s", strerror(errno) );
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
pollfd pfd = { m_fd, POLLIN, 0 };
|
||||
int ret = poll( &pfd, 1, 100 );
|
||||
switch( ret )
|
||||
{
|
||||
case 1:
|
||||
// file changed. Fall through.
|
||||
break;
|
||||
case 0: // no change. Poll again.
|
||||
return;
|
||||
case -1:
|
||||
if( errno != EINTR )
|
||||
LOG->Warn( "Error polling: %s", strerror(errno) );
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// TRICKY: We're waiting for a change in the USB device list, but
|
||||
// the usb-storage descriptors take a bit longer to update. It's more convenient to wait
|
||||
// on the USB device list because the usb-storage descriptors are separate files per
|
||||
@@ -206,52 +215,89 @@ void MemoryCardDriverThreaded_Linux::MountThreadDoOneUpdate()
|
||||
// usb-storage a chance to initialize.
|
||||
usleep(1000*300);
|
||||
|
||||
vector<UsbStorageDeviceEx> vDevicesNow;
|
||||
GetNewStorageDevices( vDevicesNow );
|
||||
|
||||
vector<UsbStorageDeviceEx> &vNew = vDevicesNow;
|
||||
vector<UsbStorageDeviceEx> &vOld = m_vDevicesLastSeen;
|
||||
vector<UsbStorageDevice> vNew;
|
||||
GetNewStorageDevices( vNew );
|
||||
vector<UsbStorageDevice> vOld = m_vDevicesLastSeen; // copy
|
||||
|
||||
// check for disconnects
|
||||
vector<UsbStorageDeviceEx*> vDisconnects;
|
||||
for( unsigned i=0; i<vOld.size(); i++ )
|
||||
vector<UsbStorageDevice> vDisconnects;
|
||||
FOREACH( UsbStorageDevice, vOld, old )
|
||||
{
|
||||
UsbStorageDeviceEx &old = vOld[i];
|
||||
if( find(vNew.begin(),vNew.end(),old) == vNew.end() )// didn't find
|
||||
bool bMatch = false;
|
||||
FOREACH( UsbStorageDevice, vNew, newd )
|
||||
{
|
||||
if( old->IdsMatch(*newd) )
|
||||
{
|
||||
LOG->Trace( "Disconnected bus %d port %d level %d path %s", old.iBus, old.iPort, old.iLevel, old.sOsMountDir.c_str() );
|
||||
vDisconnects.push_back( &old );
|
||||
bMatch = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( !bMatch ) // didn't find
|
||||
{
|
||||
LOG->Trace( "Disconnected bus %d port %d level %d path %s", old->iBus, old->iPort, old->iLevel, old->sOsMountDir.c_str() );
|
||||
vDisconnects.push_back( *old );
|
||||
|
||||
FOREACH( UsbStorageDevice, m_vDevicesLastSeen, d )
|
||||
{
|
||||
if( old->IdsMatch(*d) )
|
||||
{
|
||||
m_vDevicesLastSeen.erase( d );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// check for connects
|
||||
vector<UsbStorageDeviceEx*> vConnects;
|
||||
for( unsigned i=0; i<vNew.size(); i++ )
|
||||
{
|
||||
UsbStorageDeviceEx &newd = vNew[i];
|
||||
if( find(vOld.begin(),vOld.end(),newd) == vOld.end() )// didn't find
|
||||
{
|
||||
LOG->Trace( "Connected bus %d port %d level %d path %s", newd.iBus, newd.iPort, newd.iLevel, newd.sOsMountDir.c_str() );
|
||||
vConnects.push_back( &newd );
|
||||
}
|
||||
}
|
||||
|
||||
vector<UsbStorageDevice> vConnects;
|
||||
FOREACH( UsbStorageDevice, vNew, newd )
|
||||
{
|
||||
bool bMatch = false;
|
||||
FOREACH( UsbStorageDevice, vOld, old )
|
||||
{
|
||||
if( old->IdsMatch(*newd) )
|
||||
{
|
||||
bMatch = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( !bMatch ) // didn't find
|
||||
{
|
||||
LOG->Trace( "Connected bus %d port %d level %d path %s", newd->iBus, newd->iPort, newd->iLevel, newd->sOsMountDir.c_str() );
|
||||
vConnects.push_back( *newd );
|
||||
|
||||
m_vDevicesLastSeen.push_back( *newd );
|
||||
}
|
||||
}
|
||||
|
||||
bool bDidAnyMounts = false;
|
||||
|
||||
// unmount all disconnects
|
||||
if( ShouldDoOsMount() )
|
||||
{
|
||||
for( unsigned i=0; i<vDisconnects.size(); i++ )
|
||||
{
|
||||
UsbStorageDeviceEx &d = *vDisconnects[i];
|
||||
UsbStorageDevice &d = vDisconnects[i];
|
||||
CString sCommand = "umount " + d.sOsMountDir;
|
||||
LOG->Trace( "unmount disconnects %i/%i (%s)", i, vDisconnects.size(), sCommand.c_str() );
|
||||
ExecuteCommand( sCommand );
|
||||
LOG->Trace( "unmount disconnects %i/%i done", i, vDisconnects.size() );
|
||||
}
|
||||
|
||||
// mount all connects
|
||||
for( unsigned i=0; i<vConnects.size(); i++ )
|
||||
// mount all devices that need a write test
|
||||
for( unsigned i=0; i<m_vDevicesLastSeen.size(); i++ )
|
||||
{
|
||||
UsbStorageDeviceEx &d = *vConnects[i];
|
||||
UsbStorageDevice &d = m_vDevicesLastSeen[i];
|
||||
if( !d.bNeedsWriteTest )
|
||||
continue; // skip
|
||||
|
||||
bDidAnyMounts = true;
|
||||
|
||||
d.bNeedsWriteTest = false;
|
||||
|
||||
CString sCommand;
|
||||
|
||||
// unmount this device before trying to mount it. If this device
|
||||
@@ -278,28 +324,21 @@ void MemoryCardDriverThreaded_Linux::MountThreadDoOneUpdate()
|
||||
d.sName = profile.GetDisplayName();
|
||||
UnmountMountPoint( TEMP_MOUNT_POINT );
|
||||
|
||||
LOG->Trace( "write test %s", d.bWriteTestSucceeded ? "succeeded" : "failed" );
|
||||
LOG->Trace( "WriteTest: %s, Name: %s", d.bWriteTestSucceeded ? "succeeded" : "failed", d.sName.c_str() );
|
||||
}
|
||||
}
|
||||
|
||||
if( !vDisconnects.empty() || !vConnects.empty() )
|
||||
if( bDidAnyMounts || !vDisconnects.empty() || !vConnects.empty() )
|
||||
{
|
||||
LockMut( m_mutexStorageDevices );
|
||||
m_bStorageDevicesChanged = true;
|
||||
m_vStorageDevices = vDevicesNow;
|
||||
for( unsigned i=0; i<m_vStorageDevices.size(); i++ )
|
||||
{
|
||||
UsbStorageDeviceEx &d = m_vStorageDevices[i];
|
||||
LOG->Trace( "index %d, bWriteTestSucceeded %d", i, d.bWriteTestSucceeded );
|
||||
}
|
||||
m_vStorageDevices = m_vDevicesLastSeen;
|
||||
}
|
||||
|
||||
m_vDevicesLastSeen = vDevicesNow;
|
||||
|
||||
CHECKPOINT;
|
||||
}
|
||||
|
||||
bool ReadUsbStorageDescriptor( CString fn, int iScsiIndex, vector<UsbStorageDeviceEx>& vDevicesOut )
|
||||
bool ReadUsbStorageDescriptor( CString fn, int iScsiIndex, vector<UsbStorageDevice>& vDevicesOut )
|
||||
{
|
||||
LOG->Trace( "ReadUsbStorageDescriptor %s", fn.c_str() );
|
||||
|
||||
@@ -348,7 +387,7 @@ bool ReadUsbStorageDescriptor( CString fn, int iScsiIndex, vector<UsbStorageDevi
|
||||
|
||||
#if 0
|
||||
/* usbd must have the serial number filled in; fill in iPort and iLevel. */
|
||||
static bool GetPortAndLevelFromSerial( CString sSerial, UsbStorageDeviceEx &usbd )
|
||||
static bool GetPortAndLevelFromSerial( CString sSerial, UsbStorageDevice &usbd )
|
||||
{
|
||||
// Find all attached USB devices. Output looks like:
|
||||
// T: Bus=02 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
|
||||
@@ -410,7 +449,7 @@ static bool GetPortAndLevelFromSerial( CString sSerial, UsbStorageDeviceEx &usbd
|
||||
}
|
||||
#endif
|
||||
|
||||
void GetNewStorageDevices( vector<UsbStorageDeviceEx>& vDevicesOut )
|
||||
void GetNewStorageDevices( vector<UsbStorageDevice>& vDevicesOut )
|
||||
{
|
||||
LOG->Trace( "GetNewStorageDevices" );
|
||||
|
||||
@@ -432,7 +471,7 @@ void GetNewStorageDevices( vector<UsbStorageDeviceEx>& vDevicesOut )
|
||||
CStringArray vsLines;
|
||||
split( sOutput, "\n", vsLines );
|
||||
|
||||
UsbStorageDeviceEx usbd;
|
||||
UsbStorageDevice usbd;
|
||||
for( unsigned i=0; i<vsLines.size(); i++ )
|
||||
{
|
||||
CString &sLine = vsLines[i];
|
||||
|
||||
@@ -17,7 +17,7 @@ protected:
|
||||
virtual void MountThreadDoOneUpdate();
|
||||
|
||||
int m_fd;
|
||||
vector<UsbStorageDeviceEx> m_vDevicesLastSeen;
|
||||
vector<UsbStorageDevice> m_vDevicesLastSeen;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user