PCISerialDevice.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Devices/PCISerialDevice.h>
  7. #include <Kernel/Sections.h>
  8. namespace Kernel {
  9. static SerialDevice* s_the = nullptr;
  10. UNMAP_AFTER_INIT void PCISerialDevice::detect()
  11. {
  12. size_t current_device_minor = 68;
  13. PCI::enumerate([&](const PCI::Address& address, PCI::ID id) {
  14. if (address.is_null())
  15. return;
  16. for (auto& board_definition : board_definitions) {
  17. if (board_definition.device_id != id)
  18. continue;
  19. auto bar_base = PCI::get_BAR(address, board_definition.pci_bar) & ~1;
  20. auto port_base = IOAddress(bar_base + board_definition.first_offset);
  21. for (size_t i = 0; i < board_definition.port_count; i++) {
  22. auto serial_device = new SerialDevice(port_base.offset(board_definition.port_size * i), current_device_minor++);
  23. if (board_definition.baud_rate != SerialDevice::Baud::Baud38400) // non-default baud
  24. serial_device->set_baud(board_definition.baud_rate);
  25. // If this is the first port of the first pci serial device, store it as the debug PCI serial port (TODO: Make this configurable somehow?)
  26. if (!is_available())
  27. s_the = serial_device;
  28. // NOTE: We intentionally leak the reference to serial_device here, as it is eternal
  29. }
  30. dmesgln("PCISerialDevice: Found {} @ {}", board_definition.name, address);
  31. return;
  32. }
  33. });
  34. }
  35. SerialDevice& PCISerialDevice::the()
  36. {
  37. VERIFY(s_the);
  38. return *s_the;
  39. }
  40. bool PCISerialDevice::is_available()
  41. {
  42. return s_the;
  43. }
  44. }