hid-sensor-hub.h 8.7 KB

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