5 * This module is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
10 * This module implements the AX.25 protocol for kernel-based
11 * devices like TTYs. It interfaces between a raw TTY, and the
12 * kernel's AX.25 protocol layers, just like slip.c.
13 * AX.25 needs to be separated from slip.c while slip.c is no
14 * longer a static kernel device since it is a module.
15 * This method clears the way to implement other kiss protocols
16 * like mkiss smack g8bpq ..... so far only mkiss is implemented.
18 * Hans Alblas <hans@esrac.ele.tue.nl>
21 * Jonathan (G4KLX) Fixed to match Linux networking changes - 2.1.15.
22 * Matthias (DG2FEF) Added support for FlexNet CRC (on special request)
23 * Fixed bug in ax25_close(): dev_lock_wait() was
24 * called twice, causing a deadlock.
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <asm/system.h>
30 #include <asm/segment.h>
31 #include <asm/bitops.h>
32 #include <asm/uaccess.h>
33 #include <linux/string.h>
35 #include <linux/interrupt.h>
37 #include <linux/inet.h>
38 #include <linux/tty.h>
39 #include <linux/errno.h>
40 #include <linux/netdevice.h>
41 #include <linux/major.h>
42 #include <linux/init.h>
44 #include <linux/timer.h>
46 #include <linux/etherdevice.h>
47 #include <linux/skbuff.h>
48 #include <linux/if_arp.h>
56 #include <linux/tcp.h>
60 #define AX25_VERSION "AX25-MODULAR-NET3.019-NEWTTY"
61 #define min(a,b) (a < b ? a : b)
63 #define AX25_VERSION "AX25-NET3.019-NEWTTY"
67 #define MKISS_SERIAL_TYPE_NORMAL 1
69 struct mkiss_channel
{
70 int magic
; /* magic word */
71 int init
; /* channel exists? */
72 struct tty_struct
*tty
; /* link to tty control structure */
75 typedef struct ax25_ctrl
{
76 char if_name
[8]; /* "ax0\0" .. "ax99999\0" */
77 struct ax_disp ctrl
; /* */
78 struct net_device dev
; /* the device */
81 static ax25_ctrl_t
**ax25_ctrls
= NULL
;
83 int ax25_maxdev
= AX25_MAXDEV
; /* Can be overridden with insmod! */
85 static struct tty_ldisc ax_ldisc
;
86 static struct tty_driver mkiss_driver
;
87 static int mkiss_refcount
;
88 static struct tty_struct
*mkiss_table
[NR_MKISS
];
89 static struct termios
*mkiss_termios
[NR_MKISS
];
90 static struct termios
*mkiss_termios_locked
[NR_MKISS
];
91 struct mkiss_channel MKISS_Info
[NR_MKISS
];
93 static int ax25_init(struct net_device
*);
94 static int mkiss_init(void);
95 static int mkiss_write(struct tty_struct
*, int, const unsigned char *, int);
96 static int kiss_esc(unsigned char *, unsigned char *, int);
97 static int kiss_esc_crc(unsigned char *, unsigned char *, unsigned short, int);
98 static void kiss_unesc(struct ax_disp
*, unsigned char);
100 /*---------------------------------------------------------------------------*/
102 static const unsigned short Crc_flex_table
[] = {
103 0x0f87, 0x1e0e, 0x2c95, 0x3d1c, 0x49a3, 0x582a, 0x6ab1, 0x7b38,
104 0x83cf, 0x9246, 0xa0dd, 0xb154, 0xc5eb, 0xd462, 0xe6f9, 0xf770,
105 0x1f06, 0x0e8f, 0x3c14, 0x2d9d, 0x5922, 0x48ab, 0x7a30, 0x6bb9,
106 0x934e, 0x82c7, 0xb05c, 0xa1d5, 0xd56a, 0xc4e3, 0xf678, 0xe7f1,
107 0x2e85, 0x3f0c, 0x0d97, 0x1c1e, 0x68a1, 0x7928, 0x4bb3, 0x5a3a,
108 0xa2cd, 0xb344, 0x81df, 0x9056, 0xe4e9, 0xf560, 0xc7fb, 0xd672,
109 0x3e04, 0x2f8d, 0x1d16, 0x0c9f, 0x7820, 0x69a9, 0x5b32, 0x4abb,
110 0xb24c, 0xa3c5, 0x915e, 0x80d7, 0xf468, 0xe5e1, 0xd77a, 0xc6f3,
111 0x4d83, 0x5c0a, 0x6e91, 0x7f18, 0x0ba7, 0x1a2e, 0x28b5, 0x393c,
112 0xc1cb, 0xd042, 0xe2d9, 0xf350, 0x87ef, 0x9666, 0xa4fd, 0xb574,
113 0x5d02, 0x4c8b, 0x7e10, 0x6f99, 0x1b26, 0x0aaf, 0x3834, 0x29bd,
114 0xd14a, 0xc0c3, 0xf258, 0xe3d1, 0x976e, 0x86e7, 0xb47c, 0xa5f5,
115 0x6c81, 0x7d08, 0x4f93, 0x5e1a, 0x2aa5, 0x3b2c, 0x09b7, 0x183e,
116 0xe0c9, 0xf140, 0xc3db, 0xd252, 0xa6ed, 0xb764, 0x85ff, 0x9476,
117 0x7c00, 0x6d89, 0x5f12, 0x4e9b, 0x3a24, 0x2bad, 0x1936, 0x08bf,
118 0xf048, 0xe1c1, 0xd35a, 0xc2d3, 0xb66c, 0xa7e5, 0x957e, 0x84f7,
119 0x8b8f, 0x9a06, 0xa89d, 0xb914, 0xcdab, 0xdc22, 0xeeb9, 0xff30,
120 0x07c7, 0x164e, 0x24d5, 0x355c, 0x41e3, 0x506a, 0x62f1, 0x7378,
121 0x9b0e, 0x8a87, 0xb81c, 0xa995, 0xdd2a, 0xcca3, 0xfe38, 0xefb1,
122 0x1746, 0x06cf, 0x3454, 0x25dd, 0x5162, 0x40eb, 0x7270, 0x63f9,
123 0xaa8d, 0xbb04, 0x899f, 0x9816, 0xeca9, 0xfd20, 0xcfbb, 0xde32,
124 0x26c5, 0x374c, 0x05d7, 0x145e, 0x60e1, 0x7168, 0x43f3, 0x527a,
125 0xba0c, 0xab85, 0x991e, 0x8897, 0xfc28, 0xeda1, 0xdf3a, 0xceb3,
126 0x3644, 0x27cd, 0x1556, 0x04df, 0x7060, 0x61e9, 0x5372, 0x42fb,
127 0xc98b, 0xd802, 0xea99, 0xfb10, 0x8faf, 0x9e26, 0xacbd, 0xbd34,
128 0x45c3, 0x544a, 0x66d1, 0x7758, 0x03e7, 0x126e, 0x20f5, 0x317c,
129 0xd90a, 0xc883, 0xfa18, 0xeb91, 0x9f2e, 0x8ea7, 0xbc3c, 0xadb5,
130 0x5542, 0x44cb, 0x7650, 0x67d9, 0x1366, 0x02ef, 0x3074, 0x21fd,
131 0xe889, 0xf900, 0xcb9b, 0xda12, 0xaead, 0xbf24, 0x8dbf, 0x9c36,
132 0x64c1, 0x7548, 0x47d3, 0x565a, 0x22e5, 0x336c, 0x01f7, 0x107e,
133 0xf808, 0xe981, 0xdb1a, 0xca93, 0xbe2c, 0xafa5, 0x9d3e, 0x8cb7,
134 0x7440, 0x65c9, 0x5752, 0x46db, 0x3264, 0x23ed, 0x1176, 0x00ff
137 /*---------------------------------------------------------------------------*/
139 static unsigned short
140 calc_crc_flex(unsigned char *cp
, int size
)
142 unsigned short crc
= 0xffff;
145 crc
= (crc
<< 8) ^ Crc_flex_table
[((crc
>> 8) ^ *cp
++) & 0xff];
150 /*---------------------------------------------------------------------------*/
153 check_crc_flex(unsigned char *cp
, int size
)
155 unsigned short crc
= 0xffff;
161 crc
= (crc
<< 8) ^ Crc_flex_table
[((crc
>> 8) ^ *cp
++) & 0xff];
163 if ((crc
& 0xffff) != 0x7070)
169 /*---------------------------------------------------------------------------*/
171 /* Find a free channel, and link in this `tty' line. */
172 static inline struct ax_disp
*ax_alloc(void)
177 if (ax25_ctrls
== NULL
) /* Master array missing ! */
180 for (i
= 0; i
< ax25_maxdev
; i
++) {
183 /* Not allocated ? */
188 if (!test_and_set_bit(AXF_INUSE
, &axp
->ctrl
.flags
))
192 /* Sorry, too many, all slots in use */
193 if (i
>= ax25_maxdev
)
196 /* If no channels are available, allocate one */
197 if (axp
== NULL
&& (ax25_ctrls
[i
] = kmalloc(sizeof(ax25_ctrl_t
), GFP_KERNEL
)) != NULL
) {
199 memset(axp
, 0, sizeof(ax25_ctrl_t
));
201 /* Initialize channel control data */
202 set_bit(AXF_INUSE
, &axp
->ctrl
.flags
);
203 sprintf(axp
->if_name
, "ax%d", i
++);
204 axp
->ctrl
.tty
= NULL
;
205 axp
->dev
.name
= axp
->if_name
;
206 axp
->dev
.base_addr
= i
;
207 axp
->dev
.priv
= (void *)&axp
->ctrl
;
208 axp
->dev
.next
= NULL
;
209 axp
->dev
.init
= ax25_init
;
214 * register device so that it can be ifconfig'ed
215 * ax25_init() will be called as a side-effect
216 * SIDE-EFFECT WARNING: ax25_init() CLEARS axp->ctrl !
218 if (register_netdev(&axp
->dev
) == 0) {
219 /* (Re-)Set the INUSE bit. Very Important! */
220 set_bit(AXF_INUSE
, &axp
->ctrl
.flags
);
221 axp
->ctrl
.dev
= &axp
->dev
;
222 axp
->dev
.priv
= (void *)&axp
->ctrl
;
226 clear_bit(AXF_INUSE
,&axp
->ctrl
.flags
);
227 printk(KERN_ERR
"mkiss: ax_alloc() - register_netdev() failure.\n");
234 /* Free an AX25 channel. */
235 static inline void ax_free(struct ax_disp
*ax
)
237 /* Free all AX25 frame buffers. */
244 if (!test_and_clear_bit(AXF_INUSE
, &ax
->flags
))
245 printk(KERN_ERR
"mkiss: %s: ax_free for already free unit.\n", ax
->dev
->name
);
248 static void ax_changedmtu(struct ax_disp
*ax
)
250 struct net_device
*dev
= ax
->dev
;
251 unsigned char *xbuff
, *rbuff
, *oxbuff
, *orbuff
;
258 * allow for arrival of larger UDP packets, even if we say not to
259 * also fixes a bug in which SunOS sends 512-byte packets even with
265 xbuff
= kmalloc(len
+ 4, GFP_ATOMIC
);
266 rbuff
= kmalloc(len
+ 4, GFP_ATOMIC
);
268 if (xbuff
== NULL
|| rbuff
== NULL
) {
269 printk(KERN_ERR
"mkiss: %s: unable to grow ax25 buffers, MTU change cancelled.\n",
288 if (ax
->xleft
<= len
) {
289 memcpy(ax
->xbuff
, ax
->xhead
, ax
->xleft
);
296 ax
->xhead
= ax
->xbuff
;
299 if (ax
->rcount
<= len
) {
300 memcpy(ax
->rbuff
, orbuff
, ax
->rcount
);
303 ax
->rx_over_errors
++;
304 set_bit(AXF_ERROR
, &ax
->flags
);
308 ax
->mtu
= dev
->mtu
+ 73;
311 restore_flags(flags
);
320 /* Set the "sending" flag. This must be atomic, hence the ASM. */
321 static inline void ax_lock(struct ax_disp
*ax
)
323 if (test_and_set_bit(0, (void *)&ax
->dev
->tbusy
))
324 printk(KERN_ERR
"mkiss: %s: trying to lock already locked device!\n", ax
->dev
->name
);
328 /* Clear the "sending" flag. This must be atomic, hence the ASM. */
329 static inline void ax_unlock(struct ax_disp
*ax
)
331 if (!test_and_clear_bit(0, (void *)&ax
->dev
->tbusy
))
332 printk(KERN_ERR
"mkiss: %s: trying to unlock already unlocked device!\n", ax
->dev
->name
);
335 /* Send one completely decapsulated AX.25 packet to the AX.25 layer. */
336 static void ax_bump(struct ax_disp
*ax
)
338 struct ax_disp
*tmp_ax
;
340 struct mkiss_channel
*mkiss
;
345 if (ax
->rbuff
[0] > 0x0f) {
346 if (ax
->mkiss
!= NULL
) {
347 mkiss
= ax
->mkiss
->tty
->driver_data
;
348 if (mkiss
->magic
== MKISS_DRIVER_MAGIC
)
350 } else if (ax
->rbuff
[0] & 0x20) {
351 ax
->crcmode
= CRC_MODE_FLEX
;
352 if (check_crc_flex(ax
->rbuff
, ax
->rcount
) < 0) {
362 if ((skb
= dev_alloc_skb(count
)) == NULL
) {
363 printk(KERN_ERR
"mkiss: %s: memory squeeze, dropping packet.\n", ax
->dev
->name
);
368 skb
->dev
= tmp_ax
->dev
;
369 memcpy(skb_put(skb
,count
), ax
->rbuff
, count
);
370 skb
->mac
.raw
= skb
->data
;
371 skb
->protocol
= htons(ETH_P_AX25
);
373 tmp_ax
->rx_packets
++;
376 /* Encapsulate one AX.25 packet and stuff into a TTY queue. */
377 static void ax_encaps(struct ax_disp
*ax
, unsigned char *icp
, int len
)
381 struct mkiss_channel
*mkiss
= ax
->tty
->driver_data
;
383 if (ax
->mtu
!= ax
->dev
->mtu
+ 73) /* Someone has been ifconfigging */
386 if (len
> ax
->mtu
) { /* Sigh, shouldn't occur BUT ... */
388 printk(KERN_ERR
"mkiss: %s: truncating oversized transmit packet!\n", ax
->dev
->name
);
396 if (mkiss
->magic
!= MKISS_DRIVER_MAGIC
) {
397 switch (ax
->crcmode
) {
402 crc
= calc_crc_flex(p
, len
);
403 count
= kiss_esc_crc(p
, (unsigned char *)ax
->xbuff
, crc
, len
+2);
407 count
= kiss_esc(p
, (unsigned char *)ax
->xbuff
, len
);
410 ax
->tty
->flags
|= (1 << TTY_DO_WRITE_WAKEUP
);
411 actual
= ax
->tty
->driver
.write(ax
->tty
, 0, ax
->xbuff
, count
);
413 ax
->dev
->trans_start
= jiffies
;
414 ax
->xleft
= count
- actual
;
415 ax
->xhead
= ax
->xbuff
+ actual
;
417 count
= kiss_esc(p
, (unsigned char *) ax
->mkiss
->xbuff
, len
);
418 ax
->mkiss
->tty
->flags
|= (1 << TTY_DO_WRITE_WAKEUP
);
419 actual
= ax
->mkiss
->tty
->driver
.write(ax
->mkiss
->tty
, 0, ax
->mkiss
->xbuff
, count
);
421 ax
->mkiss
->dev
->trans_start
= jiffies
;
422 ax
->mkiss
->xleft
= count
- actual
;
423 ax
->mkiss
->xhead
= ax
->mkiss
->xbuff
+ actual
;
428 * Called by the driver when there's room for more data. If we have
429 * more packets to send, we send them here.
431 static void ax25_write_wakeup(struct tty_struct
*tty
)
434 struct ax_disp
*ax
= (struct ax_disp
*)tty
->disc_data
;
435 struct mkiss_channel
*mkiss
;
437 /* First make sure we're connected. */
438 if (ax
== NULL
|| ax
->magic
!= AX25_MAGIC
|| !ax
->dev
->start
)
440 if (ax
->xleft
<= 0) {
441 /* Now serial buffer is almost free & we can start
442 * transmission of another packet
444 tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
446 if (ax
->mkiss
!= NULL
) {
447 mkiss
= ax
->mkiss
->tty
->driver_data
;
448 if (mkiss
->magic
== MKISS_DRIVER_MAGIC
)
449 ax_unlock(ax
->mkiss
);
457 actual
= tty
->driver
.write(tty
, 0, ax
->xhead
, ax
->xleft
);
462 /* Encapsulate an AX.25 packet and kick it into a TTY queue. */
463 static int ax_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
465 struct ax_disp
*ax
= (struct ax_disp
*)dev
->priv
;
466 struct mkiss_channel
*mkiss
= ax
->tty
->driver_data
;
467 struct ax_disp
*tmp_ax
;
471 if (mkiss
->magic
== MKISS_DRIVER_MAGIC
) {
472 if (skb
->data
[0] < 0x10)
473 skb
->data
[0] = skb
->data
[0] + 0x10;
478 printk(KERN_ERR
"mkiss: %s: xmit call when iface is down\n", dev
->name
);
483 if (tmp_ax
->dev
->tbusy
)
488 printk(KERN_ERR
"mkiss: dev busy while serial dev is free\n");
494 * May be we must check transmitter timeout here ?
495 * 14 Oct 1994 Dmitry Gorodchanin.
497 if (jiffies
- dev
->trans_start
< 20 * HZ
) {
498 /* 20 sec timeout not reached */
502 printk(KERN_ERR
"mkiss: %s: transmit timed out, %s?\n", dev
->name
,
503 (ax
->tty
->driver
.chars_in_buffer(ax
->tty
) || ax
->xleft
) ?
504 "bad line quality" : "driver error");
507 ax
->tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
511 /* We were not busy, so we are now... :-) */
516 ax_encaps(ax
, skb
->data
, skb
->len
);
523 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
525 /* Return the frame type ID */
526 static int ax_header(struct sk_buff
*skb
, struct net_device
*dev
, unsigned short type
,
527 void *daddr
, void *saddr
, unsigned len
)
530 if (type
!= htons(ETH_P_AX25
))
531 return ax25_encapsulate(skb
, dev
, type
, daddr
, saddr
, len
);
537 static int ax_rebuild_header(struct sk_buff
*skb
)
540 return ax25_rebuild_header(skb
);
546 #endif /* CONFIG_{AX25,AX25_MODULE} */
548 /* Open the low-level part of the AX25 channel. Easy! */
549 static int ax_open(struct net_device
*dev
)
551 struct ax_disp
*ax
= (struct ax_disp
*)dev
->priv
;
558 * Allocate the frame buffers:
560 * rbuff Receive buffer.
561 * xbuff Transmit buffer.
562 * cbuff Temporary compression buffer.
567 * allow for arrival of larger UDP packets, even if we say not to
568 * also fixes a bug in which SunOS sends 512-byte packets even with
574 if ((ax
->rbuff
= kmalloc(len
+ 4, GFP_KERNEL
)) == NULL
)
577 if ((ax
->xbuff
= kmalloc(len
+ 4, GFP_KERNEL
)) == NULL
)
580 ax
->mtu
= dev
->mtu
+ 73;
585 ax
->flags
&= (1 << AXF_INUSE
); /* Clear ESCAPE & ERROR flags */
602 /* Close the low-level part of the AX25 channel. Easy! */
603 static int ax_close(struct net_device
*dev
)
605 struct ax_disp
*ax
= (struct ax_disp
*)dev
->priv
;
610 ax
->tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
618 static int ax25_receive_room(struct tty_struct
*tty
)
620 return 65536; /* We can handle an infinite amount of data. :-) */
624 * Handle the 'receiver data ready' interrupt.
625 * This function is called by the 'tty_io' module in the kernel when
626 * a block of data has been received, which can now be decapsulated
627 * and sent on to the AX.25 layer for further processing.
629 static void ax25_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
, char *fp
, int count
)
631 struct ax_disp
*ax
= (struct ax_disp
*)tty
->disc_data
;
633 if (ax
== NULL
|| ax
->magic
!= AX25_MAGIC
|| !ax
->dev
->start
)
637 * Argh! mtu change time! - costs us the packet part received
640 if (ax
->mtu
!= ax
->dev
->mtu
+ 73)
643 /* Read the characters out of the buffer */
645 if (fp
!= NULL
&& *fp
++) {
646 if (!test_and_set_bit(AXF_ERROR
, &ax
->flags
))
652 kiss_unesc(ax
, *cp
++);
656 static int ax25_open(struct tty_struct
*tty
)
658 struct ax_disp
*ax
= (struct ax_disp
*)tty
->disc_data
;
659 struct ax_disp
*tmp_ax
;
660 struct mkiss_channel
*mkiss
;
663 /* First make sure we're not already connected. */
664 if (ax
&& ax
->magic
== AX25_MAGIC
)
667 /* OK. Find a free AX25 channel to use. */
668 if ((ax
= ax_alloc()) == NULL
)
677 if (tty
->driver
.flush_buffer
)
678 tty
->driver
.flush_buffer(tty
);
679 if (tty
->ldisc
.flush_buffer
)
680 tty
->ldisc
.flush_buffer(tty
);
682 /* Restore default settings */
683 ax
->dev
->type
= ARPHRD_AX25
;
685 /* Perform the low-level AX25 initialization. */
686 if ((err
= ax_open(ax
->dev
)))
689 mkiss
= ax
->tty
->driver_data
;
691 if (mkiss
->magic
== MKISS_DRIVER_MAGIC
) {
692 for (cnt
= 1; cnt
< ax25_maxdev
; cnt
++) {
693 if (ax25_ctrls
[cnt
]) {
694 if (ax25_ctrls
[cnt
]->dev
.start
) {
695 if (ax
== &ax25_ctrls
[cnt
]->ctrl
) {
697 tmp_ax
= &ax25_ctrls
[cnt
]->ctrl
;
705 if (tmp_ax
!= NULL
) {
712 /* Done. We have linked the TTY line to a channel. */
713 return ax
->dev
->base_addr
;
716 static void ax25_close(struct tty_struct
*tty
)
718 struct ax_disp
*ax
= (struct ax_disp
*)tty
->disc_data
;
721 /* First make sure we're connected. */
722 if (ax
== NULL
|| ax
->magic
!= AX25_MAGIC
)
732 /* VSV = very important to remove timers */
734 unregister_netdev(ax
->dev
);
740 static struct net_device_stats
*ax_get_stats(struct net_device
*dev
)
742 static struct net_device_stats stats
;
743 struct ax_disp
*ax
= (struct ax_disp
*)dev
->priv
;
745 memset(&stats
, 0, sizeof(struct net_device_stats
));
747 stats
.rx_packets
= ax
->rx_packets
;
748 stats
.tx_packets
= ax
->tx_packets
;
749 stats
.rx_dropped
= ax
->rx_dropped
;
750 stats
.tx_dropped
= ax
->tx_dropped
;
751 stats
.tx_errors
= ax
->tx_errors
;
752 stats
.rx_errors
= ax
->rx_errors
;
753 stats
.rx_over_errors
= ax
->rx_over_errors
;
759 /************************************************************************
760 * STANDARD ENCAPSULATION *
761 ************************************************************************/
763 int kiss_esc(unsigned char *s
, unsigned char *d
, int len
)
765 unsigned char *ptr
= d
;
769 * Send an initial END character to flush out any
770 * data that may have accumulated in the receiver
799 * OK its ugly, but tell me a better solution without copying the
800 * packet to a temporary buffer :-)
802 static int kiss_esc_crc(unsigned char *s
, unsigned char *d
, unsigned short crc
, int len
)
804 unsigned char *ptr
= d
;
836 static void kiss_unesc(struct ax_disp
*ax
, unsigned char s
)
840 /* drop keeptest bit = VSV */
841 if (test_bit(AXF_KEEPTEST
, &ax
->flags
))
842 clear_bit(AXF_KEEPTEST
, &ax
->flags
);
844 if (!test_and_clear_bit(AXF_ERROR
, &ax
->flags
) && (ax
->rcount
> 2))
847 clear_bit(AXF_ESCAPE
, &ax
->flags
);
852 set_bit(AXF_ESCAPE
, &ax
->flags
);
855 if (test_and_clear_bit(AXF_ESCAPE
, &ax
->flags
))
859 if (test_and_clear_bit(AXF_ESCAPE
, &ax
->flags
))
864 if (!test_bit(AXF_ERROR
, &ax
->flags
)) {
865 if (ax
->rcount
< ax
->buffsize
) {
866 ax
->rbuff
[ax
->rcount
++] = s
;
870 ax
->rx_over_errors
++;
871 set_bit(AXF_ERROR
, &ax
->flags
);
876 int ax_set_mac_address(struct net_device
*dev
, void *addr
)
878 if (copy_from_user(dev
->dev_addr
, addr
, AX25_ADDR_LEN
))
883 static int ax_set_dev_mac_address(struct net_device
*dev
, void *addr
)
885 struct sockaddr
*sa
= addr
;
887 memcpy(dev
->dev_addr
, sa
->sa_data
, AX25_ADDR_LEN
);
893 /* Perform I/O control on an active ax25 channel. */
894 static int ax25_disp_ioctl(struct tty_struct
*tty
, void *file
, int cmd
, void *arg
)
896 struct ax_disp
*ax
= (struct ax_disp
*)tty
->disc_data
;
899 /* First make sure we're connected. */
900 if (ax
== NULL
|| ax
->magic
!= AX25_MAGIC
)
905 if (copy_to_user(arg
, ax
->dev
->name
, strlen(ax
->dev
->name
) + 1))
910 put_user(4, (int *)arg
);
914 get_user(tmp
, (int *)arg
);
916 ax
->dev
->addr_len
= AX25_ADDR_LEN
; /* sizeof an AX.25 addr */
917 ax
->dev
->hard_header_len
= AX25_KISS_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ 3;
918 ax
->dev
->type
= ARPHRD_AX25
;
922 return ax_set_mac_address(ax
->dev
, arg
);
929 static int ax_open_dev(struct net_device
*dev
)
931 struct ax_disp
*ax
= (struct ax_disp
*)dev
->priv
;
939 /* Initialize AX25 control device -- register AX25 line discipline */
940 int __init
mkiss_init_ctrl_dev(void)
944 if (ax25_maxdev
< 4) ax25_maxdev
= 4; /* Sanity */
946 if ((ax25_ctrls
= kmalloc(sizeof(void*) * ax25_maxdev
, GFP_KERNEL
)) == NULL
) {
947 printk(KERN_ERR
"mkiss: Can't allocate ax25_ctrls[] array ! No mkiss available\n");
951 /* Clear the pointer array, we allocate devices when we need them */
952 memset(ax25_ctrls
, 0, sizeof(void*) * ax25_maxdev
); /* Pointers */
954 /* Fill in our line protocol discipline, and register it */
955 memset(&ax_ldisc
, 0, sizeof(ax_ldisc
));
956 ax_ldisc
.magic
= TTY_LDISC_MAGIC
;
957 ax_ldisc
.name
= "mkiss";
959 ax_ldisc
.open
= ax25_open
;
960 ax_ldisc
.close
= ax25_close
;
961 ax_ldisc
.read
= NULL
;
962 ax_ldisc
.write
= NULL
;
963 ax_ldisc
.ioctl
= (int (*)(struct tty_struct
*, struct file
*, unsigned int, unsigned long))ax25_disp_ioctl
;
964 ax_ldisc
.poll
= NULL
;
966 ax_ldisc
.receive_buf
= ax25_receive_buf
;
967 ax_ldisc
.receive_room
= ax25_receive_room
;
968 ax_ldisc
.write_wakeup
= ax25_write_wakeup
;
970 if ((status
= tty_register_ldisc(N_AX25
, &ax_ldisc
)) != 0)
971 printk(KERN_ERR
"mkiss: can't register line discipline (err = %d)\n", status
);
979 * Return "not found", so that dev_init() will unlink
980 * the placeholder device entry for us.
987 /* Initialize the driver. Called by network startup. */
989 static int ax25_init(struct net_device
*dev
)
991 struct ax_disp
*ax
= (struct ax_disp
*)dev
->priv
;
993 static char ax25_bcast
[AX25_ADDR_LEN
] =
994 {'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
995 static char ax25_test
[AX25_ADDR_LEN
] =
996 {'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
998 if (ax
== NULL
) /* Allocation failed ?? */
1001 /* Set up the "AX25 Control Block". (And clear statistics) */
1002 memset(ax
, 0, sizeof (struct ax_disp
));
1003 ax
->magic
= AX25_MAGIC
;
1006 /* Finish setting up the DEVICE info. */
1008 dev
->hard_start_xmit
= ax_xmit
;
1009 dev
->open
= ax_open_dev
;
1010 dev
->stop
= ax_close
;
1011 dev
->get_stats
= ax_get_stats
;
1012 #ifdef HAVE_SET_MAC_ADDR
1013 dev
->set_mac_address
= ax_set_dev_mac_address
;
1015 dev
->hard_header_len
= 0;
1017 dev
->type
= ARPHRD_AX25
;
1018 dev
->tx_queue_len
= 10;
1020 memcpy(dev
->broadcast
, ax25_bcast
, AX25_ADDR_LEN
);
1021 memcpy(dev
->dev_addr
, ax25_test
, AX25_ADDR_LEN
);
1023 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1024 dev
->hard_header
= ax_header
;
1025 dev
->rebuild_header
= ax_rebuild_header
;
1028 dev_init_buffers(dev
);
1030 /* New-style flags. */
1036 static int mkiss_open(struct tty_struct
*tty
, struct file
*filp
)
1038 struct mkiss_channel
*mkiss
;
1041 chan
= MINOR(tty
->device
) - tty
->driver
.minor_start
;
1043 if (chan
< 0 || chan
>= NR_MKISS
)
1046 mkiss
= &MKISS_Info
[chan
];
1048 mkiss
->magic
= MKISS_DRIVER_MAGIC
;
1052 tty
->driver_data
= mkiss
;
1054 tty
->termios
->c_iflag
= IGNBRK
| IGNPAR
;
1055 tty
->termios
->c_cflag
= B9600
| CS8
| CLOCAL
;
1056 tty
->termios
->c_cflag
&= ~CBAUD
;
1061 static void mkiss_close(struct tty_struct
*tty
, struct file
* filp
)
1063 struct mkiss_channel
*mkiss
= tty
->driver_data
;
1065 if (mkiss
== NULL
|| mkiss
->magic
!= MKISS_DRIVER_MAGIC
)
1073 static int mkiss_write(struct tty_struct
*tty
, int from_user
, const unsigned char *buf
, int count
)
1078 static int mkiss_ioctl(struct tty_struct
*tty
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1080 /* Ignore serial ioctl's */
1088 case TCSETSF
: /* should flush first, but... */
1089 case TCSETSW
: /* should wait until flush, but... */
1092 return -ENOIOCTLCMD
;
1097 static void mkiss_dummy(struct tty_struct
*tty
)
1099 struct mkiss_channel
*mkiss
= tty
->driver_data
;
1108 static void mkiss_dummy2(struct tty_struct
*tty
, unsigned char ch
)
1110 struct mkiss_channel
*mkiss
= tty
->driver_data
;
1120 static int mkiss_write_room(struct tty_struct
* tty
)
1122 struct mkiss_channel
*mkiss
= tty
->driver_data
;
1130 return 65536; /* We can handle an infinite amount of data. :-) */
1134 static int mkiss_chars_in_buffer(struct tty_struct
*tty
)
1136 struct mkiss_channel
*mkiss
= tty
->driver_data
;
1148 static void mkiss_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
1150 /* we don't do termios */
1153 /* ******************************************************************** */
1154 /* * Init MKISS driver * */
1155 /* ******************************************************************** */
1157 static int __init
mkiss_init(void)
1159 memset(&mkiss_driver
, 0, sizeof(struct tty_driver
));
1161 mkiss_driver
.magic
= MKISS_DRIVER_MAGIC
;
1162 mkiss_driver
.name
= "mkiss";
1163 mkiss_driver
.major
= MKISS_MAJOR
;
1164 mkiss_driver
.minor_start
= 0;
1165 mkiss_driver
.num
= NR_MKISS
;
1166 mkiss_driver
.type
= TTY_DRIVER_TYPE_SERIAL
;
1167 mkiss_driver
.subtype
= MKISS_SERIAL_TYPE_NORMAL
; /* not needed */
1169 mkiss_driver
.init_termios
= tty_std_termios
;
1170 mkiss_driver
.init_termios
.c_iflag
= IGNBRK
| IGNPAR
;
1171 mkiss_driver
.init_termios
.c_cflag
= B9600
| CS8
| CLOCAL
;
1173 mkiss_driver
.flags
= TTY_DRIVER_REAL_RAW
;
1174 mkiss_driver
.refcount
= &mkiss_refcount
;
1175 mkiss_driver
.table
= mkiss_table
;
1176 mkiss_driver
.termios
= (struct termios
**)mkiss_termios
;
1177 mkiss_driver
.termios_locked
= (struct termios
**)mkiss_termios_locked
;
1179 mkiss_driver
.ioctl
= mkiss_ioctl
;
1180 mkiss_driver
.open
= mkiss_open
;
1181 mkiss_driver
.close
= mkiss_close
;
1182 mkiss_driver
.write
= mkiss_write
;
1183 mkiss_driver
.write_room
= mkiss_write_room
;
1184 mkiss_driver
.chars_in_buffer
= mkiss_chars_in_buffer
;
1185 mkiss_driver
.set_termios
= mkiss_set_termios
;
1187 /* some unused functions */
1188 mkiss_driver
.flush_buffer
= mkiss_dummy
;
1189 mkiss_driver
.throttle
= mkiss_dummy
;
1190 mkiss_driver
.unthrottle
= mkiss_dummy
;
1191 mkiss_driver
.stop
= mkiss_dummy
;
1192 mkiss_driver
.start
= mkiss_dummy
;
1193 mkiss_driver
.hangup
= mkiss_dummy
;
1194 mkiss_driver
.flush_chars
= mkiss_dummy
;
1195 mkiss_driver
.put_char
= mkiss_dummy2
;
1197 if (tty_register_driver(&mkiss_driver
)) {
1198 printk(KERN_ERR
"mkiss: couldn't register Mkiss device\n");
1202 printk(KERN_INFO
"AX.25 Multikiss device enabled\n");
1210 MODULE_PARM(ax25_maxdev
, "i");
1211 MODULE_PARM_DESC(ax25_maxdev
, "number of MKISS devices");
1213 MODULE_AUTHOR("Hans Albas PE1AYX <hans@esrac.ele.tue.nl>");
1214 MODULE_DESCRIPTION("KISS driver for AX.25 over TTYs");
1216 int init_module(void)
1218 return mkiss_init_ctrl_dev();
1221 void cleanup_module(void)
1225 if (ax25_ctrls
!= NULL
) {
1226 for (i
= 0; i
< ax25_maxdev
; i
++) {
1227 if (ax25_ctrls
[i
]) {
1229 * VSV = if dev->start==0, then device
1230 * unregistred while close proc.
1232 if (ax25_ctrls
[i
]->dev
.start
)
1233 unregister_netdev(&(ax25_ctrls
[i
]->dev
));
1235 kfree(ax25_ctrls
[i
]);
1236 ax25_ctrls
[i
] = NULL
;
1244 if ((i
= tty_register_ldisc(N_AX25
, NULL
)))
1245 printk(KERN_ERR
"mkiss: can't unregister line discipline (err = %d)\n", i
);
1247 if (tty_unregister_driver(&mkiss_driver
)) /* remove devive */
1248 printk(KERN_ERR
"mkiss: can't unregister MKISS device\n");