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/devfs_fs_kernel.h"
11 #include "asm/uaccess.h"
12 #include "chan_kern.h"
16 #include "user_util.h"
17 #include "kern_util.h"
20 #define LINE_BUFSIZE 4096
22 void line_interrupt(int irq, void *data, struct pt_regs *unused)
24 struct line *dev = data;
27 chan_interrupt(&dev->chan_list, &dev->task, dev->tty, irq,
31 void line_timer_cb(void *arg)
33 struct line *dev = arg;
35 line_interrupt(dev->driver->read_irq, dev, NULL);
38 static void buffer_data(struct line *line, const char *buf, int len)
42 if(line->buffer == NULL){
43 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
44 if(line->buffer == NULL){
45 printk("buffer_data - atomic allocation failed\n");
48 line->head = line->buffer;
49 line->tail = line->buffer;
51 end = line->buffer + LINE_BUFSIZE - line->tail;
53 memcpy(line->tail, buf, len);
57 memcpy(line->tail, buf, end);
60 memcpy(line->buffer, buf, len);
61 line->tail = line->buffer + len;
65 static int flush_buffer(struct line *line)
69 if((line->buffer == NULL) || (line->head == line->tail)) return(1);
71 if(line->tail < line->head){
72 count = line->buffer + LINE_BUFSIZE - line->head;
73 n = write_chan(&line->chan_list, line->head, count,
74 line->driver->write_irq);
76 if(n == count) line->head = line->buffer;
83 count = line->tail - line->head;
84 n = write_chan(&line->chan_list, line->head, count,
85 line->driver->write_irq);
89 return(line->head == line->tail);
92 int line_write(struct line *lines, struct tty_struct *tty, int from_user,
93 const char *buf, int len)
100 if(tty->stopped) return 0;
103 new = kmalloc(len, GFP_KERNEL);
106 n = copy_from_user(new, buf, len);
116 if(line->head != line->tail){
117 local_irq_save(flags);
118 buffer_data(line, buf, len);
119 err = flush_buffer(line);
120 local_irq_restore(flags);
125 n = write_chan(&line->chan_list, buf, len,
126 line->driver->write_irq);
132 buffer_data(line, buf + n, len - n);
139 void line_write_interrupt(int irq, void *data, struct pt_regs *unused)
141 struct line *dev = data;
142 struct tty_struct *tty = dev->tty;
145 err = flush_buffer(dev);
148 dev->head = dev->buffer;
149 dev->tail = dev->buffer;
152 if(tty == NULL) return;
154 if(test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
155 (tty->ldisc.write_wakeup != NULL))
156 (tty->ldisc.write_wakeup)(tty);
159 * In blocking mode, everything sleeps on tty->write_wait.
160 * Sleeping in the console driver would break non-blocking
164 if (waitqueue_active(&tty->write_wait))
165 wake_up_interruptible(&tty->write_wait);
169 int line_write_room(struct tty_struct *tty)
171 struct line *dev = tty->driver_data;
174 if(dev->buffer == NULL) return(LINE_BUFSIZE - 1);
176 n = dev->head - dev->tail;
177 if(n <= 0) n = LINE_BUFSIZE + n;
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(line->driver->read_irq, line);
207 if(line->driver->write_irq == current_irq)
208 free_irq_later(line->driver->write_irq, line);
210 free_irq(line->driver->write_irq, line);
215 int line_open(struct line *lines, struct tty_struct *tty,
216 struct chan_opts *opts)
221 if(tty == NULL) n = 0;
226 if(line->count == 0){
231 if(list_empty(&line->chan_list)){
232 err = parse_chan_pair(line->init_str, &line->chan_list,
233 line->init_pri, n, opts);
235 err = open_chan(&line->chan_list);
238 enable_chan(&line->chan_list, line);
239 INIT_WORK(&line->task, line_timer_cb, line);
243 chan_enable_winch(&line->chan_list, line);
247 /* This is outside the if because the initial console is opened
253 tty->driver_data = line;
254 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
255 &tty->winsize.ws_col);
264 void line_close(struct line *lines, struct tty_struct *tty)
269 if(tty == NULL) n = 0;
276 /* I don't like this, but I can't think of anything better. What's
277 * going on is that the tty is in the process of being closed for
278 * the last time. Its count hasn't been dropped yet, so it's still
279 * at 1. This may happen when line->count != 0 because of the initial
280 * console open (without a tty) bumping it up to 1.
282 if((line->tty != NULL) && (line->tty->count == 1))
285 line_disable(line, -1);
289 void close_lines(struct line *lines, int nlines)
293 for(i = 0; i < nlines; i++)
294 close_chan(&lines[i].chan_list);
297 int line_setup(struct line *lines, int num, char *init, int all_allowed)
302 if(*init == '=') n = -1;
304 n = simple_strtoul(init, &end, 0);
306 printk(KERN_ERR "line_setup failed to parse \"%s\"\n",
313 if((n >= 0) && (n >= num)){
314 printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
319 if(lines[n].count > 0){
320 printk("line_setup - device %d is open\n", n);
323 if(lines[n].init_pri <= INIT_ONE){
324 lines[n].init_pri = INIT_ONE;
325 if(!strcmp(init, "none")) lines[n].valid = 0;
327 lines[n].init_str = init;
332 else if(!all_allowed){
333 printk("line_setup - can't configure all devices from "
338 for(i = 0; i < num; i++){
339 if(lines[i].init_pri <= INIT_ALL){
340 lines[i].init_pri = INIT_ALL;
341 if(!strcmp(init, "none")) lines[i].valid = 0;
343 lines[i].init_str = init;
352 int line_config(struct line *lines, int num, char *str)
354 char *new = uml_strdup(str);
357 printk("line_config - uml_strdup failed\n");
360 return(line_setup(lines, num, new, 0));
363 int line_get_config(char *name, struct line *lines, int num, char *str,
364 int size, char **error_out)
370 dev = simple_strtoul(name, &end, 0);
371 if((*end != '\0') || (end == name)){
372 *error_out = "line_setup failed to parse device number";
376 if((dev < 0) || (dev >= num)){
377 *error_out = "device number of of range";
385 CONFIG_CHUNK(str, size, n, "none", 1);
386 else if(line->count == 0)
387 CONFIG_CHUNK(str, size, n, line->init_str, 1);
388 else n = chan_config_string(&line->chan_list, str, size, error_out);
394 int line_remove(struct line *lines, int num, char *str)
396 char config[sizeof("conxxxx=none\0")];
398 sprintf(config, "%s=none", str);
399 return(line_setup(lines, num, config, 0));
402 struct tty_driver *line_register_devfs(struct lines *set,
403 struct line_driver *line_driver,
404 struct tty_operations *ops, struct line *lines,
409 struct tty_driver *driver = alloc_tty_driver(nlines);
414 driver->driver_name = line_driver->name;
415 driver->name = line_driver->devfs_name;
416 driver->major = line_driver->major;
417 driver->minor_start = line_driver->minor_start;
418 driver->type = line_driver->type;
419 driver->subtype = line_driver->subtype;
420 driver->flags = TTY_DRIVER_REAL_RAW;
421 driver->init_termios = tty_std_termios;
422 tty_set_operations(driver, ops);
424 if (tty_register_driver(driver))
425 panic("line_register_devfs : Couldn't register driver\n");
427 from = line_driver->symlink_from;
428 to = line_driver->symlink_to;
429 err = devfs_mk_symlink(from, to);
430 if(err) printk("Symlink creation from /dev/%s to /dev/%s "
431 "returned %d\n", from, to, err);
433 for(i = 0; i < nlines; i++){
435 tty_unregister_devfs(driver, i);
438 mconsole_register_dev(&line_driver->mc);
442 void lines_init(struct line *lines, int nlines)
447 for(i = 0; i < nlines; i++){
449 INIT_LIST_HEAD(&line->chan_list);
450 sema_init(&line->sem, 1);
451 if(line->init_str != NULL){
452 line->init_str = uml_strdup(line->init_str);
453 if(line->init_str == NULL)
454 printk("lines_init - uml_strdup returned "
461 struct list_head list;
468 void winch_interrupt(int irq, void *data, struct pt_regs *unused)
470 struct winch *winch = data;
471 struct tty_struct *tty;
475 err = generic_read(winch->fd, &c, NULL);
478 printk("winch_interrupt : read failed, errno = %d\n",
480 printk("fd %d is losing SIGWINCH support\n",
487 tty = winch->line->tty;
489 chan_window_size(&winch->line->chan_list,
490 &tty->winsize.ws_row,
491 &tty->winsize.ws_col);
492 kill_pg(tty->pgrp, SIGWINCH, 1);
495 reactivate_fd(winch->fd, WINCH_IRQ);
498 DECLARE_MUTEX(winch_handler_sem);
499 LIST_HEAD(winch_handlers);
501 void register_winch_irq(int fd, int tty_fd, int pid, void *line)
505 down(&winch_handler_sem);
506 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
508 printk("register_winch_irq - kmalloc failed\n");
511 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
516 list_add(&winch->list, &winch_handlers);
517 if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
518 SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
520 printk("register_winch_irq - failed to register IRQ\n");
522 up(&winch_handler_sem);
525 static void winch_cleanup(void)
527 struct list_head *ele;
530 list_for_each(ele, &winch_handlers){
531 winch = list_entry(ele, struct winch, list);
534 os_kill_process(winch->pid, 1);
538 __uml_exitcall(winch_cleanup);
540 char *add_xterm_umid(char *base)
546 if(umid == NULL) return(base);
548 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
549 title = kmalloc(len, GFP_KERNEL);
551 printk("Failed to allocate buffer for xterm title\n");
555 snprintf(title, len, "%s (%s)", base, umid);
560 * Overrides for Emacs so that we follow Linus's tabbing style.
561 * Emacs will notice this stuff at the end of the file and automatically
562 * adjust the settings for this buffer only. This must remain at the end
564 * ---------------------------------------------------------------------------
566 * c-file-style: "linux"