1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2401ca24fSsrinivas pandruvada /* 3401ca24fSsrinivas pandruvada * HID Sensors Driver 4401ca24fSsrinivas pandruvada * Copyright (c) 2012, Intel Corporation. 5401ca24fSsrinivas pandruvada */ 6930fafd9SSrinivas Pandruvada 7401ca24fSsrinivas pandruvada #include <linux/device.h> 8401ca24fSsrinivas pandruvada #include <linux/hid.h> 9401ca24fSsrinivas pandruvada #include <linux/module.h> 10401ca24fSsrinivas pandruvada #include <linux/slab.h> 11401ca24fSsrinivas pandruvada #include <linux/mfd/core.h> 12401ca24fSsrinivas pandruvada #include <linux/list.h> 13401ca24fSsrinivas pandruvada #include <linux/hid-sensor-ids.h> 14401ca24fSsrinivas pandruvada #include <linux/hid-sensor-hub.h> 15401ca24fSsrinivas pandruvada #include "hid-ids.h" 16401ca24fSsrinivas pandruvada 17875e36f8SSrinivas Pandruvada #define HID_SENSOR_HUB_ENUM_QUIRK 0x01 18875e36f8SSrinivas Pandruvada 19401ca24fSsrinivas pandruvada /** 20401ca24fSsrinivas pandruvada * struct sensor_hub_data - Hold a instance data for a HID hub device 21401ca24fSsrinivas pandruvada * @mutex: Mutex to serialize synchronous request. 22401ca24fSsrinivas pandruvada * @lock: Spin lock to protect pending request structure. 23401ca24fSsrinivas pandruvada * @dyn_callback_list: Holds callback function 24401ca24fSsrinivas pandruvada * @dyn_callback_lock: spin lock to protect callback list 25401ca24fSsrinivas pandruvada * @hid_sensor_hub_client_devs: Stores all MFD cells for a hub instance. 26401ca24fSsrinivas pandruvada * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached). 27ca2ed12fSSrinivas Pandruvada * @ref_cnt: Number of MFD clients have opened this device 28401ca24fSsrinivas pandruvada */ 29401ca24fSsrinivas pandruvada struct sensor_hub_data { 30401ca24fSsrinivas pandruvada struct mutex mutex; 31401ca24fSsrinivas pandruvada spinlock_t lock; 32401ca24fSsrinivas pandruvada struct list_head dyn_callback_list; 33401ca24fSsrinivas pandruvada spinlock_t dyn_callback_lock; 34401ca24fSsrinivas pandruvada struct mfd_cell *hid_sensor_hub_client_devs; 35401ca24fSsrinivas pandruvada int hid_sensor_client_cnt; 36ca2ed12fSSrinivas Pandruvada int ref_cnt; 37401ca24fSsrinivas pandruvada }; 38401ca24fSsrinivas pandruvada 39401ca24fSsrinivas pandruvada /** 40401ca24fSsrinivas pandruvada * struct hid_sensor_hub_callbacks_list - Stores callback list 41401ca24fSsrinivas pandruvada * @list: list head. 42401ca24fSsrinivas pandruvada * @usage_id: usage id for a physical device. 43ff0e9ee3SLee Jones * @hsdev: Stored hid instance for current hub device. 44401ca24fSsrinivas pandruvada * @usage_callback: Stores registered callback functions. 45401ca24fSsrinivas pandruvada * @priv: Private data for a physical device. 46401ca24fSsrinivas pandruvada */ 47401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks_list { 48401ca24fSsrinivas pandruvada struct list_head list; 49401ca24fSsrinivas pandruvada u32 usage_id; 50ca2ed12fSSrinivas Pandruvada struct hid_sensor_hub_device *hsdev; 51401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks *usage_callback; 52401ca24fSsrinivas pandruvada void *priv; 53401ca24fSsrinivas pandruvada }; 54401ca24fSsrinivas pandruvada 55401ca24fSsrinivas pandruvada static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev, 56401ca24fSsrinivas pandruvada int dir) 57401ca24fSsrinivas pandruvada { 58401ca24fSsrinivas pandruvada struct hid_report *report; 59401ca24fSsrinivas pandruvada 60401ca24fSsrinivas pandruvada list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) { 61401ca24fSsrinivas pandruvada if (report->id == id) 62401ca24fSsrinivas pandruvada return report; 63401ca24fSsrinivas pandruvada } 64401ca24fSsrinivas pandruvada hid_warn(hdev, "No report with id 0x%x found\n", id); 65401ca24fSsrinivas pandruvada 66401ca24fSsrinivas pandruvada return NULL; 67401ca24fSsrinivas pandruvada } 68401ca24fSsrinivas pandruvada 69ca2ed12fSSrinivas Pandruvada static int sensor_hub_get_physical_device_count(struct hid_device *hdev) 70401ca24fSsrinivas pandruvada { 71ca2ed12fSSrinivas Pandruvada int i; 72ca2ed12fSSrinivas Pandruvada int count = 0; 73401ca24fSsrinivas pandruvada 74ca2ed12fSSrinivas Pandruvada for (i = 0; i < hdev->maxcollection; ++i) { 75ca2ed12fSSrinivas Pandruvada struct hid_collection *collection = &hdev->collection[i]; 76cb67126fSSrinivas Pandruvada if (collection->type == HID_COLLECTION_PHYSICAL || 77cb67126fSSrinivas Pandruvada collection->type == HID_COLLECTION_APPLICATION) 78ca2ed12fSSrinivas Pandruvada ++count; 79401ca24fSsrinivas pandruvada } 80401ca24fSsrinivas pandruvada 81ca2ed12fSSrinivas Pandruvada return count; 82401ca24fSsrinivas pandruvada } 83401ca24fSsrinivas pandruvada 84401ca24fSsrinivas pandruvada static void sensor_hub_fill_attr_info( 85401ca24fSsrinivas pandruvada struct hid_sensor_hub_attribute_info *info, 869f740ffaSSrinivas Pandruvada s32 index, s32 report_id, struct hid_field *field) 87401ca24fSsrinivas pandruvada { 88401ca24fSsrinivas pandruvada info->index = index; 89401ca24fSsrinivas pandruvada info->report_id = report_id; 909f740ffaSSrinivas Pandruvada info->units = field->unit; 919f740ffaSSrinivas Pandruvada info->unit_expo = field->unit_exponent; 929f740ffaSSrinivas Pandruvada info->size = (field->report_size * field->report_count)/8; 939f740ffaSSrinivas Pandruvada info->logical_minimum = field->logical_minimum; 949f740ffaSSrinivas Pandruvada info->logical_maximum = field->logical_maximum; 95401ca24fSsrinivas pandruvada } 96401ca24fSsrinivas pandruvada 97401ca24fSsrinivas pandruvada static struct hid_sensor_hub_callbacks *sensor_hub_get_callback( 98401ca24fSsrinivas pandruvada struct hid_device *hdev, 99ca2ed12fSSrinivas Pandruvada u32 usage_id, 100ca2ed12fSSrinivas Pandruvada int collection_index, 101ca2ed12fSSrinivas Pandruvada struct hid_sensor_hub_device **hsdev, 102ca2ed12fSSrinivas Pandruvada void **priv) 103401ca24fSsrinivas pandruvada { 104401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks_list *callback; 105401ca24fSsrinivas pandruvada struct sensor_hub_data *pdata = hid_get_drvdata(hdev); 106ed119770SSrinivas Pandruvada unsigned long flags; 107401ca24fSsrinivas pandruvada 108ed119770SSrinivas Pandruvada spin_lock_irqsave(&pdata->dyn_callback_lock, flags); 109401ca24fSsrinivas pandruvada list_for_each_entry(callback, &pdata->dyn_callback_list, list) 110cb67126fSSrinivas Pandruvada if ((callback->usage_id == usage_id || 111cb67126fSSrinivas Pandruvada callback->usage_id == HID_USAGE_SENSOR_COLLECTION) && 112ca2ed12fSSrinivas Pandruvada (collection_index >= 113ca2ed12fSSrinivas Pandruvada callback->hsdev->start_collection_index) && 114ca2ed12fSSrinivas Pandruvada (collection_index < 115ca2ed12fSSrinivas Pandruvada callback->hsdev->end_collection_index)) { 116401ca24fSsrinivas pandruvada *priv = callback->priv; 117ca2ed12fSSrinivas Pandruvada *hsdev = callback->hsdev; 118ed119770SSrinivas Pandruvada spin_unlock_irqrestore(&pdata->dyn_callback_lock, 119ed119770SSrinivas Pandruvada flags); 120401ca24fSsrinivas pandruvada return callback->usage_callback; 121401ca24fSsrinivas pandruvada } 122ed119770SSrinivas Pandruvada spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 123401ca24fSsrinivas pandruvada 124401ca24fSsrinivas pandruvada return NULL; 125401ca24fSsrinivas pandruvada } 126401ca24fSsrinivas pandruvada 127401ca24fSsrinivas pandruvada int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev, 128401ca24fSsrinivas pandruvada u32 usage_id, 129401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks *usage_callback) 130401ca24fSsrinivas pandruvada { 131401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks_list *callback; 132401ca24fSsrinivas pandruvada struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev); 1330ccf091dSJiri Kosina unsigned long flags; 134401ca24fSsrinivas pandruvada 1350ccf091dSJiri Kosina spin_lock_irqsave(&pdata->dyn_callback_lock, flags); 136401ca24fSsrinivas pandruvada list_for_each_entry(callback, &pdata->dyn_callback_list, list) 137ca2ed12fSSrinivas Pandruvada if (callback->usage_id == usage_id && 138ca2ed12fSSrinivas Pandruvada callback->hsdev == hsdev) { 1390ccf091dSJiri Kosina spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 140401ca24fSsrinivas pandruvada return -EINVAL; 141401ca24fSsrinivas pandruvada } 1422b7c4b8eSDan Carpenter callback = kzalloc(sizeof(*callback), GFP_ATOMIC); 143401ca24fSsrinivas pandruvada if (!callback) { 1440ccf091dSJiri Kosina spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 145401ca24fSsrinivas pandruvada return -ENOMEM; 146401ca24fSsrinivas pandruvada } 147ca2ed12fSSrinivas Pandruvada callback->hsdev = hsdev; 148401ca24fSsrinivas pandruvada callback->usage_callback = usage_callback; 149401ca24fSsrinivas pandruvada callback->usage_id = usage_id; 150401ca24fSsrinivas pandruvada callback->priv = NULL; 151cb67126fSSrinivas Pandruvada /* 152cb67126fSSrinivas Pandruvada * If there is a handler registered for the collection type, then 153cb67126fSSrinivas Pandruvada * it will handle all reports for sensors in this collection. If 154cb67126fSSrinivas Pandruvada * there is also an individual sensor handler registration, then 155cb67126fSSrinivas Pandruvada * we want to make sure that the reports are directed to collection 156cb67126fSSrinivas Pandruvada * handler, as this may be a fusion sensor. So add collection handlers 157cb67126fSSrinivas Pandruvada * to the beginning of the list, so that they are matched first. 158cb67126fSSrinivas Pandruvada */ 159cb67126fSSrinivas Pandruvada if (usage_id == HID_USAGE_SENSOR_COLLECTION) 160cb67126fSSrinivas Pandruvada list_add(&callback->list, &pdata->dyn_callback_list); 161cb67126fSSrinivas Pandruvada else 162401ca24fSsrinivas pandruvada list_add_tail(&callback->list, &pdata->dyn_callback_list); 1630ccf091dSJiri Kosina spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 164401ca24fSsrinivas pandruvada 165401ca24fSsrinivas pandruvada return 0; 166401ca24fSsrinivas pandruvada } 167401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_register_callback); 168401ca24fSsrinivas pandruvada 169401ca24fSsrinivas pandruvada int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev, 170401ca24fSsrinivas pandruvada u32 usage_id) 171401ca24fSsrinivas pandruvada { 172401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks_list *callback; 173401ca24fSsrinivas pandruvada struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev); 1740ccf091dSJiri Kosina unsigned long flags; 175401ca24fSsrinivas pandruvada 1760ccf091dSJiri Kosina spin_lock_irqsave(&pdata->dyn_callback_lock, flags); 177401ca24fSsrinivas pandruvada list_for_each_entry(callback, &pdata->dyn_callback_list, list) 178ca2ed12fSSrinivas Pandruvada if (callback->usage_id == usage_id && 179ca2ed12fSSrinivas Pandruvada callback->hsdev == hsdev) { 180401ca24fSsrinivas pandruvada list_del(&callback->list); 181401ca24fSsrinivas pandruvada kfree(callback); 182401ca24fSsrinivas pandruvada break; 183401ca24fSsrinivas pandruvada } 1840ccf091dSJiri Kosina spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 185401ca24fSsrinivas pandruvada 186401ca24fSsrinivas pandruvada return 0; 187401ca24fSsrinivas pandruvada } 188401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_remove_callback); 189401ca24fSsrinivas pandruvada 190401ca24fSsrinivas pandruvada int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id, 1913950e033SSrinivas Pandruvada u32 field_index, int buffer_size, void *buffer) 192401ca24fSsrinivas pandruvada { 193401ca24fSsrinivas pandruvada struct hid_report *report; 194401ca24fSsrinivas pandruvada struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 1953950e033SSrinivas Pandruvada __s32 *buf32 = buffer; 1963950e033SSrinivas Pandruvada int i = 0; 1973950e033SSrinivas Pandruvada int remaining_bytes; 1983950e033SSrinivas Pandruvada __s32 value; 199401ca24fSsrinivas pandruvada int ret = 0; 200401ca24fSsrinivas pandruvada 201401ca24fSsrinivas pandruvada mutex_lock(&data->mutex); 202401ca24fSsrinivas pandruvada report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT); 203401ca24fSsrinivas pandruvada if (!report || (field_index >= report->maxfield)) { 204401ca24fSsrinivas pandruvada ret = -EINVAL; 205401ca24fSsrinivas pandruvada goto done_proc; 206401ca24fSsrinivas pandruvada } 2073950e033SSrinivas Pandruvada 2088d43b49eSNicolas Pitre remaining_bytes = buffer_size % sizeof(__s32); 2098d43b49eSNicolas Pitre buffer_size = buffer_size / sizeof(__s32); 2103950e033SSrinivas Pandruvada if (buffer_size) { 2113950e033SSrinivas Pandruvada for (i = 0; i < buffer_size; ++i) { 212*edb03203SSrinivas Pandruvada ret = hid_set_field(report->field[field_index], i, 21362fad137SSrinivas Pandruvada (__force __s32)cpu_to_le32(*buf32)); 214*edb03203SSrinivas Pandruvada if (ret) 215*edb03203SSrinivas Pandruvada goto done_proc; 216*edb03203SSrinivas Pandruvada 2173950e033SSrinivas Pandruvada ++buf32; 2183950e033SSrinivas Pandruvada } 2193950e033SSrinivas Pandruvada } 2203950e033SSrinivas Pandruvada if (remaining_bytes) { 2213950e033SSrinivas Pandruvada value = 0; 2223950e033SSrinivas Pandruvada memcpy(&value, (u8 *)buf32, remaining_bytes); 223*edb03203SSrinivas Pandruvada ret = hid_set_field(report->field[field_index], i, 22462fad137SSrinivas Pandruvada (__force __s32)cpu_to_le32(value)); 225*edb03203SSrinivas Pandruvada if (ret) 226*edb03203SSrinivas Pandruvada goto done_proc; 2273950e033SSrinivas Pandruvada } 228d8814272SBenjamin Tissoires hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT); 229b7966a4dSBenjamin Tissoires hid_hw_wait(hsdev->hdev); 230401ca24fSsrinivas pandruvada 231401ca24fSsrinivas pandruvada done_proc: 232401ca24fSsrinivas pandruvada mutex_unlock(&data->mutex); 233401ca24fSsrinivas pandruvada 234401ca24fSsrinivas pandruvada return ret; 235401ca24fSsrinivas pandruvada } 236401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_set_feature); 237401ca24fSsrinivas pandruvada 238401ca24fSsrinivas pandruvada int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id, 2396adc83fcSSrinivas Pandruvada u32 field_index, int buffer_size, void *buffer) 240401ca24fSsrinivas pandruvada { 241401ca24fSsrinivas pandruvada struct hid_report *report; 242401ca24fSsrinivas pandruvada struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 2436adc83fcSSrinivas Pandruvada int report_size; 244401ca24fSsrinivas pandruvada int ret = 0; 2455459ada2SSrinivas Pandruvada u8 *val_ptr; 2465459ada2SSrinivas Pandruvada int buffer_index = 0; 2475459ada2SSrinivas Pandruvada int i; 248401ca24fSsrinivas pandruvada 249143fca77SSrinivas Pandruvada memset(buffer, 0, buffer_size); 250143fca77SSrinivas Pandruvada 251401ca24fSsrinivas pandruvada mutex_lock(&data->mutex); 252401ca24fSsrinivas pandruvada report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT); 2539e891025SKees Cook if (!report || (field_index >= report->maxfield) || 2549e891025SKees Cook report->field[field_index]->report_count < 1) { 255401ca24fSsrinivas pandruvada ret = -EINVAL; 256401ca24fSsrinivas pandruvada goto done_proc; 257401ca24fSsrinivas pandruvada } 258d8814272SBenjamin Tissoires hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT); 259b7966a4dSBenjamin Tissoires hid_hw_wait(hsdev->hdev); 2606adc83fcSSrinivas Pandruvada 2616adc83fcSSrinivas Pandruvada /* calculate number of bytes required to read this field */ 2626adc83fcSSrinivas Pandruvada report_size = DIV_ROUND_UP(report->field[field_index]->report_size, 2636adc83fcSSrinivas Pandruvada 8) * 2646adc83fcSSrinivas Pandruvada report->field[field_index]->report_count; 2656adc83fcSSrinivas Pandruvada if (!report_size) { 2666adc83fcSSrinivas Pandruvada ret = -EINVAL; 2676adc83fcSSrinivas Pandruvada goto done_proc; 2686adc83fcSSrinivas Pandruvada } 2696adc83fcSSrinivas Pandruvada ret = min(report_size, buffer_size); 2705459ada2SSrinivas Pandruvada 2715459ada2SSrinivas Pandruvada val_ptr = (u8 *)report->field[field_index]->value; 2725459ada2SSrinivas Pandruvada for (i = 0; i < report->field[field_index]->report_count; ++i) { 2735459ada2SSrinivas Pandruvada if (buffer_index >= ret) 2745459ada2SSrinivas Pandruvada break; 2755459ada2SSrinivas Pandruvada 2765459ada2SSrinivas Pandruvada memcpy(&((u8 *)buffer)[buffer_index], val_ptr, 2775459ada2SSrinivas Pandruvada report->field[field_index]->report_size / 8); 2785459ada2SSrinivas Pandruvada val_ptr += sizeof(__s32); 2795459ada2SSrinivas Pandruvada buffer_index += (report->field[field_index]->report_size / 8); 2805459ada2SSrinivas Pandruvada } 281401ca24fSsrinivas pandruvada 282401ca24fSsrinivas pandruvada done_proc: 283401ca24fSsrinivas pandruvada mutex_unlock(&data->mutex); 284401ca24fSsrinivas pandruvada 285401ca24fSsrinivas pandruvada return ret; 286401ca24fSsrinivas pandruvada } 287401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_get_feature); 288401ca24fSsrinivas pandruvada 289401ca24fSsrinivas pandruvada 290401ca24fSsrinivas pandruvada int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev, 291401ca24fSsrinivas pandruvada u32 usage_id, 292b3f4737dSSrinivas Pandruvada u32 attr_usage_id, u32 report_id, 2930145b505SHans de Goede enum sensor_hub_read_flags flag, 2940145b505SHans de Goede bool is_signed) 295401ca24fSsrinivas pandruvada { 296401ca24fSsrinivas pandruvada struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 297401ca24fSsrinivas pandruvada unsigned long flags; 298401ca24fSsrinivas pandruvada struct hid_report *report; 299401ca24fSsrinivas pandruvada int ret_val = 0; 300401ca24fSsrinivas pandruvada 301b3f4737dSSrinivas Pandruvada report = sensor_hub_report(report_id, hsdev->hdev, 302b3f4737dSSrinivas Pandruvada HID_INPUT_REPORT); 303b3f4737dSSrinivas Pandruvada if (!report) 304b3f4737dSSrinivas Pandruvada return -EINVAL; 305b3f4737dSSrinivas Pandruvada 3062d94e522SSrinivas Pandruvada mutex_lock(hsdev->mutex_ptr); 307b3f4737dSSrinivas Pandruvada if (flag == SENSOR_HUB_SYNC) { 308e651a1daSSrinivas Pandruvada memset(&hsdev->pending, 0, sizeof(hsdev->pending)); 309e651a1daSSrinivas Pandruvada init_completion(&hsdev->pending.ready); 310e651a1daSSrinivas Pandruvada hsdev->pending.usage_id = usage_id; 311e651a1daSSrinivas Pandruvada hsdev->pending.attr_usage_id = attr_usage_id; 312e651a1daSSrinivas Pandruvada hsdev->pending.raw_size = 0; 313401ca24fSsrinivas pandruvada 314401ca24fSsrinivas pandruvada spin_lock_irqsave(&data->lock, flags); 315e651a1daSSrinivas Pandruvada hsdev->pending.status = true; 316f74346a0SSrinivas Pandruvada spin_unlock_irqrestore(&data->lock, flags); 317b3f4737dSSrinivas Pandruvada } 318e651a1daSSrinivas Pandruvada mutex_lock(&data->mutex); 319d8814272SBenjamin Tissoires hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT); 320e651a1daSSrinivas Pandruvada mutex_unlock(&data->mutex); 321b3f4737dSSrinivas Pandruvada if (flag == SENSOR_HUB_SYNC) { 322b3f4737dSSrinivas Pandruvada wait_for_completion_interruptible_timeout( 323b3f4737dSSrinivas Pandruvada &hsdev->pending.ready, HZ*5); 324e651a1daSSrinivas Pandruvada switch (hsdev->pending.raw_size) { 325401ca24fSsrinivas pandruvada case 1: 3260145b505SHans de Goede if (is_signed) 3270145b505SHans de Goede ret_val = *(s8 *)hsdev->pending.raw_data; 3280145b505SHans de Goede else 329e651a1daSSrinivas Pandruvada ret_val = *(u8 *)hsdev->pending.raw_data; 330401ca24fSsrinivas pandruvada break; 331401ca24fSsrinivas pandruvada case 2: 3320145b505SHans de Goede if (is_signed) 3330145b505SHans de Goede ret_val = *(s16 *)hsdev->pending.raw_data; 3340145b505SHans de Goede else 335e651a1daSSrinivas Pandruvada ret_val = *(u16 *)hsdev->pending.raw_data; 336401ca24fSsrinivas pandruvada break; 337401ca24fSsrinivas pandruvada case 4: 338e651a1daSSrinivas Pandruvada ret_val = *(u32 *)hsdev->pending.raw_data; 339401ca24fSsrinivas pandruvada break; 340401ca24fSsrinivas pandruvada default: 341401ca24fSsrinivas pandruvada ret_val = 0; 342401ca24fSsrinivas pandruvada } 343e651a1daSSrinivas Pandruvada kfree(hsdev->pending.raw_data); 344e651a1daSSrinivas Pandruvada hsdev->pending.status = false; 345b3f4737dSSrinivas Pandruvada } 3462d94e522SSrinivas Pandruvada mutex_unlock(hsdev->mutex_ptr); 347401ca24fSsrinivas pandruvada 348401ca24fSsrinivas pandruvada return ret_val; 349401ca24fSsrinivas pandruvada } 350401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value); 351401ca24fSsrinivas pandruvada 352e02cee48SSrinivas Pandruvada int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev, 353e02cee48SSrinivas Pandruvada u32 report_id, int field_index, u32 usage_id) 354e02cee48SSrinivas Pandruvada { 355e02cee48SSrinivas Pandruvada struct hid_report *report; 356e02cee48SSrinivas Pandruvada struct hid_field *field; 357e02cee48SSrinivas Pandruvada int i; 358e02cee48SSrinivas Pandruvada 359e02cee48SSrinivas Pandruvada report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT); 360e02cee48SSrinivas Pandruvada if (!report || (field_index >= report->maxfield)) 361e02cee48SSrinivas Pandruvada goto done_proc; 362e02cee48SSrinivas Pandruvada 363e02cee48SSrinivas Pandruvada field = report->field[field_index]; 364e02cee48SSrinivas Pandruvada for (i = 0; i < field->maxusage; ++i) { 365e02cee48SSrinivas Pandruvada if (field->usage[i].hid == usage_id) 366e02cee48SSrinivas Pandruvada return field->usage[i].usage_index; 367e02cee48SSrinivas Pandruvada } 368e02cee48SSrinivas Pandruvada 369e02cee48SSrinivas Pandruvada done_proc: 370e02cee48SSrinivas Pandruvada return -EINVAL; 371e02cee48SSrinivas Pandruvada } 372e02cee48SSrinivas Pandruvada EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index); 373e02cee48SSrinivas Pandruvada 374401ca24fSsrinivas pandruvada int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev, 375401ca24fSsrinivas pandruvada u8 type, 376401ca24fSsrinivas pandruvada u32 usage_id, 377401ca24fSsrinivas pandruvada u32 attr_usage_id, 378401ca24fSsrinivas pandruvada struct hid_sensor_hub_attribute_info *info) 379401ca24fSsrinivas pandruvada { 380401ca24fSsrinivas pandruvada int ret = -1; 381ca2ed12fSSrinivas Pandruvada int i; 382401ca24fSsrinivas pandruvada struct hid_report *report; 383401ca24fSsrinivas pandruvada struct hid_field *field; 384401ca24fSsrinivas pandruvada struct hid_report_enum *report_enum; 385401ca24fSsrinivas pandruvada struct hid_device *hdev = hsdev->hdev; 386401ca24fSsrinivas pandruvada 387401ca24fSsrinivas pandruvada /* Initialize with defaults */ 388401ca24fSsrinivas pandruvada info->usage_id = usage_id; 389401ca24fSsrinivas pandruvada info->attrib_id = attr_usage_id; 390401ca24fSsrinivas pandruvada info->report_id = -1; 391401ca24fSsrinivas pandruvada info->index = -1; 392401ca24fSsrinivas pandruvada info->units = -1; 393401ca24fSsrinivas pandruvada info->unit_expo = -1; 394401ca24fSsrinivas pandruvada 395401ca24fSsrinivas pandruvada report_enum = &hdev->report_enum[type]; 396401ca24fSsrinivas pandruvada list_for_each_entry(report, &report_enum->report_list, list) { 397401ca24fSsrinivas pandruvada for (i = 0; i < report->maxfield; ++i) { 398401ca24fSsrinivas pandruvada field = report->field[i]; 399ca2ed12fSSrinivas Pandruvada if (field->maxusage) { 400401ca24fSsrinivas pandruvada if (field->physical == usage_id && 401ca2ed12fSSrinivas Pandruvada (field->logical == attr_usage_id || 402ca2ed12fSSrinivas Pandruvada field->usage[0].hid == 403ca2ed12fSSrinivas Pandruvada attr_usage_id) && 404ca2ed12fSSrinivas Pandruvada (field->usage[0].collection_index >= 405ca2ed12fSSrinivas Pandruvada hsdev->start_collection_index) && 406ca2ed12fSSrinivas Pandruvada (field->usage[0].collection_index < 407ca2ed12fSSrinivas Pandruvada hsdev->end_collection_index)) { 408ca2ed12fSSrinivas Pandruvada 409ca2ed12fSSrinivas Pandruvada sensor_hub_fill_attr_info(info, i, 410ca2ed12fSSrinivas Pandruvada report->id, 4119f740ffaSSrinivas Pandruvada field); 412401ca24fSsrinivas pandruvada ret = 0; 413401ca24fSsrinivas pandruvada break; 414401ca24fSsrinivas pandruvada } 415401ca24fSsrinivas pandruvada } 416401ca24fSsrinivas pandruvada } 417401ca24fSsrinivas pandruvada 418ca2ed12fSSrinivas Pandruvada } 419ca2ed12fSSrinivas Pandruvada 420401ca24fSsrinivas pandruvada return ret; 421401ca24fSsrinivas pandruvada } 422401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info); 423401ca24fSsrinivas pandruvada 424401ca24fSsrinivas pandruvada #ifdef CONFIG_PM 425401ca24fSsrinivas pandruvada static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message) 426401ca24fSsrinivas pandruvada { 427401ca24fSsrinivas pandruvada struct sensor_hub_data *pdata = hid_get_drvdata(hdev); 428401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks_list *callback; 4290ccf091dSJiri Kosina unsigned long flags; 430401ca24fSsrinivas pandruvada 431401ca24fSsrinivas pandruvada hid_dbg(hdev, " sensor_hub_suspend\n"); 4320ccf091dSJiri Kosina spin_lock_irqsave(&pdata->dyn_callback_lock, flags); 433401ca24fSsrinivas pandruvada list_for_each_entry(callback, &pdata->dyn_callback_list, list) { 434401ca24fSsrinivas pandruvada if (callback->usage_callback->suspend) 435401ca24fSsrinivas pandruvada callback->usage_callback->suspend( 436ca2ed12fSSrinivas Pandruvada callback->hsdev, callback->priv); 437401ca24fSsrinivas pandruvada } 4380ccf091dSJiri Kosina spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 439401ca24fSsrinivas pandruvada 440401ca24fSsrinivas pandruvada return 0; 441401ca24fSsrinivas pandruvada } 442401ca24fSsrinivas pandruvada 443401ca24fSsrinivas pandruvada static int sensor_hub_resume(struct hid_device *hdev) 444401ca24fSsrinivas pandruvada { 445401ca24fSsrinivas pandruvada struct sensor_hub_data *pdata = hid_get_drvdata(hdev); 446401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks_list *callback; 4470ccf091dSJiri Kosina unsigned long flags; 448401ca24fSsrinivas pandruvada 449401ca24fSsrinivas pandruvada hid_dbg(hdev, " sensor_hub_resume\n"); 4500ccf091dSJiri Kosina spin_lock_irqsave(&pdata->dyn_callback_lock, flags); 451401ca24fSsrinivas pandruvada list_for_each_entry(callback, &pdata->dyn_callback_list, list) { 452401ca24fSsrinivas pandruvada if (callback->usage_callback->resume) 453401ca24fSsrinivas pandruvada callback->usage_callback->resume( 454ca2ed12fSSrinivas Pandruvada callback->hsdev, callback->priv); 455401ca24fSsrinivas pandruvada } 4560ccf091dSJiri Kosina spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); 457401ca24fSsrinivas pandruvada 458401ca24fSsrinivas pandruvada return 0; 459401ca24fSsrinivas pandruvada } 460401ca24fSsrinivas pandruvada 461401ca24fSsrinivas pandruvada static int sensor_hub_reset_resume(struct hid_device *hdev) 462401ca24fSsrinivas pandruvada { 463401ca24fSsrinivas pandruvada return 0; 464401ca24fSsrinivas pandruvada } 465401ca24fSsrinivas pandruvada #endif 4665902fde1SAndy Shevchenko 467401ca24fSsrinivas pandruvada /* 468401ca24fSsrinivas pandruvada * Handle raw report as sent by device 469401ca24fSsrinivas pandruvada */ 470401ca24fSsrinivas pandruvada static int sensor_hub_raw_event(struct hid_device *hdev, 471401ca24fSsrinivas pandruvada struct hid_report *report, u8 *raw_data, int size) 472401ca24fSsrinivas pandruvada { 473401ca24fSsrinivas pandruvada int i; 474401ca24fSsrinivas pandruvada u8 *ptr; 475401ca24fSsrinivas pandruvada int sz; 476401ca24fSsrinivas pandruvada struct sensor_hub_data *pdata = hid_get_drvdata(hdev); 477401ca24fSsrinivas pandruvada unsigned long flags; 478401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks *callback = NULL; 479401ca24fSsrinivas pandruvada struct hid_collection *collection = NULL; 480401ca24fSsrinivas pandruvada void *priv = NULL; 481ca2ed12fSSrinivas Pandruvada struct hid_sensor_hub_device *hsdev = NULL; 482401ca24fSsrinivas pandruvada 483401ca24fSsrinivas pandruvada hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n", 484401ca24fSsrinivas pandruvada report->id, size, report->type); 485401ca24fSsrinivas pandruvada hid_dbg(hdev, "maxfield:%d\n", report->maxfield); 486401ca24fSsrinivas pandruvada if (report->type != HID_INPUT_REPORT) 487401ca24fSsrinivas pandruvada return 1; 488401ca24fSsrinivas pandruvada 489401ca24fSsrinivas pandruvada ptr = raw_data; 49034a9fa20SPablo Ceballos if (report->id) 491401ca24fSsrinivas pandruvada ptr++; /* Skip report id */ 492401ca24fSsrinivas pandruvada 493401ca24fSsrinivas pandruvada spin_lock_irqsave(&pdata->lock, flags); 494401ca24fSsrinivas pandruvada 495401ca24fSsrinivas pandruvada for (i = 0; i < report->maxfield; ++i) { 496401ca24fSsrinivas pandruvada hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n", 497401ca24fSsrinivas pandruvada i, report->field[i]->usage->collection_index, 498401ca24fSsrinivas pandruvada report->field[i]->usage->hid, 499d4b1bba7SSrinivas Pandruvada (report->field[i]->report_size * 500d4b1bba7SSrinivas Pandruvada report->field[i]->report_count)/8); 501d4b1bba7SSrinivas Pandruvada sz = (report->field[i]->report_size * 502d4b1bba7SSrinivas Pandruvada report->field[i]->report_count)/8; 503401ca24fSsrinivas pandruvada collection = &hdev->collection[ 504401ca24fSsrinivas pandruvada report->field[i]->usage->collection_index]; 505401ca24fSsrinivas pandruvada hid_dbg(hdev, "collection->usage %x\n", 506401ca24fSsrinivas pandruvada collection->usage); 507ca2ed12fSSrinivas Pandruvada 508ca2ed12fSSrinivas Pandruvada callback = sensor_hub_get_callback(hdev, 509401ca24fSsrinivas pandruvada report->field[i]->physical, 510ca2ed12fSSrinivas Pandruvada report->field[i]->usage[0].collection_index, 511ca2ed12fSSrinivas Pandruvada &hsdev, &priv); 512e651a1daSSrinivas Pandruvada if (!callback) { 513e651a1daSSrinivas Pandruvada ptr += sz; 514e651a1daSSrinivas Pandruvada continue; 515e651a1daSSrinivas Pandruvada } 516eb964833SSrinivas Pandruvada if (hsdev->pending.status && (hsdev->pending.attr_usage_id == 517eb964833SSrinivas Pandruvada report->field[i]->usage->hid || 518eb964833SSrinivas Pandruvada hsdev->pending.attr_usage_id == 519eb964833SSrinivas Pandruvada report->field[i]->logical)) { 520e651a1daSSrinivas Pandruvada hid_dbg(hdev, "data was pending ...\n"); 521e651a1daSSrinivas Pandruvada hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC); 522e651a1daSSrinivas Pandruvada if (hsdev->pending.raw_data) 523e651a1daSSrinivas Pandruvada hsdev->pending.raw_size = sz; 524e651a1daSSrinivas Pandruvada else 525e651a1daSSrinivas Pandruvada hsdev->pending.raw_size = 0; 526e651a1daSSrinivas Pandruvada complete(&hsdev->pending.ready); 527e651a1daSSrinivas Pandruvada } 528e651a1daSSrinivas Pandruvada if (callback->capture_sample) { 529401ca24fSsrinivas pandruvada if (report->field[i]->logical) 530ca2ed12fSSrinivas Pandruvada callback->capture_sample(hsdev, 531401ca24fSsrinivas pandruvada report->field[i]->logical, sz, ptr, 532401ca24fSsrinivas pandruvada callback->pdev); 533401ca24fSsrinivas pandruvada else 534ca2ed12fSSrinivas Pandruvada callback->capture_sample(hsdev, 535401ca24fSsrinivas pandruvada report->field[i]->usage->hid, sz, ptr, 536401ca24fSsrinivas pandruvada callback->pdev); 537401ca24fSsrinivas pandruvada } 538401ca24fSsrinivas pandruvada ptr += sz; 539401ca24fSsrinivas pandruvada } 540401ca24fSsrinivas pandruvada if (callback && collection && callback->send_event) 541ca2ed12fSSrinivas Pandruvada callback->send_event(hsdev, collection->usage, 542401ca24fSsrinivas pandruvada callback->pdev); 543401ca24fSsrinivas pandruvada spin_unlock_irqrestore(&pdata->lock, flags); 544401ca24fSsrinivas pandruvada 545401ca24fSsrinivas pandruvada return 1; 546401ca24fSsrinivas pandruvada } 547401ca24fSsrinivas pandruvada 5481df3a401SSrinivas Pandruvada int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev) 5491df3a401SSrinivas Pandruvada { 5501df3a401SSrinivas Pandruvada int ret = 0; 5511df3a401SSrinivas Pandruvada struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 5521df3a401SSrinivas Pandruvada 5531df3a401SSrinivas Pandruvada mutex_lock(&data->mutex); 554ca2ed12fSSrinivas Pandruvada if (!data->ref_cnt) { 5551df3a401SSrinivas Pandruvada ret = hid_hw_open(hsdev->hdev); 5561df3a401SSrinivas Pandruvada if (ret) { 5571df3a401SSrinivas Pandruvada hid_err(hsdev->hdev, "failed to open hid device\n"); 5581df3a401SSrinivas Pandruvada mutex_unlock(&data->mutex); 5591df3a401SSrinivas Pandruvada return ret; 5601df3a401SSrinivas Pandruvada } 5611df3a401SSrinivas Pandruvada } 562ca2ed12fSSrinivas Pandruvada data->ref_cnt++; 5631df3a401SSrinivas Pandruvada mutex_unlock(&data->mutex); 5641df3a401SSrinivas Pandruvada 5651df3a401SSrinivas Pandruvada return ret; 5661df3a401SSrinivas Pandruvada } 5671df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_open); 5681df3a401SSrinivas Pandruvada 5691df3a401SSrinivas Pandruvada void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev) 5701df3a401SSrinivas Pandruvada { 5711df3a401SSrinivas Pandruvada struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); 5721df3a401SSrinivas Pandruvada 5731df3a401SSrinivas Pandruvada mutex_lock(&data->mutex); 574ca2ed12fSSrinivas Pandruvada data->ref_cnt--; 575ca2ed12fSSrinivas Pandruvada if (!data->ref_cnt) 5761df3a401SSrinivas Pandruvada hid_hw_close(hsdev->hdev); 5771df3a401SSrinivas Pandruvada mutex_unlock(&data->mutex); 5781df3a401SSrinivas Pandruvada } 5791df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_close); 5801df3a401SSrinivas Pandruvada 581ade573ebSHans de Goede static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc, 582ade573ebSHans de Goede unsigned int *rsize) 583ade573ebSHans de Goede { 584ade573ebSHans de Goede /* 585ade573ebSHans de Goede * Checks if the report descriptor of Thinkpad Helix 2 has a logical 586ade573ebSHans de Goede * minimum for magnetic flux axis greater than the maximum. 587ade573ebSHans de Goede */ 588ade573ebSHans de Goede if (hdev->product == USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA && 589ade573ebSHans de Goede *rsize == 2558 && rdesc[913] == 0x17 && rdesc[914] == 0x40 && 590ade573ebSHans de Goede rdesc[915] == 0x81 && rdesc[916] == 0x08 && 591ade573ebSHans de Goede rdesc[917] == 0x00 && rdesc[918] == 0x27 && 592ade573ebSHans de Goede rdesc[921] == 0x07 && rdesc[922] == 0x00) { 593ade573ebSHans de Goede /* Sets negative logical minimum for mag x, y and z */ 594ade573ebSHans de Goede rdesc[914] = rdesc[935] = rdesc[956] = 0xc0; 595ade573ebSHans de Goede rdesc[915] = rdesc[936] = rdesc[957] = 0x7e; 596ade573ebSHans de Goede rdesc[916] = rdesc[937] = rdesc[958] = 0xf7; 597ade573ebSHans de Goede rdesc[917] = rdesc[938] = rdesc[959] = 0xff; 598ade573ebSHans de Goede } 599ade573ebSHans de Goede 600ade573ebSHans de Goede return rdesc; 601ade573ebSHans de Goede } 602ade573ebSHans de Goede 603401ca24fSsrinivas pandruvada static int sensor_hub_probe(struct hid_device *hdev, 604401ca24fSsrinivas pandruvada const struct hid_device_id *id) 605401ca24fSsrinivas pandruvada { 606401ca24fSsrinivas pandruvada int ret; 607401ca24fSsrinivas pandruvada struct sensor_hub_data *sd; 608401ca24fSsrinivas pandruvada int i; 609401ca24fSsrinivas pandruvada char *name; 610401ca24fSsrinivas pandruvada int dev_cnt; 611ca2ed12fSSrinivas Pandruvada struct hid_sensor_hub_device *hsdev; 612ca2ed12fSSrinivas Pandruvada struct hid_sensor_hub_device *last_hsdev = NULL; 613cb67126fSSrinivas Pandruvada struct hid_sensor_hub_device *collection_hsdev = NULL; 614401ca24fSsrinivas pandruvada 615905cc199SAndy Shevchenko sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL); 616401ca24fSsrinivas pandruvada if (!sd) { 617401ca24fSsrinivas pandruvada hid_err(hdev, "cannot allocate Sensor data\n"); 618401ca24fSsrinivas pandruvada return -ENOMEM; 619401ca24fSsrinivas pandruvada } 620ca2ed12fSSrinivas Pandruvada 621401ca24fSsrinivas pandruvada hid_set_drvdata(hdev, sd); 622ca2ed12fSSrinivas Pandruvada 623401ca24fSsrinivas pandruvada spin_lock_init(&sd->lock); 624401ca24fSsrinivas pandruvada spin_lock_init(&sd->dyn_callback_lock); 625401ca24fSsrinivas pandruvada mutex_init(&sd->mutex); 626401ca24fSsrinivas pandruvada ret = hid_parse(hdev); 627401ca24fSsrinivas pandruvada if (ret) { 628401ca24fSsrinivas pandruvada hid_err(hdev, "parse failed\n"); 629905cc199SAndy Shevchenko return ret; 630401ca24fSsrinivas pandruvada } 631401ca24fSsrinivas pandruvada INIT_LIST_HEAD(&hdev->inputs); 632401ca24fSsrinivas pandruvada 633401ca24fSsrinivas pandruvada ret = hid_hw_start(hdev, 0); 634401ca24fSsrinivas pandruvada if (ret) { 635401ca24fSsrinivas pandruvada hid_err(hdev, "hw start failed\n"); 636905cc199SAndy Shevchenko return ret; 637401ca24fSsrinivas pandruvada } 638401ca24fSsrinivas pandruvada INIT_LIST_HEAD(&sd->dyn_callback_list); 639401ca24fSsrinivas pandruvada sd->hid_sensor_client_cnt = 0; 640401ca24fSsrinivas pandruvada 641ca2ed12fSSrinivas Pandruvada dev_cnt = sensor_hub_get_physical_device_count(hdev); 642401ca24fSsrinivas pandruvada if (dev_cnt > HID_MAX_PHY_DEVICES) { 643401ca24fSsrinivas pandruvada hid_err(hdev, "Invalid Physical device count\n"); 644401ca24fSsrinivas pandruvada ret = -EINVAL; 6451df3a401SSrinivas Pandruvada goto err_stop_hw; 646401ca24fSsrinivas pandruvada } 647a86854d0SKees Cook sd->hid_sensor_hub_client_devs = devm_kcalloc(&hdev->dev, 648a86854d0SKees Cook dev_cnt, 649401ca24fSsrinivas pandruvada sizeof(struct mfd_cell), 650401ca24fSsrinivas pandruvada GFP_KERNEL); 651401ca24fSsrinivas pandruvada if (sd->hid_sensor_hub_client_devs == NULL) { 652f2f13a68SAxel Lin hid_err(hdev, "Failed to allocate memory for mfd cells\n"); 653401ca24fSsrinivas pandruvada ret = -ENOMEM; 6541df3a401SSrinivas Pandruvada goto err_stop_hw; 655401ca24fSsrinivas pandruvada } 656ca2ed12fSSrinivas Pandruvada 657ca2ed12fSSrinivas Pandruvada for (i = 0; i < hdev->maxcollection; ++i) { 658ca2ed12fSSrinivas Pandruvada struct hid_collection *collection = &hdev->collection[i]; 659ca2ed12fSSrinivas Pandruvada 660cb67126fSSrinivas Pandruvada if (collection->type == HID_COLLECTION_PHYSICAL || 661cb67126fSSrinivas Pandruvada collection->type == HID_COLLECTION_APPLICATION) { 662ca2ed12fSSrinivas Pandruvada 6635be5db24SHimangi Saraogi hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev), 6645be5db24SHimangi Saraogi GFP_KERNEL); 665ca2ed12fSSrinivas Pandruvada if (!hsdev) { 666ca2ed12fSSrinivas Pandruvada hid_err(hdev, "cannot allocate hid_sensor_hub_device\n"); 667ca2ed12fSSrinivas Pandruvada ret = -ENOMEM; 6685be5db24SHimangi Saraogi goto err_stop_hw; 669ca2ed12fSSrinivas Pandruvada } 670ca2ed12fSSrinivas Pandruvada hsdev->hdev = hdev; 671ca2ed12fSSrinivas Pandruvada hsdev->vendor_id = hdev->vendor; 672ca2ed12fSSrinivas Pandruvada hsdev->product_id = hdev->product; 673e651a1daSSrinivas Pandruvada hsdev->usage = collection->usage; 6742d94e522SSrinivas Pandruvada hsdev->mutex_ptr = devm_kzalloc(&hdev->dev, 6752d94e522SSrinivas Pandruvada sizeof(struct mutex), 6762d94e522SSrinivas Pandruvada GFP_KERNEL); 6772d94e522SSrinivas Pandruvada if (!hsdev->mutex_ptr) { 6782d94e522SSrinivas Pandruvada ret = -ENOMEM; 6792d94e522SSrinivas Pandruvada goto err_stop_hw; 6802d94e522SSrinivas Pandruvada } 6812d94e522SSrinivas Pandruvada mutex_init(hsdev->mutex_ptr); 682ca2ed12fSSrinivas Pandruvada hsdev->start_collection_index = i; 683ca2ed12fSSrinivas Pandruvada if (last_hsdev) 684ca2ed12fSSrinivas Pandruvada last_hsdev->end_collection_index = i; 685ca2ed12fSSrinivas Pandruvada last_hsdev = hsdev; 6865be5db24SHimangi Saraogi name = devm_kasprintf(&hdev->dev, GFP_KERNEL, 6875be5db24SHimangi Saraogi "HID-SENSOR-%x", 688ca2ed12fSSrinivas Pandruvada collection->usage); 689401ca24fSsrinivas pandruvada if (name == NULL) { 690f2f13a68SAxel Lin hid_err(hdev, "Failed MFD device name\n"); 691401ca24fSsrinivas pandruvada ret = -ENOMEM; 6925be5db24SHimangi Saraogi goto err_stop_hw; 693401ca24fSsrinivas pandruvada } 694401ca24fSsrinivas pandruvada sd->hid_sensor_hub_client_devs[ 695401ca24fSsrinivas pandruvada sd->hid_sensor_client_cnt].name = name; 696401ca24fSsrinivas pandruvada sd->hid_sensor_hub_client_devs[ 697401ca24fSsrinivas pandruvada sd->hid_sensor_client_cnt].platform_data = 698ca2ed12fSSrinivas Pandruvada hsdev; 699401ca24fSsrinivas pandruvada sd->hid_sensor_hub_client_devs[ 700401ca24fSsrinivas pandruvada sd->hid_sensor_client_cnt].pdata_size = 701ca2ed12fSSrinivas Pandruvada sizeof(*hsdev); 702ca2ed12fSSrinivas Pandruvada hid_dbg(hdev, "Adding %s:%d\n", name, 703ca2ed12fSSrinivas Pandruvada hsdev->start_collection_index); 704401ca24fSsrinivas pandruvada sd->hid_sensor_client_cnt++; 705cb67126fSSrinivas Pandruvada if (collection_hsdev) 706cb67126fSSrinivas Pandruvada collection_hsdev->end_collection_index = i; 707cb67126fSSrinivas Pandruvada if (collection->type == HID_COLLECTION_APPLICATION && 708cb67126fSSrinivas Pandruvada collection->usage == HID_USAGE_SENSOR_COLLECTION) 709cb67126fSSrinivas Pandruvada collection_hsdev = hsdev; 710401ca24fSsrinivas pandruvada } 711401ca24fSsrinivas pandruvada } 712ca2ed12fSSrinivas Pandruvada if (last_hsdev) 713ca2ed12fSSrinivas Pandruvada last_hsdev->end_collection_index = i; 714cb67126fSSrinivas Pandruvada if (collection_hsdev) 715cb67126fSSrinivas Pandruvada collection_hsdev->end_collection_index = i; 716ca2ed12fSSrinivas Pandruvada 71716b5fe29SJohan Hovold ret = mfd_add_hotplug_devices(&hdev->dev, 71816b5fe29SJohan Hovold sd->hid_sensor_hub_client_devs, 71916b5fe29SJohan Hovold sd->hid_sensor_client_cnt); 720401ca24fSsrinivas pandruvada if (ret < 0) 7215be5db24SHimangi Saraogi goto err_stop_hw; 722401ca24fSsrinivas pandruvada 723401ca24fSsrinivas pandruvada return ret; 724401ca24fSsrinivas pandruvada 725401ca24fSsrinivas pandruvada err_stop_hw: 726401ca24fSsrinivas pandruvada hid_hw_stop(hdev); 727401ca24fSsrinivas pandruvada 728401ca24fSsrinivas pandruvada return ret; 729401ca24fSsrinivas pandruvada } 730401ca24fSsrinivas pandruvada 731401ca24fSsrinivas pandruvada static void sensor_hub_remove(struct hid_device *hdev) 732401ca24fSsrinivas pandruvada { 733401ca24fSsrinivas pandruvada struct sensor_hub_data *data = hid_get_drvdata(hdev); 734401ca24fSsrinivas pandruvada unsigned long flags; 735e651a1daSSrinivas Pandruvada int i; 736401ca24fSsrinivas pandruvada 737401ca24fSsrinivas pandruvada hid_dbg(hdev, " hardware removed\n"); 738401ca24fSsrinivas pandruvada hid_hw_close(hdev); 739f2f13a68SAxel Lin hid_hw_stop(hdev); 740401ca24fSsrinivas pandruvada spin_lock_irqsave(&data->lock, flags); 741e651a1daSSrinivas Pandruvada for (i = 0; i < data->hid_sensor_client_cnt; ++i) { 742e651a1daSSrinivas Pandruvada struct hid_sensor_hub_device *hsdev = 743e651a1daSSrinivas Pandruvada data->hid_sensor_hub_client_devs[i].platform_data; 744e651a1daSSrinivas Pandruvada if (hsdev->pending.status) 745e651a1daSSrinivas Pandruvada complete(&hsdev->pending.ready); 746e651a1daSSrinivas Pandruvada } 747401ca24fSsrinivas pandruvada spin_unlock_irqrestore(&data->lock, flags); 748401ca24fSsrinivas pandruvada mfd_remove_devices(&hdev->dev); 749401ca24fSsrinivas pandruvada mutex_destroy(&data->mutex); 750401ca24fSsrinivas pandruvada } 751401ca24fSsrinivas pandruvada 752401ca24fSsrinivas pandruvada static const struct hid_device_id sensor_hub_devices[] = { 7530d1e4b02SMika Westerberg { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID, 7540d1e4b02SMika Westerberg HID_ANY_ID) }, 755401ca24fSsrinivas pandruvada { } 756401ca24fSsrinivas pandruvada }; 757401ca24fSsrinivas pandruvada MODULE_DEVICE_TABLE(hid, sensor_hub_devices); 758401ca24fSsrinivas pandruvada 759401ca24fSsrinivas pandruvada static struct hid_driver sensor_hub_driver = { 760401ca24fSsrinivas pandruvada .name = "hid-sensor-hub", 761401ca24fSsrinivas pandruvada .id_table = sensor_hub_devices, 762401ca24fSsrinivas pandruvada .probe = sensor_hub_probe, 763401ca24fSsrinivas pandruvada .remove = sensor_hub_remove, 764401ca24fSsrinivas pandruvada .raw_event = sensor_hub_raw_event, 765ade573ebSHans de Goede .report_fixup = sensor_hub_report_fixup, 766401ca24fSsrinivas pandruvada #ifdef CONFIG_PM 767401ca24fSsrinivas pandruvada .suspend = sensor_hub_suspend, 768401ca24fSsrinivas pandruvada .resume = sensor_hub_resume, 769401ca24fSsrinivas pandruvada .reset_resume = sensor_hub_reset_resume, 770401ca24fSsrinivas pandruvada #endif 771401ca24fSsrinivas pandruvada }; 772f425458eSH Hartley Sweeten module_hid_driver(sensor_hub_driver); 773401ca24fSsrinivas pandruvada 774401ca24fSsrinivas pandruvada MODULE_DESCRIPTION("HID Sensor Hub driver"); 775401ca24fSsrinivas pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); 776401ca24fSsrinivas pandruvada MODULE_LICENSE("GPL"); 777