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