usb-redir: add files for SCSI and USB MSC implementation

Files added without including them in compilation.
They contain implementation of SCSI commands for logical
units of mass-storage device class and USB bulk-only
mass-storage device protocol.

Signed-off-by: Alexander Nezhinsky<anezhins@redhat.com>
Signed-off-by: Yuri Benditovich <yuri.benditovich@daynix.com>
Acked-by: Frediano Ziglio <fziglio@redhat.com>
This commit is contained in:
Yuri Benditovich 2019-09-19 16:11:22 +02:00 committed by Frediano Ziglio
parent 3e20f17b90
commit ef0bee2e56
6 changed files with 3898 additions and 0 deletions

46
src/cd-scsi-dev-params.h Normal file
View File

@ -0,0 +1,46 @@
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
CD SCSI device parameters
Copyright (C) 2018 Red Hat, Inc.
Red Hat Authors:
Alexander Nezhinsky<anezhins@redhat.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <gio/gio.h>
typedef struct CdScsiDeviceParameters {
const char *vendor;
const char *product;
const char *version;
const char *serial;
} CdScsiDeviceParameters;
typedef struct CdScsiDeviceInfo {
CdScsiDeviceParameters parameters;
uint32_t started : 1;
uint32_t locked : 1;
uint32_t loaded : 1;
} CdScsiDeviceInfo;
typedef struct CdScsiMediaParameters {
GFileInputStream *stream;
uint64_t size;
uint32_t block_size;
} CdScsiMediaParameters;

2740
src/cd-scsi.c Normal file

File diff suppressed because it is too large Load Diff

117
src/cd-scsi.h Normal file
View File

@ -0,0 +1,117 @@
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
CD device emulation - SCSI engine
Copyright (C) 2018 Red Hat, Inc.
Red Hat Authors:
Alexander Nezhinsky<anezhins@redhat.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "cd-scsi-dev-params.h"
#include "cd-usb-bulk-msd.h"
#include "scsi-constants.h"
#if defined(G_OS_WIN32)
#include <winsock2.h>
#include <windows.h>
/* Windows is always LE at the moment */
#define le32toh(x) (x)
#define htole32(x) (x)
#define htobe32(x) htonl(x)
#endif
typedef enum ScsiXferDir {
SCSI_XFER_NONE = 0, /* TEST_UNIT_READY, ... */
SCSI_XFER_FROM_DEV, /* READ, INQUIRY, MODE_SENSE, ... */
SCSI_XFER_TO_DEV, /* WRITE, MODE_SELECT, ... */
} ScsiXferDir;
#define SCSI_CDB_BUF_SIZE 16
typedef enum CdScsiReqState {
SCSI_REQ_IDLE = 0,
SCSI_REQ_RUNNING,
SCSI_REQ_COMPLETE,
SCSI_REQ_CANCELED,
SCSI_REQ_DISPOSED,
} CdScsiReqState;
typedef struct CdScsiRequest {
/* request */
uint8_t cdb[SCSI_CDB_BUF_SIZE];
uint32_t cdb_len;
uint32_t lun;
uint8_t *buf;
uint32_t buf_len;
/* internal */
CdScsiReqState req_state;
ScsiXferDir xfer_dir;
uint64_t cancel_id;
void *priv_data;
uint64_t lba; /* offset in logical blocks if relevant */
uint64_t count; /* count in logical blocks */
uint64_t offset; /* scsi cdb offset, normalized to bytes */
uint64_t req_len; /* scsi cdb request length, normalized to bytes */
/* result */
uint64_t in_len; /* length of data actually available after read */
uint32_t status; /* SCSI status code */
} CdScsiRequest;
CdScsiReqState cd_scsi_get_req_state(CdScsiRequest *req);
/* SCSI target/device API */
typedef struct CdScsiTarget CdScsiTarget;
/* to be used in callbacks */
CdScsiTarget *cd_scsi_target_alloc(void *target_user_data, uint32_t max_luns);
void cd_scsi_target_free(CdScsiTarget *scsi_target);
int cd_scsi_dev_realize(CdScsiTarget *scsi_target, uint32_t lun,
const CdScsiDeviceParameters *dev_params);
int cd_scsi_dev_unrealize(CdScsiTarget *scsi_target, uint32_t lun);
int cd_scsi_dev_lock(CdScsiTarget *scsi_target, uint32_t lun, gboolean lock);
int cd_scsi_dev_load(CdScsiTarget *scsi_target, uint32_t lun,
const CdScsiMediaParameters *media_params);
int cd_scsi_dev_get_info(CdScsiTarget *scsi_target, uint32_t lun, CdScsiDeviceInfo *lun_info);
int cd_scsi_dev_unload(CdScsiTarget *scsi_target, uint32_t lun);
void cd_scsi_dev_request_submit(CdScsiTarget *scsi_target, CdScsiRequest *request);
void cd_scsi_dev_request_cancel(CdScsiTarget *scsi_target, CdScsiRequest *request);
void cd_scsi_dev_request_release(CdScsiTarget *scsi_target, CdScsiRequest *request);
int cd_scsi_dev_reset(CdScsiTarget *scsi_target, uint32_t lun);
int cd_scsi_target_reset(CdScsiTarget *scsi_target);
/* Callbacks
* These callbacks are used by upper layer to implement specific SCSI
* target devices.
*/
void cd_scsi_dev_request_complete(void *target_user_data, CdScsiRequest *request);
void cd_scsi_dev_changed(void *target_user_data, uint32_t lun);
void cd_scsi_dev_reset_complete(void *target_user_data, uint32_t lun);
void cd_scsi_target_reset_complete(void *target_user_data);

543
src/cd-usb-bulk-msd.c Normal file
View File

@ -0,0 +1,543 @@
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
USB CD Device emulation - Data Bulk transfers - Mass Storage Device
Copyright (C) 2018 Red Hat, Inc.
Red Hat Authors:
Alexander Nezhinsky<anezhins@redhat.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "spice/types.h"
#include "spice-common.h"
#include "spice-util.h"
#include "cd-usb-bulk-msd.h"
#include "cd-scsi.h"
#ifdef USE_USBREDIR
#define SPICE_ERROR(fmt, ...) \
do { SPICE_DEBUG("usb-msd error: " fmt , ## __VA_ARGS__); } while (0)
typedef enum UsbCdState {
USB_CD_STATE_INIT, /* Not ready */
USB_CD_STATE_CBW, /* Waiting for Command Block */
USB_CD_STATE_DATAOUT, /* Transfer data to device */
USB_CD_STATE_DATAIN, /* Transfer data from device */
USB_CD_STATE_ZERO_DATAIN, /* Need to send zero bulk-in before status */
USB_CD_STATE_CSW, /* Send Command Status */
USB_CD_STATE_DEVICE_RESET, /* reset of a single device */
USB_CD_STATE_TARGET_RESET /* reset of entire target */
} UsbCdState;
/* USB MSD Command Block Wrapper */
struct __attribute__((__packed__)) UsbCdCBW {
uint32_t sig;
uint32_t tag;
uint32_t exp_data_len; /* expected data xfer length for the request */
uint8_t flags;
uint8_t lun;
uint8_t cmd_len; /* actual length of the scsi command that follows */
uint8_t cmd[16]; /* scsi command to perform */
};
/* USB MSD Command Status Wrapper */
struct __attribute__((__packed__)) UsbCdCSW {
uint32_t sig;
uint32_t tag;
uint32_t residue;
uint8_t status;
};
/* UsbCdCSW::status */
typedef enum UsbMsdStatus {
USB_MSD_STATUS_GOOD = 0,
USB_MSD_STATUS_FAILED = 1,
USB_MSD_STATUS_PHASE_ERR = 2,
} UsbMsdStatus;
typedef struct UsbCdBulkMsdRequest {
CdScsiRequest scsi_req;
uint32_t lun;
uint32_t usb_req_len; /* length of data requested by usb */
uint32_t scsi_in_len; /* length of data returned by scsi limited by usb request */
uint32_t xfer_len; /* length of data transfered until now */
uint32_t bulk_in_len; /* length of the last postponed bulk-in request */
struct UsbCdCSW csw; /* usb status header */
} UsbCdBulkMsdRequest;
typedef struct UsbCdBulkMsdDevice {
UsbCdState state;
CdScsiTarget *scsi_target; /* scsi handle */
void *usb_user_data; /* used in callbacks to usb */
UsbCdBulkMsdRequest usb_req; /* now supporting a single cmd */
uint8_t *data_buf;
uint32_t data_buf_len;
} UsbCdBulkMsdDevice;
static inline const char *usb_cd_state_str(UsbCdState state)
{
switch (state) {
case USB_CD_STATE_INIT:
return "INIT";
case USB_CD_STATE_CBW:
return "CBW";
case USB_CD_STATE_DATAOUT:
return "DATAOUT";
case USB_CD_STATE_DATAIN:
return "DATAIN";
case USB_CD_STATE_ZERO_DATAIN:
return "ZERO_DATAIN";
case USB_CD_STATE_CSW:
return "CSW";
case USB_CD_STATE_DEVICE_RESET:
return "DEV_RESET";
case USB_CD_STATE_TARGET_RESET:
return "TGT_RESET";
default:
return "ILLEGAL";
}
}
static void cd_usb_bulk_msd_set_state(UsbCdBulkMsdDevice *cd, UsbCdState state)
{
SPICE_DEBUG("State %s -> %s", usb_cd_state_str(cd->state), usb_cd_state_str(state));
cd->state = state;
}
UsbCdBulkMsdDevice *cd_usb_bulk_msd_alloc(void *usb_user_data, uint32_t max_luns)
{
UsbCdBulkMsdDevice *cd = g_new0(UsbCdBulkMsdDevice, 1);
cd->data_buf_len = 256 * 1024;
cd->data_buf = g_malloc(cd->data_buf_len);
cd->scsi_target = cd_scsi_target_alloc(cd, max_luns);
if (cd->scsi_target == NULL) {
g_free(cd->data_buf);
g_free(cd);
return NULL;
}
cd_usb_bulk_msd_set_state(cd, USB_CD_STATE_INIT);
cd->usb_user_data = usb_user_data;
SPICE_DEBUG("Alloc, max_luns:%u", max_luns);
return cd;
}
int cd_usb_bulk_msd_realize(UsbCdBulkMsdDevice *cd, uint32_t lun,
const CdScsiDeviceParameters *dev_params)
{
CdScsiDeviceParameters scsi_dev_params;
int rc;
scsi_dev_params.vendor = dev_params->vendor ? : "SPICE";
scsi_dev_params.product = dev_params->product ? : "USB-CD";
scsi_dev_params.version = dev_params->version ? : "0.1";
scsi_dev_params.serial = dev_params->serial ? : "123456";
rc = cd_scsi_dev_realize(cd->scsi_target, lun, &scsi_dev_params);
if (rc != 0) {
SPICE_ERROR("Failed to realize lun:%u", lun);
return rc;
}
if (cd->state == USB_CD_STATE_INIT) {
/* wait next request */
cd_usb_bulk_msd_set_state(cd, USB_CD_STATE_CBW);
cd_scsi_dev_request_release(cd->scsi_target, &cd->usb_req.scsi_req);
}
SPICE_DEBUG("Realize OK lun:%u", lun);
return 0;
}
int cd_usb_bulk_msd_lock(UsbCdBulkMsdDevice *cd, uint32_t lun, gboolean lock)
{
int rc;
rc = cd_scsi_dev_lock(cd->scsi_target, lun, lock);
if (rc != 0) {
SPICE_ERROR("Failed to lock lun:%u", lun);
return rc;
}
SPICE_DEBUG("Lock OK lun:%u", lun);
return 0;
}
int cd_usb_bulk_msd_load(UsbCdBulkMsdDevice *cd, uint32_t lun,
const CdScsiMediaParameters *media_params)
{
int rc;
rc = cd_scsi_dev_load(cd->scsi_target, lun, media_params);
if (rc != 0) {
SPICE_ERROR("Failed to load lun:%u", lun);
return rc;
}
SPICE_DEBUG("Load OK lun:%u", lun);
return 0;
}
int cd_usb_bulk_msd_get_info(UsbCdBulkMsdDevice *cd, uint32_t lun, CdScsiDeviceInfo *lun_info)
{
int rc;
rc = cd_scsi_dev_get_info(cd->scsi_target, lun, lun_info);
if (rc != 0) {
SPICE_ERROR("Failed to get info lun:%u", lun);
return rc;
}
return 0;
}
int cd_usb_bulk_msd_unload(UsbCdBulkMsdDevice *cd, uint32_t lun)
{
int rc;
rc = cd_scsi_dev_unload(cd->scsi_target, lun);
if (rc != 0) {
SPICE_ERROR("Failed to unload lun:%u", lun);
return rc;
}
SPICE_DEBUG("Unload OK lun:%u", lun);
return 0;
}
int cd_usb_bulk_msd_unrealize(UsbCdBulkMsdDevice *cd, uint32_t lun)
{
int rc;
rc = cd_scsi_dev_unrealize(cd->scsi_target, lun);
if (rc != 0) {
SPICE_ERROR("Unrealize lun:%u", lun);
return rc;
}
SPICE_DEBUG("Unrealize lun:%u", lun);
return 0;
}
void cd_usb_bulk_msd_free(UsbCdBulkMsdDevice *cd)
{
cd_scsi_target_free(cd->scsi_target);
g_free(cd->data_buf);
g_free(cd);
SPICE_DEBUG("Free");
}
int cd_usb_bulk_msd_reset(UsbCdBulkMsdDevice *cd)
{
cd_scsi_target_reset(cd->scsi_target);
cd_usb_bulk_msd_set_state(cd, USB_CD_STATE_CBW);
SPICE_DEBUG("Reset");
return 0;
}
static int parse_usb_msd_cmd(UsbCdBulkMsdDevice *cd, uint8_t *buf, uint32_t cbw_len)
{
struct UsbCdCBW *cbw = (struct UsbCdCBW *)buf;
UsbCdBulkMsdRequest *usb_req = &cd->usb_req;
CdScsiRequest *scsi_req = &usb_req->scsi_req;
if (cbw_len != sizeof(*cbw)) {
SPICE_ERROR("CMD: Bad CBW size:%u", cbw_len);
return -1;
}
if (le32toh(cbw->sig) != 0x43425355) { /* MSD command signature */
SPICE_ERROR("CMD: Bad CBW signature:%08x", le32toh(cbw->sig));
return -1;
}
const uint8_t cmd_len = cbw->cmd_len & 0x1F;
if (cmd_len < 1 || cmd_len > 16) {
SPICE_ERROR("CMD: Bad CBW command len:%08x", cmd_len);
return -1;
}
usb_req->lun = cbw->lun;
usb_req->usb_req_len = le32toh(cbw->exp_data_len);
usb_req->scsi_in_len = 0; /* no data from scsi yet */
usb_req->xfer_len = 0; /* no bulks transfered yet */
usb_req->bulk_in_len = 0; /* no bulk-in requests yet */
if (usb_req->usb_req_len == 0) {
cd_usb_bulk_msd_set_state(cd, USB_CD_STATE_CSW); /* no data - return status */
scsi_req->buf = NULL;
scsi_req->buf_len = 0;
} else if (cbw->flags & 0x80) {
cd_usb_bulk_msd_set_state(cd, USB_CD_STATE_DATAIN); /* read command */
scsi_req->buf = cd->data_buf;
scsi_req->buf_len = cd->data_buf_len;
} else {
cd_usb_bulk_msd_set_state(cd, USB_CD_STATE_DATAOUT); /* write command */
scsi_req->buf = NULL;
scsi_req->buf_len = 0;
}
scsi_req->cdb_len = cmd_len;
g_assert(scsi_req->cdb_len <= sizeof(scsi_req->cdb));
memcpy(scsi_req->cdb, cbw->cmd, scsi_req->cdb_len);
scsi_req->lun = usb_req->lun;
SPICE_DEBUG("CMD lun:%u tag:%#x flags:%08x "
"cdb_len:%u req_len:%u",
usb_req->lun, le32toh(cbw->tag), cbw->flags,
scsi_req->cdb_len, usb_req->usb_req_len);
/* prepare status - CSW */
usb_req->csw.sig = htole32(0x53425355);
usb_req->csw.tag = cbw->tag;
usb_req->csw.residue = 0;
usb_req->csw.status = (uint8_t)USB_MSD_STATUS_GOOD;
return 0;
}
static void usb_cd_cmd_done(UsbCdBulkMsdDevice *cd)
{
UsbCdBulkMsdRequest *usb_req = &cd->usb_req;
CdScsiRequest *scsi_req = &usb_req->scsi_req;
cd_usb_bulk_msd_set_state(cd, USB_CD_STATE_CBW); /* Command next */
cd_scsi_dev_request_release(cd->scsi_target, scsi_req);
}
static void usb_cd_send_status(UsbCdBulkMsdDevice *cd)
{
UsbCdBulkMsdRequest *usb_req = &cd->usb_req;
SPICE_DEBUG("Command CSW tag:0x%x msd_status:%d len:%" G_GUINT64_FORMAT,
le32toh(usb_req->csw.tag), (int)usb_req->csw.status, sizeof(usb_req->csw));
usb_cd_cmd_done(cd);
g_assert(usb_req->csw.sig == htole32(0x53425355));
cd_usb_bulk_msd_read_complete(cd->usb_user_data,
(uint8_t *)&usb_req->csw, sizeof(usb_req->csw),
BULK_STATUS_GOOD);
}
static void usb_cd_send_canceled(UsbCdBulkMsdDevice *cd)
{
UsbCdBulkMsdRequest *usb_req = &cd->usb_req;
SPICE_DEBUG("Canceled cmd tag:0x%x, len:%" G_GUINT64_FORMAT,
le32toh(usb_req->csw.tag), sizeof(usb_req->csw));
usb_cd_cmd_done(cd);
cd_usb_bulk_msd_read_complete(cd->usb_user_data,
NULL, 0,
BULK_STATUS_CANCELED);
}
static void usb_cd_send_data_in(UsbCdBulkMsdDevice *cd, uint32_t max_len)
{
UsbCdBulkMsdRequest *usb_req = &cd->usb_req;
CdScsiRequest *scsi_req = &usb_req->scsi_req;
uint8_t *buf = ((uint8_t *)scsi_req->buf) + usb_req->xfer_len;
uint32_t avail_len = usb_req->scsi_in_len - usb_req->xfer_len;
uint32_t send_len = MIN(avail_len, max_len);
SPICE_DEBUG("Data-in cmd tag 0x%x, remains %u"
", requested %u, send %u",
usb_req->csw.tag, avail_len, max_len, send_len);
g_assert(max_len <= usb_req->usb_req_len);
cd_usb_bulk_msd_read_complete(cd->usb_user_data,
buf, send_len,
BULK_STATUS_GOOD);
if (scsi_req->status == GOOD) {
usb_req->xfer_len += send_len;
if (usb_req->xfer_len == usb_req->scsi_in_len) {
/* all data for this bulk has been transferred */
if (usb_req->scsi_in_len == usb_req->usb_req_len || /* req fully satisfied */
send_len < max_len) { /* partial bulk - no more data */
cd_usb_bulk_msd_set_state(cd, USB_CD_STATE_CSW);
} else {
/* partial cmd data fullfilled entire vulk-in request */
cd_usb_bulk_msd_set_state(cd, USB_CD_STATE_ZERO_DATAIN);
}
}
} else { /* cmd failed - no more data */
cd_usb_bulk_msd_set_state(cd, USB_CD_STATE_CSW);
}
}
int cd_usb_bulk_msd_read(UsbCdBulkMsdDevice *cd, uint32_t max_len)
{
UsbCdBulkMsdRequest *usb_req = &cd->usb_req;
CdScsiRequest *scsi_req = &usb_req->scsi_req;
SPICE_DEBUG("msd_read, state: %s, len %u",
usb_cd_state_str(cd->state), max_len);
switch (cd->state) {
case USB_CD_STATE_CSW: /* Command Status */
if (max_len < 13) {
goto fail;
}
if (cd_scsi_get_req_state(scsi_req) == SCSI_REQ_COMPLETE) {
usb_cd_send_status(cd);
} else {
usb_req->bulk_in_len += max_len;
SPICE_DEBUG("msd_read CSW, req incomplete, added len %u"
" saved len %u",
max_len, usb_req->bulk_in_len);
}
break;
case USB_CD_STATE_DATAIN: /* Bulk Data-IN */
if (cd_scsi_get_req_state(scsi_req) == SCSI_REQ_COMPLETE) {
usb_cd_send_data_in(cd, max_len);
} else {
usb_req->bulk_in_len += max_len;
SPICE_DEBUG("msd_read DATAIN, req incomplete, added len %u saved len %u",
max_len, usb_req->bulk_in_len);
}
break;
case USB_CD_STATE_ZERO_DATAIN:
cd_usb_bulk_msd_read_complete(cd->usb_user_data,
NULL, 0,
BULK_STATUS_GOOD);
cd_usb_bulk_msd_set_state(cd, USB_CD_STATE_CSW); /* Status next */
break;
default:
SPICE_ERROR("Unexpected read state: %s, len %u",
usb_cd_state_str(cd->state), max_len);
goto fail;
}
return 0;
fail:
return -1;
}
void cd_scsi_dev_request_complete(void *target_user_data, CdScsiRequest *scsi_req)
{
UsbCdBulkMsdDevice *cd = (UsbCdBulkMsdDevice *)target_user_data;
UsbCdBulkMsdRequest *usb_req = &cd->usb_req;
g_assert(scsi_req == &usb_req->scsi_req);
if (scsi_req->req_state == SCSI_REQ_COMPLETE) {
usb_req->scsi_in_len = (scsi_req->in_len <= usb_req->usb_req_len) ?
scsi_req->in_len : usb_req->usb_req_len;
/* prepare CSW */
if (usb_req->usb_req_len > usb_req->scsi_in_len) {
usb_req->csw.residue = htole32(usb_req->usb_req_len - usb_req->scsi_in_len);
}
if (scsi_req->status != GOOD) {
usb_req->csw.status = (uint8_t)USB_MSD_STATUS_FAILED;
}
if (usb_req->bulk_in_len) {
/* bulk-in request arrived while scsi was still running */
if (cd->state == USB_CD_STATE_DATAIN) {
usb_cd_send_data_in(cd, usb_req->bulk_in_len);
} else if (cd->state == USB_CD_STATE_CSW) {
usb_cd_send_status(cd);
}
usb_req->bulk_in_len = 0;
}
} else if (scsi_req->req_state == SCSI_REQ_CANCELED) {
usb_cd_send_canceled(cd);
} else {
g_assert(scsi_req->req_state == SCSI_REQ_DISPOSED);
SPICE_DEBUG("Disposed cmd tag:0x%x, len:%" G_GUINT64_FORMAT,
le32toh(usb_req->csw.tag), sizeof(usb_req->csw));
usb_cd_cmd_done(cd);
}
}
int cd_usb_bulk_msd_cancel_read(UsbCdBulkMsdDevice *cd)
{
UsbCdBulkMsdRequest *usb_req = &cd->usb_req;
CdScsiRequest *scsi_req = &usb_req->scsi_req;
cd_scsi_dev_request_cancel(cd->scsi_target, scsi_req);
return 0;
}
int cd_usb_bulk_msd_write(UsbCdBulkMsdDevice *cd, uint8_t *buf_out, uint32_t buf_out_len)
{
switch (cd->state) {
case USB_CD_STATE_CBW: /* Command Block */
parse_usb_msd_cmd(cd, buf_out, buf_out_len);
if (cd->state == USB_CD_STATE_DATAIN || cd->state == USB_CD_STATE_CSW) {
cd_scsi_dev_request_submit(cd->scsi_target, &cd->usb_req.scsi_req);
}
break;
case USB_CD_STATE_DATAOUT: /* Data-Out for a Write cmd */
cd->usb_req.scsi_req.buf = buf_out;
cd->usb_req.scsi_req.buf_len = buf_out_len;
cd_scsi_dev_request_submit(cd->scsi_target, &cd->usb_req.scsi_req);
cd_usb_bulk_msd_set_state(cd, USB_CD_STATE_CSW); /* Status next */
break;
default:
SPICE_DEBUG("Unexpected write state: %s, len %u",
usb_cd_state_str(cd->state), buf_out_len);
goto fail;
}
return 0;
fail:
return -1;
}
void cd_scsi_dev_reset_complete(void *target_user_data, uint32_t lun)
{
UsbCdBulkMsdDevice *cd = (UsbCdBulkMsdDevice *)target_user_data;
if (cd->state == USB_CD_STATE_DEVICE_RESET) {
cd_usb_bulk_msd_set_state(cd, USB_CD_STATE_CBW);
cd_usb_bulk_msd_reset_complete(cd->usb_user_data, 0);
}
}
void cd_scsi_target_reset_complete(void *target_user_data)
{
UsbCdBulkMsdDevice *cd = (UsbCdBulkMsdDevice *)target_user_data;
cd_usb_bulk_msd_set_state(cd, USB_CD_STATE_INIT);
}
void cd_scsi_dev_changed(void *target_user_data, uint32_t lun)
{
UsbCdBulkMsdDevice *cd = (UsbCdBulkMsdDevice *)target_user_data;
SPICE_DEBUG("Device changed, state: %s lun: %u",
usb_cd_state_str(cd->state), lun);
cd_usb_bulk_msd_lun_changed(cd->usb_user_data, lun);
}
#endif /* USE_USBREDIR */

131
src/cd-usb-bulk-msd.h Normal file
View File

@ -0,0 +1,131 @@
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
Copyright (C) 2018 Red Hat, Inc.
Red Hat Authors:
Alexander Nezhinsky<anezhins@redhat.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <gio/gio.h>
#include "cd-scsi-dev-params.h"
G_BEGIN_DECLS
typedef enum CdUsbBulkStatus {
BULK_STATUS_GOOD = 0,
BULK_STATUS_ERROR,
BULK_STATUS_CANCELED,
BULK_STATUS_STALL,
} CdUsbBulkStatus;
typedef struct UsbCdBulkMsdDevice UsbCdBulkMsdDevice;
/* USB backend callbacks */
/* called on completed read data bulk transfer
* user_data - user_data in unit parameters structure
* status - bulk status code
*/
void cd_usb_bulk_msd_read_complete(void *user_data,
uint8_t *data, uint32_t length,
CdUsbBulkStatus status);
/* called when state of device's unit changed to signal GUI component
* user_data - user_data in unit parameters structure
*/
void cd_usb_bulk_msd_lun_changed(void *user_data, uint32_t lun);
/* called on completed device reset
* user_data - user_data in unit parameters structure
* status - error code
*/
void cd_usb_bulk_msd_reset_complete(void *user_data,
int status);
/* MSD backend api */
/* allocate new device descriptor */
UsbCdBulkMsdDevice *cd_usb_bulk_msd_alloc(void *user_data, uint32_t max_lun);
/* free device descriptor */
void cd_usb_bulk_msd_free(UsbCdBulkMsdDevice *device);
/* configure a new Logical Unit to be represen ted by the device
* returns: error code
*/
int cd_usb_bulk_msd_realize(UsbCdBulkMsdDevice *device, uint32_t lun,
const CdScsiDeviceParameters *dev_params);
/* lock the device, prevent unloading
* returns: error code
*/
int cd_usb_bulk_msd_lock(UsbCdBulkMsdDevice *device, uint32_t lun, gboolean lock);
/* load new media, if already loaded, simulate media change
* returns: error code
*/
int cd_usb_bulk_msd_load(UsbCdBulkMsdDevice *device, uint32_t lun,
const CdScsiMediaParameters *media_params);
/* query unit parameters and status
* returns: error code
*/
int cd_usb_bulk_msd_get_info(UsbCdBulkMsdDevice *device, uint32_t lun,
CdScsiDeviceInfo *lun_info);
/* unload the media
* returns: error code
*/
int cd_usb_bulk_msd_unload(UsbCdBulkMsdDevice *device, uint32_t lun);
/* detach a Logical Unit
* returns: error code
*/
int cd_usb_bulk_msd_unrealize(UsbCdBulkMsdDevice *device, uint32_t lun);
/* reset the device instance; cancel all IO ops, reset state
* returns: error code
*/
int cd_usb_bulk_msd_reset(UsbCdBulkMsdDevice *device);
/* perform a write data bulk transfer
* data_len - length of available data to write
* returns: error code
*/
int cd_usb_bulk_msd_write(UsbCdBulkMsdDevice*device, uint8_t *buf, uint32_t data_len);
/* perform a read data bulk transfer
* max_len - length of available buffer to fill
* If data available immediately, should call cd_usb_bulk_msd_read_complete()
* and return success
* If fatal error detected immediately, should call cd_usb_bulk_msd_read_complete()
* with error code and return success
*
* returns: 0 - success, -1 - error
*/
int cd_usb_bulk_msd_read(UsbCdBulkMsdDevice *device, uint32_t max_len);
/* cancels pending read data bulk transfer
* returns: error code
*/
int cd_usb_bulk_msd_cancel_read(UsbCdBulkMsdDevice *device);
G_END_DECLS

321
src/scsi-constants.h Normal file
View File

@ -0,0 +1,321 @@
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
Copyright (C) 2018 Red Hat, Inc.
Based on the GLib header
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
/*
* SCSI opcodes
*/
#define TEST_UNIT_READY 0x00
#define REWIND 0x01
#define REQUEST_SENSE 0x03
#define FORMAT_UNIT 0x04
#define READ_BLOCK_LIMITS 0x05
#define INITIALIZE_ELEMENT_STATUS 0x07
#define REASSIGN_BLOCKS_ 0x07
#define READ_6 0x08
#define WRITE_6 0x0a
#define SET_CAPACITY 0x0b
#define READ_REVERSE 0x0f
#define WRITE_FILEMARKS 0x10
#define SPACE 0x11
#define INQUIRY 0x12
#define RECOVER_BUFFERED_DATA 0x14
#define MODE_SELECT 0x15
#define RESERVE 0x16
#define RELEASE 0x17
#define COPY 0x18
#define ERASE 0x19
#define MODE_SENSE 0x1a
#define LOAD_UNLOAD 0x1b
#define SCAN 0x1b
#define START_STOP 0x1b
#define RECEIVE_DIAGNOSTIC 0x1c
#define SEND_DIAGNOSTIC 0x1d
#define ALLOW_MEDIUM_REMOVAL 0x1e
#define SET_WINDOW 0x24
#define READ_CAPACITY_10 0x25
#define GET_WINDOW 0x25
#define READ_10 0x28
#define WRITE_10 0x2a
#define SEND 0x2a
#define SEEK_10 0x2b
#define LOCATE_10 0x2b
#define POSITION_TO_ELEMENT 0x2b
#define WRITE_VERIFY_10 0x2e
#define VERIFY_10 0x2f
#define SEARCH_HIGH 0x30
#define SEARCH_EQUAL 0x31
#define OBJECT_POSITION 0x31
#define SEARCH_LOW 0x32
#define SET_LIMITS 0x33
#define PRE_FETCH 0x34
#define READ_POSITION 0x34
#define GET_DATA_BUFFER_STATUS 0x34
#define SYNCHRONIZE_CACHE 0x35
#define LOCK_UNLOCK_CACHE 0x36
#define INITIALIZE_ELEMENT_STATUS_WITH_RANGE 0x37
#define READ_DEFECT_DATA 0x37
#define MEDIUM_SCAN 0x38
#define COMPARE 0x39
#define COPY_VERIFY 0x3a
#define WRITE_BUFFER 0x3b
#define READ_BUFFER 0x3c
#define UPDATE_BLOCK 0x3d
#define READ_LONG_10 0x3e
#define WRITE_LONG_10 0x3f
#define CHANGE_DEFINITION 0x40
#define WRITE_SAME_10 0x41
#define UNMAP 0x42
#define READ_TOC 0x43
#define REPORT_DENSITY_SUPPORT 0x44
#define GET_CONFIGURATION 0x46
#define SANITIZE 0x48
#define GET_EVENT_STATUS_NOTIFICATION 0x4a
#define LOG_SELECT 0x4c
#define LOG_SENSE 0x4d
#define READ_DISC_INFORMATION 0x51
#define READ_TRACK_INFORMATION 0x52
#define RESERVE_TRACK 0x53
#define MODE_SELECT_10 0x55
#define RESERVE_10 0x56
#define RELEASE_10 0x57
#define MODE_SENSE_10 0x5a
#define SEND_CUE_SHEET 0x5d
#define PERSISTENT_RESERVE_IN 0x5e
#define PERSISTENT_RESERVE_OUT 0x5f
#define VARLENGTH_CDB 0x7f
#define WRITE_FILEMARKS_16 0x80
#define READ_REVERSE_16 0x81
#define ALLOW_OVERWRITE 0x82
#define EXTENDED_COPY 0x83
#define ATA_PASSTHROUGH_16 0x85
#define ACCESS_CONTROL_IN 0x86
#define ACCESS_CONTROL_OUT 0x87
#define READ_16 0x88
#define COMPARE_AND_WRITE 0x89
#define WRITE_16 0x8a
#define WRITE_VERIFY_16 0x8e
#define VERIFY_16 0x8f
#define PRE_FETCH_16 0x90
#define SPACE_16 0x91
#define SYNCHRONIZE_CACHE_16 0x91
#define LOCATE_16 0x92
#define WRITE_SAME_16 0x93
#define ERASE_16 0x93
#define SERVICE_ACTION_IN_16 0x9e
#define WRITE_LONG_16 0x9f
#define REPORT_LUNS 0xa0
#define ATA_PASSTHROUGH_12 0xa1
#define MAINTENANCE_IN 0xa3
#define MAINTENANCE_OUT 0xa4
#define MOVE_MEDIUM 0xa5
#define EXCHANGE_MEDIUM 0xa6
#define SET_READ_AHEAD 0xa7
#define READ_12 0xa8
#define WRITE_12 0xaa
#define SERVICE_ACTION_IN_12 0xab
#define ERASE_12 0xac
#define WRITE_VERIFY_12 0xae
#define VERIFY_12 0xaf
#define SEARCH_HIGH_12 0xb0
#define SEARCH_EQUAL_12 0xb1
#define SEARCH_LOW_12 0xb2
#define READ_ELEMENT_STATUS 0xb8
#define SEND_VOLUME_TAG 0xb6
/* MMC-specific opcode assignment */
#define MMC_SEND_EVENT 0xa2
#define MMC_SEND_KEY 0xa3
#define MMC_REPORT_KEY 0xa4
#define MMC_GET_PERFORMANCE 0xac
#define MMC_READ_DVD_STRUCTURE 0xad
#define MMC_READ_DEFECT_DATA_12 0xb7
#define MMC_SET_CD_SPEED 0xbb
#define MMC_MECHANISM_STATUS 0xbd
#define MMC_READ_CD 0xbe
#define MMC_SEND_DVD_STRUCTURE 0xbf
/*
* SERVICE ACTION IN subcodes
*/
#define SAI_READ_CAPACITY_16 0x10
/*
* READ POSITION service action codes
*/
#define SHORT_FORM_BLOCK_ID 0x00
#define SHORT_FORM_VENDOR_SPECIFIC 0x01
#define LONG_FORM 0x06
#define EXTENDED_FORM 0x08
/*
* SAM Status codes
*/
#define GOOD 0x00
#define CHECK_CONDITION 0x02
#define CONDITION_GOOD 0x04
#define BUSY 0x08
#define INTERMEDIATE_GOOD 0x10
#define INTERMEDIATE_C_GOOD 0x14
#define RESERVATION_CONFLICT 0x18
#define COMMAND_TERMINATED 0x22
#define TASK_SET_FULL 0x28
#define ACA_ACTIVE 0x30
#define TASK_ABORTED 0x40
#define STATUS_MASK 0x3e
/*
* SENSE KEYS
*/
#define NO_SENSE 0x00
#define RECOVERED_ERROR 0x01
#define NOT_READY 0x02
#define MEDIUM_ERROR 0x03
#define HARDWARE_ERROR 0x04
#define ILLEGAL_REQUEST 0x05
#define UNIT_ATTENTION 0x06
#define DATA_PROTECT 0x07
#define BLANK_CHECK 0x08
#define COPY_ABORTED 0x0a
#define ABORTED_COMMAND 0x0b
#define VOLUME_OVERFLOW 0x0d
#define MISCOMPARE 0x0e
/*
* DEVICE TYPES
*/
#define TYPE_DISK 0x00
#define TYPE_TAPE 0x01
#define TYPE_PRINTER 0x02
#define TYPE_PROCESSOR 0x03 /* HP scanners use this */
#define TYPE_WORM 0x04 /* Treated as ROM by our system */
#define TYPE_ROM 0x05
#define TYPE_SCANNER 0x06
#define TYPE_MOD 0x07 /* Magneto-optical disk -
* - treated as TYPE_DISK */
#define TYPE_MEDIUM_CHANGER 0x08
#define TYPE_STORAGE_ARRAY 0x0c /* Storage array device */
#define TYPE_ENCLOSURE 0x0d /* Enclosure Services Device */
#define TYPE_RBC 0x0e /* Simplified Direct-Access Device */
#define TYPE_OSD 0x11 /* Object-storage Device */
#define TYPE_WLUN 0x1e /* Well known LUN */
#define TYPE_NOT_PRESENT 0x1f
#define TYPE_INACTIVE 0x20
#define TYPE_NO_LUN 0x7f
/* Mode page codes for mode sense/set */
#define MODE_PAGE_R_W_ERROR 0x01
#define MODE_PAGE_MRW 0x03
#define MODE_PAGE_HD_GEOMETRY 0x04
#define MODE_PAGE_FLEXIBLE_DISK_GEOMETRY 0x05 /* SBC */
#define MODE_PAGE_WRITE_PARAMETER 0x05 /* MMC */
#define MODE_PAGE_CACHING 0x08
#define MODE_PAGE_CD_DEVICE 0x0d
#define MODE_PAGE_AUDIO_CTL 0x0e
#define MODE_PAGE_POWER 0x1a
#define MODE_PAGE_FAULT_FAIL 0x1c
#define MODE_PAGE_TO_PROTECT 0x1d
#define MODE_PAGE_CAPS_MECH_STATUS 0x2a
#define MODE_PAGE_MRW_VENDOR 0x2C
#define MODE_PAGE_ALLS 0x3f
/* Not in Mt. Fuji, but in ATAPI 2.6 -- deprecated now in favor
* of MODE_PAGE_SENSE_POWER */
#define MODE_PAGE_CDROM 0x0d
#define MODE_PAGE_CDROM 0x0d
/* Event notification classes for GET EVENT STATUS NOTIFICATION */
#define GESN_NO_EVENTS 0
#define GESN_OPERATIONAL_CHANGE 1
#define GESN_POWER_MANAGEMENT 2
#define GESN_EXTERNAL_REQUEST 3
#define GESN_MEDIA 4
#define GESN_MULTIPLE_HOSTS 5
#define GESN_DEVICE_BUSY 6
/* Event codes for MEDIA event status notification */
#define MEC_NO_CHANGE 0
#define MEC_EJECT_REQUESTED 1
#define MEC_NEW_MEDIA 2
#define MEC_MEDIA_REMOVAL 3 /* only for media changers */
#define MEC_MEDIA_CHANGED 4 /* only for media changers */
#define MEC_BG_FORMAT_COMPLETED 5 /* MRW or DVD+RW b/g format completed */
#define MEC_BG_FORMAT_RESTARTED 6 /* MRW or DVD+RW b/g format restarted */
#define MS_TRAY_OPEN 1
#define MS_MEDIA_PRESENT 2
/*
* Based on values from <linux/cdrom.h> but extending CD_MINS
* to the maximum common size allowed by the Orange's Book ATIP
*
* 90 and 99 min CDs are also available but using them as the
* upper limit reduces the effectiveness of the heuristic to
* detect DVDs burned to less than 25% of their maximum capacity
*/
/* Some generally useful CD-ROM information */
#define CD_MINS 80 /* max. minutes per CD */
#define CD_SECS 60 /* seconds per minute */
#define CD_FRAMES 75 /* frames per second */
#define CD_FRAMESIZE 2048 /* bytes per frame, "cooked" mode */
#define CD_MAX_BYTES (CD_MINS * CD_SECS * CD_FRAMES * CD_FRAMESIZE)
#define CD_MAX_SECTORS (CD_MAX_BYTES / 512)
/*
* The MMC values are not IDE specific and might need to be moved
* to a common header if they are also needed for the SCSI emulation
*/
/* Profile list from MMC-6 revision 1 table 91 */
#define MMC_PROFILE_NONE 0x0000
#define MMC_PROFILE_CD_ROM 0x0008
#define MMC_PROFILE_CD_R 0x0009
#define MMC_PROFILE_CD_RW 0x000A
#define MMC_PROFILE_DVD_ROM 0x0010
#define MMC_PROFILE_DVD_R_SR 0x0011
#define MMC_PROFILE_DVD_RAM 0x0012
#define MMC_PROFILE_DVD_RW_RO 0x0013
#define MMC_PROFILE_DVD_RW_SR 0x0014
#define MMC_PROFILE_DVD_R_DL_SR 0x0015
#define MMC_PROFILE_DVD_R_DL_JR 0x0016
#define MMC_PROFILE_DVD_RW_DL 0x0017
#define MMC_PROFILE_DVD_DDR 0x0018
#define MMC_PROFILE_DVD_PLUS_RW 0x001A
#define MMC_PROFILE_DVD_PLUS_R 0x001B
#define MMC_PROFILE_DVD_PLUS_RW_DL 0x002A
#define MMC_PROFILE_DVD_PLUS_R_DL 0x002B
#define MMC_PROFILE_BD_ROM 0x0040
#define MMC_PROFILE_BD_R_SRM 0x0041
#define MMC_PROFILE_BD_R_RRM 0x0042
#define MMC_PROFILE_BD_RE 0x0043
#define MMC_PROFILE_HDDVD_ROM 0x0050
#define MMC_PROFILE_HDDVD_R 0x0051
#define MMC_PROFILE_HDDVD_RAM 0x0052
#define MMC_PROFILE_HDDVD_RW 0x0053
#define MMC_PROFILE_HDDVD_R_DL 0x0058
#define MMC_PROFILE_HDDVD_RW_DL 0x005A
#define MMC_PROFILE_INVALID 0xFFFF