blob: 5cff6536a8435ad0a5a407054c31956285bb7a8b [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include "chan_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include "irq_kern.h"
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070010#include "irq_user.h"
11#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#define LINE_BUFSIZE 4096
14
Al Viro7bea96f2006-10-08 22:49:34 +010015static irqreturn_t line_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016{
Jeff Dike165dc592006-01-06 00:18:57 -080017 struct chan *chan = data;
18 struct line *line = chan->line;
19 struct tty_struct *tty = line->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21 if (line)
22 chan_interrupt(&line->chan_list, &line->task, tty, irq);
23 return IRQ_HANDLED;
24}
25
Andrew Mortona2ce7742006-12-06 20:31:36 -080026static void line_timer_cb(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Andrew Mortona2ce7742006-12-06 20:31:36 -080028 struct line *line = container_of(work, struct line, task.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070030 if (!line->throttled)
Jeff Dikee4dcee82006-01-06 00:18:58 -080031 chan_interrupt(&line->chan_list, &line->task, line->tty,
32 line->driver->read_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070035/*
36 * Returns the free space inside the ring buffer of this line.
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070037 *
Simon Arlottb60745b2007-10-20 01:23:03 +020038 * Should be called while holding line->lock (this does not modify data).
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070039 */
40static int write_room(struct line *line)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 int n;
43
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070044 if (line->buffer == NULL)
45 return LINE_BUFSIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070047 /* This is for the case where the buffer is wrapped! */
48 n = line->head - line->tail;
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (n <= 0)
Jeff Dikeacb2cf32008-02-04 22:30:42 -080051 n += LINE_BUFSIZE; /* The other case */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070052 return n - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070055int line_write_room(struct tty_struct *tty)
56{
57 struct line *line = tty->driver_data;
58 unsigned long flags;
59 int room;
60
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070061 spin_lock_irqsave(&line->lock, flags);
62 room = write_room(line);
63 spin_unlock_irqrestore(&line->lock, flags);
64
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070065 return room;
66}
67
68int line_chars_in_buffer(struct tty_struct *tty)
69{
70 struct line *line = tty->driver_data;
71 unsigned long flags;
72 int ret;
73
74 spin_lock_irqsave(&line->lock, flags);
Jeff Dikeacb2cf32008-02-04 22:30:42 -080075 /* write_room subtracts 1 for the needed NULL, so we readd it.*/
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070076 ret = LINE_BUFSIZE - (write_room(line) + 1);
77 spin_unlock_irqrestore(&line->lock, flags);
78
79 return ret;
80}
81
82/*
83 * This copies the content of buf into the circular buffer associated with
84 * this line.
85 * The return value is the number of characters actually copied, i.e. the ones
86 * for which there was space: this function is not supposed to ever flush out
87 * the circular buffer.
88 *
89 * Must be called while holding line->lock!
90 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int buffer_data(struct line *line, const char *buf, int len)
92{
93 int end, room;
94
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070095 if (line->buffer == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
97 if (line->buffer == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -070098 printk(KERN_ERR "buffer_data - atomic allocation "
99 "failed\n");
100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102 line->head = line->buffer;
103 line->tail = line->buffer;
104 }
105
106 room = write_room(line);
107 len = (len > room) ? room : len;
108
109 end = line->buffer + LINE_BUFSIZE - line->tail;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700110
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700111 if (len < end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 memcpy(line->tail, buf, len);
113 line->tail += len;
Jeff Diked50084a2006-01-06 00:18:50 -0800114 }
115 else {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700116 /* The circular buffer is wrapping */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 memcpy(line->tail, buf, end);
118 buf += end;
119 memcpy(line->buffer, buf, len - end);
120 line->tail = line->buffer + len - end;
121 }
122
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700123 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700126/*
127 * Flushes the ring buffer to the output channels. That is, write_chan is
128 * called, passing it line->head as buffer, and an appropriate count.
129 *
130 * On exit, returns 1 when the buffer is empty,
131 * 0 when the buffer is not empty on exit,
132 * and -errno when an error occurred.
133 *
134 * Must be called while holding line->lock!*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static int flush_buffer(struct line *line)
136{
137 int n, count;
138
139 if ((line->buffer == NULL) || (line->head == line->tail))
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700140 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 if (line->tail < line->head) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700143 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 count = line->buffer + LINE_BUFSIZE - line->head;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 n = write_chan(&line->chan_list, line->head, count,
147 line->driver->write_irq);
148 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700149 return n;
150 if (n == count) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700151 /*
152 * We have flushed from ->head to buffer end, now we
153 * must flush only from the beginning to ->tail.
154 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 line->head = line->buffer;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700156 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160 }
161
162 count = line->tail - line->head;
Jeff Diked50084a2006-01-06 00:18:50 -0800163 n = write_chan(&line->chan_list, line->head, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700165
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700166 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700167 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700170 return line->head == line->tail;
171}
172
173void line_flush_buffer(struct tty_struct *tty)
174{
175 struct line *line = tty->driver_data;
176 unsigned long flags;
177 int err;
178
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700179 spin_lock_irqsave(&line->lock, flags);
180 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700181 spin_unlock_irqrestore(&line->lock, flags);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700182}
183
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700184/*
185 * We map both ->flush_chars and ->put_char (which go in pair) onto
186 * ->flush_buffer and ->write. Hope it's not that bad.
187 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700188void line_flush_chars(struct tty_struct *tty)
189{
190 line_flush_buffer(tty);
191}
192
193void line_put_char(struct tty_struct *tty, unsigned char ch)
194{
195 line_write(tty, &ch, sizeof(ch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
199{
200 struct line *line = tty->driver_data;
201 unsigned long flags;
Jeff Dikec59dbca2007-10-16 01:26:42 -0700202 int n, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700204 spin_lock_irqsave(&line->lock, flags);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700205 if (line->head != line->tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 ret = buffer_data(line, buf, len);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700207 else {
Jeff Diked50084a2006-01-06 00:18:50 -0800208 n = write_chan(&line->chan_list, buf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700210 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 ret = n;
212 goto out_up;
213 }
214
215 len -= n;
216 ret += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700217 if (len > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 ret += buffer_data(line, buf + n, len);
219 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700220out_up:
221 spin_unlock_irqrestore(&line->lock, flags);
222 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Alan Cox606d0992006-12-08 02:38:45 -0800225void line_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 /* nothing */
228}
229
Jeff Dike5e7672e2006-09-27 01:50:33 -0700230static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int cmd;
232 char *level;
233 char *name;
234} tty_ioctls[] = {
235 /* don't print these, they flood the log ... */
236 { TCGETS, NULL, "TCGETS" },
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700237 { TCSETS, NULL, "TCSETS" },
238 { TCSETSW, NULL, "TCSETSW" },
239 { TCFLSH, NULL, "TCFLSH" },
240 { TCSBRK, NULL, "TCSBRK" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* general tty stuff */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700243 { TCSETSF, KERN_DEBUG, "TCSETSF" },
244 { TCGETA, KERN_DEBUG, "TCGETA" },
245 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
246 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
247 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /* linux-specific ones */
250 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
251 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
252 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
253 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
254};
255
256int line_ioctl(struct tty_struct *tty, struct file * file,
257 unsigned int cmd, unsigned long arg)
258{
259 int ret;
260 int i;
261
262 ret = 0;
263 switch(cmd) {
264#ifdef TIOCGETP
265 case TIOCGETP:
266 case TIOCSETP:
267 case TIOCSETN:
268#endif
269#ifdef TIOCGETC
270 case TIOCGETC:
271 case TIOCSETC:
272#endif
273#ifdef TIOCGLTC
274 case TIOCGLTC:
275 case TIOCSLTC:
276#endif
277 case TCGETS:
278 case TCSETSF:
279 case TCSETSW:
280 case TCSETS:
281 case TCGETA:
282 case TCSETAF:
283 case TCSETAW:
284 case TCSETA:
285 case TCXONC:
286 case TCFLSH:
287 case TIOCOUTQ:
288 case TIOCINQ:
289 case TIOCGLCKTRMIOS:
290 case TIOCSLCKTRMIOS:
291 case TIOCPKT:
292 case TIOCGSOFTCAR:
293 case TIOCSSOFTCAR:
294 return -ENOIOCTLCMD;
295#if 0
296 case TCwhatever:
297 /* do something */
298 break;
299#endif
300 default:
301 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
302 if (cmd == tty_ioctls[i].cmd)
303 break;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700304 if (i == ARRAY_SIZE(tty_ioctls)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
306 __FUNCTION__, tty->name, cmd);
307 }
308 ret = -ENOIOCTLCMD;
309 break;
310 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700311 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Jeff Dikee4dcee82006-01-06 00:18:58 -0800314void line_throttle(struct tty_struct *tty)
315{
316 struct line *line = tty->driver_data;
317
318 deactivate_chan(&line->chan_list, line->driver->read_irq);
319 line->throttled = 1;
320}
321
322void line_unthrottle(struct tty_struct *tty)
323{
324 struct line *line = tty->driver_data;
325
326 line->throttled = 0;
327 chan_interrupt(&line->chan_list, &line->task, tty,
328 line->driver->read_irq);
329
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700330 /*
331 * Maybe there is enough stuff pending that calling the interrupt
Jeff Dikee4dcee82006-01-06 00:18:58 -0800332 * throttles us again. In this case, line->throttled will be 1
333 * again and we shouldn't turn the interrupt back on.
334 */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700335 if (!line->throttled)
Jeff Dikee4dcee82006-01-06 00:18:58 -0800336 reactivate_chan(&line->chan_list, line->driver->read_irq);
337}
338
Al Viro7bea96f2006-10-08 22:49:34 +0100339static irqreturn_t line_write_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Jeff Dike165dc592006-01-06 00:18:57 -0800341 struct chan *chan = data;
342 struct line *line = chan->line;
343 struct tty_struct *tty = line->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 int err;
345
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700346 /*
347 * Interrupts are disabled here because we registered the interrupt with
348 * IRQF_DISABLED (see line_setup_irq).
349 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700350
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800351 spin_lock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700353 if (err == 0) {
354 return IRQ_NONE;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700355 } else if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 line->head = line->buffer;
357 line->tail = line->buffer;
358 }
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800359 spin_unlock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700361 if (tty == NULL)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700362 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700364 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 (tty->ldisc.write_wakeup != NULL))
366 (tty->ldisc.write_wakeup)(tty);
Jeff Dike165dc592006-01-06 00:18:57 -0800367
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700368 /*
369 * BLOCKING mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * In blocking mode, everything sleeps on tty->write_wait.
371 * Sleeping in the console driver would break non-blocking
372 * writes.
373 */
374
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700375 if (waitqueue_active(&tty->write_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 wake_up_interruptible(&tty->write_wait);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700377 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Jeff Dike165dc592006-01-06 00:18:57 -0800380int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700382 const struct line_driver *driver = line->driver;
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700383 int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700385 if (input)
386 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
Jeff Diked50084a2006-01-06 00:18:50 -0800387 line_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800388 driver->read_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700389 if (err)
390 return err;
391 if (output)
392 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
Jeff Diked50084a2006-01-06 00:18:50 -0800393 line_write_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800394 driver->write_irq_name, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 line->have_irq = 1;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700396 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700399/*
400 * Normally, a driver like this can rely mostly on the tty layer
Jeff Diked79a5802007-02-10 01:43:52 -0800401 * locking, particularly when it comes to the driver structure.
402 * However, in this case, mconsole requests can come in "from the
403 * side", and race with opens and closes.
404 *
Jeff Dikec6256c62007-02-10 01:44:08 -0800405 * mconsole config requests will want to be sure the device isn't in
406 * use, and get_config, open, and close will want a stable
407 * configuration. The checking and modification of the configuration
408 * is done under a spinlock. Checking whether the device is in use is
409 * line->tty->count > 1, also under the spinlock.
Jeff Diked79a5802007-02-10 01:43:52 -0800410 *
Jeff Dikec6256c62007-02-10 01:44:08 -0800411 * tty->count serves to decide whether the device should be enabled or
412 * disabled on the host. If it's equal to 1, then we are doing the
413 * first open or last close. Otherwise, open and close just return.
Jeff Diked79a5802007-02-10 01:43:52 -0800414 */
415
Jeff Dike1f801712006-01-06 00:18:55 -0800416int line_open(struct line *lines, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Jeff Diked79a5802007-02-10 01:43:52 -0800418 struct line *line = &lines[tty->index];
Jeff Dike165dc592006-01-06 00:18:57 -0800419 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Jeff Diked79a5802007-02-10 01:43:52 -0800421 spin_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700422 if (!line->valid)
Jeff Diked79a5802007-02-10 01:43:52 -0800423 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Jeff Diked79a5802007-02-10 01:43:52 -0800425 err = 0;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700426 if (tty->count > 1)
Jeff Diked79a5802007-02-10 01:43:52 -0800427 goto out_unlock;
428
Jeff Diked79a5802007-02-10 01:43:52 -0800429 spin_unlock(&line->count_lock);
Jeff Dike165dc592006-01-06 00:18:57 -0800430
431 tty->driver_data = line;
432 line->tty = tty;
Jeff Dike165dc592006-01-06 00:18:57 -0800433
Jeff Diked14ad812007-07-15 23:38:54 -0700434 err = enable_chan(line);
435 if (err)
436 return err;
437
Jeff Diked79a5802007-02-10 01:43:52 -0800438 INIT_DELAYED_WORK(&line->task, line_timer_cb);
Jeff Dike165dc592006-01-06 00:18:57 -0800439
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700440 if (!line->sigio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800441 chan_enable_winch(&line->chan_list, tty);
442 line->sigio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
Jeff Diked79a5802007-02-10 01:43:52 -0800445 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
446 &tty->winsize.ws_col);
447
Jeff Diked79a5802007-02-10 01:43:52 -0800448 return err;
449
450out_unlock:
451 spin_unlock(&line->count_lock);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700452 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700455static void unregister_winch(struct tty_struct *tty);
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457void line_close(struct tty_struct *tty, struct file * filp)
458{
459 struct line *line = tty->driver_data;
460
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700461 /*
462 * If line_open fails (and tty->driver_data is never set),
Jeff Diked79a5802007-02-10 01:43:52 -0800463 * tty_open will call line_close. So just return in this case.
464 */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700465 if (line == NULL)
Jeff Diked79a5802007-02-10 01:43:52 -0800466 return;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700467
468 /* We ignore the error anyway! */
469 flush_buffer(line);
470
Jeff Diked79a5802007-02-10 01:43:52 -0800471 spin_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700472 if (!line->valid)
Jeff Diked79a5802007-02-10 01:43:52 -0800473 goto out_unlock;
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700474
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700475 if (tty->count > 1)
Jeff Diked79a5802007-02-10 01:43:52 -0800476 goto out_unlock;
477
Jeff Diked79a5802007-02-10 01:43:52 -0800478 spin_unlock(&line->count_lock);
479
480 line->tty = NULL;
481 tty->driver_data = NULL;
482
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700483 if (line->sigio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800484 unregister_winch(tty);
485 line->sigio = 0;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700486 }
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700487
Jeff Diked79a5802007-02-10 01:43:52 -0800488 return;
489
490out_unlock:
491 spin_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494void close_lines(struct line *lines, int nlines)
495{
496 int i;
497
498 for(i = 0; i < nlines; i++)
Jeff Dike165dc592006-01-06 00:18:57 -0800499 close_chan(&lines[i].chan_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Jeff Dikef28169d2007-02-10 01:43:53 -0800502static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
503 char **error_out)
Jeff Diked79a5802007-02-10 01:43:52 -0800504{
505 struct line *line = &lines[n];
Jeff Dikef28169d2007-02-10 01:43:53 -0800506 int err = -EINVAL;
Jeff Diked79a5802007-02-10 01:43:52 -0800507
508 spin_lock(&line->count_lock);
509
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700510 if (line->tty != NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800511 *error_out = "Device is already open";
Jeff Diked79a5802007-02-10 01:43:52 -0800512 goto out;
513 }
514
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700515 if (line->init_pri <= init_prio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800516 line->init_pri = init_prio;
517 if (!strcmp(init, "none"))
518 line->valid = 0;
519 else {
520 line->init_str = init;
521 line->valid = 1;
522 }
523 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800524 err = 0;
Jeff Diked79a5802007-02-10 01:43:52 -0800525out:
526 spin_unlock(&line->count_lock);
Jeff Dikef28169d2007-02-10 01:43:53 -0800527 return err;
Jeff Diked79a5802007-02-10 01:43:52 -0800528}
529
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700530/*
531 * Common setup code for both startup command line and mconsole initialization.
Matt LaPlante4b3f6862006-10-03 22:21:02 +0200532 * @lines contains the array (of size @num) to modify;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700533 * @init is the setup string;
Jeff Dikef28169d2007-02-10 01:43:53 -0800534 * @error_out is an error string in the case of failure;
Jeff Diked571cd12006-01-06 00:18:53 -0800535 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700536
Jeff Dikef28169d2007-02-10 01:43:53 -0800537int line_setup(struct line *lines, unsigned int num, char *init,
538 char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Jeff Dikef28169d2007-02-10 01:43:53 -0800540 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 char *end;
542
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700543 if (*init == '=') {
544 /*
545 * We said con=/ssl= instead of con#=, so we are configuring all
546 * consoles at once.
547 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700548 n = -1;
Jeff Diked50084a2006-01-06 00:18:50 -0800549 }
550 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 n = simple_strtoul(init, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700552 if (*end != '=') {
Jeff Dikef28169d2007-02-10 01:43:53 -0800553 *error_out = "Couldn't parse device number";
554 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 init = end;
557 }
558 init++;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700559
560 if (n >= (signed int) num) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800561 *error_out = "Device number out of range";
562 return -EINVAL;
Jeff Diked50084a2006-01-06 00:18:50 -0800563 }
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700564 else if (n >= 0) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800565 err = setup_one_line(lines, n, init, INIT_ONE, error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700566 if (err)
Jeff Dikef28169d2007-02-10 01:43:53 -0800567 return err;
568 }
Jeff Diked50084a2006-01-06 00:18:50 -0800569 else {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700570 for(i = 0; i < num; i++) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800571 err = setup_one_line(lines, i, init, INIT_ALL,
572 error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700573 if (err)
Jeff Dikef28169d2007-02-10 01:43:53 -0800574 return err;
575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Jeff Dike418e55d2006-01-06 00:18:54 -0800577 return n == -1 ? num : n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Jeff Dike1f801712006-01-06 00:18:55 -0800580int line_config(struct line *lines, unsigned int num, char *str,
Jeff Dikef28169d2007-02-10 01:43:53 -0800581 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Jeff Dike1f801712006-01-06 00:18:55 -0800583 struct line *line;
Jeff Dike970d6e32006-01-06 00:18:48 -0800584 char *new;
Jeff Dike418e55d2006-01-06 00:18:54 -0800585 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700587 if (*str == '=') {
Jeff Dikef28169d2007-02-10 01:43:53 -0800588 *error_out = "Can't configure all devices from mconsole";
589 return -EINVAL;
Jeff Diked571cd12006-01-06 00:18:53 -0800590 }
591
Jeff Dike970d6e32006-01-06 00:18:48 -0800592 new = kstrdup(str, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700593 if (new == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800594 *error_out = "Failed to allocate memory";
595 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800597 n = line_setup(lines, num, new, error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700598 if (n < 0)
Jeff Dikef28169d2007-02-10 01:43:53 -0800599 return n;
Jeff Dike1f801712006-01-06 00:18:55 -0800600
601 line = &lines[n];
Jeff Dikef28169d2007-02-10 01:43:53 -0800602 return parse_chan_pair(line->init_str, line, n, opts, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700605int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 int size, char **error_out)
607{
608 struct line *line;
609 char *end;
610 int dev, n = 0;
611
612 dev = simple_strtoul(name, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700613 if ((*end != '\0') || (end == name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 *error_out = "line_get_config failed to parse device number";
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700618 if ((dev < 0) || (dev >= num)) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700619 *error_out = "device number out of range";
620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
623 line = &lines[dev];
624
Jeff Diked79a5802007-02-10 01:43:52 -0800625 spin_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700626 if (!line->valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700628 else if (line->tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 CONFIG_CHUNK(str, size, n, line->init_str, 1);
630 else n = chan_config_string(&line->chan_list, str, size, error_out);
Jeff Diked79a5802007-02-10 01:43:52 -0800631 spin_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700633 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
Jeff Dike29d56cf2005-06-25 14:55:25 -0700636int line_id(char **str, int *start_out, int *end_out)
637{
638 char *end;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700639 int n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700640
641 n = simple_strtoul(*str, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700642 if ((*end != '\0') || (end == *str))
643 return -1;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700644
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700645 *str = end;
646 *start_out = n;
647 *end_out = n;
648 return n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700649}
650
Jeff Dikef28169d2007-02-10 01:43:53 -0800651int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Jeff Dike418e55d2006-01-06 00:18:54 -0800653 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 char config[sizeof("conxxxx=none\0")];
655
Jeff Dike29d56cf2005-06-25 14:55:25 -0700656 sprintf(config, "%d=none", n);
Jeff Dikef28169d2007-02-10 01:43:53 -0800657 err = line_setup(lines, num, config, error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700658 if (err >= 0)
Jeff Dike418e55d2006-01-06 00:18:54 -0800659 err = 0;
660 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Jeff Diked5c9ffc2007-02-10 01:44:08 -0800663struct tty_driver *register_lines(struct line_driver *line_driver,
664 const struct tty_operations *ops,
665 struct line *lines, int nlines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 int i;
668 struct tty_driver *driver = alloc_tty_driver(nlines);
669
670 if (!driver)
671 return NULL;
672
673 driver->driver_name = line_driver->name;
674 driver->name = line_driver->device_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 driver->major = line_driver->major;
676 driver->minor_start = line_driver->minor_start;
677 driver->type = line_driver->type;
678 driver->subtype = line_driver->subtype;
679 driver->flags = TTY_DRIVER_REAL_RAW;
680 driver->init_termios = tty_std_termios;
681 tty_set_operations(driver, ops);
682
683 if (tty_register_driver(driver)) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700684 printk(KERN_ERR "register_lines : can't register %s driver\n",
685 line_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 put_tty_driver(driver);
687 return NULL;
688 }
689
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700690 for(i = 0; i < nlines; i++) {
691 if (!lines[i].valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 tty_unregister_device(driver, i);
693 }
694
695 mconsole_register_dev(&line_driver->mc);
696 return driver;
697}
698
Jeff Dike90107722006-01-06 00:18:54 -0800699static DEFINE_SPINLOCK(winch_handler_lock);
700static LIST_HEAD(winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700701
Jeff Dike1f801712006-01-06 00:18:55 -0800702void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 struct line *line;
Jeff Dikef28169d2007-02-10 01:43:53 -0800705 char *error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 int i;
707
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700708 for(i = 0; i < nlines; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 line = &lines[i];
710 INIT_LIST_HEAD(&line->chan_list);
Jeff Dike90107722006-01-06 00:18:54 -0800711
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700712 if (line->init_str == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800713 continue;
714
715 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700716 if (line->init_str == NULL)
717 printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
Jeff Dike1f801712006-01-06 00:18:55 -0800718
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700719 if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
720 printk(KERN_ERR "parse_chan_pair failed for "
721 "device %d : %s\n", i, error);
Jeff Dike1f801712006-01-06 00:18:55 -0800722 line->valid = 0;
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725}
726
727struct winch {
728 struct list_head list;
729 int fd;
730 int tty_fd;
731 int pid;
732 struct tty_struct *tty;
Jeff Dike42a359e2007-07-15 23:38:55 -0700733 unsigned long stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734};
735
Jeff Dike42a359e2007-07-15 23:38:55 -0700736static void free_winch(struct winch *winch, int free_irq_ok)
737{
738 list_del(&winch->list);
739
740 if (winch->pid != -1)
741 os_kill_process(winch->pid, 1);
742 if (winch->fd != -1)
743 os_close_file(winch->fd);
744 if (winch->stack != 0)
745 free_stack(winch->stack, 0);
746 if (free_irq_ok)
747 free_irq(WINCH_IRQ, winch);
748 kfree(winch);
749}
750
Al Viro7bea96f2006-10-08 22:49:34 +0100751static irqreturn_t winch_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct winch *winch = data;
754 struct tty_struct *tty;
755 struct line *line;
756 int err;
757 char c;
758
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700759 if (winch->fd != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 err = generic_read(winch->fd, &c, NULL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700761 if (err < 0) {
762 if (err != -EAGAIN) {
763 printk(KERN_ERR "winch_interrupt : "
764 "read failed, errno = %d\n", -err);
765 printk(KERN_ERR "fd %d is losing SIGWINCH "
766 "support\n", winch->tty_fd);
Jeff Dike42a359e2007-07-15 23:38:55 -0700767 free_winch(winch, 0);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700768 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770 goto out;
771 }
772 }
Jeff Dike42a359e2007-07-15 23:38:55 -0700773 tty = winch->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (tty != NULL) {
775 line = tty->driver_data;
Jeff Diked50084a2006-01-06 00:18:50 -0800776 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 &tty->winsize.ws_col);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800778 kill_pgrp(tty->pgrp, SIGWINCH, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 out:
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700781 if (winch->fd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 reactivate_fd(winch->fd, WINCH_IRQ);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700783 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Jeff Dike42a359e2007-07-15 23:38:55 -0700786void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
787 unsigned long stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 struct winch *winch;
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
792 if (winch == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700793 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700794 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
798 .fd = fd,
799 .tty_fd = tty_fd,
800 .pid = pid,
Jeff Dike42a359e2007-07-15 23:38:55 -0700801 .tty = tty,
802 .stack = stack });
803
804 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
805 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
806 "winch", winch) < 0) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700807 printk(KERN_ERR "register_winch_irq - failed to register "
808 "IRQ\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700809 goto out_free;
810 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700811
812 spin_lock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 list_add(&winch->list, &winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700814 spin_unlock(&winch_handler_lock);
815
Jeff Dike42a359e2007-07-15 23:38:55 -0700816 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Jeff Dike42a359e2007-07-15 23:38:55 -0700818 out_free:
Jeff Dikee464bf22006-01-06 00:19:01 -0800819 kfree(winch);
Jeff Dike42a359e2007-07-15 23:38:55 -0700820 cleanup:
821 os_kill_process(pid, 1);
822 os_close_file(fd);
823 if (stack != 0)
824 free_stack(stack, 0);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700825}
826
Jeff Dikee464bf22006-01-06 00:19:01 -0800827static void unregister_winch(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 struct list_head *ele;
830 struct winch *winch;
831
Jeff Dikee464bf22006-01-06 00:19:01 -0800832 spin_lock(&winch_handler_lock);
833
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700834 list_for_each(ele, &winch_handlers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 winch = list_entry(ele, struct winch, list);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700836 if (winch->tty == tty) {
Jeff Dike42a359e2007-07-15 23:38:55 -0700837 free_winch(winch, 1);
Jeff Dikee464bf22006-01-06 00:19:01 -0800838 break;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700839 }
840 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800841 spin_unlock(&winch_handler_lock);
842}
843
844static void winch_cleanup(void)
845{
846 struct list_head *ele, *next;
847 struct winch *winch;
848
849 spin_lock(&winch_handler_lock);
850
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700851 list_for_each_safe(ele, next, &winch_handlers) {
Jeff Dikee464bf22006-01-06 00:19:01 -0800852 winch = list_entry(ele, struct winch, list);
Jeff Dike42a359e2007-07-15 23:38:55 -0700853 free_winch(winch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800855
856 spin_unlock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858__uml_exitcall(winch_cleanup);
859
860char *add_xterm_umid(char *base)
861{
862 char *umid, *title;
863 int len;
864
Jeff Dike7eebe8a2006-01-06 00:19:01 -0800865 umid = get_umid();
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700866 if (*umid == '\0')
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700867 return base;
Jeff Dike165dc592006-01-06 00:18:57 -0800868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
870 title = kmalloc(len, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700871 if (title == NULL) {
872 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700873 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
876 snprintf(title, len, "%s (%s)", base, umid);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700877 return title;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}