mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / hid / hid-sensor-hub.c
blobfe8618c5b5c19d24935291ab2b24802e09892663
1 /*
2 * HID Sensors Driver
3 * Copyright (c) 2012, Intel Corporation.
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.
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.
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.
19 #include <linux/device.h>
20 #include <linux/hid.h>
21 #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"
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.
38 struct 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;
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).
58 struct 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;
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.
76 struct 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;
83 static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
84 int dir)
86 struct hid_report *report;
88 list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
89 if (report->id == id)
90 return report;
92 hid_warn(hdev, "No report with id 0x%x found\n", id);
94 return NULL;
97 static int sensor_hub_get_physical_device_count(
98 struct hid_report_enum *report_enum)
100 struct hid_report *report;
101 struct hid_field *field;
102 int cnt = 0;
104 list_for_each_entry(report, &report_enum->report_list, list) {
105 field = report->field[0];
106 if (report->maxfield && field && field->physical)
107 cnt++;
110 return cnt;
113 static 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)
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;
124 static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
125 struct hid_device *hdev,
126 u32 usage_id, void **priv)
128 struct hid_sensor_hub_callbacks_list *callback;
129 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
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;
138 spin_unlock(&pdata->dyn_callback_lock);
140 return NULL;
143 int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
144 u32 usage_id,
145 struct hid_sensor_hub_callbacks *usage_callback)
147 struct hid_sensor_hub_callbacks_list *callback;
148 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
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;
156 callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
157 if (!callback) {
158 spin_unlock(&pdata->dyn_callback_lock);
159 return -ENOMEM;
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);
167 return 0;
169 EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
171 int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
172 u32 usage_id)
174 struct hid_sensor_hub_callbacks_list *callback;
175 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
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;
184 spin_unlock(&pdata->dyn_callback_lock);
186 return 0;
188 EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
190 int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
191 u32 field_index, s32 value)
193 struct hid_report *report;
194 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
195 int ret = 0;
197 mutex_lock(&data->mutex);
198 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
199 if (!report || (field_index >= report->maxfield)) {
200 ret = -EINVAL;
201 goto done_proc;
203 hid_set_field(report->field[field_index], 0, value);
204 hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
205 hid_hw_wait(hsdev->hdev);
207 done_proc:
208 mutex_unlock(&data->mutex);
210 return ret;
212 EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
214 int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
215 u32 field_index, s32 *value)
217 struct hid_report *report;
218 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
219 int ret = 0;
221 mutex_lock(&data->mutex);
222 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
223 if (!report || (field_index >= report->maxfield) ||
224 report->field[field_index]->report_count < 1) {
225 ret = -EINVAL;
226 goto done_proc;
228 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
229 hid_hw_wait(hsdev->hdev);
230 *value = report->field[field_index]->value[0];
232 done_proc:
233 mutex_unlock(&data->mutex);
235 return ret;
237 EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
240 int 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)
244 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
245 unsigned long flags;
246 struct hid_report *report;
247 int ret_val = 0;
249 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;
256 spin_lock_irqsave(&data->lock, flags);
257 data->pending.status = true;
258 spin_unlock_irqrestore(&data->lock, flags);
259 report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
260 if (!report)
261 goto err_free;
263 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
264 wait_for_completion_interruptible_timeout(&data->pending.ready, HZ*5);
265 switch (data->pending.raw_size) {
266 case 1:
267 ret_val = *(u8 *)data->pending.raw_data;
268 break;
269 case 2:
270 ret_val = *(u16 *)data->pending.raw_data;
271 break;
272 case 4:
273 ret_val = *(u32 *)data->pending.raw_data;
274 break;
275 default:
276 ret_val = 0;
278 kfree(data->pending.raw_data);
280 err_free:
281 data->pending.status = false;
282 mutex_unlock(&data->mutex);
284 return ret_val;
286 EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
288 int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
289 u8 type,
290 u32 usage_id,
291 u32 attr_usage_id,
292 struct hid_sensor_hub_attribute_info *info)
294 int ret = -1;
295 int i, j;
296 int collection_index = -1;
297 struct hid_report *report;
298 struct hid_field *field;
299 struct hid_report_enum *report_enum;
300 struct hid_device *hdev = hsdev->hdev;
302 /* Initialize with defaults */
303 info->usage_id = usage_id;
304 info->attrib_id = attr_usage_id;
305 info->report_id = -1;
306 info->index = -1;
307 info->units = -1;
308 info->unit_expo = -1;
310 for (i = 0; i < hdev->maxcollection; ++i) {
311 struct hid_collection *collection = &hdev->collection[i];
312 if (usage_id == collection->usage) {
313 collection_index = i;
314 break;
317 if (collection_index == -1)
318 goto err_ret;
320 report_enum = &hdev->report_enum[type];
321 list_for_each_entry(report, &report_enum->report_list, list) {
322 for (i = 0; i < report->maxfield; ++i) {
323 field = report->field[i];
324 if (field->physical == usage_id &&
325 field->logical == attr_usage_id) {
326 sensor_hub_fill_attr_info(info, i, report->id,
327 field->unit, field->unit_exponent,
328 field->report_size *
329 field->report_count);
330 ret = 0;
331 } else {
332 for (j = 0; j < field->maxusage; ++j) {
333 if (field->usage[j].hid ==
334 attr_usage_id &&
335 field->usage[j].collection_index ==
336 collection_index) {
337 sensor_hub_fill_attr_info(info,
338 i, report->id,
339 field->unit,
340 field->unit_exponent,
341 field->report_size *
342 field->report_count);
343 ret = 0;
344 break;
348 if (ret == 0)
349 break;
353 err_ret:
354 return ret;
356 EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
358 #ifdef CONFIG_PM
359 static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
361 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
362 struct hid_sensor_hub_callbacks_list *callback;
364 hid_dbg(hdev, " sensor_hub_suspend\n");
365 spin_lock(&pdata->dyn_callback_lock);
366 list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
367 if (callback->usage_callback->suspend)
368 callback->usage_callback->suspend(
369 pdata->hsdev, callback->priv);
371 spin_unlock(&pdata->dyn_callback_lock);
373 return 0;
376 static int sensor_hub_resume(struct hid_device *hdev)
378 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
379 struct hid_sensor_hub_callbacks_list *callback;
381 hid_dbg(hdev, " sensor_hub_resume\n");
382 spin_lock(&pdata->dyn_callback_lock);
383 list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
384 if (callback->usage_callback->resume)
385 callback->usage_callback->resume(
386 pdata->hsdev, callback->priv);
388 spin_unlock(&pdata->dyn_callback_lock);
390 return 0;
393 static int sensor_hub_reset_resume(struct hid_device *hdev)
395 return 0;
397 #endif
400 * Handle raw report as sent by device
402 static int sensor_hub_raw_event(struct hid_device *hdev,
403 struct hid_report *report, u8 *raw_data, int size)
405 int i;
406 u8 *ptr;
407 int sz;
408 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
409 unsigned long flags;
410 struct hid_sensor_hub_callbacks *callback = NULL;
411 struct hid_collection *collection = NULL;
412 void *priv = NULL;
414 hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
415 report->id, size, report->type);
416 hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
417 if (report->type != HID_INPUT_REPORT)
418 return 1;
420 ptr = raw_data;
421 ptr++; /* Skip report id */
423 spin_lock_irqsave(&pdata->lock, flags);
425 for (i = 0; i < report->maxfield; ++i) {
426 hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
427 i, report->field[i]->usage->collection_index,
428 report->field[i]->usage->hid,
429 (report->field[i]->report_size *
430 report->field[i]->report_count)/8);
431 sz = (report->field[i]->report_size *
432 report->field[i]->report_count)/8;
433 if (pdata->pending.status && pdata->pending.attr_usage_id ==
434 report->field[i]->usage->hid) {
435 hid_dbg(hdev, "data was pending ...\n");
436 pdata->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
437 if (pdata->pending.raw_data)
438 pdata->pending.raw_size = sz;
439 else
440 pdata->pending.raw_size = 0;
441 complete(&pdata->pending.ready);
443 collection = &hdev->collection[
444 report->field[i]->usage->collection_index];
445 hid_dbg(hdev, "collection->usage %x\n",
446 collection->usage);
447 callback = sensor_hub_get_callback(pdata->hsdev->hdev,
448 report->field[i]->physical,
449 &priv);
450 if (callback && callback->capture_sample) {
451 if (report->field[i]->logical)
452 callback->capture_sample(pdata->hsdev,
453 report->field[i]->logical, sz, ptr,
454 callback->pdev);
455 else
456 callback->capture_sample(pdata->hsdev,
457 report->field[i]->usage->hid, sz, ptr,
458 callback->pdev);
460 ptr += sz;
462 if (callback && collection && callback->send_event)
463 callback->send_event(pdata->hsdev, collection->usage,
464 callback->pdev);
465 spin_unlock_irqrestore(&pdata->lock, flags);
467 return 1;
470 static int sensor_hub_probe(struct hid_device *hdev,
471 const struct hid_device_id *id)
473 int ret;
474 struct sensor_hub_data *sd;
475 int i;
476 char *name;
477 struct hid_report *report;
478 struct hid_report_enum *report_enum;
479 struct hid_field *field;
480 int dev_cnt;
482 sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
483 if (!sd) {
484 hid_err(hdev, "cannot allocate Sensor data\n");
485 return -ENOMEM;
487 sd->hsdev = devm_kzalloc(&hdev->dev, sizeof(*sd->hsdev), GFP_KERNEL);
488 if (!sd->hsdev) {
489 hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
490 return -ENOMEM;
492 hid_set_drvdata(hdev, sd);
493 sd->hsdev->hdev = hdev;
494 sd->hsdev->vendor_id = hdev->vendor;
495 sd->hsdev->product_id = hdev->product;
496 spin_lock_init(&sd->lock);
497 spin_lock_init(&sd->dyn_callback_lock);
498 mutex_init(&sd->mutex);
499 ret = hid_parse(hdev);
500 if (ret) {
501 hid_err(hdev, "parse failed\n");
502 return ret;
504 INIT_LIST_HEAD(&hdev->inputs);
506 ret = hid_hw_start(hdev, 0);
507 if (ret) {
508 hid_err(hdev, "hw start failed\n");
509 return ret;
511 ret = hid_hw_open(hdev);
512 if (ret) {
513 hid_err(hdev, "failed to open input interrupt pipe\n");
514 goto err_stop_hw;
517 INIT_LIST_HEAD(&sd->dyn_callback_list);
518 sd->hid_sensor_client_cnt = 0;
519 report_enum = &hdev->report_enum[HID_INPUT_REPORT];
521 dev_cnt = sensor_hub_get_physical_device_count(report_enum);
522 if (dev_cnt > HID_MAX_PHY_DEVICES) {
523 hid_err(hdev, "Invalid Physical device count\n");
524 ret = -EINVAL;
525 goto err_close;
527 sd->hid_sensor_hub_client_devs = kzalloc(dev_cnt *
528 sizeof(struct mfd_cell),
529 GFP_KERNEL);
530 if (sd->hid_sensor_hub_client_devs == NULL) {
531 hid_err(hdev, "Failed to allocate memory for mfd cells\n");
532 ret = -ENOMEM;
533 goto err_close;
535 list_for_each_entry(report, &report_enum->report_list, list) {
536 hid_dbg(hdev, "Report id:%x\n", report->id);
537 field = report->field[0];
538 if (report->maxfield && field &&
539 field->physical) {
540 name = kasprintf(GFP_KERNEL, "HID-SENSOR-%x",
541 field->physical);
542 if (name == NULL) {
543 hid_err(hdev, "Failed MFD device name\n");
544 ret = -ENOMEM;
545 goto err_free_names;
547 sd->hid_sensor_hub_client_devs[
548 sd->hid_sensor_client_cnt].name = name;
549 sd->hid_sensor_hub_client_devs[
550 sd->hid_sensor_client_cnt].platform_data =
551 sd->hsdev;
552 sd->hid_sensor_hub_client_devs[
553 sd->hid_sensor_client_cnt].pdata_size =
554 sizeof(*sd->hsdev);
555 hid_dbg(hdev, "Adding %s:%p\n", name, sd);
556 sd->hid_sensor_client_cnt++;
559 ret = mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs,
560 sd->hid_sensor_client_cnt, NULL, 0, NULL);
561 if (ret < 0)
562 goto err_free_names;
564 return ret;
566 err_free_names:
567 for (i = 0; i < sd->hid_sensor_client_cnt ; ++i)
568 kfree(sd->hid_sensor_hub_client_devs[i].name);
569 kfree(sd->hid_sensor_hub_client_devs);
570 err_close:
571 hid_hw_close(hdev);
572 err_stop_hw:
573 hid_hw_stop(hdev);
575 return ret;
578 static void sensor_hub_remove(struct hid_device *hdev)
580 struct sensor_hub_data *data = hid_get_drvdata(hdev);
581 unsigned long flags;
582 int i;
584 hid_dbg(hdev, " hardware removed\n");
585 hid_hw_close(hdev);
586 hid_hw_stop(hdev);
587 spin_lock_irqsave(&data->lock, flags);
588 if (data->pending.status)
589 complete(&data->pending.ready);
590 spin_unlock_irqrestore(&data->lock, flags);
591 mfd_remove_devices(&hdev->dev);
592 for (i = 0; i < data->hid_sensor_client_cnt ; ++i)
593 kfree(data->hid_sensor_hub_client_devs[i].name);
594 kfree(data->hid_sensor_hub_client_devs);
595 hid_set_drvdata(hdev, NULL);
596 mutex_destroy(&data->mutex);
599 static const struct hid_device_id sensor_hub_devices[] = {
600 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
601 HID_ANY_ID) },
604 MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
606 static struct hid_driver sensor_hub_driver = {
607 .name = "hid-sensor-hub",
608 .id_table = sensor_hub_devices,
609 .probe = sensor_hub_probe,
610 .remove = sensor_hub_remove,
611 .raw_event = sensor_hub_raw_event,
612 #ifdef CONFIG_PM
613 .suspend = sensor_hub_suspend,
614 .resume = sensor_hub_resume,
615 .reset_resume = sensor_hub_reset_resume,
616 #endif
618 module_hid_driver(sensor_hub_driver);
620 MODULE_DESCRIPTION("HID Sensor Hub driver");
621 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
622 MODULE_LICENSE("GPL");