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 | #include <linux/device.h> |
| 20 | #include <linux/hid.h> |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/mfd/core.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/hid-sensor-ids.h> |
| 26 | #include <linux/hid-sensor-hub.h> |
| 27 | #include "hid-ids.h" |
| 28 | |
Srinivas Pandruvada | 875e36f | 2014-01-14 09:30:20 -0800 | [diff] [blame] | 29 | #define HID_SENSOR_HUB_ENUM_QUIRK 0x01 |
| 30 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 31 | /** |
| 32 | * struct sensor_hub_pending - Synchronous read pending information |
| 33 | * @status: Pending status true/false. |
| 34 | * @ready: Completion synchronization data. |
| 35 | * @usage_id: Usage id for physical device, E.g. Gyro usage id. |
| 36 | * @attr_usage_id: Usage Id of a field, E.g. X-AXIS for a gyro. |
| 37 | * @raw_size: Response size for a read request. |
| 38 | * @raw_data: Place holder for received response. |
| 39 | */ |
| 40 | struct sensor_hub_pending { |
| 41 | bool status; |
| 42 | struct completion ready; |
| 43 | u32 usage_id; |
| 44 | u32 attr_usage_id; |
| 45 | int raw_size; |
| 46 | u8 *raw_data; |
| 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * struct sensor_hub_data - Hold a instance data for a HID hub device |
| 51 | * @hsdev: Stored hid instance for current hub device. |
| 52 | * @mutex: Mutex to serialize synchronous request. |
| 53 | * @lock: Spin lock to protect pending request structure. |
| 54 | * @pending: Holds information of pending sync read request. |
| 55 | * @dyn_callback_list: Holds callback function |
| 56 | * @dyn_callback_lock: spin lock to protect callback list |
| 57 | * @hid_sensor_hub_client_devs: Stores all MFD cells for a hub instance. |
| 58 | * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached). |
| 59 | */ |
| 60 | struct sensor_hub_data { |
| 61 | struct hid_sensor_hub_device *hsdev; |
| 62 | struct mutex mutex; |
| 63 | spinlock_t lock; |
| 64 | struct sensor_hub_pending pending; |
| 65 | struct list_head dyn_callback_list; |
| 66 | spinlock_t dyn_callback_lock; |
| 67 | struct mfd_cell *hid_sensor_hub_client_devs; |
| 68 | int hid_sensor_client_cnt; |
Srinivas Pandruvada | 875e36f | 2014-01-14 09:30:20 -0800 | [diff] [blame] | 69 | unsigned long quirks; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | /** |
| 73 | * struct hid_sensor_hub_callbacks_list - Stores callback list |
| 74 | * @list: list head. |
| 75 | * @usage_id: usage id for a physical device. |
| 76 | * @usage_callback: Stores registered callback functions. |
| 77 | * @priv: Private data for a physical device. |
| 78 | */ |
| 79 | struct hid_sensor_hub_callbacks_list { |
| 80 | struct list_head list; |
| 81 | u32 usage_id; |
| 82 | struct hid_sensor_hub_callbacks *usage_callback; |
| 83 | void *priv; |
| 84 | }; |
| 85 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 86 | static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev, |
| 87 | int dir) |
| 88 | { |
| 89 | struct hid_report *report; |
| 90 | |
| 91 | list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) { |
| 92 | if (report->id == id) |
| 93 | return report; |
| 94 | } |
| 95 | hid_warn(hdev, "No report with id 0x%x found\n", id); |
| 96 | |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | static int sensor_hub_get_physical_device_count( |
| 101 | struct hid_report_enum *report_enum) |
| 102 | { |
| 103 | struct hid_report *report; |
| 104 | struct hid_field *field; |
| 105 | int cnt = 0; |
| 106 | |
| 107 | list_for_each_entry(report, &report_enum->report_list, list) { |
| 108 | field = report->field[0]; |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 109 | if (report->maxfield && field && field->physical) |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 110 | cnt++; |
| 111 | } |
| 112 | |
| 113 | return cnt; |
| 114 | } |
| 115 | |
| 116 | static void sensor_hub_fill_attr_info( |
| 117 | struct hid_sensor_hub_attribute_info *info, |
Srinivas Pandruvada | 9f740ff | 2013-11-27 22:19:00 +0000 | [diff] [blame] | 118 | s32 index, s32 report_id, struct hid_field *field) |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 119 | { |
| 120 | info->index = index; |
| 121 | info->report_id = report_id; |
Srinivas Pandruvada | 9f740ff | 2013-11-27 22:19:00 +0000 | [diff] [blame] | 122 | info->units = field->unit; |
| 123 | info->unit_expo = field->unit_exponent; |
| 124 | info->size = (field->report_size * field->report_count)/8; |
| 125 | info->logical_minimum = field->logical_minimum; |
| 126 | info->logical_maximum = field->logical_maximum; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static struct hid_sensor_hub_callbacks *sensor_hub_get_callback( |
| 130 | struct hid_device *hdev, |
| 131 | u32 usage_id, void **priv) |
| 132 | { |
| 133 | struct hid_sensor_hub_callbacks_list *callback; |
| 134 | struct sensor_hub_data *pdata = hid_get_drvdata(hdev); |
| 135 | |
| 136 | spin_lock(&pdata->dyn_callback_lock); |
| 137 | list_for_each_entry(callback, &pdata->dyn_callback_list, list) |
| 138 | if (callback->usage_id == usage_id) { |
| 139 | *priv = callback->priv; |
| 140 | spin_unlock(&pdata->dyn_callback_lock); |
| 141 | return callback->usage_callback; |
| 142 | } |
| 143 | spin_unlock(&pdata->dyn_callback_lock); |
| 144 | |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev, |
| 149 | u32 usage_id, |
| 150 | struct hid_sensor_hub_callbacks *usage_callback) |
| 151 | { |
| 152 | struct hid_sensor_hub_callbacks_list *callback; |
| 153 | struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev); |
| 154 | |
| 155 | spin_lock(&pdata->dyn_callback_lock); |
| 156 | list_for_each_entry(callback, &pdata->dyn_callback_list, list) |
| 157 | if (callback->usage_id == usage_id) { |
| 158 | spin_unlock(&pdata->dyn_callback_lock); |
| 159 | return -EINVAL; |
| 160 | } |
Dan Carpenter | 2b7c4b8 | 2012-09-14 06:53:23 +0000 | [diff] [blame] | 161 | callback = kzalloc(sizeof(*callback), GFP_ATOMIC); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 162 | if (!callback) { |
| 163 | spin_unlock(&pdata->dyn_callback_lock); |
| 164 | return -ENOMEM; |
| 165 | } |
| 166 | callback->usage_callback = usage_callback; |
| 167 | callback->usage_id = usage_id; |
| 168 | callback->priv = NULL; |
| 169 | list_add_tail(&callback->list, &pdata->dyn_callback_list); |
| 170 | spin_unlock(&pdata->dyn_callback_lock); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(sensor_hub_register_callback); |
| 175 | |
| 176 | int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev, |
| 177 | u32 usage_id) |
| 178 | { |
| 179 | struct hid_sensor_hub_callbacks_list *callback; |
| 180 | struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev); |
| 181 | |
| 182 | spin_lock(&pdata->dyn_callback_lock); |
| 183 | list_for_each_entry(callback, &pdata->dyn_callback_list, list) |
| 184 | if (callback->usage_id == usage_id) { |
| 185 | list_del(&callback->list); |
| 186 | kfree(callback); |
| 187 | break; |
| 188 | } |
| 189 | spin_unlock(&pdata->dyn_callback_lock); |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | EXPORT_SYMBOL_GPL(sensor_hub_remove_callback); |
| 194 | |
| 195 | int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id, |
| 196 | u32 field_index, s32 value) |
| 197 | { |
| 198 | struct hid_report *report; |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 199 | struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 200 | int ret = 0; |
| 201 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 202 | mutex_lock(&data->mutex); |
| 203 | report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT); |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 204 | if (!report || (field_index >= report->maxfield)) { |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 205 | ret = -EINVAL; |
| 206 | goto done_proc; |
| 207 | } |
| 208 | hid_set_field(report->field[field_index], 0, value); |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 209 | hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT); |
Benjamin Tissoires | b7966a4 | 2013-02-25 11:31:47 +0100 | [diff] [blame] | 210 | hid_hw_wait(hsdev->hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 211 | |
| 212 | done_proc: |
| 213 | mutex_unlock(&data->mutex); |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | EXPORT_SYMBOL_GPL(sensor_hub_set_feature); |
| 218 | |
| 219 | int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id, |
| 220 | u32 field_index, s32 *value) |
| 221 | { |
| 222 | struct hid_report *report; |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 223 | struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 224 | int ret = 0; |
| 225 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 226 | mutex_lock(&data->mutex); |
| 227 | report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT); |
Jiri Kosina | 4e5a494 | 2013-09-06 11:59:53 +0200 | [diff] [blame] | 228 | if (!report || (field_index >= report->maxfield) || |
Kees Cook | 9e89102 | 2013-08-28 22:31:44 +0200 | [diff] [blame] | 229 | report->field[field_index]->report_count < 1) { |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 230 | ret = -EINVAL; |
| 231 | goto done_proc; |
| 232 | } |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 233 | hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT); |
Benjamin Tissoires | b7966a4 | 2013-02-25 11:31:47 +0100 | [diff] [blame] | 234 | hid_hw_wait(hsdev->hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 235 | *value = report->field[field_index]->value[0]; |
| 236 | |
| 237 | done_proc: |
| 238 | mutex_unlock(&data->mutex); |
| 239 | |
| 240 | return ret; |
| 241 | } |
| 242 | EXPORT_SYMBOL_GPL(sensor_hub_get_feature); |
| 243 | |
| 244 | |
| 245 | int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev, |
| 246 | u32 usage_id, |
| 247 | u32 attr_usage_id, u32 report_id) |
| 248 | { |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 249 | struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 250 | unsigned long flags; |
| 251 | struct hid_report *report; |
| 252 | int ret_val = 0; |
| 253 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 254 | mutex_lock(&data->mutex); |
| 255 | memset(&data->pending, 0, sizeof(data->pending)); |
| 256 | init_completion(&data->pending.ready); |
| 257 | data->pending.usage_id = usage_id; |
| 258 | data->pending.attr_usage_id = attr_usage_id; |
| 259 | data->pending.raw_size = 0; |
| 260 | |
| 261 | spin_lock_irqsave(&data->lock, flags); |
| 262 | data->pending.status = true; |
| 263 | report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT); |
| 264 | if (!report) { |
| 265 | spin_unlock_irqrestore(&data->lock, flags); |
| 266 | goto err_free; |
| 267 | } |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 268 | hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 269 | spin_unlock_irqrestore(&data->lock, flags); |
| 270 | wait_for_completion_interruptible_timeout(&data->pending.ready, HZ*5); |
| 271 | switch (data->pending.raw_size) { |
| 272 | case 1: |
| 273 | ret_val = *(u8 *)data->pending.raw_data; |
| 274 | break; |
| 275 | case 2: |
| 276 | ret_val = *(u16 *)data->pending.raw_data; |
| 277 | break; |
| 278 | case 4: |
| 279 | ret_val = *(u32 *)data->pending.raw_data; |
| 280 | break; |
| 281 | default: |
| 282 | ret_val = 0; |
| 283 | } |
| 284 | kfree(data->pending.raw_data); |
| 285 | |
| 286 | err_free: |
| 287 | data->pending.status = false; |
| 288 | mutex_unlock(&data->mutex); |
| 289 | |
| 290 | return ret_val; |
| 291 | } |
| 292 | EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value); |
| 293 | |
| 294 | int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev, |
| 295 | u8 type, |
| 296 | u32 usage_id, |
| 297 | u32 attr_usage_id, |
| 298 | struct hid_sensor_hub_attribute_info *info) |
| 299 | { |
| 300 | int ret = -1; |
| 301 | int i, j; |
| 302 | int collection_index = -1; |
| 303 | struct hid_report *report; |
| 304 | struct hid_field *field; |
| 305 | struct hid_report_enum *report_enum; |
| 306 | struct hid_device *hdev = hsdev->hdev; |
| 307 | |
| 308 | /* Initialize with defaults */ |
| 309 | info->usage_id = usage_id; |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 310 | info->attrib_id = attr_usage_id; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 311 | info->report_id = -1; |
| 312 | info->index = -1; |
| 313 | info->units = -1; |
| 314 | info->unit_expo = -1; |
| 315 | |
| 316 | for (i = 0; i < hdev->maxcollection; ++i) { |
| 317 | struct hid_collection *collection = &hdev->collection[i]; |
| 318 | if (usage_id == collection->usage) { |
| 319 | collection_index = i; |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | if (collection_index == -1) |
| 324 | goto err_ret; |
| 325 | |
| 326 | report_enum = &hdev->report_enum[type]; |
| 327 | list_for_each_entry(report, &report_enum->report_list, list) { |
| 328 | for (i = 0; i < report->maxfield; ++i) { |
| 329 | field = report->field[i]; |
| 330 | if (field->physical == usage_id && |
| 331 | field->logical == attr_usage_id) { |
| 332 | sensor_hub_fill_attr_info(info, i, report->id, |
Srinivas Pandruvada | 9f740ff | 2013-11-27 22:19:00 +0000 | [diff] [blame] | 333 | field); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 334 | ret = 0; |
| 335 | } else { |
| 336 | for (j = 0; j < field->maxusage; ++j) { |
| 337 | if (field->usage[j].hid == |
| 338 | attr_usage_id && |
| 339 | field->usage[j].collection_index == |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 340 | collection_index) { |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 341 | sensor_hub_fill_attr_info(info, |
Srinivas Pandruvada | 9f740ff | 2013-11-27 22:19:00 +0000 | [diff] [blame] | 342 | i, report->id, field); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 343 | ret = 0; |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | if (ret == 0) |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | err_ret: |
| 354 | return ret; |
| 355 | } |
| 356 | EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info); |
| 357 | |
| 358 | #ifdef CONFIG_PM |
| 359 | static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message) |
| 360 | { |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 361 | struct sensor_hub_data *pdata = hid_get_drvdata(hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 362 | struct hid_sensor_hub_callbacks_list *callback; |
| 363 | |
| 364 | hid_dbg(hdev, " sensor_hub_suspend\n"); |
| 365 | spin_lock(&pdata->dyn_callback_lock); |
| 366 | list_for_each_entry(callback, &pdata->dyn_callback_list, list) { |
| 367 | if (callback->usage_callback->suspend) |
| 368 | callback->usage_callback->suspend( |
| 369 | pdata->hsdev, callback->priv); |
| 370 | } |
| 371 | spin_unlock(&pdata->dyn_callback_lock); |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static int sensor_hub_resume(struct hid_device *hdev) |
| 377 | { |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 378 | struct sensor_hub_data *pdata = hid_get_drvdata(hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 379 | struct hid_sensor_hub_callbacks_list *callback; |
| 380 | |
| 381 | hid_dbg(hdev, " sensor_hub_resume\n"); |
| 382 | spin_lock(&pdata->dyn_callback_lock); |
| 383 | list_for_each_entry(callback, &pdata->dyn_callback_list, list) { |
| 384 | if (callback->usage_callback->resume) |
| 385 | callback->usage_callback->resume( |
| 386 | pdata->hsdev, callback->priv); |
| 387 | } |
| 388 | spin_unlock(&pdata->dyn_callback_lock); |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static int sensor_hub_reset_resume(struct hid_device *hdev) |
| 394 | { |
| 395 | return 0; |
| 396 | } |
| 397 | #endif |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 398 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 399 | /* |
| 400 | * Handle raw report as sent by device |
| 401 | */ |
| 402 | static int sensor_hub_raw_event(struct hid_device *hdev, |
| 403 | struct hid_report *report, u8 *raw_data, int size) |
| 404 | { |
| 405 | int i; |
| 406 | u8 *ptr; |
| 407 | int sz; |
| 408 | struct sensor_hub_data *pdata = hid_get_drvdata(hdev); |
| 409 | unsigned long flags; |
| 410 | struct hid_sensor_hub_callbacks *callback = NULL; |
| 411 | struct hid_collection *collection = NULL; |
| 412 | void *priv = NULL; |
| 413 | |
| 414 | hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n", |
| 415 | report->id, size, report->type); |
| 416 | hid_dbg(hdev, "maxfield:%d\n", report->maxfield); |
| 417 | if (report->type != HID_INPUT_REPORT) |
| 418 | return 1; |
| 419 | |
| 420 | ptr = raw_data; |
Andy Shevchenko | 15261f6 | 2013-08-14 11:07:08 +0300 | [diff] [blame] | 421 | ptr++; /* Skip report id */ |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 422 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 423 | spin_lock_irqsave(&pdata->lock, flags); |
| 424 | |
| 425 | for (i = 0; i < report->maxfield; ++i) { |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 426 | hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n", |
| 427 | i, report->field[i]->usage->collection_index, |
| 428 | report->field[i]->usage->hid, |
Srinivas Pandruvada | d4b1bba | 2013-10-26 10:04:09 -0700 | [diff] [blame] | 429 | (report->field[i]->report_size * |
| 430 | report->field[i]->report_count)/8); |
| 431 | sz = (report->field[i]->report_size * |
| 432 | report->field[i]->report_count)/8; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 433 | if (pdata->pending.status && pdata->pending.attr_usage_id == |
| 434 | report->field[i]->usage->hid) { |
| 435 | hid_dbg(hdev, "data was pending ...\n"); |
Andy Shevchenko | 7b0692f | 2013-08-14 11:07:11 +0300 | [diff] [blame] | 436 | pdata->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC); |
| 437 | if (pdata->pending.raw_data) |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 438 | pdata->pending.raw_size = sz; |
Andy Shevchenko | 7b0692f | 2013-08-14 11:07:11 +0300 | [diff] [blame] | 439 | else |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 440 | pdata->pending.raw_size = 0; |
| 441 | complete(&pdata->pending.ready); |
| 442 | } |
| 443 | collection = &hdev->collection[ |
| 444 | report->field[i]->usage->collection_index]; |
| 445 | hid_dbg(hdev, "collection->usage %x\n", |
| 446 | collection->usage); |
| 447 | callback = sensor_hub_get_callback(pdata->hsdev->hdev, |
| 448 | report->field[i]->physical, |
| 449 | &priv); |
| 450 | if (callback && callback->capture_sample) { |
| 451 | if (report->field[i]->logical) |
| 452 | callback->capture_sample(pdata->hsdev, |
| 453 | report->field[i]->logical, sz, ptr, |
| 454 | callback->pdev); |
| 455 | else |
| 456 | callback->capture_sample(pdata->hsdev, |
| 457 | report->field[i]->usage->hid, sz, ptr, |
| 458 | callback->pdev); |
| 459 | } |
| 460 | ptr += sz; |
| 461 | } |
| 462 | if (callback && collection && callback->send_event) |
| 463 | callback->send_event(pdata->hsdev, collection->usage, |
| 464 | callback->pdev); |
| 465 | spin_unlock_irqrestore(&pdata->lock, flags); |
| 466 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 467 | return 1; |
| 468 | } |
| 469 | |
Srinivas Pandruvada | 1df3a40 | 2013-09-18 18:13:00 +0100 | [diff] [blame] | 470 | int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev) |
| 471 | { |
| 472 | int ret = 0; |
| 473 | struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); |
| 474 | |
| 475 | mutex_lock(&data->mutex); |
| 476 | if (!hsdev->ref_cnt) { |
| 477 | ret = hid_hw_open(hsdev->hdev); |
| 478 | if (ret) { |
| 479 | hid_err(hsdev->hdev, "failed to open hid device\n"); |
| 480 | mutex_unlock(&data->mutex); |
| 481 | return ret; |
| 482 | } |
| 483 | } |
| 484 | hsdev->ref_cnt++; |
| 485 | mutex_unlock(&data->mutex); |
| 486 | |
| 487 | return ret; |
| 488 | } |
| 489 | EXPORT_SYMBOL_GPL(sensor_hub_device_open); |
| 490 | |
| 491 | void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev) |
| 492 | { |
| 493 | struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); |
| 494 | |
| 495 | mutex_lock(&data->mutex); |
| 496 | hsdev->ref_cnt--; |
| 497 | if (!hsdev->ref_cnt) |
| 498 | hid_hw_close(hsdev->hdev); |
| 499 | mutex_unlock(&data->mutex); |
| 500 | } |
| 501 | EXPORT_SYMBOL_GPL(sensor_hub_device_close); |
| 502 | |
Srinivas Pandruvada | 875e36f | 2014-01-14 09:30:20 -0800 | [diff] [blame] | 503 | static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc, |
| 504 | unsigned int *rsize) |
| 505 | { |
| 506 | int index; |
| 507 | struct sensor_hub_data *sd = hid_get_drvdata(hdev); |
| 508 | unsigned char report_block[] = { |
| 509 | 0x0a, 0x16, 0x03, 0x15, 0x00, 0x25, 0x05}; |
| 510 | unsigned char power_block[] = { |
| 511 | 0x0a, 0x19, 0x03, 0x15, 0x00, 0x25, 0x05}; |
| 512 | |
| 513 | if (!(sd->quirks & HID_SENSOR_HUB_ENUM_QUIRK)) { |
| 514 | hid_dbg(hdev, "No Enum quirks\n"); |
| 515 | return rdesc; |
| 516 | } |
| 517 | |
| 518 | /* Looks for power and report state usage id and force to 1 */ |
| 519 | for (index = 0; index < *rsize; ++index) { |
| 520 | if (((*rsize - index) > sizeof(report_block)) && |
| 521 | !memcmp(&rdesc[index], report_block, |
| 522 | sizeof(report_block))) { |
| 523 | rdesc[index + 4] = 0x01; |
| 524 | index += sizeof(report_block); |
| 525 | } |
| 526 | if (((*rsize - index) > sizeof(power_block)) && |
| 527 | !memcmp(&rdesc[index], power_block, |
| 528 | sizeof(power_block))) { |
| 529 | rdesc[index + 4] = 0x01; |
| 530 | index += sizeof(power_block); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return rdesc; |
| 535 | } |
| 536 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 537 | static int sensor_hub_probe(struct hid_device *hdev, |
| 538 | const struct hid_device_id *id) |
| 539 | { |
| 540 | int ret; |
| 541 | struct sensor_hub_data *sd; |
| 542 | int i; |
| 543 | char *name; |
| 544 | struct hid_report *report; |
| 545 | struct hid_report_enum *report_enum; |
| 546 | struct hid_field *field; |
| 547 | int dev_cnt; |
| 548 | |
Andy Shevchenko | 905cc19 | 2013-08-14 11:07:10 +0300 | [diff] [blame] | 549 | sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 550 | if (!sd) { |
| 551 | hid_err(hdev, "cannot allocate Sensor data\n"); |
| 552 | return -ENOMEM; |
| 553 | } |
Andy Shevchenko | 905cc19 | 2013-08-14 11:07:10 +0300 | [diff] [blame] | 554 | sd->hsdev = devm_kzalloc(&hdev->dev, sizeof(*sd->hsdev), GFP_KERNEL); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 555 | if (!sd->hsdev) { |
| 556 | hid_err(hdev, "cannot allocate hid_sensor_hub_device\n"); |
Andy Shevchenko | 905cc19 | 2013-08-14 11:07:10 +0300 | [diff] [blame] | 557 | return -ENOMEM; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 558 | } |
| 559 | hid_set_drvdata(hdev, sd); |
Srinivas Pandruvada | 875e36f | 2014-01-14 09:30:20 -0800 | [diff] [blame] | 560 | sd->quirks = id->driver_data; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 561 | sd->hsdev->hdev = hdev; |
| 562 | sd->hsdev->vendor_id = hdev->vendor; |
| 563 | sd->hsdev->product_id = hdev->product; |
| 564 | spin_lock_init(&sd->lock); |
| 565 | spin_lock_init(&sd->dyn_callback_lock); |
| 566 | mutex_init(&sd->mutex); |
| 567 | ret = hid_parse(hdev); |
| 568 | if (ret) { |
| 569 | hid_err(hdev, "parse failed\n"); |
Andy Shevchenko | 905cc19 | 2013-08-14 11:07:10 +0300 | [diff] [blame] | 570 | return ret; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 571 | } |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 572 | INIT_LIST_HEAD(&hdev->inputs); |
| 573 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 574 | ret = hid_hw_start(hdev, 0); |
| 575 | if (ret) { |
| 576 | hid_err(hdev, "hw start failed\n"); |
Andy Shevchenko | 905cc19 | 2013-08-14 11:07:10 +0300 | [diff] [blame] | 577 | return ret; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 578 | } |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 579 | INIT_LIST_HEAD(&sd->dyn_callback_list); |
| 580 | sd->hid_sensor_client_cnt = 0; |
| 581 | report_enum = &hdev->report_enum[HID_INPUT_REPORT]; |
| 582 | |
| 583 | dev_cnt = sensor_hub_get_physical_device_count(report_enum); |
| 584 | if (dev_cnt > HID_MAX_PHY_DEVICES) { |
| 585 | hid_err(hdev, "Invalid Physical device count\n"); |
| 586 | ret = -EINVAL; |
Srinivas Pandruvada | 1df3a40 | 2013-09-18 18:13:00 +0100 | [diff] [blame] | 587 | goto err_stop_hw; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 588 | } |
| 589 | sd->hid_sensor_hub_client_devs = kzalloc(dev_cnt * |
| 590 | sizeof(struct mfd_cell), |
| 591 | GFP_KERNEL); |
| 592 | if (sd->hid_sensor_hub_client_devs == NULL) { |
Axel Lin | f2f13a6 | 2012-09-19 16:30:00 +0100 | [diff] [blame] | 593 | hid_err(hdev, "Failed to allocate memory for mfd cells\n"); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 594 | ret = -ENOMEM; |
Srinivas Pandruvada | 1df3a40 | 2013-09-18 18:13:00 +0100 | [diff] [blame] | 595 | goto err_stop_hw; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 596 | } |
| 597 | list_for_each_entry(report, &report_enum->report_list, list) { |
| 598 | hid_dbg(hdev, "Report id:%x\n", report->id); |
| 599 | field = report->field[0]; |
| 600 | if (report->maxfield && field && |
| 601 | field->physical) { |
| 602 | name = kasprintf(GFP_KERNEL, "HID-SENSOR-%x", |
| 603 | field->physical); |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 604 | if (name == NULL) { |
Axel Lin | f2f13a6 | 2012-09-19 16:30:00 +0100 | [diff] [blame] | 605 | hid_err(hdev, "Failed MFD device name\n"); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 606 | ret = -ENOMEM; |
Axel Lin | f2f13a6 | 2012-09-19 16:30:00 +0100 | [diff] [blame] | 607 | goto err_free_names; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 608 | } |
| 609 | sd->hid_sensor_hub_client_devs[ |
Srinivas Pandruvada | d81cae8 | 2013-12-05 16:34:25 -0800 | [diff] [blame] | 610 | sd->hid_sensor_client_cnt].id = PLATFORM_DEVID_AUTO; |
| 611 | sd->hid_sensor_hub_client_devs[ |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 612 | sd->hid_sensor_client_cnt].name = name; |
| 613 | sd->hid_sensor_hub_client_devs[ |
| 614 | sd->hid_sensor_client_cnt].platform_data = |
| 615 | sd->hsdev; |
| 616 | sd->hid_sensor_hub_client_devs[ |
| 617 | sd->hid_sensor_client_cnt].pdata_size = |
| 618 | sizeof(*sd->hsdev); |
| 619 | hid_dbg(hdev, "Adding %s:%p\n", name, sd); |
| 620 | sd->hid_sensor_client_cnt++; |
| 621 | } |
| 622 | } |
| 623 | ret = mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs, |
Stephen Rothwell | f45c69b | 2012-09-12 07:07:00 +0100 | [diff] [blame] | 624 | sd->hid_sensor_client_cnt, NULL, 0, NULL); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 625 | if (ret < 0) |
| 626 | goto err_free_names; |
| 627 | |
| 628 | return ret; |
| 629 | |
| 630 | err_free_names: |
| 631 | for (i = 0; i < sd->hid_sensor_client_cnt ; ++i) |
| 632 | kfree(sd->hid_sensor_hub_client_devs[i].name); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 633 | kfree(sd->hid_sensor_hub_client_devs); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 634 | err_stop_hw: |
| 635 | hid_hw_stop(hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 636 | |
| 637 | return ret; |
| 638 | } |
| 639 | |
| 640 | static void sensor_hub_remove(struct hid_device *hdev) |
| 641 | { |
| 642 | struct sensor_hub_data *data = hid_get_drvdata(hdev); |
| 643 | unsigned long flags; |
| 644 | int i; |
| 645 | |
| 646 | hid_dbg(hdev, " hardware removed\n"); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 647 | hid_hw_close(hdev); |
Axel Lin | f2f13a6 | 2012-09-19 16:30:00 +0100 | [diff] [blame] | 648 | hid_hw_stop(hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 649 | spin_lock_irqsave(&data->lock, flags); |
| 650 | if (data->pending.status) |
| 651 | complete(&data->pending.ready); |
| 652 | spin_unlock_irqrestore(&data->lock, flags); |
| 653 | mfd_remove_devices(&hdev->dev); |
| 654 | for (i = 0; i < data->hid_sensor_client_cnt ; ++i) |
| 655 | kfree(data->hid_sensor_hub_client_devs[i].name); |
| 656 | kfree(data->hid_sensor_hub_client_devs); |
| 657 | hid_set_drvdata(hdev, NULL); |
| 658 | mutex_destroy(&data->mutex); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | static const struct hid_device_id sensor_hub_devices[] = { |
Srinivas Pandruvada | 875e36f | 2014-01-14 09:30:20 -0800 | [diff] [blame] | 662 | { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_0, |
| 663 | USB_DEVICE_ID_INTEL_HID_SENSOR), |
| 664 | .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, |
| 665 | { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1, |
| 666 | USB_DEVICE_ID_INTEL_HID_SENSOR), |
| 667 | .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, |
Archana Patni | 218eb9e | 2014-02-03 12:44:16 +0530 | [diff] [blame] | 668 | { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0, |
| 669 | USB_DEVICE_ID_STM_HID_SENSOR), |
| 670 | .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, |
Mika Westerberg | 0d1e4b0 | 2013-02-11 12:31:19 +0200 | [diff] [blame] | 671 | { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID, |
| 672 | HID_ANY_ID) }, |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 673 | { } |
| 674 | }; |
| 675 | MODULE_DEVICE_TABLE(hid, sensor_hub_devices); |
| 676 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 677 | static struct hid_driver sensor_hub_driver = { |
| 678 | .name = "hid-sensor-hub", |
| 679 | .id_table = sensor_hub_devices, |
| 680 | .probe = sensor_hub_probe, |
| 681 | .remove = sensor_hub_remove, |
| 682 | .raw_event = sensor_hub_raw_event, |
Srinivas Pandruvada | 875e36f | 2014-01-14 09:30:20 -0800 | [diff] [blame] | 683 | .report_fixup = sensor_hub_report_fixup, |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 684 | #ifdef CONFIG_PM |
| 685 | .suspend = sensor_hub_suspend, |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 686 | .resume = sensor_hub_resume, |
| 687 | .reset_resume = sensor_hub_reset_resume, |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 688 | #endif |
| 689 | }; |
H Hartley Sweeten | f425458 | 2012-12-17 15:28:26 -0700 | [diff] [blame] | 690 | module_hid_driver(sensor_hub_driver); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 691 | |
| 692 | MODULE_DESCRIPTION("HID Sensor Hub driver"); |
| 693 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); |
| 694 | MODULE_LICENSE("GPL"); |