rpm-smd-debug.c 3.6 KB

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