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);
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_path", "~/.pciids-cache", "Name of the ID cache file");
73 a->id_lookup_mode = PCI_LOOKUP_CACHE;
75 for(i=0; i<PCI_ACCESS_MAX; i++)
76 if (pci_methods[i] && pci_methods[i]->config)
77 pci_methods[i]->config(a);
82 pci_malloc(struct pci_access *a, int size)
84 void *x = malloc(size);
87 a->error("Out of memory (allocation of %d bytes failed)", size);
99 pci_strdup(struct pci_access *a, char *s)
101 int len = strlen(s) + 1;
102 char *t = pci_malloc(a, len);
108 pci_generic_error(char *msg, ...)
113 fputs("pcilib: ", stderr);
114 vfprintf(stderr, msg, args);
120 pci_generic_warn(char *msg, ...)
125 fputs("pcilib: ", stderr);
126 vfprintf(stderr, msg, args);
131 pci_generic_debug(char *msg, ...)
136 vfprintf(stdout, msg, args);
141 pci_null_debug(char *msg UNUSED, ...)
146 pci_get_param(struct pci_access *acc, char *param)
150 for (p=acc->params; p; p=p->next)
151 if (!strcmp(p->param, param))
157 pci_define_param(struct pci_access *acc, char *param, char *value, char *help)
159 struct pci_param *p = pci_malloc(acc, sizeof(*p));
161 p->next = acc->params;
165 p->value_malloced = 0;
170 pci_set_param_internal(struct pci_access *acc, char *param, char *value, int copy)
174 for (p=acc->params; p; p=p->next)
175 if (!strcmp(p->param, param))
177 if (p->value_malloced)
179 p->value_malloced = copy;
181 p->value = pci_strdup(acc, value);
190 pci_set_param(struct pci_access *acc, char *param, char *value)
192 return pci_set_param_internal(acc, param, value, 1);
196 pci_free_params(struct pci_access *acc)
200 while (p = acc->params)
202 acc->params = p->next;
203 if (p->value_malloced)
210 pci_walk_params(struct pci_access *acc, struct pci_param *prev)
212 /* So far, the params form a simple linked list, but this can change in the future */
220 pci_init(struct pci_access *a)
223 a->error = pci_generic_error;
225 a->warning = pci_generic_warn;
227 a->debug = pci_generic_debug;
229 a->debug = pci_null_debug;
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];
240 for(i=0; i<PCI_ACCESS_MAX; i++)
243 a->debug("Trying method %d...", i);
244 if (pci_methods[i]->detect(a))
247 a->methods = pci_methods[i];
251 a->debug("...No.\n");
254 a->error("Cannot find any working access method.");
256 a->debug("Decided to use %s\n", a->methods->name);
261 pci_cleanup(struct pci_access *a)
263 struct pci_dev *d, *e;
265 for(d=a->devices; d; d=e)
271 a->methods->cleanup(a);
272 pci_free_name_list(a);
274 pci_set_name_list_path(a, NULL, 0);