hid-vivaldi-common.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Helpers for ChromeOS HID Vivaldi keyboards
  4. *
  5. * Copyright (C) 2022 Google, Inc
  6. */
  7. #include <linux/export.h>
  8. #include <linux/hid.h>
  9. #include <linux/input/vivaldi-fmap.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include "hid-vivaldi-common.h"
  14. #define MIN_FN_ROW_KEY 1
  15. #define MAX_FN_ROW_KEY VIVALDI_MAX_FUNCTION_ROW_KEYS
  16. #define HID_VD_FN_ROW_PHYSMAP 0x00000001
  17. #define HID_USAGE_FN_ROW_PHYSMAP (HID_UP_GOOGLEVENDOR | HID_VD_FN_ROW_PHYSMAP)
  18. /**
  19. * vivaldi_feature_mapping - Fill out vivaldi keymap data exposed via HID
  20. * @hdev: HID device to parse
  21. * @field: HID field to parse
  22. * @usage: HID usage to parse
  23. *
  24. * Note: this function assumes that driver data attached to @hdev contains an
  25. * instance of &struct vivaldi_data at the very beginning.
  26. */
  27. void vivaldi_feature_mapping(struct hid_device *hdev,
  28. struct hid_field *field, struct hid_usage *usage)
  29. {
  30. struct vivaldi_data *data = hid_get_drvdata(hdev);
  31. struct hid_report *report = field->report;
  32. u8 *report_data, *buf;
  33. u32 report_len;
  34. unsigned int fn_key;
  35. int ret;
  36. if (field->logical != HID_USAGE_FN_ROW_PHYSMAP ||
  37. (usage->hid & HID_USAGE_PAGE) != HID_UP_ORDINAL)
  38. return;
  39. fn_key = usage->hid & HID_USAGE;
  40. if (fn_key < MIN_FN_ROW_KEY || fn_key > MAX_FN_ROW_KEY)
  41. return;
  42. if (fn_key > data->num_function_row_keys)
  43. data->num_function_row_keys = fn_key;
  44. report_data = buf = hid_alloc_report_buf(report, GFP_KERNEL);
  45. if (!report_data)
  46. return;
  47. report_len = hid_report_len(report);
  48. if (!report->id) {
  49. /*
  50. * hid_hw_raw_request() will stuff report ID (which will be 0)
  51. * into the first byte of the buffer even for unnumbered
  52. * reports, so we need to account for this to avoid getting
  53. * -EOVERFLOW in return.
  54. * Note that hid_alloc_report_buf() adds 7 bytes to the size
  55. * so we can safely say that we have space for an extra byte.
  56. */
  57. report_len++;
  58. }
  59. ret = hid_hw_raw_request(hdev, report->id, report_data,
  60. report_len, HID_FEATURE_REPORT,
  61. HID_REQ_GET_REPORT);
  62. if (ret < 0) {
  63. dev_warn(&hdev->dev, "failed to fetch feature %d\n",
  64. field->report->id);
  65. goto out;
  66. }
  67. if (!report->id) {
  68. /*
  69. * Undo the damage from hid_hw_raw_request() for unnumbered
  70. * reports.
  71. */
  72. report_data++;
  73. report_len--;
  74. }
  75. ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, report_data,
  76. report_len, 0);
  77. if (ret) {
  78. dev_warn(&hdev->dev, "failed to report feature %d\n",
  79. field->report->id);
  80. goto out;
  81. }
  82. data->function_row_physmap[fn_key - MIN_FN_ROW_KEY] =
  83. field->value[usage->usage_index];
  84. out:
  85. kfree(buf);
  86. }
  87. EXPORT_SYMBOL_GPL(vivaldi_feature_mapping);
  88. static ssize_t function_row_physmap_show(struct device *dev,
  89. struct device_attribute *attr,
  90. char *buf)
  91. {
  92. struct hid_device *hdev = to_hid_device(dev);
  93. struct vivaldi_data *data = hid_get_drvdata(hdev);
  94. return vivaldi_function_row_physmap_show(data, buf);
  95. }
  96. static DEVICE_ATTR_RO(function_row_physmap);
  97. static struct attribute *vivaldi_sysfs_attrs[] = {
  98. &dev_attr_function_row_physmap.attr,
  99. NULL
  100. };
  101. static umode_t vivaldi_is_visible(struct kobject *kobj, struct attribute *attr,
  102. int n)
  103. {
  104. struct hid_device *hdev = to_hid_device(kobj_to_dev(kobj));
  105. struct vivaldi_data *data = hid_get_drvdata(hdev);
  106. if (!data->num_function_row_keys)
  107. return 0;
  108. return attr->mode;
  109. }
  110. static const struct attribute_group vivaldi_attribute_group = {
  111. .attrs = vivaldi_sysfs_attrs,
  112. .is_visible = vivaldi_is_visible,
  113. };
  114. const struct attribute_group *vivaldi_attribute_groups[] = {
  115. &vivaldi_attribute_group,
  116. NULL,
  117. };
  118. EXPORT_SYMBOL_GPL(vivaldi_attribute_groups);
  119. MODULE_LICENSE("GPL");