Raju P.L.S.S.S.N | 43c1be7 | 2017-10-31 16:50:30 +0530 | [diff] [blame] | 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 | |
| 13 | #define pr_fmt(fmt) "rpm-smd-debug: %s(): " fmt, __func__ |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/debugfs.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <soc/qcom/rpm-smd.h> |
| 24 | |
| 25 | #define MAX_MSG_BUFFER 350 |
| 26 | #define MAX_KEY_VALUE_PAIRS 20 |
| 27 | |
| 28 | static struct dentry *rpm_debugfs_dir; |
| 29 | |
| 30 | static u32 string_to_uint(const u8 *str) |
| 31 | { |
| 32 | int i, len; |
| 33 | u32 output = 0; |
| 34 | |
| 35 | len = strnlen(str, sizeof(u32)); |
| 36 | for (i = 0; i < len; i++) |
| 37 | output |= str[i] << (i * 8); |
| 38 | |
| 39 | return output; |
| 40 | } |
| 41 | |
| 42 | static ssize_t rsc_ops_write(struct file *fp, const char __user *user_buffer, |
| 43 | size_t count, loff_t *position) |
| 44 | { |
| 45 | char buf[MAX_MSG_BUFFER], rsc_type_str[6] = {}, rpm_set[8] = {}, |
| 46 | key_str[6] = {}; |
| 47 | int i, pos = -1, set = -1, nelems = -1; |
| 48 | char *cmp; |
| 49 | uint32_t rsc_type = 0, rsc_id = 0, key = 0, data = 0; |
| 50 | struct msm_rpm_request *req; |
| 51 | |
| 52 | count = min(count, sizeof(buf) - 1); |
| 53 | if (copy_from_user(&buf, user_buffer, count)) |
| 54 | return -EFAULT; |
| 55 | buf[count] = '\0'; |
| 56 | cmp = strstrip(buf); |
| 57 | |
| 58 | if (sscanf(cmp, "%7s %5s %u %d %n", rpm_set, rsc_type_str, |
| 59 | &rsc_id, &nelems, &pos) != 4) { |
| 60 | pr_err("Invalid number of arguments passed\n"); |
| 61 | goto err; |
| 62 | } |
| 63 | |
| 64 | if (strlen(rpm_set) > 6 || strlen(rsc_type_str) > 4) { |
| 65 | pr_err("Invalid value of set or resource type\n"); |
| 66 | goto err; |
| 67 | } |
| 68 | |
| 69 | if (!strcmp(rpm_set, "active")) |
| 70 | set = 0; |
| 71 | else if (!strcmp(rpm_set, "sleep")) |
| 72 | set = 1; |
| 73 | |
| 74 | rsc_type = string_to_uint(rsc_type_str); |
| 75 | |
| 76 | if (set < 0 || nelems < 0) { |
| 77 | pr_err("Invalid value of set or nelems\n"); |
| 78 | goto err; |
| 79 | } |
| 80 | if (nelems > MAX_KEY_VALUE_PAIRS) { |
| 81 | pr_err("Exceeded max no of key-value entries\n"); |
| 82 | goto err; |
| 83 | } |
| 84 | |
| 85 | req = msm_rpm_create_request(set, rsc_type, rsc_id, nelems); |
| 86 | if (!req) |
| 87 | return -ENOMEM; |
| 88 | |
| 89 | for (i = 0; i < nelems; i++) { |
| 90 | cmp += pos; |
| 91 | if (sscanf(cmp, "%5s %n", key_str, &pos) != 1) { |
| 92 | pr_err("Invalid number of arguments passed\n"); |
| 93 | goto err; |
| 94 | } |
| 95 | |
| 96 | if (strlen(key_str) > 4) { |
| 97 | pr_err("Key value cannot be more than 4 charecters"); |
| 98 | goto err; |
| 99 | } |
| 100 | key = string_to_uint(key_str); |
| 101 | if (!key) { |
| 102 | pr_err("Key values entered incorrectly\n"); |
| 103 | goto err; |
| 104 | } |
| 105 | |
| 106 | cmp += pos; |
| 107 | if (sscanf(cmp, "%u %n", &data, &pos) != 1) { |
| 108 | pr_err("Invalid number of arguments passed\n"); |
| 109 | goto err; |
| 110 | } |
| 111 | |
| 112 | if (msm_rpm_add_kvp_data(req, key, |
| 113 | (void *)&data, sizeof(data))) |
| 114 | goto err_request; |
| 115 | } |
| 116 | |
| 117 | if (msm_rpm_wait_for_ack(msm_rpm_send_request(req))) |
| 118 | pr_err("Sending the RPM message failed\n"); |
| 119 | |
| 120 | err_request: |
| 121 | msm_rpm_free_request(req); |
| 122 | err: |
| 123 | return count; |
| 124 | } |
| 125 | |
| 126 | static const struct file_operations rsc_ops = { |
| 127 | .write = rsc_ops_write, |
| 128 | }; |
| 129 | |
| 130 | static int __init rpm_smd_debugfs_init(void) |
| 131 | { |
| 132 | rpm_debugfs_dir = debugfs_create_dir("rpm_send_msg", NULL); |
| 133 | if (!rpm_debugfs_dir) |
| 134 | return -ENOMEM; |
| 135 | |
| 136 | if (!debugfs_create_file("message", 0200, rpm_debugfs_dir, NULL, |
| 137 | &rsc_ops)) |
| 138 | return -ENOMEM; |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | late_initcall(rpm_smd_debugfs_init); |
| 143 | |
| 144 | static void __exit rpm_smd_debugfs_exit(void) |
| 145 | { |
| 146 | debugfs_remove_recursive(rpm_debugfs_dir); |
| 147 | } |
| 148 | module_exit(rpm_smd_debugfs_exit); |
| 149 | |
| 150 | MODULE_DESCRIPTION("RPM SMD Debug Driver"); |
| 151 | MODULE_LICENSE("GPL v2"); |