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