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 struct pci_device*
38     PciAccessImpl::pci_device_next(struct pci_device_iterator* iter) const
39 {
40     return ::pci_device_next(iter);
41 }
42 
43 int PciAccessImpl::pci_device_probe(struct pci_device* dev) const
44 {
45     return ::pci_device_probe(dev);
46 }
47 
48 int PciAccessImpl::pci_device_cfg_read_u8(struct pci_device* dev,
49                                           std::uint8_t* data,
50                                           pciaddr_t offset) const
51 {
52     return ::pci_device_cfg_read_u8(dev, data, offset);
53 }
54 
55 int PciAccessImpl::pci_device_cfg_write_u8(struct pci_device* dev,
56                                            std::uint8_t data,
57                                            pciaddr_t offset) const
58 {
59     return ::pci_device_cfg_write_u8(dev, data, offset);
60 }
61 
62 int PciAccessImpl::pci_device_map_range(struct pci_device* dev, pciaddr_t base,
63                                         pciaddr_t size, unsigned map_flags,
64                                         void** addr) const
65 {
66     return ::pci_device_map_range(dev, base, size, map_flags, addr);
67 }
68 
69 int PciAccessImpl::pci_device_unmap_range(struct pci_device* dev, void* memory,
70                                           pciaddr_t size) const
71 {
72     return ::pci_device_unmap_range(dev, memory, size);
73 }
74 
75 PciAccessImpl::PciAccessImpl()
76 {
77     int ret = ::pci_system_init();
78     if (ret)
79     {
80         throw std::system_error(ret, std::generic_category(),
81                                 "Error setting up libpciaccess");
82     }
83 }
84 
85 PciAccessImpl::~PciAccessImpl()
86 {
87     ::pci_system_cleanup();
88 }
89 
90 } // namespace host_tool
91