ipmi_si: Introduce ipmi_panic_event_str[] array

Instead of repeating twice the constant literals, introduce
ipmi_panic_event_str[] array. It allows to simplify the code
with help of match_string() API.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Message-Id: <20210402174334.13466-6-andriy.shevchenko@linux.intel.com>
Signed-off-by: Corey Minyard <cminyard@mvista.com>
This commit is contained in:
Andy Shevchenko 2021-04-02 20:43:29 +03:00 коммит произвёл Corey Minyard
Родитель bd7a33bfa1
Коммит 649a7d46d0
1 изменённых файлов: 19 добавлений и 32 удалений

Просмотреть файл

@ -49,11 +49,17 @@ static int handle_one_recv_msg(struct ipmi_smi *intf,
static bool initialized; static bool initialized;
static bool drvregistered; static bool drvregistered;
/* Numbers in this enumerator should be mapped to ipmi_panic_event_str */
enum ipmi_panic_event_op { enum ipmi_panic_event_op {
IPMI_SEND_PANIC_EVENT_NONE, IPMI_SEND_PANIC_EVENT_NONE,
IPMI_SEND_PANIC_EVENT, IPMI_SEND_PANIC_EVENT,
IPMI_SEND_PANIC_EVENT_STRING IPMI_SEND_PANIC_EVENT_STRING,
IPMI_SEND_PANIC_EVENT_MAX
}; };
/* Indices in this array should be mapped to enum ipmi_panic_event_op */
static const char *const ipmi_panic_event_str[] = { "none", "event", "string", NULL };
#ifdef CONFIG_IPMI_PANIC_STRING #ifdef CONFIG_IPMI_PANIC_STRING
#define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_STRING #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_STRING
#elif defined(CONFIG_IPMI_PANIC_EVENT) #elif defined(CONFIG_IPMI_PANIC_EVENT)
@ -68,46 +74,27 @@ static int panic_op_write_handler(const char *val,
const struct kernel_param *kp) const struct kernel_param *kp)
{ {
char valcp[16]; char valcp[16];
char *s; int e;
strncpy(valcp, val, 15); strscpy(valcp, val, sizeof(valcp));
valcp[15] = '\0'; e = match_string(ipmi_panic_event_str, -1, strstrip(valcp));
if (e < 0)
s = strstrip(valcp); return e;
if (strcmp(s, "none") == 0)
ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_NONE;
else if (strcmp(s, "event") == 0)
ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT;
else if (strcmp(s, "string") == 0)
ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_STRING;
else
return -EINVAL;
ipmi_send_panic_event = e;
return 0; return 0;
} }
static int panic_op_read_handler(char *buffer, const struct kernel_param *kp) static int panic_op_read_handler(char *buffer, const struct kernel_param *kp)
{ {
switch (ipmi_send_panic_event) { const char *event_str;
case IPMI_SEND_PANIC_EVENT_NONE:
strcpy(buffer, "none\n");
break;
case IPMI_SEND_PANIC_EVENT: if (ipmi_send_panic_event >= IPMI_SEND_PANIC_EVENT_MAX)
strcpy(buffer, "event\n"); event_str = "???";
break; else
event_str = ipmi_panic_event_str[ipmi_send_panic_event];
case IPMI_SEND_PANIC_EVENT_STRING: return sprintf(buffer, "%s\n", event_str);
strcpy(buffer, "string\n");
break;
default:
strcpy(buffer, "???\n");
break;
}
return strlen(buffer);
} }
static const struct kernel_param_ops panic_op_ops = { static const struct kernel_param_ops panic_op_ops = {