Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 2 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <errno.h> |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 7 | #include <fcntl.h> |
| 8 | #include <termios.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include "chan_user.h" |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 10 | #include <os.h> |
| 11 | #include <um_malloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | struct tty_chan { |
| 14 | char *dev; |
| 15 | int raw; |
| 16 | struct termios tt; |
| 17 | }; |
| 18 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 19 | static void *tty_chan_init(char *str, int device, const struct chan_opts *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | { |
| 21 | struct tty_chan *data; |
| 22 | |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 23 | if (*str != ':') { |
| 24 | printk(UM_KERN_ERR "tty_init : channel type 'tty' must specify " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | "a device\n"); |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 26 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | } |
| 28 | str++; |
| 29 | |
Jeff Dike | 43f5b30 | 2008-05-12 14:01:52 -0700 | [diff] [blame] | 30 | data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 31 | if (data == NULL) |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 32 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | *data = ((struct tty_chan) { .dev = str, |
| 34 | .raw = opts->raw }); |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 35 | |
| 36 | return data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static int tty_open(int input, int output, int primary, void *d, |
| 40 | char **dev_out) |
| 41 | { |
| 42 | struct tty_chan *data = d; |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 43 | int fd, err, mode = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 45 | if (input && output) |
| 46 | mode = O_RDWR; |
| 47 | else if (input) |
| 48 | mode = O_RDONLY; |
| 49 | else if (output) |
| 50 | mode = O_WRONLY; |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 51 | |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 52 | fd = open(data->dev, mode); |
| 53 | if (fd < 0) |
| 54 | return -errno; |
| 55 | |
| 56 | if (data->raw) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | CATCH_EINTR(err = tcgetattr(fd, &data->tt)); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 58 | if (err) |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 59 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | err = raw(fd); |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 62 | if (err) |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 63 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | *dev_out = data->dev; |
Jeff Dike | 108ffa8 | 2006-07-10 04:45:14 -0700 | [diff] [blame] | 67 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 70 | const struct chan_ops tty_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | .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' Giarrusso | fd9bc53 | 2005-11-13 16:07:10 -0800 | [diff] [blame] | 77 | .console_write = generic_console_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | .window_size = generic_window_size, |
| 79 | .free = generic_free, |
| 80 | .winch = 0, |
| 81 | }; |