2 * The PCI Library -- Initialization and related things
4 * Copyright (c) 1997--2008 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
16 static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = {
18 #ifdef PCI_HAVE_PM_LINUX_SYSFS
23 #ifdef PCI_HAVE_PM_LINUX_PROC
28 #ifdef PCI_HAVE_PM_INTEL_CONF
35 #ifdef PCI_HAVE_PM_FBSD_DEVICE
40 #ifdef PCI_HAVE_PM_AIX_DEVICE
45 #ifdef PCI_HAVE_PM_NBSD_LIBPCI
50 #ifdef PCI_HAVE_PM_OBSD_DEVICE
55 #ifdef PCI_HAVE_PM_DUMP
65 struct pci_access *a = malloc(sizeof(struct pci_access));
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);
79 pci_malloc(struct pci_access *a, int size)
81 void *x = malloc(size);
84 a->error("Out of memory (allocation of %d bytes failed)", size);
96 pci_strdup(struct pci_access *a, char *s)
98 int len = strlen(s) + 1;
99 char *t = pci_malloc(a, len);
105 pci_generic_error(char *msg, ...)
110 fputs("pcilib: ", stderr);
111 vfprintf(stderr, msg, args);
117 pci_generic_warn(char *msg, ...)
122 fputs("pcilib: ", stderr);
123 vfprintf(stderr, msg, args);
128 pci_generic_debug(char *msg, ...)
133 vfprintf(stdout, msg, args);
138 pci_null_debug(char *msg UNUSED, ...)
143 pci_get_param(struct pci_access *acc, char *param)
147 for (p=acc->params; p; p=p->next)
148 if (!strcmp(p->param, param))
154 pci_define_param(struct pci_access *acc, char *param, char *value)
156 struct pci_param *p = pci_malloc(acc, sizeof(*p));
158 p->next = acc->params;
162 p->value_malloced = 0;
166 pci_set_param(struct pci_access *acc, char *param, char *value)
170 for (p=acc->params; p; p=p->next)
171 if (!strcmp(p->param, param))
173 if (p->value_malloced)
175 p->value_malloced = 1;
176 p->value = pci_strdup(acc, value);
183 pci_free_params(struct pci_access *acc)
187 while (p = acc->params)
189 acc->params = p->next;
190 if (p->value_malloced)
197 pci_walk_params(struct pci_access *acc, struct pci_param *prev)
199 /* So far, the params form a simple linked list, but this can change in the future */
207 pci_init(struct pci_access *a)
210 a->error = pci_generic_error;
212 a->warning = pci_generic_warn;
214 a->debug = pci_generic_debug;
216 a->debug = pci_null_debug;
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];
227 for(i=0; i<PCI_ACCESS_MAX; i++)
230 a->debug("Trying method %d...", i);
231 if (pci_methods[i]->detect(a))
234 a->methods = pci_methods[i];
238 a->debug("...No.\n");
241 a->error("Cannot find any working access method.");
243 a->debug("Decided to use %s\n", a->methods->name);
248 pci_cleanup(struct pci_access *a)
250 struct pci_dev *d, *e;
252 for(d=a->devices; d; d=e)
258 a->methods->cleanup(a);
259 pci_free_name_list(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);