Files
aaruremote/linux/scsi.c

80 lines
2.6 KiB
C
Raw Normal View History

2019-10-16 21:53:06 +01:00
/*
2020-03-01 05:44:49 +00:00
* This file is part of the Aaru Remote Server.
2020-01-03 17:42:13 +00:00
* Copyright (c) 2019-2020 Natalia Portillo.
2019-10-16 21:53:06 +01:00
*
* 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, version 3.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "../dicmote.h"
2019-10-26 16:11:19 +01:00
#include "linux.h"
2019-10-16 21:53:06 +01:00
#include <malloc.h>
#include <scsi/sg.h>
#include <stdint.h>
#include <string.h>
#include <sys/ioctl.h>
2019-10-26 16:11:19 +01:00
int32_t LinuxSendScsiCommand(void* device_ctx,
2019-10-19 18:30:45 +01:00
char* cdb,
char* buffer,
char** sense_buffer,
uint32_t timeout,
int32_t direction,
uint32_t* duration,
uint32_t* sense,
uint32_t cdb_len,
uint32_t* buf_len,
uint32_t* sense_len)
2019-10-16 21:53:06 +01:00
{
2019-10-26 16:11:19 +01:00
LinuxDeviceContext* ctx = device_ctx;
sg_io_hdr_t hdr;
int dir, ret;
2019-10-16 21:53:06 +01:00
*sense_len = 32;
2019-10-26 16:11:19 +01:00
if(!ctx) return -1;
2019-10-16 21:53:06 +01:00
memset(&hdr, 0, sizeof(sg_io_hdr_t));
2019-10-19 18:30:45 +01:00
*sense_buffer = malloc(*sense_len);
2019-10-16 21:53:06 +01:00
2019-10-19 18:30:45 +01:00
if(!*sense_buffer) return -1;
2019-10-16 21:53:06 +01:00
switch(direction)
{
case DICMOTE_SCSI_DIRECTION_IN: dir = SG_DXFER_FROM_DEV; break;
case DICMOTE_SCSI_DIRECTION_OUT: dir = SG_DXFER_TO_DEV; break;
2019-10-19 03:11:13 +01:00
case DICMOTE_SCSI_DIRECTION_INOUT:
case DICMOTE_SCSI_DIRECTION_UNSPECIFIED: dir = SG_DXFER_TO_FROM_DEV; break;
2019-10-16 21:53:06 +01:00
case DICMOTE_SCSI_DIRECTION_NONE:
default: dir = SG_DXFER_NONE; break;
}
hdr.interface_id = 'S';
hdr.cmd_len = (char)cdb_len;
hdr.mx_sb_len = 32;
hdr.dxfer_direction = dir;
hdr.dxfer_len = *buf_len;
hdr.dxferp = buffer;
hdr.cmdp = (unsigned char*)cdb;
2019-10-19 18:30:45 +01:00
hdr.sbp = (unsigned char*)*sense_buffer;
2019-10-16 21:53:06 +01:00
hdr.timeout = timeout;
hdr.flags = SG_FLAG_DIRECT_IO;
2019-10-26 16:11:19 +01:00
ret = ioctl(ctx->fd, SG_IO, &hdr);
2019-10-16 21:53:06 +01:00
2019-10-17 22:01:47 +01:00
*sense = (hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK;
2019-10-16 21:53:06 +01:00
// TODO: Manual timing if duration is 0
*duration = hdr.duration;
*sense_len = hdr.sb_len_wr;
return ret; // TODO: Implement
}