]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - arch/um/drivers/line.c
um: clean up the includes in ubd
[mirror_ubuntu-hirsute-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
d79a5809 412 spin_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
f71f9484 424 spin_unlock(&line->count_lock);
d14ad81f 425 err = enable_chan(line);
f71f9484 426 if (err) /* line_close() will be called by our caller */
d14ad81f
JD
427 return err;
428
d79a5809 429 INIT_DELAYED_WORK(&line->task, line_timer_cb);
165dc591 430
2f8a2dc2 431 if (!line->sigio) {
d79a5809
JD
432 chan_enable_winch(&line->chan_list, tty);
433 line->sigio = 1;
1da177e4 434 }
1da177e4 435
d79a5809
JD
436 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
437 &tty->winsize.ws_col);
438
f71f9484 439 return 0;
d79a5809
JD
440
441out_unlock:
442 spin_unlock(&line->count_lock);
b97b77cc 443 return err;
1da177e4
LT
444}
445
cd2ee4a3
JD
446static void unregister_winch(struct tty_struct *tty);
447
1da177e4
LT
448void line_close(struct tty_struct *tty, struct file * filp)
449{
450 struct line *line = tty->driver_data;
451
2f8a2dc2
JD
452 /*
453 * If line_open fails (and tty->driver_data is never set),
d79a5809
JD
454 * tty_open will call line_close. So just return in this case.
455 */
2f8a2dc2 456 if (line == NULL)
d79a5809 457 return;
b97b77cc
PBG
458
459 /* We ignore the error anyway! */
460 flush_buffer(line);
461
d79a5809 462 spin_lock(&line->count_lock);
f71f9484 463 BUG_ON(!line->valid);
cd2ee4a3 464
f71f9484 465 if (--line->count)
d79a5809
JD
466 goto out_unlock;
467
d79a5809
JD
468 line->tty = NULL;
469 tty->driver_data = NULL;
470
f71f9484
AV
471 spin_unlock(&line->count_lock);
472
2f8a2dc2 473 if (line->sigio) {
d79a5809
JD
474 unregister_winch(tty);
475 line->sigio = 0;
2f8a2dc2 476 }
cd2ee4a3 477
d79a5809
JD
478 return;
479
480out_unlock:
481 spin_unlock(&line->count_lock);
1da177e4
LT
482}
483
484void close_lines(struct line *lines, int nlines)
485{
486 int i;
487
488 for(i = 0; i < nlines; i++)
165dc591 489 close_chan(&lines[i].chan_list, 0);
1da177e4
LT
490}
491
f28169d2
JD
492static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
493 char **error_out)
d79a5809
JD
494{
495 struct line *line = &lines[n];
f28169d2 496 int err = -EINVAL;
d79a5809
JD
497
498 spin_lock(&line->count_lock);
499
f71f9484 500 if (line->count) {
f28169d2 501 *error_out = "Device is already open";
d79a5809
JD
502 goto out;
503 }
504
2f8a2dc2 505 if (line->init_pri <= init_prio) {
d79a5809
JD
506 line->init_pri = init_prio;
507 if (!strcmp(init, "none"))
508 line->valid = 0;
509 else {
510 line->init_str = init;
511 line->valid = 1;
512 }
513 }
f28169d2 514 err = 0;
d79a5809
JD
515out:
516 spin_unlock(&line->count_lock);
f28169d2 517 return err;
d79a5809
JD
518}
519
2f8a2dc2
JD
520/*
521 * Common setup code for both startup command line and mconsole initialization.
4b3f686d 522 * @lines contains the array (of size @num) to modify;
b97b77cc 523 * @init is the setup string;
f28169d2 524 * @error_out is an error string in the case of failure;
d571cd18 525 */
b97b77cc 526
f28169d2
JD
527int line_setup(struct line *lines, unsigned int num, char *init,
528 char **error_out)
1da177e4 529{
f28169d2 530 int i, n, err;
1da177e4
LT
531 char *end;
532
2f8a2dc2
JD
533 if (*init == '=') {
534 /*
535 * We said con=/ssl= instead of con#=, so we are configuring all
536 * consoles at once.
537 */
b97b77cc 538 n = -1;
d50084a2
JD
539 }
540 else {
1da177e4 541 n = simple_strtoul(init, &end, 0);
2f8a2dc2 542 if (*end != '=') {
f28169d2
JD
543 *error_out = "Couldn't parse device number";
544 return -EINVAL;
1da177e4
LT
545 }
546 init = end;
547 }
548 init++;
b97b77cc
PBG
549
550 if (n >= (signed int) num) {
f28169d2
JD
551 *error_out = "Device number out of range";
552 return -EINVAL;
553 }
2f8a2dc2 554 else if (n >= 0) {
f28169d2 555 err = setup_one_line(lines, n, init, INIT_ONE, error_out);
2f8a2dc2 556 if (err)
f28169d2 557 return err;
d50084a2 558 }
d50084a2 559 else {
2f8a2dc2 560 for(i = 0; i < num; i++) {
f28169d2
JD
561 err = setup_one_line(lines, i, init, INIT_ALL,
562 error_out);
2f8a2dc2 563 if (err)
f28169d2
JD
564 return err;
565 }
1da177e4 566 }
418e55d4 567 return n == -1 ? num : n;
1da177e4
LT
568}
569
1f80171e 570int line_config(struct line *lines, unsigned int num, char *str,
f28169d2 571 const struct chan_opts *opts, char **error_out)
1da177e4 572{
1f80171e 573 struct line *line;
970d6e3a 574 char *new;
418e55d4 575 int n;
1da177e4 576
2f8a2dc2 577 if (*str == '=') {
f28169d2
JD
578 *error_out = "Can't configure all devices from mconsole";
579 return -EINVAL;
d571cd18
JD
580 }
581
970d6e3a 582 new = kstrdup(str, GFP_KERNEL);
2f8a2dc2 583 if (new == NULL) {
f28169d2
JD
584 *error_out = "Failed to allocate memory";
585 return -ENOMEM;
1da177e4 586 }
f28169d2 587 n = line_setup(lines, num, new, error_out);
2f8a2dc2 588 if (n < 0)
f28169d2 589 return n;
1f80171e
JD
590
591 line = &lines[n];
f28169d2 592 return parse_chan_pair(line->init_str, line, n, opts, error_out);
1da177e4
LT
593}
594
b97b77cc 595int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
1da177e4
LT
596 int size, char **error_out)
597{
598 struct line *line;
599 char *end;
600 int dev, n = 0;
601
602 dev = simple_strtoul(name, &end, 0);
2f8a2dc2 603 if ((*end != '\0') || (end == name)) {
1da177e4 604 *error_out = "line_get_config failed to parse device number";
b97b77cc 605 return 0;
1da177e4
LT
606 }
607
2f8a2dc2 608 if ((dev < 0) || (dev >= num)) {
b97b77cc
PBG
609 *error_out = "device number out of range";
610 return 0;
1da177e4
LT
611 }
612
613 line = &lines[dev];
614
d79a5809 615 spin_lock(&line->count_lock);
2f8a2dc2 616 if (!line->valid)
1da177e4 617 CONFIG_CHUNK(str, size, n, "none", 1);
2f8a2dc2 618 else if (line->tty == NULL)
1da177e4
LT
619 CONFIG_CHUNK(str, size, n, line->init_str, 1);
620 else n = chan_config_string(&line->chan_list, str, size, error_out);
d79a5809 621 spin_unlock(&line->count_lock);
1da177e4 622
b97b77cc 623 return n;
1da177e4
LT
624}
625
29d56cfe
JD
626int line_id(char **str, int *start_out, int *end_out)
627{
628 char *end;
2f8a2dc2 629 int n;
29d56cfe
JD
630
631 n = simple_strtoul(*str, &end, 0);
2f8a2dc2
JD
632 if ((*end != '\0') || (end == *str))
633 return -1;
29d56cfe 634
2f8a2dc2
JD
635 *str = end;
636 *start_out = n;
637 *end_out = n;
638 return n;
29d56cfe
JD
639}
640
f28169d2 641int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
1da177e4 642{
418e55d4 643 int err;
1da177e4
LT
644 char config[sizeof("conxxxx=none\0")];
645
29d56cfe 646 sprintf(config, "%d=none", n);
f28169d2 647 err = line_setup(lines, num, config, error_out);
2f8a2dc2 648 if (err >= 0)
418e55d4
JD
649 err = 0;
650 return err;
1da177e4
LT
651}
652
d5c9ffc6
JD
653struct tty_driver *register_lines(struct line_driver *line_driver,
654 const struct tty_operations *ops,
655 struct line *lines, int nlines)
1da177e4
LT
656{
657 int i;
658 struct tty_driver *driver = alloc_tty_driver(nlines);
659
660 if (!driver)
661 return NULL;
662
663 driver->driver_name = line_driver->name;
664 driver->name = line_driver->device_name;
1da177e4
LT
665 driver->major = line_driver->major;
666 driver->minor_start = line_driver->minor_start;
667 driver->type = line_driver->type;
668 driver->subtype = line_driver->subtype;
669 driver->flags = TTY_DRIVER_REAL_RAW;
670 driver->init_termios = tty_std_termios;
671 tty_set_operations(driver, ops);
672
673 if (tty_register_driver(driver)) {
2f8a2dc2
JD
674 printk(KERN_ERR "register_lines : can't register %s driver\n",
675 line_driver->name);
1da177e4
LT
676 put_tty_driver(driver);
677 return NULL;
678 }
679
2f8a2dc2
JD
680 for(i = 0; i < nlines; i++) {
681 if (!lines[i].valid)
1da177e4
LT
682 tty_unregister_device(driver, i);
683 }
684
685 mconsole_register_dev(&line_driver->mc);
686 return driver;
687}
688
9010772c
JD
689static DEFINE_SPINLOCK(winch_handler_lock);
690static LIST_HEAD(winch_handlers);
605a69ac 691
1f80171e 692void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
1da177e4
LT
693{
694 struct line *line;
f28169d2 695 char *error;
1da177e4
LT
696 int i;
697
2f8a2dc2 698 for(i = 0; i < nlines; i++) {
1da177e4
LT
699 line = &lines[i];
700 INIT_LIST_HEAD(&line->chan_list);
9010772c 701
2f8a2dc2 702 if (line->init_str == NULL)
d50084a2
JD
703 continue;
704
705 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
2f8a2dc2
JD
706 if (line->init_str == NULL)
707 printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
1f80171e 708
2f8a2dc2
JD
709 if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
710 printk(KERN_ERR "parse_chan_pair failed for "
711 "device %d : %s\n", i, error);
1f80171e
JD
712 line->valid = 0;
713 }
1da177e4
LT
714 }
715}
716
717struct winch {
718 struct list_head list;
719 int fd;
720 int tty_fd;
721 int pid;
722 struct tty_struct *tty;
42a359e3 723 unsigned long stack;
7cf3cf21 724 struct work_struct work;
1da177e4
LT
725};
726
7cf3cf21 727static void __free_winch(struct work_struct *work)
42a359e3 728{
7cf3cf21
AV
729 struct winch *winch = container_of(work, struct winch, work);
730 free_irq(WINCH_IRQ, winch);
42a359e3
JD
731
732 if (winch->pid != -1)
733 os_kill_process(winch->pid, 1);
42a359e3
JD
734 if (winch->stack != 0)
735 free_stack(winch->stack, 0);
42a359e3
JD
736 kfree(winch);
737}
738
7cf3cf21
AV
739static void free_winch(struct winch *winch)
740{
741 int fd = winch->fd;
742 winch->fd = -1;
743 if (fd != -1)
744 os_close_file(fd);
745 list_del(&winch->list);
746 __free_winch(&winch->work);
747}
748
7bea96fd 749static irqreturn_t winch_interrupt(int irq, void *data)
1da177e4
LT
750{
751 struct winch *winch = data;
752 struct tty_struct *tty;
753 struct line *line;
7cf3cf21 754 int fd = winch->fd;
1da177e4
LT
755 int err;
756 char c;
757
7cf3cf21
AV
758 if (fd != -1) {
759 err = generic_read(fd, &c, NULL);
2f8a2dc2
JD
760 if (err < 0) {
761 if (err != -EAGAIN) {
7cf3cf21
AV
762 winch->fd = -1;
763 list_del(&winch->list);
764 os_close_file(fd);
2f8a2dc2
JD
765 printk(KERN_ERR "winch_interrupt : "
766 "read failed, errno = %d\n", -err);
767 printk(KERN_ERR "fd %d is losing SIGWINCH "
768 "support\n", winch->tty_fd);
7cf3cf21
AV
769 INIT_WORK(&winch->work, __free_winch);
770 schedule_work(&winch->work);
b97b77cc 771 return IRQ_HANDLED;
1da177e4
LT
772 }
773 goto out;
774 }
775 }
42a359e3 776 tty = winch->tty;
1da177e4
LT
777 if (tty != NULL) {
778 line = tty->driver_data;
438ee679
JD
779 if (line != NULL) {
780 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
781 &tty->winsize.ws_col);
782 kill_pgrp(tty->pgrp, SIGWINCH, 1);
783 }
1da177e4
LT
784 }
785 out:
2f8a2dc2 786 if (winch->fd != -1)
1da177e4 787 reactivate_fd(winch->fd, WINCH_IRQ);
b97b77cc 788 return IRQ_HANDLED;
1da177e4
LT
789}
790
42a359e3
JD
791void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
792 unsigned long stack)
1da177e4
LT
793{
794 struct winch *winch;
795
1da177e4
LT
796 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
797 if (winch == NULL) {
2f8a2dc2 798 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
42a359e3 799 goto cleanup;
1da177e4 800 }
605a69ac 801
1da177e4
LT
802 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
803 .fd = fd,
804 .tty_fd = tty_fd,
805 .pid = pid,
42a359e3
JD
806 .tty = tty,
807 .stack = stack });
808
809 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
c0b79a90 810 IRQF_SHARED | IRQF_SAMPLE_RANDOM,
42a359e3 811 "winch", winch) < 0) {
2f8a2dc2
JD
812 printk(KERN_ERR "register_winch_irq - failed to register "
813 "IRQ\n");
42a359e3
JD
814 goto out_free;
815 }
605a69ac
PBG
816
817 spin_lock(&winch_handler_lock);
1da177e4 818 list_add(&winch->list, &winch_handlers);
605a69ac
PBG
819 spin_unlock(&winch_handler_lock);
820
42a359e3 821 return;
e464bf2b 822
42a359e3 823 out_free:
e464bf2b 824 kfree(winch);
42a359e3
JD
825 cleanup:
826 os_kill_process(pid, 1);
827 os_close_file(fd);
828 if (stack != 0)
829 free_stack(stack, 0);
e464bf2b
JD
830}
831
cd2ee4a3
JD
832static void unregister_winch(struct tty_struct *tty)
833{
48a0b740 834 struct list_head *ele, *next;
e464bf2b 835 struct winch *winch;
cd2ee4a3 836
605a69ac 837 spin_lock(&winch_handler_lock);
e464bf2b 838
48a0b740 839 list_for_each_safe(ele, next, &winch_handlers) {
cd2ee4a3 840 winch = list_entry(ele, struct winch, list);
2f8a2dc2 841 if (winch->tty == tty) {
7cf3cf21 842 free_winch(winch);
e464bf2b 843 break;
2f8a2dc2
JD
844 }
845 }
605a69ac 846 spin_unlock(&winch_handler_lock);
cd2ee4a3
JD
847}
848
1da177e4
LT
849static void winch_cleanup(void)
850{
e464bf2b 851 struct list_head *ele, *next;
1da177e4
LT
852 struct winch *winch;
853
e464bf2b
JD
854 spin_lock(&winch_handler_lock);
855
2f8a2dc2 856 list_for_each_safe(ele, next, &winch_handlers) {
1da177e4 857 winch = list_entry(ele, struct winch, list);
7cf3cf21 858 free_winch(winch);
1da177e4 859 }
e464bf2b
JD
860
861 spin_unlock(&winch_handler_lock);
1da177e4
LT
862}
863__uml_exitcall(winch_cleanup);
864
865char *add_xterm_umid(char *base)
866{
867 char *umid, *title;
868 int len;
869
7eebe8a9 870 umid = get_umid();
2f8a2dc2 871 if (*umid == '\0')
b97b77cc 872 return base;
165dc591 873
1da177e4
LT
874 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
875 title = kmalloc(len, GFP_KERNEL);
2f8a2dc2
JD
876 if (title == NULL) {
877 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
b97b77cc 878 return base;
1da177e4
LT
879 }
880
881 snprintf(title, len, "%s (%s)", base, umid);
b97b77cc 882 return title;
1da177e4 883}