Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 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 | |
| 30 | void tty_buffer_free_all(struct tty_struct *tty) |
| 31 | { |
| 32 | struct tty_buffer *thead; |
| 33 | while ((thead = tty->buf.head) != NULL) { |
| 34 | tty->buf.head = thead->next; |
| 35 | kfree(thead); |
| 36 | } |
| 37 | while ((thead = tty->buf.free) != NULL) { |
| 38 | tty->buf.free = thead->next; |
| 39 | kfree(thead); |
| 40 | } |
| 41 | tty->buf.tail = NULL; |
| 42 | tty->buf.memory_used = 0; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * tty_buffer_alloc - allocate a tty buffer |
| 47 | * @tty: tty device |
| 48 | * @size: desired size (characters) |
| 49 | * |
| 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 |
| 52 | * per device queue |
| 53 | * |
| 54 | * Locking: Caller must hold tty->buf.lock |
| 55 | */ |
| 56 | |
| 57 | static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size) |
| 58 | { |
| 59 | struct tty_buffer *p; |
| 60 | |
| 61 | if (tty->buf.memory_used + size > 65536) |
| 62 | return NULL; |
| 63 | p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC); |
| 64 | if (p == NULL) |
| 65 | return NULL; |
| 66 | p->used = 0; |
| 67 | p->size = size; |
| 68 | p->next = NULL; |
| 69 | p->commit = 0; |
| 70 | p->read = 0; |
| 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; |
| 74 | return p; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * tty_buffer_free - free a tty buffer |
| 79 | * @tty: tty owning the buffer |
| 80 | * @b: the buffer to free |
| 81 | * |
| 82 | * Free a tty buffer, or add it to the free list according to our |
| 83 | * internal strategy |
| 84 | * |
| 85 | * Locking: Caller must hold tty->buf.lock |
| 86 | */ |
| 87 | |
| 88 | static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b) |
| 89 | { |
| 90 | /* Dumb strategy for now - should keep some stats */ |
| 91 | tty->buf.memory_used -= b->size; |
| 92 | WARN_ON(tty->buf.memory_used < 0); |
| 93 | |
| 94 | if (b->size >= 512) |
| 95 | kfree(b); |
| 96 | else { |
| 97 | b->next = tty->buf.free; |
| 98 | tty->buf.free = b; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * __tty_buffer_flush - flush full tty buffers |
| 104 | * @tty: tty to flush |
| 105 | * |
| 106 | * flush all the buffers containing receive data. Caller must |
| 107 | * hold the buffer lock and must have ensured no parallel flush to |
| 108 | * ldisc is running. |
| 109 | * |
| 110 | * Locking: Caller must hold tty->buf.lock |
| 111 | */ |
| 112 | |
| 113 | static void __tty_buffer_flush(struct tty_struct *tty) |
| 114 | { |
| 115 | struct tty_buffer *thead; |
| 116 | |
| 117 | while ((thead = tty->buf.head) != NULL) { |
| 118 | tty->buf.head = thead->next; |
| 119 | tty_buffer_free(tty, thead); |
| 120 | } |
| 121 | tty->buf.tail = NULL; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * tty_buffer_flush - flush full tty buffers |
| 126 | * @tty: tty to flush |
| 127 | * |
| 128 | * flush all the buffers containing receive data. If the buffer is |
| 129 | * being processed by flush_to_ldisc then we defer the processing |
| 130 | * to that function |
| 131 | * |
| 132 | * Locking: none |
| 133 | */ |
| 134 | |
| 135 | void tty_buffer_flush(struct tty_struct *tty) |
| 136 | { |
Jiri Slaby | 2fc2066 | 2012-10-18 22:26:44 +0200 | [diff] [blame^] | 137 | struct tty_port *port = tty->port; |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 138 | unsigned long flags; |
| 139 | spin_lock_irqsave(&tty->buf.lock, flags); |
| 140 | |
| 141 | /* If the data is being pushed to the tty layer then we can't |
| 142 | process it here. Instead set a flag and the flush_to_ldisc |
| 143 | path will process the flush request before it exits */ |
Jiri Slaby | 2fc2066 | 2012-10-18 22:26:44 +0200 | [diff] [blame^] | 144 | if (test_bit(TTYP_FLUSHING, &port->iflags)) { |
| 145 | set_bit(TTYP_FLUSHPENDING, &port->iflags); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 146 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
| 147 | wait_event(tty->read_wait, |
Jiri Slaby | 2fc2066 | 2012-10-18 22:26:44 +0200 | [diff] [blame^] | 148 | test_bit(TTYP_FLUSHPENDING, &port->iflags) == 0); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 149 | return; |
| 150 | } else |
| 151 | __tty_buffer_flush(tty); |
| 152 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * tty_buffer_find - find a free tty buffer |
| 157 | * @tty: tty owning the buffer |
| 158 | * @size: characters wanted |
| 159 | * |
| 160 | * Locate an existing suitable tty buffer or if we are lacking one then |
| 161 | * allocate a new one. We round our buffers off in 256 character chunks |
| 162 | * to get better allocation behaviour. |
| 163 | * |
| 164 | * Locking: Caller must hold tty->buf.lock |
| 165 | */ |
| 166 | |
| 167 | static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size) |
| 168 | { |
| 169 | struct tty_buffer **tbh = &tty->buf.free; |
| 170 | while ((*tbh) != NULL) { |
| 171 | struct tty_buffer *t = *tbh; |
| 172 | if (t->size >= size) { |
| 173 | *tbh = t->next; |
| 174 | t->next = NULL; |
| 175 | t->used = 0; |
| 176 | t->commit = 0; |
| 177 | t->read = 0; |
| 178 | tty->buf.memory_used += t->size; |
| 179 | return t; |
| 180 | } |
| 181 | tbh = &((*tbh)->next); |
| 182 | } |
| 183 | /* Round the buffer size out */ |
| 184 | size = (size + 0xFF) & ~0xFF; |
| 185 | return tty_buffer_alloc(tty, size); |
| 186 | /* Should possibly check if this fails for the largest buffer we |
| 187 | have queued and recycle that ? */ |
| 188 | } |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 189 | /** |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 190 | * __tty_buffer_request_room - grow tty buffer if needed |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 191 | * @tty: tty structure |
| 192 | * @size: size desired |
| 193 | * |
| 194 | * Make at least size bytes of linear space available for the tty |
| 195 | * buffer. If we fail return the size we managed to find. |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 196 | * Locking: Caller must hold tty->buf.lock |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 197 | */ |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 198 | static int __tty_buffer_request_room(struct tty_struct *tty, size_t size) |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 199 | { |
| 200 | struct tty_buffer *b, *n; |
| 201 | int left; |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 202 | /* OPTIMISATION: We could keep a per tty "zero" sized buffer to |
| 203 | remove this conditional if its worth it. This would be invisible |
| 204 | to the callers */ |
| 205 | if ((b = tty->buf.tail) != NULL) |
| 206 | left = b->size - b->used; |
| 207 | else |
| 208 | left = 0; |
| 209 | |
| 210 | if (left < size) { |
| 211 | /* This is the slow path - looking for new buffers to use */ |
| 212 | if ((n = tty_buffer_find(tty, size)) != NULL) { |
| 213 | if (b != NULL) { |
| 214 | b->next = n; |
| 215 | b->commit = b->used; |
| 216 | } else |
| 217 | tty->buf.head = n; |
| 218 | tty->buf.tail = n; |
| 219 | } else |
| 220 | size = left; |
| 221 | } |
| 222 | |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 223 | return size; |
| 224 | } |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 225 | |
| 226 | |
| 227 | /** |
| 228 | * tty_buffer_request_room - grow tty buffer if needed |
| 229 | * @tty: tty structure |
| 230 | * @size: size desired |
| 231 | * |
| 232 | * Make at least size bytes of linear space available for the tty |
| 233 | * buffer. If we fail return the size we managed to find. |
| 234 | * |
| 235 | * Locking: Takes tty->buf.lock |
| 236 | */ |
| 237 | int tty_buffer_request_room(struct tty_struct *tty, size_t size) |
| 238 | { |
| 239 | unsigned long flags; |
| 240 | int length; |
| 241 | |
| 242 | spin_lock_irqsave(&tty->buf.lock, flags); |
| 243 | length = __tty_buffer_request_room(tty, size); |
| 244 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
| 245 | return length; |
| 246 | } |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 247 | EXPORT_SYMBOL_GPL(tty_buffer_request_room); |
| 248 | |
| 249 | /** |
Alan Cox | 2832fc1 | 2010-02-18 16:43:54 +0000 | [diff] [blame] | 250 | * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 251 | * @tty: tty structure |
| 252 | * @chars: characters |
Alan Cox | 2832fc1 | 2010-02-18 16:43:54 +0000 | [diff] [blame] | 253 | * @flag: flag value for each character |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 254 | * @size: size |
| 255 | * |
| 256 | * Queue a series of bytes to the tty buffering. All the characters |
Johan Hovold | ccc5ca8 | 2010-05-07 19:58:32 +0200 | [diff] [blame] | 257 | * passed are marked with the supplied flag. Returns the number added. |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 258 | * |
| 259 | * Locking: Called functions may take tty->buf.lock |
| 260 | */ |
| 261 | |
Alan Cox | 2832fc1 | 2010-02-18 16:43:54 +0000 | [diff] [blame] | 262 | int tty_insert_flip_string_fixed_flag(struct tty_struct *tty, |
| 263 | const unsigned char *chars, char flag, size_t size) |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 264 | { |
| 265 | int copied = 0; |
| 266 | do { |
Fang Wenqi | d4bee0a | 2010-03-09 18:54:28 +0800 | [diff] [blame] | 267 | int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 268 | int space; |
| 269 | unsigned long flags; |
| 270 | struct tty_buffer *tb; |
| 271 | |
| 272 | spin_lock_irqsave(&tty->buf.lock, flags); |
| 273 | space = __tty_buffer_request_room(tty, goal); |
| 274 | tb = tty->buf.tail; |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 275 | /* If there is no space then tb may be NULL */ |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 276 | if (unlikely(space == 0)) { |
| 277 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 278 | break; |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 279 | } |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 280 | memcpy(tb->char_buf_ptr + tb->used, chars, space); |
Alan Cox | 2832fc1 | 2010-02-18 16:43:54 +0000 | [diff] [blame] | 281 | memset(tb->flag_buf_ptr + tb->used, flag, space); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 282 | tb->used += space; |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 283 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 284 | copied += space; |
| 285 | chars += space; |
| 286 | /* There is a small chance that we need to split the data over |
| 287 | several buffers. If this is the case we must loop */ |
| 288 | } while (unlikely(size > copied)); |
| 289 | return copied; |
| 290 | } |
Alan Cox | 2832fc1 | 2010-02-18 16:43:54 +0000 | [diff] [blame] | 291 | EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 292 | |
| 293 | /** |
| 294 | * tty_insert_flip_string_flags - Add characters to the tty buffer |
| 295 | * @tty: tty structure |
| 296 | * @chars: characters |
| 297 | * @flags: flag bytes |
| 298 | * @size: size |
| 299 | * |
| 300 | * Queue a series of bytes to the tty buffering. For each character |
| 301 | * the flags array indicates the status of the character. Returns the |
| 302 | * number added. |
| 303 | * |
| 304 | * Locking: Called functions may take tty->buf.lock |
| 305 | */ |
| 306 | |
| 307 | int tty_insert_flip_string_flags(struct tty_struct *tty, |
| 308 | const unsigned char *chars, const char *flags, size_t size) |
| 309 | { |
| 310 | int copied = 0; |
| 311 | do { |
Fang Wenqi | d4bee0a | 2010-03-09 18:54:28 +0800 | [diff] [blame] | 312 | int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 313 | int space; |
| 314 | unsigned long __flags; |
| 315 | struct tty_buffer *tb; |
| 316 | |
| 317 | spin_lock_irqsave(&tty->buf.lock, __flags); |
| 318 | space = __tty_buffer_request_room(tty, goal); |
| 319 | tb = tty->buf.tail; |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 320 | /* If there is no space then tb may be NULL */ |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 321 | if (unlikely(space == 0)) { |
| 322 | spin_unlock_irqrestore(&tty->buf.lock, __flags); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 323 | break; |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 324 | } |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 325 | memcpy(tb->char_buf_ptr + tb->used, chars, space); |
| 326 | memcpy(tb->flag_buf_ptr + tb->used, flags, space); |
| 327 | tb->used += space; |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 328 | spin_unlock_irqrestore(&tty->buf.lock, __flags); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 329 | copied += space; |
| 330 | chars += space; |
| 331 | flags += space; |
| 332 | /* There is a small chance that we need to split the data over |
| 333 | several buffers. If this is the case we must loop */ |
| 334 | } while (unlikely(size > copied)); |
| 335 | return copied; |
| 336 | } |
| 337 | EXPORT_SYMBOL(tty_insert_flip_string_flags); |
| 338 | |
| 339 | /** |
| 340 | * tty_schedule_flip - push characters to ldisc |
| 341 | * @tty: tty to push from |
| 342 | * |
| 343 | * Takes any pending buffers and transfers their ownership to the |
| 344 | * ldisc side of the queue. It then schedules those characters for |
| 345 | * processing by the line discipline. |
Ivo Sieben | cee4ad1 | 2012-09-27 14:02:05 +0200 | [diff] [blame] | 346 | * Note that this function can only be used when the low_latency flag |
| 347 | * is unset. Otherwise the workqueue won't be flushed. |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 348 | * |
| 349 | * Locking: Takes tty->buf.lock |
| 350 | */ |
| 351 | |
| 352 | void tty_schedule_flip(struct tty_struct *tty) |
| 353 | { |
| 354 | unsigned long flags; |
| 355 | spin_lock_irqsave(&tty->buf.lock, flags); |
| 356 | if (tty->buf.tail != NULL) |
| 357 | tty->buf.tail->commit = tty->buf.tail->used; |
| 358 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
Linus Torvalds | f23eb2b | 2011-03-22 16:17:32 -0700 | [diff] [blame] | 359 | schedule_work(&tty->buf.work); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 360 | } |
| 361 | EXPORT_SYMBOL(tty_schedule_flip); |
| 362 | |
| 363 | /** |
| 364 | * tty_prepare_flip_string - make room for characters |
| 365 | * @tty: tty |
| 366 | * @chars: return pointer for character write area |
| 367 | * @size: desired size |
| 368 | * |
| 369 | * Prepare a block of space in the buffer for data. Returns the length |
| 370 | * available and buffer pointer to the space which is now allocated and |
| 371 | * accounted for as ready for normal characters. This is used for drivers |
| 372 | * that need their own block copy routines into the buffer. There is no |
| 373 | * guarantee the buffer is a DMA target! |
| 374 | * |
| 375 | * Locking: May call functions taking tty->buf.lock |
| 376 | */ |
| 377 | |
| 378 | int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars, |
| 379 | size_t size) |
| 380 | { |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 381 | int space; |
| 382 | unsigned long flags; |
| 383 | struct tty_buffer *tb; |
| 384 | |
| 385 | spin_lock_irqsave(&tty->buf.lock, flags); |
| 386 | space = __tty_buffer_request_room(tty, size); |
| 387 | |
| 388 | tb = tty->buf.tail; |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 389 | if (likely(space)) { |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 390 | *chars = tb->char_buf_ptr + tb->used; |
| 391 | memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space); |
| 392 | tb->used += space; |
| 393 | } |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 394 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 395 | return space; |
| 396 | } |
| 397 | EXPORT_SYMBOL_GPL(tty_prepare_flip_string); |
| 398 | |
| 399 | /** |
| 400 | * tty_prepare_flip_string_flags - make room for characters |
| 401 | * @tty: tty |
| 402 | * @chars: return pointer for character write area |
| 403 | * @flags: return pointer for status flag write area |
| 404 | * @size: desired size |
| 405 | * |
| 406 | * Prepare a block of space in the buffer for data. Returns the length |
| 407 | * available and buffer pointer to the space which is now allocated and |
| 408 | * accounted for as ready for characters. This is used for drivers |
| 409 | * that need their own block copy routines into the buffer. There is no |
| 410 | * guarantee the buffer is a DMA target! |
| 411 | * |
| 412 | * Locking: May call functions taking tty->buf.lock |
| 413 | */ |
| 414 | |
| 415 | int tty_prepare_flip_string_flags(struct tty_struct *tty, |
| 416 | unsigned char **chars, char **flags, size_t size) |
| 417 | { |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 418 | int space; |
| 419 | unsigned long __flags; |
| 420 | struct tty_buffer *tb; |
| 421 | |
| 422 | spin_lock_irqsave(&tty->buf.lock, __flags); |
| 423 | space = __tty_buffer_request_room(tty, size); |
| 424 | |
| 425 | tb = tty->buf.tail; |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 426 | if (likely(space)) { |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 427 | *chars = tb->char_buf_ptr + tb->used; |
| 428 | *flags = tb->flag_buf_ptr + tb->used; |
| 429 | tb->used += space; |
| 430 | } |
Xiaobing Tu | c56a00a | 2012-03-16 03:00:26 +0000 | [diff] [blame] | 431 | spin_unlock_irqrestore(&tty->buf.lock, __flags); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 432 | return space; |
| 433 | } |
| 434 | EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags); |
| 435 | |
| 436 | |
| 437 | |
| 438 | /** |
| 439 | * flush_to_ldisc |
| 440 | * @work: tty structure passed from work queue. |
| 441 | * |
| 442 | * This routine is called out of the software interrupt to flush data |
| 443 | * from the buffer chain to the line discipline. |
| 444 | * |
| 445 | * Locking: holds tty->buf.lock to guard buffer list. Drops the lock |
| 446 | * while invoking the line discipline receive_buf method. The |
| 447 | * receive_buf method is single threaded for each tty instance. |
| 448 | */ |
| 449 | |
| 450 | static void flush_to_ldisc(struct work_struct *work) |
| 451 | { |
| 452 | struct tty_struct *tty = |
Linus Torvalds | f23eb2b | 2011-03-22 16:17:32 -0700 | [diff] [blame] | 453 | container_of(work, struct tty_struct, buf.work); |
Jiri Slaby | 2fc2066 | 2012-10-18 22:26:44 +0200 | [diff] [blame^] | 454 | struct tty_port *port = tty->port; |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 455 | unsigned long flags; |
| 456 | struct tty_ldisc *disc; |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 457 | |
| 458 | disc = tty_ldisc_ref(tty); |
| 459 | if (disc == NULL) /* !TTY_LDISC */ |
| 460 | return; |
| 461 | |
| 462 | spin_lock_irqsave(&tty->buf.lock, flags); |
Linus Torvalds | 4524200 | 2009-10-14 08:59:49 -0700 | [diff] [blame] | 463 | |
Jiri Slaby | 2fc2066 | 2012-10-18 22:26:44 +0200 | [diff] [blame^] | 464 | if (!test_and_set_bit(TTYP_FLUSHING, &port->iflags)) { |
Linus Torvalds | 81de916 | 2011-06-08 07:46:30 -0700 | [diff] [blame] | 465 | struct tty_buffer *head; |
Linus Torvalds | 4524200 | 2009-10-14 08:59:49 -0700 | [diff] [blame] | 466 | while ((head = tty->buf.head) != NULL) { |
| 467 | int count; |
| 468 | char *char_buf; |
| 469 | unsigned char *flag_buf; |
| 470 | |
| 471 | count = head->commit - head->read; |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 472 | if (!count) { |
| 473 | if (head->next == NULL) |
| 474 | break; |
Linus Torvalds | 4524200 | 2009-10-14 08:59:49 -0700 | [diff] [blame] | 475 | tty->buf.head = head->next; |
| 476 | tty_buffer_free(tty, head); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 477 | continue; |
| 478 | } |
| 479 | /* Ldisc or user is trying to flush the buffers |
| 480 | we are feeding to the ldisc, stop feeding the |
| 481 | line discipline as we want to empty the queue */ |
Jiri Slaby | 2fc2066 | 2012-10-18 22:26:44 +0200 | [diff] [blame^] | 482 | if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 483 | break; |
Linus Torvalds | 81de916 | 2011-06-08 07:46:30 -0700 | [diff] [blame] | 484 | if (!tty->receive_room) |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 485 | break; |
| 486 | if (count > tty->receive_room) |
| 487 | count = tty->receive_room; |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 488 | char_buf = head->char_buf_ptr + head->read; |
| 489 | flag_buf = head->flag_buf_ptr + head->read; |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 490 | head->read += count; |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 491 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 492 | disc->ops->receive_buf(tty, char_buf, |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 493 | flag_buf, count); |
| 494 | spin_lock_irqsave(&tty->buf.lock, flags); |
| 495 | } |
Jiri Slaby | 2fc2066 | 2012-10-18 22:26:44 +0200 | [diff] [blame^] | 496 | clear_bit(TTYP_FLUSHING, &port->iflags); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 497 | } |
Linus Torvalds | 4524200 | 2009-10-14 08:59:49 -0700 | [diff] [blame] | 498 | |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 499 | /* We may have a deferred request to flush the input buffer, |
| 500 | if so pull the chain under the lock and empty the queue */ |
Jiri Slaby | 2fc2066 | 2012-10-18 22:26:44 +0200 | [diff] [blame^] | 501 | if (test_bit(TTYP_FLUSHPENDING, &port->iflags)) { |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 502 | __tty_buffer_flush(tty); |
Jiri Slaby | 2fc2066 | 2012-10-18 22:26:44 +0200 | [diff] [blame^] | 503 | clear_bit(TTYP_FLUSHPENDING, &port->iflags); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 504 | wake_up(&tty->read_wait); |
| 505 | } |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 506 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
| 507 | |
| 508 | tty_ldisc_deref(disc); |
| 509 | } |
| 510 | |
| 511 | /** |
OGAWA Hirofumi | e043e42 | 2009-07-29 12:15:56 -0700 | [diff] [blame] | 512 | * tty_flush_to_ldisc |
| 513 | * @tty: tty to push |
| 514 | * |
| 515 | * Push the terminal flip buffers to the line discipline. |
| 516 | * |
| 517 | * Must not be called from IRQ context. |
| 518 | */ |
| 519 | void tty_flush_to_ldisc(struct tty_struct *tty) |
| 520 | { |
Ivo Sieben | cee4ad1 | 2012-09-27 14:02:05 +0200 | [diff] [blame] | 521 | if (!tty->low_latency) |
| 522 | flush_work(&tty->buf.work); |
OGAWA Hirofumi | e043e42 | 2009-07-29 12:15:56 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /** |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 526 | * tty_flip_buffer_push - terminal |
| 527 | * @tty: tty to push |
| 528 | * |
| 529 | * Queue a push of the terminal flip buffers to the line discipline. This |
| 530 | * function must not be called from IRQ context if tty->low_latency is set. |
| 531 | * |
| 532 | * In the event of the queue being busy for flipping the work will be |
| 533 | * held off and retried later. |
| 534 | * |
| 535 | * Locking: tty buffer lock. Driver locks in low latency mode. |
| 536 | */ |
| 537 | |
| 538 | void tty_flip_buffer_push(struct tty_struct *tty) |
| 539 | { |
| 540 | unsigned long flags; |
| 541 | spin_lock_irqsave(&tty->buf.lock, flags); |
| 542 | if (tty->buf.tail != NULL) |
| 543 | tty->buf.tail->commit = tty->buf.tail->used; |
| 544 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
| 545 | |
| 546 | if (tty->low_latency) |
Linus Torvalds | f23eb2b | 2011-03-22 16:17:32 -0700 | [diff] [blame] | 547 | flush_to_ldisc(&tty->buf.work); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 548 | else |
Linus Torvalds | f23eb2b | 2011-03-22 16:17:32 -0700 | [diff] [blame] | 549 | schedule_work(&tty->buf.work); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 550 | } |
| 551 | EXPORT_SYMBOL(tty_flip_buffer_push); |
| 552 | |
| 553 | /** |
| 554 | * tty_buffer_init - prepare a tty buffer structure |
| 555 | * @tty: tty to initialise |
| 556 | * |
| 557 | * Set up the initial state of the buffer management for a tty device. |
| 558 | * Must be called before the other tty buffer functions are used. |
| 559 | * |
| 560 | * Locking: none |
| 561 | */ |
| 562 | |
| 563 | void tty_buffer_init(struct tty_struct *tty) |
| 564 | { |
| 565 | spin_lock_init(&tty->buf.lock); |
| 566 | tty->buf.head = NULL; |
| 567 | tty->buf.tail = NULL; |
| 568 | tty->buf.free = NULL; |
| 569 | tty->buf.memory_used = 0; |
Linus Torvalds | f23eb2b | 2011-03-22 16:17:32 -0700 | [diff] [blame] | 570 | INIT_WORK(&tty->buf.work, flush_to_ldisc); |
Alan Cox | e049573 | 2008-10-13 10:36:58 +0100 | [diff] [blame] | 571 | } |
| 572 | |