ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / um / drivers / fd.c
1 /* 
2  * Copyright (C) 2001 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <termios.h>
10 #include "user.h"
11 #include "user_util.h"
12 #include "chan_user.h"
13
14 struct fd_chan {
15         int fd;
16         int raw;
17         struct termios tt;
18         char str[sizeof("1234567890\0")];
19 };
20
21 void *fd_init(char *str, int device, struct chan_opts *opts)
22 {
23         struct fd_chan *data;
24         char *end;
25         int n;
26
27         if(*str != ':'){
28                 printk("fd_init : channel type 'fd' must specify a file "
29                        "descriptor\n");
30                 return(NULL);
31         }
32         str++;
33         n = strtoul(str, &end, 0);
34         if((*end != '\0') || (end == str)){
35                 printk("fd_init : couldn't parse file descriptor '%s'\n", str);
36                 return(NULL);
37         }
38         if((data = um_kmalloc(sizeof(*data))) == NULL) return(NULL);
39         *data = ((struct fd_chan) { .fd         = n,
40                                     .raw        = opts->raw });
41         return(data);
42 }
43
44 int fd_open(int input, int output, int primary, void *d, char **dev_out)
45 {
46         struct fd_chan *data = d;
47
48         if(data->raw && isatty(data->fd)){
49                 tcgetattr(data->fd, &data->tt);
50                 raw(data->fd, 0);
51         }
52         sprintf(data->str, "%d", data->fd);
53         *dev_out = data->str;
54         return(data->fd);
55 }
56
57 void fd_close(int fd, void *d)
58 {
59         struct fd_chan *data = d;
60
61         if(data->raw && isatty(fd)){
62                 tcsetattr(fd, TCSAFLUSH, &data->tt);
63                 data->raw = 0;
64         }
65 }
66
67 int fd_console_write(int fd, const char *buf, int n, void *d)
68 {
69         struct fd_chan *data = d;
70
71         return(generic_console_write(fd, buf, n, &data->tt));
72 }
73
74 struct chan_ops fd_ops = {
75         .type           = "fd",
76         .init           = fd_init,
77         .open           = fd_open,
78         .close          = fd_close,
79         .read           = generic_read,
80         .write          = generic_write,
81         .console_write  = fd_console_write,
82         .window_size    = generic_window_size,
83         .free           = generic_free,
84         .winch          = 1,
85 };
86
87 /*
88  * Overrides for Emacs so that we follow Linus's tabbing style.
89  * Emacs will notice this stuff at the end of the file and automatically
90  * adjust the settings for this buffer only.  This must remain at the end
91  * of the file.
92  * ---------------------------------------------------------------------------
93  * Local variables:
94  * c-file-style: "linux"
95  * End:
96  */