19 #include <linux/module.h>
20 #include <linux/bitops.h>
21 #include <asm/uaccess.h>
23 #include <linux/string.h>
28 #include <linux/slab.h>
29 #include <linux/tty.h>
30 #include <linux/errno.h>
31 #include <linux/netdevice.h>
34 #include <linux/rtnetlink.h>
37 #include <linux/if_arp.h>
72 #define AXF_KEEPTEST 3
79 #define CRC_MODE_NONE 0
80 #define CRC_MODE_FLEX 1
81 #define CRC_MODE_SMACK 2
82 #define CRC_MODE_FLEX_TEST 3
83 #define CRC_MODE_SMACK_TEST 4
91 static const unsigned short crc_flex_table[] = {
92 0x0f87, 0x1e0e, 0x2c95, 0x3d1c, 0x49a3, 0x582a, 0x6ab1, 0x7b38,
93 0x83cf, 0x9246, 0xa0dd, 0xb154, 0xc5eb, 0xd462, 0xe6f9, 0xf770,
94 0x1f06, 0x0e8f, 0x3c14, 0x2d9d, 0x5922, 0x48ab, 0x7a30, 0x6bb9,
95 0x934e, 0x82c7, 0xb05c, 0xa1d5, 0xd56a, 0xc4e3, 0xf678, 0xe7f1,
96 0x2e85, 0x3f0c, 0x0d97, 0x1c1e, 0x68a1, 0x7928, 0x4bb3, 0x5a3a,
97 0xa2cd, 0xb344, 0x81df, 0x9056, 0xe4e9, 0xf560, 0xc7fb, 0xd672,
98 0x3e04, 0x2f8d, 0x1d16, 0x0c9f, 0x7820, 0x69a9, 0x5b32, 0x4abb,
99 0xb24c, 0xa3c5, 0x915e, 0x80d7, 0xf468, 0xe5e1, 0xd77a, 0xc6f3,
100 0x4d83, 0x5c0a, 0x6e91, 0x7f18, 0x0ba7, 0x1a2e, 0x28b5, 0x393c,
101 0xc1cb, 0xd042, 0xe2d9, 0xf350, 0x87ef, 0x9666, 0xa4fd, 0xb574,
102 0x5d02, 0x4c8b, 0x7e10, 0x6f99, 0x1b26, 0x0aaf, 0x3834, 0x29bd,
103 0xd14a, 0xc0c3, 0xf258, 0xe3d1, 0x976e, 0x86e7, 0xb47c, 0xa5f5,
104 0x6c81, 0x7d08, 0x4f93, 0x5e1a, 0x2aa5, 0x3b2c, 0x09b7, 0x183e,
105 0xe0c9, 0xf140, 0xc3db, 0xd252, 0xa6ed, 0xb764, 0x85ff, 0x9476,
106 0x7c00, 0x6d89, 0x5f12, 0x4e9b, 0x3a24, 0x2bad, 0x1936, 0x08bf,
107 0xf048, 0xe1c1, 0xd35a, 0xc2d3, 0xb66c, 0xa7e5, 0x957e, 0x84f7,
108 0x8b8f, 0x9a06, 0xa89d, 0xb914, 0xcdab, 0xdc22, 0xeeb9, 0xff30,
109 0x07c7, 0x164e, 0x24d5, 0x355c, 0x41e3, 0x506a, 0x62f1, 0x7378,
110 0x9b0e, 0x8a87, 0xb81c, 0xa995, 0xdd2a, 0xcca3, 0xfe38, 0xefb1,
111 0x1746, 0x06cf, 0x3454, 0x25dd, 0x5162, 0x40eb, 0x7270, 0x63f9,
112 0xaa8d, 0xbb04, 0x899f, 0x9816, 0xeca9, 0xfd20, 0xcfbb, 0xde32,
113 0x26c5, 0x374c, 0x05d7, 0x145e, 0x60e1, 0x7168, 0x43f3, 0x527a,
114 0xba0c, 0xab85, 0x991e, 0x8897, 0xfc28, 0xeda1, 0xdf3a, 0xceb3,
115 0x3644, 0x27cd, 0x1556, 0x04df, 0x7060, 0x61e9, 0x5372, 0x42fb,
116 0xc98b, 0xd802, 0xea99, 0xfb10, 0x8faf, 0x9e26, 0xacbd, 0xbd34,
117 0x45c3, 0x544a, 0x66d1, 0x7758, 0x03e7, 0x126e, 0x20f5, 0x317c,
118 0xd90a, 0xc883, 0xfa18, 0xeb91, 0x9f2e, 0x8ea7, 0xbc3c, 0xadb5,
119 0x5542, 0x44cb, 0x7650, 0x67d9, 0x1366, 0x02ef, 0x3074, 0x21fd,
120 0xe889, 0xf900, 0xcb9b, 0xda12, 0xaead, 0xbf24, 0x8dbf, 0x9c36,
121 0x64c1, 0x7548, 0x47d3, 0x565a, 0x22e5, 0x336c, 0x01f7, 0x107e,
122 0xf808, 0xe981, 0xdb1a, 0xca93, 0xbe2c, 0xafa5, 0x9d3e, 0x8cb7,
123 0x7440, 0x65c9, 0x5752, 0x46db, 0x3264, 0x23ed, 0x1176, 0x00ff
126 static unsigned short calc_crc_flex(
unsigned char *
cp,
int size)
128 unsigned short crc = 0xffff;
131 crc = (crc << 8) ^ crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
136 static int check_crc_flex(
unsigned char *cp,
int size)
138 unsigned short crc = 0xffff;
144 crc = (crc << 8) ^ crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
146 if ((crc & 0xffff) != 0x7070)
152 static int check_crc_16(
unsigned char *cp,
int size)
154 unsigned short crc = 0x0000;
159 crc =
crc16(0, cp, size);
171 static int kiss_esc(
unsigned char *
s,
unsigned char *
d,
int len)
173 unsigned char *
ptr =
d;
209 static int kiss_esc_crc(
unsigned char *s,
unsigned char *d,
unsigned short crc,
212 unsigned char *ptr =
d;
246 static void ax_bump(
struct mkiss *ax)
252 if (ax->
rbuff[0] > 0x0f) {
253 if (ax->
rbuff[0] & 0x80) {
255 ax->
dev->stats.rx_errors++;
262 "mkiss: %s: Switching to crc-smack\n",
268 }
else if (ax->
rbuff[0] & 0x20) {
270 ax->
dev->stats.rx_errors++;
276 "mkiss: %s: Switching to crc-flexnet\n",
294 if ((skb = dev_alloc_skb(count)) ==
NULL) {
297 ax->
dev->stats.rx_dropped++;
305 ax->
dev->stats.rx_packets++;
310 static void kiss_unesc(
struct mkiss *ax,
unsigned char s)
346 ax->
dev->stats.rx_over_errors++;
356 netif_tx_lock_bh(dev);
357 netif_addr_lock(dev);
359 netif_addr_unlock(dev);
360 netif_tx_unlock_bh(dev);
367 static void ax_changedmtu(
struct mkiss *ax)
370 unsigned char *xbuff, *rbuff, *oxbuff, *orbuff;
386 if (xbuff ==
NULL || rbuff ==
NULL) {
388 "MTU change cancelled.\n",
404 if (ax->
xleft <= len) {
408 dev->
stats.tx_dropped++;
419 dev->
stats.rx_over_errors++;
434 static void ax_encaps(
struct net_device *dev,
unsigned char *icp,
int len)
436 struct mkiss *ax = netdev_priv(dev);
440 if (ax->
mtu != ax->
dev->mtu + 73)
445 printk(
KERN_ERR "mkiss: %s: truncating oversized transmit packet!\n", ax->
dev->name);
446 dev->
stats.tx_dropped++;
447 netif_start_queue(dev);
454 if ((*p & 0x0f) != 0) {
464 int cmd = (p[1] & 0xff);
481 printk(
KERN_INFO "mkiss: %s: crc mode %s %d\n", ax->
dev->name, (len) ?
"set to" :
"is", cmd);
484 netif_start_queue(dev);
488 count = kiss_esc(p, ax->
xbuff, len);
500 count = kiss_esc_crc(p, ax->
xbuff, crc, len+2);
508 crc = calc_crc_flex(p, len);
509 count = kiss_esc_crc(p, ax->
xbuff, crc, len+2);
513 count = kiss_esc(p, ax->
xbuff, len);
519 actual = ax->
tty->ops->write(ax->
tty, ax->
xbuff, count);
520 dev->
stats.tx_packets++;
521 dev->
stats.tx_bytes += actual;
524 ax->
xleft = count - actual;
531 struct mkiss *ax = netdev_priv(dev);
533 if (!netif_running(dev)) {
538 if (netif_queue_stopped(dev)) {
550 "bad line quality" :
"driver error");
554 netif_start_queue(dev);
559 netif_stop_queue(dev);
560 ax_encaps(dev, skb->
data, skb->
len);
567 static int ax_open_dev(
struct net_device *dev)
569 struct mkiss *ax = netdev_priv(dev);
577 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
582 const void *
saddr,
unsigned len)
592 static int ax_rebuild_header(
struct sk_buff *skb)
606 struct mkiss *ax = netdev_priv(dev);
656 struct mkiss *ax = netdev_priv(dev);
661 netif_stop_queue(dev);
666 static const struct header_ops ax_header_ops = {
668 .rebuild = ax_rebuild_header,
672 .ndo_open = ax_open_dev,
673 .ndo_stop = ax_close,
674 .ndo_start_xmit = ax_xmit,
675 .ndo_set_mac_address = ax_set_mac_address,
719 static void mkiss_put(
struct mkiss *ax)
725 static int crc_force = 0;
744 ax = netdev_priv(dev);
761 if ((err = ax_open(ax->
dev))) {
762 goto out_free_netdev;
766 goto out_free_buffers;
793 ax->
crcauto = (crc_force ? 0 : 1);
795 netif_start_queue(dev);
811 static void mkiss_close(
struct tty_struct *tty)
841 unsigned int cmd,
unsigned long arg)
843 struct mkiss *ax = mkiss_get(tty);
859 err =
put_user(4, (
int __user *) arg);
863 if (
get_user(tmp, (
int __user *) arg)) {
886 netif_tx_lock_bh(dev);
888 netif_tx_unlock_bh(dev);
903 static long mkiss_compat_ioctl(
struct tty_struct *tty,
struct file *file,
904 unsigned int cmd,
unsigned long arg)
911 return mkiss_ioctl(tty, file, cmd,
912 (
unsigned long)compat_ptr(arg));
925 static void mkiss_receive_buf(
struct tty_struct *tty,
const unsigned char *cp,
928 struct mkiss *ax = mkiss_get(tty);
937 if (ax->
mtu != ax->
dev->mtu + 73)
942 if (fp !=
NULL && *fp++) {
944 ax->
dev->stats.rx_errors++;
949 kiss_unesc(ax, *cp++);
960 static void mkiss_write_wakeup(
struct tty_struct *tty)
962 struct mkiss *ax = mkiss_get(tty);
968 if (ax->
xleft <= 0) {
974 netif_wake_queue(ax->
dev);
991 .close = mkiss_close,
992 .ioctl = mkiss_ioctl,
994 .compat_ioctl = mkiss_compat_ioctl,
996 .receive_buf = mkiss_receive_buf,
997 .write_wakeup = mkiss_write_wakeup
1001 "mkiss: AX.25 Multikiss, Hans Albas PE1AYX\n";
1002 static const char msg_regfail[] __initconst =
KERN_ERR \
1003 "mkiss: can't register line discipline (err = %d)\n";
1005 static int __init mkiss_init_driver(
void)
1013 printk(msg_regfail, status);
1018 static const char msg_unregfail[] =
KERN_ERR \
1019 "mkiss: can't unregister line discipline (err = %d)\n";
1021 static void __exit mkiss_exit_driver(
void)
1026 printk(msg_unregfail, ret);
1032 MODULE_PARM_DESC(crc_force,
"crc [0 = auto | 1 = none | 2 = flexnet | 3 = smack]");