xref: /linux/drivers/hid/hid-sensor-hub.c (revision e651a1da442ae02a50081e38309dea5e89da2d41)
1401ca24fSsrinivas pandruvada /*
2401ca24fSsrinivas pandruvada  * HID Sensors Driver
3401ca24fSsrinivas pandruvada  * Copyright (c) 2012, Intel Corporation.
4401ca24fSsrinivas pandruvada  *
5401ca24fSsrinivas pandruvada  * This program is free software; you can redistribute it and/or modify it
6401ca24fSsrinivas pandruvada  * under the terms and conditions of the GNU General Public License,
7401ca24fSsrinivas pandruvada  * version 2, as published by the Free Software Foundation.
8401ca24fSsrinivas pandruvada  *
9401ca24fSsrinivas pandruvada  * This program is distributed in the hope it will be useful, but WITHOUT
10401ca24fSsrinivas pandruvada  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11401ca24fSsrinivas pandruvada  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12401ca24fSsrinivas pandruvada  * more details.
13401ca24fSsrinivas pandruvada  *
14401ca24fSsrinivas pandruvada  * You should have received a copy of the GNU General Public License along with
15401ca24fSsrinivas pandruvada  * this program; if not, write to the Free Software Foundation, Inc.,
16401ca24fSsrinivas pandruvada  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17401ca24fSsrinivas pandruvada  *
18401ca24fSsrinivas pandruvada  */
19401ca24fSsrinivas pandruvada #include <linux/device.h>
20401ca24fSsrinivas pandruvada #include <linux/hid.h>
21401ca24fSsrinivas pandruvada #include <linux/module.h>
22401ca24fSsrinivas pandruvada #include <linux/slab.h>
23401ca24fSsrinivas pandruvada #include <linux/mfd/core.h>
24401ca24fSsrinivas pandruvada #include <linux/list.h>
25401ca24fSsrinivas pandruvada #include <linux/hid-sensor-ids.h>
26401ca24fSsrinivas pandruvada #include <linux/hid-sensor-hub.h>
27401ca24fSsrinivas pandruvada #include "hid-ids.h"
28401ca24fSsrinivas pandruvada 
29875e36f8SSrinivas Pandruvada #define HID_SENSOR_HUB_ENUM_QUIRK	0x01
30875e36f8SSrinivas Pandruvada 
31401ca24fSsrinivas pandruvada /**
32401ca24fSsrinivas pandruvada  * struct sensor_hub_data - Hold a instance data for a HID hub device
33401ca24fSsrinivas pandruvada  * @hsdev:		Stored hid instance for current hub device.
34401ca24fSsrinivas pandruvada  * @mutex:		Mutex to serialize synchronous request.
35401ca24fSsrinivas pandruvada  * @lock:		Spin lock to protect pending request structure.
36401ca24fSsrinivas pandruvada  * @dyn_callback_list:	Holds callback function
37401ca24fSsrinivas pandruvada  * @dyn_callback_lock:	spin lock to protect callback list
38401ca24fSsrinivas pandruvada  * @hid_sensor_hub_client_devs:	Stores all MFD cells for a hub instance.
39401ca24fSsrinivas pandruvada  * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
40ca2ed12fSSrinivas Pandruvada  * @ref_cnt:		Number of MFD clients have opened this device
41401ca24fSsrinivas pandruvada  */
42401ca24fSsrinivas pandruvada struct sensor_hub_data {
43401ca24fSsrinivas pandruvada 	struct mutex mutex;
44401ca24fSsrinivas pandruvada 	spinlock_t lock;
45401ca24fSsrinivas pandruvada 	struct list_head dyn_callback_list;
46401ca24fSsrinivas pandruvada 	spinlock_t dyn_callback_lock;
47401ca24fSsrinivas pandruvada 	struct mfd_cell *hid_sensor_hub_client_devs;
48401ca24fSsrinivas pandruvada 	int hid_sensor_client_cnt;
49875e36f8SSrinivas Pandruvada 	unsigned long quirks;
50ca2ed12fSSrinivas Pandruvada 	int ref_cnt;
51401ca24fSsrinivas pandruvada };
52401ca24fSsrinivas pandruvada 
53401ca24fSsrinivas pandruvada /**
54401ca24fSsrinivas pandruvada  * struct hid_sensor_hub_callbacks_list - Stores callback list
55401ca24fSsrinivas pandruvada  * @list:		list head.
56401ca24fSsrinivas pandruvada  * @usage_id:		usage id for a physical device.
57401ca24fSsrinivas pandruvada  * @usage_callback:	Stores registered callback functions.
58401ca24fSsrinivas pandruvada  * @priv:		Private data for a physical device.
59401ca24fSsrinivas pandruvada  */
60401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks_list {
61401ca24fSsrinivas pandruvada 	struct list_head list;
62401ca24fSsrinivas pandruvada 	u32 usage_id;
63ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev;
64401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks *usage_callback;
65401ca24fSsrinivas pandruvada 	void *priv;
66401ca24fSsrinivas pandruvada };
67401ca24fSsrinivas pandruvada 
68401ca24fSsrinivas pandruvada static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
69401ca24fSsrinivas pandruvada 						int dir)
70401ca24fSsrinivas pandruvada {
71401ca24fSsrinivas pandruvada 	struct hid_report *report;
72401ca24fSsrinivas pandruvada 
73401ca24fSsrinivas pandruvada 	list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
74401ca24fSsrinivas pandruvada 		if (report->id == id)
75401ca24fSsrinivas pandruvada 			return report;
76401ca24fSsrinivas pandruvada 	}
77401ca24fSsrinivas pandruvada 	hid_warn(hdev, "No report with id 0x%x found\n", id);
78401ca24fSsrinivas pandruvada 
79401ca24fSsrinivas pandruvada 	return NULL;
80401ca24fSsrinivas pandruvada }
81401ca24fSsrinivas pandruvada 
82ca2ed12fSSrinivas Pandruvada static int sensor_hub_get_physical_device_count(struct hid_device *hdev)
83401ca24fSsrinivas pandruvada {
84ca2ed12fSSrinivas Pandruvada 	int i;
85ca2ed12fSSrinivas Pandruvada 	int count = 0;
86401ca24fSsrinivas pandruvada 
87ca2ed12fSSrinivas Pandruvada 	for (i = 0; i < hdev->maxcollection; ++i) {
88ca2ed12fSSrinivas Pandruvada 		struct hid_collection *collection = &hdev->collection[i];
89ca2ed12fSSrinivas Pandruvada 		if (collection->type == HID_COLLECTION_PHYSICAL)
90ca2ed12fSSrinivas Pandruvada 			++count;
91401ca24fSsrinivas pandruvada 	}
92401ca24fSsrinivas pandruvada 
93ca2ed12fSSrinivas Pandruvada 	return count;
94401ca24fSsrinivas pandruvada }
95401ca24fSsrinivas pandruvada 
96401ca24fSsrinivas pandruvada static void sensor_hub_fill_attr_info(
97401ca24fSsrinivas pandruvada 		struct hid_sensor_hub_attribute_info *info,
989f740ffaSSrinivas Pandruvada 		s32 index, s32 report_id, struct hid_field *field)
99401ca24fSsrinivas pandruvada {
100401ca24fSsrinivas pandruvada 	info->index = index;
101401ca24fSsrinivas pandruvada 	info->report_id = report_id;
1029f740ffaSSrinivas Pandruvada 	info->units = field->unit;
1039f740ffaSSrinivas Pandruvada 	info->unit_expo = field->unit_exponent;
1049f740ffaSSrinivas Pandruvada 	info->size = (field->report_size * field->report_count)/8;
1059f740ffaSSrinivas Pandruvada 	info->logical_minimum = field->logical_minimum;
1069f740ffaSSrinivas Pandruvada 	info->logical_maximum = field->logical_maximum;
107401ca24fSsrinivas pandruvada }
108401ca24fSsrinivas pandruvada 
109401ca24fSsrinivas pandruvada static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
110401ca24fSsrinivas pandruvada 					struct hid_device *hdev,
111ca2ed12fSSrinivas Pandruvada 					u32 usage_id,
112ca2ed12fSSrinivas Pandruvada 					int collection_index,
113ca2ed12fSSrinivas Pandruvada 					struct hid_sensor_hub_device **hsdev,
114ca2ed12fSSrinivas Pandruvada 					void **priv)
115401ca24fSsrinivas pandruvada {
116401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
117401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
118401ca24fSsrinivas pandruvada 
119401ca24fSsrinivas pandruvada 	spin_lock(&pdata->dyn_callback_lock);
120401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
121ca2ed12fSSrinivas Pandruvada 		if (callback->usage_id == usage_id &&
122ca2ed12fSSrinivas Pandruvada 			(collection_index >=
123ca2ed12fSSrinivas Pandruvada 				callback->hsdev->start_collection_index) &&
124ca2ed12fSSrinivas Pandruvada 			(collection_index <
125ca2ed12fSSrinivas Pandruvada 				callback->hsdev->end_collection_index)) {
126401ca24fSsrinivas pandruvada 			*priv = callback->priv;
127ca2ed12fSSrinivas Pandruvada 			*hsdev = callback->hsdev;
128401ca24fSsrinivas pandruvada 			spin_unlock(&pdata->dyn_callback_lock);
129401ca24fSsrinivas pandruvada 			return callback->usage_callback;
130401ca24fSsrinivas pandruvada 		}
131401ca24fSsrinivas pandruvada 	spin_unlock(&pdata->dyn_callback_lock);
132401ca24fSsrinivas pandruvada 
133401ca24fSsrinivas pandruvada 	return NULL;
134401ca24fSsrinivas pandruvada }
135401ca24fSsrinivas pandruvada 
136401ca24fSsrinivas pandruvada int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
137401ca24fSsrinivas pandruvada 			u32 usage_id,
138401ca24fSsrinivas pandruvada 			struct hid_sensor_hub_callbacks *usage_callback)
139401ca24fSsrinivas pandruvada {
140401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
141401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
1420ccf091dSJiri Kosina 	unsigned long flags;
143401ca24fSsrinivas pandruvada 
1440ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
145401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
146ca2ed12fSSrinivas Pandruvada 		if (callback->usage_id == usage_id &&
147ca2ed12fSSrinivas Pandruvada 						callback->hsdev == hsdev) {
1480ccf091dSJiri Kosina 			spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
149401ca24fSsrinivas pandruvada 			return -EINVAL;
150401ca24fSsrinivas pandruvada 		}
1512b7c4b8eSDan Carpenter 	callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
152401ca24fSsrinivas pandruvada 	if (!callback) {
1530ccf091dSJiri Kosina 		spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
154401ca24fSsrinivas pandruvada 		return -ENOMEM;
155401ca24fSsrinivas pandruvada 	}
156ca2ed12fSSrinivas Pandruvada 	callback->hsdev = hsdev;
157401ca24fSsrinivas pandruvada 	callback->usage_callback = usage_callback;
158401ca24fSsrinivas pandruvada 	callback->usage_id = usage_id;
159401ca24fSsrinivas pandruvada 	callback->priv = NULL;
160401ca24fSsrinivas pandruvada 	list_add_tail(&callback->list, &pdata->dyn_callback_list);
1610ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
162401ca24fSsrinivas pandruvada 
163401ca24fSsrinivas pandruvada 	return 0;
164401ca24fSsrinivas pandruvada }
165401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
166401ca24fSsrinivas pandruvada 
167401ca24fSsrinivas pandruvada int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
168401ca24fSsrinivas pandruvada 				u32 usage_id)
169401ca24fSsrinivas pandruvada {
170401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
171401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
1720ccf091dSJiri Kosina 	unsigned long flags;
173401ca24fSsrinivas pandruvada 
1740ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
175401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
176ca2ed12fSSrinivas Pandruvada 		if (callback->usage_id == usage_id &&
177ca2ed12fSSrinivas Pandruvada 						callback->hsdev == hsdev) {
178401ca24fSsrinivas pandruvada 			list_del(&callback->list);
179401ca24fSsrinivas pandruvada 			kfree(callback);
180401ca24fSsrinivas pandruvada 			break;
181401ca24fSsrinivas pandruvada 		}
1820ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
183401ca24fSsrinivas pandruvada 
184401ca24fSsrinivas pandruvada 	return 0;
185401ca24fSsrinivas pandruvada }
186401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
187401ca24fSsrinivas pandruvada 
188401ca24fSsrinivas pandruvada int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
189401ca24fSsrinivas pandruvada 				u32 field_index, s32 value)
190401ca24fSsrinivas pandruvada {
191401ca24fSsrinivas pandruvada 	struct hid_report *report;
192401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
193401ca24fSsrinivas pandruvada 	int ret = 0;
194401ca24fSsrinivas pandruvada 
195401ca24fSsrinivas pandruvada 	mutex_lock(&data->mutex);
196401ca24fSsrinivas pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
197401ca24fSsrinivas pandruvada 	if (!report || (field_index >= report->maxfield)) {
198401ca24fSsrinivas pandruvada 		ret = -EINVAL;
199401ca24fSsrinivas pandruvada 		goto done_proc;
200401ca24fSsrinivas pandruvada 	}
201401ca24fSsrinivas pandruvada 	hid_set_field(report->field[field_index], 0, value);
202d8814272SBenjamin Tissoires 	hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
203b7966a4dSBenjamin Tissoires 	hid_hw_wait(hsdev->hdev);
204401ca24fSsrinivas pandruvada 
205401ca24fSsrinivas pandruvada done_proc:
206401ca24fSsrinivas pandruvada 	mutex_unlock(&data->mutex);
207401ca24fSsrinivas pandruvada 
208401ca24fSsrinivas pandruvada 	return ret;
209401ca24fSsrinivas pandruvada }
210401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
211401ca24fSsrinivas pandruvada 
212401ca24fSsrinivas pandruvada int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
213401ca24fSsrinivas pandruvada 				u32 field_index, s32 *value)
214401ca24fSsrinivas pandruvada {
215401ca24fSsrinivas pandruvada 	struct hid_report *report;
216401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
217401ca24fSsrinivas pandruvada 	int ret = 0;
218401ca24fSsrinivas pandruvada 
219401ca24fSsrinivas pandruvada 	mutex_lock(&data->mutex);
220401ca24fSsrinivas pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
2219e891025SKees Cook 	if (!report || (field_index >= report->maxfield) ||
2229e891025SKees Cook 	    report->field[field_index]->report_count < 1) {
223401ca24fSsrinivas pandruvada 		ret = -EINVAL;
224401ca24fSsrinivas pandruvada 		goto done_proc;
225401ca24fSsrinivas pandruvada 	}
226d8814272SBenjamin Tissoires 	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
227b7966a4dSBenjamin Tissoires 	hid_hw_wait(hsdev->hdev);
228401ca24fSsrinivas pandruvada 	*value = report->field[field_index]->value[0];
229401ca24fSsrinivas pandruvada 
230401ca24fSsrinivas pandruvada done_proc:
231401ca24fSsrinivas pandruvada 	mutex_unlock(&data->mutex);
232401ca24fSsrinivas pandruvada 
233401ca24fSsrinivas pandruvada 	return ret;
234401ca24fSsrinivas pandruvada }
235401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
236401ca24fSsrinivas pandruvada 
237401ca24fSsrinivas pandruvada 
238401ca24fSsrinivas pandruvada int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
239401ca24fSsrinivas pandruvada 					u32 usage_id,
240401ca24fSsrinivas pandruvada 					u32 attr_usage_id, u32 report_id)
241401ca24fSsrinivas pandruvada {
242401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
243401ca24fSsrinivas pandruvada 	unsigned long flags;
244401ca24fSsrinivas pandruvada 	struct hid_report *report;
245401ca24fSsrinivas pandruvada 	int ret_val = 0;
246401ca24fSsrinivas pandruvada 
247*e651a1daSSrinivas Pandruvada 	mutex_lock(&hsdev->mutex);
248*e651a1daSSrinivas Pandruvada 	memset(&hsdev->pending, 0, sizeof(hsdev->pending));
249*e651a1daSSrinivas Pandruvada 	init_completion(&hsdev->pending.ready);
250*e651a1daSSrinivas Pandruvada 	hsdev->pending.usage_id = usage_id;
251*e651a1daSSrinivas Pandruvada 	hsdev->pending.attr_usage_id = attr_usage_id;
252*e651a1daSSrinivas Pandruvada 	hsdev->pending.raw_size = 0;
253401ca24fSsrinivas pandruvada 
254401ca24fSsrinivas pandruvada 	spin_lock_irqsave(&data->lock, flags);
255*e651a1daSSrinivas Pandruvada 	hsdev->pending.status = true;
256f74346a0SSrinivas Pandruvada 	spin_unlock_irqrestore(&data->lock, flags);
257401ca24fSsrinivas pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
258f74346a0SSrinivas Pandruvada 	if (!report)
259401ca24fSsrinivas pandruvada 		goto err_free;
260f74346a0SSrinivas Pandruvada 
261*e651a1daSSrinivas Pandruvada 	mutex_lock(&data->mutex);
262d8814272SBenjamin Tissoires 	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
263*e651a1daSSrinivas Pandruvada 	mutex_unlock(&data->mutex);
264*e651a1daSSrinivas Pandruvada 	wait_for_completion_interruptible_timeout(&hsdev->pending.ready, HZ*5);
265*e651a1daSSrinivas Pandruvada 	switch (hsdev->pending.raw_size) {
266401ca24fSsrinivas pandruvada 	case 1:
267*e651a1daSSrinivas Pandruvada 		ret_val = *(u8 *)hsdev->pending.raw_data;
268401ca24fSsrinivas pandruvada 		break;
269401ca24fSsrinivas pandruvada 	case 2:
270*e651a1daSSrinivas Pandruvada 		ret_val = *(u16 *)hsdev->pending.raw_data;
271401ca24fSsrinivas pandruvada 		break;
272401ca24fSsrinivas pandruvada 	case 4:
273*e651a1daSSrinivas Pandruvada 		ret_val = *(u32 *)hsdev->pending.raw_data;
274401ca24fSsrinivas pandruvada 		break;
275401ca24fSsrinivas pandruvada 	default:
276401ca24fSsrinivas pandruvada 		ret_val = 0;
277401ca24fSsrinivas pandruvada 	}
278*e651a1daSSrinivas Pandruvada 	kfree(hsdev->pending.raw_data);
279401ca24fSsrinivas pandruvada 
280401ca24fSsrinivas pandruvada err_free:
281*e651a1daSSrinivas Pandruvada 	hsdev->pending.status = false;
282*e651a1daSSrinivas Pandruvada 	mutex_unlock(&hsdev->mutex);
283401ca24fSsrinivas pandruvada 
284401ca24fSsrinivas pandruvada 	return ret_val;
285401ca24fSsrinivas pandruvada }
286401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
287401ca24fSsrinivas pandruvada 
288e02cee48SSrinivas Pandruvada int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
289e02cee48SSrinivas Pandruvada 				u32 report_id, int field_index, u32 usage_id)
290e02cee48SSrinivas Pandruvada {
291e02cee48SSrinivas Pandruvada 	struct hid_report *report;
292e02cee48SSrinivas Pandruvada 	struct hid_field *field;
293e02cee48SSrinivas Pandruvada 	int i;
294e02cee48SSrinivas Pandruvada 
295e02cee48SSrinivas Pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
296e02cee48SSrinivas Pandruvada 	if (!report || (field_index >= report->maxfield))
297e02cee48SSrinivas Pandruvada 		goto done_proc;
298e02cee48SSrinivas Pandruvada 
299e02cee48SSrinivas Pandruvada 	field = report->field[field_index];
300e02cee48SSrinivas Pandruvada 	for (i = 0; i < field->maxusage; ++i) {
301e02cee48SSrinivas Pandruvada 		if (field->usage[i].hid == usage_id)
302e02cee48SSrinivas Pandruvada 			return field->usage[i].usage_index;
303e02cee48SSrinivas Pandruvada 	}
304e02cee48SSrinivas Pandruvada 
305e02cee48SSrinivas Pandruvada done_proc:
306e02cee48SSrinivas Pandruvada 	return -EINVAL;
307e02cee48SSrinivas Pandruvada }
308e02cee48SSrinivas Pandruvada EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index);
309e02cee48SSrinivas Pandruvada 
310401ca24fSsrinivas pandruvada int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
311401ca24fSsrinivas pandruvada 				u8 type,
312401ca24fSsrinivas pandruvada 				u32 usage_id,
313401ca24fSsrinivas pandruvada 				u32 attr_usage_id,
314401ca24fSsrinivas pandruvada 				struct hid_sensor_hub_attribute_info *info)
315401ca24fSsrinivas pandruvada {
316401ca24fSsrinivas pandruvada 	int ret = -1;
317ca2ed12fSSrinivas Pandruvada 	int i;
318401ca24fSsrinivas pandruvada 	struct hid_report *report;
319401ca24fSsrinivas pandruvada 	struct hid_field *field;
320401ca24fSsrinivas pandruvada 	struct hid_report_enum *report_enum;
321401ca24fSsrinivas pandruvada 	struct hid_device *hdev = hsdev->hdev;
322401ca24fSsrinivas pandruvada 
323401ca24fSsrinivas pandruvada 	/* Initialize with defaults */
324401ca24fSsrinivas pandruvada 	info->usage_id = usage_id;
325401ca24fSsrinivas pandruvada 	info->attrib_id = attr_usage_id;
326401ca24fSsrinivas pandruvada 	info->report_id = -1;
327401ca24fSsrinivas pandruvada 	info->index = -1;
328401ca24fSsrinivas pandruvada 	info->units = -1;
329401ca24fSsrinivas pandruvada 	info->unit_expo = -1;
330401ca24fSsrinivas pandruvada 
331401ca24fSsrinivas pandruvada 	report_enum = &hdev->report_enum[type];
332401ca24fSsrinivas pandruvada 	list_for_each_entry(report, &report_enum->report_list, list) {
333401ca24fSsrinivas pandruvada 		for (i = 0; i < report->maxfield; ++i) {
334401ca24fSsrinivas pandruvada 			field = report->field[i];
335ca2ed12fSSrinivas Pandruvada 			if (field->maxusage) {
336401ca24fSsrinivas pandruvada 				if (field->physical == usage_id &&
337ca2ed12fSSrinivas Pandruvada 					(field->logical == attr_usage_id ||
338ca2ed12fSSrinivas Pandruvada 					field->usage[0].hid ==
339ca2ed12fSSrinivas Pandruvada 							attr_usage_id) &&
340ca2ed12fSSrinivas Pandruvada 					(field->usage[0].collection_index >=
341ca2ed12fSSrinivas Pandruvada 					hsdev->start_collection_index) &&
342ca2ed12fSSrinivas Pandruvada 					(field->usage[0].collection_index <
343ca2ed12fSSrinivas Pandruvada 					hsdev->end_collection_index)) {
344ca2ed12fSSrinivas Pandruvada 
345ca2ed12fSSrinivas Pandruvada 					sensor_hub_fill_attr_info(info, i,
346ca2ed12fSSrinivas Pandruvada 								report->id,
3479f740ffaSSrinivas Pandruvada 								field);
348401ca24fSsrinivas pandruvada 					ret = 0;
349401ca24fSsrinivas pandruvada 					break;
350401ca24fSsrinivas pandruvada 				}
351401ca24fSsrinivas pandruvada 			}
352401ca24fSsrinivas pandruvada 		}
353401ca24fSsrinivas pandruvada 
354ca2ed12fSSrinivas Pandruvada 	}
355ca2ed12fSSrinivas Pandruvada 
356401ca24fSsrinivas pandruvada 	return ret;
357401ca24fSsrinivas pandruvada }
358401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
359401ca24fSsrinivas pandruvada 
360401ca24fSsrinivas pandruvada #ifdef CONFIG_PM
361401ca24fSsrinivas pandruvada static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
362401ca24fSsrinivas pandruvada {
363401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
364401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
3650ccf091dSJiri Kosina 	unsigned long flags;
366401ca24fSsrinivas pandruvada 
367401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " sensor_hub_suspend\n");
3680ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
369401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
370401ca24fSsrinivas pandruvada 		if (callback->usage_callback->suspend)
371401ca24fSsrinivas pandruvada 			callback->usage_callback->suspend(
372ca2ed12fSSrinivas Pandruvada 					callback->hsdev, callback->priv);
373401ca24fSsrinivas pandruvada 	}
3740ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
375401ca24fSsrinivas pandruvada 
376401ca24fSsrinivas pandruvada 	return 0;
377401ca24fSsrinivas pandruvada }
378401ca24fSsrinivas pandruvada 
379401ca24fSsrinivas pandruvada static int sensor_hub_resume(struct hid_device *hdev)
380401ca24fSsrinivas pandruvada {
381401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
382401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
3830ccf091dSJiri Kosina 	unsigned long flags;
384401ca24fSsrinivas pandruvada 
385401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " sensor_hub_resume\n");
3860ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
387401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
388401ca24fSsrinivas pandruvada 		if (callback->usage_callback->resume)
389401ca24fSsrinivas pandruvada 			callback->usage_callback->resume(
390ca2ed12fSSrinivas Pandruvada 					callback->hsdev, callback->priv);
391401ca24fSsrinivas pandruvada 	}
3920ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
393401ca24fSsrinivas pandruvada 
394401ca24fSsrinivas pandruvada 	return 0;
395401ca24fSsrinivas pandruvada }
396401ca24fSsrinivas pandruvada 
397401ca24fSsrinivas pandruvada static int sensor_hub_reset_resume(struct hid_device *hdev)
398401ca24fSsrinivas pandruvada {
399401ca24fSsrinivas pandruvada 	return 0;
400401ca24fSsrinivas pandruvada }
401401ca24fSsrinivas pandruvada #endif
4025902fde1SAndy Shevchenko 
403401ca24fSsrinivas pandruvada /*
404401ca24fSsrinivas pandruvada  * Handle raw report as sent by device
405401ca24fSsrinivas pandruvada  */
406401ca24fSsrinivas pandruvada static int sensor_hub_raw_event(struct hid_device *hdev,
407401ca24fSsrinivas pandruvada 		struct hid_report *report, u8 *raw_data, int size)
408401ca24fSsrinivas pandruvada {
409401ca24fSsrinivas pandruvada 	int i;
410401ca24fSsrinivas pandruvada 	u8 *ptr;
411401ca24fSsrinivas pandruvada 	int sz;
412401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
413401ca24fSsrinivas pandruvada 	unsigned long flags;
414401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks *callback = NULL;
415401ca24fSsrinivas pandruvada 	struct hid_collection *collection = NULL;
416401ca24fSsrinivas pandruvada 	void *priv = NULL;
417ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev = NULL;
418401ca24fSsrinivas pandruvada 
419401ca24fSsrinivas pandruvada 	hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
420401ca24fSsrinivas pandruvada 			 report->id, size, report->type);
421401ca24fSsrinivas pandruvada 	hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
422401ca24fSsrinivas pandruvada 	if (report->type != HID_INPUT_REPORT)
423401ca24fSsrinivas pandruvada 		return 1;
424401ca24fSsrinivas pandruvada 
425401ca24fSsrinivas pandruvada 	ptr = raw_data;
426401ca24fSsrinivas pandruvada 	ptr++; /* Skip report id */
427401ca24fSsrinivas pandruvada 
428401ca24fSsrinivas pandruvada 	spin_lock_irqsave(&pdata->lock, flags);
429401ca24fSsrinivas pandruvada 
430401ca24fSsrinivas pandruvada 	for (i = 0; i < report->maxfield; ++i) {
431401ca24fSsrinivas pandruvada 		hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
432401ca24fSsrinivas pandruvada 				i, report->field[i]->usage->collection_index,
433401ca24fSsrinivas pandruvada 				report->field[i]->usage->hid,
434d4b1bba7SSrinivas Pandruvada 				(report->field[i]->report_size *
435d4b1bba7SSrinivas Pandruvada 					report->field[i]->report_count)/8);
436d4b1bba7SSrinivas Pandruvada 		sz = (report->field[i]->report_size *
437d4b1bba7SSrinivas Pandruvada 					report->field[i]->report_count)/8;
438401ca24fSsrinivas pandruvada 		collection = &hdev->collection[
439401ca24fSsrinivas pandruvada 				report->field[i]->usage->collection_index];
440401ca24fSsrinivas pandruvada 		hid_dbg(hdev, "collection->usage %x\n",
441401ca24fSsrinivas pandruvada 					collection->usage);
442ca2ed12fSSrinivas Pandruvada 
443ca2ed12fSSrinivas Pandruvada 		callback = sensor_hub_get_callback(hdev,
444401ca24fSsrinivas pandruvada 				report->field[i]->physical,
445ca2ed12fSSrinivas Pandruvada 				report->field[i]->usage[0].collection_index,
446ca2ed12fSSrinivas Pandruvada 				&hsdev, &priv);
447*e651a1daSSrinivas Pandruvada 		if (!callback) {
448*e651a1daSSrinivas Pandruvada 			ptr += sz;
449*e651a1daSSrinivas Pandruvada 			continue;
450*e651a1daSSrinivas Pandruvada 		}
451*e651a1daSSrinivas Pandruvada 		if (hsdev->pending.status && hsdev->pending.attr_usage_id ==
452*e651a1daSSrinivas Pandruvada 				report->field[i]->usage->hid) {
453*e651a1daSSrinivas Pandruvada 			hid_dbg(hdev, "data was pending ...\n");
454*e651a1daSSrinivas Pandruvada 			hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
455*e651a1daSSrinivas Pandruvada 			if (hsdev->pending.raw_data)
456*e651a1daSSrinivas Pandruvada 				hsdev->pending.raw_size = sz;
457*e651a1daSSrinivas Pandruvada 			else
458*e651a1daSSrinivas Pandruvada 				hsdev->pending.raw_size = 0;
459*e651a1daSSrinivas Pandruvada 			complete(&hsdev->pending.ready);
460*e651a1daSSrinivas Pandruvada 		}
461*e651a1daSSrinivas Pandruvada 		if (callback->capture_sample) {
462401ca24fSsrinivas pandruvada 			if (report->field[i]->logical)
463ca2ed12fSSrinivas Pandruvada 				callback->capture_sample(hsdev,
464401ca24fSsrinivas pandruvada 					report->field[i]->logical, sz, ptr,
465401ca24fSsrinivas pandruvada 					callback->pdev);
466401ca24fSsrinivas pandruvada 			else
467ca2ed12fSSrinivas Pandruvada 				callback->capture_sample(hsdev,
468401ca24fSsrinivas pandruvada 					report->field[i]->usage->hid, sz, ptr,
469401ca24fSsrinivas pandruvada 					callback->pdev);
470401ca24fSsrinivas pandruvada 		}
471401ca24fSsrinivas pandruvada 		ptr += sz;
472401ca24fSsrinivas pandruvada 	}
473401ca24fSsrinivas pandruvada 	if (callback && collection && callback->send_event)
474ca2ed12fSSrinivas Pandruvada 		callback->send_event(hsdev, collection->usage,
475401ca24fSsrinivas pandruvada 				callback->pdev);
476401ca24fSsrinivas pandruvada 	spin_unlock_irqrestore(&pdata->lock, flags);
477401ca24fSsrinivas pandruvada 
478401ca24fSsrinivas pandruvada 	return 1;
479401ca24fSsrinivas pandruvada }
480401ca24fSsrinivas pandruvada 
4811df3a401SSrinivas Pandruvada int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
4821df3a401SSrinivas Pandruvada {
4831df3a401SSrinivas Pandruvada 	int ret = 0;
4841df3a401SSrinivas Pandruvada 	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
4851df3a401SSrinivas Pandruvada 
4861df3a401SSrinivas Pandruvada 	mutex_lock(&data->mutex);
487ca2ed12fSSrinivas Pandruvada 	if (!data->ref_cnt) {
4881df3a401SSrinivas Pandruvada 		ret = hid_hw_open(hsdev->hdev);
4891df3a401SSrinivas Pandruvada 		if (ret) {
4901df3a401SSrinivas Pandruvada 			hid_err(hsdev->hdev, "failed to open hid device\n");
4911df3a401SSrinivas Pandruvada 			mutex_unlock(&data->mutex);
4921df3a401SSrinivas Pandruvada 			return ret;
4931df3a401SSrinivas Pandruvada 		}
4941df3a401SSrinivas Pandruvada 	}
495ca2ed12fSSrinivas Pandruvada 	data->ref_cnt++;
4961df3a401SSrinivas Pandruvada 	mutex_unlock(&data->mutex);
4971df3a401SSrinivas Pandruvada 
4981df3a401SSrinivas Pandruvada 	return ret;
4991df3a401SSrinivas Pandruvada }
5001df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_open);
5011df3a401SSrinivas Pandruvada 
5021df3a401SSrinivas Pandruvada void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
5031df3a401SSrinivas Pandruvada {
5041df3a401SSrinivas Pandruvada 	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
5051df3a401SSrinivas Pandruvada 
5061df3a401SSrinivas Pandruvada 	mutex_lock(&data->mutex);
507ca2ed12fSSrinivas Pandruvada 	data->ref_cnt--;
508ca2ed12fSSrinivas Pandruvada 	if (!data->ref_cnt)
5091df3a401SSrinivas Pandruvada 		hid_hw_close(hsdev->hdev);
5101df3a401SSrinivas Pandruvada 	mutex_unlock(&data->mutex);
5111df3a401SSrinivas Pandruvada }
5121df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_close);
5131df3a401SSrinivas Pandruvada 
514875e36f8SSrinivas Pandruvada static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
515875e36f8SSrinivas Pandruvada 		unsigned int *rsize)
516875e36f8SSrinivas Pandruvada {
517875e36f8SSrinivas Pandruvada 	int index;
518875e36f8SSrinivas Pandruvada 	struct sensor_hub_data *sd =  hid_get_drvdata(hdev);
519875e36f8SSrinivas Pandruvada 	unsigned char report_block[] = {
520875e36f8SSrinivas Pandruvada 				0x0a,  0x16, 0x03, 0x15, 0x00, 0x25, 0x05};
521875e36f8SSrinivas Pandruvada 	unsigned char power_block[] = {
522875e36f8SSrinivas Pandruvada 				0x0a,  0x19, 0x03, 0x15, 0x00, 0x25, 0x05};
523875e36f8SSrinivas Pandruvada 
524875e36f8SSrinivas Pandruvada 	if (!(sd->quirks & HID_SENSOR_HUB_ENUM_QUIRK)) {
525875e36f8SSrinivas Pandruvada 		hid_dbg(hdev, "No Enum quirks\n");
526875e36f8SSrinivas Pandruvada 		return rdesc;
527875e36f8SSrinivas Pandruvada 	}
528875e36f8SSrinivas Pandruvada 
529875e36f8SSrinivas Pandruvada 	/* Looks for power and report state usage id and force to 1 */
530875e36f8SSrinivas Pandruvada 	for (index = 0; index < *rsize; ++index) {
531875e36f8SSrinivas Pandruvada 		if (((*rsize - index) > sizeof(report_block)) &&
532875e36f8SSrinivas Pandruvada 			!memcmp(&rdesc[index], report_block,
533875e36f8SSrinivas Pandruvada 						sizeof(report_block))) {
534875e36f8SSrinivas Pandruvada 			rdesc[index + 4] = 0x01;
535875e36f8SSrinivas Pandruvada 			index += sizeof(report_block);
536875e36f8SSrinivas Pandruvada 		}
537875e36f8SSrinivas Pandruvada 		if (((*rsize - index) > sizeof(power_block)) &&
538875e36f8SSrinivas Pandruvada 			!memcmp(&rdesc[index], power_block,
539875e36f8SSrinivas Pandruvada 						sizeof(power_block))) {
540875e36f8SSrinivas Pandruvada 			rdesc[index + 4] = 0x01;
541875e36f8SSrinivas Pandruvada 			index += sizeof(power_block);
542875e36f8SSrinivas Pandruvada 		}
543875e36f8SSrinivas Pandruvada 	}
544875e36f8SSrinivas Pandruvada 
545875e36f8SSrinivas Pandruvada 	return rdesc;
546875e36f8SSrinivas Pandruvada }
547875e36f8SSrinivas Pandruvada 
548401ca24fSsrinivas pandruvada static int sensor_hub_probe(struct hid_device *hdev,
549401ca24fSsrinivas pandruvada 				const struct hid_device_id *id)
550401ca24fSsrinivas pandruvada {
551401ca24fSsrinivas pandruvada 	int ret;
552401ca24fSsrinivas pandruvada 	struct sensor_hub_data *sd;
553401ca24fSsrinivas pandruvada 	int i;
554401ca24fSsrinivas pandruvada 	char *name;
555401ca24fSsrinivas pandruvada 	int dev_cnt;
556ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev;
557ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *last_hsdev = NULL;
558401ca24fSsrinivas pandruvada 
559905cc199SAndy Shevchenko 	sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
560401ca24fSsrinivas pandruvada 	if (!sd) {
561401ca24fSsrinivas pandruvada 		hid_err(hdev, "cannot allocate Sensor data\n");
562401ca24fSsrinivas pandruvada 		return -ENOMEM;
563401ca24fSsrinivas pandruvada 	}
564ca2ed12fSSrinivas Pandruvada 
565401ca24fSsrinivas pandruvada 	hid_set_drvdata(hdev, sd);
566875e36f8SSrinivas Pandruvada 	sd->quirks = id->driver_data;
567ca2ed12fSSrinivas Pandruvada 
568401ca24fSsrinivas pandruvada 	spin_lock_init(&sd->lock);
569401ca24fSsrinivas pandruvada 	spin_lock_init(&sd->dyn_callback_lock);
570401ca24fSsrinivas pandruvada 	mutex_init(&sd->mutex);
571401ca24fSsrinivas pandruvada 	ret = hid_parse(hdev);
572401ca24fSsrinivas pandruvada 	if (ret) {
573401ca24fSsrinivas pandruvada 		hid_err(hdev, "parse failed\n");
574905cc199SAndy Shevchenko 		return ret;
575401ca24fSsrinivas pandruvada 	}
576401ca24fSsrinivas pandruvada 	INIT_LIST_HEAD(&hdev->inputs);
577401ca24fSsrinivas pandruvada 
578401ca24fSsrinivas pandruvada 	ret = hid_hw_start(hdev, 0);
579401ca24fSsrinivas pandruvada 	if (ret) {
580401ca24fSsrinivas pandruvada 		hid_err(hdev, "hw start failed\n");
581905cc199SAndy Shevchenko 		return ret;
582401ca24fSsrinivas pandruvada 	}
583401ca24fSsrinivas pandruvada 	INIT_LIST_HEAD(&sd->dyn_callback_list);
584401ca24fSsrinivas pandruvada 	sd->hid_sensor_client_cnt = 0;
585401ca24fSsrinivas pandruvada 
586ca2ed12fSSrinivas Pandruvada 	dev_cnt = sensor_hub_get_physical_device_count(hdev);
587401ca24fSsrinivas pandruvada 	if (dev_cnt > HID_MAX_PHY_DEVICES) {
588401ca24fSsrinivas pandruvada 		hid_err(hdev, "Invalid Physical device count\n");
589401ca24fSsrinivas pandruvada 		ret = -EINVAL;
5901df3a401SSrinivas Pandruvada 		goto err_stop_hw;
591401ca24fSsrinivas pandruvada 	}
5925be5db24SHimangi Saraogi 	sd->hid_sensor_hub_client_devs = devm_kzalloc(&hdev->dev, dev_cnt *
593401ca24fSsrinivas pandruvada 						      sizeof(struct mfd_cell),
594401ca24fSsrinivas pandruvada 						      GFP_KERNEL);
595401ca24fSsrinivas pandruvada 	if (sd->hid_sensor_hub_client_devs == NULL) {
596f2f13a68SAxel Lin 		hid_err(hdev, "Failed to allocate memory for mfd cells\n");
597401ca24fSsrinivas pandruvada 			ret = -ENOMEM;
5981df3a401SSrinivas Pandruvada 			goto err_stop_hw;
599401ca24fSsrinivas pandruvada 	}
600ca2ed12fSSrinivas Pandruvada 
601ca2ed12fSSrinivas Pandruvada 	for (i = 0; i < hdev->maxcollection; ++i) {
602ca2ed12fSSrinivas Pandruvada 		struct hid_collection *collection = &hdev->collection[i];
603ca2ed12fSSrinivas Pandruvada 
604ca2ed12fSSrinivas Pandruvada 		if (collection->type == HID_COLLECTION_PHYSICAL) {
605ca2ed12fSSrinivas Pandruvada 
6065be5db24SHimangi Saraogi 			hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev),
6075be5db24SHimangi Saraogi 					     GFP_KERNEL);
608ca2ed12fSSrinivas Pandruvada 			if (!hsdev) {
609ca2ed12fSSrinivas Pandruvada 				hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
610ca2ed12fSSrinivas Pandruvada 				ret = -ENOMEM;
6115be5db24SHimangi Saraogi 				goto err_stop_hw;
612ca2ed12fSSrinivas Pandruvada 			}
613ca2ed12fSSrinivas Pandruvada 			hsdev->hdev = hdev;
614ca2ed12fSSrinivas Pandruvada 			hsdev->vendor_id = hdev->vendor;
615ca2ed12fSSrinivas Pandruvada 			hsdev->product_id = hdev->product;
616*e651a1daSSrinivas Pandruvada 			hsdev->usage = collection->usage;
617*e651a1daSSrinivas Pandruvada 			mutex_init(&hsdev->mutex);
618ca2ed12fSSrinivas Pandruvada 			hsdev->start_collection_index = i;
619ca2ed12fSSrinivas Pandruvada 			if (last_hsdev)
620ca2ed12fSSrinivas Pandruvada 				last_hsdev->end_collection_index = i;
621ca2ed12fSSrinivas Pandruvada 			last_hsdev = hsdev;
6225be5db24SHimangi Saraogi 			name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
6235be5db24SHimangi Saraogi 					      "HID-SENSOR-%x",
624ca2ed12fSSrinivas Pandruvada 					      collection->usage);
625401ca24fSsrinivas pandruvada 			if (name == NULL) {
626f2f13a68SAxel Lin 				hid_err(hdev, "Failed MFD device name\n");
627401ca24fSsrinivas pandruvada 					ret = -ENOMEM;
6285be5db24SHimangi Saraogi 					goto err_stop_hw;
629401ca24fSsrinivas pandruvada 			}
630401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
631401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].name = name;
632401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
633401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].platform_data =
634ca2ed12fSSrinivas Pandruvada 							hsdev;
635401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
636401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].pdata_size =
637ca2ed12fSSrinivas Pandruvada 							sizeof(*hsdev);
638ca2ed12fSSrinivas Pandruvada 			hid_dbg(hdev, "Adding %s:%d\n", name,
639ca2ed12fSSrinivas Pandruvada 					hsdev->start_collection_index);
640401ca24fSsrinivas pandruvada 			sd->hid_sensor_client_cnt++;
641401ca24fSsrinivas pandruvada 		}
642401ca24fSsrinivas pandruvada 	}
643ca2ed12fSSrinivas Pandruvada 	if (last_hsdev)
644ca2ed12fSSrinivas Pandruvada 		last_hsdev->end_collection_index = i;
645ca2ed12fSSrinivas Pandruvada 
64616b5fe29SJohan Hovold 	ret = mfd_add_hotplug_devices(&hdev->dev,
64716b5fe29SJohan Hovold 			sd->hid_sensor_hub_client_devs,
64816b5fe29SJohan Hovold 			sd->hid_sensor_client_cnt);
649401ca24fSsrinivas pandruvada 	if (ret < 0)
6505be5db24SHimangi Saraogi 		goto err_stop_hw;
651401ca24fSsrinivas pandruvada 
652401ca24fSsrinivas pandruvada 	return ret;
653401ca24fSsrinivas pandruvada 
654401ca24fSsrinivas pandruvada err_stop_hw:
655401ca24fSsrinivas pandruvada 	hid_hw_stop(hdev);
656401ca24fSsrinivas pandruvada 
657401ca24fSsrinivas pandruvada 	return ret;
658401ca24fSsrinivas pandruvada }
659401ca24fSsrinivas pandruvada 
660401ca24fSsrinivas pandruvada static void sensor_hub_remove(struct hid_device *hdev)
661401ca24fSsrinivas pandruvada {
662401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hdev);
663401ca24fSsrinivas pandruvada 	unsigned long flags;
664*e651a1daSSrinivas Pandruvada 	int i;
665401ca24fSsrinivas pandruvada 
666401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " hardware removed\n");
667401ca24fSsrinivas pandruvada 	hid_hw_close(hdev);
668f2f13a68SAxel Lin 	hid_hw_stop(hdev);
669401ca24fSsrinivas pandruvada 	spin_lock_irqsave(&data->lock, flags);
670*e651a1daSSrinivas Pandruvada 	for (i = 0; i < data->hid_sensor_client_cnt; ++i) {
671*e651a1daSSrinivas Pandruvada 		struct hid_sensor_hub_device *hsdev =
672*e651a1daSSrinivas Pandruvada 			data->hid_sensor_hub_client_devs[i].platform_data;
673*e651a1daSSrinivas Pandruvada 		if (hsdev->pending.status)
674*e651a1daSSrinivas Pandruvada 			complete(&hsdev->pending.ready);
675*e651a1daSSrinivas Pandruvada 	}
676401ca24fSsrinivas pandruvada 	spin_unlock_irqrestore(&data->lock, flags);
677401ca24fSsrinivas pandruvada 	mfd_remove_devices(&hdev->dev);
678401ca24fSsrinivas pandruvada 	hid_set_drvdata(hdev, NULL);
679401ca24fSsrinivas pandruvada 	mutex_destroy(&data->mutex);
680401ca24fSsrinivas pandruvada }
681401ca24fSsrinivas pandruvada 
682401ca24fSsrinivas pandruvada static const struct hid_device_id sensor_hub_devices[] = {
683875e36f8SSrinivas Pandruvada 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_0,
68430f58877SStephen Chandler Paul 			USB_DEVICE_ID_INTEL_HID_SENSOR_0),
685875e36f8SSrinivas Pandruvada 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
686875e36f8SSrinivas Pandruvada 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
68730f58877SStephen Chandler Paul 			USB_DEVICE_ID_INTEL_HID_SENSOR_0),
68830f58877SStephen Chandler Paul 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
68930f58877SStephen Chandler Paul 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
69030f58877SStephen Chandler Paul 			USB_DEVICE_ID_INTEL_HID_SENSOR_1),
691875e36f8SSrinivas Pandruvada 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
69200478ee8SReyad Attiyat 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
69300478ee8SReyad Attiyat 			USB_DEVICE_ID_MS_SURFACE_PRO_2),
69400478ee8SReyad Attiyat 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
69500478ee8SReyad Attiyat 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
69600478ee8SReyad Attiyat 			USB_DEVICE_ID_MS_TOUCH_COVER_2),
69700478ee8SReyad Attiyat 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
69800478ee8SReyad Attiyat 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
69900478ee8SReyad Attiyat 			USB_DEVICE_ID_MS_TYPE_COVER_2),
70000478ee8SReyad Attiyat 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
701218eb9edSArchana Patni 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
702467669c5SSrinivas Pandruvada 			USB_DEVICE_ID_STM_HID_SENSOR),
703467669c5SSrinivas Pandruvada 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
704467669c5SSrinivas Pandruvada 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
705dde3b45cSArchana Patni 			USB_DEVICE_ID_STM_HID_SENSOR_1),
706218eb9edSArchana Patni 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
707825747bbSPeter F. Patel-Schneider 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_TEXAS_INSTRUMENTS,
708825747bbSPeter F. Patel-Schneider 			USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA),
709825747bbSPeter F. Patel-Schneider 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
7100d1e4b02SMika Westerberg 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
7110d1e4b02SMika Westerberg 		     HID_ANY_ID) },
712401ca24fSsrinivas pandruvada 	{ }
713401ca24fSsrinivas pandruvada };
714401ca24fSsrinivas pandruvada MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
715401ca24fSsrinivas pandruvada 
716401ca24fSsrinivas pandruvada static struct hid_driver sensor_hub_driver = {
717401ca24fSsrinivas pandruvada 	.name = "hid-sensor-hub",
718401ca24fSsrinivas pandruvada 	.id_table = sensor_hub_devices,
719401ca24fSsrinivas pandruvada 	.probe = sensor_hub_probe,
720401ca24fSsrinivas pandruvada 	.remove = sensor_hub_remove,
721401ca24fSsrinivas pandruvada 	.raw_event = sensor_hub_raw_event,
722875e36f8SSrinivas Pandruvada 	.report_fixup = sensor_hub_report_fixup,
723401ca24fSsrinivas pandruvada #ifdef CONFIG_PM
724401ca24fSsrinivas pandruvada 	.suspend = sensor_hub_suspend,
725401ca24fSsrinivas pandruvada 	.resume = sensor_hub_resume,
726401ca24fSsrinivas pandruvada 	.reset_resume = sensor_hub_reset_resume,
727401ca24fSsrinivas pandruvada #endif
728401ca24fSsrinivas pandruvada };
729f425458eSH Hartley Sweeten module_hid_driver(sensor_hub_driver);
730401ca24fSsrinivas pandruvada 
731401ca24fSsrinivas pandruvada MODULE_DESCRIPTION("HID Sensor Hub driver");
732401ca24fSsrinivas pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
733401ca24fSsrinivas pandruvada MODULE_LICENSE("GPL");
734