]> mj.ucw.cz Git - pciutils.git/blob - lib/init.c
adae8424a890088a8e48fafa5ffc4135792023a8
[pciutils.git] / lib / init.c
1 /*
2  *      The PCI Library -- Initialization and related things
3  *
4  *      Copyright (c) 1997--2018 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <string.h>
13
14 #include "internal.h"
15
16 static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = {
17   NULL,
18 #ifdef PCI_HAVE_PM_LINUX_SYSFS
19   &pm_linux_sysfs,
20 #else
21   NULL,
22 #endif
23 #ifdef PCI_HAVE_PM_LINUX_PROC
24   &pm_linux_proc,
25 #else
26   NULL,
27 #endif
28 #ifdef PCI_HAVE_PM_INTEL_CONF
29   &pm_intel_conf1,
30   &pm_intel_conf2,
31 #else
32   NULL,
33   NULL,
34 #endif
35 #ifdef PCI_HAVE_PM_FBSD_DEVICE
36   &pm_fbsd_device,
37 #else
38   NULL,
39 #endif
40 #ifdef PCI_HAVE_PM_AIX_DEVICE
41   &pm_aix_device,
42 #else
43   NULL,
44 #endif
45 #ifdef PCI_HAVE_PM_NBSD_LIBPCI
46   &pm_nbsd_libpci,
47 #else
48   NULL,
49 #endif
50 #ifdef PCI_HAVE_PM_OBSD_DEVICE
51   &pm_obsd_device,
52 #else
53   NULL,
54 #endif
55 #ifdef PCI_HAVE_PM_DUMP
56   &pm_dump,
57 #else
58   NULL,
59 #endif
60 #ifdef PCI_HAVE_PM_DARWIN_DEVICE
61   &pm_darwin,
62 #else
63   NULL,
64 #endif
65 #ifdef PCI_HAVE_PM_SYLIXOS_DEVICE
66   &pm_sylixos_device,
67 #else
68   NULL,
69 #endif
70 };
71
72 // If PCI_ACCESS_AUTO is selected, we probe the access methods in this order
73 static int probe_sequence[] = {
74   // System-specific methods
75   PCI_ACCESS_SYS_BUS_PCI,
76   PCI_ACCESS_PROC_BUS_PCI,
77   PCI_ACCESS_FBSD_DEVICE,
78   PCI_ACCESS_AIX_DEVICE,
79   PCI_ACCESS_NBSD_LIBPCI,
80   PCI_ACCESS_OBSD_DEVICE,
81   PCI_ACCESS_DARWIN,
82   PCI_ACCESS_SYLIXOS_DEVICE,
83   // Low-level methods poking the hardware directly
84   PCI_ACCESS_I386_TYPE1,
85   PCI_ACCESS_I386_TYPE2,
86   -1,
87 };
88
89 void *
90 pci_malloc(struct pci_access *a, int size)
91 {
92   void *x = malloc(size);
93
94   if (!x)
95     a->error("Out of memory (allocation of %d bytes failed)", size);
96   return x;
97 }
98
99 void
100 pci_mfree(void *x)
101 {
102   if (x)
103     free(x);
104 }
105
106 char *
107 pci_strdup(struct pci_access *a, const char *s)
108 {
109   int len = strlen(s) + 1;
110   char *t = pci_malloc(a, len);
111   memcpy(t, s, len);
112   return t;
113 }
114
115 static void
116 pci_generic_error(char *msg, ...)
117 {
118   va_list args;
119
120   va_start(args, msg);
121   fputs("pcilib: ", stderr);
122   vfprintf(stderr, msg, args);
123   va_end(args);
124   fputc('\n', stderr);
125   exit(1);
126 }
127
128 static void
129 pci_generic_warn(char *msg, ...)
130 {
131   va_list args;
132
133   va_start(args, msg);
134   fputs("pcilib: ", stderr);
135   vfprintf(stderr, msg, args);
136   va_end(args);
137   fputc('\n', stderr);
138 }
139
140 static void
141 pci_generic_debug(char *msg, ...)
142 {
143   va_list args;
144
145   va_start(args, msg);
146   vfprintf(stdout, msg, args);
147   va_end(args);
148 }
149
150 static void
151 pci_null_debug(char *msg UNUSED, ...)
152 {
153 }
154
155 int
156 pci_lookup_method(char *name)
157 {
158   int i;
159
160   for (i=0; i<PCI_ACCESS_MAX; i++)
161     if (pci_methods[i] && !strcmp(pci_methods[i]->name, name))
162       return i;
163   return -1;
164 }
165
166 char *
167 pci_get_method_name(int index)
168 {
169   if (index < 0 || index >= PCI_ACCESS_MAX)
170     return NULL;
171   else if (!pci_methods[index])
172     return "";
173   else
174     return pci_methods[index]->name;
175 }
176
177 struct pci_access *
178 pci_alloc(void)
179 {
180   struct pci_access *a = malloc(sizeof(struct pci_access));
181   int i;
182
183   memset(a, 0, sizeof(*a));
184   pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
185 #ifdef PCI_USE_DNS
186   pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's");
187   pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file");
188   a->id_lookup_mode = PCI_LOOKUP_CACHE;
189 #endif
190 #ifdef PCI_HAVE_HWDB
191   pci_define_param(a, "hwdb.disable", "0", "Do not look up names in UDEV's HWDB if non-zero");
192 #endif
193   for (i=0; i<PCI_ACCESS_MAX; i++)
194     if (pci_methods[i] && pci_methods[i]->config)
195       pci_methods[i]->config(a);
196   return a;
197 }
198
199 void
200 pci_init_v35(struct pci_access *a)
201 {
202   if (!a->error)
203     a->error = pci_generic_error;
204   if (!a->warning)
205     a->warning = pci_generic_warn;
206   if (!a->debug)
207     a->debug = pci_generic_debug;
208   if (!a->debugging)
209     a->debug = pci_null_debug;
210
211   if (a->method)
212     {
213       if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
214         a->error("This access method is not supported.");
215       a->methods = pci_methods[a->method];
216     }
217   else
218     {
219       unsigned int i;
220       for (i=0; probe_sequence[i] >= 0; i++)
221         {
222           struct pci_methods *m = pci_methods[probe_sequence[i]];
223           if (!m)
224             continue;
225           a->debug("Trying method %s...", m->name);
226           if (m->detect(a))
227             {
228               a->debug("...OK\n");
229               a->methods = m;
230               a->method = probe_sequence[i];
231               break;
232             }
233           a->debug("...No.\n");
234         }
235       if (!a->methods)
236         a->error("Cannot find any working access method.");
237     }
238   a->debug("Decided to use %s\n", a->methods->name);
239   a->methods->init(a);
240 }
241
242 STATIC_ALIAS(void pci_init(struct pci_access *a), pci_init_v35(a));
243 DEFINE_ALIAS(void pci_init_v30(struct pci_access *a), pci_init_v35);
244 SYMBOL_VERSION(pci_init_v30, pci_init@LIBPCI_3.0);
245 SYMBOL_VERSION(pci_init_v35, pci_init@@LIBPCI_3.5);
246
247 void
248 pci_cleanup(struct pci_access *a)
249 {
250   struct pci_dev *d, *e;
251
252   for (d=a->devices; d; d=e)
253     {
254       e = d->next;
255       pci_free_dev(d);
256     }
257   if (a->methods)
258     a->methods->cleanup(a);
259   pci_free_name_list(a);
260   pci_free_params(a);
261   pci_set_name_list_path(a, NULL, 0);
262   pci_mfree(a);
263 }