attrib.c (4555B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * File: attrib.c 8 * Purpose: board attribute management 9 */ 10 11 #include <sal/core/alloc.h> 12 #include <sal/core/libc.h> 13 #include <bcm/error.h> 14 #include <board/board.h> 15 #include <board/manager.h> 16 #include "mgr.h" 17 18 19 /* 20 * Function: 21 * _attribute_match 22 * Purpose: 23 * Match a requested attribute name with the registered one. 24 * Right now, just do a simple string match. It's possible 25 * to do something more complicated along the lines of the 26 * soc property suffixes, to pass additional data to the 27 * attribute function via the name field. 28 * 29 * Parameters: 30 * reqname - (IN) requested name 31 * regname - (IN) registered name 32 * Returns: 33 * 0 - match 34 * !0 - no match 35 */ 36 STATIC int 37 _attribute_match(char *reqname, char *regname) 38 { 39 return !sal_strcmp(reqname, regname); 40 } 41 42 /* 43 * Function: 44 * board_attribute_get 45 * Purpose: 46 * Return a value for an attribute of the current board. 47 * Parameters: 48 * attrib - (IN) board attribute id string 49 * value - (OUT) pointer to attribute value 50 * Returns: 51 * BCM_E_NONE - success 52 * BCM_E_UNAVAIL - attribute not supported by board driver 53 * BCM_E_NOT_FOUND - attribute unknown to board manager 54 * BCM_E_XXX - failed 55 */ 56 int 57 board_attribute_get(char *name, int *value) 58 { 59 int rv = BCM_E_FAIL; 60 board_driver_t *driver; 61 int i; 62 63 BOARD_INIT; 64 BOARD_LOCK; 65 if ((driver = board_driver())) { 66 for (i=0; i<driver->num_attribute; i++) { 67 if (_attribute_match(name, driver->attribute[i].name)) { 68 rv = BCM_E_UNAVAIL; 69 if (driver->attribute[i].get) { 70 rv = driver->attribute[i].get(driver, name, value); 71 } 72 break; 73 } 74 } 75 } 76 BOARD_UNLOCK; 77 78 return rv; 79 } 80 81 /* 82 * Function: 83 * board_attribute_set 84 * Purpose: 85 * Set attribute of the current board to the given value. 86 * Parameters: 87 * attrib - (IN) board attribute id string 88 * value - (IN) attribute value 89 * Returns: 90 * BCM_E_NONE - success 91 * BCM_E_UNAVAIL - attribute not supported by board driver 92 * BCM_E_NOT_FOUND - attribute unknown to board driver 93 * BCM_E_XXX - failed 94 */ 95 int 96 board_attribute_set(char *name, int value) 97 { 98 int rv = BCM_E_FAIL; 99 board_driver_t *driver; 100 int i; 101 102 BOARD_INIT; 103 BOARD_LOCK; 104 if ((driver = board_driver()) != NULL) { 105 for (i=0; i<driver->num_attribute; i++) { 106 if (_attribute_match(name, driver->attribute[i].name)) { 107 rv = BCM_E_UNAVAIL; 108 if (driver->attribute[i].set) { 109 rv = driver->attribute[i].set(driver, name, value); 110 } 111 break; 112 } 113 } 114 } 115 BOARD_UNLOCK; 116 117 return rv; 118 } 119 120 /* 121 * Function: 122 * board_attributes_get 123 * Purpose: 124 * Return an array of all attributes known by the board driver up 125 * to 'max_num' number of attributes. The actual length is returned 126 * in 'actual'. If 'max_num' is zero, then 'actual' will return the 127 * length of the attribute array, and not write to 'attrib'. 128 * Parameters: 129 * max_num - (IN) length of attribute array 130 * attrib - (OUT) array of board_attribute_t 131 * actual - (OUT) actual attribute array length 132 * Returns: 133 * BCM_E_NONE - success 134 * BCM_E_XXX - failed 135 */ 136 int 137 board_attributes_get(int max_num, board_attribute_t *attribute, int *actual) 138 { 139 int rv = BCM_E_UNAVAIL; 140 board_driver_t *driver; 141 142 BOARD_INIT; 143 BOARD_LOCK; 144 if ((driver = board_driver()) != NULL) { 145 if (!max_num) { 146 if (actual) { 147 *actual = driver->num_attribute; 148 rv = BCM_E_NONE; 149 } else { 150 rv = BCM_E_PARAM; 151 } 152 } else { 153 if (attribute) { 154 if (max_num < driver->num_attribute) { 155 max_num = driver->num_attribute; 156 } 157 sal_memcpy(attribute, 158 driver->attribute, 159 max_num*sizeof(board_attribute_t)); 160 if (actual) { 161 *actual = max_num; 162 } 163 rv = BCM_E_NONE; 164 } else { 165 rv = BCM_E_PARAM; 166 } 167 } 168 } 169 BOARD_UNLOCK; 170 171 return rv; 172 } 173