]> rtime.felk.cvut.cz Git - socketcan-devel.git/blobdiff - kernel/2.6/drivers/net/can/mscan/mscan.c
can: provide library functions for skb allocation
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / mscan / mscan.c
index 12a77268ea407c7490f06ee1ee00dac0a34ca8e6..e5d5004d74185886d6f1a53800b0c733a27d535f 100644 (file)
@@ -1,30 +1,22 @@
 /*
- * mscan.c
+ * CAN bus driver for the alone generic (as possible as) MSCAN controller.
  *
- * DESCRIPTION:
- *  CAN bus driver for the alone generic (as possible as) MSCAN controller.
+ * Copyright (C) 2005-2006 Andrey Volkov <avolkov@varma-el.com>,
+ *                         Varma Electronics Oy
+ * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
  *
- * AUTHOR:
- *  Andrey Volkov <avolkov@varma-el.com>
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the version 2 of the GNU General Public License
+ * as published by the Free Software Foundation
  *
- * COPYRIGHT:
- *  2005-2006, Varma Electronics Oy
- *
- * LICENCE:
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
  *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #include <linux/kernel.h>
@@ -35,9 +27,9 @@
 #include <linux/if_arp.h>
 #include <linux/if_ether.h>
 #include <linux/list.h>
-#include <linux/can.h>
-#include <linux/can/dev.h>
-#include <linux/can/error.h>
+#include <socketcan/can.h>
+#include <socketcan/can/dev.h>
+#include <socketcan/can/error.h>
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
 #include <linux/io.h>
 #else
@@ -46,7 +38,7 @@
 
 #include "mscan.h"
 
-#include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
+#include <socketcan/can/version.h>     /* for RCSID. Removed by mkpatch script */
 RCSID("$Id$");
 
 #define MSCAN_NORMAL_MODE      0
@@ -54,7 +46,7 @@ RCSID("$Id$");
 #define MSCAN_INIT_MODE                (MSCAN_INITRQ | MSCAN_SLPRQ)
 #define MSCAN_POWEROFF_MODE    (MSCAN_CSWAI | MSCAN_SLPRQ)
 #define MSCAN_SET_MODE_RETRIES 255
-
+#define MSCAN_ECHO_SKB_MAX     3
 
 #define BTR0_BRP_MASK          0x3f
 #define BTR0_SJW_SHIFT         6
@@ -72,9 +64,10 @@ RCSID("$Id$");
 #define BTR1_SET_TSEG1(tseg1)  (((tseg1) - 1) &  BTR1_TSEG1_MASK)
 #define BTR1_SET_TSEG2(tseg2)  ((((tseg2) - 1) << BTR1_TSEG2_SHIFT) & \
                                 BTR1_TSEG2_MASK)
-#define BTR1_SET_SAM(sam)      (((sam) & 1) << BTR1_SAM_SHIFT)
+#define BTR1_SET_SAM(sam)      ((sam) ? 1 << BTR1_SAM_SHIFT : 0)
 
 static struct can_bittiming_const mscan_bittiming_const = {
+       .name = "mscan",
        .tseg1_min = 4,
        .tseg1_max = 16,
        .tseg2_min = 2,
@@ -102,10 +95,11 @@ struct tx_queue_entry {
 struct mscan_priv {
        struct can_priv can;
        long open_time;
-       volatile unsigned long flags;
+       unsigned long flags;
        u8 shadow_statflg;
        u8 shadow_canrier;
        u8 cur_pri;
+       u8 prev_buf_id;
        u8 tx_active;
 
        struct list_head tx_head;
@@ -123,9 +117,9 @@ struct mscan_priv {
 #define F_TX_WAIT_ALL  2
 
 static enum can_state state_map[] = {
-       CAN_STATE_ACTIVE,
-       CAN_STATE_BUS_WARNING,
-       CAN_STATE_BUS_PASSIVE,
+       CAN_STATE_ERROR_ACTIVE,
+       CAN_STATE_ERROR_WARNING,
+       CAN_STATE_ERROR_PASSIVE,
        CAN_STATE_BUS_OFF
 };
 
@@ -140,10 +134,10 @@ static int mscan_set_mode(struct net_device *dev, u8 mode)
        if (mode != MSCAN_NORMAL_MODE) {
 
                if (priv->tx_active) {
-                       /* Abort transfers before going to sleep */
-                       out_8(&regs->cantier, 0);
+                       /* Abort transfers before going to sleep */#
                        out_8(&regs->cantarq, priv->tx_active);
-                       out_8(&regs->cantier, priv->tx_active);
+                       /* Suppress TX done interrupts */
+                       out_8(&regs->cantier, 0);
                }
 
                canctl1 = in_8(&regs->canctl1);
@@ -192,7 +186,7 @@ static int mscan_set_mode(struct net_device *dev, u8 mode)
                        if (i >= MSCAN_SET_MODE_RETRIES)
                                ret = -ENODEV;
                        else
-                               priv->can.state = CAN_STATE_ACTIVE;
+                               priv->can.state = CAN_STATE_ERROR_ACTIVE;
                }
        }
        return ret;
@@ -208,6 +202,7 @@ static int mscan_start(struct net_device *dev)
        out_8(&regs->canrier, 0);
 
        INIT_LIST_HEAD(&priv->tx_head);
+       priv->prev_buf_id = 0;
        priv->cur_pri = 0;
        priv->tx_active = 0;
        priv->shadow_canrier = 0;
@@ -230,7 +225,11 @@ static int mscan_start(struct net_device *dev)
        return 0;
 }
 
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
 static int mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
+#else
+static netdev_tx_t mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
+#endif
 {
        struct can_frame *frame = (struct can_frame *)skb->data;
        struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
@@ -241,7 +240,6 @@ static int mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
        if (frame->can_dlc > 8)
                return -EINVAL;
 
-       dev_dbg(ND2D(dev), "%s\n", __func__);
        out_8(&regs->cantier, 0);
 
        i = ~priv->tx_active & MSCAN_TXE;
@@ -254,21 +252,24 @@ static int mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
        case 1:
                /* if buf_id < 3, then current frame will be send out of order,
                   since  buffer with lower id have higher priority (hell..) */
-               if (buf_id < 3)
-                       priv->cur_pri++;
-               if (priv->cur_pri == 0xff)
-                       set_bit(F_TX_WAIT_ALL, &priv->flags);
                netif_stop_queue(dev);
        case 2:
+               if (buf_id < priv->prev_buf_id) {
+                       priv->cur_pri++;
+                       if (priv->cur_pri == 0xff) {
+                               set_bit(F_TX_WAIT_ALL, &priv->flags);
+                               netif_stop_queue(dev);
+                       }
+               }
                set_bit(F_TX_PROGRESS, &priv->flags);
+               break;
        }
+       priv->prev_buf_id = buf_id;
        out_8(&regs->cantbsel, i);
 
        rtr = frame->can_id & CAN_RTR_FLAG;
 
        if (frame->can_id & CAN_EFF_FLAG) {
-               dev_dbg(ND2D(dev), "sending extended frame\n");
-
                can_id = (frame->can_id & CAN_EFF_MASK) << 1;
                if (rtr)
                        can_id |= 1;
@@ -277,7 +278,6 @@ static int mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
                can_id >>= 16;
                can_id = (can_id & 0x7) | ((can_id << 2) & 0xffe0) | (3 << 3);
        } else {
-               dev_dbg(ND2D(dev), "sending standard frame\n");
                can_id = (frame->can_id & CAN_SFF_MASK) << 5;
                if (rtr)
                        can_id |= 1 << 4;
@@ -285,7 +285,7 @@ static int mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
        out_be16(&regs->tx.idr1_0, can_id);
 
        if (!rtr) {
-               volatile void __iomem *data = &regs->tx.dsr1_0;
+               void __iomem *data = &regs->tx.dsr1_0;
                u16 *payload = (u16 *) frame->data;
                /*Its safe to write into dsr[dlc+1] */
                for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
@@ -327,8 +327,6 @@ static inline int check_set_state(struct net_device *dev, u8 canrflg)
                              MSCAN_STATE_TX(canrflg))];
        if (priv->can.state < state)
                ret = 1;
-       if (state == CAN_STATE_BUS_OFF)
-               can_bus_off(dev);
        priv->can.state = state;
        return ret;
 }
@@ -366,7 +364,7 @@ static int mscan_rx_poll(struct net_device *dev, int *budget)
        while (npackets < quota && ((canrflg = in_8(&regs->canrflg)) &
                                    (MSCAN_RXF | MSCAN_ERR_IF))) {
 
-               skb = dev_alloc_skb(sizeof(struct can_frame));
+               skb = alloc_can_skb(dev, &frame);
                if (!skb) {
                        if (printk_ratelimit())
                                dev_notice(ND2D(dev), "packet dropped\n");
@@ -375,9 +373,6 @@ static int mscan_rx_poll(struct net_device *dev, int *budget)
                        continue;
                }
 
-               frame = (struct can_frame *)skb_put(skb, sizeof(*frame));
-               memset(frame, 0, sizeof(*frame));
-
                if (canrflg & MSCAN_RXF) {
                        can_id = in_be16(&regs->rx.idr1_0);
                        if (can_id & (1 << 3)) {
@@ -397,7 +392,7 @@ static int mscan_rx_poll(struct net_device *dev, int *budget)
                        frame->can_dlc = in_8(&regs->rx.dlr) & 0xf;
 
                        if (!(frame->can_id & CAN_RTR_FLAG)) {
-                               volatile void __iomem *data = &regs->rx.dsr1_0;
+                               void __iomem *data = &regs->rx.dsr1_0;
                                u16 *payload = (u16 *) frame->data;
                                for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
                                        *payload++ = in_be16(data);
@@ -405,33 +400,35 @@ static int mscan_rx_poll(struct net_device *dev, int *budget)
                                }
                        }
 
-                       dev_dbg(ND2D(dev),
-                               "received pkt: id: %u dlc: %u data: ",
-                               frame->can_id, frame->can_dlc);
-
                        out_8(&regs->canrflg, MSCAN_RXF);
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
                        dev->last_rx = jiffies;
+#endif
                        stats->rx_packets++;
                        stats->rx_bytes += frame->can_dlc;
                } else if (canrflg & MSCAN_ERR_IF) {
+                       dev_dbg(ND2D(dev), "error interrupt (canrflg=%#x)\n",
+                               canrflg);
                        frame->can_id = CAN_ERR_FLAG;
 
                        if (canrflg & MSCAN_OVRIF) {
                                frame->can_id |= CAN_ERR_CRTL;
                                frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
                                stats->rx_over_errors++;
+                               stats->rx_errors++;
                        } else
                                frame->data[1] = 0;
 
                        if (check_set_state(dev, canrflg)) {
-                               frame->can_id |= CAN_ERR_CRTL;
                                switch (priv->can.state) {
-                               case CAN_STATE_BUS_WARNING:
+                               case CAN_STATE_ERROR_WARNING:
+                                       frame->can_id |= CAN_ERR_CRTL;
+                                       priv->can.can_stats.error_warning++;
                                        if ((priv->shadow_statflg &
                                             MSCAN_RSTAT_MSK) <
                                            (canrflg & MSCAN_RSTAT_MSK))
                                                frame->data[1] |=
-                                                   CAN_ERR_CRTL_RX_WARNING;
+                                                       CAN_ERR_CRTL_RX_WARNING;
 
                                        if ((priv->shadow_statflg &
                                             MSCAN_TSTAT_MSK) <
@@ -439,13 +436,15 @@ static int mscan_rx_poll(struct net_device *dev, int *budget)
                                                frame->data[1] |=
                                                        CAN_ERR_CRTL_TX_WARNING;
                                        break;
-                               case CAN_STATE_BUS_PASSIVE:
+                               case CAN_STATE_ERROR_PASSIVE:
+                                       frame->can_id |= CAN_ERR_CRTL;
+                                       priv->can.can_stats.error_passive++;
                                        frame->data[1] |=
-                                           CAN_ERR_CRTL_RX_PASSIVE;
+                                               CAN_ERR_CRTL_RX_PASSIVE;
                                        break;
                                case CAN_STATE_BUS_OFF:
                                        frame->can_id |= CAN_ERR_BUSOFF;
-                                       frame->can_id &= ~CAN_ERR_CRTL;
+                                       can_bus_off(dev);
                                        break;
                                default:
                                        break;
@@ -457,9 +456,6 @@ static int mscan_rx_poll(struct net_device *dev, int *budget)
                }
 
                npackets++;
-               skb->dev = dev;
-               skb->protocol = __constant_htons(ETH_P_CAN);
-               skb->ip_summed = CHECKSUM_UNNECESSARY;
                netif_receive_skb(skb);
        }
 
@@ -484,7 +480,6 @@ static int mscan_rx_poll(struct net_device *dev, int *budget)
        return ret;
 }
 
-
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
 static irqreturn_t mscan_isr(int irq, void *dev_id, struct pt_regs *r)
 #else
@@ -606,7 +601,7 @@ static int mscan_do_set_bittiming(struct net_device *dev)
                BTR1_SET_TSEG2(bt->phase_seg2) |
                BTR1_SET_SAM(priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES));
 
-       dev_info(ND2D(dev), "BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
+       dev_info(ND2D(dev), "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
 
        out_8(&regs->canbtr0, btr0);
        out_8(&regs->canbtr1, btr1);
@@ -620,16 +615,16 @@ static int mscan_open(struct net_device *dev)
        struct mscan_priv *priv = netdev_priv(dev);
        struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
 
-       /* determine and set bittime */
-       ret = can_set_bittiming(dev);
+       /* common open */
+       ret = open_candev(dev);
        if (ret)
                return ret;
 
 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
        napi_enable(&priv->napi);
 #endif
-       ret = request_irq(dev->irq, mscan_isr, 0, dev->name, dev);
 
+       ret = request_irq(dev->irq, mscan_isr, 0, dev->name, dev);
        if (ret < 0) {
 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
                napi_disable(&priv->napi);
@@ -657,16 +652,16 @@ static int mscan_close(struct net_device *dev)
        struct mscan_regs *regs = (struct mscan_regs *)dev->base_addr;
        struct mscan_priv *priv = netdev_priv(dev);
 
+       netif_stop_queue(dev);
 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
        napi_disable(&priv->napi);
 #endif
 
        out_8(&regs->cantier, 0);
        out_8(&regs->canrier, 0);
-       free_irq(dev->irq, dev);
        mscan_set_mode(dev, MSCAN_INIT_MODE);
-       can_close_cleanup(dev);
-       netif_stop_queue(dev);
+       close_candev(dev);
+       free_irq(dev->irq, dev);
        priv->open_time = 0;
 
        return 0;
@@ -712,7 +707,7 @@ int register_mscandev(struct net_device *dev, int clock_src)
 
        return register_candev(dev);
 }
-EXPORT_SYMBOL(register_mscandev);
+EXPORT_SYMBOL_GPL(register_mscandev);
 
 void unregister_mscandev(struct net_device *dev)
 {
@@ -721,7 +716,7 @@ void unregister_mscandev(struct net_device *dev)
        out_8(&regs->canctl1, in_8(&regs->canctl1) & ~MSCAN_CANE);
        unregister_candev(dev);
 }
-EXPORT_SYMBOL(unregister_mscandev);
+EXPORT_SYMBOL_GPL(unregister_mscandev);
 
 struct net_device *alloc_mscandev(void)
 {
@@ -729,7 +724,7 @@ struct net_device *alloc_mscandev(void)
        struct mscan_priv *priv;
        int i;
 
-       dev = alloc_candev(sizeof(struct mscan_priv));
+       dev = alloc_candev(sizeof(struct mscan_priv), MSCAN_ECHO_SKB_MAX);
        if (!dev)
                return NULL;
        priv = netdev_priv(dev);
@@ -765,8 +760,8 @@ struct net_device *alloc_mscandev(void)
 
        return dev;
 }
-EXPORT_SYMBOL(alloc_mscandev);
+EXPORT_SYMBOL_GPL(alloc_mscandev);
 
 MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
 MODULE_LICENSE("GPL v2");
-MODULE_DESCRIPTION("CAN port driver for a mscan based chips");
+MODULE_DESCRIPTION("CAN port driver for a MSCAN based chips");