Main Page | Modules | Data Structures | File List | Data Fields | Globals

sysdep.c

Go to the documentation of this file.
00001 
00010 /* $Progeny: sysdep.c 3839 2003-11-17 04:25:01Z dsp $
00011  *
00012  * AUTHOR: John R. Daily <jdaily@progeny.com>
00013  *
00014  * Copyright 2002 Progeny Linux Systems, Inc.
00015  *
00016  * Permission is hereby granted, free of charge, to any person obtaining a
00017  * copy of this software and associated documentation files (the "Software"),
00018  * to deal in the Software without restriction, including without limitation
00019  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00020  * and/or sell copies of the Software, and to permit persons to whom the
00021  * Software is furnished to do so, subject to the following conditions:
00022  *
00023  * The above copyright notice and this permission notice shall be included in
00024  * all copies or substantial portions of the Software.
00025  *
00026  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00027  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00028  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00029  * THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00030  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00031  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00032  * DEALINGS IN THE SOFTWARE.
00033  */
00034 #include <assert.h>
00035 
00036 #include <stdio.h>
00037 #include <string.h>
00038 
00039 #include <discover.h>
00040 #include <discover-conf.h>
00041 #include <discover-xml.h>
00042 #include <sysdep.h>
00043 
00044 #include "device.h"
00045 #include "utils.h"
00046 
00047 
00049 typedef discover_sysdep_data_t *(raw_sysdep_function_t)(void);
00050 
00051 static raw_sysdep_function_t *raw_map[] = {
00052     _discover_get_ata_raw,
00053     _discover_get_pci_raw,
00054     _discover_get_pcmcia_raw,
00055     _discover_get_scsi_raw,
00056     _discover_get_usb_raw
00057 };
00058 
00059 
00060 /*
00061  * Utility functions
00062  */
00063 
00065 discover_sysdep_data_t *
00066 _discover_sysdep_data_new(void)
00067 {
00068     discover_sysdep_data_t *node =
00069         _discover_xmalloc(sizeof(discover_sysdep_data_t));
00070     node->busclass = NULL;
00071     node->vendor = NULL;
00072     node->model = NULL;
00073     node->next = NULL;
00074     return node;
00075 }
00076 
00078 void
00079 _discover_free_sysdep_data(discover_sysdep_data_t *head)
00080 {
00081     discover_sysdep_data_t *node;
00082 
00083     while (head) {
00084         node = head->next;
00085         if (head->vendor) {
00086             free(head->vendor);
00087         }
00088         if (head->model) {
00089             free(head->model);
00090         }
00091         if (head->busclass) {
00092             free(head->busclass);
00093         }
00094         free(head);
00095         head = node;
00096     }
00097 }
00098 
00099 /*
00100  * Functions that access the sysdeps
00101  */
00102 static discover_device_t *devices[BUS_COUNT];
00103 
00104 discover_device_t *
00105 discover_get_devices(discover_bus_t bus, discover_error_t *status)
00106 {
00107     discover_device_t *device, *last;
00108     discover_device_t *xml_devices;
00109     discover_bus_map_t *busmap;
00110     discover_sysdep_data_t *head, *node;
00111 
00112     assert(status);
00113 
00114     status->code = DISCOVER_SUCCESS;
00115     device = last = NULL;
00116 
00117     busmap = discover_conf_get_bus_map(bus, status);
00118     if (status->code != 0) {
00119         return NULL;
00120     }
00121 
00122     if (busmap->scan_never) {
00123         status->code = DISCOVER_EBUSDISABLED;
00124         return NULL;
00125     }
00126 
00127     if (devices[bus]) {
00128         return devices[bus];
00129     }
00130 
00131     xml_devices = discover_xml_get_devices(bus, status);
00132     if (!xml_devices) {
00133         return NULL;
00134     }
00135 
00136     /*
00137      * Allow overrides of this function.
00138      */
00139     if (busmap->get_raw) {
00140         head = node = busmap->get_raw();
00141     } else {
00142         head = node = raw_map[bus]();
00143     }
00144 
00145     while (node) {
00146         device =
00147             discover_xml_get_matching_devices(xml_devices, node->vendor,
00148                                               node->model, status);
00149 
00150         if (!device) {
00151             device = discover_device_new();
00152             device->model_id = strdup(node->model);
00153             device->vendor_id = strdup(node->vendor);
00154         }
00155 
00156         if (last) {
00157             last->next = device;
00158             last = device;
00159         } else {
00160             devices[bus] = last = device;
00161         }
00162 
00163         node = node->next;
00164     }
00165 
00166     _discover_free_sysdep_data(head);
00167 
00168     return devices[bus];
00169 }
00170 
00171 void
00172 discover_free_devices(void)
00173 {
00174     int i;
00175 
00176     for (i = 0; i < BUS_COUNT; i++) {
00177         discover_device_free(devices[i], 0);
00178         devices[i] = NULL;
00179     }
00180 }
00181 
00182 /*
00183  * Local variables:
00184  * c-file-style: "progeny"
00185  * indent-tabs-mode: nil
00186  * End:
00187  */
00188 /* vim: set cin fo=tcroq sw=4 et sts=4 tw=75: */

Generated on Sat Jan 31 14:39:17 2004 for discover by doxygen 1.3.4