1 /* $NetBSD: led.c,v 1.3 2018/01/10 15:58:40 jakllsch Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: led.c,v 1.3 2018/01/10 15:58:40 jakllsch Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/kmem.h>
36 #include <sys/queue.h>
37 #include <sys/sysctl.h>
38 #include <sys/once.h>
39
40 #include <dev/led.h>
41
42 struct led_device {
43 char *name;
44 led_getstate_fn getstate;
45 led_setstate_fn setstate;
46 void *priv;
47
48 struct sysctllog *slog;
49
50 TAILQ_ENTRY(led_device) devices;
51 };
52
53 static TAILQ_HEAD(, led_device) led_devices =
54 TAILQ_HEAD_INITIALIZER(led_devices);
55 static kmutex_t led_lock;
56
57 static int
led_init(void)58 led_init(void)
59 {
60 mutex_init(&led_lock, MUTEX_DEFAULT, IPL_NONE);
61
62 return 0;
63 }
64
65 static struct led_device *
led_lookup(const char * name)66 led_lookup(const char *name)
67 {
68 struct led_device *led;
69
70 KASSERT(mutex_owned(&led_lock));
71
72 TAILQ_FOREACH(led, &led_devices, devices)
73 if (strcmp(led->name, name) == 0)
74 return led;
75
76 return NULL;
77 }
78
79 static void
led_normalize_name(char * name)80 led_normalize_name(char *name)
81 {
82 unsigned char *p;
83
84 for (p = (unsigned char *)name; *p; p++)
85 if (!isalpha(*p) && !isdigit(*p) && *p != '-' && *p != '_')
86 *p = '_';
87 }
88
89 static void
led_free(struct led_device * led)90 led_free(struct led_device *led)
91 {
92 KASSERT(mutex_owned(&led_lock));
93
94 kmem_free(led->name, strlen(led->name) + 1);
95 kmem_free(led, sizeof(*led));
96 }
97
98 static int
led_sysctl_handler(SYSCTLFN_ARGS)99 led_sysctl_handler(SYSCTLFN_ARGS)
100 {
101 struct sysctlnode node;
102 struct led_device *led;
103 int error;
104 bool state;
105
106 mutex_enter(&led_lock);
107
108 node = *rnode;
109 led = node.sysctl_data;
110 state = led->getstate(led->priv);
111 node.sysctl_data = &state;
112 error = sysctl_lookup(SYSCTLFN_CALL(&node));
113 if (error || newp == NULL) {
114 mutex_exit(&led_lock);
115 return error;
116 }
117
118 led->setstate(led->priv, state);
119
120 mutex_exit(&led_lock);
121
122 return 0;
123 }
124
125 void *
led_attach(const char * name,void * priv,led_getstate_fn getstate,led_setstate_fn setstate)126 led_attach(const char *name, void *priv, led_getstate_fn getstate,
127 led_setstate_fn setstate)
128 {
129 static ONCE_DECL(control);
130 struct led_device *led;
131 const struct sysctlnode *node;
132 int error;
133
134 if (RUN_ONCE(&control, led_init) != 0)
135 return NULL;
136
137 led = kmem_zalloc(sizeof(*led), KM_SLEEP);
138 led->name = kmem_asprintf("%s", name);
139 led->getstate = getstate;
140 led->setstate = setstate;
141 led->priv = priv;
142
143 /* Convert invalid sysctl node name characters to underscores */
144 led_normalize_name(led->name);
145
146 mutex_enter(&led_lock);
147 if (led_lookup(name) != NULL) {
148 led_free(led);
149 led = NULL;
150 } else {
151 error = sysctl_createv(&led->slog, 0, NULL, &node,
152 CTLFLAG_PERMANENT, CTLTYPE_NODE, "led", NULL,
153 NULL, 0, NULL, 0,
154 CTL_HW, CTL_CREATE, CTL_EOL);
155 if (error == 0) {
156 error = sysctl_createv(&led->slog, 0, &node, NULL,
157 CTLFLAG_READWRITE, CTLTYPE_BOOL, led->name, NULL,
158 led_sysctl_handler, 0,
159 (void *)led, 0,
160 CTL_CREATE, CTL_EOL);
161 }
162 if (error != 0) {
163 printf("led: failed to create sysctl hw.led.%s: %d\n",
164 led->name, error);
165 led_free(led);
166 led = NULL;
167 }
168 }
169 if (led != NULL)
170 TAILQ_INSERT_TAIL(&led_devices, led, devices);
171 mutex_exit(&led_lock);
172
173 return led;
174 }
175
176 void
led_detach(void * handle)177 led_detach(void *handle)
178 {
179 struct led_device *led = handle;
180
181 mutex_enter(&led_lock);
182
183 TAILQ_REMOVE(&led_devices, led, devices);
184 sysctl_teardown(&led->slog);
185 led_free(led);
186
187 mutex_exit(&led_lock);
188 }
189