cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

hid-sensor-trigger.c (9198B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/*
      3 * HID Sensors Driver
      4 * Copyright (c) 2012, Intel Corporation.
      5 */
      6#include <linux/device.h>
      7#include <linux/platform_device.h>
      8#include <linux/module.h>
      9#include <linux/delay.h>
     10#include <linux/hid-sensor-hub.h>
     11#include <linux/workqueue.h>
     12#include <linux/iio/iio.h>
     13#include <linux/iio/trigger.h>
     14#include <linux/iio/triggered_buffer.h>
     15#include <linux/iio/trigger_consumer.h>
     16#include <linux/iio/sysfs.h>
     17#include "hid-sensor-trigger.h"
     18
     19static ssize_t _hid_sensor_set_report_latency(struct device *dev,
     20					      struct device_attribute *attr,
     21					      const char *buf, size_t len)
     22{
     23	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
     24	struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
     25	int integer, fract, ret;
     26	int latency;
     27
     28	ret = iio_str_to_fixpoint(buf, 100000, &integer, &fract);
     29	if (ret)
     30		return ret;
     31
     32	latency = integer * 1000 + fract / 1000;
     33	ret = hid_sensor_set_report_latency(attrb, latency);
     34	if (ret < 0)
     35		return len;
     36
     37	attrb->latency_ms = hid_sensor_get_report_latency(attrb);
     38
     39	return len;
     40}
     41
     42static ssize_t _hid_sensor_get_report_latency(struct device *dev,
     43					      struct device_attribute *attr,
     44					      char *buf)
     45{
     46	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
     47	struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
     48	int latency;
     49
     50	latency = hid_sensor_get_report_latency(attrb);
     51	if (latency < 0)
     52		return latency;
     53
     54	return sprintf(buf, "%d.%06u\n", latency / 1000, (latency % 1000) * 1000);
     55}
     56
     57static ssize_t _hid_sensor_get_fifo_state(struct device *dev,
     58					  struct device_attribute *attr,
     59					  char *buf)
     60{
     61	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
     62	struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
     63	int latency;
     64
     65	latency = hid_sensor_get_report_latency(attrb);
     66	if (latency < 0)
     67		return latency;
     68
     69	return sprintf(buf, "%d\n", !!latency);
     70}
     71
     72static IIO_DEVICE_ATTR(hwfifo_timeout, 0644,
     73		       _hid_sensor_get_report_latency,
     74		       _hid_sensor_set_report_latency, 0);
     75static IIO_DEVICE_ATTR(hwfifo_enabled, 0444,
     76		       _hid_sensor_get_fifo_state, NULL, 0);
     77
     78static const struct attribute *hid_sensor_fifo_attributes[] = {
     79	&iio_dev_attr_hwfifo_timeout.dev_attr.attr,
     80	&iio_dev_attr_hwfifo_enabled.dev_attr.attr,
     81	NULL,
     82};
     83
     84static int _hid_sensor_power_state(struct hid_sensor_common *st, bool state)
     85{
     86	int state_val;
     87	int report_val;
     88	s32 poll_value = 0;
     89
     90	if (state) {
     91		if (sensor_hub_device_open(st->hsdev))
     92			return -EIO;
     93
     94		atomic_inc(&st->data_ready);
     95
     96		state_val = hid_sensor_get_usage_index(st->hsdev,
     97			st->power_state.report_id,
     98			st->power_state.index,
     99			HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM);
    100		report_val = hid_sensor_get_usage_index(st->hsdev,
    101			st->report_state.report_id,
    102			st->report_state.index,
    103			HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM);
    104
    105		poll_value = hid_sensor_read_poll_value(st);
    106	} else {
    107		int val;
    108
    109		val = atomic_dec_if_positive(&st->data_ready);
    110		if (val < 0)
    111			return 0;
    112
    113		sensor_hub_device_close(st->hsdev);
    114		state_val = hid_sensor_get_usage_index(st->hsdev,
    115			st->power_state.report_id,
    116			st->power_state.index,
    117			HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM);
    118		report_val = hid_sensor_get_usage_index(st->hsdev,
    119			st->report_state.report_id,
    120			st->report_state.index,
    121			HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM);
    122	}
    123
    124	if (state_val >= 0) {
    125		state_val += st->power_state.logical_minimum;
    126		sensor_hub_set_feature(st->hsdev, st->power_state.report_id,
    127				       st->power_state.index, sizeof(state_val),
    128				       &state_val);
    129	}
    130
    131	if (report_val >= 0) {
    132		report_val += st->report_state.logical_minimum;
    133		sensor_hub_set_feature(st->hsdev, st->report_state.report_id,
    134				       st->report_state.index,
    135				       sizeof(report_val),
    136				       &report_val);
    137	}
    138
    139	pr_debug("HID_SENSOR %s set power_state %d report_state %d\n",
    140		 st->pdev->name, state_val, report_val);
    141
    142	sensor_hub_get_feature(st->hsdev, st->power_state.report_id,
    143			       st->power_state.index,
    144			       sizeof(state_val), &state_val);
    145	if (state && poll_value)
    146		msleep_interruptible(poll_value * 2);
    147
    148	return 0;
    149}
    150EXPORT_SYMBOL_NS(hid_sensor_power_state, IIO_HID);
    151
    152int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
    153{
    154
    155#ifdef CONFIG_PM
    156	int ret;
    157
    158	if (atomic_add_unless(&st->runtime_pm_enable, 1, 1))
    159		pm_runtime_enable(&st->pdev->dev);
    160
    161	if (state) {
    162		atomic_inc(&st->user_requested_state);
    163		ret = pm_runtime_resume_and_get(&st->pdev->dev);
    164	} else {
    165		atomic_dec(&st->user_requested_state);
    166		pm_runtime_mark_last_busy(&st->pdev->dev);
    167		pm_runtime_use_autosuspend(&st->pdev->dev);
    168		ret = pm_runtime_put_autosuspend(&st->pdev->dev);
    169	}
    170	if (ret < 0)
    171		return ret;
    172
    173	return 0;
    174#else
    175	atomic_set(&st->user_requested_state, state);
    176	return _hid_sensor_power_state(st, state);
    177#endif
    178}
    179
    180static void hid_sensor_set_power_work(struct work_struct *work)
    181{
    182	struct hid_sensor_common *attrb = container_of(work,
    183						       struct hid_sensor_common,
    184						       work);
    185
    186	if (attrb->poll_interval >= 0)
    187		sensor_hub_set_feature(attrb->hsdev, attrb->poll.report_id,
    188				       attrb->poll.index,
    189				       sizeof(attrb->poll_interval),
    190				       &attrb->poll_interval);
    191
    192	if (attrb->raw_hystersis >= 0)
    193		sensor_hub_set_feature(attrb->hsdev,
    194				       attrb->sensitivity.report_id,
    195				       attrb->sensitivity.index,
    196				       sizeof(attrb->raw_hystersis),
    197				       &attrb->raw_hystersis);
    198
    199	if (attrb->latency_ms > 0)
    200		hid_sensor_set_report_latency(attrb, attrb->latency_ms);
    201
    202	if (atomic_read(&attrb->user_requested_state))
    203		_hid_sensor_power_state(attrb, true);
    204}
    205
    206static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
    207						bool state)
    208{
    209	return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state);
    210}
    211
    212void hid_sensor_remove_trigger(struct iio_dev *indio_dev,
    213			       struct hid_sensor_common *attrb)
    214{
    215	if (atomic_read(&attrb->runtime_pm_enable))
    216		pm_runtime_disable(&attrb->pdev->dev);
    217
    218	pm_runtime_set_suspended(&attrb->pdev->dev);
    219
    220	cancel_work_sync(&attrb->work);
    221	iio_trigger_unregister(attrb->trigger);
    222	iio_trigger_free(attrb->trigger);
    223	iio_triggered_buffer_cleanup(indio_dev);
    224}
    225EXPORT_SYMBOL_NS(hid_sensor_remove_trigger, IIO_HID);
    226
    227static const struct iio_trigger_ops hid_sensor_trigger_ops = {
    228	.set_trigger_state = &hid_sensor_data_rdy_trigger_set_state,
    229};
    230
    231int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
    232				struct hid_sensor_common *attrb)
    233{
    234	const struct attribute **fifo_attrs;
    235	int ret;
    236	struct iio_trigger *trig;
    237
    238	if (hid_sensor_batch_mode_supported(attrb))
    239		fifo_attrs = hid_sensor_fifo_attributes;
    240	else
    241		fifo_attrs = NULL;
    242
    243	ret = iio_triggered_buffer_setup_ext(indio_dev,
    244					     &iio_pollfunc_store_time, NULL,
    245					     IIO_BUFFER_DIRECTION_IN,
    246					     NULL, fifo_attrs);
    247	if (ret) {
    248		dev_err(&indio_dev->dev, "Triggered Buffer Setup Failed\n");
    249		return ret;
    250	}
    251
    252	trig = iio_trigger_alloc(indio_dev->dev.parent,
    253				 "%s-dev%d", name, iio_device_id(indio_dev));
    254	if (trig == NULL) {
    255		dev_err(&indio_dev->dev, "Trigger Allocate Failed\n");
    256		ret = -ENOMEM;
    257		goto error_triggered_buffer_cleanup;
    258	}
    259
    260	iio_trigger_set_drvdata(trig, attrb);
    261	trig->ops = &hid_sensor_trigger_ops;
    262	ret = iio_trigger_register(trig);
    263
    264	if (ret) {
    265		dev_err(&indio_dev->dev, "Trigger Register Failed\n");
    266		goto error_free_trig;
    267	}
    268	attrb->trigger = trig;
    269	indio_dev->trig = iio_trigger_get(trig);
    270
    271	ret = pm_runtime_set_active(&indio_dev->dev);
    272	if (ret)
    273		goto error_unreg_trigger;
    274
    275	iio_device_set_drvdata(indio_dev, attrb);
    276
    277	INIT_WORK(&attrb->work, hid_sensor_set_power_work);
    278
    279	pm_suspend_ignore_children(&attrb->pdev->dev, true);
    280	/* Default to 3 seconds, but can be changed from sysfs */
    281	pm_runtime_set_autosuspend_delay(&attrb->pdev->dev,
    282					 3000);
    283	return ret;
    284error_unreg_trigger:
    285	iio_trigger_unregister(trig);
    286error_free_trig:
    287	iio_trigger_free(trig);
    288error_triggered_buffer_cleanup:
    289	iio_triggered_buffer_cleanup(indio_dev);
    290	return ret;
    291}
    292EXPORT_SYMBOL_NS(hid_sensor_setup_trigger, IIO_HID);
    293
    294static int __maybe_unused hid_sensor_suspend(struct device *dev)
    295{
    296	struct iio_dev *indio_dev = dev_get_drvdata(dev);
    297	struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
    298
    299	return _hid_sensor_power_state(attrb, false);
    300}
    301
    302static int __maybe_unused hid_sensor_resume(struct device *dev)
    303{
    304	struct iio_dev *indio_dev = dev_get_drvdata(dev);
    305	struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
    306	schedule_work(&attrb->work);
    307	return 0;
    308}
    309
    310static int __maybe_unused hid_sensor_runtime_resume(struct device *dev)
    311{
    312	struct iio_dev *indio_dev = dev_get_drvdata(dev);
    313	struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
    314	return _hid_sensor_power_state(attrb, true);
    315}
    316
    317const struct dev_pm_ops hid_sensor_pm_ops = {
    318	SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend, hid_sensor_resume)
    319	SET_RUNTIME_PM_OPS(hid_sensor_suspend,
    320			   hid_sensor_runtime_resume, NULL)
    321};
    322EXPORT_SYMBOL_NS(hid_sensor_pm_ops, IIO_HID);
    323
    324MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
    325MODULE_DESCRIPTION("HID Sensor trigger processing");
    326MODULE_LICENSE("GPL");
    327MODULE_IMPORT_NS(IIO_HID_ATTRIBUTES);