blob: 482a7bd4a64c2c1342f4dd33035d6250f1ab102a [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) {
Al Viro199eebb2012-02-11 03:05:32 -0500347 spin_unlock(&line->lock);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700348 return IRQ_NONE;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700349 } else if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 line->head = line->buffer;
351 line->tail = line->buffer;
352 }
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800353 spin_unlock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700355 if (tty == NULL)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700356 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
WANG Cong06ac6672008-07-29 22:33:34 -0700358 tty_wakeup(tty);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700359 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Jeff Dike165dc592006-01-06 00:18:57 -0800362int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700364 const struct line_driver *driver = line->driver;
Yong Zhangc0b79a92011-09-22 16:58:46 +0800365 int err = 0, flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700367 if (input)
368 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
Jeff Diked50084a2006-01-06 00:18:50 -0800369 line_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800370 driver->read_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700371 if (err)
372 return err;
373 if (output)
374 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
Jeff Diked50084a2006-01-06 00:18:50 -0800375 line_write_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800376 driver->write_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700377 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700380/*
381 * Normally, a driver like this can rely mostly on the tty layer
Jeff Diked79a5802007-02-10 01:43:52 -0800382 * 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 Dikec6256c62007-02-10 01:44:08 -0800386 * 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 Diked79a5802007-02-10 01:43:52 -0800391 *
Al Virof71f9482011-09-14 16:21:25 -0700392 * 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 Dikec6256c62007-02-10 01:44:08 -0800394 * first open or last close. Otherwise, open and close just return.
Jeff Diked79a5802007-02-10 01:43:52 -0800395 */
396
Jeff Dike1f801712006-01-06 00:18:55 -0800397int line_open(struct line *lines, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Jeff Diked79a5802007-02-10 01:43:52 -0800399 struct line *line = &lines[tty->index];
Jeff Dike165dc592006-01-06 00:18:57 -0800400 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Al Virod8c215a2011-09-09 17:36:37 -0400402 mutex_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700403 if (!line->valid)
Jeff Diked79a5802007-02-10 01:43:52 -0800404 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Jeff Diked79a5802007-02-10 01:43:52 -0800406 err = 0;
Jiri Slaby060ed312012-06-04 13:35:26 +0200407 if (line->port.count++)
Jeff Diked79a5802007-02-10 01:43:52 -0800408 goto out_unlock;
409
Al Virof71f9482011-09-14 16:21:25 -0700410 BUG_ON(tty->driver_data);
Jeff Dike165dc592006-01-06 00:18:57 -0800411 tty->driver_data = line;
412 line->tty = tty;
Jeff Dike165dc592006-01-06 00:18:57 -0800413
Jeff Diked14ad812007-07-15 23:38:54 -0700414 err = enable_chan(line);
Al Virof71f9482011-09-14 16:21:25 -0700415 if (err) /* line_close() will be called by our caller */
Al Virod8c215a2011-09-09 17:36:37 -0400416 goto out_unlock;
Jeff Diked14ad812007-07-15 23:38:54 -0700417
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700418 if (!line->sigio) {
Al Virobed5e392011-09-08 10:49:34 -0400419 chan_enable_winch(line->chan_out, tty);
Jeff Diked79a5802007-02-10 01:43:52 -0800420 line->sigio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
Al Virobed5e392011-09-08 10:49:34 -0400423 chan_window_size(line, &tty->winsize.ws_row,
Jeff Diked79a5802007-02-10 01:43:52 -0800424 &tty->winsize.ws_col);
Jeff Diked79a5802007-02-10 01:43:52 -0800425out_unlock:
Al Virod8c215a2011-09-09 17:36:37 -0400426 mutex_unlock(&line->count_lock);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700427 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700430static void unregister_winch(struct tty_struct *tty);
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432void line_close(struct tty_struct *tty, struct file * filp)
433{
434 struct line *line = tty->driver_data;
435
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700436 /*
437 * If line_open fails (and tty->driver_data is never set),
Jeff Diked79a5802007-02-10 01:43:52 -0800438 * tty_open will call line_close. So just return in this case.
439 */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700440 if (line == NULL)
Jeff Diked79a5802007-02-10 01:43:52 -0800441 return;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700442
443 /* We ignore the error anyway! */
444 flush_buffer(line);
445
Al Virod8c215a2011-09-09 17:36:37 -0400446 mutex_lock(&line->count_lock);
Al Virof71f9482011-09-14 16:21:25 -0700447 BUG_ON(!line->valid);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700448
Jiri Slaby060ed312012-06-04 13:35:26 +0200449 if (--line->port.count)
Jeff Diked79a5802007-02-10 01:43:52 -0800450 goto out_unlock;
451
Jeff Diked79a5802007-02-10 01:43:52 -0800452 line->tty = NULL;
453 tty->driver_data = NULL;
454
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700455 if (line->sigio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800456 unregister_winch(tty);
457 line->sigio = 0;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700458 }
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700459
Jeff Diked79a5802007-02-10 01:43:52 -0800460out_unlock:
Al Virod8c215a2011-09-09 17:36:37 -0400461 mutex_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464void close_lines(struct line *lines, int nlines)
465{
466 int i;
467
468 for(i = 0; i < nlines; i++)
Al Viro10c890c02011-09-10 08:39:18 -0400469 close_chan(&lines[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Al Viro04292b22011-09-09 20:07:05 -0400472int setup_one_line(struct line *lines, int n, char *init,
473 const struct chan_opts *opts, char **error_out)
Jeff Diked79a5802007-02-10 01:43:52 -0800474{
475 struct line *line = &lines[n];
Al Virocfe6b7c2011-09-09 19:45:42 -0400476 struct tty_driver *driver = line->driver->driver;
Jeff Dikef28169d2007-02-10 01:43:53 -0800477 int err = -EINVAL;
Jeff Diked79a5802007-02-10 01:43:52 -0800478
Al Virod8c215a2011-09-09 17:36:37 -0400479 mutex_lock(&line->count_lock);
Jeff Diked79a5802007-02-10 01:43:52 -0800480
Jiri Slaby060ed312012-06-04 13:35:26 +0200481 if (line->port.count) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800482 *error_out = "Device is already open";
Jeff Diked79a5802007-02-10 01:43:52 -0800483 goto out;
484 }
485
Al Viro31efceb2011-09-09 19:14:02 -0400486 if (!strcmp(init, "none")) {
487 if (line->valid) {
488 line->valid = 0;
489 kfree(line->init_str);
Al Virocfe6b7c2011-09-09 19:45:42 -0400490 tty_unregister_device(driver, n);
Al Viro31efceb2011-09-09 19:14:02 -0400491 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 Viroc8e28762011-09-09 20:08:48 -0400500 if (line->valid) {
Al Virocfe6b7c2011-09-09 19:45:42 -0400501 tty_unregister_device(driver, n);
Al Viroc8e28762011-09-09 20:08:48 -0400502 kfree(line->init_str);
503 }
Al Viro31efceb2011-09-09 19:14:02 -0400504 line->init_str = new;
Al Viro43574c12011-09-09 17:25:00 -0400505 line->valid = 1;
Al Viro31efceb2011-09-09 19:14:02 -0400506 err = parse_chan_pair(new, line, n, opts, error_out);
Al Virocfe6b7c2011-09-09 19:45:42 -0400507 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 Viro31efceb2011-09-09 19:14:02 -0400515 if (err) {
516 line->init_str = NULL;
517 line->valid = 0;
518 kfree(new);
519 }
Jeff Diked79a5802007-02-10 01:43:52 -0800520 }
521out:
Al Virod8c215a2011-09-09 17:36:37 -0400522 mutex_unlock(&line->count_lock);
Jeff Dikef28169d2007-02-10 01:43:53 -0800523 return err;
Jeff Diked79a5802007-02-10 01:43:52 -0800524}
525
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700526/*
527 * Common setup code for both startup command line and mconsole initialization.
Matt LaPlante4b3f6862006-10-03 22:21:02 +0200528 * @lines contains the array (of size @num) to modify;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700529 * @init is the setup string;
Jeff Dikef28169d2007-02-10 01:43:53 -0800530 * @error_out is an error string in the case of failure;
Jeff Diked571cd12006-01-06 00:18:53 -0800531 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700532
Al Viro43574c12011-09-09 17:25:00 -0400533int line_setup(char **conf, unsigned int num, char **def,
534 char *init, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Al Viro43574c12011-09-09 17:25:00 -0400536 char *error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700538 if (*init == '=') {
539 /*
540 * We said con=/ssl= instead of con#=, so we are configuring all
541 * consoles at once.
542 */
Al Viro43574c12011-09-09 17:25:00 -0400543 *def = init + 1;
544 } else {
545 char *end;
546 unsigned n = simple_strtoul(init, &end, 0);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700547
Al Viro43574c12011-09-09 17:25:00 -0400548 if (*end != '=') {
549 error = "Couldn't parse device number";
550 goto out;
Jeff Dikef28169d2007-02-10 01:43:53 -0800551 }
Al Viro43574c12011-09-09 17:25:00 -0400552 if (n >= num) {
553 error = "Device number out of range";
554 goto out;
555 }
556 conf[n] = end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Al Viro43574c12011-09-09 17:25:00 -0400558 return 0;
559
560out:
561 printk(KERN_ERR "Failed to set up %s with "
562 "configuration string \"%s\" : %s\n", name, init, error);
563 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Jeff Dike1f801712006-01-06 00:18:55 -0800566int line_config(struct line *lines, unsigned int num, char *str,
Jeff Dikef28169d2007-02-10 01:43:53 -0800567 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Al Virofe9a6b02011-09-08 20:44:06 -0400569 char *end;
Al Viro31efceb2011-09-09 19:14:02 -0400570 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700572 if (*str == '=') {
Jeff Dikef28169d2007-02-10 01:43:53 -0800573 *error_out = "Can't configure all devices from mconsole";
574 return -EINVAL;
Jeff Diked571cd12006-01-06 00:18:53 -0800575 }
576
Al Virofe9a6b02011-09-08 20:44:06 -0400577 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 Viro31efceb2011-09-09 19:14:02 -0400587 return setup_one_line(lines, n, end, opts, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700590int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 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 Dike2f8a2dc2007-10-16 01:26:43 -0700598 if ((*end != '\0') || (end == name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 *error_out = "line_get_config failed to parse device number";
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700600 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700603 if ((dev < 0) || (dev >= num)) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700604 *error_out = "device number out of range";
605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
608 line = &lines[dev];
609
Al Virod8c215a2011-09-09 17:36:37 -0400610 mutex_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700611 if (!line->valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700613 else if (line->tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 CONFIG_CHUNK(str, size, n, line->init_str, 1);
Al Virobed5e392011-09-08 10:49:34 -0400615 else n = chan_config_string(line, str, size, error_out);
Al Virod8c215a2011-09-09 17:36:37 -0400616 mutex_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700618 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Jeff Dike29d56cf2005-06-25 14:55:25 -0700621int line_id(char **str, int *start_out, int *end_out)
622{
623 char *end;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700624 int n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700625
626 n = simple_strtoul(*str, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700627 if ((*end != '\0') || (end == *str))
628 return -1;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700629
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700630 *str = end;
631 *start_out = n;
632 *end_out = n;
633 return n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700634}
635
Jeff Dikef28169d2007-02-10 01:43:53 -0800636int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Al Viroda645f32011-09-08 20:34:52 -0400638 if (n >= num) {
639 *error_out = "Device number out of range";
640 return -EINVAL;
641 }
Al Viro31efceb2011-09-09 19:14:02 -0400642 return setup_one_line(lines, n, "none", NULL, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Al Virocfe6b7c2011-09-09 19:45:42 -0400645int register_lines(struct line_driver *line_driver,
646 const struct tty_operations *ops,
647 struct line *lines, int nlines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct tty_driver *driver = alloc_tty_driver(nlines);
Al Virocfe6b7c2011-09-09 19:45:42 -0400650 int err;
Al Viro04292b22011-09-09 20:07:05 -0400651 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 if (!driver)
Al Virocfe6b7c2011-09-09 19:45:42 -0400654 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 driver->driver_name = line_driver->name;
657 driver->name = line_driver->device_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 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 Virocfe6b7c2011-09-09 19:45:42 -0400662 driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 driver->init_termios = tty_std_termios;
Al Viro04292b22011-09-09 20:07:05 -0400664
665 for (i = 0; i < nlines; i++) {
Jiri Slaby060ed312012-06-04 13:35:26 +0200666 tty_port_init(&lines[i].port);
Al Viro04292b22011-09-09 20:07:05 -0400667 spin_lock_init(&lines[i].lock);
668 mutex_init(&lines[i].count_lock);
669 lines[i].driver = line_driver;
670 INIT_LIST_HEAD(&lines[i].chan_list);
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 tty_set_operations(driver, ops);
673
Al Virocfe6b7c2011-09-09 19:45:42 -0400674 err = tty_register_driver(driver);
675 if (err) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700676 printk(KERN_ERR "register_lines : can't register %s driver\n",
677 line_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 put_tty_driver(driver);
Al Virocfe6b7c2011-09-09 19:45:42 -0400679 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
Al Virocfe6b7c2011-09-09 19:45:42 -0400682 line_driver->driver = driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 mconsole_register_dev(&line_driver->mc);
Al Virocfe6b7c2011-09-09 19:45:42 -0400684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Jeff Dike90107722006-01-06 00:18:54 -0800687static DEFINE_SPINLOCK(winch_handler_lock);
688static LIST_HEAD(winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690struct winch {
691 struct list_head list;
692 int fd;
693 int tty_fd;
694 int pid;
695 struct tty_struct *tty;
Jeff Dike42a359e2007-07-15 23:38:55 -0700696 unsigned long stack;
Al Viro7cf3cf22011-09-14 16:21:31 -0700697 struct work_struct work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698};
699
Al Viro7cf3cf22011-09-14 16:21:31 -0700700static void __free_winch(struct work_struct *work)
Jeff Dike42a359e2007-07-15 23:38:55 -0700701{
Al Viro7cf3cf22011-09-14 16:21:31 -0700702 struct winch *winch = container_of(work, struct winch, work);
Richard Weinbergerfa7a0442012-04-17 22:37:13 +0200703 um_free_irq(WINCH_IRQ, winch);
Jeff Dike42a359e2007-07-15 23:38:55 -0700704
705 if (winch->pid != -1)
706 os_kill_process(winch->pid, 1);
Jeff Dike42a359e2007-07-15 23:38:55 -0700707 if (winch->stack != 0)
708 free_stack(winch->stack, 0);
Jeff Dike42a359e2007-07-15 23:38:55 -0700709 kfree(winch);
710}
711
Al Viro7cf3cf22011-09-14 16:21:31 -0700712static void free_winch(struct winch *winch)
713{
714 int fd = winch->fd;
715 winch->fd = -1;
716 if (fd != -1)
717 os_close_file(fd);
718 list_del(&winch->list);
719 __free_winch(&winch->work);
720}
721
Al Viro7bea96f2006-10-08 22:49:34 +0100722static irqreturn_t winch_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 struct winch *winch = data;
725 struct tty_struct *tty;
726 struct line *line;
Al Viro7cf3cf22011-09-14 16:21:31 -0700727 int fd = winch->fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 int err;
729 char c;
730
Al Viro7cf3cf22011-09-14 16:21:31 -0700731 if (fd != -1) {
732 err = generic_read(fd, &c, NULL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700733 if (err < 0) {
734 if (err != -EAGAIN) {
Al Viro7cf3cf22011-09-14 16:21:31 -0700735 winch->fd = -1;
736 list_del(&winch->list);
737 os_close_file(fd);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700738 printk(KERN_ERR "winch_interrupt : "
739 "read failed, errno = %d\n", -err);
740 printk(KERN_ERR "fd %d is losing SIGWINCH "
741 "support\n", winch->tty_fd);
Al Viro7cf3cf22011-09-14 16:21:31 -0700742 INIT_WORK(&winch->work, __free_winch);
743 schedule_work(&winch->work);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700744 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 goto out;
747 }
748 }
Jeff Dike42a359e2007-07-15 23:38:55 -0700749 tty = winch->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (tty != NULL) {
751 line = tty->driver_data;
Jeff Dike438ee672008-02-04 22:31:19 -0800752 if (line != NULL) {
Al Virobed5e392011-09-08 10:49:34 -0400753 chan_window_size(line, &tty->winsize.ws_row,
Jeff Dike438ee672008-02-04 22:31:19 -0800754 &tty->winsize.ws_col);
755 kill_pgrp(tty->pgrp, SIGWINCH, 1);
756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 out:
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700759 if (winch->fd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 reactivate_fd(winch->fd, WINCH_IRQ);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700761 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Jeff Dike42a359e2007-07-15 23:38:55 -0700764void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
765 unsigned long stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 struct winch *winch;
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
770 if (winch == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700771 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700772 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
776 .fd = fd,
777 .tty_fd = tty_fd,
778 .pid = pid,
Jeff Dike42a359e2007-07-15 23:38:55 -0700779 .tty = tty,
780 .stack = stack });
781
782 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
Yong Zhangc0b79a92011-09-22 16:58:46 +0800783 IRQF_SHARED | IRQF_SAMPLE_RANDOM,
Jeff Dike42a359e2007-07-15 23:38:55 -0700784 "winch", winch) < 0) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700785 printk(KERN_ERR "register_winch_irq - failed to register "
786 "IRQ\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700787 goto out_free;
788 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700789
790 spin_lock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 list_add(&winch->list, &winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700792 spin_unlock(&winch_handler_lock);
793
Jeff Dike42a359e2007-07-15 23:38:55 -0700794 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Jeff Dike42a359e2007-07-15 23:38:55 -0700796 out_free:
Jeff Dikee464bf22006-01-06 00:19:01 -0800797 kfree(winch);
Jeff Dike42a359e2007-07-15 23:38:55 -0700798 cleanup:
799 os_kill_process(pid, 1);
800 os_close_file(fd);
801 if (stack != 0)
802 free_stack(stack, 0);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700803}
804
Jeff Dikee464bf22006-01-06 00:19:01 -0800805static void unregister_winch(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Will Newton48a0b742011-01-12 16:59:26 -0800807 struct list_head *ele, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct winch *winch;
809
Jeff Dikee464bf22006-01-06 00:19:01 -0800810 spin_lock(&winch_handler_lock);
811
Will Newton48a0b742011-01-12 16:59:26 -0800812 list_for_each_safe(ele, next, &winch_handlers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 winch = list_entry(ele, struct winch, list);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700814 if (winch->tty == tty) {
Al Viro7cf3cf22011-09-14 16:21:31 -0700815 free_winch(winch);
Jeff Dikee464bf22006-01-06 00:19:01 -0800816 break;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700817 }
818 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800819 spin_unlock(&winch_handler_lock);
820}
821
822static void winch_cleanup(void)
823{
824 struct list_head *ele, *next;
825 struct winch *winch;
826
827 spin_lock(&winch_handler_lock);
828
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700829 list_for_each_safe(ele, next, &winch_handlers) {
Jeff Dikee464bf22006-01-06 00:19:01 -0800830 winch = list_entry(ele, struct winch, list);
Al Viro7cf3cf22011-09-14 16:21:31 -0700831 free_winch(winch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800833
834 spin_unlock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836__uml_exitcall(winch_cleanup);
837
838char *add_xterm_umid(char *base)
839{
840 char *umid, *title;
841 int len;
842
Jeff Dike7eebe8a2006-01-06 00:19:01 -0800843 umid = get_umid();
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700844 if (*umid == '\0')
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700845 return base;
Jeff Dike165dc592006-01-06 00:18:57 -0800846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
848 title = kmalloc(len, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700849 if (title == NULL) {
850 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700851 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
854 snprintf(title, len, "%s (%s)", base, umid);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700855 return title;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}