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