blob: a9f87e19c5bf9f283faf134f52ad9f0de4b5fdd1 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "user.h"
12#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070013#include "um_malloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15struct tty_chan {
16 char *dev;
17 int raw;
18 struct termios tt;
19};
20
Jeff Dike5e7672e2006-09-27 01:50:33 -070021static void *tty_chan_init(char *str, int device, const struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
23 struct tty_chan *data;
24
25 if(*str != ':'){
26 printk("tty_init : channel type 'tty' must specify "
27 "a device\n");
Jeff Dike108ffa82006-07-10 04:45:14 -070028 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 }
30 str++;
31
Jeff Dikee4c4bf92007-07-15 23:38:56 -070032 data = kmalloc(sizeof(*data), UM_GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 if(data == NULL)
Jeff Dike108ffa82006-07-10 04:45:14 -070034 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 *data = ((struct tty_chan) { .dev = str,
36 .raw = opts->raw });
Jeff Dike108ffa82006-07-10 04:45:14 -070037
38 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
41static int tty_open(int input, int output, int primary, void *d,
42 char **dev_out)
43{
44 struct tty_chan *data = d;
45 int fd, err;
46
47 fd = os_open_file(data->dev, of_set_rw(OPENFLAGS(), input, output), 0);
Jeff Dike108ffa82006-07-10 04:45:14 -070048 if(fd < 0)
49 return fd;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if(data->raw){
52 CATCH_EINTR(err = tcgetattr(fd, &data->tt));
53 if(err)
Jeff Dike108ffa82006-07-10 04:45:14 -070054 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 err = raw(fd);
57 if(err)
Jeff Dike108ffa82006-07-10 04:45:14 -070058 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60
61 *dev_out = data->dev;
Jeff Dike108ffa82006-07-10 04:45:14 -070062 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Jeff Dike5e7672e2006-09-27 01:50:33 -070065const struct chan_ops tty_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 .type = "tty",
67 .init = tty_chan_init,
68 .open = tty_open,
69 .close = generic_close,
70 .read = generic_read,
71 .write = generic_write,
Paolo 'Blaisorblade' Giarrussofd9bc532005-11-13 16:07:10 -080072 .console_write = generic_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 .window_size = generic_window_size,
74 .free = generic_free,
75 .winch = 0,
76};