xref: /linux/drivers/hid/hid-sensor-hub.c (revision 143fca77cce906d35f7a60ccef648e888df589f2)
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  */
19930fafd9SSrinivas Pandruvada 
20401ca24fSsrinivas pandruvada #include <linux/device.h>
21401ca24fSsrinivas pandruvada #include <linux/hid.h>
22401ca24fSsrinivas pandruvada #include <linux/module.h>
23401ca24fSsrinivas pandruvada #include <linux/slab.h>
24401ca24fSsrinivas pandruvada #include <linux/mfd/core.h>
25401ca24fSsrinivas pandruvada #include <linux/list.h>
26401ca24fSsrinivas pandruvada #include <linux/hid-sensor-ids.h>
27401ca24fSsrinivas pandruvada #include <linux/hid-sensor-hub.h>
28401ca24fSsrinivas pandruvada #include "hid-ids.h"
29401ca24fSsrinivas pandruvada 
30875e36f8SSrinivas Pandruvada #define HID_SENSOR_HUB_ENUM_QUIRK	0x01
31875e36f8SSrinivas Pandruvada 
32401ca24fSsrinivas pandruvada /**
33401ca24fSsrinivas pandruvada  * struct sensor_hub_data - Hold a instance data for a HID hub device
34401ca24fSsrinivas pandruvada  * @hsdev:		Stored hid instance for current hub device.
35401ca24fSsrinivas pandruvada  * @mutex:		Mutex to serialize synchronous request.
36401ca24fSsrinivas pandruvada  * @lock:		Spin lock to protect pending request structure.
37401ca24fSsrinivas pandruvada  * @dyn_callback_list:	Holds callback function
38401ca24fSsrinivas pandruvada  * @dyn_callback_lock:	spin lock to protect callback list
39401ca24fSsrinivas pandruvada  * @hid_sensor_hub_client_devs:	Stores all MFD cells for a hub instance.
40401ca24fSsrinivas pandruvada  * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
41ca2ed12fSSrinivas Pandruvada  * @ref_cnt:		Number of MFD clients have opened this device
42401ca24fSsrinivas pandruvada  */
43401ca24fSsrinivas pandruvada struct sensor_hub_data {
44401ca24fSsrinivas pandruvada 	struct mutex mutex;
45401ca24fSsrinivas pandruvada 	spinlock_t lock;
46401ca24fSsrinivas pandruvada 	struct list_head dyn_callback_list;
47401ca24fSsrinivas pandruvada 	spinlock_t dyn_callback_lock;
48401ca24fSsrinivas pandruvada 	struct mfd_cell *hid_sensor_hub_client_devs;
49401ca24fSsrinivas pandruvada 	int hid_sensor_client_cnt;
50875e36f8SSrinivas Pandruvada 	unsigned long quirks;
51ca2ed12fSSrinivas Pandruvada 	int ref_cnt;
52401ca24fSsrinivas pandruvada };
53401ca24fSsrinivas pandruvada 
54401ca24fSsrinivas pandruvada /**
55401ca24fSsrinivas pandruvada  * struct hid_sensor_hub_callbacks_list - Stores callback list
56401ca24fSsrinivas pandruvada  * @list:		list head.
57401ca24fSsrinivas pandruvada  * @usage_id:		usage id for a physical device.
58401ca24fSsrinivas pandruvada  * @usage_callback:	Stores registered callback functions.
59401ca24fSsrinivas pandruvada  * @priv:		Private data for a physical device.
60401ca24fSsrinivas pandruvada  */
61401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks_list {
62401ca24fSsrinivas pandruvada 	struct list_head list;
63401ca24fSsrinivas pandruvada 	u32 usage_id;
64ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev;
65401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks *usage_callback;
66401ca24fSsrinivas pandruvada 	void *priv;
67401ca24fSsrinivas pandruvada };
68401ca24fSsrinivas pandruvada 
69401ca24fSsrinivas pandruvada static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
70401ca24fSsrinivas pandruvada 						int dir)
71401ca24fSsrinivas pandruvada {
72401ca24fSsrinivas pandruvada 	struct hid_report *report;
73401ca24fSsrinivas pandruvada 
74401ca24fSsrinivas pandruvada 	list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
75401ca24fSsrinivas pandruvada 		if (report->id == id)
76401ca24fSsrinivas pandruvada 			return report;
77401ca24fSsrinivas pandruvada 	}
78401ca24fSsrinivas pandruvada 	hid_warn(hdev, "No report with id 0x%x found\n", id);
79401ca24fSsrinivas pandruvada 
80401ca24fSsrinivas pandruvada 	return NULL;
81401ca24fSsrinivas pandruvada }
82401ca24fSsrinivas pandruvada 
83ca2ed12fSSrinivas Pandruvada static int sensor_hub_get_physical_device_count(struct hid_device *hdev)
84401ca24fSsrinivas pandruvada {
85ca2ed12fSSrinivas Pandruvada 	int i;
86ca2ed12fSSrinivas Pandruvada 	int count = 0;
87401ca24fSsrinivas pandruvada 
88ca2ed12fSSrinivas Pandruvada 	for (i = 0; i < hdev->maxcollection; ++i) {
89ca2ed12fSSrinivas Pandruvada 		struct hid_collection *collection = &hdev->collection[i];
90cb67126fSSrinivas Pandruvada 		if (collection->type == HID_COLLECTION_PHYSICAL ||
91cb67126fSSrinivas Pandruvada 		    collection->type == HID_COLLECTION_APPLICATION)
92ca2ed12fSSrinivas Pandruvada 			++count;
93401ca24fSsrinivas pandruvada 	}
94401ca24fSsrinivas pandruvada 
95ca2ed12fSSrinivas Pandruvada 	return count;
96401ca24fSsrinivas pandruvada }
97401ca24fSsrinivas pandruvada 
98401ca24fSsrinivas pandruvada static void sensor_hub_fill_attr_info(
99401ca24fSsrinivas pandruvada 		struct hid_sensor_hub_attribute_info *info,
1009f740ffaSSrinivas Pandruvada 		s32 index, s32 report_id, struct hid_field *field)
101401ca24fSsrinivas pandruvada {
102401ca24fSsrinivas pandruvada 	info->index = index;
103401ca24fSsrinivas pandruvada 	info->report_id = report_id;
1049f740ffaSSrinivas Pandruvada 	info->units = field->unit;
1059f740ffaSSrinivas Pandruvada 	info->unit_expo = field->unit_exponent;
1069f740ffaSSrinivas Pandruvada 	info->size = (field->report_size * field->report_count)/8;
1079f740ffaSSrinivas Pandruvada 	info->logical_minimum = field->logical_minimum;
1089f740ffaSSrinivas Pandruvada 	info->logical_maximum = field->logical_maximum;
109401ca24fSsrinivas pandruvada }
110401ca24fSsrinivas pandruvada 
111401ca24fSsrinivas pandruvada static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
112401ca24fSsrinivas pandruvada 					struct hid_device *hdev,
113ca2ed12fSSrinivas Pandruvada 					u32 usage_id,
114ca2ed12fSSrinivas Pandruvada 					int collection_index,
115ca2ed12fSSrinivas Pandruvada 					struct hid_sensor_hub_device **hsdev,
116ca2ed12fSSrinivas Pandruvada 					void **priv)
117401ca24fSsrinivas pandruvada {
118401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
119401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
120ed119770SSrinivas Pandruvada 	unsigned long flags;
121401ca24fSsrinivas pandruvada 
122ed119770SSrinivas Pandruvada 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
123401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
124cb67126fSSrinivas Pandruvada 		if ((callback->usage_id == usage_id ||
125cb67126fSSrinivas Pandruvada 		     callback->usage_id == HID_USAGE_SENSOR_COLLECTION) &&
126ca2ed12fSSrinivas Pandruvada 			(collection_index >=
127ca2ed12fSSrinivas Pandruvada 				callback->hsdev->start_collection_index) &&
128ca2ed12fSSrinivas Pandruvada 			(collection_index <
129ca2ed12fSSrinivas Pandruvada 				callback->hsdev->end_collection_index)) {
130401ca24fSsrinivas pandruvada 			*priv = callback->priv;
131ca2ed12fSSrinivas Pandruvada 			*hsdev = callback->hsdev;
132ed119770SSrinivas Pandruvada 			spin_unlock_irqrestore(&pdata->dyn_callback_lock,
133ed119770SSrinivas Pandruvada 					       flags);
134401ca24fSsrinivas pandruvada 			return callback->usage_callback;
135401ca24fSsrinivas pandruvada 		}
136ed119770SSrinivas Pandruvada 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
137401ca24fSsrinivas pandruvada 
138401ca24fSsrinivas pandruvada 	return NULL;
139401ca24fSsrinivas pandruvada }
140401ca24fSsrinivas pandruvada 
141401ca24fSsrinivas pandruvada int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
142401ca24fSsrinivas pandruvada 			u32 usage_id,
143401ca24fSsrinivas pandruvada 			struct hid_sensor_hub_callbacks *usage_callback)
144401ca24fSsrinivas pandruvada {
145401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
146401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
1470ccf091dSJiri Kosina 	unsigned long flags;
148401ca24fSsrinivas pandruvada 
1490ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
150401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
151ca2ed12fSSrinivas Pandruvada 		if (callback->usage_id == usage_id &&
152ca2ed12fSSrinivas Pandruvada 						callback->hsdev == hsdev) {
1530ccf091dSJiri Kosina 			spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
154401ca24fSsrinivas pandruvada 			return -EINVAL;
155401ca24fSsrinivas pandruvada 		}
1562b7c4b8eSDan Carpenter 	callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
157401ca24fSsrinivas pandruvada 	if (!callback) {
1580ccf091dSJiri Kosina 		spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
159401ca24fSsrinivas pandruvada 		return -ENOMEM;
160401ca24fSsrinivas pandruvada 	}
161ca2ed12fSSrinivas Pandruvada 	callback->hsdev = hsdev;
162401ca24fSsrinivas pandruvada 	callback->usage_callback = usage_callback;
163401ca24fSsrinivas pandruvada 	callback->usage_id = usage_id;
164401ca24fSsrinivas pandruvada 	callback->priv = NULL;
165cb67126fSSrinivas Pandruvada 	/*
166cb67126fSSrinivas Pandruvada 	 * If there is a handler registered for the collection type, then
167cb67126fSSrinivas Pandruvada 	 * it will handle all reports for sensors in this collection. If
168cb67126fSSrinivas Pandruvada 	 * there is also an individual sensor handler registration, then
169cb67126fSSrinivas Pandruvada 	 * we want to make sure that the reports are directed to collection
170cb67126fSSrinivas Pandruvada 	 * handler, as this may be a fusion sensor. So add collection handlers
171cb67126fSSrinivas Pandruvada 	 * to the beginning of the list, so that they are matched first.
172cb67126fSSrinivas Pandruvada 	 */
173cb67126fSSrinivas Pandruvada 	if (usage_id == HID_USAGE_SENSOR_COLLECTION)
174cb67126fSSrinivas Pandruvada 		list_add(&callback->list, &pdata->dyn_callback_list);
175cb67126fSSrinivas Pandruvada 	else
176401ca24fSsrinivas pandruvada 		list_add_tail(&callback->list, &pdata->dyn_callback_list);
1770ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
178401ca24fSsrinivas pandruvada 
179401ca24fSsrinivas pandruvada 	return 0;
180401ca24fSsrinivas pandruvada }
181401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
182401ca24fSsrinivas pandruvada 
183401ca24fSsrinivas pandruvada int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
184401ca24fSsrinivas pandruvada 				u32 usage_id)
185401ca24fSsrinivas pandruvada {
186401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
187401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
1880ccf091dSJiri Kosina 	unsigned long flags;
189401ca24fSsrinivas pandruvada 
1900ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
191401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
192ca2ed12fSSrinivas Pandruvada 		if (callback->usage_id == usage_id &&
193ca2ed12fSSrinivas Pandruvada 						callback->hsdev == hsdev) {
194401ca24fSsrinivas pandruvada 			list_del(&callback->list);
195401ca24fSsrinivas pandruvada 			kfree(callback);
196401ca24fSsrinivas pandruvada 			break;
197401ca24fSsrinivas pandruvada 		}
1980ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
199401ca24fSsrinivas pandruvada 
200401ca24fSsrinivas pandruvada 	return 0;
201401ca24fSsrinivas pandruvada }
202401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
203401ca24fSsrinivas pandruvada 
204401ca24fSsrinivas pandruvada int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
2053950e033SSrinivas Pandruvada 			   u32 field_index, int buffer_size, void *buffer)
206401ca24fSsrinivas pandruvada {
207401ca24fSsrinivas pandruvada 	struct hid_report *report;
208401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
2093950e033SSrinivas Pandruvada 	__s32 *buf32 = buffer;
2103950e033SSrinivas Pandruvada 	int i = 0;
2113950e033SSrinivas Pandruvada 	int remaining_bytes;
2123950e033SSrinivas Pandruvada 	__s32 value;
213401ca24fSsrinivas pandruvada 	int ret = 0;
214401ca24fSsrinivas pandruvada 
215401ca24fSsrinivas pandruvada 	mutex_lock(&data->mutex);
216401ca24fSsrinivas pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
217401ca24fSsrinivas pandruvada 	if (!report || (field_index >= report->maxfield)) {
218401ca24fSsrinivas pandruvada 		ret = -EINVAL;
219401ca24fSsrinivas pandruvada 		goto done_proc;
220401ca24fSsrinivas pandruvada 	}
2213950e033SSrinivas Pandruvada 
2228d43b49eSNicolas Pitre 	remaining_bytes = buffer_size % sizeof(__s32);
2238d43b49eSNicolas Pitre 	buffer_size = buffer_size / sizeof(__s32);
2243950e033SSrinivas Pandruvada 	if (buffer_size) {
2253950e033SSrinivas Pandruvada 		for (i = 0; i < buffer_size; ++i) {
2263950e033SSrinivas Pandruvada 			hid_set_field(report->field[field_index], i,
22762fad137SSrinivas Pandruvada 				      (__force __s32)cpu_to_le32(*buf32));
2283950e033SSrinivas Pandruvada 			++buf32;
2293950e033SSrinivas Pandruvada 		}
2303950e033SSrinivas Pandruvada 	}
2313950e033SSrinivas Pandruvada 	if (remaining_bytes) {
2323950e033SSrinivas Pandruvada 		value = 0;
2333950e033SSrinivas Pandruvada 		memcpy(&value, (u8 *)buf32, remaining_bytes);
2343950e033SSrinivas Pandruvada 		hid_set_field(report->field[field_index], i,
23562fad137SSrinivas Pandruvada 			      (__force __s32)cpu_to_le32(value));
2363950e033SSrinivas Pandruvada 	}
237d8814272SBenjamin Tissoires 	hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
238b7966a4dSBenjamin Tissoires 	hid_hw_wait(hsdev->hdev);
239401ca24fSsrinivas pandruvada 
240401ca24fSsrinivas pandruvada done_proc:
241401ca24fSsrinivas pandruvada 	mutex_unlock(&data->mutex);
242401ca24fSsrinivas pandruvada 
243401ca24fSsrinivas pandruvada 	return ret;
244401ca24fSsrinivas pandruvada }
245401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
246401ca24fSsrinivas pandruvada 
247401ca24fSsrinivas pandruvada int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
2486adc83fcSSrinivas Pandruvada 			   u32 field_index, int buffer_size, void *buffer)
249401ca24fSsrinivas pandruvada {
250401ca24fSsrinivas pandruvada 	struct hid_report *report;
251401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
2526adc83fcSSrinivas Pandruvada 	int report_size;
253401ca24fSsrinivas pandruvada 	int ret = 0;
2545459ada2SSrinivas Pandruvada 	u8 *val_ptr;
2555459ada2SSrinivas Pandruvada 	int buffer_index = 0;
2565459ada2SSrinivas Pandruvada 	int i;
257401ca24fSsrinivas pandruvada 
258*143fca77SSrinivas Pandruvada 	memset(buffer, 0, buffer_size);
259*143fca77SSrinivas Pandruvada 
260401ca24fSsrinivas pandruvada 	mutex_lock(&data->mutex);
261401ca24fSsrinivas pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
2629e891025SKees Cook 	if (!report || (field_index >= report->maxfield) ||
2639e891025SKees Cook 	    report->field[field_index]->report_count < 1) {
264401ca24fSsrinivas pandruvada 		ret = -EINVAL;
265401ca24fSsrinivas pandruvada 		goto done_proc;
266401ca24fSsrinivas pandruvada 	}
267d8814272SBenjamin Tissoires 	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
268b7966a4dSBenjamin Tissoires 	hid_hw_wait(hsdev->hdev);
2696adc83fcSSrinivas Pandruvada 
2706adc83fcSSrinivas Pandruvada 	/* calculate number of bytes required to read this field */
2716adc83fcSSrinivas Pandruvada 	report_size = DIV_ROUND_UP(report->field[field_index]->report_size,
2726adc83fcSSrinivas Pandruvada 				   8) *
2736adc83fcSSrinivas Pandruvada 				   report->field[field_index]->report_count;
2746adc83fcSSrinivas Pandruvada 	if (!report_size) {
2756adc83fcSSrinivas Pandruvada 		ret = -EINVAL;
2766adc83fcSSrinivas Pandruvada 		goto done_proc;
2776adc83fcSSrinivas Pandruvada 	}
2786adc83fcSSrinivas Pandruvada 	ret = min(report_size, buffer_size);
2795459ada2SSrinivas Pandruvada 
2805459ada2SSrinivas Pandruvada 	val_ptr = (u8 *)report->field[field_index]->value;
2815459ada2SSrinivas Pandruvada 	for (i = 0; i < report->field[field_index]->report_count; ++i) {
2825459ada2SSrinivas Pandruvada 		if (buffer_index >= ret)
2835459ada2SSrinivas Pandruvada 			break;
2845459ada2SSrinivas Pandruvada 
2855459ada2SSrinivas Pandruvada 		memcpy(&((u8 *)buffer)[buffer_index], val_ptr,
2865459ada2SSrinivas Pandruvada 		       report->field[field_index]->report_size / 8);
2875459ada2SSrinivas Pandruvada 		val_ptr += sizeof(__s32);
2885459ada2SSrinivas Pandruvada 		buffer_index += (report->field[field_index]->report_size / 8);
2895459ada2SSrinivas Pandruvada 	}
290401ca24fSsrinivas pandruvada 
291401ca24fSsrinivas pandruvada done_proc:
292401ca24fSsrinivas pandruvada 	mutex_unlock(&data->mutex);
293401ca24fSsrinivas pandruvada 
294401ca24fSsrinivas pandruvada 	return ret;
295401ca24fSsrinivas pandruvada }
296401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
297401ca24fSsrinivas pandruvada 
298401ca24fSsrinivas pandruvada 
299401ca24fSsrinivas pandruvada int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
300401ca24fSsrinivas pandruvada 					u32 usage_id,
301b3f4737dSSrinivas Pandruvada 					u32 attr_usage_id, u32 report_id,
302b3f4737dSSrinivas Pandruvada 					enum sensor_hub_read_flags flag)
303401ca24fSsrinivas pandruvada {
304401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
305401ca24fSsrinivas pandruvada 	unsigned long flags;
306401ca24fSsrinivas pandruvada 	struct hid_report *report;
307401ca24fSsrinivas pandruvada 	int ret_val = 0;
308401ca24fSsrinivas pandruvada 
309b3f4737dSSrinivas Pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev,
310b3f4737dSSrinivas Pandruvada 				   HID_INPUT_REPORT);
311b3f4737dSSrinivas Pandruvada 	if (!report)
312b3f4737dSSrinivas Pandruvada 		return -EINVAL;
313b3f4737dSSrinivas Pandruvada 
3142d94e522SSrinivas Pandruvada 	mutex_lock(hsdev->mutex_ptr);
315b3f4737dSSrinivas Pandruvada 	if (flag == SENSOR_HUB_SYNC) {
316e651a1daSSrinivas Pandruvada 		memset(&hsdev->pending, 0, sizeof(hsdev->pending));
317e651a1daSSrinivas Pandruvada 		init_completion(&hsdev->pending.ready);
318e651a1daSSrinivas Pandruvada 		hsdev->pending.usage_id = usage_id;
319e651a1daSSrinivas Pandruvada 		hsdev->pending.attr_usage_id = attr_usage_id;
320e651a1daSSrinivas Pandruvada 		hsdev->pending.raw_size = 0;
321401ca24fSsrinivas pandruvada 
322401ca24fSsrinivas pandruvada 		spin_lock_irqsave(&data->lock, flags);
323e651a1daSSrinivas Pandruvada 		hsdev->pending.status = true;
324f74346a0SSrinivas Pandruvada 		spin_unlock_irqrestore(&data->lock, flags);
325b3f4737dSSrinivas Pandruvada 	}
326e651a1daSSrinivas Pandruvada 	mutex_lock(&data->mutex);
327d8814272SBenjamin Tissoires 	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
328e651a1daSSrinivas Pandruvada 	mutex_unlock(&data->mutex);
329b3f4737dSSrinivas Pandruvada 	if (flag == SENSOR_HUB_SYNC) {
330b3f4737dSSrinivas Pandruvada 		wait_for_completion_interruptible_timeout(
331b3f4737dSSrinivas Pandruvada 						&hsdev->pending.ready, HZ*5);
332e651a1daSSrinivas Pandruvada 		switch (hsdev->pending.raw_size) {
333401ca24fSsrinivas pandruvada 		case 1:
334e651a1daSSrinivas Pandruvada 			ret_val = *(u8 *)hsdev->pending.raw_data;
335401ca24fSsrinivas pandruvada 			break;
336401ca24fSsrinivas pandruvada 		case 2:
337e651a1daSSrinivas Pandruvada 			ret_val = *(u16 *)hsdev->pending.raw_data;
338401ca24fSsrinivas pandruvada 			break;
339401ca24fSsrinivas pandruvada 		case 4:
340e651a1daSSrinivas Pandruvada 			ret_val = *(u32 *)hsdev->pending.raw_data;
341401ca24fSsrinivas pandruvada 			break;
342401ca24fSsrinivas pandruvada 		default:
343401ca24fSsrinivas pandruvada 			ret_val = 0;
344401ca24fSsrinivas pandruvada 		}
345e651a1daSSrinivas Pandruvada 		kfree(hsdev->pending.raw_data);
346e651a1daSSrinivas Pandruvada 		hsdev->pending.status = false;
347b3f4737dSSrinivas Pandruvada 	}
3482d94e522SSrinivas Pandruvada 	mutex_unlock(hsdev->mutex_ptr);
349401ca24fSsrinivas pandruvada 
350401ca24fSsrinivas pandruvada 	return ret_val;
351401ca24fSsrinivas pandruvada }
352401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
353401ca24fSsrinivas pandruvada 
354e02cee48SSrinivas Pandruvada int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
355e02cee48SSrinivas Pandruvada 				u32 report_id, int field_index, u32 usage_id)
356e02cee48SSrinivas Pandruvada {
357e02cee48SSrinivas Pandruvada 	struct hid_report *report;
358e02cee48SSrinivas Pandruvada 	struct hid_field *field;
359e02cee48SSrinivas Pandruvada 	int i;
360e02cee48SSrinivas Pandruvada 
361e02cee48SSrinivas Pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
362e02cee48SSrinivas Pandruvada 	if (!report || (field_index >= report->maxfield))
363e02cee48SSrinivas Pandruvada 		goto done_proc;
364e02cee48SSrinivas Pandruvada 
365e02cee48SSrinivas Pandruvada 	field = report->field[field_index];
366e02cee48SSrinivas Pandruvada 	for (i = 0; i < field->maxusage; ++i) {
367e02cee48SSrinivas Pandruvada 		if (field->usage[i].hid == usage_id)
368e02cee48SSrinivas Pandruvada 			return field->usage[i].usage_index;
369e02cee48SSrinivas Pandruvada 	}
370e02cee48SSrinivas Pandruvada 
371e02cee48SSrinivas Pandruvada done_proc:
372e02cee48SSrinivas Pandruvada 	return -EINVAL;
373e02cee48SSrinivas Pandruvada }
374e02cee48SSrinivas Pandruvada EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index);
375e02cee48SSrinivas Pandruvada 
376401ca24fSsrinivas pandruvada int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
377401ca24fSsrinivas pandruvada 				u8 type,
378401ca24fSsrinivas pandruvada 				u32 usage_id,
379401ca24fSsrinivas pandruvada 				u32 attr_usage_id,
380401ca24fSsrinivas pandruvada 				struct hid_sensor_hub_attribute_info *info)
381401ca24fSsrinivas pandruvada {
382401ca24fSsrinivas pandruvada 	int ret = -1;
383ca2ed12fSSrinivas Pandruvada 	int i;
384401ca24fSsrinivas pandruvada 	struct hid_report *report;
385401ca24fSsrinivas pandruvada 	struct hid_field *field;
386401ca24fSsrinivas pandruvada 	struct hid_report_enum *report_enum;
387401ca24fSsrinivas pandruvada 	struct hid_device *hdev = hsdev->hdev;
388401ca24fSsrinivas pandruvada 
389401ca24fSsrinivas pandruvada 	/* Initialize with defaults */
390401ca24fSsrinivas pandruvada 	info->usage_id = usage_id;
391401ca24fSsrinivas pandruvada 	info->attrib_id = attr_usage_id;
392401ca24fSsrinivas pandruvada 	info->report_id = -1;
393401ca24fSsrinivas pandruvada 	info->index = -1;
394401ca24fSsrinivas pandruvada 	info->units = -1;
395401ca24fSsrinivas pandruvada 	info->unit_expo = -1;
396401ca24fSsrinivas pandruvada 
397401ca24fSsrinivas pandruvada 	report_enum = &hdev->report_enum[type];
398401ca24fSsrinivas pandruvada 	list_for_each_entry(report, &report_enum->report_list, list) {
399401ca24fSsrinivas pandruvada 		for (i = 0; i < report->maxfield; ++i) {
400401ca24fSsrinivas pandruvada 			field = report->field[i];
401ca2ed12fSSrinivas Pandruvada 			if (field->maxusage) {
402401ca24fSsrinivas pandruvada 				if (field->physical == usage_id &&
403ca2ed12fSSrinivas Pandruvada 					(field->logical == attr_usage_id ||
404ca2ed12fSSrinivas Pandruvada 					field->usage[0].hid ==
405ca2ed12fSSrinivas Pandruvada 							attr_usage_id) &&
406ca2ed12fSSrinivas Pandruvada 					(field->usage[0].collection_index >=
407ca2ed12fSSrinivas Pandruvada 					hsdev->start_collection_index) &&
408ca2ed12fSSrinivas Pandruvada 					(field->usage[0].collection_index <
409ca2ed12fSSrinivas Pandruvada 					hsdev->end_collection_index)) {
410ca2ed12fSSrinivas Pandruvada 
411ca2ed12fSSrinivas Pandruvada 					sensor_hub_fill_attr_info(info, i,
412ca2ed12fSSrinivas Pandruvada 								report->id,
4139f740ffaSSrinivas Pandruvada 								field);
414401ca24fSsrinivas pandruvada 					ret = 0;
415401ca24fSsrinivas pandruvada 					break;
416401ca24fSsrinivas pandruvada 				}
417401ca24fSsrinivas pandruvada 			}
418401ca24fSsrinivas pandruvada 		}
419401ca24fSsrinivas pandruvada 
420ca2ed12fSSrinivas Pandruvada 	}
421ca2ed12fSSrinivas Pandruvada 
422401ca24fSsrinivas pandruvada 	return ret;
423401ca24fSsrinivas pandruvada }
424401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
425401ca24fSsrinivas pandruvada 
426401ca24fSsrinivas pandruvada #ifdef CONFIG_PM
427401ca24fSsrinivas pandruvada static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
428401ca24fSsrinivas pandruvada {
429401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
430401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
4310ccf091dSJiri Kosina 	unsigned long flags;
432401ca24fSsrinivas pandruvada 
433401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " sensor_hub_suspend\n");
4340ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
435401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
436401ca24fSsrinivas pandruvada 		if (callback->usage_callback->suspend)
437401ca24fSsrinivas pandruvada 			callback->usage_callback->suspend(
438ca2ed12fSSrinivas Pandruvada 					callback->hsdev, callback->priv);
439401ca24fSsrinivas pandruvada 	}
4400ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
441401ca24fSsrinivas pandruvada 
442401ca24fSsrinivas pandruvada 	return 0;
443401ca24fSsrinivas pandruvada }
444401ca24fSsrinivas pandruvada 
445401ca24fSsrinivas pandruvada static int sensor_hub_resume(struct hid_device *hdev)
446401ca24fSsrinivas pandruvada {
447401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
448401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
4490ccf091dSJiri Kosina 	unsigned long flags;
450401ca24fSsrinivas pandruvada 
451401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " sensor_hub_resume\n");
4520ccf091dSJiri Kosina 	spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
453401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
454401ca24fSsrinivas pandruvada 		if (callback->usage_callback->resume)
455401ca24fSsrinivas pandruvada 			callback->usage_callback->resume(
456ca2ed12fSSrinivas Pandruvada 					callback->hsdev, callback->priv);
457401ca24fSsrinivas pandruvada 	}
4580ccf091dSJiri Kosina 	spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
459401ca24fSsrinivas pandruvada 
460401ca24fSsrinivas pandruvada 	return 0;
461401ca24fSsrinivas pandruvada }
462401ca24fSsrinivas pandruvada 
463401ca24fSsrinivas pandruvada static int sensor_hub_reset_resume(struct hid_device *hdev)
464401ca24fSsrinivas pandruvada {
465401ca24fSsrinivas pandruvada 	return 0;
466401ca24fSsrinivas pandruvada }
467401ca24fSsrinivas pandruvada #endif
4685902fde1SAndy Shevchenko 
469401ca24fSsrinivas pandruvada /*
470401ca24fSsrinivas pandruvada  * Handle raw report as sent by device
471401ca24fSsrinivas pandruvada  */
472401ca24fSsrinivas pandruvada static int sensor_hub_raw_event(struct hid_device *hdev,
473401ca24fSsrinivas pandruvada 		struct hid_report *report, u8 *raw_data, int size)
474401ca24fSsrinivas pandruvada {
475401ca24fSsrinivas pandruvada 	int i;
476401ca24fSsrinivas pandruvada 	u8 *ptr;
477401ca24fSsrinivas pandruvada 	int sz;
478401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
479401ca24fSsrinivas pandruvada 	unsigned long flags;
480401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks *callback = NULL;
481401ca24fSsrinivas pandruvada 	struct hid_collection *collection = NULL;
482401ca24fSsrinivas pandruvada 	void *priv = NULL;
483ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev = NULL;
484401ca24fSsrinivas pandruvada 
485401ca24fSsrinivas pandruvada 	hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
486401ca24fSsrinivas pandruvada 			 report->id, size, report->type);
487401ca24fSsrinivas pandruvada 	hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
488401ca24fSsrinivas pandruvada 	if (report->type != HID_INPUT_REPORT)
489401ca24fSsrinivas pandruvada 		return 1;
490401ca24fSsrinivas pandruvada 
491401ca24fSsrinivas pandruvada 	ptr = raw_data;
492401ca24fSsrinivas pandruvada 	ptr++; /* Skip report id */
493401ca24fSsrinivas pandruvada 
494401ca24fSsrinivas pandruvada 	spin_lock_irqsave(&pdata->lock, flags);
495401ca24fSsrinivas pandruvada 
496401ca24fSsrinivas pandruvada 	for (i = 0; i < report->maxfield; ++i) {
497401ca24fSsrinivas pandruvada 		hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
498401ca24fSsrinivas pandruvada 				i, report->field[i]->usage->collection_index,
499401ca24fSsrinivas pandruvada 				report->field[i]->usage->hid,
500d4b1bba7SSrinivas Pandruvada 				(report->field[i]->report_size *
501d4b1bba7SSrinivas Pandruvada 					report->field[i]->report_count)/8);
502d4b1bba7SSrinivas Pandruvada 		sz = (report->field[i]->report_size *
503d4b1bba7SSrinivas Pandruvada 					report->field[i]->report_count)/8;
504401ca24fSsrinivas pandruvada 		collection = &hdev->collection[
505401ca24fSsrinivas pandruvada 				report->field[i]->usage->collection_index];
506401ca24fSsrinivas pandruvada 		hid_dbg(hdev, "collection->usage %x\n",
507401ca24fSsrinivas pandruvada 					collection->usage);
508ca2ed12fSSrinivas Pandruvada 
509ca2ed12fSSrinivas Pandruvada 		callback = sensor_hub_get_callback(hdev,
510401ca24fSsrinivas pandruvada 				report->field[i]->physical,
511ca2ed12fSSrinivas Pandruvada 				report->field[i]->usage[0].collection_index,
512ca2ed12fSSrinivas Pandruvada 				&hsdev, &priv);
513e651a1daSSrinivas Pandruvada 		if (!callback) {
514e651a1daSSrinivas Pandruvada 			ptr += sz;
515e651a1daSSrinivas Pandruvada 			continue;
516e651a1daSSrinivas Pandruvada 		}
517eb964833SSrinivas Pandruvada 		if (hsdev->pending.status && (hsdev->pending.attr_usage_id ==
518eb964833SSrinivas Pandruvada 					      report->field[i]->usage->hid ||
519eb964833SSrinivas Pandruvada 					      hsdev->pending.attr_usage_id ==
520eb964833SSrinivas Pandruvada 					      report->field[i]->logical)) {
521e651a1daSSrinivas Pandruvada 			hid_dbg(hdev, "data was pending ...\n");
522e651a1daSSrinivas Pandruvada 			hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
523e651a1daSSrinivas Pandruvada 			if (hsdev->pending.raw_data)
524e651a1daSSrinivas Pandruvada 				hsdev->pending.raw_size = sz;
525e651a1daSSrinivas Pandruvada 			else
526e651a1daSSrinivas Pandruvada 				hsdev->pending.raw_size = 0;
527e651a1daSSrinivas Pandruvada 			complete(&hsdev->pending.ready);
528e651a1daSSrinivas Pandruvada 		}
529e651a1daSSrinivas Pandruvada 		if (callback->capture_sample) {
530401ca24fSsrinivas pandruvada 			if (report->field[i]->logical)
531ca2ed12fSSrinivas Pandruvada 				callback->capture_sample(hsdev,
532401ca24fSsrinivas pandruvada 					report->field[i]->logical, sz, ptr,
533401ca24fSsrinivas pandruvada 					callback->pdev);
534401ca24fSsrinivas pandruvada 			else
535ca2ed12fSSrinivas Pandruvada 				callback->capture_sample(hsdev,
536401ca24fSsrinivas pandruvada 					report->field[i]->usage->hid, sz, ptr,
537401ca24fSsrinivas pandruvada 					callback->pdev);
538401ca24fSsrinivas pandruvada 		}
539401ca24fSsrinivas pandruvada 		ptr += sz;
540401ca24fSsrinivas pandruvada 	}
541401ca24fSsrinivas pandruvada 	if (callback && collection && callback->send_event)
542ca2ed12fSSrinivas Pandruvada 		callback->send_event(hsdev, collection->usage,
543401ca24fSsrinivas pandruvada 				callback->pdev);
544401ca24fSsrinivas pandruvada 	spin_unlock_irqrestore(&pdata->lock, flags);
545401ca24fSsrinivas pandruvada 
546401ca24fSsrinivas pandruvada 	return 1;
547401ca24fSsrinivas pandruvada }
548401ca24fSsrinivas pandruvada 
5491df3a401SSrinivas Pandruvada int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
5501df3a401SSrinivas Pandruvada {
5511df3a401SSrinivas Pandruvada 	int ret = 0;
5521df3a401SSrinivas Pandruvada 	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
5531df3a401SSrinivas Pandruvada 
5541df3a401SSrinivas Pandruvada 	mutex_lock(&data->mutex);
555ca2ed12fSSrinivas Pandruvada 	if (!data->ref_cnt) {
5561df3a401SSrinivas Pandruvada 		ret = hid_hw_open(hsdev->hdev);
5571df3a401SSrinivas Pandruvada 		if (ret) {
5581df3a401SSrinivas Pandruvada 			hid_err(hsdev->hdev, "failed to open hid device\n");
5591df3a401SSrinivas Pandruvada 			mutex_unlock(&data->mutex);
5601df3a401SSrinivas Pandruvada 			return ret;
5611df3a401SSrinivas Pandruvada 		}
5621df3a401SSrinivas Pandruvada 	}
563ca2ed12fSSrinivas Pandruvada 	data->ref_cnt++;
5641df3a401SSrinivas Pandruvada 	mutex_unlock(&data->mutex);
5651df3a401SSrinivas Pandruvada 
5661df3a401SSrinivas Pandruvada 	return ret;
5671df3a401SSrinivas Pandruvada }
5681df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_open);
5691df3a401SSrinivas Pandruvada 
5701df3a401SSrinivas Pandruvada void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
5711df3a401SSrinivas Pandruvada {
5721df3a401SSrinivas Pandruvada 	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
5731df3a401SSrinivas Pandruvada 
5741df3a401SSrinivas Pandruvada 	mutex_lock(&data->mutex);
575ca2ed12fSSrinivas Pandruvada 	data->ref_cnt--;
576ca2ed12fSSrinivas Pandruvada 	if (!data->ref_cnt)
5771df3a401SSrinivas Pandruvada 		hid_hw_close(hsdev->hdev);
5781df3a401SSrinivas Pandruvada 	mutex_unlock(&data->mutex);
5791df3a401SSrinivas Pandruvada }
5801df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_close);
5811df3a401SSrinivas Pandruvada 
582875e36f8SSrinivas Pandruvada static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
583875e36f8SSrinivas Pandruvada 		unsigned int *rsize)
584875e36f8SSrinivas Pandruvada {
585875e36f8SSrinivas Pandruvada 	int index;
586875e36f8SSrinivas Pandruvada 	struct sensor_hub_data *sd =  hid_get_drvdata(hdev);
587875e36f8SSrinivas Pandruvada 	unsigned char report_block[] = {
588875e36f8SSrinivas Pandruvada 				0x0a,  0x16, 0x03, 0x15, 0x00, 0x25, 0x05};
589875e36f8SSrinivas Pandruvada 	unsigned char power_block[] = {
590875e36f8SSrinivas Pandruvada 				0x0a,  0x19, 0x03, 0x15, 0x00, 0x25, 0x05};
591875e36f8SSrinivas Pandruvada 
592875e36f8SSrinivas Pandruvada 	if (!(sd->quirks & HID_SENSOR_HUB_ENUM_QUIRK)) {
593875e36f8SSrinivas Pandruvada 		hid_dbg(hdev, "No Enum quirks\n");
594875e36f8SSrinivas Pandruvada 		return rdesc;
595875e36f8SSrinivas Pandruvada 	}
596875e36f8SSrinivas Pandruvada 
597875e36f8SSrinivas Pandruvada 	/* Looks for power and report state usage id and force to 1 */
598875e36f8SSrinivas Pandruvada 	for (index = 0; index < *rsize; ++index) {
599875e36f8SSrinivas Pandruvada 		if (((*rsize - index) > sizeof(report_block)) &&
600875e36f8SSrinivas Pandruvada 			!memcmp(&rdesc[index], report_block,
601875e36f8SSrinivas Pandruvada 						sizeof(report_block))) {
602875e36f8SSrinivas Pandruvada 			rdesc[index + 4] = 0x01;
603875e36f8SSrinivas Pandruvada 			index += sizeof(report_block);
604875e36f8SSrinivas Pandruvada 		}
605875e36f8SSrinivas Pandruvada 		if (((*rsize - index) > sizeof(power_block)) &&
606875e36f8SSrinivas Pandruvada 			!memcmp(&rdesc[index], power_block,
607875e36f8SSrinivas Pandruvada 						sizeof(power_block))) {
608875e36f8SSrinivas Pandruvada 			rdesc[index + 4] = 0x01;
609875e36f8SSrinivas Pandruvada 			index += sizeof(power_block);
610875e36f8SSrinivas Pandruvada 		}
611875e36f8SSrinivas Pandruvada 	}
612875e36f8SSrinivas Pandruvada 
6139fe8eccaSFernando D S Lima 	/* Checks if the report descriptor of Thinkpad Helix 2 has a logical
6149fe8eccaSFernando D S Lima 	 * minimum for magnetic flux axis greater than the maximum */
6159fe8eccaSFernando D S Lima 	if (hdev->product == USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA &&
6169fe8eccaSFernando D S Lima 		*rsize == 2558 && rdesc[913] == 0x17 && rdesc[914] == 0x40 &&
6179fe8eccaSFernando D S Lima 		rdesc[915] == 0x81 && rdesc[916] == 0x08 &&
6189fe8eccaSFernando D S Lima 		rdesc[917] == 0x00 && rdesc[918] == 0x27 &&
6199fe8eccaSFernando D S Lima 		rdesc[921] == 0x07 && rdesc[922] == 0x00) {
6209fe8eccaSFernando D S Lima 		/* Sets negative logical minimum for mag x, y and z */
6219fe8eccaSFernando D S Lima 		rdesc[914] = rdesc[935] = rdesc[956] = 0xc0;
6229fe8eccaSFernando D S Lima 		rdesc[915] = rdesc[936] = rdesc[957] = 0x7e;
6239fe8eccaSFernando D S Lima 		rdesc[916] = rdesc[937] = rdesc[958] = 0xf7;
6249fe8eccaSFernando D S Lima 		rdesc[917] = rdesc[938] = rdesc[959] = 0xff;
6259fe8eccaSFernando D S Lima 	}
6269fe8eccaSFernando D S Lima 
627875e36f8SSrinivas Pandruvada 	return rdesc;
628875e36f8SSrinivas Pandruvada }
629875e36f8SSrinivas Pandruvada 
630401ca24fSsrinivas pandruvada static int sensor_hub_probe(struct hid_device *hdev,
631401ca24fSsrinivas pandruvada 				const struct hid_device_id *id)
632401ca24fSsrinivas pandruvada {
633401ca24fSsrinivas pandruvada 	int ret;
634401ca24fSsrinivas pandruvada 	struct sensor_hub_data *sd;
635401ca24fSsrinivas pandruvada 	int i;
636401ca24fSsrinivas pandruvada 	char *name;
637401ca24fSsrinivas pandruvada 	int dev_cnt;
638ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev;
639ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *last_hsdev = NULL;
640cb67126fSSrinivas Pandruvada 	struct hid_sensor_hub_device *collection_hsdev = NULL;
641401ca24fSsrinivas pandruvada 
642905cc199SAndy Shevchenko 	sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
643401ca24fSsrinivas pandruvada 	if (!sd) {
644401ca24fSsrinivas pandruvada 		hid_err(hdev, "cannot allocate Sensor data\n");
645401ca24fSsrinivas pandruvada 		return -ENOMEM;
646401ca24fSsrinivas pandruvada 	}
647ca2ed12fSSrinivas Pandruvada 
648401ca24fSsrinivas pandruvada 	hid_set_drvdata(hdev, sd);
649875e36f8SSrinivas Pandruvada 	sd->quirks = id->driver_data;
650ca2ed12fSSrinivas Pandruvada 
651401ca24fSsrinivas pandruvada 	spin_lock_init(&sd->lock);
652401ca24fSsrinivas pandruvada 	spin_lock_init(&sd->dyn_callback_lock);
653401ca24fSsrinivas pandruvada 	mutex_init(&sd->mutex);
654401ca24fSsrinivas pandruvada 	ret = hid_parse(hdev);
655401ca24fSsrinivas pandruvada 	if (ret) {
656401ca24fSsrinivas pandruvada 		hid_err(hdev, "parse failed\n");
657905cc199SAndy Shevchenko 		return ret;
658401ca24fSsrinivas pandruvada 	}
659401ca24fSsrinivas pandruvada 	INIT_LIST_HEAD(&hdev->inputs);
660401ca24fSsrinivas pandruvada 
661401ca24fSsrinivas pandruvada 	ret = hid_hw_start(hdev, 0);
662401ca24fSsrinivas pandruvada 	if (ret) {
663401ca24fSsrinivas pandruvada 		hid_err(hdev, "hw start failed\n");
664905cc199SAndy Shevchenko 		return ret;
665401ca24fSsrinivas pandruvada 	}
666401ca24fSsrinivas pandruvada 	INIT_LIST_HEAD(&sd->dyn_callback_list);
667401ca24fSsrinivas pandruvada 	sd->hid_sensor_client_cnt = 0;
668401ca24fSsrinivas pandruvada 
669ca2ed12fSSrinivas Pandruvada 	dev_cnt = sensor_hub_get_physical_device_count(hdev);
670401ca24fSsrinivas pandruvada 	if (dev_cnt > HID_MAX_PHY_DEVICES) {
671401ca24fSsrinivas pandruvada 		hid_err(hdev, "Invalid Physical device count\n");
672401ca24fSsrinivas pandruvada 		ret = -EINVAL;
6731df3a401SSrinivas Pandruvada 		goto err_stop_hw;
674401ca24fSsrinivas pandruvada 	}
6755be5db24SHimangi Saraogi 	sd->hid_sensor_hub_client_devs = devm_kzalloc(&hdev->dev, dev_cnt *
676401ca24fSsrinivas pandruvada 						      sizeof(struct mfd_cell),
677401ca24fSsrinivas pandruvada 						      GFP_KERNEL);
678401ca24fSsrinivas pandruvada 	if (sd->hid_sensor_hub_client_devs == NULL) {
679f2f13a68SAxel Lin 		hid_err(hdev, "Failed to allocate memory for mfd cells\n");
680401ca24fSsrinivas pandruvada 		ret = -ENOMEM;
6811df3a401SSrinivas Pandruvada 		goto err_stop_hw;
682401ca24fSsrinivas pandruvada 	}
683ca2ed12fSSrinivas Pandruvada 
684ca2ed12fSSrinivas Pandruvada 	for (i = 0; i < hdev->maxcollection; ++i) {
685ca2ed12fSSrinivas Pandruvada 		struct hid_collection *collection = &hdev->collection[i];
686ca2ed12fSSrinivas Pandruvada 
687cb67126fSSrinivas Pandruvada 		if (collection->type == HID_COLLECTION_PHYSICAL ||
688cb67126fSSrinivas Pandruvada 		    collection->type == HID_COLLECTION_APPLICATION) {
689ca2ed12fSSrinivas Pandruvada 
6905be5db24SHimangi Saraogi 			hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev),
6915be5db24SHimangi Saraogi 					     GFP_KERNEL);
692ca2ed12fSSrinivas Pandruvada 			if (!hsdev) {
693ca2ed12fSSrinivas Pandruvada 				hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
694ca2ed12fSSrinivas Pandruvada 				ret = -ENOMEM;
6955be5db24SHimangi Saraogi 				goto err_stop_hw;
696ca2ed12fSSrinivas Pandruvada 			}
697ca2ed12fSSrinivas Pandruvada 			hsdev->hdev = hdev;
698ca2ed12fSSrinivas Pandruvada 			hsdev->vendor_id = hdev->vendor;
699ca2ed12fSSrinivas Pandruvada 			hsdev->product_id = hdev->product;
700e651a1daSSrinivas Pandruvada 			hsdev->usage = collection->usage;
7012d94e522SSrinivas Pandruvada 			hsdev->mutex_ptr = devm_kzalloc(&hdev->dev,
7022d94e522SSrinivas Pandruvada 							sizeof(struct mutex),
7032d94e522SSrinivas Pandruvada 							GFP_KERNEL);
7042d94e522SSrinivas Pandruvada 			if (!hsdev->mutex_ptr) {
7052d94e522SSrinivas Pandruvada 				ret = -ENOMEM;
7062d94e522SSrinivas Pandruvada 				goto err_stop_hw;
7072d94e522SSrinivas Pandruvada 			}
7082d94e522SSrinivas Pandruvada 			mutex_init(hsdev->mutex_ptr);
709ca2ed12fSSrinivas Pandruvada 			hsdev->start_collection_index = i;
710ca2ed12fSSrinivas Pandruvada 			if (last_hsdev)
711ca2ed12fSSrinivas Pandruvada 				last_hsdev->end_collection_index = i;
712ca2ed12fSSrinivas Pandruvada 			last_hsdev = hsdev;
7135be5db24SHimangi Saraogi 			name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
7145be5db24SHimangi Saraogi 					      "HID-SENSOR-%x",
715ca2ed12fSSrinivas Pandruvada 					      collection->usage);
716401ca24fSsrinivas pandruvada 			if (name == NULL) {
717f2f13a68SAxel Lin 				hid_err(hdev, "Failed MFD device name\n");
718401ca24fSsrinivas pandruvada 				ret = -ENOMEM;
7195be5db24SHimangi Saraogi 				goto err_stop_hw;
720401ca24fSsrinivas pandruvada 			}
721401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
722401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].name = name;
723401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
724401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].platform_data =
725ca2ed12fSSrinivas Pandruvada 							hsdev;
726401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
727401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].pdata_size =
728ca2ed12fSSrinivas Pandruvada 							sizeof(*hsdev);
729ca2ed12fSSrinivas Pandruvada 			hid_dbg(hdev, "Adding %s:%d\n", name,
730ca2ed12fSSrinivas Pandruvada 					hsdev->start_collection_index);
731401ca24fSsrinivas pandruvada 			sd->hid_sensor_client_cnt++;
732cb67126fSSrinivas Pandruvada 			if (collection_hsdev)
733cb67126fSSrinivas Pandruvada 				collection_hsdev->end_collection_index = i;
734cb67126fSSrinivas Pandruvada 			if (collection->type == HID_COLLECTION_APPLICATION &&
735cb67126fSSrinivas Pandruvada 			    collection->usage == HID_USAGE_SENSOR_COLLECTION)
736cb67126fSSrinivas Pandruvada 				collection_hsdev = hsdev;
737401ca24fSsrinivas pandruvada 		}
738401ca24fSsrinivas pandruvada 	}
739ca2ed12fSSrinivas Pandruvada 	if (last_hsdev)
740ca2ed12fSSrinivas Pandruvada 		last_hsdev->end_collection_index = i;
741cb67126fSSrinivas Pandruvada 	if (collection_hsdev)
742cb67126fSSrinivas Pandruvada 		collection_hsdev->end_collection_index = i;
743ca2ed12fSSrinivas Pandruvada 
74416b5fe29SJohan Hovold 	ret = mfd_add_hotplug_devices(&hdev->dev,
74516b5fe29SJohan Hovold 			sd->hid_sensor_hub_client_devs,
74616b5fe29SJohan Hovold 			sd->hid_sensor_client_cnt);
747401ca24fSsrinivas pandruvada 	if (ret < 0)
7485be5db24SHimangi Saraogi 		goto err_stop_hw;
749401ca24fSsrinivas pandruvada 
750401ca24fSsrinivas pandruvada 	return ret;
751401ca24fSsrinivas pandruvada 
752401ca24fSsrinivas pandruvada err_stop_hw:
753401ca24fSsrinivas pandruvada 	hid_hw_stop(hdev);
754401ca24fSsrinivas pandruvada 
755401ca24fSsrinivas pandruvada 	return ret;
756401ca24fSsrinivas pandruvada }
757401ca24fSsrinivas pandruvada 
758401ca24fSsrinivas pandruvada static void sensor_hub_remove(struct hid_device *hdev)
759401ca24fSsrinivas pandruvada {
760401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hdev);
761401ca24fSsrinivas pandruvada 	unsigned long flags;
762e651a1daSSrinivas Pandruvada 	int i;
763401ca24fSsrinivas pandruvada 
764401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " hardware removed\n");
765401ca24fSsrinivas pandruvada 	hid_hw_close(hdev);
766f2f13a68SAxel Lin 	hid_hw_stop(hdev);
767401ca24fSsrinivas pandruvada 	spin_lock_irqsave(&data->lock, flags);
768e651a1daSSrinivas Pandruvada 	for (i = 0; i < data->hid_sensor_client_cnt; ++i) {
769e651a1daSSrinivas Pandruvada 		struct hid_sensor_hub_device *hsdev =
770e651a1daSSrinivas Pandruvada 			data->hid_sensor_hub_client_devs[i].platform_data;
771e651a1daSSrinivas Pandruvada 		if (hsdev->pending.status)
772e651a1daSSrinivas Pandruvada 			complete(&hsdev->pending.ready);
773e651a1daSSrinivas Pandruvada 	}
774401ca24fSsrinivas pandruvada 	spin_unlock_irqrestore(&data->lock, flags);
775401ca24fSsrinivas pandruvada 	mfd_remove_devices(&hdev->dev);
776401ca24fSsrinivas pandruvada 	hid_set_drvdata(hdev, NULL);
777401ca24fSsrinivas pandruvada 	mutex_destroy(&data->mutex);
778401ca24fSsrinivas pandruvada }
779401ca24fSsrinivas pandruvada 
780401ca24fSsrinivas pandruvada static const struct hid_device_id sensor_hub_devices[] = {
781875e36f8SSrinivas Pandruvada 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_0,
78230f58877SStephen Chandler Paul 			USB_DEVICE_ID_INTEL_HID_SENSOR_0),
783875e36f8SSrinivas Pandruvada 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
784875e36f8SSrinivas Pandruvada 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
78530f58877SStephen Chandler Paul 			USB_DEVICE_ID_INTEL_HID_SENSOR_0),
78630f58877SStephen Chandler Paul 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
78730f58877SStephen Chandler Paul 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
78830f58877SStephen Chandler Paul 			USB_DEVICE_ID_INTEL_HID_SENSOR_1),
789875e36f8SSrinivas Pandruvada 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
79000478ee8SReyad Attiyat 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
79100478ee8SReyad Attiyat 			USB_DEVICE_ID_MS_SURFACE_PRO_2),
79200478ee8SReyad Attiyat 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
79300478ee8SReyad Attiyat 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
79400478ee8SReyad Attiyat 			USB_DEVICE_ID_MS_TOUCH_COVER_2),
79500478ee8SReyad Attiyat 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
79600478ee8SReyad Attiyat 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
79700478ee8SReyad Attiyat 			USB_DEVICE_ID_MS_TYPE_COVER_2),
79800478ee8SReyad Attiyat 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
799da809197SBenjamin Tissoires 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
800da809197SBenjamin Tissoires 			0x07bd), /* Microsoft Surface 3 */
801da809197SBenjamin Tissoires 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
8025cc5084dSBenjamin Tissoires 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROCHIP,
8035cc5084dSBenjamin Tissoires 			0x0f01), /* MM7150 */
8045cc5084dSBenjamin Tissoires 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
805218eb9edSArchana Patni 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
806467669c5SSrinivas Pandruvada 			USB_DEVICE_ID_STM_HID_SENSOR),
807467669c5SSrinivas Pandruvada 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
808467669c5SSrinivas Pandruvada 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
809dde3b45cSArchana Patni 			USB_DEVICE_ID_STM_HID_SENSOR_1),
810218eb9edSArchana Patni 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
811825747bbSPeter F. Patel-Schneider 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_TEXAS_INSTRUMENTS,
812825747bbSPeter F. Patel-Schneider 			USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA),
813825747bbSPeter F. Patel-Schneider 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
81421589ebdSGuilhem Lettron 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_ITE,
81521589ebdSGuilhem Lettron 			USB_DEVICE_ID_ITE_LENOVO_YOGA),
81621589ebdSGuilhem Lettron 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
817e8e88438SRitesh Raj Sarraf 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_ITE,
818e8e88438SRitesh Raj Sarraf 			USB_DEVICE_ID_ITE_LENOVO_YOGA2),
819e8e88438SRitesh Raj Sarraf 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
82076833559SSrinivas Pandruvada 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_ITE,
82176833559SSrinivas Pandruvada 			USB_DEVICE_ID_ITE_LENOVO_YOGA900),
82276833559SSrinivas Pandruvada 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
823930fafd9SSrinivas Pandruvada 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_0,
824930fafd9SSrinivas Pandruvada 			0x22D8),
825930fafd9SSrinivas Pandruvada 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
8260d1e4b02SMika Westerberg 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
8270d1e4b02SMika Westerberg 		     HID_ANY_ID) },
828401ca24fSsrinivas pandruvada 	{ }
829401ca24fSsrinivas pandruvada };
830401ca24fSsrinivas pandruvada MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
831401ca24fSsrinivas pandruvada 
832401ca24fSsrinivas pandruvada static struct hid_driver sensor_hub_driver = {
833401ca24fSsrinivas pandruvada 	.name = "hid-sensor-hub",
834401ca24fSsrinivas pandruvada 	.id_table = sensor_hub_devices,
835401ca24fSsrinivas pandruvada 	.probe = sensor_hub_probe,
836401ca24fSsrinivas pandruvada 	.remove = sensor_hub_remove,
837401ca24fSsrinivas pandruvada 	.raw_event = sensor_hub_raw_event,
838875e36f8SSrinivas Pandruvada 	.report_fixup = sensor_hub_report_fixup,
839401ca24fSsrinivas pandruvada #ifdef CONFIG_PM
840401ca24fSsrinivas pandruvada 	.suspend = sensor_hub_suspend,
841401ca24fSsrinivas pandruvada 	.resume = sensor_hub_resume,
842401ca24fSsrinivas pandruvada 	.reset_resume = sensor_hub_reset_resume,
843401ca24fSsrinivas pandruvada #endif
844401ca24fSsrinivas pandruvada };
845f425458eSH Hartley Sweeten module_hid_driver(sensor_hub_driver);
846401ca24fSsrinivas pandruvada 
847401ca24fSsrinivas pandruvada MODULE_DESCRIPTION("HID Sensor Hub driver");
848401ca24fSsrinivas pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
849401ca24fSsrinivas pandruvada MODULE_LICENSE("GPL");
850