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" |
| 16 | #include "user_util.h" |
| 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; |
| 194 | //return 0; |
| 195 | |
| 196 | spin_lock_irqsave(&line->lock, flags); |
| 197 | err = flush_buffer(line); |
| 198 | /*if (err == 1) |
| 199 | err = 0;*/ |
| 200 | spin_unlock_irqrestore(&line->lock, flags); |
| 201 | //return err; |
| 202 | } |
| 203 | |
| 204 | /* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer |
| 205 | * and ->write. Hope it's not that bad.*/ |
| 206 | void line_flush_chars(struct tty_struct *tty) |
| 207 | { |
| 208 | line_flush_buffer(tty); |
| 209 | } |
| 210 | |
| 211 | void line_put_char(struct tty_struct *tty, unsigned char ch) |
| 212 | { |
| 213 | line_write(tty, &ch, sizeof(ch)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | int line_write(struct tty_struct *tty, const unsigned char *buf, int len) |
| 217 | { |
| 218 | struct line *line = tty->driver_data; |
| 219 | unsigned long flags; |
| 220 | int n, err, ret = 0; |
| 221 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 222 | if(tty->stopped) |
| 223 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 225 | spin_lock_irqsave(&line->lock, flags); |
| 226 | if (line->head != line->tail) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | ret = buffer_data(line, buf, len); |
| 228 | err = flush_buffer(line); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 229 | if (err <= 0 && (err != -EAGAIN || !ret)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | ret = err; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 231 | } else { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 232 | n = write_chan(&line->chan_list, buf, len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | line->driver->write_irq); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 234 | if (n < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | ret = n; |
| 236 | goto out_up; |
| 237 | } |
| 238 | |
| 239 | len -= n; |
| 240 | ret += n; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 241 | if (len > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | ret += buffer_data(line, buf + n, len); |
| 243 | } |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 244 | out_up: |
| 245 | spin_unlock_irqrestore(&line->lock, flags); |
| 246 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame^] | 249 | void line_set_termios(struct tty_struct *tty, struct ktermios * old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
| 251 | /* nothing */ |
| 252 | } |
| 253 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 254 | static const struct { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | int cmd; |
| 256 | char *level; |
| 257 | char *name; |
| 258 | } tty_ioctls[] = { |
| 259 | /* don't print these, they flood the log ... */ |
| 260 | { TCGETS, NULL, "TCGETS" }, |
| 261 | { TCSETS, NULL, "TCSETS" }, |
| 262 | { TCSETSW, NULL, "TCSETSW" }, |
| 263 | { TCFLSH, NULL, "TCFLSH" }, |
| 264 | { TCSBRK, NULL, "TCSBRK" }, |
| 265 | |
| 266 | /* general tty stuff */ |
| 267 | { TCSETSF, KERN_DEBUG, "TCSETSF" }, |
| 268 | { TCGETA, KERN_DEBUG, "TCGETA" }, |
| 269 | { TIOCMGET, KERN_DEBUG, "TIOCMGET" }, |
| 270 | { TCSBRKP, KERN_DEBUG, "TCSBRKP" }, |
| 271 | { TIOCMSET, KERN_DEBUG, "TIOCMSET" }, |
| 272 | |
| 273 | /* linux-specific ones */ |
| 274 | { TIOCLINUX, KERN_INFO, "TIOCLINUX" }, |
| 275 | { KDGKBMODE, KERN_INFO, "KDGKBMODE" }, |
| 276 | { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" }, |
| 277 | { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" }, |
| 278 | }; |
| 279 | |
| 280 | int line_ioctl(struct tty_struct *tty, struct file * file, |
| 281 | unsigned int cmd, unsigned long arg) |
| 282 | { |
| 283 | int ret; |
| 284 | int i; |
| 285 | |
| 286 | ret = 0; |
| 287 | switch(cmd) { |
| 288 | #ifdef TIOCGETP |
| 289 | case TIOCGETP: |
| 290 | case TIOCSETP: |
| 291 | case TIOCSETN: |
| 292 | #endif |
| 293 | #ifdef TIOCGETC |
| 294 | case TIOCGETC: |
| 295 | case TIOCSETC: |
| 296 | #endif |
| 297 | #ifdef TIOCGLTC |
| 298 | case TIOCGLTC: |
| 299 | case TIOCSLTC: |
| 300 | #endif |
| 301 | case TCGETS: |
| 302 | case TCSETSF: |
| 303 | case TCSETSW: |
| 304 | case TCSETS: |
| 305 | case TCGETA: |
| 306 | case TCSETAF: |
| 307 | case TCSETAW: |
| 308 | case TCSETA: |
| 309 | case TCXONC: |
| 310 | case TCFLSH: |
| 311 | case TIOCOUTQ: |
| 312 | case TIOCINQ: |
| 313 | case TIOCGLCKTRMIOS: |
| 314 | case TIOCSLCKTRMIOS: |
| 315 | case TIOCPKT: |
| 316 | case TIOCGSOFTCAR: |
| 317 | case TIOCSSOFTCAR: |
| 318 | return -ENOIOCTLCMD; |
| 319 | #if 0 |
| 320 | case TCwhatever: |
| 321 | /* do something */ |
| 322 | break; |
| 323 | #endif |
| 324 | default: |
| 325 | for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++) |
| 326 | if (cmd == tty_ioctls[i].cmd) |
| 327 | break; |
| 328 | if (i < ARRAY_SIZE(tty_ioctls)) { |
| 329 | if (NULL != tty_ioctls[i].level) |
| 330 | printk("%s%s: %s: ioctl %s called\n", |
| 331 | tty_ioctls[i].level, __FUNCTION__, |
| 332 | tty->name, tty_ioctls[i].name); |
| 333 | } else { |
| 334 | printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n", |
| 335 | __FUNCTION__, tty->name, cmd); |
| 336 | } |
| 337 | ret = -ENOIOCTLCMD; |
| 338 | break; |
| 339 | } |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 340 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Jeff Dike | e4dcee8 | 2006-01-06 00:18:58 -0800 | [diff] [blame] | 343 | void line_throttle(struct tty_struct *tty) |
| 344 | { |
| 345 | struct line *line = tty->driver_data; |
| 346 | |
| 347 | deactivate_chan(&line->chan_list, line->driver->read_irq); |
| 348 | line->throttled = 1; |
| 349 | } |
| 350 | |
| 351 | void line_unthrottle(struct tty_struct *tty) |
| 352 | { |
| 353 | struct line *line = tty->driver_data; |
| 354 | |
| 355 | line->throttled = 0; |
| 356 | chan_interrupt(&line->chan_list, &line->task, tty, |
| 357 | line->driver->read_irq); |
| 358 | |
| 359 | /* Maybe there is enough stuff pending that calling the interrupt |
| 360 | * throttles us again. In this case, line->throttled will be 1 |
| 361 | * again and we shouldn't turn the interrupt back on. |
| 362 | */ |
| 363 | if(!line->throttled) |
| 364 | reactivate_chan(&line->chan_list, line->driver->read_irq); |
| 365 | } |
| 366 | |
Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 367 | static irqreturn_t line_write_interrupt(int irq, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 369 | struct chan *chan = data; |
| 370 | struct line *line = chan->line; |
| 371 | struct tty_struct *tty = line->tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | int err; |
| 373 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 374 | /* Interrupts are enabled here because we registered the interrupt with |
Thomas Gleixner | bd6aa65 | 2006-07-01 19:29:27 -0700 | [diff] [blame] | 375 | * IRQF_DISABLED (see line_setup_irq).*/ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 376 | |
| 377 | spin_lock_irq(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | err = flush_buffer(line); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 379 | if (err == 0) { |
| 380 | return IRQ_NONE; |
| 381 | } else if(err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | line->head = line->buffer; |
| 383 | line->tail = line->buffer; |
| 384 | } |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 385 | spin_unlock_irq(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | if(tty == NULL) |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 388 | return IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 390 | if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | (tty->ldisc.write_wakeup != NULL)) |
| 392 | (tty->ldisc.write_wakeup)(tty); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | /* BLOCKING mode |
| 395 | * In blocking mode, everything sleeps on tty->write_wait. |
| 396 | * Sleeping in the console driver would break non-blocking |
| 397 | * writes. |
| 398 | */ |
| 399 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 400 | if (waitqueue_active(&tty->write_wait)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | wake_up_interruptible(&tty->write_wait); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 402 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 405 | 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] | 406 | { |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 407 | const struct line_driver *driver = line->driver; |
Thomas Gleixner | bd6aa65 | 2006-07-01 19:29:27 -0700 | [diff] [blame] | 408 | int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 410 | if (input) |
| 411 | err = um_request_irq(driver->read_irq, fd, IRQ_READ, |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 412 | line_interrupt, flags, |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 413 | driver->read_irq_name, data); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 414 | if (err) |
| 415 | return err; |
| 416 | if (output) |
| 417 | err = um_request_irq(driver->write_irq, fd, IRQ_WRITE, |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 418 | line_write_interrupt, flags, |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 419 | driver->write_irq_name, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | line->have_irq = 1; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 421 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 424 | int line_open(struct line *lines, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | { |
| 426 | struct line *line; |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 427 | int err = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | line = &lines[tty->index]; |
| 430 | tty->driver_data = line; |
| 431 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 432 | /* The IRQ which takes this lock is not yet enabled and won't be run |
| 433 | * before the end, so we don't need to use spin_lock_irq.*/ |
| 434 | spin_lock(&line->lock); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 435 | |
| 436 | tty->driver_data = line; |
| 437 | line->tty = tty; |
| 438 | if(!line->valid) |
| 439 | goto out; |
| 440 | |
| 441 | if(tty->count == 1){ |
| 442 | /* Here the device is opened, if necessary, and interrupt |
| 443 | * is registered. |
| 444 | */ |
| 445 | enable_chan(line); |
Andrew Morton | a2ce774 | 2006-12-06 20:31:36 -0800 | [diff] [blame] | 446 | INIT_DELAYED_WORK(&line->task, line_timer_cb); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 447 | |
| 448 | if(!line->sigio){ |
| 449 | chan_enable_winch(&line->chan_list, tty); |
| 450 | line->sigio = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 452 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 453 | chan_window_size(&line->chan_list, &tty->winsize.ws_row, |
| 454 | &tty->winsize.ws_col); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 457 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | out: |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 459 | spin_unlock(&line->lock); |
| 460 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 463 | static void unregister_winch(struct tty_struct *tty); |
| 464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | void line_close(struct tty_struct *tty, struct file * filp) |
| 466 | { |
| 467 | struct line *line = tty->driver_data; |
| 468 | |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 469 | /* XXX: I assume this should be called in process context, not with |
| 470 | * interrupts disabled! |
| 471 | */ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 472 | spin_lock_irq(&line->lock); |
| 473 | |
| 474 | /* We ignore the error anyway! */ |
| 475 | flush_buffer(line); |
| 476 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 477 | if(tty->count == 1){ |
| 478 | line->tty = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | tty->driver_data = NULL; |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 480 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 481 | if(line->sigio){ |
| 482 | unregister_winch(tty); |
| 483 | line->sigio = 0; |
| 484 | } |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 487 | spin_unlock_irq(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | void close_lines(struct line *lines, int nlines) |
| 491 | { |
| 492 | int i; |
| 493 | |
| 494 | for(i = 0; i < nlines; i++) |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 495 | close_chan(&lines[i].chan_list, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 498 | /* Common setup code for both startup command line and mconsole initialization. |
Matt LaPlante | 4b3f686 | 2006-10-03 22:21:02 +0200 | [diff] [blame] | 499 | * @lines contains the array (of size @num) to modify; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 500 | * @init is the setup string; |
Jeff Dike | d571cd1 | 2006-01-06 00:18:53 -0800 | [diff] [blame] | 501 | */ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 502 | |
Jeff Dike | d571cd1 | 2006-01-06 00:18:53 -0800 | [diff] [blame] | 503 | int line_setup(struct line *lines, unsigned int num, char *init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | { |
| 505 | int i, n; |
| 506 | char *end; |
| 507 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 508 | if(*init == '=') { |
| 509 | /* We said con=/ssl= instead of con#=, so we are configuring all |
| 510 | * consoles at once.*/ |
| 511 | n = -1; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 512 | } |
| 513 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | n = simple_strtoul(init, &end, 0); |
| 515 | if(*end != '='){ |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 516 | printk(KERN_ERR "line_setup failed to parse \"%s\"\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | init); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 518 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | init = end; |
| 521 | } |
| 522 | init++; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 523 | |
| 524 | if (n >= (signed int) num) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | printk("line_setup - %d out of range ((0 ... %d) allowed)\n", |
| 526 | n, num - 1); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 527 | return 0; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 528 | } |
| 529 | else if (n >= 0){ |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 530 | if (lines[n].tty != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | printk("line_setup - device %d is open\n", n); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 532 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | } |
| 534 | if (lines[n].init_pri <= INIT_ONE){ |
| 535 | lines[n].init_pri = INIT_ONE; |
| 536 | if (!strcmp(init, "none")) |
| 537 | lines[n].valid = 0; |
| 538 | else { |
| 539 | lines[n].init_str = init; |
| 540 | lines[n].valid = 1; |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 541 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | } |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 543 | } |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 544 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | for(i = 0; i < num; i++){ |
| 546 | if(lines[i].init_pri <= INIT_ALL){ |
| 547 | lines[i].init_pri = INIT_ALL; |
| 548 | if(!strcmp(init, "none")) lines[i].valid = 0; |
| 549 | else { |
| 550 | lines[i].init_str = init; |
| 551 | lines[i].valid = 1; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | } |
Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 556 | return n == -1 ? num : n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 559 | int line_config(struct line *lines, unsigned int num, char *str, |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 560 | const struct chan_opts *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | { |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 562 | struct line *line; |
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"); |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 575 | return 1; |
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); |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 578 | if(n < 0) |
| 579 | return 1; |
| 580 | |
| 581 | line = &lines[n]; |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 582 | return parse_chan_pair(line->init_str, line, n, opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 585 | 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] | 586 | int size, char **error_out) |
| 587 | { |
| 588 | struct line *line; |
| 589 | char *end; |
| 590 | int dev, n = 0; |
| 591 | |
| 592 | dev = simple_strtoul(name, &end, 0); |
| 593 | if((*end != '\0') || (end == name)){ |
| 594 | *error_out = "line_get_config failed to parse device number"; |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 595 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | if((dev < 0) || (dev >= num)){ |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 599 | *error_out = "device number out of range"; |
| 600 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | line = &lines[dev]; |
| 604 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 605 | spin_lock(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | if(!line->valid) |
| 607 | CONFIG_CHUNK(str, size, n, "none", 1); |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 608 | else if(line->tty == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | CONFIG_CHUNK(str, size, n, line->init_str, 1); |
| 610 | 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] | 611 | spin_unlock(&line->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 613 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 616 | int line_id(char **str, int *start_out, int *end_out) |
| 617 | { |
| 618 | char *end; |
| 619 | int n; |
| 620 | |
| 621 | n = simple_strtoul(*str, &end, 0); |
| 622 | if((*end != '\0') || (end == *str)) |
| 623 | return -1; |
| 624 | |
| 625 | *str = end; |
| 626 | *start_out = n; |
| 627 | *end_out = n; |
| 628 | return n; |
| 629 | } |
| 630 | |
| 631 | int line_remove(struct line *lines, unsigned int num, int n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | { |
Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 633 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | char config[sizeof("conxxxx=none\0")]; |
| 635 | |
Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 636 | sprintf(config, "%d=none", n); |
Jeff Dike | 418e55d | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 637 | err = line_setup(lines, num, config); |
| 638 | if(err >= 0) |
| 639 | err = 0; |
| 640 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | struct tty_driver *line_register_devfs(struct lines *set, |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 644 | struct line_driver *line_driver, |
| 645 | const struct tty_operations *ops, |
| 646 | struct line *lines, int nlines) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | { |
| 648 | int i; |
| 649 | struct tty_driver *driver = alloc_tty_driver(nlines); |
| 650 | |
| 651 | if (!driver) |
| 652 | return NULL; |
| 653 | |
| 654 | driver->driver_name = line_driver->name; |
| 655 | driver->name = line_driver->device_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | driver->major = line_driver->major; |
| 657 | driver->minor_start = line_driver->minor_start; |
| 658 | driver->type = line_driver->type; |
| 659 | driver->subtype = line_driver->subtype; |
| 660 | driver->flags = TTY_DRIVER_REAL_RAW; |
| 661 | driver->init_termios = tty_std_termios; |
| 662 | tty_set_operations(driver, ops); |
| 663 | |
| 664 | if (tty_register_driver(driver)) { |
| 665 | printk("%s: can't register %s driver\n", |
| 666 | __FUNCTION__,line_driver->name); |
| 667 | put_tty_driver(driver); |
| 668 | return NULL; |
| 669 | } |
| 670 | |
| 671 | for(i = 0; i < nlines; i++){ |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 672 | if(!lines[i].valid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | tty_unregister_device(driver, i); |
| 674 | } |
| 675 | |
| 676 | mconsole_register_dev(&line_driver->mc); |
| 677 | return driver; |
| 678 | } |
| 679 | |
Jeff Dike | 9010772 | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 680 | static DEFINE_SPINLOCK(winch_handler_lock); |
| 681 | static LIST_HEAD(winch_handlers); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 682 | |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 683 | void lines_init(struct line *lines, int nlines, struct chan_opts *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | { |
| 685 | struct line *line; |
| 686 | int i; |
| 687 | |
| 688 | for(i = 0; i < nlines; i++){ |
| 689 | line = &lines[i]; |
| 690 | INIT_LIST_HEAD(&line->chan_list); |
Jeff Dike | 9010772 | 2006-01-06 00:18:54 -0800 | [diff] [blame] | 691 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 692 | if(line->init_str == NULL) |
| 693 | continue; |
| 694 | |
| 695 | line->init_str = kstrdup(line->init_str, GFP_KERNEL); |
| 696 | if(line->init_str == NULL) |
| 697 | printk("lines_init - kstrdup returned NULL\n"); |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 698 | |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 699 | if(parse_chan_pair(line->init_str, line, i, opts)){ |
Jeff Dike | 1f80171 | 2006-01-06 00:18:55 -0800 | [diff] [blame] | 700 | printk("parse_chan_pair failed for device %d\n", i); |
| 701 | line->valid = 0; |
| 702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
| 706 | struct winch { |
| 707 | struct list_head list; |
| 708 | int fd; |
| 709 | int tty_fd; |
| 710 | int pid; |
| 711 | struct tty_struct *tty; |
| 712 | }; |
| 713 | |
Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 714 | static irqreturn_t winch_interrupt(int irq, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | { |
| 716 | struct winch *winch = data; |
| 717 | struct tty_struct *tty; |
| 718 | struct line *line; |
| 719 | int err; |
| 720 | char c; |
| 721 | |
| 722 | if(winch->fd != -1){ |
| 723 | err = generic_read(winch->fd, &c, NULL); |
| 724 | if(err < 0){ |
| 725 | if(err != -EAGAIN){ |
| 726 | printk("winch_interrupt : read failed, " |
| 727 | "errno = %d\n", -err); |
| 728 | printk("fd %d is losing SIGWINCH support\n", |
| 729 | winch->tty_fd); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 730 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | goto out; |
| 733 | } |
| 734 | } |
| 735 | tty = winch->tty; |
| 736 | if (tty != NULL) { |
| 737 | line = tty->driver_data; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 738 | chan_window_size(&line->chan_list, &tty->winsize.ws_row, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | &tty->winsize.ws_col); |
| 740 | kill_pg(tty->pgrp, SIGWINCH, 1); |
| 741 | } |
| 742 | out: |
| 743 | if(winch->fd != -1) |
| 744 | reactivate_fd(winch->fd, WINCH_IRQ); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 745 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty) |
| 749 | { |
| 750 | struct winch *winch; |
| 751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | winch = kmalloc(sizeof(*winch), GFP_KERNEL); |
| 753 | if (winch == NULL) { |
| 754 | printk("register_winch_irq - kmalloc failed\n"); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 755 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list), |
| 759 | .fd = fd, |
| 760 | .tty_fd = tty_fd, |
| 761 | .pid = pid, |
| 762 | .tty = tty }); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 763 | |
| 764 | spin_lock(&winch_handler_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | list_add(&winch->list, &winch_handlers); |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 766 | spin_unlock(&winch_handler_lock); |
| 767 | |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 768 | if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt, |
Thomas Gleixner | bd6aa65 | 2006-07-01 19:29:27 -0700 | [diff] [blame] | 769 | IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | "winch", winch) < 0) |
| 771 | printk("register_winch_irq - failed to register IRQ\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 774 | static void free_winch(struct winch *winch) |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 775 | { |
Paolo 'Blaisorblade' Giarrusso | 605a69a | 2005-07-07 17:56:52 -0700 | [diff] [blame] | 776 | list_del(&winch->list); |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 777 | |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 778 | if(winch->pid != -1) |
| 779 | os_kill_process(winch->pid, 1); |
| 780 | if(winch->fd != -1) |
| 781 | os_close_file(winch->fd); |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 782 | |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 783 | free_irq(WINCH_IRQ, winch); |
| 784 | kfree(winch); |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 787 | static void unregister_winch(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | { |
| 789 | struct list_head *ele; |
| 790 | struct winch *winch; |
| 791 | |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 792 | spin_lock(&winch_handler_lock); |
| 793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | list_for_each(ele, &winch_handlers){ |
| 795 | winch = list_entry(ele, struct winch, list); |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 796 | if(winch->tty == tty){ |
| 797 | free_winch(winch); |
| 798 | break; |
| 799 | } |
| 800 | } |
| 801 | spin_unlock(&winch_handler_lock); |
| 802 | } |
| 803 | |
| 804 | static void winch_cleanup(void) |
| 805 | { |
| 806 | struct list_head *ele, *next; |
| 807 | struct winch *winch; |
| 808 | |
| 809 | spin_lock(&winch_handler_lock); |
| 810 | |
| 811 | list_for_each_safe(ele, next, &winch_handlers){ |
| 812 | winch = list_entry(ele, struct winch, list); |
| 813 | free_winch(winch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | } |
Jeff Dike | e464bf2 | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 815 | |
| 816 | spin_unlock(&winch_handler_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
| 818 | __uml_exitcall(winch_cleanup); |
| 819 | |
| 820 | char *add_xterm_umid(char *base) |
| 821 | { |
| 822 | char *umid, *title; |
| 823 | int len; |
| 824 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 825 | umid = get_umid(); |
| 826 | if(*umid == '\0') |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 827 | return base; |
Jeff Dike | 165dc59 | 2006-01-06 00:18:57 -0800 | [diff] [blame] | 828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | len = strlen(base) + strlen(" ()") + strlen(umid) + 1; |
| 830 | title = kmalloc(len, GFP_KERNEL); |
| 831 | if(title == NULL){ |
| 832 | printk("Failed to allocate buffer for xterm title\n"); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 833 | return base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | snprintf(title, len, "%s (%s)", base, umid); |
Paolo 'Blaisorblade' Giarrusso | b97b77c | 2005-05-01 08:58:56 -0700 | [diff] [blame] | 837 | return title; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } |