Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright 2017 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
0004  */
0005 
0006 #include <linux/delay.h>
0007 #include <linux/slab.h>
0008 #include <linux/sched/types.h>
0009 
0010 #include <media/cec-pin.h>
0011 #include "cec-pin-priv.h"
0012 
0013 struct cec_error_inj_cmd {
0014     unsigned int mode_offset;
0015     int arg_idx;
0016     const char *cmd;
0017 };
0018 
0019 static const struct cec_error_inj_cmd cec_error_inj_cmds[] = {
0020     { CEC_ERROR_INJ_RX_NACK_OFFSET, -1, "rx-nack" },
0021     { CEC_ERROR_INJ_RX_LOW_DRIVE_OFFSET,
0022       CEC_ERROR_INJ_RX_LOW_DRIVE_ARG_IDX, "rx-low-drive" },
0023     { CEC_ERROR_INJ_RX_ADD_BYTE_OFFSET, -1, "rx-add-byte" },
0024     { CEC_ERROR_INJ_RX_REMOVE_BYTE_OFFSET, -1, "rx-remove-byte" },
0025     { CEC_ERROR_INJ_RX_ARB_LOST_OFFSET,
0026       CEC_ERROR_INJ_RX_ARB_LOST_ARG_IDX, "rx-arb-lost" },
0027 
0028     { CEC_ERROR_INJ_TX_NO_EOM_OFFSET, -1, "tx-no-eom" },
0029     { CEC_ERROR_INJ_TX_EARLY_EOM_OFFSET, -1, "tx-early-eom" },
0030     { CEC_ERROR_INJ_TX_ADD_BYTES_OFFSET,
0031       CEC_ERROR_INJ_TX_ADD_BYTES_ARG_IDX, "tx-add-bytes" },
0032     { CEC_ERROR_INJ_TX_REMOVE_BYTE_OFFSET, -1, "tx-remove-byte" },
0033     { CEC_ERROR_INJ_TX_SHORT_BIT_OFFSET,
0034       CEC_ERROR_INJ_TX_SHORT_BIT_ARG_IDX, "tx-short-bit" },
0035     { CEC_ERROR_INJ_TX_LONG_BIT_OFFSET,
0036       CEC_ERROR_INJ_TX_LONG_BIT_ARG_IDX, "tx-long-bit" },
0037     { CEC_ERROR_INJ_TX_CUSTOM_BIT_OFFSET,
0038       CEC_ERROR_INJ_TX_CUSTOM_BIT_ARG_IDX, "tx-custom-bit" },
0039     { CEC_ERROR_INJ_TX_SHORT_START_OFFSET, -1, "tx-short-start" },
0040     { CEC_ERROR_INJ_TX_LONG_START_OFFSET, -1, "tx-long-start" },
0041     { CEC_ERROR_INJ_TX_CUSTOM_START_OFFSET, -1, "tx-custom-start" },
0042     { CEC_ERROR_INJ_TX_LAST_BIT_OFFSET,
0043       CEC_ERROR_INJ_TX_LAST_BIT_ARG_IDX, "tx-last-bit" },
0044     { CEC_ERROR_INJ_TX_LOW_DRIVE_OFFSET,
0045       CEC_ERROR_INJ_TX_LOW_DRIVE_ARG_IDX, "tx-low-drive" },
0046     { 0, -1, NULL }
0047 };
0048 
0049 u16 cec_pin_rx_error_inj(struct cec_pin *pin)
0050 {
0051     u16 cmd = CEC_ERROR_INJ_OP_ANY;
0052 
0053     /* Only when 18 bits have been received do we have a valid cmd */
0054     if (!(pin->error_inj[cmd] & CEC_ERROR_INJ_RX_MASK) &&
0055         pin->rx_bit >= 18)
0056         cmd = pin->rx_msg.msg[1];
0057     return (pin->error_inj[cmd] & CEC_ERROR_INJ_RX_MASK) ? cmd :
0058         CEC_ERROR_INJ_OP_ANY;
0059 }
0060 
0061 u16 cec_pin_tx_error_inj(struct cec_pin *pin)
0062 {
0063     u16 cmd = CEC_ERROR_INJ_OP_ANY;
0064 
0065     if (!(pin->error_inj[cmd] & CEC_ERROR_INJ_TX_MASK) &&
0066         pin->tx_msg.len > 1)
0067         cmd = pin->tx_msg.msg[1];
0068     return (pin->error_inj[cmd] & CEC_ERROR_INJ_TX_MASK) ? cmd :
0069         CEC_ERROR_INJ_OP_ANY;
0070 }
0071 
0072 bool cec_pin_error_inj_parse_line(struct cec_adapter *adap, char *line)
0073 {
0074     static const char *delims = " \t\r";
0075     struct cec_pin *pin = adap->pin;
0076     unsigned int i;
0077     bool has_pos = false;
0078     char *p = line;
0079     char *token;
0080     char *comma;
0081     u64 *error;
0082     u8 *args;
0083     bool has_op;
0084     u8 op;
0085     u8 mode;
0086     u8 pos;
0087 
0088     p = skip_spaces(p);
0089     token = strsep(&p, delims);
0090     if (!strcmp(token, "clear")) {
0091         memset(pin->error_inj, 0, sizeof(pin->error_inj));
0092         pin->rx_toggle = pin->tx_toggle = false;
0093         pin->tx_ignore_nack_until_eom = false;
0094         pin->tx_custom_pulse = false;
0095         pin->tx_custom_low_usecs = CEC_TIM_CUSTOM_DEFAULT;
0096         pin->tx_custom_high_usecs = CEC_TIM_CUSTOM_DEFAULT;
0097         return true;
0098     }
0099     if (!strcmp(token, "rx-clear")) {
0100         for (i = 0; i <= CEC_ERROR_INJ_OP_ANY; i++)
0101             pin->error_inj[i] &= ~CEC_ERROR_INJ_RX_MASK;
0102         pin->rx_toggle = false;
0103         return true;
0104     }
0105     if (!strcmp(token, "tx-clear")) {
0106         for (i = 0; i <= CEC_ERROR_INJ_OP_ANY; i++)
0107             pin->error_inj[i] &= ~CEC_ERROR_INJ_TX_MASK;
0108         pin->tx_toggle = false;
0109         pin->tx_ignore_nack_until_eom = false;
0110         pin->tx_custom_pulse = false;
0111         pin->tx_custom_low_usecs = CEC_TIM_CUSTOM_DEFAULT;
0112         pin->tx_custom_high_usecs = CEC_TIM_CUSTOM_DEFAULT;
0113         return true;
0114     }
0115     if (!strcmp(token, "tx-ignore-nack-until-eom")) {
0116         pin->tx_ignore_nack_until_eom = true;
0117         return true;
0118     }
0119     if (!strcmp(token, "tx-custom-pulse")) {
0120         pin->tx_custom_pulse = true;
0121         cec_pin_start_timer(pin);
0122         return true;
0123     }
0124     if (!p)
0125         return false;
0126 
0127     p = skip_spaces(p);
0128     if (!strcmp(token, "tx-custom-low-usecs")) {
0129         u32 usecs;
0130 
0131         if (kstrtou32(p, 0, &usecs) || usecs > 10000000)
0132             return false;
0133         pin->tx_custom_low_usecs = usecs;
0134         return true;
0135     }
0136     if (!strcmp(token, "tx-custom-high-usecs")) {
0137         u32 usecs;
0138 
0139         if (kstrtou32(p, 0, &usecs) || usecs > 10000000)
0140             return false;
0141         pin->tx_custom_high_usecs = usecs;
0142         return true;
0143     }
0144 
0145     comma = strchr(token, ',');
0146     if (comma)
0147         *comma++ = '\0';
0148     if (!strcmp(token, "any")) {
0149         has_op = false;
0150         error = pin->error_inj + CEC_ERROR_INJ_OP_ANY;
0151         args = pin->error_inj_args[CEC_ERROR_INJ_OP_ANY];
0152     } else if (!kstrtou8(token, 0, &op)) {
0153         has_op = true;
0154         error = pin->error_inj + op;
0155         args = pin->error_inj_args[op];
0156     } else {
0157         return false;
0158     }
0159 
0160     mode = CEC_ERROR_INJ_MODE_ONCE;
0161     if (comma) {
0162         if (!strcmp(comma, "off"))
0163             mode = CEC_ERROR_INJ_MODE_OFF;
0164         else if (!strcmp(comma, "once"))
0165             mode = CEC_ERROR_INJ_MODE_ONCE;
0166         else if (!strcmp(comma, "always"))
0167             mode = CEC_ERROR_INJ_MODE_ALWAYS;
0168         else if (!strcmp(comma, "toggle"))
0169             mode = CEC_ERROR_INJ_MODE_TOGGLE;
0170         else
0171             return false;
0172     }
0173 
0174     token = strsep(&p, delims);
0175     if (p) {
0176         p = skip_spaces(p);
0177         has_pos = !kstrtou8(p, 0, &pos);
0178     }
0179 
0180     if (!strcmp(token, "clear")) {
0181         *error = 0;
0182         return true;
0183     }
0184     if (!strcmp(token, "rx-clear")) {
0185         *error &= ~CEC_ERROR_INJ_RX_MASK;
0186         return true;
0187     }
0188     if (!strcmp(token, "tx-clear")) {
0189         *error &= ~CEC_ERROR_INJ_TX_MASK;
0190         return true;
0191     }
0192 
0193     for (i = 0; cec_error_inj_cmds[i].cmd; i++) {
0194         const char *cmd = cec_error_inj_cmds[i].cmd;
0195         unsigned int mode_offset;
0196         u64 mode_mask;
0197         int arg_idx;
0198         bool is_bit_pos = true;
0199 
0200         if (strcmp(token, cmd))
0201             continue;
0202 
0203         mode_offset = cec_error_inj_cmds[i].mode_offset;
0204         mode_mask = CEC_ERROR_INJ_MODE_MASK << mode_offset;
0205         arg_idx = cec_error_inj_cmds[i].arg_idx;
0206 
0207         if (mode_offset == CEC_ERROR_INJ_RX_ARB_LOST_OFFSET) {
0208             if (has_op)
0209                 return false;
0210             if (!has_pos)
0211                 pos = 0x0f;
0212             is_bit_pos = false;
0213         } else if (mode_offset == CEC_ERROR_INJ_TX_ADD_BYTES_OFFSET) {
0214             if (!has_pos || !pos)
0215                 return false;
0216             is_bit_pos = false;
0217         }
0218 
0219         if (arg_idx >= 0 && is_bit_pos) {
0220             if (!has_pos || pos >= 160)
0221                 return false;
0222             if (has_op && pos < 10 + 8)
0223                 return false;
0224             /* Invalid bit position may not be the Ack bit */
0225             if ((mode_offset == CEC_ERROR_INJ_TX_SHORT_BIT_OFFSET ||
0226                  mode_offset == CEC_ERROR_INJ_TX_LONG_BIT_OFFSET ||
0227                  mode_offset == CEC_ERROR_INJ_TX_CUSTOM_BIT_OFFSET) &&
0228                 (pos % 10) == 9)
0229                 return false;
0230         }
0231         *error &= ~mode_mask;
0232         *error |= (u64)mode << mode_offset;
0233         if (arg_idx >= 0)
0234             args[arg_idx] = pos;
0235         return true;
0236     }
0237     return false;
0238 }
0239 
0240 static void cec_pin_show_cmd(struct seq_file *sf, u32 cmd, u8 mode)
0241 {
0242     if (cmd == CEC_ERROR_INJ_OP_ANY)
0243         seq_puts(sf, "any,");
0244     else
0245         seq_printf(sf, "0x%02x,", cmd);
0246     switch (mode) {
0247     case CEC_ERROR_INJ_MODE_ONCE:
0248         seq_puts(sf, "once ");
0249         break;
0250     case CEC_ERROR_INJ_MODE_ALWAYS:
0251         seq_puts(sf, "always ");
0252         break;
0253     case CEC_ERROR_INJ_MODE_TOGGLE:
0254         seq_puts(sf, "toggle ");
0255         break;
0256     default:
0257         seq_puts(sf, "off ");
0258         break;
0259     }
0260 }
0261 
0262 int cec_pin_error_inj_show(struct cec_adapter *adap, struct seq_file *sf)
0263 {
0264     struct cec_pin *pin = adap->pin;
0265     unsigned int i, j;
0266 
0267     seq_puts(sf, "# Clear error injections:\n");
0268     seq_puts(sf, "#   clear          clear all rx and tx error injections\n");
0269     seq_puts(sf, "#   rx-clear       clear all rx error injections\n");
0270     seq_puts(sf, "#   tx-clear       clear all tx error injections\n");
0271     seq_puts(sf, "#   <op> clear     clear all rx and tx error injections for <op>\n");
0272     seq_puts(sf, "#   <op> rx-clear  clear all rx error injections for <op>\n");
0273     seq_puts(sf, "#   <op> tx-clear  clear all tx error injections for <op>\n");
0274     seq_puts(sf, "#\n");
0275     seq_puts(sf, "# RX error injection:\n");
0276     seq_puts(sf, "#   <op>[,<mode>] rx-nack              NACK the message instead of sending an ACK\n");
0277     seq_puts(sf, "#   <op>[,<mode>] rx-low-drive <bit>   force a low-drive condition at this bit position\n");
0278     seq_puts(sf, "#   <op>[,<mode>] rx-add-byte          add a spurious byte to the received CEC message\n");
0279     seq_puts(sf, "#   <op>[,<mode>] rx-remove-byte       remove the last byte from the received CEC message\n");
0280     seq_puts(sf, "#    any[,<mode>] rx-arb-lost [<poll>] generate a POLL message to trigger an arbitration lost\n");
0281     seq_puts(sf, "#\n");
0282     seq_puts(sf, "# TX error injection settings:\n");
0283     seq_puts(sf, "#   tx-ignore-nack-until-eom           ignore early NACKs until EOM\n");
0284     seq_puts(sf, "#   tx-custom-low-usecs <usecs>        define the 'low' time for the custom pulse\n");
0285     seq_puts(sf, "#   tx-custom-high-usecs <usecs>       define the 'high' time for the custom pulse\n");
0286     seq_puts(sf, "#   tx-custom-pulse                    transmit the custom pulse once the bus is idle\n");
0287     seq_puts(sf, "#\n");
0288     seq_puts(sf, "# TX error injection:\n");
0289     seq_puts(sf, "#   <op>[,<mode>] tx-no-eom            don't set the EOM bit\n");
0290     seq_puts(sf, "#   <op>[,<mode>] tx-early-eom         set the EOM bit one byte too soon\n");
0291     seq_puts(sf, "#   <op>[,<mode>] tx-add-bytes <num>   append <num> (1-255) spurious bytes to the message\n");
0292     seq_puts(sf, "#   <op>[,<mode>] tx-remove-byte       drop the last byte from the message\n");
0293     seq_puts(sf, "#   <op>[,<mode>] tx-short-bit <bit>   make this bit shorter than allowed\n");
0294     seq_puts(sf, "#   <op>[,<mode>] tx-long-bit <bit>    make this bit longer than allowed\n");
0295     seq_puts(sf, "#   <op>[,<mode>] tx-custom-bit <bit>  send the custom pulse instead of this bit\n");
0296     seq_puts(sf, "#   <op>[,<mode>] tx-short-start       send a start pulse that's too short\n");
0297     seq_puts(sf, "#   <op>[,<mode>] tx-long-start        send a start pulse that's too long\n");
0298     seq_puts(sf, "#   <op>[,<mode>] tx-custom-start      send the custom pulse instead of the start pulse\n");
0299     seq_puts(sf, "#   <op>[,<mode>] tx-last-bit <bit>    stop sending after this bit\n");
0300     seq_puts(sf, "#   <op>[,<mode>] tx-low-drive <bit>   force a low-drive condition at this bit position\n");
0301     seq_puts(sf, "#\n");
0302     seq_puts(sf, "# <op>       CEC message opcode (0-255) or 'any'\n");
0303     seq_puts(sf, "# <mode>     'once' (default), 'always', 'toggle' or 'off'\n");
0304     seq_puts(sf, "# <bit>      CEC message bit (0-159)\n");
0305     seq_puts(sf, "#            10 bits per 'byte': bits 0-7: data, bit 8: EOM, bit 9: ACK\n");
0306     seq_puts(sf, "# <poll>     CEC poll message used to test arbitration lost (0x00-0xff, default 0x0f)\n");
0307     seq_puts(sf, "# <usecs>    microseconds (0-10000000, default 1000)\n");
0308 
0309     seq_puts(sf, "\nclear\n");
0310 
0311     for (i = 0; i < ARRAY_SIZE(pin->error_inj); i++) {
0312         u64 e = pin->error_inj[i];
0313 
0314         for (j = 0; cec_error_inj_cmds[j].cmd; j++) {
0315             const char *cmd = cec_error_inj_cmds[j].cmd;
0316             unsigned int mode;
0317             unsigned int mode_offset;
0318             int arg_idx;
0319 
0320             mode_offset = cec_error_inj_cmds[j].mode_offset;
0321             arg_idx = cec_error_inj_cmds[j].arg_idx;
0322             mode = (e >> mode_offset) & CEC_ERROR_INJ_MODE_MASK;
0323             if (!mode)
0324                 continue;
0325             cec_pin_show_cmd(sf, i, mode);
0326             seq_puts(sf, cmd);
0327             if (arg_idx >= 0)
0328                 seq_printf(sf, " %u",
0329                        pin->error_inj_args[i][arg_idx]);
0330             seq_puts(sf, "\n");
0331         }
0332     }
0333 
0334     if (pin->tx_ignore_nack_until_eom)
0335         seq_puts(sf, "tx-ignore-nack-until-eom\n");
0336     if (pin->tx_custom_pulse)
0337         seq_puts(sf, "tx-custom-pulse\n");
0338     if (pin->tx_custom_low_usecs != CEC_TIM_CUSTOM_DEFAULT)
0339         seq_printf(sf, "tx-custom-low-usecs %u\n",
0340                pin->tx_custom_low_usecs);
0341     if (pin->tx_custom_high_usecs != CEC_TIM_CUSTOM_DEFAULT)
0342         seq_printf(sf, "tx-custom-high-usecs %u\n",
0343                pin->tx_custom_high_usecs);
0344     return 0;
0345 }