2012-03-17 10:18:03 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
|
2012-03-17 10:18:03 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of Sortix.
|
2012-03-17 10:18:03 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix 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 3 of the License, or (at your option) any later
|
|
|
|
version.
|
2012-03-17 10:18:03 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix 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 General Public License for more
|
|
|
|
details.
|
2012-03-17 10:18:03 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
2012-03-17 10:18:03 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
com.cpp
|
|
|
|
Handles communication to COM serial ports.
|
2012-03-17 10:18:03 -04:00
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2012-03-21 19:52:29 -04:00
|
|
|
#include <sortix/kernel/platform.h>
|
2012-08-01 11:30:34 -04:00
|
|
|
#include <sortix/kernel/kthread.h>
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <sortix/kernel/refcount.h>
|
|
|
|
#include <sortix/kernel/ioctx.h>
|
|
|
|
#include <sortix/kernel/inode.h>
|
|
|
|
#include <sortix/kernel/descriptor.h>
|
|
|
|
#include <sortix/kernel/interlock.h>
|
2013-01-09 17:30:36 -05:00
|
|
|
#include <sortix/kernel/interrupt.h>
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <sortix/stat.h>
|
2013-01-09 17:30:36 -05:00
|
|
|
|
2012-09-22 10:44:50 -04:00
|
|
|
#include <errno.h>
|
2013-01-09 17:30:36 -05:00
|
|
|
|
2012-03-17 10:18:03 -04:00
|
|
|
#include "thread.h"
|
Multithreaded kernel and improvement of signal handling.
Pardon the big ass-commit, this took months to develop and debug and the
refactoring got so far that a clean merge became impossible. The good news
is that this commit does quite a bit of cleaning up and generally improves
the kernel quality.
This makes the kernel fully pre-emptive and multithreaded. This was done
by rewriting the interrupt code, the scheduler, introducing new threading
primitives, and rewriting large parts of the kernel. During the past few
commits the kernel has had its device drivers thread secured; this commit
thread secures large parts of the core kernel. There still remains some
parts of the kernel that is _not_ thread secured, but this is not a problem
at this point. Each user-space thread has an associated kernel stack that
it uses when it goes into kernel mode. This stack is by default 8 KiB since
that value works for me and is also used by Linux. Strange things tends to
happen on x86 in case of a stack overflow - there is no ideal way to catch
such a situation right now.
The system call conventions were changed, too. The %edx register is now
used to provide the errno value of the call, instead of the kernel writing
it into a registered global variable. The system call code has also been
updated to better reflect the native calling conventions: not all registers
have to be preserved. This makes system calls faster and simplifies the
assembly. In the kernel, there is no longer the event.h header or the hacky
method of 'resuming system calls' that closely resembles cooperative
multitasking. If a system call wants to block, it should just block.
The signal handling was also improved significantly. At this point, signals
cannot interrupt kernel threads (but can always interrupt user-space threads
if enabled), which introduces some problems with how a SIGINT could
interrupt a blocking read, for instance. This commit introduces and uses a
number of new primitives such as kthread_lock_mutex_signal() that attempts
to get the lock but fails if a signal is pending. In this manner, the kernel
is safer as kernel threads cannot be shut down inconveniently, but in return
for complexity as blocking operations must check they if they should fail.
Process exiting has also been refactored significantly. The _exit(2) system
call sets the exit code and sends SIGKILL to all the threads in the process.
Once all the threads have cleaned themselves up and exited, a worker thread
calls the process's LastPrayer() method that unmaps memory, deletes the
address space, notifies the parent, etc. This provides a very robust way to
terminate processes as even half-constructed processes (during a failing fork
for instance) can be gracefully terminated.
I have introduced a number of kernel threads to help avoid threading problems
and simplify kernel design. For instance, there is now a functional generic
kernel worker thread that any kernel thread can schedule jobs for. Interrupt
handlers run with interrupts off (hence they cannot call kthread_ functions
as it may deadlock the system if another thread holds the lock) therefore
they cannot use the standard kernel worker threads. Instead, they use a
special purpose interrupt worker thread that works much like the generic one
expect that interrupt handlers can safely queue work with interrupts off.
Note that this also means that interrupt handlers cannot allocate memory or
print to the kernel log/screen as such mechanisms uses locks. I'll introduce
a lock free algorithm for such cases later on.
The boot process has also changed. The original kernel init thread in
kernel.cpp creates a new bootstrap thread and becomes the system idle thread.
Note that pid=0 now means the kernel, as there is no longer a system idle
process. The bootstrap thread launches all the kernel worker threads and then
creates a new process and loads /bin/init into it and then creates a thread
in pid=1, which starts the system. The bootstrap thread then quietly waits
for pid=1 to exit after which it shuts down/reboots/panics the system.
In general, the introduction of race conditions and dead locks have forced me
to revise a lot of the design and make sure it was thread secure. Since early
parts of the kernel was quite hacky, I had to refactor such code. So it seems
that the risk of dead locks forces me to write better code.
Note that a real preemptive multithreaded kernel simplifies the construction
of blocking system calls. My hope is that this will trigger a clean up of
the filesystem code that current is almost beyond repair.
Almost all of the kernel was modified during this refactoring. To the extent
possible, these changes have been backported to older non-multithreaded
kernel, but many changes were tightly coupled and went into this commit.
Of interest is the implementation of the kthread_ api based on the design
of pthreads; this library allows easy synchronization mechanisms and
includes C++-style scoped locks. This commit also introduces new worker
threads and tested mechanisms for interrupt handlers to schedule work in a
kernel worker thread.
A lot of code have been rewritten from scratch and has become a lot more
stable and correct.
Share and enjoy!
2012-08-01 11:30:34 -04:00
|
|
|
#include "signal.h"
|
2012-03-17 10:18:03 -04:00
|
|
|
#include "com.h"
|
|
|
|
|
|
|
|
namespace Sortix {
|
|
|
|
namespace COM {
|
|
|
|
|
|
|
|
// It appears this code is unable to get interrupts working correctly. Somehow
|
|
|
|
// we don't get interrupts upon receiving data, at least under VirtualBox. This
|
|
|
|
// hack changes the code such that it polls occasionally instead. Hopefully this
|
|
|
|
// won't cause data loss, but I suspect that it will.
|
|
|
|
// TODO: It appears that this code causes kernel instability, possibly due to
|
|
|
|
// the broken way blocking system calls are implemented in Sortix.
|
|
|
|
#define POLL_HACK 1
|
|
|
|
|
|
|
|
// Another alternative is to use the polling code in a completely blocking
|
|
|
|
// manner. While this may give nicer transfer speeds and less data loss, it
|
|
|
|
// locks up the whole system.
|
|
|
|
#define POLL_BLOCKING 0
|
|
|
|
|
|
|
|
// Yet another alternative is to use POLL_HACK, but return EGAIN and let user-
|
|
|
|
// space call retry, rather than relying on the broken syscall interstructure.
|
|
|
|
#define POLL_EAGAIN 1
|
2012-08-01 11:30:34 -04:00
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
#if !POLL_EAGAIN && !POLL_HACK
|
2012-08-01 11:30:34 -04:00
|
|
|
#error The interrupt-based code was broken in the kthread branch.
|
|
|
|
#error You need to port this to the new thread/interrupt API.
|
|
|
|
#warning Oh, and fix the above mentioned bugs too.
|
|
|
|
#endif
|
2012-03-17 10:18:03 -04:00
|
|
|
|
|
|
|
const uint16_t TXR = 0; // Transmit register
|
|
|
|
const uint16_t RXR = 0; // Receive register
|
|
|
|
const uint16_t IER = 1; // Interrupt Enable
|
|
|
|
const uint16_t IIR = 2; // Interrupt ID
|
|
|
|
const uint16_t FCR = 2; // FIFO control
|
|
|
|
const uint16_t LCR = 3; // Line control
|
|
|
|
const uint16_t MCR = 4; // Modem control
|
|
|
|
const uint16_t LSR = 5; // Line Status
|
|
|
|
const uint16_t MSR = 6; // Modem Status
|
|
|
|
const uint16_t SCR = 7; // Scratch Register
|
|
|
|
const uint16_t DLL = 0; // Divisor Latch Low
|
|
|
|
const uint16_t DLM = 1; // Divisor latch High
|
|
|
|
|
|
|
|
const uint8_t LCR_DLAB = 0x80; // Divisor latch access bit
|
|
|
|
const uint8_t LCR_SBC = 0x40; // Set break control
|
|
|
|
const uint8_t LCR_SPAR = 0x20; // Stick parity (?)
|
|
|
|
const uint8_t LCR_EPAR = 0x10; // Even parity select
|
|
|
|
const uint8_t LCR_PARITY = 0x08; // Parity Enable
|
|
|
|
const uint8_t LCR_STOP = 0x04; // Stop bits: 0=1 bit, 1=2 bits
|
|
|
|
const uint8_t LCR_WLEN5 = 0x00; // Wordlength: 5 bits
|
|
|
|
const uint8_t LCR_WLEN6 = 0x01; // Wordlength: 6 bits
|
|
|
|
const uint8_t LCR_WLEN7 = 0x02; // Wordlength: 7 bits
|
|
|
|
const uint8_t LCR_WLEN8 = 0x03; // Wordlength: 8 bits
|
|
|
|
|
|
|
|
const uint8_t LSR_TEMT = 0x40; // Transmitter empty
|
|
|
|
const uint8_t LSR_THRE = 0x20; // Transmit-hold-register empty
|
|
|
|
const uint8_t LSR_READY = 0x1; // Data received
|
|
|
|
const uint8_t LSR_BOTH_EMPTY = LSR_TEMT | LSR_THRE;
|
|
|
|
|
|
|
|
const uint8_t IIR_NO_INTERRUPT = (1U<<0U);
|
|
|
|
const uint8_t IIR_INTERRUPT_TYPE = ((1U<<1U) | (1U<<2U) | (1U<<3U));
|
|
|
|
const uint8_t IIR_TIMEOUT = ((1U<<2U) | (1U<<3U));
|
|
|
|
const uint8_t IIR_RECV_LINE_STATUS = ((1U<<1U) | (1U<<2U));
|
|
|
|
const uint8_t IIR_RECV_DATA = (1U<<2U);
|
|
|
|
const uint8_t IIR_SENT_DATA = (1U<<1U);
|
|
|
|
const uint8_t IIR_MODEM_STATUS = 0;
|
|
|
|
|
|
|
|
const uint8_t IER_DATA = (1U<<0U);
|
|
|
|
const uint8_t IER_SENT = (1U<<1U);
|
|
|
|
const uint8_t IER_LINE_STATUS = (1U<<2U);
|
|
|
|
const uint8_t IER_MODEM_STATUS = (1U<<3U);
|
|
|
|
const uint8_t IER_SLEEP_MODE = (1U<<4U);
|
|
|
|
const uint8_t IER_LOW_POWER = (1U<<5U);
|
|
|
|
|
|
|
|
const unsigned BASE_BAUD = 1843200/16;
|
|
|
|
|
|
|
|
const unsigned UART8250 = 1;
|
|
|
|
const unsigned UART16450 = 2;
|
|
|
|
const unsigned UART16550 = 3;
|
|
|
|
const unsigned UART16550A = 4;
|
|
|
|
const unsigned UART16750 = 5;
|
|
|
|
|
|
|
|
const size_t NUMCOMPORTS = 4;
|
|
|
|
|
|
|
|
// The IO base ports of each COM port.
|
|
|
|
static uint16_t comports[1+NUMCOMPORTS];
|
|
|
|
|
|
|
|
// The results of running HardwareProbe on each COM port.
|
|
|
|
unsigned hwversion[1+NUMCOMPORTS];
|
|
|
|
|
|
|
|
// Uses various characteristics of the UART chips to determine the hardware.
|
|
|
|
static unsigned HardwareProbe(uint16_t port)
|
|
|
|
{
|
|
|
|
// Set the value "0xE7" to the FCR to test the status of the FIFO flags.
|
|
|
|
CPU::OutPortB(port + FCR, 0xE7);
|
|
|
|
uint8_t iir = CPU::InPortB(port + IIR);
|
|
|
|
if ( iir & (1U<<6U) )
|
|
|
|
{
|
|
|
|
if ( iir & (1<<7U) )
|
|
|
|
{
|
|
|
|
return (iir & (1U<<5U)) ? UART16750 : UART16550A;
|
|
|
|
}
|
|
|
|
return UART16550;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if the scratch register returns what we write into it. The 8520
|
|
|
|
// doesn't do it. This is technically undefined behavior, but it is useful
|
|
|
|
// to detect hardware versions.
|
|
|
|
uint16_t anyvalue = 0x2A;
|
|
|
|
CPU::OutPortB(port + SCR, anyvalue);
|
|
|
|
return CPU::InPortB(port + SCR) == anyvalue ? UART16450 : UART8250;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void WaitForEmptyBuffers(uint16_t port)
|
|
|
|
{
|
|
|
|
while ( (CPU::InPortB(port + LSR) & LSR_BOTH_EMPTY) != LSR_BOTH_EMPTY ) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsLineReady(uint16_t port)
|
|
|
|
{
|
|
|
|
return CPU::InPortB(port + LSR) & LSR_READY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool CanWriteByte(uint16_t port)
|
|
|
|
{
|
|
|
|
return CPU::InPortB(port + LSR) & LSR_THRE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t ReadBlocking(uint16_t port, void* buf, size_t size)
|
|
|
|
{
|
|
|
|
if ( SSIZE_MAX < size ) { size = SSIZE_MAX; }
|
|
|
|
uint8_t* buffer = (uint8_t*) buf;
|
|
|
|
uint8_t interruptsenabled = CPU::InPortB(port + IER);
|
|
|
|
CPU::OutPortB(port + IER, 0);
|
|
|
|
|
|
|
|
for ( size_t i = 0; i < size; i++ )
|
|
|
|
{
|
|
|
|
while ( !IsLineReady(port) ) { }
|
|
|
|
buffer[i] = CPU::InPortB(port + RXR);
|
|
|
|
}
|
|
|
|
|
|
|
|
WaitForEmptyBuffers(port);
|
|
|
|
CPU::OutPortB(port + IER, interruptsenabled);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t WriteBlocking(uint16_t port, const void* buf, size_t size)
|
|
|
|
{
|
|
|
|
if ( SSIZE_MAX < size ) { size = SSIZE_MAX; }
|
|
|
|
const uint8_t* buffer = (const uint8_t*) buf;
|
|
|
|
uint8_t interruptsenabled = CPU::InPortB(port + IER);
|
|
|
|
CPU::OutPortB(port + IER, 0);
|
|
|
|
|
|
|
|
for ( size_t i = 0; i < size; i++ )
|
|
|
|
{
|
|
|
|
while ( !CanWriteByte(port) ) { }
|
|
|
|
CPU::OutPortB(port + TXR, buffer[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
WaitForEmptyBuffers(port);
|
|
|
|
CPU::OutPortB(port + IER, interruptsenabled);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EarlyInit()
|
|
|
|
{
|
|
|
|
// We can fetch COM port information from the BIOS Data Area.
|
|
|
|
volatile uint16_t* const bioscomports = (uint16_t* const) 0x0400UL;
|
|
|
|
|
|
|
|
for ( size_t i = 1; i <= NUMCOMPORTS; i++ )
|
|
|
|
{
|
|
|
|
comports[i] = bioscomports[i-1];
|
|
|
|
if ( !comports[i] ) { continue; }
|
|
|
|
hwversion[i] = HardwareProbe(comports[i]);
|
|
|
|
CPU::OutPortB(comports[i] + IER, 0x0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
class DevCOMPort : public AbstractInode
|
2012-03-17 10:18:03 -04:00
|
|
|
{
|
|
|
|
public:
|
2012-08-07 18:19:44 -04:00
|
|
|
DevCOMPort(dev_t dev, uid_t owner, gid_t group, mode_t mode, uint16_t port);
|
2012-03-17 10:18:03 -04:00
|
|
|
virtual ~DevCOMPort();
|
2012-08-07 18:19:44 -04:00
|
|
|
virtual int sync(ioctx_t* ctx);
|
|
|
|
virtual ssize_t read(ioctx_t* ctx, uint8_t* buf, size_t count);
|
|
|
|
virtual ssize_t write(ioctx_t* ctx, const uint8_t* buf, size_t count);
|
2012-03-17 10:18:03 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
void OnInterrupt();
|
|
|
|
|
|
|
|
private:
|
2012-08-01 11:30:34 -04:00
|
|
|
kthread_mutex_t portlock;
|
2012-08-07 18:19:44 -04:00
|
|
|
uint16_t port;
|
2012-03-17 10:18:03 -04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
DevCOMPort::DevCOMPort(dev_t dev, uid_t owner, gid_t group, mode_t mode,
|
|
|
|
uint16_t port)
|
2012-03-17 10:18:03 -04:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
inode_type = INODE_TYPE_STREAM;
|
2012-03-17 10:18:03 -04:00
|
|
|
this->port = port;
|
2012-08-01 11:30:34 -04:00
|
|
|
this->portlock = KTHREAD_MUTEX_INITIALIZER;
|
2012-08-07 18:19:44 -04:00
|
|
|
this->stat_uid = owner;
|
|
|
|
this->stat_gid = group;
|
|
|
|
this->type = S_IFCHR;
|
|
|
|
this->stat_mode = (mode & S_SETABLE) | this->type;
|
|
|
|
this->dev = dev;
|
|
|
|
this->ino = (ino_t) this;
|
2012-03-17 10:18:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DevCOMPort::~DevCOMPort()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
int DevCOMPort::sync(ioctx_t* /*ctx*/)
|
|
|
|
{
|
|
|
|
// TODO: Not implemented yet, please wait for all outstanding requests.
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-17 10:18:03 -04:00
|
|
|
|
|
|
|
#if POLL_HACK
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
ssize_t DevCOMPort::read(ioctx_t* ctx, uint8_t* dest, size_t count)
|
2012-03-17 10:18:03 -04:00
|
|
|
{
|
|
|
|
if ( !count ) { return 0; }
|
|
|
|
if ( SSIZE_MAX < count ) { count = SSIZE_MAX; }
|
2012-08-01 11:30:34 -04:00
|
|
|
ScopedLock lock(&portlock);
|
2012-03-17 10:18:03 -04:00
|
|
|
|
2012-08-01 11:30:34 -04:00
|
|
|
while ( !(CPU::InPortB(port + LSR) & LSR_READY) )
|
|
|
|
if ( Signal::IsPending() )
|
|
|
|
{
|
2012-09-22 10:44:50 -04:00
|
|
|
errno = EINTR;
|
2012-08-01 11:30:34 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-03-17 10:18:03 -04:00
|
|
|
|
|
|
|
size_t sofar = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if ( count <= sofar ) { break; }
|
2012-08-07 18:19:44 -04:00
|
|
|
uint8_t val = CPU::InPortB(port + RXR);
|
|
|
|
if ( !ctx->copy_to_dest(dest + sofar++, &val, sizeof(val)) )
|
|
|
|
return -1;
|
2012-03-17 10:18:03 -04:00
|
|
|
} while ( CPU::InPortB(port + LSR) & LSR_READY);
|
|
|
|
|
|
|
|
return sofar;
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
ssize_t DevCOMPort::write(ioctx_t* ctx, const uint8_t* src, size_t count)
|
2012-03-17 10:18:03 -04:00
|
|
|
{
|
|
|
|
if ( !count ) { return 0; }
|
|
|
|
if ( SSIZE_MAX < count ) { count = SSIZE_MAX; };
|
|
|
|
|
2012-08-01 11:30:34 -04:00
|
|
|
ScopedLock lock(&portlock);
|
|
|
|
|
|
|
|
while ( !(CPU::InPortB(port + LSR) & LSR_THRE) )
|
|
|
|
if ( Signal::IsPending() )
|
|
|
|
{
|
2012-09-22 10:44:50 -04:00
|
|
|
errno = EINTR;
|
2012-08-01 11:30:34 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-03-17 10:18:03 -04:00
|
|
|
|
|
|
|
size_t sofar = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if ( count <= sofar ) { break; }
|
2012-08-07 18:19:44 -04:00
|
|
|
uint8_t val;
|
|
|
|
if ( !ctx->copy_from_src(&val, src + sofar++, sizeof(val)) )
|
|
|
|
return -1;
|
|
|
|
CPU::OutPortB(port + TXR, val);
|
2012-03-17 10:18:03 -04:00
|
|
|
} while ( CPU::InPortB(port + LSR) & LSR_THRE );
|
|
|
|
|
|
|
|
return sofar;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
#error Yeah, please port these to the new IO interface.
|
|
|
|
|
|
|
|
ssize_t DevCOMPort::Read(byte* dest, size_t count)
|
2012-03-17 10:18:03 -04:00
|
|
|
{
|
|
|
|
if ( !count ) { return 0; }
|
|
|
|
if ( SSIZE_MAX < count ) { count = SSIZE_MAX; }
|
|
|
|
#if POLL_BLOCKING
|
|
|
|
return ReadBlocking(port, dest, 1);
|
|
|
|
#endif
|
|
|
|
uint8_t lsr = CPU::InPortB(port + LSR);
|
|
|
|
if ( !(lsr & LSR_READY) )
|
|
|
|
{
|
2012-08-01 11:30:34 -04:00
|
|
|
Panic("Can't wait for com data receive event");
|
2012-08-07 18:19:44 -04:00
|
|
|
Error::Set(EBLOCKING);
|
2012-03-17 10:18:03 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t sofar = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if ( count <= sofar ) { break; }
|
|
|
|
dest[sofar++] = CPU::InPortB(port + RXR);
|
|
|
|
} while ( CPU::InPortB(port + LSR) & LSR_READY);
|
|
|
|
|
|
|
|
return sofar;
|
|
|
|
}
|
|
|
|
|
2012-09-21 13:25:22 -04:00
|
|
|
ssize_t DevCOMPort::Write(const uint8_t* src, size_t count)
|
2012-03-17 10:18:03 -04:00
|
|
|
{
|
|
|
|
if ( !count ) { return 0; }
|
|
|
|
if ( SSIZE_MAX < count ) { count = SSIZE_MAX; };
|
|
|
|
#if POLL_BLOCKING
|
|
|
|
return WriteBlocking(port, src, 1);
|
|
|
|
#endif
|
|
|
|
uint8_t lsr = CPU::InPortB(port + LSR);
|
|
|
|
if ( !(lsr & LSR_THRE) )
|
|
|
|
{
|
2012-08-01 11:30:34 -04:00
|
|
|
Panic("Can't wait for com data sent event");
|
2012-08-07 18:19:44 -04:00
|
|
|
Error::Set(EBLOCKING);
|
2012-03-17 10:18:03 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t sofar = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if ( count <= sofar ) { break; }
|
|
|
|
CPU::OutPortB(port + TXR, src[sofar++]);
|
|
|
|
} while ( CPU::InPortB(port + LSR) & LSR_THRE );
|
|
|
|
|
|
|
|
return sofar;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void DevCOMPort::OnInterrupt()
|
|
|
|
{
|
|
|
|
#if POLL_HACK || POLL_BLOCKING
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uint8_t iir = CPU::InPortB(port + IIR);
|
|
|
|
if ( iir & IIR_NO_INTERRUPT ) { return; }
|
|
|
|
uint8_t intrtype = iir & IIR_INTERRUPT_TYPE;
|
|
|
|
switch ( intrtype )
|
|
|
|
{
|
|
|
|
case IIR_TIMEOUT:
|
|
|
|
CPU::InPortB(port + RXR);
|
|
|
|
break;
|
|
|
|
case IIR_RECV_LINE_STATUS:
|
|
|
|
// TODO: Proper error handling!
|
|
|
|
CPU::InPortB(port + LSR);
|
|
|
|
break;
|
|
|
|
case IIR_RECV_DATA:
|
2012-08-01 11:30:34 -04:00
|
|
|
Panic("Can't wait for com data sent event");
|
2012-03-17 10:18:03 -04:00
|
|
|
break;
|
|
|
|
case IIR_SENT_DATA:
|
2012-08-01 11:30:34 -04:00
|
|
|
Panic("Can't wait for com data sent event");
|
2012-03-17 10:18:03 -04:00
|
|
|
CPU::InPortB(port + IIR);
|
|
|
|
break;
|
|
|
|
case IIR_MODEM_STATUS:
|
|
|
|
CPU::InPortB(port + MSR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
Ref<DevCOMPort> comdevices[1+NUMCOMPORTS];
|
2012-03-17 10:18:03 -04:00
|
|
|
|
|
|
|
static void UARTIRQHandler(CPU::InterruptRegisters* /*regs*/, void* /*user*/)
|
|
|
|
{
|
|
|
|
for ( size_t i = 1; i <= NUMCOMPORTS; i++ )
|
|
|
|
{
|
|
|
|
if ( !comdevices[i] ) { continue; }
|
|
|
|
comdevices[i]->OnInterrupt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
void Init(const char* devpath, Ref<Descriptor> slashdev)
|
2012-03-17 10:18:03 -04:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
ioctx_t ctx; SetupKernelIOCtx(&ctx);
|
2012-03-17 10:18:03 -04:00
|
|
|
for ( size_t i = 1; i <= NUMCOMPORTS; i++ )
|
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( !comports[i] ) { comdevices[i] = Ref<DevCOMPort>(); continue; }
|
|
|
|
comdevices[i] = Ref<DevCOMPort>
|
|
|
|
(new DevCOMPort(slashdev->dev, 0, 0, 0660, comports[i]));
|
2012-03-17 10:18:03 -04:00
|
|
|
if ( !comdevices[i] )
|
|
|
|
{
|
|
|
|
PanicF("Unable to allocate device for COM port %zu at 0x%x", i,
|
|
|
|
comports[i]);
|
|
|
|
}
|
|
|
|
char name[5] = "comN";
|
|
|
|
name[3] = '0' + i;
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( LinkInodeInDir(&ctx, slashdev, name, comdevices[i]) != 0 )
|
|
|
|
PanicF("Unable to link %s/%s to COM port driver.", devpath, name);
|
2012-03-17 10:18:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Interrupt::RegisterHandler(Interrupt::IRQ3, UARTIRQHandler, NULL);
|
|
|
|
Interrupt::RegisterHandler(Interrupt::IRQ4, UARTIRQHandler, NULL);
|
|
|
|
|
|
|
|
// Initialize the ports so we can transfer data.
|
|
|
|
for ( size_t i = 1; i <= NUMCOMPORTS; i++ )
|
|
|
|
{
|
|
|
|
uint16_t port = comports[i];
|
|
|
|
if ( !port ) { continue; }
|
|
|
|
#if POLL_HACK || POLL_BLOCKING
|
|
|
|
uint8_t interrupts = 0;
|
|
|
|
#else
|
|
|
|
uint8_t interrupts = IER_DATA
|
|
|
|
| IER_SENT
|
|
|
|
| IER_LINE_STATUS
|
|
|
|
| IER_MODEM_STATUS;
|
|
|
|
#endif
|
|
|
|
CPU::OutPortB(port + FCR, 0);
|
|
|
|
CPU::OutPortB(port + LCR, 0x80);
|
|
|
|
CPU::OutPortB(port + DLL, 0xC);
|
|
|
|
CPU::OutPortB(port + DLM, 0x0);
|
|
|
|
CPU::OutPortB(port + LCR, 0x3); // 8n1
|
|
|
|
CPU::OutPortB(port + MCR, 0x3); // DTR + RTS
|
|
|
|
CPU::OutPortB(port + IER, interrupts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace COM
|
|
|
|
} // namespace Sortix
|