1 /*	$NetBSD: nouveau_core_option.c,v 1.1.1.1 2014/08/06 12:36:24 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 
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: nouveau_core_option.c,v 1.1.1.1 2014/08/06 12:36:24 riastradh Exp $");
29 
30 #include <core/option.h>
31 #include <core/debug.h>
32 
33 const char *
nouveau_stropt(const char * optstr,const char * opt,int * arglen)34 nouveau_stropt(const char *optstr, const char *opt, int *arglen)
35 {
36 	while (optstr && *optstr != '\0') {
37 		int len = strcspn(optstr, ",=");
38 		switch (optstr[len]) {
39 		case '=':
40 			if (!strncasecmpz(optstr, opt, len)) {
41 				optstr += len + 1;
42 				*arglen = strcspn(optstr, ",=");
43 				return *arglen ? optstr : NULL;
44 			}
45 			optstr++;
46 			break;
47 		case ',':
48 			optstr++;
49 			break;
50 		default:
51 			break;
52 		}
53 		optstr += len;
54 	}
55 
56 	return NULL;
57 }
58 
59 bool
nouveau_boolopt(const char * optstr,const char * opt,bool value)60 nouveau_boolopt(const char *optstr, const char *opt, bool value)
61 {
62 	int arglen;
63 
64 	optstr = nouveau_stropt(optstr, opt, &arglen);
65 	if (optstr) {
66 		if (!strncasecmpz(optstr, "0", arglen) ||
67 		    !strncasecmpz(optstr, "no", arglen) ||
68 		    !strncasecmpz(optstr, "off", arglen) ||
69 		    !strncasecmpz(optstr, "false", arglen))
70 			value = false;
71 		else
72 		if (!strncasecmpz(optstr, "1", arglen) ||
73 		    !strncasecmpz(optstr, "yes", arglen) ||
74 		    !strncasecmpz(optstr, "on", arglen) ||
75 		    !strncasecmpz(optstr, "true", arglen))
76 			value = true;
77 	}
78 
79 	return value;
80 }
81 
82 int
nouveau_dbgopt(const char * optstr,const char * sub)83 nouveau_dbgopt(const char *optstr, const char *sub)
84 {
85 	int mode = 1, level = CONFIG_NOUVEAU_DEBUG_DEFAULT;
86 
87 	while (optstr) {
88 		int len = strcspn(optstr, ",=");
89 		switch (optstr[len]) {
90 		case '=':
91 			if (strncasecmpz(optstr, sub, len))
92 				mode = 0;
93 			optstr++;
94 			break;
95 		default:
96 			if (mode) {
97 				if (!strncasecmpz(optstr, "fatal", len))
98 					level = NV_DBG_FATAL;
99 				else if (!strncasecmpz(optstr, "error", len))
100 					level = NV_DBG_ERROR;
101 				else if (!strncasecmpz(optstr, "warn", len))
102 					level = NV_DBG_WARN;
103 				else if (!strncasecmpz(optstr, "info", len))
104 					level = NV_DBG_INFO_NORMAL;
105 				else if (!strncasecmpz(optstr, "debug", len))
106 					level = NV_DBG_DEBUG;
107 				else if (!strncasecmpz(optstr, "trace", len))
108 					level = NV_DBG_TRACE;
109 				else if (!strncasecmpz(optstr, "paranoia", len))
110 					level = NV_DBG_PARANOIA;
111 				else if (!strncasecmpz(optstr, "spam", len))
112 					level = NV_DBG_SPAM;
113 			}
114 
115 			if (optstr[len] != '\0') {
116 				optstr++;
117 				mode = 1;
118 				break;
119 			}
120 
121 			return level;
122 		}
123 		optstr += len;
124 	}
125 
126 	return level;
127 }
128