Serenity Operating System
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#pragma once
28
29#include <AK/HashMap.h>
30#include <AK/MappedFile.h>
31#include <AK/NonnullOwnPtr.h>
32#include <AK/RefCounted.h>
33#include <AK/RefPtr.h>
34#include <AK/StringView.h>
35
36namespace PCIDB {
37
38struct Subsystem {
39 u16 vendor_id;
40 u16 device_id;
41 StringView name;
42};
43
44struct Device {
45 u16 id;
46 StringView name;
47 HashMap<int, NonnullOwnPtr<Subsystem>> subsystems;
48};
49
50struct Vendor {
51 u16 id;
52 StringView name;
53 HashMap<int, NonnullOwnPtr<Device>> devices;
54};
55
56struct ProgrammingInterface {
57 u8 id { 0 };
58 StringView name {};
59};
60
61struct Subclass {
62 u8 id { 0 };
63 StringView name {};
64 HashMap<int, NonnullOwnPtr<ProgrammingInterface>> programming_interfaces;
65};
66
67struct Class {
68 u8 id { 0 };
69 StringView name {};
70 HashMap<int, NonnullOwnPtr<Subclass>> subclasses;
71};
72
73class Database : public RefCounted<Database> {
74public:
75 static RefPtr<Database> open(const StringView& file_name);
76 static RefPtr<Database> open() { return open("/res/pci.ids"); };
77
78 const StringView get_vendor(u16 vendor_id) const;
79 const StringView get_device(u16 vendor_id, u16 device_id) const;
80 const StringView get_subsystem(u16 vendor_id, u16 device_id, u16 subvendor_id, u16 subdevice_id) const;
81 const StringView get_class(u8 class_id) const;
82 const StringView get_subclass(u8 class_id, u8 subclass_id) const;
83 const StringView get_programming_interface(u8 class_id, u8 subclass_id, u8 programming_interface_id) const;
84
85private:
86 Database(const StringView& file_name)
87 : m_file(file_name) {};
88
89 int init();
90
91 enum ParseMode {
92 UnknownMode,
93 VendorMode,
94 ClassMode,
95 };
96
97 MappedFile m_file {};
98 StringView m_view {};
99 HashMap<int, NonnullOwnPtr<Vendor>> m_vendors;
100 HashMap<int, NonnullOwnPtr<Class>> m_classes;
101 bool m_ready { false };
102};
103
104}