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