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