Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 1 | /* |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 2 | * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 16 | #undef DEBUG |
| 17 | |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/completion.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | |
| 24 | #include <mach/msm_smd.h> |
| 25 | #include <mach/subsystem_notif.h> |
| 26 | |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 27 | #include "hsic_sysmon.h" |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 28 | #include "sysmon.h" |
| 29 | |
Matt Wagantall | e89b3ac | 2012-04-30 14:01:39 -0700 | [diff] [blame] | 30 | #define TX_BUF_SIZE 50 |
| 31 | #define RX_BUF_SIZE 500 |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 32 | #define TIMEOUT_MS 5000 |
| 33 | |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 34 | enum transports { |
| 35 | TRANSPORT_SMD, |
| 36 | TRANSPORT_HSIC, |
| 37 | }; |
| 38 | |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 39 | struct sysmon_subsys { |
| 40 | struct mutex lock; |
| 41 | struct smd_channel *chan; |
| 42 | bool chan_open; |
| 43 | struct completion resp_ready; |
Matt Wagantall | e89b3ac | 2012-04-30 14:01:39 -0700 | [diff] [blame] | 44 | char rx_buf[RX_BUF_SIZE]; |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 45 | enum transports transport; |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 48 | static struct sysmon_subsys subsys[SYSMON_NUM_SS] = { |
| 49 | [SYSMON_SS_MODEM].transport = TRANSPORT_SMD, |
| 50 | [SYSMON_SS_LPASS].transport = TRANSPORT_SMD, |
| 51 | [SYSMON_SS_WCNSS].transport = TRANSPORT_SMD, |
| 52 | [SYSMON_SS_DSPS].transport = TRANSPORT_SMD, |
| 53 | [SYSMON_SS_Q6FW].transport = TRANSPORT_SMD, |
| 54 | [SYSMON_SS_EXT_MODEM].transport = TRANSPORT_HSIC, |
| 55 | }; |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 56 | |
| 57 | static const char *notif_name[SUBSYS_NOTIF_TYPE_COUNT] = { |
| 58 | [SUBSYS_BEFORE_SHUTDOWN] = "before_shutdown", |
| 59 | [SUBSYS_AFTER_SHUTDOWN] = "after_shutdown", |
| 60 | [SUBSYS_BEFORE_POWERUP] = "before_powerup", |
| 61 | [SUBSYS_AFTER_POWERUP] = "after_powerup", |
| 62 | }; |
| 63 | |
Matt Wagantall | e89b3ac | 2012-04-30 14:01:39 -0700 | [diff] [blame] | 64 | static int sysmon_send_smd(struct sysmon_subsys *ss, const char *tx_buf, |
| 65 | size_t len) |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 66 | { |
| 67 | int ret; |
| 68 | |
| 69 | if (!ss->chan_open) |
| 70 | return -ENODEV; |
| 71 | |
| 72 | init_completion(&ss->resp_ready); |
| 73 | pr_debug("Sending SMD message: %s\n", tx_buf); |
| 74 | smd_write(ss->chan, tx_buf, len); |
| 75 | ret = wait_for_completion_timeout(&ss->resp_ready, |
| 76 | msecs_to_jiffies(TIMEOUT_MS)); |
| 77 | if (!ret) |
| 78 | return -ETIMEDOUT; |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
Matt Wagantall | e89b3ac | 2012-04-30 14:01:39 -0700 | [diff] [blame] | 83 | static int sysmon_send_hsic(struct sysmon_subsys *ss, const char *tx_buf, |
| 84 | size_t len) |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 85 | { |
| 86 | int ret; |
| 87 | size_t actual_len; |
| 88 | |
| 89 | pr_debug("Sending HSIC message: %s\n", tx_buf); |
| 90 | ret = hsic_sysmon_write(HSIC_SYSMON_DEV_EXT_MODEM, |
| 91 | tx_buf, len, TIMEOUT_MS); |
| 92 | if (ret) |
| 93 | return ret; |
| 94 | ret = hsic_sysmon_read(HSIC_SYSMON_DEV_EXT_MODEM, ss->rx_buf, |
| 95 | ARRAY_SIZE(ss->rx_buf), &actual_len, TIMEOUT_MS); |
| 96 | return ret; |
| 97 | } |
| 98 | |
Matt Wagantall | e89b3ac | 2012-04-30 14:01:39 -0700 | [diff] [blame] | 99 | static int sysmon_send_msg(struct sysmon_subsys *ss, const char *tx_buf, |
| 100 | size_t len) |
| 101 | { |
| 102 | int ret; |
| 103 | |
| 104 | switch (ss->transport) { |
| 105 | case TRANSPORT_SMD: |
| 106 | ret = sysmon_send_smd(ss, tx_buf, len); |
| 107 | break; |
| 108 | case TRANSPORT_HSIC: |
| 109 | ret = sysmon_send_hsic(ss, tx_buf, len); |
| 110 | break; |
| 111 | default: |
| 112 | ret = -EINVAL; |
| 113 | } |
| 114 | |
| 115 | if (!ret) |
| 116 | pr_debug("Received response: %s\n", ss->rx_buf); |
| 117 | |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * sysmon_send_event() - Notify a subsystem of another's state change |
| 123 | * @dest_ss: ID of subsystem the notification should be sent to |
| 124 | * @event_ss: String name of the subsystem that generated the notification |
| 125 | * @notif: ID of the notification type (ex. SUBSYS_BEFORE_SHUTDOWN) |
| 126 | * |
| 127 | * Returns 0 for success, -EINVAL for invalid destination or notification IDs, |
| 128 | * -ENODEV if the transport channel is not open, -ETIMEDOUT if the destination |
| 129 | * subsystem does not respond, and -ENOSYS if the destination subsystem |
| 130 | * responds, but with something other than an acknowledgement. |
| 131 | * |
| 132 | * If CONFIG_MSM_SYSMON_COMM is not defined, always return success (0). |
| 133 | */ |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 134 | int sysmon_send_event(enum subsys_id dest_ss, const char *event_ss, |
| 135 | enum subsys_notif_type notif) |
| 136 | { |
| 137 | struct sysmon_subsys *ss = &subsys[dest_ss]; |
Matt Wagantall | e89b3ac | 2012-04-30 14:01:39 -0700 | [diff] [blame] | 138 | char tx_buf[TX_BUF_SIZE]; |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 139 | int ret; |
| 140 | |
| 141 | if (dest_ss < 0 || dest_ss >= SYSMON_NUM_SS || |
| 142 | notif < 0 || notif >= SUBSYS_NOTIF_TYPE_COUNT || |
| 143 | event_ss == NULL) |
| 144 | return -EINVAL; |
| 145 | |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 146 | snprintf(tx_buf, ARRAY_SIZE(tx_buf), "ssr:%s:%s", event_ss, |
| 147 | notif_name[notif]); |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 148 | |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 149 | mutex_lock(&ss->lock); |
Matt Wagantall | e89b3ac | 2012-04-30 14:01:39 -0700 | [diff] [blame] | 150 | ret = sysmon_send_msg(ss, tx_buf, strlen(tx_buf)); |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 151 | if (ret) |
| 152 | goto out; |
| 153 | |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 154 | if (strncmp(ss->rx_buf, "ssr:ack", ARRAY_SIZE(ss->rx_buf))) |
| 155 | ret = -ENOSYS; |
Matt Wagantall | e89b3ac | 2012-04-30 14:01:39 -0700 | [diff] [blame] | 156 | out: |
| 157 | mutex_unlock(&ss->lock); |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * sysmon_get_reason() - Retrieve failure reason from a subsystem. |
| 163 | * @dest_ss: ID of subsystem to query |
| 164 | * @buf: Caller-allocated buffer for the returned NUL-terminated reason |
| 165 | * @len: Length of @buf |
| 166 | * |
| 167 | * Returns 0 for success, -EINVAL for an invalid destination, -ENODEV if |
| 168 | * the SMD transport channel is not open, -ETIMEDOUT if the destination |
| 169 | * subsystem does not respond, and -ENOSYS if the destination subsystem |
| 170 | * responds with something unexpected. |
| 171 | * |
| 172 | * If CONFIG_MSM_SYSMON_COMM is not defined, always return success (0). |
| 173 | */ |
| 174 | int sysmon_get_reason(enum subsys_id dest_ss, char *buf, size_t len) |
| 175 | { |
| 176 | struct sysmon_subsys *ss = &subsys[dest_ss]; |
| 177 | const char tx_buf[] = "ssr:retrieve:sfr"; |
| 178 | const char expect[] = "ssr:return:"; |
| 179 | size_t prefix_len = ARRAY_SIZE(expect) - 1; |
| 180 | int ret; |
| 181 | |
| 182 | if (dest_ss < 0 || dest_ss >= SYSMON_NUM_SS || |
| 183 | buf == NULL || len == 0) |
| 184 | return -EINVAL; |
| 185 | |
| 186 | mutex_lock(&ss->lock); |
| 187 | ret = sysmon_send_msg(ss, tx_buf, ARRAY_SIZE(tx_buf)); |
| 188 | if (ret) |
| 189 | goto out; |
| 190 | |
| 191 | if (strncmp(ss->rx_buf, expect, prefix_len)) { |
| 192 | ret = -ENOSYS; |
| 193 | goto out; |
| 194 | } |
| 195 | strlcpy(buf, ss->rx_buf + prefix_len, len); |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 196 | out: |
| 197 | mutex_unlock(&ss->lock); |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 198 | return ret; |
| 199 | } |
| 200 | |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 201 | static void sysmon_smd_notify(void *priv, unsigned int smd_event) |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 202 | { |
| 203 | struct sysmon_subsys *ss = priv; |
| 204 | |
| 205 | switch (smd_event) { |
| 206 | case SMD_EVENT_DATA: { |
| 207 | if (smd_read_avail(ss->chan) > 0) { |
| 208 | smd_read_from_cb(ss->chan, ss->rx_buf, |
| 209 | ARRAY_SIZE(ss->rx_buf)); |
| 210 | complete(&ss->resp_ready); |
| 211 | } |
| 212 | break; |
| 213 | } |
| 214 | case SMD_EVENT_OPEN: |
| 215 | ss->chan_open = true; |
| 216 | break; |
| 217 | case SMD_EVENT_CLOSE: |
| 218 | ss->chan_open = false; |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | static int sysmon_probe(struct platform_device *pdev) |
| 224 | { |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 225 | struct sysmon_subsys *ss; |
| 226 | int ret; |
| 227 | |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 228 | if (pdev->id < 0 || pdev->id >= SYSMON_NUM_SS) |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 229 | return -ENODEV; |
| 230 | |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 231 | ss = &subsys[pdev->id]; |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 232 | mutex_init(&ss->lock); |
| 233 | |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 234 | switch (ss->transport) { |
| 235 | case TRANSPORT_SMD: |
| 236 | if (pdev->id >= SMD_NUM_TYPE) |
| 237 | return -EINVAL; |
| 238 | |
| 239 | ret = smd_named_open_on_edge("sys_mon", pdev->id, &ss->chan, ss, |
| 240 | sysmon_smd_notify); |
| 241 | if (ret) { |
| 242 | pr_err("SMD open failed\n"); |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | smd_disable_read_intr(ss->chan); |
| 247 | break; |
| 248 | case TRANSPORT_HSIC: |
| 249 | if (pdev->id < SMD_NUM_TYPE) |
| 250 | return -EINVAL; |
| 251 | |
| 252 | ret = hsic_sysmon_open(HSIC_SYSMON_DEV_EXT_MODEM); |
| 253 | if (ret) { |
| 254 | pr_err("HSIC open failed\n"); |
| 255 | return ret; |
| 256 | } |
| 257 | break; |
| 258 | default: |
| 259 | return -EINVAL; |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 260 | } |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int __devexit sysmon_remove(struct platform_device *pdev) |
| 266 | { |
Matt Wagantall | c3d9a2f | 2012-04-13 12:39:17 -0700 | [diff] [blame] | 267 | struct sysmon_subsys *ss = &subsys[pdev->id]; |
| 268 | |
| 269 | switch (ss->transport) { |
| 270 | case TRANSPORT_SMD: |
| 271 | smd_close(ss->chan); |
| 272 | break; |
| 273 | case TRANSPORT_HSIC: |
| 274 | hsic_sysmon_close(HSIC_SYSMON_DEV_EXT_MODEM); |
| 275 | break; |
| 276 | } |
| 277 | |
Matt Wagantall | f802090 | 2011-08-30 21:19:23 -0700 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static struct platform_driver sysmon_driver = { |
| 282 | .probe = sysmon_probe, |
| 283 | .remove = __devexit_p(sysmon_remove), |
| 284 | .driver = { |
| 285 | .name = "sys_mon", |
| 286 | .owner = THIS_MODULE, |
| 287 | }, |
| 288 | }; |
| 289 | |
| 290 | static int __init sysmon_init(void) |
| 291 | { |
| 292 | return platform_driver_register(&sysmon_driver); |
| 293 | } |
| 294 | subsys_initcall(sysmon_init); |
| 295 | |
| 296 | static void __exit sysmon_exit(void) |
| 297 | { |
| 298 | platform_driver_unregister(&sysmon_driver); |
| 299 | } |
| 300 | module_exit(sysmon_exit); |
| 301 | |
| 302 | MODULE_LICENSE("GPL v2"); |
| 303 | MODULE_DESCRIPTION("system monitor communication library"); |
| 304 | MODULE_ALIAS("platform:sys_mon"); |