blob: f22e116db105df51990d1abfb7231b9fe9f29b25 [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 Spelvin593fb1a2013-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
Peter Hurley7bfe0b72013-06-15 09:36:08 -040025/*
26 * Byte threshold to limit memory consumption for flip buffers.
27 * The actual memory limit is > 2x this amount.
28 */
29#define TTYB_MEM_LIMIT 65536
30
31
32/**
Peter Hurleya7c8d582013-06-15 09:36:15 -040033 * tty_buffer_lock_exclusive - gain exclusive access to buffer
34 * tty_buffer_unlock_exclusive - release exclusive access
35 *
36 * @port - tty_port owning the flip buffer
37 *
38 * Guarantees safe use of the line discipline's receive_buf() method by
39 * excluding the buffer work and any pending flush from using the flip
40 * buffer. Data can continue to be added concurrently to the flip buffer
41 * from the driver side.
42 *
43 * On release, the buffer work is restarted if there is data in the
44 * flip buffer
45 */
46
47void tty_buffer_lock_exclusive(struct tty_port *port)
48{
49 struct tty_bufhead *buf = &port->buf;
50
51 atomic_inc(&buf->priority);
52 mutex_lock(&buf->lock);
53}
54
55void tty_buffer_unlock_exclusive(struct tty_port *port)
56{
57 struct tty_bufhead *buf = &port->buf;
58 int restart;
59
60 restart = buf->head->commit != buf->head->read;
61
62 atomic_dec(&buf->priority);
63 mutex_unlock(&buf->lock);
64 if (restart)
65 queue_work(system_unbound_wq, &buf->work);
66}
67
68/**
Peter Hurley7bfe0b72013-06-15 09:36:08 -040069 * tty_buffer_space_avail - return unused buffer space
70 * @port - tty_port owning the flip buffer
71 *
72 * Returns the # of bytes which can be written by the driver without
73 * reaching the buffer limit.
74 *
75 * Note: this does not guarantee that memory is available to write
76 * the returned # of bytes (use tty_prepare_flip_string_xxx() to
77 * pre-allocate if memory guarantee is required).
78 */
79
80int tty_buffer_space_avail(struct tty_port *port)
81{
82 int space = TTYB_MEM_LIMIT - atomic_read(&port->buf.memory_used);
83 return max(space, 0);
84}
85
Peter Hurley9dd51392013-06-15 09:36:03 -040086static void tty_buffer_reset(struct tty_buffer *p, size_t size)
87{
88 p->used = 0;
89 p->size = size;
90 p->next = NULL;
91 p->commit = 0;
92 p->read = 0;
93}
94
Alan Coxe0495732008-10-13 10:36:58 +010095/**
96 * tty_buffer_free_all - free buffers used by a tty
97 * @tty: tty to free from
98 *
99 * Remove all the buffers pending on a tty whether queued with data
100 * or in the free ring. Must be called when the tty is no longer in use
Alan Coxe0495732008-10-13 10:36:58 +0100101 */
102
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200103void tty_buffer_free_all(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100104{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200105 struct tty_bufhead *buf = &port->buf;
Peter Hurley809850b2013-06-15 09:36:06 -0400106 struct tty_buffer *p, *next;
107 struct llist_node *llist;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200108
Peter Hurley2cf7b672013-06-15 09:36:05 -0400109 while ((p = buf->head) != NULL) {
110 buf->head = p->next;
Peter Hurley7391ee12013-06-15 09:36:07 -0400111 if (p->size > 0)
112 kfree(p);
Alan Coxe0495732008-10-13 10:36:58 +0100113 }
Peter Hurley809850b2013-06-15 09:36:06 -0400114 llist = llist_del_all(&buf->free);
115 llist_for_each_entry_safe(p, next, llist, free)
Peter Hurley2cf7b672013-06-15 09:36:05 -0400116 kfree(p);
Peter Hurley809850b2013-06-15 09:36:06 -0400117
Peter Hurley7391ee12013-06-15 09:36:07 -0400118 tty_buffer_reset(&buf->sentinel, 0);
119 buf->head = &buf->sentinel;
120 buf->tail = &buf->sentinel;
Peter Hurley7bfe0b72013-06-15 09:36:08 -0400121
122 atomic_set(&buf->memory_used, 0);
Alan Coxe0495732008-10-13 10:36:58 +0100123}
124
125/**
126 * tty_buffer_alloc - allocate a tty buffer
127 * @tty: tty device
128 * @size: desired size (characters)
129 *
130 * Allocate a new tty buffer to hold the desired number of characters.
Peter Hurley11b9faa2013-06-15 09:36:04 -0400131 * We round our buffers off in 256 character chunks to get better
132 * allocation behaviour.
Alan Coxe0495732008-10-13 10:36:58 +0100133 * Return NULL if out of memory or the allocation would exceed the
134 * per device queue
Alan Coxe0495732008-10-13 10:36:58 +0100135 */
136
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200137static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100138{
Peter Hurley809850b2013-06-15 09:36:06 -0400139 struct llist_node *free;
Alan Coxe0495732008-10-13 10:36:58 +0100140 struct tty_buffer *p;
141
Peter Hurley11b9faa2013-06-15 09:36:04 -0400142 /* Round the buffer size out */
143 size = __ALIGN_MASK(size, TTYB_ALIGN_MASK);
144
145 if (size <= MIN_TTYB_SIZE) {
Peter Hurley809850b2013-06-15 09:36:06 -0400146 free = llist_del_first(&port->buf.free);
147 if (free) {
148 p = llist_entry(free, struct tty_buffer, free);
Peter Hurley11b9faa2013-06-15 09:36:04 -0400149 goto found;
150 }
151 }
152
153 /* Should possibly check if this fails for the largest buffer we
154 have queued and recycle that ? */
Peter Hurley7bfe0b72013-06-15 09:36:08 -0400155 if (atomic_read(&port->buf.memory_used) > TTYB_MEM_LIMIT)
Alan Coxe0495732008-10-13 10:36:58 +0100156 return NULL;
157 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
158 if (p == NULL)
159 return NULL;
Peter Hurley9dd51392013-06-15 09:36:03 -0400160
Peter Hurley11b9faa2013-06-15 09:36:04 -0400161found:
Peter Hurley9dd51392013-06-15 09:36:03 -0400162 tty_buffer_reset(p, size);
Peter Hurley7bfe0b72013-06-15 09:36:08 -0400163 atomic_add(size, &port->buf.memory_used);
Alan Coxe0495732008-10-13 10:36:58 +0100164 return p;
165}
166
167/**
168 * tty_buffer_free - free a tty buffer
169 * @tty: tty owning the buffer
170 * @b: the buffer to free
171 *
172 * Free a tty buffer, or add it to the free list according to our
173 * internal strategy
Alan Coxe0495732008-10-13 10:36:58 +0100174 */
175
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200176static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
Alan Coxe0495732008-10-13 10:36:58 +0100177{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200178 struct tty_bufhead *buf = &port->buf;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200179
Alan Coxe0495732008-10-13 10:36:58 +0100180 /* Dumb strategy for now - should keep some stats */
Peter Hurley7bfe0b72013-06-15 09:36:08 -0400181 WARN_ON(atomic_sub_return(b->size, &buf->memory_used) < 0);
Alan Coxe0495732008-10-13 10:36:58 +0100182
Peter Hurley1cef50e2013-06-15 09:36:02 -0400183 if (b->size > MIN_TTYB_SIZE)
Alan Coxe0495732008-10-13 10:36:58 +0100184 kfree(b);
Peter Hurley7391ee12013-06-15 09:36:07 -0400185 else if (b->size > 0)
Peter Hurley809850b2013-06-15 09:36:06 -0400186 llist_add(&b->free, &buf->free);
Alan Coxe0495732008-10-13 10:36:58 +0100187}
188
189/**
Alan Coxe0495732008-10-13 10:36:58 +0100190 * tty_buffer_flush - flush full tty buffers
191 * @tty: tty to flush
192 *
193 * flush all the buffers containing receive data. If the buffer is
194 * being processed by flush_to_ldisc then we defer the processing
195 * to that function
196 *
Peter Hurleya7c8d582013-06-15 09:36:15 -0400197 * Locking: takes buffer lock to ensure single-threaded flip buffer
Peter Hurleye9975fd2013-06-15 09:36:10 -0400198 * 'consumer'
Alan Coxe0495732008-10-13 10:36:58 +0100199 */
200
201void tty_buffer_flush(struct tty_struct *tty)
202{
Jiri Slaby2fc20662012-10-18 22:26:44 +0200203 struct tty_port *port = tty->port;
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200204 struct tty_bufhead *buf = &port->buf;
Peter Hurley47aa6582013-06-15 09:36:14 -0400205 struct tty_buffer *next;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200206
Peter Hurleya7c8d582013-06-15 09:36:15 -0400207 atomic_inc(&buf->priority);
Peter Hurleye9975fd2013-06-15 09:36:10 -0400208
Peter Hurleya7c8d582013-06-15 09:36:15 -0400209 mutex_lock(&buf->lock);
Peter Hurley47aa6582013-06-15 09:36:14 -0400210 while ((next = buf->head->next) != NULL) {
211 tty_buffer_free(port, buf->head);
212 buf->head = next;
213 }
214 buf->head->read = buf->head->commit;
Peter Hurleya7c8d582013-06-15 09:36:15 -0400215 atomic_dec(&buf->priority);
216 mutex_unlock(&buf->lock);
Alan Coxe0495732008-10-13 10:36:58 +0100217}
218
219/**
Ilya Zykov64325a32013-01-19 18:16:20 +0400220 * tty_buffer_request_room - grow tty buffer if needed
Alan Coxe0495732008-10-13 10:36:58 +0100221 * @tty: tty structure
222 * @size: size desired
223 *
224 * Make at least size bytes of linear space available for the tty
225 * buffer. If we fail return the size we managed to find.
Alan Coxe0495732008-10-13 10:36:58 +0100226 */
Ilya Zykov64325a32013-01-19 18:16:20 +0400227int tty_buffer_request_room(struct tty_port *port, size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100228{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200229 struct tty_bufhead *buf = &port->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100230 struct tty_buffer *b, *n;
231 int left;
Peter Hurleye8437d72013-06-15 09:36:09 -0400232
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200233 b = buf->tail;
Peter Hurley7391ee12013-06-15 09:36:07 -0400234 left = b->size - b->used;
Alan Coxe0495732008-10-13 10:36:58 +0100235
236 if (left < size) {
237 /* This is the slow path - looking for new buffers to use */
Peter Hurley11b9faa2013-06-15 09:36:04 -0400238 if ((n = tty_buffer_alloc(port, size)) != NULL) {
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200239 buf->tail = n;
Peter Hurleye8437d72013-06-15 09:36:09 -0400240 b->commit = b->used;
241 smp_mb();
242 b->next = n;
Alan Coxe0495732008-10-13 10:36:58 +0100243 } else
244 size = left;
245 }
Alan Coxe0495732008-10-13 10:36:58 +0100246 return size;
247}
248EXPORT_SYMBOL_GPL(tty_buffer_request_room);
249
250/**
Alan Cox2832fc12010-02-18 16:43:54 +0000251 * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
Jiri Slaby2f693352013-01-03 15:53:02 +0100252 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100253 * @chars: characters
Alan Cox2832fc12010-02-18 16:43:54 +0000254 * @flag: flag value for each character
Alan Coxe0495732008-10-13 10:36:58 +0100255 * @size: size
256 *
257 * Queue a series of bytes to the tty buffering. All the characters
Johan Hovoldccc5ca82010-05-07 19:58:32 +0200258 * passed are marked with the supplied flag. Returns the number added.
Alan Coxe0495732008-10-13 10:36:58 +0100259 */
260
Jiri Slaby2f693352013-01-03 15:53:02 +0100261int tty_insert_flip_string_fixed_flag(struct tty_port *port,
Alan Cox2832fc12010-02-18 16:43:54 +0000262 const unsigned char *chars, char flag, size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100263{
264 int copied = 0;
265 do {
Fang Wenqid4bee0a2010-03-09 18:54:28 +0800266 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
Ilya Zykov64325a32013-01-19 18:16:20 +0400267 int space = tty_buffer_request_room(port, goal);
268 struct tty_buffer *tb = port->buf.tail;
Peter Hurley7391ee12013-06-15 09:36:07 -0400269 if (unlikely(space == 0))
Alan Coxe0495732008-10-13 10:36:58 +0100270 break;
Peter Hurley1fc359f2013-06-15 09:36:01 -0400271 memcpy(char_buf_ptr(tb, tb->used), chars, space);
272 memset(flag_buf_ptr(tb, tb->used), flag, space);
Alan Coxe0495732008-10-13 10:36:58 +0100273 tb->used += space;
274 copied += space;
275 chars += space;
276 /* There is a small chance that we need to split the data over
277 several buffers. If this is the case we must loop */
278 } while (unlikely(size > copied));
279 return copied;
280}
Alan Cox2832fc12010-02-18 16:43:54 +0000281EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
Alan Coxe0495732008-10-13 10:36:58 +0100282
283/**
284 * tty_insert_flip_string_flags - Add characters to the tty buffer
Jiri Slaby2f693352013-01-03 15:53:02 +0100285 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100286 * @chars: characters
287 * @flags: flag bytes
288 * @size: size
289 *
290 * Queue a series of bytes to the tty buffering. For each character
291 * the flags array indicates the status of the character. Returns the
292 * number added.
Alan Coxe0495732008-10-13 10:36:58 +0100293 */
294
Jiri Slaby2f693352013-01-03 15:53:02 +0100295int tty_insert_flip_string_flags(struct tty_port *port,
Alan Coxe0495732008-10-13 10:36:58 +0100296 const unsigned char *chars, const char *flags, size_t size)
297{
298 int copied = 0;
299 do {
Fang Wenqid4bee0a2010-03-09 18:54:28 +0800300 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
Ilya Zykov64325a32013-01-19 18:16:20 +0400301 int space = tty_buffer_request_room(port, goal);
302 struct tty_buffer *tb = port->buf.tail;
Peter Hurley7391ee12013-06-15 09:36:07 -0400303 if (unlikely(space == 0))
Alan Coxe0495732008-10-13 10:36:58 +0100304 break;
Peter Hurley1fc359f2013-06-15 09:36:01 -0400305 memcpy(char_buf_ptr(tb, tb->used), chars, space);
306 memcpy(flag_buf_ptr(tb, tb->used), flags, space);
Alan Coxe0495732008-10-13 10:36:58 +0100307 tb->used += space;
308 copied += space;
309 chars += space;
310 flags += space;
311 /* There is a small chance that we need to split the data over
312 several buffers. If this is the case we must loop */
313 } while (unlikely(size > copied));
314 return copied;
315}
316EXPORT_SYMBOL(tty_insert_flip_string_flags);
317
318/**
319 * tty_schedule_flip - push characters to ldisc
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100320 * @port: tty port to push from
Alan Coxe0495732008-10-13 10:36:58 +0100321 *
322 * Takes any pending buffers and transfers their ownership to the
323 * ldisc side of the queue. It then schedules those characters for
324 * processing by the line discipline.
Ivo Siebencee4ad12012-09-27 14:02:05 +0200325 * Note that this function can only be used when the low_latency flag
326 * is unset. Otherwise the workqueue won't be flushed.
Alan Coxe0495732008-10-13 10:36:58 +0100327 */
328
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100329void tty_schedule_flip(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100330{
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100331 struct tty_bufhead *buf = &port->buf;
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100332 WARN_ON(port->low_latency);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200333
Peter Hurley7391ee12013-06-15 09:36:07 -0400334 buf->tail->commit = buf->tail->used;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200335 schedule_work(&buf->work);
Alan Coxe0495732008-10-13 10:36:58 +0100336}
337EXPORT_SYMBOL(tty_schedule_flip);
338
339/**
340 * tty_prepare_flip_string - make room for characters
Jiri Slaby2f693352013-01-03 15:53:02 +0100341 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100342 * @chars: return pointer for character write area
343 * @size: desired size
344 *
345 * Prepare a block of space in the buffer for data. Returns the length
346 * available and buffer pointer to the space which is now allocated and
347 * accounted for as ready for normal characters. This is used for drivers
348 * that need their own block copy routines into the buffer. There is no
349 * guarantee the buffer is a DMA target!
Alan Coxe0495732008-10-13 10:36:58 +0100350 */
351
Jiri Slaby2f693352013-01-03 15:53:02 +0100352int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200353 size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100354{
Ilya Zykov64325a32013-01-19 18:16:20 +0400355 int space = tty_buffer_request_room(port, size);
Alan Coxe0495732008-10-13 10:36:58 +0100356 if (likely(space)) {
Ilya Zykov64325a32013-01-19 18:16:20 +0400357 struct tty_buffer *tb = port->buf.tail;
Peter Hurley1fc359f2013-06-15 09:36:01 -0400358 *chars = char_buf_ptr(tb, tb->used);
359 memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space);
Alan Coxe0495732008-10-13 10:36:58 +0100360 tb->used += space;
361 }
362 return space;
363}
364EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
365
366/**
367 * tty_prepare_flip_string_flags - make room for characters
Jiri Slaby2f693352013-01-03 15:53:02 +0100368 * @port: tty port
Alan Coxe0495732008-10-13 10:36:58 +0100369 * @chars: return pointer for character write area
370 * @flags: return pointer for status flag write area
371 * @size: desired size
372 *
373 * Prepare a block of space in the buffer for data. Returns the length
374 * available and buffer pointer to the space which is now allocated and
375 * accounted for as ready for characters. This is used for drivers
376 * that need their own block copy routines into the buffer. There is no
377 * guarantee the buffer is a DMA target!
Alan Coxe0495732008-10-13 10:36:58 +0100378 */
379
Jiri Slaby2f693352013-01-03 15:53:02 +0100380int tty_prepare_flip_string_flags(struct tty_port *port,
Alan Coxe0495732008-10-13 10:36:58 +0100381 unsigned char **chars, char **flags, size_t size)
382{
Ilya Zykov64325a32013-01-19 18:16:20 +0400383 int space = tty_buffer_request_room(port, size);
Alan Coxe0495732008-10-13 10:36:58 +0100384 if (likely(space)) {
Ilya Zykov64325a32013-01-19 18:16:20 +0400385 struct tty_buffer *tb = port->buf.tail;
Peter Hurley1fc359f2013-06-15 09:36:01 -0400386 *chars = char_buf_ptr(tb, tb->used);
387 *flags = flag_buf_ptr(tb, tb->used);
Alan Coxe0495732008-10-13 10:36:58 +0100388 tb->used += space;
389 }
390 return space;
391}
392EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
393
394
Peter Hurleyda261e72013-06-15 09:14:14 -0400395static int
396receive_buf(struct tty_struct *tty, struct tty_buffer *head, int count)
397{
398 struct tty_ldisc *disc = tty->ldisc;
Peter Hurley1fc359f2013-06-15 09:36:01 -0400399 unsigned char *p = char_buf_ptr(head, head->read);
400 char *f = flag_buf_ptr(head, head->read);
Peter Hurleyda261e72013-06-15 09:14:14 -0400401
Peter Hurley24a89d12013-06-15 09:14:15 -0400402 if (disc->ops->receive_buf2)
403 count = disc->ops->receive_buf2(tty, p, f, count);
404 else {
405 count = min_t(int, count, tty->receive_room);
406 if (count)
407 disc->ops->receive_buf(tty, p, f, count);
408 }
Peter Hurleyda261e72013-06-15 09:14:14 -0400409 head->read += count;
410 return count;
411}
Alan Coxe0495732008-10-13 10:36:58 +0100412
413/**
414 * flush_to_ldisc
415 * @work: tty structure passed from work queue.
416 *
417 * This routine is called out of the software interrupt to flush data
418 * from the buffer chain to the line discipline.
419 *
Peter Hurleye9975fd2013-06-15 09:36:10 -0400420 * The receive_buf method is single threaded for each tty instance.
421 *
Peter Hurleya7c8d582013-06-15 09:36:15 -0400422 * Locking: takes buffer lock to ensure single-threaded flip buffer
Peter Hurleye9975fd2013-06-15 09:36:10 -0400423 * 'consumer'
Alan Coxe0495732008-10-13 10:36:58 +0100424 */
425
426static void flush_to_ldisc(struct work_struct *work)
427{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200428 struct tty_port *port = container_of(work, struct tty_port, buf.work);
429 struct tty_bufhead *buf = &port->buf;
430 struct tty_struct *tty;
Alan Coxe0495732008-10-13 10:36:58 +0100431 struct tty_ldisc *disc;
Alan Coxe0495732008-10-13 10:36:58 +0100432
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200433 tty = port->itty;
Jiri Slaby34dcfb82013-02-27 22:30:24 +0100434 if (tty == NULL)
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200435 return;
436
Alan Coxe0495732008-10-13 10:36:58 +0100437 disc = tty_ldisc_ref(tty);
Peter Hurley36697522013-06-15 07:04:48 -0400438 if (disc == NULL)
Alan Coxe0495732008-10-13 10:36:58 +0100439 return;
440
Peter Hurleya7c8d582013-06-15 09:36:15 -0400441 mutex_lock(&buf->lock);
Linus Torvalds45242002009-10-14 08:59:49 -0700442
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400443 while (1) {
444 struct tty_buffer *head = buf->head;
445 int count;
Linus Torvalds45242002009-10-14 08:59:49 -0700446
Peter Hurleya7c8d582013-06-15 09:36:15 -0400447 /* Ldisc or user is trying to gain exclusive access */
448 if (atomic_read(&buf->priority))
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400449 break;
Peter Hurleye9975fd2013-06-15 09:36:10 -0400450
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400451 count = head->commit - head->read;
452 if (!count) {
453 if (head->next == NULL)
Peter Hurley39f610e2013-03-20 13:20:43 -0400454 break;
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400455 buf->head = head->next;
456 tty_buffer_free(port, head);
457 continue;
Alan Coxe0495732008-10-13 10:36:58 +0100458 }
Peter Hurleyd7a68be2013-06-15 09:36:11 -0400459
460 count = receive_buf(tty, head, count);
461 if (!count)
462 break;
Alan Coxe0495732008-10-13 10:36:58 +0100463 }
Linus Torvalds45242002009-10-14 08:59:49 -0700464
Peter Hurleya7c8d582013-06-15 09:36:15 -0400465 mutex_unlock(&buf->lock);
Alan Coxe0495732008-10-13 10:36:58 +0100466
467 tty_ldisc_deref(disc);
468}
469
470/**
OGAWA Hirofumie043e422009-07-29 12:15:56 -0700471 * tty_flush_to_ldisc
472 * @tty: tty to push
473 *
474 * Push the terminal flip buffers to the line discipline.
475 *
476 * Must not be called from IRQ context.
477 */
478void tty_flush_to_ldisc(struct tty_struct *tty)
479{
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100480 if (!tty->port->low_latency)
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200481 flush_work(&tty->port->buf.work);
OGAWA Hirofumie043e422009-07-29 12:15:56 -0700482}
483
484/**
Alan Coxe0495732008-10-13 10:36:58 +0100485 * tty_flip_buffer_push - terminal
Jiri Slaby2e124b42013-01-03 15:53:06 +0100486 * @port: tty port to push
Alan Coxe0495732008-10-13 10:36:58 +0100487 *
488 * Queue a push of the terminal flip buffers to the line discipline. This
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100489 * function must not be called from IRQ context if port->low_latency is
490 * set.
Alan Coxe0495732008-10-13 10:36:58 +0100491 *
492 * In the event of the queue being busy for flipping the work will be
493 * held off and retried later.
Alan Coxe0495732008-10-13 10:36:58 +0100494 */
495
Jiri Slaby2e124b42013-01-03 15:53:06 +0100496void tty_flip_buffer_push(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100497{
Jiri Slaby2e124b42013-01-03 15:53:06 +0100498 struct tty_bufhead *buf = &port->buf;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200499
Peter Hurley7391ee12013-06-15 09:36:07 -0400500 buf->tail->commit = buf->tail->used;
Alan Coxe0495732008-10-13 10:36:58 +0100501
Jiri Slaby2e124b42013-01-03 15:53:06 +0100502 if (port->low_latency)
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200503 flush_to_ldisc(&buf->work);
Alan Coxe0495732008-10-13 10:36:58 +0100504 else
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200505 schedule_work(&buf->work);
Alan Coxe0495732008-10-13 10:36:58 +0100506}
507EXPORT_SYMBOL(tty_flip_buffer_push);
508
509/**
510 * tty_buffer_init - prepare a tty buffer structure
511 * @tty: tty to initialise
512 *
513 * Set up the initial state of the buffer management for a tty device.
514 * Must be called before the other tty buffer functions are used.
Alan Coxe0495732008-10-13 10:36:58 +0100515 */
516
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200517void tty_buffer_init(struct tty_port *port)
Alan Coxe0495732008-10-13 10:36:58 +0100518{
Jiri Slabyecbbfd42012-10-18 22:26:47 +0200519 struct tty_bufhead *buf = &port->buf;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200520
Peter Hurleya7c8d582013-06-15 09:36:15 -0400521 mutex_init(&buf->lock);
Peter Hurley7391ee12013-06-15 09:36:07 -0400522 tty_buffer_reset(&buf->sentinel, 0);
523 buf->head = &buf->sentinel;
524 buf->tail = &buf->sentinel;
Peter Hurley809850b2013-06-15 09:36:06 -0400525 init_llist_head(&buf->free);
Peter Hurley7bfe0b72013-06-15 09:36:08 -0400526 atomic_set(&buf->memory_used, 0);
Peter Hurleya7c8d582013-06-15 09:36:15 -0400527 atomic_set(&buf->priority, 0);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200528 INIT_WORK(&buf->work, flush_to_ldisc);
Alan Coxe0495732008-10-13 10:36:58 +0100529}