1/*	$NetBSD: nouveau_nvkm_core_option.c,v 1.5 2021/12/18 23:45:34 riastradh Exp $	*/
2
3/*
4 * Copyright 2012 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs
25 */
26#include <sys/cdefs.h>
27__KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_core_option.c,v 1.5 2021/12/18 23:45:34 riastradh Exp $");
28
29#include <core/option.h>
30#include <core/debug.h>
31
32const char *
33nvkm_stropt(const char *optstr, const char *opt, int *arglen)
34{
35	while (optstr && *optstr != '\0') {
36		int len = strcspn(optstr, ",=");
37		switch (optstr[len]) {
38		case '=':
39			if (!strncasecmpz(optstr, opt, len)) {
40				optstr += len + 1;
41				*arglen = strcspn(optstr, ",=");
42				return *arglen ? optstr : NULL;
43			}
44			optstr++;
45			break;
46		case ',':
47			optstr++;
48			break;
49		default:
50			break;
51		}
52		optstr += len;
53	}
54
55	return NULL;
56}
57
58bool
59nvkm_boolopt(const char *optstr, const char *opt, bool value)
60{
61	int arglen;
62
63	optstr = nvkm_stropt(optstr, opt, &arglen);
64	if (optstr) {
65		if (!strncasecmpz(optstr, "0", arglen) ||
66		    !strncasecmpz(optstr, "no", arglen) ||
67		    !strncasecmpz(optstr, "off", arglen) ||
68		    !strncasecmpz(optstr, "false", arglen))
69			value = false;
70		else
71		if (!strncasecmpz(optstr, "1", arglen) ||
72		    !strncasecmpz(optstr, "yes", arglen) ||
73		    !strncasecmpz(optstr, "on", arglen) ||
74		    !strncasecmpz(optstr, "true", arglen))
75			value = true;
76	}
77
78	return value;
79}
80
81long
82nvkm_longopt(const char *optstr, const char *opt, long value)
83{
84	long result = value;
85	int arglen;
86	char *s;
87
88	optstr = nvkm_stropt(optstr, opt, &arglen);
89	if (optstr && (s = kstrndup(optstr, arglen, GFP_KERNEL))) {
90		int ret = kstrtol(s, 0, &value);
91		if (ret == 0)
92			result = value;
93		kfree(s);
94	}
95
96	return result;
97}
98
99int
100nvkm_dbgopt(const char *optstr, const char *sub)
101{
102	int mode = 1, level = CONFIG_NOUVEAU_DEBUG_DEFAULT;
103
104	while (optstr) {
105		int len = strcspn(optstr, ",=");
106		switch (optstr[len]) {
107		case '=':
108			if (strncasecmpz(optstr, sub, len))
109				mode = 0;
110			optstr++;
111			break;
112		default:
113			if (mode) {
114				if (!strncasecmpz(optstr, "fatal", len))
115					level = NV_DBG_FATAL;
116				else if (!strncasecmpz(optstr, "error", len))
117					level = NV_DBG_ERROR;
118				else if (!strncasecmpz(optstr, "warn", len))
119					level = NV_DBG_WARN;
120				else if (!strncasecmpz(optstr, "info", len))
121					level = NV_DBG_INFO;
122				else if (!strncasecmpz(optstr, "debug", len))
123					level = NV_DBG_DEBUG;
124				else if (!strncasecmpz(optstr, "trace", len))
125					level = NV_DBG_TRACE;
126				else if (!strncasecmpz(optstr, "paranoia", len))
127					level = NV_DBG_PARANOIA;
128				else if (!strncasecmpz(optstr, "spam", len))
129					level = NV_DBG_SPAM;
130			}
131
132			if (optstr[len] != '\0') {
133				optstr++;
134				mode = 1;
135				break;
136			}
137
138			return level;
139		}
140		optstr += len;
141	}
142
143	return level;
144}
145