summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/ksensor/ksensor_test.c
blob: a98a8b77eb15e0285264c0e3ffb007f03e832b95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2020 Oxide Computer Company
 */

/*
 * This driver is used to implement parts of the ksensor test suite.
 */

#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/modctl.h>
#include <sys/conf.h>
#include <sys/devops.h>
#include <sys/zone.h>
#include <sys/sensors.h>

typedef struct ksensor_test {
	dev_info_t *kt_dip;
	id_t kt_sensor1;
	id_t kt_sensor2;
	id_t kt_sensor3;
	id_t kt_sensor4;
	id_t kt_sensor5;
	id_t kt_volt;
	id_t kt_current;
} ksensor_test_t;

static int
ksensor_test_temp(void *arg, sensor_ioctl_scalar_t *scalar)
{
	scalar->sis_unit = SENSOR_UNIT_CELSIUS;
	scalar->sis_gran = 4;
	scalar->sis_prec = -2;
	scalar->sis_value = 23;
	return (0);
}

static const ksensor_ops_t ksensor_test_temp_ops = {
	.kso_kind = ksensor_kind_temperature,
	.kso_scalar = ksensor_test_temp
};

static int
ksensor_test_volt(void *arg, sensor_ioctl_scalar_t *scalar)
{
	scalar->sis_unit = SENSOR_UNIT_VOLTS;
	scalar->sis_gran = 1000;
	scalar->sis_prec = 0;
	scalar->sis_value = 3300;
	return (0);
}

static const ksensor_ops_t ksensor_test_volt_ops = {
	.kso_kind = ksensor_kind_voltage,
	.kso_scalar = ksensor_test_volt
};

static int
ksensor_test_current(void *arg, sensor_ioctl_scalar_t *scalar)
{
	scalar->sis_unit = SENSOR_UNIT_AMPS;
	scalar->sis_gran = 10;
	scalar->sis_prec = 0;
	scalar->sis_value = 5;
	return (0);
}

static const ksensor_ops_t ksensor_test_current_ops = {
	.kso_kind = ksensor_kind_current,
	.kso_scalar = ksensor_test_current
};

static int
ksensor_test_kind_eio(void *arg, sensor_ioctl_kind_t *kindp)
{
	return (EIO);
}

static int
ksensor_test_temp_eio(void *arg, sensor_ioctl_scalar_t *scalar)
{
	return (EIO);
}

static const ksensor_ops_t ksensor_test_eio_ops = {
	.kso_kind = ksensor_test_kind_eio,
	.kso_scalar = ksensor_test_temp_eio
};

static int
ksensor_test_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
	int ret;
	char buf[128];
	ksensor_test_t *kt;

	switch (cmd) {
	case DDI_RESUME:
		return (DDI_SUCCESS);
	case DDI_ATTACH:
		break;
	default:
		return (DDI_FAILURE);
	}

	kt = kmem_zalloc(sizeof (ksensor_test_t), KM_SLEEP);
	kt->kt_dip = dip;

	(void) snprintf(buf, sizeof (buf), "test.temp.%d.1",
	    ddi_get_instance(dip));
	if ((ret = ksensor_create(dip, &ksensor_test_temp_ops, NULL, buf,
	    "ddi_sensor:test", &kt->kt_sensor1)) != 0) {
		dev_err(dip, CE_WARN, "failed to attatch sensor %s: %d", buf,
		    ret);
		goto err;
	}

	(void) snprintf(buf, sizeof (buf), "test.temp.%d.2",
	    ddi_get_instance(dip));
	if ((ret = ksensor_create(dip, &ksensor_test_temp_ops, NULL, buf,
	    "ddi_sensor:test", &kt->kt_sensor2)) != 0) {
		dev_err(dip, CE_WARN, "failed to attatch sensor %s: %d", buf,
		    ret);
		goto err;
	}

	(void) snprintf(buf, sizeof (buf), "test.temp.%d.3",
	    ddi_get_instance(dip));
	if ((ret = ksensor_create(dip, &ksensor_test_temp_ops, NULL, buf,
	    "ddi_sensor:test", &kt->kt_sensor3)) != 0) {
		dev_err(dip, CE_WARN, "failed to attach sensor %s: %d", buf,
		    ret);
		goto err;
	}

	(void) snprintf(buf, sizeof (buf), "test.temp.%d.4",
	    ddi_get_instance(dip));
	if ((ret = ksensor_create(dip, &ksensor_test_temp_ops, NULL, buf,
	    "ddi_sensor:test", &kt->kt_sensor4)) != 0) {
		dev_err(dip, CE_WARN, "failed to attach sensor %s: %d", buf,
		    ret);
		goto err;
	}

	(void) snprintf(buf, sizeof (buf), "test.eio.%d",
	    ddi_get_instance(dip));
	if ((ret = ksensor_create(dip, &ksensor_test_eio_ops, NULL, buf,
	    "ddi_sensor:test", &kt->kt_sensor5)) != 0) {
		dev_err(dip, CE_WARN, "failed to attach sensor %s: %d", buf,
		    ret);
		goto err;
	}

	(void) snprintf(buf, sizeof (buf), "test.volt.%d.1",
	    ddi_get_instance(dip));
	if ((ret = ksensor_create(dip, &ksensor_test_volt_ops, NULL, buf,
	    "ddi_sensor:test", &kt->kt_volt)) != 0) {
		dev_err(dip, CE_WARN, "failed to attach sensor %s: %d", buf,
		    ret);
		goto err;
	}

	(void) snprintf(buf, sizeof (buf), "test.current.%d.1",
	    ddi_get_instance(dip));
	if ((ret = ksensor_create(dip, &ksensor_test_current_ops, NULL, buf,
	    "ddi_sensor:test", &kt->kt_current)) != 0) {
		dev_err(dip, CE_WARN, "failed to attach sensor %s: %d", buf,
		    ret);
		goto err;
	}

	ddi_set_driver_private(dip, kt);

	return (DDI_SUCCESS);
err:
	(void) ksensor_remove(dip, KSENSOR_ALL_IDS);
	kmem_free(kt, sizeof (ksensor_test_t));
	return (DDI_FAILURE);
}

static int
ksensor_test_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	ksensor_test_t *kt;

	switch (cmd) {
	case DDI_DETACH:
		break;
	case DDI_SUSPEND:
		return (DDI_SUCCESS);
	default:
		return (DDI_FAILURE);
	}

	kt = ddi_get_driver_private(dip);
	if (kt == NULL) {
		dev_err(dip, CE_WARN, "failed to find ksensor_test_t");
		return (DDI_FAILURE);
	}

	if (kt->kt_sensor3 != 0 &&
	    ksensor_remove(dip, kt->kt_sensor3) != 0) {
		dev_err(dip, CE_WARN, "failed to remove sensor 3");
		return (DDI_FAILURE);
	}
	kt->kt_sensor3 = 0;
	if (ksensor_remove(dip, KSENSOR_ALL_IDS) != 0) {
		dev_err(dip, CE_WARN, "failed to remove sensors");
		return (DDI_FAILURE);
	}
	kmem_free(kt, sizeof (*kt));
	ddi_set_driver_private(dip, NULL);
	return (DDI_SUCCESS);
}

static struct dev_ops ksensor_test_dev_ops = {
	.devo_rev = DEVO_REV,
	.devo_refcnt = 0,
	.devo_getinfo = nodev,
	.devo_identify = nulldev,
	.devo_probe = nulldev,
	.devo_attach = ksensor_test_attach,
	.devo_detach = ksensor_test_detach,
	.devo_reset = nodev,
	.devo_power = ddi_power,
	.devo_quiesce = ddi_quiesce_not_needed,
};

static struct modldrv ksensor_test_modldrv = {
	.drv_modops = &mod_driverops,
	.drv_linkinfo = "Kernel Sensor test driver",
	.drv_dev_ops = &ksensor_test_dev_ops
};

static struct modlinkage ksensor_test_modlinkage = {
	.ml_rev = MODREV_1,
	.ml_linkage = { &ksensor_test_modldrv, NULL }
};

int
_init(void)
{
	return (mod_install(&ksensor_test_modlinkage));
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&ksensor_test_modlinkage, modinfop));
}

int
_fini(void)
{
	return (mod_remove(&ksensor_test_modlinkage));
}