cec-pin-error-inj.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. u32 op;
  75. u8 mode;
  76. u8 pos;
  77. u8 v;
  78. p = skip_spaces(p);
  79. token = strsep(&p, delims);
  80. if (!strcmp(token, "clear")) {
  81. memset(pin->error_inj, 0, sizeof(pin->error_inj));
  82. pin->rx_toggle = pin->tx_toggle = false;
  83. pin->tx_ignore_nack_until_eom = false;
  84. pin->tx_custom_pulse = false;
  85. pin->tx_custom_low_usecs = CEC_TIM_CUSTOM_DEFAULT;
  86. pin->tx_custom_high_usecs = CEC_TIM_CUSTOM_DEFAULT;
  87. return true;
  88. }
  89. if (!strcmp(token, "rx-clear")) {
  90. for (i = 0; i <= CEC_ERROR_INJ_OP_ANY; i++)
  91. pin->error_inj[i] &= ~CEC_ERROR_INJ_RX_MASK;
  92. pin->rx_toggle = false;
  93. return true;
  94. }
  95. if (!strcmp(token, "tx-clear")) {
  96. for (i = 0; i <= CEC_ERROR_INJ_OP_ANY; i++)
  97. pin->error_inj[i] &= ~CEC_ERROR_INJ_TX_MASK;
  98. pin->tx_toggle = false;
  99. pin->tx_ignore_nack_until_eom = false;
  100. pin->tx_custom_pulse = false;
  101. pin->tx_custom_low_usecs = CEC_TIM_CUSTOM_DEFAULT;
  102. pin->tx_custom_high_usecs = CEC_TIM_CUSTOM_DEFAULT;
  103. return true;
  104. }
  105. if (!strcmp(token, "tx-ignore-nack-until-eom")) {
  106. pin->tx_ignore_nack_until_eom = true;
  107. return true;
  108. }
  109. if (!strcmp(token, "tx-custom-pulse")) {
  110. pin->tx_custom_pulse = true;
  111. cec_pin_start_timer(pin);
  112. return true;
  113. }
  114. if (!p)
  115. return false;
  116. p = skip_spaces(p);
  117. if (!strcmp(token, "tx-custom-low-usecs")) {
  118. u32 usecs;
  119. if (kstrtou32(p, 0, &usecs) || usecs > 10000000)
  120. return false;
  121. pin->tx_custom_low_usecs = usecs;
  122. return true;
  123. }
  124. if (!strcmp(token, "tx-custom-high-usecs")) {
  125. u32 usecs;
  126. if (kstrtou32(p, 0, &usecs) || usecs > 10000000)
  127. return false;
  128. pin->tx_custom_high_usecs = usecs;
  129. return true;
  130. }
  131. comma = strchr(token, ',');
  132. if (comma)
  133. *comma++ = '\0';
  134. if (!strcmp(token, "any"))
  135. op = CEC_ERROR_INJ_OP_ANY;
  136. else if (!kstrtou8(token, 0, &v))
  137. op = v;
  138. else
  139. return false;
  140. mode = CEC_ERROR_INJ_MODE_ONCE;
  141. if (comma) {
  142. if (!strcmp(comma, "off"))
  143. mode = CEC_ERROR_INJ_MODE_OFF;
  144. else if (!strcmp(comma, "once"))
  145. mode = CEC_ERROR_INJ_MODE_ONCE;
  146. else if (!strcmp(comma, "always"))
  147. mode = CEC_ERROR_INJ_MODE_ALWAYS;
  148. else if (!strcmp(comma, "toggle"))
  149. mode = CEC_ERROR_INJ_MODE_TOGGLE;
  150. else
  151. return false;
  152. }
  153. error = pin->error_inj + op;
  154. args = pin->error_inj_args[op];
  155. has_op = op <= 0xff;
  156. token = strsep(&p, delims);
  157. if (p) {
  158. p = skip_spaces(p);
  159. has_pos = !kstrtou8(p, 0, &pos);
  160. }
  161. if (!strcmp(token, "clear")) {
  162. *error = 0;
  163. return true;
  164. }
  165. if (!strcmp(token, "rx-clear")) {
  166. *error &= ~CEC_ERROR_INJ_RX_MASK;
  167. return true;
  168. }
  169. if (!strcmp(token, "tx-clear")) {
  170. *error &= ~CEC_ERROR_INJ_TX_MASK;
  171. return true;
  172. }
  173. for (i = 0; cec_error_inj_cmds[i].cmd; i++) {
  174. const char *cmd = cec_error_inj_cmds[i].cmd;
  175. unsigned int mode_offset;
  176. u64 mode_mask;
  177. int arg_idx;
  178. bool is_bit_pos = true;
  179. if (strcmp(token, cmd))
  180. continue;
  181. mode_offset = cec_error_inj_cmds[i].mode_offset;
  182. mode_mask = CEC_ERROR_INJ_MODE_MASK << mode_offset;
  183. arg_idx = cec_error_inj_cmds[i].arg_idx;
  184. if (mode_offset == CEC_ERROR_INJ_RX_ARB_LOST_OFFSET) {
  185. if (has_op)
  186. return false;
  187. if (!has_pos)
  188. pos = 0x0f;
  189. is_bit_pos = false;
  190. } else if (mode_offset == CEC_ERROR_INJ_TX_ADD_BYTES_OFFSET) {
  191. if (!has_pos || !pos)
  192. return false;
  193. is_bit_pos = false;
  194. }
  195. if (arg_idx >= 0 && is_bit_pos) {
  196. if (!has_pos || pos >= 160)
  197. return false;
  198. if (has_op && pos < 10 + 8)
  199. return false;
  200. /* Invalid bit position may not be the Ack bit */
  201. if ((mode_offset == CEC_ERROR_INJ_TX_SHORT_BIT_OFFSET ||
  202. mode_offset == CEC_ERROR_INJ_TX_LONG_BIT_OFFSET ||
  203. mode_offset == CEC_ERROR_INJ_TX_CUSTOM_BIT_OFFSET) &&
  204. (pos % 10) == 9)
  205. return false;
  206. }
  207. *error &= ~mode_mask;
  208. *error |= (u64)mode << mode_offset;
  209. if (arg_idx >= 0)
  210. args[arg_idx] = pos;
  211. return true;
  212. }
  213. return false;
  214. }
  215. static void cec_pin_show_cmd(struct seq_file *sf, u32 cmd, u8 mode)
  216. {
  217. if (cmd == CEC_ERROR_INJ_OP_ANY)
  218. seq_puts(sf, "any,");
  219. else
  220. seq_printf(sf, "0x%02x,", cmd);
  221. switch (mode) {
  222. case CEC_ERROR_INJ_MODE_ONCE:
  223. seq_puts(sf, "once ");
  224. break;
  225. case CEC_ERROR_INJ_MODE_ALWAYS:
  226. seq_puts(sf, "always ");
  227. break;
  228. case CEC_ERROR_INJ_MODE_TOGGLE:
  229. seq_puts(sf, "toggle ");
  230. break;
  231. default:
  232. seq_puts(sf, "off ");
  233. break;
  234. }
  235. }
  236. int cec_pin_error_inj_show(struct cec_adapter *adap, struct seq_file *sf)
  237. {
  238. struct cec_pin *pin = adap->pin;
  239. unsigned int i, j;
  240. seq_puts(sf, "# Clear error injections:\n");
  241. seq_puts(sf, "# clear clear all rx and tx error injections\n");
  242. seq_puts(sf, "# rx-clear clear all rx error injections\n");
  243. seq_puts(sf, "# tx-clear clear all tx error injections\n");
  244. seq_puts(sf, "# <op> clear clear all rx and tx error injections for <op>\n");
  245. seq_puts(sf, "# <op> rx-clear clear all rx error injections for <op>\n");
  246. seq_puts(sf, "# <op> tx-clear clear all tx error injections for <op>\n");
  247. seq_puts(sf, "#\n");
  248. seq_puts(sf, "# RX error injection:\n");
  249. seq_puts(sf, "# <op>[,<mode>] rx-nack NACK the message instead of sending an ACK\n");
  250. seq_puts(sf, "# <op>[,<mode>] rx-low-drive <bit> force a low-drive condition at this bit position\n");
  251. seq_puts(sf, "# <op>[,<mode>] rx-add-byte add a spurious byte to the received CEC message\n");
  252. seq_puts(sf, "# <op>[,<mode>] rx-remove-byte remove the last byte from the received CEC message\n");
  253. seq_puts(sf, "# <op>[,<mode>] rx-arb-lost <poll> generate a POLL message to trigger an arbitration lost\n");
  254. seq_puts(sf, "#\n");
  255. seq_puts(sf, "# TX error injection settings:\n");
  256. seq_puts(sf, "# tx-ignore-nack-until-eom ignore early NACKs until EOM\n");
  257. seq_puts(sf, "# tx-custom-low-usecs <usecs> define the 'low' time for the custom pulse\n");
  258. seq_puts(sf, "# tx-custom-high-usecs <usecs> define the 'high' time for the custom pulse\n");
  259. seq_puts(sf, "# tx-custom-pulse transmit the custom pulse once the bus is idle\n");
  260. seq_puts(sf, "#\n");
  261. seq_puts(sf, "# TX error injection:\n");
  262. seq_puts(sf, "# <op>[,<mode>] tx-no-eom don't set the EOM bit\n");
  263. seq_puts(sf, "# <op>[,<mode>] tx-early-eom set the EOM bit one byte too soon\n");
  264. seq_puts(sf, "# <op>[,<mode>] tx-add-bytes <num> append <num> (1-255) spurious bytes to the message\n");
  265. seq_puts(sf, "# <op>[,<mode>] tx-remove-byte drop the last byte from the message\n");
  266. seq_puts(sf, "# <op>[,<mode>] tx-short-bit <bit> make this bit shorter than allowed\n");
  267. seq_puts(sf, "# <op>[,<mode>] tx-long-bit <bit> make this bit longer than allowed\n");
  268. seq_puts(sf, "# <op>[,<mode>] tx-custom-bit <bit> send the custom pulse instead of this bit\n");
  269. seq_puts(sf, "# <op>[,<mode>] tx-short-start send a start pulse that's too short\n");
  270. seq_puts(sf, "# <op>[,<mode>] tx-long-start send a start pulse that's too long\n");
  271. seq_puts(sf, "# <op>[,<mode>] tx-custom-start send the custom pulse instead of the start pulse\n");
  272. seq_puts(sf, "# <op>[,<mode>] tx-last-bit <bit> stop sending after this bit\n");
  273. seq_puts(sf, "# <op>[,<mode>] tx-low-drive <bit> force a low-drive condition at this bit position\n");
  274. seq_puts(sf, "#\n");
  275. seq_puts(sf, "# <op> CEC message opcode (0-255) or 'any'\n");
  276. seq_puts(sf, "# <mode> 'once' (default), 'always', 'toggle' or 'off'\n");
  277. seq_puts(sf, "# <bit> CEC message bit (0-159)\n");
  278. seq_puts(sf, "# 10 bits per 'byte': bits 0-7: data, bit 8: EOM, bit 9: ACK\n");
  279. seq_puts(sf, "# <poll> CEC poll message used to test arbitration lost (0x00-0xff, default 0x0f)\n");
  280. seq_puts(sf, "# <usecs> microseconds (0-10000000, default 1000)\n");
  281. seq_puts(sf, "\nclear\n");
  282. for (i = 0; i < ARRAY_SIZE(pin->error_inj); i++) {
  283. u64 e = pin->error_inj[i];
  284. for (j = 0; cec_error_inj_cmds[j].cmd; j++) {
  285. const char *cmd = cec_error_inj_cmds[j].cmd;
  286. unsigned int mode;
  287. unsigned int mode_offset;
  288. int arg_idx;
  289. mode_offset = cec_error_inj_cmds[j].mode_offset;
  290. arg_idx = cec_error_inj_cmds[j].arg_idx;
  291. mode = (e >> mode_offset) & CEC_ERROR_INJ_MODE_MASK;
  292. if (!mode)
  293. continue;
  294. cec_pin_show_cmd(sf, i, mode);
  295. seq_puts(sf, cmd);
  296. if (arg_idx >= 0)
  297. seq_printf(sf, " %u",
  298. pin->error_inj_args[i][arg_idx]);
  299. seq_puts(sf, "\n");
  300. }
  301. }
  302. if (pin->tx_ignore_nack_until_eom)
  303. seq_puts(sf, "tx-ignore-nack-until-eom\n");
  304. if (pin->tx_custom_pulse)
  305. seq_puts(sf, "tx-custom-pulse\n");
  306. if (pin->tx_custom_low_usecs != CEC_TIM_CUSTOM_DEFAULT)
  307. seq_printf(sf, "tx-custom-low-usecs %u\n",
  308. pin->tx_custom_low_usecs);
  309. if (pin->tx_custom_high_usecs != CEC_TIM_CUSTOM_DEFAULT)
  310. seq_printf(sf, "tx-custom-high-usecs %u\n",
  311. pin->tx_custom_high_usecs);
  312. return 0;
  313. }