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