blob: 7a656bd8bd3c185f9fe324f6bd5a8a9bc2f533d2 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "chan_kern.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;
Alexander Beregalov03315b52010-03-05 13:42:33 -080022 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24 if (line)
Alexander Beregalov03315b52010-03-05 13:42:33 -080025 chan_interrupt(&line->chan_list, &line->task, line->tty, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 return IRQ_HANDLED;
27}
28
Andrew Mortona2ce7742006-12-06 20:31:36 -080029static void line_timer_cb(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Andrew Mortona2ce7742006-12-06 20:31:36 -080031 struct line *line = container_of(work, struct line, task.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070033 if (!line->throttled)
Jeff Dikee4dcee82006-01-06 00:18:58 -080034 chan_interrupt(&line->chan_list, &line->task, line->tty,
35 line->driver->read_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070038/*
39 * Returns the free space inside the ring buffer of this line.
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070040 *
Simon Arlottb60745b2007-10-20 01:23:03 +020041 * Should be called while holding line->lock (this does not modify data).
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070042 */
43static int write_room(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 int n;
46
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070047 if (line->buffer == NULL)
48 return LINE_BUFSIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070050 /* This is for the case where the buffer is wrapped! */
51 n = line->head - line->tail;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (n <= 0)
Jeff Dikeacb2cf32008-02-04 22:30:42 -080054 n += LINE_BUFSIZE; /* The other case */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070055 return n - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070058int line_write_room(struct tty_struct *tty)
59{
60 struct line *line = tty->driver_data;
61 unsigned long flags;
62 int room;
63
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070064 spin_lock_irqsave(&line->lock, flags);
65 room = write_room(line);
66 spin_unlock_irqrestore(&line->lock, flags);
67
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070068 return room;
69}
70
71int line_chars_in_buffer(struct tty_struct *tty)
72{
73 struct line *line = tty->driver_data;
74 unsigned long flags;
75 int ret;
76
77 spin_lock_irqsave(&line->lock, flags);
Jeff Dikeacb2cf32008-02-04 22:30:42 -080078 /* write_room subtracts 1 for the needed NULL, so we readd it.*/
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070079 ret = LINE_BUFSIZE - (write_room(line) + 1);
80 spin_unlock_irqrestore(&line->lock, flags);
81
82 return ret;
83}
84
85/*
86 * This copies the content of buf into the circular buffer associated with
87 * this line.
88 * The return value is the number of characters actually copied, i.e. the ones
89 * for which there was space: this function is not supposed to ever flush out
90 * the circular buffer.
91 *
92 * Must be called while holding line->lock!
93 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int buffer_data(struct line *line, const char *buf, int len)
95{
96 int end, room;
97
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070098 if (line->buffer == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
100 if (line->buffer == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700101 printk(KERN_ERR "buffer_data - atomic allocation "
102 "failed\n");
103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105 line->head = line->buffer;
106 line->tail = line->buffer;
107 }
108
109 room = write_room(line);
110 len = (len > room) ? room : len;
111
112 end = line->buffer + LINE_BUFSIZE - line->tail;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700113
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700114 if (len < end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 memcpy(line->tail, buf, len);
116 line->tail += len;
Jeff Diked50084a2006-01-06 00:18:50 -0800117 }
118 else {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700119 /* The circular buffer is wrapping */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 memcpy(line->tail, buf, end);
121 buf += end;
122 memcpy(line->buffer, buf, len - end);
123 line->tail = line->buffer + len - end;
124 }
125
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700126 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700129/*
130 * Flushes the ring buffer to the output channels. That is, write_chan is
131 * called, passing it line->head as buffer, and an appropriate count.
132 *
133 * On exit, returns 1 when the buffer is empty,
134 * 0 when the buffer is not empty on exit,
135 * and -errno when an error occurred.
136 *
137 * Must be called while holding line->lock!*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static int flush_buffer(struct line *line)
139{
140 int n, count;
141
142 if ((line->buffer == NULL) || (line->head == line->tail))
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700143 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 if (line->tail < line->head) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700146 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 count = line->buffer + LINE_BUFSIZE - line->head;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 n = write_chan(&line->chan_list, line->head, count,
150 line->driver->write_irq);
151 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700152 return n;
153 if (n == count) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700154 /*
155 * We have flushed from ->head to buffer end, now we
156 * must flush only from the beginning to ->tail.
157 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 line->head = line->buffer;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700159 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163 }
164
165 count = line->tail - line->head;
Jeff Diked50084a2006-01-06 00:18:50 -0800166 n = write_chan(&line->chan_list, line->head, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700168
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700169 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700170 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700173 return line->head == line->tail;
174}
175
176void line_flush_buffer(struct tty_struct *tty)
177{
178 struct line *line = tty->driver_data;
179 unsigned long flags;
180 int err;
181
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700182 spin_lock_irqsave(&line->lock, flags);
183 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700184 spin_unlock_irqrestore(&line->lock, flags);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700185}
186
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700187/*
188 * We map both ->flush_chars and ->put_char (which go in pair) onto
189 * ->flush_buffer and ->write. Hope it's not that bad.
190 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700191void line_flush_chars(struct tty_struct *tty)
192{
193 line_flush_buffer(tty);
194}
195
WANG Cong3168cb92008-05-06 20:42:33 -0700196int line_put_char(struct tty_struct *tty, unsigned char ch)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700197{
WANG Cong3168cb92008-05-06 20:42:33 -0700198 return line_write(tty, &ch, sizeof(ch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
202{
203 struct line *line = tty->driver_data;
204 unsigned long flags;
Jeff Dikec59dbca2007-10-16 01:26:42 -0700205 int n, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700207 spin_lock_irqsave(&line->lock, flags);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700208 if (line->head != line->tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 ret = buffer_data(line, buf, len);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700210 else {
Jeff Diked50084a2006-01-06 00:18:50 -0800211 n = write_chan(&line->chan_list, buf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700213 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 ret = n;
215 goto out_up;
216 }
217
218 len -= n;
219 ret += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700220 if (len > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 ret += buffer_data(line, buf + n, len);
222 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700223out_up:
224 spin_unlock_irqrestore(&line->lock, flags);
225 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Alan Cox606d0992006-12-08 02:38:45 -0800228void line_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 /* nothing */
231}
232
Jeff Dike5e7672e2006-09-27 01:50:33 -0700233static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 int cmd;
235 char *level;
236 char *name;
237} tty_ioctls[] = {
238 /* don't print these, they flood the log ... */
239 { TCGETS, NULL, "TCGETS" },
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700240 { TCSETS, NULL, "TCSETS" },
241 { TCSETSW, NULL, "TCSETSW" },
242 { TCFLSH, NULL, "TCFLSH" },
243 { TCSBRK, NULL, "TCSBRK" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 /* general tty stuff */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700246 { TCSETSF, KERN_DEBUG, "TCSETSF" },
247 { TCGETA, KERN_DEBUG, "TCGETA" },
248 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
249 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
250 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* linux-specific ones */
253 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
254 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
255 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
256 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
257};
258
259int line_ioctl(struct tty_struct *tty, struct file * file,
260 unsigned int cmd, unsigned long arg)
261{
262 int ret;
263 int i;
264
265 ret = 0;
266 switch(cmd) {
267#ifdef TIOCGETP
268 case TIOCGETP:
269 case TIOCSETP:
270 case TIOCSETN:
271#endif
272#ifdef TIOCGETC
273 case TIOCGETC:
274 case TIOCSETC:
275#endif
276#ifdef TIOCGLTC
277 case TIOCGLTC:
278 case TIOCSLTC:
279#endif
Alan Coxc564b6f2008-10-13 10:36:49 +0100280 /* Note: these are out of date as we now have TCGETS2 etc but this
281 whole lot should probably go away */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 case TCGETS:
283 case TCSETSF:
284 case TCSETSW:
285 case TCSETS:
286 case TCGETA:
287 case TCSETAF:
288 case TCSETAW:
289 case TCSETA:
290 case TCXONC:
291 case TCFLSH:
292 case TIOCOUTQ:
293 case TIOCINQ:
294 case TIOCGLCKTRMIOS:
295 case TIOCSLCKTRMIOS:
296 case TIOCPKT:
297 case TIOCGSOFTCAR:
298 case TIOCSSOFTCAR:
299 return -ENOIOCTLCMD;
300#if 0
301 case TCwhatever:
302 /* do something */
303 break;
304#endif
305 default:
306 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
307 if (cmd == tty_ioctls[i].cmd)
308 break;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700309 if (i == ARRAY_SIZE(tty_ioctls)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
Harvey Harrison35957262008-04-28 02:13:53 -0700311 __func__, tty->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313 ret = -ENOIOCTLCMD;
314 break;
315 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700316 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Jeff Dikee4dcee82006-01-06 00:18:58 -0800319void line_throttle(struct tty_struct *tty)
320{
321 struct line *line = tty->driver_data;
322
323 deactivate_chan(&line->chan_list, line->driver->read_irq);
324 line->throttled = 1;
325}
326
327void line_unthrottle(struct tty_struct *tty)
328{
329 struct line *line = tty->driver_data;
330
331 line->throttled = 0;
332 chan_interrupt(&line->chan_list, &line->task, tty,
333 line->driver->read_irq);
334
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700335 /*
336 * Maybe there is enough stuff pending that calling the interrupt
Jeff Dikee4dcee82006-01-06 00:18:58 -0800337 * throttles us again. In this case, line->throttled will be 1
338 * again and we shouldn't turn the interrupt back on.
339 */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700340 if (!line->throttled)
Jeff Dikee4dcee82006-01-06 00:18:58 -0800341 reactivate_chan(&line->chan_list, line->driver->read_irq);
342}
343
Al Viro7bea96f2006-10-08 22:49:34 +0100344static irqreturn_t line_write_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Jeff Dike165dc592006-01-06 00:18:57 -0800346 struct chan *chan = data;
347 struct line *line = chan->line;
348 struct tty_struct *tty = line->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int err;
350
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700351 /*
352 * Interrupts are disabled here because we registered the interrupt with
353 * IRQF_DISABLED (see line_setup_irq).
354 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700355
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800356 spin_lock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700358 if (err == 0) {
359 return IRQ_NONE;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700360 } else if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 line->head = line->buffer;
362 line->tail = line->buffer;
363 }
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800364 spin_unlock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700366 if (tty == NULL)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700367 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
WANG Cong06ac6672008-07-29 22:33:34 -0700369 tty_wakeup(tty);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700370 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Jeff Dike165dc592006-01-06 00:18:57 -0800373int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700375 const struct line_driver *driver = line->driver;
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700376 int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700378 if (input)
379 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
Jeff Diked50084a2006-01-06 00:18:50 -0800380 line_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800381 driver->read_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700382 if (err)
383 return err;
384 if (output)
385 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
Jeff Diked50084a2006-01-06 00:18:50 -0800386 line_write_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800387 driver->write_irq_name, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 line->have_irq = 1;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700389 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700392/*
393 * Normally, a driver like this can rely mostly on the tty layer
Jeff Diked79a5802007-02-10 01:43:52 -0800394 * locking, particularly when it comes to the driver structure.
395 * However, in this case, mconsole requests can come in "from the
396 * side", and race with opens and closes.
397 *
Jeff Dikec6256c62007-02-10 01:44:08 -0800398 * mconsole config requests will want to be sure the device isn't in
399 * use, and get_config, open, and close will want a stable
400 * configuration. The checking and modification of the configuration
401 * is done under a spinlock. Checking whether the device is in use is
402 * line->tty->count > 1, also under the spinlock.
Jeff Diked79a5802007-02-10 01:43:52 -0800403 *
Jeff Dikec6256c62007-02-10 01:44:08 -0800404 * tty->count serves to decide whether the device should be enabled or
405 * disabled on the host. If it's equal to 1, then we are doing the
406 * first open or last close. Otherwise, open and close just return.
Jeff Diked79a5802007-02-10 01:43:52 -0800407 */
408
Jeff Dike1f801712006-01-06 00:18:55 -0800409int line_open(struct line *lines, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Jeff Diked79a5802007-02-10 01:43:52 -0800411 struct line *line = &lines[tty->index];
Jeff Dike165dc592006-01-06 00:18:57 -0800412 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Jeff Diked79a5802007-02-10 01:43:52 -0800414 spin_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700415 if (!line->valid)
Jeff Diked79a5802007-02-10 01:43:52 -0800416 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Jeff Diked79a5802007-02-10 01:43:52 -0800418 err = 0;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700419 if (tty->count > 1)
Jeff Diked79a5802007-02-10 01:43:52 -0800420 goto out_unlock;
421
Jeff Diked79a5802007-02-10 01:43:52 -0800422 spin_unlock(&line->count_lock);
Jeff Dike165dc592006-01-06 00:18:57 -0800423
424 tty->driver_data = line;
425 line->tty = tty;
Jeff Dike165dc592006-01-06 00:18:57 -0800426
Jeff Diked14ad812007-07-15 23:38:54 -0700427 err = enable_chan(line);
428 if (err)
429 return err;
430
Jeff Diked79a5802007-02-10 01:43:52 -0800431 INIT_DELAYED_WORK(&line->task, line_timer_cb);
Jeff Dike165dc592006-01-06 00:18:57 -0800432
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700433 if (!line->sigio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800434 chan_enable_winch(&line->chan_list, tty);
435 line->sigio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437
Jeff Diked79a5802007-02-10 01:43:52 -0800438 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
439 &tty->winsize.ws_col);
440
Jeff Diked79a5802007-02-10 01:43:52 -0800441 return err;
442
443out_unlock:
444 spin_unlock(&line->count_lock);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700445 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700448static void unregister_winch(struct tty_struct *tty);
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450void line_close(struct tty_struct *tty, struct file * filp)
451{
452 struct line *line = tty->driver_data;
453
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700454 /*
455 * If line_open fails (and tty->driver_data is never set),
Jeff Diked79a5802007-02-10 01:43:52 -0800456 * tty_open will call line_close. So just return in this case.
457 */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700458 if (line == NULL)
Jeff Diked79a5802007-02-10 01:43:52 -0800459 return;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700460
461 /* We ignore the error anyway! */
462 flush_buffer(line);
463
Jeff Diked79a5802007-02-10 01:43:52 -0800464 spin_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700465 if (!line->valid)
Jeff Diked79a5802007-02-10 01:43:52 -0800466 goto out_unlock;
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700467
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700468 if (tty->count > 1)
Jeff Diked79a5802007-02-10 01:43:52 -0800469 goto out_unlock;
470
Jeff Diked79a5802007-02-10 01:43:52 -0800471 spin_unlock(&line->count_lock);
472
473 line->tty = NULL;
474 tty->driver_data = NULL;
475
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700476 if (line->sigio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800477 unregister_winch(tty);
478 line->sigio = 0;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700479 }
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700480
Jeff Diked79a5802007-02-10 01:43:52 -0800481 return;
482
483out_unlock:
484 spin_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487void close_lines(struct line *lines, int nlines)
488{
489 int i;
490
491 for(i = 0; i < nlines; i++)
Jeff Dike165dc592006-01-06 00:18:57 -0800492 close_chan(&lines[i].chan_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Jeff Dikef28169d2007-02-10 01:43:53 -0800495static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
496 char **error_out)
Jeff Diked79a5802007-02-10 01:43:52 -0800497{
498 struct line *line = &lines[n];
Jeff Dikef28169d2007-02-10 01:43:53 -0800499 int err = -EINVAL;
Jeff Diked79a5802007-02-10 01:43:52 -0800500
501 spin_lock(&line->count_lock);
502
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700503 if (line->tty != NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800504 *error_out = "Device is already open";
Jeff Diked79a5802007-02-10 01:43:52 -0800505 goto out;
506 }
507
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700508 if (line->init_pri <= init_prio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800509 line->init_pri = init_prio;
510 if (!strcmp(init, "none"))
511 line->valid = 0;
512 else {
513 line->init_str = init;
514 line->valid = 1;
515 }
516 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800517 err = 0;
Jeff Diked79a5802007-02-10 01:43:52 -0800518out:
519 spin_unlock(&line->count_lock);
Jeff Dikef28169d2007-02-10 01:43:53 -0800520 return err;
Jeff Diked79a5802007-02-10 01:43:52 -0800521}
522
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700523/*
524 * Common setup code for both startup command line and mconsole initialization.
Matt LaPlante4b3f6862006-10-03 22:21:02 +0200525 * @lines contains the array (of size @num) to modify;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700526 * @init is the setup string;
Jeff Dikef28169d2007-02-10 01:43:53 -0800527 * @error_out is an error string in the case of failure;
Jeff Diked571cd12006-01-06 00:18:53 -0800528 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700529
Jeff Dikef28169d2007-02-10 01:43:53 -0800530int line_setup(struct line *lines, unsigned int num, char *init,
531 char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Jeff Dikef28169d2007-02-10 01:43:53 -0800533 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 char *end;
535
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700536 if (*init == '=') {
537 /*
538 * We said con=/ssl= instead of con#=, so we are configuring all
539 * consoles at once.
540 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700541 n = -1;
Jeff Diked50084a2006-01-06 00:18:50 -0800542 }
543 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 n = simple_strtoul(init, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700545 if (*end != '=') {
Jeff Dikef28169d2007-02-10 01:43:53 -0800546 *error_out = "Couldn't parse device number";
547 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 init = end;
550 }
551 init++;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700552
553 if (n >= (signed int) num) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800554 *error_out = "Device number out of range";
555 return -EINVAL;
Jeff Diked50084a2006-01-06 00:18:50 -0800556 }
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700557 else if (n >= 0) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800558 err = setup_one_line(lines, n, init, INIT_ONE, error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700559 if (err)
Jeff Dikef28169d2007-02-10 01:43:53 -0800560 return err;
561 }
Jeff Diked50084a2006-01-06 00:18:50 -0800562 else {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700563 for(i = 0; i < num; i++) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800564 err = setup_one_line(lines, i, init, INIT_ALL,
565 error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700566 if (err)
Jeff Dikef28169d2007-02-10 01:43:53 -0800567 return err;
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
Jeff Dike418e55d2006-01-06 00:18:54 -0800570 return n == -1 ? num : n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Jeff Dike1f801712006-01-06 00:18:55 -0800573int line_config(struct line *lines, unsigned int num, char *str,
Jeff Dikef28169d2007-02-10 01:43:53 -0800574 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Jeff Dike1f801712006-01-06 00:18:55 -0800576 struct line *line;
Jeff Dike970d6e32006-01-06 00:18:48 -0800577 char *new;
Jeff Dike418e55d2006-01-06 00:18:54 -0800578 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700580 if (*str == '=') {
Jeff Dikef28169d2007-02-10 01:43:53 -0800581 *error_out = "Can't configure all devices from mconsole";
582 return -EINVAL;
Jeff Diked571cd12006-01-06 00:18:53 -0800583 }
584
Jeff Dike970d6e32006-01-06 00:18:48 -0800585 new = kstrdup(str, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700586 if (new == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800587 *error_out = "Failed to allocate memory";
588 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800590 n = line_setup(lines, num, new, error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700591 if (n < 0)
Jeff Dikef28169d2007-02-10 01:43:53 -0800592 return n;
Jeff Dike1f801712006-01-06 00:18:55 -0800593
594 line = &lines[n];
Jeff Dikef28169d2007-02-10 01:43:53 -0800595 return parse_chan_pair(line->init_str, line, n, opts, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700598int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 int size, char **error_out)
600{
601 struct line *line;
602 char *end;
603 int dev, n = 0;
604
605 dev = simple_strtoul(name, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700606 if ((*end != '\0') || (end == name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 *error_out = "line_get_config failed to parse device number";
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700611 if ((dev < 0) || (dev >= num)) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700612 *error_out = "device number out of range";
613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615
616 line = &lines[dev];
617
Jeff Diked79a5802007-02-10 01:43:52 -0800618 spin_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700619 if (!line->valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700621 else if (line->tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 CONFIG_CHUNK(str, size, n, line->init_str, 1);
623 else n = chan_config_string(&line->chan_list, str, size, error_out);
Jeff Diked79a5802007-02-10 01:43:52 -0800624 spin_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700626 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Jeff Dike29d56cf2005-06-25 14:55:25 -0700629int line_id(char **str, int *start_out, int *end_out)
630{
631 char *end;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700632 int n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700633
634 n = simple_strtoul(*str, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700635 if ((*end != '\0') || (end == *str))
636 return -1;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700637
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700638 *str = end;
639 *start_out = n;
640 *end_out = n;
641 return n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700642}
643
Jeff Dikef28169d2007-02-10 01:43:53 -0800644int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Jeff Dike418e55d2006-01-06 00:18:54 -0800646 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 char config[sizeof("conxxxx=none\0")];
648
Jeff Dike29d56cf2005-06-25 14:55:25 -0700649 sprintf(config, "%d=none", n);
Jeff Dikef28169d2007-02-10 01:43:53 -0800650 err = line_setup(lines, num, config, error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700651 if (err >= 0)
Jeff Dike418e55d2006-01-06 00:18:54 -0800652 err = 0;
653 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Jeff Diked5c9ffc2007-02-10 01:44:08 -0800656struct tty_driver *register_lines(struct line_driver *line_driver,
657 const struct tty_operations *ops,
658 struct line *lines, int nlines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 int i;
661 struct tty_driver *driver = alloc_tty_driver(nlines);
662
663 if (!driver)
664 return NULL;
665
666 driver->driver_name = line_driver->name;
667 driver->name = line_driver->device_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 driver->major = line_driver->major;
669 driver->minor_start = line_driver->minor_start;
670 driver->type = line_driver->type;
671 driver->subtype = line_driver->subtype;
672 driver->flags = TTY_DRIVER_REAL_RAW;
673 driver->init_termios = tty_std_termios;
674 tty_set_operations(driver, ops);
675
676 if (tty_register_driver(driver)) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700677 printk(KERN_ERR "register_lines : can't register %s driver\n",
678 line_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 put_tty_driver(driver);
680 return NULL;
681 }
682
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700683 for(i = 0; i < nlines; i++) {
684 if (!lines[i].valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 tty_unregister_device(driver, i);
686 }
687
688 mconsole_register_dev(&line_driver->mc);
689 return driver;
690}
691
Jeff Dike90107722006-01-06 00:18:54 -0800692static DEFINE_SPINLOCK(winch_handler_lock);
693static LIST_HEAD(winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700694
Jeff Dike1f801712006-01-06 00:18:55 -0800695void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 struct line *line;
Jeff Dikef28169d2007-02-10 01:43:53 -0800698 char *error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 int i;
700
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700701 for(i = 0; i < nlines; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 line = &lines[i];
703 INIT_LIST_HEAD(&line->chan_list);
Jeff Dike90107722006-01-06 00:18:54 -0800704
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700705 if (line->init_str == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800706 continue;
707
708 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700709 if (line->init_str == NULL)
710 printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
Jeff Dike1f801712006-01-06 00:18:55 -0800711
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700712 if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
713 printk(KERN_ERR "parse_chan_pair failed for "
714 "device %d : %s\n", i, error);
Jeff Dike1f801712006-01-06 00:18:55 -0800715 line->valid = 0;
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718}
719
720struct winch {
721 struct list_head list;
722 int fd;
723 int tty_fd;
724 int pid;
725 struct tty_struct *tty;
Jeff Dike42a359e2007-07-15 23:38:55 -0700726 unsigned long stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727};
728
Jeff Dike42a359e2007-07-15 23:38:55 -0700729static void free_winch(struct winch *winch, int free_irq_ok)
730{
731 list_del(&winch->list);
732
733 if (winch->pid != -1)
734 os_kill_process(winch->pid, 1);
735 if (winch->fd != -1)
736 os_close_file(winch->fd);
737 if (winch->stack != 0)
738 free_stack(winch->stack, 0);
739 if (free_irq_ok)
740 free_irq(WINCH_IRQ, winch);
741 kfree(winch);
742}
743
Al Viro7bea96f2006-10-08 22:49:34 +0100744static irqreturn_t winch_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 struct winch *winch = data;
747 struct tty_struct *tty;
748 struct line *line;
749 int err;
750 char c;
751
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700752 if (winch->fd != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 err = generic_read(winch->fd, &c, NULL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700754 if (err < 0) {
755 if (err != -EAGAIN) {
756 printk(KERN_ERR "winch_interrupt : "
757 "read failed, errno = %d\n", -err);
758 printk(KERN_ERR "fd %d is losing SIGWINCH "
759 "support\n", winch->tty_fd);
Jeff Dike42a359e2007-07-15 23:38:55 -0700760 free_winch(winch, 0);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700761 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763 goto out;
764 }
765 }
Jeff Dike42a359e2007-07-15 23:38:55 -0700766 tty = winch->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (tty != NULL) {
768 line = tty->driver_data;
Jeff Dike438ee672008-02-04 22:31:19 -0800769 if (line != NULL) {
770 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
771 &tty->winsize.ws_col);
772 kill_pgrp(tty->pgrp, SIGWINCH, 1);
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 out:
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700776 if (winch->fd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 reactivate_fd(winch->fd, WINCH_IRQ);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700778 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Jeff Dike42a359e2007-07-15 23:38:55 -0700781void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
782 unsigned long stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 struct winch *winch;
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
787 if (winch == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700788 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700789 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
793 .fd = fd,
794 .tty_fd = tty_fd,
795 .pid = pid,
Jeff Dike42a359e2007-07-15 23:38:55 -0700796 .tty = tty,
797 .stack = stack });
798
799 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
800 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
801 "winch", winch) < 0) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700802 printk(KERN_ERR "register_winch_irq - failed to register "
803 "IRQ\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700804 goto out_free;
805 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700806
807 spin_lock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 list_add(&winch->list, &winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700809 spin_unlock(&winch_handler_lock);
810
Jeff Dike42a359e2007-07-15 23:38:55 -0700811 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Jeff Dike42a359e2007-07-15 23:38:55 -0700813 out_free:
Jeff Dikee464bf22006-01-06 00:19:01 -0800814 kfree(winch);
Jeff Dike42a359e2007-07-15 23:38:55 -0700815 cleanup:
816 os_kill_process(pid, 1);
817 os_close_file(fd);
818 if (stack != 0)
819 free_stack(stack, 0);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700820}
821
Jeff Dikee464bf22006-01-06 00:19:01 -0800822static void unregister_winch(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 struct list_head *ele;
825 struct winch *winch;
826
Jeff Dikee464bf22006-01-06 00:19:01 -0800827 spin_lock(&winch_handler_lock);
828
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700829 list_for_each(ele, &winch_handlers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 winch = list_entry(ele, struct winch, list);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700831 if (winch->tty == tty) {
Jeff Dike42a359e2007-07-15 23:38:55 -0700832 free_winch(winch, 1);
Jeff Dikee464bf22006-01-06 00:19:01 -0800833 break;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700834 }
835 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800836 spin_unlock(&winch_handler_lock);
837}
838
839static void winch_cleanup(void)
840{
841 struct list_head *ele, *next;
842 struct winch *winch;
843
844 spin_lock(&winch_handler_lock);
845
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700846 list_for_each_safe(ele, next, &winch_handlers) {
Jeff Dikee464bf22006-01-06 00:19:01 -0800847 winch = list_entry(ele, struct winch, list);
Jeff Dike42a359e2007-07-15 23:38:55 -0700848 free_winch(winch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800850
851 spin_unlock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853__uml_exitcall(winch_cleanup);
854
855char *add_xterm_umid(char *base)
856{
857 char *umid, *title;
858 int len;
859
Jeff Dike7eebe8a2006-01-06 00:19:01 -0800860 umid = get_umid();
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700861 if (*umid == '\0')
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700862 return base;
Jeff Dike165dc592006-01-06 00:18:57 -0800863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
865 title = kmalloc(len, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700866 if (title == NULL) {
867 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700868 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
871 snprintf(title, len, "%s (%s)", base, umid);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700872 return title;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}