hid-sensor-hub.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 hid_sensor_hub_device - Stores the hub instance data
  48. * @hdev: Stores the hid instance.
  49. * @vendor_id: Vendor id of hub device.
  50. * @product_id: Product id of hub device.
  51. * @start_collection_index: Starting index for a phy type collection
  52. * @end_collection_index: Last index for a phy type collection
  53. */
  54. struct hid_sensor_hub_device {
  55. struct hid_device *hdev;
  56. u32 vendor_id;
  57. u32 product_id;
  58. int start_collection_index;
  59. int end_collection_index;
  60. };
  61. /**
  62. * struct hid_sensor_hub_callbacks - Client callback functions
  63. * @pdev: Platform device instance of the client driver.
  64. * @suspend: Suspend callback.
  65. * @resume: Resume callback.
  66. * @capture_sample: Callback to get a sample.
  67. * @send_event: Send notification to indicate all samples are
  68. * captured, process and send event
  69. */
  70. struct hid_sensor_hub_callbacks {
  71. struct platform_device *pdev;
  72. int (*suspend)(struct hid_sensor_hub_device *hsdev, void *priv);
  73. int (*resume)(struct hid_sensor_hub_device *hsdev, void *priv);
  74. int (*capture_sample)(struct hid_sensor_hub_device *hsdev,
  75. u32 usage_id, size_t raw_len, char *raw_data,
  76. void *priv);
  77. int (*send_event)(struct hid_sensor_hub_device *hsdev, u32 usage_id,
  78. void *priv);
  79. };
  80. /**
  81. * sensor_hub_device_open() - Open hub device
  82. * @hsdev: Hub device instance.
  83. *
  84. * Used to open hid device for sensor hub.
  85. */
  86. int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev);
  87. /**
  88. * sensor_hub_device_clode() - Close hub device
  89. * @hsdev: Hub device instance.
  90. *
  91. * Used to clode hid device for sensor hub.
  92. */
  93. void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev);
  94. /* Registration functions */
  95. /**
  96. * sensor_hub_register_callback() - Register client callbacks
  97. * @hsdev: Hub device instance.
  98. * @usage_id: Usage id of the client (E.g. 0x200076 for Gyro).
  99. * @usage_callback: Callback function storage
  100. *
  101. * Used to register callbacks by client processing drivers. Sensor
  102. * hub core driver will call these callbacks to offload processing
  103. * of data streams and notifications.
  104. */
  105. int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
  106. u32 usage_id,
  107. struct hid_sensor_hub_callbacks *usage_callback);
  108. /**
  109. * sensor_hub_remove_callback() - Remove client callbacks
  110. * @hsdev: Hub device instance.
  111. * @usage_id: Usage id of the client (E.g. 0x200076 for Gyro).
  112. *
  113. * If there is a callback registred, this call will remove that
  114. * callbacks, so that it will stop data and event notifications.
  115. */
  116. int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
  117. u32 usage_id);
  118. /* Hid sensor hub core interfaces */
  119. /**
  120. * sensor_hub_input_get_attribute_info() - Get an attribute information
  121. * @hsdev: Hub device instance.
  122. * @type: Type of this attribute, input/output/feature
  123. * @usage_id: Attribute usage id of parent physical device as per spec
  124. * @attr_usage_id: Attribute usage id as per spec
  125. * @info: return information about attribute after parsing report
  126. *
  127. * Parses report and returns the attribute information such as report id,
  128. * field index, units and exponet etc.
  129. */
  130. int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
  131. u8 type,
  132. u32 usage_id, u32 attr_usage_id,
  133. struct hid_sensor_hub_attribute_info *info);
  134. /**
  135. * sensor_hub_input_attr_get_raw_value() - Synchronous read request
  136. * @usage_id: Attribute usage id of parent physical device as per spec
  137. * @attr_usage_id: Attribute usage id as per spec
  138. * @report_id: Report id to look for
  139. *
  140. * Issues a synchronous read request for an input attribute. Returns
  141. * data upto 32 bits. Since client can get events, so this call should
  142. * not be used for data paths, this will impact performance.
  143. */
  144. int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
  145. u32 usage_id,
  146. u32 attr_usage_id, u32 report_id);
  147. /**
  148. * sensor_hub_set_feature() - Feature set request
  149. * @report_id: Report id to look for
  150. * @field_index: Field index inside a report
  151. * @value: Value to set
  152. *
  153. * Used to set a field in feature report. For example this can set polling
  154. * interval, sensitivity, activate/deactivate state.
  155. */
  156. int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
  157. u32 field_index, s32 value);
  158. /**
  159. * sensor_hub_get_feature() - Feature get request
  160. * @report_id: Report id to look for
  161. * @field_index: Field index inside a report
  162. * @value: Place holder for return value
  163. *
  164. * Used to get a field in feature report. For example this can get polling
  165. * interval, sensitivity, activate/deactivate state.
  166. */
  167. int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
  168. u32 field_index, s32 *value);
  169. /* hid-sensor-attributes */
  170. /* Common hid sensor iio structure */
  171. struct hid_sensor_common {
  172. struct hid_sensor_hub_device *hsdev;
  173. struct platform_device *pdev;
  174. unsigned usage_id;
  175. atomic_t data_ready;
  176. struct iio_trigger *trigger;
  177. struct hid_sensor_hub_attribute_info poll;
  178. struct hid_sensor_hub_attribute_info report_state;
  179. struct hid_sensor_hub_attribute_info power_state;
  180. struct hid_sensor_hub_attribute_info sensitivity;
  181. };
  182. /* Convert from hid unit expo to regular exponent */
  183. static inline int hid_sensor_convert_exponent(int unit_expo)
  184. {
  185. if (unit_expo < 0x08)
  186. return unit_expo;
  187. else if (unit_expo <= 0x0f)
  188. return -(0x0f-unit_expo+1);
  189. else
  190. return 0;
  191. }
  192. int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
  193. u32 usage_id,
  194. struct hid_sensor_common *st);
  195. int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
  196. int val1, int val2);
  197. int hid_sensor_read_raw_hyst_value(struct hid_sensor_common *st,
  198. int *val1, int *val2);
  199. int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
  200. int val1, int val2);
  201. int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
  202. int *val1, int *val2);
  203. int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
  204. u32 report_id, int field_index, u32 usage_id);
  205. int hid_sensor_format_scale(u32 usage_id,
  206. struct hid_sensor_hub_attribute_info *attr_info,
  207. int *val0, int *val1);
  208. s32 hid_sensor_read_poll_value(struct hid_sensor_common *st);
  209. #endif