]> rtime.felk.cvut.cz Git - socketcan-devel.git/blobdiff - kernel/2.6/drivers/net/can/slcan.c
Added possibility to force the slcan driver to use a specific device number
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / slcan.c
index 7fa98fee623f1d14d0caa92685b744ccfd88d357..3cbb9b80b1a16191c139f8755065bf36cd4c0fbc 100644 (file)
@@ -1,29 +1,33 @@
 /*
  * slcan.c - serial line CAN interface driver (using tty line discipline)
  *
- * Copyright (c) 2007 Volkswagen Group Electronic Research
- * All rights reserved.
+ * This file is derived from linux/drivers/net/slip.c
  *
- * Redistribution and use in source and binary forms, with or without
- * 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'.
- * 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.
- * 3. Neither the name of Volkswagen nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
+ * 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 ...
  *
- * 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.
+ * 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>
  *
- * The provided data structures and external interfaces from this code
- * are not restricted to be used by modules with a GPL compatible license.
+ * Copyright (c) 2007-2009 Volkswagen Group Electronic Research
+ *
+ * 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. You can also get it
+ * at http://www.gnu.org/licenses/gpl.html
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  *
  */
 
-/*
- * 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>
- */
-
+#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/sched.h>
 #include <linux/string.h>
 #include <linux/mm.h>
 #include <linux/interrupt.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 <linux/can.h>
-#include <linux/can/version.h>
+#include <socketcan/can.h>
 
+#include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
 RCSID("$Id$");
 
-static __initdata const char banner[] = KERN_INFO
-                       "CAN: serial line CAN interface " VERSION "\n"; 
+static __initdata const char banner[] =
+       KERN_INFO "slcan: serial line CAN interface driver\n";
 
 MODULE_ALIAS_LDISC(N_SLCAN);
 MODULE_DESCRIPTION("serial line CAN interface");
-MODULE_LICENSE("Dual BSD/GPL");
+MODULE_LICENSE("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__), \
@@ -106,13 +105,25 @@ 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
  *
+ * NEW: Since Kernel 2.6.21 you only have to change include/linux/tty.h
+ *
+ * HACK for precompiled Kernels:
+ *
+ * In order to use the slcan driver without rebuilding the kernel, the slcan
+ * driver must be compiled to use an existing line discipline.
+ * The N_MOUSE line discipline is documented to be free for custom use and
+ * using it *should* not cause any side effect.
+ *
+ * Then, before compiling the slcan driver, add a -DN_SLCAN=N_MOUSE  
+ * compilation option in its Makefile. The slcan_attach tool must(!!) also be
+ * rebuild to use the right value for N_SLCAN. This workaround will allow  
+ * to use the slcan driver with an existing kernel.
  */
 
-#define SLC_CHECK_TRANSMIT
 #define SLCAN_MAGIC 0x53CA
 
 static int maxdev = 10;                /* MAX number of SLCAN channels;
@@ -139,8 +150,10 @@ struct slcan {
        unsigned char           *xhead;         /* pointer to next XMIT byte */
        int                     xleft;          /* bytes left in XMIT queue  */
 
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
        /* SLCAN interface statistics. */
        struct net_device_stats stats;
+#endif
 
        unsigned long           flags;          /* Flag values/ mode etc     */
 #define SLF_INUSE              0               /* Channel in use            */
@@ -153,6 +166,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                       *
   ************************************************************************/
@@ -178,7 +202,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
@@ -192,7 +216,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';
@@ -209,9 +234,15 @@ static int asc2nibble(char c) {
 /* Send one completely decapsulated can_frame to the network layer */
 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;
        int i, dlc_pos, tmp;
+       unsigned long ultmp;
        char cmd = sl->rbuff[0];
 
        if ((cmd != 't') && (cmd != 'T') && (cmd != 'r') && (cmd != 'R'))
@@ -225,11 +256,18 @@ static void slc_bump(struct slcan *sl)
        if (!((sl->rbuff[dlc_pos] >= '0') && (sl->rbuff[dlc_pos] < '9')))
                return;
 
-       cf.can_dlc = sl->rbuff[dlc_pos] & 0x0F; /* get can_dlc */
+       cf.can_dlc = sl->rbuff[dlc_pos] - '0'; /* get can_dlc from ASCII val */
 
        sl->rbuff[dlc_pos] = 0; /* terminate can_id string */
-       cf.can_id = simple_strtoul(sl->rbuff+1, NULL, 16);
-       
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
+       ultmp = simple_strtoul(sl->rbuff+1, NULL, 16);
+#else
+       if (strict_strtoul(sl->rbuff+1, 16, &ultmp))
+               return;
+#endif
+       cf.can_id = ultmp;
+
        if (!(cmd & 0x20)) /* NO tiny chars => extended frame format */
                cf.can_id |= CAN_EFF_FLAG;
 
@@ -238,12 +276,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;
        }
@@ -255,18 +295,28 @@ static void slc_bump(struct slcan *sl)
 
        skb->dev = sl->dev;
        skb->protocol = htons(ETH_P_CAN);
+       skb->pkt_type = PACKET_BROADCAST;
+       skb->ip_summed = CHECKSUM_UNNECESSARY;
        memcpy(skb_put(skb, sizeof(struct can_frame)),
               &cf, sizeof(struct can_frame));
        netif_rx(skb);
 
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
        sl->dev->last_rx = jiffies;
-       sl->stats.rx_packets++;
-       sl->stats.rx_bytes += cf.can_dlc;
+#endif
+       stats->rx_packets++;
+       stats->rx_bytes += cf.can_dlc;
 }
 
 /* parse tty input stream */
 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 */
                if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
                    (sl->rcount > 4))  {
@@ -279,7 +329,7 @@ static void slcan_unesc(struct slcan *sl, unsigned char s)
                                sl->rbuff[sl->rcount++] = s;
                                return;
                        } else {
-                               sl->stats.rx_over_errors++;
+                               stats->rx_over_errors++;
                                set_bit(SLF_ERROR, &sl->flags);
                        }
                }
@@ -293,6 +343,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)
 {
+#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;
 
@@ -326,13 +381,14 @@ 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));
-#ifdef SLC_CHECK_TRANSMIT
-       sl->dev->trans_start = jiffies;
+#else
+       actual = sl->tty->ops->write(sl->tty, sl->xbuff, strlen(sl->xbuff));
 #endif
        sl->xleft = strlen(sl->xbuff) - actual;
        sl->xhead = sl->xbuff + actual;
-       sl->stats.tx_bytes += cf->can_dlc;
+       stats->tx_bytes += cf->can_dlc;
 }
 
 /*
@@ -343,62 +399,45 @@ static void slcan_write_wakeup(struct tty_struct *tty)
 {
        int actual;
        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. */
-       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 */
-               sl->stats.tx_packets++;
+               stats->tx_packets++;
                tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
                netif_wake_queue(sl->dev);
                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;
 }
 
-static void slc_tx_timeout(struct net_device *dev)
-{
-       struct slcan *sl = netdev_priv(dev);
-
-       spin_lock(&sl->lock);
-
-       if (netif_queue_stopped(dev)) {
-               if (!netif_running(dev))
-                       goto out;
-
-               /* May be we must check transmitter timeout here ?
-                *      14 Oct 1994 Dmitry Gorodchanin.
-                */
-#ifdef SLC_CHECK_TRANSMIT
-               if (time_before(jiffies, dev->trans_start + 20 * HZ))  {
-                       /* 20 sec timeout not reached */
-                       goto out;
-               }
-               printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
-                      (sl->tty->driver->chars_in_buffer(sl->tty) || sl->xleft)
-                      ? "bad line quality" : "driver error");
-               sl->xleft = 0;
-               sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
-               netif_wake_queue(sl->dev);
-#endif
-       }
-out:
-       spin_unlock(&sl->lock);
-}
-
 
 /******************************************
  *   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)
+#else
+static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev)
+#endif
 {
        struct slcan *sl = netdev_priv(dev);
 
@@ -422,8 +461,12 @@ static int slc_xmit(struct sk_buff *skb, struct net_device *dev)
        spin_unlock(&sl->lock);
 
 out:
-       dev_kfree_skb(skb);
+       kfree_skb(skb);
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
        return 0;
+#else
+       return NETDEV_TX_OK;
+#endif
 }
 
 
@@ -450,7 +493,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);
@@ -458,35 +501,49 @@ static int slc_open(struct net_device *dev)
        return 0;
 }
 
-/* Netdevice get statistics request */
-static struct net_device_stats *slc_get_stats(struct net_device *dev)
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,31)
+/* Hook the destructor so we can free slcan devs at the right point in time */
+static void slc_free_netdev(struct net_device *dev)
 {
-       struct slcan *sl = netdev_priv(dev);
-
-       return (&sl->stats);
+       int i = dev->base_addr;
+       free_netdev(dev);
+       slcan_devs[i] = NULL;
 }
+#endif
+
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
+static const struct net_device_ops slc_netdev_ops = {
+       .ndo_open               = slc_open,
+       .ndo_stop               = slc_close,
+       .ndo_start_xmit         = slc_xmit,
+};
+#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;
+#else
        dev->open               = slc_open;
-       dev->destructor         = free_netdev;
        dev->stop               = slc_close;
-       dev->get_stats          = slc_get_stats;
        dev->hard_start_xmit    = slc_xmit;
+#endif
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,31)
+       dev->destructor         = slc_free_netdev;
+#else
+       dev->destructor         = free_netdev;
+#endif
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
+       dev->get_stats          = slc_get_stats;
+#endif
 
        dev->hard_header_len    = 0;
        dev->addr_len           = 0;
        dev->tx_queue_len       = 10;
 
-       SET_MODULE_OWNER(dev);
-
        dev->mtu                = sizeof(struct can_frame);
        dev->type               = ARPHRD_CAN;
-#ifdef SLC_CHECK_TRANSMIT
-       dev->tx_timeout         = slc_tx_timeout;
-       dev->watchdog_timeo     = 20*HZ;
-#endif
 
        /* New-style flags. */
        dev->flags              = IFF_NOARP;
@@ -517,6 +574,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;
+#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))
@@ -525,9 +587,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))  {
-                               sl->stats.rx_errors++;
-                       }
+                       if (!test_and_set_bit(SLF_ERROR, &sl->flags))
+                               stats->rx_errors++;
                        cp++;
                        continue;
                }
@@ -548,7 +609,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);
@@ -561,11 +623,13 @@ static void slc_sync(void)
 
 
 /* Find a free SLCAN channel, and link in this `tty' line. */
-static struct slcan *slc_alloc(dev_t line)
+static struct slcan *slc_alloc(dev_t line, char forcednum)
 {
        int i;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
        int sel = -1;
        int score = -1;
+#endif
        struct net_device *dev = NULL;
        struct slcan       *sl;
 
@@ -577,6 +641,7 @@ static struct slcan *slc_alloc(dev_t line)
                if (dev == NULL)
                        break;
 
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
                sl = netdev_priv(dev);
                if (sl->leased) {
                        if (sl->line != line)
@@ -615,14 +680,23 @@ static struct slcan *slc_alloc(dev_t line)
                }
        }
 
+       /* 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) {
+               if ((score > 1) && (dev)) {
                        sl = netdev_priv(dev);
                        sl->flags &= (1 << SLF_INUSE);
                        return sl;
                }
+#endif
        }
 
        /* Sorry, too many, all slots in use */
@@ -633,7 +707,6 @@ static struct slcan *slc_alloc(dev_t line)
                sl = netdev_priv(dev);
                if (test_bit(SLF_INUSE, &sl->flags)) {
                        unregister_netdevice(dev);
-                       free_netdev(dev); /* new in slcan.c */
                        dev = NULL;
                        slcan_devs[i] = NULL;
                }
@@ -641,7 +714,7 @@ static struct slcan *slc_alloc(dev_t line)
 
        if (!dev) {
                char name[IFNAMSIZ];
-               sprintf(name, "slc%d", i);
+               sprintf(name, "can%d", i);
 
                dev = alloc_netdev(sizeof(*sl), name, slc_setup);
                if (!dev)
@@ -675,9 +748,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.
@@ -696,7 +774,22 @@ 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)
+
+       /* 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);
+       }
+
+       if (sl == NULL)
                goto err_exit;
 
        sl->tty = tty;
@@ -717,15 +810,18 @@ 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;
        }
 
        /* 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:
@@ -740,30 +836,12 @@ err_exit:
        return err;
 }
 
-/*
-
-  FIXME: 1,2 are fixed 3 was never true anyway.
-
-   Let me to blame a bit.
-   1. TTY module calls this funstion on soft interrupt.
-   2. TTY module calls this function WITH MASKED INTERRUPTS!
-   3. TTY module does not notify us about line discipline
-      shutdown,
-
-   Seems, now it is clean. The solution is to consider netdevice and
-   line discipline sides as two independent threads.
-
-   By-product (not desired): slc? does not feel hangups and remains open.
-   It is supposed, that user level program (dip, diald, slattach...)
-   will catch SIGHUP and make the rest of work.
-
-   I see no way to make more with current tty code. --ANK
- */
-
 /*
  * Close down a SLCAN channel.
  * This means flushing out any pending queues, and then returning. This
  * call is serialized against other ldisc functions.
+ *
+ * We also use this method for a hangup event.
  */
 static void slcan_close(struct tty_struct *tty)
 {
@@ -778,9 +856,23 @@ static void slcan_close(struct tty_struct *tty)
        if (!sl->leased)
                sl->line = 0;
 
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,31)
+       /* Flush network side */
+       unregister_netdev(sl->dev);
+       /* This will complete via sl_free_netdev */
+#else
        /* Count references from TTY module */
+#endif
 }
 
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,31)
+static int slcan_hangup(struct tty_struct *tty)
+{
+       slcan_close(tty);
+       return 0;
+}
+#endif
+
 /* Perform I/O control on an active SLCAN channel. */
 static int slcan_ioctl(struct tty_struct *tty, struct file *file,
                       unsigned int cmd, unsigned long arg)
@@ -789,11 +881,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))
@@ -803,7 +894,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:
@@ -811,15 +902,26 @@ 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",
        .open           = slcan_open,
        .close          = slcan_close,
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,31)
+       .hangup         = slcan_hangup,
+#endif
        .ioctl          = slcan_ioctl,
        .receive_buf    = slcan_receive_buf,
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
@@ -840,7 +942,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) {
@@ -852,7 +954,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);
        }
@@ -891,6 +994,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 */
 
        for (i = 0; i < maxdev; i++) {
                dev = slcan_devs[i];
@@ -912,10 +1017,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);