Serenity Operating System
at hosted 84 lines 2.8 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/Device.h> 28#include <Kernel/FileSystem/InodeMetadata.h> 29#include <LibC/errno_numbers.h> 30 31namespace Kernel { 32 33static HashMap<u32, Device*>* s_all_devices; 34 35HashMap<u32, Device*>& Device::all_devices() 36{ 37 if (s_all_devices == nullptr) 38 s_all_devices = new HashMap<u32, Device*>; 39 return *s_all_devices; 40} 41 42void Device::for_each(Function<void(Device&)> callback) 43{ 44 for (auto& entry : all_devices()) 45 callback(*entry.value); 46} 47 48Device* Device::get_device(unsigned major, unsigned minor) 49{ 50 auto it = all_devices().find(encoded_device(major, minor)); 51 if (it == all_devices().end()) 52 return nullptr; 53 return it->value; 54} 55 56Device::Device(unsigned major, unsigned minor) 57 : m_major(major) 58 , m_minor(minor) 59{ 60 u32 device_id = encoded_device(major, minor); 61 auto it = all_devices().find(device_id); 62 if (it != all_devices().end()) { 63 dbg() << "Already registered " << major << "," << minor << ": " << it->value->class_name(); 64 } 65 ASSERT(!all_devices().contains(device_id)); 66 all_devices().set(device_id, this); 67} 68 69Device::~Device() 70{ 71 all_devices().remove(encoded_device(m_major, m_minor)); 72} 73 74String Device::absolute_path() const 75{ 76 return String::format("device:%u,%u (%s)", m_major, m_minor, class_name()); 77} 78 79String Device::absolute_path(const FileDescription&) const 80{ 81 return absolute_path(); 82} 83 84}