/* Adjust channel real bits based on report descriptor */ staticvoid magn_3d_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 */ staticint magn_3d_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask)
{ struct magn_3d_state *magn_state = iio_priv(indio_dev); int report_id = -1;
u32 address; int ret_type;
s32 min;
/* Callback handler to send event after all samples are received and captured */ staticint magn_3d_proc_event(struct hid_sensor_hub_device *hsdev, unsigned usage_id, void *priv)
{ struct iio_dev *indio_dev = platform_get_drvdata(priv); struct magn_3d_state *magn_state = iio_priv(indio_dev);
dev_dbg(&indio_dev->dev, "magn_3d_proc_event\n"); if (atomic_read(&magn_state->magn_flux_attributes.data_ready)) { if (!magn_state->timestamp)
magn_state->timestamp = iio_get_time_ns(indio_dev);
/* Capture samples in local storage */ staticint magn_3d_capture_sample(struct hid_sensor_hub_device *hsdev, unsigned usage_id,
size_t raw_len, char *raw_data, void *priv)
{ struct iio_dev *indio_dev = platform_get_drvdata(priv); struct magn_3d_state *magn_state = iio_priv(indio_dev); int offset; int ret = 0;
u32 *iio_val = NULL;
switch (usage_id) { case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS: case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS: case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS:
offset = (usage_id - HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS)
+ CHANNEL_SCAN_INDEX_X; break; case HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH: case HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH: case HID_USAGE_SENSOR_ORIENT_MAGN_NORTH: case HID_USAGE_SENSOR_ORIENT_TRUE_NORTH:
offset = (usage_id - HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH)
+ CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP; break; case HID_USAGE_SENSOR_TIME_TIMESTAMP:
magn_state->timestamp =
hid_sensor_convert_timestamp(&magn_state->magn_flux_attributes,
*(s64 *)raw_data); return ret; default: return -EINVAL;
}
iio_val = magn_state->magn_val_addr[offset];
if (iio_val != NULL)
*iio_val = *((u32 *)raw_data); else
ret = -EINVAL;
return ret;
}
/* Parse report which is specific to an usage id*/ staticint magn_3d_parse_report(struct platform_device *pdev, struct hid_sensor_hub_device *hsdev, struct iio_chan_spec **channels, int *chan_count, unsigned usage_id, struct magn_3d_state *st)
{ int i; int attr_count = 0; struct iio_chan_spec *_channels;
/* Scan for each usage attribute supported */ for (i = 0; i < MAGN_3D_CHANNEL_MAX; i++) { int status;
u32 address = magn_3d_addresses[i];
/* Check if usage attribute exists in the sensor hub device */
status = sensor_hub_input_get_attribute_info(hsdev,
HID_INPUT_REPORT,
usage_id,
address,
&(st->magn[i])); if (!status)
attr_count++;
}
if (attr_count <= 0) {
dev_err(&pdev->dev, "failed to find any supported usage attributes in report\n"); return -EINVAL;
}
/* Setup IIO channel array */
_channels = devm_kcalloc(&pdev->dev, attr_count, sizeof(struct iio_chan_spec),
GFP_KERNEL); if (!_channels) {
dev_err(&pdev->dev, "failed to allocate space for iio channels\n"); return -ENOMEM;
}
/* attr_count include timestamp channel, and the iio_vals should be aligned to 8byte */
st->iio_vals = devm_kcalloc(&pdev->dev,
((attr_count + 1) % 2 + (attr_count + 1) / 2) * 2, sizeof(u32), GFP_KERNEL); if (!st->iio_vals) {
dev_err(&pdev->dev, "failed to allocate space for iio values array\n"); return -ENOMEM;
}
for (i = 0, *chan_count = 0;
i < MAGN_3D_CHANNEL_MAX && *chan_count < attr_count;
i++){ if (st->magn[i].index >= 0) { /* Setup IIO channel struct */
(_channels[*chan_count]) = magn_3d_channels[i];
(_channels[*chan_count]).scan_index = *chan_count;
(_channels[*chan_count]).address = i;
if (i != CHANNEL_SCAN_INDEX_TIMESTAMP) { /* Set magn_val_addr to iio value address */
st->magn_val_addr[i] = &st->iio_vals[*chan_count];
magn_3d_adjust_channel_bit_mask(_channels,
*chan_count,
st->magn[i].size);
}
(*chan_count)++;
}
}
if (*chan_count <= 0) {
dev_err(&pdev->dev, "failed to find any magnetic channels setup\n"); return -EINVAL;
}
ret = hid_sensor_parse_common_attributes(hsdev,
HID_USAGE_SENSOR_COMPASS_3D,
&magn_state->magn_flux_attributes,
magn_3d_sensitivity_addresses,
ARRAY_SIZE(magn_3d_sensitivity_addresses)); if (ret) {
dev_err(&pdev->dev, "failed to setup common attributes\n"); return ret;
}
magn_state->rot_attributes = magn_state->magn_flux_attributes; /* sensitivity of rot_attribute is not the same as magn_flux_attributes */
magn_state->rot_attributes.sensitivity.index = -1;
ret = magn_3d_parse_report(pdev, hsdev,
&channels, &chan_count,
HID_USAGE_SENSOR_COMPASS_3D, magn_state); if (ret) {
dev_err(&pdev->dev, "failed to parse report\n"); return ret;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.