2007-11-22 20:51:00 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2008-04-18 16:42:50 +00:00
|
|
|
* $Id$
|
2007-11-22 20:51:00 +00:00
|
|
|
*
|
2008-05-05 10:32:46 +00:00
|
|
|
* Copyright (C) 2007 by Björn Stenberg
|
2007-11-22 20:51:00 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00: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; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2007-11-22 20:51:00 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "string.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "usb_core.h"
|
|
|
|
#include "usb_drv.h"
|
2009-04-18 20:04:52 +00:00
|
|
|
#include "usb_class_driver.h"
|
2009-11-03 16:25:03 +00:00
|
|
|
/*#define LOGF_ENABLE*/
|
2007-11-22 20:51:00 +00:00
|
|
|
#include "logf.h"
|
2008-11-01 16:14:28 +00:00
|
|
|
#include "storage.h"
|
2008-02-11 14:26:25 +00:00
|
|
|
#include "disk.h"
|
2017-03-13 02:05:44 +00:00
|
|
|
#include "fs_defines.h"
|
2008-02-27 20:25:32 +00:00
|
|
|
/* Needed to get at the audio buffer */
|
|
|
|
#include "audio.h"
|
2008-05-03 07:10:21 +00:00
|
|
|
#include "usb_storage.h"
|
2011-12-16 00:09:28 +00:00
|
|
|
#if CONFIG_RTC
|
2009-08-11 17:54:47 +00:00
|
|
|
#include "timefuncs.h"
|
2011-12-16 00:09:28 +00:00
|
|
|
#endif
|
2013-05-30 09:24:16 +00:00
|
|
|
#include "core_alloc.h"
|
2014-01-05 19:56:16 +00:00
|
|
|
#include "panic.h"
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2008-10-20 21:49:59 +00:00
|
|
|
#ifdef USB_USE_RAMDISK
|
|
|
|
#define RAMDISK_SIZE 2048
|
|
|
|
#endif
|
|
|
|
|
2011-01-05 19:35:51 +00:00
|
|
|
/* These defaults allow the operation */
|
|
|
|
#ifndef USBSTOR_READ_SECTORS_FILTER
|
|
|
|
#define USBSTOR_READ_SECTORS_FILTER() ({ 0; })
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef USBSTOR_WRITE_SECTORS_FILTER
|
2021-04-23 15:09:49 +00:00
|
|
|
#define USBSTOR_WRITE_SECTORS_FILTER() ({ 0; })
|
2011-01-05 19:35:51 +00:00
|
|
|
#endif
|
|
|
|
|
2009-03-29 19:56:21 +00:00
|
|
|
/* the ARC driver currently supports up to 64k USB transfers. This is
|
|
|
|
* enough for efficient mass storage support, as commonly host OSes
|
|
|
|
* don't do larger SCSI transfers anyway, so larger USB transfers
|
|
|
|
* wouldn't buy us anything.
|
2009-05-19 18:51:03 +00:00
|
|
|
* Due to being the double-buffering system used, using a smaller write buffer
|
|
|
|
* ends up being more efficient. Measurements have shown that 24k to 28k is
|
2009-05-20 16:57:17 +00:00
|
|
|
* optimal, except for sd devices that apparently don't gain anything from
|
|
|
|
* double-buffering
|
2008-02-20 22:54:26 +00:00
|
|
|
*/
|
2010-03-11 00:59:17 +00:00
|
|
|
#ifdef USB_READ_BUFFER_SIZE
|
|
|
|
#define READ_BUFFER_SIZE USB_READ_BUFFER_SIZE
|
|
|
|
#else
|
2011-12-31 14:42:10 +00:00
|
|
|
#if CONFIG_USBOTG == USBOTG_AS3525
|
2010-06-22 05:46:54 +00:00
|
|
|
/* We'd need to implement multidescriptor dma for sizes >65535 */
|
|
|
|
#define READ_BUFFER_SIZE (1024*63)
|
|
|
|
#else
|
2009-05-19 18:51:03 +00:00
|
|
|
#define READ_BUFFER_SIZE (1024*64)
|
2010-06-22 05:46:54 +00:00
|
|
|
#endif /* CONFIG_CPU == AS3525 */
|
|
|
|
#endif /* USB_READ_BUFFER_SIZE */
|
2009-05-20 16:57:17 +00:00
|
|
|
|
2014-01-04 22:35:00 +00:00
|
|
|
/* We don't use sizeof() here, because we *need* a multiple of 32 */
|
|
|
|
#define MAX_CBW_SIZE 32
|
2010-04-08 19:48:01 +00:00
|
|
|
|
2010-03-11 00:59:17 +00:00
|
|
|
#ifdef USB_WRITE_BUFFER_SIZE
|
|
|
|
#define WRITE_BUFFER_SIZE USB_WRITE_BUFFER_SIZE
|
|
|
|
#else
|
2009-05-20 16:57:17 +00:00
|
|
|
#if (CONFIG_STORAGE & STORAGE_SD)
|
2011-12-31 14:42:10 +00:00
|
|
|
#if CONFIG_USBOTG == USBOTG_AS3525
|
2010-06-22 05:46:54 +00:00
|
|
|
/* We'd need to implement multidescriptor dma for sizes >65535 */
|
|
|
|
#define WRITE_BUFFER_SIZE (1024*63)
|
|
|
|
#else
|
2009-05-20 16:57:17 +00:00
|
|
|
#define WRITE_BUFFER_SIZE (1024*64)
|
2010-06-22 05:46:54 +00:00
|
|
|
#endif /* CONFIG_CPU == AS3525 */
|
2009-05-20 16:57:17 +00:00
|
|
|
#else
|
2009-05-19 18:51:03 +00:00
|
|
|
#define WRITE_BUFFER_SIZE (1024*24)
|
2010-06-22 05:46:54 +00:00
|
|
|
#endif /* (CONFIG_STORAGE & STORAGE_SD) */
|
|
|
|
#endif /* USB_WRITE_BUFFER_SIZE */
|
2009-05-19 18:51:03 +00:00
|
|
|
|
|
|
|
#define ALLOCATE_BUFFER_SIZE (2*MAX(READ_BUFFER_SIZE,WRITE_BUFFER_SIZE))
|
2008-02-20 22:54:26 +00:00
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
/* bulk-only class specific requests */
|
|
|
|
#define USB_BULK_RESET_REQUEST 0xff
|
|
|
|
#define USB_BULK_GET_MAX_LUN 0xfe
|
|
|
|
|
2010-01-03 10:35:31 +00:00
|
|
|
#define DIRECT_ACCESS_DEVICE 0x00 /* disks */
|
2009-08-11 17:54:47 +00:00
|
|
|
#define DEVICE_REMOVABLE 0x80
|
2007-11-22 20:51:00 +00:00
|
|
|
|
|
|
|
#define CBW_SIGNATURE 0x43425355
|
|
|
|
#define CSW_SIGNATURE 0x53425355
|
|
|
|
|
|
|
|
#define SCSI_TEST_UNIT_READY 0x00
|
|
|
|
#define SCSI_INQUIRY 0x12
|
2008-02-20 22:54:26 +00:00
|
|
|
#define SCSI_MODE_SENSE_6 0x1a
|
|
|
|
#define SCSI_MODE_SENSE_10 0x5a
|
2008-02-11 14:26:25 +00:00
|
|
|
#define SCSI_REQUEST_SENSE 0x03
|
2007-11-22 20:51:00 +00:00
|
|
|
#define SCSI_ALLOW_MEDIUM_REMOVAL 0x1e
|
|
|
|
#define SCSI_READ_CAPACITY 0x25
|
2008-02-11 14:26:25 +00:00
|
|
|
#define SCSI_READ_FORMAT_CAPACITY 0x23
|
2007-11-22 20:51:00 +00:00
|
|
|
#define SCSI_READ_10 0x28
|
|
|
|
#define SCSI_WRITE_10 0x2a
|
2008-02-11 14:26:25 +00:00
|
|
|
#define SCSI_START_STOP_UNIT 0x1b
|
2008-02-20 22:54:26 +00:00
|
|
|
#define SCSI_REPORT_LUNS 0xa0
|
2009-08-11 17:54:47 +00:00
|
|
|
#define SCSI_WRITE_BUFFER 0x3b
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2008-02-28 22:35:23 +00:00
|
|
|
#define UMS_STATUS_GOOD 0x00
|
|
|
|
#define UMS_STATUS_FAIL 0x01
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2008-02-20 22:54:26 +00:00
|
|
|
#define SENSE_NOT_READY 0x02
|
|
|
|
#define SENSE_MEDIUM_ERROR 0x03
|
|
|
|
#define SENSE_ILLEGAL_REQUEST 0x05
|
|
|
|
#define SENSE_UNIT_ATTENTION 0x06
|
|
|
|
|
|
|
|
#define ASC_MEDIUM_NOT_PRESENT 0x3a
|
|
|
|
#define ASC_INVALID_FIELD_IN_CBD 0x24
|
|
|
|
#define ASC_LBA_OUT_OF_RANGE 0x21
|
|
|
|
#define ASC_WRITE_ERROR 0x0C
|
|
|
|
#define ASC_READ_ERROR 0x11
|
2008-03-02 00:15:02 +00:00
|
|
|
#define ASC_NOT_READY 0x04
|
2009-01-12 21:22:26 +00:00
|
|
|
#define ASC_INVALID_COMMAND 0x20
|
|
|
|
|
2008-03-02 00:15:02 +00:00
|
|
|
#define ASCQ_BECOMING_READY 0x01
|
2008-02-20 22:54:26 +00:00
|
|
|
|
2008-02-11 14:26:25 +00:00
|
|
|
#define SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA 0x02000000
|
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
/* storage interface */
|
|
|
|
|
|
|
|
#define USB_SC_SCSI 0x06 /* Transparent */
|
|
|
|
#define USB_PROT_BULK 0x50 /* bulk only */
|
|
|
|
|
|
|
|
static struct usb_interface_descriptor __attribute__((aligned(2)))
|
2009-04-18 21:32:41 +00:00
|
|
|
interface_descriptor =
|
2008-03-06 21:25:09 +00:00
|
|
|
{
|
|
|
|
.bLength = sizeof(struct usb_interface_descriptor),
|
|
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
.bAlternateSetting = 0,
|
|
|
|
.bNumEndpoints = 2,
|
|
|
|
.bInterfaceClass = USB_CLASS_MASS_STORAGE,
|
|
|
|
.bInterfaceSubClass = USB_SC_SCSI,
|
|
|
|
.bInterfaceProtocol = USB_PROT_BULK,
|
|
|
|
.iInterface = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_endpoint_descriptor __attribute__((aligned(2)))
|
|
|
|
endpoint_descriptor =
|
|
|
|
{
|
|
|
|
.bLength = sizeof(struct usb_endpoint_descriptor),
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = 0,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 0,
|
|
|
|
.bInterval = 0
|
|
|
|
};
|
2007-11-22 20:51:00 +00:00
|
|
|
|
|
|
|
struct inquiry_data {
|
|
|
|
unsigned char DeviceType;
|
|
|
|
unsigned char DeviceTypeModifier;
|
|
|
|
unsigned char Versions;
|
2009-04-18 21:32:41 +00:00
|
|
|
unsigned char Format;
|
2007-11-22 20:51:00 +00:00
|
|
|
unsigned char AdditionalLength;
|
|
|
|
unsigned char Reserved[2];
|
|
|
|
unsigned char Capability;
|
|
|
|
unsigned char VendorId[8];
|
|
|
|
unsigned char ProductId[16];
|
|
|
|
unsigned char ProductRevisionLevel[4];
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2008-02-20 22:54:26 +00:00
|
|
|
struct report_lun_data {
|
|
|
|
unsigned int lun_list_length;
|
|
|
|
unsigned int reserved1;
|
2009-07-17 22:28:49 +00:00
|
|
|
unsigned char luns[NUM_DRIVES][8];
|
2008-02-20 22:54:26 +00:00
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2008-02-11 14:26:25 +00:00
|
|
|
struct sense_data {
|
|
|
|
unsigned char ResponseCode;
|
|
|
|
unsigned char Obsolete;
|
2008-03-06 21:25:09 +00:00
|
|
|
unsigned char fei_sensekey;
|
2008-02-11 14:26:25 +00:00
|
|
|
unsigned int Information;
|
|
|
|
unsigned char AdditionalSenseLength;
|
|
|
|
unsigned int CommandSpecificInformation;
|
|
|
|
unsigned char AdditionalSenseCode;
|
|
|
|
unsigned char AdditionalSenseCodeQualifier;
|
|
|
|
unsigned char FieldReplaceableUnitCode;
|
|
|
|
unsigned char SKSV;
|
|
|
|
unsigned short SenseKeySpecific;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
struct mode_sense_bdesc_longlba {
|
|
|
|
unsigned char num_blocks[8];
|
2008-02-29 19:25:14 +00:00
|
|
|
unsigned char reserved[4];
|
|
|
|
unsigned char block_size[4];
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
struct mode_sense_bdesc_shortlba {
|
2008-02-29 19:25:14 +00:00
|
|
|
unsigned char density_code;
|
2008-03-06 21:25:09 +00:00
|
|
|
unsigned char num_blocks[3];
|
2008-02-29 19:25:14 +00:00
|
|
|
unsigned char reserved;
|
|
|
|
unsigned char block_size[3];
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
|
|
|
struct mode_sense_data_10 {
|
2008-02-20 22:54:26 +00:00
|
|
|
unsigned short mode_data_length;
|
|
|
|
unsigned char medium_type;
|
|
|
|
unsigned char device_specific;
|
2008-02-29 19:25:14 +00:00
|
|
|
unsigned char longlba;
|
|
|
|
unsigned char reserved;
|
2008-02-20 22:54:26 +00:00
|
|
|
unsigned short block_descriptor_length;
|
2008-03-06 21:25:09 +00:00
|
|
|
struct mode_sense_bdesc_longlba block_descriptor;
|
2008-02-20 22:54:26 +00:00
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2008-02-29 19:25:14 +00:00
|
|
|
struct mode_sense_data_6 {
|
2008-02-20 22:54:26 +00:00
|
|
|
unsigned char mode_data_length;
|
|
|
|
unsigned char medium_type;
|
|
|
|
unsigned char device_specific;
|
|
|
|
unsigned char block_descriptor_length;
|
2008-03-06 21:25:09 +00:00
|
|
|
struct mode_sense_bdesc_shortlba block_descriptor;
|
2008-02-20 22:54:26 +00:00
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
struct command_block_wrapper {
|
|
|
|
unsigned int signature;
|
|
|
|
unsigned int tag;
|
|
|
|
unsigned int data_transfer_length;
|
|
|
|
unsigned char flags;
|
|
|
|
unsigned char lun;
|
|
|
|
unsigned char command_length;
|
|
|
|
unsigned char command_block[16];
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
|
|
|
struct command_status_wrapper {
|
|
|
|
unsigned int signature;
|
|
|
|
unsigned int tag;
|
|
|
|
unsigned int data_residue;
|
|
|
|
unsigned char status;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
|
|
|
struct capacity {
|
|
|
|
unsigned int block_count;
|
|
|
|
unsigned int block_size;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2008-02-11 14:26:25 +00:00
|
|
|
struct format_capacity {
|
|
|
|
unsigned int following_length;
|
|
|
|
unsigned int block_count;
|
|
|
|
unsigned int block_size;
|
|
|
|
} __attribute__ ((packed));
|
|
|
|
|
2008-02-29 21:10:31 +00:00
|
|
|
|
|
|
|
static union {
|
|
|
|
unsigned char* transfer_buffer;
|
|
|
|
struct inquiry_data* inquiry;
|
|
|
|
struct capacity* capacity_data;
|
|
|
|
struct format_capacity* format_capacity_data;
|
|
|
|
struct sense_data *sense_data;
|
2008-03-06 21:25:09 +00:00
|
|
|
struct mode_sense_data_6 *ms_data_6;
|
|
|
|
struct mode_sense_data_10 *ms_data_10;
|
2008-02-29 21:10:31 +00:00
|
|
|
struct report_lun_data *lun_data;
|
|
|
|
struct command_status_wrapper* csw;
|
|
|
|
char *max_lun;
|
|
|
|
} tb;
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2009-05-18 19:45:24 +00:00
|
|
|
static char *cbw_buffer;
|
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
static struct {
|
|
|
|
unsigned int sector;
|
|
|
|
unsigned int count;
|
2009-02-28 19:39:28 +00:00
|
|
|
unsigned int orig_count;
|
|
|
|
unsigned int cur_cmd;
|
2007-11-22 20:51:00 +00:00
|
|
|
unsigned int tag;
|
2008-02-11 14:26:25 +00:00
|
|
|
unsigned int lun;
|
2008-02-20 22:54:26 +00:00
|
|
|
unsigned char *data[2];
|
|
|
|
unsigned char data_select;
|
|
|
|
unsigned int last_result;
|
2008-03-06 21:25:09 +00:00
|
|
|
} cur_cmd;
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2008-02-20 22:54:26 +00:00
|
|
|
static struct {
|
|
|
|
unsigned char sense_key;
|
|
|
|
unsigned char information;
|
|
|
|
unsigned char asc;
|
2008-03-02 00:15:02 +00:00
|
|
|
unsigned char ascq;
|
2008-02-20 22:54:26 +00:00
|
|
|
} cur_sense_data;
|
|
|
|
|
2008-02-11 14:26:25 +00:00
|
|
|
static void handle_scsi(struct command_block_wrapper* cbw);
|
2008-02-20 22:54:26 +00:00
|
|
|
static void send_csw(int status);
|
|
|
|
static void send_command_result(void *data,int size);
|
2008-04-01 20:36:51 +00:00
|
|
|
static void send_command_failed_result(void);
|
2008-02-20 22:54:26 +00:00
|
|
|
static void send_block_data(void *data,int size);
|
|
|
|
static void receive_block_data(void *data,int size);
|
2011-12-16 00:09:28 +00:00
|
|
|
#if CONFIG_RTC
|
2009-08-11 17:54:47 +00:00
|
|
|
static void receive_time(void);
|
2011-12-16 00:09:28 +00:00
|
|
|
#endif
|
2009-07-17 22:28:49 +00:00
|
|
|
static void fill_inquiry(IF_MD_NONVOID(int lun));
|
2008-02-20 22:54:26 +00:00
|
|
|
static void send_and_read_next(void);
|
2009-07-17 22:28:49 +00:00
|
|
|
static bool ejected[NUM_DRIVES];
|
|
|
|
static bool locked[NUM_DRIVES];
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
static int usb_interface;
|
2008-10-03 22:43:16 +00:00
|
|
|
static int ep_in, ep_out;
|
2008-03-06 21:25:09 +00:00
|
|
|
|
2011-09-09 16:15:35 +00:00
|
|
|
#if defined(HAVE_MULTIDRIVE)
|
|
|
|
static bool skip_first = 0;
|
|
|
|
#endif
|
|
|
|
|
2008-10-20 21:49:59 +00:00
|
|
|
#ifdef USB_USE_RAMDISK
|
|
|
|
static unsigned char* ramdisk_buffer;
|
|
|
|
#endif
|
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
static enum {
|
2008-02-20 22:54:26 +00:00
|
|
|
WAITING_FOR_COMMAND,
|
|
|
|
SENDING_BLOCKS,
|
|
|
|
SENDING_RESULT,
|
2008-04-01 20:36:51 +00:00
|
|
|
SENDING_FAILED_RESULT,
|
2008-02-20 22:54:26 +00:00
|
|
|
RECEIVING_BLOCKS,
|
2011-12-16 00:09:28 +00:00
|
|
|
#if CONFIG_RTC
|
2009-08-11 17:54:47 +00:00
|
|
|
RECEIVING_TIME,
|
2011-12-16 00:09:28 +00:00
|
|
|
#endif
|
2010-04-08 19:48:01 +00:00
|
|
|
WAITING_FOR_CSW_COMPLETION_OR_COMMAND,
|
|
|
|
WAITING_FOR_CSW_COMPLETION
|
2008-02-20 22:54:26 +00:00
|
|
|
} state = WAITING_FOR_COMMAND;
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2011-12-16 00:09:28 +00:00
|
|
|
#if CONFIG_RTC
|
2009-08-11 20:03:59 +00:00
|
|
|
static void yearday_to_daymonth(int yd, int y, int *d, int *m)
|
|
|
|
{
|
2012-05-07 05:30:45 +00:00
|
|
|
static char t[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
|
|
|
|
|
|
|
bool leap = (y%4 == 0 && y%100 != 0) || y%400 == 0;
|
2013-05-17 19:12:56 +00:00
|
|
|
t[1] = leap ? 29 : 28;
|
2012-05-07 05:30:45 +00:00
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 12 && yd >= t[i]; i++)
|
|
|
|
yd -= t[i];
|
2009-08-11 20:03:59 +00:00
|
|
|
|
|
|
|
*d = yd+1;
|
|
|
|
*m = i;
|
|
|
|
}
|
2011-12-16 00:09:28 +00:00
|
|
|
#endif /* CONFIG_RTC */
|
2009-08-11 20:03:59 +00:00
|
|
|
|
2009-07-17 22:28:49 +00:00
|
|
|
static bool check_disk_present(IF_MD_NONVOID(int volume))
|
2008-03-10 20:55:24 +00:00
|
|
|
{
|
2008-10-20 21:49:59 +00:00
|
|
|
#ifdef USB_USE_RAMDISK
|
|
|
|
return true;
|
|
|
|
#else
|
Rewrite filesystem code (WIP)
This patch redoes the filesystem code from the FAT driver up to the
clipboard code in onplay.c.
Not every aspect of this is finished therefore it is still "WIP". I
don't wish to do too much at once (haha!). What is left to do is get
dircache back in the sim and find an implementation for the dircache
indicies in the tagcache and playlist code or do something else that
has the same benefit. Leaving these out for now does not make anything
unusable. All the basics are done.
Phone app code should probably get vetted (and app path handling
just plain rewritten as environment expansions); the SDL app and
Android run well.
Main things addressed:
1) Thread safety: There is none right now in the trunk code. Most of
what currently works is luck when multiple threads are involved or
multiple descriptors to the same file are open.
2) POSIX compliance: Many of the functions behave nothing like their
counterparts on a host system. This leads to inconsistent code or very
different behavior from native to hosted. One huge offender was
rename(). Going point by point would fill a book.
3) Actual running RAM usage: Many targets will use less RAM and less
stack space (some more RAM because I upped the number of cache buffers
for large memory). There's very little memory lying fallow in rarely-used
areas (see 'Key core changes' below). Also, all targets may open the same
number of directory streams whereas before those with less than 8MB RAM
were limited to 8, not 12 implying those targets will save slightly
less.
4) Performance: The test_disk plugin shows markedly improved performance,
particularly in the area of (uncached) directory scanning, due partly to
more optimal directory reading and to a better sector cache algorithm.
Uncached times tend to be better while there is a bit of a slowdown in
dircache due to it being a bit heavier of an implementation. It's not
noticeable by a human as far as I can say.
Key core changes:
1) Files and directories share core code and data structures.
2) The filesystem code knows which descriptors refer to same file.
This ensures that changes from one stream are appropriately reflected
in every open descriptor for that file (fileobj_mgr.c).
3) File and directory cache buffers are borrowed from the main sector
cache. This means that when they are not in use by a file, they are not
wasted, but used for the cache. Most of the time, only a few of them
are needed. It also means that adding more file and directory handles
is less expensive. All one must do in ensure a large enough cache to
borrow from.
4) Relative path components are supported and the namespace is unified.
It does not support full relative paths to an implied current directory;
what is does support is use of "." and "..". Adding the former would
not be very difficult. The namespace is unified in the sense that
volumes may be specified several times along with relative parts, e.g.:
"/<0>/foo/../../<1>/bar" :<=> "/<1>/bar".
5) Stack usage is down due to sharing of data, static allocation and
less duplication of strings on the stack. This requires more
serialization than I would like but since the number of threads is
limited to a low number, the tradoff in favor of the stack seems
reasonable.
6) Separates and heirarchicalizes (sic) the SIM and APP filesystem
code. SIM path and volume handling is just like the target. Some
aspects of the APP file code get more straightforward (e.g. no path
hashing is needed).
Dircache:
Deserves its own section. Dircache is new but pays homage to the old.
The old one was not compatible and so it, since it got redone, does
all the stuff it always should have done such as:
1) It may be update and used at any time during the build process.
No longer has one to wait for it to finish building to do basic file
management (create, remove, rename, etc.).
2) It does not need to be either fully scanned or completely disabled;
it can be incomplete (i.e. overfilled, missing paths), still be
of benefit and be correct.
3) Handles mounting and dismounting of individual volumes which means
a full rebuild is not needed just because you pop a new SD card in the
slot. Now, because it reuses its freed entry data, may rebuild only
that volume.
4) Much more fundamental to the file code. When it is built, it is
the keeper of the master file list whether enabled or not ("disabled"
is just a state of the cache). Its must always to ready to be started
and bind all streams opened prior to being enabled.
5) Maintains any short filenames in OEM format which means that it does
not need to be rebuilt when changing the default codepage.
Miscellaneous Compatibility:
1) Update any other code that would otherwise not work such as the
hotswap mounting code in various card drivers.
2) File management: Clipboard needed updating because of the behavioral
changes. Still needs a little more work on some finer points.
3) Remove now-obsolete functionality such as the mutex's "no preempt"
flag (which was only for the prior FAT driver).
4) struct dirinfo uses time_t rather than raw FAT directory entry
time fields. I plan to follow up on genericizing everything there
(i.e. no FAT attributes).
5) unicode.c needed some redoing so that the file code does not try
try to load codepages during a scan, which is actually a problem with
the current code. The default codepage, if any is required, is now
kept in RAM separarately (bufalloced) from codepages specified to
iso_decode() (which must not be bufalloced because the conversion
may be done by playback threads).
Brings with it some additional reusable core code:
1) Revised file functions: Reusable code that does things such as
safe path concatenation and parsing without buffer limitations or
data duplication. Variants that copy or alter the input path may be
based off these.
To do:
1) Put dircache functionality back in the sim. Treating it internally
as a different kind of file system seems the best approach at this
time.
2) Restore use of dircache indexes in the playlist and database or
something effectively the same. Since the cache doesn't have to be
complete in order to be used, not getting a hit on the cache doesn't
unambiguously say if the path exists or not.
Change-Id: Ia30f3082a136253e3a0eae0784e3091d138915c8
Reviewed-on: http://gerrit.rockbox.org/566
Reviewed-by: Michael Sevakis <jethead71@rockbox.org>
Tested: Michael Sevakis <jethead71@rockbox.org>
2013-08-06 02:02:45 +00:00
|
|
|
return disk_present(IF_MD(volume));
|
2008-10-20 21:49:59 +00:00
|
|
|
#endif
|
2008-03-10 20:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
void usb_storage_notify_hotswap(int volume,bool inserted)
|
|
|
|
{
|
|
|
|
logf("notify %d",inserted);
|
2009-07-17 22:28:49 +00:00
|
|
|
if(inserted && check_disk_present(IF_MD(volume))) {
|
2008-03-10 20:55:24 +00:00
|
|
|
ejected[volume] = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ejected[volume] = true;
|
2009-03-08 18:45:19 +00:00
|
|
|
/* If this happens while the device is locked, weird things may happen.
|
|
|
|
At least try to keep our state consistent */
|
|
|
|
locked[volume]=false;
|
2008-03-10 20:55:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-09-09 16:15:35 +00:00
|
|
|
#ifdef HAVE_MULTIDRIVE
|
|
|
|
void usb_set_skip_first_drive(bool skip)
|
|
|
|
{
|
|
|
|
skip_first = skip;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-28 14:47:18 +00:00
|
|
|
/* called by usb_core_init() */
|
2007-11-22 20:51:00 +00:00
|
|
|
void usb_storage_init(void)
|
|
|
|
{
|
2008-02-11 14:26:25 +00:00
|
|
|
logf("usb_storage_init done");
|
2007-11-22 20:51:00 +00:00
|
|
|
}
|
|
|
|
|
2008-10-03 22:43:16 +00:00
|
|
|
int usb_storage_request_endpoints(struct usb_class_driver *drv)
|
2008-03-06 21:25:09 +00:00
|
|
|
{
|
2009-05-16 15:30:09 +00:00
|
|
|
ep_in = usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_IN, drv);
|
2008-10-03 22:43:16 +00:00
|
|
|
|
2009-04-18 21:32:41 +00:00
|
|
|
if(ep_in<0)
|
2008-10-03 22:43:16 +00:00
|
|
|
return -1;
|
2009-04-18 21:32:41 +00:00
|
|
|
|
2009-05-16 15:30:09 +00:00
|
|
|
ep_out = usb_core_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_OUT,
|
|
|
|
drv);
|
2008-10-03 22:43:16 +00:00
|
|
|
|
2009-04-18 21:32:41 +00:00
|
|
|
if(ep_out<0) {
|
2008-10-03 22:43:16 +00:00
|
|
|
usb_core_release_endpoint(ep_in);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2008-04-26 19:02:16 +00:00
|
|
|
}
|
2008-10-03 22:43:16 +00:00
|
|
|
|
2008-04-26 19:02:16 +00:00
|
|
|
int usb_storage_set_first_interface(int interface)
|
|
|
|
{
|
|
|
|
usb_interface = interface;
|
|
|
|
return interface + 1;
|
|
|
|
}
|
2008-03-06 21:25:09 +00:00
|
|
|
|
2008-04-26 19:02:16 +00:00
|
|
|
int usb_storage_get_config_descriptor(unsigned char *dest,int max_packet_size)
|
|
|
|
{
|
2009-04-18 20:04:52 +00:00
|
|
|
unsigned char *orig_dest = dest;
|
2008-03-06 21:25:09 +00:00
|
|
|
|
2009-04-18 20:04:52 +00:00
|
|
|
interface_descriptor.bInterfaceNumber = usb_interface;
|
2011-12-29 21:58:34 +00:00
|
|
|
PACK_DATA(&dest, interface_descriptor);
|
2009-04-18 20:04:52 +00:00
|
|
|
|
|
|
|
endpoint_descriptor.wMaxPacketSize = max_packet_size;
|
2008-03-06 21:25:09 +00:00
|
|
|
|
2008-10-03 22:43:16 +00:00
|
|
|
endpoint_descriptor.bEndpointAddress = ep_in;
|
2011-12-29 21:58:34 +00:00
|
|
|
PACK_DATA(&dest, endpoint_descriptor);
|
2008-03-06 21:25:09 +00:00
|
|
|
|
2008-10-03 22:43:16 +00:00
|
|
|
endpoint_descriptor.bEndpointAddress = ep_out;
|
2011-12-29 21:58:34 +00:00
|
|
|
PACK_DATA(&dest, endpoint_descriptor);
|
2008-03-06 21:25:09 +00:00
|
|
|
|
2009-04-18 20:04:52 +00:00
|
|
|
return (dest - orig_dest);
|
2008-03-06 21:25:09 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
static int usb_handle;
|
2008-04-26 19:02:16 +00:00
|
|
|
void usb_storage_init_connection(void)
|
2008-03-06 21:25:09 +00:00
|
|
|
{
|
|
|
|
logf("ums: set config");
|
|
|
|
/* prime rx endpoint. We only need room for commands */
|
|
|
|
state = WAITING_FOR_COMMAND;
|
|
|
|
|
2021-04-23 15:09:49 +00:00
|
|
|
#if (CONFIG_CPU == IMX31L || defined(CPU_TCC780X) || \
|
2016-07-31 02:22:03 +00:00
|
|
|
CONFIG_CPU == S5L8702 || CONFIG_CPU == S5L8701 || CONFIG_CPU == AS3525v2 || \
|
2011-01-15 08:19:30 +00:00
|
|
|
defined(BOOTLOADER) || CONFIG_CPU == DM320) && !defined(CPU_PP502x)
|
2010-04-08 19:48:01 +00:00
|
|
|
static unsigned char _cbw_buffer[MAX_CBW_SIZE]
|
2009-05-18 19:45:24 +00:00
|
|
|
USB_DEVBSS_ATTR __attribute__((aligned(32)));
|
|
|
|
cbw_buffer = (void *)_cbw_buffer;
|
|
|
|
|
2009-05-19 18:51:03 +00:00
|
|
|
static unsigned char _transfer_buffer[ALLOCATE_BUFFER_SIZE]
|
2009-01-19 13:41:25 +00:00
|
|
|
USB_DEVBSS_ATTR __attribute__((aligned(32)));
|
2008-04-18 16:42:50 +00:00
|
|
|
tb.transfer_buffer = (void *)_transfer_buffer;
|
2009-01-21 20:52:35 +00:00
|
|
|
#ifdef USB_USE_RAMDISK
|
|
|
|
static unsigned char _ramdisk_buffer[RAMDISK_SIZE*SECTOR_SIZE];
|
|
|
|
ramdisk_buffer = _ramdisk_buffer;
|
|
|
|
#endif
|
2008-04-18 16:42:50 +00:00
|
|
|
#else
|
2013-05-30 09:24:16 +00:00
|
|
|
unsigned char * buffer;
|
|
|
|
/* dummy ops with no callbacks, needed because by
|
|
|
|
* default buflib buffers can be moved around which must be avoided */
|
|
|
|
static struct buflib_callbacks dummy_ops;
|
2008-04-18 16:42:50 +00:00
|
|
|
|
2014-01-04 22:35:00 +00:00
|
|
|
// Add 31 to handle worst-case misalignment
|
|
|
|
usb_handle = core_alloc_ex("usb storage", ALLOCATE_BUFFER_SIZE + MAX_CBW_SIZE + 31, &dummy_ops);
|
2014-01-05 19:56:16 +00:00
|
|
|
if (usb_handle < 0)
|
|
|
|
panicf("%s(): OOM", __func__);
|
2014-01-04 22:35:00 +00:00
|
|
|
|
2013-05-30 09:24:16 +00:00
|
|
|
buffer = core_get_data(usb_handle);
|
2010-06-22 05:46:54 +00:00
|
|
|
#if defined(UNCACHED_ADDR) && CONFIG_CPU != AS3525
|
2013-05-30 09:24:16 +00:00
|
|
|
cbw_buffer = (void *)UNCACHED_ADDR((unsigned int)(buffer+31) & 0xffffffe0);
|
2009-10-20 06:37:07 +00:00
|
|
|
#else
|
2013-05-30 09:24:16 +00:00
|
|
|
cbw_buffer = (void *)((unsigned int)(buffer+31) & 0xffffffe0);
|
2009-10-20 06:37:07 +00:00
|
|
|
#endif
|
2010-04-08 19:48:01 +00:00
|
|
|
tb.transfer_buffer = cbw_buffer + MAX_CBW_SIZE;
|
2011-12-17 07:27:24 +00:00
|
|
|
commit_discard_dcache();
|
2008-10-20 21:49:59 +00:00
|
|
|
#ifdef USB_USE_RAMDISK
|
2009-05-19 18:51:03 +00:00
|
|
|
ramdisk_buffer = tb.transfer_buffer + ALLOCATE_BUFFER_SIZE;
|
2009-01-21 20:52:35 +00:00
|
|
|
#endif
|
2008-04-18 16:42:50 +00:00
|
|
|
#endif
|
2010-04-08 19:48:01 +00:00
|
|
|
usb_drv_recv(ep_out, cbw_buffer, MAX_CBW_SIZE);
|
2009-02-28 14:47:18 +00:00
|
|
|
|
2009-02-28 15:16:54 +00:00
|
|
|
int i;
|
2009-07-17 22:28:49 +00:00
|
|
|
for(i=0;i<storage_num_drives();i++) {
|
2009-03-08 18:45:19 +00:00
|
|
|
locked[i] = false;
|
2009-07-17 22:28:49 +00:00
|
|
|
ejected[i] = !check_disk_present(IF_MD(i));
|
2009-02-28 15:16:54 +00:00
|
|
|
queue_broadcast(SYS_USB_LUN_LOCKED, (i<<16)+0);
|
2009-02-28 14:47:18 +00:00
|
|
|
}
|
2008-03-06 21:25:09 +00:00
|
|
|
}
|
|
|
|
|
2008-12-13 14:49:44 +00:00
|
|
|
void usb_storage_disconnect(void)
|
|
|
|
{
|
2013-05-30 09:24:16 +00:00
|
|
|
if (usb_handle > 0)
|
|
|
|
usb_handle = core_free(usb_handle);
|
2008-12-13 14:49:44 +00:00
|
|
|
}
|
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
/* called by usb_core_transfer_complete() */
|
2008-10-03 22:43:16 +00:00
|
|
|
void usb_storage_transfer_complete(int ep,int dir,int status,int length)
|
2007-11-22 20:51:00 +00:00
|
|
|
{
|
2008-04-26 19:02:16 +00:00
|
|
|
(void)ep;
|
2009-05-18 19:45:24 +00:00
|
|
|
struct command_block_wrapper* cbw = (void*)cbw_buffer;
|
2011-12-16 00:09:28 +00:00
|
|
|
#if CONFIG_RTC
|
2009-08-11 17:54:47 +00:00
|
|
|
struct tm tm;
|
2011-12-16 00:09:28 +00:00
|
|
|
#endif
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2010-01-25 22:33:53 +00:00
|
|
|
logf("transfer result for ep %d/%d %X %d", ep,dir,status, length);
|
2008-02-20 22:54:26 +00:00
|
|
|
switch(state) {
|
|
|
|
case RECEIVING_BLOCKS:
|
2008-10-03 22:43:16 +00:00
|
|
|
if(dir==USB_DIR_IN) {
|
2008-02-20 22:54:26 +00:00
|
|
|
logf("IN received in RECEIVING");
|
|
|
|
}
|
2008-03-06 21:25:09 +00:00
|
|
|
logf("scsi write %d %d", cur_cmd.sector, cur_cmd.count);
|
2008-02-20 22:54:26 +00:00
|
|
|
if(status==0) {
|
2009-04-18 21:32:41 +00:00
|
|
|
if((unsigned int)length!=(SECTOR_SIZE* cur_cmd.count)
|
2009-05-19 18:51:03 +00:00
|
|
|
&& (unsigned int)length!=WRITE_BUFFER_SIZE) {
|
2008-02-20 22:54:26 +00:00
|
|
|
logf("unexpected length :%d",length);
|
2009-06-26 15:14:49 +00:00
|
|
|
break;
|
2008-02-11 14:26:25 +00:00
|
|
|
}
|
2008-02-20 22:54:26 +00:00
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
unsigned int next_sector = cur_cmd.sector +
|
2009-05-19 18:51:03 +00:00
|
|
|
(WRITE_BUFFER_SIZE/SECTOR_SIZE);
|
2008-03-06 21:25:09 +00:00
|
|
|
unsigned int next_count = cur_cmd.count -
|
2009-05-19 18:51:03 +00:00
|
|
|
MIN(cur_cmd.count,WRITE_BUFFER_SIZE/SECTOR_SIZE);
|
2008-07-09 19:56:09 +00:00
|
|
|
int next_select = !cur_cmd.data_select;
|
2008-02-20 22:54:26 +00:00
|
|
|
|
|
|
|
if(next_count!=0) {
|
|
|
|
/* Ask the host to send more, to the other buffer */
|
2008-07-09 19:56:09 +00:00
|
|
|
receive_block_data(cur_cmd.data[next_select],
|
2009-05-19 18:51:03 +00:00
|
|
|
MIN(WRITE_BUFFER_SIZE,next_count*SECTOR_SIZE));
|
2008-02-11 14:26:25 +00:00
|
|
|
}
|
2008-02-20 22:54:26 +00:00
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
/* Now write the data that just came in, while the host is
|
|
|
|
sending the next bit */
|
2008-10-20 21:49:59 +00:00
|
|
|
#ifdef USB_USE_RAMDISK
|
|
|
|
memcpy(ramdisk_buffer + cur_cmd.sector*SECTOR_SIZE,
|
2009-04-18 21:32:41 +00:00
|
|
|
cur_cmd.data[cur_cmd.data_select],
|
2009-05-19 18:51:03 +00:00
|
|
|
MIN(WRITE_BUFFER_SIZE/SECTOR_SIZE, cur_cmd.count)*SECTOR_SIZE);
|
2008-10-20 21:49:59 +00:00
|
|
|
#else
|
2011-01-05 19:35:51 +00:00
|
|
|
int result = USBSTOR_WRITE_SECTORS_FILTER();
|
|
|
|
|
|
|
|
if (result == 0) {
|
2013-08-17 16:18:22 +00:00
|
|
|
result = storage_write_sectors(IF_MD(cur_cmd.lun,)
|
2009-04-18 21:32:41 +00:00
|
|
|
cur_cmd.sector,
|
2009-05-19 18:51:03 +00:00
|
|
|
MIN(WRITE_BUFFER_SIZE/SECTOR_SIZE, cur_cmd.count),
|
2009-04-18 21:32:41 +00:00
|
|
|
cur_cmd.data[cur_cmd.data_select]);
|
2011-01-05 19:35:51 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 22:54:26 +00:00
|
|
|
if(result != 0) {
|
2008-02-28 22:35:23 +00:00
|
|
|
send_csw(UMS_STATUS_FAIL);
|
2008-02-20 22:54:26 +00:00
|
|
|
cur_sense_data.sense_key=SENSE_MEDIUM_ERROR;
|
|
|
|
cur_sense_data.asc=ASC_WRITE_ERROR;
|
2008-03-02 00:15:02 +00:00
|
|
|
cur_sense_data.ascq=0;
|
2008-02-20 22:54:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-04-18 21:32:41 +00:00
|
|
|
#endif
|
2008-02-20 22:54:26 +00:00
|
|
|
if(next_count==0) {
|
2008-02-28 22:35:23 +00:00
|
|
|
send_csw(UMS_STATUS_GOOD);
|
2008-02-20 22:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Switch buffers for the next one */
|
2008-03-06 21:25:09 +00:00
|
|
|
cur_cmd.data_select=!cur_cmd.data_select;
|
2008-02-20 22:54:26 +00:00
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
cur_cmd.sector = next_sector;
|
|
|
|
cur_cmd.count = next_count;
|
2007-11-22 20:51:00 +00:00
|
|
|
}
|
2008-02-20 22:54:26 +00:00
|
|
|
else {
|
|
|
|
logf("Transfer failed %X",status);
|
2008-02-28 22:35:23 +00:00
|
|
|
send_csw(UMS_STATUS_FAIL);
|
2008-02-20 22:54:26 +00:00
|
|
|
/* TODO fill in cur_sense_data */
|
|
|
|
cur_sense_data.sense_key=0;
|
|
|
|
cur_sense_data.information=0;
|
|
|
|
cur_sense_data.asc=0;
|
2008-03-02 00:15:02 +00:00
|
|
|
cur_sense_data.ascq=0;
|
2008-02-11 14:26:25 +00:00
|
|
|
}
|
2007-11-22 20:51:00 +00:00
|
|
|
break;
|
2010-04-08 19:48:01 +00:00
|
|
|
case WAITING_FOR_CSW_COMPLETION_OR_COMMAND:
|
2008-10-03 22:43:16 +00:00
|
|
|
if(dir==USB_DIR_IN) {
|
2010-04-08 19:48:01 +00:00
|
|
|
/* This was the CSW */
|
|
|
|
state = WAITING_FOR_COMMAND;
|
2008-02-28 22:35:23 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-04-08 19:48:01 +00:00
|
|
|
/* This was the command */
|
|
|
|
state = WAITING_FOR_CSW_COMPLETION;
|
|
|
|
/* We now have the CBW, but we won't execute it yet to avoid
|
|
|
|
* issues with the still-pending CSW */
|
2008-02-28 22:35:23 +00:00
|
|
|
}
|
2008-02-20 22:54:26 +00:00
|
|
|
break;
|
2010-04-08 19:48:01 +00:00
|
|
|
case WAITING_FOR_COMMAND:
|
|
|
|
if(dir==USB_DIR_IN) {
|
|
|
|
logf("IN received in WAITING_FOR_COMMAND");
|
|
|
|
}
|
|
|
|
handle_scsi(cbw);
|
|
|
|
break;
|
|
|
|
case WAITING_FOR_CSW_COMPLETION:
|
2008-10-03 22:43:16 +00:00
|
|
|
if(dir==USB_DIR_OUT) {
|
2010-04-08 19:48:01 +00:00
|
|
|
logf("OUT received in WAITING_FOR_CSW_COMPLETION");
|
2008-02-20 22:54:26 +00:00
|
|
|
}
|
2010-04-08 19:48:01 +00:00
|
|
|
handle_scsi(cbw);
|
|
|
|
break;
|
2009-03-11 20:54:32 +00:00
|
|
|
#if 0
|
2009-02-28 19:39:28 +00:00
|
|
|
if(cur_cmd.cur_cmd == SCSI_WRITE_10)
|
|
|
|
{
|
|
|
|
queue_broadcast(SYS_USB_WRITE_DATA, (cur_cmd.lun<<16)+cur_cmd.orig_count);
|
|
|
|
}
|
|
|
|
else if(cur_cmd.cur_cmd == SCSI_READ_10)
|
|
|
|
{
|
|
|
|
queue_broadcast(SYS_USB_READ_DATA, (cur_cmd.lun<<16)+cur_cmd.orig_count);
|
|
|
|
}
|
2009-03-11 20:54:32 +00:00
|
|
|
#endif
|
2008-02-20 22:54:26 +00:00
|
|
|
break;
|
|
|
|
case SENDING_RESULT:
|
2008-10-03 22:43:16 +00:00
|
|
|
if(dir==USB_DIR_OUT) {
|
2008-02-20 22:54:26 +00:00
|
|
|
logf("OUT received in SENDING");
|
|
|
|
}
|
|
|
|
if(status==0) {
|
|
|
|
//logf("data sent, now send csw");
|
2008-02-28 22:35:23 +00:00
|
|
|
send_csw(UMS_STATUS_GOOD);
|
2008-02-20 22:54:26 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
logf("Transfer failed %X",status);
|
2008-02-28 22:35:23 +00:00
|
|
|
send_csw(UMS_STATUS_FAIL);
|
2008-02-20 22:54:26 +00:00
|
|
|
/* TODO fill in cur_sense_data */
|
|
|
|
cur_sense_data.sense_key=0;
|
|
|
|
cur_sense_data.information=0;
|
|
|
|
cur_sense_data.asc=0;
|
2008-03-02 00:15:02 +00:00
|
|
|
cur_sense_data.ascq=0;
|
2008-02-20 22:54:26 +00:00
|
|
|
}
|
|
|
|
break;
|
2008-04-01 20:36:51 +00:00
|
|
|
case SENDING_FAILED_RESULT:
|
2008-10-03 22:43:16 +00:00
|
|
|
if(dir==USB_DIR_OUT) {
|
2008-04-01 20:36:51 +00:00
|
|
|
logf("OUT received in SENDING");
|
|
|
|
}
|
|
|
|
send_csw(UMS_STATUS_FAIL);
|
|
|
|
break;
|
2008-02-20 22:54:26 +00:00
|
|
|
case SENDING_BLOCKS:
|
2008-10-03 22:43:16 +00:00
|
|
|
if(dir==USB_DIR_OUT) {
|
2008-02-20 22:54:26 +00:00
|
|
|
logf("OUT received in SENDING");
|
|
|
|
}
|
|
|
|
if(status==0) {
|
2008-03-06 21:25:09 +00:00
|
|
|
if(cur_cmd.count==0) {
|
2008-02-20 22:54:26 +00:00
|
|
|
//logf("data sent, now send csw");
|
2010-12-04 13:56:19 +00:00
|
|
|
if(cur_cmd.last_result!=0) {
|
|
|
|
/* The last read failed. */
|
|
|
|
send_csw(UMS_STATUS_FAIL);
|
|
|
|
cur_sense_data.sense_key=SENSE_MEDIUM_ERROR;
|
|
|
|
cur_sense_data.asc=ASC_READ_ERROR;
|
|
|
|
cur_sense_data.ascq=0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
send_csw(UMS_STATUS_GOOD);
|
2008-02-20 22:54:26 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
send_and_read_next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logf("Transfer failed %X",status);
|
2008-02-28 22:35:23 +00:00
|
|
|
send_csw(UMS_STATUS_FAIL);
|
2008-02-20 22:54:26 +00:00
|
|
|
/* TODO fill in cur_sense_data */
|
|
|
|
cur_sense_data.sense_key=0;
|
|
|
|
cur_sense_data.information=0;
|
|
|
|
cur_sense_data.asc=0;
|
2008-03-02 00:15:02 +00:00
|
|
|
cur_sense_data.ascq=0;
|
2008-02-11 14:26:25 +00:00
|
|
|
}
|
2007-11-22 20:51:00 +00:00
|
|
|
break;
|
2011-12-16 00:09:28 +00:00
|
|
|
#if CONFIG_RTC
|
2009-08-11 17:54:47 +00:00
|
|
|
case RECEIVING_TIME:
|
2009-08-11 19:30:19 +00:00
|
|
|
tm.tm_year=(tb.transfer_buffer[0]<<8)+tb.transfer_buffer[1] - 1900;
|
2009-08-11 17:54:47 +00:00
|
|
|
tm.tm_yday=(tb.transfer_buffer[2]<<8)+tb.transfer_buffer[3];
|
|
|
|
tm.tm_hour=tb.transfer_buffer[5];
|
|
|
|
tm.tm_min=tb.transfer_buffer[6];
|
|
|
|
tm.tm_sec=tb.transfer_buffer[7];
|
2009-08-11 19:30:19 +00:00
|
|
|
yearday_to_daymonth(tm.tm_yday,tm.tm_year + 1900,&tm.tm_mday,&tm.tm_mon);
|
|
|
|
set_day_of_week(&tm);
|
2009-08-11 17:54:47 +00:00
|
|
|
set_time(&tm);
|
|
|
|
send_csw(UMS_STATUS_GOOD);
|
|
|
|
break;
|
2011-12-16 00:09:28 +00:00
|
|
|
#endif /* CONFIG_RTC */
|
2007-11-22 20:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called by usb_core_control_request() */
|
2009-04-18 20:40:50 +00:00
|
|
|
bool usb_storage_control_request(struct usb_ctrlrequest* req, unsigned char* dest)
|
2007-11-22 20:51:00 +00:00
|
|
|
{
|
|
|
|
bool handled = false;
|
|
|
|
|
2009-04-18 20:40:50 +00:00
|
|
|
(void)dest;
|
2007-11-22 20:51:00 +00:00
|
|
|
switch (req->bRequest) {
|
|
|
|
case USB_BULK_GET_MAX_LUN: {
|
2009-07-17 22:28:49 +00:00
|
|
|
*tb.max_lun = storage_num_drives() - 1;
|
2011-09-09 16:15:35 +00:00
|
|
|
#if defined(HAVE_MULTIDRIVE)
|
|
|
|
if(skip_first) (*tb.max_lun) --;
|
2008-02-20 22:54:26 +00:00
|
|
|
#endif
|
2007-11-22 20:51:00 +00:00
|
|
|
logf("ums: getmaxlun");
|
|
|
|
usb_drv_recv(EP_CONTROL, NULL, 0); /* ack */
|
2009-10-19 16:21:50 +00:00
|
|
|
usb_drv_send(EP_CONTROL, tb.max_lun, 1);
|
2007-11-22 20:51:00 +00:00
|
|
|
handled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case USB_BULK_RESET_REQUEST:
|
|
|
|
logf("ums: bulk reset");
|
2008-02-28 22:35:23 +00:00
|
|
|
state = WAITING_FOR_COMMAND;
|
|
|
|
/* UMS BOT 3.1 says The device shall preserve the value of its bulk
|
2008-03-06 21:25:09 +00:00
|
|
|
data toggle bits and endpoint STALL conditions despite
|
|
|
|
the Bulk-Only Mass Storage Reset. */
|
2008-02-28 22:35:23 +00:00
|
|
|
#if 0
|
2008-10-03 22:43:16 +00:00
|
|
|
usb_drv_reset_endpoint(ep_in, false);
|
|
|
|
usb_drv_reset_endpoint(ep_out, true);
|
2008-02-28 22:35:23 +00:00
|
|
|
#endif
|
2007-11-22 20:51:00 +00:00
|
|
|
usb_drv_send(EP_CONTROL, NULL, 0); /* ack */
|
|
|
|
handled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
}
|
|
|
|
|
2008-02-20 22:54:26 +00:00
|
|
|
static void send_and_read_next(void)
|
|
|
|
{
|
2011-01-05 19:35:51 +00:00
|
|
|
int result = USBSTOR_READ_SECTORS_FILTER();
|
|
|
|
|
|
|
|
if(result != 0 && cur_cmd.last_result == 0)
|
|
|
|
cur_cmd.last_result = result;
|
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
send_block_data(cur_cmd.data[cur_cmd.data_select],
|
2009-05-19 18:51:03 +00:00
|
|
|
MIN(READ_BUFFER_SIZE,cur_cmd.count*SECTOR_SIZE));
|
2008-02-20 22:54:26 +00:00
|
|
|
|
|
|
|
/* Switch buffers for the next one */
|
2008-03-06 21:25:09 +00:00
|
|
|
cur_cmd.data_select=!cur_cmd.data_select;
|
2008-02-20 22:54:26 +00:00
|
|
|
|
2009-05-19 18:51:03 +00:00
|
|
|
cur_cmd.sector+=(READ_BUFFER_SIZE/SECTOR_SIZE);
|
|
|
|
cur_cmd.count-=MIN(cur_cmd.count,READ_BUFFER_SIZE/SECTOR_SIZE);
|
2008-02-20 22:54:26 +00:00
|
|
|
|
2009-04-18 21:32:41 +00:00
|
|
|
if(cur_cmd.count!=0) {
|
2008-02-20 22:54:26 +00:00
|
|
|
/* already read the next bit, so we can send it out immediately when the
|
|
|
|
* current transfer completes. */
|
2008-10-20 21:49:59 +00:00
|
|
|
#ifdef USB_USE_RAMDISK
|
|
|
|
memcpy(cur_cmd.data[cur_cmd.data_select],
|
2009-04-18 21:32:41 +00:00
|
|
|
ramdisk_buffer + cur_cmd.sector*SECTOR_SIZE,
|
2009-05-19 18:51:03 +00:00
|
|
|
MIN(READ_BUFFER_SIZE/SECTOR_SIZE, cur_cmd.count)*SECTOR_SIZE);
|
2008-10-20 21:49:59 +00:00
|
|
|
#else
|
2013-08-17 16:18:22 +00:00
|
|
|
result = storage_read_sectors(IF_MD(cur_cmd.lun,)
|
2009-04-18 21:32:41 +00:00
|
|
|
cur_cmd.sector,
|
2009-05-19 18:51:03 +00:00
|
|
|
MIN(READ_BUFFER_SIZE/SECTOR_SIZE, cur_cmd.count),
|
2009-04-18 21:32:41 +00:00
|
|
|
cur_cmd.data[cur_cmd.data_select]);
|
2010-12-04 13:56:19 +00:00
|
|
|
if(cur_cmd.last_result == 0)
|
|
|
|
cur_cmd.last_result = result;
|
|
|
|
|
2008-10-20 21:49:59 +00:00
|
|
|
#endif
|
2008-02-20 22:54:26 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-22 20:51:00 +00:00
|
|
|
/****************************************************************************/
|
|
|
|
|
2008-02-11 14:26:25 +00:00
|
|
|
static void handle_scsi(struct command_block_wrapper* cbw)
|
2007-11-22 20:51:00 +00:00
|
|
|
{
|
|
|
|
/* USB Mass Storage assumes LBA capability.
|
|
|
|
TODO: support 48-bit LBA */
|
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
struct storage_info info;
|
2007-11-22 20:51:00 +00:00
|
|
|
unsigned int length = cbw->data_transfer_length;
|
2012-05-07 05:30:45 +00:00
|
|
|
unsigned int block_size, block_count;
|
2008-02-20 22:54:26 +00:00
|
|
|
bool lun_present=true;
|
2008-02-11 14:26:25 +00:00
|
|
|
unsigned char lun = cbw->lun;
|
|
|
|
unsigned int block_size_mult = 1;
|
2010-04-08 19:48:01 +00:00
|
|
|
|
|
|
|
if(letoh32(cbw->signature) != CBW_SIGNATURE) {
|
2013-07-24 20:40:12 +00:00
|
|
|
logf("ums: bad cbw signature (%x)", cbw->signature);
|
2010-04-08 19:48:01 +00:00
|
|
|
usb_drv_stall(ep_in, true,true);
|
|
|
|
usb_drv_stall(ep_out, true,false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Clear the signature to prevent possible bugs elsewhere
|
|
|
|
* to trigger a second execution of the same command with
|
|
|
|
* bogus data */
|
|
|
|
cbw->signature=0;
|
|
|
|
|
2011-09-09 16:15:35 +00:00
|
|
|
#if defined(HAVE_MULTIDRIVE)
|
|
|
|
if(skip_first) lun++;
|
2009-07-17 22:28:49 +00:00
|
|
|
#endif
|
|
|
|
|
2008-11-02 01:14:46 +00:00
|
|
|
storage_get_info(lun,&info);
|
2008-10-20 21:49:59 +00:00
|
|
|
#ifdef USB_USE_RAMDISK
|
|
|
|
block_size = SECTOR_SIZE;
|
|
|
|
block_count = RAMDISK_SIZE;
|
|
|
|
#else
|
2008-11-01 16:14:28 +00:00
|
|
|
block_size=info.sector_size;
|
|
|
|
block_count=info.num_sectors;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_HOTSWAP
|
2011-12-15 17:33:30 +00:00
|
|
|
if(storage_removable(lun) && !storage_present(lun)) {
|
2008-03-10 20:55:24 +00:00
|
|
|
ejected[lun] = true;
|
2008-02-11 14:26:25 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-03-03 22:53:25 +00:00
|
|
|
if(ejected[lun])
|
|
|
|
lun_present = false;
|
|
|
|
|
2008-02-11 14:26:25 +00:00
|
|
|
#ifdef MAX_LOG_SECTOR_SIZE
|
2011-12-15 17:33:30 +00:00
|
|
|
block_size_mult = disk_get_sector_multiplier(IF_MD(lun));
|
2008-02-11 14:26:25 +00:00
|
|
|
#endif
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
cur_cmd.tag = cbw->tag;
|
|
|
|
cur_cmd.lun = lun;
|
2009-02-28 19:39:28 +00:00
|
|
|
cur_cmd.cur_cmd = cbw->command_block[0];
|
2008-02-20 22:54:26 +00:00
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
switch (cbw->command_block[0]) {
|
|
|
|
case SCSI_TEST_UNIT_READY:
|
2008-02-11 14:26:25 +00:00
|
|
|
logf("scsi test_unit_ready %d",lun);
|
2009-01-13 16:27:35 +00:00
|
|
|
if(!usb_exclusive_storage()) {
|
2008-03-02 00:15:02 +00:00
|
|
|
send_csw(UMS_STATUS_FAIL);
|
|
|
|
cur_sense_data.sense_key=SENSE_NOT_READY;
|
2008-03-10 20:55:24 +00:00
|
|
|
cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
|
|
|
|
cur_sense_data.ascq=0;
|
2008-03-02 00:15:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-02-28 22:35:23 +00:00
|
|
|
if(lun_present) {
|
|
|
|
send_csw(UMS_STATUS_GOOD);
|
|
|
|
}
|
2008-02-20 22:54:26 +00:00
|
|
|
else {
|
2008-02-28 22:35:23 +00:00
|
|
|
send_csw(UMS_STATUS_FAIL);
|
2008-02-20 22:54:26 +00:00
|
|
|
cur_sense_data.sense_key=SENSE_NOT_READY;
|
|
|
|
cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
|
2008-03-02 00:15:02 +00:00
|
|
|
cur_sense_data.ascq=0;
|
2008-02-20 22:54:26 +00:00
|
|
|
}
|
2007-11-22 20:51:00 +00:00
|
|
|
break;
|
|
|
|
|
2008-02-20 22:54:26 +00:00
|
|
|
case SCSI_REPORT_LUNS: {
|
2008-12-31 17:36:57 +00:00
|
|
|
logf("scsi report luns %d",lun);
|
2013-06-16 09:05:05 +00:00
|
|
|
unsigned int allocation_length=0;
|
2008-11-01 16:14:28 +00:00
|
|
|
int i;
|
2009-07-17 22:28:49 +00:00
|
|
|
unsigned int response_length = 8+8*storage_num_drives();
|
2008-02-20 22:54:26 +00:00
|
|
|
allocation_length|=(cbw->command_block[6]<<24);
|
|
|
|
allocation_length|=(cbw->command_block[7]<<16);
|
|
|
|
allocation_length|=(cbw->command_block[8]<<8);
|
|
|
|
allocation_length|=(cbw->command_block[9]);
|
2008-02-29 21:10:31 +00:00
|
|
|
memset(tb.lun_data,0,sizeof(struct report_lun_data));
|
2009-07-17 22:28:49 +00:00
|
|
|
tb.lun_data->lun_list_length=htobe32(8*storage_num_drives());
|
|
|
|
for(i=0;i<storage_num_drives();i++)
|
2008-11-01 16:14:28 +00:00
|
|
|
{
|
2008-02-20 22:54:26 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2008-11-02 01:14:46 +00:00
|
|
|
if(storage_removable(i))
|
2008-11-01 16:14:28 +00:00
|
|
|
tb.lun_data->luns[i][1]=1;
|
|
|
|
else
|
2008-02-20 22:54:26 +00:00
|
|
|
#endif
|
2008-11-01 16:14:28 +00:00
|
|
|
tb.lun_data->luns[i][1]=0;
|
|
|
|
}
|
2013-06-16 09:05:05 +00:00
|
|
|
length = MIN(length, allocation_length);
|
2008-03-06 21:25:09 +00:00
|
|
|
send_command_result(tb.lun_data,
|
2009-07-17 22:28:49 +00:00
|
|
|
MIN(response_length, length));
|
2008-02-20 22:54:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
case SCSI_INQUIRY:
|
2008-02-11 14:26:25 +00:00
|
|
|
logf("scsi inquiry %d",lun);
|
2009-07-17 22:28:49 +00:00
|
|
|
fill_inquiry(IF_MD(lun));
|
2007-11-22 20:51:00 +00:00
|
|
|
length = MIN(length, cbw->command_block[4]);
|
2008-03-06 21:25:09 +00:00
|
|
|
send_command_result(tb.inquiry,
|
|
|
|
MIN(sizeof(struct inquiry_data), length));
|
2007-11-22 20:51:00 +00:00
|
|
|
break;
|
|
|
|
|
2008-02-11 14:26:25 +00:00
|
|
|
case SCSI_REQUEST_SENSE: {
|
2008-02-29 21:10:31 +00:00
|
|
|
tb.sense_data->ResponseCode=0x70;/*current error*/
|
2008-04-01 20:36:51 +00:00
|
|
|
tb.sense_data->Obsolete=0;
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.sense_data->fei_sensekey=cur_sense_data.sense_key&0x0f;
|
2008-02-29 21:10:31 +00:00
|
|
|
tb.sense_data->Information=cur_sense_data.information;
|
|
|
|
tb.sense_data->AdditionalSenseLength=10;
|
|
|
|
tb.sense_data->CommandSpecificInformation=0;
|
|
|
|
tb.sense_data->AdditionalSenseCode=cur_sense_data.asc;
|
2008-03-02 00:15:02 +00:00
|
|
|
tb.sense_data->AdditionalSenseCodeQualifier=cur_sense_data.ascq;
|
2008-02-29 21:10:31 +00:00
|
|
|
tb.sense_data->FieldReplaceableUnitCode=0;
|
|
|
|
tb.sense_data->SKSV=0;
|
|
|
|
tb.sense_data->SenseKeySpecific=0;
|
2008-02-11 14:26:25 +00:00
|
|
|
logf("scsi request_sense %d",lun);
|
2009-05-11 23:00:56 +00:00
|
|
|
send_command_result(tb.sense_data,
|
|
|
|
MIN(sizeof(struct sense_data), length));
|
2008-02-11 14:26:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-02-20 22:54:26 +00:00
|
|
|
case SCSI_MODE_SENSE_10: {
|
2009-04-18 21:32:41 +00:00
|
|
|
if(!lun_present) {
|
2008-04-01 20:36:51 +00:00
|
|
|
send_command_failed_result();
|
2008-02-28 22:35:23 +00:00
|
|
|
cur_sense_data.sense_key=SENSE_NOT_READY;
|
|
|
|
cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
|
2008-03-02 00:15:02 +00:00
|
|
|
cur_sense_data.ascq=0;
|
2008-02-28 22:35:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-02-20 22:54:26 +00:00
|
|
|
/*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
|
|
|
|
unsigned char page_code = cbw->command_block[2] & 0x3f;
|
|
|
|
logf("scsi mode_sense_10 %d %X",lun,page_code);
|
|
|
|
switch(page_code) {
|
|
|
|
case 0x3f:
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_10->mode_data_length =
|
2009-04-18 21:32:41 +00:00
|
|
|
htobe16(sizeof(struct mode_sense_data_10)-2);
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_10->medium_type = 0;
|
|
|
|
tb.ms_data_10->device_specific = 0;
|
|
|
|
tb.ms_data_10->reserved = 0;
|
|
|
|
tb.ms_data_10->longlba = 1;
|
|
|
|
tb.ms_data_10->block_descriptor_length =
|
|
|
|
htobe16(sizeof(struct mode_sense_bdesc_longlba));
|
|
|
|
|
|
|
|
memset(tb.ms_data_10->block_descriptor.reserved,0,4);
|
|
|
|
memset(tb.ms_data_10->block_descriptor.num_blocks,0,8);
|
|
|
|
|
|
|
|
tb.ms_data_10->block_descriptor.num_blocks[4] =
|
2009-04-18 21:32:41 +00:00
|
|
|
((block_count/block_size_mult) & 0xff000000)>>24;
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_10->block_descriptor.num_blocks[5] =
|
2009-04-18 21:32:41 +00:00
|
|
|
((block_count/block_size_mult) & 0x00ff0000)>>16;
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_10->block_descriptor.num_blocks[6] =
|
2009-04-18 21:32:41 +00:00
|
|
|
((block_count/block_size_mult) & 0x0000ff00)>>8;
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_10->block_descriptor.num_blocks[7] =
|
2009-04-18 21:32:41 +00:00
|
|
|
((block_count/block_size_mult) & 0x000000ff);
|
2008-03-06 21:25:09 +00:00
|
|
|
|
|
|
|
tb.ms_data_10->block_descriptor.block_size[0] =
|
2009-04-18 21:32:41 +00:00
|
|
|
((block_size*block_size_mult) & 0xff000000)>>24;
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_10->block_descriptor.block_size[1] =
|
2009-04-18 21:32:41 +00:00
|
|
|
((block_size*block_size_mult) & 0x00ff0000)>>16;
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_10->block_descriptor.block_size[2] =
|
2009-04-18 21:32:41 +00:00
|
|
|
((block_size*block_size_mult) & 0x0000ff00)>>8;
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_10->block_descriptor.block_size[3] =
|
2009-04-18 21:32:41 +00:00
|
|
|
((block_size*block_size_mult) & 0x000000ff);
|
2008-03-06 21:25:09 +00:00
|
|
|
send_command_result(tb.ms_data_10,
|
2009-04-18 21:32:41 +00:00
|
|
|
MIN(sizeof(struct mode_sense_data_10), length));
|
2008-02-20 22:54:26 +00:00
|
|
|
break;
|
|
|
|
default:
|
2008-04-01 20:36:51 +00:00
|
|
|
send_command_failed_result();
|
2008-02-20 22:54:26 +00:00
|
|
|
cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
|
|
|
|
cur_sense_data.asc=ASC_INVALID_FIELD_IN_CBD;
|
2008-03-02 00:15:02 +00:00
|
|
|
cur_sense_data.ascq=0;
|
2008-02-20 22:54:26 +00:00
|
|
|
break;
|
2009-04-18 21:32:41 +00:00
|
|
|
}
|
2008-02-20 22:54:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-04-18 21:32:41 +00:00
|
|
|
|
2008-02-20 22:54:26 +00:00
|
|
|
case SCSI_MODE_SENSE_6: {
|
2009-04-18 21:32:41 +00:00
|
|
|
if(!lun_present) {
|
2008-04-01 20:36:51 +00:00
|
|
|
send_command_failed_result();
|
2008-02-28 22:35:23 +00:00
|
|
|
cur_sense_data.sense_key=SENSE_NOT_READY;
|
|
|
|
cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
|
2008-03-02 00:15:02 +00:00
|
|
|
cur_sense_data.ascq=0;
|
2008-02-28 22:35:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-02-20 22:54:26 +00:00
|
|
|
/*unsigned char pc = (cbw->command_block[2] & 0xc0) >>6;*/
|
|
|
|
unsigned char page_code = cbw->command_block[2] & 0x3f;
|
|
|
|
logf("scsi mode_sense_6 %d %X",lun,page_code);
|
|
|
|
switch(page_code) {
|
|
|
|
case 0x3f:
|
2008-03-06 21:25:09 +00:00
|
|
|
/* All supported pages. */
|
|
|
|
tb.ms_data_6->mode_data_length =
|
2009-04-18 21:32:41 +00:00
|
|
|
sizeof(struct mode_sense_data_6)-1;
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_6->medium_type = 0;
|
|
|
|
tb.ms_data_6->device_specific = 0;
|
|
|
|
tb.ms_data_6->block_descriptor_length =
|
2009-04-18 21:32:41 +00:00
|
|
|
sizeof(struct mode_sense_bdesc_shortlba);
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_6->block_descriptor.density_code = 0;
|
|
|
|
tb.ms_data_6->block_descriptor.reserved = 0;
|
2009-04-18 21:32:41 +00:00
|
|
|
if(block_count/block_size_mult > 0xffffff) {
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_6->block_descriptor.num_blocks[0] = 0xff;
|
|
|
|
tb.ms_data_6->block_descriptor.num_blocks[1] = 0xff;
|
|
|
|
tb.ms_data_6->block_descriptor.num_blocks[2] = 0xff;
|
2008-02-29 19:25:14 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_6->block_descriptor.num_blocks[0] =
|
2009-04-18 21:32:41 +00:00
|
|
|
((block_count/block_size_mult) & 0xff0000)>>16;
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_6->block_descriptor.num_blocks[1] =
|
2009-04-18 21:32:41 +00:00
|
|
|
((block_count/block_size_mult) & 0x00ff00)>>8;
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_6->block_descriptor.num_blocks[2] =
|
2009-04-18 21:32:41 +00:00
|
|
|
((block_count/block_size_mult) & 0x0000ff);
|
2008-02-29 19:25:14 +00:00
|
|
|
}
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_6->block_descriptor.block_size[0] =
|
2009-04-18 21:32:41 +00:00
|
|
|
((block_size*block_size_mult) & 0xff0000)>>16;
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_6->block_descriptor.block_size[1] =
|
2009-04-18 21:32:41 +00:00
|
|
|
((block_size*block_size_mult) & 0x00ff00)>>8;
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.ms_data_6->block_descriptor.block_size[2] =
|
2009-04-18 21:32:41 +00:00
|
|
|
((block_size*block_size_mult) & 0x0000ff);
|
2008-03-06 21:25:09 +00:00
|
|
|
send_command_result(tb.ms_data_6,
|
2009-04-18 21:32:41 +00:00
|
|
|
MIN(sizeof(struct mode_sense_data_6), length));
|
2008-02-20 22:54:26 +00:00
|
|
|
break;
|
|
|
|
default:
|
2008-04-01 20:36:51 +00:00
|
|
|
send_command_failed_result();
|
2008-02-20 22:54:26 +00:00
|
|
|
cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
|
|
|
|
cur_sense_data.asc=ASC_INVALID_FIELD_IN_CBD;
|
2008-03-02 00:15:02 +00:00
|
|
|
cur_sense_data.ascq=0;
|
2008-02-20 22:54:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-11-22 20:51:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-02-11 14:26:25 +00:00
|
|
|
case SCSI_START_STOP_UNIT:
|
|
|
|
logf("scsi start_stop unit %d",lun);
|
2008-03-10 20:55:24 +00:00
|
|
|
if((cbw->command_block[4] & 0xf0) == 0) /*load/eject bit is valid*/
|
2008-03-06 21:25:09 +00:00
|
|
|
{ /* Process start and eject bits */
|
2008-03-10 20:55:24 +00:00
|
|
|
logf("scsi load/eject");
|
|
|
|
if((cbw->command_block[4] & 0x01) == 0) /* Don't start */
|
2008-03-03 22:53:25 +00:00
|
|
|
{
|
2008-03-10 20:55:24 +00:00
|
|
|
if((cbw->command_block[4] & 0x02) != 0) /* eject */
|
|
|
|
{
|
|
|
|
logf("scsi eject");
|
|
|
|
ejected[lun]=true;
|
|
|
|
}
|
2008-03-03 22:53:25 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-28 22:35:23 +00:00
|
|
|
send_csw(UMS_STATUS_GOOD);
|
2008-02-11 14:26:25 +00:00
|
|
|
break;
|
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
case SCSI_ALLOW_MEDIUM_REMOVAL:
|
2008-02-11 14:26:25 +00:00
|
|
|
logf("scsi allow_medium_removal %d",lun);
|
2009-04-18 21:32:41 +00:00
|
|
|
if((cbw->command_block[4] & 0x03) == 0) {
|
2009-03-08 18:45:19 +00:00
|
|
|
locked[lun]=false;
|
2009-02-28 14:47:18 +00:00
|
|
|
queue_broadcast(SYS_USB_LUN_LOCKED, (lun<<16)+0);
|
|
|
|
}
|
2009-04-18 21:32:41 +00:00
|
|
|
else {
|
2009-03-08 18:45:19 +00:00
|
|
|
locked[lun]=true;
|
2009-02-28 14:47:18 +00:00
|
|
|
queue_broadcast(SYS_USB_LUN_LOCKED, (lun<<16)+1);
|
|
|
|
}
|
2008-02-28 22:35:23 +00:00
|
|
|
send_csw(UMS_STATUS_GOOD);
|
2008-02-11 14:26:25 +00:00
|
|
|
break;
|
2009-04-18 21:32:41 +00:00
|
|
|
|
2008-02-11 14:26:25 +00:00
|
|
|
case SCSI_READ_FORMAT_CAPACITY: {
|
|
|
|
logf("scsi read_format_capacity %d",lun);
|
2008-02-28 22:35:23 +00:00
|
|
|
if(lun_present) {
|
2008-02-29 21:10:31 +00:00
|
|
|
tb.format_capacity_data->following_length=htobe32(8);
|
2008-03-06 21:25:09 +00:00
|
|
|
/* "block count" actually means "number of last block" */
|
|
|
|
tb.format_capacity_data->block_count =
|
2009-04-18 21:32:41 +00:00
|
|
|
htobe32(block_count/block_size_mult - 1);
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.format_capacity_data->block_size =
|
2009-04-18 21:32:41 +00:00
|
|
|
htobe32(block_size*block_size_mult);
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.format_capacity_data->block_size |=
|
2009-04-18 21:32:41 +00:00
|
|
|
htobe32(SCSI_FORMAT_CAPACITY_FORMATTED_MEDIA);
|
2008-02-28 22:35:23 +00:00
|
|
|
|
2008-02-29 21:10:31 +00:00
|
|
|
send_command_result(tb.format_capacity_data,
|
2009-04-18 21:32:41 +00:00
|
|
|
MIN(sizeof(struct format_capacity), length));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
send_command_failed_result();
|
|
|
|
cur_sense_data.sense_key=SENSE_NOT_READY;
|
|
|
|
cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
|
|
|
|
cur_sense_data.ascq=0;
|
|
|
|
}
|
|
|
|
break;
|
2008-02-11 14:26:25 +00:00
|
|
|
}
|
2009-04-18 21:32:41 +00:00
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
case SCSI_READ_CAPACITY: {
|
2008-02-11 14:26:25 +00:00
|
|
|
logf("scsi read_capacity %d",lun);
|
2008-02-20 22:54:26 +00:00
|
|
|
|
2008-02-28 22:35:23 +00:00
|
|
|
if(lun_present) {
|
2008-03-06 21:25:09 +00:00
|
|
|
/* "block count" actually means "number of last block" */
|
|
|
|
tb.capacity_data->block_count =
|
2009-04-18 21:32:41 +00:00
|
|
|
htobe32(block_count/block_size_mult - 1);
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.capacity_data->block_size =
|
2009-04-18 21:32:41 +00:00
|
|
|
htobe32(block_size*block_size_mult);
|
2008-03-06 21:25:09 +00:00
|
|
|
|
|
|
|
send_command_result(tb.capacity_data,
|
2009-04-18 21:32:41 +00:00
|
|
|
MIN(sizeof(struct capacity), length));
|
2008-02-28 22:35:23 +00:00
|
|
|
}
|
2009-04-18 21:32:41 +00:00
|
|
|
else {
|
2008-04-01 20:36:51 +00:00
|
|
|
send_command_failed_result();
|
2008-02-28 22:35:23 +00:00
|
|
|
cur_sense_data.sense_key=SENSE_NOT_READY;
|
|
|
|
cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
|
2008-03-02 00:15:02 +00:00
|
|
|
cur_sense_data.ascq=0;
|
2008-02-28 22:35:23 +00:00
|
|
|
}
|
2007-11-22 20:51:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SCSI_READ_10:
|
2008-02-20 22:54:26 +00:00
|
|
|
logf("scsi read10 %d",lun);
|
2009-04-18 21:32:41 +00:00
|
|
|
if(!lun_present) {
|
2008-04-01 20:36:51 +00:00
|
|
|
send_command_failed_result();
|
2008-02-20 22:54:26 +00:00
|
|
|
cur_sense_data.sense_key=SENSE_NOT_READY;
|
|
|
|
cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
|
2008-03-02 00:15:02 +00:00
|
|
|
cur_sense_data.ascq=0;
|
2008-02-20 22:54:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-08-31 15:11:04 +00:00
|
|
|
cur_cmd.data[0] = tb.transfer_buffer;
|
2009-05-19 18:51:03 +00:00
|
|
|
cur_cmd.data[1] = &tb.transfer_buffer[READ_BUFFER_SIZE];
|
2008-03-06 21:25:09 +00:00
|
|
|
cur_cmd.data_select=0;
|
|
|
|
cur_cmd.sector = block_size_mult *
|
2009-04-18 21:32:41 +00:00
|
|
|
(cbw->command_block[2] << 24 |
|
|
|
|
cbw->command_block[3] << 16 |
|
|
|
|
cbw->command_block[4] << 8 |
|
|
|
|
cbw->command_block[5] );
|
2008-03-06 21:25:09 +00:00
|
|
|
cur_cmd.count = block_size_mult *
|
2009-04-18 21:32:41 +00:00
|
|
|
(cbw->command_block[7] << 8 |
|
|
|
|
cbw->command_block[8]);
|
2009-02-28 19:39:28 +00:00
|
|
|
cur_cmd.orig_count = cur_cmd.count;
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
//logf("scsi read %d %d", cur_cmd.sector, cur_cmd.count);
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2008-03-06 21:25:09 +00:00
|
|
|
if((cur_cmd.sector + cur_cmd.count) > block_count) {
|
2008-02-28 22:35:23 +00:00
|
|
|
send_csw(UMS_STATUS_FAIL);
|
2008-02-20 22:54:26 +00:00
|
|
|
cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
|
|
|
|
cur_sense_data.asc=ASC_LBA_OUT_OF_RANGE;
|
2008-03-02 00:15:02 +00:00
|
|
|
cur_sense_data.ascq=0;
|
2008-02-11 14:26:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-10-20 21:49:59 +00:00
|
|
|
#ifdef USB_USE_RAMDISK
|
|
|
|
memcpy(cur_cmd.data[cur_cmd.data_select],
|
2009-04-18 21:32:41 +00:00
|
|
|
ramdisk_buffer + cur_cmd.sector*SECTOR_SIZE,
|
2009-05-19 18:51:03 +00:00
|
|
|
MIN(READ_BUFFER_SIZE/SECTOR_SIZE,cur_cmd.count)*SECTOR_SIZE);
|
2008-10-20 21:49:59 +00:00
|
|
|
#else
|
2013-08-17 16:18:22 +00:00
|
|
|
cur_cmd.last_result = storage_read_sectors(IF_MD(cur_cmd.lun,)
|
2009-04-18 21:32:41 +00:00
|
|
|
cur_cmd.sector,
|
2009-05-19 18:51:03 +00:00
|
|
|
MIN(READ_BUFFER_SIZE/SECTOR_SIZE, cur_cmd.count),
|
2009-04-18 21:32:41 +00:00
|
|
|
cur_cmd.data[cur_cmd.data_select]);
|
2008-10-20 21:49:59 +00:00
|
|
|
#endif
|
2008-02-20 22:54:26 +00:00
|
|
|
send_and_read_next();
|
2008-02-11 14:26:25 +00:00
|
|
|
}
|
2007-11-22 20:51:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SCSI_WRITE_10:
|
2008-02-20 22:54:26 +00:00
|
|
|
logf("scsi write10 %d",lun);
|
2009-04-18 21:32:41 +00:00
|
|
|
if(!lun_present) {
|
2008-02-28 22:35:23 +00:00
|
|
|
send_csw(UMS_STATUS_FAIL);
|
2008-02-20 22:54:26 +00:00
|
|
|
cur_sense_data.sense_key=SENSE_NOT_READY;
|
|
|
|
cur_sense_data.asc=ASC_MEDIUM_NOT_PRESENT;
|
2008-03-02 00:15:02 +00:00
|
|
|
cur_sense_data.ascq=0;
|
2008-02-20 22:54:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-08-31 15:11:04 +00:00
|
|
|
cur_cmd.data[0] = tb.transfer_buffer;
|
2009-05-19 18:51:03 +00:00
|
|
|
cur_cmd.data[1] = &tb.transfer_buffer[WRITE_BUFFER_SIZE];
|
2008-03-06 21:25:09 +00:00
|
|
|
cur_cmd.data_select=0;
|
|
|
|
cur_cmd.sector = block_size_mult *
|
2009-04-18 21:32:41 +00:00
|
|
|
(cbw->command_block[2] << 24 |
|
|
|
|
cbw->command_block[3] << 16 |
|
|
|
|
cbw->command_block[4] << 8 |
|
|
|
|
cbw->command_block[5] );
|
2008-03-06 21:25:09 +00:00
|
|
|
cur_cmd.count = block_size_mult *
|
2009-04-18 21:32:41 +00:00
|
|
|
(cbw->command_block[7] << 8 |
|
|
|
|
cbw->command_block[8]);
|
2009-02-28 19:39:28 +00:00
|
|
|
cur_cmd.orig_count = cur_cmd.count;
|
2009-02-28 16:25:07 +00:00
|
|
|
|
2008-02-11 14:26:25 +00:00
|
|
|
/* expect data */
|
2008-03-06 21:25:09 +00:00
|
|
|
if((cur_cmd.sector + cur_cmd.count) > block_count) {
|
2008-02-28 22:35:23 +00:00
|
|
|
send_csw(UMS_STATUS_FAIL);
|
2008-02-20 22:54:26 +00:00
|
|
|
cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
|
|
|
|
cur_sense_data.asc=ASC_LBA_OUT_OF_RANGE;
|
2008-03-02 00:15:02 +00:00
|
|
|
cur_sense_data.ascq=0;
|
2008-02-11 14:26:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-03-06 21:25:09 +00:00
|
|
|
receive_block_data(cur_cmd.data[0],
|
2009-05-19 18:51:03 +00:00
|
|
|
MIN(WRITE_BUFFER_SIZE, cur_cmd.count*SECTOR_SIZE));
|
2008-02-11 14:26:25 +00:00
|
|
|
}
|
2007-11-22 20:51:00 +00:00
|
|
|
break;
|
|
|
|
|
2011-12-16 00:09:28 +00:00
|
|
|
#if CONFIG_RTC
|
2009-08-11 17:54:47 +00:00
|
|
|
case SCSI_WRITE_BUFFER:
|
2009-08-11 19:30:19 +00:00
|
|
|
if(cbw->command_block[1]==1 /* mode = vendor specific */
|
|
|
|
&& cbw->command_block[2]==0 /* buffer id = 0 */
|
|
|
|
|
|
|
|
&& cbw->command_block[3]==0x0c /* offset (3 bytes) */
|
2009-08-11 17:54:47 +00:00
|
|
|
&& cbw->command_block[4]==0
|
|
|
|
&& cbw->command_block[5]==0
|
2009-08-11 19:30:19 +00:00
|
|
|
|
|
|
|
/* Some versions of itunes set the parameter list length to 0.
|
|
|
|
* Technically it should be 0x0c, which is what libgpod sends */
|
|
|
|
&& cbw->command_block[6]==0 /* parameter list (3 bytes) */
|
2009-08-11 17:54:47 +00:00
|
|
|
&& cbw->command_block[7]==0
|
|
|
|
&& (cbw->command_block[8]==0 || cbw->command_block[8]==0x0c)
|
2009-08-11 19:30:19 +00:00
|
|
|
|
2009-08-11 17:54:47 +00:00
|
|
|
&& cbw->command_block[9]==0)
|
|
|
|
receive_time();
|
|
|
|
break;
|
2011-12-16 00:09:28 +00:00
|
|
|
#endif /* CONFIG_RTC */
|
2009-08-11 17:54:47 +00:00
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
default:
|
2008-02-11 14:26:25 +00:00
|
|
|
logf("scsi unknown cmd %x",cbw->command_block[0x0]);
|
2008-02-28 22:35:23 +00:00
|
|
|
send_csw(UMS_STATUS_FAIL);
|
2009-01-12 21:22:26 +00:00
|
|
|
cur_sense_data.sense_key=SENSE_ILLEGAL_REQUEST;
|
|
|
|
cur_sense_data.asc=ASC_INVALID_COMMAND;
|
|
|
|
cur_sense_data.ascq=0;
|
2007-11-22 20:51:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 22:54:26 +00:00
|
|
|
static void send_block_data(void *data,int size)
|
|
|
|
{
|
2008-10-03 22:43:16 +00:00
|
|
|
usb_drv_send_nonblocking(ep_in, data,size);
|
2008-02-20 22:54:26 +00:00
|
|
|
state = SENDING_BLOCKS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void send_command_result(void *data,int size)
|
|
|
|
{
|
2008-10-03 22:43:16 +00:00
|
|
|
usb_drv_send_nonblocking(ep_in, data,size);
|
2008-02-20 22:54:26 +00:00
|
|
|
state = SENDING_RESULT;
|
|
|
|
}
|
|
|
|
|
2008-04-01 20:36:51 +00:00
|
|
|
static void send_command_failed_result(void)
|
|
|
|
{
|
2008-10-03 22:43:16 +00:00
|
|
|
usb_drv_send_nonblocking(ep_in, NULL, 0);
|
2008-04-01 20:36:51 +00:00
|
|
|
state = SENDING_FAILED_RESULT;
|
|
|
|
}
|
|
|
|
|
2011-12-16 00:09:28 +00:00
|
|
|
#if CONFIG_RTC
|
2009-08-11 17:54:47 +00:00
|
|
|
static void receive_time(void)
|
|
|
|
{
|
|
|
|
usb_drv_recv(ep_out, tb.transfer_buffer, 12);
|
|
|
|
state = RECEIVING_TIME;
|
|
|
|
}
|
2011-12-16 00:09:28 +00:00
|
|
|
#endif /* CONFIG_RTC */
|
|
|
|
|
2008-02-20 22:54:26 +00:00
|
|
|
static void receive_block_data(void *data,int size)
|
2007-11-22 20:51:00 +00:00
|
|
|
{
|
2008-10-03 22:43:16 +00:00
|
|
|
usb_drv_recv(ep_out, data, size);
|
2008-02-20 22:54:26 +00:00
|
|
|
state = RECEIVING_BLOCKS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void send_csw(int status)
|
|
|
|
{
|
2008-02-29 21:10:31 +00:00
|
|
|
tb.csw->signature = htole32(CSW_SIGNATURE);
|
2008-03-06 21:25:09 +00:00
|
|
|
tb.csw->tag = cur_cmd.tag;
|
2008-02-29 21:10:31 +00:00
|
|
|
tb.csw->data_residue = 0;
|
|
|
|
tb.csw->status = status;
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2008-10-03 22:43:16 +00:00
|
|
|
usb_drv_send_nonblocking(ep_in, tb.csw,
|
2009-04-18 21:32:41 +00:00
|
|
|
sizeof(struct command_status_wrapper));
|
2010-04-08 19:48:01 +00:00
|
|
|
state = WAITING_FOR_CSW_COMPLETION_OR_COMMAND;
|
2008-02-28 22:35:23 +00:00
|
|
|
//logf("CSW: %X",status);
|
2010-04-08 19:48:01 +00:00
|
|
|
/* Already start waiting for the next command */
|
|
|
|
usb_drv_recv(ep_out, cbw_buffer, MAX_CBW_SIZE);
|
|
|
|
/* The next completed transfer will be either the CSW one
|
|
|
|
* or the new command */
|
2008-02-20 22:54:26 +00:00
|
|
|
|
2008-02-28 22:35:23 +00:00
|
|
|
if(status == UMS_STATUS_GOOD) {
|
2008-02-20 22:54:26 +00:00
|
|
|
cur_sense_data.sense_key=0;
|
|
|
|
cur_sense_data.information=0;
|
|
|
|
cur_sense_data.asc=0;
|
2008-03-02 00:15:02 +00:00
|
|
|
cur_sense_data.ascq=0;
|
2008-02-20 22:54:26 +00:00
|
|
|
}
|
2007-11-22 20:51:00 +00:00
|
|
|
}
|
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
static void copy_padded(char *dest, char *src, int len)
|
2007-11-22 20:51:00 +00:00
|
|
|
{
|
2012-05-07 05:19:15 +00:00
|
|
|
for (int i = 0; i < len; i++) {
|
2012-05-07 16:54:11 +00:00
|
|
|
if (src[i] == '\0') {
|
2012-05-07 05:19:15 +00:00
|
|
|
memset(&dest[i], ' ', len - i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dest[i] = src[i];
|
|
|
|
}
|
2008-11-01 16:14:28 +00:00
|
|
|
}
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2008-11-01 16:14:28 +00:00
|
|
|
/* build SCSI INQUIRY */
|
2009-07-17 22:28:49 +00:00
|
|
|
static void fill_inquiry(IF_MD_NONVOID(int lun))
|
2008-11-01 16:14:28 +00:00
|
|
|
{
|
|
|
|
struct storage_info info;
|
2009-04-18 21:32:41 +00:00
|
|
|
memset(tb.inquiry, 0, sizeof(struct inquiry_data));
|
2008-11-02 01:14:46 +00:00
|
|
|
storage_get_info(lun,&info);
|
2008-11-01 16:14:28 +00:00
|
|
|
copy_padded(tb.inquiry->VendorId,info.vendor,sizeof(tb.inquiry->VendorId));
|
|
|
|
copy_padded(tb.inquiry->ProductId,info.product,sizeof(tb.inquiry->ProductId));
|
|
|
|
copy_padded(tb.inquiry->ProductRevisionLevel,info.revision,sizeof(tb.inquiry->ProductRevisionLevel));
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2008-02-29 21:10:31 +00:00
|
|
|
tb.inquiry->DeviceType = DIRECT_ACCESS_DEVICE;
|
|
|
|
tb.inquiry->AdditionalLength = 0x1f;
|
2021-07-23 00:21:52 +00:00
|
|
|
// memset(tb.inquiry->Reserved, 0, sizeof(tb.inquiry->Reserved)); // Redundant
|
2008-02-29 21:10:31 +00:00
|
|
|
tb.inquiry->Versions = 4; /* SPC-2 */
|
2009-04-18 21:32:41 +00:00
|
|
|
tb.inquiry->Format = 2; /* SPC-2/3 inquiry format */
|
2008-02-11 14:26:25 +00:00
|
|
|
|
2008-03-03 22:19:38 +00:00
|
|
|
tb.inquiry->DeviceTypeModifier = DEVICE_REMOVABLE;
|
2007-11-22 20:51:00 +00:00
|
|
|
}
|