00001 /* 00002 * Copyright (c) 2010 Lenka Trochtova 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 00009 * - Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * - Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * - The name of the author may not be used to endorse or promote products 00015 * derived from this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00020 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 00033 #include <str.h> 00034 00035 #include "devman.h" 00036 00044 static int compute_match_score(match_id_t *driver, match_id_t *device) 00045 { 00046 if (str_cmp(driver->id, device->id) == 0) { 00047 /* 00048 * The strings match, return the product of their scores. 00049 */ 00050 return driver->score * device->score; 00051 } else { 00052 /* 00053 * Different strings, return zero. 00054 */ 00055 return 0; 00056 } 00057 } 00058 00059 int get_match_score(driver_t *drv, dev_node_t *dev) 00060 { 00061 link_t *drv_head = &drv->match_ids.ids; 00062 link_t *dev_head = &dev->pfun->match_ids.ids; 00063 00064 if (list_empty(drv_head) || list_empty(dev_head)) 00065 return 0; 00066 00067 /* 00068 * Go through all pairs, return the highest score obtained. 00069 */ 00070 int highest_score = 0; 00071 00072 link_t *drv_link = drv->match_ids.ids.next; 00073 while (drv_link != drv_head) { 00074 link_t *dev_link = dev_head->next; 00075 while (dev_link != dev_head) { 00076 match_id_t *drv_id = list_get_instance(drv_link, match_id_t, link); 00077 match_id_t *dev_id = list_get_instance(dev_link, match_id_t, link); 00078 00079 int score = compute_match_score(drv_id, dev_id); 00080 if (score > highest_score) { 00081 highest_score = score; 00082 } 00083 00084 dev_link = dev_link->next; 00085 } 00086 00087 drv_link = drv_link->next; 00088 } 00089 00090 return highest_score; 00091 } 00092