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)
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(struct work_struct *work)
36 struct line *line = container_of(work, struct line, task.work);
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!*/
195 spin_lock_irqsave(&line->lock, flags);
196 err = flush_buffer(line);
199 spin_unlock_irqrestore(&line->lock, flags);
203 /* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer
204 * and ->write. Hope it's not that bad.*/
205 void line_flush_chars(struct tty_struct *tty)
207 line_flush_buffer(tty);
210 void line_put_char(struct tty_struct *tty, unsigned char ch)
212 line_write(tty, &ch, sizeof(ch));
215 int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
217 struct line *line = tty->driver_data;
224 spin_lock_irqsave(&line->lock, flags);
225 if (line->head != line->tail) {
226 ret = buffer_data(line, buf, len);
227 err = flush_buffer(line);
228 if (err <= 0 && (err != -EAGAIN || !ret))
231 n = write_chan(&line->chan_list, buf, len,
232 line->driver->write_irq);
241 ret += buffer_data(line, buf + n, len);
244 spin_unlock_irqrestore(&line->lock, flags);
248 void line_set_termios(struct tty_struct *tty, struct ktermios * old)
253 static const struct {
258 /* don't print these, they flood the log ... */
259 { TCGETS, NULL, "TCGETS" },
260 { TCSETS, NULL, "TCSETS" },
261 { TCSETSW, NULL, "TCSETSW" },
262 { TCFLSH, NULL, "TCFLSH" },
263 { TCSBRK, NULL, "TCSBRK" },
265 /* general tty stuff */
266 { TCSETSF, KERN_DEBUG, "TCSETSF" },
267 { TCGETA, KERN_DEBUG, "TCGETA" },
268 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
269 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
270 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
272 /* linux-specific ones */
273 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
274 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
275 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
276 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
279 int line_ioctl(struct tty_struct *tty, struct file * file,
280 unsigned int cmd, unsigned long arg)
324 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
325 if (cmd == tty_ioctls[i].cmd)
327 if (i < ARRAY_SIZE(tty_ioctls)) {
328 if (NULL != tty_ioctls[i].level)
329 printk("%s%s: %s: ioctl %s called\n",
330 tty_ioctls[i].level, __FUNCTION__,
331 tty->name, tty_ioctls[i].name);
333 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
334 __FUNCTION__, tty->name, cmd);
342 void line_throttle(struct tty_struct *tty)
344 struct line *line = tty->driver_data;
346 deactivate_chan(&line->chan_list, line->driver->read_irq);
350 void line_unthrottle(struct tty_struct *tty)
352 struct line *line = tty->driver_data;
355 chan_interrupt(&line->chan_list, &line->task, tty,
356 line->driver->read_irq);
358 /* Maybe there is enough stuff pending that calling the interrupt
359 * throttles us again. In this case, line->throttled will be 1
360 * again and we shouldn't turn the interrupt back on.
363 reactivate_chan(&line->chan_list, line->driver->read_irq);
366 static irqreturn_t line_write_interrupt(int irq, void *data)
368 struct chan *chan = data;
369 struct line *line = chan->line;
370 struct tty_struct *tty = line->tty;
373 /* Interrupts are enabled here because we registered the interrupt with
374 * IRQF_DISABLED (see line_setup_irq).*/
376 spin_lock_irq(&line->lock);
377 err = flush_buffer(line);
381 line->head = line->buffer;
382 line->tail = line->buffer;
384 spin_unlock_irq(&line->lock);
389 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
390 (tty->ldisc.write_wakeup != NULL))
391 (tty->ldisc.write_wakeup)(tty);
394 * In blocking mode, everything sleeps on tty->write_wait.
395 * Sleeping in the console driver would break non-blocking
399 if (waitqueue_active(&tty->write_wait))
400 wake_up_interruptible(&tty->write_wait);
404 int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
406 const struct line_driver *driver = line->driver;
407 int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
410 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
411 line_interrupt, flags,
412 driver->read_irq_name, data);
416 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
417 line_write_interrupt, flags,
418 driver->write_irq_name, data);
423 /* Normally, a driver like this can rely mostly on the tty layer
424 * locking, particularly when it comes to the driver structure.
425 * However, in this case, mconsole requests can come in "from the
426 * side", and race with opens and closes.
428 * The problem comes from line_setup not wanting to sleep if
429 * the device is open or being opened. This can happen because the
430 * first opener of a device is responsible for setting it up on the
431 * host, and that can sleep. The open of a port device will sleep
432 * until someone telnets to it.
434 * The obvious solution of putting everything under a mutex fails
435 * because then trying (and failing) to change the configuration of an
436 * open(ing) device will block until the open finishes. The right
437 * thing to happen is for it to fail immediately.
439 * We can put the opening (and closing) of the host device under a
440 * separate lock, but that has to be taken before the count lock is
441 * released. Otherwise, you open a window in which another open can
442 * come through and assume that the host side is opened and working.
444 * So, if the tty count is one, open will take the open mutex
445 * inside the count lock. Otherwise, it just returns. This will sleep
446 * if the last close is pending, and will block a setup or get_config,
447 * but that should not last long.
449 * So, what we end up with is that open and close take the count lock.
450 * If the first open or last close are happening, then the open mutex
451 * is taken inside the count lock and the host opening or closing is done.
453 * setup and get_config only take the count lock. setup modifies the
454 * device configuration only if the open count is zero. Arbitrarily
455 * long blocking of setup doesn't happen because something would have to be
456 * waiting for an open to happen. However, a second open with
457 * tty->count == 1 can't happen, and a close can't happen until the open
460 * We can't maintain our own count here because the tty layer doesn't
461 * match opens and closes. It will call close if an open failed, and
462 * a tty hangup will result in excess closes. So, we rely on
463 * tty->count instead. It is one on both the first open and last close.
466 int line_open(struct line *lines, struct tty_struct *tty)
468 struct line *line = &lines[tty->index];
471 spin_lock(&line->count_lock);
479 mutex_lock(&line->open_mutex);
480 spin_unlock(&line->count_lock);
482 tty->driver_data = line;
486 INIT_DELAYED_WORK(&line->task, line_timer_cb);
489 chan_enable_winch(&line->chan_list, tty);
493 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
494 &tty->winsize.ws_col);
496 mutex_unlock(&line->open_mutex);
500 spin_unlock(&line->count_lock);
504 static void unregister_winch(struct tty_struct *tty);
506 void line_close(struct tty_struct *tty, struct file * filp)
508 struct line *line = tty->driver_data;
510 /* If line_open fails (and tty->driver_data is never set),
511 * tty_open will call line_close. So just return in this case.
516 /* We ignore the error anyway! */
519 spin_lock(&line->count_lock);
526 mutex_lock(&line->open_mutex);
527 spin_unlock(&line->count_lock);
530 tty->driver_data = NULL;
533 unregister_winch(tty);
537 mutex_unlock(&line->open_mutex);
541 spin_unlock(&line->count_lock);
544 void close_lines(struct line *lines, int nlines)
548 for(i = 0; i < nlines; i++)
549 close_chan(&lines[i].chan_list, 0);
552 static void setup_one_line(struct line *lines, int n, char *init, int init_prio)
554 struct line *line = &lines[n];
556 spin_lock(&line->count_lock);
558 if(line->tty != NULL){
559 printk("line_setup - device %d is open\n", n);
563 if (line->init_pri <= init_prio){
564 line->init_pri = init_prio;
565 if (!strcmp(init, "none"))
568 line->init_str = init;
573 spin_unlock(&line->count_lock);
576 /* Common setup code for both startup command line and mconsole initialization.
577 * @lines contains the array (of size @num) to modify;
578 * @init is the setup string;
581 int line_setup(struct line *lines, unsigned int num, char *init)
587 /* We said con=/ssl= instead of con#=, so we are configuring all
588 * consoles at once.*/
592 n = simple_strtoul(init, &end, 0);
594 printk(KERN_ERR "line_setup failed to parse \"%s\"\n",
602 if (n >= (signed int) num) {
603 printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
608 setup_one_line(lines, n, init, INIT_ONE);
610 for(i = 0; i < num; i++)
611 setup_one_line(lines, i, init, INIT_ALL);
613 return n == -1 ? num : n;
616 int line_config(struct line *lines, unsigned int num, char *str,
617 const struct chan_opts *opts)
624 printk("line_config - can't configure all devices from "
629 new = kstrdup(str, GFP_KERNEL);
631 printk("line_config - kstrdup failed\n");
634 n = line_setup(lines, num, new);
639 return parse_chan_pair(line->init_str, line, n, opts);
642 int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
643 int size, char **error_out)
649 dev = simple_strtoul(name, &end, 0);
650 if((*end != '\0') || (end == name)){
651 *error_out = "line_get_config failed to parse device number";
655 if((dev < 0) || (dev >= num)){
656 *error_out = "device number out of range";
662 spin_lock(&line->count_lock);
664 CONFIG_CHUNK(str, size, n, "none", 1);
665 else if(line->tty == NULL)
666 CONFIG_CHUNK(str, size, n, line->init_str, 1);
667 else n = chan_config_string(&line->chan_list, str, size, error_out);
668 spin_unlock(&line->count_lock);
673 int line_id(char **str, int *start_out, int *end_out)
678 n = simple_strtoul(*str, &end, 0);
679 if((*end != '\0') || (end == *str))
688 int line_remove(struct line *lines, unsigned int num, int n)
691 char config[sizeof("conxxxx=none\0")];
693 sprintf(config, "%d=none", n);
694 err = line_setup(lines, num, config);
700 struct tty_driver *line_register_devfs(struct lines *set,
701 struct line_driver *line_driver,
702 const struct tty_operations *ops,
703 struct line *lines, int nlines)
706 struct tty_driver *driver = alloc_tty_driver(nlines);
711 driver->driver_name = line_driver->name;
712 driver->name = line_driver->device_name;
713 driver->major = line_driver->major;
714 driver->minor_start = line_driver->minor_start;
715 driver->type = line_driver->type;
716 driver->subtype = line_driver->subtype;
717 driver->flags = TTY_DRIVER_REAL_RAW;
718 driver->init_termios = tty_std_termios;
719 tty_set_operations(driver, ops);
721 if (tty_register_driver(driver)) {
722 printk("%s: can't register %s driver\n",
723 __FUNCTION__,line_driver->name);
724 put_tty_driver(driver);
728 for(i = 0; i < nlines; i++){
730 tty_unregister_device(driver, i);
733 mconsole_register_dev(&line_driver->mc);
737 static DEFINE_SPINLOCK(winch_handler_lock);
738 static LIST_HEAD(winch_handlers);
740 void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
745 for(i = 0; i < nlines; i++){
747 INIT_LIST_HEAD(&line->chan_list);
748 mutex_init(&line->open_mutex);
750 if(line->init_str == NULL)
753 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
754 if(line->init_str == NULL)
755 printk("lines_init - kstrdup returned NULL\n");
757 if(parse_chan_pair(line->init_str, line, i, opts)){
758 printk("parse_chan_pair failed for device %d\n", i);
765 struct list_head list;
769 struct tty_struct *tty;
772 static irqreturn_t winch_interrupt(int irq, void *data)
774 struct winch *winch = data;
775 struct tty_struct *tty;
781 err = generic_read(winch->fd, &c, NULL);
784 printk("winch_interrupt : read failed, "
785 "errno = %d\n", -err);
786 printk("fd %d is losing SIGWINCH support\n",
795 line = tty->driver_data;
796 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
797 &tty->winsize.ws_col);
798 kill_pg(tty->pgrp, SIGWINCH, 1);
802 reactivate_fd(winch->fd, WINCH_IRQ);
806 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty)
810 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
812 printk("register_winch_irq - kmalloc failed\n");
816 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
822 spin_lock(&winch_handler_lock);
823 list_add(&winch->list, &winch_handlers);
824 spin_unlock(&winch_handler_lock);
826 if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
827 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
829 printk("register_winch_irq - failed to register IRQ\n");
832 static void free_winch(struct winch *winch)
834 list_del(&winch->list);
837 os_kill_process(winch->pid, 1);
839 os_close_file(winch->fd);
841 free_irq(WINCH_IRQ, winch);
845 static void unregister_winch(struct tty_struct *tty)
847 struct list_head *ele;
850 spin_lock(&winch_handler_lock);
852 list_for_each(ele, &winch_handlers){
853 winch = list_entry(ele, struct winch, list);
854 if(winch->tty == tty){
859 spin_unlock(&winch_handler_lock);
862 static void winch_cleanup(void)
864 struct list_head *ele, *next;
867 spin_lock(&winch_handler_lock);
869 list_for_each_safe(ele, next, &winch_handlers){
870 winch = list_entry(ele, struct winch, list);
874 spin_unlock(&winch_handler_lock);
876 __uml_exitcall(winch_cleanup);
878 char *add_xterm_umid(char *base)
887 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
888 title = kmalloc(len, GFP_KERNEL);
890 printk("Failed to allocate buffer for xterm title\n");
894 snprintf(title, len, "%s (%s)", base, umid);