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