Import 2.3.10pre5
[davej-history.git] / drivers / net / hamradio / mkiss.c
blob796b019e4ae360481b859b978bcb5f474a200b97
1 /*
2 * MKISS Driver
4 * This module:
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>
20 * History
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>
34 #include <linux/mm.h>
35 #include <linux/interrupt.h>
36 #include <linux/in.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>
50 #include <net/ax25.h>
52 #include "mkiss.h"
54 #ifdef CONFIG_INET
55 #include <linux/ip.h>
56 #include <linux/tcp.h>
57 #endif
59 #ifdef MODULE
60 #define AX25_VERSION "AX25-MODULAR-NET3.019-NEWTTY"
61 #define min(a,b) (a < b ? a : b)
62 #else
63 #define AX25_VERSION "AX25-NET3.019-NEWTTY"
64 #endif
66 #define NR_MKISS 4
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 device dev; /* the device */
79 } ax25_ctrl_t;
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 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;
144 while (size--)
145 crc = (crc << 8) ^ Crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
147 return crc;
150 /*---------------------------------------------------------------------------*/
152 static int
153 check_crc_flex(unsigned char *cp, int size)
155 unsigned short crc = 0xffff;
157 if (size < 3)
158 return -1;
160 while (size--)
161 crc = (crc << 8) ^ Crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
163 if ((crc & 0xffff) != 0x7070)
164 return -1;
166 return 0;
169 /*---------------------------------------------------------------------------*/
171 /* Find a free channel, and link in this `tty' line. */
172 static inline struct ax_disp *ax_alloc(void)
174 ax25_ctrl_t *axp;
175 int i;
177 if (ax25_ctrls == NULL) /* Master array missing ! */
178 return NULL;
180 for (i = 0; i < ax25_maxdev; i++) {
181 axp = ax25_ctrls[i];
183 /* Not allocated ? */
184 if (axp == NULL)
185 break;
187 /* Not in use ? */
188 if (!test_and_set_bit(AXF_INUSE, &axp->ctrl.flags))
189 break;
192 /* Sorry, too many, all slots in use */
193 if (i >= ax25_maxdev)
194 return NULL;
196 /* If no channels are available, allocate one */
197 if (axp == NULL && (ax25_ctrls[i] = kmalloc(sizeof(ax25_ctrl_t), GFP_KERNEL)) != NULL) {
198 axp = ax25_ctrls[i];
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;
212 if (axp != NULL) {
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;
224 return &axp->ctrl;
225 } else {
226 clear_bit(AXF_INUSE,&axp->ctrl.flags);
227 printk(KERN_ERR "mkiss: ax_alloc() - register_netdev() failure.\n");
231 return NULL;
234 /* Free an AX25 channel. */
235 static inline void ax_free(struct ax_disp *ax)
237 /* Free all AX25 frame buffers. */
238 if (ax->rbuff)
239 kfree(ax->rbuff);
240 ax->rbuff = NULL;
241 if (ax->xbuff)
242 kfree(ax->xbuff);
243 ax->xbuff = NULL;
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 device *dev = ax->dev;
251 unsigned char *xbuff, *rbuff, *oxbuff, *orbuff;
252 int len;
253 unsigned long flags;
255 len = dev->mtu * 2;
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
260 * an MSS of 128
262 if (len < 576 * 2)
263 len = 576 * 2;
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",
270 ax->dev->name);
271 dev->mtu = ax->mtu;
272 if (xbuff != NULL)
273 kfree(xbuff);
274 if (rbuff != NULL)
275 kfree(rbuff);
276 return;
279 save_flags(flags);
280 cli();
282 oxbuff = ax->xbuff;
283 ax->xbuff = xbuff;
284 orbuff = ax->rbuff;
285 ax->rbuff = rbuff;
287 if (ax->xleft) {
288 if (ax->xleft <= len) {
289 memcpy(ax->xbuff, ax->xhead, ax->xleft);
290 } else {
291 ax->xleft = 0;
292 ax->tx_dropped++;
296 ax->xhead = ax->xbuff;
298 if (ax->rcount) {
299 if (ax->rcount <= len) {
300 memcpy(ax->rbuff, orbuff, ax->rcount);
301 } else {
302 ax->rcount = 0;
303 ax->rx_over_errors++;
304 set_bit(AXF_ERROR, &ax->flags);
308 ax->mtu = dev->mtu + 73;
309 ax->buffsize = len;
311 restore_flags(flags);
313 if (oxbuff != NULL)
314 kfree(oxbuff);
315 if (orbuff != NULL)
316 kfree(orbuff);
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;
339 struct sk_buff *skb;
340 struct mkiss_channel *mkiss;
341 int count;
343 tmp_ax = ax;
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)
349 tmp_ax = ax->mkiss;
350 } else if (ax->rbuff[0] & 0x20) {
351 ax->crcmode = CRC_MODE_FLEX;
352 if (check_crc_flex(ax->rbuff, ax->rcount) < 0) {
353 ax->rx_errors++;
354 return;
356 ax->rcount -= 2;
360 count = ax->rcount;
362 if ((skb = dev_alloc_skb(count)) == NULL) {
363 printk(KERN_ERR "mkiss: %s: memory squeeze, dropping packet.\n", ax->dev->name);
364 ax->rx_dropped++;
365 return;
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);
372 netif_rx(skb);
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)
379 unsigned char *p;
380 int actual, count;
381 struct mkiss_channel *mkiss = ax->tty->driver_data;
383 if (ax->mtu != ax->dev->mtu + 73) /* Someone has been ifconfigging */
384 ax_changedmtu(ax);
386 if (len > ax->mtu) { /* Sigh, shouldn't occur BUT ... */
387 len = ax->mtu;
388 printk(KERN_ERR "mkiss: %s: truncating oversized transmit packet!\n", ax->dev->name);
389 ax->tx_dropped++;
390 ax_unlock(ax);
391 return;
394 p = icp;
396 if (mkiss->magic != MKISS_DRIVER_MAGIC) {
397 switch (ax->crcmode) {
398 unsigned short crc;
400 case CRC_MODE_FLEX:
401 *p |= 0x20;
402 crc = calc_crc_flex(p, len);
403 count = kiss_esc_crc(p, (unsigned char *)ax->xbuff, crc, len+2);
404 break;
406 default:
407 count = kiss_esc(p, (unsigned char *)ax->xbuff, len);
408 break;
410 ax->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
411 actual = ax->tty->driver.write(ax->tty, 0, ax->xbuff, count);
412 ax->tx_packets++;
413 ax->dev->trans_start = jiffies;
414 ax->xleft = count - actual;
415 ax->xhead = ax->xbuff + actual;
416 } else {
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);
420 ax->tx_packets++;
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)
433 int actual;
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)
439 return;
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);
452 ax_unlock(ax);
453 mark_bh(NET_BH);
454 return;
457 actual = tty->driver.write(tty, 0, ax->xhead, ax->xleft);
458 ax->xleft -= actual;
459 ax->xhead += actual;
462 /* Encapsulate an AX.25 packet and kick it into a TTY queue. */
463 static int ax_xmit(struct sk_buff *skb, struct 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;
469 tmp_ax = NULL;
471 if (mkiss->magic == MKISS_DRIVER_MAGIC) {
472 if (skb->data[0] < 0x10)
473 skb->data[0] = skb->data[0] + 0x10;
474 tmp_ax = ax->mkiss;
477 if (!dev->start) {
478 printk(KERN_ERR "mkiss: %s: xmit call when iface is down\n", dev->name);
479 return 1;
482 if (tmp_ax != NULL)
483 if (tmp_ax->dev->tbusy)
484 return 1;
486 if (tmp_ax != NULL)
487 if (dev->tbusy) {
488 printk(KERN_ERR "mkiss: dev busy while serial dev is free\n");
489 ax_unlock(ax);
492 if (dev->tbusy) {
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 */
499 return 1;
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");
506 ax->xleft = 0;
507 ax->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
508 ax_unlock(ax);
511 /* We were not busy, so we are now... :-) */
512 if (skb != NULL) {
513 ax_lock(ax);
514 if (tmp_ax != NULL)
515 ax_lock(tmp_ax);
516 ax_encaps(ax, skb->data, skb->len);
517 kfree_skb(skb);
520 return 0;
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 device *dev, unsigned short type,
527 void *daddr, void *saddr, unsigned len)
529 #ifdef CONFIG_INET
530 if (type != htons(ETH_P_AX25))
531 return ax25_encapsulate(skb, dev, type, daddr, saddr, len);
532 #endif
533 return 0;
537 static int ax_rebuild_header(struct sk_buff *skb)
539 #ifdef CONFIG_INET
540 return ax25_rebuild_header(skb);
541 #else
542 return 0;
543 #endif
546 #endif /* CONFIG_{AX25,AX25_MODULE} */
548 /* Open the low-level part of the AX25 channel. Easy! */
549 static int ax_open(struct device *dev)
551 struct ax_disp *ax = (struct ax_disp*)dev->priv;
552 unsigned long len;
554 if (ax->tty == NULL)
555 return -ENODEV;
558 * Allocate the frame buffers:
560 * rbuff Receive buffer.
561 * xbuff Transmit buffer.
562 * cbuff Temporary compression buffer.
564 len = dev->mtu * 2;
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
569 * an MSS of 128
571 if (len < 576 * 2)
572 len = 576 * 2;
574 if ((ax->rbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
575 goto norbuff;
577 if ((ax->xbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
578 goto noxbuff;
580 ax->mtu = dev->mtu + 73;
581 ax->buffsize = len;
582 ax->rcount = 0;
583 ax->xleft = 0;
585 ax->flags &= (1 << AXF_INUSE); /* Clear ESCAPE & ERROR flags */
586 dev->tbusy = 0;
587 dev->start = 1;
589 return 0;
591 /* Cleanup */
592 kfree(ax->xbuff);
594 noxbuff:
595 kfree(ax->rbuff);
597 norbuff:
598 return -ENOMEM;
602 /* Close the low-level part of the AX25 channel. Easy! */
603 static int ax_close(struct device *dev)
605 struct ax_disp *ax = (struct ax_disp*)dev->priv;
607 if (ax->tty == NULL)
608 return -EBUSY;
610 ax->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
612 dev->tbusy = 1;
613 dev->start = 0;
615 return 0;
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)
634 return;
637 * Argh! mtu change time! - costs us the packet part received
638 * at the change
640 if (ax->mtu != ax->dev->mtu + 73)
641 ax_changedmtu(ax);
643 /* Read the characters out of the buffer */
644 while (count--) {
645 if (fp != NULL && *fp++) {
646 if (!test_and_set_bit(AXF_ERROR, &ax->flags))
647 ax->rx_errors++;
648 cp++;
649 continue;
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;
661 int err, cnt;
663 /* First make sure we're not already connected. */
664 if (ax && ax->magic == AX25_MAGIC)
665 return -EEXIST;
667 /* OK. Find a free AX25 channel to use. */
668 if ((ax = ax_alloc()) == NULL)
669 return -ENFILE;
671 ax->tty = tty;
672 tty->disc_data = ax;
674 ax->mkiss = NULL;
675 tmp_ax = 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)))
687 return err;
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) {
696 cnt--;
697 tmp_ax = &ax25_ctrls[cnt]->ctrl;
698 break;
705 if (tmp_ax != NULL) {
706 ax->mkiss = tmp_ax;
707 tmp_ax->mkiss = ax;
710 MOD_INC_USE_COUNT;
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;
719 int mkiss ;
721 /* First make sure we're connected. */
722 if (ax == NULL || ax->magic != AX25_MAGIC)
723 return;
725 mkiss = ax->mode;
727 dev_close(ax->dev);
729 tty->disc_data = 0;
730 ax->tty = NULL;
732 /* VSV = very important to remove timers */
733 ax_free(ax);
734 unregister_netdev(ax->dev);
736 MOD_DEC_USE_COUNT;
740 static struct net_device_stats *ax_get_stats(struct 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;
755 return &stats;
759 /************************************************************************
760 * STANDARD ENCAPSULATION *
761 ************************************************************************/
763 int kiss_esc(unsigned char *s, unsigned char *d, int len)
765 unsigned char *ptr = d;
766 unsigned char c;
769 * Send an initial END character to flush out any
770 * data that may have accumulated in the receiver
771 * due to line noise.
774 *ptr++ = END;
776 while (len-- > 0) {
777 switch (c = *s++) {
778 case END:
779 *ptr++ = ESC;
780 *ptr++ = ESC_END;
781 break;
782 case ESC:
783 *ptr++ = ESC;
784 *ptr++ = ESC_ESC;
785 break;
786 default:
787 *ptr++ = c;
788 break;
792 *ptr++ = END;
794 return ptr - d;
798 * MW:
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;
805 unsigned char c;
807 *ptr++ = END;
808 while (len > 0) {
809 if (len > 2)
810 c = *s++;
811 else if (len > 1)
812 c = crc >> 8;
813 else if (len > 0)
814 c = crc & 0xff;
816 len--;
818 switch (c) {
819 case END:
820 *ptr++ = ESC;
821 *ptr++ = ESC_END;
822 break;
823 case ESC:
824 *ptr++ = ESC;
825 *ptr++ = ESC_ESC;
826 break;
827 default:
828 *ptr++ = c;
829 break;
832 *ptr++ = END;
833 return ptr - d;
836 static void kiss_unesc(struct ax_disp *ax, unsigned char s)
838 switch (s) {
839 case END:
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))
845 ax_bump(ax);
847 clear_bit(AXF_ESCAPE, &ax->flags);
848 ax->rcount = 0;
849 return;
851 case ESC:
852 set_bit(AXF_ESCAPE, &ax->flags);
853 return;
854 case ESC_ESC:
855 if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
856 s = ESC;
857 break;
858 case ESC_END:
859 if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
860 s = END;
861 break;
864 if (!test_bit(AXF_ERROR, &ax->flags)) {
865 if (ax->rcount < ax->buffsize) {
866 ax->rbuff[ax->rcount++] = s;
867 return;
870 ax->rx_over_errors++;
871 set_bit(AXF_ERROR, &ax->flags);
876 int ax_set_mac_address(struct device *dev, void *addr)
878 if (copy_from_user(dev->dev_addr, addr, AX25_ADDR_LEN))
879 return -EFAULT;
880 return 0;
883 static int ax_set_dev_mac_address(struct device *dev, void *addr)
885 struct sockaddr *sa = addr;
887 memcpy(dev->dev_addr, sa->sa_data, AX25_ADDR_LEN);
889 return 0;
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;
897 unsigned int tmp;
899 /* First make sure we're connected. */
900 if (ax == NULL || ax->magic != AX25_MAGIC)
901 return -EINVAL;
903 switch (cmd) {
904 case SIOCGIFNAME:
905 if (copy_to_user(arg, ax->dev->name, strlen(ax->dev->name) + 1))
906 return -EFAULT;
907 return 0;
909 case SIOCGIFENCAP:
910 put_user(4, (int *)arg);
911 return 0;
913 case SIOCSIFENCAP:
914 get_user(tmp, (int *)arg);
915 ax->mode = tmp;
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;
919 return 0;
921 case SIOCSIFHWADDR:
922 return ax_set_mac_address(ax->dev, arg);
924 default:
925 return -ENOIOCTLCMD;
929 static int ax_open_dev(struct device *dev)
931 struct ax_disp *ax = (struct ax_disp*)dev->priv;
933 if (ax->tty==NULL)
934 return -ENODEV;
936 return 0;
939 /* Initialize AX25 control device -- register AX25 line discipline */
940 int __init mkiss_init_ctrl_dev(void)
942 int status;
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");
948 return -ENOMEM;
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";
958 ax_ldisc.flags = 0;
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);
973 mkiss_init();
975 #ifdef MODULE
976 return status;
977 #else
979 * Return "not found", so that dev_init() will unlink
980 * the placeholder device entry for us.
982 return ENODEV;
983 #endif
987 /* Initialize the driver. Called by network startup. */
989 static int ax25_init(struct 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 ?? */
999 return -ENODEV;
1001 /* Set up the "AX25 Control Block". (And clear statistics) */
1002 memset(ax, 0, sizeof (struct ax_disp));
1003 ax->magic = AX25_MAGIC;
1004 ax->dev = dev;
1006 /* Finish setting up the DEVICE info. */
1007 dev->mtu = AX_MTU;
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;
1014 #endif
1015 dev->hard_header_len = 0;
1016 dev->addr_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;
1026 #endif
1028 dev_init_buffers(dev);
1030 /* New-style flags. */
1031 dev->flags = 0;
1033 return 0;
1036 static int mkiss_open(struct tty_struct *tty, struct file *filp)
1038 struct mkiss_channel *mkiss;
1039 int chan;
1041 chan = MINOR(tty->device) - tty->driver.minor_start;
1043 if (chan < 0 || chan >= NR_MKISS)
1044 return -ENODEV;
1046 mkiss = &MKISS_Info[chan];
1048 mkiss->magic = MKISS_DRIVER_MAGIC;
1049 mkiss->init = 1;
1050 mkiss->tty = tty;
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;
1058 return 0;
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)
1066 return;
1068 mkiss->tty = NULL;
1069 mkiss->init = 0;
1070 tty->stopped = 0;
1073 static int mkiss_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
1075 return 0;
1078 static int mkiss_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1080 /* Ignore serial ioctl's */
1081 switch (cmd) {
1082 case TCSBRK:
1083 case TIOCMGET:
1084 case TIOCMBIS:
1085 case TIOCMBIC:
1086 case TIOCMSET:
1087 case TCSETS:
1088 case TCSETSF: /* should flush first, but... */
1089 case TCSETSW: /* should wait until flush, but... */
1090 return 0;
1091 default:
1092 return -ENOIOCTLCMD;
1097 static void mkiss_dummy(struct tty_struct *tty)
1099 struct mkiss_channel *mkiss = tty->driver_data;
1101 if (tty == NULL)
1102 return;
1104 if (mkiss == NULL)
1105 return;
1108 static void mkiss_dummy2(struct tty_struct *tty, unsigned char ch)
1110 struct mkiss_channel *mkiss = tty->driver_data;
1112 if (tty == NULL)
1113 return;
1115 if (mkiss == NULL)
1116 return;
1120 static int mkiss_write_room(struct tty_struct * tty)
1122 struct mkiss_channel *mkiss = tty->driver_data;
1124 if (tty == NULL)
1125 return 0;
1127 if (mkiss == NULL)
1128 return 0;
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;
1138 if (tty == NULL)
1139 return 0;
1141 if (mkiss == NULL)
1142 return 0;
1144 return 0;
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");
1199 return -EIO;
1202 printk(KERN_INFO "AX.25 Multikiss device enabled\n");
1204 return 0;
1207 #ifdef MODULE
1208 EXPORT_NO_SYMBOLS;
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)
1223 int i;
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;
1240 kfree(ax25_ctrls);
1241 ax25_ctrls = 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");
1251 #endif /* MODULE */