uml: console driver cleanups
[safe/jmp/linux-2.6] / arch / um / drivers / line.c
1 /*
2  * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3  * Licensed under the GPL
4  */
5
6 #include "linux/irqreturn.h"
7 #include "linux/kd.h"
8 #include "chan_kern.h"
9 #include "irq_kern.h"
10 #include "irq_user.h"
11 #include "os.h"
12
13 #define LINE_BUFSIZE 4096
14
15 static irqreturn_t line_interrupt(int irq, void *data)
16 {
17         struct chan *chan = data;
18         struct line *line = chan->line;
19         struct tty_struct *tty = line->tty;
20
21         if (line)
22                 chan_interrupt(&line->chan_list, &line->task, tty, irq);
23         return IRQ_HANDLED;
24 }
25
26 static void line_timer_cb(struct work_struct *work)
27 {
28         struct line *line = container_of(work, struct line, task.work);
29
30         if (!line->throttled)
31                 chan_interrupt(&line->chan_list, &line->task, line->tty,
32                                line->driver->read_irq);
33 }
34
35 /*
36  * Returns the free space inside the ring buffer of this line.
37  *
38  * Should be called while holding line->lock (this does not modify data).
39  */
40 static int write_room(struct line *line)
41 {
42         int n;
43
44         if (line->buffer == NULL)
45                 return LINE_BUFSIZE - 1;
46
47         /* This is for the case where the buffer is wrapped! */
48         n = line->head - line->tail;
49
50         if (n <= 0)
51                 n += LINE_BUFSIZE; /* The other case */
52         return n - 1;
53 }
54
55 int line_write_room(struct tty_struct *tty)
56 {
57         struct line *line = tty->driver_data;
58         unsigned long flags;
59         int room;
60
61         spin_lock_irqsave(&line->lock, flags);
62         room = write_room(line);
63         spin_unlock_irqrestore(&line->lock, flags);
64
65         return room;
66 }
67
68 int 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);
75         /* write_room subtracts 1 for the needed NULL, so we readd it.*/
76         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  */
91 static int buffer_data(struct line *line, const char *buf, int len)
92 {
93         int end, room;
94
95         if (line->buffer == NULL) {
96                 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
97                 if (line->buffer == NULL) {
98                         printk(KERN_ERR "buffer_data - atomic allocation "
99                                "failed\n");
100                         return 0;
101                 }
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;
110
111         if (len < end) {
112                 memcpy(line->tail, buf, len);
113                 line->tail += len;
114         }
115         else {
116                 /* The circular buffer is wrapping */
117                 memcpy(line->tail, buf, end);
118                 buf += end;
119                 memcpy(line->buffer, buf, len - end);
120                 line->tail = line->buffer + len - end;
121         }
122
123         return len;
124 }
125
126 /*
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!*/
135 static int flush_buffer(struct line *line)
136 {
137         int n, count;
138
139         if ((line->buffer == NULL) || (line->head == line->tail))
140                 return 1;
141
142         if (line->tail < line->head) {
143                 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
144                 count = line->buffer + LINE_BUFSIZE - line->head;
145
146                 n = write_chan(&line->chan_list, line->head, count,
147                                line->driver->write_irq);
148                 if (n < 0)
149                         return n;
150                 if (n == count) {
151                         /*
152                          * We have flushed from ->head to buffer end, now we
153                          * must flush only from the beginning to ->tail.
154                          */
155                         line->head = line->buffer;
156                 } else {
157                         line->head += n;
158                         return 0;
159                 }
160         }
161
162         count = line->tail - line->head;
163         n = write_chan(&line->chan_list, line->head, count,
164                        line->driver->write_irq);
165
166         if (n < 0)
167                 return n;
168
169         line->head += n;
170         return line->head == line->tail;
171 }
172
173 void line_flush_buffer(struct tty_struct *tty)
174 {
175         struct line *line = tty->driver_data;
176         unsigned long flags;
177         int err;
178
179         spin_lock_irqsave(&line->lock, flags);
180         err = flush_buffer(line);
181         spin_unlock_irqrestore(&line->lock, flags);
182 }
183
184 /*
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  */
188 void line_flush_chars(struct tty_struct *tty)
189 {
190         line_flush_buffer(tty);
191 }
192
193 void line_put_char(struct tty_struct *tty, unsigned char ch)
194 {
195         line_write(tty, &ch, sizeof(ch));
196 }
197
198 int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
199 {
200         struct line *line = tty->driver_data;
201         unsigned long flags;
202         int n, ret = 0;
203
204         spin_lock_irqsave(&line->lock, flags);
205         if (line->head != line->tail)
206                 ret = buffer_data(line, buf, len);
207         else {
208                 n = write_chan(&line->chan_list, buf, len,
209                                line->driver->write_irq);
210                 if (n < 0) {
211                         ret = n;
212                         goto out_up;
213                 }
214
215                 len -= n;
216                 ret += n;
217                 if (len > 0)
218                         ret += buffer_data(line, buf + n, len);
219         }
220 out_up:
221         spin_unlock_irqrestore(&line->lock, flags);
222         return ret;
223 }
224
225 void line_set_termios(struct tty_struct *tty, struct ktermios * old)
226 {
227         /* nothing */
228 }
229
230 static const struct {
231         int  cmd;
232         char *level;
233         char *name;
234 } tty_ioctls[] = {
235         /* don't print these, they flood the log ... */
236         { TCGETS,      NULL,       "TCGETS"      },
237         { TCSETS,      NULL,       "TCSETS"      },
238         { TCSETSW,     NULL,       "TCSETSW"     },
239         { TCFLSH,      NULL,       "TCFLSH"      },
240         { TCSBRK,      NULL,       "TCSBRK"      },
241
242         /* general tty stuff */
243         { TCSETSF,     KERN_DEBUG, "TCSETSF"     },
244         { TCGETA,      KERN_DEBUG, "TCGETA"      },
245         { TIOCMGET,    KERN_DEBUG, "TIOCMGET"    },
246         { TCSBRKP,     KERN_DEBUG, "TCSBRKP"     },
247         { TIOCMSET,    KERN_DEBUG, "TIOCMSET"    },
248
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
256 int 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;
304                 if (i == ARRAY_SIZE(tty_ioctls)) {
305                         printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
306                                __FUNCTION__, tty->name, cmd);
307                 }
308                 ret = -ENOIOCTLCMD;
309                 break;
310         }
311         return ret;
312 }
313
314 void 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
322 void 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
330         /*
331          * Maybe there is enough stuff pending that calling the interrupt
332          * throttles us again.  In this case, line->throttled will be 1
333          * again and we shouldn't turn the interrupt back on.
334          */
335         if (!line->throttled)
336                 reactivate_chan(&line->chan_list, line->driver->read_irq);
337 }
338
339 static irqreturn_t line_write_interrupt(int irq, void *data)
340 {
341         struct chan *chan = data;
342         struct line *line = chan->line;
343         struct tty_struct *tty = line->tty;
344         int err;
345
346         /*
347          * Interrupts are disabled here because we registered the interrupt with
348          * IRQF_DISABLED (see line_setup_irq).
349          */
350
351         spin_lock(&line->lock);
352         err = flush_buffer(line);
353         if (err == 0) {
354                 return IRQ_NONE;
355         } else if (err < 0) {
356                 line->head = line->buffer;
357                 line->tail = line->buffer;
358         }
359         spin_unlock(&line->lock);
360
361         if (tty == NULL)
362                 return IRQ_NONE;
363
364         if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
365            (tty->ldisc.write_wakeup != NULL))
366                 (tty->ldisc.write_wakeup)(tty);
367
368         /*
369          * BLOCKING mode
370          * In blocking mode, everything sleeps on tty->write_wait.
371          * Sleeping in the console driver would break non-blocking
372          * writes.
373          */
374
375         if (waitqueue_active(&tty->write_wait))
376                 wake_up_interruptible(&tty->write_wait);
377         return IRQ_HANDLED;
378 }
379
380 int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
381 {
382         const struct line_driver *driver = line->driver;
383         int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
384
385         if (input)
386                 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
387                                        line_interrupt, flags,
388                                        driver->read_irq_name, data);
389         if (err)
390                 return err;
391         if (output)
392                 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
393                                         line_write_interrupt, flags,
394                                         driver->write_irq_name, data);
395         line->have_irq = 1;
396         return err;
397 }
398
399 /*
400  * Normally, a driver like this can rely mostly on the tty layer
401  * 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  *
405  * 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.
410  *
411  * 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.
414  */
415
416 int line_open(struct line *lines, struct tty_struct *tty)
417 {
418         struct line *line = &lines[tty->index];
419         int err = -ENODEV;
420
421         spin_lock(&line->count_lock);
422         if (!line->valid)
423                 goto out_unlock;
424
425         err = 0;
426         if (tty->count > 1)
427                 goto out_unlock;
428
429         spin_unlock(&line->count_lock);
430
431         tty->driver_data = line;
432         line->tty = tty;
433
434         err = enable_chan(line);
435         if (err)
436                 return err;
437
438         INIT_DELAYED_WORK(&line->task, line_timer_cb);
439
440         if (!line->sigio) {
441                 chan_enable_winch(&line->chan_list, tty);
442                 line->sigio = 1;
443         }
444
445         chan_window_size(&line->chan_list, &tty->winsize.ws_row,
446                          &tty->winsize.ws_col);
447
448         return err;
449
450 out_unlock:
451         spin_unlock(&line->count_lock);
452         return err;
453 }
454
455 static void unregister_winch(struct tty_struct *tty);
456
457 void line_close(struct tty_struct *tty, struct file * filp)
458 {
459         struct line *line = tty->driver_data;
460
461         /*
462          * If line_open fails (and tty->driver_data is never set),
463          * tty_open will call line_close.  So just return in this case.
464          */
465         if (line == NULL)
466                 return;
467
468         /* We ignore the error anyway! */
469         flush_buffer(line);
470
471         spin_lock(&line->count_lock);
472         if (!line->valid)
473                 goto out_unlock;
474
475         if (tty->count > 1)
476                 goto out_unlock;
477
478         spin_unlock(&line->count_lock);
479
480         line->tty = NULL;
481         tty->driver_data = NULL;
482
483         if (line->sigio) {
484                 unregister_winch(tty);
485                 line->sigio = 0;
486         }
487
488         return;
489
490 out_unlock:
491         spin_unlock(&line->count_lock);
492 }
493
494 void close_lines(struct line *lines, int nlines)
495 {
496         int i;
497
498         for(i = 0; i < nlines; i++)
499                 close_chan(&lines[i].chan_list, 0);
500 }
501
502 static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
503                           char **error_out)
504 {
505         struct line *line = &lines[n];
506         int err = -EINVAL;
507
508         spin_lock(&line->count_lock);
509
510         if (line->tty != NULL) {
511                 *error_out = "Device is already open";
512                 goto out;
513         }
514
515         if (line->init_pri <= init_prio) {
516                 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         }
524         err = 0;
525 out:
526         spin_unlock(&line->count_lock);
527         return err;
528 }
529
530 /*
531  * Common setup code for both startup command line and mconsole initialization.
532  * @lines contains the array (of size @num) to modify;
533  * @init is the setup string;
534  * @error_out is an error string in the case of failure;
535  */
536
537 int line_setup(struct line *lines, unsigned int num, char *init,
538                char **error_out)
539 {
540         int i, n, err;
541         char *end;
542
543         if (*init == '=') {
544                 /*
545                  * We said con=/ssl= instead of con#=, so we are configuring all
546                  * consoles at once.
547                  */
548                 n = -1;
549         }
550         else {
551                 n = simple_strtoul(init, &end, 0);
552                 if (*end != '=') {
553                         *error_out = "Couldn't parse device number";
554                         return -EINVAL;
555                 }
556                 init = end;
557         }
558         init++;
559
560         if (n >= (signed int) num) {
561                 *error_out = "Device number out of range";
562                 return -EINVAL;
563         }
564         else if (n >= 0) {
565                 err = setup_one_line(lines, n, init, INIT_ONE, error_out);
566                 if (err)
567                         return err;
568         }
569         else {
570                 for(i = 0; i < num; i++) {
571                         err = setup_one_line(lines, i, init, INIT_ALL,
572                                              error_out);
573                         if (err)
574                                 return err;
575                 }
576         }
577         return n == -1 ? num : n;
578 }
579
580 int line_config(struct line *lines, unsigned int num, char *str,
581                 const struct chan_opts *opts, char **error_out)
582 {
583         struct line *line;
584         char *new;
585         int n;
586
587         if (*str == '=') {
588                 *error_out = "Can't configure all devices from mconsole";
589                 return -EINVAL;
590         }
591
592         new = kstrdup(str, GFP_KERNEL);
593         if (new == NULL) {
594                 *error_out = "Failed to allocate memory";
595                 return -ENOMEM;
596         }
597         n = line_setup(lines, num, new, error_out);
598         if (n < 0)
599                 return n;
600
601         line = &lines[n];
602         return parse_chan_pair(line->init_str, line, n, opts, error_out);
603 }
604
605 int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
606                     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);
613         if ((*end != '\0') || (end == name)) {
614                 *error_out = "line_get_config failed to parse device number";
615                 return 0;
616         }
617
618         if ((dev < 0) || (dev >= num)) {
619                 *error_out = "device number out of range";
620                 return 0;
621         }
622
623         line = &lines[dev];
624
625         spin_lock(&line->count_lock);
626         if (!line->valid)
627                 CONFIG_CHUNK(str, size, n, "none", 1);
628         else if (line->tty == NULL)
629                 CONFIG_CHUNK(str, size, n, line->init_str, 1);
630         else n = chan_config_string(&line->chan_list, str, size, error_out);
631         spin_unlock(&line->count_lock);
632
633         return n;
634 }
635
636 int line_id(char **str, int *start_out, int *end_out)
637 {
638         char *end;
639         int n;
640
641         n = simple_strtoul(*str, &end, 0);
642         if ((*end != '\0') || (end == *str))
643                 return -1;
644
645         *str = end;
646         *start_out = n;
647         *end_out = n;
648         return n;
649 }
650
651 int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
652 {
653         int err;
654         char config[sizeof("conxxxx=none\0")];
655
656         sprintf(config, "%d=none", n);
657         err = line_setup(lines, num, config, error_out);
658         if (err >= 0)
659                 err = 0;
660         return err;
661 }
662
663 struct tty_driver *register_lines(struct line_driver *line_driver,
664                                   const struct tty_operations *ops,
665                                   struct line *lines, int nlines)
666 {
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;
675         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)) {
684                 printk(KERN_ERR "register_lines : can't register %s driver\n",
685                        line_driver->name);
686                 put_tty_driver(driver);
687                 return NULL;
688         }
689
690         for(i = 0; i < nlines; i++) {
691                 if (!lines[i].valid)
692                         tty_unregister_device(driver, i);
693         }
694
695         mconsole_register_dev(&line_driver->mc);
696         return driver;
697 }
698
699 static DEFINE_SPINLOCK(winch_handler_lock);
700 static LIST_HEAD(winch_handlers);
701
702 void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
703 {
704         struct line *line;
705         char *error;
706         int i;
707
708         for(i = 0; i < nlines; i++) {
709                 line = &lines[i];
710                 INIT_LIST_HEAD(&line->chan_list);
711
712                 if (line->init_str == NULL)
713                         continue;
714
715                 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
716                 if (line->init_str == NULL)
717                         printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
718
719                 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);
722                         line->valid = 0;
723                 }
724         }
725 }
726
727 struct winch {
728         struct list_head list;
729         int fd;
730         int tty_fd;
731         int pid;
732         struct tty_struct *tty;
733         unsigned long stack;
734 };
735
736 static 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
751 static irqreturn_t winch_interrupt(int irq, void *data)
752 {
753         struct winch *winch = data;
754         struct tty_struct *tty;
755         struct line *line;
756         int err;
757         char c;
758
759         if (winch->fd != -1) {
760                 err = generic_read(winch->fd, &c, NULL);
761                 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);
767                                 free_winch(winch, 0);
768                                 return IRQ_HANDLED;
769                         }
770                         goto out;
771                 }
772         }
773         tty = winch->tty;
774         if (tty != NULL) {
775                 line = tty->driver_data;
776                 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
777                                  &tty->winsize.ws_col);
778                 kill_pgrp(tty->pgrp, SIGWINCH, 1);
779         }
780  out:
781         if (winch->fd != -1)
782                 reactivate_fd(winch->fd, WINCH_IRQ);
783         return IRQ_HANDLED;
784 }
785
786 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
787                         unsigned long stack)
788 {
789         struct winch *winch;
790
791         winch = kmalloc(sizeof(*winch), GFP_KERNEL);
792         if (winch == NULL) {
793                 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
794                 goto cleanup;
795         }
796
797         *winch = ((struct winch) { .list        = LIST_HEAD_INIT(winch->list),
798                                    .fd          = fd,
799                                    .tty_fd      = tty_fd,
800                                    .pid         = pid,
801                                    .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) {
807                 printk(KERN_ERR "register_winch_irq - failed to register "
808                        "IRQ\n");
809                 goto out_free;
810         }
811
812         spin_lock(&winch_handler_lock);
813         list_add(&winch->list, &winch_handlers);
814         spin_unlock(&winch_handler_lock);
815
816         return;
817
818  out_free:
819         kfree(winch);
820  cleanup:
821         os_kill_process(pid, 1);
822         os_close_file(fd);
823         if (stack != 0)
824                 free_stack(stack, 0);
825 }
826
827 static void unregister_winch(struct tty_struct *tty)
828 {
829         struct list_head *ele;
830         struct winch *winch;
831
832         spin_lock(&winch_handler_lock);
833
834         list_for_each(ele, &winch_handlers) {
835                 winch = list_entry(ele, struct winch, list);
836                 if (winch->tty == tty) {
837                         free_winch(winch, 1);
838                         break;
839                 }
840         }
841         spin_unlock(&winch_handler_lock);
842 }
843
844 static void winch_cleanup(void)
845 {
846         struct list_head *ele, *next;
847         struct winch *winch;
848
849         spin_lock(&winch_handler_lock);
850
851         list_for_each_safe(ele, next, &winch_handlers) {
852                 winch = list_entry(ele, struct winch, list);
853                 free_winch(winch, 1);
854         }
855
856         spin_unlock(&winch_handler_lock);
857 }
858 __uml_exitcall(winch_cleanup);
859
860 char *add_xterm_umid(char *base)
861 {
862         char *umid, *title;
863         int len;
864
865         umid = get_umid();
866         if (*umid == '\0')
867                 return base;
868
869         len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
870         title = kmalloc(len, GFP_KERNEL);
871         if (title == NULL) {
872                 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
873                 return base;
874         }
875
876         snprintf(title, len, "%s (%s)", base, umid);
877         return title;
878 }