xref: /linux/drivers/hid/hid-sensor-hub.c (revision cb67126f32f008b9abe97fbfca9b23a797b2458a)
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];
89*cb67126fSSrinivas Pandruvada 		if (collection->type == HID_COLLECTION_PHYSICAL ||
90*cb67126fSSrinivas Pandruvada 		    collection->type == HID_COLLECTION_APPLICATION)
91ca2ed12fSSrinivas Pandruvada 			++count;
92401ca24fSsrinivas pandruvada 	}
93401ca24fSsrinivas pandruvada 
94ca2ed12fSSrinivas Pandruvada 	return count;
95401ca24fSsrinivas pandruvada }
96401ca24fSsrinivas pandruvada 
97401ca24fSsrinivas pandruvada static void sensor_hub_fill_attr_info(
98401ca24fSsrinivas pandruvada 		struct hid_sensor_hub_attribute_info *info,
999f740ffaSSrinivas Pandruvada 		s32 index, s32 report_id, struct hid_field *field)
100401ca24fSsrinivas pandruvada {
101401ca24fSsrinivas pandruvada 	info->index = index;
102401ca24fSsrinivas pandruvada 	info->report_id = report_id;
1039f740ffaSSrinivas Pandruvada 	info->units = field->unit;
1049f740ffaSSrinivas Pandruvada 	info->unit_expo = field->unit_exponent;
1059f740ffaSSrinivas Pandruvada 	info->size = (field->report_size * field->report_count)/8;
1069f740ffaSSrinivas Pandruvada 	info->logical_minimum = field->logical_minimum;
1079f740ffaSSrinivas Pandruvada 	info->logical_maximum = field->logical_maximum;
108401ca24fSsrinivas pandruvada }
109401ca24fSsrinivas pandruvada 
110401ca24fSsrinivas pandruvada static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
111401ca24fSsrinivas pandruvada 					struct hid_device *hdev,
112ca2ed12fSSrinivas Pandruvada 					u32 usage_id,
113ca2ed12fSSrinivas Pandruvada 					int collection_index,
114ca2ed12fSSrinivas Pandruvada 					struct hid_sensor_hub_device **hsdev,
115ca2ed12fSSrinivas Pandruvada 					void **priv)
116401ca24fSsrinivas pandruvada {
117401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
118401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
119401ca24fSsrinivas pandruvada 
120401ca24fSsrinivas pandruvada 	spin_lock(&pdata->dyn_callback_lock);
121401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
122*cb67126fSSrinivas Pandruvada 		if ((callback->usage_id == usage_id ||
123*cb67126fSSrinivas Pandruvada 		     callback->usage_id == HID_USAGE_SENSOR_COLLECTION) &&
124ca2ed12fSSrinivas Pandruvada 			(collection_index >=
125ca2ed12fSSrinivas Pandruvada 				callback->hsdev->start_collection_index) &&
126ca2ed12fSSrinivas Pandruvada 			(collection_index <
127ca2ed12fSSrinivas Pandruvada 				callback->hsdev->end_collection_index)) {
128401ca24fSsrinivas pandruvada 			*priv = callback->priv;
129ca2ed12fSSrinivas Pandruvada 			*hsdev = callback->hsdev;
130401ca24fSsrinivas pandruvada 			spin_unlock(&pdata->dyn_callback_lock);
131401ca24fSsrinivas pandruvada 			return callback->usage_callback;
132401ca24fSsrinivas pandruvada 		}
133401ca24fSsrinivas pandruvada 	spin_unlock(&pdata->dyn_callback_lock);
134401ca24fSsrinivas pandruvada 
135401ca24fSsrinivas pandruvada 	return NULL;
136401ca24fSsrinivas pandruvada }
137401ca24fSsrinivas pandruvada 
138401ca24fSsrinivas pandruvada int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
139401ca24fSsrinivas pandruvada 			u32 usage_id,
140401ca24fSsrinivas pandruvada 			struct hid_sensor_hub_callbacks *usage_callback)
141401ca24fSsrinivas pandruvada {
142401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
143401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
1440ccf091dSJiri Kosina 	unsigned long flags;
145401ca24fSsrinivas pandruvada 
1460ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
147401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
148ca2ed12fSSrinivas Pandruvada 		if (callback->usage_id == usage_id &&
149ca2ed12fSSrinivas Pandruvada 						callback->hsdev == hsdev) {
1500ccf091dSJiri Kosina 			spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
151401ca24fSsrinivas pandruvada 			return -EINVAL;
152401ca24fSsrinivas pandruvada 		}
1532b7c4b8eSDan Carpenter 	callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
154401ca24fSsrinivas pandruvada 	if (!callback) {
1550ccf091dSJiri Kosina 		spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
156401ca24fSsrinivas pandruvada 		return -ENOMEM;
157401ca24fSsrinivas pandruvada 	}
158ca2ed12fSSrinivas Pandruvada 	callback->hsdev = hsdev;
159401ca24fSsrinivas pandruvada 	callback->usage_callback = usage_callback;
160401ca24fSsrinivas pandruvada 	callback->usage_id = usage_id;
161401ca24fSsrinivas pandruvada 	callback->priv = NULL;
162*cb67126fSSrinivas Pandruvada 	/*
163*cb67126fSSrinivas Pandruvada 	 * If there is a handler registered for the collection type, then
164*cb67126fSSrinivas Pandruvada 	 * it will handle all reports for sensors in this collection. If
165*cb67126fSSrinivas Pandruvada 	 * there is also an individual sensor handler registration, then
166*cb67126fSSrinivas Pandruvada 	 * we want to make sure that the reports are directed to collection
167*cb67126fSSrinivas Pandruvada 	 * handler, as this may be a fusion sensor. So add collection handlers
168*cb67126fSSrinivas Pandruvada 	 * to the beginning of the list, so that they are matched first.
169*cb67126fSSrinivas Pandruvada 	 */
170*cb67126fSSrinivas Pandruvada 	if (usage_id == HID_USAGE_SENSOR_COLLECTION)
171*cb67126fSSrinivas Pandruvada 		list_add(&callback->list, &pdata->dyn_callback_list);
172*cb67126fSSrinivas Pandruvada 	else
173401ca24fSsrinivas pandruvada 		list_add_tail(&callback->list, &pdata->dyn_callback_list);
1740ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
175401ca24fSsrinivas pandruvada 
176401ca24fSsrinivas pandruvada 	return 0;
177401ca24fSsrinivas pandruvada }
178401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
179401ca24fSsrinivas pandruvada 
180401ca24fSsrinivas pandruvada int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
181401ca24fSsrinivas pandruvada 				u32 usage_id)
182401ca24fSsrinivas pandruvada {
183401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
184401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
1850ccf091dSJiri Kosina 	unsigned long flags;
186401ca24fSsrinivas pandruvada 
1870ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
188401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
189ca2ed12fSSrinivas Pandruvada 		if (callback->usage_id == usage_id &&
190ca2ed12fSSrinivas Pandruvada 						callback->hsdev == hsdev) {
191401ca24fSsrinivas pandruvada 			list_del(&callback->list);
192401ca24fSsrinivas pandruvada 			kfree(callback);
193401ca24fSsrinivas pandruvada 			break;
194401ca24fSsrinivas pandruvada 		}
1950ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
196401ca24fSsrinivas pandruvada 
197401ca24fSsrinivas pandruvada 	return 0;
198401ca24fSsrinivas pandruvada }
199401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
200401ca24fSsrinivas pandruvada 
201401ca24fSsrinivas pandruvada int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
202401ca24fSsrinivas pandruvada 				u32 field_index, s32 value)
203401ca24fSsrinivas pandruvada {
204401ca24fSsrinivas pandruvada 	struct hid_report *report;
205401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
206401ca24fSsrinivas pandruvada 	int ret = 0;
207401ca24fSsrinivas pandruvada 
208401ca24fSsrinivas pandruvada 	mutex_lock(&data->mutex);
209401ca24fSsrinivas pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
210401ca24fSsrinivas pandruvada 	if (!report || (field_index >= report->maxfield)) {
211401ca24fSsrinivas pandruvada 		ret = -EINVAL;
212401ca24fSsrinivas pandruvada 		goto done_proc;
213401ca24fSsrinivas pandruvada 	}
214401ca24fSsrinivas pandruvada 	hid_set_field(report->field[field_index], 0, value);
215d8814272SBenjamin Tissoires 	hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
216b7966a4dSBenjamin Tissoires 	hid_hw_wait(hsdev->hdev);
217401ca24fSsrinivas pandruvada 
218401ca24fSsrinivas pandruvada done_proc:
219401ca24fSsrinivas pandruvada 	mutex_unlock(&data->mutex);
220401ca24fSsrinivas pandruvada 
221401ca24fSsrinivas pandruvada 	return ret;
222401ca24fSsrinivas pandruvada }
223401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
224401ca24fSsrinivas pandruvada 
225401ca24fSsrinivas pandruvada int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
226401ca24fSsrinivas pandruvada 				u32 field_index, s32 *value)
227401ca24fSsrinivas pandruvada {
228401ca24fSsrinivas pandruvada 	struct hid_report *report;
229401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
230401ca24fSsrinivas pandruvada 	int ret = 0;
231401ca24fSsrinivas pandruvada 
232401ca24fSsrinivas pandruvada 	mutex_lock(&data->mutex);
233401ca24fSsrinivas pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
2349e891025SKees Cook 	if (!report || (field_index >= report->maxfield) ||
2359e891025SKees Cook 	    report->field[field_index]->report_count < 1) {
236401ca24fSsrinivas pandruvada 		ret = -EINVAL;
237401ca24fSsrinivas pandruvada 		goto done_proc;
238401ca24fSsrinivas pandruvada 	}
239d8814272SBenjamin Tissoires 	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
240b7966a4dSBenjamin Tissoires 	hid_hw_wait(hsdev->hdev);
241401ca24fSsrinivas pandruvada 	*value = report->field[field_index]->value[0];
242401ca24fSsrinivas pandruvada 
243401ca24fSsrinivas pandruvada done_proc:
244401ca24fSsrinivas pandruvada 	mutex_unlock(&data->mutex);
245401ca24fSsrinivas pandruvada 
246401ca24fSsrinivas pandruvada 	return ret;
247401ca24fSsrinivas pandruvada }
248401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
249401ca24fSsrinivas pandruvada 
250401ca24fSsrinivas pandruvada 
251401ca24fSsrinivas pandruvada int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
252401ca24fSsrinivas pandruvada 					u32 usage_id,
253401ca24fSsrinivas pandruvada 					u32 attr_usage_id, u32 report_id)
254401ca24fSsrinivas pandruvada {
255401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
256401ca24fSsrinivas pandruvada 	unsigned long flags;
257401ca24fSsrinivas pandruvada 	struct hid_report *report;
258401ca24fSsrinivas pandruvada 	int ret_val = 0;
259401ca24fSsrinivas pandruvada 
260e651a1daSSrinivas Pandruvada 	mutex_lock(&hsdev->mutex);
261e651a1daSSrinivas Pandruvada 	memset(&hsdev->pending, 0, sizeof(hsdev->pending));
262e651a1daSSrinivas Pandruvada 	init_completion(&hsdev->pending.ready);
263e651a1daSSrinivas Pandruvada 	hsdev->pending.usage_id = usage_id;
264e651a1daSSrinivas Pandruvada 	hsdev->pending.attr_usage_id = attr_usage_id;
265e651a1daSSrinivas Pandruvada 	hsdev->pending.raw_size = 0;
266401ca24fSsrinivas pandruvada 
267401ca24fSsrinivas pandruvada 	spin_lock_irqsave(&data->lock, flags);
268e651a1daSSrinivas Pandruvada 	hsdev->pending.status = true;
269f74346a0SSrinivas Pandruvada 	spin_unlock_irqrestore(&data->lock, flags);
270401ca24fSsrinivas pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
271f74346a0SSrinivas Pandruvada 	if (!report)
272401ca24fSsrinivas pandruvada 		goto err_free;
273f74346a0SSrinivas Pandruvada 
274e651a1daSSrinivas Pandruvada 	mutex_lock(&data->mutex);
275d8814272SBenjamin Tissoires 	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
276e651a1daSSrinivas Pandruvada 	mutex_unlock(&data->mutex);
277e651a1daSSrinivas Pandruvada 	wait_for_completion_interruptible_timeout(&hsdev->pending.ready, HZ*5);
278e651a1daSSrinivas Pandruvada 	switch (hsdev->pending.raw_size) {
279401ca24fSsrinivas pandruvada 	case 1:
280e651a1daSSrinivas Pandruvada 		ret_val = *(u8 *)hsdev->pending.raw_data;
281401ca24fSsrinivas pandruvada 		break;
282401ca24fSsrinivas pandruvada 	case 2:
283e651a1daSSrinivas Pandruvada 		ret_val = *(u16 *)hsdev->pending.raw_data;
284401ca24fSsrinivas pandruvada 		break;
285401ca24fSsrinivas pandruvada 	case 4:
286e651a1daSSrinivas Pandruvada 		ret_val = *(u32 *)hsdev->pending.raw_data;
287401ca24fSsrinivas pandruvada 		break;
288401ca24fSsrinivas pandruvada 	default:
289401ca24fSsrinivas pandruvada 		ret_val = 0;
290401ca24fSsrinivas pandruvada 	}
291e651a1daSSrinivas Pandruvada 	kfree(hsdev->pending.raw_data);
292401ca24fSsrinivas pandruvada 
293401ca24fSsrinivas pandruvada err_free:
294e651a1daSSrinivas Pandruvada 	hsdev->pending.status = false;
295e651a1daSSrinivas Pandruvada 	mutex_unlock(&hsdev->mutex);
296401ca24fSsrinivas pandruvada 
297401ca24fSsrinivas pandruvada 	return ret_val;
298401ca24fSsrinivas pandruvada }
299401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
300401ca24fSsrinivas pandruvada 
301e02cee48SSrinivas Pandruvada int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
302e02cee48SSrinivas Pandruvada 				u32 report_id, int field_index, u32 usage_id)
303e02cee48SSrinivas Pandruvada {
304e02cee48SSrinivas Pandruvada 	struct hid_report *report;
305e02cee48SSrinivas Pandruvada 	struct hid_field *field;
306e02cee48SSrinivas Pandruvada 	int i;
307e02cee48SSrinivas Pandruvada 
308e02cee48SSrinivas Pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
309e02cee48SSrinivas Pandruvada 	if (!report || (field_index >= report->maxfield))
310e02cee48SSrinivas Pandruvada 		goto done_proc;
311e02cee48SSrinivas Pandruvada 
312e02cee48SSrinivas Pandruvada 	field = report->field[field_index];
313e02cee48SSrinivas Pandruvada 	for (i = 0; i < field->maxusage; ++i) {
314e02cee48SSrinivas Pandruvada 		if (field->usage[i].hid == usage_id)
315e02cee48SSrinivas Pandruvada 			return field->usage[i].usage_index;
316e02cee48SSrinivas Pandruvada 	}
317e02cee48SSrinivas Pandruvada 
318e02cee48SSrinivas Pandruvada done_proc:
319e02cee48SSrinivas Pandruvada 	return -EINVAL;
320e02cee48SSrinivas Pandruvada }
321e02cee48SSrinivas Pandruvada EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index);
322e02cee48SSrinivas Pandruvada 
323401ca24fSsrinivas pandruvada int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
324401ca24fSsrinivas pandruvada 				u8 type,
325401ca24fSsrinivas pandruvada 				u32 usage_id,
326401ca24fSsrinivas pandruvada 				u32 attr_usage_id,
327401ca24fSsrinivas pandruvada 				struct hid_sensor_hub_attribute_info *info)
328401ca24fSsrinivas pandruvada {
329401ca24fSsrinivas pandruvada 	int ret = -1;
330ca2ed12fSSrinivas Pandruvada 	int i;
331401ca24fSsrinivas pandruvada 	struct hid_report *report;
332401ca24fSsrinivas pandruvada 	struct hid_field *field;
333401ca24fSsrinivas pandruvada 	struct hid_report_enum *report_enum;
334401ca24fSsrinivas pandruvada 	struct hid_device *hdev = hsdev->hdev;
335401ca24fSsrinivas pandruvada 
336401ca24fSsrinivas pandruvada 	/* Initialize with defaults */
337401ca24fSsrinivas pandruvada 	info->usage_id = usage_id;
338401ca24fSsrinivas pandruvada 	info->attrib_id = attr_usage_id;
339401ca24fSsrinivas pandruvada 	info->report_id = -1;
340401ca24fSsrinivas pandruvada 	info->index = -1;
341401ca24fSsrinivas pandruvada 	info->units = -1;
342401ca24fSsrinivas pandruvada 	info->unit_expo = -1;
343401ca24fSsrinivas pandruvada 
344401ca24fSsrinivas pandruvada 	report_enum = &hdev->report_enum[type];
345401ca24fSsrinivas pandruvada 	list_for_each_entry(report, &report_enum->report_list, list) {
346401ca24fSsrinivas pandruvada 		for (i = 0; i < report->maxfield; ++i) {
347401ca24fSsrinivas pandruvada 			field = report->field[i];
348ca2ed12fSSrinivas Pandruvada 			if (field->maxusage) {
349401ca24fSsrinivas pandruvada 				if (field->physical == usage_id &&
350ca2ed12fSSrinivas Pandruvada 					(field->logical == attr_usage_id ||
351ca2ed12fSSrinivas Pandruvada 					field->usage[0].hid ==
352ca2ed12fSSrinivas Pandruvada 							attr_usage_id) &&
353ca2ed12fSSrinivas Pandruvada 					(field->usage[0].collection_index >=
354ca2ed12fSSrinivas Pandruvada 					hsdev->start_collection_index) &&
355ca2ed12fSSrinivas Pandruvada 					(field->usage[0].collection_index <
356ca2ed12fSSrinivas Pandruvada 					hsdev->end_collection_index)) {
357ca2ed12fSSrinivas Pandruvada 
358ca2ed12fSSrinivas Pandruvada 					sensor_hub_fill_attr_info(info, i,
359ca2ed12fSSrinivas Pandruvada 								report->id,
3609f740ffaSSrinivas Pandruvada 								field);
361401ca24fSsrinivas pandruvada 					ret = 0;
362401ca24fSsrinivas pandruvada 					break;
363401ca24fSsrinivas pandruvada 				}
364401ca24fSsrinivas pandruvada 			}
365401ca24fSsrinivas pandruvada 		}
366401ca24fSsrinivas pandruvada 
367ca2ed12fSSrinivas Pandruvada 	}
368ca2ed12fSSrinivas Pandruvada 
369401ca24fSsrinivas pandruvada 	return ret;
370401ca24fSsrinivas pandruvada }
371401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
372401ca24fSsrinivas pandruvada 
373401ca24fSsrinivas pandruvada #ifdef CONFIG_PM
374401ca24fSsrinivas pandruvada static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
375401ca24fSsrinivas pandruvada {
376401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
377401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
3780ccf091dSJiri Kosina 	unsigned long flags;
379401ca24fSsrinivas pandruvada 
380401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " sensor_hub_suspend\n");
3810ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
382401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
383401ca24fSsrinivas pandruvada 		if (callback->usage_callback->suspend)
384401ca24fSsrinivas pandruvada 			callback->usage_callback->suspend(
385ca2ed12fSSrinivas Pandruvada 					callback->hsdev, callback->priv);
386401ca24fSsrinivas pandruvada 	}
3870ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
388401ca24fSsrinivas pandruvada 
389401ca24fSsrinivas pandruvada 	return 0;
390401ca24fSsrinivas pandruvada }
391401ca24fSsrinivas pandruvada 
392401ca24fSsrinivas pandruvada static int sensor_hub_resume(struct hid_device *hdev)
393401ca24fSsrinivas pandruvada {
394401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
395401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
3960ccf091dSJiri Kosina 	unsigned long flags;
397401ca24fSsrinivas pandruvada 
398401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " sensor_hub_resume\n");
3990ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
400401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
401401ca24fSsrinivas pandruvada 		if (callback->usage_callback->resume)
402401ca24fSsrinivas pandruvada 			callback->usage_callback->resume(
403ca2ed12fSSrinivas Pandruvada 					callback->hsdev, callback->priv);
404401ca24fSsrinivas pandruvada 	}
4050ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
406401ca24fSsrinivas pandruvada 
407401ca24fSsrinivas pandruvada 	return 0;
408401ca24fSsrinivas pandruvada }
409401ca24fSsrinivas pandruvada 
410401ca24fSsrinivas pandruvada static int sensor_hub_reset_resume(struct hid_device *hdev)
411401ca24fSsrinivas pandruvada {
412401ca24fSsrinivas pandruvada 	return 0;
413401ca24fSsrinivas pandruvada }
414401ca24fSsrinivas pandruvada #endif
4155902fde1SAndy Shevchenko 
416401ca24fSsrinivas pandruvada /*
417401ca24fSsrinivas pandruvada  * Handle raw report as sent by device
418401ca24fSsrinivas pandruvada  */
419401ca24fSsrinivas pandruvada static int sensor_hub_raw_event(struct hid_device *hdev,
420401ca24fSsrinivas pandruvada 		struct hid_report *report, u8 *raw_data, int size)
421401ca24fSsrinivas pandruvada {
422401ca24fSsrinivas pandruvada 	int i;
423401ca24fSsrinivas pandruvada 	u8 *ptr;
424401ca24fSsrinivas pandruvada 	int sz;
425401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
426401ca24fSsrinivas pandruvada 	unsigned long flags;
427401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks *callback = NULL;
428401ca24fSsrinivas pandruvada 	struct hid_collection *collection = NULL;
429401ca24fSsrinivas pandruvada 	void *priv = NULL;
430ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev = NULL;
431401ca24fSsrinivas pandruvada 
432401ca24fSsrinivas pandruvada 	hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
433401ca24fSsrinivas pandruvada 			 report->id, size, report->type);
434401ca24fSsrinivas pandruvada 	hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
435401ca24fSsrinivas pandruvada 	if (report->type != HID_INPUT_REPORT)
436401ca24fSsrinivas pandruvada 		return 1;
437401ca24fSsrinivas pandruvada 
438401ca24fSsrinivas pandruvada 	ptr = raw_data;
439401ca24fSsrinivas pandruvada 	ptr++; /* Skip report id */
440401ca24fSsrinivas pandruvada 
441401ca24fSsrinivas pandruvada 	spin_lock_irqsave(&pdata->lock, flags);
442401ca24fSsrinivas pandruvada 
443401ca24fSsrinivas pandruvada 	for (i = 0; i < report->maxfield; ++i) {
444401ca24fSsrinivas pandruvada 		hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
445401ca24fSsrinivas pandruvada 				i, report->field[i]->usage->collection_index,
446401ca24fSsrinivas pandruvada 				report->field[i]->usage->hid,
447d4b1bba7SSrinivas Pandruvada 				(report->field[i]->report_size *
448d4b1bba7SSrinivas Pandruvada 					report->field[i]->report_count)/8);
449d4b1bba7SSrinivas Pandruvada 		sz = (report->field[i]->report_size *
450d4b1bba7SSrinivas Pandruvada 					report->field[i]->report_count)/8;
451401ca24fSsrinivas pandruvada 		collection = &hdev->collection[
452401ca24fSsrinivas pandruvada 				report->field[i]->usage->collection_index];
453401ca24fSsrinivas pandruvada 		hid_dbg(hdev, "collection->usage %x\n",
454401ca24fSsrinivas pandruvada 					collection->usage);
455ca2ed12fSSrinivas Pandruvada 
456ca2ed12fSSrinivas Pandruvada 		callback = sensor_hub_get_callback(hdev,
457401ca24fSsrinivas pandruvada 				report->field[i]->physical,
458ca2ed12fSSrinivas Pandruvada 				report->field[i]->usage[0].collection_index,
459ca2ed12fSSrinivas Pandruvada 				&hsdev, &priv);
460e651a1daSSrinivas Pandruvada 		if (!callback) {
461e651a1daSSrinivas Pandruvada 			ptr += sz;
462e651a1daSSrinivas Pandruvada 			continue;
463e651a1daSSrinivas Pandruvada 		}
464e651a1daSSrinivas Pandruvada 		if (hsdev->pending.status && hsdev->pending.attr_usage_id ==
465e651a1daSSrinivas Pandruvada 				report->field[i]->usage->hid) {
466e651a1daSSrinivas Pandruvada 			hid_dbg(hdev, "data was pending ...\n");
467e651a1daSSrinivas Pandruvada 			hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
468e651a1daSSrinivas Pandruvada 			if (hsdev->pending.raw_data)
469e651a1daSSrinivas Pandruvada 				hsdev->pending.raw_size = sz;
470e651a1daSSrinivas Pandruvada 			else
471e651a1daSSrinivas Pandruvada 				hsdev->pending.raw_size = 0;
472e651a1daSSrinivas Pandruvada 			complete(&hsdev->pending.ready);
473e651a1daSSrinivas Pandruvada 		}
474e651a1daSSrinivas Pandruvada 		if (callback->capture_sample) {
475401ca24fSsrinivas pandruvada 			if (report->field[i]->logical)
476ca2ed12fSSrinivas Pandruvada 				callback->capture_sample(hsdev,
477401ca24fSsrinivas pandruvada 					report->field[i]->logical, sz, ptr,
478401ca24fSsrinivas pandruvada 					callback->pdev);
479401ca24fSsrinivas pandruvada 			else
480ca2ed12fSSrinivas Pandruvada 				callback->capture_sample(hsdev,
481401ca24fSsrinivas pandruvada 					report->field[i]->usage->hid, sz, ptr,
482401ca24fSsrinivas pandruvada 					callback->pdev);
483401ca24fSsrinivas pandruvada 		}
484401ca24fSsrinivas pandruvada 		ptr += sz;
485401ca24fSsrinivas pandruvada 	}
486401ca24fSsrinivas pandruvada 	if (callback && collection && callback->send_event)
487ca2ed12fSSrinivas Pandruvada 		callback->send_event(hsdev, collection->usage,
488401ca24fSsrinivas pandruvada 				callback->pdev);
489401ca24fSsrinivas pandruvada 	spin_unlock_irqrestore(&pdata->lock, flags);
490401ca24fSsrinivas pandruvada 
491401ca24fSsrinivas pandruvada 	return 1;
492401ca24fSsrinivas pandruvada }
493401ca24fSsrinivas pandruvada 
4941df3a401SSrinivas Pandruvada int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
4951df3a401SSrinivas Pandruvada {
4961df3a401SSrinivas Pandruvada 	int ret = 0;
4971df3a401SSrinivas Pandruvada 	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
4981df3a401SSrinivas Pandruvada 
4991df3a401SSrinivas Pandruvada 	mutex_lock(&data->mutex);
500ca2ed12fSSrinivas Pandruvada 	if (!data->ref_cnt) {
5011df3a401SSrinivas Pandruvada 		ret = hid_hw_open(hsdev->hdev);
5021df3a401SSrinivas Pandruvada 		if (ret) {
5031df3a401SSrinivas Pandruvada 			hid_err(hsdev->hdev, "failed to open hid device\n");
5041df3a401SSrinivas Pandruvada 			mutex_unlock(&data->mutex);
5051df3a401SSrinivas Pandruvada 			return ret;
5061df3a401SSrinivas Pandruvada 		}
5071df3a401SSrinivas Pandruvada 	}
508ca2ed12fSSrinivas Pandruvada 	data->ref_cnt++;
5091df3a401SSrinivas Pandruvada 	mutex_unlock(&data->mutex);
5101df3a401SSrinivas Pandruvada 
5111df3a401SSrinivas Pandruvada 	return ret;
5121df3a401SSrinivas Pandruvada }
5131df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_open);
5141df3a401SSrinivas Pandruvada 
5151df3a401SSrinivas Pandruvada void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
5161df3a401SSrinivas Pandruvada {
5171df3a401SSrinivas Pandruvada 	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
5181df3a401SSrinivas Pandruvada 
5191df3a401SSrinivas Pandruvada 	mutex_lock(&data->mutex);
520ca2ed12fSSrinivas Pandruvada 	data->ref_cnt--;
521ca2ed12fSSrinivas Pandruvada 	if (!data->ref_cnt)
5221df3a401SSrinivas Pandruvada 		hid_hw_close(hsdev->hdev);
5231df3a401SSrinivas Pandruvada 	mutex_unlock(&data->mutex);
5241df3a401SSrinivas Pandruvada }
5251df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_close);
5261df3a401SSrinivas Pandruvada 
527875e36f8SSrinivas Pandruvada static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
528875e36f8SSrinivas Pandruvada 		unsigned int *rsize)
529875e36f8SSrinivas Pandruvada {
530875e36f8SSrinivas Pandruvada 	int index;
531875e36f8SSrinivas Pandruvada 	struct sensor_hub_data *sd =  hid_get_drvdata(hdev);
532875e36f8SSrinivas Pandruvada 	unsigned char report_block[] = {
533875e36f8SSrinivas Pandruvada 				0x0a,  0x16, 0x03, 0x15, 0x00, 0x25, 0x05};
534875e36f8SSrinivas Pandruvada 	unsigned char power_block[] = {
535875e36f8SSrinivas Pandruvada 				0x0a,  0x19, 0x03, 0x15, 0x00, 0x25, 0x05};
536875e36f8SSrinivas Pandruvada 
537875e36f8SSrinivas Pandruvada 	if (!(sd->quirks & HID_SENSOR_HUB_ENUM_QUIRK)) {
538875e36f8SSrinivas Pandruvada 		hid_dbg(hdev, "No Enum quirks\n");
539875e36f8SSrinivas Pandruvada 		return rdesc;
540875e36f8SSrinivas Pandruvada 	}
541875e36f8SSrinivas Pandruvada 
542875e36f8SSrinivas Pandruvada 	/* Looks for power and report state usage id and force to 1 */
543875e36f8SSrinivas Pandruvada 	for (index = 0; index < *rsize; ++index) {
544875e36f8SSrinivas Pandruvada 		if (((*rsize - index) > sizeof(report_block)) &&
545875e36f8SSrinivas Pandruvada 			!memcmp(&rdesc[index], report_block,
546875e36f8SSrinivas Pandruvada 						sizeof(report_block))) {
547875e36f8SSrinivas Pandruvada 			rdesc[index + 4] = 0x01;
548875e36f8SSrinivas Pandruvada 			index += sizeof(report_block);
549875e36f8SSrinivas Pandruvada 		}
550875e36f8SSrinivas Pandruvada 		if (((*rsize - index) > sizeof(power_block)) &&
551875e36f8SSrinivas Pandruvada 			!memcmp(&rdesc[index], power_block,
552875e36f8SSrinivas Pandruvada 						sizeof(power_block))) {
553875e36f8SSrinivas Pandruvada 			rdesc[index + 4] = 0x01;
554875e36f8SSrinivas Pandruvada 			index += sizeof(power_block);
555875e36f8SSrinivas Pandruvada 		}
556875e36f8SSrinivas Pandruvada 	}
557875e36f8SSrinivas Pandruvada 
558875e36f8SSrinivas Pandruvada 	return rdesc;
559875e36f8SSrinivas Pandruvada }
560875e36f8SSrinivas Pandruvada 
561401ca24fSsrinivas pandruvada static int sensor_hub_probe(struct hid_device *hdev,
562401ca24fSsrinivas pandruvada 				const struct hid_device_id *id)
563401ca24fSsrinivas pandruvada {
564401ca24fSsrinivas pandruvada 	int ret;
565401ca24fSsrinivas pandruvada 	struct sensor_hub_data *sd;
566401ca24fSsrinivas pandruvada 	int i;
567401ca24fSsrinivas pandruvada 	char *name;
568401ca24fSsrinivas pandruvada 	int dev_cnt;
569ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev;
570ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *last_hsdev = NULL;
571*cb67126fSSrinivas Pandruvada 	struct hid_sensor_hub_device *collection_hsdev = NULL;
572401ca24fSsrinivas pandruvada 
573905cc199SAndy Shevchenko 	sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
574401ca24fSsrinivas pandruvada 	if (!sd) {
575401ca24fSsrinivas pandruvada 		hid_err(hdev, "cannot allocate Sensor data\n");
576401ca24fSsrinivas pandruvada 		return -ENOMEM;
577401ca24fSsrinivas pandruvada 	}
578ca2ed12fSSrinivas Pandruvada 
579401ca24fSsrinivas pandruvada 	hid_set_drvdata(hdev, sd);
580875e36f8SSrinivas Pandruvada 	sd->quirks = id->driver_data;
581ca2ed12fSSrinivas Pandruvada 
582401ca24fSsrinivas pandruvada 	spin_lock_init(&sd->lock);
583401ca24fSsrinivas pandruvada 	spin_lock_init(&sd->dyn_callback_lock);
584401ca24fSsrinivas pandruvada 	mutex_init(&sd->mutex);
585401ca24fSsrinivas pandruvada 	ret = hid_parse(hdev);
586401ca24fSsrinivas pandruvada 	if (ret) {
587401ca24fSsrinivas pandruvada 		hid_err(hdev, "parse failed\n");
588905cc199SAndy Shevchenko 		return ret;
589401ca24fSsrinivas pandruvada 	}
590401ca24fSsrinivas pandruvada 	INIT_LIST_HEAD(&hdev->inputs);
591401ca24fSsrinivas pandruvada 
592401ca24fSsrinivas pandruvada 	ret = hid_hw_start(hdev, 0);
593401ca24fSsrinivas pandruvada 	if (ret) {
594401ca24fSsrinivas pandruvada 		hid_err(hdev, "hw start failed\n");
595905cc199SAndy Shevchenko 		return ret;
596401ca24fSsrinivas pandruvada 	}
597401ca24fSsrinivas pandruvada 	INIT_LIST_HEAD(&sd->dyn_callback_list);
598401ca24fSsrinivas pandruvada 	sd->hid_sensor_client_cnt = 0;
599401ca24fSsrinivas pandruvada 
600ca2ed12fSSrinivas Pandruvada 	dev_cnt = sensor_hub_get_physical_device_count(hdev);
601401ca24fSsrinivas pandruvada 	if (dev_cnt > HID_MAX_PHY_DEVICES) {
602401ca24fSsrinivas pandruvada 		hid_err(hdev, "Invalid Physical device count\n");
603401ca24fSsrinivas pandruvada 		ret = -EINVAL;
6041df3a401SSrinivas Pandruvada 		goto err_stop_hw;
605401ca24fSsrinivas pandruvada 	}
6065be5db24SHimangi Saraogi 	sd->hid_sensor_hub_client_devs = devm_kzalloc(&hdev->dev, dev_cnt *
607401ca24fSsrinivas pandruvada 						      sizeof(struct mfd_cell),
608401ca24fSsrinivas pandruvada 						      GFP_KERNEL);
609401ca24fSsrinivas pandruvada 	if (sd->hid_sensor_hub_client_devs == NULL) {
610f2f13a68SAxel Lin 		hid_err(hdev, "Failed to allocate memory for mfd cells\n");
611401ca24fSsrinivas pandruvada 			ret = -ENOMEM;
6121df3a401SSrinivas Pandruvada 			goto err_stop_hw;
613401ca24fSsrinivas pandruvada 	}
614ca2ed12fSSrinivas Pandruvada 
615ca2ed12fSSrinivas Pandruvada 	for (i = 0; i < hdev->maxcollection; ++i) {
616ca2ed12fSSrinivas Pandruvada 		struct hid_collection *collection = &hdev->collection[i];
617ca2ed12fSSrinivas Pandruvada 
618*cb67126fSSrinivas Pandruvada 		if (collection->type == HID_COLLECTION_PHYSICAL ||
619*cb67126fSSrinivas Pandruvada 		    collection->type == HID_COLLECTION_APPLICATION) {
620ca2ed12fSSrinivas Pandruvada 
6215be5db24SHimangi Saraogi 			hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev),
6225be5db24SHimangi Saraogi 					     GFP_KERNEL);
623ca2ed12fSSrinivas Pandruvada 			if (!hsdev) {
624ca2ed12fSSrinivas Pandruvada 				hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
625ca2ed12fSSrinivas Pandruvada 				ret = -ENOMEM;
6265be5db24SHimangi Saraogi 				goto err_stop_hw;
627ca2ed12fSSrinivas Pandruvada 			}
628ca2ed12fSSrinivas Pandruvada 			hsdev->hdev = hdev;
629ca2ed12fSSrinivas Pandruvada 			hsdev->vendor_id = hdev->vendor;
630ca2ed12fSSrinivas Pandruvada 			hsdev->product_id = hdev->product;
631e651a1daSSrinivas Pandruvada 			hsdev->usage = collection->usage;
632e651a1daSSrinivas Pandruvada 			mutex_init(&hsdev->mutex);
633ca2ed12fSSrinivas Pandruvada 			hsdev->start_collection_index = i;
634ca2ed12fSSrinivas Pandruvada 			if (last_hsdev)
635ca2ed12fSSrinivas Pandruvada 				last_hsdev->end_collection_index = i;
636ca2ed12fSSrinivas Pandruvada 			last_hsdev = hsdev;
6375be5db24SHimangi Saraogi 			name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
6385be5db24SHimangi Saraogi 					      "HID-SENSOR-%x",
639ca2ed12fSSrinivas Pandruvada 					      collection->usage);
640401ca24fSsrinivas pandruvada 			if (name == NULL) {
641f2f13a68SAxel Lin 				hid_err(hdev, "Failed MFD device name\n");
642401ca24fSsrinivas pandruvada 					ret = -ENOMEM;
6435be5db24SHimangi Saraogi 					goto err_stop_hw;
644401ca24fSsrinivas pandruvada 			}
645401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
646401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].name = name;
647401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
648401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].platform_data =
649ca2ed12fSSrinivas Pandruvada 							hsdev;
650401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
651401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].pdata_size =
652ca2ed12fSSrinivas Pandruvada 							sizeof(*hsdev);
653ca2ed12fSSrinivas Pandruvada 			hid_dbg(hdev, "Adding %s:%d\n", name,
654ca2ed12fSSrinivas Pandruvada 					hsdev->start_collection_index);
655401ca24fSsrinivas pandruvada 			sd->hid_sensor_client_cnt++;
656*cb67126fSSrinivas Pandruvada 			if (collection_hsdev)
657*cb67126fSSrinivas Pandruvada 				collection_hsdev->end_collection_index = i;
658*cb67126fSSrinivas Pandruvada 			if (collection->type == HID_COLLECTION_APPLICATION &&
659*cb67126fSSrinivas Pandruvada 			    collection->usage == HID_USAGE_SENSOR_COLLECTION)
660*cb67126fSSrinivas Pandruvada 				collection_hsdev = hsdev;
661401ca24fSsrinivas pandruvada 		}
662401ca24fSsrinivas pandruvada 	}
663ca2ed12fSSrinivas Pandruvada 	if (last_hsdev)
664ca2ed12fSSrinivas Pandruvada 		last_hsdev->end_collection_index = i;
665*cb67126fSSrinivas Pandruvada 	if (collection_hsdev)
666*cb67126fSSrinivas Pandruvada 		collection_hsdev->end_collection_index = i;
667ca2ed12fSSrinivas Pandruvada 
66816b5fe29SJohan Hovold 	ret = mfd_add_hotplug_devices(&hdev->dev,
66916b5fe29SJohan Hovold 			sd->hid_sensor_hub_client_devs,
67016b5fe29SJohan Hovold 			sd->hid_sensor_client_cnt);
671401ca24fSsrinivas pandruvada 	if (ret < 0)
6725be5db24SHimangi Saraogi 		goto err_stop_hw;
673401ca24fSsrinivas pandruvada 
674401ca24fSsrinivas pandruvada 	return ret;
675401ca24fSsrinivas pandruvada 
676401ca24fSsrinivas pandruvada err_stop_hw:
677401ca24fSsrinivas pandruvada 	hid_hw_stop(hdev);
678401ca24fSsrinivas pandruvada 
679401ca24fSsrinivas pandruvada 	return ret;
680401ca24fSsrinivas pandruvada }
681401ca24fSsrinivas pandruvada 
682401ca24fSsrinivas pandruvada static void sensor_hub_remove(struct hid_device *hdev)
683401ca24fSsrinivas pandruvada {
684401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hdev);
685401ca24fSsrinivas pandruvada 	unsigned long flags;
686e651a1daSSrinivas Pandruvada 	int i;
687401ca24fSsrinivas pandruvada 
688401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " hardware removed\n");
689401ca24fSsrinivas pandruvada 	hid_hw_close(hdev);
690f2f13a68SAxel Lin 	hid_hw_stop(hdev);
691401ca24fSsrinivas pandruvada 	spin_lock_irqsave(&data->lock, flags);
692e651a1daSSrinivas Pandruvada 	for (i = 0; i < data->hid_sensor_client_cnt; ++i) {
693e651a1daSSrinivas Pandruvada 		struct hid_sensor_hub_device *hsdev =
694e651a1daSSrinivas Pandruvada 			data->hid_sensor_hub_client_devs[i].platform_data;
695e651a1daSSrinivas Pandruvada 		if (hsdev->pending.status)
696e651a1daSSrinivas Pandruvada 			complete(&hsdev->pending.ready);
697e651a1daSSrinivas Pandruvada 	}
698401ca24fSsrinivas pandruvada 	spin_unlock_irqrestore(&data->lock, flags);
699401ca24fSsrinivas pandruvada 	mfd_remove_devices(&hdev->dev);
700401ca24fSsrinivas pandruvada 	hid_set_drvdata(hdev, NULL);
701401ca24fSsrinivas pandruvada 	mutex_destroy(&data->mutex);
702401ca24fSsrinivas pandruvada }
703401ca24fSsrinivas pandruvada 
704401ca24fSsrinivas pandruvada static const struct hid_device_id sensor_hub_devices[] = {
705875e36f8SSrinivas Pandruvada 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_0,
70630f58877SStephen Chandler Paul 			USB_DEVICE_ID_INTEL_HID_SENSOR_0),
707875e36f8SSrinivas Pandruvada 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
708875e36f8SSrinivas Pandruvada 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
70930f58877SStephen Chandler Paul 			USB_DEVICE_ID_INTEL_HID_SENSOR_0),
71030f58877SStephen Chandler Paul 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
71130f58877SStephen Chandler Paul 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
71230f58877SStephen Chandler Paul 			USB_DEVICE_ID_INTEL_HID_SENSOR_1),
713875e36f8SSrinivas Pandruvada 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
71400478ee8SReyad Attiyat 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
71500478ee8SReyad Attiyat 			USB_DEVICE_ID_MS_SURFACE_PRO_2),
71600478ee8SReyad Attiyat 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
71700478ee8SReyad Attiyat 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
71800478ee8SReyad Attiyat 			USB_DEVICE_ID_MS_TOUCH_COVER_2),
71900478ee8SReyad Attiyat 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
72000478ee8SReyad Attiyat 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
72100478ee8SReyad Attiyat 			USB_DEVICE_ID_MS_TYPE_COVER_2),
72200478ee8SReyad Attiyat 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
723218eb9edSArchana Patni 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
724467669c5SSrinivas Pandruvada 			USB_DEVICE_ID_STM_HID_SENSOR),
725467669c5SSrinivas Pandruvada 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
726467669c5SSrinivas Pandruvada 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
727dde3b45cSArchana Patni 			USB_DEVICE_ID_STM_HID_SENSOR_1),
728218eb9edSArchana Patni 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
729825747bbSPeter F. Patel-Schneider 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_TEXAS_INSTRUMENTS,
730825747bbSPeter F. Patel-Schneider 			USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA),
731825747bbSPeter F. Patel-Schneider 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
7320d1e4b02SMika Westerberg 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
7330d1e4b02SMika Westerberg 		     HID_ANY_ID) },
734401ca24fSsrinivas pandruvada 	{ }
735401ca24fSsrinivas pandruvada };
736401ca24fSsrinivas pandruvada MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
737401ca24fSsrinivas pandruvada 
738401ca24fSsrinivas pandruvada static struct hid_driver sensor_hub_driver = {
739401ca24fSsrinivas pandruvada 	.name = "hid-sensor-hub",
740401ca24fSsrinivas pandruvada 	.id_table = sensor_hub_devices,
741401ca24fSsrinivas pandruvada 	.probe = sensor_hub_probe,
742401ca24fSsrinivas pandruvada 	.remove = sensor_hub_remove,
743401ca24fSsrinivas pandruvada 	.raw_event = sensor_hub_raw_event,
744875e36f8SSrinivas Pandruvada 	.report_fixup = sensor_hub_report_fixup,
745401ca24fSsrinivas pandruvada #ifdef CONFIG_PM
746401ca24fSsrinivas pandruvada 	.suspend = sensor_hub_suspend,
747401ca24fSsrinivas pandruvada 	.resume = sensor_hub_resume,
748401ca24fSsrinivas pandruvada 	.reset_resume = sensor_hub_reset_resume,
749401ca24fSsrinivas pandruvada #endif
750401ca24fSsrinivas pandruvada };
751f425458eSH Hartley Sweeten module_hid_driver(sensor_hub_driver);
752401ca24fSsrinivas pandruvada 
753401ca24fSsrinivas pandruvada MODULE_DESCRIPTION("HID Sensor Hub driver");
754401ca24fSsrinivas pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
755401ca24fSsrinivas pandruvada MODULE_LICENSE("GPL");
756