xref: /linux/include/linux/hid-sensor-hub.h (revision fab183d632628381b466a41479489541ac0e29a0)
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  * @index:		Current write index into raw_data for multi-byte reads.
47  * @max_raw_size:	Total buffer size for multi-byte reads; 0 for single-value reads.
48  */
49 struct sensor_hub_pending {
50 	bool status;
51 	struct completion ready;
52 	u32 usage_id;
53 	u32 attr_usage_id;
54 	int raw_size;
55 	u8  *raw_data;
56 	u32 index;
57 	u32 max_raw_size;
58 };
59 
60 /**
61  * struct hid_sensor_hub_device - Stores the hub instance data
62  * @hdev:		Stores the hid instance.
63  * @vendor_id:		Vendor id of hub device.
64  * @product_id:		Product id of hub device.
65  * @usage:		Usage id for this hub device instance.
66  * @start_collection_index: Starting index for a phy type collection
67  * @end_collection_index: Last index for a phy type collection
68  * @mutex_ptr:		synchronizing mutex pointer.
69  * @pending:		Holds information of pending sync read request.
70  */
71 struct hid_sensor_hub_device {
72 	struct hid_device *hdev;
73 	u32 vendor_id;
74 	u32 product_id;
75 	u32 usage;
76 	int start_collection_index;
77 	int end_collection_index;
78 	struct mutex *mutex_ptr;
79 	struct sensor_hub_pending pending;
80 };
81 
82 /**
83  * struct hid_sensor_hub_callbacks - Client callback functions
84  * @pdev:		Platform device instance of the client driver.
85  * @suspend:		Suspend callback.
86  * @resume:		Resume callback.
87  * @capture_sample:	Callback to get a sample.
88  * @send_event:		Send notification to indicate all samples are
89  *			captured, process and send event
90  */
91 struct hid_sensor_hub_callbacks {
92 	struct platform_device *pdev;
93 	int (*suspend)(struct hid_sensor_hub_device *hsdev, void *priv);
94 	int (*resume)(struct hid_sensor_hub_device *hsdev, void *priv);
95 	int (*capture_sample)(struct hid_sensor_hub_device *hsdev,
96 			u32 usage_id, size_t raw_len, char *raw_data,
97 			void *priv);
98 	int (*send_event)(struct hid_sensor_hub_device *hsdev, u32 usage_id,
99 			 void *priv);
100 };
101 
102 /**
103 * sensor_hub_device_open() - Open hub device
104 * @hsdev:	Hub device instance.
105 *
106 * Used to open hid device for sensor hub.
107 */
108 int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev);
109 
110 /**
111 * sensor_hub_device_close() - Close hub device
112 * @hsdev:	Hub device instance.
113 *
114 * Used to close hid device for sensor hub.
115 */
116 void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev);
117 
118 /* Registration functions */
119 
120 /**
121 * sensor_hub_register_callback() - Register client callbacks
122 * @hsdev:	Hub device instance.
123 * @usage_id:	Usage id of the client (E.g. 0x200076 for Gyro).
124 * @usage_callback: Callback function storage
125 *
126 * Used to register callbacks by client processing drivers. Sensor
127 * hub core driver will call these callbacks to offload processing
128 * of data streams and notifications.
129 */
130 int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
131 			u32 usage_id,
132 			struct hid_sensor_hub_callbacks *usage_callback);
133 
134 /**
135 * sensor_hub_remove_callback() - Remove client callback
136 * @hsdev:	Hub device instance.
137 * @usage_id:	Usage id of the client (e.g. 0x200076 for gyro).
138 *
139 * Removes a previously registered callback for the given usage_id
140 * and hsdev. Once removed, the client will no longer receive data or
141 * event notifications.
142 */
143 int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
144 			u32 usage_id);
145 
146 
147 /* Hid sensor hub core interfaces */
148 
149 /**
150 * sensor_hub_input_get_attribute_info() - Get an attribute information
151 * @hsdev:	Hub device instance.
152 * @type:	Type of this attribute, input/output/feature
153 * @usage_id:	Attribute usage id of parent physical device as per spec
154 * @attr_usage_id:	Attribute usage id as per spec
155 * @info:	return information about attribute after parsing report
156 *
157 * Parses report and returns the attribute information such as report id,
158 * field index, units and exponent etc.
159 */
160 int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
161 			u8 type,
162 			u32 usage_id, u32 attr_usage_id,
163 			struct hid_sensor_hub_attribute_info *info);
164 
165 /**
166 * sensor_hub_input_attr_get_raw_value() - Synchronous read request
167 * @hsdev:	Hub device instance.
168 * @usage_id:	Attribute usage id of parent physical device as per spec
169 * @attr_usage_id:	Attribute usage id as per spec
170 * @report_id:	Report id to look for
171 * @flag:      Synchronous or asynchronous read
172 * @is_signed:   If true then fields < 32 bits will be sign-extended
173 *
174 * Issues a synchronous or asynchronous read request for an input attribute.
175 * Return: data up to 32 bits.
176 */
177 
178 enum sensor_hub_read_flags {
179 	SENSOR_HUB_SYNC,
180 	SENSOR_HUB_ASYNC,
181 };
182 
183 int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
184  					u32 usage_id,
185  					u32 attr_usage_id, u32 report_id,
186 					enum sensor_hub_read_flags flag,
187 					bool is_signed
188 );
189 
190 /**
191  * sensor_hub_input_attr_read_values() - Synchronous multi-byte read request
192  * @hsdev:		Hub device instance.
193  * @usage_id:		Attribute usage id of parent physical device as per spec
194  * @attr_usage_id:	Attribute usage id as per spec
195  * @report_id:		Report id to look for
196  * @flag:		Synchronous or asynchronous read
197  * @buffer_size:	Size of the buffer in bytes
198  * @buffer:		Buffer to store the read data
199  *
200  * Issues a synchronous or asynchronous read request for an input attribute,
201  * accumulating data into the provided buffer until it is full.
202  * Return: 0 on success, -ETIMEDOUT if the device did not respond, or a
203  * negative error code.
204  */
205 int sensor_hub_input_attr_read_values(struct hid_sensor_hub_device *hsdev,
206 				      u32 usage_id, u32 attr_usage_id,
207 				      u32 report_id,
208 				      enum sensor_hub_read_flags flag,
209 				      u32 buffer_size, u8 *buffer);
210 
211 /**
212 * sensor_hub_set_feature() - Feature set request
213 * @hsdev:	Hub device instance.
214 * @report_id:	Report id to look for
215 * @field_index:	Field index inside a report
216 * @buffer_size: size of the buffer
217 * @buffer:	buffer to use in the feature set
218 *
219 * Used to set a field in feature report. For example this can set polling
220 * interval, sensitivity, activate/deactivate state.
221 */
222 int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
223 			   u32 field_index, int buffer_size, void *buffer);
224 
225 /**
226 * sensor_hub_get_feature() - Feature get request
227 * @hsdev:	Hub device instance.
228 * @report_id:	Report id to look for
229 * @field_index:	Field index inside a report
230 * @buffer_size:	size of the buffer
231 * @buffer:	buffer to copy output
232 *
233 * Used to get a field in feature report. For example this can get polling
234 * interval, sensitivity, activate/deactivate state.
235 * Return: On success, it returns the number of bytes copied to buffer.
236 * On failure, it returns value < 0.
237 */
238 int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
239 			   u32 field_index, int buffer_size, void *buffer);
240 
241 /* hid-sensor-attributes */
242 
243 /* Common hid sensor iio structure */
244 struct hid_sensor_common {
245 	struct hid_sensor_hub_device *hsdev;
246 	struct platform_device *pdev;
247 	unsigned usage_id;
248 	atomic_t data_ready;
249 	atomic_t user_requested_state;
250 	atomic_t runtime_pm_enable;
251 	int poll_interval;
252 	int raw_hystersis;
253 	int latency_ms;
254 	struct iio_trigger *trigger;
255 	int timestamp_ns_scale;
256 	struct hid_sensor_hub_attribute_info poll;
257 	struct hid_sensor_hub_attribute_info report_state;
258 	struct hid_sensor_hub_attribute_info power_state;
259 	struct hid_sensor_hub_attribute_info sensitivity;
260 	struct hid_sensor_hub_attribute_info sensitivity_rel;
261 	struct hid_sensor_hub_attribute_info report_latency;
262 	struct work_struct work;
263 };
264 
265 /* Convert from hid unit expo to regular exponent */
hid_sensor_convert_exponent(int unit_expo)266 static inline int hid_sensor_convert_exponent(int unit_expo)
267 {
268 	if (unit_expo < 0x08)
269 		return unit_expo;
270 	else if (unit_expo <= 0x0f)
271 		return -(0x0f-unit_expo+1);
272 	else
273 		return 0;
274 }
275 
276 int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
277 					u32 usage_id,
278 					struct hid_sensor_common *st,
279 					const u32 *sensitivity_addresses,
280 					u32 sensitivity_addresses_len);
281 int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
282 					int val1, int val2);
283 int hid_sensor_write_raw_hyst_rel_value(struct hid_sensor_common *st, int val1,
284 					int val2);
285 int hid_sensor_read_raw_hyst_value(struct hid_sensor_common *st,
286 					int *val1, int *val2);
287 int hid_sensor_read_raw_hyst_rel_value(struct hid_sensor_common *st,
288 				       int *val1, int *val2);
289 int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
290 					int val1, int val2);
291 int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
292 					int *val1, int *val2);
293 
294 int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
295 				u32 report_id, int field_index, u32 usage_id);
296 
297 int hid_sensor_format_scale(u32 usage_id,
298 			    struct hid_sensor_hub_attribute_info *attr_info,
299 			    int *val0, int *val1);
300 
301 s32 hid_sensor_read_poll_value(struct hid_sensor_common *st);
302 
303 int64_t hid_sensor_convert_timestamp(struct hid_sensor_common *st,
304 				     int64_t raw_value);
305 bool hid_sensor_batch_mode_supported(struct hid_sensor_common *st);
306 int hid_sensor_set_report_latency(struct hid_sensor_common *st, int latency);
307 int hid_sensor_get_report_latency(struct hid_sensor_common *st);
308 
309 #endif
310