From: Alan Cox <alan@lxorguk.ukuu.org.uk>

The API and code have been through various bits of initial review by
serial driver people but they definitely need to live somewhere for a
while so the unconverted drivers can get knocked into shape, existing
drivers that have been updated can be better tuned and bugs whacked out.

This replaces the tty flip buffers with kmalloc objects in rings. In the
normal situation for an IRQ driven serial port at typical speeds the
behaviour is pretty much the same, two buffers end up allocated and the
kernel cycles between them as before.

When there are delays or at high speed we now behave far better as the
buffer pool can grow a bit rather than lose characters. This also means
that we can operate at higher speeds reliably.

For drivers that receive characters in blocks (DMA based, USB and
especially virtualisation) the layer allows a lot of driver specific
code that works around the tty layer with private secondary queues to be
removed. The IBM folks need this sort of layer, the smart serial port
people do, the virtualisers do (because a virtualised tty typically
operates at infinite speed rather than emulating 9600 baud). 

Finally many drivers had invalid and unsafe attempts to avoid buffer
overflows by directly invoking tty methods extracted out of the innards
of work queue structs. These are no longer needed and all go away. That
fixes various random hangs with serial ports on overflow.

The other change in here is to optimise the receive_room path that is
used by some callers. It turns out that only one ldisc uses receive room
except asa constant and it updates it far far less than the value is
read. We thus make it a variable not a function call.

I expect the code to contain bugs due to the size alone but I'll be
watching and squashing them and feeding out new patches as it goes.

Signed-off-by: Alan Cox <alan@redhat.com>

OK I have a new patch for you. Various things are untested and probably
wrong but compile in this one. Hopefully not too many. On the other hand
I've fixed a kernel memory scribble in cyclades and the gadget/serial.c
driver no longer passes random kernel memory to the tty layer so might
work.

There are two I really can't fix. serial/jsm has intimate knowledge of
and duplicates chunks of the tty layer. Its a disgusting mess that
should never have been merged. On the positive side I believe that for
someone with the chip docs the fix is to delete most of the driver which
is all code that works around our tty layer and use the new buffers.

Isdn4linux is too complex to fix, and its pointless as just looking over
the routines to fix turns up other totally invalid activity like hacking
the network buffer skb->len directly.

Signed-off-by: Andrew Morton <akpm@osdl.org>
---

 drivers/bluetooth/hci_ldisc.c         |   16 --
 drivers/char/amiserial.c              |   29 +--
 drivers/char/cyclades.c               |   89 +++--------
 drivers/char/epca.c                   |   17 --
 drivers/char/esp.c                    |   71 +++------
 drivers/char/hvc_console.c            |    6 
 drivers/char/isicom.c                 |   24 +--
 drivers/char/moxa.c                   |    5 
 drivers/char/mxser.c                  |    3 
 drivers/char/n_hdlc.c                 |   18 --
 drivers/char/n_r3964.c                |   10 -
 drivers/char/n_tty.c                  |   66 ++++----
 drivers/char/pcmcia/synclink_cs.c     |   28 +--
 drivers/char/pty.c                    |    4 
 drivers/char/riscom8.c                |   34 +---
 drivers/char/rocket.c                 |   19 +-
 drivers/char/selection.c              |    5 
 drivers/char/ser_a2232.c              |    8 -
 drivers/char/serial167.c              |   35 +---
 drivers/char/speakup/speakup.c        |    2 
 drivers/char/specialix.c              |   40 +----
 drivers/char/sx.c                     |   13 -
 drivers/char/synclink.c               |   43 ++---
 drivers/char/synclinkmp.c             |   34 +---
 drivers/char/tty_io.c                 |  264 ++++++++++++++++++++++++++++++----
 drivers/char/viocons.c                |    3 
 drivers/char/vme_scc.c                |   16 --
 drivers/input/serio/serport.c         |   13 -
 drivers/isdn/capi/capi.c              |    3 
 drivers/net/hamradio/6pack.c          |    7 
 drivers/net/hamradio/mkiss.c          |    1 
 drivers/net/irda/irtty-sir.c          |   18 --
 drivers/net/ppp_async.c               |    9 -
 drivers/net/ppp_synctty.c             |    9 -
 drivers/net/slip.c                    |   11 -
 drivers/net/wan/pc300_tty.c           |    2 
 drivers/net/wan/x25_asy.c             |    7 
 drivers/net/wireless/strip.c          |   10 -
 drivers/serial/21285.c                |    9 -
 drivers/serial/68328serial.c          |   18 +-
 drivers/serial/68360serial.c          |   54 +-----
 drivers/serial/8250.c                 |   13 -
 drivers/serial/amba-pl010.c           |    9 -
 drivers/serial/amba-pl011.c           |    9 -
 drivers/serial/au1x00_uart.c          |   31 +--
 drivers/serial/clps711x.c             |    2 
 drivers/serial/dz.c                   |    2 
 drivers/serial/icom.c                 |   54 ++----
 drivers/serial/imx.c                  |    3 
 drivers/serial/ioc4_serial.c          |   10 -
 drivers/serial/ip22zilog.c            |   34 +---
 drivers/serial/m32r_sio.c             |   31 +--
 drivers/serial/mcfserial.c            |   23 --
 drivers/serial/mpc52xx_uart.c         |   46 ++---
 drivers/serial/mpsc.c                 |    6 
 drivers/serial/mux.c                  |    9 -
 drivers/serial/pmac_zilog.c           |   38 +---
 drivers/serial/pxa.c                  |    8 -
 drivers/serial/s3c2410.c              |   10 -
 drivers/serial/sa1100.c               |    2 
 drivers/serial/serial_lh7a40x.c       |    9 -
 drivers/serial/serial_txx9.c          |    8 -
 drivers/serial/sh-sci.c               |   81 ++++------
 drivers/serial/sn_console.c           |    6 
 drivers/serial/sunsab.c               |   38 +---
 drivers/serial/sunsu.c                |   32 +---
 drivers/serial/sunzilog.c             |   34 +---
 drivers/serial/uart00.c               |    3 
 drivers/serial/vr41xx_siu.c           |    5 
 drivers/usb/class/cdc-acm.c           |   11 -
 drivers/usb/gadget/serial.c           |   19 +-
 drivers/usb/serial/cyberjack.c        |   11 -
 drivers/usb/serial/cypress_m8.c       |    4 
 drivers/usb/serial/digi_acceleport.c  |   30 +--
 drivers/usb/serial/empeg.c            |   16 --
 drivers/usb/serial/ftdi_sio.c         |   15 -
 drivers/usb/serial/garmin_gps.c       |   13 -
 drivers/usb/serial/generic.c          |   11 -
 drivers/usb/serial/io_edgeport.c      |   20 --
 drivers/usb/serial/io_ti.c            |   20 --
 drivers/usb/serial/ipaq.c             |   12 -
 drivers/usb/serial/ipw.c              |   11 -
 drivers/usb/serial/kl5kusb105.c       |   13 -
 drivers/usb/serial/kobil_sct.c        |   11 -
 drivers/usb/serial/option.c           |    9 -
 drivers/usb/serial/pl2303.c           |    8 -
 drivers/usb/serial/ti_usb_3410_5052.c |   20 --
 drivers/usb/serial/visor.c            |   11 -
 include/linux/kbd_kern.h              |    2 
 include/linux/tty.h                   |   25 ++-
 include/linux/tty_flip.h              |   20 +-
 include/linux/tty_ldisc.h             |    9 -
 net/bluetooth/rfcomm/tty.c            |    9 -
 93 files changed, 739 insertions(+), 1220 deletions(-)

diff -puN drivers/bluetooth/hci_ldisc.c~tty-layer-buffering-revamp drivers/bluetooth/hci_ldisc.c
--- 25/drivers/bluetooth/hci_ldisc.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/bluetooth/hci_ldisc.c	Wed Aug 31 12:50:56 2005
@@ -276,6 +276,7 @@ static int hci_uart_tty_open(struct tty_
 
 	tty->disc_data = hu;
 	hu->tty = tty;
+	tty->receive_room = 65536;
 
 	spin_lock_init(&hu->rx_lock);
 
@@ -343,20 +344,6 @@ static void hci_uart_tty_wakeup(struct t
 		hci_uart_tx_wakeup(hu);
 }
 
-/* hci_uart_tty_room()
- * 
- *    Callback function from tty driver. Return the amount of 
- *    space left in the receiver's buffer to decide if remote
- *    transmitter is to be throttled.
- *
- * Arguments:        tty    pointer to associated tty instance data
- * Return Value:    number of bytes left in receive buffer
- */
-static int hci_uart_tty_room (struct tty_struct *tty)
-{
-	return 65536;
-}
-
 /* hci_uart_tty_receive()
  * 
  *     Called by tty low level driver when receive data is
@@ -542,7 +529,6 @@ static int __init hci_uart_init(void)
 	hci_uart_ldisc.write       = hci_uart_tty_write;
 	hci_uart_ldisc.ioctl       = hci_uart_tty_ioctl;
 	hci_uart_ldisc.poll        = hci_uart_tty_poll;
-	hci_uart_ldisc.receive_room= hci_uart_tty_room;
 	hci_uart_ldisc.receive_buf = hci_uart_tty_receive;
 	hci_uart_ldisc.write_wakeup= hci_uart_tty_wakeup;
 	hci_uart_ldisc.owner       = THIS_MODULE;
diff -puN drivers/char/amiserial.c~tty-layer-buffering-revamp drivers/char/amiserial.c
--- 25/drivers/char/amiserial.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/amiserial.c	Wed Aug 31 12:50:56 2005
@@ -265,8 +265,9 @@ static _INLINE_ void receive_chars(struc
         int status;
 	int serdatr;
 	struct tty_struct *tty = info->tty;
-	unsigned char ch;
+	unsigned char ch, flag;
 	struct	async_icount *icount;
+	int oe = 0;
 
 	icount = &info->state->icount;
 
@@ -282,15 +283,12 @@ static _INLINE_ void receive_chars(struc
 	    status |= UART_LSR_OE;
 
 	ch = serdatr & 0xff;
-	if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-	  goto ignore_char;
-	*tty->flip.char_buf_ptr = ch;
 	icount->rx++;
 
 #ifdef SERIAL_DEBUG_INTR
 	printk("DR%02x:%02x...", ch, status);
 #endif
-	*tty->flip.flag_buf_ptr = 0;
+	flag = TTY_NORMAL;
 
 	/*
 	 * We don't handle parity or frame errors - but I have left
@@ -327,32 +325,25 @@ static _INLINE_ void receive_chars(struc
 #ifdef SERIAL_DEBUG_INTR
 	    printk("handling break....");
 #endif
-	    *tty->flip.flag_buf_ptr = TTY_BREAK;
+	    flag = TTY_BREAK;
 	    if (info->flags & ASYNC_SAK)
 	      do_SAK(tty);
 	  } else if (status & UART_LSR_PE)
-	    *tty->flip.flag_buf_ptr = TTY_PARITY;
+	    flag = TTY_PARITY;
 	  else if (status & UART_LSR_FE)
-	    *tty->flip.flag_buf_ptr = TTY_FRAME;
+	    flag = TTY_FRAME;
 	  if (status & UART_LSR_OE) {
 	    /*
 	     * Overrun is special, since it's
 	     * reported immediately, and doesn't
 	     * affect the current character
 	     */
-	    if (tty->flip.count < TTY_FLIPBUF_SIZE) {
-	      tty->flip.count++;
-	      tty->flip.flag_buf_ptr++;
-	      tty->flip.char_buf_ptr++;
-	      *tty->flip.flag_buf_ptr = TTY_OVERRUN;
-	    }
+	     oe = 1;
 	  }
 	}
-	tty->flip.flag_buf_ptr++;
-	tty->flip.char_buf_ptr++;
-	tty->flip.count++;
- ignore_char:
-
+	tty_insert_flip_char(tty, ch, flag);
+	if (oe == 1)
+		tty_insert_flip_char(tty, 0, TTY_OVERRUN);
 	tty_flip_buffer_push(tty);
 }
 
diff -puN drivers/char/cyclades.c~tty-layer-buffering-revamp drivers/char/cyclades.c
--- 25/drivers/char/cyclades.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/cyclades.c	Wed Aug 31 12:50:56 2005
@@ -641,6 +641,7 @@ static char rcsid[] =
 #include <linux/timer.h>
 #include <linux/interrupt.h>
 #include <linux/tty.h>
+#include <linux/tty_flip.h>
 #include <linux/serial.h>
 #include <linux/major.h>
 #include <linux/string.h>
@@ -1086,7 +1087,7 @@ cyy_interrupt(int irq, void *dev_id, str
   int had_work;
   int mdm_change;
   int mdm_status;
-
+  int len;
     if((cinfo = (struct cyclades_card *)dev_id) == 0){
 #ifdef CY_DEBUG_INTERRUPTS
 	printk("cyy_interrupt: spurious interrupt %d\n\r", irq);
@@ -1163,63 +1164,43 @@ cyy_interrupt(int irq, void *dev_id, str
 				info->icount.rx++;
                                 continue;
                             }
-                            if (tty->flip.count < TTY_FLIPBUF_SIZE){
-                                tty->flip.count++;
+                            if (tty_buffer_request_room(tty, 1)) {
                                 if (data & info->read_status_mask){
                                     if(data & CyBREAK){
-                                        *tty->flip.flag_buf_ptr++ =
-	    						    TTY_BREAK;
-                                        *tty->flip.char_buf_ptr++ =
-					  cy_readb(base_addr+(CyRDSR<<index));
+                                        tty_insert_flip_char(tty, cy_readb(base_addr+(CyRDSR<<index)), TTY_BREAK);
 					info->icount.rx++;
                                         if (info->flags & ASYNC_SAK){
                                             do_SAK(tty);
                                         }
                                     }else if(data & CyFRAME){
-                                        *tty->flip.flag_buf_ptr++ =
-							    TTY_FRAME;
-                                        *tty->flip.char_buf_ptr++ =
-					  cy_readb(base_addr+(CyRDSR<<index));
+                                        tty_insert_flip_char(tty, cy_readb(base_addr+(CyRDSR<<index)), TTY_FRAME);
 					info->icount.rx++;
 					info->idle_stats.frame_errs++;
                                     }else if(data & CyPARITY){
-                                        *tty->flip.flag_buf_ptr++ =
-							    TTY_PARITY;
-                                        *tty->flip.char_buf_ptr++ =
-					  cy_readb(base_addr+(CyRDSR<<index));
+					/* Pieces of seven... */
+                                        tty_insert_flip_char(tty, cy_readb(base_addr+(CyRDSR<<index)), TTY_PARITY);
 					info->icount.rx++;
 					info->idle_stats.parity_errs++;
                                     }else if(data & CyOVERRUN){
-                                        *tty->flip.flag_buf_ptr++ =
-							    TTY_OVERRUN;
-                                        *tty->flip.char_buf_ptr++ = 0;
+                                        tty_insert_flip_char(tty, 0, TTY_OVERRUN);
 					info->icount.rx++;
                                         /* If the flip buffer itself is
                                            overflowing, we still lose
                                            the next incoming character.
                                          */
-                                        if(tty->flip.count
-					           < TTY_FLIPBUF_SIZE){
-                                            tty->flip.count++;
-                                            *tty->flip.flag_buf_ptr++ =
-							     TTY_NORMAL;
-                                           *tty->flip.char_buf_ptr++ =
-					    cy_readb(base_addr+(CyRDSR<<index));
-					    info->icount.rx++;
-                                        }
+                                        tty_insert_flip_char(tty, cy_readb(base_addr+(CyRDSR<<index)), TTY_FRAME);
+				        info->icount.rx++;
 					info->idle_stats.overruns++;
                                     /* These two conditions may imply */
                                     /* a normal read should be done. */
                                     /* }else if(data & CyTIMEOUT){ */
                                     /* }else if(data & CySPECHAR){ */
-                                    }else{
-                                        *tty->flip.flag_buf_ptr++ = 0;
-                                        *tty->flip.char_buf_ptr++ = 0;
-					info->icount.rx++;
+                                    }else {
+					tty_insert_flip_char(tty, 0, TTY_NORMAL);
+				        info->icount.rx++;
                                     }
                                 }else{
-                                    *tty->flip.flag_buf_ptr++ = 0;
-                                    *tty->flip.char_buf_ptr++ = 0;
+				    tty_insert_flip_char(tty, 0, TTY_NORMAL);
 				    info->icount.rx++;
                                 }
                             }else{
@@ -1240,14 +1221,10 @@ cyy_interrupt(int irq, void *dev_id, str
                                info->mon.char_max = char_count;
                             info->mon.char_last = char_count;
 #endif
-                            while(char_count--){
-                                if (tty->flip.count >= TTY_FLIPBUF_SIZE){
-                                        break;
-                                }
-                                tty->flip.count++;
+			    len = tty_buffer_request_room(tty, char_count);
+                            while(len--){
                                 data = cy_readb(base_addr+(CyRDSR<<index));
-                                *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
-                                *tty->flip.char_buf_ptr++ = data;
+				tty_insert_flip_char(tty, data, TTY_NORMAL);
 				info->idle_stats.recv_bytes++;
 				info->icount.rx++;
 #ifdef CY_16Y_HACK
@@ -1256,7 +1233,7 @@ cyy_interrupt(int irq, void *dev_id, str
                             }
                              info->idle_stats.recv_idle = jiffies;
                         }
-                        schedule_delayed_work(&tty->flip.work, 1);
+                        schedule_delayed_work(&tty->buf.work, 1);
                     }
                     /* end of service */
                     cy_writeb(base_addr+(CyRIR<<index), (save_xir & 0x3f));
@@ -1551,6 +1528,7 @@ cyz_handle_rx(struct cyclades_port *info
   struct cyclades_card *cinfo = &cy_card[info->card];
   struct tty_struct *tty = info->tty;
   volatile int char_count;
+  int len;
 #ifdef BLOCKMOVE
   int small_count;
 #else
@@ -1606,18 +1584,11 @@ cyz_handle_rx(struct cyclades_port *info
 		tty->flip.count += small_count;
 	    }
 #else
-	    while(char_count--){
-		if (tty->flip.count >= N_TTY_BUF_SIZE - tty->read_cnt)
-                    break;
-
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-		    break;
-
+	    len = tty_buffer_request_room(tty, char_count);
+	    while(len--){
 		data = cy_readb(cinfo->base_addr + rx_bufaddr + new_rx_get);
 		new_rx_get = (new_rx_get + 1) & (rx_bufsize - 1);
-		tty->flip.count++;
-		*tty->flip.flag_buf_ptr++ = TTY_NORMAL;
-		*tty->flip.char_buf_ptr++ = data;
+		tty_insert_flip_char(tty, data, TTY_NORMAL);
 		info->idle_stats.recv_bytes++;
 		info->icount.rx++;
 	    }
@@ -1635,7 +1606,7 @@ cyz_handle_rx(struct cyclades_port *info
 	    }
 #endif
 	    info->idle_stats.recv_idle = jiffies;
-	    schedule_delayed_work(&tty->flip.work, 1);
+	    schedule_delayed_work(&tty->buf.work, 1);
 	}
 	/* Update rx_get */
 	cy_writel(&buf_ctrl->rx_get, new_rx_get);
@@ -1763,23 +1734,17 @@ cyz_handle_cmd(struct cyclades_card *cin
 
 	switch(cmd) {
 	    case C_CM_PR_ERROR:
-		tty->flip.count++;
-		*tty->flip.flag_buf_ptr++ = TTY_PARITY;
-		*tty->flip.char_buf_ptr++ = 0;
+		tty_insert_flip_char(tty, 0, TTY_PARITY);
 		info->icount.rx++;
 		special_count++;
 		break;
 	    case C_CM_FR_ERROR:
-		tty->flip.count++;
-		*tty->flip.flag_buf_ptr++ = TTY_FRAME;
-		*tty->flip.char_buf_ptr++ = 0;
+		tty_insert_flip_char(tty, 0, TTY_FRAME);
 		info->icount.rx++;
 		special_count++;
 		break;
 	    case C_CM_RXBRK:
-		tty->flip.count++;
-		*tty->flip.flag_buf_ptr++ = TTY_BREAK;
-		*tty->flip.char_buf_ptr++ = 0;
+		tty_insert_flip_char(tty, 0, TTY_BREAK);
 		info->icount.rx++;
 		special_count++;
 		break;
@@ -1844,7 +1809,7 @@ cyz_handle_cmd(struct cyclades_card *cin
 	if(delta_count)
 	    cy_sched_event(info, Cy_EVENT_DELTA_WAKEUP);
 	if(special_count)
-	    schedule_delayed_work(&tty->flip.work, 1);
+	    schedule_delayed_work(&tty->buf.work, 1);
     }
 }
 
diff -puN drivers/char/epca.c~tty-layer-buffering-revamp drivers/char/epca.c
--- 25/drivers/char/epca.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/epca.c	Wed Aug 31 12:50:56 2005
@@ -1782,9 +1782,7 @@ static void doevent(int crd)
 		if (tty)  { /* Begin if valid tty */
 			if (event & BREAK_IND)  { /* Begin if BREAK_IND */
 				/* A break has been indicated */
-				tty->flip.count++;
-				*tty->flip.flag_buf_ptr++ = TTY_BREAK;
-				*tty->flip.char_buf_ptr++ = 0;
+				tty_insert_flip_char(tty, 0, TTY_BREAK);
 				tty_schedule_flip(tty); 
 			} else if (event & LOWTX_IND)  { /* Begin LOWTX_IND */
 				if (ch->statusflags & LOWWAIT) 
@@ -2120,7 +2118,6 @@ static void receive_data(struct channel 
 	int dataToRead, wrapgap, bytesAvailable;
 	unsigned int tail, head;
 	unsigned int wrapmask;
-	int rc;
 
 	/* ---------------------------------------------------------------
 		This routine is called by doint when a receive data event 
@@ -2158,16 +2155,15 @@ static void receive_data(struct channel 
 		return;
 	}
 
-	if (tty->flip.count == TTY_FLIPBUF_SIZE) 
+	if (tty_buffer_request_room(tty, bytesAvailable + 1) == 0) 
 		return;
 
 	if (readb(&bc->orun)) {
 		writeb(0, &bc->orun);
 		printk(KERN_WARNING "epca; overrun! DigiBoard device %s\n",tty->name);
+		tty_insert_flip_char(tty, 0, TTY_OVERRUN);
 	}
 	rxwinon(ch);
-	rptr = tty->flip.char_buf_ptr;
-	rc = tty->flip.count;
 	while (bytesAvailable > 0)  { /* Begin while there is data on the card */
 		wrapgap = (head >= tail) ? head - tail : ch->rxbufsize - tail;
 		/* ---------------------------------------------------------------
@@ -2179,8 +2175,7 @@ static void receive_data(struct channel 
 		/* --------------------------------------------------------------
 		   Make sure we don't overflow the buffer
 		----------------------------------------------------------------- */
-		if ((rc + dataToRead) > TTY_FLIPBUF_SIZE)
-			dataToRead = TTY_FLIPBUF_SIZE - rc;
+		dataToRead = tty_prepare_flip_string(tty, &rptr, dataToRead);
 		if (dataToRead == 0)
 			break;
 		/* ---------------------------------------------------------------
@@ -2188,13 +2183,9 @@ static void receive_data(struct channel 
 			for translation if necessary.
 		------------------------------------------------------------------ */
 		memcpy_fromio(rptr, ch->rxptr + tail, dataToRead);
-		rc   += dataToRead;
-		rptr += dataToRead;
 		tail = (tail + dataToRead) & wrapmask;
 		bytesAvailable -= dataToRead;
 	} /* End while there is data on the card */
-	tty->flip.count = rc;
-	tty->flip.char_buf_ptr = rptr;
 	globalwinon(ch);
 	writew(tail, &bc->rout);
 	/* Must be called with global data */
diff -puN drivers/char/esp.c~tty-layer-buffering-revamp drivers/char/esp.c
--- 25/drivers/char/esp.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/esp.c	Wed Aug 31 12:50:56 2005
@@ -345,26 +345,22 @@ static inline void receive_chars_pio(str
 
 	for (i = 0; i < num_bytes; i++) {
 		if (!(err_buf->data[i] & status_mask)) {
-			*(tty->flip.char_buf_ptr++) = pio_buf->data[i];
+			int flag = 0;
 
 			if (err_buf->data[i] & 0x04) {
-				*(tty->flip.flag_buf_ptr++) = TTY_BREAK;
-
+				flag = TTY_BREAK;
 				if (info->flags & ASYNC_SAK)
 					do_SAK(tty);
 			}
 			else if (err_buf->data[i] & 0x02)
-				*(tty->flip.flag_buf_ptr++) = TTY_FRAME;
+				flag = TTY_FRAME;
 			else if (err_buf->data[i] & 0x01)
-				*(tty->flip.flag_buf_ptr++) = TTY_PARITY;
-			else
-				*(tty->flip.flag_buf_ptr++) = 0;
-		
-			tty->flip.count++;
+				flag = TTY_PARITY;
+			tty_insert_flip_char(tty, pio_buf->data[i], flag);		
 		}
 	}
 
-	schedule_delayed_work(&tty->flip.work, 1);
+	schedule_delayed_work(&tty->buf.work, 1);
 
 	info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
 	release_pio_buffer(pio_buf);
@@ -397,7 +393,6 @@ static inline void receive_chars_dma_don
 	int num_bytes;
 	unsigned long flags;
 	
-	
 	flags=claim_dma_lock();
 	disable_dma(dma);
 	clear_dma_ff(dma);
@@ -408,38 +403,31 @@ static inline void receive_chars_dma_don
 	
 	info->icount.rx += num_bytes;
 
-	memcpy(tty->flip.char_buf_ptr, dma_buffer, num_bytes);
-	tty->flip.char_buf_ptr += num_bytes;
-	tty->flip.count += num_bytes;
-	memset(tty->flip.flag_buf_ptr, 0, num_bytes);
-	tty->flip.flag_buf_ptr += num_bytes;
-
 	if (num_bytes > 0) {
-		tty->flip.flag_buf_ptr--;
-
+		tty_insert_flip_string(tty, dma_buffer, num_bytes - 1);
+	
 		status &= (0x1c & info->read_status_mask);
-
-		if (status & info->ignore_status_mask) {
-			tty->flip.count--;
-			tty->flip.char_buf_ptr--;
-			tty->flip.flag_buf_ptr--;
-		} else if (status & 0x10) {
-			*tty->flip.flag_buf_ptr = TTY_BREAK;
-			(info->icount.brk)++;
-			if (info->flags & ASYNC_SAK)
-				do_SAK(tty);
-		} else if (status & 0x08) {
-			*tty->flip.flag_buf_ptr = TTY_FRAME;
-			(info->icount.frame)++;
-		}
-		else if (status & 0x04) {
-			*tty->flip.flag_buf_ptr = TTY_PARITY;
-			(info->icount.parity)++;
-		}
-
-		tty->flip.flag_buf_ptr++;
 		
-		schedule_delayed_work(&tty->flip.work, 1);
+		/* Is the status significant or do we throw the last byte ? */
+		if (!(status & info->ignore_status_mask)) {
+			int statflag = 0;
+			
+			if (status & 0x10) {
+				statflag = TTY_BREAK;
+				(info->icount.brk)++;
+				if (info->flags & ASYNC_SAK)
+					do_SAK(tty);
+			} else if (status & 0x08) {
+				statflag = TTY_FRAME;
+				(info->icount.frame)++;
+			}
+			else if (status & 0x04) {
+				statflag = TTY_PARITY;
+				(info->icount.parity)++;
+			}
+			tty_insert_flip_char(tty, dma_buffer[num_bytes - 1], statflag);
+		}
+		schedule_delayed_work(&tty->buf.work, 1);
 	}
 
 	if (dma_bytes != num_bytes) {
@@ -693,8 +681,7 @@ static irqreturn_t rs_interrupt_single(i
 		num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
 		num_bytes |= serial_in(info, UART_ESI_STAT2);
 
-		if (num_bytes > (TTY_FLIPBUF_SIZE - info->tty->flip.count))
-		  num_bytes = TTY_FLIPBUF_SIZE - info->tty->flip.count;
+		num_bytes = tty_buffer_request_room(info->tty, num_bytes);
 
 		if (num_bytes) {
 			if (dma_bytes ||
diff -puN drivers/char/hvc_console.c~tty-layer-buffering-revamp drivers/char/hvc_console.c
--- 25/drivers/char/hvc_console.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/hvc_console.c	Wed Aug 31 12:50:56 2005
@@ -597,10 +597,8 @@ static int hvc_poll(struct hvc_struct *h
 
 	/* Read data if any */
 	for (;;) {
-		int count = N_INBUF;
-		if (count > (TTY_FLIPBUF_SIZE - tty->flip.count))
-			count = TTY_FLIPBUF_SIZE - tty->flip.count;
-
+		count = tty_buffer_request_room(tty, N_INBUF);
+		
 		/* If flip is full, just reschedule a later read */
 		if (count == 0) {
 			poll_mask |= HVC_POLL_READ;
diff -puN drivers/char/isicom.c~tty-layer-buffering-revamp drivers/char/isicom.c
--- 25/drivers/char/isicom.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/isicom.c	Wed Aug 31 12:50:56 2005
@@ -115,6 +115,7 @@
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/tty.h>
+#include <linux/tty_flip.h>
 #include <linux/termios.h>
 #include <linux/fs.h>
 #include <linux/sched.h>
@@ -773,6 +774,7 @@ static irqreturn_t isicom_interrupt(int 
 	unsigned short base, header, word_count, count;
 	unsigned char channel;
 	short byte_count;
+	unsigned char *rp;
 	
 	card = (struct isi_board *) dev_id;
 
@@ -903,14 +905,10 @@ static irqreturn_t isicom_interrupt(int 
 				break;
 				
 			case 1:	/* Received Break !!!	 */
-				if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-					break;
-				*tty->flip.flag_buf_ptr++ = TTY_BREAK;
-				*tty->flip.char_buf_ptr++ = 0;
-				tty->flip.count++;
+				tty_insert_flip_char(tty, 0, TTY_BREAK);
 				if (port->flags & ASYNC_SAK)
 					do_SAK(tty);
-				schedule_delayed_work(&tty->flip.work, 1);
+				tty_flip_buffer_push(tty);
 				break;
 				
 			case 2:	/* Statistics		 */
@@ -923,23 +921,19 @@ static irqreturn_t isicom_interrupt(int 
 		}	 
 	}
 	else {				/* Data   Packet */
-		count = min_t(unsigned short, byte_count, (TTY_FLIPBUF_SIZE - tty->flip.count));
+	
+		count = tty_prepare_flip_string(tty, &rp, byte_count & ~1);
 #ifdef ISICOM_DEBUG
 		printk(KERN_DEBUG "ISICOM: Intr: Can rx %d of %d bytes.\n", 
 					count, byte_count);
 #endif			
 		word_count = count >> 1;
-		insw(base, tty->flip.char_buf_ptr, word_count);
-		tty->flip.char_buf_ptr += (word_count << 1);		
+		insw(base, rp, word_count);
 		byte_count -= (word_count << 1);
 		if (count & 0x0001) {
-			*tty->flip.char_buf_ptr++ = (char)(inw(base) & 0xff);
+			tty_insert_flip_char(tty,  inw(base) & 0xff, TTY_NORMAL);
 			byte_count -= 2;
 		}	
-		memset(tty->flip.flag_buf_ptr, 0, count);
-		tty->flip.flag_buf_ptr += count;
-		tty->flip.count += count;
-		
 		if (byte_count > 0) {
 			printk(KERN_DEBUG "ISICOM: Intr(0x%x:%d): Flip buffer overflow! dropping bytes...\n",
 					base, channel+1);
@@ -948,7 +942,7 @@ static irqreturn_t isicom_interrupt(int 
 				byte_count -= 2;
 			}
 		}
-		schedule_delayed_work(&tty->flip.work, 1);
+		tty_flip_buffer_push(tty);
 	}
 	if (card->isa == YES)
 		ClearInterrupt(base);
diff -puN drivers/char/moxa.c~tty-layer-buffering-revamp drivers/char/moxa.c
--- 25/drivers/char/moxa.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/moxa.c	Wed Aug 31 12:50:56 2005
@@ -1150,8 +1150,9 @@ static void receive_data(struct moxa_str
 		MoxaPortFlushData(ch->port, 0);
 		return;
 	}
-	space = TTY_FLIPBUF_SIZE - tp->flip.count;
-	if (space <= 0)
+	/* Working buffer size */
+	space = tty_buffer_request_room(512);
+	if (space == 0)
 		return;
 	charptr = tp->flip.char_buf_ptr;
 	flagptr = tp->flip.flag_buf_ptr;
diff -puN drivers/char/mxser.c~tty-layer-buffering-revamp drivers/char/mxser.c
--- 25/drivers/char/mxser.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/mxser.c	Wed Aug 31 12:50:56 2005
@@ -63,7 +63,6 @@
 #include <asm/system.h>
 #include <asm/io.h>
 #include <asm/irq.h>
-#include <asm/segment.h>
 #include <asm/bitops.h>
 #include <asm/uaccess.h>
 
@@ -2001,7 +2000,7 @@ static void mxser_receive_chars(struct m
 
 	spin_lock_irqsave(&info->slock, flags);
 
-	recv_room = tty->ldisc.receive_room(tty);
+	recv_room = tty->receive_room;
 	if ((recv_room == 0) && (!info->ldisc_stop_rx)) {
 		//mxser_throttle(tty);
 		mxser_stoprx(tty);
diff -puN drivers/char/n_hdlc.c~tty-layer-buffering-revamp drivers/char/n_hdlc.c
--- 25/drivers/char/n_hdlc.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/n_hdlc.c	Wed Aug 31 12:50:56 2005
@@ -212,7 +212,6 @@ static struct tty_ldisc n_hdlc_ldisc = {
 	.ioctl		= n_hdlc_tty_ioctl,
 	.poll		= n_hdlc_tty_poll,
 	.receive_buf	= n_hdlc_tty_receive,
-	.receive_room	= n_hdlc_tty_room,
 	.write_wakeup	= n_hdlc_tty_wakeup,
 };
 
@@ -338,6 +337,7 @@ static int n_hdlc_tty_open (struct tty_s
 		
 	tty->disc_data = n_hdlc;
 	n_hdlc->tty    = tty;
+	tty->receive_room = 65536;
 	
 #if defined(TTY_NO_WRITE_SPLIT)
 	/* change tty_io write() to not split large writes into 8K chunks */
@@ -479,22 +479,6 @@ static void n_hdlc_tty_wakeup(struct tty
 }	/* end of n_hdlc_tty_wakeup() */
 
 /**
- * n_hdlc_tty_room - Return the amount of space left in the receiver's buffer
- * @tty	- pointer to associated tty instance data
- *
- * Callback function from tty driver. Return the amount of space left in the
- * receiver's buffer to decide if remote transmitter is to be throttled.
- */
-static int n_hdlc_tty_room(struct tty_struct *tty)
-{
-	if (debuglevel >= DEBUG_LEVEL_INFO)	
-		printk("%s(%d)n_hdlc_tty_room() called\n",__FILE__,__LINE__);
-	/* always return a larger number to prevent */
-	/* throttling of remote transmitter. */
-	return 65536;
-}	/* end of n_hdlc_tty_root() */
-
-/**
  * n_hdlc_tty_receive - Called by tty driver when receive data is available
  * @tty	- pointer to tty instance data
  * @data - pointer to received data
diff -puN drivers/char/n_r3964.c~tty-layer-buffering-revamp drivers/char/n_r3964.c
--- 25/drivers/char/n_r3964.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/n_r3964.c	Wed Aug 31 12:50:56 2005
@@ -147,7 +147,6 @@ static unsigned int r3964_poll(struct tt
 		      struct poll_table_struct  *wait);
 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
                               char *fp, int count);
-static int  r3964_receive_room(struct tty_struct *tty);
 
 static struct tty_ldisc tty_ldisc_N_R3964 = {
 	.owner	 = THIS_MODULE,
@@ -161,7 +160,6 @@ static struct tty_ldisc tty_ldisc_N_R396
 	.set_termios = r3964_set_termios,
 	.poll	= r3964_poll,            
 	.receive_buf = r3964_receive_buf,
-	.receive_room = r3964_receive_room,
 };
 
 
@@ -1119,6 +1117,7 @@ static int r3964_open(struct tty_struct 
    pInfo->nRetry = 0;
    
    tty->disc_data = pInfo;
+   tty->receive_room = 65536;
 
    init_timer(&pInfo->tmr);
    pInfo->tmr.data = (unsigned long)pInfo;
@@ -1405,12 +1404,5 @@ static void r3964_receive_buf(struct tty
     }
 }
 
-static int r3964_receive_room(struct tty_struct *tty)
-{
-   TRACE_L("receive_room");
-   return -1;
-}
-
-
 MODULE_LICENSE("GPL");
 MODULE_ALIAS_LDISC(N_R3964);
diff -puN drivers/char/n_tty.c~tty-layer-buffering-revamp drivers/char/n_tty.c
--- 25/drivers/char/n_tty.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/n_tty.c	Wed Aug 31 12:50:56 2005
@@ -78,7 +78,32 @@ static inline void free_buf(unsigned cha
 		free_page((unsigned long) buf);
 }
 
-static inline void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
+/**
+ *	n_tty_set__room	-	receive space
+ *	@tty: terminal
+ *
+ *	Called by the driver to find out how much data it is
+ *	permitted to feed to the line discipline without any being lost
+ *	and thus to manage flow control. Not serialized. Answers for the
+ *	"instant".
+ */
+ 
+static void n_tty_set_room(struct tty_struct *tty)
+{
+	int	left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
+
+	/*
+	 * If we are doing input canonicalization, and there are no
+	 * pending newlines, let characters through without limit, so
+	 * that erase characters will be handled.  Other excess
+	 * characters will be beeped.
+	 */
+	if (left <= 0)
+		left = tty->icanon && !tty->canon_data;
+	tty->receive_room = left;
+}
+
+static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
 {
 	if (tty->read_cnt < N_TTY_BUF_SIZE) {
 		tty->read_buf[tty->read_head] = c;
@@ -87,7 +112,7 @@ static inline void put_tty_queue_nolock(
 	}
 }
 
-static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
+static void put_tty_queue(unsigned char c, struct tty_struct *tty)
 {
 	unsigned long flags;
 	/*
@@ -136,6 +161,7 @@ static void reset_buffer_flags(struct tt
 	spin_unlock_irqrestore(&tty->read_lock, flags);
 	tty->canon_head = tty->canon_data = tty->erasing = 0;
 	memset(&tty->read_flags, 0, sizeof tty->read_flags);
+	n_tty_set_room(tty);
 	check_unthrottle(tty);
 }
 
@@ -838,30 +864,6 @@ send_signal:
 	put_tty_queue(c, tty);
 }	
 
-/**
- *	n_tty_receive_room	-	receive space
- *	@tty: terminal
- *
- *	Called by the driver to find out how much data it is
- *	permitted to feed to the line discipline without any being lost
- *	and thus to manage flow control. Not serialized. Answers for the
- *	"instant".
- */
- 
-static int n_tty_receive_room(struct tty_struct *tty)
-{
-	int	left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
-
-	/*
-	 * If we are doing input canonicalization, and there are no
-	 * pending newlines, let characters through without limit, so
-	 * that erase characters will be handled.  Other excess
-	 * characters will be beeped.
-	 */
-	if (left <= 0)
-		left = tty->icanon && !tty->canon_data;
-	return left;
-}
 
 /**
  *	n_tty_write_wakeup	-	asynchronous I/O notifier
@@ -953,6 +955,8 @@ static void n_tty_receive_buf(struct tty
 			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))
@@ -964,7 +968,7 @@ static void n_tty_receive_buf(struct tty
 	 * mode.  We don't want to throttle the driver if we're in
 	 * canonical mode and don't have a newline yet!
 	 */
-	if (n_tty_receive_room(tty) < TTY_THRESHOLD_THROTTLE) {
+	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)
@@ -999,6 +1003,7 @@ static void n_tty_set_termios(struct tty
 	if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
 		tty->raw = 1;
 		tty->real_raw = 1;
+		n_tty_set_room(tty);
 		return;
 	}
 	if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
@@ -1051,6 +1056,7 @@ static void n_tty_set_termios(struct tty
 		else
 			tty->real_raw = 0;
 	}
+	n_tty_set_room(tty);
 }
 
 /**
@@ -1130,7 +1136,7 @@ static inline int input_available_p(stru
  *
  */
  
-static inline int copy_from_read_buf(struct tty_struct *tty,
+static int copy_from_read_buf(struct tty_struct *tty,
 				      unsigned char __user **b,
 				      size_t *nr)
 
@@ -1308,6 +1314,7 @@ do_it_again:
 				retval = -ERESTARTSYS;
 				break;
 			}
+			n_tty_set_room(tty);
 			clear_bit(TTY_DONT_FLIP, &tty->flags);
 			timeout = schedule_timeout(timeout);
 			set_bit(TTY_DONT_FLIP, &tty->flags);
@@ -1400,6 +1407,8 @@ do_it_again:
 	       		clear_bit(TTY_PUSH, &tty->flags);
 	} else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
 		 goto do_it_again;
+		 
+	n_tty_set_room(tty);
 
 	return retval;
 }
@@ -1553,7 +1562,6 @@ struct tty_ldisc tty_ldisc_N_TTY = {
 	normal_poll,		/* poll */
 	NULL,			/* hangup */
 	n_tty_receive_buf,	/* receive_buf */
-	n_tty_receive_room,	/* receive_room */
 	n_tty_write_wakeup	/* write_wakeup */
 };
 
diff -puN drivers/char/pcmcia/synclink_cs.c~tty-layer-buffering-revamp drivers/char/pcmcia/synclink_cs.c
--- 25/drivers/char/pcmcia/synclink_cs.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/pcmcia/synclink_cs.c	Wed Aug 31 12:50:56 2005
@@ -1059,8 +1059,9 @@ static void rx_ready_hdlc(MGSLPC_INFO *i
 
 static void rx_ready_async(MGSLPC_INFO *info, int tcd)
 {
-	unsigned char data, status;
+	unsigned char data, status, flag;
 	int fifo_count;
+	int work = 0;
  	struct tty_struct *tty = info->tty;
  	struct mgsl_icount *icount = &info->icount;
 
@@ -1075,20 +1076,16 @@ static void rx_ready_async(MGSLPC_INFO *
 			fifo_count = 32;
 	} else
 		fifo_count = 32;
-	
+
+	tty_buffer_request_room(tty, fifo_count);	
 	/* Flush received async data to receive data buffer. */ 
 	while (fifo_count) {
 		data   = read_reg(info, CHA + RXFIFO);
 		status = read_reg(info, CHA + RXFIFO);
 		fifo_count -= 2;
 
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-			break;
-			
-		*tty->flip.char_buf_ptr = data;
 		icount->rx++;
-		
-		*tty->flip.flag_buf_ptr = 0;
+		flag = TTY_NORMAL;
 
 		// if no frameing/crc error then save data
 		// BIT7:parity error
@@ -1107,26 +1104,23 @@ static void rx_ready_async(MGSLPC_INFO *
 			status &= info->read_status_mask;
 
 			if (status & BIT7)
-				*tty->flip.flag_buf_ptr = TTY_PARITY;
+				flag = TTY_PARITY;
 			else if (status & BIT6)
-				*tty->flip.flag_buf_ptr = TTY_FRAME;
+				flag = TTY_FRAME;
 		}
-		
-		tty->flip.flag_buf_ptr++;
-		tty->flip.char_buf_ptr++;
-		tty->flip.count++;
+		work += tty_insert_flip_char(tty, data, flag);	
 	}
 	issue_command(info, CHA, CMD_RXFIFO);
 
 	if (debug_level >= DEBUG_LEVEL_ISR) {
-		printk("%s(%d):rx_ready_async count=%d\n",
-			__FILE__,__LINE__,tty->flip.count);
+		printk("%s(%d):rx_ready_async",
+			__FILE__,__LINE__);
 		printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
 			__FILE__,__LINE__,icount->rx,icount->brk,
 			icount->parity,icount->frame,icount->overrun);
 	}
 			
-	if (tty->flip.count)
+	if (work)
 		tty_flip_buffer_push(tty);
 }
 
diff -puN drivers/char/pty.c~tty-layer-buffering-revamp drivers/char/pty.c
--- 25/drivers/char/pty.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/pty.c	Wed Aug 31 12:50:56 2005
@@ -111,7 +111,7 @@ static int pty_write(struct tty_struct *
 	if (!to || tty->stopped)
 		return 0;
 
-	c = to->ldisc.receive_room(to);
+	c = to->receive_room;
 	if (c > count)
 		c = count;
 	to->ldisc.receive_buf(to, buf, NULL, c);
@@ -126,7 +126,7 @@ static int pty_write_room(struct tty_str
 	if (!to || tty->stopped)
 		return 0;
 
-	return to->ldisc.receive_room(to);
+	return to->receive_room;
 }
 
 /*
diff -puN drivers/char/riscom8.c~tty-layer-buffering-revamp drivers/char/riscom8.c
--- 25/drivers/char/riscom8.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/riscom8.c	Wed Aug 31 12:50:56 2005
@@ -354,28 +354,17 @@ static inline void rc_receive_exc(struct
 	struct riscom_port *port;
 	struct tty_struct *tty;
 	unsigned char status;
-	unsigned char ch;
+	unsigned char ch, flag;
 	
 	if (!(port = rc_get_port(bp, "Receive")))
 		return;
 
 	tty = port->tty;
-	if (tty->flip.count >= TTY_FLIPBUF_SIZE)  {
-		printk(KERN_WARNING "rc%d: port %d: Working around flip "
-				    "buffer overflow.\n",
-		       board_No(bp), port_No(port));
-		return;
-	}
 	
 #ifdef RC_REPORT_OVERRUN	
 	status = rc_in(bp, CD180_RCSR);
-	if (status & RCSR_OE)  {
+	if (status & RCSR_OE)
 		port->overrun++;
-#if 0		
-		printk(KERN_ERR "rc%d: port %d: Overrun. Total %ld overruns\n", 
-		       board_No(bp), port_No(port), port->overrun);
-#endif		
-	}
 	status &= port->mark_mask;
 #else	
 	status = rc_in(bp, CD180_RCSR) & port->mark_mask;
@@ -393,24 +382,23 @@ static inline void rc_receive_exc(struct
 	} else if (status & RCSR_BREAK)  {
 		printk(KERN_INFO "rc%d: port %d: Handling break...\n",
 		       board_No(bp), port_No(port));
-		*tty->flip.flag_buf_ptr++ = TTY_BREAK;
+		flag = TTY_BREAK;
 		if (port->flags & ASYNC_SAK)
 			do_SAK(tty);
 		
 	} else if (status & RCSR_PE) 
-		*tty->flip.flag_buf_ptr++ = TTY_PARITY;
+		flag = TTY_PARITY;
 	
 	else if (status & RCSR_FE) 
-		*tty->flip.flag_buf_ptr++ = TTY_FRAME;
+		flag = TTY_FRAME;
 	
         else if (status & RCSR_OE)
-		*tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
+		flag = TTY_OVERRUN;
 	
 	else
-		*tty->flip.flag_buf_ptr++ = 0;
+		flag = TTY_NORMAL;
 	
-	*tty->flip.char_buf_ptr++ = ch;
-	tty->flip.count++;
+	tty_insert_flip_char(tty, ch, flag);
 	schedule_delayed_work(&tty->flip.work, 1);
 }
 
@@ -432,15 +420,13 @@ static inline void rc_receive(struct ris
 #endif	
 	
 	while (count--)  {
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE)  {
+		if (tty_buffer_request_room(tty, 1) == 0)  {
 			printk(KERN_WARNING "rc%d: port %d: Working around "
 					    "flip buffer overflow.\n",
 			       board_No(bp), port_No(port));
 			break;
 		}
-		*tty->flip.char_buf_ptr++ = rc_in(bp, CD180_RDR);
-		*tty->flip.flag_buf_ptr++ = 0;
-		tty->flip.count++;
+		tty_insert_flip_char(tty, rc_in(bp, CD180_RDR), TTY_NORMAL);
 	}
 	schedule_delayed_work(&tty->flip.work, 1);
 }
diff -puN drivers/char/rocket.c~tty-layer-buffering-revamp drivers/char/rocket.c
--- 25/drivers/char/rocket.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/rocket.c	Wed Aug 31 12:50:56 2005
@@ -329,19 +329,16 @@ static void rp_do_receive(struct r_port 
 {
 	unsigned int CharNStat;
 	int ToRecv, wRecv, space = 0, count;
-	unsigned char *cbuf;
-	char *fbuf;
+	unsigned char *cbuf, *chead;
+	char *fbuf, *fhead;
 	struct tty_ldisc *ld;
 
 	ld = tty_ldisc_ref(tty);
 
 	ToRecv = sGetRxCnt(cp);
-	if (ld)
-		space = ld->receive_room(tty);
+	space = tty->receive_room;
 	if (space > 2 * TTY_FLIPBUF_SIZE)
 		space = 2 * TTY_FLIPBUF_SIZE;
-	cbuf = tty->flip.char_buf;
-	fbuf = tty->flip.flag_buf;
 	count = 0;
 #ifdef ROCKET_DEBUG_INTR
 	printk(KERN_INFO "rp_do_receive(%d, %d)...", ToRecv, space);
@@ -354,8 +351,12 @@ static void rp_do_receive(struct r_port 
 	if (ToRecv > space)
 		ToRecv = space;
 
+	ToRecv = tty_prepare_flip_string_flags(tty, &chead, &fhead, ToRecv);
 	if (ToRecv <= 0)
 		goto done;
+		
+	cbuf = chead;
+	fbuf = fhead;
 
 	/*
 	 * if status indicates there are errored characters in the
@@ -403,7 +404,7 @@ static void rp_do_receive(struct r_port 
 			else if (CharNStat & STMRCVROVRH)
 				*fbuf++ = TTY_OVERRUN;
 			else
-				*fbuf++ = 0;
+				*fbuf++ = TTY_NORMAL;
 			*cbuf++ = CharNStat & 0xff;
 			count++;
 			ToRecv--;
@@ -430,13 +431,13 @@ static void rp_do_receive(struct r_port 
 			sInStrW(sGetTxRxDataIO(cp), (unsigned short *) cbuf, wRecv);
 		if (ToRecv & 1)
 			cbuf[ToRecv - 1] = sInB(sGetTxRxDataIO(cp));
-		memset(fbuf, 0, ToRecv);
+		memset(fbuf, TTY_NORMAL, ToRecv);
 		cbuf += ToRecv;
 		fbuf += ToRecv;
 		count += ToRecv;
 	}
 	/*  Push the data up to the tty layer */
-	ld->receive_buf(tty, tty->flip.char_buf, tty->flip.flag_buf, count);
+	ld->receive_buf(tty, cbuf, fbuf, count);
 done:
 	tty_ldisc_deref(ld);
 }
diff -puN drivers/char/selection.c~tty-layer-buffering-revamp drivers/char/selection.c
--- 25/drivers/char/selection.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/selection.c	Wed Aug 31 12:50:56 2005
@@ -276,7 +276,8 @@ int set_selection(const struct tiocl_sel
 int paste_selection(struct tty_struct *tty)
 {
 	struct vc_data *vc = (struct vc_data *)tty->driver_data;
-	int	pasted = 0, count;
+	int	pasted = 0;
+	unsigned int count;
 	struct  tty_ldisc *ld;
 	DECLARE_WAITQUEUE(wait, current);
 
@@ -294,7 +295,7 @@ int paste_selection(struct tty_struct *t
 			continue;
 		}
 		count = sel_buffer_lth - pasted;
-		count = min(count, tty->ldisc.receive_room(tty));
+		count = min(count, tty->receive_room);
 		tty->ldisc.receive_buf(tty, sel_buffer + pasted, NULL, count);
 		pasted += count;
 	}
diff -puN drivers/char/ser_a2232.c~tty-layer-buffering-revamp drivers/char/ser_a2232.c
--- 25/drivers/char/ser_a2232.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/ser_a2232.c	Wed Aug 31 12:50:56 2005
@@ -194,11 +194,6 @@ static inline void a2232_receive_char(st
 */
 	struct tty_struct *tty = port->gs.tty;
 
-	if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-		return;
-
-	tty->flip.count++;
-
 #if 0
 	switch(err) {
 	case TTY_BREAK:
@@ -212,8 +207,7 @@ static inline void a2232_receive_char(st
 	}
 #endif
 
-	*tty->flip.flag_buf_ptr++ = err;
-	*tty->flip.char_buf_ptr++ = ch;
+	tty_insert_flip_char(tty, ch, err);
 	tty_flip_buffer_push(tty);
 }
 
diff -puN drivers/char/serial167.c~tty-layer-buffering-revamp drivers/char/serial167.c
--- 25/drivers/char/serial167.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/serial167.c	Wed Aug 31 12:50:56 2005
@@ -422,45 +422,35 @@ cd2401_rxerr_interrupt(int irq, void *de
 	    base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
 	    return IRQ_HANDLED;
 	}
-	if (tty->flip.count < TTY_FLIPBUF_SIZE){
-	    tty->flip.count++;
+	if (tty_buffer_request_room(tty, 1) != 0){
 	    if (err & info->read_status_mask){
 		if(err & CyBREAK){
-		    *tty->flip.flag_buf_ptr++ = TTY_BREAK;
-		    *tty->flip.char_buf_ptr++ = data;
+		    tty_insert_flip_char(tty, data, TTY_BREAK);
 		    if (info->flags & ASYNC_SAK){
 			do_SAK(tty);
 		    }
 		}else if(err & CyFRAME){
-		    *tty->flip.flag_buf_ptr++ = TTY_FRAME;
-		    *tty->flip.char_buf_ptr++ = data;
+		    tty_insert_flip_char(tty, data, TTY_FRAME);
 		}else if(err & CyPARITY){
-		    *tty->flip.flag_buf_ptr++ = TTY_PARITY;
-		    *tty->flip.char_buf_ptr++ = data;
+		    tty_insert_flip_char(tty, data, TTY_PARITY);
 		}else if(err & CyOVERRUN){
-		    *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
-		    *tty->flip.char_buf_ptr++ = 0;
+		    tty_insert_flip_char(tty, 0, TTY_OVERRUN);
 		    /*
 		       If the flip buffer itself is
 		       overflowing, we still loose
 		       the next incoming character.
 		     */
-		    if(tty->flip.count < TTY_FLIPBUF_SIZE){
-			tty->flip.count++;
-			*tty->flip.flag_buf_ptr++ = TTY_NORMAL;
-			*tty->flip.char_buf_ptr++ = data;
-		    }
+		    tty_insert_flip_char(tty, data, TTY_NORMAL);
+		}
 		/* These two conditions may imply */
 		/* a normal read should be done. */
 		/* else if(data & CyTIMEOUT) */
 		/* else if(data & CySPECHAR) */
 		}else{
-		    *tty->flip.flag_buf_ptr++ = 0;
-		    *tty->flip.char_buf_ptr++ = 0;
+		    tty_insert_flip_char(tty, 0, TTY_NORMAL);
 		}
 	    }else{
-		*tty->flip.flag_buf_ptr++ = 0;
-		*tty->flip.char_buf_ptr++ = 0;
+		    tty_insert_flip_char(tty, data, TTY_NORMAL);
 	    }
 	}else{
 	    /* there was a software buffer overrun
@@ -692,12 +682,7 @@ cd2401_rx_interrupt(int irq, void *dev_i
 #endif
 	while(char_count--){
 	    data = base_addr[CyRDR];
-	    if (tty->flip.count >= TTY_FLIPBUF_SIZE){
-		continue;
-	    }
-	    tty->flip.count++;
-	    *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
-	    *tty->flip.char_buf_ptr++ = data;
+	    tty_insert_flip_char(tty, data, TTY_NORMAL);
 #ifdef CYCLOM_16Y_HACK
 	    udelay(10L);
 #endif
diff -puN drivers/char/speakup/speakup.c~tty-layer-buffering-revamp drivers/char/speakup/speakup.c
--- 25/drivers/char/speakup/speakup.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/speakup/speakup.c	Wed Aug 31 12:50:56 2005
@@ -488,7 +488,7 @@ speakup_paste_selection(struct tty_struc
       continue;
     }
     count = sel_buffer_lth - pasted;
-    count = MIN(count, tty->ldisc.receive_room(tty));
+    count = MIN(count, tty->receive_room);
     tty->ldisc.receive_buf(tty, sel_buffer + pasted, 0, count);
     pasted += count;
   }
diff -puN drivers/char/specialix.c~tty-layer-buffering-revamp drivers/char/specialix.c
--- 25/drivers/char/specialix.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/specialix.c	Wed Aug 31 12:50:56 2005
@@ -85,6 +85,7 @@
 #include <linux/interrupt.h>
 #include <linux/errno.h>
 #include <linux/tty.h>
+#include <linux/tty_flip.h>
 #include <linux/mm.h>
 #include <linux/serial.h>
 #include <linux/fcntl.h>
@@ -668,7 +669,7 @@ static inline void sx_receive_exc(struct
 	struct specialix_port *port;
 	struct tty_struct *tty;
 	unsigned char status;
-	unsigned char ch;
+	unsigned char ch, flag;
 
 	func_enter();
 
@@ -679,8 +680,6 @@ static inline void sx_receive_exc(struct
 		return;
 	}
 	tty = port->tty;
-	dprintk (SX_DEBUG_RX, "port: %p count: %d BUFF_SIZE: %d\n",
-		 port,  tty->flip.count, TTY_FLIPBUF_SIZE);
 	
 	status = sx_in(bp, CD186x_RCSR);
 
@@ -694,7 +693,7 @@ static inline void sx_receive_exc(struct
 
 	/* This flip buffer check needs to be below the reading of the
 	   status register to reset the chip's IRQ.... */
-	if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
+	if (tty_buffer_request_room(tty, 1) == 0) {
 		dprintk(SX_DEBUG_FIFO, "sx%d: port %d: Working around flip buffer overflow.\n",
 		       board_No(bp), port_No(port));
 		func_exit();
@@ -715,26 +714,24 @@ static inline void sx_receive_exc(struct
 	} else if (status & RCSR_BREAK) {
 		dprintk(SX_DEBUG_RX, "sx%d: port %d: Handling break...\n",
 		       board_No(bp), port_No(port));
-		*tty->flip.flag_buf_ptr++ = TTY_BREAK;
+		flag = TTY_BREAK;
 		if (port->flags & ASYNC_SAK)
 			do_SAK(tty);
 		
 	} else if (status & RCSR_PE) 
-		*tty->flip.flag_buf_ptr++ = TTY_PARITY;
+		flag = TTY_PARITY;
 	
 	else if (status & RCSR_FE) 
-		*tty->flip.flag_buf_ptr++ = TTY_FRAME;
+		flag = TTY_FRAME;
 	
 	else if (status & RCSR_OE)
-		*tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
+		flag = TTY_OVERRUN;
 	
 	else
-		*tty->flip.flag_buf_ptr++ = 0;
+		flag = TTY_NORMAL;
 	
-	*tty->flip.char_buf_ptr++ = ch;
-	tty->flip.count++;
-	schedule_delayed_work(&tty->flip.work, 1);
-
+	if(tty_insert_flip_char(tty, ch, flag))
+		tty_flip_buffer_push(tty);
 	func_exit();
 }
 
@@ -758,18 +755,11 @@ static inline void sx_receive(struct spe
 	dprintk (SX_DEBUG_RX, "port: %p: count: %d\n", port, count);
 	port->hits[count > 8 ? 9 : count]++;
 	
-	while (count--) {
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
-			printk(KERN_INFO "sx%d: port %d: Working around flip buffer overflow.\n",
-			       board_No(bp), port_No(port));
-			break;
-		}
-		*tty->flip.char_buf_ptr++ = sx_in(bp, CD186x_RDR);
-		*tty->flip.flag_buf_ptr++ = 0;
-		tty->flip.count++;
-	}
-	schedule_delayed_work(&tty->flip.work, 1);
-
+	tty_buffer_request_room(tty, count);
+	
+	while (count--)
+		tty_insert_flip_char(tty, sx_in(bp, CD186x_RDR), TTY_NORMAL);
+	tty_flip_buffer_push(tty);
 	func_exit();
 }
 
diff -puN drivers/char/sx.c~tty-layer-buffering-revamp drivers/char/sx.c
--- 25/drivers/char/sx.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/sx.c	Wed Aug 31 12:50:56 2005
@@ -1085,6 +1085,7 @@ static inline void sx_receive_chars (str
 	int rx_op;
 	struct tty_struct *tty;
 	int copied=0;
+	unsigned char *rp;
 
 	func_enter2 ();
 	tty = port->gs.tty;
@@ -1095,8 +1096,8 @@ static inline void sx_receive_chars (str
 		sx_dprintk (SX_DEBUG_RECEIVE, "rxop=%d, c = %d.\n", rx_op, c); 
 
 		/* Don't copy more bytes than there is room for in the buffer */
-		if (tty->flip.count + c > TTY_FLIPBUF_SIZE) 
-			c = TTY_FLIPBUF_SIZE - tty->flip.count;
+
+		c = tty_prepare_flip_string(tty, &rp, c);
 
 		sx_dprintk (SX_DEBUG_RECEIVE, "c = %d.\n", c); 
 
@@ -1111,14 +1112,8 @@ static inline void sx_receive_chars (str
 		sx_dprintk (SX_DEBUG_RECEIVE , "Copying over %d chars. First is %d at %lx\n", c, 
 		            read_sx_byte (port->board, CHAN_OFFSET(port,hi_rxbuf) + rx_op),
 		            CHAN_OFFSET(port, hi_rxbuf)); 
-		memcpy_fromio (tty->flip.char_buf_ptr, 
+		memcpy_fromio (rp, 
 		               port->board->base + CHAN_OFFSET(port,hi_rxbuf) + rx_op, c);
-		memset(tty->flip.flag_buf_ptr, TTY_NORMAL, c);
-
-		/* Update the kernel buffer end */
-		tty->flip.count += c;
-		tty->flip.char_buf_ptr += c;
-		tty->flip.flag_buf_ptr += c;
 
 		/* This one last. ( Not essential.)
 		   It allows the card to start putting more data into the buffer! 
diff -puN drivers/char/synclink.c~tty-layer-buffering-revamp drivers/char/synclink.c
--- 25/drivers/char/synclink.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/synclink.c	Wed Aug 31 12:50:56 2005
@@ -1464,6 +1464,7 @@ static void mgsl_isr_receive_data( struc
 {
 	int Fifocount;
 	u16 status;
+	int work = 0;
 	unsigned char DataByte;
  	struct tty_struct *tty = info->tty;
  	struct	mgsl_icount *icount = &info->icount;
@@ -1484,6 +1485,8 @@ static void mgsl_isr_receive_data( struc
 	/* flush the receive FIFO */
 
 	while( (Fifocount = (usc_InReg(info,RICR) >> 8)) ) {
+		int flag;
+		
 		/* read one byte from RxFIFO */
 		outw( (inw(info->io_base + CCAR) & 0x0780) | (RDR+LSBONLY),
 		      info->io_base + CCAR );
@@ -1495,13 +1498,9 @@ static void mgsl_isr_receive_data( struc
 				RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) )
 			usc_UnlatchRxstatusBits(info,RXSTATUS_ALL);
 		
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-			continue;
-			
-		*tty->flip.char_buf_ptr = DataByte;
 		icount->rx++;
 		
-		*tty->flip.flag_buf_ptr = 0;
+		flag = 0;
 		if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR +
 				RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) ) {
 			printk("rxerr=%04X\n",status);					
@@ -1527,41 +1526,31 @@ static void mgsl_isr_receive_data( struc
 			status &= info->read_status_mask;
 		
 			if (status & RXSTATUS_BREAK_RECEIVED) {
-				*tty->flip.flag_buf_ptr = TTY_BREAK;
+				flag = TTY_BREAK;
 				if (info->flags & ASYNC_SAK)
 					do_SAK(tty);
 			} else if (status & RXSTATUS_PARITY_ERROR)
-				*tty->flip.flag_buf_ptr = TTY_PARITY;
+				flag = TTY_PARITY;
 			else if (status & RXSTATUS_FRAMING_ERROR)
-				*tty->flip.flag_buf_ptr = TTY_FRAME;
-			if (status & RXSTATUS_OVERRUN) {
-				/* Overrun is special, since it's
-				 * reported immediately, and doesn't
-				 * affect the current character
-				 */
-				if (tty->flip.count < TTY_FLIPBUF_SIZE) {
-					tty->flip.count++;
-					tty->flip.flag_buf_ptr++;
-					tty->flip.char_buf_ptr++;
-					*tty->flip.flag_buf_ptr = TTY_OVERRUN;
-				}
-			}
+				flag = TTY_FRAME;
 		}	/* end of if (error) */
-		
-		tty->flip.flag_buf_ptr++;
-		tty->flip.char_buf_ptr++;
-		tty->flip.count++;
+		tty_insert_flip_char(tty, DataByte, flag);		
+		if (status & RXSTATUS_OVERRUN) {
+			/* Overrun is special, since it's
+			 * reported immediately, and doesn't
+			 * affect the current character
+			 */
+			work += tty_insert_flip_char(tty, 0, TTY_OVERRUN);
+		}
 	}
 
 	if ( debug_level >= DEBUG_LEVEL_ISR ) {
-		printk("%s(%d):mgsl_isr_receive_data flip count=%d\n",
-			__FILE__,__LINE__,tty->flip.count);
 		printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
 			__FILE__,__LINE__,icount->rx,icount->brk,
 			icount->parity,icount->frame,icount->overrun);
 	}
 			
-	if ( tty->flip.count )
+	if(work)
 		tty_flip_buffer_push(tty);
 }
 
diff -puN drivers/char/synclinkmp.c~tty-layer-buffering-revamp drivers/char/synclinkmp.c
--- 25/drivers/char/synclinkmp.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/synclinkmp.c	Wed Aug 31 12:50:56 2005
@@ -2198,7 +2198,7 @@ void isr_rxint(SLMP_INFO * info)
 			if ( tty ) {
 				if (!(status & info->ignore_status_mask1)) {
 					if (info->read_status_mask1 & BRKD) {
-						*tty->flip.flag_buf_ptr = TTY_BREAK;
+						tty_insert_flip_char(tty, 0, TTY_BREAK);
 						if (info->flags & ASYNC_SAK)
 							do_SAK(tty);
 					}
@@ -2242,16 +2242,10 @@ void isr_rxrdy(SLMP_INFO * info)
 
 	while((status = read_reg(info,CST0)) & BIT0)
 	{
+		int flag = 0;
+		int over = 0;
 		DataByte = read_reg(info,TRB);
 
-		if ( tty ) {
-			if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-				continue;
-
-			*tty->flip.char_buf_ptr = DataByte;
-			*tty->flip.flag_buf_ptr = 0;
-		}
-
 		icount->rx++;
 
 		if ( status & (PE + FRME + OVRN) ) {
@@ -2274,42 +2268,34 @@ void isr_rxrdy(SLMP_INFO * info)
 
 			if ( tty ) {
 				if (status & PE)
-					*tty->flip.flag_buf_ptr = TTY_PARITY;
+					flag = TTY_PARITY;
 				else if (status & FRME)
-					*tty->flip.flag_buf_ptr = TTY_FRAME;
+					flag = TTY_FRAME;
 				if (status & OVRN) {
 					/* Overrun is special, since it's
 					 * reported immediately, and doesn't
 					 * affect the current character
 					 */
-					if (tty->flip.count < TTY_FLIPBUF_SIZE) {
-						tty->flip.count++;
-						tty->flip.flag_buf_ptr++;
-						tty->flip.char_buf_ptr++;
-						*tty->flip.flag_buf_ptr = TTY_OVERRUN;
-					}
+					over = 1;
 				}
 			}
 		}	/* end of if (error) */
 
 		if ( tty ) {
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
+			tty_insert_flip_char(tty, DataByte, flag);
+			if (over) 
+				tty_insert_flip_char(tty, 0, TTY_OVERRUN);
 		}
 	}
 
 	if ( debug_level >= DEBUG_LEVEL_ISR ) {
-		printk("%s(%d):%s isr_rxrdy() flip count=%d\n",
-			__FILE__,__LINE__,info->device_name,
-			tty ? tty->flip.count : 0);
 		printk("%s(%d):%s rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
 			__FILE__,__LINE__,info->device_name,
 			icount->rx,icount->brk,icount->parity,
 			icount->frame,icount->overrun);
 	}
 
-	if ( tty && tty->flip.count )
+	if ( tty )
 		tty_flip_buffer_push(tty);
 }
 
diff -puN drivers/char/tty_io.c~tty-layer-buffering-revamp drivers/char/tty_io.c
--- 25/drivers/char/tty_io.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/tty_io.c	Wed Aug 31 12:50:56 2005
@@ -166,9 +166,12 @@ static struct tty_struct *alloc_tty_stru
 	return tty;
 }
 
+static void tty_buffer_free_all(struct tty_struct *);
+
 static inline void free_tty_struct(struct tty_struct *tty)
 {
 	kfree(tty->write_buf);
+	tty_buffer_free_all(tty);
 	kfree(tty);
 }
 
@@ -231,6 +234,201 @@ static int check_tty_count(struct tty_st
 }
 
 /*
+ * Tty buffer allocation management
+ */
+ 
+static void tty_buffer_free_all(struct tty_struct *tty)
+{
+	struct tty_buffer *thead;
+	while((thead = tty->buf.head) != NULL) {
+		tty->buf.head = thead->next;
+		kfree(thead);
+	}
+	while((thead = tty->buf.free) != NULL) {
+		tty->buf.free = thead->next;
+		kfree(thead);
+	}
+	tty->buf.tail = NULL;
+}
+
+static void tty_buffer_init(struct tty_struct *tty)
+{
+	tty->buf.head = NULL;
+	tty->buf.tail = NULL;
+	tty->buf.free = NULL;
+}
+
+static struct tty_buffer *tty_buffer_alloc(size_t size)
+{
+	struct tty_buffer *p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
+	if(p == NULL)
+		return NULL;
+	p->used = 0;
+	p->size = size;
+	p->next = NULL;
+	p->char_buf_ptr = (char *)(p->data);
+	p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
+/* 	printk("Flip create %p\n", p); */
+	return p;
+}
+
+/* Must be called with the tty_read lock held. This needs to acquire strategy
+   code to decide if we should kfree or relink a given expired buffer */
+
+static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
+{
+	/* Dumb strategy for now - should keep some stats */
+/* 	printk("Flip dispose %p\n", b); */
+	if(b->size >= 512)
+		kfree(b);
+	else {
+		b->next = tty->buf.free;
+		tty->buf.free = b;
+	}
+}
+
+static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
+{
+	struct tty_buffer **tbh = &tty->buf.free;
+	while((*tbh) != NULL) {
+		struct tty_buffer *t = *tbh;
+		if(t->size >= size) {
+			*tbh = t->next;
+			t->next = NULL;
+			t->used = 0;
+			/* DEBUG ONLY */
+			memset(t->data, '*', size);
+/* 			printk("Flip recycle %p\n", t); */
+			return t;
+		}
+		tbh = &((*tbh)->next);
+	}	
+	/* Round the buffer size out */
+	size = (size + 0xFF) & ~ 0xFF;
+	return tty_buffer_alloc(size);
+	/* Should possibly check if this fails for the largest buffer we
+	   have queued and recycle that ? */
+}
+
+int tty_buffer_request_room(struct tty_struct *tty, size_t size)
+{
+	struct tty_buffer *b = tty->buf.head, *n;
+	int left = 0;
+
+	/* OPTIMISATION: We could keep a per tty "zero" sized buffer to 
+	   remove this conditional if its worth it. This would be invisible
+	   to the callers */
+	if(b != NULL)
+		left = b->size - b->used;
+	if(left >= size)
+		return size;
+	/* This is the slow path - looking for new buffers to use */
+	n = tty_buffer_find(tty, size);
+	if(n == NULL)
+		return left;
+	n->next = b;
+	if(b != NULL)
+		b->next = n;
+	else 
+		tty->buf.head = n;
+	tty->buf.tail = n;
+	return size;
+}
+
+EXPORT_SYMBOL_GPL(tty_buffer_request_room);
+
+int tty_insert_flip_string(struct tty_struct *tty, unsigned char *chars, size_t size)
+{
+	int copied = 0;
+	do {
+		int space = tty_buffer_request_room(tty, size - copied);
+		struct tty_buffer *tb = tty->buf.tail;
+		/* If there is no space then tb may be NULL */
+		if(unlikely(space == 0))
+			break;
+		memcpy(tb->char_buf_ptr + tb->used, chars, space);
+		memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
+		tb->used += space;
+		copied += space;
+		chars += space;
+/* 		printk("Flip insert %d.\n", space); */
+	}
+	/* There is a small chance that we need to split the data over
+	   several buffers. If this is the case we must loop */
+	while (unlikely(size > copied));
+	return copied;
+}
+
+EXPORT_SYMBOL_GPL(tty_insert_flip_string);
+
+int tty_insert_flip_string_flags(struct tty_struct *tty, unsigned char *chars, char *flags, size_t size)
+{
+	int copied = 0;
+	do {
+		int space = tty_buffer_request_room(tty, size - copied);
+		struct tty_buffer *tb = tty->buf.tail;
+		/* If there is no space then tb may be NULL */
+		if(unlikely(space == 0))
+			break;
+		memcpy(tb->char_buf_ptr + tb->used, chars, space);
+		memcpy(tb->flag_buf_ptr + tb->used, flags, space);
+		tb->used += space;
+		copied += space;
+		chars += space;
+		flags += space;
+	}
+	/* There is a small chance that we need to split the data over
+	   several buffers. If this is the case we must loop */
+	while (unlikely(size > copied));
+	return copied;
+}
+
+EXPORT_SYMBOL_GPL(tty_insert_flip_string_flags);
+
+	
+/*
+ *	Prepare a block of space in the buffer for data. Returns the length 
+ *	available and buffer pointer to the space which is now allocated and
+ *	accounted for as ready for normal characters. This is used for drivers
+ *	that need their own block copy routines into the buffer. There is no
+ *	guarantee the buffer is a DMA target!
+ */
+ 
+int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars, size_t size)
+{
+	int space = tty_buffer_request_room(tty, size);
+	struct tty_buffer *tb = tty->buf.tail;
+	*chars = tb->char_buf_ptr + tb->used;
+	memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
+	tb->used += space;
+	return space;
+}
+
+EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
+
+/*
+ *	Prepare a block of space in the buffer for data. Returns the length 
+ *	available and buffer pointer to the space which is now allocated and
+ *	accounted for as ready for characters. This is used for drivers
+ *	that need their own block copy routines into the buffer. There is no
+ *	guarantee the buffer is a DMA target!
+ */
+ 
+int tty_prepare_flip_string_flags(struct tty_struct *tty, unsigned char **chars, char **flags, size_t size)
+{
+	int space = tty_buffer_request_room(tty, size);
+	struct tty_buffer *tb = tty->buf.tail;
+	*chars = tb->char_buf_ptr + tb->used;
+	*flags = tb->flag_buf_ptr + tb->used;
+	tb->used += space;
+	return space;
+}
+
+EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
+
+
+
+/*
  *	This is probably overkill for real world processors but
  *	they are not on hot paths so a little discipline won't do 
  *	any harm.
@@ -492,6 +690,17 @@ restart:
 	if (ld == NULL)
 		return -EINVAL;
 
+	/*
+	 *	No more input please, we are switching. The new ldisc
+	 *	will update this value in the ldisc open function
+	 */	
+	 
+	tty->receive_room = 0;
+	
+	/*
+	 *	Problem: What do we do if this blocks ?
+	 */
+	 
 	tty_wait_until_sent(tty, 0);
 
 	if (tty->ldisc.num == ldisc) {
@@ -560,9 +769,9 @@ restart:
 	 *	we say so later on.
 	 */
 
-	work = cancel_delayed_work(&tty->flip.work);
+	work = cancel_delayed_work(&tty->buf.work);
 	/*
-	 * Wait for ->hangup_work and ->flip.work handlers to terminate
+	 * Wait for ->hangup_work and ->buf.work handlers to terminate
 	 */
 	 
 	flush_scheduled_work();
@@ -616,7 +825,7 @@ restart:
 	/* Restart it in case no characters kick it off. Safe if
 	   already running */
 	if (work)
-		schedule_delayed_work(&tty->flip.work, 1);
+		schedule_delayed_work(&tty->buf.work, 1);
 	return retval;
 }
 
@@ -1724,10 +1933,10 @@ static void release_dev(struct file * fi
 	 */
 	clear_bit(TTY_LDISC, &tty->flags);
 	clear_bit(TTY_DONT_FLIP, &tty->flags);
-	cancel_delayed_work(&tty->flip.work);
+	cancel_delayed_work(&tty->buf.work);
 
 	/*
-	 * Wait for ->hangup_work and ->flip.work handlers to terminate
+	 * Wait for ->hangup_work and ->buf.work handlers to terminate
 	 */
 	 
 	flush_scheduled_work();
@@ -2519,17 +2728,15 @@ EXPORT_SYMBOL(do_SAK);
 
 /*
  * This routine is called out of the software interrupt to flush data
- * from the flip buffer to the line discipline. 
+ * from the buffer chain to the line discipline. 
  */
  
 static void flush_to_ldisc(void *private_)
 {
 	struct tty_struct *tty = (struct tty_struct *) private_;
-	unsigned char	*cp;
-	char		*fp;
-	int		count;
 	unsigned long 	flags;
 	struct tty_ldisc *disc;
+	struct tty_buffer *tbuf;
 
 	disc = tty_ldisc_ref(tty);
 	if (disc == NULL)	/*  !TTY_LDISC */
@@ -2539,28 +2746,22 @@ static void flush_to_ldisc(void *private
 		/*
 		 * Do it after the next timer tick:
 		 */
-		schedule_delayed_work(&tty->flip.work, 1);
+		schedule_delayed_work(&tty->buf.work, 1);
 		goto out;
 	}
 	spin_lock_irqsave(&tty->read_lock, flags);
-	if (tty->flip.buf_num) {
-		cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
-		fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
-		tty->flip.buf_num = 0;
-		tty->flip.char_buf_ptr = tty->flip.char_buf;
-		tty->flip.flag_buf_ptr = tty->flip.flag_buf;
-	} else {
-		cp = tty->flip.char_buf;
-		fp = tty->flip.flag_buf;
-		tty->flip.buf_num = 1;
-		tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
-		tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
+	while((tbuf = tty->buf.head) != NULL) {
+		tty->buf.head = tbuf->next;
+		spin_unlock_irqrestore(&tty->read_lock, flags);
+		/* printk("Process buffer %p for %d\n", tbuf, tbuf->used); */
+		disc->receive_buf(tty, tbuf->char_buf_ptr, 
+				       tbuf->flag_buf_ptr,
+				       tbuf->used);
+		spin_lock_irqsave(&tty->read_lock, flags);
+		tty_buffer_free(tty, tbuf);
 	}
-	count = tty->flip.count;
-	tty->flip.count = 0;
+	tty->buf.tail = NULL;
 	spin_unlock_irqrestore(&tty->read_lock, flags);
-
-	disc->receive_buf(tty, cp, fp, count);
 out:
 	tty_ldisc_deref(disc);
 }
@@ -2655,11 +2856,12 @@ void tty_flip_buffer_push(struct tty_str
 	if (tty->low_latency)
 		flush_to_ldisc((void *) tty);
 	else
-		schedule_delayed_work(&tty->flip.work, 1);
+		schedule_delayed_work(&tty->buf.work, 1);
 }
 
 EXPORT_SYMBOL(tty_flip_buffer_push);
 
+
 /*
  * This subroutine initializes a tty structure.
  */
@@ -2670,10 +2872,10 @@ static void initialize_tty_struct(struct
 	tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
 	tty->pgrp = -1;
 	tty->overrun_time = jiffies;
-	tty->flip.char_buf_ptr = tty->flip.char_buf;
-	tty->flip.flag_buf_ptr = tty->flip.flag_buf;
-	INIT_WORK(&tty->flip.work, flush_to_ldisc, tty);
-	init_MUTEX(&tty->flip.pty_sem);
+	tty->buf.head = tty->buf.tail = NULL;
+	tty_buffer_init(tty);
+	INIT_WORK(&tty->buf.work, flush_to_ldisc, tty);
+	init_MUTEX(&tty->buf.pty_sem);
 	init_MUTEX(&tty->termios_sem);
 	init_waitqueue_head(&tty->write_wait);
 	init_waitqueue_head(&tty->read_wait);
diff -puN drivers/char/viocons.c~tty-layer-buffering-revamp drivers/char/viocons.c
--- 25/drivers/char/viocons.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/viocons.c	Wed Aug 31 12:50:56 2005
@@ -994,11 +994,10 @@ static void vioHandleData(struct HvLpEve
 		 * Don't attempt to copy more data into the buffer than we
 		 * have room for because it would fail without indication.
 		 */
-		if ((tty->flip.count + 1) > TTY_FLIPBUF_SIZE) {
+		if(tty_insert_flip_char(tty, cevent->data[index], TTY_NORMAL) == 0) {
 			printk(VIOCONS_KERN_WARN "input buffer overflow!\n");
 			break;
 		}
-		tty_insert_flip_char(tty, cevent->data[index], TTY_NORMAL);
 	}
 
 	/* if cevent->len == 0 then no data was added to the buffer and flip.count == 0 */
diff -puN drivers/char/vme_scc.c~tty-layer-buffering-revamp drivers/char/vme_scc.c
--- 25/drivers/char/vme_scc.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/char/vme_scc.c	Wed Aug 31 12:50:56 2005
@@ -434,13 +434,7 @@ static irqreturn_t scc_rx_int(int irq, v
 		SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
 		return IRQ_HANDLED;
 	}
-	if (tty->flip.count < TTY_FLIPBUF_SIZE) {
-		*tty->flip.char_buf_ptr = ch;
-		*tty->flip.flag_buf_ptr = 0;
-		tty->flip.flag_buf_ptr++;
-		tty->flip.char_buf_ptr++;
-		tty->flip.count++;
-	}
+	tty_insert_flip_char(tty, ch, 0);
 
 	/* Check if another character is already ready; in that case, the
 	 * spcond_int() function must be used, because this character may have an
@@ -487,13 +481,7 @@ static irqreturn_t scc_spcond_int(int ir
 		else
 			err = 0;
 
-		if (tty->flip.count < TTY_FLIPBUF_SIZE) {
-			*tty->flip.char_buf_ptr = ch;
-			*tty->flip.flag_buf_ptr = err;
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
-		}
+		tty_insert_flip_char(tty, ch, err);
 
 		/* ++TeSche: *All* errors have to be cleared manually,
 		 * else the condition persists for the next chars
diff -puN drivers/input/serio/serport.c~tty-layer-buffering-revamp drivers/input/serio/serport.c
--- 25/drivers/input/serio/serport.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/input/serio/serport.c	Wed Aug 31 12:50:56 2005
@@ -96,6 +96,7 @@ static int serport_ldisc_open(struct tty
 	init_waitqueue_head(&serport->wait);
 
 	tty->disc_data = serport;
+	tty->receive_room = 256;
 	set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
 
 	return 0;
@@ -140,17 +141,6 @@ out:
 }
 
 /*
- * serport_ldisc_room() reports how much room we do have for receiving data.
- * Although we in fact have infinite room, we need to specify some value
- * here, and 256 seems to be reasonable.
- */
-
-static int serport_ldisc_room(struct tty_struct *tty)
-{
-	return 256;
-}
-
-/*
  * serport_ldisc_read() just waits indefinitely if everything goes well.
  * However, when the serio driver closes the serio port, it finishes,
  * returning 0 characters.
@@ -237,7 +227,6 @@ static struct tty_ldisc serport_ldisc = 
 	.read =		serport_ldisc_read,
 	.ioctl =	serport_ldisc_ioctl,
 	.receive_buf =	serport_ldisc_receive,
-	.receive_room =	serport_ldisc_room,
 	.write_wakeup =	serport_ldisc_write_wakeup
 };
 
diff -puN drivers/isdn/capi/capi.c~tty-layer-buffering-revamp drivers/isdn/capi/capi.c
--- 25/drivers/isdn/capi/capi.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/isdn/capi/capi.c	Wed Aug 31 12:50:56 2005
@@ -463,8 +463,7 @@ static int handle_recv_skb(struct capimi
 #endif
 		goto bad;
 	}
-	if (ld->receive_room &&
-	    ld->receive_room(mp->tty) < datalen) {
+	if (mp->tty->receive_room < datalen) {
 #if defined(_DEBUG_DATAFLOW) || defined(_DEBUG_TTYFUNCS)
 		printk(KERN_DEBUG "capi: no room in tty\n");
 #endif
diff -puN drivers/net/hamradio/6pack.c~tty-layer-buffering-revamp drivers/net/hamradio/6pack.c
--- 25/drivers/net/hamradio/6pack.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/net/hamradio/6pack.c	Wed Aug 31 12:50:56 2005
@@ -456,11 +456,6 @@ out:
 
 /* ----------------------------------------------------------------------- */
 
-static int sixpack_receive_room(struct tty_struct *tty)
-{
-	return 65536;  /* We can handle an infinite amount of data. :-) */
-}
-
 /*
  * Handle the 'receiver data ready' interrupt.
  * This function is called by the 'tty_io' module in the kernel when
@@ -671,6 +666,7 @@ static int sixpack_open(struct tty_struc
 
 	/* Done.  We have linked the TTY line to a channel. */
 	tty->disc_data = sp;
+	tty->receive_room = 65536;
 
 	/* Now we're ready to register. */
 	if (register_netdev(dev))
@@ -802,7 +798,6 @@ static struct tty_ldisc sp_ldisc = {
 	.close		= sixpack_close,
 	.ioctl		= sixpack_ioctl,
 	.receive_buf	= sixpack_receive_buf,
-	.receive_room	= sixpack_receive_room,
 	.write_wakeup	= sixpack_write_wakeup,
 };
 
diff -puN drivers/net/hamradio/mkiss.c~tty-layer-buffering-revamp drivers/net/hamradio/mkiss.c
--- 25/drivers/net/hamradio/mkiss.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/net/hamradio/mkiss.c	Wed Aug 31 12:50:58 2005
@@ -667,6 +667,7 @@ static int mkiss_open(struct tty_struct 
 
 	ax->tty = tty;
 	tty->disc_data = ax;
+	tty->receive_room = 65535;
 
 	if (tty->driver->flush_buffer)
 		tty->driver->flush_buffer(tty);
diff -puN drivers/net/irda/irtty-sir.c~tty-layer-buffering-revamp drivers/net/irda/irtty-sir.c
--- 25/drivers/net/irda/irtty-sir.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/net/irda/irtty-sir.c	Wed Aug 31 12:50:58 2005
@@ -289,22 +289,6 @@ static void irtty_receive_buf(struct tty
 }
 
 /*
- * Function irtty_receive_room (tty)
- *
- *    Used by the TTY to find out how much data we can receive at a time
- * 
-*/
-static int irtty_receive_room(struct tty_struct *tty) 
-{
-	struct sirtty_cb *priv = tty->disc_data;
-
-	IRDA_ASSERT(priv != NULL, return 0;);
-	IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return 0;);
-
-	return 65536;  /* We can handle an infinite amount of data. :-) */
-}
-
-/*
  * Function irtty_write_wakeup (tty)
  *
  *    Called by the driver when there's room for more data.  If we have
@@ -534,6 +518,7 @@ static int irtty_open(struct tty_struct 
 
 	dev->priv = priv;
 	tty->disc_data = priv;
+	tty->receive_room = 65536;
 
 	up(&irtty_sem);
 
@@ -605,7 +590,6 @@ static struct tty_ldisc irda_ldisc = {
 	.ioctl		= irtty_ioctl,
  	.poll		= NULL,
 	.receive_buf	= irtty_receive_buf,
-	.receive_room	= irtty_receive_room,
 	.write_wakeup	= irtty_write_wakeup,
 	.owner		= THIS_MODULE,
 };
diff -puN drivers/net/ppp_async.c~tty-layer-buffering-revamp drivers/net/ppp_async.c
--- 25/drivers/net/ppp_async.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/net/ppp_async.c	Wed Aug 31 12:50:58 2005
@@ -189,7 +189,7 @@ ppp_asynctty_open(struct tty_struct *tty
 		goto out_free;
 
 	tty->disc_data = ap;
-
+	tty->receive_room = 65536;
 	return 0;
 
  out_free:
@@ -343,12 +343,6 @@ ppp_asynctty_poll(struct tty_struct *tty
 	return 0;
 }
 
-static int
-ppp_asynctty_room(struct tty_struct *tty)
-{
-	return 65535;
-}
-
 /*
  * This can now be called from hard interrupt level as well
  * as soft interrupt level or mainline.
@@ -398,7 +392,6 @@ static struct tty_ldisc ppp_ldisc = {
 	.write	= ppp_asynctty_write,
 	.ioctl	= ppp_asynctty_ioctl,
 	.poll	= ppp_asynctty_poll,
-	.receive_room = ppp_asynctty_room,
 	.receive_buf = ppp_asynctty_receive,
 	.write_wakeup = ppp_asynctty_wakeup,
 };
diff -puN drivers/net/ppp_synctty.c~tty-layer-buffering-revamp drivers/net/ppp_synctty.c
--- 25/drivers/net/ppp_synctty.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/net/ppp_synctty.c	Wed Aug 31 12:50:58 2005
@@ -237,7 +237,7 @@ ppp_sync_open(struct tty_struct *tty)
 		goto out_free;
 
 	tty->disc_data = ap;
-
+	tty->receive_room = 65536;
 	return 0;
 
  out_free:
@@ -384,12 +384,6 @@ ppp_sync_poll(struct tty_struct *tty, st
 	return 0;
 }
 
-static int
-ppp_sync_room(struct tty_struct *tty)
-{
-	return 65535;
-}
-
 /*
  * This can now be called from hard interrupt level as well
  * as soft interrupt level or mainline.
@@ -439,7 +433,6 @@ static struct tty_ldisc ppp_sync_ldisc =
 	.write	= ppp_sync_write,
 	.ioctl	= ppp_synctty_ioctl,
 	.poll	= ppp_sync_poll,
-	.receive_room = ppp_sync_room,
 	.receive_buf = ppp_sync_receive,
 	.write_wakeup = ppp_sync_wakeup,
 };
diff -puN drivers/net/slip.c~tty-layer-buffering-revamp drivers/net/slip.c
--- 25/drivers/net/slip.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/net/slip.c	Wed Aug 31 12:50:58 2005
@@ -651,11 +651,6 @@ static void sl_setup(struct net_device *
  ******************************************/
 
 
-static int slip_receive_room(struct tty_struct *tty)
-{
-	return 65536;  /* We can handle an infinite amount of data. :-) */
-}
-
 /*
  * Handle the 'receiver data ready' interrupt.
  * This function is called by the 'tty_io' module in the kernel when
@@ -869,10 +864,6 @@ static int slip_open(struct tty_struct *
 	sl->line = tty_devnum(tty);
 	sl->pid = current->pid;
 	
-	/* FIXME: already done before we were called - seems this can go */
-	if (tty->driver->flush_buffer)
-		tty->driver->flush_buffer(tty);
-		
 	if (!test_bit(SLF_INUSE, &sl->flags)) {
 		/* Perform the low-level SLIP initialization. */
 		if ((err = sl_alloc_bufs(sl, SL_MTU)) != 0)
@@ -897,6 +888,7 @@ static int slip_open(struct tty_struct *
 
 	/* Done.  We have linked the TTY line to a channel. */
 	rtnl_unlock();
+	tty->receive_room = 65536;	/* We don't flow control */
 	return sl->dev->base_addr;
 
 err_free_bufs:
@@ -1329,7 +1321,6 @@ static struct tty_ldisc	sl_ldisc = {
 	.close	 	= slip_close,
 	.ioctl		= slip_ioctl,
 	.receive_buf	= slip_receive_buf,
-	.receive_room	= slip_receive_room,
 	.write_wakeup	= slip_write_wakeup,
 };
 
diff -puN drivers/net/wan/pc300_tty.c~tty-layer-buffering-revamp drivers/net/wan/pc300_tty.c
--- 25/drivers/net/wan/pc300_tty.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/net/wan/pc300_tty.c	Wed Aug 31 12:50:58 2005
@@ -689,7 +689,7 @@ static void cpc_tty_rx_work(void * data)
 					}
 				}	
 				cpc_tty->buf_rx.first = cpc_tty->buf_rx.first->next;
-				kfree(buf);
+				kfree((void *)buf);
 				buf = cpc_tty->buf_rx.first;
 				flg_rx = 1;
 			}
diff -puN drivers/net/wan/x25_asy.c~tty-layer-buffering-revamp drivers/net/wan/x25_asy.c
--- 25/drivers/net/wan/x25_asy.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/net/wan/x25_asy.c	Wed Aug 31 12:50:58 2005
@@ -515,11 +515,6 @@ static int x25_asy_close(struct net_devi
 	return 0;
 }
 
-static int x25_asy_receive_room(struct tty_struct *tty)
-{
-	return 65536;  /* We can handle an infinite amount of data. :-) */
-}
-
 /*
  * Handle the 'receiver data ready' interrupt.
  * This function is called by the 'tty_io' module in the kernel when
@@ -573,6 +568,7 @@ static int x25_asy_open_tty(struct tty_s
 
 	sl->tty = tty;
 	tty->disc_data = sl;
+	tty->receive_room = 65536;
 	if (tty->driver->flush_buffer)  {
 		tty->driver->flush_buffer(tty);
 	}
@@ -779,7 +775,6 @@ static struct tty_ldisc x25_ldisc = {
 	.close		= x25_asy_close_tty,
 	.ioctl		= x25_asy_ioctl,
 	.receive_buf	= x25_asy_receive_buf,
-	.receive_room	= x25_asy_receive_room,
 	.write_wakeup	= x25_asy_write_wakeup,
 };
 
diff -puN drivers/net/wireless/strip.c~tty-layer-buffering-revamp drivers/net/wireless/strip.c
--- 25/drivers/net/wireless/strip.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/net/wireless/strip.c	Wed Aug 31 12:50:58 2005
@@ -1682,11 +1682,6 @@ static int strip_rebuild_header(struct s
 /************************************************************************/
 /* Receiving routines							*/
 
-static int strip_receive_room(struct tty_struct *tty)
-{
-	return 0x10000;		/* We can handle an infinite amount of data. :-) */
-}
-
 /*
  * This function parses the response to the ATS300? command,
  * extracting the radio version and serial number.
@@ -2431,7 +2426,7 @@ static struct net_device_stats *strip_ge
 /*
  * Here's the order things happen:
  * When the user runs "slattach -p strip ..."
- *  1. The TTY module calls strip_open
+ *  1. The TTY module calls strip_open;;
  *  2. strip_open calls strip_alloc
  *  3.                  strip_alloc calls register_netdev
  *  4.                  register_netdev calls strip_dev_init
@@ -2664,6 +2659,8 @@ static int strip_open(struct tty_struct 
 
 	strip_info->tty = tty;
 	tty->disc_data = strip_info;
+	tty->receive_room = 65536;
+	
 	if (tty->driver->flush_buffer)
 		tty->driver->flush_buffer(tty);
 
@@ -2774,7 +2771,6 @@ static struct tty_ldisc strip_ldisc = {
 	.close = strip_close,
 	.ioctl = strip_ioctl,
 	.receive_buf = strip_receive_buf,
-	.receive_room = strip_receive_room,
 	.write_wakeup = strip_write_some_more,
 };
 
diff -puN drivers/serial/21285.c~tty-layer-buffering-revamp drivers/serial/21285.c
--- 25/drivers/serial/21285.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/21285.c	Wed Aug 31 12:50:58 2005
@@ -96,15 +96,6 @@ static irqreturn_t serial21285_rx_chars(
 
 	status = *CSR_UARTFLG;
 	while (!(status & 0x10) && max_count--) {
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
-			if (tty->low_latency)
-				tty_flip_buffer_push(tty);
-			/*
-			 * If this failed then we will throw away the
-			 * bytes but must do so to clear interrupts
-			 */
-		}
-
 		ch = *CSR_UARTDR;
 		flag = TTY_NORMAL;
 		port->icount.rx++;
diff -puN drivers/serial/68328serial.c~tty-layer-buffering-revamp drivers/serial/68328serial.c
--- 25/drivers/serial/68328serial.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/68328serial.c	Wed Aug 31 12:50:58 2005
@@ -294,7 +294,7 @@ static _INLINE_ void receive_chars(struc
 {
 	struct tty_struct *tty = info->tty;
 	m68328_uart *uart = &uart_addr[info->line];
-	unsigned char ch;
+	unsigned char ch, flag;
 
 	/*
 	 * This do { } while() loop will get ALL chars out of Rx FIFO 
@@ -332,26 +332,24 @@ static _INLINE_ void receive_chars(struc
 		/*
 		 * Make sure that we do not overflow the buffer
 		 */
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
+		if (tty_request_buffer_room(tty, 1) == 0) {
 			schedule_work(&tty->flip.work);
 			return;
 		}
 
+		flag = TTY_NORMAL;
+		
 		if(rx & URX_PARITY_ERROR) {
-			*tty->flip.flag_buf_ptr++ = TTY_PARITY;
+			flag = TTY_PARITY;
 			status_handle(info, rx);
 		} else if(rx & URX_OVRUN) {
-			*tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
+			flag = TTY_OVERRUN;
 			status_handle(info, rx);
 		} else if(rx & URX_FRAME_ERROR) {
-			*tty->flip.flag_buf_ptr++ = TTY_FRAME;
+			flag = TTY_FRAME;
 			status_handle(info, rx);
-		} else {
-			*tty->flip.flag_buf_ptr++ = 0; /* XXX */
 		}
-                *tty->flip.char_buf_ptr++ = ch;
-		tty->flip.count++;
-
+		tty_insert_flip_char(tty, ch, flag);
 #ifndef CONFIG_XCOPILOT_BUGS
 	} while((rx = uart->urx.w) & URX_DATA_READY);
 #endif
diff -puN drivers/serial/68360serial.c~tty-layer-buffering-revamp drivers/serial/68360serial.c
--- 25/drivers/serial/68360serial.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/68360serial.c	Wed Aug 31 12:50:58 2005
@@ -394,7 +394,7 @@ static void rs_360_start(struct tty_stru
 static _INLINE_ void receive_chars(ser_info_t *info)
 {
 	struct tty_struct *tty = info->tty;
-	unsigned char ch, *cp;
+	unsigned char ch, flag, *cp;
 	/*int	ignored = 0;*/
 	int	i;
 	ushort	status;
@@ -438,24 +438,15 @@ static _INLINE_ void receive_chars(ser_i
 		cp = (char *)bdp->buf;
 		status = bdp->status;
 
-		/* Check to see if there is room in the tty buffer for
-		 * the characters in our BD buffer.  If not, we exit
-		 * now, leaving the BD with the characters.  We'll pick
-		 * them up again on the next receive interrupt (which could
-		 * be a timeout).
-		 */
-		if ((tty->flip.count + i) >= TTY_FLIPBUF_SIZE)
-			break;
-
 		while (i-- > 0) {
 			ch = *cp++;
-			*tty->flip.char_buf_ptr = ch;
 			icount->rx++;
 
 #ifdef SERIAL_DEBUG_INTR
 			printk("DR%02x:%02x...", ch, status);
 #endif
-			*tty->flip.flag_buf_ptr = 0;
+			flag = TTY_NORMAL;
+
 			if (status & (BD_SC_BR | BD_SC_FR |
 				       BD_SC_PR | BD_SC_OV)) {
 				/*
@@ -490,30 +481,18 @@ static _INLINE_ void receive_chars(ser_i
 					if (info->flags & ASYNC_SAK)
 						do_SAK(tty);
 				} else if (status & BD_SC_PR)
-					*tty->flip.flag_buf_ptr = TTY_PARITY;
+					flag = TTY_PARITY;
 				else if (status & BD_SC_FR)
-					*tty->flip.flag_buf_ptr = TTY_FRAME;
-				if (status & BD_SC_OV) {
-					/*
-					 * Overrun is special, since it's
-					 * reported immediately, and doesn't
-					 * affect the current character
-					 */
-					if (tty->flip.count < TTY_FLIPBUF_SIZE) {
-						tty->flip.count++;
-						tty->flip.flag_buf_ptr++;
-						tty->flip.char_buf_ptr++;
-						*tty->flip.flag_buf_ptr =
-								TTY_OVERRUN;
-					}
-				}
+					flag = TTY_FRAME;
 			}
-			if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-				break;
-
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
+			tty_insert_flip_char(tty, ch, flag);
+			if (status & BD_SC_OV) 
+				/*
+				 * Overrun is special, since it's
+				 * reported immediately, and doesn't
+				 * affect the current character
+				 */
+				tty_insert_flip_char(tty, 0, TTY_OVERRUN);
 		}
 
 		/* This BD is ready to be used again.  Clear status.
@@ -541,12 +520,7 @@ static _INLINE_ void receive_break(ser_i
 	/* Check to see if there is room in the tty buffer for
 	 * the break.  If not, we exit now, losing the break.  FIXME
 	 */
-	if ((tty->flip.count + 1) >= TTY_FLIPBUF_SIZE)
-		return;
-	*(tty->flip.flag_buf_ptr++) = TTY_BREAK;
-	*(tty->flip.char_buf_ptr++) = 0;
-	tty->flip.count++;
-
+	tty_insert_flip_char(tty, 0, TTY_BREAK);
 	schedule_work(&tty->flip.work);
 }
 
diff -puN drivers/serial/8250.c~tty-layer-buffering-revamp drivers/serial/8250.c
--- 25/drivers/serial/8250.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/8250.c	Wed Aug 31 12:50:58 2005
@@ -1070,19 +1070,6 @@ receive_chars(struct uart_8250_port *up,
 	char flag;
 
 	do {
-		/* The following is not allowed by the tty layer and
-		   unsafe. It should be fixed ASAP */
-		if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
-			if (tty->low_latency) {
-				spin_unlock(&up->port.lock);
-				tty_flip_buffer_push(tty);
-				spin_lock(&up->port.lock);
-			}
-			/*
-			 * If this failed then we will throw away the
-			 * bytes but must do so to clear interrupts
-			 */
-		}
 		ch = serial_inp(up, UART_RX);
 		flag = TTY_NORMAL;
 		up->port.icount.rx++;
diff -puN drivers/serial/amba-pl010.c~tty-layer-buffering-revamp drivers/serial/amba-pl010.c
--- 25/drivers/serial/amba-pl010.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/amba-pl010.c	Wed Aug 31 12:50:58 2005
@@ -153,15 +153,6 @@ pl010_rx_chars(struct uart_port *port)
 
 	status = UART_GET_FR(port);
 	while (UART_RX_DATA(status) && max_count--) {
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
-			if (tty->low_latency)
-				tty_flip_buffer_push(tty);
-			/*
-			 * If this failed then we will throw away the
-			 * bytes but must do so to clear interrupts.
-			 */
-		}
-
 		ch = UART_GET_CHAR(port);
 		flag = TTY_NORMAL;
 
diff -puN drivers/serial/amba-pl011.c~tty-layer-buffering-revamp drivers/serial/amba-pl011.c
--- 25/drivers/serial/amba-pl011.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/amba-pl011.c	Wed Aug 31 12:50:58 2005
@@ -119,15 +119,6 @@ pl011_rx_chars(struct uart_amba_port *ua
 
 	status = readw(uap->port.membase + UART01x_FR);
 	while ((status & UART01x_FR_RXFE) == 0 && max_count--) {
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
-			if (tty->low_latency)
-				tty_flip_buffer_push(tty);
-			/*
-			 * If this failed then we will throw away the
-			 * bytes but must do so to clear interrupts
-			 */
-		}
-
 		ch = readw(uap->port.membase + UART01x_DR);
 		flag = TTY_NORMAL;
 		uap->port.icount.rx++;
diff -puN drivers/serial/au1x00_uart.c~tty-layer-buffering-revamp drivers/serial/au1x00_uart.c
--- 25/drivers/serial/au1x00_uart.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/au1x00_uart.c	Wed Aug 31 12:50:58 2005
@@ -241,18 +241,12 @@ static _INLINE_ void
 receive_chars(struct uart_8250_port *up, int *status, struct pt_regs *regs)
 {
 	struct tty_struct *tty = up->port.info->tty;
-	unsigned char ch;
+	unsigned char ch, flag;
 	int max_count = 256;
 
 	do {
-		if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
-			tty->flip.work.func((void *)tty);
-			if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-				return; // if TTY_DONT_FLIP is set
-		}
 		ch = serial_inp(up, UART_RX);
-		*tty->flip.char_buf_ptr = ch;
-		*tty->flip.flag_buf_ptr = TTY_NORMAL;
+		flag = TTY_NORMAL;
 		up->port.icount.rx++;
 
 		if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
@@ -292,30 +286,23 @@ receive_chars(struct uart_8250_port *up,
 #endif
 			if (*status & UART_LSR_BI) {
 				DEBUG_INTR("handling break....");
-				*tty->flip.flag_buf_ptr = TTY_BREAK;
+				flag = TTY_BREAK;
 			} else if (*status & UART_LSR_PE)
-				*tty->flip.flag_buf_ptr = TTY_PARITY;
+				flag = TTY_PARITY;
 			else if (*status & UART_LSR_FE)
-				*tty->flip.flag_buf_ptr = TTY_FRAME;
+				flag = TTY_FRAME;
 		}
 		if (uart_handle_sysrq_char(&up->port, ch, regs))
 			goto ignore_char;
-		if ((*status & up->port.ignore_status_mask) == 0) {
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
-		}
-		if ((*status & UART_LSR_OE) &&
-		    tty->flip.count < TTY_FLIPBUF_SIZE) {
+		if ((*status & up->port.ignore_status_mask) == 0)
+			tty_insert_flip_char(tty, ch, flag);
+		if (*status & UART_LSR_OE)
 			/*
 			 * Overrun is special, since it's reported
 			 * immediately, and doesn't affect the current
 			 * character.
 			 */
-			*tty->flip.flag_buf_ptr = TTY_OVERRUN;
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
+			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
 		}
 	ignore_char:
 		*status = serial_inp(up, UART_LSR);
diff -puN drivers/serial/clps711x.c~tty-layer-buffering-revamp drivers/serial/clps711x.c
--- 25/drivers/serial/clps711x.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/clps711x.c	Wed Aug 31 12:50:58 2005
@@ -106,8 +106,6 @@ static irqreturn_t clps711xuart_int_rx(i
 	while (!(status & SYSFLG_URXFE)) {
 		ch = clps_readl(UARTDR(port));
 
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-			goto ignore_char;
 		port->icount.rx++;
 
 		flg = TTY_NORMAL;
diff -puN drivers/serial/dz.c~tty-layer-buffering-revamp drivers/serial/dz.c
--- 25/drivers/serial/dz.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/dz.c	Wed Aug 31 12:50:58 2005
@@ -216,8 +216,6 @@ static inline void dz_receive_chars(stru
 
 		if (!tty)
 			break;
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-			break;
 
 		icount->rx++;
 
diff -puN drivers/serial/icom.c~tty-layer-buffering-revamp drivers/serial/icom.c
--- 25/drivers/serial/icom.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/icom.c	Wed Aug 31 12:50:58 2005
@@ -730,6 +730,7 @@ static void recv_interrupt(u16 port_int_
 	unsigned short int status;
 	struct uart_icount *icount;
 	unsigned long offset;
+	unsigned char flag;
 
 	trace(icom_port, "RCV_COMPLETE", 0);
 	rcv_buff = icom_port->next_rcv;
@@ -740,9 +741,8 @@ static void recv_interrupt(u16 port_int_
 		trace(icom_port, "FID_STATUS", status);
 		count = cpu_to_le16(icom_port->statStg->rcv[rcv_buff].leLength);
 
+                count = tty_buffer_request_room(tty, count);
 		trace(icom_port, "RCV_COUNT", count);
-		if (count > (TTY_FLIPBUF_SIZE - tty->flip.count))
-			count = TTY_FLIPBUF_SIZE - tty->flip.count;
 
 		trace(icom_port, "REAL_COUNT", count);
 
@@ -750,16 +750,9 @@ static void recv_interrupt(u16 port_int_
 			cpu_to_le32(icom_port->statStg->rcv[rcv_buff].leBuffer) -
 			icom_port->recv_buf_pci;
 
-		memcpy(tty->flip.char_buf_ptr,(unsigned char *)
-		       ((unsigned long)icom_port->recv_buf + offset), count);
-
-		if (count > 0) {
-			tty->flip.count += count - 1;
-			tty->flip.char_buf_ptr += count - 1;
-
-			memset(tty->flip.flag_buf_ptr, 0, count);
-			tty->flip.flag_buf_ptr += count - 1;
-		}
+		/* Block copy all but the last byte as this may have status */
+		if(count > 0)
+			tty_insert_flip_string(tty, icon_port->recv_buf + offset, count - 1);
 
 		icount = &icom_port->uart_port.icount;
 		icount->rx += count;
@@ -772,6 +765,8 @@ static void recv_interrupt(u16 port_int_
 			trace(icom_port, "BREAK_DET", 0);
 		}
 
+		flag = TTY_NORMAL;
+
 		if (status &
 		    (SA_FLAGS_BREAK_DET | SA_FLAGS_PARITY_ERROR |
 		     SA_FLAGS_FRAME_ERROR | SA_FLAGS_OVERRUN)) {
@@ -798,33 +793,26 @@ static void recv_interrupt(u16 port_int_
 			status &= icom_port->read_status_mask;
 
 			if (status & SA_FLAGS_BREAK_DET) {
-				*tty->flip.flag_buf_ptr = TTY_BREAK;
+				flag = TTY_BREAK;
 			} else if (status & SA_FLAGS_PARITY_ERROR) {
 				trace(icom_port, "PARITY_ERROR", 0);
-				*tty->flip.flag_buf_ptr = TTY_PARITY;
+				flag = TTY_PARITY;
 			} else if (status & SA_FLAGS_FRAME_ERROR)
-				*tty->flip.flag_buf_ptr = TTY_FRAME;
+				flag = TTY_FRAME;
 
-			if (status & SA_FLAGS_OVERRUN) {
-				/*
-				 * Overrun is special, since it's
-				 * reported immediately, and doesn't
-				 * affect the current character
-				 */
-				if (tty->flip.count < TTY_FLIPBUF_SIZE) {
-					tty->flip.count++;
-					tty->flip.flag_buf_ptr++;
-					tty->flip.char_buf_ptr++;
-					*tty->flip.flag_buf_ptr = TTY_OVERRUN;
-				}
-			}
 		}
 
-		tty->flip.flag_buf_ptr++;
-		tty->flip.char_buf_ptr++;
-		tty->flip.count++;
-		ignore_char:
-			icom_port->statStg->rcv[rcv_buff].flags = 0;
+		tty_insert_flip_char(tty, icon_port->recv_buf + offset + count - 1, flag);
+
+		if (status & SA_FLAGS_OVERRUN)
+			/*
+			 * Overrun is special, since it's
+			 * reported immediately, and doesn't
+			 * affect the current character
+			 */
+			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
+ignore_char:
+		icom_port->statStg->rcv[rcv_buff].flags = 0;
 		icom_port->statStg->rcv[rcv_buff].leLength = 0;
 		icom_port->statStg->rcv[rcv_buff].WorkingLength =
 			(unsigned short int) cpu_to_le16(RCV_BUFF_SZ);
diff -puN drivers/serial/imx.c~tty-layer-buffering-revamp drivers/serial/imx.c
--- 25/drivers/serial/imx.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/imx.c	Wed Aug 31 12:50:58 2005
@@ -240,9 +240,6 @@ static irqreturn_t imx_rxint(int irq, vo
 	error_return:
 		tty_insert_flip_char(tty, rx, flg);
 
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-			goto out;
-
 	ignore_char:
 		rx = URXD0((u32)sport->port.membase);
 	} while(rx & URXD_CHARRDY);
diff -puN drivers/serial/ioc4_serial.c~tty-layer-buffering-revamp drivers/serial/ioc4_serial.c
--- 25/drivers/serial/ioc4_serial.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/ioc4_serial.c	Wed Aug 31 12:50:58 2005
@@ -2331,19 +2331,13 @@ static void receive_chars(struct uart_po
 
 	tty = info->tty;
 
-	request_count = TTY_FLIPBUF_SIZE - tty->flip.count - 1;
+	request_count = tty_request_room(tty, IOC4_MAX_CHARS - 2);
 
 	if (request_count > 0) {
-		if (request_count > IOC4_MAX_CHARS - 2)
-			request_count = IOC4_MAX_CHARS - 2;
 		icount = &the_port->icount;
 		read_count = do_read(the_port, ch, request_count);
 		if (read_count > 0) {
-			memcpy(tty->flip.char_buf_ptr, ch, read_count);
-			memset(tty->flip.flag_buf_ptr, TTY_NORMAL, read_count);
-			tty->flip.char_buf_ptr += read_count;
-			tty->flip.flag_buf_ptr += read_count;
-			tty->flip.count += read_count;
+			tty_insert_flip_string(tty, ch, read_count);
 			icount->rx += read_count;
 		}
 	}
diff -puN drivers/serial/ip22zilog.c~tty-layer-buffering-revamp drivers/serial/ip22zilog.c
--- 25/drivers/serial/ip22zilog.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/ip22zilog.c	Wed Aug 31 12:50:58 2005
@@ -259,13 +259,7 @@ static void ip22zilog_receive_chars(stru
 	struct tty_struct *tty = up->port.info->tty;	/* XXX info==NULL? */
 
 	while (1) {
-		unsigned char ch, r1;
-
-		if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
-			tty->flip.work.func((void *)tty);
-			if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-				return;		/* XXX Ignores SysRq when we need it most. Fix. */
-		}
+		unsigned char ch, r1, flag;
 
 		r1 = read_zsreg(channel, R1);
 		if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
@@ -303,8 +297,7 @@ static void ip22zilog_receive_chars(stru
 		}
 
 		/* A real serial line, record the character and status.  */
-		*tty->flip.char_buf_ptr = ch;
-		*tty->flip.flag_buf_ptr = TTY_NORMAL;
+		flag = TTY_NORMAL;
 		up->port.icount.rx++;
 		if (r1 & (BRK_ABRT | PAR_ERR | Rx_OVR | CRC_ERR)) {
 			if (r1 & BRK_ABRT) {
@@ -321,28 +314,21 @@ static void ip22zilog_receive_chars(stru
 				up->port.icount.overrun++;
 			r1 &= up->port.read_status_mask;
 			if (r1 & BRK_ABRT)
-				*tty->flip.flag_buf_ptr = TTY_BREAK;
+				flag = TTY_BREAK;
 			else if (r1 & PAR_ERR)
-				*tty->flip.flag_buf_ptr = TTY_PARITY;
+				flag = TTY_PARITY;
 			else if (r1 & CRC_ERR)
-				*tty->flip.flag_buf_ptr = TTY_FRAME;
+				flag = TTY_FRAME;
 		}
 		if (uart_handle_sysrq_char(&up->port, ch, regs))
 			goto next_char;
 
 		if (up->port.ignore_status_mask == 0xff ||
-		    (r1 & up->port.ignore_status_mask) == 0) {
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
-		}
-		if ((r1 & Rx_OVR) &&
-		    tty->flip.count < TTY_FLIPBUF_SIZE) {
-			*tty->flip.flag_buf_ptr = TTY_OVERRUN;
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
-		}
+		    (r1 & up->port.ignore_status_mask) == 0)
+		    	tty_insert_flip_char(tty, ch, flag);
+
+		if (r1 & Rx_OVR)
+			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
 	next_char:
 		ch = readb(&channel->control);
 		ZSDELAY();
diff -puN drivers/serial/m32r_sio.c~tty-layer-buffering-revamp drivers/serial/m32r_sio.c
--- 25/drivers/serial/m32r_sio.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/m32r_sio.c	Wed Aug 31 12:50:58 2005
@@ -331,17 +331,12 @@ static _INLINE_ void receive_chars(struc
 {
 	struct tty_struct *tty = up->port.info->tty;
 	unsigned char ch;
+	unsigned char flag;
 	int max_count = 256;
 
 	do {
-		if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
-			tty->flip.work.func((void *)tty);
-			if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-				return; // if TTY_DONT_FLIP is set
-		}
 		ch = sio_in(up, SIORXB);
-		*tty->flip.char_buf_ptr = ch;
-		*tty->flip.flag_buf_ptr = TTY_NORMAL;
+		flag = TTY_NORMAL;
 		up->port.icount.rx++;
 
 		if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
@@ -380,30 +375,24 @@ static _INLINE_ void receive_chars(struc
 
 			if (*status & UART_LSR_BI) {
 				DEBUG_INTR("handling break....");
-				*tty->flip.flag_buf_ptr = TTY_BREAK;
+				flag = TTY_BREAK;
 			} else if (*status & UART_LSR_PE)
-				*tty->flip.flag_buf_ptr = TTY_PARITY;
+				flag = TTY_PARITY;
 			else if (*status & UART_LSR_FE)
-				*tty->flip.flag_buf_ptr = TTY_FRAME;
+				flag = TTY_FRAME;
 		}
 		if (uart_handle_sysrq_char(&up->port, ch, regs))
 			goto ignore_char;
-		if ((*status & up->port.ignore_status_mask) == 0) {
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
-		}
-		if ((*status & UART_LSR_OE) &&
-		    tty->flip.count < TTY_FLIPBUF_SIZE) {
+		if ((*status & up->port.ignore_status_mask) == 0)
+			tty_insert_flip_char(tty, ch, flag);
+
+		if (*status & UART_LSR_OE)
 			/*
 			 * Overrun is special, since it's reported
 			 * immediately, and doesn't affect the current
 			 * character.
 			 */
-			*tty->flip.flag_buf_ptr = TTY_OVERRUN;
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
+			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
 		}
 	ignore_char:
 		*status = serial_in(up, UART_LSR);
diff -puN drivers/serial/mcfserial.c~tty-layer-buffering-revamp drivers/serial/mcfserial.c
--- 25/drivers/serial/mcfserial.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/mcfserial.c	Wed Aug 31 12:50:58 2005
@@ -307,7 +307,7 @@ static inline void receive_chars(struct 
 {
 	volatile unsigned char	*uartp;
 	struct tty_struct	*tty = info->tty;
-	unsigned char		status, ch;
+	unsigned char		status, ch, flag;
 
 	if (!tty)
 		return;
@@ -315,10 +315,6 @@ static inline void receive_chars(struct 
 	uartp = info->addr;
 
 	while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
-
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-			break;
-
 		ch = uartp[MCFUART_URB];
 		info->stats.rx++;
 
@@ -329,29 +325,24 @@ static inline void receive_chars(struct 
 		}
 #endif
 
-		tty->flip.count++;
+		flag = TTY_NORMAL;
 		if (status & MCFUART_USR_RXERR) {
 			uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
 			if (status & MCFUART_USR_RXBREAK) {
 				info->stats.rxbreak++;
-				*tty->flip.flag_buf_ptr++ = TTY_BREAK;
+				flag = TTY_BREAK;
 			} else if (status & MCFUART_USR_RXPARITY) {
 				info->stats.rxparity++;
-				*tty->flip.flag_buf_ptr++ = TTY_PARITY;
+				flag = TTY_PARITY;
 			} else if (status & MCFUART_USR_RXOVERRUN) {
 				info->stats.rxoverrun++;
-				*tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
+				flag = TTY_OVERRUN;
 			} else if (status & MCFUART_USR_RXFRAMING) {
 				info->stats.rxframing++;
-				*tty->flip.flag_buf_ptr++ = TTY_FRAME;
-			} else {
-				/* This should never happen... */
-				*tty->flip.flag_buf_ptr++ = 0;
+				flag = TTY_FRAME;
 			}
-		} else {
-			*tty->flip.flag_buf_ptr++ = 0;
 		}
-		*tty->flip.char_buf_ptr++ = ch;
+		tty_insert_flip_char(tty, ch, flag);
 	}
 
 	schedule_work(&tty->flip.work);
diff -puN drivers/serial/mpc52xx_uart.c~tty-layer-buffering-revamp drivers/serial/mpc52xx_uart.c
--- 25/drivers/serial/mpc52xx_uart.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/mpc52xx_uart.c	Wed Aug 31 12:50:58 2005
@@ -401,17 +401,13 @@ static inline int
 mpc52xx_uart_int_rx_chars(struct uart_port *port, struct pt_regs *regs)
 {
 	struct tty_struct *tty = port->info->tty;
-	unsigned char ch;
+	unsigned char ch, flag;
 	unsigned short status;
 
 	/* While we can read, do so ! */
 	while ( (status = in_be16(&PSC(port)->mpc52xx_psc_status)) &
 	        MPC52xx_PSC_SR_RXRDY) {
 
-		/* If we are full, just stop reading */
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-			break;
-		
 		/* Get the char */
 		ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8);
 
@@ -424,45 +420,35 @@ mpc52xx_uart_int_rx_chars(struct uart_po
 #endif
 
 		/* Store it */
-		*tty->flip.char_buf_ptr = ch;
-		*tty->flip.flag_buf_ptr = 0;
+
+		flag = TTY_NORMAL;
 		port->icount.rx++;
 	
 		if ( status & (MPC52xx_PSC_SR_PE |
 		               MPC52xx_PSC_SR_FE |
-		               MPC52xx_PSC_SR_RB |
-		               MPC52xx_PSC_SR_OE) ) {
+		               MPC52xx_PSC_SR_RB) ) {
 			
 			if (status & MPC52xx_PSC_SR_RB) {
-				*tty->flip.flag_buf_ptr = TTY_BREAK;
+				flag = TTY_BREAK;
 				uart_handle_break(port);
 			} else if (status & MPC52xx_PSC_SR_PE)
-				*tty->flip.flag_buf_ptr = TTY_PARITY;
+				flag = TTY_PARITY;
 			else if (status & MPC52xx_PSC_SR_FE)
-				*tty->flip.flag_buf_ptr = TTY_FRAME;
-			if (status & MPC52xx_PSC_SR_OE) {
-				/*
-				 * Overrun is special, since it's
-				 * reported immediately, and doesn't
-				 * affect the current character
-				 */
-				if (tty->flip.count < (TTY_FLIPBUF_SIZE-1)) {
-					tty->flip.flag_buf_ptr++;
-					tty->flip.char_buf_ptr++;
-					tty->flip.count++;
-				}
-				*tty->flip.flag_buf_ptr = TTY_OVERRUN;
-			}
+				flag = TTY_FRAME;
 
 			/* Clear error condition */
 			out_8(&PSC(port)->command,MPC52xx_PSC_RST_ERR_STAT);
 
 		}
-
-		tty->flip.char_buf_ptr++;
-		tty->flip.flag_buf_ptr++;
-		tty->flip.count++;
-
+		tty_insert_flip_char(tty, ch, flag);
+		if (status & MPC52xx_PSC_SR_OE) {
+			/*
+			 * Overrun is special, since it's
+			 * reported immediately, and doesn't
+			 * affect the current character
+			 */
+			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
+		}
 	}
 
 	tty_flip_buffer_push(tty);
diff -puN drivers/serial/mpsc.c~tty-layer-buffering-revamp drivers/serial/mpsc.c
--- 25/drivers/serial/mpsc.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/mpsc.c	Wed Aug 31 12:50:58 2005
@@ -767,12 +767,12 @@ mpsc_rx_intr(struct mpsc_port_info *pi, 
 		bytes_in = be16_to_cpu(rxre->bytecnt);
 
 		/* Following use of tty struct directly is deprecated */
-		if (unlikely((tty->flip.count + bytes_in) >= TTY_FLIPBUF_SIZE)){
+		if (unlikely(tty_buffer_request_room(tty, bytes_in) < bytes_in)) {
 			if (tty->low_latency)
 				tty_flip_buffer_push(tty);
 			/*
-			 * If this failed then we will throw awa the bytes
-			 * but mst do so to clear interrupts.
+			 * If this failed then we will throw away the bytes
+			 * but must do so to clear interrupts.
 			 */
 		}
 
diff -puN drivers/serial/mux.c~tty-layer-buffering-revamp drivers/serial/mux.c
--- 25/drivers/serial/mux.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/mux.c	Wed Aug 31 12:50:58 2005
@@ -228,11 +228,6 @@ static void mux_read(struct uart_port *p
 		if (MUX_EOFIFO(data))
 			break;
 
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-			continue;
-
-		*tty->flip.char_buf_ptr = data & 0xffu;
-		*tty->flip.flag_buf_ptr = TTY_NORMAL;
 		port->icount.rx++;
 
 		if (MUX_BREAK(data)) {
@@ -244,9 +239,7 @@ static void mux_read(struct uart_port *p
 		if (uart_handle_sysrq_char(port, data & 0xffu, NULL))
 			continue;
 
-		tty->flip.flag_buf_ptr++;
-		tty->flip.char_buf_ptr++;
-		tty->flip.count++;
+		tty_insert_flip_char(tty, data & 0xFF, TTY_NORMAL);
 	}
 	
 	if (start_count != port->icount.rx) {
diff -puN drivers/serial/pmac_zilog.c~tty-layer-buffering-revamp drivers/serial/pmac_zilog.c
--- 25/drivers/serial/pmac_zilog.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/pmac_zilog.c	Wed Aug 31 12:50:58 2005
@@ -210,7 +210,7 @@ static struct tty_struct *pmz_receive_ch
 					    struct pt_regs *regs)
 {
 	struct tty_struct *tty = NULL;
-	unsigned char ch, r1, drop, error;
+	unsigned char ch, r1, drop, error, flag;
 	int loops = 0;
 
  retry:
@@ -246,20 +246,6 @@ static struct tty_struct *pmz_receive_ch
 		error = 0;
 		drop = 0;
 
-		if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
-			/* Have to drop the lock here */
-			pmz_debug("pmz: flip overflow\n");
-			spin_unlock(&uap->port.lock);
-			tty->flip.work.func((void *)tty);
-			spin_lock(&uap->port.lock);
-			if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-				drop = 1;
-			if (ZS_IS_ASLEEP(uap))
-				return NULL;
-			if (!ZS_IS_OPEN(uap))
-				goto retry;
-		}
-
 		r1 = read_zsreg(uap, R1);
 		ch = read_zsdata(uap);
 
@@ -295,8 +281,7 @@ static struct tty_struct *pmz_receive_ch
 		if (drop)
 			goto next_char;
 
-		*tty->flip.char_buf_ptr = ch;
-		*tty->flip.flag_buf_ptr = TTY_NORMAL;
+		flag = TTY_NORMAL;
 		uap->port.icount.rx++;
 
 		if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR | BRK_ABRT)) {
@@ -316,26 +301,19 @@ static struct tty_struct *pmz_receive_ch
 				uap->port.icount.overrun++;
 			r1 &= uap->port.read_status_mask;
 			if (r1 & BRK_ABRT)
-				*tty->flip.flag_buf_ptr = TTY_BREAK;
+				flag = TTY_BREAK;
 			else if (r1 & PAR_ERR)
-				*tty->flip.flag_buf_ptr = TTY_PARITY;
+				flag = TTY_PARITY;
 			else if (r1 & CRC_ERR)
-				*tty->flip.flag_buf_ptr = TTY_FRAME;
+				flag = TTY_FRAME;
 		}
 
 		if (uap->port.ignore_status_mask == 0xff ||
 		    (r1 & uap->port.ignore_status_mask) == 0) {
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
-		}
-		if ((r1 & Rx_OVR) &&
-		    tty->flip.count < TTY_FLIPBUF_SIZE) {
-			*tty->flip.flag_buf_ptr = TTY_OVERRUN;
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
+		    	tty_insert_flip_char(tty, ch, flag);
 		}
+		if (r1 & Rx_OVR)
+			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
 	next_char:
 		/* We can get stuck in an infinite loop getting char 0 when the
 		 * line is in a wrong HW state, we break that here.
diff -puN drivers/serial/pxa.c~tty-layer-buffering-revamp drivers/serial/pxa.c
--- 25/drivers/serial/pxa.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/pxa.c	Wed Aug 31 12:50:58 2005
@@ -107,14 +107,6 @@ receive_chars(struct uart_pxa_port *up, 
 	int max_count = 256;
 
 	do {
-		if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
-			if (tty->low_latency)
-				tty_flip_buffer_push(tty);
-			/*
-			 * If this failed then we will throw away the
-			 * bytes but must do so to clear interrupts
-			 */
-		}
 		ch = serial_in(up, UART_RX);
 		flag = TTY_NORMAL;
 		up->port.icount.rx++;
diff -puN drivers/serial/s3c2410.c~tty-layer-buffering-revamp drivers/serial/s3c2410.c
--- 25/drivers/serial/s3c2410.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/s3c2410.c	Wed Aug 31 12:50:58 2005
@@ -327,16 +327,6 @@ s3c24xx_serial_rx_chars(int irq, void *d
 		if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
 			break;
 
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
-			if (tty->low_latency)
-				tty_flip_buffer_push(tty);
-
-			/*
-			 * If this failed then we will throw away the
-			 * bytes but must do so to clear interrupts
-			 */
-		}
-
 		uerstat = rd_regl(port, S3C2410_UERSTAT);
 		ch = rd_regb(port, S3C2410_URXH);
 
diff -puN drivers/serial/sa1100.c~tty-layer-buffering-revamp drivers/serial/sa1100.c
--- 25/drivers/serial/sa1100.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/sa1100.c	Wed Aug 31 12:50:58 2005
@@ -204,8 +204,6 @@ sa1100_rx_chars(struct sa1100_port *spor
 	while (status & UTSR1_TO_SM(UTSR1_RNE)) {
 		ch = UART_GET_CHAR(sport);
 
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-			goto ignore_char;
 		sport->port.icount.rx++;
 
 		flg = TTY_NORMAL;
diff -puN drivers/serial/serial_lh7a40x.c~tty-layer-buffering-revamp drivers/serial/serial_lh7a40x.c
--- 25/drivers/serial/serial_lh7a40x.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/serial_lh7a40x.c	Wed Aug 31 12:50:58 2005
@@ -149,15 +149,6 @@ lh7a40xuart_rx_chars (struct uart_port* 
 	unsigned int data, flag;/* Received data and status */
 
 	while (!(UR (port, UART_R_STATUS) & nRxRdy) && --cbRxMax) {
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
-			if (tty->low_latency)
-				tty_flip_buffer_push(tty);
-			/*
-			 * If this failed then we will throw away the
-			 * bytes but must do so to clear interrupts
-			 */
-		}
-
 		data = UR (port, UART_R_DATA);
 		flag = TTY_NORMAL;
 		++port->icount.rx;
diff -puN drivers/serial/serial_txx9.c~tty-layer-buffering-revamp drivers/serial/serial_txx9.c
--- 25/drivers/serial/serial_txx9.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/serial_txx9.c	Wed Aug 31 12:50:58 2005
@@ -301,14 +301,6 @@ receive_chars(struct uart_txx9_port *up,
 	char flag;
 
 	do {
-		/* The following is not allowed by the tty layer and
-		   unsafe. It should be fixed ASAP */
-		if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
-			if(tty->low_latency)
-				tty_flip_buffer_push(tty);
-			/* If this failed then we will throw away the
-			   bytes but must do so to clear interrupts */
-		}
 		ch = sio_in(up, TXX9_SIRFIFO);
 		flag = TTY_NORMAL;
 		up->port.icount.rx++;
diff -puN drivers/serial/sh-sci.c~tty-layer-buffering-revamp drivers/serial/sh-sci.c
--- 25/drivers/serial/sh-sci.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/sh-sci.c	Wed Aug 31 12:50:58 2005
@@ -482,6 +482,7 @@ static inline void sci_receive_chars(str
 	struct tty_struct *tty = port->info->tty;
 	int i, count, copied = 0;
 	unsigned short status;
+	unsigned char flag;
 
 	status = sci_in(port, SCxSR);
 	if (!(status & SCxSR_RDxF(port)))
@@ -499,8 +500,7 @@ static inline void sci_receive_chars(str
 #endif
 
 		/* Don't copy more bytes than there is room for in the buffer */
-		if (tty->flip.count + count > TTY_FLIPBUF_SIZE)
-			count = TTY_FLIPBUF_SIZE - tty->flip.count;
+		count = tty_buffer_request_room(tty, count);
 
 		/* If for any reason we can't copy more data, we're done! */
 		if (count == 0)
@@ -512,8 +512,7 @@ static inline void sci_receive_chars(str
 			    || uart_handle_sysrq_char(port, c, regs)) {
 				count = 0;
 			} else {
-			    tty->flip.char_buf_ptr[0] = c;
-			    tty->flip.flag_buf_ptr[0] = TTY_NORMAL;
+			    tty_insert_flip_char(tty, c, TTY_NORMAL);
 			}
 		} else {
 			for (i=0; i<count; i++) {
@@ -542,26 +541,21 @@ static inline void sci_receive_chars(str
 				}
 
 				/* Store data and status */
-				tty->flip.char_buf_ptr[i] = c;
 				if (status&SCxSR_FER(port)) {
-					tty->flip.flag_buf_ptr[i] = TTY_FRAME;
+					flag = TTY_FRAME;
 					pr_debug("sci: frame error\n");
 				} else if (status&SCxSR_PER(port)) {
-					tty->flip.flag_buf_ptr[i] = TTY_PARITY;
+					flag = TTY_PARITY;
 					pr_debug("sci: parity error\n");
-				} else {
-					tty->flip.flag_buf_ptr[i] = TTY_NORMAL;
-				}
+				} else 
+					flag = TTY_NORMAL;
+				tty_insert_flip_char(tty, c, flag);
 			}
 		}
 
 		sci_in(port, SCxSR); /* dummy read */
 		sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
 
-		/* Update the kernel buffer end */
-		tty->flip.count += count;
-		tty->flip.char_buf_ptr += count;
-		tty->flip.flag_buf_ptr += count;
 		copied += count;
 		port->icount.rx += count;
 	}
@@ -608,48 +602,45 @@ static inline int sci_handle_errors(stru
 	unsigned short status = sci_in(port, SCxSR);
 	struct tty_struct *tty = port->info->tty;
 
-	if (status&SCxSR_ORER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
+	if (status&SCxSR_ORER(port)) {
 		/* overrun error */
-		copied++;
-		*tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
+		if(tty_insert_flip_char(tty, 0, TTY_OVERRUN))
+			copied++;
 		pr_debug("sci: overrun error\n");
 	}
 
-	if (status&SCxSR_FER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
+	if (status&SCxSR_FER(port)) {
 		if (sci_rxd_in(port) == 0) {
 			/* Notify of BREAK */
 			struct sci_port * sci_port = (struct sci_port *)port;
-                       if(!sci_port->break_flag) {
-	                        sci_port->break_flag = 1;
-                               sci_schedule_break_timer((struct sci_port *)port);
+			if(!sci_port->break_flag) {
+	                	sci_port->break_flag = 1;
+	                	sci_schedule_break_timer((struct sci_port *)port);
 				/* Do sysrq handling. */
-				if(uart_handle_break(port)) {
+				if(uart_handle_break(port))
 					return 0;
-				}
 			        pr_debug("sci: BREAK detected\n");
-			        copied++;
-			        *tty->flip.flag_buf_ptr++ = TTY_BREAK;
+			        if(tty_insert_flip_char(tty, 0, TTY_BREAK))
+				        copied++;
                        }
 		}
 		else {
 			/* frame error */
-			copied++;
-			*tty->flip.flag_buf_ptr++ = TTY_FRAME;
+			if(tty_insert_flip_char(tty, 0, TTY_FRAME))
+				copied++;
 			pr_debug("sci: frame error\n");
 		}
 	}
 
-	if (status&SCxSR_PER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
+	if (status&SCxSR_PER(port)) {
+		if(tty_insert_flip_char(tty, 0, TTY_PARITY))
+			copied++;
 		/* parity error */
-		copied++;
-		*tty->flip.flag_buf_ptr++ = TTY_PARITY;
 		pr_debug("sci: parity error\n");
 	}
 
-	if (copied) {
-		tty->flip.count += copied;
+	if (copied)
 		tty_flip_buffer_push(tty);
-	}
 
 	return copied;
 }
@@ -661,15 +652,14 @@ static inline int sci_handle_breaks(stru
 	struct tty_struct *tty = port->info->tty;
 	struct sci_port *s = &sci_ports[port->line];
 
-	if (!s->break_flag && status & SCxSR_BRK(port) &&
-	    tty->flip.count < TTY_FLIPBUF_SIZE) {
+	if (!s->break_flag && status & SCxSR_BRK(port))
 #if defined(CONFIG_CPU_SH3)
 		/* Debounce break */
 		s->break_flag = 1;
 #endif
 		/* Notify of BREAK */
-		copied++;
-		*tty->flip.flag_buf_ptr++ = TTY_BREAK;
+		if(tty_insert_flip_char(tty, 0, TTY_BREAK))
+			copied++;
 		pr_debug("sci: BREAK detected\n");
 	}
 
@@ -677,19 +667,15 @@ static inline int sci_handle_breaks(stru
 	/* XXX: Handle SCIF overrun error */
 	if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
 		sci_out(port, SCLSR, 0);
-		if(tty->flip.count<TTY_FLIPBUF_SIZE) {
+		if(tty_insert_flip_char(tty, 0, TTY_OVERRUN)) {
 			copied++;
-			*tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
 			pr_debug("sci: overrun error\n");
 		}
 	}
 #endif
 
-	if (copied) {
-		tty->flip.count += copied;
+	if (copied)
 		tty_flip_buffer_push(tty);
-	}
-
 	return copied;
 }
 
@@ -732,12 +718,9 @@ static irqreturn_t sci_er_interrupt(int 
 			struct tty_struct *tty = port->info->tty;
 
 			sci_out(port, SCLSR, 0);
-			if(tty->flip.count<TTY_FLIPBUF_SIZE) {
-				*tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
-				tty->flip.count++;
-				tty_flip_buffer_push(tty);
-				pr_debug("scif: overrun error\n");
-			}
+			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
+			tty_flip_buffer_push(tty);
+			pr_debug("scif: overrun error\n");
 		}
 #endif
 		sci_rx_interrupt(irq, ptr, regs);
diff -puN drivers/serial/sn_console.c~tty-layer-buffering-revamp drivers/serial/sn_console.c
--- 25/drivers/serial/sn_console.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/sn_console.c	Wed Aug 31 12:50:58 2005
@@ -521,11 +521,7 @@ sn_receive_chars(struct sn_cons_port *po
 
 		/* record the character to pass up to the tty layer */
 		if (tty) {
-			*tty->flip.char_buf_ptr = ch;
-			*tty->flip.flag_buf_ptr = TTY_NORMAL;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
-			if (tty->flip.count == TTY_FLIPBUF_SIZE)
+			if(tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
 				break;
 		}
 		port->sc_port.icount.rx++;
diff -puN drivers/serial/sunsab.c~tty-layer-buffering-revamp drivers/serial/sunsab.c
--- 25/drivers/serial/sunsab.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/sunsab.c	Wed Aug 31 12:50:58 2005
@@ -159,21 +159,14 @@ receive_chars(struct uart_sunsab_port *u
 		saw_console_brk = 1;
 
 	for (i = 0; i < count; i++) {
-		unsigned char ch = buf[i];
+		unsigned char ch = buf[i], flag;
 
 		if (tty == NULL) {
 			uart_handle_sysrq_char(&up->port, ch, regs);
 			continue;
 		}
 
-		if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
-			tty->flip.work.func((void *)tty);
-			if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-				return tty; // if TTY_DONT_FLIP is set
-		}
-
-		*tty->flip.char_buf_ptr = ch;
-		*tty->flip.flag_buf_ptr = TTY_NORMAL;
+		flag = TTY_NORMAL;
 		up->port.icount.rx++;
 
 		if (unlikely(stat->sreg.isr0 & (SAB82532_ISR0_PERR |
@@ -209,34 +202,21 @@ receive_chars(struct uart_sunsab_port *u
 			stat->sreg.isr1 &= ((up->port.read_status_mask >> 8) & 0xff);
 
 			if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
-				*tty->flip.flag_buf_ptr = TTY_BREAK;
+				flag = TTY_BREAK;
 			} else if (stat->sreg.isr0 & SAB82532_ISR0_PERR)
-				*tty->flip.flag_buf_ptr = TTY_PARITY;
+				flag = TTY_PARITY;
 			else if (stat->sreg.isr0 & SAB82532_ISR0_FERR)
-				*tty->flip.flag_buf_ptr = TTY_FRAME;
+				flag = TTY_FRAME;
 		}
 
 		if (uart_handle_sysrq_char(&up->port, ch, regs))
 			continue;
 
 		if ((stat->sreg.isr0 & (up->port.ignore_status_mask & 0xff)) == 0 &&
-		    (stat->sreg.isr1 & ((up->port.ignore_status_mask >> 8) & 0xff)) == 0){
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
-		}
-		if ((stat->sreg.isr0 & SAB82532_ISR0_RFO) &&
-		    tty->flip.count < TTY_FLIPBUF_SIZE) {
-			/*
-			 * Overrun is special, since it's reported
-			 * immediately, and doesn't affect the current
-			 * character.
-			 */
-			*tty->flip.flag_buf_ptr = TTY_OVERRUN;
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
-		}
+		    (stat->sreg.isr1 & ((up->port.ignore_status_mask >> 8) & 0xff)) == 0)
+			tty_insert_flip_char(tty, ch, flag);
+		if (stat->sreg.isr0 & SAB82532_ISR0_RFO) {
+			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
 	}
 
 	if (saw_console_brk)
diff -puN drivers/serial/sunsu.c~tty-layer-buffering-revamp drivers/serial/sunsu.c
--- 25/drivers/serial/sunsu.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/sunsu.c	Wed Aug 31 12:50:58 2005
@@ -313,19 +313,13 @@ static _INLINE_ struct tty_struct *
 receive_chars(struct uart_sunsu_port *up, unsigned char *status, struct pt_regs *regs)
 {
 	struct tty_struct *tty = up->port.info->tty;
-	unsigned char ch;
+	unsigned char ch, flag;
 	int max_count = 256;
 	int saw_console_brk = 0;
 
 	do {
-		if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
-			tty->flip.work.func((void *)tty);
-			if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-				return tty; // if TTY_DONT_FLIP is set
-		}
 		ch = serial_inp(up, UART_RX);
-		*tty->flip.char_buf_ptr = ch;
-		*tty->flip.flag_buf_ptr = TTY_NORMAL;
+		flag = TTY_NORMAL;
 		up->port.icount.rx++;
 
 		if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
@@ -367,31 +361,23 @@ receive_chars(struct uart_sunsu_port *up
 			}
 
 			if (*status & UART_LSR_BI) {
-				*tty->flip.flag_buf_ptr = TTY_BREAK;
+				flag = TTY_BREAK;
 			} else if (*status & UART_LSR_PE)
-				*tty->flip.flag_buf_ptr = TTY_PARITY;
+				flag = TTY_PARITY;
 			else if (*status & UART_LSR_FE)
-				*tty->flip.flag_buf_ptr = TTY_FRAME;
+				flag = TTY_FRAME;
 		}
 		if (uart_handle_sysrq_char(&up->port, ch, regs))
 			goto ignore_char;
-		if ((*status & up->port.ignore_status_mask) == 0) {
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
-		}
-		if ((*status & UART_LSR_OE) &&
-		    tty->flip.count < TTY_FLIPBUF_SIZE) {
+		if ((*status & up->port.ignore_status_mask) == 0)
+			tty_insert_flip_char(tty, ch, flag);
+		if (*status & UART_LSR_OE)
 			/*
 			 * Overrun is special, since it's reported
 			 * immediately, and doesn't affect the current
 			 * character.
 			 */
-			*tty->flip.flag_buf_ptr = TTY_OVERRUN;
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
-		}
+			 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
 	ignore_char:
 		*status = serial_inp(up, UART_LSR);
 	} while ((*status & UART_LSR_DR) && (max_count-- > 0));
diff -puN drivers/serial/sunzilog.c~tty-layer-buffering-revamp drivers/serial/sunzilog.c
--- 25/drivers/serial/sunzilog.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/sunzilog.c	Wed Aug 31 12:50:58 2005
@@ -319,7 +319,7 @@ sunzilog_receive_chars(struct uart_sunzi
 		       struct pt_regs *regs)
 {
 	struct tty_struct *tty;
-	unsigned char ch, r1;
+	unsigned char ch, r1, flag;
 
 	tty = NULL;
 	if (up->port.info != NULL &&		/* Unopened serial console */
@@ -362,19 +362,8 @@ sunzilog_receive_chars(struct uart_sunzi
 			continue;
 		}
 
-		if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
-			tty->flip.work.func((void *)tty);
-			/*
-			 * The 8250 bails out of the loop here,
-			 * but we need to read everything, or die.
-			 */
-			if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-				continue;
-		}
-
 		/* A real serial line, record the character and status.  */
-		*tty->flip.char_buf_ptr = ch;
-		*tty->flip.flag_buf_ptr = TTY_NORMAL;
+		flag = TTY_NORMAL;
 		up->port.icount.rx++;
 		if (r1 & (BRK_ABRT | PAR_ERR | Rx_OVR | CRC_ERR)) {
 			if (r1 & BRK_ABRT) {
@@ -391,28 +380,21 @@ sunzilog_receive_chars(struct uart_sunzi
 				up->port.icount.overrun++;
 			r1 &= up->port.read_status_mask;
 			if (r1 & BRK_ABRT)
-				*tty->flip.flag_buf_ptr = TTY_BREAK;
+				flag = TTY_BREAK;
 			else if (r1 & PAR_ERR)
-				*tty->flip.flag_buf_ptr = TTY_PARITY;
+				flag = TTY_PARITY;
 			else if (r1 & CRC_ERR)
-				*tty->flip.flag_buf_ptr = TTY_FRAME;
+				flag = TTY_FRAME;
 		}
 		if (uart_handle_sysrq_char(&up->port, ch, regs))
 			continue;
 
 		if (up->port.ignore_status_mask == 0xff ||
 		    (r1 & up->port.ignore_status_mask) == 0) {
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
-		}
-		if ((r1 & Rx_OVR) &&
-		    tty->flip.count < TTY_FLIPBUF_SIZE) {
-			*tty->flip.flag_buf_ptr = TTY_OVERRUN;
-			tty->flip.flag_buf_ptr++;
-			tty->flip.char_buf_ptr++;
-			tty->flip.count++;
+		    	tty_insert_flip_char(tty, ch, flag);
 		}
+		if (r1 & Rx_OVR)
+			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
 	}
 
 	return tty;
diff -puN drivers/serial/uart00.c~tty-layer-buffering-revamp drivers/serial/uart00.c
--- 25/drivers/serial/uart00.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/uart00.c	Wed Aug 31 12:50:58 2005
@@ -118,9 +118,6 @@ uart00_rx_chars(struct uart_port *port, 
 		ch = UART_GET_CHAR(port);
 		port->icount.rx++;
 
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-			goto ignore_char;
-
 		flg = TTY_NORMAL;
 
 		/*
diff -puN drivers/serial/vr41xx_siu.c~tty-layer-buffering-revamp drivers/serial/vr41xx_siu.c
--- 25/drivers/serial/vr41xx_siu.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/serial/vr41xx_siu.c	Wed Aug 31 12:50:58 2005
@@ -371,11 +371,6 @@ static inline void receive_chars(struct 
 	lsr = *status;
 
 	do {
-		if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
-			if (tty->low_latency)
-				tty_flip_buffer_push(tty);
-		}
-
 		ch = siu_read(port, UART_RX);
 		port->icount.rx++;
 		flag = TTY_NORMAL;
diff -puN drivers/usb/class/cdc-acm.c~tty-layer-buffering-revamp drivers/usb/class/cdc-acm.c
--- 25/drivers/usb/class/cdc-acm.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/class/cdc-acm.c	Wed Aug 31 12:50:58 2005
@@ -307,14 +307,9 @@ static void acm_rx_tasklet(unsigned long
 	dbg("Entering acm_rx_tasklet");
 
 	if (urb->actual_length > 0 && !acm->throttle)  {
-		for (i = 0; i < urb->actual_length && !acm->throttle; i++) {
-			/* if we insert more than TTY_FLIPBUF_SIZE characters,
-			 * we drop them. */
-			if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
-				tty_flip_buffer_push(tty);
-			}
-			tty_insert_flip_char(tty, data[i], 0);
-		}
+		tty_buffer_request_room(tty, urb->actual_length);
+		if(!acm->throttle)
+			tty_insert_flip_string(tty, data, urb->actual_length);
 		dbg("Handed %d bytes to tty layer", i+1);
 		tty_flip_buffer_push(tty);
 	}
diff -puN drivers/usb/gadget/serial.c~tty-layer-buffering-revamp drivers/usb/gadget/serial.c
--- 25/drivers/usb/gadget/serial.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/gadget/serial.c	Wed Aug 31 12:50:58 2005
@@ -1269,6 +1269,7 @@ static int gs_recv_packet(struct gs_dev 
 	unsigned int len;
 	struct gs_port *port;
 	int ret;
+	struct tty_struct *tty;
 
 	/* TEMPORARY -- only port 0 is supported right now */
 	port = dev->dev_port[0];
@@ -1288,7 +1289,10 @@ static int gs_recv_packet(struct gs_dev 
 		goto exit;
 	}
 
-	if (port->port_tty == NULL) {
+
+	tty = port->port_tty;
+	
+	if (tty == NULL) {
 		printk(KERN_ERR "gs_recv_packet: port=%d, NULL tty pointer\n",
 			port->port_num);
 		ret = -EIO;
@@ -1302,20 +1306,13 @@ static int gs_recv_packet(struct gs_dev 
 		goto exit;
 	}
 
-	len = (unsigned int)(TTY_FLIPBUF_SIZE - port->port_tty->flip.count);
-	if (len < size)
-		size = len;
-
-	if (size > 0) {
-		memcpy(port->port_tty->flip.char_buf_ptr, packet, size);
-		port->port_tty->flip.char_buf_ptr += size;
-		port->port_tty->flip.count += size;
+	len = tty_buffer_request_room(tty, size);
+	if (len > 0) {
+		tty_insert_flip_string(tty, packet, len);
 		tty_flip_buffer_push(port->port_tty);
 		wake_up_interruptible(&port->port_tty->read_wait);
 	}
-
 	ret = 0;
-
 exit:
 	spin_unlock(&port->port_lock);
 	return ret;
diff -puN drivers/usb/serial/cyberjack.c~tty-layer-buffering-revamp drivers/usb/serial/cyberjack.c
--- 25/drivers/usb/serial/cyberjack.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/cyberjack.c	Wed Aug 31 12:50:58 2005
@@ -362,7 +362,6 @@ static void cyberjack_read_bulk_callback
 	struct tty_struct *tty;
 	unsigned char *data = urb->transfer_buffer;
 	short todo;
-	int i;
 	int result;
 
 	dbg("%s - port %d", __FUNCTION__, port->number);
@@ -379,14 +378,8 @@ static void cyberjack_read_bulk_callback
 		return;
 	}
 	if (urb->actual_length) {
-		for (i = 0; i < urb->actual_length ; ++i) {
-			/* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
-			if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
-				tty_flip_buffer_push(tty);
-			}
-			/* this doesn't actually push the data through unless tty->low_latency is set */
-			tty_insert_flip_char(tty, data[i], 0);
-		}
+		tty_buffer_request_room(tty, urb->actual_length);
+		tty_insert_flip_string(tty, data, urb->actual_length);
 	  	tty_flip_buffer_push(tty);
 	}
 
diff -puN drivers/usb/serial/cypress_m8.c~tty-layer-buffering-revamp drivers/usb/serial/cypress_m8.c
--- 25/drivers/usb/serial/cypress_m8.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/cypress_m8.c	Wed Aug 31 12:50:58 2005
@@ -1259,12 +1259,10 @@ static void cypress_read_int_callback(st
 
 	/* process read if there is data other than line status */
 	if (tty && (bytes > i)) {
+		bytes = tty_buffer_request_room(tty, bytes);
 		for (; i < bytes ; ++i) {
 			dbg("pushing byte number %d - %d - %c", i, data[i],
 					data[i]);
-			if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
-				tty_flip_buffer_push(tty);
-			}
 			tty_insert_flip_char(tty, data[i], tty_flag);
 		}
 		tty_flip_buffer_push(port->tty);
diff -puN drivers/usb/serial/digi_acceleport.c~tty-layer-buffering-revamp drivers/usb/serial/digi_acceleport.c
--- 25/drivers/usb/serial/digi_acceleport.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/digi_acceleport.c	Wed Aug 31 12:50:58 2005
@@ -942,13 +942,10 @@ dbg( "digi_rx_unthrottle: TOP: port=%d",
 	spin_lock_irqsave( &priv->dp_port_lock, flags );
 
 	/* send any buffered chars from throttle time on to tty subsystem */
-	len = min(priv->dp_in_buf_len, TTY_FLIPBUF_SIZE - tty->flip.count );
+	
+	len = tty_buffer_request_room(tty, priv->dp_in_buf_len);
 	if( len > 0 ) {
-		memcpy( tty->flip.char_buf_ptr, priv->dp_in_buf, len );
-		memcpy( tty->flip.flag_buf_ptr, priv->dp_in_flag_buf, len );
-		tty->flip.char_buf_ptr += len;
-		tty->flip.flag_buf_ptr += len;
-		tty->flip.count += len;
+		tty_insert_flip_string_flags(tty, priv->dp_in_buf, priv->dp_in_flag_buf, len);
 		tty_flip_buffer_push( tty );
 	}
 
@@ -1823,7 +1820,8 @@ static int digi_read_inb_callback( struc
 	int status = ((unsigned char *)urb->transfer_buffer)[2];
 	unsigned char *data = ((unsigned char *)urb->transfer_buffer)+3;
 	int flag,throttled;
-
+	int i;
+	
 	/* do not process callbacks on closed ports */
 	/* but do continue the read chain */
 	if( port->open_count == 0 )
@@ -1881,20 +1879,18 @@ static int digi_read_inb_callback( struc
 			}
 
 		} else {
-
-			len = min( len, TTY_FLIPBUF_SIZE - tty->flip.count );
-
+			len = tty_buffer_request_room(tty, len);
 			if( len > 0 ) {
-				memcpy( tty->flip.char_buf_ptr, data, len );
-				memset( tty->flip.flag_buf_ptr, flag, len );
-				tty->flip.char_buf_ptr += len;
-				tty->flip.flag_buf_ptr += len;
-				tty->flip.count += len;
+				/* Hot path */
+				if(flag == TTY_NORMAL)
+					tty_insert_flip_string(tty, data, len);
+				else {
+					for(i = 0; i < len; i++)
+						tty_insert_flip_char(tty, data[i], flag);
+				}
 				tty_flip_buffer_push( tty );
 			}
-
 		}
-
 	}
 
 	spin_unlock( &priv->dp_port_lock );
diff -puN drivers/usb/serial/empeg.c~tty-layer-buffering-revamp drivers/usb/serial/empeg.c
--- 25/drivers/usb/serial/empeg.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/empeg.c	Wed Aug 31 12:50:58 2005
@@ -342,7 +342,6 @@ static void empeg_read_bulk_callback (st
 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
 	struct tty_struct *tty;
 	unsigned char *data = urb->transfer_buffer;
-	int i;
 	int result;
 
 	dbg("%s - port %d", __FUNCTION__, port->number);
@@ -357,19 +356,8 @@ static void empeg_read_bulk_callback (st
 	tty = port->tty;
 
 	if (urb->actual_length) {
-		for (i = 0; i < urb->actual_length ; ++i) {
-			/* gb - 2000/11/13
-			 * If we insert too many characters we'll overflow the buffer.
-			 * This means we'll lose bytes - Decidedly bad.
-			 */
-			if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
-				tty_flip_buffer_push(tty);
-				}
-			tty_insert_flip_char(tty, data[i], 0);
-		}
-		/* gb - 2000/11/13
-		 * Goes straight through instead of scheduling - if tty->low_latency is set.
-		 */
+		tty_buffer_request_room(tty, urb->actual_length);
+		tty_insert_flip_string(tty, data, urb->actual_length);
 		tty_flip_buffer_push(tty);
 		bytes_in += urb->actual_length;
 	}
diff -puN drivers/usb/serial/ftdi_sio.c~tty-layer-buffering-revamp drivers/usb/serial/ftdi_sio.c
--- 25/drivers/usb/serial/ftdi_sio.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/ftdi_sio.c	Wed Aug 31 12:50:58 2005
@@ -1598,24 +1598,11 @@ static void ftdi_process_read (void *par
 			length = 0;
 		}
 
-		/* have to make sure we don't overflow the buffer
-		   with tty_insert_flip_char's */
-		if (tty->flip.count+length > TTY_FLIPBUF_SIZE) {
-			tty_flip_buffer_push(tty);
-			need_flip = 0;
-
-			if (tty->flip.count != 0) {
-				/* flip didn't work, this happens when ftdi_process_read() is
-				 * called from ftdi_unthrottle, because TTY_DONT_FLIP is set */
-				dbg("%s - flip buffer push failed", __FUNCTION__);
-				break;
-			}
-		}
 		if (priv->rx_flags & THROTTLED) {
 			dbg("%s - throttled", __FUNCTION__);
 			break;
 		}
-		if (tty->ldisc.receive_room(tty)-tty->flip.count < length) {
+		if (tty_buffer_request_room(tty, length) < length) {
 			/* break out & wait for throttling/unthrottling to happen */
 			dbg("%s - receive room low", __FUNCTION__);
 			break;
diff -puN drivers/usb/serial/garmin_gps.c~tty-layer-buffering-revamp drivers/usb/serial/garmin_gps.c
--- 25/drivers/usb/serial/garmin_gps.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/garmin_gps.c	Wed Aug 31 12:50:58 2005
@@ -275,23 +275,14 @@ static void send_to_tty(struct usb_seria
                         char *data, unsigned int actual_length)
 {
 	struct tty_struct *tty = port->tty;
-	int i;
 
 	if (tty && actual_length) {
 
 		usb_serial_debug_data(debug, &port->dev, 
 					__FUNCTION__, actual_length, data);
 
-		for (i = 0; i < actual_length ; ++i) {
-			/* if we insert more than TTY_FLIPBUF_SIZE characters,
-			   we drop them. */
-			if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
-				tty_flip_buffer_push(tty);
-			}
-			/* this doesn't actually push the data through unless
-			   tty->low_latency is set */
-			tty_insert_flip_char(tty, data[i], 0);
-		}
+		tty_buffer_request_room(tty, actual_length);
+		tty_insert_flip_string(tty, data, actual_length);
 		tty_flip_buffer_push(tty);
 	}
 }
diff -puN drivers/usb/serial/generic.c~tty-layer-buffering-revamp drivers/usb/serial/generic.c
--- 25/drivers/usb/serial/generic.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/generic.c	Wed Aug 31 12:50:58 2005
@@ -253,7 +253,6 @@ void usb_serial_generic_read_bulk_callba
 	struct usb_serial *serial = port->serial;
 	struct tty_struct *tty;
 	unsigned char *data = urb->transfer_buffer;
-	int i;
 	int result;
 
 	dbg("%s - port %d", __FUNCTION__, port->number);
@@ -267,14 +266,8 @@ void usb_serial_generic_read_bulk_callba
 
 	tty = port->tty;
 	if (tty && urb->actual_length) {
-		for (i = 0; i < urb->actual_length ; ++i) {
-			/* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
-			if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
-				tty_flip_buffer_push(tty);
-			}
-			/* this doesn't actually push the data through unless tty->low_latency is set */
-			tty_insert_flip_char(tty, data[i], 0);
-		}
+		tty_buffer_request_room(tty, urb->actual_length);
+		tty_insert_flip_string(tty, data, urb->actual_length);
 	  	tty_flip_buffer_push(tty);
 	}
 
diff -puN drivers/usb/serial/io_edgeport.c~tty-layer-buffering-revamp drivers/usb/serial/io_edgeport.c
--- 25/drivers/usb/serial/io_edgeport.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/io_edgeport.c	Wed Aug 31 12:50:58 2005
@@ -2184,20 +2184,14 @@ static void edge_tty_recv(struct device 
 	int cnt;
 
 	do {
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
-			tty_flip_buffer_push(tty);
-			if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
-				dev_err(dev, "%s - dropping data, %d bytes lost\n",
-					__FUNCTION__, length);
-				return;
-			}
+		cnt = tty_buffer_request_room(tty, length);
+		if (cnt < length) {
+			dev_err(dev, "%s - dropping data, %d bytes lost\n",
+					__FUNCTION__, length - cnt);
+			if(cnt == 0)
+				break;
 		}
-		cnt = min(length, TTY_FLIPBUF_SIZE - tty->flip.count);
-		memcpy(tty->flip.char_buf_ptr, data, cnt);
-		memset(tty->flip.flag_buf_ptr, 0, cnt);
-		tty->flip.char_buf_ptr += cnt;
-		tty->flip.flag_buf_ptr += cnt;
-		tty->flip.count += cnt;
+		tty_insert_flip_string(tty, data, cnt);
 		data += cnt;
 		length -= cnt;
 	} while (length > 0);
diff -puN drivers/usb/serial/io_ti.c~tty-layer-buffering-revamp drivers/usb/serial/io_ti.c
--- 25/drivers/usb/serial/io_ti.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/io_ti.c	Wed Aug 31 12:50:58 2005
@@ -1865,20 +1865,14 @@ static void edge_tty_recv(struct device 
 	int cnt;
 
 	do {
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
-			tty_flip_buffer_push(tty);
-			if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
-				dev_err(dev, "%s - dropping data, %d bytes lost\n",
-					__FUNCTION__, length);
-				return;
-			}
+		cnt = tty_buffer_request_room(tty, length);
+		if (cnt < length) {
+			dev_err(dev, "%s - dropping data, %d bytes lost\n",
+				__FUNCTION__, length - cnt);
+			if(cnt == 0)
+				break;
 		}
-		cnt = min(length, TTY_FLIPBUF_SIZE - tty->flip.count);
-		memcpy(tty->flip.char_buf_ptr, data, cnt);
-		memset(tty->flip.flag_buf_ptr, 0, cnt);
-		tty->flip.char_buf_ptr += cnt;
-		tty->flip.flag_buf_ptr += cnt;
-		tty->flip.count += cnt;
+		tty_insert_flip_string(tty, data, cnt);
 		data += cnt;
 		length -= cnt;
 	} while (length > 0);
diff -puN drivers/usb/serial/ipaq.c~tty-layer-buffering-revamp drivers/usb/serial/ipaq.c
--- 25/drivers/usb/serial/ipaq.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/ipaq.c	Wed Aug 31 12:50:58 2005
@@ -704,7 +704,7 @@ static void ipaq_read_bulk_callback(stru
 	struct usb_serial_port	*port = (struct usb_serial_port *)urb->context;
 	struct tty_struct	*tty;
 	unsigned char		*data = urb->transfer_buffer;
-	int			i, result;
+	int			result;
 
 	dbg("%s - port %d", __FUNCTION__, port->number);
 
@@ -717,14 +717,8 @@ static void ipaq_read_bulk_callback(stru
 
 	tty = port->tty;
 	if (tty && urb->actual_length) {
-		for (i = 0; i < urb->actual_length ; ++i) {
-			/* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
-			if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
-				tty_flip_buffer_push(tty);
-			}
-			/* this doesn't actually push the data through unless tty->low_latency is set */
-			tty_insert_flip_char(tty, data[i], 0);
-		}
+		tty_buffer_request_room(tty, urb->actual_length);
+		tty_insert_flip_string(tty, data, urb->actual_length);
 		tty_flip_buffer_push(tty);
 		bytes_in += urb->actual_length;
 	}
diff -puN drivers/usb/serial/ipw.c~tty-layer-buffering-revamp drivers/usb/serial/ipw.c
--- 25/drivers/usb/serial/ipw.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/ipw.c	Wed Aug 31 12:50:58 2005
@@ -167,7 +167,6 @@ static void ipw_read_bulk_callback(struc
 	struct usb_serial_port *port = urb->context;
 	unsigned char *data = urb->transfer_buffer;
 	struct tty_struct *tty;
-	int i;
 	int result;
 
 	dbg("%s - port %d", __FUNCTION__, port->number);
@@ -181,14 +180,8 @@ static void ipw_read_bulk_callback(struc
 
 	tty = port->tty;
 	if (tty && urb->actual_length) {
-		for (i = 0; i < urb->actual_length ; ++i) {
-			/* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
-			if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
-				tty_flip_buffer_push(tty);
-			}
-			/* this doesn't actually push the data through unless tty->low_latency is set */
-			tty_insert_flip_char(tty, data[i], 0);
-		}
+		tty_buffer_request_room(tty, urb->actual_length);
+		tty_insert_flip_string(tty, data, urb->actual_length);
 		tty_flip_buffer_push(tty);
 	}
 
diff -puN drivers/usb/serial/kl5kusb105.c~tty-layer-buffering-revamp drivers/usb/serial/kl5kusb105.c
--- 25/drivers/usb/serial/kl5kusb105.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/kl5kusb105.c	Wed Aug 31 12:50:58 2005
@@ -646,7 +646,6 @@ static void klsi_105_read_bulk_callback 
 		usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
 				      urb->actual_length, data);
 	} else {
-		int i;
 		int bytes_sent = ((__u8 *) data)[0] +
 				 ((unsigned int) ((__u8 *) data)[1] << 8);
 		tty = port->tty;
@@ -667,16 +666,8 @@ static void klsi_105_read_bulk_callback 
 			bytes_sent = urb->actual_length - 2;
 		}
 
-		for (i = 2; i < 2+bytes_sent; i++) {
-			/* if we insert more than TTY_FLIPBUF_SIZE characters,
-			 * we drop them. */
-			if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
-				tty_flip_buffer_push(tty);
-			}
-			/* this doesn't actually push the data through unless 
-			 * tty->low_latency is set */
-			tty_insert_flip_char(tty, ((__u8*) data)[i], 0);
-		}
+		tty_buffer_request_room(tty, bytes_sent);
+		tty_insert_flip_string(tty, data + 2, bytes_sent);
 		tty_flip_buffer_push(tty);
 
 		/* again lockless, but debug info only */
diff -puN drivers/usb/serial/kobil_sct.c~tty-layer-buffering-revamp drivers/usb/serial/kobil_sct.c
--- 25/drivers/usb/serial/kobil_sct.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/kobil_sct.c	Wed Aug 31 12:50:58 2005
@@ -362,7 +362,6 @@ static void kobil_close (struct usb_seri
 
 static void kobil_read_int_callback( struct urb *purb, struct pt_regs *regs)
 {
-	int i;
 	int result;
 	struct usb_serial_port *port = (struct usb_serial_port *) purb->context;
 	struct tty_struct *tty;
@@ -394,14 +393,8 @@ static void kobil_read_int_callback( str
 		*/
 		// END DEBUG
 
-		for (i = 0; i < purb->actual_length; ++i) {
-			// if we insert more than TTY_FLIPBUF_SIZE characters, we drop them.
-			if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
-				tty_flip_buffer_push(tty);
-			}
-			// this doesn't actually push the data through unless tty->low_latency is set
-			tty_insert_flip_char(tty, data[i], 0);
-		}
+		tty_buffer_request_room(tty, purb->actual_length);
+		tty_insert_flip_string(tty, data, purb->actual_length);
 		tty_flip_buffer_push(tty);
 	}
 
diff -puN drivers/usb/serial/option.c~tty-layer-buffering-revamp drivers/usb/serial/option.c
--- 25/drivers/usb/serial/option.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/option.c	Wed Aug 31 12:50:58 2005
@@ -310,7 +310,7 @@ static int option_write(struct usb_seria
 
 static void option_indat_callback(struct urb *urb, struct pt_regs *regs)
 {
-	int i, err;
+	int err;
 	int endpoint;
 	struct usb_serial_port *port;
 	struct tty_struct *tty;
@@ -327,11 +327,8 @@ static void option_indat_callback(struct
 	} else {
 		tty = port->tty;
 		if (urb->actual_length) {
-			for (i = 0; i < urb->actual_length ; ++i) {
-				if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-					tty_flip_buffer_push(tty);
-				tty_insert_flip_char(tty, data[i], 0);
-			}
+			tty_buffer_request_room(tty, urb->actual_length);
+			tty_insert_flip_string(tty, data, urb->actual_length);
 			tty_flip_buffer_push(tty);
 		} else {
 			dbg("%s: empty read urb received", __FUNCTION__);
diff -puN drivers/usb/serial/pl2303.c~tty-layer-buffering-revamp drivers/usb/serial/pl2303.c
--- 25/drivers/usb/serial/pl2303.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/pl2303.c	Wed Aug 31 12:50:58 2005
@@ -940,16 +940,12 @@ static void pl2303_read_bulk_callback (s
 
 	tty = port->tty;
 	if (tty && urb->actual_length) {
+		tty_buffer_request_room(tty, urb->actual_length + 1);
 		/* overrun is special, not associated with a char */
 		if (status & UART_OVERRUN_ERROR)
 			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
-
-		for (i = 0; i < urb->actual_length; ++i) {
-			if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
-				tty_flip_buffer_push(tty);
-			}
+		for (i = 0; i < urb->actual_length; ++i)
 			tty_insert_flip_char (tty, data[i], tty_flag);
-		}
 		tty_flip_buffer_push (tty);
 	}
 
diff -puN drivers/usb/serial/ti_usb_3410_5052.c~tty-layer-buffering-revamp drivers/usb/serial/ti_usb_3410_5052.c
--- 25/drivers/usb/serial/ti_usb_3410_5052.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/ti_usb_3410_5052.c	Wed Aug 31 12:50:58 2005
@@ -1277,24 +1277,18 @@ static void ti_recv(struct device *dev, 
 	int cnt;
 
 	do {
-		if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
-			tty_flip_buffer_push(tty);
-			if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
-				dev_err(dev, "%s - dropping data, %d bytes lost\n", __FUNCTION__, length);
-				return;
-			}
+		cnt = tty_buffer_request_room(tty, length);
+		if (cnt < length) {
+			dev_err(dev, "%s - dropping data, %d bytes lost\n", __FUNCTION__, length - cnt);
+			if(cnt == 0)
+				break;
 		}
-		cnt = min(length, TTY_FLIPBUF_SIZE - tty->flip.count);
-		memcpy(tty->flip.char_buf_ptr, data, cnt);
-		memset(tty->flip.flag_buf_ptr, 0, cnt);
-		tty->flip.char_buf_ptr += cnt;
-		tty->flip.flag_buf_ptr += cnt;
-		tty->flip.count += cnt;
+		tty_insert_flip_string(tty, data, cnt);	
+		tty_flip_buffer_push(tty);
 		data += cnt;
 		length -= cnt;
 	} while (length > 0);
 
-	tty_flip_buffer_push(tty);
 }
 
 
diff -puN drivers/usb/serial/visor.c~tty-layer-buffering-revamp drivers/usb/serial/visor.c
--- 25/drivers/usb/serial/visor.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/drivers/usb/serial/visor.c	Wed Aug 31 12:50:58 2005
@@ -612,7 +612,6 @@ static void visor_read_bulk_callback (st
 	unsigned char *data = urb->transfer_buffer;
 	struct tty_struct *tty;
 	unsigned long flags;
-	int i;
 	int throttled;
 	int result;
 
@@ -627,14 +626,8 @@ static void visor_read_bulk_callback (st
 
 	tty = port->tty;
 	if (tty && urb->actual_length) {
-		for (i = 0; i < urb->actual_length ; ++i) {
-			/* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
-			if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
-				tty_flip_buffer_push(tty);
-			}
-			/* this doesn't actually push the data through unless tty->low_latency is set */
-			tty_insert_flip_char(tty, data[i], 0);
-		}
+		tty_buffer_request_room(tty, urb->actual_length);
+		tty_insert_flip_string(tty, data, urb->actual_length);
 		tty_flip_buffer_push(tty);
 	}
 	spin_lock_irqsave(&priv->lock, flags);
diff -puN include/linux/kbd_kern.h~tty-layer-buffering-revamp include/linux/kbd_kern.h
--- 25/include/linux/kbd_kern.h~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/include/linux/kbd_kern.h	Wed Aug 31 12:50:58 2005
@@ -151,7 +151,7 @@ extern unsigned int keymap_count;
 
 static inline void con_schedule_flip(struct tty_struct *t)
 {
-	schedule_work(&t->flip.work);
+	schedule_work(&t->buf.work);
 }
 
 #endif
diff -puN include/linux/tty_flip.h~tty-layer-buffering-revamp include/linux/tty_flip.h
--- 25/include/linux/tty_flip.h~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/include/linux/tty_flip.h	Wed Aug 31 12:50:58 2005
@@ -1,25 +1,33 @@
 #ifndef _LINUX_TTY_FLIP_H
 #define _LINUX_TTY_FLIP_H
 
+extern int tty_buffer_request_room(struct tty_struct *tty, size_t size);
+extern int tty_insert_flip_string(struct tty_struct *tty, unsigned char *chars, size_t size);
+extern int tty_insert_flip_string_flags(struct tty_struct *tty, unsigned char *chars, char *flags, size_t size);
+extern int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars, size_t size);
+extern int tty_prepare_flip_string_flags(struct tty_struct *tty, unsigned char **chars, char **flags, size_t size);
+
 #ifdef INCLUDE_INLINE_FUNCS
 #define _INLINE_ extern
 #else
 #define _INLINE_ static __inline__
 #endif
 
-_INLINE_ void tty_insert_flip_char(struct tty_struct *tty,
+_INLINE_ int tty_insert_flip_char(struct tty_struct *tty,
 				   unsigned char ch, char flag)
 {
-	if (tty->flip.count < TTY_FLIPBUF_SIZE) {
-		tty->flip.count++;
-		*tty->flip.flag_buf_ptr++ = flag;
-		*tty->flip.char_buf_ptr++ = ch;
+	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);
 }
 
 _INLINE_ void tty_schedule_flip(struct tty_struct *tty)
 {
-	schedule_delayed_work(&tty->flip.work, 1);
+	schedule_delayed_work(&tty->buf.work, 1);
 }
 
 #undef _INLINE_
diff -puN include/linux/tty.h~tty-layer-buffering-revamp include/linux/tty.h
--- 25/include/linux/tty.h~tty-layer-buffering-revamp	Wed Aug 31 12:50:55 2005
+++ 25-akpm/include/linux/tty.h	Wed Aug 31 12:50:58 2005
@@ -120,16 +120,22 @@ extern struct screen_info screen_info;
  */
 #define TTY_FLIPBUF_SIZE 512
 
-struct tty_flip_buffer {
+struct tty_buffer {
+	struct tty_buffer *next;
+	char *char_buf_ptr;
+	unsigned char *flag_buf_ptr;
+	int used;
+	int size;
+	/* Data points here */
+	unsigned long data[0];
+};
+
+struct tty_bufhead {
 	struct work_struct		work;
 	struct semaphore pty_sem;
-	char		*char_buf_ptr;
-	unsigned char	*flag_buf_ptr;
-	int		count;
-	int		buf_num;
-	unsigned char	char_buf[2*TTY_FLIPBUF_SIZE];
-	char		flag_buf[2*TTY_FLIPBUF_SIZE];
-	unsigned char	slop[4]; /* N.B. bug overwrites buffer by 1 */
+	struct tty_buffer *head;	/* Queue head */
+	struct tty_buffer *tail;	/* Active buffer */
+	struct tty_buffer *free;	/* Free queue head */
 };
 /*
  * The pty uses char_buf and flag_buf as a contiguous buffer
@@ -255,10 +261,11 @@ struct tty_struct {
 	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_flip_buffer flip;
+	struct tty_bufhead buf;
 	int max_flip_cnt;
 	int alt_speed;		/* For magic substitution of 38400 bps */
 	wait_queue_head_t write_wait;
diff -puN include/linux/tty_ldisc.h~tty-layer-buffering-revamp include/linux/tty_ldisc.h
--- 25/include/linux/tty_ldisc.h~tty-layer-buffering-revamp	Wed Aug 31 12:50:56 2005
+++ 25-akpm/include/linux/tty_ldisc.h	Wed Aug 31 12:50:58 2005
@@ -81,14 +81,6 @@
  * 	pointer of flag bytes which indicate whether a character was
  * 	received with a parity error, etc.
  * 
- * int	(*receive_room)(struct tty_struct *);
- *
- * 	This function is called by the low-level tty driver to
- * 	determine how many characters the line discpline can accept.
- * 	The low-level driver must not send more characters than was
- * 	indicated by receive_room, or the line discpline may drop
- * 	those characters.
- * 
  * void	(*write_wakeup)(struct tty_struct *);
  *
  * 	This function is called by the low-level tty driver to signal
@@ -136,7 +128,6 @@ struct tty_ldisc {
 	 */
 	void	(*receive_buf)(struct tty_struct *, const unsigned char *cp,
 			       char *fp, int count);
-	int	(*receive_room)(struct tty_struct *);
 	void	(*write_wakeup)(struct tty_struct *);
 
 	struct  module *owner;
diff -puN net/bluetooth/rfcomm/tty.c~tty-layer-buffering-revamp net/bluetooth/rfcomm/tty.c
--- 25/net/bluetooth/rfcomm/tty.c~tty-layer-buffering-revamp	Wed Aug 31 12:50:56 2005
+++ 25-akpm/net/bluetooth/rfcomm/tty.c	Wed Aug 31 12:50:58 2005
@@ -480,13 +480,8 @@ static void rfcomm_dev_data_ready(struct
 	BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);
 
 	if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
-		register int i;
-		for (i = 0; i < skb->len; i++) {
-			if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-				tty_flip_buffer_push(tty);
-
-			tty_insert_flip_char(tty, skb->data[i], 0);
-		}
+		tty_buffer_request_room(tty, skb->len);
+		tty_insert_flip_string(tty, skb->data, skb->len);
 		tty_flip_buffer_push(tty);
 	} else
 		tty->ldisc.receive_buf(tty, skb->data, NULL, skb->len);
_