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