Serenity Operating System
at hosted 140 lines 4.1 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <Kernel/Devices/SerialDevice.h> 28#include <LibBareMetal/IO.h> 29 30namespace Kernel { 31 32SerialDevice::SerialDevice(int base_addr, unsigned minor) 33 : CharacterDevice(4, minor) 34 , m_base_addr(base_addr) 35{ 36 initialize(); 37} 38 39SerialDevice::~SerialDevice() 40{ 41} 42 43bool SerialDevice::can_read(const FileDescription&) const 44{ 45 return (get_line_status() & DataReady) != 0; 46} 47 48ssize_t SerialDevice::read(FileDescription&, u8* buffer, ssize_t size) 49{ 50 if (!size) 51 return 0; 52 53 if (!(get_line_status() & DataReady)) 54 return 0; 55 56 buffer[0] = IO::in8(m_base_addr); 57 58 return 1; 59} 60 61bool SerialDevice::can_write(const FileDescription&) const 62{ 63 return (get_line_status() & EmptyTransmitterHoldingRegister) != 0; 64} 65 66ssize_t SerialDevice::write(FileDescription&, const u8* buffer, ssize_t size) 67{ 68 if (!size) 69 return 0; 70 71 if (!(get_line_status() & EmptyTransmitterHoldingRegister)) 72 return 0; 73 74 IO::out8(m_base_addr, buffer[0]); 75 76 return 1; 77} 78 79void SerialDevice::initialize() 80{ 81 set_interrupts(0); 82 set_baud(Baud38400); 83 set_line_control(None, One, EightBits); 84 set_fifo_control(EnableFIFO | ClearReceiveFIFO | ClearTransmitFIFO | TriggerLevel4); 85 set_modem_control(RequestToSend | DataTerminalReady); 86} 87 88void SerialDevice::set_interrupts(char interrupt_enable) 89{ 90 m_interrupt_enable = interrupt_enable; 91 92 IO::out8(m_base_addr + 1, interrupt_enable); 93} 94 95void SerialDevice::set_baud(Baud baud) 96{ 97 m_baud = baud; 98 99 IO::out8(m_base_addr + 3, IO::in8(m_base_addr + 3) | 0x80); // turn on DLAB 100 IO::out8(m_base_addr + 0, ((char)(baud)) >> 2); // lower half of divisor 101 IO::out8(m_base_addr + 1, ((char)(baud)) & 0xff); // upper half of divisor 102 IO::out8(m_base_addr + 3, IO::in8(m_base_addr + 3) & 0x7f); // turn off DLAB 103} 104 105void SerialDevice::set_fifo_control(char fifo_control) 106{ 107 m_fifo_control = fifo_control; 108 109 IO::out8(m_base_addr + 2, fifo_control); 110} 111 112void SerialDevice::set_line_control(ParitySelect parity_select, StopBits stop_bits, WordLength word_length) 113{ 114 m_parity_select = parity_select; 115 m_stop_bits = stop_bits; 116 m_word_length = word_length; 117 118 IO::out8(m_base_addr + 3, IO::in8(m_base_addr + 3) & (0xc0 | parity_select | stop_bits | word_length)); 119} 120 121void SerialDevice::set_break_enable(bool break_enable) 122{ 123 m_break_enable = break_enable; 124 125 IO::out8(m_base_addr + 3, IO::in8(m_base_addr + 3) & (break_enable ? 0xff : 0xbf)); 126} 127 128void SerialDevice::set_modem_control(char modem_control) 129{ 130 m_modem_control = modem_control; 131 132 IO::out8(m_base_addr + 4, modem_control); 133} 134 135char SerialDevice::get_line_status() const 136{ 137 return IO::in8(m_base_addr + 5); 138} 139 140}