1 /* $NetBSD: drm_sysctl.c,v 1.7 2018/08/27 13:57:24 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.7 2018/08/27 13:57:24 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 127 void 128 drm_sysctl_init(struct drm_sysctl_def *def) 129 { 130 const void * const *b = def->bp, * const *e = def->ep; 131 const struct sysctlnode *rnode = NULL, *cnode; 132 const char *name = "drm2"; 133 134 int error; 135 if ((error = sysctl_createv(&def->log, 0, NULL, &rnode, 136 CTLFLAG_PERMANENT, CTLTYPE_NODE, name, 137 SYSCTL_DESCR("DRM driver parameters"), 138 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) { 139 aprint_error("sysctl_createv returned %d, " 140 "for %s ignoring\n", error, name); 141 return; 142 } 143 144 for (; b < e; b++) { 145 const struct linux_module_param_info *p = *b; 146 char copy[256], *n, *nn; 147 strlcpy(copy, p->name, sizeof(copy)); 148 cnode = rnode; 149 for (n = copy; (nn = strchr(n, '.')) != NULL; n = nn) { 150 *nn++ = '\0'; 151 if ((error = drm_sysctl_node(n, &cnode, &def->log)) 152 != 0) { 153 aprint_error("sysctl_createv returned %d, " 154 "for %s ignoring\n", error, n); 155 continue; 156 } 157 } 158 159 if ((error = sysctl_createv(&def->log, 0, &cnode, 160 &cnode, p->mode == 0600 ? CTLFLAG_READWRITE : 0, 161 drm_sysctl_get_type(p), n, 162 SYSCTL_DESCR(drm_sysctl_get_description(p, def)), 163 NULL, 0, p->ptr, 0, CTL_CREATE, CTL_EOL)) != 0) 164 aprint_error("sysctl_createv returned %d, " 165 "for %s ignoring\n", error, n); 166 } 167 } 168 169 void 170 drm_sysctl_fini(struct drm_sysctl_def *def) 171 { 172 sysctl_teardown(&def->log); 173 } 174