SerialDevice.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #include <Kernel/Devices/SerialDevice.h>
  27. #include <Kernel/IO.h>
  28. namespace Kernel {
  29. UNMAP_AFTER_INIT SerialDevice::SerialDevice(int base_addr, unsigned minor)
  30. : CharacterDevice(4, minor)
  31. , m_base_addr(base_addr)
  32. {
  33. initialize();
  34. }
  35. UNMAP_AFTER_INIT SerialDevice::~SerialDevice()
  36. {
  37. }
  38. bool SerialDevice::can_read(const FileDescription&, size_t) const
  39. {
  40. return (get_line_status() & DataReady) != 0;
  41. }
  42. KResultOr<size_t> SerialDevice::read(FileDescription&, size_t, UserOrKernelBuffer& buffer, size_t size)
  43. {
  44. if (!size)
  45. return 0;
  46. if (!(get_line_status() & DataReady))
  47. return 0;
  48. ssize_t nwritten = buffer.write_buffered<128>(size, [&](u8* data, size_t data_size) {
  49. for (size_t i = 0; i < data_size; i++)
  50. data[i] = IO::in8(m_base_addr);
  51. return (ssize_t)data_size;
  52. });
  53. if (nwritten < 0)
  54. return KResult((ErrnoCode)-nwritten);
  55. return size;
  56. }
  57. bool SerialDevice::can_write(const FileDescription&, size_t) const
  58. {
  59. return (get_line_status() & EmptyTransmitterHoldingRegister) != 0;
  60. }
  61. KResultOr<size_t> SerialDevice::write(FileDescription&, size_t, const UserOrKernelBuffer& buffer, size_t size)
  62. {
  63. if (!size)
  64. return 0;
  65. if (!(get_line_status() & EmptyTransmitterHoldingRegister))
  66. return 0;
  67. ssize_t nread = buffer.read_buffered<128>(size, [&](const u8* data, size_t data_size) {
  68. for (size_t i = 0; i < data_size; i++)
  69. IO::out8(m_base_addr, data[i]);
  70. return (ssize_t)data_size;
  71. });
  72. if (nread < 0)
  73. return KResult((ErrnoCode)-nread);
  74. return (size_t)nread;
  75. }
  76. String SerialDevice::device_name() const
  77. {
  78. return String::formatted("ttyS{}", minor() - 64);
  79. }
  80. UNMAP_AFTER_INIT void SerialDevice::initialize()
  81. {
  82. set_interrupts(0);
  83. set_baud(Baud38400);
  84. set_line_control(None, One, EightBits);
  85. set_fifo_control(EnableFIFO | ClearReceiveFIFO | ClearTransmitFIFO | TriggerLevel4);
  86. set_modem_control(RequestToSend | DataTerminalReady);
  87. }
  88. UNMAP_AFTER_INIT void SerialDevice::set_interrupts(char interrupt_enable)
  89. {
  90. m_interrupt_enable = interrupt_enable;
  91. IO::out8(m_base_addr + 1, interrupt_enable);
  92. }
  93. void SerialDevice::set_baud(Baud baud)
  94. {
  95. m_baud = baud;
  96. IO::out8(m_base_addr + 3, IO::in8(m_base_addr + 3) | 0x80); // turn on DLAB
  97. IO::out8(m_base_addr + 0, ((char)(baud)) >> 2); // lower half of divisor
  98. IO::out8(m_base_addr + 1, ((char)(baud)) & 0xff); // upper half of divisor
  99. IO::out8(m_base_addr + 3, IO::in8(m_base_addr + 3) & 0x7f); // turn off DLAB
  100. }
  101. void SerialDevice::set_fifo_control(char fifo_control)
  102. {
  103. m_fifo_control = fifo_control;
  104. IO::out8(m_base_addr + 2, fifo_control);
  105. }
  106. void SerialDevice::set_line_control(ParitySelect parity_select, StopBits stop_bits, WordLength word_length)
  107. {
  108. m_parity_select = parity_select;
  109. m_stop_bits = stop_bits;
  110. m_word_length = word_length;
  111. IO::out8(m_base_addr + 3, IO::in8(m_base_addr + 3) & (0xc0 | parity_select | stop_bits | word_length));
  112. }
  113. void SerialDevice::set_break_enable(bool break_enable)
  114. {
  115. m_break_enable = break_enable;
  116. IO::out8(m_base_addr + 3, IO::in8(m_base_addr + 3) & (break_enable ? 0xff : 0xbf));
  117. }
  118. void SerialDevice::set_modem_control(char modem_control)
  119. {
  120. m_modem_control = modem_control;
  121. IO::out8(m_base_addr + 4, modem_control);
  122. }
  123. char SerialDevice::get_line_status() const
  124. {
  125. return IO::in8(m_base_addr + 5);
  126. }
  127. }