blob: 495858a090e4f49e50d4a79db016173ea4b3a6df [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"
Jeff Dikee99525f2007-10-16 01:26:41 -070010#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070012#include "um_malloc.h"
Jeff Dikee99525f2007-10-16 01:26:41 -070013#include "user.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
Jeff Dikee99525f2007-10-16 01:26:41 -070025 if (*str != ':') {
26 printk(UM_KERN_ERR "tty_init : channel type 'tty' must specify "
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 "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 Dike43f5b302008-05-12 14:01:52 -070032 data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
Jeff Dikee99525f2007-10-16 01:26:41 -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;
Jeff Dikee99525f2007-10-16 01:26:41 -070045 int fd, err, mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Jeff Dikee99525f2007-10-16 01:26:41 -070047 if (input && output)
48 mode = O_RDWR;
49 else if (input)
50 mode = O_RDONLY;
51 else if (output)
52 mode = O_WRONLY;
Jeff Dike108ffa82006-07-10 04:45:14 -070053
Jeff Dikee99525f2007-10-16 01:26:41 -070054 fd = open(data->dev, mode);
55 if (fd < 0)
56 return -errno;
57
58 if (data->raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 CATCH_EINTR(err = tcgetattr(fd, &data->tt));
Jeff Dikee99525f2007-10-16 01:26:41 -070060 if (err)
Jeff Dike108ffa82006-07-10 04:45:14 -070061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 err = raw(fd);
Jeff Dikee99525f2007-10-16 01:26:41 -070064 if (err)
Jeff Dike108ffa82006-07-10 04:45:14 -070065 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
67
68 *dev_out = data->dev;
Jeff Dike108ffa82006-07-10 04:45:14 -070069 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
Jeff Dike5e7672e2006-09-27 01:50:33 -070072const struct chan_ops tty_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 .type = "tty",
74 .init = tty_chan_init,
75 .open = tty_open,
76 .close = generic_close,
77 .read = generic_read,
78 .write = generic_write,
Paolo 'Blaisorblade' Giarrussofd9bc532005-11-13 16:07:10 -080079 .console_write = generic_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 .window_size = generic_window_size,
81 .free = generic_free,
82 .winch = 0,
83};