⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ppp_synctty.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * PPP synchronous tty channel driver for Linux. * * This is a ppp channel driver that can be used with tty device drivers * that are frame oriented, such as synchronous HDLC devices. * * Complete PPP frames without encoding/decoding are exchanged between * the channel driver and the device driver. *  * The async map IOCTL codes are implemented to keep the user mode * applications happy if they call them. Synchronous PPP does not use * the async maps. * * Copyright 1999 Paul Mackerras. * * Also touched by the grubby hands of Paul Fulghum paulkf@microgate.com * *  This program is free software; you can redistribute it and/or *  modify it under the terms of the GNU General Public License *  as published by the Free Software Foundation; either version *  2 of the License, or (at your option) any later version. * * This driver provides the encapsulation and framing for sending * and receiving PPP frames over sync serial lines.  It relies on * the generic PPP layer to give it frames to send and to process * received frames.  It implements the PPP line discipline. * * Part of the code in this driver was inspired by the old sync-only * PPP driver, written by Michael Callahan and Al Longyear, and * subsequently hacked by Paul Mackerras. * * ==FILEVERSION 20000322== */#include <linux/module.h>#include <linux/kernel.h>#include <linux/skbuff.h>#include <linux/tty.h>#include <linux/netdevice.h>#include <linux/poll.h>#include <linux/ppp_defs.h>#include <linux/if_ppp.h>#include <linux/ppp_channel.h>#include <linux/init.h>#include <asm/uaccess.h>#ifndef spin_trylock_bh#define spin_trylock_bh(lock)	({ int __r; local_bh_disable();	\				   __r = spin_trylock(lock);	\				   if (!__r) local_bh_enable();	\				   __r; })#endif#define PPP_VERSION	"2.4.1"/* Structure for storing local state. */struct syncppp {	struct tty_struct *tty;	unsigned int	flags;	unsigned int	rbits;	int		mru;	spinlock_t	xmit_lock;	spinlock_t	recv_lock;	unsigned long	xmit_flags;	u32		xaccm[8];	u32		raccm;	unsigned int	bytes_sent;	unsigned int	bytes_rcvd;	struct sk_buff	*tpkt;	unsigned long	last_xmit;	struct sk_buff	*rpkt;	struct ppp_channel chan;	/* interface to generic ppp layer */};/* Bit numbers in xmit_flags */#define XMIT_WAKEUP	0#define XMIT_FULL	1/* Bits in rbits */#define SC_RCV_BITS	(SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)#define PPPSYNC_MAX_RQLEN	32	/* arbitrary *//* * Prototypes. */static struct sk_buff* ppp_sync_txmunge(struct syncppp *ap, struct sk_buff *);static int ppp_sync_send(struct ppp_channel *chan, struct sk_buff *skb);static int ppp_sync_ioctl(struct ppp_channel *chan, unsigned int cmd,			  unsigned long arg);static int ppp_sync_push(struct syncppp *ap);static void ppp_sync_flush_output(struct syncppp *ap);static void ppp_sync_input(struct syncppp *ap, const unsigned char *buf,			   char *flags, int count);struct ppp_channel_ops sync_ops = {	ppp_sync_send,	ppp_sync_ioctl};/* * Utility procedures to print a buffer in hex/ascii */static voidppp_print_hex (register __u8 * out, const __u8 * in, int count){	register __u8 next_ch;	static char hex[] = "0123456789ABCDEF";	while (count-- > 0) {		next_ch = *in++;		*out++ = hex[(next_ch >> 4) & 0x0F];		*out++ = hex[next_ch & 0x0F];		++out;	}}static voidppp_print_char (register __u8 * out, const __u8 * in, int count){	register __u8 next_ch;	while (count-- > 0) {		next_ch = *in++;		if (next_ch < 0x20 || next_ch > 0x7e)			*out++ = '.';		else {			*out++ = next_ch;			if (next_ch == '%')   /* printk/syslogd has a bug !! */				*out++ = '%';		}	}	*out = '\0';}static voidppp_print_buffer (const char *name, const __u8 *buf, int count){	__u8 line[44];	if (name != NULL)		printk(KERN_DEBUG "ppp_synctty: %s, count = %d\n", name, count);	while (count > 8) {		memset (line, 32, 44);		ppp_print_hex (line, buf, 8);		ppp_print_char (&line[8 * 3], buf, 8);		printk(KERN_DEBUG "%s\n", line);		count -= 8;		buf += 8;	}	if (count > 0) {		memset (line, 32, 44);		ppp_print_hex (line, buf, count);		ppp_print_char (&line[8 * 3], buf, count);		printk(KERN_DEBUG "%s\n", line);	}}/* * Routines implementing the synchronous PPP line discipline. *//* * Called when a tty is put into line discipline. */static intppp_sync_open(struct tty_struct *tty){	struct syncppp *ap;	int err;	MOD_INC_USE_COUNT;	ap = kmalloc(sizeof(*ap), GFP_KERNEL);	err = -ENOMEM;	if (ap == 0)		goto out;	/* initialize the syncppp structure */	memset(ap, 0, sizeof(*ap));	ap->tty = tty;	ap->mru = PPP_MRU;	spin_lock_init(&ap->xmit_lock);	spin_lock_init(&ap->recv_lock);	ap->xaccm[0] = ~0U;	ap->xaccm[3] = 0x60000000U;	ap->raccm = ~0U;	ap->chan.private = ap;	ap->chan.ops = &sync_ops;	ap->chan.mtu = PPP_MRU;	ap->chan.hdrlen = 2;	/* for A/C bytes */	err = ppp_register_channel(&ap->chan);	if (err)		goto out_free;	tty->disc_data = ap;	return 0; out_free:	kfree(ap); out:	MOD_DEC_USE_COUNT;	return err;}/* * Called when the tty is put into another line discipline * (or it hangs up). */static voidppp_sync_close(struct tty_struct *tty){	struct syncppp *ap = tty->disc_data;	if (ap == 0)		return;	tty->disc_data = 0;	ppp_unregister_channel(&ap->chan);	if (ap->rpkt != 0)		kfree_skb(ap->rpkt);	if (ap->tpkt != 0)		kfree_skb(ap->tpkt);	kfree(ap);	MOD_DEC_USE_COUNT;}/* * Read a PPP frame, for compatibility until pppd is updated. */static ssize_tppp_sync_read(struct tty_struct *tty, struct file *file,	       unsigned char *buf, size_t count){	struct syncppp *ap = tty->disc_data;	if (ap == 0)		return -ENXIO;	return ppp_channel_read(&ap->chan, file, buf, count);}/* * Write a ppp frame, for compatibility until pppd is updated. */static ssize_tppp_sync_write(struct tty_struct *tty, struct file *file,		const unsigned char *buf, size_t count){	struct syncppp *ap = tty->disc_data;	if (ap == 0)		return -ENXIO;	return ppp_channel_write(&ap->chan, buf, count);}static intppp_synctty_ioctl(struct tty_struct *tty, struct file *file,		  unsigned int cmd, unsigned long arg){	struct syncppp *ap = tty->disc_data;	int err, val;	err = -EFAULT;	switch (cmd) {	case PPPIOCGCHAN:		err = -ENXIO;		if (ap == 0)			break;		err = -EFAULT;		if (put_user(ppp_channel_index(&ap->chan), (int *) arg))			break;		err = 0;		break;	case PPPIOCGUNIT:		err = -ENXIO;		if (ap == 0)			break;		err = -EFAULT;		if (put_user(ppp_unit_number(&ap->chan), (int *) arg))			break;		err = 0;		break;	case TCGETS:	case TCGETA:		err = n_tty_ioctl(tty, file, cmd, arg);		break;	case TCFLSH:		/* flush our buffers and the serial port's buffer */		if (arg == TCIOFLUSH || arg == TCOFLUSH)			ppp_sync_flush_output(ap);		err = n_tty_ioctl(tty, file, cmd, arg);		break;	case FIONREAD:		val = 0;		if (put_user(val, (int *) arg))			break;		err = 0;		break;	/*	 * Compatibility calls until pppd is updated.	 */	case PPPIOCGFLAGS:	case PPPIOCSFLAGS:	case PPPIOCGASYNCMAP:	case PPPIOCSASYNCMAP:	case PPPIOCGRASYNCMAP:	case PPPIOCSRASYNCMAP:	case PPPIOCGXASYNCMAP:	case PPPIOCSXASYNCMAP:	case PPPIOCGMRU:	case PPPIOCSMRU:		err = -EPERM;		if (!capable(CAP_NET_ADMIN))			break;		err = ppp_sync_ioctl(&ap->chan, cmd, arg);		break;	case PPPIOCATTACH:	case PPPIOCDETACH:		err = ppp_channel_ioctl(&ap->chan, cmd, arg);		break;	default:		err = -ENOIOCTLCMD;	}	return err;}/* No kernel lock - fine */static unsigned intppp_sync_poll(struct tty_struct *tty, struct file *file, poll_table *wait){	struct syncppp *ap = tty->disc_data;	unsigned int mask;	mask = POLLOUT | POLLWRNORM;	/* compatibility for old pppd */	if (ap != 0)		mask |= ppp_channel_poll(&ap->chan, file, wait);	if (test_bit(TTY_OTHER_CLOSED, &tty->flags) || tty_hung_up_p(file))		mask |= POLLHUP;	return mask;}static intppp_sync_room(struct tty_struct *tty){	return 65535;}static voidppp_sync_receive(struct tty_struct *tty, const unsigned char *buf,		  char *flags, int count){	struct syncppp *ap = tty->disc_data;	if (ap == 0)		return;	spin_lock_bh(&ap->recv_lock);	ppp_sync_input(ap, buf, flags, count);	spin_unlock_bh(&ap->recv_lock);	if (test_and_clear_bit(TTY_THROTTLED, &tty->flags)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -