axwin2 - Little cleanup
[tpg/acess2.git] / Modules / Storage / FDD / fdd.c
index 6ad2b1a..3b886e1 100644 (file)
@@ -69,7 +69,7 @@ enum FloppyPorts {
 
 enum FloppyCommands {
        FIX_DRIVE_DATA  = 0x03,
-       HECK_DRIVE_STATUS       = 0x04,
+       CHECK_DRIVE_STATUS      = 0x04,
        CALIBRATE_DRIVE = 0x07,
        CHECK_INTERRUPT_STATUS = 0x08,
        SEEK_TRACK              = 0x0F,
@@ -88,7 +88,7 @@ enum FloppyCommands {
 void   FDD_UnloadModule();
 // --- VFS Methods
 char   *FDD_ReadDir(tVFS_Node *Node, int pos);
-tVFS_Node      *FDD_FindDir(tVFS_Node *dirNode, char *Name);
+tVFS_Node      *FDD_FindDir(tVFS_Node *dirNode, const char *Name);
  int   FDD_IOCtl(tVFS_Node *Node, int ID, void *Data);
 Uint64 FDD_ReadFS(tVFS_Node *node, Uint64 off, Uint64 len, void *buffer);
 // --- Functions for IOCache/DrvUtil
@@ -105,15 +105,15 @@ void      FDD_int_SendByte(int base, char byte);
 void   FDD_Reset(int id);
 void   FDD_Recalibrate(int disk);
  int   FDD_int_SeekTrack(int disk, int head, int track);
-void   FDD_int_TimerCallback(int arg);
-void   FDD_int_StopMotor(int disk);
-void   FDD_int_StartMotor(int disk);
+void   FDD_int_TimerCallback(void *Arg);
+void   FDD_int_StopMotor(void *Arg);
+void   FDD_int_StartMotor(int Disk);
  int   FDD_int_GetDims(int type, int lba, int *c, int *h, int *s, int *spt);
 
 // === GLOBALS ===
 MODULE_DEFINE(0, FDD_VERSION, FDD, FDD_Install, NULL, "ISADMA", NULL);
 t_floppyDevice gFDD_Devices[2];
-tSpinlock      glFDD;
+tMutex glFDD;
 volatile int   gbFDD_IrqFired = 0;
 tDevFS_Driver  gFDD_DriverInfo = {
        NULL, "fdd",
@@ -136,6 +136,7 @@ tDevFS_Driver       gFDD_DriverInfo = {
 int FDD_Install(char **Arguments)
 {
        Uint8 data;
+       char    **args = Arguments;
        
        // Determine Floppy Types (From CMOS)
        outb(0x70, 0x10);
@@ -145,6 +146,14 @@ int FDD_Install(char **Arguments)
        gFDD_Devices[0].track[0] = -1;
        gFDD_Devices[1].track[1] = -1;
        
+       if(args) {
+               for(;*args;args++)
+               {
+                       if(strcmp(*args, "disable")==0)
+                               return MODULE_ERR_NOTNEEDED;
+               }
+       }
+       
        Log_Log("FDD", "Detected Disk 0: %s and Disk 1: %s", cFDD_TYPES[data>>4], cFDD_TYPES[data&0xF]);
        
        if( data == 0 ) {
@@ -204,13 +213,13 @@ int FDD_Install(char **Arguments)
 void FDD_UnloadModule()
 {
         int    i;
-       //DevFS_DelDevice( &gFDD_DriverInfo );
-       LOCK(&glFDD);
+       DevFS_DelDevice( &gFDD_DriverInfo );
+       Mutex_Acquire(&glFDD);
        for(i=0;i<4;i++) {
                Time_RemoveTimer(gFDD_Devices[i].timer);
-               FDD_int_StopMotor(i);
+               FDD_int_StopMotor((void *)(Uint)i);
        }
-       RELEASE(&glFDD);
+       Mutex_Release(&glFDD);
        //IRQ_Clear(6);
 }
 
@@ -218,7 +227,7 @@ void FDD_UnloadModule()
  * \fn char *FDD_ReadDir(tVFS_Node *Node, int pos)
  * \brief Read Directory
  */
-char *FDD_ReadDir(tVFS_Node *Node, int Pos)
+char *FDD_ReadDir(tVFS_Node *UNUSED(Node), int Pos)
 {
        char    name[2] = "0\0";
 
@@ -232,10 +241,10 @@ char *FDD_ReadDir(tVFS_Node *Node, int Pos)
 }
 
 /**
- * \fn tVFS_Node *FDD_FindDir(tVFS_Node *Node, char *filename);
+ * \fn tVFS_Node *FDD_FindDir(tVFS_Node *Node, const char *filename);
  * \brief Find File Routine (for vfs_node)
  */
-tVFS_Node *FDD_FindDir(tVFS_Node *Node, char *Filename)
+tVFS_Node *FDD_FindDir(tVFS_Node *UNUSED(Node), const char *Filename)
 {
         int    i;
        
@@ -278,14 +287,11 @@ static const char *casIOCTLS[] = {DRV_IOCTLNAMES,DRV_DISK_IOCTLNAMES,NULL};
  * \fn int FDD_IOCtl(tVFS_Node *Node, int id, void *data)
  * \brief Stub ioctl function
  */
-int FDD_IOCtl(tVFS_Node *Node, int ID, void *Data)
+int FDD_IOCtl(tVFS_Node *UNUSED(Node), int ID, void *Data)
 {
        switch(ID)
        {
-       case DRV_IOCTL_TYPE:    return DRV_TYPE_DISK;
-       case DRV_IOCTL_IDENT:   return ModUtil_SetIdent(Data, "FDD");
-       case DRV_IOCTL_VERSION: return FDD_VERSION;
-       case DRV_IOCTL_LOOKUP:  return ModUtil_LookupString((char**)casIOCTLS, Data);
+       BASE_IOCTLS(DRV_TYPE_DISK, "FDD", FDD_VERSION, casIOCTLS);
        
        case DISK_IOCTL_GETBLOCKSIZE:   return 512;     
        
@@ -364,11 +370,11 @@ int FDD_int_ReadWriteSector(Uint32 Disk, Uint64 SectorAddr, int Write, void *Buf
        }
        LOG("Cyl=%i, Head=%i, Sector=%i", cyl, head, sec);
        
-       LOCK(&glFDD);   // Lock to stop the motor stopping on us
+       Mutex_Acquire(&glFDD);  // Lock to stop the motor stopping on us
        Time_RemoveTimer(gFDD_Devices[Disk].timer);     // Remove Old Timer
        // Start motor if needed
        if(gFDD_Devices[Disk].motorState != 2)  FDD_int_StartMotor(Disk);
-       RELEASE(&glFDD);
+       Mutex_Release(&glFDD);
        
        LOG("Wait for the motor to spin up");
        
@@ -376,7 +382,7 @@ int FDD_int_ReadWriteSector(Uint32 Disk, Uint64 SectorAddr, int Write, void *Buf
        while(gFDD_Devices[Disk].motorState == 1)       Threads_Yield();
        
        LOG("Acquire Spinlock");
-       LOCK(&glFDD);
+       Mutex_Acquire(&glFDD);
        
        // Seek to track
        outb(base + CALIBRATE_DRIVE, 0);
@@ -384,7 +390,7 @@ int FDD_int_ReadWriteSector(Uint32 Disk, Uint64 SectorAddr, int Write, void *Buf
        while(FDD_int_SeekTrack(Disk, head, (Uint8)cyl) == 0 && i++ < FDD_SEEK_TIMEOUT )
                Threads_Yield();
        if( i > FDD_SEEK_TIMEOUT ) {
-               RELEASE(&glFDD);
+               Mutex_Release(&glFDD);
                LEAVE('i', 0);
                return 0;
        }
@@ -392,13 +398,10 @@ int FDD_int_ReadWriteSector(Uint32 Disk, Uint64 SectorAddr, int Write, void *Buf
                
        // Read Data from DMA
        LOG("Setting DMA for read");
-       DMA_SetChannel(2, 512, !Write); // Read 512 Bytes from channel 2
+       DMA_SetChannel(2, 512, !Write); // Read/Write 512 Bytes from channel 2
        
        LOG("Sending command");
        
-       //Threads_Wait(100);    // Wait for Head to settle
-       Time_Delay(100);
-       
        for( i = 0; i < FDD_MAX_READWRITE_ATTEMPTS; i ++ )
        {
                if( Write )
@@ -435,7 +438,7 @@ int FDD_int_ReadWriteSector(Uint32 Disk, Uint64 SectorAddr, int Write, void *Buf
                st1 = FDD_int_GetByte(base);
                st2 = FDD_int_GetByte(base);
                
-               // Cylinder, Head and Sector (mutilated in some way
+               // Cylinder, Head and Sector (mutilated in some way)
                rcy = FDD_int_GetByte(base);
                rhe = FDD_int_GetByte(base);
                rse = FDD_int_GetByte(base);
@@ -481,7 +484,7 @@ int FDD_int_ReadWriteSector(Uint32 Disk, Uint64 SectorAddr, int Write, void *Buf
        
        // Release Spinlock
        LOG("Realeasing Spinlock and setting motor to stop");
-       RELEASE(&glFDD);
+       Mutex_Release(&glFDD);
        
        if(i == FDD_MAX_READWRITE_ATTEMPTS) {
                Log_Warning("FDD", "Exceeded %i attempts in %s the disk",
@@ -493,6 +496,7 @@ int FDD_int_ReadWriteSector(Uint32 Disk, Uint64 SectorAddr, int Write, void *Buf
        // Don't turn the motor off now, wait for a while
        gFDD_Devices[Disk].timer = Time_CreateTimer(MOTOR_OFF_DELAY, FDD_int_StopMotor, (void*)(tVAddr)Disk);
 
+       // Error check
        if( i < FDD_MAX_READWRITE_ATTEMPTS ) {
                LEAVE('i', 0);
                return 0;
@@ -574,6 +578,10 @@ int FDD_int_SeekTrack(int disk, int head, int track)
        
        // Set Track in structure
        gFDD_Devices[disk].track[head] = track;
+       
+       // Wait for Head to settle
+       Time_Delay(100);
+       
        return 1;
 }
 
@@ -656,11 +664,12 @@ inline void FDD_WaitIRQ()
 
 void FDD_SensInt(int base, Uint8 *sr0, Uint8 *cyl)
 {
+       Uint8   byte;
        FDD_int_SendByte(base, CHECK_INTERRUPT_STATUS);
-       if(sr0) *sr0 = FDD_int_GetByte(base);
-       else    FDD_int_GetByte(base);
-       if(cyl) *cyl = FDD_int_GetByte(base);
-       else    FDD_int_GetByte(base);
+       byte = FDD_int_GetByte(base);
+       if(sr0) *sr0 = byte;
+       byte = FDD_int_GetByte(base);
+       if(cyl) *cyl = byte;
 }
 
 /**
@@ -669,22 +678,26 @@ void FDD_SensInt(int base, Uint8 *sr0, Uint8 *cyl)
  */
 void FDD_int_SendByte(int base, char byte)
 {
-       volatile int state;
-       int timeout = 128;
-       for( ; timeout--; )
+        int    timeout = 128;
+       
+       while( (inb(base + PORT_MAINSTATUS) & 0xC0) != 0x80 && timeout-- )
+               inb(0x80);      //Delay
+       
+       if( timeout >= 0 )
        {
-           state = inb(base + PORT_MAINSTATUS);
-           if ((state & 0xC0) == 0x80)
-           {
-               outb(base + PORT_DATA, byte);
-               return;
-           }
-           inb(0x80);  //Delay
+               #if 0 && DEBUG
+               static int totalTimeout = 0;
+               static int totalCount = 0;
+               totalTimeout += timeout;
+               totalCount ++;
+               LOG("timeout = %i, average %i", timeout, totalTimeout/totalCount);
+               #endif
+               outb(base + PORT_DATA, byte);
+       }
+       else
+       {
+               Log_Warning("FDD", "FDD_int_SendByte: Timeout sending byte 0x%x to base 0x%x\n", byte, base);
        }
-       
-       #if WARN
-       Warning("FDD_int_SendByte - Timeout sending byte 0x%x to base 0x%x\n", byte, base);
-       #endif
 }
 
 /**
@@ -693,16 +706,27 @@ void FDD_int_SendByte(int base, char byte)
  */
 int FDD_int_GetByte(int base)
 {
-       volatile int state;
-       int timeout;
-       for( timeout = 128; timeout--; )
+        int    timeout = 128;
+       
+       while( (inb(base + PORT_MAINSTATUS) & 0xd0) != 0xd0 && timeout-- )
+               inb(0x80);      //Delay
+       
+       if( timeout >= 0 )
        {
-           state = inb((base + PORT_MAINSTATUS));
-           if ((state & 0xd0) == 0xd0)
-                   return inb(base + PORT_DATA);
-           inb(0x80);
+               #if 0 && DEBUG
+               static int totalTimeout = 0;
+               static int totalCount = 0;
+               totalTimeout += timeout;
+               totalCount ++;
+               LOG("timeout = %i, average %i", timeout, totalTimeout/totalCount);
+               #endif
+           return inb(base + PORT_DATA);
+       }
+       else
+       {
+               Log_Warning("FDD", "FDD_int_GetByte: Timeout reading byte from base 0x%x\n", base);
+               return -1;
        }
-       return -1;
 }
 
 /**
@@ -726,7 +750,7 @@ void FDD_Recalibrate(int disk)
        FDD_SensInt(cPORTBASE[disk>>1], NULL, NULL);
        
        LOG("Stopping Motor");
-       FDD_int_StopMotor(disk);
+       gFDD_Devices[disk].timer = Time_CreateTimer(MOTOR_OFF_DELAY, FDD_int_StopMotor, (void*)(Uint)disk);
        LEAVE('-');
 }
 
@@ -766,13 +790,14 @@ void FDD_Reset(int id)
  * \fn void FDD_int_TimerCallback()
  * \brief Called by timer
  */
-void FDD_int_TimerCallback(int arg)
+void FDD_int_TimerCallback(void *Arg)
 {
-       ENTER("iarg", arg);
-       if(gFDD_Devices[arg].motorState == 1)
-               gFDD_Devices[arg].motorState = 2;
-       Time_RemoveTimer(gFDD_Devices[arg].timer);
-       gFDD_Devices[arg].timer = -1;
+        int    disk = (Uint)Arg;
+       ENTER("iarg", disk);
+       if(gFDD_Devices[disk].motorState == 1)
+               gFDD_Devices[disk].motorState = 2;
+       Time_RemoveTimer(gFDD_Devices[disk].timer);
+       gFDD_Devices[disk].timer = -1;
        LEAVE('-');
 }
 
@@ -787,17 +812,17 @@ void FDD_int_StartMotor(int disk)
        state |= 1 << (4+disk);
        outb( cPORTBASE[ disk>>1 ] + PORT_DIGOUTPUT, state );
        gFDD_Devices[disk].motorState = 1;
-       gFDD_Devices[disk].timer = Time_CreateTimer(MOTOR_ON_DELAY, FDD_int_TimerCallback, (void*)(tVAddr)disk);
+       gFDD_Devices[disk].timer = Time_CreateTimer(MOTOR_ON_DELAY, FDD_int_TimerCallback, (void*)(Uint)disk);
 }
 
 /**
  * \fn void FDD_int_StopMotor(int disk)
  * \brief Stops FDD Motor
  */
-void FDD_int_StopMotor(int disk)
+void FDD_int_StopMotor(void *Arg)
 {
-       Uint8   state;
-       if( IS_LOCKED(&glFDD) ) return ;
+       Uint8   state, disk = (Uint)Arg;
+       if( Mutex_IsLocked(&glFDD) )    return ;
        ENTER("iDisk", disk);
        
        state = inb( cPORTBASE[ disk>>1 ] + PORT_DIGOUTPUT );

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