xref: /linux/drivers/hid/hid-sensor-hub.c (revision ca2ed12f163bf430a425ec725ef76df5254b9f45)
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_pending - Synchronous read pending information
33401ca24fSsrinivas pandruvada  * @status:		Pending status true/false.
34401ca24fSsrinivas pandruvada  * @ready:		Completion synchronization data.
35401ca24fSsrinivas pandruvada  * @usage_id:		Usage id for physical device, E.g. Gyro usage id.
36401ca24fSsrinivas pandruvada  * @attr_usage_id:	Usage Id of a field, E.g. X-AXIS for a gyro.
37401ca24fSsrinivas pandruvada  * @raw_size:		Response size for a read request.
38401ca24fSsrinivas pandruvada  * @raw_data:		Place holder for received response.
39401ca24fSsrinivas pandruvada  */
40401ca24fSsrinivas pandruvada struct sensor_hub_pending {
41401ca24fSsrinivas pandruvada 	bool status;
42401ca24fSsrinivas pandruvada 	struct completion ready;
43401ca24fSsrinivas pandruvada 	u32 usage_id;
44401ca24fSsrinivas pandruvada 	u32 attr_usage_id;
45401ca24fSsrinivas pandruvada 	int raw_size;
46401ca24fSsrinivas pandruvada 	u8  *raw_data;
47401ca24fSsrinivas pandruvada };
48401ca24fSsrinivas pandruvada 
49401ca24fSsrinivas pandruvada /**
50401ca24fSsrinivas pandruvada  * struct sensor_hub_data - Hold a instance data for a HID hub device
51401ca24fSsrinivas pandruvada  * @hsdev:		Stored hid instance for current hub device.
52401ca24fSsrinivas pandruvada  * @mutex:		Mutex to serialize synchronous request.
53401ca24fSsrinivas pandruvada  * @lock:		Spin lock to protect pending request structure.
54401ca24fSsrinivas pandruvada  * @pending:		Holds information of pending sync read request.
55401ca24fSsrinivas pandruvada  * @dyn_callback_list:	Holds callback function
56401ca24fSsrinivas pandruvada  * @dyn_callback_lock:	spin lock to protect callback list
57401ca24fSsrinivas pandruvada  * @hid_sensor_hub_client_devs:	Stores all MFD cells for a hub instance.
58401ca24fSsrinivas pandruvada  * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
59*ca2ed12fSSrinivas Pandruvada  * @ref_cnt:		Number of MFD clients have opened this device
60401ca24fSsrinivas pandruvada  */
61401ca24fSsrinivas pandruvada struct sensor_hub_data {
62401ca24fSsrinivas pandruvada 	struct mutex mutex;
63401ca24fSsrinivas pandruvada 	spinlock_t lock;
64401ca24fSsrinivas pandruvada 	struct sensor_hub_pending pending;
65401ca24fSsrinivas pandruvada 	struct list_head dyn_callback_list;
66401ca24fSsrinivas pandruvada 	spinlock_t dyn_callback_lock;
67401ca24fSsrinivas pandruvada 	struct mfd_cell *hid_sensor_hub_client_devs;
68401ca24fSsrinivas pandruvada 	int hid_sensor_client_cnt;
69875e36f8SSrinivas Pandruvada 	unsigned long quirks;
70*ca2ed12fSSrinivas Pandruvada 	int ref_cnt;
71401ca24fSsrinivas pandruvada };
72401ca24fSsrinivas pandruvada 
73401ca24fSsrinivas pandruvada /**
74401ca24fSsrinivas pandruvada  * struct hid_sensor_hub_callbacks_list - Stores callback list
75401ca24fSsrinivas pandruvada  * @list:		list head.
76401ca24fSsrinivas pandruvada  * @usage_id:		usage id for a physical device.
77401ca24fSsrinivas pandruvada  * @usage_callback:	Stores registered callback functions.
78401ca24fSsrinivas pandruvada  * @priv:		Private data for a physical device.
79401ca24fSsrinivas pandruvada  */
80401ca24fSsrinivas pandruvada struct hid_sensor_hub_callbacks_list {
81401ca24fSsrinivas pandruvada 	struct list_head list;
82401ca24fSsrinivas pandruvada 	u32 usage_id;
83*ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev;
84401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks *usage_callback;
85401ca24fSsrinivas pandruvada 	void *priv;
86401ca24fSsrinivas pandruvada };
87401ca24fSsrinivas pandruvada 
88401ca24fSsrinivas pandruvada static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
89401ca24fSsrinivas pandruvada 						int dir)
90401ca24fSsrinivas pandruvada {
91401ca24fSsrinivas pandruvada 	struct hid_report *report;
92401ca24fSsrinivas pandruvada 
93401ca24fSsrinivas pandruvada 	list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
94401ca24fSsrinivas pandruvada 		if (report->id == id)
95401ca24fSsrinivas pandruvada 			return report;
96401ca24fSsrinivas pandruvada 	}
97401ca24fSsrinivas pandruvada 	hid_warn(hdev, "No report with id 0x%x found\n", id);
98401ca24fSsrinivas pandruvada 
99401ca24fSsrinivas pandruvada 	return NULL;
100401ca24fSsrinivas pandruvada }
101401ca24fSsrinivas pandruvada 
102*ca2ed12fSSrinivas Pandruvada static int sensor_hub_get_physical_device_count(struct hid_device *hdev)
103401ca24fSsrinivas pandruvada {
104*ca2ed12fSSrinivas Pandruvada 	int i;
105*ca2ed12fSSrinivas Pandruvada 	int count = 0;
106401ca24fSsrinivas pandruvada 
107*ca2ed12fSSrinivas Pandruvada 	for (i = 0; i < hdev->maxcollection; ++i) {
108*ca2ed12fSSrinivas Pandruvada 		struct hid_collection *collection = &hdev->collection[i];
109*ca2ed12fSSrinivas Pandruvada 		if (collection->type == HID_COLLECTION_PHYSICAL)
110*ca2ed12fSSrinivas Pandruvada 			++count;
111401ca24fSsrinivas pandruvada 	}
112401ca24fSsrinivas pandruvada 
113*ca2ed12fSSrinivas Pandruvada 	return count;
114401ca24fSsrinivas pandruvada }
115401ca24fSsrinivas pandruvada 
116401ca24fSsrinivas pandruvada static void sensor_hub_fill_attr_info(
117401ca24fSsrinivas pandruvada 		struct hid_sensor_hub_attribute_info *info,
1189f740ffaSSrinivas Pandruvada 		s32 index, s32 report_id, struct hid_field *field)
119401ca24fSsrinivas pandruvada {
120401ca24fSsrinivas pandruvada 	info->index = index;
121401ca24fSsrinivas pandruvada 	info->report_id = report_id;
1229f740ffaSSrinivas Pandruvada 	info->units = field->unit;
1239f740ffaSSrinivas Pandruvada 	info->unit_expo = field->unit_exponent;
1249f740ffaSSrinivas Pandruvada 	info->size = (field->report_size * field->report_count)/8;
1259f740ffaSSrinivas Pandruvada 	info->logical_minimum = field->logical_minimum;
1269f740ffaSSrinivas Pandruvada 	info->logical_maximum = field->logical_maximum;
127401ca24fSsrinivas pandruvada }
128401ca24fSsrinivas pandruvada 
129401ca24fSsrinivas pandruvada static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
130401ca24fSsrinivas pandruvada 					struct hid_device *hdev,
131*ca2ed12fSSrinivas Pandruvada 					u32 usage_id,
132*ca2ed12fSSrinivas Pandruvada 					int collection_index,
133*ca2ed12fSSrinivas Pandruvada 					struct hid_sensor_hub_device **hsdev,
134*ca2ed12fSSrinivas Pandruvada 					void **priv)
135401ca24fSsrinivas pandruvada {
136401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
137401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
138401ca24fSsrinivas pandruvada 
139401ca24fSsrinivas pandruvada 	spin_lock(&pdata->dyn_callback_lock);
140401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
141*ca2ed12fSSrinivas Pandruvada 		if (callback->usage_id == usage_id &&
142*ca2ed12fSSrinivas Pandruvada 			(collection_index >=
143*ca2ed12fSSrinivas Pandruvada 				callback->hsdev->start_collection_index) &&
144*ca2ed12fSSrinivas Pandruvada 			(collection_index <
145*ca2ed12fSSrinivas Pandruvada 				callback->hsdev->end_collection_index)) {
146401ca24fSsrinivas pandruvada 			*priv = callback->priv;
147*ca2ed12fSSrinivas Pandruvada 			*hsdev = callback->hsdev;
148401ca24fSsrinivas pandruvada 			spin_unlock(&pdata->dyn_callback_lock);
149401ca24fSsrinivas pandruvada 			return callback->usage_callback;
150401ca24fSsrinivas pandruvada 		}
151401ca24fSsrinivas pandruvada 	spin_unlock(&pdata->dyn_callback_lock);
152401ca24fSsrinivas pandruvada 
153401ca24fSsrinivas pandruvada 	return NULL;
154401ca24fSsrinivas pandruvada }
155401ca24fSsrinivas pandruvada 
156401ca24fSsrinivas pandruvada int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
157401ca24fSsrinivas pandruvada 			u32 usage_id,
158401ca24fSsrinivas pandruvada 			struct hid_sensor_hub_callbacks *usage_callback)
159401ca24fSsrinivas pandruvada {
160401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
161401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
162401ca24fSsrinivas pandruvada 
163401ca24fSsrinivas pandruvada 	spin_lock(&pdata->dyn_callback_lock);
164401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
165*ca2ed12fSSrinivas Pandruvada 		if (callback->usage_id == usage_id &&
166*ca2ed12fSSrinivas Pandruvada 						callback->hsdev == hsdev) {
167401ca24fSsrinivas pandruvada 			spin_unlock(&pdata->dyn_callback_lock);
168401ca24fSsrinivas pandruvada 			return -EINVAL;
169401ca24fSsrinivas pandruvada 		}
1702b7c4b8eSDan Carpenter 	callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
171401ca24fSsrinivas pandruvada 	if (!callback) {
172401ca24fSsrinivas pandruvada 		spin_unlock(&pdata->dyn_callback_lock);
173401ca24fSsrinivas pandruvada 		return -ENOMEM;
174401ca24fSsrinivas pandruvada 	}
175*ca2ed12fSSrinivas Pandruvada 	callback->hsdev = hsdev;
176401ca24fSsrinivas pandruvada 	callback->usage_callback = usage_callback;
177401ca24fSsrinivas pandruvada 	callback->usage_id = usage_id;
178401ca24fSsrinivas pandruvada 	callback->priv = NULL;
179401ca24fSsrinivas pandruvada 	list_add_tail(&callback->list, &pdata->dyn_callback_list);
180401ca24fSsrinivas pandruvada 	spin_unlock(&pdata->dyn_callback_lock);
181401ca24fSsrinivas pandruvada 
182401ca24fSsrinivas pandruvada 	return 0;
183401ca24fSsrinivas pandruvada }
184401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
185401ca24fSsrinivas pandruvada 
186401ca24fSsrinivas pandruvada int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
187401ca24fSsrinivas pandruvada 				u32 usage_id)
188401ca24fSsrinivas pandruvada {
189401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
190401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
191401ca24fSsrinivas pandruvada 
192401ca24fSsrinivas pandruvada 	spin_lock(&pdata->dyn_callback_lock);
193401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list)
194*ca2ed12fSSrinivas Pandruvada 		if (callback->usage_id == usage_id &&
195*ca2ed12fSSrinivas Pandruvada 						callback->hsdev == hsdev) {
196401ca24fSsrinivas pandruvada 			list_del(&callback->list);
197401ca24fSsrinivas pandruvada 			kfree(callback);
198401ca24fSsrinivas pandruvada 			break;
199401ca24fSsrinivas pandruvada 		}
200401ca24fSsrinivas pandruvada 	spin_unlock(&pdata->dyn_callback_lock);
201401ca24fSsrinivas pandruvada 
202401ca24fSsrinivas pandruvada 	return 0;
203401ca24fSsrinivas pandruvada }
204401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
205401ca24fSsrinivas pandruvada 
206401ca24fSsrinivas pandruvada int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
207401ca24fSsrinivas pandruvada 				u32 field_index, s32 value)
208401ca24fSsrinivas pandruvada {
209401ca24fSsrinivas pandruvada 	struct hid_report *report;
210401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
211401ca24fSsrinivas pandruvada 	int ret = 0;
212401ca24fSsrinivas pandruvada 
213401ca24fSsrinivas pandruvada 	mutex_lock(&data->mutex);
214401ca24fSsrinivas pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
215401ca24fSsrinivas pandruvada 	if (!report || (field_index >= report->maxfield)) {
216401ca24fSsrinivas pandruvada 		ret = -EINVAL;
217401ca24fSsrinivas pandruvada 		goto done_proc;
218401ca24fSsrinivas pandruvada 	}
219401ca24fSsrinivas pandruvada 	hid_set_field(report->field[field_index], 0, value);
220d8814272SBenjamin Tissoires 	hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
221b7966a4dSBenjamin Tissoires 	hid_hw_wait(hsdev->hdev);
222401ca24fSsrinivas pandruvada 
223401ca24fSsrinivas pandruvada done_proc:
224401ca24fSsrinivas pandruvada 	mutex_unlock(&data->mutex);
225401ca24fSsrinivas pandruvada 
226401ca24fSsrinivas pandruvada 	return ret;
227401ca24fSsrinivas pandruvada }
228401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
229401ca24fSsrinivas pandruvada 
230401ca24fSsrinivas pandruvada int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
231401ca24fSsrinivas pandruvada 				u32 field_index, s32 *value)
232401ca24fSsrinivas pandruvada {
233401ca24fSsrinivas pandruvada 	struct hid_report *report;
234401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
235401ca24fSsrinivas pandruvada 	int ret = 0;
236401ca24fSsrinivas pandruvada 
237401ca24fSsrinivas pandruvada 	mutex_lock(&data->mutex);
238401ca24fSsrinivas pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
2399e891025SKees Cook 	if (!report || (field_index >= report->maxfield) ||
2409e891025SKees Cook 	    report->field[field_index]->report_count < 1) {
241401ca24fSsrinivas pandruvada 		ret = -EINVAL;
242401ca24fSsrinivas pandruvada 		goto done_proc;
243401ca24fSsrinivas pandruvada 	}
244d8814272SBenjamin Tissoires 	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
245b7966a4dSBenjamin Tissoires 	hid_hw_wait(hsdev->hdev);
246401ca24fSsrinivas pandruvada 	*value = report->field[field_index]->value[0];
247401ca24fSsrinivas pandruvada 
248401ca24fSsrinivas pandruvada done_proc:
249401ca24fSsrinivas pandruvada 	mutex_unlock(&data->mutex);
250401ca24fSsrinivas pandruvada 
251401ca24fSsrinivas pandruvada 	return ret;
252401ca24fSsrinivas pandruvada }
253401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
254401ca24fSsrinivas pandruvada 
255401ca24fSsrinivas pandruvada 
256401ca24fSsrinivas pandruvada int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
257401ca24fSsrinivas pandruvada 					u32 usage_id,
258401ca24fSsrinivas pandruvada 					u32 attr_usage_id, u32 report_id)
259401ca24fSsrinivas pandruvada {
260401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
261401ca24fSsrinivas pandruvada 	unsigned long flags;
262401ca24fSsrinivas pandruvada 	struct hid_report *report;
263401ca24fSsrinivas pandruvada 	int ret_val = 0;
264401ca24fSsrinivas pandruvada 
265401ca24fSsrinivas pandruvada 	mutex_lock(&data->mutex);
266401ca24fSsrinivas pandruvada 	memset(&data->pending, 0, sizeof(data->pending));
267401ca24fSsrinivas pandruvada 	init_completion(&data->pending.ready);
268401ca24fSsrinivas pandruvada 	data->pending.usage_id = usage_id;
269401ca24fSsrinivas pandruvada 	data->pending.attr_usage_id = attr_usage_id;
270401ca24fSsrinivas pandruvada 	data->pending.raw_size = 0;
271401ca24fSsrinivas pandruvada 
272401ca24fSsrinivas pandruvada 	spin_lock_irqsave(&data->lock, flags);
273401ca24fSsrinivas pandruvada 	data->pending.status = true;
274401ca24fSsrinivas pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
275401ca24fSsrinivas pandruvada 	if (!report) {
276401ca24fSsrinivas pandruvada 		spin_unlock_irqrestore(&data->lock, flags);
277401ca24fSsrinivas pandruvada 		goto err_free;
278401ca24fSsrinivas pandruvada 	}
279d8814272SBenjamin Tissoires 	hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
280401ca24fSsrinivas pandruvada 	spin_unlock_irqrestore(&data->lock, flags);
281401ca24fSsrinivas pandruvada 	wait_for_completion_interruptible_timeout(&data->pending.ready, HZ*5);
282401ca24fSsrinivas pandruvada 	switch (data->pending.raw_size) {
283401ca24fSsrinivas pandruvada 	case 1:
284401ca24fSsrinivas pandruvada 		ret_val = *(u8 *)data->pending.raw_data;
285401ca24fSsrinivas pandruvada 		break;
286401ca24fSsrinivas pandruvada 	case 2:
287401ca24fSsrinivas pandruvada 		ret_val = *(u16 *)data->pending.raw_data;
288401ca24fSsrinivas pandruvada 		break;
289401ca24fSsrinivas pandruvada 	case 4:
290401ca24fSsrinivas pandruvada 		ret_val = *(u32 *)data->pending.raw_data;
291401ca24fSsrinivas pandruvada 		break;
292401ca24fSsrinivas pandruvada 	default:
293401ca24fSsrinivas pandruvada 		ret_val = 0;
294401ca24fSsrinivas pandruvada 	}
295401ca24fSsrinivas pandruvada 	kfree(data->pending.raw_data);
296401ca24fSsrinivas pandruvada 
297401ca24fSsrinivas pandruvada err_free:
298401ca24fSsrinivas pandruvada 	data->pending.status = false;
299401ca24fSsrinivas pandruvada 	mutex_unlock(&data->mutex);
300401ca24fSsrinivas pandruvada 
301401ca24fSsrinivas pandruvada 	return ret_val;
302401ca24fSsrinivas pandruvada }
303401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
304401ca24fSsrinivas pandruvada 
305e02cee48SSrinivas Pandruvada int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
306e02cee48SSrinivas Pandruvada 				u32 report_id, int field_index, u32 usage_id)
307e02cee48SSrinivas Pandruvada {
308e02cee48SSrinivas Pandruvada 	struct hid_report *report;
309e02cee48SSrinivas Pandruvada 	struct hid_field *field;
310e02cee48SSrinivas Pandruvada 	int i;
311e02cee48SSrinivas Pandruvada 
312e02cee48SSrinivas Pandruvada 	report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
313e02cee48SSrinivas Pandruvada 	if (!report || (field_index >= report->maxfield))
314e02cee48SSrinivas Pandruvada 		goto done_proc;
315e02cee48SSrinivas Pandruvada 
316e02cee48SSrinivas Pandruvada 	field = report->field[field_index];
317e02cee48SSrinivas Pandruvada 	for (i = 0; i < field->maxusage; ++i) {
318e02cee48SSrinivas Pandruvada 		if (field->usage[i].hid == usage_id)
319e02cee48SSrinivas Pandruvada 			return field->usage[i].usage_index;
320e02cee48SSrinivas Pandruvada 	}
321e02cee48SSrinivas Pandruvada 
322e02cee48SSrinivas Pandruvada done_proc:
323e02cee48SSrinivas Pandruvada 	return -EINVAL;
324e02cee48SSrinivas Pandruvada }
325e02cee48SSrinivas Pandruvada EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index);
326e02cee48SSrinivas Pandruvada 
327401ca24fSsrinivas pandruvada int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
328401ca24fSsrinivas pandruvada 				u8 type,
329401ca24fSsrinivas pandruvada 				u32 usage_id,
330401ca24fSsrinivas pandruvada 				u32 attr_usage_id,
331401ca24fSsrinivas pandruvada 				struct hid_sensor_hub_attribute_info *info)
332401ca24fSsrinivas pandruvada {
333401ca24fSsrinivas pandruvada 	int ret = -1;
334*ca2ed12fSSrinivas Pandruvada 	int i;
335401ca24fSsrinivas pandruvada 	struct hid_report *report;
336401ca24fSsrinivas pandruvada 	struct hid_field *field;
337401ca24fSsrinivas pandruvada 	struct hid_report_enum *report_enum;
338401ca24fSsrinivas pandruvada 	struct hid_device *hdev = hsdev->hdev;
339401ca24fSsrinivas pandruvada 
340401ca24fSsrinivas pandruvada 	/* Initialize with defaults */
341401ca24fSsrinivas pandruvada 	info->usage_id = usage_id;
342401ca24fSsrinivas pandruvada 	info->attrib_id = attr_usage_id;
343401ca24fSsrinivas pandruvada 	info->report_id = -1;
344401ca24fSsrinivas pandruvada 	info->index = -1;
345401ca24fSsrinivas pandruvada 	info->units = -1;
346401ca24fSsrinivas pandruvada 	info->unit_expo = -1;
347401ca24fSsrinivas pandruvada 
348401ca24fSsrinivas pandruvada 	report_enum = &hdev->report_enum[type];
349401ca24fSsrinivas pandruvada 	list_for_each_entry(report, &report_enum->report_list, list) {
350401ca24fSsrinivas pandruvada 		for (i = 0; i < report->maxfield; ++i) {
351401ca24fSsrinivas pandruvada 			field = report->field[i];
352*ca2ed12fSSrinivas Pandruvada 			if (field->maxusage) {
353401ca24fSsrinivas pandruvada 				if (field->physical == usage_id &&
354*ca2ed12fSSrinivas Pandruvada 					(field->logical == attr_usage_id ||
355*ca2ed12fSSrinivas Pandruvada 					field->usage[0].hid ==
356*ca2ed12fSSrinivas Pandruvada 							attr_usage_id) &&
357*ca2ed12fSSrinivas Pandruvada 					(field->usage[0].collection_index >=
358*ca2ed12fSSrinivas Pandruvada 					hsdev->start_collection_index) &&
359*ca2ed12fSSrinivas Pandruvada 					(field->usage[0].collection_index <
360*ca2ed12fSSrinivas Pandruvada 					hsdev->end_collection_index)) {
361*ca2ed12fSSrinivas Pandruvada 
362*ca2ed12fSSrinivas Pandruvada 					sensor_hub_fill_attr_info(info, i,
363*ca2ed12fSSrinivas Pandruvada 								report->id,
3649f740ffaSSrinivas Pandruvada 								field);
365401ca24fSsrinivas pandruvada 					ret = 0;
366401ca24fSsrinivas pandruvada 					break;
367401ca24fSsrinivas pandruvada 				}
368401ca24fSsrinivas pandruvada 			}
369401ca24fSsrinivas pandruvada 		}
370401ca24fSsrinivas pandruvada 
371*ca2ed12fSSrinivas Pandruvada 	}
372*ca2ed12fSSrinivas Pandruvada 
373401ca24fSsrinivas pandruvada 	return ret;
374401ca24fSsrinivas pandruvada }
375401ca24fSsrinivas pandruvada EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
376401ca24fSsrinivas pandruvada 
377401ca24fSsrinivas pandruvada #ifdef CONFIG_PM
378401ca24fSsrinivas pandruvada static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
379401ca24fSsrinivas pandruvada {
380401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
381401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
382401ca24fSsrinivas pandruvada 
383401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " sensor_hub_suspend\n");
384401ca24fSsrinivas pandruvada 	spin_lock(&pdata->dyn_callback_lock);
385401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
386401ca24fSsrinivas pandruvada 		if (callback->usage_callback->suspend)
387401ca24fSsrinivas pandruvada 			callback->usage_callback->suspend(
388*ca2ed12fSSrinivas Pandruvada 					callback->hsdev, callback->priv);
389401ca24fSsrinivas pandruvada 	}
390401ca24fSsrinivas pandruvada 	spin_unlock(&pdata->dyn_callback_lock);
391401ca24fSsrinivas pandruvada 
392401ca24fSsrinivas pandruvada 	return 0;
393401ca24fSsrinivas pandruvada }
394401ca24fSsrinivas pandruvada 
395401ca24fSsrinivas pandruvada static int sensor_hub_resume(struct hid_device *hdev)
396401ca24fSsrinivas pandruvada {
397401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
398401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks_list *callback;
399401ca24fSsrinivas pandruvada 
400401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " sensor_hub_resume\n");
401401ca24fSsrinivas pandruvada 	spin_lock(&pdata->dyn_callback_lock);
402401ca24fSsrinivas pandruvada 	list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
403401ca24fSsrinivas pandruvada 		if (callback->usage_callback->resume)
404401ca24fSsrinivas pandruvada 			callback->usage_callback->resume(
405*ca2ed12fSSrinivas Pandruvada 					callback->hsdev, callback->priv);
406401ca24fSsrinivas pandruvada 	}
407401ca24fSsrinivas pandruvada 	spin_unlock(&pdata->dyn_callback_lock);
408401ca24fSsrinivas pandruvada 
409401ca24fSsrinivas pandruvada 	return 0;
410401ca24fSsrinivas pandruvada }
411401ca24fSsrinivas pandruvada 
412401ca24fSsrinivas pandruvada static int sensor_hub_reset_resume(struct hid_device *hdev)
413401ca24fSsrinivas pandruvada {
414401ca24fSsrinivas pandruvada 	return 0;
415401ca24fSsrinivas pandruvada }
416401ca24fSsrinivas pandruvada #endif
4175902fde1SAndy Shevchenko 
418401ca24fSsrinivas pandruvada /*
419401ca24fSsrinivas pandruvada  * Handle raw report as sent by device
420401ca24fSsrinivas pandruvada  */
421401ca24fSsrinivas pandruvada static int sensor_hub_raw_event(struct hid_device *hdev,
422401ca24fSsrinivas pandruvada 		struct hid_report *report, u8 *raw_data, int size)
423401ca24fSsrinivas pandruvada {
424401ca24fSsrinivas pandruvada 	int i;
425401ca24fSsrinivas pandruvada 	u8 *ptr;
426401ca24fSsrinivas pandruvada 	int sz;
427401ca24fSsrinivas pandruvada 	struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
428401ca24fSsrinivas pandruvada 	unsigned long flags;
429401ca24fSsrinivas pandruvada 	struct hid_sensor_hub_callbacks *callback = NULL;
430401ca24fSsrinivas pandruvada 	struct hid_collection *collection = NULL;
431401ca24fSsrinivas pandruvada 	void *priv = NULL;
432*ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev = NULL;
433401ca24fSsrinivas pandruvada 
434401ca24fSsrinivas pandruvada 	hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
435401ca24fSsrinivas pandruvada 			 report->id, size, report->type);
436401ca24fSsrinivas pandruvada 	hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
437401ca24fSsrinivas pandruvada 	if (report->type != HID_INPUT_REPORT)
438401ca24fSsrinivas pandruvada 		return 1;
439401ca24fSsrinivas pandruvada 
440401ca24fSsrinivas pandruvada 	ptr = raw_data;
441401ca24fSsrinivas pandruvada 	ptr++; /* Skip report id */
442401ca24fSsrinivas pandruvada 
443401ca24fSsrinivas pandruvada 	spin_lock_irqsave(&pdata->lock, flags);
444401ca24fSsrinivas pandruvada 
445401ca24fSsrinivas pandruvada 	for (i = 0; i < report->maxfield; ++i) {
446401ca24fSsrinivas pandruvada 		hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
447401ca24fSsrinivas pandruvada 				i, report->field[i]->usage->collection_index,
448401ca24fSsrinivas pandruvada 				report->field[i]->usage->hid,
449d4b1bba7SSrinivas Pandruvada 				(report->field[i]->report_size *
450d4b1bba7SSrinivas Pandruvada 					report->field[i]->report_count)/8);
451d4b1bba7SSrinivas Pandruvada 		sz = (report->field[i]->report_size *
452d4b1bba7SSrinivas Pandruvada 					report->field[i]->report_count)/8;
453401ca24fSsrinivas pandruvada 		if (pdata->pending.status && pdata->pending.attr_usage_id ==
454401ca24fSsrinivas pandruvada 				report->field[i]->usage->hid) {
455401ca24fSsrinivas pandruvada 			hid_dbg(hdev, "data was pending ...\n");
4567b0692f1SAndy Shevchenko 			pdata->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
4577b0692f1SAndy Shevchenko 			if (pdata->pending.raw_data)
458401ca24fSsrinivas pandruvada 				pdata->pending.raw_size = sz;
4597b0692f1SAndy Shevchenko 			else
460401ca24fSsrinivas pandruvada 				pdata->pending.raw_size = 0;
461401ca24fSsrinivas pandruvada 			complete(&pdata->pending.ready);
462401ca24fSsrinivas pandruvada 		}
463401ca24fSsrinivas pandruvada 		collection = &hdev->collection[
464401ca24fSsrinivas pandruvada 				report->field[i]->usage->collection_index];
465401ca24fSsrinivas pandruvada 		hid_dbg(hdev, "collection->usage %x\n",
466401ca24fSsrinivas pandruvada 					collection->usage);
467*ca2ed12fSSrinivas Pandruvada 
468*ca2ed12fSSrinivas Pandruvada 		callback = sensor_hub_get_callback(hdev,
469401ca24fSsrinivas pandruvada 				report->field[i]->physical,
470*ca2ed12fSSrinivas Pandruvada 				report->field[i]->usage[0].collection_index,
471*ca2ed12fSSrinivas Pandruvada 				&hsdev, &priv);
472*ca2ed12fSSrinivas Pandruvada 
473401ca24fSsrinivas pandruvada 		if (callback && callback->capture_sample) {
474401ca24fSsrinivas pandruvada 			if (report->field[i]->logical)
475*ca2ed12fSSrinivas Pandruvada 				callback->capture_sample(hsdev,
476401ca24fSsrinivas pandruvada 					report->field[i]->logical, sz, ptr,
477401ca24fSsrinivas pandruvada 					callback->pdev);
478401ca24fSsrinivas pandruvada 			else
479*ca2ed12fSSrinivas Pandruvada 				callback->capture_sample(hsdev,
480401ca24fSsrinivas pandruvada 					report->field[i]->usage->hid, sz, ptr,
481401ca24fSsrinivas pandruvada 					callback->pdev);
482401ca24fSsrinivas pandruvada 		}
483401ca24fSsrinivas pandruvada 		ptr += sz;
484401ca24fSsrinivas pandruvada 	}
485401ca24fSsrinivas pandruvada 	if (callback && collection && callback->send_event)
486*ca2ed12fSSrinivas Pandruvada 		callback->send_event(hsdev, collection->usage,
487401ca24fSsrinivas pandruvada 				callback->pdev);
488401ca24fSsrinivas pandruvada 	spin_unlock_irqrestore(&pdata->lock, flags);
489401ca24fSsrinivas pandruvada 
490401ca24fSsrinivas pandruvada 	return 1;
491401ca24fSsrinivas pandruvada }
492401ca24fSsrinivas pandruvada 
4931df3a401SSrinivas Pandruvada int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
4941df3a401SSrinivas Pandruvada {
4951df3a401SSrinivas Pandruvada 	int ret = 0;
4961df3a401SSrinivas Pandruvada 	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
4971df3a401SSrinivas Pandruvada 
4981df3a401SSrinivas Pandruvada 	mutex_lock(&data->mutex);
499*ca2ed12fSSrinivas Pandruvada 	if (!data->ref_cnt) {
5001df3a401SSrinivas Pandruvada 		ret = hid_hw_open(hsdev->hdev);
5011df3a401SSrinivas Pandruvada 		if (ret) {
5021df3a401SSrinivas Pandruvada 			hid_err(hsdev->hdev, "failed to open hid device\n");
5031df3a401SSrinivas Pandruvada 			mutex_unlock(&data->mutex);
5041df3a401SSrinivas Pandruvada 			return ret;
5051df3a401SSrinivas Pandruvada 		}
5061df3a401SSrinivas Pandruvada 	}
507*ca2ed12fSSrinivas Pandruvada 	data->ref_cnt++;
5081df3a401SSrinivas Pandruvada 	mutex_unlock(&data->mutex);
5091df3a401SSrinivas Pandruvada 
5101df3a401SSrinivas Pandruvada 	return ret;
5111df3a401SSrinivas Pandruvada }
5121df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_open);
5131df3a401SSrinivas Pandruvada 
5141df3a401SSrinivas Pandruvada void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
5151df3a401SSrinivas Pandruvada {
5161df3a401SSrinivas Pandruvada 	struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
5171df3a401SSrinivas Pandruvada 
5181df3a401SSrinivas Pandruvada 	mutex_lock(&data->mutex);
519*ca2ed12fSSrinivas Pandruvada 	data->ref_cnt--;
520*ca2ed12fSSrinivas Pandruvada 	if (!data->ref_cnt)
5211df3a401SSrinivas Pandruvada 		hid_hw_close(hsdev->hdev);
5221df3a401SSrinivas Pandruvada 	mutex_unlock(&data->mutex);
5231df3a401SSrinivas Pandruvada }
5241df3a401SSrinivas Pandruvada EXPORT_SYMBOL_GPL(sensor_hub_device_close);
5251df3a401SSrinivas Pandruvada 
526875e36f8SSrinivas Pandruvada static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
527875e36f8SSrinivas Pandruvada 		unsigned int *rsize)
528875e36f8SSrinivas Pandruvada {
529875e36f8SSrinivas Pandruvada 	int index;
530875e36f8SSrinivas Pandruvada 	struct sensor_hub_data *sd =  hid_get_drvdata(hdev);
531875e36f8SSrinivas Pandruvada 	unsigned char report_block[] = {
532875e36f8SSrinivas Pandruvada 				0x0a,  0x16, 0x03, 0x15, 0x00, 0x25, 0x05};
533875e36f8SSrinivas Pandruvada 	unsigned char power_block[] = {
534875e36f8SSrinivas Pandruvada 				0x0a,  0x19, 0x03, 0x15, 0x00, 0x25, 0x05};
535875e36f8SSrinivas Pandruvada 
536875e36f8SSrinivas Pandruvada 	if (!(sd->quirks & HID_SENSOR_HUB_ENUM_QUIRK)) {
537875e36f8SSrinivas Pandruvada 		hid_dbg(hdev, "No Enum quirks\n");
538875e36f8SSrinivas Pandruvada 		return rdesc;
539875e36f8SSrinivas Pandruvada 	}
540875e36f8SSrinivas Pandruvada 
541875e36f8SSrinivas Pandruvada 	/* Looks for power and report state usage id and force to 1 */
542875e36f8SSrinivas Pandruvada 	for (index = 0; index < *rsize; ++index) {
543875e36f8SSrinivas Pandruvada 		if (((*rsize - index) > sizeof(report_block)) &&
544875e36f8SSrinivas Pandruvada 			!memcmp(&rdesc[index], report_block,
545875e36f8SSrinivas Pandruvada 						sizeof(report_block))) {
546875e36f8SSrinivas Pandruvada 			rdesc[index + 4] = 0x01;
547875e36f8SSrinivas Pandruvada 			index += sizeof(report_block);
548875e36f8SSrinivas Pandruvada 		}
549875e36f8SSrinivas Pandruvada 		if (((*rsize - index) > sizeof(power_block)) &&
550875e36f8SSrinivas Pandruvada 			!memcmp(&rdesc[index], power_block,
551875e36f8SSrinivas Pandruvada 						sizeof(power_block))) {
552875e36f8SSrinivas Pandruvada 			rdesc[index + 4] = 0x01;
553875e36f8SSrinivas Pandruvada 			index += sizeof(power_block);
554875e36f8SSrinivas Pandruvada 		}
555875e36f8SSrinivas Pandruvada 	}
556875e36f8SSrinivas Pandruvada 
557875e36f8SSrinivas Pandruvada 	return rdesc;
558875e36f8SSrinivas Pandruvada }
559875e36f8SSrinivas Pandruvada 
560401ca24fSsrinivas pandruvada static int sensor_hub_probe(struct hid_device *hdev,
561401ca24fSsrinivas pandruvada 				const struct hid_device_id *id)
562401ca24fSsrinivas pandruvada {
563401ca24fSsrinivas pandruvada 	int ret;
564401ca24fSsrinivas pandruvada 	struct sensor_hub_data *sd;
565401ca24fSsrinivas pandruvada 	int i;
566401ca24fSsrinivas pandruvada 	char *name;
567401ca24fSsrinivas pandruvada 	int dev_cnt;
568*ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev;
569*ca2ed12fSSrinivas Pandruvada 	struct hid_sensor_hub_device *last_hsdev = NULL;
570401ca24fSsrinivas pandruvada 
571905cc199SAndy Shevchenko 	sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
572401ca24fSsrinivas pandruvada 	if (!sd) {
573401ca24fSsrinivas pandruvada 		hid_err(hdev, "cannot allocate Sensor data\n");
574401ca24fSsrinivas pandruvada 		return -ENOMEM;
575401ca24fSsrinivas pandruvada 	}
576*ca2ed12fSSrinivas Pandruvada 
577401ca24fSsrinivas pandruvada 	hid_set_drvdata(hdev, sd);
578875e36f8SSrinivas Pandruvada 	sd->quirks = id->driver_data;
579*ca2ed12fSSrinivas Pandruvada 
580401ca24fSsrinivas pandruvada 	spin_lock_init(&sd->lock);
581401ca24fSsrinivas pandruvada 	spin_lock_init(&sd->dyn_callback_lock);
582401ca24fSsrinivas pandruvada 	mutex_init(&sd->mutex);
583401ca24fSsrinivas pandruvada 	ret = hid_parse(hdev);
584401ca24fSsrinivas pandruvada 	if (ret) {
585401ca24fSsrinivas pandruvada 		hid_err(hdev, "parse failed\n");
586905cc199SAndy Shevchenko 		return ret;
587401ca24fSsrinivas pandruvada 	}
588401ca24fSsrinivas pandruvada 	INIT_LIST_HEAD(&hdev->inputs);
589401ca24fSsrinivas pandruvada 
590401ca24fSsrinivas pandruvada 	ret = hid_hw_start(hdev, 0);
591401ca24fSsrinivas pandruvada 	if (ret) {
592401ca24fSsrinivas pandruvada 		hid_err(hdev, "hw start failed\n");
593905cc199SAndy Shevchenko 		return ret;
594401ca24fSsrinivas pandruvada 	}
595401ca24fSsrinivas pandruvada 	INIT_LIST_HEAD(&sd->dyn_callback_list);
596401ca24fSsrinivas pandruvada 	sd->hid_sensor_client_cnt = 0;
597401ca24fSsrinivas pandruvada 
598*ca2ed12fSSrinivas Pandruvada 	dev_cnt = sensor_hub_get_physical_device_count(hdev);
599401ca24fSsrinivas pandruvada 	if (dev_cnt > HID_MAX_PHY_DEVICES) {
600401ca24fSsrinivas pandruvada 		hid_err(hdev, "Invalid Physical device count\n");
601401ca24fSsrinivas pandruvada 		ret = -EINVAL;
6021df3a401SSrinivas Pandruvada 		goto err_stop_hw;
603401ca24fSsrinivas pandruvada 	}
604401ca24fSsrinivas pandruvada 	sd->hid_sensor_hub_client_devs = kzalloc(dev_cnt *
605401ca24fSsrinivas pandruvada 						sizeof(struct mfd_cell),
606401ca24fSsrinivas pandruvada 						GFP_KERNEL);
607401ca24fSsrinivas pandruvada 	if (sd->hid_sensor_hub_client_devs == NULL) {
608f2f13a68SAxel Lin 		hid_err(hdev, "Failed to allocate memory for mfd cells\n");
609401ca24fSsrinivas pandruvada 			ret = -ENOMEM;
6101df3a401SSrinivas Pandruvada 			goto err_stop_hw;
611401ca24fSsrinivas pandruvada 	}
612*ca2ed12fSSrinivas Pandruvada 
613*ca2ed12fSSrinivas Pandruvada 	for (i = 0; i < hdev->maxcollection; ++i) {
614*ca2ed12fSSrinivas Pandruvada 		struct hid_collection *collection = &hdev->collection[i];
615*ca2ed12fSSrinivas Pandruvada 
616*ca2ed12fSSrinivas Pandruvada 		if (collection->type == HID_COLLECTION_PHYSICAL) {
617*ca2ed12fSSrinivas Pandruvada 
618*ca2ed12fSSrinivas Pandruvada 			hsdev = kzalloc(sizeof(*hsdev), GFP_KERNEL);
619*ca2ed12fSSrinivas Pandruvada 			if (!hsdev) {
620*ca2ed12fSSrinivas Pandruvada 				hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
621*ca2ed12fSSrinivas Pandruvada 				ret = -ENOMEM;
622*ca2ed12fSSrinivas Pandruvada 				goto err_no_mem;
623*ca2ed12fSSrinivas Pandruvada 			}
624*ca2ed12fSSrinivas Pandruvada 			hsdev->hdev = hdev;
625*ca2ed12fSSrinivas Pandruvada 			hsdev->vendor_id = hdev->vendor;
626*ca2ed12fSSrinivas Pandruvada 			hsdev->product_id = hdev->product;
627*ca2ed12fSSrinivas Pandruvada 			hsdev->start_collection_index = i;
628*ca2ed12fSSrinivas Pandruvada 			if (last_hsdev)
629*ca2ed12fSSrinivas Pandruvada 				last_hsdev->end_collection_index = i;
630*ca2ed12fSSrinivas Pandruvada 			last_hsdev = hsdev;
631401ca24fSsrinivas pandruvada 			name = kasprintf(GFP_KERNEL, "HID-SENSOR-%x",
632*ca2ed12fSSrinivas Pandruvada 					collection->usage);
633401ca24fSsrinivas pandruvada 			if (name == NULL) {
634f2f13a68SAxel Lin 				hid_err(hdev, "Failed MFD device name\n");
635401ca24fSsrinivas pandruvada 					ret = -ENOMEM;
636*ca2ed12fSSrinivas Pandruvada 					goto err_no_mem;
637401ca24fSsrinivas pandruvada 			}
638401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
639*ca2ed12fSSrinivas Pandruvada 				sd->hid_sensor_client_cnt].id =
640*ca2ed12fSSrinivas Pandruvada 							PLATFORM_DEVID_AUTO;
641d81cae80SSrinivas Pandruvada 			sd->hid_sensor_hub_client_devs[
642401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].name = name;
643401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
644401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].platform_data =
645*ca2ed12fSSrinivas Pandruvada 							hsdev;
646401ca24fSsrinivas pandruvada 			sd->hid_sensor_hub_client_devs[
647401ca24fSsrinivas pandruvada 				sd->hid_sensor_client_cnt].pdata_size =
648*ca2ed12fSSrinivas Pandruvada 							sizeof(*hsdev);
649*ca2ed12fSSrinivas Pandruvada 			hid_dbg(hdev, "Adding %s:%d\n", name,
650*ca2ed12fSSrinivas Pandruvada 					hsdev->start_collection_index);
651401ca24fSsrinivas pandruvada 			sd->hid_sensor_client_cnt++;
652401ca24fSsrinivas pandruvada 		}
653401ca24fSsrinivas pandruvada 	}
654*ca2ed12fSSrinivas Pandruvada 	if (last_hsdev)
655*ca2ed12fSSrinivas Pandruvada 		last_hsdev->end_collection_index = i;
656*ca2ed12fSSrinivas Pandruvada 
657401ca24fSsrinivas pandruvada 	ret = mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs,
658f45c69b1SStephen Rothwell 		sd->hid_sensor_client_cnt, NULL, 0, NULL);
659401ca24fSsrinivas pandruvada 	if (ret < 0)
660*ca2ed12fSSrinivas Pandruvada 		goto err_no_mem;
661401ca24fSsrinivas pandruvada 
662401ca24fSsrinivas pandruvada 	return ret;
663401ca24fSsrinivas pandruvada 
664*ca2ed12fSSrinivas Pandruvada err_no_mem:
665*ca2ed12fSSrinivas Pandruvada 	for (i = 0; i < sd->hid_sensor_client_cnt; ++i) {
666401ca24fSsrinivas pandruvada 		kfree(sd->hid_sensor_hub_client_devs[i].name);
667*ca2ed12fSSrinivas Pandruvada 		kfree(sd->hid_sensor_hub_client_devs[i].platform_data);
668*ca2ed12fSSrinivas Pandruvada 	}
669401ca24fSsrinivas pandruvada 	kfree(sd->hid_sensor_hub_client_devs);
670401ca24fSsrinivas pandruvada err_stop_hw:
671401ca24fSsrinivas pandruvada 	hid_hw_stop(hdev);
672401ca24fSsrinivas pandruvada 
673401ca24fSsrinivas pandruvada 	return ret;
674401ca24fSsrinivas pandruvada }
675401ca24fSsrinivas pandruvada 
676401ca24fSsrinivas pandruvada static void sensor_hub_remove(struct hid_device *hdev)
677401ca24fSsrinivas pandruvada {
678401ca24fSsrinivas pandruvada 	struct sensor_hub_data *data = hid_get_drvdata(hdev);
679401ca24fSsrinivas pandruvada 	unsigned long flags;
680401ca24fSsrinivas pandruvada 	int i;
681401ca24fSsrinivas pandruvada 
682401ca24fSsrinivas pandruvada 	hid_dbg(hdev, " hardware removed\n");
683401ca24fSsrinivas pandruvada 	hid_hw_close(hdev);
684f2f13a68SAxel Lin 	hid_hw_stop(hdev);
685401ca24fSsrinivas pandruvada 	spin_lock_irqsave(&data->lock, flags);
686401ca24fSsrinivas pandruvada 	if (data->pending.status)
687401ca24fSsrinivas pandruvada 		complete(&data->pending.ready);
688401ca24fSsrinivas pandruvada 	spin_unlock_irqrestore(&data->lock, flags);
689401ca24fSsrinivas pandruvada 	mfd_remove_devices(&hdev->dev);
690*ca2ed12fSSrinivas Pandruvada 	for (i = 0; i < data->hid_sensor_client_cnt; ++i) {
691401ca24fSsrinivas pandruvada 		kfree(data->hid_sensor_hub_client_devs[i].name);
692*ca2ed12fSSrinivas Pandruvada 		kfree(data->hid_sensor_hub_client_devs[i].platform_data);
693*ca2ed12fSSrinivas Pandruvada 	}
694401ca24fSsrinivas pandruvada 	kfree(data->hid_sensor_hub_client_devs);
695401ca24fSsrinivas pandruvada 	hid_set_drvdata(hdev, NULL);
696401ca24fSsrinivas pandruvada 	mutex_destroy(&data->mutex);
697401ca24fSsrinivas pandruvada }
698401ca24fSsrinivas pandruvada 
699401ca24fSsrinivas pandruvada static const struct hid_device_id sensor_hub_devices[] = {
700875e36f8SSrinivas Pandruvada 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_0,
701875e36f8SSrinivas Pandruvada 			USB_DEVICE_ID_INTEL_HID_SENSOR),
702875e36f8SSrinivas Pandruvada 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
703875e36f8SSrinivas Pandruvada 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
704875e36f8SSrinivas Pandruvada 			USB_DEVICE_ID_INTEL_HID_SENSOR),
705875e36f8SSrinivas Pandruvada 			.driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
7060d1e4b02SMika Westerberg 	{ HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
7070d1e4b02SMika Westerberg 		     HID_ANY_ID) },
708401ca24fSsrinivas pandruvada 	{ }
709401ca24fSsrinivas pandruvada };
710401ca24fSsrinivas pandruvada MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
711401ca24fSsrinivas pandruvada 
712401ca24fSsrinivas pandruvada static struct hid_driver sensor_hub_driver = {
713401ca24fSsrinivas pandruvada 	.name = "hid-sensor-hub",
714401ca24fSsrinivas pandruvada 	.id_table = sensor_hub_devices,
715401ca24fSsrinivas pandruvada 	.probe = sensor_hub_probe,
716401ca24fSsrinivas pandruvada 	.remove = sensor_hub_remove,
717401ca24fSsrinivas pandruvada 	.raw_event = sensor_hub_raw_event,
718875e36f8SSrinivas Pandruvada 	.report_fixup = sensor_hub_report_fixup,
719401ca24fSsrinivas pandruvada #ifdef CONFIG_PM
720401ca24fSsrinivas pandruvada 	.suspend = sensor_hub_suspend,
721401ca24fSsrinivas pandruvada 	.resume = sensor_hub_resume,
722401ca24fSsrinivas pandruvada 	.reset_resume = sensor_hub_reset_resume,
723401ca24fSsrinivas pandruvada #endif
724401ca24fSsrinivas pandruvada };
725f425458eSH Hartley Sweeten module_hid_driver(sensor_hub_driver);
726401ca24fSsrinivas pandruvada 
727401ca24fSsrinivas pandruvada MODULE_DESCRIPTION("HID Sensor Hub driver");
728401ca24fSsrinivas pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
729401ca24fSsrinivas pandruvada MODULE_LICENSE("GPL");
730