Move MODULE_* to end of file
[elmcan.git] / module / elmcan.c
index 69ca8df78ae3fddf54cb396706898f36e273f52e..31aa8fcdf6887674892c5c6a3a03464d32b15350 100644 (file)
@@ -4,9 +4,14 @@
  * This driver started as a derivative of linux/drivers/net/can/slcan.c
  * and my thanks go to the original authors for their inspiration, even
  * after almost none of their code is left.
+ *
+ * elmcan.c Author : Max Staudt <max-linux@enpas.org>
+ * slcan.c Author  : Oliver Hartkopp <socketcan@hartkopp.net>
+ * slip.c Authors  : Laurence Culhane <loz@holmes.demon.co.uk>
+ *                   Fred N. van Kempen <waltje@uwalt.nl.mugnet.org>
  */
 
-#define pr_fmt(fmt) "[elmcan] " fmt
+#define pr_fmt(fmt) "elmcan: " fmt
 
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/can/led.h>
 #include <linux/can/rx-offload.h>
 
-MODULE_ALIAS_LDISC(N_DEVELOPMENT);
-MODULE_DESCRIPTION("ELM327 based CAN interface");
-MODULE_LICENSE("GPL");
-MODULE_AUTHOR("Max Staudt <max-linux@enpas.org>");
-
 /* 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
@@ -50,6 +50,11 @@ MODULE_AUTHOR("Max Staudt <max-linux@enpas.org>");
 #define N_DEVELOPMENT 29
 #endif
 
+/* Compatibility for Linux < 5.11 */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5,11,0)
+#define len can_dlc
+#endif
+
 #define ELM327_NAPI_WEIGHT 4
 
 #define ELM327_SIZE_RXBUF 256
@@ -289,21 +294,11 @@ static void elm327_init(struct elmcan *elm)
 
 /* Assumes elm->lock taken. */
 static void elm327_feed_frame_to_netdev(struct elmcan *elm,
-                                       const struct can_frame *frame)
+                                       struct sk_buff *skb)
 {
-       struct can_frame *cf;
-       struct sk_buff *skb;
-
        if (!netif_running(elm->dev))
                return;
 
-       skb = alloc_can_skb(elm->dev, &cf);
-
-       if (!skb)
-               return;
-
-       memcpy(cf, frame, sizeof(struct can_frame));
-
        /* Queue for NAPI pickup.
         * rx-offload will update stats and LEDs for us.
         */
@@ -321,20 +316,24 @@ static void elm327_feed_frame_to_netdev(struct elmcan *elm,
  */
 static inline void elm327_hw_failure(struct elmcan *elm)
 {
-       struct can_frame frame;
+       struct can_frame *frame;
+       struct sk_buff *skb;
 
-       memset(&frame, 0, sizeof(frame));
-       frame.can_id = CAN_ERR_FLAG;
-       frame.can_dlc = CAN_ERR_DLC;
-       frame.data[5] = 'R';
-       frame.data[6] = 'I';
-       frame.data[7] = 'P';
-       elm327_feed_frame_to_netdev(elm, &frame);
+       elm->hw_failure = true;
+
+       elm->can.can_stats.bus_off++;
+       netif_stop_queue(elm->dev);
+       elm->can.state = CAN_STATE_BUS_OFF;
+       can_bus_off(elm->dev);
 
        netdev_err(elm->dev, "ELM327 misbehaved. Blocking further communication.\n");
 
-       elm->hw_failure = true;
-       can_bus_off(elm->dev);
+       skb = alloc_can_err_skb(elm->dev, &frame);
+       if (!skb)
+               return;
+
+       frame->can_id |= CAN_ERR_BUSOFF;
+       elm327_feed_frame_to_netdev(elm, skb);
 }
 
 /* Compare a buffer to a fixed string */
@@ -359,11 +358,15 @@ static inline int _len_memstrcmp(const u8 *mem, size_t mem_len, const char *str)
 /* Assumes elm->lock taken. */
 static void elm327_parse_error(struct elmcan *elm, size_t len)
 {
-       struct can_frame frame;
+       struct can_frame *frame;
+       struct sk_buff *skb;
 
-       memset(&frame, 0, sizeof(frame));
-       frame.can_id = CAN_ERR_FLAG;
-       frame.can_dlc = CAN_ERR_DLC;
+       skb = alloc_can_err_skb(elm->dev, &frame);
+       if (!skb)
+               /* It's okay to return here:
+                * The outer parsing loop will drop this UART buffer.
+                */
+               return;
 
        /* Filter possible error messages based on length of RX'd line */
        if (!_len_memstrcmp(elm->rxbuf, len, "UNABLE TO CONNECT")) {
@@ -374,25 +377,25 @@ static void elm327_parse_error(struct elmcan *elm, size_t len)
                 * Otherwise, elm327_parse_frame() will heuristically
                 * emit this kind of error frame instead.
                 */
-               frame.can_id |= CAN_ERR_CRTL;
-               frame.data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
+               frame->can_id |= CAN_ERR_CRTL;
+               frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
        } else if (!_len_memstrcmp(elm->rxbuf, len, "BUS ERROR")) {
-               frame.can_id |= CAN_ERR_BUSERROR;
+               frame->can_id |= CAN_ERR_BUSERROR;
        } else if (!_len_memstrcmp(elm->rxbuf, len, "CAN ERROR")) {
-               frame.can_id |= CAN_ERR_PROT;
+               frame->can_id |= CAN_ERR_PROT;
        } else if (!_len_memstrcmp(elm->rxbuf, len, "<RX ERROR")) {
-               frame.can_id |= CAN_ERR_PROT;
+               frame->can_id |= CAN_ERR_PROT;
        } else if (!_len_memstrcmp(elm->rxbuf, len, "BUS BUSY")) {
-               frame.can_id |= CAN_ERR_PROT;
-               frame.data[2] = CAN_ERR_PROT_OVERLOAD;
+               frame->can_id |= CAN_ERR_PROT;
+               frame->data[2] = CAN_ERR_PROT_OVERLOAD;
        } else if (!_len_memstrcmp(elm->rxbuf, len, "FB ERROR")) {
-               frame.can_id |= CAN_ERR_PROT;
-               frame.data[2] = CAN_ERR_PROT_TX;
+               frame->can_id |= CAN_ERR_PROT;
+               frame->data[2] = CAN_ERR_PROT_TX;
        } else if (len == 5 && !_memstrcmp(elm->rxbuf, "ERR")) {
                /* ERR is followed by two digits, hence line length 5 */
                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;
+               frame->can_id |= CAN_ERR_CRTL;
        } else {
                /* Something else has happened.
                 * Maybe garbage on the UART line.
@@ -400,7 +403,7 @@ static void elm327_parse_error(struct elmcan *elm, size_t len)
                 */
        }
 
-       elm327_feed_frame_to_netdev(elm, &frame);
+       elm327_feed_frame_to_netdev(elm, skb);
 }
 
 /* Parse CAN frames coming as ASCII from ELM327.
@@ -419,12 +422,15 @@ static void elm327_parse_error(struct elmcan *elm, size_t len)
  */
 static int elm327_parse_frame(struct elmcan *elm, size_t len)
 {
-       struct can_frame frame;
+       struct can_frame *frame;
+       struct sk_buff *skb;
        int hexlen;
        int datastart;
        int i;
 
-       memset(&frame, 0, sizeof(frame));
+       skb = alloc_can_skb(elm->dev, &frame);
+       if (!skb)
+               return -ENOMEM;
 
        /* Find first non-hex and non-space character:
         *  - In the simplest case, there is none.
@@ -459,10 +465,10 @@ static int elm327_parse_frame(struct elmcan *elm, size_t len)
        if (elm->rxbuf[2] == ' ' && elm->rxbuf[5] == ' ' &&
            elm->rxbuf[8] == ' ' && elm->rxbuf[11] == ' ' &&
            elm->rxbuf[13] == ' ') {
-               frame.can_id = CAN_EFF_FLAG;
+               frame->can_id = CAN_EFF_FLAG;
                datastart = 14;
        } else if (elm->rxbuf[3] == ' ' && elm->rxbuf[5] == ' ') {
-               frame.can_id = 0;
+               frame->can_id = 0;
                datastart = 6;
        } else {
                /* This is not a well-formatted data line.
@@ -483,44 +489,43 @@ static int elm327_parse_frame(struct elmcan *elm, size_t len)
         */
 
        /* Read CAN data length */
-       frame.can_dlc = (hex_to_bin(elm->rxbuf[datastart - 2]) << 0);
+       frame->len = (hex_to_bin(elm->rxbuf[datastart - 2]) << 0);
 
        /* Read CAN ID */
-       if (frame.can_id & CAN_EFF_FLAG) {
-               frame.can_id |= (hex_to_bin(elm->rxbuf[0]) << 28)
-                             | (hex_to_bin(elm->rxbuf[1]) << 24)
-                             | (hex_to_bin(elm->rxbuf[3]) << 20)
-                             | (hex_to_bin(elm->rxbuf[4]) << 16)
-                             | (hex_to_bin(elm->rxbuf[6]) << 12)
-                             | (hex_to_bin(elm->rxbuf[7]) << 8)
-                             | (hex_to_bin(elm->rxbuf[9]) << 4)
-                             | (hex_to_bin(elm->rxbuf[10]) << 0);
+       if (frame->can_id & CAN_EFF_FLAG) {
+               frame->can_id |= (hex_to_bin(elm->rxbuf[0]) << 28)
+                              | (hex_to_bin(elm->rxbuf[1]) << 24)
+                              | (hex_to_bin(elm->rxbuf[3]) << 20)
+                              | (hex_to_bin(elm->rxbuf[4]) << 16)
+                              | (hex_to_bin(elm->rxbuf[6]) << 12)
+                              | (hex_to_bin(elm->rxbuf[7]) << 8)
+                              | (hex_to_bin(elm->rxbuf[9]) << 4)
+                              | (hex_to_bin(elm->rxbuf[10]) << 0);
        } else {
-               frame.can_id |= (hex_to_bin(elm->rxbuf[0]) << 8)
-                             | (hex_to_bin(elm->rxbuf[1]) << 4)
-                             | (hex_to_bin(elm->rxbuf[2]) << 0);
+               frame->can_id |= (hex_to_bin(elm->rxbuf[0]) << 8)
+                              | (hex_to_bin(elm->rxbuf[1]) << 4)
+                              | (hex_to_bin(elm->rxbuf[2]) << 0);
        }
 
        /* Check for RTR frame */
        if (elm->rxfill >= hexlen + 3 &&
            !_memstrcmp(&elm->rxbuf[hexlen], "RTR")) {
-               frame.can_id |= CAN_RTR_FLAG;
+               frame->can_id |= CAN_RTR_FLAG;
        }
 
        /* Is the line long enough to hold the advertised payload?
         * Note: RTR frames have a DLC, but no actual payload.
         */
-       if (!(frame.can_id & CAN_RTR_FLAG) &&
-           (hexlen < frame.can_dlc * 3 + datastart)) {
-               /* Incomplete frame. */
-
-               /* Probably the ELM327's RS232 TX buffer was full.
+       if (!(frame->can_id & CAN_RTR_FLAG) &&
+           (hexlen < frame->len * 3 + datastart)) {
+               /* Incomplete frame.
+                * Probably the ELM327's RS232 TX buffer was full.
                 * Emit an error frame and exit.
                 */
-               frame.can_id = CAN_ERR_FLAG | CAN_ERR_CRTL;
-               frame.can_dlc = CAN_ERR_DLC;
-               frame.data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
-               elm327_feed_frame_to_netdev(elm, &frame);
+               frame->can_id = CAN_ERR_FLAG | CAN_ERR_CRTL;
+               frame->len = CAN_ERR_DLC;
+               frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
+               elm327_feed_frame_to_netdev(elm, skb);
 
                /* Signal failure to parse.
                 * The line will be re-parsed as an error line, which will fail.
@@ -531,13 +536,13 @@ static int elm327_parse_frame(struct elmcan *elm, size_t len)
        }
 
        /* Parse the data nibbles. */
-       for (i = 0; i < frame.can_dlc; i++) {
-               frame.data[i] = (hex_to_bin(elm->rxbuf[datastart + 3*i]) << 4)
-                             | (hex_to_bin(elm->rxbuf[datastart + 3*i + 1]));
+       for (i = 0; i < frame->len; i++) {
+               frame->data[i] = (hex_to_bin(elm->rxbuf[datastart + 3*i]) << 4)
+                              | (hex_to_bin(elm->rxbuf[datastart + 3*i + 1]));
        }
 
        /* Feed the frame to the network layer. */
-       elm327_feed_frame_to_netdev(elm, &frame);
+       elm327_feed_frame_to_netdev(elm, skb);
 
        return 0;
 }
@@ -558,18 +563,13 @@ static void elm327_parse_line(struct elmcan *elm, size_t len)
        }
 
        /* Regular parsing */
-       switch (elm->state) {
-       case ELM_RECEIVING:
-               if (elm327_parse_frame(elm, len)) {
-                       /* Parse an error line. */
-                       elm327_parse_error(elm, len);
+       if (elm->state == ELM_RECEIVING
+           && elm327_parse_frame(elm, len)) {
+               /* Parse an error line. */
+               elm327_parse_error(elm, len);
 
-                       /* Start afresh. */
-                       elm327_kick_into_cmd_mode(elm);
-               }
-               break;
-       default:
-               break;
+               /* Start afresh. */
+               elm327_kick_into_cmd_mode(elm);
        }
 }
 
@@ -635,7 +635,7 @@ static void elm327_handle_prompt(struct elmcan *elm)
                        /* Send a regular CAN data frame */
                        int i;
 
-                       for (i = 0; i < frame->can_dlc; i++) {
+                       for (i = 0; i < frame->len; i++) {
                                sprintf(&local_txbuf[2 * i], "%02X",
                                        frame->data[i]);
                        }
@@ -669,6 +669,7 @@ static void elm327_drop_bytes(struct elmcan *elm, size_t i)
 static void elm327_parse_rxbuf(struct elmcan *elm)
 {
        size_t len;
+       int i;
 
        switch (elm->state) {
        case ELM_NOTINIT:
@@ -678,8 +679,6 @@ static void elm327_parse_rxbuf(struct elmcan *elm)
        case ELM_GETDUMMYCHAR:
        {
                /* Wait for 'y' or '>' */
-               int i;
-
                for (i = 0; i < elm->rxfill; i++) {
                        if (elm->rxbuf[i] == ELM327_DUMMY_CHAR) {
                                elm327_send(elm, "\r", 1);
@@ -844,14 +843,6 @@ static netdev_tx_t elmcan_netdev_start_xmit(struct sk_buff *skb,
        struct elmcan *elm = netdev_priv(dev);
        struct can_frame *frame = (struct can_frame *)skb->data;
 
-       if (skb->len != sizeof(struct can_frame))
-               goto out;
-
-       if (!netif_running(dev))  {
-               netdev_warn(elm->dev, "xmit: iface is down.\n");
-               goto out;
-       }
-
        /* BHs are already disabled, so no spin_lock_bh().
         * See Documentation/networking/netdevices.txt
         */
@@ -875,7 +866,7 @@ static netdev_tx_t elmcan_netdev_start_xmit(struct sk_buff *skb,
        spin_unlock(&elm->lock);
 
        dev->stats.tx_packets++;
-       dev->stats.tx_bytes += frame->can_dlc;
+       dev->stats.tx_bytes += frame->len;
 
        can_led_event(dev, CAN_LED_EVENT_TX);
 
@@ -1291,3 +1282,8 @@ static void __exit elmcan_exit(void)
 
 module_init(elmcan_init);
 module_exit(elmcan_exit);
+
+MODULE_ALIAS_LDISC(N_DEVELOPMENT);
+MODULE_DESCRIPTION("ELM327 based CAN interface");
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Max Staudt <max-linux@enpas.org>");