xref: /linux/drivers/hid/hid-sensor-hub.c (revision 8e2f79f41a5d1b1a4a53ec524eb7609ca89f3c65)
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) {
212edb03203SSrinivas Pandruvada 			ret = hid_set_field(report->field[field_index], i,
21362fad137SSrinivas Pandruvada 					    (__force __s32)cpu_to_le32(*buf32));
214edb03203SSrinivas Pandruvada 			if (ret)
215edb03203SSrinivas Pandruvada 				goto done_proc;
216edb03203SSrinivas 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);
223edb03203SSrinivas Pandruvada 		ret = hid_set_field(report->field[field_index], i,
22462fad137SSrinivas Pandruvada 				    (__force __s32)cpu_to_le32(value));
225edb03203SSrinivas Pandruvada 		if (ret)
226edb03203SSrinivas 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) {
400e04955dbSRonald Tschalär 				if ((field->physical == usage_id ||
401e04955dbSRonald Tschalär 				     field->application == usage_id) &&
402ca2ed12fSSrinivas Pandruvada 					(field->logical == attr_usage_id ||
403ca2ed12fSSrinivas Pandruvada 					field->usage[0].hid ==
404ca2ed12fSSrinivas Pandruvada 							attr_usage_id) &&
405ca2ed12fSSrinivas Pandruvada 					(field->usage[0].collection_index >=
406ca2ed12fSSrinivas Pandruvada 					hsdev->start_collection_index) &&
407ca2ed12fSSrinivas Pandruvada 					(field->usage[0].collection_index <
408ca2ed12fSSrinivas Pandruvada 					hsdev->end_collection_index)) {
409ca2ed12fSSrinivas Pandruvada 
410ca2ed12fSSrinivas Pandruvada 					sensor_hub_fill_attr_info(info, i,
411ca2ed12fSSrinivas Pandruvada 								report->id,
4129f740ffaSSrinivas Pandruvada 								field);
413401ca24fSsrinivas pandruvada 					ret = 0;
414401ca24fSsrinivas pandruvada 					break;
415401ca24fSsrinivas pandruvada 				}
416401ca24fSsrinivas pandruvada 			}
417401ca24fSsrinivas pandruvada 		}
418401ca24fSsrinivas pandruvada 
419ca2ed12fSSrinivas Pandruvada 	}
420ca2ed12fSSrinivas Pandruvada 
421401ca24fSsrinivas pandruvada 	return ret;
422401ca24fSsrinivas pandruvada }
423401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
424401ca24fSsrinivas pandruvada 
425401ca24fSsrinivas pandruvada #ifdef CONFIG_PM
426401ca24fSsrinivas pandruvada static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
427401ca24fSsrinivas pandruvada {
428401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
429401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
4300ccf091dSJiri Kosina 	unsigned long flags;
431401ca24fSsrinivas pandruvada 
432401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " sensor_hub_suspend\n");
4330ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
434401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
435401ca24fSsrinivas pandruvada 		if (callback->usage_callback->suspend)
436401ca24fSsrinivas pandruvada 			callback->usage_callback->suspend(
437ca2ed12fSSrinivas Pandruvada 					callback->hsdev, callback->priv);
438401ca24fSsrinivas pandruvada 	}
4390ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
440401ca24fSsrinivas pandruvada 
441401ca24fSsrinivas pandruvada 	return 0;
442401ca24fSsrinivas pandruvada }
443401ca24fSsrinivas pandruvada 
444401ca24fSsrinivas pandruvada static int sensor_hub_resume(struct hid_device *hdev)
445401ca24fSsrinivas pandruvada {
446401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
447401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
4480ccf091dSJiri Kosina 	unsigned long flags;
449401ca24fSsrinivas pandruvada 
450401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " sensor_hub_resume\n");
4510ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
452401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
453401ca24fSsrinivas pandruvada 		if (callback->usage_callback->resume)
454401ca24fSsrinivas pandruvada 			callback->usage_callback->resume(
455ca2ed12fSSrinivas Pandruvada 					callback->hsdev, callback->priv);
456401ca24fSsrinivas pandruvada 	}
4570ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
458401ca24fSsrinivas pandruvada 
459401ca24fSsrinivas pandruvada 	return 0;
460401ca24fSsrinivas pandruvada }
461401ca24fSsrinivas pandruvada 
462401ca24fSsrinivas pandruvada static int sensor_hub_reset_resume(struct hid_device *hdev)
463401ca24fSsrinivas pandruvada {
464401ca24fSsrinivas pandruvada 	return 0;
465401ca24fSsrinivas pandruvada }
466401ca24fSsrinivas pandruvada #endif
4675902fde1SAndy Shevchenko 
468401ca24fSsrinivas pandruvada /*
469401ca24fSsrinivas pandruvada  * Handle raw report as sent by device
470401ca24fSsrinivas pandruvada  */
471401ca24fSsrinivas pandruvada static int sensor_hub_raw_event(struct hid_device *hdev,
472401ca24fSsrinivas pandruvada 		struct hid_report *report, u8 *raw_data, int size)
473401ca24fSsrinivas pandruvada {
474401ca24fSsrinivas pandruvada 	int i;
475401ca24fSsrinivas pandruvada 	u8 *ptr;
476401ca24fSsrinivas pandruvada 	int sz;
477401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
478401ca24fSsrinivas pandruvada 	unsigned long flags;
479401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks *callback = NULL;
480401ca24fSsrinivas pandruvada 	struct hid_collection *collection = NULL;
481401ca24fSsrinivas pandruvada 	void *priv = NULL;
482ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev = NULL;
483401ca24fSsrinivas pandruvada 
484401ca24fSsrinivas pandruvada 	hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
485401ca24fSsrinivas pandruvada 			 report->id, size, report->type);
486401ca24fSsrinivas pandruvada 	hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
487401ca24fSsrinivas pandruvada 	if (report->type != HID_INPUT_REPORT)
488401ca24fSsrinivas pandruvada 		return 1;
489401ca24fSsrinivas pandruvada 
490401ca24fSsrinivas pandruvada 	ptr = raw_data;
49134a9fa20SPablo Ceballos 	if (report->id)
492401ca24fSsrinivas pandruvada 		ptr++; /* Skip report id */
493401ca24fSsrinivas pandruvada 
494401ca24fSsrinivas pandruvada 	spin_lock_irqsave(&pdata->lock, flags);
495401ca24fSsrinivas pandruvada 
496401ca24fSsrinivas pandruvada 	for (i = 0; i < report->maxfield; ++i) {
497401ca24fSsrinivas pandruvada 		hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
498401ca24fSsrinivas pandruvada 				i, report->field[i]->usage->collection_index,
499401ca24fSsrinivas pandruvada 				report->field[i]->usage->hid,
500d4b1bba7SSrinivas Pandruvada 				(report->field[i]->report_size *
501d4b1bba7SSrinivas Pandruvada 					report->field[i]->report_count)/8);
502d4b1bba7SSrinivas Pandruvada 		sz = (report->field[i]->report_size *
503d4b1bba7SSrinivas Pandruvada 					report->field[i]->report_count)/8;
504401ca24fSsrinivas pandruvada 		collection = &hdev->collection[
505401ca24fSsrinivas pandruvada 				report->field[i]->usage->collection_index];
506401ca24fSsrinivas pandruvada 		hid_dbg(hdev, "collection->usage %x\n",
507401ca24fSsrinivas pandruvada 					collection->usage);
508ca2ed12fSSrinivas Pandruvada 
509ca2ed12fSSrinivas Pandruvada 		callback = sensor_hub_get_callback(hdev,
510e04955dbSRonald Tschalär 				report->field[i]->physical ? report->field[i]->physical :
511e04955dbSRonald Tschalär 							     report->field[i]->application,
512ca2ed12fSSrinivas Pandruvada 				report->field[i]->usage[0].collection_index,
513ca2ed12fSSrinivas Pandruvada 				&hsdev, &priv);
514e651a1daSSrinivas Pandruvada 		if (!callback) {
515e651a1daSSrinivas Pandruvada 			ptr += sz;
516e651a1daSSrinivas Pandruvada 			continue;
517e651a1daSSrinivas Pandruvada 		}
518eb964833SSrinivas Pandruvada 		if (hsdev->pending.status && (hsdev->pending.attr_usage_id ==
519eb964833SSrinivas Pandruvada 					      report->field[i]->usage->hid ||
520eb964833SSrinivas Pandruvada 					      hsdev->pending.attr_usage_id ==
521eb964833SSrinivas Pandruvada 					      report->field[i]->logical)) {
522e651a1daSSrinivas Pandruvada 			hid_dbg(hdev, "data was pending ...\n");
523e651a1daSSrinivas Pandruvada 			hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
524e651a1daSSrinivas Pandruvada 			if (hsdev->pending.raw_data)
525e651a1daSSrinivas Pandruvada 				hsdev->pending.raw_size = sz;
526e651a1daSSrinivas Pandruvada 			else
527e651a1daSSrinivas Pandruvada 				hsdev->pending.raw_size = 0;
528e651a1daSSrinivas Pandruvada 			complete(&hsdev->pending.ready);
529e651a1daSSrinivas Pandruvada 		}
530e651a1daSSrinivas Pandruvada 		if (callback->capture_sample) {
531401ca24fSsrinivas pandruvada 			if (report->field[i]->logical)
532ca2ed12fSSrinivas Pandruvada 				callback->capture_sample(hsdev,
533401ca24fSsrinivas pandruvada 					report->field[i]->logical, sz, ptr,
534401ca24fSsrinivas pandruvada 					callback->pdev);
535401ca24fSsrinivas pandruvada 			else
536ca2ed12fSSrinivas Pandruvada 				callback->capture_sample(hsdev,
537401ca24fSsrinivas pandruvada 					report->field[i]->usage->hid, sz, ptr,
538401ca24fSsrinivas pandruvada 					callback->pdev);
539401ca24fSsrinivas pandruvada 		}
540401ca24fSsrinivas pandruvada 		ptr += sz;
541401ca24fSsrinivas pandruvada 	}
542401ca24fSsrinivas pandruvada 	if (callback && collection && callback->send_event)
543ca2ed12fSSrinivas Pandruvada 		callback->send_event(hsdev, collection->usage,
544401ca24fSsrinivas pandruvada 				callback->pdev);
545401ca24fSsrinivas pandruvada 	spin_unlock_irqrestore(&pdata->lock, flags);
546401ca24fSsrinivas pandruvada 
547401ca24fSsrinivas pandruvada 	return 1;
548401ca24fSsrinivas pandruvada }
549401ca24fSsrinivas pandruvada 
5501df3a401SSrinivas Pandruvada int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
5511df3a401SSrinivas Pandruvada {
5521df3a401SSrinivas Pandruvada 	int ret = 0;
5531df3a401SSrinivas Pandruvada 	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
5541df3a401SSrinivas Pandruvada 
5551df3a401SSrinivas Pandruvada 	mutex_lock(&data->mutex);
556ca2ed12fSSrinivas Pandruvada 	if (!data->ref_cnt) {
5571df3a401SSrinivas Pandruvada 		ret = hid_hw_open(hsdev->hdev);
5581df3a401SSrinivas Pandruvada 		if (ret) {
5591df3a401SSrinivas Pandruvada 			hid_err(hsdev->hdev, "failed to open hid device\n");
5601df3a401SSrinivas Pandruvada 			mutex_unlock(&data->mutex);
5611df3a401SSrinivas Pandruvada 			return ret;
5621df3a401SSrinivas Pandruvada 		}
5631df3a401SSrinivas Pandruvada 	}
564ca2ed12fSSrinivas Pandruvada 	data->ref_cnt++;
5651df3a401SSrinivas Pandruvada 	mutex_unlock(&data->mutex);
5661df3a401SSrinivas Pandruvada 
5671df3a401SSrinivas Pandruvada 	return ret;
5681df3a401SSrinivas Pandruvada }
5691df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_open);
5701df3a401SSrinivas Pandruvada 
5711df3a401SSrinivas Pandruvada void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
5721df3a401SSrinivas Pandruvada {
5731df3a401SSrinivas Pandruvada 	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
5741df3a401SSrinivas Pandruvada 
5751df3a401SSrinivas Pandruvada 	mutex_lock(&data->mutex);
576ca2ed12fSSrinivas Pandruvada 	data->ref_cnt--;
577ca2ed12fSSrinivas Pandruvada 	if (!data->ref_cnt)
5781df3a401SSrinivas Pandruvada 		hid_hw_close(hsdev->hdev);
5791df3a401SSrinivas Pandruvada 	mutex_unlock(&data->mutex);
5801df3a401SSrinivas Pandruvada }
5811df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_close);
5821df3a401SSrinivas Pandruvada 
583ade573ebSHans de Goede static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
584ade573ebSHans de Goede 		unsigned int *rsize)
585ade573ebSHans de Goede {
586ade573ebSHans de Goede 	/*
587ade573ebSHans de Goede 	 * Checks if the report descriptor of Thinkpad Helix 2 has a logical
588ade573ebSHans de Goede 	 * minimum for magnetic flux axis greater than the maximum.
589ade573ebSHans de Goede 	 */
590ade573ebSHans de Goede 	if (hdev->product == USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA &&
591ade573ebSHans de Goede 		*rsize == 2558 && rdesc[913] == 0x17 && rdesc[914] == 0x40 &&
592ade573ebSHans de Goede 		rdesc[915] == 0x81 && rdesc[916] == 0x08 &&
593ade573ebSHans de Goede 		rdesc[917] == 0x00 && rdesc[918] == 0x27 &&
594ade573ebSHans de Goede 		rdesc[921] == 0x07 && rdesc[922] == 0x00) {
595ade573ebSHans de Goede 		/* Sets negative logical minimum for mag x, y and z */
596ade573ebSHans de Goede 		rdesc[914] = rdesc[935] = rdesc[956] = 0xc0;
597ade573ebSHans de Goede 		rdesc[915] = rdesc[936] = rdesc[957] = 0x7e;
598ade573ebSHans de Goede 		rdesc[916] = rdesc[937] = rdesc[958] = 0xf7;
599ade573ebSHans de Goede 		rdesc[917] = rdesc[938] = rdesc[959] = 0xff;
600ade573ebSHans de Goede 	}
601ade573ebSHans de Goede 
602ade573ebSHans de Goede 	return rdesc;
603ade573ebSHans de Goede }
604ade573ebSHans de Goede 
605401ca24fSsrinivas pandruvada static int sensor_hub_probe(struct hid_device *hdev,
606401ca24fSsrinivas pandruvada 				const struct hid_device_id *id)
607401ca24fSsrinivas pandruvada {
608401ca24fSsrinivas pandruvada 	int ret;
609401ca24fSsrinivas pandruvada 	struct sensor_hub_data *sd;
610401ca24fSsrinivas pandruvada 	int i;
611401ca24fSsrinivas pandruvada 	char *name;
612401ca24fSsrinivas pandruvada 	int dev_cnt;
613ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev;
614ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *last_hsdev = NULL;
615cb67126fSSrinivas Pandruvada 	struct hid_sensor_hub_device *collection_hsdev = NULL;
616401ca24fSsrinivas pandruvada 
617905cc199SAndy Shevchenko 	sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
618401ca24fSsrinivas pandruvada 	if (!sd) {
619401ca24fSsrinivas pandruvada 		hid_err(hdev, "cannot allocate Sensor data\n");
620401ca24fSsrinivas pandruvada 		return -ENOMEM;
621401ca24fSsrinivas pandruvada 	}
622ca2ed12fSSrinivas Pandruvada 
623401ca24fSsrinivas pandruvada 	hid_set_drvdata(hdev, sd);
624ca2ed12fSSrinivas Pandruvada 
625401ca24fSsrinivas pandruvada 	spin_lock_init(&sd->lock);
626401ca24fSsrinivas pandruvada 	spin_lock_init(&sd->dyn_callback_lock);
627401ca24fSsrinivas pandruvada 	mutex_init(&sd->mutex);
628401ca24fSsrinivas pandruvada 	ret = hid_parse(hdev);
629401ca24fSsrinivas pandruvada 	if (ret) {
630401ca24fSsrinivas pandruvada 		hid_err(hdev, "parse failed\n");
631905cc199SAndy Shevchenko 		return ret;
632401ca24fSsrinivas pandruvada 	}
633401ca24fSsrinivas pandruvada 	INIT_LIST_HEAD(&hdev->inputs);
634401ca24fSsrinivas pandruvada 
635*8e2f79f4SYauhen Kharuzhy 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT | HID_CONNECT_DRIVER);
636401ca24fSsrinivas pandruvada 	if (ret) {
637401ca24fSsrinivas pandruvada 		hid_err(hdev, "hw start failed\n");
638905cc199SAndy Shevchenko 		return ret;
639401ca24fSsrinivas pandruvada 	}
640401ca24fSsrinivas pandruvada 	INIT_LIST_HEAD(&sd->dyn_callback_list);
641401ca24fSsrinivas pandruvada 	sd->hid_sensor_client_cnt = 0;
642401ca24fSsrinivas pandruvada 
643ca2ed12fSSrinivas Pandruvada 	dev_cnt = sensor_hub_get_physical_device_count(hdev);
644401ca24fSsrinivas pandruvada 	if (dev_cnt > HID_MAX_PHY_DEVICES) {
645401ca24fSsrinivas pandruvada 		hid_err(hdev, "Invalid Physical device count\n");
646401ca24fSsrinivas pandruvada 		ret = -EINVAL;
6471df3a401SSrinivas Pandruvada 		goto err_stop_hw;
648401ca24fSsrinivas pandruvada 	}
649a86854d0SKees Cook 	sd->hid_sensor_hub_client_devs = devm_kcalloc(&hdev->dev,
650a86854d0SKees Cook 						      dev_cnt,
651401ca24fSsrinivas pandruvada 						      sizeof(struct mfd_cell),
652401ca24fSsrinivas pandruvada 						      GFP_KERNEL);
653401ca24fSsrinivas pandruvada 	if (sd->hid_sensor_hub_client_devs == NULL) {
654f2f13a68SAxel Lin 		hid_err(hdev, "Failed to allocate memory for mfd cells\n");
655401ca24fSsrinivas pandruvada 		ret = -ENOMEM;
6561df3a401SSrinivas Pandruvada 		goto err_stop_hw;
657401ca24fSsrinivas pandruvada 	}
658ca2ed12fSSrinivas Pandruvada 
659ca2ed12fSSrinivas Pandruvada 	for (i = 0; i < hdev->maxcollection; ++i) {
660ca2ed12fSSrinivas Pandruvada 		struct hid_collection *collection = &hdev->collection[i];
661ca2ed12fSSrinivas Pandruvada 
662cb67126fSSrinivas Pandruvada 		if (collection->type == HID_COLLECTION_PHYSICAL ||
663cb67126fSSrinivas Pandruvada 		    collection->type == HID_COLLECTION_APPLICATION) {
664ca2ed12fSSrinivas Pandruvada 
6655be5db24SHimangi Saraogi 			hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev),
6665be5db24SHimangi Saraogi 					     GFP_KERNEL);
667ca2ed12fSSrinivas Pandruvada 			if (!hsdev) {
668ca2ed12fSSrinivas Pandruvada 				hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
669ca2ed12fSSrinivas Pandruvada 				ret = -ENOMEM;
6705be5db24SHimangi Saraogi 				goto err_stop_hw;
671ca2ed12fSSrinivas Pandruvada 			}
672ca2ed12fSSrinivas Pandruvada 			hsdev->hdev = hdev;
673ca2ed12fSSrinivas Pandruvada 			hsdev->vendor_id = hdev->vendor;
674ca2ed12fSSrinivas Pandruvada 			hsdev->product_id = hdev->product;
675e651a1daSSrinivas Pandruvada 			hsdev->usage = collection->usage;
6762d94e522SSrinivas Pandruvada 			hsdev->mutex_ptr = devm_kzalloc(&hdev->dev,
6772d94e522SSrinivas Pandruvada 							sizeof(struct mutex),
6782d94e522SSrinivas Pandruvada 							GFP_KERNEL);
6792d94e522SSrinivas Pandruvada 			if (!hsdev->mutex_ptr) {
6802d94e522SSrinivas Pandruvada 				ret = -ENOMEM;
6812d94e522SSrinivas Pandruvada 				goto err_stop_hw;
6822d94e522SSrinivas Pandruvada 			}
6832d94e522SSrinivas Pandruvada 			mutex_init(hsdev->mutex_ptr);
684ca2ed12fSSrinivas Pandruvada 			hsdev->start_collection_index = i;
685ca2ed12fSSrinivas Pandruvada 			if (last_hsdev)
686ca2ed12fSSrinivas Pandruvada 				last_hsdev->end_collection_index = i;
687ca2ed12fSSrinivas Pandruvada 			last_hsdev = hsdev;
6885be5db24SHimangi Saraogi 			name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
6895be5db24SHimangi Saraogi 					      "HID-SENSOR-%x",
690ca2ed12fSSrinivas Pandruvada 					      collection->usage);
691401ca24fSsrinivas pandruvada 			if (name == NULL) {
692f2f13a68SAxel Lin 				hid_err(hdev, "Failed MFD device name\n");
693401ca24fSsrinivas pandruvada 				ret = -ENOMEM;
6945be5db24SHimangi Saraogi 				goto err_stop_hw;
695401ca24fSsrinivas pandruvada 			}
696401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
697401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].name = name;
698401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
699401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].platform_data =
700ca2ed12fSSrinivas Pandruvada 							hsdev;
701401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
702401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].pdata_size =
703ca2ed12fSSrinivas Pandruvada 							sizeof(*hsdev);
704ca2ed12fSSrinivas Pandruvada 			hid_dbg(hdev, "Adding %s:%d\n", name,
705ca2ed12fSSrinivas Pandruvada 					hsdev->start_collection_index);
706401ca24fSsrinivas pandruvada 			sd->hid_sensor_client_cnt++;
707cb67126fSSrinivas Pandruvada 			if (collection_hsdev)
708cb67126fSSrinivas Pandruvada 				collection_hsdev->end_collection_index = i;
709cb67126fSSrinivas Pandruvada 			if (collection->type == HID_COLLECTION_APPLICATION &&
710cb67126fSSrinivas Pandruvada 			    collection->usage == HID_USAGE_SENSOR_COLLECTION)
711cb67126fSSrinivas Pandruvada 				collection_hsdev = hsdev;
712401ca24fSsrinivas pandruvada 		}
713401ca24fSsrinivas pandruvada 	}
714ca2ed12fSSrinivas Pandruvada 	if (last_hsdev)
715ca2ed12fSSrinivas Pandruvada 		last_hsdev->end_collection_index = i;
716cb67126fSSrinivas Pandruvada 	if (collection_hsdev)
717cb67126fSSrinivas Pandruvada 		collection_hsdev->end_collection_index = i;
718ca2ed12fSSrinivas Pandruvada 
71916b5fe29SJohan Hovold 	ret = mfd_add_hotplug_devices(&hdev->dev,
72016b5fe29SJohan Hovold 			sd->hid_sensor_hub_client_devs,
72116b5fe29SJohan Hovold 			sd->hid_sensor_client_cnt);
722401ca24fSsrinivas pandruvada 	if (ret < 0)
7235be5db24SHimangi Saraogi 		goto err_stop_hw;
724401ca24fSsrinivas pandruvada 
725401ca24fSsrinivas pandruvada 	return ret;
726401ca24fSsrinivas pandruvada 
727401ca24fSsrinivas pandruvada err_stop_hw:
728401ca24fSsrinivas pandruvada 	hid_hw_stop(hdev);
729401ca24fSsrinivas pandruvada 
730401ca24fSsrinivas pandruvada 	return ret;
731401ca24fSsrinivas pandruvada }
732401ca24fSsrinivas pandruvada 
733401ca24fSsrinivas pandruvada static void sensor_hub_remove(struct hid_device *hdev)
734401ca24fSsrinivas pandruvada {
735401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hdev);
736401ca24fSsrinivas pandruvada 	unsigned long flags;
737e651a1daSSrinivas Pandruvada 	int i;
738401ca24fSsrinivas pandruvada 
739401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " hardware removed\n");
740401ca24fSsrinivas pandruvada 	hid_hw_close(hdev);
741f2f13a68SAxel Lin 	hid_hw_stop(hdev);
742401ca24fSsrinivas pandruvada 	spin_lock_irqsave(&data->lock, flags);
743e651a1daSSrinivas Pandruvada 	for (i = 0; i < data->hid_sensor_client_cnt; ++i) {
744e651a1daSSrinivas Pandruvada 		struct hid_sensor_hub_device *hsdev =
745e651a1daSSrinivas Pandruvada 			data->hid_sensor_hub_client_devs[i].platform_data;
746e651a1daSSrinivas Pandruvada 		if (hsdev->pending.status)
747e651a1daSSrinivas Pandruvada 			complete(&hsdev->pending.ready);
748e651a1daSSrinivas Pandruvada 	}
749401ca24fSsrinivas pandruvada 	spin_unlock_irqrestore(&data->lock, flags);
750401ca24fSsrinivas pandruvada 	mfd_remove_devices(&hdev->dev);
751401ca24fSsrinivas pandruvada 	mutex_destroy(&data->mutex);
752401ca24fSsrinivas pandruvada }
753401ca24fSsrinivas pandruvada 
754401ca24fSsrinivas pandruvada static const struct hid_device_id sensor_hub_devices[] = {
7550d1e4b02SMika Westerberg 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
7560d1e4b02SMika Westerberg 		     HID_ANY_ID) },
757401ca24fSsrinivas pandruvada 	{ }
758401ca24fSsrinivas pandruvada };
759401ca24fSsrinivas pandruvada MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
760401ca24fSsrinivas pandruvada 
761401ca24fSsrinivas pandruvada static struct hid_driver sensor_hub_driver = {
762401ca24fSsrinivas pandruvada 	.name = "hid-sensor-hub",
763401ca24fSsrinivas pandruvada 	.id_table = sensor_hub_devices,
764401ca24fSsrinivas pandruvada 	.probe = sensor_hub_probe,
765401ca24fSsrinivas pandruvada 	.remove = sensor_hub_remove,
766401ca24fSsrinivas pandruvada 	.raw_event = sensor_hub_raw_event,
767ade573ebSHans de Goede 	.report_fixup = sensor_hub_report_fixup,
768401ca24fSsrinivas pandruvada #ifdef CONFIG_PM
769401ca24fSsrinivas pandruvada 	.suspend = sensor_hub_suspend,
770401ca24fSsrinivas pandruvada 	.resume = sensor_hub_resume,
771401ca24fSsrinivas pandruvada 	.reset_resume = sensor_hub_reset_resume,
772401ca24fSsrinivas pandruvada #endif
773401ca24fSsrinivas pandruvada };
774f425458eSH Hartley Sweeten module_hid_driver(sensor_hub_driver);
775401ca24fSsrinivas pandruvada 
776401ca24fSsrinivas pandruvada MODULE_DESCRIPTION("HID Sensor Hub driver");
777401ca24fSsrinivas pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
778401ca24fSsrinivas pandruvada MODULE_LICENSE("GPL");
779