1/*
2 *  linux/drivers/video/console/sticon.c - console driver using HP's STI firmware
3 *
4 *	Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
5 *	Copyright (C) 2002 Helge Deller <deller@gmx.de>
6 *
7 *  Based on linux/drivers/video/vgacon.c and linux/drivers/video/fbcon.c,
8 *  which were
9 *
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
13 *			    1995  Jay Estabrook
14 *	Copyright (C) 1995 Geert Uytterhoeven
15 *	Copyright (C) 1993 Bjoern Brauel
16 *			   Roman Hodek
17 *	Copyright (C) 1993 Hamish Macdonald
18 *			   Greg Harp
19 *	Copyright (C) 1994 David Carter [carter@compsci.bristol.ac.uk]
20 *
21 *	      with work by William Rucklidge (wjr@cs.cornell.edu)
22 *			   Geert Uytterhoeven
23 *			   Jes Sorensen (jds@kom.auc.dk)
24 *			   Martin Apel
25 *	      with work by Guenther Kelleter
26 *			   Martin Schaller
27 *			   Andreas Schwab
28 *			   Emmanuel Marty (core@ggi-project.org)
29 *			   Jakub Jelinek (jj@ultra.linux.cz)
30 *			   Martin Mares <mj@ucw.cz>
31 *
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
34 *  more details.
35 *
36 */
37
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>
43#include <linux/kd.h>
44#include <linux/selection.h>
45#include <linux/module.h>
46
47#include <asm/io.h>
48
49#include "../sticore.h"
50
51/* switching to graphics mode */
52#define BLANK 0
53static int vga_is_gfx;
54
55/* this is the sti_struct used for this console */
56static struct sti_struct *sticon_sti;
57
58/* Software scrollback */
59static unsigned long softback_buf, softback_curr;
60static unsigned long softback_in;
61static unsigned long /* softback_top, */ softback_end;
62static int softback_lines;
63
64/* software cursor */
65static int cursor_drawn;
66#define CURSOR_DRAW_DELAY		(1)
67#define DEFAULT_CURSOR_BLINK_RATE	(20)
68
69static int vbl_cursor_cnt;
70
71static inline void cursor_undrawn(void)
72{
73    vbl_cursor_cnt = 0;
74    cursor_drawn = 0;
75}
76
77static const char *sticon_startup(void)
78{
79    return "STI console";
80}
81
82static int sticon_set_palette(struct vc_data *c, unsigned char *table)
83{
84    return -EINVAL;
85}
86
87static void sticon_putc(struct vc_data *conp, int c, int ypos, int xpos)
88{
89    int redraw_cursor = 0;
90
91    if (vga_is_gfx || console_blanked)
92	    return;
93
94    if (conp->vc_mode != KD_TEXT)
95    	    return;
96
97    sti_putc(sticon_sti, c, ypos, xpos);
98
99    if (redraw_cursor)
100	    vbl_cursor_cnt = CURSOR_DRAW_DELAY;
101}
102
103static void sticon_putcs(struct vc_data *conp, const unsigned short *s,
104			 int count, int ypos, int xpos)
105{
106    int redraw_cursor = 0;
107
108    if (vga_is_gfx || console_blanked)
109	    return;
110
111    if (conp->vc_mode != KD_TEXT)
112    	    return;
113
114
115    while (count--) {
116	sti_putc(sticon_sti, scr_readw(s++), ypos, xpos++);
117    }
118
119    if (redraw_cursor)
120	    vbl_cursor_cnt = CURSOR_DRAW_DELAY;
121}
122
123static void sticon_cursor(struct vc_data *conp, int mode)
124{
125    unsigned short car1;
126
127    car1 = conp->vc_screenbuf[conp->vc_x + conp->vc_y * conp->vc_cols];
128    switch (mode) {
129    case CM_ERASE:
130	sti_putc(sticon_sti, car1, conp->vc_y, conp->vc_x);
131	break;
132    case CM_MOVE:
133    case CM_DRAW:
134	switch (conp->vc_cursor_type & 0x0f) {
135	case CUR_UNDERLINE:
136	case CUR_LOWER_THIRD:
137	case CUR_LOWER_HALF:
138	case CUR_TWO_THIRDS:
139	case CUR_BLOCK:
140	    sti_putc(sticon_sti, (car1 & 255) + (0 << 8) + (7 << 11),
141		     conp->vc_y, conp->vc_x);
142	    break;
143	}
144	break;
145    }
146}
147
148static int sticon_scroll(struct vc_data *conp, int t, int b, int dir, int count)
149{
150    struct sti_struct *sti = sticon_sti;
151
152    if (vga_is_gfx)
153        return 0;
154
155    sticon_cursor(conp, CM_ERASE);
156
157    switch (dir) {
158    case SM_UP:
159	sti_bmove(sti, t + count, 0, t, 0, b - t - count, conp->vc_cols);
160	sti_clear(sti, b - count, 0, count, conp->vc_cols, conp->vc_video_erase_char);
161	break;
162
163    case SM_DOWN:
164	sti_bmove(sti, t, 0, t + count, 0, b - t - count, conp->vc_cols);
165	sti_clear(sti, t, 0, count, conp->vc_cols, conp->vc_video_erase_char);
166	break;
167    }
168
169    return 0;
170}
171
172static void sticon_bmove(struct vc_data *conp, int sy, int sx,
173	int dy, int dx, int height, int width)
174{
175    if (!width || !height)
176	    return;
177
178    sti_bmove(sticon_sti, sy, sx, dy, dx, height, width);
179}
180
181static void sticon_init(struct vc_data *c, int init)
182{
183    struct sti_struct *sti = sticon_sti;
184    int vc_cols, vc_rows;
185
186    sti_set(sti, 0, 0, sti_onscreen_y(sti), sti_onscreen_x(sti), 0);
187    vc_cols = sti_onscreen_x(sti) / sti->font_width;
188    vc_rows = sti_onscreen_y(sti) / sti->font_height;
189    c->vc_can_do_color = 1;
190
191    if (init) {
192	c->vc_cols = vc_cols;
193	c->vc_rows = vc_rows;
194    } else {
195	/* vc_rows = (c->vc_rows > vc_rows) ? vc_rows : c->vc_rows; */
196	/* vc_cols = (c->vc_cols > vc_cols) ? vc_cols : c->vc_cols; */
197	vc_resize(c, vc_cols, vc_rows);
198/*	vc_resize_con(vc_rows, vc_cols, c->vc_num); */
199    }
200}
201
202static void sticon_deinit(struct vc_data *c)
203{
204}
205
206static void sticon_clear(struct vc_data *conp, int sy, int sx, int height,
207			 int width)
208{
209    if (!height || !width)
210	return;
211
212    sti_clear(sticon_sti, sy, sx, height, width, conp->vc_video_erase_char);
213}
214
215static int sticon_switch(struct vc_data *conp)
216{
217    return 1;	/* needs refreshing */
218}
219
220static int sticon_set_origin(struct vc_data *conp)
221{
222    return 0;
223}
224
225static int sticon_blank(struct vc_data *c, int blank, int mode_switch)
226{
227    if (blank == 0) {
228	if (mode_switch)
229	    vga_is_gfx = 0;
230	return 1;
231    }
232    sticon_set_origin(c);
233    sti_clear(sticon_sti, 0,0, c->vc_rows, c->vc_cols, BLANK);
234    if (mode_switch)
235	vga_is_gfx = 1;
236    return 1;
237}
238
239static int sticon_scrolldelta(struct vc_data *conp, int lines)
240{
241    return 0;
242}
243
244static u16 *sticon_screen_pos(struct vc_data *conp, int offset)
245{
246    int line;
247    unsigned long p;
248
249    if (conp->vc_num != fg_console || !softback_lines)
250    	return (u16 *)(conp->vc_origin + offset);
251    line = offset / conp->vc_size_row;
252    if (line >= softback_lines)
253    	return (u16 *)(conp->vc_origin + offset - softback_lines * conp->vc_size_row);
254    p = softback_curr + offset;
255    if (p >= softback_end)
256    	p += softback_buf - softback_end;
257    return (u16 *)p;
258}
259
260static unsigned long sticon_getxy(struct vc_data *conp, unsigned long pos,
261				  int *px, int *py)
262{
263    int x, y;
264    unsigned long ret;
265    if (pos >= conp->vc_origin && pos < conp->vc_scr_end) {
266    	unsigned long offset = (pos - conp->vc_origin) / 2;
267
268    	x = offset % conp->vc_cols;
269    	y = offset / conp->vc_cols;
270    	if (conp->vc_num == fg_console)
271    	    y += softback_lines;
272    	ret = pos + (conp->vc_cols - x) * 2;
273    } else if (conp->vc_num == fg_console && softback_lines) {
274    	unsigned long offset = pos - softback_curr;
275
276    	if (pos < softback_curr)
277    	    offset += softback_end - softback_buf;
278    	offset /= 2;
279    	x = offset % conp->vc_cols;
280    	y = offset / conp->vc_cols;
281	ret = pos + (conp->vc_cols - x) * 2;
282	if (ret == softback_end)
283	    ret = softback_buf;
284	if (ret == softback_in)
285	    ret = conp->vc_origin;
286    } else {
287    	/* Should not happen */
288    	x = y = 0;
289    	ret = conp->vc_origin;
290    }
291    if (px) *px = x;
292    if (py) *py = y;
293    return ret;
294}
295
296static u8 sticon_build_attr(struct vc_data *conp, u8 color, u8 intens,
297			    u8 blink, u8 underline, u8 reverse, u8 italic)
298{
299    u8 attr = ((color & 0x70) >> 1) | ((color & 7));
300
301    if (reverse) {
302	color = ((color >> 3) & 0x7) | ((color & 0x7) << 3);
303    }
304
305    return attr;
306}
307
308static void sticon_invert_region(struct vc_data *conp, u16 *p, int count)
309{
310    int col = 1; /* vga_can_do_color; */
311
312    while (count--) {
313	u16 a = scr_readw(p);
314
315	if (col)
316		a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
317	else
318		a = ((a & 0x0700) == 0x0100) ? 0x7000 : 0x7700;
319
320	scr_writew(a, p++);
321    }
322}
323
324static void sticon_save_screen(struct vc_data *conp)
325{
326}
327
328static const struct consw sti_con = {
329	.owner			= THIS_MODULE,
330	.con_startup		= sticon_startup,
331	.con_init		= sticon_init,
332	.con_deinit		= sticon_deinit,
333	.con_clear		= sticon_clear,
334	.con_putc		= sticon_putc,
335	.con_putcs		= sticon_putcs,
336	.con_cursor		= sticon_cursor,
337	.con_scroll		= sticon_scroll,
338	.con_bmove		= sticon_bmove,
339	.con_switch		= sticon_switch,
340	.con_blank		= sticon_blank,
341	.con_set_palette	= sticon_set_palette,
342	.con_scrolldelta	= sticon_scrolldelta,
343	.con_set_origin		= sticon_set_origin,
344	.con_save_screen	= sticon_save_screen,
345	.con_build_attr		= sticon_build_attr,
346	.con_invert_region	= sticon_invert_region,
347	.con_screen_pos		= sticon_screen_pos,
348	.con_getxy		= sticon_getxy,
349};
350
351
352
353int __init sticonsole_init(void)
354{
355    /* already initialized ? */
356    if (sticon_sti)
357	 return 0;
358
359    sticon_sti = sti_get_rom(0);
360    if (!sticon_sti)
361	return -ENODEV;
362
363    if (conswitchp == &dummy_con) {
364	printk(KERN_INFO "sticon: Initializing STI text console.\n");
365	return take_over_console(&sti_con, 0, MAX_NR_CONSOLES - 1, 1);
366    }
367    return 0;
368}
369
370module_init(sticonsole_init);
371MODULE_LICENSE("GPL");
372