hid-sensor-hub.h 9.0 KB

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