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