source: mainline/kernel/genarch/src/drivers/ega/ega.c@ bab75df6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bab75df6 was bab75df6, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Let kernel code get printf via the standard stdio header. Clean up unused includes.

  • Property mode set to 100644
File size: 12.1 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup kernel_genarch
30 * @{
31 */
32/**
33 * @file
34 * @brief EGA driver.
35 */
36
37#include <debug.h>
38#include <genarch/drivers/ega/ega.h>
39#include <mm/km.h>
40#include <mm/as.h>
41#include <mm/slab.h>
42#include <typedefs.h>
43#include <arch/asm.h>
44#include <mem.h>
45#include <str.h>
46#include <console/chardev.h>
47#include <console/console.h>
48#include <sysinfo/sysinfo.h>
49#include <ddi/ddi.h>
50
51/*
52 * The EGA driver.
53 * Simple and short. Function for displaying characters and "scrolling".
54 */
55
56#define SPACE 0x20
57#define STYLE 0x1e
58#define INVAL 0x17
59
60#define EMPTY_CHAR ((STYLE << 8) | SPACE)
61
62typedef struct {
63 IRQ_SPINLOCK_DECLARE(lock);
64
65 parea_t parea;
66
67 uint32_t cursor;
68 uint8_t *addr;
69 uint8_t *backbuf;
70 ioport8_t *base;
71} ega_instance_t;
72
73static void ega_putwchar(outdev_t *, wchar_t);
74static void ega_redraw(outdev_t *);
75
76static outdev_operations_t egadev_ops = {
77 .write = ega_putwchar,
78 .redraw = ega_redraw,
79 .scroll_up = NULL,
80 .scroll_down = NULL
81};
82
83static uint16_t ega_oem_glyph(const wchar_t ch)
84{
85 if ((ch >= 0x0000) && (ch <= 0x007f))
86 return ch;
87
88 if (ch == 0x00a0)
89 return 255;
90
91 if (ch == 0x00a1)
92 return 173;
93
94 if ((ch >= 0x00a2) && (ch <= 0x00a3))
95 return (ch - 7);
96
97 if (ch == 0x00a5)
98 return 157;
99
100 if (ch == 0x00aa)
101 return 166;
102
103 if (ch == 0x00ab)
104 return 174;
105
106 if (ch == 0x00ac)
107 return 170;
108
109 if (ch == 0x00b0)
110 return 248;
111
112 if (ch == 0x00b1)
113 return 241;
114
115 if (ch == 0x00b2)
116 return 253;
117
118 if (ch == 0x00b5)
119 return 230;
120
121 if (ch == 0x00b7)
122 return 250;
123
124 if (ch == 0x00ba)
125 return 167;
126
127 if (ch == 0x00bb)
128 return 175;
129
130 if (ch == 0x00bc)
131 return 172;
132
133 if (ch == 0x00bd)
134 return 171;
135
136 if (ch == 0x00bf)
137 return 168;
138
139 if ((ch >= 0x00c4) && (ch <= 0x00c5))
140 return (ch - 54);
141
142 if (ch == 0x00c6)
143 return 146;
144
145 if (ch == 0x00c7)
146 return 128;
147
148 if (ch == 0x00c9)
149 return 144;
150
151 if (ch == 0x00d1)
152 return 165;
153
154 if (ch == 0x00d6)
155 return 153;
156
157 if (ch == 0x00dc)
158 return 154;
159
160 if (ch == 0x00df)
161 return 225;
162
163 if (ch == 0x00e0)
164 return 133;
165
166 if (ch == 0x00e1)
167 return 160;
168
169 if (ch == 0x00e2)
170 return 131;
171
172 if (ch == 0x00e4)
173 return 132;
174
175 if (ch == 0x00e5)
176 return 134;
177
178 if (ch == 0x00e6)
179 return 145;
180
181 if (ch == 0x00e7)
182 return 135;
183
184 if (ch == 0x00e8)
185 return 138;
186
187 if (ch == 0x00e9)
188 return 130;
189
190 if ((ch >= 0x00ea) && (ch <= 0x00eb))
191 return (ch - 98);
192
193 if (ch == 0x00ec)
194 return 141;
195
196 if (ch == 0x00ed)
197 return 161;
198
199 if (ch == 0x00ee)
200 return 140;
201
202 if (ch == 0x00ef)
203 return 139;
204
205 if (ch == 0x00f1)
206 return 164;
207
208 if (ch == 0x00f2)
209 return 149;
210
211 if (ch == 0x00f3)
212 return 162;
213
214 if (ch == 0x00f4)
215 return 147;
216
217 if (ch == 0x00f6)
218 return 148;
219
220 if (ch == 0x00f7)
221 return 246;
222
223 if (ch == 0x00f9)
224 return 151;
225
226 if (ch == 0x00fa)
227 return 163;
228
229 if (ch == 0x00fb)
230 return 150;
231
232 if (ch == 0x00fc)
233 return 129;
234
235 if (ch == 0x00ff)
236 return 152;
237
238 if (ch == 0x0192)
239 return 159;
240
241 if (ch == 0x0393)
242 return 226;
243
244 if (ch == 0x0398)
245 return 233;
246
247 if (ch == 0x03a3)
248 return 228;
249
250 if (ch == 0x03a6)
251 return 232;
252
253 if (ch == 0x03a9)
254 return 234;
255
256 if (ch == 0x03b1)
257 return 224;
258
259 if (ch == 0x03b4)
260 return 235;
261
262 if (ch == 0x03b5)
263 return 238;
264
265 if (ch == 0x03c0)
266 return 227;
267
268 if (ch == 0x03c3)
269 return 229;
270
271 if (ch == 0x03c4)
272 return 231;
273
274 if (ch == 0x03c6)
275 return 237;
276
277 if (ch == 0x207f)
278 return 252;
279
280 if (ch == 0x20a7)
281 return 158;
282
283 if (ch == 0x2219)
284 return 249;
285
286 if (ch == 0x221a)
287 return 251;
288
289 if (ch == 0x221e)
290 return 236;
291
292 if (ch == 0x2229)
293 return 239;
294
295 if (ch == 0x2248)
296 return 247;
297
298 if (ch == 0x2261)
299 return 240;
300
301 if (ch == 0x2264)
302 return 243;
303
304 if (ch == 0x2265)
305 return 242;
306
307 if (ch == 0x2310)
308 return 169;
309
310 if ((ch >= 0x2320) && (ch <= 0x2321))
311 return (ch - 8748);
312
313 if (ch == 0x2500)
314 return 196;
315
316 if (ch == 0x2502)
317 return 179;
318
319 if (ch == 0x250c)
320 return 218;
321
322 if (ch == 0x2510)
323 return 191;
324
325 if (ch == 0x2514)
326 return 192;
327
328 if (ch == 0x2518)
329 return 217;
330
331 if (ch == 0x251c)
332 return 195;
333
334 if (ch == 0x2524)
335 return 180;
336
337 if (ch == 0x252c)
338 return 194;
339
340 if (ch == 0x2534)
341 return 193;
342
343 if (ch == 0x253c)
344 return 197;
345
346 if (ch == 0x2550)
347 return 205;
348
349 if (ch == 0x2551)
350 return 186;
351
352 if ((ch >= 0x2552) && (ch <= 0x2553))
353 return (ch - 9341);
354
355 if (ch == 0x2554)
356 return 201;
357
358 if (ch == 0x2555)
359 return 184;
360
361 if (ch == 0x2556)
362 return 183;
363
364 if (ch == 0x2557)
365 return 187;
366
367 if (ch == 0x2558)
368 return 212;
369
370 if (ch == 0x2559)
371 return 211;
372
373 if (ch == 0x255a)
374 return 200;
375
376 if (ch == 0x255b)
377 return 190;
378
379 if (ch == 0x255c)
380 return 189;
381
382 if (ch == 0x255d)
383 return 188;
384
385 if ((ch >= 0x255e) && (ch <= 0x255f))
386 return (ch - 9368);
387
388 if (ch == 0x2560)
389 return 204;
390
391 if ((ch >= 0x2561) && (ch <= 0x2562))
392 return (ch - 9388);
393
394 if (ch == 0x2563)
395 return 185;
396
397 if ((ch >= 0x2564) && (ch <= 0x2565))
398 return (ch - 9363);
399
400 if (ch == 0x2566)
401 return 203;
402
403 if ((ch >= 0x2567) && (ch <= 0x2568))
404 return (ch - 9368);
405
406 if (ch == 0x2569)
407 return 202;
408
409 if (ch == 0x256a)
410 return 216;
411
412 if (ch == 0x256b)
413 return 215;
414
415 if (ch == 0x256c)
416 return 206;
417
418 if (ch == 0x2580)
419 return 223;
420
421 if (ch == 0x2584)
422 return 220;
423
424 if (ch == 0x2588)
425 return 219;
426
427 if (ch == 0x258c)
428 return 221;
429
430 if (ch == 0x2590)
431 return 222;
432
433 if ((ch >= 0x2591) && (ch <= 0x2593))
434 return (ch - 9441);
435
436 return 256;
437}
438
439/*
440 * This function takes care of scrolling.
441 */
442static void ega_check_cursor(ega_instance_t *instance)
443{
444 if (instance->cursor < EGA_SCREEN)
445 return;
446
447 memmove((void *) instance->backbuf,
448 (void *) (instance->backbuf + EGA_COLS * 2),
449 (EGA_SCREEN - EGA_COLS) * 2);
450 memsetw(instance->backbuf + (EGA_SCREEN - EGA_COLS) * 2,
451 EGA_COLS, EMPTY_CHAR);
452
453 if ((!instance->parea.mapped) || (console_override)) {
454 memmove((void *) instance->addr,
455 (void *) (instance->addr + EGA_COLS * 2),
456 (EGA_SCREEN - EGA_COLS) * 2);
457 memsetw(instance->addr + (EGA_SCREEN - EGA_COLS) * 2,
458 EGA_COLS, EMPTY_CHAR);
459 }
460
461 instance->cursor = instance->cursor - EGA_COLS;
462}
463
464static void ega_show_cursor(ega_instance_t *instance)
465{
466 if ((!instance->parea.mapped) || (console_override)) {
467 pio_write_8(instance->base + EGA_INDEX_REG, 0x0a);
468 uint8_t stat = pio_read_8(instance->base + EGA_DATA_REG);
469 pio_write_8(instance->base + EGA_INDEX_REG, 0x0a);
470 pio_write_8(instance->base + EGA_DATA_REG, stat & (~(1 << 5)));
471 }
472}
473
474static void ega_move_cursor(ega_instance_t *instance)
475{
476 if ((!instance->parea.mapped) || (console_override)) {
477 pio_write_8(instance->base + EGA_INDEX_REG, 0x0e);
478 pio_write_8(instance->base + EGA_DATA_REG,
479 (uint8_t) ((instance->cursor >> 8) & 0xff));
480 pio_write_8(instance->base + EGA_INDEX_REG, 0x0f);
481 pio_write_8(instance->base + EGA_DATA_REG,
482 (uint8_t) (instance->cursor & 0xff));
483 }
484}
485
486static void ega_sync_cursor(ega_instance_t *instance)
487{
488 if ((!instance->parea.mapped) || (console_override)) {
489 pio_write_8(instance->base + EGA_INDEX_REG, 0x0e);
490 uint8_t hi = pio_read_8(instance->base + EGA_DATA_REG);
491 pio_write_8(instance->base + EGA_INDEX_REG, 0x0f);
492 uint8_t lo = pio_read_8(instance->base + EGA_DATA_REG);
493
494 instance->cursor = (hi << 8) | lo;
495 } else
496 instance->cursor = 0;
497
498 if (instance->cursor >= EGA_SCREEN)
499 instance->cursor = 0;
500
501 if ((instance->cursor % EGA_COLS) != 0)
502 instance->cursor =
503 (instance->cursor + EGA_COLS) - instance->cursor % EGA_COLS;
504
505 memsetw(instance->backbuf + instance->cursor * 2,
506 EGA_SCREEN - instance->cursor, EMPTY_CHAR);
507
508 if ((!instance->parea.mapped) || (console_override))
509 memsetw(instance->addr + instance->cursor * 2,
510 EGA_SCREEN - instance->cursor, EMPTY_CHAR);
511
512 ega_check_cursor(instance);
513 ega_move_cursor(instance);
514 ega_show_cursor(instance);
515}
516
517static void ega_display_wchar(ega_instance_t *instance, wchar_t ch)
518{
519 uint16_t index = ega_oem_glyph(ch);
520 uint8_t glyph;
521 uint8_t style;
522
523 if ((index >> 8)) {
524 glyph = U_SPECIAL;
525 style = INVAL;
526 } else {
527 glyph = index & 0xff;
528 style = STYLE;
529 }
530
531 instance->backbuf[instance->cursor * 2] = glyph;
532 instance->backbuf[instance->cursor * 2 + 1] = style;
533
534 if ((!instance->parea.mapped) || (console_override)) {
535 instance->addr[instance->cursor * 2] = glyph;
536 instance->addr[instance->cursor * 2 + 1] = style;
537 }
538}
539
540static void ega_putwchar(outdev_t *dev, wchar_t ch)
541{
542 ega_instance_t *instance = (ega_instance_t *) dev->data;
543
544 irq_spinlock_lock(&instance->lock, true);
545
546 switch (ch) {
547 case '\n':
548 instance->cursor = (instance->cursor + EGA_COLS) -
549 instance->cursor % EGA_COLS;
550 break;
551 case '\t':
552 instance->cursor = (instance->cursor + 8) -
553 instance->cursor % 8;
554 break;
555 case '\b':
556 if (instance->cursor % EGA_COLS)
557 instance->cursor--;
558 break;
559 default:
560 ega_display_wchar(instance, ch);
561 instance->cursor++;
562 break;
563 }
564 ega_check_cursor(instance);
565 ega_move_cursor(instance);
566
567 irq_spinlock_unlock(&instance->lock, true);
568}
569
570static void ega_redraw(outdev_t *dev)
571{
572 ega_instance_t *instance = (ega_instance_t *) dev->data;
573
574 irq_spinlock_lock(&instance->lock, true);
575
576 memcpy(instance->addr, instance->backbuf, EGA_VRAM_SIZE);
577 ega_move_cursor(instance);
578 ega_show_cursor(instance);
579
580 irq_spinlock_unlock(&instance->lock, true);
581}
582
583outdev_t *ega_init(ioport8_t *base, uintptr_t addr)
584{
585 outdev_t *egadev = malloc(sizeof(outdev_t));
586 if (!egadev)
587 return NULL;
588
589 ega_instance_t *instance = malloc(sizeof(ega_instance_t));
590 if (!instance) {
591 free(egadev);
592 return NULL;
593 }
594
595 outdev_initialize("egadev", egadev, &egadev_ops);
596 egadev->data = instance;
597
598 irq_spinlock_initialize(&instance->lock, "*ega.instance.lock");
599
600 instance->base = base;
601 instance->addr = (uint8_t *) km_map(addr, EGA_VRAM_SIZE,
602 KM_NATURAL_ALIGNMENT, PAGE_WRITE | PAGE_NOT_CACHEABLE);
603 if (!instance->addr) {
604 LOG("Unable to EGA video memory.");
605 free(instance);
606 free(egadev);
607 return NULL;
608 }
609
610 instance->backbuf = (uint8_t *) malloc(EGA_VRAM_SIZE);
611 if (!instance->backbuf) {
612 LOG("Unable to allocate backbuffer.");
613 free(instance);
614 free(egadev);
615 return NULL;
616 }
617
618 link_initialize(&instance->parea.link);
619 instance->parea.pbase = addr;
620 instance->parea.frames = SIZE2FRAMES(EGA_VRAM_SIZE);
621 instance->parea.unpriv = false;
622 instance->parea.mapped = false;
623 ddi_parea_register(&instance->parea);
624
625 /* Synchronize the back buffer and cursor position. */
626 memcpy(instance->backbuf, instance->addr, EGA_VRAM_SIZE);
627 ega_sync_cursor(instance);
628
629 if (!fb_exported) {
630 /*
631 * We export the kernel framebuffer for uspace usage.
632 * This is used in the case the uspace framebuffer
633 * driver is not self-sufficient.
634 */
635 sysinfo_set_item_val("fb", NULL, true);
636 sysinfo_set_item_val("fb.kind", NULL, 2);
637 sysinfo_set_item_val("fb.width", NULL, EGA_COLS);
638 sysinfo_set_item_val("fb.height", NULL, EGA_ROWS);
639 sysinfo_set_item_val("fb.blinking", NULL, true);
640 sysinfo_set_item_val("fb.address.physical", NULL, addr);
641
642 fb_exported = true;
643 }
644
645 return egadev;
646}
647
648/** @}
649 */
Note: See TracBrowser for help on using the repository browser.