More meth updates.
[linux-2.6/linux-mips.git] / drivers / net / ppp_synctty.c
blob06feb5870fc89b611bc17697112350c0e4365e96
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 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. */
52 struct syncppp {
53 struct tty_struct *tty;
54 unsigned int flags;
55 unsigned int rbits;
56 int mru;
57 spinlock_t xmit_lock;
58 spinlock_t recv_lock;
59 unsigned long xmit_flags;
60 u32 xaccm[8];
61 u32 raccm;
62 unsigned int bytes_sent;
63 unsigned int bytes_rcvd;
65 struct sk_buff *tpkt;
66 unsigned long last_xmit;
68 struct sk_buff *rpkt;
70 atomic_t refcnt;
71 struct semaphore dead_sem;
72 struct ppp_channel chan; /* interface to generic ppp layer */
75 /* Bit numbers in xmit_flags */
76 #define XMIT_WAKEUP 0
77 #define XMIT_FULL 1
79 /* Bits in rbits */
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 */
85 * Prototypes.
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,
90 unsigned long arg);
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 = {
97 ppp_sync_send,
98 ppp_sync_ioctl
102 * Utility procedures to print a buffer in hex/ascii
104 static void
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) {
111 next_ch = *in++;
112 *out++ = hex[(next_ch >> 4) & 0x0F];
113 *out++ = hex[next_ch & 0x0F];
114 ++out;
118 static void
119 ppp_print_char (register __u8 * out, const __u8 * in, int count)
121 register __u8 next_ch;
123 while (count-- > 0) {
124 next_ch = *in++;
126 if (next_ch < 0x20 || next_ch > 0x7e)
127 *out++ = '.';
128 else {
129 *out++ = next_ch;
130 if (next_ch == '%') /* printk/syslogd has a bug !! */
131 *out++ = '%';
134 *out = '\0';
137 static void
138 ppp_print_buffer (const char *name, const __u8 *buf, int count)
140 __u8 line[44];
142 if (name != NULL)
143 printk(KERN_DEBUG "ppp_synctty: %s, count = %d\n", name, count);
145 while (count > 8) {
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);
150 count -= 8;
151 buf += 8;
154 if (count > 0) {
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)
180 struct syncppp *ap;
182 read_lock(&disc_data_lock);
183 ap = tty->disc_data;
184 if (ap != NULL)
185 atomic_inc(&ap->refcnt);
186 read_unlock(&disc_data_lock);
187 return ap;
190 static void sp_put(struct syncppp *ap)
192 if (atomic_dec_and_test(&ap->refcnt))
193 up(&ap->dead_sem);
197 * Called when a tty is put into sync-PPP line discipline.
199 static int
200 ppp_sync_open(struct tty_struct *tty)
202 struct syncppp *ap;
203 int err;
205 ap = kmalloc(sizeof(*ap), GFP_KERNEL);
206 err = -ENOMEM;
207 if (ap == 0)
208 goto out;
210 /* initialize the syncppp structure */
211 memset(ap, 0, sizeof(*ap));
212 ap->tty = tty;
213 ap->mru = PPP_MRU;
214 spin_lock_init(&ap->xmit_lock);
215 spin_lock_init(&ap->recv_lock);
216 ap->xaccm[0] = ~0U;
217 ap->xaccm[3] = 0x60000000U;
218 ap->raccm = ~0U;
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);
228 if (err)
229 goto out_free;
231 tty->disc_data = ap;
233 return 0;
235 out_free:
236 kfree(ap);
237 out:
238 return err;
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.
249 static void
250 ppp_sync_close(struct tty_struct *tty)
252 struct syncppp *ap;
254 write_lock(&disc_data_lock);
255 ap = tty->disc_data;
256 tty->disc_data = 0;
257 write_unlock(&disc_data_lock);
258 if (ap == 0)
259 return;
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))
269 down(&ap->dead_sem);
271 ppp_unregister_channel(&ap->chan);
272 if (ap->rpkt != 0)
273 kfree_skb(ap->rpkt);
274 if (ap->tpkt != 0)
275 kfree_skb(ap->tpkt);
276 kfree(ap);
280 * Read does nothing - no data is ever available this way.
281 * Pppd reads and writes packets via /dev/ppp instead.
283 static ssize_t
284 ppp_sync_read(struct tty_struct *tty, struct file *file,
285 unsigned char *buf, size_t count)
287 return -EAGAIN;
291 * Write on the tty does nothing, the packets all come in
292 * from the ppp generic stuff.
294 static ssize_t
295 ppp_sync_write(struct tty_struct *tty, struct file *file,
296 const unsigned char *buf, size_t count)
298 return -EAGAIN;
301 static int
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);
306 int err, val;
308 if (ap == 0)
309 return -ENXIO;
310 err = -EFAULT;
311 switch (cmd) {
312 case PPPIOCGCHAN:
313 err = -ENXIO;
314 if (ap == 0)
315 break;
316 err = -EFAULT;
317 if (put_user(ppp_channel_index(&ap->chan), (int *) arg))
318 break;
319 err = 0;
320 break;
322 case PPPIOCGUNIT:
323 err = -ENXIO;
324 if (ap == 0)
325 break;
326 err = -EFAULT;
327 if (put_user(ppp_unit_number(&ap->chan), (int *) arg))
328 break;
329 err = 0;
330 break;
332 case TCGETS:
333 case TCGETA:
334 err = n_tty_ioctl(tty, file, cmd, arg);
335 break;
337 case TCFLSH:
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);
342 break;
344 case FIONREAD:
345 val = 0;
346 if (put_user(val, (int *) arg))
347 break;
348 err = 0;
349 break;
351 default:
352 err = -ENOIOCTLCMD;
355 sp_put(ap);
356 return err;
359 /* No kernel lock - fine */
360 static unsigned int
361 ppp_sync_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
363 return 0;
366 static int
367 ppp_sync_room(struct tty_struct *tty)
369 return 65535;
372 static void
373 ppp_sync_receive(struct tty_struct *tty, const unsigned char *buf,
374 char *flags, int count)
376 struct syncppp *ap = sp_get(tty);
378 if (ap == 0)
379 return;
380 spin_lock_bh(&ap->recv_lock);
381 ppp_sync_input(ap, buf, flags, count);
382 spin_unlock_bh(&ap->recv_lock);
383 sp_put(ap);
384 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags)
385 && tty->driver->unthrottle)
386 tty->driver->unthrottle(tty);
389 static void
390 ppp_sync_wakeup(struct tty_struct *tty)
392 struct syncppp *ap = sp_get(tty);
394 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
395 if (ap == 0)
396 return;
397 if (ppp_sync_push(ap))
398 ppp_output_wakeup(&ap->chan);
399 sp_put(ap);
403 static struct tty_ldisc ppp_sync_ldisc = {
404 .owner = THIS_MODULE,
405 .magic = TTY_LDISC_MAGIC,
406 .name = "pppsync",
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,
418 static int __init
419 ppp_sync_init(void)
421 int err;
423 err = tty_register_ldisc(N_SYNC_PPP, &ppp_sync_ldisc);
424 if (err != 0)
425 printk(KERN_ERR "PPP_sync: error %d registering line disc.\n",
426 err);
427 return err;
431 * The following routines provide the PPP channel interface.
433 static int
434 ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg)
436 struct syncppp *ap = chan->private;
437 int err, val;
438 u32 accm[8];
440 err = -EFAULT;
441 switch (cmd) {
442 case PPPIOCGFLAGS:
443 val = ap->flags | ap->rbits;
444 if (put_user(val, (int *) arg))
445 break;
446 err = 0;
447 break;
448 case PPPIOCSFLAGS:
449 if (get_user(val, (int *) arg))
450 break;
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);
455 err = 0;
456 break;
458 case PPPIOCGASYNCMAP:
459 if (put_user(ap->xaccm[0], (u32 *) arg))
460 break;
461 err = 0;
462 break;
463 case PPPIOCSASYNCMAP:
464 if (get_user(ap->xaccm[0], (u32 *) arg))
465 break;
466 err = 0;
467 break;
469 case PPPIOCGRASYNCMAP:
470 if (put_user(ap->raccm, (u32 *) arg))
471 break;
472 err = 0;
473 break;
474 case PPPIOCSRASYNCMAP:
475 if (get_user(ap->raccm, (u32 *) arg))
476 break;
477 err = 0;
478 break;
480 case PPPIOCGXASYNCMAP:
481 if (copy_to_user((void *) arg, ap->xaccm, sizeof(ap->xaccm)))
482 break;
483 err = 0;
484 break;
485 case PPPIOCSXASYNCMAP:
486 if (copy_from_user(accm, (void *) arg, sizeof(accm)))
487 break;
488 accm[2] &= ~0x40000000U; /* can't escape 0x5e */
489 accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
490 memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
491 err = 0;
492 break;
494 case PPPIOCGMRU:
495 if (put_user(ap->mru, (int *) arg))
496 break;
497 err = 0;
498 break;
499 case PPPIOCSMRU:
500 if (get_user(val, (int *) arg))
501 break;
502 if (val < PPP_MRU)
503 val = PPP_MRU;
504 ap->mru = val;
505 err = 0;
506 break;
508 default:
509 err = -ENOTTY;
511 return err;
515 * Procedures for encapsulation and framing.
518 struct sk_buff*
519 ppp_sync_txmunge(struct syncppp *ap, struct sk_buff *skb)
521 int proto;
522 unsigned char *data;
523 int islcp;
525 data = skb->data;
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)
536 skb_pull(skb,1);
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);
542 if (npkt == NULL) {
543 kfree_skb(skb);
544 return NULL;
546 skb_reserve(npkt,2);
547 memcpy(skb_put(npkt,skb->len), skb->data, skb->len);
548 kfree_skb(skb);
549 skb = npkt;
551 skb_push(skb,2);
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);
561 return skb;
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.
574 static int
575 ppp_sync_send(struct ppp_channel *chan, struct sk_buff *skb)
577 struct syncppp *ap = chan->private;
579 ppp_sync_push(ap);
581 if (test_and_set_bit(XMIT_FULL, &ap->xmit_flags))
582 return 0; /* already full */
583 skb = ppp_sync_txmunge(ap, skb);
584 if (skb != NULL)
585 ap->tpkt = skb;
586 else
587 clear_bit(XMIT_FULL, &ap->xmit_flags);
589 ppp_sync_push(ap);
590 return 1;
594 * Push as much data as possible out to the tty.
596 static int
597 ppp_sync_push(struct syncppp *ap)
599 int sent, done = 0;
600 struct tty_struct *tty = ap->tty;
601 int tty_stuffed = 0;
603 set_bit(XMIT_WAKEUP, &ap->xmit_flags);
604 if (!spin_trylock_bh(&ap->xmit_lock))
605 return 0;
606 for (;;) {
607 if (test_and_clear_bit(XMIT_WAKEUP, &ap->xmit_flags))
608 tty_stuffed = 0;
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);
612 if (sent < 0)
613 goto flush; /* error, e.g. loss of CD */
614 if (sent < ap->tpkt->len) {
615 tty_stuffed = 1;
616 } else {
617 kfree_skb(ap->tpkt);
618 ap->tpkt = 0;
619 clear_bit(XMIT_FULL, &ap->xmit_flags);
620 done = 1;
622 continue;
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)))
628 break;
629 if (!spin_trylock_bh(&ap->xmit_lock))
630 break;
632 return done;
634 flush:
635 if (ap->tpkt != 0) {
636 kfree_skb(ap->tpkt);
637 ap->tpkt = 0;
638 clear_bit(XMIT_FULL, &ap->xmit_flags);
639 done = 1;
641 spin_unlock_bh(&ap->xmit_lock);
642 return done;
646 * Flush output from our internal buffers.
647 * Called for the TCFLSH ioctl.
649 static void
650 ppp_sync_flush_output(struct syncppp *ap)
652 int done = 0;
654 spin_lock_bh(&ap->xmit_lock);
655 if (ap->tpkt != NULL) {
656 kfree_skb(ap->tpkt);
657 ap->tpkt = 0;
658 clear_bit(XMIT_FULL, &ap->xmit_flags);
659 done = 1;
661 spin_unlock_bh(&ap->xmit_lock);
662 if (done)
663 ppp_output_wakeup(&ap->chan);
667 * Receive-side routines.
670 static inline void
671 process_input_packet(struct syncppp *ap)
673 struct sk_buff *skb;
674 unsigned char *p;
675 int code = 0;
677 skb = ap->rpkt;
678 ap->rpkt = 0;
680 /* strip address/control field if present */
681 p = skb->data;
682 if (p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
683 /* chop off address/control */
684 if (skb->len < 3)
685 goto err;
686 p = skb_pull(skb, 2);
689 /* decompress protocol field if compressed */
690 if (p[0] & 1) {
691 /* protocol is compressed */
692 skb_push(skb, 1)[0] = 0;
693 } else if (skb->len < 2)
694 goto err;
696 /* pass to generic layer */
697 ppp_input(&ap->chan, skb);
698 return;
700 err:
701 kfree_skb(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.
711 static void
712 ppp_sync_input(struct syncppp *ap, const unsigned char *buf,
713 char *flags, int count)
715 struct sk_buff *skb;
716 unsigned char *sp;
718 if (count == 0)
719 return;
721 /* if flag set, then error, ignore frame */
722 if (flags != 0 && *flags) {
723 ppp_input_error(&ap->chan, *flags);
724 return;
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);
735 return;
737 /* Try to get the payload 4-byte aligned */
738 if (buf[0] != PPP_ALLSTATIONS)
739 skb_reserve(skb, 2 + (buf[0] & 1));
740 ap->rpkt = skb;
742 if (count > skb_tailroom(skb)) {
743 /* packet overflowed MRU */
744 ppp_input_error(&ap->chan, 1);
745 } else {
746 sp = skb_put(skb, count);
747 memcpy(sp, buf, count);
748 process_input_packet(ap);
752 static void __exit
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");