hid-sensor-hub.h 7.0 KB

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