1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/* $Progeny$ */
/* pci.c - Scan the PCI bus
*
* AUTHOR: John R. Daily <jdaily@progeny.com>
*
* Copyright 2002 Hewlett-Packard Company
* Copyright 2001, 2002 Progeny Linux Systems, Inc.
* Copyright (C) 1998-2000 MandrakeSoft
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <discover/sysdep.h>
#include <discover/utils.h>
/*
* We just need a path length sufficiently long for paths like
* /proc/bus/pci/02/07.0
*/
#ifdef _MAX_PATH_LEN
#undef _MAX_PATH_LEN
#endif
#define _MAX_PATH_LEN 256
/*
* This function is specific to each sysdep.
*/
static void
_discover_sysdep_init(discover_sysdep_data_t *node)
{
node->busclass = _discover_xmalloc(5);
node->vendor = _discover_xmalloc(5);
node->model = _discover_xmalloc(5);
}
discover_sysdep_data_t *
_discover_get_pci_raw_proc(void)
{
FILE *devices_file;
int individual_device_file;
char *line = NULL;
size_t len = 0;
discover_sysdep_data_t *head = NULL, *node, *last = NULL;
unsigned int i;
unsigned int bus;
unsigned int slot;
unsigned int function;
unsigned int sfcombo; /* Slot/function combination byte */
char file_path[_MAX_PATH_LEN];
u_int16_t devtype;
if ((devices_file = fopen(PATH_PROC_PCI, "r"))) {
while (getline(&line, &len, devices_file) >= 0) {
if (line[0] == '\n' || line[0] == '#') {
continue;
}
node = _discover_sysdep_data_new();
_discover_sysdep_init(node);
if (sscanf(line, "%02x%02x\t%4s%4s", &bus, &sfcombo,
node->vendor, node->model) != 4) {
perror("Failed to read expected data from devices file");
exit(-1);
}
/*
* The first 5 bits of the 2nd bite refer to the slot in the
* PCI bus where this device can be found. The last 3 bits
* describe the function this card is performing in this
* context.
*/
slot = (sfcombo & 0xf8) >> 3;
function = (sfcombo & 0x7);
/*
* Now we can read the busclass from the individual PCI file
* in /proc/bus/pci/<bus>/<function>.<slot> and extract the
* device type.
*/
if (snprintf(file_path, _MAX_PATH_LEN, "%s/%02x/%02x.%1x",
PATH_PROC_PCI_DIR,
bus, slot, function) >= _MAX_PATH_LEN) {
fprintf(stderr, "Path to PCI device file too long.\n");
exit(-1);
}
/* We use open instead of fopen because fopen will give us
* a buffered file which causes a read-ahead. On some
* devices reading undefined parts of the PCI
* configuration causes the system to crash.
*/
individual_device_file = open(file_path, O_RDONLY);
if (individual_device_file == -1) {
perror("Unable to locate device file");
exit(-1);
}
lseek(individual_device_file, 5 * sizeof(u_int16_t), SEEK_SET);
if (read(individual_device_file, &devtype,
sizeof(u_int16_t)) != sizeof(u_int16_t)) {
perror("Unable to read device type from file");
exit(-1);
}
close(individual_device_file);
#ifdef WORDS_BIGENDIAN
/* If we're on a big-endian machine, then convert DEVTYPE
to little-endian so we don't have mismatches. */
devtype = ((devtype << 8) & 0xff00) | ((devtype >> 8) & 0xff);
#endif /* WORD_BIGENDIAN */
snprintf(node->busclass, 5, "%.4x", devtype);
if (head == NULL) {
head = node;
last = head;
} else {
last->next = node;
last = node;
}
}
free(line);
fclose(devices_file);
}
return head;
}
discover_sysdep_data_t *
_discover_get_pci_raw_sys(void)
{
discover_sysdep_data_t *head = NULL, *node, *last = NULL;
FILE *f;
DIR *pciDir;
struct dirent *pci_device_entry;
size_t len = 0;
char *device_dir, *line, *class, *vendor, *model, *p;
char **device_dir_list = NULL;
size_t device_dir_list_len, device_dir_index, device_dir_index2;
char path[256];
/* Open the directory containing all the PCI device dirs. */
pciDir = opendir(PATH_SYS_PCI);
if (pciDir == NULL)
return _discover_get_pci_raw_proc();
/*
* The order of links in PATH_SYS_PCI is not sorted. Since
* module load order can affect things like device naming,
* we should collect the device directory names and sort them.
* We need to sort so that we mimic the order provided in
* PATH_PROC_PCI, so upgrades from 2.4 to 2.6 aren't affected.
* (Perniciously, the unsorted order in PATH_SYS_PCI seems to be
* the exact opposite of that in PATH_PROC_PCI, thus guaranteeing
* that modules will be loaded in the opposite order without a sort.)
*/
for (pci_device_entry = readdir(pciDir); pci_device_entry;
pci_device_entry = readdir(pciDir)) {
device_dir = strdup(pci_device_entry->d_name);
if (device_dir == NULL)
continue;
if (device_dir[0] == '.') {
free(device_dir);
continue;
}
if (device_dir_list == NULL) {
device_dir_list = (char **)malloc(sizeof(char *));
device_dir_list_len = 1;
} else {
device_dir_list =
(char **)realloc(device_dir_list,
sizeof(char *) * ++device_dir_list_len);
}
if (device_dir_list != NULL) {
device_dir_list[device_dir_list_len - 1] = device_dir;
} else {
free(device_dir);
}
}
closedir(pciDir);
if (device_dir_list == NULL)
return _discover_get_pci_raw_proc();
/* Do the sort. */
/* XXX: this turns out to not quite mimic the order we need. */
/* qsort(device_dir_list, device_dir_list_len, sizeof(char *), strcmp); */
/* For now, observe the pernicious nature of /sys and reverse the
raw unsorted order. */
device_dir_index = 0;
device_dir_index2 = device_dir_list_len - 1;
while (device_dir_index < device_dir_index2) {
device_dir = device_dir_list[device_dir_index];
device_dir_list[device_dir_index] = device_dir_list[device_dir_index2];
device_dir_list[device_dir_index2] = device_dir;
device_dir_index++;
device_dir_index2--;
}
/* Loop through the PCI device dirs. */
class = vendor = model = NULL;
for (device_dir_index = 0; device_dir_index < device_dir_list_len;
device_dir_index++) {
device_dir = device_dir_list[device_dir_index];
if (device_dir == NULL)
continue;
/* Clean up from tne last loop, if necessary. */
if (class) {
free(class);
class = NULL;
}
if (vendor) {
free(vendor);
vendor = NULL;
}
if (model) {
free(model);
model = NULL;
}
/* Read each of the three files holding the busclass,
vendor, and model information. */
snprintf(path, 256, "%s/%s/class", PATH_SYS_PCI, device_dir);
f = fopen(path, "r");
if (!f) continue;
getline(&class, &len, f);
fclose(f);
class[6] = '\0';
for (p = class; *(p + 2) != '\0'; p++) *p = *(p + 2);
*p = '\0';
snprintf(path, 256, "%s/%s/vendor", PATH_SYS_PCI, device_dir);
f = fopen(path, "r");
if (!f) continue;
getline(&vendor, &len, f);
fclose(f);
vendor[6] = '\0';
for (p = vendor; *(p + 2) != '\0'; p++) *p = *(p + 2);
*p = '\0';
snprintf(path, 256, "%s/%s/device", PATH_SYS_PCI, device_dir);
f = fopen(path, "r");
if (!f) continue;
getline(&model, &len, f);
fclose(f);
model[6] = '\0';
for (p = model; *(p + 2) != '\0'; p++) *p = *(p + 2);
*p = '\0';
/* Create a new sysdep node and populate it. */
node = _discover_sysdep_data_new();
if (!node) continue;
node->busclass = class;
node->vendor = vendor;
node->model = model;
class = vendor = model = NULL;
/* Stick the new node in the list. */
if (head == NULL) {
head = node;
last = head;
} else {
last->next = node;
last = node;
}
/* Clean up the directory list memory as we go. */
free(device_dir);
}
free(device_dir_list);
return head;
}
discover_sysdep_data_t *
_discover_get_pci_raw(void)
{
if (!access(PATH_SYS_PCI, R_OK))
return _discover_get_pci_raw_sys();
else
return _discover_get_pci_raw_proc();
}
/*
* Local variables:
* c-file-style: "progeny"
* indent-tabs-mode: nil
* End:
*/
/* vim: set cin fo=tcroq sw=4 et sts=4 tw=75: */
|