blob: eaa201bca5ed85db9ad0486ae9495498bcfb676a [file] [log] [blame]
Jeff Dike108ffa82006-07-10 04:45:14 -07001/*
Jeff Dikee99525f2007-10-16 01:26:41 -07002 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <errno.h>
Jeff Dikee99525f2007-10-16 01:26:41 -07007#include <fcntl.h>
8#include <termios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include "chan_user.h"
Al Viro37185b32012-10-08 03:27:32 +010010#include <os.h>
11#include <um_malloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13struct tty_chan {
14 char *dev;
15 int raw;
16 struct termios tt;
17};
18
Jeff Dike5e7672e2006-09-27 01:50:33 -070019static void *tty_chan_init(char *str, int device, const struct chan_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
21 struct tty_chan *data;
22
Jeff Dikee99525f2007-10-16 01:26:41 -070023 if (*str != ':') {
24 printk(UM_KERN_ERR "tty_init : channel type 'tty' must specify "
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 "a device\n");
Jeff Dike108ffa82006-07-10 04:45:14 -070026 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 }
28 str++;
29
Jeff Dike43f5b302008-05-12 14:01:52 -070030 data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
Jeff Dikee99525f2007-10-16 01:26:41 -070031 if (data == NULL)
Jeff Dike108ffa82006-07-10 04:45:14 -070032 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *data = ((struct tty_chan) { .dev = str,
34 .raw = opts->raw });
Jeff Dike108ffa82006-07-10 04:45:14 -070035
36 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
39static int tty_open(int input, int output, int primary, void *d,
40 char **dev_out)
41{
42 struct tty_chan *data = d;
Jeff Dikee99525f2007-10-16 01:26:41 -070043 int fd, err, mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Jeff Dikee99525f2007-10-16 01:26:41 -070045 if (input && output)
46 mode = O_RDWR;
47 else if (input)
48 mode = O_RDONLY;
49 else if (output)
50 mode = O_WRONLY;
Jeff Dike108ffa82006-07-10 04:45:14 -070051
Jeff Dikee99525f2007-10-16 01:26:41 -070052 fd = open(data->dev, mode);
53 if (fd < 0)
54 return -errno;
55
56 if (data->raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 CATCH_EINTR(err = tcgetattr(fd, &data->tt));
Jeff Dikee99525f2007-10-16 01:26:41 -070058 if (err)
Jeff Dike108ffa82006-07-10 04:45:14 -070059 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 err = raw(fd);
Jeff Dikee99525f2007-10-16 01:26:41 -070062 if (err)
Jeff Dike108ffa82006-07-10 04:45:14 -070063 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
65
66 *dev_out = data->dev;
Jeff Dike108ffa82006-07-10 04:45:14 -070067 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Jeff Dike5e7672e2006-09-27 01:50:33 -070070const struct chan_ops tty_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 .type = "tty",
72 .init = tty_chan_init,
73 .open = tty_open,
74 .close = generic_close,
75 .read = generic_read,
76 .write = generic_write,
Paolo 'Blaisorblade' Giarrussofd9bc532005-11-13 16:07:10 -080077 .console_write = generic_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 .window_size = generic_window_size,
79 .free = generic_free,
80 .winch = 0,
81};