Device.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <AK/Singleton.h>
  27. #include <Kernel/Devices/Device.h>
  28. #include <Kernel/FileSystem/InodeMetadata.h>
  29. #include <LibC/errno_numbers.h>
  30. namespace Kernel {
  31. static AK::Singleton<HashMap<u32, Device*>> s_all_devices;
  32. HashMap<u32, Device*>& Device::all_devices()
  33. {
  34. return *s_all_devices;
  35. }
  36. void Device::for_each(Function<void(Device&)> callback)
  37. {
  38. for (auto& entry : all_devices())
  39. callback(*entry.value);
  40. }
  41. Device* Device::get_device(unsigned major, unsigned minor)
  42. {
  43. auto it = all_devices().find(encoded_device(major, minor));
  44. if (it == all_devices().end())
  45. return nullptr;
  46. return it->value;
  47. }
  48. Device::Device(unsigned major, unsigned minor)
  49. : m_major(major)
  50. , m_minor(minor)
  51. {
  52. u32 device_id = encoded_device(major, minor);
  53. auto it = all_devices().find(device_id);
  54. if (it != all_devices().end()) {
  55. dbgln("Already registered {},{}: {}", major, minor, it->value->class_name());
  56. }
  57. VERIFY(!all_devices().contains(device_id));
  58. all_devices().set(device_id, this);
  59. }
  60. Device::~Device()
  61. {
  62. all_devices().remove(encoded_device(m_major, m_minor));
  63. }
  64. String Device::absolute_path() const
  65. {
  66. return String::formatted("device:{},{} ({})", m_major, m_minor, class_name());
  67. }
  68. String Device::absolute_path(const FileDescription&) const
  69. {
  70. return absolute_path();
  71. }
  72. void Device::process_next_queued_request(Badge<AsyncDeviceRequest>, const AsyncDeviceRequest& completed_request)
  73. {
  74. AsyncDeviceRequest* next_request = nullptr;
  75. {
  76. ScopedSpinLock lock(m_requests_lock);
  77. VERIFY(!m_requests.is_empty());
  78. VERIFY(m_requests.first().ptr() == &completed_request);
  79. m_requests.remove(m_requests.begin());
  80. if (!m_requests.is_empty())
  81. next_request = m_requests.first().ptr();
  82. }
  83. if (next_request)
  84. next_request->start();
  85. evaluate_block_conditions();
  86. }
  87. }