2 * Tty buffer allocation management
5 #include <linux/types.h>
6 #include <linux/errno.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>
21 * tty_buffer_free_all - free buffers used by a tty
22 * @tty: tty to free from
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
30 void tty_buffer_free_all(struct tty_struct
*tty
)
32 struct tty_buffer
*thead
;
33 while ((thead
= tty
->buf
.head
) != NULL
) {
34 tty
->buf
.head
= thead
->next
;
37 while ((thead
= tty
->buf
.free
) != NULL
) {
38 tty
->buf
.free
= thead
->next
;
42 tty
->buf
.memory_used
= 0;
46 * tty_buffer_alloc - allocate a tty buffer
48 * @size: desired size (characters)
50 * Allocate a new tty buffer to hold the desired number of characters.
51 * Return NULL if out of memory or the allocation would exceed the
54 * Locking: Caller must hold tty->buf.lock
57 static struct tty_buffer
*tty_buffer_alloc(struct tty_struct
*tty
, size_t size
)
61 if (tty
->buf
.memory_used
+ size
> 65536)
63 p
= kmalloc(sizeof(struct tty_buffer
) + 2 * size
, GFP_ATOMIC
);
71 p
->char_buf_ptr
= (char *)(p
->data
);
72 p
->flag_buf_ptr
= (unsigned char *)p
->char_buf_ptr
+ size
;
73 tty
->buf
.memory_used
+= size
;
78 * tty_buffer_free - free a tty buffer
79 * @tty: tty owning the buffer
80 * @b: the buffer to free
82 * Free a tty buffer, or add it to the free list according to our
85 * Locking: Caller must hold tty->buf.lock
88 static void tty_buffer_free(struct tty_struct
*tty
, struct tty_buffer
*b
)
90 /* Dumb strategy for now - should keep some stats */
91 tty
->buf
.memory_used
-= b
->size
;
92 WARN_ON(tty
->buf
.memory_used
< 0);
97 b
->next
= tty
->buf
.free
;
103 * __tty_buffer_flush - flush full tty buffers
106 * flush all the buffers containing receive data. Caller must
107 * hold the buffer lock and must have ensured no parallel flush to
110 * Locking: Caller must hold tty->buf.lock
113 static void __tty_buffer_flush(struct tty_struct
*tty
)
115 struct tty_buffer
*thead
;
117 if (tty
->buf
.head
== NULL
)
119 while ((thead
= tty
->buf
.head
->next
) != NULL
) {
120 tty_buffer_free(tty
, tty
->buf
.head
);
121 tty
->buf
.head
= thead
;
123 WARN_ON(tty
->buf
.head
!= tty
->buf
.tail
);
124 tty
->buf
.head
->read
= tty
->buf
.head
->commit
;
128 * tty_buffer_flush - flush full tty buffers
131 * flush all the buffers containing receive data. If the buffer is
132 * being processed by flush_to_ldisc then we defer the processing
138 void tty_buffer_flush(struct tty_struct
*tty
)
141 spin_lock_irqsave(&tty
->buf
.lock
, flags
);
143 /* If the data is being pushed to the tty layer then we can't
144 process it here. Instead set a flag and the flush_to_ldisc
145 path will process the flush request before it exits */
146 if (test_bit(TTY_FLUSHING
, &tty
->flags
)) {
147 set_bit(TTY_FLUSHPENDING
, &tty
->flags
);
148 spin_unlock_irqrestore(&tty
->buf
.lock
, flags
);
149 wait_event(tty
->read_wait
,
150 test_bit(TTY_FLUSHPENDING
, &tty
->flags
) == 0);
153 __tty_buffer_flush(tty
);
154 spin_unlock_irqrestore(&tty
->buf
.lock
, flags
);
158 * tty_buffer_find - find a free tty buffer
159 * @tty: tty owning the buffer
160 * @size: characters wanted
162 * Locate an existing suitable tty buffer or if we are lacking one then
163 * allocate a new one. We round our buffers off in 256 character chunks
164 * to get better allocation behaviour.
166 * Locking: Caller must hold tty->buf.lock
169 static struct tty_buffer
*tty_buffer_find(struct tty_struct
*tty
, size_t size
)
171 struct tty_buffer
**tbh
= &tty
->buf
.free
;
172 while ((*tbh
) != NULL
) {
173 struct tty_buffer
*t
= *tbh
;
174 if (t
->size
>= size
) {
180 tty
->buf
.memory_used
+= t
->size
;
183 tbh
= &((*tbh
)->next
);
185 /* Round the buffer size out */
186 size
= (size
+ 0xFF) & ~0xFF;
187 return tty_buffer_alloc(tty
, size
);
188 /* Should possibly check if this fails for the largest buffer we
189 have queued and recycle that ? */
193 * tty_buffer_request_room - grow tty buffer if needed
194 * @tty: tty structure
195 * @size: size desired
197 * Make at least size bytes of linear space available for the tty
198 * buffer. If we fail return the size we managed to find.
200 * Locking: Takes tty->buf.lock
202 int tty_buffer_request_room(struct tty_struct
*tty
, size_t size
)
204 struct tty_buffer
*b
, *n
;
208 spin_lock_irqsave(&tty
->buf
.lock
, flags
);
210 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
211 remove this conditional if its worth it. This would be invisible
213 if ((b
= tty
->buf
.tail
) != NULL
)
214 left
= b
->size
- b
->used
;
219 /* This is the slow path - looking for new buffers to use */
220 if ((n
= tty_buffer_find(tty
, size
)) != NULL
) {
231 spin_unlock_irqrestore(&tty
->buf
.lock
, flags
);
234 EXPORT_SYMBOL_GPL(tty_buffer_request_room
);
237 * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
238 * @tty: tty structure
240 * @flag: flag value for each character
243 * Queue a series of bytes to the tty buffering. All the characters
244 * passed are marked with the supplied flag. Returns the number added.
246 * Locking: Called functions may take tty->buf.lock
249 int tty_insert_flip_string_fixed_flag(struct tty_struct
*tty
,
250 const unsigned char *chars
, char flag
, size_t size
)
254 int goal
= min_t(size_t, size
- copied
, TTY_BUFFER_PAGE
);
255 int space
= tty_buffer_request_room(tty
, goal
);
256 struct tty_buffer
*tb
= tty
->buf
.tail
;
257 /* If there is no space then tb may be NULL */
258 if (unlikely(space
== 0))
260 memcpy(tb
->char_buf_ptr
+ tb
->used
, chars
, space
);
261 memset(tb
->flag_buf_ptr
+ tb
->used
, flag
, space
);
265 /* There is a small chance that we need to split the data over
266 several buffers. If this is the case we must loop */
267 } while (unlikely(size
> copied
));
270 EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag
);
273 * tty_insert_flip_string_flags - Add characters to the tty buffer
274 * @tty: tty structure
279 * Queue a series of bytes to the tty buffering. For each character
280 * the flags array indicates the status of the character. Returns the
283 * Locking: Called functions may take tty->buf.lock
286 int tty_insert_flip_string_flags(struct tty_struct
*tty
,
287 const unsigned char *chars
, const char *flags
, size_t size
)
291 int goal
= min_t(size_t, size
- copied
, TTY_BUFFER_PAGE
);
292 int space
= tty_buffer_request_room(tty
, goal
);
293 struct tty_buffer
*tb
= tty
->buf
.tail
;
294 /* If there is no space then tb may be NULL */
295 if (unlikely(space
== 0))
297 memcpy(tb
->char_buf_ptr
+ tb
->used
, chars
, space
);
298 memcpy(tb
->flag_buf_ptr
+ tb
->used
, flags
, space
);
303 /* There is a small chance that we need to split the data over
304 several buffers. If this is the case we must loop */
305 } while (unlikely(size
> copied
));
308 EXPORT_SYMBOL(tty_insert_flip_string_flags
);
311 * tty_schedule_flip - push characters to ldisc
312 * @tty: tty to push from
314 * Takes any pending buffers and transfers their ownership to the
315 * ldisc side of the queue. It then schedules those characters for
316 * processing by the line discipline.
318 * Locking: Takes tty->buf.lock
321 void tty_schedule_flip(struct tty_struct
*tty
)
324 spin_lock_irqsave(&tty
->buf
.lock
, flags
);
325 if (tty
->buf
.tail
!= NULL
)
326 tty
->buf
.tail
->commit
= tty
->buf
.tail
->used
;
327 spin_unlock_irqrestore(&tty
->buf
.lock
, flags
);
328 schedule_work(&tty
->buf
.work
);
330 EXPORT_SYMBOL(tty_schedule_flip
);
333 * tty_prepare_flip_string - make room for characters
335 * @chars: return pointer for character write area
336 * @size: desired size
338 * Prepare a block of space in the buffer for data. Returns the length
339 * available and buffer pointer to the space which is now allocated and
340 * accounted for as ready for normal characters. This is used for drivers
341 * that need their own block copy routines into the buffer. There is no
342 * guarantee the buffer is a DMA target!
344 * Locking: May call functions taking tty->buf.lock
347 int tty_prepare_flip_string(struct tty_struct
*tty
, unsigned char **chars
,
350 int space
= tty_buffer_request_room(tty
, size
);
352 struct tty_buffer
*tb
= tty
->buf
.tail
;
353 *chars
= tb
->char_buf_ptr
+ tb
->used
;
354 memset(tb
->flag_buf_ptr
+ tb
->used
, TTY_NORMAL
, space
);
359 EXPORT_SYMBOL_GPL(tty_prepare_flip_string
);
362 * tty_prepare_flip_string_flags - make room for characters
364 * @chars: return pointer for character write area
365 * @flags: return pointer for status flag write area
366 * @size: desired size
368 * Prepare a block of space in the buffer for data. Returns the length
369 * available and buffer pointer to the space which is now allocated and
370 * accounted for as ready for characters. This is used for drivers
371 * that need their own block copy routines into the buffer. There is no
372 * guarantee the buffer is a DMA target!
374 * Locking: May call functions taking tty->buf.lock
377 int tty_prepare_flip_string_flags(struct tty_struct
*tty
,
378 unsigned char **chars
, char **flags
, size_t size
)
380 int space
= tty_buffer_request_room(tty
, size
);
382 struct tty_buffer
*tb
= tty
->buf
.tail
;
383 *chars
= tb
->char_buf_ptr
+ tb
->used
;
384 *flags
= tb
->flag_buf_ptr
+ tb
->used
;
389 EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags
);
395 * @work: tty structure passed from work queue.
397 * This routine is called out of the software interrupt to flush data
398 * from the buffer chain to the line discipline.
400 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
401 * while invoking the line discipline receive_buf method. The
402 * receive_buf method is single threaded for each tty instance.
405 static void flush_to_ldisc(struct work_struct
*work
)
407 struct tty_struct
*tty
=
408 container_of(work
, struct tty_struct
, buf
.work
);
410 struct tty_ldisc
*disc
;
412 disc
= tty_ldisc_ref(tty
);
413 if (disc
== NULL
) /* !TTY_LDISC */
416 spin_lock_irqsave(&tty
->buf
.lock
, flags
);
418 if (!test_and_set_bit(TTY_FLUSHING
, &tty
->flags
)) {
419 struct tty_buffer
*head
;
420 while ((head
= tty
->buf
.head
) != NULL
) {
423 unsigned char *flag_buf
;
425 count
= head
->commit
- head
->read
;
427 if (head
->next
== NULL
)
429 tty
->buf
.head
= head
->next
;
430 tty_buffer_free(tty
, head
);
433 /* Ldisc or user is trying to flush the buffers
434 we are feeding to the ldisc, stop feeding the
435 line discipline as we want to empty the queue */
436 if (test_bit(TTY_FLUSHPENDING
, &tty
->flags
))
438 if (!tty
->receive_room
)
440 if (count
> tty
->receive_room
)
441 count
= tty
->receive_room
;
442 char_buf
= head
->char_buf_ptr
+ head
->read
;
443 flag_buf
= head
->flag_buf_ptr
+ head
->read
;
445 spin_unlock_irqrestore(&tty
->buf
.lock
, flags
);
446 disc
->ops
->receive_buf(tty
, char_buf
,
448 spin_lock_irqsave(&tty
->buf
.lock
, flags
);
450 clear_bit(TTY_FLUSHING
, &tty
->flags
);
453 /* We may have a deferred request to flush the input buffer,
454 if so pull the chain under the lock and empty the queue */
455 if (test_bit(TTY_FLUSHPENDING
, &tty
->flags
)) {
456 __tty_buffer_flush(tty
);
457 clear_bit(TTY_FLUSHPENDING
, &tty
->flags
);
458 wake_up(&tty
->read_wait
);
460 spin_unlock_irqrestore(&tty
->buf
.lock
, flags
);
462 tty_ldisc_deref(disc
);
469 * Push the terminal flip buffers to the line discipline.
471 * Must not be called from IRQ context.
473 void tty_flush_to_ldisc(struct tty_struct
*tty
)
475 flush_work(&tty
->buf
.work
);
479 * tty_flip_buffer_push - terminal
482 * Queue a push of the terminal flip buffers to the line discipline. This
483 * function must not be called from IRQ context if tty->low_latency is set.
485 * In the event of the queue being busy for flipping the work will be
486 * held off and retried later.
488 * Locking: tty buffer lock. Driver locks in low latency mode.
491 void tty_flip_buffer_push(struct tty_struct
*tty
)
494 spin_lock_irqsave(&tty
->buf
.lock
, flags
);
495 if (tty
->buf
.tail
!= NULL
)
496 tty
->buf
.tail
->commit
= tty
->buf
.tail
->used
;
497 spin_unlock_irqrestore(&tty
->buf
.lock
, flags
);
499 if (tty
->low_latency
)
500 flush_to_ldisc(&tty
->buf
.work
);
502 schedule_work(&tty
->buf
.work
);
504 EXPORT_SYMBOL(tty_flip_buffer_push
);
507 * tty_buffer_init - prepare a tty buffer structure
508 * @tty: tty to initialise
510 * Set up the initial state of the buffer management for a tty device.
511 * Must be called before the other tty buffer functions are used.
516 void tty_buffer_init(struct tty_struct
*tty
)
518 spin_lock_init(&tty
->buf
.lock
);
519 tty
->buf
.head
= NULL
;
520 tty
->buf
.tail
= NULL
;
521 tty
->buf
.free
= NULL
;
522 tty
->buf
.memory_used
= 0;
523 INIT_WORK(&tty
->buf
.work
, flush_to_ldisc
);