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