2007-03-18 14:17:28 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2011-02-25 01:05:47 +03:00
|
|
|
// $Id$
|
2007-03-18 14:17:28 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2012-09-02 13:37:47 +04:00
|
|
|
// USB mass storage device support (ported from QEMU)
|
2012-08-31 00:41:25 +04:00
|
|
|
//
|
|
|
|
// Copyright (c) 2006 CodeSourcery.
|
|
|
|
// Written by Paul Brook
|
2015-07-12 17:45:34 +03:00
|
|
|
// Copyright (C) 2009-2015 The Bochs Project
|
2007-03-18 14:17:28 +03:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
2009-02-08 12:05:52 +03:00
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2009-02-08 00:05:31 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2007-03-18 14:17:28 +03:00
|
|
|
|
|
|
|
// Define BX_PLUGGABLE in files that can be compiled into plugins. For
|
2008-01-27 01:24:03 +03:00
|
|
|
// platforms that require a special tag on exported symbols, BX_PLUGGABLE
|
2007-03-18 14:17:28 +03:00
|
|
|
// is used to know when we are exporting symbols and when we are importing.
|
|
|
|
#define BX_PLUGGABLE
|
|
|
|
|
|
|
|
#include "iodev.h"
|
2009-01-18 16:11:27 +03:00
|
|
|
|
2007-03-18 14:17:28 +03:00
|
|
|
#if BX_SUPPORT_PCI && BX_SUPPORT_PCIUSB
|
2009-01-18 16:11:27 +03:00
|
|
|
#include "usb_common.h"
|
2012-08-05 22:13:38 +04:00
|
|
|
#include "hdimage/cdrom.h"
|
|
|
|
#include "hdimage/hdimage.h"
|
2007-03-18 14:17:28 +03:00
|
|
|
#include "scsi_device.h"
|
|
|
|
#include "usb_msd.h"
|
|
|
|
|
|
|
|
#define LOG_THIS
|
|
|
|
|
2007-04-09 13:04:54 +04:00
|
|
|
enum USBMSDMode {
|
|
|
|
USB_MSDM_CBW,
|
|
|
|
USB_MSDM_DATAOUT,
|
|
|
|
USB_MSDM_DATAIN,
|
|
|
|
USB_MSDM_CSW
|
|
|
|
};
|
|
|
|
|
2007-03-18 14:17:28 +03:00
|
|
|
struct usb_msd_cbw {
|
|
|
|
Bit32u sig;
|
|
|
|
Bit32u tag;
|
|
|
|
Bit32u data_len;
|
|
|
|
Bit8u flags;
|
|
|
|
Bit8u lun;
|
|
|
|
Bit8u cmd_len;
|
|
|
|
Bit8u cmd[16];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct usb_msd_csw {
|
|
|
|
Bit32u sig;
|
|
|
|
Bit32u tag;
|
|
|
|
Bit32u residue;
|
|
|
|
Bit8u status;
|
|
|
|
};
|
|
|
|
|
|
|
|
// USB requests
|
|
|
|
#define MassStorageReset 0xff
|
|
|
|
#define GetMaxLun 0xfe
|
|
|
|
|
2014-09-22 23:49:39 +04:00
|
|
|
// Low-, Full-, and High-speed
|
2007-03-18 14:17:28 +03:00
|
|
|
static const Bit8u bx_msd_dev_descriptor[] = {
|
|
|
|
0x12, /* u8 bLength; */
|
|
|
|
0x01, /* u8 bDescriptorType; Device */
|
|
|
|
0x00, 0x02, /* u16 bcdUSB; v2.0 */
|
|
|
|
|
|
|
|
0x00, /* u8 bDeviceClass; */
|
|
|
|
0x00, /* u8 bDeviceSubClass; */
|
|
|
|
0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */
|
|
|
|
0x40, /* u8 bMaxPacketSize0; 64 Bytes */
|
|
|
|
|
|
|
|
/* Vendor and product id are arbitrary. */
|
|
|
|
0x00, 0x00, /* u16 idVendor; */
|
|
|
|
0x00, 0x00, /* u16 idProduct; */
|
|
|
|
0x00, 0x01, /* u16 bcdDevice */
|
|
|
|
|
|
|
|
0x01, /* u8 iManufacturer; */
|
|
|
|
0x02, /* u8 iProduct; */
|
|
|
|
0x03, /* u8 iSerialNumber; */
|
|
|
|
0x01 /* u8 bNumConfigurations; */
|
|
|
|
};
|
|
|
|
|
2014-09-22 23:49:39 +04:00
|
|
|
// Low-, Full-, and High-speed
|
2007-03-18 14:17:28 +03:00
|
|
|
static const Bit8u bx_msd_config_descriptor[] = {
|
|
|
|
|
|
|
|
/* one configuration */
|
|
|
|
0x09, /* u8 bLength; */
|
|
|
|
0x02, /* u8 bDescriptorType; Configuration */
|
|
|
|
0x20, 0x00, /* u16 wTotalLength; */
|
|
|
|
0x01, /* u8 bNumInterfaces; (1) */
|
|
|
|
0x01, /* u8 bConfigurationValue; */
|
|
|
|
0x00, /* u8 iConfiguration; */
|
2008-01-27 01:24:03 +03:00
|
|
|
0xc0, /* u8 bmAttributes;
|
2007-03-18 14:17:28 +03:00
|
|
|
Bit 7: must be set,
|
|
|
|
6: Self-powered,
|
|
|
|
5: Remote wakeup,
|
|
|
|
4..0: resvd */
|
|
|
|
0x00, /* u8 MaxPower; */
|
|
|
|
|
|
|
|
/* one interface */
|
|
|
|
0x09, /* u8 if_bLength; */
|
|
|
|
0x04, /* u8 if_bDescriptorType; Interface */
|
|
|
|
0x00, /* u8 if_bInterfaceNumber; */
|
|
|
|
0x00, /* u8 if_bAlternateSetting; */
|
|
|
|
0x02, /* u8 if_bNumEndpoints; */
|
|
|
|
0x08, /* u8 if_bInterfaceClass; MASS STORAGE */
|
|
|
|
0x06, /* u8 if_bInterfaceSubClass; SCSI */
|
|
|
|
0x50, /* u8 if_bInterfaceProtocol; Bulk Only */
|
|
|
|
0x00, /* u8 if_iInterface; */
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2007-03-18 14:17:28 +03:00
|
|
|
/* Bulk-In endpoint */
|
|
|
|
0x07, /* u8 ep_bLength; */
|
|
|
|
0x05, /* u8 ep_bDescriptorType; Endpoint */
|
|
|
|
0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
|
|
|
|
0x02, /* u8 ep_bmAttributes; Bulk */
|
|
|
|
0x40, 0x00, /* u16 ep_wMaxPacketSize; */
|
|
|
|
0x00, /* u8 ep_bInterval; */
|
|
|
|
|
|
|
|
/* Bulk-Out endpoint */
|
|
|
|
0x07, /* u8 ep_bLength; */
|
|
|
|
0x05, /* u8 ep_bDescriptorType; Endpoint */
|
|
|
|
0x02, /* u8 ep_bEndpointAddress; OUT Endpoint 2 */
|
|
|
|
0x02, /* u8 ep_bmAttributes; Bulk */
|
|
|
|
0x40, 0x00, /* u16 ep_wMaxPacketSize; */
|
|
|
|
0x00 /* u8 ep_bInterval; */
|
|
|
|
};
|
|
|
|
|
2014-09-22 23:49:39 +04:00
|
|
|
// Super-speed
|
|
|
|
static const Bit8u bx_msd_dev_descriptor3[] = {
|
|
|
|
0x12, /* u8 bLength; */
|
|
|
|
0x01, /* u8 bDescriptorType; Device */
|
|
|
|
0x00, 0x03, /* u16 bcdUSB; v3.0 */
|
|
|
|
|
|
|
|
0x00, /* u8 bDeviceClass; */
|
|
|
|
0x00, /* u8 bDeviceSubClass; */
|
|
|
|
0x00, /* u8 bDeviceProtocol; */
|
|
|
|
0x09, /* u8 bMaxPacketSize0; 2^^9 = 512 Bytes */
|
2009-04-06 19:36:55 +04:00
|
|
|
|
2014-09-22 23:49:39 +04:00
|
|
|
/* Vendor and product id are arbitrary. */
|
|
|
|
0x00, 0x00, /* u16 idVendor; */
|
|
|
|
0x00, 0x00, /* u16 idProduct; */
|
|
|
|
0x00, 0x01, /* u16 bcdDevice */
|
|
|
|
|
|
|
|
0x01, /* u8 iManufacturer; */
|
|
|
|
0x02, /* u8 iProduct; */
|
|
|
|
0x03, /* u8 iSerialNumber; */
|
|
|
|
0x01 /* u8 bNumConfigurations; */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const Bit8u bx_msd_config_descriptor3[] = {
|
|
|
|
/* one configuration */
|
|
|
|
0x09, /* u8 bLength; */
|
|
|
|
0x02, /* u8 bDescriptorType; Configuration */
|
|
|
|
0x2C, 0x00, /* u16 wTotalLength; */
|
|
|
|
0x01, /* u8 bNumInterfaces; (1) */
|
|
|
|
0x01, /* u8 bConfigurationValue; */
|
|
|
|
0x00, /* u8 iConfiguration; */
|
|
|
|
0x80, /* u8 bmAttributes;
|
|
|
|
Bit 7: must be set,
|
|
|
|
6: Self-powered,
|
|
|
|
5: Remote wakeup,
|
|
|
|
4..0: resvd */
|
|
|
|
0x3F, /* u8 MaxPower; */
|
|
|
|
|
|
|
|
/* one interface */
|
|
|
|
0x09, /* u8 if_bLength; */
|
|
|
|
0x04, /* u8 if_bDescriptorType; Interface */
|
|
|
|
0x00, /* u8 if_bInterfaceNumber; */
|
|
|
|
0x00, /* u8 if_bAlternateSetting; */
|
|
|
|
0x02, /* u8 if_bNumEndpoints; */
|
|
|
|
0x08, /* u8 if_bInterfaceClass; MASS STORAGE */
|
|
|
|
0x06, /* u8 if_bInterfaceSubClass; SCSI */
|
|
|
|
0x50, /* u8 if_bInterfaceProtocol; Bulk Only */
|
|
|
|
0x00, /* u8 if_iInterface; */
|
|
|
|
|
|
|
|
/* Bulk-In endpoint */
|
|
|
|
0x07, /* u8 ep_bLength; */
|
|
|
|
0x05, /* u8 ep_bDescriptorType; Endpoint */
|
|
|
|
0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
|
|
|
|
0x02, /* u8 ep_bmAttributes; Bulk */
|
|
|
|
0x00, 0x04, /* u16 ep_wMaxPacketSize; */
|
|
|
|
0x00, /* u8 ep_bInterval; */
|
|
|
|
|
|
|
|
/* Bulk-In companion descriptor */
|
|
|
|
0x06, /* u8 epc_bLength; */
|
|
|
|
0x30, /* u8 epc_bDescriptorType; Endpoint Companion */
|
|
|
|
0x0F, /* u8 epc_bMaxPerBurst; */
|
|
|
|
0x00, /* u8 epc_bmAttributes; */
|
|
|
|
0x00, 0x00, /* u16 epc_reserved; */
|
|
|
|
|
|
|
|
/* Bulk-Out endpoint */
|
|
|
|
0x07, /* u8 ep_bLength; */
|
|
|
|
0x05, /* u8 ep_bDescriptorType; Endpoint */
|
|
|
|
0x02, /* u8 ep_bEndpointAddress; OUT Endpoint 2 */
|
|
|
|
0x02, /* u8 ep_bmAttributes; Bulk */
|
|
|
|
0x00, 0x04, /* u16 ep_wMaxPacketSize; */
|
|
|
|
0x00, /* u8 ep_bInterval; */
|
|
|
|
|
|
|
|
/* Bulk-Out companion descriptor */
|
|
|
|
0x06, /* u8 epc_bLength; */
|
|
|
|
0x30, /* u8 epc_bDescriptorType; Endpoint Companion */
|
|
|
|
0x0F, /* u8 epc_bMaxPerBurst; */
|
|
|
|
0x00, /* u8 epc_bmAttributes; */
|
|
|
|
0x00, 0x00 /* u16 epc_reserved; */
|
|
|
|
};
|
|
|
|
|
|
|
|
// BOS Descriptor
|
|
|
|
static const Bit8u bx_msd_bos_descriptor3[] = {
|
|
|
|
/* stub */
|
|
|
|
0x05, /* u8 bos_bLength; */
|
|
|
|
0x0F, /* u8 bos_bDescriptorType; BOS */
|
|
|
|
0x16, 0x00, /* u16 bos_wTotalLength; */
|
|
|
|
0x02, /* u8 bos_bNumCapEntries; BOS */
|
|
|
|
|
|
|
|
/* USB 2.0 Extention */
|
|
|
|
0x07, /* u8 bss_bLength; */
|
|
|
|
0x10, /* u8 bss_bType; Device Cap */
|
|
|
|
0x02, /* u8 bss_bCapType; USB 2.0 Ext */
|
|
|
|
0x02, 0x00, /* u32 bss_bmAttributes; */
|
|
|
|
0x00, 0x00,
|
|
|
|
|
|
|
|
/* USB 3.0 */
|
|
|
|
0x0A, /* u8 bss_bLength; */
|
|
|
|
0x10, /* u8 bss_bType; Device Cap */
|
|
|
|
0x03, /* u8 bss_bCapType; USB 3.0 */
|
|
|
|
0x00, /* u8 bss_bmAttributes; */
|
|
|
|
0x0E, 0x00, /* u16 bss_bmSupSpeeds; */
|
|
|
|
0x01, /* u8 bss_bSupFunct; */
|
|
|
|
0x0A, /* u8 bss_bU1DevExitLat; */
|
|
|
|
0x20, 0x00 /* u16 bss_wU2DevExitLat; */
|
|
|
|
};
|
|
|
|
|
2015-08-29 17:02:13 +03:00
|
|
|
void usb_msd_restore_handler(void *dev, bx_list_c *conf);
|
|
|
|
|
2014-09-22 23:49:39 +04:00
|
|
|
static int usb_cdrom_count = 0;
|
2009-04-06 19:36:55 +04:00
|
|
|
|
2009-03-09 15:18:40 +03:00
|
|
|
usb_msd_device_c::usb_msd_device_c(usbdev_type type, const char *filename)
|
2007-03-18 14:17:28 +03:00
|
|
|
{
|
2009-04-06 19:36:55 +04:00
|
|
|
char pname[10];
|
|
|
|
char label[32];
|
2010-12-10 20:02:18 +03:00
|
|
|
char tmpfname[BX_PATHNAME_LEN];
|
|
|
|
char *ptr1, *ptr2;
|
2009-04-06 19:36:55 +04:00
|
|
|
bx_param_string_c *path;
|
2013-01-25 21:56:40 +04:00
|
|
|
bx_param_enum_c *status;
|
2009-04-06 19:36:55 +04:00
|
|
|
|
2009-03-09 15:18:40 +03:00
|
|
|
d.type = type;
|
2014-09-22 23:49:39 +04:00
|
|
|
d.maxspeed = USB_SPEED_SUPER;
|
2015-08-16 19:15:46 +03:00
|
|
|
d.speed = USB_SPEED_FULL;
|
2009-04-06 19:36:55 +04:00
|
|
|
memset((void*)&s, 0, sizeof(s));
|
2009-03-09 15:18:40 +03:00
|
|
|
if (d.type == USB_DEV_TYPE_DISK) {
|
|
|
|
strcpy(d.devname, "BOCHS USB HARDDRIVE");
|
2010-12-10 20:02:18 +03:00
|
|
|
strcpy(tmpfname, filename);
|
|
|
|
ptr1 = strtok(tmpfname, ":");
|
|
|
|
ptr2 = strtok(NULL, ":");
|
|
|
|
if ((ptr2 == NULL) || (strlen(ptr1) < 2)) {
|
|
|
|
s.image_mode = BX_HDIMAGE_MODE_FLAT;
|
|
|
|
s.fname = filename;
|
|
|
|
} else {
|
|
|
|
s.image_mode = SIM->hdimage_get_mode(ptr1);
|
|
|
|
s.fname = filename+strlen(ptr1)+1;
|
|
|
|
}
|
2011-01-16 15:46:48 +03:00
|
|
|
s.journal[0] = 0;
|
2014-09-29 21:48:30 +04:00
|
|
|
s.size = 0;
|
2015-07-14 00:57:09 +03:00
|
|
|
s.statusbar_id = bx_gui->register_statusitem("USB-HD", 1);
|
2009-03-09 15:18:40 +03:00
|
|
|
} else if (d.type == USB_DEV_TYPE_CDROM) {
|
|
|
|
strcpy(d.devname, "BOCHS USB CDROM");
|
2010-12-10 20:02:18 +03:00
|
|
|
s.fname = filename;
|
2009-04-06 19:36:55 +04:00
|
|
|
// config options
|
|
|
|
bx_list_c *usb_rt = (bx_list_c*)SIM->get_param(BXPN_MENU_RUNTIME_USB);
|
2013-11-05 22:06:39 +04:00
|
|
|
sprintf(pname, "cdrom%d", ++usb_cdrom_count);
|
|
|
|
sprintf(label, "USB CD-ROM #%d Configuration", usb_cdrom_count);
|
2012-02-12 22:43:20 +04:00
|
|
|
s.config = new bx_list_c(usb_rt, pname, label);
|
2013-01-25 21:56:40 +04:00
|
|
|
s.config->set_options(bx_list_c::SERIES_ASK | bx_list_c::USE_BOX_TITLE);
|
2009-04-06 19:36:55 +04:00
|
|
|
s.config->set_device_param(this);
|
|
|
|
path = new bx_param_string_c(s.config, "path", "Path", "", "", BX_PATHNAME_LEN);
|
|
|
|
path->set(s.fname);
|
2015-08-23 19:09:24 +03:00
|
|
|
path->set_handler(cdrom_path_handler);
|
2013-01-25 21:56:40 +04:00
|
|
|
status = new bx_param_enum_c(s.config,
|
|
|
|
"status",
|
|
|
|
"Status",
|
|
|
|
"CD-ROM media status (inserted / ejected)",
|
|
|
|
media_status_names,
|
2014-01-22 00:56:50 +04:00
|
|
|
BX_INSERTED,
|
2013-01-25 21:56:40 +04:00
|
|
|
BX_EJECTED);
|
2015-08-23 19:09:24 +03:00
|
|
|
status->set_handler(cdrom_status_handler);
|
2013-01-25 21:56:40 +04:00
|
|
|
status->set_ask_format("Is the device inserted or ejected? [%s] ");
|
2014-05-01 12:42:04 +04:00
|
|
|
if (SIM->is_wx_selected()) {
|
|
|
|
bx_list_c *usb = (bx_list_c*)SIM->get_param("ports.usb");
|
|
|
|
usb->add(s.config);
|
|
|
|
}
|
2015-07-14 00:57:09 +03:00
|
|
|
s.statusbar_id = bx_gui->register_statusitem("USB-CD", 1);
|
2009-03-09 15:18:40 +03:00
|
|
|
}
|
2007-03-18 14:17:28 +03:00
|
|
|
|
2013-12-29 16:56:52 +04:00
|
|
|
put("usb_msd", "USBMSD");
|
2007-03-18 14:17:28 +03:00
|
|
|
}
|
|
|
|
|
2009-01-19 12:48:12 +03:00
|
|
|
usb_msd_device_c::~usb_msd_device_c(void)
|
2007-03-18 14:17:28 +03:00
|
|
|
{
|
2015-07-14 00:57:09 +03:00
|
|
|
bx_gui->unregister_statusitem(s.statusbar_id);
|
2009-01-20 00:39:03 +03:00
|
|
|
if (s.scsi_dev != NULL)
|
|
|
|
delete s.scsi_dev;
|
2009-03-09 15:18:40 +03:00
|
|
|
if (s.hdimage != NULL) {
|
2015-07-12 17:45:34 +03:00
|
|
|
s.hdimage->close();
|
2009-03-09 15:18:40 +03:00
|
|
|
delete s.hdimage;
|
|
|
|
} else if (s.cdrom != NULL) {
|
|
|
|
delete s.cdrom;
|
2014-05-01 12:42:04 +04:00
|
|
|
if (SIM->is_wx_selected()) {
|
|
|
|
bx_list_c *usb = (bx_list_c*)SIM->get_param("ports.usb");
|
|
|
|
usb->remove(s.config->get_name());
|
|
|
|
}
|
2009-04-06 19:36:55 +04:00
|
|
|
bx_list_c *usb_rt = (bx_list_c*)SIM->get_param(BXPN_MENU_RUNTIME_USB);
|
|
|
|
usb_rt->remove(s.config->get_name());
|
2009-03-09 15:18:40 +03:00
|
|
|
}
|
2007-03-18 14:17:28 +03:00
|
|
|
}
|
|
|
|
|
2011-01-16 15:46:48 +03:00
|
|
|
bx_bool usb_msd_device_c::set_option(const char *option)
|
|
|
|
{
|
2014-09-29 21:48:30 +04:00
|
|
|
char *suffix;
|
|
|
|
|
2011-01-16 15:46:48 +03:00
|
|
|
if (!strncmp(option, "journal:", 8)) {
|
2014-09-29 21:48:30 +04:00
|
|
|
if (d.type == USB_DEV_TYPE_DISK) {
|
|
|
|
strcpy(s.journal, option+8);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
BX_ERROR(("Option 'journal' is only valid for USB disks"));
|
|
|
|
}
|
|
|
|
} else if (!strncmp(option, "size:", 5)) {
|
|
|
|
if ((d.type == USB_DEV_TYPE_DISK) && (s.image_mode == BX_HDIMAGE_MODE_VVFAT)) {
|
|
|
|
s.size = (int)strtol(option+5, &suffix, 10);
|
|
|
|
if (!strcmp(suffix, "G")) {
|
|
|
|
s.size <<= 10;
|
|
|
|
} else if (strcmp(suffix, "M")) {
|
|
|
|
BX_ERROR(("Unknown VVFAT disk size suffix '%s' - using default", suffix));
|
|
|
|
s.size = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((s.size < 128) || (s.size >= 131072)) {
|
|
|
|
BX_ERROR(("Invalid VVFAT disk size value - using default"));
|
|
|
|
s.size = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
BX_ERROR(("Option 'size' is only valid for USB VVFAT disks"));
|
|
|
|
}
|
2011-01-16 15:46:48 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-01 22:29:36 +03:00
|
|
|
bx_bool usb_msd_device_c::init()
|
2007-03-18 14:17:28 +03:00
|
|
|
{
|
2009-03-09 15:18:40 +03:00
|
|
|
if (d.type == USB_DEV_TYPE_DISK) {
|
2011-01-16 15:46:48 +03:00
|
|
|
s.hdimage = DEV_hdimage_init_image(s.image_mode, 0, s.journal);
|
2014-09-29 21:48:30 +04:00
|
|
|
if (s.image_mode == BX_HDIMAGE_MODE_VVFAT) {
|
|
|
|
Bit64u hdsize = ((Bit64u)s.size) << 20;
|
|
|
|
s.hdimage->cylinders = (Bit64u)(hdsize/16.0/63.0/512.0);
|
|
|
|
s.hdimage->heads = 16;
|
|
|
|
s.hdimage->spt = 63;
|
|
|
|
}
|
2009-03-09 15:18:40 +03:00
|
|
|
if (s.hdimage->open(s.fname) < 0) {
|
|
|
|
BX_ERROR(("could not open hard drive image file '%s'", s.fname));
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
s.scsi_dev = new scsi_device_t(s.hdimage, 0, usb_msd_command_complete, (void*)this);
|
|
|
|
}
|
2010-12-10 20:02:18 +03:00
|
|
|
sprintf(s.info_txt, "USB HD: path='%s', mode='%s'", s.fname, hdimage_mode_names[s.image_mode]);
|
2009-03-09 15:18:40 +03:00
|
|
|
} else if (d.type == USB_DEV_TYPE_CDROM) {
|
2011-07-17 21:23:12 +04:00
|
|
|
s.cdrom = DEV_hdimage_init_cdrom(s.fname);
|
2014-01-22 00:56:50 +04:00
|
|
|
s.scsi_dev = new scsi_device_t(s.cdrom, 0, usb_msd_command_complete, (void*)this);
|
|
|
|
if (set_inserted(1)) {
|
|
|
|
sprintf(s.info_txt, "USB CD: path='%s'", s.fname);
|
2009-03-09 15:18:40 +03:00
|
|
|
} else {
|
2014-01-22 00:56:50 +04:00
|
|
|
sprintf(s.info_txt, "USB CD: media not present");
|
2009-03-09 15:18:40 +03:00
|
|
|
}
|
2007-03-18 14:17:28 +03:00
|
|
|
}
|
2009-03-09 15:18:40 +03:00
|
|
|
s.scsi_dev->register_state(s.sr_list, "scsidev");
|
2015-09-03 21:54:51 +03:00
|
|
|
if (getonoff(LOGLEV_DEBUG) == ACT_REPORT) {
|
|
|
|
s.scsi_dev->set_debug_mode();
|
|
|
|
}
|
2009-03-09 15:18:40 +03:00
|
|
|
s.mode = USB_MSDM_CBW;
|
|
|
|
d.connected = 1;
|
2015-08-30 11:37:27 +03:00
|
|
|
s.status_changed = 0;
|
2009-03-09 15:18:40 +03:00
|
|
|
return 1;
|
2007-03-18 14:17:28 +03:00
|
|
|
}
|
|
|
|
|
2010-12-05 16:09:41 +03:00
|
|
|
const char* usb_msd_device_c::get_info()
|
|
|
|
{
|
|
|
|
return s.info_txt;
|
|
|
|
}
|
|
|
|
|
2009-01-19 12:48:12 +03:00
|
|
|
void usb_msd_device_c::register_state_specific(bx_list_c *parent)
|
2007-04-09 13:04:54 +04:00
|
|
|
{
|
2012-02-12 22:43:20 +04:00
|
|
|
s.sr_list = new bx_list_c(parent, "s", "USB MSD Device State");
|
2015-08-29 17:02:13 +03:00
|
|
|
if (d.type == USB_DEV_TYPE_CDROM) {
|
|
|
|
bx_list_c *rt_config = new bx_list_c(s.sr_list, "rt_config");
|
|
|
|
rt_config->add(s.config->get_by_name("path"));
|
|
|
|
rt_config->add(s.config->get_by_name("status"));
|
|
|
|
rt_config->set_restore_handler(this, usb_msd_restore_handler);
|
|
|
|
} else if ((d.type == USB_DEV_TYPE_DISK) && (s.hdimage != NULL)) {
|
2012-09-16 23:51:30 +04:00
|
|
|
s.hdimage->register_state(s.sr_list);
|
|
|
|
}
|
2007-05-05 16:30:23 +04:00
|
|
|
new bx_shadow_num_c(s.sr_list, "mode", &s.mode);
|
|
|
|
new bx_shadow_num_c(s.sr_list, "scsi_len", &s.scsi_len);
|
|
|
|
new bx_shadow_num_c(s.sr_list, "usb_len", &s.usb_len);
|
|
|
|
new bx_shadow_num_c(s.sr_list, "data_len", &s.data_len);
|
|
|
|
new bx_shadow_num_c(s.sr_list, "residue", &s.residue);
|
|
|
|
new bx_shadow_num_c(s.sr_list, "tag", &s.tag);
|
|
|
|
new bx_shadow_num_c(s.sr_list, "result", &s.result);
|
2007-04-09 13:04:54 +04:00
|
|
|
}
|
|
|
|
|
2009-01-19 12:48:12 +03:00
|
|
|
void usb_msd_device_c::handle_reset()
|
2007-03-18 14:17:28 +03:00
|
|
|
{
|
|
|
|
BX_DEBUG(("Reset"));
|
|
|
|
s.mode = USB_MSDM_CBW;
|
|
|
|
}
|
|
|
|
|
2009-01-19 12:48:12 +03:00
|
|
|
int usb_msd_device_c::handle_control(int request, int value, int index, int length, Bit8u *data)
|
2007-03-18 14:17:28 +03:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
switch (request) {
|
|
|
|
case DeviceRequest | USB_REQ_GET_STATUS:
|
2009-03-09 17:44:06 +03:00
|
|
|
case EndpointRequest | USB_REQ_GET_STATUS:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("USB_REQ_GET_STATUS:"));
|
2007-03-18 14:17:28 +03:00
|
|
|
data[0] = (1 << USB_DEVICE_SELF_POWERED) |
|
|
|
|
(d.remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
|
|
|
|
data[1] = 0x00;
|
|
|
|
ret = 2;
|
|
|
|
break;
|
|
|
|
case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("USB_REQ_CLEAR_FEATURE:"));
|
2007-03-18 14:17:28 +03:00
|
|
|
if (value == USB_DEVICE_REMOTE_WAKEUP) {
|
|
|
|
d.remote_wakeup = 0;
|
|
|
|
} else {
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("USB_REQ_CLEAR_FEATURE: Not handled: %i %i %i %i", request, value, index, length ));
|
2007-03-18 14:17:28 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case DeviceOutRequest | USB_REQ_SET_FEATURE:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("USB_REQ_SET_FEATURE:"));
|
|
|
|
switch (value) {
|
|
|
|
case USB_DEVICE_REMOTE_WAKEUP:
|
|
|
|
d.remote_wakeup = 1;
|
|
|
|
break;
|
|
|
|
case USB_DEVICE_U1_ENABLE:
|
|
|
|
case USB_DEVICE_U2_ENABLE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BX_DEBUG(("USB_REQ_SET_FEATURE: Not handled: %i %i %i %i", request, value, index, length ));
|
2007-03-18 14:17:28 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case DeviceOutRequest | USB_REQ_SET_ADDRESS:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("USB_REQ_SET_ADDRESS:"));
|
2007-03-18 14:17:28 +03:00
|
|
|
d.addr = value;
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
|
2014-09-22 23:49:39 +04:00
|
|
|
switch (value >> 8) {
|
2007-03-18 14:17:28 +03:00
|
|
|
case USB_DT_DEVICE:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("USB_REQ_GET_DESCRIPTOR: Device"));
|
|
|
|
if (get_speed() == USB_SPEED_SUPER) {
|
|
|
|
memcpy(data, bx_msd_dev_descriptor3, sizeof(bx_msd_dev_descriptor3));
|
|
|
|
ret = sizeof(bx_msd_dev_descriptor3);
|
|
|
|
} else {
|
|
|
|
memcpy(data, bx_msd_dev_descriptor, sizeof(bx_msd_dev_descriptor));
|
|
|
|
ret = sizeof(bx_msd_dev_descriptor);
|
|
|
|
}
|
2007-03-18 14:17:28 +03:00
|
|
|
break;
|
|
|
|
case USB_DT_CONFIG:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("USB_REQ_GET_DESCRIPTOR: Config"));
|
|
|
|
if (get_speed() == USB_SPEED_SUPER) {
|
|
|
|
memcpy(data, bx_msd_config_descriptor3, sizeof(bx_msd_config_descriptor3));
|
|
|
|
ret = sizeof(bx_msd_config_descriptor3);
|
|
|
|
} else {
|
|
|
|
memcpy(data, bx_msd_config_descriptor, sizeof(bx_msd_config_descriptor));
|
|
|
|
ret = sizeof(bx_msd_config_descriptor);
|
|
|
|
}
|
2007-03-18 14:17:28 +03:00
|
|
|
break;
|
|
|
|
case USB_DT_STRING:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("USB_REQ_GET_DESCRIPTOR: String"));
|
2007-03-18 14:17:28 +03:00
|
|
|
switch(value & 0xff) {
|
|
|
|
case 0:
|
|
|
|
// language IDs
|
|
|
|
data[0] = 4;
|
|
|
|
data[1] = 3;
|
|
|
|
data[2] = 0x09;
|
|
|
|
data[3] = 0x04;
|
|
|
|
ret = 4;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// vendor description
|
|
|
|
ret = set_usb_string(data, "BOCHS");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// product description
|
2007-03-24 14:43:41 +03:00
|
|
|
if (strlen(d.devname) > 0) {
|
|
|
|
ret = set_usb_string(data, d.devname);
|
|
|
|
} else {
|
|
|
|
goto fail;
|
|
|
|
}
|
2007-03-18 14:17:28 +03:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
// serial number
|
2009-04-03 20:42:56 +04:00
|
|
|
ret = set_usb_string(data, s.scsi_dev->get_serial_number());
|
2007-03-18 14:17:28 +03:00
|
|
|
break;
|
2014-09-22 23:49:39 +04:00
|
|
|
case 0xEE:
|
|
|
|
// Microsoft OS Descriptor check
|
|
|
|
// We don't support this check, so fail
|
|
|
|
goto fail;
|
2007-03-18 14:17:28 +03:00
|
|
|
default:
|
2009-02-14 13:06:20 +03:00
|
|
|
BX_ERROR(("USB MSD handle_control: unknown string descriptor 0x%02x", value & 0xff));
|
2007-03-18 14:17:28 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case USB_DT_DEVICE_QUALIFIER:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("USB_REQ_GET_DESCRIPTOR: Device Qualifier"));
|
2007-03-18 14:17:28 +03:00
|
|
|
// device qualifier
|
2015-06-17 23:15:53 +03:00
|
|
|
if (get_speed() <= USB_SPEED_FULL) {
|
|
|
|
// a low- or full-speed only device (i.e.: a non high-speed device) must return
|
|
|
|
// request error on this function
|
|
|
|
BX_ERROR(("USB MSD handle_control: full-speed only device returning stall on Device Qualifier."));
|
|
|
|
goto fail;
|
|
|
|
} else {
|
|
|
|
data[0] = 10;
|
|
|
|
data[1] = USB_DT_DEVICE_QUALIFIER;
|
|
|
|
memcpy(data+2, bx_msd_dev_descriptor+2, 6);
|
|
|
|
data[8] = 1;
|
|
|
|
data[9] = 0;
|
|
|
|
ret = 10;
|
|
|
|
}
|
2007-03-18 14:17:28 +03:00
|
|
|
break;
|
2014-09-22 23:49:39 +04:00
|
|
|
case USB_DT_BIN_DEV_OBJ_STORE:
|
|
|
|
BX_DEBUG(("USB_REQ_GET_DESCRIPTOR: BOS"));
|
|
|
|
if (get_speed() == USB_SPEED_SUPER) {
|
|
|
|
memcpy(data, bx_msd_bos_descriptor3, sizeof(bx_msd_bos_descriptor3));
|
|
|
|
ret = sizeof(bx_msd_bos_descriptor3);
|
|
|
|
} else
|
|
|
|
goto fail;
|
|
|
|
break;
|
2007-03-18 14:17:28 +03:00
|
|
|
default:
|
2009-02-14 13:06:20 +03:00
|
|
|
BX_ERROR(("USB MSD handle_control: unknown descriptor type 0x%02x", value >> 8));
|
2007-03-18 14:17:28 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DeviceRequest | USB_REQ_GET_CONFIGURATION:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("USB_REQ_GET_CONFIGURATION:"));
|
2007-03-18 14:17:28 +03:00
|
|
|
data[0] = 1;
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("USB_REQ_SET_CONFIGURATION:"));
|
2007-03-18 14:17:28 +03:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case DeviceRequest | USB_REQ_GET_INTERFACE:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("USB_REQ_GET_INFTERFACE:"));
|
2007-03-18 14:17:28 +03:00
|
|
|
data[0] = 0;
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
case DeviceOutRequest | USB_REQ_SET_INTERFACE:
|
2009-03-09 17:44:06 +03:00
|
|
|
case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("USB_REQ_SET_INFTERFACE:"));
|
2007-03-18 14:17:28 +03:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("USB_REQ_CLEAR_FEATURE:"));
|
2007-03-18 14:17:28 +03:00
|
|
|
if (value == 0 && index != 0x81) { /* clear ep halt */
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
break;
|
2014-09-22 23:49:39 +04:00
|
|
|
case DeviceOutRequest | USB_REQ_SET_SEL:
|
|
|
|
// Set U1 and U2 System Exit Latency
|
|
|
|
BX_DEBUG(("SET_SEL (U1 and U2):"));
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
// Class specific requests
|
2009-02-17 19:43:51 +03:00
|
|
|
case InterfaceOutClassRequest | MassStorageReset:
|
2007-03-18 14:17:28 +03:00
|
|
|
case MassStorageReset:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("MASS STORAGE RESET:"));
|
2007-03-18 14:17:28 +03:00
|
|
|
s.mode = USB_MSDM_CBW;
|
|
|
|
ret = 0;
|
|
|
|
break;
|
2009-02-17 19:43:51 +03:00
|
|
|
case InterfaceInClassRequest | GetMaxLun:
|
2007-03-18 14:17:28 +03:00
|
|
|
case GetMaxLun:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("MASS STORAGE: GET MAX LUN"));
|
2007-03-18 14:17:28 +03:00
|
|
|
data[0] = 0;
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2007-03-25 21:37:59 +04:00
|
|
|
BX_ERROR(("USB MSD handle_control: unknown request 0x%04x", request));
|
2007-03-18 14:17:28 +03:00
|
|
|
fail:
|
2009-04-11 00:26:14 +04:00
|
|
|
d.stall = 1;
|
2007-03-18 14:17:28 +03:00
|
|
|
ret = USB_RET_STALL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-01-19 12:48:12 +03:00
|
|
|
int usb_msd_device_c::handle_data(USBPacket *p)
|
2007-03-18 14:17:28 +03:00
|
|
|
{
|
|
|
|
struct usb_msd_cbw cbw;
|
|
|
|
int ret = 0;
|
|
|
|
Bit8u devep = p->devep;
|
|
|
|
Bit8u *data = p->data;
|
|
|
|
int len = p->len;
|
2015-09-13 23:04:41 +03:00
|
|
|
bx_bool async = (p->complete_cb != NULL);
|
2007-03-18 14:17:28 +03:00
|
|
|
|
|
|
|
switch (p->pid) {
|
|
|
|
case USB_TOKEN_OUT:
|
|
|
|
usb_dump_packet(data, len);
|
|
|
|
if (devep != 2)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
switch (s.mode) {
|
|
|
|
case USB_MSDM_CBW:
|
|
|
|
if (len != 31) {
|
|
|
|
BX_ERROR(("bad CBW len"));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
memcpy(&cbw, data, 31);
|
|
|
|
if (dtoh32(cbw.sig) != 0x43425355) {
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_ERROR(("bad signature %08X", dtoh32(cbw.sig)));
|
2007-03-18 14:17:28 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
BX_DEBUG(("command on LUN %d", cbw.lun));
|
|
|
|
s.tag = dtoh32(cbw.tag);
|
|
|
|
s.data_len = dtoh32(cbw.data_len);
|
|
|
|
if (s.data_len == 0) {
|
|
|
|
s.mode = USB_MSDM_CSW;
|
|
|
|
} else if (cbw.flags & 0x80) {
|
|
|
|
s.mode = USB_MSDM_DATAIN;
|
|
|
|
} else {
|
|
|
|
s.mode = USB_MSDM_DATAOUT;
|
|
|
|
}
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_DEBUG(("command tag 0x%X flags %08X len %d data %d",
|
2007-03-18 14:17:28 +03:00
|
|
|
s.tag, cbw.flags, cbw.cmd_len, s.data_len));
|
|
|
|
s.residue = 0;
|
2015-09-13 23:04:41 +03:00
|
|
|
s.scsi_dev->scsi_send_command(s.tag, cbw.cmd, cbw.lun, async);
|
2007-03-18 14:17:28 +03:00
|
|
|
if (s.residue == 0) {
|
|
|
|
if (s.mode == USB_MSDM_DATAIN) {
|
2015-08-23 19:09:24 +03:00
|
|
|
if (s.scsi_dev->scsi_read_data(s.tag)) {
|
|
|
|
bx_gui->statusbar_setitem(s.statusbar_id, 1);
|
|
|
|
}
|
2007-03-18 14:17:28 +03:00
|
|
|
} else if (s.mode == USB_MSDM_DATAOUT) {
|
2015-08-23 19:09:24 +03:00
|
|
|
if (s.scsi_dev->scsi_write_data(s.tag)) {
|
|
|
|
bx_gui->statusbar_setitem(s.statusbar_id, 1, 1);
|
|
|
|
}
|
2007-03-18 14:17:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = len;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_MSDM_DATAOUT:
|
|
|
|
BX_DEBUG(("data out %d/%d", len, s.data_len));
|
|
|
|
if (len > (int)s.data_len)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
s.usb_buf = data;
|
|
|
|
s.usb_len = len;
|
|
|
|
if (s.scsi_len) {
|
|
|
|
copy_data();
|
|
|
|
}
|
|
|
|
if (s.residue && s.usb_len) {
|
|
|
|
s.data_len -= s.usb_len;
|
|
|
|
if (s.data_len == 0)
|
2007-04-09 13:04:54 +04:00
|
|
|
s.mode = USB_MSDM_CSW;
|
2007-03-18 14:17:28 +03:00
|
|
|
s.usb_len = 0;
|
|
|
|
}
|
|
|
|
if (s.usb_len) {
|
2015-09-15 20:55:32 +03:00
|
|
|
BX_DEBUG(("deferring packet %p", p));
|
2009-03-09 15:18:40 +03:00
|
|
|
usb_defer_packet(p, this);
|
2007-03-18 14:17:28 +03:00
|
|
|
s.packet = p;
|
|
|
|
ret = USB_RET_ASYNC;
|
|
|
|
} else {
|
|
|
|
ret = len;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_ERROR(("USB MSD handle_data: unexpected mode at USB_TOKEN_OUT: (0x%02X)", s.mode));
|
2007-03-18 14:17:28 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_TOKEN_IN:
|
|
|
|
if (devep != 1)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
switch (s.mode) {
|
|
|
|
case USB_MSDM_DATAOUT:
|
2009-04-11 00:26:14 +04:00
|
|
|
if ((s.data_len > 0) && (len == 13)) {
|
|
|
|
s.usb_len = len;
|
|
|
|
s.usb_buf = data;
|
|
|
|
send_status();
|
|
|
|
ret = 13;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-18 14:17:28 +03:00
|
|
|
if (s.data_len != 0 || len < 13)
|
|
|
|
goto fail;
|
2009-03-09 15:18:40 +03:00
|
|
|
usb_defer_packet(p, this);
|
2007-03-18 14:17:28 +03:00
|
|
|
s.packet = p;
|
|
|
|
ret = USB_RET_ASYNC;
|
|
|
|
break;
|
|
|
|
|
2009-04-01 22:19:46 +04:00
|
|
|
case USB_MSDM_CSW:
|
|
|
|
BX_DEBUG(("command status %d tag 0x%x, len %d",
|
|
|
|
s.result, s.tag, len));
|
|
|
|
if (len < 13)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
s.usb_len = len;
|
|
|
|
s.usb_buf = data;
|
|
|
|
send_status();
|
|
|
|
s.mode = USB_MSDM_CBW;
|
|
|
|
ret = 13;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_MSDM_DATAIN:
|
|
|
|
BX_DEBUG(("data in %d/%d", len, s.data_len));
|
|
|
|
if (len > (int)s.data_len)
|
2007-03-18 14:17:28 +03:00
|
|
|
len = s.data_len;
|
2009-04-01 22:19:46 +04:00
|
|
|
s.usb_buf = data;
|
|
|
|
s.usb_len = len;
|
|
|
|
if (s.scsi_len) {
|
|
|
|
copy_data();
|
|
|
|
}
|
|
|
|
if (s.residue && s.usb_len) {
|
|
|
|
s.data_len -= s.usb_len;
|
|
|
|
memset(s.usb_buf, 0, s.usb_len);
|
|
|
|
if (s.data_len == 0)
|
|
|
|
s.mode = USB_MSDM_CSW;
|
|
|
|
s.usb_len = 0;
|
|
|
|
}
|
|
|
|
if (s.usb_len) {
|
|
|
|
BX_INFO(("deferring packet %p", p));
|
|
|
|
usb_defer_packet(p, this);
|
|
|
|
s.packet = p;
|
|
|
|
ret = USB_RET_ASYNC;
|
|
|
|
} else {
|
2007-03-18 14:17:28 +03:00
|
|
|
ret = len;
|
2009-04-01 22:19:46 +04:00
|
|
|
}
|
|
|
|
break;
|
2007-03-18 14:17:28 +03:00
|
|
|
|
2009-04-01 22:19:46 +04:00
|
|
|
default:
|
2014-09-22 23:49:39 +04:00
|
|
|
BX_ERROR(("USB MSD handle_data: unexpected mode at USB_TOKEN_IN: (0x%02X)", s.mode));
|
2009-04-01 22:19:46 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (ret > 0) usb_dump_packet(data, ret);
|
|
|
|
break;
|
2007-03-18 14:17:28 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
BX_ERROR(("USB MSD handle_data: bad token"));
|
|
|
|
fail:
|
2009-04-11 00:26:14 +04:00
|
|
|
d.stall = 1;
|
2007-03-18 14:17:28 +03:00
|
|
|
ret = USB_RET_STALL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-01-19 12:48:12 +03:00
|
|
|
void usb_msd_device_c::copy_data()
|
2007-03-18 14:17:28 +03:00
|
|
|
{
|
|
|
|
Bit32u len = s.usb_len;
|
|
|
|
if (len > s.scsi_len)
|
|
|
|
len = s.scsi_len;
|
|
|
|
if (s.mode == USB_MSDM_DATAIN) {
|
|
|
|
memcpy(s.usb_buf, s.scsi_buf, len);
|
|
|
|
} else {
|
|
|
|
memcpy(s.scsi_buf, s.usb_buf, len);
|
|
|
|
}
|
|
|
|
s.usb_len -= len;
|
|
|
|
s.scsi_len -= len;
|
|
|
|
s.usb_buf += len;
|
|
|
|
s.scsi_buf += len;
|
|
|
|
s.data_len -= len;
|
|
|
|
if (s.scsi_len == 0) {
|
|
|
|
if (s.mode == USB_MSDM_DATAIN) {
|
2015-08-23 19:09:24 +03:00
|
|
|
if (s.scsi_dev->scsi_read_data(s.tag)) {
|
|
|
|
bx_gui->statusbar_setitem(s.statusbar_id, 1);
|
|
|
|
}
|
2007-03-18 14:17:28 +03:00
|
|
|
} else if (s.mode == USB_MSDM_DATAOUT) {
|
2015-08-23 19:09:24 +03:00
|
|
|
if (s.scsi_dev->scsi_write_data(s.tag)) {
|
|
|
|
bx_gui->statusbar_setitem(s.statusbar_id, 1, 1);
|
|
|
|
}
|
2007-03-18 14:17:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-19 12:48:12 +03:00
|
|
|
void usb_msd_device_c::send_status()
|
2007-03-18 14:17:28 +03:00
|
|
|
{
|
|
|
|
struct usb_msd_csw csw;
|
|
|
|
|
|
|
|
csw.sig = htod32(0x53425355);
|
|
|
|
csw.tag = htod32(s.tag);
|
|
|
|
csw.residue = s.residue;
|
|
|
|
csw.status = s.result;
|
|
|
|
memcpy(s.usb_buf, &csw, 13);
|
|
|
|
}
|
|
|
|
|
2009-01-19 12:48:12 +03:00
|
|
|
void usb_msd_device_c::usb_msd_command_complete(void *this_ptr, int reason, Bit32u tag, Bit32u arg)
|
2007-03-18 14:17:28 +03:00
|
|
|
{
|
2009-01-19 12:48:12 +03:00
|
|
|
usb_msd_device_c *class_ptr = (usb_msd_device_c *) this_ptr;
|
2007-03-18 14:17:28 +03:00
|
|
|
class_ptr->command_complete(reason, tag, arg);
|
|
|
|
}
|
|
|
|
|
2009-01-19 12:48:12 +03:00
|
|
|
void usb_msd_device_c::command_complete(int reason, Bit32u tag, Bit32u arg)
|
2007-03-18 14:17:28 +03:00
|
|
|
{
|
|
|
|
USBPacket *p = s.packet;
|
|
|
|
|
|
|
|
if (tag != s.tag) {
|
|
|
|
BX_ERROR(("usb-msd_command_complete: unexpected SCSI tag 0x%x", tag));
|
|
|
|
}
|
|
|
|
if (reason == SCSI_REASON_DONE) {
|
|
|
|
BX_DEBUG(("command complete %d", arg));
|
|
|
|
s.residue = s.data_len;
|
|
|
|
s.result = arg != 0;
|
|
|
|
if (s.packet) {
|
|
|
|
if (s.data_len == 0 && s.mode == USB_MSDM_DATAOUT) {
|
|
|
|
send_status();
|
|
|
|
s.mode = USB_MSDM_CBW;
|
|
|
|
} else {
|
|
|
|
if (s.data_len) {
|
|
|
|
s.data_len -= s.usb_len;
|
|
|
|
if (s.mode == USB_MSDM_DATAIN)
|
|
|
|
memset(s.usb_buf, 0, s.usb_len);
|
|
|
|
s.usb_len = 0;
|
|
|
|
}
|
|
|
|
if (s.data_len == 0)
|
|
|
|
s.mode = USB_MSDM_CSW;
|
|
|
|
}
|
|
|
|
s.packet = NULL;
|
2015-09-13 23:04:41 +03:00
|
|
|
usb_packet_complete(p);
|
2007-03-18 14:17:28 +03:00
|
|
|
} else if (s.data_len == 0) {
|
|
|
|
s.mode = USB_MSDM_CSW;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s.scsi_len = arg;
|
|
|
|
s.scsi_buf = s.scsi_dev->scsi_get_buf(tag);
|
|
|
|
if (p) {
|
2015-09-15 20:55:32 +03:00
|
|
|
if (s.scsi_len > 0) {
|
|
|
|
usb_dump_packet(s.scsi_buf, s.scsi_len);
|
|
|
|
}
|
2007-03-18 14:17:28 +03:00
|
|
|
copy_data();
|
|
|
|
if (s.usb_len == 0) {
|
2015-09-15 20:55:32 +03:00
|
|
|
BX_DEBUG(("packet complete %p", p));
|
|
|
|
if (s.packet != NULL) {
|
|
|
|
s.packet = NULL;
|
|
|
|
usb_packet_complete(p);
|
|
|
|
}
|
2007-03-18 14:17:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-09 15:18:40 +03:00
|
|
|
void usb_msd_device_c::cancel_packet(USBPacket *p)
|
|
|
|
{
|
|
|
|
s.scsi_dev->scsi_cancel_io(s.tag);
|
|
|
|
s.packet = NULL;
|
|
|
|
s.scsi_len = 0;
|
|
|
|
}
|
|
|
|
|
2014-01-22 00:56:50 +04:00
|
|
|
bx_bool usb_msd_device_c::set_inserted(bx_bool value)
|
2009-04-06 19:36:55 +04:00
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
path = SIM->get_param_string("path", s.config)->getptr();
|
2015-09-03 21:54:51 +03:00
|
|
|
if ((strlen(path) > 0) && (strcmp(path, "none"))) {
|
|
|
|
if (!s.cdrom->insert_cdrom(path)) {
|
|
|
|
value = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
value = 0;
|
|
|
|
}
|
|
|
|
if (!value) {
|
2014-01-22 00:56:50 +04:00
|
|
|
SIM->get_param_enum("status", s.config)->set(BX_EJECTED);
|
2015-09-03 21:54:51 +03:00
|
|
|
s.status_changed = 0;
|
2009-04-06 19:36:55 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.cdrom->eject_cdrom();
|
|
|
|
}
|
|
|
|
s.scsi_dev->set_inserted(value);
|
2014-01-22 00:56:50 +04:00
|
|
|
return value;
|
2009-04-06 19:36:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bx_bool usb_msd_device_c::get_inserted()
|
|
|
|
{
|
|
|
|
return s.scsi_dev->get_inserted();
|
|
|
|
}
|
|
|
|
|
2015-08-23 19:09:24 +03:00
|
|
|
bx_bool usb_msd_device_c::get_locked()
|
|
|
|
{
|
|
|
|
return s.scsi_dev->get_locked();
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_msd_device_c::runtime_config(void)
|
|
|
|
{
|
2015-08-29 17:02:13 +03:00
|
|
|
if (d.type == USB_DEV_TYPE_CDROM) {
|
|
|
|
if (s.status_changed) {
|
|
|
|
set_inserted(0);
|
|
|
|
if (SIM->get_param_enum("status", s.config)->get() == BX_INSERTED) {
|
|
|
|
set_inserted(1);
|
|
|
|
}
|
|
|
|
s.status_changed = 0;
|
2015-08-23 19:09:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-06 19:36:55 +04:00
|
|
|
#undef LOG_THIS
|
|
|
|
#define LOG_THIS cdrom->
|
|
|
|
|
2015-08-30 11:37:27 +03:00
|
|
|
// USB cdrom runtime parameter handlers
|
2015-08-23 19:09:24 +03:00
|
|
|
const char *usb_msd_device_c::cdrom_path_handler(bx_param_string_c *param, int set,
|
2015-08-30 11:37:27 +03:00
|
|
|
const char *oldval, const char *val, int maxlen)
|
2009-04-06 19:36:55 +04:00
|
|
|
{
|
|
|
|
usb_msd_device_c *cdrom;
|
|
|
|
|
|
|
|
if (set) {
|
2015-08-23 19:09:24 +03:00
|
|
|
if (strlen(val) < 1) {
|
|
|
|
val = "none";
|
|
|
|
}
|
2009-04-06 19:36:55 +04:00
|
|
|
cdrom = (usb_msd_device_c*) param->get_parent()->get_device_param();
|
|
|
|
if (cdrom != NULL) {
|
2015-08-23 19:09:24 +03:00
|
|
|
if (!cdrom->get_locked()) {
|
|
|
|
cdrom->s.status_changed = 1;
|
2009-04-06 19:36:55 +04:00
|
|
|
} else {
|
2015-08-23 19:09:24 +03:00
|
|
|
val = oldval;
|
|
|
|
BX_ERROR(("cdrom tray locked: path change failed"));
|
2009-04-06 19:36:55 +04:00
|
|
|
}
|
|
|
|
} else {
|
2015-08-23 19:09:24 +03:00
|
|
|
BX_PANIC(("cdrom_path_handler: cdrom not found"));
|
2009-04-06 19:36:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2015-08-23 19:09:24 +03:00
|
|
|
Bit64s usb_msd_device_c::cdrom_status_handler(bx_param_c *param, int set, Bit64s val)
|
2009-04-06 19:36:55 +04:00
|
|
|
{
|
|
|
|
usb_msd_device_c *cdrom;
|
|
|
|
|
|
|
|
if (set) {
|
|
|
|
cdrom = (usb_msd_device_c*) param->get_parent()->get_device_param();
|
|
|
|
if (cdrom != NULL) {
|
2015-08-23 19:09:24 +03:00
|
|
|
if ((val == 1) || !cdrom->get_locked()) {
|
|
|
|
cdrom->s.status_changed = 1;
|
|
|
|
} else if (cdrom->get_locked()) {
|
|
|
|
BX_ERROR(("cdrom tray locked: eject failed"));
|
|
|
|
return BX_INSERTED;
|
2009-04-06 19:36:55 +04:00
|
|
|
}
|
|
|
|
} else {
|
2015-08-23 19:09:24 +03:00
|
|
|
BX_PANIC(("cdrom_status_handler: cdrom not found"));
|
2009-04-06 19:36:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2015-08-29 17:02:13 +03:00
|
|
|
void usb_msd_restore_handler(void *dev, bx_list_c *conf)
|
|
|
|
{
|
|
|
|
((usb_msd_device_c*)dev)->restore_handler(conf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_msd_device_c::restore_handler(bx_list_c *conf)
|
|
|
|
{
|
|
|
|
runtime_config();
|
|
|
|
}
|
|
|
|
|
2007-03-18 14:17:28 +03:00
|
|
|
#endif // BX_SUPPORT_PCI && BX_SUPPORT_PCIUSB
|