]> rtime.felk.cvut.cz Git - socketcan-devel.git/blobdiff - kernel/2.6/drivers/net/can/slcan.c
Reverted the removal of dev->get_stats = can_get_stats for kernels < 2.6.23
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / slcan.c
index 818a1d5d6148dc992363a32bacb0554b747dfff7..53f15b3f6550651260031b2229bca37dd498a9fe 100644 (file)
@@ -8,8 +8,7 @@
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions, the following disclaimer and
- *    the referenced file 'COPYING'.
+ *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
@@ -19,8 +18,8 @@
  *
  * Alternatively, provided that this notice is retained in full, this
  * software may be distributed under the terms of the GNU General
- * Public License ("GPL") version 2 as distributed in the 'COPYING'
- * file from the main directory of the linux kernel source.
+ * Public License ("GPL") version 2, in which case the provisions of the
+ * GPL apply INSTEAD OF those given above.
  *
  * The provided data structures and external interfaces from this code
  * are not restricted to be used by modules with a GPL compatible license.
  *                 Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  */
 
+#include <linux/version.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 
 #include <asm/system.h>
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17)
+#include <linux/uaccess.h>
+#else
 #include <asm/uaccess.h>
+#endif
 #include <linux/bitops.h>
 #include <linux/string.h>
 #include <linux/mm.h>
@@ -90,7 +94,7 @@ MODULE_LICENSE("Dual BSD/GPL");
 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
 
 #ifdef CONFIG_CAN_DEBUG_DEVICES
-static int debug = 0;
+static int debug;
 module_param(debug, int, S_IRUGO);
 #define DBG(args...)       (debug & 1 ? \
                               (printk(KERN_DEBUG "slcan %s: ", __func__), \
@@ -107,7 +111,7 @@ module_param(debug, int, S_IRUGO);
  * you will have to modify two kernel includes & recompile the kernel.
  *
  * Add this in include/asm/termios.h after the definition of N_HCI:
- *        #define N_SLCAN         16 
+ *        #define N_SLCAN         16
  *
  * Increment NR_LDICS in include/linux/tty.h from 16 to 17
  *
@@ -158,6 +162,17 @@ 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)
+{
+       struct slcan *sl = netdev_priv(dev);
+
+       return &sl->stats;
+}
+#endif
+
  /************************************************************************
   *                    SLCAN ENCAPSULATION FORMAT                       *
   ************************************************************************/
@@ -183,7 +198,7 @@ static struct net_device **slcan_devs;
  * The <id> is 3 (standard) or 8 (extended) bytes in ASCII Hex (base64).
  * The <dlc> is a one byte ASCII number ('0' - '8')
  * The <data> section has at much ASCII Hex bytes as defined by the <dlc>
- * 
+ *
  * Examples:
  *
  * t1230 : can_id 0x123, can_dlc 0, no data
@@ -197,7 +212,8 @@ static struct net_device **slcan_devs;
   *                    STANDARD SLCAN DECAPSULATION                     *
   ************************************************************************/
 
-static int asc2nibble(char c) {
+static int asc2nibble(char c)
+{
 
        if ((c >= '0') && (c <= '9'))
                return c - '0';
@@ -214,7 +230,11 @@ static int asc2nibble(char c) {
 /* Send one completely decapsulated can_frame to the network layer */
 static void slc_bump(struct slcan *sl)
 {
-       struct net_device_stats *stats = sl->dev->get_stats(sl->dev);
+#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;
        int i, dlc_pos, tmp;
@@ -234,8 +254,14 @@ static void slc_bump(struct slcan *sl)
        cf.can_dlc = sl->rbuff[dlc_pos] & 0x0F; /* get can_dlc */
 
        sl->rbuff[dlc_pos] = 0; /* terminate can_id string */
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
        cf.can_id = simple_strtoul(sl->rbuff+1, NULL, 16);
-       
+#else
+       if (strict_strtoul(sl->rbuff+1, 16, (unsigned long *) &cf.can_id))
+               return;
+#endif
+
        if (!(cmd & 0x20)) /* NO tiny chars => extended frame format */
                cf.can_id |= CAN_EFF_FLAG;
 
@@ -244,12 +270,14 @@ static void slc_bump(struct slcan *sl)
 
        *(u64 *) (&cf.data) = 0; /* clear payload */
 
-       for (i = 0, dlc_pos++; i < cf.can_dlc; i++){
-               
-               if ((tmp = asc2nibble(sl->rbuff[dlc_pos++])) > 0x0F)
+       for (i = 0, dlc_pos++; i < cf.can_dlc; i++) {
+
+               tmp = asc2nibble(sl->rbuff[dlc_pos++]);
+               if (tmp > 0x0F)
                        return;
                cf.data[i] = (tmp << 4);
-               if ((tmp = asc2nibble(sl->rbuff[dlc_pos++])) > 0x0F)
+               tmp = asc2nibble(sl->rbuff[dlc_pos++]);
+               if (tmp > 0x0F)
                        return;
                cf.data[i] |= tmp;
        }
@@ -275,7 +303,11 @@ static void slc_bump(struct slcan *sl)
 /* parse tty input stream */
 static void slcan_unesc(struct slcan *sl, unsigned char s)
 {
-       struct net_device_stats *stats = sl->dev->get_stats(sl->dev);
+#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 */
                if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
@@ -303,7 +335,11 @@ static void slcan_unesc(struct slcan *sl, unsigned char s)
 /* Encapsulate one can_frame and stuff into a TTY queue. */
 static void slc_encaps(struct slcan *sl, struct can_frame *cf)
 {
-       struct net_device_stats *stats = sl->dev->get_stats(sl->dev);
+#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;
 
@@ -337,7 +373,11 @@ static void slc_encaps(struct slcan *sl, struct can_frame *cf)
         *       14 Oct 1994  Dmitry Gorodchanin.
         */
        sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
        actual = sl->tty->driver->write(sl->tty, sl->xbuff, strlen(sl->xbuff));
+#else
+       actual = sl->tty->ops->write(sl->tty, sl->xbuff, strlen(sl->xbuff));
+#endif
 #ifdef SLC_CHECK_TRANSMIT
        sl->dev->trans_start = jiffies;
 #endif
@@ -354,12 +394,16 @@ static void slcan_write_wakeup(struct tty_struct *tty)
 {
        int actual;
        struct slcan *sl = (struct slcan *) tty->disc_data;
-       struct net_device_stats *stats = sl->dev->get_stats(sl->dev);
+#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. */
-       if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev)) {
+       if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev))
                return;
-       }
+
        if (sl->xleft <= 0)  {
                /* Now serial buffer is almost free & we can start
                 * transmission of another packet */
@@ -369,7 +413,11 @@ static void slcan_write_wakeup(struct tty_struct *tty)
                return;
        }
 
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
        actual = tty->driver->write(tty, sl->xhead, sl->xleft);
+#else
+       actual = tty->ops->write(tty, sl->xhead, sl->xleft);
+#endif
        sl->xleft -= actual;
        sl->xhead += actual;
 }
@@ -393,7 +441,11 @@ static void slc_tx_timeout(struct net_device *dev)
                        goto out;
                }
                printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
                       (sl->tty->driver->chars_in_buffer(sl->tty) || sl->xleft)
+#else
+                      (tty_chars_in_buffer(sl->tty) || sl->xleft)
+#endif
                       ? "bad line quality" : "driver error");
                sl->xleft = 0;
                sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
@@ -462,7 +514,7 @@ static int slc_open(struct net_device *dev)
 {
        struct slcan *sl = netdev_priv(dev);
 
-       if (sl->tty==NULL)
+       if (sl->tty == NULL)
                return -ENODEV;
 
        sl->flags &= (1 << SLF_INUSE);
@@ -470,16 +522,6 @@ static int slc_open(struct net_device *dev)
        return 0;
 }
 
-#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)
-{
-       struct slcan *sl = netdev_priv(dev);
-
-       return (&sl->stats);
-}
-#endif
-
 /* Netdevice register callback */
 static void slc_setup(struct net_device *dev)
 {
@@ -487,7 +529,7 @@ static void slc_setup(struct net_device *dev)
        dev->destructor         = free_netdev;
        dev->stop               = slc_close;
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
-       dev->get_stats          = slc_get_stats;
+       dev->get_stats          = slc_get_stats;
 #endif
        dev->hard_start_xmit    = slc_xmit;
 
@@ -495,10 +537,6 @@ static void slc_setup(struct net_device *dev)
        dev->addr_len           = 0;
        dev->tx_queue_len       = 10;
 
-#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
-       SET_MODULE_OWNER(dev);
-#endif
-
        dev->mtu                = sizeof(struct can_frame);
        dev->type               = ARPHRD_CAN;
 #ifdef SLC_CHECK_TRANSMIT
@@ -535,7 +573,11 @@ static void slcan_receive_buf(struct tty_struct *tty,
                              const unsigned char *cp, char *fp, int count)
 {
        struct slcan *sl = (struct slcan *) tty->disc_data;
-       struct net_device_stats *stats = sl->dev->get_stats(sl->dev);
+#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))
@@ -544,9 +586,8 @@ static void slcan_receive_buf(struct tty_struct *tty,
        /* Read the characters out of the buffer */
        while (count--) {
                if (fp && *fp++) {
-                       if (!test_and_set_bit(SLF_ERROR, &sl->flags))  {
+                       if (!test_and_set_bit(SLF_ERROR, &sl->flags))
                                stats->rx_errors++;
-                       }
                        cp++;
                        continue;
                }
@@ -567,7 +608,8 @@ static void slc_sync(void)
        struct slcan      *sl;
 
        for (i = 0; i < maxdev; i++) {
-               if ((dev = slcan_devs[i]) == NULL)
+               dev = slcan_devs[i];
+               if (dev == NULL)
                        break;
 
                sl = netdev_priv(dev);
@@ -694,9 +736,14 @@ static int slcan_open(struct tty_struct *tty)
        struct slcan *sl;
        int err;
 
-       if(!capable(CAP_NET_ADMIN))
+       if (!capable(CAP_NET_ADMIN))
                return -EPERM;
 
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,25)
+       if (tty->ops->write == NULL)
+               return -EOPNOTSUPP;
+#endif
+
        /* RTnetlink lock is misused here to serialize concurrent
           opens of slcan channels. There are better ways, but it is
           the simplest one.
@@ -715,7 +762,8 @@ static int slcan_open(struct tty_struct *tty)
 
        /* OK.  Find a free SLCAN channel to use. */
        err = -ENFILE;
-       if ((sl = slc_alloc(tty_devnum(tty))) == NULL)
+       sl = slc_alloc(tty_devnum(tty));
+       if (sl == NULL)
                goto err_exit;
 
        sl->tty = tty;
@@ -736,7 +784,8 @@ static int slcan_open(struct tty_struct *tty)
 
                set_bit(SLF_INUSE, &sl->flags);
 
-               if ((err = register_netdevice(sl->dev)))
+               err = register_netdevice(sl->dev);
+               if (err)
                        goto err_free_chan;
        }
 
@@ -810,11 +859,10 @@ static int slcan_ioctl(struct tty_struct *tty, struct file *file,
        unsigned int tmp;
 
        /* First make sure we're connected. */
-       if (!sl || sl->magic != SLCAN_MAGIC) {
+       if (!sl || sl->magic != SLCAN_MAGIC)
                return -EINVAL;
-       }
 
-       switch(cmd) {
+       switch (cmd) {
        case SIOCGIFNAME:
                tmp = strlen(sl->dev->name) + 1;
                if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
@@ -824,7 +872,7 @@ static int slcan_ioctl(struct tty_struct *tty, struct file *file,
        case SIOCSIFHWADDR:
                return -EINVAL;
 
-
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
        /* Allow stty to read, but not set, the serial port */
        case TCGETS:
        case TCGETA:
@@ -832,10 +880,18 @@ static int slcan_ioctl(struct tty_struct *tty, struct file *file,
 
        default:
                return -ENOIOCTLCMD;
+#else
+       default:
+               return tty_mode_ioctl(tty, file, cmd, arg);
+#endif
        }
 }
 
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
 static struct tty_ldisc        slc_ldisc = {
+#else
+static struct tty_ldisc_ops slc_ldisc = {
+#endif
        .owner          = THIS_MODULE,
        .magic          = TTY_LDISC_MAGIC,
        .name           = "slcan",
@@ -861,7 +917,7 @@ static int __init slcan_init(void)
                maxdev = 4; /* Sanity */
 
        printk(banner);
-       printk(KERN_INFO "slcan: %d dynamic interface channels.\n", maxdev );
+       printk(KERN_INFO "slcan: %d dynamic interface channels.\n", maxdev);
 
        slcan_devs = kmalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
        if (!slcan_devs) {
@@ -873,7 +929,8 @@ static int __init slcan_init(void)
        memset(slcan_devs, 0, sizeof(struct net_device *)*maxdev);
 
        /* Fill in our line protocol discipline, and register it */
-       if ((status = tty_register_ldisc(N_SLCAN, &slc_ldisc)) != 0)  {
+       status = tty_register_ldisc(N_SLCAN, &slc_ldisc);
+       if (status != 0)  {
                printk(KERN_ERR "slcan: can't register line discipline\n");
                kfree(slcan_devs);
        }
@@ -933,10 +990,9 @@ static void __exit slcan_exit(void)
        kfree(slcan_devs);
        slcan_devs = NULL;
 
-       if ((i = tty_unregister_ldisc(N_SLCAN)))
-       {
+       i = tty_unregister_ldisc(N_SLCAN);
+       if (i)
                printk(KERN_ERR "slcan: can't unregister ldisc (err %d)\n", i);
-       }
 }
 
 module_init(slcan_init);