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 sync-only
29 * PPP driver, written by Michael Callahan and Al Longyear, and
30 * subsequently hacked by Paul Mackerras.
32 * ==FILEVERSION 20000322==
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/init.h>
45 #include <asm/uaccess.h>
47 #ifndef spin_trylock_bh
48 #define spin_trylock_bh(lock) ({ int __r; local_bh_disable(); \
49 __r = spin_trylock(lock); \
50 if (!__r) local_bh_enable(); \
54 #define PPP_VERSION "2.4.1"
56 /* Structure for storing local state. */
58 struct tty_struct
*tty
;
64 unsigned long xmit_flags
;
67 unsigned int bytes_sent
;
68 unsigned int bytes_rcvd
;
71 unsigned long last_xmit
;
75 struct ppp_channel chan
; /* interface to generic ppp layer */
78 /* Bit numbers in xmit_flags */
83 #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
85 #define PPPSYNC_MAX_RQLEN 32 /* arbitrary */
90 static struct sk_buff
* ppp_sync_txmunge(struct syncppp
*ap
, struct sk_buff
*);
91 static int ppp_sync_send(struct ppp_channel
*chan
, struct sk_buff
*skb
);
92 static int ppp_sync_ioctl(struct ppp_channel
*chan
, unsigned int cmd
,
94 static int ppp_sync_push(struct syncppp
*ap
);
95 static void ppp_sync_flush_output(struct syncppp
*ap
);
96 static void ppp_sync_input(struct syncppp
*ap
, const unsigned char *buf
,
97 char *flags
, int count
);
99 struct ppp_channel_ops sync_ops
= {
105 * Utility procedures to print a buffer in hex/ascii
108 ppp_print_hex (register __u8
* out
, const __u8
* in
, int count
)
110 register __u8 next_ch
;
111 static char hex
[] = "0123456789ABCDEF";
113 while (count
-- > 0) {
115 *out
++ = hex
[(next_ch
>> 4) & 0x0F];
116 *out
++ = hex
[next_ch
& 0x0F];
122 ppp_print_char (register __u8
* out
, const __u8
* in
, int count
)
124 register __u8 next_ch
;
126 while (count
-- > 0) {
129 if (next_ch
< 0x20 || next_ch
> 0x7e)
133 if (next_ch
== '%') /* printk/syslogd has a bug !! */
141 ppp_print_buffer (const char *name
, const __u8
*buf
, int count
)
146 printk(KERN_DEBUG
"ppp_synctty: %s, count = %d\n", name
, count
);
149 memset (line
, 32, 44);
150 ppp_print_hex (line
, buf
, 8);
151 ppp_print_char (&line
[8 * 3], buf
, 8);
152 printk(KERN_DEBUG
"%s\n", line
);
158 memset (line
, 32, 44);
159 ppp_print_hex (line
, buf
, count
);
160 ppp_print_char (&line
[8 * 3], buf
, count
);
161 printk(KERN_DEBUG
"%s\n", line
);
167 * Routines implementing the synchronous PPP line discipline.
171 * Called when a tty is put into line discipline.
174 ppp_sync_open(struct tty_struct
*tty
)
180 ap
= kmalloc(sizeof(*ap
), GFP_KERNEL
);
185 /* initialize the syncppp structure */
186 memset(ap
, 0, sizeof(*ap
));
189 spin_lock_init(&ap
->xmit_lock
);
190 spin_lock_init(&ap
->recv_lock
);
192 ap
->xaccm
[3] = 0x60000000U
;
195 ap
->chan
.private = ap
;
196 ap
->chan
.ops
= &sync_ops
;
197 ap
->chan
.mtu
= PPP_MRU
;
198 ap
->chan
.hdrlen
= 2; /* for A/C bytes */
199 err
= ppp_register_channel(&ap
->chan
);
215 * Called when the tty is put into another line discipline
219 ppp_sync_close(struct tty_struct
*tty
)
221 struct syncppp
*ap
= tty
->disc_data
;
226 ppp_unregister_channel(&ap
->chan
);
236 * Read a PPP frame, for compatibility until pppd is updated.
239 ppp_sync_read(struct tty_struct
*tty
, struct file
*file
,
240 unsigned char *buf
, size_t count
)
242 struct syncppp
*ap
= tty
->disc_data
;
246 return ppp_channel_read(&ap
->chan
, file
, buf
, count
);
250 * Write a ppp frame, for compatibility until pppd is updated.
253 ppp_sync_write(struct tty_struct
*tty
, struct file
*file
,
254 const unsigned char *buf
, size_t count
)
256 struct syncppp
*ap
= tty
->disc_data
;
260 return ppp_channel_write(&ap
->chan
, buf
, count
);
264 ppp_synctty_ioctl(struct tty_struct
*tty
, struct file
*file
,
265 unsigned int cmd
, unsigned long arg
)
267 struct syncppp
*ap
= tty
->disc_data
;
277 if (put_user(ppp_channel_index(&ap
->chan
), (int *) arg
))
287 if (put_user(ppp_unit_number(&ap
->chan
), (int *) arg
))
294 err
= n_tty_ioctl(tty
, file
, cmd
, arg
);
298 /* flush our buffers and the serial port's buffer */
299 if (arg
== TCIOFLUSH
|| arg
== TCOFLUSH
)
300 ppp_sync_flush_output(ap
);
301 err
= n_tty_ioctl(tty
, file
, cmd
, arg
);
306 if (put_user(val
, (int *) arg
))
312 * Compatibility calls until pppd is updated.
316 case PPPIOCGASYNCMAP
:
317 case PPPIOCSASYNCMAP
:
318 case PPPIOCGRASYNCMAP
:
319 case PPPIOCSRASYNCMAP
:
320 case PPPIOCGXASYNCMAP
:
321 case PPPIOCSXASYNCMAP
:
325 if (!capable(CAP_NET_ADMIN
))
327 err
= ppp_sync_ioctl(&ap
->chan
, cmd
, arg
);
332 err
= ppp_channel_ioctl(&ap
->chan
, cmd
, arg
);
342 /* No kernel lock - fine */
344 ppp_sync_poll(struct tty_struct
*tty
, struct file
*file
, poll_table
*wait
)
346 struct syncppp
*ap
= tty
->disc_data
;
349 mask
= POLLOUT
| POLLWRNORM
;
350 /* compatibility for old pppd */
352 mask
|= ppp_channel_poll(&ap
->chan
, file
, wait
);
353 if (test_bit(TTY_OTHER_CLOSED
, &tty
->flags
) || tty_hung_up_p(file
))
359 ppp_sync_room(struct tty_struct
*tty
)
365 ppp_sync_receive(struct tty_struct
*tty
, const unsigned char *buf
,
366 char *flags
, int count
)
368 struct syncppp
*ap
= tty
->disc_data
;
372 spin_lock_bh(&ap
->recv_lock
);
373 ppp_sync_input(ap
, buf
, flags
, count
);
374 spin_unlock_bh(&ap
->recv_lock
);
375 if (test_and_clear_bit(TTY_THROTTLED
, &tty
->flags
)
376 && tty
->driver
.unthrottle
)
377 tty
->driver
.unthrottle(tty
);
381 ppp_sync_wakeup(struct tty_struct
*tty
)
383 struct syncppp
*ap
= tty
->disc_data
;
385 clear_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
388 if (ppp_sync_push(ap
))
389 ppp_output_wakeup(&ap
->chan
);
393 static struct tty_ldisc ppp_sync_ldisc
= {
394 magic
: TTY_LDISC_MAGIC
,
397 close
: ppp_sync_close
,
399 write
: ppp_sync_write
,
400 ioctl
: ppp_synctty_ioctl
,
402 receive_room
: ppp_sync_room
,
403 receive_buf
: ppp_sync_receive
,
404 write_wakeup
: ppp_sync_wakeup
,
412 err
= tty_register_ldisc(N_SYNC_PPP
, &ppp_sync_ldisc
);
414 printk(KERN_ERR
"PPP_sync: error %d registering line disc.\n",
420 * The following routines provide the PPP channel interface.
423 ppp_sync_ioctl(struct ppp_channel
*chan
, unsigned int cmd
, unsigned long arg
)
425 struct syncppp
*ap
= chan
->private;
432 val
= ap
->flags
| ap
->rbits
;
433 if (put_user(val
, (int *) arg
))
438 if (get_user(val
, (int *) arg
))
440 ap
->flags
= val
& ~SC_RCV_BITS
;
441 spin_lock_bh(&ap
->recv_lock
);
442 ap
->rbits
= val
& SC_RCV_BITS
;
443 spin_unlock_bh(&ap
->recv_lock
);
447 case PPPIOCGASYNCMAP
:
448 if (put_user(ap
->xaccm
[0], (u32
*) arg
))
452 case PPPIOCSASYNCMAP
:
453 if (get_user(ap
->xaccm
[0], (u32
*) arg
))
458 case PPPIOCGRASYNCMAP
:
459 if (put_user(ap
->raccm
, (u32
*) arg
))
463 case PPPIOCSRASYNCMAP
:
464 if (get_user(ap
->raccm
, (u32
*) arg
))
469 case PPPIOCGXASYNCMAP
:
470 if (copy_to_user((void *) arg
, ap
->xaccm
, sizeof(ap
->xaccm
)))
474 case PPPIOCSXASYNCMAP
:
475 if (copy_from_user(accm
, (void *) arg
, sizeof(accm
)))
477 accm
[2] &= ~0x40000000U
; /* can't escape 0x5e */
478 accm
[3] |= 0x60000000U
; /* must escape 0x7d, 0x7e */
479 memcpy(ap
->xaccm
, accm
, sizeof(ap
->xaccm
));
484 if (put_user(ap
->mru
, (int *) arg
))
489 if (get_user(val
, (int *) arg
))
504 * Procedures for encapsulation and framing.
508 ppp_sync_txmunge(struct syncppp
*ap
, struct sk_buff
*skb
)
515 proto
= (data
[0] << 8) + data
[1];
517 /* LCP packets with codes between 1 (configure-request)
518 * and 7 (code-reject) must be sent as though no options
519 * have been negotiated.
521 islcp
= proto
== PPP_LCP
&& 1 <= data
[2] && data
[2] <= 7;
523 /* compress protocol field if option enabled */
524 if (data
[0] == 0 && (ap
->flags
& SC_COMP_PROT
) && !islcp
)
527 /* prepend address/control fields if necessary */
528 if ((ap
->flags
& SC_COMP_AC
) == 0 || islcp
) {
529 if (skb_headroom(skb
) < 2) {
530 struct sk_buff
*npkt
= dev_alloc_skb(skb
->len
+ 2);
536 memcpy(skb_put(npkt
,skb
->len
), skb
->data
, skb
->len
);
541 skb
->data
[0] = PPP_ALLSTATIONS
;
542 skb
->data
[1] = PPP_UI
;
545 ap
->last_xmit
= jiffies
;
547 if (skb
&& ap
->flags
& SC_LOG_OUTPKT
)
548 ppp_print_buffer ("send buffer", skb
->data
, skb
->len
);
554 * Transmit-side routines.
558 * Send a packet to the peer over an sync tty line.
559 * Returns 1 iff the packet was accepted.
560 * If the packet was not accepted, we will call ppp_output_wakeup
561 * at some later time.
564 ppp_sync_send(struct ppp_channel
*chan
, struct sk_buff
*skb
)
566 struct syncppp
*ap
= chan
->private;
570 if (test_and_set_bit(XMIT_FULL
, &ap
->xmit_flags
))
571 return 0; /* already full */
572 skb
= ppp_sync_txmunge(ap
, skb
);
576 clear_bit(XMIT_FULL
, &ap
->xmit_flags
);
583 * Push as much data as possible out to the tty.
586 ppp_sync_push(struct syncppp
*ap
)
589 struct tty_struct
*tty
= ap
->tty
;
592 set_bit(XMIT_WAKEUP
, &ap
->xmit_flags
);
593 if (!spin_trylock_bh(&ap
->xmit_lock
))
596 if (test_and_clear_bit(XMIT_WAKEUP
, &ap
->xmit_flags
))
598 if (!tty_stuffed
&& ap
->tpkt
!= 0) {
599 set_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
);
600 sent
= tty
->driver
.write(tty
, 0, ap
->tpkt
->data
, ap
->tpkt
->len
);
602 goto flush
; /* error, e.g. loss of CD */
603 if (sent
< ap
->tpkt
->len
) {
608 clear_bit(XMIT_FULL
, &ap
->xmit_flags
);
613 /* haven't made any progress */
614 spin_unlock_bh(&ap
->xmit_lock
);
615 if (!(test_bit(XMIT_WAKEUP
, &ap
->xmit_flags
)
616 || (!tty_stuffed
&& ap
->tpkt
!= 0)))
618 if (!spin_trylock_bh(&ap
->xmit_lock
))
627 clear_bit(XMIT_FULL
, &ap
->xmit_flags
);
630 spin_unlock_bh(&ap
->xmit_lock
);
635 * Flush output from our internal buffers.
636 * Called for the TCFLSH ioctl.
639 ppp_sync_flush_output(struct syncppp
*ap
)
643 spin_lock_bh(&ap
->xmit_lock
);
644 if (ap
->tpkt
!= NULL
) {
647 clear_bit(XMIT_FULL
, &ap
->xmit_flags
);
650 spin_unlock_bh(&ap
->xmit_lock
);
652 ppp_output_wakeup(&ap
->chan
);
656 * Receive-side routines.
660 process_input_packet(struct syncppp
*ap
)
669 /* strip address/control field if present */
671 if (p
[0] == PPP_ALLSTATIONS
&& p
[1] == PPP_UI
) {
672 /* chop off address/control */
675 p
= skb_pull(skb
, 2);
678 /* decompress protocol field if compressed */
680 /* protocol is compressed */
681 skb_push(skb
, 1)[0] = 0;
682 } else if (skb
->len
< 2)
685 /* pass to generic layer */
686 ppp_input(&ap
->chan
, skb
);
691 ppp_input_error(&ap
->chan
, code
);
694 /* called when the tty driver has data for us.
696 * Data is frame oriented: each call to ppp_sync_input is considered
697 * a whole frame. If the 1st flag byte is non-zero then the whole
698 * frame is considered to be in error and is tossed.
701 ppp_sync_input(struct syncppp
*ap
, const unsigned char *buf
,
702 char *flags
, int count
)
710 /* if flag set, then error, ignore frame */
711 if (flags
!= 0 && *flags
) {
712 ppp_input_error(&ap
->chan
, *flags
);
716 if (ap
->flags
& SC_LOG_INPKT
)
717 ppp_print_buffer ("receive buffer", buf
, count
);
719 /* stuff the chars in the skb */
720 if ((skb
= ap
->rpkt
) == 0) {
721 if ((skb
= dev_alloc_skb(ap
->mru
+ PPP_HDRLEN
+ 2)) == 0) {
722 printk(KERN_ERR
"PPPsync: no memory (input pkt)\n");
723 ppp_input_error(&ap
->chan
, 0);
726 /* Try to get the payload 4-byte aligned */
727 if (buf
[0] != PPP_ALLSTATIONS
)
728 skb_reserve(skb
, 2 + (buf
[0] & 1));
731 if (count
> skb_tailroom(skb
)) {
732 /* packet overflowed MRU */
733 ppp_input_error(&ap
->chan
, 1);
735 sp
= skb_put(skb
, count
);
736 memcpy(sp
, buf
, count
);
737 process_input_packet(ap
);
742 ppp_sync_cleanup(void)
744 if (tty_register_ldisc(N_SYNC_PPP
, NULL
) != 0)
745 printk(KERN_ERR
"failed to unregister Sync PPP line discipline\n");
748 module_init(ppp_sync_init
);
749 module_exit(ppp_sync_cleanup
);