blob: d87f7cb4bee5af2b6c9d08dc02630c08f0f69cfb [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,
Srinivas Pandruvada9f740ff2013-11-27 22:19:00 +0000115 s32 index, s32 report_id, struct hid_field *field)
srinivas pandruvada401ca242012-09-05 13:56:00 +0100116{
117 info->index = index;
118 info->report_id = report_id;
Srinivas Pandruvada9f740ff2013-11-27 22:19:00 +0000119 info->units = field->unit;
120 info->unit_expo = field->unit_exponent;
121 info->size = (field->report_size * field->report_count)/8;
122 info->logical_minimum = field->logical_minimum;
123 info->logical_maximum = field->logical_maximum;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100124}
125
126static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
127 struct hid_device *hdev,
128 u32 usage_id, void **priv)
129{
130 struct hid_sensor_hub_callbacks_list *callback;
131 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
132
133 spin_lock(&pdata->dyn_callback_lock);
134 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
135 if (callback->usage_id == usage_id) {
136 *priv = callback->priv;
137 spin_unlock(&pdata->dyn_callback_lock);
138 return callback->usage_callback;
139 }
140 spin_unlock(&pdata->dyn_callback_lock);
141
142 return NULL;
143}
144
145int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
146 u32 usage_id,
147 struct hid_sensor_hub_callbacks *usage_callback)
148{
149 struct hid_sensor_hub_callbacks_list *callback;
150 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
151
152 spin_lock(&pdata->dyn_callback_lock);
153 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
154 if (callback->usage_id == usage_id) {
155 spin_unlock(&pdata->dyn_callback_lock);
156 return -EINVAL;
157 }
Dan Carpenter2b7c4b82012-09-14 06:53:23 +0000158 callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100159 if (!callback) {
160 spin_unlock(&pdata->dyn_callback_lock);
161 return -ENOMEM;
162 }
163 callback->usage_callback = usage_callback;
164 callback->usage_id = usage_id;
165 callback->priv = NULL;
166 list_add_tail(&callback->list, &pdata->dyn_callback_list);
167 spin_unlock(&pdata->dyn_callback_lock);
168
169 return 0;
170}
171EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
172
173int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
174 u32 usage_id)
175{
176 struct hid_sensor_hub_callbacks_list *callback;
177 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
178
179 spin_lock(&pdata->dyn_callback_lock);
180 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
181 if (callback->usage_id == usage_id) {
182 list_del(&callback->list);
183 kfree(callback);
184 break;
185 }
186 spin_unlock(&pdata->dyn_callback_lock);
187
188 return 0;
189}
190EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
191
192int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
193 u32 field_index, s32 value)
194{
195 struct hid_report *report;
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300196 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100197 int ret = 0;
198
srinivas pandruvada401ca242012-09-05 13:56:00 +0100199 mutex_lock(&data->mutex);
200 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300201 if (!report || (field_index >= report->maxfield)) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100202 ret = -EINVAL;
203 goto done_proc;
204 }
205 hid_set_field(report->field[field_index], 0, value);
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100206 hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100207 hid_hw_wait(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100208
209done_proc:
210 mutex_unlock(&data->mutex);
211
212 return ret;
213}
214EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
215
216int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
217 u32 field_index, s32 *value)
218{
219 struct hid_report *report;
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300220 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100221 int ret = 0;
222
srinivas pandruvada401ca242012-09-05 13:56:00 +0100223 mutex_lock(&data->mutex);
224 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
Jiri Kosina4e5a4942013-09-06 11:59:53 +0200225 if (!report || (field_index >= report->maxfield) ||
Kees Cook9e891022013-08-28 22:31:44 +0200226 report->field[field_index]->report_count < 1) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100227 ret = -EINVAL;
228 goto done_proc;
229 }
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100230 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100231 hid_hw_wait(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100232 *value = report->field[field_index]->value[0];
233
234done_proc:
235 mutex_unlock(&data->mutex);
236
237 return ret;
238}
239EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
240
241
242int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
243 u32 usage_id,
244 u32 attr_usage_id, u32 report_id)
245{
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300246 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100247 unsigned long flags;
248 struct hid_report *report;
249 int ret_val = 0;
250
srinivas pandruvada401ca242012-09-05 13:56:00 +0100251 mutex_lock(&data->mutex);
252 memset(&data->pending, 0, sizeof(data->pending));
253 init_completion(&data->pending.ready);
254 data->pending.usage_id = usage_id;
255 data->pending.attr_usage_id = attr_usage_id;
256 data->pending.raw_size = 0;
257
258 spin_lock_irqsave(&data->lock, flags);
259 data->pending.status = true;
260 report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
261 if (!report) {
262 spin_unlock_irqrestore(&data->lock, flags);
263 goto err_free;
264 }
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100265 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100266 spin_unlock_irqrestore(&data->lock, flags);
267 wait_for_completion_interruptible_timeout(&data->pending.ready, HZ*5);
268 switch (data->pending.raw_size) {
269 case 1:
270 ret_val = *(u8 *)data->pending.raw_data;
271 break;
272 case 2:
273 ret_val = *(u16 *)data->pending.raw_data;
274 break;
275 case 4:
276 ret_val = *(u32 *)data->pending.raw_data;
277 break;
278 default:
279 ret_val = 0;
280 }
281 kfree(data->pending.raw_data);
282
283err_free:
284 data->pending.status = false;
285 mutex_unlock(&data->mutex);
286
287 return ret_val;
288}
289EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
290
291int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
292 u8 type,
293 u32 usage_id,
294 u32 attr_usage_id,
295 struct hid_sensor_hub_attribute_info *info)
296{
297 int ret = -1;
298 int i, j;
299 int collection_index = -1;
300 struct hid_report *report;
301 struct hid_field *field;
302 struct hid_report_enum *report_enum;
303 struct hid_device *hdev = hsdev->hdev;
304
305 /* Initialize with defaults */
306 info->usage_id = usage_id;
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300307 info->attrib_id = attr_usage_id;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100308 info->report_id = -1;
309 info->index = -1;
310 info->units = -1;
311 info->unit_expo = -1;
312
313 for (i = 0; i < hdev->maxcollection; ++i) {
314 struct hid_collection *collection = &hdev->collection[i];
315 if (usage_id == collection->usage) {
316 collection_index = i;
317 break;
318 }
319 }
320 if (collection_index == -1)
321 goto err_ret;
322
323 report_enum = &hdev->report_enum[type];
324 list_for_each_entry(report, &report_enum->report_list, list) {
325 for (i = 0; i < report->maxfield; ++i) {
326 field = report->field[i];
327 if (field->physical == usage_id &&
328 field->logical == attr_usage_id) {
329 sensor_hub_fill_attr_info(info, i, report->id,
Srinivas Pandruvada9f740ff2013-11-27 22:19:00 +0000330 field);
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,
Srinivas Pandruvada9f740ff2013-11-27 22:19:00 +0000339 i, report->id, field);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100340 ret = 0;
341 break;
342 }
343 }
344 }
345 if (ret == 0)
346 break;
347 }
348 }
349
350err_ret:
351 return ret;
352}
353EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
354
355#ifdef CONFIG_PM
356static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
357{
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300358 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100359 struct hid_sensor_hub_callbacks_list *callback;
360
361 hid_dbg(hdev, " sensor_hub_suspend\n");
362 spin_lock(&pdata->dyn_callback_lock);
363 list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
364 if (callback->usage_callback->suspend)
365 callback->usage_callback->suspend(
366 pdata->hsdev, callback->priv);
367 }
368 spin_unlock(&pdata->dyn_callback_lock);
369
370 return 0;
371}
372
373static int sensor_hub_resume(struct hid_device *hdev)
374{
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300375 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100376 struct hid_sensor_hub_callbacks_list *callback;
377
378 hid_dbg(hdev, " sensor_hub_resume\n");
379 spin_lock(&pdata->dyn_callback_lock);
380 list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
381 if (callback->usage_callback->resume)
382 callback->usage_callback->resume(
383 pdata->hsdev, callback->priv);
384 }
385 spin_unlock(&pdata->dyn_callback_lock);
386
387 return 0;
388}
389
390static int sensor_hub_reset_resume(struct hid_device *hdev)
391{
392 return 0;
393}
394#endif
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300395
srinivas pandruvada401ca242012-09-05 13:56:00 +0100396/*
397 * Handle raw report as sent by device
398 */
399static int sensor_hub_raw_event(struct hid_device *hdev,
400 struct hid_report *report, u8 *raw_data, int size)
401{
402 int i;
403 u8 *ptr;
404 int sz;
405 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
406 unsigned long flags;
407 struct hid_sensor_hub_callbacks *callback = NULL;
408 struct hid_collection *collection = NULL;
409 void *priv = NULL;
410
411 hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
412 report->id, size, report->type);
413 hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
414 if (report->type != HID_INPUT_REPORT)
415 return 1;
416
417 ptr = raw_data;
Andy Shevchenko15261f62013-08-14 11:07:08 +0300418 ptr++; /* Skip report id */
srinivas pandruvada401ca242012-09-05 13:56:00 +0100419
srinivas pandruvada401ca242012-09-05 13:56:00 +0100420 spin_lock_irqsave(&pdata->lock, flags);
421
422 for (i = 0; i < report->maxfield; ++i) {
srinivas pandruvada401ca242012-09-05 13:56:00 +0100423 hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
424 i, report->field[i]->usage->collection_index,
425 report->field[i]->usage->hid,
Srinivas Pandruvadad4b1bba2013-10-26 10:04:09 -0700426 (report->field[i]->report_size *
427 report->field[i]->report_count)/8);
428 sz = (report->field[i]->report_size *
429 report->field[i]->report_count)/8;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100430 if (pdata->pending.status && pdata->pending.attr_usage_id ==
431 report->field[i]->usage->hid) {
432 hid_dbg(hdev, "data was pending ...\n");
Andy Shevchenko7b0692f2013-08-14 11:07:11 +0300433 pdata->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
434 if (pdata->pending.raw_data)
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300435 pdata->pending.raw_size = sz;
Andy Shevchenko7b0692f2013-08-14 11:07:11 +0300436 else
srinivas pandruvada401ca242012-09-05 13:56:00 +0100437 pdata->pending.raw_size = 0;
438 complete(&pdata->pending.ready);
439 }
440 collection = &hdev->collection[
441 report->field[i]->usage->collection_index];
442 hid_dbg(hdev, "collection->usage %x\n",
443 collection->usage);
444 callback = sensor_hub_get_callback(pdata->hsdev->hdev,
445 report->field[i]->physical,
446 &priv);
447 if (callback && callback->capture_sample) {
448 if (report->field[i]->logical)
449 callback->capture_sample(pdata->hsdev,
450 report->field[i]->logical, sz, ptr,
451 callback->pdev);
452 else
453 callback->capture_sample(pdata->hsdev,
454 report->field[i]->usage->hid, sz, ptr,
455 callback->pdev);
456 }
457 ptr += sz;
458 }
459 if (callback && collection && callback->send_event)
460 callback->send_event(pdata->hsdev, collection->usage,
461 callback->pdev);
462 spin_unlock_irqrestore(&pdata->lock, flags);
463
srinivas pandruvada401ca242012-09-05 13:56:00 +0100464 return 1;
465}
466
Srinivas Pandruvada1df3a402013-09-18 18:13:00 +0100467int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
468{
469 int ret = 0;
470 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
471
472 mutex_lock(&data->mutex);
473 if (!hsdev->ref_cnt) {
474 ret = hid_hw_open(hsdev->hdev);
475 if (ret) {
476 hid_err(hsdev->hdev, "failed to open hid device\n");
477 mutex_unlock(&data->mutex);
478 return ret;
479 }
480 }
481 hsdev->ref_cnt++;
482 mutex_unlock(&data->mutex);
483
484 return ret;
485}
486EXPORT_SYMBOL_GPL(sensor_hub_device_open);
487
488void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
489{
490 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
491
492 mutex_lock(&data->mutex);
493 hsdev->ref_cnt--;
494 if (!hsdev->ref_cnt)
495 hid_hw_close(hsdev->hdev);
496 mutex_unlock(&data->mutex);
497}
498EXPORT_SYMBOL_GPL(sensor_hub_device_close);
499
srinivas pandruvada401ca242012-09-05 13:56:00 +0100500static int sensor_hub_probe(struct hid_device *hdev,
501 const struct hid_device_id *id)
502{
503 int ret;
504 struct sensor_hub_data *sd;
505 int i;
506 char *name;
507 struct hid_report *report;
508 struct hid_report_enum *report_enum;
509 struct hid_field *field;
510 int dev_cnt;
511
Andy Shevchenko905cc192013-08-14 11:07:10 +0300512 sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100513 if (!sd) {
514 hid_err(hdev, "cannot allocate Sensor data\n");
515 return -ENOMEM;
516 }
Andy Shevchenko905cc192013-08-14 11:07:10 +0300517 sd->hsdev = devm_kzalloc(&hdev->dev, sizeof(*sd->hsdev), GFP_KERNEL);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100518 if (!sd->hsdev) {
519 hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
Andy Shevchenko905cc192013-08-14 11:07:10 +0300520 return -ENOMEM;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100521 }
522 hid_set_drvdata(hdev, sd);
523 sd->hsdev->hdev = hdev;
524 sd->hsdev->vendor_id = hdev->vendor;
525 sd->hsdev->product_id = hdev->product;
526 spin_lock_init(&sd->lock);
527 spin_lock_init(&sd->dyn_callback_lock);
528 mutex_init(&sd->mutex);
529 ret = hid_parse(hdev);
530 if (ret) {
531 hid_err(hdev, "parse failed\n");
Andy Shevchenko905cc192013-08-14 11:07:10 +0300532 return ret;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100533 }
srinivas pandruvada401ca242012-09-05 13:56:00 +0100534 INIT_LIST_HEAD(&hdev->inputs);
535
srinivas pandruvada401ca242012-09-05 13:56:00 +0100536 ret = hid_hw_start(hdev, 0);
537 if (ret) {
538 hid_err(hdev, "hw start failed\n");
Andy Shevchenko905cc192013-08-14 11:07:10 +0300539 return ret;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100540 }
srinivas pandruvada401ca242012-09-05 13:56:00 +0100541 INIT_LIST_HEAD(&sd->dyn_callback_list);
542 sd->hid_sensor_client_cnt = 0;
543 report_enum = &hdev->report_enum[HID_INPUT_REPORT];
544
545 dev_cnt = sensor_hub_get_physical_device_count(report_enum);
546 if (dev_cnt > HID_MAX_PHY_DEVICES) {
547 hid_err(hdev, "Invalid Physical device count\n");
548 ret = -EINVAL;
Srinivas Pandruvada1df3a402013-09-18 18:13:00 +0100549 goto err_stop_hw;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100550 }
551 sd->hid_sensor_hub_client_devs = kzalloc(dev_cnt *
552 sizeof(struct mfd_cell),
553 GFP_KERNEL);
554 if (sd->hid_sensor_hub_client_devs == NULL) {
Axel Linf2f13a62012-09-19 16:30:00 +0100555 hid_err(hdev, "Failed to allocate memory for mfd cells\n");
srinivas pandruvada401ca242012-09-05 13:56:00 +0100556 ret = -ENOMEM;
Srinivas Pandruvada1df3a402013-09-18 18:13:00 +0100557 goto err_stop_hw;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100558 }
559 list_for_each_entry(report, &report_enum->report_list, list) {
560 hid_dbg(hdev, "Report id:%x\n", report->id);
561 field = report->field[0];
562 if (report->maxfield && field &&
563 field->physical) {
564 name = kasprintf(GFP_KERNEL, "HID-SENSOR-%x",
565 field->physical);
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300566 if (name == NULL) {
Axel Linf2f13a62012-09-19 16:30:00 +0100567 hid_err(hdev, "Failed MFD device name\n");
srinivas pandruvada401ca242012-09-05 13:56:00 +0100568 ret = -ENOMEM;
Axel Linf2f13a62012-09-19 16:30:00 +0100569 goto err_free_names;
srinivas pandruvada401ca242012-09-05 13:56:00 +0100570 }
571 sd->hid_sensor_hub_client_devs[
572 sd->hid_sensor_client_cnt].name = name;
573 sd->hid_sensor_hub_client_devs[
574 sd->hid_sensor_client_cnt].platform_data =
575 sd->hsdev;
576 sd->hid_sensor_hub_client_devs[
577 sd->hid_sensor_client_cnt].pdata_size =
578 sizeof(*sd->hsdev);
579 hid_dbg(hdev, "Adding %s:%p\n", name, sd);
580 sd->hid_sensor_client_cnt++;
581 }
582 }
583 ret = mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs,
Stephen Rothwellf45c69b2012-09-12 07:07:00 +0100584 sd->hid_sensor_client_cnt, NULL, 0, NULL);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100585 if (ret < 0)
586 goto err_free_names;
587
588 return ret;
589
590err_free_names:
591 for (i = 0; i < sd->hid_sensor_client_cnt ; ++i)
592 kfree(sd->hid_sensor_hub_client_devs[i].name);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100593 kfree(sd->hid_sensor_hub_client_devs);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100594err_stop_hw:
595 hid_hw_stop(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100596
597 return ret;
598}
599
600static void sensor_hub_remove(struct hid_device *hdev)
601{
602 struct sensor_hub_data *data = hid_get_drvdata(hdev);
603 unsigned long flags;
604 int i;
605
606 hid_dbg(hdev, " hardware removed\n");
srinivas pandruvada401ca242012-09-05 13:56:00 +0100607 hid_hw_close(hdev);
Axel Linf2f13a62012-09-19 16:30:00 +0100608 hid_hw_stop(hdev);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100609 spin_lock_irqsave(&data->lock, flags);
610 if (data->pending.status)
611 complete(&data->pending.ready);
612 spin_unlock_irqrestore(&data->lock, flags);
613 mfd_remove_devices(&hdev->dev);
614 for (i = 0; i < data->hid_sensor_client_cnt ; ++i)
615 kfree(data->hid_sensor_hub_client_devs[i].name);
616 kfree(data->hid_sensor_hub_client_devs);
617 hid_set_drvdata(hdev, NULL);
618 mutex_destroy(&data->mutex);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100619}
620
621static const struct hid_device_id sensor_hub_devices[] = {
Mika Westerberg0d1e4b02013-02-11 12:31:19 +0200622 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
623 HID_ANY_ID) },
srinivas pandruvada401ca242012-09-05 13:56:00 +0100624 { }
625};
626MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
627
srinivas pandruvada401ca242012-09-05 13:56:00 +0100628static struct hid_driver sensor_hub_driver = {
629 .name = "hid-sensor-hub",
630 .id_table = sensor_hub_devices,
631 .probe = sensor_hub_probe,
632 .remove = sensor_hub_remove,
633 .raw_event = sensor_hub_raw_event,
634#ifdef CONFIG_PM
635 .suspend = sensor_hub_suspend,
Andy Shevchenko5902fde2013-08-14 11:07:09 +0300636 .resume = sensor_hub_resume,
637 .reset_resume = sensor_hub_reset_resume,
srinivas pandruvada401ca242012-09-05 13:56:00 +0100638#endif
639};
H Hartley Sweetenf4254582012-12-17 15:28:26 -0700640module_hid_driver(sensor_hub_driver);
srinivas pandruvada401ca242012-09-05 13:56:00 +0100641
642MODULE_DESCRIPTION("HID Sensor Hub driver");
643MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
644MODULE_LICENSE("GPL");