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 "linux/devfs_fs_kernel.h"
12 #include "asm/uaccess.h"
13 #include "chan_kern.h"
17 #include "user_util.h"
18 #include "kern_util.h"
22 #define LINE_BUFSIZE 4096
24 static irqreturn_t line_interrupt(int irq, void *data, struct pt_regs *unused)
26 struct tty_struct *tty = data;
27 struct line *line = tty->driver_data;
30 chan_interrupt(&line->chan_list, &line->task, tty, irq);
34 static void line_timer_cb(void *arg)
36 struct tty_struct *tty = arg;
37 struct line *line = tty->driver_data;
39 line_interrupt(line->driver->read_irq, arg, NULL);
42 /* Returns the free space inside the ring buffer of this line.
44 * Should be called while holding line->lock (this does not modify datas).
46 static int write_room(struct line *line)
50 if (line->buffer == NULL)
51 return LINE_BUFSIZE - 1;
53 /* This is for the case where the buffer is wrapped! */
54 n = line->head - line->tail;
57 n = LINE_BUFSIZE + n; /* The other case */
61 int line_write_room(struct tty_struct *tty)
63 struct line *line = tty->driver_data;
70 spin_lock_irqsave(&line->lock, flags);
71 room = write_room(line);
72 spin_unlock_irqrestore(&line->lock, flags);
74 /*XXX: Warning to remove */
76 printk(KERN_DEBUG "%s: %s: no room left in buffer\n",
77 __FUNCTION__,tty->name);
81 int line_chars_in_buffer(struct tty_struct *tty)
83 struct line *line = tty->driver_data;
87 spin_lock_irqsave(&line->lock, flags);
89 /*write_room subtracts 1 for the needed NULL, so we readd it.*/
90 ret = LINE_BUFSIZE - (write_room(line) + 1);
91 spin_unlock_irqrestore(&line->lock, flags);
97 * This copies the content of buf into the circular buffer associated with
99 * The return value is the number of characters actually copied, i.e. the ones
100 * for which there was space: this function is not supposed to ever flush out
101 * the circular buffer.
103 * Must be called while holding line->lock!
105 static int buffer_data(struct line *line, const char *buf, int len)
109 if(line->buffer == NULL){
110 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
111 if (line->buffer == NULL) {
112 printk("buffer_data - atomic allocation failed\n");
115 line->head = line->buffer;
116 line->tail = line->buffer;
119 room = write_room(line);
120 len = (len > room) ? room : len;
122 end = line->buffer + LINE_BUFSIZE - line->tail;
125 memcpy(line->tail, buf, len);
128 /* The circular buffer is wrapping */
129 memcpy(line->tail, buf, end);
131 memcpy(line->buffer, buf, len - end);
132 line->tail = line->buffer + len - end;
139 * Flushes the ring buffer to the output channels. That is, write_chan is
140 * called, passing it line->head as buffer, and an appropriate count.
142 * On exit, returns 1 when the buffer is empty,
143 * 0 when the buffer is not empty on exit,
144 * and -errno when an error occurred.
146 * Must be called while holding line->lock!*/
147 static int flush_buffer(struct line *line)
151 if ((line->buffer == NULL) || (line->head == line->tail))
154 if (line->tail < line->head) {
155 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
156 count = line->buffer + LINE_BUFSIZE - line->head;
158 n = write_chan(&line->chan_list, line->head, count,
159 line->driver->write_irq);
163 /* We have flushed from ->head to buffer end, now we
164 * must flush only from the beginning to ->tail.*/
165 line->head = line->buffer;
172 count = line->tail - line->head;
173 n = write_chan(&line->chan_list, line->head, count,
174 line->driver->write_irq);
180 return line->head == line->tail;
183 void line_flush_buffer(struct tty_struct *tty)
185 struct line *line = tty->driver_data;
189 /*XXX: copied from line_write, verify if it is correct!*/
194 spin_lock_irqsave(&line->lock, flags);
195 err = flush_buffer(line);
198 spin_unlock_irqrestore(&line->lock, flags);
202 /* We map both ->flush_chars and ->put_char (which go in pair) onto ->flush_buffer
203 * and ->write. Hope it's not that bad.*/
204 void line_flush_chars(struct tty_struct *tty)
206 line_flush_buffer(tty);
209 void line_put_char(struct tty_struct *tty, unsigned char ch)
211 line_write(tty, &ch, sizeof(ch));
214 int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
216 struct line *line = tty->driver_data;
223 spin_lock_irqsave(&line->lock, flags);
224 if (line->head != line->tail) {
225 ret = buffer_data(line, buf, len);
226 err = flush_buffer(line);
227 if (err <= 0 && (err != -EAGAIN || !ret))
230 n = write_chan(&line->chan_list, buf, len,
231 line->driver->write_irq);
240 ret += buffer_data(line, buf + n, len);
243 spin_unlock_irqrestore(&line->lock, flags);
247 void line_set_termios(struct tty_struct *tty, struct termios * old)
257 /* don't print these, they flood the log ... */
258 { TCGETS, NULL, "TCGETS" },
259 { TCSETS, NULL, "TCSETS" },
260 { TCSETSW, NULL, "TCSETSW" },
261 { TCFLSH, NULL, "TCFLSH" },
262 { TCSBRK, NULL, "TCSBRK" },
264 /* general tty stuff */
265 { TCSETSF, KERN_DEBUG, "TCSETSF" },
266 { TCGETA, KERN_DEBUG, "TCGETA" },
267 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
268 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
269 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
271 /* linux-specific ones */
272 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
273 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
274 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
275 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
278 int line_ioctl(struct tty_struct *tty, struct file * file,
279 unsigned int cmd, unsigned long arg)
323 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
324 if (cmd == tty_ioctls[i].cmd)
326 if (i < ARRAY_SIZE(tty_ioctls)) {
327 if (NULL != tty_ioctls[i].level)
328 printk("%s%s: %s: ioctl %s called\n",
329 tty_ioctls[i].level, __FUNCTION__,
330 tty->name, tty_ioctls[i].name);
332 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
333 __FUNCTION__, tty->name, cmd);
341 static irqreturn_t line_write_interrupt(int irq, void *data,
342 struct pt_regs *unused)
344 struct tty_struct *tty = data;
345 struct line *line = tty->driver_data;
348 /* Interrupts are enabled here because we registered the interrupt with
349 * SA_INTERRUPT (see line_setup_irq).*/
351 spin_lock_irq(&line->lock);
352 err = flush_buffer(line);
356 line->head = line->buffer;
357 line->tail = line->buffer;
359 spin_unlock_irq(&line->lock);
364 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
365 (tty->ldisc.write_wakeup != NULL))
366 (tty->ldisc.write_wakeup)(tty);
369 * In blocking mode, everything sleeps on tty->write_wait.
370 * Sleeping in the console driver would break non-blocking
374 if (waitqueue_active(&tty->write_wait))
375 wake_up_interruptible(&tty->write_wait);
379 int line_setup_irq(int fd, int input, int output, struct tty_struct *tty)
381 struct line *line = tty->driver_data;
382 struct line_driver *driver = line->driver;
383 int err = 0, flags = SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM;
386 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
387 line_interrupt, flags,
388 driver->read_irq_name, tty);
392 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
393 line_write_interrupt, flags,
394 driver->write_irq_name, tty);
399 void line_disable(struct tty_struct *tty, int current_irq)
401 struct line *line = tty->driver_data;
406 if(line->driver->read_irq == current_irq)
407 free_irq_later(line->driver->read_irq, tty);
409 free_irq_by_irq_and_dev(line->driver->read_irq, tty);
410 free_irq(line->driver->read_irq, tty);
413 if(line->driver->write_irq == current_irq)
414 free_irq_later(line->driver->write_irq, tty);
416 free_irq_by_irq_and_dev(line->driver->write_irq, tty);
417 free_irq(line->driver->write_irq, tty);
423 int line_open(struct line *lines, struct tty_struct *tty,
424 struct chan_opts *opts)
429 line = &lines[tty->index];
430 tty->driver_data = line;
432 /* The IRQ which takes this lock is not yet enabled and won't be run
433 * before the end, so we don't need to use spin_lock_irq.*/
434 spin_lock(&line->lock);
435 if (tty->count == 1) {
440 if (list_empty(&line->chan_list)) {
441 err = parse_chan_pair(line->init_str, &line->chan_list,
442 line->init_pri, tty->index, opts);
444 err = open_chan(&line->chan_list);
447 /* Here the interrupt is registered.*/
448 enable_chan(&line->chan_list, tty);
449 INIT_WORK(&line->task, line_timer_cb, tty);
453 chan_enable_winch(&line->chan_list, tty);
456 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
457 &tty->winsize.ws_col);
461 spin_unlock(&line->lock);
465 void line_close(struct tty_struct *tty, struct file * filp)
467 struct line *line = tty->driver_data;
469 /* XXX: I assume this should be called in process context, not with interrupt
471 spin_lock_irq(&line->lock);
473 /* We ignore the error anyway! */
477 if (tty->count == 1) {
478 line_disable(tty, -1);
479 tty->driver_data = NULL;
481 spin_unlock_irq(&line->lock);
484 void close_lines(struct line *lines, int nlines)
488 for(i = 0; i < nlines; i++)
489 close_chan(&lines[i].chan_list);
492 /* Common setup code for both startup command line and mconsole initialization.
493 * @lines contains the the array (of size @num) to modify;
494 * @init is the setup string;
495 * @all_allowed is a boolean saying if we can setup the whole @lines
496 * at once. For instance, it will be usually true for startup init. (where we
497 * can use con=xterm) and false for mconsole.*/
499 int line_setup(struct line *lines, unsigned int num, char *init, int all_allowed)
505 /* We said con=/ssl= instead of con#=, so we are configuring all
506 * consoles at once.*/
509 n = simple_strtoul(init, &end, 0);
511 printk(KERN_ERR "line_setup failed to parse \"%s\"\n",
519 if (n >= (signed int) num) {
520 printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
524 if (lines[n].count > 0) {
525 printk("line_setup - device %d is open\n", n);
528 if (lines[n].init_pri <= INIT_ONE){
529 lines[n].init_pri = INIT_ONE;
530 if (!strcmp(init, "none"))
533 lines[n].init_str = init;
537 } else if(!all_allowed){
538 printk("line_setup - can't configure all devices from "
542 for(i = 0; i < num; i++){
543 if(lines[i].init_pri <= INIT_ALL){
544 lines[i].init_pri = INIT_ALL;
545 if(!strcmp(init, "none")) lines[i].valid = 0;
547 lines[i].init_str = init;
556 int line_config(struct line *lines, unsigned int num, char *str)
558 char *new = uml_strdup(str);
561 printk("line_config - uml_strdup failed\n");
564 return !line_setup(lines, num, new, 0);
567 int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
568 int size, char **error_out)
574 dev = simple_strtoul(name, &end, 0);
575 if((*end != '\0') || (end == name)){
576 *error_out = "line_get_config failed to parse device number";
580 if((dev < 0) || (dev >= num)){
581 *error_out = "device number out of range";
587 spin_lock(&line->lock);
589 CONFIG_CHUNK(str, size, n, "none", 1);
590 else if(line->count == 0)
591 CONFIG_CHUNK(str, size, n, line->init_str, 1);
592 else n = chan_config_string(&line->chan_list, str, size, error_out);
593 spin_unlock(&line->lock);
598 int line_remove(struct line *lines, unsigned int num, char *str)
600 char config[sizeof("conxxxx=none\0")];
602 sprintf(config, "%s=none", str);
603 return !line_setup(lines, num, config, 0);
606 struct tty_driver *line_register_devfs(struct lines *set,
607 struct line_driver *line_driver,
608 struct tty_operations *ops, struct line *lines,
612 struct tty_driver *driver = alloc_tty_driver(nlines);
617 driver->driver_name = line_driver->name;
618 driver->name = line_driver->device_name;
619 driver->devfs_name = line_driver->devfs_name;
620 driver->major = line_driver->major;
621 driver->minor_start = line_driver->minor_start;
622 driver->type = line_driver->type;
623 driver->subtype = line_driver->subtype;
624 driver->flags = TTY_DRIVER_REAL_RAW;
625 driver->init_termios = tty_std_termios;
626 tty_set_operations(driver, ops);
628 if (tty_register_driver(driver)) {
629 printk("%s: can't register %s driver\n",
630 __FUNCTION__,line_driver->name);
631 put_tty_driver(driver);
635 for(i = 0; i < nlines; i++){
637 tty_unregister_device(driver, i);
640 mconsole_register_dev(&line_driver->mc);
644 void lines_init(struct line *lines, int nlines)
649 for(i = 0; i < nlines; i++){
651 INIT_LIST_HEAD(&line->chan_list);
652 spin_lock_init(&line->lock);
653 if(line->init_str != NULL){
654 line->init_str = uml_strdup(line->init_str);
655 if(line->init_str == NULL)
656 printk("lines_init - uml_strdup returned "
663 struct list_head list;
667 struct tty_struct *tty;
670 irqreturn_t winch_interrupt(int irq, void *data, struct pt_regs *unused)
672 struct winch *winch = data;
673 struct tty_struct *tty;
679 err = generic_read(winch->fd, &c, NULL);
682 printk("winch_interrupt : read failed, "
683 "errno = %d\n", -err);
684 printk("fd %d is losing SIGWINCH support\n",
693 line = tty->driver_data;
694 chan_window_size(&line->chan_list,
695 &tty->winsize.ws_row,
696 &tty->winsize.ws_col);
697 kill_pg(tty->pgrp, SIGWINCH, 1);
701 reactivate_fd(winch->fd, WINCH_IRQ);
705 DECLARE_MUTEX(winch_handler_sem);
706 LIST_HEAD(winch_handlers);
708 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty)
712 down(&winch_handler_sem);
713 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
715 printk("register_winch_irq - kmalloc failed\n");
718 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
723 list_add(&winch->list, &winch_handlers);
724 if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
725 SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
727 printk("register_winch_irq - failed to register IRQ\n");
729 up(&winch_handler_sem);
732 static void winch_cleanup(void)
734 struct list_head *ele;
737 list_for_each(ele, &winch_handlers){
738 winch = list_entry(ele, struct winch, list);
740 deactivate_fd(winch->fd, WINCH_IRQ);
741 os_close_file(winch->fd);
744 os_kill_process(winch->pid, 1);
747 __uml_exitcall(winch_cleanup);
749 char *add_xterm_umid(char *base)
758 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
759 title = kmalloc(len, GFP_KERNEL);
761 printk("Failed to allocate buffer for xterm title\n");
765 snprintf(title, len, "%s (%s)", base, umid);