hid-sensor-hub.h 6.9 KB

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