blob: a5e396217f765e7076d70257e966da0466a74445 [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
Peter Hurley1cef50e2013-06-15 09:36:02 -040021
22#define MIN_TTYB_SIZE 256
23#define TTYB_ALIGN_MASK 255
24
Alan Coxe0495732008-10-13 10:36:58 +010025/**
26 * tty_buffer_free_all - free buffers used by a tty
27 * @tty: tty to free from
28 *
29 * Remove all the buffers pending on a tty whether queued with data
30 * or in the free ring. Must be called when the tty is no longer in use
31 *
32 * Locking: none
33 */
34
Jiri Slabyecbbfd42012-10-18 22:26:47 +020035void tty_buffer_free_all(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +010036{
Jiri Slabyecbbfd42012-10-18 22:26:47 +020037 struct tty_bufhead *buf = &port->buf;
Alan Coxe0495732008-10-13 10:36:58 +010038 struct tty_buffer *thead;
Jiri Slaby5cff39c2012-10-18 22:26:45 +020039
40 while ((thead = buf->head) != NULL) {
41 buf->head = thead->next;
Alan Coxe0495732008-10-13 10:36:58 +010042 kfree(thead);
43 }
Jiri Slaby5cff39c2012-10-18 22:26:45 +020044 while ((thead = buf->free) != NULL) {
45 buf->free = thead->next;
Alan Coxe0495732008-10-13 10:36:58 +010046 kfree(thead);
47 }
Jiri Slaby5cff39c2012-10-18 22:26:45 +020048 buf->tail = NULL;
49 buf->memory_used = 0;
Alan Coxe0495732008-10-13 10:36:58 +010050}
51
52/**
53 * tty_buffer_alloc - allocate a tty buffer
54 * @tty: tty device
55 * @size: desired size (characters)
56 *
57 * Allocate a new tty buffer to hold the desired number of characters.
58 * Return NULL if out of memory or the allocation would exceed the
59 * per device queue
60 *
61 * Locking: Caller must hold tty->buf.lock
62 */
63
Jiri Slabyecbbfd42012-10-18 22:26:47 +020064static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
Alan Coxe0495732008-10-13 10:36:58 +010065{
66 struct tty_buffer *p;
67
Jiri Slabyecbbfd42012-10-18 22:26:47 +020068 if (port->buf.memory_used + size > 65536)
Alan Coxe0495732008-10-13 10:36:58 +010069 return NULL;
70 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
71 if (p == NULL)
72 return NULL;
73 p->used = 0;
74 p->size = size;
75 p->next = NULL;
76 p->commit = 0;
77 p->read = 0;
Jiri Slabyecbbfd42012-10-18 22:26:47 +020078 port->buf.memory_used += size;
Alan Coxe0495732008-10-13 10:36:58 +010079 return p;
80}
81
82/**
83 * tty_buffer_free - free a tty buffer
84 * @tty: tty owning the buffer
85 * @b: the buffer to free
86 *
87 * Free a tty buffer, or add it to the free list according to our
88 * internal strategy
89 *
90 * Locking: Caller must hold tty->buf.lock
91 */
92
Jiri Slabyecbbfd42012-10-18 22:26:47 +020093static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
Alan Coxe0495732008-10-13 10:36:58 +010094{
Jiri Slabyecbbfd42012-10-18 22:26:47 +020095 struct tty_bufhead *buf = &port->buf;
Jiri Slaby5cff39c2012-10-18 22:26:45 +020096
Alan Coxe0495732008-10-13 10:36:58 +010097 /* Dumb strategy for now - should keep some stats */
Jiri Slaby5cff39c2012-10-18 22:26:45 +020098 buf->memory_used -= b->size;
99 WARN_ON(buf->memory_used < 0);
Alan Coxe0495732008-10-13 10:36:58 +0100100
Peter Hurley1cef50e2013-06-15 09:36:02 -0400101 if (b->size > MIN_TTYB_SIZE)
Alan Coxe0495732008-10-13 10:36:58 +0100102 kfree(b);
103 else {
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200104 b->next = buf->free;
105 buf->free = b;
Alan Coxe0495732008-10-13 10:36:58 +0100106 }
107}
108
109/**
110 * __tty_buffer_flush - flush full tty buffers
111 * @tty: tty to flush
112 *
113 * flush all the buffers containing receive data. Caller must
114 * hold the buffer lock and must have ensured no parallel flush to
115 * ldisc is running.
116 *
117 * Locking: Caller must hold tty->buf.lock
118 */
119
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200120static void __tty_buffer_flush(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100121{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200122 struct tty_bufhead *buf = &port->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100123 struct tty_buffer *thead;
124
Ilya Zykov64325a32013-01-19 18:16:20 +0400125 if (unlikely(buf->head == NULL))
126 return;
127 while ((thead = buf->head->next) != NULL) {
128 tty_buffer_free(port, buf->head);
129 buf->head = thead;
Alan Coxe0495732008-10-13 10:36:58 +0100130 }
Ilya Zykov64325a32013-01-19 18:16:20 +0400131 WARN_ON(buf->head != buf->tail);
132 buf->head->read = buf->head->commit;
Alan Coxe0495732008-10-13 10:36:58 +0100133}
134
135/**
136 * tty_buffer_flush - flush full tty buffers
137 * @tty: tty to flush
138 *
139 * flush all the buffers containing receive data. If the buffer is
140 * being processed by flush_to_ldisc then we defer the processing
141 * to that function
142 *
143 * Locking: none
144 */
145
146void tty_buffer_flush(struct tty_struct *tty)
147{
Jiri Slaby2fc20662012-10-18 22:26:44 +0200148 struct tty_port *port = tty->port;
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200149 struct tty_bufhead *buf = &port->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100150 unsigned long flags;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200151
152 spin_lock_irqsave(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100153
154 /* If the data is being pushed to the tty layer then we can't
155 process it here. Instead set a flag and the flush_to_ldisc
156 path will process the flush request before it exits */
Jiri Slaby2fc20662012-10-18 22:26:44 +0200157 if (test_bit(TTYP_FLUSHING, &port->iflags)) {
158 set_bit(TTYP_FLUSHPENDING, &port->iflags);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200159 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100160 wait_event(tty->read_wait,
Jiri Slaby2fc20662012-10-18 22:26:44 +0200161 test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0);
Alan Coxe0495732008-10-13 10:36:58 +0100162 return;
163 } else
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200164 __tty_buffer_flush(port);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200165 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100166}
167
168/**
169 * tty_buffer_find - find a free tty buffer
170 * @tty: tty owning the buffer
171 * @size: characters wanted
172 *
173 * Locate an existing suitable tty buffer or if we are lacking one then
174 * allocate a new one. We round our buffers off in 256 character chunks
175 * to get better allocation behaviour.
176 *
177 * Locking: Caller must hold tty->buf.lock
178 */
179
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200180static struct tty_buffer *tty_buffer_find(struct tty_port *port, size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100181{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200182 struct tty_buffer **tbh = &port->buf.free;
Peter Hurley1cef50e2013-06-15 09:36:02 -0400183 if (size <= MIN_TTYB_SIZE) {
184 if (*tbh) {
185 struct tty_buffer *t = *tbh;
186
Alan Coxe0495732008-10-13 10:36:58 +0100187 *tbh = t->next;
188 t->next = NULL;
189 t->used = 0;
190 t->commit = 0;
191 t->read = 0;
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200192 port->buf.memory_used += t->size;
Alan Coxe0495732008-10-13 10:36:58 +0100193 return t;
194 }
Alan Coxe0495732008-10-13 10:36:58 +0100195 }
196 /* Round the buffer size out */
Peter Hurley1cef50e2013-06-15 09:36:02 -0400197 size = __ALIGN_MASK(size, TTYB_ALIGN_MASK);
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200198 return tty_buffer_alloc(port, size);
Alan Coxe0495732008-10-13 10:36:58 +0100199 /* Should possibly check if this fails for the largest buffer we
200 have queued and recycle that ? */
201}
Alan Coxe0495732008-10-13 10:36:58 +0100202/**
Ilya Zykov64325a32013-01-19 18:16:20 +0400203 * tty_buffer_request_room - grow tty buffer if needed
Alan Coxe0495732008-10-13 10:36:58 +0100204 * @tty: tty structure
205 * @size: size desired
206 *
207 * Make at least size bytes of linear space available for the tty
208 * buffer. If we fail return the size we managed to find.
Ilya Zykov64325a32013-01-19 18:16:20 +0400209 *
210 * Locking: Takes port->buf.lock
Alan Coxe0495732008-10-13 10:36:58 +0100211 */
Ilya Zykov64325a32013-01-19 18:16:20 +0400212int tty_buffer_request_room(struct tty_port *port, size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100213{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200214 struct tty_bufhead *buf = &port->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100215 struct tty_buffer *b, *n;
216 int left;
Ilya Zykov64325a32013-01-19 18:16:20 +0400217 unsigned long flags;
218 spin_lock_irqsave(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100219 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
220 remove this conditional if its worth it. This would be invisible
221 to the callers */
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200222 b = buf->tail;
223 if (b != NULL)
Alan Coxe0495732008-10-13 10:36:58 +0100224 left = b->size - b->used;
225 else
226 left = 0;
227
228 if (left < size) {
229 /* This is the slow path - looking for new buffers to use */
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200230 if ((n = tty_buffer_find(port, size)) != NULL) {
Alan Coxe0495732008-10-13 10:36:58 +0100231 if (b != NULL) {
232 b->next = n;
233 b->commit = b->used;
234 } else
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200235 buf->head = n;
236 buf->tail = n;
Alan Coxe0495732008-10-13 10:36:58 +0100237 } else
238 size = left;
239 }
Ilya Zykov64325a32013-01-19 18:16:20 +0400240 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100241 return size;
242}
243EXPORT_SYMBOL_GPL(tty_buffer_request_room);
244
245/**
Alan Cox2832fc12010-02-18 16:43:54 +0000246 * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
Jiri Slaby2f693352013-01-03 15:53:02 +0100247 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100248 * @chars: characters
Alan Cox2832fc12010-02-18 16:43:54 +0000249 * @flag: flag value for each character
Alan Coxe0495732008-10-13 10:36:58 +0100250 * @size: size
251 *
252 * Queue a series of bytes to the tty buffering. All the characters
Johan Hovoldccc5ca82010-05-07 19:58:32 +0200253 * passed are marked with the supplied flag. Returns the number added.
Alan Coxe0495732008-10-13 10:36:58 +0100254 *
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200255 * Locking: Called functions may take port->buf.lock
Alan Coxe0495732008-10-13 10:36:58 +0100256 */
257
Jiri Slaby2f693352013-01-03 15:53:02 +0100258int tty_insert_flip_string_fixed_flag(struct tty_port *port,
Alan Cox2832fc12010-02-18 16:43:54 +0000259 const unsigned char *chars, char flag, size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100260{
261 int copied = 0;
262 do {
Fang Wenqid4bee0a2010-03-09 18:54:28 +0800263 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
Ilya Zykov64325a32013-01-19 18:16:20 +0400264 int space = tty_buffer_request_room(port, goal);
265 struct tty_buffer *tb = port->buf.tail;
Alan Coxe0495732008-10-13 10:36:58 +0100266 /* If there is no space then tb may be NULL */
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000267 if (unlikely(space == 0)) {
Alan Coxe0495732008-10-13 10:36:58 +0100268 break;
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000269 }
Peter Hurley1fc359f2013-06-15 09:36:01 -0400270 memcpy(char_buf_ptr(tb, tb->used), chars, space);
271 memset(flag_buf_ptr(tb, tb->used), flag, space);
Alan Coxe0495732008-10-13 10:36:58 +0100272 tb->used += space;
273 copied += space;
274 chars += space;
275 /* There is a small chance that we need to split the data over
276 several buffers. If this is the case we must loop */
277 } while (unlikely(size > copied));
278 return copied;
279}
Alan Cox2832fc12010-02-18 16:43:54 +0000280EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
Alan Coxe0495732008-10-13 10:36:58 +0100281
282/**
283 * tty_insert_flip_string_flags - Add characters to the tty buffer
Jiri Slaby2f693352013-01-03 15:53:02 +0100284 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100285 * @chars: characters
286 * @flags: flag bytes
287 * @size: size
288 *
289 * Queue a series of bytes to the tty buffering. For each character
290 * the flags array indicates the status of the character. Returns the
291 * number added.
292 *
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200293 * Locking: Called functions may take port->buf.lock
Alan Coxe0495732008-10-13 10:36:58 +0100294 */
295
Jiri Slaby2f693352013-01-03 15:53:02 +0100296int tty_insert_flip_string_flags(struct tty_port *port,
Alan Coxe0495732008-10-13 10:36:58 +0100297 const unsigned char *chars, const char *flags, size_t size)
298{
299 int copied = 0;
300 do {
Fang Wenqid4bee0a2010-03-09 18:54:28 +0800301 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
Ilya Zykov64325a32013-01-19 18:16:20 +0400302 int space = tty_buffer_request_room(port, goal);
303 struct tty_buffer *tb = port->buf.tail;
Alan Coxe0495732008-10-13 10:36:58 +0100304 /* If there is no space then tb may be NULL */
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000305 if (unlikely(space == 0)) {
Alan Coxe0495732008-10-13 10:36:58 +0100306 break;
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000307 }
Peter Hurley1fc359f2013-06-15 09:36:01 -0400308 memcpy(char_buf_ptr(tb, tb->used), chars, space);
309 memcpy(flag_buf_ptr(tb, tb->used), flags, space);
Alan Coxe0495732008-10-13 10:36:58 +0100310 tb->used += space;
311 copied += space;
312 chars += space;
313 flags += space;
314 /* There is a small chance that we need to split the data over
315 several buffers. If this is the case we must loop */
316 } while (unlikely(size > copied));
317 return copied;
318}
319EXPORT_SYMBOL(tty_insert_flip_string_flags);
320
321/**
322 * tty_schedule_flip - push characters to ldisc
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100323 * @port: tty port to push from
Alan Coxe0495732008-10-13 10:36:58 +0100324 *
325 * Takes any pending buffers and transfers their ownership to the
326 * ldisc side of the queue. It then schedules those characters for
327 * processing by the line discipline.
Ivo Siebencee4ad12012-09-27 14:02:05 +0200328 * Note that this function can only be used when the low_latency flag
329 * is unset. Otherwise the workqueue won't be flushed.
Alan Coxe0495732008-10-13 10:36:58 +0100330 *
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200331 * Locking: Takes port->buf.lock
Alan Coxe0495732008-10-13 10:36:58 +0100332 */
333
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100334void tty_schedule_flip(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100335{
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100336 struct tty_bufhead *buf = &port->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100337 unsigned long flags;
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100338 WARN_ON(port->low_latency);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200339
340 spin_lock_irqsave(&buf->lock, flags);
341 if (buf->tail != NULL)
342 buf->tail->commit = buf->tail->used;
343 spin_unlock_irqrestore(&buf->lock, flags);
344 schedule_work(&buf->work);
Alan Coxe0495732008-10-13 10:36:58 +0100345}
346EXPORT_SYMBOL(tty_schedule_flip);
347
348/**
349 * tty_prepare_flip_string - make room for characters
Jiri Slaby2f693352013-01-03 15:53:02 +0100350 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100351 * @chars: return pointer for character write area
352 * @size: desired size
353 *
354 * Prepare a block of space in the buffer for data. Returns the length
355 * available and buffer pointer to the space which is now allocated and
356 * accounted for as ready for normal characters. This is used for drivers
357 * that need their own block copy routines into the buffer. There is no
358 * guarantee the buffer is a DMA target!
359 *
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200360 * Locking: May call functions taking port->buf.lock
Alan Coxe0495732008-10-13 10:36:58 +0100361 */
362
Jiri Slaby2f693352013-01-03 15:53:02 +0100363int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200364 size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100365{
Ilya Zykov64325a32013-01-19 18:16:20 +0400366 int space = tty_buffer_request_room(port, size);
Alan Coxe0495732008-10-13 10:36:58 +0100367 if (likely(space)) {
Ilya Zykov64325a32013-01-19 18:16:20 +0400368 struct tty_buffer *tb = port->buf.tail;
Peter Hurley1fc359f2013-06-15 09:36:01 -0400369 *chars = char_buf_ptr(tb, tb->used);
370 memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space);
Alan Coxe0495732008-10-13 10:36:58 +0100371 tb->used += space;
372 }
373 return space;
374}
375EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
376
377/**
378 * tty_prepare_flip_string_flags - make room for characters
Jiri Slaby2f693352013-01-03 15:53:02 +0100379 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100380 * @chars: return pointer for character write area
381 * @flags: return pointer for status flag write area
382 * @size: desired size
383 *
384 * Prepare a block of space in the buffer for data. Returns the length
385 * available and buffer pointer to the space which is now allocated and
386 * accounted for as ready for characters. This is used for drivers
387 * that need their own block copy routines into the buffer. There is no
388 * guarantee the buffer is a DMA target!
389 *
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200390 * Locking: May call functions taking port->buf.lock
Alan Coxe0495732008-10-13 10:36:58 +0100391 */
392
Jiri Slaby2f693352013-01-03 15:53:02 +0100393int tty_prepare_flip_string_flags(struct tty_port *port,
Alan Coxe0495732008-10-13 10:36:58 +0100394 unsigned char **chars, char **flags, size_t size)
395{
Ilya Zykov64325a32013-01-19 18:16:20 +0400396 int space = tty_buffer_request_room(port, size);
Alan Coxe0495732008-10-13 10:36:58 +0100397 if (likely(space)) {
Ilya Zykov64325a32013-01-19 18:16:20 +0400398 struct tty_buffer *tb = port->buf.tail;
Peter Hurley1fc359f2013-06-15 09:36:01 -0400399 *chars = char_buf_ptr(tb, tb->used);
400 *flags = flag_buf_ptr(tb, tb->used);
Alan Coxe0495732008-10-13 10:36:58 +0100401 tb->used += space;
402 }
403 return space;
404}
405EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
406
407
Peter Hurleyda261e72013-06-15 09:14:14 -0400408static int
409receive_buf(struct tty_struct *tty, struct tty_buffer *head, int count)
410{
411 struct tty_ldisc *disc = tty->ldisc;
Peter Hurley1fc359f2013-06-15 09:36:01 -0400412 unsigned char *p = char_buf_ptr(head, head->read);
413 char *f = flag_buf_ptr(head, head->read);
Peter Hurleyda261e72013-06-15 09:14:14 -0400414
Peter Hurley24a89d12013-06-15 09:14:15 -0400415 if (disc->ops->receive_buf2)
416 count = disc->ops->receive_buf2(tty, p, f, count);
417 else {
418 count = min_t(int, count, tty->receive_room);
419 if (count)
420 disc->ops->receive_buf(tty, p, f, count);
421 }
Peter Hurleyda261e72013-06-15 09:14:14 -0400422 head->read += count;
423 return count;
424}
Alan Coxe0495732008-10-13 10:36:58 +0100425
426/**
427 * flush_to_ldisc
428 * @work: tty structure passed from work queue.
429 *
430 * This routine is called out of the software interrupt to flush data
431 * from the buffer chain to the line discipline.
432 *
433 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
434 * while invoking the line discipline receive_buf method. The
435 * receive_buf method is single threaded for each tty instance.
436 */
437
438static void flush_to_ldisc(struct work_struct *work)
439{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200440 struct tty_port *port = container_of(work, struct tty_port, buf.work);
441 struct tty_bufhead *buf = &port->buf;
442 struct tty_struct *tty;
Alan Coxe0495732008-10-13 10:36:58 +0100443 unsigned long flags;
444 struct tty_ldisc *disc;
Alan Coxe0495732008-10-13 10:36:58 +0100445
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200446 tty = port->itty;
Jiri Slaby34dcfb82013-02-27 22:30:24 +0100447 if (tty == NULL)
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200448 return;
449
Alan Coxe0495732008-10-13 10:36:58 +0100450 disc = tty_ldisc_ref(tty);
Peter Hurley36697522013-06-15 07:04:48 -0400451 if (disc == NULL)
Alan Coxe0495732008-10-13 10:36:58 +0100452 return;
453
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200454 spin_lock_irqsave(&buf->lock, flags);
Linus Torvalds45242002009-10-14 08:59:49 -0700455
Jiri Slaby2fc20662012-10-18 22:26:44 +0200456 if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) {
Linus Torvalds81de9162011-06-08 07:46:30 -0700457 struct tty_buffer *head;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200458 while ((head = buf->head) != NULL) {
Linus Torvalds45242002009-10-14 08:59:49 -0700459 int count;
Linus Torvalds45242002009-10-14 08:59:49 -0700460
461 count = head->commit - head->read;
Alan Coxe0495732008-10-13 10:36:58 +0100462 if (!count) {
463 if (head->next == NULL)
464 break;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200465 buf->head = head->next;
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200466 tty_buffer_free(port, head);
Alan Coxe0495732008-10-13 10:36:58 +0100467 continue;
468 }
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200469 spin_unlock_irqrestore(&buf->lock, flags);
Peter Hurleyda261e72013-06-15 09:14:14 -0400470
471 count = receive_buf(tty, head, count);
472
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200473 spin_lock_irqsave(&buf->lock, flags);
Peter Hurley39f610e2013-03-20 13:20:43 -0400474 /* Ldisc or user is trying to flush the buffers.
475 We may have a deferred request to flush the
476 input buffer, if so pull the chain under the lock
477 and empty the queue */
478 if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) {
479 __tty_buffer_flush(port);
480 clear_bit(TTYP_FLUSHPENDING, &port->iflags);
481 wake_up(&tty->read_wait);
482 break;
Peter Hurleyda261e72013-06-15 09:14:14 -0400483 } else if (!count)
484 break;
Alan Coxe0495732008-10-13 10:36:58 +0100485 }
Jiri Slaby2fc20662012-10-18 22:26:44 +0200486 clear_bit(TTYP_FLUSHING, &port->iflags);
Alan Coxe0495732008-10-13 10:36:58 +0100487 }
Linus Torvalds45242002009-10-14 08:59:49 -0700488
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200489 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100490
491 tty_ldisc_deref(disc);
492}
493
494/**
OGAWA Hirofumie043e422009-07-29 12:15:56 -0700495 * tty_flush_to_ldisc
496 * @tty: tty to push
497 *
498 * Push the terminal flip buffers to the line discipline.
499 *
500 * Must not be called from IRQ context.
501 */
502void tty_flush_to_ldisc(struct tty_struct *tty)
503{
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100504 if (!tty->port->low_latency)
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200505 flush_work(&tty->port->buf.work);
OGAWA Hirofumie043e422009-07-29 12:15:56 -0700506}
507
508/**
Alan Coxe0495732008-10-13 10:36:58 +0100509 * tty_flip_buffer_push - terminal
Jiri Slaby2e124b42013-01-03 15:53:06 +0100510 * @port: tty port to push
Alan Coxe0495732008-10-13 10:36:58 +0100511 *
512 * Queue a push of the terminal flip buffers to the line discipline. This
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100513 * function must not be called from IRQ context if port->low_latency is
514 * set.
Alan Coxe0495732008-10-13 10:36:58 +0100515 *
516 * In the event of the queue being busy for flipping the work will be
517 * held off and retried later.
518 *
519 * Locking: tty buffer lock. Driver locks in low latency mode.
520 */
521
Jiri Slaby2e124b42013-01-03 15:53:06 +0100522void tty_flip_buffer_push(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100523{
Jiri Slaby2e124b42013-01-03 15:53:06 +0100524 struct tty_bufhead *buf = &port->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100525 unsigned long flags;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200526
527 spin_lock_irqsave(&buf->lock, flags);
528 if (buf->tail != NULL)
529 buf->tail->commit = buf->tail->used;
530 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100531
Jiri Slaby2e124b42013-01-03 15:53:06 +0100532 if (port->low_latency)
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200533 flush_to_ldisc(&buf->work);
Alan Coxe0495732008-10-13 10:36:58 +0100534 else
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200535 schedule_work(&buf->work);
Alan Coxe0495732008-10-13 10:36:58 +0100536}
537EXPORT_SYMBOL(tty_flip_buffer_push);
538
539/**
540 * tty_buffer_init - prepare a tty buffer structure
541 * @tty: tty to initialise
542 *
543 * Set up the initial state of the buffer management for a tty device.
544 * Must be called before the other tty buffer functions are used.
545 *
546 * Locking: none
547 */
548
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200549void tty_buffer_init(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100550{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200551 struct tty_bufhead *buf = &port->buf;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200552
553 spin_lock_init(&buf->lock);
554 buf->head = NULL;
555 buf->tail = NULL;
556 buf->free = NULL;
557 buf->memory_used = 0;
558 INIT_WORK(&buf->work, flush_to_ldisc);
Alan Coxe0495732008-10-13 10:36:58 +0100559}
560