17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
22 #include <linux/bitops.h>
23 #include <linux/string.h>
27 #include <linux/tty.h>
28 #include <linux/errno.h>
29 #include <linux/netdevice.h>
32 #include <linux/if_arp.h>
35 #include <linux/rtnetlink.h>
37 #include <linux/slab.h>
47 static int x25_asy_esc(
unsigned char *
p,
unsigned char *
d,
int len);
48 static void x25_asy_unesc(
struct x25_asy *
sl,
unsigned char c);
52 static struct x25_asy *x25_asy_alloc(
void)
58 if (x25_asy_devs ==
NULL)
61 for (i = 0; i < x25_asy_maxdev; i++) {
62 dev = x25_asy_devs[
i];
68 sl = netdev_priv(dev);
76 if (i >= x25_asy_maxdev)
90 sl = netdev_priv(dev);
97 x25_asy_devs[
i] =
dev;
100 pr_warn(
"%s(): register_netdev() failure\n", __func__);
109 static void x25_asy_free(
struct x25_asy *sl)
118 netdev_err(sl->
dev,
"x25_asy_free for already free unit\n");
121 static int x25_asy_change_mtu(
struct net_device *dev,
int newmtu)
123 struct x25_asy *sl = netdev_priv(dev);
125 int len = 2 * newmtu;
130 if (xbuff ==
NULL || rbuff ==
NULL) {
131 netdev_warn(dev,
"unable to grow X.25 buffers, MTU change cancelled\n");
137 spin_lock_bh(&sl->
lock);
140 if (sl->
xleft <= len) {
144 dev->
stats.tx_dropped++;
155 dev->
stats.rx_over_errors++;
163 spin_unlock_bh(&sl->
lock);
173 static inline void x25_asy_lock(
struct x25_asy *sl)
175 netif_stop_queue(sl->
dev);
181 static inline void x25_asy_unlock(
struct x25_asy *sl)
183 netif_wake_queue(sl->
dev);
188 static void x25_asy_bump(
struct x25_asy *sl)
198 skb = dev_alloc_skb(count+1);
200 netdev_warn(sl->
dev,
"memory squeeze, dropping packet\n");
201 dev->
stats.rx_dropped++;
213 dev->
stats.rx_packets++;
218 static void x25_asy_encaps(
struct x25_asy *sl,
unsigned char *icp,
int len)
221 int actual,
count, mtu = sl->
dev->mtu;
228 sl->
dev->stats.tx_dropped++;
234 count = x25_asy_esc(p, sl->
xbuff, len);
245 actual = sl->
tty->ops->write(sl->
tty, sl->
xbuff, count);
246 sl->
xleft = count - actual;
256 static void x25_asy_write_wakeup(
struct tty_struct *tty)
265 if (sl->
xleft <= 0) {
268 sl->
dev->stats.tx_packets++;
279 static void x25_asy_timeout(
struct net_device *dev)
281 struct x25_asy *sl = netdev_priv(dev);
283 spin_lock(&sl->
lock);
284 if (netif_queue_stopped(dev)) {
288 netdev_warn(dev,
"transmit timed out, %s?\n",
290 "bad line quality" :
"driver error");
295 spin_unlock(&sl->
lock);
303 struct x25_asy *sl = netdev_priv(dev);
306 if (!netif_running(sl->
dev)) {
307 netdev_err(dev,
"xmit call when iface is down\n");
312 switch (skb->
data[0]) {
318 netdev_err(dev,
"lapb_connect_request error: %d\n",
325 netdev_err(dev,
"lapb_disconnect_request error: %d\n",
345 netdev_err(dev,
"lapb_data_request error: %d\n", err);
375 struct x25_asy *sl = netdev_priv(dev);
377 spin_lock(&sl->
lock);
378 if (netif_queue_stopped(sl->
dev) || sl->
tty ==
NULL) {
379 spin_unlock(&sl->
lock);
380 netdev_err(dev,
"tbusy drop\n");
388 x25_asy_encaps(sl, skb->
data, skb->
len);
391 spin_unlock(&sl->
lock);
400 struct x25_asy *sl = netdev_priv(dev);
404 skb = dev_alloc_skb(1);
406 netdev_err(dev,
"out of memory\n");
417 static void x25_asy_disconnected(
struct net_device *dev,
int reason)
419 struct x25_asy *sl = netdev_priv(dev);
423 skb = dev_alloc_skb(1);
425 netdev_err(dev,
"out of memory\n");
437 .connect_confirmation = x25_asy_connected,
438 .connect_indication = x25_asy_connected,
439 .disconnect_confirmation = x25_asy_disconnected,
440 .disconnect_indication = x25_asy_disconnected,
441 .data_indication = x25_asy_data_indication,
442 .data_transmit = x25_asy_data_transmit,
447 static int x25_asy_open(
struct net_device *dev)
449 struct x25_asy *sl = netdev_priv(dev);
477 netif_start_queue(dev);
496 static int x25_asy_close(
struct net_device *dev)
498 struct x25_asy *sl = netdev_priv(dev);
500 spin_lock(&sl->
lock);
504 netif_stop_queue(dev);
507 spin_unlock(&sl->
lock);
518 static void x25_asy_receive_buf(
struct tty_struct *tty,
519 const unsigned char *
cp,
char *
fp,
int count)
531 sl->
dev->stats.rx_errors++;
535 x25_asy_unesc(sl, *cp++);
547 static int x25_asy_open_tty(
struct tty_struct *tty)
560 sl = x25_asy_alloc();
574 err = x25_asy_open(sl->
dev);
588 static void x25_asy_close_tty(
struct tty_struct *tty)
604 pr_err(
"x25_asy_close: lapb_unregister error: %d\n",
616 static int x25_asy_esc(
unsigned char *
s,
unsigned char *
d,
int len)
618 unsigned char *ptr =
d;
653 static void x25_asy_unesc(
struct x25_asy *sl,
unsigned char s)
678 sl->
dev->stats.rx_over_errors++;
686 unsigned int cmd,
unsigned long arg)
708 static long x25_asy_compat_ioctl(
struct tty_struct *tty,
struct file *file,
709 unsigned int cmd,
unsigned long arg)
714 return x25_asy_ioctl(tty, file, cmd,
715 (
unsigned long)compat_ptr(arg));
722 static int x25_asy_open_dev(
struct net_device *dev)
724 struct x25_asy *sl = netdev_priv(dev);
731 .ndo_open = x25_asy_open_dev,
732 .ndo_stop = x25_asy_close,
733 .ndo_start_xmit = x25_asy_xmit,
734 .ndo_tx_timeout = x25_asy_timeout,
735 .ndo_change_mtu = x25_asy_change_mtu,
739 static void x25_asy_setup(
struct net_device *dev)
741 struct x25_asy *sl = netdev_priv(dev);
768 .open = x25_asy_open_tty,
769 .close = x25_asy_close_tty,
770 .ioctl = x25_asy_ioctl,
772 .compat_ioctl = x25_asy_compat_ioctl,
774 .receive_buf = x25_asy_receive_buf,
775 .write_wakeup = x25_asy_write_wakeup,
778 static int __init init_x25_asy(
void)
780 if (x25_asy_maxdev < 4)
783 pr_info(
"X.25 async: version 0.00 ALPHA (dynamic channels, max=%d)\n",
786 x25_asy_devs = kcalloc(x25_asy_maxdev,
sizeof(
struct net_device *),
795 static void __exit exit_x25_asy(
void)
800 for (i = 0; i < x25_asy_maxdev; i++) {
801 dev = x25_asy_devs[
i];
803 struct x25_asy *sl = netdev_priv(dev);
805 spin_lock_bh(&sl->
lock);
809 spin_unlock_bh(&sl->
lock);