blob: ddd74d41cbb2bff7eaf4fbe4112b735e384c72e9 [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>
19
20/**
21 * tty_buffer_free_all - free buffers used by a tty
22 * @tty: tty to free from
23 *
24 * Remove all the buffers pending on a tty whether queued with data
25 * or in the free ring. Must be called when the tty is no longer in use
26 *
27 * Locking: none
28 */
29
30void tty_buffer_free_all(struct tty_struct *tty)
31{
Jiri Slaby5cff39c2012-10-18 22:26:45 +020032 struct tty_bufhead *buf = &tty->buf;
Alan Coxe0495732008-10-13 10:36:58 +010033 struct tty_buffer *thead;
Jiri Slaby5cff39c2012-10-18 22:26:45 +020034
35 while ((thead = buf->head) != NULL) {
36 buf->head = thead->next;
Alan Coxe0495732008-10-13 10:36:58 +010037 kfree(thead);
38 }
Jiri Slaby5cff39c2012-10-18 22:26:45 +020039 while ((thead = buf->free) != NULL) {
40 buf->free = thead->next;
Alan Coxe0495732008-10-13 10:36:58 +010041 kfree(thead);
42 }
Jiri Slaby5cff39c2012-10-18 22:26:45 +020043 buf->tail = NULL;
44 buf->memory_used = 0;
Alan Coxe0495732008-10-13 10:36:58 +010045}
46
47/**
48 * tty_buffer_alloc - allocate a tty buffer
49 * @tty: tty device
50 * @size: desired size (characters)
51 *
52 * Allocate a new tty buffer to hold the desired number of characters.
53 * Return NULL if out of memory or the allocation would exceed the
54 * per device queue
55 *
56 * Locking: Caller must hold tty->buf.lock
57 */
58
59static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)
60{
61 struct tty_buffer *p;
62
63 if (tty->buf.memory_used + size > 65536)
64 return NULL;
65 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
66 if (p == NULL)
67 return NULL;
68 p->used = 0;
69 p->size = size;
70 p->next = NULL;
71 p->commit = 0;
72 p->read = 0;
73 p->char_buf_ptr = (char *)(p->data);
74 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
75 tty->buf.memory_used += size;
76 return p;
77}
78
79/**
80 * tty_buffer_free - free a tty buffer
81 * @tty: tty owning the buffer
82 * @b: the buffer to free
83 *
84 * Free a tty buffer, or add it to the free list according to our
85 * internal strategy
86 *
87 * Locking: Caller must hold tty->buf.lock
88 */
89
90static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
91{
Jiri Slaby5cff39c2012-10-18 22:26:45 +020092 struct tty_bufhead *buf = &tty->buf;
93
Alan Coxe0495732008-10-13 10:36:58 +010094 /* Dumb strategy for now - should keep some stats */
Jiri Slaby5cff39c2012-10-18 22:26:45 +020095 buf->memory_used -= b->size;
96 WARN_ON(buf->memory_used < 0);
Alan Coxe0495732008-10-13 10:36:58 +010097
98 if (b->size >= 512)
99 kfree(b);
100 else {
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200101 b->next = buf->free;
102 buf->free = b;
Alan Coxe0495732008-10-13 10:36:58 +0100103 }
104}
105
106/**
107 * __tty_buffer_flush - flush full tty buffers
108 * @tty: tty to flush
109 *
110 * flush all the buffers containing receive data. Caller must
111 * hold the buffer lock and must have ensured no parallel flush to
112 * ldisc is running.
113 *
114 * Locking: Caller must hold tty->buf.lock
115 */
116
117static void __tty_buffer_flush(struct tty_struct *tty)
118{
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200119 struct tty_bufhead *buf = &tty->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100120 struct tty_buffer *thead;
121
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200122 while ((thead = buf->head) != NULL) {
123 buf->head = thead->next;
Alan Coxe0495732008-10-13 10:36:58 +0100124 tty_buffer_free(tty, thead);
125 }
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200126 buf->tail = NULL;
Alan Coxe0495732008-10-13 10:36:58 +0100127}
128
129/**
130 * tty_buffer_flush - flush full tty buffers
131 * @tty: tty to flush
132 *
133 * flush all the buffers containing receive data. If the buffer is
134 * being processed by flush_to_ldisc then we defer the processing
135 * to that function
136 *
137 * Locking: none
138 */
139
140void tty_buffer_flush(struct tty_struct *tty)
141{
Jiri Slaby2fc20662012-10-18 22:26:44 +0200142 struct tty_port *port = tty->port;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200143 struct tty_bufhead *buf = &tty->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100144 unsigned long flags;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200145
146 spin_lock_irqsave(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100147
148 /* If the data is being pushed to the tty layer then we can't
149 process it here. Instead set a flag and the flush_to_ldisc
150 path will process the flush request before it exits */
Jiri Slaby2fc20662012-10-18 22:26:44 +0200151 if (test_bit(TTYP_FLUSHING, &port->iflags)) {
152 set_bit(TTYP_FLUSHPENDING, &port->iflags);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200153 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100154 wait_event(tty->read_wait,
Jiri Slaby2fc20662012-10-18 22:26:44 +0200155 test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0);
Alan Coxe0495732008-10-13 10:36:58 +0100156 return;
157 } else
158 __tty_buffer_flush(tty);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200159 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100160}
161
162/**
163 * tty_buffer_find - find a free tty buffer
164 * @tty: tty owning the buffer
165 * @size: characters wanted
166 *
167 * Locate an existing suitable tty buffer or if we are lacking one then
168 * allocate a new one. We round our buffers off in 256 character chunks
169 * to get better allocation behaviour.
170 *
171 * Locking: Caller must hold tty->buf.lock
172 */
173
174static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
175{
176 struct tty_buffer **tbh = &tty->buf.free;
177 while ((*tbh) != NULL) {
178 struct tty_buffer *t = *tbh;
179 if (t->size >= size) {
180 *tbh = t->next;
181 t->next = NULL;
182 t->used = 0;
183 t->commit = 0;
184 t->read = 0;
185 tty->buf.memory_used += t->size;
186 return t;
187 }
188 tbh = &((*tbh)->next);
189 }
190 /* Round the buffer size out */
191 size = (size + 0xFF) & ~0xFF;
192 return tty_buffer_alloc(tty, size);
193 /* Should possibly check if this fails for the largest buffer we
194 have queued and recycle that ? */
195}
Alan Coxe0495732008-10-13 10:36:58 +0100196/**
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000197 * __tty_buffer_request_room - grow tty buffer if needed
Alan Coxe0495732008-10-13 10:36:58 +0100198 * @tty: tty structure
199 * @size: size desired
200 *
201 * Make at least size bytes of linear space available for the tty
202 * buffer. If we fail return the size we managed to find.
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000203 * Locking: Caller must hold tty->buf.lock
Alan Coxe0495732008-10-13 10:36:58 +0100204 */
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000205static int __tty_buffer_request_room(struct tty_struct *tty, size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100206{
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200207 struct tty_bufhead *buf = &tty->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100208 struct tty_buffer *b, *n;
209 int left;
Alan Coxe0495732008-10-13 10:36:58 +0100210 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
211 remove this conditional if its worth it. This would be invisible
212 to the callers */
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200213 b = buf->tail;
214 if (b != NULL)
Alan Coxe0495732008-10-13 10:36:58 +0100215 left = b->size - b->used;
216 else
217 left = 0;
218
219 if (left < size) {
220 /* This is the slow path - looking for new buffers to use */
221 if ((n = tty_buffer_find(tty, size)) != NULL) {
222 if (b != NULL) {
223 b->next = n;
224 b->commit = b->used;
225 } else
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200226 buf->head = n;
227 buf->tail = n;
Alan Coxe0495732008-10-13 10:36:58 +0100228 } else
229 size = left;
230 }
231
Alan Coxe0495732008-10-13 10:36:58 +0100232 return size;
233}
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000234
235
236/**
237 * tty_buffer_request_room - grow tty buffer if needed
238 * @tty: tty structure
239 * @size: size desired
240 *
241 * Make at least size bytes of linear space available for the tty
242 * buffer. If we fail return the size we managed to find.
243 *
244 * Locking: Takes tty->buf.lock
245 */
246int tty_buffer_request_room(struct tty_struct *tty, size_t size)
247{
248 unsigned long flags;
249 int length;
250
251 spin_lock_irqsave(&tty->buf.lock, flags);
252 length = __tty_buffer_request_room(tty, size);
253 spin_unlock_irqrestore(&tty->buf.lock, flags);
254 return length;
255}
Alan Coxe0495732008-10-13 10:36:58 +0100256EXPORT_SYMBOL_GPL(tty_buffer_request_room);
257
258/**
Alan Cox2832fc12010-02-18 16:43:54 +0000259 * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
Alan Coxe0495732008-10-13 10:36:58 +0100260 * @tty: tty structure
261 * @chars: characters
Alan Cox2832fc12010-02-18 16:43:54 +0000262 * @flag: flag value for each character
Alan Coxe0495732008-10-13 10:36:58 +0100263 * @size: size
264 *
265 * Queue a series of bytes to the tty buffering. All the characters
Johan Hovoldccc5ca82010-05-07 19:58:32 +0200266 * passed are marked with the supplied flag. Returns the number added.
Alan Coxe0495732008-10-13 10:36:58 +0100267 *
268 * Locking: Called functions may take tty->buf.lock
269 */
270
Alan Cox2832fc12010-02-18 16:43:54 +0000271int tty_insert_flip_string_fixed_flag(struct tty_struct *tty,
272 const unsigned char *chars, char flag, size_t size)
Alan Coxe0495732008-10-13 10:36:58 +0100273{
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200274 struct tty_bufhead *buf = &tty->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100275 int copied = 0;
276 do {
Fang Wenqid4bee0a2010-03-09 18:54:28 +0800277 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000278 int space;
279 unsigned long flags;
280 struct tty_buffer *tb;
281
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200282 spin_lock_irqsave(&buf->lock, flags);
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000283 space = __tty_buffer_request_room(tty, goal);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200284 tb = buf->tail;
Alan Coxe0495732008-10-13 10:36:58 +0100285 /* If there is no space then tb may be NULL */
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000286 if (unlikely(space == 0)) {
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200287 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100288 break;
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000289 }
Alan Coxe0495732008-10-13 10:36:58 +0100290 memcpy(tb->char_buf_ptr + tb->used, chars, space);
Alan Cox2832fc12010-02-18 16:43:54 +0000291 memset(tb->flag_buf_ptr + tb->used, flag, space);
Alan Coxe0495732008-10-13 10:36:58 +0100292 tb->used += space;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200293 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100294 copied += space;
295 chars += space;
296 /* There is a small chance that we need to split the data over
297 several buffers. If this is the case we must loop */
298 } while (unlikely(size > copied));
299 return copied;
300}
Alan Cox2832fc12010-02-18 16:43:54 +0000301EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
Alan Coxe0495732008-10-13 10:36:58 +0100302
303/**
304 * tty_insert_flip_string_flags - Add characters to the tty buffer
305 * @tty: tty structure
306 * @chars: characters
307 * @flags: flag bytes
308 * @size: size
309 *
310 * Queue a series of bytes to the tty buffering. For each character
311 * the flags array indicates the status of the character. Returns the
312 * number added.
313 *
314 * Locking: Called functions may take tty->buf.lock
315 */
316
317int tty_insert_flip_string_flags(struct tty_struct *tty,
318 const unsigned char *chars, const char *flags, size_t size)
319{
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200320 struct tty_bufhead *buf = &tty->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100321 int copied = 0;
322 do {
Fang Wenqid4bee0a2010-03-09 18:54:28 +0800323 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000324 int space;
325 unsigned long __flags;
326 struct tty_buffer *tb;
327
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200328 spin_lock_irqsave(&buf->lock, __flags);
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000329 space = __tty_buffer_request_room(tty, goal);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200330 tb = buf->tail;
Alan Coxe0495732008-10-13 10:36:58 +0100331 /* If there is no space then tb may be NULL */
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000332 if (unlikely(space == 0)) {
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200333 spin_unlock_irqrestore(&buf->lock, __flags);
Alan Coxe0495732008-10-13 10:36:58 +0100334 break;
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000335 }
Alan Coxe0495732008-10-13 10:36:58 +0100336 memcpy(tb->char_buf_ptr + tb->used, chars, space);
337 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
338 tb->used += space;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200339 spin_unlock_irqrestore(&buf->lock, __flags);
Alan Coxe0495732008-10-13 10:36:58 +0100340 copied += space;
341 chars += space;
342 flags += space;
343 /* There is a small chance that we need to split the data over
344 several buffers. If this is the case we must loop */
345 } while (unlikely(size > copied));
346 return copied;
347}
348EXPORT_SYMBOL(tty_insert_flip_string_flags);
349
350/**
351 * tty_schedule_flip - push characters to ldisc
352 * @tty: tty to push from
353 *
354 * Takes any pending buffers and transfers their ownership to the
355 * ldisc side of the queue. It then schedules those characters for
356 * processing by the line discipline.
Ivo Siebencee4ad12012-09-27 14:02:05 +0200357 * Note that this function can only be used when the low_latency flag
358 * is unset. Otherwise the workqueue won't be flushed.
Alan Coxe0495732008-10-13 10:36:58 +0100359 *
360 * Locking: Takes tty->buf.lock
361 */
362
363void tty_schedule_flip(struct tty_struct *tty)
364{
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200365 struct tty_bufhead *buf = &tty->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100366 unsigned long flags;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200367
368 spin_lock_irqsave(&buf->lock, flags);
369 if (buf->tail != NULL)
370 buf->tail->commit = buf->tail->used;
371 spin_unlock_irqrestore(&buf->lock, flags);
372 schedule_work(&buf->work);
Alan Coxe0495732008-10-13 10:36:58 +0100373}
374EXPORT_SYMBOL(tty_schedule_flip);
375
376/**
377 * tty_prepare_flip_string - make room for characters
378 * @tty: tty
379 * @chars: return pointer for character 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 normal 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 *
388 * Locking: May call functions taking tty->buf.lock
389 */
390
391int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars,
392 size_t size)
393{
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200394 struct tty_bufhead *buf = &tty->buf;
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000395 int space;
396 unsigned long flags;
397 struct tty_buffer *tb;
398
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200399 spin_lock_irqsave(&buf->lock, flags);
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000400 space = __tty_buffer_request_room(tty, size);
401
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200402 tb = buf->tail;
Alan Coxe0495732008-10-13 10:36:58 +0100403 if (likely(space)) {
Alan Coxe0495732008-10-13 10:36:58 +0100404 *chars = tb->char_buf_ptr + tb->used;
405 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
406 tb->used += space;
407 }
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200408 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100409 return space;
410}
411EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
412
413/**
414 * tty_prepare_flip_string_flags - make room for characters
415 * @tty: tty
416 * @chars: return pointer for character write area
417 * @flags: return pointer for status flag write area
418 * @size: desired size
419 *
420 * Prepare a block of space in the buffer for data. Returns the length
421 * available and buffer pointer to the space which is now allocated and
422 * accounted for as ready for characters. This is used for drivers
423 * that need their own block copy routines into the buffer. There is no
424 * guarantee the buffer is a DMA target!
425 *
426 * Locking: May call functions taking tty->buf.lock
427 */
428
429int tty_prepare_flip_string_flags(struct tty_struct *tty,
430 unsigned char **chars, char **flags, size_t size)
431{
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200432 struct tty_bufhead *buf = &tty->buf;
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000433 int space;
434 unsigned long __flags;
435 struct tty_buffer *tb;
436
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200437 spin_lock_irqsave(&buf->lock, __flags);
Xiaobing Tuc56a00a2012-03-16 03:00:26 +0000438 space = __tty_buffer_request_room(tty, size);
439
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200440 tb = buf->tail;
Alan Coxe0495732008-10-13 10:36:58 +0100441 if (likely(space)) {
Alan Coxe0495732008-10-13 10:36:58 +0100442 *chars = tb->char_buf_ptr + tb->used;
443 *flags = tb->flag_buf_ptr + tb->used;
444 tb->used += space;
445 }
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200446 spin_unlock_irqrestore(&buf->lock, __flags);
Alan Coxe0495732008-10-13 10:36:58 +0100447 return space;
448}
449EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
450
451
452
453/**
454 * flush_to_ldisc
455 * @work: tty structure passed from work queue.
456 *
457 * This routine is called out of the software interrupt to flush data
458 * from the buffer chain to the line discipline.
459 *
460 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
461 * while invoking the line discipline receive_buf method. The
462 * receive_buf method is single threaded for each tty instance.
463 */
464
465static void flush_to_ldisc(struct work_struct *work)
466{
467 struct tty_struct *tty =
Linus Torvaldsf23eb2b2011-03-22 16:17:32 -0700468 container_of(work, struct tty_struct, buf.work);
Jiri Slaby2fc20662012-10-18 22:26:44 +0200469 struct tty_port *port = tty->port;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200470 struct tty_bufhead *buf = &tty->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100471 unsigned long flags;
472 struct tty_ldisc *disc;
Alan Coxe0495732008-10-13 10:36:58 +0100473
474 disc = tty_ldisc_ref(tty);
475 if (disc == NULL) /* !TTY_LDISC */
476 return;
477
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200478 spin_lock_irqsave(&buf->lock, flags);
Linus Torvalds45242002009-10-14 08:59:49 -0700479
Jiri Slaby2fc20662012-10-18 22:26:44 +0200480 if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) {
Linus Torvalds81de9162011-06-08 07:46:30 -0700481 struct tty_buffer *head;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200482 while ((head = buf->head) != NULL) {
Linus Torvalds45242002009-10-14 08:59:49 -0700483 int count;
484 char *char_buf;
485 unsigned char *flag_buf;
486
487 count = head->commit - head->read;
Alan Coxe0495732008-10-13 10:36:58 +0100488 if (!count) {
489 if (head->next == NULL)
490 break;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200491 buf->head = head->next;
Linus Torvalds45242002009-10-14 08:59:49 -0700492 tty_buffer_free(tty, head);
Alan Coxe0495732008-10-13 10:36:58 +0100493 continue;
494 }
495 /* Ldisc or user is trying to flush the buffers
496 we are feeding to the ldisc, stop feeding the
497 line discipline as we want to empty the queue */
Jiri Slaby2fc20662012-10-18 22:26:44 +0200498 if (test_bit(TTYP_FLUSHPENDING, &port->iflags))
Alan Coxe0495732008-10-13 10:36:58 +0100499 break;
Linus Torvalds81de9162011-06-08 07:46:30 -0700500 if (!tty->receive_room)
Linus Torvalds55db4c62011-06-04 06:33:24 +0900501 break;
502 if (count > tty->receive_room)
503 count = tty->receive_room;
Alan Coxe0495732008-10-13 10:36:58 +0100504 char_buf = head->char_buf_ptr + head->read;
505 flag_buf = head->flag_buf_ptr + head->read;
Linus Torvalds55db4c62011-06-04 06:33:24 +0900506 head->read += count;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200507 spin_unlock_irqrestore(&buf->lock, flags);
Linus Torvalds55db4c62011-06-04 06:33:24 +0900508 disc->ops->receive_buf(tty, char_buf,
Alan Coxe0495732008-10-13 10:36:58 +0100509 flag_buf, count);
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200510 spin_lock_irqsave(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100511 }
Jiri Slaby2fc20662012-10-18 22:26:44 +0200512 clear_bit(TTYP_FLUSHING, &port->iflags);
Alan Coxe0495732008-10-13 10:36:58 +0100513 }
Linus Torvalds45242002009-10-14 08:59:49 -0700514
Alan Coxe0495732008-10-13 10:36:58 +0100515 /* We may have a deferred request to flush the input buffer,
516 if so pull the chain under the lock and empty the queue */
Jiri Slaby2fc20662012-10-18 22:26:44 +0200517 if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) {
Alan Coxe0495732008-10-13 10:36:58 +0100518 __tty_buffer_flush(tty);
Jiri Slaby2fc20662012-10-18 22:26:44 +0200519 clear_bit(TTYP_FLUSHPENDING, &port->iflags);
Alan Coxe0495732008-10-13 10:36:58 +0100520 wake_up(&tty->read_wait);
521 }
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200522 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100523
524 tty_ldisc_deref(disc);
525}
526
527/**
OGAWA Hirofumie043e422009-07-29 12:15:56 -0700528 * tty_flush_to_ldisc
529 * @tty: tty to push
530 *
531 * Push the terminal flip buffers to the line discipline.
532 *
533 * Must not be called from IRQ context.
534 */
535void tty_flush_to_ldisc(struct tty_struct *tty)
536{
Ivo Siebencee4ad12012-09-27 14:02:05 +0200537 if (!tty->low_latency)
538 flush_work(&tty->buf.work);
OGAWA Hirofumie043e422009-07-29 12:15:56 -0700539}
540
541/**
Alan Coxe0495732008-10-13 10:36:58 +0100542 * tty_flip_buffer_push - terminal
543 * @tty: tty to push
544 *
545 * Queue a push of the terminal flip buffers to the line discipline. This
546 * function must not be called from IRQ context if tty->low_latency is set.
547 *
548 * In the event of the queue being busy for flipping the work will be
549 * held off and retried later.
550 *
551 * Locking: tty buffer lock. Driver locks in low latency mode.
552 */
553
554void tty_flip_buffer_push(struct tty_struct *tty)
555{
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200556 struct tty_bufhead *buf = &tty->buf;
Alan Coxe0495732008-10-13 10:36:58 +0100557 unsigned long flags;
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200558
559 spin_lock_irqsave(&buf->lock, flags);
560 if (buf->tail != NULL)
561 buf->tail->commit = buf->tail->used;
562 spin_unlock_irqrestore(&buf->lock, flags);
Alan Coxe0495732008-10-13 10:36:58 +0100563
564 if (tty->low_latency)
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200565 flush_to_ldisc(&buf->work);
Alan Coxe0495732008-10-13 10:36:58 +0100566 else
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200567 schedule_work(&buf->work);
Alan Coxe0495732008-10-13 10:36:58 +0100568}
569EXPORT_SYMBOL(tty_flip_buffer_push);
570
571/**
572 * tty_buffer_init - prepare a tty buffer structure
573 * @tty: tty to initialise
574 *
575 * Set up the initial state of the buffer management for a tty device.
576 * Must be called before the other tty buffer functions are used.
577 *
578 * Locking: none
579 */
580
581void tty_buffer_init(struct tty_struct *tty)
582{
Jiri Slaby5cff39c2012-10-18 22:26:45 +0200583 struct tty_bufhead *buf = &tty->buf;
584
585 spin_lock_init(&buf->lock);
586 buf->head = NULL;
587 buf->tail = NULL;
588 buf->free = NULL;
589 buf->memory_used = 0;
590 INIT_WORK(&buf->work, flush_to_ldisc);
Alan Coxe0495732008-10-13 10:36:58 +0100591}
592