Style: Fix big comments
[elmcan.git] / module / elmcan.c
index b85c1c21b73d9f0acfdaf26eceeef393bd3849ab..ba4ef0d710ca00bf2d59b0b666837b8d3446e69f 100644 (file)
@@ -156,11 +156,11 @@ static inline void elm327_hw_failure(struct elmcan *elm);
 
 
 
- /************************************************************************
-  *            ELM327: Transmission                            *
-  *                                                            *
-  * (all functions assume elm->lock taken)                     *
-  ************************************************************************/
+ /***********************************************************************
+  *            ELM327: Transmission                                    *
+  *                                                                    *
+  * (all functions assume elm->lock taken)                             *
+  ***********************************************************************/
 
 static void elm327_send(struct elmcan *elm, const void *buf, size_t len)
 {
@@ -194,7 +194,11 @@ static void elm327_send(struct elmcan *elm, const void *buf, size_t len)
 }
 
 
-/* Take the ELM327 out of almost any state and back into command mode. */
+/* Take the ELM327 out of almost any state and back into command mode.
+ * We send ELM327_MAGIC_CHAR which will either abort any running
+ * operation, or be echoed back to us in case we're already in command
+ * mode.
+ */
 static void elm327_kick_into_cmd_mode(struct elmcan *elm)
 {
        if (elm->state != ELM_GETMAGICCHAR && elm->state != ELM_GETPROMPT) {
@@ -243,11 +247,11 @@ static void elm327_send_frame(struct elmcan *elm, struct can_frame *frame)
 
 
 
- /************************************************************************
-  *            ELM327: Initialization sequence                 *
-  *                                                            *
-  * (assumes elm->lock taken)                                  *
-  ************************************************************************/
+ /***********************************************************************
+  *            ELM327: Initialization sequence                         *
+  *                                                                    *
+  * (assumes elm->lock taken)                                          *
+  ***********************************************************************/
 
 static char *elm327_init_script[] = {
        "AT WS\r",        /* v1.0: Warm Start */
@@ -301,11 +305,11 @@ static void elm327_init(struct elmcan *elm)
 
 
 
- /************************************************************************
-  *            ELM327: Reception -> netdev glue                *
-  *                                                            *
-  * (assumes elm->lock taken)                                  *
-  ************************************************************************/
+ /***********************************************************************
+  *            ELM327: Reception -> netdev glue                        *
+  *                                                                    *
+  * (assumes elm->lock taken)                                          *
+  ***********************************************************************/
 
 static void elm327_feed_frame_to_netdev(struct elmcan *elm,
                                        const struct can_frame *frame)
@@ -332,11 +336,11 @@ static void elm327_feed_frame_to_netdev(struct elmcan *elm,
 
 
 
- /************************************************************************
-  *            ELM327: "Panic" handler                         *
-  *                                                            *
-  * (assumes elm->lock taken)                                  *
-  ************************************************************************/
+ /***********************************************************************
+  *            ELM327: "Panic" handler                                 *
+  *                                                                    *
+  * (assumes elm->lock taken)                                          *
+  ***********************************************************************/
 
 /* Called when we're out of ideas and just want it all to end. */
 static inline void elm327_hw_failure(struct elmcan *elm)
@@ -694,7 +698,7 @@ static void elm327_parse_rxbuf(struct elmcan *elm)
        switch (elm->state) {
        case ELM_NOTINIT:
                elm->rxfill = 0;
-               return;
+               break;
 
        case ELM_GETMAGICCHAR:
        {
@@ -716,7 +720,7 @@ static void elm327_parse_rxbuf(struct elmcan *elm)
 
                elm327_drop_bytes(elm, i);
 
-               return;
+               break;
        }
 
        case ELM_GETPROMPT:
@@ -725,7 +729,7 @@ static void elm327_parse_rxbuf(struct elmcan *elm)
                        elm327_handle_prompt(elm);
 
                elm->rxfill = 0;
-               return;
+               break;
 
        case ELM_RECEIVING:
                /* Find <CR> delimiting feedback lines. */
@@ -742,7 +746,7 @@ static void elm327_parse_rxbuf(struct elmcan *elm)
                        netdev_err(elm->dev,
                                "RX buffer overflow. Faulty ELM327 or UART?\n");
                        elm327_hw_failure(elm);
-                       return;
+                       break;
                } else if (len == elm->rxfill) {
                        if (elm327_is_ready_char(elm->rxbuf[elm->rxfill - 1])) {
                                /* The ELM327's AT ST response timeout ran out,
@@ -752,13 +756,13 @@ static void elm327_parse_rxbuf(struct elmcan *elm)
                                elm->rxfill = 0;
 
                                elm327_handle_prompt(elm);
-                               return;
+                               break;
                        }
 
                        /* No <CR> found - we haven't received a full line yet.
                         * Wait for more data.
                         */
-                       return;
+                       break;
                }
 
                /* We have a full line to parse. */
@@ -777,11 +781,11 @@ static void elm327_parse_rxbuf(struct elmcan *elm)
 
 
 
- /************************************************************************
-  *            netdev                                          *
-  *                                                            *
-  * (takes elm->lock)                                          *
-  ************************************************************************/
+ /***********************************************************************
+  *            netdev                                                  *
+  *                                                                    *
+  * (takes elm->lock)                                                  *
+  ***********************************************************************/
 
 static int elmcan_netdev_open(struct net_device *dev)
 {
@@ -893,27 +897,23 @@ out:
        return NETDEV_TX_OK;
 }
 
-static int elmcan_netdev_change_mtu(struct net_device *dev, int new_mtu)
-{
-       return -EINVAL;
-}
 
 static const struct net_device_ops elmcan_netdev_ops = {
        .ndo_open       = elmcan_netdev_open,
        .ndo_stop       = elmcan_netdev_close,
        .ndo_start_xmit = elmcan_netdev_start_xmit,
-       .ndo_change_mtu = elmcan_netdev_change_mtu,
+       .ndo_change_mtu = can_change_mtu,
 };
 
 
 
 
 
- /************************************************************************
-  *            Line discipline                                 *
-  *                                                            *
-  * (takes elm->lock)                                          *
-  ************************************************************************/
+ /***********************************************************************
+  *            Line discipline                                         *
+  *                                                                    *
+  * (takes elm->lock)                                                  *
+  ***********************************************************************/
 
 /* Get a reference to our struct, taking into account locks/refcounts.
  * This is to ensure ordering in case we are shutting down, and to ensure
@@ -949,6 +949,23 @@ static void put_elm(struct elmcan *elm)
 }
 
 
+static bool elmcan_is_valid_rx_char(char c)
+{
+       return (accept_flaky_uart
+               || isdigit(c)
+               || isupper(c)
+               || ELM327_MAGIC_CHAR == c
+               || ELM327_READY_CHAR == c
+               || '<' == c
+               || 'a' == c
+               || 'b' == c
+               || 'v' == c
+               || '.' == c
+               || '?' == c
+               || '\r' == c
+               || ' ' == c);
+}
+
 /* Handle incoming ELM327 ASCII data.
  * This will not be re-entered while running, but other ldisc
  * functions may be called in parallel.
@@ -962,11 +979,9 @@ static void elmcan_ldisc_rx(struct tty_struct *tty,
                return;
 
        spin_lock_bh(&elm->lock);
-       if (elm->hw_failure) {
-               spin_unlock_bh(&elm->lock);
 
-               put_elm(elm);
-               return;
+       if (elm->hw_failure) {
+               goto out;
        }
 
        while (count-- && elm->rxfill < sizeof(elm->rxbuf)) {
@@ -974,10 +989,8 @@ static void elmcan_ldisc_rx(struct tty_struct *tty,
                        netdev_err(elm->dev, "Error in received character stream. Check your wiring.");
 
                        elm327_hw_failure(elm);
-                       spin_unlock_bh(&elm->lock);
 
-                       put_elm(elm);
-                       return;
+                       goto out;
                }
 
                /* Ignore NUL characters, which the PIC microcontroller may
@@ -989,27 +1002,13 @@ static void elmcan_ldisc_rx(struct tty_struct *tty,
                        /* Check for stray characters on the UART line.
                         * Likely caused by bad hardware.
                         */
-                       if (!accept_flaky_uart
-                           && !isdigit(*cp)
-                           && !isupper(*cp)
-                           && ELM327_MAGIC_CHAR != *cp
-                           && ELM327_READY_CHAR != *cp
-                           && '<' != *cp
-                           && 'a' != *cp
-                           && 'b' != *cp
-                           && 'v' != *cp
-                           && '.' != *cp
-                           && '?' != *cp
-                           && '\r' != *cp
-                           && ' ' != *cp) {
+                       if (!elmcan_is_valid_rx_char(*cp)) {
                                netdev_err(elm->dev,
                                           "Received illegal character %02x.\n",
                                           *cp);
                                elm327_hw_failure(elm);
-                               spin_unlock_bh(&elm->lock);
 
-                               put_elm(elm);
-                               return;
+                               goto out;
                        }
 
                        elm->rxbuf[elm->rxfill++] = *cp;
@@ -1022,15 +1021,14 @@ static void elmcan_ldisc_rx(struct tty_struct *tty,
                netdev_err(elm->dev, "Receive buffer overflowed. Bad chip or wiring?");
 
                elm327_hw_failure(elm);
-               spin_unlock_bh(&elm->lock);
 
-               put_elm(elm);
-               return;
+               goto out;
        }
 
        elm327_parse_rxbuf(elm);
-       spin_unlock_bh(&elm->lock);
 
+out:
+       spin_unlock_bh(&elm->lock);
        put_elm(elm);
 }
 
@@ -1115,6 +1113,8 @@ 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;
 }
 
@@ -1265,12 +1265,6 @@ static struct tty_ldisc_ops elmcan_ldisc = {
 
 
 
-
-
- /************************************************************************
-  *            Module init/exit                                *
-  ************************************************************************/
-
 static int __init elmcan_init(void)
 {
        int status;