Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:16

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /* Driver Manager Driver Resource Interface Implementation.
0004  *
0005  * COPYRIGHT (c) 2009 Cobham Gaisler AB.
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #include <string.h>
0030 #include <drvmgr/drvmgr.h>
0031 
0032 /* Find all the resource keys for a device among all bus resources */
0033 int drvmgr_keys_get(struct drvmgr_dev *dev, struct drvmgr_key **keys)
0034 {
0035     struct drvmgr_bus *bus;
0036     struct drvmgr_bus_res *node;
0037     struct drvmgr_drv_res *res;
0038     uint64_t drv_id;
0039 
0040     bus = dev->parent;
0041     if (!bus || !dev->drv)
0042         return -1;
0043 
0044     drv_id = dev->drv->drv_id;
0045 
0046     /* Loop all resource arrays */
0047     node = bus->reslist;
0048     while (node) {
0049         /* Find driver ID in resource array */
0050         res = &node->resource[0];
0051         while (res->drv_id) {
0052             if (res->drv_id == drv_id) {
0053                 /* Found resource matching driver, now check
0054                  * that this resource is for this device.
0055                  */
0056                 if (dev->minor_bus == res->minor_bus) {
0057                     /* Matching driver and core number */
0058                     if (keys)
0059                         *keys = res->keys;
0060                     return 0;
0061                 }
0062             }
0063             res++;
0064         }
0065         node = node->next;
0066     }
0067     if (keys)
0068         *keys = NULL;
0069     return 1;
0070 }
0071 
0072 /* Return key that matches key name */
0073 struct drvmgr_key *drvmgr_key_get(
0074     struct drvmgr_key *keys,
0075     char *key_name)
0076 {
0077     struct drvmgr_key *key;
0078 
0079     if (!keys)
0080         return NULL;
0081 
0082     key = keys;
0083     while (key->key_type != DRVMGR_KT_NONE) {
0084         if (strcmp(key_name, key->key_name) == 0)
0085             return key;
0086         key++;
0087     }
0088     return NULL;
0089 }
0090 
0091 union drvmgr_key_value *drvmgr_key_val_get(
0092     struct drvmgr_key *keys,
0093     char *key_name,
0094     enum drvmgr_kt key_type)
0095 {
0096     struct drvmgr_key *key_match;
0097 
0098     key_match = drvmgr_key_get(keys, key_name);
0099     if (key_match) {
0100         /* Found key, put pointer to value into */
0101         if ((key_type == DRVMGR_KT_ANY) ||
0102             (key_match->key_type == key_type))
0103             return &key_match->key_value;
0104     }
0105     return NULL;
0106 }
0107 
0108 union drvmgr_key_value *drvmgr_dev_key_get(
0109     struct drvmgr_dev *dev,
0110     char *key_name,
0111     enum drvmgr_kt key_type)
0112 {
0113     struct drvmgr_key *keys = NULL;
0114 
0115     /* Find first entry in key array for the device */
0116     if (drvmgr_keys_get(dev, &keys))
0117         return NULL;
0118 
0119     /* Find a specific key among the device keys */
0120     return drvmgr_key_val_get(keys, key_name, key_type);
0121 }