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