rpm-smd-debug.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "rpm-smd-debug: %s(): " fmt, __func__
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/list.h>
  12. #include <linux/io.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/slab.h>
  15. #include <soc/qcom/rpm-smd.h>
  16. #define MAX_MSG_BUFFER 350
  17. #define MAX_KEY_VALUE_PAIRS 20
  18. static struct dentry *rpm_debugfs_dir;
  19. static u32 string_to_uint(const u8 *str)
  20. {
  21. int i, len;
  22. u32 output = 0;
  23. len = strnlen(str, sizeof(u32));
  24. for (i = 0; i < len; i++)
  25. output |= str[i] << (i * 8);
  26. return output;
  27. }
  28. static ssize_t rsc_ops_write(struct file *fp, const char __user *user_buffer,
  29. size_t count, loff_t *position)
  30. {
  31. char buf[MAX_MSG_BUFFER], rsc_type_str[6] = {}, rpm_set[8] = {},
  32. key_str[6] = {};
  33. int i, pos = -1, set = -1, nelems = -1;
  34. char *cmp;
  35. uint32_t rsc_type = 0, rsc_id = 0, key = 0, data = 0;
  36. struct msm_rpm_request *req;
  37. count = min(count, sizeof(buf) - 1);
  38. if (copy_from_user(&buf, user_buffer, count))
  39. return -EFAULT;
  40. buf[count] = '\0';
  41. cmp = strstrip(buf);
  42. if (sscanf(cmp, "%7s %5s %u %d %n", rpm_set, rsc_type_str,
  43. &rsc_id, &nelems, &pos) != 4) {
  44. pr_err("Invalid number of arguments passed\n");
  45. goto err;
  46. }
  47. if (strlen(rpm_set) > 6 || strlen(rsc_type_str) > 4) {
  48. pr_err("Invalid value of set or resource type\n");
  49. goto err;
  50. }
  51. if (!strcmp(rpm_set, "active"))
  52. set = 0;
  53. else if (!strcmp(rpm_set, "sleep"))
  54. set = 1;
  55. rsc_type = string_to_uint(rsc_type_str);
  56. if (set < 0 || nelems < 0) {
  57. pr_err("Invalid value of set or nelems\n");
  58. goto err;
  59. }
  60. if (nelems > MAX_KEY_VALUE_PAIRS) {
  61. pr_err("Exceeded max no of key-value entries\n");
  62. goto err;
  63. }
  64. req = msm_rpm_create_request(set, rsc_type, rsc_id, nelems);
  65. if (!req)
  66. return -ENOMEM;
  67. for (i = 0; i < nelems; i++) {
  68. cmp += pos;
  69. if (sscanf(cmp, "%5s %n", key_str, &pos) != 1) {
  70. pr_err("Invalid number of arguments passed\n");
  71. goto err_request;
  72. }
  73. if (strlen(key_str) > 4) {
  74. pr_err("Key value cannot be more than 4 charecters\n");
  75. goto err_request;
  76. }
  77. key = string_to_uint(key_str);
  78. if (!key) {
  79. pr_err("Key values entered incorrectly\n");
  80. goto err_request;
  81. }
  82. cmp += pos;
  83. if (sscanf(cmp, "%u %n", &data, &pos) != 1) {
  84. pr_err("Invalid number of arguments passed\n");
  85. goto err_request;
  86. }
  87. if (msm_rpm_add_kvp_data(req, key,
  88. (void *)&data, sizeof(data)))
  89. goto err_request;
  90. }
  91. if (msm_rpm_wait_for_ack(msm_rpm_send_request(req)))
  92. pr_err("Sending the RPM message failed\n");
  93. err_request:
  94. msm_rpm_free_request(req);
  95. err:
  96. return count;
  97. }
  98. static const struct file_operations rsc_ops = {
  99. .write = rsc_ops_write,
  100. };
  101. static int __init rpm_smd_debugfs_init(void)
  102. {
  103. rpm_debugfs_dir = debugfs_create_dir("rpm_send_msg", NULL);
  104. if (!rpm_debugfs_dir)
  105. return -ENOMEM;
  106. if (!debugfs_create_file("message", 0200, rpm_debugfs_dir, NULL,
  107. &rsc_ops))
  108. return -ENOMEM;
  109. return 0;
  110. }
  111. late_initcall(rpm_smd_debugfs_init);
  112. static void __exit rpm_smd_debugfs_exit(void)
  113. {
  114. debugfs_remove_recursive(rpm_debugfs_dir);
  115. }
  116. module_exit(rpm_smd_debugfs_exit);
  117. MODULE_DESCRIPTION("RPM SMD Debug Driver");
  118. MODULE_LICENSE("GPL");