Import 2.1.36
[davej-history.git] / drivers / net / mkiss.c
blob6f772fd68feb970adba0e7077f72c51e3e149a2c
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 seperated 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 Hansa@cuci.nl
20 * History
21 * Jonathan (G4KLX) Fixed to match Linux networking changes - 2.1.15.
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <asm/system.h>
27 #include <asm/segment.h>
28 #include <asm/bitops.h>
29 #include <asm/uaccess.h>
30 #include <linux/string.h>
31 #include <linux/mm.h>
32 #include <linux/interrupt.h>
33 #include <linux/in.h>
34 #include <linux/tty.h>
35 #include <linux/errno.h>
36 #include <linux/netdevice.h>
37 #include <linux/major.h>
38 #include <linux/init.h>
40 #include <linux/timer.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
44 #include <linux/if_arp.h>
46 #include <net/ax25.h>
48 #include "mkiss.h"
50 #ifdef CONFIG_INET
51 #include <linux/ip.h>
52 #include <linux/tcp.h>
53 #endif
55 #ifdef MODULE
56 #define AX25_VERSION "AX25-MODULAR-NET3.019-NEWTTY"
57 #define min(a,b) (a < b ? a : b)
58 #else
59 #define AX25_VERSION "AX25-NET3.019-NEWTTY"
60 #endif
62 #define NR_MKISS 4
63 #define MKISS_SERIAL_TYPE_NORMAL 1
65 struct mkiss_channel {
66 int magic; /* magic word */
67 int init; /* channel exists? */
68 struct tty_struct *tty; /* link to tty control structure */
71 typedef struct ax25_ctrl {
72 char if_name[8]; /* "ax0\0" .. "ax99999\0" */
73 struct ax_disp ctrl; /* */
74 struct device dev; /* the device */
75 } ax25_ctrl_t;
77 static ax25_ctrl_t **ax25_ctrls = NULL;
78 int ax25_maxdev = AX25_MAXDEV; /* Can be overridden with insmod! */
79 MODULE_PARM(ax25_maxdev, "i");
81 static struct tty_ldisc ax_ldisc;
82 static struct tty_driver mkiss_driver;
83 static int mkiss_refcount;
84 static struct tty_struct *mkiss_table[NR_MKISS];
85 static struct termios *mkiss_termios[NR_MKISS];
86 static struct termios *mkiss_termios_locked[NR_MKISS];
87 struct mkiss_channel MKISS_Info[NR_MKISS];
89 static int ax25_init(struct device *);
90 static int mkiss_init(void);
91 static int mkiss_write(struct tty_struct *, int, const unsigned char *, int);
92 static int kiss_esc(unsigned char *, unsigned char *, int);
93 static void kiss_unesc(struct ax_disp *, unsigned char);
95 /* Find a free channel, and link in this `tty' line. */
96 static inline struct ax_disp *ax_alloc(void)
98 ax25_ctrl_t *axp;
99 int i;
101 if (ax25_ctrls == NULL) /* Master array missing ! */
102 return NULL;
104 for (i = 0; i < ax25_maxdev; i++) {
105 axp = ax25_ctrls[i];
107 /* Not allocated ? */
108 if (axp == NULL)
109 break;
111 /* Not in use ? */
112 if (!set_bit(AXF_INUSE, &axp->ctrl.flags))
113 break;
116 /* Sorry, too many, all slots in use */
117 if (i >= ax25_maxdev)
118 return NULL;
120 /* If no channels are available, allocate one */
121 if (axp == NULL && (ax25_ctrls[i] = (ax25_ctrl_t *)kmalloc(sizeof(ax25_ctrl_t), GFP_KERNEL)) != NULL) {
122 axp = ax25_ctrls[i];
123 memset(axp, 0, sizeof(ax25_ctrl_t));
125 /* Initialize channel control data */
126 set_bit(AXF_INUSE, &axp->ctrl.flags);
127 sprintf(axp->if_name, "ax%d", i++);
128 axp->ctrl.tty = NULL;
129 axp->dev.name = axp->if_name;
130 axp->dev.base_addr = i;
131 axp->dev.priv = (void *)&axp->ctrl;
132 axp->dev.next = NULL;
133 axp->dev.init = ax25_init;
136 if (axp != NULL) {
138 * register device so that it can be ifconfig'ed
139 * ax25_init() will be called as a side-effect
140 * SIDE-EFFECT WARNING: ax25_init() CLEARS axp->ctrl !
142 if (register_netdev(&axp->dev) == 0) {
143 /* (Re-)Set the INUSE bit. Very Important! */
144 set_bit(AXF_INUSE, &axp->ctrl.flags);
145 axp->ctrl.dev = &axp->dev;
146 axp->dev.priv = (void *)&axp->ctrl;
148 return &axp->ctrl;
149 } else {
150 clear_bit(AXF_INUSE,&axp->ctrl.flags);
151 printk(KERN_ERR "ax_alloc() - register_netdev() failure.\n");
155 return NULL;
158 /* Free an AX25 channel. */
159 static inline void ax_free(struct ax_disp *ax)
161 /* Free all AX25 frame buffers. */
162 if (ax->rbuff)
163 kfree(ax->rbuff);
164 ax->rbuff = NULL;
165 if (ax->xbuff)
166 kfree(ax->xbuff);
167 ax->xbuff = NULL;
168 if (!clear_bit(AXF_INUSE, &ax->flags))
169 printk(KERN_ERR "%s: ax_free for already free unit.\n", ax->dev->name);
172 static void ax_changedmtu(struct ax_disp *ax)
174 struct device *dev = ax->dev;
175 unsigned char *xbuff, *rbuff, *oxbuff, *orbuff;
176 int len;
177 unsigned long flags;
179 len = dev->mtu * 2;
182 * allow for arrival of larger UDP packets, even if we say not to
183 * also fixes a bug in which SunOS sends 512-byte packets even with
184 * an MSS of 128
186 if (len < 576 * 2)
187 len = 576 * 2;
189 xbuff = (unsigned char *)kmalloc(len + 4, GFP_ATOMIC);
190 rbuff = (unsigned char *)kmalloc(len + 4, GFP_ATOMIC);
192 if (xbuff == NULL || rbuff == NULL) {
193 printk(KERN_ERR "%s: unable to grow ax25 buffers, MTU change cancelled.\n",
194 ax->dev->name);
195 dev->mtu = ax->mtu;
196 if (xbuff != NULL)
197 kfree(xbuff);
198 if (rbuff != NULL)
199 kfree(rbuff);
200 return;
203 save_flags(flags);
204 cli();
206 oxbuff = ax->xbuff;
207 ax->xbuff = xbuff;
208 orbuff = ax->rbuff;
209 ax->rbuff = rbuff;
211 if (ax->xleft) {
212 if (ax->xleft <= len) {
213 memcpy(ax->xbuff, ax->xhead, ax->xleft);
214 } else {
215 ax->xleft = 0;
216 ax->tx_dropped++;
220 ax->xhead = ax->xbuff;
222 if (ax->rcount) {
223 if (ax->rcount <= len) {
224 memcpy(ax->rbuff, orbuff, ax->rcount);
225 } else {
226 ax->rcount = 0;
227 ax->rx_over_errors++;
228 set_bit(AXF_ERROR, &ax->flags);
232 ax->mtu = dev->mtu + 73;
233 ax->buffsize = len;
235 restore_flags(flags);
237 if (oxbuff != NULL)
238 kfree(oxbuff);
239 if (orbuff != NULL)
240 kfree(orbuff);
244 /* Set the "sending" flag. This must be atomic, hence the ASM. */
245 static inline void ax_lock(struct ax_disp *ax)
247 if (set_bit(0, (void *)&ax->dev->tbusy))
248 printk(KERN_ERR "%s: trying to lock already locked device!\n", ax->dev->name);
252 /* Clear the "sending" flag. This must be atomic, hence the ASM. */
253 static inline void ax_unlock(struct ax_disp *ax)
255 if (!clear_bit(0, (void *)&ax->dev->tbusy))
256 printk(KERN_ERR "%s: trying to unlock already unlocked device!\n", ax->dev->name);
259 /* Send one completely decapsulated AX.25 packet to the AX.25 layer. */
260 static void ax_bump(struct ax_disp *ax)
262 struct ax_disp *tmp_ax;
263 struct sk_buff *skb;
264 struct mkiss_channel *mkiss;
265 int count;
267 tmp_ax = ax;
269 if (ax->rbuff[0] > 0x0f) {
270 if (ax->mkiss != NULL) {
271 mkiss= ax->mkiss->tty->driver_data;
272 if (mkiss->magic == MKISS_DRIVER_MAGIC)
273 tmp_ax = ax->mkiss;
277 count = ax->rcount;
279 if ((skb = dev_alloc_skb(count)) == NULL) {
280 printk(KERN_ERR "%s: memory squeeze, dropping packet.\n", ax->dev->name);
281 ax->rx_dropped++;
282 return;
285 skb->dev = tmp_ax->dev;
286 memcpy(skb_put(skb,count), ax->rbuff, count);
287 skb->mac.raw = skb->data;
288 skb->protocol = htons(ETH_P_AX25);
289 netif_rx(skb);
290 tmp_ax->rx_packets++;
293 /* Encapsulate one AX.25 packet and stuff into a TTY queue. */
294 static void ax_encaps(struct ax_disp *ax, unsigned char *icp, int len)
296 unsigned char *p;
297 int actual, count;
298 struct mkiss_channel *mkiss = ax->tty->driver_data;
300 if (ax->mtu != ax->dev->mtu + 73) /* Someone has been ifconfigging */
301 ax_changedmtu(ax);
303 if (len > ax->mtu) { /* Sigh, shouldn't occur BUT ... */
304 len = ax->mtu;
305 printk(KERN_ERR "%s: truncating oversized transmit packet!\n", ax->dev->name);
306 ax->tx_dropped++;
307 ax_unlock(ax);
308 return;
311 p = icp;
313 if (mkiss->magic != MKISS_DRIVER_MAGIC) {
314 count = kiss_esc(p, (unsigned char *)ax->xbuff, len);
315 ax->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
316 actual = ax->tty->driver.write(ax->tty, 0, ax->xbuff, count);
317 ax->tx_packets++;
318 ax->dev->trans_start = jiffies;
319 ax->xleft = count - actual;
320 ax->xhead = ax->xbuff + actual;
321 } else {
322 count = kiss_esc(p, (unsigned char *) ax->mkiss->xbuff, len);
323 ax->mkiss->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
324 actual = ax->mkiss->tty->driver.write(ax->mkiss->tty, 0, ax->mkiss->xbuff, count);
325 ax->tx_packets++;
326 ax->mkiss->dev->trans_start = jiffies;
327 ax->mkiss->xleft = count - actual;
328 ax->mkiss->xhead = ax->mkiss->xbuff + actual;
333 * Called by the driver when there's room for more data. If we have
334 * more packets to send, we send them here.
336 static void ax25_write_wakeup(struct tty_struct *tty)
338 int actual;
339 struct ax_disp *ax = (struct ax_disp *)tty->disc_data;
340 struct mkiss_channel *mkiss;
342 /* First make sure we're connected. */
343 if (ax == NULL || ax->magic != AX25_MAGIC || !ax->dev->start)
344 return;
345 if (ax->xleft <= 0) {
346 /* Now serial buffer is almost free & we can start
347 * transmission of another packet
349 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
351 if (ax->mkiss != NULL) {
352 mkiss= ax->mkiss->tty->driver_data;
353 if (mkiss->magic == MKISS_DRIVER_MAGIC)
354 ax_unlock(ax->mkiss);
357 ax_unlock(ax);
358 mark_bh(NET_BH);
359 return;
362 actual = tty->driver.write(tty, 0, ax->xhead, ax->xleft);
363 ax->xleft -= actual;
364 ax->xhead += actual;
367 /* Encapsulate an AX.25 packet and kick it into a TTY queue. */
368 static int ax_xmit(struct sk_buff *skb, struct device *dev)
370 struct ax_disp *ax = (struct ax_disp*)dev->priv;
371 struct mkiss_channel *mkiss = ax->tty->driver_data;
372 struct ax_disp *tmp_ax;
374 tmp_ax = NULL;
376 if (mkiss->magic == MKISS_DRIVER_MAGIC) {
377 if (skb->data[0] < 0x10)
378 skb->data[0] = skb->data[0] + 0x10;
379 tmp_ax = ax->mkiss;
382 if (!dev->start) {
383 printk(KERN_ERR "%s: xmit call when iface is down\n", dev->name);
384 return 1;
387 if (tmp_ax != NULL)
388 if (tmp_ax->dev->tbusy)
389 return 1;
391 if (tmp_ax != NULL)
392 if (dev->tbusy) {
393 printk(KERN_ERR "mkiss: dev busy while serial dev is free\n");
394 ax_unlock(ax);
397 if (dev->tbusy) {
399 * May be we must check transmitter timeout here ?
400 * 14 Oct 1994 Dmitry Gorodchanin.
402 if (jiffies - dev->trans_start < 20 * HZ) {
403 /* 20 sec timeout not reached */
404 return 1;
407 printk(KERN_ERR "%s: transmit timed out, %s?\n", dev->name,
408 (ax->tty->driver.chars_in_buffer(ax->tty) || ax->xleft) ?
409 "bad line quality" : "driver error");
411 ax->xleft = 0;
412 ax->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
413 ax_unlock(ax);
416 /* We were not busy, so we are now... :-) */
417 if (skb != NULL) {
418 ax_lock(ax);
419 if (tmp_ax != NULL)
420 ax_lock(tmp_ax);
421 ax_encaps(ax, skb->data, skb->len);
422 dev_kfree_skb(skb, FREE_WRITE);
425 return 0;
428 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
430 /* Return the frame type ID */
431 static int ax_header(struct sk_buff *skb, struct device *dev, unsigned short type,
432 void *daddr, void *saddr, unsigned len)
434 #ifdef CONFIG_INET
435 if (type != htons(ETH_P_AX25))
436 return ax25_encapsulate(skb, dev, type, daddr, saddr, len);
437 #endif
438 return 0;
442 static int ax_rebuild_header(struct sk_buff *skb)
444 #ifdef CONFIG_INET
445 return ax25_rebuild_header(skb);
446 #else
447 return 0;
448 #endif
451 #endif /* CONFIG_{AX25,AX25_MODULE} */
453 /* Open the low-level part of the AX25 channel. Easy! */
454 static int ax_open(struct device *dev)
456 struct ax_disp *ax = (struct ax_disp*)dev->priv;
457 unsigned long len;
459 if (ax->tty == NULL)
460 return -ENODEV;
463 * Allocate the frame buffers:
465 * rbuff Receive buffer.
466 * xbuff Transmit buffer.
467 * cbuff Temporary compression buffer.
469 len = dev->mtu * 2;
472 * allow for arrival of larger UDP packets, even if we say not to
473 * also fixes a bug in which SunOS sends 512-byte packets even with
474 * an MSS of 128
476 if (len < 576 * 2)
477 len = 576 * 2;
479 if ((ax->rbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL)) == NULL)
480 goto norbuff;
482 if ((ax->xbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL)) == NULL)
483 goto noxbuff;
485 ax->mtu = dev->mtu + 73;
486 ax->buffsize = len;
487 ax->rcount = 0;
488 ax->xleft = 0;
490 ax->flags &= (1 << AXF_INUSE); /* Clear ESCAPE & ERROR flags */
491 /* Needed because address '0' is special */
492 if (dev->pa_addr == 0)
493 dev->pa_addr = ntohl(0xC0A80001);
494 dev->tbusy = 0;
495 dev->start = 1;
497 return 0;
499 /* Cleanup */
500 kfree(ax->xbuff);
502 noxbuff:
503 kfree(ax->rbuff);
505 norbuff:
506 return -ENOMEM;
510 /* Close the low-level part of the AX25 channel. Easy! */
511 static int ax_close(struct device *dev)
513 struct ax_disp *ax = (struct ax_disp*)dev->priv;
515 if (ax->tty == NULL)
516 return -EBUSY;
518 ax->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
520 dev->tbusy = 1;
521 dev->start = 0;
523 return 0;
526 static int ax25_receive_room(struct tty_struct *tty)
528 return 65536; /* We can handle an infinite amount of data. :-) */
532 * Handle the 'receiver data ready' interrupt.
533 * This function is called by the 'tty_io' module in the kernel when
534 * a block of data has been received, which can now be decapsulated
535 * and sent on to the AX.25 layer for further processing.
537 static void ax25_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
539 struct ax_disp *ax = (struct ax_disp *)tty->disc_data;
541 if (ax == NULL || ax->magic != AX25_MAGIC || !ax->dev->start)
542 return;
545 * Argh! mtu change time! - costs us the packet part received
546 * at the change
548 if (ax->mtu != ax->dev->mtu + 73)
549 ax_changedmtu(ax);
551 /* Read the characters out of the buffer */
552 while (count--) {
553 if (fp != NULL && *fp++) {
554 if (!set_bit(AXF_ERROR, &ax->flags))
555 ax->rx_errors++;
556 cp++;
557 continue;
560 kiss_unesc(ax, *cp++);
564 static int ax25_open(struct tty_struct *tty)
566 struct ax_disp *ax = (struct ax_disp *)tty->disc_data;
567 struct ax_disp *tmp_ax;
568 struct mkiss_channel *mkiss;
569 int err, cnt;
571 /* First make sure we're not already connected. */
572 if (ax && ax->magic == AX25_MAGIC)
573 return -EEXIST;
575 /* OK. Find a free AX25 channel to use. */
576 if ((ax = ax_alloc()) == NULL)
577 return -ENFILE;
579 ax->tty = tty;
580 tty->disc_data = ax;
582 ax->mkiss = NULL;
583 tmp_ax = NULL;
585 if (tty->driver.flush_buffer)
586 tty->driver.flush_buffer(tty);
587 if (tty->ldisc.flush_buffer)
588 tty->ldisc.flush_buffer(tty);
590 /* Restore default settings */
591 ax->dev->type = ARPHRD_AX25;
593 /* Perform the low-level AX25 initialization. */
594 if ((err = ax_open(ax->dev)))
595 return err;
597 mkiss= ax->tty->driver_data;
599 if (mkiss->magic == MKISS_DRIVER_MAGIC) {
600 for (cnt = 1; cnt < ax25_maxdev; cnt++) {
601 if (ax25_ctrls[cnt]) {
602 if (ax25_ctrls[cnt]->dev.start) {
603 if (ax == &ax25_ctrls[cnt]->ctrl) {
604 cnt--;
605 tmp_ax = &ax25_ctrls[cnt]->ctrl;
606 break;
613 if (tmp_ax != NULL) {
614 ax->mkiss = tmp_ax;
615 tmp_ax->mkiss = ax;
618 MOD_INC_USE_COUNT;
620 /* Done. We have linked the TTY line to a channel. */
621 return ax->dev->base_addr;
624 static void ax25_close(struct tty_struct *tty)
626 struct ax_disp *ax = (struct ax_disp *)tty->disc_data;
627 int mkiss ;
629 /* First make sure we're connected. */
630 if (ax == NULL || ax->magic != AX25_MAGIC)
631 return;
633 mkiss = ax->mode;
634 dev_close(ax->dev);
636 tty->disc_data = 0;
637 ax->tty = NULL;
639 /* VSV = very important to remove timers */
640 ax_free(ax);
641 unregister_netdev(ax->dev);
643 MOD_DEC_USE_COUNT;
647 static struct net_device_stats *ax_get_stats(struct device *dev)
649 static struct net_device_stats stats;
650 struct ax_disp *ax = (struct ax_disp*)dev->priv;
652 memset(&stats, 0, sizeof(struct net_device_stats));
654 stats.rx_packets = ax->rx_packets;
655 stats.tx_packets = ax->tx_packets;
656 stats.rx_dropped = ax->rx_dropped;
657 stats.tx_dropped = ax->tx_dropped;
658 stats.tx_errors = ax->tx_errors;
659 stats.rx_errors = ax->rx_errors;
660 stats.rx_over_errors = ax->rx_over_errors;
662 return &stats;
666 /************************************************************************
667 * STANDARD ENCAPSULATION *
668 ************************************************************************/
670 int kiss_esc(unsigned char *s, unsigned char *d, int len)
672 unsigned char *ptr = d;
673 unsigned char c;
676 * Send an initial END character to flush out any
677 * data that may have accumulated in the receiver
678 * due to line noise.
681 *ptr++ = END;
683 while (len-- > 0) {
684 switch (c = *s++) {
685 case END:
686 *ptr++ = ESC;
687 *ptr++ = ESC_END;
688 break;
689 case ESC:
690 *ptr++ = ESC;
691 *ptr++ = ESC_ESC;
692 break;
693 default:
694 *ptr++ = c;
695 break;
699 *ptr++ = END;
701 return ptr - d;
704 static void kiss_unesc(struct ax_disp *ax, unsigned char s)
706 switch (s) {
707 case END:
708 /* drop keeptest bit = VSV */
709 if (test_bit(AXF_KEEPTEST, &ax->flags))
710 clear_bit(AXF_KEEPTEST, &ax->flags);
712 if (!clear_bit(AXF_ERROR, &ax->flags) && (ax->rcount > 2))
713 ax_bump(ax);
715 clear_bit(AXF_ESCAPE, &ax->flags);
716 ax->rcount = 0;
717 return;
719 case ESC:
720 set_bit(AXF_ESCAPE, &ax->flags);
721 return;
722 case ESC_ESC:
723 if (clear_bit(AXF_ESCAPE, &ax->flags))
724 s = ESC;
725 break;
726 case ESC_END:
727 if (clear_bit(AXF_ESCAPE, &ax->flags))
728 s = END;
729 break;
732 if (!test_bit(AXF_ERROR, &ax->flags)) {
733 if (ax->rcount < ax->buffsize) {
734 ax->rbuff[ax->rcount++] = s;
735 return;
738 ax->rx_over_errors++;
739 set_bit(AXF_ERROR, &ax->flags);
744 int ax_set_mac_address(struct device *dev, void *addr)
746 int err;
748 if ((err = verify_area(VERIFY_READ, addr, AX25_ADDR_LEN)) != 0)
749 return err;
751 /* addr is an AX.25 shifted ASCII mac address */
752 copy_from_user(dev->dev_addr, addr, AX25_ADDR_LEN);
754 return 0;
757 static int ax_set_dev_mac_address(struct device *dev, void *addr)
759 struct sockaddr *sa = addr;
761 memcpy(dev->dev_addr, sa->sa_data, AX25_ADDR_LEN);
763 return 0;
767 /* Perform I/O control on an active ax25 channel. */
768 static int ax25_disp_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg)
770 struct ax_disp *ax = (struct ax_disp *)tty->disc_data;
771 int err;
772 unsigned int tmp;
774 /* First make sure we're connected. */
775 if (ax == NULL || ax->magic != AX25_MAGIC)
776 return -EINVAL;
778 switch (cmd) {
779 case SIOCGIFNAME:
780 if ((err = verify_area(VERIFY_WRITE, arg, strlen(ax->dev->name) + 1)) != 0)
781 return err;
782 copy_to_user(arg, ax->dev->name, strlen(ax->dev->name) + 1);
783 return 0;
785 case SIOCGIFENCAP:
786 if ((err = verify_area(VERIFY_WRITE, arg, sizeof(int))) != 0)
787 return err;
788 put_user(4, (int *)arg);
789 return 0;
791 case SIOCSIFENCAP:
792 if ((err = verify_area(VERIFY_READ, arg, sizeof(int))) != 0)
793 return err;
794 get_user(tmp, (int *)arg);
795 ax->mode = tmp;
796 ax->dev->addr_len = AX25_ADDR_LEN; /* sizeof an AX.25 addr */
797 ax->dev->hard_header_len = AX25_KISS_HEADER_LEN + AX25_MAX_HEADER_LEN + 3;
798 ax->dev->type = ARPHRD_AX25;
799 return 0;
801 case SIOCSIFHWADDR:
802 return ax_set_mac_address(ax->dev, arg);
804 default:
805 return -ENOIOCTLCMD;
809 static int ax_open_dev(struct device *dev)
811 struct ax_disp *ax = (struct ax_disp*)dev->priv;
813 if (ax->tty==NULL)
814 return -ENODEV;
816 return 0;
819 /* Initialize AX25 control device -- register AX25 line discipline */
820 __initfunc(int mkiss_init_ctrl_dev(void))
822 int status;
824 if (ax25_maxdev < 4) ax25_maxdev = 4; /* Sanity */
826 if ((ax25_ctrls = (ax25_ctrl_t **)kmalloc(sizeof(void*) * ax25_maxdev, GFP_KERNEL)) == NULL) {
827 printk(KERN_ERR "mkiss: Can't allocate ax25_ctrls[] array ! No mkiss available\n");
828 return -ENOMEM;
831 /* Clear the pointer array, we allocate devices when we need them */
832 memset(ax25_ctrls, 0, sizeof(void*) * ax25_maxdev); /* Pointers */
834 /* Fill in our line protocol discipline, and register it */
835 memset(&ax_ldisc, 0, sizeof(ax_ldisc));
836 ax_ldisc.magic = TTY_LDISC_MAGIC;
837 ax_ldisc.name = "mkiss";
838 ax_ldisc.flags = 0;
839 ax_ldisc.open = ax25_open;
840 ax_ldisc.close = ax25_close;
841 ax_ldisc.read = NULL;
842 ax_ldisc.write = NULL;
843 ax_ldisc.ioctl = (int (*)(struct tty_struct *, struct file *, unsigned int, unsigned long))ax25_disp_ioctl;
844 ax_ldisc.poll = NULL;
846 ax_ldisc.receive_buf = ax25_receive_buf;
847 ax_ldisc.receive_room = ax25_receive_room;
848 ax_ldisc.write_wakeup = ax25_write_wakeup;
850 if ((status = tty_register_ldisc(N_AX25, &ax_ldisc)) != 0)
851 printk(KERN_ERR "mkiss: can't register line discipline (err = %d)\n", status);
853 mkiss_init();
855 #ifdef MODULE
856 return status;
857 #else
859 * Return "not found", so that dev_init() will unlink
860 * the placeholder device entry for us.
862 return ENODEV;
863 #endif
867 /* Initialize the driver. Called by network startup. */
869 static int ax25_init(struct device *dev)
871 struct ax_disp *ax = (struct ax_disp*)dev->priv;
873 static char ax25_bcast[AX25_ADDR_LEN] =
874 {'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
875 static char ax25_test[AX25_ADDR_LEN] =
876 {'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
878 if (ax == NULL) /* Allocation failed ?? */
879 return -ENODEV;
881 /* Set up the "AX25 Control Block". (And clear statistics) */
882 memset(ax, 0, sizeof (struct ax_disp));
883 ax->magic = AX25_MAGIC;
884 ax->dev = dev;
886 /* Finish setting up the DEVICE info. */
887 dev->mtu = AX_MTU;
888 dev->hard_start_xmit = ax_xmit;
889 dev->open = ax_open_dev;
890 dev->stop = ax_close;
891 dev->get_stats = ax_get_stats;
892 #ifdef HAVE_SET_MAC_ADDR
893 dev->set_mac_address = ax_set_dev_mac_address;
894 #endif
895 dev->hard_header_len = 0;
896 dev->addr_len = 0;
897 dev->type = ARPHRD_AX25;
898 dev->tx_queue_len = 10;
900 memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
901 memcpy(dev->dev_addr, ax25_test, AX25_ADDR_LEN);
903 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
904 dev->hard_header = ax_header;
905 dev->rebuild_header = ax_rebuild_header;
906 #endif
908 dev_init_buffers(dev);
910 /* New-style flags. */
911 dev->flags = 0;
912 dev->family = AF_INET;
913 dev->pa_addr = 0;
914 dev->pa_brdaddr = 0;
915 dev->pa_mask = 0;
916 dev->pa_alen = 4;
918 return 0;
921 static int mkiss_open(struct tty_struct *tty, struct file *filp)
923 struct mkiss_channel *mkiss;
924 int chan;
926 chan = MINOR(tty->device) - tty->driver.minor_start;
928 if (chan < 0 || chan >= NR_MKISS)
929 return -ENODEV;
931 mkiss = &MKISS_Info[chan];
933 mkiss->magic = MKISS_DRIVER_MAGIC;
934 mkiss->init = 1;
935 mkiss->tty = tty;
937 tty->driver_data = mkiss;
939 tty->termios->c_iflag = IGNBRK | IGNPAR;
940 tty->termios->c_cflag = B9600 | CS8 | CLOCAL;
941 tty->termios->c_cflag &= ~CBAUD;
943 return 0;
946 static void mkiss_close(struct tty_struct *tty, struct file * filp)
948 struct mkiss_channel *mkiss = tty->driver_data;
950 if (mkiss == NULL || mkiss->magic != MKISS_DRIVER_MAGIC)
951 return;
953 mkiss->tty = NULL;
954 mkiss->init = 0;
955 tty->stopped = 0;
958 static int mkiss_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
960 return 0;
963 static int mkiss_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
965 /* Ignore serial ioctl's */
966 switch (cmd) {
967 case TCSBRK:
968 case TIOCMGET:
969 case TIOCMBIS:
970 case TIOCMBIC:
971 case TIOCMSET:
972 case TCSETS:
973 case TCSETSF: /* should flush first, but... */
974 case TCSETSW: /* should wait until flush, but... */
975 return 0;
976 default:
977 return -ENOIOCTLCMD;
982 static void mkiss_dummy(struct tty_struct *tty)
984 struct mkiss_channel *mkiss = tty->driver_data;
986 if (tty == NULL)
987 return;
989 if (mkiss == NULL)
990 return;
993 static void mkiss_dummy2(struct tty_struct *tty, unsigned char ch)
995 struct mkiss_channel *mkiss = tty->driver_data;
997 if (tty == NULL)
998 return;
1000 if (mkiss == NULL)
1001 return;
1005 static int mkiss_write_room(struct tty_struct * tty)
1007 struct mkiss_channel *mkiss = tty->driver_data;
1009 if (tty == NULL)
1010 return 0;
1012 if (mkiss == NULL)
1013 return 0;
1015 return 65536; /* We can handle an infinite amount of data. :-) */
1019 static int mkiss_chars_in_buffer(struct tty_struct *tty)
1021 struct mkiss_channel *mkiss = tty->driver_data;
1023 if (tty == NULL)
1024 return 0;
1026 if (mkiss == NULL)
1027 return 0;
1029 return 0;
1033 static void mkiss_set_termios(struct tty_struct *tty, struct termios *old_termios)
1035 /* we don't do termios */
1038 /* ******************************************************************** */
1039 /* * Init MKISS driver * */
1040 /* ******************************************************************** */
1042 __initfunc(static int mkiss_init(void))
1044 memset(&mkiss_driver, 0, sizeof(struct tty_driver));
1046 mkiss_driver.magic = MKISS_DRIVER_MAGIC;
1047 mkiss_driver.name = "mkiss";
1048 mkiss_driver.major = MKISS_MAJOR;
1049 mkiss_driver.minor_start = 0;
1050 mkiss_driver.num = NR_MKISS;
1051 mkiss_driver.type = TTY_DRIVER_TYPE_SERIAL;
1052 mkiss_driver.subtype = MKISS_SERIAL_TYPE_NORMAL; /* not needed */
1054 mkiss_driver.init_termios = tty_std_termios;
1055 mkiss_driver.init_termios.c_iflag = IGNBRK | IGNPAR;
1056 mkiss_driver.init_termios.c_cflag = B9600 | CS8 | CLOCAL;
1058 mkiss_driver.flags = TTY_DRIVER_REAL_RAW;
1059 mkiss_driver.refcount = &mkiss_refcount;
1060 mkiss_driver.table = mkiss_table;
1061 mkiss_driver.termios = (struct termios **)mkiss_termios;
1062 mkiss_driver.termios_locked = (struct termios **)mkiss_termios_locked;
1064 mkiss_driver.ioctl = mkiss_ioctl;
1065 mkiss_driver.open = mkiss_open;
1066 mkiss_driver.close = mkiss_close;
1067 mkiss_driver.write = mkiss_write;
1068 mkiss_driver.write_room = mkiss_write_room;
1069 mkiss_driver.chars_in_buffer = mkiss_chars_in_buffer;
1070 mkiss_driver.set_termios = mkiss_set_termios;
1072 /* some unused functions */
1073 mkiss_driver.flush_buffer = mkiss_dummy;
1074 mkiss_driver.throttle = mkiss_dummy;
1075 mkiss_driver.unthrottle = mkiss_dummy;
1076 mkiss_driver.stop = mkiss_dummy;
1077 mkiss_driver.start = mkiss_dummy;
1078 mkiss_driver.hangup = mkiss_dummy;
1079 mkiss_driver.flush_chars = mkiss_dummy;
1080 mkiss_driver.put_char = mkiss_dummy2;
1082 if (tty_register_driver(&mkiss_driver)) {
1083 printk(KERN_ERR "Couldn't register Mkiss device\n");
1084 return -EIO;
1087 printk(KERN_INFO "AX.25 Multikiss device enabled\n");
1089 return 0;
1092 #ifdef MODULE
1093 EXPORT_NO_SYMBOLS;
1095 int init_module(void)
1097 return mkiss_init_ctrl_dev();
1100 void cleanup_module(void)
1102 int i;
1104 if (ax25_ctrls != NULL) {
1105 for (i = 0; i < ax25_maxdev; i++) {
1106 if (ax25_ctrls[i]) {
1108 * VSV = if dev->start==0, then device
1109 * unregistred while close proc.
1111 if (ax25_ctrls[i]->dev.start)
1112 unregister_netdev(&(ax25_ctrls[i]->dev));
1114 kfree(ax25_ctrls[i]);
1115 ax25_ctrls[i] = NULL;
1119 kfree(ax25_ctrls);
1120 ax25_ctrls = NULL;
1123 if ((i = tty_register_ldisc(N_AX25, NULL)))
1124 printk(KERN_ERR "mkiss: can't unregister line discipline (err = %d)\n", i);
1126 if (tty_unregister_driver(&mkiss_driver)) /* remove devive */
1127 printk(KERN_ERR "mkiss: can't unregister MKISS device\n");
1130 #endif /* MODULE */