2005-11-21 21:57:27 +00:00
|
|
|
/*
|
|
|
|
*
|
2006-09-15 14:55:39 +00:00
|
|
|
* (c) 2005 Laurent Vivier <Laurent@lvivier.info>
|
2005-11-21 21:57:27 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-10-27 17:14:30 +00:00
|
|
|
#include <stdio.h>
|
2005-11-21 21:55:36 +00:00
|
|
|
#include <stdlib.h>
|
2006-11-01 17:47:32 +00:00
|
|
|
#include <scsi/scsi.h>
|
2005-11-21 21:55:36 +00:00
|
|
|
|
2005-11-22 00:53:48 +00:00
|
|
|
#include <macos/errors.h>
|
2005-11-21 21:55:36 +00:00
|
|
|
|
2005-11-22 00:53:48 +00:00
|
|
|
#include "libscsi.h"
|
2005-11-21 21:55:36 +00:00
|
|
|
|
2006-10-27 17:14:30 +00:00
|
|
|
#define BUFFER_SIZE (255)
|
2005-11-21 21:55:36 +00:00
|
|
|
scsi_device_t *scsi_open(int target)
|
|
|
|
{
|
|
|
|
scsi_device_t *dev;
|
2006-10-27 17:14:30 +00:00
|
|
|
unsigned char buff[BUFFER_SIZE];
|
2005-11-22 00:53:48 +00:00
|
|
|
OSErr err;
|
2006-11-01 17:47:32 +00:00
|
|
|
int retries;
|
2006-10-27 17:14:30 +00:00
|
|
|
|
|
|
|
err = scsi_INQUIRY(target, buff, BUFFER_SIZE);
|
|
|
|
if (err != noErr)
|
|
|
|
printf("WARNING: cannot execute INQUIRY\n");
|
|
|
|
|
2006-11-01 17:47:32 +00:00
|
|
|
retries = 0;
|
|
|
|
while (retries < 300)
|
|
|
|
{
|
|
|
|
err = scsi_TEST_UNIT_READY(target);
|
|
|
|
retries++;
|
|
|
|
if (err == noErr) {
|
|
|
|
err = scsi_REQUEST_SENSE(target, buff, BUFFER_SIZE);
|
|
|
|
if ((err == noErr) && (buff[2] != UNIT_ATTENTION) &&
|
|
|
|
(buff[2] != NOT_READY))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (retries == 300)
|
|
|
|
{
|
|
|
|
printf("ERROR: unit not ready !\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
retries = 0;
|
|
|
|
while (retries < 3)
|
|
|
|
{
|
|
|
|
err = scsi_READ_CAPACITY(target, buff, BUFFER_SIZE);
|
|
|
|
if (err == noErr)
|
|
|
|
break;
|
|
|
|
retries++;
|
|
|
|
}
|
2006-10-27 17:14:30 +00:00
|
|
|
if (err != noErr) {
|
|
|
|
printf("ERROR: cannot execute READ_CAPACITY\n");
|
2005-11-22 00:53:48 +00:00
|
|
|
return NULL;
|
2006-10-27 17:14:30 +00:00
|
|
|
}
|
2005-11-21 21:55:36 +00:00
|
|
|
|
|
|
|
dev = (scsi_device_t *)malloc(sizeof(scsi_device_t));
|
|
|
|
if (dev == NULL)
|
|
|
|
return NULL;
|
|
|
|
dev->target = target;
|
2005-11-22 00:53:48 +00:00
|
|
|
dev->capacity = (buff[0] << 24) | (buff[1] << 16) | (buff[2] << 8) | buff[3];
|
|
|
|
dev->sector_size = (buff[4] << 24) | (buff[5] << 16) | (buff[6] << 8) | buff[7];
|
2005-11-21 21:55:36 +00:00
|
|
|
|
2006-10-27 17:14:30 +00:00
|
|
|
if ((dev->sector_size != 512) && (dev->sector_size != 2048))
|
|
|
|
{
|
|
|
|
dev->sector_size = 2048;
|
|
|
|
}
|
2005-11-21 21:55:36 +00:00
|
|
|
return dev;
|
|
|
|
}
|