]>
Commit | Line | Data |
---|---|---|
5ba3f43e A |
1 | /* |
2 | * Copyright (c) 2000-2007 Apple Inc. All rights reserved. | |
3 | */ | |
4 | /* | |
5 | * Copyright (c) 1992 NeXT Computer, Inc. All rights reserved. | |
0a7de745 | 6 | * |
5ba3f43e | 7 | * km.m - kernel keyboard/monitor module, procedural interface. |
0a7de745 | 8 | * |
5ba3f43e A |
9 | * HISTORY |
10 | */ | |
11 | #include <sys/param.h> | |
12 | #include <sys/tty.h> | |
13 | ||
14 | #include <machine/cons.h> | |
15 | #include <sys/conf.h> | |
16 | #include <sys/systm.h> | |
17 | #include <sys/uio.h> | |
0a7de745 | 18 | #include <sys/fcntl.h> /* for kmopen */ |
5ba3f43e | 19 | #include <sys/errno.h> |
0a7de745 | 20 | #include <sys/proc.h> /* for kmopen */ |
5ba3f43e A |
21 | #include <sys/msgbuf.h> |
22 | #include <sys/time.h> | |
23 | #include <dev/kmreg_com.h> | |
24 | #include <pexpert/pexpert.h> | |
25 | #include <console/serial_protos.h> | |
26 | ||
27 | extern int hz; | |
28 | ||
29 | extern void cnputcusr(char); | |
30 | extern void cnputsusr(char *, int); | |
31 | extern int cngetc(void); | |
32 | ||
33 | ||
0a7de745 A |
34 | void kminit(void); |
35 | void cons_cinput(char ch); | |
5ba3f43e A |
36 | |
37 | /* | |
38 | * 'Global' variables, shared only by this file and conf.c. | |
39 | */ | |
40 | struct tty *km_tty[1] = { 0 }; | |
41 | ||
42 | /* | |
43 | * this works early on, after initialize_screen() but before autoconf (and thus | |
44 | * before we have a kmDevice). | |
45 | */ | |
46 | int disableConsoleOutput; | |
47 | ||
48 | /* | |
49 | * 'Global' variables, shared only by this file and kmDevice.m. | |
50 | */ | |
51 | int initialized = 0; | |
52 | ||
53 | static int kmoutput(struct tty * tp); | |
54 | static void kmstart(struct tty * tp); | |
55 | ||
56 | extern void KeyboardOpen(void); | |
57 | ||
58 | void | |
59 | kminit(void) | |
60 | { | |
61 | km_tty[0] = ttymalloc(); | |
0a7de745 | 62 | km_tty[0]->t_dev = makedev(12, 0); |
5ba3f43e A |
63 | initialized = 1; |
64 | } | |
65 | ||
66 | /* | |
67 | * cdevsw interface to km driver. | |
68 | */ | |
69 | int | |
70 | kmopen(dev_t dev, int flag, __unused int devtype, proc_t pp) | |
71 | { | |
72 | int unit; | |
73 | struct tty *tp; | |
74 | struct winsize *wp; | |
75 | int ret; | |
76 | ||
77 | unit = minor(dev); | |
0a7de745 A |
78 | if (unit >= 1) { |
79 | return ENXIO; | |
80 | } | |
5ba3f43e A |
81 | |
82 | tp = km_tty[unit]; | |
83 | ||
84 | tty_lock(tp); | |
85 | ||
86 | tp->t_oproc = kmstart; | |
87 | tp->t_param = NULL; | |
88 | tp->t_dev = dev; | |
89 | ||
90 | if (!(tp->t_state & TS_ISOPEN)) { | |
91 | tp->t_iflag = TTYDEF_IFLAG; | |
92 | tp->t_oflag = TTYDEF_OFLAG; | |
93 | tp->t_cflag = (CREAD | CS8 | CLOCAL); | |
94 | tp->t_lflag = TTYDEF_LFLAG; | |
95 | tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; | |
96 | termioschars(&tp->t_termios); | |
97 | ttsetwater(tp); | |
98 | } else if ((tp->t_state & TS_XCLUDE) && proc_suser(pp)) { | |
99 | ret = EBUSY; | |
100 | goto out; | |
101 | } | |
102 | ||
0a7de745 A |
103 | tp->t_state |= TS_CARR_ON; /* lie and say carrier exists and is |
104 | * on. */ | |
105 | ret = ((*linesw[tp->t_line].l_open)(dev, tp)); | |
5ba3f43e A |
106 | { |
107 | PE_Video video; | |
108 | wp = &tp->t_winsize; | |
109 | /* | |
110 | * Magic numbers. These are CHARWIDTH and CHARHEIGHT from | |
111 | * pexpert/i386/video_console.c | |
112 | */ | |
113 | wp->ws_xpixel = 8; | |
114 | wp->ws_ypixel = 16; | |
115 | ||
0a7de745 | 116 | tty_unlock(tp); /* XXX race window */ |
5ba3f43e | 117 | |
0a7de745 | 118 | if (flag & O_POPUP) { |
5ba3f43e | 119 | PE_initialize_console(0, kPETextScreen); |
0a7de745 | 120 | } |
5ba3f43e A |
121 | |
122 | bzero(&video, sizeof(video)); | |
123 | PE_current_console(&video); | |
124 | ||
125 | tty_lock(tp); | |
126 | ||
127 | if (serialmode & SERIALMODE_OUTPUT) { | |
128 | wp->ws_col = 80; | |
129 | wp->ws_row = 24; | |
130 | } else if (video.v_width != 0 && video.v_height != 0) { | |
131 | wp->ws_col = video.v_width / wp->ws_xpixel; | |
132 | wp->ws_row = video.v_height / wp->ws_ypixel; | |
133 | } else { | |
134 | wp->ws_col = 100; | |
135 | wp->ws_row = 36; | |
136 | } | |
137 | } | |
138 | ||
139 | out: | |
140 | tty_unlock(tp); | |
141 | ||
142 | return ret; | |
143 | } | |
144 | ||
145 | int | |
146 | kmclose(dev_t dev, int flag, __unused int mode, __unused proc_t p) | |
147 | { | |
148 | int ret; | |
149 | struct tty *tp = km_tty[minor(dev)]; | |
150 | ||
151 | tty_lock(tp); | |
152 | ret = (*linesw[tp->t_line].l_close)(tp, flag); | |
153 | ttyclose(tp); | |
154 | tty_unlock(tp); | |
155 | ||
0a7de745 | 156 | return ret; |
5ba3f43e A |
157 | } |
158 | ||
159 | int | |
160 | kmread(dev_t dev, struct uio * uio, int ioflag) | |
161 | { | |
162 | int ret; | |
163 | struct tty *tp = km_tty[minor(dev)]; | |
164 | ||
165 | tty_lock(tp); | |
166 | ret = (*linesw[tp->t_line].l_read)(tp, uio, ioflag); | |
167 | tty_unlock(tp); | |
168 | ||
0a7de745 | 169 | return ret; |
5ba3f43e A |
170 | } |
171 | ||
172 | int | |
173 | kmwrite(dev_t dev, struct uio * uio, int ioflag) | |
174 | { | |
175 | int ret; | |
176 | struct tty *tp = km_tty[minor(dev)]; | |
177 | ||
178 | tty_lock(tp); | |
179 | ret = (*linesw[tp->t_line].l_write)(tp, uio, ioflag); | |
180 | tty_unlock(tp); | |
181 | ||
0a7de745 | 182 | return ret; |
5ba3f43e A |
183 | } |
184 | ||
185 | int | |
186 | kmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, proc_t p) | |
187 | { | |
188 | int error = 0; | |
189 | struct tty *tp = km_tty[minor(dev)]; | |
190 | struct winsize *wp; | |
191 | ||
192 | tty_lock(tp); | |
193 | ||
194 | switch (cmd) { | |
195 | case KMIOCSIZE: | |
196 | wp = (struct winsize *) data; | |
197 | *wp = tp->t_winsize; | |
198 | break; | |
199 | ||
200 | case TIOCSWINSZ: | |
201 | /* | |
202 | * Prevent changing of console size -- this ensures that | |
203 | * login doesn't revert to the termcap-defined size | |
204 | */ | |
205 | error = EINVAL; | |
206 | break; | |
207 | ||
0a7de745 | 208 | /* Bodge in the CLOCAL flag as the km device is always local */ |
5ba3f43e A |
209 | case TIOCSETA_32: |
210 | case TIOCSETAW_32: | |
211 | case TIOCSETAF_32: | |
0a7de745 A |
212 | { |
213 | struct termios32 *t = (struct termios32 *)data; | |
214 | t->c_cflag |= CLOCAL; | |
215 | /* No Break */ | |
216 | } | |
5ba3f43e A |
217 | goto fallthrough; |
218 | case TIOCSETA_64: | |
219 | case TIOCSETAW_64: | |
220 | case TIOCSETAF_64: | |
0a7de745 A |
221 | { |
222 | struct user_termios *t = (struct user_termios *)data; | |
223 | t->c_cflag |= CLOCAL; | |
224 | /* No Break */ | |
225 | } | |
5ba3f43e A |
226 | fallthrough: |
227 | default: | |
0a7de745 A |
228 | error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); |
229 | if (ENOTTY != error) { | |
5ba3f43e | 230 | break; |
0a7de745 | 231 | } |
5ba3f43e A |
232 | error = ttioctl_locked(tp, cmd, data, flag, p); |
233 | break; | |
234 | } | |
235 | ||
236 | tty_unlock(tp); | |
237 | ||
0a7de745 | 238 | return error; |
5ba3f43e A |
239 | } |
240 | ||
241 | ||
242 | /* | |
243 | * kmputc | |
244 | * | |
245 | * Output a character to the serial console driver via cnputcusr(), | |
246 | * which is exported by that driver. | |
247 | * | |
248 | * Locks: Assumes tp in the calling tty driver code is locked on | |
249 | * entry, remains locked on exit | |
250 | * | |
251 | * Notes: Called from kmoutput(); giving the locking output | |
252 | * assumptions here, this routine should be static (and | |
253 | * inlined, given there is only one call site). | |
254 | */ | |
0a7de745 | 255 | int |
5ba3f43e A |
256 | kmputc(__unused dev_t dev, char c) |
257 | { | |
0a7de745 | 258 | if (!disableConsoleOutput && initialized) { |
5ba3f43e | 259 | /* OCRNL */ |
0a7de745 | 260 | if (c == '\n') { |
5ba3f43e | 261 | cnputcusr('\r'); |
0a7de745 | 262 | } |
5ba3f43e A |
263 | cnputcusr(c); |
264 | } | |
265 | ||
0a7de745 | 266 | return 0; |
5ba3f43e A |
267 | } |
268 | ||
269 | ||
270 | /* | |
271 | * Callouts from linesw. | |
272 | */ | |
273 | ||
0a7de745 | 274 | #define KM_LOWAT_DELAY ((ns_time_t)1000) |
5ba3f43e A |
275 | |
276 | /* | |
277 | * t_oproc for this driver; called from within the line discipline | |
278 | * | |
279 | * Locks: Assumes tp is locked on entry, remains locked on exit | |
280 | */ | |
281 | static void | |
282 | kmstart(struct tty *tp) | |
283 | { | |
0a7de745 | 284 | if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) { |
5ba3f43e | 285 | goto out; |
0a7de745 A |
286 | } |
287 | if (tp->t_outq.c_cc == 0) { | |
5ba3f43e | 288 | goto out; |
0a7de745 | 289 | } |
5ba3f43e A |
290 | tp->t_state |= TS_BUSY; |
291 | if (tp->t_outq.c_cc > tp->t_lowat) { | |
292 | /* | |
293 | * Start immediately. | |
294 | */ | |
295 | kmoutput(tp); | |
296 | } else { | |
297 | /* | |
298 | * Wait a bit... | |
299 | */ | |
300 | #if 0 | |
301 | /* FIXME */ | |
302 | timeout(kmtimeout, tp, hz); | |
303 | #else | |
304 | kmoutput(tp); | |
305 | #endif | |
306 | } | |
307 | return; | |
308 | ||
309 | out: | |
0a7de745 | 310 | (*linesw[tp->t_line].l_start)(tp); |
5ba3f43e A |
311 | return; |
312 | } | |
313 | ||
314 | /* | |
315 | * One-shot output retry timeout from kmoutput(); re-calls kmoutput() at | |
316 | * intervals until the output queue for the tty is empty, at which point | |
317 | * the timeout is not rescheduled by kmoutput() | |
318 | * | |
319 | * This function must take the tty_lock() around the kmoutput() call; it | |
320 | * ignores the return value. | |
321 | */ | |
322 | static void | |
323 | kmtimeout(void *arg) | |
324 | { | |
325 | struct tty *tp = (struct tty *)arg; | |
326 | ||
327 | tty_lock(tp); | |
328 | (void)kmoutput(tp); | |
329 | tty_unlock(tp); | |
330 | } | |
331 | ||
332 | /* | |
333 | * kmoutput | |
334 | * | |
335 | * Locks: Assumes tp is locked on entry, remains locked on exit | |
336 | * | |
337 | * Notes: Called from kmstart() and kmtimeout(); kmtimeout() is a | |
338 | * timer initiated by this routine to deal with pending | |
339 | * output not yet flushed (output is flushed at a maximum | |
340 | * of sizeof(buf) charatcers at a time before dropping into | |
341 | * the timeout code). | |
342 | */ | |
343 | static int | |
344 | kmoutput(struct tty * tp) | |
345 | { | |
0a7de745 A |
346 | unsigned char buf[80]; /* buffer; limits output per call */ |
347 | unsigned char *cp; | |
348 | int cc = -1; | |
5ba3f43e A |
349 | |
350 | /* While there is data available to be output... */ | |
351 | while (tp->t_outq.c_cc > 0) { | |
352 | cc = ndqb(&tp->t_outq, 0); | |
0a7de745 | 353 | if (cc == 0) { |
5ba3f43e | 354 | break; |
0a7de745 | 355 | } |
5ba3f43e A |
356 | /* |
357 | * attempt to output as many characters as are available, | |
358 | * up to the available transfer buffer size. | |
359 | */ | |
360 | cc = min(cc, sizeof(buf)); | |
361 | /* copy the output queue contents to the buffer */ | |
362 | (void) q_to_b(&tp->t_outq, buf, cc); | |
363 | for (cp = buf; cp < &buf[cc]; cp++) { | |
364 | /* output the buffer one charatcer at a time */ | |
365 | *cp = *cp & 0x7f; | |
366 | } | |
367 | if (cc > 1) { | |
368 | cnputsusr((char *)buf, cc); | |
369 | } else { | |
370 | kmputc(tp->t_dev, *buf); | |
371 | } | |
372 | } | |
373 | /* | |
374 | * XXX This is likely not necessary, as the tty output queue is not | |
375 | * XXX writeable while we hold the tty_lock(). | |
376 | */ | |
377 | if (tp->t_outq.c_cc > 0) { | |
378 | timeout(kmtimeout, tp, hz); | |
379 | } | |
380 | tp->t_state &= ~TS_BUSY; | |
381 | /* Start the output processing for the line discipline */ | |
0a7de745 | 382 | (*linesw[tp->t_line].l_start)(tp); |
5ba3f43e A |
383 | |
384 | return 0; | |
385 | } | |
386 | ||
387 | ||
388 | /* | |
389 | * cons_cinput | |
390 | * | |
391 | * Driver character input from the polled mode serial console driver calls | |
392 | * this routine to input a character from the serial driver into the tty | |
393 | * line discipline specific input processing receiv interrupt routine, | |
394 | * l_rint(). | |
395 | * | |
396 | * Locks: Assumes that the tty_lock() is NOT held on the tp, so a | |
397 | * serial driver should NOT call this function as a result | |
398 | * of being called from a function which already holds the | |
399 | * lock; ECHOE will be handled at the line discipline, if | |
400 | * output echo processing is going to occur. | |
401 | */ | |
402 | void | |
403 | cons_cinput(char ch) | |
404 | { | |
0a7de745 | 405 | struct tty *tp = km_tty[0]; /* XXX */ |
5ba3f43e A |
406 | |
407 | tty_lock(tp); | |
0a7de745 | 408 | (*linesw[tp->t_line].l_rint)(ch, tp); |
5ba3f43e A |
409 | tty_unlock(tp); |
410 | } |