1
2
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#include <linux/ratelimit.h>
20
21
22
23
24
25
26
27
28
29
30
31void tty_buffer_free_all(struct tty_port *port)
32{
33 struct tty_bufhead *buf = &port->buf;
34 struct tty_buffer *thead;
35
36 while ((thead = buf->head) != NULL) {
37 buf->head = thead->next;
38 kfree(thead);
39 }
40 while ((thead = buf->free) != NULL) {
41 buf->free = thead->next;
42 kfree(thead);
43 }
44 buf->tail = NULL;
45 buf->memory_used = 0;
46}
47
48
49
50
51
52
53
54
55
56
57
58
59
60static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
61{
62 struct tty_buffer *p;
63
64 if (port->buf.memory_used + size > 65536)
65 return NULL;
66 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
67 if (p == NULL)
68 return NULL;
69 p->used = 0;
70 p->size = size;
71 p->next = NULL;
72 p->commit = 0;
73 p->read = 0;
74 p->char_buf_ptr = (char *)(p->data);
75 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
76 port->buf.memory_used += size;
77 return p;
78}
79
80
81
82
83
84
85
86
87
88
89
90
91static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
92{
93 struct tty_bufhead *buf = &port->buf;
94
95
96 buf->memory_used -= b->size;
97 WARN_ON(buf->memory_used < 0);
98
99 if (b->size >= 512)
100 kfree(b);
101 else {
102 b->next = buf->free;
103 buf->free = b;
104 }
105}
106
107
108
109
110
111
112
113
114
115
116
117
118static void __tty_buffer_flush(struct tty_port *port)
119{
120 struct tty_bufhead *buf = &port->buf;
121 struct tty_buffer *thead;
122
123 if (unlikely(buf->head == NULL))
124 return;
125 while ((thead = buf->head->next) != NULL) {
126 tty_buffer_free(port, buf->head);
127 buf->head = thead;
128 }
129 WARN_ON(buf->head != buf->tail);
130 buf->head->read = buf->head->commit;
131}
132
133
134
135
136
137
138
139
140
141
142
143
144void tty_buffer_flush(struct tty_struct *tty)
145{
146 struct tty_port *port = tty->port;
147 struct tty_bufhead *buf = &port->buf;
148 unsigned long flags;
149
150 spin_lock_irqsave(&buf->lock, flags);
151
152
153
154
155 if (test_bit(TTYP_FLUSHING, &port->iflags)) {
156 set_bit(TTYP_FLUSHPENDING, &port->iflags);
157 spin_unlock_irqrestore(&buf->lock, flags);
158 wait_event(tty->read_wait,
159 test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0);
160 return;
161 } else
162 __tty_buffer_flush(port);
163 spin_unlock_irqrestore(&buf->lock, flags);
164}
165
166
167
168
169
170
171
172
173
174
175
176
177
178static struct tty_buffer *tty_buffer_find(struct tty_port *port, size_t size)
179{
180 struct tty_buffer **tbh = &port->buf.free;
181 while ((*tbh) != NULL) {
182 struct tty_buffer *t = *tbh;
183 if (t->size >= size) {
184 *tbh = t->next;
185 t->next = NULL;
186 t->used = 0;
187 t->commit = 0;
188 t->read = 0;
189 port->buf.memory_used += t->size;
190 return t;
191 }
192 tbh = &((*tbh)->next);
193 }
194
195 size = (size + 0xFF) & ~0xFF;
196 return tty_buffer_alloc(port, size);
197
198
199}
200
201
202
203
204
205
206
207
208
209
210int tty_buffer_request_room(struct tty_port *port, size_t size)
211{
212 struct tty_bufhead *buf = &port->buf;
213 struct tty_buffer *b, *n;
214 int left;
215 unsigned long flags;
216 spin_lock_irqsave(&buf->lock, flags);
217
218
219
220 b = buf->tail;
221 if (b != NULL)
222 left = b->size - b->used;
223 else
224 left = 0;
225
226 if (left < size) {
227
228 if ((n = tty_buffer_find(port, size)) != NULL) {
229 if (b != NULL) {
230 b->next = n;
231 b->commit = b->used;
232 } else
233 buf->head = n;
234 buf->tail = n;
235 } else
236 size = left;
237 }
238 spin_unlock_irqrestore(&buf->lock, flags);
239 return size;
240}
241EXPORT_SYMBOL_GPL(tty_buffer_request_room);
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256int tty_insert_flip_string_fixed_flag(struct tty_port *port,
257 const unsigned char *chars, char flag, size_t size)
258{
259 int copied = 0;
260 do {
261 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
262 int space = tty_buffer_request_room(port, goal);
263 struct tty_buffer *tb = port->buf.tail;
264
265 if (unlikely(space == 0)) {
266 break;
267 }
268 memcpy(tb->char_buf_ptr + tb->used, chars, space);
269 memset(tb->flag_buf_ptr + tb->used, flag, space);
270 tb->used += space;
271 copied += space;
272 chars += space;
273
274
275 } while (unlikely(size > copied));
276 return copied;
277}
278EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294int tty_insert_flip_string_flags(struct tty_port *port,
295 const unsigned char *chars, const char *flags, size_t size)
296{
297 int copied = 0;
298 do {
299 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
300 int space = tty_buffer_request_room(port, goal);
301 struct tty_buffer *tb = port->buf.tail;
302
303 if (unlikely(space == 0)) {
304 break;
305 }
306 memcpy(tb->char_buf_ptr + tb->used, chars, space);
307 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
308 tb->used += space;
309 copied += space;
310 chars += space;
311 flags += space;
312
313
314 } while (unlikely(size > copied));
315 return copied;
316}
317EXPORT_SYMBOL(tty_insert_flip_string_flags);
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332void tty_schedule_flip(struct tty_port *port)
333{
334 struct tty_bufhead *buf = &port->buf;
335 unsigned long flags;
336 WARN_ON(port->low_latency);
337
338 spin_lock_irqsave(&buf->lock, flags);
339 if (buf->tail != NULL)
340 buf->tail->commit = buf->tail->used;
341 spin_unlock_irqrestore(&buf->lock, flags);
342 schedule_work(&buf->work);
343}
344EXPORT_SYMBOL(tty_schedule_flip);
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
362 size_t size)
363{
364 int space = tty_buffer_request_room(port, size);
365 if (likely(space)) {
366 struct tty_buffer *tb = port->buf.tail;
367 *chars = tb->char_buf_ptr + tb->used;
368 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
369 tb->used += space;
370 }
371 return space;
372}
373EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391int tty_prepare_flip_string_flags(struct tty_port *port,
392 unsigned char **chars, char **flags, size_t size)
393{
394 int space = tty_buffer_request_room(port, size);
395 if (likely(space)) {
396 struct tty_buffer *tb = port->buf.tail;
397 *chars = tb->char_buf_ptr + tb->used;
398 *flags = tb->flag_buf_ptr + tb->used;
399 tb->used += space;
400 }
401 return space;
402}
403EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419static void flush_to_ldisc(struct work_struct *work)
420{
421 struct tty_port *port = container_of(work, struct tty_port, buf.work);
422 struct tty_bufhead *buf = &port->buf;
423 struct tty_struct *tty;
424 unsigned long flags;
425 struct tty_ldisc *disc;
426
427 tty = port->itty;
428 if (tty == NULL)
429 return;
430
431 disc = tty_ldisc_ref(tty);
432 if (disc == NULL)
433 return;
434
435 spin_lock_irqsave(&buf->lock, flags);
436
437 if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) {
438 struct tty_buffer *head;
439 while ((head = buf->head) != NULL) {
440 int count;
441 char *char_buf;
442 unsigned char *flag_buf;
443
444 count = head->commit - head->read;
445 if (!count) {
446 if (head->next == NULL)
447 break;
448 buf->head = head->next;
449 tty_buffer_free(port, head);
450 continue;
451 }
452 if (!tty->receive_room)
453 break;
454 if (count > tty->receive_room)
455 count = tty->receive_room;
456 char_buf = head->char_buf_ptr + head->read;
457 flag_buf = head->flag_buf_ptr + head->read;
458 head->read += count;
459 spin_unlock_irqrestore(&buf->lock, flags);
460 disc->ops->receive_buf(tty, char_buf,
461 flag_buf, count);
462 spin_lock_irqsave(&buf->lock, flags);
463
464
465
466
467 if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) {
468 __tty_buffer_flush(port);
469 clear_bit(TTYP_FLUSHPENDING, &port->iflags);
470 wake_up(&tty->read_wait);
471 break;
472 }
473 }
474 clear_bit(TTYP_FLUSHING, &port->iflags);
475 }
476
477 spin_unlock_irqrestore(&buf->lock, flags);
478
479 tty_ldisc_deref(disc);
480}
481
482
483
484
485
486
487
488
489
490void tty_flush_to_ldisc(struct tty_struct *tty)
491{
492 if (!tty->port->low_latency)
493 flush_work(&tty->port->buf.work);
494}
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510void tty_flip_buffer_push(struct tty_port *port)
511{
512 struct tty_bufhead *buf = &port->buf;
513 unsigned long flags;
514
515 spin_lock_irqsave(&buf->lock, flags);
516 if (buf->tail != NULL)
517 buf->tail->commit = buf->tail->used;
518 spin_unlock_irqrestore(&buf->lock, flags);
519
520 if (port->low_latency)
521 flush_to_ldisc(&buf->work);
522 else
523 schedule_work(&buf->work);
524}
525EXPORT_SYMBOL(tty_flip_buffer_push);
526
527
528
529
530
531
532
533
534
535
536
537void tty_buffer_init(struct tty_port *port)
538{
539 struct tty_bufhead *buf = &port->buf;
540
541 spin_lock_init(&buf->lock);
542 buf->head = NULL;
543 buf->tail = NULL;
544 buf->free = NULL;
545 buf->memory_used = 0;
546 INIT_WORK(&buf->work, flush_to_ldisc);
547}
548
549