Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *	Things to sort out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *	o	tbusy handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *	o	allow users to set the parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *	o	sync/async switching ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *	Note: This does _not_ implement CCITT X.25 asynchronous framing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *	recommendations. Its primarily for testing purposes. If you wanted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *	to do CCITT then in theory all you need is to nick the HDLC async
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *	checksum routines from ppp.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *      Changes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *	2000-10-29	Henner Eisen	lapb_data_indication() return status.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/lapb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <net/x25device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include "x25_asy.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static struct net_device **x25_asy_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static int x25_asy_maxdev = SL_NRUNIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) module_param(x25_asy_maxdev, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static int x25_asy_esc(unsigned char *p, unsigned char *d, int len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static void x25_asy_unesc(struct x25_asy *sl, unsigned char c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static void x25_asy_setup(struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) /* Find a free X.25 channel, and link in this `tty' line. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static struct x25_asy *x25_asy_alloc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct net_device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct x25_asy *sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (x25_asy_devs == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		return NULL;	/* Master array missing ! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	for (i = 0; i < x25_asy_maxdev; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		dev = x25_asy_devs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		/* Not allocated ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		if (dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		/* Not in use ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		if (!test_and_set_bit(SLF_INUSE, &sl->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			return sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	/* Sorry, too many, all slots in use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (i >= x25_asy_maxdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	/* If no channels are available, allocate one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		char name[IFNAMSIZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		sprintf(name, "x25asy%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		dev = alloc_netdev(sizeof(struct x25_asy), name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 				   NET_NAME_UNKNOWN, x25_asy_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		/* Initialize channel control data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		dev->base_addr    = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		/* register device so that it can be ifconfig'ed       */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		if (register_netdev(dev) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			/* (Re-)Set the INUSE bit.   Very Important! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			set_bit(SLF_INUSE, &sl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			x25_asy_devs[i] = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			return sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			pr_warn("%s(): register_netdev() failure\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /* Free an X.25 channel. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static void x25_asy_free(struct x25_asy *sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	/* Free all X.25 frame buffers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	kfree(sl->rbuff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	sl->rbuff = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	kfree(sl->xbuff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	sl->xbuff = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (!test_and_clear_bit(SLF_INUSE, &sl->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		netdev_err(sl->dev, "x25_asy_free for already free unit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static int x25_asy_change_mtu(struct net_device *dev, int newmtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	struct x25_asy *sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	unsigned char *xbuff, *rbuff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	len = 2 * newmtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	xbuff = kmalloc(len + 4, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	rbuff = kmalloc(len + 4, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (xbuff == NULL || rbuff == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		kfree(xbuff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		kfree(rbuff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	spin_lock_bh(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	xbuff    = xchg(&sl->xbuff, xbuff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (sl->xleft)  {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		if (sl->xleft <= len)  {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			memcpy(sl->xbuff, sl->xhead, sl->xleft);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		} else  {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			sl->xleft = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			dev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	sl->xhead = sl->xbuff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	rbuff	 = xchg(&sl->rbuff, rbuff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (sl->rcount)  {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		if (sl->rcount <= len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			memcpy(sl->rbuff, rbuff, sl->rcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		} else  {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			sl->rcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			dev->stats.rx_over_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			set_bit(SLF_ERROR, &sl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	dev->mtu    = newmtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	sl->buffsize = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	spin_unlock_bh(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	kfree(xbuff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	kfree(rbuff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* Set the "sending" flag.  This must be atomic, hence the ASM. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static inline void x25_asy_lock(struct x25_asy *sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	netif_stop_queue(sl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* Clear the "sending" flag.  This must be atomic, hence the ASM. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static inline void x25_asy_unlock(struct x25_asy *sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	netif_wake_queue(sl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /* Send an LAPB frame to the LAPB module to process. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static void x25_asy_bump(struct x25_asy *sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	struct net_device *dev = sl->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	count = sl->rcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	dev->stats.rx_bytes += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	skb = dev_alloc_skb(count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (skb == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		netdev_warn(sl->dev, "memory squeeze, dropping packet\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	skb_put_data(skb, sl->rbuff, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	err = lapb_data_received(sl->dev, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	if (err != LAPB_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		printk(KERN_DEBUG "x25_asy: data received err - %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		dev->stats.rx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /* Encapsulate one IP datagram and stuff into a TTY queue. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static void x25_asy_encaps(struct x25_asy *sl, unsigned char *icp, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	unsigned char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	int actual, count, mtu = sl->dev->mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (len > mtu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		/* Sigh, shouldn't occur BUT ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		len = mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		printk(KERN_DEBUG "%s: truncating oversized transmit packet!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 					sl->dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		sl->dev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		x25_asy_unlock(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	p = icp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	count = x25_asy_esc(p, sl->xbuff, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	/* Order of next two lines is *very* important.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	 * When we are sending a little amount of data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	 * the transfer may be completed inside driver.write()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	 * routine, because it's running with interrupts enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	 * In this case we *never* got WRITE_WAKEUP event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	 * if we did not request it before write operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	 *       14 Oct 1994  Dmitry Gorodchanin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	actual = sl->tty->ops->write(sl->tty, sl->xbuff, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	sl->xleft = count - actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	sl->xhead = sl->xbuff + actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)  * Called by the driver when there's room for more data.  If we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)  * more packets to send, we send them here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static void x25_asy_write_wakeup(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	int actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	struct x25_asy *sl = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	/* First make sure we're connected. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	if (!sl || sl->magic != X25_ASY_MAGIC || !netif_running(sl->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (sl->xleft <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		/* Now serial buffer is almost free & we can start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		 * transmission of another packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		sl->dev->stats.tx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		x25_asy_unlock(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	actual = tty->ops->write(tty, sl->xhead, sl->xleft);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	sl->xleft -= actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	sl->xhead += actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static void x25_asy_timeout(struct net_device *dev, unsigned int txqueue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	struct x25_asy *sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	spin_lock(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (netif_queue_stopped(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		/* May be we must check transmitter timeout here ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		 *      14 Oct 1994 Dmitry Gorodchanin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		netdev_warn(dev, "transmit timed out, %s?\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			    (tty_chars_in_buffer(sl->tty) || sl->xleft) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			    "bad line quality" : "driver error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		sl->xleft = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		x25_asy_unlock(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	spin_unlock(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /* Encapsulate an IP datagram and kick it into a TTY queue. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static netdev_tx_t x25_asy_xmit(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 				      struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	struct x25_asy *sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (!netif_running(sl->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		netdev_err(dev, "xmit call when iface is down\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	/* There should be a pseudo header of 1 byte added by upper layers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	 * Check to make sure it is there before reading it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (skb->len < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	switch (skb->data[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	case X25_IFACE_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	case X25_IFACE_CONNECT: /* Connection request .. do nothing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		err = lapb_connect_request(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		if (err != LAPB_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			netdev_err(dev, "lapb_connect_request error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 				   err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	case X25_IFACE_DISCONNECT: /* do nothing - hang up ?? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		err = lapb_disconnect_request(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		if (err != LAPB_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			netdev_err(dev, "lapb_disconnect_request error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 				   err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	skb_pull(skb, 1);	/* Remove control byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	 * If we are busy already- too bad.  We ought to be able
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	 * to queue things at this point, to allow for a little
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	 * frame buffer.  Oh well...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	 * -----------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	 * I hate queues in X.25 driver. May be it's efficient,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	 * but for me latency is more important. ;)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	 * So, no queues !
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	 *        14 Oct 1994  Dmitry Gorodchanin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	err = lapb_data_request(dev, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (err != LAPB_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		netdev_err(dev, "lapb_data_request error: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)  *	LAPB interface boilerplate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)  *	Called when I frame data arrive. We add a pseudo header for upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)  *	layers and pass it to upper layers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static int x25_asy_data_indication(struct net_device *dev, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	if (skb_cow(skb, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	skb_push(skb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	skb->data[0] = X25_IFACE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	skb->protocol = x25_type_trans(skb, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	return netif_rx(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)  *	Data has emerged from the LAPB protocol machine. We don't handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)  *	busy cases too well. Its tricky to see how to do this nicely -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)  *	perhaps lapb should allow us to bounce this ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static void x25_asy_data_transmit(struct net_device *dev, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	struct x25_asy *sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	spin_lock(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	if (netif_queue_stopped(sl->dev) || sl->tty == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		spin_unlock(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		netdev_err(dev, "tbusy drop\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	/* We were not busy, so we are now... :-) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	if (skb != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		x25_asy_lock(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		dev->stats.tx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		x25_asy_encaps(sl, skb->data, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		dev_kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	spin_unlock(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)  *	LAPB connection establish/down information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) static void x25_asy_connected(struct net_device *dev, int reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	struct x25_asy *sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	unsigned char *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	skb = dev_alloc_skb(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	if (skb == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		netdev_err(dev, "out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	ptr  = skb_put(skb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	*ptr = X25_IFACE_CONNECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	skb->protocol = x25_type_trans(skb, sl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	netif_rx(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) static void x25_asy_disconnected(struct net_device *dev, int reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	struct x25_asy *sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	unsigned char *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	skb = dev_alloc_skb(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (skb == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		netdev_err(dev, "out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	ptr  = skb_put(skb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	*ptr = X25_IFACE_DISCONNECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	skb->protocol = x25_type_trans(skb, sl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	netif_rx(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) static const struct lapb_register_struct x25_asy_callbacks = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	.connect_confirmation = x25_asy_connected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	.connect_indication = x25_asy_connected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	.disconnect_confirmation = x25_asy_disconnected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	.disconnect_indication = x25_asy_disconnected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	.data_indication = x25_asy_data_indication,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	.data_transmit = x25_asy_data_transmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /* Open the low-level part of the X.25 channel. Easy! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) static int x25_asy_open(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	struct x25_asy *sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	unsigned long len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	if (sl->tty == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	 * Allocate the X.25 frame buffers:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	 * rbuff	Receive buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	 * xbuff	Transmit buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	len = dev->mtu * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	sl->rbuff = kmalloc(len + 4, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	if (sl->rbuff == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		goto norbuff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	sl->xbuff = kmalloc(len + 4, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	if (sl->xbuff == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		goto noxbuff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	sl->buffsize = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	sl->rcount   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	sl->xleft    = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	sl->flags   &= (1 << SLF_INUSE);      /* Clear ESCAPE & ERROR flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	/* Cleanup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	kfree(sl->xbuff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	sl->xbuff = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) noxbuff:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	kfree(sl->rbuff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	sl->rbuff = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) norbuff:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) /* Close the low-level part of the X.25 channel. Easy! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) static int x25_asy_close(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	struct x25_asy *sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	spin_lock(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	if (sl->tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	sl->rcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	sl->xleft  = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	spin_unlock(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)  * Handle the 'receiver data ready' interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)  * This function is called by the 'tty_io' module in the kernel when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)  * a block of X.25 data has been received, which can now be decapsulated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)  * and sent on to some IP layer for further processing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static void x25_asy_receive_buf(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 				const unsigned char *cp, char *fp, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	struct x25_asy *sl = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	if (!sl || sl->magic != X25_ASY_MAGIC || !netif_running(sl->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	/* Read the characters out of the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	while (count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		if (fp && *fp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 			if (!test_and_set_bit(SLF_ERROR, &sl->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 				sl->dev->stats.rx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 			cp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		x25_asy_unesc(sl, *cp++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)  * Open the high-level part of the X.25 channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)  * This function is called by the TTY module when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)  * X.25 line discipline is called for.  Because we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)  * sure the tty line exists, we only have to link it to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)  * a free X.25 channel...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) static int x25_asy_open_tty(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	struct x25_asy *sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	if (tty->ops->write == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	/* OK.  Find a free X.25 channel to use. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	sl = x25_asy_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	if (sl == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		return -ENFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	sl->tty = tty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	tty->disc_data = sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	tty->receive_room = 65536;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	tty_driver_flush_buffer(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	tty_ldisc_flush(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	/* Restore default settings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	sl->dev->type = ARPHRD_X25;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	/* Perform the low-level X.25 async init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	err = x25_asy_open(sl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		x25_asy_free(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	/* Done.  We have linked the TTY line to a channel. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)  * Close down an X.25 channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)  * This means flushing out any pending queues, and then restoring the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)  * TTY line discipline to what it was before it got hooked to X.25
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)  * (which usually is TTY again).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) static void x25_asy_close_tty(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	struct x25_asy *sl = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	/* First make sure we're connected. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	if (!sl || sl->magic != X25_ASY_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	if (sl->dev->flags & IFF_UP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 		dev_close(sl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	tty->disc_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	sl->tty = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	x25_asy_free(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)  /************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)   *			STANDARD X.25 ENCAPSULATION		  	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)   ************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) static int x25_asy_esc(unsigned char *s, unsigned char *d, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	unsigned char *ptr = d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	unsigned char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	 * Send an initial END character to flush out any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	 * data that may have accumulated in the receiver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	 * due to line noise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	*ptr++ = X25_END;	/* Send 10111110 bit seq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	 * For each byte in the packet, send the appropriate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	 * character sequence, according to the X.25 protocol.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	while (len-- > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 		switch (c = *s++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		case X25_END:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 			*ptr++ = X25_ESC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 			*ptr++ = X25_ESCAPE(X25_END);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 		case X25_ESC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 			*ptr++ = X25_ESC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 			*ptr++ = X25_ESCAPE(X25_ESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 			*ptr++ = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	*ptr++ = X25_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	return ptr - d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) static void x25_asy_unesc(struct x25_asy *sl, unsigned char s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	switch (s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	case X25_END:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 		if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 		    sl->rcount >= 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 			x25_asy_bump(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		clear_bit(SLF_ESCAPE, &sl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		sl->rcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	case X25_ESC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		set_bit(SLF_ESCAPE, &sl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	case X25_ESCAPE(X25_ESC):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	case X25_ESCAPE(X25_END):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 			s = X25_UNESCAPE(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	if (!test_bit(SLF_ERROR, &sl->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 		if (sl->rcount < sl->buffsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 			sl->rbuff[sl->rcount++] = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		sl->dev->stats.rx_over_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 		set_bit(SLF_ERROR, &sl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) /* Perform I/O control on an active X.25 channel. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) static int x25_asy_ioctl(struct tty_struct *tty, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 			 unsigned int cmd,  unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	struct x25_asy *sl = tty->disc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	/* First make sure we're connected. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	if (!sl || sl->magic != X25_ASY_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	case SIOCGIFNAME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 		if (copy_to_user((void __user *)arg, sl->dev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 					strlen(sl->dev->name) + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	case SIOCSIFHWADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 		return tty_mode_ioctl(tty, file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) static int x25_asy_open_dev(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	struct x25_asy *sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	if (sl->tty == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	err = lapb_register(dev, &x25_asy_callbacks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	if (err != LAPB_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	netif_start_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) static int x25_asy_close_dev(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	netif_stop_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	err = lapb_unregister(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	if (err != LAPB_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 		pr_err("%s: lapb_unregister error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		       __func__, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	x25_asy_close(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) static const struct net_device_ops x25_asy_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	.ndo_open	= x25_asy_open_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	.ndo_stop	= x25_asy_close_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	.ndo_start_xmit	= x25_asy_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	.ndo_tx_timeout	= x25_asy_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	.ndo_change_mtu	= x25_asy_change_mtu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) /* Initialise the X.25 driver.  Called by the device init code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) static void x25_asy_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	struct x25_asy *sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	sl->magic  = X25_ASY_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	sl->dev	   = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	spin_lock_init(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	set_bit(SLF_INUSE, &sl->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	 *	Finish setting up the DEVICE info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	dev->mtu		= SL_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	dev->min_mtu		= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	dev->max_mtu		= 65534;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	dev->netdev_ops		= &x25_asy_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	dev->watchdog_timeo	= HZ*20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	dev->hard_header_len	= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	dev->addr_len		= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	dev->type		= ARPHRD_X25;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 	dev->tx_queue_len	= 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	/* When transmitting data:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	 * first this driver removes a pseudo header of 1 byte,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	 * then the lapb module prepends an LAPB header of at most 3 bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 	dev->needed_headroom	= 3 - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	/* New-style flags. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 	dev->flags		= IFF_NOARP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) static struct tty_ldisc_ops x25_ldisc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	.magic		= TTY_LDISC_MAGIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 	.name		= "X.25",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	.open		= x25_asy_open_tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	.close		= x25_asy_close_tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 	.ioctl		= x25_asy_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	.receive_buf	= x25_asy_receive_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	.write_wakeup	= x25_asy_write_wakeup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) static int __init init_x25_asy(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	if (x25_asy_maxdev < 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 		x25_asy_maxdev = 4; /* Sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	pr_info("X.25 async: version 0.00 ALPHA (dynamic channels, max=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 		x25_asy_maxdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	x25_asy_devs = kcalloc(x25_asy_maxdev, sizeof(struct net_device *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 				GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	if (!x25_asy_devs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 	return tty_register_ldisc(N_X25, &x25_ldisc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) static void __exit exit_x25_asy(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 	for (i = 0; i < x25_asy_maxdev; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 		dev = x25_asy_devs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 		if (dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 			struct x25_asy *sl = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 			spin_lock_bh(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 			if (sl->tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 				tty_hangup(sl->tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 			spin_unlock_bh(&sl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 			 * VSV = if dev->start==0, then device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 			 * unregistered while close proc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 			unregister_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 			free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 	kfree(x25_asy_devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 	tty_unregister_ldisc(N_X25);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) module_init(init_x25_asy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) module_exit(exit_x25_asy);