Home | History | Annotate | Line # | Download | only in dev
led.c revision 1.2
      1 /* $NetBSD: led.c,v 1.2 2017/07/08 19:25:37 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017 Jared McNeill <jmcneill (at) 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.2 2017/07/08 19:25:37 jmcneill 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
     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 *
     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
     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
     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
     99 led_sysctl_handler(SYSCTLFN_ARGS)
    100 {
    101 	struct sysctlnode node;
    102 	struct led_device *led;
    103 	int error, state;
    104 
    105 	mutex_enter(&led_lock);
    106 
    107 	node = *rnode;
    108 	led = node.sysctl_data;
    109 	state = led->getstate(led->priv);
    110 	node.sysctl_data = &state;
    111 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    112 	if (error || newp == NULL) {
    113 		mutex_exit(&led_lock);
    114 		return error;
    115 	}
    116 
    117 	if (state < LED_STATE_OFF || state > LED_STATE_ON) {
    118 		mutex_exit(&led_lock);
    119 		return EINVAL;
    120 	}
    121 
    122 	led->setstate(led->priv, state);
    123 
    124 	mutex_exit(&led_lock);
    125 
    126 	return 0;
    127 }
    128 
    129 void *
    130 led_attach(const char *name, void *priv, led_getstate_fn getstate,
    131     led_setstate_fn setstate)
    132 {
    133 	static ONCE_DECL(control);
    134 	struct led_device *led;
    135 	const struct sysctlnode *node;
    136 	int error;
    137 
    138 	if (RUN_ONCE(&control, led_init) != 0)
    139 		return NULL;
    140 
    141 	led = kmem_zalloc(sizeof(*led), KM_SLEEP);
    142 	led->name = kmem_asprintf("%s", name);
    143 	led->getstate = getstate;
    144 	led->setstate = setstate;
    145 	led->priv = priv;
    146 
    147 	/* Convert invalid sysctl node name characters to underscores */
    148 	led_normalize_name(led->name);
    149 
    150 	mutex_enter(&led_lock);
    151 	if (led_lookup(name) != NULL) {
    152 		led_free(led);
    153 		led = NULL;
    154 	} else {
    155 		error = sysctl_createv(&led->slog, 0, NULL, &node,
    156 		    CTLFLAG_PERMANENT, CTLTYPE_NODE, "led", NULL,
    157 		    NULL, 0, NULL, 0,
    158 		    CTL_HW, CTL_CREATE, CTL_EOL);
    159 		if (error == 0) {
    160 			error = sysctl_createv(&led->slog, 0, &node, NULL,
    161 			    CTLFLAG_READWRITE, CTLTYPE_BOOL, led->name, NULL,
    162 			    led_sysctl_handler, 0,
    163 			    (void *)led, 0,
    164 			    CTL_CREATE, CTL_EOL);
    165 		}
    166 		if (error != 0) {
    167 			printf("led: failed to create sysctl hw.led.%s: %d\n",
    168 			    led->name, error);
    169 			led_free(led);
    170 			led = NULL;
    171 		}
    172 	}
    173 	if (led != NULL)
    174 		TAILQ_INSERT_TAIL(&led_devices, led, devices);
    175 	mutex_exit(&led_lock);
    176 
    177 	return led;
    178 }
    179 
    180 void
    181 led_detach(void *handle)
    182 {
    183 	struct led_device *led = handle;
    184 
    185 	mutex_enter(&led_lock);
    186 
    187 	TAILQ_REMOVE(&led_devices, led, devices);
    188 	sysctl_teardown(&led->slog);
    189 	led_free(led);
    190 
    191 	mutex_exit(&led_lock);
    192 }
    193