]> mj.ucw.cz Git - pciutils.git/blob - lib/init.c
Renamed `net.cache_path' to `net.cache_name', it's more logical.
[pciutils.git] / lib / init.c
1 /*
2  *      The PCI Library -- Initialization and related things
3  *
4  *      Copyright (c) 1997--2008 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 };
61
62 struct pci_access *
63 pci_alloc(void)
64 {
65   struct pci_access *a = malloc(sizeof(struct pci_access));
66   int i;
67
68   memset(a, 0, sizeof(*a));
69   pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
70 #ifdef PCI_USE_DNS
71   pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's");
72   pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file");
73   a->id_lookup_mode = PCI_LOOKUP_CACHE;
74 #endif
75   for(i=0; i<PCI_ACCESS_MAX; i++)
76     if (pci_methods[i] && pci_methods[i]->config)
77       pci_methods[i]->config(a);
78   return a;
79 }
80
81 void *
82 pci_malloc(struct pci_access *a, int size)
83 {
84   void *x = malloc(size);
85
86   if (!x)
87     a->error("Out of memory (allocation of %d bytes failed)", size);
88   return x;
89 }
90
91 void
92 pci_mfree(void *x)
93 {
94   if (x)
95     free(x);
96 }
97
98 char *
99 pci_strdup(struct pci_access *a, char *s)
100 {
101   int len = strlen(s) + 1;
102   char *t = pci_malloc(a, len);
103   memcpy(t, s, len);
104   return t;
105 }
106
107 static void
108 pci_generic_error(char *msg, ...)
109 {
110   va_list args;
111
112   va_start(args, msg);
113   fputs("pcilib: ", stderr);
114   vfprintf(stderr, msg, args);
115   fputc('\n', stderr);
116   exit(1);
117 }
118
119 static void
120 pci_generic_warn(char *msg, ...)
121 {
122   va_list args;
123
124   va_start(args, msg);
125   fputs("pcilib: ", stderr);
126   vfprintf(stderr, msg, args);
127   fputc('\n', stderr);
128 }
129
130 static void
131 pci_generic_debug(char *msg, ...)
132 {
133   va_list args;
134
135   va_start(args, msg);
136   vfprintf(stdout, msg, args);
137   va_end(args);
138 }
139
140 static void
141 pci_null_debug(char *msg UNUSED, ...)
142 {
143 }
144
145 char *
146 pci_get_param(struct pci_access *acc, char *param)
147 {
148   struct pci_param *p;
149
150   for (p=acc->params; p; p=p->next)
151     if (!strcmp(p->param, param))
152       return p->value;
153   return NULL;
154 }
155
156 void
157 pci_define_param(struct pci_access *acc, char *param, char *value, char *help)
158 {
159   struct pci_param *p = pci_malloc(acc, sizeof(*p));
160
161   p->next = acc->params;
162   acc->params = p;
163   p->param = param;
164   p->value = value;
165   p->value_malloced = 0;
166   p->help = help;
167 }
168
169 int
170 pci_set_param_internal(struct pci_access *acc, char *param, char *value, int copy)
171 {
172   struct pci_param *p;
173
174   for (p=acc->params; p; p=p->next)
175     if (!strcmp(p->param, param))
176       {
177         if (p->value_malloced)
178           pci_mfree(p->value);
179         p->value_malloced = copy;
180         if (copy)
181           p->value = pci_strdup(acc, value);
182         else
183           p->value = value;
184         return 0;
185       }
186   return -1;
187 }
188
189 int
190 pci_set_param(struct pci_access *acc, char *param, char *value)
191 {
192   return pci_set_param_internal(acc, param, value, 1);
193 }
194
195 static void
196 pci_free_params(struct pci_access *acc)
197 {
198   struct pci_param *p;
199
200   while (p = acc->params)
201     {
202       acc->params = p->next;
203       if (p->value_malloced)
204         pci_mfree(p->value);
205       pci_mfree(p);
206     }
207 }
208
209 struct pci_param *
210 pci_walk_params(struct pci_access *acc, struct pci_param *prev)
211 {
212   /* So far, the params form a simple linked list, but this can change in the future */
213   if (!prev)
214     return acc->params;
215   else
216     return prev->next;
217 }
218
219 void
220 pci_init(struct pci_access *a)
221 {
222   if (!a->error)
223     a->error = pci_generic_error;
224   if (!a->warning)
225     a->warning = pci_generic_warn;
226   if (!a->debug)
227     a->debug = pci_generic_debug;
228   if (!a->debugging)
229     a->debug = pci_null_debug;
230
231   if (a->method)
232     {
233       if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
234         a->error("This access method is not supported.");
235       a->methods = pci_methods[a->method];
236     }
237   else
238     {
239       unsigned int i;
240       for(i=0; i<PCI_ACCESS_MAX; i++)
241         if (pci_methods[i])
242           {
243             a->debug("Trying method %d...", i);
244             if (pci_methods[i]->detect(a))
245               {
246                 a->debug("...OK\n");
247                 a->methods = pci_methods[i];
248                 a->method = i;
249                 break;
250               }
251             a->debug("...No.\n");
252           }
253       if (!a->methods)
254         a->error("Cannot find any working access method.");
255     }
256   a->debug("Decided to use %s\n", a->methods->name);
257   a->methods->init(a);
258 }
259
260 void
261 pci_cleanup(struct pci_access *a)
262 {
263   struct pci_dev *d, *e;
264
265   for(d=a->devices; d; d=e)
266     {
267       e = d->next;
268       pci_free_dev(d);
269     }
270   if (a->methods)
271     a->methods->cleanup(a);
272   pci_free_name_list(a);
273   pci_free_params(a);
274   pci_set_name_list_path(a, NULL, 0);
275   pci_mfree(a);
276 }