/* td.c - Handle all TrackDisk related tasks
** Copyright (C) 1997,1998 Karl J. Ots
** 
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
** 
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*--------------------*/
/* TrackDisk Routines */
/*--------------------*/

#include <exec/types.h>
#include <exec/io.h>
#include <devices/trackdisk.h>
#include <clib/exec_protos.h>

#include "td.h"


/* Private functions */
BYTE rwTrack (APTR  Buffer, UBYTE NumTracks, UBYTE TrackNum, 
              struct IOExtTD *DiskReq);


/* 
** Read a number of complete tracks from a specified unit.
** Return DoIO()'s return value.
*/
BYTE readTrack (APTR   rBuffer,
                UBYTE  rNumTracks,
                UBYTE  rTrackNum,
                struct IOExtTD *rDiskReq)
{
  /* Read from track */
  rDiskReq->iotd_Req.io_Command = CMD_READ;
  return rwTrack (rBuffer, rNumTracks, rTrackNum, rDiskReq);
}


/*
** Write a number of complete tracks to a specified unit with TD_FORMAT.
** Return DoIO()'s return value.
*/
BYTE writeTrack (APTR   wBuffer,
                 UBYTE  wNumTracks,
                 UBYTE  wTrackNum,
                 struct IOExtTD *wDiskReq)
{
  BYTE TDError;
  
  /* First format the track */
  wDiskReq->iotd_Req.io_Command = TD_FORMAT;
  TDError = rwTrack (wBuffer, wNumTracks, wTrackNum, wDiskReq);
  if (TDError) return TDError;

  /* Write to the track */
  wDiskReq->iotd_Req.io_Command = CMD_WRITE;
  return rwTrack (wBuffer, wNumTracks, wTrackNum, wDiskReq);
}


/*
** Perform track-sized IO on specified unit.
** Return DoIO()'s return value.
*/
BYTE rwTrack (APTR  Buffer,
              UBYTE NumTracks,
              UBYTE TrackNum,
              struct IOExtTD *DiskReq)
{
  DiskReq->iotd_Req.io_Flags   = 0;
  DiskReq->iotd_Req.io_Data    = Buffer;
  DiskReq->iotd_Req.io_Length  = NumTracks * TRACK_SIZE;
  DiskReq->iotd_Req.io_Offset  = TrackNum  * TRACK_SIZE;
  DiskReq->iotd_SecLabel       = NULL;
  
  return DoIO ((struct IORequest *)DiskReq);
}

/*
** Flush the track buffer on the specified unit.
** Return DoIO()'s return value.
*/
BYTE flushTrack (struct IOExtTD *DiskReq)
{
  DiskReq->iotd_Req.io_Command = CMD_UPDATE;
  return DoIO ((struct IORequest *)DiskReq);
}
