xref: /linux/drivers/hid/hid-sensor-hub.c (revision ff0e9ee3a6d40c8a1c6e19ea8620ef94816eb51c)
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.
43*ff0e9ee3SLee 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) {
2123950e033SSrinivas Pandruvada 			hid_set_field(report->field[field_index], i,
21362fad137SSrinivas Pandruvada 				      (__force __s32)cpu_to_le32(*buf32));
2143950e033SSrinivas Pandruvada 			++buf32;
2153950e033SSrinivas Pandruvada 		}
2163950e033SSrinivas Pandruvada 	}
2173950e033SSrinivas Pandruvada 	if (remaining_bytes) {
2183950e033SSrinivas Pandruvada 		value = 0;
2193950e033SSrinivas Pandruvada 		memcpy(&value, (u8 *)buf32, remaining_bytes);
2203950e033SSrinivas Pandruvada 		hid_set_field(report->field[field_index], i,
22162fad137SSrinivas Pandruvada 			      (__force __s32)cpu_to_le32(value));
2223950e033SSrinivas Pandruvada 	}
223d8814272SBenjamin Tissoires 	hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
224b7966a4dSBenjamin Tissoires 	hid_hw_wait(hsdev->hdev);
225401ca24fSsrinivas pandruvada 
226401ca24fSsrinivas pandruvada done_proc:
227401ca24fSsrinivas pandruvada 	mutex_unlock(&data->mutex);
228401ca24fSsrinivas pandruvada 
229401ca24fSsrinivas pandruvada 	return ret;
230401ca24fSsrinivas pandruvada }
231401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
232401ca24fSsrinivas pandruvada 
233401ca24fSsrinivas pandruvada int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
2346adc83fcSSrinivas Pandruvada 			   u32 field_index, int buffer_size, void *buffer)
235401ca24fSsrinivas pandruvada {
236401ca24fSsrinivas pandruvada 	struct hid_report *report;
237401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
2386adc83fcSSrinivas Pandruvada 	int report_size;
239401ca24fSsrinivas pandruvada 	int ret = 0;
2405459ada2SSrinivas Pandruvada 	u8 *val_ptr;
2415459ada2SSrinivas Pandruvada 	int buffer_index = 0;
2425459ada2SSrinivas Pandruvada 	int i;
243401ca24fSsrinivas pandruvada 
244143fca77SSrinivas Pandruvada 	memset(buffer, 0, buffer_size);
245143fca77SSrinivas Pandruvada 
246401ca24fSsrinivas pandruvada 	mutex_lock(&data->mutex);
247401ca24fSsrinivas pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
2489e891025SKees Cook 	if (!report || (field_index >= report->maxfield) ||
2499e891025SKees Cook 	    report->field[field_index]->report_count < 1) {
250401ca24fSsrinivas pandruvada 		ret = -EINVAL;
251401ca24fSsrinivas pandruvada 		goto done_proc;
252401ca24fSsrinivas pandruvada 	}
253d8814272SBenjamin Tissoires 	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
254b7966a4dSBenjamin Tissoires 	hid_hw_wait(hsdev->hdev);
2556adc83fcSSrinivas Pandruvada 
2566adc83fcSSrinivas Pandruvada 	/* calculate number of bytes required to read this field */
2576adc83fcSSrinivas Pandruvada 	report_size = DIV_ROUND_UP(report->field[field_index]->report_size,
2586adc83fcSSrinivas Pandruvada 				   8) *
2596adc83fcSSrinivas Pandruvada 				   report->field[field_index]->report_count;
2606adc83fcSSrinivas Pandruvada 	if (!report_size) {
2616adc83fcSSrinivas Pandruvada 		ret = -EINVAL;
2626adc83fcSSrinivas Pandruvada 		goto done_proc;
2636adc83fcSSrinivas Pandruvada 	}
2646adc83fcSSrinivas Pandruvada 	ret = min(report_size, buffer_size);
2655459ada2SSrinivas Pandruvada 
2665459ada2SSrinivas Pandruvada 	val_ptr = (u8 *)report->field[field_index]->value;
2675459ada2SSrinivas Pandruvada 	for (i = 0; i < report->field[field_index]->report_count; ++i) {
2685459ada2SSrinivas Pandruvada 		if (buffer_index >= ret)
2695459ada2SSrinivas Pandruvada 			break;
2705459ada2SSrinivas Pandruvada 
2715459ada2SSrinivas Pandruvada 		memcpy(&((u8 *)buffer)[buffer_index], val_ptr,
2725459ada2SSrinivas Pandruvada 		       report->field[field_index]->report_size / 8);
2735459ada2SSrinivas Pandruvada 		val_ptr += sizeof(__s32);
2745459ada2SSrinivas Pandruvada 		buffer_index += (report->field[field_index]->report_size / 8);
2755459ada2SSrinivas Pandruvada 	}
276401ca24fSsrinivas pandruvada 
277401ca24fSsrinivas pandruvada done_proc:
278401ca24fSsrinivas pandruvada 	mutex_unlock(&data->mutex);
279401ca24fSsrinivas pandruvada 
280401ca24fSsrinivas pandruvada 	return ret;
281401ca24fSsrinivas pandruvada }
282401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
283401ca24fSsrinivas pandruvada 
284401ca24fSsrinivas pandruvada 
285401ca24fSsrinivas pandruvada int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
286401ca24fSsrinivas pandruvada 					u32 usage_id,
287b3f4737dSSrinivas Pandruvada 					u32 attr_usage_id, u32 report_id,
2880145b505SHans de Goede 					enum sensor_hub_read_flags flag,
2890145b505SHans de Goede 					bool is_signed)
290401ca24fSsrinivas pandruvada {
291401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
292401ca24fSsrinivas pandruvada 	unsigned long flags;
293401ca24fSsrinivas pandruvada 	struct hid_report *report;
294401ca24fSsrinivas pandruvada 	int ret_val = 0;
295401ca24fSsrinivas pandruvada 
296b3f4737dSSrinivas Pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev,
297b3f4737dSSrinivas Pandruvada 				   HID_INPUT_REPORT);
298b3f4737dSSrinivas Pandruvada 	if (!report)
299b3f4737dSSrinivas Pandruvada 		return -EINVAL;
300b3f4737dSSrinivas Pandruvada 
3012d94e522SSrinivas Pandruvada 	mutex_lock(hsdev->mutex_ptr);
302b3f4737dSSrinivas Pandruvada 	if (flag == SENSOR_HUB_SYNC) {
303e651a1daSSrinivas Pandruvada 		memset(&hsdev->pending, 0, sizeof(hsdev->pending));
304e651a1daSSrinivas Pandruvada 		init_completion(&hsdev->pending.ready);
305e651a1daSSrinivas Pandruvada 		hsdev->pending.usage_id = usage_id;
306e651a1daSSrinivas Pandruvada 		hsdev->pending.attr_usage_id = attr_usage_id;
307e651a1daSSrinivas Pandruvada 		hsdev->pending.raw_size = 0;
308401ca24fSsrinivas pandruvada 
309401ca24fSsrinivas pandruvada 		spin_lock_irqsave(&data->lock, flags);
310e651a1daSSrinivas Pandruvada 		hsdev->pending.status = true;
311f74346a0SSrinivas Pandruvada 		spin_unlock_irqrestore(&data->lock, flags);
312b3f4737dSSrinivas Pandruvada 	}
313e651a1daSSrinivas Pandruvada 	mutex_lock(&data->mutex);
314d8814272SBenjamin Tissoires 	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
315e651a1daSSrinivas Pandruvada 	mutex_unlock(&data->mutex);
316b3f4737dSSrinivas Pandruvada 	if (flag == SENSOR_HUB_SYNC) {
317b3f4737dSSrinivas Pandruvada 		wait_for_completion_interruptible_timeout(
318b3f4737dSSrinivas Pandruvada 						&hsdev->pending.ready, HZ*5);
319e651a1daSSrinivas Pandruvada 		switch (hsdev->pending.raw_size) {
320401ca24fSsrinivas pandruvada 		case 1:
3210145b505SHans de Goede 			if (is_signed)
3220145b505SHans de Goede 				ret_val = *(s8 *)hsdev->pending.raw_data;
3230145b505SHans de Goede 			else
324e651a1daSSrinivas Pandruvada 				ret_val = *(u8 *)hsdev->pending.raw_data;
325401ca24fSsrinivas pandruvada 			break;
326401ca24fSsrinivas pandruvada 		case 2:
3270145b505SHans de Goede 			if (is_signed)
3280145b505SHans de Goede 				ret_val = *(s16 *)hsdev->pending.raw_data;
3290145b505SHans de Goede 			else
330e651a1daSSrinivas Pandruvada 				ret_val = *(u16 *)hsdev->pending.raw_data;
331401ca24fSsrinivas pandruvada 			break;
332401ca24fSsrinivas pandruvada 		case 4:
333e651a1daSSrinivas Pandruvada 			ret_val = *(u32 *)hsdev->pending.raw_data;
334401ca24fSsrinivas pandruvada 			break;
335401ca24fSsrinivas pandruvada 		default:
336401ca24fSsrinivas pandruvada 			ret_val = 0;
337401ca24fSsrinivas pandruvada 		}
338e651a1daSSrinivas Pandruvada 		kfree(hsdev->pending.raw_data);
339e651a1daSSrinivas Pandruvada 		hsdev->pending.status = false;
340b3f4737dSSrinivas Pandruvada 	}
3412d94e522SSrinivas Pandruvada 	mutex_unlock(hsdev->mutex_ptr);
342401ca24fSsrinivas pandruvada 
343401ca24fSsrinivas pandruvada 	return ret_val;
344401ca24fSsrinivas pandruvada }
345401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
346401ca24fSsrinivas pandruvada 
347e02cee48SSrinivas Pandruvada int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
348e02cee48SSrinivas Pandruvada 				u32 report_id, int field_index, u32 usage_id)
349e02cee48SSrinivas Pandruvada {
350e02cee48SSrinivas Pandruvada 	struct hid_report *report;
351e02cee48SSrinivas Pandruvada 	struct hid_field *field;
352e02cee48SSrinivas Pandruvada 	int i;
353e02cee48SSrinivas Pandruvada 
354e02cee48SSrinivas Pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
355e02cee48SSrinivas Pandruvada 	if (!report || (field_index >= report->maxfield))
356e02cee48SSrinivas Pandruvada 		goto done_proc;
357e02cee48SSrinivas Pandruvada 
358e02cee48SSrinivas Pandruvada 	field = report->field[field_index];
359e02cee48SSrinivas Pandruvada 	for (i = 0; i < field->maxusage; ++i) {
360e02cee48SSrinivas Pandruvada 		if (field->usage[i].hid == usage_id)
361e02cee48SSrinivas Pandruvada 			return field->usage[i].usage_index;
362e02cee48SSrinivas Pandruvada 	}
363e02cee48SSrinivas Pandruvada 
364e02cee48SSrinivas Pandruvada done_proc:
365e02cee48SSrinivas Pandruvada 	return -EINVAL;
366e02cee48SSrinivas Pandruvada }
367e02cee48SSrinivas Pandruvada EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index);
368e02cee48SSrinivas Pandruvada 
369401ca24fSsrinivas pandruvada int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
370401ca24fSsrinivas pandruvada 				u8 type,
371401ca24fSsrinivas pandruvada 				u32 usage_id,
372401ca24fSsrinivas pandruvada 				u32 attr_usage_id,
373401ca24fSsrinivas pandruvada 				struct hid_sensor_hub_attribute_info *info)
374401ca24fSsrinivas pandruvada {
375401ca24fSsrinivas pandruvada 	int ret = -1;
376ca2ed12fSSrinivas Pandruvada 	int i;
377401ca24fSsrinivas pandruvada 	struct hid_report *report;
378401ca24fSsrinivas pandruvada 	struct hid_field *field;
379401ca24fSsrinivas pandruvada 	struct hid_report_enum *report_enum;
380401ca24fSsrinivas pandruvada 	struct hid_device *hdev = hsdev->hdev;
381401ca24fSsrinivas pandruvada 
382401ca24fSsrinivas pandruvada 	/* Initialize with defaults */
383401ca24fSsrinivas pandruvada 	info->usage_id = usage_id;
384401ca24fSsrinivas pandruvada 	info->attrib_id = attr_usage_id;
385401ca24fSsrinivas pandruvada 	info->report_id = -1;
386401ca24fSsrinivas pandruvada 	info->index = -1;
387401ca24fSsrinivas pandruvada 	info->units = -1;
388401ca24fSsrinivas pandruvada 	info->unit_expo = -1;
389401ca24fSsrinivas pandruvada 
390401ca24fSsrinivas pandruvada 	report_enum = &hdev->report_enum[type];
391401ca24fSsrinivas pandruvada 	list_for_each_entry(report, &report_enum->report_list, list) {
392401ca24fSsrinivas pandruvada 		for (i = 0; i < report->maxfield; ++i) {
393401ca24fSsrinivas pandruvada 			field = report->field[i];
394ca2ed12fSSrinivas Pandruvada 			if (field->maxusage) {
395401ca24fSsrinivas pandruvada 				if (field->physical == usage_id &&
396ca2ed12fSSrinivas Pandruvada 					(field->logical == attr_usage_id ||
397ca2ed12fSSrinivas Pandruvada 					field->usage[0].hid ==
398ca2ed12fSSrinivas Pandruvada 							attr_usage_id) &&
399ca2ed12fSSrinivas Pandruvada 					(field->usage[0].collection_index >=
400ca2ed12fSSrinivas Pandruvada 					hsdev->start_collection_index) &&
401ca2ed12fSSrinivas Pandruvada 					(field->usage[0].collection_index <
402ca2ed12fSSrinivas Pandruvada 					hsdev->end_collection_index)) {
403ca2ed12fSSrinivas Pandruvada 
404ca2ed12fSSrinivas Pandruvada 					sensor_hub_fill_attr_info(info, i,
405ca2ed12fSSrinivas Pandruvada 								report->id,
4069f740ffaSSrinivas Pandruvada 								field);
407401ca24fSsrinivas pandruvada 					ret = 0;
408401ca24fSsrinivas pandruvada 					break;
409401ca24fSsrinivas pandruvada 				}
410401ca24fSsrinivas pandruvada 			}
411401ca24fSsrinivas pandruvada 		}
412401ca24fSsrinivas pandruvada 
413ca2ed12fSSrinivas Pandruvada 	}
414ca2ed12fSSrinivas Pandruvada 
415401ca24fSsrinivas pandruvada 	return ret;
416401ca24fSsrinivas pandruvada }
417401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
418401ca24fSsrinivas pandruvada 
419401ca24fSsrinivas pandruvada #ifdef CONFIG_PM
420401ca24fSsrinivas pandruvada static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
421401ca24fSsrinivas pandruvada {
422401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
423401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
4240ccf091dSJiri Kosina 	unsigned long flags;
425401ca24fSsrinivas pandruvada 
426401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " sensor_hub_suspend\n");
4270ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
428401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
429401ca24fSsrinivas pandruvada 		if (callback->usage_callback->suspend)
430401ca24fSsrinivas pandruvada 			callback->usage_callback->suspend(
431ca2ed12fSSrinivas Pandruvada 					callback->hsdev, callback->priv);
432401ca24fSsrinivas pandruvada 	}
4330ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
434401ca24fSsrinivas pandruvada 
435401ca24fSsrinivas pandruvada 	return 0;
436401ca24fSsrinivas pandruvada }
437401ca24fSsrinivas pandruvada 
438401ca24fSsrinivas pandruvada static int sensor_hub_resume(struct hid_device *hdev)
439401ca24fSsrinivas pandruvada {
440401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
441401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
4420ccf091dSJiri Kosina 	unsigned long flags;
443401ca24fSsrinivas pandruvada 
444401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " sensor_hub_resume\n");
4450ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
446401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
447401ca24fSsrinivas pandruvada 		if (callback->usage_callback->resume)
448401ca24fSsrinivas pandruvada 			callback->usage_callback->resume(
449ca2ed12fSSrinivas Pandruvada 					callback->hsdev, callback->priv);
450401ca24fSsrinivas pandruvada 	}
4510ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
452401ca24fSsrinivas pandruvada 
453401ca24fSsrinivas pandruvada 	return 0;
454401ca24fSsrinivas pandruvada }
455401ca24fSsrinivas pandruvada 
456401ca24fSsrinivas pandruvada static int sensor_hub_reset_resume(struct hid_device *hdev)
457401ca24fSsrinivas pandruvada {
458401ca24fSsrinivas pandruvada 	return 0;
459401ca24fSsrinivas pandruvada }
460401ca24fSsrinivas pandruvada #endif
4615902fde1SAndy Shevchenko 
462401ca24fSsrinivas pandruvada /*
463401ca24fSsrinivas pandruvada  * Handle raw report as sent by device
464401ca24fSsrinivas pandruvada  */
465401ca24fSsrinivas pandruvada static int sensor_hub_raw_event(struct hid_device *hdev,
466401ca24fSsrinivas pandruvada 		struct hid_report *report, u8 *raw_data, int size)
467401ca24fSsrinivas pandruvada {
468401ca24fSsrinivas pandruvada 	int i;
469401ca24fSsrinivas pandruvada 	u8 *ptr;
470401ca24fSsrinivas pandruvada 	int sz;
471401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
472401ca24fSsrinivas pandruvada 	unsigned long flags;
473401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks *callback = NULL;
474401ca24fSsrinivas pandruvada 	struct hid_collection *collection = NULL;
475401ca24fSsrinivas pandruvada 	void *priv = NULL;
476ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev = NULL;
477401ca24fSsrinivas pandruvada 
478401ca24fSsrinivas pandruvada 	hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
479401ca24fSsrinivas pandruvada 			 report->id, size, report->type);
480401ca24fSsrinivas pandruvada 	hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
481401ca24fSsrinivas pandruvada 	if (report->type != HID_INPUT_REPORT)
482401ca24fSsrinivas pandruvada 		return 1;
483401ca24fSsrinivas pandruvada 
484401ca24fSsrinivas pandruvada 	ptr = raw_data;
48534a9fa20SPablo Ceballos 	if (report->id)
486401ca24fSsrinivas pandruvada 		ptr++; /* Skip report id */
487401ca24fSsrinivas pandruvada 
488401ca24fSsrinivas pandruvada 	spin_lock_irqsave(&pdata->lock, flags);
489401ca24fSsrinivas pandruvada 
490401ca24fSsrinivas pandruvada 	for (i = 0; i < report->maxfield; ++i) {
491401ca24fSsrinivas pandruvada 		hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
492401ca24fSsrinivas pandruvada 				i, report->field[i]->usage->collection_index,
493401ca24fSsrinivas pandruvada 				report->field[i]->usage->hid,
494d4b1bba7SSrinivas Pandruvada 				(report->field[i]->report_size *
495d4b1bba7SSrinivas Pandruvada 					report->field[i]->report_count)/8);
496d4b1bba7SSrinivas Pandruvada 		sz = (report->field[i]->report_size *
497d4b1bba7SSrinivas Pandruvada 					report->field[i]->report_count)/8;
498401ca24fSsrinivas pandruvada 		collection = &hdev->collection[
499401ca24fSsrinivas pandruvada 				report->field[i]->usage->collection_index];
500401ca24fSsrinivas pandruvada 		hid_dbg(hdev, "collection->usage %x\n",
501401ca24fSsrinivas pandruvada 					collection->usage);
502ca2ed12fSSrinivas Pandruvada 
503ca2ed12fSSrinivas Pandruvada 		callback = sensor_hub_get_callback(hdev,
504401ca24fSsrinivas pandruvada 				report->field[i]->physical,
505ca2ed12fSSrinivas Pandruvada 				report->field[i]->usage[0].collection_index,
506ca2ed12fSSrinivas Pandruvada 				&hsdev, &priv);
507e651a1daSSrinivas Pandruvada 		if (!callback) {
508e651a1daSSrinivas Pandruvada 			ptr += sz;
509e651a1daSSrinivas Pandruvada 			continue;
510e651a1daSSrinivas Pandruvada 		}
511eb964833SSrinivas Pandruvada 		if (hsdev->pending.status && (hsdev->pending.attr_usage_id ==
512eb964833SSrinivas Pandruvada 					      report->field[i]->usage->hid ||
513eb964833SSrinivas Pandruvada 					      hsdev->pending.attr_usage_id ==
514eb964833SSrinivas Pandruvada 					      report->field[i]->logical)) {
515e651a1daSSrinivas Pandruvada 			hid_dbg(hdev, "data was pending ...\n");
516e651a1daSSrinivas Pandruvada 			hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
517e651a1daSSrinivas Pandruvada 			if (hsdev->pending.raw_data)
518e651a1daSSrinivas Pandruvada 				hsdev->pending.raw_size = sz;
519e651a1daSSrinivas Pandruvada 			else
520e651a1daSSrinivas Pandruvada 				hsdev->pending.raw_size = 0;
521e651a1daSSrinivas Pandruvada 			complete(&hsdev->pending.ready);
522e651a1daSSrinivas Pandruvada 		}
523e651a1daSSrinivas Pandruvada 		if (callback->capture_sample) {
524401ca24fSsrinivas pandruvada 			if (report->field[i]->logical)
525ca2ed12fSSrinivas Pandruvada 				callback->capture_sample(hsdev,
526401ca24fSsrinivas pandruvada 					report->field[i]->logical, sz, ptr,
527401ca24fSsrinivas pandruvada 					callback->pdev);
528401ca24fSsrinivas pandruvada 			else
529ca2ed12fSSrinivas Pandruvada 				callback->capture_sample(hsdev,
530401ca24fSsrinivas pandruvada 					report->field[i]->usage->hid, sz, ptr,
531401ca24fSsrinivas pandruvada 					callback->pdev);
532401ca24fSsrinivas pandruvada 		}
533401ca24fSsrinivas pandruvada 		ptr += sz;
534401ca24fSsrinivas pandruvada 	}
535401ca24fSsrinivas pandruvada 	if (callback && collection && callback->send_event)
536ca2ed12fSSrinivas Pandruvada 		callback->send_event(hsdev, collection->usage,
537401ca24fSsrinivas pandruvada 				callback->pdev);
538401ca24fSsrinivas pandruvada 	spin_unlock_irqrestore(&pdata->lock, flags);
539401ca24fSsrinivas pandruvada 
540401ca24fSsrinivas pandruvada 	return 1;
541401ca24fSsrinivas pandruvada }
542401ca24fSsrinivas pandruvada 
5431df3a401SSrinivas Pandruvada int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
5441df3a401SSrinivas Pandruvada {
5451df3a401SSrinivas Pandruvada 	int ret = 0;
5461df3a401SSrinivas Pandruvada 	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
5471df3a401SSrinivas Pandruvada 
5481df3a401SSrinivas Pandruvada 	mutex_lock(&data->mutex);
549ca2ed12fSSrinivas Pandruvada 	if (!data->ref_cnt) {
5501df3a401SSrinivas Pandruvada 		ret = hid_hw_open(hsdev->hdev);
5511df3a401SSrinivas Pandruvada 		if (ret) {
5521df3a401SSrinivas Pandruvada 			hid_err(hsdev->hdev, "failed to open hid device\n");
5531df3a401SSrinivas Pandruvada 			mutex_unlock(&data->mutex);
5541df3a401SSrinivas Pandruvada 			return ret;
5551df3a401SSrinivas Pandruvada 		}
5561df3a401SSrinivas Pandruvada 	}
557ca2ed12fSSrinivas Pandruvada 	data->ref_cnt++;
5581df3a401SSrinivas Pandruvada 	mutex_unlock(&data->mutex);
5591df3a401SSrinivas Pandruvada 
5601df3a401SSrinivas Pandruvada 	return ret;
5611df3a401SSrinivas Pandruvada }
5621df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_open);
5631df3a401SSrinivas Pandruvada 
5641df3a401SSrinivas Pandruvada void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
5651df3a401SSrinivas Pandruvada {
5661df3a401SSrinivas Pandruvada 	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
5671df3a401SSrinivas Pandruvada 
5681df3a401SSrinivas Pandruvada 	mutex_lock(&data->mutex);
569ca2ed12fSSrinivas Pandruvada 	data->ref_cnt--;
570ca2ed12fSSrinivas Pandruvada 	if (!data->ref_cnt)
5711df3a401SSrinivas Pandruvada 		hid_hw_close(hsdev->hdev);
5721df3a401SSrinivas Pandruvada 	mutex_unlock(&data->mutex);
5731df3a401SSrinivas Pandruvada }
5741df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_close);
5751df3a401SSrinivas Pandruvada 
576ade573ebSHans de Goede static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
577ade573ebSHans de Goede 		unsigned int *rsize)
578ade573ebSHans de Goede {
579ade573ebSHans de Goede 	/*
580ade573ebSHans de Goede 	 * Checks if the report descriptor of Thinkpad Helix 2 has a logical
581ade573ebSHans de Goede 	 * minimum for magnetic flux axis greater than the maximum.
582ade573ebSHans de Goede 	 */
583ade573ebSHans de Goede 	if (hdev->product == USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA &&
584ade573ebSHans de Goede 		*rsize == 2558 && rdesc[913] == 0x17 && rdesc[914] == 0x40 &&
585ade573ebSHans de Goede 		rdesc[915] == 0x81 && rdesc[916] == 0x08 &&
586ade573ebSHans de Goede 		rdesc[917] == 0x00 && rdesc[918] == 0x27 &&
587ade573ebSHans de Goede 		rdesc[921] == 0x07 && rdesc[922] == 0x00) {
588ade573ebSHans de Goede 		/* Sets negative logical minimum for mag x, y and z */
589ade573ebSHans de Goede 		rdesc[914] = rdesc[935] = rdesc[956] = 0xc0;
590ade573ebSHans de Goede 		rdesc[915] = rdesc[936] = rdesc[957] = 0x7e;
591ade573ebSHans de Goede 		rdesc[916] = rdesc[937] = rdesc[958] = 0xf7;
592ade573ebSHans de Goede 		rdesc[917] = rdesc[938] = rdesc[959] = 0xff;
593ade573ebSHans de Goede 	}
594ade573ebSHans de Goede 
595ade573ebSHans de Goede 	return rdesc;
596ade573ebSHans de Goede }
597ade573ebSHans de Goede 
598401ca24fSsrinivas pandruvada static int sensor_hub_probe(struct hid_device *hdev,
599401ca24fSsrinivas pandruvada 				const struct hid_device_id *id)
600401ca24fSsrinivas pandruvada {
601401ca24fSsrinivas pandruvada 	int ret;
602401ca24fSsrinivas pandruvada 	struct sensor_hub_data *sd;
603401ca24fSsrinivas pandruvada 	int i;
604401ca24fSsrinivas pandruvada 	char *name;
605401ca24fSsrinivas pandruvada 	int dev_cnt;
606ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev;
607ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *last_hsdev = NULL;
608cb67126fSSrinivas Pandruvada 	struct hid_sensor_hub_device *collection_hsdev = NULL;
609401ca24fSsrinivas pandruvada 
610905cc199SAndy Shevchenko 	sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
611401ca24fSsrinivas pandruvada 	if (!sd) {
612401ca24fSsrinivas pandruvada 		hid_err(hdev, "cannot allocate Sensor data\n");
613401ca24fSsrinivas pandruvada 		return -ENOMEM;
614401ca24fSsrinivas pandruvada 	}
615ca2ed12fSSrinivas Pandruvada 
616401ca24fSsrinivas pandruvada 	hid_set_drvdata(hdev, sd);
617ca2ed12fSSrinivas Pandruvada 
618401ca24fSsrinivas pandruvada 	spin_lock_init(&sd->lock);
619401ca24fSsrinivas pandruvada 	spin_lock_init(&sd->dyn_callback_lock);
620401ca24fSsrinivas pandruvada 	mutex_init(&sd->mutex);
621401ca24fSsrinivas pandruvada 	ret = hid_parse(hdev);
622401ca24fSsrinivas pandruvada 	if (ret) {
623401ca24fSsrinivas pandruvada 		hid_err(hdev, "parse failed\n");
624905cc199SAndy Shevchenko 		return ret;
625401ca24fSsrinivas pandruvada 	}
626401ca24fSsrinivas pandruvada 	INIT_LIST_HEAD(&hdev->inputs);
627401ca24fSsrinivas pandruvada 
628401ca24fSsrinivas pandruvada 	ret = hid_hw_start(hdev, 0);
629401ca24fSsrinivas pandruvada 	if (ret) {
630401ca24fSsrinivas pandruvada 		hid_err(hdev, "hw start failed\n");
631905cc199SAndy Shevchenko 		return ret;
632401ca24fSsrinivas pandruvada 	}
633401ca24fSsrinivas pandruvada 	INIT_LIST_HEAD(&sd->dyn_callback_list);
634401ca24fSsrinivas pandruvada 	sd->hid_sensor_client_cnt = 0;
635401ca24fSsrinivas pandruvada 
636ca2ed12fSSrinivas Pandruvada 	dev_cnt = sensor_hub_get_physical_device_count(hdev);
637401ca24fSsrinivas pandruvada 	if (dev_cnt > HID_MAX_PHY_DEVICES) {
638401ca24fSsrinivas pandruvada 		hid_err(hdev, "Invalid Physical device count\n");
639401ca24fSsrinivas pandruvada 		ret = -EINVAL;
6401df3a401SSrinivas Pandruvada 		goto err_stop_hw;
641401ca24fSsrinivas pandruvada 	}
642a86854d0SKees Cook 	sd->hid_sensor_hub_client_devs = devm_kcalloc(&hdev->dev,
643a86854d0SKees Cook 						      dev_cnt,
644401ca24fSsrinivas pandruvada 						      sizeof(struct mfd_cell),
645401ca24fSsrinivas pandruvada 						      GFP_KERNEL);
646401ca24fSsrinivas pandruvada 	if (sd->hid_sensor_hub_client_devs == NULL) {
647f2f13a68SAxel Lin 		hid_err(hdev, "Failed to allocate memory for mfd cells\n");
648401ca24fSsrinivas pandruvada 		ret = -ENOMEM;
6491df3a401SSrinivas Pandruvada 		goto err_stop_hw;
650401ca24fSsrinivas pandruvada 	}
651ca2ed12fSSrinivas Pandruvada 
652ca2ed12fSSrinivas Pandruvada 	for (i = 0; i < hdev->maxcollection; ++i) {
653ca2ed12fSSrinivas Pandruvada 		struct hid_collection *collection = &hdev->collection[i];
654ca2ed12fSSrinivas Pandruvada 
655cb67126fSSrinivas Pandruvada 		if (collection->type == HID_COLLECTION_PHYSICAL ||
656cb67126fSSrinivas Pandruvada 		    collection->type == HID_COLLECTION_APPLICATION) {
657ca2ed12fSSrinivas Pandruvada 
6585be5db24SHimangi Saraogi 			hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev),
6595be5db24SHimangi Saraogi 					     GFP_KERNEL);
660ca2ed12fSSrinivas Pandruvada 			if (!hsdev) {
661ca2ed12fSSrinivas Pandruvada 				hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
662ca2ed12fSSrinivas Pandruvada 				ret = -ENOMEM;
6635be5db24SHimangi Saraogi 				goto err_stop_hw;
664ca2ed12fSSrinivas Pandruvada 			}
665ca2ed12fSSrinivas Pandruvada 			hsdev->hdev = hdev;
666ca2ed12fSSrinivas Pandruvada 			hsdev->vendor_id = hdev->vendor;
667ca2ed12fSSrinivas Pandruvada 			hsdev->product_id = hdev->product;
668e651a1daSSrinivas Pandruvada 			hsdev->usage = collection->usage;
6692d94e522SSrinivas Pandruvada 			hsdev->mutex_ptr = devm_kzalloc(&hdev->dev,
6702d94e522SSrinivas Pandruvada 							sizeof(struct mutex),
6712d94e522SSrinivas Pandruvada 							GFP_KERNEL);
6722d94e522SSrinivas Pandruvada 			if (!hsdev->mutex_ptr) {
6732d94e522SSrinivas Pandruvada 				ret = -ENOMEM;
6742d94e522SSrinivas Pandruvada 				goto err_stop_hw;
6752d94e522SSrinivas Pandruvada 			}
6762d94e522SSrinivas Pandruvada 			mutex_init(hsdev->mutex_ptr);
677ca2ed12fSSrinivas Pandruvada 			hsdev->start_collection_index = i;
678ca2ed12fSSrinivas Pandruvada 			if (last_hsdev)
679ca2ed12fSSrinivas Pandruvada 				last_hsdev->end_collection_index = i;
680ca2ed12fSSrinivas Pandruvada 			last_hsdev = hsdev;
6815be5db24SHimangi Saraogi 			name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
6825be5db24SHimangi Saraogi 					      "HID-SENSOR-%x",
683ca2ed12fSSrinivas Pandruvada 					      collection->usage);
684401ca24fSsrinivas pandruvada 			if (name == NULL) {
685f2f13a68SAxel Lin 				hid_err(hdev, "Failed MFD device name\n");
686401ca24fSsrinivas pandruvada 				ret = -ENOMEM;
6875be5db24SHimangi Saraogi 				goto err_stop_hw;
688401ca24fSsrinivas pandruvada 			}
689401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
690401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].name = name;
691401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
692401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].platform_data =
693ca2ed12fSSrinivas Pandruvada 							hsdev;
694401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
695401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].pdata_size =
696ca2ed12fSSrinivas Pandruvada 							sizeof(*hsdev);
697ca2ed12fSSrinivas Pandruvada 			hid_dbg(hdev, "Adding %s:%d\n", name,
698ca2ed12fSSrinivas Pandruvada 					hsdev->start_collection_index);
699401ca24fSsrinivas pandruvada 			sd->hid_sensor_client_cnt++;
700cb67126fSSrinivas Pandruvada 			if (collection_hsdev)
701cb67126fSSrinivas Pandruvada 				collection_hsdev->end_collection_index = i;
702cb67126fSSrinivas Pandruvada 			if (collection->type == HID_COLLECTION_APPLICATION &&
703cb67126fSSrinivas Pandruvada 			    collection->usage == HID_USAGE_SENSOR_COLLECTION)
704cb67126fSSrinivas Pandruvada 				collection_hsdev = hsdev;
705401ca24fSsrinivas pandruvada 		}
706401ca24fSsrinivas pandruvada 	}
707ca2ed12fSSrinivas Pandruvada 	if (last_hsdev)
708ca2ed12fSSrinivas Pandruvada 		last_hsdev->end_collection_index = i;
709cb67126fSSrinivas Pandruvada 	if (collection_hsdev)
710cb67126fSSrinivas Pandruvada 		collection_hsdev->end_collection_index = i;
711ca2ed12fSSrinivas Pandruvada 
71216b5fe29SJohan Hovold 	ret = mfd_add_hotplug_devices(&hdev->dev,
71316b5fe29SJohan Hovold 			sd->hid_sensor_hub_client_devs,
71416b5fe29SJohan Hovold 			sd->hid_sensor_client_cnt);
715401ca24fSsrinivas pandruvada 	if (ret < 0)
7165be5db24SHimangi Saraogi 		goto err_stop_hw;
717401ca24fSsrinivas pandruvada 
718401ca24fSsrinivas pandruvada 	return ret;
719401ca24fSsrinivas pandruvada 
720401ca24fSsrinivas pandruvada err_stop_hw:
721401ca24fSsrinivas pandruvada 	hid_hw_stop(hdev);
722401ca24fSsrinivas pandruvada 
723401ca24fSsrinivas pandruvada 	return ret;
724401ca24fSsrinivas pandruvada }
725401ca24fSsrinivas pandruvada 
726401ca24fSsrinivas pandruvada static void sensor_hub_remove(struct hid_device *hdev)
727401ca24fSsrinivas pandruvada {
728401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hdev);
729401ca24fSsrinivas pandruvada 	unsigned long flags;
730e651a1daSSrinivas Pandruvada 	int i;
731401ca24fSsrinivas pandruvada 
732401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " hardware removed\n");
733401ca24fSsrinivas pandruvada 	hid_hw_close(hdev);
734f2f13a68SAxel Lin 	hid_hw_stop(hdev);
735401ca24fSsrinivas pandruvada 	spin_lock_irqsave(&data->lock, flags);
736e651a1daSSrinivas Pandruvada 	for (i = 0; i < data->hid_sensor_client_cnt; ++i) {
737e651a1daSSrinivas Pandruvada 		struct hid_sensor_hub_device *hsdev =
738e651a1daSSrinivas Pandruvada 			data->hid_sensor_hub_client_devs[i].platform_data;
739e651a1daSSrinivas Pandruvada 		if (hsdev->pending.status)
740e651a1daSSrinivas Pandruvada 			complete(&hsdev->pending.ready);
741e651a1daSSrinivas Pandruvada 	}
742401ca24fSsrinivas pandruvada 	spin_unlock_irqrestore(&data->lock, flags);
743401ca24fSsrinivas pandruvada 	mfd_remove_devices(&hdev->dev);
744401ca24fSsrinivas pandruvada 	mutex_destroy(&data->mutex);
745401ca24fSsrinivas pandruvada }
746401ca24fSsrinivas pandruvada 
747401ca24fSsrinivas pandruvada static const struct hid_device_id sensor_hub_devices[] = {
7480d1e4b02SMika Westerberg 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
7490d1e4b02SMika Westerberg 		     HID_ANY_ID) },
750401ca24fSsrinivas pandruvada 	{ }
751401ca24fSsrinivas pandruvada };
752401ca24fSsrinivas pandruvada MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
753401ca24fSsrinivas pandruvada 
754401ca24fSsrinivas pandruvada static struct hid_driver sensor_hub_driver = {
755401ca24fSsrinivas pandruvada 	.name = "hid-sensor-hub",
756401ca24fSsrinivas pandruvada 	.id_table = sensor_hub_devices,
757401ca24fSsrinivas pandruvada 	.probe = sensor_hub_probe,
758401ca24fSsrinivas pandruvada 	.remove = sensor_hub_remove,
759401ca24fSsrinivas pandruvada 	.raw_event = sensor_hub_raw_event,
760ade573ebSHans de Goede 	.report_fixup = sensor_hub_report_fixup,
761401ca24fSsrinivas pandruvada #ifdef CONFIG_PM
762401ca24fSsrinivas pandruvada 	.suspend = sensor_hub_suspend,
763401ca24fSsrinivas pandruvada 	.resume = sensor_hub_resume,
764401ca24fSsrinivas pandruvada 	.reset_resume = sensor_hub_reset_resume,
765401ca24fSsrinivas pandruvada #endif
766401ca24fSsrinivas pandruvada };
767f425458eSH Hartley Sweeten module_hid_driver(sensor_hub_driver);
768401ca24fSsrinivas pandruvada 
769401ca24fSsrinivas pandruvada MODULE_DESCRIPTION("HID Sensor Hub driver");
770401ca24fSsrinivas pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
771401ca24fSsrinivas pandruvada MODULE_LICENSE("GPL");
772