import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / video / sti / sticon.c
1 /*
2  *  linux/drivers/video/sti/sticon.c - console driver using HP's STI firmware
3  *
4  *      Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
5  *      Copyright (C) 2001 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/tty.h>
41 #include <linux/console.h>
42 #include <linux/console_struct.h>
43 #include <linux/errno.h>
44 #include <linux/vt_kern.h>
45 #include <linux/kd.h>
46 #include <linux/selection.h>
47
48 #include <asm/io.h>
49
50 #include "sticore.h"
51
52 /* switching to graphics mode */
53 #define BLANK 0
54 static int vga_is_gfx;
55
56
57 /* Software scrollback */
58
59 static unsigned long softback_buf, softback_curr;
60 static unsigned long softback_in;
61 static unsigned long /* softback_top, */ softback_end;
62 static int softback_lines;
63
64
65 /* software cursor */
66
67 static int cursor_drawn;
68 #define CURSOR_DRAW_DELAY               (1)
69 #define DEFAULT_CURSOR_BLINK_RATE       (20)
70
71 static int vbl_cursor_cnt;
72
73 static inline void cursor_undrawn(void)
74 {
75     vbl_cursor_cnt = 0;
76     cursor_drawn = 0;
77 }
78
79
80 static const char *__init sticon_startup(void)
81 {
82     return "STI console";
83 }
84
85 static int sticon_set_palette(struct vc_data *c, unsigned char *table)
86 {
87     return -EINVAL;
88 }
89
90 static int sticon_font_op(struct vc_data *c, struct console_font_op *op)
91 {
92     return -ENOSYS;
93 }
94
95 static void sticon_putc(struct vc_data *conp, int c, int ypos, int xpos)
96 {
97     int unit = conp->vc_num;
98     int redraw_cursor = 0;
99
100     if (vga_is_gfx || console_blanked)
101             return;
102             
103     if (vt_cons[unit]->vc_mode != KD_TEXT)
104             return;
105 #if 0
106     if ((p->cursor_x == xpos) && (p->cursor_y == ypos)) {
107             cursor_undrawn();
108             redraw_cursor = 1;
109     }
110 #endif
111
112     sti_putc(default_sti, c, ypos, xpos);
113
114     if (redraw_cursor)
115             vbl_cursor_cnt = CURSOR_DRAW_DELAY;
116 }
117
118 static void sticon_putcs(struct vc_data *conp, const unsigned short *s,
119                          int count, int ypos, int xpos)
120 {
121     int unit = conp->vc_num;
122     int redraw_cursor = 0;
123
124     if (vga_is_gfx || console_blanked)
125             return;
126
127     if (vt_cons[unit]->vc_mode != KD_TEXT)
128             return;
129     
130 #if 0
131     if ((p->cursor_y == ypos) && (xpos <= p->cursor_x) &&
132         (p->cursor_x < (xpos + count))) {
133             cursor_undrawn();
134             redraw_cursor = 1;
135     }
136 #endif
137
138     while (count--) {
139         sti_putc(default_sti, scr_readw(s++), ypos, xpos++);
140     }
141
142     if (redraw_cursor)
143             vbl_cursor_cnt = CURSOR_DRAW_DELAY;
144 }
145
146 static void sticon_cursor(struct vc_data *conp, int mode)
147 {
148     unsigned short car1;
149
150     car1 = conp->vc_screenbuf[conp->vc_x + conp->vc_y * conp->vc_cols];
151     switch (mode) {
152     case CM_ERASE:
153         sti_putc(default_sti, car1, conp->vc_y, conp->vc_x);
154         break;
155     case CM_MOVE:
156     case CM_DRAW:
157         switch (conp->vc_cursor_type & 0x0f) {
158         case CUR_UNDERLINE:
159         case CUR_LOWER_THIRD:
160         case CUR_LOWER_HALF:
161         case CUR_TWO_THIRDS:
162         case CUR_BLOCK:
163             sti_putc(default_sti, (car1 & 255) + (0 << 8) + (7 << 11),
164                      conp->vc_y, conp->vc_x);
165             break;
166         }
167         break;
168     }
169 }
170
171 static int
172 sticon_scroll(struct vc_data *conp, int t, int b, int dir, int count)
173 {
174     struct sti_struct *sti = default_sti;
175
176     if (vga_is_gfx)
177         return 0;
178
179     sticon_cursor(conp, CM_ERASE);
180
181     switch (dir) {
182     case SM_UP:
183         sti_bmove(sti, t + count, 0, t, 0, b - t - count, conp->vc_cols);
184         sti_clear(sti, b - count, 0, count, conp->vc_cols, conp->vc_video_erase_char);
185         break;
186
187     case SM_DOWN:
188         sti_bmove(sti, t, 0, t + count, 0, b - t - count, conp->vc_cols);
189         sti_clear(sti, t, 0, count, conp->vc_cols, conp->vc_video_erase_char);
190         break;
191     }
192
193     return 0;
194 }
195
196 static void
197 sticon_bmove(struct vc_data *conp, int sy, int sx, int dy, int dx,
198              int height, int width)
199 {
200     if (!width || !height)
201             return;
202 #if 0
203     if (((sy <= p->cursor_y) && (p->cursor_y < sy+height) &&
204         (sx <= p->cursor_x) && (p->cursor_x < sx+width)) ||
205         ((dy <= p->cursor_y) && (p->cursor_y < dy+height) &&
206         (dx <= p->cursor_x) && (p->cursor_x < dx+width)))
207                 sticon_cursor(p, CM_ERASE /*|CM_SOFTBACK*/);
208 #endif
209
210     sti_bmove(default_sti, sy, sx, dy, dx, height, width);
211 }
212
213 static void sticon_init(struct vc_data *c, int init)
214 {
215     struct sti_struct *sti = default_sti;
216     int vc_cols, vc_rows;
217
218     sti_set(sti, 0, 0, sti_onscreen_y(sti), sti_onscreen_x(sti), 0);
219     vc_cols = sti_onscreen_x(sti) / sti->font_width;
220     vc_rows = sti_onscreen_y(sti) / sti->font_height;
221     c->vc_can_do_color = 1;
222     
223     if (init) {
224         c->vc_cols = vc_cols;
225         c->vc_rows = vc_rows;
226     } else {
227         /* vc_rows = (c->vc_rows > vc_rows) ? vc_rows : c->vc_rows; */
228         /* vc_cols = (c->vc_cols > vc_cols) ? vc_cols : c->vc_cols; */
229         vc_resize_con(vc_rows, vc_cols, c->vc_num);
230     }
231 }
232
233 static void sticon_deinit(struct vc_data *c)
234 {
235 }
236
237 static void sticon_clear(struct vc_data *conp, int sy, int sx, int height,
238                          int width)
239 {
240     if (!height || !width)
241         return;
242
243     sti_clear(default_sti, sy, sx, height, width, conp->vc_video_erase_char);
244 }
245
246 static int sticon_switch(struct vc_data *conp)
247 {
248     return 1;   /* needs refreshing */
249 }
250
251 static int sticon_set_origin(struct vc_data *conp)
252 {
253     return 0;
254 }
255
256 static int sticon_blank(struct vc_data *c, int blank)
257 {
258     switch (blank) {
259     case 0:             /* unblank */
260         vga_is_gfx = 0;
261         /* Tell console.c that it has to restore the screen itself */
262         return 1;
263     case 1:             /* normal blanking */
264     default:            /* VESA blanking */
265         if (vga_is_gfx)
266                 return 0;
267         sticon_set_origin(c);
268         sti_clear(default_sti, 0,0, c->vc_rows, c->vc_cols, BLANK);
269         return 1;
270     case -1:            /* Entering graphic mode */
271         sti_clear(default_sti, 0,0, c->vc_rows, c->vc_cols, BLANK);
272         vga_is_gfx = 1;
273         return 1;
274     }
275     return 1;           /* console needs to restore screen itself */
276 }
277
278 static int sticon_scrolldelta(struct vc_data *conp, int lines)
279 {
280     return 0;
281 }
282
283 static u16 *sticon_screen_pos(struct vc_data *conp, int offset)
284 {
285     int line;
286     unsigned long p;
287
288     if (conp->vc_num != fg_console || !softback_lines)
289         return (u16 *)(conp->vc_origin + offset);
290     line = offset / conp->vc_size_row;
291     if (line >= softback_lines)
292         return (u16 *)(conp->vc_origin + offset - softback_lines * conp->vc_size_row);
293     p = softback_curr + offset;
294     if (p >= softback_end)
295         p += softback_buf - softback_end;
296     return (u16 *)p;
297 }
298
299 static unsigned long sticon_getxy(struct vc_data *conp, unsigned long pos,
300                                   int *px, int *py)
301 {
302     int x, y;
303     unsigned long ret;
304     if (pos >= conp->vc_origin && pos < conp->vc_scr_end) {
305         unsigned long offset = (pos - conp->vc_origin) / 2;
306         
307         x = offset % conp->vc_cols;
308         y = offset / conp->vc_cols;
309         if (conp->vc_num == fg_console)
310             y += softback_lines;
311         ret = pos + (conp->vc_cols - x) * 2;
312     } else if (conp->vc_num == fg_console && softback_lines) {
313         unsigned long offset = pos - softback_curr;
314         
315         if (pos < softback_curr)
316             offset += softback_end - softback_buf;
317         offset /= 2;
318         x = offset % conp->vc_cols;
319         y = offset / conp->vc_cols;
320         ret = pos + (conp->vc_cols - x) * 2;
321         if (ret == softback_end)
322             ret = softback_buf;
323         if (ret == softback_in)
324             ret = conp->vc_origin;
325     } else {
326         /* Should not happen */
327         x = y = 0;
328         ret = conp->vc_origin;
329     }
330     if (px) *px = x;
331     if (py) *py = y;
332     return ret;
333 }
334
335 static u8 sticon_build_attr(struct vc_data *conp, u8 color, u8 intens,
336                             u8 blink, u8 underline, u8 reverse)
337 {
338     u8 attr = ((color & 0x70) >> 1) | ((color & 7));
339
340     if (reverse) {
341         color = ((color >> 3) & 0x7) | ((color & 0x7) << 3);
342     }
343
344     return attr;
345 }
346
347 void sticon_invert_region(struct vc_data *conp, u16 *p, int count)
348 {
349     int col = 1; /* vga_can_do_color; */
350
351     while (count--) {
352         u16 a = scr_readw(p);
353
354         if (col)
355                 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
356         else
357                 a = ((a & 0x0700) == 0x0100) ? 0x7000 : 0x7700;
358
359         scr_writew(a, p++);
360     }
361 }
362
363 void sticon_save_screen(struct vc_data *conp)
364 {
365 }
366
367 struct consw sti_con = {
368     con_startup:        sticon_startup,
369     con_init:           sticon_init,
370     con_deinit:         sticon_deinit,
371     con_clear:          sticon_clear,
372     con_putc:           sticon_putc,
373     con_putcs:          sticon_putcs,
374     con_cursor:         sticon_cursor,
375     con_scroll:         sticon_scroll,
376     con_bmove:          sticon_bmove,
377     con_switch:         sticon_switch,
378     con_blank:          sticon_blank,
379     con_font_op:        sticon_font_op,
380     con_set_palette:    sticon_set_palette,
381     con_scrolldelta:    sticon_scrolldelta,
382     con_set_origin:     sticon_set_origin,
383     con_save_screen:    sticon_save_screen, 
384     con_build_attr:     sticon_build_attr,
385     con_invert_region:  sticon_invert_region, 
386     con_screen_pos:     sticon_screen_pos,
387     con_getxy:          sticon_getxy,
388 };
389
390
391
392 int __init sticonsole_init(void)
393 {
394     if (sti_init_roms()) {
395         if (conswitchp == &dummy_con) {
396                 printk(KERN_INFO "sticon: Initializing STI text console.\n");
397                 take_over_console(&sti_con, 0, MAX_NR_CONSOLES - 1, 1);
398         }
399         return 0;
400     } else
401         return -ENODEV;
402 }
403
404 module_init(sticonsole_init);