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