]> mj.ucw.cz Git - pciutils.git/blob - lib/init.c
Added a simple infrastructure for setting of arbitrary parameters.
[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   pci_set_net_domain(a, PCI_ID_DOMAIN, 0);
71   a->id_lookup_mode = PCI_LOOKUP_CACHE;
72   for(i=0; i<PCI_ACCESS_MAX; i++)
73     if (pci_methods[i] && pci_methods[i]->config)
74       pci_methods[i]->config(a);
75   return a;
76 }
77
78 void *
79 pci_malloc(struct pci_access *a, int size)
80 {
81   void *x = malloc(size);
82
83   if (!x)
84     a->error("Out of memory (allocation of %d bytes failed)", size);
85   return x;
86 }
87
88 void
89 pci_mfree(void *x)
90 {
91   if (x)
92     free(x);
93 }
94
95 char *
96 pci_strdup(struct pci_access *a, char *s)
97 {
98   int len = strlen(s) + 1;
99   char *t = pci_malloc(a, len);
100   memcpy(t, s, len);
101   return t;
102 }
103
104 static void
105 pci_generic_error(char *msg, ...)
106 {
107   va_list args;
108
109   va_start(args, msg);
110   fputs("pcilib: ", stderr);
111   vfprintf(stderr, msg, args);
112   fputc('\n', stderr);
113   exit(1);
114 }
115
116 static void
117 pci_generic_warn(char *msg, ...)
118 {
119   va_list args;
120
121   va_start(args, msg);
122   fputs("pcilib: ", stderr);
123   vfprintf(stderr, msg, args);
124   fputc('\n', stderr);
125 }
126
127 static void
128 pci_generic_debug(char *msg, ...)
129 {
130   va_list args;
131
132   va_start(args, msg);
133   vfprintf(stdout, msg, args);
134   va_end(args);
135 }
136
137 static void
138 pci_null_debug(char *msg UNUSED, ...)
139 {
140 }
141
142 char *
143 pci_get_param(struct pci_access *acc, char *param)
144 {
145   struct pci_param *p;
146
147   for (p=acc->params; p; p=p->next)
148     if (!strcmp(p->param, param))
149       return p->value;
150   return NULL;
151 }
152
153 void
154 pci_define_param(struct pci_access *acc, char *param, char *value)
155 {
156   struct pci_param *p = pci_malloc(acc, sizeof(*p));
157
158   p->next = acc->params;
159   acc->params = p;
160   p->param = param;
161   p->value = value;
162   p->value_malloced = 0;
163 }
164
165 int
166 pci_set_param(struct pci_access *acc, char *param, char *value)
167 {
168   struct pci_param *p;
169
170   for (p=acc->params; p; p=p->next)
171     if (!strcmp(p->param, param))
172       {
173         if (p->value_malloced)
174           pci_mfree(p->value);
175         p->value_malloced = 1;
176         p->value = pci_strdup(acc, value);
177         return 0;
178       }
179   return -1;
180 }
181
182 static void
183 pci_free_params(struct pci_access *acc)
184 {
185   struct pci_param *p;
186
187   while (p = acc->params)
188     {
189       acc->params = p->next;
190       if (p->value_malloced)
191         pci_mfree(p->value);
192       pci_mfree(p);
193     }
194 }
195
196 struct pci_param *
197 pci_walk_params(struct pci_access *acc, struct pci_param *prev)
198 {
199   /* So far, the params form a simple linked list, but this can change in the future */
200   if (!prev)
201     return acc->params;
202   else
203     return prev->next;
204 }
205
206 void
207 pci_init(struct pci_access *a)
208 {
209   if (!a->error)
210     a->error = pci_generic_error;
211   if (!a->warning)
212     a->warning = pci_generic_warn;
213   if (!a->debug)
214     a->debug = pci_generic_debug;
215   if (!a->debugging)
216     a->debug = pci_null_debug;
217
218   if (a->method)
219     {
220       if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
221         a->error("This access method is not supported.");
222       a->methods = pci_methods[a->method];
223     }
224   else
225     {
226       unsigned int i;
227       for(i=0; i<PCI_ACCESS_MAX; i++)
228         if (pci_methods[i])
229           {
230             a->debug("Trying method %d...", i);
231             if (pci_methods[i]->detect(a))
232               {
233                 a->debug("...OK\n");
234                 a->methods = pci_methods[i];
235                 a->method = i;
236                 break;
237               }
238             a->debug("...No.\n");
239           }
240       if (!a->methods)
241         a->error("Cannot find any working access method.");
242     }
243   a->debug("Decided to use %s\n", a->methods->name);
244   a->methods->init(a);
245 }
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_set_net_domain(a, NULL, 0);
263   pci_set_id_cache(a, NULL, 0);
264   pci_mfree(a);
265 }