hid-sensor-hub.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * HID Sensors Driver
  3. * Copyright (c) 2012, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. */
  19. #ifndef _HID_SENSORS_HUB_H
  20. #define _HID_SENSORS_HUB_H
  21. #include <linux/hid.h>
  22. #include <linux/hid-sensor-ids.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/iio/trigger.h>
  25. /**
  26. * struct hid_sensor_hub_attribute_info - Attribute info
  27. * @usage_id: Parent usage id of a physical device.
  28. * @attrib_id: Attribute id for this attribute.
  29. * @report_id: Report id in which this information resides.
  30. * @index: Field index in the report.
  31. * @units: Measurment unit for this attribute.
  32. * @unit_expo: Exponent used in the data.
  33. * @size: Size in bytes for data size.
  34. */
  35. struct hid_sensor_hub_attribute_info {
  36. u32 usage_id;
  37. u32 attrib_id;
  38. s32 report_id;
  39. s32 index;
  40. s32 units;
  41. s32 unit_expo;
  42. s32 size;
  43. s32 logical_minimum;
  44. s32 logical_maximum;
  45. };
  46. /**
  47. * struct sensor_hub_pending - Synchronous read pending information
  48. * @status: Pending status true/false.
  49. * @ready: Completion synchronization data.
  50. * @usage_id: Usage id for physical device, E.g. Gyro usage id.
  51. * @attr_usage_id: Usage Id of a field, E.g. X-AXIS for a gyro.
  52. * @raw_size: Response size for a read request.
  53. * @raw_data: Place holder for received response.
  54. */
  55. struct sensor_hub_pending {
  56. bool status;
  57. struct completion ready;
  58. u32 usage_id;
  59. u32 attr_usage_id;
  60. int raw_size;
  61. u8 *raw_data;
  62. };
  63. /**
  64. * struct hid_sensor_hub_device - Stores the hub instance data
  65. * @hdev: Stores the hid instance.
  66. * @vendor_id: Vendor id of hub device.
  67. * @product_id: Product id of hub device.
  68. * @usage: Usage id for this hub device instance.
  69. * @start_collection_index: Starting index for a phy type collection
  70. * @end_collection_index: Last index for a phy type collection
  71. * @mutex: synchronizing mutex.
  72. * @pending: Holds information of pending sync read request.
  73. */
  74. struct hid_sensor_hub_device {
  75. struct hid_device *hdev;
  76. u32 vendor_id;
  77. u32 product_id;
  78. u32 usage;
  79. int start_collection_index;
  80. int end_collection_index;
  81. struct mutex mutex;
  82. struct sensor_hub_pending pending;
  83. };
  84. /**
  85. * struct hid_sensor_hub_callbacks - Client callback functions
  86. * @pdev: Platform device instance of the client driver.
  87. * @suspend: Suspend callback.
  88. * @resume: Resume callback.
  89. * @capture_sample: Callback to get a sample.
  90. * @send_event: Send notification to indicate all samples are
  91. * captured, process and send event
  92. */
  93. struct hid_sensor_hub_callbacks {
  94. struct platform_device *pdev;
  95. int (*suspend)(struct hid_sensor_hub_device *hsdev, void *priv);
  96. int (*resume)(struct hid_sensor_hub_device *hsdev, void *priv);
  97. int (*capture_sample)(struct hid_sensor_hub_device *hsdev,
  98. u32 usage_id, size_t raw_len, char *raw_data,
  99. void *priv);
  100. int (*send_event)(struct hid_sensor_hub_device *hsdev, u32 usage_id,
  101. void *priv);
  102. };
  103. /**
  104. * sensor_hub_device_open() - Open hub device
  105. * @hsdev: Hub device instance.
  106. *
  107. * Used to open hid device for sensor hub.
  108. */
  109. int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev);
  110. /**
  111. * sensor_hub_device_clode() - Close hub device
  112. * @hsdev: Hub device instance.
  113. *
  114. * Used to clode hid device for sensor hub.
  115. */
  116. void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev);
  117. /* Registration functions */
  118. /**
  119. * sensor_hub_register_callback() - Register client callbacks
  120. * @hsdev: Hub device instance.
  121. * @usage_id: Usage id of the client (E.g. 0x200076 for Gyro).
  122. * @usage_callback: Callback function storage
  123. *
  124. * Used to register callbacks by client processing drivers. Sensor
  125. * hub core driver will call these callbacks to offload processing
  126. * of data streams and notifications.
  127. */
  128. int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
  129. u32 usage_id,
  130. struct hid_sensor_hub_callbacks *usage_callback);
  131. /**
  132. * sensor_hub_remove_callback() - Remove client callbacks
  133. * @hsdev: Hub device instance.
  134. * @usage_id: Usage id of the client (E.g. 0x200076 for Gyro).
  135. *
  136. * If there is a callback registred, this call will remove that
  137. * callbacks, so that it will stop data and event notifications.
  138. */
  139. int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
  140. u32 usage_id);
  141. /* Hid sensor hub core interfaces */
  142. /**
  143. * sensor_hub_input_get_attribute_info() - Get an attribute information
  144. * @hsdev: Hub device instance.
  145. * @type: Type of this attribute, input/output/feature
  146. * @usage_id: Attribute usage id of parent physical device as per spec
  147. * @attr_usage_id: Attribute usage id as per spec
  148. * @info: return information about attribute after parsing report
  149. *
  150. * Parses report and returns the attribute information such as report id,
  151. * field index, units and exponet etc.
  152. */
  153. int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
  154. u8 type,
  155. u32 usage_id, u32 attr_usage_id,
  156. struct hid_sensor_hub_attribute_info *info);
  157. /**
  158. * sensor_hub_input_attr_get_raw_value() - Attribute read request
  159. * @usage_id: Attribute usage id of parent physical device as per spec
  160. * @attr_usage_id: Attribute usage id as per spec
  161. * @report_id: Report id to look for
  162. * @flag: Synchronous or asynchronous read
  163. *
  164. * Issues a synchronous or asynchronous read request for an input attribute.
  165. * Returns data upto 32 bits.
  166. */
  167. enum sensor_hub_read_flags {
  168. SENSOR_HUB_SYNC,
  169. SENSOR_HUB_ASYNC,
  170. };
  171. int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
  172. u32 usage_id,
  173. u32 attr_usage_id, u32 report_id,
  174. enum sensor_hub_read_flags flag
  175. );
  176. /**
  177. * sensor_hub_set_feature() - Feature set request
  178. * @report_id: Report id to look for
  179. * @field_index: Field index inside a report
  180. * @value: Value to set
  181. *
  182. * Used to set a field in feature report. For example this can set polling
  183. * interval, sensitivity, activate/deactivate state.
  184. */
  185. int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
  186. u32 field_index, s32 value);
  187. /**
  188. * sensor_hub_get_feature() - Feature get request
  189. * @report_id: Report id to look for
  190. * @field_index: Field index inside a report
  191. * @value: Place holder for return value
  192. *
  193. * Used to get a field in feature report. For example this can get polling
  194. * interval, sensitivity, activate/deactivate state.
  195. */
  196. int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
  197. u32 field_index, s32 *value);
  198. /* hid-sensor-attributes */
  199. /* Common hid sensor iio structure */
  200. struct hid_sensor_common {
  201. struct hid_sensor_hub_device *hsdev;
  202. struct platform_device *pdev;
  203. unsigned usage_id;
  204. atomic_t data_ready;
  205. struct iio_trigger *trigger;
  206. struct hid_sensor_hub_attribute_info poll;
  207. struct hid_sensor_hub_attribute_info report_state;
  208. struct hid_sensor_hub_attribute_info power_state;
  209. struct hid_sensor_hub_attribute_info sensitivity;
  210. };
  211. /* Convert from hid unit expo to regular exponent */
  212. static inline int hid_sensor_convert_exponent(int unit_expo)
  213. {
  214. if (unit_expo < 0x08)
  215. return unit_expo;
  216. else if (unit_expo <= 0x0f)
  217. return -(0x0f-unit_expo+1);
  218. else
  219. return 0;
  220. }
  221. int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
  222. u32 usage_id,
  223. struct hid_sensor_common *st);
  224. int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
  225. int val1, int val2);
  226. int hid_sensor_read_raw_hyst_value(struct hid_sensor_common *st,
  227. int *val1, int *val2);
  228. int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
  229. int val1, int val2);
  230. int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
  231. int *val1, int *val2);
  232. int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
  233. u32 report_id, int field_index, u32 usage_id);
  234. int hid_sensor_format_scale(u32 usage_id,
  235. struct hid_sensor_hub_attribute_info *attr_info,
  236. int *val0, int *val1);
  237. s32 hid_sensor_read_poll_value(struct hid_sensor_common *st);
  238. #endif