[e2b9a993] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup devman
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
[38b3baf] | 32 |
|
---|
[c47e1a8] | 33 | #include <str.h>
|
---|
[e2b9a993] | 34 |
|
---|
| 35 | #include "devman.h"
|
---|
| 36 |
|
---|
[cc70d57] | 37 | /** Compute compound score of driver and device.
|
---|
| 38 | *
|
---|
| 39 | * @param driver Match id of the driver.
|
---|
| 40 | * @param device Match id of the device.
|
---|
| 41 | * @return Compound score.
|
---|
| 42 | * @retval 0 No match at all.
|
---|
| 43 | */
|
---|
| 44 | static int compute_match_score(match_id_t *driver, match_id_t *device)
|
---|
| 45 | {
|
---|
| 46 | if (str_cmp(driver->id, device->id) == 0) {
|
---|
| 47 | /*
|
---|
[4006447] | 48 | * The strings match, return the product of their scores.
|
---|
[cc70d57] | 49 | */
|
---|
| 50 | return driver->score * device->score;
|
---|
| 51 | } else {
|
---|
| 52 | /*
|
---|
| 53 | * Different strings, return zero.
|
---|
| 54 | */
|
---|
| 55 | return 0;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[ba38f72c] | 59 | int get_match_score(driver_t *drv, dev_node_t *dev)
|
---|
[38b3baf] | 60 | {
|
---|
| 61 | link_t *drv_head = &drv->match_ids.ids;
|
---|
[ba38f72c] | 62 | link_t *dev_head = &dev->pfun->match_ids.ids;
|
---|
[e2b9a993] | 63 |
|
---|
[38b3baf] | 64 | if (list_empty(drv_head) || list_empty(dev_head))
|
---|
[e2b9a993] | 65 | return 0;
|
---|
| 66 |
|
---|
[cc70d57] | 67 | /*
|
---|
[4006447] | 68 | * Go through all pairs, return the highest score obtained.
|
---|
[cc70d57] | 69 | */
|
---|
| 70 | int highest_score = 0;
|
---|
[38b3baf] | 71 |
|
---|
[cc70d57] | 72 | link_t *drv_link = drv->match_ids.ids.next;
|
---|
| 73 | while (drv_link != drv_head) {
|
---|
| 74 | link_t *dev_link = dev_head->next;
|
---|
| 75 | while (dev_link != dev_head) {
|
---|
| 76 | match_id_t *drv_id = list_get_instance(drv_link, match_id_t, link);
|
---|
| 77 | match_id_t *dev_id = list_get_instance(dev_link, match_id_t, link);
|
---|
| 78 |
|
---|
| 79 | int score = compute_match_score(drv_id, dev_id);
|
---|
| 80 | if (score > highest_score) {
|
---|
| 81 | highest_score = score;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[e2b9a993] | 84 | dev_link = dev_link->next;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[cc70d57] | 87 | drv_link = drv_link->next;
|
---|
| 88 | }
|
---|
[e2b9a993] | 89 |
|
---|
[cc70d57] | 90 | return highest_score;
|
---|
[e2b9a993] | 91 | }
|
---|
| 92 |
|
---|
[38b3baf] | 93 | /** @}
|
---|
| 94 | */
|
---|