blob: 9021c9ce885157b89ba0e82ad4aabff27c82b4be [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/**
32 * struct sensor_hub_pending - Synchronous read pending information
33 * @status: Pending status true/false.
34 * @ready: Completion synchronization data.
35 * @usage_id: Usage id for physical device, E.g. Gyro usage id.
36 * @attr_usage_id: Usage Id of a field, E.g. X-AXIS for a gyro.
37 * @raw_size: Response size for a read request.
38 * @raw_data: Place holder for received response.
39 */
40struct sensor_hub_pending {
41 bool status;
42 struct completion ready;
43 u32 usage_id;
44 u32 attr_usage_id;
45 int raw_size;
46 u8 *raw_data;
47};
48
49/**
50 * struct sensor_hub_data - Hold a instance data for a HID hub device
51 * @hsdev: Stored hid instance for current hub device.
52 * @mutex: Mutex to serialize synchronous request.
53 * @lock: Spin lock to protect pending request structure.
54 * @pending: Holds information of pending sync read request.
55 * @dyn_callback_list: Holds callback function
56 * @dyn_callback_lock: spin lock to protect callback list
57 * @hid_sensor_hub_client_devs: Stores all MFD cells for a hub instance.
58 * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
59 */
60struct sensor_hub_data {
61 struct hid_sensor_hub_device *hsdev;
62 struct mutex mutex;
63 spinlock_t lock;
64 struct sensor_hub_pending pending;
65 struct list_head dyn_callback_list;
66 spinlock_t dyn_callback_lock;
67 struct mfd_cell *hid_sensor_hub_client_devs;
68 int hid_sensor_client_cnt;
Srinivas Pandruvada875e36f2014-01-14 09:30:20 -080069 unsigned long quirks;
srinivas pandruvada401ca242012-09-05 13:56:00 +010070};
71
72/**
73 * struct hid_sensor_hub_callbacks_list - Stores callback list
74 * @list: list head.
75 * @usage_id: usage id for a physical device.
76 * @usage_callback: Stores registered callback functions.
77 * @priv: Private data for a physical device.
78 */
79struct hid_sensor_hub_callbacks_list {
80 struct list_head list;
81 u32 usage_id;
82 struct hid_sensor_hub_callbacks *usage_callback;
83 void *priv;
84};
85
srinivas pandruvada401ca242012-09-05 13:56:00 +010086static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
87 int dir)
88{
89 struct hid_report *report;
90
91 list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
92 if (report->id == id)
93 return report;
94 }
95 hid_warn(hdev, "No report with id 0x%x found\n", id);
96
97 return NULL;
98}
99
100static int sensor_hub_get_physical_device_count(
101 struct hid_report_enum *report_enum)
102{
103 struct hid_report *report;
104 struct hid_field *field;
105 int cnt = 0;
106
107 list_for_each_entry(report, &report_enum->report_list, list) {
108 field = report->field[0];
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300109 if (report->maxfield && field && field->physical)
srinivas pandruvada401ca242012-09-05 13:56:00 +0100110 cnt++;
111 }
112
113 return cnt;
114}
115
116static void sensor_hub_fill_attr_info(
117 struct hid_sensor_hub_attribute_info *info,
Srinivas Pandruvada9f740ff2013-11-27 22:19:00 +0000118 s32 index, s32 report_id, struct hid_field *field)
srinivas pandruvada401ca242012-09-05 13:56:00 +0100119{
120 info->index = index;
121 info->report_id = report_id;
Srinivas Pandruvada9f740ff2013-11-27 22:19:00 +0000122 info->units = field->unit;
123 info->unit_expo = field->unit_exponent;
124 info->size = (field->report_size * field->report_count)/8;
125 info->logical_minimum = field->logical_minimum;
126 info->logical_maximum = field->logical_maximum;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100127}
128
129static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
130 struct hid_device *hdev,
131 u32 usage_id, void **priv)
132{
133 struct hid_sensor_hub_callbacks_list *callback;
134 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
135
136 spin_lock(&pdata->dyn_callback_lock);
137 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
138 if (callback->usage_id == usage_id) {
139 *priv = callback->priv;
140 spin_unlock(&pdata->dyn_callback_lock);
141 return callback->usage_callback;
142 }
143 spin_unlock(&pdata->dyn_callback_lock);
144
145 return NULL;
146}
147
148int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
149 u32 usage_id,
150 struct hid_sensor_hub_callbacks *usage_callback)
151{
152 struct hid_sensor_hub_callbacks_list *callback;
153 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
154
155 spin_lock(&pdata->dyn_callback_lock);
156 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
157 if (callback->usage_id == usage_id) {
158 spin_unlock(&pdata->dyn_callback_lock);
159 return -EINVAL;
160 }
Dan Carpenter2b7c4b82012-09-14 06:53:23 +0000161 callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100162 if (!callback) {
163 spin_unlock(&pdata->dyn_callback_lock);
164 return -ENOMEM;
165 }
166 callback->usage_callback = usage_callback;
167 callback->usage_id = usage_id;
168 callback->priv = NULL;
169 list_add_tail(&callback->list, &pdata->dyn_callback_list);
170 spin_unlock(&pdata->dyn_callback_lock);
171
172 return 0;
173}
174EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
175
176int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
177 u32 usage_id)
178{
179 struct hid_sensor_hub_callbacks_list *callback;
180 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
181
182 spin_lock(&pdata->dyn_callback_lock);
183 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
184 if (callback->usage_id == usage_id) {
185 list_del(&callback->list);
186 kfree(callback);
187 break;
188 }
189 spin_unlock(&pdata->dyn_callback_lock);
190
191 return 0;
192}
193EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
194
195int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
196 u32 field_index, s32 value)
197{
198 struct hid_report *report;
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300199 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100200 int ret = 0;
201
srinivas pandruvada401ca242012-09-05 13:56:00 +0100202 mutex_lock(&data->mutex);
203 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300204 if (!report || (field_index >= report->maxfield)) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100205 ret = -EINVAL;
206 goto done_proc;
207 }
208 hid_set_field(report->field[field_index], 0, value);
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100209 hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100210 hid_hw_wait(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100211
212done_proc:
213 mutex_unlock(&data->mutex);
214
215 return ret;
216}
217EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
218
219int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
220 u32 field_index, s32 *value)
221{
222 struct hid_report *report;
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300223 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100224 int ret = 0;
225
srinivas pandruvada401ca242012-09-05 13:56:00 +0100226 mutex_lock(&data->mutex);
227 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
Jiri Kosina4e5a4942013-09-06 11:59:53 +0200228 if (!report || (field_index >= report->maxfield) ||
Kees Cook9e891022013-08-28 22:31:44 +0200229 report->field[field_index]->report_count < 1) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100230 ret = -EINVAL;
231 goto done_proc;
232 }
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100233 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100234 hid_hw_wait(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100235 *value = report->field[field_index]->value[0];
236
237done_proc:
238 mutex_unlock(&data->mutex);
239
240 return ret;
241}
242EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
243
244
245int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
246 u32 usage_id,
247 u32 attr_usage_id, u32 report_id)
248{
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300249 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100250 unsigned long flags;
251 struct hid_report *report;
252 int ret_val = 0;
253
srinivas pandruvada401ca242012-09-05 13:56:00 +0100254 mutex_lock(&data->mutex);
255 memset(&data->pending, 0, sizeof(data->pending));
256 init_completion(&data->pending.ready);
257 data->pending.usage_id = usage_id;
258 data->pending.attr_usage_id = attr_usage_id;
259 data->pending.raw_size = 0;
260
261 spin_lock_irqsave(&data->lock, flags);
262 data->pending.status = true;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100263 spin_unlock_irqrestore(&data->lock, flags);
Srinivas Pandruvadaf74346a2014-03-24 16:25:04 -0700264 report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
265 if (!report)
266 goto err_free;
267
268 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100269 wait_for_completion_interruptible_timeout(&data->pending.ready, HZ*5);
270 switch (data->pending.raw_size) {
271 case 1:
272 ret_val = *(u8 *)data->pending.raw_data;
273 break;
274 case 2:
275 ret_val = *(u16 *)data->pending.raw_data;
276 break;
277 case 4:
278 ret_val = *(u32 *)data->pending.raw_data;
279 break;
280 default:
281 ret_val = 0;
282 }
283 kfree(data->pending.raw_data);
284
285err_free:
286 data->pending.status = false;
287 mutex_unlock(&data->mutex);
288
289 return ret_val;
290}
291EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
292
293int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
294 u8 type,
295 u32 usage_id,
296 u32 attr_usage_id,
297 struct hid_sensor_hub_attribute_info *info)
298{
299 int ret = -1;
300 int i, j;
301 int collection_index = -1;
302 struct hid_report *report;
303 struct hid_field *field;
304 struct hid_report_enum *report_enum;
305 struct hid_device *hdev = hsdev->hdev;
306
307 /* Initialize with defaults */
308 info->usage_id = usage_id;
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300309 info->attrib_id = attr_usage_id;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100310 info->report_id = -1;
311 info->index = -1;
312 info->units = -1;
313 info->unit_expo = -1;
314
315 for (i = 0; i < hdev->maxcollection; ++i) {
316 struct hid_collection *collection = &hdev->collection[i];
317 if (usage_id == collection->usage) {
318 collection_index = i;
319 break;
320 }
321 }
322 if (collection_index == -1)
323 goto err_ret;
324
325 report_enum = &hdev->report_enum[type];
326 list_for_each_entry(report, &report_enum->report_list, list) {
327 for (i = 0; i < report->maxfield; ++i) {
328 field = report->field[i];
329 if (field->physical == usage_id &&
330 field->logical == attr_usage_id) {
331 sensor_hub_fill_attr_info(info, i, report->id,
Srinivas Pandruvada9f740ff2013-11-27 22:19:00 +0000332 field);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100333 ret = 0;
334 } else {
335 for (j = 0; j < field->maxusage; ++j) {
336 if (field->usage[j].hid ==
337 attr_usage_id &&
338 field->usage[j].collection_index ==
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300339 collection_index) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100340 sensor_hub_fill_attr_info(info,
Srinivas Pandruvada9f740ff2013-11-27 22:19:00 +0000341 i, report->id, field);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100342 ret = 0;
343 break;
344 }
345 }
346 }
347 if (ret == 0)
348 break;
349 }
350 }
351
352err_ret:
353 return ret;
354}
355EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
356
357#ifdef CONFIG_PM
358static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
359{
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300360 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100361 struct hid_sensor_hub_callbacks_list *callback;
362
363 hid_dbg(hdev, " sensor_hub_suspend\n");
364 spin_lock(&pdata->dyn_callback_lock);
365 list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
366 if (callback->usage_callback->suspend)
367 callback->usage_callback->suspend(
368 pdata->hsdev, callback->priv);
369 }
370 spin_unlock(&pdata->dyn_callback_lock);
371
372 return 0;
373}
374
375static int sensor_hub_resume(struct hid_device *hdev)
376{
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300377 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100378 struct hid_sensor_hub_callbacks_list *callback;
379
380 hid_dbg(hdev, " sensor_hub_resume\n");
381 spin_lock(&pdata->dyn_callback_lock);
382 list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
383 if (callback->usage_callback->resume)
384 callback->usage_callback->resume(
385 pdata->hsdev, callback->priv);
386 }
387 spin_unlock(&pdata->dyn_callback_lock);
388
389 return 0;
390}
391
392static int sensor_hub_reset_resume(struct hid_device *hdev)
393{
394 return 0;
395}
396#endif
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300397
srinivas pandruvada401ca242012-09-05 13:56:00 +0100398/*
399 * Handle raw report as sent by device
400 */
401static int sensor_hub_raw_event(struct hid_device *hdev,
402 struct hid_report *report, u8 *raw_data, int size)
403{
404 int i;
405 u8 *ptr;
406 int sz;
407 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
408 unsigned long flags;
409 struct hid_sensor_hub_callbacks *callback = NULL;
410 struct hid_collection *collection = NULL;
411 void *priv = NULL;
412
413 hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
414 report->id, size, report->type);
415 hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
416 if (report->type != HID_INPUT_REPORT)
417 return 1;
418
419 ptr = raw_data;
Andy Shevchenko15261f62013-08-14 11:07:08 +0300420 ptr++; /* Skip report id */
srinivas pandruvada401ca242012-09-05 13:56:00 +0100421
srinivas pandruvada401ca242012-09-05 13:56:00 +0100422 spin_lock_irqsave(&pdata->lock, flags);
423
424 for (i = 0; i < report->maxfield; ++i) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100425 hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
426 i, report->field[i]->usage->collection_index,
427 report->field[i]->usage->hid,
Srinivas Pandruvadad4b1bba2013-10-26 10:04:09 -0700428 (report->field[i]->report_size *
429 report->field[i]->report_count)/8);
430 sz = (report->field[i]->report_size *
431 report->field[i]->report_count)/8;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100432 if (pdata->pending.status && pdata->pending.attr_usage_id ==
433 report->field[i]->usage->hid) {
434 hid_dbg(hdev, "data was pending ...\n");
Andy Shevchenko7b0692f2013-08-14 11:07:11 +0300435 pdata->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
436 if (pdata->pending.raw_data)
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300437 pdata->pending.raw_size = sz;
Andy Shevchenko7b0692f2013-08-14 11:07:11 +0300438 else
srinivas pandruvada401ca242012-09-05 13:56:00 +0100439 pdata->pending.raw_size = 0;
440 complete(&pdata->pending.ready);
441 }
442 collection = &hdev->collection[
443 report->field[i]->usage->collection_index];
444 hid_dbg(hdev, "collection->usage %x\n",
445 collection->usage);
446 callback = sensor_hub_get_callback(pdata->hsdev->hdev,
447 report->field[i]->physical,
448 &priv);
449 if (callback && callback->capture_sample) {
450 if (report->field[i]->logical)
451 callback->capture_sample(pdata->hsdev,
452 report->field[i]->logical, sz, ptr,
453 callback->pdev);
454 else
455 callback->capture_sample(pdata->hsdev,
456 report->field[i]->usage->hid, sz, ptr,
457 callback->pdev);
458 }
459 ptr += sz;
460 }
461 if (callback && collection && callback->send_event)
462 callback->send_event(pdata->hsdev, collection->usage,
463 callback->pdev);
464 spin_unlock_irqrestore(&pdata->lock, flags);
465
srinivas pandruvada401ca242012-09-05 13:56:00 +0100466 return 1;
467}
468
Srinivas Pandruvada1df3a402013-09-18 18:13:00 +0100469int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
470{
471 int ret = 0;
472 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
473
474 mutex_lock(&data->mutex);
475 if (!hsdev->ref_cnt) {
476 ret = hid_hw_open(hsdev->hdev);
477 if (ret) {
478 hid_err(hsdev->hdev, "failed to open hid device\n");
479 mutex_unlock(&data->mutex);
480 return ret;
481 }
482 }
483 hsdev->ref_cnt++;
484 mutex_unlock(&data->mutex);
485
486 return ret;
487}
488EXPORT_SYMBOL_GPL(sensor_hub_device_open);
489
490void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
491{
492 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
493
494 mutex_lock(&data->mutex);
495 hsdev->ref_cnt--;
496 if (!hsdev->ref_cnt)
497 hid_hw_close(hsdev->hdev);
498 mutex_unlock(&data->mutex);
499}
500EXPORT_SYMBOL_GPL(sensor_hub_device_close);
501
Srinivas Pandruvada875e36f2014-01-14 09:30:20 -0800502static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
503 unsigned int *rsize)
504{
505 int index;
506 struct sensor_hub_data *sd = hid_get_drvdata(hdev);
507 unsigned char report_block[] = {
508 0x0a, 0x16, 0x03, 0x15, 0x00, 0x25, 0x05};
509 unsigned char power_block[] = {
510 0x0a, 0x19, 0x03, 0x15, 0x00, 0x25, 0x05};
511
512 if (!(sd->quirks & HID_SENSOR_HUB_ENUM_QUIRK)) {
513 hid_dbg(hdev, "No Enum quirks\n");
514 return rdesc;
515 }
516
517 /* Looks for power and report state usage id and force to 1 */
518 for (index = 0; index < *rsize; ++index) {
519 if (((*rsize - index) > sizeof(report_block)) &&
520 !memcmp(&rdesc[index], report_block,
521 sizeof(report_block))) {
522 rdesc[index + 4] = 0x01;
523 index += sizeof(report_block);
524 }
525 if (((*rsize - index) > sizeof(power_block)) &&
526 !memcmp(&rdesc[index], power_block,
527 sizeof(power_block))) {
528 rdesc[index + 4] = 0x01;
529 index += sizeof(power_block);
530 }
531 }
532
533 return rdesc;
534}
535
srinivas pandruvada401ca242012-09-05 13:56:00 +0100536static int sensor_hub_probe(struct hid_device *hdev,
537 const struct hid_device_id *id)
538{
539 int ret;
540 struct sensor_hub_data *sd;
541 int i;
542 char *name;
543 struct hid_report *report;
544 struct hid_report_enum *report_enum;
545 struct hid_field *field;
546 int dev_cnt;
547
Andy Shevchenko905cc192013-08-14 11:07:10 +0300548 sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100549 if (!sd) {
550 hid_err(hdev, "cannot allocate Sensor data\n");
551 return -ENOMEM;
552 }
Andy Shevchenko905cc192013-08-14 11:07:10 +0300553 sd->hsdev = devm_kzalloc(&hdev->dev, sizeof(*sd->hsdev), GFP_KERNEL);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100554 if (!sd->hsdev) {
555 hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
Andy Shevchenko905cc192013-08-14 11:07:10 +0300556 return -ENOMEM;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100557 }
558 hid_set_drvdata(hdev, sd);
Srinivas Pandruvada875e36f2014-01-14 09:30:20 -0800559 sd->quirks = id->driver_data;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100560 sd->hsdev->hdev = hdev;
561 sd->hsdev->vendor_id = hdev->vendor;
562 sd->hsdev->product_id = hdev->product;
563 spin_lock_init(&sd->lock);
564 spin_lock_init(&sd->dyn_callback_lock);
565 mutex_init(&sd->mutex);
566 ret = hid_parse(hdev);
567 if (ret) {
568 hid_err(hdev, "parse failed\n");
Andy Shevchenko905cc192013-08-14 11:07:10 +0300569 return ret;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100570 }
srinivas pandruvada401ca242012-09-05 13:56:00 +0100571 INIT_LIST_HEAD(&hdev->inputs);
572
srinivas pandruvada401ca242012-09-05 13:56:00 +0100573 ret = hid_hw_start(hdev, 0);
574 if (ret) {
575 hid_err(hdev, "hw start failed\n");
Andy Shevchenko905cc192013-08-14 11:07:10 +0300576 return ret;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100577 }
srinivas pandruvada401ca242012-09-05 13:56:00 +0100578 INIT_LIST_HEAD(&sd->dyn_callback_list);
579 sd->hid_sensor_client_cnt = 0;
580 report_enum = &hdev->report_enum[HID_INPUT_REPORT];
581
582 dev_cnt = sensor_hub_get_physical_device_count(report_enum);
583 if (dev_cnt > HID_MAX_PHY_DEVICES) {
584 hid_err(hdev, "Invalid Physical device count\n");
585 ret = -EINVAL;
Srinivas Pandruvada1df3a402013-09-18 18:13:00 +0100586 goto err_stop_hw;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100587 }
588 sd->hid_sensor_hub_client_devs = kzalloc(dev_cnt *
589 sizeof(struct mfd_cell),
590 GFP_KERNEL);
591 if (sd->hid_sensor_hub_client_devs == NULL) {
Axel Linf2f13a62012-09-19 16:30:00 +0100592 hid_err(hdev, "Failed to allocate memory for mfd cells\n");
srinivas pandruvada401ca242012-09-05 13:56:00 +0100593 ret = -ENOMEM;
Srinivas Pandruvada1df3a402013-09-18 18:13:00 +0100594 goto err_stop_hw;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100595 }
596 list_for_each_entry(report, &report_enum->report_list, list) {
597 hid_dbg(hdev, "Report id:%x\n", report->id);
598 field = report->field[0];
599 if (report->maxfield && field &&
600 field->physical) {
601 name = kasprintf(GFP_KERNEL, "HID-SENSOR-%x",
602 field->physical);
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300603 if (name == NULL) {
Axel Linf2f13a62012-09-19 16:30:00 +0100604 hid_err(hdev, "Failed MFD device name\n");
srinivas pandruvada401ca242012-09-05 13:56:00 +0100605 ret = -ENOMEM;
Axel Linf2f13a62012-09-19 16:30:00 +0100606 goto err_free_names;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100607 }
608 sd->hid_sensor_hub_client_devs[
Srinivas Pandruvadad81cae82013-12-05 16:34:25 -0800609 sd->hid_sensor_client_cnt].id = PLATFORM_DEVID_AUTO;
610 sd->hid_sensor_hub_client_devs[
srinivas pandruvada401ca242012-09-05 13:56:00 +0100611 sd->hid_sensor_client_cnt].name = name;
612 sd->hid_sensor_hub_client_devs[
613 sd->hid_sensor_client_cnt].platform_data =
614 sd->hsdev;
615 sd->hid_sensor_hub_client_devs[
616 sd->hid_sensor_client_cnt].pdata_size =
617 sizeof(*sd->hsdev);
618 hid_dbg(hdev, "Adding %s:%p\n", name, sd);
619 sd->hid_sensor_client_cnt++;
620 }
621 }
622 ret = mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs,
Stephen Rothwellf45c69b2012-09-12 07:07:00 +0100623 sd->hid_sensor_client_cnt, NULL, 0, NULL);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100624 if (ret < 0)
625 goto err_free_names;
626
627 return ret;
628
629err_free_names:
630 for (i = 0; i < sd->hid_sensor_client_cnt ; ++i)
631 kfree(sd->hid_sensor_hub_client_devs[i].name);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100632 kfree(sd->hid_sensor_hub_client_devs);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100633err_stop_hw:
634 hid_hw_stop(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100635
636 return ret;
637}
638
639static void sensor_hub_remove(struct hid_device *hdev)
640{
641 struct sensor_hub_data *data = hid_get_drvdata(hdev);
642 unsigned long flags;
643 int i;
644
645 hid_dbg(hdev, " hardware removed\n");
srinivas pandruvada401ca242012-09-05 13:56:00 +0100646 hid_hw_close(hdev);
Axel Linf2f13a62012-09-19 16:30:00 +0100647 hid_hw_stop(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100648 spin_lock_irqsave(&data->lock, flags);
649 if (data->pending.status)
650 complete(&data->pending.ready);
651 spin_unlock_irqrestore(&data->lock, flags);
652 mfd_remove_devices(&hdev->dev);
653 for (i = 0; i < data->hid_sensor_client_cnt ; ++i)
654 kfree(data->hid_sensor_hub_client_devs[i].name);
655 kfree(data->hid_sensor_hub_client_devs);
656 hid_set_drvdata(hdev, NULL);
657 mutex_destroy(&data->mutex);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100658}
659
660static const struct hid_device_id sensor_hub_devices[] = {
Srinivas Pandruvada875e36f2014-01-14 09:30:20 -0800661 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_0,
662 USB_DEVICE_ID_INTEL_HID_SENSOR),
663 .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
664 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
665 USB_DEVICE_ID_INTEL_HID_SENSOR),
666 .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
Archana Patni218eb9e2014-02-03 12:44:16 +0530667 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
668 USB_DEVICE_ID_STM_HID_SENSOR),
669 .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
Mika Westerberg0d1e4b02013-02-11 12:31:19 +0200670 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
671 HID_ANY_ID) },
srinivas pandruvada401ca242012-09-05 13:56:00 +0100672 { }
673};
674MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
675
srinivas pandruvada401ca242012-09-05 13:56:00 +0100676static struct hid_driver sensor_hub_driver = {
677 .name = "hid-sensor-hub",
678 .id_table = sensor_hub_devices,
679 .probe = sensor_hub_probe,
680 .remove = sensor_hub_remove,
681 .raw_event = sensor_hub_raw_event,
Srinivas Pandruvada875e36f2014-01-14 09:30:20 -0800682 .report_fixup = sensor_hub_report_fixup,
srinivas pandruvada401ca242012-09-05 13:56:00 +0100683#ifdef CONFIG_PM
684 .suspend = sensor_hub_suspend,
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300685 .resume = sensor_hub_resume,
686 .reset_resume = sensor_hub_reset_resume,
srinivas pandruvada401ca242012-09-05 13:56:00 +0100687#endif
688};
H Hartley Sweetenf4254582012-12-17 15:28:26 -0700689module_hid_driver(sensor_hub_driver);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100690
691MODULE_DESCRIPTION("HID Sensor Hub driver");
692MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
693MODULE_LICENSE("GPL");