Linux 2.6.26-rc5
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / net / wan / x25_asy.c
blob249e18053d5fa4a2d6e29a60e33878bbe1f78c90
1 /*
2 * Things to sort out:
4 * o tbusy handling
5 * o allow users to set the parameters
6 * o sync/async switching ?
8 * Note: This does _not_ implement CCITT X.25 asynchronous framing
9 * recommendations. Its primarily for testing purposes. If you wanted
10 * to do CCITT then in theory all you need is to nick the HDLC async
11 * checksum routines from ppp.c
12 * Changes:
14 * 2000-10-29 Henner Eisen lapb_data_indication() return status.
17 #include <linux/module.h>
19 #include <asm/system.h>
20 #include <linux/uaccess.h>
21 #include <linux/bitops.h>
22 #include <linux/string.h>
23 #include <linux/mm.h>
24 #include <linux/interrupt.h>
25 #include <linux/in.h>
26 #include <linux/tty.h>
27 #include <linux/errno.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/if_arp.h>
32 #include <linux/x25.h>
33 #include <linux/lapb.h>
34 #include <linux/init.h>
35 #include "x25_asy.h"
37 #include <net/x25device.h>
39 static struct net_device **x25_asy_devs;
40 static int x25_asy_maxdev = SL_NRUNIT;
42 module_param(x25_asy_maxdev, int, 0);
43 MODULE_LICENSE("GPL");
45 static int x25_asy_esc(unsigned char *p, unsigned char *d, int len);
46 static void x25_asy_unesc(struct x25_asy *sl, unsigned char c);
47 static void x25_asy_setup(struct net_device *dev);
49 /* Find a free X.25 channel, and link in this `tty' line. */
50 static struct x25_asy *x25_asy_alloc(void)
52 struct net_device *dev = NULL;
53 struct x25_asy *sl;
54 int i;
56 if (x25_asy_devs == NULL)
57 return NULL; /* Master array missing ! */
59 for (i = 0; i < x25_asy_maxdev; i++) {
60 dev = x25_asy_devs[i];
62 /* Not allocated ? */
63 if (dev == NULL)
64 break;
66 sl = dev->priv;
67 /* Not in use ? */
68 if (!test_and_set_bit(SLF_INUSE, &sl->flags))
69 return sl;
73 /* Sorry, too many, all slots in use */
74 if (i >= x25_asy_maxdev)
75 return NULL;
77 /* If no channels are available, allocate one */
78 if (!dev) {
79 char name[IFNAMSIZ];
80 sprintf(name, "x25asy%d", i);
82 dev = alloc_netdev(sizeof(struct x25_asy),
83 name, x25_asy_setup);
84 if (!dev)
85 return NULL;
87 /* Initialize channel control data */
88 sl = dev->priv;
89 dev->base_addr = i;
91 /* register device so that it can be ifconfig'ed */
92 if (register_netdev(dev) == 0) {
93 /* (Re-)Set the INUSE bit. Very Important! */
94 set_bit(SLF_INUSE, &sl->flags);
95 x25_asy_devs[i] = dev;
96 return sl;
97 } else {
98 printk(KERN_WARNING "x25_asy_alloc() - register_netdev() failure.\n");
99 free_netdev(dev);
102 return NULL;
106 /* Free an X.25 channel. */
107 static void x25_asy_free(struct x25_asy *sl)
109 /* Free all X.25 frame buffers. */
110 kfree(sl->rbuff);
111 sl->rbuff = NULL;
112 kfree(sl->xbuff);
113 sl->xbuff = NULL;
115 if (!test_and_clear_bit(SLF_INUSE, &sl->flags))
116 printk(KERN_ERR "%s: x25_asy_free for already free unit.\n",
117 sl->dev->name);
120 static int x25_asy_change_mtu(struct net_device *dev, int newmtu)
122 struct x25_asy *sl = dev->priv;
123 unsigned char *xbuff, *rbuff;
124 int len = 2 * newmtu;
126 xbuff = kmalloc(len + 4, GFP_ATOMIC);
127 rbuff = kmalloc(len + 4, GFP_ATOMIC);
129 if (xbuff == NULL || rbuff == NULL) {
130 printk(KERN_WARNING "%s: unable to grow X.25 buffers, MTU change cancelled.\n",
131 dev->name);
132 kfree(xbuff);
133 kfree(rbuff);
134 return -ENOMEM;
137 spin_lock_bh(&sl->lock);
138 xbuff = xchg(&sl->xbuff, xbuff);
139 if (sl->xleft) {
140 if (sl->xleft <= len) {
141 memcpy(sl->xbuff, sl->xhead, sl->xleft);
142 } else {
143 sl->xleft = 0;
144 sl->stats.tx_dropped++;
147 sl->xhead = sl->xbuff;
149 rbuff = xchg(&sl->rbuff, rbuff);
150 if (sl->rcount) {
151 if (sl->rcount <= len) {
152 memcpy(sl->rbuff, rbuff, sl->rcount);
153 } else {
154 sl->rcount = 0;
155 sl->stats.rx_over_errors++;
156 set_bit(SLF_ERROR, &sl->flags);
160 dev->mtu = newmtu;
161 sl->buffsize = len;
163 spin_unlock_bh(&sl->lock);
165 kfree(xbuff);
166 kfree(rbuff);
167 return 0;
171 /* Set the "sending" flag. This must be atomic, hence the ASM. */
173 static inline void x25_asy_lock(struct x25_asy *sl)
175 netif_stop_queue(sl->dev);
179 /* Clear the "sending" flag. This must be atomic, hence the ASM. */
181 static inline void x25_asy_unlock(struct x25_asy *sl)
183 netif_wake_queue(sl->dev);
186 /* Send one completely decapsulated IP datagram to the IP layer. */
188 static void x25_asy_bump(struct x25_asy *sl)
190 struct sk_buff *skb;
191 int count;
192 int err;
194 count = sl->rcount;
195 sl->stats.rx_bytes += count;
197 skb = dev_alloc_skb(count+1);
198 if (skb == NULL) {
199 printk(KERN_WARNING "%s: memory squeeze, dropping packet.\n",
200 sl->dev->name);
201 sl->stats.rx_dropped++;
202 return;
204 skb_push(skb, 1); /* LAPB internal control */
205 memcpy(skb_put(skb, count), sl->rbuff, count);
206 skb->protocol = x25_type_trans(skb, sl->dev);
207 err = lapb_data_received(skb->dev, skb);
208 if (err != LAPB_OK) {
209 kfree_skb(skb);
210 printk(KERN_DEBUG "x25_asy: data received err - %d\n", err);
211 } else {
212 netif_rx(skb);
213 sl->dev->last_rx = jiffies;
214 sl->stats.rx_packets++;
218 /* Encapsulate one IP datagram and stuff into a TTY queue. */
219 static void x25_asy_encaps(struct x25_asy *sl, unsigned char *icp, int len)
221 unsigned char *p;
222 int actual, count, mtu = sl->dev->mtu;
224 if (len > mtu) {
225 /* Sigh, shouldn't occur BUT ... */
226 len = mtu;
227 printk(KERN_DEBUG "%s: truncating oversized transmit packet!\n",
228 sl->dev->name);
229 sl->stats.tx_dropped++;
230 x25_asy_unlock(sl);
231 return;
234 p = icp;
235 count = x25_asy_esc(p, (unsigned char *) sl->xbuff, len);
237 /* Order of next two lines is *very* important.
238 * When we are sending a little amount of data,
239 * the transfer may be completed inside driver.write()
240 * routine, because it's running with interrupts enabled.
241 * In this case we *never* got WRITE_WAKEUP event,
242 * if we did not request it before write operation.
243 * 14 Oct 1994 Dmitry Gorodchanin.
245 sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
246 actual = sl->tty->ops->write(sl->tty, sl->xbuff, count);
247 sl->xleft = count - actual;
248 sl->xhead = sl->xbuff + actual;
249 /* VSV */
250 clear_bit(SLF_OUTWAIT, &sl->flags); /* reset outfill flag */
254 * Called by the driver when there's room for more data. If we have
255 * more packets to send, we send them here.
257 static void x25_asy_write_wakeup(struct tty_struct *tty)
259 int actual;
260 struct x25_asy *sl = (struct x25_asy *) tty->disc_data;
262 /* First make sure we're connected. */
263 if (!sl || sl->magic != X25_ASY_MAGIC || !netif_running(sl->dev))
264 return;
266 if (sl->xleft <= 0) {
267 /* Now serial buffer is almost free & we can start
268 * transmission of another packet */
269 sl->stats.tx_packets++;
270 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
271 x25_asy_unlock(sl);
272 return;
275 actual = tty->ops->write(tty, sl->xhead, sl->xleft);
276 sl->xleft -= actual;
277 sl->xhead += actual;
280 static void x25_asy_timeout(struct net_device *dev)
282 struct x25_asy *sl = dev->priv;
284 spin_lock(&sl->lock);
285 if (netif_queue_stopped(dev)) {
286 /* May be we must check transmitter timeout here ?
287 * 14 Oct 1994 Dmitry Gorodchanin.
289 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
290 (tty_chars_in_buffer(sl->tty) || sl->xleft) ?
291 "bad line quality" : "driver error");
292 sl->xleft = 0;
293 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
294 x25_asy_unlock(sl);
296 spin_unlock(&sl->lock);
299 /* Encapsulate an IP datagram and kick it into a TTY queue. */
301 static int x25_asy_xmit(struct sk_buff *skb, struct net_device *dev)
303 struct x25_asy *sl = dev->priv;
304 int err;
306 if (!netif_running(sl->dev)) {
307 printk(KERN_ERR "%s: xmit call when iface is down\n",
308 dev->name);
309 kfree_skb(skb);
310 return 0;
313 switch (skb->data[0]) {
314 case 0x00:
315 break;
316 case 0x01: /* Connection request .. do nothing */
317 err = lapb_connect_request(dev);
318 if (err != LAPB_OK)
319 printk(KERN_ERR "x25_asy: lapb_connect_request error - %d\n", err);
320 kfree_skb(skb);
321 return 0;
322 case 0x02: /* Disconnect request .. do nothing - hang up ?? */
323 err = lapb_disconnect_request(dev);
324 if (err != LAPB_OK)
325 printk(KERN_ERR "x25_asy: lapb_disconnect_request error - %d\n", err);
326 default:
327 kfree_skb(skb);
328 return 0;
330 skb_pull(skb, 1); /* Remove control byte */
332 * If we are busy already- too bad. We ought to be able
333 * to queue things at this point, to allow for a little
334 * frame buffer. Oh well...
335 * -----------------------------------------------------
336 * I hate queues in X.25 driver. May be it's efficient,
337 * but for me latency is more important. ;)
338 * So, no queues !
339 * 14 Oct 1994 Dmitry Gorodchanin.
342 err = lapb_data_request(dev, skb);
343 if (err != LAPB_OK) {
344 printk(KERN_ERR "x25_asy: lapb_data_request error - %d\n", err);
345 kfree_skb(skb);
346 return 0;
348 return 0;
353 * LAPB interface boilerplate
357 * Called when I frame data arrives. We did the work above - throw it
358 * at the net layer.
361 static int x25_asy_data_indication(struct net_device *dev, struct sk_buff *skb)
363 skb->dev->last_rx = jiffies;
364 return netif_rx(skb);
368 * Data has emerged from the LAPB protocol machine. We don't handle
369 * busy cases too well. Its tricky to see how to do this nicely -
370 * perhaps lapb should allow us to bounce this ?
373 static void x25_asy_data_transmit(struct net_device *dev, struct sk_buff *skb)
375 struct x25_asy *sl = dev->priv;
377 spin_lock(&sl->lock);
378 if (netif_queue_stopped(sl->dev) || sl->tty == NULL) {
379 spin_unlock(&sl->lock);
380 printk(KERN_ERR "x25_asy: tbusy drop\n");
381 kfree_skb(skb);
382 return;
384 /* We were not busy, so we are now... :-) */
385 if (skb != NULL) {
386 x25_asy_lock(sl);
387 sl->stats.tx_bytes += skb->len;
388 x25_asy_encaps(sl, skb->data, skb->len);
389 dev_kfree_skb(skb);
391 spin_unlock(&sl->lock);
395 * LAPB connection establish/down information.
398 static void x25_asy_connected(struct net_device *dev, int reason)
400 struct x25_asy *sl = dev->priv;
401 struct sk_buff *skb;
402 unsigned char *ptr;
404 skb = dev_alloc_skb(1);
405 if (skb == NULL) {
406 printk(KERN_ERR "x25_asy: out of memory\n");
407 return;
410 ptr = skb_put(skb, 1);
411 *ptr = 0x01;
413 skb->protocol = x25_type_trans(skb, sl->dev);
414 netif_rx(skb);
415 sl->dev->last_rx = jiffies;
418 static void x25_asy_disconnected(struct net_device *dev, int reason)
420 struct x25_asy *sl = dev->priv;
421 struct sk_buff *skb;
422 unsigned char *ptr;
424 skb = dev_alloc_skb(1);
425 if (skb == NULL) {
426 printk(KERN_ERR "x25_asy: out of memory\n");
427 return;
430 ptr = skb_put(skb, 1);
431 *ptr = 0x02;
433 skb->protocol = x25_type_trans(skb, sl->dev);
434 netif_rx(skb);
435 sl->dev->last_rx = jiffies;
438 static struct lapb_register_struct x25_asy_callbacks = {
439 .connect_confirmation = x25_asy_connected,
440 .connect_indication = x25_asy_connected,
441 .disconnect_confirmation = x25_asy_disconnected,
442 .disconnect_indication = x25_asy_disconnected,
443 .data_indication = x25_asy_data_indication,
444 .data_transmit = x25_asy_data_transmit,
449 /* Open the low-level part of the X.25 channel. Easy! */
450 static int x25_asy_open(struct net_device *dev)
452 struct x25_asy *sl = dev->priv;
453 unsigned long len;
454 int err;
456 if (sl->tty == NULL)
457 return -ENODEV;
460 * Allocate the X.25 frame buffers:
462 * rbuff Receive buffer.
463 * xbuff Transmit buffer.
466 len = dev->mtu * 2;
468 sl->rbuff = kmalloc(len + 4, GFP_KERNEL);
469 if (sl->rbuff == NULL)
470 goto norbuff;
471 sl->xbuff = kmalloc(len + 4, GFP_KERNEL);
472 if (sl->xbuff == NULL)
473 goto noxbuff;
475 sl->buffsize = len;
476 sl->rcount = 0;
477 sl->xleft = 0;
478 sl->flags &= (1 << SLF_INUSE); /* Clear ESCAPE & ERROR flags */
480 netif_start_queue(dev);
483 * Now attach LAPB
485 err = lapb_register(dev, &x25_asy_callbacks);
486 if (err == LAPB_OK)
487 return 0;
489 /* Cleanup */
490 kfree(sl->xbuff);
491 noxbuff:
492 kfree(sl->rbuff);
493 norbuff:
494 return -ENOMEM;
498 /* Close the low-level part of the X.25 channel. Easy! */
499 static int x25_asy_close(struct net_device *dev)
501 struct x25_asy *sl = dev->priv;
502 int err;
504 spin_lock(&sl->lock);
505 if (sl->tty)
506 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
508 netif_stop_queue(dev);
509 sl->rcount = 0;
510 sl->xleft = 0;
511 err = lapb_unregister(dev);
512 if (err != LAPB_OK)
513 printk(KERN_ERR "x25_asy_close: lapb_unregister error -%d\n",
514 err);
515 spin_unlock(&sl->lock);
516 return 0;
520 * Handle the 'receiver data ready' interrupt.
521 * This function is called by the 'tty_io' module in the kernel when
522 * a block of X.25 data has been received, which can now be decapsulated
523 * and sent on to some IP layer for further processing.
526 static void x25_asy_receive_buf(struct tty_struct *tty,
527 const unsigned char *cp, char *fp, int count)
529 struct x25_asy *sl = (struct x25_asy *) tty->disc_data;
531 if (!sl || sl->magic != X25_ASY_MAGIC || !netif_running(sl->dev))
532 return;
535 /* Read the characters out of the buffer */
536 while (count--) {
537 if (fp && *fp++) {
538 if (!test_and_set_bit(SLF_ERROR, &sl->flags))
539 sl->stats.rx_errors++;
540 cp++;
541 continue;
543 x25_asy_unesc(sl, *cp++);
548 * Open the high-level part of the X.25 channel.
549 * This function is called by the TTY module when the
550 * X.25 line discipline is called for. Because we are
551 * sure the tty line exists, we only have to link it to
552 * a free X.25 channel...
555 static int x25_asy_open_tty(struct tty_struct *tty)
557 struct x25_asy *sl = (struct x25_asy *) tty->disc_data;
558 int err;
560 if (tty->ops->write == NULL)
561 return -EOPNOTSUPP;
563 /* First make sure we're not already connected. */
564 if (sl && sl->magic == X25_ASY_MAGIC)
565 return -EEXIST;
567 /* OK. Find a free X.25 channel to use. */
568 sl = x25_asy_alloc();
569 if (sl == NULL)
570 return -ENFILE;
572 sl->tty = tty;
573 tty->disc_data = sl;
574 tty->receive_room = 65536;
575 tty_driver_flush_buffer(tty);
576 tty_ldisc_flush(tty);
578 /* Restore default settings */
579 sl->dev->type = ARPHRD_X25;
581 /* Perform the low-level X.25 async init */
582 err = x25_asy_open(sl->dev);
583 if (err)
584 return err;
585 /* Done. We have linked the TTY line to a channel. */
586 return sl->dev->base_addr;
591 * Close down an X.25 channel.
592 * This means flushing out any pending queues, and then restoring the
593 * TTY line discipline to what it was before it got hooked to X.25
594 * (which usually is TTY again).
596 static void x25_asy_close_tty(struct tty_struct *tty)
598 struct x25_asy *sl = (struct x25_asy *) tty->disc_data;
600 /* First make sure we're connected. */
601 if (!sl || sl->magic != X25_ASY_MAGIC)
602 return;
604 if (sl->dev->flags & IFF_UP)
605 dev_close(sl->dev);
607 tty->disc_data = NULL;
608 sl->tty = NULL;
609 x25_asy_free(sl);
613 static struct net_device_stats *x25_asy_get_stats(struct net_device *dev)
615 struct x25_asy *sl = dev->priv;
616 return &sl->stats;
620 /************************************************************************
621 * STANDARD X.25 ENCAPSULATION *
622 ************************************************************************/
624 int x25_asy_esc(unsigned char *s, unsigned char *d, int len)
626 unsigned char *ptr = d;
627 unsigned char c;
630 * Send an initial END character to flush out any
631 * data that may have accumulated in the receiver
632 * due to line noise.
635 *ptr++ = X25_END; /* Send 10111110 bit seq */
638 * For each byte in the packet, send the appropriate
639 * character sequence, according to the X.25 protocol.
642 while (len-- > 0) {
643 switch (c = *s++) {
644 case X25_END:
645 *ptr++ = X25_ESC;
646 *ptr++ = X25_ESCAPE(X25_END);
647 break;
648 case X25_ESC:
649 *ptr++ = X25_ESC;
650 *ptr++ = X25_ESCAPE(X25_ESC);
651 break;
652 default:
653 *ptr++ = c;
654 break;
657 *ptr++ = X25_END;
658 return (ptr - d);
661 static void x25_asy_unesc(struct x25_asy *sl, unsigned char s)
664 switch (s) {
665 case X25_END:
666 if (!test_and_clear_bit(SLF_ERROR, &sl->flags)
667 && sl->rcount > 2)
668 x25_asy_bump(sl);
669 clear_bit(SLF_ESCAPE, &sl->flags);
670 sl->rcount = 0;
671 return;
672 case X25_ESC:
673 set_bit(SLF_ESCAPE, &sl->flags);
674 return;
675 case X25_ESCAPE(X25_ESC):
676 case X25_ESCAPE(X25_END):
677 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))
678 s = X25_UNESCAPE(s);
679 break;
681 if (!test_bit(SLF_ERROR, &sl->flags)) {
682 if (sl->rcount < sl->buffsize) {
683 sl->rbuff[sl->rcount++] = s;
684 return;
686 sl->stats.rx_over_errors++;
687 set_bit(SLF_ERROR, &sl->flags);
692 /* Perform I/O control on an active X.25 channel. */
693 static int x25_asy_ioctl(struct tty_struct *tty, struct file *file,
694 unsigned int cmd, unsigned long arg)
696 struct x25_asy *sl = (struct x25_asy *) tty->disc_data;
698 /* First make sure we're connected. */
699 if (!sl || sl->magic != X25_ASY_MAGIC)
700 return -EINVAL;
702 switch (cmd) {
703 case SIOCGIFNAME:
704 if (copy_to_user((void __user *)arg, sl->dev->name,
705 strlen(sl->dev->name) + 1))
706 return -EFAULT;
707 return 0;
708 case SIOCSIFHWADDR:
709 return -EINVAL;
710 default:
711 return tty_mode_ioctl(tty, file, cmd, arg);
715 static int x25_asy_open_dev(struct net_device *dev)
717 struct x25_asy *sl = dev->priv;
718 if (sl->tty == NULL)
719 return -ENODEV;
720 return 0;
723 /* Initialise the X.25 driver. Called by the device init code */
724 static void x25_asy_setup(struct net_device *dev)
726 struct x25_asy *sl = dev->priv;
728 sl->magic = X25_ASY_MAGIC;
729 sl->dev = dev;
730 spin_lock_init(&sl->lock);
731 set_bit(SLF_INUSE, &sl->flags);
734 * Finish setting up the DEVICE info.
737 dev->mtu = SL_MTU;
738 dev->hard_start_xmit = x25_asy_xmit;
739 dev->tx_timeout = x25_asy_timeout;
740 dev->watchdog_timeo = HZ*20;
741 dev->open = x25_asy_open_dev;
742 dev->stop = x25_asy_close;
743 dev->get_stats = x25_asy_get_stats;
744 dev->change_mtu = x25_asy_change_mtu;
745 dev->hard_header_len = 0;
746 dev->addr_len = 0;
747 dev->type = ARPHRD_X25;
748 dev->tx_queue_len = 10;
750 /* New-style flags. */
751 dev->flags = IFF_NOARP;
754 static struct tty_ldisc x25_ldisc = {
755 .owner = THIS_MODULE,
756 .magic = TTY_LDISC_MAGIC,
757 .name = "X.25",
758 .open = x25_asy_open_tty,
759 .close = x25_asy_close_tty,
760 .ioctl = x25_asy_ioctl,
761 .receive_buf = x25_asy_receive_buf,
762 .write_wakeup = x25_asy_write_wakeup,
765 static int __init init_x25_asy(void)
767 if (x25_asy_maxdev < 4)
768 x25_asy_maxdev = 4; /* Sanity */
770 printk(KERN_INFO "X.25 async: version 0.00 ALPHA "
771 "(dynamic channels, max=%d).\n", x25_asy_maxdev);
773 x25_asy_devs = kcalloc(x25_asy_maxdev, sizeof(struct net_device *),
774 GFP_KERNEL);
775 if (!x25_asy_devs) {
776 printk(KERN_WARNING "X25 async: Can't allocate x25_asy_ctrls[] "
777 "array! Uaargh! (-> No X.25 available)\n");
778 return -ENOMEM;
781 return tty_register_ldisc(N_X25, &x25_ldisc);
785 static void __exit exit_x25_asy(void)
787 struct net_device *dev;
788 int i;
790 for (i = 0; i < x25_asy_maxdev; i++) {
791 dev = x25_asy_devs[i];
792 if (dev) {
793 struct x25_asy *sl = dev->priv;
795 spin_lock_bh(&sl->lock);
796 if (sl->tty)
797 tty_hangup(sl->tty);
799 spin_unlock_bh(&sl->lock);
801 * VSV = if dev->start==0, then device
802 * unregistered while close proc.
804 unregister_netdev(dev);
805 free_netdev(dev);
809 kfree(x25_asy_devs);
810 tty_unregister_ldisc(N_X25);
813 module_init(init_x25_asy);
814 module_exit(exit_x25_asy);