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