// SPDX-License-Identifier: GPL-2.0-only
/*
* hid-sensor-custom.c
* Copyright (c) 2015, Intel Corporation.
*/
#include <linux/ctype.h>
#include <linux/dmi.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/kfifo.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/bsearch.h>
#include <linux/platform_device.h>
#include <linux/hid-sensor-hub.h>
#define HID_CUSTOM_NAME_LENGTH 64
#define HID_CUSTOM_MAX_CORE_ATTRS 10
#define HID_CUSTOM_TOTAL_ATTRS (HID_CUSTOM_MAX_CORE_ATTRS + 1)
#define HID_CUSTOM_FIFO_SIZE 4096
#define HID_CUSTOM_MAX_FEATURE_BYTES 64
#define HID_SENSOR_USAGE_LENGTH (4 + 1)
struct hid_sensor_custom_field {
int report_id;
char group_name[HID_CUSTOM_NAME_LENGTH];
struct hid_sensor_hub_attribute_info attribute;
struct device_attribute sd_attrs[HID_CUSTOM_MAX_CORE_ATTRS];
char attr_name[HID_CUSTOM_TOTAL_ATTRS][HID_CUSTOM_NAME_LENGTH];
struct attribute *attrs[HID_CUSTOM_TOTAL_ATTRS];
struct attribute_group hid_custom_attribute_group;
};
struct hid_sensor_custom {
struct mutex mutex;
struct platform_device *pdev;
struct hid_sensor_hub_device *hsdev;
struct hid_sensor_hub_callbacks callbacks;
int sensor_field_count;
struct hid_sensor_custom_field *fields;
int input_field_count;
int input_report_size;
int input_report_recd_size;
bool input_skip_sample;
bool enable;
struct hid_sensor_custom_field *power_state;
struct hid_sensor_custom_field *report_state;
struct miscdevice custom_dev;
struct kfifo data_fifo;
unsigned long misc_opened;
wait_queue_head_t wait;
struct platform_device *custom_pdev;
};
/* Header for each sample to user space via dev interface */
struct hid_sensor_sample {
u32 usage_id;
u64 timestamp;
u32 raw_len;
} __packed;
static struct attribute hid_custom_attrs[HID_CUSTOM_TOTAL_ATTRS] = {
{.name = "name", .mode = S_IRUGO},
{.name = "units", .mode = S_IRUGO},
{.name = "unit-expo", .mode = S_IRUGO},
{.name = "minimum", .mode = S_IRUGO},
{.name = "maximum", .mode = S_IRUGO},
{.name = "size", .mode = S_IRUGO},
{.name = "value", .mode = S_IWUSR | S_IRUGO},
{.name = NULL}
};
static const struct hid_custom_usage_desc {
int usage_id;
char *desc;
} hid_custom_usage_desc_table[] = {
{0x200201, "event-sensor-state"},
{0x200202, "event-sensor-event"},
{0x200301, "property-friendly-name"},
{0x200302, "property-persistent-unique-id"},
{0x200303, "property-sensor-status"},
{0x200304, "property-min-report-interval"},
{0x200305, "property-sensor-manufacturer"},
{0x200306, "property-sensor-model"},
{0x200307, "property-sensor-serial-number"},
{0x200308, "property-sensor-description"},
{0x200309, "property-sensor-connection-type"},
{0x20030A, "property-sensor-device-path"},
{0x20030B, "property-hardware-revision"},
{0x20030C, "property-firmware-version"},
{0x20030D, "property-release-date"},
{0x20030E, "property-report-interval"},
{0x20030F, "property-change-sensitivity-absolute"},
{0x200310, "property-change-sensitivity-percent-range"},
{0x200311, "property-change-sensitivity-percent-relative"},
{0x200312, "property-accuracy"},
{0x200313, "property-resolution"},
{0x200314, "property-maximum"},
{0x200315, "property-minimum"},
{0x200316, "property-reporting-state"},
{0x200317, "property-sampling-rate"},
{0x200318, "property-response-curve"},
{0x200319, "property-power-state"},
{0x200540, "data-field-custom"},
{0x200541, "data-field-custom-usage"},
{0x200542, "data-field-custom-boolean-array"},
{0x200543, "data-field-custom-value"},
{0x200544, "data-field-custom-value_1"},
{0x200545, "data-field-custom-value_2"},
{0x200546, "data-field-custom-value_3"},