cec-pin-error-inj.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2017 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/slab.h>
  7. #include <linux/sched/types.h>
  8. #include <media/cec-pin.h>
  9. #include "cec-pin-priv.h"
  10. struct cec_error_inj_cmd {
  11. unsigned int mode_offset;
  12. int arg_idx;
  13. const char *cmd;
  14. };
  15. static const struct cec_error_inj_cmd cec_error_inj_cmds[] = {
  16. { CEC_ERROR_INJ_RX_NACK_OFFSET, -1, "rx-nack" },
  17. { CEC_ERROR_INJ_RX_LOW_DRIVE_OFFSET,
  18. CEC_ERROR_INJ_RX_LOW_DRIVE_ARG_IDX, "rx-low-drive" },
  19. { CEC_ERROR_INJ_RX_ADD_BYTE_OFFSET, -1, "rx-add-byte" },
  20. { CEC_ERROR_INJ_RX_REMOVE_BYTE_OFFSET, -1, "rx-remove-byte" },
  21. { CEC_ERROR_INJ_RX_ARB_LOST_OFFSET,
  22. CEC_ERROR_INJ_RX_ARB_LOST_ARG_IDX, "rx-arb-lost" },
  23. { CEC_ERROR_INJ_TX_NO_EOM_OFFSET, -1, "tx-no-eom" },
  24. { CEC_ERROR_INJ_TX_EARLY_EOM_OFFSET, -1, "tx-early-eom" },
  25. { CEC_ERROR_INJ_TX_ADD_BYTES_OFFSET,
  26. CEC_ERROR_INJ_TX_ADD_BYTES_ARG_IDX, "tx-add-bytes" },
  27. { CEC_ERROR_INJ_TX_REMOVE_BYTE_OFFSET, -1, "tx-remove-byte" },
  28. { CEC_ERROR_INJ_TX_SHORT_BIT_OFFSET,
  29. CEC_ERROR_INJ_TX_SHORT_BIT_ARG_IDX, "tx-short-bit" },
  30. { CEC_ERROR_INJ_TX_LONG_BIT_OFFSET,
  31. CEC_ERROR_INJ_TX_LONG_BIT_ARG_IDX, "tx-long-bit" },
  32. { CEC_ERROR_INJ_TX_CUSTOM_BIT_OFFSET,
  33. CEC_ERROR_INJ_TX_CUSTOM_BIT_ARG_IDX, "tx-custom-bit" },
  34. { CEC_ERROR_INJ_TX_SHORT_START_OFFSET, -1, "tx-short-start" },
  35. { CEC_ERROR_INJ_TX_LONG_START_OFFSET, -1, "tx-long-start" },
  36. { CEC_ERROR_INJ_TX_CUSTOM_START_OFFSET, -1, "tx-custom-start" },
  37. { CEC_ERROR_INJ_TX_LAST_BIT_OFFSET,
  38. CEC_ERROR_INJ_TX_LAST_BIT_ARG_IDX, "tx-last-bit" },
  39. { CEC_ERROR_INJ_TX_LOW_DRIVE_OFFSET,
  40. CEC_ERROR_INJ_TX_LOW_DRIVE_ARG_IDX, "tx-low-drive" },
  41. { 0, -1, NULL }
  42. };
  43. u16 cec_pin_rx_error_inj(struct cec_pin *pin)
  44. {
  45. u16 cmd = CEC_ERROR_INJ_OP_ANY;
  46. /* Only when 18 bits have been received do we have a valid cmd */
  47. if (!(pin->error_inj[cmd] & CEC_ERROR_INJ_RX_MASK) &&
  48. pin->rx_bit >= 18)
  49. cmd = pin->rx_msg.msg[1];
  50. return (pin->error_inj[cmd] & CEC_ERROR_INJ_RX_MASK) ? cmd :
  51. CEC_ERROR_INJ_OP_ANY;
  52. }
  53. u16 cec_pin_tx_error_inj(struct cec_pin *pin)
  54. {
  55. u16 cmd = CEC_ERROR_INJ_OP_ANY;
  56. if (!(pin->error_inj[cmd] & CEC_ERROR_INJ_TX_MASK) &&
  57. pin->tx_msg.len > 1)
  58. cmd = pin->tx_msg.msg[1];
  59. return (pin->error_inj[cmd] & CEC_ERROR_INJ_TX_MASK) ? cmd :
  60. CEC_ERROR_INJ_OP_ANY;
  61. }
  62. bool cec_pin_error_inj_parse_line(struct cec_adapter *adap, char *line)
  63. {
  64. static const char *delims = " \t\r";
  65. struct cec_pin *pin = adap->pin;
  66. unsigned int i;
  67. bool has_pos = false;
  68. char *p = line;
  69. char *token;
  70. char *comma;
  71. u64 *error;
  72. u8 *args;
  73. bool has_op;
  74. u8 op;
  75. u8 mode;
  76. u8 pos;
  77. p = skip_spaces(p);
  78. token = strsep(&p, delims);
  79. if (!strcmp(token, "clear")) {
  80. memset(pin->error_inj, 0, sizeof(pin->error_inj));
  81. pin->rx_toggle = pin->tx_toggle = false;
  82. pin->tx_ignore_nack_until_eom = false;
  83. pin->tx_custom_pulse = false;
  84. pin->tx_custom_low_usecs = CEC_TIM_CUSTOM_DEFAULT;
  85. pin->tx_custom_high_usecs = CEC_TIM_CUSTOM_DEFAULT;
  86. return true;
  87. }
  88. if (!strcmp(token, "rx-clear")) {
  89. for (i = 0; i <= CEC_ERROR_INJ_OP_ANY; i++)
  90. pin->error_inj[i] &= ~CEC_ERROR_INJ_RX_MASK;
  91. pin->rx_toggle = false;
  92. return true;
  93. }
  94. if (!strcmp(token, "tx-clear")) {
  95. for (i = 0; i <= CEC_ERROR_INJ_OP_ANY; i++)
  96. pin->error_inj[i] &= ~CEC_ERROR_INJ_TX_MASK;
  97. pin->tx_toggle = false;
  98. pin->tx_ignore_nack_until_eom = false;
  99. pin->tx_custom_pulse = false;
  100. pin->tx_custom_low_usecs = CEC_TIM_CUSTOM_DEFAULT;
  101. pin->tx_custom_high_usecs = CEC_TIM_CUSTOM_DEFAULT;
  102. return true;
  103. }
  104. if (!strcmp(token, "tx-ignore-nack-until-eom")) {
  105. pin->tx_ignore_nack_until_eom = true;
  106. return true;
  107. }
  108. if (!strcmp(token, "tx-custom-pulse")) {
  109. pin->tx_custom_pulse = true;
  110. cec_pin_start_timer(pin);
  111. return true;
  112. }
  113. if (!p)
  114. return false;
  115. p = skip_spaces(p);
  116. if (!strcmp(token, "tx-custom-low-usecs")) {
  117. u32 usecs;
  118. if (kstrtou32(p, 0, &usecs) || usecs > 10000000)
  119. return false;
  120. pin->tx_custom_low_usecs = usecs;
  121. return true;
  122. }
  123. if (!strcmp(token, "tx-custom-high-usecs")) {
  124. u32 usecs;
  125. if (kstrtou32(p, 0, &usecs) || usecs > 10000000)
  126. return false;
  127. pin->tx_custom_high_usecs = usecs;
  128. return true;
  129. }
  130. comma = strchr(token, ',');
  131. if (comma)
  132. *comma++ = '\0';
  133. if (!strcmp(token, "any")) {
  134. has_op = false;
  135. error = pin->error_inj + CEC_ERROR_INJ_OP_ANY;
  136. args = pin->error_inj_args[CEC_ERROR_INJ_OP_ANY];
  137. } else if (!kstrtou8(token, 0, &op)) {
  138. has_op = true;
  139. error = pin->error_inj + op;
  140. args = pin->error_inj_args[op];
  141. } else {
  142. return false;
  143. }
  144. mode = CEC_ERROR_INJ_MODE_ONCE;
  145. if (comma) {
  146. if (!strcmp(comma, "off"))
  147. mode = CEC_ERROR_INJ_MODE_OFF;
  148. else if (!strcmp(comma, "once"))
  149. mode = CEC_ERROR_INJ_MODE_ONCE;
  150. else if (!strcmp(comma, "always"))
  151. mode = CEC_ERROR_INJ_MODE_ALWAYS;
  152. else if (!strcmp(comma, "toggle"))
  153. mode = CEC_ERROR_INJ_MODE_TOGGLE;
  154. else
  155. return false;
  156. }
  157. token = strsep(&p, delims);
  158. if (p) {
  159. p = skip_spaces(p);
  160. has_pos = !kstrtou8(p, 0, &pos);
  161. }
  162. if (!strcmp(token, "clear")) {
  163. *error = 0;
  164. return true;
  165. }
  166. if (!strcmp(token, "rx-clear")) {
  167. *error &= ~CEC_ERROR_INJ_RX_MASK;
  168. return true;
  169. }
  170. if (!strcmp(token, "tx-clear")) {
  171. *error &= ~CEC_ERROR_INJ_TX_MASK;
  172. return true;
  173. }
  174. for (i = 0; cec_error_inj_cmds[i].cmd; i++) {
  175. const char *cmd = cec_error_inj_cmds[i].cmd;
  176. unsigned int mode_offset;
  177. u64 mode_mask;
  178. int arg_idx;
  179. bool is_bit_pos = true;
  180. if (strcmp(token, cmd))
  181. continue;
  182. mode_offset = cec_error_inj_cmds[i].mode_offset;
  183. mode_mask = CEC_ERROR_INJ_MODE_MASK << mode_offset;
  184. arg_idx = cec_error_inj_cmds[i].arg_idx;
  185. if (mode_offset == CEC_ERROR_INJ_RX_ARB_LOST_OFFSET) {
  186. if (has_op)
  187. return false;
  188. if (!has_pos)
  189. pos = 0x0f;
  190. is_bit_pos = false;
  191. } else if (mode_offset == CEC_ERROR_INJ_TX_ADD_BYTES_OFFSET) {
  192. if (!has_pos || !pos)
  193. return false;
  194. is_bit_pos = false;
  195. }
  196. if (arg_idx >= 0 && is_bit_pos) {
  197. if (!has_pos || pos >= 160)
  198. return false;
  199. if (has_op && pos < 10 + 8)
  200. return false;
  201. /* Invalid bit position may not be the Ack bit */
  202. if ((mode_offset == CEC_ERROR_INJ_TX_SHORT_BIT_OFFSET ||
  203. mode_offset == CEC_ERROR_INJ_TX_LONG_BIT_OFFSET ||
  204. mode_offset == CEC_ERROR_INJ_TX_CUSTOM_BIT_OFFSET) &&
  205. (pos % 10) == 9)
  206. return false;
  207. }
  208. *error &= ~mode_mask;
  209. *error |= (u64)mode << mode_offset;
  210. if (arg_idx >= 0)
  211. args[arg_idx] = pos;
  212. return true;
  213. }
  214. return false;
  215. }
  216. static void cec_pin_show_cmd(struct seq_file *sf, u32 cmd, u8 mode)
  217. {
  218. if (cmd == CEC_ERROR_INJ_OP_ANY)
  219. seq_puts(sf, "any,");
  220. else
  221. seq_printf(sf, "0x%02x,", cmd);
  222. switch (mode) {
  223. case CEC_ERROR_INJ_MODE_ONCE:
  224. seq_puts(sf, "once ");
  225. break;
  226. case CEC_ERROR_INJ_MODE_ALWAYS:
  227. seq_puts(sf, "always ");
  228. break;
  229. case CEC_ERROR_INJ_MODE_TOGGLE:
  230. seq_puts(sf, "toggle ");
  231. break;
  232. default:
  233. seq_puts(sf, "off ");
  234. break;
  235. }
  236. }
  237. int cec_pin_error_inj_show(struct cec_adapter *adap, struct seq_file *sf)
  238. {
  239. struct cec_pin *pin = adap->pin;
  240. unsigned int i, j;
  241. seq_puts(sf, "# Clear error injections:\n");
  242. seq_puts(sf, "# clear clear all rx and tx error injections\n");
  243. seq_puts(sf, "# rx-clear clear all rx error injections\n");
  244. seq_puts(sf, "# tx-clear clear all tx error injections\n");
  245. seq_puts(sf, "# <op> clear clear all rx and tx error injections for <op>\n");
  246. seq_puts(sf, "# <op> rx-clear clear all rx error injections for <op>\n");
  247. seq_puts(sf, "# <op> tx-clear clear all tx error injections for <op>\n");
  248. seq_puts(sf, "#\n");
  249. seq_puts(sf, "# RX error injection:\n");
  250. seq_puts(sf, "# <op>[,<mode>] rx-nack NACK the message instead of sending an ACK\n");
  251. seq_puts(sf, "# <op>[,<mode>] rx-low-drive <bit> force a low-drive condition at this bit position\n");
  252. seq_puts(sf, "# <op>[,<mode>] rx-add-byte add a spurious byte to the received CEC message\n");
  253. seq_puts(sf, "# <op>[,<mode>] rx-remove-byte remove the last byte from the received CEC message\n");
  254. seq_puts(sf, "# <op>[,<mode>] rx-arb-lost <poll> generate a POLL message to trigger an arbitration lost\n");
  255. seq_puts(sf, "#\n");
  256. seq_puts(sf, "# TX error injection settings:\n");
  257. seq_puts(sf, "# tx-ignore-nack-until-eom ignore early NACKs until EOM\n");
  258. seq_puts(sf, "# tx-custom-low-usecs <usecs> define the 'low' time for the custom pulse\n");
  259. seq_puts(sf, "# tx-custom-high-usecs <usecs> define the 'high' time for the custom pulse\n");
  260. seq_puts(sf, "# tx-custom-pulse transmit the custom pulse once the bus is idle\n");
  261. seq_puts(sf, "#\n");
  262. seq_puts(sf, "# TX error injection:\n");
  263. seq_puts(sf, "# <op>[,<mode>] tx-no-eom don't set the EOM bit\n");
  264. seq_puts(sf, "# <op>[,<mode>] tx-early-eom set the EOM bit one byte too soon\n");
  265. seq_puts(sf, "# <op>[,<mode>] tx-add-bytes <num> append <num> (1-255) spurious bytes to the message\n");
  266. seq_puts(sf, "# <op>[,<mode>] tx-remove-byte drop the last byte from the message\n");
  267. seq_puts(sf, "# <op>[,<mode>] tx-short-bit <bit> make this bit shorter than allowed\n");
  268. seq_puts(sf, "# <op>[,<mode>] tx-long-bit <bit> make this bit longer than allowed\n");
  269. seq_puts(sf, "# <op>[,<mode>] tx-custom-bit <bit> send the custom pulse instead of this bit\n");
  270. seq_puts(sf, "# <op>[,<mode>] tx-short-start send a start pulse that's too short\n");
  271. seq_puts(sf, "# <op>[,<mode>] tx-long-start send a start pulse that's too long\n");
  272. seq_puts(sf, "# <op>[,<mode>] tx-custom-start send the custom pulse instead of the start pulse\n");
  273. seq_puts(sf, "# <op>[,<mode>] tx-last-bit <bit> stop sending after this bit\n");
  274. seq_puts(sf, "# <op>[,<mode>] tx-low-drive <bit> force a low-drive condition at this bit position\n");
  275. seq_puts(sf, "#\n");
  276. seq_puts(sf, "# <op> CEC message opcode (0-255) or 'any'\n");
  277. seq_puts(sf, "# <mode> 'once' (default), 'always', 'toggle' or 'off'\n");
  278. seq_puts(sf, "# <bit> CEC message bit (0-159)\n");
  279. seq_puts(sf, "# 10 bits per 'byte': bits 0-7: data, bit 8: EOM, bit 9: ACK\n");
  280. seq_puts(sf, "# <poll> CEC poll message used to test arbitration lost (0x00-0xff, default 0x0f)\n");
  281. seq_puts(sf, "# <usecs> microseconds (0-10000000, default 1000)\n");
  282. seq_puts(sf, "\nclear\n");
  283. for (i = 0; i < ARRAY_SIZE(pin->error_inj); i++) {
  284. u64 e = pin->error_inj[i];
  285. for (j = 0; cec_error_inj_cmds[j].cmd; j++) {
  286. const char *cmd = cec_error_inj_cmds[j].cmd;
  287. unsigned int mode;
  288. unsigned int mode_offset;
  289. int arg_idx;
  290. mode_offset = cec_error_inj_cmds[j].mode_offset;
  291. arg_idx = cec_error_inj_cmds[j].arg_idx;
  292. mode = (e >> mode_offset) & CEC_ERROR_INJ_MODE_MASK;
  293. if (!mode)
  294. continue;
  295. cec_pin_show_cmd(sf, i, mode);
  296. seq_puts(sf, cmd);
  297. if (arg_idx >= 0)
  298. seq_printf(sf, " %u",
  299. pin->error_inj_args[i][arg_idx]);
  300. seq_puts(sf, "\n");
  301. }
  302. }
  303. if (pin->tx_ignore_nack_until_eom)
  304. seq_puts(sf, "tx-ignore-nack-until-eom\n");
  305. if (pin->tx_custom_pulse)
  306. seq_puts(sf, "tx-custom-pulse\n");
  307. if (pin->tx_custom_low_usecs != CEC_TIM_CUSTOM_DEFAULT)
  308. seq_printf(sf, "tx-custom-low-usecs %u\n",
  309. pin->tx_custom_low_usecs);
  310. if (pin->tx_custom_high_usecs != CEC_TIM_CUSTOM_DEFAULT)
  311. seq_printf(sf, "tx-custom-high-usecs %u\n",
  312. pin->tx_custom_high_usecs);
  313. return 0;
  314. }