]>
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 | |
f427ee49 | 70 | kmopen(dev_t dev, __unused int flag, __unused int devtype, proc_t pp) |
5ba3f43e A |
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 | |
5ba3f43e A |
118 | bzero(&video, sizeof(video)); |
119 | PE_current_console(&video); | |
120 | ||
121 | tty_lock(tp); | |
122 | ||
123 | if (serialmode & SERIALMODE_OUTPUT) { | |
124 | wp->ws_col = 80; | |
125 | wp->ws_row = 24; | |
126 | } else if (video.v_width != 0 && video.v_height != 0) { | |
f427ee49 A |
127 | unsigned long ws_col = video.v_width / wp->ws_xpixel; |
128 | unsigned long ws_row = video.v_height / wp->ws_ypixel; | |
129 | ||
130 | assert((ws_col <= USHRT_MAX) && (ws_row <= USHRT_MAX)); | |
131 | ||
132 | wp->ws_col = (unsigned short)ws_col; | |
133 | wp->ws_row = (unsigned short)ws_row; | |
5ba3f43e A |
134 | } else { |
135 | wp->ws_col = 100; | |
136 | wp->ws_row = 36; | |
137 | } | |
138 | } | |
139 | ||
140 | out: | |
141 | tty_unlock(tp); | |
142 | ||
143 | return ret; | |
144 | } | |
145 | ||
146 | int | |
147 | kmclose(dev_t dev, int flag, __unused int mode, __unused proc_t p) | |
148 | { | |
149 | int ret; | |
150 | struct tty *tp = km_tty[minor(dev)]; | |
151 | ||
152 | tty_lock(tp); | |
153 | ret = (*linesw[tp->t_line].l_close)(tp, flag); | |
154 | ttyclose(tp); | |
155 | tty_unlock(tp); | |
156 | ||
0a7de745 | 157 | return ret; |
5ba3f43e A |
158 | } |
159 | ||
160 | int | |
161 | kmread(dev_t dev, struct uio * uio, int ioflag) | |
162 | { | |
163 | int ret; | |
164 | struct tty *tp = km_tty[minor(dev)]; | |
165 | ||
166 | tty_lock(tp); | |
167 | ret = (*linesw[tp->t_line].l_read)(tp, uio, ioflag); | |
168 | tty_unlock(tp); | |
169 | ||
0a7de745 | 170 | return ret; |
5ba3f43e A |
171 | } |
172 | ||
173 | int | |
174 | kmwrite(dev_t dev, struct uio * uio, int ioflag) | |
175 | { | |
176 | int ret; | |
177 | struct tty *tp = km_tty[minor(dev)]; | |
178 | ||
179 | tty_lock(tp); | |
180 | ret = (*linesw[tp->t_line].l_write)(tp, uio, ioflag); | |
181 | tty_unlock(tp); | |
182 | ||
0a7de745 | 183 | return ret; |
5ba3f43e A |
184 | } |
185 | ||
186 | int | |
187 | kmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, proc_t p) | |
188 | { | |
189 | int error = 0; | |
190 | struct tty *tp = km_tty[minor(dev)]; | |
191 | struct winsize *wp; | |
192 | ||
193 | tty_lock(tp); | |
194 | ||
195 | switch (cmd) { | |
196 | case KMIOCSIZE: | |
197 | wp = (struct winsize *) data; | |
198 | *wp = tp->t_winsize; | |
199 | break; | |
200 | ||
201 | case TIOCSWINSZ: | |
202 | /* | |
203 | * Prevent changing of console size -- this ensures that | |
204 | * login doesn't revert to the termcap-defined size | |
205 | */ | |
206 | error = EINVAL; | |
207 | break; | |
208 | ||
0a7de745 | 209 | /* Bodge in the CLOCAL flag as the km device is always local */ |
5ba3f43e A |
210 | case TIOCSETA_32: |
211 | case TIOCSETAW_32: | |
212 | case TIOCSETAF_32: | |
0a7de745 A |
213 | { |
214 | struct termios32 *t = (struct termios32 *)data; | |
215 | t->c_cflag |= CLOCAL; | |
216 | /* No Break */ | |
217 | } | |
5ba3f43e A |
218 | goto fallthrough; |
219 | case TIOCSETA_64: | |
220 | case TIOCSETAW_64: | |
221 | case TIOCSETAF_64: | |
0a7de745 A |
222 | { |
223 | struct user_termios *t = (struct user_termios *)data; | |
224 | t->c_cflag |= CLOCAL; | |
225 | /* No Break */ | |
226 | } | |
5ba3f43e A |
227 | fallthrough: |
228 | default: | |
0a7de745 A |
229 | error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); |
230 | if (ENOTTY != error) { | |
5ba3f43e | 231 | break; |
0a7de745 | 232 | } |
5ba3f43e A |
233 | error = ttioctl_locked(tp, cmd, data, flag, p); |
234 | break; | |
235 | } | |
236 | ||
237 | tty_unlock(tp); | |
238 | ||
0a7de745 | 239 | return error; |
5ba3f43e A |
240 | } |
241 | ||
242 | ||
243 | /* | |
244 | * kmputc | |
245 | * | |
246 | * Output a character to the serial console driver via cnputcusr(), | |
247 | * which is exported by that driver. | |
248 | * | |
249 | * Locks: Assumes tp in the calling tty driver code is locked on | |
250 | * entry, remains locked on exit | |
251 | * | |
252 | * Notes: Called from kmoutput(); giving the locking output | |
253 | * assumptions here, this routine should be static (and | |
254 | * inlined, given there is only one call site). | |
255 | */ | |
0a7de745 | 256 | int |
5ba3f43e A |
257 | kmputc(__unused dev_t dev, char c) |
258 | { | |
0a7de745 | 259 | if (!disableConsoleOutput && initialized) { |
5ba3f43e | 260 | /* OCRNL */ |
0a7de745 | 261 | if (c == '\n') { |
5ba3f43e | 262 | cnputcusr('\r'); |
0a7de745 | 263 | } |
5ba3f43e A |
264 | cnputcusr(c); |
265 | } | |
266 | ||
0a7de745 | 267 | return 0; |
5ba3f43e A |
268 | } |
269 | ||
270 | ||
271 | /* | |
272 | * Callouts from linesw. | |
273 | */ | |
274 | ||
0a7de745 | 275 | #define KM_LOWAT_DELAY ((ns_time_t)1000) |
5ba3f43e A |
276 | |
277 | /* | |
278 | * t_oproc for this driver; called from within the line discipline | |
279 | * | |
280 | * Locks: Assumes tp is locked on entry, remains locked on exit | |
281 | */ | |
282 | static void | |
283 | kmstart(struct tty *tp) | |
284 | { | |
0a7de745 | 285 | if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) { |
5ba3f43e | 286 | goto out; |
0a7de745 A |
287 | } |
288 | if (tp->t_outq.c_cc == 0) { | |
5ba3f43e | 289 | goto out; |
0a7de745 | 290 | } |
5ba3f43e A |
291 | tp->t_state |= TS_BUSY; |
292 | if (tp->t_outq.c_cc > tp->t_lowat) { | |
293 | /* | |
294 | * Start immediately. | |
295 | */ | |
296 | kmoutput(tp); | |
297 | } else { | |
298 | /* | |
299 | * Wait a bit... | |
300 | */ | |
301 | #if 0 | |
302 | /* FIXME */ | |
303 | timeout(kmtimeout, tp, hz); | |
304 | #else | |
305 | kmoutput(tp); | |
306 | #endif | |
307 | } | |
308 | return; | |
309 | ||
310 | out: | |
0a7de745 | 311 | (*linesw[tp->t_line].l_start)(tp); |
5ba3f43e A |
312 | return; |
313 | } | |
314 | ||
315 | /* | |
316 | * One-shot output retry timeout from kmoutput(); re-calls kmoutput() at | |
317 | * intervals until the output queue for the tty is empty, at which point | |
318 | * the timeout is not rescheduled by kmoutput() | |
319 | * | |
320 | * This function must take the tty_lock() around the kmoutput() call; it | |
321 | * ignores the return value. | |
322 | */ | |
323 | static void | |
324 | kmtimeout(void *arg) | |
325 | { | |
326 | struct tty *tp = (struct tty *)arg; | |
327 | ||
328 | tty_lock(tp); | |
329 | (void)kmoutput(tp); | |
330 | tty_unlock(tp); | |
331 | } | |
332 | ||
333 | /* | |
334 | * kmoutput | |
335 | * | |
336 | * Locks: Assumes tp is locked on entry, remains locked on exit | |
337 | * | |
338 | * Notes: Called from kmstart() and kmtimeout(); kmtimeout() is a | |
339 | * timer initiated by this routine to deal with pending | |
340 | * output not yet flushed (output is flushed at a maximum | |
341 | * of sizeof(buf) charatcers at a time before dropping into | |
342 | * the timeout code). | |
343 | */ | |
344 | static int | |
345 | kmoutput(struct tty * tp) | |
346 | { | |
0a7de745 A |
347 | unsigned char buf[80]; /* buffer; limits output per call */ |
348 | unsigned char *cp; | |
349 | int cc = -1; | |
5ba3f43e A |
350 | |
351 | /* While there is data available to be output... */ | |
352 | while (tp->t_outq.c_cc > 0) { | |
353 | cc = ndqb(&tp->t_outq, 0); | |
0a7de745 | 354 | if (cc == 0) { |
5ba3f43e | 355 | break; |
0a7de745 | 356 | } |
5ba3f43e A |
357 | /* |
358 | * attempt to output as many characters as are available, | |
359 | * up to the available transfer buffer size. | |
360 | */ | |
361 | cc = min(cc, sizeof(buf)); | |
362 | /* copy the output queue contents to the buffer */ | |
363 | (void) q_to_b(&tp->t_outq, buf, cc); | |
364 | for (cp = buf; cp < &buf[cc]; cp++) { | |
365 | /* output the buffer one charatcer at a time */ | |
366 | *cp = *cp & 0x7f; | |
367 | } | |
368 | if (cc > 1) { | |
369 | cnputsusr((char *)buf, cc); | |
370 | } else { | |
371 | kmputc(tp->t_dev, *buf); | |
372 | } | |
373 | } | |
374 | /* | |
375 | * XXX This is likely not necessary, as the tty output queue is not | |
376 | * XXX writeable while we hold the tty_lock(). | |
377 | */ | |
378 | if (tp->t_outq.c_cc > 0) { | |
379 | timeout(kmtimeout, tp, hz); | |
380 | } | |
381 | tp->t_state &= ~TS_BUSY; | |
382 | /* Start the output processing for the line discipline */ | |
0a7de745 | 383 | (*linesw[tp->t_line].l_start)(tp); |
5ba3f43e A |
384 | |
385 | return 0; | |
386 | } | |
387 | ||
388 | ||
389 | /* | |
390 | * cons_cinput | |
391 | * | |
392 | * Driver character input from the polled mode serial console driver calls | |
393 | * this routine to input a character from the serial driver into the tty | |
394 | * line discipline specific input processing receiv interrupt routine, | |
395 | * l_rint(). | |
396 | * | |
397 | * Locks: Assumes that the tty_lock() is NOT held on the tp, so a | |
398 | * serial driver should NOT call this function as a result | |
399 | * of being called from a function which already holds the | |
400 | * lock; ECHOE will be handled at the line discipline, if | |
401 | * output echo processing is going to occur. | |
402 | */ | |
403 | void | |
404 | cons_cinput(char ch) | |
405 | { | |
0a7de745 | 406 | struct tty *tp = km_tty[0]; /* XXX */ |
5ba3f43e A |
407 | |
408 | tty_lock(tp); | |
0a7de745 | 409 | (*linesw[tp->t_line].l_rint)(ch, tp); |
5ba3f43e A |
410 | tty_unlock(tp); |
411 | } |