2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
6 #include "linux/irqreturn.h"
13 #define LINE_BUFSIZE 4096
15 static irqreturn_t line_interrupt(int irq, void *data)
17 struct chan *chan = data;
18 struct line *line = chan->line;
19 struct tty_struct *tty = line->tty;
22 chan_interrupt(&line->chan_list, &line->task, tty, irq);
26 static void line_timer_cb(struct work_struct *work)
28 struct line *line = container_of(work, struct line, task.work);
31 chan_interrupt(&line->chan_list, &line->task, line->tty,
32 line->driver->read_irq);
36 * Returns the free space inside the ring buffer of this line.
38 * Should be called while holding line->lock (this does not modify data).
40 static int write_room(struct line *line)
44 if (line->buffer == NULL)
45 return LINE_BUFSIZE - 1;
47 /* This is for the case where the buffer is wrapped! */
48 n = line->head - line->tail;
51 n += LINE_BUFSIZE; /* The other case */
55 int line_write_room(struct tty_struct *tty)
57 struct line *line = tty->driver_data;
61 spin_lock_irqsave(&line->lock, flags);
62 room = write_room(line);
63 spin_unlock_irqrestore(&line->lock, flags);
68 int line_chars_in_buffer(struct tty_struct *tty)
70 struct line *line = tty->driver_data;
74 spin_lock_irqsave(&line->lock, flags);
75 /* write_room subtracts 1 for the needed NULL, so we readd it.*/
76 ret = LINE_BUFSIZE - (write_room(line) + 1);
77 spin_unlock_irqrestore(&line->lock, flags);
83 * This copies the content of buf into the circular buffer associated with
85 * The return value is the number of characters actually copied, i.e. the ones
86 * for which there was space: this function is not supposed to ever flush out
87 * the circular buffer.
89 * Must be called while holding line->lock!
91 static int buffer_data(struct line *line, const char *buf, int len)
95 if (line->buffer == NULL) {
96 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
97 if (line->buffer == NULL) {
98 printk(KERN_ERR "buffer_data - atomic allocation "
102 line->head = line->buffer;
103 line->tail = line->buffer;
106 room = write_room(line);
107 len = (len > room) ? room : len;
109 end = line->buffer + LINE_BUFSIZE - line->tail;
112 memcpy(line->tail, buf, len);
116 /* The circular buffer is wrapping */
117 memcpy(line->tail, buf, end);
119 memcpy(line->buffer, buf, len - end);
120 line->tail = line->buffer + len - end;
127 * Flushes the ring buffer to the output channels. That is, write_chan is
128 * called, passing it line->head as buffer, and an appropriate count.
130 * On exit, returns 1 when the buffer is empty,
131 * 0 when the buffer is not empty on exit,
132 * and -errno when an error occurred.
134 * Must be called while holding line->lock!*/
135 static int flush_buffer(struct line *line)
139 if ((line->buffer == NULL) || (line->head == line->tail))
142 if (line->tail < line->head) {
143 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
144 count = line->buffer + LINE_BUFSIZE - line->head;
146 n = write_chan(&line->chan_list, line->head, count,
147 line->driver->write_irq);
152 * We have flushed from ->head to buffer end, now we
153 * must flush only from the beginning to ->tail.
155 line->head = line->buffer;
162 count = line->tail - line->head;
163 n = write_chan(&line->chan_list, line->head, count,
164 line->driver->write_irq);
170 return line->head == line->tail;
173 void line_flush_buffer(struct tty_struct *tty)
175 struct line *line = tty->driver_data;
179 spin_lock_irqsave(&line->lock, flags);
180 err = flush_buffer(line);
181 spin_unlock_irqrestore(&line->lock, flags);
185 * We map both ->flush_chars and ->put_char (which go in pair) onto
186 * ->flush_buffer and ->write. Hope it's not that bad.
188 void line_flush_chars(struct tty_struct *tty)
190 line_flush_buffer(tty);
193 void line_put_char(struct tty_struct *tty, unsigned char ch)
195 line_write(tty, &ch, sizeof(ch));
198 int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
200 struct line *line = tty->driver_data;
204 spin_lock_irqsave(&line->lock, flags);
205 if (line->head != line->tail)
206 ret = buffer_data(line, buf, len);
208 n = write_chan(&line->chan_list, buf, len,
209 line->driver->write_irq);
218 ret += buffer_data(line, buf + n, len);
221 spin_unlock_irqrestore(&line->lock, flags);
225 void line_set_termios(struct tty_struct *tty, struct ktermios * old)
230 static const struct {
235 /* don't print these, they flood the log ... */
236 { TCGETS, NULL, "TCGETS" },
237 { TCSETS, NULL, "TCSETS" },
238 { TCSETSW, NULL, "TCSETSW" },
239 { TCFLSH, NULL, "TCFLSH" },
240 { TCSBRK, NULL, "TCSBRK" },
242 /* general tty stuff */
243 { TCSETSF, KERN_DEBUG, "TCSETSF" },
244 { TCGETA, KERN_DEBUG, "TCGETA" },
245 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
246 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
247 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
249 /* linux-specific ones */
250 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
251 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
252 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
253 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
256 int line_ioctl(struct tty_struct *tty, struct file * file,
257 unsigned int cmd, unsigned long arg)
301 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
302 if (cmd == tty_ioctls[i].cmd)
304 if (i == ARRAY_SIZE(tty_ioctls)) {
305 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
306 __FUNCTION__, tty->name, cmd);
314 void line_throttle(struct tty_struct *tty)
316 struct line *line = tty->driver_data;
318 deactivate_chan(&line->chan_list, line->driver->read_irq);
322 void line_unthrottle(struct tty_struct *tty)
324 struct line *line = tty->driver_data;
327 chan_interrupt(&line->chan_list, &line->task, tty,
328 line->driver->read_irq);
331 * Maybe there is enough stuff pending that calling the interrupt
332 * throttles us again. In this case, line->throttled will be 1
333 * again and we shouldn't turn the interrupt back on.
335 if (!line->throttled)
336 reactivate_chan(&line->chan_list, line->driver->read_irq);
339 static irqreturn_t line_write_interrupt(int irq, void *data)
341 struct chan *chan = data;
342 struct line *line = chan->line;
343 struct tty_struct *tty = line->tty;
347 * Interrupts are disabled here because we registered the interrupt with
348 * IRQF_DISABLED (see line_setup_irq).
351 spin_lock(&line->lock);
352 err = flush_buffer(line);
355 } else if (err < 0) {
356 line->head = line->buffer;
357 line->tail = line->buffer;
359 spin_unlock(&line->lock);
364 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
365 (tty->ldisc.write_wakeup != NULL))
366 (tty->ldisc.write_wakeup)(tty);
370 * In blocking mode, everything sleeps on tty->write_wait.
371 * Sleeping in the console driver would break non-blocking
375 if (waitqueue_active(&tty->write_wait))
376 wake_up_interruptible(&tty->write_wait);
380 int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
382 const struct line_driver *driver = line->driver;
383 int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
386 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
387 line_interrupt, flags,
388 driver->read_irq_name, data);
392 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
393 line_write_interrupt, flags,
394 driver->write_irq_name, data);
400 * Normally, a driver like this can rely mostly on the tty layer
401 * locking, particularly when it comes to the driver structure.
402 * However, in this case, mconsole requests can come in "from the
403 * side", and race with opens and closes.
405 * mconsole config requests will want to be sure the device isn't in
406 * use, and get_config, open, and close will want a stable
407 * configuration. The checking and modification of the configuration
408 * is done under a spinlock. Checking whether the device is in use is
409 * line->tty->count > 1, also under the spinlock.
411 * tty->count serves to decide whether the device should be enabled or
412 * disabled on the host. If it's equal to 1, then we are doing the
413 * first open or last close. Otherwise, open and close just return.
416 int line_open(struct line *lines, struct tty_struct *tty)
418 struct line *line = &lines[tty->index];
421 spin_lock(&line->count_lock);
429 spin_unlock(&line->count_lock);
431 tty->driver_data = line;
434 err = enable_chan(line);
438 INIT_DELAYED_WORK(&line->task, line_timer_cb);
441 chan_enable_winch(&line->chan_list, tty);
445 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
446 &tty->winsize.ws_col);
451 spin_unlock(&line->count_lock);
455 static void unregister_winch(struct tty_struct *tty);
457 void line_close(struct tty_struct *tty, struct file * filp)
459 struct line *line = tty->driver_data;
462 * If line_open fails (and tty->driver_data is never set),
463 * tty_open will call line_close. So just return in this case.
468 /* We ignore the error anyway! */
471 spin_lock(&line->count_lock);
478 spin_unlock(&line->count_lock);
481 tty->driver_data = NULL;
484 unregister_winch(tty);
491 spin_unlock(&line->count_lock);
494 void close_lines(struct line *lines, int nlines)
498 for(i = 0; i < nlines; i++)
499 close_chan(&lines[i].chan_list, 0);
502 static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
505 struct line *line = &lines[n];
508 spin_lock(&line->count_lock);
510 if (line->tty != NULL) {
511 *error_out = "Device is already open";
515 if (line->init_pri <= init_prio) {
516 line->init_pri = init_prio;
517 if (!strcmp(init, "none"))
520 line->init_str = init;
526 spin_unlock(&line->count_lock);
531 * Common setup code for both startup command line and mconsole initialization.
532 * @lines contains the array (of size @num) to modify;
533 * @init is the setup string;
534 * @error_out is an error string in the case of failure;
537 int line_setup(struct line *lines, unsigned int num, char *init,
545 * We said con=/ssl= instead of con#=, so we are configuring all
551 n = simple_strtoul(init, &end, 0);
553 *error_out = "Couldn't parse device number";
560 if (n >= (signed int) num) {
561 *error_out = "Device number out of range";
565 err = setup_one_line(lines, n, init, INIT_ONE, error_out);
570 for(i = 0; i < num; i++) {
571 err = setup_one_line(lines, i, init, INIT_ALL,
577 return n == -1 ? num : n;
580 int line_config(struct line *lines, unsigned int num, char *str,
581 const struct chan_opts *opts, char **error_out)
588 *error_out = "Can't configure all devices from mconsole";
592 new = kstrdup(str, GFP_KERNEL);
594 *error_out = "Failed to allocate memory";
597 n = line_setup(lines, num, new, error_out);
602 return parse_chan_pair(line->init_str, line, n, opts, error_out);
605 int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
606 int size, char **error_out)
612 dev = simple_strtoul(name, &end, 0);
613 if ((*end != '\0') || (end == name)) {
614 *error_out = "line_get_config failed to parse device number";
618 if ((dev < 0) || (dev >= num)) {
619 *error_out = "device number out of range";
625 spin_lock(&line->count_lock);
627 CONFIG_CHUNK(str, size, n, "none", 1);
628 else if (line->tty == NULL)
629 CONFIG_CHUNK(str, size, n, line->init_str, 1);
630 else n = chan_config_string(&line->chan_list, str, size, error_out);
631 spin_unlock(&line->count_lock);
636 int line_id(char **str, int *start_out, int *end_out)
641 n = simple_strtoul(*str, &end, 0);
642 if ((*end != '\0') || (end == *str))
651 int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
654 char config[sizeof("conxxxx=none\0")];
656 sprintf(config, "%d=none", n);
657 err = line_setup(lines, num, config, error_out);
663 struct tty_driver *register_lines(struct line_driver *line_driver,
664 const struct tty_operations *ops,
665 struct line *lines, int nlines)
668 struct tty_driver *driver = alloc_tty_driver(nlines);
673 driver->driver_name = line_driver->name;
674 driver->name = line_driver->device_name;
675 driver->major = line_driver->major;
676 driver->minor_start = line_driver->minor_start;
677 driver->type = line_driver->type;
678 driver->subtype = line_driver->subtype;
679 driver->flags = TTY_DRIVER_REAL_RAW;
680 driver->init_termios = tty_std_termios;
681 tty_set_operations(driver, ops);
683 if (tty_register_driver(driver)) {
684 printk(KERN_ERR "register_lines : can't register %s driver\n",
686 put_tty_driver(driver);
690 for(i = 0; i < nlines; i++) {
692 tty_unregister_device(driver, i);
695 mconsole_register_dev(&line_driver->mc);
699 static DEFINE_SPINLOCK(winch_handler_lock);
700 static LIST_HEAD(winch_handlers);
702 void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
708 for(i = 0; i < nlines; i++) {
710 INIT_LIST_HEAD(&line->chan_list);
712 if (line->init_str == NULL)
715 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
716 if (line->init_str == NULL)
717 printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
719 if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
720 printk(KERN_ERR "parse_chan_pair failed for "
721 "device %d : %s\n", i, error);
728 struct list_head list;
732 struct tty_struct *tty;
736 static void free_winch(struct winch *winch, int free_irq_ok)
738 list_del(&winch->list);
740 if (winch->pid != -1)
741 os_kill_process(winch->pid, 1);
743 os_close_file(winch->fd);
744 if (winch->stack != 0)
745 free_stack(winch->stack, 0);
747 free_irq(WINCH_IRQ, winch);
751 static irqreturn_t winch_interrupt(int irq, void *data)
753 struct winch *winch = data;
754 struct tty_struct *tty;
759 if (winch->fd != -1) {
760 err = generic_read(winch->fd, &c, NULL);
762 if (err != -EAGAIN) {
763 printk(KERN_ERR "winch_interrupt : "
764 "read failed, errno = %d\n", -err);
765 printk(KERN_ERR "fd %d is losing SIGWINCH "
766 "support\n", winch->tty_fd);
767 free_winch(winch, 0);
775 line = tty->driver_data;
776 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
777 &tty->winsize.ws_col);
778 kill_pgrp(tty->pgrp, SIGWINCH, 1);
782 reactivate_fd(winch->fd, WINCH_IRQ);
786 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
791 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
793 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
797 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
804 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
805 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
806 "winch", winch) < 0) {
807 printk(KERN_ERR "register_winch_irq - failed to register "
812 spin_lock(&winch_handler_lock);
813 list_add(&winch->list, &winch_handlers);
814 spin_unlock(&winch_handler_lock);
821 os_kill_process(pid, 1);
824 free_stack(stack, 0);
827 static void unregister_winch(struct tty_struct *tty)
829 struct list_head *ele;
832 spin_lock(&winch_handler_lock);
834 list_for_each(ele, &winch_handlers) {
835 winch = list_entry(ele, struct winch, list);
836 if (winch->tty == tty) {
837 free_winch(winch, 1);
841 spin_unlock(&winch_handler_lock);
844 static void winch_cleanup(void)
846 struct list_head *ele, *next;
849 spin_lock(&winch_handler_lock);
851 list_for_each_safe(ele, next, &winch_handlers) {
852 winch = list_entry(ele, struct winch, list);
853 free_winch(winch, 1);
856 spin_unlock(&winch_handler_lock);
858 __uml_exitcall(winch_cleanup);
860 char *add_xterm_umid(char *base)
869 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
870 title = kmalloc(len, GFP_KERNEL);
872 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
876 snprintf(title, len, "%s (%s)", base, umid);