Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 1 | /* |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 2 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 6 | #include <linux/irqreturn.h> |
| 7 | #include <linux/kd.h> |
| 8 | #include <linux/sched.h> |
| 9 | #include <linux/slab.h> |
Al Viro | 510c72a3 | 2011-08-18 20:08:29 +0100 | [diff] [blame] | 10 | #include "chan.h" |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 11 | #include <irq_kern.h> |
| 12 | #include <irq_user.h> |
| 13 | #include <kern_util.h> |
| 14 | #include <os.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #define LINE_BUFSIZE 4096 |
| 17 | |
Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 18 | static irqreturn_t line_interrupt(int irq, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | { |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 20 | struct chan *chan = data; |
| 21 | struct line *line = chan->line; |
Jiri Slaby | 6fc5884 | 2012-06-04 13:35:27 +0200 | [diff] [blame] | 22 | struct tty_struct *tty = tty_port_tty_get(&line->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | if (line) |
Jiri Slaby | 6fc5884 | 2012-06-04 13:35:27 +0200 | [diff] [blame] | 25 | chan_interrupt(line, tty, irq); |
| 26 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | return IRQ_HANDLED; |
| 28 | } |
| 29 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 30 | /* |
| 31 | * Returns the free space inside the ring buffer of this line. |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 32 | * |
Simon Arlott | b60745b | 2007-10-20 01:23:03 +0200 | [diff] [blame] | 33 | * Should be called while holding line->lock (this does not modify data). |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 34 | */ |
| 35 | static int write_room(struct line *line) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
| 37 | int n; |
| 38 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 39 | if (line->buffer == NULL) |
| 40 | return LINE_BUFSIZE - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 42 | /* This is for the case where the buffer is wrapped! */ |
| 43 | n = line->head - line->tail; |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | if (n <= 0) |
Jeff Dike | acb2cf3 | 2008-02-04 22:30:42 -0800 | [diff] [blame] | 46 | n += LINE_BUFSIZE; /* The other case */ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 47 | return n - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 50 | int line_write_room(struct tty_struct *tty) |
| 51 | { |
| 52 | struct line *line = tty->driver_data; |
| 53 | unsigned long flags; |
| 54 | int room; |
| 55 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 56 | spin_lock_irqsave(&line->lock, flags); |
| 57 | room = write_room(line); |
| 58 | spin_unlock_irqrestore(&line->lock, flags); |
| 59 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 60 | return room; |
| 61 | } |
| 62 | |
| 63 | int line_chars_in_buffer(struct tty_struct *tty) |
| 64 | { |
| 65 | struct line *line = tty->driver_data; |
| 66 | unsigned long flags; |
| 67 | int ret; |
| 68 | |
| 69 | spin_lock_irqsave(&line->lock, flags); |
Jeff Dike | acb2cf3 | 2008-02-04 22:30:42 -0800 | [diff] [blame] | 70 | /* write_room subtracts 1 for the needed NULL, so we readd it.*/ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 71 | ret = LINE_BUFSIZE - (write_room(line) + 1); |
| 72 | spin_unlock_irqrestore(&line->lock, flags); |
| 73 | |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * This copies the content of buf into the circular buffer associated with |
| 79 | * this line. |
| 80 | * The return value is the number of characters actually copied, i.e. the ones |
| 81 | * for which there was space: this function is not supposed to ever flush out |
| 82 | * the circular buffer. |
| 83 | * |
| 84 | * Must be called while holding line->lock! |
| 85 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | static int buffer_data(struct line *line, const char *buf, int len) |
| 87 | { |
| 88 | int end, room; |
| 89 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 90 | if (line->buffer == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC); |
| 92 | if (line->buffer == NULL) { |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 93 | printk(KERN_ERR "buffer_data - atomic allocation " |
| 94 | "failed\n"); |
| 95 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | line->head = line->buffer; |
| 98 | line->tail = line->buffer; |
| 99 | } |
| 100 | |
| 101 | room = write_room(line); |
| 102 | len = (len > room) ? room : len; |
| 103 | |
| 104 | end = line->buffer + LINE_BUFSIZE - line->tail; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 105 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 106 | if (len < end) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | memcpy(line->tail, buf, len); |
| 108 | line->tail += len; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 109 | } |
| 110 | else { |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 111 | /* The circular buffer is wrapping */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | memcpy(line->tail, buf, end); |
| 113 | buf += end; |
| 114 | memcpy(line->buffer, buf, len - end); |
| 115 | line->tail = line->buffer + len - end; |
| 116 | } |
| 117 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 118 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 121 | /* |
| 122 | * Flushes the ring buffer to the output channels. That is, write_chan is |
| 123 | * called, passing it line->head as buffer, and an appropriate count. |
| 124 | * |
| 125 | * On exit, returns 1 when the buffer is empty, |
| 126 | * 0 when the buffer is not empty on exit, |
| 127 | * and -errno when an error occurred. |
| 128 | * |
| 129 | * Must be called while holding line->lock!*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | static int flush_buffer(struct line *line) |
| 131 | { |
| 132 | int n, count; |
| 133 | |
| 134 | if ((line->buffer == NULL) || (line->head == line->tail)) |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 135 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | if (line->tail < line->head) { |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 138 | /* line->buffer + LINE_BUFSIZE is the end of the buffer! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | count = line->buffer + LINE_BUFSIZE - line->head; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 140 | |
Al Viro | bed5e39 | 2011-09-08 10:49:34 -0400 | [diff] [blame] | 141 | n = write_chan(line->chan_out, line->head, count, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | line->driver->write_irq); |
| 143 | if (n < 0) |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 144 | return n; |
| 145 | if (n == count) { |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 146 | /* |
| 147 | * We have flushed from ->head to buffer end, now we |
| 148 | * must flush only from the beginning to ->tail. |
| 149 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | line->head = line->buffer; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 151 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | line->head += n; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 153 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | count = line->tail - line->head; |
Al Viro | bed5e39 | 2011-09-08 10:49:34 -0400 | [diff] [blame] | 158 | n = write_chan(line->chan_out, line->head, count, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | line->driver->write_irq); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 160 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 161 | if (n < 0) |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 162 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | line->head += n; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 165 | return line->head == line->tail; |
| 166 | } |
| 167 | |
| 168 | void line_flush_buffer(struct tty_struct *tty) |
| 169 | { |
| 170 | struct line *line = tty->driver_data; |
| 171 | unsigned long flags; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 172 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 173 | spin_lock_irqsave(&line->lock, flags); |
Richard Weinberger | f1c93e4 | 2011-07-25 17:12:55 -0700 | [diff] [blame] | 174 | flush_buffer(line); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 175 | spin_unlock_irqrestore(&line->lock, flags); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 178 | /* |
| 179 | * We map both ->flush_chars and ->put_char (which go in pair) onto |
| 180 | * ->flush_buffer and ->write. Hope it's not that bad. |
| 181 | */ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 182 | void line_flush_chars(struct tty_struct *tty) |
| 183 | { |
| 184 | line_flush_buffer(tty); |
| 185 | } |
| 186 | |
WANG Cong | 3168cb9 | 2008-05-06 20:42:33 -0700 | [diff] [blame] | 187 | int line_put_char(struct tty_struct *tty, unsigned char ch) |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 188 | { |
WANG Cong | 3168cb9 | 2008-05-06 20:42:33 -0700 | [diff] [blame] | 189 | return line_write(tty, &ch, sizeof(ch)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | int line_write(struct tty_struct *tty, const unsigned char *buf, int len) |
| 193 | { |
| 194 | struct line *line = tty->driver_data; |
| 195 | unsigned long flags; |
Jeff Dike | c59dbca | 2007-10-16 01:26:42 -0700 | [diff] [blame] | 196 | int n, ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 198 | spin_lock_irqsave(&line->lock, flags); |
Jeff Dike | c59dbca | 2007-10-16 01:26:42 -0700 | [diff] [blame] | 199 | if (line->head != line->tail) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | ret = buffer_data(line, buf, len); |
Jeff Dike | c59dbca | 2007-10-16 01:26:42 -0700 | [diff] [blame] | 201 | else { |
Al Viro | bed5e39 | 2011-09-08 10:49:34 -0400 | [diff] [blame] | 202 | n = write_chan(line->chan_out, buf, len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | line->driver->write_irq); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 204 | if (n < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | ret = n; |
| 206 | goto out_up; |
| 207 | } |
| 208 | |
| 209 | len -= n; |
| 210 | ret += n; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 211 | if (len > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | ret += buffer_data(line, buf + n, len); |
| 213 | } |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 214 | out_up: |
| 215 | spin_unlock_irqrestore(&line->lock, flags); |
| 216 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 219 | void line_set_termios(struct tty_struct *tty, struct ktermios * old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
| 221 | /* nothing */ |
| 222 | } |
| 223 | |
Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 224 | void line_throttle(struct tty_struct *tty) |
| 225 | { |
| 226 | struct line *line = tty->driver_data; |
| 227 | |
Al Viro | bed5e39 | 2011-09-08 10:49:34 -0400 | [diff] [blame] | 228 | deactivate_chan(line->chan_in, line->driver->read_irq); |
Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 229 | line->throttled = 1; |
| 230 | } |
| 231 | |
| 232 | void line_unthrottle(struct tty_struct *tty) |
| 233 | { |
| 234 | struct line *line = tty->driver_data; |
| 235 | |
| 236 | line->throttled = 0; |
Al Viro | 0fcd719 | 2011-09-10 08:17:04 -0400 | [diff] [blame] | 237 | chan_interrupt(line, tty, line->driver->read_irq); |
Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 238 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 239 | /* |
| 240 | * Maybe there is enough stuff pending that calling the interrupt |
Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 241 | * throttles us again. In this case, line->throttled will be 1 |
| 242 | * again and we shouldn't turn the interrupt back on. |
| 243 | */ |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 244 | if (!line->throttled) |
Al Viro | bed5e39 | 2011-09-08 10:49:34 -0400 | [diff] [blame] | 245 | reactivate_chan(line->chan_in, line->driver->read_irq); |
Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 248 | static irqreturn_t line_write_interrupt(int irq, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 250 | struct chan *chan = data; |
| 251 | struct line *line = chan->line; |
Jiri Slaby | 6fc5884 | 2012-06-04 13:35:27 +0200 | [diff] [blame] | 252 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | int err; |
| 254 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 255 | /* |
Yong Zhang | c0b79a9 | 2011-09-22 16:58:46 +0800 | [diff] [blame] | 256 | * Interrupts are disabled here because genirq keep irqs disabled when |
| 257 | * calling the action handler. |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 258 | */ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 259 | |
Paolo 'Blaisorblade' Giarrusso | ec0ac8a | 2007-03-07 20:41:12 -0800 | [diff] [blame] | 260 | spin_lock(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | err = flush_buffer(line); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 262 | if (err == 0) { |
Al Viro | 199eebb | 2012-02-11 03:05:32 -0500 | [diff] [blame] | 263 | spin_unlock(&line->lock); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 264 | return IRQ_NONE; |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 265 | } else if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | line->head = line->buffer; |
| 267 | line->tail = line->buffer; |
| 268 | } |
Paolo 'Blaisorblade' Giarrusso | ec0ac8a | 2007-03-07 20:41:12 -0800 | [diff] [blame] | 269 | spin_unlock(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Jiri Slaby | 6fc5884 | 2012-06-04 13:35:27 +0200 | [diff] [blame] | 271 | tty = tty_port_tty_get(&line->port); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 272 | if (tty == NULL) |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 273 | return IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
WANG Cong | 06ac667 | 2008-07-29 22:33:34 -0700 | [diff] [blame] | 275 | tty_wakeup(tty); |
Jiri Slaby | 6fc5884 | 2012-06-04 13:35:27 +0200 | [diff] [blame] | 276 | tty_kref_put(tty); |
| 277 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 278 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 281 | int line_setup_irq(int fd, int input, int output, struct line *line, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | { |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 283 | const struct line_driver *driver = line->driver; |
Theodore Ts'o | aab9446 | 2012-07-17 14:18:23 -0400 | [diff] [blame] | 284 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 286 | if (input) |
| 287 | err = um_request_irq(driver->read_irq, fd, IRQ_READ, |
Theodore Ts'o | aab9446 | 2012-07-17 14:18:23 -0400 | [diff] [blame] | 288 | line_interrupt, IRQF_SHARED, |
| 289 | driver->read_irq_name, data); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 290 | if (err) |
| 291 | return err; |
| 292 | if (output) |
| 293 | err = um_request_irq(driver->write_irq, fd, IRQ_WRITE, |
Theodore Ts'o | aab9446 | 2012-07-17 14:18:23 -0400 | [diff] [blame] | 294 | line_write_interrupt, IRQF_SHARED, |
| 295 | driver->write_irq_name, data); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 296 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Richard Weinberger | 79e0273 | 2012-06-04 21:57:24 +0200 | [diff] [blame] | 299 | static int line_activate(struct tty_port *port, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Richard Weinberger | 79e0273 | 2012-06-04 21:57:24 +0200 | [diff] [blame] | 301 | int ret; |
| 302 | struct line *line = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Richard Weinberger | 79e0273 | 2012-06-04 21:57:24 +0200 | [diff] [blame] | 304 | ret = enable_chan(line); |
| 305 | if (ret) |
| 306 | return ret; |
Jeff Dike | d14ad81 | 2007-07-15 23:38:54 -0700 | [diff] [blame] | 307 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 308 | if (!line->sigio) { |
Al Viro | bed5e39 | 2011-09-08 10:49:34 -0400 | [diff] [blame] | 309 | chan_enable_winch(line->chan_out, tty); |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 310 | line->sigio = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Al Viro | bed5e39 | 2011-09-08 10:49:34 -0400 | [diff] [blame] | 313 | chan_window_size(line, &tty->winsize.ws_row, |
Richard Weinberger | 79e0273 | 2012-06-04 21:57:24 +0200 | [diff] [blame] | 314 | &tty->winsize.ws_col); |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static const struct tty_port_operations line_port_ops = { |
| 320 | .activate = line_activate, |
| 321 | }; |
| 322 | |
| 323 | int line_open(struct tty_struct *tty, struct file *filp) |
| 324 | { |
| 325 | struct line *line = tty->driver_data; |
| 326 | |
| 327 | return tty_port_open(&line->port, tty, filp); |
| 328 | } |
| 329 | |
| 330 | int line_install(struct tty_driver *driver, struct tty_struct *tty, |
| 331 | struct line *line) |
| 332 | { |
| 333 | int ret; |
| 334 | |
| 335 | ret = tty_standard_install(driver, tty); |
| 336 | if (ret) |
| 337 | return ret; |
| 338 | |
| 339 | tty->driver_data = line; |
| 340 | |
| 341 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 344 | static void unregister_winch(struct tty_struct *tty); |
| 345 | |
Richard Weinberger | 79e0273 | 2012-06-04 21:57:24 +0200 | [diff] [blame] | 346 | void line_cleanup(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | { |
| 348 | struct line *line = tty->driver_data; |
| 349 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 350 | if (line->sigio) { |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 351 | unregister_winch(tty); |
| 352 | line->sigio = 0; |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 353 | } |
Richard Weinberger | 79e0273 | 2012-06-04 21:57:24 +0200 | [diff] [blame] | 354 | } |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 355 | |
Richard Weinberger | 79e0273 | 2012-06-04 21:57:24 +0200 | [diff] [blame] | 356 | void line_close(struct tty_struct *tty, struct file * filp) |
| 357 | { |
| 358 | struct line *line = tty->driver_data; |
| 359 | |
| 360 | tty_port_close(&line->port, tty, filp); |
| 361 | } |
| 362 | |
| 363 | void line_hangup(struct tty_struct *tty) |
| 364 | { |
| 365 | struct line *line = tty->driver_data; |
| 366 | |
| 367 | tty_port_hangup(&line->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void close_lines(struct line *lines, int nlines) |
| 371 | { |
| 372 | int i; |
| 373 | |
| 374 | for(i = 0; i < nlines; i++) |
Al Viro | 10c890c0 | 2011-09-10 08:39:18 -0400 | [diff] [blame] | 375 | close_chan(&lines[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Al Viro | 04292b2 | 2011-09-09 20:07:05 -0400 | [diff] [blame] | 378 | int setup_one_line(struct line *lines, int n, char *init, |
| 379 | const struct chan_opts *opts, char **error_out) |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 380 | { |
| 381 | struct line *line = &lines[n]; |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 382 | struct tty_driver *driver = line->driver->driver; |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 383 | int err = -EINVAL; |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 384 | |
Jiri Slaby | 060ed31 | 2012-06-04 13:35:26 +0200 | [diff] [blame] | 385 | if (line->port.count) { |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 386 | *error_out = "Device is already open"; |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 387 | goto out; |
| 388 | } |
| 389 | |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 390 | if (!strcmp(init, "none")) { |
| 391 | if (line->valid) { |
| 392 | line->valid = 0; |
| 393 | kfree(line->init_str); |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 394 | tty_unregister_device(driver, n); |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 395 | parse_chan_pair(NULL, line, n, opts, error_out); |
| 396 | err = 0; |
| 397 | } |
| 398 | } else { |
| 399 | char *new = kstrdup(init, GFP_KERNEL); |
| 400 | if (!new) { |
| 401 | *error_out = "Failed to allocate memory"; |
| 402 | return -ENOMEM; |
| 403 | } |
Al Viro | c8e2876 | 2011-09-09 20:08:48 -0400 | [diff] [blame] | 404 | if (line->valid) { |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 405 | tty_unregister_device(driver, n); |
Al Viro | c8e2876 | 2011-09-09 20:08:48 -0400 | [diff] [blame] | 406 | kfree(line->init_str); |
| 407 | } |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 408 | line->init_str = new; |
Al Viro | 43574c1 | 2011-09-09 17:25:00 -0400 | [diff] [blame] | 409 | line->valid = 1; |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 410 | err = parse_chan_pair(new, line, n, opts, error_out); |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 411 | if (!err) { |
Jiri Slaby | 734cc17 | 2012-08-07 21:47:47 +0200 | [diff] [blame] | 412 | struct device *d = tty_port_register_device(&line->port, |
| 413 | driver, n, NULL); |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 414 | if (IS_ERR(d)) { |
| 415 | *error_out = "Failed to register device"; |
| 416 | err = PTR_ERR(d); |
| 417 | parse_chan_pair(NULL, line, n, opts, error_out); |
| 418 | } |
| 419 | } |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 420 | if (err) { |
| 421 | line->init_str = NULL; |
| 422 | line->valid = 0; |
| 423 | kfree(new); |
| 424 | } |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 425 | } |
| 426 | out: |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 427 | return err; |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 430 | /* |
| 431 | * Common setup code for both startup command line and mconsole initialization. |
Matt LaPlante | 4b3f686 | 2006-10-03 22:21:02 +0200 | [diff] [blame] | 432 | * @lines contains the array (of size @num) to modify; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 433 | * @init is the setup string; |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 434 | * @error_out is an error string in the case of failure; |
Jeff Dike | d571cd1 | 2006-01-06 00:18:53 -0800 | [diff] [blame] | 435 | */ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 436 | |
Al Viro | 43574c1 | 2011-09-09 17:25:00 -0400 | [diff] [blame] | 437 | int line_setup(char **conf, unsigned int num, char **def, |
| 438 | char *init, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
Al Viro | 43574c1 | 2011-09-09 17:25:00 -0400 | [diff] [blame] | 440 | char *error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 442 | if (*init == '=') { |
| 443 | /* |
| 444 | * We said con=/ssl= instead of con#=, so we are configuring all |
| 445 | * consoles at once. |
| 446 | */ |
Al Viro | 43574c1 | 2011-09-09 17:25:00 -0400 | [diff] [blame] | 447 | *def = init + 1; |
| 448 | } else { |
| 449 | char *end; |
| 450 | unsigned n = simple_strtoul(init, &end, 0); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 451 | |
Al Viro | 43574c1 | 2011-09-09 17:25:00 -0400 | [diff] [blame] | 452 | if (*end != '=') { |
| 453 | error = "Couldn't parse device number"; |
| 454 | goto out; |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 455 | } |
Al Viro | 43574c1 | 2011-09-09 17:25:00 -0400 | [diff] [blame] | 456 | if (n >= num) { |
| 457 | error = "Device number out of range"; |
| 458 | goto out; |
| 459 | } |
| 460 | conf[n] = end + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |
Al Viro | 43574c1 | 2011-09-09 17:25:00 -0400 | [diff] [blame] | 462 | return 0; |
| 463 | |
| 464 | out: |
| 465 | printk(KERN_ERR "Failed to set up %s with " |
| 466 | "configuration string \"%s\" : %s\n", name, init, error); |
| 467 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 470 | int line_config(struct line *lines, unsigned int num, char *str, |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 471 | const struct chan_opts *opts, char **error_out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { |
Al Viro | fe9a6b0 | 2011-09-08 20:44:06 -0400 | [diff] [blame] | 473 | char *end; |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 474 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 476 | if (*str == '=') { |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 477 | *error_out = "Can't configure all devices from mconsole"; |
| 478 | return -EINVAL; |
Jeff Dike | d571cd1 | 2006-01-06 00:18:53 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Al Viro | fe9a6b0 | 2011-09-08 20:44:06 -0400 | [diff] [blame] | 481 | n = simple_strtoul(str, &end, 0); |
| 482 | if (*end++ != '=') { |
| 483 | *error_out = "Couldn't parse device number"; |
| 484 | return -EINVAL; |
| 485 | } |
| 486 | if (n >= num) { |
| 487 | *error_out = "Device number out of range"; |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 491 | return setup_one_line(lines, n, end, opts, error_out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 494 | int line_get_config(char *name, struct line *lines, unsigned int num, char *str, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | int size, char **error_out) |
| 496 | { |
| 497 | struct line *line; |
| 498 | char *end; |
| 499 | int dev, n = 0; |
| 500 | |
| 501 | dev = simple_strtoul(name, &end, 0); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 502 | if ((*end != '\0') || (end == name)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | *error_out = "line_get_config failed to parse device number"; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 504 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 507 | if ((dev < 0) || (dev >= num)) { |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 508 | *error_out = "device number out of range"; |
| 509 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | line = &lines[dev]; |
| 513 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 514 | if (!line->valid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | CONFIG_CHUNK(str, size, n, "none", 1); |
Jiri Slaby | 6fc5884 | 2012-06-04 13:35:27 +0200 | [diff] [blame] | 516 | else { |
| 517 | struct tty_struct *tty = tty_port_tty_get(&line->port); |
| 518 | if (tty == NULL) { |
| 519 | CONFIG_CHUNK(str, size, n, line->init_str, 1); |
| 520 | } else { |
| 521 | n = chan_config_string(line, str, size, error_out); |
| 522 | tty_kref_put(tty); |
| 523 | } |
| 524 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 526 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 529 | int line_id(char **str, int *start_out, int *end_out) |
| 530 | { |
| 531 | char *end; |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 532 | int n; |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 533 | |
| 534 | n = simple_strtoul(*str, &end, 0); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 535 | if ((*end != '\0') || (end == *str)) |
| 536 | return -1; |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 537 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 538 | *str = end; |
| 539 | *start_out = n; |
| 540 | *end_out = n; |
| 541 | return n; |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 544 | int line_remove(struct line *lines, unsigned int num, int n, char **error_out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | { |
Al Viro | da645f3 | 2011-09-08 20:34:52 -0400 | [diff] [blame] | 546 | if (n >= num) { |
| 547 | *error_out = "Device number out of range"; |
| 548 | return -EINVAL; |
| 549 | } |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 550 | return setup_one_line(lines, n, "none", NULL, error_out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 553 | int register_lines(struct line_driver *line_driver, |
| 554 | const struct tty_operations *ops, |
| 555 | struct line *lines, int nlines) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | struct tty_driver *driver = alloc_tty_driver(nlines); |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 558 | int err; |
Al Viro | 04292b2 | 2011-09-09 20:07:05 -0400 | [diff] [blame] | 559 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
| 561 | if (!driver) |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 562 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | driver->driver_name = line_driver->name; |
| 565 | driver->name = line_driver->device_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | driver->major = line_driver->major; |
| 567 | driver->minor_start = line_driver->minor_start; |
| 568 | driver->type = line_driver->type; |
| 569 | driver->subtype = line_driver->subtype; |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 570 | driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | driver->init_termios = tty_std_termios; |
Al Viro | 04292b2 | 2011-09-09 20:07:05 -0400 | [diff] [blame] | 572 | |
| 573 | for (i = 0; i < nlines; i++) { |
Jiri Slaby | 060ed31 | 2012-06-04 13:35:26 +0200 | [diff] [blame] | 574 | tty_port_init(&lines[i].port); |
Richard Weinberger | 79e0273 | 2012-06-04 21:57:24 +0200 | [diff] [blame] | 575 | lines[i].port.ops = &line_port_ops; |
Al Viro | 04292b2 | 2011-09-09 20:07:05 -0400 | [diff] [blame] | 576 | spin_lock_init(&lines[i].lock); |
Al Viro | 04292b2 | 2011-09-09 20:07:05 -0400 | [diff] [blame] | 577 | lines[i].driver = line_driver; |
| 578 | INIT_LIST_HEAD(&lines[i].chan_list); |
| 579 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | tty_set_operations(driver, ops); |
| 581 | |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 582 | err = tty_register_driver(driver); |
| 583 | if (err) { |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 584 | printk(KERN_ERR "register_lines : can't register %s driver\n", |
| 585 | line_driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | put_tty_driver(driver); |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 587 | for (i = 0; i < nlines; i++) |
| 588 | tty_port_destroy(&lines[i].port); |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 589 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 592 | line_driver->driver = driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | mconsole_register_dev(&line_driver->mc); |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 594 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Jeff Dike | 9010772 | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 597 | static DEFINE_SPINLOCK(winch_handler_lock); |
| 598 | static LIST_HEAD(winch_handlers); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | struct winch { |
| 601 | struct list_head list; |
| 602 | int fd; |
| 603 | int tty_fd; |
| 604 | int pid; |
| 605 | struct tty_struct *tty; |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 606 | unsigned long stack; |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 607 | struct work_struct work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | }; |
| 609 | |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 610 | static void __free_winch(struct work_struct *work) |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 611 | { |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 612 | struct winch *winch = container_of(work, struct winch, work); |
Richard Weinberger | fa7a044 | 2012-04-17 22:37:13 +0200 | [diff] [blame] | 613 | um_free_irq(WINCH_IRQ, winch); |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 614 | |
| 615 | if (winch->pid != -1) |
| 616 | os_kill_process(winch->pid, 1); |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 617 | if (winch->stack != 0) |
| 618 | free_stack(winch->stack, 0); |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 619 | kfree(winch); |
| 620 | } |
| 621 | |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 622 | static void free_winch(struct winch *winch) |
| 623 | { |
| 624 | int fd = winch->fd; |
| 625 | winch->fd = -1; |
| 626 | if (fd != -1) |
| 627 | os_close_file(fd); |
| 628 | list_del(&winch->list); |
| 629 | __free_winch(&winch->work); |
| 630 | } |
| 631 | |
Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 632 | static irqreturn_t winch_interrupt(int irq, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | { |
| 634 | struct winch *winch = data; |
| 635 | struct tty_struct *tty; |
| 636 | struct line *line; |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 637 | int fd = winch->fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | int err; |
| 639 | char c; |
| 640 | |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 641 | if (fd != -1) { |
| 642 | err = generic_read(fd, &c, NULL); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 643 | if (err < 0) { |
| 644 | if (err != -EAGAIN) { |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 645 | winch->fd = -1; |
| 646 | list_del(&winch->list); |
| 647 | os_close_file(fd); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 648 | printk(KERN_ERR "winch_interrupt : " |
| 649 | "read failed, errno = %d\n", -err); |
| 650 | printk(KERN_ERR "fd %d is losing SIGWINCH " |
| 651 | "support\n", winch->tty_fd); |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 652 | INIT_WORK(&winch->work, __free_winch); |
| 653 | schedule_work(&winch->work); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 654 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | } |
| 656 | goto out; |
| 657 | } |
| 658 | } |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 659 | tty = winch->tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | if (tty != NULL) { |
| 661 | line = tty->driver_data; |
Jeff Dike | 438ee67 | 2008-02-04 22:31:19 -0800 | [diff] [blame] | 662 | if (line != NULL) { |
Al Viro | bed5e39 | 2011-09-08 10:49:34 -0400 | [diff] [blame] | 663 | chan_window_size(line, &tty->winsize.ws_row, |
Jeff Dike | 438ee67 | 2008-02-04 22:31:19 -0800 | [diff] [blame] | 664 | &tty->winsize.ws_col); |
| 665 | kill_pgrp(tty->pgrp, SIGWINCH, 1); |
| 666 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | } |
| 668 | out: |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 669 | if (winch->fd != -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | reactivate_fd(winch->fd, WINCH_IRQ); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 671 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 674 | void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty, |
| 675 | unsigned long stack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | { |
| 677 | struct winch *winch; |
| 678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | winch = kmalloc(sizeof(*winch), GFP_KERNEL); |
| 680 | if (winch == NULL) { |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 681 | printk(KERN_ERR "register_winch_irq - kmalloc failed\n"); |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 682 | goto cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | } |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list), |
| 686 | .fd = fd, |
| 687 | .tty_fd = tty_fd, |
| 688 | .pid = pid, |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 689 | .tty = tty, |
| 690 | .stack = stack }); |
| 691 | |
| 692 | if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt, |
Theodore Ts'o | aab9446 | 2012-07-17 14:18:23 -0400 | [diff] [blame] | 693 | IRQF_SHARED, "winch", winch) < 0) { |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 694 | printk(KERN_ERR "register_winch_irq - failed to register " |
| 695 | "IRQ\n"); |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 696 | goto out_free; |
| 697 | } |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 698 | |
| 699 | spin_lock(&winch_handler_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | list_add(&winch->list, &winch_handlers); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 701 | spin_unlock(&winch_handler_lock); |
| 702 | |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 703 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 705 | out_free: |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 706 | kfree(winch); |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 707 | cleanup: |
| 708 | os_kill_process(pid, 1); |
| 709 | os_close_file(fd); |
| 710 | if (stack != 0) |
| 711 | free_stack(stack, 0); |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 714 | static void unregister_winch(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | { |
Will Newton | 48a0b74 | 2011-01-12 16:59:26 -0800 | [diff] [blame] | 716 | struct list_head *ele, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | struct winch *winch; |
| 718 | |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 719 | spin_lock(&winch_handler_lock); |
| 720 | |
Will Newton | 48a0b74 | 2011-01-12 16:59:26 -0800 | [diff] [blame] | 721 | list_for_each_safe(ele, next, &winch_handlers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | winch = list_entry(ele, struct winch, list); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 723 | if (winch->tty == tty) { |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 724 | free_winch(winch); |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 725 | break; |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 726 | } |
| 727 | } |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 728 | spin_unlock(&winch_handler_lock); |
| 729 | } |
| 730 | |
| 731 | static void winch_cleanup(void) |
| 732 | { |
| 733 | struct list_head *ele, *next; |
| 734 | struct winch *winch; |
| 735 | |
| 736 | spin_lock(&winch_handler_lock); |
| 737 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 738 | list_for_each_safe(ele, next, &winch_handlers) { |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 739 | winch = list_entry(ele, struct winch, list); |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 740 | free_winch(winch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | } |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 742 | |
| 743 | spin_unlock(&winch_handler_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
| 745 | __uml_exitcall(winch_cleanup); |
| 746 | |
| 747 | char *add_xterm_umid(char *base) |
| 748 | { |
| 749 | char *umid, *title; |
| 750 | int len; |
| 751 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 752 | umid = get_umid(); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 753 | if (*umid == '\0') |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 754 | return base; |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | len = strlen(base) + strlen(" ()") + strlen(umid) + 1; |
| 757 | title = kmalloc(len, GFP_KERNEL); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 758 | if (title == NULL) { |
| 759 | printk(KERN_ERR "Failed to allocate buffer for xterm title\n"); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 760 | return base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | snprintf(title, len, "%s (%s)", base, umid); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 764 | return title; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | } |