1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2012, Intel Corporation.
7 #include <linux/device.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/mfd/core.h>
12 #include <linux/list.h>
13 #include <linux/hid-sensor-ids.h>
14 #include <linux/hid-sensor-hub.h>
17 #define HID_SENSOR_HUB_ENUM_QUIRK 0x01
20 * struct sensor_hub_data - Hold a instance data for a HID hub device
21 * @hsdev: Stored hid instance for current hub device.
22 * @mutex: Mutex to serialize synchronous request.
23 * @lock: Spin lock to protect pending request structure.
24 * @dyn_callback_list: Holds callback function
25 * @dyn_callback_lock: spin lock to protect callback list
26 * @hid_sensor_hub_client_devs: Stores all MFD cells for a hub instance.
27 * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
28 * @ref_cnt: Number of MFD clients have opened this device
30 struct sensor_hub_data {
33 struct list_head dyn_callback_list;
34 spinlock_t dyn_callback_lock;
35 struct mfd_cell *hid_sensor_hub_client_devs;
36 int hid_sensor_client_cnt;
42 * struct hid_sensor_hub_callbacks_list - Stores callback list
44 * @usage_id: usage id for a physical device.
45 * @usage_callback: Stores registered callback functions.
46 * @priv: Private data for a physical device.
48 struct hid_sensor_hub_callbacks_list {
49 struct list_head list;
51 struct hid_sensor_hub_device *hsdev;
52 struct hid_sensor_hub_callbacks *usage_callback;
56 static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
59 struct hid_report *report;
61 list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
65 hid_warn(hdev, "No report with id 0x%x found\n", id);
70 static int sensor_hub_get_physical_device_count(struct hid_device *hdev)
75 for (i = 0; i < hdev->maxcollection; ++i) {
76 struct hid_collection *collection = &hdev->collection[i];
77 if (collection->type == HID_COLLECTION_PHYSICAL ||
78 collection->type == HID_COLLECTION_APPLICATION)
85 static void sensor_hub_fill_attr_info(
86 struct hid_sensor_hub_attribute_info *info,
87 s32 index, s32 report_id, struct hid_field *field)
90 info->report_id = report_id;
91 info->units = field->unit;
92 info->unit_expo = field->unit_exponent;
93 info->size = (field->report_size * field->report_count)/8;
94 info->logical_minimum = field->logical_minimum;
95 info->logical_maximum = field->logical_maximum;
98 static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
99 struct hid_device *hdev,
101 int collection_index,
102 struct hid_sensor_hub_device **hsdev,
105 struct hid_sensor_hub_callbacks_list *callback;
106 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
109 spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
110 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
111 if ((callback->usage_id == usage_id ||
112 callback->usage_id == HID_USAGE_SENSOR_COLLECTION) &&
114 callback->hsdev->start_collection_index) &&
116 callback->hsdev->end_collection_index)) {
117 *priv = callback->priv;
118 *hsdev = callback->hsdev;
119 spin_unlock_irqrestore(&pdata->dyn_callback_lock,
121 return callback->usage_callback;
123 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
128 int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
130 struct hid_sensor_hub_callbacks *usage_callback)
132 struct hid_sensor_hub_callbacks_list *callback;
133 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
136 spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
137 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
138 if (callback->usage_id == usage_id &&
139 callback->hsdev == hsdev) {
140 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
143 callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
145 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
148 callback->hsdev = hsdev;
149 callback->usage_callback = usage_callback;
150 callback->usage_id = usage_id;
151 callback->priv = NULL;
153 * If there is a handler registered for the collection type, then
154 * it will handle all reports for sensors in this collection. If
155 * there is also an individual sensor handler registration, then
156 * we want to make sure that the reports are directed to collection
157 * handler, as this may be a fusion sensor. So add collection handlers
158 * to the beginning of the list, so that they are matched first.
160 if (usage_id == HID_USAGE_SENSOR_COLLECTION)
161 list_add(&callback->list, &pdata->dyn_callback_list);
163 list_add_tail(&callback->list, &pdata->dyn_callback_list);
164 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
168 EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
170 int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
173 struct hid_sensor_hub_callbacks_list *callback;
174 struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
177 spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
178 list_for_each_entry(callback, &pdata->dyn_callback_list, list)
179 if (callback->usage_id == usage_id &&
180 callback->hsdev == hsdev) {
181 list_del(&callback->list);
185 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
189 EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
191 int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
192 u32 field_index, int buffer_size, void *buffer)
194 struct hid_report *report;
195 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
196 __s32 *buf32 = buffer;
202 mutex_lock(&data->mutex);
203 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
204 if (!report || (field_index >= report->maxfield)) {
209 remaining_bytes = buffer_size % sizeof(__s32);
210 buffer_size = buffer_size / sizeof(__s32);
212 for (i = 0; i < buffer_size; ++i) {
213 hid_set_field(report->field[field_index], i,
214 (__force __s32)cpu_to_le32(*buf32));
218 if (remaining_bytes) {
220 memcpy(&value, (u8 *)buf32, remaining_bytes);
221 hid_set_field(report->field[field_index], i,
222 (__force __s32)cpu_to_le32(value));
224 hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
225 hid_hw_wait(hsdev->hdev);
228 mutex_unlock(&data->mutex);
232 EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
234 int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
235 u32 field_index, int buffer_size, void *buffer)
237 struct hid_report *report;
238 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
242 int buffer_index = 0;
245 memset(buffer, 0, buffer_size);
247 mutex_lock(&data->mutex);
248 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
249 if (!report || (field_index >= report->maxfield) ||
250 report->field[field_index]->report_count < 1) {
254 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
255 hid_hw_wait(hsdev->hdev);
257 /* calculate number of bytes required to read this field */
258 report_size = DIV_ROUND_UP(report->field[field_index]->report_size,
260 report->field[field_index]->report_count;
265 ret = min(report_size, buffer_size);
267 val_ptr = (u8 *)report->field[field_index]->value;
268 for (i = 0; i < report->field[field_index]->report_count; ++i) {
269 if (buffer_index >= ret)
272 memcpy(&((u8 *)buffer)[buffer_index], val_ptr,
273 report->field[field_index]->report_size / 8);
274 val_ptr += sizeof(__s32);
275 buffer_index += (report->field[field_index]->report_size / 8);
279 mutex_unlock(&data->mutex);
283 EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
286 int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
288 u32 attr_usage_id, u32 report_id,
289 enum sensor_hub_read_flags flag,
292 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
294 struct hid_report *report;
297 report = sensor_hub_report(report_id, hsdev->hdev,
302 mutex_lock(hsdev->mutex_ptr);
303 if (flag == SENSOR_HUB_SYNC) {
304 memset(&hsdev->pending, 0, sizeof(hsdev->pending));
305 init_completion(&hsdev->pending.ready);
306 hsdev->pending.usage_id = usage_id;
307 hsdev->pending.attr_usage_id = attr_usage_id;
308 hsdev->pending.raw_size = 0;
310 spin_lock_irqsave(&data->lock, flags);
311 hsdev->pending.status = true;
312 spin_unlock_irqrestore(&data->lock, flags);
314 mutex_lock(&data->mutex);
315 hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
316 mutex_unlock(&data->mutex);
317 if (flag == SENSOR_HUB_SYNC) {
318 wait_for_completion_interruptible_timeout(
319 &hsdev->pending.ready, HZ*5);
320 switch (hsdev->pending.raw_size) {
323 ret_val = *(s8 *)hsdev->pending.raw_data;
325 ret_val = *(u8 *)hsdev->pending.raw_data;
329 ret_val = *(s16 *)hsdev->pending.raw_data;
331 ret_val = *(u16 *)hsdev->pending.raw_data;
334 ret_val = *(u32 *)hsdev->pending.raw_data;
339 kfree(hsdev->pending.raw_data);
340 hsdev->pending.status = false;
342 mutex_unlock(hsdev->mutex_ptr);
346 EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
348 int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
349 u32 report_id, int field_index, u32 usage_id)
351 struct hid_report *report;
352 struct hid_field *field;
355 report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
356 if (!report || (field_index >= report->maxfield))
359 field = report->field[field_index];
360 for (i = 0; i < field->maxusage; ++i) {
361 if (field->usage[i].hid == usage_id)
362 return field->usage[i].usage_index;
368 EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index);
370 int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
374 struct hid_sensor_hub_attribute_info *info)
378 struct hid_report *report;
379 struct hid_field *field;
380 struct hid_report_enum *report_enum;
381 struct hid_device *hdev = hsdev->hdev;
383 /* Initialize with defaults */
384 info->usage_id = usage_id;
385 info->attrib_id = attr_usage_id;
386 info->report_id = -1;
389 info->unit_expo = -1;
391 report_enum = &hdev->report_enum[type];
392 list_for_each_entry(report, &report_enum->report_list, list) {
393 for (i = 0; i < report->maxfield; ++i) {
394 field = report->field[i];
395 if (field->maxusage) {
396 if (field->physical == usage_id &&
397 (field->logical == attr_usage_id ||
398 field->usage[0].hid ==
400 (field->usage[0].collection_index >=
401 hsdev->start_collection_index) &&
402 (field->usage[0].collection_index <
403 hsdev->end_collection_index)) {
405 sensor_hub_fill_attr_info(info, i,
418 EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
421 static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
423 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
424 struct hid_sensor_hub_callbacks_list *callback;
427 hid_dbg(hdev, " sensor_hub_suspend\n");
428 spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
429 list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
430 if (callback->usage_callback->suspend)
431 callback->usage_callback->suspend(
432 callback->hsdev, callback->priv);
434 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
439 static int sensor_hub_resume(struct hid_device *hdev)
441 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
442 struct hid_sensor_hub_callbacks_list *callback;
445 hid_dbg(hdev, " sensor_hub_resume\n");
446 spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
447 list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
448 if (callback->usage_callback->resume)
449 callback->usage_callback->resume(
450 callback->hsdev, callback->priv);
452 spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
457 static int sensor_hub_reset_resume(struct hid_device *hdev)
464 * Handle raw report as sent by device
466 static int sensor_hub_raw_event(struct hid_device *hdev,
467 struct hid_report *report, u8 *raw_data, int size)
472 struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
474 struct hid_sensor_hub_callbacks *callback = NULL;
475 struct hid_collection *collection = NULL;
477 struct hid_sensor_hub_device *hsdev = NULL;
479 hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
480 report->id, size, report->type);
481 hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
482 if (report->type != HID_INPUT_REPORT)
486 ptr++; /* Skip report id */
488 spin_lock_irqsave(&pdata->lock, flags);
490 for (i = 0; i < report->maxfield; ++i) {
491 hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
492 i, report->field[i]->usage->collection_index,
493 report->field[i]->usage->hid,
494 (report->field[i]->report_size *
495 report->field[i]->report_count)/8);
496 sz = (report->field[i]->report_size *
497 report->field[i]->report_count)/8;
498 collection = &hdev->collection[
499 report->field[i]->usage->collection_index];
500 hid_dbg(hdev, "collection->usage %x\n",
503 callback = sensor_hub_get_callback(hdev,
504 report->field[i]->physical,
505 report->field[i]->usage[0].collection_index,
511 if (hsdev->pending.status && (hsdev->pending.attr_usage_id ==
512 report->field[i]->usage->hid ||
513 hsdev->pending.attr_usage_id ==
514 report->field[i]->logical)) {
515 hid_dbg(hdev, "data was pending ...\n");
516 hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
517 if (hsdev->pending.raw_data)
518 hsdev->pending.raw_size = sz;
520 hsdev->pending.raw_size = 0;
521 complete(&hsdev->pending.ready);
523 if (callback->capture_sample) {
524 if (report->field[i]->logical)
525 callback->capture_sample(hsdev,
526 report->field[i]->logical, sz, ptr,
529 callback->capture_sample(hsdev,
530 report->field[i]->usage->hid, sz, ptr,
535 if (callback && collection && callback->send_event)
536 callback->send_event(hsdev, collection->usage,
538 spin_unlock_irqrestore(&pdata->lock, flags);
543 int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
546 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
548 mutex_lock(&data->mutex);
549 if (!data->ref_cnt) {
550 ret = hid_hw_open(hsdev->hdev);
552 hid_err(hsdev->hdev, "failed to open hid device\n");
553 mutex_unlock(&data->mutex);
558 mutex_unlock(&data->mutex);
562 EXPORT_SYMBOL_GPL(sensor_hub_device_open);
564 void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
566 struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
568 mutex_lock(&data->mutex);
571 hid_hw_close(hsdev->hdev);
572 mutex_unlock(&data->mutex);
574 EXPORT_SYMBOL_GPL(sensor_hub_device_close);
576 static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
580 * Checks if the report descriptor of Thinkpad Helix 2 has a logical
581 * minimum for magnetic flux axis greater than the maximum.
583 if (hdev->product == USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA &&
584 *rsize == 2558 && rdesc[913] == 0x17 && rdesc[914] == 0x40 &&
585 rdesc[915] == 0x81 && rdesc[916] == 0x08 &&
586 rdesc[917] == 0x00 && rdesc[918] == 0x27 &&
587 rdesc[921] == 0x07 && rdesc[922] == 0x00) {
588 /* Sets negative logical minimum for mag x, y and z */
589 rdesc[914] = rdesc[935] = rdesc[956] = 0xc0;
590 rdesc[915] = rdesc[936] = rdesc[957] = 0x7e;
591 rdesc[916] = rdesc[937] = rdesc[958] = 0xf7;
592 rdesc[917] = rdesc[938] = rdesc[959] = 0xff;
598 static int sensor_hub_probe(struct hid_device *hdev,
599 const struct hid_device_id *id)
602 struct sensor_hub_data *sd;
606 struct hid_sensor_hub_device *hsdev;
607 struct hid_sensor_hub_device *last_hsdev = NULL;
608 struct hid_sensor_hub_device *collection_hsdev = NULL;
610 sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
612 hid_err(hdev, "cannot allocate Sensor data\n");
616 hid_set_drvdata(hdev, sd);
617 sd->quirks = id->driver_data;
619 spin_lock_init(&sd->lock);
620 spin_lock_init(&sd->dyn_callback_lock);
621 mutex_init(&sd->mutex);
622 ret = hid_parse(hdev);
624 hid_err(hdev, "parse failed\n");
627 INIT_LIST_HEAD(&hdev->inputs);
629 ret = hid_hw_start(hdev, 0);
631 hid_err(hdev, "hw start failed\n");
634 INIT_LIST_HEAD(&sd->dyn_callback_list);
635 sd->hid_sensor_client_cnt = 0;
637 dev_cnt = sensor_hub_get_physical_device_count(hdev);
638 if (dev_cnt > HID_MAX_PHY_DEVICES) {
639 hid_err(hdev, "Invalid Physical device count\n");
643 sd->hid_sensor_hub_client_devs = devm_kcalloc(&hdev->dev,
645 sizeof(struct mfd_cell),
647 if (sd->hid_sensor_hub_client_devs == NULL) {
648 hid_err(hdev, "Failed to allocate memory for mfd cells\n");
653 for (i = 0; i < hdev->maxcollection; ++i) {
654 struct hid_collection *collection = &hdev->collection[i];
656 if (collection->type == HID_COLLECTION_PHYSICAL ||
657 collection->type == HID_COLLECTION_APPLICATION) {
659 hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev),
662 hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
667 hsdev->vendor_id = hdev->vendor;
668 hsdev->product_id = hdev->product;
669 hsdev->usage = collection->usage;
670 hsdev->mutex_ptr = devm_kzalloc(&hdev->dev,
671 sizeof(struct mutex),
673 if (!hsdev->mutex_ptr) {
677 mutex_init(hsdev->mutex_ptr);
678 hsdev->start_collection_index = i;
680 last_hsdev->end_collection_index = i;
682 name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
686 hid_err(hdev, "Failed MFD device name\n");
690 sd->hid_sensor_hub_client_devs[
691 sd->hid_sensor_client_cnt].name = name;
692 sd->hid_sensor_hub_client_devs[
693 sd->hid_sensor_client_cnt].platform_data =
695 sd->hid_sensor_hub_client_devs[
696 sd->hid_sensor_client_cnt].pdata_size =
698 hid_dbg(hdev, "Adding %s:%d\n", name,
699 hsdev->start_collection_index);
700 sd->hid_sensor_client_cnt++;
701 if (collection_hsdev)
702 collection_hsdev->end_collection_index = i;
703 if (collection->type == HID_COLLECTION_APPLICATION &&
704 collection->usage == HID_USAGE_SENSOR_COLLECTION)
705 collection_hsdev = hsdev;
709 last_hsdev->end_collection_index = i;
710 if (collection_hsdev)
711 collection_hsdev->end_collection_index = i;
713 ret = mfd_add_hotplug_devices(&hdev->dev,
714 sd->hid_sensor_hub_client_devs,
715 sd->hid_sensor_client_cnt);
727 static void sensor_hub_remove(struct hid_device *hdev)
729 struct sensor_hub_data *data = hid_get_drvdata(hdev);
733 hid_dbg(hdev, " hardware removed\n");
736 spin_lock_irqsave(&data->lock, flags);
737 for (i = 0; i < data->hid_sensor_client_cnt; ++i) {
738 struct hid_sensor_hub_device *hsdev =
739 data->hid_sensor_hub_client_devs[i].platform_data;
740 if (hsdev->pending.status)
741 complete(&hsdev->pending.ready);
743 spin_unlock_irqrestore(&data->lock, flags);
744 mfd_remove_devices(&hdev->dev);
745 mutex_destroy(&data->mutex);
748 static const struct hid_device_id sensor_hub_devices[] = {
749 { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
753 MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
755 static struct hid_driver sensor_hub_driver = {
756 .name = "hid-sensor-hub",
757 .id_table = sensor_hub_devices,
758 .probe = sensor_hub_probe,
759 .remove = sensor_hub_remove,
760 .raw_event = sensor_hub_raw_event,
761 .report_fixup = sensor_hub_report_fixup,
763 .suspend = sensor_hub_suspend,
764 .resume = sensor_hub_resume,
765 .reset_resume = sensor_hub_reset_resume,
768 module_hid_driver(sensor_hub_driver);
770 MODULE_DESCRIPTION("HID Sensor Hub driver");
771 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
772 MODULE_LICENSE("GPL");