Import 2.4.0-test4
[davej-history.git] / drivers / net / ppp_synctty.c
blob5d852dfc685d382257b31dd23fa603890600813f
1 /*
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.
9 *
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
12 * the async maps.
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(); \
51 __r; })
52 #endif
54 #define PPP_VERSION "2.4.1"
56 /* Structure for storing local state. */
57 struct syncppp {
58 struct tty_struct *tty;
59 unsigned int flags;
60 unsigned int rbits;
61 int mru;
62 spinlock_t xmit_lock;
63 spinlock_t recv_lock;
64 unsigned long xmit_flags;
65 u32 xaccm[8];
66 u32 raccm;
67 unsigned int bytes_sent;
68 unsigned int bytes_rcvd;
70 struct sk_buff *tpkt;
71 unsigned long last_xmit;
73 struct sk_buff *rpkt;
75 struct ppp_channel chan; /* interface to generic ppp layer */
78 /* Bit numbers in xmit_flags */
79 #define XMIT_WAKEUP 0
80 #define XMIT_FULL 1
82 /* Bits in rbits */
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 */
88 * Prototypes.
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,
93 unsigned long arg);
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 = {
100 ppp_sync_send,
101 ppp_sync_ioctl
105 * Utility procedures to print a buffer in hex/ascii
107 static void
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) {
114 next_ch = *in++;
115 *out++ = hex[(next_ch >> 4) & 0x0F];
116 *out++ = hex[next_ch & 0x0F];
117 ++out;
121 static void
122 ppp_print_char (register __u8 * out, const __u8 * in, int count)
124 register __u8 next_ch;
126 while (count-- > 0) {
127 next_ch = *in++;
129 if (next_ch < 0x20 || next_ch > 0x7e)
130 *out++ = '.';
131 else {
132 *out++ = next_ch;
133 if (next_ch == '%') /* printk/syslogd has a bug !! */
134 *out++ = '%';
137 *out = '\0';
140 static void
141 ppp_print_buffer (const char *name, const __u8 *buf, int count)
143 __u8 line[44];
145 if (name != NULL)
146 printk(KERN_DEBUG "ppp_synctty: %s, count = %d\n", name, count);
148 while (count > 8) {
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);
153 count -= 8;
154 buf += 8;
157 if (count > 0) {
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.
173 static int
174 ppp_sync_open(struct tty_struct *tty)
176 struct syncppp *ap;
177 int err;
179 MOD_INC_USE_COUNT;
180 ap = kmalloc(sizeof(*ap), GFP_KERNEL);
181 err = -ENOMEM;
182 if (ap == 0)
183 goto out;
185 /* initialize the syncppp structure */
186 memset(ap, 0, sizeof(*ap));
187 ap->tty = tty;
188 ap->mru = PPP_MRU;
189 spin_lock_init(&ap->xmit_lock);
190 spin_lock_init(&ap->recv_lock);
191 ap->xaccm[0] = ~0U;
192 ap->xaccm[3] = 0x60000000U;
193 ap->raccm = ~0U;
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);
200 if (err)
201 goto out_free;
203 tty->disc_data = ap;
205 return 0;
207 out_free:
208 kfree(ap);
209 out:
210 MOD_DEC_USE_COUNT;
211 return err;
215 * Called when the tty is put into another line discipline
216 * (or it hangs up).
218 static void
219 ppp_sync_close(struct tty_struct *tty)
221 struct syncppp *ap = tty->disc_data;
223 if (ap == 0)
224 return;
225 tty->disc_data = 0;
226 ppp_unregister_channel(&ap->chan);
227 if (ap->rpkt != 0)
228 kfree_skb(ap->rpkt);
229 if (ap->tpkt != 0)
230 kfree_skb(ap->tpkt);
231 kfree(ap);
232 MOD_DEC_USE_COUNT;
236 * Read a PPP frame, for compatibility until pppd is updated.
238 static ssize_t
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;
244 if (ap == 0)
245 return -ENXIO;
246 return ppp_channel_read(&ap->chan, file, buf, count);
250 * Write a ppp frame, for compatibility until pppd is updated.
252 static ssize_t
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;
258 if (ap == 0)
259 return -ENXIO;
260 return ppp_channel_write(&ap->chan, buf, count);
263 static int
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;
268 int err, val;
270 err = -EFAULT;
271 switch (cmd) {
272 case PPPIOCGCHAN:
273 err = -ENXIO;
274 if (ap == 0)
275 break;
276 err = -EFAULT;
277 if (put_user(ppp_channel_index(&ap->chan), (int *) arg))
278 break;
279 err = 0;
280 break;
282 case PPPIOCGUNIT:
283 err = -ENXIO;
284 if (ap == 0)
285 break;
286 err = -EFAULT;
287 if (put_user(ppp_unit_number(&ap->chan), (int *) arg))
288 break;
289 err = 0;
290 break;
292 case TCGETS:
293 case TCGETA:
294 err = n_tty_ioctl(tty, file, cmd, arg);
295 break;
297 case TCFLSH:
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);
302 break;
304 case FIONREAD:
305 val = 0;
306 if (put_user(val, (int *) arg))
307 break;
308 err = 0;
309 break;
312 * Compatibility calls until pppd is updated.
314 case PPPIOCGFLAGS:
315 case PPPIOCSFLAGS:
316 case PPPIOCGASYNCMAP:
317 case PPPIOCSASYNCMAP:
318 case PPPIOCGRASYNCMAP:
319 case PPPIOCSRASYNCMAP:
320 case PPPIOCGXASYNCMAP:
321 case PPPIOCSXASYNCMAP:
322 case PPPIOCGMRU:
323 case PPPIOCSMRU:
324 err = -EPERM;
325 if (!capable(CAP_NET_ADMIN))
326 break;
327 err = ppp_sync_ioctl(&ap->chan, cmd, arg);
328 break;
330 case PPPIOCATTACH:
331 case PPPIOCDETACH:
332 err = ppp_channel_ioctl(&ap->chan, cmd, arg);
333 break;
335 default:
336 err = -ENOIOCTLCMD;
339 return err;
342 /* No kernel lock - fine */
343 static unsigned int
344 ppp_sync_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
346 struct syncppp *ap = tty->disc_data;
347 unsigned int mask;
349 mask = POLLOUT | POLLWRNORM;
350 /* compatibility for old pppd */
351 if (ap != 0)
352 mask |= ppp_channel_poll(&ap->chan, file, wait);
353 if (test_bit(TTY_OTHER_CLOSED, &tty->flags) || tty_hung_up_p(file))
354 mask |= POLLHUP;
355 return mask;
358 static int
359 ppp_sync_room(struct tty_struct *tty)
361 return 65535;
364 static void
365 ppp_sync_receive(struct tty_struct *tty, const unsigned char *buf,
366 char *flags, int count)
368 struct syncppp *ap = tty->disc_data;
370 if (ap == 0)
371 return;
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);
380 static void
381 ppp_sync_wakeup(struct tty_struct *tty)
383 struct syncppp *ap = tty->disc_data;
385 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
386 if (ap == 0)
387 return;
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,
395 name: "pppsync",
396 open: ppp_sync_open,
397 close: ppp_sync_close,
398 read: ppp_sync_read,
399 write: ppp_sync_write,
400 ioctl: ppp_synctty_ioctl,
401 poll: ppp_sync_poll,
402 receive_room: ppp_sync_room,
403 receive_buf: ppp_sync_receive,
404 write_wakeup: ppp_sync_wakeup,
408 ppp_sync_init(void)
410 int err;
412 err = tty_register_ldisc(N_SYNC_PPP, &ppp_sync_ldisc);
413 if (err != 0)
414 printk(KERN_ERR "PPP_sync: error %d registering line disc.\n",
415 err);
416 return err;
420 * The following routines provide the PPP channel interface.
422 static int
423 ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg)
425 struct syncppp *ap = chan->private;
426 int err, val;
427 u32 accm[8];
429 err = -EFAULT;
430 switch (cmd) {
431 case PPPIOCGFLAGS:
432 val = ap->flags | ap->rbits;
433 if (put_user(val, (int *) arg))
434 break;
435 err = 0;
436 break;
437 case PPPIOCSFLAGS:
438 if (get_user(val, (int *) arg))
439 break;
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);
444 err = 0;
445 break;
447 case PPPIOCGASYNCMAP:
448 if (put_user(ap->xaccm[0], (u32 *) arg))
449 break;
450 err = 0;
451 break;
452 case PPPIOCSASYNCMAP:
453 if (get_user(ap->xaccm[0], (u32 *) arg))
454 break;
455 err = 0;
456 break;
458 case PPPIOCGRASYNCMAP:
459 if (put_user(ap->raccm, (u32 *) arg))
460 break;
461 err = 0;
462 break;
463 case PPPIOCSRASYNCMAP:
464 if (get_user(ap->raccm, (u32 *) arg))
465 break;
466 err = 0;
467 break;
469 case PPPIOCGXASYNCMAP:
470 if (copy_to_user((void *) arg, ap->xaccm, sizeof(ap->xaccm)))
471 break;
472 err = 0;
473 break;
474 case PPPIOCSXASYNCMAP:
475 if (copy_from_user(accm, (void *) arg, sizeof(accm)))
476 break;
477 accm[2] &= ~0x40000000U; /* can't escape 0x5e */
478 accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
479 memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
480 err = 0;
481 break;
483 case PPPIOCGMRU:
484 if (put_user(ap->mru, (int *) arg))
485 break;
486 err = 0;
487 break;
488 case PPPIOCSMRU:
489 if (get_user(val, (int *) arg))
490 break;
491 if (val < PPP_MRU)
492 val = PPP_MRU;
493 ap->mru = val;
494 err = 0;
495 break;
497 default:
498 err = -ENOTTY;
500 return err;
504 * Procedures for encapsulation and framing.
507 struct sk_buff*
508 ppp_sync_txmunge(struct syncppp *ap, struct sk_buff *skb)
510 int proto;
511 unsigned char *data;
512 int islcp;
514 data = skb->data;
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)
525 skb_pull(skb,1);
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);
531 if (npkt == NULL) {
532 kfree_skb(skb);
533 return NULL;
535 skb_reserve(npkt,2);
536 memcpy(skb_put(npkt,skb->len), skb->data, skb->len);
537 kfree_skb(skb);
538 skb = npkt;
540 skb_push(skb,2);
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);
550 return skb;
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.
563 static int
564 ppp_sync_send(struct ppp_channel *chan, struct sk_buff *skb)
566 struct syncppp *ap = chan->private;
568 ppp_sync_push(ap);
570 if (test_and_set_bit(XMIT_FULL, &ap->xmit_flags))
571 return 0; /* already full */
572 skb = ppp_sync_txmunge(ap, skb);
573 if (skb != NULL)
574 ap->tpkt = skb;
575 else
576 clear_bit(XMIT_FULL, &ap->xmit_flags);
578 ppp_sync_push(ap);
579 return 1;
583 * Push as much data as possible out to the tty.
585 static int
586 ppp_sync_push(struct syncppp *ap)
588 int sent, done = 0;
589 struct tty_struct *tty = ap->tty;
590 int tty_stuffed = 0;
592 set_bit(XMIT_WAKEUP, &ap->xmit_flags);
593 if (!spin_trylock_bh(&ap->xmit_lock))
594 return 0;
595 for (;;) {
596 if (test_and_clear_bit(XMIT_WAKEUP, &ap->xmit_flags))
597 tty_stuffed = 0;
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);
601 if (sent < 0)
602 goto flush; /* error, e.g. loss of CD */
603 if (sent < ap->tpkt->len) {
604 tty_stuffed = 1;
605 } else {
606 kfree_skb(ap->tpkt);
607 ap->tpkt = 0;
608 clear_bit(XMIT_FULL, &ap->xmit_flags);
609 done = 1;
611 continue;
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)))
617 break;
618 if (!spin_trylock_bh(&ap->xmit_lock))
619 break;
621 return done;
623 flush:
624 if (ap->tpkt != 0) {
625 kfree_skb(ap->tpkt);
626 ap->tpkt = 0;
627 clear_bit(XMIT_FULL, &ap->xmit_flags);
628 done = 1;
630 spin_unlock_bh(&ap->xmit_lock);
631 return done;
635 * Flush output from our internal buffers.
636 * Called for the TCFLSH ioctl.
638 static void
639 ppp_sync_flush_output(struct syncppp *ap)
641 int done = 0;
643 spin_lock_bh(&ap->xmit_lock);
644 if (ap->tpkt != NULL) {
645 kfree_skb(ap->tpkt);
646 ap->tpkt = 0;
647 clear_bit(XMIT_FULL, &ap->xmit_flags);
648 done = 1;
650 spin_unlock_bh(&ap->xmit_lock);
651 if (done)
652 ppp_output_wakeup(&ap->chan);
656 * Receive-side routines.
659 static inline void
660 process_input_packet(struct syncppp *ap)
662 struct sk_buff *skb;
663 unsigned char *p;
664 int code = 0;
666 skb = ap->rpkt;
667 ap->rpkt = 0;
669 /* strip address/control field if present */
670 p = skb->data;
671 if (p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
672 /* chop off address/control */
673 if (skb->len < 3)
674 goto err;
675 p = skb_pull(skb, 2);
678 /* decompress protocol field if compressed */
679 if (p[0] & 1) {
680 /* protocol is compressed */
681 skb_push(skb, 1)[0] = 0;
682 } else if (skb->len < 2)
683 goto err;
685 /* pass to generic layer */
686 ppp_input(&ap->chan, skb);
687 return;
689 err:
690 kfree_skb(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.
700 static void
701 ppp_sync_input(struct syncppp *ap, const unsigned char *buf,
702 char *flags, int count)
704 struct sk_buff *skb;
705 unsigned char *sp;
707 if (count == 0)
708 return;
710 /* if flag set, then error, ignore frame */
711 if (flags != 0 && *flags) {
712 ppp_input_error(&ap->chan, *flags);
713 return;
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);
724 return;
726 /* Try to get the payload 4-byte aligned */
727 if (buf[0] != PPP_ALLSTATIONS)
728 skb_reserve(skb, 2 + (buf[0] & 1));
729 ap->rpkt = skb;
731 if (count > skb_tailroom(skb)) {
732 /* packet overflowed MRU */
733 ppp_input_error(&ap->chan, 1);
734 } else {
735 sp = skb_put(skb, count);
736 memcpy(sp, buf, count);
737 process_input_packet(ap);
741 void __exit
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);