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