Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzi...
[sfrench/cifs-2.6.git] / drivers / misc / iwmc3200top / log.c
1 /*
2  * iwmc3200top - Intel Wireless MultiCom 3200 Top Driver
3  * drivers/misc/iwmc3200top/log.c
4  *
5  * Copyright (C) 2009 Intel Corporation. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License version
9  * 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  *
21  *
22  * Author Name: Maxim Grabarnik <maxim.grabarnink@intel.com>
23  *  -
24  *
25  */
26
27 #include <linux/kernel.h>
28 #include <linux/mmc/sdio_func.h>
29 #include <linux/ctype.h>
30 #include "fw-msg.h"
31 #include "iwmc3200top.h"
32 #include "log.h"
33
34 /* Maximal hexadecimal string size of the FW memdump message */
35 #define LOG_MSG_SIZE_MAX                12400
36
37 /* iwmct_logdefs is a global used by log macros */
38 u8 iwmct_logdefs[LOG_SRC_MAX];
39 static u8 iwmct_fw_logdefs[FW_LOG_SRC_MAX];
40
41
42 static int _log_set_log_filter(u8 *logdefs, int size, u8 src, u8 logmask)
43 {
44         int i;
45
46         if (src < size)
47                 logdefs[src] = logmask;
48         else if (src == LOG_SRC_ALL)
49                 for (i = 0; i < size; i++)
50                         logdefs[i] = logmask;
51         else
52                 return -1;
53
54         return 0;
55 }
56
57
58 int iwmct_log_set_filter(u8 src, u8 logmask)
59 {
60         return _log_set_log_filter(iwmct_logdefs, LOG_SRC_MAX, src, logmask);
61 }
62
63
64 int iwmct_log_set_fw_filter(u8 src, u8 logmask)
65 {
66         return _log_set_log_filter(iwmct_fw_logdefs,
67                                    FW_LOG_SRC_MAX, src, logmask);
68 }
69
70
71 static int log_msg_format_hex(char *str, int slen, u8 *ibuf,
72                               int ilen, char *pref)
73 {
74         int pos = 0;
75         int i;
76         int len;
77
78         for (pos = 0, i = 0; pos < slen - 2 && pref[i] != '\0'; i++, pos++)
79                 str[pos] = pref[i];
80
81         for (i = 0; pos < slen - 2 && i < ilen; pos += len, i++)
82                 len = snprintf(&str[pos], slen - pos - 1, " %2.2X", ibuf[i]);
83
84         if (i < ilen)
85                 return -1;
86
87         return 0;
88 }
89
90 /*      NOTE: This function is not thread safe.
91         Currently it's called only from sdio rx worker - no race there
92 */
93 void iwmct_log_top_message(struct iwmct_priv *priv, u8 *buf, int len)
94 {
95         struct top_msg *msg;
96         static char logbuf[LOG_MSG_SIZE_MAX];
97
98         msg = (struct top_msg *)buf;
99
100         if (len < sizeof(msg->hdr) + sizeof(msg->u.log.log_hdr)) {
101                 LOG_ERROR(priv, FW_MSG, "Log message from TOP "
102                           "is too short %d (expected %zd)\n",
103                           len, sizeof(msg->hdr) + sizeof(msg->u.log.log_hdr));
104                 return;
105         }
106
107         if (!(iwmct_fw_logdefs[msg->u.log.log_hdr.logsource] &
108                 BIT(msg->u.log.log_hdr.severity)) ||
109             !(iwmct_logdefs[LOG_SRC_FW_MSG] & BIT(msg->u.log.log_hdr.severity)))
110                 return;
111
112         switch (msg->hdr.category) {
113         case COMM_CATEGORY_TESTABILITY:
114                 if (!(iwmct_logdefs[LOG_SRC_TST] &
115                       BIT(msg->u.log.log_hdr.severity)))
116                         return;
117                 if (log_msg_format_hex(logbuf, LOG_MSG_SIZE_MAX, buf,
118                                        le16_to_cpu(msg->hdr.length) +
119                                        sizeof(msg->hdr), "<TST>"))
120                         LOG_WARNING(priv, TST,
121                                   "TOP TST message is too long, truncating...");
122                 LOG_WARNING(priv, TST, "%s\n", logbuf);
123                 break;
124         case COMM_CATEGORY_DEBUG:
125                 if (msg->hdr.opcode == OP_DBG_ZSTR_MSG)
126                         LOG_INFO(priv, FW_MSG, "%s %s", "<DBG>",
127                                        ((u8 *)msg) + sizeof(msg->hdr)
128                                         + sizeof(msg->u.log.log_hdr));
129                 else {
130                         if (log_msg_format_hex(logbuf, LOG_MSG_SIZE_MAX, buf,
131                                         le16_to_cpu(msg->hdr.length)
132                                                 + sizeof(msg->hdr),
133                                         "<DBG>"))
134                                 LOG_WARNING(priv, FW_MSG,
135                                         "TOP DBG message is too long,"
136                                         "truncating...");
137                         LOG_WARNING(priv, FW_MSG, "%s\n", logbuf);
138                 }
139                 break;
140         default:
141                 break;
142         }
143 }
144
145 static int _log_get_filter_str(u8 *logdefs, int logdefsz, char *buf, int size)
146 {
147         int i, pos, len;
148         for (i = 0, pos = 0; (pos < size-1) && (i < logdefsz); i++) {
149                 len = snprintf(&buf[pos], size - pos - 1, "0x%02X%02X,",
150                                 i, logdefs[i]);
151                 pos += len;
152         }
153         buf[pos-1] = '\n';
154         buf[pos] = '\0';
155
156         if (i < logdefsz)
157                 return -1;
158         return 0;
159 }
160
161 int log_get_filter_str(char *buf, int size)
162 {
163         return _log_get_filter_str(iwmct_logdefs, LOG_SRC_MAX, buf, size);
164 }
165
166 int log_get_fw_filter_str(char *buf, int size)
167 {
168         return _log_get_filter_str(iwmct_fw_logdefs, FW_LOG_SRC_MAX, buf, size);
169 }
170
171 #define HEXADECIMAL_RADIX       16
172 #define LOG_SRC_FORMAT          7 /* log level is in format of "0xXXXX," */
173
174 ssize_t show_iwmct_log_level(struct device *d,
175                                 struct device_attribute *attr, char *buf)
176 {
177         struct iwmct_priv *priv = dev_get_drvdata(d);
178         char *str_buf;
179         int buf_size;
180         ssize_t ret;
181
182         buf_size = (LOG_SRC_FORMAT * LOG_SRC_MAX) + 1;
183         str_buf = kzalloc(buf_size, GFP_KERNEL);
184         if (!str_buf) {
185                 LOG_ERROR(priv, DEBUGFS,
186                         "failed to allocate %d bytes\n", buf_size);
187                 ret = -ENOMEM;
188                 goto exit;
189         }
190
191         if (log_get_filter_str(str_buf, buf_size) < 0) {
192                 ret = -EINVAL;
193                 goto exit;
194         }
195
196         ret = sprintf(buf, "%s", str_buf);
197
198 exit:
199         kfree(str_buf);
200         return ret;
201 }
202
203 ssize_t store_iwmct_log_level(struct device *d,
204                         struct device_attribute *attr,
205                         const char *buf, size_t count)
206 {
207         struct iwmct_priv *priv = dev_get_drvdata(d);
208         char *token, *str_buf = NULL;
209         long val;
210         ssize_t ret = count;
211         u8 src, mask;
212
213         if (!count)
214                 goto exit;
215
216         str_buf = kzalloc(count, GFP_KERNEL);
217         if (!str_buf) {
218                 LOG_ERROR(priv, DEBUGFS,
219                         "failed to allocate %zd bytes\n", count);
220                 ret = -ENOMEM;
221                 goto exit;
222         }
223
224         memcpy(str_buf, buf, count);
225
226         while ((token = strsep(&str_buf, ",")) != NULL) {
227                 while (isspace(*token))
228                         ++token;
229                 if (strict_strtol(token, HEXADECIMAL_RADIX, &val)) {
230                         LOG_ERROR(priv, DEBUGFS,
231                                   "failed to convert string to long %s\n",
232                                   token);
233                         ret = -EINVAL;
234                         goto exit;
235                 }
236
237                 mask  = val & 0xFF;
238                 src = (val & 0XFF00) >> 8;
239                 iwmct_log_set_filter(src, mask);
240         }
241
242 exit:
243         kfree(str_buf);
244         return ret;
245 }
246
247 ssize_t show_iwmct_log_level_fw(struct device *d,
248                         struct device_attribute *attr, char *buf)
249 {
250         struct iwmct_priv *priv = dev_get_drvdata(d);
251         char *str_buf;
252         int buf_size;
253         ssize_t ret;
254
255         buf_size = (LOG_SRC_FORMAT * FW_LOG_SRC_MAX) + 2;
256
257         str_buf = kzalloc(buf_size, GFP_KERNEL);
258         if (!str_buf) {
259                 LOG_ERROR(priv, DEBUGFS,
260                         "failed to allocate %d bytes\n", buf_size);
261                 ret = -ENOMEM;
262                 goto exit;
263         }
264
265         if (log_get_fw_filter_str(str_buf, buf_size) < 0) {
266                 ret = -EINVAL;
267                 goto exit;
268         }
269
270         ret = sprintf(buf, "%s", str_buf);
271
272 exit:
273         kfree(str_buf);
274         return ret;
275 }
276
277 ssize_t store_iwmct_log_level_fw(struct device *d,
278                         struct device_attribute *attr,
279                         const char *buf, size_t count)
280 {
281         struct iwmct_priv *priv = dev_get_drvdata(d);
282         struct top_msg cmd;
283         char *token, *str_buf = NULL;
284         ssize_t ret = count;
285         u16 cmdlen = 0;
286         int i;
287         long val;
288         u8 src, mask;
289
290         if (!count)
291                 goto exit;
292
293         str_buf = kzalloc(count, GFP_KERNEL);
294         if (!str_buf) {
295                 LOG_ERROR(priv, DEBUGFS,
296                         "failed to allocate %zd bytes\n", count);
297                 ret = -ENOMEM;
298                 goto exit;
299         }
300
301         memcpy(str_buf, buf, count);
302
303         cmd.hdr.type = COMM_TYPE_H2D;
304         cmd.hdr.category = COMM_CATEGORY_DEBUG;
305         cmd.hdr.opcode = CMD_DBG_LOG_LEVEL;
306
307         for (i = 0; ((token = strsep(&str_buf, ",")) != NULL) &&
308                      (i < FW_LOG_SRC_MAX); i++) {
309
310                 while (isspace(*token))
311                         ++token;
312
313                 if (strict_strtol(token, HEXADECIMAL_RADIX, &val)) {
314                         LOG_ERROR(priv, DEBUGFS,
315                                   "failed to convert string to long %s\n",
316                                   token);
317                         ret = -EINVAL;
318                         goto exit;
319                 }
320
321                 mask  = val & 0xFF; /* LSB */
322                 src = (val & 0XFF00) >> 8; /* 2nd least significant byte. */
323                 iwmct_log_set_fw_filter(src, mask);
324
325                 cmd.u.logdefs[i].logsource = src;
326                 cmd.u.logdefs[i].sevmask = mask;
327         }
328
329         cmd.hdr.length = cpu_to_le16(i * sizeof(cmd.u.logdefs[0]));
330         cmdlen = (i * sizeof(cmd.u.logdefs[0]) + sizeof(cmd.hdr));
331
332         ret = iwmct_send_hcmd(priv, (u8 *)&cmd, cmdlen);
333         if (ret) {
334                 LOG_ERROR(priv, DEBUGFS,
335                           "Failed to send %d bytes of fwcmd, ret=%zd\n",
336                           cmdlen, ret);
337                 goto exit;
338         } else
339                 LOG_INFO(priv, DEBUGFS, "fwcmd sent (%d bytes)\n", cmdlen);
340
341         ret = count;
342
343 exit:
344         kfree(str_buf);
345         return ret;
346 }
347