blob: 78290a263448365f5b29f73d76bee75f2ebb4237 [file] [log] [blame]
Matt Wagantallf8020902011-08-30 21:19:23 -07001/*
Matt Wagantallc3d9a2f2012-04-13 12:39:17 -07002 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Matt Wagantallf8020902011-08-30 21:19:23 -07003 *
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 Wagantallc3d9a2f2012-04-13 12:39:17 -070027#include "hsic_sysmon.h"
Matt Wagantallf8020902011-08-30 21:19:23 -070028#include "sysmon.h"
29
Matt Wagantalle89b3ac2012-04-30 14:01:39 -070030#define TX_BUF_SIZE 50
31#define RX_BUF_SIZE 500
Matt Wagantallf8020902011-08-30 21:19:23 -070032#define TIMEOUT_MS 5000
33
Matt Wagantallc3d9a2f2012-04-13 12:39:17 -070034enum transports {
35 TRANSPORT_SMD,
36 TRANSPORT_HSIC,
37};
38
Matt Wagantallf8020902011-08-30 21:19:23 -070039struct sysmon_subsys {
40 struct mutex lock;
41 struct smd_channel *chan;
42 bool chan_open;
43 struct completion resp_ready;
Matt Wagantalle89b3ac2012-04-30 14:01:39 -070044 char rx_buf[RX_BUF_SIZE];
Matt Wagantallc3d9a2f2012-04-13 12:39:17 -070045 enum transports transport;
Matt Wagantallf8020902011-08-30 21:19:23 -070046};
47
Matt Wagantallc3d9a2f2012-04-13 12:39:17 -070048static 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 Wagantallf8020902011-08-30 21:19:23 -070056
57static 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 Wagantalle89b3ac2012-04-30 14:01:39 -070064static int sysmon_send_smd(struct sysmon_subsys *ss, const char *tx_buf,
65 size_t len)
Matt Wagantallc3d9a2f2012-04-13 12:39:17 -070066{
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 Wagantalle89b3ac2012-04-30 14:01:39 -070083static int sysmon_send_hsic(struct sysmon_subsys *ss, const char *tx_buf,
84 size_t len)
Matt Wagantallc3d9a2f2012-04-13 12:39:17 -070085{
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 Wagantalle89b3ac2012-04-30 14:01:39 -070099static 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 Wagantallf8020902011-08-30 21:19:23 -0700134int 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 Wagantalle89b3ac2012-04-30 14:01:39 -0700138 char tx_buf[TX_BUF_SIZE];
Matt Wagantallf8020902011-08-30 21:19:23 -0700139 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 Wagantallf8020902011-08-30 21:19:23 -0700146 snprintf(tx_buf, ARRAY_SIZE(tx_buf), "ssr:%s:%s", event_ss,
147 notif_name[notif]);
Matt Wagantallf8020902011-08-30 21:19:23 -0700148
Matt Wagantallc3d9a2f2012-04-13 12:39:17 -0700149 mutex_lock(&ss->lock);
Matt Wagantalle89b3ac2012-04-30 14:01:39 -0700150 ret = sysmon_send_msg(ss, tx_buf, strlen(tx_buf));
Matt Wagantallc3d9a2f2012-04-13 12:39:17 -0700151 if (ret)
152 goto out;
153
Matt Wagantallc3d9a2f2012-04-13 12:39:17 -0700154 if (strncmp(ss->rx_buf, "ssr:ack", ARRAY_SIZE(ss->rx_buf)))
155 ret = -ENOSYS;
Matt Wagantalle89b3ac2012-04-30 14:01:39 -0700156out:
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 */
174int 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
Kim,Do-Yeob1d7938c2012-08-21 14:48:25 +0900186 if ((!ss->chan_open) && (dest_ss != SYSMON_SS_EXT_MODEM))
Iliyan Malcheva4bfdcd2012-07-03 14:24:56 -0700187 return -ENODEV;
188
Matt Wagantalle89b3ac2012-04-30 14:01:39 -0700189 mutex_lock(&ss->lock);
190 ret = sysmon_send_msg(ss, tx_buf, ARRAY_SIZE(tx_buf));
191 if (ret)
192 goto out;
193
194 if (strncmp(ss->rx_buf, expect, prefix_len)) {
195 ret = -ENOSYS;
196 goto out;
197 }
198 strlcpy(buf, ss->rx_buf + prefix_len, len);
Matt Wagantallc3d9a2f2012-04-13 12:39:17 -0700199out:
200 mutex_unlock(&ss->lock);
Matt Wagantallf8020902011-08-30 21:19:23 -0700201 return ret;
202}
203
Matt Wagantallc3d9a2f2012-04-13 12:39:17 -0700204static void sysmon_smd_notify(void *priv, unsigned int smd_event)
Matt Wagantallf8020902011-08-30 21:19:23 -0700205{
206 struct sysmon_subsys *ss = priv;
207
208 switch (smd_event) {
209 case SMD_EVENT_DATA: {
210 if (smd_read_avail(ss->chan) > 0) {
211 smd_read_from_cb(ss->chan, ss->rx_buf,
212 ARRAY_SIZE(ss->rx_buf));
213 complete(&ss->resp_ready);
214 }
215 break;
216 }
217 case SMD_EVENT_OPEN:
218 ss->chan_open = true;
219 break;
220 case SMD_EVENT_CLOSE:
221 ss->chan_open = false;
222 break;
223 }
224}
225
226static int sysmon_probe(struct platform_device *pdev)
227{
Matt Wagantallf8020902011-08-30 21:19:23 -0700228 struct sysmon_subsys *ss;
229 int ret;
230
Matt Wagantallc3d9a2f2012-04-13 12:39:17 -0700231 if (pdev->id < 0 || pdev->id >= SYSMON_NUM_SS)
Matt Wagantallf8020902011-08-30 21:19:23 -0700232 return -ENODEV;
233
Matt Wagantallc3d9a2f2012-04-13 12:39:17 -0700234 ss = &subsys[pdev->id];
Matt Wagantallf8020902011-08-30 21:19:23 -0700235 mutex_init(&ss->lock);
236
Matt Wagantallc3d9a2f2012-04-13 12:39:17 -0700237 switch (ss->transport) {
238 case TRANSPORT_SMD:
239 if (pdev->id >= SMD_NUM_TYPE)
240 return -EINVAL;
241
242 ret = smd_named_open_on_edge("sys_mon", pdev->id, &ss->chan, ss,
243 sysmon_smd_notify);
244 if (ret) {
245 pr_err("SMD open failed\n");
246 return ret;
247 }
248
249 smd_disable_read_intr(ss->chan);
250 break;
251 case TRANSPORT_HSIC:
252 if (pdev->id < SMD_NUM_TYPE)
253 return -EINVAL;
254
255 ret = hsic_sysmon_open(HSIC_SYSMON_DEV_EXT_MODEM);
256 if (ret) {
257 pr_err("HSIC open failed\n");
258 return ret;
259 }
260 break;
261 default:
262 return -EINVAL;
Matt Wagantallf8020902011-08-30 21:19:23 -0700263 }
Matt Wagantallf8020902011-08-30 21:19:23 -0700264
265 return 0;
266}
267
268static int __devexit sysmon_remove(struct platform_device *pdev)
269{
Matt Wagantallc3d9a2f2012-04-13 12:39:17 -0700270 struct sysmon_subsys *ss = &subsys[pdev->id];
271
272 switch (ss->transport) {
273 case TRANSPORT_SMD:
274 smd_close(ss->chan);
275 break;
276 case TRANSPORT_HSIC:
277 hsic_sysmon_close(HSIC_SYSMON_DEV_EXT_MODEM);
278 break;
279 }
280
Matt Wagantallf8020902011-08-30 21:19:23 -0700281 return 0;
282}
283
284static struct platform_driver sysmon_driver = {
285 .probe = sysmon_probe,
286 .remove = __devexit_p(sysmon_remove),
287 .driver = {
288 .name = "sys_mon",
289 .owner = THIS_MODULE,
290 },
291};
292
293static int __init sysmon_init(void)
294{
295 return platform_driver_register(&sysmon_driver);
296}
297subsys_initcall(sysmon_init);
298
299static void __exit sysmon_exit(void)
300{
301 platform_driver_unregister(&sysmon_driver);
302}
303module_exit(sysmon_exit);
304
305MODULE_LICENSE("GPL v2");
306MODULE_DESCRIPTION("system monitor communication library");
307MODULE_ALIAS("platform:sys_mon");