2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
6 #include "linux/irqreturn.h"
8 #include "linux/sched.h"
9 #include "linux/slab.h"
10 #include "chan_kern.h"
13 #include "kern_util.h"
16 #define LINE_BUFSIZE 4096
18 static irqreturn_t line_interrupt(int irq, void *data)
20 struct chan *chan = data;
21 struct line *line = chan->line;
22 struct tty_struct *tty;
25 chan_interrupt(&line->chan_list, &line->task, line->tty, irq);
29 static void line_timer_cb(struct work_struct *work)
31 struct line *line = container_of(work, struct line, task.work);
34 chan_interrupt(&line->chan_list, &line->task, line->tty,
35 line->driver->read_irq);
39 * Returns the free space inside the ring buffer of this line.
41 * Should be called while holding line->lock (this does not modify data).
43 static int write_room(struct line *line)
47 if (line->buffer == NULL)
48 return LINE_BUFSIZE - 1;
50 /* This is for the case where the buffer is wrapped! */
51 n = line->head - line->tail;
54 n += LINE_BUFSIZE; /* The other case */
58 int line_write_room(struct tty_struct *tty)
60 struct line *line = tty->driver_data;
64 spin_lock_irqsave(&line->lock, flags);
65 room = write_room(line);
66 spin_unlock_irqrestore(&line->lock, flags);
71 int line_chars_in_buffer(struct tty_struct *tty)
73 struct line *line = tty->driver_data;
77 spin_lock_irqsave(&line->lock, flags);
78 /* write_room subtracts 1 for the needed NULL, so we readd it.*/
79 ret = LINE_BUFSIZE - (write_room(line) + 1);
80 spin_unlock_irqrestore(&line->lock, flags);
86 * This copies the content of buf into the circular buffer associated with
88 * The return value is the number of characters actually copied, i.e. the ones
89 * for which there was space: this function is not supposed to ever flush out
90 * the circular buffer.
92 * Must be called while holding line->lock!
94 static int buffer_data(struct line *line, const char *buf, int len)
98 if (line->buffer == NULL) {
99 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
100 if (line->buffer == NULL) {
101 printk(KERN_ERR "buffer_data - atomic allocation "
105 line->head = line->buffer;
106 line->tail = line->buffer;
109 room = write_room(line);
110 len = (len > room) ? room : len;
112 end = line->buffer + LINE_BUFSIZE - line->tail;
115 memcpy(line->tail, buf, len);
119 /* The circular buffer is wrapping */
120 memcpy(line->tail, buf, end);
122 memcpy(line->buffer, buf, len - end);
123 line->tail = line->buffer + len - end;
130 * Flushes the ring buffer to the output channels. That is, write_chan is
131 * called, passing it line->head as buffer, and an appropriate count.
133 * On exit, returns 1 when the buffer is empty,
134 * 0 when the buffer is not empty on exit,
135 * and -errno when an error occurred.
137 * Must be called while holding line->lock!*/
138 static int flush_buffer(struct line *line)
142 if ((line->buffer == NULL) || (line->head == line->tail))
145 if (line->tail < line->head) {
146 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
147 count = line->buffer + LINE_BUFSIZE - line->head;
149 n = write_chan(&line->chan_list, line->head, count,
150 line->driver->write_irq);
155 * We have flushed from ->head to buffer end, now we
156 * must flush only from the beginning to ->tail.
158 line->head = line->buffer;
165 count = line->tail - line->head;
166 n = write_chan(&line->chan_list, line->head, count,
167 line->driver->write_irq);
173 return line->head == line->tail;
176 void line_flush_buffer(struct tty_struct *tty)
178 struct line *line = tty->driver_data;
182 spin_lock_irqsave(&line->lock, flags);
183 err = flush_buffer(line);
184 spin_unlock_irqrestore(&line->lock, flags);
188 * We map both ->flush_chars and ->put_char (which go in pair) onto
189 * ->flush_buffer and ->write. Hope it's not that bad.
191 void line_flush_chars(struct tty_struct *tty)
193 line_flush_buffer(tty);
196 int line_put_char(struct tty_struct *tty, unsigned char ch)
198 return line_write(tty, &ch, sizeof(ch));
201 int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
203 struct line *line = tty->driver_data;
207 spin_lock_irqsave(&line->lock, flags);
208 if (line->head != line->tail)
209 ret = buffer_data(line, buf, len);
211 n = write_chan(&line->chan_list, buf, len,
212 line->driver->write_irq);
221 ret += buffer_data(line, buf + n, len);
224 spin_unlock_irqrestore(&line->lock, flags);
228 void line_set_termios(struct tty_struct *tty, struct ktermios * old)
233 static const struct {
238 /* don't print these, they flood the log ... */
239 { TCGETS, NULL, "TCGETS" },
240 { TCSETS, NULL, "TCSETS" },
241 { TCSETSW, NULL, "TCSETSW" },
242 { TCFLSH, NULL, "TCFLSH" },
243 { TCSBRK, NULL, "TCSBRK" },
245 /* general tty stuff */
246 { TCSETSF, KERN_DEBUG, "TCSETSF" },
247 { TCGETA, KERN_DEBUG, "TCGETA" },
248 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
249 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
250 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
252 /* linux-specific ones */
253 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
254 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
255 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
256 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
259 int line_ioctl(struct tty_struct *tty, struct file * file,
260 unsigned int cmd, unsigned long arg)
280 /* Note: these are out of date as we now have TCGETS2 etc but this
281 whole lot should probably go away */
306 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
307 if (cmd == tty_ioctls[i].cmd)
309 if (i == ARRAY_SIZE(tty_ioctls)) {
310 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
311 __func__, tty->name, cmd);
319 void line_throttle(struct tty_struct *tty)
321 struct line *line = tty->driver_data;
323 deactivate_chan(&line->chan_list, line->driver->read_irq);
327 void line_unthrottle(struct tty_struct *tty)
329 struct line *line = tty->driver_data;
332 chan_interrupt(&line->chan_list, &line->task, tty,
333 line->driver->read_irq);
336 * Maybe there is enough stuff pending that calling the interrupt
337 * throttles us again. In this case, line->throttled will be 1
338 * again and we shouldn't turn the interrupt back on.
340 if (!line->throttled)
341 reactivate_chan(&line->chan_list, line->driver->read_irq);
344 static irqreturn_t line_write_interrupt(int irq, void *data)
346 struct chan *chan = data;
347 struct line *line = chan->line;
348 struct tty_struct *tty = line->tty;
352 * Interrupts are disabled here because we registered the interrupt with
353 * IRQF_DISABLED (see line_setup_irq).
356 spin_lock(&line->lock);
357 err = flush_buffer(line);
360 } else if (err < 0) {
361 line->head = line->buffer;
362 line->tail = line->buffer;
364 spin_unlock(&line->lock);
373 int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
375 const struct line_driver *driver = line->driver;
376 int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
379 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
380 line_interrupt, flags,
381 driver->read_irq_name, data);
385 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
386 line_write_interrupt, flags,
387 driver->write_irq_name, data);
393 * Normally, a driver like this can rely mostly on the tty layer
394 * locking, particularly when it comes to the driver structure.
395 * However, in this case, mconsole requests can come in "from the
396 * side", and race with opens and closes.
398 * mconsole config requests will want to be sure the device isn't in
399 * use, and get_config, open, and close will want a stable
400 * configuration. The checking and modification of the configuration
401 * is done under a spinlock. Checking whether the device is in use is
402 * line->tty->count > 1, also under the spinlock.
404 * tty->count serves to decide whether the device should be enabled or
405 * disabled on the host. If it's equal to 1, then we are doing the
406 * first open or last close. Otherwise, open and close just return.
409 int line_open(struct line *lines, struct tty_struct *tty)
411 struct line *line = &lines[tty->index];
414 spin_lock(&line->count_lock);
422 spin_unlock(&line->count_lock);
424 tty->driver_data = line;
427 err = enable_chan(line);
431 INIT_DELAYED_WORK(&line->task, line_timer_cb);
434 chan_enable_winch(&line->chan_list, tty);
438 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
439 &tty->winsize.ws_col);
444 spin_unlock(&line->count_lock);
448 static void unregister_winch(struct tty_struct *tty);
450 void line_close(struct tty_struct *tty, struct file * filp)
452 struct line *line = tty->driver_data;
455 * If line_open fails (and tty->driver_data is never set),
456 * tty_open will call line_close. So just return in this case.
461 /* We ignore the error anyway! */
464 spin_lock(&line->count_lock);
471 spin_unlock(&line->count_lock);
474 tty->driver_data = NULL;
477 unregister_winch(tty);
484 spin_unlock(&line->count_lock);
487 void close_lines(struct line *lines, int nlines)
491 for(i = 0; i < nlines; i++)
492 close_chan(&lines[i].chan_list, 0);
495 static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
498 struct line *line = &lines[n];
501 spin_lock(&line->count_lock);
503 if (line->tty != NULL) {
504 *error_out = "Device is already open";
508 if (line->init_pri <= init_prio) {
509 line->init_pri = init_prio;
510 if (!strcmp(init, "none"))
513 line->init_str = init;
519 spin_unlock(&line->count_lock);
524 * Common setup code for both startup command line and mconsole initialization.
525 * @lines contains the array (of size @num) to modify;
526 * @init is the setup string;
527 * @error_out is an error string in the case of failure;
530 int line_setup(struct line *lines, unsigned int num, char *init,
538 * We said con=/ssl= instead of con#=, so we are configuring all
544 n = simple_strtoul(init, &end, 0);
546 *error_out = "Couldn't parse device number";
553 if (n >= (signed int) num) {
554 *error_out = "Device number out of range";
558 err = setup_one_line(lines, n, init, INIT_ONE, error_out);
563 for(i = 0; i < num; i++) {
564 err = setup_one_line(lines, i, init, INIT_ALL,
570 return n == -1 ? num : n;
573 int line_config(struct line *lines, unsigned int num, char *str,
574 const struct chan_opts *opts, char **error_out)
581 *error_out = "Can't configure all devices from mconsole";
585 new = kstrdup(str, GFP_KERNEL);
587 *error_out = "Failed to allocate memory";
590 n = line_setup(lines, num, new, error_out);
595 return parse_chan_pair(line->init_str, line, n, opts, error_out);
598 int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
599 int size, char **error_out)
605 dev = simple_strtoul(name, &end, 0);
606 if ((*end != '\0') || (end == name)) {
607 *error_out = "line_get_config failed to parse device number";
611 if ((dev < 0) || (dev >= num)) {
612 *error_out = "device number out of range";
618 spin_lock(&line->count_lock);
620 CONFIG_CHUNK(str, size, n, "none", 1);
621 else if (line->tty == NULL)
622 CONFIG_CHUNK(str, size, n, line->init_str, 1);
623 else n = chan_config_string(&line->chan_list, str, size, error_out);
624 spin_unlock(&line->count_lock);
629 int line_id(char **str, int *start_out, int *end_out)
634 n = simple_strtoul(*str, &end, 0);
635 if ((*end != '\0') || (end == *str))
644 int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
647 char config[sizeof("conxxxx=none\0")];
649 sprintf(config, "%d=none", n);
650 err = line_setup(lines, num, config, error_out);
656 struct tty_driver *register_lines(struct line_driver *line_driver,
657 const struct tty_operations *ops,
658 struct line *lines, int nlines)
661 struct tty_driver *driver = alloc_tty_driver(nlines);
666 driver->driver_name = line_driver->name;
667 driver->name = line_driver->device_name;
668 driver->major = line_driver->major;
669 driver->minor_start = line_driver->minor_start;
670 driver->type = line_driver->type;
671 driver->subtype = line_driver->subtype;
672 driver->flags = TTY_DRIVER_REAL_RAW;
673 driver->init_termios = tty_std_termios;
674 tty_set_operations(driver, ops);
676 if (tty_register_driver(driver)) {
677 printk(KERN_ERR "register_lines : can't register %s driver\n",
679 put_tty_driver(driver);
683 for(i = 0; i < nlines; i++) {
685 tty_unregister_device(driver, i);
688 mconsole_register_dev(&line_driver->mc);
692 static DEFINE_SPINLOCK(winch_handler_lock);
693 static LIST_HEAD(winch_handlers);
695 void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
701 for(i = 0; i < nlines; i++) {
703 INIT_LIST_HEAD(&line->chan_list);
705 if (line->init_str == NULL)
708 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
709 if (line->init_str == NULL)
710 printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
712 if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
713 printk(KERN_ERR "parse_chan_pair failed for "
714 "device %d : %s\n", i, error);
721 struct list_head list;
725 struct tty_struct *tty;
729 static void free_winch(struct winch *winch, int free_irq_ok)
731 list_del(&winch->list);
733 if (winch->pid != -1)
734 os_kill_process(winch->pid, 1);
736 os_close_file(winch->fd);
737 if (winch->stack != 0)
738 free_stack(winch->stack, 0);
740 free_irq(WINCH_IRQ, winch);
744 static irqreturn_t winch_interrupt(int irq, void *data)
746 struct winch *winch = data;
747 struct tty_struct *tty;
752 if (winch->fd != -1) {
753 err = generic_read(winch->fd, &c, NULL);
755 if (err != -EAGAIN) {
756 printk(KERN_ERR "winch_interrupt : "
757 "read failed, errno = %d\n", -err);
758 printk(KERN_ERR "fd %d is losing SIGWINCH "
759 "support\n", winch->tty_fd);
760 free_winch(winch, 0);
768 line = tty->driver_data;
770 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
771 &tty->winsize.ws_col);
772 kill_pgrp(tty->pgrp, SIGWINCH, 1);
777 reactivate_fd(winch->fd, WINCH_IRQ);
781 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
786 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
788 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
792 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
799 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
800 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
801 "winch", winch) < 0) {
802 printk(KERN_ERR "register_winch_irq - failed to register "
807 spin_lock(&winch_handler_lock);
808 list_add(&winch->list, &winch_handlers);
809 spin_unlock(&winch_handler_lock);
816 os_kill_process(pid, 1);
819 free_stack(stack, 0);
822 static void unregister_winch(struct tty_struct *tty)
824 struct list_head *ele;
827 spin_lock(&winch_handler_lock);
829 list_for_each(ele, &winch_handlers) {
830 winch = list_entry(ele, struct winch, list);
831 if (winch->tty == tty) {
832 free_winch(winch, 1);
836 spin_unlock(&winch_handler_lock);
839 static void winch_cleanup(void)
841 struct list_head *ele, *next;
844 spin_lock(&winch_handler_lock);
846 list_for_each_safe(ele, next, &winch_handlers) {
847 winch = list_entry(ele, struct winch, list);
848 free_winch(winch, 1);
851 spin_unlock(&winch_handler_lock);
853 __uml_exitcall(winch_cleanup);
855 char *add_xterm_umid(char *base)
864 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
865 title = kmalloc(len, GFP_KERNEL);
867 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
871 snprintf(title, len, "%s (%s)", base, umid);