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 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 6 | #include "linux/irqreturn.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include "linux/kd.h" |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 8 | #include "linux/sched.h" |
Jan Kiszka | 7f3c1fa | 2010-04-19 17:46:23 +0900 | [diff] [blame] | 9 | #include "linux/slab.h" |
Al Viro | 510c72a3 | 2011-08-18 20:08:29 +0100 | [diff] [blame] | 10 | #include "chan.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include "irq_kern.h" |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 12 | #include "irq_user.h" |
Jeff Dike | edea138 | 2008-02-04 22:30:46 -0800 | [diff] [blame] | 13 | #include "kern_util.h" |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 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 | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 224 | static const struct { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | int cmd; |
| 226 | char *level; |
| 227 | char *name; |
| 228 | } tty_ioctls[] = { |
| 229 | /* don't print these, they flood the log ... */ |
| 230 | { TCGETS, NULL, "TCGETS" }, |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 231 | { TCSETS, NULL, "TCSETS" }, |
| 232 | { TCSETSW, NULL, "TCSETSW" }, |
| 233 | { TCFLSH, NULL, "TCFLSH" }, |
| 234 | { TCSBRK, NULL, "TCSBRK" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
| 236 | /* general tty stuff */ |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 237 | { TCSETSF, KERN_DEBUG, "TCSETSF" }, |
| 238 | { TCGETA, KERN_DEBUG, "TCGETA" }, |
| 239 | { TIOCMGET, KERN_DEBUG, "TIOCMGET" }, |
| 240 | { TCSBRKP, KERN_DEBUG, "TCSBRKP" }, |
| 241 | { TIOCMSET, KERN_DEBUG, "TIOCMSET" }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | /* linux-specific ones */ |
| 244 | { TIOCLINUX, KERN_INFO, "TIOCLINUX" }, |
| 245 | { KDGKBMODE, KERN_INFO, "KDGKBMODE" }, |
| 246 | { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" }, |
| 247 | { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" }, |
| 248 | }; |
| 249 | |
Richard Weinberger | 8a06dc4d | 2011-03-22 16:33:48 -0700 | [diff] [blame] | 250 | int line_ioctl(struct tty_struct *tty, unsigned int cmd, |
| 251 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
| 253 | int ret; |
| 254 | int i; |
| 255 | |
| 256 | ret = 0; |
| 257 | switch(cmd) { |
| 258 | #ifdef TIOCGETP |
| 259 | case TIOCGETP: |
| 260 | case TIOCSETP: |
| 261 | case TIOCSETN: |
| 262 | #endif |
| 263 | #ifdef TIOCGETC |
| 264 | case TIOCGETC: |
| 265 | case TIOCSETC: |
| 266 | #endif |
| 267 | #ifdef TIOCGLTC |
| 268 | case TIOCGLTC: |
| 269 | case TIOCSLTC: |
| 270 | #endif |
Alan Cox | c564b6f | 2008-10-13 10:36:49 +0100 | [diff] [blame] | 271 | /* Note: these are out of date as we now have TCGETS2 etc but this |
| 272 | whole lot should probably go away */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | case TCGETS: |
| 274 | case TCSETSF: |
| 275 | case TCSETSW: |
| 276 | case TCSETS: |
| 277 | case TCGETA: |
| 278 | case TCSETAF: |
| 279 | case TCSETAW: |
| 280 | case TCSETA: |
| 281 | case TCXONC: |
| 282 | case TCFLSH: |
| 283 | case TIOCOUTQ: |
| 284 | case TIOCINQ: |
| 285 | case TIOCGLCKTRMIOS: |
| 286 | case TIOCSLCKTRMIOS: |
| 287 | case TIOCPKT: |
| 288 | case TIOCGSOFTCAR: |
| 289 | case TIOCSSOFTCAR: |
| 290 | return -ENOIOCTLCMD; |
| 291 | #if 0 |
| 292 | case TCwhatever: |
| 293 | /* do something */ |
| 294 | break; |
| 295 | #endif |
| 296 | default: |
| 297 | for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++) |
| 298 | if (cmd == tty_ioctls[i].cmd) |
| 299 | break; |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 300 | if (i == ARRAY_SIZE(tty_ioctls)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n", |
Harvey Harrison | 3595726 | 2008-04-28 02:13:53 -0700 | [diff] [blame] | 302 | __func__, tty->name, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
| 304 | ret = -ENOIOCTLCMD; |
| 305 | break; |
| 306 | } |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 307 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 310 | void line_throttle(struct tty_struct *tty) |
| 311 | { |
| 312 | struct line *line = tty->driver_data; |
| 313 | |
Al Viro | bed5e39 | 2011-09-08 10:49:34 -0400 | [diff] [blame] | 314 | deactivate_chan(line->chan_in, line->driver->read_irq); |
Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 315 | line->throttled = 1; |
| 316 | } |
| 317 | |
| 318 | void line_unthrottle(struct tty_struct *tty) |
| 319 | { |
| 320 | struct line *line = tty->driver_data; |
| 321 | |
| 322 | line->throttled = 0; |
Al Viro | 0fcd719 | 2011-09-10 08:17:04 -0400 | [diff] [blame] | 323 | chan_interrupt(line, tty, line->driver->read_irq); |
Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 324 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 325 | /* |
| 326 | * Maybe there is enough stuff pending that calling the interrupt |
Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 327 | * throttles us again. In this case, line->throttled will be 1 |
| 328 | * again and we shouldn't turn the interrupt back on. |
| 329 | */ |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 330 | if (!line->throttled) |
Al Viro | bed5e39 | 2011-09-08 10:49:34 -0400 | [diff] [blame] | 331 | reactivate_chan(line->chan_in, line->driver->read_irq); |
Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 334 | static irqreturn_t line_write_interrupt(int irq, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | { |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 336 | struct chan *chan = data; |
| 337 | struct line *line = chan->line; |
Jiri Slaby | 6fc5884 | 2012-06-04 13:35:27 +0200 | [diff] [blame^] | 338 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | int err; |
| 340 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 341 | /* |
Yong Zhang | c0b79a9 | 2011-09-22 16:58:46 +0800 | [diff] [blame] | 342 | * Interrupts are disabled here because genirq keep irqs disabled when |
| 343 | * calling the action handler. |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 344 | */ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 345 | |
Paolo 'Blaisorblade' Giarrusso | ec0ac8a | 2007-03-07 20:41:12 -0800 | [diff] [blame] | 346 | spin_lock(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | err = flush_buffer(line); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 348 | if (err == 0) { |
Al Viro | 199eebb | 2012-02-11 03:05:32 -0500 | [diff] [blame] | 349 | spin_unlock(&line->lock); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 350 | return IRQ_NONE; |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 351 | } else if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | line->head = line->buffer; |
| 353 | line->tail = line->buffer; |
| 354 | } |
Paolo 'Blaisorblade' Giarrusso | ec0ac8a | 2007-03-07 20:41:12 -0800 | [diff] [blame] | 355 | spin_unlock(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Jiri Slaby | 6fc5884 | 2012-06-04 13:35:27 +0200 | [diff] [blame^] | 357 | tty = tty_port_tty_get(&line->port); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 358 | if (tty == NULL) |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 359 | return IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
WANG Cong | 06ac667 | 2008-07-29 22:33:34 -0700 | [diff] [blame] | 361 | tty_wakeup(tty); |
Jiri Slaby | 6fc5884 | 2012-06-04 13:35:27 +0200 | [diff] [blame^] | 362 | tty_kref_put(tty); |
| 363 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 364 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 367 | 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] | 368 | { |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 369 | const struct line_driver *driver = line->driver; |
Yong Zhang | c0b79a9 | 2011-09-22 16:58:46 +0800 | [diff] [blame] | 370 | int err = 0, flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 372 | if (input) |
| 373 | err = um_request_irq(driver->read_irq, fd, IRQ_READ, |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 374 | line_interrupt, flags, |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 375 | driver->read_irq_name, data); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 376 | if (err) |
| 377 | return err; |
| 378 | if (output) |
| 379 | err = um_request_irq(driver->write_irq, fd, IRQ_WRITE, |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 380 | line_write_interrupt, flags, |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 381 | driver->write_irq_name, data); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 382 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 385 | /* |
| 386 | * Normally, a driver like this can rely mostly on the tty layer |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 387 | * locking, particularly when it comes to the driver structure. |
| 388 | * However, in this case, mconsole requests can come in "from the |
| 389 | * side", and race with opens and closes. |
| 390 | * |
Jeff Dike | c6256c6 | 2007-02-10 01:44:08 -0800 | [diff] [blame] | 391 | * mconsole config requests will want to be sure the device isn't in |
| 392 | * use, and get_config, open, and close will want a stable |
| 393 | * configuration. The checking and modification of the configuration |
| 394 | * is done under a spinlock. Checking whether the device is in use is |
| 395 | * line->tty->count > 1, also under the spinlock. |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 396 | * |
Al Viro | f71f948 | 2011-09-14 16:21:25 -0700 | [diff] [blame] | 397 | * line->count serves to decide whether the device should be enabled or |
| 398 | * disabled on the host. If it's equal to 0, then we are doing the |
Jeff Dike | c6256c6 | 2007-02-10 01:44:08 -0800 | [diff] [blame] | 399 | * first open or last close. Otherwise, open and close just return. |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 400 | */ |
| 401 | |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 402 | int line_open(struct line *lines, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 404 | struct line *line = &lines[tty->index]; |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 405 | int err = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
Al Viro | d8c215a | 2011-09-09 17:36:37 -0400 | [diff] [blame] | 407 | mutex_lock(&line->count_lock); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 408 | if (!line->valid) |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 409 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 411 | err = 0; |
Jiri Slaby | 060ed31 | 2012-06-04 13:35:26 +0200 | [diff] [blame] | 412 | if (line->port.count++) |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 413 | goto out_unlock; |
| 414 | |
Al Viro | f71f948 | 2011-09-14 16:21:25 -0700 | [diff] [blame] | 415 | BUG_ON(tty->driver_data); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 416 | tty->driver_data = line; |
Jiri Slaby | 6fc5884 | 2012-06-04 13:35:27 +0200 | [diff] [blame^] | 417 | tty_port_tty_set(&line->port, tty); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 418 | |
Jeff Dike | d14ad81 | 2007-07-15 23:38:54 -0700 | [diff] [blame] | 419 | err = enable_chan(line); |
Al Viro | f71f948 | 2011-09-14 16:21:25 -0700 | [diff] [blame] | 420 | if (err) /* line_close() will be called by our caller */ |
Al Viro | d8c215a | 2011-09-09 17:36:37 -0400 | [diff] [blame] | 421 | goto out_unlock; |
Jeff Dike | d14ad81 | 2007-07-15 23:38:54 -0700 | [diff] [blame] | 422 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 423 | if (!line->sigio) { |
Al Viro | bed5e39 | 2011-09-08 10:49:34 -0400 | [diff] [blame] | 424 | chan_enable_winch(line->chan_out, tty); |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 425 | line->sigio = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Al Viro | bed5e39 | 2011-09-08 10:49:34 -0400 | [diff] [blame] | 428 | chan_window_size(line, &tty->winsize.ws_row, |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 429 | &tty->winsize.ws_col); |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 430 | out_unlock: |
Al Viro | d8c215a | 2011-09-09 17:36:37 -0400 | [diff] [blame] | 431 | mutex_unlock(&line->count_lock); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 432 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 435 | static void unregister_winch(struct tty_struct *tty); |
| 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | void line_close(struct tty_struct *tty, struct file * filp) |
| 438 | { |
| 439 | struct line *line = tty->driver_data; |
| 440 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 441 | /* |
| 442 | * If line_open fails (and tty->driver_data is never set), |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 443 | * tty_open will call line_close. So just return in this case. |
| 444 | */ |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 445 | if (line == NULL) |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 446 | return; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 447 | |
| 448 | /* We ignore the error anyway! */ |
| 449 | flush_buffer(line); |
| 450 | |
Al Viro | d8c215a | 2011-09-09 17:36:37 -0400 | [diff] [blame] | 451 | mutex_lock(&line->count_lock); |
Al Viro | f71f948 | 2011-09-14 16:21:25 -0700 | [diff] [blame] | 452 | BUG_ON(!line->valid); |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 453 | |
Jiri Slaby | 060ed31 | 2012-06-04 13:35:26 +0200 | [diff] [blame] | 454 | if (--line->port.count) |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 455 | goto out_unlock; |
| 456 | |
Jiri Slaby | 6fc5884 | 2012-06-04 13:35:27 +0200 | [diff] [blame^] | 457 | tty_port_tty_set(&line->port, NULL); |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 458 | tty->driver_data = NULL; |
| 459 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 460 | if (line->sigio) { |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 461 | unregister_winch(tty); |
| 462 | line->sigio = 0; |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 463 | } |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 464 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 465 | out_unlock: |
Al Viro | d8c215a | 2011-09-09 17:36:37 -0400 | [diff] [blame] | 466 | mutex_unlock(&line->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | void close_lines(struct line *lines, int nlines) |
| 470 | { |
| 471 | int i; |
| 472 | |
| 473 | for(i = 0; i < nlines; i++) |
Al Viro | 10c890c | 2011-09-10 08:39:18 -0400 | [diff] [blame] | 474 | close_chan(&lines[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Al Viro | 04292b2 | 2011-09-09 20:07:05 -0400 | [diff] [blame] | 477 | int setup_one_line(struct line *lines, int n, char *init, |
| 478 | const struct chan_opts *opts, char **error_out) |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 479 | { |
| 480 | struct line *line = &lines[n]; |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 481 | struct tty_driver *driver = line->driver->driver; |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 482 | int err = -EINVAL; |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 483 | |
Al Viro | d8c215a | 2011-09-09 17:36:37 -0400 | [diff] [blame] | 484 | mutex_lock(&line->count_lock); |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 485 | |
Jiri Slaby | 060ed31 | 2012-06-04 13:35:26 +0200 | [diff] [blame] | 486 | if (line->port.count) { |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 487 | *error_out = "Device is already open"; |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 488 | goto out; |
| 489 | } |
| 490 | |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 491 | if (!strcmp(init, "none")) { |
| 492 | if (line->valid) { |
| 493 | line->valid = 0; |
| 494 | kfree(line->init_str); |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 495 | tty_unregister_device(driver, n); |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 496 | parse_chan_pair(NULL, line, n, opts, error_out); |
| 497 | err = 0; |
| 498 | } |
| 499 | } else { |
| 500 | char *new = kstrdup(init, GFP_KERNEL); |
| 501 | if (!new) { |
| 502 | *error_out = "Failed to allocate memory"; |
| 503 | return -ENOMEM; |
| 504 | } |
Al Viro | c8e2876 | 2011-09-09 20:08:48 -0400 | [diff] [blame] | 505 | if (line->valid) { |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 506 | tty_unregister_device(driver, n); |
Al Viro | c8e2876 | 2011-09-09 20:08:48 -0400 | [diff] [blame] | 507 | kfree(line->init_str); |
| 508 | } |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 509 | line->init_str = new; |
Al Viro | 43574c1 | 2011-09-09 17:25:00 -0400 | [diff] [blame] | 510 | line->valid = 1; |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 511 | err = parse_chan_pair(new, line, n, opts, error_out); |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 512 | if (!err) { |
| 513 | struct device *d = tty_register_device(driver, n, NULL); |
| 514 | if (IS_ERR(d)) { |
| 515 | *error_out = "Failed to register device"; |
| 516 | err = PTR_ERR(d); |
| 517 | parse_chan_pair(NULL, line, n, opts, error_out); |
| 518 | } |
| 519 | } |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 520 | if (err) { |
| 521 | line->init_str = NULL; |
| 522 | line->valid = 0; |
| 523 | kfree(new); |
| 524 | } |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 525 | } |
| 526 | out: |
Al Viro | d8c215a | 2011-09-09 17:36:37 -0400 | [diff] [blame] | 527 | mutex_unlock(&line->count_lock); |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 528 | return err; |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 531 | /* |
| 532 | * Common setup code for both startup command line and mconsole initialization. |
Matt LaPlante | 4b3f686 | 2006-10-03 22:21:02 +0200 | [diff] [blame] | 533 | * @lines contains the array (of size @num) to modify; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 534 | * @init is the setup string; |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 535 | * @error_out is an error string in the case of failure; |
Jeff Dike | d571cd1 | 2006-01-06 00:18:53 -0800 | [diff] [blame] | 536 | */ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 537 | |
Al Viro | 43574c1 | 2011-09-09 17:25:00 -0400 | [diff] [blame] | 538 | int line_setup(char **conf, unsigned int num, char **def, |
| 539 | char *init, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | { |
Al Viro | 43574c1 | 2011-09-09 17:25:00 -0400 | [diff] [blame] | 541 | char *error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 543 | if (*init == '=') { |
| 544 | /* |
| 545 | * We said con=/ssl= instead of con#=, so we are configuring all |
| 546 | * consoles at once. |
| 547 | */ |
Al Viro | 43574c1 | 2011-09-09 17:25:00 -0400 | [diff] [blame] | 548 | *def = init + 1; |
| 549 | } else { |
| 550 | char *end; |
| 551 | unsigned n = simple_strtoul(init, &end, 0); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 552 | |
Al Viro | 43574c1 | 2011-09-09 17:25:00 -0400 | [diff] [blame] | 553 | if (*end != '=') { |
| 554 | error = "Couldn't parse device number"; |
| 555 | goto out; |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 556 | } |
Al Viro | 43574c1 | 2011-09-09 17:25:00 -0400 | [diff] [blame] | 557 | if (n >= num) { |
| 558 | error = "Device number out of range"; |
| 559 | goto out; |
| 560 | } |
| 561 | conf[n] = end + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } |
Al Viro | 43574c1 | 2011-09-09 17:25:00 -0400 | [diff] [blame] | 563 | return 0; |
| 564 | |
| 565 | out: |
| 566 | printk(KERN_ERR "Failed to set up %s with " |
| 567 | "configuration string \"%s\" : %s\n", name, init, error); |
| 568 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 571 | int line_config(struct line *lines, unsigned int num, char *str, |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 572 | const struct chan_opts *opts, char **error_out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | { |
Al Viro | fe9a6b0 | 2011-09-08 20:44:06 -0400 | [diff] [blame] | 574 | char *end; |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 575 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 577 | if (*str == '=') { |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 578 | *error_out = "Can't configure all devices from mconsole"; |
| 579 | return -EINVAL; |
Jeff Dike | d571cd1 | 2006-01-06 00:18:53 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Al Viro | fe9a6b0 | 2011-09-08 20:44:06 -0400 | [diff] [blame] | 582 | n = simple_strtoul(str, &end, 0); |
| 583 | if (*end++ != '=') { |
| 584 | *error_out = "Couldn't parse device number"; |
| 585 | return -EINVAL; |
| 586 | } |
| 587 | if (n >= num) { |
| 588 | *error_out = "Device number out of range"; |
| 589 | return -EINVAL; |
| 590 | } |
| 591 | |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 592 | return setup_one_line(lines, n, end, opts, error_out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 595 | 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] | 596 | int size, char **error_out) |
| 597 | { |
| 598 | struct line *line; |
| 599 | char *end; |
| 600 | int dev, n = 0; |
| 601 | |
| 602 | dev = simple_strtoul(name, &end, 0); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 603 | if ((*end != '\0') || (end == name)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | *error_out = "line_get_config failed to parse device number"; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 605 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 608 | if ((dev < 0) || (dev >= num)) { |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 609 | *error_out = "device number out of range"; |
| 610 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | line = &lines[dev]; |
| 614 | |
Al Viro | d8c215a | 2011-09-09 17:36:37 -0400 | [diff] [blame] | 615 | mutex_lock(&line->count_lock); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 616 | if (!line->valid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | CONFIG_CHUNK(str, size, n, "none", 1); |
Jiri Slaby | 6fc5884 | 2012-06-04 13:35:27 +0200 | [diff] [blame^] | 618 | else { |
| 619 | struct tty_struct *tty = tty_port_tty_get(&line->port); |
| 620 | if (tty == NULL) { |
| 621 | CONFIG_CHUNK(str, size, n, line->init_str, 1); |
| 622 | } else { |
| 623 | n = chan_config_string(line, str, size, error_out); |
| 624 | tty_kref_put(tty); |
| 625 | } |
| 626 | } |
Al Viro | d8c215a | 2011-09-09 17:36:37 -0400 | [diff] [blame] | 627 | mutex_unlock(&line->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 629 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 632 | int line_id(char **str, int *start_out, int *end_out) |
| 633 | { |
| 634 | char *end; |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 635 | int n; |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 636 | |
| 637 | n = simple_strtoul(*str, &end, 0); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 638 | if ((*end != '\0') || (end == *str)) |
| 639 | return -1; |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 640 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 641 | *str = end; |
| 642 | *start_out = n; |
| 643 | *end_out = n; |
| 644 | return n; |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 647 | 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] | 648 | { |
Al Viro | da645f3 | 2011-09-08 20:34:52 -0400 | [diff] [blame] | 649 | if (n >= num) { |
| 650 | *error_out = "Device number out of range"; |
| 651 | return -EINVAL; |
| 652 | } |
Al Viro | 31efceb | 2011-09-09 19:14:02 -0400 | [diff] [blame] | 653 | return setup_one_line(lines, n, "none", NULL, error_out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 656 | int register_lines(struct line_driver *line_driver, |
| 657 | const struct tty_operations *ops, |
| 658 | struct line *lines, int nlines) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | struct tty_driver *driver = alloc_tty_driver(nlines); |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 661 | int err; |
Al Viro | 04292b2 | 2011-09-09 20:07:05 -0400 | [diff] [blame] | 662 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | if (!driver) |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 665 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
| 667 | driver->driver_name = line_driver->name; |
| 668 | driver->name = line_driver->device_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | driver->major = line_driver->major; |
| 670 | driver->minor_start = line_driver->minor_start; |
| 671 | driver->type = line_driver->type; |
| 672 | driver->subtype = line_driver->subtype; |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 673 | driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | driver->init_termios = tty_std_termios; |
Al Viro | 04292b2 | 2011-09-09 20:07:05 -0400 | [diff] [blame] | 675 | |
| 676 | for (i = 0; i < nlines; i++) { |
Jiri Slaby | 060ed31 | 2012-06-04 13:35:26 +0200 | [diff] [blame] | 677 | tty_port_init(&lines[i].port); |
Al Viro | 04292b2 | 2011-09-09 20:07:05 -0400 | [diff] [blame] | 678 | spin_lock_init(&lines[i].lock); |
| 679 | mutex_init(&lines[i].count_lock); |
| 680 | lines[i].driver = line_driver; |
| 681 | INIT_LIST_HEAD(&lines[i].chan_list); |
| 682 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | tty_set_operations(driver, ops); |
| 684 | |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 685 | err = tty_register_driver(driver); |
| 686 | if (err) { |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 687 | printk(KERN_ERR "register_lines : can't register %s driver\n", |
| 688 | line_driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | put_tty_driver(driver); |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 690 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 693 | line_driver->driver = driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | mconsole_register_dev(&line_driver->mc); |
Al Viro | cfe6b7c | 2011-09-09 19:45:42 -0400 | [diff] [blame] | 695 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Jeff Dike | 9010772 | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 698 | static DEFINE_SPINLOCK(winch_handler_lock); |
| 699 | static LIST_HEAD(winch_handlers); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | struct winch { |
| 702 | struct list_head list; |
| 703 | int fd; |
| 704 | int tty_fd; |
| 705 | int pid; |
| 706 | struct tty_struct *tty; |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 707 | unsigned long stack; |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 708 | struct work_struct work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | }; |
| 710 | |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 711 | static void __free_winch(struct work_struct *work) |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 712 | { |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 713 | struct winch *winch = container_of(work, struct winch, work); |
Richard Weinberger | fa7a044 | 2012-04-17 22:37:13 +0200 | [diff] [blame] | 714 | um_free_irq(WINCH_IRQ, winch); |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 715 | |
| 716 | if (winch->pid != -1) |
| 717 | os_kill_process(winch->pid, 1); |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 718 | if (winch->stack != 0) |
| 719 | free_stack(winch->stack, 0); |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 720 | kfree(winch); |
| 721 | } |
| 722 | |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 723 | static void free_winch(struct winch *winch) |
| 724 | { |
| 725 | int fd = winch->fd; |
| 726 | winch->fd = -1; |
| 727 | if (fd != -1) |
| 728 | os_close_file(fd); |
| 729 | list_del(&winch->list); |
| 730 | __free_winch(&winch->work); |
| 731 | } |
| 732 | |
Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 733 | static irqreturn_t winch_interrupt(int irq, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | { |
| 735 | struct winch *winch = data; |
| 736 | struct tty_struct *tty; |
| 737 | struct line *line; |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 738 | int fd = winch->fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | int err; |
| 740 | char c; |
| 741 | |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 742 | if (fd != -1) { |
| 743 | err = generic_read(fd, &c, NULL); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 744 | if (err < 0) { |
| 745 | if (err != -EAGAIN) { |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 746 | winch->fd = -1; |
| 747 | list_del(&winch->list); |
| 748 | os_close_file(fd); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 749 | printk(KERN_ERR "winch_interrupt : " |
| 750 | "read failed, errno = %d\n", -err); |
| 751 | printk(KERN_ERR "fd %d is losing SIGWINCH " |
| 752 | "support\n", winch->tty_fd); |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 753 | INIT_WORK(&winch->work, __free_winch); |
| 754 | schedule_work(&winch->work); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 755 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | goto out; |
| 758 | } |
| 759 | } |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 760 | tty = winch->tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | if (tty != NULL) { |
| 762 | line = tty->driver_data; |
Jeff Dike | 438ee67 | 2008-02-04 22:31:19 -0800 | [diff] [blame] | 763 | if (line != NULL) { |
Al Viro | bed5e39 | 2011-09-08 10:49:34 -0400 | [diff] [blame] | 764 | chan_window_size(line, &tty->winsize.ws_row, |
Jeff Dike | 438ee67 | 2008-02-04 22:31:19 -0800 | [diff] [blame] | 765 | &tty->winsize.ws_col); |
| 766 | kill_pgrp(tty->pgrp, SIGWINCH, 1); |
| 767 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
| 769 | out: |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 770 | if (winch->fd != -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | reactivate_fd(winch->fd, WINCH_IRQ); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 772 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 775 | void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty, |
| 776 | unsigned long stack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | { |
| 778 | struct winch *winch; |
| 779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | winch = kmalloc(sizeof(*winch), GFP_KERNEL); |
| 781 | if (winch == NULL) { |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 782 | printk(KERN_ERR "register_winch_irq - kmalloc failed\n"); |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 783 | goto cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | } |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list), |
| 787 | .fd = fd, |
| 788 | .tty_fd = tty_fd, |
| 789 | .pid = pid, |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 790 | .tty = tty, |
| 791 | .stack = stack }); |
| 792 | |
| 793 | if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt, |
Yong Zhang | c0b79a9 | 2011-09-22 16:58:46 +0800 | [diff] [blame] | 794 | IRQF_SHARED | IRQF_SAMPLE_RANDOM, |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 795 | "winch", winch) < 0) { |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 796 | printk(KERN_ERR "register_winch_irq - failed to register " |
| 797 | "IRQ\n"); |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 798 | goto out_free; |
| 799 | } |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 800 | |
| 801 | spin_lock(&winch_handler_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | list_add(&winch->list, &winch_handlers); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 803 | spin_unlock(&winch_handler_lock); |
| 804 | |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 805 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 807 | out_free: |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 808 | kfree(winch); |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 809 | cleanup: |
| 810 | os_kill_process(pid, 1); |
| 811 | os_close_file(fd); |
| 812 | if (stack != 0) |
| 813 | free_stack(stack, 0); |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 816 | static void unregister_winch(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | { |
Will Newton | 48a0b74 | 2011-01-12 16:59:26 -0800 | [diff] [blame] | 818 | struct list_head *ele, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | struct winch *winch; |
| 820 | |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 821 | spin_lock(&winch_handler_lock); |
| 822 | |
Will Newton | 48a0b74 | 2011-01-12 16:59:26 -0800 | [diff] [blame] | 823 | list_for_each_safe(ele, next, &winch_handlers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | winch = list_entry(ele, struct winch, list); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 825 | if (winch->tty == tty) { |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 826 | free_winch(winch); |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 827 | break; |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 828 | } |
| 829 | } |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 830 | spin_unlock(&winch_handler_lock); |
| 831 | } |
| 832 | |
| 833 | static void winch_cleanup(void) |
| 834 | { |
| 835 | struct list_head *ele, *next; |
| 836 | struct winch *winch; |
| 837 | |
| 838 | spin_lock(&winch_handler_lock); |
| 839 | |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 840 | list_for_each_safe(ele, next, &winch_handlers) { |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 841 | winch = list_entry(ele, struct winch, list); |
Al Viro | 7cf3cf2 | 2011-09-14 16:21:31 -0700 | [diff] [blame] | 842 | free_winch(winch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | } |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 844 | |
| 845 | spin_unlock(&winch_handler_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | } |
| 847 | __uml_exitcall(winch_cleanup); |
| 848 | |
| 849 | char *add_xterm_umid(char *base) |
| 850 | { |
| 851 | char *umid, *title; |
| 852 | int len; |
| 853 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 854 | umid = get_umid(); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 855 | if (*umid == '\0') |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 856 | return base; |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | len = strlen(base) + strlen(" ()") + strlen(umid) + 1; |
| 859 | title = kmalloc(len, GFP_KERNEL); |
Jeff Dike | 2f8a2dc | 2007-10-16 01:26:43 -0700 | [diff] [blame] | 860 | if (title == NULL) { |
| 861 | printk(KERN_ERR "Failed to allocate buffer for xterm title\n"); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 862 | return base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | snprintf(title, len, "%s (%s)", base, umid); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 866 | return title; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | } |