Commit | Line | Data |
---|---|---|
e0495736 AC |
1 | /* |
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 | ||
ecbbfd44 | 30 | void tty_buffer_free_all(struct tty_port *port) |
e0495736 | 31 | { |
ecbbfd44 | 32 | struct tty_bufhead *buf = &port->buf; |
e0495736 | 33 | struct tty_buffer *thead; |
5cff39c6 JS |
34 | |
35 | while ((thead = buf->head) != NULL) { | |
36 | buf->head = thead->next; | |
e0495736 AC |
37 | kfree(thead); |
38 | } | |
5cff39c6 JS |
39 | while ((thead = buf->free) != NULL) { |
40 | buf->free = thead->next; | |
e0495736 AC |
41 | kfree(thead); |
42 | } | |
5cff39c6 JS |
43 | buf->tail = NULL; |
44 | buf->memory_used = 0; | |
e0495736 AC |
45 | } |
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 | ||
ecbbfd44 | 59 | static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size) |
e0495736 AC |
60 | { |
61 | struct tty_buffer *p; | |
62 | ||
ecbbfd44 | 63 | if (port->buf.memory_used + size > 65536) |
e0495736 AC |
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; | |
ecbbfd44 | 75 | port->buf.memory_used += size; |
e0495736 AC |
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 | ||
ecbbfd44 | 90 | static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b) |
e0495736 | 91 | { |
ecbbfd44 | 92 | struct tty_bufhead *buf = &port->buf; |
5cff39c6 | 93 | |
e0495736 | 94 | /* Dumb strategy for now - should keep some stats */ |
5cff39c6 JS |
95 | buf->memory_used -= b->size; |
96 | WARN_ON(buf->memory_used < 0); | |
e0495736 AC |
97 | |
98 | if (b->size >= 512) | |
99 | kfree(b); | |
100 | else { | |
5cff39c6 JS |
101 | b->next = buf->free; |
102 | buf->free = b; | |
e0495736 AC |
103 | } |
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 | ||
ecbbfd44 | 117 | static void __tty_buffer_flush(struct tty_port *port) |
e0495736 | 118 | { |
ecbbfd44 | 119 | struct tty_bufhead *buf = &port->buf; |
e0495736 AC |
120 | struct tty_buffer *thead; |
121 | ||
5cff39c6 JS |
122 | while ((thead = buf->head) != NULL) { |
123 | buf->head = thead->next; | |
ecbbfd44 | 124 | tty_buffer_free(port, thead); |
e0495736 | 125 | } |
5cff39c6 | 126 | buf->tail = NULL; |
e0495736 AC |
127 | } |
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 | ||
140 | void tty_buffer_flush(struct tty_struct *tty) | |
141 | { | |
2fc20661 | 142 | struct tty_port *port = tty->port; |
ecbbfd44 | 143 | struct tty_bufhead *buf = &port->buf; |
e0495736 | 144 | unsigned long flags; |
5cff39c6 JS |
145 | |
146 | spin_lock_irqsave(&buf->lock, flags); | |
e0495736 AC |
147 | |
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 */ | |
2fc20661 JS |
151 | if (test_bit(TTYP_FLUSHING, &port->iflags)) { |
152 | set_bit(TTYP_FLUSHPENDING, &port->iflags); | |
5cff39c6 | 153 | spin_unlock_irqrestore(&buf->lock, flags); |
e0495736 | 154 | wait_event(tty->read_wait, |
2fc20661 | 155 | test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0); |
e0495736 AC |
156 | return; |
157 | } else | |
ecbbfd44 | 158 | __tty_buffer_flush(port); |
5cff39c6 | 159 | spin_unlock_irqrestore(&buf->lock, flags); |
e0495736 AC |
160 | } |
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 | ||
ecbbfd44 | 174 | static struct tty_buffer *tty_buffer_find(struct tty_port *port, size_t size) |
e0495736 | 175 | { |
ecbbfd44 | 176 | struct tty_buffer **tbh = &port->buf.free; |
e0495736 AC |
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; | |
ecbbfd44 | 185 | port->buf.memory_used += t->size; |
e0495736 AC |
186 | return t; |
187 | } | |
188 | tbh = &((*tbh)->next); | |
189 | } | |
190 | /* Round the buffer size out */ | |
191 | size = (size + 0xFF) & ~0xFF; | |
ecbbfd44 | 192 | return tty_buffer_alloc(port, size); |
e0495736 AC |
193 | /* Should possibly check if this fails for the largest buffer we |
194 | have queued and recycle that ? */ | |
195 | } | |
e0495736 | 196 | /** |
c56a00a1 | 197 | * __tty_buffer_request_room - grow tty buffer if needed |
e0495736 AC |
198 | * @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. | |
ecbbfd44 | 203 | * Locking: Caller must hold port->buf.lock |
e0495736 | 204 | */ |
ecbbfd44 | 205 | static int __tty_buffer_request_room(struct tty_port *port, size_t size) |
e0495736 | 206 | { |
ecbbfd44 | 207 | struct tty_bufhead *buf = &port->buf; |
e0495736 AC |
208 | struct tty_buffer *b, *n; |
209 | int left; | |
e0495736 AC |
210 | /* 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 */ | |
5cff39c6 JS |
213 | b = buf->tail; |
214 | if (b != NULL) | |
e0495736 AC |
215 | 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 */ | |
ecbbfd44 | 221 | if ((n = tty_buffer_find(port, size)) != NULL) { |
e0495736 AC |
222 | if (b != NULL) { |
223 | b->next = n; | |
224 | b->commit = b->used; | |
225 | } else | |
5cff39c6 JS |
226 | buf->head = n; |
227 | buf->tail = n; | |
e0495736 AC |
228 | } else |
229 | size = left; | |
230 | } | |
231 | ||
e0495736 AC |
232 | return size; |
233 | } | |
c56a00a1 XT |
234 | |
235 | ||
236 | /** | |
237 | * tty_buffer_request_room - grow tty buffer if needed | |
227434f8 | 238 | * @port: tty port structure |
c56a00a1 XT |
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 | * | |
ecbbfd44 | 244 | * Locking: Takes port->buf.lock |
c56a00a1 | 245 | */ |
227434f8 | 246 | int tty_buffer_request_room(struct tty_port *port, size_t size) |
c56a00a1 XT |
247 | { |
248 | unsigned long flags; | |
249 | int length; | |
250 | ||
ecbbfd44 JS |
251 | spin_lock_irqsave(&port->buf.lock, flags); |
252 | length = __tty_buffer_request_room(port, size); | |
253 | spin_unlock_irqrestore(&port->buf.lock, flags); | |
c56a00a1 XT |
254 | return length; |
255 | } | |
e0495736 AC |
256 | EXPORT_SYMBOL_GPL(tty_buffer_request_room); |
257 | ||
258 | /** | |
2832fc11 | 259 | * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer |
2f693357 | 260 | * @port: tty port |
e0495736 | 261 | * @chars: characters |
2832fc11 | 262 | * @flag: flag value for each character |
e0495736 AC |
263 | * @size: size |
264 | * | |
265 | * Queue a series of bytes to the tty buffering. All the characters | |
ccc5ca8d | 266 | * passed are marked with the supplied flag. Returns the number added. |
e0495736 | 267 | * |
ecbbfd44 | 268 | * Locking: Called functions may take port->buf.lock |
e0495736 AC |
269 | */ |
270 | ||
2f693357 | 271 | int tty_insert_flip_string_fixed_flag(struct tty_port *port, |
2832fc11 | 272 | const unsigned char *chars, char flag, size_t size) |
e0495736 | 273 | { |
2f693357 | 274 | struct tty_bufhead *buf = &port->buf; |
e0495736 AC |
275 | int copied = 0; |
276 | do { | |
d4bee0a6 | 277 | int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); |
c56a00a1 XT |
278 | int space; |
279 | unsigned long flags; | |
280 | struct tty_buffer *tb; | |
281 | ||
5cff39c6 | 282 | spin_lock_irqsave(&buf->lock, flags); |
2f693357 | 283 | space = __tty_buffer_request_room(port, goal); |
5cff39c6 | 284 | tb = buf->tail; |
e0495736 | 285 | /* If there is no space then tb may be NULL */ |
c56a00a1 | 286 | if (unlikely(space == 0)) { |
5cff39c6 | 287 | spin_unlock_irqrestore(&buf->lock, flags); |
e0495736 | 288 | break; |
c56a00a1 | 289 | } |
e0495736 | 290 | memcpy(tb->char_buf_ptr + tb->used, chars, space); |
2832fc11 | 291 | memset(tb->flag_buf_ptr + tb->used, flag, space); |
e0495736 | 292 | tb->used += space; |
5cff39c6 | 293 | spin_unlock_irqrestore(&buf->lock, flags); |
e0495736 AC |
294 | 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 | } | |
2832fc11 | 301 | EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag); |
e0495736 AC |
302 | |
303 | /** | |
304 | * tty_insert_flip_string_flags - Add characters to the tty buffer | |
2f693357 | 305 | * @port: tty port |
e0495736 AC |
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 | * | |
ecbbfd44 | 314 | * Locking: Called functions may take port->buf.lock |
e0495736 AC |
315 | */ |
316 | ||
2f693357 | 317 | int tty_insert_flip_string_flags(struct tty_port *port, |
e0495736 AC |
318 | const unsigned char *chars, const char *flags, size_t size) |
319 | { | |
2f693357 | 320 | struct tty_bufhead *buf = &port->buf; |
e0495736 AC |
321 | int copied = 0; |
322 | do { | |
d4bee0a6 | 323 | int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); |
c56a00a1 XT |
324 | int space; |
325 | unsigned long __flags; | |
326 | struct tty_buffer *tb; | |
327 | ||
5cff39c6 | 328 | spin_lock_irqsave(&buf->lock, __flags); |
2f693357 | 329 | space = __tty_buffer_request_room(port, goal); |
5cff39c6 | 330 | tb = buf->tail; |
e0495736 | 331 | /* If there is no space then tb may be NULL */ |
c56a00a1 | 332 | if (unlikely(space == 0)) { |
5cff39c6 | 333 | spin_unlock_irqrestore(&buf->lock, __flags); |
e0495736 | 334 | break; |
c56a00a1 | 335 | } |
e0495736 AC |
336 | memcpy(tb->char_buf_ptr + tb->used, chars, space); |
337 | memcpy(tb->flag_buf_ptr + tb->used, flags, space); | |
338 | tb->used += space; | |
5cff39c6 | 339 | spin_unlock_irqrestore(&buf->lock, __flags); |
e0495736 AC |
340 | 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 | } | |
348 | EXPORT_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. | |
cee4ad1e IS |
357 | * Note that this function can only be used when the low_latency flag |
358 | * is unset. Otherwise the workqueue won't be flushed. | |
e0495736 | 359 | * |
ecbbfd44 | 360 | * Locking: Takes port->buf.lock |
e0495736 AC |
361 | */ |
362 | ||
363 | void tty_schedule_flip(struct tty_struct *tty) | |
364 | { | |
ecbbfd44 | 365 | struct tty_bufhead *buf = &tty->port->buf; |
e0495736 | 366 | unsigned long flags; |
b8b345ba | 367 | WARN_ON(tty->low_latency); |
5cff39c6 JS |
368 | |
369 | spin_lock_irqsave(&buf->lock, flags); | |
370 | if (buf->tail != NULL) | |
371 | buf->tail->commit = buf->tail->used; | |
372 | spin_unlock_irqrestore(&buf->lock, flags); | |
373 | schedule_work(&buf->work); | |
e0495736 AC |
374 | } |
375 | EXPORT_SYMBOL(tty_schedule_flip); | |
376 | ||
377 | /** | |
378 | * tty_prepare_flip_string - make room for characters | |
2f693357 | 379 | * @port: tty port |
e0495736 AC |
380 | * @chars: return pointer for character write area |
381 | * @size: desired size | |
382 | * | |
383 | * Prepare a block of space in the buffer for data. Returns the length | |
384 | * available and buffer pointer to the space which is now allocated and | |
385 | * accounted for as ready for normal characters. This is used for drivers | |
386 | * that need their own block copy routines into the buffer. There is no | |
387 | * guarantee the buffer is a DMA target! | |
388 | * | |
ecbbfd44 | 389 | * Locking: May call functions taking port->buf.lock |
e0495736 AC |
390 | */ |
391 | ||
2f693357 | 392 | int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars, |
ecbbfd44 | 393 | size_t size) |
e0495736 | 394 | { |
2f693357 | 395 | struct tty_bufhead *buf = &port->buf; |
c56a00a1 XT |
396 | int space; |
397 | unsigned long flags; | |
398 | struct tty_buffer *tb; | |
399 | ||
5cff39c6 | 400 | spin_lock_irqsave(&buf->lock, flags); |
2f693357 | 401 | space = __tty_buffer_request_room(port, size); |
c56a00a1 | 402 | |
5cff39c6 | 403 | tb = buf->tail; |
e0495736 | 404 | if (likely(space)) { |
e0495736 AC |
405 | *chars = tb->char_buf_ptr + tb->used; |
406 | memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space); | |
407 | tb->used += space; | |
408 | } | |
5cff39c6 | 409 | spin_unlock_irqrestore(&buf->lock, flags); |
e0495736 AC |
410 | return space; |
411 | } | |
412 | EXPORT_SYMBOL_GPL(tty_prepare_flip_string); | |
413 | ||
414 | /** | |
415 | * tty_prepare_flip_string_flags - make room for characters | |
2f693357 | 416 | * @port: tty port |
e0495736 AC |
417 | * @chars: return pointer for character write area |
418 | * @flags: return pointer for status flag write area | |
419 | * @size: desired size | |
420 | * | |
421 | * Prepare a block of space in the buffer for data. Returns the length | |
422 | * available and buffer pointer to the space which is now allocated and | |
423 | * accounted for as ready for characters. This is used for drivers | |
424 | * that need their own block copy routines into the buffer. There is no | |
425 | * guarantee the buffer is a DMA target! | |
426 | * | |
ecbbfd44 | 427 | * Locking: May call functions taking port->buf.lock |
e0495736 AC |
428 | */ |
429 | ||
2f693357 | 430 | int tty_prepare_flip_string_flags(struct tty_port *port, |
e0495736 AC |
431 | unsigned char **chars, char **flags, size_t size) |
432 | { | |
2f693357 | 433 | struct tty_bufhead *buf = &port->buf; |
c56a00a1 XT |
434 | int space; |
435 | unsigned long __flags; | |
436 | struct tty_buffer *tb; | |
437 | ||
5cff39c6 | 438 | spin_lock_irqsave(&buf->lock, __flags); |
2f693357 | 439 | space = __tty_buffer_request_room(port, size); |
c56a00a1 | 440 | |
5cff39c6 | 441 | tb = buf->tail; |
e0495736 | 442 | if (likely(space)) { |
e0495736 AC |
443 | *chars = tb->char_buf_ptr + tb->used; |
444 | *flags = tb->flag_buf_ptr + tb->used; | |
445 | tb->used += space; | |
446 | } | |
5cff39c6 | 447 | spin_unlock_irqrestore(&buf->lock, __flags); |
e0495736 AC |
448 | return space; |
449 | } | |
450 | EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags); | |
451 | ||
452 | ||
453 | ||
454 | /** | |
455 | * flush_to_ldisc | |
456 | * @work: tty structure passed from work queue. | |
457 | * | |
458 | * This routine is called out of the software interrupt to flush data | |
459 | * from the buffer chain to the line discipline. | |
460 | * | |
461 | * Locking: holds tty->buf.lock to guard buffer list. Drops the lock | |
462 | * while invoking the line discipline receive_buf method. The | |
463 | * receive_buf method is single threaded for each tty instance. | |
464 | */ | |
465 | ||
466 | static void flush_to_ldisc(struct work_struct *work) | |
467 | { | |
ecbbfd44 JS |
468 | struct tty_port *port = container_of(work, struct tty_port, buf.work); |
469 | struct tty_bufhead *buf = &port->buf; | |
470 | struct tty_struct *tty; | |
e0495736 AC |
471 | unsigned long flags; |
472 | struct tty_ldisc *disc; | |
e0495736 | 473 | |
ecbbfd44 | 474 | tty = port->itty; |
cadf7486 | 475 | if (WARN_RATELIMIT(tty == NULL, "tty is NULL\n")) |
ecbbfd44 JS |
476 | return; |
477 | ||
e0495736 AC |
478 | disc = tty_ldisc_ref(tty); |
479 | if (disc == NULL) /* !TTY_LDISC */ | |
480 | return; | |
481 | ||
5cff39c6 | 482 | spin_lock_irqsave(&buf->lock, flags); |
45242006 | 483 | |
2fc20661 | 484 | if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) { |
81de916f | 485 | struct tty_buffer *head; |
5cff39c6 | 486 | while ((head = buf->head) != NULL) { |
45242006 LT |
487 | int count; |
488 | char *char_buf; | |
489 | unsigned char *flag_buf; | |
490 | ||
491 | count = head->commit - head->read; | |
e0495736 AC |
492 | if (!count) { |
493 | if (head->next == NULL) | |
494 | break; | |
5cff39c6 | 495 | buf->head = head->next; |
ecbbfd44 | 496 | tty_buffer_free(port, head); |
e0495736 AC |
497 | continue; |
498 | } | |
499 | /* Ldisc or user is trying to flush the buffers | |
500 | we are feeding to the ldisc, stop feeding the | |
501 | line discipline as we want to empty the queue */ | |
2fc20661 | 502 | if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) |
e0495736 | 503 | break; |
81de916f | 504 | if (!tty->receive_room) |
55db4c64 LT |
505 | break; |
506 | if (count > tty->receive_room) | |
507 | count = tty->receive_room; | |
e0495736 AC |
508 | char_buf = head->char_buf_ptr + head->read; |
509 | flag_buf = head->flag_buf_ptr + head->read; | |
55db4c64 | 510 | head->read += count; |
5cff39c6 | 511 | spin_unlock_irqrestore(&buf->lock, flags); |
55db4c64 | 512 | disc->ops->receive_buf(tty, char_buf, |
e0495736 | 513 | flag_buf, count); |
5cff39c6 | 514 | spin_lock_irqsave(&buf->lock, flags); |
e0495736 | 515 | } |
2fc20661 | 516 | clear_bit(TTYP_FLUSHING, &port->iflags); |
e0495736 | 517 | } |
45242006 | 518 | |
e0495736 AC |
519 | /* We may have a deferred request to flush the input buffer, |
520 | if so pull the chain under the lock and empty the queue */ | |
2fc20661 | 521 | if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) { |
ecbbfd44 | 522 | __tty_buffer_flush(port); |
2fc20661 | 523 | clear_bit(TTYP_FLUSHPENDING, &port->iflags); |
e0495736 AC |
524 | wake_up(&tty->read_wait); |
525 | } | |
5cff39c6 | 526 | spin_unlock_irqrestore(&buf->lock, flags); |
e0495736 AC |
527 | |
528 | tty_ldisc_deref(disc); | |
529 | } | |
530 | ||
e043e42b OH |
531 | /** |
532 | * tty_flush_to_ldisc | |
533 | * @tty: tty to push | |
534 | * | |
535 | * Push the terminal flip buffers to the line discipline. | |
536 | * | |
537 | * Must not be called from IRQ context. | |
538 | */ | |
539 | void tty_flush_to_ldisc(struct tty_struct *tty) | |
540 | { | |
cee4ad1e | 541 | if (!tty->low_latency) |
ecbbfd44 | 542 | flush_work(&tty->port->buf.work); |
e043e42b OH |
543 | } |
544 | ||
e0495736 AC |
545 | /** |
546 | * tty_flip_buffer_push - terminal | |
547 | * @tty: tty to push | |
548 | * | |
549 | * Queue a push of the terminal flip buffers to the line discipline. This | |
550 | * function must not be called from IRQ context if tty->low_latency is set. | |
551 | * | |
552 | * In the event of the queue being busy for flipping the work will be | |
553 | * held off and retried later. | |
554 | * | |
555 | * Locking: tty buffer lock. Driver locks in low latency mode. | |
556 | */ | |
557 | ||
558 | void tty_flip_buffer_push(struct tty_struct *tty) | |
559 | { | |
ecbbfd44 | 560 | struct tty_bufhead *buf = &tty->port->buf; |
e0495736 | 561 | unsigned long flags; |
5cff39c6 JS |
562 | |
563 | spin_lock_irqsave(&buf->lock, flags); | |
564 | if (buf->tail != NULL) | |
565 | buf->tail->commit = buf->tail->used; | |
566 | spin_unlock_irqrestore(&buf->lock, flags); | |
e0495736 AC |
567 | |
568 | if (tty->low_latency) | |
5cff39c6 | 569 | flush_to_ldisc(&buf->work); |
e0495736 | 570 | else |
5cff39c6 | 571 | schedule_work(&buf->work); |
e0495736 AC |
572 | } |
573 | EXPORT_SYMBOL(tty_flip_buffer_push); | |
574 | ||
575 | /** | |
576 | * tty_buffer_init - prepare a tty buffer structure | |
577 | * @tty: tty to initialise | |
578 | * | |
579 | * Set up the initial state of the buffer management for a tty device. | |
580 | * Must be called before the other tty buffer functions are used. | |
581 | * | |
582 | * Locking: none | |
583 | */ | |
584 | ||
ecbbfd44 | 585 | void tty_buffer_init(struct tty_port *port) |
e0495736 | 586 | { |
ecbbfd44 | 587 | struct tty_bufhead *buf = &port->buf; |
5cff39c6 JS |
588 | |
589 | spin_lock_init(&buf->lock); | |
590 | buf->head = NULL; | |
591 | buf->tail = NULL; | |
592 | buf->free = NULL; | |
593 | buf->memory_used = 0; | |
594 | INIT_WORK(&buf->work, flush_to_ldisc); | |
e0495736 AC |
595 | } |
596 |