2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include "linux/kernel.h"
7 #include "linux/sched.h"
8 #include "linux/slab.h"
9 #include "linux/list.h"
11 #include "linux/interrupt.h"
12 #include "asm/uaccess.h"
13 #include "chan_kern.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);
228 n = write_chan(&line->chan_list, buf, len,
229 line->driver->write_irq);
238 ret += buffer_data(line, buf + n, len);
241 spin_unlock_irqrestore(&line->lock, flags);
245 void line_set_termios(struct tty_struct *tty, struct ktermios * old)
250 static const struct {
255 /* don't print these, they flood the log ... */
256 { TCGETS, NULL, "TCGETS" },
257 { TCSETS, NULL, "TCSETS" },
258 { TCSETSW, NULL, "TCSETSW" },
259 { TCFLSH, NULL, "TCFLSH" },
260 { TCSBRK, NULL, "TCSBRK" },
262 /* general tty stuff */
263 { TCSETSF, KERN_DEBUG, "TCSETSF" },
264 { TCGETA, KERN_DEBUG, "TCGETA" },
265 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
266 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
267 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
269 /* linux-specific ones */
270 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
271 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
272 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
273 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
276 int line_ioctl(struct tty_struct *tty, struct file * file,
277 unsigned int cmd, unsigned long arg)
321 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
322 if (cmd == tty_ioctls[i].cmd)
324 if (i < ARRAY_SIZE(tty_ioctls)) {
325 if (NULL != tty_ioctls[i].level)
326 printk("%s%s: %s: ioctl %s called\n",
327 tty_ioctls[i].level, __FUNCTION__,
328 tty->name, tty_ioctls[i].name);
330 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
331 __FUNCTION__, tty->name, cmd);
339 void line_throttle(struct tty_struct *tty)
341 struct line *line = tty->driver_data;
343 deactivate_chan(&line->chan_list, line->driver->read_irq);
347 void line_unthrottle(struct tty_struct *tty)
349 struct line *line = tty->driver_data;
352 chan_interrupt(&line->chan_list, &line->task, tty,
353 line->driver->read_irq);
355 /* Maybe there is enough stuff pending that calling the interrupt
356 * throttles us again. In this case, line->throttled will be 1
357 * again and we shouldn't turn the interrupt back on.
360 reactivate_chan(&line->chan_list, line->driver->read_irq);
363 static irqreturn_t line_write_interrupt(int irq, void *data)
365 struct chan *chan = data;
366 struct line *line = chan->line;
367 struct tty_struct *tty = line->tty;
370 /* Interrupts are disabled here because we registered the interrupt with
371 * IRQF_DISABLED (see line_setup_irq).*/
373 spin_lock(&line->lock);
374 err = flush_buffer(line);
378 line->head = line->buffer;
379 line->tail = line->buffer;
381 spin_unlock(&line->lock);
386 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
387 (tty->ldisc.write_wakeup != NULL))
388 (tty->ldisc.write_wakeup)(tty);
391 * In blocking mode, everything sleeps on tty->write_wait.
392 * Sleeping in the console driver would break non-blocking
396 if (waitqueue_active(&tty->write_wait))
397 wake_up_interruptible(&tty->write_wait);
401 int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
403 const struct line_driver *driver = line->driver;
404 int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
407 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
408 line_interrupt, flags,
409 driver->read_irq_name, data);
413 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
414 line_write_interrupt, flags,
415 driver->write_irq_name, data);
420 /* Normally, a driver like this can rely mostly on the tty layer
421 * locking, particularly when it comes to the driver structure.
422 * However, in this case, mconsole requests can come in "from the
423 * side", and race with opens and closes.
425 * mconsole config requests will want to be sure the device isn't in
426 * use, and get_config, open, and close will want a stable
427 * configuration. The checking and modification of the configuration
428 * is done under a spinlock. Checking whether the device is in use is
429 * line->tty->count > 1, also under the spinlock.
431 * tty->count serves to decide whether the device should be enabled or
432 * disabled on the host. If it's equal to 1, then we are doing the
433 * first open or last close. Otherwise, open and close just return.
436 int line_open(struct line *lines, struct tty_struct *tty)
438 struct line *line = &lines[tty->index];
441 spin_lock(&line->count_lock);
449 spin_unlock(&line->count_lock);
451 tty->driver_data = line;
454 err = enable_chan(line);
458 INIT_DELAYED_WORK(&line->task, line_timer_cb);
461 chan_enable_winch(&line->chan_list, tty);
465 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
466 &tty->winsize.ws_col);
471 spin_unlock(&line->count_lock);
475 static void unregister_winch(struct tty_struct *tty);
477 void line_close(struct tty_struct *tty, struct file * filp)
479 struct line *line = tty->driver_data;
481 /* If line_open fails (and tty->driver_data is never set),
482 * tty_open will call line_close. So just return in this case.
487 /* We ignore the error anyway! */
490 spin_lock(&line->count_lock);
497 spin_unlock(&line->count_lock);
500 tty->driver_data = NULL;
503 unregister_winch(tty);
510 spin_unlock(&line->count_lock);
513 void close_lines(struct line *lines, int nlines)
517 for(i = 0; i < nlines; i++)
518 close_chan(&lines[i].chan_list, 0);
521 static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
524 struct line *line = &lines[n];
527 spin_lock(&line->count_lock);
529 if(line->tty != NULL){
530 *error_out = "Device is already open";
534 if (line->init_pri <= init_prio){
535 line->init_pri = init_prio;
536 if (!strcmp(init, "none"))
539 line->init_str = init;
545 spin_unlock(&line->count_lock);
549 /* Common setup code for both startup command line and mconsole initialization.
550 * @lines contains the array (of size @num) to modify;
551 * @init is the setup string;
552 * @error_out is an error string in the case of failure;
555 int line_setup(struct line *lines, unsigned int num, char *init,
562 /* We said con=/ssl= instead of con#=, so we are configuring all
563 * consoles at once.*/
567 n = simple_strtoul(init, &end, 0);
569 *error_out = "Couldn't parse device number";
576 if (n >= (signed int) num) {
577 *error_out = "Device number out of range";
581 err = setup_one_line(lines, n, init, INIT_ONE, error_out);
586 for(i = 0; i < num; i++){
587 err = setup_one_line(lines, i, init, INIT_ALL,
593 return n == -1 ? num : n;
596 int line_config(struct line *lines, unsigned int num, char *str,
597 const struct chan_opts *opts, char **error_out)
604 *error_out = "Can't configure all devices from mconsole";
608 new = kstrdup(str, GFP_KERNEL);
610 *error_out = "Failed to allocate memory";
613 n = line_setup(lines, num, new, error_out);
618 return parse_chan_pair(line->init_str, line, n, opts, error_out);
621 int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
622 int size, char **error_out)
628 dev = simple_strtoul(name, &end, 0);
629 if((*end != '\0') || (end == name)){
630 *error_out = "line_get_config failed to parse device number";
634 if((dev < 0) || (dev >= num)){
635 *error_out = "device number out of range";
641 spin_lock(&line->count_lock);
643 CONFIG_CHUNK(str, size, n, "none", 1);
644 else if(line->tty == NULL)
645 CONFIG_CHUNK(str, size, n, line->init_str, 1);
646 else n = chan_config_string(&line->chan_list, str, size, error_out);
647 spin_unlock(&line->count_lock);
652 int line_id(char **str, int *start_out, int *end_out)
657 n = simple_strtoul(*str, &end, 0);
658 if((*end != '\0') || (end == *str))
667 int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
670 char config[sizeof("conxxxx=none\0")];
672 sprintf(config, "%d=none", n);
673 err = line_setup(lines, num, config, error_out);
679 struct tty_driver *register_lines(struct line_driver *line_driver,
680 const struct tty_operations *ops,
681 struct line *lines, int nlines)
684 struct tty_driver *driver = alloc_tty_driver(nlines);
689 driver->driver_name = line_driver->name;
690 driver->name = line_driver->device_name;
691 driver->major = line_driver->major;
692 driver->minor_start = line_driver->minor_start;
693 driver->type = line_driver->type;
694 driver->subtype = line_driver->subtype;
695 driver->flags = TTY_DRIVER_REAL_RAW;
696 driver->init_termios = tty_std_termios;
697 tty_set_operations(driver, ops);
699 if (tty_register_driver(driver)) {
700 printk("%s: can't register %s driver\n",
701 __FUNCTION__,line_driver->name);
702 put_tty_driver(driver);
706 for(i = 0; i < nlines; i++){
708 tty_unregister_device(driver, i);
711 mconsole_register_dev(&line_driver->mc);
715 static DEFINE_SPINLOCK(winch_handler_lock);
716 static LIST_HEAD(winch_handlers);
718 void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
724 for(i = 0; i < nlines; i++){
726 INIT_LIST_HEAD(&line->chan_list);
728 if(line->init_str == NULL)
731 line->init_str = kstrdup(line->init_str, GFP_KERNEL);
732 if(line->init_str == NULL)
733 printk("lines_init - kstrdup returned NULL\n");
735 if(parse_chan_pair(line->init_str, line, i, opts, &error)){
736 printk("parse_chan_pair failed for device %d : %s\n",
744 struct list_head list;
748 struct tty_struct *tty;
752 static void free_winch(struct winch *winch, int free_irq_ok)
754 list_del(&winch->list);
756 if (winch->pid != -1)
757 os_kill_process(winch->pid, 1);
759 os_close_file(winch->fd);
760 if (winch->stack != 0)
761 free_stack(winch->stack, 0);
763 free_irq(WINCH_IRQ, winch);
767 static irqreturn_t winch_interrupt(int irq, void *data)
769 struct winch *winch = data;
770 struct tty_struct *tty;
776 err = generic_read(winch->fd, &c, NULL);
779 printk("winch_interrupt : read failed, "
780 "errno = %d\n", -err);
781 printk("fd %d is losing SIGWINCH support\n",
783 free_winch(winch, 0);
791 line = tty->driver_data;
792 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
793 &tty->winsize.ws_col);
794 kill_pgrp(tty->pgrp, SIGWINCH, 1);
798 reactivate_fd(winch->fd, WINCH_IRQ);
802 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
807 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
809 printk("register_winch_irq - kmalloc failed\n");
813 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
820 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
821 IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
822 "winch", winch) < 0) {
823 printk("register_winch_irq - failed to register IRQ\n");
827 spin_lock(&winch_handler_lock);
828 list_add(&winch->list, &winch_handlers);
829 spin_unlock(&winch_handler_lock);
836 os_kill_process(pid, 1);
839 free_stack(stack, 0);
842 static void unregister_winch(struct tty_struct *tty)
844 struct list_head *ele;
847 spin_lock(&winch_handler_lock);
849 list_for_each(ele, &winch_handlers){
850 winch = list_entry(ele, struct winch, list);
851 if(winch->tty == tty){
852 free_winch(winch, 1);
856 spin_unlock(&winch_handler_lock);
859 static void winch_cleanup(void)
861 struct list_head *ele, *next;
864 spin_lock(&winch_handler_lock);
866 list_for_each_safe(ele, next, &winch_handlers){
867 winch = list_entry(ele, struct winch, list);
868 free_winch(winch, 1);
871 spin_unlock(&winch_handler_lock);
873 __uml_exitcall(winch_cleanup);
875 char *add_xterm_umid(char *base)
884 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
885 title = kmalloc(len, GFP_KERNEL);
887 printk("Failed to allocate buffer for xterm title\n");
891 snprintf(title, len, "%s (%s)", base, umid);