]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
5d5c5d0d A |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. |
3 | * | |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* Copyright (c) 1992 NeXT Computer, Inc. All rights reserved. | |
29 | * | |
30 | * km.m - kernel keyboard/monitor module, procedural interface. | |
31 | * | |
32 | * HISTORY | |
33 | */ | |
34 | ||
91447636 | 35 | #include <sys/kernel.h> |
1c79356b A |
36 | #include <sys/tty.h> |
37 | ||
38 | #include <dev/ppc/cons.h> | |
39 | #include <sys/conf.h> | |
40 | #include <sys/systm.h> | |
41 | #include <sys/uio.h> | |
42 | #include <sys/fcntl.h> /* for kmopen */ | |
43 | #include <sys/errno.h> | |
44 | #include <sys/proc.h> /* for kmopen */ | |
45 | #include <sys/msgbuf.h> | |
46 | #include <sys/time.h> | |
47 | #include <dev/kmreg_com.h> | |
48 | #include <pexpert/pexpert.h> | |
49 | ||
50 | /* | |
51 | * 'Global' variables, shared only by this file and conf.c. | |
52 | */ | |
1c79356b A |
53 | struct tty *km_tty[1] = { &cons }; |
54 | ||
55 | /* | |
56 | * this works early on, after initialize_screen() but before autoconf (and thus | |
57 | * before we have a kmDevice). | |
58 | */ | |
59 | int disableConsoleOutput; | |
60 | ||
91447636 A |
61 | static int initialized = 0; |
62 | ||
91447636 A |
63 | extern void kminit(void); |
64 | ||
65 | // used by or implemented in the osfmk project | |
66 | extern void cnputcusr(char); // From osfmk | |
67 | extern int cngetc(void); // From osfmk | |
68 | extern void cons_cinput(char ch); // Used by osfmk | |
1c79356b A |
69 | |
70 | static int kmoutput(struct tty *tp); | |
71 | static void kmtimeout(struct tty *tp); | |
72 | static void kmstart(struct tty *tp); | |
73 | ||
74 | extern void KeyboardOpen(void); | |
75 | ||
91447636 A |
76 | void |
77 | kminit(void) | |
1c79356b A |
78 | { |
79 | cons.t_dev = makedev(12, 0); | |
80 | initialized = 1; | |
81 | } | |
82 | /* | |
83 | * cdevsw interface to km driver. | |
84 | */ | |
85 | int | |
91447636 | 86 | kmopen(dev_t dev, int flag, __unused int devtype, struct proc *pp) |
1c79356b | 87 | { |
1c79356b A |
88 | int unit; |
89 | struct tty *tp; | |
90 | struct winsize *wp; | |
91 | int ret; | |
92 | ||
93 | unit = minor(dev); | |
94 | if(unit >= 1) | |
95 | return (ENXIO); | |
96 | ||
97 | tp = (struct tty *)&cons; | |
98 | tp->t_oproc = kmstart; | |
99 | tp->t_param = NULL; | |
100 | tp->t_dev = dev; | |
101 | ||
102 | if ( !(tp->t_state & TS_ISOPEN) ) { | |
103 | tp->t_iflag = TTYDEF_IFLAG; | |
104 | tp->t_oflag = TTYDEF_OFLAG; | |
105 | tp->t_cflag = (CREAD | CS8 | CLOCAL); | |
106 | tp->t_lflag = TTYDEF_LFLAG; | |
107 | tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; | |
108 | termioschars(&tp->t_termios); | |
109 | ttsetwater(tp); | |
91447636 | 110 | } else if ((tp->t_state & TS_XCLUDE) && proc_suser(pp)) |
1c79356b A |
111 | return EBUSY; |
112 | ||
113 | tp->t_state |= TS_CARR_ON; /* lie and say carrier exists and is on. */ | |
114 | ret = ((*linesw[tp->t_line].l_open)(dev, tp)); | |
115 | { | |
116 | PE_Video video; | |
117 | wp = &tp->t_winsize; | |
118 | /* Magic numbers. These are CHARWIDTH and CHARHEIGHT | |
119 | * from osfmk/ppc/POWERMAC/video_console.c | |
120 | */ | |
121 | wp->ws_xpixel = 8; | |
122 | wp->ws_ypixel = 16; | |
123 | ||
124 | if (flag & O_POPUP) | |
125 | PE_initialize_console(0, kPETextScreen); | |
126 | ||
127 | bzero(&video, sizeof(video)); | |
128 | PE_current_console(&video); | |
129 | if( video.v_width != 0 && video.v_height != 0 ) { | |
130 | wp->ws_col = video.v_width / wp->ws_xpixel; | |
131 | wp->ws_row = video.v_height / wp->ws_ypixel; | |
132 | } else { | |
133 | wp->ws_col = 100; | |
134 | wp->ws_row = 36; | |
135 | } | |
136 | } | |
137 | return ret; | |
138 | } | |
139 | ||
140 | int | |
91447636 A |
141 | kmclose(__unused dev_t dev, __unused int flag, __unused int mode, |
142 | __unused struct proc *p) | |
1c79356b A |
143 | { |
144 | ||
145 | struct tty *tp; | |
146 | ||
147 | tp = &cons; | |
148 | (*linesw[tp->t_line].l_close)(tp,flag); | |
149 | ttyclose(tp); | |
150 | return (0); | |
151 | } | |
152 | ||
153 | int | |
91447636 | 154 | kmread(__unused dev_t dev, struct uio *uio, int ioflag) |
1c79356b A |
155 | { |
156 | register struct tty *tp; | |
157 | ||
158 | tp = &cons; | |
159 | return ((*linesw[tp->t_line].l_read)(tp, uio, ioflag)); | |
160 | } | |
161 | ||
162 | int | |
91447636 | 163 | kmwrite(__unused dev_t dev, struct uio *uio, int ioflag) |
1c79356b A |
164 | { |
165 | register struct tty *tp; | |
166 | ||
167 | tp = &cons; | |
168 | return ((*linesw[tp->t_line].l_write)(tp, uio, ioflag)); | |
169 | } | |
170 | ||
171 | int | |
91447636 | 172 | kmioctl( __unused dev_t dev, u_long cmd, caddr_t data, int flag, |
1c79356b A |
173 | struct proc *p) |
174 | { | |
175 | int error; | |
176 | struct tty *tp = &cons; | |
177 | struct winsize *wp; | |
178 | ||
179 | switch (cmd) { | |
180 | ||
181 | ||
182 | ||
183 | case KMIOCSIZE: | |
184 | wp = (struct winsize *)data; | |
185 | *wp = tp->t_winsize; | |
186 | return 0; | |
187 | ||
188 | case TIOCSWINSZ: | |
189 | /* Prevent changing of console size -- | |
190 | * this ensures that login doesn't revert to the | |
191 | * termcap-defined size | |
192 | */ | |
193 | return EINVAL; | |
194 | ||
195 | /* Bodge in the CLOCAL flag as the km device is always local */ | |
196 | case TIOCSETA: | |
197 | case TIOCSETAW: | |
198 | case TIOCSETAF: { | |
199 | register struct termios *t = (struct termios *)data; | |
200 | t->c_cflag |= CLOCAL; | |
201 | /* No Break */ | |
202 | } | |
203 | default: | |
204 | error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); | |
91447636 | 205 | if (ENOTTY != error) |
1c79356b | 206 | return error; |
91447636 | 207 | return ttioctl (tp, cmd, data, flag, p); |
1c79356b A |
208 | } |
209 | } | |
210 | ||
211 | int | |
91447636 | 212 | kmputc(__unused dev_t dev, char c) |
1c79356b A |
213 | { |
214 | ||
215 | if( disableConsoleOutput) | |
216 | return( 0); | |
217 | ||
218 | if(!initialized) | |
219 | return( 0); | |
220 | ||
221 | if(c == '\n') | |
222 | cnputcusr('\r'); | |
223 | ||
224 | cnputcusr(c); | |
225 | ||
226 | return 0; | |
227 | } | |
228 | ||
229 | int | |
91447636 | 230 | kmgetc(__unused dev_t dev) |
1c79356b A |
231 | { |
232 | int c; | |
233 | ||
234 | c= cngetc(); | |
235 | ||
236 | if (c == '\r') { | |
237 | c = '\n'; | |
238 | } | |
239 | cnputcusr(c); | |
240 | return c; | |
241 | } | |
242 | ||
91447636 | 243 | #if 0 |
1c79356b A |
244 | int |
245 | kmgetc_silent( | |
91447636 | 246 | __unused dev_t dev) |
1c79356b A |
247 | { |
248 | int c; | |
249 | ||
250 | c= cngetc(); | |
251 | if (c == '\r') { | |
252 | c = '\n'; | |
253 | } | |
254 | return c; | |
255 | } | |
91447636 | 256 | #endif /* 0 */ |
1c79356b A |
257 | |
258 | /* | |
259 | * Callouts from linesw. | |
260 | */ | |
261 | ||
262 | #define KM_LOWAT_DELAY ((ns_time_t)1000) | |
263 | ||
264 | static void | |
91447636 | 265 | kmstart(struct tty *tp) |
1c79356b | 266 | { |
1c79356b A |
267 | if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) |
268 | goto out; | |
269 | if (tp->t_outq.c_cc == 0) | |
270 | goto out; | |
271 | tp->t_state |= TS_BUSY; | |
91447636 A |
272 | kmoutput(tp); |
273 | return; | |
274 | ||
1c79356b | 275 | out: |
91447636 A |
276 | (*linesw[tp->t_line].l_start)(tp); |
277 | return; | |
1c79356b A |
278 | } |
279 | ||
280 | static void | |
91447636 | 281 | kmtimeout(struct tty *tp) |
1c79356b A |
282 | { |
283 | boolean_t funnel_state; | |
284 | ||
285 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
286 | kmoutput(tp); | |
9bccf70c | 287 | (void) thread_funnel_set(kernel_flock, funnel_state); |
1c79356b A |
288 | |
289 | ||
290 | } | |
291 | static int | |
91447636 | 292 | kmoutput(struct tty *tp) |
1c79356b A |
293 | { |
294 | /* | |
295 | * FIXME - to be grokked...copied from m68k km.c. | |
296 | */ | |
297 | char buf[80]; | |
298 | char *cp; | |
299 | int cc = -1; | |
1c79356b A |
300 | |
301 | while (tp->t_outq.c_cc > 0) { | |
302 | cc = ndqb(&tp->t_outq, 0); | |
303 | if (cc == 0) | |
304 | break; | |
305 | cc = min(cc, sizeof buf); | |
2d21ac55 | 306 | (void) q_to_b(&tp->t_outq, (unsigned char *)buf, cc); |
91447636 A |
307 | for (cp = buf; cp < &buf[cc]; cp++) |
308 | kmputc(tp->t_dev, *cp & 0x7f); | |
1c79356b A |
309 | } |
310 | if (tp->t_outq.c_cc > 0) { | |
55e303ae | 311 | timeout((timeout_fcn_t)kmtimeout, tp, hz); |
1c79356b A |
312 | } |
313 | tp->t_state &= ~TS_BUSY; | |
91447636 | 314 | (*linesw[tp->t_line].l_start)(tp); |
1c79356b A |
315 | |
316 | return 0; | |
317 | } | |
91447636 A |
318 | |
319 | void cons_cinput(char ch) | |
1c79356b A |
320 | { |
321 | struct tty *tp = &cons; | |
1c79356b A |
322 | |
323 | (*linesw[tp->t_line].l_rint) (ch, tp); | |
1c79356b A |
324 | } |
325 |