Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux...
[deliverable/linux.git] / drivers / hid / hid-sensor-hub.c
1 /*
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>
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"
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 */
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;
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 */
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;
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 */
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;
81 };
82
83 static 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
97 static 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];
106 if (report->maxfield && field && field->physical)
107 cnt++;
108 }
109
110 return cnt;
111 }
112
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)
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
124 static 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
143 int 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 }
156 callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
157 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 }
169 EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
170
171 int 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 }
188 EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
189
190 int 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;
194 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
195 int ret = 0;
196
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;
202 }
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);
206
207 done_proc:
208 mutex_unlock(&data->mutex);
209
210 return ret;
211 }
212 EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
213
214 int 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;
218 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
219 int ret = 0;
220
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;
227 }
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];
231
232 done_proc:
233 mutex_unlock(&data->mutex);
234
235 return ret;
236 }
237 EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
238
239
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)
243 {
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;
248
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;
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 }
263 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
264 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
281 err_free:
282 data->pending.status = false;
283 mutex_unlock(&data->mutex);
284
285 return ret_val;
286 }
287 EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
288
289 int 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;
305 info->attrib_id = attr_usage_id;
306 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,
329 field->report_size);
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 ret = 0;
343 break;
344 }
345 }
346 }
347 if (ret == 0)
348 break;
349 }
350 }
351
352 err_ret:
353 return ret;
354 }
355 EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
356
357 #ifdef CONFIG_PM
358 static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
359 {
360 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
361 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
375 static int sensor_hub_resume(struct hid_device *hdev)
376 {
377 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
378 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
392 static int sensor_hub_reset_resume(struct hid_device *hdev)
393 {
394 return 0;
395 }
396 #endif
397
398 /*
399 * Handle raw report as sent by device
400 */
401 static 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;
420 ptr++; /* Skip report id */
421
422 spin_lock_irqsave(&pdata->lock, flags);
423
424 for (i = 0; i < report->maxfield; ++i) {
425 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,
428 report->field[i]->report_size/8);
429
430 sz = report->field[i]->report_size/8;
431 if (pdata->pending.status && pdata->pending.attr_usage_id ==
432 report->field[i]->usage->hid) {
433 hid_dbg(hdev, "data was pending ...\n");
434 pdata->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
435 if (pdata->pending.raw_data)
436 pdata->pending.raw_size = sz;
437 else
438 pdata->pending.raw_size = 0;
439 complete(&pdata->pending.ready);
440 }
441 collection = &hdev->collection[
442 report->field[i]->usage->collection_index];
443 hid_dbg(hdev, "collection->usage %x\n",
444 collection->usage);
445 callback = sensor_hub_get_callback(pdata->hsdev->hdev,
446 report->field[i]->physical,
447 &priv);
448 if (callback && callback->capture_sample) {
449 if (report->field[i]->logical)
450 callback->capture_sample(pdata->hsdev,
451 report->field[i]->logical, sz, ptr,
452 callback->pdev);
453 else
454 callback->capture_sample(pdata->hsdev,
455 report->field[i]->usage->hid, sz, ptr,
456 callback->pdev);
457 }
458 ptr += sz;
459 }
460 if (callback && collection && callback->send_event)
461 callback->send_event(pdata->hsdev, collection->usage,
462 callback->pdev);
463 spin_unlock_irqrestore(&pdata->lock, flags);
464
465 return 1;
466 }
467
468 int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
469 {
470 int ret = 0;
471 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
472
473 mutex_lock(&data->mutex);
474 if (!hsdev->ref_cnt) {
475 ret = hid_hw_open(hsdev->hdev);
476 if (ret) {
477 hid_err(hsdev->hdev, "failed to open hid device\n");
478 mutex_unlock(&data->mutex);
479 return ret;
480 }
481 }
482 hsdev->ref_cnt++;
483 mutex_unlock(&data->mutex);
484
485 return ret;
486 }
487 EXPORT_SYMBOL_GPL(sensor_hub_device_open);
488
489 void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
490 {
491 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
492
493 mutex_lock(&data->mutex);
494 hsdev->ref_cnt--;
495 if (!hsdev->ref_cnt)
496 hid_hw_close(hsdev->hdev);
497 mutex_unlock(&data->mutex);
498 }
499 EXPORT_SYMBOL_GPL(sensor_hub_device_close);
500
501 static int sensor_hub_probe(struct hid_device *hdev,
502 const struct hid_device_id *id)
503 {
504 int ret;
505 struct sensor_hub_data *sd;
506 int i;
507 char *name;
508 struct hid_report *report;
509 struct hid_report_enum *report_enum;
510 struct hid_field *field;
511 int dev_cnt;
512
513 sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
514 if (!sd) {
515 hid_err(hdev, "cannot allocate Sensor data\n");
516 return -ENOMEM;
517 }
518 sd->hsdev = devm_kzalloc(&hdev->dev, sizeof(*sd->hsdev), GFP_KERNEL);
519 if (!sd->hsdev) {
520 hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
521 return -ENOMEM;
522 }
523 hid_set_drvdata(hdev, sd);
524 sd->hsdev->hdev = hdev;
525 sd->hsdev->vendor_id = hdev->vendor;
526 sd->hsdev->product_id = hdev->product;
527 spin_lock_init(&sd->lock);
528 spin_lock_init(&sd->dyn_callback_lock);
529 mutex_init(&sd->mutex);
530 ret = hid_parse(hdev);
531 if (ret) {
532 hid_err(hdev, "parse failed\n");
533 return ret;
534 }
535 INIT_LIST_HEAD(&hdev->inputs);
536
537 ret = hid_hw_start(hdev, 0);
538 if (ret) {
539 hid_err(hdev, "hw start failed\n");
540 return ret;
541 }
542 INIT_LIST_HEAD(&sd->dyn_callback_list);
543 sd->hid_sensor_client_cnt = 0;
544 report_enum = &hdev->report_enum[HID_INPUT_REPORT];
545
546 dev_cnt = sensor_hub_get_physical_device_count(report_enum);
547 if (dev_cnt > HID_MAX_PHY_DEVICES) {
548 hid_err(hdev, "Invalid Physical device count\n");
549 ret = -EINVAL;
550 goto err_stop_hw;
551 }
552 sd->hid_sensor_hub_client_devs = kzalloc(dev_cnt *
553 sizeof(struct mfd_cell),
554 GFP_KERNEL);
555 if (sd->hid_sensor_hub_client_devs == NULL) {
556 hid_err(hdev, "Failed to allocate memory for mfd cells\n");
557 ret = -ENOMEM;
558 goto err_stop_hw;
559 }
560 list_for_each_entry(report, &report_enum->report_list, list) {
561 hid_dbg(hdev, "Report id:%x\n", report->id);
562 field = report->field[0];
563 if (report->maxfield && field &&
564 field->physical) {
565 name = kasprintf(GFP_KERNEL, "HID-SENSOR-%x",
566 field->physical);
567 if (name == NULL) {
568 hid_err(hdev, "Failed MFD device name\n");
569 ret = -ENOMEM;
570 goto err_free_names;
571 }
572 sd->hid_sensor_hub_client_devs[
573 sd->hid_sensor_client_cnt].name = name;
574 sd->hid_sensor_hub_client_devs[
575 sd->hid_sensor_client_cnt].platform_data =
576 sd->hsdev;
577 sd->hid_sensor_hub_client_devs[
578 sd->hid_sensor_client_cnt].pdata_size =
579 sizeof(*sd->hsdev);
580 hid_dbg(hdev, "Adding %s:%p\n", name, sd);
581 sd->hid_sensor_client_cnt++;
582 }
583 }
584 ret = mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs,
585 sd->hid_sensor_client_cnt, NULL, 0, NULL);
586 if (ret < 0)
587 goto err_free_names;
588
589 return ret;
590
591 err_free_names:
592 for (i = 0; i < sd->hid_sensor_client_cnt ; ++i)
593 kfree(sd->hid_sensor_hub_client_devs[i].name);
594 kfree(sd->hid_sensor_hub_client_devs);
595 err_stop_hw:
596 hid_hw_stop(hdev);
597
598 return ret;
599 }
600
601 static void sensor_hub_remove(struct hid_device *hdev)
602 {
603 struct sensor_hub_data *data = hid_get_drvdata(hdev);
604 unsigned long flags;
605 int i;
606
607 hid_dbg(hdev, " hardware removed\n");
608 hid_hw_close(hdev);
609 hid_hw_stop(hdev);
610 spin_lock_irqsave(&data->lock, flags);
611 if (data->pending.status)
612 complete(&data->pending.ready);
613 spin_unlock_irqrestore(&data->lock, flags);
614 mfd_remove_devices(&hdev->dev);
615 for (i = 0; i < data->hid_sensor_client_cnt ; ++i)
616 kfree(data->hid_sensor_hub_client_devs[i].name);
617 kfree(data->hid_sensor_hub_client_devs);
618 hid_set_drvdata(hdev, NULL);
619 mutex_destroy(&data->mutex);
620 }
621
622 static const struct hid_device_id sensor_hub_devices[] = {
623 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
624 HID_ANY_ID) },
625 { }
626 };
627 MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
628
629 static struct hid_driver sensor_hub_driver = {
630 .name = "hid-sensor-hub",
631 .id_table = sensor_hub_devices,
632 .probe = sensor_hub_probe,
633 .remove = sensor_hub_remove,
634 .raw_event = sensor_hub_raw_event,
635 #ifdef CONFIG_PM
636 .suspend = sensor_hub_suspend,
637 .resume = sensor_hub_resume,
638 .reset_resume = sensor_hub_reset_resume,
639 #endif
640 };
641 module_hid_driver(sensor_hub_driver);
642
643 MODULE_DESCRIPTION("HID Sensor Hub driver");
644 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
645 MODULE_LICENSE("GPL");
This page took 0.043258 seconds and 6 git commands to generate.