EMILE/libscsi/scsi_command.c

94 lines
1.5 KiB
C
Raw Normal View History

2005-11-17 06:44:44 +00:00
/*
*
* (c) 2004, 2005 Laurent Vivier <Laurent@Vivier.EU>
2005-11-17 06:44:44 +00:00
*
*/
#include <stdio.h>
#include <sys/types.h>
2005-11-17 06:44:44 +00:00
#include <scsi/scsi.h>
#include <macos/types.h>
#include <macos/errors.h>
2006-10-27 09:19:53 +00:00
#include <macos/scsi.h>
2006-10-30 21:52:58 +00:00
#include <macos/lowmem.h>
2005-11-17 06:44:44 +00:00
#include "libscsi.h"
2007-09-03 19:44:42 +00:00
#define COMPLETION_TIMEOUT (30*60) /* 30 seconds */
2005-11-17 06:44:44 +00:00
2006-10-30 21:52:58 +00:00
#define SCSI_BUSY (1 << 6)
#define SCSI_SEL (1 << 1)
static inline int scsi_busy(void)
{
return (SCSIStat() & (SCSI_BUSY | SCSI_SEL)) != 0;
}
static inline int scsi_wait_bus()
{
int timeout;
timeout = Ticks + 300;
while (scsi_busy())
if (Ticks > timeout)
return scsiBusy;
return noErr;
}
int scsi_command(int target, unsigned char* cdb, int count, TIB_t* tib)
2005-11-17 06:44:44 +00:00
{
int err;
short stat;
short message;
2006-10-30 21:52:58 +00:00
err = scsi_wait_bus();
if (err != noErr)
{
printf("SCSI bus is busy\n");
return err;
}
2005-11-17 06:44:44 +00:00
err = SCSIGet();
if (err != noErr)
{
printf("Cannot get SCSI bus (%d)\n", err);
return err;
}
err = SCSISelect(target);
if (err != noErr)
{
printf("Cannot select target %d (%d)\n", target, err);
return err;
}
err = SCSICmd(cdb, count);
if (err != noErr)
{
printf("Cannot send command (%d)\n", err);
goto complete;
}
if (tib != NULL)
2005-11-17 06:44:44 +00:00
{
err = SCSIRead(tib);
if ((err != scPhaseErr) && (err != noErr))
{
printf("Cannot read data (%d)\n", err);
goto complete;
}
2005-11-17 06:44:44 +00:00
}
err = SCSIComplete(&stat, &message, COMPLETION_TIMEOUT);
if (err != noErr)
return err;
return noErr;
complete:
SCSIComplete(&stat, &message, COMPLETION_TIMEOUT);
return err;
2005-11-17 06:44:44 +00:00
}