1/* $NetBSD: drm_sysctl.c,v 1.6 2015/07/30 04:36:48 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.6 2015/07/30 04:36:48 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
46static const char *
47drm_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
62static uint64_t
63drm_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 default:
71 aprint_error("unhandled module param type %d for %s\n",
72 p->type, p->name);
73 return 0;
74 }
75}
76
77static size_t
78drm_sysctl_get_size(const struct linux_module_param_info *p)
79{
80 switch (p->type) {
81 case MTYPE_bool:
82 return sizeof(bool);
83 case MTYPE_int:
84 return sizeof(int);
85 default:
86 aprint_error("unhandled module param type %d for %s\n",
87 p->type, p->name);
88 return sizeof(void *);
89 }
90}
91#endif
92
93static int
94drm_sysctl_get_type(const struct linux_module_param_info *p)
95{
96 switch (p->type) {
97 case MTYPE_bool:
98 return CTLTYPE_BOOL;
99 case MTYPE_int:
100 return CTLTYPE_INT;
101 case MTYPE_charp:
102 return CTLTYPE_STRING;
103 default:
104 aprint_error("unhandled module param type %d for %s\n",
105 p->type, p->name);
106 return CTLTYPE_NODE;
107 }
108}
109
110
111static int
112drm_sysctl_node(const char *name, const struct sysctlnode **node,
113 struct sysctllog **log)
114{
115 return sysctl_createv(log, 0, node, node,
116 CTLFLAG_PERMANENT, CTLTYPE_NODE, name, NULL,
117 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
118}
119
120
121void
122drm_sysctl_init(struct drm_sysctl_def *def)
123{
124 const void * const *b = def->bp, * const *e = def->ep;
125 const struct sysctlnode *rnode = NULL, *cnode;
126 const char *name = "drm2";
127
128 int error;
129 if ((error = sysctl_createv(&def->log, 0, NULL, &rnode,
130 CTLFLAG_PERMANENT, CTLTYPE_NODE, name,
131 SYSCTL_DESCR("DRM driver parameters"),
132 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
133 aprint_error("sysctl_createv returned %d, "
134 "for %s ignoring\n", error, name);
135 return;
136 }
137
138 for (; b < e; b++) {
139 const struct linux_module_param_info *p = *b;
140 char copy[256], *n, *nn;
141 strlcpy(copy, p->name, sizeof(copy));
142 cnode = rnode;
143 for (n = copy; (nn = strchr(n, '.')) != NULL; n = nn) {
144 *nn++ = '\0';
145 if ((error = drm_sysctl_node(n, &cnode, &def->log))
146 != 0) {
147 aprint_error("sysctl_createv returned %d, "
148 "for %s ignoring\n", error, n);
149 continue;
150 }
151 }
152
153 if ((error = sysctl_createv(&def->log, 0, &cnode,
154 &cnode, p->mode == 0600 ? CTLFLAG_READWRITE : 0,
155 drm_sysctl_get_type(p), n,
156 SYSCTL_DESCR(drm_sysctl_get_description(p, def)),
157 NULL, 0, p->ptr, 0, CTL_CREATE, CTL_EOL)) != 0)
158 aprint_error("sysctl_createv returned %d, "
159 "for %s ignoring\n", error, n);
160 }
161}
162
163void
164drm_sysctl_fini(struct drm_sysctl_def *def)
165{
166 sysctl_teardown(&def->log);
167}
168