blob: cf8a97f3451856665b11fdb6172b67953d6ee513 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include "chan_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "irq_kern.h"
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070011#include "irq_user.h"
Jeff Dikeedea1382008-02-04 22:30:46 -080012#include "kern_util.h"
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070013#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#define LINE_BUFSIZE 4096
16
Al Viro7bea96f2006-10-08 22:49:34 +010017static irqreturn_t line_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018{
Jeff Dike165dc592006-01-06 00:18:57 -080019 struct chan *chan = data;
20 struct line *line = chan->line;
21 struct tty_struct *tty = line->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23 if (line)
24 chan_interrupt(&line->chan_list, &line->task, tty, irq);
25 return IRQ_HANDLED;
26}
27
Andrew Mortona2ce7742006-12-06 20:31:36 -080028static void line_timer_cb(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Andrew Mortona2ce7742006-12-06 20:31:36 -080030 struct line *line = container_of(work, struct line, task.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070032 if (!line->throttled)
Jeff Dikee4dcee82006-01-06 00:18:58 -080033 chan_interrupt(&line->chan_list, &line->task, line->tty,
34 line->driver->read_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070037/*
38 * Returns the free space inside the ring buffer of this line.
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070039 *
Simon Arlottb60745b2007-10-20 01:23:03 +020040 * Should be called while holding line->lock (this does not modify data).
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070041 */
42static int write_room(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 int n;
45
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070046 if (line->buffer == NULL)
47 return LINE_BUFSIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070049 /* This is for the case where the buffer is wrapped! */
50 n = line->head - line->tail;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (n <= 0)
Jeff Dikeacb2cf32008-02-04 22:30:42 -080053 n += LINE_BUFSIZE; /* The other case */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070054 return n - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070057int line_write_room(struct tty_struct *tty)
58{
59 struct line *line = tty->driver_data;
60 unsigned long flags;
61 int room;
62
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070063 spin_lock_irqsave(&line->lock, flags);
64 room = write_room(line);
65 spin_unlock_irqrestore(&line->lock, flags);
66
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070067 return room;
68}
69
70int line_chars_in_buffer(struct tty_struct *tty)
71{
72 struct line *line = tty->driver_data;
73 unsigned long flags;
74 int ret;
75
76 spin_lock_irqsave(&line->lock, flags);
Jeff Dikeacb2cf32008-02-04 22:30:42 -080077 /* write_room subtracts 1 for the needed NULL, so we readd it.*/
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070078 ret = LINE_BUFSIZE - (write_room(line) + 1);
79 spin_unlock_irqrestore(&line->lock, flags);
80
81 return ret;
82}
83
84/*
85 * This copies the content of buf into the circular buffer associated with
86 * this line.
87 * The return value is the number of characters actually copied, i.e. the ones
88 * for which there was space: this function is not supposed to ever flush out
89 * the circular buffer.
90 *
91 * Must be called while holding line->lock!
92 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static int buffer_data(struct line *line, const char *buf, int len)
94{
95 int end, room;
96
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070097 if (line->buffer == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
99 if (line->buffer == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700100 printk(KERN_ERR "buffer_data - atomic allocation "
101 "failed\n");
102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104 line->head = line->buffer;
105 line->tail = line->buffer;
106 }
107
108 room = write_room(line);
109 len = (len > room) ? room : len;
110
111 end = line->buffer + LINE_BUFSIZE - line->tail;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700112
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700113 if (len < end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 memcpy(line->tail, buf, len);
115 line->tail += len;
Jeff Diked50084a2006-01-06 00:18:50 -0800116 }
117 else {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700118 /* The circular buffer is wrapping */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 memcpy(line->tail, buf, end);
120 buf += end;
121 memcpy(line->buffer, buf, len - end);
122 line->tail = line->buffer + len - end;
123 }
124
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700125 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700128/*
129 * Flushes the ring buffer to the output channels. That is, write_chan is
130 * called, passing it line->head as buffer, and an appropriate count.
131 *
132 * On exit, returns 1 when the buffer is empty,
133 * 0 when the buffer is not empty on exit,
134 * and -errno when an error occurred.
135 *
136 * Must be called while holding line->lock!*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static int flush_buffer(struct line *line)
138{
139 int n, count;
140
141 if ((line->buffer == NULL) || (line->head == line->tail))
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700142 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 if (line->tail < line->head) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700145 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 count = line->buffer + LINE_BUFSIZE - line->head;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 n = write_chan(&line->chan_list, line->head, count,
149 line->driver->write_irq);
150 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700151 return n;
152 if (n == count) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700153 /*
154 * We have flushed from ->head to buffer end, now we
155 * must flush only from the beginning to ->tail.
156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 line->head = line->buffer;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700158 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162 }
163
164 count = line->tail - line->head;
Jeff Diked50084a2006-01-06 00:18:50 -0800165 n = write_chan(&line->chan_list, line->head, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700167
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700168 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700169 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700172 return line->head == line->tail;
173}
174
175void line_flush_buffer(struct tty_struct *tty)
176{
177 struct line *line = tty->driver_data;
178 unsigned long flags;
179 int err;
180
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700181 spin_lock_irqsave(&line->lock, flags);
182 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700183 spin_unlock_irqrestore(&line->lock, flags);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700184}
185
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700186/*
187 * We map both ->flush_chars and ->put_char (which go in pair) onto
188 * ->flush_buffer and ->write. Hope it's not that bad.
189 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700190void line_flush_chars(struct tty_struct *tty)
191{
192 line_flush_buffer(tty);
193}
194
WANG Cong3168cb92008-05-06 20:42:33 -0700195int line_put_char(struct tty_struct *tty, unsigned char ch)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700196{
WANG Cong3168cb92008-05-06 20:42:33 -0700197 return line_write(tty, &ch, sizeof(ch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
201{
202 struct line *line = tty->driver_data;
203 unsigned long flags;
Jeff Dikec59dbca2007-10-16 01:26:42 -0700204 int n, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700206 spin_lock_irqsave(&line->lock, flags);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700207 if (line->head != line->tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 ret = buffer_data(line, buf, len);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700209 else {
Jeff Diked50084a2006-01-06 00:18:50 -0800210 n = write_chan(&line->chan_list, buf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700212 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 ret = n;
214 goto out_up;
215 }
216
217 len -= n;
218 ret += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700219 if (len > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 ret += buffer_data(line, buf + n, len);
221 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700222out_up:
223 spin_unlock_irqrestore(&line->lock, flags);
224 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Alan Cox606d0992006-12-08 02:38:45 -0800227void line_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 /* nothing */
230}
231
Jeff Dike5e7672e2006-09-27 01:50:33 -0700232static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 int cmd;
234 char *level;
235 char *name;
236} tty_ioctls[] = {
237 /* don't print these, they flood the log ... */
238 { TCGETS, NULL, "TCGETS" },
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700239 { TCSETS, NULL, "TCSETS" },
240 { TCSETSW, NULL, "TCSETSW" },
241 { TCFLSH, NULL, "TCFLSH" },
242 { TCSBRK, NULL, "TCSBRK" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 /* general tty stuff */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700245 { TCSETSF, KERN_DEBUG, "TCSETSF" },
246 { TCGETA, KERN_DEBUG, "TCGETA" },
247 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
248 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
249 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /* linux-specific ones */
252 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
253 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
254 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
255 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
256};
257
258int line_ioctl(struct tty_struct *tty, struct file * file,
259 unsigned int cmd, unsigned long arg)
260{
261 int ret;
262 int i;
263
264 ret = 0;
265 switch(cmd) {
266#ifdef TIOCGETP
267 case TIOCGETP:
268 case TIOCSETP:
269 case TIOCSETN:
270#endif
271#ifdef TIOCGETC
272 case TIOCGETC:
273 case TIOCSETC:
274#endif
275#ifdef TIOCGLTC
276 case TIOCGLTC:
277 case TIOCSLTC:
278#endif
Alan Coxc564b6f2008-10-13 10:36:49 +0100279 /* Note: these are out of date as we now have TCGETS2 etc but this
280 whole lot should probably go away */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 case TCGETS:
282 case TCSETSF:
283 case TCSETSW:
284 case TCSETS:
285 case TCGETA:
286 case TCSETAF:
287 case TCSETAW:
288 case TCSETA:
289 case TCXONC:
290 case TCFLSH:
291 case TIOCOUTQ:
292 case TIOCINQ:
293 case TIOCGLCKTRMIOS:
294 case TIOCSLCKTRMIOS:
295 case TIOCPKT:
296 case TIOCGSOFTCAR:
297 case TIOCSSOFTCAR:
298 return -ENOIOCTLCMD;
299#if 0
300 case TCwhatever:
301 /* do something */
302 break;
303#endif
304 default:
305 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
306 if (cmd == tty_ioctls[i].cmd)
307 break;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700308 if (i == ARRAY_SIZE(tty_ioctls)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
Harvey Harrison35957262008-04-28 02:13:53 -0700310 __func__, tty->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312 ret = -ENOIOCTLCMD;
313 break;
314 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700315 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Jeff Dikee4dcee82006-01-06 00:18:58 -0800318void line_throttle(struct tty_struct *tty)
319{
320 struct line *line = tty->driver_data;
321
322 deactivate_chan(&line->chan_list, line->driver->read_irq);
323 line->throttled = 1;
324}
325
326void line_unthrottle(struct tty_struct *tty)
327{
328 struct line *line = tty->driver_data;
329
330 line->throttled = 0;
331 chan_interrupt(&line->chan_list, &line->task, tty,
332 line->driver->read_irq);
333
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700334 /*
335 * Maybe there is enough stuff pending that calling the interrupt
Jeff Dikee4dcee82006-01-06 00:18:58 -0800336 * throttles us again. In this case, line->throttled will be 1
337 * again and we shouldn't turn the interrupt back on.
338 */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700339 if (!line->throttled)
Jeff Dikee4dcee82006-01-06 00:18:58 -0800340 reactivate_chan(&line->chan_list, line->driver->read_irq);
341}
342
Al Viro7bea96f2006-10-08 22:49:34 +0100343static irqreturn_t line_write_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Jeff Dike165dc592006-01-06 00:18:57 -0800345 struct chan *chan = data;
346 struct line *line = chan->line;
347 struct tty_struct *tty = line->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int err;
349
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700350 /*
351 * Interrupts are disabled here because we registered the interrupt with
352 * IRQF_DISABLED (see line_setup_irq).
353 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700354
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800355 spin_lock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700357 if (err == 0) {
358 return IRQ_NONE;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700359 } else if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 line->head = line->buffer;
361 line->tail = line->buffer;
362 }
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800363 spin_unlock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700365 if (tty == NULL)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700366 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
WANG Cong06ac6672008-07-29 22:33:34 -0700368 tty_wakeup(tty);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700369 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Jeff Dike165dc592006-01-06 00:18:57 -0800372int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700374 const struct line_driver *driver = line->driver;
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700375 int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700377 if (input)
378 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
Jeff Diked50084a2006-01-06 00:18:50 -0800379 line_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800380 driver->read_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700381 if (err)
382 return err;
383 if (output)
384 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
Jeff Diked50084a2006-01-06 00:18:50 -0800385 line_write_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800386 driver->write_irq_name, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 line->have_irq = 1;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700388 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700391/*
392 * Normally, a driver like this can rely mostly on the tty layer
Jeff Diked79a5802007-02-10 01:43:52 -0800393 * locking, particularly when it comes to the driver structure.
394 * However, in this case, mconsole requests can come in "from the
395 * side", and race with opens and closes.
396 *
Jeff Dikec6256c62007-02-10 01:44:08 -0800397 * mconsole config requests will want to be sure the device isn't in
398 * use, and get_config, open, and close will want a stable
399 * configuration. The checking and modification of the configuration
400 * is done under a spinlock. Checking whether the device is in use is
401 * line->tty->count > 1, also under the spinlock.
Jeff Diked79a5802007-02-10 01:43:52 -0800402 *
Jeff Dikec6256c62007-02-10 01:44:08 -0800403 * tty->count serves to decide whether the device should be enabled or
404 * disabled on the host. If it's equal to 1, then we are doing the
405 * first open or last close. Otherwise, open and close just return.
Jeff Diked79a5802007-02-10 01:43:52 -0800406 */
407
Jeff Dike1f801712006-01-06 00:18:55 -0800408int line_open(struct line *lines, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Jeff Diked79a5802007-02-10 01:43:52 -0800410 struct line *line = &lines[tty->index];
Jeff Dike165dc592006-01-06 00:18:57 -0800411 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Jeff Diked79a5802007-02-10 01:43:52 -0800413 spin_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700414 if (!line->valid)
Jeff Diked79a5802007-02-10 01:43:52 -0800415 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Jeff Diked79a5802007-02-10 01:43:52 -0800417 err = 0;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700418 if (tty->count > 1)
Jeff Diked79a5802007-02-10 01:43:52 -0800419 goto out_unlock;
420
Jeff Diked79a5802007-02-10 01:43:52 -0800421 spin_unlock(&line->count_lock);
Jeff Dike165dc592006-01-06 00:18:57 -0800422
423 tty->driver_data = line;
424 line->tty = tty;
Jeff Dike165dc592006-01-06 00:18:57 -0800425
Jeff Diked14ad812007-07-15 23:38:54 -0700426 err = enable_chan(line);
427 if (err)
428 return err;
429
Jeff Diked79a5802007-02-10 01:43:52 -0800430 INIT_DELAYED_WORK(&line->task, line_timer_cb);
Jeff Dike165dc592006-01-06 00:18:57 -0800431
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700432 if (!line->sigio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800433 chan_enable_winch(&line->chan_list, tty);
434 line->sigio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
Jeff Diked79a5802007-02-10 01:43:52 -0800437 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
438 &tty->winsize.ws_col);
439
Jeff Diked79a5802007-02-10 01:43:52 -0800440 return err;
441
442out_unlock:
443 spin_unlock(&line->count_lock);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700444 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700447static void unregister_winch(struct tty_struct *tty);
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449void line_close(struct tty_struct *tty, struct file * filp)
450{
451 struct line *line = tty->driver_data;
452
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700453 /*
454 * If line_open fails (and tty->driver_data is never set),
Jeff Diked79a5802007-02-10 01:43:52 -0800455 * tty_open will call line_close. So just return in this case.
456 */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700457 if (line == NULL)
Jeff Diked79a5802007-02-10 01:43:52 -0800458 return;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700459
460 /* We ignore the error anyway! */
461 flush_buffer(line);
462
Jeff Diked79a5802007-02-10 01:43:52 -0800463 spin_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700464 if (!line->valid)
Jeff Diked79a5802007-02-10 01:43:52 -0800465 goto out_unlock;
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700466
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700467 if (tty->count > 1)
Jeff Diked79a5802007-02-10 01:43:52 -0800468 goto out_unlock;
469
Jeff Diked79a5802007-02-10 01:43:52 -0800470 spin_unlock(&line->count_lock);
471
472 line->tty = NULL;
473 tty->driver_data = NULL;
474
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700475 if (line->sigio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800476 unregister_winch(tty);
477 line->sigio = 0;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700478 }
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700479
Jeff Diked79a5802007-02-10 01:43:52 -0800480 return;
481
482out_unlock:
483 spin_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486void close_lines(struct line *lines, int nlines)
487{
488 int i;
489
490 for(i = 0; i < nlines; i++)
Jeff Dike165dc592006-01-06 00:18:57 -0800491 close_chan(&lines[i].chan_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Jeff Dikef28169d2007-02-10 01:43:53 -0800494static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
495 char **error_out)
Jeff Diked79a5802007-02-10 01:43:52 -0800496{
497 struct line *line = &lines[n];
Jeff Dikef28169d2007-02-10 01:43:53 -0800498 int err = -EINVAL;
Jeff Diked79a5802007-02-10 01:43:52 -0800499
500 spin_lock(&line->count_lock);
501
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700502 if (line->tty != NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800503 *error_out = "Device is already open";
Jeff Diked79a5802007-02-10 01:43:52 -0800504 goto out;
505 }
506
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700507 if (line->init_pri <= init_prio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800508 line->init_pri = init_prio;
509 if (!strcmp(init, "none"))
510 line->valid = 0;
511 else {
512 line->init_str = init;
513 line->valid = 1;
514 }
515 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800516 err = 0;
Jeff Diked79a5802007-02-10 01:43:52 -0800517out:
518 spin_unlock(&line->count_lock);
Jeff Dikef28169d2007-02-10 01:43:53 -0800519 return err;
Jeff Diked79a5802007-02-10 01:43:52 -0800520}
521
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700522/*
523 * Common setup code for both startup command line and mconsole initialization.
Matt LaPlante4b3f6862006-10-03 22:21:02 +0200524 * @lines contains the array (of size @num) to modify;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700525 * @init is the setup string;
Jeff Dikef28169d2007-02-10 01:43:53 -0800526 * @error_out is an error string in the case of failure;
Jeff Diked571cd12006-01-06 00:18:53 -0800527 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700528
Jeff Dikef28169d2007-02-10 01:43:53 -0800529int line_setup(struct line *lines, unsigned int num, char *init,
530 char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Jeff Dikef28169d2007-02-10 01:43:53 -0800532 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 char *end;
534
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700535 if (*init == '=') {
536 /*
537 * We said con=/ssl= instead of con#=, so we are configuring all
538 * consoles at once.
539 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700540 n = -1;
Jeff Diked50084a2006-01-06 00:18:50 -0800541 }
542 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 n = simple_strtoul(init, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700544 if (*end != '=') {
Jeff Dikef28169d2007-02-10 01:43:53 -0800545 *error_out = "Couldn't parse device number";
546 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 init = end;
549 }
550 init++;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700551
552 if (n >= (signed int) num) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800553 *error_out = "Device number out of range";
554 return -EINVAL;
Jeff Diked50084a2006-01-06 00:18:50 -0800555 }
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700556 else if (n >= 0) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800557 err = setup_one_line(lines, n, init, INIT_ONE, error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700558 if (err)
Jeff Dikef28169d2007-02-10 01:43:53 -0800559 return err;
560 }
Jeff Diked50084a2006-01-06 00:18:50 -0800561 else {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700562 for(i = 0; i < num; i++) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800563 err = setup_one_line(lines, i, init, INIT_ALL,
564 error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700565 if (err)
Jeff Dikef28169d2007-02-10 01:43:53 -0800566 return err;
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Jeff Dike418e55d2006-01-06 00:18:54 -0800569 return n == -1 ? num : n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Jeff Dike1f801712006-01-06 00:18:55 -0800572int line_config(struct line *lines, unsigned int num, char *str,
Jeff Dikef28169d2007-02-10 01:43:53 -0800573 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Jeff Dike1f801712006-01-06 00:18:55 -0800575 struct line *line;
Jeff Dike970d6e32006-01-06 00:18:48 -0800576 char *new;
Jeff Dike418e55d2006-01-06 00:18:54 -0800577 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700579 if (*str == '=') {
Jeff Dikef28169d2007-02-10 01:43:53 -0800580 *error_out = "Can't configure all devices from mconsole";
581 return -EINVAL;
Jeff Diked571cd12006-01-06 00:18:53 -0800582 }
583
Jeff Dike970d6e32006-01-06 00:18:48 -0800584 new = kstrdup(str, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700585 if (new == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800586 *error_out = "Failed to allocate memory";
587 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800589 n = line_setup(lines, num, new, error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700590 if (n < 0)
Jeff Dikef28169d2007-02-10 01:43:53 -0800591 return n;
Jeff Dike1f801712006-01-06 00:18:55 -0800592
593 line = &lines[n];
Jeff Dikef28169d2007-02-10 01:43:53 -0800594 return parse_chan_pair(line->init_str, line, n, opts, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700597int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 int size, char **error_out)
599{
600 struct line *line;
601 char *end;
602 int dev, n = 0;
603
604 dev = simple_strtoul(name, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700605 if ((*end != '\0') || (end == name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 *error_out = "line_get_config failed to parse device number";
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700610 if ((dev < 0) || (dev >= num)) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700611 *error_out = "device number out of range";
612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
615 line = &lines[dev];
616
Jeff Diked79a5802007-02-10 01:43:52 -0800617 spin_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700618 if (!line->valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700620 else if (line->tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 CONFIG_CHUNK(str, size, n, line->init_str, 1);
622 else n = chan_config_string(&line->chan_list, str, size, error_out);
Jeff Diked79a5802007-02-10 01:43:52 -0800623 spin_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700625 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Jeff Dike29d56cf2005-06-25 14:55:25 -0700628int line_id(char **str, int *start_out, int *end_out)
629{
630 char *end;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700631 int n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700632
633 n = simple_strtoul(*str, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700634 if ((*end != '\0') || (end == *str))
635 return -1;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700636
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700637 *str = end;
638 *start_out = n;
639 *end_out = n;
640 return n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700641}
642
Jeff Dikef28169d2007-02-10 01:43:53 -0800643int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Jeff Dike418e55d2006-01-06 00:18:54 -0800645 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 char config[sizeof("conxxxx=none\0")];
647
Jeff Dike29d56cf2005-06-25 14:55:25 -0700648 sprintf(config, "%d=none", n);
Jeff Dikef28169d2007-02-10 01:43:53 -0800649 err = line_setup(lines, num, config, error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700650 if (err >= 0)
Jeff Dike418e55d2006-01-06 00:18:54 -0800651 err = 0;
652 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Jeff Diked5c9ffc2007-02-10 01:44:08 -0800655struct tty_driver *register_lines(struct line_driver *line_driver,
656 const struct tty_operations *ops,
657 struct line *lines, int nlines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 int i;
660 struct tty_driver *driver = alloc_tty_driver(nlines);
661
662 if (!driver)
663 return NULL;
664
665 driver->driver_name = line_driver->name;
666 driver->name = line_driver->device_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 driver->major = line_driver->major;
668 driver->minor_start = line_driver->minor_start;
669 driver->type = line_driver->type;
670 driver->subtype = line_driver->subtype;
671 driver->flags = TTY_DRIVER_REAL_RAW;
672 driver->init_termios = tty_std_termios;
673 tty_set_operations(driver, ops);
674
675 if (tty_register_driver(driver)) {
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);
679 return NULL;
680 }
681
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700682 for(i = 0; i < nlines; i++) {
683 if (!lines[i].valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 tty_unregister_device(driver, i);
685 }
686
687 mconsole_register_dev(&line_driver->mc);
688 return driver;
689}
690
Jeff Dike90107722006-01-06 00:18:54 -0800691static DEFINE_SPINLOCK(winch_handler_lock);
692static LIST_HEAD(winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700693
Jeff Dike1f801712006-01-06 00:18:55 -0800694void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct line *line;
Jeff Dikef28169d2007-02-10 01:43:53 -0800697 char *error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int i;
699
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700700 for(i = 0; i < nlines; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 line = &lines[i];
702 INIT_LIST_HEAD(&line->chan_list);
Jeff Dike90107722006-01-06 00:18:54 -0800703
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700704 if (line->init_str == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800705 continue;
706
707 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700708 if (line->init_str == NULL)
709 printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
Jeff Dike1f801712006-01-06 00:18:55 -0800710
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700711 if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
712 printk(KERN_ERR "parse_chan_pair failed for "
713 "device %d : %s\n", i, error);
Jeff Dike1f801712006-01-06 00:18:55 -0800714 line->valid = 0;
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717}
718
719struct winch {
720 struct list_head list;
721 int fd;
722 int tty_fd;
723 int pid;
724 struct tty_struct *tty;
Jeff Dike42a359e2007-07-15 23:38:55 -0700725 unsigned long stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726};
727
Jeff Dike42a359e2007-07-15 23:38:55 -0700728static void free_winch(struct winch *winch, int free_irq_ok)
729{
730 list_del(&winch->list);
731
732 if (winch->pid != -1)
733 os_kill_process(winch->pid, 1);
734 if (winch->fd != -1)
735 os_close_file(winch->fd);
736 if (winch->stack != 0)
737 free_stack(winch->stack, 0);
738 if (free_irq_ok)
739 free_irq(WINCH_IRQ, winch);
740 kfree(winch);
741}
742
Al Viro7bea96f2006-10-08 22:49:34 +0100743static irqreturn_t winch_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct winch *winch = data;
746 struct tty_struct *tty;
747 struct line *line;
748 int err;
749 char c;
750
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700751 if (winch->fd != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 err = generic_read(winch->fd, &c, NULL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700753 if (err < 0) {
754 if (err != -EAGAIN) {
755 printk(KERN_ERR "winch_interrupt : "
756 "read failed, errno = %d\n", -err);
757 printk(KERN_ERR "fd %d is losing SIGWINCH "
758 "support\n", winch->tty_fd);
Jeff Dike42a359e2007-07-15 23:38:55 -0700759 free_winch(winch, 0);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700760 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762 goto out;
763 }
764 }
Jeff Dike42a359e2007-07-15 23:38:55 -0700765 tty = winch->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (tty != NULL) {
767 line = tty->driver_data;
Jeff Dike438ee672008-02-04 22:31:19 -0800768 if (line != NULL) {
769 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
770 &tty->winsize.ws_col);
771 kill_pgrp(tty->pgrp, SIGWINCH, 1);
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774 out:
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700775 if (winch->fd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 reactivate_fd(winch->fd, WINCH_IRQ);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700777 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Jeff Dike42a359e2007-07-15 23:38:55 -0700780void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
781 unsigned long stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 struct winch *winch;
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
786 if (winch == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700787 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700788 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
792 .fd = fd,
793 .tty_fd = tty_fd,
794 .pid = pid,
Jeff Dike42a359e2007-07-15 23:38:55 -0700795 .tty = tty,
796 .stack = stack });
797
798 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
799 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
800 "winch", winch) < 0) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700801 printk(KERN_ERR "register_winch_irq - failed to register "
802 "IRQ\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700803 goto out_free;
804 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700805
806 spin_lock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 list_add(&winch->list, &winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700808 spin_unlock(&winch_handler_lock);
809
Jeff Dike42a359e2007-07-15 23:38:55 -0700810 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Jeff Dike42a359e2007-07-15 23:38:55 -0700812 out_free:
Jeff Dikee464bf22006-01-06 00:19:01 -0800813 kfree(winch);
Jeff Dike42a359e2007-07-15 23:38:55 -0700814 cleanup:
815 os_kill_process(pid, 1);
816 os_close_file(fd);
817 if (stack != 0)
818 free_stack(stack, 0);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700819}
820
Jeff Dikee464bf22006-01-06 00:19:01 -0800821static void unregister_winch(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 struct list_head *ele;
824 struct winch *winch;
825
Jeff Dikee464bf22006-01-06 00:19:01 -0800826 spin_lock(&winch_handler_lock);
827
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700828 list_for_each(ele, &winch_handlers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 winch = list_entry(ele, struct winch, list);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700830 if (winch->tty == tty) {
Jeff Dike42a359e2007-07-15 23:38:55 -0700831 free_winch(winch, 1);
Jeff Dikee464bf22006-01-06 00:19:01 -0800832 break;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700833 }
834 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800835 spin_unlock(&winch_handler_lock);
836}
837
838static void winch_cleanup(void)
839{
840 struct list_head *ele, *next;
841 struct winch *winch;
842
843 spin_lock(&winch_handler_lock);
844
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700845 list_for_each_safe(ele, next, &winch_handlers) {
Jeff Dikee464bf22006-01-06 00:19:01 -0800846 winch = list_entry(ele, struct winch, list);
Jeff Dike42a359e2007-07-15 23:38:55 -0700847 free_winch(winch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800849
850 spin_unlock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852__uml_exitcall(winch_cleanup);
853
854char *add_xterm_umid(char *base)
855{
856 char *umid, *title;
857 int len;
858
Jeff Dike7eebe8a2006-01-06 00:19:01 -0800859 umid = get_umid();
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700860 if (*umid == '\0')
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700861 return base;
Jeff Dike165dc592006-01-06 00:18:57 -0800862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
864 title = kmalloc(len, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700865 if (title == NULL) {
866 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700867 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
870 snprintf(title, len, "%s (%s)", base, umid);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700871 return title;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}