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