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