Re-align buffers, make RX buffer a power of two
[elmcan.git] / module / can327.c
index aba198651476e1fe79f30ecdf4398e773e0d2c8d..799905fd2d29fc4fa48fa3e96c03f8e08bd11b25 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>
@@ -39,7 +35,6 @@
 #include <linux/can.h>
 #include <linux/can/dev.h>
 #include <linux/can/error.h>
-#include <linux/can/led.h>
 #include <linux/can/rx-offload.h>
 
 /* Line discipline ID number.
@@ -57,8 +52,8 @@
 
 #define ELM327_NAPI_WEIGHT 4
 
-#define ELM327_SIZE_RXBUF 224
 #define ELM327_SIZE_TXBUF 32
+#define ELM327_SIZE_RXBUF 1024
 
 #define ELM327_CAN_CONFIG_SEND_SFF           0x8000
 #define ELM327_CAN_CONFIG_VARIABLE_DLC       0x4000
@@ -89,8 +84,8 @@ struct can327 {
        struct can_rx_offload offload;
 
        /* TTY buffers */
-       u8 rxbuf[ELM327_SIZE_RXBUF];
        u8 txbuf[ELM327_SIZE_TXBUF];
+       u8 rxbuf[ELM327_SIZE_RXBUF];
 
        /* Per-channel lock */
        spinlock_t lock;
@@ -260,7 +255,7 @@ static void elm327_init(struct can327 *elm)
        elm->drop_next_line = 0;
 
        /* We can only set the bitrate as a fraction of 500000.
-        * The bit timing constants in can327_bittiming_const will
+        * The bitrates listed in can327_bitrate_const will
         * limit the user to the right values.
         */
        elm->can_bitrate_divisor = 500000 / elm->can.bittiming.bitrate;
@@ -444,6 +439,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 +458,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 +466,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 +518,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 +681,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 +694,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 +720,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 +754,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 +780,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)
 {
@@ -815,7 +824,6 @@ static int can327_netdev_open(struct net_device *dev)
 
        can_rx_offload_enable(&elm->offload);
 
-       can_led_event(dev, CAN_LED_EVENT_OPEN);
        elm->can.state = CAN_STATE_ERROR_ACTIVE;
        netif_start_queue(dev);
 
@@ -841,7 +849,6 @@ static int can327_netdev_close(struct net_device *dev)
        elm->can.state = CAN_STATE_STOPPED;
        can_rx_offload_del(&elm->offload);
        close_candev(dev);
-       can_led_event(dev, CAN_LED_EVENT_STOP);
 
        return 0;
 }
@@ -882,8 +889,6 @@ static netdev_tx_t can327_netdev_start_xmit(struct sk_buff *skb,
        dev->stats.tx_packets++;
        dev->stats.tx_bytes += frame->len;
 
-       can_led_event(dev, CAN_LED_EVENT_TX);
-
 out:
        kfree_skb(skb);
        return NETDEV_TX_OK;
@@ -935,12 +940,18 @@ 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);
 
+       /* 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++) {
                        netdev_err(elm->dev, "Error in received character stream. Check your wiring.");
@@ -977,7 +988,7 @@ 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);
 
@@ -985,7 +996,7 @@ static void can327_ldisc_rx(struct tty_struct *tty,
                return;
        }
 
-       elm327_parse_rxbuf(elm);
+       elm327_parse_rxbuf(elm, first_new_char_idx);
        spin_unlock_bh(&elm->lock);
 }
 
@@ -1036,7 +1047,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,
@@ -1089,8 +1100,6 @@ static int can327_ldisc_open(struct tty_struct *tty)
        elm->tty = tty;
        tty->disc_data = elm;
 
-       devm_can_led_init(elm->dev);
-
        /* Let 'er rip */
        err = register_candev(elm->dev);
        if (err) {