blob: 62fadf8c86fc6c826174035d1022615b7e43cf52 [file] [log] [blame]
srinivas pandruvada401ca242012-09-05 13:56:00 +01001/*
2 * HID Sensors Driver
3 * Copyright (c) 2012, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */
19#include <linux/device.h>
20#include <linux/hid.h>
srinivas pandruvada401ca242012-09-05 13:56:00 +010021#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/mfd/core.h>
24#include <linux/list.h>
25#include <linux/hid-sensor-ids.h>
26#include <linux/hid-sensor-hub.h>
27#include "hid-ids.h"
28
Srinivas Pandruvada875e36f2014-01-14 09:30:20 -080029#define HID_SENSOR_HUB_ENUM_QUIRK 0x01
30
srinivas pandruvada401ca242012-09-05 13:56:00 +010031/**
srinivas pandruvada401ca242012-09-05 13:56:00 +010032 * struct sensor_hub_data - Hold a instance data for a HID hub device
33 * @hsdev: Stored hid instance for current hub device.
34 * @mutex: Mutex to serialize synchronous request.
35 * @lock: Spin lock to protect pending request structure.
srinivas pandruvada401ca242012-09-05 13:56:00 +010036 * @dyn_callback_list: Holds callback function
37 * @dyn_callback_lock: spin lock to protect callback list
38 * @hid_sensor_hub_client_devs: Stores all MFD cells for a hub instance.
39 * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -080040 * @ref_cnt: Number of MFD clients have opened this device
srinivas pandruvada401ca242012-09-05 13:56:00 +010041 */
42struct sensor_hub_data {
srinivas pandruvada401ca242012-09-05 13:56:00 +010043 struct mutex mutex;
44 spinlock_t lock;
srinivas pandruvada401ca242012-09-05 13:56:00 +010045 struct list_head dyn_callback_list;
46 spinlock_t dyn_callback_lock;
47 struct mfd_cell *hid_sensor_hub_client_devs;
48 int hid_sensor_client_cnt;
Srinivas Pandruvada875e36f2014-01-14 09:30:20 -080049 unsigned long quirks;
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -080050 int ref_cnt;
srinivas pandruvada401ca242012-09-05 13:56:00 +010051};
52
53/**
54 * struct hid_sensor_hub_callbacks_list - Stores callback list
55 * @list: list head.
56 * @usage_id: usage id for a physical device.
57 * @usage_callback: Stores registered callback functions.
58 * @priv: Private data for a physical device.
59 */
60struct hid_sensor_hub_callbacks_list {
61 struct list_head list;
62 u32 usage_id;
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -080063 struct hid_sensor_hub_device *hsdev;
srinivas pandruvada401ca242012-09-05 13:56:00 +010064 struct hid_sensor_hub_callbacks *usage_callback;
65 void *priv;
66};
67
srinivas pandruvada401ca242012-09-05 13:56:00 +010068static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
69 int dir)
70{
71 struct hid_report *report;
72
73 list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
74 if (report->id == id)
75 return report;
76 }
77 hid_warn(hdev, "No report with id 0x%x found\n", id);
78
79 return NULL;
80}
81
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -080082static int sensor_hub_get_physical_device_count(struct hid_device *hdev)
srinivas pandruvada401ca242012-09-05 13:56:00 +010083{
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -080084 int i;
85 int count = 0;
srinivas pandruvada401ca242012-09-05 13:56:00 +010086
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -080087 for (i = 0; i < hdev->maxcollection; ++i) {
88 struct hid_collection *collection = &hdev->collection[i];
89 if (collection->type == HID_COLLECTION_PHYSICAL)
90 ++count;
srinivas pandruvada401ca242012-09-05 13:56:00 +010091 }
92
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -080093 return count;
srinivas pandruvada401ca242012-09-05 13:56:00 +010094}
95
96static void sensor_hub_fill_attr_info(
97 struct hid_sensor_hub_attribute_info *info,
Srinivas Pandruvada9f740ff2013-11-27 22:19:00 +000098 s32 index, s32 report_id, struct hid_field *field)
srinivas pandruvada401ca242012-09-05 13:56:00 +010099{
100 info->index = index;
101 info->report_id = report_id;
Srinivas Pandruvada9f740ff2013-11-27 22:19:00 +0000102 info->units = field->unit;
103 info->unit_expo = field->unit_exponent;
104 info->size = (field->report_size * field->report_count)/8;
105 info->logical_minimum = field->logical_minimum;
106 info->logical_maximum = field->logical_maximum;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100107}
108
109static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
110 struct hid_device *hdev,
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800111 u32 usage_id,
112 int collection_index,
113 struct hid_sensor_hub_device **hsdev,
114 void **priv)
srinivas pandruvada401ca242012-09-05 13:56:00 +0100115{
116 struct hid_sensor_hub_callbacks_list *callback;
117 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
118
119 spin_lock(&pdata->dyn_callback_lock);
120 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800121 if (callback->usage_id == usage_id &&
122 (collection_index >=
123 callback->hsdev->start_collection_index) &&
124 (collection_index <
125 callback->hsdev->end_collection_index)) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100126 *priv = callback->priv;
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800127 *hsdev = callback->hsdev;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100128 spin_unlock(&pdata->dyn_callback_lock);
129 return callback->usage_callback;
130 }
131 spin_unlock(&pdata->dyn_callback_lock);
132
133 return NULL;
134}
135
136int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
137 u32 usage_id,
138 struct hid_sensor_hub_callbacks *usage_callback)
139{
140 struct hid_sensor_hub_callbacks_list *callback;
141 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
Jiri Kosina0ccf0912014-06-10 11:05:43 +0200142 unsigned long flags;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100143
Jiri Kosina0ccf0912014-06-10 11:05:43 +0200144 spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100145 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800146 if (callback->usage_id == usage_id &&
147 callback->hsdev == hsdev) {
Jiri Kosina0ccf0912014-06-10 11:05:43 +0200148 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100149 return -EINVAL;
150 }
Dan Carpenter2b7c4b82012-09-14 06:53:23 +0000151 callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100152 if (!callback) {
Jiri Kosina0ccf0912014-06-10 11:05:43 +0200153 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100154 return -ENOMEM;
155 }
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800156 callback->hsdev = hsdev;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100157 callback->usage_callback = usage_callback;
158 callback->usage_id = usage_id;
159 callback->priv = NULL;
160 list_add_tail(&callback->list, &pdata->dyn_callback_list);
Jiri Kosina0ccf0912014-06-10 11:05:43 +0200161 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100162
163 return 0;
164}
165EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
166
167int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
168 u32 usage_id)
169{
170 struct hid_sensor_hub_callbacks_list *callback;
171 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
Jiri Kosina0ccf0912014-06-10 11:05:43 +0200172 unsigned long flags;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100173
Jiri Kosina0ccf0912014-06-10 11:05:43 +0200174 spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100175 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800176 if (callback->usage_id == usage_id &&
177 callback->hsdev == hsdev) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100178 list_del(&callback->list);
179 kfree(callback);
180 break;
181 }
Jiri Kosina0ccf0912014-06-10 11:05:43 +0200182 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100183
184 return 0;
185}
186EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
187
188int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
189 u32 field_index, s32 value)
190{
191 struct hid_report *report;
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300192 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100193 int ret = 0;
194
srinivas pandruvada401ca242012-09-05 13:56:00 +0100195 mutex_lock(&data->mutex);
196 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300197 if (!report || (field_index >= report->maxfield)) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100198 ret = -EINVAL;
199 goto done_proc;
200 }
201 hid_set_field(report->field[field_index], 0, value);
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100202 hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100203 hid_hw_wait(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100204
205done_proc:
206 mutex_unlock(&data->mutex);
207
208 return ret;
209}
210EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
211
212int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
213 u32 field_index, s32 *value)
214{
215 struct hid_report *report;
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300216 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100217 int ret = 0;
218
srinivas pandruvada401ca242012-09-05 13:56:00 +0100219 mutex_lock(&data->mutex);
220 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
Jiri Kosina4e5a4942013-09-06 11:59:53 +0200221 if (!report || (field_index >= report->maxfield) ||
Kees Cook9e891022013-08-28 22:31:44 +0200222 report->field[field_index]->report_count < 1) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100223 ret = -EINVAL;
224 goto done_proc;
225 }
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100226 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100227 hid_hw_wait(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100228 *value = report->field[field_index]->value[0];
229
230done_proc:
231 mutex_unlock(&data->mutex);
232
233 return ret;
234}
235EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
236
237
238int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
239 u32 usage_id,
240 u32 attr_usage_id, u32 report_id)
241{
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300242 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100243 unsigned long flags;
244 struct hid_report *report;
245 int ret_val = 0;
246
Srinivas Pandruvadae651a1d2015-02-19 15:31:25 -0800247 mutex_lock(&hsdev->mutex);
248 memset(&hsdev->pending, 0, sizeof(hsdev->pending));
249 init_completion(&hsdev->pending.ready);
250 hsdev->pending.usage_id = usage_id;
251 hsdev->pending.attr_usage_id = attr_usage_id;
252 hsdev->pending.raw_size = 0;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100253
254 spin_lock_irqsave(&data->lock, flags);
Srinivas Pandruvadae651a1d2015-02-19 15:31:25 -0800255 hsdev->pending.status = true;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100256 spin_unlock_irqrestore(&data->lock, flags);
Srinivas Pandruvadaf74346a2014-03-24 16:25:04 -0700257 report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
258 if (!report)
259 goto err_free;
260
Srinivas Pandruvadae651a1d2015-02-19 15:31:25 -0800261 mutex_lock(&data->mutex);
Srinivas Pandruvadaf74346a2014-03-24 16:25:04 -0700262 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
Srinivas Pandruvadae651a1d2015-02-19 15:31:25 -0800263 mutex_unlock(&data->mutex);
264 wait_for_completion_interruptible_timeout(&hsdev->pending.ready, HZ*5);
265 switch (hsdev->pending.raw_size) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100266 case 1:
Srinivas Pandruvadae651a1d2015-02-19 15:31:25 -0800267 ret_val = *(u8 *)hsdev->pending.raw_data;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100268 break;
269 case 2:
Srinivas Pandruvadae651a1d2015-02-19 15:31:25 -0800270 ret_val = *(u16 *)hsdev->pending.raw_data;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100271 break;
272 case 4:
Srinivas Pandruvadae651a1d2015-02-19 15:31:25 -0800273 ret_val = *(u32 *)hsdev->pending.raw_data;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100274 break;
275 default:
276 ret_val = 0;
277 }
Srinivas Pandruvadae651a1d2015-02-19 15:31:25 -0800278 kfree(hsdev->pending.raw_data);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100279
280err_free:
Srinivas Pandruvadae651a1d2015-02-19 15:31:25 -0800281 hsdev->pending.status = false;
282 mutex_unlock(&hsdev->mutex);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100283
284 return ret_val;
285}
286EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
287
Srinivas Pandruvadae02cee42014-01-23 18:50:21 -0800288int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
289 u32 report_id, int field_index, u32 usage_id)
290{
291 struct hid_report *report;
292 struct hid_field *field;
293 int i;
294
295 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
296 if (!report || (field_index >= report->maxfield))
297 goto done_proc;
298
299 field = report->field[field_index];
300 for (i = 0; i < field->maxusage; ++i) {
301 if (field->usage[i].hid == usage_id)
302 return field->usage[i].usage_index;
303 }
304
305done_proc:
306 return -EINVAL;
307}
308EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index);
309
srinivas pandruvada401ca242012-09-05 13:56:00 +0100310int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
311 u8 type,
312 u32 usage_id,
313 u32 attr_usage_id,
314 struct hid_sensor_hub_attribute_info *info)
315{
316 int ret = -1;
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800317 int i;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100318 struct hid_report *report;
319 struct hid_field *field;
320 struct hid_report_enum *report_enum;
321 struct hid_device *hdev = hsdev->hdev;
322
323 /* Initialize with defaults */
324 info->usage_id = usage_id;
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300325 info->attrib_id = attr_usage_id;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100326 info->report_id = -1;
327 info->index = -1;
328 info->units = -1;
329 info->unit_expo = -1;
330
srinivas pandruvada401ca242012-09-05 13:56:00 +0100331 report_enum = &hdev->report_enum[type];
332 list_for_each_entry(report, &report_enum->report_list, list) {
333 for (i = 0; i < report->maxfield; ++i) {
334 field = report->field[i];
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800335 if (field->maxusage) {
336 if (field->physical == usage_id &&
337 (field->logical == attr_usage_id ||
338 field->usage[0].hid ==
339 attr_usage_id) &&
340 (field->usage[0].collection_index >=
341 hsdev->start_collection_index) &&
342 (field->usage[0].collection_index <
343 hsdev->end_collection_index)) {
344
345 sensor_hub_fill_attr_info(info, i,
346 report->id,
347 field);
348 ret = 0;
349 break;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100350 }
351 }
srinivas pandruvada401ca242012-09-05 13:56:00 +0100352 }
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800353
srinivas pandruvada401ca242012-09-05 13:56:00 +0100354 }
355
srinivas pandruvada401ca242012-09-05 13:56:00 +0100356 return ret;
357}
358EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
359
360#ifdef CONFIG_PM
361static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
362{
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300363 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100364 struct hid_sensor_hub_callbacks_list *callback;
Jiri Kosina0ccf0912014-06-10 11:05:43 +0200365 unsigned long flags;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100366
367 hid_dbg(hdev, " sensor_hub_suspend\n");
Jiri Kosina0ccf0912014-06-10 11:05:43 +0200368 spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100369 list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
370 if (callback->usage_callback->suspend)
371 callback->usage_callback->suspend(
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800372 callback->hsdev, callback->priv);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100373 }
Jiri Kosina0ccf0912014-06-10 11:05:43 +0200374 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100375
376 return 0;
377}
378
379static int sensor_hub_resume(struct hid_device *hdev)
380{
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300381 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100382 struct hid_sensor_hub_callbacks_list *callback;
Jiri Kosina0ccf0912014-06-10 11:05:43 +0200383 unsigned long flags;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100384
385 hid_dbg(hdev, " sensor_hub_resume\n");
Jiri Kosina0ccf0912014-06-10 11:05:43 +0200386 spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100387 list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
388 if (callback->usage_callback->resume)
389 callback->usage_callback->resume(
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800390 callback->hsdev, callback->priv);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100391 }
Jiri Kosina0ccf0912014-06-10 11:05:43 +0200392 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100393
394 return 0;
395}
396
397static int sensor_hub_reset_resume(struct hid_device *hdev)
398{
399 return 0;
400}
401#endif
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300402
srinivas pandruvada401ca242012-09-05 13:56:00 +0100403/*
404 * Handle raw report as sent by device
405 */
406static int sensor_hub_raw_event(struct hid_device *hdev,
407 struct hid_report *report, u8 *raw_data, int size)
408{
409 int i;
410 u8 *ptr;
411 int sz;
412 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
413 unsigned long flags;
414 struct hid_sensor_hub_callbacks *callback = NULL;
415 struct hid_collection *collection = NULL;
416 void *priv = NULL;
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800417 struct hid_sensor_hub_device *hsdev = NULL;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100418
419 hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
420 report->id, size, report->type);
421 hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
422 if (report->type != HID_INPUT_REPORT)
423 return 1;
424
425 ptr = raw_data;
Andy Shevchenko15261f62013-08-14 11:07:08 +0300426 ptr++; /* Skip report id */
srinivas pandruvada401ca242012-09-05 13:56:00 +0100427
srinivas pandruvada401ca242012-09-05 13:56:00 +0100428 spin_lock_irqsave(&pdata->lock, flags);
429
430 for (i = 0; i < report->maxfield; ++i) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100431 hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
432 i, report->field[i]->usage->collection_index,
433 report->field[i]->usage->hid,
Srinivas Pandruvadad4b1bba2013-10-26 10:04:09 -0700434 (report->field[i]->report_size *
435 report->field[i]->report_count)/8);
436 sz = (report->field[i]->report_size *
437 report->field[i]->report_count)/8;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100438 collection = &hdev->collection[
439 report->field[i]->usage->collection_index];
440 hid_dbg(hdev, "collection->usage %x\n",
441 collection->usage);
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800442
443 callback = sensor_hub_get_callback(hdev,
444 report->field[i]->physical,
445 report->field[i]->usage[0].collection_index,
446 &hsdev, &priv);
Srinivas Pandruvadae651a1d2015-02-19 15:31:25 -0800447 if (!callback) {
448 ptr += sz;
449 continue;
450 }
451 if (hsdev->pending.status && hsdev->pending.attr_usage_id ==
452 report->field[i]->usage->hid) {
453 hid_dbg(hdev, "data was pending ...\n");
454 hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
455 if (hsdev->pending.raw_data)
456 hsdev->pending.raw_size = sz;
457 else
458 hsdev->pending.raw_size = 0;
459 complete(&hsdev->pending.ready);
460 }
461 if (callback->capture_sample) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100462 if (report->field[i]->logical)
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800463 callback->capture_sample(hsdev,
srinivas pandruvada401ca242012-09-05 13:56:00 +0100464 report->field[i]->logical, sz, ptr,
465 callback->pdev);
466 else
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800467 callback->capture_sample(hsdev,
srinivas pandruvada401ca242012-09-05 13:56:00 +0100468 report->field[i]->usage->hid, sz, ptr,
469 callback->pdev);
470 }
471 ptr += sz;
472 }
473 if (callback && collection && callback->send_event)
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800474 callback->send_event(hsdev, collection->usage,
srinivas pandruvada401ca242012-09-05 13:56:00 +0100475 callback->pdev);
476 spin_unlock_irqrestore(&pdata->lock, flags);
477
srinivas pandruvada401ca242012-09-05 13:56:00 +0100478 return 1;
479}
480
Srinivas Pandruvada1df3a402013-09-18 18:13:00 +0100481int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
482{
483 int ret = 0;
484 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
485
486 mutex_lock(&data->mutex);
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800487 if (!data->ref_cnt) {
Srinivas Pandruvada1df3a402013-09-18 18:13:00 +0100488 ret = hid_hw_open(hsdev->hdev);
489 if (ret) {
490 hid_err(hsdev->hdev, "failed to open hid device\n");
491 mutex_unlock(&data->mutex);
492 return ret;
493 }
494 }
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800495 data->ref_cnt++;
Srinivas Pandruvada1df3a402013-09-18 18:13:00 +0100496 mutex_unlock(&data->mutex);
497
498 return ret;
499}
500EXPORT_SYMBOL_GPL(sensor_hub_device_open);
501
502void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
503{
504 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
505
506 mutex_lock(&data->mutex);
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800507 data->ref_cnt--;
508 if (!data->ref_cnt)
Srinivas Pandruvada1df3a402013-09-18 18:13:00 +0100509 hid_hw_close(hsdev->hdev);
510 mutex_unlock(&data->mutex);
511}
512EXPORT_SYMBOL_GPL(sensor_hub_device_close);
513
Srinivas Pandruvada875e36f2014-01-14 09:30:20 -0800514static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
515 unsigned int *rsize)
516{
517 int index;
518 struct sensor_hub_data *sd = hid_get_drvdata(hdev);
519 unsigned char report_block[] = {
520 0x0a, 0x16, 0x03, 0x15, 0x00, 0x25, 0x05};
521 unsigned char power_block[] = {
522 0x0a, 0x19, 0x03, 0x15, 0x00, 0x25, 0x05};
523
524 if (!(sd->quirks & HID_SENSOR_HUB_ENUM_QUIRK)) {
525 hid_dbg(hdev, "No Enum quirks\n");
526 return rdesc;
527 }
528
529 /* Looks for power and report state usage id and force to 1 */
530 for (index = 0; index < *rsize; ++index) {
531 if (((*rsize - index) > sizeof(report_block)) &&
532 !memcmp(&rdesc[index], report_block,
533 sizeof(report_block))) {
534 rdesc[index + 4] = 0x01;
535 index += sizeof(report_block);
536 }
537 if (((*rsize - index) > sizeof(power_block)) &&
538 !memcmp(&rdesc[index], power_block,
539 sizeof(power_block))) {
540 rdesc[index + 4] = 0x01;
541 index += sizeof(power_block);
542 }
543 }
544
545 return rdesc;
546}
547
srinivas pandruvada401ca242012-09-05 13:56:00 +0100548static int sensor_hub_probe(struct hid_device *hdev,
549 const struct hid_device_id *id)
550{
551 int ret;
552 struct sensor_hub_data *sd;
553 int i;
554 char *name;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100555 int dev_cnt;
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800556 struct hid_sensor_hub_device *hsdev;
557 struct hid_sensor_hub_device *last_hsdev = NULL;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100558
Andy Shevchenko905cc192013-08-14 11:07:10 +0300559 sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100560 if (!sd) {
561 hid_err(hdev, "cannot allocate Sensor data\n");
562 return -ENOMEM;
563 }
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800564
srinivas pandruvada401ca242012-09-05 13:56:00 +0100565 hid_set_drvdata(hdev, sd);
Srinivas Pandruvada875e36f2014-01-14 09:30:20 -0800566 sd->quirks = id->driver_data;
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800567
srinivas pandruvada401ca242012-09-05 13:56:00 +0100568 spin_lock_init(&sd->lock);
569 spin_lock_init(&sd->dyn_callback_lock);
570 mutex_init(&sd->mutex);
571 ret = hid_parse(hdev);
572 if (ret) {
573 hid_err(hdev, "parse failed\n");
Andy Shevchenko905cc192013-08-14 11:07:10 +0300574 return ret;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100575 }
srinivas pandruvada401ca242012-09-05 13:56:00 +0100576 INIT_LIST_HEAD(&hdev->inputs);
577
srinivas pandruvada401ca242012-09-05 13:56:00 +0100578 ret = hid_hw_start(hdev, 0);
579 if (ret) {
580 hid_err(hdev, "hw start failed\n");
Andy Shevchenko905cc192013-08-14 11:07:10 +0300581 return ret;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100582 }
srinivas pandruvada401ca242012-09-05 13:56:00 +0100583 INIT_LIST_HEAD(&sd->dyn_callback_list);
584 sd->hid_sensor_client_cnt = 0;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100585
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800586 dev_cnt = sensor_hub_get_physical_device_count(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100587 if (dev_cnt > HID_MAX_PHY_DEVICES) {
588 hid_err(hdev, "Invalid Physical device count\n");
589 ret = -EINVAL;
Srinivas Pandruvada1df3a402013-09-18 18:13:00 +0100590 goto err_stop_hw;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100591 }
Himangi Saraogi5be5db22014-08-03 03:41:35 +0530592 sd->hid_sensor_hub_client_devs = devm_kzalloc(&hdev->dev, dev_cnt *
593 sizeof(struct mfd_cell),
594 GFP_KERNEL);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100595 if (sd->hid_sensor_hub_client_devs == NULL) {
Axel Linf2f13a62012-09-19 16:30:00 +0100596 hid_err(hdev, "Failed to allocate memory for mfd cells\n");
srinivas pandruvada401ca242012-09-05 13:56:00 +0100597 ret = -ENOMEM;
Srinivas Pandruvada1df3a402013-09-18 18:13:00 +0100598 goto err_stop_hw;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100599 }
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800600
601 for (i = 0; i < hdev->maxcollection; ++i) {
602 struct hid_collection *collection = &hdev->collection[i];
603
604 if (collection->type == HID_COLLECTION_PHYSICAL) {
605
Himangi Saraogi5be5db22014-08-03 03:41:35 +0530606 hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev),
607 GFP_KERNEL);
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800608 if (!hsdev) {
609 hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
610 ret = -ENOMEM;
Himangi Saraogi5be5db22014-08-03 03:41:35 +0530611 goto err_stop_hw;
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800612 }
613 hsdev->hdev = hdev;
614 hsdev->vendor_id = hdev->vendor;
615 hsdev->product_id = hdev->product;
Srinivas Pandruvadae651a1d2015-02-19 15:31:25 -0800616 hsdev->usage = collection->usage;
617 mutex_init(&hsdev->mutex);
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800618 hsdev->start_collection_index = i;
619 if (last_hsdev)
620 last_hsdev->end_collection_index = i;
621 last_hsdev = hsdev;
Himangi Saraogi5be5db22014-08-03 03:41:35 +0530622 name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
623 "HID-SENSOR-%x",
624 collection->usage);
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300625 if (name == NULL) {
Axel Linf2f13a62012-09-19 16:30:00 +0100626 hid_err(hdev, "Failed MFD device name\n");
srinivas pandruvada401ca242012-09-05 13:56:00 +0100627 ret = -ENOMEM;
Himangi Saraogi5be5db22014-08-03 03:41:35 +0530628 goto err_stop_hw;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100629 }
630 sd->hid_sensor_hub_client_devs[
631 sd->hid_sensor_client_cnt].name = name;
632 sd->hid_sensor_hub_client_devs[
633 sd->hid_sensor_client_cnt].platform_data =
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800634 hsdev;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100635 sd->hid_sensor_hub_client_devs[
636 sd->hid_sensor_client_cnt].pdata_size =
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800637 sizeof(*hsdev);
638 hid_dbg(hdev, "Adding %s:%d\n", name,
639 hsdev->start_collection_index);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100640 sd->hid_sensor_client_cnt++;
641 }
642 }
Srinivas Pandruvadaca2ed122014-01-31 12:04:10 -0800643 if (last_hsdev)
644 last_hsdev->end_collection_index = i;
645
Johan Hovold16b5fe22014-09-26 12:55:32 +0200646 ret = mfd_add_hotplug_devices(&hdev->dev,
647 sd->hid_sensor_hub_client_devs,
648 sd->hid_sensor_client_cnt);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100649 if (ret < 0)
Himangi Saraogi5be5db22014-08-03 03:41:35 +0530650 goto err_stop_hw;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100651
652 return ret;
653
srinivas pandruvada401ca242012-09-05 13:56:00 +0100654err_stop_hw:
655 hid_hw_stop(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100656
657 return ret;
658}
659
660static void sensor_hub_remove(struct hid_device *hdev)
661{
662 struct sensor_hub_data *data = hid_get_drvdata(hdev);
663 unsigned long flags;
Srinivas Pandruvadae651a1d2015-02-19 15:31:25 -0800664 int i;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100665
666 hid_dbg(hdev, " hardware removed\n");
srinivas pandruvada401ca242012-09-05 13:56:00 +0100667 hid_hw_close(hdev);
Axel Linf2f13a62012-09-19 16:30:00 +0100668 hid_hw_stop(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100669 spin_lock_irqsave(&data->lock, flags);
Srinivas Pandruvadae651a1d2015-02-19 15:31:25 -0800670 for (i = 0; i < data->hid_sensor_client_cnt; ++i) {
671 struct hid_sensor_hub_device *hsdev =
672 data->hid_sensor_hub_client_devs[i].platform_data;
673 if (hsdev->pending.status)
674 complete(&hsdev->pending.ready);
675 }
srinivas pandruvada401ca242012-09-05 13:56:00 +0100676 spin_unlock_irqrestore(&data->lock, flags);
677 mfd_remove_devices(&hdev->dev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100678 hid_set_drvdata(hdev, NULL);
679 mutex_destroy(&data->mutex);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100680}
681
682static const struct hid_device_id sensor_hub_devices[] = {
Srinivas Pandruvada875e36f2014-01-14 09:30:20 -0800683 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_0,
Stephen Chandler Paul30f58872014-04-03 16:27:55 -0400684 USB_DEVICE_ID_INTEL_HID_SENSOR_0),
Srinivas Pandruvada875e36f2014-01-14 09:30:20 -0800685 .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
686 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
Stephen Chandler Paul30f58872014-04-03 16:27:55 -0400687 USB_DEVICE_ID_INTEL_HID_SENSOR_0),
688 .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
689 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
690 USB_DEVICE_ID_INTEL_HID_SENSOR_1),
Srinivas Pandruvada875e36f2014-01-14 09:30:20 -0800691 .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
Reyad Attiyat00478ee2014-05-27 02:29:10 -0500692 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
693 USB_DEVICE_ID_MS_SURFACE_PRO_2),
694 .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
695 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
696 USB_DEVICE_ID_MS_TOUCH_COVER_2),
697 .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
698 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
699 USB_DEVICE_ID_MS_TYPE_COVER_2),
700 .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
Archana Patni218eb9e2014-02-03 12:44:16 +0530701 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
Srinivas Pandruvada467669c2014-09-03 16:00:54 -0700702 USB_DEVICE_ID_STM_HID_SENSOR),
703 .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
704 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
Archana Patnidde3b452014-06-02 07:40:49 -0400705 USB_DEVICE_ID_STM_HID_SENSOR_1),
Archana Patni218eb9e2014-02-03 12:44:16 +0530706 .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
Peter F. Patel-Schneider825747b2014-05-05 15:55:32 -0700707 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_TEXAS_INSTRUMENTS,
708 USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA),
709 .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
Mika Westerberg0d1e4b02013-02-11 12:31:19 +0200710 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
711 HID_ANY_ID) },
srinivas pandruvada401ca242012-09-05 13:56:00 +0100712 { }
713};
714MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
715
srinivas pandruvada401ca242012-09-05 13:56:00 +0100716static struct hid_driver sensor_hub_driver = {
717 .name = "hid-sensor-hub",
718 .id_table = sensor_hub_devices,
719 .probe = sensor_hub_probe,
720 .remove = sensor_hub_remove,
721 .raw_event = sensor_hub_raw_event,
Srinivas Pandruvada875e36f2014-01-14 09:30:20 -0800722 .report_fixup = sensor_hub_report_fixup,
srinivas pandruvada401ca242012-09-05 13:56:00 +0100723#ifdef CONFIG_PM
724 .suspend = sensor_hub_suspend,
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300725 .resume = sensor_hub_resume,
726 .reset_resume = sensor_hub_reset_resume,
srinivas pandruvada401ca242012-09-05 13:56:00 +0100727#endif
728};
H Hartley Sweetenf4254582012-12-17 15:28:26 -0700729module_hid_driver(sensor_hub_driver);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100730
731MODULE_DESCRIPTION("HID Sensor Hub driver");
732MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
733MODULE_LICENSE("GPL");