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"
9 #include "linux/interrupt.h"
10 #include "linux/devfs_fs_kernel.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, struct pt_regs *unused)
25 struct line *dev = data;
28 chan_interrupt(&dev->chan_list, &dev->task, dev->tty, irq,
33 static void line_timer_cb(void *arg)
35 struct line *dev = arg;
37 line_interrupt(dev->driver->read_irq, dev, NULL);
40 static int write_room(struct line *dev)
44 if(dev->buffer == NULL) return(LINE_BUFSIZE - 1);
46 n = dev->head - dev->tail;
47 if(n <= 0) n = LINE_BUFSIZE + n;
51 static int buffer_data(struct line *line, const char *buf, int len)
55 if(line->buffer == NULL){
56 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
57 if(line->buffer == NULL){
58 printk("buffer_data - atomic allocation failed\n");
61 line->head = line->buffer;
62 line->tail = line->buffer;
65 room = write_room(line);
66 len = (len > room) ? room : len;
68 end = line->buffer + LINE_BUFSIZE - line->tail;
70 memcpy(line->tail, buf, len);
74 memcpy(line->tail, buf, end);
77 memcpy(line->buffer, buf, len);
78 line->tail = line->buffer + len;
84 static int flush_buffer(struct line *line)
88 if((line->buffer == NULL) || (line->head == line->tail)) return(1);
90 if(line->tail < line->head){
91 count = line->buffer + LINE_BUFSIZE - line->head;
92 n = write_chan(&line->chan_list, line->head, count,
93 line->driver->write_irq);
95 if(n == count) line->head = line->buffer;
102 count = line->tail - line->head;
103 n = write_chan(&line->chan_list, line->head, count,
104 line->driver->write_irq);
108 return(line->head == line->tail);
111 int line_write(struct line *lines, struct tty_struct *tty, int from_user,
112 const char *buf, int len)
117 int n, err, i, ret = 0;
119 if(tty->stopped) return 0;
122 new = kmalloc(len, GFP_KERNEL);
125 n = copy_from_user(new, buf, len);
139 if(line->head != line->tail){
140 local_irq_save(flags);
141 ret += buffer_data(line, buf, len);
142 err = flush_buffer(line);
143 local_irq_restore(flags);
148 n = write_chan(&line->chan_list, buf, len,
149 line->driver->write_irq);
158 ret += buffer_data(line, buf + n, len);
168 static irqreturn_t line_write_interrupt(int irq, void *data,
169 struct pt_regs *unused)
171 struct line *dev = data;
172 struct tty_struct *tty = dev->tty;
175 err = flush_buffer(dev);
179 dev->head = dev->buffer;
180 dev->tail = dev->buffer;
186 if(test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
187 (tty->ldisc.write_wakeup != NULL))
188 (tty->ldisc.write_wakeup)(tty);
191 * In blocking mode, everything sleeps on tty->write_wait.
192 * Sleeping in the console driver would break non-blocking
196 if(waitqueue_active(&tty->write_wait))
197 wake_up_interruptible(&tty->write_wait);
201 int line_setup_irq(int fd, int input, int output, void *data)
203 struct line *line = data;
204 struct line_driver *driver = line->driver;
205 int err = 0, flags = SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM;
207 if(input) err = um_request_irq(driver->read_irq, fd, IRQ_READ,
208 line_interrupt, flags,
209 driver->read_irq_name, line);
211 if(output) err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
212 line_write_interrupt, flags,
213 driver->write_irq_name, line);
218 void line_disable(struct line *line, int current_irq)
220 if(!line->have_irq) return;
222 if(line->driver->read_irq == current_irq)
223 free_irq_later(line->driver->read_irq, line);
225 free_irq(line->driver->read_irq, line);
227 if(line->driver->write_irq == current_irq)
228 free_irq_later(line->driver->write_irq, line);
230 free_irq(line->driver->write_irq, line);
235 int line_open(struct line *lines, struct tty_struct *tty,
236 struct chan_opts *opts)
241 if(tty == NULL) n = 0;
246 if(line->count == 0){
251 if(list_empty(&line->chan_list)){
252 err = parse_chan_pair(line->init_str, &line->chan_list,
253 line->init_pri, n, opts);
255 err = open_chan(&line->chan_list);
258 enable_chan(&line->chan_list, line);
259 INIT_WORK(&line->task, line_timer_cb, line);
263 chan_enable_winch(&line->chan_list, line);
267 /* This is outside the if because the initial console is opened
273 tty->driver_data = line;
274 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
275 &tty->winsize.ws_col);
284 void line_close(struct line *lines, struct tty_struct *tty)
289 if(tty == NULL) n = 0;
296 /* I don't like this, but I can't think of anything better. What's
297 * going on is that the tty is in the process of being closed for
298 * the last time. Its count hasn't been dropped yet, so it's still
299 * at 1. This may happen when line->count != 0 because of the initial
300 * console open (without a tty) bumping it up to 1.
302 if((line->tty != NULL) && (line->tty->count == 1))
305 line_disable(line, -1);
309 void close_lines(struct line *lines, int nlines)
313 for(i = 0; i < nlines; i++)
314 close_chan(&lines[i].chan_list);
317 int line_setup(struct line *lines, int num, char *init, int all_allowed)
322 if(*init == '=') n = -1;
324 n = simple_strtoul(init, &end, 0);
326 printk(KERN_ERR "line_setup failed to parse \"%s\"\n",
333 if((n >= 0) && (n >= num)){
334 printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
339 if(lines[n].count > 0){
340 printk("line_setup - device %d is open\n", n);
343 if(lines[n].init_pri <= INIT_ONE){
344 lines[n].init_pri = INIT_ONE;
345 if(!strcmp(init, "none")) lines[n].valid = 0;
347 lines[n].init_str = init;
352 else if(!all_allowed){
353 printk("line_setup - can't configure all devices from "
358 for(i = 0; i < num; i++){
359 if(lines[i].init_pri <= INIT_ALL){
360 lines[i].init_pri = INIT_ALL;
361 if(!strcmp(init, "none")) lines[i].valid = 0;
363 lines[i].init_str = init;
372 int line_config(struct line *lines, int num, char *str)
374 char *new = uml_strdup(str);
377 printk("line_config - uml_strdup failed\n");
380 return(!line_setup(lines, num, new, 0));
383 int line_get_config(char *name, struct line *lines, int num, char *str,
384 int size, char **error_out)
390 dev = simple_strtoul(name, &end, 0);
391 if((*end != '\0') || (end == name)){
392 *error_out = "line_get_config failed to parse device number";
396 if((dev < 0) || (dev >= num)){
397 *error_out = "device number of of range";
405 CONFIG_CHUNK(str, size, n, "none", 1);
406 else if(line->count == 0)
407 CONFIG_CHUNK(str, size, n, line->init_str, 1);
408 else n = chan_config_string(&line->chan_list, str, size, error_out);
414 int line_remove(struct line *lines, int num, char *str)
416 char config[sizeof("conxxxx=none\0")];
418 sprintf(config, "%s=none", str);
419 return(!line_setup(lines, num, config, 0));
422 int line_write_room(struct tty_struct *tty)
424 struct line *dev = tty->driver_data;
426 return(write_room(dev));
429 struct tty_driver *line_register_devfs(struct lines *set,
430 struct line_driver *line_driver,
431 struct tty_operations *ops, struct line *lines,
436 struct tty_driver *driver = alloc_tty_driver(nlines);
441 driver->driver_name = line_driver->name;
442 driver->name = line_driver->device_name;
443 driver->devfs_name = line_driver->devfs_name;
444 driver->major = line_driver->major;
445 driver->minor_start = line_driver->minor_start;
446 driver->type = line_driver->type;
447 driver->subtype = line_driver->subtype;
448 driver->flags = TTY_DRIVER_REAL_RAW;
449 driver->init_termios = tty_std_termios;
450 tty_set_operations(driver, ops);
452 if (tty_register_driver(driver))
453 panic("line_register_devfs : Couldn't register driver\n");
455 from = line_driver->symlink_from;
456 to = line_driver->symlink_to;
457 err = devfs_mk_symlink(from, to);
458 if(err) printk("Symlink creation from /dev/%s to /dev/%s "
459 "returned %d\n", from, to, err);
461 for(i = 0; i < nlines; i++){
463 tty_unregister_device(driver, i);
466 mconsole_register_dev(&line_driver->mc);
470 void lines_init(struct line *lines, int nlines)
475 for(i = 0; i < nlines; i++){
477 INIT_LIST_HEAD(&line->chan_list);
478 sema_init(&line->sem, 1);
479 if(line->init_str != NULL){
480 line->init_str = uml_strdup(line->init_str);
481 if(line->init_str == NULL)
482 printk("lines_init - uml_strdup returned "
489 struct list_head list;
496 irqreturn_t winch_interrupt(int irq, void *data, struct pt_regs *unused)
498 struct winch *winch = data;
499 struct tty_struct *tty;
504 err = generic_read(winch->fd, &c, NULL);
507 printk("winch_interrupt : read failed, "
508 "errno = %d\n", -err);
509 printk("fd %d is losing SIGWINCH support\n",
516 tty = winch->line->tty;
518 chan_window_size(&winch->line->chan_list,
519 &tty->winsize.ws_row,
520 &tty->winsize.ws_col);
521 kill_pg(tty->pgrp, SIGWINCH, 1);
525 reactivate_fd(winch->fd, WINCH_IRQ);
529 DECLARE_MUTEX(winch_handler_sem);
530 LIST_HEAD(winch_handlers);
532 void register_winch_irq(int fd, int tty_fd, int pid, void *line)
536 down(&winch_handler_sem);
537 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
539 printk("register_winch_irq - kmalloc failed\n");
542 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
547 list_add(&winch->list, &winch_handlers);
548 if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
549 SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
551 printk("register_winch_irq - failed to register IRQ\n");
553 up(&winch_handler_sem);
556 static void winch_cleanup(void)
558 struct list_head *ele;
561 list_for_each(ele, &winch_handlers){
562 winch = list_entry(ele, struct winch, list);
564 deactivate_fd(winch->fd, WINCH_IRQ);
565 os_close_file(winch->fd);
568 os_kill_process(winch->pid, 1);
572 __uml_exitcall(winch_cleanup);
574 char *add_xterm_umid(char *base)
580 if(umid == NULL) return(base);
582 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
583 title = kmalloc(len, GFP_KERNEL);
585 printk("Failed to allocate buffer for xterm title\n");
589 snprintf(title, len, "%s (%s)", base, umid);
594 * Overrides for Emacs so that we follow Linus's tabbing style.
595 * Emacs will notice this stuff at the end of the file and automatically
596 * adjust the settings for this buffer only. This must remain at the end
598 * ---------------------------------------------------------------------------
600 * c-file-style: "linux"