/*
* 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
+ *
+ * slip.c Authors : Laurence Culhane <loz@holmes.demon.co.uk>
+ * Fred N. van Kempen <waltje@uwalt.nl.mugnet.org>
+ * slcan.c Author : Oliver Hartkopp <socketcan@hartkopp.net>
*
- * 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 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.
- * 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.
+ * 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.
*
- * 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, in which case the provisions of the
- * GPL apply INSTEAD OF those given above.
+ * 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.
*
- * The provided data structures and external interfaces from this code
- * are not restricted to be used by modules with a GPL compatible license.
+ * 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/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/if_slip.h>
#include <linux/delay.h>
#include <linux/init.h>
+#include <socketcan/can.h>
-#include <linux/can.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$");
static __initdata const char banner[] =
MODULE_ALIAS_LDISC(N_SLCAN);
MODULE_DESCRIPTION("serial line CAN interface");
-MODULE_LICENSE("Dual BSD/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_LICENSE("GPL");
+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
*
* 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.
*/
+#endif
-#define SLC_CHECK_TRANSMIT
#define SLCAN_MAGIC 0x53CA
static int maxdev = 10; /* MAX number of SLCAN channels;
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 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'))
if (!((sl->rbuff[dlc_pos] >= '0') && (sl->rbuff[dlc_pos] < '9')))
return;
- cf.can_dlc = sl->rbuff[dlc_pos] & 0x0F; /* get can_dlc */
-
- if (cf.can_dlc > 8)
- return;
+ cf.can_dlc = sl->rbuff[dlc_pos] - '0'; /* get can_dlc from ASCII val */
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);
+ ultmp = simple_strtoul(sl->rbuff+1, NULL, 16);
#else
- if (strict_strtoul(sl->rbuff+1, 16, (unsigned long *) &cf.can_id))
+ 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;
&cf, sizeof(struct can_frame));
netif_rx(skb);
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
sl->dev->last_rx = jiffies;
+#endif
stats->rx_packets++;
stats->rx_bytes += cf.can_dlc;
}
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
actual = sl->tty->ops->write(sl->tty, sl->xbuff, strlen(sl->xbuff));
-#endif
-#ifdef SLC_CHECK_TRANSMIT
- sl->dev->trans_start = jiffies;
#endif
sl->xleft = strlen(sl->xbuff) - actual;
sl->xhead = sl->xbuff + actual;
/* Now serial buffer is almost free & we can start
* transmission of another packet */
stats->tx_packets++;
- tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
+ clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
netif_wake_queue(sl->dev);
return;
}
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,
-#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);
- 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);
printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
goto out;
}
-
if (sl->tty == NULL) {
spin_unlock(&sl->lock);
goto out;
out:
kfree_skb(skb);
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
return 0;
+#else
+ return NETDEV_TX_OK;
+#endif
}
+/******************************************
+ * Routines looking at netdevice side.
+ ******************************************/
+
/* Netdevice UP -> DOWN routine */
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;
return 0;
}
-/* Netdevice register callback */
+#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)
+{
+ 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
+
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->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_start_xmit = slc_xmit;
dev->hard_header_len = 0;
dev->addr_len = 0;
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;
}
/******************************************
- * Routines looking at TTY side.
+ Routines looking at TTY side.
******************************************/
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
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 */
************************************/
/* Collect hanged up channels */
-
static void slc_sync(void)
{
int i;
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)
{
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;
if (dev == NULL)
break;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
sl = netdev_priv(dev);
if (sl->leased) {
if (sl->line != line)
sl->flags &= (1 << SLF_INUSE);
return sl;
}
+#endif
}
/* Sorry, too many, all slots in use */
if (!dev) {
char name[IFNAMSIZ];
- sprintf(name, "slc%d", i);
+ sprintf(name, "slcan%d", i);
dev = alloc_netdev(sizeof(*sl), name, slc_setup);
if (!dev)
/* 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. */
/* 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:
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)
{
struct slcan *sl = (struct slcan *) tty->disc_data;
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,
.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)
.write_wakeup = slcan_write_wakeup,
};
-/************************************
- * general slcan module init/exit
- ************************************/
-
static int __init slcan_init(void)
{
int status;
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);
}
}
} while (busy && time_before(jiffies, timeout));
+ /* FIXME: hangup is async so we should wait when doing this second
+ phase */
for (i = 0; i < maxdev; i++) {
dev = slcan_devs[i];