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

sysdep.c

Go to the documentation of this file.
00001 
00010 /* $Progeny: sysdep.c 4368 2004-05-13 19:57:37Z licquia $
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/discover.h>
00040 #include <discover/discover-conf.h>
00041 #include <discover/discover-xml.h>
00042 #include <discover/sysdep.h>
00043 
00044 #include <discover/device.h>
00045 #include <discover/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         /*
00147          * The variable names are ambiguous:
00148          *   node   -  Hardware data retrieved from the OS
00149          *   device -  Matching hardware data from our database
00150          */
00151         device =
00152             discover_xml_get_matching_devices(xml_devices, node->vendor,
00153                                               node->model, status);
00154 
00155         if (!device) {
00156             device = discover_device_new();
00157             device->model_id = strdup(node->model);
00158             device->vendor_id = strdup(node->vendor);
00159         }
00160 
00161         /*
00162          * If we get a busclass from the hardware, treat it as
00163          * canonical.
00164          */
00165         if (node->busclass != NULL) {
00166             if (device->busclass != NULL) {
00167                 free(device->busclass);
00168             }
00169             device->busclass = strdup(node->busclass);
00170         }
00171 
00172         if (last) {
00173             last->next = device;
00174             last = device;
00175         } else {
00176             devices[bus] = last = device;
00177         }
00178 
00179         node = node->next;
00180     }
00181 
00182     _discover_free_sysdep_data(head);
00183 
00184     return devices[bus];
00185 }
00186 
00187 void
00188 discover_free_devices(void)
00189 {
00190     int i;
00191 
00192     for (i = 0; i < BUS_COUNT; i++) {
00193         discover_device_free(devices[i], 0);
00194         devices[i] = NULL;
00195     }
00196 }
00197 
00198 /*
00199  * Local variables:
00200  * c-file-style: "progeny"
00201  * indent-tabs-mode: nil
00202  * End:
00203  */
00204 /* vim: set cin fo=tcroq sw=4 et sts=4 tw=75: */

Generated on Mon Feb 7 02:23:03 2005 for discover by  doxygen 1.3.9.1