Chinaunix首页 | 论坛 | 博客
  • 博客访问: 34353
  • 博文数量: 9
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 30
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-24 23:18
文章分类
文章存档

2014年(9)

我的朋友

分类: 嵌入式

2014-04-29 18:52:07

按照数据流过程对函数进行分析。

在linux中,串口字符的接收是通过中断来驱动的。

串口芯片层文件:linux-2.6.22.6\drivers\serial\8250.c
/*
 * In order to handle ISA shared interrupts properly, we need to check
 * that all ports have been serviced, and therefore the ISA interrupt
 * line has been de-asserted.
 *
 * This means we need to loop through all ports. checking that they
 * don't have an interrupt pending.
 */

static irqreturn_t serial8250_interrupt(int irq, void *dev_id)/*串口芯片层*/
{/*检查所有口是否有中断,如果有则进行处理*/
struct irq_info *i = dev_id;/*dev_id就是类型为struct irq_info {spinlock_t lock;struct list_head *head;}的指针*/
struct list_head *l, *end = NULL;/*双向链表头指针struct list_head {struct list_head *next, *prev;};*/
int pass_counter = 0, handled = 0;


DEBUG_INTR("serial8250_interrupt(%d)...", irq);


spin_lock(&i->lock);/*开自旋锁*/


l = i->head;/*链表头*/
do {
struct uart_8250_port *up;/*接口信息(包括一些寄存器)*/
unsigned int iir;


up = list_entry(l, struct uart_8250_port, list);
/*#define list_entry(ptr, type, member) container_of(ptr, type, member)
 * list_entry - get the struct for this entry
 * @ptr: the &struct list_head pointer.
 * @type: the type of the struct this is embedded in.
 * @member: the name of the list_struct within the struct.
 */

iir = serial_in(up, UART_IIR);/*static unsigned int serial_in(struct uart_8250_port *up, int offset),读中断寄存器*/
if (!(iir & UART_IIR_NO_INT)) {/*如果有中断*/
serial8250_handle_port(up);/**/


handled = 1;


end = NULL;
} else if (up->port.iotype == UPIO_DWAPB &&
 (iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
/* The DesignWare APB UART has an Busy Detect (0x07)
* interrupt meaning an LCR write attempt occured while the
* UART was busy. The interrupt must be cleared by reading
* the UART status register (USR) and the LCR re-written. */
unsigned int status;
status = *(volatile u32 *)up->port.private_data;
serial_out(up, UART_LCR, up->lcr);


handled = 1;


end = NULL;
} else if (end == NULL)
end = l;


l = l->next;


if (l == i->head && pass_counter++ > PASS_LIMIT) {
/* If we hit this, we're dead. */
printk(KERN_ERR "serial8250: too much work for "
"irq%d\n", irq);
break;
}
} while (l != end);/*struct list_head *l, *end = NULL;l = i->head.检查链表上的所有接口是否有中断,有则进行处理(函数前面的英文以作说明)*/

spin_unlock(&i->lock);/*关锁*/


DEBUG_INTR("end.\n");


return IRQ_RETVAL(handled);
}

以上代码涉及到的数据结构:
struct irq_info *i = dev_id;

struct irq_info {
spinlock_t lock;/*锁的结构体,如下*/
struct list_head *head;/*双向链表头指针,如下*/
};

typedef struct {
raw_spinlock_t raw_lock;
#if defined(CONFIG_PREEMPT) && defined(CONFIG_SMP)
unsigned int break_lock;
#endif
#ifdef CONFIG_DEBUG_SPINLOCK
unsigned int magic, owner_cpu;
void *owner;
#endif
#ifdef CONFIG_DEBUG_LOCK_ALLOC
struct lockdep_map dep_map;
#endif
} spinlock_t;

/*
 * Simple doubly linked list implementation.
 *
 * Some of the internal functions ("__xxx") are useful when
 * manipulating whole lists rather than single entries, as
 * sometimes we already know the next/prev entries and we can
 * generate better code by using them directly rather than
 * using the generic single-entry routines.
 */
struct list_head {
struct list_head *next, *prev;
};

串口芯片层文件:linux-2.6.22.6\drivers\serial\8250.c
/*
 * This handles the interrupt from one port.
 */
static inline void serial8250_handle_port(struct uart_8250_port *up)/*串口芯片层*/
{/*处理来自某个口的中断:读或者写*/
unsigned int status;
unsigned long flags;


spin_lock_irqsave(&up->port.lock, flags);


status = serial_inp(up, UART_LSR);/*读状态寄存器*/


DEBUG_INTR("status = %x...", status);


if (status & UART_LSR_DR)/*接收数据ready*/
receive_chars(up, &status);/*接收n个字符,并把它们一个个传到串口抽象层**/
check_modem_status(up);
if (status & UART_LSR_THRE)/*传输寄存器为空*/
transmit_chars(up);/*发送*/

spin_unlock_irqrestore(&up->port.lock, flags);
}

串口芯片层文件:linux-2.6.22.6\drivers\serial\8250.c
static void receive_chars(struct uart_8250_port *up, unsigned int *status)/*串口芯片层*/
{/*接收n个字符,并把它们一个个传到串口抽象层,另外根据链路状态寄存器进行不同的处理*/

struct tty_struct *tty = up->port.info->tty;
unsigned char ch, lsr = *status;
int max_count = 256;
char flag;


do {
ch = serial_inp(up, UART_RX);/*从接收缓存读取一个字符*/
flag = TTY_NORMAL;
up->port.icount.rx++;/*对收到的字节计数*/


#ifdef CONFIG_SERIAL_8250_CONSOLE
/*
* Recover the break flag from console xmit
*/
if (up->port.line == up->port.cons->index) {
lsr |= up->lsr_break_flag;
up->lsr_break_flag = 0;
}
#endif


if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
   UART_LSR_FE | UART_LSR_OE))) {
/*
* For statistics only
*/
if (lsr & UART_LSR_BI) {
lsr &= ~(UART_LSR_FE | UART_LSR_PE);
up->port.icount.brk++;
/*
* We do the SysRQ and SAK checking
* here because otherwise the break
* may get masked by ignore_status_mask
* or read_status_mask.
*/
if (uart_handle_break(&up->port))
goto ignore_char;
} else if (lsr & UART_LSR_PE)
up->port.icount.parity++;
else if (lsr & UART_LSR_FE)
up->port.icount.frame++;
if (lsr & UART_LSR_OE)
up->port.icount.overrun++;


/*
* Mask off conditions which should be ignored.
*/
lsr &= up->port.read_status_mask;


if (lsr & UART_LSR_BI) {
DEBUG_INTR("handling break....");
flag = TTY_BREAK;
} else if (lsr & UART_LSR_PE)
flag = TTY_PARITY;
else if (lsr & UART_LSR_FE)
flag = TTY_FRAME;
}
if (uart_handle_sysrq_char(&up->port, ch))
goto ignore_char;


uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);/*把字符传到另一个函数(终端设备层)*/


ignore_char:
lsr = serial_inp(up, UART_LSR);
} while ((lsr & UART_LSR_DR) && (max_count-- > 0));
spin_unlock(&up->port.lock);
tty_flip_buffer_push(tty);
spin_lock(&up->port.lock);
*status = lsr;
}

struct uart_8250_port {
struct uart_port port;
struct timer_list timer; /* "no irq" timer */
struct list_head list; /* ports on this IRQ */
unsigned short capabilities; /* port capabilities */
unsigned short bugs; /* port bugs */
unsigned int tx_loadsz; /* transmit fifo load size */
unsigned char acr;
unsigned char ier;
unsigned char lcr;
unsigned char mcr;
unsigned char mcr_mask; /* mask of user bits */
unsigned char mcr_force; /* mask of forced bits */
unsigned char lsr_break_flag;

/*
* We provide a per-port pm hook.
*/
void (*pm)(struct uart_port *port,
     unsigned int state, unsigned int old);
};

struct uart_port {
spinlock_t lock; /* port lock */
unsigned int iobase; /* in/out[bwl] */
unsigned char __iomem *membase; /* read/write[bwl] */
unsigned int irq; /* irq number */
unsigned int uartclk; /* base uart clock */
unsigned int fifosize; /* tx fifo size */
unsigned char x_char; /* xon/xoff char */
unsigned char regshift; /* reg offset shift */
unsigned char iotype; /* io access style */
unsigned char unused1;


#define UPIO_PORT (0)
#define UPIO_HUB6 (1)
#define UPIO_MEM (2)
#define UPIO_MEM32 (3)
#define UPIO_AU (4) /* Au1x00 type IO */
#define UPIO_TSI (5) /* Tsi108/109 type IO */
#define UPIO_DWAPB (6) /* DesignWare APB UART */
#define UPIO_RM9000 (7) /* RM9000 type IO */


unsigned int read_status_mask; /* driver specific */
unsigned int ignore_status_mask; /* driver specific */
struct uart_info *info; /* pointer to parent info */
struct uart_icount icount; /* statistics */


struct console *cons; /* struct console, if any */
#ifdef CONFIG_SERIAL_CORE_CONSOLE
unsigned long sysrq; /* sysrq timeout */
#endif


upf_t flags;


#define UPF_FOURPORT ((__force upf_t) (1 << 1))
#define UPF_SAK ((__force upf_t) (1 << 2))
#define UPF_SPD_MASK ((__force upf_t) (0x1030))
#define UPF_SPD_HI ((__force upf_t) (0x0010))
#define UPF_SPD_VHI ((__force upf_t) (0x0020))
#define UPF_SPD_CUST ((__force upf_t) (0x0030))
#define UPF_SPD_SHI ((__force upf_t) (0x1000))
#define UPF_SPD_WARP ((__force upf_t) (0x1010))
#define UPF_SKIP_TEST ((__force upf_t) (1 << 6))
#define UPF_AUTO_IRQ ((__force upf_t) (1 << 7))
#define UPF_HARDPPS_CD ((__force upf_t) (1 << 11))
#define UPF_LOW_LATENCY ((__force upf_t) (1 << 13))
#define UPF_BUGGY_UART ((__force upf_t) (1 << 14))
#define UPF_MAGIC_MULTIPLIER ((__force upf_t) (1 << 16))
#define UPF_CONS_FLOW ((__force upf_t) (1 << 23))
#define UPF_SHARE_IRQ ((__force upf_t) (1 << 24))
#define UPF_BOOT_AUTOCONF ((__force upf_t) (1 << 28))
#define UPF_FIXED_PORT ((__force upf_t) (1 << 29))
#define UPF_DEAD ((__force upf_t) (1 << 30))
#define UPF_IOREMAP ((__force upf_t) (1 << 31))


#define UPF_CHANGE_MASK ((__force upf_t) (0x17fff))
#define UPF_USR_MASK ((__force upf_t) (UPF_SPD_MASK|UPF_LOW_LATENCY))


unsigned int mctrl; /* current modem ctrl settings */
unsigned int timeout; /* character-based timeout */
unsigned int type; /* port type */
const struct uart_ops *ops;
unsigned int custom_divisor;
unsigned int line; /* port index */
unsigned long mapbase; /* for ioremap */
struct device *dev; /* parent device */
unsigned char hub6; /* this should be in the 8250 driver */
unsigned char unused[3];
void *private_data; /* generic platform data pointer */
};

struct timer_list {
struct list_head entry;
unsigned long expires;


void (*function)(unsigned long);
unsigned long data;


struct tvec_t_base_s *base;
#ifdef CONFIG_TIMER_STATS
void *start_site;
char start_comm[16];
int start_pid;
#endif
};



串口抽象层文件:linux-2.6.22.6\include\linux\serial_core.h

static inline void uart_insert_char(struct uart_port *port, unsigned int status,
unsigned int overrun, unsigned int ch, unsigned int flag)/*串口抽象层*/
{/*把字符传给tty_insert_flip_char函数*/
struct tty_struct *tty = port->info->tty;/*tty_struct和uart_info结构体定义,如下*/

if ((status & port->ignore_status_mask & ~overrun) == 0)
tty_insert_flip_char(tty, ch, flag);

/*
* Overrun is special.  Since it's reported immediately,
* it doesn't affect the current character.
*/
if (status & ~port->ignore_status_mask & overrun)
tty_insert_flip_char(tty, 0, TTY_OVERRUN);
}


/*
 * This is the state information which is only valid when the port
 * is open; it may be freed by the core driver once the device has
 * been closed.  Either the low level driver or the core can modify
 * stuff here.
 */
struct uart_info {
struct tty_struct *tty;
struct circ_buf xmit;
uif_t flags;

/*
 * Definitions for info->flags.  These are _private_ to serial_core, and
 * are specific to this structure.  They may be queried by low level drivers.
 */
#define UIF_CHECK_CD ((__force uif_t) (1 << 25))
#define UIF_CTS_FLOW ((__force uif_t) (1 << 26))
#define UIF_NORMAL_ACTIVE ((__force uif_t) (1 << 29))
#define UIF_INITIALIZED ((__force uif_t) (1 << 31))
#define UIF_SUSPENDED ((__force uif_t) (1 << 30))

int blocked_open;

struct tasklet_struct tlet;

wait_queue_head_t open_wait;
wait_queue_head_t delta_msr_wait;
};

/*
 * Where all of the state associated with a tty is kept while the tty
 * is open.  Since the termios state should be kept even if the tty
 * has been closed --- for things like the baud rate, etc --- it is
 * not stored here, but rather a pointer to the real state is stored
 * here.  Possible the winsize structure should have the same
 * treatment, but (1) the default 80x24 is usually right and (2) it's
 * most often used by a windowing system, which will set the correct
 * size each time the window is created or resized anyway.
 * - TYT, 9/14/92
 */
struct tty_struct {
int magic;
struct tty_driver *driver;
int index;
struct tty_ldisc ldisc;
struct mutex termios_mutex;
struct ktermios *termios, *termios_locked;
char name[64];
struct pid *pgrp;
struct pid *session;
unsigned long flags;
int count;
struct winsize winsize;
unsigned char stopped:1, hw_stopped:1, flow_stopped:1, packet:1;
unsigned char low_latency:1, warned:1;
unsigned char ctrl_status;
unsigned int receive_room; /* Bytes free for queue */

struct tty_struct *link;
struct fasync_struct *fasync;
struct tty_bufhead buf;
int alt_speed; /* For magic substitution of 38400 bps */
wait_queue_head_t write_wait;
wait_queue_head_t read_wait;
struct work_struct hangup_work;
void *disc_data;
void *driver_data;
struct list_head tty_files;


#define N_TTY_BUF_SIZE 4096

/*
* The following is data for the N_TTY line discipline.  For
* historical reasons, this is included in the tty structure.
*/
unsigned int column;
unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
unsigned char closing:1;
unsigned short minimum_to_wake;
unsigned long overrun_time;
int num_overrun;
unsigned long process_char_map[256/(8*sizeof(unsigned long))];
char *read_buf;
int read_head;
int read_tail;
int read_cnt;
unsigned long read_flags[N_TTY_BUF_SIZE/(8*sizeof(unsigned long))];
int canon_data;
unsigned long canon_head;
unsigned int canon_column;
struct mutex atomic_read_lock;
struct mutex atomic_write_lock;
unsigned char *write_buf;
int write_cnt;
spinlock_t read_lock;
/* If the tty has a pending do_SAK, queue it here - akpm */
struct work_struct SAK_work;
};


终端设备层文件:linux-2.6.22.6\include\linux\tty_flip.h

static inline int tty_insert_flip_char(struct tty_struct *tty,
unsigned char ch, char flag)/*终端设备层*/
{/*把字符和标志放入缓冲区*/
struct tty_buffer *tb = tty->buf.tail;
if (tb && tb->used < tb->size) {
tb->flag_buf_ptr[tb->used] = flag;/*保存标志*/
tb->char_buf_ptr[tb->used++] = ch;/*保存字符*/
return 1;
}
return tty_insert_flip_string_flags(tty, &ch, &flag, 1);
}


终端设备层文件:linux-2.6.22.6\drivers\char\tty_io.c

/**
 * tty_flip_buffer_push - terminal
 * @tty: tty to push
 *
 * Queue a push of the terminal flip buffers to the line discipline. This
 * function must not be called from IRQ context if tty->low_latency is set.
 *
 * In the event of the queue being busy for flipping the work will be
 * held off and retried later.
 *
 * Locking: tty buffer lock. Driver locks in low latency mode.
 */
void tty_flip_buffer_push(struct tty_struct *tty)/*终端设备层*/
{/*把缓冲区中的字符推到行规层*/
unsigned long flags;
spin_lock_irqsave(&tty->buf.lock, flags);
if (tty->buf.tail != NULL)
tty->buf.tail->commit = tty->buf.tail->used;
spin_unlock_irqrestore(&tty->buf.lock, flags);

if (tty->low_latency)
flush_to_ldisc(&tty->buf.work.work);/**/
else
schedule_delayed_work(&tty->buf.work, 1);
}


终端设备层文件:linux-2.6.22.6\drivers\char\tty_io.c

/**
 * flush_to_ldisc
 * @work: tty structure passed from work queue.
 *
 * This routine is called out of the software interrupt to flush data
 * from the buffer chain to the line discipline.
 *
 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
 * while invoking the line discipline receive_buf method. The
 * receive_buf method is single threaded for each tty instance.
 */
static void flush_to_ldisc(struct work_struct *work)/*终端设备层*/
{/*把数据从缓存推到行规层*/
struct tty_struct *tty =
container_of(work, struct tty_struct, buf.work.work);
unsigned long flags;
struct tty_ldisc *disc;/*如下*/
struct tty_buffer *tbuf, *head;/*如下*/
char *char_buf;
unsigned char *flag_buf;

disc = tty_ldisc_ref(tty);/*disc是个结构体变量,里面有很多操作函数*/
if (disc == NULL) /*  !TTY_LDISC */
return;

spin_lock_irqsave(&tty->buf.lock, flags);
head = tty->buf.head;
if (head != NULL) {
tty->buf.head = NULL;
for (;;) {
int count = head->commit - head->read;
if (!count) {
if (head->next == NULL)
break;
tbuf = head;
head = head->next;
tty_buffer_free(tty, tbuf);
continue;
}
if (!tty->receive_room) {
schedule_delayed_work(&tty->buf.work, 1);
break;
}
if (count > tty->receive_room)
count = tty->receive_room;
char_buf = head->char_buf_ptr + head->read;
flag_buf = head->flag_buf_ptr + head->read;
head->read += count;
spin_unlock_irqrestore(&tty->buf.lock, flags);
disc->receive_buf(tty, char_buf, flag_buf, count);/*即static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,char *fp, int count)*/
spin_lock_irqsave(&tty->buf.lock, flags);
}
tty->buf.head = head;
}
spin_unlock_irqrestore(&tty->buf.lock, flags);

tty_ldisc_deref(disc);
}


struct tty_ldisc {
int magic;
char *name;
int num;
int flags;

/*
* The following routines are called from above.
*/
int (*open)(struct tty_struct *);
void (*close)(struct tty_struct *);
void (*flush_buffer)(struct tty_struct *tty);
ssize_t (*chars_in_buffer)(struct tty_struct *tty);
ssize_t (*read)(struct tty_struct * tty, struct file * file,
unsigned char __user * buf, size_t nr);
ssize_t (*write)(struct tty_struct * tty, struct file * file,
const unsigned char * buf, size_t nr);
int (*ioctl)(struct tty_struct * tty, struct file * file,
unsigned int cmd, unsigned long arg);
long (*compat_ioctl)(struct tty_struct * tty, struct file * file,
unsigned int cmd, unsigned long arg);
void (*set_termios)(struct tty_struct *tty, struct ktermios * old);
unsigned int (*poll)(struct tty_struct *, struct file *,
    struct poll_table_struct *);
int (*hangup)(struct tty_struct *tty);

/*
* The following routines are called from below.
*/
void (*receive_buf)(struct tty_struct *, const unsigned char *cp,
      char *fp, int count);
void (*write_wakeup)(struct tty_struct *);

struct  module *owner;

int refcount;
};


struct tty_buffer {
struct tty_buffer *next;
char *char_buf_ptr;
unsigned char *flag_buf_ptr;
int used;
int size;
int commit;
int read;
/* Data points here */
unsigned long data[0];
};


disc->receive_buf(tty, char_buf, flag_buf, count);/*本函数的前后代码循环运行*/
即linux-2.6.22.6\drivers\char\n_tty.c的函数
static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
     char *fp, int count)【行规层】

/**
 * n_tty_receive_buf - data receive
 * @tty: terminal device
 * @cp: buffer
 * @fp: flag buffer
 * @count: characters
 *
 * Called by the terminal driver when a block of characters has
 * been received. This function must be called from soft contexts
 * not from interrupt context. The driver is responsible for making
 * calls one at a time and in order (or using flush_to_ldisc)
 */
static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
     char *fp, int count)/*行规层*/
{/*处理输入字符, 将输出字符缓冲在终端的循环缓冲区(read_buf)之中*/
const unsigned char *p;
char *f, flags = TTY_NORMAL;
int i;
char buf[64];
unsigned long cpuflags;

if (!tty->read_buf)
return;

if (tty->real_raw) {
spin_lock_irqsave(&tty->read_lock, cpuflags);
i = min(N_TTY_BUF_SIZE - tty->read_cnt,
N_TTY_BUF_SIZE - tty->read_head);
i = min(count, i);
memcpy(tty->read_buf + tty->read_head, cp, i);
tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
tty->read_cnt += i;
cp += i;
count -= i;

i = min(N_TTY_BUF_SIZE - tty->read_cnt,
N_TTY_BUF_SIZE - tty->read_head);
i = min(count, i);
memcpy(tty->read_buf + tty->read_head, cp, i);
tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
tty->read_cnt += i;
spin_unlock_irqrestore(&tty->read_lock, cpuflags);
} else {
for (i=count, p = cp, f = fp; i; i--, p++) {
if (f)
flags = *f++;
switch (flags) {
case TTY_NORMAL:
n_tty_receive_char(tty, *p);/**/
break;
case TTY_BREAK:
n_tty_receive_break(tty);
break;
case TTY_PARITY:
case TTY_FRAME:
n_tty_receive_parity_error(tty, *p);
break;
case TTY_OVERRUN:
n_tty_receive_overrun(tty);
break;
default:
printk("%s: unknown flag %d\n",
      tty_name(tty, buf), flags);
break;
}
}
if (tty->driver->flush_chars)
tty->driver->flush_chars(tty);
}

n_tty_set_room(tty);

if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
kill_fasync(&tty->fasync, SIGIO, POLL_IN);
if (waitqueue_active(&tty->read_wait))
wake_up_interruptible(&tty->read_wait);
}
/*
* Check the remaining room for the input canonicalization
* mode.  We don't want to throttle the driver if we're in
* canonical mode and don't have a newline yet!
*/
if (tty->receive_room < TTY_THRESHOLD_THROTTLE) {
/* check TTY_THROTTLED first so it indicates our state */
if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
   tty->driver->throttle)
tty->driver->throttle(tty);
}
}



行规层文件:linux-2.6.22.6\drivers\char\n_tty.c
/**
 * n_tty_receive_char - perform processing
 * @tty: terminal device
 * @c: character
 *
 * Process an individual character of input received from the driver.
 * This is serialized with respect to itself by the rules for the 
 * driver above.
 */
/*对于普通字符将调用echo_char将它回显;对于回车符,回显之后还要调用waitqueue_active唤醒等待数据的进程*/
static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)/*行规层*/
{/*处理驱动输入的单个字符,不同的字符进行不同的处理:"Ctrl+C"发送SIGINT*/
unsigned long flags;

if (tty->raw) {
put_tty_queue(c, tty);
return;
}

if (tty->stopped && !tty->flow_stopped &&
   I_IXON(tty) && I_IXANY(tty)) {
start_tty(tty);
return;
}

if (I_ISTRIP(tty))
c &= 0x7f;
if (I_IUCLC(tty) && L_IEXTEN(tty))
c=tolower(c);

if (tty->closing) {
if (I_IXON(tty)) {
if (c == START_CHAR(tty))
start_tty(tty);
else if (c == STOP_CHAR(tty))
stop_tty(tty);
}
return;
}
/*
* If the previous character was LNEXT, or we know that this
* character is not one of the characters that we'll have to
* handle specially, do shortcut processing to speed things
* up.
*/
if (!test_bit(c, tty->process_char_map) || tty->lnext) {
finish_erasing(tty);
tty->lnext = 0;
if (L_ECHO(tty)) {
if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
put_char('\a', tty); /* beep if no space */
return;
}
/* Record the column of first canon char. */
if (tty->canon_head == tty->read_head)
tty->canon_column = tty->column;
echo_char(c, tty);
}
if (I_PARMRK(tty) && c == (unsigned char) '\377')
put_tty_queue(c, tty);
put_tty_queue(c, tty);
return;
}

if (c == '\r') {
if (I_IGNCR(tty))
return;
if (I_ICRNL(tty))
c = '\n';
} else if (c == '\n' && I_INLCR(tty))
c = '\r';
if (I_IXON(tty)) {
if (c == START_CHAR(tty)) {
start_tty(tty);
return;
}
if (c == STOP_CHAR(tty)) {
stop_tty(tty);
return;
}
}
if (L_ISIG(tty)) {
int signal;
signal = SIGINT;
if (c == INTR_CHAR(tty))
goto send_signal;
signal = SIGQUIT;
if (c == QUIT_CHAR(tty))
goto send_signal;
signal = SIGTSTP;
if (c == SUSP_CHAR(tty)) {
send_signal:
isig(signal, tty, 0);
return;
}
}
if (tty->icanon) {
if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
   (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
eraser(c, tty);
return;
}
if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
tty->lnext = 1;
if (L_ECHO(tty)) {
finish_erasing(tty);
if (L_ECHOCTL(tty)) {
put_char('^', tty);
put_char('\b', tty);
}
}
return;
}
if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
   L_IEXTEN(tty)) {
unsigned long tail = tty->canon_head;

finish_erasing(tty);
echo_char(c, tty);
opost('\n', tty);
while (tail != tty->read_head) {
echo_char(tty->read_buf[tail], tty);
tail = (tail+1) & (N_TTY_BUF_SIZE-1);
}
return;
}
if (c == '\n') {
if (L_ECHO(tty) || L_ECHONL(tty)) {
if (tty->read_cnt >= N_TTY_BUF_SIZE-1)
put_char('\a', tty);
opost('\n', tty);
}
goto handle_newline;
}
if (c == EOF_CHAR(tty)) {
       if (tty->canon_head != tty->read_head)
       set_bit(TTY_PUSH, &tty->flags);
c = __DISABLED_CHAR;
goto handle_newline;
}
if ((c == EOL_CHAR(tty)) ||
   (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
/*
* XXX are EOL_CHAR and EOL2_CHAR echoed?!?
*/
if (L_ECHO(tty)) {
if (tty->read_cnt >= N_TTY_BUF_SIZE-1)
put_char('\a', tty);
/* Record the column of first canon char. */
if (tty->canon_head == tty->read_head)
tty->canon_column = tty->column;
echo_char(c, tty);
}
/*
* XXX does PARMRK doubling happen for
* EOL_CHAR and EOL2_CHAR?
*/
if (I_PARMRK(tty) && c == (unsigned char) '\377')
put_tty_queue(c, tty);

handle_newline:
spin_lock_irqsave(&tty->read_lock, flags);
set_bit(tty->read_head, tty->read_flags);
put_tty_queue_nolock(c, tty);
tty->canon_head = tty->read_head;
tty->canon_data++;
spin_unlock_irqrestore(&tty->read_lock, flags);
kill_fasync(&tty->fasync, SIGIO, POLL_IN);
if (waitqueue_active(&tty->read_wait))
wake_up_interruptible(&tty->read_wait);
return;
}
}

finish_erasing(tty);
if (L_ECHO(tty)) {
if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
put_char('\a', tty); /* beep if no space */
return;
}
if (c == '\n')
opost('\n', tty);
else {
/* Record the column of first canon char. */
if (tty->canon_head == tty->read_head)
tty->canon_column = tty->column;
echo_char(c, tty);
}
}

if (I_PARMRK(tty) && c == (unsigned char) '\377')
put_tty_queue(c, tty);

put_tty_queue(c, tty);
}


阅读(2342) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~