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