Change N_ELMCAN to N_DEVELOPMENT
[elmcan.git] / module / elmcan.c
index 9b4ea45d94d8f4fb44b575bbc812a1cd59e1397b..76ec682e9450b6d93d16d199ba426b604a2ce944 100644 (file)
 #include <linux/can/led.h>
 #include <linux/can/rx-offload.h>
 
-MODULE_ALIAS_LDISC(N_ELMCAN);
+MODULE_ALIAS_LDISC(N_DEVELOPMENT);
 MODULE_DESCRIPTION("ELM327 based CAN interface");
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Max Staudt <max-linux@enpas.org>");
 
-/* If this is enabled, we'll try to make the best of the situation
- * even if we receive unexpected characters on the line.
- * No guarantees.
- * Handle with care, it's likely your hardware is unreliable!
+/* Line discipline ID number.
+ * N_DEVELOPMENT will likely be defined from Linux 5.18 onwards:
+ * https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty.git/commit/?h=tty-next&id=c2faf737abfb10f88f2d2612d573e9edc3c42c37
  */
-static bool accept_flaky_uart;
-module_param_named(accept_flaky_uart, accept_flaky_uart, bool, 0444);
-MODULE_PARM_DESC(accept_flaky_uart, "Don't bail at the first invalid character. Behavior undefined.");
-
-/* Line discipline ID number */
-#ifndef N_ELMCAN
-#define N_ELMCAN 29
+#ifndef N_DEVELOPMENT
+#define N_DEVELOPMENT 29
 #endif
 
 #define ELM327_NAPI_WEIGHT 4
@@ -343,6 +337,12 @@ static inline void elm327_hw_failure(struct elmcan *elm)
        can_bus_off(elm->dev);
 }
 
+/* Compare a buffer to a fixed string */
+static int _memstrcmp(const u8 *mem, const char *str)
+{
+       return memcmp(mem, str, strlen(str));
+}
+
 /* Assumes elm->lock taken. */
 static void elm327_parse_error(struct elmcan *elm, int len)
 {
@@ -352,15 +352,16 @@ static void elm327_parse_error(struct elmcan *elm, int len)
        frame.can_id = CAN_ERR_FLAG;
        frame.can_dlc = CAN_ERR_DLC;
 
+       /* Filter possible error messages based on length of RX'd line */
        switch (len) {
        case 17:
-               if (!memcmp(elm->rxbuf, "UNABLE TO CONNECT", 17)) {
+               if (!_memstrcmp(elm->rxbuf, "UNABLE TO CONNECT")) {
                        netdev_err(elm->dev,
                                   "ELM327 reported UNABLE TO CONNECT. Please check your setup.\n");
                }
                break;
        case 11:
-               if (!memcmp(elm->rxbuf, "BUFFER FULL", 11)) {
+               if (!_memstrcmp(elm->rxbuf, "BUFFER FULL")) {
                        /* This case will only happen if the last data
                         * line was complete.
                         * Otherwise, elm327_parse_frame() will heuristically
@@ -371,33 +372,36 @@ static void elm327_parse_error(struct elmcan *elm, int len)
                }
                break;
        case 9:
-               if (!memcmp(elm->rxbuf, "BUS ERROR", 9))
+               if (!_memstrcmp(elm->rxbuf, "BUS ERROR"))
                        frame.can_id |= CAN_ERR_BUSERROR;
-               if (!memcmp(elm->rxbuf, "CAN ERROR", 9))
+               if (!_memstrcmp(elm->rxbuf, "CAN ERROR"))
                        frame.can_id |= CAN_ERR_PROT;
-               if (!memcmp(elm->rxbuf, "<RX ERROR", 9))
+               if (!_memstrcmp(elm->rxbuf, "<RX ERROR"))
                        frame.can_id |= CAN_ERR_PROT;
                break;
        case 8:
-               if (!memcmp(elm->rxbuf, "BUS BUSY", 8)) {
+               if (!_memstrcmp(elm->rxbuf, "BUS BUSY")) {
                        frame.can_id |= CAN_ERR_PROT;
                        frame.data[2] = CAN_ERR_PROT_OVERLOAD;
                }
-               if (!memcmp(elm->rxbuf, "FB ERROR", 8)) {
+               if (!_memstrcmp(elm->rxbuf, "FB ERROR")) {
                        frame.can_id |= CAN_ERR_PROT;
                        frame.data[2] = CAN_ERR_PROT_TX;
                }
                break;
-       case 5:
-               if (!memcmp(elm->rxbuf, "ERR", 3)) {
+       case 5: /* ERR is followed by two digits, hence line length 5 */
+               if (!_memstrcmp(elm->rxbuf, "ERR")) {
                        netdev_err(elm->dev, "ELM327 reported an ERR%c%c. Please power it off and on again.\n",
                                   elm->rxbuf[3], elm->rxbuf[4]);
                        frame.can_id |= CAN_ERR_CRTL;
                }
                break;
        default:
-               /* Don't emit an error frame if we're unsure */
-               return;
+               /* Something else has happened.
+                * Maybe garbage on the UART line.
+                * Emit a generic error frame.
+                */
+               break;
        }
 
        elm327_feed_frame_to_netdev(elm, &frame);
@@ -438,12 +442,10 @@ static int elm327_parse_frame(struct elmcan *elm, int len)
                }
        }
 
-       /* If we accept stray characters coming in:
-        * Check for stray characters on a payload line.
-        * No idea what causes this.
+       /* Sanity check whether the line is really a clean hexdump,
+        * or terminated by an error message, or contains garbage.
         */
-       if (accept_flaky_uart &&
-           hexlen < len &&
+       if (hexlen < len &&
            !isdigit(elm->rxbuf[hexlen]) &&
            !isupper(elm->rxbuf[hexlen]) &&
            '<' != elm->rxbuf[hexlen] &&
@@ -451,7 +453,6 @@ static int elm327_parse_frame(struct elmcan *elm, int len)
                /* The line is likely garbled anyway, so bail.
                 * The main code will restart listening.
                 */
-               elm327_kick_into_cmd_mode(elm);
                return -ENODATA;
        }
 
@@ -506,7 +507,7 @@ static int elm327_parse_frame(struct elmcan *elm, int len)
 
        /* Check for RTR frame */
        if (elm->rxfill >= hexlen + 3 &&
-           !memcmp(&elm->rxbuf[hexlen], "RTR", 3)) {
+           !_memstrcmp(&elm->rxbuf[hexlen], "RTR")) {
                frame.can_id |= CAN_RTR_FLAG;
        }
 
@@ -556,7 +557,7 @@ static void elm327_parse_line(struct elmcan *elm, int len)
        if (elm->drop_next_line) {
                elm->drop_next_line = 0;
                return;
-       } else if (elm->rxbuf[0] == 'A' && elm->rxbuf[1] == 'T') {
+       } else if (!_memstrcmp(elm->rxbuf, "AT")) {
                return;
        }
 
@@ -929,8 +930,7 @@ static void put_elm(struct elmcan *elm)
 
 static bool elmcan_is_valid_rx_char(char c)
 {
-       return (accept_flaky_uart ||
-               isdigit(c) ||
+       return (isdigit(c) ||
                isupper(c) ||
                c == ELM327_MAGIC_CHAR ||
                c == ELM327_READY_CHAR ||
@@ -1092,8 +1092,6 @@ static const u32 elmcan_bitrate_const[64] = {
 /* Dummy needed to use bitrate_const */
 static int elmcan_do_set_bittiming(struct net_device *netdev)
 {
-       (void)netdev;
-
        return 0;
 }
 
@@ -1254,7 +1252,7 @@ static int elmcan_ldisc_ioctl(struct tty_struct *tty,
 static struct tty_ldisc_ops elmcan_ldisc = {
        .owner          = THIS_MODULE,
        .name           = "elmcan",
-       .num            = N_ELMCAN,
+       .num            = N_DEVELOPMENT,
        .receive_buf    = elmcan_ldisc_rx,
        .write_wakeup   = elmcan_ldisc_tx_wakeup,
        .open           = elmcan_ldisc_open,
@@ -1267,11 +1265,8 @@ static int __init elmcan_init(void)
 {
        int status;
 
-       pr_info("ELM327 based best effort CAN interface driver\n");
-       pr_info("This device is severely limited as a CAN interface, see documentation.\n");
-
 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,14,0)
-       status = tty_register_ldisc(N_ELMCAN, &elmcan_ldisc);
+       status = tty_register_ldisc(N_DEVELOPMENT, &elmcan_ldisc);
 #else
        status = tty_register_ldisc(&elmcan_ldisc);
 #endif
@@ -1289,7 +1284,7 @@ static void __exit elmcan_exit(void)
 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,14,0)
        int status;
 
-       status = tty_unregister_ldisc(N_ELMCAN);
+       status = tty_unregister_ldisc(N_DEVELOPMENT);
        if (status)
                pr_err("Can't unregister line discipline (error: %d)\n",
                       status);