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 static int write_room(struct line *dev)
46 if (dev->buffer == NULL)
47 return (LINE_BUFSIZE - 1);
49 n = dev->head - dev->tail;
55 static int buffer_data(struct line *line, const char *buf, int len)
59 if(line->buffer == NULL){
60 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
61 if (line->buffer == NULL) {
62 printk("buffer_data - atomic allocation failed\n");
65 line->head = line->buffer;
66 line->tail = line->buffer;
69 room = write_room(line);
70 len = (len > room) ? room : len;
72 end = line->buffer + LINE_BUFSIZE - line->tail;
74 memcpy(line->tail, buf, len);
78 memcpy(line->tail, buf, end);
80 memcpy(line->buffer, buf, len - end);
81 line->tail = line->buffer + len - end;
87 static int flush_buffer(struct line *line)
91 if ((line->buffer == NULL) || (line->head == line->tail))
94 if (line->tail < line->head) {
95 count = line->buffer + LINE_BUFSIZE - line->head;
96 n = write_chan(&line->chan_list, line->head, count,
97 line->driver->write_irq);
101 line->head = line->buffer;
108 count = line->tail - line->head;
109 n = write_chan(&line->chan_list, line->head, count,
110 line->driver->write_irq);
114 return(line->head == line->tail);
117 int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
119 struct line *line = tty->driver_data;
123 if(tty->stopped) return 0;
126 if(line->head != line->tail){
127 local_irq_save(flags);
128 ret = buffer_data(line, buf, len);
129 err = flush_buffer(line);
130 local_irq_restore(flags);
131 if(err <= 0 && (err != -EAGAIN || !ret))
135 n = write_chan(&line->chan_list, buf, len,
136 line->driver->write_irq);
145 ret += buffer_data(line, buf + n, len);
152 void line_put_char(struct tty_struct *tty, unsigned char ch)
154 line_write(tty, &ch, sizeof(ch));
157 void line_set_termios(struct tty_struct *tty, struct termios * old)
162 int line_chars_in_buffer(struct tty_struct *tty)
172 /* don't print these, they flood the log ... */
173 { TCGETS, NULL, "TCGETS" },
174 { TCSETS, NULL, "TCSETS" },
175 { TCSETSW, NULL, "TCSETSW" },
176 { TCFLSH, NULL, "TCFLSH" },
177 { TCSBRK, NULL, "TCSBRK" },
179 /* general tty stuff */
180 { TCSETSF, KERN_DEBUG, "TCSETSF" },
181 { TCGETA, KERN_DEBUG, "TCGETA" },
182 { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
183 { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
184 { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
186 /* linux-specific ones */
187 { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
188 { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
189 { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
190 { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
193 int line_ioctl(struct tty_struct *tty, struct file * file,
194 unsigned int cmd, unsigned long arg)
238 for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
239 if (cmd == tty_ioctls[i].cmd)
241 if (i < ARRAY_SIZE(tty_ioctls)) {
242 if (NULL != tty_ioctls[i].level)
243 printk("%s%s: %s: ioctl %s called\n",
244 tty_ioctls[i].level, __FUNCTION__,
245 tty->name, tty_ioctls[i].name);
247 printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
248 __FUNCTION__, tty->name, cmd);
256 static irqreturn_t line_write_interrupt(int irq, void *data,
257 struct pt_regs *unused)
259 struct tty_struct *tty = data;
260 struct line *line = tty->driver_data;
263 err = flush_buffer(line);
267 line->head = line->buffer;
268 line->tail = line->buffer;
274 if(test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
275 (tty->ldisc.write_wakeup != NULL))
276 (tty->ldisc.write_wakeup)(tty);
279 * In blocking mode, everything sleeps on tty->write_wait.
280 * Sleeping in the console driver would break non-blocking
284 if(waitqueue_active(&tty->write_wait))
285 wake_up_interruptible(&tty->write_wait);
289 int line_setup_irq(int fd, int input, int output, struct tty_struct *tty)
291 struct line *line = tty->driver_data;
292 struct line_driver *driver = line->driver;
293 int err = 0, flags = SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM;
295 if(input) err = um_request_irq(driver->read_irq, fd, IRQ_READ,
296 line_interrupt, flags,
297 driver->read_irq_name, tty);
299 if(output) err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
300 line_write_interrupt, flags,
301 driver->write_irq_name, tty);
306 void line_disable(struct tty_struct *tty, int current_irq)
308 struct line *line = tty->driver_data;
313 if(line->driver->read_irq == current_irq)
314 free_irq_later(line->driver->read_irq, tty);
316 free_irq_by_irq_and_dev(line->driver->read_irq, tty);
317 free_irq(line->driver->read_irq, tty);
320 if(line->driver->write_irq == current_irq)
321 free_irq_later(line->driver->write_irq, tty);
323 free_irq_by_irq_and_dev(line->driver->write_irq, tty);
324 free_irq(line->driver->write_irq, tty);
330 int line_open(struct line *lines, struct tty_struct *tty,
331 struct chan_opts *opts)
336 line = &lines[tty->index];
337 tty->driver_data = line;
340 if (tty->count == 1) {
345 if (list_empty(&line->chan_list)) {
346 err = parse_chan_pair(line->init_str, &line->chan_list,
347 line->init_pri, tty->index, opts);
349 err = open_chan(&line->chan_list);
352 enable_chan(&line->chan_list, tty);
353 INIT_WORK(&line->task, line_timer_cb, tty);
357 chan_enable_winch(&line->chan_list, tty);
360 chan_window_size(&line->chan_list, &tty->winsize.ws_row,
361 &tty->winsize.ws_col);
369 void line_close(struct tty_struct *tty, struct file * filp)
371 struct line *line = tty->driver_data;
375 if (tty->count == 1) {
376 line_disable(tty, -1);
377 tty->driver_data = NULL;
382 void close_lines(struct line *lines, int nlines)
386 for(i = 0; i < nlines; i++)
387 close_chan(&lines[i].chan_list);
390 int line_setup(struct line *lines, int num, char *init, int all_allowed)
395 if(*init == '=') n = -1;
397 n = simple_strtoul(init, &end, 0);
399 printk(KERN_ERR "line_setup failed to parse \"%s\"\n",
406 if((n >= 0) && (n >= num)){
407 printk("line_setup - %d out of range ((0 ... %d) allowed)\n",
412 if (lines[n].count > 0) {
413 printk("line_setup - device %d is open\n", n);
416 if (lines[n].init_pri <= INIT_ONE){
417 lines[n].init_pri = INIT_ONE;
418 if (!strcmp(init, "none"))
421 lines[n].init_str = init;
426 else if(!all_allowed){
427 printk("line_setup - can't configure all devices from "
432 for(i = 0; i < num; i++){
433 if(lines[i].init_pri <= INIT_ALL){
434 lines[i].init_pri = INIT_ALL;
435 if(!strcmp(init, "none")) lines[i].valid = 0;
437 lines[i].init_str = init;
446 int line_config(struct line *lines, int num, char *str)
448 char *new = uml_strdup(str);
451 printk("line_config - uml_strdup failed\n");
454 return(!line_setup(lines, num, new, 0));
457 int line_get_config(char *name, struct line *lines, int num, char *str,
458 int size, char **error_out)
464 dev = simple_strtoul(name, &end, 0);
465 if((*end != '\0') || (end == name)){
466 *error_out = "line_get_config failed to parse device number";
470 if((dev < 0) || (dev >= num)){
471 *error_out = "device number of of range";
479 CONFIG_CHUNK(str, size, n, "none", 1);
480 else if(line->count == 0)
481 CONFIG_CHUNK(str, size, n, line->init_str, 1);
482 else n = chan_config_string(&line->chan_list, str, size, error_out);
488 int line_remove(struct line *lines, int num, char *str)
490 char config[sizeof("conxxxx=none\0")];
492 sprintf(config, "%s=none", str);
493 return(!line_setup(lines, num, config, 0));
496 int line_write_room(struct tty_struct *tty)
498 struct line *dev = tty->driver_data;
503 room = write_room(dev);
505 printk(KERN_DEBUG "%s: %s: no room left in buffer\n",
506 __FUNCTION__,tty->name);
510 struct tty_driver *line_register_devfs(struct lines *set,
511 struct line_driver *line_driver,
512 struct tty_operations *ops, struct line *lines,
516 struct tty_driver *driver = alloc_tty_driver(nlines);
521 driver->driver_name = line_driver->name;
522 driver->name = line_driver->device_name;
523 driver->devfs_name = line_driver->devfs_name;
524 driver->major = line_driver->major;
525 driver->minor_start = line_driver->minor_start;
526 driver->type = line_driver->type;
527 driver->subtype = line_driver->subtype;
528 driver->flags = TTY_DRIVER_REAL_RAW;
529 driver->init_termios = tty_std_termios;
530 tty_set_operations(driver, ops);
532 if (tty_register_driver(driver)) {
533 printk("%s: can't register %s driver\n",
534 __FUNCTION__,line_driver->name);
535 put_tty_driver(driver);
539 for(i = 0; i < nlines; i++){
541 tty_unregister_device(driver, i);
544 mconsole_register_dev(&line_driver->mc);
548 void lines_init(struct line *lines, int nlines)
553 for(i = 0; i < nlines; i++){
555 INIT_LIST_HEAD(&line->chan_list);
556 sema_init(&line->sem, 1);
557 if(line->init_str != NULL){
558 line->init_str = uml_strdup(line->init_str);
559 if(line->init_str == NULL)
560 printk("lines_init - uml_strdup returned "
567 struct list_head list;
571 struct tty_struct *tty;
574 irqreturn_t winch_interrupt(int irq, void *data, struct pt_regs *unused)
576 struct winch *winch = data;
577 struct tty_struct *tty;
583 err = generic_read(winch->fd, &c, NULL);
586 printk("winch_interrupt : read failed, "
587 "errno = %d\n", -err);
588 printk("fd %d is losing SIGWINCH support\n",
597 line = tty->driver_data;
598 chan_window_size(&line->chan_list,
599 &tty->winsize.ws_row,
600 &tty->winsize.ws_col);
601 kill_pg(tty->pgrp, SIGWINCH, 1);
605 reactivate_fd(winch->fd, WINCH_IRQ);
609 DECLARE_MUTEX(winch_handler_sem);
610 LIST_HEAD(winch_handlers);
612 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty)
616 down(&winch_handler_sem);
617 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
619 printk("register_winch_irq - kmalloc failed\n");
622 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
627 list_add(&winch->list, &winch_handlers);
628 if(um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
629 SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
631 printk("register_winch_irq - failed to register IRQ\n");
633 up(&winch_handler_sem);
636 static void winch_cleanup(void)
638 struct list_head *ele;
641 list_for_each(ele, &winch_handlers){
642 winch = list_entry(ele, struct winch, list);
644 deactivate_fd(winch->fd, WINCH_IRQ);
645 os_close_file(winch->fd);
648 os_kill_process(winch->pid, 1);
651 __uml_exitcall(winch_cleanup);
653 char *add_xterm_umid(char *base)
659 if(umid == NULL) return(base);
661 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
662 title = kmalloc(len, GFP_KERNEL);
664 printk("Failed to allocate buffer for xterm title\n");
668 snprintf(title, len, "%s (%s)", base, umid);
673 * Overrides for Emacs so that we follow Linus's tabbing style.
674 * Emacs will notice this stuff at the end of the file and automatically
675 * adjust the settings for this buffer only. This must remain at the end
677 * ---------------------------------------------------------------------------
679 * c-file-style: "linux"