blob: 6c7a1d043c764f0d827f85e71434f2c08a83ddba [file] [log] [blame]
Alan Coxe0495732008-10-13 10:36:58 +01001/*
2 * Tty buffer allocation management
3 */
4
5#include <linux/types.h>
6#include <linux/errno.h>
7#include <linux/tty.h>
8#include <linux/tty_driver.h>
9#include <linux/tty_flip.h>
10#include <linux/timer.h>
11#include <linux/string.h>
12#include <linux/slab.h>
13#include <linux/sched.h>
14#include <linux/init.h>
15#include <linux/wait.h>
16#include <linux/bitops.h>
17#include <linux/delay.h>
18#include <linux/module.h>
George Spelvin593fb1ae42013-02-12 02:00:43 -050019#include <linux/ratelimit.h>
Alan Coxe0495732008-10-13 10:36:58 +010020
21/**
22 * tty_buffer_free_all - free buffers used by a tty
23 * @tty: tty to free from
24 *
25 * Remove all the buffers pending on a tty whether queued with data
26 * or in the free ring. Must be called when the tty is no longer in use
27 *
28 * Locking: none
29 */
30
Jiri Slabyecbbfd42012-10-18 22:26:47 +020031void tty_buffer_free_all(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +010032{
Jiri Slabyecbbfd42012-10-18 22:26:47 +020033 struct tty_bufhead *buf = &port->buf;
Alan Coxe0495732008-10-13 10:36:58 +010034 struct tty_buffer *thead;
Jiri Slaby5cff39c2012-10-18 22:26:45 +020035
36 while ((thead = buf->head) != NULL) {
37 buf->head = thead->next;
Alan Coxe0495732008-10-13 10:36:58 +010038 kfree(thead);
39 }
Jiri Slaby5cff39c2012-10-18 22:26:45 +020040 while ((thead = buf->free) != NULL) {
41 buf->free = thead->next;
Alan Coxe0495732008-10-13 10:36:58 +010042 kfree(thead);
43 }
Jiri Slaby5cff39c2012-10-18 22:26:45 +020044 buf->tail = NULL;
45 buf->memory_used = 0;
Alan Coxe0495732008-10-13 10:36:58 +010046}
47
48/**
49 * tty_buffer_alloc - allocate a tty buffer
50 * @tty: tty device
51 * @size: desired size (characters)
52 *
53 * Allocate a new tty buffer to hold the desired number of characters.
54 * Return NULL if out of memory or the allocation would exceed the
55 * per device queue
56 *
57 * Locking: Caller must hold tty->buf.lock
58 */
59
Jiri Slabyecbbfd42012-10-18 22:26:47 +020060static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
Alan Coxe0495732008-10-13 10:36:58 +010061{
62 struct tty_buffer *p;
63
Jiri Slabyecbbfd42012-10-18 22:26:47 +020064 if (port->buf.memory_used + size > 65536)
Alan Coxe0495732008-10-13 10:36:58 +010065 return NULL;
66 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
67 if (p == NULL)
68 return NULL;
69 p->used = 0;
70 p->size = size;
71 p->next = NULL;
72 p->commit = 0;
73 p->read = 0;
74 p->char_buf_ptr = (char *)(p->data);
75 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
Jiri Slabyecbbfd42012-10-18 22:26:47 +020076 port->buf.memory_used += size;
Alan Coxe0495732008-10-13 10:36:58 +010077 return p;
78}
79
80/**
81 * tty_buffer_free - free a tty buffer
82 * @tty: tty owning the buffer
83 * @b: the buffer to free
84 *
85 * Free a tty buffer, or add it to the free list according to our
86 * internal strategy
87 *
88 * Locking: Caller must hold tty->buf.lock
89 */
90
Jiri Slabyecbbfd42012-10-18 22:26:47 +020091static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
Alan Coxe0495732008-10-13 10:36:58 +010092{
Jiri Slabyecbbfd42012-10-18 22:26:47 +020093 struct tty_bufhead *buf = &port->buf;
Jiri Slaby5cff39c2012-10-18 22:26:45 +020094
Alan Coxe0495732008-10-13 10:36:58 +010095 /* Dumb strategy for now - should keep some stats */
Jiri Slaby5cff39c2012-10-18 22:26:45 +020096 buf->memory_used -= b->size;
97 WARN_ON(buf->memory_used < 0);
Alan Coxe0495732008-10-13 10:36:58 +010098
99 if (b->size >= 512)
100 kfree(b);
101 else {
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200102 b->next = buf->free;
103 buf->free = b;
Alan Coxe0495732008-10-13 10:36:58 +0100104 }
105}
106
107/**
108 * __tty_buffer_flush - flush full tty buffers
109 * @tty: tty to flush
110 *
111 * flush all the buffers containing receive data. Caller must
112 * hold the buffer lock and must have ensured no parallel flush to
113 * ldisc is running.
114 *
115 * Locking: Caller must hold tty->buf.lock
116 */
117
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200118static void __tty_buffer_flush(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100119{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200120 struct tty_bufhead *buf = &port->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100121 struct tty_buffer *thead;
122
Ilya Zykov64325a32013-01-19 18:16:20 +0400123 if (unlikely(buf->head == NULL))
124 return;
125 while ((thead = buf->head->next) != NULL) {
126 tty_buffer_free(port, buf->head);
127 buf->head = thead;
Alan Coxe0495732008-10-13 10:36:58 +0100128 }
Ilya Zykov64325a32013-01-19 18:16:20 +0400129 WARN_ON(buf->head != buf->tail);
130 buf->head->read = buf->head->commit;
Alan Coxe0495732008-10-13 10:36:58 +0100131}
132
133/**
134 * tty_buffer_flush - flush full tty buffers
135 * @tty: tty to flush
136 *
137 * flush all the buffers containing receive data. If the buffer is
138 * being processed by flush_to_ldisc then we defer the processing
139 * to that function
140 *
141 * Locking: none
142 */
143
144void tty_buffer_flush(struct tty_struct *tty)
145{
Jiri Slaby2fc20662012-10-18 22:26:44 +0200146 struct tty_port *port = tty->port;
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200147 struct tty_bufhead *buf = &port->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100148 unsigned long flags;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200149
150 spin_lock_irqsave(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100151
152 /* If the data is being pushed to the tty layer then we can't
153 process it here. Instead set a flag and the flush_to_ldisc
154 path will process the flush request before it exits */
Jiri Slaby2fc20662012-10-18 22:26:44 +0200155 if (test_bit(TTYP_FLUSHING, &port->iflags)) {
156 set_bit(TTYP_FLUSHPENDING, &port->iflags);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200157 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100158 wait_event(tty->read_wait,
Jiri Slaby2fc20662012-10-18 22:26:44 +0200159 test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0);
Alan Coxe0495732008-10-13 10:36:58 +0100160 return;
161 } else
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200162 __tty_buffer_flush(port);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200163 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100164}
165
166/**
167 * tty_buffer_find - find a free tty buffer
168 * @tty: tty owning the buffer
169 * @size: characters wanted
170 *
171 * Locate an existing suitable tty buffer or if we are lacking one then
172 * allocate a new one. We round our buffers off in 256 character chunks
173 * to get better allocation behaviour.
174 *
175 * Locking: Caller must hold tty->buf.lock
176 */
177
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200178static struct tty_buffer *tty_buffer_find(struct tty_port *port, size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100179{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200180 struct tty_buffer **tbh = &port->buf.free;
Alan Coxe0495732008-10-13 10:36:58 +0100181 while ((*tbh) != NULL) {
182 struct tty_buffer *t = *tbh;
183 if (t->size >= size) {
184 *tbh = t->next;
185 t->next = NULL;
186 t->used = 0;
187 t->commit = 0;
188 t->read = 0;
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200189 port->buf.memory_used += t->size;
Alan Coxe0495732008-10-13 10:36:58 +0100190 return t;
191 }
192 tbh = &((*tbh)->next);
193 }
194 /* Round the buffer size out */
195 size = (size + 0xFF) & ~0xFF;
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200196 return tty_buffer_alloc(port, size);
Alan Coxe0495732008-10-13 10:36:58 +0100197 /* Should possibly check if this fails for the largest buffer we
198 have queued and recycle that ? */
199}
Alan Coxe0495732008-10-13 10:36:58 +0100200/**
Ilya Zykov64325a32013-01-19 18:16:20 +0400201 * tty_buffer_request_room - grow tty buffer if needed
Alan Coxe0495732008-10-13 10:36:58 +0100202 * @tty: tty structure
203 * @size: size desired
204 *
205 * Make at least size bytes of linear space available for the tty
206 * buffer. If we fail return the size we managed to find.
Ilya Zykov64325a32013-01-19 18:16:20 +0400207 *
208 * Locking: Takes port->buf.lock
Alan Coxe0495732008-10-13 10:36:58 +0100209 */
Ilya Zykov64325a32013-01-19 18:16:20 +0400210int tty_buffer_request_room(struct tty_port *port, size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100211{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200212 struct tty_bufhead *buf = &port->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100213 struct tty_buffer *b, *n;
214 int left;
Ilya Zykov64325a32013-01-19 18:16:20 +0400215 unsigned long flags;
216 spin_lock_irqsave(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100217 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
218 remove this conditional if its worth it. This would be invisible
219 to the callers */
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200220 b = buf->tail;
221 if (b != NULL)
Alan Coxe0495732008-10-13 10:36:58 +0100222 left = b->size - b->used;
223 else
224 left = 0;
225
226 if (left < size) {
227 /* This is the slow path - looking for new buffers to use */
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200228 if ((n = tty_buffer_find(port, size)) != NULL) {
Alan Coxe0495732008-10-13 10:36:58 +0100229 if (b != NULL) {
230 b->next = n;
231 b->commit = b->used;
232 } else
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200233 buf->head = n;
234 buf->tail = n;
Alan Coxe0495732008-10-13 10:36:58 +0100235 } else
236 size = left;
237 }
Ilya Zykov64325a32013-01-19 18:16:20 +0400238 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100239 return size;
240}
241EXPORT_SYMBOL_GPL(tty_buffer_request_room);
242
243/**
Alan Cox2832fc12010-02-18 16:43:54 +0000244 * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
Jiri Slaby2f693352013-01-03 15:53:02 +0100245 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100246 * @chars: characters
Alan Cox2832fc12010-02-18 16:43:54 +0000247 * @flag: flag value for each character
Alan Coxe0495732008-10-13 10:36:58 +0100248 * @size: size
249 *
250 * Queue a series of bytes to the tty buffering. All the characters
Johan Hovoldccc5ca82010-05-07 19:58:32 +0200251 * passed are marked with the supplied flag. Returns the number added.
Alan Coxe0495732008-10-13 10:36:58 +0100252 *
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200253 * Locking: Called functions may take port->buf.lock
Alan Coxe0495732008-10-13 10:36:58 +0100254 */
255
Jiri Slaby2f693352013-01-03 15:53:02 +0100256int tty_insert_flip_string_fixed_flag(struct tty_port *port,
Alan Cox2832fc12010-02-18 16:43:54 +0000257 const unsigned char *chars, char flag, size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100258{
259 int copied = 0;
260 do {
Fang Wenqid4bee0a2010-03-09 18:54:28 +0800261 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
Ilya Zykov64325a32013-01-19 18:16:20 +0400262 int space = tty_buffer_request_room(port, goal);
263 struct tty_buffer *tb = port->buf.tail;
Alan Coxe0495732008-10-13 10:36:58 +0100264 /* If there is no space then tb may be NULL */
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000265 if (unlikely(space == 0)) {
Alan Coxe0495732008-10-13 10:36:58 +0100266 break;
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000267 }
Alan Coxe0495732008-10-13 10:36:58 +0100268 memcpy(tb->char_buf_ptr + tb->used, chars, space);
Alan Cox2832fc12010-02-18 16:43:54 +0000269 memset(tb->flag_buf_ptr + tb->used, flag, space);
Alan Coxe0495732008-10-13 10:36:58 +0100270 tb->used += space;
271 copied += space;
272 chars += space;
273 /* There is a small chance that we need to split the data over
274 several buffers. If this is the case we must loop */
275 } while (unlikely(size > copied));
276 return copied;
277}
Alan Cox2832fc12010-02-18 16:43:54 +0000278EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
Alan Coxe0495732008-10-13 10:36:58 +0100279
280/**
281 * tty_insert_flip_string_flags - Add characters to the tty buffer
Jiri Slaby2f693352013-01-03 15:53:02 +0100282 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100283 * @chars: characters
284 * @flags: flag bytes
285 * @size: size
286 *
287 * Queue a series of bytes to the tty buffering. For each character
288 * the flags array indicates the status of the character. Returns the
289 * number added.
290 *
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200291 * Locking: Called functions may take port->buf.lock
Alan Coxe0495732008-10-13 10:36:58 +0100292 */
293
Jiri Slaby2f693352013-01-03 15:53:02 +0100294int tty_insert_flip_string_flags(struct tty_port *port,
Alan Coxe0495732008-10-13 10:36:58 +0100295 const unsigned char *chars, const char *flags, size_t size)
296{
297 int copied = 0;
298 do {
Fang Wenqid4bee0a2010-03-09 18:54:28 +0800299 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
Ilya Zykov64325a32013-01-19 18:16:20 +0400300 int space = tty_buffer_request_room(port, goal);
301 struct tty_buffer *tb = port->buf.tail;
Alan Coxe0495732008-10-13 10:36:58 +0100302 /* If there is no space then tb may be NULL */
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000303 if (unlikely(space == 0)) {
Alan Coxe0495732008-10-13 10:36:58 +0100304 break;
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000305 }
Alan Coxe0495732008-10-13 10:36:58 +0100306 memcpy(tb->char_buf_ptr + tb->used, chars, space);
307 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
308 tb->used += space;
309 copied += space;
310 chars += space;
311 flags += space;
312 /* There is a small chance that we need to split the data over
313 several buffers. If this is the case we must loop */
314 } while (unlikely(size > copied));
315 return copied;
316}
317EXPORT_SYMBOL(tty_insert_flip_string_flags);
318
319/**
320 * tty_schedule_flip - push characters to ldisc
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100321 * @port: tty port to push from
Alan Coxe0495732008-10-13 10:36:58 +0100322 *
323 * Takes any pending buffers and transfers their ownership to the
324 * ldisc side of the queue. It then schedules those characters for
325 * processing by the line discipline.
Ivo Siebencee4ad12012-09-27 14:02:05 +0200326 * Note that this function can only be used when the low_latency flag
327 * is unset. Otherwise the workqueue won't be flushed.
Alan Coxe0495732008-10-13 10:36:58 +0100328 *
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200329 * Locking: Takes port->buf.lock
Alan Coxe0495732008-10-13 10:36:58 +0100330 */
331
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100332void tty_schedule_flip(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100333{
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100334 struct tty_bufhead *buf = &port->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100335 unsigned long flags;
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100336 WARN_ON(port->low_latency);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200337
338 spin_lock_irqsave(&buf->lock, flags);
339 if (buf->tail != NULL)
340 buf->tail->commit = buf->tail->used;
341 spin_unlock_irqrestore(&buf->lock, flags);
342 schedule_work(&buf->work);
Alan Coxe0495732008-10-13 10:36:58 +0100343}
344EXPORT_SYMBOL(tty_schedule_flip);
345
346/**
347 * tty_prepare_flip_string - make room for characters
Jiri Slaby2f693352013-01-03 15:53:02 +0100348 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100349 * @chars: return pointer for character write area
350 * @size: desired size
351 *
352 * Prepare a block of space in the buffer for data. Returns the length
353 * available and buffer pointer to the space which is now allocated and
354 * accounted for as ready for normal characters. This is used for drivers
355 * that need their own block copy routines into the buffer. There is no
356 * guarantee the buffer is a DMA target!
357 *
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200358 * Locking: May call functions taking port->buf.lock
Alan Coxe0495732008-10-13 10:36:58 +0100359 */
360
Jiri Slaby2f693352013-01-03 15:53:02 +0100361int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200362 size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100363{
Ilya Zykov64325a32013-01-19 18:16:20 +0400364 int space = tty_buffer_request_room(port, size);
Alan Coxe0495732008-10-13 10:36:58 +0100365 if (likely(space)) {
Ilya Zykov64325a32013-01-19 18:16:20 +0400366 struct tty_buffer *tb = port->buf.tail;
Alan Coxe0495732008-10-13 10:36:58 +0100367 *chars = tb->char_buf_ptr + tb->used;
368 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
369 tb->used += space;
370 }
371 return space;
372}
373EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
374
375/**
376 * tty_prepare_flip_string_flags - make room for characters
Jiri Slaby2f693352013-01-03 15:53:02 +0100377 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100378 * @chars: return pointer for character write area
379 * @flags: return pointer for status flag write area
380 * @size: desired size
381 *
382 * Prepare a block of space in the buffer for data. Returns the length
383 * available and buffer pointer to the space which is now allocated and
384 * accounted for as ready for characters. This is used for drivers
385 * that need their own block copy routines into the buffer. There is no
386 * guarantee the buffer is a DMA target!
387 *
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200388 * Locking: May call functions taking port->buf.lock
Alan Coxe0495732008-10-13 10:36:58 +0100389 */
390
Jiri Slaby2f693352013-01-03 15:53:02 +0100391int tty_prepare_flip_string_flags(struct tty_port *port,
Alan Coxe0495732008-10-13 10:36:58 +0100392 unsigned char **chars, char **flags, size_t size)
393{
Ilya Zykov64325a32013-01-19 18:16:20 +0400394 int space = tty_buffer_request_room(port, size);
Alan Coxe0495732008-10-13 10:36:58 +0100395 if (likely(space)) {
Ilya Zykov64325a32013-01-19 18:16:20 +0400396 struct tty_buffer *tb = port->buf.tail;
Alan Coxe0495732008-10-13 10:36:58 +0100397 *chars = tb->char_buf_ptr + tb->used;
398 *flags = tb->flag_buf_ptr + tb->used;
399 tb->used += space;
400 }
401 return space;
402}
403EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
404
405
Peter Hurleyda261e72013-06-15 09:14:14 -0400406static int
407receive_buf(struct tty_struct *tty, struct tty_buffer *head, int count)
408{
409 struct tty_ldisc *disc = tty->ldisc;
410
411 count = min_t(int, count, tty->receive_room);
412 if (count)
413 disc->ops->receive_buf(tty, head->char_buf_ptr + head->read,
414 head->flag_buf_ptr + head->read, count);
415 head->read += count;
416 return count;
417}
Alan Coxe0495732008-10-13 10:36:58 +0100418
419/**
420 * flush_to_ldisc
421 * @work: tty structure passed from work queue.
422 *
423 * This routine is called out of the software interrupt to flush data
424 * from the buffer chain to the line discipline.
425 *
426 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
427 * while invoking the line discipline receive_buf method. The
428 * receive_buf method is single threaded for each tty instance.
429 */
430
431static void flush_to_ldisc(struct work_struct *work)
432{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200433 struct tty_port *port = container_of(work, struct tty_port, buf.work);
434 struct tty_bufhead *buf = &port->buf;
435 struct tty_struct *tty;
Alan Coxe0495732008-10-13 10:36:58 +0100436 unsigned long flags;
437 struct tty_ldisc *disc;
Alan Coxe0495732008-10-13 10:36:58 +0100438
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200439 tty = port->itty;
Jiri Slaby34dcfb82013-02-27 22:30:24 +0100440 if (tty == NULL)
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200441 return;
442
Alan Coxe0495732008-10-13 10:36:58 +0100443 disc = tty_ldisc_ref(tty);
Peter Hurley36697522013-06-15 07:04:48 -0400444 if (disc == NULL)
Alan Coxe0495732008-10-13 10:36:58 +0100445 return;
446
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200447 spin_lock_irqsave(&buf->lock, flags);
Linus Torvalds45242002009-10-14 08:59:49 -0700448
Jiri Slaby2fc20662012-10-18 22:26:44 +0200449 if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) {
Linus Torvalds81de9162011-06-08 07:46:30 -0700450 struct tty_buffer *head;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200451 while ((head = buf->head) != NULL) {
Linus Torvalds45242002009-10-14 08:59:49 -0700452 int count;
Linus Torvalds45242002009-10-14 08:59:49 -0700453
454 count = head->commit - head->read;
Alan Coxe0495732008-10-13 10:36:58 +0100455 if (!count) {
456 if (head->next == NULL)
457 break;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200458 buf->head = head->next;
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200459 tty_buffer_free(port, head);
Alan Coxe0495732008-10-13 10:36:58 +0100460 continue;
461 }
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200462 spin_unlock_irqrestore(&buf->lock, flags);
Peter Hurleyda261e72013-06-15 09:14:14 -0400463
464 count = receive_buf(tty, head, count);
465
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200466 spin_lock_irqsave(&buf->lock, flags);
Peter Hurley39f610e2013-03-20 13:20:43 -0400467 /* Ldisc or user is trying to flush the buffers.
468 We may have a deferred request to flush the
469 input buffer, if so pull the chain under the lock
470 and empty the queue */
471 if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) {
472 __tty_buffer_flush(port);
473 clear_bit(TTYP_FLUSHPENDING, &port->iflags);
474 wake_up(&tty->read_wait);
475 break;
Peter Hurleyda261e72013-06-15 09:14:14 -0400476 } else if (!count)
477 break;
Alan Coxe0495732008-10-13 10:36:58 +0100478 }
Jiri Slaby2fc20662012-10-18 22:26:44 +0200479 clear_bit(TTYP_FLUSHING, &port->iflags);
Alan Coxe0495732008-10-13 10:36:58 +0100480 }
Linus Torvalds45242002009-10-14 08:59:49 -0700481
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200482 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100483
484 tty_ldisc_deref(disc);
485}
486
487/**
OGAWA Hirofumie043e422009-07-29 12:15:56 -0700488 * tty_flush_to_ldisc
489 * @tty: tty to push
490 *
491 * Push the terminal flip buffers to the line discipline.
492 *
493 * Must not be called from IRQ context.
494 */
495void tty_flush_to_ldisc(struct tty_struct *tty)
496{
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100497 if (!tty->port->low_latency)
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200498 flush_work(&tty->port->buf.work);
OGAWA Hirofumie043e422009-07-29 12:15:56 -0700499}
500
501/**
Alan Coxe0495732008-10-13 10:36:58 +0100502 * tty_flip_buffer_push - terminal
Jiri Slaby2e124b42013-01-03 15:53:06 +0100503 * @port: tty port to push
Alan Coxe0495732008-10-13 10:36:58 +0100504 *
505 * Queue a push of the terminal flip buffers to the line discipline. This
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100506 * function must not be called from IRQ context if port->low_latency is
507 * set.
Alan Coxe0495732008-10-13 10:36:58 +0100508 *
509 * In the event of the queue being busy for flipping the work will be
510 * held off and retried later.
511 *
512 * Locking: tty buffer lock. Driver locks in low latency mode.
513 */
514
Jiri Slaby2e124b42013-01-03 15:53:06 +0100515void tty_flip_buffer_push(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100516{
Jiri Slaby2e124b42013-01-03 15:53:06 +0100517 struct tty_bufhead *buf = &port->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100518 unsigned long flags;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200519
520 spin_lock_irqsave(&buf->lock, flags);
521 if (buf->tail != NULL)
522 buf->tail->commit = buf->tail->used;
523 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100524
Jiri Slaby2e124b42013-01-03 15:53:06 +0100525 if (port->low_latency)
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200526 flush_to_ldisc(&buf->work);
Alan Coxe0495732008-10-13 10:36:58 +0100527 else
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200528 schedule_work(&buf->work);
Alan Coxe0495732008-10-13 10:36:58 +0100529}
530EXPORT_SYMBOL(tty_flip_buffer_push);
531
532/**
533 * tty_buffer_init - prepare a tty buffer structure
534 * @tty: tty to initialise
535 *
536 * Set up the initial state of the buffer management for a tty device.
537 * Must be called before the other tty buffer functions are used.
538 *
539 * Locking: none
540 */
541
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200542void tty_buffer_init(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100543{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200544 struct tty_bufhead *buf = &port->buf;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200545
546 spin_lock_init(&buf->lock);
547 buf->head = NULL;
548 buf->tail = NULL;
549 buf->free = NULL;
550 buf->memory_used = 0;
551 INIT_WORK(&buf->work, flush_to_ldisc);
Alan Coxe0495732008-10-13 10:36:58 +0100552}
553