Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * HID Sensors Driver
0004  * Copyright (c) 2012, Intel Corporation.
0005  */
0006 #ifndef _HID_SENSORS_HUB_H
0007 #define _HID_SENSORS_HUB_H
0008 
0009 #include <linux/hid.h>
0010 #include <linux/hid-sensor-ids.h>
0011 #include <linux/iio/iio.h>
0012 #include <linux/iio/trigger.h>
0013 
0014 /**
0015  * struct hid_sensor_hub_attribute_info - Attribute info
0016  * @usage_id:       Parent usage id of a physical device.
0017  * @attrib_id:      Attribute id for this attribute.
0018  * @report_id:      Report id in which this information resides.
0019  * @index:      Field index in the report.
0020  * @units:      Measurment unit for this attribute.
0021  * @unit_expo:      Exponent used in the data.
0022  * @size:       Size in bytes for data size.
0023  * @logical_minimum:    Logical minimum value for this attribute.
0024  * @logical_maximum:    Logical maximum value for this attribute.
0025  */
0026 struct hid_sensor_hub_attribute_info {
0027     u32 usage_id;
0028     u32 attrib_id;
0029     s32 report_id;
0030     s32 index;
0031     s32 units;
0032     s32 unit_expo;
0033     s32 size;
0034     s32 logical_minimum;
0035     s32 logical_maximum;
0036 };
0037 
0038 /**
0039  * struct sensor_hub_pending - Synchronous read pending information
0040  * @status:     Pending status true/false.
0041  * @ready:      Completion synchronization data.
0042  * @usage_id:       Usage id for physical device, E.g. Gyro usage id.
0043  * @attr_usage_id:  Usage Id of a field, E.g. X-AXIS for a gyro.
0044  * @raw_size:       Response size for a read request.
0045  * @raw_data:       Place holder for received response.
0046  */
0047 struct sensor_hub_pending {
0048     bool status;
0049     struct completion ready;
0050     u32 usage_id;
0051     u32 attr_usage_id;
0052     int raw_size;
0053     u8  *raw_data;
0054 };
0055 
0056 /**
0057  * struct hid_sensor_hub_device - Stores the hub instance data
0058  * @hdev:       Stores the hid instance.
0059  * @vendor_id:      Vendor id of hub device.
0060  * @product_id:     Product id of hub device.
0061  * @usage:      Usage id for this hub device instance.
0062  * @start_collection_index: Starting index for a phy type collection
0063  * @end_collection_index: Last index for a phy type collection
0064  * @mutex_ptr:      synchronizing mutex pointer.
0065  * @pending:        Holds information of pending sync read request.
0066  */
0067 struct hid_sensor_hub_device {
0068     struct hid_device *hdev;
0069     u32 vendor_id;
0070     u32 product_id;
0071     u32 usage;
0072     int start_collection_index;
0073     int end_collection_index;
0074     struct mutex *mutex_ptr;
0075     struct sensor_hub_pending pending;
0076 };
0077 
0078 /**
0079  * struct hid_sensor_hub_callbacks - Client callback functions
0080  * @pdev:       Platform device instance of the client driver.
0081  * @suspend:        Suspend callback.
0082  * @resume:     Resume callback.
0083  * @capture_sample: Callback to get a sample.
0084  * @send_event:     Send notification to indicate all samples are
0085  *          captured, process and send event
0086  */
0087 struct hid_sensor_hub_callbacks {
0088     struct platform_device *pdev;
0089     int (*suspend)(struct hid_sensor_hub_device *hsdev, void *priv);
0090     int (*resume)(struct hid_sensor_hub_device *hsdev, void *priv);
0091     int (*capture_sample)(struct hid_sensor_hub_device *hsdev,
0092             u32 usage_id, size_t raw_len, char *raw_data,
0093             void *priv);
0094     int (*send_event)(struct hid_sensor_hub_device *hsdev, u32 usage_id,
0095              void *priv);
0096 };
0097 
0098 /**
0099 * sensor_hub_device_open() - Open hub device
0100 * @hsdev:   Hub device instance.
0101 *
0102 * Used to open hid device for sensor hub.
0103 */
0104 int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev);
0105 
0106 /**
0107 * sensor_hub_device_clode() - Close hub device
0108 * @hsdev:   Hub device instance.
0109 *
0110 * Used to clode hid device for sensor hub.
0111 */
0112 void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev);
0113 
0114 /* Registration functions */
0115 
0116 /**
0117 * sensor_hub_register_callback() - Register client callbacks
0118 * @hsdev:   Hub device instance.
0119 * @usage_id:    Usage id of the client (E.g. 0x200076 for Gyro).
0120 * @usage_callback: Callback function storage
0121 *
0122 * Used to register callbacks by client processing drivers. Sensor
0123 * hub core driver will call these callbacks to offload processing
0124 * of data streams and notifications.
0125 */
0126 int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
0127             u32 usage_id,
0128             struct hid_sensor_hub_callbacks *usage_callback);
0129 
0130 /**
0131 * sensor_hub_remove_callback() - Remove client callbacks
0132 * @hsdev:   Hub device instance.
0133 * @usage_id:    Usage id of the client (E.g. 0x200076 for Gyro).
0134 *
0135 * If there is a callback registred, this call will remove that
0136 * callbacks, so that it will stop data and event notifications.
0137 */
0138 int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
0139             u32 usage_id);
0140 
0141 
0142 /* Hid sensor hub core interfaces */
0143 
0144 /**
0145 * sensor_hub_input_get_attribute_info() - Get an attribute information
0146 * @hsdev:   Hub device instance.
0147 * @type:    Type of this attribute, input/output/feature
0148 * @usage_id:    Attribute usage id of parent physical device as per spec
0149 * @attr_usage_id:   Attribute usage id as per spec
0150 * @info:    return information about attribute after parsing report
0151 *
0152 * Parses report and returns the attribute information such as report id,
0153 * field index, units and exponent etc.
0154 */
0155 int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
0156             u8 type,
0157             u32 usage_id, u32 attr_usage_id,
0158             struct hid_sensor_hub_attribute_info *info);
0159 
0160 /**
0161 * sensor_hub_input_attr_get_raw_value() - Synchronous read request
0162 * @hsdev:   Hub device instance.
0163 * @usage_id:    Attribute usage id of parent physical device as per spec
0164 * @attr_usage_id:   Attribute usage id as per spec
0165 * @report_id:   Report id to look for
0166 * @flag:      Synchronous or asynchronous read
0167 * @is_signed:   If true then fields < 32 bits will be sign-extended
0168 *
0169 * Issues a synchronous or asynchronous read request for an input attribute.
0170 * Return: data up to 32 bits.
0171 */
0172 
0173 enum sensor_hub_read_flags {
0174     SENSOR_HUB_SYNC,
0175     SENSOR_HUB_ASYNC,
0176 };
0177 
0178 int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
0179                     u32 usage_id,
0180                     u32 attr_usage_id, u32 report_id,
0181                     enum sensor_hub_read_flags flag,
0182                     bool is_signed
0183 );
0184 
0185 /**
0186 * sensor_hub_set_feature() - Feature set request
0187 * @hsdev:   Hub device instance.
0188 * @report_id:   Report id to look for
0189 * @field_index: Field index inside a report
0190 * @buffer_size: size of the buffer
0191 * @buffer:  buffer to use in the feature set
0192 *
0193 * Used to set a field in feature report. For example this can set polling
0194 * interval, sensitivity, activate/deactivate state.
0195 */
0196 int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
0197                u32 field_index, int buffer_size, void *buffer);
0198 
0199 /**
0200 * sensor_hub_get_feature() - Feature get request
0201 * @hsdev:   Hub device instance.
0202 * @report_id:   Report id to look for
0203 * @field_index: Field index inside a report
0204 * @buffer_size: size of the buffer
0205 * @buffer:  buffer to copy output
0206 *
0207 * Used to get a field in feature report. For example this can get polling
0208 * interval, sensitivity, activate/deactivate state.
0209 * Return: On success, it returns the number of bytes copied to buffer.
0210 * On failure, it returns value < 0.
0211 */
0212 int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
0213                u32 field_index, int buffer_size, void *buffer);
0214 
0215 /* hid-sensor-attributes */
0216 
0217 /* Common hid sensor iio structure */
0218 struct hid_sensor_common {
0219     struct hid_sensor_hub_device *hsdev;
0220     struct platform_device *pdev;
0221     unsigned usage_id;
0222     atomic_t data_ready;
0223     atomic_t user_requested_state;
0224     atomic_t runtime_pm_enable;
0225     int poll_interval;
0226     int raw_hystersis;
0227     int latency_ms;
0228     struct iio_trigger *trigger;
0229     int timestamp_ns_scale;
0230     struct hid_sensor_hub_attribute_info poll;
0231     struct hid_sensor_hub_attribute_info report_state;
0232     struct hid_sensor_hub_attribute_info power_state;
0233     struct hid_sensor_hub_attribute_info sensitivity;
0234     struct hid_sensor_hub_attribute_info sensitivity_rel;
0235     struct hid_sensor_hub_attribute_info report_latency;
0236     struct work_struct work;
0237 };
0238 
0239 /* Convert from hid unit expo to regular exponent */
0240 static inline int hid_sensor_convert_exponent(int unit_expo)
0241 {
0242     if (unit_expo < 0x08)
0243         return unit_expo;
0244     else if (unit_expo <= 0x0f)
0245         return -(0x0f-unit_expo+1);
0246     else
0247         return 0;
0248 }
0249 
0250 int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
0251                     u32 usage_id,
0252                     struct hid_sensor_common *st,
0253                     const u32 *sensitivity_addresses,
0254                     u32 sensitivity_addresses_len);
0255 int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
0256                     int val1, int val2);
0257 int hid_sensor_write_raw_hyst_rel_value(struct hid_sensor_common *st, int val1,
0258                     int val2);
0259 int hid_sensor_read_raw_hyst_value(struct hid_sensor_common *st,
0260                     int *val1, int *val2);
0261 int hid_sensor_read_raw_hyst_rel_value(struct hid_sensor_common *st,
0262                        int *val1, int *val2);
0263 int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
0264                     int val1, int val2);
0265 int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
0266                     int *val1, int *val2);
0267 
0268 int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
0269                 u32 report_id, int field_index, u32 usage_id);
0270 
0271 int hid_sensor_format_scale(u32 usage_id,
0272                 struct hid_sensor_hub_attribute_info *attr_info,
0273                 int *val0, int *val1);
0274 
0275 s32 hid_sensor_read_poll_value(struct hid_sensor_common *st);
0276 
0277 int64_t hid_sensor_convert_timestamp(struct hid_sensor_common *st,
0278                      int64_t raw_value);
0279 bool hid_sensor_batch_mode_supported(struct hid_sensor_common *st);
0280 int hid_sensor_set_report_latency(struct hid_sensor_common *st, int latency);
0281 int hid_sensor_get_report_latency(struct hid_sensor_common *st);
0282 
0283 #endif