nfc_logger.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) 2016 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * NFC logger
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/version.h>
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/string.h>
  15. #include <linux/types.h>
  16. #include <linux/stat.h>
  17. #include <linux/module.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/time.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/ktime.h>
  22. #if LINUX_VERSION_CODE > KERNEL_VERSION(4, 10, 0)
  23. #include <linux/sched/clock.h>
  24. #else
  25. #include <linux/sched.h>
  26. #endif
  27. #ifdef CONFIG_SEC_NFC_LOGGER_ADD_ACPM_LOG
  28. #include <linux/io.h>
  29. #include <soc/samsung/acpm_ipc_ctrl.h>
  30. #include <soc/samsung/debug-snapshot.h>
  31. #endif
  32. #include "nfc_logger.h"
  33. #define BUF_SIZE SZ_128K
  34. #define BOOT_LOG_SIZE 2400
  35. #define MAX_STR_LEN 160
  36. #define PROC_FILE_NAME "nfclog"
  37. #define LOG_PREFIX "sec-nfc"
  38. #define PRINT_DATE_FREQ 20
  39. static char nfc_log_buf[BUF_SIZE];
  40. static unsigned int g_curpos;
  41. static int is_nfc_logger_init;
  42. static int is_buf_full;
  43. static int g_log_max_count = -1;
  44. static void (*print_nfc_status)(void);
  45. struct proc_dir_entry *g_entry;
  46. /* set max log count, if count is -1, no limit */
  47. void nfc_logger_set_max_count(int count)
  48. {
  49. g_log_max_count = count;
  50. }
  51. void nfc_logger_get_date_time(char *date_time, int size)
  52. {
  53. struct timespec64 ts;
  54. struct tm tm;
  55. unsigned long sec;
  56. int len = 0;
  57. ktime_get_real_ts64(&ts);
  58. sec = ts.tv_sec - (sys_tz.tz_minuteswest * 60);
  59. time64_to_tm(sec, 0, &tm);
  60. len = snprintf(date_time, size, "%02d-%02d %02d:%02d:%02d.%03lu", tm.tm_mon + 1, tm.tm_mday,
  61. tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec / 1000000);
  62. #ifdef CONFIG_SEC_NFC_LOGGER_ADD_ACPM_LOG
  63. snprintf(date_time + len, size - len, ", rtc: %u", date_time,
  64. nfc_logger_acpm_get_rtc_time());
  65. #endif
  66. }
  67. void nfc_logger_print(char *fmt, ...)
  68. {
  69. int len;
  70. va_list args;
  71. char buf[MAX_STR_LEN] = {0, };
  72. u64 time;
  73. u32 nsec;
  74. unsigned int curpos;
  75. static unsigned int log_count = PRINT_DATE_FREQ;
  76. char date_time[64] = {0, };
  77. bool date_time_print = false;
  78. if (!is_nfc_logger_init)
  79. return;
  80. if (g_log_max_count == 0)
  81. return;
  82. else if (g_log_max_count > 0)
  83. g_log_max_count--;
  84. if (--log_count == 0) {
  85. nfc_logger_get_date_time(date_time, sizeof(date_time));
  86. log_count = PRINT_DATE_FREQ;
  87. date_time_print = true;
  88. }
  89. time = local_clock();
  90. nsec = do_div(time, 1000000000);
  91. if (g_curpos < BOOT_LOG_SIZE)
  92. len = snprintf(buf, sizeof(buf), "[B%4llu.%06u] ", time, nsec / 1000);
  93. else
  94. len = snprintf(buf, sizeof(buf), "[%5llu.%06u] ", time, nsec / 1000);
  95. if (date_time_print)
  96. len += snprintf(buf + len, sizeof(buf) - len, "[%s] ", date_time);
  97. va_start(args, fmt);
  98. len += vsnprintf(buf + len, MAX_STR_LEN - len, fmt, args);
  99. va_end(args);
  100. if (len > MAX_STR_LEN)
  101. len = MAX_STR_LEN;
  102. curpos = g_curpos;
  103. if (curpos + len >= BUF_SIZE) {
  104. g_curpos = curpos = BOOT_LOG_SIZE;
  105. is_buf_full = 1;
  106. }
  107. memcpy(nfc_log_buf + curpos, buf, len);
  108. g_curpos += len;
  109. }
  110. void nfc_logger_register_nfc_stauts_func(void (*print_status_callback)(void))
  111. {
  112. print_nfc_status = print_status_callback;
  113. }
  114. void nfc_print_hex_dump(void *buf, void *pref, size_t size)
  115. {
  116. uint8_t *ptr = buf;
  117. size_t i;
  118. char tmp[128] = {0x0, };
  119. char *ptmp = tmp;
  120. int len;
  121. if (!is_nfc_logger_init)
  122. return;
  123. if (g_log_max_count == 0)
  124. return;
  125. else if (g_log_max_count > 0)
  126. g_log_max_count--;
  127. for (i = 0; i < size; i++) {
  128. len = snprintf(ptmp, 4, "%02x ", *ptr++);
  129. ptmp = ptmp + len;
  130. if (((i+1)%16) == 0) {
  131. nfc_logger_print("%s%s\n", pref, tmp);
  132. ptmp = tmp;
  133. }
  134. }
  135. if (i % 16) {
  136. len = ptmp - tmp;
  137. tmp[len] = 0x0;
  138. nfc_logger_print("%s%s\n", pref, tmp);
  139. }
  140. }
  141. static ssize_t nfc_logger_read(struct file *file, char __user *buf, size_t len, loff_t *offset)
  142. {
  143. loff_t pos = *offset;
  144. ssize_t count;
  145. size_t size;
  146. unsigned int curpos = g_curpos;
  147. if (is_buf_full || BUF_SIZE <= curpos)
  148. size = BUF_SIZE;
  149. else
  150. size = (size_t)curpos;
  151. if (pos >= size)
  152. return 0;
  153. if (print_nfc_status && !pos)
  154. print_nfc_status();
  155. count = min(len, size);
  156. if ((pos + count) > size)
  157. count = size - pos;
  158. if (copy_to_user(buf, nfc_log_buf + pos, count))
  159. return -EFAULT;
  160. *offset += count;
  161. return count;
  162. }
  163. #if KERNEL_VERSION(5, 6, 0) <= LINUX_VERSION_CODE
  164. static const struct proc_ops nfc_logger_ops = {
  165. .proc_read = nfc_logger_read,
  166. .proc_lseek = default_llseek,
  167. };
  168. #else
  169. static const struct file_operations nfc_logger_ops = {
  170. .owner = THIS_MODULE,
  171. .read = nfc_logger_read,
  172. .llseek = default_llseek,
  173. };
  174. #endif
  175. int nfc_logger_init(void)
  176. {
  177. struct proc_dir_entry *entry;
  178. if (is_nfc_logger_init)
  179. return 0;
  180. entry = proc_create(PROC_FILE_NAME, 0444, NULL, &nfc_logger_ops);
  181. if (!entry) {
  182. pr_err("%s: failed to create proc entry\n", __func__);
  183. return 0;
  184. }
  185. proc_set_size(entry, BUF_SIZE);
  186. is_nfc_logger_init = 1;
  187. nfc_logger_print("nfc logger init ok\n");
  188. g_entry = entry;
  189. return 0;
  190. }
  191. void nfc_logger_deinit(void)
  192. {
  193. if (!g_entry)
  194. return;
  195. proc_remove(g_entry);
  196. g_entry = NULL;
  197. }
  198. #ifdef CONFIG_SEC_NFC_LOGGER_ADD_ACPM_LOG
  199. static __iomem *g_rtc_reg;
  200. u32 nfc_logger_acpm_get_rtc_time(void)
  201. {
  202. u32 rtc = 0;
  203. if (g_rtc_reg)
  204. rtc = readl(g_rtc_reg);
  205. return rtc;
  206. }
  207. void nfc_logger_acpm_log_print(void)
  208. {
  209. struct nfc_clk_req_log *acpm_log;
  210. int last_ptr, len, i;
  211. u32 rtc;
  212. if (!acpm_get_nfc_log_buf(&acpm_log, &last_ptr, &len)) {
  213. for (i = 0; i < len; i++) {
  214. rtc = nfc_logger_acpm_get_rtc_time();
  215. NFC_LOG_INFO("rtc[%u] - acpm[%2d][%d] %d\n",
  216. rtc, i, acpm_log[i].timestamp, acpm_log[i].is_on);
  217. }
  218. }
  219. }
  220. void nfc_logger_acpm_log_init(u32 rtc_addr)
  221. {
  222. u32 rtc_reg_addr = CONFIG_SEC_NFC_LOGGER_RTC_REG_ADDR;
  223. if (rtc_addr)
  224. rtc_reg_addr = rtc_addr;
  225. if (rtc_reg_addr) {
  226. NFC_LOG_INFO("rtc: 0x%X\n", rtc_reg_addr);
  227. g_rtc_reg = ioremap(rtc_reg_addr, 0x4);
  228. }
  229. }
  230. #endif