blob: 83bf15a3dda88472dc5aea5ade3353e3f772e0e2 [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)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -070051 n = LINE_BUFSIZE + n; /* The other case */
52 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
61 if (tty->stopped)
62 return 0;
63
64 spin_lock_irqsave(&line->lock, flags);
65 room = write_room(line);
66 spin_unlock_irqrestore(&line->lock, flags);
67
68 /*XXX: Warning to remove */
69 if (0 == room)
70 printk(KERN_DEBUG "%s: %s: no room left in buffer\n",
71 __FUNCTION__,tty->name);
72 return room;
73}
74
75int line_chars_in_buffer(struct tty_struct *tty)
76{
77 struct line *line = tty->driver_data;
78 unsigned long flags;
79 int ret;
80
81 spin_lock_irqsave(&line->lock, flags);
82
83 /*write_room subtracts 1 for the needed NULL, so we readd it.*/
84 ret = LINE_BUFSIZE - (write_room(line) + 1);
85 spin_unlock_irqrestore(&line->lock, flags);
86
87 return ret;
88}
89
90/*
91 * This copies the content of buf into the circular buffer associated with
92 * this line.
93 * The return value is the number of characters actually copied, i.e. the ones
94 * for which there was space: this function is not supposed to ever flush out
95 * the circular buffer.
96 *
97 * Must be called while holding line->lock!
98 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int buffer_data(struct line *line, const char *buf, int len)
100{
101 int end, room;
102
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700103 if (line->buffer == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
105 if (line->buffer == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700106 printk(KERN_ERR "buffer_data - atomic allocation "
107 "failed\n");
108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110 line->head = line->buffer;
111 line->tail = line->buffer;
112 }
113
114 room = write_room(line);
115 len = (len > room) ? room : len;
116
117 end = line->buffer + LINE_BUFSIZE - line->tail;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700118
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700119 if (len < end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 memcpy(line->tail, buf, len);
121 line->tail += len;
Jeff Diked50084a2006-01-06 00:18:50 -0800122 }
123 else {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700124 /* The circular buffer is wrapping */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 memcpy(line->tail, buf, end);
126 buf += end;
127 memcpy(line->buffer, buf, len - end);
128 line->tail = line->buffer + len - end;
129 }
130
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700131 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700134/*
135 * Flushes the ring buffer to the output channels. That is, write_chan is
136 * called, passing it line->head as buffer, and an appropriate count.
137 *
138 * On exit, returns 1 when the buffer is empty,
139 * 0 when the buffer is not empty on exit,
140 * and -errno when an error occurred.
141 *
142 * Must be called while holding line->lock!*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static int flush_buffer(struct line *line)
144{
145 int n, count;
146
147 if ((line->buffer == NULL) || (line->head == line->tail))
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700148 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 if (line->tail < line->head) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700151 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 count = line->buffer + LINE_BUFSIZE - line->head;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 n = write_chan(&line->chan_list, line->head, count,
155 line->driver->write_irq);
156 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700157 return n;
158 if (n == count) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700159 /*
160 * We have flushed from ->head to buffer end, now we
161 * must flush only from the beginning to ->tail.
162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 line->head = line->buffer;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700164 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168 }
169
170 count = line->tail - line->head;
Jeff Diked50084a2006-01-06 00:18:50 -0800171 n = write_chan(&line->chan_list, line->head, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700173
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700174 if (n < 0)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700175 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 line->head += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700178 return line->head == line->tail;
179}
180
181void line_flush_buffer(struct tty_struct *tty)
182{
183 struct line *line = tty->driver_data;
184 unsigned long flags;
185 int err;
186
187 /*XXX: copied from line_write, verify if it is correct!*/
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700188 if (tty->stopped)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700189 return;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700190
191 spin_lock_irqsave(&line->lock, flags);
192 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700193 spin_unlock_irqrestore(&line->lock, flags);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700194}
195
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700196/*
197 * We map both ->flush_chars and ->put_char (which go in pair) onto
198 * ->flush_buffer and ->write. Hope it's not that bad.
199 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700200void line_flush_chars(struct tty_struct *tty)
201{
202 line_flush_buffer(tty);
203}
204
205void line_put_char(struct tty_struct *tty, unsigned char ch)
206{
207 line_write(tty, &ch, sizeof(ch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
211{
212 struct line *line = tty->driver_data;
213 unsigned long flags;
Jeff Dikec59dbca2007-10-16 01:26:42 -0700214 int n, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700216 if (tty->stopped)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700219 spin_lock_irqsave(&line->lock, flags);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700220 if (line->head != line->tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 ret = buffer_data(line, buf, len);
Jeff Dikec59dbca2007-10-16 01:26:42 -0700222 else {
Jeff Diked50084a2006-01-06 00:18:50 -0800223 n = write_chan(&line->chan_list, buf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 line->driver->write_irq);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700225 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 ret = n;
227 goto out_up;
228 }
229
230 len -= n;
231 ret += n;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700232 if (len > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 ret += buffer_data(line, buf + n, len);
234 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700235out_up:
236 spin_unlock_irqrestore(&line->lock, flags);
237 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Alan Cox606d0992006-12-08 02:38:45 -0800240void line_set_termios(struct tty_struct *tty, struct ktermios * old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 /* nothing */
243}
244
Jeff Dike5e7672e2006-09-27 01:50:33 -0700245static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 int cmd;
247 char *level;
248 char *name;
249} tty_ioctls[] = {
250 /* don't print these, they flood the log ... */
251 { TCGETS, NULL, "TCGETS" },
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700252 { TCSETS, NULL, "TCSETS" },
253 { TCSETSW, NULL, "TCSETSW" },
254 { TCFLSH, NULL, "TCFLSH" },
255 { TCSBRK, NULL, "TCSBRK" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* general tty stuff */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700258 { TCSETSF, KERN_DEBUG, "TCSETSF" },
259 { TCGETA, KERN_DEBUG, "TCGETA" },
260 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
261 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
262 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* linux-specific ones */
265 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
266 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
267 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
268 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
269};
270
271int line_ioctl(struct tty_struct *tty, struct file * file,
272 unsigned int cmd, unsigned long arg)
273{
274 int ret;
275 int i;
276
277 ret = 0;
278 switch(cmd) {
279#ifdef TIOCGETP
280 case TIOCGETP:
281 case TIOCSETP:
282 case TIOCSETN:
283#endif
284#ifdef TIOCGETC
285 case TIOCGETC:
286 case TIOCSETC:
287#endif
288#ifdef TIOCGLTC
289 case TIOCGLTC:
290 case TIOCSLTC:
291#endif
292 case TCGETS:
293 case TCSETSF:
294 case TCSETSW:
295 case TCSETS:
296 case TCGETA:
297 case TCSETAF:
298 case TCSETAW:
299 case TCSETA:
300 case TCXONC:
301 case TCFLSH:
302 case TIOCOUTQ:
303 case TIOCINQ:
304 case TIOCGLCKTRMIOS:
305 case TIOCSLCKTRMIOS:
306 case TIOCPKT:
307 case TIOCGSOFTCAR:
308 case TIOCSSOFTCAR:
309 return -ENOIOCTLCMD;
310#if 0
311 case TCwhatever:
312 /* do something */
313 break;
314#endif
315 default:
316 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
317 if (cmd == tty_ioctls[i].cmd)
318 break;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700319 if (i == ARRAY_SIZE(tty_ioctls)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
321 __FUNCTION__, tty->name, cmd);
322 }
323 ret = -ENOIOCTLCMD;
324 break;
325 }
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700326 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Jeff Dikee4dcee82006-01-06 00:18:58 -0800329void line_throttle(struct tty_struct *tty)
330{
331 struct line *line = tty->driver_data;
332
333 deactivate_chan(&line->chan_list, line->driver->read_irq);
334 line->throttled = 1;
335}
336
337void line_unthrottle(struct tty_struct *tty)
338{
339 struct line *line = tty->driver_data;
340
341 line->throttled = 0;
342 chan_interrupt(&line->chan_list, &line->task, tty,
343 line->driver->read_irq);
344
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700345 /*
346 * Maybe there is enough stuff pending that calling the interrupt
Jeff Dikee4dcee82006-01-06 00:18:58 -0800347 * throttles us again. In this case, line->throttled will be 1
348 * again and we shouldn't turn the interrupt back on.
349 */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700350 if (!line->throttled)
Jeff Dikee4dcee82006-01-06 00:18:58 -0800351 reactivate_chan(&line->chan_list, line->driver->read_irq);
352}
353
Al Viro7bea96f2006-10-08 22:49:34 +0100354static irqreturn_t line_write_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Jeff Dike165dc592006-01-06 00:18:57 -0800356 struct chan *chan = data;
357 struct line *line = chan->line;
358 struct tty_struct *tty = line->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 int err;
360
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700361 /*
362 * Interrupts are disabled here because we registered the interrupt with
363 * IRQF_DISABLED (see line_setup_irq).
364 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700365
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800366 spin_lock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 err = flush_buffer(line);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700368 if (err == 0) {
369 return IRQ_NONE;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700370 } else if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 line->head = line->buffer;
372 line->tail = line->buffer;
373 }
Paolo 'Blaisorblade' Giarrussoec0ac8a2007-03-07 20:41:12 -0800374 spin_unlock(&line->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700376 if (tty == NULL)
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700377 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700379 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 (tty->ldisc.write_wakeup != NULL))
381 (tty->ldisc.write_wakeup)(tty);
Jeff Dike165dc592006-01-06 00:18:57 -0800382
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700383 /*
384 * BLOCKING mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 * In blocking mode, everything sleeps on tty->write_wait.
386 * Sleeping in the console driver would break non-blocking
387 * writes.
388 */
389
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700390 if (waitqueue_active(&tty->write_wait))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 wake_up_interruptible(&tty->write_wait);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700392 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Jeff Dike165dc592006-01-06 00:18:57 -0800395int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Jeff Dike5e7672e2006-09-27 01:50:33 -0700397 const struct line_driver *driver = line->driver;
Thomas Gleixnerbd6aa652006-07-01 19:29:27 -0700398 int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700400 if (input)
401 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
Jeff Diked50084a2006-01-06 00:18:50 -0800402 line_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800403 driver->read_irq_name, data);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700404 if (err)
405 return err;
406 if (output)
407 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
Jeff Diked50084a2006-01-06 00:18:50 -0800408 line_write_interrupt, flags,
Jeff Dike165dc592006-01-06 00:18:57 -0800409 driver->write_irq_name, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 line->have_irq = 1;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700411 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700414/*
415 * Normally, a driver like this can rely mostly on the tty layer
Jeff Diked79a5802007-02-10 01:43:52 -0800416 * locking, particularly when it comes to the driver structure.
417 * However, in this case, mconsole requests can come in "from the
418 * side", and race with opens and closes.
419 *
Jeff Dikec6256c62007-02-10 01:44:08 -0800420 * mconsole config requests will want to be sure the device isn't in
421 * use, and get_config, open, and close will want a stable
422 * configuration. The checking and modification of the configuration
423 * is done under a spinlock. Checking whether the device is in use is
424 * line->tty->count > 1, also under the spinlock.
Jeff Diked79a5802007-02-10 01:43:52 -0800425 *
Jeff Dikec6256c62007-02-10 01:44:08 -0800426 * tty->count serves to decide whether the device should be enabled or
427 * disabled on the host. If it's equal to 1, then we are doing the
428 * first open or last close. Otherwise, open and close just return.
Jeff Diked79a5802007-02-10 01:43:52 -0800429 */
430
Jeff Dike1f801712006-01-06 00:18:55 -0800431int line_open(struct line *lines, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Jeff Diked79a5802007-02-10 01:43:52 -0800433 struct line *line = &lines[tty->index];
Jeff Dike165dc592006-01-06 00:18:57 -0800434 int err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Jeff Diked79a5802007-02-10 01:43:52 -0800436 spin_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700437 if (!line->valid)
Jeff Diked79a5802007-02-10 01:43:52 -0800438 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Jeff Diked79a5802007-02-10 01:43:52 -0800440 err = 0;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700441 if (tty->count > 1)
Jeff Diked79a5802007-02-10 01:43:52 -0800442 goto out_unlock;
443
Jeff Diked79a5802007-02-10 01:43:52 -0800444 spin_unlock(&line->count_lock);
Jeff Dike165dc592006-01-06 00:18:57 -0800445
446 tty->driver_data = line;
447 line->tty = tty;
Jeff Dike165dc592006-01-06 00:18:57 -0800448
Jeff Diked14ad812007-07-15 23:38:54 -0700449 err = enable_chan(line);
450 if (err)
451 return err;
452
Jeff Diked79a5802007-02-10 01:43:52 -0800453 INIT_DELAYED_WORK(&line->task, line_timer_cb);
Jeff Dike165dc592006-01-06 00:18:57 -0800454
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700455 if (!line->sigio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800456 chan_enable_winch(&line->chan_list, tty);
457 line->sigio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
Jeff Diked79a5802007-02-10 01:43:52 -0800460 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
461 &tty->winsize.ws_col);
462
Jeff Diked79a5802007-02-10 01:43:52 -0800463 return err;
464
465out_unlock:
466 spin_unlock(&line->count_lock);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700467 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700470static void unregister_winch(struct tty_struct *tty);
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472void line_close(struct tty_struct *tty, struct file * filp)
473{
474 struct line *line = tty->driver_data;
475
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700476 /*
477 * If line_open fails (and tty->driver_data is never set),
Jeff Diked79a5802007-02-10 01:43:52 -0800478 * tty_open will call line_close. So just return in this case.
479 */
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700480 if (line == NULL)
Jeff Diked79a5802007-02-10 01:43:52 -0800481 return;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700482
483 /* We ignore the error anyway! */
484 flush_buffer(line);
485
Jeff Diked79a5802007-02-10 01:43:52 -0800486 spin_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700487 if (!line->valid)
Jeff Diked79a5802007-02-10 01:43:52 -0800488 goto out_unlock;
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700489
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700490 if (tty->count > 1)
Jeff Diked79a5802007-02-10 01:43:52 -0800491 goto out_unlock;
492
Jeff Diked79a5802007-02-10 01:43:52 -0800493 spin_unlock(&line->count_lock);
494
495 line->tty = NULL;
496 tty->driver_data = NULL;
497
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700498 if (line->sigio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800499 unregister_winch(tty);
500 line->sigio = 0;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700501 }
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700502
Jeff Diked79a5802007-02-10 01:43:52 -0800503 return;
504
505out_unlock:
506 spin_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
509void close_lines(struct line *lines, int nlines)
510{
511 int i;
512
513 for(i = 0; i < nlines; i++)
Jeff Dike165dc592006-01-06 00:18:57 -0800514 close_chan(&lines[i].chan_list, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Jeff Dikef28169d2007-02-10 01:43:53 -0800517static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
518 char **error_out)
Jeff Diked79a5802007-02-10 01:43:52 -0800519{
520 struct line *line = &lines[n];
Jeff Dikef28169d2007-02-10 01:43:53 -0800521 int err = -EINVAL;
Jeff Diked79a5802007-02-10 01:43:52 -0800522
523 spin_lock(&line->count_lock);
524
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700525 if (line->tty != NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800526 *error_out = "Device is already open";
Jeff Diked79a5802007-02-10 01:43:52 -0800527 goto out;
528 }
529
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700530 if (line->init_pri <= init_prio) {
Jeff Diked79a5802007-02-10 01:43:52 -0800531 line->init_pri = init_prio;
532 if (!strcmp(init, "none"))
533 line->valid = 0;
534 else {
535 line->init_str = init;
536 line->valid = 1;
537 }
538 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800539 err = 0;
Jeff Diked79a5802007-02-10 01:43:52 -0800540out:
541 spin_unlock(&line->count_lock);
Jeff Dikef28169d2007-02-10 01:43:53 -0800542 return err;
Jeff Diked79a5802007-02-10 01:43:52 -0800543}
544
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700545/*
546 * Common setup code for both startup command line and mconsole initialization.
Matt LaPlante4b3f6862006-10-03 22:21:02 +0200547 * @lines contains the array (of size @num) to modify;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700548 * @init is the setup string;
Jeff Dikef28169d2007-02-10 01:43:53 -0800549 * @error_out is an error string in the case of failure;
Jeff Diked571cd12006-01-06 00:18:53 -0800550 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700551
Jeff Dikef28169d2007-02-10 01:43:53 -0800552int line_setup(struct line *lines, unsigned int num, char *init,
553 char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Jeff Dikef28169d2007-02-10 01:43:53 -0800555 int i, n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 char *end;
557
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700558 if (*init == '=') {
559 /*
560 * We said con=/ssl= instead of con#=, so we are configuring all
561 * consoles at once.
562 */
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700563 n = -1;
Jeff Diked50084a2006-01-06 00:18:50 -0800564 }
565 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 n = simple_strtoul(init, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700567 if (*end != '=') {
Jeff Dikef28169d2007-02-10 01:43:53 -0800568 *error_out = "Couldn't parse device number";
569 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571 init = end;
572 }
573 init++;
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700574
575 if (n >= (signed int) num) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800576 *error_out = "Device number out of range";
577 return -EINVAL;
Jeff Diked50084a2006-01-06 00:18:50 -0800578 }
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700579 else if (n >= 0) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800580 err = setup_one_line(lines, n, init, INIT_ONE, error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700581 if (err)
Jeff Dikef28169d2007-02-10 01:43:53 -0800582 return err;
583 }
Jeff Diked50084a2006-01-06 00:18:50 -0800584 else {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700585 for(i = 0; i < num; i++) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800586 err = setup_one_line(lines, i, init, INIT_ALL,
587 error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700588 if (err)
Jeff Dikef28169d2007-02-10 01:43:53 -0800589 return err;
590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Jeff Dike418e55d2006-01-06 00:18:54 -0800592 return n == -1 ? num : n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Jeff Dike1f801712006-01-06 00:18:55 -0800595int line_config(struct line *lines, unsigned int num, char *str,
Jeff Dikef28169d2007-02-10 01:43:53 -0800596 const struct chan_opts *opts, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Jeff Dike1f801712006-01-06 00:18:55 -0800598 struct line *line;
Jeff Dike970d6e32006-01-06 00:18:48 -0800599 char *new;
Jeff Dike418e55d2006-01-06 00:18:54 -0800600 int n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700602 if (*str == '=') {
Jeff Dikef28169d2007-02-10 01:43:53 -0800603 *error_out = "Can't configure all devices from mconsole";
604 return -EINVAL;
Jeff Diked571cd12006-01-06 00:18:53 -0800605 }
606
Jeff Dike970d6e32006-01-06 00:18:48 -0800607 new = kstrdup(str, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700608 if (new == NULL) {
Jeff Dikef28169d2007-02-10 01:43:53 -0800609 *error_out = "Failed to allocate memory";
610 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
Jeff Dikef28169d2007-02-10 01:43:53 -0800612 n = line_setup(lines, num, new, error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700613 if (n < 0)
Jeff Dikef28169d2007-02-10 01:43:53 -0800614 return n;
Jeff Dike1f801712006-01-06 00:18:55 -0800615
616 line = &lines[n];
Jeff Dikef28169d2007-02-10 01:43:53 -0800617 return parse_chan_pair(line->init_str, line, n, opts, error_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700620int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 int size, char **error_out)
622{
623 struct line *line;
624 char *end;
625 int dev, n = 0;
626
627 dev = simple_strtoul(name, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700628 if ((*end != '\0') || (end == name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 *error_out = "line_get_config failed to parse device number";
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700630 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700633 if ((dev < 0) || (dev >= num)) {
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700634 *error_out = "device number out of range";
635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637
638 line = &lines[dev];
639
Jeff Diked79a5802007-02-10 01:43:52 -0800640 spin_lock(&line->count_lock);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700641 if (!line->valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 CONFIG_CHUNK(str, size, n, "none", 1);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700643 else if (line->tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 CONFIG_CHUNK(str, size, n, line->init_str, 1);
645 else n = chan_config_string(&line->chan_list, str, size, error_out);
Jeff Diked79a5802007-02-10 01:43:52 -0800646 spin_unlock(&line->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700648 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Jeff Dike29d56cf2005-06-25 14:55:25 -0700651int line_id(char **str, int *start_out, int *end_out)
652{
653 char *end;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700654 int n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700655
656 n = simple_strtoul(*str, &end, 0);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700657 if ((*end != '\0') || (end == *str))
658 return -1;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700659
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700660 *str = end;
661 *start_out = n;
662 *end_out = n;
663 return n;
Jeff Dike29d56cf2005-06-25 14:55:25 -0700664}
665
Jeff Dikef28169d2007-02-10 01:43:53 -0800666int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Jeff Dike418e55d2006-01-06 00:18:54 -0800668 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 char config[sizeof("conxxxx=none\0")];
670
Jeff Dike29d56cf2005-06-25 14:55:25 -0700671 sprintf(config, "%d=none", n);
Jeff Dikef28169d2007-02-10 01:43:53 -0800672 err = line_setup(lines, num, config, error_out);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700673 if (err >= 0)
Jeff Dike418e55d2006-01-06 00:18:54 -0800674 err = 0;
675 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Jeff Diked5c9ffc2007-02-10 01:44:08 -0800678struct tty_driver *register_lines(struct line_driver *line_driver,
679 const struct tty_operations *ops,
680 struct line *lines, int nlines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 int i;
683 struct tty_driver *driver = alloc_tty_driver(nlines);
684
685 if (!driver)
686 return NULL;
687
688 driver->driver_name = line_driver->name;
689 driver->name = line_driver->device_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 driver->major = line_driver->major;
691 driver->minor_start = line_driver->minor_start;
692 driver->type = line_driver->type;
693 driver->subtype = line_driver->subtype;
694 driver->flags = TTY_DRIVER_REAL_RAW;
695 driver->init_termios = tty_std_termios;
696 tty_set_operations(driver, ops);
697
698 if (tty_register_driver(driver)) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700699 printk(KERN_ERR "register_lines : can't register %s driver\n",
700 line_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 put_tty_driver(driver);
702 return NULL;
703 }
704
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700705 for(i = 0; i < nlines; i++) {
706 if (!lines[i].valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 tty_unregister_device(driver, i);
708 }
709
710 mconsole_register_dev(&line_driver->mc);
711 return driver;
712}
713
Jeff Dike90107722006-01-06 00:18:54 -0800714static DEFINE_SPINLOCK(winch_handler_lock);
715static LIST_HEAD(winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700716
Jeff Dike1f801712006-01-06 00:18:55 -0800717void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 struct line *line;
Jeff Dikef28169d2007-02-10 01:43:53 -0800720 char *error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 int i;
722
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700723 for(i = 0; i < nlines; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 line = &lines[i];
725 INIT_LIST_HEAD(&line->chan_list);
Jeff Dike90107722006-01-06 00:18:54 -0800726
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700727 if (line->init_str == NULL)
Jeff Diked50084a2006-01-06 00:18:50 -0800728 continue;
729
730 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700731 if (line->init_str == NULL)
732 printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
Jeff Dike1f801712006-01-06 00:18:55 -0800733
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700734 if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
735 printk(KERN_ERR "parse_chan_pair failed for "
736 "device %d : %s\n", i, error);
Jeff Dike1f801712006-01-06 00:18:55 -0800737 line->valid = 0;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740}
741
742struct winch {
743 struct list_head list;
744 int fd;
745 int tty_fd;
746 int pid;
747 struct tty_struct *tty;
Jeff Dike42a359e2007-07-15 23:38:55 -0700748 unsigned long stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749};
750
Jeff Dike42a359e2007-07-15 23:38:55 -0700751static void free_winch(struct winch *winch, int free_irq_ok)
752{
753 list_del(&winch->list);
754
755 if (winch->pid != -1)
756 os_kill_process(winch->pid, 1);
757 if (winch->fd != -1)
758 os_close_file(winch->fd);
759 if (winch->stack != 0)
760 free_stack(winch->stack, 0);
761 if (free_irq_ok)
762 free_irq(WINCH_IRQ, winch);
763 kfree(winch);
764}
765
Al Viro7bea96f2006-10-08 22:49:34 +0100766static irqreturn_t winch_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 struct winch *winch = data;
769 struct tty_struct *tty;
770 struct line *line;
771 int err;
772 char c;
773
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700774 if (winch->fd != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 err = generic_read(winch->fd, &c, NULL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700776 if (err < 0) {
777 if (err != -EAGAIN) {
778 printk(KERN_ERR "winch_interrupt : "
779 "read failed, errno = %d\n", -err);
780 printk(KERN_ERR "fd %d is losing SIGWINCH "
781 "support\n", winch->tty_fd);
Jeff Dike42a359e2007-07-15 23:38:55 -0700782 free_winch(winch, 0);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700783 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785 goto out;
786 }
787 }
Jeff Dike42a359e2007-07-15 23:38:55 -0700788 tty = winch->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (tty != NULL) {
790 line = tty->driver_data;
Jeff Diked50084a2006-01-06 00:18:50 -0800791 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 &tty->winsize.ws_col);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800793 kill_pgrp(tty->pgrp, SIGWINCH, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795 out:
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700796 if (winch->fd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 reactivate_fd(winch->fd, WINCH_IRQ);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700798 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
Jeff Dike42a359e2007-07-15 23:38:55 -0700801void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
802 unsigned long stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 struct winch *winch;
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
807 if (winch == NULL) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700808 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700809 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
813 .fd = fd,
814 .tty_fd = tty_fd,
815 .pid = pid,
Jeff Dike42a359e2007-07-15 23:38:55 -0700816 .tty = tty,
817 .stack = stack });
818
819 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
820 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
821 "winch", winch) < 0) {
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700822 printk(KERN_ERR "register_winch_irq - failed to register "
823 "IRQ\n");
Jeff Dike42a359e2007-07-15 23:38:55 -0700824 goto out_free;
825 }
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700826
827 spin_lock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 list_add(&winch->list, &winch_handlers);
Paolo 'Blaisorblade' Giarrusso605a69a2005-07-07 17:56:52 -0700829 spin_unlock(&winch_handler_lock);
830
Jeff Dike42a359e2007-07-15 23:38:55 -0700831 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Jeff Dike42a359e2007-07-15 23:38:55 -0700833 out_free:
Jeff Dikee464bf22006-01-06 00:19:01 -0800834 kfree(winch);
Jeff Dike42a359e2007-07-15 23:38:55 -0700835 cleanup:
836 os_kill_process(pid, 1);
837 os_close_file(fd);
838 if (stack != 0)
839 free_stack(stack, 0);
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700840}
841
Jeff Dikee464bf22006-01-06 00:19:01 -0800842static void unregister_winch(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 struct list_head *ele;
845 struct winch *winch;
846
Jeff Dikee464bf22006-01-06 00:19:01 -0800847 spin_lock(&winch_handler_lock);
848
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700849 list_for_each(ele, &winch_handlers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 winch = list_entry(ele, struct winch, list);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700851 if (winch->tty == tty) {
Jeff Dike42a359e2007-07-15 23:38:55 -0700852 free_winch(winch, 1);
Jeff Dikee464bf22006-01-06 00:19:01 -0800853 break;
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700854 }
855 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800856 spin_unlock(&winch_handler_lock);
857}
858
859static void winch_cleanup(void)
860{
861 struct list_head *ele, *next;
862 struct winch *winch;
863
864 spin_lock(&winch_handler_lock);
865
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700866 list_for_each_safe(ele, next, &winch_handlers) {
Jeff Dikee464bf22006-01-06 00:19:01 -0800867 winch = list_entry(ele, struct winch, list);
Jeff Dike42a359e2007-07-15 23:38:55 -0700868 free_winch(winch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Jeff Dikee464bf22006-01-06 00:19:01 -0800870
871 spin_unlock(&winch_handler_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873__uml_exitcall(winch_cleanup);
874
875char *add_xterm_umid(char *base)
876{
877 char *umid, *title;
878 int len;
879
Jeff Dike7eebe8a2006-01-06 00:19:01 -0800880 umid = get_umid();
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700881 if (*umid == '\0')
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700882 return base;
Jeff Dike165dc592006-01-06 00:18:57 -0800883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
885 title = kmalloc(len, GFP_KERNEL);
Jeff Dike2f8a2dc2007-10-16 01:26:43 -0700886 if (title == NULL) {
887 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700888 return base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
891 snprintf(title, len, "%s (%s)", base, umid);
Paolo 'Blaisorblade' Giarrussob97b77c2005-05-01 08:58:56 -0700892 return title;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}