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