5
+ * pcimodules: Load all kernel modules for PCI device currently
6
+ * plugged into any PCI slot.
8
+ * Copyright 2000 Yggdrasil Computing, Incorporated
9
+ * This file may be copied under the terms and conditions of version
10
+ * two of the GNU General Public License, as published by the Free
11
+ * Software Foundation (Cambridge, Massachusetts, USA).
13
+ * This file is based on pciutils/lib/example.c, which has the following
14
+ * authorship and copyright statement:
16
+ * Written by Martin Mares and put to public domain. You can do
17
+ * with it anything you want, but I don't give you any warranty.
25
+#include <sys/utsname.h>
26
+#include <sys/param.h>
27
+#include <sys/types.h>
32
+#include "pciutils.h"
34
+const char program_name[] = "pcimodules";
36
+#define MODDIR "/lib/modules"
37
+#define PCIMAP "modules.pcimap"
39
+#define LINELENGTH 8000
41
+#define DEVICE_ANY 0xffffffff
42
+#define VENDOR_ANY 0xffffffff
46
+struct pcimap_entry {
47
+ unsigned int vendor, subsys_vendor, dev, subsys_dev, class, class_mask;
49
+ struct pcimap_entry *next;
52
+static struct pcimap_entry *pcimap_list = NULL;
54
+#define OPT_STRING "h"
55
+static struct option long_options[] = {
56
+ {"class", required_argument, NULL, 'c'},
57
+ {"classmask", required_argument, NULL, 'm'},
58
+ {"help", no_argument, NULL, 'h'},
62
+static unsigned long desired_class;
63
+static unsigned long desired_classmask; /* Default is 0: accept all classes.*/
68
+ struct utsname utsname;
69
+ char filename[MAXPATHLEN];
71
+ char line[LINELENGTH];
72
+ struct pcimap_entry *entry;
73
+ unsigned int driver_data;
74
+ char *prevmodule = "";
75
+ char module[LINELENGTH];
77
+ if (uname(&utsname) < 0) {
81
+ sprintf(filename, "%s/%s/%s", MODDIR, utsname.release, PCIMAP);
82
+ if ((pcimap_file = fopen(filename, "r")) == NULL) {
87
+ while(fgets(line, LINELENGTH, pcimap_file) != NULL) {
91
+ entry = xmalloc(sizeof(struct pcimap_entry));
93
+ if (sscanf(line, "%s 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
95
+ &entry->vendor, &entry->dev,
96
+ &entry->subsys_vendor, &entry->subsys_dev,
97
+ &entry->class, &entry->class_mask,
98
+ &driver_data) != 8) {
100
+ "modules.pcimap unparsable line: %s.\n", line);
105
+ /* Optimize memory allocation a bit, in case someday we
106
+ have Linux systems with ~100,000 modules. It also
107
+ allows us to just compare pointers to avoid trying
108
+ to load a module twice. */
109
+ if (strcmp(module, prevmodule) != 0) {
110
+ prevmodule = xmalloc(strlen(module)+1);
111
+ strcpy(prevmodule, module);
113
+ entry->module = prevmodule;
114
+ entry->next = pcimap_list;
115
+ pcimap_list = entry;
117
+ fclose(pcimap_file);
120
+/* Return a filled in pci_access->dev tree, with the device classes
121
+ stored in dev->aux.
124
+match_pci_modules(void)
126
+ struct pci_access *pacc;
127
+ struct pci_dev *dev;
128
+ unsigned int class, subsys_dev, subsys_vendor;
129
+ struct pcimap_entry *map;
130
+ const char *prevmodule = "";
132
+ pacc = pci_alloc(); /* Get the pci_access structure */
133
+ /* Set all options you want -- here we stick with the defaults */
134
+ pci_init(pacc); /* Initialize the PCI library */
135
+ pci_scan_bus(pacc); /* We want to get the list of devices */
136
+ for(dev=pacc->devices; dev; dev=dev->next) {
137
+ pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES);
138
+ class = (pci_read_word(dev, PCI_CLASS_DEVICE) << 8)
139
+ | pci_read_byte(dev, PCI_CLASS_PROG);
140
+ subsys_dev = pci_read_word(dev, PCI_SUBSYSTEM_ID);
141
+ subsys_vendor = pci_read_word(dev,PCI_SUBSYSTEM_VENDOR_ID);
142
+ for(map = pcimap_list; map != NULL; map = map->next) {
143
+ if (((map->class ^ class) & map->class_mask) == 0 &&
144
+ ((desired_class ^ class) & desired_classmask)==0 &&
145
+ (map->dev == DEVICE_ANY ||
146
+ map->dev == dev->device_id) &&
147
+ (map->vendor == VENDOR_ANY ||
148
+ map->vendor == dev->vendor_id) &&
149
+ (map->subsys_dev == DEVICE_ANY ||
150
+ map->subsys_dev == subsys_dev) &&
151
+ (map->subsys_vendor == VENDOR_ANY ||
152
+ map->subsys_vendor == subsys_vendor) &&
153
+ prevmodule != map->module) {
154
+ printf("%s\n", map->module);
155
+ prevmodule = map->module;
164
+main (int argc, char **argv)
169
+ while ((opt = getopt_long(argc, argv, OPT_STRING, long_options,
170
+ &opt_index)) != -1) {
173
+ desired_class = strtol(optarg, NULL, 0);
176
+ desired_classmask = strtol(optarg, NULL, 0);
179
+ printf ("Usage: pcimodules [--help]\n"
180
+ " Lists kernel modules corresponding to PCI devices currently plugged"
181
+ " into the computer.\n");
186
+ match_pci_modules();
192
+.TH pcimodules 8 "@TODAY@" "@VERSION@" "Linux PCI Utilities"
195
+pcimodules \- List kernel driver modules available for all currently plugged
199
+.RB [ --class class_id ]
200
+.RB [ --classmask mask ]
204
+lists all driver modules for all currently plugged in PCI devices.
206
+should be run at boot time, and whenever a PCI device is "hot plugged"
207
+into the system. This can be done by the following Bourne shell syntax:
209
+ for module in $(pcimodules) ; do
211
+ modprobe -s -k "$module"
215
+When a PCI device is removed from the system, the Linux kernel will
216
+decrement a usage count on PCI driver module. If this count drops
217
+to zero (i.e., there are no PCI drivers), then the
219
+process that is normally configured to run from cron every few minutes
220
+will eventually remove the unneeded module.
222
+The --class and --classmask arguments can be used to limit the search
223
+to certain classes of PCI devices. This is useful, for example, to
224
+generate a list of ethernet card drivers to be loaded when the kernel
225
+has indicated that it is trying to resolve an unknown network interface.
227
+Modules are listed in the order in which the PCI devices are physically
228
+arranged so that the computer owner can arrange things like having scsi
229
+device 0 be on a controller that is not alphabetically the first scsi
233
+.B --class class --classmask mask
235
+--class and --classmask limit the search to PCI
236
+cards in particular classes. These arguments are always used together.
237
+The arguments to --class and --classmask
238
+can be given as hexadecimal numbers by prefixing a leading "0x".
239
+Note that the classes used by pcimodules are in "Linux" format,
240
+meaning the class value that you see with lspci would be shifted
241
+left eight bits, with the new low eight bits programming interface ID.
242
+An examples of how to use class and classmask is provided below.
244
+Print a help message and exit.
248
+lists all modules corresponding to currently plugged in PCI devices.
250
+pcimodules --class 0x20000 --classmask 0xffff00
251
+lists all modules corresponding to currently plugged in ethernet PCI devices.
254
+.B /lib/modules/<kernel-version>/modules.pcimap
255
+This file is automatically generated by
259
+to determine which modules correspond to which PCI ID's.
262
+An interface to PCI bus configuration space provided by the post-2.1.82 Linux
263
+kernels. Contains per-bus subdirectories with per-card config space files and a
265
+file containing a list of all PCI devices.
271
+The Linux PCI Utilities are maintained by Martin Mares <mj@suse.cz>.
275
+was written by Adam J. Richter <adam@yggdrasil.com>, based on public
276
+domain example code by Martin Mares <mj@suse.cz>.
280
+is copyright 2000, Yggdrasil Computing, Incorporated, and may
281
+be copied under the terms and conditions of version 2 of the GNU
282
+General Public License as published by the Free Software Foundation
283
+(Cambridge, Massachusetts, United States of America).