blob: a184e1921c11190ffce1033e5b7d4f512d55f798 [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
29/**
30 * struct sensor_hub_pending - Synchronous read pending information
31 * @status: Pending status true/false.
32 * @ready: Completion synchronization data.
33 * @usage_id: Usage id for physical device, E.g. Gyro usage id.
34 * @attr_usage_id: Usage Id of a field, E.g. X-AXIS for a gyro.
35 * @raw_size: Response size for a read request.
36 * @raw_data: Place holder for received response.
37 */
38struct sensor_hub_pending {
39 bool status;
40 struct completion ready;
41 u32 usage_id;
42 u32 attr_usage_id;
43 int raw_size;
44 u8 *raw_data;
45};
46
47/**
48 * struct sensor_hub_data - Hold a instance data for a HID hub device
49 * @hsdev: Stored hid instance for current hub device.
50 * @mutex: Mutex to serialize synchronous request.
51 * @lock: Spin lock to protect pending request structure.
52 * @pending: Holds information of pending sync read request.
53 * @dyn_callback_list: Holds callback function
54 * @dyn_callback_lock: spin lock to protect callback list
55 * @hid_sensor_hub_client_devs: Stores all MFD cells for a hub instance.
56 * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
57 */
58struct sensor_hub_data {
59 struct hid_sensor_hub_device *hsdev;
60 struct mutex mutex;
61 spinlock_t lock;
62 struct sensor_hub_pending pending;
63 struct list_head dyn_callback_list;
64 spinlock_t dyn_callback_lock;
65 struct mfd_cell *hid_sensor_hub_client_devs;
66 int hid_sensor_client_cnt;
67};
68
69/**
70 * struct hid_sensor_hub_callbacks_list - Stores callback list
71 * @list: list head.
72 * @usage_id: usage id for a physical device.
73 * @usage_callback: Stores registered callback functions.
74 * @priv: Private data for a physical device.
75 */
76struct hid_sensor_hub_callbacks_list {
77 struct list_head list;
78 u32 usage_id;
79 struct hid_sensor_hub_callbacks *usage_callback;
80 void *priv;
81};
82
srinivas pandruvada401ca242012-09-05 13:56:00 +010083static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
84 int dir)
85{
86 struct hid_report *report;
87
88 list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
89 if (report->id == id)
90 return report;
91 }
92 hid_warn(hdev, "No report with id 0x%x found\n", id);
93
94 return NULL;
95}
96
97static int sensor_hub_get_physical_device_count(
98 struct hid_report_enum *report_enum)
99{
100 struct hid_report *report;
101 struct hid_field *field;
102 int cnt = 0;
103
104 list_for_each_entry(report, &report_enum->report_list, list) {
105 field = report->field[0];
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300106 if (report->maxfield && field && field->physical)
srinivas pandruvada401ca242012-09-05 13:56:00 +0100107 cnt++;
108 }
109
110 return cnt;
111}
112
113static void sensor_hub_fill_attr_info(
114 struct hid_sensor_hub_attribute_info *info,
115 s32 index, s32 report_id, s32 units, s32 unit_expo, s32 size)
116{
117 info->index = index;
118 info->report_id = report_id;
119 info->units = units;
120 info->unit_expo = unit_expo;
121 info->size = size/8;
122}
123
124static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
125 struct hid_device *hdev,
126 u32 usage_id, void **priv)
127{
128 struct hid_sensor_hub_callbacks_list *callback;
129 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
130
131 spin_lock(&pdata->dyn_callback_lock);
132 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
133 if (callback->usage_id == usage_id) {
134 *priv = callback->priv;
135 spin_unlock(&pdata->dyn_callback_lock);
136 return callback->usage_callback;
137 }
138 spin_unlock(&pdata->dyn_callback_lock);
139
140 return NULL;
141}
142
143int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
144 u32 usage_id,
145 struct hid_sensor_hub_callbacks *usage_callback)
146{
147 struct hid_sensor_hub_callbacks_list *callback;
148 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
149
150 spin_lock(&pdata->dyn_callback_lock);
151 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
152 if (callback->usage_id == usage_id) {
153 spin_unlock(&pdata->dyn_callback_lock);
154 return -EINVAL;
155 }
Dan Carpenter2b7c4b82012-09-14 06:53:23 +0000156 callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100157 if (!callback) {
158 spin_unlock(&pdata->dyn_callback_lock);
159 return -ENOMEM;
160 }
161 callback->usage_callback = usage_callback;
162 callback->usage_id = usage_id;
163 callback->priv = NULL;
164 list_add_tail(&callback->list, &pdata->dyn_callback_list);
165 spin_unlock(&pdata->dyn_callback_lock);
166
167 return 0;
168}
169EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
170
171int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
172 u32 usage_id)
173{
174 struct hid_sensor_hub_callbacks_list *callback;
175 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
176
177 spin_lock(&pdata->dyn_callback_lock);
178 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
179 if (callback->usage_id == usage_id) {
180 list_del(&callback->list);
181 kfree(callback);
182 break;
183 }
184 spin_unlock(&pdata->dyn_callback_lock);
185
186 return 0;
187}
188EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
189
190int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
191 u32 field_index, s32 value)
192{
193 struct hid_report *report;
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300194 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100195 int ret = 0;
196
srinivas pandruvada401ca242012-09-05 13:56:00 +0100197 mutex_lock(&data->mutex);
198 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300199 if (!report || (field_index >= report->maxfield)) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100200 ret = -EINVAL;
201 goto done_proc;
202 }
203 hid_set_field(report->field[field_index], 0, value);
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100204 hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100205 hid_hw_wait(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100206
207done_proc:
208 mutex_unlock(&data->mutex);
209
210 return ret;
211}
212EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
213
214int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
215 u32 field_index, s32 *value)
216{
217 struct hid_report *report;
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300218 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100219 int ret = 0;
220
srinivas pandruvada401ca242012-09-05 13:56:00 +0100221 mutex_lock(&data->mutex);
222 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
Jiri Kosina4e5a4942013-09-06 11:59:53 +0200223 if (!report || (field_index >= report->maxfield) ||
Kees Cook9e891022013-08-28 22:31:44 +0200224 report->field[field_index]->report_count < 1) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100225 ret = -EINVAL;
226 goto done_proc;
227 }
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100228 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100229 hid_hw_wait(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100230 *value = report->field[field_index]->value[0];
231
232done_proc:
233 mutex_unlock(&data->mutex);
234
235 return ret;
236}
237EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
238
239
240int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
241 u32 usage_id,
242 u32 attr_usage_id, u32 report_id)
243{
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300244 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100245 unsigned long flags;
246 struct hid_report *report;
247 int ret_val = 0;
248
srinivas pandruvada401ca242012-09-05 13:56:00 +0100249 mutex_lock(&data->mutex);
250 memset(&data->pending, 0, sizeof(data->pending));
251 init_completion(&data->pending.ready);
252 data->pending.usage_id = usage_id;
253 data->pending.attr_usage_id = attr_usage_id;
254 data->pending.raw_size = 0;
255
256 spin_lock_irqsave(&data->lock, flags);
257 data->pending.status = true;
258 report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
259 if (!report) {
260 spin_unlock_irqrestore(&data->lock, flags);
261 goto err_free;
262 }
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100263 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100264 spin_unlock_irqrestore(&data->lock, flags);
265 wait_for_completion_interruptible_timeout(&data->pending.ready, HZ*5);
266 switch (data->pending.raw_size) {
267 case 1:
268 ret_val = *(u8 *)data->pending.raw_data;
269 break;
270 case 2:
271 ret_val = *(u16 *)data->pending.raw_data;
272 break;
273 case 4:
274 ret_val = *(u32 *)data->pending.raw_data;
275 break;
276 default:
277 ret_val = 0;
278 }
279 kfree(data->pending.raw_data);
280
281err_free:
282 data->pending.status = false;
283 mutex_unlock(&data->mutex);
284
285 return ret_val;
286}
287EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
288
289int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
290 u8 type,
291 u32 usage_id,
292 u32 attr_usage_id,
293 struct hid_sensor_hub_attribute_info *info)
294{
295 int ret = -1;
296 int i, j;
297 int collection_index = -1;
298 struct hid_report *report;
299 struct hid_field *field;
300 struct hid_report_enum *report_enum;
301 struct hid_device *hdev = hsdev->hdev;
302
303 /* Initialize with defaults */
304 info->usage_id = usage_id;
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300305 info->attrib_id = attr_usage_id;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100306 info->report_id = -1;
307 info->index = -1;
308 info->units = -1;
309 info->unit_expo = -1;
310
311 for (i = 0; i < hdev->maxcollection; ++i) {
312 struct hid_collection *collection = &hdev->collection[i];
313 if (usage_id == collection->usage) {
314 collection_index = i;
315 break;
316 }
317 }
318 if (collection_index == -1)
319 goto err_ret;
320
321 report_enum = &hdev->report_enum[type];
322 list_for_each_entry(report, &report_enum->report_list, list) {
323 for (i = 0; i < report->maxfield; ++i) {
324 field = report->field[i];
325 if (field->physical == usage_id &&
326 field->logical == attr_usage_id) {
327 sensor_hub_fill_attr_info(info, i, report->id,
328 field->unit, field->unit_exponent,
Srinivas Pandruvadad4b1bba2013-10-26 10:04:09 -0700329 field->report_size *
330 field->report_count);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100331 ret = 0;
332 } else {
333 for (j = 0; j < field->maxusage; ++j) {
334 if (field->usage[j].hid ==
335 attr_usage_id &&
336 field->usage[j].collection_index ==
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300337 collection_index) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100338 sensor_hub_fill_attr_info(info,
339 i, report->id,
340 field->unit,
341 field->unit_exponent,
Srinivas Pandruvadad4b1bba2013-10-26 10:04:09 -0700342 field->report_size *
343 field->report_count);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100344 ret = 0;
345 break;
346 }
347 }
348 }
349 if (ret == 0)
350 break;
351 }
352 }
353
354err_ret:
355 return ret;
356}
357EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
358
359#ifdef CONFIG_PM
360static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
361{
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300362 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100363 struct hid_sensor_hub_callbacks_list *callback;
364
365 hid_dbg(hdev, " sensor_hub_suspend\n");
366 spin_lock(&pdata->dyn_callback_lock);
367 list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
368 if (callback->usage_callback->suspend)
369 callback->usage_callback->suspend(
370 pdata->hsdev, callback->priv);
371 }
372 spin_unlock(&pdata->dyn_callback_lock);
373
374 return 0;
375}
376
377static int sensor_hub_resume(struct hid_device *hdev)
378{
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300379 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100380 struct hid_sensor_hub_callbacks_list *callback;
381
382 hid_dbg(hdev, " sensor_hub_resume\n");
383 spin_lock(&pdata->dyn_callback_lock);
384 list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
385 if (callback->usage_callback->resume)
386 callback->usage_callback->resume(
387 pdata->hsdev, callback->priv);
388 }
389 spin_unlock(&pdata->dyn_callback_lock);
390
391 return 0;
392}
393
394static int sensor_hub_reset_resume(struct hid_device *hdev)
395{
396 return 0;
397}
398#endif
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300399
srinivas pandruvada401ca242012-09-05 13:56:00 +0100400/*
401 * Handle raw report as sent by device
402 */
403static int sensor_hub_raw_event(struct hid_device *hdev,
404 struct hid_report *report, u8 *raw_data, int size)
405{
406 int i;
407 u8 *ptr;
408 int sz;
409 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
410 unsigned long flags;
411 struct hid_sensor_hub_callbacks *callback = NULL;
412 struct hid_collection *collection = NULL;
413 void *priv = NULL;
414
415 hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
416 report->id, size, report->type);
417 hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
418 if (report->type != HID_INPUT_REPORT)
419 return 1;
420
421 ptr = raw_data;
Andy Shevchenko15261f62013-08-14 11:07:08 +0300422 ptr++; /* Skip report id */
srinivas pandruvada401ca242012-09-05 13:56:00 +0100423
srinivas pandruvada401ca242012-09-05 13:56:00 +0100424 spin_lock_irqsave(&pdata->lock, flags);
425
426 for (i = 0; i < report->maxfield; ++i) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100427 hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
428 i, report->field[i]->usage->collection_index,
429 report->field[i]->usage->hid,
Srinivas Pandruvadad4b1bba2013-10-26 10:04:09 -0700430 (report->field[i]->report_size *
431 report->field[i]->report_count)/8);
432 sz = (report->field[i]->report_size *
433 report->field[i]->report_count)/8;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100434 if (pdata->pending.status && pdata->pending.attr_usage_id ==
435 report->field[i]->usage->hid) {
436 hid_dbg(hdev, "data was pending ...\n");
Andy Shevchenko7b0692f2013-08-14 11:07:11 +0300437 pdata->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
438 if (pdata->pending.raw_data)
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300439 pdata->pending.raw_size = sz;
Andy Shevchenko7b0692f2013-08-14 11:07:11 +0300440 else
srinivas pandruvada401ca242012-09-05 13:56:00 +0100441 pdata->pending.raw_size = 0;
442 complete(&pdata->pending.ready);
443 }
444 collection = &hdev->collection[
445 report->field[i]->usage->collection_index];
446 hid_dbg(hdev, "collection->usage %x\n",
447 collection->usage);
448 callback = sensor_hub_get_callback(pdata->hsdev->hdev,
449 report->field[i]->physical,
450 &priv);
451 if (callback && callback->capture_sample) {
452 if (report->field[i]->logical)
453 callback->capture_sample(pdata->hsdev,
454 report->field[i]->logical, sz, ptr,
455 callback->pdev);
456 else
457 callback->capture_sample(pdata->hsdev,
458 report->field[i]->usage->hid, sz, ptr,
459 callback->pdev);
460 }
461 ptr += sz;
462 }
463 if (callback && collection && callback->send_event)
464 callback->send_event(pdata->hsdev, collection->usage,
465 callback->pdev);
466 spin_unlock_irqrestore(&pdata->lock, flags);
467
srinivas pandruvada401ca242012-09-05 13:56:00 +0100468 return 1;
469}
470
Srinivas Pandruvada1df3a402013-09-18 18:13:00 +0100471int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
472{
473 int ret = 0;
474 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
475
476 mutex_lock(&data->mutex);
477 if (!hsdev->ref_cnt) {
478 ret = hid_hw_open(hsdev->hdev);
479 if (ret) {
480 hid_err(hsdev->hdev, "failed to open hid device\n");
481 mutex_unlock(&data->mutex);
482 return ret;
483 }
484 }
485 hsdev->ref_cnt++;
486 mutex_unlock(&data->mutex);
487
488 return ret;
489}
490EXPORT_SYMBOL_GPL(sensor_hub_device_open);
491
492void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
493{
494 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
495
496 mutex_lock(&data->mutex);
497 hsdev->ref_cnt--;
498 if (!hsdev->ref_cnt)
499 hid_hw_close(hsdev->hdev);
500 mutex_unlock(&data->mutex);
501}
502EXPORT_SYMBOL_GPL(sensor_hub_device_close);
503
srinivas pandruvada401ca242012-09-05 13:56:00 +0100504static int sensor_hub_probe(struct hid_device *hdev,
505 const struct hid_device_id *id)
506{
507 int ret;
508 struct sensor_hub_data *sd;
509 int i;
510 char *name;
511 struct hid_report *report;
512 struct hid_report_enum *report_enum;
513 struct hid_field *field;
514 int dev_cnt;
515
Andy Shevchenko905cc192013-08-14 11:07:10 +0300516 sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100517 if (!sd) {
518 hid_err(hdev, "cannot allocate Sensor data\n");
519 return -ENOMEM;
520 }
Andy Shevchenko905cc192013-08-14 11:07:10 +0300521 sd->hsdev = devm_kzalloc(&hdev->dev, sizeof(*sd->hsdev), GFP_KERNEL);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100522 if (!sd->hsdev) {
523 hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
Andy Shevchenko905cc192013-08-14 11:07:10 +0300524 return -ENOMEM;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100525 }
526 hid_set_drvdata(hdev, sd);
527 sd->hsdev->hdev = hdev;
528 sd->hsdev->vendor_id = hdev->vendor;
529 sd->hsdev->product_id = hdev->product;
530 spin_lock_init(&sd->lock);
531 spin_lock_init(&sd->dyn_callback_lock);
532 mutex_init(&sd->mutex);
533 ret = hid_parse(hdev);
534 if (ret) {
535 hid_err(hdev, "parse failed\n");
Andy Shevchenko905cc192013-08-14 11:07:10 +0300536 return ret;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100537 }
srinivas pandruvada401ca242012-09-05 13:56:00 +0100538 INIT_LIST_HEAD(&hdev->inputs);
539
srinivas pandruvada401ca242012-09-05 13:56:00 +0100540 ret = hid_hw_start(hdev, 0);
541 if (ret) {
542 hid_err(hdev, "hw start failed\n");
Andy Shevchenko905cc192013-08-14 11:07:10 +0300543 return ret;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100544 }
srinivas pandruvada401ca242012-09-05 13:56:00 +0100545 INIT_LIST_HEAD(&sd->dyn_callback_list);
546 sd->hid_sensor_client_cnt = 0;
547 report_enum = &hdev->report_enum[HID_INPUT_REPORT];
548
549 dev_cnt = sensor_hub_get_physical_device_count(report_enum);
550 if (dev_cnt > HID_MAX_PHY_DEVICES) {
551 hid_err(hdev, "Invalid Physical device count\n");
552 ret = -EINVAL;
Srinivas Pandruvada1df3a402013-09-18 18:13:00 +0100553 goto err_stop_hw;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100554 }
555 sd->hid_sensor_hub_client_devs = kzalloc(dev_cnt *
556 sizeof(struct mfd_cell),
557 GFP_KERNEL);
558 if (sd->hid_sensor_hub_client_devs == NULL) {
Axel Linf2f13a62012-09-19 16:30:00 +0100559 hid_err(hdev, "Failed to allocate memory for mfd cells\n");
srinivas pandruvada401ca242012-09-05 13:56:00 +0100560 ret = -ENOMEM;
Srinivas Pandruvada1df3a402013-09-18 18:13:00 +0100561 goto err_stop_hw;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100562 }
563 list_for_each_entry(report, &report_enum->report_list, list) {
564 hid_dbg(hdev, "Report id:%x\n", report->id);
565 field = report->field[0];
566 if (report->maxfield && field &&
567 field->physical) {
568 name = kasprintf(GFP_KERNEL, "HID-SENSOR-%x",
569 field->physical);
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300570 if (name == NULL) {
Axel Linf2f13a62012-09-19 16:30:00 +0100571 hid_err(hdev, "Failed MFD device name\n");
srinivas pandruvada401ca242012-09-05 13:56:00 +0100572 ret = -ENOMEM;
Axel Linf2f13a62012-09-19 16:30:00 +0100573 goto err_free_names;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100574 }
575 sd->hid_sensor_hub_client_devs[
576 sd->hid_sensor_client_cnt].name = name;
577 sd->hid_sensor_hub_client_devs[
578 sd->hid_sensor_client_cnt].platform_data =
579 sd->hsdev;
580 sd->hid_sensor_hub_client_devs[
581 sd->hid_sensor_client_cnt].pdata_size =
582 sizeof(*sd->hsdev);
583 hid_dbg(hdev, "Adding %s:%p\n", name, sd);
584 sd->hid_sensor_client_cnt++;
585 }
586 }
587 ret = mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs,
Stephen Rothwellf45c69b2012-09-12 07:07:00 +0100588 sd->hid_sensor_client_cnt, NULL, 0, NULL);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100589 if (ret < 0)
590 goto err_free_names;
591
592 return ret;
593
594err_free_names:
595 for (i = 0; i < sd->hid_sensor_client_cnt ; ++i)
596 kfree(sd->hid_sensor_hub_client_devs[i].name);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100597 kfree(sd->hid_sensor_hub_client_devs);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100598err_stop_hw:
599 hid_hw_stop(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100600
601 return ret;
602}
603
604static void sensor_hub_remove(struct hid_device *hdev)
605{
606 struct sensor_hub_data *data = hid_get_drvdata(hdev);
607 unsigned long flags;
608 int i;
609
610 hid_dbg(hdev, " hardware removed\n");
srinivas pandruvada401ca242012-09-05 13:56:00 +0100611 hid_hw_close(hdev);
Axel Linf2f13a62012-09-19 16:30:00 +0100612 hid_hw_stop(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100613 spin_lock_irqsave(&data->lock, flags);
614 if (data->pending.status)
615 complete(&data->pending.ready);
616 spin_unlock_irqrestore(&data->lock, flags);
617 mfd_remove_devices(&hdev->dev);
618 for (i = 0; i < data->hid_sensor_client_cnt ; ++i)
619 kfree(data->hid_sensor_hub_client_devs[i].name);
620 kfree(data->hid_sensor_hub_client_devs);
621 hid_set_drvdata(hdev, NULL);
622 mutex_destroy(&data->mutex);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100623}
624
625static const struct hid_device_id sensor_hub_devices[] = {
Mika Westerberg0d1e4b02013-02-11 12:31:19 +0200626 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
627 HID_ANY_ID) },
srinivas pandruvada401ca242012-09-05 13:56:00 +0100628 { }
629};
630MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
631
srinivas pandruvada401ca242012-09-05 13:56:00 +0100632static struct hid_driver sensor_hub_driver = {
633 .name = "hid-sensor-hub",
634 .id_table = sensor_hub_devices,
635 .probe = sensor_hub_probe,
636 .remove = sensor_hub_remove,
637 .raw_event = sensor_hub_raw_event,
638#ifdef CONFIG_PM
639 .suspend = sensor_hub_suspend,
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300640 .resume = sensor_hub_resume,
641 .reset_resume = sensor_hub_reset_resume,
srinivas pandruvada401ca242012-09-05 13:56:00 +0100642#endif
643};
H Hartley Sweetenf4254582012-12-17 15:28:26 -0700644module_hid_driver(sensor_hub_driver);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100645
646MODULE_DESCRIPTION("HID Sensor Hub driver");
647MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
648MODULE_LICENSE("GPL");