sec_crashkey.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * COPYRIGHT(C) 2019-2023 Samsung Electronics Co., Ltd. All Right Reserved.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ":%s() " fmt, __func__
  6. #include <linux/delay.h>
  7. #include <linux/device.h>
  8. #include <linux/init.h>
  9. #include <linux/input.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/of.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/syscore_ops.h>
  18. #include <linux/samsung/bsp/sec_key_notifier.h>
  19. #include <linux/samsung/debug/sec_debug.h>
  20. #include <linux/samsung/debug/sec_log_buf.h>
  21. #include <linux/samsung/of_early_populate.h>
  22. #include <linux/samsung/sec_kunit.h>
  23. #include <linux/samsung/sec_of.h>
  24. #include "sec_crashkey.h"
  25. static DEFINE_MUTEX(crashkey_list_lock);
  26. static LIST_HEAD(crashkey_dev_list);
  27. __ss_static struct crashkey_drvdata *__crashkey_find_by_name_locked(
  28. const char *name, struct list_head *head)
  29. {
  30. struct crashkey_drvdata *drvdata;
  31. size_t name_len = strlen(name);
  32. list_for_each_entry(drvdata, head, list) {
  33. size_t len = strnlen(drvdata->name, name_len + 1);
  34. if (len != name_len)
  35. continue;
  36. if (!strncmp(drvdata->name, name, name_len))
  37. return drvdata;
  38. }
  39. return ERR_PTR(-ENOENT);
  40. }
  41. __ss_static int __crashkey_add_preparing_panic_locked(
  42. struct crashkey_drvdata *drvdata, struct notifier_block *nb)
  43. {
  44. struct crashkey_notify *notify = &drvdata->notify;
  45. return raw_notifier_chain_register(&notify->list, nb);
  46. }
  47. int sec_crashkey_add_preparing_panic(struct notifier_block *nb,
  48. const char *name)
  49. {
  50. struct crashkey_drvdata *drvdata;
  51. int err;
  52. mutex_lock(&crashkey_list_lock);
  53. drvdata = __crashkey_find_by_name_locked(name, &crashkey_dev_list);
  54. if (IS_ERR(drvdata)) {
  55. pr_warn("%s is not a valid drvdata device!\n", name);
  56. err = -ENODEV;
  57. goto err_invalid_name;
  58. }
  59. err = __crashkey_add_preparing_panic_locked(drvdata, nb);
  60. if (err) {
  61. struct device *dev = drvdata->bd.dev;
  62. dev_warn(dev, "failed to add a notifier for %s (%d)!\n",
  63. name, err);
  64. dev_warn(dev, "Caller is %pS\n", __builtin_return_address(0));
  65. }
  66. err_invalid_name:
  67. mutex_unlock(&crashkey_list_lock);
  68. return err;
  69. }
  70. EXPORT_SYMBOL_GPL(sec_crashkey_add_preparing_panic);
  71. __ss_static int __crashkey_del_preparing_panic_locked(
  72. struct crashkey_drvdata *drvdata, struct notifier_block *nb)
  73. {
  74. struct crashkey_notify *notify = &drvdata->notify;
  75. return raw_notifier_chain_unregister(&notify->list, nb);
  76. }
  77. int sec_crashkey_del_preparing_panic(struct notifier_block *nb,
  78. const char *name)
  79. {
  80. struct crashkey_drvdata *drvdata;
  81. int err;
  82. mutex_lock(&crashkey_list_lock);
  83. drvdata = __crashkey_find_by_name_locked(name, &crashkey_dev_list);
  84. if (IS_ERR(drvdata)) {
  85. pr_warn("%s is not a valid drvdata device!\n", name);
  86. err = -ENODEV;
  87. goto err_invalid_name;
  88. }
  89. err = __crashkey_del_preparing_panic_locked(drvdata, nb);
  90. if (err) {
  91. struct device *dev = drvdata->bd.dev;
  92. dev_warn(dev, "failed to remove a notifier for %s!\n", name);
  93. dev_warn(dev, "Caller is %pS\n", __builtin_return_address(0));
  94. }
  95. err_invalid_name:
  96. mutex_unlock(&crashkey_list_lock);
  97. return err;
  98. }
  99. EXPORT_SYMBOL_GPL(sec_crashkey_del_preparing_panic);
  100. static __always_inline bool __crashkey_is_same_pattern(
  101. struct crashkey_drvdata *drvdata,
  102. const struct sec_key_notifier_param *param)
  103. {
  104. struct crashkey_kelog *keylog = &drvdata->keylog;
  105. const struct sec_key_notifier_param *desired =
  106. &keylog->desired[keylog->sequence];
  107. if (param->keycode == desired->keycode && param->down == desired->down) {
  108. keylog->sequence++;
  109. return true;
  110. }
  111. return false;
  112. }
  113. static __always_inline void __crashkey_clear_received_state(
  114. struct crashkey_drvdata *drvdata,
  115. const struct sec_key_notifier_param *param)
  116. {
  117. struct crashkey_kelog *keylog = &drvdata->keylog;
  118. struct crashkey_timer *timer = &drvdata->timer;
  119. keylog->sequence = 0;
  120. ratelimit_state_init(&timer->rs, timer->interval,
  121. keylog->nr_pattern - 1);
  122. /* NOTE: if the current pattern is same as the 1st one of desried,
  123. * advande a 'keylog->sequence'.
  124. */
  125. if (param && __crashkey_is_same_pattern(drvdata, param))
  126. __ratelimit(&timer->rs);
  127. }
  128. static __always_inline void __crashkey_call_crashkey_notify(
  129. struct crashkey_drvdata *drvdata)
  130. {
  131. struct crashkey_notify *notify = &drvdata->notify;
  132. raw_notifier_call_chain(&notify->list, 0, NULL);
  133. }
  134. __ss_static int __crashkey_notifier_call(struct crashkey_drvdata *drvdata,
  135. const struct sec_key_notifier_param *param)
  136. {
  137. struct crashkey_kelog *keylog = &drvdata->keylog;
  138. struct crashkey_timer *timer = &drvdata->timer;
  139. if (!__crashkey_is_same_pattern(drvdata, param))
  140. goto clear_state;
  141. if (!timer->interval || !__ratelimit(&timer->rs)) {
  142. if (keylog->sequence == keylog->nr_pattern)
  143. __crashkey_call_crashkey_notify(drvdata);
  144. else if (timer->interval)
  145. goto clear_state;
  146. }
  147. return NOTIFY_OK;
  148. clear_state:
  149. __crashkey_clear_received_state(drvdata, param);
  150. return NOTIFY_OK;
  151. }
  152. static int sec_crashkey_notifier_call(struct notifier_block *this,
  153. unsigned long type, void *data)
  154. {
  155. struct crashkey_drvdata *drvdata =
  156. container_of(this, struct crashkey_drvdata, nb);
  157. struct sec_key_notifier_param *param = data;
  158. return __crashkey_notifier_call(drvdata, param);
  159. }
  160. __ss_static noinline int __crashkey_parse_dt_name(struct builder *bd,
  161. struct device_node *np)
  162. {
  163. struct crashkey_drvdata *drvdata =
  164. container_of(bd, struct crashkey_drvdata, bd);
  165. return of_property_read_string(np, "sec,name", &drvdata->name);
  166. }
  167. __ss_static int __crashkey_test_dt_debug_level(struct crashkey_drvdata *drvdata,
  168. struct device_node *np, unsigned int sec_dbg_level)
  169. {
  170. struct device *dev = drvdata->bd.dev;
  171. int err;
  172. err = sec_of_test_debug_level(np, "sec,debug_level", sec_dbg_level);
  173. if (err == -ENOENT) {
  174. dev_warn(dev, "this crashkey_dev (%s) will be enabled all sec debug levels!\n",
  175. drvdata->name);
  176. return 0;
  177. } else if (err < 0)
  178. return -ENODEV;
  179. return 0;
  180. }
  181. static noinline int __crashkey_parse_dt_debug_level(struct builder *bd,
  182. struct device_node *np)
  183. {
  184. struct crashkey_drvdata *drvdata =
  185. container_of(bd, struct crashkey_drvdata, bd);
  186. unsigned int sec_dbg_level = sec_debug_level();
  187. return __crashkey_test_dt_debug_level(drvdata, np, sec_dbg_level);
  188. }
  189. __ss_static noinline int __crashkey_parse_dt_panic_msg(struct builder *bd,
  190. struct device_node *np)
  191. {
  192. struct crashkey_drvdata *drvdata =
  193. container_of(bd, struct crashkey_drvdata, bd);
  194. struct crashkey_notify *notify = &drvdata->notify;
  195. return of_property_read_string(np, "sec,panic_msg", &notify->panic_msg);
  196. }
  197. __ss_static noinline int __crashkey_parse_dt_interval(struct builder *bd,
  198. struct device_node *np)
  199. {
  200. struct crashkey_drvdata *drvdata =
  201. container_of(bd, struct crashkey_drvdata, bd);
  202. struct crashkey_timer *timer = &drvdata->timer;
  203. s32 interval;
  204. int err;
  205. err = of_property_read_s32(np, "sec,interval", &interval);
  206. if (err)
  207. return -EINVAL;
  208. timer->interval = (int)interval * HZ;
  209. return 0;
  210. }
  211. __ss_static noinline int __crashkey_parse_dt_desired_pattern(struct builder *bd,
  212. struct device_node *np)
  213. {
  214. struct crashkey_drvdata *drvdata =
  215. container_of(bd, struct crashkey_drvdata, bd);
  216. struct device *dev = bd->dev;
  217. struct crashkey_kelog *keylog = &drvdata->keylog;
  218. struct sec_key_notifier_param *desired;
  219. int nr_pattern;
  220. u32 keycode, down;
  221. int i;
  222. nr_pattern = of_property_count_u32_elems(np, "sec,desired_pattern");
  223. nr_pattern /= 2; /* <keycode, down> */
  224. if (nr_pattern <= 0)
  225. return -EINVAL;
  226. desired = devm_kmalloc_array(dev,
  227. nr_pattern, sizeof(*desired), GFP_KERNEL);
  228. if (!desired)
  229. return -ENOMEM;
  230. keylog->nr_pattern = (size_t)nr_pattern;
  231. keylog->desired = desired;
  232. for (i = 0; i < nr_pattern; i++) {
  233. of_property_read_u32_index(np, "sec,desired_pattern",
  234. 2 * i, &keycode);
  235. of_property_read_u32_index(np, "sec,desired_pattern",
  236. 2 * i + 1, &down);
  237. desired[i].keycode = keycode;
  238. desired[i].down = down;
  239. }
  240. return 0;
  241. }
  242. static const struct dt_builder __crashkey_dt_builder[] = {
  243. DT_BUILDER(__crashkey_parse_dt_name),
  244. DT_BUILDER(__crashkey_parse_dt_debug_level),
  245. DT_BUILDER(__crashkey_parse_dt_panic_msg),
  246. DT_BUILDER(__crashkey_parse_dt_interval),
  247. DT_BUILDER(__crashkey_parse_dt_desired_pattern),
  248. };
  249. static noinline int __crashkey_parse_dt(struct builder *bd)
  250. {
  251. return sec_director_parse_dt(bd, __crashkey_dt_builder,
  252. ARRAY_SIZE(__crashkey_dt_builder));
  253. }
  254. __ss_static noinline int __crashkey_probe_prolog(struct builder *bd)
  255. {
  256. struct crashkey_drvdata *drvdata =
  257. container_of(bd, struct crashkey_drvdata, bd);
  258. struct crashkey_notify *notify = &drvdata->notify;
  259. struct crashkey_kelog *keylog = &drvdata->keylog;
  260. struct crashkey_timer *timer = &drvdata->timer;
  261. RAW_INIT_NOTIFIER_HEAD(&notify->list);
  262. ratelimit_state_init(&timer->rs, timer->interval,
  263. keylog->nr_pattern - 1);
  264. return 0;
  265. }
  266. __ss_static void __crashkey_add_to_crashkey_dev_list_locked(
  267. struct crashkey_drvdata *drvdata, struct list_head *head)
  268. {
  269. list_add(&drvdata->list, head);
  270. }
  271. static int __crashkey_add_to_crashkey_dev_list(struct builder *bd)
  272. {
  273. struct crashkey_drvdata *drvdata =
  274. container_of(bd, struct crashkey_drvdata, bd);
  275. mutex_lock(&crashkey_list_lock);
  276. __crashkey_add_to_crashkey_dev_list_locked(drvdata, &crashkey_dev_list);
  277. mutex_unlock(&crashkey_list_lock);
  278. return 0;
  279. }
  280. __ss_static void __crashkey_del_from_crashkey_dev_list_locked(
  281. struct crashkey_drvdata *drvdata)
  282. {
  283. list_del(&drvdata->list);
  284. }
  285. static void __crashkey_del_from_crashkey_dev_list(struct builder *bd)
  286. {
  287. struct crashkey_drvdata *drvdata =
  288. container_of(bd, struct crashkey_drvdata, bd);
  289. mutex_lock(&crashkey_list_lock);
  290. __crashkey_del_from_crashkey_dev_list_locked(drvdata);
  291. mutex_unlock(&crashkey_list_lock);
  292. }
  293. static int __crashkey_panic_on_matched(struct notifier_block *this,
  294. unsigned long type, void *data)
  295. {
  296. struct crashkey_notify *notify =
  297. container_of(this, struct crashkey_notify, panic);
  298. panic("%s", notify->panic_msg);
  299. return NOTIFY_OK;
  300. }
  301. static int __crashkey_set_panic_on_matched(struct builder *bd)
  302. {
  303. struct crashkey_drvdata *drvdata =
  304. container_of(bd, struct crashkey_drvdata, bd);
  305. struct crashkey_notify *notify = &drvdata->notify;
  306. int err;
  307. /* NOTE: register a calling kernel panic in the end of notifier chain */
  308. notify->panic.notifier_call = __crashkey_panic_on_matched;
  309. notify->panic.priority = INT_MIN;
  310. err = sec_crashkey_add_preparing_panic(&notify->panic, drvdata->name);
  311. if (err)
  312. return err;
  313. return 0;
  314. }
  315. static void __crashkey_unset_panic_on_matched(struct builder *bd)
  316. {
  317. struct crashkey_drvdata *drvdata =
  318. container_of(bd, struct crashkey_drvdata, bd);
  319. struct crashkey_notify *notify = &drvdata->notify;
  320. sec_crashkey_del_preparing_panic(&notify->panic, drvdata->name);
  321. }
  322. __ss_static noinline int __crashkey_init_used_key(struct builder *bd)
  323. {
  324. struct crashkey_drvdata *drvdata =
  325. container_of(bd, struct crashkey_drvdata, bd);
  326. struct device *dev = bd->dev;
  327. struct crashkey_kelog *keylog = &drvdata->keylog;
  328. const struct sec_key_notifier_param *desired = keylog->desired;
  329. static unsigned int *used_key;
  330. size_t nr_used_key;
  331. bool is_new;
  332. size_t i, j;
  333. used_key = devm_kmalloc_array(dev,
  334. keylog->nr_pattern, sizeof(*used_key), GFP_KERNEL);
  335. if (!used_key)
  336. return -ENOMEM;
  337. used_key[0] = desired[0].keycode;
  338. nr_used_key = 1;
  339. for (i = 1; i < keylog->nr_pattern; i++) {
  340. for (j = 0, is_new = true; j < nr_used_key; j++) {
  341. if (used_key[j] == desired[i].keycode)
  342. is_new = false;
  343. }
  344. if (is_new)
  345. used_key[nr_used_key++] = desired[i].keycode;
  346. }
  347. keylog->used_key = used_key;
  348. keylog->nr_used_key = nr_used_key;
  349. return 0;
  350. }
  351. static int __crashkey_install_keyboard_notifier(struct builder *bd)
  352. {
  353. struct crashkey_drvdata *drvdata =
  354. container_of(bd, struct crashkey_drvdata, bd);
  355. struct crashkey_kelog *keylog = &drvdata->keylog;
  356. int err;
  357. drvdata->nb.notifier_call = sec_crashkey_notifier_call;
  358. err = sec_kn_register_notifier(&drvdata->nb,
  359. keylog->used_key, keylog->nr_used_key);
  360. return err;
  361. }
  362. static void __crashkey_uninstall_keyboard_notifier(struct builder *bd)
  363. {
  364. struct crashkey_drvdata *drvdata =
  365. container_of(bd, struct crashkey_drvdata, bd);
  366. struct crashkey_kelog *keylog = &drvdata->keylog;
  367. sec_kn_unregister_notifier(&drvdata->nb,
  368. keylog->used_key, keylog->nr_used_key);
  369. }
  370. static noinline int __crashkey_probe_epilog(struct builder *bd)
  371. {
  372. struct crashkey_drvdata *drvdata =
  373. container_of(bd, struct crashkey_drvdata, bd);
  374. struct device *dev = bd->dev;
  375. dev_set_drvdata(dev, drvdata);
  376. return 0;
  377. }
  378. static int __crashkey_probe(struct platform_device *pdev,
  379. const struct dev_builder *builder, ssize_t n)
  380. {
  381. struct device *dev = &pdev->dev;
  382. struct crashkey_drvdata *drvdata;
  383. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  384. if (!drvdata)
  385. return -ENOMEM;
  386. drvdata->bd.dev = dev;
  387. return sec_director_probe_dev(&drvdata->bd, builder, n);
  388. }
  389. static int __crashkey_remove(struct platform_device *pdev,
  390. const struct dev_builder *builder, ssize_t n)
  391. {
  392. struct crashkey_drvdata *drvdata = platform_get_drvdata(pdev);
  393. sec_director_destruct_dev(&drvdata->bd, builder, n, n);
  394. return 0;
  395. }
  396. static const struct dev_builder __crashkey_dev_builder[] = {
  397. DEVICE_BUILDER(__crashkey_parse_dt, NULL),
  398. DEVICE_BUILDER(__crashkey_probe_prolog, NULL),
  399. DEVICE_BUILDER(__crashkey_add_to_crashkey_dev_list,
  400. __crashkey_del_from_crashkey_dev_list),
  401. DEVICE_BUILDER(__crashkey_set_panic_on_matched,
  402. __crashkey_unset_panic_on_matched),
  403. DEVICE_BUILDER(__crashkey_init_used_key, NULL),
  404. DEVICE_BUILDER(__crashkey_install_keyboard_notifier,
  405. __crashkey_uninstall_keyboard_notifier),
  406. DEVICE_BUILDER(__crashkey_probe_epilog, NULL),
  407. };
  408. static int sec_crashkey_probe(struct platform_device *pdev)
  409. {
  410. return __crashkey_probe(pdev, __crashkey_dev_builder,
  411. ARRAY_SIZE(__crashkey_dev_builder));
  412. }
  413. static int sec_crashkey_remove(struct platform_device *pdev)
  414. {
  415. return __crashkey_remove(pdev, __crashkey_dev_builder,
  416. ARRAY_SIZE(__crashkey_dev_builder));
  417. }
  418. static const struct of_device_id sec_crashkey_match_table[] = {
  419. { .compatible = "samsung,crashkey" },
  420. {},
  421. };
  422. MODULE_DEVICE_TABLE(of, sec_crashkey_match_table);
  423. static struct platform_driver sec_crashkey_driver = {
  424. .driver = {
  425. .name = "sec,crashkey",
  426. .of_match_table = of_match_ptr(sec_crashkey_match_table),
  427. },
  428. .probe = sec_crashkey_probe,
  429. .remove = sec_crashkey_remove,
  430. };
  431. static int sec_crashkey_suspend(void)
  432. {
  433. struct crashkey_drvdata *drvdata;
  434. list_for_each_entry(drvdata, &crashkey_dev_list, list) {
  435. __crashkey_clear_received_state(drvdata, NULL);
  436. }
  437. return 0;
  438. }
  439. static struct syscore_ops sec_crashkey_syscore_ops = {
  440. .suspend = sec_crashkey_suspend,
  441. };
  442. static int __init sec_crashkey_init(void)
  443. {
  444. int err;
  445. err = platform_driver_register(&sec_crashkey_driver);
  446. if (err)
  447. return err;
  448. err = __of_platform_early_populate_init(sec_crashkey_match_table);
  449. if (err)
  450. return err;
  451. register_syscore_ops(&sec_crashkey_syscore_ops);
  452. return 0;
  453. }
  454. core_initcall(sec_crashkey_init);
  455. static void __exit sec_crashkey_exit(void)
  456. {
  457. unregister_syscore_ops(&sec_crashkey_syscore_ops);
  458. platform_driver_unregister(&sec_crashkey_driver);
  459. }
  460. module_exit(sec_crashkey_exit);
  461. MODULE_AUTHOR("Samsung Electronics");
  462. MODULE_DESCRIPTION("Force key crash driver");
  463. MODULE_LICENSE("GPL v2");