Home | History | Annotate | Line # | Download | only in sysmon
swsensor.c revision 1.3.2.2
      1 /*	$NetBSD: swsensor.c,v 1.3.2.2 2010/10/22 07:22:17 uebayasi Exp $ */
      2 /*
      3  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     16  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     22  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     24  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: swsensor.c,v 1.3.2.2 2010/10/22 07:22:17 uebayasi Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/kernel.h>
     34 #include <sys/module.h>
     35 #include <sys/sysctl.h>
     36 
     37 #include <dev/sysmon/sysmonvar.h>
     38 
     39 #include <prop/proplib.h>
     40 
     41 int swsensorattach(int);
     42 
     43 static struct sysctllog *swsensor_sysctllog = NULL;
     44 
     45 static int sensor_value_sysctl = 0;
     46 
     47 static struct sysmon_envsys *swsensor_sme;
     48 static envsys_data_t swsensor_edata;
     49 
     50 static int32_t sw_sensor_value;
     51 
     52 MODULE(MODULE_CLASS_DRIVER, swsensor, NULL);
     53 
     54 /*
     55  * Set-up the sysctl interface for setting the sensor's cur_value
     56  */
     57 
     58 static
     59 void
     60 sysctl_swsensor_setup(void)
     61 {
     62 	int ret;
     63 	int node_sysctl_num;
     64 	const struct sysctlnode *me = NULL;
     65 
     66 	KASSERT(swsensor_sysctllog == NULL);
     67 
     68 	ret = sysctl_createv(&swsensor_sysctllog, 0, NULL, &me,
     69 			     CTLFLAG_READWRITE,
     70 			     CTLTYPE_NODE, "swsensor", NULL,
     71 			     NULL, 0, NULL, 0,
     72 			     CTL_HW, CTL_CREATE, CTL_EOL);
     73 	if (ret != 0)
     74 		return;
     75 
     76 	node_sysctl_num = me->sysctl_num;
     77 	ret = sysctl_createv(&swsensor_sysctllog, 0, NULL, &me,
     78 			     CTLFLAG_READWRITE,
     79 			     CTLTYPE_INT, "cur_value", NULL,
     80 			     NULL, 0, &sw_sensor_value, 0,
     81 			     CTL_HW, node_sysctl_num, CTL_CREATE, CTL_EOL);
     82 
     83 	if (ret == 0)
     84 		sensor_value_sysctl = me->sysctl_num;
     85 }
     86 
     87 /*
     88  * "Polling" routine to update sensor value
     89  */
     90 static
     91 void
     92 swsensor_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
     93 {
     94 
     95 	edata->value_cur = sw_sensor_value;
     96 	edata->state = ENVSYS_SVALID;
     97 }
     98 
     99 /*
    100  * Module management
    101  */
    102 
    103 static
    104 int
    105 swsensor_init(void *arg)
    106 {
    107 	int error;
    108 	prop_dictionary_t pd = (prop_dictionary_t)arg;
    109 	prop_object_t po = NULL;
    110 	int pv;
    111 
    112 	swsensor_sme = sysmon_envsys_create();
    113 	if (swsensor_sme == NULL)
    114 		return ENOTTY;
    115 
    116 	swsensor_sme->sme_name = "swsensor";
    117 	swsensor_sme->sme_cookie = &swsensor_edata;
    118 	swsensor_sme->sme_refresh = swsensor_refresh;
    119 	swsensor_sme->sme_set_limits = NULL;
    120 	swsensor_sme->sme_get_limits = NULL;
    121 
    122 	/* See if prop dictionary supplies a sensor type */
    123 	if (pd != NULL)
    124 		po = prop_dictionary_get(pd, "type");
    125 
    126 	pv = -1;
    127 	if (po != NULL && prop_object_type(po) == PROP_TYPE_NUMBER)
    128 		swsensor_edata.units = prop_number_integer_value(po);
    129 	else
    130 		swsensor_edata.units = ENVSYS_INTEGER;
    131 
    132 	/* See if prop dictionary supplies sensor flags */
    133 	if (pd != NULL)
    134 		po = prop_dictionary_get(pd, "flags");
    135 
    136 	pv = -1;
    137 	if (po != NULL && prop_object_type(po) == PROP_TYPE_NUMBER)
    138 		swsensor_edata.flags = prop_number_integer_value(po);
    139 	else
    140 		swsensor_edata.flags = 0;
    141 
    142 	swsensor_edata.value_cur = 0;
    143 	strlcpy(swsensor_edata.desc, "sensor", ENVSYS_DESCLEN);
    144 
    145 	error = sysmon_envsys_sensor_attach(swsensor_sme, &swsensor_edata);
    146 
    147 	if (error == 0)
    148 		error = sysmon_envsys_register(swsensor_sme);
    149 	else {
    150 		printf("sysmon_envsys_sensor_attach failed: %d\n", error);
    151 		return error;
    152 	}
    153 
    154 	if (error == 0)
    155 		sysctl_swsensor_setup();
    156 	else
    157 		printf("sysmon_envsys_register failed: %d\n", error);
    158 
    159 	return error;
    160 }
    161 
    162 static
    163 int
    164 swsensor_fini(void *arg)
    165 {
    166 
    167 	sysmon_envsys_unregister(swsensor_sme);
    168 
    169 	sysctl_teardown(&swsensor_sysctllog);
    170 
    171 	return 0;
    172 }
    173 
    174 static
    175 int
    176 swsensor_modcmd(modcmd_t cmd, void *arg)
    177 {
    178 	int ret;
    179 
    180 	switch (cmd) {
    181 	case MODULE_CMD_INIT:
    182 		ret = swsensor_init(arg);
    183 		break;
    184 
    185 	case MODULE_CMD_FINI:
    186 		ret = swsensor_fini(arg);
    187 		break;
    188 
    189 	case MODULE_CMD_STAT:
    190 	default:
    191 		ret = ENOTTY;
    192 	}
    193 
    194 	return ret;
    195 }
    196