SerialDevice.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. SerialDevice::SerialDevice(int base_addr, unsigned minor)
  30. : CharacterDevice(4, minor)
  31. , m_base_addr(base_addr)
  32. {
  33. initialize();
  34. }
  35. 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(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(nread);
  74. return (size_t)nread;
  75. }
  76. void SerialDevice::initialize()
  77. {
  78. set_interrupts(0);
  79. set_baud(Baud38400);
  80. set_line_control(None, One, EightBits);
  81. set_fifo_control(EnableFIFO | ClearReceiveFIFO | ClearTransmitFIFO | TriggerLevel4);
  82. set_modem_control(RequestToSend | DataTerminalReady);
  83. }
  84. void SerialDevice::set_interrupts(char interrupt_enable)
  85. {
  86. m_interrupt_enable = interrupt_enable;
  87. IO::out8(m_base_addr + 1, interrupt_enable);
  88. }
  89. void SerialDevice::set_baud(Baud baud)
  90. {
  91. m_baud = baud;
  92. IO::out8(m_base_addr + 3, IO::in8(m_base_addr + 3) | 0x80); // turn on DLAB
  93. IO::out8(m_base_addr + 0, ((char)(baud)) >> 2); // lower half of divisor
  94. IO::out8(m_base_addr + 1, ((char)(baud)) & 0xff); // upper half of divisor
  95. IO::out8(m_base_addr + 3, IO::in8(m_base_addr + 3) & 0x7f); // turn off DLAB
  96. }
  97. void SerialDevice::set_fifo_control(char fifo_control)
  98. {
  99. m_fifo_control = fifo_control;
  100. IO::out8(m_base_addr + 2, fifo_control);
  101. }
  102. void SerialDevice::set_line_control(ParitySelect parity_select, StopBits stop_bits, WordLength word_length)
  103. {
  104. m_parity_select = parity_select;
  105. m_stop_bits = stop_bits;
  106. m_word_length = word_length;
  107. IO::out8(m_base_addr + 3, IO::in8(m_base_addr + 3) & (0xc0 | parity_select | stop_bits | word_length));
  108. }
  109. void SerialDevice::set_break_enable(bool break_enable)
  110. {
  111. m_break_enable = break_enable;
  112. IO::out8(m_base_addr + 3, IO::in8(m_base_addr + 3) & (break_enable ? 0xff : 0xbf));
  113. }
  114. void SerialDevice::set_modem_control(char modem_control)
  115. {
  116. m_modem_control = modem_control;
  117. IO::out8(m_base_addr + 4, modem_control);
  118. }
  119. char SerialDevice::get_line_status() const
  120. {
  121. return IO::in8(m_base_addr + 5);
  122. }
  123. }