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);
76 memcpy(line->buffer, buf, len - end);
77 line->tail = line->buffer + len - end;
83 static int flush_buffer(struct line *line)
87 if((line->buffer == NULL) || (line->head == line->tail)) return(1);
89 if(line->tail < line->head){
90 count = line->buffer + LINE_BUFSIZE - line->head;
91 n = write_chan(&line->chan_list, line->head, count,
92 line->driver->write_irq);
94 if(n == count) line->head = line->buffer;
101 count = line->tail - line->head;
102 n = write_chan(&line->chan_list, line->head, count,
103 line->driver->write_irq);
107 return(line->head == line->tail);
110 int line_write(struct line *lines, struct tty_struct *tty, const char *buf, int len)
114 int n, err, i, ret = 0;
116 if(tty->stopped) return 0;
122 if(line->head != line->tail){
123 local_irq_save(flags);
124 ret += buffer_data(line, buf, len);
125 err = flush_buffer(line);
126 local_irq_restore(flags);
131 n = write_chan(&line->chan_list, buf, len,
132 line->driver->write_irq);
141 ret += buffer_data(line, buf + n, len);
148 static irqreturn_t line_write_interrupt(int irq, void *data,
149 struct pt_regs *unused)
151 struct line *dev = data;
152 struct tty_struct *tty = dev->tty;
155 err = flush_buffer(dev);
159 dev->head = dev->buffer;
160 dev->tail = dev->buffer;
166 if(test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
167 (tty->ldisc.write_wakeup != NULL))
168 (tty->ldisc.write_wakeup)(tty);
171 * In blocking mode, everything sleeps on tty->write_wait.
172 * Sleeping in the console driver would break non-blocking
176 if(waitqueue_active(&tty->write_wait))
177 wake_up_interruptible(&tty->write_wait);
181 int line_setup_irq(int fd, int input, int output, void *data)
183 struct line *line = data;
184 struct line_driver *driver = line->driver;
185 int err = 0, flags = SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM;
187 if(input) err = um_request_irq(driver->read_irq, fd, IRQ_READ,
188 line_interrupt, flags,
189 driver->read_irq_name, line);
191 if(output) err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
192 line_write_interrupt, flags,
193 driver->write_irq_name, line);
198 void line_disable(struct line *line, int current_irq)
200 if(!line->have_irq) return;
202 if(line->driver->read_irq == current_irq)
203 free_irq_later(line->driver->read_irq, line);
205 free_irq_by_irq_and_dev(line->driver->read_irq, line);
206 free_irq(line->driver->read_irq, line);
209 if(line->driver->write_irq == current_irq)
210 free_irq_later(line->driver->write_irq, line);
212 free_irq_by_irq_and_dev(line->driver->write_irq, line);
213 free_irq(line->driver->write_irq, line);
219 int line_open(struct line *lines, struct tty_struct *tty,
220 struct chan_opts *opts)
225 if(tty == NULL) n = 0;
230 if(line->count == 0){
235 if(list_empty(&line->chan_list)){
236 err = parse_chan_pair(line->init_str, &line->chan_list,
237 line->init_pri, n, opts);
239 err = open_chan(&line->chan_list);
242 enable_chan(&line->chan_list, line);
243 INIT_WORK(&line->task, line_timer_cb, line);
247 chan_enable_winch(&line->chan_list, line);
251 /* This is outside the if because the initial console is opened
257 tty->driver_data = line;
258 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
259 &tty->winsize.ws_col);
268 void line_close(struct line *lines, struct tty_struct *tty)
273 if(tty == NULL) n = 0;
280 /* I don't like this, but I can't think of anything better. What's
281 * going on is that the tty is in the process of being closed for
282 * the last time. Its count hasn't been dropped yet, so it's still
283 * at 1. This may happen when line->count != 0 because of the initial
284 * console open (without a tty) bumping it up to 1.
286 if((line->tty != NULL) && (line->tty->count == 1))
289 line_disable(line, -1);
293 void close_lines(struct line *lines, int nlines)
297 for(i = 0; i < nlines; i++)
298 close_chan(&lines[i].chan_list);
301 int line_setup(struct line *lines, int num, char *init, int all_allowed)
306 if(*init == '=') n = -1;
308 n = simple_strtoul(init, &end, 0);
310 printk(KERN_ERR "line_setup failed to parse \"%s\"\n",
317 if((n >= 0) && (n >= num)){
318 printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
323 if(lines[n].count > 0){
324 printk("line_setup - device %d is open\n", n);
327 if(lines[n].init_pri <= INIT_ONE){
328 lines[n].init_pri = INIT_ONE;
329 if(!strcmp(init, "none")) lines[n].valid = 0;
331 lines[n].init_str = init;
336 else if(!all_allowed){
337 printk("line_setup - can't configure all devices from "
342 for(i = 0; i < num; i++){
343 if(lines[i].init_pri <= INIT_ALL){
344 lines[i].init_pri = INIT_ALL;
345 if(!strcmp(init, "none")) lines[i].valid = 0;
347 lines[i].init_str = init;
356 int line_config(struct line *lines, int num, char *str)
358 char *new = uml_strdup(str);
361 printk("line_config - uml_strdup failed\n");
364 return(!line_setup(lines, num, new, 0));
367 int line_get_config(char *name, struct line *lines, int num, char *str,
368 int size, char **error_out)
374 dev = simple_strtoul(name, &end, 0);
375 if((*end != '\0') || (end == name)){
376 *error_out = "line_get_config failed to parse device number";
380 if((dev < 0) || (dev >= num)){
381 *error_out = "device number of of range";
389 CONFIG_CHUNK(str, size, n, "none", 1);
390 else if(line->count == 0)
391 CONFIG_CHUNK(str, size, n, line->init_str, 1);
392 else n = chan_config_string(&line->chan_list, str, size, error_out);
398 int line_remove(struct line *lines, int num, char *str)
400 char config[sizeof("conxxxx=none\0")];
402 sprintf(config, "%s=none", str);
403 return(!line_setup(lines, num, config, 0));
406 int line_write_room(struct tty_struct *tty)
408 struct line *dev = tty->driver_data;
410 return(write_room(dev));
413 struct tty_driver *line_register_devfs(struct lines *set,
414 struct line_driver *line_driver,
415 struct tty_operations *ops, struct line *lines,
420 struct tty_driver *driver = alloc_tty_driver(nlines);
425 driver->driver_name = line_driver->name;
426 driver->name = line_driver->device_name;
427 driver->devfs_name = line_driver->devfs_name;
428 driver->major = line_driver->major;
429 driver->minor_start = line_driver->minor_start;
430 driver->type = line_driver->type;
431 driver->subtype = line_driver->subtype;
432 driver->flags = TTY_DRIVER_REAL_RAW;
433 driver->init_termios = tty_std_termios;
434 tty_set_operations(driver, ops);
436 if (tty_register_driver(driver))
437 panic("line_register_devfs : Couldn't register driver\n");
439 from = line_driver->symlink_from;
440 to = line_driver->symlink_to;
441 err = devfs_mk_symlink(from, to);
442 if(err) printk("Symlink creation from /dev/%s to /dev/%s "
443 "returned %d\n", from, to, err);
445 for(i = 0; i < nlines; i++){
447 tty_unregister_device(driver, i);
450 mconsole_register_dev(&line_driver->mc);
454 void lines_init(struct line *lines, int nlines)
459 for(i = 0; i < nlines; i++){
461 INIT_LIST_HEAD(&line->chan_list);
462 sema_init(&line->sem, 1);
463 if(line->init_str != NULL){
464 line->init_str = uml_strdup(line->init_str);
465 if(line->init_str == NULL)
466 printk("lines_init - uml_strdup returned "
473 struct list_head list;
480 irqreturn_t winch_interrupt(int irq, void *data, struct pt_regs *unused)
482 struct winch *winch = data;
483 struct tty_struct *tty;
488 err = generic_read(winch->fd, &c, NULL);
491 printk("winch_interrupt : read failed, "
492 "errno = %d\n", -err);
493 printk("fd %d is losing SIGWINCH support\n",
500 tty = winch->line->tty;
502 chan_window_size(&winch->line->chan_list,
503 &tty->winsize.ws_row,
504 &tty->winsize.ws_col);
505 kill_pg(tty->pgrp, SIGWINCH, 1);
509 reactivate_fd(winch->fd, WINCH_IRQ);
513 DECLARE_MUTEX(winch_handler_sem);
514 LIST_HEAD(winch_handlers);
516 void register_winch_irq(int fd, int tty_fd, int pid, void *line)
520 down(&winch_handler_sem);
521 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
523 printk("register_winch_irq - kmalloc failed\n");
526 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
531 list_add(&winch->list, &winch_handlers);
532 if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
533 SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
535 printk("register_winch_irq - failed to register IRQ\n");
537 up(&winch_handler_sem);
540 static void winch_cleanup(void)
542 struct list_head *ele;
545 list_for_each(ele, &winch_handlers){
546 winch = list_entry(ele, struct winch, list);
548 deactivate_fd(winch->fd, WINCH_IRQ);
549 os_close_file(winch->fd);
552 os_kill_process(winch->pid, 1);
556 __uml_exitcall(winch_cleanup);
558 char *add_xterm_umid(char *base)
564 if(umid == NULL) return(base);
566 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
567 title = kmalloc(len, GFP_KERNEL);
569 printk("Failed to allocate buffer for xterm title\n");
573 snprintf(title, len, "%s (%s)", base, umid);
578 * Overrides for Emacs so that we follow Linus's tabbing style.
579 * Emacs will notice this stuff at the end of the file and automatically
580 * adjust the settings for this buffer only. This must remain at the end
582 * ---------------------------------------------------------------------------
584 * c-file-style: "linux"