Home | History | Annotate | Line # | Download | only in drm
drm_sysctl.c revision 1.5
      1 /*-
      2  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Christos Zoulas.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: drm_sysctl.c,v 1.5 2015/02/25 14:00:52 riastradh Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/types.h>
     34 #include <sys/systm.h>
     35 #include <sys/sysctl.h>
     36 #include <linux/module.h>
     37 #include <linux/moduleparam.h>
     38 /* We need to specify the type programmatically */
     39 #undef sysctl_createv
     40 
     41 #include <drm/drm_sysctl.h>
     42 
     43 #ifdef SYSCTL_INCLUDE_DESCR
     44 static const char *
     45 drm_sysctl_get_description(const struct linux_module_param_info *p,
     46     const struct drm_sysctl_def *def)
     47 {
     48 	const void * const *b = def->bd, * const *e = def->ed;
     49 
     50 	for (; b < e; b++) {
     51 		const struct linux_module_param_desc *d = *b;
     52 		if (strcmp(p->dname, d->name) == 0)
     53 			return d->description;
     54 	}
     55 	return NULL;
     56 }
     57 #endif
     58 
     59 #ifdef notyet
     60 static uint64_t
     61 drm_sysctl_get_value(const struct linux_module_param_info *p)
     62 {
     63 	switch (p->type) {
     64 	case MTYPE_bool:
     65 		return *(bool *)p->ptr;
     66 	case MTYPE_int:
     67 		return *(int *)p->ptr;
     68 	default:
     69 		aprint_error("unhandled module param type %d for %s\n",
     70 		    p->type, p->name);
     71 		return 0;
     72 	}
     73 }
     74 
     75 static size_t
     76 drm_sysctl_get_size(const struct linux_module_param_info *p)
     77 {
     78 	switch (p->type) {
     79 	case MTYPE_bool:
     80 		return sizeof(bool);
     81 	case MTYPE_int:
     82 		return sizeof(int);
     83 	default:
     84 		aprint_error("unhandled module param type %d for %s\n",
     85 		    p->type, p->name);
     86 		return sizeof(void *);
     87 	}
     88 }
     89 #endif
     90 
     91 static int
     92 drm_sysctl_get_type(const struct linux_module_param_info *p)
     93 {
     94 	switch (p->type) {
     95 	case MTYPE_bool:
     96 		return CTLTYPE_BOOL;
     97 	case MTYPE_int:
     98 		return CTLTYPE_INT;
     99 	case MTYPE_charp:
    100 		return CTLTYPE_STRING;
    101 	default:
    102 		aprint_error("unhandled module param type %d for %s\n",
    103 		    p->type, p->name);
    104 		return CTLTYPE_NODE;
    105 	}
    106 }
    107 
    108 
    109 static int
    110 drm_sysctl_node(const char *name, const struct sysctlnode **node,
    111     struct sysctllog **log)
    112 {
    113 	return sysctl_createv(log, 0, node, node,
    114 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, name, NULL,
    115 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    116 }
    117 
    118 
    119 void
    120 drm_sysctl_init(struct drm_sysctl_def *def)
    121 {
    122 	const void * const *b = def->bp, * const *e = def->ep;
    123 	const struct sysctlnode *rnode = NULL, *cnode;
    124 	const char *name = "drm2";
    125 
    126 	int error;
    127 	if ((error = sysctl_createv(&def->log, 0, NULL, &rnode,
    128 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, name,
    129 	    SYSCTL_DESCR("DRM driver parameters"),
    130 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
    131 		aprint_error("sysctl_createv returned %d, "
    132 		    "for %s ignoring\n", error, name);
    133 		return;
    134 	}
    135 
    136 	for (; b < e; b++) {
    137 		const struct linux_module_param_info *p = *b;
    138 		char copy[256], *n, *nn;
    139 		strlcpy(copy, p->name, sizeof(copy));
    140 		cnode = rnode;
    141 		for (n = copy; (nn = strchr(n, '.')) != NULL; n = nn) {
    142 			*nn++ = '\0';
    143 			if ((error = drm_sysctl_node(n, &cnode, &def->log))
    144 			    != 0) {
    145 				aprint_error("sysctl_createv returned %d, "
    146 				    "for %s ignoring\n", error, n);
    147 				continue;
    148 			}
    149 		}
    150 
    151 	        if ((error = sysctl_createv(&def->log, 0, &cnode,
    152 		    &cnode, p->mode == 0600 ? CTLFLAG_READWRITE : 0,
    153 		    drm_sysctl_get_type(p), n,
    154 		    SYSCTL_DESCR(drm_sysctl_get_description(p, def)),
    155 		    NULL, 0, p->ptr, 0, CTL_CREATE, CTL_EOL)) != 0)
    156 			aprint_error("sysctl_createv returned %d, "
    157 			    "for %s ignoring\n", error, n);
    158 	}
    159 }
    160 
    161 void
    162 drm_sysctl_fini(struct drm_sysctl_def *def)
    163 {
    164 	sysctl_teardown(&def->log);
    165 }
    166