drivers/iio/orientation/hid-sensor-rotation.c

Source file repositories/reference/linux-study-clean/drivers/iio/orientation/hid-sensor-rotation.c

File Facts

System
Linux kernel
Corpus path
drivers/iio/orientation/hid-sensor-rotation.c
Extension
.c
Size
10617 bytes
Lines
389
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 dev_rot_state {
	struct hid_sensor_hub_callbacks callbacks;
	struct hid_sensor_common common_attributes;
	struct hid_sensor_hub_attribute_info quaternion;
	struct {
		IIO_DECLARE_QUATERNION(s32, sampled_vals);
		/*
		 * ABI regression avoidance: There are two copies of the same
		 * timestamp in case of userspace depending on broken alignment
		 * from older kernels.
		 */
		aligned_s64 timestamp[2];
	} scan;
	int scale_pre_decml;
	int scale_post_decml;
	int scale_precision;
	int value_offset;
	s64 timestamp;
};

static const u32 rotation_sensitivity_addresses[] = {
	HID_USAGE_SENSOR_DATA_ORIENTATION,
	HID_USAGE_SENSOR_ORIENT_QUATERNION,
};

/* Channel definitions */
static const struct iio_chan_spec dev_rot_channels[] = {
	{
		.type = IIO_ROT,
		.modified = 1,
		.channel2 = IIO_MOD_QUATERNION,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
					BIT(IIO_CHAN_INFO_OFFSET) |
					BIT(IIO_CHAN_INFO_SCALE) |
					BIT(IIO_CHAN_INFO_HYSTERESIS),
		.scan_index = 0
	},
	IIO_CHAN_SOFT_TIMESTAMP(1)
};

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

/* Channel read_raw handler */
static int dev_rot_read_raw(struct iio_dev *indio_dev,
				struct iio_chan_spec const *chan,
				int size, int *vals, int *val_len,
				long mask)
{
	struct dev_rot_state *rot_state = iio_priv(indio_dev);
	int ret_type;
	int i;

	vals[0] = 0;
	vals[1] = 0;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		if (size >= 4) {
			for (i = 0; i < 4; ++i)
				vals[i] = rot_state->scan.sampled_vals[i];
			ret_type = IIO_VAL_INT_MULTIPLE;
			*val_len =  4;
		} else
			ret_type = -EINVAL;
		break;
	case IIO_CHAN_INFO_SCALE:
		vals[0] = rot_state->scale_pre_decml;
		vals[1] = rot_state->scale_post_decml;
		return rot_state->scale_precision;

	case IIO_CHAN_INFO_OFFSET:
		*vals = rot_state->value_offset;
		return IIO_VAL_INT;

	case IIO_CHAN_INFO_SAMP_FREQ:
		ret_type = hid_sensor_read_samp_freq_value(
			&rot_state->common_attributes, &vals[0], &vals[1]);
		break;
	case IIO_CHAN_INFO_HYSTERESIS:

Annotation

Implementation Notes