Merge tag 'sched_ext-for-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[linux-2.6-block.git] / arch / um / drivers / line.c
CommitLineData
dbddf429 1// SPDX-License-Identifier: GPL-2.0
165dc591 2/*
2f8a2dc2 3 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
1da177e4
LT
4 */
5
37185b33
AV
6#include <linux/irqreturn.h>
7#include <linux/kd.h>
3f07c014 8#include <linux/sched/signal.h>
37185b33 9#include <linux/slab.h>
3f07c014 10
510c72a3 11#include "chan.h"
37185b33
AV
12#include <irq_kern.h>
13#include <irq_user.h>
14#include <kern_util.h>
15#include <os.h>
1da177e4
LT
16
17#define LINE_BUFSIZE 4096
18
7bea96fd 19static irqreturn_t line_interrupt(int irq, void *data)
1da177e4 20{
165dc591
JD
21 struct chan *chan = data;
22 struct line *line = chan->line;
1da177e4
LT
23
24 if (line)
2e124b4a
JS
25 chan_interrupt(line, irq);
26
1da177e4
LT
27 return IRQ_HANDLED;
28}
29
2f8a2dc2
JD
30/*
31 * Returns the free space inside the ring buffer of this line.
b97b77cc 32 *
b60745b9 33 * Should be called while holding line->lock (this does not modify data).
b97b77cc 34 */
03b3b1a2 35static unsigned int write_room(struct line *line)
1da177e4
LT
36{
37 int n;
38
b97b77cc
PBG
39 if (line->buffer == NULL)
40 return LINE_BUFSIZE - 1;
41
42 /* This is for the case where the buffer is wrapped! */
43 n = line->head - line->tail;
1da177e4 44
1da177e4 45 if (n <= 0)
acb2cf34 46 n += LINE_BUFSIZE; /* The other case */
b97b77cc
PBG
47 return n - 1;
48}
49
03b3b1a2 50unsigned int line_write_room(struct tty_struct *tty)
b97b77cc
PBG
51{
52 struct line *line = tty->driver_data;
53 unsigned long flags;
03b3b1a2 54 unsigned int room;
b97b77cc 55
b97b77cc
PBG
56 spin_lock_irqsave(&line->lock, flags);
57 room = write_room(line);
58 spin_unlock_irqrestore(&line->lock, flags);
59
b97b77cc 60 return room;
1da177e4
LT
61}
62
fff4ef17 63unsigned int line_chars_in_buffer(struct tty_struct *tty)
b97b77cc
PBG
64{
65 struct line *line = tty->driver_data;
66 unsigned long flags;
fff4ef17 67 unsigned int ret;
b97b77cc
PBG
68
69 spin_lock_irqsave(&line->lock, flags);
acb2cf34 70 /* write_room subtracts 1 for the needed NULL, so we readd it.*/
b97b77cc
PBG
71 ret = LINE_BUFSIZE - (write_room(line) + 1);
72 spin_unlock_irqrestore(&line->lock, flags);
73
74 return ret;
75}
76
77/*
78 * This copies the content of buf into the circular buffer associated with
79 * this line.
80 * The return value is the number of characters actually copied, i.e. the ones
81 * for which there was space: this function is not supposed to ever flush out
82 * the circular buffer.
83 *
84 * Must be called while holding line->lock!
85 */
b49d1849 86static int buffer_data(struct line *line, const u8 *buf, size_t len)
1da177e4
LT
87{
88 int end, room;
89
2f8a2dc2 90 if (line->buffer == NULL) {
1da177e4
LT
91 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
92 if (line->buffer == NULL) {
2f8a2dc2
JD
93 printk(KERN_ERR "buffer_data - atomic allocation "
94 "failed\n");
95 return 0;
1da177e4
LT
96 }
97 line->head = line->buffer;
98 line->tail = line->buffer;
99 }
100
101 room = write_room(line);
102 len = (len > room) ? room : len;
103
104 end = line->buffer + LINE_BUFSIZE - line->tail;
b97b77cc 105
2f8a2dc2 106 if (len < end) {
1da177e4
LT
107 memcpy(line->tail, buf, len);
108 line->tail += len;
d50084a2
JD
109 }
110 else {
b97b77cc 111 /* The circular buffer is wrapping */
1da177e4
LT
112 memcpy(line->tail, buf, end);
113 buf += end;
114 memcpy(line->buffer, buf, len - end);
115 line->tail = line->buffer + len - end;
116 }
117
b97b77cc 118 return len;
1da177e4
LT
119}
120
b97b77cc
PBG
121/*
122 * Flushes the ring buffer to the output channels. That is, write_chan is
123 * called, passing it line->head as buffer, and an appropriate count.
124 *
125 * On exit, returns 1 when the buffer is empty,
126 * 0 when the buffer is not empty on exit,
127 * and -errno when an error occurred.
128 *
129 * Must be called while holding line->lock!*/
1da177e4
LT
130static int flush_buffer(struct line *line)
131{
132 int n, count;
133
134 if ((line->buffer == NULL) || (line->head == line->tail))
b97b77cc 135 return 1;
1da177e4
LT
136
137 if (line->tail < line->head) {
b97b77cc 138 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
1da177e4 139 count = line->buffer + LINE_BUFSIZE - line->head;
b97b77cc 140
bed5e39c 141 n = write_chan(line->chan_out, line->head, count,
d5a9597d 142 line->write_irq);
1da177e4 143 if (n < 0)
b97b77cc
PBG
144 return n;
145 if (n == count) {
2f8a2dc2
JD
146 /*
147 * We have flushed from ->head to buffer end, now we
148 * must flush only from the beginning to ->tail.
149 */
1da177e4 150 line->head = line->buffer;
b97b77cc 151 } else {
1da177e4 152 line->head += n;
b97b77cc 153 return 0;
1da177e4
LT
154 }
155 }
156
157 count = line->tail - line->head;
bed5e39c 158 n = write_chan(line->chan_out, line->head, count,
d5a9597d 159 line->write_irq);
b97b77cc 160
2f8a2dc2 161 if (n < 0)
b97b77cc 162 return n;
1da177e4
LT
163
164 line->head += n;
b97b77cc
PBG
165 return line->head == line->tail;
166}
167
168void line_flush_buffer(struct tty_struct *tty)
169{
170 struct line *line = tty->driver_data;
171 unsigned long flags;
b97b77cc 172
b97b77cc 173 spin_lock_irqsave(&line->lock, flags);
f1c93e49 174 flush_buffer(line);
b97b77cc 175 spin_unlock_irqrestore(&line->lock, flags);
b97b77cc
PBG
176}
177
2f8a2dc2
JD
178/*
179 * We map both ->flush_chars and ->put_char (which go in pair) onto
180 * ->flush_buffer and ->write. Hope it's not that bad.
181 */
b97b77cc
PBG
182void line_flush_chars(struct tty_struct *tty)
183{
184 line_flush_buffer(tty);
185}
186
95713967 187ssize_t line_write(struct tty_struct *tty, const u8 *buf, size_t len)
1da177e4
LT
188{
189 struct line *line = tty->driver_data;
190 unsigned long flags;
c59dbcad 191 int n, ret = 0;
1da177e4 192
b97b77cc 193 spin_lock_irqsave(&line->lock, flags);
c59dbcad 194 if (line->head != line->tail)
1da177e4 195 ret = buffer_data(line, buf, len);
c59dbcad 196 else {
bed5e39c 197 n = write_chan(line->chan_out, buf, len,
d5a9597d 198 line->write_irq);
b97b77cc 199 if (n < 0) {
1da177e4
LT
200 ret = n;
201 goto out_up;
202 }
203
204 len -= n;
205 ret += n;
b97b77cc 206 if (len > 0)
1da177e4
LT
207 ret += buffer_data(line, buf + n, len);
208 }
b97b77cc
PBG
209out_up:
210 spin_unlock_irqrestore(&line->lock, flags);
211 return ret;
1da177e4
LT
212}
213
e4dcee80
JD
214void line_throttle(struct tty_struct *tty)
215{
216 struct line *line = tty->driver_data;
217
d5a9597d 218 deactivate_chan(line->chan_in, line->read_irq);
e4dcee80
JD
219 line->throttled = 1;
220}
221
222void line_unthrottle(struct tty_struct *tty)
223{
224 struct line *line = tty->driver_data;
225
226 line->throttled = 0;
d5a9597d 227 chan_interrupt(line, line->read_irq);
e4dcee80
JD
228}
229
7bea96fd 230static irqreturn_t line_write_interrupt(int irq, void *data)
1da177e4 231{
165dc591
JD
232 struct chan *chan = data;
233 struct line *line = chan->line;
1da177e4
LT
234 int err;
235
2f8a2dc2 236 /*
c0b79a90
YZ
237 * Interrupts are disabled here because genirq keep irqs disabled when
238 * calling the action handler.
2f8a2dc2 239 */
b97b77cc 240
ec0ac8ad 241 spin_lock(&line->lock);
1da177e4 242 err = flush_buffer(line);
b97b77cc 243 if (err == 0) {
199eebba 244 spin_unlock(&line->lock);
b97b77cc 245 return IRQ_NONE;
917e2fd2 246 } else if ((err < 0) && (err != -EAGAIN)) {
1da177e4
LT
247 line->head = line->buffer;
248 line->tail = line->buffer;
249 }
ec0ac8ad 250 spin_unlock(&line->lock);
1da177e4 251
6aad04f2 252 tty_port_tty_wakeup(&line->port);
6fc58845 253
b97b77cc 254 return IRQ_HANDLED;
1da177e4
LT
255}
256
165dc591 257int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
1da177e4 258{
5e7672ec 259 const struct line_driver *driver = line->driver;
36d46a59 260 int err;
1da177e4 261
36d46a59 262 if (input) {
d5a9597d
JB
263 err = um_request_irq(UM_IRQ_ALLOC, fd, IRQ_READ,
264 line_interrupt, 0,
aab94460 265 driver->read_irq_name, data);
36d46a59
JB
266 if (err < 0)
267 return err;
d5a9597d
JB
268
269 line->read_irq = err;
36d46a59
JB
270 }
271
272 if (output) {
d5a9597d
JB
273 err = um_request_irq(UM_IRQ_ALLOC, fd, IRQ_WRITE,
274 line_write_interrupt, 0,
aab94460 275 driver->write_irq_name, data);
36d46a59
JB
276 if (err < 0)
277 return err;
d5a9597d
JB
278
279 line->write_irq = err;
36d46a59
JB
280 }
281
282 return 0;
1da177e4
LT
283}
284
79e0273d 285static int line_activate(struct tty_port *port, struct tty_struct *tty)
1da177e4 286{
79e0273d
RW
287 int ret;
288 struct line *line = tty->driver_data;
165dc591 289
79e0273d
RW
290 ret = enable_chan(line);
291 if (ret)
292 return ret;
d14ad81f 293
2f8a2dc2 294 if (!line->sigio) {
2116bda6 295 chan_enable_winch(line->chan_out, port);
d79a5809 296 line->sigio = 1;
1da177e4 297 }
1da177e4 298
bed5e39c 299 chan_window_size(line, &tty->winsize.ws_row,
79e0273d
RW
300 &tty->winsize.ws_col);
301
302 return 0;
1da177e4
LT
303}
304
cc4f0248
RW
305static void unregister_winch(struct tty_struct *tty);
306
307static void line_destruct(struct tty_port *port)
308{
309 struct tty_struct *tty = tty_port_tty_get(port);
310 struct line *line = tty->driver_data;
311
312 if (line->sigio) {
313 unregister_winch(tty);
314 line->sigio = 0;
315 }
316}
317
79e0273d
RW
318static const struct tty_port_operations line_port_ops = {
319 .activate = line_activate,
cc4f0248 320 .destruct = line_destruct,
79e0273d 321};
cd2ee4a3 322
79e0273d 323int line_open(struct tty_struct *tty, struct file *filp)
1da177e4
LT
324{
325 struct line *line = tty->driver_data;
326
79e0273d
RW
327 return tty_port_open(&line->port, tty, filp);
328}
b97b77cc 329
79e0273d
RW
330int line_install(struct tty_driver *driver, struct tty_struct *tty,
331 struct line *line)
332{
333 int ret;
b97b77cc 334
79e0273d
RW
335 ret = tty_standard_install(driver, tty);
336 if (ret)
337 return ret;
cd2ee4a3 338
79e0273d 339 tty->driver_data = line;
d79a5809 340
79e0273d
RW
341 return 0;
342}
343
79e0273d
RW
344void line_close(struct tty_struct *tty, struct file * filp)
345{
346 struct line *line = tty->driver_data;
cd2ee4a3 347
79e0273d
RW
348 tty_port_close(&line->port, tty, filp);
349}
350
351void line_hangup(struct tty_struct *tty)
352{
353 struct line *line = tty->driver_data;
354
355 tty_port_hangup(&line->port);
1da177e4
LT
356}
357
358void close_lines(struct line *lines, int nlines)
359{
360 int i;
361
362 for(i = 0; i < nlines; i++)
10c890c0 363 close_chan(&lines[i]);
1da177e4
LT
364}
365
04292b2c
AV
366int setup_one_line(struct line *lines, int n, char *init,
367 const struct chan_opts *opts, char **error_out)
d79a5809
JD
368{
369 struct line *line = &lines[n];
cfe6b7c7 370 struct tty_driver *driver = line->driver->driver;
f28169d2 371 int err = -EINVAL;
d79a5809 372
060ed31d 373 if (line->port.count) {
f28169d2 374 *error_out = "Device is already open";
d79a5809
JD
375 goto out;
376 }
377
31efcebb
AV
378 if (!strcmp(init, "none")) {
379 if (line->valid) {
380 line->valid = 0;
381 kfree(line->init_str);
cfe6b7c7 382 tty_unregister_device(driver, n);
31efcebb
AV
383 parse_chan_pair(NULL, line, n, opts, error_out);
384 err = 0;
385 }
824ac4a5 386 *error_out = "configured as 'none'";
31efcebb
AV
387 } else {
388 char *new = kstrdup(init, GFP_KERNEL);
389 if (!new) {
390 *error_out = "Failed to allocate memory";
391 return -ENOMEM;
392 }
c8e2876f 393 if (line->valid) {
cfe6b7c7 394 tty_unregister_device(driver, n);
c8e2876f
AV
395 kfree(line->init_str);
396 }
31efcebb 397 line->init_str = new;
43574c1a 398 line->valid = 1;
31efcebb 399 err = parse_chan_pair(new, line, n, opts, error_out);
cfe6b7c7 400 if (!err) {
734cc178
JS
401 struct device *d = tty_port_register_device(&line->port,
402 driver, n, NULL);
cfe6b7c7
AV
403 if (IS_ERR(d)) {
404 *error_out = "Failed to register device";
405 err = PTR_ERR(d);
406 parse_chan_pair(NULL, line, n, opts, error_out);
407 }
408 }
31efcebb 409 if (err) {
824ac4a5 410 *error_out = "failed to parse channel pair";
31efcebb
AV
411 line->init_str = NULL;
412 line->valid = 0;
413 kfree(new);
414 }
d79a5809
JD
415 }
416out:
f28169d2 417 return err;
d79a5809
JD
418}
419
2f8a2dc2
JD
420/*
421 * Common setup code for both startup command line and mconsole initialization.
4b3f686d 422 * @lines contains the array (of size @num) to modify;
b97b77cc 423 * @init is the setup string;
f28169d2 424 * @error_out is an error string in the case of failure;
d571cd18 425 */
b97b77cc 426
43574c1a
AV
427int line_setup(char **conf, unsigned int num, char **def,
428 char *init, char *name)
1da177e4 429{
43574c1a 430 char *error;
1da177e4 431
2f8a2dc2
JD
432 if (*init == '=') {
433 /*
434 * We said con=/ssl= instead of con#=, so we are configuring all
435 * consoles at once.
436 */
43574c1a
AV
437 *def = init + 1;
438 } else {
439 char *end;
440 unsigned n = simple_strtoul(init, &end, 0);
441
2f8a2dc2 442 if (*end != '=') {
43574c1a
AV
443 error = "Couldn't parse device number";
444 goto out;
1da177e4 445 }
43574c1a
AV
446 if (n >= num) {
447 error = "Device number out of range";
448 goto out;
f28169d2 449 }
43574c1a 450 conf[n] = end + 1;
1da177e4 451 }
43574c1a
AV
452 return 0;
453
454out:
455 printk(KERN_ERR "Failed to set up %s with "
456 "configuration string \"%s\" : %s\n", name, init, error);
457 return -EINVAL;
1da177e4
LT
458}
459
1f80171e 460int line_config(struct line *lines, unsigned int num, char *str,
f28169d2 461 const struct chan_opts *opts, char **error_out)
1da177e4 462{
fe9a6b00 463 char *end;
31efcebb 464 int n;
1da177e4 465
2f8a2dc2 466 if (*str == '=') {
f28169d2
JD
467 *error_out = "Can't configure all devices from mconsole";
468 return -EINVAL;
d571cd18
JD
469 }
470
fe9a6b00
AV
471 n = simple_strtoul(str, &end, 0);
472 if (*end++ != '=') {
473 *error_out = "Couldn't parse device number";
474 return -EINVAL;
475 }
476 if (n >= num) {
477 *error_out = "Device number out of range";
478 return -EINVAL;
479 }
480
31efcebb 481 return setup_one_line(lines, n, end, opts, error_out);
1da177e4
LT
482}
483
b97b77cc 484int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
1da177e4
LT
485 int size, char **error_out)
486{
487 struct line *line;
488 char *end;
489 int dev, n = 0;
490
491 dev = simple_strtoul(name, &end, 0);
2f8a2dc2 492 if ((*end != '\0') || (end == name)) {
1da177e4 493 *error_out = "line_get_config failed to parse device number";
b97b77cc 494 return 0;
1da177e4
LT
495 }
496
2f8a2dc2 497 if ((dev < 0) || (dev >= num)) {
b97b77cc
PBG
498 *error_out = "device number out of range";
499 return 0;
1da177e4
LT
500 }
501
502 line = &lines[dev];
503
2f8a2dc2 504 if (!line->valid)
1da177e4 505 CONFIG_CHUNK(str, size, n, "none", 1);
6fc58845
JS
506 else {
507 struct tty_struct *tty = tty_port_tty_get(&line->port);
508 if (tty == NULL) {
509 CONFIG_CHUNK(str, size, n, line->init_str, 1);
510 } else {
511 n = chan_config_string(line, str, size, error_out);
512 tty_kref_put(tty);
513 }
514 }
1da177e4 515
b97b77cc 516 return n;
1da177e4
LT
517}
518
29d56cfe
JD
519int line_id(char **str, int *start_out, int *end_out)
520{
521 char *end;
2f8a2dc2 522 int n;
29d56cfe
JD
523
524 n = simple_strtoul(*str, &end, 0);
2f8a2dc2
JD
525 if ((*end != '\0') || (end == *str))
526 return -1;
29d56cfe 527
2f8a2dc2
JD
528 *str = end;
529 *start_out = n;
530 *end_out = n;
531 return n;
29d56cfe
JD
532}
533
f28169d2 534int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
1da177e4 535{
da645f3b
AV
536 if (n >= num) {
537 *error_out = "Device number out of range";
538 return -EINVAL;
539 }
31efcebb 540 return setup_one_line(lines, n, "none", NULL, error_out);
1da177e4
LT
541}
542
cfe6b7c7
AV
543int register_lines(struct line_driver *line_driver,
544 const struct tty_operations *ops,
545 struct line *lines, int nlines)
1da177e4 546{
39b7b42b 547 struct tty_driver *driver;
cfe6b7c7 548 int err;
04292b2c 549 int i;
1da177e4 550
39b7b42b
JS
551 driver = tty_alloc_driver(nlines, TTY_DRIVER_REAL_RAW |
552 TTY_DRIVER_DYNAMIC_DEV);
553 if (IS_ERR(driver))
554 return PTR_ERR(driver);
1da177e4
LT
555
556 driver->driver_name = line_driver->name;
557 driver->name = line_driver->device_name;
1da177e4
LT
558 driver->major = line_driver->major;
559 driver->minor_start = line_driver->minor_start;
560 driver->type = line_driver->type;
561 driver->subtype = line_driver->subtype;
1da177e4 562 driver->init_termios = tty_std_termios;
39b7b42b 563
04292b2c 564 for (i = 0; i < nlines; i++) {
060ed31d 565 tty_port_init(&lines[i].port);
79e0273d 566 lines[i].port.ops = &line_port_ops;
04292b2c 567 spin_lock_init(&lines[i].lock);
04292b2c
AV
568 lines[i].driver = line_driver;
569 INIT_LIST_HEAD(&lines[i].chan_list);
570 }
1da177e4
LT
571 tty_set_operations(driver, ops);
572
cfe6b7c7
AV
573 err = tty_register_driver(driver);
574 if (err) {
2f8a2dc2
JD
575 printk(KERN_ERR "register_lines : can't register %s driver\n",
576 line_driver->name);
9f90a4dd 577 tty_driver_kref_put(driver);
191c5f10
JS
578 for (i = 0; i < nlines; i++)
579 tty_port_destroy(&lines[i].port);
cfe6b7c7 580 return err;
1da177e4
LT
581 }
582
cfe6b7c7 583 line_driver->driver = driver;
1da177e4 584 mconsole_register_dev(&line_driver->mc);
cfe6b7c7 585 return 0;
1da177e4
LT
586}
587
9010772c
JD
588static DEFINE_SPINLOCK(winch_handler_lock);
589static LIST_HEAD(winch_handlers);
605a69ac 590
1da177e4
LT
591struct winch {
592 struct list_head list;
593 int fd;
594 int tty_fd;
595 int pid;
2116bda6 596 struct tty_port *port;
42a359e3 597 unsigned long stack;
7cf3cf21 598 struct work_struct work;
1da177e4
LT
599};
600
7cf3cf21 601static void __free_winch(struct work_struct *work)
42a359e3 602{
7cf3cf21 603 struct winch *winch = container_of(work, struct winch, work);
fa7a0449 604 um_free_irq(WINCH_IRQ, winch);
42a359e3
JD
605
606 if (winch->pid != -1)
607 os_kill_process(winch->pid, 1);
42a359e3
JD
608 if (winch->stack != 0)
609 free_stack(winch->stack, 0);
42a359e3
JD
610 kfree(winch);
611}
612
7cf3cf21
AV
613static void free_winch(struct winch *winch)
614{
615 int fd = winch->fd;
616 winch->fd = -1;
617 if (fd != -1)
618 os_close_file(fd);
7cf3cf21
AV
619 __free_winch(&winch->work);
620}
621
7bea96fd 622static irqreturn_t winch_interrupt(int irq, void *data)
1da177e4
LT
623{
624 struct winch *winch = data;
625 struct tty_struct *tty;
626 struct line *line;
7cf3cf21 627 int fd = winch->fd;
1da177e4
LT
628 int err;
629 char c;
8a8a5510 630 struct pid *pgrp;
1da177e4 631
7cf3cf21
AV
632 if (fd != -1) {
633 err = generic_read(fd, &c, NULL);
139e6e8e
BB
634 /* A read of 2 means the winch thread failed and has warned */
635 if (err < 0 || (err == 1 && c == 2)) {
2f8a2dc2 636 if (err != -EAGAIN) {
7cf3cf21
AV
637 winch->fd = -1;
638 list_del(&winch->list);
639 os_close_file(fd);
139e6e8e
BB
640 if (err < 0) {
641 printk(KERN_ERR "winch_interrupt : read failed, errno = %d\n",
642 -err);
643 printk(KERN_ERR "fd %d is losing SIGWINCH support\n",
644 winch->tty_fd);
645 }
7cf3cf21
AV
646 INIT_WORK(&winch->work, __free_winch);
647 schedule_work(&winch->work);
b97b77cc 648 return IRQ_HANDLED;
1da177e4
LT
649 }
650 goto out;
651 }
652 }
2116bda6 653 tty = tty_port_tty_get(winch->port);
1da177e4
LT
654 if (tty != NULL) {
655 line = tty->driver_data;
438ee679 656 if (line != NULL) {
bed5e39c 657 chan_window_size(line, &tty->winsize.ws_row,
438ee679 658 &tty->winsize.ws_col);
8a8a5510
PH
659 pgrp = tty_get_pgrp(tty);
660 if (pgrp)
661 kill_pgrp(pgrp, SIGWINCH, 1);
662 put_pid(pgrp);
438ee679 663 }
2116bda6 664 tty_kref_put(tty);
1da177e4
LT
665 }
666 out:
b97b77cc 667 return IRQ_HANDLED;
1da177e4
LT
668}
669
2116bda6 670void register_winch_irq(int fd, int tty_fd, int pid, struct tty_port *port,
42a359e3 671 unsigned long stack)
1da177e4
LT
672{
673 struct winch *winch;
674
1da177e4
LT
675 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
676 if (winch == NULL) {
2f8a2dc2 677 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
42a359e3 678 goto cleanup;
1da177e4 679 }
605a69ac 680
a0fbbd36 681 *winch = ((struct winch) { .fd = fd,
1da177e4
LT
682 .tty_fd = tty_fd,
683 .pid = pid,
2116bda6 684 .port = port,
42a359e3
JD
685 .stack = stack });
686
a0fbbd36
RS
687 spin_lock(&winch_handler_lock);
688 list_add(&winch->list, &winch_handlers);
689 spin_unlock(&winch_handler_lock);
690
42a359e3 691 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
aab94460 692 IRQF_SHARED, "winch", winch) < 0) {
2f8a2dc2
JD
693 printk(KERN_ERR "register_winch_irq - failed to register "
694 "IRQ\n");
a0fbbd36
RS
695 spin_lock(&winch_handler_lock);
696 list_del(&winch->list);
697 spin_unlock(&winch_handler_lock);
42a359e3
JD
698 goto out_free;
699 }
605a69ac 700
42a359e3 701 return;
e464bf2b 702
42a359e3 703 out_free:
e464bf2b 704 kfree(winch);
42a359e3
JD
705 cleanup:
706 os_kill_process(pid, 1);
707 os_close_file(fd);
708 if (stack != 0)
709 free_stack(stack, 0);
e464bf2b
JD
710}
711
cd2ee4a3
JD
712static void unregister_winch(struct tty_struct *tty)
713{
48a0b740 714 struct list_head *ele, *next;
e464bf2b 715 struct winch *winch;
2116bda6 716 struct tty_struct *wtty;
cd2ee4a3 717
605a69ac 718 spin_lock(&winch_handler_lock);
e464bf2b 719
48a0b740 720 list_for_each_safe(ele, next, &winch_handlers) {
cd2ee4a3 721 winch = list_entry(ele, struct winch, list);
2116bda6
RW
722 wtty = tty_port_tty_get(winch->port);
723 if (wtty == tty) {
f4ab7818
JB
724 list_del(&winch->list);
725 spin_unlock(&winch_handler_lock);
7cf3cf21 726 free_winch(winch);
e464bf2b 727 break;
2f8a2dc2 728 }
2116bda6 729 tty_kref_put(wtty);
2f8a2dc2 730 }
605a69ac 731 spin_unlock(&winch_handler_lock);
cd2ee4a3
JD
732}
733
1da177e4
LT
734static void winch_cleanup(void)
735{
1da177e4
LT
736 struct winch *winch;
737
e464bf2b 738 spin_lock(&winch_handler_lock);
f4ab7818
JB
739 while ((winch = list_first_entry_or_null(&winch_handlers,
740 struct winch, list))) {
741 list_del(&winch->list);
742 spin_unlock(&winch_handler_lock);
e464bf2b 743
7cf3cf21 744 free_winch(winch);
f4ab7818
JB
745
746 spin_lock(&winch_handler_lock);
1da177e4 747 }
e464bf2b
JD
748
749 spin_unlock(&winch_handler_lock);
1da177e4
LT
750}
751__uml_exitcall(winch_cleanup);
752
753char *add_xterm_umid(char *base)
754{
755 char *umid, *title;
756 int len;
757
7eebe8a9 758 umid = get_umid();
2f8a2dc2 759 if (*umid == '\0')
b97b77cc 760 return base;
165dc591 761
1da177e4
LT
762 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
763 title = kmalloc(len, GFP_KERNEL);
2f8a2dc2
JD
764 if (title == NULL) {
765 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
b97b77cc 766 return base;
1da177e4
LT
767 }
768
769 snprintf(title, len, "%s (%s)", base, umid);
b97b77cc 770 return title;
1da177e4 771}