DevSerial.cpp revision 85a8039cb441b4ab1462507454334d8d85969194
/* $Id$ */
/** @file
* DevSerial - 16450 UART emulation.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*
* This code is based on:
*
* QEMU 16450 UART emulation
*
* Copyright (c) 2003-2004 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_SERIAL
#include <iprt/semaphore.h>
#include <iprt/critsect.h>
#include "../Builtins.h"
#undef VBOX_SERIAL_PCI /* The PCI variant has lots of problems: wrong IRQ line and wrong IO base assigned. */
#ifdef VBOX_SERIAL_PCI
#endif /* VBOX_SERIAL_PCI */
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
#define SERIAL_SAVED_STATE_VERSION 3
/*
* These are the definitions for the Modem Control Register
*/
/*
* These are the definitions for the Modem Status Register
*/
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Serial device.
*
* @implements PDMIBASE
* @implements PDMICHARPORT
*/
struct SerialState
{
/** Access critical section. */
/** Pointer to the device instance - R3 Ptr. */
/** Pointer to the device instance - R0 Ptr. */
/** Pointer to the device instance - RC Ptr. */
/** LUN\#0: The base interface. */
/** LUN\#0: The character port interface. */
/** Pointer to the attached base driver. */
/** Pointer to the attached character driver. */
/* NOTE: this hidden state is necessary for tx irq generation as
it can be reset while reading iir */
int thr_ipending;
int irq;
bool msr_changed;
bool fGCEnabled;
bool fR0Enabled;
bool fYieldOnLSRRead;
bool afAlignment[4];
int last_break_enable;
#ifdef VBOX_SERIAL_PCI
#endif /* VBOX_SERIAL_PCI */
};
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
#ifdef VBOX_SERIAL_PCI
#define PCIDEV_2_SERIALSTATE(pPciDev) ( (SerialState *)((uintptr_t)(pPciDev) - RT_OFFSETOF(SerialState, dev)) )
#endif /* VBOX_SERIAL_PCI */
#define PDMIBASE_2_SERIALSTATE(pInstance) ( (SerialState *)((uintptr_t)(pInterface) - RT_OFFSETOF(SerialState, IBase)) )
#define PDMICHARPORT_2_SERIALSTATE(pInstance) ( (SerialState *)((uintptr_t)(pInterface) - RT_OFFSETOF(SerialState, ICharPort)) )
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
PDMBOTHCBDECL(int) serialIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
PDMBOTHCBDECL(int) serialIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
#ifdef IN_RING3
static void serial_update_irq(SerialState *s)
{
s->iir = UART_IIR_RDI;
s->iir = UART_IIR_THRI;
s->iir = UART_IIR_RLSI;
} else if (s->lsr & UART_LSR_BI) {
s->iir = 0; /* No special status bit */
} else {
s->iir = UART_IIR_NO_INT;
}
if (s->iir != UART_IIR_NO_INT) {
# ifdef VBOX_SERIAL_PCI
# else /* !VBOX_SERIAL_PCI */
# endif /* !VBOX_SERIAL_PCI */
} else {
# ifdef VBOX_SERIAL_PCI
# else /* !VBOX_SERIAL_PCI */
# endif /* !VBOX_SERIAL_PCI */
}
}
static void serial_update_parameters(SerialState *s)
{
if (s->lcr & 0x08) {
if (s->lcr & 0x10)
parity = 'E';
else
parity = 'O';
} else {
parity = 'N';
}
if (s->lcr & 0x04)
stop_bits = 2;
else
stop_bits = 1;
if (s->divider == 0)
return;
}
#endif /* IN_RING3 */
{
unsigned char ch;
addr &= 7;
#ifndef IN_RING3
NOREF(s);
return VINF_IOM_HC_IOPORT_WRITE;
#else
switch(addr) {
default:
case 0:
if (s->lcr & UART_LCR_DLAB) {
} else if (s->lsr & UART_LSR_THRE) {
s->thr_ipending = 0;
{
}
s->thr_ipending = 1;
} else
Log(("serial: THR not EMPTY!\n"));
break;
case 1:
if (s->lcr & UART_LCR_DLAB) {
} else {
if (s->lsr & UART_LSR_THRE) {
s->thr_ipending = 1;
}
}
break;
case 2:
break;
case 3:
{
int break_enable;
{
}
if (break_enable != s->last_break_enable) {
{
}
}
}
break;
case 4:
{
int rc = s->pDrvChar->pfnSetModemLines(s->pDrvChar, !!(s->mcr & UART_MCR_RTS), !!(s->mcr & UART_MCR_DTR));
}
break;
case 5:
break;
case 6:
break;
case 7:
break;
}
return VINF_SUCCESS;
#endif
}
{
*pRC = VINF_SUCCESS;
addr &= 7;
switch(addr) {
default:
case 0:
if (s->lcr & UART_LCR_DLAB) {
} else {
#ifndef IN_RING3
#else
{
}
#endif
}
break;
case 1:
if (s->lcr & UART_LCR_DLAB) {
} else {
}
break;
case 2:
#ifndef IN_RING3
#else
/* reset THR pending bit */
s->thr_ipending = 0;
/* reset msr changed bit */
s->msr_changed = false;
#endif
break;
case 3:
break;
case 4:
break;
case 5:
{
/* No data available and yielding is enabled, so yield in ring3. */
#ifndef IN_RING3
break;
#else
RTThreadYield ();
#endif
}
break;
case 6:
if (s->mcr & UART_MCR_LOOP) {
/* in loopback, the modem output pins are connected to the
inputs */
} else {
/* Reset delta bits. */
s->msr &= ~UART_MSR_ANY_DELTA;
}
break;
case 7:
break;
}
return ret;
}
#ifdef IN_RING3
static DECLCALLBACK(int) serialNotifyRead(PPDMICHARPORT pInterface, const void *pvBuf, size_t *pcbRead)
{
int rc;
{
/* If a character is still in the read queue, then wait for it to be emptied. */
if (RT_FAILURE(rc))
return rc;
}
{
*pcbRead = 1;
rc = VINF_SUCCESS;
}
else
rc = VERR_TIMEOUT;
return rc;
}
static DECLCALLBACK(int) serialNotifyStatusLinesChanged(PPDMICHARPORT pInterface, uint32_t newStatusLines)
{
/* Set new states. */
newMsr |= UART_MSR_DCD;
newMsr |= UART_MSR_RI;
newMsr |= UART_MSR_DSR;
newMsr |= UART_MSR_CTS;
/* Compare the old and the new states and set the delta bits accordingly. */
newMsr |= UART_MSR_DDCD;
newMsr |= UART_MSR_TERI;
newMsr |= UART_MSR_DDSR;
newMsr |= UART_MSR_DCTS;
pThis->msr_changed = true;
return VINF_SUCCESS;
}
{
if (fFull)
{
}
else
{
}
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
#endif /* IN_RING3 */
/**
* Port I/O Handler for OUT operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument.
* @param Port Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
{
int rc = VINF_SUCCESS;
if (cb == 1)
{
if (rc == VINF_SUCCESS)
{
}
}
else
return rc;
}
/**
* Port I/O Handler for IN operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument.
* @param Port Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
{
int rc = VINF_SUCCESS;
if (cb == 1)
{
if (rc == VINF_SUCCESS)
{
}
}
else
return rc;
}
#ifdef IN_RING3
/**
* @copydoc FNSSMDEVLIVEEXEC
*/
{
return VINF_SSM_DONT_CALL_AGAIN;
}
/**
* @copydoc FNSSMDEVSAVEEXEC
*/
{
}
/**
* @copydoc FNSSMDEVLOADEXEC
*/
{
AssertMsgReturn(uVersion == SERIAL_SAVED_STATE_VERSION, ("%d\n", uVersion), VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION);
if (uPass == SSM_PASS_FINAL)
{
}
if (uPass == SSM_PASS_FINAL)
return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - saved irq=%#x iobase=%#x; configured irq=%#x iobase=%#x"),
if (uPass == SSM_PASS_FINAL)
{
if (RT_FAILURE(rc))
return rc;
{
}
/* this isn't strictly necessary but cannot hurt... */
}
return VINF_SUCCESS;
}
/**
* @copydoc FNPDMDEVRELOCATE
*/
{
}
#ifdef VBOX_SERIAL_PCI
static DECLCALLBACK(int) serialIOPortRegionMap(PPCIDEVICE pPciDev, /* unsigned */ int iRegion, RTGCPHYS GCPhysAddress, uint32_t cb, PCIADDRESSSPACE enmType)
{
int rc = VINF_SUCCESS;
AssertMsg(RT_ALIGN(GCPhysAddress, 8) == GCPhysAddress, ("Expected 8 byte alignment. GCPhysAddress=%#x\n", GCPhysAddress));
/*
* Register our port IO handlers.
*/
return rc;
}
#endif /* VBOX_SERIAL_PCI */
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
{
return NULL;
}
/**
* Destruct a device instance.
*
* Most VM resources are freed by the VM. This callback is provided so that any non-VM
* resources can be freed correctly.
*
* @returns VBox status.
* @param pDevIns The device instance data.
*/
{
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMDEVREG,pfnConstruct}
*/
int iInstance,
{
int rc;
/*
* Initialize the instance data.
* (Do this early or the destructor might choke on something!)
*/
/* IBase */
/* ICharPort */
#ifdef VBOX_SERIAL_PCI
/* the PCI device */
#endif /* VBOX_SERIAL_PCI */
/*
* Validate and read the configuration.
*/
{
AssertMsgFailed(("serialConstruct Invalid configuration values\n"));
}
if (RT_FAILURE(rc))
N_("Configuration error: Failed to get the \"GCEnabled\" value"));
if (RT_FAILURE(rc))
N_("Configuration error: Failed to get the \"R0Enabled\" value"));
if (RT_FAILURE(rc))
N_("Configuration error: Failed to get the \"YieldOnLSRRead\" value"));
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
{
/* Provide sensible defaults. */
if (iInstance == 0)
irq_lvl = 4;
else if (iInstance == 1)
irq_lvl = 3;
else
AssertReleaseFailed(); /* irq_lvl is undefined. */
}
else if (RT_FAILURE(rc))
N_("Configuration error: Failed to get the \"IRQ\" value"));
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
{
if (iInstance == 0)
io_base = 0x3f8;
else if (iInstance == 1)
io_base = 0x2f8;
else
AssertReleaseFailed(); /* io_base is undefined */
}
else if (RT_FAILURE(rc))
N_("Configuration error: Failed to get the \"IOBase\" value"));
#ifdef VBOX_SERIAL_PCI
#else
#endif
/*
* Initialize critical section and the semaphore.
* This must of course be done before attaching drivers or anything else which can call us back..
*/
if (RT_FAILURE(rc))
return rc;
#ifdef VBOX_SERIAL_PCI
/*
* Register the PCI Device and region.
*/
if (RT_FAILURE(rc))
return rc;
if (RT_FAILURE(rc))
return rc;
#else /* !VBOX_SERIAL_PCI */
/*
* Register the I/O ports.
*/
if (RT_FAILURE(rc))
return rc;
if (pThis->fGCEnabled)
{
if (RT_FAILURE(rc))
return rc;
}
if (pThis->fR0Enabled)
{
if (RT_FAILURE(rc))
return rc;
}
#endif /* !VBOX_SERIAL_PCI */
/*
* Saved state.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Attach the char driver and get the interfaces.
* For now no run-time changes are supported.
*/
if (RT_SUCCESS(rc))
{
{
return VERR_PDM_MISSING_INTERFACE;
}
/** @todo provide read notification interface!!!! */
}
else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
{
}
else
{
/* Don't call VMSetError here as we assume that the driver already set an appropriate error */
return rc;
}
return VINF_SUCCESS;
}
/**
* The device registration structure.
*/
const PDMDEVREG g_DeviceSerialPort =
{
/* u32Version */
/* szName */
"serial",
/* szRCMod */
"VBoxDDGC.gc",
/* szR0Mod */
"VBoxDDR0.r0",
/* pszDescription */
"Serial Communication Port",
/* fFlags */
/* fClass */
/* cMaxInstances */
/* cbInstance */
sizeof(SerialState),
/* pfnConstruct */
/* pfnDestruct */
/* pfnRelocate */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnQueryInterface. */
NULL,
/* pfnInitComplete */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32VersionEnd */
};
#endif /* IN_RING3 */
#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */