1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * HID Sensors Driver
4 * Copyright (c) 2012, Intel Corporation.
5 */
6 #ifndef _HID_SENSORS_HUB_H
7 #define _HID_SENSORS_HUB_H
8
9 #include <linux/hid.h>
10 #include <linux/hid-sensor-ids.h>
11 #include <linux/iio/iio.h>
12 #include <linux/iio/trigger.h>
13
14 /**
15 * struct hid_sensor_hub_attribute_info - Attribute info
16 * @usage_id: Parent usage id of a physical device.
17 * @attrib_id: Attribute id for this attribute.
18 * @report_id: Report id in which this information resides.
19 * @index: Field index in the report.
20 * @units: Measurement unit for this attribute.
21 * @unit_expo: Exponent used in the data.
22 * @size: Size in bytes for data size.
23 * @logical_minimum: Logical minimum value for this attribute.
24 * @logical_maximum: Logical maximum value for this attribute.
25 */
26 struct hid_sensor_hub_attribute_info {
27 u32 usage_id;
28 u32 attrib_id;
29 s32 report_id;
30 s32 index;
31 s32 units;
32 s32 unit_expo;
33 s32 size;
34 s32 logical_minimum;
35 s32 logical_maximum;
36 };
37
38 /**
39 * struct sensor_hub_pending - Synchronous read pending information
40 * @status: Pending status true/false.
41 * @ready: Completion synchronization data.
42 * @usage_id: Usage id for physical device, e.g. gyro usage id.
43 * @attr_usage_id: Usage Id of a field, e.g. X-axis for a gyro.
44 * @raw_size: Response size for a read request.
45 * @raw_data: Place holder for received response.
46 */
47 struct sensor_hub_pending {
48 bool status;
49 struct completion ready;
50 u32 usage_id;
51 u32 attr_usage_id;
52 int raw_size;
53 u8 *raw_data;
54 };
55
56 /**
57 * struct hid_sensor_hub_device - Stores the hub instance data
58 * @hdev: Stores the hid instance.
59 * @vendor_id: Vendor id of hub device.
60 * @product_id: Product id of hub device.
61 * @usage: Usage id for this hub device instance.
62 * @start_collection_index: Starting index for a phy type collection
63 * @end_collection_index: Last index for a phy type collection
64 * @mutex_ptr: synchronizing mutex pointer.
65 * @pending: Holds information of pending sync read request.
66 */
67 struct hid_sensor_hub_device {
68 struct hid_device *hdev;
69 u32 vendor_id;
70 u32 product_id;
71 u32 usage;
72 int start_collection_index;
73 int end_collection_index;
74 struct mutex *mutex_ptr;
75 struct sensor_hub_pending pending;
76 };
77
78 /**
79 * struct hid_sensor_hub_callbacks - Client callback functions
80 * @pdev: Platform device instance of the client driver.
81 * @suspend: Suspend callback.
82 * @resume: Resume callback.
83 * @capture_sample: Callback to get a sample.
84 * @send_event: Send notification to indicate all samples are
85 * captured, process and send event
86 */
87 struct hid_sensor_hub_callbacks {
88 struct platform_device *pdev;
89 int (*suspend)(struct hid_sensor_hub_device *hsdev, void *priv);
90 int (*resume)(struct hid_sensor_hub_device *hsdev, void *priv);
91 int (*capture_sample)(struct hid_sensor_hub_device *hsdev,
92 u32 usage_id, size_t raw_len, char *raw_data,
93 void *priv);
94 int (*send_event)(struct hid_sensor_hub_device *hsdev, u32 usage_id,
95 void *priv);
96 };
97
98 /**
99 * sensor_hub_device_open() - Open hub device
100 * @hsdev: Hub device instance.
101 *
102 * Used to open hid device for sensor hub.
103 */
104 int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev);
105
106 /**
107 * sensor_hub_device_close() - Close hub device
108 * @hsdev: Hub device instance.
109 *
110 * Used to close hid device for sensor hub.
111 */
112 void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev);
113
114 /* Registration functions */
115
116 /**
117 * sensor_hub_register_callback() - Register client callbacks
118 * @hsdev: Hub device instance.
119 * @usage_id: Usage id of the client (E.g. 0x200076 for Gyro).
120 * @usage_callback: Callback function storage
121 *
122 * Used to register callbacks by client processing drivers. Sensor
123 * hub core driver will call these callbacks to offload processing
124 * of data streams and notifications.
125 */
126 int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
127 u32 usage_id,
128 struct hid_sensor_hub_callbacks *usage_callback);
129
130 /**
131 * sensor_hub_remove_callback() - Remove client callback
132 * @hsdev: Hub device instance.
133 * @usage_id: Usage id of the client (e.g. 0x200076 for gyro).
134 *
135 * Removes a previously registered callback for the given usage_id
136 * and hsdev. Once removed, the client will no longer receive data or
137 * event notifications.
138 */
139 int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
140 u32 usage_id);
141
142
143 /* Hid sensor hub core interfaces */
144
145 /**
146 * sensor_hub_input_get_attribute_info() - Get an attribute information
147 * @hsdev: Hub device instance.
148 * @type: Type of this attribute, input/output/feature
149 * @usage_id: Attribute usage id of parent physical device as per spec
150 * @attr_usage_id: Attribute usage id as per spec
151 * @info: return information about attribute after parsing report
152 *
153 * Parses report and returns the attribute information such as report id,
154 * field index, units and exponent etc.
155 */
156 int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
157 u8 type,
158 u32 usage_id, u32 attr_usage_id,
159 struct hid_sensor_hub_attribute_info *info);
160
161 /**
162 * sensor_hub_input_attr_get_raw_value() - Synchronous read request
163 * @hsdev: Hub device instance.
164 * @usage_id: Attribute usage id of parent physical device as per spec
165 * @attr_usage_id: Attribute usage id as per spec
166 * @report_id: Report id to look for
167 * @flag: Synchronous or asynchronous read
168 * @is_signed: If true then fields < 32 bits will be sign-extended
169 *
170 * Issues a synchronous or asynchronous read request for an input attribute.
171 * Return: data up to 32 bits.
172 */
173
174 enum sensor_hub_read_flags {
175 SENSOR_HUB_SYNC,
176 SENSOR_HUB_ASYNC,
177 };
178
179 int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
180 u32 usage_id,
181 u32 attr_usage_id, u32 report_id,
182 enum sensor_hub_read_flags flag,
183 bool is_signed
184 );
185
186 /**
187 * sensor_hub_set_feature() - Feature set request
188 * @hsdev: Hub device instance.
189 * @report_id: Report id to look for
190 * @field_index: Field index inside a report
191 * @buffer_size: size of the buffer
192 * @buffer: buffer to use in the feature set
193 *
194 * Used to set a field in feature report. For example this can set polling
195 * interval, sensitivity, activate/deactivate state.
196 */
197 int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
198 u32 field_index, int buffer_size, void *buffer);
199
200 /**
201 * sensor_hub_get_feature() - Feature get request
202 * @hsdev: Hub device instance.
203 * @report_id: Report id to look for
204 * @field_index: Field index inside a report
205 * @buffer_size: size of the buffer
206 * @buffer: buffer to copy output
207 *
208 * Used to get a field in feature report. For example this can get polling
209 * interval, sensitivity, activate/deactivate state.
210 * Return: On success, it returns the number of bytes copied to buffer.
211 * On failure, it returns value < 0.
212 */
213 int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
214 u32 field_index, int buffer_size, void *buffer);
215
216 /* hid-sensor-attributes */
217
218 /* Common hid sensor iio structure */
219 struct hid_sensor_common {
220 struct hid_sensor_hub_device *hsdev;
221 struct platform_device *pdev;
222 unsigned usage_id;
223 atomic_t data_ready;
224 atomic_t user_requested_state;
225 atomic_t runtime_pm_enable;
226 int poll_interval;
227 int raw_hystersis;
228 int latency_ms;
229 struct iio_trigger *trigger;
230 int timestamp_ns_scale;
231 struct hid_sensor_hub_attribute_info poll;
232 struct hid_sensor_hub_attribute_info report_state;
233 struct hid_sensor_hub_attribute_info power_state;
234 struct hid_sensor_hub_attribute_info sensitivity;
235 struct hid_sensor_hub_attribute_info sensitivity_rel;
236 struct hid_sensor_hub_attribute_info report_latency;
237 struct work_struct work;
238 };
239
240 /* Convert from hid unit expo to regular exponent */
hid_sensor_convert_exponent(int unit_expo)241 static inline int hid_sensor_convert_exponent(int unit_expo)
242 {
243 if (unit_expo < 0x08)
244 return unit_expo;
245 else if (unit_expo <= 0x0f)
246 return -(0x0f-unit_expo+1);
247 else
248 return 0;
249 }
250
251 int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
252 u32 usage_id,
253 struct hid_sensor_common *st,
254 const u32 *sensitivity_addresses,
255 u32 sensitivity_addresses_len);
256 int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
257 int val1, int val2);
258 int hid_sensor_write_raw_hyst_rel_value(struct hid_sensor_common *st, int val1,
259 int val2);
260 int hid_sensor_read_raw_hyst_value(struct hid_sensor_common *st,
261 int *val1, int *val2);
262 int hid_sensor_read_raw_hyst_rel_value(struct hid_sensor_common *st,
263 int *val1, int *val2);
264 int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
265 int val1, int val2);
266 int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
267 int *val1, int *val2);
268
269 int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
270 u32 report_id, int field_index, u32 usage_id);
271
272 int hid_sensor_format_scale(u32 usage_id,
273 struct hid_sensor_hub_attribute_info *attr_info,
274 int *val0, int *val1);
275
276 s32 hid_sensor_read_poll_value(struct hid_sensor_common *st);
277
278 int64_t hid_sensor_convert_timestamp(struct hid_sensor_common *st,
279 int64_t raw_value);
280 bool hid_sensor_batch_mode_supported(struct hid_sensor_common *st);
281 int hid_sensor_set_report_latency(struct hid_sensor_common *st, int latency);
282 int hid_sensor_get_report_latency(struct hid_sensor_common *st);
283
284 #endif
285