import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / fs / proc / proc_tty.c
1 /*
2  * proc_tty.c -- handles /proc/tty
3  *
4  * Copyright 1997, Theodore Ts'o
5  */
6
7 #include <asm/uaccess.h>
8
9 #include <linux/init.h>
10 #include <linux/errno.h>
11 #include <linux/sched.h>
12 #include <linux/proc_fs.h>
13 #include <linux/stat.h>
14 #include <linux/tty.h>
15 #include <asm/bitops.h>
16
17 extern struct tty_driver *tty_drivers;  /* linked list of tty drivers */
18 extern struct tty_ldisc ldiscs[];
19
20
21 static int tty_drivers_read_proc(char *page, char **start, off_t off,
22                                  int count, int *eof, void *data);
23 static int tty_ldiscs_read_proc(char *page, char **start, off_t off,
24                                 int count, int *eof, void *data);
25
26 /*
27  * The /proc/tty directory inodes...
28  */
29 static struct proc_dir_entry *proc_tty_ldisc, *proc_tty_driver;
30
31 /*
32  * This is the handler for /proc/tty/drivers
33  */
34 static int tty_drivers_read_proc(char *page, char **start, off_t off,
35                                  int count, int *eof, void *data)
36 {
37         int     len = 0;
38         off_t   begin = 0;
39         struct tty_driver *p;
40         char    range[20], deftype[20];
41         char    *type;
42
43         for (p = tty_drivers; p; p = p->next) {
44                 if (p->num > 1)
45                         sprintf(range, "%d-%d", p->minor_start,
46                                 p->minor_start + p->num - 1);
47                 else
48                         sprintf(range, "%d", p->minor_start);
49                 switch (p->type) {
50                 case TTY_DRIVER_TYPE_SYSTEM:
51                         if (p->subtype == SYSTEM_TYPE_TTY)
52                                 type = "system:/dev/tty";
53                         else if (p->subtype == SYSTEM_TYPE_SYSCONS)
54                                 type = "system:console";
55                         else if (p->subtype == SYSTEM_TYPE_CONSOLE)
56                                 type = "system:vtmaster";
57                         else
58                                 type = "system";
59                         break;
60                 case TTY_DRIVER_TYPE_CONSOLE:
61                         type = "console";
62                         break;
63                 case TTY_DRIVER_TYPE_SERIAL:
64                         if (p->subtype == 2)
65                                 type = "serial:callout";
66                         else
67                                 type = "serial";
68                         break;
69                 case TTY_DRIVER_TYPE_PTY:
70                         if (p->subtype == PTY_TYPE_MASTER)
71                                 type = "pty:master";
72                         else if (p->subtype == PTY_TYPE_SLAVE)
73                                 type = "pty:slave";
74                         else
75                                 type = "pty";
76                         break;
77                 default:
78                         sprintf(deftype, "type:%d.%d", p->type, p->subtype);
79                         type = deftype;
80                         break;
81                 }
82                 len += sprintf(page+len, "%-20s /dev/%-8s %3d %7s %s\n",
83                                p->driver_name ? p->driver_name : "unknown",
84                                p->name, p->major, range, type);
85                 if (len+begin > off+count)
86                         break;
87                 if (len+begin < off) {
88                         begin += len;
89                         len = 0;
90                 }
91         }
92         if (!p)
93                 *eof = 1;
94         if (off >= len+begin)
95                 return 0;
96         *start = page + (off-begin);
97         return ((count < begin+len-off) ? count : begin+len-off);
98 }
99
100 /*
101  * This is the handler for /proc/tty/ldiscs
102  */
103 static int tty_ldiscs_read_proc(char *page, char **start, off_t off,
104                                 int count, int *eof, void *data)
105 {
106         int     i;
107         int     len = 0;
108         off_t   begin = 0;
109
110         for (i=0; i < NR_LDISCS; i++) {
111                 if (!(ldiscs[i].flags & LDISC_FLAG_DEFINED))
112                         continue;
113                 len += sprintf(page+len, "%-10s %2d\n",
114                                ldiscs[i].name ? ldiscs[i].name : "???", i);
115                 if (len+begin > off+count)
116                         break;
117                 if (len+begin < off) {
118                         begin += len;
119                         len = 0;
120                 }
121         }
122         if (i >= NR_LDISCS)
123                 *eof = 1;
124         if (off >= len+begin)
125                 return 0;
126         *start = page + (off-begin);
127         return ((count < begin+len-off) ? count : begin+len-off);
128 }
129
130 /*
131  * Thsi function is called by register_tty_driver() to handle
132  * registering the driver's /proc handler into /proc/tty/driver/<foo>
133  */
134 void proc_tty_register_driver(struct tty_driver *driver)
135 {
136         struct proc_dir_entry *ent;
137                 
138         if ((!driver->read_proc && !driver->write_proc) ||
139             !driver->driver_name ||
140             driver->proc_entry)
141                 return;
142
143         ent = create_proc_entry(driver->driver_name, 0, proc_tty_driver);
144         if (!ent)
145                 return;
146         ent->read_proc = driver->read_proc;
147         ent->write_proc = driver->write_proc;
148         ent->data = driver;
149
150         driver->proc_entry = ent;
151 }
152
153 /*
154  * This function is called by unregister_tty_driver()
155  */
156 void proc_tty_unregister_driver(struct tty_driver *driver)
157 {
158         struct proc_dir_entry *ent;
159
160         ent = driver->proc_entry;
161         if (!ent)
162                 return;
163                 
164         remove_proc_entry(driver->driver_name, proc_tty_driver);
165         
166         driver->proc_entry = 0;
167 }
168
169 /*
170  * Called by proc_root_init() to initialize the /proc/tty subtree
171  */
172 void __init proc_tty_init(void)
173 {
174         if (!proc_mkdir("tty", 0))
175                 return;
176         proc_tty_ldisc = proc_mkdir("tty/ldisc", 0);
177         proc_tty_driver = proc_mkdir("tty/driver", 0);
178
179         create_proc_read_entry("tty/ldiscs", 0, 0, tty_ldiscs_read_proc,NULL);
180         create_proc_read_entry("tty/drivers", 0, 0, tty_drivers_read_proc,NULL);
181 }