Merge branch 'sh/smp'
[pandora-kernel.git] / 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 "linux/sched.h"
9 #include "linux/slab.h"
10 #include "chan_kern.h"
11 #include "irq_kern.h"
12 #include "irq_user.h"
13 #include "kern_util.h"
14 #include "os.h"
15
16 #define LINE_BUFSIZE 4096
17
18 static irqreturn_t line_interrupt(int irq, void *data)
19 {
20         struct chan *chan = data;
21         struct line *line = chan->line;
22         struct tty_struct *tty;
23
24         if (line)
25                 chan_interrupt(&line->chan_list, &line->task, line->tty, irq);
26         return IRQ_HANDLED;
27 }
28
29 static void line_timer_cb(struct work_struct *work)
30 {
31         struct line *line = container_of(work, struct line, task.work);
32
33         if (!line->throttled)
34                 chan_interrupt(&line->chan_list, &line->task, line->tty,
35                                line->driver->read_irq);
36 }
37
38 /*
39  * Returns the free space inside the ring buffer of this line.
40  *
41  * Should be called while holding line->lock (this does not modify data).
42  */
43 static int write_room(struct line *line)
44 {
45         int n;
46
47         if (line->buffer == NULL)
48                 return LINE_BUFSIZE - 1;
49
50         /* This is for the case where the buffer is wrapped! */
51         n = line->head - line->tail;
52
53         if (n <= 0)
54                 n += LINE_BUFSIZE; /* The other case */
55         return n - 1;
56 }
57
58 int line_write_room(struct tty_struct *tty)
59 {
60         struct line *line = tty->driver_data;
61         unsigned long flags;
62         int room;
63
64         spin_lock_irqsave(&line->lock, flags);
65         room = write_room(line);
66         spin_unlock_irqrestore(&line->lock, flags);
67
68         return room;
69 }
70
71 int line_chars_in_buffer(struct tty_struct *tty)
72 {
73         struct line *line = tty->driver_data;
74         unsigned long flags;
75         int ret;
76
77         spin_lock_irqsave(&line->lock, flags);
78         /* write_room subtracts 1 for the needed NULL, so we readd it.*/
79         ret = LINE_BUFSIZE - (write_room(line) + 1);
80         spin_unlock_irqrestore(&line->lock, flags);
81
82         return ret;
83 }
84
85 /*
86  * This copies the content of buf into the circular buffer associated with
87  * this line.
88  * The return value is the number of characters actually copied, i.e. the ones
89  * for which there was space: this function is not supposed to ever flush out
90  * the circular buffer.
91  *
92  * Must be called while holding line->lock!
93  */
94 static int buffer_data(struct line *line, const char *buf, int len)
95 {
96         int end, room;
97
98         if (line->buffer == NULL) {
99                 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
100                 if (line->buffer == NULL) {
101                         printk(KERN_ERR "buffer_data - atomic allocation "
102                                "failed\n");
103                         return 0;
104                 }
105                 line->head = line->buffer;
106                 line->tail = line->buffer;
107         }
108
109         room = write_room(line);
110         len = (len > room) ? room : len;
111
112         end = line->buffer + LINE_BUFSIZE - line->tail;
113
114         if (len < end) {
115                 memcpy(line->tail, buf, len);
116                 line->tail += len;
117         }
118         else {
119                 /* The circular buffer is wrapping */
120                 memcpy(line->tail, buf, end);
121                 buf += end;
122                 memcpy(line->buffer, buf, len - end);
123                 line->tail = line->buffer + len - end;
124         }
125
126         return len;
127 }
128
129 /*
130  * Flushes the ring buffer to the output channels. That is, write_chan is
131  * called, passing it line->head as buffer, and an appropriate count.
132  *
133  * On exit, returns 1 when the buffer is empty,
134  * 0 when the buffer is not empty on exit,
135  * and -errno when an error occurred.
136  *
137  * Must be called while holding line->lock!*/
138 static int flush_buffer(struct line *line)
139 {
140         int n, count;
141
142         if ((line->buffer == NULL) || (line->head == line->tail))
143                 return 1;
144
145         if (line->tail < line->head) {
146                 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
147                 count = line->buffer + LINE_BUFSIZE - line->head;
148
149                 n = write_chan(&line->chan_list, line->head, count,
150                                line->driver->write_irq);
151                 if (n < 0)
152                         return n;
153                 if (n == count) {
154                         /*
155                          * We have flushed from ->head to buffer end, now we
156                          * must flush only from the beginning to ->tail.
157                          */
158                         line->head = line->buffer;
159                 } else {
160                         line->head += n;
161                         return 0;
162                 }
163         }
164
165         count = line->tail - line->head;
166         n = write_chan(&line->chan_list, line->head, count,
167                        line->driver->write_irq);
168
169         if (n < 0)
170                 return n;
171
172         line->head += n;
173         return line->head == line->tail;
174 }
175
176 void line_flush_buffer(struct tty_struct *tty)
177 {
178         struct line *line = tty->driver_data;
179         unsigned long flags;
180         int err;
181
182         spin_lock_irqsave(&line->lock, flags);
183         err = flush_buffer(line);
184         spin_unlock_irqrestore(&line->lock, flags);
185 }
186
187 /*
188  * We map both ->flush_chars and ->put_char (which go in pair) onto
189  * ->flush_buffer and ->write. Hope it's not that bad.
190  */
191 void line_flush_chars(struct tty_struct *tty)
192 {
193         line_flush_buffer(tty);
194 }
195
196 int line_put_char(struct tty_struct *tty, unsigned char ch)
197 {
198         return line_write(tty, &ch, sizeof(ch));
199 }
200
201 int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
202 {
203         struct line *line = tty->driver_data;
204         unsigned long flags;
205         int n, ret = 0;
206
207         spin_lock_irqsave(&line->lock, flags);
208         if (line->head != line->tail)
209                 ret = buffer_data(line, buf, len);
210         else {
211                 n = write_chan(&line->chan_list, buf, len,
212                                line->driver->write_irq);
213                 if (n < 0) {
214                         ret = n;
215                         goto out_up;
216                 }
217
218                 len -= n;
219                 ret += n;
220                 if (len > 0)
221                         ret += buffer_data(line, buf + n, len);
222         }
223 out_up:
224         spin_unlock_irqrestore(&line->lock, flags);
225         return ret;
226 }
227
228 void line_set_termios(struct tty_struct *tty, struct ktermios * old)
229 {
230         /* nothing */
231 }
232
233 static const struct {
234         int  cmd;
235         char *level;
236         char *name;
237 } tty_ioctls[] = {
238         /* don't print these, they flood the log ... */
239         { TCGETS,      NULL,       "TCGETS"      },
240         { TCSETS,      NULL,       "TCSETS"      },
241         { TCSETSW,     NULL,       "TCSETSW"     },
242         { TCFLSH,      NULL,       "TCFLSH"      },
243         { TCSBRK,      NULL,       "TCSBRK"      },
244
245         /* general tty stuff */
246         { TCSETSF,     KERN_DEBUG, "TCSETSF"     },
247         { TCGETA,      KERN_DEBUG, "TCGETA"      },
248         { TIOCMGET,    KERN_DEBUG, "TIOCMGET"    },
249         { TCSBRKP,     KERN_DEBUG, "TCSBRKP"     },
250         { TIOCMSET,    KERN_DEBUG, "TIOCMSET"    },
251
252         /* linux-specific ones */
253         { TIOCLINUX,   KERN_INFO,  "TIOCLINUX"   },
254         { KDGKBMODE,   KERN_INFO,  "KDGKBMODE"   },
255         { KDGKBTYPE,   KERN_INFO,  "KDGKBTYPE"   },
256         { KDSIGACCEPT, KERN_INFO,  "KDSIGACCEPT" },
257 };
258
259 int line_ioctl(struct tty_struct *tty, struct file * file,
260                unsigned int cmd, unsigned long arg)
261 {
262         int ret;
263         int i;
264
265         ret = 0;
266         switch(cmd) {
267 #ifdef TIOCGETP
268         case TIOCGETP:
269         case TIOCSETP:
270         case TIOCSETN:
271 #endif
272 #ifdef TIOCGETC
273         case TIOCGETC:
274         case TIOCSETC:
275 #endif
276 #ifdef TIOCGLTC
277         case TIOCGLTC:
278         case TIOCSLTC:
279 #endif
280         /* Note: these are out of date as we now have TCGETS2 etc but this
281            whole lot should probably go away */
282         case TCGETS:
283         case TCSETSF:
284         case TCSETSW:
285         case TCSETS:
286         case TCGETA:
287         case TCSETAF:
288         case TCSETAW:
289         case TCSETA:
290         case TCXONC:
291         case TCFLSH:
292         case TIOCOUTQ:
293         case TIOCINQ:
294         case TIOCGLCKTRMIOS:
295         case TIOCSLCKTRMIOS:
296         case TIOCPKT:
297         case TIOCGSOFTCAR:
298         case TIOCSSOFTCAR:
299                 return -ENOIOCTLCMD;
300 #if 0
301         case TCwhatever:
302                 /* do something */
303                 break;
304 #endif
305         default:
306                 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
307                         if (cmd == tty_ioctls[i].cmd)
308                                 break;
309                 if (i == ARRAY_SIZE(tty_ioctls)) {
310                         printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
311                                __func__, tty->name, cmd);
312                 }
313                 ret = -ENOIOCTLCMD;
314                 break;
315         }
316         return ret;
317 }
318
319 void line_throttle(struct tty_struct *tty)
320 {
321         struct line *line = tty->driver_data;
322
323         deactivate_chan(&line->chan_list, line->driver->read_irq);
324         line->throttled = 1;
325 }
326
327 void line_unthrottle(struct tty_struct *tty)
328 {
329         struct line *line = tty->driver_data;
330
331         line->throttled = 0;
332         chan_interrupt(&line->chan_list, &line->task, tty,
333                        line->driver->read_irq);
334
335         /*
336          * Maybe there is enough stuff pending that calling the interrupt
337          * throttles us again.  In this case, line->throttled will be 1
338          * again and we shouldn't turn the interrupt back on.
339          */
340         if (!line->throttled)
341                 reactivate_chan(&line->chan_list, line->driver->read_irq);
342 }
343
344 static irqreturn_t line_write_interrupt(int irq, void *data)
345 {
346         struct chan *chan = data;
347         struct line *line = chan->line;
348         struct tty_struct *tty = line->tty;
349         int err;
350
351         /*
352          * Interrupts are disabled here because we registered the interrupt with
353          * IRQF_DISABLED (see line_setup_irq).
354          */
355
356         spin_lock(&line->lock);
357         err = flush_buffer(line);
358         if (err == 0) {
359                 return IRQ_NONE;
360         } else if (err < 0) {
361                 line->head = line->buffer;
362                 line->tail = line->buffer;
363         }
364         spin_unlock(&line->lock);
365
366         if (tty == NULL)
367                 return IRQ_NONE;
368
369         tty_wakeup(tty);
370         return IRQ_HANDLED;
371 }
372
373 int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
374 {
375         const struct line_driver *driver = line->driver;
376         int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
377
378         if (input)
379                 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
380                                        line_interrupt, flags,
381                                        driver->read_irq_name, data);
382         if (err)
383                 return err;
384         if (output)
385                 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
386                                         line_write_interrupt, flags,
387                                         driver->write_irq_name, data);
388         line->have_irq = 1;
389         return err;
390 }
391
392 /*
393  * Normally, a driver like this can rely mostly on the tty layer
394  * locking, particularly when it comes to the driver structure.
395  * However, in this case, mconsole requests can come in "from the
396  * side", and race with opens and closes.
397  *
398  * mconsole config requests will want to be sure the device isn't in
399  * use, and get_config, open, and close will want a stable
400  * configuration.  The checking and modification of the configuration
401  * is done under a spinlock.  Checking whether the device is in use is
402  * line->tty->count > 1, also under the spinlock.
403  *
404  * tty->count serves to decide whether the device should be enabled or
405  * disabled on the host.  If it's equal to 1, then we are doing the
406  * first open or last close.  Otherwise, open and close just return.
407  */
408
409 int line_open(struct line *lines, struct tty_struct *tty)
410 {
411         struct line *line = &lines[tty->index];
412         int err = -ENODEV;
413
414         spin_lock(&line->count_lock);
415         if (!line->valid)
416                 goto out_unlock;
417
418         err = 0;
419         if (tty->count > 1)
420                 goto out_unlock;
421
422         spin_unlock(&line->count_lock);
423
424         tty->driver_data = line;
425         line->tty = tty;
426
427         err = enable_chan(line);
428         if (err)
429                 return err;
430
431         INIT_DELAYED_WORK(&line->task, line_timer_cb);
432
433         if (!line->sigio) {
434                 chan_enable_winch(&line->chan_list, tty);
435                 line->sigio = 1;
436         }
437
438         chan_window_size(&line->chan_list, &tty->winsize.ws_row,
439                          &tty->winsize.ws_col);
440
441         return err;
442
443 out_unlock:
444         spin_unlock(&line->count_lock);
445         return err;
446 }
447
448 static void unregister_winch(struct tty_struct *tty);
449
450 void line_close(struct tty_struct *tty, struct file * filp)
451 {
452         struct line *line = tty->driver_data;
453
454         /*
455          * If line_open fails (and tty->driver_data is never set),
456          * tty_open will call line_close.  So just return in this case.
457          */
458         if (line == NULL)
459                 return;
460
461         /* We ignore the error anyway! */
462         flush_buffer(line);
463
464         spin_lock(&line->count_lock);
465         if (!line->valid)
466                 goto out_unlock;
467
468         if (tty->count > 1)
469                 goto out_unlock;
470
471         spin_unlock(&line->count_lock);
472
473         line->tty = NULL;
474         tty->driver_data = NULL;
475
476         if (line->sigio) {
477                 unregister_winch(tty);
478                 line->sigio = 0;
479         }
480
481         return;
482
483 out_unlock:
484         spin_unlock(&line->count_lock);
485 }
486
487 void close_lines(struct line *lines, int nlines)
488 {
489         int i;
490
491         for(i = 0; i < nlines; i++)
492                 close_chan(&lines[i].chan_list, 0);
493 }
494
495 static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
496                           char **error_out)
497 {
498         struct line *line = &lines[n];
499         int err = -EINVAL;
500
501         spin_lock(&line->count_lock);
502
503         if (line->tty != NULL) {
504                 *error_out = "Device is already open";
505                 goto out;
506         }
507
508         if (line->init_pri <= init_prio) {
509                 line->init_pri = init_prio;
510                 if (!strcmp(init, "none"))
511                         line->valid = 0;
512                 else {
513                         line->init_str = init;
514                         line->valid = 1;
515                 }
516         }
517         err = 0;
518 out:
519         spin_unlock(&line->count_lock);
520         return err;
521 }
522
523 /*
524  * Common setup code for both startup command line and mconsole initialization.
525  * @lines contains the array (of size @num) to modify;
526  * @init is the setup string;
527  * @error_out is an error string in the case of failure;
528  */
529
530 int line_setup(struct line *lines, unsigned int num, char *init,
531                char **error_out)
532 {
533         int i, n, err;
534         char *end;
535
536         if (*init == '=') {
537                 /*
538                  * We said con=/ssl= instead of con#=, so we are configuring all
539                  * consoles at once.
540                  */
541                 n = -1;
542         }
543         else {
544                 n = simple_strtoul(init, &end, 0);
545                 if (*end != '=') {
546                         *error_out = "Couldn't parse device number";
547                         return -EINVAL;
548                 }
549                 init = end;
550         }
551         init++;
552
553         if (n >= (signed int) num) {
554                 *error_out = "Device number out of range";
555                 return -EINVAL;
556         }
557         else if (n >= 0) {
558                 err = setup_one_line(lines, n, init, INIT_ONE, error_out);
559                 if (err)
560                         return err;
561         }
562         else {
563                 for(i = 0; i < num; i++) {
564                         err = setup_one_line(lines, i, init, INIT_ALL,
565                                              error_out);
566                         if (err)
567                                 return err;
568                 }
569         }
570         return n == -1 ? num : n;
571 }
572
573 int line_config(struct line *lines, unsigned int num, char *str,
574                 const struct chan_opts *opts, char **error_out)
575 {
576         struct line *line;
577         char *new;
578         int n;
579
580         if (*str == '=') {
581                 *error_out = "Can't configure all devices from mconsole";
582                 return -EINVAL;
583         }
584
585         new = kstrdup(str, GFP_KERNEL);
586         if (new == NULL) {
587                 *error_out = "Failed to allocate memory";
588                 return -ENOMEM;
589         }
590         n = line_setup(lines, num, new, error_out);
591         if (n < 0)
592                 return n;
593
594         line = &lines[n];
595         return parse_chan_pair(line->init_str, line, n, opts, error_out);
596 }
597
598 int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
599                     int size, char **error_out)
600 {
601         struct line *line;
602         char *end;
603         int dev, n = 0;
604
605         dev = simple_strtoul(name, &end, 0);
606         if ((*end != '\0') || (end == name)) {
607                 *error_out = "line_get_config failed to parse device number";
608                 return 0;
609         }
610
611         if ((dev < 0) || (dev >= num)) {
612                 *error_out = "device number out of range";
613                 return 0;
614         }
615
616         line = &lines[dev];
617
618         spin_lock(&line->count_lock);
619         if (!line->valid)
620                 CONFIG_CHUNK(str, size, n, "none", 1);
621         else if (line->tty == NULL)
622                 CONFIG_CHUNK(str, size, n, line->init_str, 1);
623         else n = chan_config_string(&line->chan_list, str, size, error_out);
624         spin_unlock(&line->count_lock);
625
626         return n;
627 }
628
629 int line_id(char **str, int *start_out, int *end_out)
630 {
631         char *end;
632         int n;
633
634         n = simple_strtoul(*str, &end, 0);
635         if ((*end != '\0') || (end == *str))
636                 return -1;
637
638         *str = end;
639         *start_out = n;
640         *end_out = n;
641         return n;
642 }
643
644 int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
645 {
646         int err;
647         char config[sizeof("conxxxx=none\0")];
648
649         sprintf(config, "%d=none", n);
650         err = line_setup(lines, num, config, error_out);
651         if (err >= 0)
652                 err = 0;
653         return err;
654 }
655
656 struct tty_driver *register_lines(struct line_driver *line_driver,
657                                   const struct tty_operations *ops,
658                                   struct line *lines, int nlines)
659 {
660         int i;
661         struct tty_driver *driver = alloc_tty_driver(nlines);
662
663         if (!driver)
664                 return NULL;
665
666         driver->driver_name = line_driver->name;
667         driver->name = line_driver->device_name;
668         driver->major = line_driver->major;
669         driver->minor_start = line_driver->minor_start;
670         driver->type = line_driver->type;
671         driver->subtype = line_driver->subtype;
672         driver->flags = TTY_DRIVER_REAL_RAW;
673         driver->init_termios = tty_std_termios;
674         tty_set_operations(driver, ops);
675
676         if (tty_register_driver(driver)) {
677                 printk(KERN_ERR "register_lines : can't register %s driver\n",
678                        line_driver->name);
679                 put_tty_driver(driver);
680                 return NULL;
681         }
682
683         for(i = 0; i < nlines; i++) {
684                 if (!lines[i].valid)
685                         tty_unregister_device(driver, i);
686         }
687
688         mconsole_register_dev(&line_driver->mc);
689         return driver;
690 }
691
692 static DEFINE_SPINLOCK(winch_handler_lock);
693 static LIST_HEAD(winch_handlers);
694
695 void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
696 {
697         struct line *line;
698         char *error;
699         int i;
700
701         for(i = 0; i < nlines; i++) {
702                 line = &lines[i];
703                 INIT_LIST_HEAD(&line->chan_list);
704
705                 if (line->init_str == NULL)
706                         continue;
707
708                 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
709                 if (line->init_str == NULL)
710                         printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
711
712                 if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
713                         printk(KERN_ERR "parse_chan_pair failed for "
714                                "device %d : %s\n", i, error);
715                         line->valid = 0;
716                 }
717         }
718 }
719
720 struct winch {
721         struct list_head list;
722         int fd;
723         int tty_fd;
724         int pid;
725         struct tty_struct *tty;
726         unsigned long stack;
727 };
728
729 static void free_winch(struct winch *winch, int free_irq_ok)
730 {
731         list_del(&winch->list);
732
733         if (winch->pid != -1)
734                 os_kill_process(winch->pid, 1);
735         if (winch->fd != -1)
736                 os_close_file(winch->fd);
737         if (winch->stack != 0)
738                 free_stack(winch->stack, 0);
739         if (free_irq_ok)
740                 free_irq(WINCH_IRQ, winch);
741         kfree(winch);
742 }
743
744 static irqreturn_t winch_interrupt(int irq, void *data)
745 {
746         struct winch *winch = data;
747         struct tty_struct *tty;
748         struct line *line;
749         int err;
750         char c;
751
752         if (winch->fd != -1) {
753                 err = generic_read(winch->fd, &c, NULL);
754                 if (err < 0) {
755                         if (err != -EAGAIN) {
756                                 printk(KERN_ERR "winch_interrupt : "
757                                        "read failed, errno = %d\n", -err);
758                                 printk(KERN_ERR "fd %d is losing SIGWINCH "
759                                        "support\n", winch->tty_fd);
760                                 free_winch(winch, 0);
761                                 return IRQ_HANDLED;
762                         }
763                         goto out;
764                 }
765         }
766         tty = winch->tty;
767         if (tty != NULL) {
768                 line = tty->driver_data;
769                 if (line != NULL) {
770                         chan_window_size(&line->chan_list, &tty->winsize.ws_row,
771                                          &tty->winsize.ws_col);
772                         kill_pgrp(tty->pgrp, SIGWINCH, 1);
773                 }
774         }
775  out:
776         if (winch->fd != -1)
777                 reactivate_fd(winch->fd, WINCH_IRQ);
778         return IRQ_HANDLED;
779 }
780
781 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
782                         unsigned long stack)
783 {
784         struct winch *winch;
785
786         winch = kmalloc(sizeof(*winch), GFP_KERNEL);
787         if (winch == NULL) {
788                 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
789                 goto cleanup;
790         }
791
792         *winch = ((struct winch) { .list        = LIST_HEAD_INIT(winch->list),
793                                    .fd          = fd,
794                                    .tty_fd      = tty_fd,
795                                    .pid         = pid,
796                                    .tty         = tty,
797                                    .stack       = stack });
798
799         if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
800                            IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
801                            "winch", winch) < 0) {
802                 printk(KERN_ERR "register_winch_irq - failed to register "
803                        "IRQ\n");
804                 goto out_free;
805         }
806
807         spin_lock(&winch_handler_lock);
808         list_add(&winch->list, &winch_handlers);
809         spin_unlock(&winch_handler_lock);
810
811         return;
812
813  out_free:
814         kfree(winch);
815  cleanup:
816         os_kill_process(pid, 1);
817         os_close_file(fd);
818         if (stack != 0)
819                 free_stack(stack, 0);
820 }
821
822 static void unregister_winch(struct tty_struct *tty)
823 {
824         struct list_head *ele;
825         struct winch *winch;
826
827         spin_lock(&winch_handler_lock);
828
829         list_for_each(ele, &winch_handlers) {
830                 winch = list_entry(ele, struct winch, list);
831                 if (winch->tty == tty) {
832                         free_winch(winch, 1);
833                         break;
834                 }
835         }
836         spin_unlock(&winch_handler_lock);
837 }
838
839 static void winch_cleanup(void)
840 {
841         struct list_head *ele, *next;
842         struct winch *winch;
843
844         spin_lock(&winch_handler_lock);
845
846         list_for_each_safe(ele, next, &winch_handlers) {
847                 winch = list_entry(ele, struct winch, list);
848                 free_winch(winch, 1);
849         }
850
851         spin_unlock(&winch_handler_lock);
852 }
853 __uml_exitcall(winch_cleanup);
854
855 char *add_xterm_umid(char *base)
856 {
857         char *umid, *title;
858         int len;
859
860         umid = get_umid();
861         if (*umid == '\0')
862                 return base;
863
864         len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
865         title = kmalloc(len, GFP_KERNEL);
866         if (title == NULL) {
867                 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
868                 return base;
869         }
870
871         snprintf(title, len, "%s (%s)", base, umid);
872         return title;
873 }