1f2dbfd3Robert Mustacchi/*
2f2dbfd3Robert Mustacchi * This file and its contents are supplied under the terms of the
3f2dbfd3Robert Mustacchi * Common Development and Distribution License ("CDDL"), version 1.0.
4f2dbfd3Robert Mustacchi * You may only use this file in accordance with the terms of version
5f2dbfd3Robert Mustacchi * 1.0 of the CDDL.
6f2dbfd3Robert Mustacchi *
7f2dbfd3Robert Mustacchi * A full copy of the text of the CDDL should have accompanied this
8f2dbfd3Robert Mustacchi * source.  A copy of the CDDL is also available via the Internet at
9f2dbfd3Robert Mustacchi * http://www.illumos.org/license/CDDL.
10f2dbfd3Robert Mustacchi */
11f2dbfd3Robert Mustacchi
12f2dbfd3Robert Mustacchi/*
13f2dbfd3Robert Mustacchi * Copyright 2019, Joyent, Inc.
143ce5372Robert Mustacchi * Copyright 2020 Oxide Computer Company
15f2dbfd3Robert Mustacchi */
16f2dbfd3Robert Mustacchi
17f2dbfd3Robert Mustacchi#ifndef _SYS_SENSORS_H
18f2dbfd3Robert Mustacchi#define	_SYS_SENSORS_H
19f2dbfd3Robert Mustacchi
20f2dbfd3Robert Mustacchi/*
21f2dbfd3Robert Mustacchi * Consolidated sensor ioctls for various parts of the operating system. These
22f2dbfd3Robert Mustacchi * interfaces should not be relied on at all. They are evolving and will change
23f2dbfd3Robert Mustacchi * as we add more to the system for this. This may eventually become a larger
24f2dbfd3Robert Mustacchi * framework, though it's more likely we'll consolidate that in userland.
25f2dbfd3Robert Mustacchi */
26f2dbfd3Robert Mustacchi
27f2dbfd3Robert Mustacchi#ifdef __cplusplus
28f2dbfd3Robert Mustacchiextern "C" {
29f2dbfd3Robert Mustacchi#endif
30f2dbfd3Robert Mustacchi
31f2dbfd3Robert Mustacchi/*
32f2dbfd3Robert Mustacchi * List of different possible kinds of sensors.
33f2dbfd3Robert Mustacchi */
34f2dbfd3Robert Mustacchi#define	SENSOR_KIND_UNKNOWN		0x00
35f2dbfd3Robert Mustacchi#define	SENSOR_KIND_TEMPERATURE		0x01
361045e13Robert Mustacchi#define	SENSOR_KIND_VOLTAGE		0x02
371045e13Robert Mustacchi#define	SENSOR_KIND_CURRENT		0x03
38f2dbfd3Robert Mustacchi
39f2dbfd3Robert Mustacchi/*
40f2dbfd3Robert Mustacchi * Lists of units that senors may have.
41f2dbfd3Robert Mustacchi */
42f2dbfd3Robert Mustacchi#define	SENSOR_UNIT_UNKNOWN		0x00
43f2dbfd3Robert Mustacchi#define	SENSOR_UNIT_CELSIUS		0x01
44f2dbfd3Robert Mustacchi#define	SENSOR_UNIT_FAHRENHEIT		0x02
45f2dbfd3Robert Mustacchi#define	SENSOR_UNIT_KELVIN		0x03
461045e13Robert Mustacchi#define	SENSOR_UNIT_VOLTS		0x04
471045e13Robert Mustacchi#define	SENSOR_UNIT_AMPS		0x05
48f2dbfd3Robert Mustacchi
49f2dbfd3Robert Mustacchi#define	SENSOR_IOCTL	(('s' << 24) | ('e' << 16) | ('n' << 8))
50f2dbfd3Robert Mustacchi
51f2dbfd3Robert Mustacchi/*
52f2dbfd3Robert Mustacchi * Ask the sensor what kind of sensor it is.
53f2dbfd3Robert Mustacchi */
541045e13Robert Mustacchi#define	SENSOR_IOCTL_KIND	(SENSOR_IOCTL | 0x01)
55f2dbfd3Robert Mustacchi
56f2dbfd3Robert Mustacchitypedef struct sensor_ioctl_kind {
57f2dbfd3Robert Mustacchi	uint64_t	sik_kind;
58f2dbfd3Robert Mustacchi} sensor_ioctl_kind_t;
59f2dbfd3Robert Mustacchi
60f2dbfd3Robert Mustacchi/*
611045e13Robert Mustacchi * Ask the sensor for a scalar measurement. The sensor is responsible for
621045e13Robert Mustacchi * returning the units it's in.  A scalar measurement is broken down into a
63f2dbfd3Robert Mustacchi * signed value and a notion of its granularity. The sit_gran member indicates
641045e13Robert Mustacchi * the granularity: the number of increments per unit in the measurement (the
651045e13Robert Mustacchi * sit_value member). sit_gran is signed and the sign indicates whether one
661045e13Robert Mustacchi * needs to multiply or divide the granularity. The sit_prec member describes a
671045e13Robert Mustacchi * +/- value (taking sit_gran into account) that describes the precision of the
681045e13Robert Mustacchi * sensor.
691045e13Robert Mustacchi *
701045e13Robert Mustacchi * For example, consider a temperature sensor that set sit_gran to 10. This
711045e13Robert Mustacchi * would mean that the value in sit_value was in 10ths of a degree and that to
721045e13Robert Mustacchi * get the actual value in degrees, one would divide by 10. On the other hand, a
731045e13Robert Mustacchi * negative value means that we effectively have to multiply to get there. For
741045e13Robert Mustacchi * example, a value of -2 would indicate that each value in sit_value indicated
751045e13Robert Mustacchi * two degrees and to get the temperature in degrees you would multiply
761045e13Robert Mustacchi * sit_value * by two.
77f2dbfd3Robert Mustacchi */
781045e13Robert Mustacchi#define	SENSOR_IOCTL_SCALAR	(SENSOR_IOCTL | 0x02)
79f2dbfd3Robert Mustacchi
801045e13Robert Mustacchitypedef struct sensor_ioctl_scalar {
811045e13Robert Mustacchi	uint32_t	sis_unit;
821045e13Robert Mustacchi	int32_t		sis_gran;
831045e13Robert Mustacchi	uint32_t	sis_prec;
841045e13Robert Mustacchi	uint32_t	sis_pad;
851045e13Robert Mustacchi	int64_t		sis_value;
861045e13Robert Mustacchi} sensor_ioctl_scalar_t;
87f2dbfd3Robert Mustacchi
883ce5372Robert Mustacchi#ifdef	_KERNEL
893ce5372Robert Mustacchitypedef int (*ksensor_kind_f)(void *, sensor_ioctl_kind_t *);
901045e13Robert Mustacchitypedef int (*ksensor_scalar_f)(void *, sensor_ioctl_scalar_t *);
913ce5372Robert Mustacchi
923ce5372Robert Mustacchitypedef struct {
931045e13Robert Mustacchi	ksensor_kind_f		kso_kind;
941045e13Robert Mustacchi	ksensor_scalar_f	kso_scalar;
953ce5372Robert Mustacchi} ksensor_ops_t;
963ce5372Robert Mustacchi
973ce5372Robert Mustacchiextern int ksensor_kind_temperature(void *, sensor_ioctl_kind_t *);
981045e13Robert Mustacchiextern int ksensor_kind_voltage(void *, sensor_ioctl_kind_t *);
991045e13Robert Mustacchiextern int ksensor_kind_current(void *, sensor_ioctl_kind_t *);
1003ce5372Robert Mustacchi
1013ce5372Robert Mustacchi/*
1023ce5372Robert Mustacchi * Create a sensor where the class and name is supplied.
1033ce5372Robert Mustacchi */
1043ce5372Robert Mustacchiextern int ksensor_create(dev_info_t *, const ksensor_ops_t *, void *,
1053ce5372Robert Mustacchi    const char *, const char *, id_t *);
1063ce5372Robert Mustacchi
1073ce5372Robert Mustacchi/*
1081045e13Robert Mustacchi * Create a scalar sensor for a PCI device. If this is not a device-wide
1093ce5372Robert Mustacchi * (e.g. per-function) sensor, this should not be used.
1103ce5372Robert Mustacchi */
1111045e13Robert Mustacchiextern int ksensor_create_scalar_pcidev(dev_info_t *, uint_t,
1121045e13Robert Mustacchi    const ksensor_ops_t *, void *, const char *, id_t *);
1133ce5372Robert Mustacchi
1143ce5372Robert Mustacchi/*
1153ce5372Robert Mustacchi * Remove a named or all sensors from this driver.
1163ce5372Robert Mustacchi */
1173ce5372Robert Mustacchi#define	KSENSOR_ALL_IDS	INT_MIN
1183ce5372Robert Mustacchiextern int ksensor_remove(dev_info_t *, id_t);
1193ce5372Robert Mustacchi
1203ce5372Robert Mustacchi#endif
1213ce5372Robert Mustacchi
122f2dbfd3Robert Mustacchi#ifdef __cplusplus
123f2dbfd3Robert Mustacchi}
124f2dbfd3Robert Mustacchi#endif
125f2dbfd3Robert Mustacchi
126f2dbfd3Robert Mustacchi#endif /* _SYS_SENSORS_H */
127