blob: d95d64309eaf3defc373ba6195a0b5fc03cbea45 [file] [log] [blame]
Jeff Dike108ffa82006-07-10 04:45:14 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2001 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <stdio.h>
7#include <termios.h>
8#include <errno.h>
9#include <unistd.h>
10#include "chan_user.h"
11#include "user_util.h"
12#include "user.h"
13#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070014#include "um_malloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16struct tty_chan {
17 char *dev;
18 int raw;
19 struct termios tt;
20};
21
Jeff Dike5e7672e2006-09-27 01:50:33 -070022static void *tty_chan_init(char *str, int device, const struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
24 struct tty_chan *data;
25
26 if(*str != ':'){
27 printk("tty_init : channel type 'tty' must specify "
28 "a device\n");
Jeff Dike108ffa82006-07-10 04:45:14 -070029 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 }
31 str++;
32
33 data = um_kmalloc(sizeof(*data));
34 if(data == NULL)
Jeff Dike108ffa82006-07-10 04:45:14 -070035 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 *data = ((struct tty_chan) { .dev = str,
37 .raw = opts->raw });
Jeff Dike108ffa82006-07-10 04:45:14 -070038
39 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
42static int tty_open(int input, int output, int primary, void *d,
43 char **dev_out)
44{
45 struct tty_chan *data = d;
46 int fd, err;
47
48 fd = os_open_file(data->dev, of_set_rw(OPENFLAGS(), input, output), 0);
Jeff Dike108ffa82006-07-10 04:45:14 -070049 if(fd < 0)
50 return fd;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if(data->raw){
53 CATCH_EINTR(err = tcgetattr(fd, &data->tt));
54 if(err)
Jeff Dike108ffa82006-07-10 04:45:14 -070055 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 err = raw(fd);
58 if(err)
Jeff Dike108ffa82006-07-10 04:45:14 -070059 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
61
62 *dev_out = data->dev;
Jeff Dike108ffa82006-07-10 04:45:14 -070063 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Jeff Dike5e7672e2006-09-27 01:50:33 -070066const struct chan_ops tty_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .type = "tty",
68 .init = tty_chan_init,
69 .open = tty_open,
70 .close = generic_close,
71 .read = generic_read,
72 .write = generic_write,
Paolo 'Blaisorblade' Giarrussofd9bc532005-11-13 16:07:10 -080073 .console_write = generic_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 .window_size = generic_window_size,
75 .free = generic_free,
76 .winch = 0,
77};