1401ca24fSsrinivas pandruvada /* 2401ca24fSsrinivas pandruvada * HID Sensors Driver 3401ca24fSsrinivas pandruvada * Copyright (c) 2012, Intel Corporation. 4401ca24fSsrinivas pandruvada * 5401ca24fSsrinivas pandruvada * This program is free software; you can redistribute it and/or modify it 6401ca24fSsrinivas pandruvada * under the terms and conditions of the GNU General Public License, 7401ca24fSsrinivas pandruvada * version 2, as published by the Free Software Foundation. 8401ca24fSsrinivas pandruvada * 9401ca24fSsrinivas pandruvada * This program is distributed in the hope it will be useful, but WITHOUT 10401ca24fSsrinivas pandruvada * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11401ca24fSsrinivas pandruvada * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12401ca24fSsrinivas pandruvada * more details. 13401ca24fSsrinivas pandruvada * 14401ca24fSsrinivas pandruvada * You should have received a copy of the GNU General Public License along with 15401ca24fSsrinivas pandruvada * this program; if not, write to the Free Software Foundation, Inc., 16401ca24fSsrinivas pandruvada * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 17401ca24fSsrinivas pandruvada * 18401ca24fSsrinivas pandruvada */ 19401ca24fSsrinivas pandruvada #include <linux/device.h> 20401ca24fSsrinivas pandruvada #include <linux/hid.h> 21401ca24fSsrinivas pandruvada #include <linux/module.h> 22401ca24fSsrinivas pandruvada #include <linux/slab.h> 23401ca24fSsrinivas pandruvada #include <linux/mfd/core.h> 24401ca24fSsrinivas pandruvada #include <linux/list.h> 25401ca24fSsrinivas pandruvada #include <linux/hid-sensor-ids.h> 26401ca24fSsrinivas pandruvada #include <linux/hid-sensor-hub.h> 27401ca24fSsrinivas pandruvada #include "hid-ids.h" 28401ca24fSsrinivas pandruvada 29875e36f8SSrinivas Pandruvada #define HID_SENSOR_HUB_ENUM_QUIRK 0x01 30875e36f8SSrinivas Pandruvada 31401ca24fSsrinivas pandruvada /** 32401ca24fSsrinivas pandruvada * struct sensor_hub_data - Hold a instance data for a HID hub device 33401ca24fSsrinivas pandruvada * @hsdev: Stored hid instance for current hub device. 34401ca24fSsrinivas pandruvada * @mutex: Mutex to serialize synchronous request. 35401ca24fSsrinivas pandruvada * @lock: Spin lock to protect pending request structure. 36401ca24fSsrinivas pandruvada * @dyn_callback_list: Holds callback function 37401ca24fSsrinivas pandruvada * @dyn_callback_lock: spin lock to protect callback list 38401ca24fSsrinivas pandruvada * @hid_sensor_hub_client_devs: Stores all MFD cells for a hub instance. 39401ca24fSsrinivas pandruvada * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached). 40ca2ed12fSSrinivas Pandruvada * @ref_cnt: Number of MFD clients have opened this device 41401ca24fSsrinivas pandruvada */ 42401ca24fSsrinivas pandruvada struct sensor_hub_data { 43401ca24fSsrinivas pandruvada struct mutex mutex; 44401ca24fSsrinivas pandruvada spinlock_t lock; 45401ca24fSsrinivas pandruvada struct list_head dyn_callback_list; 46401ca24fSsrinivas pandruvada spinlock_t dyn_callback_lock; 47401ca24fSsrinivas pandruvada struct mfd_cell *hid_sensor_hub_client_devs; 48401ca24fSsrinivas pandruvada int hid_sensor_client_cnt; 49875e36f8SSrinivas Pandruvada unsigned long quirks; 50ca2ed12fSSrinivas Pandruvada int ref_cnt; 51401ca24fSsrinivas pandruvada }; 52401ca24fSsrinivas pandruvada 53401ca24fSsrinivas pandruvada /** 54401ca24fSsrinivas pandruvada * struct hid_sensor_hub_callbacks_list - Stores callback list 55401ca24fSsrinivas pandruvada * @list: list head. 56401ca24fSsrinivas pandruvada * @usage_id: usage id for a physical device. 57401ca24fSsrinivas pandruvada * @usage_callback: Stores registered callback functions. 58401ca24fSsrinivas pandruvada * @priv: Private data for a physical device. 59401ca24fSsrinivas pandruvada */ 60401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks_list { 61401ca24fSsrinivas pandruvada struct list_head list; 62401ca24fSsrinivas pandruvada u32 usage_id; 63ca2ed12fSSrinivas Pandruvada struct hid_sensor_hub_device *hsdev; 64401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks *usage_callback; 65401ca24fSsrinivas pandruvada void *priv; 66401ca24fSsrinivas pandruvada }; 67401ca24fSsrinivas pandruvada 68401ca24fSsrinivas pandruvada static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev, 69401ca24fSsrinivas pandruvada int dir) 70401ca24fSsrinivas pandruvada { 71401ca24fSsrinivas pandruvada struct hid_report *report; 72401ca24fSsrinivas pandruvada 73401ca24fSsrinivas pandruvada list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) { 74401ca24fSsrinivas pandruvada if (report->id == id) 75401ca24fSsrinivas pandruvada return report; 76401ca24fSsrinivas pandruvada } 77401ca24fSsrinivas pandruvada hid_warn(hdev, "No report with id 0x%x found\n", id); 78401ca24fSsrinivas pandruvada 79401ca24fSsrinivas pandruvada return NULL; 80401ca24fSsrinivas pandruvada } 81401ca24fSsrinivas pandruvada 82ca2ed12fSSrinivas Pandruvada static int sensor_hub_get_physical_device_count(struct hid_device *hdev) 83401ca24fSsrinivas pandruvada { 84ca2ed12fSSrinivas Pandruvada int i; 85ca2ed12fSSrinivas Pandruvada int count = 0; 86401ca24fSsrinivas pandruvada 87ca2ed12fSSrinivas Pandruvada for (i = 0; i < hdev->maxcollection; ++i) { 88ca2ed12fSSrinivas Pandruvada struct hid_collection *collection = &hdev->collection[i]; 89cb67126fSSrinivas Pandruvada if (collection->type == HID_COLLECTION_PHYSICAL || 90cb67126fSSrinivas Pandruvada collection->type == HID_COLLECTION_APPLICATION) 91ca2ed12fSSrinivas Pandruvada ++count; 92401ca24fSsrinivas pandruvada } 93401ca24fSsrinivas pandruvada 94ca2ed12fSSrinivas Pandruvada return count; 95401ca24fSsrinivas pandruvada } 96401ca24fSsrinivas pandruvada 97401ca24fSsrinivas pandruvada static void sensor_hub_fill_attr_info( 98401ca24fSsrinivas pandruvada struct hid_sensor_hub_attribute_info *info, 999f740ffaSSrinivas Pandruvada s32 index, s32 report_id, struct hid_field *field) 100401ca24fSsrinivas pandruvada { 101401ca24fSsrinivas pandruvada info->index = index; 102401ca24fSsrinivas pandruvada info->report_id = report_id; 1039f740ffaSSrinivas Pandruvada info->units = field->unit; 1049f740ffaSSrinivas Pandruvada info->unit_expo = field->unit_exponent; 1059f740ffaSSrinivas Pandruvada info->size = (field->report_size * field->report_count)/8; 1069f740ffaSSrinivas Pandruvada info->logical_minimum = field->logical_minimum; 1079f740ffaSSrinivas Pandruvada info->logical_maximum = field->logical_maximum; 108401ca24fSsrinivas pandruvada } 109401ca24fSsrinivas pandruvada 110401ca24fSsrinivas pandruvada static struct hid_sensor_hub_callbacks *sensor_hub_get_callback( 111401ca24fSsrinivas pandruvada struct hid_device *hdev, 112ca2ed12fSSrinivas Pandruvada u32 usage_id, 113ca2ed12fSSrinivas Pandruvada int collection_index, 114ca2ed12fSSrinivas Pandruvada struct hid_sensor_hub_device **hsdev, 115ca2ed12fSSrinivas Pandruvada void **priv) 116401ca24fSsrinivas pandruvada { 117401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks_list *callback; 118401ca24fSsrinivas pandruvada struct sensor_hub_data *pdata = hid_get_drvdata(hdev); 119ed119770SSrinivas Pandruvada unsigned long flags; 120401ca24fSsrinivas pandruvada 121ed119770SSrinivas Pandruvada spin_lock_irqsave(&pdata->dyn_callback_lock, flags); 122401ca24fSsrinivas pandruvada list_for_each_entry(callback, &pdata->dyn_callback_list, list) 123cb67126fSSrinivas Pandruvada if ((callback->usage_id == usage_id || 124cb67126fSSrinivas Pandruvada callback->usage_id == HID_USAGE_SENSOR_COLLECTION) && 125ca2ed12fSSrinivas Pandruvada (collection_index >= 126ca2ed12fSSrinivas Pandruvada callback->hsdev->start_collection_index) && 127ca2ed12fSSrinivas Pandruvada (collection_index < 128ca2ed12fSSrinivas Pandruvada callback->hsdev->end_collection_index)) { 129401ca24fSsrinivas pandruvada *priv = callback->priv; 130ca2ed12fSSrinivas Pandruvada *hsdev = callback->hsdev; 131ed119770SSrinivas Pandruvada spin_unlock_irqrestore(&pdata->dyn_callback_lock, 132ed119770SSrinivas Pandruvada flags); 133401ca24fSsrinivas pandruvada return callback->usage_callback; 134401ca24fSsrinivas pandruvada } 135ed119770SSrinivas Pandruvada spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 136401ca24fSsrinivas pandruvada 137401ca24fSsrinivas pandruvada return NULL; 138401ca24fSsrinivas pandruvada } 139401ca24fSsrinivas pandruvada 140401ca24fSsrinivas pandruvada int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev, 141401ca24fSsrinivas pandruvada u32 usage_id, 142401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks *usage_callback) 143401ca24fSsrinivas pandruvada { 144401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks_list *callback; 145401ca24fSsrinivas pandruvada struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev); 1460ccf091dSJiri Kosina unsigned long flags; 147401ca24fSsrinivas pandruvada 1480ccf091dSJiri Kosina spin_lock_irqsave(&pdata->dyn_callback_lock, flags); 149401ca24fSsrinivas pandruvada list_for_each_entry(callback, &pdata->dyn_callback_list, list) 150ca2ed12fSSrinivas Pandruvada if (callback->usage_id == usage_id && 151ca2ed12fSSrinivas Pandruvada callback->hsdev == hsdev) { 1520ccf091dSJiri Kosina spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 153401ca24fSsrinivas pandruvada return -EINVAL; 154401ca24fSsrinivas pandruvada } 1552b7c4b8eSDan Carpenter callback = kzalloc(sizeof(*callback), GFP_ATOMIC); 156401ca24fSsrinivas pandruvada if (!callback) { 1570ccf091dSJiri Kosina spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 158401ca24fSsrinivas pandruvada return -ENOMEM; 159401ca24fSsrinivas pandruvada } 160ca2ed12fSSrinivas Pandruvada callback->hsdev = hsdev; 161401ca24fSsrinivas pandruvada callback->usage_callback = usage_callback; 162401ca24fSsrinivas pandruvada callback->usage_id = usage_id; 163401ca24fSsrinivas pandruvada callback->priv = NULL; 164cb67126fSSrinivas Pandruvada /* 165cb67126fSSrinivas Pandruvada * If there is a handler registered for the collection type, then 166cb67126fSSrinivas Pandruvada * it will handle all reports for sensors in this collection. If 167cb67126fSSrinivas Pandruvada * there is also an individual sensor handler registration, then 168cb67126fSSrinivas Pandruvada * we want to make sure that the reports are directed to collection 169cb67126fSSrinivas Pandruvada * handler, as this may be a fusion sensor. So add collection handlers 170cb67126fSSrinivas Pandruvada * to the beginning of the list, so that they are matched first. 171cb67126fSSrinivas Pandruvada */ 172cb67126fSSrinivas Pandruvada if (usage_id == HID_USAGE_SENSOR_COLLECTION) 173cb67126fSSrinivas Pandruvada list_add(&callback->list, &pdata->dyn_callback_list); 174cb67126fSSrinivas Pandruvada else 175401ca24fSsrinivas pandruvada list_add_tail(&callback->list, &pdata->dyn_callback_list); 1760ccf091dSJiri Kosina spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 177401ca24fSsrinivas pandruvada 178401ca24fSsrinivas pandruvada return 0; 179401ca24fSsrinivas pandruvada } 180401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_register_callback); 181401ca24fSsrinivas pandruvada 182401ca24fSsrinivas pandruvada int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev, 183401ca24fSsrinivas pandruvada u32 usage_id) 184401ca24fSsrinivas pandruvada { 185401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks_list *callback; 186401ca24fSsrinivas pandruvada struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev); 1870ccf091dSJiri Kosina unsigned long flags; 188401ca24fSsrinivas pandruvada 1890ccf091dSJiri Kosina spin_lock_irqsave(&pdata->dyn_callback_lock, flags); 190401ca24fSsrinivas pandruvada list_for_each_entry(callback, &pdata->dyn_callback_list, list) 191ca2ed12fSSrinivas Pandruvada if (callback->usage_id == usage_id && 192ca2ed12fSSrinivas Pandruvada callback->hsdev == hsdev) { 193401ca24fSsrinivas pandruvada list_del(&callback->list); 194401ca24fSsrinivas pandruvada kfree(callback); 195401ca24fSsrinivas pandruvada break; 196401ca24fSsrinivas pandruvada } 1970ccf091dSJiri Kosina spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 198401ca24fSsrinivas pandruvada 199401ca24fSsrinivas pandruvada return 0; 200401ca24fSsrinivas pandruvada } 201401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_remove_callback); 202401ca24fSsrinivas pandruvada 203401ca24fSsrinivas pandruvada int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id, 2043950e033SSrinivas Pandruvada u32 field_index, int buffer_size, void *buffer) 205401ca24fSsrinivas pandruvada { 206401ca24fSsrinivas pandruvada struct hid_report *report; 207401ca24fSsrinivas pandruvada struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 2083950e033SSrinivas Pandruvada __s32 *buf32 = buffer; 2093950e033SSrinivas Pandruvada int i = 0; 2103950e033SSrinivas Pandruvada int remaining_bytes; 2113950e033SSrinivas Pandruvada __s32 value; 212401ca24fSsrinivas pandruvada int ret = 0; 213401ca24fSsrinivas pandruvada 214401ca24fSsrinivas pandruvada mutex_lock(&data->mutex); 215401ca24fSsrinivas pandruvada report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT); 216401ca24fSsrinivas pandruvada if (!report || (field_index >= report->maxfield)) { 217401ca24fSsrinivas pandruvada ret = -EINVAL; 218401ca24fSsrinivas pandruvada goto done_proc; 219401ca24fSsrinivas pandruvada } 2203950e033SSrinivas Pandruvada 2213950e033SSrinivas Pandruvada remaining_bytes = do_div(buffer_size, sizeof(__s32)); 2223950e033SSrinivas Pandruvada if (buffer_size) { 2233950e033SSrinivas Pandruvada for (i = 0; i < buffer_size; ++i) { 2243950e033SSrinivas Pandruvada hid_set_field(report->field[field_index], i, 22562fad137SSrinivas Pandruvada (__force __s32)cpu_to_le32(*buf32)); 2263950e033SSrinivas Pandruvada ++buf32; 2273950e033SSrinivas Pandruvada } 2283950e033SSrinivas Pandruvada } 2293950e033SSrinivas Pandruvada if (remaining_bytes) { 2303950e033SSrinivas Pandruvada value = 0; 2313950e033SSrinivas Pandruvada memcpy(&value, (u8 *)buf32, remaining_bytes); 2323950e033SSrinivas Pandruvada hid_set_field(report->field[field_index], i, 23362fad137SSrinivas Pandruvada (__force __s32)cpu_to_le32(value)); 2343950e033SSrinivas Pandruvada } 235d8814272SBenjamin Tissoires hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT); 236b7966a4dSBenjamin Tissoires hid_hw_wait(hsdev->hdev); 237401ca24fSsrinivas pandruvada 238401ca24fSsrinivas pandruvada done_proc: 239401ca24fSsrinivas pandruvada mutex_unlock(&data->mutex); 240401ca24fSsrinivas pandruvada 241401ca24fSsrinivas pandruvada return ret; 242401ca24fSsrinivas pandruvada } 243401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_set_feature); 244401ca24fSsrinivas pandruvada 245401ca24fSsrinivas pandruvada int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id, 2466adc83fcSSrinivas Pandruvada u32 field_index, int buffer_size, void *buffer) 247401ca24fSsrinivas pandruvada { 248401ca24fSsrinivas pandruvada struct hid_report *report; 249401ca24fSsrinivas pandruvada struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 2506adc83fcSSrinivas Pandruvada int report_size; 251401ca24fSsrinivas pandruvada int ret = 0; 252401ca24fSsrinivas pandruvada 253401ca24fSsrinivas pandruvada mutex_lock(&data->mutex); 254401ca24fSsrinivas pandruvada report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT); 2559e891025SKees Cook if (!report || (field_index >= report->maxfield) || 2569e891025SKees Cook report->field[field_index]->report_count < 1) { 257401ca24fSsrinivas pandruvada ret = -EINVAL; 258401ca24fSsrinivas pandruvada goto done_proc; 259401ca24fSsrinivas pandruvada } 260d8814272SBenjamin Tissoires hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT); 261b7966a4dSBenjamin Tissoires hid_hw_wait(hsdev->hdev); 2626adc83fcSSrinivas Pandruvada 2636adc83fcSSrinivas Pandruvada /* calculate number of bytes required to read this field */ 2646adc83fcSSrinivas Pandruvada report_size = DIV_ROUND_UP(report->field[field_index]->report_size, 2656adc83fcSSrinivas Pandruvada 8) * 2666adc83fcSSrinivas Pandruvada report->field[field_index]->report_count; 2676adc83fcSSrinivas Pandruvada if (!report_size) { 2686adc83fcSSrinivas Pandruvada ret = -EINVAL; 2696adc83fcSSrinivas Pandruvada goto done_proc; 2706adc83fcSSrinivas Pandruvada } 2716adc83fcSSrinivas Pandruvada ret = min(report_size, buffer_size); 2726adc83fcSSrinivas Pandruvada memcpy(buffer, report->field[field_index]->value, ret); 273401ca24fSsrinivas pandruvada 274401ca24fSsrinivas pandruvada done_proc: 275401ca24fSsrinivas pandruvada mutex_unlock(&data->mutex); 276401ca24fSsrinivas pandruvada 277401ca24fSsrinivas pandruvada return ret; 278401ca24fSsrinivas pandruvada } 279401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_get_feature); 280401ca24fSsrinivas pandruvada 281401ca24fSsrinivas pandruvada 282401ca24fSsrinivas pandruvada int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev, 283401ca24fSsrinivas pandruvada u32 usage_id, 284b3f4737dSSrinivas Pandruvada u32 attr_usage_id, u32 report_id, 285b3f4737dSSrinivas Pandruvada enum sensor_hub_read_flags flag) 286401ca24fSsrinivas pandruvada { 287401ca24fSsrinivas pandruvada struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 288401ca24fSsrinivas pandruvada unsigned long flags; 289401ca24fSsrinivas pandruvada struct hid_report *report; 290401ca24fSsrinivas pandruvada int ret_val = 0; 291401ca24fSsrinivas pandruvada 292b3f4737dSSrinivas Pandruvada report = sensor_hub_report(report_id, hsdev->hdev, 293b3f4737dSSrinivas Pandruvada HID_INPUT_REPORT); 294b3f4737dSSrinivas Pandruvada if (!report) 295b3f4737dSSrinivas Pandruvada return -EINVAL; 296b3f4737dSSrinivas Pandruvada 297*2d94e522SSrinivas Pandruvada mutex_lock(hsdev->mutex_ptr); 298b3f4737dSSrinivas Pandruvada if (flag == SENSOR_HUB_SYNC) { 299e651a1daSSrinivas Pandruvada memset(&hsdev->pending, 0, sizeof(hsdev->pending)); 300e651a1daSSrinivas Pandruvada init_completion(&hsdev->pending.ready); 301e651a1daSSrinivas Pandruvada hsdev->pending.usage_id = usage_id; 302e651a1daSSrinivas Pandruvada hsdev->pending.attr_usage_id = attr_usage_id; 303e651a1daSSrinivas Pandruvada hsdev->pending.raw_size = 0; 304401ca24fSsrinivas pandruvada 305401ca24fSsrinivas pandruvada spin_lock_irqsave(&data->lock, flags); 306e651a1daSSrinivas Pandruvada hsdev->pending.status = true; 307f74346a0SSrinivas Pandruvada spin_unlock_irqrestore(&data->lock, flags); 308b3f4737dSSrinivas Pandruvada } 309e651a1daSSrinivas Pandruvada mutex_lock(&data->mutex); 310d8814272SBenjamin Tissoires hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT); 311e651a1daSSrinivas Pandruvada mutex_unlock(&data->mutex); 312b3f4737dSSrinivas Pandruvada if (flag == SENSOR_HUB_SYNC) { 313b3f4737dSSrinivas Pandruvada wait_for_completion_interruptible_timeout( 314b3f4737dSSrinivas Pandruvada &hsdev->pending.ready, HZ*5); 315e651a1daSSrinivas Pandruvada switch (hsdev->pending.raw_size) { 316401ca24fSsrinivas pandruvada case 1: 317e651a1daSSrinivas Pandruvada ret_val = *(u8 *)hsdev->pending.raw_data; 318401ca24fSsrinivas pandruvada break; 319401ca24fSsrinivas pandruvada case 2: 320e651a1daSSrinivas Pandruvada ret_val = *(u16 *)hsdev->pending.raw_data; 321401ca24fSsrinivas pandruvada break; 322401ca24fSsrinivas pandruvada case 4: 323e651a1daSSrinivas Pandruvada ret_val = *(u32 *)hsdev->pending.raw_data; 324401ca24fSsrinivas pandruvada break; 325401ca24fSsrinivas pandruvada default: 326401ca24fSsrinivas pandruvada ret_val = 0; 327401ca24fSsrinivas pandruvada } 328e651a1daSSrinivas Pandruvada kfree(hsdev->pending.raw_data); 329e651a1daSSrinivas Pandruvada hsdev->pending.status = false; 330b3f4737dSSrinivas Pandruvada } 331*2d94e522SSrinivas Pandruvada mutex_unlock(hsdev->mutex_ptr); 332401ca24fSsrinivas pandruvada 333401ca24fSsrinivas pandruvada return ret_val; 334401ca24fSsrinivas pandruvada } 335401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value); 336401ca24fSsrinivas pandruvada 337e02cee48SSrinivas Pandruvada int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev, 338e02cee48SSrinivas Pandruvada u32 report_id, int field_index, u32 usage_id) 339e02cee48SSrinivas Pandruvada { 340e02cee48SSrinivas Pandruvada struct hid_report *report; 341e02cee48SSrinivas Pandruvada struct hid_field *field; 342e02cee48SSrinivas Pandruvada int i; 343e02cee48SSrinivas Pandruvada 344e02cee48SSrinivas Pandruvada report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT); 345e02cee48SSrinivas Pandruvada if (!report || (field_index >= report->maxfield)) 346e02cee48SSrinivas Pandruvada goto done_proc; 347e02cee48SSrinivas Pandruvada 348e02cee48SSrinivas Pandruvada field = report->field[field_index]; 349e02cee48SSrinivas Pandruvada for (i = 0; i < field->maxusage; ++i) { 350e02cee48SSrinivas Pandruvada if (field->usage[i].hid == usage_id) 351e02cee48SSrinivas Pandruvada return field->usage[i].usage_index; 352e02cee48SSrinivas Pandruvada } 353e02cee48SSrinivas Pandruvada 354e02cee48SSrinivas Pandruvada done_proc: 355e02cee48SSrinivas Pandruvada return -EINVAL; 356e02cee48SSrinivas Pandruvada } 357e02cee48SSrinivas Pandruvada EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index); 358e02cee48SSrinivas Pandruvada 359401ca24fSsrinivas pandruvada int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev, 360401ca24fSsrinivas pandruvada u8 type, 361401ca24fSsrinivas pandruvada u32 usage_id, 362401ca24fSsrinivas pandruvada u32 attr_usage_id, 363401ca24fSsrinivas pandruvada struct hid_sensor_hub_attribute_info *info) 364401ca24fSsrinivas pandruvada { 365401ca24fSsrinivas pandruvada int ret = -1; 366ca2ed12fSSrinivas Pandruvada int i; 367401ca24fSsrinivas pandruvada struct hid_report *report; 368401ca24fSsrinivas pandruvada struct hid_field *field; 369401ca24fSsrinivas pandruvada struct hid_report_enum *report_enum; 370401ca24fSsrinivas pandruvada struct hid_device *hdev = hsdev->hdev; 371401ca24fSsrinivas pandruvada 372401ca24fSsrinivas pandruvada /* Initialize with defaults */ 373401ca24fSsrinivas pandruvada info->usage_id = usage_id; 374401ca24fSsrinivas pandruvada info->attrib_id = attr_usage_id; 375401ca24fSsrinivas pandruvada info->report_id = -1; 376401ca24fSsrinivas pandruvada info->index = -1; 377401ca24fSsrinivas pandruvada info->units = -1; 378401ca24fSsrinivas pandruvada info->unit_expo = -1; 379401ca24fSsrinivas pandruvada 380401ca24fSsrinivas pandruvada report_enum = &hdev->report_enum[type]; 381401ca24fSsrinivas pandruvada list_for_each_entry(report, &report_enum->report_list, list) { 382401ca24fSsrinivas pandruvada for (i = 0; i < report->maxfield; ++i) { 383401ca24fSsrinivas pandruvada field = report->field[i]; 384ca2ed12fSSrinivas Pandruvada if (field->maxusage) { 385401ca24fSsrinivas pandruvada if (field->physical == usage_id && 386ca2ed12fSSrinivas Pandruvada (field->logical == attr_usage_id || 387ca2ed12fSSrinivas Pandruvada field->usage[0].hid == 388ca2ed12fSSrinivas Pandruvada attr_usage_id) && 389ca2ed12fSSrinivas Pandruvada (field->usage[0].collection_index >= 390ca2ed12fSSrinivas Pandruvada hsdev->start_collection_index) && 391ca2ed12fSSrinivas Pandruvada (field->usage[0].collection_index < 392ca2ed12fSSrinivas Pandruvada hsdev->end_collection_index)) { 393ca2ed12fSSrinivas Pandruvada 394ca2ed12fSSrinivas Pandruvada sensor_hub_fill_attr_info(info, i, 395ca2ed12fSSrinivas Pandruvada report->id, 3969f740ffaSSrinivas Pandruvada field); 397401ca24fSsrinivas pandruvada ret = 0; 398401ca24fSsrinivas pandruvada break; 399401ca24fSsrinivas pandruvada } 400401ca24fSsrinivas pandruvada } 401401ca24fSsrinivas pandruvada } 402401ca24fSsrinivas pandruvada 403ca2ed12fSSrinivas Pandruvada } 404ca2ed12fSSrinivas Pandruvada 405401ca24fSsrinivas pandruvada return ret; 406401ca24fSsrinivas pandruvada } 407401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info); 408401ca24fSsrinivas pandruvada 409401ca24fSsrinivas pandruvada #ifdef CONFIG_PM 410401ca24fSsrinivas pandruvada static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message) 411401ca24fSsrinivas pandruvada { 412401ca24fSsrinivas pandruvada struct sensor_hub_data *pdata = hid_get_drvdata(hdev); 413401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks_list *callback; 4140ccf091dSJiri Kosina unsigned long flags; 415401ca24fSsrinivas pandruvada 416401ca24fSsrinivas pandruvada hid_dbg(hdev, " sensor_hub_suspend\n"); 4170ccf091dSJiri Kosina spin_lock_irqsave(&pdata->dyn_callback_lock, flags); 418401ca24fSsrinivas pandruvada list_for_each_entry(callback, &pdata->dyn_callback_list, list) { 419401ca24fSsrinivas pandruvada if (callback->usage_callback->suspend) 420401ca24fSsrinivas pandruvada callback->usage_callback->suspend( 421ca2ed12fSSrinivas Pandruvada callback->hsdev, callback->priv); 422401ca24fSsrinivas pandruvada } 4230ccf091dSJiri Kosina spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 424401ca24fSsrinivas pandruvada 425401ca24fSsrinivas pandruvada return 0; 426401ca24fSsrinivas pandruvada } 427401ca24fSsrinivas pandruvada 428401ca24fSsrinivas pandruvada static int sensor_hub_resume(struct hid_device *hdev) 429401ca24fSsrinivas pandruvada { 430401ca24fSsrinivas pandruvada struct sensor_hub_data *pdata = hid_get_drvdata(hdev); 431401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks_list *callback; 4320ccf091dSJiri Kosina unsigned long flags; 433401ca24fSsrinivas pandruvada 434401ca24fSsrinivas pandruvada hid_dbg(hdev, " sensor_hub_resume\n"); 4350ccf091dSJiri Kosina spin_lock_irqsave(&pdata->dyn_callback_lock, flags); 436401ca24fSsrinivas pandruvada list_for_each_entry(callback, &pdata->dyn_callback_list, list) { 437401ca24fSsrinivas pandruvada if (callback->usage_callback->resume) 438401ca24fSsrinivas pandruvada callback->usage_callback->resume( 439ca2ed12fSSrinivas Pandruvada callback->hsdev, callback->priv); 440401ca24fSsrinivas pandruvada } 4410ccf091dSJiri Kosina spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 442401ca24fSsrinivas pandruvada 443401ca24fSsrinivas pandruvada return 0; 444401ca24fSsrinivas pandruvada } 445401ca24fSsrinivas pandruvada 446401ca24fSsrinivas pandruvada static int sensor_hub_reset_resume(struct hid_device *hdev) 447401ca24fSsrinivas pandruvada { 448401ca24fSsrinivas pandruvada return 0; 449401ca24fSsrinivas pandruvada } 450401ca24fSsrinivas pandruvada #endif 4515902fde1SAndy Shevchenko 452401ca24fSsrinivas pandruvada /* 453401ca24fSsrinivas pandruvada * Handle raw report as sent by device 454401ca24fSsrinivas pandruvada */ 455401ca24fSsrinivas pandruvada static int sensor_hub_raw_event(struct hid_device *hdev, 456401ca24fSsrinivas pandruvada struct hid_report *report, u8 *raw_data, int size) 457401ca24fSsrinivas pandruvada { 458401ca24fSsrinivas pandruvada int i; 459401ca24fSsrinivas pandruvada u8 *ptr; 460401ca24fSsrinivas pandruvada int sz; 461401ca24fSsrinivas pandruvada struct sensor_hub_data *pdata = hid_get_drvdata(hdev); 462401ca24fSsrinivas pandruvada unsigned long flags; 463401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks *callback = NULL; 464401ca24fSsrinivas pandruvada struct hid_collection *collection = NULL; 465401ca24fSsrinivas pandruvada void *priv = NULL; 466ca2ed12fSSrinivas Pandruvada struct hid_sensor_hub_device *hsdev = NULL; 467401ca24fSsrinivas pandruvada 468401ca24fSsrinivas pandruvada hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n", 469401ca24fSsrinivas pandruvada report->id, size, report->type); 470401ca24fSsrinivas pandruvada hid_dbg(hdev, "maxfield:%d\n", report->maxfield); 471401ca24fSsrinivas pandruvada if (report->type != HID_INPUT_REPORT) 472401ca24fSsrinivas pandruvada return 1; 473401ca24fSsrinivas pandruvada 474401ca24fSsrinivas pandruvada ptr = raw_data; 475401ca24fSsrinivas pandruvada ptr++; /* Skip report id */ 476401ca24fSsrinivas pandruvada 477401ca24fSsrinivas pandruvada spin_lock_irqsave(&pdata->lock, flags); 478401ca24fSsrinivas pandruvada 479401ca24fSsrinivas pandruvada for (i = 0; i < report->maxfield; ++i) { 480401ca24fSsrinivas pandruvada hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n", 481401ca24fSsrinivas pandruvada i, report->field[i]->usage->collection_index, 482401ca24fSsrinivas pandruvada report->field[i]->usage->hid, 483d4b1bba7SSrinivas Pandruvada (report->field[i]->report_size * 484d4b1bba7SSrinivas Pandruvada report->field[i]->report_count)/8); 485d4b1bba7SSrinivas Pandruvada sz = (report->field[i]->report_size * 486d4b1bba7SSrinivas Pandruvada report->field[i]->report_count)/8; 487401ca24fSsrinivas pandruvada collection = &hdev->collection[ 488401ca24fSsrinivas pandruvada report->field[i]->usage->collection_index]; 489401ca24fSsrinivas pandruvada hid_dbg(hdev, "collection->usage %x\n", 490401ca24fSsrinivas pandruvada collection->usage); 491ca2ed12fSSrinivas Pandruvada 492ca2ed12fSSrinivas Pandruvada callback = sensor_hub_get_callback(hdev, 493401ca24fSsrinivas pandruvada report->field[i]->physical, 494ca2ed12fSSrinivas Pandruvada report->field[i]->usage[0].collection_index, 495ca2ed12fSSrinivas Pandruvada &hsdev, &priv); 496e651a1daSSrinivas Pandruvada if (!callback) { 497e651a1daSSrinivas Pandruvada ptr += sz; 498e651a1daSSrinivas Pandruvada continue; 499e651a1daSSrinivas Pandruvada } 500eb964833SSrinivas Pandruvada if (hsdev->pending.status && (hsdev->pending.attr_usage_id == 501eb964833SSrinivas Pandruvada report->field[i]->usage->hid || 502eb964833SSrinivas Pandruvada hsdev->pending.attr_usage_id == 503eb964833SSrinivas Pandruvada report->field[i]->logical)) { 504e651a1daSSrinivas Pandruvada hid_dbg(hdev, "data was pending ...\n"); 505e651a1daSSrinivas Pandruvada hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC); 506e651a1daSSrinivas Pandruvada if (hsdev->pending.raw_data) 507e651a1daSSrinivas Pandruvada hsdev->pending.raw_size = sz; 508e651a1daSSrinivas Pandruvada else 509e651a1daSSrinivas Pandruvada hsdev->pending.raw_size = 0; 510e651a1daSSrinivas Pandruvada complete(&hsdev->pending.ready); 511e651a1daSSrinivas Pandruvada } 512e651a1daSSrinivas Pandruvada if (callback->capture_sample) { 513401ca24fSsrinivas pandruvada if (report->field[i]->logical) 514ca2ed12fSSrinivas Pandruvada callback->capture_sample(hsdev, 515401ca24fSsrinivas pandruvada report->field[i]->logical, sz, ptr, 516401ca24fSsrinivas pandruvada callback->pdev); 517401ca24fSsrinivas pandruvada else 518ca2ed12fSSrinivas Pandruvada callback->capture_sample(hsdev, 519401ca24fSsrinivas pandruvada report->field[i]->usage->hid, sz, ptr, 520401ca24fSsrinivas pandruvada callback->pdev); 521401ca24fSsrinivas pandruvada } 522401ca24fSsrinivas pandruvada ptr += sz; 523401ca24fSsrinivas pandruvada } 524401ca24fSsrinivas pandruvada if (callback && collection && callback->send_event) 525ca2ed12fSSrinivas Pandruvada callback->send_event(hsdev, collection->usage, 526401ca24fSsrinivas pandruvada callback->pdev); 527401ca24fSsrinivas pandruvada spin_unlock_irqrestore(&pdata->lock, flags); 528401ca24fSsrinivas pandruvada 529401ca24fSsrinivas pandruvada return 1; 530401ca24fSsrinivas pandruvada } 531401ca24fSsrinivas pandruvada 5321df3a401SSrinivas Pandruvada int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev) 5331df3a401SSrinivas Pandruvada { 5341df3a401SSrinivas Pandruvada int ret = 0; 5351df3a401SSrinivas Pandruvada struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 5361df3a401SSrinivas Pandruvada 5371df3a401SSrinivas Pandruvada mutex_lock(&data->mutex); 538ca2ed12fSSrinivas Pandruvada if (!data->ref_cnt) { 5391df3a401SSrinivas Pandruvada ret = hid_hw_open(hsdev->hdev); 5401df3a401SSrinivas Pandruvada if (ret) { 5411df3a401SSrinivas Pandruvada hid_err(hsdev->hdev, "failed to open hid device\n"); 5421df3a401SSrinivas Pandruvada mutex_unlock(&data->mutex); 5431df3a401SSrinivas Pandruvada return ret; 5441df3a401SSrinivas Pandruvada } 5451df3a401SSrinivas Pandruvada } 546ca2ed12fSSrinivas Pandruvada data->ref_cnt++; 5471df3a401SSrinivas Pandruvada mutex_unlock(&data->mutex); 5481df3a401SSrinivas Pandruvada 5491df3a401SSrinivas Pandruvada return ret; 5501df3a401SSrinivas Pandruvada } 5511df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_open); 5521df3a401SSrinivas Pandruvada 5531df3a401SSrinivas Pandruvada void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev) 5541df3a401SSrinivas Pandruvada { 5551df3a401SSrinivas Pandruvada struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 5561df3a401SSrinivas Pandruvada 5571df3a401SSrinivas Pandruvada mutex_lock(&data->mutex); 558ca2ed12fSSrinivas Pandruvada data->ref_cnt--; 559ca2ed12fSSrinivas Pandruvada if (!data->ref_cnt) 5601df3a401SSrinivas Pandruvada hid_hw_close(hsdev->hdev); 5611df3a401SSrinivas Pandruvada mutex_unlock(&data->mutex); 5621df3a401SSrinivas Pandruvada } 5631df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_close); 5641df3a401SSrinivas Pandruvada 565875e36f8SSrinivas Pandruvada static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc, 566875e36f8SSrinivas Pandruvada unsigned int *rsize) 567875e36f8SSrinivas Pandruvada { 568875e36f8SSrinivas Pandruvada int index; 569875e36f8SSrinivas Pandruvada struct sensor_hub_data *sd = hid_get_drvdata(hdev); 570875e36f8SSrinivas Pandruvada unsigned char report_block[] = { 571875e36f8SSrinivas Pandruvada 0x0a, 0x16, 0x03, 0x15, 0x00, 0x25, 0x05}; 572875e36f8SSrinivas Pandruvada unsigned char power_block[] = { 573875e36f8SSrinivas Pandruvada 0x0a, 0x19, 0x03, 0x15, 0x00, 0x25, 0x05}; 574875e36f8SSrinivas Pandruvada 575875e36f8SSrinivas Pandruvada if (!(sd->quirks & HID_SENSOR_HUB_ENUM_QUIRK)) { 576875e36f8SSrinivas Pandruvada hid_dbg(hdev, "No Enum quirks\n"); 577875e36f8SSrinivas Pandruvada return rdesc; 578875e36f8SSrinivas Pandruvada } 579875e36f8SSrinivas Pandruvada 580875e36f8SSrinivas Pandruvada /* Looks for power and report state usage id and force to 1 */ 581875e36f8SSrinivas Pandruvada for (index = 0; index < *rsize; ++index) { 582875e36f8SSrinivas Pandruvada if (((*rsize - index) > sizeof(report_block)) && 583875e36f8SSrinivas Pandruvada !memcmp(&rdesc[index], report_block, 584875e36f8SSrinivas Pandruvada sizeof(report_block))) { 585875e36f8SSrinivas Pandruvada rdesc[index + 4] = 0x01; 586875e36f8SSrinivas Pandruvada index += sizeof(report_block); 587875e36f8SSrinivas Pandruvada } 588875e36f8SSrinivas Pandruvada if (((*rsize - index) > sizeof(power_block)) && 589875e36f8SSrinivas Pandruvada !memcmp(&rdesc[index], power_block, 590875e36f8SSrinivas Pandruvada sizeof(power_block))) { 591875e36f8SSrinivas Pandruvada rdesc[index + 4] = 0x01; 592875e36f8SSrinivas Pandruvada index += sizeof(power_block); 593875e36f8SSrinivas Pandruvada } 594875e36f8SSrinivas Pandruvada } 595875e36f8SSrinivas Pandruvada 596875e36f8SSrinivas Pandruvada return rdesc; 597875e36f8SSrinivas Pandruvada } 598875e36f8SSrinivas Pandruvada 599401ca24fSsrinivas pandruvada static int sensor_hub_probe(struct hid_device *hdev, 600401ca24fSsrinivas pandruvada const struct hid_device_id *id) 601401ca24fSsrinivas pandruvada { 602401ca24fSsrinivas pandruvada int ret; 603401ca24fSsrinivas pandruvada struct sensor_hub_data *sd; 604401ca24fSsrinivas pandruvada int i; 605401ca24fSsrinivas pandruvada char *name; 606401ca24fSsrinivas pandruvada int dev_cnt; 607ca2ed12fSSrinivas Pandruvada struct hid_sensor_hub_device *hsdev; 608ca2ed12fSSrinivas Pandruvada struct hid_sensor_hub_device *last_hsdev = NULL; 609cb67126fSSrinivas Pandruvada struct hid_sensor_hub_device *collection_hsdev = NULL; 610401ca24fSsrinivas pandruvada 611905cc199SAndy Shevchenko sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL); 612401ca24fSsrinivas pandruvada if (!sd) { 613401ca24fSsrinivas pandruvada hid_err(hdev, "cannot allocate Sensor data\n"); 614401ca24fSsrinivas pandruvada return -ENOMEM; 615401ca24fSsrinivas pandruvada } 616ca2ed12fSSrinivas Pandruvada 617401ca24fSsrinivas pandruvada hid_set_drvdata(hdev, sd); 618875e36f8SSrinivas Pandruvada sd->quirks = id->driver_data; 619ca2ed12fSSrinivas Pandruvada 620401ca24fSsrinivas pandruvada spin_lock_init(&sd->lock); 621401ca24fSsrinivas pandruvada spin_lock_init(&sd->dyn_callback_lock); 622401ca24fSsrinivas pandruvada mutex_init(&sd->mutex); 623401ca24fSsrinivas pandruvada ret = hid_parse(hdev); 624401ca24fSsrinivas pandruvada if (ret) { 625401ca24fSsrinivas pandruvada hid_err(hdev, "parse failed\n"); 626905cc199SAndy Shevchenko return ret; 627401ca24fSsrinivas pandruvada } 628401ca24fSsrinivas pandruvada INIT_LIST_HEAD(&hdev->inputs); 629401ca24fSsrinivas pandruvada 630401ca24fSsrinivas pandruvada ret = hid_hw_start(hdev, 0); 631401ca24fSsrinivas pandruvada if (ret) { 632401ca24fSsrinivas pandruvada hid_err(hdev, "hw start failed\n"); 633905cc199SAndy Shevchenko return ret; 634401ca24fSsrinivas pandruvada } 635401ca24fSsrinivas pandruvada INIT_LIST_HEAD(&sd->dyn_callback_list); 636401ca24fSsrinivas pandruvada sd->hid_sensor_client_cnt = 0; 637401ca24fSsrinivas pandruvada 638ca2ed12fSSrinivas Pandruvada dev_cnt = sensor_hub_get_physical_device_count(hdev); 639401ca24fSsrinivas pandruvada if (dev_cnt > HID_MAX_PHY_DEVICES) { 640401ca24fSsrinivas pandruvada hid_err(hdev, "Invalid Physical device count\n"); 641401ca24fSsrinivas pandruvada ret = -EINVAL; 6421df3a401SSrinivas Pandruvada goto err_stop_hw; 643401ca24fSsrinivas pandruvada } 6445be5db24SHimangi Saraogi sd->hid_sensor_hub_client_devs = devm_kzalloc(&hdev->dev, dev_cnt * 645401ca24fSsrinivas pandruvada sizeof(struct mfd_cell), 646401ca24fSsrinivas pandruvada GFP_KERNEL); 647401ca24fSsrinivas pandruvada if (sd->hid_sensor_hub_client_devs == NULL) { 648f2f13a68SAxel Lin hid_err(hdev, "Failed to allocate memory for mfd cells\n"); 649401ca24fSsrinivas pandruvada ret = -ENOMEM; 6501df3a401SSrinivas Pandruvada goto err_stop_hw; 651401ca24fSsrinivas pandruvada } 652ca2ed12fSSrinivas Pandruvada 653ca2ed12fSSrinivas Pandruvada for (i = 0; i < hdev->maxcollection; ++i) { 654ca2ed12fSSrinivas Pandruvada struct hid_collection *collection = &hdev->collection[i]; 655ca2ed12fSSrinivas Pandruvada 656cb67126fSSrinivas Pandruvada if (collection->type == HID_COLLECTION_PHYSICAL || 657cb67126fSSrinivas Pandruvada collection->type == HID_COLLECTION_APPLICATION) { 658ca2ed12fSSrinivas Pandruvada 6595be5db24SHimangi Saraogi hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev), 6605be5db24SHimangi Saraogi GFP_KERNEL); 661ca2ed12fSSrinivas Pandruvada if (!hsdev) { 662ca2ed12fSSrinivas Pandruvada hid_err(hdev, "cannot allocate hid_sensor_hub_device\n"); 663ca2ed12fSSrinivas Pandruvada ret = -ENOMEM; 6645be5db24SHimangi Saraogi goto err_stop_hw; 665ca2ed12fSSrinivas Pandruvada } 666ca2ed12fSSrinivas Pandruvada hsdev->hdev = hdev; 667ca2ed12fSSrinivas Pandruvada hsdev->vendor_id = hdev->vendor; 668ca2ed12fSSrinivas Pandruvada hsdev->product_id = hdev->product; 669e651a1daSSrinivas Pandruvada hsdev->usage = collection->usage; 670*2d94e522SSrinivas Pandruvada hsdev->mutex_ptr = devm_kzalloc(&hdev->dev, 671*2d94e522SSrinivas Pandruvada sizeof(struct mutex), 672*2d94e522SSrinivas Pandruvada GFP_KERNEL); 673*2d94e522SSrinivas Pandruvada if (!hsdev->mutex_ptr) { 674*2d94e522SSrinivas Pandruvada ret = -ENOMEM; 675*2d94e522SSrinivas Pandruvada goto err_stop_hw; 676*2d94e522SSrinivas Pandruvada } 677*2d94e522SSrinivas Pandruvada mutex_init(hsdev->mutex_ptr); 678ca2ed12fSSrinivas Pandruvada hsdev->start_collection_index = i; 679ca2ed12fSSrinivas Pandruvada if (last_hsdev) 680ca2ed12fSSrinivas Pandruvada last_hsdev->end_collection_index = i; 681ca2ed12fSSrinivas Pandruvada last_hsdev = hsdev; 6825be5db24SHimangi Saraogi name = devm_kasprintf(&hdev->dev, GFP_KERNEL, 6835be5db24SHimangi Saraogi "HID-SENSOR-%x", 684ca2ed12fSSrinivas Pandruvada collection->usage); 685401ca24fSsrinivas pandruvada if (name == NULL) { 686f2f13a68SAxel Lin hid_err(hdev, "Failed MFD device name\n"); 687401ca24fSsrinivas pandruvada ret = -ENOMEM; 6885be5db24SHimangi Saraogi goto err_stop_hw; 689401ca24fSsrinivas pandruvada } 690401ca24fSsrinivas pandruvada sd->hid_sensor_hub_client_devs[ 691401ca24fSsrinivas pandruvada sd->hid_sensor_client_cnt].name = name; 692401ca24fSsrinivas pandruvada sd->hid_sensor_hub_client_devs[ 693401ca24fSsrinivas pandruvada sd->hid_sensor_client_cnt].platform_data = 694ca2ed12fSSrinivas Pandruvada hsdev; 695401ca24fSsrinivas pandruvada sd->hid_sensor_hub_client_devs[ 696401ca24fSsrinivas pandruvada sd->hid_sensor_client_cnt].pdata_size = 697ca2ed12fSSrinivas Pandruvada sizeof(*hsdev); 698ca2ed12fSSrinivas Pandruvada hid_dbg(hdev, "Adding %s:%d\n", name, 699ca2ed12fSSrinivas Pandruvada hsdev->start_collection_index); 700401ca24fSsrinivas pandruvada sd->hid_sensor_client_cnt++; 701cb67126fSSrinivas Pandruvada if (collection_hsdev) 702cb67126fSSrinivas Pandruvada collection_hsdev->end_collection_index = i; 703cb67126fSSrinivas Pandruvada if (collection->type == HID_COLLECTION_APPLICATION && 704cb67126fSSrinivas Pandruvada collection->usage == HID_USAGE_SENSOR_COLLECTION) 705cb67126fSSrinivas Pandruvada collection_hsdev = hsdev; 706401ca24fSsrinivas pandruvada } 707401ca24fSsrinivas pandruvada } 708ca2ed12fSSrinivas Pandruvada if (last_hsdev) 709ca2ed12fSSrinivas Pandruvada last_hsdev->end_collection_index = i; 710cb67126fSSrinivas Pandruvada if (collection_hsdev) 711cb67126fSSrinivas Pandruvada collection_hsdev->end_collection_index = i; 712ca2ed12fSSrinivas Pandruvada 71316b5fe29SJohan Hovold ret = mfd_add_hotplug_devices(&hdev->dev, 71416b5fe29SJohan Hovold sd->hid_sensor_hub_client_devs, 71516b5fe29SJohan Hovold sd->hid_sensor_client_cnt); 716401ca24fSsrinivas pandruvada if (ret < 0) 7175be5db24SHimangi Saraogi goto err_stop_hw; 718401ca24fSsrinivas pandruvada 719401ca24fSsrinivas pandruvada return ret; 720401ca24fSsrinivas pandruvada 721401ca24fSsrinivas pandruvada err_stop_hw: 722401ca24fSsrinivas pandruvada hid_hw_stop(hdev); 723401ca24fSsrinivas pandruvada 724401ca24fSsrinivas pandruvada return ret; 725401ca24fSsrinivas pandruvada } 726401ca24fSsrinivas pandruvada 727401ca24fSsrinivas pandruvada static void sensor_hub_remove(struct hid_device *hdev) 728401ca24fSsrinivas pandruvada { 729401ca24fSsrinivas pandruvada struct sensor_hub_data *data = hid_get_drvdata(hdev); 730401ca24fSsrinivas pandruvada unsigned long flags; 731e651a1daSSrinivas Pandruvada int i; 732401ca24fSsrinivas pandruvada 733401ca24fSsrinivas pandruvada hid_dbg(hdev, " hardware removed\n"); 734401ca24fSsrinivas pandruvada hid_hw_close(hdev); 735f2f13a68SAxel Lin hid_hw_stop(hdev); 736401ca24fSsrinivas pandruvada spin_lock_irqsave(&data->lock, flags); 737e651a1daSSrinivas Pandruvada for (i = 0; i < data->hid_sensor_client_cnt; ++i) { 738e651a1daSSrinivas Pandruvada struct hid_sensor_hub_device *hsdev = 739e651a1daSSrinivas Pandruvada data->hid_sensor_hub_client_devs[i].platform_data; 740e651a1daSSrinivas Pandruvada if (hsdev->pending.status) 741e651a1daSSrinivas Pandruvada complete(&hsdev->pending.ready); 742e651a1daSSrinivas Pandruvada } 743401ca24fSsrinivas pandruvada spin_unlock_irqrestore(&data->lock, flags); 744401ca24fSsrinivas pandruvada mfd_remove_devices(&hdev->dev); 745401ca24fSsrinivas pandruvada hid_set_drvdata(hdev, NULL); 746401ca24fSsrinivas pandruvada mutex_destroy(&data->mutex); 747401ca24fSsrinivas pandruvada } 748401ca24fSsrinivas pandruvada 749401ca24fSsrinivas pandruvada static const struct hid_device_id sensor_hub_devices[] = { 750875e36f8SSrinivas Pandruvada { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_0, 75130f58877SStephen Chandler Paul USB_DEVICE_ID_INTEL_HID_SENSOR_0), 752875e36f8SSrinivas Pandruvada .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, 753875e36f8SSrinivas Pandruvada { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1, 75430f58877SStephen Chandler Paul USB_DEVICE_ID_INTEL_HID_SENSOR_0), 75530f58877SStephen Chandler Paul .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, 75630f58877SStephen Chandler Paul { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1, 75730f58877SStephen Chandler Paul USB_DEVICE_ID_INTEL_HID_SENSOR_1), 758875e36f8SSrinivas Pandruvada .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, 75900478ee8SReyad Attiyat { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT, 76000478ee8SReyad Attiyat USB_DEVICE_ID_MS_SURFACE_PRO_2), 76100478ee8SReyad Attiyat .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, 76200478ee8SReyad Attiyat { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT, 76300478ee8SReyad Attiyat USB_DEVICE_ID_MS_TOUCH_COVER_2), 76400478ee8SReyad Attiyat .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, 76500478ee8SReyad Attiyat { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT, 76600478ee8SReyad Attiyat USB_DEVICE_ID_MS_TYPE_COVER_2), 76700478ee8SReyad Attiyat .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, 768218eb9edSArchana Patni { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0, 769467669c5SSrinivas Pandruvada USB_DEVICE_ID_STM_HID_SENSOR), 770467669c5SSrinivas Pandruvada .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, 771467669c5SSrinivas Pandruvada { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0, 772dde3b45cSArchana Patni USB_DEVICE_ID_STM_HID_SENSOR_1), 773218eb9edSArchana Patni .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, 774825747bbSPeter F. Patel-Schneider { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_TEXAS_INSTRUMENTS, 775825747bbSPeter F. Patel-Schneider USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA), 776825747bbSPeter F. Patel-Schneider .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, 7770d1e4b02SMika Westerberg { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID, 7780d1e4b02SMika Westerberg HID_ANY_ID) }, 779401ca24fSsrinivas pandruvada { } 780401ca24fSsrinivas pandruvada }; 781401ca24fSsrinivas pandruvada MODULE_DEVICE_TABLE(hid, sensor_hub_devices); 782401ca24fSsrinivas pandruvada 783401ca24fSsrinivas pandruvada static struct hid_driver sensor_hub_driver = { 784401ca24fSsrinivas pandruvada .name = "hid-sensor-hub", 785401ca24fSsrinivas pandruvada .id_table = sensor_hub_devices, 786401ca24fSsrinivas pandruvada .probe = sensor_hub_probe, 787401ca24fSsrinivas pandruvada .remove = sensor_hub_remove, 788401ca24fSsrinivas pandruvada .raw_event = sensor_hub_raw_event, 789875e36f8SSrinivas Pandruvada .report_fixup = sensor_hub_report_fixup, 790401ca24fSsrinivas pandruvada #ifdef CONFIG_PM 791401ca24fSsrinivas pandruvada .suspend = sensor_hub_suspend, 792401ca24fSsrinivas pandruvada .resume = sensor_hub_resume, 793401ca24fSsrinivas pandruvada .reset_resume = sensor_hub_reset_resume, 794401ca24fSsrinivas pandruvada #endif 795401ca24fSsrinivas pandruvada }; 796f425458eSH Hartley Sweeten module_hid_driver(sensor_hub_driver); 797401ca24fSsrinivas pandruvada 798401ca24fSsrinivas pandruvada MODULE_DESCRIPTION("HID Sensor Hub driver"); 799401ca24fSsrinivas pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); 800401ca24fSsrinivas pandruvada MODULE_LICENSE("GPL"); 801