1 /*
2  * Copyright 2020 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 "pciaccess.hpp"
18 
19 #include <cstdio>
20 #include <cstring>
21 #include <system_error>
22 
23 namespace host_tool
24 {
25 
26 struct pci_device_iterator* PciAccessImpl::pci_id_match_iterator_create(
27     const struct pci_id_match* match) const
28 {
29     return ::pci_id_match_iterator_create(match);
30 }
31 
32 void PciAccessImpl::pci_iterator_destroy(struct pci_device_iterator* iter) const
33 {
34     return ::pci_iterator_destroy(iter);
35 }
36 
37 void PciAccessImpl::pci_device_enable(struct pci_device* dev) const
38 {
39     return ::pci_device_enable(dev);
40 }
41 
42 struct pci_device*
43     PciAccessImpl::pci_device_next(struct pci_device_iterator* iter) const
44 {
45     return ::pci_device_next(iter);
46 }
47 
48 int PciAccessImpl::pci_device_probe(struct pci_device* dev) const
49 {
50     return ::pci_device_probe(dev);
51 }
52 
53 int PciAccessImpl::pci_device_cfg_read_u8(struct pci_device* dev,
54                                           std::uint8_t* data,
55                                           pciaddr_t offset) const
56 {
57     return ::pci_device_cfg_read_u8(dev, data, offset);
58 }
59 
60 int PciAccessImpl::pci_device_cfg_write_u8(struct pci_device* dev,
61                                            std::uint8_t data,
62                                            pciaddr_t offset) const
63 {
64     return ::pci_device_cfg_write_u8(dev, data, offset);
65 }
66 
67 int PciAccessImpl::pci_device_map_range(struct pci_device* dev, pciaddr_t base,
68                                         pciaddr_t size, unsigned map_flags,
69                                         void** addr) const
70 {
71     return ::pci_device_map_range(dev, base, size, map_flags, addr);
72 }
73 
74 int PciAccessImpl::pci_device_unmap_range(struct pci_device* dev, void* memory,
75                                           pciaddr_t size) const
76 {
77     return ::pci_device_unmap_range(dev, memory, size);
78 }
79 
80 PciAccessImpl::PciAccessImpl()
81 {
82     int ret = ::pci_system_init();
83     if (ret)
84     {
85         throw std::system_error(ret, std::generic_category(),
86                                 "Error setting up libpciaccess");
87     }
88 }
89 
90 PciAccessImpl::~PciAccessImpl()
91 {
92     ::pci_system_cleanup();
93 }
94 
95 } // namespace host_tool
96