blob: b502a34296fc8889925e2112db4641244c6ae071 [file] [log] [blame]
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -07001/*
2 * Copyright 2019 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "pci.hpp"
18
19extern "C" {
20#include <pci/pci.h>
21} // extern "C"
22
Patrick Venture24141612019-05-03 17:59:18 -070023#include <cstring>
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070024#include <optional>
25#include <vector>
26
27namespace host_tool
28{
29
30std::vector<PciDevice>
31 PciUtilImpl::getPciDevices(std::optional<PciFilter> filter)
32{
33 PciFilter test;
34 bool check = false;
35 std::vector<PciDevice> results;
36
37 if (filter.has_value())
38 {
39 test = filter.value();
40 check = true;
41 }
42
43 pci_scan_bus(pacc);
44 struct pci_dev* dev;
45
46 for (dev = pacc->devices; dev; dev = dev->next)
47 {
48 PciDevice item;
49
50 pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS);
51
52 item.bus = dev->bus;
53 item.dev = dev->dev;
54 item.func = dev->func;
55 item.vid = dev->vendor_id;
56 item.did = dev->device_id;
Patrick Venture24141612019-05-03 17:59:18 -070057 std::memcpy(item.bars, dev->base_addr, sizeof(dev->base_addr));
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070058
59 if (check)
60 {
61 if (test.vid == dev->vendor_id && test.did == dev->device_id)
62 {
63 results.push_back(item);
64 }
65 }
66 else
67 {
68 results.push_back(item);
69 }
70 }
71
72 return results;
73}
74
75} // namespace host_tool