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