blob: 9299b8a2d1717cd43bdffa37377f4b459dfcd929 [file] [log] [blame]
Jeff Dike165dc592006-01-06 00:18:57 -08001/*
Jeff Dike2f8a2dc2007-10-16 01:26:43 -07002 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Jeff Dike2f8a2dc2007-10-16 01:26:43 -07006#include "linux/irqreturn.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "linux/kd.h"
Alexey Dobriyand43c36d2009-10-07 17:09:06 +04008#include "linux/sched.h"
Jan Kiszka7f3c1fa2010-04-19 17:46:23 +09009#include "linux/slab.h"
Al Viro510c72a32011-08-18 20:08:29 +010010#include "chan.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "irq_kern.h"
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070012#include "irq_user.h"
Jeff Dikeedea1382008-02-04 22:30:46 -080013#include "kern_util.h"
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070014#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#define LINE_BUFSIZE 4096
17
Al Viro7bea96f2006-10-08 22:49:34 +010018static irqreturn_t line_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019{
Jeff Dike165dc592006-01-06 00:18:57 -080020 struct chan *chan = data;
21 struct line *line = chan->line;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23 if (line)
Al Viro0fcd7192011-09-10 08:17:04 -040024 chan_interrupt(line, line->tty, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 return IRQ_HANDLED;
26}
27
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070028/*
29 * Returns the free space inside the ring buffer of this line.
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070030 *
Simon Arlottb60745b2007-10-20 01:23:03 +020031 * Should be called while holding line->lock (this does not modify data).
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070032 */
33static int write_room(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 int n;
36
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070037 if (line->buffer == NULL)
38 return LINE_BUFSIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070040 /* This is for the case where the buffer is wrapped! */
41 n = line->head - line->tail;
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 if (n <= 0)
Jeff Dikeacb2cf32008-02-04 22:30:42 -080044 n += LINE_BUFSIZE; /* The other case */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070045 return n - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070048int 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' Giarrussob97b77c2005-05-01 08:58:56 -070054 spin_lock_irqsave(&line->lock, flags);
55 room = write_room(line);
56 spin_unlock_irqrestore(&line->lock, flags);
57
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070058 return room;
59}
60
61int 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 Dikeacb2cf32008-02-04 22:30:42 -080068 /* write_room subtracts 1 for the needed NULL, so we readd it.*/
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070069 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 Torvalds1da177e2005-04-16 15:20:36 -070084static int buffer_data(struct line *line, const char *buf, int len)
85{
86 int end, room;
87
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070088 if (line->buffer == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
90 if (line->buffer == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070091 printk(KERN_ERR "buffer_data - atomic allocation "
92 "failed\n");
93 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
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' Giarrussob97b77c2005-05-01 08:58:56 -0700103
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700104 if (len < end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 memcpy(line->tail, buf, len);
106 line->tail += len;
Jeff Diked50084a2006-01-06 00:18:50 -0800107 }
108 else {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700109 /* The circular buffer is wrapping */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 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' Giarrussob97b77c2005-05-01 08:58:56 -0700116 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700119/*
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 Torvalds1da177e2005-04-16 15:20:36 -0700128static int flush_buffer(struct line *line)
129{
130 int n, count;
131
132 if ((line->buffer == NULL) || (line->head == line->tail))
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700133 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 if (line->tail < line->head) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700136 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 count = line->buffer + LINE_BUFSIZE - line->head;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700138
Al Virobed5e392011-09-08 10:49:34 -0400139 n = write_chan(line->chan_out, line->head, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 line->driver->write_irq);
141 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700142 return n;
143 if (n == count) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700144 /*
145 * We have flushed from ->head to buffer end, now we
146 * must flush only from the beginning to ->tail.
147 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 line->head = line->buffer;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700149 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153 }
154
155 count = line->tail - line->head;
Al Virobed5e392011-09-08 10:49:34 -0400156 n = write_chan(line->chan_out, line->head, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700158
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700159 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700160 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700163 return line->head == line->tail;
164}
165
166void line_flush_buffer(struct tty_struct *tty)
167{
168 struct line *line = tty->driver_data;
169 unsigned long flags;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700170
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700171 spin_lock_irqsave(&line->lock, flags);
Richard Weinbergerf1c93e42011-07-25 17:12:55 -0700172 flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700173 spin_unlock_irqrestore(&line->lock, flags);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700174}
175
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700176/*
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' Giarrussob97b77c2005-05-01 08:58:56 -0700180void line_flush_chars(struct tty_struct *tty)
181{
182 line_flush_buffer(tty);
183}
184
WANG Cong3168cb92008-05-06 20:42:33 -0700185int line_put_char(struct tty_struct *tty, unsigned char ch)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700186{
WANG Cong3168cb92008-05-06 20:42:33 -0700187 return line_write(tty, &ch, sizeof(ch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190int 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 Dikec59dbca2007-10-16 01:26:42 -0700194 int n, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700196 spin_lock_irqsave(&line->lock, flags);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700197 if (line->head != line->tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 ret = buffer_data(line, buf, len);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700199 else {
Al Virobed5e392011-09-08 10:49:34 -0400200 n = write_chan(line->chan_out, buf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700202 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 ret = n;
204 goto out_up;
205 }
206
207 len -= n;
208 ret += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700209 if (len > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 ret += buffer_data(line, buf + n, len);
211 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700212out_up:
213 spin_unlock_irqrestore(&line->lock, flags);
214 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Alan Cox606d0992006-12-08 02:38:45 -0800217void line_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 /* nothing */
220}
221
Jeff Dike5e7672e2006-09-27 01:50:33 -0700222static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 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 Dike2f8a2dc2007-10-16 01:26:43 -0700229 { TCSETS, NULL, "TCSETS" },
230 { TCSETSW, NULL, "TCSETSW" },
231 { TCFLSH, NULL, "TCFLSH" },
232 { TCSBRK, NULL, "TCSBRK" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /* general tty stuff */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700235 { 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 Torvalds1da177e2005-04-16 15:20:36 -0700240
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 Weinberger8a06dc4d2011-03-22 16:33:48 -0700248int line_ioctl(struct tty_struct *tty, unsigned int cmd,
249 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
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 Coxc564b6f2008-10-13 10:36:49 +0100269 /* Note: these are out of date as we now have TCGETS2 etc but this
270 whole lot should probably go away */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 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 Dike2f8a2dc2007-10-16 01:26:43 -0700298 if (i == ARRAY_SIZE(tty_ioctls)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
Harvey Harrison35957262008-04-28 02:13:53 -0700300 __func__, tty->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 ret = -ENOIOCTLCMD;
303 break;
304 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700305 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Jeff Dikee4dcee82006-01-06 00:18:58 -0800308void line_throttle(struct tty_struct *tty)
309{
310 struct line *line = tty->driver_data;
311
Al Virobed5e392011-09-08 10:49:34 -0400312 deactivate_chan(line->chan_in, line->driver->read_irq);
Jeff Dikee4dcee82006-01-06 00:18:58 -0800313 line->throttled = 1;
314}
315
316void line_unthrottle(struct tty_struct *tty)
317{
318 struct line *line = tty->driver_data;
319
320 line->throttled = 0;
Al Viro0fcd7192011-09-10 08:17:04 -0400321 chan_interrupt(line, tty, line->driver->read_irq);
Jeff Dikee4dcee82006-01-06 00:18:58 -0800322
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700323 /*
324 * Maybe there is enough stuff pending that calling the interrupt
Jeff Dikee4dcee82006-01-06 00:18:58 -0800325 * throttles us again. In this case, line->throttled will be 1
326 * again and we shouldn't turn the interrupt back on.
327 */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700328 if (!line->throttled)
Al Virobed5e392011-09-08 10:49:34 -0400329 reactivate_chan(line->chan_in, line->driver->read_irq);
Jeff Dikee4dcee82006-01-06 00:18:58 -0800330}
331
Al Viro7bea96f2006-10-08 22:49:34 +0100332static irqreturn_t line_write_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Jeff Dike165dc592006-01-06 00:18:57 -0800334 struct chan *chan = data;
335 struct line *line = chan->line;
336 struct tty_struct *tty = line->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int err;
338
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700339 /*
Yong Zhangc0b79a92011-09-22 16:58:46 +0800340 * Interrupts are disabled here because genirq keep irqs disabled when
341 * calling the action handler.
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700342 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700343
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800344 spin_lock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700346 if (err == 0) {
347 return IRQ_NONE;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700348 } else if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 line->head = line->buffer;
350 line->tail = line->buffer;
351 }
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800352 spin_unlock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700354 if (tty == NULL)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700355 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
WANG Cong06ac6672008-07-29 22:33:34 -0700357 tty_wakeup(tty);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700358 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Jeff Dike165dc592006-01-06 00:18:57 -0800361int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700363 const struct line_driver *driver = line->driver;
Yong Zhangc0b79a92011-09-22 16:58:46 +0800364 int err = 0, flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700366 if (input)
367 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
Jeff Diked50084a2006-01-06 00:18:50 -0800368 line_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800369 driver->read_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700370 if (err)
371 return err;
372 if (output)
373 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
Jeff Diked50084a2006-01-06 00:18:50 -0800374 line_write_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800375 driver->write_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700376 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700379/*
380 * Normally, a driver like this can rely mostly on the tty layer
Jeff Diked79a5802007-02-10 01:43:52 -0800381 * locking, particularly when it comes to the driver structure.
382 * However, in this case, mconsole requests can come in "from the
383 * side", and race with opens and closes.
384 *
Jeff Dikec6256c62007-02-10 01:44:08 -0800385 * mconsole config requests will want to be sure the device isn't in
386 * use, and get_config, open, and close will want a stable
387 * configuration. The checking and modification of the configuration
388 * is done under a spinlock. Checking whether the device is in use is
389 * line->tty->count > 1, also under the spinlock.
Jeff Diked79a5802007-02-10 01:43:52 -0800390 *
Al Virof71f9482011-09-14 16:21:25 -0700391 * line->count serves to decide whether the device should be enabled or
392 * disabled on the host. If it's equal to 0, then we are doing the
Jeff Dikec6256c62007-02-10 01:44:08 -0800393 * first open or last close. Otherwise, open and close just return.
Jeff Diked79a5802007-02-10 01:43:52 -0800394 */
395
Jeff Dike1f801712006-01-06 00:18:55 -0800396int line_open(struct line *lines, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Jeff Diked79a5802007-02-10 01:43:52 -0800398 struct line *line = &lines[tty->index];
Jeff Dike165dc592006-01-06 00:18:57 -0800399 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Al Virod8c215a2011-09-09 17:36:37 -0400401 mutex_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700402 if (!line->valid)
Jeff Diked79a5802007-02-10 01:43:52 -0800403 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Jeff Diked79a5802007-02-10 01:43:52 -0800405 err = 0;
Al Virof71f9482011-09-14 16:21:25 -0700406 if (line->count++)
Jeff Diked79a5802007-02-10 01:43:52 -0800407 goto out_unlock;
408
Al Virof71f9482011-09-14 16:21:25 -0700409 BUG_ON(tty->driver_data);
Jeff Dike165dc592006-01-06 00:18:57 -0800410 tty->driver_data = line;
411 line->tty = tty;
Jeff Dike165dc592006-01-06 00:18:57 -0800412
Jeff Diked14ad812007-07-15 23:38:54 -0700413 err = enable_chan(line);
Al Virof71f9482011-09-14 16:21:25 -0700414 if (err) /* line_close() will be called by our caller */
Al Virod8c215a2011-09-09 17:36:37 -0400415 goto out_unlock;
Jeff Diked14ad812007-07-15 23:38:54 -0700416
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700417 if (!line->sigio) {
Al Virobed5e392011-09-08 10:49:34 -0400418 chan_enable_winch(line->chan_out, tty);
Jeff Diked79a5802007-02-10 01:43:52 -0800419 line->sigio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
Al Virobed5e392011-09-08 10:49:34 -0400422 chan_window_size(line, &tty->winsize.ws_row,
Jeff Diked79a5802007-02-10 01:43:52 -0800423 &tty->winsize.ws_col);
Jeff Diked79a5802007-02-10 01:43:52 -0800424out_unlock:
Al Virod8c215a2011-09-09 17:36:37 -0400425 mutex_unlock(&line->count_lock);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700429static void unregister_winch(struct tty_struct *tty);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431void line_close(struct tty_struct *tty, struct file * filp)
432{
433 struct line *line = tty->driver_data;
434
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700435 /*
436 * If line_open fails (and tty->driver_data is never set),
Jeff Diked79a5802007-02-10 01:43:52 -0800437 * tty_open will call line_close. So just return in this case.
438 */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700439 if (line == NULL)
Jeff Diked79a5802007-02-10 01:43:52 -0800440 return;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700441
442 /* We ignore the error anyway! */
443 flush_buffer(line);
444
Al Virod8c215a2011-09-09 17:36:37 -0400445 mutex_lock(&line->count_lock);
Al Virof71f9482011-09-14 16:21:25 -0700446 BUG_ON(!line->valid);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700447
Al Virof71f9482011-09-14 16:21:25 -0700448 if (--line->count)
Jeff Diked79a5802007-02-10 01:43:52 -0800449 goto out_unlock;
450
Jeff Diked79a5802007-02-10 01:43:52 -0800451 line->tty = NULL;
452 tty->driver_data = NULL;
453
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700454 if (line->sigio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800455 unregister_winch(tty);
456 line->sigio = 0;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700457 }
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700458
Jeff Diked79a5802007-02-10 01:43:52 -0800459out_unlock:
Al Virod8c215a2011-09-09 17:36:37 -0400460 mutex_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463void close_lines(struct line *lines, int nlines)
464{
465 int i;
466
467 for(i = 0; i < nlines; i++)
Jeff Dike165dc592006-01-06 00:18:57 -0800468 close_chan(&lines[i].chan_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Al Viro04292b22011-09-09 20:07:05 -0400471int setup_one_line(struct line *lines, int n, char *init,
472 const struct chan_opts *opts, char **error_out)
Jeff Diked79a5802007-02-10 01:43:52 -0800473{
474 struct line *line = &lines[n];
Al Virocfe6b7c2011-09-09 19:45:42 -0400475 struct tty_driver *driver = line->driver->driver;
Jeff Dikef28169d2007-02-10 01:43:53 -0800476 int err = -EINVAL;
Jeff Diked79a5802007-02-10 01:43:52 -0800477
Al Virod8c215a2011-09-09 17:36:37 -0400478 mutex_lock(&line->count_lock);
Jeff Diked79a5802007-02-10 01:43:52 -0800479
Al Virof71f9482011-09-14 16:21:25 -0700480 if (line->count) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800481 *error_out = "Device is already open";
Jeff Diked79a5802007-02-10 01:43:52 -0800482 goto out;
483 }
484
Al Viro31efceb2011-09-09 19:14:02 -0400485 if (!strcmp(init, "none")) {
486 if (line->valid) {
487 line->valid = 0;
488 kfree(line->init_str);
Al Virocfe6b7c2011-09-09 19:45:42 -0400489 tty_unregister_device(driver, n);
Al Viro31efceb2011-09-09 19:14:02 -0400490 parse_chan_pair(NULL, line, n, opts, error_out);
491 err = 0;
492 }
493 } else {
494 char *new = kstrdup(init, GFP_KERNEL);
495 if (!new) {
496 *error_out = "Failed to allocate memory";
497 return -ENOMEM;
498 }
Al Viroc8e28762011-09-09 20:08:48 -0400499 if (line->valid) {
Al Virocfe6b7c2011-09-09 19:45:42 -0400500 tty_unregister_device(driver, n);
Al Viroc8e28762011-09-09 20:08:48 -0400501 kfree(line->init_str);
502 }
Al Viro31efceb2011-09-09 19:14:02 -0400503 line->init_str = new;
Al Viro43574c12011-09-09 17:25:00 -0400504 line->valid = 1;
Al Viro31efceb2011-09-09 19:14:02 -0400505 err = parse_chan_pair(new, line, n, opts, error_out);
Al Virocfe6b7c2011-09-09 19:45:42 -0400506 if (!err) {
507 struct device *d = tty_register_device(driver, n, NULL);
508 if (IS_ERR(d)) {
509 *error_out = "Failed to register device";
510 err = PTR_ERR(d);
511 parse_chan_pair(NULL, line, n, opts, error_out);
512 }
513 }
Al Viro31efceb2011-09-09 19:14:02 -0400514 if (err) {
515 line->init_str = NULL;
516 line->valid = 0;
517 kfree(new);
518 }
Jeff Diked79a5802007-02-10 01:43:52 -0800519 }
520out:
Al Virod8c215a2011-09-09 17:36:37 -0400521 mutex_unlock(&line->count_lock);
Jeff Dikef28169d2007-02-10 01:43:53 -0800522 return err;
Jeff Diked79a5802007-02-10 01:43:52 -0800523}
524
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700525/*
526 * Common setup code for both startup command line and mconsole initialization.
Matt LaPlante4b3f6862006-10-03 22:21:02 +0200527 * @lines contains the array (of size @num) to modify;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700528 * @init is the setup string;
Jeff Dikef28169d2007-02-10 01:43:53 -0800529 * @error_out is an error string in the case of failure;
Jeff Diked571cd12006-01-06 00:18:53 -0800530 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700531
Al Viro43574c12011-09-09 17:25:00 -0400532int line_setup(char **conf, unsigned int num, char **def,
533 char *init, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Al Viro43574c12011-09-09 17:25:00 -0400535 char *error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700537 if (*init == '=') {
538 /*
539 * We said con=/ssl= instead of con#=, so we are configuring all
540 * consoles at once.
541 */
Al Viro43574c12011-09-09 17:25:00 -0400542 *def = init + 1;
543 } else {
544 char *end;
545 unsigned n = simple_strtoul(init, &end, 0);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700546
Al Viro43574c12011-09-09 17:25:00 -0400547 if (*end != '=') {
548 error = "Couldn't parse device number";
549 goto out;
Jeff Dikef28169d2007-02-10 01:43:53 -0800550 }
Al Viro43574c12011-09-09 17:25:00 -0400551 if (n >= num) {
552 error = "Device number out of range";
553 goto out;
554 }
555 conf[n] = end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Al Viro43574c12011-09-09 17:25:00 -0400557 return 0;
558
559out:
560 printk(KERN_ERR "Failed to set up %s with "
561 "configuration string \"%s\" : %s\n", name, init, error);
562 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Jeff Dike1f801712006-01-06 00:18:55 -0800565int line_config(struct line *lines, unsigned int num, char *str,
Jeff Dikef28169d2007-02-10 01:43:53 -0800566 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Al Virofe9a6b02011-09-08 20:44:06 -0400568 char *end;
Al Viro31efceb2011-09-09 19:14:02 -0400569 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700571 if (*str == '=') {
Jeff Dikef28169d2007-02-10 01:43:53 -0800572 *error_out = "Can't configure all devices from mconsole";
573 return -EINVAL;
Jeff Diked571cd12006-01-06 00:18:53 -0800574 }
575
Al Virofe9a6b02011-09-08 20:44:06 -0400576 n = simple_strtoul(str, &end, 0);
577 if (*end++ != '=') {
578 *error_out = "Couldn't parse device number";
579 return -EINVAL;
580 }
581 if (n >= num) {
582 *error_out = "Device number out of range";
583 return -EINVAL;
584 }
585
Al Viro31efceb2011-09-09 19:14:02 -0400586 return setup_one_line(lines, n, end, opts, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700589int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 int size, char **error_out)
591{
592 struct line *line;
593 char *end;
594 int dev, n = 0;
595
596 dev = simple_strtoul(name, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700597 if ((*end != '\0') || (end == name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 *error_out = "line_get_config failed to parse device number";
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700602 if ((dev < 0) || (dev >= num)) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700603 *error_out = "device number out of range";
604 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606
607 line = &lines[dev];
608
Al Virod8c215a2011-09-09 17:36:37 -0400609 mutex_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700610 if (!line->valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700612 else if (line->tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 CONFIG_CHUNK(str, size, n, line->init_str, 1);
Al Virobed5e392011-09-08 10:49:34 -0400614 else n = chan_config_string(line, str, size, error_out);
Al Virod8c215a2011-09-09 17:36:37 -0400615 mutex_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700617 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Jeff Dike29d56cf2005-06-25 14:55:25 -0700620int line_id(char **str, int *start_out, int *end_out)
621{
622 char *end;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700623 int n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700624
625 n = simple_strtoul(*str, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700626 if ((*end != '\0') || (end == *str))
627 return -1;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700628
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700629 *str = end;
630 *start_out = n;
631 *end_out = n;
632 return n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700633}
634
Jeff Dikef28169d2007-02-10 01:43:53 -0800635int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Al Viroda645f32011-09-08 20:34:52 -0400637 if (n >= num) {
638 *error_out = "Device number out of range";
639 return -EINVAL;
640 }
Al Viro31efceb2011-09-09 19:14:02 -0400641 return setup_one_line(lines, n, "none", NULL, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Al Virocfe6b7c2011-09-09 19:45:42 -0400644int register_lines(struct line_driver *line_driver,
645 const struct tty_operations *ops,
646 struct line *lines, int nlines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct tty_driver *driver = alloc_tty_driver(nlines);
Al Virocfe6b7c2011-09-09 19:45:42 -0400649 int err;
Al Viro04292b22011-09-09 20:07:05 -0400650 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 if (!driver)
Al Virocfe6b7c2011-09-09 19:45:42 -0400653 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 driver->driver_name = line_driver->name;
656 driver->name = line_driver->device_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 driver->major = line_driver->major;
658 driver->minor_start = line_driver->minor_start;
659 driver->type = line_driver->type;
660 driver->subtype = line_driver->subtype;
Al Virocfe6b7c2011-09-09 19:45:42 -0400661 driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 driver->init_termios = tty_std_termios;
Al Viro04292b22011-09-09 20:07:05 -0400663
664 for (i = 0; i < nlines; i++) {
665 spin_lock_init(&lines[i].lock);
666 mutex_init(&lines[i].count_lock);
667 lines[i].driver = line_driver;
668 INIT_LIST_HEAD(&lines[i].chan_list);
669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 tty_set_operations(driver, ops);
671
Al Virocfe6b7c2011-09-09 19:45:42 -0400672 err = tty_register_driver(driver);
673 if (err) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700674 printk(KERN_ERR "register_lines : can't register %s driver\n",
675 line_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 put_tty_driver(driver);
Al Virocfe6b7c2011-09-09 19:45:42 -0400677 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Al Virocfe6b7c2011-09-09 19:45:42 -0400680 line_driver->driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 mconsole_register_dev(&line_driver->mc);
Al Virocfe6b7c2011-09-09 19:45:42 -0400682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Jeff Dike90107722006-01-06 00:18:54 -0800685static DEFINE_SPINLOCK(winch_handler_lock);
686static LIST_HEAD(winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688struct winch {
689 struct list_head list;
690 int fd;
691 int tty_fd;
692 int pid;
693 struct tty_struct *tty;
Jeff Dike42a359e2007-07-15 23:38:55 -0700694 unsigned long stack;
Al Viro7cf3cf22011-09-14 16:21:31 -0700695 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696};
697
Al Viro7cf3cf22011-09-14 16:21:31 -0700698static void __free_winch(struct work_struct *work)
Jeff Dike42a359e2007-07-15 23:38:55 -0700699{
Al Viro7cf3cf22011-09-14 16:21:31 -0700700 struct winch *winch = container_of(work, struct winch, work);
701 free_irq(WINCH_IRQ, winch);
Jeff Dike42a359e2007-07-15 23:38:55 -0700702
703 if (winch->pid != -1)
704 os_kill_process(winch->pid, 1);
Jeff Dike42a359e2007-07-15 23:38:55 -0700705 if (winch->stack != 0)
706 free_stack(winch->stack, 0);
Jeff Dike42a359e2007-07-15 23:38:55 -0700707 kfree(winch);
708}
709
Al Viro7cf3cf22011-09-14 16:21:31 -0700710static void free_winch(struct winch *winch)
711{
712 int fd = winch->fd;
713 winch->fd = -1;
714 if (fd != -1)
715 os_close_file(fd);
716 list_del(&winch->list);
717 __free_winch(&winch->work);
718}
719
Al Viro7bea96f2006-10-08 22:49:34 +0100720static irqreturn_t winch_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct winch *winch = data;
723 struct tty_struct *tty;
724 struct line *line;
Al Viro7cf3cf22011-09-14 16:21:31 -0700725 int fd = winch->fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 int err;
727 char c;
728
Al Viro7cf3cf22011-09-14 16:21:31 -0700729 if (fd != -1) {
730 err = generic_read(fd, &c, NULL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700731 if (err < 0) {
732 if (err != -EAGAIN) {
Al Viro7cf3cf22011-09-14 16:21:31 -0700733 winch->fd = -1;
734 list_del(&winch->list);
735 os_close_file(fd);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700736 printk(KERN_ERR "winch_interrupt : "
737 "read failed, errno = %d\n", -err);
738 printk(KERN_ERR "fd %d is losing SIGWINCH "
739 "support\n", winch->tty_fd);
Al Viro7cf3cf22011-09-14 16:21:31 -0700740 INIT_WORK(&winch->work, __free_winch);
741 schedule_work(&winch->work);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700742 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744 goto out;
745 }
746 }
Jeff Dike42a359e2007-07-15 23:38:55 -0700747 tty = winch->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (tty != NULL) {
749 line = tty->driver_data;
Jeff Dike438ee672008-02-04 22:31:19 -0800750 if (line != NULL) {
Al Virobed5e392011-09-08 10:49:34 -0400751 chan_window_size(line, &tty->winsize.ws_row,
Jeff Dike438ee672008-02-04 22:31:19 -0800752 &tty->winsize.ws_col);
753 kill_pgrp(tty->pgrp, SIGWINCH, 1);
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756 out:
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700757 if (winch->fd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 reactivate_fd(winch->fd, WINCH_IRQ);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700759 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Jeff Dike42a359e2007-07-15 23:38:55 -0700762void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
763 unsigned long stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 struct winch *winch;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
768 if (winch == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700769 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700770 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
774 .fd = fd,
775 .tty_fd = tty_fd,
776 .pid = pid,
Jeff Dike42a359e2007-07-15 23:38:55 -0700777 .tty = tty,
778 .stack = stack });
779
780 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
Yong Zhangc0b79a92011-09-22 16:58:46 +0800781 IRQF_SHARED | IRQF_SAMPLE_RANDOM,
Jeff Dike42a359e2007-07-15 23:38:55 -0700782 "winch", winch) < 0) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700783 printk(KERN_ERR "register_winch_irq - failed to register "
784 "IRQ\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700785 goto out_free;
786 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700787
788 spin_lock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 list_add(&winch->list, &winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700790 spin_unlock(&winch_handler_lock);
791
Jeff Dike42a359e2007-07-15 23:38:55 -0700792 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Jeff Dike42a359e2007-07-15 23:38:55 -0700794 out_free:
Jeff Dikee464bf22006-01-06 00:19:01 -0800795 kfree(winch);
Jeff Dike42a359e2007-07-15 23:38:55 -0700796 cleanup:
797 os_kill_process(pid, 1);
798 os_close_file(fd);
799 if (stack != 0)
800 free_stack(stack, 0);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700801}
802
Jeff Dikee464bf22006-01-06 00:19:01 -0800803static void unregister_winch(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Will Newton48a0b742011-01-12 16:59:26 -0800805 struct list_head *ele, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 struct winch *winch;
807
Jeff Dikee464bf22006-01-06 00:19:01 -0800808 spin_lock(&winch_handler_lock);
809
Will Newton48a0b742011-01-12 16:59:26 -0800810 list_for_each_safe(ele, next, &winch_handlers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 winch = list_entry(ele, struct winch, list);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700812 if (winch->tty == tty) {
Al Viro7cf3cf22011-09-14 16:21:31 -0700813 free_winch(winch);
Jeff Dikee464bf22006-01-06 00:19:01 -0800814 break;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700815 }
816 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800817 spin_unlock(&winch_handler_lock);
818}
819
820static void winch_cleanup(void)
821{
822 struct list_head *ele, *next;
823 struct winch *winch;
824
825 spin_lock(&winch_handler_lock);
826
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700827 list_for_each_safe(ele, next, &winch_handlers) {
Jeff Dikee464bf22006-01-06 00:19:01 -0800828 winch = list_entry(ele, struct winch, list);
Al Viro7cf3cf22011-09-14 16:21:31 -0700829 free_winch(winch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800831
832 spin_unlock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834__uml_exitcall(winch_cleanup);
835
836char *add_xterm_umid(char *base)
837{
838 char *umid, *title;
839 int len;
840
Jeff Dike7eebe8a2006-01-06 00:19:01 -0800841 umid = get_umid();
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700842 if (*umid == '\0')
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700843 return base;
Jeff Dike165dc592006-01-06 00:18:57 -0800844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
846 title = kmalloc(len, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700847 if (title == NULL) {
848 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700849 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851
852 snprintf(title, len, "%s (%s)", base, umid);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700853 return title;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}