mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-09-24 13:30:33 +08:00
[DM/FEATURE] Support SCSI bus (#9592)
* [DM/FEATURE] Support block for SCSI 1. Support SD and CD-ROM. 2. SD will port to UFS and ATA device. Signed-off-by: GuEe-GUI <2991707448@qq.com>
This commit is contained in:
@@ -162,6 +162,13 @@ endif
|
||||
bool "Using devfs for device objects"
|
||||
default y
|
||||
|
||||
if RT_USING_DFS_V1
|
||||
config RT_USING_DFS_ISO9660
|
||||
bool "Using ISO9660 filesystem"
|
||||
depends on RT_USING_MEMHEAP
|
||||
default n
|
||||
endif
|
||||
|
||||
config RT_USING_DFS_ROMFS
|
||||
bool "Enable ReadOnly file system on flash"
|
||||
default n
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
# RT-Thread building script for component
|
||||
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.c')
|
||||
CPPPATH = [cwd]
|
||||
|
||||
group = DefineGroup('Filesystem', src, depend = ['RT_USING_DFS', 'RT_USING_DFS_ISO9660'], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-02-25 GuEe-GUI the first version
|
||||
*/
|
||||
|
||||
#ifndef __DFS_ISO9660_H__
|
||||
#define __DFS_ISO9660_H__
|
||||
|
||||
int dfs_iso9660_init(void);
|
||||
|
||||
#endif /* __DFS_ISO9660_H__ */
|
||||
@@ -322,8 +322,8 @@ int dfs_mount(const char *device_name,
|
||||
/* open device, but do not check the status of device */
|
||||
if (dev_id != NULL)
|
||||
{
|
||||
if (rt_device_open(fs->dev_id,
|
||||
RT_DEVICE_OFLAG_RDWR) != RT_EOK)
|
||||
if (rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR) != RT_EOK &&
|
||||
rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDONLY) != RT_EOK)
|
||||
{
|
||||
/* The underlying device has error, clear the entry. */
|
||||
dfs_lock();
|
||||
|
||||
@@ -22,6 +22,7 @@ rsource "graphic/Kconfig"
|
||||
rsource "hwcrypto/Kconfig"
|
||||
rsource "wlan/Kconfig"
|
||||
rsource "block/Kconfig"
|
||||
rsource "scsi/Kconfig"
|
||||
rsource "virtio/Kconfig"
|
||||
rsource "dma/Kconfig"
|
||||
rsource "mfd/Kconfig"
|
||||
|
||||
@@ -0,0 +1,461 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-02-25 GuEe-GUI the first version
|
||||
*/
|
||||
|
||||
#ifndef __SCSI_H__
|
||||
#define __SCSI_H__
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
#include <drivers/byteorder.h>
|
||||
|
||||
#define RT_SCSI_LUN_SHIFT 5
|
||||
|
||||
rt_packed(struct rt_scsi_unknow
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_test_unit_ready
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t reserved[4];
|
||||
rt_uint8_t control;
|
||||
rt_uint8_t pad[6]; /* To be ATAPI compatible */
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_inquiry
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-2 Reserved, 1 Obsolete Formerly CMDDT, 0 EVPD */
|
||||
rt_uint8_t page; /* Page code if EVPD=1 */
|
||||
rt_uint8_t reserved;
|
||||
rt_uint8_t alloc_length;
|
||||
rt_uint8_t control;
|
||||
rt_uint8_t pad[6]; /* To be ATAPI compatible */
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_inquiry_data
|
||||
{
|
||||
#define RT_SCSI_DEVTYPE_MASK 31
|
||||
rt_uint8_t devtype;
|
||||
#define RT_SCSI_REMOVABLE_BIT 7
|
||||
rt_uint8_t rmb;
|
||||
rt_uint8_t reserved[2];
|
||||
rt_uint8_t length;
|
||||
rt_uint8_t reserved1[3];
|
||||
char vendor[8];
|
||||
char prodid[16];
|
||||
char prodrev[4];
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_request_sense
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-2 Reserved, 1 Obsolete, 0 SP */
|
||||
rt_uint8_t reserved[2];
|
||||
rt_uint8_t alloc_length;
|
||||
rt_uint8_t control;
|
||||
rt_uint8_t pad[6]; /* To be ATAPI compatible */
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_request_sense_data
|
||||
{
|
||||
rt_uint8_t error_code; /* 7 Valid, 6-0 Err. code */
|
||||
rt_uint8_t segment_number;
|
||||
rt_uint8_t sense_key; /* 7 FileMark, 6 EndOfMedia, 5 ILI, 4-0 sense key */
|
||||
rt_be32_t information;
|
||||
rt_uint8_t additional_sense_length;
|
||||
rt_be32_t cmd_specific_info;
|
||||
rt_uint8_t additional_sense_code;
|
||||
rt_uint8_t additional_sense_code_qualifier;
|
||||
rt_uint8_t field_replaceable_unit_code;
|
||||
rt_uint8_t sense_key_specific[3];
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_read_capacity10
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-1 Reserved, 0 Obsolete */
|
||||
rt_be32_t logical_block_addr; /* only if PMI=1 */
|
||||
rt_uint8_t reserved[2];
|
||||
rt_uint8_t pmi;
|
||||
rt_uint8_t control;
|
||||
rt_be16_t pad; /* To be ATAPI compatible */
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_read_capacity10_data
|
||||
{
|
||||
rt_be32_t last_block;
|
||||
rt_be32_t block_size;
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_read_capacity16
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-5 Reserved, 4-0 SERVICE ACTION 0x10 */
|
||||
rt_be64_t logical_block_addr; /* only if PMI=1 */
|
||||
rt_be32_t alloc_len;
|
||||
rt_uint8_t pmi;
|
||||
rt_uint8_t control;
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_read_capacity16_data
|
||||
{
|
||||
rt_be64_t last_block;
|
||||
rt_be32_t block_size;
|
||||
rt_uint8_t pad[20];
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_read10
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-5 RDPROTECT, 4 DPO, 3 FUA, 2 RARC, 1 Obsolete, 0 Obsolete */
|
||||
rt_be32_t lba;
|
||||
rt_uint8_t reserved;
|
||||
rt_be16_t size;
|
||||
rt_uint8_t reserved2;
|
||||
rt_be16_t pad;
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_read12
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-5 RDPROTECT, 4 DPO, 3 FUA, 2 RARC, 1 Obsolete, 0 Obsolete */
|
||||
rt_be32_t lba;
|
||||
rt_be32_t size;
|
||||
rt_uint8_t reserved;
|
||||
rt_uint8_t control;
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_read16
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-5 RDPROTECT, 4 DPO, 3 FUA, 2 RARC, 1 Obsolete, 0 DLD2 */
|
||||
rt_be64_t lba;
|
||||
rt_be32_t size;
|
||||
rt_uint8_t reserved;
|
||||
rt_uint8_t control;
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_write10
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-5 WRPROTECT, 4 DPO, 3 FUA, 2 Reserved, 1 Obsolete, 0 Obsolete */
|
||||
rt_be32_t lba;
|
||||
rt_uint8_t reserved;
|
||||
rt_be16_t size;
|
||||
rt_uint8_t reserved2;
|
||||
rt_be16_t pad;
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_write12
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-5 WRPROTECT, 4 DPO, 3 FUA, 2 Reserved, 1 Obsolete, 0 Obsolete */
|
||||
rt_be32_t lba;
|
||||
rt_be32_t size;
|
||||
rt_uint8_t reserved;
|
||||
rt_uint8_t control;
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_write16
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-5 WRPROTECT, 4 DPO, 3 FUA, 2 Reserved, 1 Obsolete, 0 DLD2 */
|
||||
rt_be64_t lba;
|
||||
rt_be32_t size;
|
||||
rt_uint8_t reserved;
|
||||
rt_uint8_t control;
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_synchronize_cache10
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-3 Reserved, 2 Obsolete, 1 IMMED, 0 Obsolete */
|
||||
rt_be32_t lba;
|
||||
rt_uint8_t reserved;
|
||||
rt_be16_t size;
|
||||
rt_uint8_t control;
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_synchronize_cache16
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-3 Reserved, 2 Obsolete, 1 IMMED, 0 Obsolete */
|
||||
rt_be64_t lba;
|
||||
rt_be32_t size;
|
||||
rt_uint8_t reserved;
|
||||
rt_uint8_t control;
|
||||
});
|
||||
|
||||
#define RT_SCSI_UNMAP_SHIFT 3
|
||||
|
||||
rt_packed(struct rt_scsi_write_same10
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-5 WRPROTECT, 4 ANCHOR, 3 UNMAP, 2 Obsolete, 1 Obsolete, 0 Obsolete */
|
||||
rt_be32_t lba;
|
||||
rt_uint8_t reserved;
|
||||
rt_be16_t size;
|
||||
rt_uint8_t control;
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_write_same16
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-5 WRPROTECT, 4 ANCHOR, 3 UNMAP, 2 Obsolete, 1 Obsolete, 0 NDOB */
|
||||
rt_be64_t lba;
|
||||
rt_be32_t size;
|
||||
rt_uint8_t reserved;
|
||||
rt_uint8_t control;
|
||||
});
|
||||
|
||||
#define RT_SCSI_PF_SHIFT 4
|
||||
#define RT_SCSI_RTD_SHIFT 1
|
||||
#define RT_SCSI_SP_SHIFT 0
|
||||
|
||||
rt_packed(struct rt_scsi_mode_select6
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-5 Reserved, 4 PF, 3-2 Reserved, 1 RTD, 0 SP */
|
||||
rt_uint8_t reserved[2];
|
||||
rt_uint8_t param_list_len;
|
||||
rt_uint8_t control;
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_mode_select10
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-5 Reserved, 4 PF, 3-1 Reserved, 0 SP */
|
||||
rt_uint8_t reserved[5];
|
||||
rt_be16_t param_list_len;
|
||||
rt_uint8_t control;
|
||||
});
|
||||
|
||||
struct rt_scsi_mode_select_data
|
||||
{
|
||||
rt_uint32_t length;
|
||||
rt_uint16_t block_descriptor_length;
|
||||
rt_uint8_t medium_type;
|
||||
rt_uint8_t device_specific;
|
||||
rt_uint8_t header_length;
|
||||
rt_uint8_t longlba:1;
|
||||
};
|
||||
|
||||
#define RT_SCSI_DBD_SHIFT 3
|
||||
#define RT_SCSI_LLBAA_SHIFT 4
|
||||
#define RT_SCSI_PC_SHIFT 6
|
||||
#define RT_SCSI_PAGE_CODE_SHIFT 0
|
||||
|
||||
rt_packed(struct rt_scsi_mode_sense6
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-4 Reserved, 3 DBD, 2-0 Reserved */
|
||||
rt_uint8_t page_control_code;
|
||||
rt_uint8_t subpage_code;
|
||||
rt_uint8_t allocation_len;
|
||||
rt_uint8_t control;
|
||||
});
|
||||
|
||||
rt_packed(struct rt_scsi_mode_sense10
|
||||
{
|
||||
rt_uint8_t opcode;
|
||||
rt_uint8_t config; /* 7-5 Reserved, 4 LLBAA, 3 DBD, 2-0 Reserved */
|
||||
rt_uint8_t page_control_code;
|
||||
rt_uint8_t subpage_code;
|
||||
rt_uint8_t reserved[3];
|
||||
rt_be16_t allocation_len;
|
||||
rt_uint8_t control;
|
||||
});
|
||||
|
||||
#define RT_SCSI_CMD_TEST_UNIT_READY 0x00
|
||||
#define RT_SCSI_CMD_REQUEST_SENSE 0x03
|
||||
#define RT_SCSI_CMD_INQUIRY 0x12
|
||||
#define RT_SCSI_CMD_MODE_SELECT 0x15
|
||||
#define RT_SCSI_CMD_MODE_SENSE 0x1a
|
||||
#define RT_SCSI_CMD_READ_CAPACITY10 0x25
|
||||
#define RT_SCSI_CMD_READ10 0x28
|
||||
#define RT_SCSI_CMD_WRITE10 0x2a
|
||||
#define RT_SCSI_CMD_SYNCHRONIZE_CACHE10 0x35
|
||||
#define RT_SCSI_CMD_WRITE_SAME10 0x41
|
||||
#define RT_SCSI_CMD_MODE_SELECT10 0x55
|
||||
#define RT_SCSI_CMD_MODE_SENSE10 0x5a
|
||||
#define RT_SCSI_CMD_READ16 0x88
|
||||
#define RT_SCSI_CMD_WRITE16 0x8a
|
||||
#define RT_SCSI_CMD_SYNCHRONIZE_CACHE16 0x91
|
||||
#define RT_SCSI_CMD_WRITE_SAME16 0x93
|
||||
#define RT_SCSI_CMD_READ_CAPACITY16 0x9e
|
||||
#define RT_SCSI_CMD_READ12 0xa8
|
||||
#define RT_SCSI_CMD_WRITE12 0xaa
|
||||
|
||||
struct rt_scsi_cmd
|
||||
{
|
||||
union
|
||||
{
|
||||
struct rt_scsi_unknow unknow;
|
||||
struct rt_scsi_test_unit_ready test_unit_ready;
|
||||
struct rt_scsi_inquiry inquiry;
|
||||
struct rt_scsi_request_sense request_sense;
|
||||
struct rt_scsi_read_capacity10 read_capacity10;
|
||||
struct rt_scsi_read_capacity16 read_capacity16;
|
||||
struct rt_scsi_read10 read10;
|
||||
struct rt_scsi_read12 read12;
|
||||
struct rt_scsi_read16 read16;
|
||||
struct rt_scsi_write10 write10;
|
||||
struct rt_scsi_write12 write12;
|
||||
struct rt_scsi_write16 write16;
|
||||
struct rt_scsi_synchronize_cache10 synchronize_cache10;
|
||||
struct rt_scsi_synchronize_cache16 synchronize_cache16;
|
||||
struct rt_scsi_write_same10 write_same10;
|
||||
struct rt_scsi_write_same16 write_same16;
|
||||
struct rt_scsi_mode_select6 mode_select6;
|
||||
struct rt_scsi_mode_select10 mode_select10;
|
||||
struct rt_scsi_mode_sense6 mode_sense6;
|
||||
struct rt_scsi_mode_sense10 mode_sense10;
|
||||
} op;
|
||||
rt_size_t op_size;
|
||||
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
struct rt_scsi_inquiry_data inquiry;
|
||||
struct rt_scsi_request_sense_data request_sense;
|
||||
struct rt_scsi_read_capacity10_data read_capacity10;
|
||||
struct rt_scsi_read_capacity16_data read_capacity16;
|
||||
};
|
||||
struct
|
||||
{
|
||||
void *ptr;
|
||||
rt_size_t size;
|
||||
};
|
||||
} data;
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
SCSI_DEVICE_TYPE_DIRECT = 0x00, /* DiskPeripheral (GenDisk) */
|
||||
SCSI_DEVICE_TYPE_SEQUENTIAL = 0x01, /* TapePeripheral */
|
||||
SCSI_DEVICE_TYPE_PRINTER = 0x02, /* PrinterPeripheral (GenPrinter) */
|
||||
SCSI_DEVICE_TYPE_PROCESSOR = 0x03, /* OtherPeripheral */
|
||||
SCSI_DEVICE_TYPE_WRITE_ONCE_READ_MULTIPLE = 0x04, /* WormPeripheral (GenWorm) */
|
||||
SCSI_DEVICE_TYPE_CDROM = 0x05, /* CdRomPeripheral (GenCdRom) */
|
||||
SCSI_DEVICE_TYPE_SCANNER = 0x06, /* ScannerPeripheral (GenScanner) */
|
||||
SCSI_DEVICE_TYPE_OPTICAL = 0x07, /* OpticalDiskPeripheral (GenOptical) */
|
||||
SCSI_DEVICE_TYPE_MEDIUM_CHANGER = 0x08, /* MediumChangerPeripheral (ScsiChanger) */
|
||||
SCSI_DEVICE_TYPE_COMMUNICATION = 0x09, /* CommunicationsPeripheral (ScsiNet) */
|
||||
SCSI_DEVICE_TYPE_ASC_PREPRESS_GRAPHICS10 = 0x0a, /* ASCPrePressGraphicsPeripheral (ScsiASCIT8) */
|
||||
SCSI_DEVICE_TYPE_ASC_PREPRESS_GRAPHICS11 = 0x0b, /* ASCPrePressGraphicsPeripheral (ScsiASCIT8) */
|
||||
SCSI_DEVICE_TYPE_ARRAY = 0x0c, /* ArrayPeripheral (ScsiArray) */
|
||||
SCSI_DEVICE_TYPE_ENCLOSURE = 0x0d, /* EnclosurePeripheral (ScsiEnclosure) */
|
||||
SCSI_DEVICE_TYPE_RBC = 0x0e, /* RBCPeripheral (ScsiRBC) */
|
||||
SCSI_DEVICE_TYPE_CARDREADER = 0x0f, /* CardReaderPeripheral (ScsiCardReader) */
|
||||
SCSI_DEVICE_TYPE_BRIDGE = 0x10, /* BridgePeripheral (ScsiBridge) */
|
||||
SCSI_DEVICE_TYPE_OTHER = 0x11, /* OtherPeripheral (ScsiOther) */
|
||||
SCSI_DEVICE_TYPE_MAX,
|
||||
};
|
||||
|
||||
struct rt_scsi_ops;
|
||||
|
||||
struct rt_scsi_host
|
||||
{
|
||||
struct rt_device *dev;
|
||||
|
||||
const struct rt_scsi_ops *ops;
|
||||
|
||||
rt_size_t max_id;
|
||||
rt_size_t max_lun;
|
||||
|
||||
rt_list_t lun_nodes;
|
||||
};
|
||||
|
||||
struct rt_scsi_device
|
||||
{
|
||||
struct rt_scsi_host *host;
|
||||
|
||||
rt_list_t list;
|
||||
|
||||
rt_size_t id;
|
||||
rt_size_t lun;
|
||||
rt_uint32_t devtype;
|
||||
rt_uint32_t removable;
|
||||
rt_size_t last_block;
|
||||
rt_size_t block_size;
|
||||
|
||||
void *priv;
|
||||
};
|
||||
|
||||
struct rt_scsi_ops
|
||||
{
|
||||
rt_err_t (*reset)(struct rt_scsi_device *sdev);
|
||||
rt_err_t (*transfer)(struct rt_scsi_device *sdev, struct rt_scsi_cmd *cmd);
|
||||
};
|
||||
|
||||
rt_err_t rt_scsi_host_register(struct rt_scsi_host *scsi);
|
||||
rt_err_t rt_scsi_host_unregister(struct rt_scsi_host *scsi);
|
||||
|
||||
rt_inline rt_bool_t rt_scsi_cmd_is_write(struct rt_scsi_cmd *cmd)
|
||||
{
|
||||
return cmd->op.write10.opcode == RT_SCSI_CMD_WRITE10 ||
|
||||
cmd->op.write12.opcode == RT_SCSI_CMD_WRITE16 ||
|
||||
cmd->op.write16.opcode == RT_SCSI_CMD_WRITE12;
|
||||
}
|
||||
|
||||
rt_err_t rt_scsi_request_sense(struct rt_scsi_device *sdev,
|
||||
struct rt_scsi_request_sense_data *out_data);
|
||||
|
||||
rt_err_t rt_scsi_test_unit_ready(struct rt_scsi_device *sdev);
|
||||
rt_err_t rt_scsi_inquiry(struct rt_scsi_device *sdev,
|
||||
struct rt_scsi_inquiry_data *out_data);
|
||||
|
||||
rt_err_t rt_scsi_read_capacity10(struct rt_scsi_device *sdev,
|
||||
struct rt_scsi_read_capacity10_data *out_data);
|
||||
rt_err_t rt_scsi_read_capacity16(struct rt_scsi_device *sdev,
|
||||
struct rt_scsi_read_capacity16_data *out_data);
|
||||
|
||||
rt_err_t rt_scsi_read10(struct rt_scsi_device *sdev,
|
||||
rt_off_t lba, void *buffer, rt_size_t size);
|
||||
rt_err_t rt_scsi_read12(struct rt_scsi_device *sdev,
|
||||
rt_off_t lba, void *buffer, rt_size_t size);
|
||||
rt_err_t rt_scsi_read16(struct rt_scsi_device *sdev,
|
||||
rt_off_t lba, void *buffer, rt_size_t size);
|
||||
|
||||
rt_err_t rt_scsi_write10(struct rt_scsi_device *sdev,
|
||||
rt_off_t lba, const void *buffer, rt_size_t size);
|
||||
rt_err_t rt_scsi_write12(struct rt_scsi_device *sdev,
|
||||
rt_off_t lba, const void *buffer, rt_size_t size);
|
||||
rt_err_t rt_scsi_write16(struct rt_scsi_device *sdev,
|
||||
rt_off_t lba, const void *buffer, rt_size_t size);
|
||||
|
||||
rt_err_t rt_scsi_synchronize_cache10(struct rt_scsi_device *sdev,
|
||||
rt_off_t lba, rt_size_t size);
|
||||
rt_err_t rt_scsi_synchronize_cache16(struct rt_scsi_device *sdev,
|
||||
rt_off_t lba, rt_size_t size);
|
||||
|
||||
rt_err_t rt_scsi_write_same10(struct rt_scsi_device *sdev,
|
||||
rt_off_t lba, rt_size_t size);
|
||||
rt_err_t rt_scsi_write_same16(struct rt_scsi_device *sdev,
|
||||
rt_off_t lba, rt_size_t size);
|
||||
|
||||
rt_err_t rt_scsi_mode_select6(struct rt_scsi_device *sdev,
|
||||
rt_uint8_t pf, rt_uint8_t sp, void *buffer, rt_size_t size,
|
||||
struct rt_scsi_mode_select_data *data);
|
||||
rt_err_t rt_scsi_mode_select10(struct rt_scsi_device *sdev,
|
||||
rt_uint8_t pf, rt_uint8_t sp, void *buffer, rt_size_t size,
|
||||
struct rt_scsi_mode_select_data *data);
|
||||
|
||||
rt_err_t rt_scsi_mode_sense6(struct rt_scsi_device *sdev,
|
||||
rt_uint8_t dbd, rt_uint8_t modepage, rt_uint8_t subpage, void *buffer, rt_size_t size,
|
||||
struct rt_scsi_mode_select_data *data);
|
||||
rt_err_t rt_scsi_mode_sense10(struct rt_scsi_device *sdev,
|
||||
rt_uint8_t dbd, rt_uint8_t modepage, rt_uint8_t subpage, void *buffer, rt_size_t size,
|
||||
struct rt_scsi_mode_select_data *data);
|
||||
|
||||
#endif /* __SCSI_H__ */
|
||||
@@ -67,6 +67,10 @@ extern "C" {
|
||||
#include "drivers/pic.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_SCSI
|
||||
#include "drivers/scsi.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_MFD_SYSCON
|
||||
#include "drivers/syscon.h"
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
menuconfig RT_USING_SCSI
|
||||
bool "Using Small Computer System Interface (SCSI)"
|
||||
depends on RT_USING_DM
|
||||
default n
|
||||
|
||||
config RT_SCSI_SD
|
||||
bool "SD device on SCSI"
|
||||
depends on RT_USING_SCSI
|
||||
depends on RT_USING_BLK
|
||||
default y
|
||||
|
||||
config RT_SCSI_CDROM
|
||||
bool "CD-ROM device on SCSI"
|
||||
depends on RT_USING_SCSI
|
||||
depends on RT_USING_BLK
|
||||
default y
|
||||
|
||||
if RT_USING_SCSI
|
||||
osource "$(SOC_DM_SCSI_DIR)/Kconfig"
|
||||
endif
|
||||
@@ -0,0 +1,21 @@
|
||||
from building import *
|
||||
|
||||
group = []
|
||||
|
||||
if not GetDepend(['RT_USING_SCSI']):
|
||||
Return('group')
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
CPPPATH = [cwd + '/../include']
|
||||
|
||||
src = ['scsi.c']
|
||||
|
||||
if GetDepend(['RT_SCSI_SD']):
|
||||
src += ['scsi_sd.c']
|
||||
|
||||
if GetDepend(['RT_SCSI_CDROM']):
|
||||
src += ['scsi_cdrom.c']
|
||||
|
||||
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-02-25 GuEe-GUI the first version
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#define DBG_TAG "scsi.blk"
|
||||
#define DBG_LVL DBG_INFO
|
||||
#include <rtdbg.h>
|
||||
|
||||
struct scsi_cdrom
|
||||
{
|
||||
struct rt_blk_disk parent;
|
||||
struct rt_scsi_device *sdev;
|
||||
|
||||
int cdrom_id;
|
||||
struct rt_device_blk_geometry geometry;
|
||||
};
|
||||
|
||||
#define raw_to_scsi_cdrom(raw) rt_container_of(raw, struct scsi_cdrom, parent)
|
||||
|
||||
static struct rt_dm_ida cdrom_ida = RT_DM_IDA_INIT(CUSTOM);
|
||||
static struct rt_dm_ida scsi_cdrom_ida = RT_DM_IDA_INIT(SCSI_CDROM);
|
||||
|
||||
static rt_ssize_t scsi_cdrom_read(struct rt_blk_disk *disk, rt_off_t sector,
|
||||
void *buffer, rt_size_t sector_count)
|
||||
{
|
||||
rt_err_t err;
|
||||
struct scsi_cdrom *scdrom = raw_to_scsi_cdrom(disk);
|
||||
struct rt_scsi_device *sdev = scdrom->sdev;
|
||||
|
||||
sector_count &= RT_UINT32_MAX;
|
||||
|
||||
if (sector >> 32)
|
||||
{
|
||||
err = rt_scsi_read16(sdev, sector, buffer, sector_count);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = rt_scsi_read12(sdev, sector, buffer, sector_count);
|
||||
}
|
||||
|
||||
return !err ? sector_count : (rt_ssize_t)err;
|
||||
}
|
||||
|
||||
static rt_err_t scsi_cdrom_getgeome(struct rt_blk_disk *disk,
|
||||
struct rt_device_blk_geometry *geometry)
|
||||
{
|
||||
struct scsi_cdrom *scdrom = raw_to_scsi_cdrom(disk);
|
||||
|
||||
rt_memcpy(geometry, &scdrom->geometry, sizeof(scdrom->geometry));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static const struct rt_blk_disk_ops scsi_cdrom_ops =
|
||||
{
|
||||
.read = scsi_cdrom_read,
|
||||
.getgeome = scsi_cdrom_getgeome,
|
||||
};
|
||||
|
||||
rt_err_t scsi_cdrom_probe(struct rt_scsi_device *sdev)
|
||||
{
|
||||
rt_err_t err;
|
||||
union
|
||||
{
|
||||
struct rt_scsi_read_capacity10_data capacity10;
|
||||
struct rt_scsi_read_capacity16_data capacity16;
|
||||
} data;
|
||||
struct scsi_cdrom *scdrom = rt_calloc(1, sizeof(*scdrom));
|
||||
|
||||
if (!scdrom)
|
||||
{
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
if ((scdrom->cdrom_id = rt_dm_ida_alloc(&cdrom_ida)) < 0)
|
||||
{
|
||||
return -RT_EFULL;
|
||||
}
|
||||
|
||||
sdev->priv = scdrom;
|
||||
scdrom->sdev = sdev;
|
||||
scdrom->parent.ida = &scsi_cdrom_ida;
|
||||
scdrom->parent.read_only = RT_TRUE;
|
||||
scdrom->parent.parallel_io = RT_FALSE;
|
||||
scdrom->parent.ops = &scsi_cdrom_ops;
|
||||
scdrom->parent.max_partitions = RT_BLK_PARTITION_NONE;
|
||||
|
||||
if ((err = rt_scsi_read_capacity10(sdev, &data.capacity10)))
|
||||
{
|
||||
goto _fail;
|
||||
}
|
||||
if (data.capacity10.last_block == 0xffffffff)
|
||||
{
|
||||
if ((err = rt_scsi_read_capacity16(sdev, &data.capacity16)))
|
||||
{
|
||||
goto _fail;
|
||||
}
|
||||
}
|
||||
scdrom->geometry.bytes_per_sector = sdev->block_size;
|
||||
scdrom->geometry.block_size = sdev->block_size;
|
||||
scdrom->geometry.sector_count = sdev->last_block + 1;
|
||||
|
||||
rt_dm_dev_set_name(&scdrom->parent.parent, "cdrom%u", scdrom->cdrom_id);
|
||||
|
||||
if ((err = rt_hw_blk_disk_register(&scdrom->parent)))
|
||||
{
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
|
||||
_fail:
|
||||
rt_dm_ida_free(&cdrom_ida, scdrom->cdrom_id);
|
||||
rt_free(scdrom);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_err_t scsi_cdrom_remove(struct rt_scsi_device *sdev)
|
||||
{
|
||||
struct scsi_cdrom *scdrom = sdev->priv;
|
||||
|
||||
rt_dm_ida_free(&cdrom_ida, scdrom->cdrom_id);
|
||||
|
||||
return rt_hw_blk_disk_unregister(&scdrom->parent);
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-02-25 GuEe-GUI the first version
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#define DBG_TAG "scsi.blk"
|
||||
#define DBG_LVL DBG_INFO
|
||||
#include <rtdbg.h>
|
||||
|
||||
#include "../block/blk_dev.h"
|
||||
|
||||
struct scsi_sd
|
||||
{
|
||||
struct rt_blk_disk parent;
|
||||
struct rt_scsi_device *sdev;
|
||||
|
||||
int sd_id;
|
||||
rt_bool_t use16;
|
||||
struct rt_device_blk_geometry geometry;
|
||||
};
|
||||
|
||||
#define raw_to_scsi_sd(raw) rt_container_of(raw, struct scsi_sd, parent)
|
||||
|
||||
static struct rt_dm_ida sd_ida = RT_DM_IDA_INIT(CUSTOM);
|
||||
static struct rt_dm_ida scsi_sd_ida = RT_DM_IDA_INIT(SCSI_SD);
|
||||
|
||||
static rt_ssize_t scsi_sd_read(struct rt_blk_disk *disk, rt_off_t sector,
|
||||
void *buffer, rt_size_t sector_count)
|
||||
{
|
||||
rt_err_t err;
|
||||
struct scsi_sd *ssd = raw_to_scsi_sd(disk);
|
||||
struct rt_scsi_device *sdev = ssd->sdev;
|
||||
|
||||
sector_count &= RT_UINT32_MAX;
|
||||
|
||||
if (sector >> 32)
|
||||
{
|
||||
err = rt_scsi_read16(sdev, sector, buffer, sector_count);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = rt_scsi_read10(sdev, sector, buffer, sector_count);
|
||||
}
|
||||
|
||||
return !err ? sector_count : (rt_ssize_t)err;
|
||||
}
|
||||
|
||||
static rt_ssize_t scsi_sd_write(struct rt_blk_disk *disk, rt_off_t sector,
|
||||
const void *buffer, rt_size_t sector_count)
|
||||
{
|
||||
rt_err_t err;
|
||||
struct scsi_sd *ssd = raw_to_scsi_sd(disk);
|
||||
struct rt_scsi_device *sdev = ssd->sdev;
|
||||
|
||||
sector_count &= RT_UINT32_MAX;
|
||||
|
||||
if (sector >> 32)
|
||||
{
|
||||
err = rt_scsi_write16(sdev, sector, buffer, sector_count);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = rt_scsi_write10(sdev, sector, buffer, sector_count);
|
||||
}
|
||||
|
||||
return !err ? sector_count : (rt_ssize_t)err;
|
||||
}
|
||||
|
||||
static rt_err_t scsi_sd_getgeome(struct rt_blk_disk *disk,
|
||||
struct rt_device_blk_geometry *geometry)
|
||||
{
|
||||
struct scsi_sd *ssd = raw_to_scsi_sd(disk);
|
||||
|
||||
rt_memcpy(geometry, &ssd->geometry, sizeof(ssd->geometry));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t scsi_sd_sync(struct rt_blk_disk *disk)
|
||||
{
|
||||
rt_err_t err;
|
||||
rt_size_t lba_count;
|
||||
struct scsi_sd *ssd = raw_to_scsi_sd(disk);
|
||||
struct rt_scsi_device *sdev = ssd->sdev;
|
||||
|
||||
lba_count = ssd->geometry.sector_count;
|
||||
|
||||
if (ssd->use16)
|
||||
{
|
||||
err = rt_scsi_synchronize_cache16(sdev, 0, lba_count);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = rt_scsi_synchronize_cache10(sdev, 0, lba_count);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static rt_err_t scsi_sd_erase(struct rt_blk_disk *disk)
|
||||
{
|
||||
rt_err_t err;
|
||||
rt_size_t lba_count;
|
||||
struct scsi_sd *ssd = raw_to_scsi_sd(disk);
|
||||
struct rt_scsi_device *sdev = ssd->sdev;
|
||||
|
||||
lba_count = ssd->geometry.sector_count;
|
||||
|
||||
if (ssd->use16)
|
||||
{
|
||||
err = rt_scsi_write_same16(sdev, 0, lba_count);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = rt_scsi_write_same10(sdev, 0, lba_count);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static rt_err_t scsi_sd_autorefresh(struct rt_blk_disk *disk, rt_bool_t is_auto)
|
||||
{
|
||||
rt_err_t err;
|
||||
int sp;
|
||||
rt_size_t size;
|
||||
rt_uint8_t buffer[64];
|
||||
rt_uint8_t *buffer_data;
|
||||
rt_bool_t use6 = RT_TRUE;
|
||||
struct scsi_sd *ssd = raw_to_scsi_sd(disk);
|
||||
struct rt_scsi_device *sdev = ssd->sdev;
|
||||
struct rt_scsi_mode_select_data data;
|
||||
|
||||
err = rt_scsi_mode_sense6(sdev, 0x08, 8, 0, buffer, sizeof(buffer), &data);
|
||||
|
||||
if (err && err != -RT_ENOMEM)
|
||||
{
|
||||
use6 = RT_FALSE;
|
||||
err = rt_scsi_mode_sense10(sdev, 0x08, 8, 0, buffer, sizeof(buffer), &data);
|
||||
}
|
||||
if (err)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
size = rt_min_t(rt_size_t, sizeof(buffer),
|
||||
data.length - data.header_length - data.block_descriptor_length);
|
||||
buffer_data = buffer + data.header_length + data.block_descriptor_length;
|
||||
buffer_data[2] &= ~0x05;
|
||||
buffer_data[2] |= (!!is_auto) << 2 | (!!is_auto);
|
||||
sp = buffer_data[0] & 0x80 ? 1 : 0;
|
||||
buffer_data[0] &= ~0x80;
|
||||
data.device_specific = 0;
|
||||
|
||||
if (use6)
|
||||
{
|
||||
err = rt_scsi_mode_select6(sdev, 1, sp, buffer_data, size, &data);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = rt_scsi_mode_select10(sdev, 1, sp, buffer_data, size, &data);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static const struct rt_blk_disk_ops scsi_sd_ops =
|
||||
{
|
||||
.read = scsi_sd_read,
|
||||
.write = scsi_sd_write,
|
||||
.getgeome = scsi_sd_getgeome,
|
||||
.sync = scsi_sd_sync,
|
||||
.erase = scsi_sd_erase,
|
||||
.autorefresh = scsi_sd_autorefresh,
|
||||
};
|
||||
|
||||
rt_err_t scsi_sd_probe(struct rt_scsi_device *sdev)
|
||||
{
|
||||
rt_err_t err;
|
||||
union
|
||||
{
|
||||
struct rt_scsi_read_capacity10_data capacity10;
|
||||
struct rt_scsi_read_capacity16_data capacity16;
|
||||
} data;
|
||||
struct scsi_sd *ssd = rt_calloc(1, sizeof(*ssd));
|
||||
|
||||
if (!ssd)
|
||||
{
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
if ((ssd->sd_id = rt_dm_ida_alloc(&sd_ida)) < 0)
|
||||
{
|
||||
return -RT_EFULL;
|
||||
}
|
||||
|
||||
sdev->priv = ssd;
|
||||
ssd->sdev = sdev;
|
||||
ssd->parent.ida = &scsi_sd_ida;
|
||||
ssd->parent.parallel_io = RT_FALSE;
|
||||
ssd->parent.ops = &scsi_sd_ops;
|
||||
ssd->parent.max_partitions = RT_BLK_PARTITION_MAX;
|
||||
|
||||
if ((err = rt_scsi_read_capacity10(sdev, &data.capacity10)))
|
||||
{
|
||||
goto _fail;
|
||||
}
|
||||
if (data.capacity10.last_block == 0xffffffff)
|
||||
{
|
||||
if ((err = rt_scsi_read_capacity16(sdev, &data.capacity16)))
|
||||
{
|
||||
goto _fail;
|
||||
}
|
||||
ssd->use16 = RT_TRUE;
|
||||
}
|
||||
ssd->geometry.bytes_per_sector = sdev->block_size;
|
||||
ssd->geometry.block_size = sdev->block_size;
|
||||
ssd->geometry.sector_count = sdev->last_block + 1;
|
||||
|
||||
rt_dm_dev_set_name(&ssd->parent.parent, "sd%c%c", letter_name(ssd->sd_id));
|
||||
|
||||
if ((err = rt_hw_blk_disk_register(&ssd->parent)))
|
||||
{
|
||||
goto _fail;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
|
||||
_fail:
|
||||
rt_dm_ida_free(&sd_ida, ssd->sd_id);
|
||||
rt_free(ssd);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_err_t scsi_sd_remove(struct rt_scsi_device *sdev)
|
||||
{
|
||||
struct scsi_sd *ssd = sdev->priv;
|
||||
|
||||
rt_dm_ida_free(&sd_ida, ssd->sd_id);
|
||||
|
||||
return rt_hw_blk_disk_unregister(&ssd->parent);
|
||||
}
|
||||
Reference in New Issue
Block a user