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