Merge branch 'x86-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / um / drivers / fd.c
CommitLineData
cb8fa61c 1/*
e99525f9 2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
1da177e4
LT
3 * Licensed under the GPL
4 */
5
e99525f9 6#include <stdio.h>
cb8fa61c
JD
7#include <stdlib.h>
8#include <unistd.h>
1da177e4 9#include <errno.h>
e99525f9 10#include <termios.h>
1da177e4 11#include "chan_user.h"
cb8fa61c
JD
12#include "kern_constants.h"
13#include "os.h"
c13e5690 14#include "um_malloc.h"
e99525f9 15#include "user.h"
1da177e4
LT
16
17struct fd_chan {
18 int fd;
19 int raw;
20 struct termios tt;
21 char str[sizeof("1234567890\0")];
22};
23
5e7672ec 24static void *fd_init(char *str, int device, const struct chan_opts *opts)
1da177e4
LT
25{
26 struct fd_chan *data;
27 char *end;
28 int n;
29
e99525f9
JD
30 if (*str != ':') {
31 printk(UM_KERN_ERR "fd_init : channel type 'fd' must specify a "
32 "file descriptor\n");
33 return NULL;
1da177e4
LT
34 }
35 str++;
36 n = strtoul(str, &end, 0);
e99525f9
JD
37 if ((*end != '\0') || (end == str)) {
38 printk(UM_KERN_ERR "fd_init : couldn't parse file descriptor "
39 "'%s'\n", str);
40 return NULL;
1da177e4 41 }
e99525f9 42
43f5b308 43 data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
cb8fa61c 44 if (data == NULL)
e99525f9
JD
45 return NULL;
46
1da177e4
LT
47 *data = ((struct fd_chan) { .fd = n,
48 .raw = opts->raw });
e99525f9 49 return data;
1da177e4
LT
50}
51
52static int fd_open(int input, int output, int primary, void *d, char **dev_out)
53{
54 struct fd_chan *data = d;
55 int err;
56
e99525f9 57 if (data->raw && isatty(data->fd)) {
1da177e4 58 CATCH_EINTR(err = tcgetattr(data->fd, &data->tt));
e99525f9
JD
59 if (err)
60 return err;
1da177e4
LT
61
62 err = raw(data->fd);
e99525f9
JD
63 if (err)
64 return err;
1da177e4
LT
65 }
66 sprintf(data->str, "%d", data->fd);
67 *dev_out = data->str;
e99525f9 68 return data->fd;
1da177e4
LT
69}
70
71static void fd_close(int fd, void *d)
72{
73 struct fd_chan *data = d;
74 int err;
75
e99525f9
JD
76 if (!data->raw || !isatty(fd))
77 return;
78
79 CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &data->tt));
80 if (err)
81 printk(UM_KERN_ERR "Failed to restore terminal state - "
82 "errno = %d\n", -err);
83 data->raw = 0;
1da177e4
LT
84}
85
5e7672ec 86const struct chan_ops fd_ops = {
1da177e4
LT
87 .type = "fd",
88 .init = fd_init,
89 .open = fd_open,
90 .close = fd_close,
91 .read = generic_read,
92 .write = generic_write,
fd9bc53b 93 .console_write = generic_console_write,
1da177e4
LT
94 .window_size = generic_window_size,
95 .free = generic_free,
96 .winch = 1,
97};