2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include "linux/sched.h"
7 #include "linux/slab.h"
8 #include "linux/list.h"
10 #include "linux/interrupt.h"
11 #include "asm/uaccess.h"
12 #include "chan_kern.h"
16 #include "user_util.h"
17 #include "kern_util.h"
21 #define LINE_BUFSIZE 4096
23 static irqreturn_t line_interrupt(int irq, void *data, struct pt_regs *unused)
25 struct chan *chan = data;
26 struct line *line = chan->line;
27 struct tty_struct *tty = line->tty;
30 chan_interrupt(&line->chan_list, &line->task, tty, irq);
34 static void line_timer_cb(void *arg)
36 struct line *line = arg;
39 chan_interrupt(&line->chan_list, &line->task, line->tty,
40 line->driver->read_irq);
43 /* Returns the free space inside the ring buffer of this line.
45 * Should be called while holding line->lock (this does not modify datas).
47 static int write_room(struct line *line)
51 if (line->buffer == NULL)
52 return LINE_BUFSIZE - 1;
54 /* This is for the case where the buffer is wrapped! */
55 n = line->head - line->tail;
58 n = LINE_BUFSIZE + n; /* The other case */
62 int line_write_room(struct tty_struct *tty)
64 struct line *line = tty->driver_data;
71 spin_lock_irqsave(&line->lock, flags);
72 room = write_room(line);
73 spin_unlock_irqrestore(&line->lock, flags);
75 /*XXX: Warning to remove */
77 printk(KERN_DEBUG "%s: %s: no room left in buffer\n",
78 __FUNCTION__,tty->name);
82 int line_chars_in_buffer(struct tty_struct *tty)
84 struct line *line = tty->driver_data;
88 spin_lock_irqsave(&line->lock, flags);
90 /*write_room subtracts 1 for the needed NULL, so we readd it.*/
91 ret = LINE_BUFSIZE - (write_room(line) + 1);
92 spin_unlock_irqrestore(&line->lock, flags);
98 * This copies the content of buf into the circular buffer associated with
100 * The return value is the number of characters actually copied, i.e. the ones
101 * for which there was space: this function is not supposed to ever flush out
102 * the circular buffer.
104 * Must be called while holding line->lock!
106 static int buffer_data(struct line *line, const char *buf, int len)
110 if(line->buffer == NULL){
111 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
112 if (line->buffer == NULL) {
113 printk("buffer_data - atomic allocation failed\n");
116 line->head = line->buffer;
117 line->tail = line->buffer;
120 room = write_room(line);
121 len = (len > room) ? room : len;
123 end = line->buffer + LINE_BUFSIZE - line->tail;
126 memcpy(line->tail, buf, len);
130 /* The circular buffer is wrapping */
131 memcpy(line->tail, buf, end);
133 memcpy(line->buffer, buf, len - end);
134 line->tail = line->buffer + len - end;
141 * Flushes the ring buffer to the output channels. That is, write_chan is
142 * called, passing it line->head as buffer, and an appropriate count.
144 * On exit, returns 1 when the buffer is empty,
145 * 0 when the buffer is not empty on exit,
146 * and -errno when an error occurred.
148 * Must be called while holding line->lock!*/
149 static int flush_buffer(struct line *line)
153 if ((line->buffer == NULL) || (line->head == line->tail))
156 if (line->tail < line->head) {
157 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
158 count = line->buffer + LINE_BUFSIZE - line->head;
160 n = write_chan(&line->chan_list, line->head, count,
161 line->driver->write_irq);
165 /* We have flushed from ->head to buffer end, now we
166 * must flush only from the beginning to ->tail.*/
167 line->head = line->buffer;
174 count = line->tail - line->head;
175 n = write_chan(&line->chan_list, line->head, count,
176 line->driver->write_irq);
182 return line->head == line->tail;
185 void line_flush_buffer(struct tty_struct *tty)
187 struct line *line = tty->driver_data;
191 /*XXX: copied from line_write, verify if it is correct!*/
196 spin_lock_irqsave(&line->lock, flags);
197 err = flush_buffer(line);
200 spin_unlock_irqrestore(&line->lock, flags);
204 /* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer
205 * and ->write. Hope it's not that bad.*/
206 void line_flush_chars(struct tty_struct *tty)
208 line_flush_buffer(tty);
211 void line_put_char(struct tty_struct *tty, unsigned char ch)
213 line_write(tty, &ch, sizeof(ch));
216 int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
218 struct line *line = tty->driver_data;
225 spin_lock_irqsave(&line->lock, flags);
226 if (line->head != line->tail) {
227 ret = buffer_data(line, buf, len);
228 err = flush_buffer(line);
229 if (err <= 0 && (err != -EAGAIN || !ret))
232 n = write_chan(&line->chan_list, buf, len,
233 line->driver->write_irq);
242 ret += buffer_data(line, buf + n, len);
245 spin_unlock_irqrestore(&line->lock, flags);
249 void line_set_termios(struct tty_struct *tty, struct termios * old)
254 static const struct {
259 /* don't print these, they flood the log ... */
260 { TCGETS, NULL, "TCGETS" },
261 { TCSETS, NULL, "TCSETS" },
262 { TCSETSW, NULL, "TCSETSW" },
263 { TCFLSH, NULL, "TCFLSH" },
264 { TCSBRK, NULL, "TCSBRK" },
266 /* general tty stuff */
267 { TCSETSF, KERN_DEBUG, "TCSETSF" },
268 { TCGETA, KERN_DEBUG, "TCGETA" },
269 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
270 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
271 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
273 /* linux-specific ones */
274 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
275 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
276 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
277 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
280 int line_ioctl(struct tty_struct *tty, struct file * file,
281 unsigned int cmd, unsigned long arg)
325 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
326 if (cmd == tty_ioctls[i].cmd)
328 if (i < ARRAY_SIZE(tty_ioctls)) {
329 if (NULL != tty_ioctls[i].level)
330 printk("%s%s: %s: ioctl %s called\n",
331 tty_ioctls[i].level, __FUNCTION__,
332 tty->name, tty_ioctls[i].name);
334 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
335 __FUNCTION__, tty->name, cmd);
343 void line_throttle(struct tty_struct *tty)
345 struct line *line = tty->driver_data;
347 deactivate_chan(&line->chan_list, line->driver->read_irq);
351 void line_unthrottle(struct tty_struct *tty)
353 struct line *line = tty->driver_data;
356 chan_interrupt(&line->chan_list, &line->task, tty,
357 line->driver->read_irq);
359 /* Maybe there is enough stuff pending that calling the interrupt
360 * throttles us again. In this case, line->throttled will be 1
361 * again and we shouldn't turn the interrupt back on.
364 reactivate_chan(&line->chan_list, line->driver->read_irq);
367 static irqreturn_t line_write_interrupt(int irq, void *data,
368 struct pt_regs *unused)
370 struct chan *chan = data;
371 struct line *line = chan->line;
372 struct tty_struct *tty = line->tty;
375 /* Interrupts are enabled here because we registered the interrupt with
376 * IRQF_DISABLED (see line_setup_irq).*/
378 spin_lock_irq(&line->lock);
379 err = flush_buffer(line);
383 line->head = line->buffer;
384 line->tail = line->buffer;
386 spin_unlock_irq(&line->lock);
391 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
392 (tty->ldisc.write_wakeup != NULL))
393 (tty->ldisc.write_wakeup)(tty);
396 * In blocking mode, everything sleeps on tty->write_wait.
397 * Sleeping in the console driver would break non-blocking
401 if (waitqueue_active(&tty->write_wait))
402 wake_up_interruptible(&tty->write_wait);
406 int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
408 const struct line_driver *driver = line->driver;
409 int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
412 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
413 line_interrupt, flags,
414 driver->read_irq_name, data);
418 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
419 line_write_interrupt, flags,
420 driver->write_irq_name, data);
425 int line_open(struct line *lines, struct tty_struct *tty)
430 line = &lines[tty->index];
431 tty->driver_data = line;
433 /* The IRQ which takes this lock is not yet enabled and won't be run
434 * before the end, so we don't need to use spin_lock_irq.*/
435 spin_lock(&line->lock);
437 tty->driver_data = line;
443 /* Here the device is opened, if necessary, and interrupt
447 INIT_WORK(&line->task, line_timer_cb, line);
450 chan_enable_winch(&line->chan_list, tty);
454 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
455 &tty->winsize.ws_col);
460 spin_unlock(&line->lock);
464 static void unregister_winch(struct tty_struct *tty);
466 void line_close(struct tty_struct *tty, struct file * filp)
468 struct line *line = tty->driver_data;
470 /* XXX: I assume this should be called in process context, not with
471 * interrupts disabled!
473 spin_lock_irq(&line->lock);
475 /* We ignore the error anyway! */
480 tty->driver_data = NULL;
483 unregister_winch(tty);
488 spin_unlock_irq(&line->lock);
491 void close_lines(struct line *lines, int nlines)
495 for(i = 0; i < nlines; i++)
496 close_chan(&lines[i].chan_list, 0);
499 /* Common setup code for both startup command line and mconsole initialization.
500 * @lines contains the the array (of size @num) to modify;
501 * @init is the setup string;
504 int line_setup(struct line *lines, unsigned int num, char *init)
510 /* We said con=/ssl= instead of con#=, so we are configuring all
511 * consoles at once.*/
515 n = simple_strtoul(init, &end, 0);
517 printk(KERN_ERR "line_setup failed to parse \"%s\"\n",
525 if (n >= (signed int) num) {
526 printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
531 if (lines[n].tty != NULL) {
532 printk("line_setup - device %d is open\n", n);
535 if (lines[n].init_pri <= INIT_ONE){
536 lines[n].init_pri = INIT_ONE;
537 if (!strcmp(init, "none"))
540 lines[n].init_str = init;
546 for(i = 0; i < num; i++){
547 if(lines[i].init_pri <= INIT_ALL){
548 lines[i].init_pri = INIT_ALL;
549 if(!strcmp(init, "none")) lines[i].valid = 0;
551 lines[i].init_str = init;
557 return n == -1 ? num : n;
560 int line_config(struct line *lines, unsigned int num, char *str,
561 const struct chan_opts *opts)
568 printk("line_config - can't configure all devices from "
573 new = kstrdup(str, GFP_KERNEL);
575 printk("line_config - kstrdup failed\n");
578 n = line_setup(lines, num, new);
583 return parse_chan_pair(line->init_str, line, n, opts);
586 int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
587 int size, char **error_out)
593 dev = simple_strtoul(name, &end, 0);
594 if((*end != '\0') || (end == name)){
595 *error_out = "line_get_config failed to parse device number";
599 if((dev < 0) || (dev >= num)){
600 *error_out = "device number out of range";
606 spin_lock(&line->lock);
608 CONFIG_CHUNK(str, size, n, "none", 1);
609 else if(line->tty == NULL)
610 CONFIG_CHUNK(str, size, n, line->init_str, 1);
611 else n = chan_config_string(&line->chan_list, str, size, error_out);
612 spin_unlock(&line->lock);
617 int line_id(char **str, int *start_out, int *end_out)
622 n = simple_strtoul(*str, &end, 0);
623 if((*end != '\0') || (end == *str))
632 int line_remove(struct line *lines, unsigned int num, int n)
635 char config[sizeof("conxxxx=none\0")];
637 sprintf(config, "%d=none", n);
638 err = line_setup(lines, num, config);
644 struct tty_driver *line_register_devfs(struct lines *set,
645 struct line_driver *line_driver,
646 struct tty_operations *ops, struct line *lines,
650 struct tty_driver *driver = alloc_tty_driver(nlines);
655 driver->driver_name = line_driver->name;
656 driver->name = line_driver->device_name;
657 driver->major = line_driver->major;
658 driver->minor_start = line_driver->minor_start;
659 driver->type = line_driver->type;
660 driver->subtype = line_driver->subtype;
661 driver->flags = TTY_DRIVER_REAL_RAW;
662 driver->init_termios = tty_std_termios;
663 tty_set_operations(driver, ops);
665 if (tty_register_driver(driver)) {
666 printk("%s: can't register %s driver\n",
667 __FUNCTION__,line_driver->name);
668 put_tty_driver(driver);
672 for(i = 0; i < nlines; i++){
674 tty_unregister_device(driver, i);
677 mconsole_register_dev(&line_driver->mc);
681 static DEFINE_SPINLOCK(winch_handler_lock);
682 static LIST_HEAD(winch_handlers);
684 void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
689 for(i = 0; i < nlines; i++){
691 INIT_LIST_HEAD(&line->chan_list);
693 if(line->init_str == NULL)
696 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
697 if(line->init_str == NULL)
698 printk("lines_init - kstrdup returned NULL\n");
700 if(parse_chan_pair(line->init_str, line, i, opts)){
701 printk("parse_chan_pair failed for device %d\n", i);
708 struct list_head list;
712 struct tty_struct *tty;
715 static irqreturn_t winch_interrupt(int irq, void *data, struct pt_regs *unused)
717 struct winch *winch = data;
718 struct tty_struct *tty;
724 err = generic_read(winch->fd, &c, NULL);
727 printk("winch_interrupt : read failed, "
728 "errno = %d\n", -err);
729 printk("fd %d is losing SIGWINCH support\n",
738 line = tty->driver_data;
739 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
740 &tty->winsize.ws_col);
741 kill_pg(tty->pgrp, SIGWINCH, 1);
745 reactivate_fd(winch->fd, WINCH_IRQ);
749 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty)
753 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
755 printk("register_winch_irq - kmalloc failed\n");
759 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
765 spin_lock(&winch_handler_lock);
766 list_add(&winch->list, &winch_handlers);
767 spin_unlock(&winch_handler_lock);
769 if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
770 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
772 printk("register_winch_irq - failed to register IRQ\n");
775 static void free_winch(struct winch *winch)
777 list_del(&winch->list);
780 os_kill_process(winch->pid, 1);
782 os_close_file(winch->fd);
784 free_irq(WINCH_IRQ, winch);
788 static void unregister_winch(struct tty_struct *tty)
790 struct list_head *ele;
793 spin_lock(&winch_handler_lock);
795 list_for_each(ele, &winch_handlers){
796 winch = list_entry(ele, struct winch, list);
797 if(winch->tty == tty){
802 spin_unlock(&winch_handler_lock);
805 static void winch_cleanup(void)
807 struct list_head *ele, *next;
810 spin_lock(&winch_handler_lock);
812 list_for_each_safe(ele, next, &winch_handlers){
813 winch = list_entry(ele, struct winch, list);
817 spin_unlock(&winch_handler_lock);
819 __uml_exitcall(winch_cleanup);
821 char *add_xterm_umid(char *base)
830 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
831 title = kmalloc(len, GFP_KERNEL);
833 printk("Failed to allocate buffer for xterm title\n");
837 snprintf(title, len, "%s (%s)", base, umid);