Remove fixed array size for can327_bitrate_const
[elmcan.git] / module / can327.c
index 66233cc65f76d7ba7c3dfb7fcee6b2881cb3fff2..d522c08720563fb9c57b84829f83c9f79bf9386f 100644 (file)
 
 #include <linux/init.h>
 #include <linux/module.h>
-#include <linux/moduleparam.h>
 
-#include <linux/atomic.h>
 #include <linux/bitops.h>
 #include <linux/ctype.h>
-#include <linux/delay.h>
 #include <linux/errno.h>
-#include <linux/if_ether.h>
 #include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/lockdep.h>
@@ -57,7 +53,7 @@
 
 #define ELM327_NAPI_WEIGHT 4
 
-#define ELM327_SIZE_RXBUF 224
+#define ELM327_SIZE_RXBUF 992
 #define ELM327_SIZE_TXBUF 32
 
 #define ELM327_CAN_CONFIG_SEND_SFF           0x8000
@@ -90,7 +86,7 @@ struct can327 {
 
        /* TTY buffers */
        u8 rxbuf[ELM327_SIZE_RXBUF];
-       u8 txbuf[ELM327_SIZE_TXBUF] ____cacheline_aligned;
+       u8 txbuf[ELM327_SIZE_TXBUF];
 
        /* Per-channel lock */
        spinlock_t lock;
@@ -336,7 +332,7 @@ static inline void elm327_uart_side_failure(struct can327 *elm)
  * The reason to use strings is so we can easily include them in the C code,
  * and to avoid hardcoding lengths.
  */
-static inline int elm327_rxbuf_cmp(const u8 *buf, size_t nbytes, const char *reference)
+static inline bool elm327_rxbuf_cmp(const u8 *buf, size_t nbytes, const char *reference)
 {
        size_t ref_len = strlen(reference);
 
@@ -444,6 +440,7 @@ static int elm327_parse_frame(struct can327 *elm, size_t len)
                /* The line is likely garbled anyway, so bail.
                 * The main code will restart listening.
                 */
+               kfree_skb(skb);
                return -ENODATA;
        }
 
@@ -462,6 +459,7 @@ static int elm327_parse_frame(struct can327 *elm, size_t len)
                /* This is not a well-formatted data line.
                 * Assume it's an error message.
                 */
+               kfree_skb(skb);
                return -ENODATA;
        }
 
@@ -469,6 +467,7 @@ static int elm327_parse_frame(struct can327 *elm, size_t len)
                /* The line is too short to be a valid frame hex dump.
                 * Something interrupted the hex dump or it is invalid.
                 */
+               kfree_skb(skb);
                return -ENODATA;
        }
 
@@ -520,6 +519,7 @@ static int elm327_parse_frame(struct can327 *elm, size_t len)
                 * However, this will correctly drop the state machine back into
                 * command mode.
                 */
+               kfree_skb(skb);
                return -ENODATA;
        }
 
@@ -682,10 +682,9 @@ static void elm327_drop_bytes(struct can327 *elm, size_t i)
        elm->rxfill -= i;
 }
 
-static void elm327_parse_rxbuf(struct can327 *elm)
+static void elm327_parse_rxbuf(struct can327 *elm, size_t first_new_char_idx)
 {
-       size_t len;
-       int i;
+       size_t len, pos;
 
        lockdep_assert_held(&elm->lock);
 
@@ -696,20 +695,20 @@ static void elm327_parse_rxbuf(struct can327 *elm)
 
        case ELM327_STATE_GETDUMMYCHAR:
                /* Wait for 'y' or '>' */
-               for (i = 0; i < elm->rxfill; i++) {
-                       if (elm->rxbuf[i] == ELM327_DUMMY_CHAR) {
+               for (pos = 0; pos < elm->rxfill; pos++) {
+                       if (elm->rxbuf[pos] == ELM327_DUMMY_CHAR) {
                                elm327_send(elm, "\r", 1);
                                elm->state = ELM327_STATE_GETPROMPT;
-                               i++;
+                               pos++;
                                break;
-                       } else if (elm327_is_ready_char(elm->rxbuf[i])) {
+                       } else if (elm327_is_ready_char(elm->rxbuf[pos])) {
                                elm327_send(elm, ELM327_DUMMY_STRING, 1);
-                               i++;
+                               pos++;
                                break;
                        }
                }
 
-               elm327_drop_bytes(elm, i);
+               elm327_drop_bytes(elm, pos);
                break;
 
        case ELM327_STATE_GETPROMPT:
@@ -722,7 +721,7 @@ static void elm327_parse_rxbuf(struct can327 *elm)
 
        case ELM327_STATE_RECEIVING:
                /* Find <CR> delimiting feedback lines. */
-               len = 0;
+               len = first_new_char_idx;
                while (len < elm->rxfill && elm->rxbuf[len] != '\r')
                        len++;
 
@@ -756,13 +755,23 @@ static void elm327_parse_rxbuf(struct can327 *elm)
 
                        /* More data to parse? */
                        if (elm->rxfill)
-                               elm327_parse_rxbuf(elm);
+                               elm327_parse_rxbuf(elm, 0);
                }
        }
 }
 
 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,10,0)
 /* Dummy needed to use can_rx_offload */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5,5,0)
+static unsigned int *can327_mailbox_read(struct can_rx_offload *offload,
+                                        struct can_frame *cf,
+                                        u32 *timestamp, unsigned int n)
+{
+       WARN_ON_ONCE(1); /* This function is a dummy, so don't call it! */
+
+       return -ENOBUFS;
+}
+#else /* Since 4e9c9484b085 (included in v5.5) */
 static struct sk_buff *can327_mailbox_read(struct can_rx_offload *offload,
                                           unsigned int n, u32 *timestamp,
                                           bool drop)
@@ -772,6 +781,7 @@ static struct sk_buff *can327_mailbox_read(struct can_rx_offload *offload,
        return ERR_PTR(-ENOBUFS);
 }
 #endif
+#endif
 
 static int can327_netdev_open(struct net_device *dev)
 {
@@ -856,25 +866,26 @@ static netdev_tx_t can327_netdev_start_xmit(struct sk_buff *skb,
        if (can_dropped_invalid_skb(dev, skb))
                return NETDEV_TX_OK;
 
-       /* BHs are already disabled, so no spin_lock_bh().
-        * See Documentation/networking/netdevices.txt
+       /* This check will be part of can_dropped_invalid_skb()
+        * in future kernels.
         */
-       spin_lock(&elm->lock);
+       if (elm->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
+               goto out;
 
        /* We shouldn't get here after a hardware fault:
         * can_bus_off() calls netif_carrier_off()
         */
-       WARN_ON_ONCE(elm->uart_side_failure);
-
-       if (!elm->tty ||
-           elm->uart_side_failure ||
-           elm->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
-               spin_unlock(&elm->lock);
+       if (elm->uart_side_failure) {
+               WARN_ON_ONCE(elm->uart_side_failure);
                goto out;
        }
 
        netif_stop_queue(dev);
 
+       /* BHs are already disabled, so no spin_lock_bh().
+        * See Documentation/networking/netdevices.txt
+        */
+       spin_lock(&elm->lock);
        elm327_send_frame(elm, frame);
        spin_unlock(&elm->lock);
 
@@ -895,20 +906,30 @@ static const struct net_device_ops can327_netdev_ops = {
        .ndo_change_mtu = can_change_mtu,
 };
 
-static bool can327_is_valid_rx_char(char c)
+static bool can327_is_valid_rx_char(u8 c)
 {
-       return (isdigit(c) ||
-               isupper(c) ||
-               c == ELM327_DUMMY_CHAR ||
-               c == ELM327_READY_CHAR ||
-               c == '<' ||
-               c == 'a' ||
-               c == 'b' ||
-               c == 'v' ||
-               c == '.' ||
-               c == '?' ||
-               c == '\r' ||
-               c == ' ');
+       static const bool lut_char_is_valid['z'] = {
+               ['\r'] = true,
+               [' '] = true,
+               ['.'] = true,
+               ['0'] = true, true, true, true, true,
+               ['5'] = true, true, true, true, true,
+               ['<'] = true,
+               [ELM327_READY_CHAR] = true,
+               ['?'] = true,
+               ['A'] = true, true, true, true, true, true, true,
+               ['H'] = true, true, true, true, true, true, true,
+               ['O'] = true, true, true, true, true, true, true,
+               ['V'] = true, true, true, true, true,
+               ['a'] = true,
+               ['b'] = true,
+               ['v'] = true,
+               [ELM327_DUMMY_CHAR] = true,
+       };
+       BUILD_BUG_ON(ELM327_DUMMY_CHAR >= 'z');
+
+       return (c < ARRAY_SIZE(lut_char_is_valid) &&
+               lut_char_is_valid[c]);
 }
 
 /* Handle incoming ELM327 ASCII data.
@@ -924,11 +945,17 @@ static void can327_ldisc_rx(struct tty_struct *tty,
 #endif
 {
        struct can327 *elm = (struct can327 *)tty->disc_data;
+       size_t first_new_char_idx;
+
+       if (elm->uart_side_failure)
+               return;
 
        spin_lock_bh(&elm->lock);
 
-       if (elm->uart_side_failure)
-               goto out;
+       /* Store old rxfill, so elm327_parse_rxbuf() will have
+        * the option of skipping already checked characters.
+        */
+       first_new_char_idx = elm->rxfill;
 
        while (count-- && elm->rxfill < ELM327_SIZE_RXBUF) {
                if (fp && *fp++) {
@@ -936,7 +963,8 @@ static void can327_ldisc_rx(struct tty_struct *tty,
 
                        elm327_uart_side_failure(elm);
 
-                       goto out;
+                       spin_unlock_bh(&elm->lock);
+                       return;
                }
 
                /* Ignore NUL characters, which the PIC microcontroller may
@@ -954,7 +982,8 @@ static void can327_ldisc_rx(struct tty_struct *tty,
                                           *cp);
                                elm327_uart_side_failure(elm);
 
-                               goto out;
+                               spin_unlock_bh(&elm->lock);
+                               return;
                        }
 
                        elm->rxbuf[elm->rxfill++] = *cp;
@@ -964,16 +993,15 @@ static void can327_ldisc_rx(struct tty_struct *tty,
        }
 
        if (count >= 0) {
-               netdev_err(elm->dev, "Receive buffer overflowed. Bad chip or wiring?");
+               netdev_err(elm->dev, "Receive buffer overflowed. Bad chip or wiring? count = %i", count);
 
                elm327_uart_side_failure(elm);
 
-               goto out;
+               spin_unlock_bh(&elm->lock);
+               return;
        }
 
-       elm327_parse_rxbuf(elm);
-
-out:
+       elm327_parse_rxbuf(elm, first_new_char_idx);
        spin_unlock_bh(&elm->lock);
 }
 
@@ -997,6 +1025,7 @@ static void can327_ldisc_tx_worker(struct work_struct *work)
                                   "Failed to write to tty %s.\n",
                                   elm->tty->name);
                        elm327_uart_side_failure(elm);
+
                        spin_unlock_bh(&elm->lock);
                        return;
                }
@@ -1005,12 +1034,10 @@ static void can327_ldisc_tx_worker(struct work_struct *work)
                elm->txhead += written;
        }
 
-       if (!elm->txleft)  {
+       if (!elm->txleft)
                clear_bit(TTY_DO_WRITE_WAKEUP, &elm->tty->flags);
-               spin_unlock_bh(&elm->lock);
-       } else {
-               spin_unlock_bh(&elm->lock);
-       }
+
+       spin_unlock_bh(&elm->lock);
 }
 
 /* Called by the driver when there's room for more data. */
@@ -1025,7 +1052,7 @@ static void can327_ldisc_tx_wakeup(struct tty_struct *tty)
  * or 7/8 of that. Divisors are 1 to 64.
  * Currently we don't implement support for 7/8 rates.
  */
-static const u32 can327_bitrate_const[64] = {
+static const u32 can327_bitrate_const[] = {
         7812,  7936,  8064,  8196,  8333,  8474,  8620,  8771,
         8928,  9090,  9259,  9433,  9615,  9803, 10000, 10204,
        10416, 10638, 10869, 11111, 11363, 11627, 11904, 12195,
@@ -1082,16 +1109,14 @@ static int can327_ldisc_open(struct tty_struct *tty)
 
        /* Let 'er rip */
        err = register_candev(elm->dev);
-       if (err)
-               goto out_err;
+       if (err) {
+               free_candev(elm->dev);
+               return err;
+       }
 
        netdev_info(elm->dev, "can327 on %s.\n", tty->name);
 
        return 0;
-
-out_err:
-       free_candev(elm->dev);
-       return err;
 }
 
 /* Close down a can327 channel.