2 * linux/drivers/video/console/sticon.c - console driver using HP's STI firmware
4 * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
5 * Copyright (C) 2002-2020 Helge Deller <deller@gmx.de>
7 * Based on linux/drivers/video/vgacon.c and linux/drivers/video/fbcon.c,
10 * Created 28 Sep 1997 by Geert Uytterhoeven
11 * Rewritten by Martin Mares <mj@ucw.cz>, July 1998
12 * Copyright (C) 1991, 1992 Linus Torvalds
14 * Copyright (C) 1995 Geert Uytterhoeven
15 * Copyright (C) 1993 Bjoern Brauel
17 * Copyright (C) 1993 Hamish Macdonald
19 * Copyright (C) 1994 David Carter [carter@compsci.bristol.ac.uk]
21 * with work by William Rucklidge (wjr@cs.cornell.edu)
23 * Jes Sorensen (jds@kom.auc.dk)
25 * with work by Guenther Kelleter
28 * Emmanuel Marty (core@ggi-project.org)
29 * Jakub Jelinek (jj@ultra.linux.cz)
30 * Martin Mares <mj@ucw.cz>
32 * This file is subject to the terms and conditions of the GNU General Public
33 * License. See the file COPYING in the main directory of this archive for
38 #include <linux/init.h>
39 #include <linux/kernel.h>
40 #include <linux/console.h>
41 #include <linux/errno.h>
42 #include <linux/vt_kern.h>
44 #include <linux/selection.h>
45 #include <linux/module.h>
46 #include <linux/slab.h>
47 #include <linux/font.h>
48 #include <linux/crc32.h>
53 #include "../fbdev/sticore.h"
55 /* switching to graphics mode */
57 static int vga_is_gfx;
59 #define STI_DEF_FONT sticon_sti->font
61 /* borrowed from fbcon.c */
62 #define FNTREFCOUNT(fd) (fd->refcount)
63 #define FNTCRC(fd) (fd->crc)
64 static struct sti_cooked_font *font_data[MAX_NR_CONSOLES];
66 /* this is the sti_struct used for this console */
67 static struct sti_struct *sticon_sti;
69 static const char *sticon_startup(void)
74 static void sticon_putc(struct vc_data *conp, int c, int ypos, int xpos)
76 if (vga_is_gfx || console_blanked)
79 if (conp->vc_mode != KD_TEXT)
82 sti_putc(sticon_sti, c, ypos, xpos, font_data[conp->vc_num]);
85 static void sticon_putcs(struct vc_data *conp, const unsigned short *s,
86 int count, int ypos, int xpos)
88 if (vga_is_gfx || console_blanked)
91 if (conp->vc_mode != KD_TEXT)
95 sti_putc(sticon_sti, scr_readw(s++), ypos, xpos++,
96 font_data[conp->vc_num]);
100 static void sticon_cursor(struct vc_data *conp, int mode)
104 /* no cursor update if screen is blanked */
105 if (vga_is_gfx || console_blanked)
108 car1 = conp->vc_screenbuf[conp->state.x + conp->state.y * conp->vc_cols];
111 sti_putc(sticon_sti, car1, conp->state.y, conp->state.x,
112 font_data[conp->vc_num]);
116 switch (CUR_SIZE(conp->vc_cursor_type)) {
118 case CUR_LOWER_THIRD:
122 sti_putc(sticon_sti, (car1 & 255) + (0 << 8) + (7 << 11),
123 conp->state.y, conp->state.x, font_data[conp->vc_num]);
130 static bool sticon_scroll(struct vc_data *conp, unsigned int t,
131 unsigned int b, enum con_scroll dir, unsigned int count)
133 struct sti_struct *sti = sticon_sti;
138 sticon_cursor(conp, CM_ERASE);
142 sti_bmove(sti, t + count, 0, t, 0, b - t - count, conp->vc_cols,
143 font_data[conp->vc_num]);
144 sti_clear(sti, b - count, 0, count, conp->vc_cols,
145 conp->vc_video_erase_char, font_data[conp->vc_num]);
149 sti_bmove(sti, t, 0, t + count, 0, b - t - count, conp->vc_cols,
150 font_data[conp->vc_num]);
151 sti_clear(sti, t, 0, count, conp->vc_cols,
152 conp->vc_video_erase_char, font_data[conp->vc_num]);
159 static int sticon_set_def_font(int unit, struct console_font *op)
161 if (font_data[unit] != STI_DEF_FONT) {
162 if (--FNTREFCOUNT(font_data[unit]) == 0) {
163 kfree(font_data[unit]->raw_ptr);
164 kfree(font_data[unit]);
166 font_data[unit] = STI_DEF_FONT;
172 static int sticon_set_font(struct vc_data *vc, struct console_font *op)
174 struct sti_struct *sti = sticon_sti;
175 int vc_cols, vc_rows, vc_old_cols, vc_old_rows;
176 int unit = vc->vc_num;
179 int size, i, bpc, pitch;
180 struct sti_rom_font *new_font;
181 struct sti_cooked_font *cooked_font;
182 unsigned char *data = op->data, *p;
184 if ((w < 6) || (h < 6) || (w > 32) || (h > 32)
185 || (op->charcount != 256 && op->charcount != 512))
187 pitch = ALIGN(w, 8) / 8;
189 size = bpc * op->charcount;
191 new_font = kmalloc(sizeof(*new_font) + size, STI_LOWMEM);
195 new_font->first_char = 0;
196 new_font->last_char = op->charcount - 1;
198 new_font->height = h;
199 new_font->font_type = STI_FONT_HPROMAN8;
200 new_font->bytes_per_char = bpc;
201 new_font->underline_height = 0;
202 new_font->underline_pos = 0;
204 cooked_font = kzalloc(sizeof(*cooked_font), GFP_KERNEL);
209 cooked_font->raw = new_font;
210 cooked_font->raw_ptr = new_font;
211 cooked_font->width = w;
212 cooked_font->height = h;
213 FNTREFCOUNT(cooked_font) = 0; /* usage counter */
215 p = (unsigned char *) new_font;
216 p += sizeof(*new_font);
217 for (i = 0; i < op->charcount; i++) {
218 memcpy(p, data, bpc);
222 FNTCRC(cooked_font) = crc32(0, new_font, size + sizeof(*new_font));
223 sti_font_convert_bytemode(sti, cooked_font);
224 new_font = cooked_font->raw_ptr;
226 /* check if font is already used by other console */
227 for (i = 0; i < MAX_NR_CONSOLES; i++) {
228 if (font_data[i] != STI_DEF_FONT
229 && (FNTCRC(font_data[i]) == FNTCRC(cooked_font))) {
232 /* current font is the same as the new one */
235 cooked_font = font_data[i];
236 new_font = cooked_font->raw_ptr;
241 /* clear screen with old font: we now may have less rows */
242 vc_old_rows = vc->vc_rows;
243 vc_old_cols = vc->vc_cols;
244 sti_clear(sticon_sti, 0, 0, vc_old_rows, vc_old_cols,
245 vc->vc_video_erase_char, font_data[vc->vc_num]);
247 /* delete old font in case it is a user font */
248 sticon_set_def_font(unit, NULL);
250 FNTREFCOUNT(cooked_font)++;
251 font_data[unit] = cooked_font;
253 vc_cols = sti_onscreen_x(sti) / cooked_font->width;
254 vc_rows = sti_onscreen_y(sti) / cooked_font->height;
255 vc_resize(vc, vc_cols, vc_rows);
257 /* need to repaint screen if cols & rows are same as old font */
258 if (vc_cols == vc_old_cols && vc_rows == vc_old_rows)
264 static int sticon_font_default(struct vc_data *vc, struct console_font *op, char *name)
266 return sticon_set_def_font(vc->vc_num, op);
269 static int sticon_font_set(struct vc_data *vc, struct console_font *font,
272 return sticon_set_font(vc, font);
275 static void sticon_init(struct vc_data *c, int init)
277 struct sti_struct *sti = sticon_sti;
278 int vc_cols, vc_rows;
280 sti_set(sti, 0, 0, sti_onscreen_y(sti), sti_onscreen_x(sti), 0);
281 vc_cols = sti_onscreen_x(sti) / sti->font->width;
282 vc_rows = sti_onscreen_y(sti) / sti->font->height;
283 c->vc_can_do_color = 1;
286 c->vc_cols = vc_cols;
287 c->vc_rows = vc_rows;
289 vc_resize(c, vc_cols, vc_rows);
293 static void sticon_deinit(struct vc_data *c)
297 /* free memory used by user font */
298 for (i = 0; i < MAX_NR_CONSOLES; i++)
299 sticon_set_def_font(i, NULL);
302 static void sticon_clear(struct vc_data *conp, int sy, int sx, int height,
305 if (!height || !width)
308 sti_clear(sticon_sti, sy, sx, height, width,
309 conp->vc_video_erase_char, font_data[conp->vc_num]);
312 static int sticon_switch(struct vc_data *conp)
314 return 1; /* needs refreshing */
317 static int sticon_blank(struct vc_data *c, int blank, int mode_switch)
324 sti_clear(sticon_sti, 0, 0, c->vc_rows, c->vc_cols, BLANK,
325 font_data[c->vc_num]);
331 static u8 sticon_build_attr(struct vc_data *conp, u8 color,
332 enum vc_intensity intens,
333 bool blink, bool underline, bool reverse,
337 u8 bg = (color & 0x70) >> 4;
340 return (fg << 3) | bg;
342 return (bg << 3) | fg;
345 static void sticon_invert_region(struct vc_data *conp, u16 *p, int count)
347 int col = 1; /* vga_can_do_color; */
350 u16 a = scr_readw(p);
353 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
355 a = ((a & 0x0700) == 0x0100) ? 0x7000 : 0x7700;
361 static const struct consw sti_con = {
362 .owner = THIS_MODULE,
363 .con_startup = sticon_startup,
364 .con_init = sticon_init,
365 .con_deinit = sticon_deinit,
366 .con_clear = sticon_clear,
367 .con_putc = sticon_putc,
368 .con_putcs = sticon_putcs,
369 .con_cursor = sticon_cursor,
370 .con_scroll = sticon_scroll,
371 .con_switch = sticon_switch,
372 .con_blank = sticon_blank,
373 .con_font_set = sticon_font_set,
374 .con_font_default = sticon_font_default,
375 .con_build_attr = sticon_build_attr,
376 .con_invert_region = sticon_invert_region,
381 static int __init sticonsole_init(void)
385 /* already initialized ? */
389 sticon_sti = sti_get_rom(0);
393 for (i = 0; i < MAX_NR_CONSOLES; i++)
394 font_data[i] = STI_DEF_FONT;
396 pr_info("sticon: Initializing STI text console on %s at [%s]\n",
397 sticon_sti->sti_data->inq_outptr.dev_name,
398 sticon_sti->pa_path);
400 err = do_take_over_console(&sti_con, 0, MAX_NR_CONSOLES - 1,
401 PAGE0->mem_cons.cl_class != CL_DUPLEX);
407 module_init(sticonsole_init);
408 MODULE_LICENSE("GPL");