Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Jeff Dike | e16f535 | 2007-06-08 13:46:54 -0700 | [diff] [blame] | 6 | #include "linux/kernel.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include "linux/sched.h" |
| 8 | #include "linux/slab.h" |
| 9 | #include "linux/list.h" |
| 10 | #include "linux/kd.h" |
| 11 | #include "linux/interrupt.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "asm/uaccess.h" |
| 13 | #include "chan_kern.h" |
| 14 | #include "irq_user.h" |
| 15 | #include "line.h" |
| 16 | #include "kern.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "kern_util.h" |
| 18 | #include "os.h" |
| 19 | #include "irq_kern.h" |
| 20 | |
| 21 | #define LINE_BUFSIZE 4096 |
| 22 | |
Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 23 | static irqreturn_t line_interrupt(int irq, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | { |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 25 | struct chan *chan = data; |
| 26 | struct line *line = chan->line; |
| 27 | struct tty_struct *tty = line->tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | if (line) |
| 30 | chan_interrupt(&line->chan_list, &line->task, tty, irq); |
| 31 | return IRQ_HANDLED; |
| 32 | } |
| 33 | |
Andrew Morton | a2ce774 | 2006-12-06 20:31:36 -0800 | [diff] [blame] | 34 | static void line_timer_cb(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
Andrew Morton | a2ce774 | 2006-12-06 20:31:36 -0800 | [diff] [blame] | 36 | struct line *line = container_of(work, struct line, task.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 38 | if(!line->throttled) |
| 39 | chan_interrupt(&line->chan_list, &line->task, line->tty, |
| 40 | line->driver->read_irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 43 | /* Returns the free space inside the ring buffer of this line. |
| 44 | * |
| 45 | * Should be called while holding line->lock (this does not modify datas). |
| 46 | */ |
| 47 | static int write_room(struct line *line) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
| 49 | int n; |
| 50 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 51 | if (line->buffer == NULL) |
| 52 | return LINE_BUFSIZE - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 54 | /* This is for the case where the buffer is wrapped! */ |
| 55 | n = line->head - line->tail; |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | if (n <= 0) |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 58 | n = LINE_BUFSIZE + n; /* The other case */ |
| 59 | return n - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 62 | int line_write_room(struct tty_struct *tty) |
| 63 | { |
| 64 | struct line *line = tty->driver_data; |
| 65 | unsigned long flags; |
| 66 | int room; |
| 67 | |
| 68 | if (tty->stopped) |
| 69 | return 0; |
| 70 | |
| 71 | spin_lock_irqsave(&line->lock, flags); |
| 72 | room = write_room(line); |
| 73 | spin_unlock_irqrestore(&line->lock, flags); |
| 74 | |
| 75 | /*XXX: Warning to remove */ |
| 76 | if (0 == room) |
| 77 | printk(KERN_DEBUG "%s: %s: no room left in buffer\n", |
| 78 | __FUNCTION__,tty->name); |
| 79 | return room; |
| 80 | } |
| 81 | |
| 82 | int line_chars_in_buffer(struct tty_struct *tty) |
| 83 | { |
| 84 | struct line *line = tty->driver_data; |
| 85 | unsigned long flags; |
| 86 | int ret; |
| 87 | |
| 88 | spin_lock_irqsave(&line->lock, flags); |
| 89 | |
| 90 | /*write_room subtracts 1 for the needed NULL, so we readd it.*/ |
| 91 | ret = LINE_BUFSIZE - (write_room(line) + 1); |
| 92 | spin_unlock_irqrestore(&line->lock, flags); |
| 93 | |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * This copies the content of buf into the circular buffer associated with |
| 99 | * this line. |
| 100 | * The return value is the number of characters actually copied, i.e. the ones |
| 101 | * for which there was space: this function is not supposed to ever flush out |
| 102 | * the circular buffer. |
| 103 | * |
| 104 | * Must be called while holding line->lock! |
| 105 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | static int buffer_data(struct line *line, const char *buf, int len) |
| 107 | { |
| 108 | int end, room; |
| 109 | |
| 110 | if(line->buffer == NULL){ |
| 111 | line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC); |
| 112 | if (line->buffer == NULL) { |
| 113 | printk("buffer_data - atomic allocation failed\n"); |
| 114 | return(0); |
| 115 | } |
| 116 | line->head = line->buffer; |
| 117 | line->tail = line->buffer; |
| 118 | } |
| 119 | |
| 120 | room = write_room(line); |
| 121 | len = (len > room) ? room : len; |
| 122 | |
| 123 | end = line->buffer + LINE_BUFSIZE - line->tail; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 124 | |
| 125 | if (len < end){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | memcpy(line->tail, buf, len); |
| 127 | line->tail += len; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 128 | } |
| 129 | else { |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 130 | /* The circular buffer is wrapping */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | memcpy(line->tail, buf, end); |
| 132 | buf += end; |
| 133 | memcpy(line->buffer, buf, len - end); |
| 134 | line->tail = line->buffer + len - end; |
| 135 | } |
| 136 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 137 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 140 | /* |
| 141 | * Flushes the ring buffer to the output channels. That is, write_chan is |
| 142 | * called, passing it line->head as buffer, and an appropriate count. |
| 143 | * |
| 144 | * On exit, returns 1 when the buffer is empty, |
| 145 | * 0 when the buffer is not empty on exit, |
| 146 | * and -errno when an error occurred. |
| 147 | * |
| 148 | * Must be called while holding line->lock!*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | static int flush_buffer(struct line *line) |
| 150 | { |
| 151 | int n, count; |
| 152 | |
| 153 | if ((line->buffer == NULL) || (line->head == line->tail)) |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 154 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | if (line->tail < line->head) { |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 157 | /* line->buffer + LINE_BUFSIZE is the end of the buffer! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | count = line->buffer + LINE_BUFSIZE - line->head; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | n = write_chan(&line->chan_list, line->head, count, |
| 161 | line->driver->write_irq); |
| 162 | if (n < 0) |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 163 | return n; |
| 164 | if (n == count) { |
| 165 | /* We have flushed from ->head to buffer end, now we |
| 166 | * must flush only from the beginning to ->tail.*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | line->head = line->buffer; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 168 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | line->head += n; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 170 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | count = line->tail - line->head; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 175 | n = write_chan(&line->chan_list, line->head, count, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | line->driver->write_irq); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 177 | |
| 178 | if(n < 0) |
| 179 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | line->head += n; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 182 | return line->head == line->tail; |
| 183 | } |
| 184 | |
| 185 | void line_flush_buffer(struct tty_struct *tty) |
| 186 | { |
| 187 | struct line *line = tty->driver_data; |
| 188 | unsigned long flags; |
| 189 | int err; |
| 190 | |
| 191 | /*XXX: copied from line_write, verify if it is correct!*/ |
| 192 | if(tty->stopped) |
| 193 | return; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 194 | |
| 195 | spin_lock_irqsave(&line->lock, flags); |
| 196 | err = flush_buffer(line); |
| 197 | /*if (err == 1) |
| 198 | err = 0;*/ |
| 199 | spin_unlock_irqrestore(&line->lock, flags); |
| 200 | //return err; |
| 201 | } |
| 202 | |
| 203 | /* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer |
| 204 | * and ->write. Hope it's not that bad.*/ |
| 205 | void line_flush_chars(struct tty_struct *tty) |
| 206 | { |
| 207 | line_flush_buffer(tty); |
| 208 | } |
| 209 | |
| 210 | void line_put_char(struct tty_struct *tty, unsigned char ch) |
| 211 | { |
| 212 | line_write(tty, &ch, sizeof(ch)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | int line_write(struct tty_struct *tty, const unsigned char *buf, int len) |
| 216 | { |
| 217 | struct line *line = tty->driver_data; |
| 218 | unsigned long flags; |
| 219 | int n, err, ret = 0; |
| 220 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 221 | if(tty->stopped) |
| 222 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 224 | spin_lock_irqsave(&line->lock, flags); |
| 225 | if (line->head != line->tail) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | ret = buffer_data(line, buf, len); |
| 227 | err = flush_buffer(line); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 228 | if (err <= 0 && (err != -EAGAIN || !ret)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | ret = err; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 230 | } else { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 231 | n = write_chan(&line->chan_list, buf, len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | line->driver->write_irq); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 233 | if (n < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | ret = n; |
| 235 | goto out_up; |
| 236 | } |
| 237 | |
| 238 | len -= n; |
| 239 | ret += n; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 240 | if (len > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | ret += buffer_data(line, buf + n, len); |
| 242 | } |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 243 | out_up: |
| 244 | spin_unlock_irqrestore(&line->lock, flags); |
| 245 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 248 | void line_set_termios(struct tty_struct *tty, struct ktermios * old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
| 250 | /* nothing */ |
| 251 | } |
| 252 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 253 | static const struct { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | int cmd; |
| 255 | char *level; |
| 256 | char *name; |
| 257 | } tty_ioctls[] = { |
| 258 | /* don't print these, they flood the log ... */ |
| 259 | { TCGETS, NULL, "TCGETS" }, |
| 260 | { TCSETS, NULL, "TCSETS" }, |
| 261 | { TCSETSW, NULL, "TCSETSW" }, |
| 262 | { TCFLSH, NULL, "TCFLSH" }, |
| 263 | { TCSBRK, NULL, "TCSBRK" }, |
| 264 | |
| 265 | /* general tty stuff */ |
| 266 | { TCSETSF, KERN_DEBUG, "TCSETSF" }, |
| 267 | { TCGETA, KERN_DEBUG, "TCGETA" }, |
| 268 | { TIOCMGET, KERN_DEBUG, "TIOCMGET" }, |
| 269 | { TCSBRKP, KERN_DEBUG, "TCSBRKP" }, |
| 270 | { TIOCMSET, KERN_DEBUG, "TIOCMSET" }, |
| 271 | |
| 272 | /* linux-specific ones */ |
| 273 | { TIOCLINUX, KERN_INFO, "TIOCLINUX" }, |
| 274 | { KDGKBMODE, KERN_INFO, "KDGKBMODE" }, |
| 275 | { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" }, |
| 276 | { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" }, |
| 277 | }; |
| 278 | |
| 279 | int line_ioctl(struct tty_struct *tty, struct file * file, |
| 280 | unsigned int cmd, unsigned long arg) |
| 281 | { |
| 282 | int ret; |
| 283 | int i; |
| 284 | |
| 285 | ret = 0; |
| 286 | switch(cmd) { |
| 287 | #ifdef TIOCGETP |
| 288 | case TIOCGETP: |
| 289 | case TIOCSETP: |
| 290 | case TIOCSETN: |
| 291 | #endif |
| 292 | #ifdef TIOCGETC |
| 293 | case TIOCGETC: |
| 294 | case TIOCSETC: |
| 295 | #endif |
| 296 | #ifdef TIOCGLTC |
| 297 | case TIOCGLTC: |
| 298 | case TIOCSLTC: |
| 299 | #endif |
| 300 | case TCGETS: |
| 301 | case TCSETSF: |
| 302 | case TCSETSW: |
| 303 | case TCSETS: |
| 304 | case TCGETA: |
| 305 | case TCSETAF: |
| 306 | case TCSETAW: |
| 307 | case TCSETA: |
| 308 | case TCXONC: |
| 309 | case TCFLSH: |
| 310 | case TIOCOUTQ: |
| 311 | case TIOCINQ: |
| 312 | case TIOCGLCKTRMIOS: |
| 313 | case TIOCSLCKTRMIOS: |
| 314 | case TIOCPKT: |
| 315 | case TIOCGSOFTCAR: |
| 316 | case TIOCSSOFTCAR: |
| 317 | return -ENOIOCTLCMD; |
| 318 | #if 0 |
| 319 | case TCwhatever: |
| 320 | /* do something */ |
| 321 | break; |
| 322 | #endif |
| 323 | default: |
| 324 | for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++) |
| 325 | if (cmd == tty_ioctls[i].cmd) |
| 326 | break; |
| 327 | if (i < ARRAY_SIZE(tty_ioctls)) { |
| 328 | if (NULL != tty_ioctls[i].level) |
| 329 | printk("%s%s: %s: ioctl %s called\n", |
| 330 | tty_ioctls[i].level, __FUNCTION__, |
| 331 | tty->name, tty_ioctls[i].name); |
| 332 | } else { |
| 333 | printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n", |
| 334 | __FUNCTION__, tty->name, cmd); |
| 335 | } |
| 336 | ret = -ENOIOCTLCMD; |
| 337 | break; |
| 338 | } |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 339 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 342 | void line_throttle(struct tty_struct *tty) |
| 343 | { |
| 344 | struct line *line = tty->driver_data; |
| 345 | |
| 346 | deactivate_chan(&line->chan_list, line->driver->read_irq); |
| 347 | line->throttled = 1; |
| 348 | } |
| 349 | |
| 350 | void line_unthrottle(struct tty_struct *tty) |
| 351 | { |
| 352 | struct line *line = tty->driver_data; |
| 353 | |
| 354 | line->throttled = 0; |
| 355 | chan_interrupt(&line->chan_list, &line->task, tty, |
| 356 | line->driver->read_irq); |
| 357 | |
| 358 | /* Maybe there is enough stuff pending that calling the interrupt |
| 359 | * throttles us again. In this case, line->throttled will be 1 |
| 360 | * again and we shouldn't turn the interrupt back on. |
| 361 | */ |
| 362 | if(!line->throttled) |
| 363 | reactivate_chan(&line->chan_list, line->driver->read_irq); |
| 364 | } |
| 365 | |
Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 366 | static irqreturn_t line_write_interrupt(int irq, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 368 | struct chan *chan = data; |
| 369 | struct line *line = chan->line; |
| 370 | struct tty_struct *tty = line->tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | int err; |
| 372 | |
Paolo 'Blaisorblade' Giarrusso | ec0ac8a | 2007-03-07 20:41:12 -0800 | [diff] [blame] | 373 | /* Interrupts are disabled here because we registered the interrupt with |
Thomas Gleixner | bd6aa65 | 2006-07-01 19:29:27 -0700 | [diff] [blame] | 374 | * IRQF_DISABLED (see line_setup_irq).*/ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 375 | |
Paolo 'Blaisorblade' Giarrusso | ec0ac8a | 2007-03-07 20:41:12 -0800 | [diff] [blame] | 376 | spin_lock(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | err = flush_buffer(line); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 378 | if (err == 0) { |
| 379 | return IRQ_NONE; |
| 380 | } else if(err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | line->head = line->buffer; |
| 382 | line->tail = line->buffer; |
| 383 | } |
Paolo 'Blaisorblade' Giarrusso | ec0ac8a | 2007-03-07 20:41:12 -0800 | [diff] [blame] | 384 | spin_unlock(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
| 386 | if(tty == NULL) |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 387 | return IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 389 | if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | (tty->ldisc.write_wakeup != NULL)) |
| 391 | (tty->ldisc.write_wakeup)(tty); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | /* BLOCKING mode |
| 394 | * In blocking mode, everything sleeps on tty->write_wait. |
| 395 | * Sleeping in the console driver would break non-blocking |
| 396 | * writes. |
| 397 | */ |
| 398 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 399 | if (waitqueue_active(&tty->write_wait)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | wake_up_interruptible(&tty->write_wait); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 401 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 404 | 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] | 405 | { |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 406 | const struct line_driver *driver = line->driver; |
Thomas Gleixner | bd6aa65 | 2006-07-01 19:29:27 -0700 | [diff] [blame] | 407 | int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 409 | if (input) |
| 410 | err = um_request_irq(driver->read_irq, fd, IRQ_READ, |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 411 | line_interrupt, flags, |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 412 | driver->read_irq_name, data); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 413 | if (err) |
| 414 | return err; |
| 415 | if (output) |
| 416 | err = um_request_irq(driver->write_irq, fd, IRQ_WRITE, |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 417 | line_write_interrupt, flags, |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 418 | driver->write_irq_name, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | line->have_irq = 1; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 420 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 423 | /* Normally, a driver like this can rely mostly on the tty layer |
| 424 | * locking, particularly when it comes to the driver structure. |
| 425 | * However, in this case, mconsole requests can come in "from the |
| 426 | * side", and race with opens and closes. |
| 427 | * |
Jeff Dike | c6256c6 | 2007-02-10 01:44:08 -0800 | [diff] [blame] | 428 | * mconsole config requests will want to be sure the device isn't in |
| 429 | * use, and get_config, open, and close will want a stable |
| 430 | * configuration. The checking and modification of the configuration |
| 431 | * is done under a spinlock. Checking whether the device is in use is |
| 432 | * line->tty->count > 1, also under the spinlock. |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 433 | * |
Jeff Dike | c6256c6 | 2007-02-10 01:44:08 -0800 | [diff] [blame] | 434 | * tty->count serves to decide whether the device should be enabled or |
| 435 | * disabled on the host. If it's equal to 1, then we are doing the |
| 436 | * first open or last close. Otherwise, open and close just return. |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 437 | */ |
| 438 | |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 439 | int line_open(struct line *lines, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 441 | struct line *line = &lines[tty->index]; |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 442 | int err = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 444 | spin_lock(&line->count_lock); |
| 445 | if(!line->valid) |
| 446 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 448 | err = 0; |
| 449 | if(tty->count > 1) |
| 450 | goto out_unlock; |
| 451 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 452 | spin_unlock(&line->count_lock); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 453 | |
| 454 | tty->driver_data = line; |
| 455 | line->tty = tty; |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 456 | |
Jeff Dike | d14ad81 | 2007-07-15 23:38:54 -0700 | [diff] [blame^] | 457 | err = enable_chan(line); |
| 458 | if (err) |
| 459 | return err; |
| 460 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 461 | INIT_DELAYED_WORK(&line->task, line_timer_cb); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 462 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 463 | if(!line->sigio){ |
| 464 | chan_enable_winch(&line->chan_list, tty); |
| 465 | line->sigio = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 468 | chan_window_size(&line->chan_list, &tty->winsize.ws_row, |
| 469 | &tty->winsize.ws_col); |
| 470 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 471 | return err; |
| 472 | |
| 473 | out_unlock: |
| 474 | spin_unlock(&line->count_lock); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 475 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 478 | static void unregister_winch(struct tty_struct *tty); |
| 479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | void line_close(struct tty_struct *tty, struct file * filp) |
| 481 | { |
| 482 | struct line *line = tty->driver_data; |
| 483 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 484 | /* If line_open fails (and tty->driver_data is never set), |
| 485 | * tty_open will call line_close. So just return in this case. |
| 486 | */ |
| 487 | if(line == NULL) |
| 488 | return; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 489 | |
| 490 | /* We ignore the error anyway! */ |
| 491 | flush_buffer(line); |
| 492 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 493 | spin_lock(&line->count_lock); |
| 494 | if(!line->valid) |
| 495 | goto out_unlock; |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 496 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 497 | if(tty->count > 1) |
| 498 | goto out_unlock; |
| 499 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 500 | spin_unlock(&line->count_lock); |
| 501 | |
| 502 | line->tty = NULL; |
| 503 | tty->driver_data = NULL; |
| 504 | |
| 505 | if(line->sigio){ |
| 506 | unregister_winch(tty); |
| 507 | line->sigio = 0; |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 510 | return; |
| 511 | |
| 512 | out_unlock: |
| 513 | spin_unlock(&line->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | void close_lines(struct line *lines, int nlines) |
| 517 | { |
| 518 | int i; |
| 519 | |
| 520 | for(i = 0; i < nlines; i++) |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 521 | close_chan(&lines[i].chan_list, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 524 | static int setup_one_line(struct line *lines, int n, char *init, int init_prio, |
| 525 | char **error_out) |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 526 | { |
| 527 | struct line *line = &lines[n]; |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 528 | int err = -EINVAL; |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 529 | |
| 530 | spin_lock(&line->count_lock); |
| 531 | |
| 532 | if(line->tty != NULL){ |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 533 | *error_out = "Device is already open"; |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 534 | goto out; |
| 535 | } |
| 536 | |
| 537 | if (line->init_pri <= init_prio){ |
| 538 | line->init_pri = init_prio; |
| 539 | if (!strcmp(init, "none")) |
| 540 | line->valid = 0; |
| 541 | else { |
| 542 | line->init_str = init; |
| 543 | line->valid = 1; |
| 544 | } |
| 545 | } |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 546 | err = 0; |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 547 | out: |
| 548 | spin_unlock(&line->count_lock); |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 549 | return err; |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 550 | } |
| 551 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 552 | /* Common setup code for both startup command line and mconsole initialization. |
Matt LaPlante | 4b3f686 | 2006-10-03 22:21:02 +0200 | [diff] [blame] | 553 | * @lines contains the array (of size @num) to modify; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 554 | * @init is the setup string; |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 555 | * @error_out is an error string in the case of failure; |
Jeff Dike | d571cd1 | 2006-01-06 00:18:53 -0800 | [diff] [blame] | 556 | */ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 557 | |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 558 | int line_setup(struct line *lines, unsigned int num, char *init, |
| 559 | char **error_out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | { |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 561 | int i, n, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | char *end; |
| 563 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 564 | if(*init == '=') { |
| 565 | /* We said con=/ssl= instead of con#=, so we are configuring all |
| 566 | * consoles at once.*/ |
| 567 | n = -1; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 568 | } |
| 569 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | n = simple_strtoul(init, &end, 0); |
| 571 | if(*end != '='){ |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 572 | *error_out = "Couldn't parse device number"; |
| 573 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
| 575 | init = end; |
| 576 | } |
| 577 | init++; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 578 | |
| 579 | if (n >= (signed int) num) { |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 580 | *error_out = "Device number out of range"; |
| 581 | return -EINVAL; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 582 | } |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 583 | else if (n >= 0){ |
| 584 | err = setup_one_line(lines, n, init, INIT_ONE, error_out); |
| 585 | if(err) |
| 586 | return err; |
| 587 | } |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 588 | else { |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 589 | for(i = 0; i < num; i++){ |
| 590 | err = setup_one_line(lines, i, init, INIT_ALL, |
| 591 | error_out); |
| 592 | if(err) |
| 593 | return err; |
| 594 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 596 | return n == -1 ? num : n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 599 | int line_config(struct line *lines, unsigned int num, char *str, |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 600 | const struct chan_opts *opts, char **error_out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | { |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 602 | struct line *line; |
Jeff Dike | 970d6e3 | 2006-01-06 00:18:48 -0800 | [diff] [blame] | 603 | char *new; |
Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 604 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
Jeff Dike | d571cd1 | 2006-01-06 00:18:53 -0800 | [diff] [blame] | 606 | if(*str == '='){ |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 607 | *error_out = "Can't configure all devices from mconsole"; |
| 608 | return -EINVAL; |
Jeff Dike | d571cd1 | 2006-01-06 00:18:53 -0800 | [diff] [blame] | 609 | } |
| 610 | |
Jeff Dike | 970d6e3 | 2006-01-06 00:18:48 -0800 | [diff] [blame] | 611 | new = kstrdup(str, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | if(new == NULL){ |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 613 | *error_out = "Failed to allocate memory"; |
| 614 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 616 | n = line_setup(lines, num, new, error_out); |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 617 | if(n < 0) |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 618 | return n; |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 619 | |
| 620 | line = &lines[n]; |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 621 | return parse_chan_pair(line->init_str, line, n, opts, error_out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 624 | 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] | 625 | int size, char **error_out) |
| 626 | { |
| 627 | struct line *line; |
| 628 | char *end; |
| 629 | int dev, n = 0; |
| 630 | |
| 631 | dev = simple_strtoul(name, &end, 0); |
| 632 | if((*end != '\0') || (end == name)){ |
| 633 | *error_out = "line_get_config failed to parse device number"; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 634 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | if((dev < 0) || (dev >= num)){ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 638 | *error_out = "device number out of range"; |
| 639 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | line = &lines[dev]; |
| 643 | |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 644 | spin_lock(&line->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | if(!line->valid) |
| 646 | CONFIG_CHUNK(str, size, n, "none", 1); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 647 | else if(line->tty == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | CONFIG_CHUNK(str, size, n, line->init_str, 1); |
| 649 | else n = chan_config_string(&line->chan_list, str, size, error_out); |
Jeff Dike | d79a580 | 2007-02-10 01:43:52 -0800 | [diff] [blame] | 650 | spin_unlock(&line->count_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 652 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 655 | int line_id(char **str, int *start_out, int *end_out) |
| 656 | { |
| 657 | char *end; |
| 658 | int n; |
| 659 | |
| 660 | n = simple_strtoul(*str, &end, 0); |
| 661 | if((*end != '\0') || (end == *str)) |
| 662 | return -1; |
| 663 | |
| 664 | *str = end; |
| 665 | *start_out = n; |
| 666 | *end_out = n; |
| 667 | return n; |
| 668 | } |
| 669 | |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 670 | 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] | 671 | { |
Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 672 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | char config[sizeof("conxxxx=none\0")]; |
| 674 | |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 675 | sprintf(config, "%d=none", n); |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 676 | err = line_setup(lines, num, config, error_out); |
Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 677 | if(err >= 0) |
| 678 | err = 0; |
| 679 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Jeff Dike | d5c9ffc | 2007-02-10 01:44:08 -0800 | [diff] [blame] | 682 | struct tty_driver *register_lines(struct line_driver *line_driver, |
| 683 | const struct tty_operations *ops, |
| 684 | struct line *lines, int nlines) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | { |
| 686 | int i; |
| 687 | struct tty_driver *driver = alloc_tty_driver(nlines); |
| 688 | |
| 689 | if (!driver) |
| 690 | return NULL; |
| 691 | |
| 692 | driver->driver_name = line_driver->name; |
| 693 | driver->name = line_driver->device_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | driver->major = line_driver->major; |
| 695 | driver->minor_start = line_driver->minor_start; |
| 696 | driver->type = line_driver->type; |
| 697 | driver->subtype = line_driver->subtype; |
| 698 | driver->flags = TTY_DRIVER_REAL_RAW; |
| 699 | driver->init_termios = tty_std_termios; |
| 700 | tty_set_operations(driver, ops); |
| 701 | |
| 702 | if (tty_register_driver(driver)) { |
| 703 | printk("%s: can't register %s driver\n", |
| 704 | __FUNCTION__,line_driver->name); |
| 705 | put_tty_driver(driver); |
| 706 | return NULL; |
| 707 | } |
| 708 | |
| 709 | for(i = 0; i < nlines; i++){ |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 710 | if(!lines[i].valid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | tty_unregister_device(driver, i); |
| 712 | } |
| 713 | |
| 714 | mconsole_register_dev(&line_driver->mc); |
| 715 | return driver; |
| 716 | } |
| 717 | |
Jeff Dike | 9010772 | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 718 | static DEFINE_SPINLOCK(winch_handler_lock); |
| 719 | static LIST_HEAD(winch_handlers); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 720 | |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 721 | void lines_init(struct line *lines, int nlines, struct chan_opts *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | { |
| 723 | struct line *line; |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 724 | char *error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | int i; |
| 726 | |
| 727 | for(i = 0; i < nlines; i++){ |
| 728 | line = &lines[i]; |
| 729 | INIT_LIST_HEAD(&line->chan_list); |
Jeff Dike | 9010772 | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 730 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 731 | if(line->init_str == NULL) |
| 732 | continue; |
| 733 | |
| 734 | line->init_str = kstrdup(line->init_str, GFP_KERNEL); |
| 735 | if(line->init_str == NULL) |
| 736 | printk("lines_init - kstrdup returned NULL\n"); |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 737 | |
Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 738 | if(parse_chan_pair(line->init_str, line, i, opts, &error)){ |
| 739 | printk("parse_chan_pair failed for device %d : %s\n", |
| 740 | i, error); |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 741 | line->valid = 0; |
| 742 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
| 746 | struct winch { |
| 747 | struct list_head list; |
| 748 | int fd; |
| 749 | int tty_fd; |
| 750 | int pid; |
| 751 | struct tty_struct *tty; |
| 752 | }; |
| 753 | |
Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 754 | static irqreturn_t winch_interrupt(int irq, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | { |
| 756 | struct winch *winch = data; |
| 757 | struct tty_struct *tty; |
| 758 | struct line *line; |
| 759 | int err; |
| 760 | char c; |
| 761 | |
| 762 | if(winch->fd != -1){ |
| 763 | err = generic_read(winch->fd, &c, NULL); |
| 764 | if(err < 0){ |
| 765 | if(err != -EAGAIN){ |
| 766 | printk("winch_interrupt : read failed, " |
| 767 | "errno = %d\n", -err); |
| 768 | printk("fd %d is losing SIGWINCH support\n", |
| 769 | winch->tty_fd); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 770 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | } |
| 772 | goto out; |
| 773 | } |
| 774 | } |
| 775 | tty = winch->tty; |
| 776 | if (tty != NULL) { |
| 777 | line = tty->driver_data; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 778 | chan_window_size(&line->chan_list, &tty->winsize.ws_row, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | &tty->winsize.ws_col); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 780 | kill_pgrp(tty->pgrp, SIGWINCH, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | } |
| 782 | out: |
| 783 | if(winch->fd != -1) |
| 784 | reactivate_fd(winch->fd, WINCH_IRQ); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 785 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty) |
| 789 | { |
| 790 | struct winch *winch; |
| 791 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | winch = kmalloc(sizeof(*winch), GFP_KERNEL); |
| 793 | if (winch == NULL) { |
| 794 | printk("register_winch_irq - kmalloc failed\n"); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 795 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | } |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list), |
| 799 | .fd = fd, |
| 800 | .tty_fd = tty_fd, |
| 801 | .pid = pid, |
| 802 | .tty = tty }); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 803 | |
| 804 | spin_lock(&winch_handler_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | list_add(&winch->list, &winch_handlers); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 806 | spin_unlock(&winch_handler_lock); |
| 807 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 808 | if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt, |
Thomas Gleixner | bd6aa65 | 2006-07-01 19:29:27 -0700 | [diff] [blame] | 809 | IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | "winch", winch) < 0) |
| 811 | printk("register_winch_irq - failed to register IRQ\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 814 | static void free_winch(struct winch *winch) |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 815 | { |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 816 | list_del(&winch->list); |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 817 | |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 818 | if(winch->pid != -1) |
| 819 | os_kill_process(winch->pid, 1); |
| 820 | if(winch->fd != -1) |
| 821 | os_close_file(winch->fd); |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 822 | |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 823 | free_irq(WINCH_IRQ, winch); |
| 824 | kfree(winch); |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 827 | static void unregister_winch(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | { |
| 829 | struct list_head *ele; |
| 830 | struct winch *winch; |
| 831 | |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 832 | spin_lock(&winch_handler_lock); |
| 833 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | list_for_each(ele, &winch_handlers){ |
| 835 | winch = list_entry(ele, struct winch, list); |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 836 | if(winch->tty == tty){ |
| 837 | free_winch(winch); |
| 838 | break; |
| 839 | } |
| 840 | } |
| 841 | spin_unlock(&winch_handler_lock); |
| 842 | } |
| 843 | |
| 844 | static void winch_cleanup(void) |
| 845 | { |
| 846 | struct list_head *ele, *next; |
| 847 | struct winch *winch; |
| 848 | |
| 849 | spin_lock(&winch_handler_lock); |
| 850 | |
| 851 | list_for_each_safe(ele, next, &winch_handlers){ |
| 852 | winch = list_entry(ele, struct winch, list); |
| 853 | free_winch(winch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | } |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 855 | |
| 856 | spin_unlock(&winch_handler_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } |
| 858 | __uml_exitcall(winch_cleanup); |
| 859 | |
| 860 | char *add_xterm_umid(char *base) |
| 861 | { |
| 862 | char *umid, *title; |
| 863 | int len; |
| 864 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 865 | umid = get_umid(); |
| 866 | if(*umid == '\0') |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 867 | return base; |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | len = strlen(base) + strlen(" ()") + strlen(umid) + 1; |
| 870 | title = kmalloc(len, GFP_KERNEL); |
| 871 | if(title == NULL){ |
| 872 | printk("Failed to allocate buffer for xterm title\n"); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 873 | return base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | snprintf(title, len, "%s (%s)", base, umid); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 877 | return title; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | } |