aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/n_tty.c
diff options
context:
space:
mode:
authorAlan Cox <alan@lxorguk.ukuu.org.uk>2006-01-09 23:54:13 -0500
committerLinus Torvalds <torvalds@g5.osdl.org>2006-01-10 11:01:59 -0500
commit33f0f88f1c51ae5c2d593d26960c760ea154c2e2 (patch)
treef53a38cf49406863f079d74d0e8f91b276f7c1a9 /drivers/char/n_tty.c
parent6ed80991a2dce4afc113be35089c564d62fa1f11 (diff)
[PATCH] TTY layer buffering revamp
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. Because the buffers now dynamically expand you should only run out of buffering when the kernel runs out of memory for real. That means a lot of the horrible hacks high performance drivers used to do just aren't needed any more. Description: tty_insert_flip_char is an old API and continues to work as before, as does tty_flip_buffer_push() [this is why many drivers dont need modification]. It does now also return the number of chars inserted There are also tty_buffer_request_room(tty, len) which asks for a buffer block of the length requested and returns the space found. This improves efficiency with hardware that knows how much to transfer. and tty_insert_flip_string_flags(tty, str, flags, len) to insert a string of characters and flags For a smart interface the usual code is len = tty_request_buffer_room(tty, amount_hardware_says); tty_insert_flip_string(tty, buffer_from_card, len); More description! At the moment tty buffers are attached directly to the tty. This is causing a lot of the problems related to tty layer locking, also problems at high speed and also with bursty data (such as occurs in virtualised environments) I'm working on ripping out the flip buffers and replacing them with a pool of dynamically allocated buffers. This allows both for old style "byte I/O" devices and also helps virtualisation and smart devices where large blocks of data suddenely materialise and need storing. So far so good. Lots of drivers reference tty->flip.*. Several of them also call directly and unsafely into function pointers it provides. This will all break. Most drivers can use tty_insert_flip_char which can be kept as an API but others need more. At the moment I've added the following interfaces, if people think more will be needed now is a good time to say int tty_buffer_request_room(tty, size) Try and ensure at least size bytes are available, returns actual room (may be zero). At the moment it just uses the flipbuf space but that will change. Repeated calls without characters being added are not cumulative. (ie if you call it with 1, 1, 1, and then 4 you'll have four characters of space. The other functions will also try and grow buffers in future but this will be a more efficient way when you know block sizes. int tty_insert_flip_char(tty, ch, flag) As before insert a character if there is room. Now returns 1 for success, 0 for failure. int tty_insert_flip_string(tty, str, len) Insert a block of non error characters. Returns the number inserted. int tty_prepare_flip_string(tty, strptr, len) Adjust the buffer to allow len characters to be added. Returns a buffer pointer in strptr and the length available. This allows for hardware that needs to use functions like insl or mencpy_fromio. Signed-off-by: Alan Cox <alan@redhat.com> Cc: Paul Fulghum <paulkf@microgate.com> Signed-off-by: Hirokazu Takata <takata@linux-m32r.org> Signed-off-by: Serge Hallyn <serue@us.ibm.com> Signed-off-by: Jeff Dike <jdike@addtoit.com> Signed-off-by: John Hawkes <hawkes@sgi.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com> Signed-off-by: Adrian Bunk <bunk@stusta.de> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'drivers/char/n_tty.c')
-rw-r--r--drivers/char/n_tty.c66
1 files changed, 37 insertions, 29 deletions
diff --git a/drivers/char/n_tty.c b/drivers/char/n_tty.c
index c556f4d3ccd7..ccad7ae94541 100644
--- a/drivers/char/n_tty.c
+++ b/drivers/char/n_tty.c
@@ -78,7 +78,32 @@ static inline void free_buf(unsigned char *buf)
78 free_page((unsigned long) buf); 78 free_page((unsigned long) buf);
79} 79}
80 80
81static inline void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty) 81/**
82 * n_tty_set__room - receive space
83 * @tty: terminal
84 *
85 * Called by the driver to find out how much data it is
86 * permitted to feed to the line discipline without any being lost
87 * and thus to manage flow control. Not serialized. Answers for the
88 * "instant".
89 */
90
91static void n_tty_set_room(struct tty_struct *tty)
92{
93 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
94
95 /*
96 * If we are doing input canonicalization, and there are no
97 * pending newlines, let characters through without limit, so
98 * that erase characters will be handled. Other excess
99 * characters will be beeped.
100 */
101 if (left <= 0)
102 left = tty->icanon && !tty->canon_data;
103 tty->receive_room = left;
104}
105
106static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
82{ 107{
83 if (tty->read_cnt < N_TTY_BUF_SIZE) { 108 if (tty->read_cnt < N_TTY_BUF_SIZE) {
84 tty->read_buf[tty->read_head] = c; 109 tty->read_buf[tty->read_head] = c;
@@ -87,7 +112,7 @@ static inline void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
87 } 112 }
88} 113}
89 114
90static inline void put_tty_queue(unsigned char c, struct tty_struct *tty) 115static void put_tty_queue(unsigned char c, struct tty_struct *tty)
91{ 116{
92 unsigned long flags; 117 unsigned long flags;
93 /* 118 /*
@@ -136,6 +161,7 @@ static void reset_buffer_flags(struct tty_struct *tty)
136 spin_unlock_irqrestore(&tty->read_lock, flags); 161 spin_unlock_irqrestore(&tty->read_lock, flags);
137 tty->canon_head = tty->canon_data = tty->erasing = 0; 162 tty->canon_head = tty->canon_data = tty->erasing = 0;
138 memset(&tty->read_flags, 0, sizeof tty->read_flags); 163 memset(&tty->read_flags, 0, sizeof tty->read_flags);
164 n_tty_set_room(tty);
139 check_unthrottle(tty); 165 check_unthrottle(tty);
140} 166}
141 167
@@ -838,30 +864,6 @@ send_signal:
838 put_tty_queue(c, tty); 864 put_tty_queue(c, tty);
839} 865}
840 866
841/**
842 * n_tty_receive_room - receive space
843 * @tty: terminal
844 *
845 * Called by the driver to find out how much data it is
846 * permitted to feed to the line discipline without any being lost
847 * and thus to manage flow control. Not serialized. Answers for the
848 * "instant".
849 */
850
851static int n_tty_receive_room(struct tty_struct *tty)
852{
853 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
854
855 /*
856 * If we are doing input canonicalization, and there are no
857 * pending newlines, let characters through without limit, so
858 * that erase characters will be handled. Other excess
859 * characters will be beeped.
860 */
861 if (left <= 0)
862 left = tty->icanon && !tty->canon_data;
863 return left;
864}
865 867
866/** 868/**
867 * n_tty_write_wakeup - asynchronous I/O notifier 869 * n_tty_write_wakeup - asynchronous I/O notifier
@@ -953,6 +955,8 @@ static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
953 tty->driver->flush_chars(tty); 955 tty->driver->flush_chars(tty);
954 } 956 }
955 957
958 n_tty_set_room(tty);
959
956 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) { 960 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
957 kill_fasync(&tty->fasync, SIGIO, POLL_IN); 961 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
958 if (waitqueue_active(&tty->read_wait)) 962 if (waitqueue_active(&tty->read_wait))
@@ -964,7 +968,7 @@ static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
964 * mode. We don't want to throttle the driver if we're in 968 * mode. We don't want to throttle the driver if we're in
965 * canonical mode and don't have a newline yet! 969 * canonical mode and don't have a newline yet!
966 */ 970 */
967 if (n_tty_receive_room(tty) < TTY_THRESHOLD_THROTTLE) { 971 if (tty->receive_room < TTY_THRESHOLD_THROTTLE) {
968 /* check TTY_THROTTLED first so it indicates our state */ 972 /* check TTY_THROTTLED first so it indicates our state */
969 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) && 973 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
970 tty->driver->throttle) 974 tty->driver->throttle)
@@ -999,6 +1003,7 @@ static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
999 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) { 1003 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
1000 tty->raw = 1; 1004 tty->raw = 1;
1001 tty->real_raw = 1; 1005 tty->real_raw = 1;
1006 n_tty_set_room(tty);
1002 return; 1007 return;
1003 } 1008 }
1004 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) || 1009 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
@@ -1051,6 +1056,7 @@ static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
1051 else 1056 else
1052 tty->real_raw = 0; 1057 tty->real_raw = 0;
1053 } 1058 }
1059 n_tty_set_room(tty);
1054} 1060}
1055 1061
1056/** 1062/**
@@ -1130,7 +1136,7 @@ static inline int input_available_p(struct tty_struct *tty, int amt)
1130 * 1136 *
1131 */ 1137 */
1132 1138
1133static inline int copy_from_read_buf(struct tty_struct *tty, 1139static int copy_from_read_buf(struct tty_struct *tty,
1134 unsigned char __user **b, 1140 unsigned char __user **b,
1135 size_t *nr) 1141 size_t *nr)
1136 1142
@@ -1308,6 +1314,7 @@ do_it_again:
1308 retval = -ERESTARTSYS; 1314 retval = -ERESTARTSYS;
1309 break; 1315 break;
1310 } 1316 }
1317 n_tty_set_room(tty);
1311 clear_bit(TTY_DONT_FLIP, &tty->flags); 1318 clear_bit(TTY_DONT_FLIP, &tty->flags);
1312 timeout = schedule_timeout(timeout); 1319 timeout = schedule_timeout(timeout);
1313 set_bit(TTY_DONT_FLIP, &tty->flags); 1320 set_bit(TTY_DONT_FLIP, &tty->flags);
@@ -1401,6 +1408,8 @@ do_it_again:
1401 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags)) 1408 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1402 goto do_it_again; 1409 goto do_it_again;
1403 1410
1411 n_tty_set_room(tty);
1412
1404 return retval; 1413 return retval;
1405} 1414}
1406 1415
@@ -1553,7 +1562,6 @@ struct tty_ldisc tty_ldisc_N_TTY = {
1553 normal_poll, /* poll */ 1562 normal_poll, /* poll */
1554 NULL, /* hangup */ 1563 NULL, /* hangup */
1555 n_tty_receive_buf, /* receive_buf */ 1564 n_tty_receive_buf, /* receive_buf */
1556 n_tty_receive_room, /* receive_room */
1557 n_tty_write_wakeup /* write_wakeup */ 1565 n_tty_write_wakeup /* write_wakeup */
1558}; 1566};
1559 1567