Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | extern "C" { |
| 20 | #include <pci/pci.h> |
| 21 | } // extern "C" |
| 22 | |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 23 | #include <cstring> |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 24 | #include <optional> |
| 25 | #include <vector> |
| 26 | |
| 27 | namespace host_tool |
| 28 | { |
| 29 | |
| 30 | std::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 Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 57 | std::memcpy(item.bars, dev->base_addr, sizeof(dev->base_addr)); |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 58 | |
| 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 |