^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * hid-sensor-custom.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2015, Intel Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kfifo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/bsearch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/hid-sensor-hub.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define HID_CUSTOM_NAME_LENGTH 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define HID_CUSTOM_MAX_CORE_ATTRS 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define HID_CUSTOM_TOTAL_ATTRS (HID_CUSTOM_MAX_CORE_ATTRS + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define HID_CUSTOM_FIFO_SIZE 4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define HID_CUSTOM_MAX_FEATURE_BYTES 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct hid_sensor_custom_field {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int report_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) char group_name[HID_CUSTOM_NAME_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct hid_sensor_hub_attribute_info attribute;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct device_attribute sd_attrs[HID_CUSTOM_MAX_CORE_ATTRS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) char attr_name[HID_CUSTOM_TOTAL_ATTRS][HID_CUSTOM_NAME_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct attribute *attrs[HID_CUSTOM_TOTAL_ATTRS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct attribute_group hid_custom_attribute_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct hid_sensor_custom {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct hid_sensor_hub_device *hsdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct hid_sensor_hub_callbacks callbacks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int sensor_field_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct hid_sensor_custom_field *fields;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int input_field_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int input_report_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int input_report_recd_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) bool input_skip_sample;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) bool enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct hid_sensor_custom_field *power_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct hid_sensor_custom_field *report_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct miscdevice custom_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct kfifo data_fifo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned long misc_opened;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /* Header for each sample to user space via dev interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct hid_sensor_sample {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) u32 usage_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) u64 timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) u32 raw_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static struct attribute hid_custom_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {.name = "name", .mode = S_IRUGO},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {.name = "units", .mode = S_IRUGO},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {.name = "unit-expo", .mode = S_IRUGO},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {.name = "minimum", .mode = S_IRUGO},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {.name = "maximum", .mode = S_IRUGO},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {.name = "size", .mode = S_IRUGO},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {.name = "value", .mode = S_IWUSR | S_IRUGO},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {.name = NULL}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static const struct hid_custom_usage_desc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int usage_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) char *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) } hid_custom_usage_desc_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {0x200201, "event-sensor-state"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {0x200202, "event-sensor-event"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {0x200301, "property-friendly-name"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {0x200302, "property-persistent-unique-id"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {0x200303, "property-sensor-status"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {0x200304, "property-min-report-interval"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {0x200305, "property-sensor-manufacturer"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {0x200306, "property-sensor-model"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {0x200307, "property-sensor-serial-number"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {0x200308, "property-sensor-description"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {0x200309, "property-sensor-connection-type"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {0x20030A, "property-sensor-device-path"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {0x20030B, "property-hardware-revision"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {0x20030C, "property-firmware-version"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {0x20030D, "property-release-date"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {0x20030E, "property-report-interval"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {0x20030F, "property-change-sensitivity-absolute"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {0x200310, "property-change-sensitivity-percent-range"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {0x200311, "property-change-sensitivity-percent-relative"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {0x200312, "property-accuracy"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {0x200313, "property-resolution"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {0x200314, "property-maximum"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {0x200315, "property-minimum"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {0x200316, "property-reporting-state"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {0x200317, "property-sampling-rate"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {0x200318, "property-response-curve"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {0x200319, "property-power-state"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {0x200540, "data-field-custom"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {0x200541, "data-field-custom-usage"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {0x200542, "data-field-custom-boolean-array"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {0x200543, "data-field-custom-value"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {0x200544, "data-field-custom-value_1"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {0x200545, "data-field-custom-value_2"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {0x200546, "data-field-custom-value_3"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {0x200547, "data-field-custom-value_4"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {0x200548, "data-field-custom-value_5"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {0x200549, "data-field-custom-value_6"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {0x20054A, "data-field-custom-value_7"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {0x20054B, "data-field-custom-value_8"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {0x20054C, "data-field-custom-value_9"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {0x20054D, "data-field-custom-value_10"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {0x20054E, "data-field-custom-value_11"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {0x20054F, "data-field-custom-value_12"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {0x200550, "data-field-custom-value_13"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {0x200551, "data-field-custom-value_14"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {0x200552, "data-field-custom-value_15"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {0x200553, "data-field-custom-value_16"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {0x200554, "data-field-custom-value_17"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {0x200555, "data-field-custom-value_18"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {0x200556, "data-field-custom-value_19"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {0x200557, "data-field-custom-value_20"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {0x200558, "data-field-custom-value_21"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {0x200559, "data-field-custom-value_22"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {0x20055A, "data-field-custom-value_23"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {0x20055B, "data-field-custom-value_24"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {0x20055C, "data-field-custom-value_25"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {0x20055D, "data-field-custom-value_26"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {0x20055E, "data-field-custom-value_27"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {0x20055F, "data-field-custom-value_28"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int usage_id_cmp(const void *p1, const void *p2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (*(int *)p1 < *(int *)p2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (*(int *)p1 > *(int *)p2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static ssize_t enable_sensor_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return sprintf(buf, "%d\n", sensor_inst->enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int set_power_report_state(struct hid_sensor_custom *sensor_inst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) bool state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int power_val = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int report_val = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) u32 power_state_usage_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) u32 report_state_usage_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * It is possible that the power/report state ids are not present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * In this case this function will return success. But if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * ids are present, then it will return error if set fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) power_state_usage_id =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) report_state_usage_id =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) power_state_usage_id =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) report_state_usage_id =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (sensor_inst->power_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) power_val = hid_sensor_get_usage_index(sensor_inst->hsdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) sensor_inst->power_state->attribute.report_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) sensor_inst->power_state->attribute.index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) power_state_usage_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (sensor_inst->report_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) report_val = hid_sensor_get_usage_index(sensor_inst->hsdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) sensor_inst->report_state->attribute.report_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) sensor_inst->report_state->attribute.index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) report_state_usage_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (power_val >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) power_val +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) sensor_inst->power_state->attribute.logical_minimum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ret = sensor_hub_set_feature(sensor_inst->hsdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) sensor_inst->power_state->attribute.report_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) sensor_inst->power_state->attribute.index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) sizeof(power_val),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) &power_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) hid_err(sensor_inst->hsdev->hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) "Set power state failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (report_val >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) report_val +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) sensor_inst->report_state->attribute.logical_minimum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) ret = sensor_hub_set_feature(sensor_inst->hsdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) sensor_inst->report_state->attribute.report_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) sensor_inst->report_state->attribute.index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) sizeof(report_val),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) &report_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) hid_err(sensor_inst->hsdev->hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) "Set report state failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static ssize_t enable_sensor_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (kstrtoint(buf, 0, &value) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) mutex_lock(&sensor_inst->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (value && !sensor_inst->enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) ret = sensor_hub_device_open(sensor_inst->hsdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) goto unlock_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) ret = set_power_report_state(sensor_inst, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) sensor_hub_device_close(sensor_inst->hsdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) goto unlock_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) sensor_inst->enable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) } else if (!value && sensor_inst->enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) ret = set_power_report_state(sensor_inst, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) sensor_hub_device_close(sensor_inst->hsdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) sensor_inst->enable = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) unlock_state:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) mutex_unlock(&sensor_inst->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static DEVICE_ATTR_RW(enable_sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static struct attribute *enable_sensor_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) &dev_attr_enable_sensor.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static const struct attribute_group enable_sensor_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) .attrs = enable_sensor_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static ssize_t show_value(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) struct hid_sensor_hub_attribute_info *attribute;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) int index, usage, field_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) char name[HID_CUSTOM_NAME_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) bool feature = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) bool input = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) int value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (sscanf(attr->attr.name, "feature-%x-%x-%s", &index, &usage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) name) == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) feature = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) field_index = index + sensor_inst->input_field_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) } else if (sscanf(attr->attr.name, "input-%x-%x-%s", &index, &usage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) name) == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) input = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) field_index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (!strncmp(name, "value", strlen("value"))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) u32 report_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) attribute = &sensor_inst->fields[field_index].attribute;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) report_id = attribute->report_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (feature) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) u8 values[HID_CUSTOM_MAX_FEATURE_BYTES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) int len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) u64 value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) ret = sensor_hub_get_feature(sensor_inst->hsdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) report_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) sizeof(values), values);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) while (i < ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (i + attribute->size > ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) len += scnprintf(&buf[len],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) PAGE_SIZE - len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) "%d ", values[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) switch (attribute->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) value = (u64) *(u16 *)&values[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) i += attribute->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) value = (u64) *(u32 *)&values[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) i += attribute->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) value = *(u64 *)&values[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) i += attribute->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) value = (u64) values[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) ++i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) len += scnprintf(&buf[len], PAGE_SIZE - len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) "%lld ", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) len += scnprintf(&buf[len], PAGE_SIZE - len, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) } else if (input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) value = sensor_hub_input_attr_get_raw_value(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) sensor_inst->hsdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) sensor_inst->hsdev->usage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) usage, report_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) SENSOR_HUB_SYNC, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) } else if (!strncmp(name, "units", strlen("units")))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) value = sensor_inst->fields[field_index].attribute.units;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) else if (!strncmp(name, "unit-expo", strlen("unit-expo")))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) value = sensor_inst->fields[field_index].attribute.unit_expo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) else if (!strncmp(name, "size", strlen("size")))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) value = sensor_inst->fields[field_index].attribute.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) else if (!strncmp(name, "minimum", strlen("minimum")))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) value = sensor_inst->fields[field_index].attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) logical_minimum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) else if (!strncmp(name, "maximum", strlen("maximum")))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) value = sensor_inst->fields[field_index].attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) logical_maximum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) else if (!strncmp(name, "name", strlen("name"))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct hid_custom_usage_desc *usage_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) usage_desc = bsearch(&usage, hid_custom_usage_desc_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) ARRAY_SIZE(hid_custom_usage_desc_table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) sizeof(struct hid_custom_usage_desc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) usage_id_cmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (usage_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return snprintf(buf, PAGE_SIZE, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) usage_desc->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) return sprintf(buf, "not-specified\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) return sprintf(buf, "%d\n", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static ssize_t store_value(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) struct hid_sensor_custom *sensor_inst = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) int index, field_index, usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) char name[HID_CUSTOM_NAME_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (sscanf(attr->attr.name, "feature-%x-%x-%s", &index, &usage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) name) == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) field_index = index + sensor_inst->input_field_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (!strncmp(name, "value", strlen("value"))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) u32 report_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (kstrtoint(buf, 0, &value) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) report_id = sensor_inst->fields[field_index].attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) report_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) ret = sensor_hub_set_feature(sensor_inst->hsdev, report_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) index, sizeof(value), &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static int hid_sensor_capture_sample(struct hid_sensor_hub_device *hsdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) unsigned usage_id, size_t raw_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) char *raw_data, void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) struct hid_sensor_custom *sensor_inst = platform_get_drvdata(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) struct hid_sensor_sample header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) /* If any error occurs in a sample, rest of the fields are ignored */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (sensor_inst->input_skip_sample) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) hid_err(sensor_inst->hsdev->hdev, "Skipped remaining data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) hid_dbg(sensor_inst->hsdev->hdev, "%s received %d of %d\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) (int) (sensor_inst->input_report_recd_size + raw_len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) sensor_inst->input_report_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (!test_bit(0, &sensor_inst->misc_opened))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (!sensor_inst->input_report_recd_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) int required_size = sizeof(struct hid_sensor_sample) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) sensor_inst->input_report_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) header.usage_id = hsdev->usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) header.raw_len = sensor_inst->input_report_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) header.timestamp = ktime_get_real_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (kfifo_avail(&sensor_inst->data_fifo) >= required_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) kfifo_in(&sensor_inst->data_fifo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) (unsigned char *)&header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) sizeof(header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) sensor_inst->input_skip_sample = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (kfifo_avail(&sensor_inst->data_fifo) >= raw_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) kfifo_in(&sensor_inst->data_fifo, (unsigned char *)raw_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) raw_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) sensor_inst->input_report_recd_size += raw_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) static int hid_sensor_send_event(struct hid_sensor_hub_device *hsdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) unsigned usage_id, void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) struct hid_sensor_custom *sensor_inst = platform_get_drvdata(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if (!test_bit(0, &sensor_inst->misc_opened))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) sensor_inst->input_report_recd_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) sensor_inst->input_skip_sample = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) wake_up(&sensor_inst->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) static int hid_sensor_custom_add_field(struct hid_sensor_custom *sensor_inst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) int index, int report_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) struct hid_report *report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) struct hid_field *field)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) struct hid_sensor_custom_field *sensor_field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) void *fields;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) fields = krealloc(sensor_inst->fields,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) (sensor_inst->sensor_field_count + 1) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) sizeof(struct hid_sensor_custom_field), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) if (!fields) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) kfree(sensor_inst->fields);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) sensor_inst->fields = fields;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) sensor_field = &sensor_inst->fields[sensor_inst->sensor_field_count];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) sensor_field->attribute.usage_id = sensor_inst->hsdev->usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (field->logical)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) sensor_field->attribute.attrib_id = field->logical;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) sensor_field->attribute.attrib_id = field->usage[0].hid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) sensor_field->attribute.index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) sensor_field->attribute.report_id = report->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) sensor_field->attribute.units = field->unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) sensor_field->attribute.unit_expo = field->unit_exponent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) sensor_field->attribute.size = (field->report_size / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) sensor_field->attribute.logical_minimum = field->logical_minimum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) sensor_field->attribute.logical_maximum = field->logical_maximum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (report_type == HID_FEATURE_REPORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) snprintf(sensor_field->group_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) sizeof(sensor_field->group_name), "feature-%x-%x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) sensor_field->attribute.index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) sensor_field->attribute.attrib_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) else if (report_type == HID_INPUT_REPORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) snprintf(sensor_field->group_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) sizeof(sensor_field->group_name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) "input-%x-%x", sensor_field->attribute.index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) sensor_field->attribute.attrib_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) sensor_inst->input_field_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) sensor_inst->input_report_size += (field->report_size *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) field->report_count) / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) memset(&sensor_field->hid_custom_attribute_group, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) sizeof(struct attribute_group));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) sensor_inst->sensor_field_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) static int hid_sensor_custom_add_fields(struct hid_sensor_custom *sensor_inst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) struct hid_report_enum *report_enum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) int report_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) struct hid_report *report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) struct hid_field *field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) struct hid_sensor_hub_device *hsdev = sensor_inst->hsdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) list_for_each_entry(report, &report_enum->report_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) for (i = 0; i < report->maxfield; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) field = report->field[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (field->maxusage &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) ((field->usage[0].collection_index >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) hsdev->start_collection_index) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) (field->usage[0].collection_index <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) hsdev->end_collection_index))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) ret = hid_sensor_custom_add_field(sensor_inst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) report_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) field);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) static int hid_sensor_custom_add_attributes(struct hid_sensor_custom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) *sensor_inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) struct hid_sensor_hub_device *hsdev = sensor_inst->hsdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) struct hid_device *hdev = hsdev->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) for (j = 0; j < HID_REPORT_TYPES; ++j) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (j == HID_OUTPUT_REPORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) ret = hid_sensor_custom_add_fields(sensor_inst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) &hdev->report_enum[j], j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) /* Create sysfs attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) for (i = 0; i < sensor_inst->sensor_field_count; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) while (j < HID_CUSTOM_TOTAL_ATTRS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) hid_custom_attrs[j].name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) struct device_attribute *device_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) device_attr = &sensor_inst->fields[i].sd_attrs[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) snprintf((char *)&sensor_inst->fields[i].attr_name[j],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) HID_CUSTOM_NAME_LENGTH, "%s-%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) sensor_inst->fields[i].group_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) hid_custom_attrs[j].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) sysfs_attr_init(&device_attr->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) device_attr->attr.name =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) (char *)&sensor_inst->fields[i].attr_name[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) device_attr->attr.mode = hid_custom_attrs[j].mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) device_attr->show = show_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) if (hid_custom_attrs[j].mode & S_IWUSR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) device_attr->store = store_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) sensor_inst->fields[i].attrs[j] = &device_attr->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) ++j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) sensor_inst->fields[i].attrs[j] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) sensor_inst->fields[i].hid_custom_attribute_group.attrs =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) sensor_inst->fields[i].attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) sensor_inst->fields[i].hid_custom_attribute_group.name =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) sensor_inst->fields[i].group_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) ret = sysfs_create_group(&sensor_inst->pdev->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) &sensor_inst->fields[i].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) hid_custom_attribute_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) /* For power or report field store indexes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) if (sensor_inst->fields[i].attribute.attrib_id ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) HID_USAGE_SENSOR_PROY_POWER_STATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) sensor_inst->power_state = &sensor_inst->fields[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) else if (sensor_inst->fields[i].attribute.attrib_id ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) HID_USAGE_SENSOR_PROP_REPORT_STATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) sensor_inst->report_state = &sensor_inst->fields[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) static void hid_sensor_custom_remove_attributes(struct hid_sensor_custom *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) sensor_inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) for (i = 0; i < sensor_inst->sensor_field_count; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) &sensor_inst->fields[i].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) hid_custom_attribute_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) kfree(sensor_inst->fields);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) static ssize_t hid_sensor_custom_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) size_t count, loff_t *f_ps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) struct hid_sensor_custom *sensor_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) unsigned int copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) sensor_inst = container_of(file->private_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) struct hid_sensor_custom, custom_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) if (count < sizeof(struct hid_sensor_sample))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) if (kfifo_is_empty(&sensor_inst->data_fifo)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (file->f_flags & O_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) ret = wait_event_interruptible(sensor_inst->wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) !kfifo_is_empty(&sensor_inst->data_fifo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) ret = kfifo_to_user(&sensor_inst->data_fifo, buf, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) &copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) } while (copied == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) return copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) static int hid_sensor_custom_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) struct hid_sensor_custom *sensor_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) sensor_inst = container_of(file->private_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) struct hid_sensor_custom, custom_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) clear_bit(0, &sensor_inst->misc_opened);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) static int hid_sensor_custom_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) struct hid_sensor_custom *sensor_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) sensor_inst = container_of(file->private_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) struct hid_sensor_custom, custom_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) /* We essentially have single reader and writer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) if (test_and_set_bit(0, &sensor_inst->misc_opened))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) return stream_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) static __poll_t hid_sensor_custom_poll(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) struct poll_table_struct *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) struct hid_sensor_custom *sensor_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) __poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) sensor_inst = container_of(file->private_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) struct hid_sensor_custom, custom_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) poll_wait(file, &sensor_inst->wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) if (!kfifo_is_empty(&sensor_inst->data_fifo))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) mask = EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) static const struct file_operations hid_sensor_custom_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) .open = hid_sensor_custom_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) .read = hid_sensor_custom_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) .release = hid_sensor_custom_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) .poll = hid_sensor_custom_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) static int hid_sensor_custom_dev_if_add(struct hid_sensor_custom *sensor_inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) ret = kfifo_alloc(&sensor_inst->data_fifo, HID_CUSTOM_FIFO_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) init_waitqueue_head(&sensor_inst->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) sensor_inst->custom_dev.minor = MISC_DYNAMIC_MINOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) sensor_inst->custom_dev.name = dev_name(&sensor_inst->pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) sensor_inst->custom_dev.fops = &hid_sensor_custom_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) ret = misc_register(&sensor_inst->custom_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) kfifo_free(&sensor_inst->data_fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) static void hid_sensor_custom_dev_if_remove(struct hid_sensor_custom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) *sensor_inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) wake_up(&sensor_inst->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) misc_deregister(&sensor_inst->custom_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) kfifo_free(&sensor_inst->data_fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) static int hid_sensor_custom_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) struct hid_sensor_custom *sensor_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) sensor_inst = devm_kzalloc(&pdev->dev, sizeof(*sensor_inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) if (!sensor_inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) sensor_inst->callbacks.capture_sample = hid_sensor_capture_sample;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) sensor_inst->callbacks.send_event = hid_sensor_send_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) sensor_inst->callbacks.pdev = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) sensor_inst->hsdev = hsdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) sensor_inst->pdev = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) mutex_init(&sensor_inst->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) platform_set_drvdata(pdev, sensor_inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) ret = sensor_hub_register_callback(hsdev, hsdev->usage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) &sensor_inst->callbacks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) dev_err(&pdev->dev, "callback reg failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) ret = sysfs_create_group(&sensor_inst->pdev->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) &enable_sensor_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) goto err_remove_callback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) ret = hid_sensor_custom_add_attributes(sensor_inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) goto err_remove_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) ret = hid_sensor_custom_dev_if_add(sensor_inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) goto err_remove_attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) err_remove_attributes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) hid_sensor_custom_remove_attributes(sensor_inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) err_remove_group:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) &enable_sensor_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) err_remove_callback:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) sensor_hub_remove_callback(hsdev, hsdev->usage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) static int hid_sensor_custom_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) struct hid_sensor_custom *sensor_inst = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) hid_sensor_custom_dev_if_remove(sensor_inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) hid_sensor_custom_remove_attributes(sensor_inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) sysfs_remove_group(&sensor_inst->pdev->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) &enable_sensor_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) sensor_hub_remove_callback(hsdev, hsdev->usage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) static const struct platform_device_id hid_sensor_custom_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) .name = "HID-SENSOR-2000e1",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) .name = "HID-SENSOR-2000e2",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) { /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) MODULE_DEVICE_TABLE(platform, hid_sensor_custom_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) static struct platform_driver hid_sensor_custom_platform_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) .id_table = hid_sensor_custom_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) .name = KBUILD_MODNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) .probe = hid_sensor_custom_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) .remove = hid_sensor_custom_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) module_platform_driver(hid_sensor_custom_platform_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) MODULE_DESCRIPTION("HID Sensor Custom and Generic sensor Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) MODULE_LICENSE("GPL");