2 * PPP synchronous tty channel driver for Linux.
4 * This is a ppp channel driver that can be used with tty device drivers
5 * that are frame oriented, such as synchronous HDLC devices.
7 * Complete PPP frames without encoding/decoding are exchanged between
8 * the channel driver and the device driver.
10 * The async map IOCTL codes are implemented to keep the user mode
11 * applications happy if they call them. Synchronous PPP does not use
14 * Copyright 1999 Paul Mackerras.
16 * Also touched by the grubby hands of Paul Fulghum paulkf@microgate.com
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
23 * This driver provides the encapsulation and framing for sending
24 * and receiving PPP frames over sync serial lines. It relies on
25 * the generic PPP layer to give it frames to send and to process
26 * received frames. It implements the PPP line discipline.
28 * Part of the code in this driver was inspired by the old async-only
29 * PPP driver, written by Michael Callahan and Al Longyear, and
30 * subsequently hacked by Paul Mackerras.
32 * ==FILEVERSION 20020125==
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/skbuff.h>
38 #include <linux/tty.h>
39 #include <linux/netdevice.h>
40 #include <linux/poll.h>
41 #include <linux/ppp_defs.h>
42 #include <linux/if_ppp.h>
43 #include <linux/ppp_channel.h>
44 #include <linux/spinlock.h>
45 #include <linux/init.h>
46 #include <asm/uaccess.h>
47 #include <asm/semaphore.h>
49 #define PPP_VERSION "2.4.2"
51 /* Structure for storing local state. */
53 struct tty_struct *tty;
59 unsigned long xmit_flags;
62 unsigned int bytes_sent;
63 unsigned int bytes_rcvd;
66 unsigned long last_xmit;
71 struct semaphore dead_sem;
72 struct ppp_channel chan; /* interface to generic ppp layer */
75 /* Bit numbers in xmit_flags */
80 #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
82 #define PPPSYNC_MAX_RQLEN 32 /* arbitrary */
87 static struct sk_buff* ppp_sync_txmunge(struct syncppp *ap, struct sk_buff *);
88 static int ppp_sync_send(struct ppp_channel *chan, struct sk_buff *skb);
89 static int ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd,
91 static int ppp_sync_push(struct syncppp *ap);
92 static void ppp_sync_flush_output(struct syncppp *ap);
93 static void ppp_sync_input(struct syncppp *ap, const unsigned char *buf,
94 char *flags, int count);
96 static struct ppp_channel_ops sync_ops = {
102 * Utility procedures to print a buffer in hex/ascii
105 ppp_print_hex (register __u8 * out, const __u8 * in, int count)
107 register __u8 next_ch;
108 static char hex[] = "0123456789ABCDEF";
110 while (count-- > 0) {
112 *out++ = hex[(next_ch >> 4) & 0x0F];
113 *out++ = hex[next_ch & 0x0F];
119 ppp_print_char (register __u8 * out, const __u8 * in, int count)
121 register __u8 next_ch;
123 while (count-- > 0) {
126 if (next_ch < 0x20 || next_ch > 0x7e)
130 if (next_ch == '%') /* printk/syslogd has a bug !! */
138 ppp_print_buffer (const char *name, const __u8 *buf, int count)
143 printk(KERN_DEBUG "ppp_synctty: %s, count = %d\n", name, count);
146 memset (line, 32, 44);
147 ppp_print_hex (line, buf, 8);
148 ppp_print_char (&line[8 * 3], buf, 8);
149 printk(KERN_DEBUG "%s\n", line);
155 memset (line, 32, 44);
156 ppp_print_hex (line, buf, count);
157 ppp_print_char (&line[8 * 3], buf, count);
158 printk(KERN_DEBUG "%s\n", line);
164 * Routines implementing the synchronous PPP line discipline.
168 * We have a potential race on dereferencing tty->disc_data,
169 * because the tty layer provides no locking at all - thus one
170 * cpu could be running ppp_synctty_receive while another
171 * calls ppp_synctty_close, which zeroes tty->disc_data and
172 * frees the memory that ppp_synctty_receive is using. The best
173 * way to fix this is to use a rwlock in the tty struct, but for now
174 * we use a single global rwlock for all ttys in ppp line discipline.
176 static rwlock_t disc_data_lock = RW_LOCK_UNLOCKED;
178 static struct syncppp *sp_get(struct tty_struct *tty)
182 read_lock(&disc_data_lock);
185 atomic_inc(&ap->refcnt);
186 read_unlock(&disc_data_lock);
190 static void sp_put(struct syncppp *ap)
192 if (atomic_dec_and_test(&ap->refcnt))
197 * Called when a tty is put into sync-PPP line discipline.
200 ppp_sync_open(struct tty_struct *tty)
205 ap = kmalloc(sizeof(*ap), GFP_KERNEL);
210 /* initialize the syncppp structure */
211 memset(ap, 0, sizeof(*ap));
214 spin_lock_init(&ap->xmit_lock);
215 spin_lock_init(&ap->recv_lock);
217 ap->xaccm[3] = 0x60000000U;
220 atomic_set(&ap->refcnt, 1);
221 init_MUTEX_LOCKED(&ap->dead_sem);
223 ap->chan.private = ap;
224 ap->chan.ops = &sync_ops;
225 ap->chan.mtu = PPP_MRU;
226 ap->chan.hdrlen = 2; /* for A/C bytes */
227 err = ppp_register_channel(&ap->chan);
242 * Called when the tty is put into another line discipline
243 * or it hangs up. We have to wait for any cpu currently
244 * executing in any of the other ppp_synctty_* routines to
245 * finish before we can call ppp_unregister_channel and free
246 * the syncppp struct. This routine must be called from
247 * process context, not interrupt or softirq context.
250 ppp_sync_close(struct tty_struct *tty)
254 write_lock(&disc_data_lock);
257 write_unlock(&disc_data_lock);
262 * We have now ensured that nobody can start using ap from now
263 * on, but we have to wait for all existing users to finish.
264 * Note that ppp_unregister_channel ensures that no calls to
265 * our channel ops (i.e. ppp_sync_send/ioctl) are in progress
266 * by the time it returns.
268 if (!atomic_dec_and_test(&ap->refcnt))
271 ppp_unregister_channel(&ap->chan);
280 * Read does nothing - no data is ever available this way.
281 * Pppd reads and writes packets via /dev/ppp instead.
284 ppp_sync_read(struct tty_struct *tty, struct file *file,
285 unsigned char *buf, size_t count)
291 * Write on the tty does nothing, the packets all come in
292 * from the ppp generic stuff.
295 ppp_sync_write(struct tty_struct *tty, struct file *file,
296 const unsigned char *buf, size_t count)
302 ppp_synctty_ioctl(struct tty_struct *tty, struct file *file,
303 unsigned int cmd, unsigned long arg)
305 struct syncppp *ap = sp_get(tty);
317 if (put_user(ppp_channel_index(&ap->chan), (int *) arg))
327 if (put_user(ppp_unit_number(&ap->chan), (int *) arg))
334 err = n_tty_ioctl(tty, file, cmd, arg);
338 /* flush our buffers and the serial port's buffer */
339 if (arg == TCIOFLUSH || arg == TCOFLUSH)
340 ppp_sync_flush_output(ap);
341 err = n_tty_ioctl(tty, file, cmd, arg);
346 if (put_user(val, (int *) arg))
359 /* No kernel lock - fine */
361 ppp_sync_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
367 ppp_sync_room(struct tty_struct *tty)
373 ppp_sync_receive(struct tty_struct *tty, const unsigned char *buf,
374 char *flags, int count)
376 struct syncppp *ap = sp_get(tty);
380 spin_lock_bh(&ap->recv_lock);
381 ppp_sync_input(ap, buf, flags, count);
382 spin_unlock_bh(&ap->recv_lock);
384 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags)
385 && tty->driver->unthrottle)
386 tty->driver->unthrottle(tty);
390 ppp_sync_wakeup(struct tty_struct *tty)
392 struct syncppp *ap = sp_get(tty);
394 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
397 if (ppp_sync_push(ap))
398 ppp_output_wakeup(&ap->chan);
403 static struct tty_ldisc ppp_sync_ldisc = {
404 .owner = THIS_MODULE,
405 .magic = TTY_LDISC_MAGIC,
407 .open = ppp_sync_open,
408 .close = ppp_sync_close,
409 .read = ppp_sync_read,
410 .write = ppp_sync_write,
411 .ioctl = ppp_synctty_ioctl,
412 .poll = ppp_sync_poll,
413 .receive_room = ppp_sync_room,
414 .receive_buf = ppp_sync_receive,
415 .write_wakeup = ppp_sync_wakeup,
423 err = tty_register_ldisc(N_SYNC_PPP, &ppp_sync_ldisc);
425 printk(KERN_ERR "PPP_sync: error %d registering line disc.\n",
431 * The following routines provide the PPP channel interface.
434 ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg)
436 struct syncppp *ap = chan->private;
443 val = ap->flags | ap->rbits;
444 if (put_user(val, (int *) arg))
449 if (get_user(val, (int *) arg))
451 ap->flags = val & ~SC_RCV_BITS;
452 spin_lock_bh(&ap->recv_lock);
453 ap->rbits = val & SC_RCV_BITS;
454 spin_unlock_bh(&ap->recv_lock);
458 case PPPIOCGASYNCMAP:
459 if (put_user(ap->xaccm[0], (u32 *) arg))
463 case PPPIOCSASYNCMAP:
464 if (get_user(ap->xaccm[0], (u32 *) arg))
469 case PPPIOCGRASYNCMAP:
470 if (put_user(ap->raccm, (u32 *) arg))
474 case PPPIOCSRASYNCMAP:
475 if (get_user(ap->raccm, (u32 *) arg))
480 case PPPIOCGXASYNCMAP:
481 if (copy_to_user((void *) arg, ap->xaccm, sizeof(ap->xaccm)))
485 case PPPIOCSXASYNCMAP:
486 if (copy_from_user(accm, (void *) arg, sizeof(accm)))
488 accm[2] &= ~0x40000000U; /* can't escape 0x5e */
489 accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
490 memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
495 if (put_user(ap->mru, (int *) arg))
500 if (get_user(val, (int *) arg))
515 * Procedures for encapsulation and framing.
519 ppp_sync_txmunge(struct syncppp *ap, struct sk_buff *skb)
526 proto = (data[0] << 8) + data[1];
528 /* LCP packets with codes between 1 (configure-request)
529 * and 7 (code-reject) must be sent as though no options
530 * have been negotiated.
532 islcp = proto == PPP_LCP && 1 <= data[2] && data[2] <= 7;
534 /* compress protocol field if option enabled */
535 if (data[0] == 0 && (ap->flags & SC_COMP_PROT) && !islcp)
538 /* prepend address/control fields if necessary */
539 if ((ap->flags & SC_COMP_AC) == 0 || islcp) {
540 if (skb_headroom(skb) < 2) {
541 struct sk_buff *npkt = dev_alloc_skb(skb->len + 2);
547 memcpy(skb_put(npkt,skb->len), skb->data, skb->len);
552 skb->data[0] = PPP_ALLSTATIONS;
553 skb->data[1] = PPP_UI;
556 ap->last_xmit = jiffies;
558 if (skb && ap->flags & SC_LOG_OUTPKT)
559 ppp_print_buffer ("send buffer", skb->data, skb->len);
565 * Transmit-side routines.
569 * Send a packet to the peer over an sync tty line.
570 * Returns 1 iff the packet was accepted.
571 * If the packet was not accepted, we will call ppp_output_wakeup
572 * at some later time.
575 ppp_sync_send(struct ppp_channel *chan, struct sk_buff *skb)
577 struct syncppp *ap = chan->private;
581 if (test_and_set_bit(XMIT_FULL, &ap->xmit_flags))
582 return 0; /* already full */
583 skb = ppp_sync_txmunge(ap, skb);
587 clear_bit(XMIT_FULL, &ap->xmit_flags);
594 * Push as much data as possible out to the tty.
597 ppp_sync_push(struct syncppp *ap)
600 struct tty_struct *tty = ap->tty;
603 set_bit(XMIT_WAKEUP, &ap->xmit_flags);
604 if (!spin_trylock_bh(&ap->xmit_lock))
607 if (test_and_clear_bit(XMIT_WAKEUP, &ap->xmit_flags))
609 if (!tty_stuffed && ap->tpkt != 0) {
610 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
611 sent = tty->driver->write(tty, 0, ap->tpkt->data, ap->tpkt->len);
613 goto flush; /* error, e.g. loss of CD */
614 if (sent < ap->tpkt->len) {
619 clear_bit(XMIT_FULL, &ap->xmit_flags);
624 /* haven't made any progress */
625 spin_unlock_bh(&ap->xmit_lock);
626 if (!(test_bit(XMIT_WAKEUP, &ap->xmit_flags)
627 || (!tty_stuffed && ap->tpkt != 0)))
629 if (!spin_trylock_bh(&ap->xmit_lock))
638 clear_bit(XMIT_FULL, &ap->xmit_flags);
641 spin_unlock_bh(&ap->xmit_lock);
646 * Flush output from our internal buffers.
647 * Called for the TCFLSH ioctl.
650 ppp_sync_flush_output(struct syncppp *ap)
654 spin_lock_bh(&ap->xmit_lock);
655 if (ap->tpkt != NULL) {
658 clear_bit(XMIT_FULL, &ap->xmit_flags);
661 spin_unlock_bh(&ap->xmit_lock);
663 ppp_output_wakeup(&ap->chan);
667 * Receive-side routines.
671 process_input_packet(struct syncppp *ap)
680 /* strip address/control field if present */
682 if (p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
683 /* chop off address/control */
686 p = skb_pull(skb, 2);
689 /* decompress protocol field if compressed */
691 /* protocol is compressed */
692 skb_push(skb, 1)[0] = 0;
693 } else if (skb->len < 2)
696 /* pass to generic layer */
697 ppp_input(&ap->chan, skb);
702 ppp_input_error(&ap->chan, code);
705 /* called when the tty driver has data for us.
707 * Data is frame oriented: each call to ppp_sync_input is considered
708 * a whole frame. If the 1st flag byte is non-zero then the whole
709 * frame is considered to be in error and is tossed.
712 ppp_sync_input(struct syncppp *ap, const unsigned char *buf,
713 char *flags, int count)
721 /* if flag set, then error, ignore frame */
722 if (flags != 0 && *flags) {
723 ppp_input_error(&ap->chan, *flags);
727 if (ap->flags & SC_LOG_INPKT)
728 ppp_print_buffer ("receive buffer", buf, count);
730 /* stuff the chars in the skb */
731 if ((skb = ap->rpkt) == 0) {
732 if ((skb = dev_alloc_skb(ap->mru + PPP_HDRLEN + 2)) == 0) {
733 printk(KERN_ERR "PPPsync: no memory (input pkt)\n");
734 ppp_input_error(&ap->chan, 0);
737 /* Try to get the payload 4-byte aligned */
738 if (buf[0] != PPP_ALLSTATIONS)
739 skb_reserve(skb, 2 + (buf[0] & 1));
742 if (count > skb_tailroom(skb)) {
743 /* packet overflowed MRU */
744 ppp_input_error(&ap->chan, 1);
746 sp = skb_put(skb, count);
747 memcpy(sp, buf, count);
748 process_input_packet(ap);
753 ppp_sync_cleanup(void)
755 if (tty_register_ldisc(N_SYNC_PPP, NULL) != 0)
756 printk(KERN_ERR "failed to unregister Sync PPP line discipline\n");
759 module_init(ppp_sync_init);
760 module_exit(ppp_sync_cleanup);
761 MODULE_LICENSE("GPL");
762 MODULE_ALIAS_LDISC(N_SYNC_PPP);