drivers/iio/light/hid-sensor-prox.c

Source file repositories/reference/linux-study-clean/drivers/iio/light/hid-sensor-prox.c

File Facts

System
Linux kernel
Corpus path
drivers/iio/light/hid-sensor-prox.c
Extension
.c
Size
10697 bytes
Lines
387
Domain
Driver Families
Bucket
drivers/iio
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

struct prox_state {
	struct hid_sensor_hub_callbacks callbacks;
	struct hid_sensor_common common_attributes;
	struct hid_sensor_hub_attribute_info prox_attr[MAX_CHANNELS];
	struct iio_chan_spec channels[MAX_CHANNELS];
	u32 channel2usage[MAX_CHANNELS];
	u32 human_presence[MAX_CHANNELS];
	int scale_pre_decml[MAX_CHANNELS];
	int scale_post_decml[MAX_CHANNELS];
	int scale_precision[MAX_CHANNELS];
	unsigned long scan_mask[2]; /* One entry plus one terminator. */
	int num_channels;
};

static const u32 prox_sensitivity_addresses[] = {
	HID_USAGE_SENSOR_HUMAN_PRESENCE,
	HID_USAGE_SENSOR_DATA_PRESENCE,
};

#define PROX_CHANNEL(_is_proximity, _channel) \
	{\
		.type = _is_proximity ? IIO_PROXIMITY : IIO_ATTENTION,\
		.info_mask_separate = \
		(_is_proximity ? BIT(IIO_CHAN_INFO_RAW) :\
				BIT(IIO_CHAN_INFO_PROCESSED)) |\
		BIT(IIO_CHAN_INFO_OFFSET) |\
		BIT(IIO_CHAN_INFO_SCALE) |\
		BIT(IIO_CHAN_INFO_SAMP_FREQ) |\
		BIT(IIO_CHAN_INFO_HYSTERESIS),\
		.indexed = _is_proximity,\
		.channel = _channel,\
	}

/* Channel definitions (same order as prox_usage_ids) */
static const struct iio_chan_spec prox_channels[] = {
	PROX_CHANNEL(true, HID_HUMAN_PRESENCE),
	PROX_CHANNEL(true, HID_HUMAN_PROXIMITY),
	PROX_CHANNEL(false, 0),
};

/* Adjust channel real bits based on report descriptor */
static void prox_adjust_channel_bit_mask(struct iio_chan_spec *channels,
					int channel, int size)
{
	channels[channel].scan_type.sign = 's';
	/* Real storage bits will change based on the report desc. */
	channels[channel].scan_type.realbits = size * 8;
	/* Maximum size of a sample to capture is u32 */
	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
}

/* Channel read_raw handler */
static int prox_read_raw(struct iio_dev *indio_dev,
			      struct iio_chan_spec const *chan,
			      int *val, int *val2,
			      long mask)
{
	struct prox_state *prox_state = iio_priv(indio_dev);
	struct hid_sensor_hub_device *hsdev;
	int report_id;
	u32 address;
	int ret_type;
	s32 min;

	*val = 0;
	*val2 = 0;
	switch (mask) {
	case IIO_CHAN_INFO_RAW:
	case IIO_CHAN_INFO_PROCESSED:
		if (chan->scan_index >= prox_state->num_channels)
			return -EINVAL;
		address = prox_state->channel2usage[chan->scan_index];
		report_id = prox_state->prox_attr[chan->scan_index].report_id;
		hsdev = prox_state->common_attributes.hsdev;
		min = prox_state->prox_attr[chan->scan_index].logical_minimum;
		hid_sensor_power_state(&prox_state->common_attributes, true);
		*val = sensor_hub_input_attr_get_raw_value(hsdev,
							   hsdev->usage,
							   address,
							   report_id,
							   SENSOR_HUB_SYNC,
							   min < 0);
		if (prox_state->channel2usage[chan->scan_index] ==
		    HID_USAGE_SENSOR_HUMAN_ATTENTION)
			*val *= 100;
		hid_sensor_power_state(&prox_state->common_attributes, false);
		ret_type = IIO_VAL_INT;
		break;
	case IIO_CHAN_INFO_SCALE:
		if (chan->scan_index >= prox_state->num_channels)

Annotation

Implementation Notes