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