]> rtime.felk.cvut.cz Git - socketcan-devel.git/blobdiff - kernel/2.6/drivers/net/can/slcan.c
slcan: Fix whitespace issues for mainline.
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / slcan.c
index 3cbb9b80b1a16191c139f8755065bf36cd4c0fbc..cf7e59360b65ceaf5a472e35295c4d4bcef44a0c 100644 (file)
@@ -3,16 +3,9 @@
  *
  * This file is derived from linux/drivers/net/slip.c
  *
- * Therefore it has the same (strange?) behaviour not to unregister the
- * netdevice when detaching the tty. Is there any better solution?
- *
- * Do not try to attach, detach and re-attach a tty for this reason ...
- *
  * slip.c Authors  : Laurence Culhane <loz@holmes.demon.co.uk>
  *                   Fred N. van Kempen <waltje@uwalt.nl.mugnet.org>
- * slcan.c Author  : Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
- *
- * Copyright (c) 2007-2009 Volkswagen Group Electronic Research
+ * slcan.c Author  : Oliver Hartkopp <socketcan@hartkopp.net>
  *
  * 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
 #include <asm/uaccess.h>
 #endif
 #include <linux/bitops.h>
-#include <linux/sched.h>
 #include <linux/string.h>
-#include <linux/mm.h>
-#include <linux/interrupt.h>
-#include <linux/in.h>
 #include <linux/tty.h>
 #include <linux/errno.h>
 #include <linux/netdevice.h>
-#include <linux/etherdevice.h>
 #include <linux/skbuff.h>
 #include <linux/rtnetlink.h>
 #include <linux/if_arp.h>
 #include <linux/if_ether.h>
-#include <linux/if_slip.h>
 #include <linux/delay.h>
 #include <linux/init.h>
-
 #include <socketcan/can.h>
 
 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
@@ -85,18 +71,17 @@ static __initdata const char banner[] =
 MODULE_ALIAS_LDISC(N_SLCAN);
 MODULE_DESCRIPTION("serial line CAN interface");
 MODULE_LICENSE("GPL");
-MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
-
-#ifdef CONFIG_CAN_DEBUG_DEVICES
-static int debug;
-module_param(debug, int, S_IRUGO);
-#define DBG(args...)       (debug & 1 ? \
-                              (printk(KERN_DEBUG "slcan %s: ", __func__), \
-                               printk(args)) : 0)
-#else
-#define DBG(args...)
+MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
+static inline void *kzalloc(size_t size, unsigned int __nocast flags)
+{
+       void *ret = kmalloc(size, flags);
+       if (ret)
+               memset(ret, 0, size);
+       return ret;
+}
 #endif
-
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
 #ifndef N_SLCAN
 #error Your kernel does not support tty line discipline N_SLCAN
 #endif
@@ -123,6 +108,7 @@ module_param(debug, int, S_IRUGO);
  * rebuild to use the right value for N_SLCAN. This workaround will allow  
  * to use the slcan driver with an existing kernel.
  */
+#endif
 
 #define SLCAN_MAGIC 0x53CA
 
@@ -166,7 +152,6 @@ struct slcan {
 
 static struct net_device **slcan_devs;
 
-
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
 /* Netdevice get statistics request */
 static struct net_device_stats *slc_get_stats(struct net_device *dev)
@@ -178,7 +163,7 @@ static struct net_device_stats *slc_get_stats(struct net_device *dev)
 #endif
 
  /************************************************************************
-  *                    SLCAN ENCAPSULATION FORMAT                       *
+  *                    SLCAN ENCAPSULATION FORMAT                       *
   ************************************************************************/
 
 /*
@@ -213,7 +198,7 @@ static struct net_device_stats *slc_get_stats(struct net_device *dev)
  */
 
  /************************************************************************
-  *                    STANDARD SLCAN DECAPSULATION                     *
+  *                    STANDARD SLCAN DECAPSULATION                     *
   ************************************************************************/
 
 static int asc2nibble(char c)
@@ -236,8 +221,6 @@ static void slc_bump(struct slcan *sl)
 {
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
        struct net_device_stats *stats = slc_get_stats(sl->dev);
-#else
-       struct net_device_stats *stats = &sl->dev->stats;
 #endif
        struct sk_buff *skb;
        struct can_frame cf;
@@ -266,6 +249,7 @@ static void slc_bump(struct slcan *sl)
        if (strict_strtoul(sl->rbuff+1, 16, &ultmp))
                return;
 #endif
+
        cf.can_id = ultmp;
 
        if (!(cmd & 0x20)) /* NO tiny chars => extended frame format */
@@ -304,8 +288,13 @@ static void slc_bump(struct slcan *sl)
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
        sl->dev->last_rx = jiffies;
 #endif
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
        stats->rx_packets++;
        stats->rx_bytes += cf.can_dlc;
+#else
+       sl->dev->stats.rx_packets++;
+       sl->dev->stats.rx_bytes += cf.can_dlc;
+#endif
 }
 
 /* parse tty input stream */
@@ -313,8 +302,6 @@ static void slcan_unesc(struct slcan *sl, unsigned char s)
 {
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
        struct net_device_stats *stats = slc_get_stats(sl->dev);
-#else
-       struct net_device_stats *stats = &sl->dev->stats;
 #endif
 
        if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
@@ -329,7 +316,11 @@ static void slcan_unesc(struct slcan *sl, unsigned char s)
                                sl->rbuff[sl->rcount++] = s;
                                return;
                        } else {
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
                                stats->rx_over_errors++;
+#else
+                               sl->dev->stats.rx_over_errors++;
+#endif
                                set_bit(SLF_ERROR, &sl->flags);
                        }
                }
@@ -337,7 +328,7 @@ static void slcan_unesc(struct slcan *sl, unsigned char s)
 }
 
  /************************************************************************
-  *                    STANDARD SLCAN ENCAPSULATION                     *
+  *                    STANDARD SLCAN ENCAPSULATION                     *
   ************************************************************************/
 
 /* Encapsulate one can_frame and stuff into a TTY queue. */
@@ -345,8 +336,6 @@ static void slc_encaps(struct slcan *sl, struct can_frame *cf)
 {
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
        struct net_device_stats *stats = slc_get_stats(sl->dev);
-#else
-       struct net_device_stats *stats = &sl->dev->stats;
 #endif
        int actual, idx, i;
        char cmd;
@@ -368,19 +357,17 @@ static void slc_encaps(struct slcan *sl, struct can_frame *cf)
        for (i = 0; i < cf->can_dlc; i++)
                sprintf(&sl->xbuff[idx + 2*i], "%02X", cf->data[i]);
 
-       DBG("ASCII frame = '%s'\n", sl->xbuff);
-
        strcat(sl->xbuff, "\r"); /* add terminating character */
 
        /* Order of next two lines is *very* important.
         * When we are sending a little amount of data,
-        * the transfer may be completed inside driver.write()
+        * the transfer may be completed inside the ops->write()
         * routine, because it's running with interrupts enabled.
         * In this case we *never* got WRITE_WAKEUP event,
         * if we did not request it before write operation.
         *       14 Oct 1994  Dmitry Gorodchanin.
         */
-       sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
+       set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
        actual = sl->tty->driver->write(sl->tty, sl->xbuff, strlen(sl->xbuff));
 #else
@@ -388,7 +375,11 @@ static void slc_encaps(struct slcan *sl, struct can_frame *cf)
 #endif
        sl->xleft = strlen(sl->xbuff) - actual;
        sl->xhead = sl->xbuff + actual;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
        stats->tx_bytes += cf->can_dlc;
+#else
+       sl->dev->stats.tx_bytes += cf->can_dlc;
+#endif
 }
 
 /*
@@ -401,8 +392,6 @@ static void slcan_write_wakeup(struct tty_struct *tty)
        struct slcan *sl = (struct slcan *) tty->disc_data;
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
        struct net_device_stats *stats = slc_get_stats(sl->dev);
-#else
-       struct net_device_stats *stats = &sl->dev->stats;
 #endif
 
        /* First make sure we're connected. */
@@ -412,8 +401,12 @@ static void slcan_write_wakeup(struct tty_struct *tty)
        if (sl->xleft <= 0)  {
                /* Now serial buffer is almost free & we can start
                 * transmission of another packet */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
                stats->tx_packets++;
-               tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
+#else
+               sl->dev->stats.tx_packets++;
+#endif
+               clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
                netif_wake_queue(sl->dev);
                return;
        }
@@ -427,11 +420,6 @@ static void slcan_write_wakeup(struct tty_struct *tty)
        sl->xhead += actual;
 }
 
-
-/******************************************
- *   Routines looking at netdevice side.
- ******************************************/
-
 /* Send a can_frame to a TTY queue. */
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
 static int slc_xmit(struct sk_buff *skb, struct net_device *dev)
@@ -450,7 +438,6 @@ static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev)
                printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
                goto out;
        }
-
        if (sl->tty == NULL) {
                spin_unlock(&sl->lock);
                goto out;
@@ -470,6 +457,10 @@ out:
 }
 
 
+/******************************************
+ *   Routines looking at netdevice side.
+ ******************************************/
+
 /* Netdevice UP -> DOWN routine */
 static int slc_close(struct net_device *dev)
 {
@@ -478,7 +469,7 @@ static int slc_close(struct net_device *dev)
        spin_lock_bh(&sl->lock);
        if (sl->tty) {
                /* TTY discipline is running. */
-               sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
+               clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
        }
        netif_stop_queue(dev);
        sl->rcount   = 0;
@@ -519,11 +510,10 @@ static const struct net_device_ops slc_netdev_ops = {
 };
 #endif
 
-/* Netdevice register callback */
 static void slc_setup(struct net_device *dev)
 {
 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
-       dev->netdev_ops = &slc_netdev_ops;
+       dev->netdev_ops         = &slc_netdev_ops;
 #else
        dev->open               = slc_open;
        dev->stop               = slc_close;
@@ -551,7 +541,7 @@ static void slc_setup(struct net_device *dev)
 }
 
 /******************************************
* Routines looking at TTY side.
+  Routines looking at TTY side.
  ******************************************/
 
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
@@ -576,19 +566,20 @@ static void slcan_receive_buf(struct tty_struct *tty,
        struct slcan *sl = (struct slcan *) tty->disc_data;
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
        struct net_device_stats *stats = slc_get_stats(sl->dev);
-#else
-       struct net_device_stats *stats = &sl->dev->stats;
 #endif
 
-       if (!sl || sl->magic != SLCAN_MAGIC ||
-           !netif_running(sl->dev))
+       if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev))
                return;
 
        /* Read the characters out of the buffer */
        while (count--) {
                if (fp && *fp++) {
                        if (!test_and_set_bit(SLF_ERROR, &sl->flags))
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
                                stats->rx_errors++;
+#else
+                               sl->dev->stats.rx_errors++;
+#endif
                        cp++;
                        continue;
                }
@@ -601,7 +592,6 @@ static void slcan_receive_buf(struct tty_struct *tty,
  ************************************/
 
 /* Collect hanged up channels */
-
 static void slc_sync(void)
 {
        int i;
@@ -616,14 +606,13 @@ static void slc_sync(void)
                sl = netdev_priv(dev);
                if (sl->tty || sl->leased)
                        continue;
-               if (dev->flags&IFF_UP)
+               if (dev->flags & IFF_UP)
                        dev_close(dev);
        }
 }
 
-
 /* Find a free SLCAN channel, and link in this `tty' line. */
-static struct slcan *slc_alloc(dev_t line, char forcednum)
+static struct slcan *slc_alloc(dev_t line)
 {
        int i;
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
@@ -680,18 +669,10 @@ static struct slcan *slc_alloc(dev_t line, char forcednum)
                }
        }
 
-       /* Check if the user specified an exact interface to use */
-       if (forcednum) {
-               sel = forcednum - '0';
-               score = 0;
-               i = sel;
-               dev = slcan_devs[i];
-       }
-
        if (sel >= 0) {
                i = sel;
                dev = slcan_devs[i];
-               if ((score > 1) && (dev)) {
+               if (score > 1) {
                        sl = netdev_priv(dev);
                        sl->flags &= (1 << SLF_INUSE);
                        return sl;
@@ -714,7 +695,7 @@ static struct slcan *slc_alloc(dev_t line, char forcednum)
 
        if (!dev) {
                char name[IFNAMSIZ];
-               sprintf(name, "can%d", i);
+               sprintf(name, "slcan%d", i);
 
                dev = alloc_netdev(sizeof(*sl), name, slc_setup);
                if (!dev)
@@ -725,8 +706,8 @@ static struct slcan *slc_alloc(dev_t line, char forcednum)
        sl = netdev_priv(dev);
 
        /* Initialize channel control data */
-       sl->magic       = SLCAN_MAGIC;
-       sl->dev         = dev;
+       sl->magic = SLCAN_MAGIC;
+       sl->dev = dev;
        spin_lock_init(&sl->lock);
        slcan_devs[i] = dev;
 
@@ -765,7 +746,7 @@ static int slcan_open(struct tty_struct *tty)
        /* Collect hanged up channels. */
        slc_sync();
 
-       sl = (struct slcan *) tty->disc_data;
+       sl = tty->disc_data;
 
        err = -EEXIST;
        /* First make sure we're not already connected. */
@@ -774,21 +755,7 @@ static int slcan_open(struct tty_struct *tty)
 
        /* OK.  Find a free SLCAN channel to use. */
        err = -ENFILE;
-
-       /* Look to see if the user has requested a specific channel
-        * to be used (encoded as '0' plus the interface number requested in
-        * the otherwise unused swtch termios variable )
-        * stty swtch 'channel' device
-        *  where 'channel' is '0' to maxdevs (0)
-        *  where device is the name of the serial device (/dev/ttyUSB0)
-        */
-       if ((SWTC_CHAR(tty)>='0') && (SWTC_CHAR(tty)<'0'+maxdev)){
-               sl = slc_alloc(tty_devnum(tty),SWTC_CHAR(tty));
-       } else {
-               /* OK.  Find a free SLCAN channel to use. */
-               sl = slc_alloc(tty_devnum(tty),0);
-       }
-
+       sl = slc_alloc(tty_devnum(tty));
        if (sl == NULL)
                goto err_exit;
 
@@ -817,11 +784,9 @@ static int slcan_open(struct tty_struct *tty)
 
        /* Done.  We have linked the TTY line to a channel. */
        rtnl_unlock();
-
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
        tty->receive_room = 65536;      /* We don't flow control */
 #endif
-
        return sl->dev->base_addr;
 
 err_free_chan:
@@ -843,6 +808,7 @@ err_exit:
  *
  * We also use this method for a hangup event.
  */
+
 static void slcan_close(struct tty_struct *tty)
 {
        struct slcan *sl = (struct slcan *) tty->disc_data;
@@ -914,26 +880,22 @@ static struct tty_ldisc   slc_ldisc = {
 #else
 static struct tty_ldisc_ops slc_ldisc = {
 #endif
-       .owner          = THIS_MODULE,
-       .magic          = TTY_LDISC_MAGIC,
-       .name           = "slcan",
-       .open           = slcan_open,
-       .close          = slcan_close,
+       .owner          = THIS_MODULE,
+       .magic          = TTY_LDISC_MAGIC,
+       .name           = "slcan",
+       .open           = slcan_open,
+       .close          = slcan_close,
 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,31)
-       .hangup         = slcan_hangup,
+       .hangup         = slcan_hangup,
 #endif
        .ioctl          = slcan_ioctl,
        .receive_buf    = slcan_receive_buf,
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
-       .receive_room   = slcan_receive_room,
+       .receive_room   = slcan_receive_room,
 #endif
        .write_wakeup   = slcan_write_wakeup,
 };
 
-/************************************
- * general slcan module init/exit
- ************************************/
-
 static int __init slcan_init(void)
 {
        int status;
@@ -944,18 +906,15 @@ static int __init slcan_init(void)
        printk(banner);
        printk(KERN_INFO "slcan: %d dynamic interface channels.\n", maxdev);
 
-       slcan_devs = kmalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
+       slcan_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
        if (!slcan_devs) {
                printk(KERN_ERR "slcan: can't allocate slcan device array!\n");
                return -ENOMEM;
        }
 
-       /* Clear the pointer array, we allocate devices when we need them */
-       memset(slcan_devs, 0, sizeof(struct net_device *)*maxdev);
-
        /* Fill in our line protocol discipline, and register it */
        status = tty_register_ldisc(N_SLCAN, &slc_ldisc);
-       if (status != 0)  {
+       if (status)  {
                printk(KERN_ERR "slcan: can't register line discipline\n");
                kfree(slcan_devs);
        }
@@ -994,8 +953,8 @@ static void __exit slcan_exit(void)
                }
        } while (busy && time_before(jiffies, timeout));
 
-       /* FIXME (2.6.32+): hangup is async so we should wait when doing
-          this second phase */
+       /* FIXME: hangup is async so we should wait when doing this second
+          phase */
 
        for (i = 0; i < maxdev; i++) {
                dev = slcan_devs[i];