]> mj.ucw.cz Git - pciutils.git/blob - lib/init.c
Moved functions related to parameters to params.c.
[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 void *
63 pci_malloc(struct pci_access *a, int size)
64 {
65   void *x = malloc(size);
66
67   if (!x)
68     a->error("Out of memory (allocation of %d bytes failed)", size);
69   return x;
70 }
71
72 void
73 pci_mfree(void *x)
74 {
75   if (x)
76     free(x);
77 }
78
79 char *
80 pci_strdup(struct pci_access *a, char *s)
81 {
82   int len = strlen(s) + 1;
83   char *t = pci_malloc(a, len);
84   memcpy(t, s, len);
85   return t;
86 }
87
88 static void
89 pci_generic_error(char *msg, ...)
90 {
91   va_list args;
92
93   va_start(args, msg);
94   fputs("pcilib: ", stderr);
95   vfprintf(stderr, msg, args);
96   fputc('\n', stderr);
97   exit(1);
98 }
99
100 static void
101 pci_generic_warn(char *msg, ...)
102 {
103   va_list args;
104
105   va_start(args, msg);
106   fputs("pcilib: ", stderr);
107   vfprintf(stderr, msg, args);
108   fputc('\n', stderr);
109 }
110
111 static void
112 pci_generic_debug(char *msg, ...)
113 {
114   va_list args;
115
116   va_start(args, msg);
117   vfprintf(stdout, msg, args);
118   va_end(args);
119 }
120
121 static void
122 pci_null_debug(char *msg UNUSED, ...)
123 {
124 }
125
126 struct pci_access *
127 pci_alloc(void)
128 {
129   struct pci_access *a = malloc(sizeof(struct pci_access));
130   int i;
131
132   memset(a, 0, sizeof(*a));
133   pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
134 #ifdef PCI_USE_DNS
135   pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's");
136   pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file");
137   a->id_lookup_mode = PCI_LOOKUP_CACHE;
138 #endif
139   for(i=0; i<PCI_ACCESS_MAX; i++)
140     if (pci_methods[i] && pci_methods[i]->config)
141       pci_methods[i]->config(a);
142   return a;
143 }
144
145 void
146 pci_init(struct pci_access *a)
147 {
148   if (!a->error)
149     a->error = pci_generic_error;
150   if (!a->warning)
151     a->warning = pci_generic_warn;
152   if (!a->debug)
153     a->debug = pci_generic_debug;
154   if (!a->debugging)
155     a->debug = pci_null_debug;
156
157   if (a->method)
158     {
159       if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
160         a->error("This access method is not supported.");
161       a->methods = pci_methods[a->method];
162     }
163   else
164     {
165       unsigned int i;
166       for(i=0; i<PCI_ACCESS_MAX; i++)
167         if (pci_methods[i])
168           {
169             a->debug("Trying method %d...", i);
170             if (pci_methods[i]->detect(a))
171               {
172                 a->debug("...OK\n");
173                 a->methods = pci_methods[i];
174                 a->method = i;
175                 break;
176               }
177             a->debug("...No.\n");
178           }
179       if (!a->methods)
180         a->error("Cannot find any working access method.");
181     }
182   a->debug("Decided to use %s\n", a->methods->name);
183   a->methods->init(a);
184 }
185
186 void
187 pci_cleanup(struct pci_access *a)
188 {
189   struct pci_dev *d, *e;
190
191   for(d=a->devices; d; d=e)
192     {
193       e = d->next;
194       pci_free_dev(d);
195     }
196   if (a->methods)
197     a->methods->cleanup(a);
198   pci_free_name_list(a);
199   pci_free_params(a);
200   pci_set_name_list_path(a, NULL, 0);
201   pci_mfree(a);
202 }