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
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>
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 {
40 struct completion ready;
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;
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;
70 * struct hid_sensor_hub_callbacks_list - Stores callback list
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;
79 struct hid_sensor_hub_callbacks *usage_callback;
83 static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
86 struct hid_report *report;
88 list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
92 hid_warn(hdev, "No report with id 0x%x found\n", id);
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;
104 list_for_each_entry(report, &report_enum->report_list, list) {
105 field = report->field[0];
106 if (report->maxfield && field && field->physical)
113 static void sensor_hub_fill_attr_info(
114 struct hid_sensor_hub_attribute_info *info,
115 s32 index, s32 report_id, struct hid_field *field)
118 info->report_id = report_id;
119 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;
126 static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
127 struct hid_device *hdev,
128 u32 usage_id, void **priv)
130 struct hid_sensor_hub_callbacks_list *callback;
131 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
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;
140 spin_unlock(&pdata->dyn_callback_lock);
145 int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
147 struct hid_sensor_hub_callbacks *usage_callback)
149 struct hid_sensor_hub_callbacks_list *callback;
150 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
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);
158 callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
160 spin_unlock(&pdata->dyn_callback_lock);
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);
171 EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
173 int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
176 struct hid_sensor_hub_callbacks_list *callback;
177 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
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);
186 spin_unlock(&pdata->dyn_callback_lock);
190 EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
192 int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
193 u32 field_index, s32 value)
195 struct hid_report *report;
196 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
199 mutex_lock(&data->mutex);
200 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
201 if (!report || (field_index >= report->maxfield)) {
205 hid_set_field(report->field[field_index], 0, value);
206 hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
207 hid_hw_wait(hsdev->hdev);
210 mutex_unlock(&data->mutex);
214 EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
216 int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
217 u32 field_index, s32 *value)
219 struct hid_report *report;
220 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
223 mutex_lock(&data->mutex);
224 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
225 if (!report || (field_index >= report->maxfield) ||
226 report->field[field_index]->report_count < 1) {
230 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
231 hid_hw_wait(hsdev->hdev);
232 *value = report->field[field_index]->value[0];
235 mutex_unlock(&data->mutex);
239 EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
242 int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
244 u32 attr_usage_id, u32 report_id)
246 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
248 struct hid_report *report;
251 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;
258 spin_lock_irqsave(&data->lock, flags);
259 data->pending.status = true;
260 report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
262 spin_unlock_irqrestore(&data->lock, flags);
265 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
266 spin_unlock_irqrestore(&data->lock, flags);
267 wait_for_completion_interruptible_timeout(&data->pending.ready, HZ*5);
268 switch (data->pending.raw_size) {
270 ret_val = *(u8 *)data->pending.raw_data;
273 ret_val = *(u16 *)data->pending.raw_data;
276 ret_val = *(u32 *)data->pending.raw_data;
281 kfree(data->pending.raw_data);
284 data->pending.status = false;
285 mutex_unlock(&data->mutex);
289 EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
291 int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
295 struct hid_sensor_hub_attribute_info *info)
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;
305 /* Initialize with defaults */
306 info->usage_id = usage_id;
307 info->attrib_id = attr_usage_id;
308 info->report_id = -1;
311 info->unit_expo = -1;
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;
320 if (collection_index == -1)
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,
333 for (j = 0; j < field->maxusage; ++j) {
334 if (field->usage[j].hid ==
336 field->usage[j].collection_index ==
338 sensor_hub_fill_attr_info(info,
339 i, report->id, field);
353 EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
356 static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
358 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
359 struct hid_sensor_hub_callbacks_list *callback;
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);
368 spin_unlock(&pdata->dyn_callback_lock);
373 static int sensor_hub_resume(struct hid_device *hdev)
375 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
376 struct hid_sensor_hub_callbacks_list *callback;
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);
385 spin_unlock(&pdata->dyn_callback_lock);
390 static int sensor_hub_reset_resume(struct hid_device *hdev)
397 * Handle raw report as sent by device
399 static int sensor_hub_raw_event(struct hid_device *hdev,
400 struct hid_report *report, u8 *raw_data, int size)
405 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
407 struct hid_sensor_hub_callbacks *callback = NULL;
408 struct hid_collection *collection = NULL;
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)
418 ptr++; /* Skip report id */
420 spin_lock_irqsave(&pdata->lock, flags);
422 for (i = 0; i < report->maxfield; ++i) {
423 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,
426 (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;
430 if (pdata->pending.status && pdata->pending.attr_usage_id ==
431 report->field[i]->usage->hid) {
432 hid_dbg(hdev, "data was pending ...\n");
433 pdata->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
434 if (pdata->pending.raw_data)
435 pdata->pending.raw_size = sz;
437 pdata->pending.raw_size = 0;
438 complete(&pdata->pending.ready);
440 collection = &hdev->collection[
441 report->field[i]->usage->collection_index];
442 hid_dbg(hdev, "collection->usage %x\n",
444 callback = sensor_hub_get_callback(pdata->hsdev->hdev,
445 report->field[i]->physical,
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,
453 callback->capture_sample(pdata->hsdev,
454 report->field[i]->usage->hid, sz, ptr,
459 if (callback && collection && callback->send_event)
460 callback->send_event(pdata->hsdev, collection->usage,
462 spin_unlock_irqrestore(&pdata->lock, flags);
467 int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
470 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
472 mutex_lock(&data->mutex);
473 if (!hsdev->ref_cnt) {
474 ret = hid_hw_open(hsdev->hdev);
476 hid_err(hsdev->hdev, "failed to open hid device\n");
477 mutex_unlock(&data->mutex);
482 mutex_unlock(&data->mutex);
486 EXPORT_SYMBOL_GPL(sensor_hub_device_open);
488 void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
490 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
492 mutex_lock(&data->mutex);
495 hid_hw_close(hsdev->hdev);
496 mutex_unlock(&data->mutex);
498 EXPORT_SYMBOL_GPL(sensor_hub_device_close);
500 static int sensor_hub_probe(struct hid_device *hdev,
501 const struct hid_device_id *id)
504 struct sensor_hub_data *sd;
507 struct hid_report *report;
508 struct hid_report_enum *report_enum;
509 struct hid_field *field;
512 sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
514 hid_err(hdev, "cannot allocate Sensor data\n");
517 sd->hsdev = devm_kzalloc(&hdev->dev, sizeof(*sd->hsdev), GFP_KERNEL);
519 hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
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);
531 hid_err(hdev, "parse failed\n");
534 INIT_LIST_HEAD(&hdev->inputs);
536 ret = hid_hw_start(hdev, 0);
538 hid_err(hdev, "hw start failed\n");
541 INIT_LIST_HEAD(&sd->dyn_callback_list);
542 sd->hid_sensor_client_cnt = 0;
543 report_enum = &hdev->report_enum[HID_INPUT_REPORT];
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");
551 sd->hid_sensor_hub_client_devs = kzalloc(dev_cnt *
552 sizeof(struct mfd_cell),
554 if (sd->hid_sensor_hub_client_devs == NULL) {
555 hid_err(hdev, "Failed to allocate memory for mfd cells\n");
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 &&
564 name = kasprintf(GFP_KERNEL, "HID-SENSOR-%x",
567 hid_err(hdev, "Failed MFD device name\n");
571 sd->hid_sensor_hub_client_devs[
572 sd->hid_sensor_client_cnt].id = PLATFORM_DEVID_AUTO;
573 sd->hid_sensor_hub_client_devs[
574 sd->hid_sensor_client_cnt].name = name;
575 sd->hid_sensor_hub_client_devs[
576 sd->hid_sensor_client_cnt].platform_data =
578 sd->hid_sensor_hub_client_devs[
579 sd->hid_sensor_client_cnt].pdata_size =
581 hid_dbg(hdev, "Adding %s:%p\n", name, sd);
582 sd->hid_sensor_client_cnt++;
585 ret = mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs,
586 sd->hid_sensor_client_cnt, NULL, 0, NULL);
593 for (i = 0; i < sd->hid_sensor_client_cnt ; ++i)
594 kfree(sd->hid_sensor_hub_client_devs[i].name);
595 kfree(sd->hid_sensor_hub_client_devs);
602 static void sensor_hub_remove(struct hid_device *hdev)
604 struct sensor_hub_data *data = hid_get_drvdata(hdev);
608 hid_dbg(hdev, " hardware removed\n");
611 spin_lock_irqsave(&data->lock, flags);
612 if (data->pending.status)
613 complete(&data->pending.ready);
614 spin_unlock_irqrestore(&data->lock, flags);
615 mfd_remove_devices(&hdev->dev);
616 for (i = 0; i < data->hid_sensor_client_cnt ; ++i)
617 kfree(data->hid_sensor_hub_client_devs[i].name);
618 kfree(data->hid_sensor_hub_client_devs);
619 hid_set_drvdata(hdev, NULL);
620 mutex_destroy(&data->mutex);
623 static const struct hid_device_id sensor_hub_devices[] = {
624 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
628 MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
630 static struct hid_driver sensor_hub_driver = {
631 .name = "hid-sensor-hub",
632 .id_table = sensor_hub_devices,
633 .probe = sensor_hub_probe,
634 .remove = sensor_hub_remove,
635 .raw_event = sensor_hub_raw_event,
637 .suspend = sensor_hub_suspend,
638 .resume = sensor_hub_resume,
639 .reset_resume = sensor_hub_reset_resume,
642 module_hid_driver(sensor_hub_driver);
644 MODULE_DESCRIPTION("HID Sensor Hub driver");
645 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
646 MODULE_LICENSE("GPL");