Commit 815f62bf authored by Ralf Baechle's avatar Ralf Baechle Committed by Jeff Garzik

[PATCH] SMP rewrite of mkiss

Rewrite the mkiss driver to make it SMP-proof following the example of
6pack.c.
Signed-off-by: default avatarRalf Baechle DL5RB <ralf@linux-mips.org>
Signed-off-by: default avatarJeff Garzik <jgarzik@pobox.com>
parent 75a95178
config MKISS
tristate "Serial port KISS driver"
depends on AX25 && BROKEN_ON_SMP
depends on AX25
---help---
KISS is a protocol used for the exchange of data between a computer
and a Terminal Node Controller (a small embedded system commonly
......
/*
* MKISS Driver
* This program is free software; you can distribute it and/or modify it
* under the terms of the GNU General Public License (Version 2) as
* published by the Free Software Foundation.
*
* This module:
* This module 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 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.
*
* This module implements the AX.25 protocol for kernel-based
* devices like TTYs. It interfaces between a raw TTY, and the
* kernel's AX.25 protocol layers, just like slip.c.
* AX.25 needs to be separated from slip.c while slip.c is no
* longer a static kernel device since it is a module.
* This method clears the way to implement other kiss protocols
* like mkiss smack g8bpq ..... so far only mkiss is implemented.
* 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.
*
* Hans Alblas <hans@esrac.ele.tue.nl>
*
* History
* Jonathan (G4KLX) Fixed to match Linux networking changes - 2.1.15.
* Matthias (DG2FEF) Added support for FlexNet CRC (on special request)
* Fixed bug in ax25_close(): dev_lock_wait() was
* called twice, causing a deadlock.
* Jeroen (PE1RXQ) Removed old MKISS_MAGIC stuff and calls to
* MOD_*_USE_COUNT
* Remove cli() and fix rtnl lock usage.
* Copyright (C) Hans Alblas PE1AYX <hans@esrac.ele.tue.nl>
* Copyright (C) 2004, 05 Ralf Baechle DL5RB <ralf@linux-mips.org>
*/
#include <linux/config.h>
......@@ -50,34 +39,58 @@
#include <net/ax25.h>
#include "mkiss.h"
#ifdef CONFIG_INET
#include <linux/ip.h>
#include <linux/tcp.h>
#endif
static char banner[] __initdata = KERN_INFO "mkiss: AX.25 Multikiss, Hans Albas PE1AYX\n";
typedef struct ax25_ctrl {
struct ax_disp ctrl; /* */
struct net_device dev; /* the device */
} ax25_ctrl_t;
static ax25_ctrl_t **ax25_ctrls;
int ax25_maxdev = AX25_MAXDEV; /* Can be overridden with insmod! */
static struct tty_ldisc ax_ldisc;
static int ax25_init(struct net_device *);
static int kiss_esc(unsigned char *, unsigned char *, int);
static int kiss_esc_crc(unsigned char *, unsigned char *, unsigned short, int);
static void kiss_unesc(struct ax_disp *, unsigned char);
#define AX_MTU 236
/* SLIP/KISS protocol characters. */
#define END 0300 /* indicates end of frame */
#define ESC 0333 /* indicates byte stuffing */
#define ESC_END 0334 /* ESC ESC_END means END 'data' */
#define ESC_ESC 0335 /* ESC ESC_ESC means ESC 'data' */
struct mkiss {
struct tty_struct *tty; /* ptr to TTY structure */
struct net_device *dev; /* easy for intr handling */
/* These are pointers to the malloc()ed frame buffers. */
spinlock_t buflock;/* lock for rbuf and xbuf */
unsigned char *rbuff; /* receiver buffer */
int rcount; /* received chars counter */
unsigned char *xbuff; /* transmitter buffer */
unsigned char *xhead; /* pointer to next byte to XMIT */
int xleft; /* bytes left in XMIT queue */
struct net_device_stats stats;
/* Detailed SLIP statistics. */
int mtu; /* Our mtu (to spot changes!) */
int buffsize; /* Max buffers sizes */
unsigned long flags; /* Flag values/ mode etc */
/* long req'd: used by set_bit --RR */
#define AXF_INUSE 0 /* Channel in use */
#define AXF_ESCAPE 1 /* ESC received */
#define AXF_ERROR 2 /* Parity, etc. error */
#define AXF_KEEPTEST 3 /* Keepalive test flag */
#define AXF_OUTWAIT 4 /* is outpacket was flag */
int mode;
int crcmode; /* MW: for FlexNet, SMACK etc. */
#define CRC_MODE_NONE 0
#define CRC_MODE_FLEX 1
#define CRC_MODE_SMACK 2
atomic_t refcnt;
struct semaphore dead_sem;
};
/*---------------------------------------------------------------------------*/
static const unsigned short Crc_flex_table[] = {
static const unsigned short crc_flex_table[] = {
0x0f87, 0x1e0e, 0x2c95, 0x3d1c, 0x49a3, 0x582a, 0x6ab1, 0x7b38,
0x83cf, 0x9246, 0xa0dd, 0xb154, 0xc5eb, 0xd462, 0xe6f9, 0xf770,
0x1f06, 0x0e8f, 0x3c14, 0x2d9d, 0x5922, 0x48ab, 0x7a30, 0x6bb9,
......@@ -112,20 +125,16 @@ static const unsigned short Crc_flex_table[] = {
0x7440, 0x65c9, 0x5752, 0x46db, 0x3264, 0x23ed, 0x1176, 0x00ff
};
/*---------------------------------------------------------------------------*/
static unsigned short calc_crc_flex(unsigned char *cp, int size)
{
unsigned short crc = 0xffff;
while (size--)
crc = (crc << 8) ^ Crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
crc = (crc << 8) ^ crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
return crc;
}
/*---------------------------------------------------------------------------*/
static int check_crc_flex(unsigned char *cp, int size)
{
unsigned short crc = 0xffff;
......@@ -134,7 +143,7 @@ static int check_crc_flex(unsigned char *cp, int size)
return -1;
while (size--)
crc = (crc << 8) ^ Crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
crc = (crc << 8) ^ crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
if ((crc & 0xffff) != 0x7070)
return -1;
......@@ -142,82 +151,184 @@ static int check_crc_flex(unsigned char *cp, int size)
return 0;
}
/*---------------------------------------------------------------------------*/
/*
* Standard encapsulation
*/
/* Find a free channel, and link in this `tty' line. */
static inline struct ax_disp *ax_alloc(void)
static int kiss_esc(unsigned char *s, unsigned char *d, int len)
{
ax25_ctrl_t *axp=NULL;
int i;
unsigned char *ptr = d;
unsigned char c;
/*
* Send an initial END character to flush out any data that may have
* accumulated in the receiver due to line noise.
*/
for (i = 0; i < ax25_maxdev; i++) {
axp = ax25_ctrls[i];
*ptr++ = END;
/* Not allocated ? */
if (axp == NULL)
while (len-- > 0) {
switch (c = *s++) {
case END:
*ptr++ = ESC;
*ptr++ = ESC_END;
break;
case ESC:
*ptr++ = ESC;
*ptr++ = ESC_ESC;
break;
default:
*ptr++ = c;
break;
}
}
*ptr++ = END;
return ptr - d;
}
/*
* MW:
* OK its ugly, but tell me a better solution without copying the
* packet to a temporary buffer :-)
*/
static int kiss_esc_crc(unsigned char *s, unsigned char *d, unsigned short crc,
int len)
{
unsigned char *ptr = d;
unsigned char c=0;
*ptr++ = END;
while (len > 0) {
if (len > 2)
c = *s++;
else if (len > 1)
c = crc >> 8;
else if (len > 0)
c = crc & 0xff;
len--;
/* Not in use ? */
if (!test_and_set_bit(AXF_INUSE, &axp->ctrl.flags))
switch (c) {
case END:
*ptr++ = ESC;
*ptr++ = ESC_END;
break;
case ESC:
*ptr++ = ESC;
*ptr++ = ESC_ESC;
break;
default:
*ptr++ = c;
break;
}
}
*ptr++ = END;
return ptr - d;
}
/* Sorry, too many, all slots in use */
if (i >= ax25_maxdev)
return NULL;
/* Send one completely decapsulated AX.25 packet to the AX.25 layer. */
static void ax_bump(struct mkiss *ax)
{
struct sk_buff *skb;
int count;
/* If no channels are available, allocate one */
if (axp == NULL && (ax25_ctrls[i] = kmalloc(sizeof(ax25_ctrl_t), GFP_KERNEL)) != NULL) {
axp = ax25_ctrls[i];
spin_lock_bh(&ax->buflock);
if (ax->rbuff[0] > 0x0f) {
if (ax->rbuff[0] & 0x20) {
ax->crcmode = CRC_MODE_FLEX;
if (check_crc_flex(ax->rbuff, ax->rcount) < 0) {
ax->stats.rx_errors++;
return;
}
memset(axp, 0, sizeof(ax25_ctrl_t));
/* Initialize channel control data */
set_bit(AXF_INUSE, &axp->ctrl.flags);
sprintf(axp->dev.name, "ax%d", i++);
axp->ctrl.tty = NULL;
axp->dev.base_addr = i;
axp->dev.priv = (void *)&axp->ctrl;
axp->dev.next = NULL;
axp->dev.init = ax25_init;
if (axp != NULL) {
/*
* register device so that it can be ifconfig'ed
* ax25_init() will be called as a side-effect
* SIDE-EFFECT WARNING: ax25_init() CLEARS axp->ctrl !
ax->rcount -= 2;
/* dl9sau bugfix: the trailling two bytes flexnet crc
* will not be passed to the kernel. thus we have
* to correct the kissparm signature, because it
* indicates a crc but there's none
*/
if (register_netdev(&axp->dev) == 0) {
/* (Re-)Set the INUSE bit. Very Important! */
set_bit(AXF_INUSE, &axp->ctrl.flags);
axp->ctrl.dev = &axp->dev;
axp->dev.priv = (void *) &axp->ctrl;
*ax->rbuff &= ~0x20;
}
}
spin_unlock_bh(&ax->buflock);
return &axp->ctrl;
} else {
clear_bit(AXF_INUSE,&axp->ctrl.flags);
printk(KERN_ERR "mkiss: ax_alloc() - register_netdev() failure.\n");
count = ax->rcount;
if ((skb = dev_alloc_skb(count)) == NULL) {
printk(KERN_ERR "mkiss: %s: memory squeeze, dropping packet.\n",
ax->dev->name);
ax->stats.rx_dropped++;
return;
}
spin_lock_bh(&ax->buflock);
memcpy(skb_put(skb,count), ax->rbuff, count);
spin_unlock_bh(&ax->buflock);
skb->protocol = ax25_type_trans(skb, ax->dev);
netif_rx(skb);
ax->dev->last_rx = jiffies;
ax->stats.rx_packets++;
ax->stats.rx_bytes += count;
}
static void kiss_unesc(struct mkiss *ax, unsigned char s)
{
switch (s) {
case END:
/* drop keeptest bit = VSV */
if (test_bit(AXF_KEEPTEST, &ax->flags))
clear_bit(AXF_KEEPTEST, &ax->flags);
if (!test_and_clear_bit(AXF_ERROR, &ax->flags) && (ax->rcount > 2))
ax_bump(ax);
clear_bit(AXF_ESCAPE, &ax->flags);
ax->rcount = 0;
return;
case ESC:
set_bit(AXF_ESCAPE, &ax->flags);
return;
case ESC_ESC:
if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
s = ESC;
break;
case ESC_END:
if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
s = END;
break;
}
spin_lock_bh(&ax->buflock);
if (!test_bit(AXF_ERROR, &ax->flags)) {
if (ax->rcount < ax->buffsize) {
ax->rbuff[ax->rcount++] = s;
spin_unlock_bh(&ax->buflock);
return;
}
return NULL;
ax->stats.rx_over_errors++;
set_bit(AXF_ERROR, &ax->flags);
}
spin_unlock_bh(&ax->buflock);
}
/* Free an AX25 channel. */
static inline void ax_free(struct ax_disp *ax)
static int ax_set_mac_address(struct net_device *dev, void *addr)
{
/* Free all AX25 frame buffers. */
if (ax->rbuff)
kfree(ax->rbuff);
ax->rbuff = NULL;
if (ax->xbuff)
kfree(ax->xbuff);
ax->xbuff = NULL;
if (!test_and_clear_bit(AXF_INUSE, &ax->flags))
printk(KERN_ERR "mkiss: %s: ax_free for already free unit.\n", ax->dev->name);
struct sockaddr_ax25 *sa = addr;
spin_lock_irq(&dev->xmit_lock);
memcpy(dev->dev_addr, &sa->sax25_call, AX25_ADDR_LEN);
spin_unlock_irq(&dev->xmit_lock);
return 0;
}
static void ax_changedmtu(struct ax_disp *ax)
/*---------------------------------------------------------------------------*/
static void ax_changedmtu(struct mkiss *ax)
{
struct net_device *dev = ax->dev;
unsigned char *xbuff, *rbuff, *oxbuff, *orbuff;
......@@ -237,7 +348,8 @@ static void ax_changedmtu(struct ax_disp *ax)
rbuff = kmalloc(len + 4, GFP_ATOMIC);
if (xbuff == NULL || rbuff == NULL) {
printk(KERN_ERR "mkiss: %s: unable to grow ax25 buffers, MTU change cancelled.\n",
printk(KERN_ERR "mkiss: %s: unable to grow ax25 buffers, "
"MTU change cancelled.\n",
ax->dev->name);
dev->mtu = ax->mtu;
if (xbuff != NULL)
......@@ -259,7 +371,7 @@ static void ax_changedmtu(struct ax_disp *ax)
memcpy(ax->xbuff, ax->xhead, ax->xleft);
} else {
ax->xleft = 0;
ax->tx_dropped++;
ax->stats.tx_dropped++;
}
}
......@@ -270,7 +382,7 @@ static void ax_changedmtu(struct ax_disp *ax)
memcpy(ax->rbuff, orbuff, ax->rcount);
} else {
ax->rcount = 0;
ax->rx_over_errors++;
ax->stats.rx_over_errors++;
set_bit(AXF_ERROR, &ax->flags);
}
}
......@@ -280,72 +392,14 @@ static void ax_changedmtu(struct ax_disp *ax)
spin_unlock_bh(&ax->buflock);
if (oxbuff != NULL)
kfree(oxbuff);
if (orbuff != NULL)
kfree(orbuff);
}
/* Set the "sending" flag. This must be atomic. */
static inline void ax_lock(struct ax_disp *ax)
{
netif_stop_queue(ax->dev);
}
/* Clear the "sending" flag. This must be atomic. */
static inline void ax_unlock(struct ax_disp *ax)
{
netif_start_queue(ax->dev);
}
/* Send one completely decapsulated AX.25 packet to the AX.25 layer. */
static void ax_bump(struct ax_disp *ax)
{
struct sk_buff *skb;
int count;
spin_lock_bh(&ax->buflock);
if (ax->rbuff[0] > 0x0f) {
if (ax->rbuff[0] & 0x20) {
ax->crcmode = CRC_MODE_FLEX;
if (check_crc_flex(ax->rbuff, ax->rcount) < 0) {
ax->rx_errors++;
return;
}
ax->rcount -= 2;
/* dl9sau bugfix: the trailling two bytes flexnet crc
* will not be passed to the kernel. thus we have
* to correct the kissparm signature, because it
* indicates a crc but there's none
*/
*ax->rbuff &= ~0x20;
}
}
spin_unlock_bh(&ax->buflock);
count = ax->rcount;
if ((skb = dev_alloc_skb(count)) == NULL) {
printk(KERN_ERR "mkiss: %s: memory squeeze, dropping packet.\n", ax->dev->name);
ax->rx_dropped++;
return;
}
spin_lock_bh(&ax->buflock);
memcpy(skb_put(skb,count), ax->rbuff, count);
spin_unlock_bh(&ax->buflock);
skb->protocol = ax25_type_trans(skb, ax->dev);
netif_rx(skb);
ax->dev->last_rx = jiffies;
ax->rx_packets++;
ax->rx_bytes+=count;
}
/* Encapsulate one AX.25 packet and stuff into a TTY queue. */
static void ax_encaps(struct ax_disp *ax, unsigned char *icp, int len)
static void ax_encaps(struct net_device *dev, unsigned char *icp, int len)
{
struct mkiss *ax = netdev_priv(dev);
unsigned char *p;
int actual, count;
......@@ -355,8 +409,8 @@ static void ax_encaps(struct ax_disp *ax, unsigned char *icp, int len)
if (len > ax->mtu) { /* Sigh, shouldn't occur BUT ... */
len = ax->mtu;
printk(KERN_ERR "mkiss: %s: truncating oversized transmit packet!\n", ax->dev->name);
ax->tx_dropped++;
ax_unlock(ax);
ax->stats.tx_dropped++;
netif_start_queue(dev);
return;
}
......@@ -377,10 +431,11 @@ static void ax_encaps(struct ax_disp *ax, unsigned char *icp, int len)
break;
}
ax->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
set_bit(TTY_DO_WRITE_WAKEUP, &ax->tty->flags);
actual = ax->tty->driver->write(ax->tty, ax->xbuff, count);
ax->tx_packets++;
ax->tx_bytes+=actual;
ax->stats.tx_packets++;
ax->stats.tx_bytes += actual;
ax->dev->trans_start = jiffies;
ax->xleft = count - actual;
ax->xhead = ax->xbuff + actual;
......@@ -388,37 +443,10 @@ static void ax_encaps(struct ax_disp *ax, unsigned char *icp, int len)
spin_unlock_bh(&ax->buflock);
}
/*
* Called by the driver when there's room for more data. If we have
* more packets to send, we send them here.
*/
static void ax25_write_wakeup(struct tty_struct *tty)
{
int actual;
struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
/* First make sure we're connected. */
if (ax == NULL || ax->magic != AX25_MAGIC || !netif_running(ax->dev))
return;
if (ax->xleft <= 0) {
/* Now serial buffer is almost free & we can start
* transmission of another packet
*/
tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
netif_wake_queue(ax->dev);
return;
}
actual = tty->driver->write(tty, ax->xhead, ax->xleft);
ax->xleft -= actual;
ax->xhead += actual;
}
/* Encapsulate an AX.25 packet and kick it into a TTY queue. */
static int ax_xmit(struct sk_buff *skb, struct net_device *dev)
{
struct ax_disp *ax = netdev_priv(dev);
struct mkiss *ax = netdev_priv(dev);
if (!netif_running(dev)) {
printk(KERN_ERR "mkiss: %s: xmit call when iface is down\n", dev->name);
......@@ -440,20 +468,30 @@ static int ax_xmit(struct sk_buff *skb, struct net_device *dev)
"bad line quality" : "driver error");
ax->xleft = 0;
ax->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
ax_unlock(ax);
clear_bit(TTY_DO_WRITE_WAKEUP, &ax->tty->flags);
netif_start_queue(dev);
}
/* We were not busy, so we are now... :-) */
if (skb != NULL) {
ax_lock(ax);
ax_encaps(ax, skb->data, skb->len);
netif_stop_queue(dev);
ax_encaps(dev, skb->data, skb->len);
kfree_skb(skb);
}
return 0;
}
static int ax_open_dev(struct net_device *dev)
{
struct mkiss *ax = netdev_priv(dev);
if (ax->tty == NULL)
return -ENODEV;
return 0;
}
#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
/* Return the frame type ID */
......@@ -482,7 +520,7 @@ static int ax_rebuild_header(struct sk_buff *skb)
/* Open the low-level part of the AX25 channel. Easy! */
static int ax_open(struct net_device *dev)
{
struct ax_disp *ax = netdev_priv(dev);
struct mkiss *ax = netdev_priv(dev);
unsigned long len;
if (ax->tty == NULL)
......@@ -519,7 +557,6 @@ static int ax_open(struct net_device *dev)
spin_lock_init(&ax->buflock);
netif_start_queue(dev);
return 0;
noxbuff:
......@@ -533,68 +570,100 @@ static int ax_open(struct net_device *dev)
/* Close the low-level part of the AX25 channel. Easy! */
static int ax_close(struct net_device *dev)
{
struct ax_disp *ax = netdev_priv(dev);
if (ax->tty == NULL)
return -EBUSY;
struct mkiss *ax = netdev_priv(dev);
ax->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
if (ax->tty)
clear_bit(TTY_DO_WRITE_WAKEUP, &ax->tty->flags);
netif_stop_queue(dev);
return 0;
}
static int ax25_receive_room(struct tty_struct *tty)
static struct net_device_stats *ax_get_stats(struct net_device *dev)
{
return 65536; /* We can handle an infinite amount of data. :-) */
struct mkiss *ax = netdev_priv(dev);
return &ax->stats;
}
/*
* Handle the 'receiver data ready' interrupt.
* This function is called by the 'tty_io' module in the kernel when
* a block of data has been received, which can now be decapsulated
* and sent on to the AX.25 layer for further processing.
*/
static void ax25_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
static void ax_setup(struct net_device *dev)
{
struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
if (ax == NULL || ax->magic != AX25_MAGIC || !netif_running(ax->dev))
return;
static char ax25_bcast[AX25_ADDR_LEN] =
{'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
static char ax25_test[AX25_ADDR_LEN] =
{'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
/*
* Argh! mtu change time! - costs us the packet part received
* at the change
/* Finish setting up the DEVICE info. */
dev->mtu = AX_MTU;
dev->hard_start_xmit = ax_xmit;
dev->open = ax_open_dev;
dev->stop = ax_close;
dev->get_stats = ax_get_stats;
dev->set_mac_address = ax_set_mac_address;
dev->hard_header_len = 0;
dev->addr_len = 0;
dev->type = ARPHRD_AX25;
dev->tx_queue_len = 10;
dev->hard_header = ax_header;
dev->rebuild_header = ax_rebuild_header;
memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
memcpy(dev->dev_addr, ax25_test, AX25_ADDR_LEN);
dev->flags = IFF_BROADCAST | IFF_MULTICAST;
}
/*
* We have a potential race on dereferencing tty->disc_data, because the tty
* layer provides no locking at all - thus one cpu could be running
* sixpack_receive_buf while another calls sixpack_close, which zeroes
* tty->disc_data and frees the memory that sixpack_receive_buf is using. The
* best way to fix this is to use a rwlock in the tty struct, but for now we
* use a single global rwlock for all ttys in ppp line discipline.
*/
if (ax->mtu != ax->dev->mtu + 73)
ax_changedmtu(ax);
static rwlock_t disc_data_lock = RW_LOCK_UNLOCKED;
/* Read the characters out of the buffer */
while (count--) {
if (fp != NULL && *fp++) {
if (!test_and_set_bit(AXF_ERROR, &ax->flags))
ax->rx_errors++;
cp++;
continue;
}
static struct mkiss *mkiss_get(struct tty_struct *tty)
{
struct mkiss *ax;
kiss_unesc(ax, *cp++);
}
read_lock(&disc_data_lock);
ax = tty->disc_data;
if (ax)
atomic_inc(&ax->refcnt);
read_unlock(&disc_data_lock);
return ax;
}
static int ax25_open(struct tty_struct *tty)
static void mkiss_put(struct mkiss *ax)
{
struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
if (atomic_dec_and_test(&ax->refcnt))
up(&ax->dead_sem);
}
static int mkiss_open(struct tty_struct *tty)
{
struct net_device *dev;
struct mkiss *ax;
int err;
/* First make sure we're not already connected. */
if (ax && ax->magic == AX25_MAGIC)
return -EEXIST;
if (!capable(CAP_NET_ADMIN))
return -EPERM;
/* OK. Find a free AX25 channel to use. */
if ((ax = ax_alloc()) == NULL)
return -ENFILE;
dev = alloc_netdev(sizeof(struct mkiss), "ax%d", ax_setup);
if (!dev) {
err = -ENOMEM;
goto out;
}
ax = netdev_priv(dev);
ax->dev = dev;
spin_lock_init(&ax->buflock);
atomic_set(&ax->refcnt, 1);
init_MUTEX_LOCKED(&ax->dead_sem);
ax->tty = tty;
tty->disc_data = ax;
......@@ -603,283 +672,212 @@ static int ax25_open(struct tty_struct *tty)
tty->driver->flush_buffer(tty);
/* Restore default settings */
ax->dev->type = ARPHRD_AX25;
dev->type = ARPHRD_AX25;
/* Perform the low-level AX25 initialization. */
if ((err = ax_open(ax->dev)))
return err;
if ((err = ax_open(ax->dev))) {
goto out_free_netdev;
}
/* Done. We have linked the TTY line to a channel. */
return ax->dev->base_addr;
}
if (register_netdev(dev))
goto out_free_buffers;
static void ax25_close(struct tty_struct *tty)
{
struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
netif_start_queue(dev);
/* First make sure we're connected. */
if (ax == NULL || ax->magic != AX25_MAGIC)
return;
/* Done. We have linked the TTY line to a channel. */
return 0;
unregister_netdev(ax->dev);
out_free_buffers:
kfree(ax->rbuff);
kfree(ax->xbuff);
tty->disc_data = NULL;
ax->tty = NULL;
out_free_netdev:
free_netdev(dev);
ax_free(ax);
out:
return err;
}
static struct net_device_stats *ax_get_stats(struct net_device *dev)
static void mkiss_close(struct tty_struct *tty)
{
static struct net_device_stats stats;
struct ax_disp *ax = netdev_priv(dev);
memset(&stats, 0, sizeof(struct net_device_stats));
stats.rx_packets = ax->rx_packets;
stats.tx_packets = ax->tx_packets;
stats.rx_bytes = ax->rx_bytes;
stats.tx_bytes = ax->tx_bytes;
stats.rx_dropped = ax->rx_dropped;
stats.tx_dropped = ax->tx_dropped;
stats.tx_errors = ax->tx_errors;
stats.rx_errors = ax->rx_errors;
stats.rx_over_errors = ax->rx_over_errors;
return &stats;
}
struct mkiss *ax;
write_lock(&disc_data_lock);
ax = tty->disc_data;
tty->disc_data = NULL;
write_unlock(&disc_data_lock);
/************************************************************************
* STANDARD ENCAPSULATION *
************************************************************************/
static int kiss_esc(unsigned char *s, unsigned char *d, int len)
{
unsigned char *ptr = d;
unsigned char c;
if (ax == 0)
return;
/*
* Send an initial END character to flush out any
* data that may have accumulated in the receiver
* due to line noise.
* We have now ensured that nobody can start using ap from now on, but
* we have to wait for all existing users to finish.
*/
if (!atomic_dec_and_test(&ax->refcnt))
down(&ax->dead_sem);
*ptr++ = END;
while (len-- > 0) {
switch (c = *s++) {
case END:
*ptr++ = ESC;
*ptr++ = ESC_END;
break;
case ESC:
*ptr++ = ESC;
*ptr++ = ESC_ESC;
break;
default:
*ptr++ = c;
break;
}
}
unregister_netdev(ax->dev);
*ptr++ = END;
/* Free all AX25 frame buffers. */
kfree(ax->rbuff);
kfree(ax->xbuff);
return ptr - d;
ax->tty = NULL;
}
/*
* MW:
* OK its ugly, but tell me a better solution without copying the
* packet to a temporary buffer :-)
*/
static int kiss_esc_crc(unsigned char *s, unsigned char *d, unsigned short crc, int len)
/* Perform I/O control on an active ax25 channel. */
static int mkiss_ioctl(struct tty_struct *tty, struct file *file,
unsigned int cmd, unsigned long arg)
{
unsigned char *ptr = d;
unsigned char c=0;
*ptr++ = END;
while (len > 0) {
if (len > 2)
c = *s++;
else if (len > 1)
c = crc >> 8;
else if (len > 0)
c = crc & 0xff;
struct mkiss *ax = mkiss_get(tty);
struct net_device *dev = ax->dev;
unsigned int tmp, err;
len--;
/* First make sure we're connected. */
if (ax == NULL)
return -ENXIO;
switch (c) {
case END:
*ptr++ = ESC;
*ptr++ = ESC_END;
switch (cmd) {
case SIOCGIFNAME:
err = copy_to_user((void __user *) arg, ax->dev->name,
strlen(ax->dev->name) + 1) ? -EFAULT : 0;
break;
case ESC:
*ptr++ = ESC;
*ptr++ = ESC_ESC;
case SIOCGIFENCAP:
err = put_user(4, (int __user *) arg);
break;
default:
*ptr++ = c;
case SIOCSIFENCAP:
if (get_user(tmp, (int __user *) arg)) {
err = -EFAULT;
break;
}
}
*ptr++ = END;
return ptr - d;
}
static void kiss_unesc(struct ax_disp *ax, unsigned char s)
{
switch (s) {
case END:
/* drop keeptest bit = VSV */
if (test_bit(AXF_KEEPTEST, &ax->flags))
clear_bit(AXF_KEEPTEST, &ax->flags);
ax->mode = tmp;
dev->addr_len = AX25_ADDR_LEN;
dev->hard_header_len = AX25_KISS_HEADER_LEN +
AX25_MAX_HEADER_LEN + 3;
dev->type = ARPHRD_AX25;
if (!test_and_clear_bit(AXF_ERROR, &ax->flags) && (ax->rcount > 2))
ax_bump(ax);
err = 0;
break;
clear_bit(AXF_ESCAPE, &ax->flags);
ax->rcount = 0;
return;
case SIOCSIFHWADDR: {
char addr[AX25_ADDR_LEN];
printk(KERN_INFO "In SIOCSIFHWADDR");
case ESC:
set_bit(AXF_ESCAPE, &ax->flags);
return;
case ESC_ESC:
if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
s = ESC;
break;
case ESC_END:
if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
s = END;
if (copy_from_user(&addr,
(void __user *) arg, AX25_ADDR_LEN)) {
err = -EFAULT;
break;
}
spin_lock_bh(&ax->buflock);
if (!test_bit(AXF_ERROR, &ax->flags)) {
if (ax->rcount < ax->buffsize) {
ax->rbuff[ax->rcount++] = s;
spin_unlock_bh(&ax->buflock);
return;
}
spin_lock_irq(&dev->xmit_lock);
memcpy(dev->dev_addr, addr, AX25_ADDR_LEN);
spin_unlock_irq(&dev->xmit_lock);
ax->rx_over_errors++;
set_bit(AXF_ERROR, &ax->flags);
err = 0;
break;
}
default:
err = -ENOIOCTLCMD;
}
spin_unlock_bh(&ax->buflock);
}
static int ax_set_mac_address(struct net_device *dev, void __user *addr)
{
if (copy_from_user(dev->dev_addr, addr, AX25_ADDR_LEN))
return -EFAULT;
return 0;
}
static int ax_set_dev_mac_address(struct net_device *dev, void *addr)
{
struct sockaddr *sa = addr;
memcpy(dev->dev_addr, sa->sa_data, AX25_ADDR_LEN);
mkiss_put(ax);
return 0;
return err;
}
/* Perform I/O control on an active ax25 channel. */
static int ax25_disp_ioctl(struct tty_struct *tty, void *file, int cmd, void __user *arg)
/*
* Handle the 'receiver data ready' interrupt.
* This function is called by the 'tty_io' module in the kernel when
* a block of data has been received, which can now be decapsulated
* and sent on to the AX.25 layer for further processing.
*/
static void mkiss_receive_buf(struct tty_struct *tty, const unsigned char *cp,
char *fp, int count)
{
struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
unsigned int tmp;
/* First make sure we're connected. */
if (ax == NULL || ax->magic != AX25_MAGIC)
return -EINVAL;
switch (cmd) {
case SIOCGIFNAME:
if (copy_to_user(arg, ax->dev->name, strlen(ax->dev->name) + 1))
return -EFAULT;
return 0;
struct mkiss *ax = mkiss_get(tty);
case SIOCGIFENCAP:
return put_user(4, (int __user *)arg);
if (!ax)
return;
case SIOCSIFENCAP:
if (get_user(tmp, (int __user *)arg))
return -EFAULT;
ax->mode = tmp;
ax->dev->addr_len = AX25_ADDR_LEN; /* sizeof an AX.25 addr */
ax->dev->hard_header_len = AX25_KISS_HEADER_LEN + AX25_MAX_HEADER_LEN + 3;
ax->dev->type = ARPHRD_AX25;
return 0;
/*
* Argh! mtu change time! - costs us the packet part received
* at the change
*/
if (ax->mtu != ax->dev->mtu + 73)
ax_changedmtu(ax);
case SIOCSIFHWADDR:
return ax_set_mac_address(ax->dev, arg);
/* Read the characters out of the buffer */
while (count--) {
if (fp != NULL && *fp++) {
if (!test_and_set_bit(AXF_ERROR, &ax->flags))
ax->stats.rx_errors++;
cp++;
continue;
}
default:
return -ENOIOCTLCMD;
kiss_unesc(ax, *cp++);
}
mkiss_put(ax);
if (test_and_clear_bit(TTY_THROTTLED, &tty->flags)
&& tty->driver->unthrottle)
tty->driver->unthrottle(tty);
}
static int ax_open_dev(struct net_device *dev)
static int mkiss_receive_room(struct tty_struct *tty)
{
struct ax_disp *ax = netdev_priv(dev);
if (ax->tty == NULL)
return -ENODEV;
return 0;
return 65536; /* We can handle an infinite amount of data. :-) */
}
/* Initialize the driver. Called by network startup. */
static int ax25_init(struct net_device *dev)
/*
* Called by the driver when there's room for more data. If we have
* more packets to send, we send them here.
*/
static void mkiss_write_wakeup(struct tty_struct *tty)
{
struct ax_disp *ax = netdev_priv(dev);
static char ax25_bcast[AX25_ADDR_LEN] =
{'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
static char ax25_test[AX25_ADDR_LEN] =
{'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
if (ax == NULL) /* Allocation failed ?? */
return -ENODEV;
struct mkiss *ax = mkiss_get(tty);
int actual;
/* Set up the "AX25 Control Block". (And clear statistics) */
memset(ax, 0, sizeof (struct ax_disp));
ax->magic = AX25_MAGIC;
ax->dev = dev;
if (!ax)
return;
/* Finish setting up the DEVICE info. */
dev->mtu = AX_MTU;
dev->hard_start_xmit = ax_xmit;
dev->open = ax_open_dev;
dev->stop = ax_close;
dev->get_stats = ax_get_stats;
dev->set_mac_address = ax_set_dev_mac_address;
dev->hard_header_len = 0;
dev->addr_len = 0;
dev->type = ARPHRD_AX25;
dev->tx_queue_len = 10;
dev->hard_header = ax_header;
dev->rebuild_header = ax_rebuild_header;
if (ax->xleft <= 0) {
/* Now serial buffer is almost free & we can start
* transmission of another packet
*/
clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
memcpy(dev->dev_addr, ax25_test, AX25_ADDR_LEN);
netif_wake_queue(ax->dev);
goto out;
}
/* New-style flags. */
dev->flags = IFF_BROADCAST | IFF_MULTICAST;
actual = tty->driver->write(tty, ax->xhead, ax->xleft);
ax->xleft -= actual;
ax->xhead += actual;
return 0;
out:
mkiss_put(ax);
}
static struct tty_ldisc ax_ldisc = {
.magic = TTY_LDISC_MAGIC,
.name = "mkiss",
.open = mkiss_open,
.close = mkiss_close,
.ioctl = mkiss_ioctl,
.receive_buf = mkiss_receive_buf,
.receive_room = mkiss_receive_room,
.write_wakeup = mkiss_write_wakeup
};
/* ******************************************************************** */
/* * Init MKISS driver * */
/* ******************************************************************** */
static char banner[] __initdata = KERN_INFO \
"mkiss: AX.25 Multikiss, Hans Albas PE1AYX\n";
static char msg_regfail[] __initdata = KERN_ERR \
"mkiss: can't register line discipline (err = %d)\n";
static int __init mkiss_init_driver(void)
{
......@@ -887,64 +885,27 @@ static int __init mkiss_init_driver(void)
printk(banner);
if (ax25_maxdev < 4)
ax25_maxdev = 4; /* Sanity */
if ((status = tty_register_ldisc(N_AX25, &ax_ldisc)) != 0)
printk(msg_regfail);
if ((ax25_ctrls = kmalloc(sizeof(void *) * ax25_maxdev, GFP_KERNEL)) == NULL) {
printk(KERN_ERR "mkiss: Can't allocate ax25_ctrls[] array!\n");
return -ENOMEM;
}
/* Clear the pointer array, we allocate devices when we need them */
memset(ax25_ctrls, 0, sizeof(void*) * ax25_maxdev); /* Pointers */
/* Fill in our line protocol discipline, and register it */
ax_ldisc.magic = TTY_LDISC_MAGIC;
ax_ldisc.name = "mkiss";
ax_ldisc.open = ax25_open;
ax_ldisc.close = ax25_close;
ax_ldisc.ioctl = (int (*)(struct tty_struct *, struct file *,
unsigned int, unsigned long))ax25_disp_ioctl;
ax_ldisc.receive_buf = ax25_receive_buf;
ax_ldisc.receive_room = ax25_receive_room;
ax_ldisc.write_wakeup = ax25_write_wakeup;
if ((status = tty_register_ldisc(N_AX25, &ax_ldisc)) != 0) {
printk(KERN_ERR "mkiss: can't register line discipline (err = %d)\n", status);
kfree(ax25_ctrls);
}
return status;
}
static const char msg_unregfail[] __exitdata = KERN_ERR \
"mkiss: can't unregister line discipline (err = %d)\n";
static void __exit mkiss_exit_driver(void)
{
int i;
int ret;
for (i = 0; i < ax25_maxdev; i++) {
if (ax25_ctrls[i]) {
/*
* VSV = if dev->start==0, then device
* unregistered while close proc.
*/
if (netif_running(&ax25_ctrls[i]->dev))
unregister_netdev(&ax25_ctrls[i]->dev);
kfree(ax25_ctrls[i]);
}
}
kfree(ax25_ctrls);
ax25_ctrls = NULL;
if ((i = tty_unregister_ldisc(N_AX25)))
printk(KERN_ERR "mkiss: can't unregister line discipline (err = %d)\n", i);
if ((ret = tty_unregister_ldisc(N_AX25)))
printk(msg_unregfail, ret);
}
MODULE_AUTHOR("Hans Albas PE1AYX <hans@esrac.ele.tue.nl>");
MODULE_AUTHOR("Ralf Baechle DL5RB <ralf@linux-mips.org>");
MODULE_DESCRIPTION("KISS driver for AX.25 over TTYs");
MODULE_PARM(ax25_maxdev, "i");
MODULE_PARM_DESC(ax25_maxdev, "number of MKISS devices");
MODULE_LICENSE("GPL");
MODULE_ALIAS_LDISC(N_AX25);
module_init(mkiss_init_driver);
module_exit(mkiss_exit_driver);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment