1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2017 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 #include <linux/delay.h>
7 #include <linux/sched/types.h>
8 #include <linux/seq_file.h>
9 #include <linux/slab.h>
11 #include <media/cec-pin.h>
12 #include "cec-pin-priv.h"
14 struct cec_error_inj_cmd {
15 unsigned int mode_offset;
20 static const struct cec_error_inj_cmd cec_error_inj_cmds[] = {
21 { CEC_ERROR_INJ_RX_NACK_OFFSET, -1, "rx-nack" },
22 { CEC_ERROR_INJ_RX_LOW_DRIVE_OFFSET,
23 CEC_ERROR_INJ_RX_LOW_DRIVE_ARG_IDX, "rx-low-drive" },
24 { CEC_ERROR_INJ_RX_ADD_BYTE_OFFSET, -1, "rx-add-byte" },
25 { CEC_ERROR_INJ_RX_REMOVE_BYTE_OFFSET, -1, "rx-remove-byte" },
26 { CEC_ERROR_INJ_RX_ARB_LOST_OFFSET,
27 CEC_ERROR_INJ_RX_ARB_LOST_ARG_IDX, "rx-arb-lost" },
29 { CEC_ERROR_INJ_TX_NO_EOM_OFFSET, -1, "tx-no-eom" },
30 { CEC_ERROR_INJ_TX_EARLY_EOM_OFFSET, -1, "tx-early-eom" },
31 { CEC_ERROR_INJ_TX_ADD_BYTES_OFFSET,
32 CEC_ERROR_INJ_TX_ADD_BYTES_ARG_IDX, "tx-add-bytes" },
33 { CEC_ERROR_INJ_TX_REMOVE_BYTE_OFFSET, -1, "tx-remove-byte" },
34 { CEC_ERROR_INJ_TX_SHORT_BIT_OFFSET,
35 CEC_ERROR_INJ_TX_SHORT_BIT_ARG_IDX, "tx-short-bit" },
36 { CEC_ERROR_INJ_TX_LONG_BIT_OFFSET,
37 CEC_ERROR_INJ_TX_LONG_BIT_ARG_IDX, "tx-long-bit" },
38 { CEC_ERROR_INJ_TX_CUSTOM_BIT_OFFSET,
39 CEC_ERROR_INJ_TX_CUSTOM_BIT_ARG_IDX, "tx-custom-bit" },
40 { CEC_ERROR_INJ_TX_SHORT_START_OFFSET, -1, "tx-short-start" },
41 { CEC_ERROR_INJ_TX_LONG_START_OFFSET, -1, "tx-long-start" },
42 { CEC_ERROR_INJ_TX_CUSTOM_START_OFFSET, -1, "tx-custom-start" },
43 { CEC_ERROR_INJ_TX_LAST_BIT_OFFSET,
44 CEC_ERROR_INJ_TX_LAST_BIT_ARG_IDX, "tx-last-bit" },
45 { CEC_ERROR_INJ_TX_LOW_DRIVE_OFFSET,
46 CEC_ERROR_INJ_TX_LOW_DRIVE_ARG_IDX, "tx-low-drive" },
50 u16 cec_pin_rx_error_inj(struct cec_pin *pin)
52 u16 cmd = CEC_ERROR_INJ_OP_ANY;
54 /* Only when 18 bits have been received do we have a valid cmd */
55 if (!(pin->error_inj[cmd] & CEC_ERROR_INJ_RX_MASK) &&
57 cmd = pin->rx_msg.msg[1];
58 return (pin->error_inj[cmd] & CEC_ERROR_INJ_RX_MASK) ? cmd :
62 u16 cec_pin_tx_error_inj(struct cec_pin *pin)
64 u16 cmd = CEC_ERROR_INJ_OP_ANY;
66 if (!(pin->error_inj[cmd] & CEC_ERROR_INJ_TX_MASK) &&
68 cmd = pin->tx_msg.msg[1];
69 return (pin->error_inj[cmd] & CEC_ERROR_INJ_TX_MASK) ? cmd :
73 bool cec_pin_error_inj_parse_line(struct cec_adapter *adap, char *line)
75 static const char *delims = " \t\r";
76 struct cec_pin *pin = adap->pin;
90 token = strsep(&p, delims);
91 if (!strcmp(token, "clear")) {
92 memset(pin->error_inj, 0, sizeof(pin->error_inj));
93 pin->rx_toggle = pin->tx_toggle = false;
94 pin->tx_ignore_nack_until_eom = false;
95 pin->tx_custom_pulse = false;
96 pin->tx_custom_low_usecs = CEC_TIM_CUSTOM_DEFAULT;
97 pin->tx_custom_high_usecs = CEC_TIM_CUSTOM_DEFAULT;
100 if (!strcmp(token, "rx-clear")) {
101 for (i = 0; i <= CEC_ERROR_INJ_OP_ANY; i++)
102 pin->error_inj[i] &= ~CEC_ERROR_INJ_RX_MASK;
103 pin->rx_toggle = false;
106 if (!strcmp(token, "tx-clear")) {
107 for (i = 0; i <= CEC_ERROR_INJ_OP_ANY; i++)
108 pin->error_inj[i] &= ~CEC_ERROR_INJ_TX_MASK;
109 pin->tx_toggle = false;
110 pin->tx_ignore_nack_until_eom = false;
111 pin->tx_custom_pulse = false;
112 pin->tx_custom_low_usecs = CEC_TIM_CUSTOM_DEFAULT;
113 pin->tx_custom_high_usecs = CEC_TIM_CUSTOM_DEFAULT;
116 if (!strcmp(token, "tx-ignore-nack-until-eom")) {
117 pin->tx_ignore_nack_until_eom = true;
120 if (!strcmp(token, "tx-custom-pulse")) {
121 pin->tx_custom_pulse = true;
122 cec_pin_start_timer(pin);
129 if (!strcmp(token, "tx-custom-low-usecs")) {
132 if (kstrtou32(p, 0, &usecs) || usecs > 10000000)
134 pin->tx_custom_low_usecs = usecs;
137 if (!strcmp(token, "tx-custom-high-usecs")) {
140 if (kstrtou32(p, 0, &usecs) || usecs > 10000000)
142 pin->tx_custom_high_usecs = usecs;
146 comma = strchr(token, ',');
149 if (!strcmp(token, "any")) {
151 error = pin->error_inj + CEC_ERROR_INJ_OP_ANY;
152 args = pin->error_inj_args[CEC_ERROR_INJ_OP_ANY];
153 } else if (!kstrtou8(token, 0, &op)) {
155 error = pin->error_inj + op;
156 args = pin->error_inj_args[op];
161 mode = CEC_ERROR_INJ_MODE_ONCE;
163 if (!strcmp(comma, "off"))
164 mode = CEC_ERROR_INJ_MODE_OFF;
165 else if (!strcmp(comma, "once"))
166 mode = CEC_ERROR_INJ_MODE_ONCE;
167 else if (!strcmp(comma, "always"))
168 mode = CEC_ERROR_INJ_MODE_ALWAYS;
169 else if (!strcmp(comma, "toggle"))
170 mode = CEC_ERROR_INJ_MODE_TOGGLE;
175 token = strsep(&p, delims);
178 has_pos = !kstrtou8(p, 0, &pos);
181 if (!strcmp(token, "clear")) {
185 if (!strcmp(token, "rx-clear")) {
186 *error &= ~CEC_ERROR_INJ_RX_MASK;
189 if (!strcmp(token, "tx-clear")) {
190 *error &= ~CEC_ERROR_INJ_TX_MASK;
194 for (i = 0; cec_error_inj_cmds[i].cmd; i++) {
195 const char *cmd = cec_error_inj_cmds[i].cmd;
196 unsigned int mode_offset;
199 bool is_bit_pos = true;
201 if (strcmp(token, cmd))
204 mode_offset = cec_error_inj_cmds[i].mode_offset;
205 mode_mask = CEC_ERROR_INJ_MODE_MASK << mode_offset;
206 arg_idx = cec_error_inj_cmds[i].arg_idx;
208 if (mode_offset == CEC_ERROR_INJ_RX_ARB_LOST_OFFSET) {
214 } else if (mode_offset == CEC_ERROR_INJ_TX_ADD_BYTES_OFFSET) {
215 if (!has_pos || !pos)
220 if (arg_idx >= 0 && is_bit_pos) {
221 if (!has_pos || pos >= 160)
223 if (has_op && pos < 10 + 8)
225 /* Invalid bit position may not be the Ack bit */
226 if ((mode_offset == CEC_ERROR_INJ_TX_SHORT_BIT_OFFSET ||
227 mode_offset == CEC_ERROR_INJ_TX_LONG_BIT_OFFSET ||
228 mode_offset == CEC_ERROR_INJ_TX_CUSTOM_BIT_OFFSET) &&
232 *error &= ~mode_mask;
233 *error |= (u64)mode << mode_offset;
241 static void cec_pin_show_cmd(struct seq_file *sf, u32 cmd, u8 mode)
243 if (cmd == CEC_ERROR_INJ_OP_ANY)
244 seq_puts(sf, "any,");
246 seq_printf(sf, "0x%02x,", cmd);
248 case CEC_ERROR_INJ_MODE_ONCE:
249 seq_puts(sf, "once ");
251 case CEC_ERROR_INJ_MODE_ALWAYS:
252 seq_puts(sf, "always ");
254 case CEC_ERROR_INJ_MODE_TOGGLE:
255 seq_puts(sf, "toggle ");
258 seq_puts(sf, "off ");
263 int cec_pin_error_inj_show(struct cec_adapter *adap, struct seq_file *sf)
265 struct cec_pin *pin = adap->pin;
268 seq_puts(sf, "# Clear error injections:\n");
269 seq_puts(sf, "# clear clear all rx and tx error injections\n");
270 seq_puts(sf, "# rx-clear clear all rx error injections\n");
271 seq_puts(sf, "# tx-clear clear all tx error injections\n");
272 seq_puts(sf, "# <op> clear clear all rx and tx error injections for <op>\n");
273 seq_puts(sf, "# <op> rx-clear clear all rx error injections for <op>\n");
274 seq_puts(sf, "# <op> tx-clear clear all tx error injections for <op>\n");
276 seq_puts(sf, "# RX error injection:\n");
277 seq_puts(sf, "# <op>[,<mode>] rx-nack NACK the message instead of sending an ACK\n");
278 seq_puts(sf, "# <op>[,<mode>] rx-low-drive <bit> force a low-drive condition at this bit position\n");
279 seq_puts(sf, "# <op>[,<mode>] rx-add-byte add a spurious byte to the received CEC message\n");
280 seq_puts(sf, "# <op>[,<mode>] rx-remove-byte remove the last byte from the received CEC message\n");
281 seq_puts(sf, "# any[,<mode>] rx-arb-lost [<poll>] generate a POLL message to trigger an arbitration lost\n");
283 seq_puts(sf, "# TX error injection settings:\n");
284 seq_puts(sf, "# tx-ignore-nack-until-eom ignore early NACKs until EOM\n");
285 seq_puts(sf, "# tx-custom-low-usecs <usecs> define the 'low' time for the custom pulse\n");
286 seq_puts(sf, "# tx-custom-high-usecs <usecs> define the 'high' time for the custom pulse\n");
287 seq_puts(sf, "# tx-custom-pulse transmit the custom pulse once the bus is idle\n");
289 seq_puts(sf, "# TX error injection:\n");
290 seq_puts(sf, "# <op>[,<mode>] tx-no-eom don't set the EOM bit\n");
291 seq_puts(sf, "# <op>[,<mode>] tx-early-eom set the EOM bit one byte too soon\n");
292 seq_puts(sf, "# <op>[,<mode>] tx-add-bytes <num> append <num> (1-255) spurious bytes to the message\n");
293 seq_puts(sf, "# <op>[,<mode>] tx-remove-byte drop the last byte from the message\n");
294 seq_puts(sf, "# <op>[,<mode>] tx-short-bit <bit> make this bit shorter than allowed\n");
295 seq_puts(sf, "# <op>[,<mode>] tx-long-bit <bit> make this bit longer than allowed\n");
296 seq_puts(sf, "# <op>[,<mode>] tx-custom-bit <bit> send the custom pulse instead of this bit\n");
297 seq_puts(sf, "# <op>[,<mode>] tx-short-start send a start pulse that's too short\n");
298 seq_puts(sf, "# <op>[,<mode>] tx-long-start send a start pulse that's too long\n");
299 seq_puts(sf, "# <op>[,<mode>] tx-custom-start send the custom pulse instead of the start pulse\n");
300 seq_puts(sf, "# <op>[,<mode>] tx-last-bit <bit> stop sending after this bit\n");
301 seq_puts(sf, "# <op>[,<mode>] tx-low-drive <bit> force a low-drive condition at this bit position\n");
303 seq_puts(sf, "# <op> CEC message opcode (0-255) or 'any'\n");
304 seq_puts(sf, "# <mode> 'once' (default), 'always', 'toggle' or 'off'\n");
305 seq_puts(sf, "# <bit> CEC message bit (0-159)\n");
306 seq_puts(sf, "# 10 bits per 'byte': bits 0-7: data, bit 8: EOM, bit 9: ACK\n");
307 seq_puts(sf, "# <poll> CEC poll message used to test arbitration lost (0x00-0xff, default 0x0f)\n");
308 seq_puts(sf, "# <usecs> microseconds (0-10000000, default 1000)\n");
310 seq_puts(sf, "\nclear\n");
312 for (i = 0; i < ARRAY_SIZE(pin->error_inj); i++) {
313 u64 e = pin->error_inj[i];
315 for (j = 0; cec_error_inj_cmds[j].cmd; j++) {
316 const char *cmd = cec_error_inj_cmds[j].cmd;
318 unsigned int mode_offset;
321 mode_offset = cec_error_inj_cmds[j].mode_offset;
322 arg_idx = cec_error_inj_cmds[j].arg_idx;
323 mode = (e >> mode_offset) & CEC_ERROR_INJ_MODE_MASK;
326 cec_pin_show_cmd(sf, i, mode);
329 seq_printf(sf, " %u",
330 pin->error_inj_args[i][arg_idx]);
335 if (pin->tx_ignore_nack_until_eom)
336 seq_puts(sf, "tx-ignore-nack-until-eom\n");
337 if (pin->tx_custom_pulse)
338 seq_puts(sf, "tx-custom-pulse\n");
339 if (pin->tx_custom_low_usecs != CEC_TIM_CUSTOM_DEFAULT)
340 seq_printf(sf, "tx-custom-low-usecs %u\n",
341 pin->tx_custom_low_usecs);
342 if (pin->tx_custom_high_usecs != CEC_TIM_CUSTOM_DEFAULT)
343 seq_printf(sf, "tx-custom-high-usecs %u\n",
344 pin->tx_custom_high_usecs);