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