Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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" |
| 11 | #include "linux/devfs_fs_kernel.h" |
| 12 | #include "asm/uaccess.h" |
| 13 | #include "chan_kern.h" |
| 14 | #include "irq_user.h" |
| 15 | #include "line.h" |
| 16 | #include "kern.h" |
| 17 | #include "user_util.h" |
| 18 | #include "kern_util.h" |
| 19 | #include "os.h" |
| 20 | #include "irq_kern.h" |
| 21 | |
| 22 | #define LINE_BUFSIZE 4096 |
| 23 | |
| 24 | static irqreturn_t line_interrupt(int irq, void *data, struct pt_regs *unused) |
| 25 | { |
| 26 | struct tty_struct *tty = data; |
| 27 | struct line *line = tty->driver_data; |
| 28 | |
| 29 | if (line) |
| 30 | chan_interrupt(&line->chan_list, &line->task, tty, irq); |
| 31 | return IRQ_HANDLED; |
| 32 | } |
| 33 | |
| 34 | static void line_timer_cb(void *arg) |
| 35 | { |
| 36 | struct tty_struct *tty = arg; |
| 37 | struct line *line = tty->driver_data; |
| 38 | |
| 39 | line_interrupt(line->driver->read_irq, arg, NULL); |
| 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; |
| 193 | //return 0; |
| 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 | |
| 248 | void line_set_termios(struct tty_struct *tty, struct termios * old) |
| 249 | { |
| 250 | /* nothing */ |
| 251 | } |
| 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | static struct { |
| 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 | |
| 342 | static irqreturn_t line_write_interrupt(int irq, void *data, |
| 343 | struct pt_regs *unused) |
| 344 | { |
| 345 | struct tty_struct *tty = data; |
| 346 | struct line *line = tty->driver_data; |
| 347 | int err; |
| 348 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 349 | /* Interrupts are enabled here because we registered the interrupt with |
| 350 | * SA_INTERRUPT (see line_setup_irq).*/ |
| 351 | |
| 352 | spin_lock_irq(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | err = flush_buffer(line); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 354 | if (err == 0) { |
| 355 | return IRQ_NONE; |
| 356 | } else if(err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | line->head = line->buffer; |
| 358 | line->tail = line->buffer; |
| 359 | } |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 360 | spin_unlock_irq(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
| 362 | if(tty == NULL) |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 363 | return IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 365 | if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | (tty->ldisc.write_wakeup != NULL)) |
| 367 | (tty->ldisc.write_wakeup)(tty); |
| 368 | |
| 369 | /* BLOCKING mode |
| 370 | * In blocking mode, everything sleeps on tty->write_wait. |
| 371 | * Sleeping in the console driver would break non-blocking |
| 372 | * writes. |
| 373 | */ |
| 374 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 375 | if (waitqueue_active(&tty->write_wait)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | wake_up_interruptible(&tty->write_wait); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 377 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | int line_setup_irq(int fd, int input, int output, struct tty_struct *tty) |
| 381 | { |
| 382 | struct line *line = tty->driver_data; |
| 383 | struct line_driver *driver = line->driver; |
| 384 | int err = 0, flags = SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM; |
| 385 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 386 | if (input) |
| 387 | err = um_request_irq(driver->read_irq, fd, IRQ_READ, |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 388 | line_interrupt, flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | driver->read_irq_name, tty); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 390 | if (err) |
| 391 | return err; |
| 392 | if (output) |
| 393 | err = um_request_irq(driver->write_irq, fd, IRQ_WRITE, |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 394 | line_write_interrupt, flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | driver->write_irq_name, tty); |
| 396 | line->have_irq = 1; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 397 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void line_disable(struct tty_struct *tty, int current_irq) |
| 401 | { |
| 402 | struct line *line = tty->driver_data; |
| 403 | |
| 404 | if(!line->have_irq) |
| 405 | return; |
| 406 | |
| 407 | if(line->driver->read_irq == current_irq) |
| 408 | free_irq_later(line->driver->read_irq, tty); |
| 409 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | free_irq(line->driver->read_irq, tty); |
| 411 | } |
| 412 | |
| 413 | if(line->driver->write_irq == current_irq) |
| 414 | free_irq_later(line->driver->write_irq, tty); |
| 415 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | free_irq(line->driver->write_irq, tty); |
| 417 | } |
| 418 | |
| 419 | line->have_irq = 0; |
| 420 | } |
| 421 | |
| 422 | int line_open(struct line *lines, struct tty_struct *tty, |
| 423 | struct chan_opts *opts) |
| 424 | { |
| 425 | struct line *line; |
| 426 | int err = 0; |
| 427 | |
| 428 | line = &lines[tty->index]; |
| 429 | tty->driver_data = line; |
| 430 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 431 | /* The IRQ which takes this lock is not yet enabled and won't be run |
| 432 | * before the end, so we don't need to use spin_lock_irq.*/ |
| 433 | spin_lock(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | if (tty->count == 1) { |
| 435 | if (!line->valid) { |
| 436 | err = -ENODEV; |
| 437 | goto out; |
| 438 | } |
| 439 | if (list_empty(&line->chan_list)) { |
| 440 | err = parse_chan_pair(line->init_str, &line->chan_list, |
Jeff Dike | 88890b8 | 2006-01-06 00:18:52 -0800 | [diff] [blame] | 441 | tty->index, opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | if(err) goto out; |
| 443 | err = open_chan(&line->chan_list); |
| 444 | if(err) goto out; |
| 445 | } |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 446 | /* Here the interrupt is registered.*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | enable_chan(&line->chan_list, tty); |
| 448 | INIT_WORK(&line->task, line_timer_cb, tty); |
| 449 | } |
| 450 | |
| 451 | if(!line->sigio){ |
| 452 | chan_enable_winch(&line->chan_list, tty); |
| 453 | line->sigio = 1; |
| 454 | } |
| 455 | chan_window_size(&line->chan_list, &tty->winsize.ws_row, |
| 456 | &tty->winsize.ws_col); |
| 457 | line->count++; |
| 458 | |
| 459 | out: |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 460 | spin_unlock(&line->lock); |
| 461 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 464 | static void unregister_winch(struct tty_struct *tty); |
| 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | void line_close(struct tty_struct *tty, struct file * filp) |
| 467 | { |
| 468 | struct line *line = tty->driver_data; |
| 469 | |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 470 | /* XXX: I assume this should be called in process context, not with |
| 471 | * interrupts disabled! |
| 472 | */ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 473 | spin_lock_irq(&line->lock); |
| 474 | |
| 475 | /* We ignore the error anyway! */ |
| 476 | flush_buffer(line); |
| 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | line->count--; |
| 479 | if (tty->count == 1) { |
| 480 | line_disable(tty, -1); |
| 481 | tty->driver_data = NULL; |
| 482 | } |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 483 | |
| 484 | if((line->count == 0) && line->sigio){ |
| 485 | unregister_winch(tty); |
| 486 | line->sigio = 0; |
| 487 | } |
| 488 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 489 | spin_unlock_irq(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | void close_lines(struct line *lines, int nlines) |
| 493 | { |
| 494 | int i; |
| 495 | |
| 496 | for(i = 0; i < nlines; i++) |
| 497 | close_chan(&lines[i].chan_list); |
| 498 | } |
| 499 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 500 | /* Common setup code for both startup command line and mconsole initialization. |
| 501 | * @lines contains the the array (of size @num) to modify; |
| 502 | * @init is the setup string; |
Jeff Dike | d571cd1 | 2006-01-06 00:18:53 -0800 | [diff] [blame] | 503 | */ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 504 | |
Jeff Dike | d571cd1 | 2006-01-06 00:18:53 -0800 | [diff] [blame] | 505 | int line_setup(struct line *lines, unsigned int num, char *init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | { |
| 507 | int i, n; |
| 508 | char *end; |
| 509 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 510 | if(*init == '=') { |
| 511 | /* We said con=/ssl= instead of con#=, so we are configuring all |
| 512 | * consoles at once.*/ |
| 513 | n = -1; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 514 | } |
| 515 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | n = simple_strtoul(init, &end, 0); |
| 517 | if(*end != '='){ |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 518 | printk(KERN_ERR "line_setup failed to parse \"%s\"\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | init); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 520 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | init = end; |
| 523 | } |
| 524 | init++; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 525 | |
| 526 | if (n >= (signed int) num) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | printk("line_setup - %d out of range ((0 ... %d) allowed)\n", |
| 528 | n, num - 1); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 529 | return 0; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 530 | } |
| 531 | else if (n >= 0){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | if (lines[n].count > 0) { |
| 533 | printk("line_setup - device %d is open\n", n); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 534 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | } |
| 536 | if (lines[n].init_pri <= INIT_ONE){ |
| 537 | lines[n].init_pri = INIT_ONE; |
| 538 | if (!strcmp(init, "none")) |
| 539 | lines[n].valid = 0; |
| 540 | else { |
| 541 | lines[n].init_str = init; |
| 542 | lines[n].valid = 1; |
| 543 | } |
| 544 | } |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 545 | } |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 546 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | for(i = 0; i < num; i++){ |
| 548 | if(lines[i].init_pri <= INIT_ALL){ |
| 549 | lines[i].init_pri = INIT_ALL; |
| 550 | if(!strcmp(init, "none")) lines[i].valid = 0; |
| 551 | else { |
| 552 | lines[i].init_str = init; |
| 553 | lines[i].valid = 1; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | } |
Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame^] | 558 | return n == -1 ? num : n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 561 | int line_config(struct line *lines, unsigned int num, char *str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | { |
Jeff Dike | 970d6e3 | 2006-01-06 00:18:48 -0800 | [diff] [blame] | 563 | char *new; |
Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame^] | 564 | int n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
Jeff Dike | d571cd1 | 2006-01-06 00:18:53 -0800 | [diff] [blame] | 566 | if(*str == '='){ |
| 567 | printk("line_config - can't configure all devices from " |
| 568 | "mconsole\n"); |
| 569 | return 1; |
| 570 | } |
| 571 | |
Jeff Dike | 970d6e3 | 2006-01-06 00:18:48 -0800 | [diff] [blame] | 572 | new = kstrdup(str, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | if(new == NULL){ |
Jeff Dike | 970d6e3 | 2006-01-06 00:18:48 -0800 | [diff] [blame] | 574 | printk("line_config - kstrdup failed\n"); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 575 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } |
Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame^] | 577 | n = line_setup(lines, num, new); |
| 578 | return n < 0 ? n : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 581 | 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] | 582 | int size, char **error_out) |
| 583 | { |
| 584 | struct line *line; |
| 585 | char *end; |
| 586 | int dev, n = 0; |
| 587 | |
| 588 | dev = simple_strtoul(name, &end, 0); |
| 589 | if((*end != '\0') || (end == name)){ |
| 590 | *error_out = "line_get_config failed to parse device number"; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 591 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | if((dev < 0) || (dev >= num)){ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 595 | *error_out = "device number out of range"; |
| 596 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | line = &lines[dev]; |
| 600 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 601 | spin_lock(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | if(!line->valid) |
| 603 | CONFIG_CHUNK(str, size, n, "none", 1); |
| 604 | else if(line->count == 0) |
| 605 | CONFIG_CHUNK(str, size, n, line->init_str, 1); |
| 606 | else n = chan_config_string(&line->chan_list, str, size, error_out); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 607 | spin_unlock(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 609 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 612 | int line_id(char **str, int *start_out, int *end_out) |
| 613 | { |
| 614 | char *end; |
| 615 | int n; |
| 616 | |
| 617 | n = simple_strtoul(*str, &end, 0); |
| 618 | if((*end != '\0') || (end == *str)) |
| 619 | return -1; |
| 620 | |
| 621 | *str = end; |
| 622 | *start_out = n; |
| 623 | *end_out = n; |
| 624 | return n; |
| 625 | } |
| 626 | |
| 627 | int line_remove(struct line *lines, unsigned int num, int n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | { |
Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame^] | 629 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | char config[sizeof("conxxxx=none\0")]; |
| 631 | |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 632 | sprintf(config, "%d=none", n); |
Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame^] | 633 | err = line_setup(lines, num, config); |
| 634 | if(err >= 0) |
| 635 | err = 0; |
| 636 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | struct tty_driver *line_register_devfs(struct lines *set, |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 640 | struct line_driver *line_driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | struct tty_operations *ops, struct line *lines, |
| 642 | int nlines) |
| 643 | { |
| 644 | int i; |
| 645 | struct tty_driver *driver = alloc_tty_driver(nlines); |
| 646 | |
| 647 | if (!driver) |
| 648 | return NULL; |
| 649 | |
| 650 | driver->driver_name = line_driver->name; |
| 651 | driver->name = line_driver->device_name; |
| 652 | driver->devfs_name = line_driver->devfs_name; |
| 653 | driver->major = line_driver->major; |
| 654 | driver->minor_start = line_driver->minor_start; |
| 655 | driver->type = line_driver->type; |
| 656 | driver->subtype = line_driver->subtype; |
| 657 | driver->flags = TTY_DRIVER_REAL_RAW; |
| 658 | driver->init_termios = tty_std_termios; |
| 659 | tty_set_operations(driver, ops); |
| 660 | |
| 661 | if (tty_register_driver(driver)) { |
| 662 | printk("%s: can't register %s driver\n", |
| 663 | __FUNCTION__,line_driver->name); |
| 664 | put_tty_driver(driver); |
| 665 | return NULL; |
| 666 | } |
| 667 | |
| 668 | for(i = 0; i < nlines; i++){ |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 669 | if(!lines[i].valid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | tty_unregister_device(driver, i); |
| 671 | } |
| 672 | |
| 673 | mconsole_register_dev(&line_driver->mc); |
| 674 | return driver; |
| 675 | } |
| 676 | |
Jeff Dike | 9010772 | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 677 | static DEFINE_SPINLOCK(winch_handler_lock); |
| 678 | static LIST_HEAD(winch_handlers); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | void lines_init(struct line *lines, int nlines) |
| 681 | { |
| 682 | struct line *line; |
| 683 | int i; |
| 684 | |
| 685 | for(i = 0; i < nlines; i++){ |
| 686 | line = &lines[i]; |
| 687 | INIT_LIST_HEAD(&line->chan_list); |
Jeff Dike | 9010772 | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 688 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 689 | if(line->init_str == NULL) |
| 690 | continue; |
| 691 | |
| 692 | line->init_str = kstrdup(line->init_str, GFP_KERNEL); |
| 693 | if(line->init_str == NULL) |
| 694 | printk("lines_init - kstrdup returned NULL\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
| 698 | struct winch { |
| 699 | struct list_head list; |
| 700 | int fd; |
| 701 | int tty_fd; |
| 702 | int pid; |
| 703 | struct tty_struct *tty; |
| 704 | }; |
| 705 | |
| 706 | irqreturn_t winch_interrupt(int irq, void *data, struct pt_regs *unused) |
| 707 | { |
| 708 | struct winch *winch = data; |
| 709 | struct tty_struct *tty; |
| 710 | struct line *line; |
| 711 | int err; |
| 712 | char c; |
| 713 | |
| 714 | if(winch->fd != -1){ |
| 715 | err = generic_read(winch->fd, &c, NULL); |
| 716 | if(err < 0){ |
| 717 | if(err != -EAGAIN){ |
| 718 | printk("winch_interrupt : read failed, " |
| 719 | "errno = %d\n", -err); |
| 720 | printk("fd %d is losing SIGWINCH support\n", |
| 721 | winch->tty_fd); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 722 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | } |
| 724 | goto out; |
| 725 | } |
| 726 | } |
| 727 | tty = winch->tty; |
| 728 | if (tty != NULL) { |
| 729 | line = tty->driver_data; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 730 | chan_window_size(&line->chan_list, &tty->winsize.ws_row, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | &tty->winsize.ws_col); |
| 732 | kill_pg(tty->pgrp, SIGWINCH, 1); |
| 733 | } |
| 734 | out: |
| 735 | if(winch->fd != -1) |
| 736 | reactivate_fd(winch->fd, WINCH_IRQ); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 737 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty) |
| 741 | { |
| 742 | struct winch *winch; |
| 743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | winch = kmalloc(sizeof(*winch), GFP_KERNEL); |
| 745 | if (winch == NULL) { |
| 746 | printk("register_winch_irq - kmalloc failed\n"); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 747 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | } |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list), |
| 751 | .fd = fd, |
| 752 | .tty_fd = tty_fd, |
| 753 | .pid = pid, |
| 754 | .tty = tty }); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 755 | |
| 756 | spin_lock(&winch_handler_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | list_add(&winch->list, &winch_handlers); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 758 | spin_unlock(&winch_handler_lock); |
| 759 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 760 | if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt, |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 761 | SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | "winch", winch) < 0) |
| 763 | printk("register_winch_irq - failed to register IRQ\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 766 | static void unregister_winch(struct tty_struct *tty) |
| 767 | { |
| 768 | struct list_head *ele; |
| 769 | struct winch *winch, *found = NULL; |
| 770 | |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 771 | spin_lock(&winch_handler_lock); |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 772 | list_for_each(ele, &winch_handlers){ |
| 773 | winch = list_entry(ele, struct winch, list); |
| 774 | if(winch->tty == tty){ |
| 775 | found = winch; |
| 776 | break; |
| 777 | } |
| 778 | } |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 779 | if(found == NULL) |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 780 | goto err; |
| 781 | |
| 782 | list_del(&winch->list); |
| 783 | spin_unlock(&winch_handler_lock); |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 784 | |
| 785 | if(winch->pid != -1) |
| 786 | os_kill_process(winch->pid, 1); |
| 787 | |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 788 | free_irq(WINCH_IRQ, winch); |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 789 | kfree(winch); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 790 | |
| 791 | return; |
| 792 | err: |
| 793 | spin_unlock(&winch_handler_lock); |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 796 | /* XXX: No lock as it's an exitcall... is this valid? Depending on cleanup |
| 797 | * order... are we sure that nothing else is done on the list? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | static void winch_cleanup(void) |
| 799 | { |
| 800 | struct list_head *ele; |
| 801 | struct winch *winch; |
| 802 | |
| 803 | list_for_each(ele, &winch_handlers){ |
| 804 | winch = list_entry(ele, struct winch, list); |
| 805 | if(winch->fd != -1){ |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 806 | /* Why is this different from the above free_irq(), |
| 807 | * which deactivates SIGIO? This searches the FD |
| 808 | * somewhere else and removes it from the list... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | deactivate_fd(winch->fd, WINCH_IRQ); |
| 810 | os_close_file(winch->fd); |
| 811 | } |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 812 | if(winch->pid != -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | os_kill_process(winch->pid, 1); |
| 814 | } |
| 815 | } |
| 816 | __uml_exitcall(winch_cleanup); |
| 817 | |
| 818 | char *add_xterm_umid(char *base) |
| 819 | { |
| 820 | char *umid, *title; |
| 821 | int len; |
| 822 | |
| 823 | umid = get_umid(1); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 824 | if(umid == NULL) |
| 825 | return base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | |
| 827 | len = strlen(base) + strlen(" ()") + strlen(umid) + 1; |
| 828 | title = kmalloc(len, GFP_KERNEL); |
| 829 | if(title == NULL){ |
| 830 | printk("Failed to allocate buffer for xterm title\n"); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 831 | return base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | snprintf(title, len, "%s (%s)", base, umid); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 835 | return title; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | } |