source: trunk/minix/servers/fs/dmap.c@ 20

Last change on this file since 20 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 8.8 KB
Line 
1/* This file contains the table with device <-> driver mappings. It also
2 * contains some routines to dynamically add and/ or remove device drivers
3 * or change mappings.
4 */
5
6#include "fs.h"
7#include "fproc.h"
8#include <string.h>
9#include <stdlib.h>
10#include <ctype.h>
11#include <unistd.h>
12#include <minix/com.h>
13#include "param.h"
14
15/* Some devices may or may not be there in the next table. */
16#define DT(enable, opcl, io, driver, flags) \
17 { (enable?(opcl):no_dev), (enable?(io):0), \
18 (enable?(driver):0), (flags) },
19#define NC(x) (NR_CTRLRS >= (x))
20
21/* The order of the entries here determines the mapping between major device
22 * numbers and tasks. The first entry (major device 0) is not used. The
23 * next entry is major device 1, etc. Character and block devices can be
24 * intermixed at random. The ordering determines the device numbers in /dev/.
25 * Note that FS knows the device number of /dev/ram/ to load the RAM disk.
26 * Also note that the major device numbers used in /dev/ are NOT the same as
27 * the process numbers of the device drivers.
28 */
29/*
30 Driver enabled Open/Cls I/O Driver # Flags Device File
31 -------------- -------- ------ ----------- ----- ------ ----
32 */
33struct dmap dmap[NR_DEVICES]; /* actual map */
34PRIVATE struct dmap init_dmap[] = {
35 DT(1, no_dev, 0, 0, 0) /* 0 = not used */
36 DT(1, gen_opcl, gen_io, MEM_PROC_NR, 0) /* 1 = /dev/mem */
37 DT(0, no_dev, 0, 0, DMAP_MUTABLE) /* 2 = /dev/fd0 */
38 DT(0, no_dev, 0, 0, DMAP_MUTABLE) /* 3 = /dev/c0 */
39 DT(1, tty_opcl, gen_io, TTY_PROC_NR, 0) /* 4 = /dev/tty00 */
40 DT(1, ctty_opcl,ctty_io, TTY_PROC_NR, 0) /* 5 = /dev/tty */
41 DT(0, no_dev, 0, NONE, DMAP_MUTABLE) /* 6 = /dev/lp */
42
43#if (MACHINE == IBM_PC)
44 DT(1, no_dev, 0, 0, DMAP_MUTABLE) /* 7 = /dev/ip */
45 DT(0, no_dev, 0, NONE, DMAP_MUTABLE) /* 8 = /dev/c1 */
46 DT(0, 0, 0, 0, DMAP_MUTABLE) /* 9 = not used */
47 DT(0, no_dev, 0, 0, DMAP_MUTABLE) /*10 = /dev/c2 */
48 DT(0, 0, 0, 0, DMAP_MUTABLE) /*11 = not used */
49 DT(0, no_dev, 0, NONE, DMAP_MUTABLE) /*12 = /dev/c3 */
50 DT(0, no_dev, 0, NONE, DMAP_MUTABLE) /*13 = /dev/audio */
51 DT(0, no_dev, 0, NONE, DMAP_MUTABLE) /*14 = /dev/mixer */
52 DT(1, gen_opcl, gen_io, LOG_PROC_NR, 0) /*15 = /dev/klog */
53 DT(0, no_dev, 0, NONE, DMAP_MUTABLE) /*16 = /dev/random*/
54 DT(0, no_dev, 0, NONE, DMAP_MUTABLE) /*17 = /dev/cmos */
55#endif /* IBM_PC */
56};
57
58/*===========================================================================*
59 * do_devctl *
60 *===========================================================================*/
61PUBLIC int do_devctl()
62{
63 int result, proc_nr_e, proc_nr_n;
64
65 switch(m_in.ctl_req) {
66 case DEV_MAP:
67 /* Check process number of new driver. */
68 proc_nr_e= m_in.driver_nr;
69 if (isokendpt(proc_nr_e, &proc_nr_n) != OK)
70 return(EINVAL);
71
72 /* Try to update device mapping. */
73 result = map_driver(m_in.dev_nr, proc_nr_e, m_in.dev_style);
74 if (result == OK)
75 {
76 /* If a driver has completed its exec(), it can be announced to be
77 * up.
78 */
79 if(fproc[proc_nr_n].fp_execced) {
80 dev_up(m_in.dev_nr);
81 } else {
82 dmap[m_in.dev_nr].dmap_flags |= DMAP_BABY;
83 }
84 }
85 break;
86 case DEV_UNMAP:
87 result = map_driver(m_in.dev_nr, NONE, 0);
88 break;
89 default:
90 result = EINVAL;
91 }
92 return(result);
93}
94
95/*===========================================================================*
96 * map_driver *
97 *===========================================================================*/
98PUBLIC int map_driver(major, proc_nr_e, style)
99int major; /* major number of the device */
100int proc_nr_e; /* process number of the driver */
101int style; /* style of the device */
102{
103/* Set a new device driver mapping in the dmap table. Given that correct
104 * arguments are given, this only works if the entry is mutable and the
105 * current driver is not busy. If the proc_nr is set to NONE, we're supposed
106 * to unmap it.
107 *
108 * Normal error codes are returned so that this function can be used from
109 * a system call that tries to dynamically install a new driver.
110 */
111 struct dmap *dp;
112 int proc_nr_n;
113
114 /* Get pointer to device entry in the dmap table. */
115 if (major < 0 || major >= NR_DEVICES) return(ENODEV);
116 dp = &dmap[major];
117
118 /* Check if we're supposed to unmap it. If so, do it even
119 * if busy or unmutable, as unmap is called when driver has
120 * exited.
121 */
122 if(proc_nr_e == NONE) {
123 dp->dmap_opcl = no_dev;
124 dp->dmap_io = no_dev_io;
125 dp->dmap_driver = NONE;
126 dp->dmap_flags = DMAP_MUTABLE; /* When gone, not busy or reserved. */
127 return(OK);
128 }
129
130 /* See if updating the entry is allowed. */
131 if (! (dp->dmap_flags & DMAP_MUTABLE)) return(EPERM);
132 if (dp->dmap_flags & DMAP_BUSY) return(EBUSY);
133
134 /* Check process number of new driver. */
135 if (isokendpt(proc_nr_e, &proc_nr_n) != OK)
136 return(EINVAL);
137
138 /* Try to update the entry. */
139 switch (style) {
140 case STYLE_DEV: dp->dmap_opcl = gen_opcl; break;
141 case STYLE_TTY: dp->dmap_opcl = tty_opcl; break;
142 case STYLE_CLONE: dp->dmap_opcl = clone_opcl; break;
143 default: return(EINVAL);
144 }
145 dp->dmap_io = gen_io;
146 dp->dmap_driver = proc_nr_e;
147
148 return(OK);
149}
150
151/*===========================================================================*
152 * dmap_unmap_by_endpt *
153 *===========================================================================*/
154PUBLIC void dmap_unmap_by_endpt(int proc_nr_e)
155{
156 int i, r;
157 for (i=0; i<NR_DEVICES; i++)
158 if(dmap[i].dmap_driver && dmap[i].dmap_driver == proc_nr_e)
159 if((r=map_driver(i, NONE, 0)) != OK)
160 printf("FS: unmap of p %d / d %d failed: %d\n", proc_nr_e,i,r);
161
162 return;
163
164}
165
166/*===========================================================================*
167 * build_dmap *
168 *===========================================================================*/
169PUBLIC void build_dmap()
170{
171/* Initialize the table with all device <-> driver mappings. Then, map
172 * the boot driver to a controller and update the dmap table to that
173 * selection. The boot driver and the controller it handles are set at
174 * the boot monitor.
175 */
176 int i;
177 struct dmap *dp;
178
179 /* Build table with device <-> driver mappings. */
180 for (i=0; i<NR_DEVICES; i++) {
181 dp = &dmap[i];
182 if (i < sizeof(init_dmap)/sizeof(struct dmap) &&
183 init_dmap[i].dmap_opcl != no_dev) { /* a preset driver */
184 dp->dmap_opcl = init_dmap[i].dmap_opcl;
185 dp->dmap_io = init_dmap[i].dmap_io;
186 dp->dmap_driver = init_dmap[i].dmap_driver;
187 dp->dmap_flags = init_dmap[i].dmap_flags;
188 } else { /* no default */
189 dp->dmap_opcl = no_dev;
190 dp->dmap_io = no_dev_io;
191 dp->dmap_driver = NONE;
192 dp->dmap_flags = DMAP_MUTABLE;
193 }
194 }
195
196#if 0
197 /* Get settings of 'controller' and 'driver' at the boot monitor. */
198 if ((s = env_get_param("label", driver, sizeof(driver))) != OK)
199 panic(__FILE__,"couldn't get boot monitor parameter 'driver'", s);
200 if ((s = env_get_param("controller", controller, sizeof(controller))) != OK)
201 panic(__FILE__,"couldn't get boot monitor parameter 'controller'", s);
202
203 /* Determine major number to map driver onto. */
204 if (controller[0] == 'f' && controller[1] == 'd') {
205 major = FLOPPY_MAJOR;
206 }
207 else if (controller[0] == 'c' && isdigit(controller[1])) {
208 if ((nr = (unsigned) atoi(&controller[1])) > NR_CTRLRS)
209 panic(__FILE__,"monitor 'controller' maximum 'c#' is", NR_CTRLRS);
210 major = CTRLR(nr);
211 }
212 else {
213 panic(__FILE__,"monitor 'controller' syntax is 'c#' of 'fd'", NO_NUM);
214 }
215
216 /* Now try to set the actual mapping and report to the user. */
217 if ((s=map_driver(major, DRVR_PROC_NR, STYLE_DEV)) != OK)
218 panic(__FILE__,"map_driver failed",s);
219 printf("Boot medium driver: %s driver mapped onto controller %s.\n",
220 driver, controller);
221#endif
222}
223
224/*===========================================================================*
225 * dmap_driver_match *
226 *===========================================================================*/
227PUBLIC int dmap_driver_match(int proc, int major)
228{
229 if (major < 0 || major >= NR_DEVICES) return(0);
230 if(dmap[major].dmap_driver != NONE && dmap[major].dmap_driver == proc)
231 return 1;
232 return 0;
233}
234
235/*===========================================================================*
236 * dmap_endpt_up *
237 *===========================================================================*/
238PUBLIC void dmap_endpt_up(int proc_e)
239{
240 int i;
241 for (i=0; i<NR_DEVICES; i++) {
242 if(dmap[i].dmap_driver != NONE
243 && dmap[i].dmap_driver == proc_e
244 && (dmap[i].dmap_flags & DMAP_BABY)) {
245 dmap[i].dmap_flags &= ~DMAP_BABY;
246 dev_up(i);
247 }
248 }
249 return;
250}
Note: See TracBrowser for help on using the repository browser.